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 < 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 * Check a packet against the given filter
166 * Returns 0 to accept the packet, non-zero to drop the packet.
167 * If psecs is not NULL, populate it with the timeout associated
168 * with the filter rule matched.
170 * If filtering is enabled, the initial fragment of a datagram must
171 * contain the complete protocol header, and subsequent fragments
172 * must not attempt to over-write it.
174 * One (and only one) of pip or pip6 must be set.
177 FilterCheck(const unsigned char *packet, u_int32_t family,
178 const struct filter *filter, unsigned *psecs)
180 int gotinfo; /* true if IP payload decoded */
181 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
182 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
183 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
184 int n; /* filter rule to process */
185 int len; /* bytes used in dbuff */
186 int didname; /* true if filter header printed */
187 int match; /* true if condition matched */
188 int mindata; /* minimum data size or zero */
189 const struct filterent *fp = filter->rule;
190 char dbuff[100], dstip[16], prototxt[16];
192 struct ncpaddr srcaddr, dstaddr;
193 const char *payload; /* IP payload */
194 int datalen; /* IP datagram length */
196 if (fp->f_action == A_NONE)
197 return 0; /* No rule is given. Permit this packet */
200 if (family == AF_INET6) {
201 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
203 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
204 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
205 datalen = ntohs(pip6->ip6_plen);
206 payload = packet + sizeof *pip6;
207 cproto = pip6->ip6_nxt;
212 * Deny any packet fragment that tries to over-write the header.
213 * Since we no longer have the real header available, punt on the
214 * largest normal header - 20 bytes for TCP without options, rounded
215 * up to the next possible fragment boundary. Since the smallest
216 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
217 * fragmentation within this range is dubious at best
219 const struct ip *pip = (const struct ip *)packet;
221 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
222 if (len > 0) { /* Not first fragment within datagram */
223 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
224 log_Printf(LogFILTER, " error: illegal header\n");
227 /* permit fragments on in and out filter */
228 if (!filter->fragok) {
229 log_Printf(LogFILTER, " error: illegal fragmentation\n");
235 ncpaddr_setip4(&srcaddr, pip->ip_src);
236 ncpaddr_setip4(&dstaddr, pip->ip_dst);
237 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
238 payload = packet + (pip->ip_hl << 2);
242 if ((pe = getprotobynumber(cproto)) == NULL)
243 snprintf(prototxt, sizeof prototxt, "%d", cproto);
245 snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
246 gotinfo = estab = syn = finrst = didname = 0;
249 for (n = 0; n < MAXFILTERS; ) {
250 if (fp->f_action == A_NONE) {
257 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
263 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
264 ncprange_contains(&fp->f_src, &srcaddr)) &&
265 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
266 ncprange_contains(&fp->f_dst, &dstaddr))) {
267 if (fp->f_proto != 0) {
269 const struct tcphdr *th;
270 const struct udphdr *uh;
271 const struct icmp *ih;
273 const struct icmp6_hdr *ih6;
277 estab = syn = finrst = -1;
281 mindata = 8; /* ICMP must be at least 8 octets */
282 ih = (const struct icmp *)payload;
283 sport = ntohs(ih->icmp_type);
284 if (log_IsKept(LogDEBUG))
285 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
290 mindata = 8; /* ICMP must be at least 8 octets */
291 ih6 = (const struct icmp6_hdr *)payload;
292 sport = ntohs(ih6->icmp6_type);
293 if (log_IsKept(LogDEBUG))
294 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
299 mindata = 8; /* IGMP uses 8-octet messages */
304 mindata = 2; /* GRE uses 2-octet+ messages */
307 #ifdef IPPROTO_OSPFIGP
308 case IPPROTO_OSPFIGP:
309 mindata = 8; /* IGMP uses 8-octet messages */
314 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
319 mindata = 8; /* UDP header is 8 octets */
320 uh = (const struct udphdr *)payload;
321 sport = ntohs(uh->uh_sport);
322 dport = ntohs(uh->uh_dport);
323 if (log_IsKept(LogDEBUG))
324 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
329 th = (const struct tcphdr *)payload;
331 * TCP headers are variable length. The following code
332 * ensures that the TCP header length isn't de-referenced if
333 * the datagram is too short
335 if (datalen < 20 || datalen < (th->th_off << 2)) {
336 log_Printf(LogFILTER, " error: TCP header incorrect\n");
339 sport = ntohs(th->th_sport);
340 dport = ntohs(th->th_dport);
341 estab = (th->th_flags & TH_ACK);
342 syn = (th->th_flags & TH_SYN);
343 finrst = (th->th_flags & (TH_FIN|TH_RST));
344 if (log_IsKept(LogDEBUG)) {
346 snprintf(dbuff, sizeof dbuff,
347 "flags = %02x, sport = %d, dport = %d",
348 th->th_flags, sport, dport);
357 if (datalen < mindata) {
358 log_Printf(LogFILTER, " error: proto %s must be at least"
359 " %d octets\n", prototxt, mindata);
363 if (log_IsKept(LogDEBUG)) {
366 snprintf(dbuff + len, sizeof dbuff - len,
367 ", estab = %d, syn = %d, finrst = %d",
370 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff);
375 if (log_IsKept(LogDEBUG)) {
376 if (fp->f_srcop != OP_NONE) {
377 snprintf(dbuff, sizeof dbuff, ", src %s %d",
378 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
382 if (fp->f_dstop != OP_NONE) {
383 snprintf(dbuff + len, sizeof dbuff - len,
384 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
389 log_Printf(LogDEBUG, " rule = %d: Address match, "
390 "check against proto %d%s, action = %s\n",
391 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
394 if (cproto == fp->f_proto) {
395 if ((fp->f_srcop == OP_NONE ||
396 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
397 (fp->f_dstop == OP_NONE ||
398 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
399 (fp->f_estab == 0 || estab) &&
400 (fp->f_syn == 0 || syn) &&
401 (fp->f_finrst == 0 || finrst)) {
406 /* Address is matched and no protocol specified. Make a decision. */
407 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
408 filter_Action2Nam(fp->f_action));
412 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
414 if (match != fp->f_invert) {
415 /* Take specified action */
416 if (fp->f_action < A_NONE)
417 fp = &filter->rule[n = fp->f_action];
419 if (fp->f_action == A_PERMIT) {
421 *psecs = fp->timeout;
422 if (strcmp(filter->name, "DIAL") == 0) {
423 /* If dial filter then even print out accept packets */
424 if (log_IsKept(LogFILTER)) {
425 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
426 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
427 "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt,
428 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
433 if (log_IsKept(LogFILTER)) {
434 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
435 log_Printf(LogFILTER,
436 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
437 filter->name, n, prototxt,
438 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
441 } /* Explict match. Deny this packet */
449 if (log_IsKept(LogFILTER)) {
450 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
451 log_Printf(LogFILTER,
452 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
453 filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport,
457 return 1; /* No rule matched, deny this packet */
461 ip_LogDNS(const struct udphdr *uh, const char *direction)
463 struct dns_header header;
464 const u_short *pktptr;
469 ptr = (const char *)uh + sizeof *uh;
470 len = ntohs(uh->uh_ulen) - sizeof *uh;
471 if (len < sizeof header + 5) /* rfc1024 */
474 pktptr = (const u_short *)ptr;
475 hptr = (u_short *)&header;
476 ptr += sizeof header;
477 len -= sizeof header;
479 while (pktptr < (const u_short *)ptr) {
480 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
484 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
486 char namewithdot[MAXHOSTNAMELEN + 1], *n;
487 const char *qtype, *qclass;
492 if (end - ptr >= sizeof namewithdot)
493 end = ptr + sizeof namewithdot - 1;
498 if (n != namewithdot)
506 if (log_IsKept(LogDNS)) {
507 memcpy(&tmp, end, sizeof tmp);
508 qtype = dns_Qtype2Txt(ntohs(tmp));
509 memcpy(&tmp, end + 2, sizeof tmp);
510 qclass = dns_Qclass2Txt(ntohs(tmp));
512 log_Printf(LogDNS, "%sbound query %s %s %s\n",
513 direction, qclass, qtype, namewithdot);
519 * Check if the given packet matches the given filter.
520 * One of pip or pip6 must be set.
523 PacketCheck(struct bundle *bundle, u_int32_t family,
524 const unsigned char *packet, int nb, struct filter *filter,
525 const char *prefix, unsigned *psecs)
527 static const char *const TcpFlags[] = {
528 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
530 const struct tcphdr *th;
531 const struct udphdr *uh;
532 const struct icmp *icmph;
534 const struct icmp6_hdr *icmp6h;
536 const unsigned char *payload;
537 struct ncpaddr srcaddr, dstaddr;
538 int cproto, mask, len, n, pri, logit, loglen, result;
543 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
544 (!filter || filter->logok);
549 if (family == AF_INET6) {
550 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
552 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
553 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
554 datalen = ntohs(pip6->ip6_plen);
555 payload = packet + sizeof *pip6;
556 cproto = pip6->ip6_nxt;
557 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
558 frag = 0; /* XXX: ??? */
562 const struct ip *pip = (const struct ip *)packet;
564 ncpaddr_setip4(&srcaddr, pip->ip_src);
565 ncpaddr_setip4(&dstaddr, pip->ip_dst);
566 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
567 payload = packet + (pip->ip_hl << 2);
570 frag = ntohs(pip->ip_off) & IP_OFFMASK;
575 if (logit && loglen < sizeof logbuf) {
577 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
579 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
581 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
582 loglen += strlen(logbuf + loglen);
587 if (logit && loglen < sizeof logbuf) {
588 len = datalen - sizeof *icmph;
589 icmph = (const struct icmp *)payload;
590 snprintf(logbuf + loglen, sizeof logbuf - loglen,
591 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
592 loglen += strlen(logbuf + loglen);
593 snprintf(logbuf + loglen, sizeof logbuf - loglen,
594 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
595 loglen += strlen(logbuf + loglen);
601 if (logit && loglen < sizeof logbuf) {
602 len = datalen - sizeof *icmp6h;
603 icmp6h = (const struct icmp6_hdr *)payload;
604 snprintf(logbuf + loglen, sizeof logbuf - loglen,
605 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
606 loglen += strlen(logbuf + loglen);
607 snprintf(logbuf + loglen, sizeof logbuf - loglen,
608 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
609 loglen += strlen(logbuf + loglen);
615 uh = (const struct udphdr *)payload;
616 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
619 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
620 ntohs(uh->uh_dport)))
623 if (logit && loglen < sizeof logbuf) {
624 len = datalen - sizeof *uh;
625 snprintf(logbuf + loglen, sizeof logbuf - loglen,
626 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
627 loglen += strlen(logbuf + loglen);
628 snprintf(logbuf + loglen, sizeof logbuf - loglen,
629 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
631 loglen += strlen(logbuf + loglen);
634 if (Enabled(bundle, OPT_FILTERDECAP) &&
635 payload[sizeof *uh] == HDLC_ADDR &&
636 payload[sizeof *uh + 1] == HDLC_UI) {
640 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
643 switch (ntohs(proto)) {
645 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
646 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
647 nb - (payload - packet) - sizeof *uh - 4, filter,
654 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
655 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
656 case PROTO_MP: type = "Multi-link"; break;
657 case PROTO_ICOMPD: type = "Individual link CCP"; break;
658 case PROTO_COMPD: type = "CCP"; break;
659 case PROTO_IPCP: type = "IPCP"; break;
660 case PROTO_LCP: type = "LCP"; break;
661 case PROTO_PAP: type = "PAP"; break;
662 case PROTO_CBCP: type = "CBCP"; break;
663 case PROTO_LQR: type = "LQR"; break;
664 case PROTO_CHAP: type = "CHAP"; break;
667 snprintf(logbuf + loglen, sizeof logbuf - loglen,
669 loglen += strlen(logbuf + loglen);
677 if (logit && loglen < sizeof logbuf) {
678 snprintf(logbuf + loglen, sizeof logbuf - loglen,
679 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
680 loglen += strlen(logbuf + loglen);
681 snprintf(logbuf + loglen, sizeof logbuf - loglen,
682 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
683 loglen += strlen(logbuf + loglen);
688 #ifdef IPPROTO_OSPFIGP
689 case IPPROTO_OSPFIGP:
690 if (logit && loglen < sizeof logbuf) {
691 snprintf(logbuf + loglen, sizeof logbuf - loglen,
692 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
693 loglen += strlen(logbuf + loglen);
694 snprintf(logbuf + loglen, sizeof logbuf - loglen,
695 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
696 loglen += strlen(logbuf + loglen);
703 if (logit && loglen < sizeof logbuf) {
704 snprintf(logbuf + loglen, sizeof logbuf - loglen,
705 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
706 loglen += strlen(logbuf + loglen);
707 snprintf(logbuf + loglen, sizeof logbuf - loglen,
708 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
709 loglen += strlen(logbuf + loglen);
712 if (Enabled(bundle, OPT_FILTERDECAP)) {
713 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
714 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
715 filter, logbuf, psecs);
723 if (logit && loglen < sizeof logbuf) {
724 snprintf(logbuf + loglen, sizeof logbuf - loglen,
725 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
726 loglen += strlen(logbuf + loglen);
727 snprintf(logbuf + loglen, sizeof logbuf - loglen,
728 "%s", ncpaddr_ntoa(&dstaddr));
729 loglen += strlen(logbuf + loglen);
732 if (Enabled(bundle, OPT_FILTERDECAP) &&
733 ((const struct ip *)payload)->ip_v == 4) {
734 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
735 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
736 filter, logbuf, psecs);
743 if (logit && loglen < sizeof logbuf) {
744 snprintf(logbuf + loglen, sizeof logbuf - loglen,
745 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
746 loglen += strlen(logbuf + loglen);
747 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
748 ncpaddr_ntoa(&dstaddr), payload);
749 loglen += strlen(logbuf + loglen);
754 if (logit && loglen < sizeof logbuf) {
755 snprintf(logbuf + loglen, sizeof logbuf - loglen,
756 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
757 loglen += strlen(logbuf + loglen);
758 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
759 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
760 loglen += strlen(logbuf + loglen);
765 if (logit && loglen < sizeof logbuf) {
766 uh = (const struct udphdr *)payload;
767 snprintf(logbuf + loglen, sizeof logbuf - loglen,
768 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
769 ntohs(uh->uh_sport));
770 loglen += strlen(logbuf + loglen);
771 snprintf(logbuf + loglen, sizeof logbuf - loglen,
772 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
773 loglen += strlen(logbuf + loglen);
778 th = (const struct tcphdr *)payload;
779 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
782 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
783 ntohs(th->th_dport)))
786 if (logit && loglen < sizeof logbuf) {
787 len = datalen - (th->th_off << 2);
788 snprintf(logbuf + loglen, sizeof logbuf - loglen,
789 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
790 loglen += strlen(logbuf + loglen);
791 snprintf(logbuf + loglen, sizeof logbuf - loglen,
792 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
793 loglen += strlen(logbuf + loglen);
795 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
796 if (th->th_flags & mask) {
797 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
798 loglen += strlen(logbuf + loglen);
802 snprintf(logbuf + loglen, sizeof logbuf - loglen,
803 " seq:%lx ack:%lx (%d/%d)",
804 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
805 loglen += strlen(logbuf + loglen);
806 if ((th->th_flags & TH_SYN) && nb > 40) {
809 sp = (const u_short *)(payload + 20);
810 if (ntohs(sp[0]) == 0x0204) {
811 snprintf(logbuf + loglen, sizeof logbuf - loglen,
812 " MSS = %d", ntohs(sp[1]));
813 loglen += strlen(logbuf + loglen);
823 if (logit && loglen < sizeof logbuf) {
824 snprintf(logbuf + loglen, sizeof logbuf - loglen,
825 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
826 loglen += strlen(logbuf + loglen);
827 snprintf(logbuf + loglen, sizeof logbuf - loglen,
828 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
829 loglen += strlen(logbuf + loglen);
834 if (filter && FilterCheck(packet, family, filter, psecs)) {
836 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
839 /* Check Keep Alive filter */
840 if (logit && log_IsKept(LogTCPIP)) {
845 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
846 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
847 else if (psecs != NULL) {
851 if (*psecs != alivesecs)
852 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
853 logbuf, *psecs, alivesecs);
855 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
857 log_Printf(LogTCPIP, "%s\n", logbuf);
863 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
864 ip_LogDNS(uh, filter->name);
870 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
875 unsigned secs, alivesecs;
878 if (nb > sizeof tun.data) {
879 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
880 l->name, nb, (int)(sizeof tun.data));
884 mbuf_Read(bp, tun.data, nb);
887 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
892 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
895 bundle_StartIdleTimer(bundle, secs);
898 if (bundle->dev.header) {
899 tun.header.family = htonl(af);
900 nb += sizeof tun - sizeof tun.data;
905 nw = write(bundle->dev.fd, data, nb);
908 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
909 l->name, nb, strerror(errno));
911 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
918 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
922 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
923 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
928 m_settype(bp, MB_IPIN);
930 nb = ip_Input(bundle, l, bp, AF_INET);
931 ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
938 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
942 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
943 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
948 m_settype(bp, MB_IPV6IN);
950 nb = ip_Input(bundle, l, bp, AF_INET6);
951 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);