]> git.ozlabs.org Git - ppp.git/blob - pppd/ipcp.c
Some MS-DNS changes: if the peer sends a conf-nak prompting us
[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.71 2008/03/26 10:57:11 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, 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 *, int));     /* 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     BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
700 }
701
702
703 /*
704  * ipcp_cilen - Return length of our CI.
705  * Called by fsm_sconfreq, Send Configure Request.
706  */
707 static int
708 ipcp_cilen(f)
709     fsm *f;
710 {
711     ipcp_options *go = &ipcp_gotoptions[f->unit];
712     ipcp_options *wo = &ipcp_wantoptions[f->unit];
713     ipcp_options *ho = &ipcp_hisoptions[f->unit];
714
715 #define LENCIADDRS(neg)         (neg ? CILEN_ADDRS : 0)
716 #define LENCIVJ(neg, old)       (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
717 #define LENCIADDR(neg)          (neg ? CILEN_ADDR : 0)
718 #define LENCIDNS(neg)           (neg ? (CILEN_ADDR) : 0)
719
720     /*
721      * First see if we want to change our options to the old
722      * forms because we have received old forms from the peer.
723      */
724     if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
725         go->neg_addr = 0;
726     if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
727         /* try an older style of VJ negotiation */
728         /* use the old style only if the peer did */
729         if (ho->neg_vj && ho->old_vj) {
730             go->neg_vj = 1;
731             go->old_vj = 1;
732             go->vj_protocol = ho->vj_protocol;
733         }
734     }
735
736     return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
737             LENCIVJ(go->neg_vj, go->old_vj) +
738             LENCIADDR(go->neg_addr) +
739             LENCIDNS(go->req_dns1) +
740             LENCIDNS(go->req_dns2)) ;
741 }
742
743
744 /*
745  * ipcp_addci - Add our desired CIs to a packet.
746  * Called by fsm_sconfreq, Send Configure Request.
747  */
748 static void
749 ipcp_addci(f, ucp, lenp)
750     fsm *f;
751     u_char *ucp;
752     int *lenp;
753 {
754     ipcp_options *go = &ipcp_gotoptions[f->unit];
755     int len = *lenp;
756
757 #define ADDCIADDRS(opt, neg, val1, val2) \
758     if (neg) { \
759         if (len >= CILEN_ADDRS) { \
760             u_int32_t l; \
761             PUTCHAR(opt, ucp); \
762             PUTCHAR(CILEN_ADDRS, ucp); \
763             l = ntohl(val1); \
764             PUTLONG(l, ucp); \
765             l = ntohl(val2); \
766             PUTLONG(l, ucp); \
767             len -= CILEN_ADDRS; \
768         } else \
769             go->old_addrs = 0; \
770     }
771
772 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
773     if (neg) { \
774         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
775         if (len >= vjlen) { \
776             PUTCHAR(opt, ucp); \
777             PUTCHAR(vjlen, ucp); \
778             PUTSHORT(val, ucp); \
779             if (!old) { \
780                 PUTCHAR(maxslotindex, ucp); \
781                 PUTCHAR(cflag, ucp); \
782             } \
783             len -= vjlen; \
784         } else \
785             neg = 0; \
786     }
787
788 #define ADDCIADDR(opt, neg, val) \
789     if (neg) { \
790         if (len >= CILEN_ADDR) { \
791             u_int32_t l; \
792             PUTCHAR(opt, ucp); \
793             PUTCHAR(CILEN_ADDR, ucp); \
794             l = ntohl(val); \
795             PUTLONG(l, ucp); \
796             len -= CILEN_ADDR; \
797         } else \
798             neg = 0; \
799     }
800
801 #define ADDCIDNS(opt, neg, addr) \
802     if (neg) { \
803         if (len >= CILEN_ADDR) { \
804             u_int32_t l; \
805             PUTCHAR(opt, ucp); \
806             PUTCHAR(CILEN_ADDR, ucp); \
807             l = ntohl(addr); \
808             PUTLONG(l, ucp); \
809             len -= CILEN_ADDR; \
810         } else \
811             neg = 0; \
812     }
813
814     ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
815                go->hisaddr);
816
817     ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
818             go->maxslotindex, go->cflag);
819
820     ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
821
822     ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
823
824     ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
825
826     *lenp -= len;
827 }
828
829
830 /*
831  * ipcp_ackci - Ack our CIs.
832  * Called by fsm_rconfack, Receive Configure ACK.
833  *
834  * Returns:
835  *      0 - Ack was bad.
836  *      1 - Ack was good.
837  */
838 static int
839 ipcp_ackci(f, p, len)
840     fsm *f;
841     u_char *p;
842     int len;
843 {
844     ipcp_options *go = &ipcp_gotoptions[f->unit];
845     u_short cilen, citype, cishort;
846     u_int32_t cilong;
847     u_char cimaxslotindex, cicflag;
848
849     /*
850      * CIs must be in exactly the same order that we sent...
851      * Check packet length and CI length at each step.
852      * If we find any deviations, then this packet is bad.
853      */
854
855 #define ACKCIADDRS(opt, neg, val1, val2) \
856     if (neg) { \
857         u_int32_t l; \
858         if ((len -= CILEN_ADDRS) < 0) \
859             goto bad; \
860         GETCHAR(citype, p); \
861         GETCHAR(cilen, p); \
862         if (cilen != CILEN_ADDRS || \
863             citype != opt) \
864             goto bad; \
865         GETLONG(l, p); \
866         cilong = htonl(l); \
867         if (val1 != cilong) \
868             goto bad; \
869         GETLONG(l, p); \
870         cilong = htonl(l); \
871         if (val2 != cilong) \
872             goto bad; \
873     }
874
875 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
876     if (neg) { \
877         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
878         if ((len -= vjlen) < 0) \
879             goto bad; \
880         GETCHAR(citype, p); \
881         GETCHAR(cilen, p); \
882         if (cilen != vjlen || \
883             citype != opt)  \
884             goto bad; \
885         GETSHORT(cishort, p); \
886         if (cishort != val) \
887             goto bad; \
888         if (!old) { \
889             GETCHAR(cimaxslotindex, p); \
890             if (cimaxslotindex != maxslotindex) \
891                 goto bad; \
892             GETCHAR(cicflag, p); \
893             if (cicflag != cflag) \
894                 goto bad; \
895         } \
896     }
897
898 #define ACKCIADDR(opt, neg, val) \
899     if (neg) { \
900         u_int32_t l; \
901         if ((len -= CILEN_ADDR) < 0) \
902             goto bad; \
903         GETCHAR(citype, p); \
904         GETCHAR(cilen, p); \
905         if (cilen != CILEN_ADDR || \
906             citype != opt) \
907             goto bad; \
908         GETLONG(l, p); \
909         cilong = htonl(l); \
910         if (val != cilong) \
911             goto bad; \
912     }
913
914 #define ACKCIDNS(opt, neg, addr) \
915     if (neg) { \
916         u_int32_t l; \
917         if ((len -= CILEN_ADDR) < 0) \
918             goto bad; \
919         GETCHAR(citype, p); \
920         GETCHAR(cilen, p); \
921         if (cilen != CILEN_ADDR || citype != opt) \
922             goto bad; \
923         GETLONG(l, p); \
924         cilong = htonl(l); \
925         if (addr != cilong) \
926             goto bad; \
927     }
928
929     ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
930                go->hisaddr);
931
932     ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
933             go->maxslotindex, go->cflag);
934
935     ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
936
937     ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
938
939     ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
940
941     /*
942      * If there are any remaining CIs, then this packet is bad.
943      */
944     if (len != 0)
945         goto bad;
946     return (1);
947
948 bad:
949     IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
950     return (0);
951 }
952
953 /*
954  * ipcp_nakci - Peer has sent a NAK for some of our CIs.
955  * This should not modify any state if the Nak is bad
956  * or if IPCP is in the OPENED state.
957  * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
958  *
959  * Returns:
960  *      0 - Nak was bad.
961  *      1 - Nak was good.
962  */
963 static int
964 ipcp_nakci(f, p, len, treat_as_reject)
965     fsm *f;
966     u_char *p;
967     int len;
968     int treat_as_reject;
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 (treat_as_reject) {
1045                    try.old_addrs = 0;
1046                } else {
1047                    if (go->accept_local && ciaddr1) {
1048                        /* take his idea of our address */
1049                        try.ouraddr = ciaddr1;
1050                    }
1051                    if (go->accept_remote && ciaddr2) {
1052                        /* take his idea of his address */
1053                        try.hisaddr = ciaddr2;
1054                    }
1055                }
1056         );
1057
1058     /*
1059      * Accept the peer's value of maxslotindex provided that it
1060      * is less than what we asked for.  Turn off slot-ID compression
1061      * if the peer wants.  Send old-style compress-type option if
1062      * the peer wants.
1063      */
1064     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1065             if (treat_as_reject) {
1066                 try.neg_vj = 0;
1067             } else if (cilen == CILEN_VJ) {
1068                 GETCHAR(cimaxslotindex, p);
1069                 GETCHAR(cicflag, p);
1070                 if (cishort == IPCP_VJ_COMP) {
1071                     try.old_vj = 0;
1072                     if (cimaxslotindex < go->maxslotindex)
1073                         try.maxslotindex = cimaxslotindex;
1074                     if (!cicflag)
1075                         try.cflag = 0;
1076                 } else {
1077                     try.neg_vj = 0;
1078                 }
1079             } else {
1080                 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1081                     try.old_vj = 1;
1082                     try.vj_protocol = cishort;
1083                 } else {
1084                     try.neg_vj = 0;
1085                 }
1086             }
1087             );
1088
1089     NAKCIADDR(CI_ADDR, neg_addr,
1090               if (treat_as_reject) {
1091                   try.neg_addr = 0;
1092                   try.old_addrs = 0;
1093               } else if (go->accept_local && ciaddr1) {
1094                   /* take his idea of our address */
1095                   try.ouraddr = ciaddr1;
1096               }
1097               );
1098
1099     NAKCIDNS(CI_MS_DNS1, req_dns1,
1100              if (treat_as_reject) {
1101                  try.req_dns1 = 0;
1102              } else {
1103                  try.dnsaddr[0] = cidnsaddr;
1104              }
1105              );
1106
1107     NAKCIDNS(CI_MS_DNS2, req_dns2,
1108              if (treat_as_reject) {
1109                  try.req_dns2 = 0;
1110              } else {
1111                  try.dnsaddr[1] = cidnsaddr;
1112              }
1113              );
1114
1115     /*
1116      * There may be remaining CIs, if the peer is requesting negotiation
1117      * on an option that we didn't include in our request packet.
1118      * If they want to negotiate about IP addresses, we comply.
1119      * If they want us to ask for compression, we refuse.
1120      * If they want us to ask for ms-dns, we do that, since some
1121      * peers get huffy if we don't.
1122      */
1123     while (len >= CILEN_VOID) {
1124         GETCHAR(citype, p);
1125         GETCHAR(cilen, p);
1126         if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1127             goto bad;
1128         next = p + cilen - 2;
1129
1130         switch (citype) {
1131         case CI_COMPRESSTYPE:
1132             if (go->neg_vj || no.neg_vj ||
1133                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1134                 goto bad;
1135             no.neg_vj = 1;
1136             break;
1137         case CI_ADDRS:
1138             if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1139                 || cilen != CILEN_ADDRS)
1140                 goto bad;
1141             try.neg_addr = 0;
1142             GETLONG(l, p);
1143             ciaddr1 = htonl(l);
1144             if (ciaddr1 && go->accept_local)
1145                 try.ouraddr = ciaddr1;
1146             GETLONG(l, p);
1147             ciaddr2 = htonl(l);
1148             if (ciaddr2 && go->accept_remote)
1149                 try.hisaddr = ciaddr2;
1150             no.old_addrs = 1;
1151             break;
1152         case CI_ADDR:
1153             if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1154                 goto bad;
1155             try.old_addrs = 0;
1156             GETLONG(l, p);
1157             ciaddr1 = htonl(l);
1158             if (ciaddr1 && go->accept_local)
1159                 try.ouraddr = ciaddr1;
1160             if (try.ouraddr != 0)
1161                 try.neg_addr = 1;
1162             no.neg_addr = 1;
1163             break;
1164         case CI_MS_DNS1:
1165             if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1166                 goto bad;
1167             GETLONG(l, p);
1168             try.dnsaddr[0] = htonl(l);
1169             try.req_dns1 = 1;
1170             no.req_dns1 = 1;
1171             break;
1172         case CI_MS_DNS2:
1173             if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1174                 goto bad;
1175             GETLONG(l, p);
1176             try.dnsaddr[1] = htonl(l);
1177             try.req_dns2 = 1;
1178             no.req_dns2 = 1;
1179             break;
1180         }
1181         p = next;
1182     }
1183
1184     /*
1185      * OK, the Nak is good.  Now we can update state.
1186      * If there are any remaining options, we ignore them.
1187      */
1188     if (f->state != OPENED)
1189         *go = try;
1190
1191     return 1;
1192
1193 bad:
1194     IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1195     return 0;
1196 }
1197
1198
1199 /*
1200  * ipcp_rejci - Reject some of our CIs.
1201  * Callback from fsm_rconfnakrej.
1202  */
1203 static int
1204 ipcp_rejci(f, p, len)
1205     fsm *f;
1206     u_char *p;
1207     int len;
1208 {
1209     ipcp_options *go = &ipcp_gotoptions[f->unit];
1210     u_char cimaxslotindex, ciflag, cilen;
1211     u_short cishort;
1212     u_int32_t cilong;
1213     ipcp_options try;           /* options to request next time */
1214
1215     try = *go;
1216     /*
1217      * Any Rejected CIs must be in exactly the same order that we sent.
1218      * Check packet length and CI length at each step.
1219      * If we find any deviations, then this packet is bad.
1220      */
1221 #define REJCIADDRS(opt, neg, val1, val2) \
1222     if ((neg) && \
1223         (cilen = p[1]) == CILEN_ADDRS && \
1224         len >= cilen && \
1225         p[0] == opt) { \
1226         u_int32_t l; \
1227         len -= cilen; \
1228         INCPTR(2, p); \
1229         GETLONG(l, p); \
1230         cilong = htonl(l); \
1231         /* Check rejected value. */ \
1232         if (cilong != val1) \
1233             goto bad; \
1234         GETLONG(l, p); \
1235         cilong = htonl(l); \
1236         /* Check rejected value. */ \
1237         if (cilong != val2) \
1238             goto bad; \
1239         try.old_addrs = 0; \
1240     }
1241
1242 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1243     if (go->neg && \
1244         p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1245         len >= p[1] && \
1246         p[0] == opt) { \
1247         len -= p[1]; \
1248         INCPTR(2, p); \
1249         GETSHORT(cishort, p); \
1250         /* Check rejected value. */  \
1251         if (cishort != val) \
1252             goto bad; \
1253         if (!old) { \
1254            GETCHAR(cimaxslotindex, p); \
1255            if (cimaxslotindex != maxslot) \
1256              goto bad; \
1257            GETCHAR(ciflag, p); \
1258            if (ciflag != cflag) \
1259              goto bad; \
1260         } \
1261         try.neg = 0; \
1262      }
1263
1264 #define REJCIADDR(opt, neg, val) \
1265     if (go->neg && \
1266         (cilen = p[1]) == CILEN_ADDR && \
1267         len >= cilen && \
1268         p[0] == opt) { \
1269         u_int32_t l; \
1270         len -= cilen; \
1271         INCPTR(2, p); \
1272         GETLONG(l, p); \
1273         cilong = htonl(l); \
1274         /* Check rejected value. */ \
1275         if (cilong != val) \
1276             goto bad; \
1277         try.neg = 0; \
1278     }
1279
1280 #define REJCIDNS(opt, neg, dnsaddr) \
1281     if (go->neg && \
1282         ((cilen = p[1]) == CILEN_ADDR) && \
1283         len >= cilen && \
1284         p[0] == opt) { \
1285         u_int32_t l; \
1286         len -= cilen; \
1287         INCPTR(2, p); \
1288         GETLONG(l, p); \
1289         cilong = htonl(l); \
1290         /* Check rejected value. */ \
1291         if (cilong != dnsaddr) \
1292             goto bad; \
1293         try.neg = 0; \
1294     }
1295
1296
1297     REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1298                go->ouraddr, go->hisaddr);
1299
1300     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1301             go->maxslotindex, go->cflag);
1302
1303     REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1304
1305     REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1306
1307     REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1308
1309     /*
1310      * If there are any remaining CIs, then this packet is bad.
1311      */
1312     if (len != 0)
1313         goto bad;
1314     /*
1315      * Now we can update state.
1316      */
1317     if (f->state != OPENED)
1318         *go = try;
1319     return 1;
1320
1321 bad:
1322     IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1323     return 0;
1324 }
1325
1326
1327 /*
1328  * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1329  * Callback from fsm_rconfreq, Receive Configure Request
1330  *
1331  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1332  * appropriately.  If reject_if_disagree is non-zero, doesn't return
1333  * CONFNAK; returns CONFREJ if it can't return CONFACK.
1334  */
1335 static int
1336 ipcp_reqci(f, inp, len, reject_if_disagree)
1337     fsm *f;
1338     u_char *inp;                /* Requested CIs */
1339     int *len;                   /* Length of requested CIs */
1340     int reject_if_disagree;
1341 {
1342     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1343     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1344     ipcp_options *ao = &ipcp_allowoptions[f->unit];
1345     u_char *cip, *next;         /* Pointer to current and next CIs */
1346     u_short cilen, citype;      /* Parsed len, type */
1347     u_short cishort;            /* Parsed short value */
1348     u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1349     int rc = CONFACK;           /* Final packet return code */
1350     int orc;                    /* Individual option return code */
1351     u_char *p;                  /* Pointer to next char to parse */
1352     u_char *ucp = inp;          /* Pointer to current output char */
1353     int l = *len;               /* Length left */
1354     u_char maxslotindex, cflag;
1355     int d;
1356
1357     /*
1358      * Reset all his options.
1359      */
1360     BZERO(ho, sizeof(*ho));
1361     
1362     /*
1363      * Process all his options.
1364      */
1365     next = inp;
1366     while (l) {
1367         orc = CONFACK;                  /* Assume success */
1368         cip = p = next;                 /* Remember begining of CI */
1369         if (l < 2 ||                    /* Not enough data for CI header or */
1370             p[1] < 2 ||                 /*  CI length too small or */
1371             p[1] > l) {                 /*  CI length too big? */
1372             IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1373             orc = CONFREJ;              /* Reject bad CI */
1374             cilen = l;                  /* Reject till end of packet */
1375             l = 0;                      /* Don't loop again */
1376             goto endswitch;
1377         }
1378         GETCHAR(citype, p);             /* Parse CI type */
1379         GETCHAR(cilen, p);              /* Parse CI length */
1380         l -= cilen;                     /* Adjust remaining length */
1381         next += cilen;                  /* Step to next CI */
1382
1383         switch (citype) {               /* Check CI type */
1384         case CI_ADDRS:
1385             if (!ao->old_addrs || ho->neg_addr ||
1386                 cilen != CILEN_ADDRS) { /* Check CI length */
1387                 orc = CONFREJ;          /* Reject CI */
1388                 break;
1389             }
1390
1391             /*
1392              * If he has no address, or if we both have his address but
1393              * disagree about it, then NAK it with our idea.
1394              * In particular, if we don't know his address, but he does,
1395              * then accept it.
1396              */
1397             GETLONG(tl, p);             /* Parse source address (his) */
1398             ciaddr1 = htonl(tl);
1399             if (ciaddr1 != wo->hisaddr
1400                 && (ciaddr1 == 0 || !wo->accept_remote)) {
1401                 orc = CONFNAK;
1402                 if (!reject_if_disagree) {
1403                     DECPTR(sizeof(u_int32_t), p);
1404                     tl = ntohl(wo->hisaddr);
1405                     PUTLONG(tl, p);
1406                 }
1407             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1408                 /*
1409                  * If neither we nor he knows his address, reject the option.
1410                  */
1411                 orc = CONFREJ;
1412                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
1413                 break;
1414             }
1415
1416             /*
1417              * If he doesn't know our address, or if we both have our address
1418              * but disagree about it, then NAK it with our idea.
1419              */
1420             GETLONG(tl, p);             /* Parse desination address (ours) */
1421             ciaddr2 = htonl(tl);
1422             if (ciaddr2 != wo->ouraddr) {
1423                 if (ciaddr2 == 0 || !wo->accept_local) {
1424                     orc = CONFNAK;
1425                     if (!reject_if_disagree) {
1426                         DECPTR(sizeof(u_int32_t), p);
1427                         tl = ntohl(wo->ouraddr);
1428                         PUTLONG(tl, p);
1429                     }
1430                 } else {
1431                     wo->ouraddr = ciaddr2;      /* accept peer's idea */
1432                 }
1433             }
1434
1435             ho->old_addrs = 1;
1436             ho->hisaddr = ciaddr1;
1437             ho->ouraddr = ciaddr2;
1438             break;
1439
1440         case CI_ADDR:
1441             if (!ao->neg_addr || ho->old_addrs ||
1442                 cilen != CILEN_ADDR) {  /* Check CI length */
1443                 orc = CONFREJ;          /* Reject CI */
1444                 break;
1445             }
1446
1447             /*
1448              * If he has no address, or if we both have his address but
1449              * disagree about it, then NAK it with our idea.
1450              * In particular, if we don't know his address, but he does,
1451              * then accept it.
1452              */
1453             GETLONG(tl, p);     /* Parse source address (his) */
1454             ciaddr1 = htonl(tl);
1455             if (ciaddr1 != wo->hisaddr
1456                 && (ciaddr1 == 0 || !wo->accept_remote)) {
1457                 orc = CONFNAK;
1458                 if (!reject_if_disagree) {
1459                     DECPTR(sizeof(u_int32_t), p);
1460                     tl = ntohl(wo->hisaddr);
1461                     PUTLONG(tl, p);
1462                 }
1463             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1464                 /*
1465                  * Don't ACK an address of 0.0.0.0 - reject it instead.
1466                  */
1467                 orc = CONFREJ;
1468                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
1469                 break;
1470             }
1471         
1472             ho->neg_addr = 1;
1473             ho->hisaddr = ciaddr1;
1474             break;
1475
1476         case CI_MS_DNS1:
1477         case CI_MS_DNS2:
1478             /* Microsoft primary or secondary DNS request */
1479             d = citype == CI_MS_DNS2;
1480
1481             /* If we do not have a DNS address then we cannot send it */
1482             if (ao->dnsaddr[d] == 0 ||
1483                 cilen != CILEN_ADDR) {  /* Check CI length */
1484                 orc = CONFREJ;          /* Reject CI */
1485                 break;
1486             }
1487             GETLONG(tl, p);
1488             if (htonl(tl) != ao->dnsaddr[d]) {
1489                 DECPTR(sizeof(u_int32_t), p);
1490                 tl = ntohl(ao->dnsaddr[d]);
1491                 PUTLONG(tl, p);
1492                 orc = CONFNAK;
1493             }
1494             break;
1495
1496         case CI_MS_WINS1:
1497         case CI_MS_WINS2:
1498             /* Microsoft primary or secondary WINS request */
1499             d = citype == CI_MS_WINS2;
1500
1501             /* If we do not have a DNS address then we cannot send it */
1502             if (ao->winsaddr[d] == 0 ||
1503                 cilen != CILEN_ADDR) {  /* Check CI length */
1504                 orc = CONFREJ;          /* Reject CI */
1505                 break;
1506             }
1507             GETLONG(tl, p);
1508             if (htonl(tl) != ao->winsaddr[d]) {
1509                 DECPTR(sizeof(u_int32_t), p);
1510                 tl = ntohl(ao->winsaddr[d]);
1511                 PUTLONG(tl, p);
1512                 orc = CONFNAK;
1513             }
1514             break;
1515         
1516         case CI_COMPRESSTYPE:
1517             if (!ao->neg_vj ||
1518                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1519                 orc = CONFREJ;
1520                 break;
1521             }
1522             GETSHORT(cishort, p);
1523
1524             if (!(cishort == IPCP_VJ_COMP ||
1525                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1526                 orc = CONFREJ;
1527                 break;
1528             }
1529
1530             ho->neg_vj = 1;
1531             ho->vj_protocol = cishort;
1532             if (cilen == CILEN_VJ) {
1533                 GETCHAR(maxslotindex, p);
1534                 if (maxslotindex > ao->maxslotindex) { 
1535                     orc = CONFNAK;
1536                     if (!reject_if_disagree){
1537                         DECPTR(1, p);
1538                         PUTCHAR(ao->maxslotindex, p);
1539                     }
1540                 }
1541                 GETCHAR(cflag, p);
1542                 if (cflag && !ao->cflag) {
1543                     orc = CONFNAK;
1544                     if (!reject_if_disagree){
1545                         DECPTR(1, p);
1546                         PUTCHAR(wo->cflag, p);
1547                     }
1548                 }
1549                 ho->maxslotindex = maxslotindex;
1550                 ho->cflag = cflag;
1551             } else {
1552                 ho->old_vj = 1;
1553                 ho->maxslotindex = MAX_STATES - 1;
1554                 ho->cflag = 1;
1555             }
1556             break;
1557
1558         default:
1559             orc = CONFREJ;
1560             break;
1561         }
1562 endswitch:
1563         if (orc == CONFACK &&           /* Good CI */
1564             rc != CONFACK)              /*  but prior CI wasnt? */
1565             continue;                   /* Don't send this one */
1566
1567         if (orc == CONFNAK) {           /* Nak this CI? */
1568             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1569                 orc = CONFREJ;          /* Get tough if so */
1570             else {
1571                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1572                     continue;           /* Don't send this one */
1573                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1574                     rc = CONFNAK;       /* Not anymore... */
1575                     ucp = inp;          /* Backup */
1576                 }
1577             }
1578         }
1579
1580         if (orc == CONFREJ &&           /* Reject this CI */
1581             rc != CONFREJ) {            /*  but no prior ones? */
1582             rc = CONFREJ;
1583             ucp = inp;                  /* Backup */
1584         }
1585
1586         /* Need to move CI? */
1587         if (ucp != cip)
1588             BCOPY(cip, ucp, cilen);     /* Move it */
1589
1590         /* Update output pointer */
1591         INCPTR(cilen, ucp);
1592     }
1593
1594     /*
1595      * If we aren't rejecting this packet, and we want to negotiate
1596      * their address, and they didn't send their address, then we
1597      * send a NAK with a CI_ADDR option appended.  We assume the
1598      * input buffer is long enough that we can append the extra
1599      * option safely.
1600      */
1601     if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1602         wo->req_addr && !reject_if_disagree) {
1603         if (rc == CONFACK) {
1604             rc = CONFNAK;
1605             ucp = inp;                  /* reset pointer */
1606             wo->req_addr = 0;           /* don't ask again */
1607         }
1608         PUTCHAR(CI_ADDR, ucp);
1609         PUTCHAR(CILEN_ADDR, ucp);
1610         tl = ntohl(wo->hisaddr);
1611         PUTLONG(tl, ucp);
1612     }
1613
1614     *len = ucp - inp;                   /* Compute output length */
1615     IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1616     return (rc);                        /* Return final code */
1617 }
1618
1619
1620 /*
1621  * ip_check_options - check that any IP-related options are OK,
1622  * and assign appropriate defaults.
1623  */
1624 static void
1625 ip_check_options()
1626 {
1627     struct hostent *hp;
1628     u_int32_t local;
1629     ipcp_options *wo = &ipcp_wantoptions[0];
1630
1631     /*
1632      * Default our local IP address based on our hostname.
1633      * If local IP address already given, don't bother.
1634      */
1635     if (wo->ouraddr == 0 && !disable_defaultip) {
1636         /*
1637          * Look up our hostname (possibly with domain name appended)
1638          * and take the first IP address as our local IP address.
1639          * If there isn't an IP address for our hostname, too bad.
1640          */
1641         wo->accept_local = 1;   /* don't insist on this default value */
1642         if ((hp = gethostbyname(hostname)) != NULL) {
1643             local = *(u_int32_t *)hp->h_addr;
1644             if (local != 0 && !bad_ip_adrs(local))
1645                 wo->ouraddr = local;
1646         }
1647     }
1648     ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1649 }
1650
1651
1652 /*
1653  * ip_demand_conf - configure the interface as though
1654  * IPCP were up, for use with dial-on-demand.
1655  */
1656 static int
1657 ip_demand_conf(u)
1658     int u;
1659 {
1660     ipcp_options *wo = &ipcp_wantoptions[u];
1661
1662     if (wo->hisaddr == 0) {
1663         /* make up an arbitrary address for the peer */
1664         wo->hisaddr = htonl(0x0a707070 + ifunit);
1665         wo->accept_remote = 1;
1666     }
1667     if (wo->ouraddr == 0) {
1668         /* make up an arbitrary address for us */
1669         wo->ouraddr = htonl(0x0a404040 + ifunit);
1670         wo->accept_local = 1;
1671         ask_for_local = 0;      /* don't tell the peer this address */
1672     }
1673     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1674         return 0;
1675     ipcp_script(_PATH_IPPREUP, 1);
1676     if (!sifup(u))
1677         return 0;
1678     if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1679         return 0;
1680     if (wo->default_route)
1681         if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1682             default_route_set[u] = 1;
1683     if (wo->proxy_arp)
1684         if (sifproxyarp(u, wo->hisaddr))
1685             proxy_arp_set[u] = 1;
1686
1687     notice("local  IP address %I", wo->ouraddr);
1688     notice("remote IP address %I", wo->hisaddr);
1689
1690     return 1;
1691 }
1692
1693
1694 /*
1695  * ipcp_up - IPCP has come UP.
1696  *
1697  * Configure the IP network interface appropriately and bring it up.
1698  */
1699 static void
1700 ipcp_up(f)
1701     fsm *f;
1702 {
1703     u_int32_t mask;
1704     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1705     ipcp_options *go = &ipcp_gotoptions[f->unit];
1706     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1707
1708     IPCPDEBUG(("ipcp: up"));
1709
1710     /*
1711      * We must have a non-zero IP address for both ends of the link.
1712      */
1713     if (!ho->neg_addr && !ho->old_addrs)
1714         ho->hisaddr = wo->hisaddr;
1715
1716     if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1717         && wo->ouraddr != 0) {
1718         error("Peer refused to agree to our IP address");
1719         ipcp_close(f->unit, "Refused our IP address");
1720         return;
1721     }
1722     if (go->ouraddr == 0) {
1723         error("Could not determine local IP address");
1724         ipcp_close(f->unit, "Could not determine local IP address");
1725         return;
1726     }
1727     if (ho->hisaddr == 0) {
1728         ho->hisaddr = htonl(0x0a404040 + ifunit);
1729         warn("Could not determine remote IP address: defaulting to %I",
1730              ho->hisaddr);
1731     }
1732     script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1733     script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1734
1735     if (go->dnsaddr[0])
1736         script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1737     if (go->dnsaddr[1])
1738         script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1739     if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1740         script_setenv("USEPEERDNS", "1", 0);
1741         create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1742     }
1743
1744     /*
1745      * Check that the peer is allowed to use the IP address it wants.
1746      */
1747     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1748         error("Peer is not authorized to use remote address %I", ho->hisaddr);
1749         ipcp_close(f->unit, "Unauthorized remote IP address");
1750         return;
1751     }
1752
1753     /* set tcp compression */
1754     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1755
1756     /*
1757      * If we are doing dial-on-demand, the interface is already
1758      * configured, so we put out any saved-up packets, then set the
1759      * interface to pass IP packets.
1760      */
1761     if (demand) {
1762         if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1763             ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1764             if (go->ouraddr != wo->ouraddr) {
1765                 warn("Local IP address changed to %I", go->ouraddr);
1766                 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1767                 wo->ouraddr = go->ouraddr;
1768             } else
1769                 script_unsetenv("OLDIPLOCAL");
1770             if (ho->hisaddr != wo->hisaddr) {
1771                 warn("Remote IP address changed to %I", ho->hisaddr);
1772                 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1773                 wo->hisaddr = ho->hisaddr;
1774             } else
1775                 script_unsetenv("OLDIPREMOTE");
1776
1777             /* Set the interface to the new addresses */
1778             mask = GetMask(go->ouraddr);
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
1786             /* assign a default route through the interface if required */
1787             if (ipcp_wantoptions[f->unit].default_route) 
1788                 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1789                     default_route_set[f->unit] = 1;
1790
1791             /* Make a proxy ARP entry if requested. */
1792             if (ipcp_wantoptions[f->unit].proxy_arp)
1793                 if (sifproxyarp(f->unit, ho->hisaddr))
1794                     proxy_arp_set[f->unit] = 1;
1795
1796         }
1797         demand_rexmit(PPP_IP);
1798         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1799
1800     } else {
1801         /*
1802          * Set IP addresses and (if specified) netmask.
1803          */
1804         mask = GetMask(go->ouraddr);
1805
1806 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1807         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1808             if (debug)
1809                 warn("Interface configuration failed");
1810             ipcp_close(f->unit, "Interface configuration failed");
1811             return;
1812         }
1813 #endif
1814
1815         /* run the pre-up script, if any, and wait for it to finish */
1816         ipcp_script(_PATH_IPPREUP, 1);
1817
1818         /* bring the interface up for IP */
1819         if (!sifup(f->unit)) {
1820             if (debug)
1821                 warn("Interface failed to come up");
1822             ipcp_close(f->unit, "Interface configuration failed");
1823             return;
1824         }
1825
1826 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1827         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1828             if (debug)
1829                 warn("Interface configuration failed");
1830             ipcp_close(f->unit, "Interface configuration failed");
1831             return;
1832         }
1833 #endif
1834         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1835
1836         /* assign a default route through the interface if required */
1837         if (ipcp_wantoptions[f->unit].default_route) 
1838             if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1839                 default_route_set[f->unit] = 1;
1840
1841         /* Make a proxy ARP entry if requested. */
1842         if (ipcp_wantoptions[f->unit].proxy_arp)
1843             if (sifproxyarp(f->unit, ho->hisaddr))
1844                 proxy_arp_set[f->unit] = 1;
1845
1846         ipcp_wantoptions[0].ouraddr = go->ouraddr;
1847
1848         notice("local  IP address %I", go->ouraddr);
1849         notice("remote IP address %I", ho->hisaddr);
1850         if (go->dnsaddr[0])
1851             notice("primary   DNS address %I", go->dnsaddr[0]);
1852         if (go->dnsaddr[1])
1853             notice("secondary DNS address %I", go->dnsaddr[1]);
1854     }
1855
1856     reset_link_stats(f->unit);
1857
1858     np_up(f->unit, PPP_IP);
1859     ipcp_is_up = 1;
1860
1861     notify(ip_up_notifier, 0);
1862     if (ip_up_hook)
1863         ip_up_hook();
1864
1865     /*
1866      * Execute the ip-up script, like this:
1867      *  /etc/ppp/ip-up interface tty speed local-IP remote-IP
1868      */
1869     if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1870         ipcp_script_state = s_up;
1871         ipcp_script(_PATH_IPUP, 0);
1872     }
1873 }
1874
1875
1876 /*
1877  * ipcp_down - IPCP has gone DOWN.
1878  *
1879  * Take the IP network interface down, clear its addresses
1880  * and delete routes through it.
1881  */
1882 static void
1883 ipcp_down(f)
1884     fsm *f;
1885 {
1886     IPCPDEBUG(("ipcp: down"));
1887     /* XXX a bit IPv4-centric here, we only need to get the stats
1888      * before the interface is marked down. */
1889     /* XXX more correct: we must get the stats before running the notifiers,
1890      * at least for the radius plugin */
1891     update_link_stats(f->unit);
1892     notify(ip_down_notifier, 0);
1893     if (ip_down_hook)
1894         ip_down_hook();
1895     if (ipcp_is_up) {
1896         ipcp_is_up = 0;
1897         np_down(f->unit, PPP_IP);
1898     }
1899     sifvjcomp(f->unit, 0, 0, 0);
1900
1901     print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1902                          * because print_link_stats() sets link_stats_valid
1903                          * to 0 (zero) */
1904
1905     /*
1906      * If we are doing dial-on-demand, set the interface
1907      * to queue up outgoing packets (for now).
1908      */
1909     if (demand) {
1910         sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1911     } else {
1912         sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1913         sifdown(f->unit);
1914         ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1915                          ipcp_hisoptions[f->unit].hisaddr);
1916     }
1917
1918     /* Execute the ip-down script */
1919     if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1920         ipcp_script_state = s_down;
1921         ipcp_script(_PATH_IPDOWN, 0);
1922     }
1923 }
1924
1925
1926 /*
1927  * ipcp_clear_addrs() - clear the interface addresses, routes,
1928  * proxy arp entries, etc.
1929  */
1930 static void
1931 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1932     int unit;
1933     u_int32_t ouraddr;  /* local address */
1934     u_int32_t hisaddr;  /* remote address */
1935 {
1936     if (proxy_arp_set[unit]) {
1937         cifproxyarp(unit, hisaddr);
1938         proxy_arp_set[unit] = 0;
1939     }
1940     if (default_route_set[unit]) {
1941         cifdefaultroute(unit, ouraddr, hisaddr);
1942         default_route_set[unit] = 0;
1943     }
1944     cifaddr(unit, ouraddr, hisaddr);
1945 }
1946
1947
1948 /*
1949  * ipcp_finished - possibly shut down the lower layers.
1950  */
1951 static void
1952 ipcp_finished(f)
1953     fsm *f;
1954 {
1955         if (ipcp_is_open) {
1956                 ipcp_is_open = 0;
1957                 np_finished(f->unit, PPP_IP);
1958         }
1959 }
1960
1961
1962 /*
1963  * ipcp_script_done - called when the ip-up or ip-down script
1964  * has finished.
1965  */
1966 static void
1967 ipcp_script_done(arg)
1968     void *arg;
1969 {
1970     ipcp_script_pid = 0;
1971     switch (ipcp_script_state) {
1972     case s_up:
1973         if (ipcp_fsm[0].state != OPENED) {
1974             ipcp_script_state = s_down;
1975             ipcp_script(_PATH_IPDOWN, 0);
1976         }
1977         break;
1978     case s_down:
1979         if (ipcp_fsm[0].state == OPENED) {
1980             ipcp_script_state = s_up;
1981             ipcp_script(_PATH_IPUP, 0);
1982         }
1983         break;
1984     }
1985 }
1986
1987
1988 /*
1989  * ipcp_script - Execute a script with arguments
1990  * interface-name tty-name speed local-IP remote-IP.
1991  */
1992 static void
1993 ipcp_script(script, wait)
1994     char *script;
1995     int wait;
1996 {
1997     char strspeed[32], strlocal[32], strremote[32];
1998     char *argv[8];
1999
2000     slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2001     slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2002     slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2003
2004     argv[0] = script;
2005     argv[1] = ifname;
2006     argv[2] = devnam;
2007     argv[3] = strspeed;
2008     argv[4] = strlocal;
2009     argv[5] = strremote;
2010     argv[6] = ipparam;
2011     argv[7] = NULL;
2012     if (wait)
2013         run_program(script, argv, 0, NULL, NULL, 1);
2014     else
2015         ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2016                                       NULL, 0);
2017 }
2018
2019 /*
2020  * create_resolv - create the replacement resolv.conf file
2021  */
2022 static void
2023 create_resolv(peerdns1, peerdns2)
2024     u_int32_t peerdns1, peerdns2;
2025 {
2026     FILE *f;
2027
2028     f = fopen(_PATH_RESOLV, "w");
2029     if (f == NULL) {
2030         error("Failed to create %s: %m", _PATH_RESOLV);
2031         return;
2032     }
2033
2034     if (peerdns1)
2035         fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2036
2037     if (peerdns2)
2038         fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2039
2040     if (ferror(f))
2041         error("Write failed to %s: %m", _PATH_RESOLV);
2042
2043     fclose(f);
2044 }
2045
2046 /*
2047  * ipcp_printpkt - print the contents of an IPCP packet.
2048  */
2049 static char *ipcp_codenames[] = {
2050     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2051     "TermReq", "TermAck", "CodeRej"
2052 };
2053
2054 static int
2055 ipcp_printpkt(p, plen, printer, arg)
2056     u_char *p;
2057     int plen;
2058     void (*printer) __P((void *, char *, ...));
2059     void *arg;
2060 {
2061     int code, id, len, olen;
2062     u_char *pstart, *optend;
2063     u_short cishort;
2064     u_int32_t cilong;
2065
2066     if (plen < HEADERLEN)
2067         return 0;
2068     pstart = p;
2069     GETCHAR(code, p);
2070     GETCHAR(id, p);
2071     GETSHORT(len, p);
2072     if (len < HEADERLEN || len > plen)
2073         return 0;
2074
2075     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2076         printer(arg, " %s", ipcp_codenames[code-1]);
2077     else
2078         printer(arg, " code=0x%x", code);
2079     printer(arg, " id=0x%x", id);
2080     len -= HEADERLEN;
2081     switch (code) {
2082     case CONFREQ:
2083     case CONFACK:
2084     case CONFNAK:
2085     case CONFREJ:
2086         /* print option list */
2087         while (len >= 2) {
2088             GETCHAR(code, p);
2089             GETCHAR(olen, p);
2090             p -= 2;
2091             if (olen < 2 || olen > len) {
2092                 break;
2093             }
2094             printer(arg, " <");
2095             len -= olen;
2096             optend = p + olen;
2097             switch (code) {
2098             case CI_ADDRS:
2099                 if (olen == CILEN_ADDRS) {
2100                     p += 2;
2101                     GETLONG(cilong, p);
2102                     printer(arg, "addrs %I", htonl(cilong));
2103                     GETLONG(cilong, p);
2104                     printer(arg, " %I", htonl(cilong));
2105                 }
2106                 break;
2107             case CI_COMPRESSTYPE:
2108                 if (olen >= CILEN_COMPRESS) {
2109                     p += 2;
2110                     GETSHORT(cishort, p);
2111                     printer(arg, "compress ");
2112                     switch (cishort) {
2113                     case IPCP_VJ_COMP:
2114                         printer(arg, "VJ");
2115                         break;
2116                     case IPCP_VJ_COMP_OLD:
2117                         printer(arg, "old-VJ");
2118                         break;
2119                     default:
2120                         printer(arg, "0x%x", cishort);
2121                     }
2122                 }
2123                 break;
2124             case CI_ADDR:
2125                 if (olen == CILEN_ADDR) {
2126                     p += 2;
2127                     GETLONG(cilong, p);
2128                     printer(arg, "addr %I", htonl(cilong));
2129                 }
2130                 break;
2131             case CI_MS_DNS1:
2132             case CI_MS_DNS2:
2133                 p += 2;
2134                 GETLONG(cilong, p);
2135                 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2136                         htonl(cilong));
2137                 break;
2138             case CI_MS_WINS1:
2139             case CI_MS_WINS2:
2140                 p += 2;
2141                 GETLONG(cilong, p);
2142                 printer(arg, "ms-wins %I", htonl(cilong));
2143                 break;
2144             }
2145             while (p < optend) {
2146                 GETCHAR(code, p);
2147                 printer(arg, " %.2x", code);
2148             }
2149             printer(arg, ">");
2150         }
2151         break;
2152
2153     case TERMACK:
2154     case TERMREQ:
2155         if (len > 0 && *p >= ' ' && *p < 0x7f) {
2156             printer(arg, " ");
2157             print_string((char *)p, len, printer, arg);
2158             p += len;
2159             len = 0;
2160         }
2161         break;
2162     }
2163
2164     /* print the rest of the bytes in the packet */
2165     for (; len > 0; --len) {
2166         GETCHAR(code, p);
2167         printer(arg, " %.2x", code);
2168     }
2169
2170     return p - pstart;
2171 }
2172
2173 /*
2174  * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2175  * We don't bring the link up for IP fragments or for TCP FIN packets
2176  * with no data.
2177  */
2178 #define IP_HDRLEN       20      /* bytes */
2179 #define IP_OFFMASK      0x1fff
2180 #ifndef IPPROTO_TCP
2181 #define IPPROTO_TCP     6
2182 #endif
2183 #define TCP_HDRLEN      20
2184 #define TH_FIN          0x01
2185
2186 /*
2187  * We use these macros because the IP header may be at an odd address,
2188  * and some compilers might use word loads to get th_off or ip_hl.
2189  */
2190
2191 #define net_short(x)    (((x)[0] << 8) + (x)[1])
2192 #define get_iphl(x)     (((unsigned char *)(x))[0] & 0xF)
2193 #define get_ipoff(x)    net_short((unsigned char *)(x) + 6)
2194 #define get_ipproto(x)  (((unsigned char *)(x))[9])
2195 #define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
2196 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2197
2198 static int
2199 ip_active_pkt(pkt, len)
2200     u_char *pkt;
2201     int len;
2202 {
2203     u_char *tcp;
2204     int hlen;
2205
2206     len -= PPP_HDRLEN;
2207     pkt += PPP_HDRLEN;
2208     if (len < IP_HDRLEN)
2209         return 0;
2210     if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2211         return 0;
2212     if (get_ipproto(pkt) != IPPROTO_TCP)
2213         return 1;
2214     hlen = get_iphl(pkt) * 4;
2215     if (len < hlen + TCP_HDRLEN)
2216         return 0;
2217     tcp = pkt + hlen;
2218     if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
2219         return 0;
2220     return 1;
2221 }