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