]> git.ozlabs.org Git - ppp.git/blob - pppd/ipcp.c
syslog stuff back to main.c; removed set_filters
[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.24 1996/04/04 03:57:12 paulus Exp $";
22 #endif
23
24 /*
25  * TODO:
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <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     ipcp_options *wo = &ipcp_wantoptions[f->unit];
270     ipcp_options *ho = &ipcp_hisoptions[f->unit];
271
272 #define LENCIVJ(neg, old)       (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
273 #define LENCIADDR(neg, old)     (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
274
275     /*
276      * First see if we want to change our options to the old
277      * forms because we have received old forms from the peer.
278      */
279     if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
280         /* use the old style of address negotiation */
281         go->neg_addr = 1;
282         go->old_addrs = 1;
283     }
284     if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
285         /* try an older style of VJ negotiation */
286         if (cis_received[f->unit] == 0) {
287             /* keep trying the new style until we see some CI from the peer */
288             go->neg_vj = 1;
289         } else {
290             /* use the old style only if the peer did */
291             if (ho->neg_vj && ho->old_vj) {
292                 go->neg_vj = 1;
293                 go->old_vj = 1;
294                 go->vj_protocol = ho->vj_protocol;
295             }
296         }
297     }
298
299     return (LENCIADDR(go->neg_addr, go->old_addrs) +
300             LENCIVJ(go->neg_vj, go->old_vj));
301 }
302
303
304 /*
305  * ipcp_addci - Add our desired CIs to a packet.
306  */
307 static void
308 ipcp_addci(f, ucp, lenp)
309     fsm *f;
310     u_char *ucp;
311     int *lenp;
312 {
313     ipcp_options *go = &ipcp_gotoptions[f->unit];
314     int len = *lenp;
315
316 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
317     if (neg) { \
318         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
319         if (len >= vjlen) { \
320             PUTCHAR(opt, ucp); \
321             PUTCHAR(vjlen, ucp); \
322             PUTSHORT(val, ucp); \
323             if (!old) { \
324                 PUTCHAR(maxslotindex, ucp); \
325                 PUTCHAR(cflag, ucp); \
326             } \
327             len -= vjlen; \
328         } else \
329             neg = 0; \
330     }
331
332 #define ADDCIADDR(opt, neg, old, val1, val2) \
333     if (neg) { \
334         int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
335         if (len >= addrlen) { \
336             u_int32_t l; \
337             PUTCHAR(opt, ucp); \
338             PUTCHAR(addrlen, ucp); \
339             l = ntohl(val1); \
340             PUTLONG(l, ucp); \
341             if (old) { \
342                 l = ntohl(val2); \
343                 PUTLONG(l, ucp); \
344             } \
345             len -= addrlen; \
346         } else \
347             neg = 0; \
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     int d;
745
746     cis_received[f->unit] = 1;
747
748     /*
749      * Reset all his options.
750      */
751     BZERO(ho, sizeof(*ho));
752     
753     /*
754      * Process all his options.
755      */
756     next = inp;
757     while (l) {
758         orc = CONFACK;                  /* Assume success */
759         cip = p = next;                 /* Remember begining of CI */
760         if (l < 2 ||                    /* Not enough data for CI header or */
761             p[1] < 2 ||                 /*  CI length too small or */
762             p[1] > l) {                 /*  CI length too big? */
763             IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
764             orc = CONFREJ;              /* Reject bad CI */
765             cilen = l;                  /* Reject till end of packet */
766             l = 0;                      /* Don't loop again */
767             goto endswitch;
768         }
769         GETCHAR(citype, p);             /* Parse CI type */
770         GETCHAR(cilen, p);              /* Parse CI length */
771         l -= cilen;                     /* Adjust remaining length */
772         next += cilen;                  /* Step to next CI */
773
774         switch (citype) {               /* Check CI type */
775         case CI_ADDRS:
776             IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
777             if (!ao->neg_addr ||
778                 cilen != CILEN_ADDRS) { /* Check CI length */
779                 orc = CONFREJ;          /* Reject CI */
780                 break;
781             }
782
783             /*
784              * If he has no address, or if we both have his address but
785              * disagree about it, then NAK it with our idea.
786              * In particular, if we don't know his address, but he does,
787              * then accept it.
788              */
789             GETLONG(tl, p);             /* Parse source address (his) */
790             ciaddr1 = htonl(tl);
791             IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
792             if (ciaddr1 != wo->hisaddr
793                 && (ciaddr1 == 0 || !wo->accept_remote)) {
794                 orc = CONFNAK;
795                 if (!reject_if_disagree) {
796                     DECPTR(sizeof(u_int32_t), p);
797                     tl = ntohl(wo->hisaddr);
798                     PUTLONG(tl, p);
799                 }
800             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
801                 /*
802                  * If neither we nor he knows his address, reject the option.
803                  */
804                 orc = CONFREJ;
805                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
806                 break;
807             }
808
809             /*
810              * If he doesn't know our address, or if we both have our address
811              * but disagree about it, then NAK it with our idea.
812              */
813             GETLONG(tl, p);             /* Parse desination address (ours) */
814             ciaddr2 = htonl(tl);
815             IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
816             if (ciaddr2 != wo->ouraddr) {
817                 if (ciaddr2 == 0 || !wo->accept_local) {
818                     orc = CONFNAK;
819                     if (!reject_if_disagree) {
820                         DECPTR(sizeof(u_int32_t), p);
821                         tl = ntohl(wo->ouraddr);
822                         PUTLONG(tl, p);
823                     }
824                 } else {
825                     go->ouraddr = ciaddr2;      /* accept peer's idea */
826                 }
827             }
828
829             ho->neg_addr = 1;
830             ho->old_addrs = 1;
831             ho->hisaddr = ciaddr1;
832             ho->ouraddr = ciaddr2;
833             break;
834
835         case CI_ADDR:
836             IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
837
838             if (!ao->neg_addr ||
839                 cilen != CILEN_ADDR) {  /* Check CI length */
840                 orc = CONFREJ;          /* Reject CI */
841                 break;
842             }
843
844             /*
845              * If he has no address, or if we both have his address but
846              * disagree about it, then NAK it with our idea.
847              * In particular, if we don't know his address, but he does,
848              * then accept it.
849              */
850             GETLONG(tl, p);     /* Parse source address (his) */
851             ciaddr1 = htonl(tl);
852             IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
853             if (ciaddr1 != wo->hisaddr
854                 && (ciaddr1 == 0 || !wo->accept_remote)) {
855                 orc = CONFNAK;
856                 if (!reject_if_disagree) {
857                     DECPTR(sizeof(u_int32_t), p);
858                     tl = ntohl(wo->hisaddr);
859                     PUTLONG(tl, p);
860                 }
861             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
862                 /*
863                  * Don't ACK an address of 0.0.0.0 - reject it instead.
864                  */
865                 orc = CONFREJ;
866                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
867                 break;
868             }
869         
870             ho->neg_addr = 1;
871             ho->hisaddr = ciaddr1;
872             break;
873
874         case CI_MS_DNS1:
875         case CI_MS_DNS2:
876             /* Microsoft primary or secondary DNS request */
877             d = citype == CI_MS_DNS2;
878             IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
879
880             /* If we do not have a DNS address then we cannot send it */
881             if (ao->dnsaddr[d] == 0 ||
882                 cilen != CILEN_ADDR) {  /* Check CI length */
883                 orc = CONFREJ;          /* Reject CI */
884                 break;
885             }
886             GETLONG(tl, p);
887             if (htonl(tl) != ao->dnsaddr[d]) {
888                 DECPTR(sizeof(u_int32_t), p);
889                 tl = ntohl(ao->dnsaddr[d]);
890                 PUTLONG(tl, p);
891                 orc = CONFNAK;
892             }
893             break;
894         
895         case CI_COMPRESSTYPE:
896             IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
897             if (!ao->neg_vj ||
898                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
899                 orc = CONFREJ;
900                 break;
901             }
902             GETSHORT(cishort, p);
903             IPCPDEBUG((LOG_INFO, "(%d)", cishort));
904
905             if (!(cishort == IPCP_VJ_COMP ||
906                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
907                 orc = CONFREJ;
908                 break;
909             }
910
911             ho->neg_vj = 1;
912             ho->vj_protocol = cishort;
913             if (cilen == CILEN_VJ) {
914                 GETCHAR(maxslotindex, p);
915                 if (maxslotindex > ao->maxslotindex) { 
916                     orc = CONFNAK;
917                     if (!reject_if_disagree){
918                         DECPTR(1, p);
919                         PUTCHAR(ao->maxslotindex, p);
920                     }
921                 }
922                 GETCHAR(cflag, p);
923                 if (cflag && !ao->cflag) {
924                     orc = CONFNAK;
925                     if (!reject_if_disagree){
926                         DECPTR(1, p);
927                         PUTCHAR(wo->cflag, p);
928                     }
929                 }
930                 ho->maxslotindex = maxslotindex;
931                 ho->cflag = cflag;
932             } else {
933                 ho->old_vj = 1;
934                 ho->maxslotindex = MAX_STATES - 1;
935                 ho->cflag = 1;
936             }
937             break;
938
939         default:
940             orc = CONFREJ;
941             break;
942         }
943
944 endswitch:
945         IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
946
947         if (orc == CONFACK &&           /* Good CI */
948             rc != CONFACK)              /*  but prior CI wasnt? */
949             continue;                   /* Don't send this one */
950
951         if (orc == CONFNAK) {           /* Nak this CI? */
952             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
953                 orc = CONFREJ;          /* Get tough if so */
954             else {
955                 if (rc == CONFREJ)      /* Rejecting prior CI? */
956                     continue;           /* Don't send this one */
957                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
958                     rc = CONFNAK;       /* Not anymore... */
959                     ucp = inp;          /* Backup */
960                 }
961             }
962         }
963
964         if (orc == CONFREJ &&           /* Reject this CI */
965             rc != CONFREJ) {            /*  but no prior ones? */
966             rc = CONFREJ;
967             ucp = inp;                  /* Backup */
968         }
969
970         /* Need to move CI? */
971         if (ucp != cip)
972             BCOPY(cip, ucp, cilen);     /* Move it */
973
974         /* Update output pointer */
975         INCPTR(cilen, ucp);
976     }
977
978     /*
979      * If we aren't rejecting this packet, and we want to negotiate
980      * their address, and they didn't send their address, then we
981      * send a NAK with a CI_ADDR option appended.  We assume the
982      * input buffer is long enough that we can append the extra
983      * option safely.
984      */
985     if (rc != CONFREJ && !ho->neg_addr &&
986         wo->req_addr && !reject_if_disagree) {
987         if (rc == CONFACK) {
988             rc = CONFNAK;
989             ucp = inp;                  /* reset pointer */
990             wo->req_addr = 0;           /* don't ask again */
991         }
992         PUTCHAR(CI_ADDR, ucp);
993         PUTCHAR(CILEN_ADDR, ucp);
994         tl = ntohl(wo->hisaddr);
995         PUTLONG(tl, ucp);
996     }
997
998     *len = ucp - inp;                   /* Compute output length */
999     IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1000     return (rc);                        /* Return final code */
1001 }
1002
1003
1004 /*
1005  * ip_check_options - check that any IP-related options are OK,
1006  * and assign appropriate defaults.
1007  */
1008 void
1009 ip_check_options()
1010 {
1011     struct hostent *hp;
1012     u_int32_t local;
1013     ipcp_options *wo = &ipcp_wantoptions[0];
1014
1015     /*
1016      * Default our local IP address based on our hostname.
1017      * If local IP address already given, don't bother.
1018      */
1019     if (wo->ouraddr == 0 && !disable_defaultip) {
1020         /*
1021          * Look up our hostname (possibly with domain name appended)
1022          * and take the first IP address as our local IP address.
1023          * If there isn't an IP address for our hostname, too bad.
1024          */
1025         wo->accept_local = 1;   /* don't insist on this default value */
1026         if ((hp = gethostbyname(hostname)) != NULL) {
1027             local = *(u_int32_t *)hp->h_addr;
1028             if (local != 0 && !bad_ip_adrs(local))
1029                 wo->ouraddr = local;
1030         }
1031     }
1032
1033     if (demand && wo->hisaddr == 0) {
1034         fprintf(stderr, "%s: remote IP address required for demand-dialling\n",
1035                 progname);
1036         exit(1);
1037     }
1038     if (demand && wo->accept_remote) {
1039         fprintf(stderr, "%s: ipcp-accept-remote is incompatible with demand\n",
1040                 progname);
1041         exit(1);
1042     }
1043 }
1044
1045
1046 /*
1047  * ip_demand_conf - configure the interface as though
1048  * IPCP were up, for use with dial-on-demand.
1049  */
1050 int
1051 ip_demand_conf(u)
1052     int u;
1053 {
1054     ipcp_options *wo = &ipcp_wantoptions[u];
1055
1056     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1057         return 0;
1058     if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1059         return 0;
1060     if (!sifup(u))
1061         return 0;
1062     if (wo->default_route)
1063         if (sifdefaultroute(u, wo->hisaddr))
1064             default_route_set[u] = 1;
1065     if (wo->proxy_arp)
1066         if (sifproxyarp(u, wo->hisaddr))
1067             proxy_arp_set[u] = 1;
1068
1069     syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(wo->ouraddr));
1070     syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1071
1072     return 1;
1073 }
1074
1075
1076 /*
1077  * ipcp_up - IPCP has come UP.
1078  *
1079  * Configure the IP network interface appropriately and bring it up.
1080  */
1081 static void
1082 ipcp_up(f)
1083     fsm *f;
1084 {
1085     u_int32_t mask;
1086     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1087     ipcp_options *go = &ipcp_gotoptions[f->unit];
1088     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1089
1090     np_up(f->unit, PPP_IP);
1091     IPCPDEBUG((LOG_INFO, "ipcp: up"));
1092
1093     /*
1094      * We must have a non-zero IP address for both ends of the link.
1095      */
1096     if (!ho->neg_addr)
1097         ho->hisaddr = wo->hisaddr;
1098
1099     if (ho->hisaddr == 0) {
1100         syslog(LOG_ERR, "Could not determine remote IP address");
1101         ipcp_close(f->unit, "Could not determine remote IP address");
1102         return;
1103     }
1104     if (go->ouraddr == 0) {
1105         syslog(LOG_ERR, "Could not determine local IP address");
1106         ipcp_close(f->unit, "Could not determine local IP address");
1107         return;
1108     }
1109
1110     /*
1111      * Check that the peer is allowed to use the IP address it wants.
1112      */
1113     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1114         syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1115                ip_ntoa(ho->hisaddr));
1116         ipcp_close(f->unit, "Unauthorized remote IP address");
1117         return;
1118     }
1119
1120     /* set tcp compression */
1121     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1122
1123     /*
1124      * If we are doing dial-on-demand, the interface is already
1125      * configured, so we put out any saved-up packets, then set the
1126      * interface to pass IP packets.
1127      */
1128     if (demand) {
1129         if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1130             syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1131             ipcp_close(f->unit, "Wrong IP addresses");
1132             return;
1133         }
1134         demand_rexmit(PPP_IP);
1135         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1136
1137     } else {
1138
1139         /*
1140          * Set IP addresses and (if specified) netmask.
1141          */
1142         mask = GetMask(go->ouraddr);
1143         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1144             IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1145             ipcp_close(f->unit, "Interface configuration failed");
1146             return;
1147         }
1148
1149         /* bring the interface up for IP */
1150         if (!sifup(f->unit)) {
1151             IPCPDEBUG((LOG_WARNING, "sifup failed"));
1152             ipcp_close(f->unit, "Interface configuration failed");
1153             return;
1154         }
1155
1156         /* assign a default route through the interface if required */
1157         if (ipcp_wantoptions[f->unit].default_route) 
1158             if (sifdefaultroute(f->unit, ho->hisaddr))
1159                 default_route_set[f->unit] = 1;
1160
1161         /* Make a proxy ARP entry if requested. */
1162         if (ipcp_wantoptions[f->unit].proxy_arp)
1163             if (sifproxyarp(f->unit, ho->hisaddr))
1164                 proxy_arp_set[f->unit] = 1;
1165
1166         syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1167         syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1168     }
1169
1170     /*
1171      * Execute the ip-up script, like this:
1172      *  /etc/ppp/ip-up interface tty speed local-IP remote-IP
1173      */
1174     ipcp_script(f, _PATH_IPUP);
1175
1176 }
1177
1178
1179 /*
1180  * ipcp_down - IPCP has gone DOWN.
1181  *
1182  * Take the IP network interface down, clear its addresses
1183  * and delete routes through it.
1184  */
1185 static void
1186 ipcp_down(f)
1187     fsm *f;
1188 {
1189     u_int32_t ouraddr, hisaddr;
1190
1191     np_down(f->unit, PPP_IP);
1192     IPCPDEBUG((LOG_INFO, "ipcp: down"));
1193
1194     /*
1195      * If we are doing dial-on-demand, set the interface
1196      * to queue up outgoing packets (for now).
1197      */
1198     if (demand) {
1199         sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1200
1201     } else {
1202         ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1203         hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1204         if (proxy_arp_set[f->unit]) {
1205             cifproxyarp(f->unit, hisaddr);
1206             proxy_arp_set[f->unit] = 0;
1207         }
1208         if (default_route_set[f->unit]) {
1209             cifdefaultroute(f->unit, hisaddr);
1210             default_route_set[f->unit] = 0;
1211         }
1212         sifdown(f->unit);
1213         cifaddr(f->unit, ouraddr, hisaddr);
1214     }
1215
1216     /* Execute the ip-down script */
1217     ipcp_script(f, _PATH_IPDOWN);
1218 }
1219
1220
1221 /*
1222  * ipcp_finished - possibly shut down the lower layers.
1223  */
1224 static void
1225 ipcp_finished(f)
1226     fsm *f;
1227 {
1228     np_finished(f->unit, PPP_IP);
1229 }
1230
1231
1232 /*
1233  * ipcp_script - Execute a script with arguments
1234  * interface-name tty-name speed local-IP remote-IP.
1235  */
1236 static void
1237 ipcp_script(f, script)
1238     fsm *f;
1239     char *script;
1240 {
1241     char strspeed[32], strlocal[32], strremote[32];
1242     char *argv[8];
1243
1244     sprintf(strspeed, "%d", baud_rate);
1245     strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1246     strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1247
1248     argv[0] = script;
1249     argv[1] = ifname;
1250     argv[2] = devnam;
1251     argv[3] = strspeed;
1252     argv[4] = strlocal;
1253     argv[5] = strremote;
1254     argv[6] = ipparam;
1255     argv[7] = NULL;
1256     run_program(script, argv, 0);
1257 }
1258
1259 /*
1260  * ipcp_printpkt - print the contents of an IPCP packet.
1261  */
1262 char *ipcp_codenames[] = {
1263     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1264     "TermReq", "TermAck", "CodeRej"
1265 };
1266
1267 int
1268 ipcp_printpkt(p, plen, printer, arg)
1269     u_char *p;
1270     int plen;
1271     void (*printer)();
1272     void *arg;
1273 {
1274     int code, id, len, olen;
1275     u_char *pstart, *optend;
1276     u_short cishort;
1277     u_int32_t cilong;
1278
1279     if (plen < HEADERLEN)
1280         return 0;
1281     pstart = p;
1282     GETCHAR(code, p);
1283     GETCHAR(id, p);
1284     GETSHORT(len, p);
1285     if (len < HEADERLEN || len > plen)
1286         return 0;
1287
1288     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1289         printer(arg, " %s", ipcp_codenames[code-1]);
1290     else
1291         printer(arg, " code=0x%x", code);
1292     printer(arg, " id=0x%x", id);
1293     len -= HEADERLEN;
1294     switch (code) {
1295     case CONFREQ:
1296     case CONFACK:
1297     case CONFNAK:
1298     case CONFREJ:
1299         /* print option list */
1300         while (len >= 2) {
1301             GETCHAR(code, p);
1302             GETCHAR(olen, p);
1303             p -= 2;
1304             if (olen < 2 || olen > len) {
1305                 break;
1306             }
1307             printer(arg, " <");
1308             len -= olen;
1309             optend = p + olen;
1310             switch (code) {
1311             case CI_ADDRS:
1312                 if (olen == CILEN_ADDRS) {
1313                     p += 2;
1314                     GETLONG(cilong, p);
1315                     printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1316                     GETLONG(cilong, p);
1317                     printer(arg, " %s", ip_ntoa(htonl(cilong)));
1318                 }
1319                 break;
1320             case CI_COMPRESSTYPE:
1321                 if (olen >= CILEN_COMPRESS) {
1322                     p += 2;
1323                     GETSHORT(cishort, p);
1324                     printer(arg, "compress ");
1325                     switch (cishort) {
1326                     case IPCP_VJ_COMP:
1327                         printer(arg, "VJ");
1328                         break;
1329                     case IPCP_VJ_COMP_OLD:
1330                         printer(arg, "old-VJ");
1331                         break;
1332                     default:
1333                         printer(arg, "0x%x", cishort);
1334                     }
1335                 }
1336                 break;
1337             case CI_ADDR:
1338                 if (olen == CILEN_ADDR) {
1339                     p += 2;
1340                     GETLONG(cilong, p);
1341                     printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1342                 }
1343                 break;
1344             }
1345             while (p < optend) {
1346                 GETCHAR(code, p);
1347                 printer(arg, " %.2x", code);
1348             }
1349             printer(arg, ">");
1350         }
1351         break;
1352     }
1353
1354     /* print the rest of the bytes in the packet */
1355     for (; len > 0; --len) {
1356         GETCHAR(code, p);
1357         printer(arg, " %.2x", code);
1358     }
1359
1360     return p - pstart;
1361 }