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