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