Home | History | Annotate | Download | only in pppd
      1 /*
      2  * ipcp.c - PPP IP Control Protocol.
      3  *
      4  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  *
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in
     15  *    the documentation and/or other materials provided with the
     16  *    distribution.
     17  *
     18  * 3. The name "Carnegie Mellon University" must not be used to
     19  *    endorse or promote products derived from this software without
     20  *    prior written permission. For permission or any legal
     21  *    details, please contact
     22  *      Office of Technology Transfer
     23  *      Carnegie Mellon University
     24  *      5000 Forbes Avenue
     25  *      Pittsburgh, PA  15213-3890
     26  *      (412) 268-4387, fax: (412) 268-7395
     27  *      tech-transfer (at) andrew.cmu.edu
     28  *
     29  * 4. Redistributions of any form whatsoever must retain the following
     30  *    acknowledgment:
     31  *    "This product includes software developed by Computing Services
     32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
     33  *
     34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
     35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
     37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
     39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     41  */
     42 
     43 #define RCSID	"$Id: ipcp.c,v 1.73 2008/05/26 08:33:22 paulus Exp $"
     44 
     45 /*
     46  * TODO:
     47  */
     48 
     49 #include <stdio.h>
     50 #include <string.h>
     51 #include <stdlib.h>
     52 #include <netdb.h>
     53 #include <sys/param.h>
     54 #include <sys/types.h>
     55 #include <sys/socket.h>
     56 #include <netinet/in.h>
     57 #include <arpa/inet.h>
     58 
     59 #include "pppd.h"
     60 #include "fsm.h"
     61 #include "ipcp.h"
     62 #include "pathnames.h"
     63 
     64 static const char rcsid[] = RCSID;
     65 
     66 /* global vars */
     67 ipcp_options ipcp_wantoptions[NUM_PPP];	/* Options that we want to request */
     68 ipcp_options ipcp_gotoptions[NUM_PPP];	/* Options that peer ack'd */
     69 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
     70 ipcp_options ipcp_hisoptions[NUM_PPP];	/* Options that we ack'd */
     71 
     72 u_int32_t netmask = 0;		/* IP netmask to set on interface */
     73 
     74 bool	disable_defaultip = 0;	/* Don't use hostname for default IP adrs */
     75 bool	noremoteip = 0;		/* Let him have no IP address */
     76 
     77 /* Hook for a plugin to know when IP protocol has come up */
     78 void (*ip_up_hook) __P((void)) = NULL;
     79 
     80 /* Hook for a plugin to know when IP protocol has come down */
     81 void (*ip_down_hook) __P((void)) = NULL;
     82 
     83 /* Hook for a plugin to choose the remote IP address */
     84 void (*ip_choose_hook) __P((u_int32_t *)) = NULL;
     85 
     86 /* Notifiers for when IPCP goes up and down */
     87 struct notifier *ip_up_notifier = NULL;
     88 struct notifier *ip_down_notifier = NULL;
     89 
     90 /* local vars */
     91 static int default_route_set[NUM_PPP];	/* Have set up a default route */
     92 static int proxy_arp_set[NUM_PPP];	/* Have created proxy arp entry */
     93 static bool usepeerdns;			/* Ask peer for DNS addrs */
     94 static int ipcp_is_up;			/* have called np_up() */
     95 static int ipcp_is_open;		/* haven't called np_finished() */
     96 static bool ask_for_local;		/* request our address from peer */
     97 static char vj_value[8];		/* string form of vj option value */
     98 static char netmask_str[20];		/* string form of netmask value */
     99 
    100 /*
    101  * Callbacks for fsm code.  (CI = Configuration Information)
    102  */
    103 static void ipcp_resetci __P((fsm *));	/* Reset our CI */
    104 static int  ipcp_cilen __P((fsm *));	        /* Return length of our CI */
    105 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
    106 static int  ipcp_ackci __P((fsm *, u_char *, int));	/* Peer ack'd our CI */
    107 static int  ipcp_nakci __P((fsm *, u_char *, int, int));/* Peer nak'd our CI */
    108 static int  ipcp_rejci __P((fsm *, u_char *, int));	/* Peer rej'd our CI */
    109 static int  ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
    110 static void ipcp_up __P((fsm *));		/* We're UP */
    111 static void ipcp_down __P((fsm *));		/* We're DOWN */
    112 static void ipcp_finished __P((fsm *));	/* Don't need lower layer */
    113 
    114 fsm ipcp_fsm[NUM_PPP];		/* IPCP fsm structure */
    115 
    116 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
    117     ipcp_resetci,		/* Reset our Configuration Information */
    118     ipcp_cilen,			/* Length of our Configuration Information */
    119     ipcp_addci,			/* Add our Configuration Information */
    120     ipcp_ackci,			/* ACK our Configuration Information */
    121     ipcp_nakci,			/* NAK our Configuration Information */
    122     ipcp_rejci,			/* Reject our Configuration Information */
    123     ipcp_reqci,			/* Request peer's Configuration Information */
    124     ipcp_up,			/* Called when fsm reaches OPENED state */
    125     ipcp_down,			/* Called when fsm leaves OPENED state */
    126     NULL,			/* Called when we want the lower layer up */
    127     ipcp_finished,		/* Called when we want the lower layer down */
    128     NULL,			/* Called when Protocol-Reject received */
    129     NULL,			/* Retransmission is necessary */
    130     NULL,			/* Called to handle protocol-specific codes */
    131     "IPCP"			/* String name of protocol */
    132 };
    133 
    134 /*
    135  * Command-line options.
    136  */
    137 static int setvjslots __P((char **));
    138 static int setdnsaddr __P((char **));
    139 static int setwinsaddr __P((char **));
    140 static int setnetmask __P((char **));
    141 int setipaddr __P((char *, char **, int));
    142 static void printipaddr __P((option_t *, void (*)(void *, char *,...),void *));
    143 
    144 static option_t ipcp_option_list[] = {
    145     { "noip", o_bool, &ipcp_protent.enabled_flag,
    146       "Disable IP and IPCP" },
    147     { "-ip", o_bool, &ipcp_protent.enabled_flag,
    148       "Disable IP and IPCP", OPT_ALIAS },
    149 
    150     { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
    151       "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
    152     { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
    153       "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
    154       &ipcp_allowoptions[0].neg_vj },
    155 
    156     { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
    157       "Disable VJ connection-ID compression", OPT_A2CLR,
    158       &ipcp_allowoptions[0].cflag },
    159     { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
    160       "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
    161       &ipcp_allowoptions[0].cflag },
    162 
    163     { "vj-max-slots", o_special, (void *)setvjslots,
    164       "Set maximum VJ header slots",
    165       OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
    166 
    167     { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
    168       "Accept peer's address for us", 1 },
    169     { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
    170       "Accept peer's address for it", 1 },
    171 
    172     { "ipparam", o_string, &ipparam,
    173       "Set ip script parameter", OPT_PRIO },
    174 
    175     { "noipdefault", o_bool, &disable_defaultip,
    176       "Don't use name for default IP adrs", 1 },
    177 
    178     { "ms-dns", 1, (void *)setdnsaddr,
    179       "DNS address for the peer's use" },
    180     { "ms-wins", 1, (void *)setwinsaddr,
    181       "Nameserver for SMB over TCP/IP for peer" },
    182 
    183     { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
    184       "Set timeout for IPCP", OPT_PRIO },
    185     { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
    186       "Set max #xmits for term-reqs", OPT_PRIO },
    187     { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
    188       "Set max #xmits for conf-reqs", OPT_PRIO },
    189     { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
    190       "Set max #conf-naks for IPCP", OPT_PRIO },
    191 
    192     { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
    193       "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
    194     { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
    195       "disable defaultroute option", OPT_A2CLR,
    196       &ipcp_wantoptions[0].default_route },
    197     { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
    198       "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
    199       &ipcp_wantoptions[0].default_route },
    200 
    201     { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
    202       "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
    203     { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
    204       "disable proxyarp option", OPT_A2CLR,
    205       &ipcp_wantoptions[0].proxy_arp },
    206     { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
    207       "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
    208       &ipcp_wantoptions[0].proxy_arp },
    209 
    210     { "usepeerdns", o_bool, &usepeerdns,
    211       "Ask peer for DNS address(es)", 1 },
    212 
    213     { "netmask", o_special, (void *)setnetmask,
    214       "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
    215 
    216     { "ipcp-no-addresses", o_bool, &ipcp_wantoptions[0].old_addrs,
    217       "Disable old-style IP-Addresses usage", OPT_A2CLR,
    218       &ipcp_allowoptions[0].old_addrs },
    219     { "ipcp-no-address", o_bool, &ipcp_wantoptions[0].neg_addr,
    220       "Disable IP-Address usage", OPT_A2CLR,
    221       &ipcp_allowoptions[0].neg_addr },
    222 #ifdef __linux__
    223     { "noremoteip", o_bool, &noremoteip,
    224       "Allow peer to have no IP address", 1 },
    225 #endif
    226     { "nosendip", o_bool, &ipcp_wantoptions[0].neg_addr,
    227       "Don't send our IP address to peer", OPT_A2CLR,
    228       &ipcp_wantoptions[0].old_addrs},
    229 
    230     { "IP addresses", o_wild, (void *) &setipaddr,
    231       "set local and remote IP addresses",
    232       OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
    233 
    234     { NULL }
    235 };
    236 
    237 /*
    238  * Protocol entry points from main code.
    239  */
    240 static void ipcp_init __P((int));
    241 static void ipcp_open __P((int));
    242 static void ipcp_close __P((int, char *));
    243 static void ipcp_lowerup __P((int));
    244 static void ipcp_lowerdown __P((int));
    245 static void ipcp_input __P((int, u_char *, int));
    246 static void ipcp_protrej __P((int));
    247 static int  ipcp_printpkt __P((u_char *, int,
    248 			       void (*) __P((void *, char *, ...)), void *));
    249 static void ip_check_options __P((void));
    250 static int  ip_demand_conf __P((int));
    251 static int  ip_active_pkt __P((u_char *, int));
    252 static void create_resolv __P((u_int32_t, u_int32_t));
    253 
    254 struct protent ipcp_protent = {
    255     PPP_IPCP,
    256     ipcp_init,
    257     ipcp_input,
    258     ipcp_protrej,
    259     ipcp_lowerup,
    260     ipcp_lowerdown,
    261     ipcp_open,
    262     ipcp_close,
    263     ipcp_printpkt,
    264     NULL,
    265     1,
    266     "IPCP",
    267     "IP",
    268     ipcp_option_list,
    269     ip_check_options,
    270     ip_demand_conf,
    271     ip_active_pkt
    272 };
    273 
    274 static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t));
    275 static void ipcp_script __P((char *, int));	/* Run an up/down script */
    276 static void ipcp_script_done __P((void *));
    277 
    278 /*
    279  * Lengths of configuration options.
    280  */
    281 #define CILEN_VOID	2
    282 #define CILEN_COMPRESS	4	/* min length for compression protocol opt. */
    283 #define CILEN_VJ	6	/* length for RFC1332 Van-Jacobson opt. */
    284 #define CILEN_ADDR	6	/* new-style single address option */
    285 #define CILEN_ADDRS	10	/* old-style dual address option */
    286 
    287 
    288 #define CODENAME(x)	((x) == CONFACK ? "ACK" : \
    289 			 (x) == CONFNAK ? "NAK" : "REJ")
    290 
    291 /*
    292  * This state variable is used to ensure that we don't
    293  * run an ipcp-up/down script while one is already running.
    294  */
    295 static enum script_state {
    296     s_down,
    297     s_up,
    298 } ipcp_script_state;
    299 static pid_t ipcp_script_pid;
    300 
    301 /*
    302  * Make a string representation of a network IP address.
    303  */
    304 char *
    305 ip_ntoa(ipaddr)
    306 u_int32_t ipaddr;
    307 {
    308     static char b[64];
    309 
    310     slprintf(b, sizeof(b), "%I", ipaddr);
    311     return b;
    312 }
    313 
    314 /*
    315  * Option parsing.
    316  */
    317 
    318 /*
    319  * setvjslots - set maximum number of connection slots for VJ compression
    320  */
    321 static int
    322 setvjslots(argv)
    323     char **argv;
    324 {
    325     int value;
    326 
    327     if (!int_option(*argv, &value))
    328 	return 0;
    329     if (value < 2 || value > 16) {
    330 	option_error("vj-max-slots value must be between 2 and 16");
    331 	return 0;
    332     }
    333     ipcp_wantoptions [0].maxslotindex =
    334         ipcp_allowoptions[0].maxslotindex = value - 1;
    335     slprintf(vj_value, sizeof(vj_value), "%d", value);
    336     return 1;
    337 }
    338 
    339 /*
    340  * setdnsaddr - set the dns address(es)
    341  */
    342 static int
    343 setdnsaddr(argv)
    344     char **argv;
    345 {
    346     u_int32_t dns;
    347     struct hostent *hp;
    348 
    349     dns = inet_addr(*argv);
    350     if (dns == (u_int32_t) -1) {
    351 	if ((hp = gethostbyname(*argv)) == NULL) {
    352 	    option_error("invalid address parameter '%s' for ms-dns option",
    353 			 *argv);
    354 	    return 0;
    355 	}
    356 	dns = *(u_int32_t *)hp->h_addr;
    357     }
    358 
    359     /* We take the last 2 values given, the 2nd-last as the primary
    360        and the last as the secondary.  If only one is given it
    361        becomes both primary and secondary. */
    362     if (ipcp_allowoptions[0].dnsaddr[1] == 0)
    363 	ipcp_allowoptions[0].dnsaddr[0] = dns;
    364     else
    365 	ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
    366 
    367     /* always set the secondary address value. */
    368     ipcp_allowoptions[0].dnsaddr[1] = dns;
    369 
    370     return (1);
    371 }
    372 
    373 /*
    374  * setwinsaddr - set the wins address(es)
    375  * This is primrarly used with the Samba package under UNIX or for pointing
    376  * the caller to the existing WINS server on a Windows NT platform.
    377  */
    378 static int
    379 setwinsaddr(argv)
    380     char **argv;
    381 {
    382     u_int32_t wins;
    383     struct hostent *hp;
    384 
    385     wins = inet_addr(*argv);
    386     if (wins == (u_int32_t) -1) {
    387 	if ((hp = gethostbyname(*argv)) == NULL) {
    388 	    option_error("invalid address parameter '%s' for ms-wins option",
    389 			 *argv);
    390 	    return 0;
    391 	}
    392 	wins = *(u_int32_t *)hp->h_addr;
    393     }
    394 
    395     /* We take the last 2 values given, the 2nd-last as the primary
    396        and the last as the secondary.  If only one is given it
    397        becomes both primary and secondary. */
    398     if (ipcp_allowoptions[0].winsaddr[1] == 0)
    399 	ipcp_allowoptions[0].winsaddr[0] = wins;
    400     else
    401 	ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
    402 
    403     /* always set the secondary address value. */
    404     ipcp_allowoptions[0].winsaddr[1] = wins;
    405 
    406     return (1);
    407 }
    408 
    409 /*
    410  * setipaddr - Set the IP address
    411  * If doit is 0, the call is to check whether this option is
    412  * potentially an IP address specification.
    413  * Not static so that plugins can call it to set the addresses
    414  */
    415 int
    416 setipaddr(arg, argv, doit)
    417     char *arg;
    418     char **argv;
    419     int doit;
    420 {
    421     struct hostent *hp;
    422     char *colon;
    423     u_int32_t local, remote;
    424     ipcp_options *wo = &ipcp_wantoptions[0];
    425     static int prio_local = 0, prio_remote = 0;
    426 
    427     /*
    428      * IP address pair separated by ":".
    429      */
    430     if ((colon = strchr(arg, ':')) == NULL)
    431 	return 0;
    432     if (!doit)
    433 	return 1;
    434 
    435     /*
    436      * If colon first character, then no local addr.
    437      */
    438     if (colon != arg && option_priority >= prio_local) {
    439 	*colon = '\0';
    440 	if ((local = inet_addr(arg)) == (u_int32_t) -1) {
    441 	    if ((hp = gethostbyname(arg)) == NULL) {
    442 		option_error("unknown host: %s", arg);
    443 		return 0;
    444 	    }
    445 	    local = *(u_int32_t *)hp->h_addr;
    446 	}
    447 	if (bad_ip_adrs(local)) {
    448 	    option_error("bad local IP address %s", ip_ntoa(local));
    449 	    return 0;
    450 	}
    451 	if (local != 0)
    452 	    wo->ouraddr = local;
    453 	*colon = ':';
    454 	prio_local = option_priority;
    455     }
    456 
    457     /*
    458      * If colon last character, then no remote addr.
    459      */
    460     if (*++colon != '\0' && option_priority >= prio_remote) {
    461 	if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
    462 	    if ((hp = gethostbyname(colon)) == NULL) {
    463 		option_error("unknown host: %s", colon);
    464 		return 0;
    465 	    }
    466 	    remote = *(u_int32_t *)hp->h_addr;
    467 	    if (remote_name[0] == 0)
    468 		strlcpy(remote_name, colon, sizeof(remote_name));
    469 	}
    470 	if (bad_ip_adrs(remote)) {
    471 	    option_error("bad remote IP address %s", ip_ntoa(remote));
    472 	    return 0;
    473 	}
    474 	if (remote != 0)
    475 	    wo->hisaddr = remote;
    476 	prio_remote = option_priority;
    477     }
    478 
    479     return 1;
    480 }
    481 
    482 static void
    483 printipaddr(opt, printer, arg)
    484     option_t *opt;
    485     void (*printer) __P((void *, char *, ...));
    486     void *arg;
    487 {
    488 	ipcp_options *wo = &ipcp_wantoptions[0];
    489 
    490 	if (wo->ouraddr != 0)
    491 		printer(arg, "%I", wo->ouraddr);
    492 	printer(arg, ":");
    493 	if (wo->hisaddr != 0)
    494 		printer(arg, "%I", wo->hisaddr);
    495 }
    496 
    497 /*
    498  * setnetmask - set the netmask to be used on the interface.
    499  */
    500 static int
    501 setnetmask(argv)
    502     char **argv;
    503 {
    504     u_int32_t mask;
    505     int n;
    506     char *p;
    507 
    508     /*
    509      * Unfortunately, if we use inet_addr, we can't tell whether
    510      * a result of all 1s is an error or a valid 255.255.255.255.
    511      */
    512     p = *argv;
    513     n = parse_dotted_ip(p, &mask);
    514 
    515     mask = htonl(mask);
    516 
    517     if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
    518 	option_error("invalid netmask value '%s'", *argv);
    519 	return 0;
    520     }
    521 
    522     netmask = mask;
    523     slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
    524 
    525     return (1);
    526 }
    527 
    528 int
    529 parse_dotted_ip(p, vp)
    530     char *p;
    531     u_int32_t *vp;
    532 {
    533     int n;
    534     u_int32_t v, b;
    535     char *endp, *p0 = p;
    536 
    537     v = 0;
    538     for (n = 3;; --n) {
    539 	b = strtoul(p, &endp, 0);
    540 	if (endp == p)
    541 	    return 0;
    542 	if (b > 255) {
    543 	    if (n < 3)
    544 		return 0;
    545 	    /* accept e.g. 0xffffff00 */
    546 	    *vp = b;
    547 	    return endp - p0;
    548 	}
    549 	v |= b << (n * 8);
    550 	p = endp;
    551 	if (n == 0)
    552 	    break;
    553 	if (*p != '.')
    554 	    return 0;
    555 	++p;
    556     }
    557     *vp = v;
    558     return p - p0;
    559 }
    560 
    561 
    562 /*
    563  * ipcp_init - Initialize IPCP.
    564  */
    565 static void
    566 ipcp_init(unit)
    567     int unit;
    568 {
    569     fsm *f = &ipcp_fsm[unit];
    570     ipcp_options *wo = &ipcp_wantoptions[unit];
    571     ipcp_options *ao = &ipcp_allowoptions[unit];
    572 
    573     f->unit = unit;
    574     f->protocol = PPP_IPCP;
    575     f->callbacks = &ipcp_callbacks;
    576     fsm_init(&ipcp_fsm[unit]);
    577 
    578     /*
    579      * Some 3G modems use repeated IPCP NAKs as a way of stalling
    580      * until they can contact a server on the network, so we increase
    581      * the default number of NAKs we accept before we start treating
    582      * them as rejects.
    583      */
    584     f->maxnakloops = 100;
    585 
    586     memset(wo, 0, sizeof(*wo));
    587     memset(ao, 0, sizeof(*ao));
    588 
    589     wo->neg_addr = wo->old_addrs = 1;
    590     wo->neg_vj = 1;
    591     wo->vj_protocol = IPCP_VJ_COMP;
    592     wo->maxslotindex = MAX_STATES - 1; /* really max index */
    593     wo->cflag = 1;
    594 
    595 
    596     /* max slots and slot-id compression are currently hardwired in */
    597     /* ppp_if.c to 16 and 1, this needs to be changed (among other */
    598     /* things) gmc */
    599 
    600     ao->neg_addr = ao->old_addrs = 1;
    601     ao->neg_vj = 1;
    602     ao->maxslotindex = MAX_STATES - 1;
    603     ao->cflag = 1;
    604 
    605     /*
    606      * XXX These control whether the user may use the proxyarp
    607      * and defaultroute options.
    608      */
    609     ao->proxy_arp = 1;
    610     ao->default_route = 1;
    611 }
    612 
    613 
    614 /*
    615  * ipcp_open - IPCP is allowed to come up.
    616  */
    617 static void
    618 ipcp_open(unit)
    619     int unit;
    620 {
    621     fsm_open(&ipcp_fsm[unit]);
    622     ipcp_is_open = 1;
    623 }
    624 
    625 
    626 /*
    627  * ipcp_close - Take IPCP down.
    628  */
    629 static void
    630 ipcp_close(unit, reason)
    631     int unit;
    632     char *reason;
    633 {
    634     fsm_close(&ipcp_fsm[unit], reason);
    635 }
    636 
    637 
    638 /*
    639  * ipcp_lowerup - The lower layer is up.
    640  */
    641 static void
    642 ipcp_lowerup(unit)
    643     int unit;
    644 {
    645     fsm_lowerup(&ipcp_fsm[unit]);
    646 }
    647 
    648 
    649 /*
    650  * ipcp_lowerdown - The lower layer is down.
    651  */
    652 static void
    653 ipcp_lowerdown(unit)
    654     int unit;
    655 {
    656     fsm_lowerdown(&ipcp_fsm[unit]);
    657 }
    658 
    659 
    660 /*
    661  * ipcp_input - Input IPCP packet.
    662  */
    663 static void
    664 ipcp_input(unit, p, len)
    665     int unit;
    666     u_char *p;
    667     int len;
    668 {
    669     fsm_input(&ipcp_fsm[unit], p, len);
    670 }
    671 
    672 
    673 /*
    674  * ipcp_protrej - A Protocol-Reject was received for IPCP.
    675  *
    676  * Pretend the lower layer went down, so we shut up.
    677  */
    678 static void
    679 ipcp_protrej(unit)
    680     int unit;
    681 {
    682     fsm_lowerdown(&ipcp_fsm[unit]);
    683 }
    684 
    685 
    686 /*
    687  * ipcp_resetci - Reset our CI.
    688  * Called by fsm_sconfreq, Send Configure Request.
    689  */
    690 static void
    691 ipcp_resetci(f)
    692     fsm *f;
    693 {
    694     ipcp_options *wo = &ipcp_wantoptions[f->unit];
    695     ipcp_options *go = &ipcp_gotoptions[f->unit];
    696     ipcp_options *ao = &ipcp_allowoptions[f->unit];
    697 
    698     wo->req_addr = (wo->neg_addr || wo->old_addrs) &&
    699 	(ao->neg_addr || ao->old_addrs);
    700     if (wo->ouraddr == 0)
    701 	wo->accept_local = 1;
    702     if (wo->hisaddr == 0)
    703 	wo->accept_remote = 1;
    704     wo->req_dns1 = usepeerdns;	/* Request DNS addresses from the peer */
    705     wo->req_dns2 = usepeerdns;
    706     *go = *wo;
    707     if (!ask_for_local)
    708 	go->ouraddr = 0;
    709     if (ip_choose_hook) {
    710 	ip_choose_hook(&wo->hisaddr);
    711 	if (wo->hisaddr) {
    712 	    wo->accept_remote = 0;
    713 	}
    714     }
    715     BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
    716 }
    717 
    718 
    719 /*
    720  * ipcp_cilen - Return length of our CI.
    721  * Called by fsm_sconfreq, Send Configure Request.
    722  */
    723 static int
    724 ipcp_cilen(f)
    725     fsm *f;
    726 {
    727     ipcp_options *go = &ipcp_gotoptions[f->unit];
    728     ipcp_options *wo = &ipcp_wantoptions[f->unit];
    729     ipcp_options *ho = &ipcp_hisoptions[f->unit];
    730 
    731 #define LENCIADDRS(neg)		(neg ? CILEN_ADDRS : 0)
    732 #define LENCIVJ(neg, old)	(neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
    733 #define LENCIADDR(neg)		(neg ? CILEN_ADDR : 0)
    734 #define LENCIDNS(neg)		LENCIADDR(neg)
    735 #define LENCIWINS(neg)		LENCIADDR(neg)
    736 
    737     /*
    738      * First see if we want to change our options to the old
    739      * forms because we have received old forms from the peer.
    740      */
    741     if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
    742 	go->neg_addr = 0;
    743     if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
    744 	/* try an older style of VJ negotiation */
    745 	/* use the old style only if the peer did */
    746 	if (ho->neg_vj && ho->old_vj) {
    747 	    go->neg_vj = 1;
    748 	    go->old_vj = 1;
    749 	    go->vj_protocol = ho->vj_protocol;
    750 	}
    751     }
    752 
    753     return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
    754 	    LENCIVJ(go->neg_vj, go->old_vj) +
    755 	    LENCIADDR(go->neg_addr) +
    756 	    LENCIDNS(go->req_dns1) +
    757 	    LENCIDNS(go->req_dns2) +
    758 	    LENCIWINS(go->winsaddr[0]) +
    759 	    LENCIWINS(go->winsaddr[1])) ;
    760 }
    761 
    762 
    763 /*
    764  * ipcp_addci - Add our desired CIs to a packet.
    765  * Called by fsm_sconfreq, Send Configure Request.
    766  */
    767 static void
    768 ipcp_addci(f, ucp, lenp)
    769     fsm *f;
    770     u_char *ucp;
    771     int *lenp;
    772 {
    773     ipcp_options *go = &ipcp_gotoptions[f->unit];
    774     int len = *lenp;
    775 
    776 #define ADDCIADDRS(opt, neg, val1, val2) \
    777     if (neg) { \
    778 	if (len >= CILEN_ADDRS) { \
    779 	    u_int32_t l; \
    780 	    PUTCHAR(opt, ucp); \
    781 	    PUTCHAR(CILEN_ADDRS, ucp); \
    782 	    l = ntohl(val1); \
    783 	    PUTLONG(l, ucp); \
    784 	    l = ntohl(val2); \
    785 	    PUTLONG(l, ucp); \
    786 	    len -= CILEN_ADDRS; \
    787 	} else \
    788 	    go->old_addrs = 0; \
    789     }
    790 
    791 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
    792     if (neg) { \
    793 	int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
    794 	if (len >= vjlen) { \
    795 	    PUTCHAR(opt, ucp); \
    796 	    PUTCHAR(vjlen, ucp); \
    797 	    PUTSHORT(val, ucp); \
    798 	    if (!old) { \
    799 		PUTCHAR(maxslotindex, ucp); \
    800 		PUTCHAR(cflag, ucp); \
    801 	    } \
    802 	    len -= vjlen; \
    803 	} else \
    804 	    neg = 0; \
    805     }
    806 
    807 #define ADDCIADDR(opt, neg, val) \
    808     if (neg) { \
    809 	if (len >= CILEN_ADDR) { \
    810 	    u_int32_t l; \
    811 	    PUTCHAR(opt, ucp); \
    812 	    PUTCHAR(CILEN_ADDR, ucp); \
    813 	    l = ntohl(val); \
    814 	    PUTLONG(l, ucp); \
    815 	    len -= CILEN_ADDR; \
    816 	} else \
    817 	    neg = 0; \
    818     }
    819 
    820 #define ADDCIDNS(opt, neg, addr) \
    821     if (neg) { \
    822 	if (len >= CILEN_ADDR) { \
    823 	    u_int32_t l; \
    824 	    PUTCHAR(opt, ucp); \
    825 	    PUTCHAR(CILEN_ADDR, ucp); \
    826 	    l = ntohl(addr); \
    827 	    PUTLONG(l, ucp); \
    828 	    len -= CILEN_ADDR; \
    829 	} else \
    830 	    neg = 0; \
    831     }
    832 
    833 #define ADDCIWINS(opt, addr) \
    834     if (addr) { \
    835 	if (len >= CILEN_ADDR) { \
    836 	    u_int32_t l; \
    837 	    PUTCHAR(opt, ucp); \
    838 	    PUTCHAR(CILEN_ADDR, ucp); \
    839 	    l = ntohl(addr); \
    840 	    PUTLONG(l, ucp); \
    841 	    len -= CILEN_ADDR; \
    842 	} else \
    843 	    addr = 0; \
    844     }
    845 
    846     ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
    847 	       go->hisaddr);
    848 
    849     ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
    850 	    go->maxslotindex, go->cflag);
    851 
    852     ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
    853 
    854     ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
    855 
    856     ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
    857 
    858     ADDCIWINS(CI_MS_WINS1, go->winsaddr[0]);
    859 
    860     ADDCIWINS(CI_MS_WINS2, go->winsaddr[1]);
    861 
    862     *lenp -= len;
    863 }
    864 
    865 
    866 /*
    867  * ipcp_ackci - Ack our CIs.
    868  * Called by fsm_rconfack, Receive Configure ACK.
    869  *
    870  * Returns:
    871  *	0 - Ack was bad.
    872  *	1 - Ack was good.
    873  */
    874 static int
    875 ipcp_ackci(f, p, len)
    876     fsm *f;
    877     u_char *p;
    878     int len;
    879 {
    880     ipcp_options *go = &ipcp_gotoptions[f->unit];
    881     u_short cilen, citype, cishort;
    882     u_int32_t cilong;
    883     u_char cimaxslotindex, cicflag;
    884 
    885     /*
    886      * CIs must be in exactly the same order that we sent...
    887      * Check packet length and CI length at each step.
    888      * If we find any deviations, then this packet is bad.
    889      */
    890 
    891 #define ACKCIADDRS(opt, neg, val1, val2) \
    892     if (neg) { \
    893 	u_int32_t l; \
    894 	if ((len -= CILEN_ADDRS) < 0) \
    895 	    goto bad; \
    896 	GETCHAR(citype, p); \
    897 	GETCHAR(cilen, p); \
    898 	if (cilen != CILEN_ADDRS || \
    899 	    citype != opt) \
    900 	    goto bad; \
    901 	GETLONG(l, p); \
    902 	cilong = htonl(l); \
    903 	if (val1 != cilong) \
    904 	    goto bad; \
    905 	GETLONG(l, p); \
    906 	cilong = htonl(l); \
    907 	if (val2 != cilong) \
    908 	    goto bad; \
    909     }
    910 
    911 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
    912     if (neg) { \
    913 	int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
    914 	if ((len -= vjlen) < 0) \
    915 	    goto bad; \
    916 	GETCHAR(citype, p); \
    917 	GETCHAR(cilen, p); \
    918 	if (cilen != vjlen || \
    919 	    citype != opt)  \
    920 	    goto bad; \
    921 	GETSHORT(cishort, p); \
    922 	if (cishort != val) \
    923 	    goto bad; \
    924 	if (!old) { \
    925 	    GETCHAR(cimaxslotindex, p); \
    926 	    if (cimaxslotindex != maxslotindex) \
    927 		goto bad; \
    928 	    GETCHAR(cicflag, p); \
    929 	    if (cicflag != cflag) \
    930 		goto bad; \
    931 	} \
    932     }
    933 
    934 #define ACKCIADDR(opt, neg, val) \
    935     if (neg) { \
    936 	u_int32_t l; \
    937 	if ((len -= CILEN_ADDR) < 0) \
    938 	    goto bad; \
    939 	GETCHAR(citype, p); \
    940 	GETCHAR(cilen, p); \
    941 	if (cilen != CILEN_ADDR || \
    942 	    citype != opt) \
    943 	    goto bad; \
    944 	GETLONG(l, p); \
    945 	cilong = htonl(l); \
    946 	if (val != cilong) \
    947 	    goto bad; \
    948     }
    949 
    950 #define ACKCIDNS(opt, neg, addr) \
    951     if (neg) { \
    952 	u_int32_t l; \
    953 	if ((len -= CILEN_ADDR) < 0) \
    954 	    goto bad; \
    955 	GETCHAR(citype, p); \
    956 	GETCHAR(cilen, p); \
    957 	if (cilen != CILEN_ADDR || citype != opt) \
    958 	    goto bad; \
    959 	GETLONG(l, p); \
    960 	cilong = htonl(l); \
    961 	if (addr != cilong) \
    962 	    goto bad; \
    963     }
    964 
    965 #define ACKCIWINS(opt, addr) \
    966     if (addr) { \
    967 	u_int32_t l; \
    968 	if ((len -= CILEN_ADDR) < 0) \
    969 	    goto bad; \
    970 	GETCHAR(citype, p); \
    971 	GETCHAR(cilen, p); \
    972 	if (cilen != CILEN_ADDR || citype != opt) \
    973 	    goto bad; \
    974 	GETLONG(l, p); \
    975 	cilong = htonl(l); \
    976 	if (addr != cilong) \
    977 	    goto bad; \
    978     }
    979 
    980     ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
    981 	       go->hisaddr);
    982 
    983     ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
    984 	    go->maxslotindex, go->cflag);
    985 
    986     ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
    987 
    988     ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
    989 
    990     ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
    991 
    992     ACKCIWINS(CI_MS_WINS1, go->winsaddr[0]);
    993 
    994     ACKCIWINS(CI_MS_WINS2, go->winsaddr[1]);
    995 
    996     /*
    997      * If there are any remaining CIs, then this packet is bad.
    998      */
    999     if (len != 0)
   1000 	goto bad;
   1001     return (1);
   1002 
   1003 bad:
   1004     IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
   1005     return (0);
   1006 }
   1007 
   1008 /*
   1009  * ipcp_nakci - Peer has sent a NAK for some of our CIs.
   1010  * This should not modify any state if the Nak is bad
   1011  * or if IPCP is in the OPENED state.
   1012  * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
   1013  *
   1014  * Returns:
   1015  *	0 - Nak was bad.
   1016  *	1 - Nak was good.
   1017  */
   1018 static int
   1019 ipcp_nakci(f, p, len, treat_as_reject)
   1020     fsm *f;
   1021     u_char *p;
   1022     int len;
   1023     int treat_as_reject;
   1024 {
   1025     ipcp_options *go = &ipcp_gotoptions[f->unit];
   1026     u_char cimaxslotindex, cicflag;
   1027     u_char citype, cilen, *next;
   1028     u_short cishort;
   1029     u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
   1030     ipcp_options no;		/* options we've seen Naks for */
   1031     ipcp_options try;		/* options to request next time */
   1032 
   1033     BZERO(&no, sizeof(no));
   1034     try = *go;
   1035 
   1036     /*
   1037      * Any Nak'd CIs must be in exactly the same order that we sent.
   1038      * Check packet length and CI length at each step.
   1039      * If we find any deviations, then this packet is bad.
   1040      */
   1041 #define NAKCIADDRS(opt, neg, code) \
   1042     if ((neg) && \
   1043 	(cilen = p[1]) == CILEN_ADDRS && \
   1044 	len >= cilen && \
   1045 	p[0] == opt) { \
   1046 	len -= cilen; \
   1047 	INCPTR(2, p); \
   1048 	GETLONG(l, p); \
   1049 	ciaddr1 = htonl(l); \
   1050 	GETLONG(l, p); \
   1051 	ciaddr2 = htonl(l); \
   1052 	no.old_addrs = 1; \
   1053 	code \
   1054     }
   1055 
   1056 #define NAKCIVJ(opt, neg, code) \
   1057     if (go->neg && \
   1058 	((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
   1059 	len >= cilen && \
   1060 	p[0] == opt) { \
   1061 	len -= cilen; \
   1062 	INCPTR(2, p); \
   1063 	GETSHORT(cishort, p); \
   1064 	no.neg = 1; \
   1065         code \
   1066     }
   1067 
   1068 #define NAKCIADDR(opt, neg, code) \
   1069     if (go->neg && \
   1070 	(cilen = p[1]) == CILEN_ADDR && \
   1071 	len >= cilen && \
   1072 	p[0] == opt) { \
   1073 	len -= cilen; \
   1074 	INCPTR(2, p); \
   1075 	GETLONG(l, p); \
   1076 	ciaddr1 = htonl(l); \
   1077 	no.neg = 1; \
   1078 	code \
   1079     }
   1080 
   1081 #define NAKCIDNS(opt, neg, code) \
   1082     if (go->neg && \
   1083 	((cilen = p[1]) == CILEN_ADDR) && \
   1084 	len >= cilen && \
   1085 	p[0] == opt) { \
   1086 	len -= cilen; \
   1087 	INCPTR(2, p); \
   1088 	GETLONG(l, p); \
   1089 	cidnsaddr = htonl(l); \
   1090 	no.neg = 1; \
   1091 	code \
   1092     }
   1093 
   1094     /*
   1095      * Accept the peer's idea of {our,his} address, if different
   1096      * from our idea, only if the accept_{local,remote} flag is set.
   1097      */
   1098     NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
   1099 	       if (treat_as_reject) {
   1100 		   try.old_addrs = 0;
   1101 	       } else {
   1102 		   if (go->accept_local && ciaddr1) {
   1103 		       /* take his idea of our address */
   1104 		       try.ouraddr = ciaddr1;
   1105 		   }
   1106 		   if (go->accept_remote && ciaddr2) {
   1107 		       /* take his idea of his address */
   1108 		       try.hisaddr = ciaddr2;
   1109 		   }
   1110 	       }
   1111 	);
   1112 
   1113     /*
   1114      * Accept the peer's value of maxslotindex provided that it
   1115      * is less than what we asked for.  Turn off slot-ID compression
   1116      * if the peer wants.  Send old-style compress-type option if
   1117      * the peer wants.
   1118      */
   1119     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
   1120 	    if (treat_as_reject) {
   1121 		try.neg_vj = 0;
   1122 	    } else if (cilen == CILEN_VJ) {
   1123 		GETCHAR(cimaxslotindex, p);
   1124 		GETCHAR(cicflag, p);
   1125 		if (cishort == IPCP_VJ_COMP) {
   1126 		    try.old_vj = 0;
   1127 		    if (cimaxslotindex < go->maxslotindex)
   1128 			try.maxslotindex = cimaxslotindex;
   1129 		    if (!cicflag)
   1130 			try.cflag = 0;
   1131 		} else {
   1132 		    try.neg_vj = 0;
   1133 		}
   1134 	    } else {
   1135 		if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
   1136 		    try.old_vj = 1;
   1137 		    try.vj_protocol = cishort;
   1138 		} else {
   1139 		    try.neg_vj = 0;
   1140 		}
   1141 	    }
   1142 	    );
   1143 
   1144     NAKCIADDR(CI_ADDR, neg_addr,
   1145 	      if (treat_as_reject) {
   1146 		  try.neg_addr = 0;
   1147 		  try.old_addrs = 0;
   1148 	      } else if (go->accept_local && ciaddr1) {
   1149 		  /* take his idea of our address */
   1150 		  try.ouraddr = ciaddr1;
   1151 	      }
   1152 	      );
   1153 
   1154     NAKCIDNS(CI_MS_DNS1, req_dns1,
   1155 	     if (treat_as_reject) {
   1156 		 try.req_dns1 = 0;
   1157 	     } else {
   1158 		 try.dnsaddr[0] = cidnsaddr;
   1159 	     }
   1160 	     );
   1161 
   1162     NAKCIDNS(CI_MS_DNS2, req_dns2,
   1163 	     if (treat_as_reject) {
   1164 		 try.req_dns2 = 0;
   1165 	     } else {
   1166 		 try.dnsaddr[1] = cidnsaddr;
   1167 	     }
   1168 	     );
   1169 
   1170     /*
   1171      * There may be remaining CIs, if the peer is requesting negotiation
   1172      * on an option that we didn't include in our request packet.
   1173      * If they want to negotiate about IP addresses, we comply.
   1174      * If they want us to ask for compression, we refuse.
   1175      * If they want us to ask for ms-dns, we do that, since some
   1176      * peers get huffy if we don't.
   1177      */
   1178     while (len >= CILEN_VOID) {
   1179 	GETCHAR(citype, p);
   1180 	GETCHAR(cilen, p);
   1181 	if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
   1182 	    goto bad;
   1183 	next = p + cilen - 2;
   1184 
   1185 	switch (citype) {
   1186 	case CI_COMPRESSTYPE:
   1187 	    if (go->neg_vj || no.neg_vj ||
   1188 		(cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
   1189 		goto bad;
   1190 	    no.neg_vj = 1;
   1191 	    break;
   1192 	case CI_ADDRS:
   1193 	    if ((!go->neg_addr && go->old_addrs) || no.old_addrs
   1194 		|| cilen != CILEN_ADDRS)
   1195 		goto bad;
   1196 	    try.neg_addr = 0;
   1197 	    GETLONG(l, p);
   1198 	    ciaddr1 = htonl(l);
   1199 	    if (ciaddr1 && go->accept_local)
   1200 		try.ouraddr = ciaddr1;
   1201 	    GETLONG(l, p);
   1202 	    ciaddr2 = htonl(l);
   1203 	    if (ciaddr2 && go->accept_remote)
   1204 		try.hisaddr = ciaddr2;
   1205 	    no.old_addrs = 1;
   1206 	    break;
   1207 	case CI_ADDR:
   1208 	    if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
   1209 		goto bad;
   1210 	    try.old_addrs = 0;
   1211 	    GETLONG(l, p);
   1212 	    ciaddr1 = htonl(l);
   1213 	    if (ciaddr1 && go->accept_local)
   1214 		try.ouraddr = ciaddr1;
   1215 	    if (try.ouraddr != 0)
   1216 		try.neg_addr = 1;
   1217 	    no.neg_addr = 1;
   1218 	    break;
   1219 	case CI_MS_DNS1:
   1220 	    if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
   1221 		goto bad;
   1222 	    GETLONG(l, p);
   1223 	    try.dnsaddr[0] = htonl(l);
   1224 	    try.req_dns1 = 1;
   1225 	    no.req_dns1 = 1;
   1226 	    break;
   1227 	case CI_MS_DNS2:
   1228 	    if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
   1229 		goto bad;
   1230 	    GETLONG(l, p);
   1231 	    try.dnsaddr[1] = htonl(l);
   1232 	    try.req_dns2 = 1;
   1233 	    no.req_dns2 = 1;
   1234 	    break;
   1235 	case CI_MS_WINS1:
   1236 	case CI_MS_WINS2:
   1237 	    if (cilen != CILEN_ADDR)
   1238 		goto bad;
   1239 	    GETLONG(l, p);
   1240 	    ciaddr1 = htonl(l);
   1241 	    if (ciaddr1)
   1242 		try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
   1243 	    break;
   1244 	}
   1245 	p = next;
   1246     }
   1247 
   1248     /*
   1249      * OK, the Nak is good.  Now we can update state.
   1250      * If there are any remaining options, we ignore them.
   1251      */
   1252     if (f->state != OPENED)
   1253 	*go = try;
   1254 
   1255     return 1;
   1256 
   1257 bad:
   1258     IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
   1259     return 0;
   1260 }
   1261 
   1262 
   1263 /*
   1264  * ipcp_rejci - Reject some of our CIs.
   1265  * Callback from fsm_rconfnakrej.
   1266  */
   1267 static int
   1268 ipcp_rejci(f, p, len)
   1269     fsm *f;
   1270     u_char *p;
   1271     int len;
   1272 {
   1273     ipcp_options *go = &ipcp_gotoptions[f->unit];
   1274     u_char cimaxslotindex, ciflag, cilen;
   1275     u_short cishort;
   1276     u_int32_t cilong;
   1277     ipcp_options try;		/* options to request next time */
   1278 
   1279     try = *go;
   1280     /*
   1281      * Any Rejected CIs must be in exactly the same order that we sent.
   1282      * Check packet length and CI length at each step.
   1283      * If we find any deviations, then this packet is bad.
   1284      */
   1285 #define REJCIADDRS(opt, neg, val1, val2) \
   1286     if ((neg) && \
   1287 	(cilen = p[1]) == CILEN_ADDRS && \
   1288 	len >= cilen && \
   1289 	p[0] == opt) { \
   1290 	u_int32_t l; \
   1291 	len -= cilen; \
   1292 	INCPTR(2, p); \
   1293 	GETLONG(l, p); \
   1294 	cilong = htonl(l); \
   1295 	/* Check rejected value. */ \
   1296 	if (cilong != val1) \
   1297 	    goto bad; \
   1298 	GETLONG(l, p); \
   1299 	cilong = htonl(l); \
   1300 	/* Check rejected value. */ \
   1301 	if (cilong != val2) \
   1302 	    goto bad; \
   1303 	try.old_addrs = 0; \
   1304     }
   1305 
   1306 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
   1307     if (go->neg && \
   1308 	p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
   1309 	len >= p[1] && \
   1310 	p[0] == opt) { \
   1311 	len -= p[1]; \
   1312 	INCPTR(2, p); \
   1313 	GETSHORT(cishort, p); \
   1314 	/* Check rejected value. */  \
   1315 	if (cishort != val) \
   1316 	    goto bad; \
   1317 	if (!old) { \
   1318 	   GETCHAR(cimaxslotindex, p); \
   1319 	   if (cimaxslotindex != maxslot) \
   1320 	     goto bad; \
   1321 	   GETCHAR(ciflag, p); \
   1322 	   if (ciflag != cflag) \
   1323 	     goto bad; \
   1324         } \
   1325 	try.neg = 0; \
   1326      }
   1327 
   1328 #define REJCIADDR(opt, neg, val) \
   1329     if (go->neg && \
   1330 	(cilen = p[1]) == CILEN_ADDR && \
   1331 	len >= cilen && \
   1332 	p[0] == opt) { \
   1333 	u_int32_t l; \
   1334 	len -= cilen; \
   1335 	INCPTR(2, p); \
   1336 	GETLONG(l, p); \
   1337 	cilong = htonl(l); \
   1338 	/* Check rejected value. */ \
   1339 	if (cilong != val) \
   1340 	    goto bad; \
   1341 	try.neg = 0; \
   1342     }
   1343 
   1344 #define REJCIDNS(opt, neg, dnsaddr) \
   1345     if (go->neg && \
   1346 	((cilen = p[1]) == CILEN_ADDR) && \
   1347 	len >= cilen && \
   1348 	p[0] == opt) { \
   1349 	u_int32_t l; \
   1350 	len -= cilen; \
   1351 	INCPTR(2, p); \
   1352 	GETLONG(l, p); \
   1353 	cilong = htonl(l); \
   1354 	/* Check rejected value. */ \
   1355 	if (cilong != dnsaddr) \
   1356 	    goto bad; \
   1357 	try.neg = 0; \
   1358     }
   1359 
   1360 #define REJCIWINS(opt, addr) \
   1361     if (addr && \
   1362 	((cilen = p[1]) == CILEN_ADDR) && \
   1363 	len >= cilen && \
   1364 	p[0] == opt) { \
   1365 	u_int32_t l; \
   1366 	len -= cilen; \
   1367 	INCPTR(2, p); \
   1368 	GETLONG(l, p); \
   1369 	cilong = htonl(l); \
   1370 	/* Check rejected value. */ \
   1371 	if (cilong != addr) \
   1372 	    goto bad; \
   1373 	try.winsaddr[opt == CI_MS_WINS2] = 0; \
   1374     }
   1375 
   1376     REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
   1377 	       go->ouraddr, go->hisaddr);
   1378 
   1379     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
   1380 	    go->maxslotindex, go->cflag);
   1381 
   1382     REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
   1383 
   1384     REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
   1385 
   1386     REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
   1387 
   1388     REJCIWINS(CI_MS_WINS1, go->winsaddr[0]);
   1389 
   1390     REJCIWINS(CI_MS_WINS2, go->winsaddr[1]);
   1391 
   1392     /*
   1393      * If there are any remaining CIs, then this packet is bad.
   1394      */
   1395     if (len != 0)
   1396 	goto bad;
   1397     /*
   1398      * Now we can update state.
   1399      */
   1400     if (f->state != OPENED)
   1401 	*go = try;
   1402     return 1;
   1403 
   1404 bad:
   1405     IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
   1406     return 0;
   1407 }
   1408 
   1409 
   1410 /*
   1411  * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
   1412  * Callback from fsm_rconfreq, Receive Configure Request
   1413  *
   1414  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
   1415  * appropriately.  If reject_if_disagree is non-zero, doesn't return
   1416  * CONFNAK; returns CONFREJ if it can't return CONFACK.
   1417  */
   1418 static int
   1419 ipcp_reqci(f, inp, len, reject_if_disagree)
   1420     fsm *f;
   1421     u_char *inp;		/* Requested CIs */
   1422     int *len;			/* Length of requested CIs */
   1423     int reject_if_disagree;
   1424 {
   1425     ipcp_options *wo = &ipcp_wantoptions[f->unit];
   1426     ipcp_options *ho = &ipcp_hisoptions[f->unit];
   1427     ipcp_options *ao = &ipcp_allowoptions[f->unit];
   1428     u_char *cip, *next;		/* Pointer to current and next CIs */
   1429     u_short cilen, citype;	/* Parsed len, type */
   1430     u_short cishort;		/* Parsed short value */
   1431     u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
   1432     int rc = CONFACK;		/* Final packet return code */
   1433     int orc;			/* Individual option return code */
   1434     u_char *p;			/* Pointer to next char to parse */
   1435     u_char *ucp = inp;		/* Pointer to current output char */
   1436     int l = *len;		/* Length left */
   1437     u_char maxslotindex, cflag;
   1438     int d;
   1439 
   1440     /*
   1441      * Reset all his options.
   1442      */
   1443     BZERO(ho, sizeof(*ho));
   1444 
   1445     /*
   1446      * Process all his options.
   1447      */
   1448     next = inp;
   1449     while (l) {
   1450 	orc = CONFACK;			/* Assume success */
   1451 	cip = p = next;			/* Remember begining of CI */
   1452 	if (l < 2 ||			/* Not enough data for CI header or */
   1453 	    p[1] < 2 ||			/*  CI length too small or */
   1454 	    p[1] > l) {			/*  CI length too big? */
   1455 	    IPCPDEBUG(("ipcp_reqci: bad CI length!"));
   1456 	    orc = CONFREJ;		/* Reject bad CI */
   1457 	    cilen = l;			/* Reject till end of packet */
   1458 	    l = 0;			/* Don't loop again */
   1459 	    goto endswitch;
   1460 	}
   1461 	GETCHAR(citype, p);		/* Parse CI type */
   1462 	GETCHAR(cilen, p);		/* Parse CI length */
   1463 	l -= cilen;			/* Adjust remaining length */
   1464 	next += cilen;			/* Step to next CI */
   1465 
   1466 	switch (citype) {		/* Check CI type */
   1467 	case CI_ADDRS:
   1468 	    if (!ao->old_addrs || ho->neg_addr ||
   1469 		cilen != CILEN_ADDRS) {	/* Check CI length */
   1470 		orc = CONFREJ;		/* Reject CI */
   1471 		break;
   1472 	    }
   1473 
   1474 	    /*
   1475 	     * If he has no address, or if we both have his address but
   1476 	     * disagree about it, then NAK it with our idea.
   1477 	     * In particular, if we don't know his address, but he does,
   1478 	     * then accept it.
   1479 	     */
   1480 	    GETLONG(tl, p);		/* Parse source address (his) */
   1481 	    ciaddr1 = htonl(tl);
   1482 	    if (ciaddr1 != wo->hisaddr
   1483 		&& (ciaddr1 == 0 || !wo->accept_remote)) {
   1484 		orc = CONFNAK;
   1485 		if (!reject_if_disagree) {
   1486 		    DECPTR(sizeof(u_int32_t), p);
   1487 		    tl = ntohl(wo->hisaddr);
   1488 		    PUTLONG(tl, p);
   1489 		}
   1490 	    } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
   1491 		/*
   1492 		 * If neither we nor he knows his address, reject the option.
   1493 		 */
   1494 		orc = CONFREJ;
   1495 		wo->req_addr = 0;	/* don't NAK with 0.0.0.0 later */
   1496 		break;
   1497 	    }
   1498 
   1499 	    /*
   1500 	     * If he doesn't know our address, or if we both have our address
   1501 	     * but disagree about it, then NAK it with our idea.
   1502 	     */
   1503 	    GETLONG(tl, p);		/* Parse desination address (ours) */
   1504 	    ciaddr2 = htonl(tl);
   1505 	    if (ciaddr2 != wo->ouraddr) {
   1506 		if (ciaddr2 == 0 || !wo->accept_local) {
   1507 		    orc = CONFNAK;
   1508 		    if (!reject_if_disagree) {
   1509 			DECPTR(sizeof(u_int32_t), p);
   1510 			tl = ntohl(wo->ouraddr);
   1511 			PUTLONG(tl, p);
   1512 		    }
   1513 		} else {
   1514 		    wo->ouraddr = ciaddr2;	/* accept peer's idea */
   1515 		}
   1516 	    }
   1517 
   1518 	    ho->old_addrs = 1;
   1519 	    ho->hisaddr = ciaddr1;
   1520 	    ho->ouraddr = ciaddr2;
   1521 	    break;
   1522 
   1523 	case CI_ADDR:
   1524 	    if (!ao->neg_addr || ho->old_addrs ||
   1525 		cilen != CILEN_ADDR) {	/* Check CI length */
   1526 		orc = CONFREJ;		/* Reject CI */
   1527 		break;
   1528 	    }
   1529 
   1530 	    /*
   1531 	     * If he has no address, or if we both have his address but
   1532 	     * disagree about it, then NAK it with our idea.
   1533 	     * In particular, if we don't know his address, but he does,
   1534 	     * then accept it.
   1535 	     */
   1536 	    GETLONG(tl, p);	/* Parse source address (his) */
   1537 	    ciaddr1 = htonl(tl);
   1538 	    if (ciaddr1 != wo->hisaddr
   1539 		&& (ciaddr1 == 0 || !wo->accept_remote)) {
   1540 		orc = CONFNAK;
   1541 		if (!reject_if_disagree) {
   1542 		    DECPTR(sizeof(u_int32_t), p);
   1543 		    tl = ntohl(wo->hisaddr);
   1544 		    PUTLONG(tl, p);
   1545 		}
   1546 	    } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
   1547 		/*
   1548 		 * Don't ACK an address of 0.0.0.0 - reject it instead.
   1549 		 */
   1550 		orc = CONFREJ;
   1551 		wo->req_addr = 0;	/* don't NAK with 0.0.0.0 later */
   1552 		break;
   1553 	    }
   1554 
   1555 	    ho->neg_addr = 1;
   1556 	    ho->hisaddr = ciaddr1;
   1557 	    break;
   1558 
   1559 	case CI_MS_DNS1:
   1560 	case CI_MS_DNS2:
   1561 	    /* Microsoft primary or secondary DNS request */
   1562 	    d = citype == CI_MS_DNS2;
   1563 
   1564 	    /* If we do not have a DNS address then we cannot send it */
   1565 	    if (ao->dnsaddr[d] == 0 ||
   1566 		cilen != CILEN_ADDR) {	/* Check CI length */
   1567 		orc = CONFREJ;		/* Reject CI */
   1568 		break;
   1569 	    }
   1570 	    GETLONG(tl, p);
   1571 	    if (htonl(tl) != ao->dnsaddr[d]) {
   1572                 DECPTR(sizeof(u_int32_t), p);
   1573 		tl = ntohl(ao->dnsaddr[d]);
   1574 		PUTLONG(tl, p);
   1575 		orc = CONFNAK;
   1576             }
   1577             break;
   1578 
   1579 	case CI_MS_WINS1:
   1580 	case CI_MS_WINS2:
   1581 	    /* Microsoft primary or secondary WINS request */
   1582 	    d = citype == CI_MS_WINS2;
   1583 
   1584 	    /* If we do not have a DNS address then we cannot send it */
   1585 	    if (ao->winsaddr[d] == 0 ||
   1586 		cilen != CILEN_ADDR) {	/* Check CI length */
   1587 		orc = CONFREJ;		/* Reject CI */
   1588 		break;
   1589 	    }
   1590 	    GETLONG(tl, p);
   1591 	    if (htonl(tl) != ao->winsaddr[d]) {
   1592                 DECPTR(sizeof(u_int32_t), p);
   1593 		tl = ntohl(ao->winsaddr[d]);
   1594 		PUTLONG(tl, p);
   1595 		orc = CONFNAK;
   1596             }
   1597             break;
   1598 
   1599 	case CI_COMPRESSTYPE:
   1600 	    if (!ao->neg_vj ||
   1601 		(cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
   1602 		orc = CONFREJ;
   1603 		break;
   1604 	    }
   1605 	    GETSHORT(cishort, p);
   1606 
   1607 	    if (!(cishort == IPCP_VJ_COMP ||
   1608 		  (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
   1609 		orc = CONFREJ;
   1610 		break;
   1611 	    }
   1612 
   1613 	    ho->neg_vj = 1;
   1614 	    ho->vj_protocol = cishort;
   1615 	    if (cilen == CILEN_VJ) {
   1616 		GETCHAR(maxslotindex, p);
   1617 		if (maxslotindex > ao->maxslotindex) {
   1618 		    orc = CONFNAK;
   1619 		    if (!reject_if_disagree){
   1620 			DECPTR(1, p);
   1621 			PUTCHAR(ao->maxslotindex, p);
   1622 		    }
   1623 		}
   1624 		GETCHAR(cflag, p);
   1625 		if (cflag && !ao->cflag) {
   1626 		    orc = CONFNAK;
   1627 		    if (!reject_if_disagree){
   1628 			DECPTR(1, p);
   1629 			PUTCHAR(wo->cflag, p);
   1630 		    }
   1631 		}
   1632 		ho->maxslotindex = maxslotindex;
   1633 		ho->cflag = cflag;
   1634 	    } else {
   1635 		ho->old_vj = 1;
   1636 		ho->maxslotindex = MAX_STATES - 1;
   1637 		ho->cflag = 1;
   1638 	    }
   1639 	    break;
   1640 
   1641 	default:
   1642 	    orc = CONFREJ;
   1643 	    break;
   1644 	}
   1645 endswitch:
   1646 	if (orc == CONFACK &&		/* Good CI */
   1647 	    rc != CONFACK)		/*  but prior CI wasnt? */
   1648 	    continue;			/* Don't send this one */
   1649 
   1650 	if (orc == CONFNAK) {		/* Nak this CI? */
   1651 	    if (reject_if_disagree)	/* Getting fed up with sending NAKs? */
   1652 		orc = CONFREJ;		/* Get tough if so */
   1653 	    else {
   1654 		if (rc == CONFREJ)	/* Rejecting prior CI? */
   1655 		    continue;		/* Don't send this one */
   1656 		if (rc == CONFACK) {	/* Ack'd all prior CIs? */
   1657 		    rc = CONFNAK;	/* Not anymore... */
   1658 		    ucp = inp;		/* Backup */
   1659 		}
   1660 	    }
   1661 	}
   1662 
   1663 	if (orc == CONFREJ &&		/* Reject this CI */
   1664 	    rc != CONFREJ) {		/*  but no prior ones? */
   1665 	    rc = CONFREJ;
   1666 	    ucp = inp;			/* Backup */
   1667 	}
   1668 
   1669 	/* Need to move CI? */
   1670 	if (ucp != cip)
   1671 	    BCOPY(cip, ucp, cilen);	/* Move it */
   1672 
   1673 	/* Update output pointer */
   1674 	INCPTR(cilen, ucp);
   1675     }
   1676 
   1677     /*
   1678      * If we aren't rejecting this packet, and we want to negotiate
   1679      * their address, and they didn't send their address, then we
   1680      * send a NAK with a CI_ADDR option appended.  We assume the
   1681      * input buffer is long enough that we can append the extra
   1682      * option safely.
   1683      */
   1684     if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
   1685 	wo->req_addr && !reject_if_disagree && !noremoteip) {
   1686 	if (rc == CONFACK) {
   1687 	    rc = CONFNAK;
   1688 	    ucp = inp;			/* reset pointer */
   1689 	    wo->req_addr = 0;		/* don't ask again */
   1690 	}
   1691 	PUTCHAR(CI_ADDR, ucp);
   1692 	PUTCHAR(CILEN_ADDR, ucp);
   1693 	tl = ntohl(wo->hisaddr);
   1694 	PUTLONG(tl, ucp);
   1695     }
   1696 
   1697     *len = ucp - inp;			/* Compute output length */
   1698     IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
   1699     return (rc);			/* Return final code */
   1700 }
   1701 
   1702 
   1703 /*
   1704  * ip_check_options - check that any IP-related options are OK,
   1705  * and assign appropriate defaults.
   1706  */
   1707 static void
   1708 ip_check_options()
   1709 {
   1710     struct hostent *hp;
   1711     u_int32_t local;
   1712     ipcp_options *wo = &ipcp_wantoptions[0];
   1713 
   1714     /*
   1715      * Default our local IP address based on our hostname.
   1716      * If local IP address already given, don't bother.
   1717      */
   1718     if (wo->ouraddr == 0 && !disable_defaultip) {
   1719 	/*
   1720 	 * Look up our hostname (possibly with domain name appended)
   1721 	 * and take the first IP address as our local IP address.
   1722 	 * If there isn't an IP address for our hostname, too bad.
   1723 	 */
   1724 	wo->accept_local = 1;	/* don't insist on this default value */
   1725 	if ((hp = gethostbyname(hostname)) != NULL) {
   1726 	    local = *(u_int32_t *)hp->h_addr;
   1727 	    if (local != 0 && !bad_ip_adrs(local))
   1728 		wo->ouraddr = local;
   1729 	}
   1730     }
   1731     ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
   1732 }
   1733 
   1734 
   1735 /*
   1736  * ip_demand_conf - configure the interface as though
   1737  * IPCP were up, for use with dial-on-demand.
   1738  */
   1739 static int
   1740 ip_demand_conf(u)
   1741     int u;
   1742 {
   1743     ipcp_options *wo = &ipcp_wantoptions[u];
   1744 
   1745     if (wo->hisaddr == 0 && !noremoteip) {
   1746 	/* make up an arbitrary address for the peer */
   1747 	wo->hisaddr = htonl(0x0a707070 + ifunit);
   1748 	wo->accept_remote = 1;
   1749     }
   1750     if (wo->ouraddr == 0) {
   1751 	/* make up an arbitrary address for us */
   1752 	wo->ouraddr = htonl(0x0a404040 + ifunit);
   1753 	wo->accept_local = 1;
   1754 	ask_for_local = 0;	/* don't tell the peer this address */
   1755     }
   1756     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
   1757 	return 0;
   1758     ipcp_script(_PATH_IPPREUP, 1);
   1759     if (!sifup(u))
   1760 	return 0;
   1761     if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
   1762 	return 0;
   1763     if (wo->default_route)
   1764 	if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
   1765 	    default_route_set[u] = 1;
   1766     if (wo->proxy_arp)
   1767 	if (sifproxyarp(u, wo->hisaddr))
   1768 	    proxy_arp_set[u] = 1;
   1769 
   1770     notice("local  IP address %I", wo->ouraddr);
   1771     if (wo->hisaddr)
   1772 	notice("remote IP address %I", wo->hisaddr);
   1773 
   1774     return 1;
   1775 }
   1776 
   1777 
   1778 /*
   1779  * ipcp_up - IPCP has come UP.
   1780  *
   1781  * Configure the IP network interface appropriately and bring it up.
   1782  */
   1783 static void
   1784 ipcp_up(f)
   1785     fsm *f;
   1786 {
   1787     u_int32_t mask;
   1788     ipcp_options *ho = &ipcp_hisoptions[f->unit];
   1789     ipcp_options *go = &ipcp_gotoptions[f->unit];
   1790     ipcp_options *wo = &ipcp_wantoptions[f->unit];
   1791 
   1792     IPCPDEBUG(("ipcp: up"));
   1793 
   1794     /*
   1795      * We must have a non-zero IP address for both ends of the link.
   1796      */
   1797     if (!ho->neg_addr && !ho->old_addrs)
   1798 	ho->hisaddr = wo->hisaddr;
   1799 
   1800     if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
   1801 	&& wo->ouraddr != 0) {
   1802 	error("Peer refused to agree to our IP address");
   1803 	ipcp_close(f->unit, "Refused our IP address");
   1804 	return;
   1805     }
   1806     if (go->ouraddr == 0) {
   1807 	error("Could not determine local IP address");
   1808 	ipcp_close(f->unit, "Could not determine local IP address");
   1809 	return;
   1810     }
   1811     if (ho->hisaddr == 0 && !noremoteip) {
   1812 	ho->hisaddr = htonl(0x0a404040 + ifunit);
   1813 	warn("Could not determine remote IP address: defaulting to %I",
   1814 	     ho->hisaddr);
   1815     }
   1816     script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
   1817     if (ho->hisaddr != 0)
   1818 	script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
   1819 
   1820     if (!go->req_dns1)
   1821 	    go->dnsaddr[0] = 0;
   1822     if (!go->req_dns2)
   1823 	    go->dnsaddr[1] = 0;
   1824     if (go->dnsaddr[0])
   1825 	script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
   1826     if (go->dnsaddr[1])
   1827 	script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
   1828     if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
   1829 	script_setenv("USEPEERDNS", "1", 0);
   1830 	create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
   1831     }
   1832 
   1833     /*
   1834      * Check that the peer is allowed to use the IP address it wants.
   1835      */
   1836     if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
   1837 	error("Peer is not authorized to use remote address %I", ho->hisaddr);
   1838 	ipcp_close(f->unit, "Unauthorized remote IP address");
   1839 	return;
   1840     }
   1841 
   1842     /* set tcp compression */
   1843     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
   1844 
   1845     /*
   1846      * If we are doing dial-on-demand, the interface is already
   1847      * configured, so we put out any saved-up packets, then set the
   1848      * interface to pass IP packets.
   1849      */
   1850     if (demand) {
   1851 	if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
   1852 	    ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
   1853 	    if (go->ouraddr != wo->ouraddr) {
   1854 		warn("Local IP address changed to %I", go->ouraddr);
   1855 		script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
   1856 		wo->ouraddr = go->ouraddr;
   1857 	    } else
   1858 		script_unsetenv("OLDIPLOCAL");
   1859 	    if (ho->hisaddr != wo->hisaddr && wo->hisaddr != 0) {
   1860 		warn("Remote IP address changed to %I", ho->hisaddr);
   1861 		script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
   1862 		wo->hisaddr = ho->hisaddr;
   1863 	    } else
   1864 		script_unsetenv("OLDIPREMOTE");
   1865 
   1866 	    /* Set the interface to the new addresses */
   1867 	    mask = GetMask(go->ouraddr);
   1868 	    if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
   1869 		if (debug)
   1870 		    warn("Interface configuration failed");
   1871 		ipcp_close(f->unit, "Interface configuration failed");
   1872 		return;
   1873 	    }
   1874 
   1875 	    /* assign a default route through the interface if required */
   1876 	    if (ipcp_wantoptions[f->unit].default_route)
   1877 		if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
   1878 		    default_route_set[f->unit] = 1;
   1879 
   1880 	    /* Make a proxy ARP entry if requested. */
   1881 	    if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
   1882 		if (sifproxyarp(f->unit, ho->hisaddr))
   1883 		    proxy_arp_set[f->unit] = 1;
   1884 
   1885 	}
   1886 	demand_rexmit(PPP_IP);
   1887 	sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
   1888 
   1889     } else {
   1890 	/*
   1891 	 * Set IP addresses and (if specified) netmask.
   1892 	 */
   1893 	mask = GetMask(go->ouraddr);
   1894 
   1895 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
   1896 	if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
   1897 	    if (debug)
   1898 		warn("Interface configuration failed");
   1899 	    ipcp_close(f->unit, "Interface configuration failed");
   1900 	    return;
   1901 	}
   1902 #endif
   1903 
   1904 	/* run the pre-up script, if any, and wait for it to finish */
   1905 	ipcp_script(_PATH_IPPREUP, 1);
   1906 
   1907 	/* bring the interface up for IP */
   1908 	if (!sifup(f->unit)) {
   1909 	    if (debug)
   1910 		warn("Interface failed to come up");
   1911 	    ipcp_close(f->unit, "Interface configuration failed");
   1912 	    return;
   1913 	}
   1914 
   1915 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
   1916 	if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
   1917 	    if (debug)
   1918 		warn("Interface configuration failed");
   1919 	    ipcp_close(f->unit, "Interface configuration failed");
   1920 	    return;
   1921 	}
   1922 #endif
   1923 	sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
   1924 
   1925 	/* assign a default route through the interface if required */
   1926 	if (ipcp_wantoptions[f->unit].default_route)
   1927 	    if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
   1928 		default_route_set[f->unit] = 1;
   1929 
   1930 	/* Make a proxy ARP entry if requested. */
   1931 	if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
   1932 	    if (sifproxyarp(f->unit, ho->hisaddr))
   1933 		proxy_arp_set[f->unit] = 1;
   1934 
   1935 	ipcp_wantoptions[0].ouraddr = go->ouraddr;
   1936 
   1937 	notice("local  IP address %I", go->ouraddr);
   1938 	if (ho->hisaddr != 0)
   1939 	    notice("remote IP address %I", ho->hisaddr);
   1940 	if (go->dnsaddr[0])
   1941 	    notice("primary   DNS address %I", go->dnsaddr[0]);
   1942 	if (go->dnsaddr[1])
   1943 	    notice("secondary DNS address %I", go->dnsaddr[1]);
   1944     }
   1945 
   1946     reset_link_stats(f->unit);
   1947 
   1948     np_up(f->unit, PPP_IP);
   1949     ipcp_is_up = 1;
   1950 
   1951     notify(ip_up_notifier, 0);
   1952     if (ip_up_hook)
   1953 	ip_up_hook();
   1954 
   1955     /*
   1956      * Execute the ip-up script, like this:
   1957      *	/etc/ppp/ip-up interface tty speed local-IP remote-IP
   1958      */
   1959     if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
   1960 	ipcp_script_state = s_up;
   1961 	ipcp_script(_PATH_IPUP, 0);
   1962     }
   1963 }
   1964 
   1965 
   1966 /*
   1967  * ipcp_down - IPCP has gone DOWN.
   1968  *
   1969  * Take the IP network interface down, clear its addresses
   1970  * and delete routes through it.
   1971  */
   1972 static void
   1973 ipcp_down(f)
   1974     fsm *f;
   1975 {
   1976     IPCPDEBUG(("ipcp: down"));
   1977     /* XXX a bit IPv4-centric here, we only need to get the stats
   1978      * before the interface is marked down. */
   1979     /* XXX more correct: we must get the stats before running the notifiers,
   1980      * at least for the radius plugin */
   1981     update_link_stats(f->unit);
   1982     notify(ip_down_notifier, 0);
   1983     if (ip_down_hook)
   1984 	ip_down_hook();
   1985     if (ipcp_is_up) {
   1986 	ipcp_is_up = 0;
   1987 	np_down(f->unit, PPP_IP);
   1988     }
   1989     sifvjcomp(f->unit, 0, 0, 0);
   1990 
   1991     print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
   1992 			 * because print_link_stats() sets link_stats_valid
   1993 			 * to 0 (zero) */
   1994 
   1995     /*
   1996      * If we are doing dial-on-demand, set the interface
   1997      * to queue up outgoing packets (for now).
   1998      */
   1999     if (demand) {
   2000 	sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
   2001     } else {
   2002 	sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
   2003 	sifdown(f->unit);
   2004 	ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
   2005 			 ipcp_hisoptions[f->unit].hisaddr);
   2006     }
   2007 
   2008     /* Execute the ip-down script */
   2009     if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
   2010 	ipcp_script_state = s_down;
   2011 	ipcp_script(_PATH_IPDOWN, 0);
   2012     }
   2013 }
   2014 
   2015 
   2016 /*
   2017  * ipcp_clear_addrs() - clear the interface addresses, routes,
   2018  * proxy arp entries, etc.
   2019  */
   2020 static void
   2021 ipcp_clear_addrs(unit, ouraddr, hisaddr)
   2022     int unit;
   2023     u_int32_t ouraddr;  /* local address */
   2024     u_int32_t hisaddr;  /* remote address */
   2025 {
   2026     if (proxy_arp_set[unit]) {
   2027 	cifproxyarp(unit, hisaddr);
   2028 	proxy_arp_set[unit] = 0;
   2029     }
   2030     if (default_route_set[unit]) {
   2031 	cifdefaultroute(unit, ouraddr, hisaddr);
   2032 	default_route_set[unit] = 0;
   2033     }
   2034     cifaddr(unit, ouraddr, hisaddr);
   2035 }
   2036 
   2037 
   2038 /*
   2039  * ipcp_finished - possibly shut down the lower layers.
   2040  */
   2041 static void
   2042 ipcp_finished(f)
   2043     fsm *f;
   2044 {
   2045 	if (ipcp_is_open) {
   2046 		ipcp_is_open = 0;
   2047 		np_finished(f->unit, PPP_IP);
   2048 	}
   2049 }
   2050 
   2051 
   2052 /*
   2053  * ipcp_script_done - called when the ip-up or ip-down script
   2054  * has finished.
   2055  */
   2056 static void
   2057 ipcp_script_done(arg)
   2058     void *arg;
   2059 {
   2060     ipcp_script_pid = 0;
   2061     switch (ipcp_script_state) {
   2062     case s_up:
   2063 	if (ipcp_fsm[0].state != OPENED) {
   2064 	    ipcp_script_state = s_down;
   2065 	    ipcp_script(_PATH_IPDOWN, 0);
   2066 	}
   2067 	break;
   2068     case s_down:
   2069 	if (ipcp_fsm[0].state == OPENED) {
   2070 	    ipcp_script_state = s_up;
   2071 	    ipcp_script(_PATH_IPUP, 0);
   2072 	}
   2073 	break;
   2074     }
   2075 }
   2076 
   2077 
   2078 /*
   2079  * ipcp_script - Execute a script with arguments
   2080  * interface-name tty-name speed local-IP remote-IP.
   2081  */
   2082 static void
   2083 ipcp_script(script, wait)
   2084     char *script;
   2085     int wait;
   2086 {
   2087     char strspeed[32], strlocal[32], strremote[32];
   2088     char *argv[8];
   2089 
   2090     slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
   2091     slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
   2092     slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
   2093 
   2094     argv[0] = script;
   2095     argv[1] = ifname;
   2096     argv[2] = devnam;
   2097     argv[3] = strspeed;
   2098     argv[4] = strlocal;
   2099     argv[5] = strremote;
   2100     argv[6] = ipparam;
   2101     argv[7] = NULL;
   2102     if (wait)
   2103 	run_program(script, argv, 0, NULL, NULL, 1);
   2104     else
   2105 	ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
   2106 				      NULL, 0);
   2107 }
   2108 
   2109 /*
   2110  * create_resolv - create the replacement resolv.conf file
   2111  */
   2112 static void
   2113 create_resolv(peerdns1, peerdns2)
   2114     u_int32_t peerdns1, peerdns2;
   2115 {
   2116 #if !defined(__ANDROID__)
   2117     FILE *f;
   2118 
   2119     f = fopen(_PATH_RESOLV, "w");
   2120     if (f == NULL) {
   2121 	error("Failed to create %s: %m", _PATH_RESOLV);
   2122 	return;
   2123     }
   2124 
   2125     if (peerdns1)
   2126 	fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
   2127 
   2128     if (peerdns2)
   2129 	fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
   2130 
   2131     if (ferror(f))
   2132 	error("Write failed to %s: %m", _PATH_RESOLV);
   2133 
   2134     fclose(f);
   2135 #endif
   2136 }
   2137 
   2138 /*
   2139  * ipcp_printpkt - print the contents of an IPCP packet.
   2140  */
   2141 static char *ipcp_codenames[] = {
   2142     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
   2143     "TermReq", "TermAck", "CodeRej"
   2144 };
   2145 
   2146 static int
   2147 ipcp_printpkt(p, plen, printer, arg)
   2148     u_char *p;
   2149     int plen;
   2150     void (*printer) __P((void *, char *, ...));
   2151     void *arg;
   2152 {
   2153     int code, id, len, olen;
   2154     u_char *pstart, *optend;
   2155     u_short cishort;
   2156     u_int32_t cilong;
   2157 
   2158     if (plen < HEADERLEN)
   2159 	return 0;
   2160     pstart = p;
   2161     GETCHAR(code, p);
   2162     GETCHAR(id, p);
   2163     GETSHORT(len, p);
   2164     if (len < HEADERLEN || len > plen)
   2165 	return 0;
   2166 
   2167     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
   2168 	printer(arg, " %s", ipcp_codenames[code-1]);
   2169     else
   2170 	printer(arg, " code=0x%x", code);
   2171     printer(arg, " id=0x%x", id);
   2172     len -= HEADERLEN;
   2173     switch (code) {
   2174     case CONFREQ:
   2175     case CONFACK:
   2176     case CONFNAK:
   2177     case CONFREJ:
   2178 	/* print option list */
   2179 	while (len >= 2) {
   2180 	    GETCHAR(code, p);
   2181 	    GETCHAR(olen, p);
   2182 	    p -= 2;
   2183 	    if (olen < 2 || olen > len) {
   2184 		break;
   2185 	    }
   2186 	    printer(arg, " <");
   2187 	    len -= olen;
   2188 	    optend = p + olen;
   2189 	    switch (code) {
   2190 	    case CI_ADDRS:
   2191 		if (olen == CILEN_ADDRS) {
   2192 		    p += 2;
   2193 		    GETLONG(cilong, p);
   2194 		    printer(arg, "addrs %I", htonl(cilong));
   2195 		    GETLONG(cilong, p);
   2196 		    printer(arg, " %I", htonl(cilong));
   2197 		}
   2198 		break;
   2199 	    case CI_COMPRESSTYPE:
   2200 		if (olen >= CILEN_COMPRESS) {
   2201 		    p += 2;
   2202 		    GETSHORT(cishort, p);
   2203 		    printer(arg, "compress ");
   2204 		    switch (cishort) {
   2205 		    case IPCP_VJ_COMP:
   2206 			printer(arg, "VJ");
   2207 			break;
   2208 		    case IPCP_VJ_COMP_OLD:
   2209 			printer(arg, "old-VJ");
   2210 			break;
   2211 		    default:
   2212 			printer(arg, "0x%x", cishort);
   2213 		    }
   2214 		}
   2215 		break;
   2216 	    case CI_ADDR:
   2217 		if (olen == CILEN_ADDR) {
   2218 		    p += 2;
   2219 		    GETLONG(cilong, p);
   2220 		    printer(arg, "addr %I", htonl(cilong));
   2221 		}
   2222 		break;
   2223 	    case CI_MS_DNS1:
   2224 	    case CI_MS_DNS2:
   2225 	        p += 2;
   2226 		GETLONG(cilong, p);
   2227 		printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
   2228 			htonl(cilong));
   2229 		break;
   2230 	    case CI_MS_WINS1:
   2231 	    case CI_MS_WINS2:
   2232 	        p += 2;
   2233 		GETLONG(cilong, p);
   2234 		printer(arg, "ms-wins %I", htonl(cilong));
   2235 		break;
   2236 	    }
   2237 	    while (p < optend) {
   2238 		GETCHAR(code, p);
   2239 		printer(arg, " %.2x", code);
   2240 	    }
   2241 	    printer(arg, ">");
   2242 	}
   2243 	break;
   2244 
   2245     case TERMACK:
   2246     case TERMREQ:
   2247 	if (len > 0 && *p >= ' ' && *p < 0x7f) {
   2248 	    printer(arg, " ");
   2249 	    print_string((char *)p, len, printer, arg);
   2250 	    p += len;
   2251 	    len = 0;
   2252 	}
   2253 	break;
   2254     }
   2255 
   2256     /* print the rest of the bytes in the packet */
   2257     for (; len > 0; --len) {
   2258 	GETCHAR(code, p);
   2259 	printer(arg, " %.2x", code);
   2260     }
   2261 
   2262     return p - pstart;
   2263 }
   2264 
   2265 /*
   2266  * ip_active_pkt - see if this IP packet is worth bringing the link up for.
   2267  * We don't bring the link up for IP fragments or for TCP FIN packets
   2268  * with no data.
   2269  */
   2270 #define IP_HDRLEN	20	/* bytes */
   2271 #define IP_OFFMASK	0x1fff
   2272 #ifndef IPPROTO_TCP
   2273 #define IPPROTO_TCP	6
   2274 #endif
   2275 #define TCP_HDRLEN	20
   2276 #define TH_FIN		0x01
   2277 
   2278 /*
   2279  * We use these macros because the IP header may be at an odd address,
   2280  * and some compilers might use word loads to get th_off or ip_hl.
   2281  */
   2282 
   2283 #define net_short(x)	(((x)[0] << 8) + (x)[1])
   2284 #define get_iphl(x)	(((unsigned char *)(x))[0] & 0xF)
   2285 #define get_ipoff(x)	net_short((unsigned char *)(x) + 6)
   2286 #define get_ipproto(x)	(((unsigned char *)(x))[9])
   2287 #define get_tcpoff(x)	(((unsigned char *)(x))[12] >> 4)
   2288 #define get_tcpflags(x)	(((unsigned char *)(x))[13])
   2289 
   2290 static int
   2291 ip_active_pkt(pkt, len)
   2292     u_char *pkt;
   2293     int len;
   2294 {
   2295     u_char *tcp;
   2296     int hlen;
   2297 
   2298     len -= PPP_HDRLEN;
   2299     pkt += PPP_HDRLEN;
   2300     if (len < IP_HDRLEN)
   2301 	return 0;
   2302     if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
   2303 	return 0;
   2304     if (get_ipproto(pkt) != IPPROTO_TCP)
   2305 	return 1;
   2306     hlen = get_iphl(pkt) * 4;
   2307     if (len < hlen + TCP_HDRLEN)
   2308 	return 0;
   2309     tcp = pkt + hlen;
   2310     if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
   2311 	return 0;
   2312     return 1;
   2313 }
   2314