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