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