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