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