]> git.ozlabs.org Git - ppp.git/blob - pppd/ipcp.c
fixes for old C compilers and for Ultrix
[ppp.git] / pppd / ipcp.c
1 /*
2  * ipcp.c - PPP IP Control Protocol.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Carnegie Mellon University.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #ifndef lint
21 static char rcsid[] = "$Id: ipcp.c,v 1.28 1996/08/28 06:40:29 paulus Exp $";
22 #endif
23
24 /*
25  * TODO:
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <netdb.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
39
40 #include "pppd.h"
41 #include "fsm.h"
42 #include "ipcp.h"
43 #include "pathnames.h"
44
45 /* global vars */
46 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
47 ipcp_options ipcp_gotoptions[NUM_PPP];  /* Options that peer ack'd */
48 ipcp_options ipcp_allowoptions[NUM_PPP];        /* Options we allow peer to request */
49 ipcp_options ipcp_hisoptions[NUM_PPP];  /* Options that we ack'd */
50
51 /* local vars */
52 static int cis_received[NUM_PPP];       /* # Conf-Reqs received */
53 static int default_route_set[NUM_PPP];  /* Have set up a default route */
54 static int proxy_arp_set[NUM_PPP];      /* Have created proxy arp entry */
55
56 /*
57  * Callbacks for fsm code.  (CI = Configuration Information)
58  */
59 static void ipcp_resetci __P((fsm *));  /* Reset our CI */
60 static int  ipcp_cilen __P((fsm *));            /* Return length of our CI */
61 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
62 static int  ipcp_ackci __P((fsm *, u_char *, int));     /* Peer ack'd our CI */
63 static int  ipcp_nakci __P((fsm *, u_char *, int));     /* Peer nak'd our CI */
64 static int  ipcp_rejci __P((fsm *, u_char *, int));     /* Peer rej'd our CI */
65 static int  ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
66 static void ipcp_up __P((fsm *));               /* We're UP */
67 static void ipcp_down __P((fsm *));             /* We're DOWN */
68 static void ipcp_script __P((fsm *, char *)); /* Run an up/down script */
69 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
70
71 fsm ipcp_fsm[NUM_PPP];          /* IPCP fsm structure */
72
73 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
74     ipcp_resetci,               /* Reset our Configuration Information */
75     ipcp_cilen,                 /* Length of our Configuration Information */
76     ipcp_addci,                 /* Add our Configuration Information */
77     ipcp_ackci,                 /* ACK our Configuration Information */
78     ipcp_nakci,                 /* NAK our Configuration Information */
79     ipcp_rejci,                 /* Reject our Configuration Information */
80     ipcp_reqci,                 /* Request peer's Configuration Information */
81     ipcp_up,                    /* Called when fsm reaches OPENED state */
82     ipcp_down,                  /* Called when fsm leaves OPENED state */
83     NULL,                       /* Called when we want the lower layer up */
84     ipcp_finished,              /* Called when we want the lower layer down */
85     NULL,                       /* Called when Protocol-Reject received */
86     NULL,                       /* Retransmission is necessary */
87     NULL,                       /* Called to handle protocol-specific codes */
88     "IPCP"                      /* String name of protocol */
89 };
90
91 /*
92  * Protocol entry points from main code.
93  */
94 static void ipcp_init __P((int));
95 static void ipcp_open __P((int));
96 static void ipcp_close __P((int, char *));
97 static void ipcp_lowerup __P((int));
98 static void ipcp_lowerdown __P((int));
99 static void ipcp_input __P((int, u_char *, int));
100 static void ipcp_protrej __P((int));
101 static int  ipcp_printpkt __P((u_char *, int,
102                                void (*) __P((void *, char *, ...)), void *));
103 static void ip_check_options __P((void));
104 static int  ip_demand_conf __P((int));
105 static int  ip_active_pkt __P((u_char *, int));
106
107 struct protent ipcp_protent = {
108     PPP_IPCP,
109     ipcp_init,
110     ipcp_input,
111     ipcp_protrej,
112     ipcp_lowerup,
113     ipcp_lowerdown,
114     ipcp_open,
115     ipcp_close,
116     ipcp_printpkt,
117     NULL,
118     1,
119     "IPCP",
120     ip_check_options,
121     ip_demand_conf,
122     ip_active_pkt
123 };
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_COMPRESSTYPE:
927             IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
928             if (!ao->neg_vj ||
929                 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
930                 orc = CONFREJ;
931                 break;
932             }
933             GETSHORT(cishort, p);
934             IPCPDEBUG((LOG_INFO, "(%d)", cishort));
935
936             if (!(cishort == IPCP_VJ_COMP ||
937                   (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
938                 orc = CONFREJ;
939                 break;
940             }
941
942             ho->neg_vj = 1;
943             ho->vj_protocol = cishort;
944             if (cilen == CILEN_VJ) {
945                 GETCHAR(maxslotindex, p);
946                 if (maxslotindex > ao->maxslotindex) { 
947                     orc = CONFNAK;
948                     if (!reject_if_disagree){
949                         DECPTR(1, p);
950                         PUTCHAR(ao->maxslotindex, p);
951                     }
952                 }
953                 GETCHAR(cflag, p);
954                 if (cflag && !ao->cflag) {
955                     orc = CONFNAK;
956                     if (!reject_if_disagree){
957                         DECPTR(1, p);
958                         PUTCHAR(wo->cflag, p);
959                     }
960                 }
961                 ho->maxslotindex = maxslotindex;
962                 ho->cflag = cflag;
963             } else {
964                 ho->old_vj = 1;
965                 ho->maxslotindex = MAX_STATES - 1;
966                 ho->cflag = 1;
967             }
968             break;
969
970         default:
971             orc = CONFREJ;
972             break;
973         }
974
975 endswitch:
976         IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
977
978         if (orc == CONFACK &&           /* Good CI */
979             rc != CONFACK)              /*  but prior CI wasnt? */
980             continue;                   /* Don't send this one */
981
982         if (orc == CONFNAK) {           /* Nak this CI? */
983             if (reject_if_disagree)     /* Getting fed up with sending NAKs? */
984                 orc = CONFREJ;          /* Get tough if so */
985             else {
986                 if (rc == CONFREJ)      /* Rejecting prior CI? */
987                     continue;           /* Don't send this one */
988                 if (rc == CONFACK) {    /* Ack'd all prior CIs? */
989                     rc = CONFNAK;       /* Not anymore... */
990                     ucp = inp;          /* Backup */
991                 }
992             }
993         }
994
995         if (orc == CONFREJ &&           /* Reject this CI */
996             rc != CONFREJ) {            /*  but no prior ones? */
997             rc = CONFREJ;
998             ucp = inp;                  /* Backup */
999         }
1000
1001         /* Need to move CI? */
1002         if (ucp != cip)
1003             BCOPY(cip, ucp, cilen);     /* Move it */
1004
1005         /* Update output pointer */
1006         INCPTR(cilen, ucp);
1007     }
1008
1009     /*
1010      * If we aren't rejecting this packet, and we want to negotiate
1011      * their address, and they didn't send their address, then we
1012      * send a NAK with a CI_ADDR option appended.  We assume the
1013      * input buffer is long enough that we can append the extra
1014      * option safely.
1015      */
1016     if (rc != CONFREJ && !ho->neg_addr &&
1017         wo->req_addr && !reject_if_disagree) {
1018         if (rc == CONFACK) {
1019             rc = CONFNAK;
1020             ucp = inp;                  /* reset pointer */
1021             wo->req_addr = 0;           /* don't ask again */
1022         }
1023         PUTCHAR(CI_ADDR, ucp);
1024         PUTCHAR(CILEN_ADDR, ucp);
1025         tl = ntohl(wo->hisaddr);
1026         PUTLONG(tl, ucp);
1027     }
1028
1029     *len = ucp - inp;                   /* Compute output length */
1030     IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1031     return (rc);                        /* Return final code */
1032 }
1033
1034
1035 /*
1036  * ip_check_options - check that any IP-related options are OK,
1037  * and assign appropriate defaults.
1038  */
1039 static void
1040 ip_check_options()
1041 {
1042     struct hostent *hp;
1043     u_int32_t local;
1044     ipcp_options *wo = &ipcp_wantoptions[0];
1045
1046     /*
1047      * Default our local IP address based on our hostname.
1048      * If local IP address already given, don't bother.
1049      */
1050     if (wo->ouraddr == 0 && !disable_defaultip) {
1051         /*
1052          * Look up our hostname (possibly with domain name appended)
1053          * and take the first IP address as our local IP address.
1054          * If there isn't an IP address for our hostname, too bad.
1055          */
1056         wo->accept_local = 1;   /* don't insist on this default value */
1057         if ((hp = gethostbyname(hostname)) != NULL) {
1058             local = *(u_int32_t *)hp->h_addr;
1059             if (local != 0 && !bad_ip_adrs(local))
1060                 wo->ouraddr = local;
1061         }
1062     }
1063
1064     if (demand && wo->hisaddr == 0) {
1065         option_error("remote IP address required for demand-dialling\n");
1066         exit(1);
1067     }
1068     if (demand && wo->accept_remote) {
1069         option_error("ipcp-accept-remote is incompatible with demand\n");
1070         exit(1);
1071     }
1072 }
1073
1074
1075 /*
1076  * ip_demand_conf - configure the interface as though
1077  * IPCP were up, for use with dial-on-demand.
1078  */
1079 static int
1080 ip_demand_conf(u)
1081     int u;
1082 {
1083     ipcp_options *wo = &ipcp_wantoptions[u];
1084
1085     if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1086         return 0;
1087     if (!sifup(u))
1088         return 0;
1089     if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1090         return 0;
1091     if (wo->default_route)
1092         if (sifdefaultroute(u, wo->hisaddr))
1093             default_route_set[u] = 1;
1094     if (wo->proxy_arp)
1095         if (sifproxyarp(u, wo->hisaddr))
1096             proxy_arp_set[u] = 1;
1097
1098     syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(wo->ouraddr));
1099     syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1100
1101     return 1;
1102 }
1103
1104
1105 /*
1106  * ipcp_up - IPCP has come UP.
1107  *
1108  * Configure the IP network interface appropriately and bring it up.
1109  */
1110 static void
1111 ipcp_up(f)
1112     fsm *f;
1113 {
1114     u_int32_t mask;
1115     ipcp_options *ho = &ipcp_hisoptions[f->unit];
1116     ipcp_options *go = &ipcp_gotoptions[f->unit];
1117     ipcp_options *wo = &ipcp_wantoptions[f->unit];
1118
1119     np_up(f->unit, PPP_IP);
1120     IPCPDEBUG((LOG_INFO, "ipcp: up"));
1121
1122     /*
1123      * We must have a non-zero IP address for both ends of the link.
1124      */
1125     if (!ho->neg_addr)
1126         ho->hisaddr = wo->hisaddr;
1127
1128     if (ho->hisaddr == 0) {
1129         syslog(LOG_ERR, "Could not determine remote IP address");
1130         ipcp_close(f->unit, "Could not determine remote IP address");
1131         return;
1132     }
1133     if (go->ouraddr == 0) {
1134         syslog(LOG_ERR, "Could not determine local IP address");
1135         ipcp_close(f->unit, "Could not determine local IP address");
1136         return;
1137     }
1138
1139     /*
1140      * Check that the peer is allowed to use the IP address it wants.
1141      */
1142     if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1143         syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1144                ip_ntoa(ho->hisaddr));
1145         ipcp_close(f->unit, "Unauthorized remote IP address");
1146         return;
1147     }
1148
1149     /* set tcp compression */
1150     sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1151
1152     /*
1153      * If we are doing dial-on-demand, the interface is already
1154      * configured, so we put out any saved-up packets, then set the
1155      * interface to pass IP packets.
1156      */
1157     if (demand) {
1158         if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1159             syslog(LOG_ERR, "Failed to negotiate desired IP addresses");
1160             ipcp_close(f->unit, "Wrong IP addresses");
1161             return;
1162         }
1163         demand_rexmit(PPP_IP);
1164         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1165     }
1166 #ifndef _linux_  /* Linux destroys routes when the device goes down. */
1167     else         /* always use the code which adds the routes. */
1168 #endif
1169     {
1170         /*
1171          * Set IP addresses and (if specified) netmask.
1172          */
1173         mask = GetMask(go->ouraddr);
1174         if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1175             IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1176             ipcp_close(f->unit, "Interface configuration failed");
1177             return;
1178         }
1179
1180         /* bring the interface up for IP */
1181         if (!sifup(f->unit)) {
1182             IPCPDEBUG((LOG_WARNING, "sifup failed"));
1183             ipcp_close(f->unit, "Interface configuration failed");
1184             return;
1185         }
1186         sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1187
1188         /* assign a default route through the interface if required */
1189         if (ipcp_wantoptions[f->unit].default_route) 
1190             if (sifdefaultroute(f->unit, ho->hisaddr))
1191                 default_route_set[f->unit] = 1;
1192
1193         /* Make a proxy ARP entry if requested. */
1194         if (ipcp_wantoptions[f->unit].proxy_arp)
1195             if (sifproxyarp(f->unit, ho->hisaddr))
1196                 proxy_arp_set[f->unit] = 1;
1197
1198         syslog(LOG_NOTICE, "local  IP address %s", ip_ntoa(go->ouraddr));
1199         syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1200     }
1201
1202     /*
1203      * Execute the ip-up script, like this:
1204      *  /etc/ppp/ip-up interface tty speed local-IP remote-IP
1205      */
1206     ipcp_script(f, _PATH_IPUP);
1207
1208 }
1209
1210
1211 /*
1212  * ipcp_down - IPCP has gone DOWN.
1213  *
1214  * Take the IP network interface down, clear its addresses
1215  * and delete routes through it.
1216  */
1217 static void
1218 ipcp_down(f)
1219     fsm *f;
1220 {
1221     u_int32_t ouraddr, hisaddr;
1222
1223     IPCPDEBUG((LOG_INFO, "ipcp: down"));
1224     np_down(f->unit, PPP_IP);
1225     sifvjcomp(f->unit, 0, 0, 0);
1226
1227     /*
1228      * If we are doing dial-on-demand, set the interface
1229      * to queue up outgoing packets (for now).
1230      */
1231     if (demand) {
1232         sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1233
1234     } else {
1235         ouraddr = ipcp_gotoptions[f->unit].ouraddr;
1236         hisaddr = ipcp_hisoptions[f->unit].hisaddr;
1237         if (proxy_arp_set[f->unit]) {
1238             cifproxyarp(f->unit, hisaddr);
1239             proxy_arp_set[f->unit] = 0;
1240         }
1241         if (default_route_set[f->unit]) {
1242             cifdefaultroute(f->unit, hisaddr);
1243             default_route_set[f->unit] = 0;
1244         }
1245         sifdown(f->unit);
1246         cifaddr(f->unit, ouraddr, hisaddr);
1247     }
1248
1249     /* Execute the ip-down script */
1250     ipcp_script(f, _PATH_IPDOWN);
1251 }
1252
1253
1254 /*
1255  * ipcp_finished - possibly shut down the lower layers.
1256  */
1257 static void
1258 ipcp_finished(f)
1259     fsm *f;
1260 {
1261     np_finished(f->unit, PPP_IP);
1262 }
1263
1264
1265 /*
1266  * ipcp_script - Execute a script with arguments
1267  * interface-name tty-name speed local-IP remote-IP.
1268  */
1269 static void
1270 ipcp_script(f, script)
1271     fsm *f;
1272     char *script;
1273 {
1274     char strspeed[32], strlocal[32], strremote[32];
1275     char *argv[8];
1276
1277     sprintf(strspeed, "%d", baud_rate);
1278     strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1279     strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1280
1281     argv[0] = script;
1282     argv[1] = ifname;
1283     argv[2] = devnam;
1284     argv[3] = strspeed;
1285     argv[4] = strlocal;
1286     argv[5] = strremote;
1287     argv[6] = ipparam;
1288     argv[7] = NULL;
1289     run_program(script, argv, 0);
1290 }
1291
1292 /*
1293  * ipcp_printpkt - print the contents of an IPCP packet.
1294  */
1295 static char *ipcp_codenames[] = {
1296     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1297     "TermReq", "TermAck", "CodeRej"
1298 };
1299
1300 static int
1301 ipcp_printpkt(p, plen, printer, arg)
1302     u_char *p;
1303     int plen;
1304     void (*printer) __P((void *, char *, ...));
1305     void *arg;
1306 {
1307     int code, id, len, olen;
1308     u_char *pstart, *optend;
1309     u_short cishort;
1310     u_int32_t cilong;
1311
1312     if (plen < HEADERLEN)
1313         return 0;
1314     pstart = p;
1315     GETCHAR(code, p);
1316     GETCHAR(id, p);
1317     GETSHORT(len, p);
1318     if (len < HEADERLEN || len > plen)
1319         return 0;
1320
1321     if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1322         printer(arg, " %s", ipcp_codenames[code-1]);
1323     else
1324         printer(arg, " code=0x%x", code);
1325     printer(arg, " id=0x%x", id);
1326     len -= HEADERLEN;
1327     switch (code) {
1328     case CONFREQ:
1329     case CONFACK:
1330     case CONFNAK:
1331     case CONFREJ:
1332         /* print option list */
1333         while (len >= 2) {
1334             GETCHAR(code, p);
1335             GETCHAR(olen, p);
1336             p -= 2;
1337             if (olen < 2 || olen > len) {
1338                 break;
1339             }
1340             printer(arg, " <");
1341             len -= olen;
1342             optend = p + olen;
1343             switch (code) {
1344             case CI_ADDRS:
1345                 if (olen == CILEN_ADDRS) {
1346                     p += 2;
1347                     GETLONG(cilong, p);
1348                     printer(arg, "addrs %s", ip_ntoa(htonl(cilong)));
1349                     GETLONG(cilong, p);
1350                     printer(arg, " %s", ip_ntoa(htonl(cilong)));
1351                 }
1352                 break;
1353             case CI_COMPRESSTYPE:
1354                 if (olen >= CILEN_COMPRESS) {
1355                     p += 2;
1356                     GETSHORT(cishort, p);
1357                     printer(arg, "compress ");
1358                     switch (cishort) {
1359                     case IPCP_VJ_COMP:
1360                         printer(arg, "VJ");
1361                         break;
1362                     case IPCP_VJ_COMP_OLD:
1363                         printer(arg, "old-VJ");
1364                         break;
1365                     default:
1366                         printer(arg, "0x%x", cishort);
1367                     }
1368                 }
1369                 break;
1370             case CI_ADDR:
1371                 if (olen == CILEN_ADDR) {
1372                     p += 2;
1373                     GETLONG(cilong, p);
1374                     printer(arg, "addr %s", ip_ntoa(htonl(cilong)));
1375                 }
1376                 break;
1377             }
1378             while (p < optend) {
1379                 GETCHAR(code, p);
1380                 printer(arg, " %.2x", code);
1381             }
1382             printer(arg, ">");
1383         }
1384         break;
1385
1386     case TERMACK:
1387     case TERMREQ:
1388         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1389             printer(arg, " ");
1390             print_string(p, len, printer, arg);
1391             p += len;
1392             len = 0;
1393         }
1394         break;
1395     }
1396
1397     /* print the rest of the bytes in the packet */
1398     for (; len > 0; --len) {
1399         GETCHAR(code, p);
1400         printer(arg, " %.2x", code);
1401     }
1402
1403     return p - pstart;
1404 }
1405
1406 /*
1407  * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1408  * We don't bring the link up for IP fragments or for TCP FIN packets
1409  * with no data.
1410  */
1411 #ifndef IP_OFFMASK
1412 #define IP_OFFMASK      0x1fff
1413 #endif
1414
1415 /*
1416  * We use these macros because the IP header may be at an odd address,
1417  * and some compilers might use word loads to get th_off or ip_hl.
1418  */
1419
1420 #define net_short(x)    (((x)[0] << 8) + (x)[1])
1421 #define get_iphl(x)     (((unsigned char *)(x))[0] & 0xF)
1422 #define get_ipoff(x)    net_short((unsigned char *)(x) + 6)
1423 #define get_tcpoff(x)   (((unsigned char *)(x))[12] >> 4)
1424
1425 static int
1426 ip_active_pkt(pkt, len)
1427     u_char *pkt;
1428     int len;
1429 {
1430     struct ip *ip;
1431     struct tcphdr *tcp;
1432     int hlen;
1433
1434     len -= PPP_HDRLEN;
1435     if (len < sizeof(struct ip) + PPP_HDRLEN)
1436         return 0;
1437     pkt += PPP_HDRLEN;
1438     ip = (struct ip *) pkt;
1439     if ((get_ipoff(ip) & IP_OFFMASK) != 0)
1440         return 0;
1441     if (ip->ip_p != IPPROTO_TCP)
1442         return 1;
1443     hlen = get_iphl(ip) * 4;
1444     if (len < hlen + sizeof(struct tcphdr))
1445         return 0;
1446     tcp = (struct tcphdr *) (pkt + hlen);
1447     if ((tcp->th_flags & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)
1448         return 0;
1449     return 1;
1450 }