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