165309e5cSBrian Somers /*- 265309e5cSBrian Somers * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 365309e5cSBrian Somers * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 465309e5cSBrian Somers * Internet Initiative Japan, Inc (IIJ) 565309e5cSBrian Somers * All rights reserved. 6af57ed9fSAtsushi Murai * 765309e5cSBrian Somers * Redistribution and use in source and binary forms, with or without 865309e5cSBrian Somers * modification, are permitted provided that the following conditions 965309e5cSBrian Somers * are met: 1065309e5cSBrian Somers * 1. Redistributions of source code must retain the above copyright 1165309e5cSBrian Somers * notice, this list of conditions and the following disclaimer. 1265309e5cSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 1365309e5cSBrian Somers * notice, this list of conditions and the following disclaimer in the 1465309e5cSBrian Somers * documentation and/or other materials provided with the distribution. 15af57ed9fSAtsushi Murai * 1665309e5cSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1765309e5cSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1865309e5cSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1965309e5cSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2065309e5cSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2165309e5cSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2265309e5cSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2365309e5cSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2465309e5cSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2565309e5cSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2665309e5cSBrian Somers * SUCH DAMAGE. 27af57ed9fSAtsushi Murai * 2897d92980SPeter Wemm * $FreeBSD$ 29af57ed9fSAtsushi Murai */ 3065309e5cSBrian Somers 31972a1bcfSBrian Somers #include <sys/param.h> 326a6b4bbbSBrian Somers #include <sys/socket.h> 3375240ed1SBrian Somers #include <netinet/in.h> 34af57ed9fSAtsushi Murai #include <netinet/in_systm.h> 35af57ed9fSAtsushi Murai #include <netinet/ip.h> 3630949fd4SBrian Somers #ifndef NOINET6 3730949fd4SBrian Somers #include <netinet/icmp6.h> 3830949fd4SBrian Somers #include <netinet/ip6.h> 3930949fd4SBrian Somers #endif 40af57ed9fSAtsushi Murai #include <netinet/ip_icmp.h> 41af57ed9fSAtsushi Murai #include <netinet/udp.h> 42af57ed9fSAtsushi Murai #include <netinet/tcp.h> 431fa665f5SBrian Somers #include <sys/un.h> 4475240ed1SBrian Somers 4557fd05c4SBrian Somers #include <errno.h> 4630949fd4SBrian Somers #include <netdb.h> 4775240ed1SBrian Somers #include <stdio.h> 4875240ed1SBrian Somers #include <string.h> 495d9e6103SBrian Somers #include <termios.h> 5075240ed1SBrian Somers #include <unistd.h> 5175240ed1SBrian Somers 525d9e6103SBrian Somers #include "layer.h" 535d9e6103SBrian Somers #include "proto.h" 54927145beSBrian Somers #include "mbuf.h" 55927145beSBrian Somers #include "log.h" 5675240ed1SBrian Somers #include "defs.h" 5775240ed1SBrian Somers #include "timer.h" 5875240ed1SBrian Somers #include "fsm.h" 59879ed6faSBrian Somers #include "lqr.h" 6075240ed1SBrian Somers #include "hdlc.h" 615828db6dSBrian Somers #include "throughput.h" 625828db6dSBrian Somers #include "iplist.h" 63eaa4df37SBrian Somers #include "slcompress.h" 6430949fd4SBrian Somers #include "ncpaddr.h" 6530949fd4SBrian Somers #include "ip.h" 6675240ed1SBrian Somers #include "ipcp.h" 671ae349f5Scvs2svn #include "filter.h" 682f786681SBrian Somers #include "descriptor.h" 6975240ed1SBrian Somers #include "lcp.h" 703b0f8d2eSBrian Somers #include "ccp.h" 713b0f8d2eSBrian Somers #include "link.h" 723b0f8d2eSBrian Somers #include "mp.h" 73972a1bcfSBrian Somers #ifndef NORADIUS 74972a1bcfSBrian Somers #include "radius.h" 75972a1bcfSBrian Somers #endif 7630949fd4SBrian Somers #include "ipv6cp.h" 7730949fd4SBrian Somers #include "ncp.h" 787a6f8720SBrian Somers #include "bundle.h" 796a6b4bbbSBrian Somers #include "tun.h" 80af57ed9fSAtsushi Murai 8152c9ca19SBrian Somers 8252c9ca19SBrian Somers #define OPCODE_QUERY 0 8352c9ca19SBrian Somers #define OPCODE_IQUERY 1 8452c9ca19SBrian Somers #define OPCODE_STATUS 2 8552c9ca19SBrian Somers 8652c9ca19SBrian Somers struct dns_header { 8752c9ca19SBrian Somers u_short id; 8852c9ca19SBrian Somers unsigned qr : 1; 8952c9ca19SBrian Somers unsigned opcode : 4; 9052c9ca19SBrian Somers unsigned aa : 1; 9152c9ca19SBrian Somers unsigned tc : 1; 9252c9ca19SBrian Somers unsigned rd : 1; 9352c9ca19SBrian Somers unsigned ra : 1; 9452c9ca19SBrian Somers unsigned z : 3; 9552c9ca19SBrian Somers unsigned rcode : 4; 9652c9ca19SBrian Somers u_short qdcount; 9752c9ca19SBrian Somers u_short ancount; 9852c9ca19SBrian Somers u_short nscount; 9952c9ca19SBrian Somers u_short arcount; 100182c898aSBrian Somers }; 101af57ed9fSAtsushi Murai 10252c9ca19SBrian Somers static const char * 10352c9ca19SBrian Somers dns_Qclass2Txt(u_short qclass) 10452c9ca19SBrian Somers { 10552c9ca19SBrian Somers static char failure[6]; 10652c9ca19SBrian Somers struct { 10752c9ca19SBrian Somers u_short id; 10852c9ca19SBrian Somers const char *txt; 10952c9ca19SBrian Somers } qtxt[] = { 11052c9ca19SBrian Somers /* rfc1035 */ 11152c9ca19SBrian Somers { 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" } 11252c9ca19SBrian Somers }; 11352c9ca19SBrian Somers int f; 11452c9ca19SBrian Somers 11552c9ca19SBrian Somers for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++) 11652c9ca19SBrian Somers if (qtxt[f].id == qclass) 11752c9ca19SBrian Somers return qtxt[f].txt; 11852c9ca19SBrian Somers 119d6d3eeabSBrian Somers return HexStr(qclass, failure, sizeof failure); 12052c9ca19SBrian Somers } 12152c9ca19SBrian Somers 12252c9ca19SBrian Somers static const char * 12352c9ca19SBrian Somers dns_Qtype2Txt(u_short qtype) 12452c9ca19SBrian Somers { 12552c9ca19SBrian Somers static char failure[6]; 12652c9ca19SBrian Somers struct { 12752c9ca19SBrian Somers u_short id; 12852c9ca19SBrian Somers const char *txt; 12952c9ca19SBrian Somers } qtxt[] = { 13052c9ca19SBrian Somers /* rfc1035/rfc1700 */ 13152c9ca19SBrian Somers { 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" }, 13252c9ca19SBrian Somers { 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" }, 13352c9ca19SBrian Somers { 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" }, 13452c9ca19SBrian Somers { 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" }, 13552c9ca19SBrian Somers { 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" }, 13652c9ca19SBrian Somers { 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" }, 13752c9ca19SBrian Somers { 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" }, 13852c9ca19SBrian Somers { 254, "MAILA" }, { 255, "*" } 13952c9ca19SBrian Somers }; 14052c9ca19SBrian Somers int f; 14152c9ca19SBrian Somers 14252c9ca19SBrian Somers for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++) 14352c9ca19SBrian Somers if (qtxt[f].id == qtype) 14452c9ca19SBrian Somers return qtxt[f].txt; 14552c9ca19SBrian Somers 146d6d3eeabSBrian Somers return HexStr(qtype, failure, sizeof failure); 14752c9ca19SBrian Somers } 14852c9ca19SBrian Somers 149cad7e742SBrian Somers static __inline int 150944f7098SBrian Somers PortMatch(int op, u_short pport, u_short rport) 151af57ed9fSAtsushi Murai { 152af57ed9fSAtsushi Murai switch (op) { 153af57ed9fSAtsushi Murai case OP_EQ: 1540a4b6c5cSBrian Somers return pport == rport; 155af57ed9fSAtsushi Murai case OP_GT: 1560a4b6c5cSBrian Somers return pport > rport; 157af57ed9fSAtsushi Murai case OP_LT: 1580a4b6c5cSBrian Somers return pport < rport; 159af57ed9fSAtsushi Murai default: 1600a4b6c5cSBrian Somers return 0; 161af57ed9fSAtsushi Murai } 162af57ed9fSAtsushi Murai } 163af57ed9fSAtsushi Murai 164af57ed9fSAtsushi Murai /* 16530949fd4SBrian Somers * Check a packet against the given filter 16630949fd4SBrian Somers * Returns 0 to accept the packet, non-zero to drop the packet. 16730949fd4SBrian Somers * If psecs is not NULL, populate it with the timeout associated 16830949fd4SBrian Somers * with the filter rule matched. 169cad7e742SBrian Somers * 170cad7e742SBrian Somers * If filtering is enabled, the initial fragment of a datagram must 171cad7e742SBrian Somers * contain the complete protocol header, and subsequent fragments 172cad7e742SBrian Somers * must not attempt to over-write it. 17330949fd4SBrian Somers * 17430949fd4SBrian Somers * One (and only one) of pip or pip6 must be set. 175af57ed9fSAtsushi Murai */ 17630949fd4SBrian Somers int 17730949fd4SBrian Somers FilterCheck(const unsigned char *packet, u_int32_t family, 17830949fd4SBrian Somers const struct filter *filter, unsigned *psecs) 179af57ed9fSAtsushi Murai { 180cad7e742SBrian Somers int gotinfo; /* true if IP payload decoded */ 18130949fd4SBrian Somers int cproto; /* IPPROTO_* protocol number if (gotinfo) */ 182cad7e742SBrian Somers int estab, syn, finrst; /* TCP state flags if (gotinfo) */ 183cad7e742SBrian Somers u_short sport, dport; /* src, dest port from packet if (gotinfo) */ 184cad7e742SBrian Somers int n; /* filter rule to process */ 185cad7e742SBrian Somers int len; /* bytes used in dbuff */ 186cad7e742SBrian Somers int didname; /* true if filter header printed */ 187cad7e742SBrian Somers int match; /* true if condition matched */ 18830949fd4SBrian Somers int mindata; /* minimum data size or zero */ 189cad7e742SBrian Somers const struct filterent *fp = filter->rule; 19030949fd4SBrian Somers char dbuff[100], dstip[16], prototxt[16]; 19130949fd4SBrian Somers struct protoent *pe; 19230949fd4SBrian Somers struct ncpaddr srcaddr, dstaddr; 19330949fd4SBrian Somers const char *payload; /* IP payload */ 19430949fd4SBrian Somers int datalen; /* IP datagram length */ 195af57ed9fSAtsushi Murai 196cad7e742SBrian Somers if (fp->f_action == A_NONE) 1970a4b6c5cSBrian Somers return 0; /* No rule is given. Permit this packet */ 198cad7e742SBrian Somers 19930949fd4SBrian Somers #ifndef NOINET6 20030949fd4SBrian Somers if (family == AF_INET6) { 20130949fd4SBrian Somers const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 20230949fd4SBrian Somers 20330949fd4SBrian Somers ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 20430949fd4SBrian Somers ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 2056de54bbeSBrian Somers datalen = ntohs(pip6->ip6_plen); 2066de54bbeSBrian Somers payload = packet + sizeof *pip6; 20730949fd4SBrian Somers cproto = pip6->ip6_nxt; 20830949fd4SBrian Somers } else 20930949fd4SBrian Somers #endif 21030949fd4SBrian Somers { 2110a4b6c5cSBrian Somers /* 2120a4b6c5cSBrian Somers * Deny any packet fragment that tries to over-write the header. 213cad7e742SBrian Somers * Since we no longer have the real header available, punt on the 214cad7e742SBrian Somers * largest normal header - 20 bytes for TCP without options, rounded 215cad7e742SBrian Somers * up to the next possible fragment boundary. Since the smallest 216cad7e742SBrian Somers * `legal' MTU is 576, and the smallest recommended MTU is 296, any 2170a4b6c5cSBrian Somers * fragmentation within this range is dubious at best 2180a4b6c5cSBrian Somers */ 21930949fd4SBrian Somers const struct ip *pip = (const struct ip *)packet; 22030949fd4SBrian Somers 221cad7e742SBrian Somers len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */ 222cad7e742SBrian Somers if (len > 0) { /* Not first fragment within datagram */ 22306a43ce0SBrian Somers if (len < (24 >> 3)) { /* don't allow fragment to over-write header */ 22406a43ce0SBrian Somers log_Printf(LogFILTER, " error: illegal header\n"); 2250a4b6c5cSBrian Somers return 1; 22606a43ce0SBrian Somers } 227cad7e742SBrian Somers /* permit fragments on in and out filter */ 22806a43ce0SBrian Somers if (!filter->fragok) { 22906a43ce0SBrian Somers log_Printf(LogFILTER, " error: illegal fragmentation\n"); 23006a43ce0SBrian Somers return 1; 23106a43ce0SBrian Somers } else 23206a43ce0SBrian Somers return 0; 233cad7e742SBrian Somers } 234cad7e742SBrian Somers 23530949fd4SBrian Somers ncpaddr_setip4(&srcaddr, pip->ip_src); 23630949fd4SBrian Somers ncpaddr_setip4(&dstaddr, pip->ip_dst); 23730949fd4SBrian Somers datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 23830949fd4SBrian Somers payload = packet + (pip->ip_hl << 2); 23930949fd4SBrian Somers cproto = pip->ip_p; 24030949fd4SBrian Somers } 24130949fd4SBrian Somers 24230949fd4SBrian Somers if ((pe = getprotobynumber(cproto)) == NULL) 24330949fd4SBrian Somers snprintf(prototxt, sizeof prototxt, "%d", cproto); 24430949fd4SBrian Somers else 24530949fd4SBrian Somers snprintf(prototxt, sizeof prototxt, "%s", pe->p_name); 24630949fd4SBrian Somers gotinfo = estab = syn = finrst = didname = 0; 247af57ed9fSAtsushi Murai sport = dport = 0; 24830949fd4SBrian Somers 249cad7e742SBrian Somers for (n = 0; n < MAXFILTERS; ) { 250cad7e742SBrian Somers if (fp->f_action == A_NONE) { 251cad7e742SBrian Somers n++; 252cad7e742SBrian Somers fp++; 253cad7e742SBrian Somers continue; 254cad7e742SBrian Somers } 2555ca5389aSBrian Somers 256cad7e742SBrian Somers if (!didname) { 257dd7e2610SBrian Somers log_Printf(LogDEBUG, "%s filter:\n", filter->name); 2588390b576SBrian Somers didname = 1; 259cad7e742SBrian Somers } 2608390b576SBrian Somers 261cad7e742SBrian Somers match = 0; 26230949fd4SBrian Somers 26330949fd4SBrian Somers if ((ncprange_family(&fp->f_src) == AF_UNSPEC || 26430949fd4SBrian Somers ncprange_contains(&fp->f_src, &srcaddr)) && 26530949fd4SBrian Somers (ncprange_family(&fp->f_dst) == AF_UNSPEC || 26630949fd4SBrian Somers ncprange_contains(&fp->f_dst, &dstaddr))) { 26730949fd4SBrian Somers if (fp->f_proto != 0) { 268af57ed9fSAtsushi Murai if (!gotinfo) { 269cad7e742SBrian Somers const struct tcphdr *th; 270cad7e742SBrian Somers const struct udphdr *uh; 271cad7e742SBrian Somers const struct icmp *ih; 27230949fd4SBrian Somers #ifndef NOINET6 27330949fd4SBrian Somers const struct icmp6_hdr *ih6; 27430949fd4SBrian Somers #endif 27530949fd4SBrian Somers mindata = 0; 27630949fd4SBrian Somers sport = dport = 0; 27763f98b41SBrian Somers estab = syn = finrst = -1; 27830949fd4SBrian Somers 27930949fd4SBrian Somers switch (cproto) { 28030949fd4SBrian Somers case IPPROTO_ICMP: 28130949fd4SBrian Somers mindata = 8; /* ICMP must be at least 8 octets */ 28230949fd4SBrian Somers ih = (const struct icmp *)payload; 28392941b90SBrian Somers sport = ih->icmp_type; 284dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 2858390b576SBrian Somers snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 286af57ed9fSAtsushi Murai break; 28730949fd4SBrian Somers 28830949fd4SBrian Somers #ifndef NOINET6 28930949fd4SBrian Somers case IPPROTO_ICMPV6: 29030949fd4SBrian Somers mindata = 8; /* ICMP must be at least 8 octets */ 29130949fd4SBrian Somers ih6 = (const struct icmp6_hdr *)payload; 29292941b90SBrian Somers sport = ih6->icmp6_type; 29330949fd4SBrian Somers if (log_IsKept(LogDEBUG)) 29430949fd4SBrian Somers snprintf(dbuff, sizeof dbuff, "sport = %d", sport); 2951f9e5fe5SBrian Somers break; 29630949fd4SBrian Somers #endif 29730949fd4SBrian Somers 29830949fd4SBrian Somers case IPPROTO_IGMP: 29930949fd4SBrian Somers mindata = 8; /* IGMP uses 8-octet messages */ 30030949fd4SBrian Somers break; 30130949fd4SBrian Somers 30228149effSBrian Somers #ifdef IPPROTO_GRE 30328149effSBrian Somers case IPPROTO_GRE: 30430949fd4SBrian Somers mindata = 2; /* GRE uses 2-octet+ messages */ 30528149effSBrian Somers break; 30628149effSBrian Somers #endif 30762e85934SBrian Somers #ifdef IPPROTO_OSPFIGP 3082faae814SBrian Somers case IPPROTO_OSPFIGP: 30930949fd4SBrian Somers mindata = 8; /* IGMP uses 8-octet messages */ 3102faae814SBrian Somers break; 31162e85934SBrian Somers #endif 31230949fd4SBrian Somers #ifndef NOINET6 31330949fd4SBrian Somers case IPPROTO_IPV6: 31430949fd4SBrian Somers mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */ 3152231246bSBrian Somers break; 31630949fd4SBrian Somers #endif 31706a43ce0SBrian Somers 31830949fd4SBrian Somers case IPPROTO_UDP: 31930949fd4SBrian Somers mindata = 8; /* UDP header is 8 octets */ 32030949fd4SBrian Somers uh = (const struct udphdr *)payload; 321944f7098SBrian Somers sport = ntohs(uh->uh_sport); 322944f7098SBrian Somers dport = ntohs(uh->uh_dport); 323dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) 3248390b576SBrian Somers snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d", 3258390b576SBrian Somers sport, dport); 326af57ed9fSAtsushi Murai break; 32730949fd4SBrian Somers 328af57ed9fSAtsushi Murai case IPPROTO_TCP: 32930949fd4SBrian Somers th = (const struct tcphdr *)payload; 33030949fd4SBrian Somers /* 33130949fd4SBrian Somers * TCP headers are variable length. The following code 332cad7e742SBrian Somers * ensures that the TCP header length isn't de-referenced if 333cad7e742SBrian Somers * the datagram is too short 334cad7e742SBrian Somers */ 33506a43ce0SBrian Somers if (datalen < 20 || datalen < (th->th_off << 2)) { 33606a43ce0SBrian Somers log_Printf(LogFILTER, " error: TCP header incorrect\n"); 3370a4b6c5cSBrian Somers return 1; 33806a43ce0SBrian Somers } 339944f7098SBrian Somers sport = ntohs(th->th_sport); 340944f7098SBrian Somers dport = ntohs(th->th_dport); 341af57ed9fSAtsushi Murai estab = (th->th_flags & TH_ACK); 34263f98b41SBrian Somers syn = (th->th_flags & TH_SYN); 34363f98b41SBrian Somers finrst = (th->th_flags & (TH_FIN|TH_RST)); 3442b81c773SBrian Somers if (log_IsKept(LogDEBUG)) { 3452b81c773SBrian Somers if (!estab) 3468390b576SBrian Somers snprintf(dbuff, sizeof dbuff, 3478390b576SBrian Somers "flags = %02x, sport = %d, dport = %d", 348927145beSBrian Somers th->th_flags, sport, dport); 3492b81c773SBrian Somers else 3502b81c773SBrian Somers *dbuff = '\0'; 3512b81c773SBrian Somers } 352af57ed9fSAtsushi Murai break; 353af57ed9fSAtsushi Murai default: 35430949fd4SBrian Somers break; 35530949fd4SBrian Somers } 35630949fd4SBrian Somers 35730949fd4SBrian Somers if (datalen < mindata) { 35830949fd4SBrian Somers log_Printf(LogFILTER, " error: proto %s must be at least" 35930949fd4SBrian Somers " %d octets\n", prototxt, mindata); 36030949fd4SBrian Somers return 1; 361af57ed9fSAtsushi Murai } 362cad7e742SBrian Somers 363dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) { 3648390b576SBrian Somers if (estab != -1) { 3658390b576SBrian Somers len = strlen(dbuff); 36663f98b41SBrian Somers snprintf(dbuff + len, sizeof dbuff - len, 36763f98b41SBrian Somers ", estab = %d, syn = %d, finrst = %d", 36863f98b41SBrian Somers estab, syn, finrst); 369af57ed9fSAtsushi Murai } 37030949fd4SBrian Somers log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff); 3718390b576SBrian Somers } 3728390b576SBrian Somers gotinfo = 1; 3738390b576SBrian Somers } 37430949fd4SBrian Somers 375dd7e2610SBrian Somers if (log_IsKept(LogDEBUG)) { 376cad7e742SBrian Somers if (fp->f_srcop != OP_NONE) { 3778390b576SBrian Somers snprintf(dbuff, sizeof dbuff, ", src %s %d", 378cad7e742SBrian Somers filter_Op2Nam(fp->f_srcop), fp->f_srcport); 3798390b576SBrian Somers len = strlen(dbuff); 3808390b576SBrian Somers } else 3818390b576SBrian Somers len = 0; 382cad7e742SBrian Somers if (fp->f_dstop != OP_NONE) { 3838390b576SBrian Somers snprintf(dbuff + len, sizeof dbuff - len, 384cad7e742SBrian Somers ", dst %s %d", filter_Op2Nam(fp->f_dstop), 385cad7e742SBrian Somers fp->f_dstport); 3868390b576SBrian Somers } else if (!len) 3878390b576SBrian Somers *dbuff = '\0'; 3888390b576SBrian Somers 389dd7e2610SBrian Somers log_Printf(LogDEBUG, " rule = %d: Address match, " 39030949fd4SBrian Somers "check against proto %d%s, action = %s\n", 39130949fd4SBrian Somers n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action)); 3928390b576SBrian Somers } 393927145beSBrian Somers 394cad7e742SBrian Somers if (cproto == fp->f_proto) { 395cad7e742SBrian Somers if ((fp->f_srcop == OP_NONE || 396cad7e742SBrian Somers PortMatch(fp->f_srcop, sport, fp->f_srcport)) && 397cad7e742SBrian Somers (fp->f_dstop == OP_NONE || 398cad7e742SBrian Somers PortMatch(fp->f_dstop, dport, fp->f_dstport)) && 399cad7e742SBrian Somers (fp->f_estab == 0 || estab) && 400cad7e742SBrian Somers (fp->f_syn == 0 || syn) && 401cad7e742SBrian Somers (fp->f_finrst == 0 || finrst)) { 402cad7e742SBrian Somers match = 1; 403af57ed9fSAtsushi Murai } 404af57ed9fSAtsushi Murai } 405af57ed9fSAtsushi Murai } else { 406cad7e742SBrian Somers /* Address is matched and no protocol specified. Make a decision. */ 407dd7e2610SBrian Somers log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n, 408cad7e742SBrian Somers filter_Action2Nam(fp->f_action)); 409cad7e742SBrian Somers match = 1; 410af57ed9fSAtsushi Murai } 4118390b576SBrian Somers } else 412dd7e2610SBrian Somers log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n); 413cad7e742SBrian Somers 414cad7e742SBrian Somers if (match != fp->f_invert) { 415cad7e742SBrian Somers /* Take specified action */ 416cad7e742SBrian Somers if (fp->f_action < A_NONE) 417cad7e742SBrian Somers fp = &filter->rule[n = fp->f_action]; 41806a43ce0SBrian Somers else { 4190a4b6c5cSBrian Somers if (fp->f_action == A_PERMIT) { 4200a4b6c5cSBrian Somers if (psecs != NULL) 4210a4b6c5cSBrian Somers *psecs = fp->timeout; 42206a43ce0SBrian Somers if (strcmp(filter->name, "DIAL") == 0) { 42306a43ce0SBrian Somers /* If dial filter then even print out accept packets */ 42406a43ce0SBrian Somers if (log_IsKept(LogFILTER)) { 42530949fd4SBrian Somers snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 42606a43ce0SBrian Somers log_Printf(LogFILTER, "%sbound rule = %d accept %s " 42730949fd4SBrian Somers "src = %s:%d dst = %s:%d\n", filter->name, n, prototxt, 42830949fd4SBrian Somers ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 42906a43ce0SBrian Somers } 43006a43ce0SBrian Somers } 4310a4b6c5cSBrian Somers return 0; 43206a43ce0SBrian Somers } else { 43306a43ce0SBrian Somers if (log_IsKept(LogFILTER)) { 43430949fd4SBrian Somers snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 43506a43ce0SBrian Somers log_Printf(LogFILTER, 43606a43ce0SBrian Somers "%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n", 43730949fd4SBrian Somers filter->name, n, prototxt, 43830949fd4SBrian Somers ncpaddr_ntoa(&srcaddr), sport, dstip, dport); 43906a43ce0SBrian Somers } 4400a4b6c5cSBrian Somers return 1; 44130949fd4SBrian Somers } /* Explict match. Deny this packet */ 44206a43ce0SBrian Somers } 443cad7e742SBrian Somers } else { 444cad7e742SBrian Somers n++; 445af57ed9fSAtsushi Murai fp++; 446af57ed9fSAtsushi Murai } 447af57ed9fSAtsushi Murai } 44806a43ce0SBrian Somers 44906a43ce0SBrian Somers if (log_IsKept(LogFILTER)) { 45030949fd4SBrian Somers snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr)); 45106a43ce0SBrian Somers log_Printf(LogFILTER, 45206a43ce0SBrian Somers "%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n", 45330949fd4SBrian Somers filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport, 45430949fd4SBrian Somers dstip, dport); 45506a43ce0SBrian Somers } 45606a43ce0SBrian Somers 45730949fd4SBrian Somers return 1; /* No rule matched, deny this packet */ 458af57ed9fSAtsushi Murai } 459af57ed9fSAtsushi Murai 46052c9ca19SBrian Somers static void 46152c9ca19SBrian Somers ip_LogDNS(const struct udphdr *uh, const char *direction) 46252c9ca19SBrian Somers { 46352c9ca19SBrian Somers struct dns_header header; 46452c9ca19SBrian Somers const u_short *pktptr; 46552c9ca19SBrian Somers const u_char *ptr; 46647fe010cSBrian Somers u_short *hptr, tmp; 46752c9ca19SBrian Somers int len; 46852c9ca19SBrian Somers 46952c9ca19SBrian Somers ptr = (const char *)uh + sizeof *uh; 47052c9ca19SBrian Somers len = ntohs(uh->uh_ulen) - sizeof *uh; 47152c9ca19SBrian Somers if (len < sizeof header + 5) /* rfc1024 */ 47252c9ca19SBrian Somers return; 47352c9ca19SBrian Somers 47452c9ca19SBrian Somers pktptr = (const u_short *)ptr; 47552c9ca19SBrian Somers hptr = (u_short *)&header; 47652c9ca19SBrian Somers ptr += sizeof header; 47752c9ca19SBrian Somers len -= sizeof header; 47852c9ca19SBrian Somers 47952c9ca19SBrian Somers while (pktptr < (const u_short *)ptr) { 48052c9ca19SBrian Somers *hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */ 48152c9ca19SBrian Somers pktptr++; 48252c9ca19SBrian Somers } 48352c9ca19SBrian Somers 48452c9ca19SBrian Somers if (header.opcode == OPCODE_QUERY && header.qr == 0) { 48552c9ca19SBrian Somers /* rfc1035 */ 48626e6a622SBrian Somers char namewithdot[MAXHOSTNAMELEN + 1], *n; 48752c9ca19SBrian Somers const char *qtype, *qclass; 48852c9ca19SBrian Somers const u_char *end; 48952c9ca19SBrian Somers 49026e6a622SBrian Somers n = namewithdot; 49152c9ca19SBrian Somers end = ptr + len - 4; 49226e6a622SBrian Somers if (end - ptr >= sizeof namewithdot) 49326e6a622SBrian Somers end = ptr + sizeof namewithdot - 1; 49452c9ca19SBrian Somers while (ptr < end) { 49552c9ca19SBrian Somers len = *ptr++; 49652c9ca19SBrian Somers if (len > end - ptr) 49752c9ca19SBrian Somers len = end - ptr; 49826e6a622SBrian Somers if (n != namewithdot) 49952c9ca19SBrian Somers *n++ = '.'; 50052c9ca19SBrian Somers memcpy(n, ptr, len); 50152c9ca19SBrian Somers ptr += len; 50252c9ca19SBrian Somers n += len; 50352c9ca19SBrian Somers } 50452c9ca19SBrian Somers *n = '\0'; 50547fe010cSBrian Somers 50647fe010cSBrian Somers if (log_IsKept(LogDNS)) { 50747fe010cSBrian Somers memcpy(&tmp, end, sizeof tmp); 50847fe010cSBrian Somers qtype = dns_Qtype2Txt(ntohs(tmp)); 50947fe010cSBrian Somers memcpy(&tmp, end + 2, sizeof tmp); 51047fe010cSBrian Somers qclass = dns_Qclass2Txt(ntohs(tmp)); 51152c9ca19SBrian Somers 51252c9ca19SBrian Somers log_Printf(LogDNS, "%sbound query %s %s %s\n", 51326e6a622SBrian Somers direction, qclass, qtype, namewithdot); 51452c9ca19SBrian Somers } 51552c9ca19SBrian Somers } 51647fe010cSBrian Somers } 51752c9ca19SBrian Somers 518af57ed9fSAtsushi Murai /* 51930949fd4SBrian Somers * Check if the given packet matches the given filter. 52030949fd4SBrian Somers * One of pip or pip6 must be set. 521af57ed9fSAtsushi Murai */ 522af57ed9fSAtsushi Murai int 52330949fd4SBrian Somers PacketCheck(struct bundle *bundle, u_int32_t family, 52430949fd4SBrian Somers const unsigned char *packet, int nb, struct filter *filter, 52530949fd4SBrian Somers const char *prefix, unsigned *psecs) 526af57ed9fSAtsushi Murai { 52752c9ca19SBrian Somers static const char *const TcpFlags[] = { 52852c9ca19SBrian Somers "FIN", "SYN", "RST", "PSH", "ACK", "URG" 52952c9ca19SBrian Somers }; 53030949fd4SBrian Somers const struct tcphdr *th; 53130949fd4SBrian Somers const struct udphdr *uh; 53230949fd4SBrian Somers const struct icmp *icmph; 53330949fd4SBrian Somers #ifndef NOINET6 53430949fd4SBrian Somers const struct icmp6_hdr *icmp6h; 53530949fd4SBrian Somers #endif 53630949fd4SBrian Somers const unsigned char *payload; 53730949fd4SBrian Somers struct ncpaddr srcaddr, dstaddr; 53830949fd4SBrian Somers int cproto, mask, len, n, pri, logit, loglen, result; 539d93d3a9cSBrian Somers char logbuf[200]; 54030949fd4SBrian Somers int datalen, frag; 54130949fd4SBrian Somers u_char tos; 542af57ed9fSAtsushi Murai 543b565321aSBrian Somers logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) && 544b565321aSBrian Somers (!filter || filter->logok); 54555a8cdeaSBrian Somers loglen = 0; 54652c9ca19SBrian Somers pri = 0; 547af57ed9fSAtsushi Murai 54830949fd4SBrian Somers #ifndef NOINET6 54930949fd4SBrian Somers if (family == AF_INET6) { 55030949fd4SBrian Somers const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet; 55130949fd4SBrian Somers 55230949fd4SBrian Somers ncpaddr_setip6(&srcaddr, &pip6->ip6_src); 55330949fd4SBrian Somers ncpaddr_setip6(&dstaddr, &pip6->ip6_dst); 55430949fd4SBrian Somers datalen = ntohs(pip6->ip6_plen); 55530949fd4SBrian Somers payload = packet + sizeof *pip6; 55630949fd4SBrian Somers cproto = pip6->ip6_nxt; 5576de54bbeSBrian Somers tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */ 55830949fd4SBrian Somers frag = 0; /* XXX: ??? */ 55930949fd4SBrian Somers } else 56030949fd4SBrian Somers #endif 56130949fd4SBrian Somers { 56230949fd4SBrian Somers const struct ip *pip = (const struct ip *)packet; 56330949fd4SBrian Somers 56430949fd4SBrian Somers ncpaddr_setip4(&srcaddr, pip->ip_src); 56530949fd4SBrian Somers ncpaddr_setip4(&dstaddr, pip->ip_dst); 56630949fd4SBrian Somers datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2); 56730949fd4SBrian Somers payload = packet + (pip->ip_hl << 2); 56830949fd4SBrian Somers cproto = pip->ip_p; 56930949fd4SBrian Somers tos = pip->ip_tos; 57030949fd4SBrian Somers frag = ntohs(pip->ip_off) & IP_OFFMASK; 57130949fd4SBrian Somers } 57230949fd4SBrian Somers 57352c9ca19SBrian Somers uh = NULL; 574af57ed9fSAtsushi Murai 57555a8cdeaSBrian Somers if (logit && loglen < sizeof logbuf) { 57698251667SBrian Somers if (prefix) 57798251667SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix); 57898251667SBrian Somers else if (filter) 5795ca5389aSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name); 580b565321aSBrian Somers else 581b565321aSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, " "); 58255a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 58355a8cdeaSBrian Somers } 584af57ed9fSAtsushi Murai 58530949fd4SBrian Somers switch (cproto) { 586af57ed9fSAtsushi Murai case IPPROTO_ICMP: 58755a8cdeaSBrian Somers if (logit && loglen < sizeof logbuf) { 58830949fd4SBrian Somers len = datalen - sizeof *icmph; 58930949fd4SBrian Somers icmph = (const struct icmp *)payload; 59055a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 59130949fd4SBrian Somers "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type); 59255a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 59355a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 59430949fd4SBrian Somers "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 59555a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 596af57ed9fSAtsushi Murai } 597af57ed9fSAtsushi Murai break; 598da477886SBrian Somers 59930949fd4SBrian Somers #ifndef NOINET6 60030949fd4SBrian Somers case IPPROTO_ICMPV6: 60130949fd4SBrian Somers if (logit && loglen < sizeof logbuf) { 60230949fd4SBrian Somers len = datalen - sizeof *icmp6h; 60330949fd4SBrian Somers icmp6h = (const struct icmp6_hdr *)payload; 60430949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 60530949fd4SBrian Somers "ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type); 60630949fd4SBrian Somers loglen += strlen(logbuf + loglen); 60730949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 60830949fd4SBrian Somers "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb); 60930949fd4SBrian Somers loglen += strlen(logbuf + loglen); 61030949fd4SBrian Somers } 61130949fd4SBrian Somers break; 61230949fd4SBrian Somers #endif 61330949fd4SBrian Somers 614af57ed9fSAtsushi Murai case IPPROTO_UDP: 61530949fd4SBrian Somers uh = (const struct udphdr *)payload; 61630949fd4SBrian Somers if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 617da477886SBrian Somers pri++; 618da477886SBrian Somers 61930949fd4SBrian Somers if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport), 620da477886SBrian Somers ntohs(uh->uh_dport))) 621da477886SBrian Somers pri++; 622da477886SBrian Somers 623da477886SBrian Somers if (logit && loglen < sizeof logbuf) { 62430949fd4SBrian Somers len = datalen - sizeof *uh; 62555a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 62630949fd4SBrian Somers "UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport)); 62755a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 62855a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 62930949fd4SBrian Somers "%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport), 63098251667SBrian Somers len, nb); 63155a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 632af57ed9fSAtsushi Murai } 63398251667SBrian Somers 63498251667SBrian Somers if (Enabled(bundle, OPT_FILTERDECAP) && 63530949fd4SBrian Somers payload[sizeof *uh] == HDLC_ADDR && 63630949fd4SBrian Somers payload[sizeof *uh + 1] == HDLC_UI) { 63798251667SBrian Somers u_short proto; 63898251667SBrian Somers const char *type; 63998251667SBrian Somers 64030949fd4SBrian Somers memcpy(&proto, payload + sizeof *uh + 2, sizeof proto); 64198251667SBrian Somers type = NULL; 64298251667SBrian Somers 64398251667SBrian Somers switch (ntohs(proto)) { 64498251667SBrian Somers case PROTO_IP: 64598251667SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 64630949fd4SBrian Somers result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4, 64730949fd4SBrian Somers nb - (payload - packet) - sizeof *uh - 4, filter, 6480a4b6c5cSBrian Somers logbuf, psecs); 64998251667SBrian Somers if (result != -2) 65098251667SBrian Somers return result; 65198251667SBrian Somers type = "IP"; 65298251667SBrian Somers break; 65398251667SBrian Somers 65498251667SBrian Somers case PROTO_VJUNCOMP: type = "compressed VJ"; break; 65598251667SBrian Somers case PROTO_VJCOMP: type = "uncompressed VJ"; break; 65698251667SBrian Somers case PROTO_MP: type = "Multi-link"; break; 65798251667SBrian Somers case PROTO_ICOMPD: type = "Individual link CCP"; break; 65898251667SBrian Somers case PROTO_COMPD: type = "CCP"; break; 65998251667SBrian Somers case PROTO_IPCP: type = "IPCP"; break; 66098251667SBrian Somers case PROTO_LCP: type = "LCP"; break; 66198251667SBrian Somers case PROTO_PAP: type = "PAP"; break; 66298251667SBrian Somers case PROTO_CBCP: type = "CBCP"; break; 66398251667SBrian Somers case PROTO_LQR: type = "LQR"; break; 66498251667SBrian Somers case PROTO_CHAP: type = "CHAP"; break; 66598251667SBrian Somers } 66698251667SBrian Somers if (type) { 66798251667SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 66898251667SBrian Somers " - %s data", type); 66998251667SBrian Somers loglen += strlen(logbuf + loglen); 67098251667SBrian Somers } 67198251667SBrian Somers } 67298251667SBrian Somers 673af57ed9fSAtsushi Murai break; 674da477886SBrian Somers 67528149effSBrian Somers #ifdef IPPROTO_GRE 67628149effSBrian Somers case IPPROTO_GRE: 67728149effSBrian Somers if (logit && loglen < sizeof logbuf) { 67828149effSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 67930949fd4SBrian Somers "GRE: %s ---> ", ncpaddr_ntoa(&srcaddr)); 68028149effSBrian Somers loglen += strlen(logbuf + loglen); 68128149effSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 68230949fd4SBrian Somers "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 68328149effSBrian Somers loglen += strlen(logbuf + loglen); 68428149effSBrian Somers } 68528149effSBrian Somers break; 68628149effSBrian Somers #endif 68728149effSBrian Somers 68862e85934SBrian Somers #ifdef IPPROTO_OSPFIGP 6892faae814SBrian Somers case IPPROTO_OSPFIGP: 6902faae814SBrian Somers if (logit && loglen < sizeof logbuf) { 6912faae814SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 69230949fd4SBrian Somers "OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr)); 6932faae814SBrian Somers loglen += strlen(logbuf + loglen); 6942faae814SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 69530949fd4SBrian Somers "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 6962faae814SBrian Somers loglen += strlen(logbuf + loglen); 6972faae814SBrian Somers } 6982faae814SBrian Somers break; 69962e85934SBrian Somers #endif 700da477886SBrian Somers 70130949fd4SBrian Somers #ifndef NOINET6 70230949fd4SBrian Somers case IPPROTO_IPV6: 70330949fd4SBrian Somers if (logit && loglen < sizeof logbuf) { 70430949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 70530949fd4SBrian Somers "IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr)); 70630949fd4SBrian Somers loglen += strlen(logbuf + loglen); 70730949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 70830949fd4SBrian Somers "%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb); 70930949fd4SBrian Somers loglen += strlen(logbuf + loglen); 71030949fd4SBrian Somers } 71130949fd4SBrian Somers 71230949fd4SBrian Somers if (Enabled(bundle, OPT_FILTERDECAP)) { 71330949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 71430949fd4SBrian Somers result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet), 71530949fd4SBrian Somers filter, logbuf, psecs); 71630949fd4SBrian Somers if (result != -2) 71730949fd4SBrian Somers return result; 71830949fd4SBrian Somers } 71930949fd4SBrian Somers break; 72030949fd4SBrian Somers #endif 72130949fd4SBrian Somers 722eee772ecSBrian Somers case IPPROTO_IPIP: 723eee772ecSBrian Somers if (logit && loglen < sizeof logbuf) { 724eee772ecSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 72530949fd4SBrian Somers "IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 726eee772ecSBrian Somers loglen += strlen(logbuf + loglen); 727eee772ecSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 72830949fd4SBrian Somers "%s", ncpaddr_ntoa(&dstaddr)); 729eee772ecSBrian Somers loglen += strlen(logbuf + loglen); 73030949fd4SBrian Somers } 731da70ad60SBrian Somers 73230949fd4SBrian Somers if (Enabled(bundle, OPT_FILTERDECAP) && 73330949fd4SBrian Somers ((const struct ip *)payload)->ip_v == 4) { 734da70ad60SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains "); 73530949fd4SBrian Somers result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet), 73630949fd4SBrian Somers filter, logbuf, psecs); 737da70ad60SBrian Somers if (result != -2) 738da70ad60SBrian Somers return result; 739da70ad60SBrian Somers } 740eee772ecSBrian Somers break; 741da477886SBrian Somers 7422231246bSBrian Somers case IPPROTO_ESP: 7432231246bSBrian Somers if (logit && loglen < sizeof logbuf) { 7442231246bSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 74530949fd4SBrian Somers "ESP: %s ---> ", ncpaddr_ntoa(&srcaddr)); 7462231246bSBrian Somers loglen += strlen(logbuf + loglen); 74725254215SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 74830949fd4SBrian Somers ncpaddr_ntoa(&dstaddr), payload); 7492231246bSBrian Somers loglen += strlen(logbuf + loglen); 7502231246bSBrian Somers } 7512231246bSBrian Somers break; 7522231246bSBrian Somers 7532231246bSBrian Somers case IPPROTO_AH: 7542231246bSBrian Somers if (logit && loglen < sizeof logbuf) { 7552231246bSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 75630949fd4SBrian Somers "AH: %s ---> ", ncpaddr_ntoa(&srcaddr)); 7572231246bSBrian Somers loglen += strlen(logbuf + loglen); 75825254215SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p", 75930949fd4SBrian Somers ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t)); 7602231246bSBrian Somers loglen += strlen(logbuf + loglen); 7612231246bSBrian Somers } 7622231246bSBrian Somers break; 7632231246bSBrian Somers 764eee772ecSBrian Somers case IPPROTO_IGMP: 765eee772ecSBrian Somers if (logit && loglen < sizeof logbuf) { 76630949fd4SBrian Somers uh = (const struct udphdr *)payload; 767eee772ecSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 76830949fd4SBrian Somers "IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), 7690a4b6c5cSBrian Somers ntohs(uh->uh_sport)); 770eee772ecSBrian Somers loglen += strlen(logbuf + loglen); 771eee772ecSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 77230949fd4SBrian Somers "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport)); 773eee772ecSBrian Somers loglen += strlen(logbuf + loglen); 774eee772ecSBrian Somers } 775eee772ecSBrian Somers break; 776da477886SBrian Somers 777af57ed9fSAtsushi Murai case IPPROTO_TCP: 77830949fd4SBrian Somers th = (const struct tcphdr *)payload; 77930949fd4SBrian Somers if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos) 780442f8495SBrian Somers pri++; 781da477886SBrian Somers 78230949fd4SBrian Somers if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport), 783442f8495SBrian Somers ntohs(th->th_dport))) 784442f8495SBrian Somers pri++; 785442f8495SBrian Somers 78655a8cdeaSBrian Somers if (logit && loglen < sizeof logbuf) { 78730949fd4SBrian Somers len = datalen - (th->th_off << 2); 78855a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 78930949fd4SBrian Somers "TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport)); 79055a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 79155a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 79230949fd4SBrian Somers "%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport)); 79355a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 794af57ed9fSAtsushi Murai n = 0; 795af57ed9fSAtsushi Murai for (mask = TH_FIN; mask != 0x40; mask <<= 1) { 79655a8cdeaSBrian Somers if (th->th_flags & mask) { 79755a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]); 79855a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 79955a8cdeaSBrian Somers } 800af57ed9fSAtsushi Murai n++; 801af57ed9fSAtsushi Murai } 80255a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 8033a2e4f62SBrian Somers " seq:%lx ack:%lx (%d/%d)", 8043a2e4f62SBrian Somers (u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb); 80555a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 806af57ed9fSAtsushi Murai if ((th->th_flags & TH_SYN) && nb > 40) { 80730949fd4SBrian Somers const u_short *sp; 808af57ed9fSAtsushi Murai 80930949fd4SBrian Somers sp = (const u_short *)(payload + 20); 81055a8cdeaSBrian Somers if (ntohs(sp[0]) == 0x0204) { 81155a8cdeaSBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 81255a8cdeaSBrian Somers " MSS = %d", ntohs(sp[1])); 81355a8cdeaSBrian Somers loglen += strlen(logbuf + loglen); 81455a8cdeaSBrian Somers } 815af57ed9fSAtsushi Murai } 816af57ed9fSAtsushi Murai } 817af57ed9fSAtsushi Murai break; 81898251667SBrian Somers 81998251667SBrian Somers default: 82098251667SBrian Somers if (prefix) 82198251667SBrian Somers return -2; 82230949fd4SBrian Somers 82330949fd4SBrian Somers if (logit && loglen < sizeof logbuf) { 82430949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 82530949fd4SBrian Somers "<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr)); 82630949fd4SBrian Somers loglen += strlen(logbuf + loglen); 82730949fd4SBrian Somers snprintf(logbuf + loglen, sizeof logbuf - loglen, 82830949fd4SBrian Somers "%s (%d)", ncpaddr_ntoa(&dstaddr), nb); 82930949fd4SBrian Somers loglen += strlen(logbuf + loglen); 83030949fd4SBrian Somers } 83130949fd4SBrian Somers break; 832af57ed9fSAtsushi Murai } 83376bd0c0aSDoug Rabson 83430949fd4SBrian Somers if (filter && FilterCheck(packet, family, filter, psecs)) { 835710e9c29SBrian Somers if (logit) 836dd7e2610SBrian Somers log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf); 83752c9ca19SBrian Somers result = -1; 838af57ed9fSAtsushi Murai } else { 8395ca5389aSBrian Somers /* Check Keep Alive filter */ 84052c9ca19SBrian Somers if (logit && log_IsKept(LogTCPIP)) { 8410a4b6c5cSBrian Somers unsigned alivesecs; 8420a4b6c5cSBrian Somers 8430a4b6c5cSBrian Somers alivesecs = 0; 84430949fd4SBrian Somers if (filter && 84530949fd4SBrian Somers FilterCheck(packet, family, &bundle->filter.alive, &alivesecs)) 846dd7e2610SBrian Somers log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf); 8470a4b6c5cSBrian Somers else if (psecs != NULL) { 8480a4b6c5cSBrian Somers if(*psecs == 0) 8490a4b6c5cSBrian Somers *psecs = alivesecs; 8500a4b6c5cSBrian Somers if (*psecs) { 8510a4b6c5cSBrian Somers if (*psecs != alivesecs) 8520a4b6c5cSBrian Somers log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n", 8530a4b6c5cSBrian Somers logbuf, *psecs, alivesecs); 8541e991daaSBrian Somers else 8550a4b6c5cSBrian Somers log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs); 8560a4b6c5cSBrian Somers } else 857dd7e2610SBrian Somers log_Printf(LogTCPIP, "%s\n", logbuf); 85853c9f6c0SAtsushi Murai } 8590a4b6c5cSBrian Somers } 86052c9ca19SBrian Somers result = pri; 861af57ed9fSAtsushi Murai } 86252c9ca19SBrian Somers 863b565321aSBrian Somers if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS)) 86452c9ca19SBrian Somers ip_LogDNS(uh, filter->name); 86552c9ca19SBrian Somers 86652c9ca19SBrian Somers return result; 867af57ed9fSAtsushi Murai } 868af57ed9fSAtsushi Murai 86930949fd4SBrian Somers static int 87030949fd4SBrian Somers ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af) 8717a6f8720SBrian Somers { 872af57ed9fSAtsushi Murai int nb, nw; 873b6e82f33SBrian Somers struct tun_data tun; 8743a7b6d76SBrian Somers char *data; 8750a4b6c5cSBrian Somers unsigned secs, alivesecs; 8765d9e6103SBrian Somers 87726af0ae9SBrian Somers nb = m_length(bp); 87876d98538SBrian Somers if (nb > sizeof tun.data) { 87976d98538SBrian Somers log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n", 88076d98538SBrian Somers l->name, nb, (int)(sizeof tun.data)); 88126af0ae9SBrian Somers m_freem(bp); 88230949fd4SBrian Somers return 0; 88376d98538SBrian Somers } 8845d9e6103SBrian Somers mbuf_Read(bp, tun.data, nb); 885af57ed9fSAtsushi Murai 8860a4b6c5cSBrian Somers secs = 0; 88730949fd4SBrian Somers if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in, 88830949fd4SBrian Somers NULL, &secs) < 0) 88930949fd4SBrian Somers return 0; 8906db75539SBrian Somers 8910a4b6c5cSBrian Somers alivesecs = 0; 89230949fd4SBrian Somers if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) { 8930a4b6c5cSBrian Somers if (secs == 0) 8940a4b6c5cSBrian Somers secs = alivesecs; 8950a4b6c5cSBrian Somers bundle_StartIdleTimer(bundle, secs); 8960a4b6c5cSBrian Somers } 8971e991daaSBrian Somers 8983a7b6d76SBrian Somers if (bundle->dev.header) { 89930949fd4SBrian Somers tun.header.family = htonl(af); 90070ee81ffSBrian Somers nb += sizeof tun - sizeof tun.data; 9013a7b6d76SBrian Somers data = (char *)&tun; 9023a7b6d76SBrian Somers } else 9033a7b6d76SBrian Somers data = tun.data; 9043a7b6d76SBrian Somers 9053a7b6d76SBrian Somers nw = write(bundle->dev.fd, data, nb); 906c4c4aaacSBrian Somers if (nw != nb) { 90757fd05c4SBrian Somers if (nw == -1) 90876d98538SBrian Somers log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n", 90976d98538SBrian Somers l->name, nb, strerror(errno)); 91057fd05c4SBrian Somers else 91176d98538SBrian Somers log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw); 9126db75539SBrian Somers } 9135d9e6103SBrian Somers 91430949fd4SBrian Somers return nb; 91530949fd4SBrian Somers } 91630949fd4SBrian Somers 91730949fd4SBrian Somers struct mbuf * 91830949fd4SBrian Somers ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 91930949fd4SBrian Somers { 92030949fd4SBrian Somers int nb; 92130949fd4SBrian Somers 92230949fd4SBrian Somers if (bundle->ncp.ipcp.fsm.state != ST_OPENED) { 92330949fd4SBrian Somers log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n"); 92430949fd4SBrian Somers m_freem(bp); 9255d9e6103SBrian Somers return NULL; 926af57ed9fSAtsushi Murai } 927af57ed9fSAtsushi Murai 92830949fd4SBrian Somers m_settype(bp, MB_IPIN); 92930949fd4SBrian Somers 93030949fd4SBrian Somers nb = ip_Input(bundle, l, bp, AF_INET); 93130949fd4SBrian Somers ipcp_AddInOctets(&bundle->ncp.ipcp, nb); 93230949fd4SBrian Somers 93330949fd4SBrian Somers return NULL; 93430949fd4SBrian Somers } 93530949fd4SBrian Somers 93630949fd4SBrian Somers #ifndef NOINET6 93730949fd4SBrian Somers struct mbuf * 93830949fd4SBrian Somers ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp) 939af57ed9fSAtsushi Murai { 94030949fd4SBrian Somers int nb; 941af57ed9fSAtsushi Murai 9421136c6acSBrian Somers if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) { 94330949fd4SBrian Somers log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n"); 94430949fd4SBrian Somers m_freem(bp); 94530949fd4SBrian Somers return NULL; 946af57ed9fSAtsushi Murai } 947af57ed9fSAtsushi Murai 94830949fd4SBrian Somers m_settype(bp, MB_IPV6IN); 9496f8e9f0aSBrian Somers 95030949fd4SBrian Somers nb = ip_Input(bundle, l, bp, AF_INET6); 95130949fd4SBrian Somers ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb); 95230949fd4SBrian Somers 95330949fd4SBrian Somers return NULL; 9546f8e9f0aSBrian Somers } 95530949fd4SBrian Somers #endif 956