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