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.2 2003/06/17 04:30:00 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 * Check a packet against the given filter
167 * Returns 0 to accept the packet, non-zero to drop the packet.
168 * If psecs is not NULL, populate it with the timeout associated
169 * with the filter rule matched.
171 * If filtering is enabled, the initial fragment of a datagram must
172 * contain the complete protocol header, and subsequent fragments
173 * must not attempt to over-write it.
175 * One (and only one) of pip or pip6 must be set.
178 FilterCheck(const unsigned char *packet, u_int32_t family,
179 const struct filter *filter, unsigned *psecs)
181 int gotinfo; /* true if IP payload decoded */
182 int cproto; /* IPPROTO_* protocol number if (gotinfo) */
183 int estab, syn, finrst; /* TCP state flags if (gotinfo) */
184 u_short sport, dport; /* src, dest port from packet if (gotinfo) */
185 int n; /* filter rule to process */
186 int len; /* bytes used in dbuff */
187 int didname; /* true if filter header printed */
188 int match; /* true if condition matched */
189 int mindata; /* minimum data size or zero */
190 const struct filterent *fp = filter->rule;
191 char dbuff[100], dstip[16], prototxt[16];
193 struct ncpaddr srcaddr, dstaddr;
194 const char *payload; /* IP payload */
195 int datalen; /* IP datagram length */
197 if (fp->f_action == A_NONE)
198 return 0; /* No rule is given. Permit this packet */
201 if (family == AF_INET6) {
202 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
204 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
205 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
206 datalen = ntohs(pip6->ip6_plen);
207 payload = packet + sizeof *pip6;
208 cproto = pip6->ip6_nxt;
213 * Deny any packet fragment that tries to over-write the header.
214 * Since we no longer have the real header available, punt on the
215 * largest normal header - 20 bytes for TCP without options, rounded
216 * up to the next possible fragment boundary. Since the smallest
217 * `legal' MTU is 576, and the smallest recommended MTU is 296, any
218 * fragmentation within this range is dubious at best
220 const struct ip *pip = (const struct ip *)packet;
222 len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
223 if (len > 0) { /* Not first fragment within datagram */
224 if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
225 log_Printf(LogFILTER, " error: illegal header\n");
228 /* permit fragments on in and out filter */
229 if (!filter->fragok) {
230 log_Printf(LogFILTER, " error: illegal fragmentation\n");
236 ncpaddr_setip4(&srcaddr, pip->ip_src);
237 ncpaddr_setip4(&dstaddr, pip->ip_dst);
238 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
239 payload = packet + (pip->ip_hl << 2);
243 if ((pe = getprotobynumber(cproto)) == NULL)
244 snprintf(prototxt, sizeof prototxt, "%d", cproto);
246 snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
247 gotinfo = estab = syn = finrst = didname = 0;
250 for (n = 0; n < MAXFILTERS; ) {
251 if (fp->f_action == A_NONE) {
258 log_Printf(LogDEBUG, "%s filter:\n", filter->name);
264 if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
265 ncprange_contains(&fp->f_src, &srcaddr)) &&
266 (ncprange_family(&fp->f_dst) == AF_UNSPEC ||
267 ncprange_contains(&fp->f_dst, &dstaddr))) {
268 if (fp->f_proto != 0) {
270 const struct tcphdr *th;
271 const struct udphdr *uh;
272 const struct icmp *ih;
274 const struct icmp6_hdr *ih6;
278 estab = syn = finrst = -1;
282 mindata = 8; /* ICMP must be at least 8 octets */
283 ih = (const struct icmp *)payload;
284 sport = ntohs(ih->icmp_type);
285 if (log_IsKept(LogDEBUG))
286 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
291 mindata = 8; /* ICMP must be at least 8 octets */
292 ih6 = (const struct icmp6_hdr *)payload;
293 sport = ntohs(ih6->icmp6_type);
294 if (log_IsKept(LogDEBUG))
295 snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
300 mindata = 8; /* IGMP uses 8-octet messages */
305 mindata = 2; /* GRE uses 2-octet+ messages */
308 #ifdef IPPROTO_OSPFIGP
309 case IPPROTO_OSPFIGP:
310 mindata = 8; /* IGMP uses 8-octet messages */
315 mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
320 mindata = 8; /* UDP header is 8 octets */
321 uh = (const struct udphdr *)payload;
322 sport = ntohs(uh->uh_sport);
323 dport = ntohs(uh->uh_dport);
324 if (log_IsKept(LogDEBUG))
325 snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
330 th = (const struct tcphdr *)payload;
332 * TCP headers are variable length. The following code
333 * ensures that the TCP header length isn't de-referenced if
334 * the datagram is too short
336 if (datalen < 20 || datalen < (th->th_off << 2)) {
337 log_Printf(LogFILTER, " error: TCP header incorrect\n");
340 sport = ntohs(th->th_sport);
341 dport = ntohs(th->th_dport);
342 estab = (th->th_flags & TH_ACK);
343 syn = (th->th_flags & TH_SYN);
344 finrst = (th->th_flags & (TH_FIN|TH_RST));
345 if (log_IsKept(LogDEBUG)) {
347 snprintf(dbuff, sizeof dbuff,
348 "flags = %02x, sport = %d, dport = %d",
349 th->th_flags, sport, dport);
358 if (datalen < mindata) {
359 log_Printf(LogFILTER, " error: proto %s must be at least"
360 " %d octets\n", prototxt, mindata);
364 if (log_IsKept(LogDEBUG)) {
367 snprintf(dbuff + len, sizeof dbuff - len,
368 ", estab = %d, syn = %d, finrst = %d",
371 log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff);
376 if (log_IsKept(LogDEBUG)) {
377 if (fp->f_srcop != OP_NONE) {
378 snprintf(dbuff, sizeof dbuff, ", src %s %d",
379 filter_Op2Nam(fp->f_srcop), fp->f_srcport);
383 if (fp->f_dstop != OP_NONE) {
384 snprintf(dbuff + len, sizeof dbuff - len,
385 ", dst %s %d", filter_Op2Nam(fp->f_dstop),
390 log_Printf(LogDEBUG, " rule = %d: Address match, "
391 "check against proto %d%s, action = %s\n",
392 n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
395 if (cproto == fp->f_proto) {
396 if ((fp->f_srcop == OP_NONE ||
397 PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
398 (fp->f_dstop == OP_NONE ||
399 PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
400 (fp->f_estab == 0 || estab) &&
401 (fp->f_syn == 0 || syn) &&
402 (fp->f_finrst == 0 || finrst)) {
407 /* Address is matched and no protocol specified. Make a decision. */
408 log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
409 filter_Action2Nam(fp->f_action));
413 log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
415 if (match != fp->f_invert) {
416 /* Take specified action */
417 if (fp->f_action < A_NONE)
418 fp = &filter->rule[n = fp->f_action];
420 if (fp->f_action == A_PERMIT) {
422 *psecs = fp->timeout;
423 if (strcmp(filter->name, "DIAL") == 0) {
424 /* If dial filter then even print out accept packets */
425 if (log_IsKept(LogFILTER)) {
426 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
427 log_Printf(LogFILTER, "%sbound rule = %d accept %s "
428 "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt,
429 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
434 if (log_IsKept(LogFILTER)) {
435 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
436 log_Printf(LogFILTER,
437 "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
438 filter->name, n, prototxt,
439 ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
442 } /* Explict match. Deny this packet */
450 if (log_IsKept(LogFILTER)) {
451 snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
452 log_Printf(LogFILTER,
453 "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
454 filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport,
458 return 1; /* No rule matched, deny this packet */
462 ip_LogDNS(const struct udphdr *uh, const char *direction)
464 struct dns_header header;
465 const u_short *pktptr;
470 ptr = (const char *)uh + sizeof *uh;
471 len = ntohs(uh->uh_ulen) - sizeof *uh;
472 if (len < sizeof header + 5) /* rfc1024 */
475 pktptr = (const u_short *)ptr;
476 hptr = (u_short *)&header;
477 ptr += sizeof header;
478 len -= sizeof header;
480 while (pktptr < (const u_short *)ptr) {
481 *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
485 if (header.opcode == OPCODE_QUERY && header.qr == 0) {
487 char namewithdot[MAXHOSTNAMELEN + 1], *n;
488 const char *qtype, *qclass;
493 if (end - ptr >= sizeof namewithdot)
494 end = ptr + sizeof namewithdot - 1;
499 if (n != namewithdot)
507 if (log_IsKept(LogDNS)) {
508 memcpy(&tmp, end, sizeof tmp);
509 qtype = dns_Qtype2Txt(ntohs(tmp));
510 memcpy(&tmp, end + 2, sizeof tmp);
511 qclass = dns_Qclass2Txt(ntohs(tmp));
513 log_Printf(LogDNS, "%sbound query %s %s %s\n",
514 direction, qclass, qtype, namewithdot);
520 * Check if the given packet matches the given filter.
521 * One of pip or pip6 must be set.
524 PacketCheck(struct bundle *bundle, u_int32_t family,
525 const unsigned char *packet, int nb, struct filter *filter,
526 const char *prefix, unsigned *psecs)
528 static const char *const TcpFlags[] = {
529 "FIN", "SYN", "RST", "PSH", "ACK", "URG"
531 const struct tcphdr *th;
532 const struct udphdr *uh;
533 const struct icmp *icmph;
535 const struct icmp6_hdr *icmp6h;
537 const unsigned char *payload;
538 struct ncpaddr srcaddr, dstaddr;
539 int cproto, mask, len, n, pri, logit, loglen, result;
544 logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
545 (!filter || filter->logok);
550 if (family == AF_INET6) {
551 const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
553 ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
554 ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
555 datalen = ntohs(pip6->ip6_plen);
556 payload = packet + sizeof *pip6;
557 cproto = pip6->ip6_nxt;
558 tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
559 frag = 0; /* XXX: ??? */
563 const struct ip *pip = (const struct ip *)packet;
565 ncpaddr_setip4(&srcaddr, pip->ip_src);
566 ncpaddr_setip4(&dstaddr, pip->ip_dst);
567 datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
568 payload = packet + (pip->ip_hl << 2);
571 frag = ntohs(pip->ip_off) & IP_OFFMASK;
576 if (logit && loglen < sizeof logbuf) {
578 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
580 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
582 snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
583 loglen += strlen(logbuf + loglen);
588 if (logit && loglen < sizeof logbuf) {
589 len = datalen - sizeof *icmph;
590 icmph = (const struct icmp *)payload;
591 snprintf(logbuf + loglen, sizeof logbuf - loglen,
592 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
593 loglen += strlen(logbuf + loglen);
594 snprintf(logbuf + loglen, sizeof logbuf - loglen,
595 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
596 loglen += strlen(logbuf + loglen);
602 if (logit && loglen < sizeof logbuf) {
603 len = datalen - sizeof *icmp6h;
604 icmp6h = (const struct icmp6_hdr *)payload;
605 snprintf(logbuf + loglen, sizeof logbuf - loglen,
606 "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
607 loglen += strlen(logbuf + loglen);
608 snprintf(logbuf + loglen, sizeof logbuf - loglen,
609 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
610 loglen += strlen(logbuf + loglen);
616 uh = (const struct udphdr *)payload;
617 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
620 if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
621 ntohs(uh->uh_dport)))
624 if (logit && loglen < sizeof logbuf) {
625 len = datalen - sizeof *uh;
626 snprintf(logbuf + loglen, sizeof logbuf - loglen,
627 "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
628 loglen += strlen(logbuf + loglen);
629 snprintf(logbuf + loglen, sizeof logbuf - loglen,
630 "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
632 loglen += strlen(logbuf + loglen);
635 if (Enabled(bundle, OPT_FILTERDECAP) &&
636 payload[sizeof *uh] == HDLC_ADDR &&
637 payload[sizeof *uh + 1] == HDLC_UI) {
641 memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
644 switch (ntohs(proto)) {
646 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
647 result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
648 nb - (payload - packet) - sizeof *uh - 4, filter,
655 case PROTO_VJUNCOMP: type = "compressed VJ"; break;
656 case PROTO_VJCOMP: type = "uncompressed VJ"; break;
657 case PROTO_MP: type = "Multi-link"; break;
658 case PROTO_ICOMPD: type = "Individual link CCP"; break;
659 case PROTO_COMPD: type = "CCP"; break;
660 case PROTO_IPCP: type = "IPCP"; break;
661 case PROTO_LCP: type = "LCP"; break;
662 case PROTO_PAP: type = "PAP"; break;
663 case PROTO_CBCP: type = "CBCP"; break;
664 case PROTO_LQR: type = "LQR"; break;
665 case PROTO_CHAP: type = "CHAP"; break;
668 snprintf(logbuf + loglen, sizeof logbuf - loglen,
670 loglen += strlen(logbuf + loglen);
678 if (logit && loglen < sizeof logbuf) {
679 snprintf(logbuf + loglen, sizeof logbuf - loglen,
680 "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
681 loglen += strlen(logbuf + loglen);
682 snprintf(logbuf + loglen, sizeof logbuf - loglen,
683 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
684 loglen += strlen(logbuf + loglen);
689 #ifdef IPPROTO_OSPFIGP
690 case IPPROTO_OSPFIGP:
691 if (logit && loglen < sizeof logbuf) {
692 snprintf(logbuf + loglen, sizeof logbuf - loglen,
693 "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
694 loglen += strlen(logbuf + loglen);
695 snprintf(logbuf + loglen, sizeof logbuf - loglen,
696 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
697 loglen += strlen(logbuf + loglen);
704 if (logit && loglen < sizeof logbuf) {
705 snprintf(logbuf + loglen, sizeof logbuf - loglen,
706 "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
707 loglen += strlen(logbuf + loglen);
708 snprintf(logbuf + loglen, sizeof logbuf - loglen,
709 "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
710 loglen += strlen(logbuf + loglen);
713 if (Enabled(bundle, OPT_FILTERDECAP)) {
714 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
715 result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
716 filter, logbuf, psecs);
724 if (logit && loglen < sizeof logbuf) {
725 snprintf(logbuf + loglen, sizeof logbuf - loglen,
726 "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
727 loglen += strlen(logbuf + loglen);
728 snprintf(logbuf + loglen, sizeof logbuf - loglen,
729 "%s", ncpaddr_ntoa(&dstaddr));
730 loglen += strlen(logbuf + loglen);
733 if (Enabled(bundle, OPT_FILTERDECAP) &&
734 ((const struct ip *)payload)->ip_v == 4) {
735 snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
736 result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
737 filter, logbuf, psecs);
744 if (logit && loglen < sizeof logbuf) {
745 snprintf(logbuf + loglen, sizeof logbuf - loglen,
746 "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
747 loglen += strlen(logbuf + loglen);
748 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
749 ncpaddr_ntoa(&dstaddr), payload);
750 loglen += strlen(logbuf + loglen);
755 if (logit && loglen < sizeof logbuf) {
756 snprintf(logbuf + loglen, sizeof logbuf - loglen,
757 "AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
758 loglen += strlen(logbuf + loglen);
759 snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
760 ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
761 loglen += strlen(logbuf + loglen);
766 if (logit && loglen < sizeof logbuf) {
767 uh = (const struct udphdr *)payload;
768 snprintf(logbuf + loglen, sizeof logbuf - loglen,
769 "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
770 ntohs(uh->uh_sport));
771 loglen += strlen(logbuf + loglen);
772 snprintf(logbuf + loglen, sizeof logbuf - loglen,
773 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
774 loglen += strlen(logbuf + loglen);
779 th = (const struct tcphdr *)payload;
780 if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
783 if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
784 ntohs(th->th_dport)))
787 if (logit && loglen < sizeof logbuf) {
788 len = datalen - (th->th_off << 2);
789 snprintf(logbuf + loglen, sizeof logbuf - loglen,
790 "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
791 loglen += strlen(logbuf + loglen);
792 snprintf(logbuf + loglen, sizeof logbuf - loglen,
793 "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
794 loglen += strlen(logbuf + loglen);
796 for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
797 if (th->th_flags & mask) {
798 snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
799 loglen += strlen(logbuf + loglen);
803 snprintf(logbuf + loglen, sizeof logbuf - loglen,
804 " seq:%lx ack:%lx (%d/%d)",
805 (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
806 loglen += strlen(logbuf + loglen);
807 if ((th->th_flags & TH_SYN) && nb > 40) {
810 sp = (const u_short *)(payload + 20);
811 if (ntohs(sp[0]) == 0x0204) {
812 snprintf(logbuf + loglen, sizeof logbuf - loglen,
813 " MSS = %d", ntohs(sp[1]));
814 loglen += strlen(logbuf + loglen);
824 if (logit && loglen < sizeof logbuf) {
825 snprintf(logbuf + loglen, sizeof logbuf - loglen,
826 "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
827 loglen += strlen(logbuf + loglen);
828 snprintf(logbuf + loglen, sizeof logbuf - loglen,
829 "%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
830 loglen += strlen(logbuf + loglen);
835 if (filter && FilterCheck(packet, family, filter, psecs)) {
837 log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
840 /* Check Keep Alive filter */
841 if (logit && log_IsKept(LogTCPIP)) {
846 FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
847 log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
848 else if (psecs != NULL) {
852 if (*psecs != alivesecs)
853 log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
854 logbuf, *psecs, alivesecs);
856 log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
858 log_Printf(LogTCPIP, "%s\n", logbuf);
864 if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
865 ip_LogDNS(uh, filter->name);
871 ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
876 unsigned secs, alivesecs;
879 if (nb > sizeof tun.data) {
880 log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
881 l->name, nb, (int)(sizeof tun.data));
885 mbuf_Read(bp, tun.data, nb);
888 if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
893 if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
896 bundle_StartIdleTimer(bundle, secs);
899 if (bundle->dev.header) {
900 tun.header.family = htonl(af);
901 nb += sizeof tun - sizeof tun.data;
906 nw = write(bundle->dev.fd, data, nb);
909 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
910 l->name, nb, strerror(errno));
912 log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
919 ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
923 if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
924 log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
929 m_settype(bp, MB_IPIN);
931 nb = ip_Input(bundle, l, bp, AF_INET);
932 ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
939 ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
943 if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
944 log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
949 m_settype(bp, MB_IPV6IN);
951 nb = ip_Input(bundle, l, bp, AF_INET6);
952 ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);