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