ip_fw2.c revision 209589
1210284Sjmallett/*- 2232812Sjmallett * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 3215990Sjmallett * 4210284Sjmallett * Redistribution and use in source and binary forms, with or without 5210284Sjmallett * modification, are permitted provided that the following conditions 6215990Sjmallett * are met: 7215990Sjmallett * 1. Redistributions of source code must retain the above copyright 8215990Sjmallett * notice, this list of conditions and the following disclaimer. 9210284Sjmallett * 2. Redistributions in binary form must reproduce the above copyright 10215990Sjmallett * notice, this list of conditions and the following disclaimer in the 11215990Sjmallett * documentation and/or other materials provided with the distribution. 12210284Sjmallett * 13215990Sjmallett * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14215990Sjmallett * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15215990Sjmallett * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16215990Sjmallett * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17210284Sjmallett * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18232812Sjmallett * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19215990Sjmallett * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20215990Sjmallett * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21215990Sjmallett * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22210284Sjmallett * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23215990Sjmallett * SUCH DAMAGE. 24215990Sjmallett */ 25215990Sjmallett 26215990Sjmallett#include <sys/cdefs.h> 27210284Sjmallett__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw2.c 209589 2010-06-29 16:57:30Z glebius $"); 28215990Sjmallett 29232812Sjmallett/* 30215990Sjmallett * The FreeBSD IP packet firewall, main file 31215990Sjmallett */ 32215990Sjmallett 33215990Sjmallett#if !defined(KLD_MODULE) 34215990Sjmallett#include "opt_ipfw.h" 35215990Sjmallett#include "opt_ipdivert.h" 36215990Sjmallett#include "opt_ipdn.h" 37215990Sjmallett#include "opt_inet.h" 38215990Sjmallett#ifndef INET 39210284Sjmallett#error IPFIREWALL requires INET. 40210284Sjmallett#endif /* INET */ 41210284Sjmallett#endif 42210284Sjmallett#include "opt_inet6.h" 43210284Sjmallett#include "opt_ipsec.h" 44210284Sjmallett 45210284Sjmallett#include <sys/param.h> 46232812Sjmallett#include <sys/systm.h> 47210284Sjmallett#include <sys/condvar.h> 48210284Sjmallett#include <sys/eventhandler.h> 49210284Sjmallett#include <sys/malloc.h> 50210284Sjmallett#include <sys/mbuf.h> 51210284Sjmallett#include <sys/kernel.h> 52210284Sjmallett#include <sys/lock.h> 53210284Sjmallett#include <sys/jail.h> 54210284Sjmallett#include <sys/module.h> 55215990Sjmallett#include <sys/priv.h> 56215990Sjmallett#include <sys/proc.h> 57215990Sjmallett#include <sys/rwlock.h> 58210284Sjmallett#include <sys/socket.h> 59210284Sjmallett#include <sys/socketvar.h> 60210284Sjmallett#include <sys/sysctl.h> 61215990Sjmallett#include <sys/syslog.h> 62210284Sjmallett#include <sys/ucred.h> 63232812Sjmallett#include <net/ethernet.h> /* for ETHERTYPE_IP */ 64232812Sjmallett#include <net/if.h> 65215990Sjmallett#include <net/route.h> 66232812Sjmallett#include <net/pf_mtag.h> 67210284Sjmallett#include <net/vnet.h> 68210284Sjmallett 69210284Sjmallett#include <netinet/in.h> 70210284Sjmallett#include <netinet/in_var.h> 71232812Sjmallett#include <netinet/in_pcb.h> 72210284Sjmallett#include <netinet/ip.h> 73215990Sjmallett#include <netinet/ip_var.h> 74215990Sjmallett#include <netinet/ip_icmp.h> 75215990Sjmallett#include <netinet/ip_fw.h> 76210284Sjmallett#include <netinet/ipfw/ip_fw_private.h> 77210284Sjmallett#include <netinet/ip_carp.h> 78210284Sjmallett#include <netinet/pim.h> 79215990Sjmallett#include <netinet/tcp_var.h> 80210284Sjmallett#include <netinet/udp.h> 81215990Sjmallett#include <netinet/udp_var.h> 82210284Sjmallett#include <netinet/sctp.h> 83215990Sjmallett 84210284Sjmallett#include <netinet/ip6.h> 85215990Sjmallett#include <netinet/icmp6.h> 86210284Sjmallett#ifdef INET6 87215990Sjmallett#include <netinet6/scope6_var.h> 88210284Sjmallett#include <netinet6/ip6_var.h> 89215990Sjmallett#endif 90210284Sjmallett 91215990Sjmallett#include <machine/in_cksum.h> /* XXX for in_cksum */ 92210284Sjmallett 93215990Sjmallett#ifdef MAC 94210284Sjmallett#include <security/mac/mac_framework.h> 95215990Sjmallett#endif 96210284Sjmallett 97215990Sjmallett/* 98210284Sjmallett * static variables followed by global ones. 99210284Sjmallett * All ipfw global variables are here. 100210284Sjmallett */ 101210284Sjmallett 102210284Sjmallett/* ipfw_vnet_ready controls when we are open for business */ 103210284Sjmallettstatic VNET_DEFINE(int, ipfw_vnet_ready) = 0; 104210284Sjmallett#define V_ipfw_vnet_ready VNET(ipfw_vnet_ready) 105215990Sjmallett 106210284Sjmallettstatic VNET_DEFINE(int, fw_deny_unknown_exthdrs); 107215990Sjmallett#define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs) 108210284Sjmallett 109215990Sjmallett#ifdef IPFIREWALL_DEFAULT_TO_ACCEPT 110210284Sjmallettstatic int default_to_accept = 1; 111215990Sjmallett#else 112210284Sjmallettstatic int default_to_accept; 113215990Sjmallett#endif 114210284Sjmallett 115215990SjmallettVNET_DEFINE(int, autoinc_step); 116210284Sjmallett 117210284Sjmallett/* 118210284Sjmallett * Each rule belongs to one of 32 different sets (0..31). 119210284Sjmallett * The variable set_disable contains one bit per set. 120210284Sjmallett * If the bit is set, all rules in the corresponding set 121210284Sjmallett * are disabled. Set RESVD_SET(31) is reserved for the default rule 122210284Sjmallett * and rules that are not deleted by the flush command, 123210284Sjmallett * and CANNOT be disabled. 124210284Sjmallett * Rules in set RESVD_SET can only be deleted individually. 125210284Sjmallett */ 126210284SjmallettVNET_DEFINE(u_int32_t, set_disable); 127210284Sjmallett#define V_set_disable VNET(set_disable) 128210284Sjmallett 129210284SjmallettVNET_DEFINE(int, fw_verbose); 130210284Sjmallett/* counter for ipfw_log(NULL...) */ 131210284SjmallettVNET_DEFINE(u_int64_t, norule_counter); 132210284SjmallettVNET_DEFINE(int, verbose_limit); 133210284Sjmallett 134215990Sjmallett/* layer3_chain contains the list of rules for layer 3 */ 135215990SjmallettVNET_DEFINE(struct ip_fw_chain, layer3_chain); 136215990Sjmallett 137215990Sjmallettipfw_nat_t *ipfw_nat_ptr = NULL; 138215990Sjmallettstruct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int); 139215990Sjmallettipfw_nat_cfg_t *ipfw_nat_cfg_ptr; 140215990Sjmallettipfw_nat_cfg_t *ipfw_nat_del_ptr; 141215990Sjmallettipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr; 142215990Sjmallettipfw_nat_cfg_t *ipfw_nat_get_log_ptr; 143215990Sjmallett 144215990Sjmallett#ifdef SYSCTL_NODE 145215990Sjmallettuint32_t dummy_def = IPFW_DEFAULT_RULE; 146215990Sjmallettuint32_t dummy_tables_max = IPFW_TABLES_MAX; 147215990Sjmallett 148215990SjmallettSYSBEGIN(f3) 149215990Sjmallett 150215990SjmallettSYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 151215990SjmallettSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass, 152215990Sjmallett CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0, 153215990Sjmallett "Only do a single pass through ipfw when using dummynet(4)"); 154215990SjmallettSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, 155215990Sjmallett CTLFLAG_RW, &VNET_NAME(autoinc_step), 0, 156215990Sjmallett "Rule number auto-increment step"); 157215990SjmallettSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose, 158215990Sjmallett CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0, 159210284Sjmallett "Log matches to ipfw rules"); 160210284SjmallettSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, 161210284Sjmallett CTLFLAG_RW, &VNET_NAME(verbose_limit), 0, 162210284Sjmallett "Set upper limit of matches of ipfw rules logged"); 163210284SjmallettSYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD, 164210284Sjmallett &dummy_def, 0, 165210284Sjmallett "The default/max possible rule number."); 166210284SjmallettSYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, tables_max, CTLFLAG_RD, 167210284Sjmallett &dummy_tables_max, 0, 168210284Sjmallett "The maximum number of tables."); 169210284SjmallettSYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN, 170210284Sjmallett &default_to_accept, 0, 171210284Sjmallett "Make the default rule accept all packets."); 172210284SjmallettTUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept); 173210284SjmallettSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count, 174210284Sjmallett CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0, 175210284Sjmallett "Number of static rules"); 176210284Sjmallett 177210284Sjmallett#ifdef INET6 178210284SjmallettSYSCTL_DECL(_net_inet6_ip6); 179210284SjmallettSYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 180210284SjmallettSYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs, 181210284Sjmallett CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0, 182210284Sjmallett "Deny packets with unknown IPv6 Extension Headers"); 183210284Sjmallett#endif /* INET6 */ 184210284Sjmallett 185210284SjmallettSYSEND 186210284Sjmallett 187210284Sjmallett#endif /* SYSCTL_NODE */ 188210284Sjmallett 189210284Sjmallett 190210284Sjmallett/* 191210284Sjmallett * Some macros used in the various matching options. 192210284Sjmallett * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 193210284Sjmallett * Other macros just cast void * into the appropriate type 194210284Sjmallett */ 195210284Sjmallett#define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 196210284Sjmallett#define TCP(p) ((struct tcphdr *)(p)) 197210284Sjmallett#define SCTP(p) ((struct sctphdr *)(p)) 198210284Sjmallett#define UDP(p) ((struct udphdr *)(p)) 199210284Sjmallett#define ICMP(p) ((struct icmphdr *)(p)) 200210284Sjmallett#define ICMP6(p) ((struct icmp6_hdr *)(p)) 201210284Sjmallett 202210284Sjmallettstatic __inline int 203210284Sjmalletticmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd) 204210284Sjmallett{ 205232812Sjmallett int type = icmp->icmp_type; 206232812Sjmallett 207232812Sjmallett return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) ); 208232812Sjmallett} 209232812Sjmallett 210232812Sjmallett#define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \ 211232812Sjmallett (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) ) 212232812Sjmallett 213232812Sjmallettstatic int 214232812Sjmallettis_icmp_query(struct icmphdr *icmp) 215232812Sjmallett{ 216232812Sjmallett int type = icmp->icmp_type; 217232812Sjmallett 218232812Sjmallett return (type <= ICMP_MAXTYPE && (TT & (1<<type)) ); 219232812Sjmallett} 220232812Sjmallett#undef TT 221232812Sjmallett 222232812Sjmallett/* 223232812Sjmallett * The following checks use two arrays of 8 or 16 bits to store the 224232812Sjmallett * bits that we want set or clear, respectively. They are in the 225232812Sjmallett * low and high half of cmd->arg1 or cmd->d[0]. 226232812Sjmallett * 227232812Sjmallett * We scan options and store the bits we find set. We succeed if 228232812Sjmallett * 229232812Sjmallett * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear 230232812Sjmallett * 231232812Sjmallett * The code is sometimes optimized not to store additional variables. 232232812Sjmallett */ 233232812Sjmallett 234232812Sjmallettstatic int 235232812Sjmallettflags_match(ipfw_insn *cmd, u_int8_t bits) 236232812Sjmallett{ 237232812Sjmallett u_char want_clear; 238232812Sjmallett bits = ~bits; 239210284Sjmallett 240210284Sjmallett if ( ((cmd->arg1 & 0xff) & bits) != 0) 241210284Sjmallett return 0; /* some bits we want set were clear */ 242210284Sjmallett want_clear = (cmd->arg1 >> 8) & 0xff; 243210284Sjmallett if ( (want_clear & bits) != want_clear) 244210284Sjmallett return 0; /* some bits we want clear were set */ 245210284Sjmallett return 1; 246210284Sjmallett} 247210284Sjmallett 248210284Sjmallettstatic int 249210284Sjmallettipopts_match(struct ip *ip, ipfw_insn *cmd) 250210284Sjmallett{ 251210284Sjmallett int optlen, bits = 0; 252210284Sjmallett u_char *cp = (u_char *)(ip + 1); 253210284Sjmallett int x = (ip->ip_hl << 2) - sizeof (struct ip); 254210284Sjmallett 255210284Sjmallett for (; x > 0; x -= optlen, cp += optlen) { 256210284Sjmallett int opt = cp[IPOPT_OPTVAL]; 257232812Sjmallett 258232812Sjmallett if (opt == IPOPT_EOL) 259232812Sjmallett break; 260215990Sjmallett if (opt == IPOPT_NOP) 261215990Sjmallett optlen = 1; 262215990Sjmallett else { 263215990Sjmallett optlen = cp[IPOPT_OLEN]; 264215990Sjmallett if (optlen <= 0 || optlen > x) 265215990Sjmallett return 0; /* invalid or truncated */ 266215990Sjmallett } 267215990Sjmallett switch (opt) { 268215990Sjmallett 269215990Sjmallett default: 270215990Sjmallett break; 271215990Sjmallett 272215990Sjmallett case IPOPT_LSRR: 273215990Sjmallett bits |= IP_FW_IPOPT_LSRR; 274215990Sjmallett break; 275215990Sjmallett 276210284Sjmallett case IPOPT_SSRR: 277210284Sjmallett bits |= IP_FW_IPOPT_SSRR; 278210284Sjmallett break; 279210284Sjmallett 280210284Sjmallett case IPOPT_RR: 281210284Sjmallett bits |= IP_FW_IPOPT_RR; 282210284Sjmallett break; 283210284Sjmallett 284215990Sjmallett case IPOPT_TS: 285210284Sjmallett bits |= IP_FW_IPOPT_TS; 286210284Sjmallett break; 287210284Sjmallett } 288210284Sjmallett } 289210284Sjmallett return (flags_match(cmd, bits)); 290210284Sjmallett} 291210284Sjmallett 292210284Sjmallettstatic int 293210284Sjmalletttcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd) 294210284Sjmallett{ 295215990Sjmallett int optlen, bits = 0; 296215990Sjmallett u_char *cp = (u_char *)(tcp + 1); 297210284Sjmallett int x = (tcp->th_off << 2) - sizeof(struct tcphdr); 298210284Sjmallett 299232812Sjmallett for (; x > 0; x -= optlen, cp += optlen) { 300232812Sjmallett int opt = cp[0]; 301232812Sjmallett if (opt == TCPOPT_EOL) 302210284Sjmallett break; 303232812Sjmallett if (opt == TCPOPT_NOP) 304232812Sjmallett optlen = 1; 305232812Sjmallett else { 306210284Sjmallett optlen = cp[1]; 307232812Sjmallett if (optlen <= 0) 308232812Sjmallett break; 309210284Sjmallett } 310232812Sjmallett 311232812Sjmallett switch (opt) { 312210284Sjmallett 313210284Sjmallett default: 314210284Sjmallett break; 315210284Sjmallett 316210284Sjmallett case TCPOPT_MAXSEG: 317210284Sjmallett bits |= IP_FW_TCPOPT_MSS; 318210284Sjmallett break; 319210284Sjmallett 320210284Sjmallett case TCPOPT_WINDOW: 321210284Sjmallett bits |= IP_FW_TCPOPT_WINDOW; 322210284Sjmallett break; 323210284Sjmallett 324232812Sjmallett case TCPOPT_SACK_PERMITTED: 325232812Sjmallett case TCPOPT_SACK: 326232812Sjmallett bits |= IP_FW_TCPOPT_SACK; 327232812Sjmallett break; 328232812Sjmallett 329232812Sjmallett case TCPOPT_TIMESTAMP: 330232812Sjmallett bits |= IP_FW_TCPOPT_TS; 331232812Sjmallett break; 332232812Sjmallett 333232812Sjmallett } 334232812Sjmallett } 335210284Sjmallett return (flags_match(cmd, bits)); 336210284Sjmallett} 337210284Sjmallett 338210284Sjmallettstatic int 339210284Sjmallettiface_match(struct ifnet *ifp, ipfw_insn_if *cmd) 340210284Sjmallett{ 341210284Sjmallett if (ifp == NULL) /* no iface with this packet, match fails */ 342210284Sjmallett return 0; 343210284Sjmallett /* Check by name or by IP address */ 344210284Sjmallett if (cmd->name[0] != '\0') { /* match by name */ 345210284Sjmallett /* Check name */ 346232812Sjmallett if (cmd->p.glob) { 347232812Sjmallett if (fnmatch(cmd->name, ifp->if_xname, 0) == 0) 348232812Sjmallett return(1); 349232812Sjmallett } else { 350232812Sjmallett if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0) 351232812Sjmallett return(1); 352232812Sjmallett } 353232812Sjmallett } else { 354232812Sjmallett#ifdef __FreeBSD__ /* and OSX too ? */ 355232812Sjmallett struct ifaddr *ia; 356232812Sjmallett 357210284Sjmallett if_addr_rlock(ifp); 358210284Sjmallett TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 359210284Sjmallett if (ia->ifa_addr->sa_family != AF_INET) 360210284Sjmallett continue; 361210284Sjmallett if (cmd->p.ip.s_addr == ((struct sockaddr_in *) 362210284Sjmallett (ia->ifa_addr))->sin_addr.s_addr) { 363232812Sjmallett if_addr_runlock(ifp); 364210284Sjmallett return(1); /* match */ 365210284Sjmallett } 366210284Sjmallett } 367210284Sjmallett if_addr_runlock(ifp); 368210284Sjmallett#endif /* __FreeBSD__ */ 369210284Sjmallett } 370210284Sjmallett return(0); /* no match, fail ... */ 371210284Sjmallett} 372210284Sjmallett 373210284Sjmallett/* 374210284Sjmallett * The verify_path function checks if a route to the src exists and 375210284Sjmallett * if it is reachable via ifp (when provided). 376210284Sjmallett * 377210284Sjmallett * The 'verrevpath' option checks that the interface that an IP packet 378210284Sjmallett * arrives on is the same interface that traffic destined for the 379210284Sjmallett * packet's source address would be routed out of. 380232812Sjmallett * The 'versrcreach' option just checks that the source address is 381232812Sjmallett * reachable via any route (except default) in the routing table. 382210284Sjmallett * These two are a measure to block forged packets. This is also 383210284Sjmallett * commonly known as "anti-spoofing" or Unicast Reverse Path 384210284Sjmallett * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs 385232812Sjmallett * is purposely reminiscent of the Cisco IOS command, 386232812Sjmallett * 387210284Sjmallett * ip verify unicast reverse-path 388210284Sjmallett * ip verify unicast source reachable-via any 389210284Sjmallett * 390210284Sjmallett * which implements the same functionality. But note that the syntax 391210284Sjmallett * is misleading, and the check may be performed on all IP packets 392232812Sjmallett * whether unicast, multicast, or broadcast. 393215990Sjmallett */ 394232812Sjmallettstatic int 395232812Sjmallettverify_path(struct in_addr src, struct ifnet *ifp, u_int fib) 396232812Sjmallett{ 397232812Sjmallett#ifndef __FreeBSD__ 398232812Sjmallett return 0; 399232812Sjmallett#else 400232812Sjmallett struct route ro; 401232812Sjmallett struct sockaddr_in *dst; 402232812Sjmallett 403232812Sjmallett bzero(&ro, sizeof(ro)); 404232812Sjmallett 405232812Sjmallett dst = (struct sockaddr_in *)&(ro.ro_dst); 406232812Sjmallett dst->sin_family = AF_INET; 407232812Sjmallett dst->sin_len = sizeof(*dst); 408232812Sjmallett dst->sin_addr = src; 409232812Sjmallett in_rtalloc_ign(&ro, 0, fib); 410215990Sjmallett 411232812Sjmallett if (ro.ro_rt == NULL) 412232812Sjmallett return 0; 413232812Sjmallett 414232812Sjmallett /* 415232812Sjmallett * If ifp is provided, check for equality with rtentry. 416232812Sjmallett * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 417232812Sjmallett * in order to pass packets injected back by if_simloop(): 418232812Sjmallett * if useloopback == 1 routing entry (via lo0) for our own address 419232812Sjmallett * may exist, so we need to handle routing assymetry. 420232812Sjmallett */ 421232812Sjmallett if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 422232812Sjmallett RTFREE(ro.ro_rt); 423232812Sjmallett return 0; 424232812Sjmallett } 425232812Sjmallett 426232812Sjmallett /* if no ifp provided, check if rtentry is not default route */ 427232812Sjmallett if (ifp == NULL && 428232812Sjmallett satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) { 429232812Sjmallett RTFREE(ro.ro_rt); 430232812Sjmallett return 0; 431232812Sjmallett } 432232812Sjmallett 433232812Sjmallett /* or if this is a blackhole/reject route */ 434232812Sjmallett if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 435232812Sjmallett RTFREE(ro.ro_rt); 436232812Sjmallett return 0; 437232812Sjmallett } 438232812Sjmallett 439232812Sjmallett /* found valid route */ 440232812Sjmallett RTFREE(ro.ro_rt); 441232812Sjmallett return 1; 442232812Sjmallett#endif /* __FreeBSD__ */ 443232812Sjmallett} 444232812Sjmallett 445232812Sjmallett#ifdef INET6 446232812Sjmallett/* 447232812Sjmallett * ipv6 specific rules here... 448232812Sjmallett */ 449232812Sjmallettstatic __inline int 450232812Sjmalletticmp6type_match (int type, ipfw_insn_u32 *cmd) 451232812Sjmallett{ 452232812Sjmallett return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) ); 453232812Sjmallett} 454232812Sjmallett 455232812Sjmallettstatic int 456232812Sjmallettflow6id_match( int curr_flow, ipfw_insn_u32 *cmd ) 457232812Sjmallett{ 458232812Sjmallett int i; 459232812Sjmallett for (i=0; i <= cmd->o.arg1; ++i ) 460232812Sjmallett if (curr_flow == cmd->d[i] ) 461215990Sjmallett return 1; 462215990Sjmallett return 0; 463232812Sjmallett} 464232812Sjmallett 465232812Sjmallett/* support for IP6_*_ME opcodes */ 466215990Sjmallettstatic int 467210284Sjmallettsearch_ip6_addr_net (struct in6_addr * ip6_addr) 468210284Sjmallett{ 469210284Sjmallett struct ifnet *mdc; 470210284Sjmallett struct ifaddr *mdc2; 471210284Sjmallett struct in6_ifaddr *fdm; 472210284Sjmallett struct in6_addr copia; 473210284Sjmallett 474210284Sjmallett TAILQ_FOREACH(mdc, &V_ifnet, if_link) { 475210284Sjmallett if_addr_rlock(mdc); 476210284Sjmallett TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) { 477210284Sjmallett if (mdc2->ifa_addr->sa_family == AF_INET6) { 478210284Sjmallett fdm = (struct in6_ifaddr *)mdc2; 479210284Sjmallett copia = fdm->ia_addr.sin6_addr; 480210284Sjmallett /* need for leaving scope_id in the sock_addr */ 481210284Sjmallett in6_clearscope(&copia); 482210284Sjmallett if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) { 483210284Sjmallett if_addr_runlock(mdc); 484210284Sjmallett return 1; 485210284Sjmallett } 486210284Sjmallett } 487232812Sjmallett } 488232812Sjmallett if_addr_runlock(mdc); 489232812Sjmallett } 490232812Sjmallett return 0; 491232812Sjmallett} 492232812Sjmallett 493232812Sjmallettstatic int 494232812Sjmallettverify_path6(struct in6_addr *src, struct ifnet *ifp) 495232812Sjmallett{ 496232812Sjmallett struct route_in6 ro; 497210284Sjmallett struct sockaddr_in6 *dst; 498210284Sjmallett 499210284Sjmallett bzero(&ro, sizeof(ro)); 500210284Sjmallett 501210284Sjmallett dst = (struct sockaddr_in6 * )&(ro.ro_dst); 502210284Sjmallett dst->sin6_family = AF_INET6; 503210284Sjmallett dst->sin6_len = sizeof(*dst); 504210284Sjmallett dst->sin6_addr = *src; 505210284Sjmallett /* XXX MRT 0 for ipv6 at this time */ 506210284Sjmallett rtalloc_ign((struct route *)&ro, 0); 507210284Sjmallett 508210284Sjmallett if (ro.ro_rt == NULL) 509210284Sjmallett return 0; 510210284Sjmallett 511210284Sjmallett /* 512210284Sjmallett * if ifp is provided, check for equality with rtentry 513210284Sjmallett * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 514210284Sjmallett * to support the case of sending packets to an address of our own. 515210284Sjmallett * (where the former interface is the first argument of if_simloop() 516210284Sjmallett * (=ifp), the latter is lo0) 517210284Sjmallett */ 518210284Sjmallett if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 519210284Sjmallett RTFREE(ro.ro_rt); 520210284Sjmallett return 0; 521210284Sjmallett } 522210284Sjmallett 523210284Sjmallett /* if no ifp provided, check if rtentry is not default route */ 524210284Sjmallett if (ifp == NULL && 525210284Sjmallett IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) { 526210284Sjmallett RTFREE(ro.ro_rt); 527210284Sjmallett return 0; 528210284Sjmallett } 529210284Sjmallett 530210284Sjmallett /* or if this is a blackhole/reject route */ 531210284Sjmallett if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 532210284Sjmallett RTFREE(ro.ro_rt); 533210284Sjmallett return 0; 534210284Sjmallett } 535210284Sjmallett 536210284Sjmallett /* found valid route */ 537210284Sjmallett RTFREE(ro.ro_rt); 538210284Sjmallett return 1; 539210284Sjmallett 540210284Sjmallett} 541210284Sjmallett 542210284Sjmallettstatic int 543210284Sjmallettis_icmp6_query(int icmp6_type) 544210284Sjmallett{ 545210284Sjmallett if ((icmp6_type <= ICMP6_MAXTYPE) && 546210284Sjmallett (icmp6_type == ICMP6_ECHO_REQUEST || 547210284Sjmallett icmp6_type == ICMP6_MEMBERSHIP_QUERY || 548210284Sjmallett icmp6_type == ICMP6_WRUREQUEST || 549210284Sjmallett icmp6_type == ICMP6_FQDN_QUERY || 550210284Sjmallett icmp6_type == ICMP6_NI_QUERY)) 551210284Sjmallett return (1); 552210284Sjmallett 553210284Sjmallett return (0); 554210284Sjmallett} 555210284Sjmallett 556210284Sjmallettstatic void 557210284Sjmallettsend_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6) 558210284Sjmallett{ 559210284Sjmallett struct mbuf *m; 560210284Sjmallett 561210284Sjmallett m = args->m; 562210284Sjmallett if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) { 563210284Sjmallett struct tcphdr *tcp; 564210284Sjmallett tcp = (struct tcphdr *)((char *)ip6 + hlen); 565210284Sjmallett 566210284Sjmallett if ((tcp->th_flags & TH_RST) == 0) { 567210284Sjmallett struct mbuf *m0; 568210284Sjmallett m0 = ipfw_send_pkt(args->m, &(args->f_id), 569210284Sjmallett ntohl(tcp->th_seq), ntohl(tcp->th_ack), 570210284Sjmallett tcp->th_flags | TH_RST); 571210284Sjmallett if (m0 != NULL) 572210284Sjmallett ip6_output(m0, NULL, NULL, 0, NULL, NULL, 573210284Sjmallett NULL); 574210284Sjmallett } 575210284Sjmallett FREE_PKT(m); 576210284Sjmallett } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */ 577210284Sjmallett#if 0 578210284Sjmallett /* 579232812Sjmallett * Unlike above, the mbufs need to line up with the ip6 hdr, 580232812Sjmallett * as the contents are read. We need to m_adj() the 581232812Sjmallett * needed amount. 582232812Sjmallett * The mbuf will however be thrown away so we can adjust it. 583232812Sjmallett * Remember we did an m_pullup on it already so we 584232812Sjmallett * can make some assumptions about contiguousness. 585232812Sjmallett */ 586232812Sjmallett if (args->L3offset) 587232812Sjmallett m_adj(m, args->L3offset); 588232812Sjmallett#endif 589232812Sjmallett icmp6_error(m, ICMP6_DST_UNREACH, code, 0); 590232812Sjmallett } else 591232812Sjmallett FREE_PKT(m); 592232812Sjmallett 593232812Sjmallett args->m = NULL; 594232812Sjmallett} 595232812Sjmallett 596232812Sjmallett#endif /* INET6 */ 597232812Sjmallett 598232812Sjmallett 599232812Sjmallett/* 600232812Sjmallett * sends a reject message, consuming the mbuf passed as an argument. 601232812Sjmallett */ 602232812Sjmallettstatic void 603232812Sjmallettsend_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip) 604232812Sjmallett{ 605232812Sjmallett 606232812Sjmallett#if 0 607232812Sjmallett /* XXX When ip is not guaranteed to be at mtod() we will 608232812Sjmallett * need to account for this */ 609232812Sjmallett * The mbuf will however be thrown away so we can adjust it. 610232812Sjmallett * Remember we did an m_pullup on it already so we 611232812Sjmallett * can make some assumptions about contiguousness. 612232812Sjmallett */ 613232812Sjmallett if (args->L3offset) 614232812Sjmallett m_adj(m, args->L3offset); 615232812Sjmallett#endif 616232812Sjmallett if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */ 617232812Sjmallett /* We need the IP header in host order for icmp_error(). */ 618232812Sjmallett SET_HOST_IPLEN(ip); 619232812Sjmallett icmp_error(args->m, ICMP_UNREACH, code, 0L, 0); 620232812Sjmallett } else if (args->f_id.proto == IPPROTO_TCP) { 621232812Sjmallett struct tcphdr *const tcp = 622232812Sjmallett L3HDR(struct tcphdr, mtod(args->m, struct ip *)); 623232812Sjmallett if ( (tcp->th_flags & TH_RST) == 0) { 624232812Sjmallett struct mbuf *m; 625232812Sjmallett m = ipfw_send_pkt(args->m, &(args->f_id), 626232812Sjmallett ntohl(tcp->th_seq), ntohl(tcp->th_ack), 627232812Sjmallett tcp->th_flags | TH_RST); 628232812Sjmallett if (m != NULL) 629232812Sjmallett ip_output(m, NULL, NULL, 0, NULL, NULL); 630232812Sjmallett } 631232812Sjmallett FREE_PKT(args->m); 632232812Sjmallett } else 633232812Sjmallett FREE_PKT(args->m); 634232812Sjmallett args->m = NULL; 635232812Sjmallett} 636232812Sjmallett 637232812Sjmallett/* 638232812Sjmallett * Support for uid/gid/jail lookup. These tests are expensive 639232812Sjmallett * (because we may need to look into the list of active sockets) 640232812Sjmallett * so we cache the results. ugid_lookupp is 0 if we have not 641232812Sjmallett * yet done a lookup, 1 if we succeeded, and -1 if we tried 642232812Sjmallett * and failed. The function always returns the match value. 643232812Sjmallett * We could actually spare the variable and use *uc, setting 644232812Sjmallett * it to '(void *)check_uidgid if we have no info, NULL if 645232812Sjmallett * we tried and failed, or any other value if successful. 646232812Sjmallett */ 647232812Sjmallettstatic int 648232812Sjmallettcheck_uidgid(ipfw_insn_u32 *insn, int proto, struct ifnet *oif, 649232812Sjmallett struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip, 650232812Sjmallett u_int16_t src_port, int *ugid_lookupp, 651232812Sjmallett struct ucred **uc, struct inpcb *inp) 652232812Sjmallett{ 653232812Sjmallett#ifndef __FreeBSD__ 654232812Sjmallett return cred_check(insn, proto, oif, 655232812Sjmallett dst_ip, dst_port, src_ip, src_port, 656232812Sjmallett (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb); 657232812Sjmallett#else /* FreeBSD */ 658232812Sjmallett struct inpcbinfo *pi; 659232812Sjmallett int wildcard; 660232812Sjmallett struct inpcb *pcb; 661232812Sjmallett int match; 662232812Sjmallett 663232812Sjmallett /* 664232812Sjmallett * Check to see if the UDP or TCP stack supplied us with 665232812Sjmallett * the PCB. If so, rather then holding a lock and looking 666232812Sjmallett * up the PCB, we can use the one that was supplied. 667232812Sjmallett */ 668232812Sjmallett if (inp && *ugid_lookupp == 0) { 669232812Sjmallett INP_LOCK_ASSERT(inp); 670232812Sjmallett if (inp->inp_socket != NULL) { 671232812Sjmallett *uc = crhold(inp->inp_cred); 672232812Sjmallett *ugid_lookupp = 1; 673232812Sjmallett } else 674232812Sjmallett *ugid_lookupp = -1; 675232812Sjmallett } 676232812Sjmallett /* 677232812Sjmallett * If we have already been here and the packet has no 678232812Sjmallett * PCB entry associated with it, then we can safely 679232812Sjmallett * assume that this is a no match. 680232812Sjmallett */ 681232812Sjmallett if (*ugid_lookupp == -1) 682232812Sjmallett return (0); 683232812Sjmallett if (proto == IPPROTO_TCP) { 684232812Sjmallett wildcard = 0; 685232812Sjmallett pi = &V_tcbinfo; 686232812Sjmallett } else if (proto == IPPROTO_UDP) { 687232812Sjmallett wildcard = INPLOOKUP_WILDCARD; 688232812Sjmallett pi = &V_udbinfo; 689232812Sjmallett } else 690232812Sjmallett return 0; 691232812Sjmallett match = 0; 692232812Sjmallett if (*ugid_lookupp == 0) { 693232812Sjmallett INP_INFO_RLOCK(pi); 694232812Sjmallett pcb = (oif) ? 695232812Sjmallett in_pcblookup_hash(pi, 696232812Sjmallett dst_ip, htons(dst_port), 697232812Sjmallett src_ip, htons(src_port), 698232812Sjmallett wildcard, oif) : 699232812Sjmallett in_pcblookup_hash(pi, 700232812Sjmallett src_ip, htons(src_port), 701232812Sjmallett dst_ip, htons(dst_port), 702232812Sjmallett wildcard, NULL); 703232812Sjmallett if (pcb != NULL) { 704232812Sjmallett *uc = crhold(pcb->inp_cred); 705232812Sjmallett *ugid_lookupp = 1; 706232812Sjmallett } 707232812Sjmallett INP_INFO_RUNLOCK(pi); 708232812Sjmallett if (*ugid_lookupp == 0) { 709232812Sjmallett /* 710232812Sjmallett * We tried and failed, set the variable to -1 711232812Sjmallett * so we will not try again on this packet. 712232812Sjmallett */ 713232812Sjmallett *ugid_lookupp = -1; 714232812Sjmallett return (0); 715232812Sjmallett } 716232812Sjmallett } 717232812Sjmallett if (insn->o.opcode == O_UID) 718232812Sjmallett match = ((*uc)->cr_uid == (uid_t)insn->d[0]); 719232812Sjmallett else if (insn->o.opcode == O_GID) 720232812Sjmallett match = groupmember((gid_t)insn->d[0], *uc); 721232812Sjmallett else if (insn->o.opcode == O_JAIL) 722232812Sjmallett match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]); 723232812Sjmallett return match; 724232812Sjmallett#endif /* __FreeBSD__ */ 725232812Sjmallett} 726232812Sjmallett 727232812Sjmallett/* 728232812Sjmallett * Helper function to set args with info on the rule after the matching 729232812Sjmallett * one. slot is precise, whereas we guess rule_id as they are 730232812Sjmallett * assigned sequentially. 731232812Sjmallett */ 732232812Sjmallettstatic inline void 733232812Sjmallettset_match(struct ip_fw_args *args, int slot, 734232812Sjmallett struct ip_fw_chain *chain) 735232812Sjmallett{ 736232812Sjmallett args->rule.chain_id = chain->id; 737232812Sjmallett args->rule.slot = slot + 1; /* we use 0 as a marker */ 738232812Sjmallett args->rule.rule_id = 1 + chain->map[slot]->id; 739232812Sjmallett args->rule.rulenum = chain->map[slot]->rulenum; 740232812Sjmallett} 741232812Sjmallett 742232812Sjmallett/* 743232812Sjmallett * The main check routine for the firewall. 744232812Sjmallett * 745232812Sjmallett * All arguments are in args so we can modify them and return them 746232812Sjmallett * back to the caller. 747232812Sjmallett * 748232812Sjmallett * Parameters: 749232812Sjmallett * 750232812Sjmallett * args->m (in/out) The packet; we set to NULL when/if we nuke it. 751232812Sjmallett * Starts with the IP header. 752232812Sjmallett * args->eh (in) Mac header if present, NULL for layer3 packet. 753232812Sjmallett * args->L3offset Number of bytes bypassed if we came from L2. 754232812Sjmallett * e.g. often sizeof(eh) ** NOTYET ** 755232812Sjmallett * args->oif Outgoing interface, NULL if packet is incoming. 756232812Sjmallett * The incoming interface is in the mbuf. (in) 757232812Sjmallett * args->divert_rule (in/out) 758232812Sjmallett * Skip up to the first rule past this rule number; 759232812Sjmallett * upon return, non-zero port number for divert or tee. 760232812Sjmallett * 761232812Sjmallett * args->rule Pointer to the last matching rule (in/out) 762232812Sjmallett * args->next_hop Socket we are forwarding to (out). 763232812Sjmallett * args->f_id Addresses grabbed from the packet (out) 764232812Sjmallett * args->rule.info a cookie depending on rule action 765232812Sjmallett * 766232812Sjmallett * Return value: 767232812Sjmallett * 768232812Sjmallett * IP_FW_PASS the packet must be accepted 769232812Sjmallett * IP_FW_DENY the packet must be dropped 770232812Sjmallett * IP_FW_DIVERT divert packet, port in m_tag 771232812Sjmallett * IP_FW_TEE tee packet, port in m_tag 772232812Sjmallett * IP_FW_DUMMYNET to dummynet, pipe in args->cookie 773232812Sjmallett * IP_FW_NETGRAPH into netgraph, cookie args->cookie 774232812Sjmallett * args->rule contains the matching rule, 775232812Sjmallett * args->rule.info has additional information. 776232812Sjmallett * 777232812Sjmallett */ 778232812Sjmallettint 779232812Sjmallettipfw_chk(struct ip_fw_args *args) 780232812Sjmallett{ 781232812Sjmallett 782232812Sjmallett /* 783232812Sjmallett * Local variables holding state while processing a packet: 784232812Sjmallett * 785232812Sjmallett * IMPORTANT NOTE: to speed up the processing of rules, there 786232812Sjmallett * are some assumption on the values of the variables, which 787232812Sjmallett * are documented here. Should you change them, please check 788232812Sjmallett * the implementation of the various instructions to make sure 789232812Sjmallett * that they still work. 790232812Sjmallett * 791232812Sjmallett * args->eh The MAC header. It is non-null for a layer2 792232812Sjmallett * packet, it is NULL for a layer-3 packet. 793232812Sjmallett * **notyet** 794232812Sjmallett * args->L3offset Offset in the packet to the L3 (IP or equiv.) header. 795232812Sjmallett * 796232812Sjmallett * m | args->m Pointer to the mbuf, as received from the caller. 797232812Sjmallett * It may change if ipfw_chk() does an m_pullup, or if it 798232812Sjmallett * consumes the packet because it calls send_reject(). 799232812Sjmallett * XXX This has to change, so that ipfw_chk() never modifies 800232812Sjmallett * or consumes the buffer. 801232812Sjmallett * ip is the beginning of the ip(4 or 6) header. 802210284Sjmallett * Calculated by adding the L3offset to the start of data. 803210284Sjmallett * (Until we start using L3offset, the packet is 804210284Sjmallett * supposed to start with the ip header). 805210284Sjmallett */ 806210284Sjmallett struct mbuf *m = args->m; 807 struct ip *ip = mtod(m, struct ip *); 808 809 /* 810 * For rules which contain uid/gid or jail constraints, cache 811 * a copy of the users credentials after the pcb lookup has been 812 * executed. This will speed up the processing of rules with 813 * these types of constraints, as well as decrease contention 814 * on pcb related locks. 815 */ 816#ifndef __FreeBSD__ 817 struct bsd_ucred ucred_cache; 818#else 819 struct ucred *ucred_cache = NULL; 820#endif 821 int ucred_lookup = 0; 822 823 /* 824 * oif | args->oif If NULL, ipfw_chk has been called on the 825 * inbound path (ether_input, ip_input). 826 * If non-NULL, ipfw_chk has been called on the outbound path 827 * (ether_output, ip_output). 828 */ 829 struct ifnet *oif = args->oif; 830 831 int f_pos = 0; /* index of current rule in the array */ 832 int retval = 0; 833 834 /* 835 * hlen The length of the IP header. 836 */ 837 u_int hlen = 0; /* hlen >0 means we have an IP pkt */ 838 839 /* 840 * offset The offset of a fragment. offset != 0 means that 841 * we have a fragment at this offset of an IPv4 packet. 842 * offset == 0 means that (if this is an IPv4 packet) 843 * this is the first or only fragment. 844 * For IPv6 offset == 0 means there is no Fragment Header. 845 * If offset != 0 for IPv6 always use correct mask to 846 * get the correct offset because we add IP6F_MORE_FRAG 847 * to be able to dectect the first fragment which would 848 * otherwise have offset = 0. 849 */ 850 u_short offset = 0; 851 852 /* 853 * Local copies of addresses. They are only valid if we have 854 * an IP packet. 855 * 856 * proto The protocol. Set to 0 for non-ip packets, 857 * or to the protocol read from the packet otherwise. 858 * proto != 0 means that we have an IPv4 packet. 859 * 860 * src_port, dst_port port numbers, in HOST format. Only 861 * valid for TCP and UDP packets. 862 * 863 * src_ip, dst_ip ip addresses, in NETWORK format. 864 * Only valid for IPv4 packets. 865 */ 866 uint8_t proto; 867 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */ 868 struct in_addr src_ip, dst_ip; /* NOTE: network format */ 869 uint16_t iplen=0; 870 int pktlen; 871 uint16_t etype = 0; /* Host order stored ether type */ 872 873 /* 874 * dyn_dir = MATCH_UNKNOWN when rules unchecked, 875 * MATCH_NONE when checked and not matched (q = NULL), 876 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) 877 */ 878 int dyn_dir = MATCH_UNKNOWN; 879 ipfw_dyn_rule *q = NULL; 880 struct ip_fw_chain *chain = &V_layer3_chain; 881 882 /* 883 * We store in ulp a pointer to the upper layer protocol header. 884 * In the ipv4 case this is easy to determine from the header, 885 * but for ipv6 we might have some additional headers in the middle. 886 * ulp is NULL if not found. 887 */ 888 void *ulp = NULL; /* upper layer protocol pointer. */ 889 890 /* XXX ipv6 variables */ 891 int is_ipv6 = 0; 892 uint8_t icmp6_type = 0; 893 uint16_t ext_hd = 0; /* bits vector for extension header filtering */ 894 /* end of ipv6 variables */ 895 896 int is_ipv4 = 0; 897 898 int done = 0; /* flag to exit the outer loop */ 899 900 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready)) 901 return (IP_FW_PASS); /* accept */ 902 903 dst_ip.s_addr = 0; /* make sure it is initialized */ 904 src_ip.s_addr = 0; /* make sure it is initialized */ 905 pktlen = m->m_pkthdr.len; 906 args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */ 907 proto = args->f_id.proto = 0; /* mark f_id invalid */ 908 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */ 909 910/* 911 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, 912 * then it sets p to point at the offset "len" in the mbuf. WARNING: the 913 * pointer might become stale after other pullups (but we never use it 914 * this way). 915 */ 916#define PULLUP_TO(_len, p, T) \ 917do { \ 918 int x = (_len) + sizeof(T); \ 919 if ((m)->m_len < x) { \ 920 args->m = m = m_pullup(m, x); \ 921 if (m == NULL) \ 922 goto pullup_failed; \ 923 } \ 924 p = (mtod(m, char *) + (_len)); \ 925} while (0) 926 927 /* 928 * if we have an ether header, 929 */ 930 if (args->eh) 931 etype = ntohs(args->eh->ether_type); 932 933 /* Identify IP packets and fill up variables. */ 934 if (pktlen >= sizeof(struct ip6_hdr) && 935 (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) { 936 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip; 937 is_ipv6 = 1; 938 args->f_id.addr_type = 6; 939 hlen = sizeof(struct ip6_hdr); 940 proto = ip6->ip6_nxt; 941 942 /* Search extension headers to find upper layer protocols */ 943 while (ulp == NULL) { 944 switch (proto) { 945 case IPPROTO_ICMPV6: 946 PULLUP_TO(hlen, ulp, struct icmp6_hdr); 947 icmp6_type = ICMP6(ulp)->icmp6_type; 948 break; 949 950 case IPPROTO_TCP: 951 PULLUP_TO(hlen, ulp, struct tcphdr); 952 dst_port = TCP(ulp)->th_dport; 953 src_port = TCP(ulp)->th_sport; 954 /* save flags for dynamic rules */ 955 args->f_id._flags = TCP(ulp)->th_flags; 956 break; 957 958 case IPPROTO_SCTP: 959 PULLUP_TO(hlen, ulp, struct sctphdr); 960 src_port = SCTP(ulp)->src_port; 961 dst_port = SCTP(ulp)->dest_port; 962 break; 963 964 case IPPROTO_UDP: 965 PULLUP_TO(hlen, ulp, struct udphdr); 966 dst_port = UDP(ulp)->uh_dport; 967 src_port = UDP(ulp)->uh_sport; 968 break; 969 970 case IPPROTO_HOPOPTS: /* RFC 2460 */ 971 PULLUP_TO(hlen, ulp, struct ip6_hbh); 972 ext_hd |= EXT_HOPOPTS; 973 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 974 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 975 ulp = NULL; 976 break; 977 978 case IPPROTO_ROUTING: /* RFC 2460 */ 979 PULLUP_TO(hlen, ulp, struct ip6_rthdr); 980 switch (((struct ip6_rthdr *)ulp)->ip6r_type) { 981 case 0: 982 ext_hd |= EXT_RTHDR0; 983 break; 984 case 2: 985 ext_hd |= EXT_RTHDR2; 986 break; 987 default: 988 printf("IPFW2: IPV6 - Unknown Routing " 989 "Header type(%d)\n", 990 ((struct ip6_rthdr *)ulp)->ip6r_type); 991 if (V_fw_deny_unknown_exthdrs) 992 return (IP_FW_DENY); 993 break; 994 } 995 ext_hd |= EXT_ROUTING; 996 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3; 997 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; 998 ulp = NULL; 999 break; 1000 1001 case IPPROTO_FRAGMENT: /* RFC 2460 */ 1002 PULLUP_TO(hlen, ulp, struct ip6_frag); 1003 ext_hd |= EXT_FRAGMENT; 1004 hlen += sizeof (struct ip6_frag); 1005 proto = ((struct ip6_frag *)ulp)->ip6f_nxt; 1006 offset = ((struct ip6_frag *)ulp)->ip6f_offlg & 1007 IP6F_OFF_MASK; 1008 /* Add IP6F_MORE_FRAG for offset of first 1009 * fragment to be != 0. */ 1010 offset |= ((struct ip6_frag *)ulp)->ip6f_offlg & 1011 IP6F_MORE_FRAG; 1012 if (offset == 0) { 1013 printf("IPFW2: IPV6 - Invalid Fragment " 1014 "Header\n"); 1015 if (V_fw_deny_unknown_exthdrs) 1016 return (IP_FW_DENY); 1017 break; 1018 } 1019 args->f_id.extra = 1020 ntohl(((struct ip6_frag *)ulp)->ip6f_ident); 1021 ulp = NULL; 1022 break; 1023 1024 case IPPROTO_DSTOPTS: /* RFC 2460 */ 1025 PULLUP_TO(hlen, ulp, struct ip6_hbh); 1026 ext_hd |= EXT_DSTOPTS; 1027 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 1028 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 1029 ulp = NULL; 1030 break; 1031 1032 case IPPROTO_AH: /* RFC 2402 */ 1033 PULLUP_TO(hlen, ulp, struct ip6_ext); 1034 ext_hd |= EXT_AH; 1035 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2; 1036 proto = ((struct ip6_ext *)ulp)->ip6e_nxt; 1037 ulp = NULL; 1038 break; 1039 1040 case IPPROTO_ESP: /* RFC 2406 */ 1041 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */ 1042 /* Anything past Seq# is variable length and 1043 * data past this ext. header is encrypted. */ 1044 ext_hd |= EXT_ESP; 1045 break; 1046 1047 case IPPROTO_NONE: /* RFC 2460 */ 1048 /* 1049 * Packet ends here, and IPv6 header has 1050 * already been pulled up. If ip6e_len!=0 1051 * then octets must be ignored. 1052 */ 1053 ulp = ip; /* non-NULL to get out of loop. */ 1054 break; 1055 1056 case IPPROTO_OSPFIGP: 1057 /* XXX OSPF header check? */ 1058 PULLUP_TO(hlen, ulp, struct ip6_ext); 1059 break; 1060 1061 case IPPROTO_PIM: 1062 /* XXX PIM header check? */ 1063 PULLUP_TO(hlen, ulp, struct pim); 1064 break; 1065 1066 case IPPROTO_CARP: 1067 PULLUP_TO(hlen, ulp, struct carp_header); 1068 if (((struct carp_header *)ulp)->carp_version != 1069 CARP_VERSION) 1070 return (IP_FW_DENY); 1071 if (((struct carp_header *)ulp)->carp_type != 1072 CARP_ADVERTISEMENT) 1073 return (IP_FW_DENY); 1074 break; 1075 1076 case IPPROTO_IPV6: /* RFC 2893 */ 1077 PULLUP_TO(hlen, ulp, struct ip6_hdr); 1078 break; 1079 1080 case IPPROTO_IPV4: /* RFC 2893 */ 1081 PULLUP_TO(hlen, ulp, struct ip); 1082 break; 1083 1084 default: 1085 printf("IPFW2: IPV6 - Unknown Extension " 1086 "Header(%d), ext_hd=%x\n", proto, ext_hd); 1087 if (V_fw_deny_unknown_exthdrs) 1088 return (IP_FW_DENY); 1089 PULLUP_TO(hlen, ulp, struct ip6_ext); 1090 break; 1091 } /*switch */ 1092 } 1093 ip = mtod(m, struct ip *); 1094 ip6 = (struct ip6_hdr *)ip; 1095 args->f_id.src_ip6 = ip6->ip6_src; 1096 args->f_id.dst_ip6 = ip6->ip6_dst; 1097 args->f_id.src_ip = 0; 1098 args->f_id.dst_ip = 0; 1099 args->f_id.flow_id6 = ntohl(ip6->ip6_flow); 1100 } else if (pktlen >= sizeof(struct ip) && 1101 (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) { 1102 is_ipv4 = 1; 1103 hlen = ip->ip_hl << 2; 1104 args->f_id.addr_type = 4; 1105 1106 /* 1107 * Collect parameters into local variables for faster matching. 1108 */ 1109 proto = ip->ip_p; 1110 src_ip = ip->ip_src; 1111 dst_ip = ip->ip_dst; 1112 offset = ntohs(ip->ip_off) & IP_OFFMASK; 1113 iplen = ntohs(ip->ip_len); 1114 pktlen = iplen < pktlen ? iplen : pktlen; 1115 1116 if (offset == 0) { 1117 switch (proto) { 1118 case IPPROTO_TCP: 1119 PULLUP_TO(hlen, ulp, struct tcphdr); 1120 dst_port = TCP(ulp)->th_dport; 1121 src_port = TCP(ulp)->th_sport; 1122 /* save flags for dynamic rules */ 1123 args->f_id._flags = TCP(ulp)->th_flags; 1124 break; 1125 1126 case IPPROTO_UDP: 1127 PULLUP_TO(hlen, ulp, struct udphdr); 1128 dst_port = UDP(ulp)->uh_dport; 1129 src_port = UDP(ulp)->uh_sport; 1130 break; 1131 1132 case IPPROTO_ICMP: 1133 PULLUP_TO(hlen, ulp, struct icmphdr); 1134 //args->f_id.flags = ICMP(ulp)->icmp_type; 1135 break; 1136 1137 default: 1138 break; 1139 } 1140 } 1141 1142 ip = mtod(m, struct ip *); 1143 args->f_id.src_ip = ntohl(src_ip.s_addr); 1144 args->f_id.dst_ip = ntohl(dst_ip.s_addr); 1145 } 1146#undef PULLUP_TO 1147 if (proto) { /* we may have port numbers, store them */ 1148 args->f_id.proto = proto; 1149 args->f_id.src_port = src_port = ntohs(src_port); 1150 args->f_id.dst_port = dst_port = ntohs(dst_port); 1151 } 1152 1153 IPFW_RLOCK(chain); 1154 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */ 1155 IPFW_RUNLOCK(chain); 1156 return (IP_FW_PASS); /* accept */ 1157 } 1158 if (args->rule.slot) { 1159 /* 1160 * Packet has already been tagged as a result of a previous 1161 * match on rule args->rule aka args->rule_id (PIPE, QUEUE, 1162 * REASS, NETGRAPH, DIVERT/TEE...) 1163 * Validate the slot and continue from the next one 1164 * if still present, otherwise do a lookup. 1165 */ 1166 f_pos = (args->rule.chain_id == chain->id) ? 1167 args->rule.slot : 1168 ipfw_find_rule(chain, args->rule.rulenum, 1169 args->rule.rule_id); 1170 } else { 1171 f_pos = 0; 1172 } 1173 1174 /* 1175 * Now scan the rules, and parse microinstructions for each rule. 1176 * We have two nested loops and an inner switch. Sometimes we 1177 * need to break out of one or both loops, or re-enter one of 1178 * the loops with updated variables. Loop variables are: 1179 * 1180 * f_pos (outer loop) points to the current rule. 1181 * On output it points to the matching rule. 1182 * done (outer loop) is used as a flag to break the loop. 1183 * l (inner loop) residual length of current rule. 1184 * cmd points to the current microinstruction. 1185 * 1186 * We break the inner loop by setting l=0 and possibly 1187 * cmdlen=0 if we don't want to advance cmd. 1188 * We break the outer loop by setting done=1 1189 * We can restart the inner loop by setting l>0 and f_pos, f, cmd 1190 * as needed. 1191 */ 1192 for (; f_pos < chain->n_rules; f_pos++) { 1193 ipfw_insn *cmd; 1194 uint32_t tablearg = 0; 1195 int l, cmdlen, skip_or; /* skip rest of OR block */ 1196 struct ip_fw *f; 1197 1198 f = chain->map[f_pos]; 1199 if (V_set_disable & (1 << f->set) ) 1200 continue; 1201 1202 skip_or = 0; 1203 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; 1204 l -= cmdlen, cmd += cmdlen) { 1205 int match; 1206 1207 /* 1208 * check_body is a jump target used when we find a 1209 * CHECK_STATE, and need to jump to the body of 1210 * the target rule. 1211 */ 1212 1213/* check_body: */ 1214 cmdlen = F_LEN(cmd); 1215 /* 1216 * An OR block (insn_1 || .. || insn_n) has the 1217 * F_OR bit set in all but the last instruction. 1218 * The first match will set "skip_or", and cause 1219 * the following instructions to be skipped until 1220 * past the one with the F_OR bit clear. 1221 */ 1222 if (skip_or) { /* skip this instruction */ 1223 if ((cmd->len & F_OR) == 0) 1224 skip_or = 0; /* next one is good */ 1225 continue; 1226 } 1227 match = 0; /* set to 1 if we succeed */ 1228 1229 switch (cmd->opcode) { 1230 /* 1231 * The first set of opcodes compares the packet's 1232 * fields with some pattern, setting 'match' if a 1233 * match is found. At the end of the loop there is 1234 * logic to deal with F_NOT and F_OR flags associated 1235 * with the opcode. 1236 */ 1237 case O_NOP: 1238 match = 1; 1239 break; 1240 1241 case O_FORWARD_MAC: 1242 printf("ipfw: opcode %d unimplemented\n", 1243 cmd->opcode); 1244 break; 1245 1246 case O_GID: 1247 case O_UID: 1248 case O_JAIL: 1249 /* 1250 * We only check offset == 0 && proto != 0, 1251 * as this ensures that we have a 1252 * packet with the ports info. 1253 */ 1254 if (offset!=0) 1255 break; 1256 if (is_ipv6) /* XXX to be fixed later */ 1257 break; 1258 if (proto == IPPROTO_TCP || 1259 proto == IPPROTO_UDP) 1260 match = check_uidgid( 1261 (ipfw_insn_u32 *)cmd, 1262 proto, oif, 1263 dst_ip, dst_port, 1264 src_ip, src_port, &ucred_lookup, 1265#ifdef __FreeBSD__ 1266 &ucred_cache, args->inp); 1267#else 1268 (void *)&ucred_cache, 1269 (struct inpcb *)args->m); 1270#endif 1271 break; 1272 1273 case O_RECV: 1274 match = iface_match(m->m_pkthdr.rcvif, 1275 (ipfw_insn_if *)cmd); 1276 break; 1277 1278 case O_XMIT: 1279 match = iface_match(oif, (ipfw_insn_if *)cmd); 1280 break; 1281 1282 case O_VIA: 1283 match = iface_match(oif ? oif : 1284 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd); 1285 break; 1286 1287 case O_MACADDR2: 1288 if (args->eh != NULL) { /* have MAC header */ 1289 u_int32_t *want = (u_int32_t *) 1290 ((ipfw_insn_mac *)cmd)->addr; 1291 u_int32_t *mask = (u_int32_t *) 1292 ((ipfw_insn_mac *)cmd)->mask; 1293 u_int32_t *hdr = (u_int32_t *)args->eh; 1294 1295 match = 1296 ( want[0] == (hdr[0] & mask[0]) && 1297 want[1] == (hdr[1] & mask[1]) && 1298 want[2] == (hdr[2] & mask[2]) ); 1299 } 1300 break; 1301 1302 case O_MAC_TYPE: 1303 if (args->eh != NULL) { 1304 u_int16_t *p = 1305 ((ipfw_insn_u16 *)cmd)->ports; 1306 int i; 1307 1308 for (i = cmdlen - 1; !match && i>0; 1309 i--, p += 2) 1310 match = (etype >= p[0] && 1311 etype <= p[1]); 1312 } 1313 break; 1314 1315 case O_FRAG: 1316 match = (offset != 0); 1317 break; 1318 1319 case O_IN: /* "out" is "not in" */ 1320 match = (oif == NULL); 1321 break; 1322 1323 case O_LAYER2: 1324 match = (args->eh != NULL); 1325 break; 1326 1327 case O_DIVERTED: 1328 { 1329 /* For diverted packets, args->rule.info 1330 * contains the divert port (in host format) 1331 * reason and direction. 1332 */ 1333 uint32_t i = args->rule.info; 1334 match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT && 1335 cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2); 1336 } 1337 break; 1338 1339 case O_PROTO: 1340 /* 1341 * We do not allow an arg of 0 so the 1342 * check of "proto" only suffices. 1343 */ 1344 match = (proto == cmd->arg1); 1345 break; 1346 1347 case O_IP_SRC: 1348 match = is_ipv4 && 1349 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1350 src_ip.s_addr); 1351 break; 1352 1353 case O_IP_SRC_LOOKUP: 1354 case O_IP_DST_LOOKUP: 1355 if (is_ipv4) { 1356 uint32_t key = 1357 (cmd->opcode == O_IP_DST_LOOKUP) ? 1358 dst_ip.s_addr : src_ip.s_addr; 1359 uint32_t v = 0; 1360 1361 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) { 1362 /* generic lookup. The key must be 1363 * in 32bit big-endian format. 1364 */ 1365 v = ((ipfw_insn_u32 *)cmd)->d[1]; 1366 if (v == 0) 1367 key = dst_ip.s_addr; 1368 else if (v == 1) 1369 key = src_ip.s_addr; 1370 else if (v == 6) /* dscp */ 1371 key = (ip->ip_tos >> 2) & 0x3f; 1372 else if (offset != 0) 1373 break; 1374 else if (proto != IPPROTO_TCP && 1375 proto != IPPROTO_UDP) 1376 break; 1377 else if (v == 2) 1378 key = htonl(dst_port); 1379 else if (v == 3) 1380 key = htonl(src_port); 1381 else if (v == 4 || v == 5) { 1382 check_uidgid( 1383 (ipfw_insn_u32 *)cmd, 1384 proto, oif, 1385 dst_ip, dst_port, 1386 src_ip, src_port, &ucred_lookup, 1387#ifdef __FreeBSD__ 1388 &ucred_cache, args->inp); 1389 if (v == 4 /* O_UID */) 1390 key = ucred_cache->cr_uid; 1391 else if (v == 5 /* O_JAIL */) 1392 key = ucred_cache->cr_prison->pr_id; 1393#else /* !__FreeBSD__ */ 1394 (void *)&ucred_cache, 1395 (struct inpcb *)args->m); 1396 if (v ==4 /* O_UID */) 1397 key = ucred_cache.uid; 1398 else if (v == 5 /* O_JAIL */) 1399 key = ucred_cache.xid; 1400#endif /* !__FreeBSD__ */ 1401 key = htonl(key); 1402 } else 1403 break; 1404 } 1405 match = ipfw_lookup_table(chain, 1406 cmd->arg1, key, &v); 1407 if (!match) 1408 break; 1409 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) 1410 match = 1411 ((ipfw_insn_u32 *)cmd)->d[0] == v; 1412 else 1413 tablearg = v; 1414 } 1415 break; 1416 1417 case O_IP_SRC_MASK: 1418 case O_IP_DST_MASK: 1419 if (is_ipv4) { 1420 uint32_t a = 1421 (cmd->opcode == O_IP_DST_MASK) ? 1422 dst_ip.s_addr : src_ip.s_addr; 1423 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; 1424 int i = cmdlen-1; 1425 1426 for (; !match && i>0; i-= 2, p+= 2) 1427 match = (p[0] == (a & p[1])); 1428 } 1429 break; 1430 1431 case O_IP_SRC_ME: 1432 if (is_ipv4) { 1433 struct ifnet *tif; 1434 1435 INADDR_TO_IFP(src_ip, tif); 1436 match = (tif != NULL); 1437 break; 1438 } 1439#ifdef INET6 1440 /* FALLTHROUGH */ 1441 case O_IP6_SRC_ME: 1442 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6); 1443#endif 1444 break; 1445 1446 case O_IP_DST_SET: 1447 case O_IP_SRC_SET: 1448 if (is_ipv4) { 1449 u_int32_t *d = (u_int32_t *)(cmd+1); 1450 u_int32_t addr = 1451 cmd->opcode == O_IP_DST_SET ? 1452 args->f_id.dst_ip : 1453 args->f_id.src_ip; 1454 1455 if (addr < d[0]) 1456 break; 1457 addr -= d[0]; /* subtract base */ 1458 match = (addr < cmd->arg1) && 1459 ( d[ 1 + (addr>>5)] & 1460 (1<<(addr & 0x1f)) ); 1461 } 1462 break; 1463 1464 case O_IP_DST: 1465 match = is_ipv4 && 1466 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1467 dst_ip.s_addr); 1468 break; 1469 1470 case O_IP_DST_ME: 1471 if (is_ipv4) { 1472 struct ifnet *tif; 1473 1474 INADDR_TO_IFP(dst_ip, tif); 1475 match = (tif != NULL); 1476 break; 1477 } 1478#ifdef INET6 1479 /* FALLTHROUGH */ 1480 case O_IP6_DST_ME: 1481 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6); 1482#endif 1483 break; 1484 1485 1486 case O_IP_SRCPORT: 1487 case O_IP_DSTPORT: 1488 /* 1489 * offset == 0 && proto != 0 is enough 1490 * to guarantee that we have a 1491 * packet with port info. 1492 */ 1493 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP) 1494 && offset == 0) { 1495 u_int16_t x = 1496 (cmd->opcode == O_IP_SRCPORT) ? 1497 src_port : dst_port ; 1498 u_int16_t *p = 1499 ((ipfw_insn_u16 *)cmd)->ports; 1500 int i; 1501 1502 for (i = cmdlen - 1; !match && i>0; 1503 i--, p += 2) 1504 match = (x>=p[0] && x<=p[1]); 1505 } 1506 break; 1507 1508 case O_ICMPTYPE: 1509 match = (offset == 0 && proto==IPPROTO_ICMP && 1510 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); 1511 break; 1512 1513#ifdef INET6 1514 case O_ICMP6TYPE: 1515 match = is_ipv6 && offset == 0 && 1516 proto==IPPROTO_ICMPV6 && 1517 icmp6type_match( 1518 ICMP6(ulp)->icmp6_type, 1519 (ipfw_insn_u32 *)cmd); 1520 break; 1521#endif /* INET6 */ 1522 1523 case O_IPOPT: 1524 match = (is_ipv4 && 1525 ipopts_match(ip, cmd) ); 1526 break; 1527 1528 case O_IPVER: 1529 match = (is_ipv4 && 1530 cmd->arg1 == ip->ip_v); 1531 break; 1532 1533 case O_IPID: 1534 case O_IPLEN: 1535 case O_IPTTL: 1536 if (is_ipv4) { /* only for IP packets */ 1537 uint16_t x; 1538 uint16_t *p; 1539 int i; 1540 1541 if (cmd->opcode == O_IPLEN) 1542 x = iplen; 1543 else if (cmd->opcode == O_IPTTL) 1544 x = ip->ip_ttl; 1545 else /* must be IPID */ 1546 x = ntohs(ip->ip_id); 1547 if (cmdlen == 1) { 1548 match = (cmd->arg1 == x); 1549 break; 1550 } 1551 /* otherwise we have ranges */ 1552 p = ((ipfw_insn_u16 *)cmd)->ports; 1553 i = cmdlen - 1; 1554 for (; !match && i>0; i--, p += 2) 1555 match = (x >= p[0] && x <= p[1]); 1556 } 1557 break; 1558 1559 case O_IPPRECEDENCE: 1560 match = (is_ipv4 && 1561 (cmd->arg1 == (ip->ip_tos & 0xe0)) ); 1562 break; 1563 1564 case O_IPTOS: 1565 match = (is_ipv4 && 1566 flags_match(cmd, ip->ip_tos)); 1567 break; 1568 1569 case O_TCPDATALEN: 1570 if (proto == IPPROTO_TCP && offset == 0) { 1571 struct tcphdr *tcp; 1572 uint16_t x; 1573 uint16_t *p; 1574 int i; 1575 1576 tcp = TCP(ulp); 1577 x = iplen - 1578 ((ip->ip_hl + tcp->th_off) << 2); 1579 if (cmdlen == 1) { 1580 match = (cmd->arg1 == x); 1581 break; 1582 } 1583 /* otherwise we have ranges */ 1584 p = ((ipfw_insn_u16 *)cmd)->ports; 1585 i = cmdlen - 1; 1586 for (; !match && i>0; i--, p += 2) 1587 match = (x >= p[0] && x <= p[1]); 1588 } 1589 break; 1590 1591 case O_TCPFLAGS: 1592 match = (proto == IPPROTO_TCP && offset == 0 && 1593 flags_match(cmd, TCP(ulp)->th_flags)); 1594 break; 1595 1596 case O_TCPOPTS: 1597 match = (proto == IPPROTO_TCP && offset == 0 && 1598 tcpopts_match(TCP(ulp), cmd)); 1599 break; 1600 1601 case O_TCPSEQ: 1602 match = (proto == IPPROTO_TCP && offset == 0 && 1603 ((ipfw_insn_u32 *)cmd)->d[0] == 1604 TCP(ulp)->th_seq); 1605 break; 1606 1607 case O_TCPACK: 1608 match = (proto == IPPROTO_TCP && offset == 0 && 1609 ((ipfw_insn_u32 *)cmd)->d[0] == 1610 TCP(ulp)->th_ack); 1611 break; 1612 1613 case O_TCPWIN: 1614 match = (proto == IPPROTO_TCP && offset == 0 && 1615 cmd->arg1 == TCP(ulp)->th_win); 1616 break; 1617 1618 case O_ESTAB: 1619 /* reject packets which have SYN only */ 1620 /* XXX should i also check for TH_ACK ? */ 1621 match = (proto == IPPROTO_TCP && offset == 0 && 1622 (TCP(ulp)->th_flags & 1623 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); 1624 break; 1625 1626 case O_ALTQ: { 1627 struct pf_mtag *at; 1628 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 1629 1630 match = 1; 1631 at = pf_find_mtag(m); 1632 if (at != NULL && at->qid != 0) 1633 break; 1634 at = pf_get_mtag(m); 1635 if (at == NULL) { 1636 /* 1637 * Let the packet fall back to the 1638 * default ALTQ. 1639 */ 1640 break; 1641 } 1642 at->qid = altq->qid; 1643 if (is_ipv4) 1644 at->af = AF_INET; 1645 else 1646 at->af = AF_LINK; 1647 at->hdr = ip; 1648 break; 1649 } 1650 1651 case O_LOG: 1652 ipfw_log(f, hlen, args, m, 1653 oif, offset, tablearg, ip); 1654 match = 1; 1655 break; 1656 1657 case O_PROB: 1658 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); 1659 break; 1660 1661 case O_VERREVPATH: 1662 /* Outgoing packets automatically pass/match */ 1663 match = ((oif != NULL) || 1664 (m->m_pkthdr.rcvif == NULL) || 1665 ( 1666#ifdef INET6 1667 is_ipv6 ? 1668 verify_path6(&(args->f_id.src_ip6), 1669 m->m_pkthdr.rcvif) : 1670#endif 1671 verify_path(src_ip, m->m_pkthdr.rcvif, 1672 args->f_id.fib))); 1673 break; 1674 1675 case O_VERSRCREACH: 1676 /* Outgoing packets automatically pass/match */ 1677 match = (hlen > 0 && ((oif != NULL) || 1678#ifdef INET6 1679 is_ipv6 ? 1680 verify_path6(&(args->f_id.src_ip6), 1681 NULL) : 1682#endif 1683 verify_path(src_ip, NULL, args->f_id.fib))); 1684 break; 1685 1686 case O_ANTISPOOF: 1687 /* Outgoing packets automatically pass/match */ 1688 if (oif == NULL && hlen > 0 && 1689 ( (is_ipv4 && in_localaddr(src_ip)) 1690#ifdef INET6 1691 || (is_ipv6 && 1692 in6_localaddr(&(args->f_id.src_ip6))) 1693#endif 1694 )) 1695 match = 1696#ifdef INET6 1697 is_ipv6 ? verify_path6( 1698 &(args->f_id.src_ip6), 1699 m->m_pkthdr.rcvif) : 1700#endif 1701 verify_path(src_ip, 1702 m->m_pkthdr.rcvif, 1703 args->f_id.fib); 1704 else 1705 match = 1; 1706 break; 1707 1708 case O_IPSEC: 1709#ifdef IPSEC 1710 match = (m_tag_find(m, 1711 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); 1712#endif 1713 /* otherwise no match */ 1714 break; 1715 1716#ifdef INET6 1717 case O_IP6_SRC: 1718 match = is_ipv6 && 1719 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, 1720 &((ipfw_insn_ip6 *)cmd)->addr6); 1721 break; 1722 1723 case O_IP6_DST: 1724 match = is_ipv6 && 1725 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, 1726 &((ipfw_insn_ip6 *)cmd)->addr6); 1727 break; 1728 case O_IP6_SRC_MASK: 1729 case O_IP6_DST_MASK: 1730 if (is_ipv6) { 1731 int i = cmdlen - 1; 1732 struct in6_addr p; 1733 struct in6_addr *d = 1734 &((ipfw_insn_ip6 *)cmd)->addr6; 1735 1736 for (; !match && i > 0; d += 2, 1737 i -= F_INSN_SIZE(struct in6_addr) 1738 * 2) { 1739 p = (cmd->opcode == 1740 O_IP6_SRC_MASK) ? 1741 args->f_id.src_ip6: 1742 args->f_id.dst_ip6; 1743 APPLY_MASK(&p, &d[1]); 1744 match = 1745 IN6_ARE_ADDR_EQUAL(&d[0], 1746 &p); 1747 } 1748 } 1749 break; 1750 1751 case O_FLOW6ID: 1752 match = is_ipv6 && 1753 flow6id_match(args->f_id.flow_id6, 1754 (ipfw_insn_u32 *) cmd); 1755 break; 1756 1757 case O_EXT_HDR: 1758 match = is_ipv6 && 1759 (ext_hd & ((ipfw_insn *) cmd)->arg1); 1760 break; 1761 1762 case O_IP6: 1763 match = is_ipv6; 1764 break; 1765#endif 1766 1767 case O_IP4: 1768 match = is_ipv4; 1769 break; 1770 1771 case O_TAG: { 1772 struct m_tag *mtag; 1773 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1774 tablearg : cmd->arg1; 1775 1776 /* Packet is already tagged with this tag? */ 1777 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL); 1778 1779 /* We have `untag' action when F_NOT flag is 1780 * present. And we must remove this mtag from 1781 * mbuf and reset `match' to zero (`match' will 1782 * be inversed later). 1783 * Otherwise we should allocate new mtag and 1784 * push it into mbuf. 1785 */ 1786 if (cmd->len & F_NOT) { /* `untag' action */ 1787 if (mtag != NULL) 1788 m_tag_delete(m, mtag); 1789 match = 0; 1790 } else if (mtag == NULL) { 1791 if ((mtag = m_tag_alloc(MTAG_IPFW, 1792 tag, 0, M_NOWAIT)) != NULL) 1793 m_tag_prepend(m, mtag); 1794 match = 1; 1795 } 1796 break; 1797 } 1798 1799 case O_FIB: /* try match the specified fib */ 1800 if (args->f_id.fib == cmd->arg1) 1801 match = 1; 1802 break; 1803 1804 case O_TAGGED: { 1805 struct m_tag *mtag; 1806 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1807 tablearg : cmd->arg1; 1808 1809 if (cmdlen == 1) { 1810 match = m_tag_locate(m, MTAG_IPFW, 1811 tag, NULL) != NULL; 1812 break; 1813 } 1814 1815 /* we have ranges */ 1816 for (mtag = m_tag_first(m); 1817 mtag != NULL && !match; 1818 mtag = m_tag_next(m, mtag)) { 1819 uint16_t *p; 1820 int i; 1821 1822 if (mtag->m_tag_cookie != MTAG_IPFW) 1823 continue; 1824 1825 p = ((ipfw_insn_u16 *)cmd)->ports; 1826 i = cmdlen - 1; 1827 for(; !match && i > 0; i--, p += 2) 1828 match = 1829 mtag->m_tag_id >= p[0] && 1830 mtag->m_tag_id <= p[1]; 1831 } 1832 break; 1833 } 1834 1835 /* 1836 * The second set of opcodes represents 'actions', 1837 * i.e. the terminal part of a rule once the packet 1838 * matches all previous patterns. 1839 * Typically there is only one action for each rule, 1840 * and the opcode is stored at the end of the rule 1841 * (but there are exceptions -- see below). 1842 * 1843 * In general, here we set retval and terminate the 1844 * outer loop (would be a 'break 3' in some language, 1845 * but we need to set l=0, done=1) 1846 * 1847 * Exceptions: 1848 * O_COUNT and O_SKIPTO actions: 1849 * instead of terminating, we jump to the next rule 1850 * (setting l=0), or to the SKIPTO target (setting 1851 * f/f_len, cmd and l as needed), respectively. 1852 * 1853 * O_TAG, O_LOG and O_ALTQ action parameters: 1854 * perform some action and set match = 1; 1855 * 1856 * O_LIMIT and O_KEEP_STATE: these opcodes are 1857 * not real 'actions', and are stored right 1858 * before the 'action' part of the rule. 1859 * These opcodes try to install an entry in the 1860 * state tables; if successful, we continue with 1861 * the next opcode (match=1; break;), otherwise 1862 * the packet must be dropped (set retval, 1863 * break loops with l=0, done=1) 1864 * 1865 * O_PROBE_STATE and O_CHECK_STATE: these opcodes 1866 * cause a lookup of the state table, and a jump 1867 * to the 'action' part of the parent rule 1868 * if an entry is found, or 1869 * (CHECK_STATE only) a jump to the next rule if 1870 * the entry is not found. 1871 * The result of the lookup is cached so that 1872 * further instances of these opcodes become NOPs. 1873 * The jump to the next rule is done by setting 1874 * l=0, cmdlen=0. 1875 */ 1876 case O_LIMIT: 1877 case O_KEEP_STATE: 1878 if (ipfw_install_state(f, 1879 (ipfw_insn_limit *)cmd, args, tablearg)) { 1880 /* error or limit violation */ 1881 retval = IP_FW_DENY; 1882 l = 0; /* exit inner loop */ 1883 done = 1; /* exit outer loop */ 1884 } 1885 match = 1; 1886 break; 1887 1888 case O_PROBE_STATE: 1889 case O_CHECK_STATE: 1890 /* 1891 * dynamic rules are checked at the first 1892 * keep-state or check-state occurrence, 1893 * with the result being stored in dyn_dir. 1894 * The compiler introduces a PROBE_STATE 1895 * instruction for us when we have a 1896 * KEEP_STATE (because PROBE_STATE needs 1897 * to be run first). 1898 */ 1899 if (dyn_dir == MATCH_UNKNOWN && 1900 (q = ipfw_lookup_dyn_rule(&args->f_id, 1901 &dyn_dir, proto == IPPROTO_TCP ? 1902 TCP(ulp) : NULL)) 1903 != NULL) { 1904 /* 1905 * Found dynamic entry, update stats 1906 * and jump to the 'action' part of 1907 * the parent rule by setting 1908 * f, cmd, l and clearing cmdlen. 1909 */ 1910 q->pcnt++; 1911 q->bcnt += pktlen; 1912 /* XXX we would like to have f_pos 1913 * readily accessible in the dynamic 1914 * rule, instead of having to 1915 * lookup q->rule. 1916 */ 1917 f = q->rule; 1918 f_pos = ipfw_find_rule(chain, 1919 f->rulenum, f->id); 1920 cmd = ACTION_PTR(f); 1921 l = f->cmd_len - f->act_ofs; 1922 ipfw_dyn_unlock(); 1923 cmdlen = 0; 1924 match = 1; 1925 break; 1926 } 1927 /* 1928 * Dynamic entry not found. If CHECK_STATE, 1929 * skip to next rule, if PROBE_STATE just 1930 * ignore and continue with next opcode. 1931 */ 1932 if (cmd->opcode == O_CHECK_STATE) 1933 l = 0; /* exit inner loop */ 1934 match = 1; 1935 break; 1936 1937 case O_ACCEPT: 1938 retval = 0; /* accept */ 1939 l = 0; /* exit inner loop */ 1940 done = 1; /* exit outer loop */ 1941 break; 1942 1943 case O_PIPE: 1944 case O_QUEUE: 1945 set_match(args, f_pos, chain); 1946 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ? 1947 tablearg : cmd->arg1; 1948 if (cmd->opcode == O_PIPE) 1949 args->rule.info |= IPFW_IS_PIPE; 1950 if (V_fw_one_pass) 1951 args->rule.info |= IPFW_ONEPASS; 1952 retval = IP_FW_DUMMYNET; 1953 l = 0; /* exit inner loop */ 1954 done = 1; /* exit outer loop */ 1955 break; 1956 1957 case O_DIVERT: 1958 case O_TEE: 1959 if (args->eh) /* not on layer 2 */ 1960 break; 1961 /* otherwise this is terminal */ 1962 l = 0; /* exit inner loop */ 1963 done = 1; /* exit outer loop */ 1964 retval = (cmd->opcode == O_DIVERT) ? 1965 IP_FW_DIVERT : IP_FW_TEE; 1966 set_match(args, f_pos, chain); 1967 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ? 1968 tablearg : cmd->arg1; 1969 break; 1970 1971 case O_COUNT: 1972 f->pcnt++; /* update stats */ 1973 f->bcnt += pktlen; 1974 f->timestamp = time_uptime; 1975 l = 0; /* exit inner loop */ 1976 break; 1977 1978 case O_SKIPTO: 1979 f->pcnt++; /* update stats */ 1980 f->bcnt += pktlen; 1981 f->timestamp = time_uptime; 1982 /* If possible use cached f_pos (in f->next_rule), 1983 * whose version is written in f->next_rule 1984 * (horrible hacks to avoid changing the ABI). 1985 */ 1986 if (cmd->arg1 != IP_FW_TABLEARG && 1987 (uintptr_t)f->x_next == chain->id) { 1988 f_pos = (uintptr_t)f->next_rule; 1989 } else { 1990 int i = (cmd->arg1 == IP_FW_TABLEARG) ? 1991 tablearg : cmd->arg1; 1992 /* make sure we do not jump backward */ 1993 if (i <= f->rulenum) 1994 i = f->rulenum + 1; 1995 f_pos = ipfw_find_rule(chain, i, 0); 1996 /* update the cache */ 1997 if (cmd->arg1 != IP_FW_TABLEARG) { 1998 f->next_rule = 1999 (void *)(uintptr_t)f_pos; 2000 f->x_next = 2001 (void *)(uintptr_t)chain->id; 2002 } 2003 } 2004 /* 2005 * Skip disabled rules, and re-enter 2006 * the inner loop with the correct 2007 * f_pos, f, l and cmd. 2008 * Also clear cmdlen and skip_or 2009 */ 2010 for (; f_pos < chain->n_rules - 1 && 2011 (V_set_disable & 2012 (1 << chain->map[f_pos]->set)); 2013 f_pos++) 2014 ; 2015 /* Re-enter the inner loop at the skipto rule. */ 2016 f = chain->map[f_pos]; 2017 l = f->cmd_len; 2018 cmd = f->cmd; 2019 match = 1; 2020 cmdlen = 0; 2021 skip_or = 0; 2022 continue; 2023 break; /* not reached */ 2024 2025 case O_REJECT: 2026 /* 2027 * Drop the packet and send a reject notice 2028 * if the packet is not ICMP (or is an ICMP 2029 * query), and it is not multicast/broadcast. 2030 */ 2031 if (hlen > 0 && is_ipv4 && offset == 0 && 2032 (proto != IPPROTO_ICMP || 2033 is_icmp_query(ICMP(ulp))) && 2034 !(m->m_flags & (M_BCAST|M_MCAST)) && 2035 !IN_MULTICAST(ntohl(dst_ip.s_addr))) { 2036 send_reject(args, cmd->arg1, iplen, ip); 2037 m = args->m; 2038 } 2039 /* FALLTHROUGH */ 2040#ifdef INET6 2041 case O_UNREACH6: 2042 if (hlen > 0 && is_ipv6 && 2043 ((offset & IP6F_OFF_MASK) == 0) && 2044 (proto != IPPROTO_ICMPV6 || 2045 (is_icmp6_query(icmp6_type) == 1)) && 2046 !(m->m_flags & (M_BCAST|M_MCAST)) && 2047 !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) { 2048 send_reject6( 2049 args, cmd->arg1, hlen, 2050 (struct ip6_hdr *)ip); 2051 m = args->m; 2052 } 2053 /* FALLTHROUGH */ 2054#endif 2055 case O_DENY: 2056 retval = IP_FW_DENY; 2057 l = 0; /* exit inner loop */ 2058 done = 1; /* exit outer loop */ 2059 break; 2060 2061 case O_FORWARD_IP: 2062 if (args->eh) /* not valid on layer2 pkts */ 2063 break; 2064 if (!q || dyn_dir == MATCH_FORWARD) { 2065 struct sockaddr_in *sa; 2066 sa = &(((ipfw_insn_sa *)cmd)->sa); 2067 if (sa->sin_addr.s_addr == INADDR_ANY) { 2068 bcopy(sa, &args->hopstore, 2069 sizeof(*sa)); 2070 args->hopstore.sin_addr.s_addr = 2071 htonl(tablearg); 2072 args->next_hop = &args->hopstore; 2073 } else { 2074 args->next_hop = sa; 2075 } 2076 } 2077 retval = IP_FW_PASS; 2078 l = 0; /* exit inner loop */ 2079 done = 1; /* exit outer loop */ 2080 break; 2081 2082 case O_NETGRAPH: 2083 case O_NGTEE: 2084 set_match(args, f_pos, chain); 2085 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ? 2086 tablearg : cmd->arg1; 2087 retval = (cmd->opcode == O_NETGRAPH) ? 2088 IP_FW_NETGRAPH : IP_FW_NGTEE; 2089 l = 0; /* exit inner loop */ 2090 done = 1; /* exit outer loop */ 2091 break; 2092 2093 case O_SETFIB: 2094 f->pcnt++; /* update stats */ 2095 f->bcnt += pktlen; 2096 f->timestamp = time_uptime; 2097 M_SETFIB(m, cmd->arg1); 2098 args->f_id.fib = cmd->arg1; 2099 l = 0; /* exit inner loop */ 2100 break; 2101 2102 case O_NAT: 2103 if (!IPFW_NAT_LOADED) { 2104 retval = IP_FW_DENY; 2105 } else { 2106 struct cfg_nat *t; 2107 int nat_id; 2108 2109 set_match(args, f_pos, chain); 2110 t = ((ipfw_insn_nat *)cmd)->nat; 2111 if (t == NULL) { 2112 nat_id = (cmd->arg1 == IP_FW_TABLEARG) ? 2113 tablearg : cmd->arg1; 2114 t = (*lookup_nat_ptr)(&chain->nat, nat_id); 2115 2116 if (t == NULL) { 2117 retval = IP_FW_DENY; 2118 l = 0; /* exit inner loop */ 2119 done = 1; /* exit outer loop */ 2120 break; 2121 } 2122 if (cmd->arg1 != IP_FW_TABLEARG) 2123 ((ipfw_insn_nat *)cmd)->nat = t; 2124 } 2125 retval = ipfw_nat_ptr(args, t, m); 2126 } 2127 l = 0; /* exit inner loop */ 2128 done = 1; /* exit outer loop */ 2129 break; 2130 2131 case O_REASS: { 2132 int ip_off; 2133 2134 f->pcnt++; 2135 f->bcnt += pktlen; 2136 l = 0; /* in any case exit inner loop */ 2137 ip_off = ntohs(ip->ip_off); 2138 2139 /* if not fragmented, go to next rule */ 2140 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0) 2141 break; 2142 /* 2143 * ip_reass() expects len & off in host 2144 * byte order. 2145 */ 2146 SET_HOST_IPLEN(ip); 2147 2148 args->m = m = ip_reass(m); 2149 2150 /* 2151 * do IP header checksum fixup. 2152 */ 2153 if (m == NULL) { /* fragment got swallowed */ 2154 retval = IP_FW_DENY; 2155 } else { /* good, packet complete */ 2156 int hlen; 2157 2158 ip = mtod(m, struct ip *); 2159 hlen = ip->ip_hl << 2; 2160 SET_NET_IPLEN(ip); 2161 ip->ip_sum = 0; 2162 if (hlen == sizeof(struct ip)) 2163 ip->ip_sum = in_cksum_hdr(ip); 2164 else 2165 ip->ip_sum = in_cksum(m, hlen); 2166 retval = IP_FW_REASS; 2167 set_match(args, f_pos, chain); 2168 } 2169 done = 1; /* exit outer loop */ 2170 break; 2171 } 2172 2173 default: 2174 panic("-- unknown opcode %d\n", cmd->opcode); 2175 } /* end of switch() on opcodes */ 2176 /* 2177 * if we get here with l=0, then match is irrelevant. 2178 */ 2179 2180 if (cmd->len & F_NOT) 2181 match = !match; 2182 2183 if (match) { 2184 if (cmd->len & F_OR) 2185 skip_or = 1; 2186 } else { 2187 if (!(cmd->len & F_OR)) /* not an OR block, */ 2188 break; /* try next rule */ 2189 } 2190 2191 } /* end of inner loop, scan opcodes */ 2192 2193 if (done) 2194 break; 2195 2196/* next_rule:; */ /* try next rule */ 2197 2198 } /* end of outer for, scan rules */ 2199 2200 if (done) { 2201 struct ip_fw *rule = chain->map[f_pos]; 2202 /* Update statistics */ 2203 rule->pcnt++; 2204 rule->bcnt += pktlen; 2205 rule->timestamp = time_uptime; 2206 } else { 2207 retval = IP_FW_DENY; 2208 printf("ipfw: ouch!, skip past end of rules, denying packet\n"); 2209 } 2210 IPFW_RUNLOCK(chain); 2211#ifdef __FreeBSD__ 2212 if (ucred_cache != NULL) 2213 crfree(ucred_cache); 2214#endif 2215 return (retval); 2216 2217pullup_failed: 2218 if (V_fw_verbose) 2219 printf("ipfw: pullup failed\n"); 2220 return (IP_FW_DENY); 2221} 2222 2223/* 2224 * Module and VNET glue 2225 */ 2226 2227/* 2228 * Stuff that must be initialised only on boot or module load 2229 */ 2230static int 2231ipfw_init(void) 2232{ 2233 int error = 0; 2234 2235 ipfw_dyn_attach(); 2236 /* 2237 * Only print out this stuff the first time around, 2238 * when called from the sysinit code. 2239 */ 2240 printf("ipfw2 " 2241#ifdef INET6 2242 "(+ipv6) " 2243#endif 2244 "initialized, divert %s, nat %s, " 2245 "rule-based forwarding " 2246#ifdef IPFIREWALL_FORWARD 2247 "enabled, " 2248#else 2249 "disabled, " 2250#endif 2251 "default to %s, logging ", 2252#ifdef IPDIVERT 2253 "enabled", 2254#else 2255 "loadable", 2256#endif 2257#ifdef IPFIREWALL_NAT 2258 "enabled", 2259#else 2260 "loadable", 2261#endif 2262 default_to_accept ? "accept" : "deny"); 2263 2264 /* 2265 * Note: V_xxx variables can be accessed here but the vnet specific 2266 * initializer may not have been called yet for the VIMAGE case. 2267 * Tuneables will have been processed. We will print out values for 2268 * the default vnet. 2269 * XXX This should all be rationalized AFTER 8.0 2270 */ 2271 if (V_fw_verbose == 0) 2272 printf("disabled\n"); 2273 else if (V_verbose_limit == 0) 2274 printf("unlimited\n"); 2275 else 2276 printf("limited to %d packets/entry by default\n", 2277 V_verbose_limit); 2278 2279 ipfw_log_bpf(1); /* init */ 2280 return (error); 2281} 2282 2283/* 2284 * Called for the removal of the last instance only on module unload. 2285 */ 2286static void 2287ipfw_destroy(void) 2288{ 2289 2290 ipfw_log_bpf(0); /* uninit */ 2291 ipfw_dyn_detach(); 2292 printf("IP firewall unloaded\n"); 2293} 2294 2295/* 2296 * Stuff that must be initialized for every instance 2297 * (including the first of course). 2298 */ 2299static int 2300vnet_ipfw_init(const void *unused) 2301{ 2302 int error; 2303 struct ip_fw *rule = NULL; 2304 struct ip_fw_chain *chain; 2305 2306 chain = &V_layer3_chain; 2307 2308 /* First set up some values that are compile time options */ 2309 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ 2310 V_fw_deny_unknown_exthdrs = 1; 2311#ifdef IPFIREWALL_VERBOSE 2312 V_fw_verbose = 1; 2313#endif 2314#ifdef IPFIREWALL_VERBOSE_LIMIT 2315 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT; 2316#endif 2317#ifdef IPFIREWALL_NAT 2318 LIST_INIT(&chain->nat); 2319#endif 2320 2321 /* insert the default rule and create the initial map */ 2322 chain->n_rules = 1; 2323 chain->static_len = sizeof(struct ip_fw); 2324 chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_NOWAIT | M_ZERO); 2325 if (chain->map) 2326 rule = malloc(chain->static_len, M_IPFW, M_NOWAIT | M_ZERO); 2327 if (rule == NULL) { 2328 if (chain->map) 2329 free(chain->map, M_IPFW); 2330 printf("ipfw2: ENOSPC initializing default rule " 2331 "(support disabled)\n"); 2332 return (ENOSPC); 2333 } 2334 error = ipfw_init_tables(chain); 2335 if (error) { 2336 panic("init_tables"); /* XXX Marko fix this ! */ 2337 } 2338 2339 /* fill and insert the default rule */ 2340 rule->act_ofs = 0; 2341 rule->rulenum = IPFW_DEFAULT_RULE; 2342 rule->cmd_len = 1; 2343 rule->set = RESVD_SET; 2344 rule->cmd[0].len = 1; 2345 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY; 2346 chain->rules = chain->default_rule = chain->map[0] = rule; 2347 chain->id = rule->id = 1; 2348 2349 IPFW_LOCK_INIT(chain); 2350 ipfw_dyn_init(); 2351 2352 /* First set up some values that are compile time options */ 2353 V_ipfw_vnet_ready = 1; /* Open for business */ 2354 2355 /* 2356 * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr) 2357 * and pfil hooks for ipv4 and ipv6. Even if the latter two fail 2358 * we still keep the module alive because the sockopt and 2359 * layer2 paths are still useful. 2360 * ipfw[6]_hook return 0 on success, ENOENT on failure, 2361 * so we can ignore the exact return value and just set a flag. 2362 * 2363 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so 2364 * changes in the underlying (per-vnet) variables trigger 2365 * immediate hook()/unhook() calls. 2366 * In layer2 we have the same behaviour, except that V_ether_ipfw 2367 * is checked on each packet because there are no pfil hooks. 2368 */ 2369 V_ip_fw_ctl_ptr = ipfw_ctl; 2370 V_ip_fw_chk_ptr = ipfw_chk; 2371 error = ipfw_attach_hooks(1); 2372 return (error); 2373} 2374 2375/* 2376 * Called for the removal of each instance. 2377 */ 2378static int 2379vnet_ipfw_uninit(const void *unused) 2380{ 2381 struct ip_fw *reap, *rule; 2382 struct ip_fw_chain *chain = &V_layer3_chain; 2383 int i; 2384 2385 V_ipfw_vnet_ready = 0; /* tell new callers to go away */ 2386 /* 2387 * disconnect from ipv4, ipv6, layer2 and sockopt. 2388 * Then grab, release and grab again the WLOCK so we make 2389 * sure the update is propagated and nobody will be in. 2390 */ 2391 (void)ipfw_attach_hooks(0 /* detach */); 2392 V_ip_fw_chk_ptr = NULL; 2393 V_ip_fw_ctl_ptr = NULL; 2394 IPFW_UH_WLOCK(chain); 2395 IPFW_UH_WUNLOCK(chain); 2396 IPFW_UH_WLOCK(chain); 2397 2398 IPFW_WLOCK(chain); 2399 IPFW_WUNLOCK(chain); 2400 IPFW_WLOCK(chain); 2401 2402 ipfw_dyn_uninit(0); /* run the callout_drain */ 2403 ipfw_destroy_tables(chain); 2404 reap = NULL; 2405 for (i = 0; i < chain->n_rules; i++) { 2406 rule = chain->map[i]; 2407 rule->x_next = reap; 2408 reap = rule; 2409 } 2410 if (chain->map) 2411 free(chain->map, M_IPFW); 2412 IPFW_WUNLOCK(chain); 2413 IPFW_UH_WUNLOCK(chain); 2414 if (reap != NULL) 2415 ipfw_reap_rules(reap); 2416 IPFW_LOCK_DESTROY(chain); 2417 ipfw_dyn_uninit(1); /* free the remaining parts */ 2418 return 0; 2419} 2420 2421/* 2422 * Module event handler. 2423 * In general we have the choice of handling most of these events by the 2424 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to 2425 * use the SYSINIT handlers as they are more capable of expressing the 2426 * flow of control during module and vnet operations, so this is just 2427 * a skeleton. Note there is no SYSINIT equivalent of the module 2428 * SHUTDOWN handler, but we don't have anything to do in that case anyhow. 2429 */ 2430static int 2431ipfw_modevent(module_t mod, int type, void *unused) 2432{ 2433 int err = 0; 2434 2435 switch (type) { 2436 case MOD_LOAD: 2437 /* Called once at module load or 2438 * system boot if compiled in. */ 2439 break; 2440 case MOD_QUIESCE: 2441 /* Called before unload. May veto unloading. */ 2442 break; 2443 case MOD_UNLOAD: 2444 /* Called during unload. */ 2445 break; 2446 case MOD_SHUTDOWN: 2447 /* Called during system shutdown. */ 2448 break; 2449 default: 2450 err = EOPNOTSUPP; 2451 break; 2452 } 2453 return err; 2454} 2455 2456static moduledata_t ipfwmod = { 2457 "ipfw", 2458 ipfw_modevent, 2459 0 2460}; 2461 2462/* Define startup order. */ 2463#define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_IFATTACHDOMAIN 2464#define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */ 2465#define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */ 2466#define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */ 2467 2468DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER); 2469MODULE_VERSION(ipfw, 2); 2470/* should declare some dependencies here */ 2471 2472/* 2473 * Starting up. Done in order after ipfwmod() has been called. 2474 * VNET_SYSINIT is also called for each existing vnet and each new vnet. 2475 */ 2476SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2477 ipfw_init, NULL); 2478VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2479 vnet_ipfw_init, NULL); 2480 2481/* 2482 * Closing up shop. These are done in REVERSE ORDER, but still 2483 * after ipfwmod() has been called. Not called on reboot. 2484 * VNET_SYSUNINIT is also called for each exiting vnet as it exits. 2485 * or when the module is unloaded. 2486 */ 2487SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2488 ipfw_destroy, NULL); 2489VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2490 vnet_ipfw_uninit, NULL); 2491/* end of file */ 2492