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