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