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