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