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