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