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