Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / ppp / ip.c
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.sbin/ppp/ip.c,v 1.78.2.11 2002/09/01 02:12:27 brian Exp $
29  * $DragonFly: src/usr.sbin/ppp/ip.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #ifndef NOINET6
38 #include <netinet/icmp6.h>
39 #include <netinet/ip6.h>
40 #endif
41 #include <netinet/ip_icmp.h>
42 #include <netinet/udp.h>
43 #include <netinet/tcp.h>
44 #include <sys/un.h>
45
46 #include <errno.h>
47 #include <netdb.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <termios.h>
51 #include <unistd.h>
52
53 #include "layer.h"
54 #include "proto.h"
55 #include "mbuf.h"
56 #include "log.h"
57 #include "defs.h"
58 #include "timer.h"
59 #include "fsm.h"
60 #include "lqr.h"
61 #include "hdlc.h"
62 #include "throughput.h"
63 #include "iplist.h"
64 #include "slcompress.h"
65 #include "ncpaddr.h"
66 #include "ip.h"
67 #include "ipcp.h"
68 #include "filter.h"
69 #include "descriptor.h"
70 #include "lcp.h"
71 #include "ccp.h"
72 #include "link.h"
73 #include "mp.h"
74 #ifndef NORADIUS
75 #include "radius.h"
76 #endif
77 #include "ipv6cp.h"
78 #include "ncp.h"
79 #include "bundle.h"
80 #include "tun.h"
81
82
83 #define OPCODE_QUERY    0
84 #define OPCODE_IQUERY   1
85 #define OPCODE_STATUS   2
86
87 struct dns_header {
88   u_short id;
89   unsigned qr : 1;
90   unsigned opcode : 4;
91   unsigned aa : 1;
92   unsigned tc : 1;
93   unsigned rd : 1;
94   unsigned ra : 1;
95   unsigned z : 3;
96   unsigned rcode : 4;
97   u_short qdcount;
98   u_short ancount;
99   u_short nscount;
100   u_short arcount;
101 };
102
103 static const char *
104 dns_Qclass2Txt(u_short qclass)
105 {
106   static char failure[6];
107   struct {
108     u_short id;
109     const char *txt;
110   } qtxt[] = {
111     /* rfc1035 */
112     { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" }
113   };
114   int f;
115
116   for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
117     if (qtxt[f].id == qclass)
118       return qtxt[f].txt;
119
120   return HexStr(qclass, failure, sizeof failure);
121 }
122
123 static const char *
124 dns_Qtype2Txt(u_short qtype)
125 {
126   static char failure[6];
127   struct {
128     u_short id;
129     const char *txt;
130   } qtxt[] = {
131     /* rfc1035/rfc1700 */
132     { 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" },
133     { 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" },
134     { 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" },
135     { 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" },
136     { 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" },
137     { 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" },
138     { 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" },
139     { 254, "MAILA" }, { 255, "*" }
140   };
141   int f;
142
143   for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
144     if (qtxt[f].id == qtype)
145       return qtxt[f].txt;
146
147   return HexStr(qtype, failure, sizeof failure);
148 }
149
150 static __inline int
151 PortMatch(int op, u_short pport, u_short rport)
152 {
153   switch (op) {
154   case OP_EQ:
155     return pport == rport;
156   case OP_GT:
157     return pport > rport;
158   case OP_LT:
159     return pport < rport;
160   default:
161     return 0;
162   }
163 }
164
165 /*
166  * Check a packet against the given filter
167  * Returns 0 to accept the packet, non-zero to drop the packet.
168  * If psecs is not NULL, populate it with the timeout associated
169  * with the filter rule matched.
170  *
171  * If filtering is enabled, the initial fragment of a datagram must
172  * contain the complete protocol header, and subsequent fragments
173  * must not attempt to over-write it.
174  *
175  * One (and only one) of pip or pip6 must be set.
176  */
177 int
178 FilterCheck(const unsigned char *packet, u_int32_t family,
179             const struct filter *filter, unsigned *psecs)
180 {
181   int gotinfo;                  /* true if IP payload decoded */
182   int cproto;                   /* IPPROTO_* protocol number if (gotinfo) */
183   int estab, syn, finrst;       /* TCP state flags if (gotinfo) */
184   u_short sport, dport;         /* src, dest port from packet if (gotinfo) */
185   int n;                        /* filter rule to process */
186   int len;                      /* bytes used in dbuff */
187   int didname;                  /* true if filter header printed */
188   int match;                    /* true if condition matched */
189   int mindata;                  /* minimum data size or zero */
190   const struct filterent *fp = filter->rule;
191   char dbuff[100], dstip[16], prototxt[16];
192   struct protoent *pe;
193   struct ncpaddr srcaddr, dstaddr;
194   const char *payload;          /* IP payload */
195   int datalen;                  /* IP datagram length */
196
197   if (fp->f_action == A_NONE)
198     return 0;           /* No rule is given. Permit this packet */
199
200 #ifndef NOINET6
201   if (family == AF_INET6) {
202     const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
203
204     ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
205     ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
206     datalen = ntohs(pip6->ip6_plen);
207     payload = packet + sizeof *pip6;
208     cproto = pip6->ip6_nxt;
209   } else
210 #endif
211   {
212     /*
213      * Deny any packet fragment that tries to over-write the header.
214      * Since we no longer have the real header available, punt on the
215      * largest normal header - 20 bytes for TCP without options, rounded
216      * up to the next possible fragment boundary.  Since the smallest
217      * `legal' MTU is 576, and the smallest recommended MTU is 296, any
218      * fragmentation within this range is dubious at best
219      */
220     const struct ip *pip = (const struct ip *)packet;
221
222     len = ntohs(pip->ip_off) & IP_OFFMASK;      /* fragment offset */
223     if (len > 0) {              /* Not first fragment within datagram */
224       if (len < (24 >> 3)) {    /* don't allow fragment to over-write header */
225         log_Printf(LogFILTER, " error: illegal header\n");
226         return 1;
227       }
228       /* permit fragments on in and out filter */
229       if (!filter->fragok) {
230         log_Printf(LogFILTER, " error: illegal fragmentation\n");
231         return 1;
232       } else
233         return 0;
234     }
235
236     ncpaddr_setip4(&srcaddr, pip->ip_src);
237     ncpaddr_setip4(&dstaddr, pip->ip_dst);
238     datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
239     payload = packet + (pip->ip_hl << 2);
240     cproto = pip->ip_p;
241   }
242
243   if ((pe = getprotobynumber(cproto)) == NULL)
244     snprintf(prototxt, sizeof prototxt, "%d", cproto);
245   else
246     snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
247   gotinfo = estab = syn = finrst = didname = 0;
248   sport = dport = 0;
249
250   for (n = 0; n < MAXFILTERS; ) {
251     if (fp->f_action == A_NONE) {
252       n++;
253       fp++;
254       continue;
255     }
256
257     if (!didname) {
258       log_Printf(LogDEBUG, "%s filter:\n", filter->name);
259       didname = 1;
260     }
261
262     match = 0;
263
264     if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
265          ncprange_contains(&fp->f_src, &srcaddr)) &&
266         (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
267          ncprange_contains(&fp->f_dst, &dstaddr))) {
268       if (fp->f_proto != 0) {
269         if (!gotinfo) {
270           const struct tcphdr *th;
271           const struct udphdr *uh;
272           const struct icmp *ih;
273 #ifndef NOINET6
274           const struct icmp6_hdr *ih6;
275 #endif
276           mindata = 0;
277           sport = dport = 0;
278           estab = syn = finrst = -1;
279
280           switch (cproto) {
281           case IPPROTO_ICMP:
282             mindata = 8;        /* ICMP must be at least 8 octets */
283             ih = (const struct icmp *)payload;
284             sport = ntohs(ih->icmp_type);
285             if (log_IsKept(LogDEBUG))
286               snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
287             break;
288
289 #ifndef NOINET6
290           case IPPROTO_ICMPV6:
291             mindata = 8;        /* ICMP must be at least 8 octets */
292             ih6 = (const struct icmp6_hdr *)payload;
293             sport = ntohs(ih6->icmp6_type);
294             if (log_IsKept(LogDEBUG))
295               snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
296             break;
297 #endif
298
299           case IPPROTO_IGMP:
300             mindata = 8;        /* IGMP uses 8-octet messages */
301             break;
302
303 #ifdef IPPROTO_GRE
304           case IPPROTO_GRE:
305             mindata = 2;        /* GRE uses 2-octet+ messages */
306             break;
307 #endif
308 #ifdef IPPROTO_OSPFIGP
309           case IPPROTO_OSPFIGP:
310             mindata = 8;        /* IGMP uses 8-octet messages */
311             break;
312 #endif
313 #ifndef NOINET6
314           case IPPROTO_IPV6:
315             mindata = 20;       /* RFC2893 Section 3.5: 5 * 32bit words */
316             break;
317 #endif
318
319           case IPPROTO_UDP:
320             mindata = 8;        /* UDP header is 8 octets */
321             uh = (const struct udphdr *)payload;
322             sport = ntohs(uh->uh_sport);
323             dport = ntohs(uh->uh_dport);
324             if (log_IsKept(LogDEBUG))
325               snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
326                        sport, dport);
327             break;
328
329           case IPPROTO_TCP:
330             th = (const struct tcphdr *)payload;
331             /*
332              * TCP headers are variable length.  The following code
333              * ensures that the TCP header length isn't de-referenced if
334              * the datagram is too short
335              */
336             if (datalen < 20 || datalen < (th->th_off << 2)) {
337               log_Printf(LogFILTER, " error: TCP header incorrect\n");
338               return 1;
339             }
340             sport = ntohs(th->th_sport);
341             dport = ntohs(th->th_dport);
342             estab = (th->th_flags & TH_ACK);
343             syn = (th->th_flags & TH_SYN);
344             finrst = (th->th_flags & (TH_FIN|TH_RST));
345             if (log_IsKept(LogDEBUG)) {
346               if (!estab)
347                 snprintf(dbuff, sizeof dbuff,
348                          "flags = %02x, sport = %d, dport = %d",
349                          th->th_flags, sport, dport);
350               else
351                 *dbuff = '\0';
352             }
353             break;
354           default:
355             break;
356           }
357
358           if (datalen < mindata) {
359             log_Printf(LogFILTER, " error: proto %s must be at least"
360                        " %d octets\n", prototxt, mindata);
361             return 1;
362           }
363
364           if (log_IsKept(LogDEBUG)) {
365             if (estab != -1) {
366               len = strlen(dbuff);
367               snprintf(dbuff + len, sizeof dbuff - len,
368                        ", estab = %d, syn = %d, finrst = %d",
369                        estab, syn, finrst);
370             }
371             log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff);
372           }
373           gotinfo = 1;
374         }
375
376         if (log_IsKept(LogDEBUG)) {
377           if (fp->f_srcop != OP_NONE) {
378             snprintf(dbuff, sizeof dbuff, ", src %s %d",
379                      filter_Op2Nam(fp->f_srcop), fp->f_srcport);
380             len = strlen(dbuff);
381           } else
382             len = 0;
383           if (fp->f_dstop != OP_NONE) {
384             snprintf(dbuff + len, sizeof dbuff - len,
385                      ", dst %s %d", filter_Op2Nam(fp->f_dstop),
386                      fp->f_dstport);
387           } else if (!len)
388             *dbuff = '\0';
389
390           log_Printf(LogDEBUG, "  rule = %d: Address match, "
391                      "check against proto %d%s, action = %s\n",
392                      n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
393         }
394
395         if (cproto == fp->f_proto) {
396           if ((fp->f_srcop == OP_NONE ||
397                PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
398               (fp->f_dstop == OP_NONE ||
399                PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
400               (fp->f_estab == 0 || estab) &&
401               (fp->f_syn == 0 || syn) &&
402               (fp->f_finrst == 0 || finrst)) {
403             match = 1;
404           }
405         }
406       } else {
407         /* Address is matched and no protocol specified. Make a decision. */
408         log_Printf(LogDEBUG, "  rule = %d: Address match, action = %s\n", n,
409                    filter_Action2Nam(fp->f_action));
410         match = 1;
411       }
412     } else
413       log_Printf(LogDEBUG, "  rule = %d: Address mismatch\n", n);
414
415     if (match != fp->f_invert) {
416       /* Take specified action */
417       if (fp->f_action < A_NONE)
418         fp = &filter->rule[n = fp->f_action];
419       else {
420         if (fp->f_action == A_PERMIT) {
421           if (psecs != NULL)
422             *psecs = fp->timeout;
423           if (strcmp(filter->name, "DIAL") == 0) {
424             /* If dial filter then even print out accept packets */
425             if (log_IsKept(LogFILTER)) {
426               snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
427               log_Printf(LogFILTER, "%sbound rule = %d accept %s "
428                          "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt,
429                          ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
430             }
431           }
432           return 0;
433         } else {
434           if (log_IsKept(LogFILTER)) {
435             snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
436             log_Printf(LogFILTER,
437                        "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
438                        filter->name, n, prototxt,
439                        ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
440           }
441           return 1;
442         }               /* Explict match.  Deny this packet */
443       }
444     } else {
445       n++;
446       fp++;
447     }
448   }
449
450   if (log_IsKept(LogFILTER)) {
451     snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
452     log_Printf(LogFILTER,
453                "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
454                filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport,
455                dstip, dport);
456   }
457
458   return 1;             /* No rule matched, deny this packet */
459 }
460
461 static void
462 ip_LogDNS(const struct udphdr *uh, const char *direction)
463 {
464   struct dns_header header;
465   const u_short *pktptr;
466   const u_char *ptr;
467   u_short *hptr, tmp;
468   int len;
469
470   ptr = (const char *)uh + sizeof *uh;
471   len = ntohs(uh->uh_ulen) - sizeof *uh;
472   if (len < sizeof header + 5)          /* rfc1024 */
473     return;
474
475   pktptr = (const u_short *)ptr;
476   hptr = (u_short *)&header;
477   ptr += sizeof header;
478   len -= sizeof header;
479
480   while (pktptr < (const u_short *)ptr) {
481     *hptr++ = ntohs(*pktptr);           /* Careful of macro side-effects ! */
482     pktptr++;
483   }
484
485   if (header.opcode == OPCODE_QUERY && header.qr == 0) {
486     /* rfc1035 */
487     char namewithdot[MAXHOSTNAMELEN + 1], *n;
488     const char *qtype, *qclass;
489     const u_char *end;
490
491     n = namewithdot;
492     end = ptr + len - 4;
493     if (end - ptr >= sizeof namewithdot)
494       end = ptr + sizeof namewithdot - 1;
495     while (ptr < end) {
496       len = *ptr++;
497       if (len > end - ptr)
498         len = end - ptr;
499       if (n != namewithdot)
500         *n++ = '.';
501       memcpy(n, ptr, len);
502       ptr += len;
503       n += len;
504     }
505     *n = '\0';
506
507     if (log_IsKept(LogDNS)) {
508       memcpy(&tmp, end, sizeof tmp);
509       qtype = dns_Qtype2Txt(ntohs(tmp));
510       memcpy(&tmp, end + 2, sizeof tmp);
511       qclass = dns_Qclass2Txt(ntohs(tmp));
512
513       log_Printf(LogDNS, "%sbound query %s %s %s\n",
514                  direction, qclass, qtype, namewithdot);
515     }
516   }
517 }
518
519 /*
520  * Check if the given packet matches the given filter.
521  * One of pip or pip6 must be set.
522  */
523 int
524 PacketCheck(struct bundle *bundle, u_int32_t family,
525             const unsigned char *packet, int nb, struct filter *filter,
526             const char *prefix, unsigned *psecs)
527 {
528   static const char *const TcpFlags[] = {
529     "FIN", "SYN", "RST", "PSH", "ACK", "URG"
530   };
531   const struct tcphdr *th;
532   const struct udphdr *uh;
533   const struct icmp *icmph;
534 #ifndef NOINET6
535   const struct icmp6_hdr *icmp6h;
536 #endif
537   const unsigned char *payload;
538   struct ncpaddr srcaddr, dstaddr;
539   int cproto, mask, len, n, pri, logit, loglen, result;
540   char logbuf[200];
541   int datalen, frag;
542   u_char tos;
543
544   logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
545           (!filter || filter->logok);
546   loglen = 0;
547   pri = 0;
548
549 #ifndef NOINET6
550   if (family == AF_INET6) {
551     const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
552
553     ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
554     ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
555     datalen = ntohs(pip6->ip6_plen);
556     payload = packet + sizeof *pip6;
557     cproto = pip6->ip6_nxt;
558     tos = 0;                                    /* XXX: pip6->ip6_vfc >> 4 ? */
559     frag = 0;                                   /* XXX: ??? */
560   } else
561 #endif
562   {
563     const struct ip *pip = (const struct ip *)packet;
564
565     ncpaddr_setip4(&srcaddr, pip->ip_src);
566     ncpaddr_setip4(&dstaddr, pip->ip_dst);
567     datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
568     payload = packet + (pip->ip_hl << 2);
569     cproto = pip->ip_p;
570     tos = pip->ip_tos;
571     frag = ntohs(pip->ip_off) & IP_OFFMASK;
572   }
573
574   uh = NULL;
575
576   if (logit && loglen < sizeof logbuf) {
577     if (prefix)
578       snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
579     else if (filter)
580       snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
581     else
582       snprintf(logbuf + loglen, sizeof logbuf - loglen, "  ");
583     loglen += strlen(logbuf + loglen);
584   }
585
586   switch (cproto) {
587   case IPPROTO_ICMP:
588     if (logit && loglen < sizeof logbuf) {
589       len = datalen - sizeof *icmph;
590       icmph = (const struct icmp *)payload;
591       snprintf(logbuf + loglen, sizeof logbuf - loglen,
592                "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
593       loglen += strlen(logbuf + loglen);
594       snprintf(logbuf + loglen, sizeof logbuf - loglen,
595                "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
596       loglen += strlen(logbuf + loglen);
597     }
598     break;
599
600 #ifndef NOINET6
601   case IPPROTO_ICMPV6:
602     if (logit && loglen < sizeof logbuf) {
603       len = datalen - sizeof *icmp6h;
604       icmp6h = (const struct icmp6_hdr *)payload;
605       snprintf(logbuf + loglen, sizeof logbuf - loglen,
606                "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
607       loglen += strlen(logbuf + loglen);
608       snprintf(logbuf + loglen, sizeof logbuf - loglen,
609                "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
610       loglen += strlen(logbuf + loglen);
611     }
612     break;
613 #endif
614
615   case IPPROTO_UDP:
616     uh = (const struct udphdr *)payload;
617     if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
618       pri++;
619
620     if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
621                                      ntohs(uh->uh_dport)))
622       pri++;
623
624     if (logit && loglen < sizeof logbuf) {
625       len = datalen - sizeof *uh;
626       snprintf(logbuf + loglen, sizeof logbuf - loglen,
627                "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
628       loglen += strlen(logbuf + loglen);
629       snprintf(logbuf + loglen, sizeof logbuf - loglen,
630                "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
631                len, nb);
632       loglen += strlen(logbuf + loglen);
633     }
634
635     if (Enabled(bundle, OPT_FILTERDECAP) &&
636         payload[sizeof *uh] == HDLC_ADDR &&
637         payload[sizeof *uh + 1] == HDLC_UI) {
638       u_short proto;
639       const char *type;
640
641       memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
642       type = NULL;
643
644       switch (ntohs(proto)) {
645         case PROTO_IP:
646           snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
647           result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
648                                nb - (payload - packet) - sizeof *uh - 4, filter,
649                                logbuf, psecs);
650           if (result != -2)
651               return result;
652           type = "IP";
653           break;
654
655         case PROTO_VJUNCOMP: type = "compressed VJ";   break;
656         case PROTO_VJCOMP:   type = "uncompressed VJ"; break;
657         case PROTO_MP:       type = "Multi-link"; break;
658         case PROTO_ICOMPD:   type = "Individual link CCP"; break;
659         case PROTO_COMPD:    type = "CCP"; break;
660         case PROTO_IPCP:     type = "IPCP"; break;
661         case PROTO_LCP:      type = "LCP"; break;
662         case PROTO_PAP:      type = "PAP"; break;
663         case PROTO_CBCP:     type = "CBCP"; break;
664         case PROTO_LQR:      type = "LQR"; break;
665         case PROTO_CHAP:     type = "CHAP"; break;
666       }
667       if (type) {
668         snprintf(logbuf + loglen, sizeof logbuf - loglen,
669                  " - %s data", type);
670         loglen += strlen(logbuf + loglen);
671       }
672     }
673
674     break;
675
676 #ifdef IPPROTO_GRE
677   case IPPROTO_GRE:
678     if (logit && loglen < sizeof logbuf) {
679       snprintf(logbuf + loglen, sizeof logbuf - loglen,
680           "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
681       loglen += strlen(logbuf + loglen);
682       snprintf(logbuf + loglen, sizeof logbuf - loglen,
683               "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
684       loglen += strlen(logbuf + loglen);
685     }
686     break;
687 #endif
688
689 #ifdef IPPROTO_OSPFIGP
690   case IPPROTO_OSPFIGP:
691     if (logit && loglen < sizeof logbuf) {
692       snprintf(logbuf + loglen, sizeof logbuf - loglen,
693                "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
694       loglen += strlen(logbuf + loglen);
695       snprintf(logbuf + loglen, sizeof logbuf - loglen,
696                "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
697       loglen += strlen(logbuf + loglen);
698     }
699     break;
700 #endif
701
702 #ifndef NOINET6
703   case IPPROTO_IPV6:
704     if (logit && loglen < sizeof logbuf) {
705       snprintf(logbuf + loglen, sizeof logbuf - loglen,
706                "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
707       loglen += strlen(logbuf + loglen);
708       snprintf(logbuf + loglen, sizeof logbuf - loglen,
709                "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
710       loglen += strlen(logbuf + loglen);
711     }
712
713     if (Enabled(bundle, OPT_FILTERDECAP)) {
714       snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
715       result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
716                            filter, logbuf, psecs);
717       if (result != -2)
718         return result;
719     }
720     break;
721 #endif
722
723   case IPPROTO_IPIP:
724     if (logit && loglen < sizeof logbuf) {
725       snprintf(logbuf + loglen, sizeof logbuf - loglen,
726                "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
727       loglen += strlen(logbuf + loglen);
728       snprintf(logbuf + loglen, sizeof logbuf - loglen,
729                "%s", ncpaddr_ntoa(&dstaddr));
730       loglen += strlen(logbuf + loglen);
731     }
732
733     if (Enabled(bundle, OPT_FILTERDECAP) &&
734         ((const struct ip *)payload)->ip_v == 4) {
735       snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
736       result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
737                            filter, logbuf, psecs);
738       if (result != -2)
739         return result;
740     }
741     break;
742
743   case IPPROTO_ESP:
744     if (logit && loglen < sizeof logbuf) {
745       snprintf(logbuf + loglen, sizeof logbuf - loglen,
746                "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
747       loglen += strlen(logbuf + loglen);
748       snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
749                ncpaddr_ntoa(&dstaddr), payload);
750       loglen += strlen(logbuf + loglen);
751     }
752     break;
753
754   case IPPROTO_AH:
755     if (logit && loglen < sizeof logbuf) {
756       snprintf(logbuf + loglen, sizeof logbuf - loglen,
757                "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
758       loglen += strlen(logbuf + loglen);
759       snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
760                ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
761       loglen += strlen(logbuf + loglen);
762     }
763     break;
764
765   case IPPROTO_IGMP:
766     if (logit && loglen < sizeof logbuf) {
767       uh = (const struct udphdr *)payload;
768       snprintf(logbuf + loglen, sizeof logbuf - loglen,
769                "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
770                ntohs(uh->uh_sport));
771       loglen += strlen(logbuf + loglen);
772       snprintf(logbuf + loglen, sizeof logbuf - loglen,
773                "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
774       loglen += strlen(logbuf + loglen);
775     }
776     break;
777
778   case IPPROTO_TCP:
779     th = (const struct tcphdr *)payload;
780     if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
781       pri++;
782
783     if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
784                                      ntohs(th->th_dport)))
785       pri++;
786
787     if (logit && loglen < sizeof logbuf) {
788       len = datalen - (th->th_off << 2);
789       snprintf(logbuf + loglen, sizeof logbuf - loglen,
790            "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
791       loglen += strlen(logbuf + loglen);
792       snprintf(logbuf + loglen, sizeof logbuf - loglen,
793                "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
794       loglen += strlen(logbuf + loglen);
795       n = 0;
796       for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
797         if (th->th_flags & mask) {
798           snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
799           loglen += strlen(logbuf + loglen);
800         }
801         n++;
802       }
803       snprintf(logbuf + loglen, sizeof logbuf - loglen,
804                "  seq:%lx  ack:%lx (%d/%d)",
805                (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
806       loglen += strlen(logbuf + loglen);
807       if ((th->th_flags & TH_SYN) && nb > 40) {
808         const u_short *sp;
809
810         sp = (const u_short *)(payload + 20);
811         if (ntohs(sp[0]) == 0x0204) {
812           snprintf(logbuf + loglen, sizeof logbuf - loglen,
813                    " MSS = %d", ntohs(sp[1]));
814           loglen += strlen(logbuf + loglen);
815         }
816       }
817     }
818     break;
819
820   default:
821     if (prefix)
822       return -2;
823
824     if (logit && loglen < sizeof logbuf) {
825       snprintf(logbuf + loglen, sizeof logbuf - loglen,
826                "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
827       loglen += strlen(logbuf + loglen);
828       snprintf(logbuf + loglen, sizeof logbuf - loglen,
829                "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
830       loglen += strlen(logbuf + loglen);
831     }
832     break;
833   }
834
835   if (filter && FilterCheck(packet, family, filter, psecs)) {
836     if (logit)
837       log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
838     result = -1;
839   } else {
840     /* Check Keep Alive filter */
841     if (logit && log_IsKept(LogTCPIP)) {
842       unsigned alivesecs;
843
844       alivesecs = 0;
845       if (filter &&
846           FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
847         log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
848       else if (psecs != NULL) {
849         if(*psecs == 0)
850           *psecs = alivesecs;
851         if (*psecs) {
852           if (*psecs != alivesecs)
853             log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
854                        logbuf, *psecs, alivesecs);
855           else
856             log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
857         } else
858           log_Printf(LogTCPIP, "%s\n", logbuf);
859       }
860     }
861     result = pri;
862   }
863
864   if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
865     ip_LogDNS(uh, filter->name);
866
867   return result;
868 }
869
870 static int
871 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
872 {
873   int nb, nw;
874   struct tun_data tun;
875   char *data;
876   unsigned secs, alivesecs;
877
878   nb = m_length(bp);
879   if (nb > sizeof tun.data) {
880     log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
881                l->name, nb, (int)(sizeof tun.data));
882     m_freem(bp);
883     return 0;
884   }
885   mbuf_Read(bp, tun.data, nb);
886
887   secs = 0;
888   if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
889                   NULL, &secs) < 0)
890     return 0;
891
892   alivesecs = 0;
893   if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
894     if (secs == 0)
895       secs = alivesecs;
896     bundle_StartIdleTimer(bundle, secs);
897   }
898
899   if (bundle->dev.header) {
900     tun.header.family = htonl(af);
901     nb += sizeof tun - sizeof tun.data;
902     data = (char *)&tun;
903   } else
904     data = tun.data;
905
906   nw = write(bundle->dev.fd, data, nb);
907   if (nw != nb) {
908     if (nw == -1)
909       log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
910                  l->name, nb, strerror(errno));
911     else
912       log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
913   }
914
915   return nb;
916 }
917
918 struct mbuf *
919 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
920 {
921   int nb;
922
923   if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
924     log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
925     m_freem(bp);
926     return NULL;
927   }
928
929   m_settype(bp, MB_IPIN);
930
931   nb = ip_Input(bundle, l, bp, AF_INET);
932   ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
933
934   return NULL;
935 }
936
937 #ifndef NOINET6
938 struct mbuf *
939 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
940 {
941   int nb;
942
943   if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
944     log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
945     m_freem(bp);
946     return NULL;
947   }
948
949   m_settype(bp, MB_IPV6IN);
950
951   nb = ip_Input(bundle, l, bp, AF_INET6);
952   ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);
953
954   return NULL;
955 }
956 #endif