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
28 * $FreeBSD: src/usr.sbin/ppp/ip.c,v 1.78.2.11 2002/09/01 02:12:27 brian Exp $
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 < NELEM(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 < NELEM(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 toprototxt(int cproto)
167 static char prototxt[16];
170 if ((pe = getprotobynumber(cproto)) == NULL)
171 snprintf(prototxt, sizeof(prototxt), "%d", cproto);
173 snprintf(prototxt, sizeof(prototxt), "%s", pe->p_name);
178 * Check a packet against the given filter
179 * Returns 0 to accept the packet, non-zero to drop the packet.
180 * If psecs is not NULL, populate it with the timeout associated
181 * with the filter rule matched.
183 * If filtering is enabled, the initial fragment of a datagram must
184 * contain the complete protocol header, and subsequent fragments
185 * must not attempt to over-write it.
187 * One (and only one) of pip or pip6 must be set.
190 FilterCheck(const unsigned char *packet, u_int32_t family,
191 const struct filter *filter, unsigned *psecs)
193 int gotinfo; /* true if IP payload decoded */
194 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
195 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
196 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
197 int n; /* filter rule to process */
198 int len; /* bytes used in dbuff */
199 int didname; /* true if filter header printed */
200 int match; /* true if condition matched */
201 int mindata; /* minimum data size or zero */
202 const struct filterent *fp = filter->rule;
203 char dbuff[100], dstip[16];
204 struct ncpaddr srcaddr, dstaddr;
205 const char *payload; /* IP payload */
206 int datalen; /* IP datagram length */
208 if (fp->f_action == A_NONE)
209 return 0; /* No rule is given. Permit this packet */
212 if (family == AF_INET6) {
213 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
215 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
216 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
217 datalen = ntohs(pip6->ip6_plen);
218 payload = packet + sizeof *pip6;
219 cproto = pip6->ip6_nxt;
224 * Deny any packet fragment that tries to over-write the header.
225 * Since we no longer have the real header available, punt on the
226 * largest normal header - 20 bytes for TCP without options, rounded
227 * up to the next possible fragment boundary. Since the smallest
228 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
229 * fragmentation within this range is dubious at best
231 const struct ip *pip = (const struct ip *)packet;
233 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
234 if (len > 0) { /* Not first fragment within datagram */
235 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
236 log_Printf(LogFILTER, " error: illegal header\n");
239 /* permit fragments on in and out filter */
240 if (!filter->fragok) {
241 log_Printf(LogFILTER, " error: illegal fragmentation\n");
247 ncpaddr_setip4(&srcaddr, pip->ip_src);
248 ncpaddr_setip4(&dstaddr, pip->ip_dst);
249 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
250 payload = packet + (pip->ip_hl << 2);
254 gotinfo = estab = syn = finrst = didname = 0;
257 for (n = 0; n < MAXFILTERS; ) {
258 if (fp->f_action == A_NONE) {
265 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
271 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
272 ncprange_contains(&fp->f_src, &srcaddr)) &&
273 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
274 ncprange_contains(&fp->f_dst, &dstaddr))) {
275 if (fp->f_proto != 0) {
277 const struct tcphdr *th;
278 const struct udphdr *uh;
279 const struct icmp *ih;
281 const struct icmp6_hdr *ih6;
285 estab = syn = finrst = -1;
289 mindata = 8; /* ICMP must be at least 8 octets */
290 ih = (const struct icmp *)payload;
291 sport = ntohs(ih->icmp_type);
292 if (log_IsKept(LogDEBUG))
293 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
298 mindata = 8; /* ICMP must be at least 8 octets */
299 ih6 = (const struct icmp6_hdr *)payload;
300 sport = ntohs(ih6->icmp6_type);
301 if (log_IsKept(LogDEBUG))
302 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
307 mindata = 8; /* IGMP uses 8-octet messages */
312 mindata = 2; /* GRE uses 2-octet+ messages */
315 #ifdef IPPROTO_OSPFIGP
316 case IPPROTO_OSPFIGP:
317 mindata = 8; /* IGMP uses 8-octet messages */
322 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
327 mindata = 8; /* UDP header is 8 octets */
328 uh = (const struct udphdr *)payload;
329 sport = ntohs(uh->uh_sport);
330 dport = ntohs(uh->uh_dport);
331 if (log_IsKept(LogDEBUG))
332 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
337 th = (const struct tcphdr *)payload;
339 * TCP headers are variable length. The following code
340 * ensures that the TCP header length isn't de-referenced if
341 * the datagram is too short
343 if (datalen < 20 || datalen < (th->th_off << 2)) {
344 log_Printf(LogFILTER, " error: TCP header incorrect\n");
347 sport = ntohs(th->th_sport);
348 dport = ntohs(th->th_dport);
349 estab = (th->th_flags & TH_ACK);
350 syn = (th->th_flags & TH_SYN);
351 finrst = (th->th_flags & (TH_FIN|TH_RST));
352 if (log_IsKept(LogDEBUG)) {
354 snprintf(dbuff, sizeof dbuff,
355 "flags = %02x, sport = %d, dport = %d",
356 th->th_flags, sport, dport);
365 if (datalen < mindata) {
366 log_Printf(LogFILTER, " error: proto %s must be at least"
367 " %d octets\n", toprototxt(cproto), mindata);
371 if (log_IsKept(LogDEBUG)) {
374 snprintf(dbuff + len, sizeof dbuff - len,
375 ", estab = %d, syn = %d, finrst = %d",
378 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", toprototxt(cproto), dbuff);
383 if (log_IsKept(LogDEBUG)) {
384 if (fp->f_srcop != OP_NONE) {
385 snprintf(dbuff, sizeof dbuff, ", src %s %d",
386 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
390 if (fp->f_dstop != OP_NONE) {
391 snprintf(dbuff + len, sizeof dbuff - len,
392 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
397 log_Printf(LogDEBUG, " rule = %d: Address match, "
398 "check against proto %d%s, action = %s\n",
399 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
402 if (cproto == fp->f_proto) {
403 if ((fp->f_srcop == OP_NONE ||
404 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
405 (fp->f_dstop == OP_NONE ||
406 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
407 (fp->f_estab == 0 || estab) &&
408 (fp->f_syn == 0 || syn) &&
409 (fp->f_finrst == 0 || finrst)) {
414 /* Address is matched and no protocol specified. Make a decision. */
415 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
416 filter_Action2Nam(fp->f_action));
420 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
422 if (match != fp->f_invert) {
423 /* Take specified action */
424 if (fp->f_action < A_NONE)
425 fp = &filter->rule[n = fp->f_action];
427 if (fp->f_action == A_PERMIT) {
429 *psecs = fp->timeout;
430 if (strcmp(filter->name, "DIAL") == 0) {
431 /* If dial filter then even print out accept packets */
432 if (log_IsKept(LogFILTER)) {
433 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
434 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
435 "src = %s:%d dst = %s:%d\n", filter->name, n,
437 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
442 if (log_IsKept(LogFILTER)) {
443 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
444 log_Printf(LogFILTER,
445 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
446 filter->name, n, toprototxt(cproto),
447 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
450 } /* Explict match. Deny this packet */
458 if (log_IsKept(LogFILTER)) {
459 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
460 log_Printf(LogFILTER,
461 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
462 filter->name, toprototxt(cproto), ncpaddr_ntoa(&srcaddr), sport,
466 return 1; /* No rule matched, deny this packet */
470 ip_LogDNS(const struct udphdr *uh, const char *direction)
472 struct dns_header header;
473 const u_short *pktptr;
478 ptr = (const char *)uh + sizeof *uh;
479 len = ntohs(uh->uh_ulen) - sizeof *uh;
480 if (len < sizeof header + 5) /* rfc1024 */
483 pktptr = (const u_short *)ptr;
484 hptr = (u_short *)&header;
485 ptr += sizeof header;
486 len -= sizeof header;
488 while (pktptr < (const u_short *)ptr) {
489 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
493 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
495 char namewithdot[MAXHOSTNAMELEN + 1], *n;
496 const char *qtype, *qclass;
501 if (end - ptr >= (int)sizeof namewithdot)
502 end = ptr + sizeof namewithdot - 1;
505 if ((int)len > end - ptr)
507 if (n != namewithdot)
515 if (log_IsKept(LogDNS)) {
516 memcpy(&tmp, end, sizeof tmp);
517 qtype = dns_Qtype2Txt(ntohs(tmp));
518 memcpy(&tmp, end + 2, sizeof tmp);
519 qclass = dns_Qclass2Txt(ntohs(tmp));
521 log_Printf(LogDNS, "%sbound query %s %s %s\n",
522 direction, qclass, qtype, namewithdot);
528 * Check if the given packet matches the given filter.
529 * One of pip or pip6 must be set.
532 PacketCheck(struct bundle *bundle, u_int32_t family,
533 const unsigned char *packet, int nb, struct filter *filter,
534 const char *prefix, unsigned *psecs)
537 static const char *const TcpFlags[] = {
538 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
540 const struct tcphdr *th;
541 const struct udphdr *uh;
542 const struct icmp *icmph;
544 const struct icmp6_hdr *icmp6h;
546 const unsigned char *payload;
547 struct ncpaddr srcaddr, dstaddr;
548 int cproto, mask, len, n, pri, logit, result, datalen, frag;
552 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
553 (!filter || filter->logok);
558 if (family == AF_INET6) {
559 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
561 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
562 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
563 datalen = ntohs(pip6->ip6_plen);
564 payload = packet + sizeof *pip6;
565 cproto = pip6->ip6_nxt;
566 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
567 frag = 0; /* XXX: ??? */
571 const struct ip *pip = (const struct ip *)packet;
573 ncpaddr_setip4(&srcaddr, pip->ip_src);
574 ncpaddr_setip4(&dstaddr, pip->ip_dst);
575 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
576 payload = packet + (pip->ip_hl << 2);
579 frag = ntohs(pip->ip_off) & IP_OFFMASK;
584 if (logit && loglen < sizeof logbuf) {
586 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
588 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
590 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
591 loglen += strlen(logbuf + loglen);
596 if (logit && loglen < sizeof logbuf) {
597 len = datalen - sizeof *icmph;
598 icmph = (const struct icmp *)payload;
599 snprintf(logbuf + loglen, sizeof logbuf - loglen,
600 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
601 loglen += strlen(logbuf + loglen);
602 snprintf(logbuf + loglen, sizeof logbuf - loglen,
603 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
604 loglen += strlen(logbuf + loglen);
610 if (logit && loglen < sizeof logbuf) {
611 len = datalen - sizeof *icmp6h;
612 icmp6h = (const struct icmp6_hdr *)payload;
613 snprintf(logbuf + loglen, sizeof logbuf - loglen,
614 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
615 loglen += strlen(logbuf + loglen);
616 snprintf(logbuf + loglen, sizeof logbuf - loglen,
617 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
618 loglen += strlen(logbuf + loglen);
624 uh = (const struct udphdr *)payload;
625 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
628 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
629 ntohs(uh->uh_dport)))
632 if (logit && loglen < sizeof logbuf) {
633 len = datalen - sizeof *uh;
634 snprintf(logbuf + loglen, sizeof logbuf - loglen,
635 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
636 loglen += strlen(logbuf + loglen);
637 snprintf(logbuf + loglen, sizeof logbuf - loglen,
638 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
640 loglen += strlen(logbuf + loglen);
643 if (Enabled(bundle, OPT_FILTERDECAP) &&
644 payload[sizeof *uh] == HDLC_ADDR &&
645 payload[sizeof *uh + 1] == HDLC_UI) {
649 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
652 switch (ntohs(proto)) {
654 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
655 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
656 nb - (payload - packet) - sizeof *uh - 4, filter,
663 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
664 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
665 case PROTO_MP: type = "Multi-link"; break;
666 case PROTO_ICOMPD: type = "Individual link CCP"; break;
667 case PROTO_COMPD: type = "CCP"; break;
668 case PROTO_IPCP: type = "IPCP"; break;
669 case PROTO_LCP: type = "LCP"; break;
670 case PROTO_PAP: type = "PAP"; break;
671 case PROTO_CBCP: type = "CBCP"; break;
672 case PROTO_LQR: type = "LQR"; break;
673 case PROTO_CHAP: type = "CHAP"; break;
676 snprintf(logbuf + loglen, sizeof logbuf - loglen,
678 loglen += strlen(logbuf + loglen);
686 if (logit && loglen < sizeof logbuf) {
687 snprintf(logbuf + loglen, sizeof logbuf - loglen,
688 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
689 loglen += strlen(logbuf + loglen);
690 snprintf(logbuf + loglen, sizeof logbuf - loglen,
691 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
692 loglen += strlen(logbuf + loglen);
697 #ifdef IPPROTO_OSPFIGP
698 case IPPROTO_OSPFIGP:
699 if (logit && loglen < sizeof logbuf) {
700 snprintf(logbuf + loglen, sizeof logbuf - loglen,
701 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
702 loglen += strlen(logbuf + loglen);
703 snprintf(logbuf + loglen, sizeof logbuf - loglen,
704 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
705 loglen += strlen(logbuf + loglen);
712 if (logit && loglen < sizeof logbuf) {
713 snprintf(logbuf + loglen, sizeof logbuf - loglen,
714 "IPv6: %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);
721 if (Enabled(bundle, OPT_FILTERDECAP)) {
722 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
723 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
724 filter, logbuf, psecs);
732 if (logit && loglen < sizeof logbuf) {
733 snprintf(logbuf + loglen, sizeof logbuf - loglen,
734 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
735 loglen += strlen(logbuf + loglen);
736 snprintf(logbuf + loglen, sizeof logbuf - loglen,
737 "%s", ncpaddr_ntoa(&dstaddr));
738 loglen += strlen(logbuf + loglen);
741 if (Enabled(bundle, OPT_FILTERDECAP) &&
742 ((const struct ip *)payload)->ip_v == 4) {
743 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
744 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
745 filter, logbuf, psecs);
746 loglen += strlen(logbuf + loglen);
753 if (logit && loglen < sizeof logbuf) {
754 snprintf(logbuf + loglen, sizeof logbuf - loglen,
755 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
756 loglen += strlen(logbuf + loglen);
757 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
758 ncpaddr_ntoa(&dstaddr), payload);
759 loglen += strlen(logbuf + loglen);
764 if (logit && loglen < sizeof logbuf) {
765 snprintf(logbuf + loglen, sizeof logbuf - loglen,
766 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
767 loglen += strlen(logbuf + loglen);
768 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
769 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
770 loglen += strlen(logbuf + loglen);
775 if (logit && loglen < sizeof logbuf) {
776 uh = (const struct udphdr *)payload;
777 snprintf(logbuf + loglen, sizeof logbuf - loglen,
778 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
779 ntohs(uh->uh_sport));
780 loglen += strlen(logbuf + loglen);
781 snprintf(logbuf + loglen, sizeof logbuf - loglen,
782 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
783 loglen += strlen(logbuf + loglen);
788 th = (const struct tcphdr *)payload;
789 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
792 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
793 ntohs(th->th_dport)))
796 if (logit && loglen < sizeof logbuf) {
797 len = datalen - (th->th_off << 2);
798 snprintf(logbuf + loglen, sizeof logbuf - loglen,
799 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
800 loglen += strlen(logbuf + loglen);
801 snprintf(logbuf + loglen, sizeof logbuf - loglen,
802 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
803 loglen += strlen(logbuf + loglen);
805 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
806 if (th->th_flags & mask) {
807 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
808 loglen += strlen(logbuf + loglen);
812 snprintf(logbuf + loglen, sizeof logbuf - loglen,
813 " seq:%lx ack:%lx (%d/%d)",
814 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
815 loglen += strlen(logbuf + loglen);
816 if ((th->th_flags & TH_SYN) && nb > 40) {
819 sp = (const u_short *)(payload + 20);
820 if (ntohs(sp[0]) == 0x0204) {
821 snprintf(logbuf + loglen, sizeof logbuf - loglen,
822 " MSS = %d", ntohs(sp[1]));
823 loglen += strlen(logbuf + loglen);
833 if (logit && loglen < sizeof logbuf) {
834 snprintf(logbuf + loglen, sizeof logbuf - loglen,
835 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
836 loglen += strlen(logbuf + loglen);
837 snprintf(logbuf + loglen, sizeof logbuf - loglen,
838 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
839 loglen += strlen(logbuf + loglen);
844 if (filter && FilterCheck(packet, family, filter, psecs)) {
846 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
849 /* Check Keep Alive filter */
850 if (logit && log_IsKept(LogTCPIP)) {
855 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
856 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
857 else if (psecs != NULL) {
861 if (*psecs != alivesecs)
862 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
863 logbuf, *psecs, alivesecs);
865 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
867 log_Printf(LogTCPIP, "%s\n", logbuf);
873 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
874 ip_LogDNS(uh, filter->name);
880 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
886 unsigned secs, alivesecs;
889 if (nb > sizeof tun.data) {
890 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %zu, max %d)\n",
891 l->name, nb, (int)(sizeof tun.data));
895 mbuf_Read(bp, tun.data, nb);
898 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
903 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
906 bundle_StartIdleTimer(bundle, secs);
909 if (bundle->dev.header) {
910 tun.header.family = htonl(af);
911 nb += sizeof tun - sizeof tun.data;
916 nw = write(bundle->dev.fd, data, nb);
917 if (nw != (ssize_t)nb) {
919 log_Printf(LogERROR, "ip_Input: %s: wrote %zu, got %s\n",
920 l->name, nb, strerror(errno));
922 log_Printf(LogERROR, "ip_Input: %s: wrote %zu, got %zu\n", l->name, nb, nw);
929 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
933 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
934 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
939 m_settype(bp, MB_IPIN);
941 nb = ip_Input(bundle, l, bp, AF_INET);
942 ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
949 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
953 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
954 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
959 m_settype(bp, MB_IPV6IN);
961 nb = ip_Input(bundle, l, bp, AF_INET6);
962 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);