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