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 $
29 * $DragonFly: src/usr.sbin/ppp/ip.c,v 1.3 2004/08/09 19:54:36 dillon Exp $
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>
38 #include <netinet/icmp6.h>
39 #include <netinet/ip6.h>
41 #include <netinet/ip_icmp.h>
42 #include <netinet/udp.h>
43 #include <netinet/tcp.h>
62 #include "throughput.h"
64 #include "slcompress.h"
69 #include "descriptor.h"
83 #define OPCODE_QUERY 0
84 #define OPCODE_IQUERY 1
85 #define OPCODE_STATUS 2
104 dns_Qclass2Txt(u_short qclass)
106 static char failure[6];
112 { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" }
116 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
117 if (qtxt[f].id == qclass)
120 return HexStr(qclass, failure, sizeof failure);
124 dns_Qtype2Txt(u_short qtype)
126 static char failure[6];
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, "*" }
143 for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
144 if (qtxt[f].id == qtype)
147 return HexStr(qtype, failure, sizeof failure);
151 PortMatch(int op, u_short pport, u_short rport)
155 return pport == rport;
157 return pport > rport;
159 return pport < rport;
166 toprototxt(int cproto)
168 static char prototxt[16];
171 if ((pe = getprotobynumber(cproto)) == NULL)
172 snprintf(prototxt, sizeof(prototxt), "%d", cproto);
174 snprintf(prototxt, sizeof(prototxt), "%s", pe->p_name);
179 * Check a packet against the given filter
180 * Returns 0 to accept the packet, non-zero to drop the packet.
181 * If psecs is not NULL, populate it with the timeout associated
182 * with the filter rule matched.
184 * If filtering is enabled, the initial fragment of a datagram must
185 * contain the complete protocol header, and subsequent fragments
186 * must not attempt to over-write it.
188 * One (and only one) of pip or pip6 must be set.
191 FilterCheck(const unsigned char *packet, u_int32_t family,
192 const struct filter *filter, unsigned *psecs)
194 int gotinfo; /* true if IP payload decoded */
195 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
196 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
197 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
198 int n; /* filter rule to process */
199 int len; /* bytes used in dbuff */
200 int didname; /* true if filter header printed */
201 int match; /* true if condition matched */
202 int mindata; /* minimum data size or zero */
203 const struct filterent *fp = filter->rule;
204 char dbuff[100], dstip[16];
206 struct ncpaddr srcaddr, dstaddr;
207 const char *payload; /* IP payload */
208 int datalen; /* IP datagram length */
210 if (fp->f_action == A_NONE)
211 return 0; /* No rule is given. Permit this packet */
214 if (family == AF_INET6) {
215 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
217 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
218 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
219 datalen = ntohs(pip6->ip6_plen);
220 payload = packet + sizeof *pip6;
221 cproto = pip6->ip6_nxt;
226 * Deny any packet fragment that tries to over-write the header.
227 * Since we no longer have the real header available, punt on the
228 * largest normal header - 20 bytes for TCP without options, rounded
229 * up to the next possible fragment boundary. Since the smallest
230 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
231 * fragmentation within this range is dubious at best
233 const struct ip *pip = (const struct ip *)packet;
235 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
236 if (len > 0) { /* Not first fragment within datagram */
237 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
238 log_Printf(LogFILTER, " error: illegal header\n");
241 /* permit fragments on in and out filter */
242 if (!filter->fragok) {
243 log_Printf(LogFILTER, " error: illegal fragmentation\n");
249 ncpaddr_setip4(&srcaddr, pip->ip_src);
250 ncpaddr_setip4(&dstaddr, pip->ip_dst);
251 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
252 payload = packet + (pip->ip_hl << 2);
256 gotinfo = estab = syn = finrst = didname = 0;
259 for (n = 0; n < MAXFILTERS; ) {
260 if (fp->f_action == A_NONE) {
267 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
273 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
274 ncprange_contains(&fp->f_src, &srcaddr)) &&
275 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
276 ncprange_contains(&fp->f_dst, &dstaddr))) {
277 if (fp->f_proto != 0) {
279 const struct tcphdr *th;
280 const struct udphdr *uh;
281 const struct icmp *ih;
283 const struct icmp6_hdr *ih6;
287 estab = syn = finrst = -1;
291 mindata = 8; /* ICMP must be at least 8 octets */
292 ih = (const struct icmp *)payload;
293 sport = ntohs(ih->icmp_type);
294 if (log_IsKept(LogDEBUG))
295 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
300 mindata = 8; /* ICMP must be at least 8 octets */
301 ih6 = (const struct icmp6_hdr *)payload;
302 sport = ntohs(ih6->icmp6_type);
303 if (log_IsKept(LogDEBUG))
304 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
309 mindata = 8; /* IGMP uses 8-octet messages */
314 mindata = 2; /* GRE uses 2-octet+ messages */
317 #ifdef IPPROTO_OSPFIGP
318 case IPPROTO_OSPFIGP:
319 mindata = 8; /* IGMP uses 8-octet messages */
324 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
329 mindata = 8; /* UDP header is 8 octets */
330 uh = (const struct udphdr *)payload;
331 sport = ntohs(uh->uh_sport);
332 dport = ntohs(uh->uh_dport);
333 if (log_IsKept(LogDEBUG))
334 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
339 th = (const struct tcphdr *)payload;
341 * TCP headers are variable length. The following code
342 * ensures that the TCP header length isn't de-referenced if
343 * the datagram is too short
345 if (datalen < 20 || datalen < (th->th_off << 2)) {
346 log_Printf(LogFILTER, " error: TCP header incorrect\n");
349 sport = ntohs(th->th_sport);
350 dport = ntohs(th->th_dport);
351 estab = (th->th_flags & TH_ACK);
352 syn = (th->th_flags & TH_SYN);
353 finrst = (th->th_flags & (TH_FIN|TH_RST));
354 if (log_IsKept(LogDEBUG)) {
356 snprintf(dbuff, sizeof dbuff,
357 "flags = %02x, sport = %d, dport = %d",
358 th->th_flags, sport, dport);
367 if (datalen < mindata) {
368 log_Printf(LogFILTER, " error: proto %s must be at least"
369 " %d octets\n", toprototxt(cproto), mindata);
373 if (log_IsKept(LogDEBUG)) {
376 snprintf(dbuff + len, sizeof dbuff - len,
377 ", estab = %d, syn = %d, finrst = %d",
380 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", toprototxt(cproto), dbuff);
385 if (log_IsKept(LogDEBUG)) {
386 if (fp->f_srcop != OP_NONE) {
387 snprintf(dbuff, sizeof dbuff, ", src %s %d",
388 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
392 if (fp->f_dstop != OP_NONE) {
393 snprintf(dbuff + len, sizeof dbuff - len,
394 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
399 log_Printf(LogDEBUG, " rule = %d: Address match, "
400 "check against proto %d%s, action = %s\n",
401 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
404 if (cproto == fp->f_proto) {
405 if ((fp->f_srcop == OP_NONE ||
406 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
407 (fp->f_dstop == OP_NONE ||
408 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
409 (fp->f_estab == 0 || estab) &&
410 (fp->f_syn == 0 || syn) &&
411 (fp->f_finrst == 0 || finrst)) {
416 /* Address is matched and no protocol specified. Make a decision. */
417 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
418 filter_Action2Nam(fp->f_action));
422 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
424 if (match != fp->f_invert) {
425 /* Take specified action */
426 if (fp->f_action < A_NONE)
427 fp = &filter->rule[n = fp->f_action];
429 if (fp->f_action == A_PERMIT) {
431 *psecs = fp->timeout;
432 if (strcmp(filter->name, "DIAL") == 0) {
433 /* If dial filter then even print out accept packets */
434 if (log_IsKept(LogFILTER)) {
435 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
436 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
437 "src = %s:%d dst = %s:%d\n", filter->name, n,
439 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
444 if (log_IsKept(LogFILTER)) {
445 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
446 log_Printf(LogFILTER,
447 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
448 filter->name, n, toprototxt(cproto),
449 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
452 } /* Explict match. Deny this packet */
460 if (log_IsKept(LogFILTER)) {
461 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
462 log_Printf(LogFILTER,
463 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
464 filter->name, toprototxt(cproto), ncpaddr_ntoa(&srcaddr), sport,
468 return 1; /* No rule matched, deny this packet */
472 ip_LogDNS(const struct udphdr *uh, const char *direction)
474 struct dns_header header;
475 const u_short *pktptr;
480 ptr = (const char *)uh + sizeof *uh;
481 len = ntohs(uh->uh_ulen) - sizeof *uh;
482 if (len < sizeof header + 5) /* rfc1024 */
485 pktptr = (const u_short *)ptr;
486 hptr = (u_short *)&header;
487 ptr += sizeof header;
488 len -= sizeof header;
490 while (pktptr < (const u_short *)ptr) {
491 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
495 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
497 char namewithdot[MAXHOSTNAMELEN + 1], *n;
498 const char *qtype, *qclass;
503 if (end - ptr >= sizeof namewithdot)
504 end = ptr + sizeof namewithdot - 1;
509 if (n != namewithdot)
517 if (log_IsKept(LogDNS)) {
518 memcpy(&tmp, end, sizeof tmp);
519 qtype = dns_Qtype2Txt(ntohs(tmp));
520 memcpy(&tmp, end + 2, sizeof tmp);
521 qclass = dns_Qclass2Txt(ntohs(tmp));
523 log_Printf(LogDNS, "%sbound query %s %s %s\n",
524 direction, qclass, qtype, namewithdot);
530 * Check if the given packet matches the given filter.
531 * One of pip or pip6 must be set.
534 PacketCheck(struct bundle *bundle, u_int32_t family,
535 const unsigned char *packet, int nb, struct filter *filter,
536 const char *prefix, unsigned *psecs)
538 static const char *const TcpFlags[] = {
539 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
541 const struct tcphdr *th;
542 const struct udphdr *uh;
543 const struct icmp *icmph;
545 const struct icmp6_hdr *icmp6h;
547 const unsigned char *payload;
548 struct ncpaddr srcaddr, dstaddr;
549 int cproto, mask, len, n, pri, logit, loglen, result;
554 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
555 (!filter || filter->logok);
560 if (family == AF_INET6) {
561 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
563 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
564 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
565 datalen = ntohs(pip6->ip6_plen);
566 payload = packet + sizeof *pip6;
567 cproto = pip6->ip6_nxt;
568 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
569 frag = 0; /* XXX: ??? */
573 const struct ip *pip = (const struct ip *)packet;
575 ncpaddr_setip4(&srcaddr, pip->ip_src);
576 ncpaddr_setip4(&dstaddr, pip->ip_dst);
577 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
578 payload = packet + (pip->ip_hl << 2);
581 frag = ntohs(pip->ip_off) & IP_OFFMASK;
586 if (logit && loglen < sizeof logbuf) {
588 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
590 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
592 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
593 loglen += strlen(logbuf + loglen);
598 if (logit && loglen < sizeof logbuf) {
599 len = datalen - sizeof *icmph;
600 icmph = (const struct icmp *)payload;
601 snprintf(logbuf + loglen, sizeof logbuf - loglen,
602 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
603 loglen += strlen(logbuf + loglen);
604 snprintf(logbuf + loglen, sizeof logbuf - loglen,
605 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
606 loglen += strlen(logbuf + loglen);
612 if (logit && loglen < sizeof logbuf) {
613 len = datalen - sizeof *icmp6h;
614 icmp6h = (const struct icmp6_hdr *)payload;
615 snprintf(logbuf + loglen, sizeof logbuf - loglen,
616 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
617 loglen += strlen(logbuf + loglen);
618 snprintf(logbuf + loglen, sizeof logbuf - loglen,
619 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
620 loglen += strlen(logbuf + loglen);
626 uh = (const struct udphdr *)payload;
627 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
630 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
631 ntohs(uh->uh_dport)))
634 if (logit && loglen < sizeof logbuf) {
635 len = datalen - sizeof *uh;
636 snprintf(logbuf + loglen, sizeof logbuf - loglen,
637 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
638 loglen += strlen(logbuf + loglen);
639 snprintf(logbuf + loglen, sizeof logbuf - loglen,
640 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
642 loglen += strlen(logbuf + loglen);
645 if (Enabled(bundle, OPT_FILTERDECAP) &&
646 payload[sizeof *uh] == HDLC_ADDR &&
647 payload[sizeof *uh + 1] == HDLC_UI) {
651 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
654 switch (ntohs(proto)) {
656 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
657 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
658 nb - (payload - packet) - sizeof *uh - 4, filter,
665 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
666 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
667 case PROTO_MP: type = "Multi-link"; break;
668 case PROTO_ICOMPD: type = "Individual link CCP"; break;
669 case PROTO_COMPD: type = "CCP"; break;
670 case PROTO_IPCP: type = "IPCP"; break;
671 case PROTO_LCP: type = "LCP"; break;
672 case PROTO_PAP: type = "PAP"; break;
673 case PROTO_CBCP: type = "CBCP"; break;
674 case PROTO_LQR: type = "LQR"; break;
675 case PROTO_CHAP: type = "CHAP"; break;
678 snprintf(logbuf + loglen, sizeof logbuf - loglen,
680 loglen += strlen(logbuf + loglen);
688 if (logit && loglen < sizeof logbuf) {
689 snprintf(logbuf + loglen, sizeof logbuf - loglen,
690 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
691 loglen += strlen(logbuf + loglen);
692 snprintf(logbuf + loglen, sizeof logbuf - loglen,
693 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
694 loglen += strlen(logbuf + loglen);
699 #ifdef IPPROTO_OSPFIGP
700 case IPPROTO_OSPFIGP:
701 if (logit && loglen < sizeof logbuf) {
702 snprintf(logbuf + loglen, sizeof logbuf - loglen,
703 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
704 loglen += strlen(logbuf + loglen);
705 snprintf(logbuf + loglen, sizeof logbuf - loglen,
706 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
707 loglen += strlen(logbuf + loglen);
714 if (logit && loglen < sizeof logbuf) {
715 snprintf(logbuf + loglen, sizeof logbuf - loglen,
716 "IPv6: %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);
723 if (Enabled(bundle, OPT_FILTERDECAP)) {
724 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
725 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
726 filter, logbuf, psecs);
734 if (logit && loglen < sizeof logbuf) {
735 snprintf(logbuf + loglen, sizeof logbuf - loglen,
736 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
737 loglen += strlen(logbuf + loglen);
738 snprintf(logbuf + loglen, sizeof logbuf - loglen,
739 "%s", ncpaddr_ntoa(&dstaddr));
740 loglen += strlen(logbuf + loglen);
743 if (Enabled(bundle, OPT_FILTERDECAP) &&
744 ((const struct ip *)payload)->ip_v == 4) {
745 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
746 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
747 filter, logbuf, psecs);
754 if (logit && loglen < sizeof logbuf) {
755 snprintf(logbuf + loglen, sizeof logbuf - loglen,
756 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
757 loglen += strlen(logbuf + loglen);
758 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
759 ncpaddr_ntoa(&dstaddr), payload);
760 loglen += strlen(logbuf + loglen);
765 if (logit && loglen < sizeof logbuf) {
766 snprintf(logbuf + loglen, sizeof logbuf - loglen,
767 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
768 loglen += strlen(logbuf + loglen);
769 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
770 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
771 loglen += strlen(logbuf + loglen);
776 if (logit && loglen < sizeof logbuf) {
777 uh = (const struct udphdr *)payload;
778 snprintf(logbuf + loglen, sizeof logbuf - loglen,
779 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
780 ntohs(uh->uh_sport));
781 loglen += strlen(logbuf + loglen);
782 snprintf(logbuf + loglen, sizeof logbuf - loglen,
783 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
784 loglen += strlen(logbuf + loglen);
789 th = (const struct tcphdr *)payload;
790 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
793 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
794 ntohs(th->th_dport)))
797 if (logit && loglen < sizeof logbuf) {
798 len = datalen - (th->th_off << 2);
799 snprintf(logbuf + loglen, sizeof logbuf - loglen,
800 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
801 loglen += strlen(logbuf + loglen);
802 snprintf(logbuf + loglen, sizeof logbuf - loglen,
803 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
804 loglen += strlen(logbuf + loglen);
806 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
807 if (th->th_flags & mask) {
808 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
809 loglen += strlen(logbuf + loglen);
813 snprintf(logbuf + loglen, sizeof logbuf - loglen,
814 " seq:%lx ack:%lx (%d/%d)",
815 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
816 loglen += strlen(logbuf + loglen);
817 if ((th->th_flags & TH_SYN) && nb > 40) {
820 sp = (const u_short *)(payload + 20);
821 if (ntohs(sp[0]) == 0x0204) {
822 snprintf(logbuf + loglen, sizeof logbuf - loglen,
823 " MSS = %d", ntohs(sp[1]));
824 loglen += strlen(logbuf + loglen);
834 if (logit && loglen < sizeof logbuf) {
835 snprintf(logbuf + loglen, sizeof logbuf - loglen,
836 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
837 loglen += strlen(logbuf + loglen);
838 snprintf(logbuf + loglen, sizeof logbuf - loglen,
839 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
840 loglen += strlen(logbuf + loglen);
845 if (filter && FilterCheck(packet, family, filter, psecs)) {
847 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
850 /* Check Keep Alive filter */
851 if (logit && log_IsKept(LogTCPIP)) {
856 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
857 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
858 else if (psecs != NULL) {
862 if (*psecs != alivesecs)
863 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
864 logbuf, *psecs, alivesecs);
866 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
868 log_Printf(LogTCPIP, "%s\n", logbuf);
874 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
875 ip_LogDNS(uh, filter->name);
881 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 %d, 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);
919 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
920 l->name, nb, strerror(errno));
922 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\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);