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