]> git.ozlabs.org Git - ppp.git/blob - pppd/ipcp.c
removed some unnecessary includes
[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.6 1994/05/24 05:57:56 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 <net/if.h>
35 #include <net/if_ppp.h>
36
37 #include "pppd.h"
38 #include "ppp.h"
39 #include "fsm.h"
40 #include "ipcp.h"
41 #include "pathnames.h"
42
43 /* global vars */
44 ipcp_options ipcp_wantoptions[NPPP];    /* Options that we want to request */
45 ipcp_options ipcp_gotoptions[NPPP];     /* Options that peer ack'd */
46 ipcp_options ipcp_allowoptions[NPPP];   /* Options we allow peer to request */
47 ipcp_options ipcp_hisoptions[NPPP];     /* Options that we ack'd */
48
49 extern char ifname[];
50 extern char devname[];
51 extern int baud_rate;
52
53 /* local vars */
54 static int cis_received[NPPP];          /* # Conf-Reqs received */
55
56 /*
57  * Callbacks for fsm code.  (CI = Configuration Information)
58  */
59 static void ipcp_resetci __ARGS((fsm *));       /* Reset our CI */
60 static int  ipcp_cilen __ARGS((fsm *));         /* Return length of our CI */
61 static void ipcp_addci __ARGS((fsm *, u_char *, int *)); /* Add our CI */
62 static int  ipcp_ackci __ARGS((fsm *, u_char *, int));  /* Peer ack'd our CI */
63 static int  ipcp_nakci __ARGS((fsm *, u_char *, int));  /* Peer nak'd our CI */
64 static int  ipcp_rejci __ARGS((fsm *, u_char *, int));  /* Peer rej'd our CI */
65 static int  ipcp_reqci __ARGS((fsm *, u_char *, int *, int)); /* Rcv CI */
66 static void ipcp_up __ARGS((fsm *));            /* We're UP */
67 static void ipcp_down __ARGS((fsm *));          /* We're DOWN */
68 static void ipcp_script __ARGS((fsm *, char *)); /* Run an up/down script */
69
70 fsm ipcp_fsm[NPPP];             /* IPCP fsm structure */
71
72 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
73     ipcp_resetci,               /* Reset our Configuration Information */
74     ipcp_cilen,                 /* Length of our Configuration Information */
75     ipcp_addci,                 /* Add our Configuration Information */
76     ipcp_ackci,                 /* ACK our Configuration Information */
77     ipcp_nakci,                 /* NAK our Configuration Information */
78     ipcp_rejci,                 /* Reject our Configuration Information */
79     ipcp_reqci,                 /* Request peer's Configuration Information */
80     ipcp_up,                    /* Called when fsm reaches OPENED state */
81     ipcp_down,                  /* Called when fsm leaves OPENED state */
82     NULL,                       /* Called when we want the lower layer up */
83     NULL,                       /* Called when we want the lower layer down */
84     NULL,                       /* Called when Protocol-Reject received */
85     NULL,                       /* Retransmission is necessary */
86     NULL,                       /* Called to handle protocol-specific codes */
87     "IPCP"                      /* String name of protocol */
88 };
89
90 /*
91  * Lengths of configuration options.
92  */
93 #define CILEN_VOID      2
94 #define CILEN_COMPRESS  4       /* min length for compression protocol opt. */
95 #define CILEN_VJ        6       /* length for RFC1332 Van-Jacobson opt. */
96 #define CILEN_ADDR      6       /* new-style single address option */
97 #define CILEN_ADDRS     10      /* old-style dual address option */
98
99
100 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
101                          (x) == CONFNAK ? "NAK" : "REJ")
102
103
104 /*
105  * Make a string representation of a network IP address.
106  */
107 char *
108 ip_ntoa(ipaddr)
109 u_long ipaddr;
110 {
111     static char b[64];
112
113     ipaddr = ntohl(ipaddr);
114
115     sprintf(b, "%d.%d.%d.%d",
116             (u_char)(ipaddr >> 24),
117             (u_char)(ipaddr >> 16),
118             (u_char)(ipaddr >> 8),
119             (u_char)(ipaddr));
120     return b;
121 }
122
123
124 /*
125  * ipcp_init - Initialize IPCP.
126  */
127 void
128 ipcp_init(unit)
129     int unit;
130 {
131     fsm *f = &ipcp_fsm[unit];
132     ipcp_options *wo = &ipcp_wantoptions[unit];
133     ipcp_options *ao = &ipcp_allowoptions[unit];
134
135     f->unit = unit;
136     f->protocol = IPCP;
137     f->callbacks = &ipcp_callbacks;
138     fsm_init(&ipcp_fsm[unit]);
139
140     wo->neg_addr = 1;
141     wo->old_addrs = 0;
142     wo->ouraddr = 0;
143     wo->hisaddr = 0;
144
145     wo->neg_vj = 1;
146     wo->old_vj = 0;
147     wo->vj_protocol = IPCP_VJ_COMP;
148     wo->maxslotindex = MAX_STATES - 1; /* really max index */
149     wo->cflag = 1;
150
151     /* max slots and slot-id compression are currently hardwired in */
152     /* ppp_if.c to 16 and 1, this needs to be changed (among other */
153     /* things) gmc */
154
155     ao->neg_addr = 1;
156     ao->neg_vj = 1;
157     ao->maxslotindex = MAX_STATES - 1;
158     ao->cflag = 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_long 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_long 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_long 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_long 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         if (cilen == CILEN_VJ) { \
497             GETCHAR(cimaxslotindex, p); \
498             GETCHAR(cicflag, p); \
499         } \
500         no.neg = 1; \
501         code \
502     }
503
504     /*
505      * Accept the peer's idea of {our,his} address, if different
506      * from our idea, only if the accept_{local,remote} flag is set.
507      */
508     NAKCIADDR(CI_ADDR, neg_addr, go->old_addrs,
509               if (go->accept_local && ciaddr1) { /* Do we know our address? */
510                   try.ouraddr = ciaddr1;
511                   IPCPDEBUG((LOG_INFO, "local IP address %s",
512                              ip_ntoa(ciaddr1)));
513               }
514               if (go->accept_remote && ciaddr2) { /* Does he know his? */
515                   try.hisaddr = ciaddr2;
516                   IPCPDEBUG((LOG_INFO, "remote IP address %s",
517                              ip_ntoa(ciaddr2)));
518               }
519               );
520
521     /*
522      * Accept the peer's value of maxslotindex provided that it
523      * is less than what we asked for.  Turn off slot-ID compression
524      * if the peer wants.  Send old-style compress-type option if
525      * the peer wants.
526      */
527     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
528             if (cilen == CILEN_VJ) {
529                 if (cishort == IPCP_VJ_COMP) {
530                     try.old_vj = 0;
531                     if (cimaxslotindex < go->maxslotindex)
532                         try.maxslotindex = cimaxslotindex;
533                     if (!cicflag)
534                         try.cflag = 0;
535                 } else {
536                     try.neg_vj = 0;
537                 }
538             } else {
539                 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
540                     try.old_vj = 1;
541                     try.vj_protocol = cishort;
542                 } else {
543                     try.neg_vj = 0;
544                 }
545             }
546             );
547
548     /*
549      * There may be remaining CIs, if the peer is requesting negotiation
550      * on an option that we didn't include in our request packet.
551      * If they want to negotiate about IP addresses, we comply.
552      * If they want us to ask for compression, we refuse.
553      */
554     while (len > CILEN_VOID) {
555         GETCHAR(citype, p);
556         GETCHAR(cilen, p);
557         if( (len -= cilen) < 0 )
558             goto bad;
559         next = p + cilen - 2;
560
561         switch (citype) {
562         case CI_COMPRESSTYPE:
563             if (go->neg_vj || no.neg_vj ||
564                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
565                 goto bad;
566             no.neg_vj = 1;
567             break;
568         case CI_ADDRS:
569             if (go->neg_addr && go->old_addrs || no.old_addrs
570                 || cilen != CILEN_ADDRS)
571                 goto bad;
572             try.neg_addr = 1;
573             try.old_addrs = 1;
574             GETLONG(l, p);
575             ciaddr1 = htonl(l);
576             if (ciaddr1 && go->accept_local)
577                 try.ouraddr = ciaddr1;
578             GETLONG(l, p);
579             ciaddr2 = htonl(l);
580             if (ciaddr2 && go->accept_remote)
581                 try.hisaddr = ciaddr2;
582             no.old_addrs = 1;
583             break;
584         case CI_ADDR:
585             if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
586                 goto bad;
587             try.neg_addr = 1;
588             try.old_addrs = 0;
589             GETLONG(l, p);
590             ciaddr1 = htonl(l);
591             if (ciaddr1 && go->accept_local)
592                 try.ouraddr = ciaddr1;
593             no.neg_addr = 1;
594             break;
595         default:
596             goto bad;
597         }
598         p = next;
599     }
600
601     /* If there is still anything left, this packet is bad. */
602     if (len != 0)
603         goto bad;
604
605     /*
606      * OK, the Nak is good.  Now we can update state.
607      */
608     if (f->state != OPENED)
609         *go = try;
610
611     return 1;
612
613 bad:
614     IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
615     return 0;
616 }
617
618
619 /*
620  * ipcp_rejci - Reject some of our CIs.
621  */
622 static int
623 ipcp_rejci(f, p, len)
624     fsm *f;
625     u_char *p;
626     int len;
627 {
628     ipcp_options *go = &ipcp_gotoptions[f->unit];
629     u_char cimaxslotindex, ciflag, cilen;
630     u_short cishort;
631     u_long cilong;
632     ipcp_options try;           /* options to request next time */
633
634     try = *go;
635     /*
636      * Any Rejected CIs must be in exactly the same order that we sent.
637      * Check packet length and CI length at each step.
638      * If we find any deviations, then this packet is bad.
639      */
640 #define REJCIADDR(opt, neg, old, val1, val2) \
641     if (go->neg && \
642         len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
643         p[1] == cilen && \
644         p[0] == opt) { \
645         u_long l; \
646         len -= cilen; \
647         INCPTR(2, p); \
648         GETLONG(l, p); \
649         cilong = htonl(l); \
650         /* Check rejected value. */ \
651         if (cilong != val1) \
652             goto bad; \
653         if (old) { \
654             GETLONG(l, p); \
655             cilong = htonl(l); \
656             /* Check rejected value. */ \
657             if (cilong != val2) \
658                 goto bad; \
659         } \
660         try.neg = 0; \
661     }
662
663 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
664     if (go->neg && \
665         p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
666         len >= p[1] && \
667         p[0] == opt) { \
668         len -= p[1]; \
669         INCPTR(2, p); \
670         GETSHORT(cishort, p); \
671         /* Check rejected value. */  \
672         if (cishort != val) \
673             goto bad; \
674         if (!old) { \
675            GETCHAR(cimaxslotindex, p); \
676            if (cimaxslotindex != maxslot) \
677              goto bad; \
678            GETCHAR(ciflag, p); \
679            if (ciflag != cflag) \
680              goto bad; \
681         } \
682         try.neg = 0; \
683      }
684
685     REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
686               go->old_addrs, go->ouraddr, go->hisaddr);
687
688     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
689             go->maxslotindex, go->cflag);
690
691     /*
692      * If there are any remaining CIs, then this packet is bad.
693      */
694     if (len != 0)
695         goto bad;
696     /*
697      * Now we can update state.
698      */
699     if (f->state != OPENED)
700         *go = try;
701     return 1;
702
703 bad:
704     IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
705     return 0;
706 }
707
708
709 /*
710  * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
711  *
712  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
713  * appropriately.  If reject_if_disagree is non-zero, doesn't return
714  * CONFNAK; returns CONFREJ if it can't return CONFACK.
715  */
716 static int
717 ipcp_reqci(f, inp, len, reject_if_disagree)
718     fsm *f;
719     u_char *inp;                /* Requested CIs */
720     int *len;                   /* Length of requested CIs */
721     int reject_if_disagree;
722 {
723     ipcp_options *wo = &ipcp_wantoptions[f->unit];
724     ipcp_options *ho = &ipcp_hisoptions[f->unit];
725     ipcp_options *ao = &ipcp_allowoptions[f->unit];
726     ipcp_options *go = &ipcp_gotoptions[f->unit];
727     u_char *cip, *next;         /* Pointer to current and next CIs */
728     u_short cilen, citype;      /* Parsed len, type */
729     u_short cishort;            /* Parsed short value */
730     u_long tl, ciaddr1, ciaddr2;/* Parsed address values */
731     int rc = CONFACK;           /* Final packet return code */
732     int orc;                    /* Individual option return code */
733     u_char *p;                  /* Pointer to next char to parse */
734     u_char *ucp = inp;          /* Pointer to current output char */
735     int l = *len;               /* Length left */
736     u_char maxslotindex, cflag;
737
738     /*
739      * Reset all his options.
740      */
741     BZERO(ho, sizeof(*ho));
742     
743     /*
744      * Process all his options.
745      */
746     next = inp;
747     while (l) {
748         orc = CONFACK;                  /* Assume success */
749         cip = p = next;                 /* Remember begining of CI */
750         if (l < 2 ||                    /* Not enough data for CI header or */
751             p[1] < 2 ||                 /*  CI length too small or */
752             p[1] > l) {                 /*  CI length too big? */
753             IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
754             orc = CONFREJ;              /* Reject bad CI */
755             cilen = l;                  /* Reject till end of packet */
756             l = 0;                      /* Don't loop again */
757             goto endswitch;
758         }
759         GETCHAR(citype, p);             /* Parse CI type */
760         GETCHAR(cilen, p);              /* Parse CI length */
761         l -= cilen;                     /* Adjust remaining length */
762         next += cilen;                  /* Step to next CI */
763
764         switch (citype) {               /* Check CI type */
765         case CI_ADDRS:
766             IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
767             if (!ao->neg_addr ||
768                 cilen != CILEN_ADDRS) { /* Check CI length */
769                 orc = CONFREJ;          /* Reject CI */
770                 break;
771             }
772
773             /*
774              * If he has no address, or if we both have his address but
775              * disagree about it, then NAK it with our idea.
776              * In particular, if we don't know his address, but he does,
777              * then accept it.
778              */
779             GETLONG(tl, p);             /* Parse source address (his) */
780             ciaddr1 = htonl(tl);
781             IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
782             if (ciaddr1 != wo->hisaddr
783                 && (ciaddr1 == 0 || !wo->accept_remote)) {
784                 orc = CONFNAK;
785                 if (!reject_if_disagree) {
786                     DECPTR(sizeof (long), p);
787                     tl = ntohl(wo->hisaddr);
788                     PUTLONG(tl, p);
789                 }
790             }
791
792             /*
793              * If he doesn't know our address, or if we both have our address
794              * but disagree about it, then NAK it with our idea.
795              */
796             GETLONG(tl, p);             /* Parse desination address (ours) */
797             ciaddr2 = htonl(tl);
798             IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
799             if (ciaddr2 != wo->ouraddr) {
800                 if (ciaddr2 == 0 || !wo->accept_local) {
801                     orc = CONFNAK;
802                     if (!reject_if_disagree) {
803                         DECPTR(sizeof (long), p);
804                         tl = ntohl(wo->ouraddr);
805                         PUTLONG(tl, p);
806                     }
807                 } else {
808                     go->ouraddr = ciaddr2;      /* accept peer's idea */
809                 }
810             }
811
812             ho->neg_addr = 1;
813             ho->old_addrs = 1;
814             ho->hisaddr = ciaddr1;
815             ho->ouraddr = ciaddr2;
816             break;
817
818         case CI_ADDR:
819             IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
820
821             if (!ao->neg_addr ||
822                 cilen != CILEN_ADDR) {  /* Check CI length */
823                 orc = CONFREJ;          /* Reject CI */
824                 break;
825             }
826
827             /*
828              * If he has no address, or if we both have his address but
829              * disagree about it, then NAK it with our idea.
830              * In particular, if we don't know his address, but he does,
831              * then accept it.
832              */
833             GETLONG(tl, p);     /* Parse source address (his) */
834             ciaddr1 = htonl(tl);
835             IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
836             if (ciaddr1 != wo->hisaddr
837                 && (ciaddr1 == 0 || !wo->accept_remote)) {
838                 orc = CONFNAK;
839                 if (!reject_if_disagree) {
840                     DECPTR(sizeof (long), p);
841                     tl = ntohl(wo->hisaddr);
842                     PUTLONG(tl, p);
843                 }
844             }
845         
846             ho->neg_addr = 1;
847             ho->hisaddr = ciaddr1;
848             break;
849         
850         case CI_COMPRESSTYPE:
851             IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
852             if (!ao->neg_vj ||
853                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
854                 orc = CONFREJ;
855                 break;
856             }
857             GETSHORT(cishort, p);
858             IPCPDEBUG((LOG_INFO, "(%d)", cishort));
859
860             if (!(cishort == IPCP_VJ_COMP ||
861                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
862                 orc = CONFREJ;
863                 break;
864             }
865
866             ho->neg_vj = 1;
867             ho->vj_protocol = cishort;
868             if (cilen == CILEN_VJ) {
869                 GETCHAR(maxslotindex, p);
870                 if (maxslotindex > ao->maxslotindex) { 
871                     orc = CONFNAK;
872                     if (!reject_if_disagree){
873                         DECPTR(1, p);
874                         PUTCHAR(ao->maxslotindex, p);
875                     }
876                 }
877                 GETCHAR(cflag, p);
878                 if (cflag && !ao->cflag) {
879                     orc = CONFNAK;
880                     if (!reject_if_disagree){
881                         DECPTR(1, p);
882                         PUTCHAR(wo->cflag, p);
883                     }
884                 }
885                 ho->maxslotindex = maxslotindex;
886                 ho->cflag = wo->cflag;
887             } else {
888                 ho->old_vj = 1;
889                 ho->maxslotindex = MAX_STATES - 1;
890                 ho->cflag = 1;
891             }
892             break;
893
894         default:
895             orc = CONFREJ;
896             break;
897         }
898
899 endswitch:
900         IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
901
902         if (orc == CONFACK &&           /* Good CI */
903             rc != CONFACK)              /*  but prior CI wasnt? */
904             continue;                   /* Don't send this one */
905
906         if (orc == CONFNAK) {           /* Nak this CI? */
907             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
908                 orc = CONFREJ;          /* Get tough if so */
909             else {
910                 if (rc == CONFREJ)      /* Rejecting prior CI? */
911                     continue;           /* Don't send this one */
912                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
913                     rc = CONFNAK;       /* Not anymore... */
914                     ucp = inp;          /* Backup */
915                 }
916             }
917         }
918
919         if (orc == CONFREJ &&           /* Reject this CI */
920             rc != CONFREJ) {            /*  but no prior ones? */
921             rc = CONFREJ;
922             ucp = inp;                  /* Backup */
923         }
924
925         /* Need to move CI? */
926         if (ucp != cip)
927             BCOPY(cip, ucp, cilen);     /* Move it */
928
929         /* Update output pointer */
930         INCPTR(cilen, ucp);
931     }
932
933     /*
934      * If we aren't rejecting this packet, and we want to negotiate
935      * their address, and they didn't send their address, then we
936      * send a NAK with a CI_ADDR option appended.  We assume the
937      * input buffer is long enough that we can append the extra
938      * option safely.
939      */
940     if (rc != CONFREJ && !ho->neg_addr &&
941         wo->req_addr && !reject_if_disagree) {
942         if (rc == CONFACK) {
943             rc = CONFNAK;
944             ucp = inp;                  /* reset pointer */
945             wo->req_addr = 0;           /* don't ask again */
946         }
947         PUTCHAR(CI_ADDR, ucp);
948         PUTCHAR(CILEN_ADDR, ucp);
949         tl = ntohl(wo->hisaddr);
950         PUTLONG(tl, ucp);
951     }
952
953     *len = ucp - inp;                   /* Compute output length */
954     IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
955     return (rc);                        /* Return final code */
956 }
957
958
959 /*
960  * ipcp_up - IPCP has come UP.
961  *
962  * Configure the IP network interface appropriately and bring it up.
963  */
964 static void
965 ipcp_up(f)
966     fsm *f;
967 {
968     u_long mask;
969     ipcp_options *ho = &ipcp_hisoptions[f->unit];
970     ipcp_options *go = &ipcp_gotoptions[f->unit];
971
972     IPCPDEBUG((LOG_INFO, "ipcp: up"));
973     go->default_route = 0;
974     go->proxy_arp = 0;
975
976     /*
977      * We must have a non-zero IP address for both ends of the link.
978      */
979     if (!ho->neg_addr)
980         ho->hisaddr = ipcp_wantoptions[f->unit].hisaddr;
981
982     if (ho->hisaddr == 0) {
983         syslog(LOG_ERR, "Could not determine remote IP address");
984         ipcp_close(f->unit);
985         return;
986     }
987     if (go->ouraddr == 0) {
988         syslog(LOG_ERR, "Could not determine local IP address");
989         ipcp_close(f->unit);
990         return;
991     }
992
993     /*
994      * Check that the peer is allowed to use the IP address it wants.
995      */
996     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
997         syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
998                ip_ntoa(ho->hisaddr));
999         ipcp_close(f->unit);
1000         return;
1001     }
1002
1003     syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1004     syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1005
1006     /*
1007      * Set IP addresses and (if specified) netmask.
1008      */
1009     mask = GetMask(go->ouraddr);
1010     if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1011         IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1012         ipcp_close(f->unit);
1013         return;
1014     }
1015
1016     /* set tcp compression */
1017     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1018
1019     /* bring the interface up for IP */
1020     if (!sifup(f->unit)) {
1021         IPCPDEBUG((LOG_WARNING, "sifup failed"));
1022         ipcp_close(f->unit);
1023         return;
1024     }
1025
1026     /* assign a default route through the interface if required */
1027     if (ipcp_wantoptions[f->unit].default_route) 
1028         if (sifdefaultroute(f->unit, ho->hisaddr))
1029             go->default_route = 1;
1030
1031     /* Make a proxy ARP entry if requested. */
1032     if (ipcp_wantoptions[f->unit].proxy_arp)
1033         if (sifproxyarp(f->unit, ho->hisaddr))
1034             go->proxy_arp = 1;
1035
1036     /*
1037      * Execute the ip-up script, like this:
1038      *  /etc/ppp/ip-up interface tty speed local-IP remote-IP
1039      */
1040     ipcp_script(f, _PATH_IPUP);
1041
1042 }
1043
1044
1045 /*
1046  * ipcp_down - IPCP has gone DOWN.
1047  *
1048  * Take the IP network interface down, clear its addresses
1049  * and delete routes through it.
1050  */
1051 static void
1052 ipcp_down(f)
1053     fsm *f;
1054 {
1055     u_long ouraddr, hisaddr;
1056
1057     IPCPDEBUG((LOG_INFO, "ipcp: down"));
1058
1059     ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1060     hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1061     if (ipcp_gotoptions[f->unit].proxy_arp)
1062         cifproxyarp(f->unit, hisaddr);
1063     if (ipcp_gotoptions[f->unit].default_route) 
1064         cifdefaultroute(f->unit, hisaddr);
1065     sifdown(f->unit);
1066     cifaddr(f->unit, ouraddr, hisaddr);
1067
1068     /* Execute the ip-down script */
1069     ipcp_script(f, _PATH_IPDOWN);
1070 }
1071
1072
1073 /*
1074  * ipcp_script - Execute a script with arguments
1075  * interface-name tty-name speed local-IP remote-IP.
1076  */
1077 static void
1078 ipcp_script(f, script)
1079     fsm *f;
1080     char *script;
1081 {
1082     char strspeed[32], strlocal[32], strremote[32];
1083     char *argv[8];
1084
1085     sprintf(strspeed, "%d", baud_rate);
1086     strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1087     strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1088
1089     argv[0] = script;
1090     argv[1] = ifname;
1091     argv[2] = devname;
1092     argv[3] = strspeed;
1093     argv[4] = strlocal;
1094     argv[5] = strremote;
1095     argv[6] = NULL;
1096     run_program(script, argv, 0);
1097 }
1098
1099 /*
1100  * ipcp_printpkt - print the contents of an IPCP packet.
1101  */
1102 char *ipcp_codenames[] = {
1103     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1104     "TermReq", "TermAck", "CodeRej"
1105 };
1106
1107 int
1108 ipcp_printpkt(p, plen, printer, arg)
1109     u_char *p;
1110     int plen;
1111     void (*printer)();
1112     void *arg;
1113 {
1114     int code, id, len, olen;
1115     u_char *pstart, *optend;
1116     u_short cishort;
1117     u_long cilong;
1118
1119     if (plen < HEADERLEN)
1120         return 0;
1121     pstart = p;
1122     GETCHAR(code, p);
1123     GETCHAR(id, p);
1124     GETSHORT(len, p);
1125     if (len < HEADERLEN || len > plen)
1126         return 0;
1127
1128     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1129         printer(arg, " %s", ipcp_codenames[code-1]);
1130     else
1131         printer(arg, " code=0x%x", code);
1132     printer(arg, " id=0x%x", id);
1133     len -= HEADERLEN;
1134     switch (code) {
1135     case CONFREQ:
1136     case CONFACK:
1137     case CONFNAK:
1138     case CONFREJ:
1139         /* print option list */
1140         while (len >= 2) {
1141             GETCHAR(code, p);
1142             GETCHAR(olen, p);
1143             p -= 2;
1144             if (olen < 2 || olen > len) {
1145                 break;
1146             }
1147             printer(arg, " <");
1148             len -= olen;
1149             optend = p + olen;
1150             switch (code) {
1151             case CI_ADDRS:
1152                 if (olen == CILEN_ADDRS) {
1153                     p += 2;
1154                     GETLONG(cilong, p);
1155                     printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1156                     GETLONG(cilong, p);
1157                     printer(arg, " %s", ip_ntoa(htonl(cilong)));
1158                 }
1159                 break;
1160             case CI_COMPRESSTYPE:
1161                 if (olen >= CILEN_COMPRESS) {
1162                     p += 2;
1163                     GETSHORT(cishort, p);
1164                     printer(arg, "compress ");
1165                     switch (cishort) {
1166                     case IPCP_VJ_COMP:
1167                         printer(arg, "VJ");
1168                         break;
1169                     case IPCP_VJ_COMP_OLD:
1170                         printer(arg, "old-VJ");
1171                         break;
1172                     default:
1173                         printer(arg, "0x%x", cishort);
1174                     }
1175                 }
1176                 break;
1177             case CI_ADDR:
1178                 if (olen == CILEN_ADDR) {
1179                     p += 2;
1180                     GETLONG(cilong, p);
1181                     printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1182                 }
1183                 break;
1184             }
1185             while (p < optend) {
1186                 GETCHAR(code, p);
1187                 printer(arg, " %.2x", code);
1188             }
1189             printer(arg, ">");
1190         }
1191         break;
1192     }
1193
1194     /* print the rest of the bytes in the packet */
1195     for (; len > 0; --len) {
1196         GETCHAR(code, p);
1197         printer(arg, " %.2x", code);
1198     }
1199
1200     return p - pstart;
1201 }