root/src/router/pppd.new/pppd/ipcp.c

Revision 7833, 55.5 kB (checked in by BrainSlayer, 2 years ago)

another try to strip it down. already tested by a forum user

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