2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
39 #include <netinet/icmp6.h>
40 #include <netinet/ip6.h>
42 #include <netinet/ip_icmp.h>
43 #include <netinet/udp.h>
44 #include <netinet/tcp.h>
63 #include "throughput.h"
65 #include "slcompress.h"
70 #include "descriptor.h"
84 #define OPCODE_QUERY 0
85 #define OPCODE_IQUERY 1
86 #define OPCODE_STATUS 2
105 dns_Qclass2Txt(u_short qclass)
107 static char failure[6];
113 { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" }
117 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
118 if (qtxt[f].id == qclass)
121 return HexStr(qclass, failure, sizeof failure);
125 dns_Qtype2Txt(u_short qtype)
127 static char failure[6];
132 /* rfc1035/rfc1700 */
133 { 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" },
134 { 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" },
135 { 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" },
136 { 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" },
137 { 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" },
138 { 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" },
139 { 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" },
140 { 254, "MAILA" }, { 255, "*" }
144 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
145 if (qtxt[f].id == qtype)
148 return HexStr(qtype, failure, sizeof failure);
152 PortMatch(int op, u_short pport, u_short rport)
156 return pport == rport;
158 return pport > rport;
160 return pport < rport;
167 * Return a text string representing the cproto protocol number.
169 * The purpose of this routine is calculate this result, for
170 * the many times it is needed in FilterCheck, only on demand
171 * (i.e. when the corresponding logging functions are invoked).
173 * This optimization saves, over the previous implementation, which
174 * calculated prototxt at the beginning of FilterCheck, an
175 * open/read/close system call sequence per packet, approximately
176 * halving the ppp system overhead and reducing the overall (u + s)
179 * The caching performed here is just a side effect.
184 static int oproto = -1;
185 static char protobuff[16] = "-1";
188 if (cproto == oproto)
190 if ((pe = getprotobynumber(cproto)) == NULL)
191 snprintf(protobuff, sizeof protobuff, "%d", cproto);
193 snprintf(protobuff, sizeof protobuff, "%s", pe->p_name);
199 * Check a packet against the given filter
200 * Returns 0 to accept the packet, non-zero to drop the packet.
201 * If psecs is not NULL, populate it with the timeout associated
202 * with the filter rule matched.
204 * If filtering is enabled, the initial fragment of a datagram must
205 * contain the complete protocol header, and subsequent fragments
206 * must not attempt to over-write it.
208 * One (and only one) of pip or pip6 must be set.
211 FilterCheck(const unsigned char *packet,
213 u_int32_t family __unused,
217 const struct filter *filter, unsigned *psecs)
219 int gotinfo; /* true if IP payload decoded */
220 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
221 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
222 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
223 int n; /* filter rule to process */
224 int len; /* bytes used in dbuff */
225 int didname; /* true if filter header printed */
226 int match; /* true if condition matched */
227 int mindata; /* minimum data size or zero */
228 const struct filterent *fp = filter->rule;
229 char dbuff[100], dstip[NCP_ASCIIBUFFERSIZE];
230 struct ncpaddr srcaddr, dstaddr;
231 const char *payload; /* IP payload */
232 int datalen; /* IP datagram length */
234 if (fp->f_action == A_NONE)
235 return 0; /* No rule is given. Permit this packet */
238 if (family == AF_INET6) {
239 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
241 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
242 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
243 datalen = ntohs(pip6->ip6_plen);
244 payload = packet + sizeof *pip6;
245 cproto = pip6->ip6_nxt;
250 * Deny any packet fragment that tries to over-write the header.
251 * Since we no longer have the real header available, punt on the
252 * largest normal header - 20 bytes for TCP without options, rounded
253 * up to the next possible fragment boundary. Since the smallest
254 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
255 * fragmentation within this range is dubious at best
257 const struct ip *pip = (const struct ip *)packet;
259 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
260 if (len > 0) { /* Not first fragment within datagram */
261 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
262 log_Printf(LogFILTER, " error: illegal header\n");
265 /* permit fragments on in and out filter */
266 if (!filter->fragok) {
267 log_Printf(LogFILTER, " error: illegal fragmentation\n");
273 ncpaddr_setip4(&srcaddr, pip->ip_src);
274 ncpaddr_setip4(&dstaddr, pip->ip_dst);
275 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
276 payload = packet + (pip->ip_hl << 2);
281 gotinfo = estab = syn = finrst = didname = 0;
284 for (n = 0; n < MAXFILTERS; ) {
285 if (fp->f_action == A_NONE) {
292 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
298 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
299 ncprange_contains(&fp->f_src, &srcaddr)) &&
300 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
301 ncprange_contains(&fp->f_dst, &dstaddr))) {
302 if (fp->f_proto != 0) {
304 const struct tcphdr *th;
305 const struct udphdr *uh;
306 const struct icmp *ih;
308 const struct icmp6_hdr *ih6;
312 estab = syn = finrst = -1;
316 mindata = 8; /* ICMP must be at least 8 octets */
317 ih = (const struct icmp *)payload;
318 sport = ih->icmp_type;
319 if (log_IsKept(LogDEBUG))
320 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
325 mindata = 8; /* ICMP must be at least 8 octets */
326 ih6 = (const struct icmp6_hdr *)payload;
327 sport = ih6->icmp6_type;
328 if (log_IsKept(LogDEBUG))
329 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
334 mindata = 8; /* IGMP uses 8-octet messages */
339 mindata = 2; /* GRE uses 2-octet+ messages */
342 #ifdef IPPROTO_OSPFIGP
343 case IPPROTO_OSPFIGP:
344 mindata = 8; /* IGMP uses 8-octet messages */
349 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
354 mindata = 8; /* UDP header is 8 octets */
355 uh = (const struct udphdr *)payload;
356 sport = ntohs(uh->uh_sport);
357 dport = ntohs(uh->uh_dport);
358 if (log_IsKept(LogDEBUG))
359 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
364 th = (const struct tcphdr *)payload;
366 * TCP headers are variable length. The following code
367 * ensures that the TCP header length isn't de-referenced if
368 * the datagram is too short
370 if (datalen < 20 || datalen < (th->th_off << 2)) {
371 log_Printf(LogFILTER, " error: TCP header incorrect\n");
374 sport = ntohs(th->th_sport);
375 dport = ntohs(th->th_dport);
376 estab = (th->th_flags & TH_ACK);
377 syn = (th->th_flags & TH_SYN);
378 finrst = (th->th_flags & (TH_FIN|TH_RST));
379 if (log_IsKept(LogDEBUG)) {
381 snprintf(dbuff, sizeof dbuff,
382 "flags = %02x, sport = %d, dport = %d",
383 th->th_flags, sport, dport);
392 if (datalen < mindata) {
393 log_Printf(LogFILTER, " error: proto %s must be at least"
394 " %d octets\n", prototxt(cproto), mindata);
398 if (log_IsKept(LogDEBUG)) {
401 snprintf(dbuff + len, sizeof dbuff - len,
402 ", estab = %d, syn = %d, finrst = %d",
405 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
406 prototxt(cproto), dbuff);
411 if (log_IsKept(LogDEBUG)) {
412 if (fp->f_srcop != OP_NONE) {
413 snprintf(dbuff, sizeof dbuff, ", src %s %d",
414 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
418 if (fp->f_dstop != OP_NONE) {
419 snprintf(dbuff + len, sizeof dbuff - len,
420 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
425 log_Printf(LogDEBUG, " rule = %d: Address match, "
426 "check against proto %d%s, action = %s\n",
427 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
430 if (cproto == fp->f_proto) {
431 if ((fp->f_srcop == OP_NONE ||
432 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
433 (fp->f_dstop == OP_NONE ||
434 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
435 (fp->f_estab == 0 || estab) &&
436 (fp->f_syn == 0 || syn) &&
437 (fp->f_finrst == 0 || finrst)) {
442 /* Address is matched and no protocol specified. Make a decision. */
443 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
444 filter_Action2Nam(fp->f_action));
448 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
450 if (match != fp->f_invert) {
451 /* Take specified action */
452 if (fp->f_action < A_NONE)
453 fp = &filter->rule[n = fp->f_action];
455 if (fp->f_action == A_PERMIT) {
457 *psecs = fp->timeout;
458 if (strcmp(filter->name, "DIAL") == 0) {
459 /* If dial filter then even print out accept packets */
460 if (log_IsKept(LogFILTER)) {
461 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
462 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
463 "src = %s:%d dst = %s:%d\n", filter->name, n,
464 prototxt(cproto), ncpaddr_ntoa(&srcaddr), sport,
470 if (log_IsKept(LogFILTER)) {
471 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
472 log_Printf(LogFILTER,
473 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
474 filter->name, n, prototxt(cproto),
475 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
478 } /* Explicit match. Deny this packet */
486 if (log_IsKept(LogFILTER)) {
487 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
488 log_Printf(LogFILTER,
489 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
490 filter->name, prototxt(cproto), ncpaddr_ntoa(&srcaddr),
491 sport, dstip, dport);
494 return 1; /* No rule matched, deny this packet */
498 ip_LogDNS(const struct udphdr *uh, const char *direction)
500 struct dns_header header;
501 const u_short *pktptr;
506 ptr = (const char *)uh + sizeof *uh;
507 len = ntohs(uh->uh_ulen) - sizeof *uh;
508 if (len < sizeof header + 5) /* rfc1024 */
511 pktptr = (const u_short *)ptr;
512 hptr = (u_short *)&header;
513 ptr += sizeof header;
514 len -= sizeof header;
516 while (pktptr < (const u_short *)ptr) {
517 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
521 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
523 char namewithdot[MAXHOSTNAMELEN + 1], *n;
524 const char *qtype, *qclass;
529 if (end - ptr >= (int)sizeof namewithdot)
530 end = ptr + sizeof namewithdot - 1;
533 if ((int)len > end - ptr)
535 if (n != namewithdot)
543 if (log_IsKept(LogDNS)) {
544 memcpy(&tmp, end, sizeof tmp);
545 qtype = dns_Qtype2Txt(ntohs(tmp));
546 memcpy(&tmp, end + 2, sizeof tmp);
547 qclass = dns_Qclass2Txt(ntohs(tmp));
549 log_Printf(LogDNS, "%sbound query %s %s %s\n",
550 direction, qclass, qtype, namewithdot);
556 * Check if the given packet matches the given filter.
557 * One of pip or pip6 must be set.
560 PacketCheck(struct bundle *bundle, u_int32_t family,
561 const unsigned char *packet, int nb, struct filter *filter,
562 const char *prefix, unsigned *psecs)
565 static const char *const TcpFlags[] = {
566 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
568 const struct tcphdr *th;
569 const struct udphdr *uh;
570 const struct icmp *icmph;
572 const struct icmp6_hdr *icmp6h;
574 const unsigned char *payload;
575 struct ncpaddr srcaddr, dstaddr;
576 int cproto, mask, len, n, pri, logit, result, datalen, frag;
580 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
581 (!filter || filter->logok);
586 if (family == AF_INET6) {
587 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
589 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
590 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
591 datalen = ntohs(pip6->ip6_plen);
592 payload = packet + sizeof *pip6;
593 cproto = pip6->ip6_nxt;
594 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
595 frag = 0; /* XXX: ??? */
599 const struct ip *pip = (const struct ip *)packet;
601 ncpaddr_setip4(&srcaddr, pip->ip_src);
602 ncpaddr_setip4(&dstaddr, pip->ip_dst);
603 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
604 payload = packet + (pip->ip_hl << 2);
607 frag = ntohs(pip->ip_off) & IP_OFFMASK;
612 if (logit && loglen < sizeof logbuf) {
614 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
616 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
618 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
619 loglen += strlen(logbuf + loglen);
624 if (logit && loglen < sizeof logbuf) {
625 len = datalen - sizeof *icmph;
626 icmph = (const struct icmp *)payload;
627 snprintf(logbuf + loglen, sizeof logbuf - loglen,
628 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
629 loglen += strlen(logbuf + loglen);
630 snprintf(logbuf + loglen, sizeof logbuf - loglen,
631 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
632 loglen += strlen(logbuf + loglen);
638 if (logit && loglen < sizeof logbuf) {
639 len = datalen - sizeof *icmp6h;
640 icmp6h = (const struct icmp6_hdr *)payload;
641 snprintf(logbuf + loglen, sizeof logbuf - loglen,
642 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
643 loglen += strlen(logbuf + loglen);
644 snprintf(logbuf + loglen, sizeof logbuf - loglen,
645 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
646 loglen += strlen(logbuf + loglen);
652 uh = (const struct udphdr *)payload;
653 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
656 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
657 ntohs(uh->uh_dport)))
660 if (logit && loglen < sizeof logbuf) {
661 len = datalen - sizeof *uh;
662 snprintf(logbuf + loglen, sizeof logbuf - loglen,
663 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
664 loglen += strlen(logbuf + loglen);
665 snprintf(logbuf + loglen, sizeof logbuf - loglen,
666 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
668 loglen += strlen(logbuf + loglen);
671 if (Enabled(bundle, OPT_FILTERDECAP) &&
672 payload[sizeof *uh] == HDLC_ADDR &&
673 payload[sizeof *uh + 1] == HDLC_UI) {
677 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
680 switch (ntohs(proto)) {
682 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
683 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
684 nb - (payload - packet) - sizeof *uh - 4, filter,
691 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
692 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
693 case PROTO_MP: type = "Multi-link"; break;
694 case PROTO_ICOMPD: type = "Individual link CCP"; break;
695 case PROTO_COMPD: type = "CCP"; break;
696 case PROTO_IPCP: type = "IPCP"; break;
697 case PROTO_LCP: type = "LCP"; break;
698 case PROTO_PAP: type = "PAP"; break;
699 case PROTO_CBCP: type = "CBCP"; break;
700 case PROTO_LQR: type = "LQR"; break;
701 case PROTO_CHAP: type = "CHAP"; break;
704 snprintf(logbuf + loglen, sizeof logbuf - loglen,
706 loglen += strlen(logbuf + loglen);
714 if (logit && loglen < sizeof logbuf) {
715 snprintf(logbuf + loglen, sizeof logbuf - loglen,
716 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
717 loglen += strlen(logbuf + loglen);
718 snprintf(logbuf + loglen, sizeof logbuf - loglen,
719 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
720 loglen += strlen(logbuf + loglen);
725 #ifdef IPPROTO_OSPFIGP
726 case IPPROTO_OSPFIGP:
727 if (logit && loglen < sizeof logbuf) {
728 snprintf(logbuf + loglen, sizeof logbuf - loglen,
729 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
730 loglen += strlen(logbuf + loglen);
731 snprintf(logbuf + loglen, sizeof logbuf - loglen,
732 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
733 loglen += strlen(logbuf + loglen);
740 if (logit && loglen < sizeof logbuf) {
741 snprintf(logbuf + loglen, sizeof logbuf - loglen,
742 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
743 loglen += strlen(logbuf + loglen);
744 snprintf(logbuf + loglen, sizeof logbuf - loglen,
745 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
746 loglen += strlen(logbuf + loglen);
749 if (Enabled(bundle, OPT_FILTERDECAP)) {
750 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
751 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
752 filter, logbuf, psecs);
760 if (logit && loglen < sizeof logbuf) {
761 snprintf(logbuf + loglen, sizeof logbuf - loglen,
762 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
763 loglen += strlen(logbuf + loglen);
764 snprintf(logbuf + loglen, sizeof logbuf - loglen,
765 "%s", ncpaddr_ntoa(&dstaddr));
766 loglen += strlen(logbuf + loglen);
769 if (Enabled(bundle, OPT_FILTERDECAP) &&
770 ((const struct ip *)payload)->ip_v == 4) {
771 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
772 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
773 filter, logbuf, psecs);
774 loglen += strlen(logbuf + loglen);
781 if (logit && loglen < sizeof logbuf) {
782 snprintf(logbuf + loglen, sizeof logbuf - loglen,
783 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
784 loglen += strlen(logbuf + loglen);
785 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
786 ncpaddr_ntoa(&dstaddr), payload);
787 loglen += strlen(logbuf + loglen);
792 if (logit && loglen < sizeof logbuf) {
793 snprintf(logbuf + loglen, sizeof logbuf - loglen,
794 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
795 loglen += strlen(logbuf + loglen);
796 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
797 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
798 loglen += strlen(logbuf + loglen);
803 if (logit && loglen < sizeof logbuf) {
804 uh = (const struct udphdr *)payload;
805 snprintf(logbuf + loglen, sizeof logbuf - loglen,
806 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
807 ntohs(uh->uh_sport));
808 loglen += strlen(logbuf + loglen);
809 snprintf(logbuf + loglen, sizeof logbuf - loglen,
810 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
811 loglen += strlen(logbuf + loglen);
816 th = (const struct tcphdr *)payload;
817 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
820 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
821 ntohs(th->th_dport)))
823 else if (!frag && ncp_IsUrgentTcpLen(&bundle->ncp, datalen))
826 if (logit && loglen < sizeof logbuf) {
827 len = datalen - (th->th_off << 2);
828 snprintf(logbuf + loglen, sizeof logbuf - loglen,
829 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
830 loglen += strlen(logbuf + loglen);
831 snprintf(logbuf + loglen, sizeof logbuf - loglen,
832 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
833 loglen += strlen(logbuf + loglen);
835 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
836 if (th->th_flags & mask) {
837 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
838 loglen += strlen(logbuf + loglen);
842 snprintf(logbuf + loglen, sizeof logbuf - loglen,
843 " seq:%lx ack:%lx (%d/%d)",
844 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
845 loglen += strlen(logbuf + loglen);
846 if ((th->th_flags & TH_SYN) && nb > 40) {
849 sp = (const u_short *)(payload + 20);
850 if (ntohs(sp[0]) == 0x0204) {
851 snprintf(logbuf + loglen, sizeof logbuf - loglen,
852 " MSS = %d", ntohs(sp[1]));
853 loglen += strlen(logbuf + loglen);
856 snprintf(logbuf + loglen, sizeof logbuf - loglen, " pri:%d", pri);
857 loglen += strlen(logbuf + loglen);
865 if (logit && loglen < sizeof logbuf) {
866 snprintf(logbuf + loglen, sizeof logbuf - loglen,
867 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
868 loglen += strlen(logbuf + loglen);
869 snprintf(logbuf + loglen, sizeof logbuf - loglen,
870 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
871 loglen += strlen(logbuf + loglen);
876 if (filter && FilterCheck(packet, family, filter, psecs)) {
878 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
881 /* Check Keep Alive filter */
882 if (logit && log_IsKept(LogTCPIP)) {
887 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
888 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
889 else if (psecs != NULL) {
893 if (*psecs != alivesecs)
894 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
895 logbuf, *psecs, alivesecs);
897 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
899 log_Printf(LogTCPIP, "%s\n", logbuf);
905 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
906 ip_LogDNS(uh, filter->name);
912 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
918 unsigned secs, alivesecs;
921 if (nb > sizeof tun.data) {
922 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %zd, max %d)\n",
923 l->name, nb, (int)(sizeof tun.data));
927 mbuf_Read(bp, tun.data, nb);
930 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
935 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
938 bundle_StartIdleTimer(bundle, secs);
941 if (bundle->dev.header) {
942 tun.header.family = htonl(af);
943 nb += sizeof tun - sizeof tun.data;
948 nw = write(bundle->dev.fd, data, nb);
949 if (nw != (ssize_t)nb) {
951 log_Printf(LogERROR, "ip_Input: %s: wrote %zd, got %s\n",
952 l->name, nb, strerror(errno));
954 log_Printf(LogERROR, "ip_Input: %s: wrote %zd, got %zd\n", l->name, nb,
962 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
966 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
967 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
972 m_settype(bp, MB_IPIN);
974 nb = ip_Input(bundle, l, bp, AF_INET);
975 ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
982 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
986 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
987 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
992 m_settype(bp, MB_IPV6IN);
994 nb = ip_Input(bundle, l, bp, AF_INET6);
995 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);