]> git.ozlabs.org Git - ppp.git/blob - pppd/ipcp.c
Update to state of pppd-new directory
[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.19.2.1 1995/06/01 07:01: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 <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34
35 #include "pppd.h"
36 #include "fsm.h"
37 #include "ipcp.h"
38 #include "pathnames.h"
39
40 /* global vars */
41 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
42 ipcp_options ipcp_gotoptions[NUM_PPP];  /* Options that peer ack'd */
43 ipcp_options ipcp_allowoptions[NUM_PPP];        /* Options we allow peer to request */
44 ipcp_options ipcp_hisoptions[NUM_PPP];  /* Options that we ack'd */
45
46 /* local vars */
47 static int cis_received[NUM_PPP];       /* # Conf-Reqs received */
48 static int default_route_set[NUM_PPP];  /* Have set up a default route */
49 static int proxy_arp_set[NUM_PPP];      /* Have created proxy arp entry */
50
51 /*
52  * Callbacks for fsm code.  (CI = Configuration Information)
53  */
54 static void ipcp_resetci __P((fsm *));  /* Reset our CI */
55 static int  ipcp_cilen __P((fsm *));            /* Return length of our CI */
56 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
57 static int  ipcp_ackci __P((fsm *, u_char *, int));     /* Peer ack'd our CI */
58 static int  ipcp_nakci __P((fsm *, u_char *, int));     /* Peer nak'd our CI */
59 static int  ipcp_rejci __P((fsm *, u_char *, int));     /* Peer rej'd our CI */
60 static int  ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
61 static void ipcp_up __P((fsm *));               /* We're UP */
62 static void ipcp_down __P((fsm *));             /* We're DOWN */
63 static void ipcp_script __P((fsm *, char *)); /* Run an up/down script */
64 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
65
66 fsm ipcp_fsm[NUM_PPP];          /* IPCP fsm structure */
67
68 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
69     ipcp_resetci,               /* Reset our Configuration Information */
70     ipcp_cilen,                 /* Length of our Configuration Information */
71     ipcp_addci,                 /* Add our Configuration Information */
72     ipcp_ackci,                 /* ACK our Configuration Information */
73     ipcp_nakci,                 /* NAK our Configuration Information */
74     ipcp_rejci,                 /* Reject our Configuration Information */
75     ipcp_reqci,                 /* Request peer's Configuration Information */
76     ipcp_up,                    /* Called when fsm reaches OPENED state */
77     ipcp_down,                  /* Called when fsm leaves OPENED state */
78     NULL,                       /* Called when we want the lower layer up */
79     ipcp_finished,              /* Called when we want the lower layer down */
80     NULL,                       /* Called when Protocol-Reject received */
81     NULL,                       /* Retransmission is necessary */
82     NULL,                       /* Called to handle protocol-specific codes */
83     "IPCP"                      /* String name of protocol */
84 };
85
86 /*
87  * Lengths of configuration options.
88  */
89 #define CILEN_VOID      2
90 #define CILEN_COMPRESS  4       /* min length for compression protocol opt. */
91 #define CILEN_VJ        6       /* length for RFC1332 Van-Jacobson opt. */
92 #define CILEN_ADDR      6       /* new-style single address option */
93 #define CILEN_ADDRS     10      /* old-style dual address option */
94
95
96 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
97                          (x) == CONFNAK ? "NAK" : "REJ")
98
99
100 /*
101  * Make a string representation of a network IP address.
102  */
103 char *
104 ip_ntoa(ipaddr)
105 u_int32_t ipaddr;
106 {
107     static char b[64];
108
109     ipaddr = ntohl(ipaddr);
110
111     sprintf(b, "%d.%d.%d.%d",
112             (u_char)(ipaddr >> 24),
113             (u_char)(ipaddr >> 16),
114             (u_char)(ipaddr >> 8),
115             (u_char)(ipaddr));
116     return b;
117 }
118
119
120 /*
121  * ipcp_init - Initialize IPCP.
122  */
123 void
124 ipcp_init(unit)
125     int unit;
126 {
127     fsm *f = &ipcp_fsm[unit];
128     ipcp_options *wo = &ipcp_wantoptions[unit];
129     ipcp_options *ao = &ipcp_allowoptions[unit];
130
131     f->unit = unit;
132     f->protocol = PPP_IPCP;
133     f->callbacks = &ipcp_callbacks;
134     fsm_init(&ipcp_fsm[unit]);
135
136     wo->neg_addr = 1;
137     wo->old_addrs = 0;
138     wo->ouraddr = 0;
139     wo->hisaddr = 0;
140
141     wo->neg_vj = 1;
142     wo->old_vj = 0;
143     wo->vj_protocol = IPCP_VJ_COMP;
144     wo->maxslotindex = MAX_STATES - 1; /* really max index */
145     wo->cflag = 1;
146
147     /* max slots and slot-id compression are currently hardwired in */
148     /* ppp_if.c to 16 and 1, this needs to be changed (among other */
149     /* things) gmc */
150
151     ao->neg_addr = 1;
152     ao->neg_vj = 1;
153     ao->maxslotindex = MAX_STATES - 1;
154     ao->cflag = 1;
155     ao->default_route = 1;
156     ao->proxy_arp = 1;
157 }
158
159
160 /*
161  * ipcp_open - IPCP is allowed to come up.
162  */
163 void
164 ipcp_open(unit)
165     int unit;
166 {
167     fsm_open(&ipcp_fsm[unit]);
168 }
169
170
171 /*
172  * ipcp_close - Take IPCP down.
173  */
174 void
175 ipcp_close(unit)
176     int unit;
177 {
178     fsm_close(&ipcp_fsm[unit]);
179 }
180
181
182 /*
183  * ipcp_lowerup - The lower layer is up.
184  */
185 void
186 ipcp_lowerup(unit)
187     int unit;
188 {
189     fsm_lowerup(&ipcp_fsm[unit]);
190 }
191
192
193 /*
194  * ipcp_lowerdown - The lower layer is down.
195  */
196 void
197 ipcp_lowerdown(unit)
198     int unit;
199 {
200     fsm_lowerdown(&ipcp_fsm[unit]);
201 }
202
203
204 /*
205  * ipcp_input - Input IPCP packet.
206  */
207 void
208 ipcp_input(unit, p, len)
209     int unit;
210     u_char *p;
211     int len;
212 {
213     fsm_input(&ipcp_fsm[unit], p, len);
214 }
215
216
217 /*
218  * ipcp_protrej - A Protocol-Reject was received for IPCP.
219  *
220  * Pretend the lower layer went down, so we shut up.
221  */
222 void
223 ipcp_protrej(unit)
224     int unit;
225 {
226     fsm_lowerdown(&ipcp_fsm[unit]);
227 }
228
229
230 /*
231  * ipcp_resetci - Reset our CI.
232  */
233 static void
234 ipcp_resetci(f)
235     fsm *f;
236 {
237     ipcp_options *wo = &ipcp_wantoptions[f->unit];
238
239     wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
240     if (wo->ouraddr == 0)
241         wo->accept_local = 1;
242     if (wo->hisaddr == 0)
243         wo->accept_remote = 1;
244     ipcp_gotoptions[f->unit] = *wo;
245     cis_received[f->unit] = 0;
246 }
247
248
249 /*
250  * ipcp_cilen - Return length of our CI.
251  */
252 static int
253 ipcp_cilen(f)
254     fsm *f;
255 {
256     ipcp_options *go = &ipcp_gotoptions[f->unit];
257
258 #define LENCIVJ(neg, old)       (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
259 #define LENCIADDR(neg, old)     (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
260
261     return (LENCIADDR(go->neg_addr, go->old_addrs) +
262             LENCIVJ(go->neg_vj, go->old_vj));
263 }
264
265
266 /*
267  * ipcp_addci - Add our desired CIs to a packet.
268  */
269 static void
270 ipcp_addci(f, ucp, lenp)
271     fsm *f;
272     u_char *ucp;
273     int *lenp;
274 {
275     ipcp_options *wo = &ipcp_wantoptions[f->unit];
276     ipcp_options *go = &ipcp_gotoptions[f->unit];
277     ipcp_options *ho = &ipcp_hisoptions[f->unit];
278     int len = *lenp;
279
280 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
281     if (neg) { \
282         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
283         if (len >= vjlen) { \
284             PUTCHAR(opt, ucp); \
285             PUTCHAR(vjlen, ucp); \
286             PUTSHORT(val, ucp); \
287             if (!old) { \
288                 PUTCHAR(maxslotindex, ucp); \
289                 PUTCHAR(cflag, ucp); \
290             } \
291             len -= vjlen; \
292         } else \
293             neg = 0; \
294     }
295
296 #define ADDCIADDR(opt, neg, old, val1, val2) \
297     if (neg) { \
298         int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
299         if (len >= addrlen) { \
300             u_int32_t l; \
301             PUTCHAR(opt, ucp); \
302             PUTCHAR(addrlen, ucp); \
303             l = ntohl(val1); \
304             PUTLONG(l, ucp); \
305             if (old) { \
306                 l = ntohl(val2); \
307                 PUTLONG(l, ucp); \
308             } \
309             len -= addrlen; \
310         } else \
311             neg = 0; \
312     }
313
314     /*
315      * First see if we want to change our options to the old
316      * forms because we have received old forms from the peer.
317      */
318     if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
319         /* use the old style of address negotiation */
320         go->neg_addr = 1;
321         go->old_addrs = 1;
322     }
323     if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
324         /* try an older style of VJ negotiation */
325         if (cis_received[f->unit] == 0) {
326             /* keep trying the new style until we see some CI from the peer */
327             go->neg_vj = 1;
328         } else {
329             /* use the old style only if the peer did */
330             if (ho->neg_vj && ho->old_vj) {
331                 go->neg_vj = 1;
332                 go->old_vj = 1;
333                 go->vj_protocol = ho->vj_protocol;
334             }
335         }
336     }
337
338     ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
339               go->old_addrs, go->ouraddr, go->hisaddr);
340
341     ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
342             go->maxslotindex, go->cflag);
343
344     *lenp -= len;
345 }
346
347
348 /*
349  * ipcp_ackci - Ack our CIs.
350  *
351  * Returns:
352  *      0 - Ack was bad.
353  *      1 - Ack was good.
354  */
355 static int
356 ipcp_ackci(f, p, len)
357     fsm *f;
358     u_char *p;
359     int len;
360 {
361     ipcp_options *go = &ipcp_gotoptions[f->unit];
362     u_short cilen, citype, cishort;
363     u_int32_t cilong;
364     u_char cimaxslotindex, cicflag;
365
366     /*
367      * CIs must be in exactly the same order that we sent...
368      * Check packet length and CI length at each step.
369      * If we find any deviations, then this packet is bad.
370      */
371
372 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
373     if (neg) { \
374         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
375         if ((len -= vjlen) < 0) \
376             goto bad; \
377         GETCHAR(citype, p); \
378         GETCHAR(cilen, p); \
379         if (cilen != vjlen || \
380             citype != opt)  \
381             goto bad; \
382         GETSHORT(cishort, p); \
383         if (cishort != val) \
384             goto bad; \
385         if (!old) { \
386             GETCHAR(cimaxslotindex, p); \
387             if (cimaxslotindex != maxslotindex) \
388                 goto bad; \
389             GETCHAR(cicflag, p); \
390             if (cicflag != cflag) \
391                 goto bad; \
392         } \
393     }
394
395 #define ACKCIADDR(opt, neg, old, val1, val2) \
396     if (neg) { \
397         int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
398         u_int32_t l; \
399         if ((len -= addrlen) < 0) \
400             goto bad; \
401         GETCHAR(citype, p); \
402         GETCHAR(cilen, p); \
403         if (cilen != addrlen || \
404             citype != opt) \
405             goto bad; \
406         GETLONG(l, p); \
407         cilong = htonl(l); \
408         if (val1 != cilong) \
409             goto bad; \
410         if (old) { \
411             GETLONG(l, p); \
412             cilong = htonl(l); \
413             if (val2 != cilong) \
414                 goto bad; \
415         } \
416     }
417
418     ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
419               go->old_addrs, go->ouraddr, go->hisaddr);
420
421     ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
422             go->maxslotindex, go->cflag);
423
424     /*
425      * If there are any remaining CIs, then this packet is bad.
426      */
427     if (len != 0)
428         goto bad;
429     return (1);
430
431 bad:
432     IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
433     return (0);
434 }
435
436 /*
437  * ipcp_nakci - Peer has sent a NAK for some of our CIs.
438  * This should not modify any state if the Nak is bad
439  * or if IPCP is in the OPENED state.
440  *
441  * Returns:
442  *      0 - Nak was bad.
443  *      1 - Nak was good.
444  */
445 static int
446 ipcp_nakci(f, p, len)
447     fsm *f;
448     u_char *p;
449     int len;
450 {
451     ipcp_options *go = &ipcp_gotoptions[f->unit];
452     u_char cimaxslotindex, cicflag;
453     u_char citype, cilen, *next;
454     u_short cishort;
455     u_int32_t ciaddr1, ciaddr2, l;
456     ipcp_options no;            /* options we've seen Naks for */
457     ipcp_options try;           /* options to request next time */
458
459     BZERO(&no, sizeof(no));
460     try = *go;
461
462     /*
463      * Any Nak'd CIs must be in exactly the same order that we sent.
464      * Check packet length and CI length at each step.
465      * If we find any deviations, then this packet is bad.
466      */
467 #define NAKCIADDR(opt, neg, old, code) \
468     if (go->neg && \
469         len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
470         p[1] == cilen && \
471         p[0] == opt) { \
472         len -= cilen; \
473         INCPTR(2, p); \
474         GETLONG(l, p); \
475         ciaddr1 = htonl(l); \
476         if (old) { \
477             GETLONG(l, p); \
478             ciaddr2 = htonl(l); \
479             no.old_addrs = 1; \
480         } else \
481             ciaddr2 = 0; \
482         no.neg = 1; \
483         code \
484     }
485
486 #define NAKCIVJ(opt, neg, code) \
487     if (go->neg && \
488         ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
489         len >= cilen && \
490         p[0] == opt) { \
491         len -= cilen; \
492         INCPTR(2, p); \
493         GETSHORT(cishort, p); \
494         no.neg = 1; \
495         code \
496     }
497
498     /*
499      * Accept the peer's idea of {our,his} address, if different
500      * from our idea, only if the accept_{local,remote} flag is set.
501      */
502     NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
503               if (go->accept_local && ciaddr1) { /* Do we know our address? */
504                   try.ouraddr = ciaddr1;
505                   IPCPDEBUG((LOG_INFO, "local IP address %s",
506                              ip_ntoa(ciaddr1)));
507               }
508               if (go->accept_remote && ciaddr2) { /* Does he know his? */
509                   try.hisaddr = ciaddr2;
510                   IPCPDEBUG((LOG_INFO, "remote IP address %s",
511                              ip_ntoa(ciaddr2)));
512               }
513               );
514
515     /*
516      * Accept the peer's value of maxslotindex provided that it
517      * is less than what we asked for.  Turn off slot-ID compression
518      * if the peer wants.  Send old-style compress-type option if
519      * the peer wants.
520      */
521     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
522             if (cilen == CILEN_VJ) {
523                 GETCHAR(cimaxslotindex, p);
524                 GETCHAR(cicflag, p);
525                 if (cishort == IPCP_VJ_COMP) {
526                     try.old_vj = 0;
527                     if (cimaxslotindex < go->maxslotindex)
528                         try.maxslotindex = cimaxslotindex;
529                     if (!cicflag)
530                         try.cflag = 0;
531                 } else {
532                     try.neg_vj = 0;
533                 }
534             } else {
535                 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
536                     try.old_vj = 1;
537                     try.vj_protocol = cishort;
538                 } else {
539                     try.neg_vj = 0;
540                 }
541             }
542             );
543
544     /*
545      * There may be remaining CIs, if the peer is requesting negotiation
546      * on an option that we didn't include in our request packet.
547      * If they want to negotiate about IP addresses, we comply.
548      * If they want us to ask for compression, we refuse.
549      */
550     while (len > CILEN_VOID) {
551         GETCHAR(citype, p);
552         GETCHAR(cilen, p);
553         if( (len -= cilen) < 0 )
554             goto bad;
555         next = p + cilen - 2;
556
557         switch (citype) {
558         case CI_COMPRESSTYPE:
559             if (go->neg_vj || no.neg_vj ||
560                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
561                 goto bad;
562             no.neg_vj = 1;
563             break;
564         case CI_ADDRS:
565             if (go->neg_addr && go->old_addrs || no.old_addrs
566                 || cilen != CILEN_ADDRS)
567                 goto bad;
568             try.neg_addr = 1;
569             try.old_addrs = 1;
570             GETLONG(l, p);
571             ciaddr1 = htonl(l);
572             if (ciaddr1 && go->accept_local)
573                 try.ouraddr = ciaddr1;
574             GETLONG(l, p);
575             ciaddr2 = htonl(l);
576             if (ciaddr2 && go->accept_remote)
577                 try.hisaddr = ciaddr2;
578             no.old_addrs = 1;
579             break;
580         case CI_ADDR:
581             if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
582                 goto bad;
583             try.old_addrs = 0;
584             GETLONG(l, p);
585             ciaddr1 = htonl(l);
586             if (ciaddr1 && go->accept_local)
587                 try.ouraddr = ciaddr1;
588             if (try.ouraddr != 0)
589                 try.neg_addr = 1;
590             no.neg_addr = 1;
591             break;
592         default:
593             goto bad;
594         }
595         p = next;
596     }
597
598     /* If there is still anything left, this packet is bad. */
599     if (len != 0)
600         goto bad;
601
602     /*
603      * OK, the Nak is good.  Now we can update state.
604      */
605     if (f->state != OPENED)
606         *go = try;
607
608     return 1;
609
610 bad:
611     IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
612     return 0;
613 }
614
615
616 /*
617  * ipcp_rejci - Reject some of our CIs.
618  */
619 static int
620 ipcp_rejci(f, p, len)
621     fsm *f;
622     u_char *p;
623     int len;
624 {
625     ipcp_options *go = &ipcp_gotoptions[f->unit];
626     u_char cimaxslotindex, ciflag, cilen;
627     u_short cishort;
628     u_int32_t cilong;
629     ipcp_options try;           /* options to request next time */
630
631     try = *go;
632     /*
633      * Any Rejected CIs must be in exactly the same order that we sent.
634      * Check packet length and CI length at each step.
635      * If we find any deviations, then this packet is bad.
636      */
637 #define REJCIADDR(opt, neg, old, val1, val2) \
638     if (go->neg && \
639         len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
640         p[1] == cilen && \
641         p[0] == opt) { \
642         u_int32_t l; \
643         len -= cilen; \
644         INCPTR(2, p); \
645         GETLONG(l, p); \
646         cilong = htonl(l); \
647         /* Check rejected value. */ \
648         if (cilong != val1) \
649             goto bad; \
650         if (old) { \
651             GETLONG(l, p); \
652             cilong = htonl(l); \
653             /* Check rejected value. */ \
654             if (cilong != val2) \
655                 goto bad; \
656         } \
657         try.neg = 0; \
658     }
659
660 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
661     if (go->neg && \
662         p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
663         len >= p[1] && \
664         p[0] == opt) { \
665         len -= p[1]; \
666         INCPTR(2, p); \
667         GETSHORT(cishort, p); \
668         /* Check rejected value. */  \
669         if (cishort != val) \
670             goto bad; \
671         if (!old) { \
672            GETCHAR(cimaxslotindex, p); \
673            if (cimaxslotindex != maxslot) \
674              goto bad; \
675            GETCHAR(ciflag, p); \
676            if (ciflag != cflag) \
677              goto bad; \
678         } \
679         try.neg = 0; \
680      }
681
682     REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
683               go->old_addrs, go->ouraddr, go->hisaddr);
684
685     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
686             go->maxslotindex, go->cflag);
687
688     /*
689      * If there are any remaining CIs, then this packet is bad.
690      */
691     if (len != 0)
692         goto bad;
693     /*
694      * Now we can update state.
695      */
696     if (f->state != OPENED)
697         *go = try;
698     return 1;
699
700 bad:
701     IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
702     return 0;
703 }
704
705
706 /*
707  * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
708  *
709  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
710  * appropriately.  If reject_if_disagree is non-zero, doesn't return
711  * CONFNAK; returns CONFREJ if it can't return CONFACK.
712  */
713 static int
714 ipcp_reqci(f, inp, len, reject_if_disagree)
715     fsm *f;
716     u_char *inp;                /* Requested CIs */
717     int *len;                   /* Length of requested CIs */
718     int reject_if_disagree;
719 {
720     ipcp_options *wo = &ipcp_wantoptions[f->unit];
721     ipcp_options *ho = &ipcp_hisoptions[f->unit];
722     ipcp_options *ao = &ipcp_allowoptions[f->unit];
723     ipcp_options *go = &ipcp_gotoptions[f->unit];
724     u_char *cip, *next;         /* Pointer to current and next CIs */
725     u_short cilen, citype;      /* Parsed len, type */
726     u_short cishort;            /* Parsed short value */
727     u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
728     int rc = CONFACK;           /* Final packet return code */
729     int orc;                    /* Individual option return code */
730     u_char *p;                  /* Pointer to next char to parse */
731     u_char *ucp = inp;          /* Pointer to current output char */
732     int l = *len;               /* Length left */
733     u_char maxslotindex, cflag;
734
735     /*
736      * Reset all his options.
737      */
738     BZERO(ho, sizeof(*ho));
739     
740     /*
741      * Process all his options.
742      */
743     next = inp;
744     while (l) {
745         orc = CONFACK;                  /* Assume success */
746         cip = p = next;                 /* Remember begining of CI */
747         if (l < 2 ||                    /* Not enough data for CI header or */
748             p[1] < 2 ||                 /*  CI length too small or */
749             p[1] > l) {                 /*  CI length too big? */
750             IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
751             orc = CONFREJ;              /* Reject bad CI */
752             cilen = l;                  /* Reject till end of packet */
753             l = 0;                      /* Don't loop again */
754             goto endswitch;
755         }
756         GETCHAR(citype, p);             /* Parse CI type */
757         GETCHAR(cilen, p);              /* Parse CI length */
758         l -= cilen;                     /* Adjust remaining length */
759         next += cilen;                  /* Step to next CI */
760
761         switch (citype) {               /* Check CI type */
762         case CI_ADDRS:
763             IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
764             if (!ao->neg_addr ||
765                 cilen != CILEN_ADDRS) { /* Check CI length */
766                 orc = CONFREJ;          /* Reject CI */
767                 break;
768             }
769
770             /*
771              * If he has no address, or if we both have his address but
772              * disagree about it, then NAK it with our idea.
773              * In particular, if we don't know his address, but he does,
774              * then accept it.
775              */
776             GETLONG(tl, p);             /* Parse source address (his) */
777             ciaddr1 = htonl(tl);
778             IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
779             if (ciaddr1 != wo->hisaddr
780                 && (ciaddr1 == 0 || !wo->accept_remote)) {
781                 orc = CONFNAK;
782                 if (!reject_if_disagree) {
783                     DECPTR(sizeof(u_int32_t), p);
784                     tl = ntohl(wo->hisaddr);
785                     PUTLONG(tl, p);
786                 }
787             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
788                 /*
789                  * If neither we nor he knows his address, reject the option.
790                  */
791                 orc = CONFREJ;
792                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
793                 break;
794             }
795
796             /*
797              * If he doesn't know our address, or if we both have our address
798              * but disagree about it, then NAK it with our idea.
799              */
800             GETLONG(tl, p);             /* Parse desination address (ours) */
801             ciaddr2 = htonl(tl);
802             IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
803             if (ciaddr2 != wo->ouraddr) {
804                 if (ciaddr2 == 0 || !wo->accept_local) {
805                     orc = CONFNAK;
806                     if (!reject_if_disagree) {
807                         DECPTR(sizeof(u_int32_t), p);
808                         tl = ntohl(wo->ouraddr);
809                         PUTLONG(tl, p);
810                     }
811                 } else {
812                     go->ouraddr = ciaddr2;      /* accept peer's idea */
813                 }
814             }
815
816             ho->neg_addr = 1;
817             ho->old_addrs = 1;
818             ho->hisaddr = ciaddr1;
819             ho->ouraddr = ciaddr2;
820             break;
821
822         case CI_ADDR:
823             IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
824
825             if (!ao->neg_addr ||
826                 cilen != CILEN_ADDR) {  /* Check CI length */
827                 orc = CONFREJ;          /* Reject CI */
828                 break;
829             }
830
831             /*
832              * If he has no address, or if we both have his address but
833              * disagree about it, then NAK it with our idea.
834              * In particular, if we don't know his address, but he does,
835              * then accept it.
836              */
837             GETLONG(tl, p);     /* Parse source address (his) */
838             ciaddr1 = htonl(tl);
839             IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
840             if (ciaddr1 != wo->hisaddr
841                 && (ciaddr1 == 0 || !wo->accept_remote)) {
842                 orc = CONFNAK;
843                 if (!reject_if_disagree) {
844                     DECPTR(sizeof(u_int32_t), p);
845                     tl = ntohl(wo->hisaddr);
846                     PUTLONG(tl, p);
847                 }
848             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
849                 /*
850                  * Don't ACK an address of 0.0.0.0 - reject it instead.
851                  */
852                 orc = CONFREJ;
853                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
854                 break;
855             }
856         
857             ho->neg_addr = 1;
858             ho->hisaddr = ciaddr1;
859             break;
860         
861         case CI_COMPRESSTYPE:
862             IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
863             if (!ao->neg_vj ||
864                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
865                 orc = CONFREJ;
866                 break;
867             }
868             GETSHORT(cishort, p);
869             IPCPDEBUG((LOG_INFO, "(%d)", cishort));
870
871             if (!(cishort == IPCP_VJ_COMP ||
872                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
873                 orc = CONFREJ;
874                 break;
875             }
876
877             ho->neg_vj = 1;
878             ho->vj_protocol = cishort;
879             if (cilen == CILEN_VJ) {
880                 GETCHAR(maxslotindex, p);
881                 if (maxslotindex > ao->maxslotindex) { 
882                     orc = CONFNAK;
883                     if (!reject_if_disagree){
884                         DECPTR(1, p);
885                         PUTCHAR(ao->maxslotindex, p);
886                     }
887                 }
888                 GETCHAR(cflag, p);
889                 if (cflag && !ao->cflag) {
890                     orc = CONFNAK;
891                     if (!reject_if_disagree){
892                         DECPTR(1, p);
893                         PUTCHAR(wo->cflag, p);
894                     }
895                 }
896                 ho->maxslotindex = maxslotindex;
897                 ho->cflag = cflag;
898             } else {
899                 ho->old_vj = 1;
900                 ho->maxslotindex = MAX_STATES - 1;
901                 ho->cflag = 1;
902             }
903             break;
904
905         default:
906             orc = CONFREJ;
907             break;
908         }
909
910 endswitch:
911         IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
912
913         if (orc == CONFACK &&           /* Good CI */
914             rc != CONFACK)              /*  but prior CI wasnt? */
915             continue;                   /* Don't send this one */
916
917         if (orc == CONFNAK) {           /* Nak this CI? */
918             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
919                 orc = CONFREJ;          /* Get tough if so */
920             else {
921                 if (rc == CONFREJ)      /* Rejecting prior CI? */
922                     continue;           /* Don't send this one */
923                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
924                     rc = CONFNAK;       /* Not anymore... */
925                     ucp = inp;          /* Backup */
926                 }
927             }
928         }
929
930         if (orc == CONFREJ &&           /* Reject this CI */
931             rc != CONFREJ) {            /*  but no prior ones? */
932             rc = CONFREJ;
933             ucp = inp;                  /* Backup */
934         }
935
936         /* Need to move CI? */
937         if (ucp != cip)
938             BCOPY(cip, ucp, cilen);     /* Move it */
939
940         /* Update output pointer */
941         INCPTR(cilen, ucp);
942     }
943
944     /*
945      * If we aren't rejecting this packet, and we want to negotiate
946      * their address, and they didn't send their address, then we
947      * send a NAK with a CI_ADDR option appended.  We assume the
948      * input buffer is long enough that we can append the extra
949      * option safely.
950      */
951     if (rc != CONFREJ && !ho->neg_addr &&
952         wo->req_addr && !reject_if_disagree) {
953         if (rc == CONFACK) {
954             rc = CONFNAK;
955             ucp = inp;                  /* reset pointer */
956             wo->req_addr = 0;           /* don't ask again */
957         }
958         PUTCHAR(CI_ADDR, ucp);
959         PUTCHAR(CILEN_ADDR, ucp);
960         tl = ntohl(wo->hisaddr);
961         PUTLONG(tl, ucp);
962     }
963
964     *len = ucp - inp;                   /* Compute output length */
965     IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
966     return (rc);                        /* Return final code */
967 }
968
969
970 /*
971  * ipcp_demand_conf - configure the interface as though
972  * IPCP were up, for use with dial-on-demand.
973  */
974 int
975 ipcp_demand_conf(u)
976     int u;
977 {
978     ipcp_options *wo = &ipcp_wantoptions[u];
979
980     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
981         return 0;
982     if (!sifup(u))
983         return 0;
984     if (wo->default_route)
985         if (sifdefaultroute(u, wo->hisaddr))
986             default_route_set[u] = 1;
987     if (wo->proxy_arp)
988         if (sifproxyarp(u, wo->hisaddr))
989             proxy_arp_set[u] = 1;
990
991     syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(wo->ouraddr));
992     syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
993
994     return 1;
995 }
996
997
998 /*
999  * ipcp_demand_reset - reset the interface, which has been
1000  * configured for dial-on-demand.
1001  */
1002 void
1003 ipcp_demand_reset(u)
1004     int u;
1005 {
1006     ipcp_options *wo = &ipcp_wantoptions[u];
1007
1008     if (proxy_arp_set[u]) {
1009         cifproxyarp(u, wo->hisaddr);
1010         proxy_arp_set[u] = 0;
1011     }
1012     if (default_route_set[u]) {
1013         cifdefaultroute(u, wo->hisaddr);
1014         default_route_set[u] = 0;
1015     }
1016     sifdown(u);
1017     cifaddr(u, wo->ouraddr, wo->hisaddr);
1018 }
1019
1020
1021 /*
1022  * ipcp_up - IPCP has come UP.
1023  *
1024  * Configure the IP network interface appropriately and bring it up.
1025  */
1026 static void
1027 ipcp_up(f)
1028     fsm *f;
1029 {
1030     u_int32_t mask;
1031     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1032     ipcp_options *go = &ipcp_gotoptions[f->unit];
1033     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1034
1035     np_up(f->unit, PPP_IP);
1036     IPCPDEBUG((LOG_INFO, "ipcp: up"));
1037
1038     /*
1039      * We must have a non-zero IP address for both ends of the link.
1040      */
1041     if (!ho->neg_addr)
1042         ho->hisaddr = wo->hisaddr;
1043
1044     if (ho->hisaddr == 0) {
1045         syslog(LOG_ERR, "Could not determine remote IP address");
1046         ipcp_close(f->unit);
1047         return;
1048     }
1049     if (go->ouraddr == 0) {
1050         syslog(LOG_ERR, "Could not determine local IP address");
1051         ipcp_close(f->unit);
1052         return;
1053     }
1054
1055     /*
1056      * Check that the peer is allowed to use the IP address it wants.
1057      */
1058     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1059         syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1060                ip_ntoa(ho->hisaddr));
1061         ipcp_close(f->unit);
1062         return;
1063     }
1064
1065     /* set tcp compression */
1066     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1067
1068     /*
1069      * If we are doing dial-on-demand, the interface is already
1070      * configured, so we put out any saved-up packets, then set the
1071      * interface to pass IP packets.
1072      */
1073     if (demand) {
1074         if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1075             syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1076             ipcp_close(f->unit);
1077             return;
1078         }
1079         demand_rexmit(PPP_IP);
1080         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1081
1082     } else {
1083
1084         /*
1085          * Set IP addresses and (if specified) netmask.
1086          */
1087         mask = GetMask(go->ouraddr);
1088         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1089             IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1090             ipcp_close(f->unit);
1091             return;
1092         }
1093
1094         /* bring the interface up for IP */
1095         if (!sifup(f->unit)) {
1096             IPCPDEBUG((LOG_WARNING, "sifup failed"));
1097             ipcp_close(f->unit);
1098             return;
1099         }
1100
1101         /* assign a default route through the interface if required */
1102         if (ipcp_wantoptions[f->unit].default_route) 
1103             if (sifdefaultroute(f->unit, ho->hisaddr))
1104                 default_route_set[f->unit] = 1;
1105
1106         /* Make a proxy ARP entry if requested. */
1107         if (ipcp_wantoptions[f->unit].proxy_arp)
1108             if (sifproxyarp(f->unit, ho->hisaddr))
1109                 proxy_arp_set[f->unit] = 1;
1110
1111         syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1112         syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1113     }
1114
1115     /*
1116      * Execute the ip-up script, like this:
1117      *  /etc/ppp/ip-up interface tty speed local-IP remote-IP
1118      */
1119     ipcp_script(f, _PATH_IPUP);
1120
1121 }
1122
1123
1124 /*
1125  * ipcp_down - IPCP has gone DOWN.
1126  *
1127  * Take the IP network interface down, clear its addresses
1128  * and delete routes through it.
1129  */
1130 static void
1131 ipcp_down(f)
1132     fsm *f;
1133 {
1134     u_int32_t ouraddr, hisaddr;
1135
1136     np_down(f->unit, PPP_IP);
1137     IPCPDEBUG((LOG_INFO, "ipcp: down"));
1138
1139     /*
1140      * If we are doing dial-on-demand, set the interface
1141      * to drop outgoing packets with an error (for now).
1142      */
1143     if (demand) {
1144         sifnpmode(f->unit, PPP_IP, NPMODE_ERROR);
1145
1146     } else {
1147         ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1148         hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1149         if (proxy_arp_set[f->unit]) {
1150             cifproxyarp(f->unit, hisaddr);
1151             proxy_arp_set[f->unit] = 0;
1152         }
1153         if (default_route_set[f->unit]) {
1154             cifdefaultroute(f->unit, hisaddr);
1155             default_route_set[f->unit] = 0;
1156         }
1157         sifdown(f->unit);
1158         cifaddr(f->unit, ouraddr, hisaddr);
1159     }
1160
1161     /* Execute the ip-down script */
1162     ipcp_script(f, _PATH_IPDOWN);
1163 }
1164
1165
1166 /*
1167  * ipcp_finished - possibly shut down the lower layers.
1168  */
1169 static void
1170 ipcp_finished(f)
1171     fsm *f;
1172 {
1173     np_finished(f->unit, PPP_IP);
1174 }
1175
1176
1177 /*
1178  * ipcp_script - Execute a script with arguments
1179  * interface-name tty-name speed local-IP remote-IP.
1180  */
1181 static void
1182 ipcp_script(f, script)
1183     fsm *f;
1184     char *script;
1185 {
1186     char strspeed[32], strlocal[32], strremote[32];
1187     char *argv[8];
1188
1189     sprintf(strspeed, "%d", baud_rate);
1190     strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1191     strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1192
1193     argv[0] = script;
1194     argv[1] = ifname;
1195     argv[2] = devnam;
1196     argv[3] = strspeed;
1197     argv[4] = strlocal;
1198     argv[5] = strremote;
1199     argv[6] = ipparam;
1200     argv[7] = NULL;
1201     run_program(script, argv, 0);
1202 }
1203
1204 /*
1205  * ipcp_printpkt - print the contents of an IPCP packet.
1206  */
1207 char *ipcp_codenames[] = {
1208     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1209     "TermReq", "TermAck", "CodeRej"
1210 };
1211
1212 int
1213 ipcp_printpkt(p, plen, printer, arg)
1214     u_char *p;
1215     int plen;
1216     void (*printer)();
1217     void *arg;
1218 {
1219     int code, id, len, olen;
1220     u_char *pstart, *optend;
1221     u_short cishort;
1222     u_int32_t cilong;
1223
1224     if (plen < HEADERLEN)
1225         return 0;
1226     pstart = p;
1227     GETCHAR(code, p);
1228     GETCHAR(id, p);
1229     GETSHORT(len, p);
1230     if (len < HEADERLEN || len > plen)
1231         return 0;
1232
1233     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1234         printer(arg, " %s", ipcp_codenames[code-1]);
1235     else
1236         printer(arg, " code=0x%x", code);
1237     printer(arg, " id=0x%x", id);
1238     len -= HEADERLEN;
1239     switch (code) {
1240     case CONFREQ:
1241     case CONFACK:
1242     case CONFNAK:
1243     case CONFREJ:
1244         /* print option list */
1245         while (len >= 2) {
1246             GETCHAR(code, p);
1247             GETCHAR(olen, p);
1248             p -= 2;
1249             if (olen < 2 || olen > len) {
1250                 break;
1251             }
1252             printer(arg, " <");
1253             len -= olen;
1254             optend = p + olen;
1255             switch (code) {
1256             case CI_ADDRS:
1257                 if (olen == CILEN_ADDRS) {
1258                     p += 2;
1259                     GETLONG(cilong, p);
1260                     printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1261                     GETLONG(cilong, p);
1262                     printer(arg, " %s", ip_ntoa(htonl(cilong)));
1263                 }
1264                 break;
1265             case CI_COMPRESSTYPE:
1266                 if (olen >= CILEN_COMPRESS) {
1267                     p += 2;
1268                     GETSHORT(cishort, p);
1269                     printer(arg, "compress ");
1270                     switch (cishort) {
1271                     case IPCP_VJ_COMP:
1272                         printer(arg, "VJ");
1273                         break;
1274                     case IPCP_VJ_COMP_OLD:
1275                         printer(arg, "old-VJ");
1276                         break;
1277                     default:
1278                         printer(arg, "0x%x", cishort);
1279                     }
1280                 }
1281                 break;
1282             case CI_ADDR:
1283                 if (olen == CILEN_ADDR) {
1284                     p += 2;
1285                     GETLONG(cilong, p);
1286                     printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1287                 }
1288                 break;
1289             }
1290             while (p < optend) {
1291                 GETCHAR(code, p);
1292                 printer(arg, " %.2x", code);
1293             }
1294             printer(arg, ">");
1295         }
1296         break;
1297     }
1298
1299     /* print the rest of the bytes in the packet */
1300     for (; len > 0; --len) {
1301         GETCHAR(code, p);
1302         printer(arg, " %.2x", code);
1303     }
1304
1305     return p - pstart;
1306 }