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