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