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