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