]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/ppp/v2_0/src/ipcp.c
Merge branch 'master' of git+ssh://git.kernelconcepts.de/karo-tx-redboot
[karo-tx-redboot.git] / packages / net / ppp / v2_0 / src / ipcp.c
1 //==========================================================================
2 //
3 //      src/ipcp.c
4 //
5 //==========================================================================
6 //####ECOSGPLCOPYRIGHTBEGIN####
7 // -------------------------------------------
8 // This file is part of eCos, the Embedded Configurable Operating System.
9 // Portions created by Nick Garnett are
10 // Copyright (C) 2003 eCosCentric Ltd.
11 //
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
15 //
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 // for more details.
20 //
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 //
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
31 //
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
34 //
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //####BSDCOPYRIGHTBEGIN####
38 //
39 // -------------------------------------------
40 //
41 // Portions of this software may have been derived from OpenBSD, 
42 // FreeBSD or other sources, and are covered by the appropriate
43 // copyright disclaimers included herein.
44 //
45 // -------------------------------------------
46 //
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
49
50 /*
51  * ipcp.c - PPP IP Control Protocol.
52  *
53  * Copyright (c) 1989 Carnegie Mellon University.
54  * All rights reserved.
55  *
56  * Redistribution and use in source and binary forms are permitted
57  * provided that the above copyright notice and this paragraph are
58  * duplicated in all such forms and that any documentation,
59  * advertising materials, and other materials related to such
60  * distribution and use acknowledge that the software was developed
61  * by Carnegie Mellon University.  The name of the
62  * University may not be used to endorse or promote products derived
63  * from this software without specific prior written permission.
64  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
65  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
66  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
67  */
68
69 #ifndef lint
70 //static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/ipcp.c,v 1.12 1999/08/28 01:19:03 peter Exp $";
71 #endif
72
73 /*
74  * TODO:
75  */
76
77 #include <stdio.h>
78 #include <string.h>
79 #include <cyg/ppp/syslog.h>
80 #include <netdb.h>
81 #include <sys/param.h>
82 #include <sys/types.h>
83 #include <sys/socket.h>
84 #include <netinet/in.h>
85
86 #include <pkgconf/system.h>
87 #include <pkgconf/ppp.h>
88 #include "cyg/ppp/pppd.h"
89 #include "cyg/ppp/fsm.h"
90 #include "cyg/ppp/ipcp.h"
91 #include "cyg/ppp/ppp_io.h"
92
93 #ifdef CYGPKG_NS_DNS
94 #include <cyg/ns/dns/dns.h>
95 #endif
96
97 #define option_error(msg) db_printf("Option error: %s\n", msg )
98
99 /* global vars */
100 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
101 ipcp_options ipcp_gotoptions[NUM_PPP];  /* Options that peer ack'd */
102 ipcp_options ipcp_allowoptions[NUM_PPP];        /* Options we allow peer to request */
103 ipcp_options ipcp_hisoptions[NUM_PPP];  /* Options that we ack'd */
104
105 /* local vars */
106 static int cis_received[NUM_PPP];       /* # Conf-Reqs received */
107 static int default_route_set[NUM_PPP];  /* Have set up a default route */
108 static int proxy_arp_set[NUM_PPP];      /* Have created proxy arp entry */
109
110 /*
111  * Callbacks for fsm code.  (CI = Configuration Information)
112  */
113 static void ipcp_resetci __P((fsm *));  /* Reset our CI */
114 static int  ipcp_cilen __P((fsm *));            /* Return length of our CI */
115 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
116 static int  ipcp_ackci __P((fsm *, u_char *, int));     /* Peer ack'd our CI */
117 static int  ipcp_nakci __P((fsm *, u_char *, int));     /* Peer nak'd our CI */
118 static int  ipcp_rejci __P((fsm *, u_char *, int));     /* Peer rej'd our CI */
119 static int  ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
120 static void ipcp_up __P((fsm *));               /* We're UP */
121 static void ipcp_down __P((fsm *));             /* We're DOWN */
122 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
123
124 fsm ipcp_fsm[NUM_PPP];          /* IPCP fsm structure */
125
126 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
127     ipcp_resetci,               /* Reset our Configuration Information */
128     ipcp_cilen,                 /* Length of our Configuration Information */
129     ipcp_addci,                 /* Add our Configuration Information */
130     ipcp_ackci,                 /* ACK our Configuration Information */
131     ipcp_nakci,                 /* NAK our Configuration Information */
132     ipcp_rejci,                 /* Reject our Configuration Information */
133     ipcp_reqci,                 /* Request peer's Configuration Information */
134     ipcp_up,                    /* Called when fsm reaches OPENED state */
135     ipcp_down,                  /* Called when fsm leaves OPENED state */
136     NULL,                       /* Called when we want the lower layer up */
137     ipcp_finished,              /* Called when we want the lower layer down */
138     NULL,                       /* Called when Protocol-Reject received */
139     NULL,                       /* Retransmission is necessary */
140     NULL,                       /* Called to handle protocol-specific codes */
141     "IPCP"                      /* String name of protocol */
142 };
143
144 /*
145  * Protocol entry points from main code.
146  */
147 static void ipcp_init __P((int));
148 static void ipcp_open __P((int));
149 static void ipcp_close __P((int, char *));
150 static void ipcp_lowerup __P((int));
151 static void ipcp_lowerdown __P((int));
152 static void ipcp_input __P((int, u_char *, int));
153 static void ipcp_protrej __P((int));
154 static int  ipcp_printpkt __P((u_char *, int,
155                                void (*) __P((void *, char *, ...)), void *));
156 static void ip_check_options __P((void));
157 static int  ip_demand_conf __P((int));
158 static int  ip_active_pkt __P((u_char *, int));
159
160 struct protent ipcp_protent = {
161     PPP_IPCP,
162     ipcp_init,
163     ipcp_input,
164     ipcp_protrej,
165     ipcp_lowerup,
166     ipcp_lowerdown,
167     ipcp_open,
168     ipcp_close,
169     ipcp_printpkt,
170     NULL,
171     1,
172     "IPCP",
173     ip_check_options,
174     ip_demand_conf,
175     ip_active_pkt
176 };
177
178 static void ipcp_clear_addrs __P((int));
179
180 /*
181  * Lengths of configuration options.
182  */
183 #define CILEN_VOID      2
184 #define CILEN_COMPRESS  4       /* min length for compression protocol opt. */
185 #define CILEN_VJ        6       /* length for RFC1332 Van-Jacobson opt. */
186 #define CILEN_ADDR      6       /* new-style single address option */
187 #define CILEN_ADDRS     10      /* old-style dual address option */
188
189
190 #define CODENAME(x)     ((x) == CONFACK ? "ACK" : \
191                          (x) == CONFNAK ? "NAK" : "REJ")
192
193
194 /*
195  * Make a string representation of a network IP address.
196  */
197 char *
198 ip_ntoa(ipaddr)
199 u_int32_t ipaddr;
200 {
201     static char b[64];
202
203     ipaddr = ntohl(ipaddr);
204
205     sprintf(b, "%d.%d.%d.%d",
206             (u_char)(ipaddr >> 24),
207             (u_char)(ipaddr >> 16),
208             (u_char)(ipaddr >> 8),
209             (u_char)(ipaddr));
210     return b;
211 }
212
213
214 /*
215  * ipcp_init - Initialize IPCP.
216  */
217 static void
218 ipcp_init(unit)
219     int unit;
220 {
221     fsm *f = &ipcp_fsm[unit];
222     ipcp_options *wo = &ipcp_wantoptions[unit];
223     ipcp_options *ao = &ipcp_allowoptions[unit];
224
225     f->unit = unit;
226     f->protocol = PPP_IPCP;
227     f->callbacks = &ipcp_callbacks;
228     fsm_init(&ipcp_fsm[unit]);
229
230     memset(wo, 0, sizeof(*wo));
231     memset(ao, 0, sizeof(*ao));
232
233     wo->neg_addr = 1;
234     wo->neg_vj = 1;
235     wo->vj_protocol = IPCP_VJ_COMP;
236     wo->maxslotindex = MAX_STATES - 1; /* really max index */
237     wo->cflag = 1;
238     wo->ouraddr     = ppp_tty.options->our_address;
239     wo->hisaddr     = ppp_tty.options->his_address;
240     wo->default_route = ppp_tty.options->default_route;
241
242 #ifdef CYGOPT_PPP_NS_NEGOTIATE
243     wo->neg_dns1 = 1;
244     wo->neg_dns2 = 1;
245 #endif
246
247     /* max slots and slot-id compression are currently hardwired in */
248     /* ppp_if.c to 16 and 1, this needs to be changed (among other */
249     /* things) gmc */
250
251     ao->neg_addr = 1;
252     ao->neg_vj = 1;
253     ao->maxslotindex = MAX_STATES - 1;
254     ao->cflag = 1;
255
256     /*
257      * XXX These control whether the user may use the proxyarp
258      * and defaultroute options.
259      */
260     ao->proxy_arp = 1;
261     ao->default_route = 1;
262 }
263
264
265 /*
266  * ipcp_open - IPCP is allowed to come up.
267  */
268 static void
269 ipcp_open(unit)
270     int unit;
271 {
272     fsm_open(&ipcp_fsm[unit]);
273 }
274
275
276 /*
277  * ipcp_close - Take IPCP down.
278  */
279 static void
280 ipcp_close(unit, reason)
281     int unit;
282     char *reason;
283 {
284     fsm_close(&ipcp_fsm[unit], reason);
285 }
286
287
288 /*
289  * ipcp_lowerup - The lower layer is up.
290  */
291 static void
292 ipcp_lowerup(unit)
293     int unit;
294 {
295     fsm_lowerup(&ipcp_fsm[unit]);
296 }
297
298
299 /*
300  * ipcp_lowerdown - The lower layer is down.
301  */
302 static void
303 ipcp_lowerdown(unit)
304     int unit;
305 {
306     fsm_lowerdown(&ipcp_fsm[unit]);
307 }
308
309
310 /*
311  * ipcp_input - Input IPCP packet.
312  */
313 static void
314 ipcp_input(unit, p, len)
315     int unit;
316     u_char *p;
317     int len;
318 {
319     fsm_input(&ipcp_fsm[unit], p, len);
320 }
321
322
323 /*
324  * ipcp_protrej - A Protocol-Reject was received for IPCP.
325  *
326  * Pretend the lower layer went down, so we shut up.
327  */
328 static void
329 ipcp_protrej(unit)
330     int unit;
331 {
332     fsm_lowerdown(&ipcp_fsm[unit]);
333 }
334
335
336 /*
337  * ipcp_resetci - Reset our CI.
338  */
339 static void
340 ipcp_resetci(f)
341     fsm *f;
342 {
343     ipcp_options *wo = &ipcp_wantoptions[f->unit];
344
345     wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
346     if (wo->ouraddr == 0)
347         wo->accept_local = 1;
348     if (wo->hisaddr == 0)
349         wo->accept_remote = 1;
350     ipcp_gotoptions[f->unit] = *wo;
351     cis_received[f->unit] = 0;
352 }
353
354
355 /*
356  * ipcp_cilen - Return length of our CI.
357  */
358 static int
359 ipcp_cilen(f)
360     fsm *f;
361 {
362     ipcp_options *go = &ipcp_gotoptions[f->unit];
363     ipcp_options *wo = &ipcp_wantoptions[f->unit];
364     ipcp_options *ho = &ipcp_hisoptions[f->unit];
365     int len;
366
367 #define LENCIVJ(neg, old)       (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
368 #define LENCIADDR(neg, old)     (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
369
370     /*
371      * First see if we want to change our options to the old
372      * forms because we have received old forms from the peer.
373      */
374     if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
375         /* use the old style of address negotiation */
376         go->neg_addr = 1;
377         go->old_addrs = 1;
378     }
379     if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
380         /* try an older style of VJ negotiation */
381         if (cis_received[f->unit] == 0) {
382 #ifndef PPP_INHIBIT_OLD_VJ_COMPRESSION
383             /* keep trying the new style until we see some CI from the peer */
384             go->neg_vj = 1;
385 #endif
386         } else {
387             /* use the old style only if the peer did */
388             if (ho->neg_vj && ho->old_vj) {
389                 go->neg_vj = 1;
390                 go->old_vj = 1;
391                 go->vj_protocol = ho->vj_protocol;
392             }
393         }
394     }
395
396     len = LENCIADDR(go->neg_addr, go->old_addrs) +
397           LENCIVJ(go->neg_vj, go->old_vj);
398     
399 #ifdef CYGOPT_PPP_NS_NEGOTIATE
400     len += LENCIADDR(go->neg_dns1, 0) +
401            LENCIADDR(go->neg_wins1, 0) +
402            LENCIADDR(go->neg_dns2, 0) +
403            LENCIADDR(go->neg_wins2, 0);
404 #endif
405     return (len);
406 }
407
408
409 /*
410  * ipcp_addci - Add our desired CIs to a packet.
411  */
412 static void
413 ipcp_addci(f, ucp, lenp)
414     fsm *f;
415     u_char *ucp;
416     int *lenp;
417 {
418     ipcp_options *go = &ipcp_gotoptions[f->unit];
419     int len = *lenp;
420
421 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
422     if (neg) { \
423         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
424         if (len >= vjlen) { \
425             PUTCHAR(opt, ucp); \
426             PUTCHAR(vjlen, ucp); \
427             PUTSHORT(val, ucp); \
428             if (!old) { \
429                 PUTCHAR(maxslotindex, ucp); \
430                 PUTCHAR(cflag, ucp); \
431             } \
432             len -= vjlen; \
433         } else \
434             neg = 0; \
435     }
436
437 #define ADDCIADDR(opt, neg, old, val1, val2) \
438     if (neg) { \
439         int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
440         if (len >= addrlen) { \
441             u_int32_t l; \
442             PUTCHAR(opt, ucp); \
443             PUTCHAR(addrlen, ucp); \
444             l = ntohl(val1); \
445             PUTLONG(l, ucp); \
446             if (old) { \
447                 l = ntohl(val2); \
448                 PUTLONG(l, ucp); \
449             } \
450             len -= addrlen; \
451         } else \
452             neg = 0; \
453     }
454
455     ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
456               go->old_addrs, go->ouraddr, go->hisaddr);
457
458     ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
459             go->maxslotindex, go->cflag);
460
461 #ifdef CYGOPT_PPP_NS_NEGOTIATE
462     ADDCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
463     ADDCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
464     ADDCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
465     ADDCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
466 #endif
467
468     *lenp -= len;
469 }
470
471
472 /*
473  * ipcp_ackci - Ack our CIs.
474  *
475  * Returns:
476  *      0 - Ack was bad.
477  *      1 - Ack was good.
478  */
479 static int
480 ipcp_ackci(f, p, len)
481     fsm *f;
482     u_char *p;
483     int len;
484 {
485     ipcp_options *go = &ipcp_gotoptions[f->unit];
486     u_short cilen, citype, cishort;
487     u_int32_t cilong;
488     u_char cimaxslotindex, cicflag;
489
490     /*
491      * CIs must be in exactly the same order that we sent...
492      * Check packet length and CI length at each step.
493      * If we find any deviations, then this packet is bad.
494      */
495
496 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
497     if (neg) { \
498         int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
499         if ((len -= vjlen) < 0) \
500             goto bad; \
501         GETCHAR(citype, p); \
502         GETCHAR(cilen, p); \
503         if (cilen != vjlen || \
504             citype != opt)  \
505             goto bad; \
506         GETSHORT(cishort, p); \
507         if (cishort != val) \
508             goto bad; \
509         if (!old) { \
510             GETCHAR(cimaxslotindex, p); \
511             if (cimaxslotindex != maxslotindex) \
512                 goto bad; \
513             GETCHAR(cicflag, p); \
514             if (cicflag != cflag) \
515                 goto bad; \
516         } \
517     }
518
519 #define ACKCIADDR(opt, neg, old, val1, val2) \
520     if (neg) { \
521         int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
522         u_int32_t l; \
523         if ((len -= addrlen) < 0) \
524             goto bad; \
525         GETCHAR(citype, p); \
526         GETCHAR(cilen, p); \
527         if (cilen != addrlen || \
528             citype != opt) \
529             goto bad; \
530         GETLONG(l, p); \
531         cilong = htonl(l); \
532         if (val1 != cilong) \
533             goto bad; \
534         if (old) { \
535             GETLONG(l, p); \
536             cilong = htonl(l); \
537             if (val2 != cilong) \
538                 goto bad; \
539         } \
540     }
541
542     ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
543               go->old_addrs, go->ouraddr, go->hisaddr);
544
545     ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
546             go->maxslotindex, go->cflag);
547
548 #ifdef CYGOPT_PPP_NS_NEGOTIATE
549     ACKCIADDR(CI_MS_DNS1, go->neg_dns1, 0, go->dnsaddr[0], 0);
550     ACKCIADDR(CI_MS_WINS1, go->neg_wins1, 0, go->winsaddr[0], 0);
551     ACKCIADDR(CI_MS_DNS2, go->neg_dns2, 0, go->dnsaddr[1], 0);
552     ACKCIADDR(CI_MS_WINS2, go->neg_wins2, 0, go->winsaddr[1], 0);
553 #endif
554
555     /*
556      * If there are any remaining CIs, then this packet is bad.
557      */
558     if (len != 0)
559         goto bad;
560     return (1);
561
562 bad:
563     IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
564     return (0);
565 }
566
567 /*
568  * ipcp_nakci - Peer has sent a NAK for some of our CIs.
569  * This should not modify any state if the Nak is bad
570  * or if IPCP is in the OPENED state.
571  *
572  * Returns:
573  *      0 - Nak was bad.
574  *      1 - Nak was good.
575  */
576 static int
577 ipcp_nakci(f, p, len)
578     fsm *f;
579     u_char *p;
580     int len;
581 {
582     ipcp_options *go = &ipcp_gotoptions[f->unit];
583     u_char cimaxslotindex, cicflag;
584     u_char citype, cilen, *next;
585     u_short cishort;
586     u_int32_t ciaddr1, ciaddr2, l;
587     ipcp_options no;            /* options we've seen Naks for */
588     ipcp_options try;           /* options to request next time */
589
590     BZERO(&no, sizeof(no));
591     try = *go;
592
593     /*
594      * Any Nak'd CIs must be in exactly the same order that we sent.
595      * Check packet length and CI length at each step.
596      * If we find any deviations, then this packet is bad.
597      */
598 #define NAKCIADDR(opt, neg, old, code) \
599     if (go->neg && \
600         len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
601         p[1] == cilen && \
602         p[0] == opt) { \
603         len -= cilen; \
604         INCPTR(2, p); \
605         GETLONG(l, p); \
606         ciaddr1 = htonl(l); \
607         if (old) { \
608             GETLONG(l, p); \
609             ciaddr2 = htonl(l); \
610             no.old_addrs = 1; \
611         } else \
612             ciaddr2 = 0; \
613         no.neg = 1; \
614         code \
615     }
616
617 #define NAKCIVJ(opt, neg, code) \
618     if (go->neg && \
619         ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
620         len >= cilen && \
621         p[0] == opt) { \
622         len -= cilen; \
623         INCPTR(2, p); \
624         GETSHORT(cishort, p); \
625         no.neg = 1; \
626         code \
627     }
628
629     /*
630      * Accept the peer's idea of {our,his} address, if different
631      * from our idea, only if the accept_{local,remote} flag is set.
632      */
633     NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
634               if (go->accept_local && ciaddr1) { /* Do we know our address? */
635                   try.ouraddr = ciaddr1;
636                   IPCPDEBUG((LOG_INFO, "local IP address %s",
637                              ip_ntoa(ciaddr1)));
638               }
639               if (go->accept_remote && ciaddr2) { /* Does he know his? */
640                   try.hisaddr = ciaddr2;
641                   IPCPDEBUG((LOG_INFO, "remote IP address %s",
642                              ip_ntoa(ciaddr2)));
643               }
644               );
645
646     /*
647      * Accept the peer's value of maxslotindex provided that it
648      * is less than what we asked for.  Turn off slot-ID compression
649      * if the peer wants.  Send old-style compress-type option if
650      * the peer wants.
651      */
652     NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
653             if (cilen == CILEN_VJ) {
654                 GETCHAR(cimaxslotindex, p);
655                 GETCHAR(cicflag, p);
656                 if (cishort == IPCP_VJ_COMP) {
657                     try.old_vj = 0;
658                     if (cimaxslotindex < go->maxslotindex)
659                         try.maxslotindex = cimaxslotindex;
660                     if (!cicflag)
661                         try.cflag = 0;
662                 } else {
663                     try.neg_vj = 0;
664                 }
665             } else {
666                 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
667                     try.old_vj = 1;
668                     try.vj_protocol = cishort;
669                 } else {
670                     try.neg_vj = 0;
671                 }
672             }
673             );
674
675 #ifdef CYGOPT_PPP_NS_NEGOTIATE
676     /*
677      * Accept the peer's idea of the DNS and WINS addresses
678      */
679     NAKCIADDR(CI_MS_DNS1, neg_dns1, 0, try.dnsaddr[0] = ciaddr1;);
680     NAKCIADDR(CI_MS_WINS1, neg_wins1, 0, try.winsaddr[0] = ciaddr1;);
681     NAKCIADDR(CI_MS_DNS2, neg_dns2, 0, try.dnsaddr[1] = ciaddr1;);
682     NAKCIADDR(CI_MS_WINS2, neg_wins2, 0, try.winsaddr[1] = ciaddr1;);
683 #ifdef CYGOPT_PPP_DNS_CONFIGURE
684         if (try.dnsaddr[0] != 0 )
685         {
686         cyg_dns_res_init((struct in_addr *)&try.dnsaddr[0]);
687     }
688 #endif
689 #endif
690
691     /*
692      * There may be remaining CIs, if the peer is requesting negotiation
693      * on an option that we didn't include in our request packet.
694      * If they want to negotiate about IP addresses, we comply.
695      * If they want us to ask for compression, we refuse.
696      */
697     while (len > CILEN_VOID) {
698         GETCHAR(citype, p);
699         GETCHAR(cilen, p);
700         if( (len -= cilen) < 0 )
701             goto bad;
702         next = p + cilen - 2;
703
704         switch (citype) {
705         case CI_COMPRESSTYPE:
706             if (go->neg_vj || no.neg_vj ||
707                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
708                 goto bad;
709             no.neg_vj = 1;
710             break;
711         case CI_ADDRS:
712             if ((go->neg_addr && go->old_addrs) || no.old_addrs
713                 || cilen != CILEN_ADDRS)
714                 goto bad;
715             try.neg_addr = 1;
716             try.old_addrs = 1;
717             GETLONG(l, p);
718             ciaddr1 = htonl(l);
719             if (ciaddr1 && go->accept_local)
720                 try.ouraddr = ciaddr1;
721             GETLONG(l, p);
722             ciaddr2 = htonl(l);
723             if (ciaddr2 && go->accept_remote)
724                 try.hisaddr = ciaddr2;
725             no.old_addrs = 1;
726             break;
727         case CI_ADDR:
728             if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
729                 goto bad;
730             try.old_addrs = 0;
731             GETLONG(l, p);
732             ciaddr1 = htonl(l);
733             if (ciaddr1 && go->accept_local)
734                 try.ouraddr = ciaddr1;
735             if (try.ouraddr != 0)
736                 try.neg_addr = 1;
737             no.neg_addr = 1;
738             break;
739         }
740         p = next;
741     }
742
743     /* If there is still anything left, this packet is bad. */
744     if (len != 0)
745         goto bad;
746
747     /*
748      * OK, the Nak is good.  Now we can update state.
749      */
750     if (f->state != OPENED)
751         *go = try;
752
753     return 1;
754
755 bad:
756     IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
757     return 0;
758 }
759
760
761 /*
762  * ipcp_rejci - Reject some of our CIs.
763  */
764 static int
765 ipcp_rejci(f, p, len)
766     fsm *f;
767     u_char *p;
768     int len;
769 {
770     ipcp_options *go = &ipcp_gotoptions[f->unit];
771     u_char cimaxslotindex, ciflag, cilen;
772     u_short cishort;
773     u_int32_t cilong;
774     ipcp_options try;           /* options to request next time */
775 #ifdef CYGOPT_PPP_NS_NEGOTIATE
776     u_char citype, *next;    
777 #endif
778
779     try = *go;
780     /*
781      * Any Rejected CIs must be in exactly the same order that we sent.
782      * Check packet length and CI length at each step.
783      * If we find any deviations, then this packet is bad.
784      */
785 #define REJCIADDR(opt, neg, old, val1, val2) \
786     if (go->neg && \
787         len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
788         p[1] == cilen && \
789         p[0] == opt) { \
790         u_int32_t l; \
791         len -= cilen; \
792         INCPTR(2, p); \
793         GETLONG(l, p); \
794         cilong = htonl(l); \
795         /* Check rejected value. */ \
796         if (cilong != val1) \
797             goto bad; \
798         if (old) { \
799             GETLONG(l, p); \
800             cilong = htonl(l); \
801             /* Check rejected value. */ \
802             if (cilong != val2) \
803                 goto bad; \
804         } \
805         try.neg = 0; \
806     }
807
808 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
809     if (go->neg && \
810         p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
811         len >= p[1] && \
812         p[0] == opt) { \
813         len -= p[1]; \
814         INCPTR(2, p); \
815         GETSHORT(cishort, p); \
816         /* Check rejected value. */  \
817         if (cishort != val) \
818             goto bad; \
819         if (!old) { \
820            GETCHAR(cimaxslotindex, p); \
821            if (cimaxslotindex != maxslot) \
822              goto bad; \
823            GETCHAR(ciflag, p); \
824            if (ciflag != cflag) \
825              goto bad; \
826         } \
827         try.neg = 0; \
828      }
829
830     REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
831               go->old_addrs, go->ouraddr, go->hisaddr);
832
833     REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
834             go->maxslotindex, go->cflag);
835
836 #ifdef CYGOPT_PPP_NS_NEGOTIATE
837 /*
838      * There may be remaining CIs, if the peer is unable to support
839      * DNS or WINS negotiation.  If so, just turn them off
840      *
841      */     
842             
843     while (len > CILEN_VOID) {
844         GETCHAR(citype, p);
845         GETCHAR(cilen, p);
846         if( (len -= cilen) < 0 )
847             goto bad;
848         next = p + cilen - 2;
849
850         switch (citype) {
851         case CI_MS_DNS1:
852                 try.neg_dns1 = 0;
853                 break;
854         case CI_MS_WINS1:
855                 try.neg_wins1 = 0;
856                 break;
857         case CI_MS_DNS2:
858                 try.neg_dns2 = 0;
859                 break;
860         case CI_MS_WINS2:
861                 try.neg_wins2 = 0;
862                 break;
863         }
864         p = next;
865     }           
866 #endif
867
868     /*
869      * If there are any remaining CIs, then this packet is bad.
870      */
871     if (len != 0)
872         goto bad;
873     /*
874      * Now we can update state.
875      */
876     if (f->state != OPENED)
877         *go = try;
878     return 1;
879
880 bad:
881     IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
882     return 0;
883 }
884
885
886 /*
887  * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
888  *
889  * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
890  * appropriately.  If reject_if_disagree is non-zero, doesn't return
891  * CONFNAK; returns CONFREJ if it can't return CONFACK.
892  */
893 static int
894 ipcp_reqci(f, inp, len, reject_if_disagree)
895     fsm *f;
896     u_char *inp;                /* Requested CIs */
897     int *len;                   /* Length of requested CIs */
898     int reject_if_disagree;
899 {
900     ipcp_options *wo = &ipcp_wantoptions[f->unit];
901     ipcp_options *ho = &ipcp_hisoptions[f->unit];
902     ipcp_options *ao = &ipcp_allowoptions[f->unit];
903     ipcp_options *go = &ipcp_gotoptions[f->unit];
904     u_char *cip, *next;         /* Pointer to current and next CIs */
905     u_short cilen, citype;      /* Parsed len, type */
906     u_short cishort;            /* Parsed short value */
907     u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
908     int rc = CONFACK;           /* Final packet return code */
909     int orc;                    /* Individual option return code */
910     u_char *p;                  /* Pointer to next char to parse */
911     u_char *ucp = inp;          /* Pointer to current output char */
912     int l = *len;               /* Length left */
913     u_char maxslotindex, cflag;
914     int d;
915
916     cis_received[f->unit] = 1;
917
918     /*
919      * Reset all his options.
920      */
921     BZERO(ho, sizeof(*ho));
922     
923     /*
924      * Process all his options.
925      */
926     next = inp;
927     while (l) {
928         orc = CONFACK;                  /* Assume success */
929         cip = p = next;                 /* Remember begining of CI */
930         if (l < 2 ||                    /* Not enough data for CI header or */
931             p[1] < 2 ||                 /*  CI length too small or */
932             p[1] > l) {                 /*  CI length too big? */
933             IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
934             orc = CONFREJ;              /* Reject bad CI */
935             cilen = l;                  /* Reject till end of packet */
936             l = 0;                      /* Don't loop again */
937             goto endswitch;
938         }
939         GETCHAR(citype, p);             /* Parse CI type */
940         GETCHAR(cilen, p);              /* Parse CI length */
941         l -= cilen;                     /* Adjust remaining length */
942         next += cilen;                  /* Step to next CI */
943
944         switch (citype) {               /* Check CI type */
945         case CI_ADDRS:
946             IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
947             if (!ao->neg_addr ||
948                 cilen != CILEN_ADDRS) { /* Check CI length */
949                 orc = CONFREJ;          /* Reject CI */
950                 break;
951             }
952
953             /*
954              * If he has no address, or if we both have his address but
955              * disagree about it, then NAK it with our idea.
956              * In particular, if we don't know his address, but he does,
957              * then accept it.
958              */
959             GETLONG(tl, p);             /* Parse source address (his) */
960             ciaddr1 = htonl(tl);
961             IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
962             if (ciaddr1 != wo->hisaddr
963                 && (ciaddr1 == 0 || !wo->accept_remote)) {
964                 orc = CONFNAK;
965                 if (!reject_if_disagree) {
966                     DECPTR(sizeof(u_int32_t), p);
967                     tl = ntohl(wo->hisaddr);
968                     PUTLONG(tl, p);
969                 }
970             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
971                 /*
972                  * If neither we nor he knows his address, reject the option.
973                  */
974                 orc = CONFREJ;
975                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
976                 break;
977             }
978
979             /*
980              * If he doesn't know our address, or if we both have our address
981              * but disagree about it, then NAK it with our idea.
982              */
983             GETLONG(tl, p);             /* Parse desination address (ours) */
984             ciaddr2 = htonl(tl);
985             IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
986             if (ciaddr2 != wo->ouraddr) {
987                 if (ciaddr2 == 0 || !wo->accept_local) {
988                     orc = CONFNAK;
989                     if (!reject_if_disagree) {
990                         DECPTR(sizeof(u_int32_t), p);
991                         tl = ntohl(wo->ouraddr);
992                         PUTLONG(tl, p);
993                     }
994                 } else {
995                     go->ouraddr = ciaddr2;      /* accept peer's idea */
996                 }
997             }
998
999             ho->neg_addr = 1;
1000             ho->old_addrs = 1;
1001             ho->hisaddr = ciaddr1;
1002             ho->ouraddr = ciaddr2;
1003             break;
1004
1005         case CI_ADDR:
1006             IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1007
1008             if (!ao->neg_addr ||
1009                 cilen != CILEN_ADDR) {  /* Check CI length */
1010                 orc = CONFREJ;          /* Reject CI */
1011                 break;
1012             }
1013
1014             /*
1015              * If he has no address, or if we both have his address but
1016              * disagree about it, then NAK it with our idea.
1017              * In particular, if we don't know his address, but he does,
1018              * then accept it.
1019              */
1020             GETLONG(tl, p);     /* Parse source address (his) */
1021             ciaddr1 = htonl(tl);
1022             IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1023             if (ciaddr1 != wo->hisaddr
1024                 && (ciaddr1 == 0 || !wo->accept_remote)) {
1025                 orc = CONFNAK;
1026                 if (!reject_if_disagree) {
1027                     DECPTR(sizeof(u_int32_t), p);
1028                     tl = ntohl(wo->hisaddr);
1029                     PUTLONG(tl, p);
1030                 }
1031             } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1032                 /*
1033                  * Don't ACK an address of 0.0.0.0 - reject it instead.
1034                  */
1035                 orc = CONFREJ;
1036                 wo->req_addr = 0;       /* don't NAK with 0.0.0.0 later */
1037                 break;
1038             }
1039         
1040             ho->neg_addr = 1;
1041             ho->hisaddr = ciaddr1;
1042             break;
1043
1044         case CI_MS_DNS1:
1045         case CI_MS_DNS2:
1046             /* Microsoft primary or secondary DNS request */
1047             d = citype == CI_MS_DNS2;
1048             IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1049
1050             /* If we do not have a DNS address then we cannot send it */
1051             if (ao->dnsaddr[d] == 0 ||
1052                 cilen != CILEN_ADDR) {  /* Check CI length */
1053                 orc = CONFREJ;          /* Reject CI */
1054                 break;
1055             }
1056             GETLONG(tl, p);
1057             if (htonl(tl) != ao->dnsaddr[d]) {
1058                 DECPTR(sizeof(u_int32_t), p);
1059                 tl = ntohl(ao->dnsaddr[d]);
1060                 PUTLONG(tl, p);
1061                 orc = CONFNAK;
1062             }
1063             break;
1064
1065         case CI_MS_WINS1:
1066         case CI_MS_WINS2:
1067             /* Microsoft primary or secondary WINS request */
1068             d = citype == CI_MS_WINS2;
1069             IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1070
1071             /* If we do not have a DNS address then we cannot send it */
1072             if (ao->winsaddr[d] == 0 ||
1073                 cilen != CILEN_ADDR) {  /* Check CI length */
1074                 orc = CONFREJ;          /* Reject CI */
1075                 break;
1076             }
1077             GETLONG(tl, p);
1078             if (htonl(tl) != ao->winsaddr[d]) {
1079                 DECPTR(sizeof(u_int32_t), p);
1080                 tl = ntohl(ao->winsaddr[d]);
1081                 PUTLONG(tl, p);
1082                 orc = CONFNAK;
1083             }
1084             break;
1085         
1086         case CI_COMPRESSTYPE:
1087             IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1088             if (!ao->neg_vj ||
1089                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1090                 orc = CONFREJ;
1091                 break;
1092             }
1093             GETSHORT(cishort, p);
1094             IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1095
1096             if (!(cishort == IPCP_VJ_COMP ||
1097                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1098                 orc = CONFREJ;
1099                 break;
1100             }
1101
1102             ho->neg_vj = 1;
1103             ho->vj_protocol = cishort;
1104             if (cilen == CILEN_VJ) {
1105                 GETCHAR(maxslotindex, p);
1106                 if (maxslotindex > ao->maxslotindex) { 
1107                     orc = CONFNAK;
1108                     if (!reject_if_disagree){
1109                         DECPTR(1, p);
1110                         PUTCHAR(ao->maxslotindex, p);
1111                     }
1112                 }
1113                 GETCHAR(cflag, p);
1114                 if (cflag && !ao->cflag) {
1115                     orc = CONFNAK;
1116                     if (!reject_if_disagree){
1117                         DECPTR(1, p);
1118                         PUTCHAR(wo->cflag, p);
1119                     }
1120                 }
1121                 ho->maxslotindex = maxslotindex;
1122                 ho->cflag = cflag;
1123             } else {
1124                 ho->old_vj = 1;
1125                 ho->maxslotindex = MAX_STATES - 1;
1126                 ho->cflag = 1;
1127             }
1128             break;
1129
1130         default:
1131             orc = CONFREJ;
1132             break;
1133         }
1134
1135 endswitch:
1136         IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1137
1138         if (orc == CONFACK &&           /* Good CI */
1139             rc != CONFACK)              /*  but prior CI wasnt? */
1140             continue;                   /* Don't send this one */
1141
1142         if (orc == CONFNAK) {           /* Nak this CI? */
1143             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
1144                 orc = CONFREJ;          /* Get tough if so */
1145             else {
1146                 if (rc == CONFREJ)      /* Rejecting prior CI? */
1147                     continue;           /* Don't send this one */
1148                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
1149                     rc = CONFNAK;       /* Not anymore... */
1150                     ucp = inp;          /* Backup */
1151                 }
1152             }
1153         }
1154
1155         if (orc == CONFREJ &&           /* Reject this CI */
1156             rc != CONFREJ) {            /*  but no prior ones? */
1157             rc = CONFREJ;
1158             ucp = inp;                  /* Backup */
1159         }
1160
1161         /* Need to move CI? */
1162         if (ucp != cip)
1163             BCOPY(cip, ucp, cilen);     /* Move it */
1164
1165         /* Update output pointer */
1166         INCPTR(cilen, ucp);
1167     }
1168
1169     /*
1170      * If we aren't rejecting this packet, and we want to negotiate
1171      * their address, and they didn't send their address, then we
1172      * send a NAK with a CI_ADDR option appended.  We assume the
1173      * input buffer is long enough that we can append the extra
1174      * option safely.
1175      */
1176     if (rc != CONFREJ && !ho->neg_addr &&
1177         wo->req_addr && !reject_if_disagree) {
1178         if (rc == CONFACK) {
1179             rc = CONFNAK;
1180             ucp = inp;                  /* reset pointer */
1181             wo->req_addr = 0;           /* don't ask again */
1182         }
1183         PUTCHAR(CI_ADDR, ucp);
1184         PUTCHAR(CILEN_ADDR, ucp);
1185         tl = ntohl(wo->hisaddr);
1186         PUTLONG(tl, ucp);
1187     }
1188
1189     *len = ucp - inp;                   /* Compute output length */
1190     IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1191     return (rc);                        /* Return final code */
1192 }
1193
1194
1195 /*
1196  * ip_check_options - check that any IP-related options are OK,
1197  * and assign appropriate defaults.
1198  */
1199 static void
1200 ip_check_options()
1201 {
1202     struct hostent *hp;
1203     u_int32_t local;
1204     ipcp_options *wo = &ipcp_wantoptions[0];
1205
1206     /*
1207      * Default our local IP address based on our hostname.
1208      * If local IP address already given, don't bother.
1209      */
1210     if (wo->ouraddr == 0 && !disable_defaultip) {
1211         /*
1212          * Look up our hostname (possibly with domain name appended)
1213          * and take the first IP address as our local IP address.
1214          * If there isn't an IP address for our hostname, too bad.
1215          */
1216         wo->accept_local = 1;   /* don't insist on this default value */
1217         if ((hp = gethostbyname(cyg_ppp_hostname)) != NULL) {
1218             local = *(u_int32_t *)hp->h_addr;
1219             if (local != 0 && !bad_ip_adrs(local))
1220                 wo->ouraddr = local;
1221         }
1222     }
1223
1224     if (demand && wo->hisaddr == 0) {
1225         option_error("remote IP address required for demand-dialling\n");
1226     }
1227 }
1228
1229
1230 /*
1231  * ip_demand_conf - configure the interface as though
1232  * IPCP were up, for use with dial-on-demand.
1233  */
1234 static int
1235 ip_demand_conf(u)
1236     int u;
1237 {
1238     ipcp_options *wo = &ipcp_wantoptions[u];
1239
1240     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1241         return 0;
1242     if (!sifup(u))
1243         return 0;
1244     if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1245         return 0;
1246     if (wo->default_route)
1247         if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1248             default_route_set[u] = 1;
1249     if (wo->proxy_arp)
1250         if (sifproxyarp(u, wo->hisaddr))
1251             proxy_arp_set[u] = 1;
1252
1253     syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(wo->ouraddr));
1254     syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1255
1256     return 1;
1257 }
1258
1259
1260 /*
1261  * ipcp_up - IPCP has come UP.
1262  *
1263  * Configure the IP network interface appropriately and bring it up.
1264  */
1265 static void
1266 ipcp_up(f)
1267     fsm *f;
1268 {
1269     u_int32_t mask;
1270     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1271     ipcp_options *go = &ipcp_gotoptions[f->unit];
1272     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1273
1274     np_up(f->unit, PPP_IP);
1275     IPCPDEBUG((LOG_INFO, "ipcp: up"));
1276
1277     /*
1278      * We must have a non-zero IP address for both ends of the link.
1279      */
1280     if (!ho->neg_addr)
1281         ho->hisaddr = wo->hisaddr;
1282
1283     if (ho->hisaddr == 0) {
1284         syslog(LOG_ERR, "Could not determine remote IP address");
1285         ipcp_close(f->unit, "Could not determine remote IP address");
1286         return;
1287     }
1288     if (go->ouraddr == 0) {
1289         syslog(LOG_ERR, "Could not determine local IP address");
1290         ipcp_close(f->unit, "Could not determine local IP address");
1291         return;
1292     }
1293     
1294     /*
1295      * Check that the peer is allowed to use the IP address it wants.
1296      */
1297     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1298         syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1299                ip_ntoa(ho->hisaddr));
1300         ipcp_close(f->unit, "Unauthorized remote IP address");
1301         return;
1302     }
1303
1304     /* set tcp compression */
1305     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1306
1307     /*
1308      * If we are doing dial-on-demand, the interface is already
1309      * configured, so we put out any saved-up packets, then set the
1310      * interface to pass IP packets.
1311      */
1312     if (demand) {
1313         if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1314             if (go->ouraddr != wo->ouraddr)
1315                 syslog(LOG_WARNING, "Local IP address changed to %s",
1316                        ip_ntoa(go->ouraddr));
1317             if (ho->hisaddr != wo->hisaddr)
1318                 syslog(LOG_WARNING, "Remote IP address changed to %s",
1319                        ip_ntoa(ho->hisaddr));
1320             ipcp_clear_addrs(f->unit);
1321
1322             /* Set the interface to the new addresses */
1323             mask = GetMask(go->ouraddr);
1324             if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1325                 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1326                 ipcp_close(f->unit, "Interface configuration failed");
1327                 return;
1328             }
1329
1330             /* assign a default route through the interface if required */
1331             if (ipcp_wantoptions[f->unit].default_route) 
1332                 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1333                     default_route_set[f->unit] = 1;
1334
1335             /* Make a proxy ARP entry if requested. */
1336             if (ipcp_wantoptions[f->unit].proxy_arp)
1337                 if (sifproxyarp(f->unit, ho->hisaddr))
1338                     proxy_arp_set[f->unit] = 1;
1339
1340         }
1341         demand_rexmit(PPP_IP);
1342         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1343
1344     } else {
1345         /*
1346          * Set IP addresses and (if specified) netmask.
1347          */
1348         mask = GetMask(go->ouraddr);
1349
1350 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1351         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1352             IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1353             ipcp_close(f->unit, "Interface configuration failed");
1354             return;
1355         }
1356 #endif
1357
1358         /* bring the interface up for IP */
1359         if (!sifup(f->unit)) {
1360             IPCPDEBUG((LOG_WARNING, "sifup failed"));
1361             ipcp_close(f->unit, "Interface configuration failed");
1362             return;
1363         }
1364
1365 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1366         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1367             IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1368             ipcp_close(f->unit, "Interface configuration failed");
1369             return;
1370         }
1371 #endif
1372         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1373
1374         /* assign a default route through the interface if required */
1375         if (ipcp_wantoptions[f->unit].default_route) 
1376             if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1377                 default_route_set[f->unit] = 1;
1378
1379         /* Make a proxy ARP entry if requested. */
1380         if (ipcp_wantoptions[f->unit].proxy_arp)
1381             if (sifproxyarp(f->unit, ho->hisaddr))
1382                 proxy_arp_set[f->unit] = 1;
1383
1384         syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1385         syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1386     }
1387
1388 }
1389
1390
1391 /*
1392  * ipcp_down - IPCP has gone DOWN.
1393  *
1394  * Take the IP network interface down, clear its addresses
1395  * and delete routes through it.
1396  */
1397 static void
1398 ipcp_down(f)
1399     fsm *f;
1400 {
1401     IPCPDEBUG((LOG_INFO, "ipcp: down"));
1402     np_down(f->unit, PPP_IP);
1403     sifvjcomp(f->unit, 0, 0, 0);
1404
1405     /*
1406      * If we are doing dial-on-demand, set the interface
1407      * to queue up outgoing packets (for now).
1408      */
1409     if (demand) {
1410         sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1411     } else {
1412         sifdown(f->unit);
1413         ipcp_clear_addrs(f->unit);
1414     }
1415
1416 }
1417
1418
1419 /*
1420  * ipcp_clear_addrs() - clear the interface addresses, routes,
1421  * proxy arp entries, etc.
1422  */
1423 static void
1424 ipcp_clear_addrs(unit)
1425     int unit;
1426 {
1427     u_int32_t ouraddr, hisaddr;
1428
1429     ouraddr = ipcp_gotoptions[unit].ouraddr;
1430     hisaddr = ipcp_hisoptions[unit].hisaddr;
1431     if (proxy_arp_set[unit]) {
1432         cifproxyarp(unit, hisaddr);
1433         proxy_arp_set[unit] = 0;
1434     }
1435     if (default_route_set[unit]) {
1436         cifdefaultroute(unit, ouraddr, hisaddr);
1437         default_route_set[unit] = 0;
1438     }
1439     cifaddr(unit, ouraddr, hisaddr);
1440 }
1441
1442
1443 /*
1444  * ipcp_finished - possibly shut down the lower layers.
1445  */
1446 static void
1447 ipcp_finished(f)
1448     fsm *f;
1449 {
1450     np_finished(f->unit, PPP_IP);
1451 }
1452
1453 /*
1454  * ipcp_printpkt - print the contents of an IPCP packet.
1455  */
1456 static char *ipcp_codenames[] = {
1457     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1458     "TermReq", "TermAck", "CodeRej"
1459 };
1460
1461 static int
1462 ipcp_printpkt(p, plen, printer, arg)
1463     u_char *p;
1464     int plen;
1465     void (*printer) __P((void *, char *, ...));
1466     void *arg;
1467 {
1468     int code, id, len, olen;
1469     u_char *pstart, *optend;
1470     u_short cishort;
1471     u_int32_t cilong;
1472
1473     if (plen < HEADERLEN)
1474         return 0;
1475     pstart = p;
1476     GETCHAR(code, p);
1477     GETCHAR(id, p);
1478     GETSHORT(len, p);
1479     if (len < HEADERLEN || len > plen)
1480         return 0;
1481
1482     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1483         printer(arg, " %s", ipcp_codenames[code-1]);
1484     else
1485         printer(arg, " code=0x%x", code);
1486     printer(arg, " id=0x%x", id);
1487     len -= HEADERLEN;
1488     switch (code) {
1489     case CONFREQ:
1490     case CONFACK:
1491     case CONFNAK:
1492     case CONFREJ:
1493         /* print option list */
1494         while (len >= 2) {
1495             GETCHAR(code, p);
1496             GETCHAR(olen, p);
1497             p -= 2;
1498             if (olen < 2 || olen > len) {
1499                 break;
1500             }
1501             printer(arg, " <");
1502             len -= olen;
1503             optend = p + olen;
1504             switch (code) {
1505             case CI_ADDRS:
1506                 if (olen == CILEN_ADDRS) {
1507                     p += 2;
1508                     GETLONG(cilong, p);
1509                     printer(arg, "addrs %I", htonl(cilong));
1510                     GETLONG(cilong, p);
1511                     printer(arg, " %I", htonl(cilong));
1512                 }
1513                 break;
1514             case CI_COMPRESSTYPE:
1515                 if (olen >= CILEN_COMPRESS) {
1516                     p += 2;
1517                     GETSHORT(cishort, p);
1518                     printer(arg, "compress ");
1519                     switch (cishort) {
1520                     case IPCP_VJ_COMP:
1521                         printer(arg, "VJ");
1522                         break;
1523                     case IPCP_VJ_COMP_OLD:
1524                         printer(arg, "old-VJ");
1525                         break;
1526                     default:
1527                         printer(arg, "0x%x", cishort);
1528                     }
1529                 }
1530                 break;
1531             case CI_ADDR:
1532                 if (olen == CILEN_ADDR) {
1533                     p += 2;
1534                     GETLONG(cilong, p);
1535                     printer(arg, "addr %I", htonl(cilong));
1536                 }
1537                 break;
1538             case CI_MS_DNS1:
1539             case CI_MS_DNS2:
1540                 p += 2;
1541                 GETLONG(cilong, p);
1542                 printer(arg, "ms-dns %I", htonl(cilong));
1543                 break;
1544             case CI_MS_WINS1:
1545             case CI_MS_WINS2:
1546                 p += 2;
1547                 GETLONG(cilong, p);
1548                 printer(arg, "ms-wins %I", htonl(cilong));
1549                 break;
1550             }
1551             while (p < optend) {
1552                 GETCHAR(code, p);
1553                 printer(arg, " %.2x", code);
1554             }
1555             printer(arg, ">");
1556         }
1557         break;
1558
1559     case TERMACK:
1560     case TERMREQ:
1561         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1562             printer(arg, " ");
1563             print_string(p, len, printer, arg);
1564             p += len;
1565             len = 0;
1566         }
1567         break;
1568     }
1569
1570     /* print the rest of the bytes in the packet */
1571     for (; len > 0; --len) {
1572         GETCHAR(code, p);
1573         printer(arg, " %.2x", code);
1574     }
1575
1576     return p - pstart;
1577 }
1578
1579 /*
1580  * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1581  * We don't bring the link up for IP fragments or for TCP FIN packets
1582  * with no data.
1583  */
1584 #define IP_HDRLEN       20      /* bytes */
1585 #define IP_OFFMASK      0x1fff
1586 #define IPPROTO_TCP     6
1587 #define TCP_HDRLEN      20
1588 #define TH_FIN          0x01
1589
1590 /*
1591  * We use these macros because the IP header may be at an odd address,
1592  * and some compilers might use word loads to get th_off or ip_hl.
1593  */
1594
1595 #define net_short(x)    (((x)[0] << 8) + (x)[1])
1596 #define get_iphl(x)     (((unsigned char *)(x))[0] & 0xF)
1597 #define get_ipoff(x)    net_short((unsigned char *)(x) + 6)
1598 #define get_ipproto(x)  (((unsigned char *)(x))[9])
1599 #define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
1600 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1601
1602 static int
1603 ip_active_pkt(pkt, len)
1604     u_char *pkt;
1605     int len;
1606 {
1607     u_char *tcp;
1608     int hlen;
1609
1610     len -= PPP_HDRLEN;
1611     pkt += PPP_HDRLEN;
1612     if (len < IP_HDRLEN)
1613         return 0;
1614     if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1615         return 0;
1616     if (get_ipproto(pkt) != IPPROTO_TCP)
1617         return 1;
1618     hlen = get_iphl(pkt) * 4;
1619     if (len < hlen + TCP_HDRLEN)
1620         return 0;
1621     tcp = pkt + hlen;
1622     if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
1623         return 0;
1624     return 1;
1625 }