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