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