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