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