]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/ipcp.c
update to 2.3 based on netbsd stuff
[ppp.git] / pppd / ipcp.c
index fea0c7a3e90edd94ca68683b32fa567891ec55f2..d8f1933ba98ac8dd3542d819ab2016ea798570ac 100644 (file)
@@ -18,7 +18,7 @@
  */
 
 #ifndef lint
-static char rcsid[] = "$Id: ipcp.c,v 1.23 1996/01/01 22:56:53 paulus Exp $";
+static char rcsid[] = "$Id: ipcp.c,v 1.26 1996/07/01 01:13:53 paulus Exp $";
 #endif
 
 /*
@@ -29,9 +29,13 @@ static char rcsid[] = "$Id: ipcp.c,v 1.23 1996/01/01 22:56:53 paulus Exp $";
 #include <string.h>
 #include <syslog.h>
 #include <netdb.h>
+#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
 
 #include "pppd.h"
 #include "fsm.h"
@@ -84,11 +88,38 @@ static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
     "IPCP"                     /* String name of protocol */
 };
 
+/*
+ * Protocol entry points from main code.
+ */
+static void ipcp_init __P((int));
+static void ipcp_open __P((int));
+static void ipcp_close __P((int, char *));
+static void ipcp_lowerup __P((int));
+static void ipcp_lowerdown __P((int));
+static void ipcp_input __P((int, u_char *, int));
+static void ipcp_protrej __P((int));
+static int  ipcp_printpkt __P((u_char *, int,
+                              void (*) __P((void *, char *, ...)), void *));
+static void ip_check_options __P((void));
+static int  ip_demand_conf __P((int));
+static int  ip_active_pkt __P((u_char *, int));
+
 struct protent ipcp_protent = {
-    PPP_IPCP, ipcp_init, ipcp_input, ipcp_protrej,
-    ipcp_lowerup, ipcp_lowerdown, ipcp_open, ipcp_close,
-    ipcp_printpkt, NULL, 1, "IPCP",
-    ip_check_options, ip_demand_conf,
+    PPP_IPCP,
+    ipcp_init,
+    ipcp_input,
+    ipcp_protrej,
+    ipcp_lowerup,
+    ipcp_lowerdown,
+    ipcp_open,
+    ipcp_close,
+    ipcp_printpkt,
+    NULL,
+    1,
+    "IPCP",
+    ip_check_options,
+    ip_demand_conf,
+    ip_active_pkt
 };
 
 /*
@@ -128,7 +159,7 @@ u_int32_t ipaddr;
 /*
  * ipcp_init - Initialize IPCP.
  */
-void
+static void
 ipcp_init(unit)
     int unit;
 {
@@ -171,7 +202,7 @@ ipcp_init(unit)
 /*
  * ipcp_open - IPCP is allowed to come up.
  */
-void
+static void
 ipcp_open(unit)
     int unit;
 {
@@ -182,7 +213,7 @@ ipcp_open(unit)
 /*
  * ipcp_close - Take IPCP down.
  */
-void
+static void
 ipcp_close(unit, reason)
     int unit;
     char *reason;
@@ -194,7 +225,7 @@ ipcp_close(unit, reason)
 /*
  * ipcp_lowerup - The lower layer is up.
  */
-void
+static void
 ipcp_lowerup(unit)
     int unit;
 {
@@ -205,7 +236,7 @@ ipcp_lowerup(unit)
 /*
  * ipcp_lowerdown - The lower layer is down.
  */
-void
+static void
 ipcp_lowerdown(unit)
     int unit;
 {
@@ -216,7 +247,7 @@ ipcp_lowerdown(unit)
 /*
  * ipcp_input - Input IPCP packet.
  */
-void
+static void
 ipcp_input(unit, p, len)
     int unit;
     u_char *p;
@@ -231,7 +262,7 @@ ipcp_input(unit, p, len)
  *
  * Pretend the lower layer went down, so we shut up.
  */
-void
+static void
 ipcp_protrej(unit)
     int unit;
 {
@@ -266,10 +297,36 @@ ipcp_cilen(f)
     fsm *f;
 {
     ipcp_options *go = &ipcp_gotoptions[f->unit];
+    ipcp_options *wo = &ipcp_wantoptions[f->unit];
+    ipcp_options *ho = &ipcp_hisoptions[f->unit];
 
 #define LENCIVJ(neg, old)      (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
 #define LENCIADDR(neg, old)    (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
 
+    /*
+     * First see if we want to change our options to the old
+     * forms because we have received old forms from the peer.
+     */
+    if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
+       /* use the old style of address negotiation */
+       go->neg_addr = 1;
+       go->old_addrs = 1;
+    }
+    if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
+       /* try an older style of VJ negotiation */
+       if (cis_received[f->unit] == 0) {
+           /* keep trying the new style until we see some CI from the peer */
+           go->neg_vj = 1;
+       } else {
+           /* use the old style only if the peer did */
+           if (ho->neg_vj && ho->old_vj) {
+               go->neg_vj = 1;
+               go->old_vj = 1;
+               go->vj_protocol = ho->vj_protocol;
+           }
+       }
+    }
+
     return (LENCIADDR(go->neg_addr, go->old_addrs) +
            LENCIVJ(go->neg_vj, go->old_vj));
 }
@@ -284,9 +341,7 @@ ipcp_addci(f, ucp, lenp)
     u_char *ucp;
     int *lenp;
 {
-    ipcp_options *wo = &ipcp_wantoptions[f->unit];
     ipcp_options *go = &ipcp_gotoptions[f->unit];
-    ipcp_options *ho = &ipcp_hisoptions[f->unit];
     int len = *lenp;
 
 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
@@ -323,30 +378,6 @@ ipcp_addci(f, ucp, lenp)
            neg = 0; \
     }
 
-    /*
-     * First see if we want to change our options to the old
-     * forms because we have received old forms from the peer.
-     */
-    if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
-       /* use the old style of address negotiation */
-       go->neg_addr = 1;
-       go->old_addrs = 1;
-    }
-    if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
-       /* try an older style of VJ negotiation */
-       if (cis_received[f->unit] == 0) {
-           /* keep trying the new style until we see some CI from the peer */
-           go->neg_vj = 1;
-       } else {
-           /* use the old style only if the peer did */
-           if (ho->neg_vj && ho->old_vj) {
-               go->neg_vj = 1;
-               go->old_vj = 1;
-               go->vj_protocol = ho->vj_protocol;
-           }
-       }
-    }
-
     ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
              go->old_addrs, go->ouraddr, go->hisaddr);
 
@@ -741,6 +772,9 @@ ipcp_reqci(f, inp, len, reject_if_disagree)
     u_char *ucp = inp;         /* Pointer to current output char */
     int l = *len;              /* Length left */
     u_char maxslotindex, cflag;
+    int d;
+
+    cis_received[f->unit] = 1;
 
     /*
      * Reset all his options.
@@ -868,45 +902,26 @@ ipcp_reqci(f, inp, len, reject_if_disagree)
            ho->hisaddr = ciaddr1;
            break;
 
-#ifdef USE_MS_DNS
        case CI_MS_DNS1:
-           /* Microsoft primary DNS request */
-           IPCPDEBUG((LOG_INFO, "ipcp: received DNS1 Request "));
-
-           /* If we do not have a DNS address then we cannot send it */
-           if (ao->dnsaddr[0] == 0 ||
-               cilen != CILEN_ADDR) {  /* Check CI length */
-               orc = CONFREJ;          /* Reject CI */
-               break;
-           }
-           GETLONG(tl, p);
-           if (htonl(tl) != ao->dnsaddr[0]) {
-                DECPTR(sizeof(u_int32_t), p);
-               tl = ntohl(ao->dnsaddr[0]);
-               PUTLONG(tl, p);
-               orc = CONFNAK;
-            }
-            break;
-
        case CI_MS_DNS2:
-           /* Microsoft secondary DNS request */
-           IPCPDEBUG((LOG_INFO, "ipcp: received DNS2 Request "));
+           /* Microsoft primary or secondary DNS request */
+           d = citype == CI_MS_DNS2;
+           IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
 
            /* If we do not have a DNS address then we cannot send it */
-           if (ao->dnsaddr[1] == 0 ||  /* Yes, this is the first one! */
+           if (ao->dnsaddr[d] == 0 ||
                cilen != CILEN_ADDR) {  /* Check CI length */
                orc = CONFREJ;          /* Reject CI */
                break;
            }
            GETLONG(tl, p);
-           if (htonl(tl) != ao->dnsaddr[1]) { /* and this is the 2nd one */
+           if (htonl(tl) != ao->dnsaddr[d]) {
                 DECPTR(sizeof(u_int32_t), p);
-               tl = ntohl(ao->dnsaddr[1]);
+               tl = ntohl(ao->dnsaddr[d]);
                PUTLONG(tl, p);
                orc = CONFNAK;
             }
             break;
-#endif
        
        case CI_COMPRESSTYPE:
            IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
@@ -1021,7 +1036,7 @@ endswitch:
  * ip_check_options - check that any IP-related options are OK,
  * and assign appropriate defaults.
  */
-void
+static void
 ip_check_options()
 {
     struct hostent *hp;
@@ -1047,8 +1062,13 @@ ip_check_options()
     }
 
     if (demand && wo->hisaddr == 0) {
-       fprintf(stderr,
-               "Remote IP address must be specified for demand-dialling\n");
+       fprintf(stderr, "%s: remote IP address required for demand-dialling\n",
+               progname);
+       exit(1);
+    }
+    if (demand && wo->accept_remote) {
+       fprintf(stderr, "%s: ipcp-accept-remote is incompatible with demand\n",
+               progname);
        exit(1);
     }
 }
@@ -1058,7 +1078,7 @@ ip_check_options()
  * ip_demand_conf - configure the interface as though
  * IPCP were up, for use with dial-on-demand.
  */
-int
+static int
 ip_demand_conf(u)
     int u;
 {
@@ -1144,9 +1164,11 @@ ipcp_up(f)
        }
        demand_rexmit(PPP_IP);
        sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
-
-    } else {
-
+    }
+#ifndef _linux_  /* Linux destroys routes when the device goes down. */
+    else         /* always use the code which adds the routes. */
+#endif
+    {
        /*
         * Set IP addresses and (if specified) netmask.
         */
@@ -1163,6 +1185,7 @@ ipcp_up(f)
            ipcp_close(f->unit, "Interface configuration failed");
            return;
        }
+       sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
 
        /* assign a default route through the interface if required */
        if (ipcp_wantoptions[f->unit].default_route) 
@@ -1270,16 +1293,16 @@ ipcp_script(f, script)
 /*
  * ipcp_printpkt - print the contents of an IPCP packet.
  */
-char *ipcp_codenames[] = {
+static char *ipcp_codenames[] = {
     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
     "TermReq", "TermAck", "CodeRej"
 };
 
-int
+static int
 ipcp_printpkt(p, plen, printer, arg)
     u_char *p;
     int plen;
-    void (*printer)();
+    void (*printer) __P((void *, char *, ...));
     void *arg;
 {
     int code, id, len, olen;
@@ -1360,6 +1383,16 @@ ipcp_printpkt(p, plen, printer, arg)
            printer(arg, ">");
        }
        break;
+
+    case TERMACK:
+    case TERMREQ:
+       if (len > 0 && *p >= ' ' && *p < 0x7f) {
+           printer(arg, " ");
+           print_string(p, len, printer, arg);
+           p += len;
+           len = 0;
+       }
+       break;
     }
 
     /* print the rest of the bytes in the packet */
@@ -1370,3 +1403,37 @@ ipcp_printpkt(p, plen, printer, arg)
 
     return p - pstart;
 }
+
+/*
+ * ip_active_pkt - see if this IP packet is worth bringing the link up for.
+ * We don't bring the link up for IP fragments or for TCP FIN packets
+ * with no data.
+ */
+#ifndef IP_OFFMASK
+#define IP_OFFMASK     0x1fff
+#endif
+
+static int
+ip_active_pkt(pkt, len)
+    u_char *pkt;
+    int len;
+{
+    struct ip *ip;
+    struct tcphdr *tcp;
+    int hlen;
+
+    if (len < sizeof(struct ip) + PPP_HDRLEN)
+       return 0;
+    ip = (struct ip *) (pkt + PPP_HDRLEN);
+    if ((ntohs(ip->ip_off) & IP_OFFMASK) != 0)
+       return 0;
+    if (ip->ip_p != IPPROTO_TCP)
+       return 1;
+    hlen = ip->ip_hl * 4;
+    if (len < hlen + sizeof(struct tcphdr) + PPP_HDRLEN)
+       return 0;
+    tcp = (struct tcphdr *) (pkt + PPP_HDRLEN + hlen);
+    if ((tcp->th_flags & TH_FIN) != 0 && hlen + tcp->th_off * 4 == len)
+       return 0;
+    return 1;
+}