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)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
37 #include <netinet/icmp6.h>
38 #include <netinet/ip6.h>
40 #include <netinet/ip_icmp.h>
41 #include <netinet/udp.h>
42 #include <netinet/tcp.h>
61 #include "throughput.h"
63 #include "slcompress.h"
68 #include "descriptor.h"
82 #define OPCODE_QUERY 0
83 #define OPCODE_IQUERY 1
84 #define OPCODE_STATUS 2
103 dns_Qclass2Txt(u_short qclass)
105 static char failure[6];
111 { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" }
115 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
116 if (qtxt[f].id == qclass)
119 return HexStr(qclass, failure, sizeof failure);
123 dns_Qtype2Txt(u_short qtype)
125 static char failure[6];
130 /* rfc1035/rfc1700 */
131 { 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" },
132 { 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" },
133 { 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" },
134 { 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" },
135 { 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" },
136 { 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" },
137 { 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" },
138 { 254, "MAILA" }, { 255, "*" }
142 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
143 if (qtxt[f].id == qtype)
146 return HexStr(qtype, failure, sizeof failure);
150 PortMatch(int op, u_short pport, u_short rport)
154 return pport == rport;
156 return pport > rport;
158 return pport < rport;
165 * Return a text string representing the cproto protocol number.
167 * The purpose of this routine is calculate this result, for
168 * the many times it is needed in FilterCheck, only on demand
169 * (i.e. when the corresponding logging functions are invoked).
171 * This optimization saves, over the previous implementation, which
172 * calculated prototxt at the beginning of FilterCheck, an
173 * open/read/close system call sequence per packet, approximately
174 * halving the ppp system overhead and reducing the overall (u + s)
177 * The caching performed here is just a side effect.
182 static int oproto = -1;
183 static char protobuff[16] = "-1";
186 if (cproto == oproto)
188 if ((pe = getprotobynumber(cproto)) == NULL)
189 snprintf(protobuff, sizeof protobuff, "%d", cproto);
191 snprintf(protobuff, sizeof protobuff, "%s", pe->p_name);
197 * Check a packet against the given filter
198 * Returns 0 to accept the packet, non-zero to drop the packet.
199 * If psecs is not NULL, populate it with the timeout associated
200 * with the filter rule matched.
202 * If filtering is enabled, the initial fragment of a datagram must
203 * contain the complete protocol header, and subsequent fragments
204 * must not attempt to over-write it.
206 * One (and only one) of pip or pip6 must be set.
209 FilterCheck(const unsigned char *packet,
211 u_int32_t family __unused,
215 const struct filter *filter, unsigned *psecs)
217 int gotinfo; /* true if IP payload decoded */
218 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
219 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
220 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
221 int n; /* filter rule to process */
222 int len; /* bytes used in dbuff */
223 int didname; /* true if filter header printed */
224 int match; /* true if condition matched */
225 int mindata; /* minimum data size or zero */
226 const struct filterent *fp = filter->rule;
227 char dbuff[100], dstip[16];
228 struct ncpaddr srcaddr, dstaddr;
229 const char *payload; /* IP payload */
230 int datalen; /* IP datagram length */
232 if (fp->f_action == A_NONE)
233 return 0; /* No rule is given. Permit this packet */
236 if (family == AF_INET6) {
237 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
239 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
240 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
241 datalen = ntohs(pip6->ip6_plen);
242 payload = packet + sizeof *pip6;
243 cproto = pip6->ip6_nxt;
248 * Deny any packet fragment that tries to over-write the header.
249 * Since we no longer have the real header available, punt on the
250 * largest normal header - 20 bytes for TCP without options, rounded
251 * up to the next possible fragment boundary. Since the smallest
252 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
253 * fragmentation within this range is dubious at best
255 const struct ip *pip = (const struct ip *)packet;
257 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
258 if (len > 0) { /* Not first fragment within datagram */
259 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
260 log_Printf(LogFILTER, " error: illegal header\n");
263 /* permit fragments on in and out filter */
264 if (!filter->fragok) {
265 log_Printf(LogFILTER, " error: illegal fragmentation\n");
271 ncpaddr_setip4(&srcaddr, pip->ip_src);
272 ncpaddr_setip4(&dstaddr, pip->ip_dst);
273 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
274 payload = packet + (pip->ip_hl << 2);
279 gotinfo = estab = syn = finrst = didname = 0;
282 for (n = 0; n < MAXFILTERS; ) {
283 if (fp->f_action == A_NONE) {
290 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
296 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
297 ncprange_contains(&fp->f_src, &srcaddr)) &&
298 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
299 ncprange_contains(&fp->f_dst, &dstaddr))) {
300 if (fp->f_proto != 0) {
302 const struct tcphdr *th;
303 const struct udphdr *uh;
304 const struct icmp *ih;
306 const struct icmp6_hdr *ih6;
310 estab = syn = finrst = -1;
314 mindata = 8; /* ICMP must be at least 8 octets */
315 ih = (const struct icmp *)payload;
316 sport = ih->icmp_type;
317 if (log_IsKept(LogDEBUG))
318 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
323 mindata = 8; /* ICMP must be at least 8 octets */
324 ih6 = (const struct icmp6_hdr *)payload;
325 sport = ih6->icmp6_type;
326 if (log_IsKept(LogDEBUG))
327 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
332 mindata = 8; /* IGMP uses 8-octet messages */
337 mindata = 2; /* GRE uses 2-octet+ messages */
340 #ifdef IPPROTO_OSPFIGP
341 case IPPROTO_OSPFIGP:
342 mindata = 8; /* IGMP uses 8-octet messages */
347 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
352 mindata = 8; /* UDP header is 8 octets */
353 uh = (const struct udphdr *)payload;
354 sport = ntohs(uh->uh_sport);
355 dport = ntohs(uh->uh_dport);
356 if (log_IsKept(LogDEBUG))
357 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
362 th = (const struct tcphdr *)payload;
364 * TCP headers are variable length. The following code
365 * ensures that the TCP header length isn't de-referenced if
366 * the datagram is too short
368 if (datalen < 20 || datalen < (th->th_off << 2)) {
369 log_Printf(LogFILTER, " error: TCP header incorrect\n");
372 sport = ntohs(th->th_sport);
373 dport = ntohs(th->th_dport);
374 estab = (th->th_flags & TH_ACK);
375 syn = (th->th_flags & TH_SYN);
376 finrst = (th->th_flags & (TH_FIN|TH_RST));
377 if (log_IsKept(LogDEBUG)) {
379 snprintf(dbuff, sizeof dbuff,
380 "flags = %02x, sport = %d, dport = %d",
381 th->th_flags, sport, dport);
390 if (datalen < mindata) {
391 log_Printf(LogFILTER, " error: proto %s must be at least"
392 " %d octets\n", prototxt(cproto), mindata);
396 if (log_IsKept(LogDEBUG)) {
399 snprintf(dbuff + len, sizeof dbuff - len,
400 ", estab = %d, syn = %d, finrst = %d",
403 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
404 prototxt(cproto), dbuff);
409 if (log_IsKept(LogDEBUG)) {
410 if (fp->f_srcop != OP_NONE) {
411 snprintf(dbuff, sizeof dbuff, ", src %s %d",
412 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
416 if (fp->f_dstop != OP_NONE) {
417 snprintf(dbuff + len, sizeof dbuff - len,
418 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
423 log_Printf(LogDEBUG, " rule = %d: Address match, "
424 "check against proto %d%s, action = %s\n",
425 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
428 if (cproto == fp->f_proto) {
429 if ((fp->f_srcop == OP_NONE ||
430 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
431 (fp->f_dstop == OP_NONE ||
432 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
433 (fp->f_estab == 0 || estab) &&
434 (fp->f_syn == 0 || syn) &&
435 (fp->f_finrst == 0 || finrst)) {
440 /* Address is matched and no protocol specified. Make a decision. */
441 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
442 filter_Action2Nam(fp->f_action));
446 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
448 if (match != fp->f_invert) {
449 /* Take specified action */
450 if (fp->f_action < A_NONE)
451 fp = &filter->rule[n = fp->f_action];
453 if (fp->f_action == A_PERMIT) {
455 *psecs = fp->timeout;
456 if (strcmp(filter->name, "DIAL") == 0) {
457 /* If dial filter then even print out accept packets */
458 if (log_IsKept(LogFILTER)) {
459 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
460 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
461 "src = %s:%d dst = %s:%d\n", filter->name, n,
462 prototxt(cproto), ncpaddr_ntoa(&srcaddr), sport,
468 if (log_IsKept(LogFILTER)) {
469 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
470 log_Printf(LogFILTER,
471 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
472 filter->name, n, prototxt(cproto),
473 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
476 } /* Explicit match. Deny this packet */
484 if (log_IsKept(LogFILTER)) {
485 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
486 log_Printf(LogFILTER,
487 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
488 filter->name, prototxt(cproto), ncpaddr_ntoa(&srcaddr),
489 sport, dstip, dport);
492 return 1; /* No rule matched, deny this packet */
496 ip_LogDNS(const struct udphdr *uh, const char *direction)
498 struct dns_header header;
499 const u_short *pktptr;
504 ptr = (const char *)uh + sizeof *uh;
505 len = ntohs(uh->uh_ulen) - sizeof *uh;
506 if (len < sizeof header + 5) /* rfc1024 */
509 pktptr = (const u_short *)ptr;
510 hptr = (u_short *)&header;
511 ptr += sizeof header;
512 len -= sizeof header;
514 while (pktptr < (const u_short *)ptr) {
515 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
519 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
521 char namewithdot[MAXHOSTNAMELEN + 1], *n;
522 const char *qtype, *qclass;
527 if (end - ptr >= (int)sizeof namewithdot)
528 end = ptr + sizeof namewithdot - 1;
531 if ((int)len > end - ptr)
533 if (n != namewithdot)
541 if (log_IsKept(LogDNS)) {
542 memcpy(&tmp, end, sizeof tmp);
543 qtype = dns_Qtype2Txt(ntohs(tmp));
544 memcpy(&tmp, end + 2, sizeof tmp);
545 qclass = dns_Qclass2Txt(ntohs(tmp));
547 log_Printf(LogDNS, "%sbound query %s %s %s\n",
548 direction, qclass, qtype, namewithdot);
554 * Check if the given packet matches the given filter.
555 * One of pip or pip6 must be set.
558 PacketCheck(struct bundle *bundle, u_int32_t family,
559 const unsigned char *packet, int nb, struct filter *filter,
560 const char *prefix, unsigned *psecs)
563 static const char *const TcpFlags[] = {
564 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
566 const struct tcphdr *th;
567 const struct udphdr *uh;
568 const struct icmp *icmph;
570 const struct icmp6_hdr *icmp6h;
572 const unsigned char *payload;
573 struct ncpaddr srcaddr, dstaddr;
574 int cproto, mask, len, n, pri, logit, result, datalen, frag;
578 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
579 (!filter || filter->logok);
584 if (family == AF_INET6) {
585 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
587 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
588 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
589 datalen = ntohs(pip6->ip6_plen);
590 payload = packet + sizeof *pip6;
591 cproto = pip6->ip6_nxt;
592 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
593 frag = 0; /* XXX: ??? */
597 const struct ip *pip = (const struct ip *)packet;
599 ncpaddr_setip4(&srcaddr, pip->ip_src);
600 ncpaddr_setip4(&dstaddr, pip->ip_dst);
601 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
602 payload = packet + (pip->ip_hl << 2);
605 frag = ntohs(pip->ip_off) & IP_OFFMASK;
610 if (logit && loglen < sizeof logbuf) {
612 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
614 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
616 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
617 loglen += strlen(logbuf + loglen);
622 if (logit && loglen < sizeof logbuf) {
623 len = datalen - sizeof *icmph;
624 icmph = (const struct icmp *)payload;
625 snprintf(logbuf + loglen, sizeof logbuf - loglen,
626 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
627 loglen += strlen(logbuf + loglen);
628 snprintf(logbuf + loglen, sizeof logbuf - loglen,
629 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
630 loglen += strlen(logbuf + loglen);
636 if (logit && loglen < sizeof logbuf) {
637 len = datalen - sizeof *icmp6h;
638 icmp6h = (const struct icmp6_hdr *)payload;
639 snprintf(logbuf + loglen, sizeof logbuf - loglen,
640 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
641 loglen += strlen(logbuf + loglen);
642 snprintf(logbuf + loglen, sizeof logbuf - loglen,
643 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
644 loglen += strlen(logbuf + loglen);
650 uh = (const struct udphdr *)payload;
651 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
654 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
655 ntohs(uh->uh_dport)))
658 if (logit && loglen < sizeof logbuf) {
659 len = datalen - sizeof *uh;
660 snprintf(logbuf + loglen, sizeof logbuf - loglen,
661 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
662 loglen += strlen(logbuf + loglen);
663 snprintf(logbuf + loglen, sizeof logbuf - loglen,
664 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
666 loglen += strlen(logbuf + loglen);
669 if (Enabled(bundle, OPT_FILTERDECAP) &&
670 payload[sizeof *uh] == HDLC_ADDR &&
671 payload[sizeof *uh + 1] == HDLC_UI) {
675 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
678 switch (ntohs(proto)) {
680 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
681 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
682 nb - (payload - packet) - sizeof *uh - 4, filter,
689 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
690 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
691 case PROTO_MP: type = "Multi-link"; break;
692 case PROTO_ICOMPD: type = "Individual link CCP"; break;
693 case PROTO_COMPD: type = "CCP"; break;
694 case PROTO_IPCP: type = "IPCP"; break;
695 case PROTO_LCP: type = "LCP"; break;
696 case PROTO_PAP: type = "PAP"; break;
697 case PROTO_CBCP: type = "CBCP"; break;
698 case PROTO_LQR: type = "LQR"; break;
699 case PROTO_CHAP: type = "CHAP"; break;
702 snprintf(logbuf + loglen, sizeof logbuf - loglen,
704 loglen += strlen(logbuf + loglen);
712 if (logit && loglen < sizeof logbuf) {
713 snprintf(logbuf + loglen, sizeof logbuf - loglen,
714 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
715 loglen += strlen(logbuf + loglen);
716 snprintf(logbuf + loglen, sizeof logbuf - loglen,
717 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
718 loglen += strlen(logbuf + loglen);
723 #ifdef IPPROTO_OSPFIGP
724 case IPPROTO_OSPFIGP:
725 if (logit && loglen < sizeof logbuf) {
726 snprintf(logbuf + loglen, sizeof logbuf - loglen,
727 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
728 loglen += strlen(logbuf + loglen);
729 snprintf(logbuf + loglen, sizeof logbuf - loglen,
730 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
731 loglen += strlen(logbuf + loglen);
738 if (logit && loglen < sizeof logbuf) {
739 snprintf(logbuf + loglen, sizeof logbuf - loglen,
740 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
741 loglen += strlen(logbuf + loglen);
742 snprintf(logbuf + loglen, sizeof logbuf - loglen,
743 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
744 loglen += strlen(logbuf + loglen);
747 if (Enabled(bundle, OPT_FILTERDECAP)) {
748 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
749 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
750 filter, logbuf, psecs);
758 if (logit && loglen < sizeof logbuf) {
759 snprintf(logbuf + loglen, sizeof logbuf - loglen,
760 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
761 loglen += strlen(logbuf + loglen);
762 snprintf(logbuf + loglen, sizeof logbuf - loglen,
763 "%s", ncpaddr_ntoa(&dstaddr));
764 loglen += strlen(logbuf + loglen);
767 if (Enabled(bundle, OPT_FILTERDECAP) &&
768 ((const struct ip *)payload)->ip_v == 4) {
769 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
770 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
771 filter, logbuf, psecs);
772 loglen += strlen(logbuf + loglen);
779 if (logit && loglen < sizeof logbuf) {
780 snprintf(logbuf + loglen, sizeof logbuf - loglen,
781 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
782 loglen += strlen(logbuf + loglen);
783 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
784 ncpaddr_ntoa(&dstaddr), payload);
785 loglen += strlen(logbuf + loglen);
790 if (logit && loglen < sizeof logbuf) {
791 snprintf(logbuf + loglen, sizeof logbuf - loglen,
792 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
793 loglen += strlen(logbuf + loglen);
794 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
795 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
796 loglen += strlen(logbuf + loglen);
801 if (logit && loglen < sizeof logbuf) {
802 uh = (const struct udphdr *)payload;
803 snprintf(logbuf + loglen, sizeof logbuf - loglen,
804 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
805 ntohs(uh->uh_sport));
806 loglen += strlen(logbuf + loglen);
807 snprintf(logbuf + loglen, sizeof logbuf - loglen,
808 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
809 loglen += strlen(logbuf + loglen);
814 th = (const struct tcphdr *)payload;
815 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
818 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
819 ntohs(th->th_dport)))
822 if (logit && loglen < sizeof logbuf) {
823 len = datalen - (th->th_off << 2);
824 snprintf(logbuf + loglen, sizeof logbuf - loglen,
825 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
826 loglen += strlen(logbuf + loglen);
827 snprintf(logbuf + loglen, sizeof logbuf - loglen,
828 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
829 loglen += strlen(logbuf + loglen);
831 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
832 if (th->th_flags & mask) {
833 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
834 loglen += strlen(logbuf + loglen);
838 snprintf(logbuf + loglen, sizeof logbuf - loglen,
839 " seq:%lx ack:%lx (%d/%d)",
840 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
841 loglen += strlen(logbuf + loglen);
842 if ((th->th_flags & TH_SYN) && nb > 40) {
845 sp = (const u_short *)(payload + 20);
846 if (ntohs(sp[0]) == 0x0204) {
847 snprintf(logbuf + loglen, sizeof logbuf - loglen,
848 " MSS = %d", ntohs(sp[1]));
849 loglen += strlen(logbuf + loglen);
859 if (logit && loglen < sizeof logbuf) {
860 snprintf(logbuf + loglen, sizeof logbuf - loglen,
861 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
862 loglen += strlen(logbuf + loglen);
863 snprintf(logbuf + loglen, sizeof logbuf - loglen,
864 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
865 loglen += strlen(logbuf + loglen);
870 if (filter && FilterCheck(packet, family, filter, psecs)) {
872 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
875 /* Check Keep Alive filter */
876 if (logit && log_IsKept(LogTCPIP)) {
881 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
882 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
883 else if (psecs != NULL) {
887 if (*psecs != alivesecs)
888 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
889 logbuf, *psecs, alivesecs);
891 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
893 log_Printf(LogTCPIP, "%s\n", logbuf);
899 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
900 ip_LogDNS(uh, filter->name);
906 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
912 unsigned secs, alivesecs;
915 if (nb > sizeof tun.data) {
916 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %zd, max %d)\n",
917 l->name, nb, (int)(sizeof tun.data));
921 mbuf_Read(bp, tun.data, nb);
924 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
929 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
932 bundle_StartIdleTimer(bundle, secs);
935 if (bundle->dev.header) {
936 tun.header.family = htonl(af);
937 nb += sizeof tun - sizeof tun.data;
942 nw = write(bundle->dev.fd, data, nb);
943 if (nw != (ssize_t)nb) {
945 log_Printf(LogERROR, "ip_Input: %s: wrote %zd, got %s\n",
946 l->name, nb, strerror(errno));
948 log_Printf(LogERROR, "ip_Input: %s: wrote %zd, got %zd\n", l->name, nb,
956 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
960 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
961 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
966 m_settype(bp, MB_IPIN);
968 nb = ip_Input(bundle, l, bp, AF_INET);
969 ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
976 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
980 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
981 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
986 m_settype(bp, MB_IPV6IN);
988 nb = ip_Input(bundle, l, bp, AF_INET6);
989 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);