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