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