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