ip_fw2.c revision 201150
1252995Sdteske/*- 2252995Sdteske * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 3252995Sdteske * 4252995Sdteske * Redistribution and use in source and binary forms, with or without 5252995Sdteske * modification, are permitted provided that the following conditions 6252995Sdteske * are met: 7252995Sdteske * 1. Redistributions of source code must retain the above copyright 8252995Sdteske * notice, this list of conditions and the following disclaimer. 9252995Sdteske * 2. Redistributions in binary form must reproduce the above copyright 10252995Sdteske * notice, this list of conditions and the following disclaimer in the 11252995Sdteske * documentation and/or other materials provided with the distribution. 12252995Sdteske * 13252995Sdteske * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14252995Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15252995Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16252995Sdteske * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17252995Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18252995Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19252995Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20252995Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21252995Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22252995Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23252995Sdteske * SUCH DAMAGE. 24252995Sdteske */ 25252995Sdteske 26252995Sdteske#include <sys/cdefs.h> 27252995Sdteske__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw2.c 201150 2009-12-29 00:02:57Z luigi $"); 28252995Sdteske 29252995Sdteske/* 30252995Sdteske * The FreeBSD IP packet firewall, main file 31252995Sdteske */ 32252995Sdteske 33252995Sdteske#if !defined(KLD_MODULE) 34252995Sdteske#include "opt_ipfw.h" 35252995Sdteske#include "opt_ipdivert.h" 36252995Sdteske#include "opt_ipdn.h" 37252995Sdteske#include "opt_inet.h" 38252995Sdteske#ifndef INET 39252995Sdteske#error IPFIREWALL requires INET. 40252995Sdteske#endif /* INET */ 41252995Sdteske#endif 42252995Sdteske#include "opt_inet6.h" 43252995Sdteske#include "opt_ipsec.h" 44252995Sdteske 45252995Sdteske#include <sys/param.h> 46252995Sdteske#include <sys/systm.h> 47252995Sdteske#include <sys/condvar.h> 48252995Sdteske#include <sys/eventhandler.h> 49252995Sdteske#include <sys/malloc.h> 50252995Sdteske#include <sys/mbuf.h> 51252995Sdteske#include <sys/kernel.h> 52252995Sdteske#include <sys/lock.h> 53252995Sdteske#include <sys/jail.h> 54252995Sdteske#include <sys/module.h> 55252995Sdteske#include <sys/priv.h> 56252995Sdteske#include <sys/proc.h> 57252995Sdteske#include <sys/rwlock.h> 58252995Sdteske#include <sys/socket.h> 59252995Sdteske#include <sys/socketvar.h> 60252995Sdteske#include <sys/sysctl.h> 61252995Sdteske#include <sys/syslog.h> 62252995Sdteske#include <sys/ucred.h> 63252995Sdteske#include <net/ethernet.h> /* for ETHERTYPE_IP */ 64252995Sdteske#include <net/if.h> 65252995Sdteske#include <net/route.h> 66252995Sdteske#include <net/pf_mtag.h> 67252995Sdteske#include <net/vnet.h> 68252995Sdteske 69252995Sdteske#include <netinet/in.h> 70252995Sdteske#include <netinet/in_var.h> 71252995Sdteske#include <netinet/in_pcb.h> 72252995Sdteske#include <netinet/ip.h> 73252995Sdteske#include <netinet/ip_var.h> 74252995Sdteske#include <netinet/ip_icmp.h> 75252995Sdteske#include <netinet/ip_fw.h> 76252995Sdteske#include <netinet/ipfw/ip_fw_private.h> 77252995Sdteske#include <netinet/ip_divert.h> 78252995Sdteske#include <netinet/ip_carp.h> 79252995Sdteske#include <netinet/pim.h> 80252995Sdteske#include <netinet/tcp_var.h> 81252995Sdteske#include <netinet/udp.h> 82252995Sdteske#include <netinet/udp_var.h> 83252995Sdteske#include <netinet/sctp.h> 84252995Sdteske 85252995Sdteske#include <netinet/ip6.h> 86252995Sdteske#include <netinet/icmp6.h> 87252995Sdteske#ifdef INET6 88252995Sdteske#include <netinet6/scope6_var.h> 89252995Sdteske#include <netinet6/ip6_var.h> 90252995Sdteske#endif 91252995Sdteske 92252995Sdteske#include <machine/in_cksum.h> /* XXX for in_cksum */ 93252995Sdteske 94252995Sdteske#ifdef MAC 95252995Sdteske#include <security/mac/mac_framework.h> 96252995Sdteske#endif 97252995Sdteske 98252995Sdteske/* 99252995Sdteske * static variables followed by global ones. 100252995Sdteske * All ipfw global variables are here. 101252995Sdteske */ 102252995Sdteske 103252995Sdteske/* ipfw_vnet_ready controls when we are open for business */ 104252995Sdteskestatic VNET_DEFINE(int, ipfw_vnet_ready) = 0; 105252995Sdteske#define V_ipfw_vnet_ready VNET(ipfw_vnet_ready) 106252995Sdteske 107252995Sdteskestatic VNET_DEFINE(int, fw_deny_unknown_exthdrs); 108252995Sdteske#define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs) 109252995Sdteske 110252995Sdteske#ifdef IPFIREWALL_DEFAULT_TO_ACCEPT 111252995Sdteskestatic int default_to_accept = 1; 112252995Sdteske#else 113252995Sdteskestatic int default_to_accept; 114252995Sdteske#endif 115252995Sdteske 116252995SdteskeVNET_DEFINE(int, autoinc_step); 117252995Sdteske 118252995Sdteske/* 119252995Sdteske * Each rule belongs to one of 32 different sets (0..31). 120252995Sdteske * The variable set_disable contains one bit per set. 121252995Sdteske * If the bit is set, all rules in the corresponding set 122252995Sdteske * are disabled. Set RESVD_SET(31) is reserved for the default rule 123252995Sdteske * and rules that are not deleted by the flush command, 124252995Sdteske * and CANNOT be disabled. 125252995Sdteske * Rules in set RESVD_SET can only be deleted individually. 126252995Sdteske */ 127252995SdteskeVNET_DEFINE(u_int32_t, set_disable); 128252995Sdteske#define V_set_disable VNET(set_disable) 129252995Sdteske 130252995SdteskeVNET_DEFINE(int, fw_verbose); 131252995Sdteske/* counter for ipfw_log(NULL...) */ 132252995SdteskeVNET_DEFINE(u_int64_t, norule_counter); 133252995SdteskeVNET_DEFINE(int, verbose_limit); 134252995Sdteske 135252995Sdteske/* layer3_chain contains the list of rules for layer 3 */ 136252995SdteskeVNET_DEFINE(struct ip_fw_chain, layer3_chain); 137252995Sdteske 138252995Sdteskeipfw_nat_t *ipfw_nat_ptr = NULL; 139252995Sdteskestruct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int); 140252995Sdteskeipfw_nat_cfg_t *ipfw_nat_cfg_ptr; 141252995Sdteskeipfw_nat_cfg_t *ipfw_nat_del_ptr; 142252995Sdteskeipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr; 143252995Sdteskeipfw_nat_cfg_t *ipfw_nat_get_log_ptr; 144252995Sdteske 145252995Sdteske#ifdef SYSCTL_NODE 146252995SdteskeSYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 147263791SdteskeSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass, 148263791Sdteske CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0, 149252995Sdteske "Only do a single pass through ipfw when using dummynet(4)"); 150263791SdteskeSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, 151252995Sdteske CTLFLAG_RW, &VNET_NAME(autoinc_step), 0, 152252995Sdteske "Rule number auto-increment step"); 153263791SdteskeSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose, 154263791Sdteske CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0, 155252995Sdteske "Log matches to ipfw rules"); 156252995SdteskeSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, 157252995Sdteske CTLFLAG_RW, &VNET_NAME(verbose_limit), 0, 158252995Sdteske "Set upper limit of matches of ipfw rules logged"); 159252995SdteskeSYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD, 160252995Sdteske NULL, IPFW_DEFAULT_RULE, 161252995Sdteske "The default/max possible rule number."); 162252995SdteskeSYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, tables_max, CTLFLAG_RD, 163252995Sdteske NULL, IPFW_TABLES_MAX, 164252995Sdteske "The maximum number of tables."); 165252995SdteskeSYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN, 166252995Sdteske &default_to_accept, 0, 167263791Sdteske "Make the default rule accept all packets."); 168263791SdteskeTUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept); 169252995SdteskeSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count, 170252995Sdteske CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0, 171263791Sdteske "Number of static rules"); 172252995Sdteske 173252995Sdteske#ifdef INET6 174252995SdteskeSYSCTL_DECL(_net_inet6_ip6); 175252995SdteskeSYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 176252995SdteskeSYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs, 177252995Sdteske CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0, 178252995Sdteske "Deny packets with unknown IPv6 Extension Headers"); 179252995Sdteske#endif /* INET6 */ 180252995Sdteske 181252995Sdteske#endif /* SYSCTL_NODE */ 182252995Sdteske 183252995Sdteske 184252995Sdteske/* 185252995Sdteske * Some macros used in the various matching options. 186252995Sdteske * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 187252995Sdteske * Other macros just cast void * into the appropriate type 188252995Sdteske */ 189252995Sdteske#define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 190252995Sdteske#define TCP(p) ((struct tcphdr *)(p)) 191252995Sdteske#define SCTP(p) ((struct sctphdr *)(p)) 192252995Sdteske#define UDP(p) ((struct udphdr *)(p)) 193252995Sdteske#define ICMP(p) ((struct icmphdr *)(p)) 194252995Sdteske#define ICMP6(p) ((struct icmp6_hdr *)(p)) 195252995Sdteske 196252995Sdteskestatic __inline int 197252995Sdteskeicmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd) 198252995Sdteske{ 199252995Sdteske int type = icmp->icmp_type; 200252995Sdteske 201252995Sdteske return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) ); 202252995Sdteske} 203252995Sdteske 204252995Sdteske#define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \ 205252995Sdteske (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) ) 206252995Sdteske 207252995Sdteskestatic int 208252995Sdteskeis_icmp_query(struct icmphdr *icmp) 209252995Sdteske{ 210252995Sdteske int type = icmp->icmp_type; 211252995Sdteske 212263791Sdteske return (type <= ICMP_MAXTYPE && (TT & (1<<type)) ); 213252995Sdteske} 214252995Sdteske#undef TT 215252995Sdteske 216252995Sdteske/* 217252995Sdteske * The following checks use two arrays of 8 or 16 bits to store the 218252995Sdteske * bits that we want set or clear, respectively. They are in the 219252995Sdteske * low and high half of cmd->arg1 or cmd->d[0]. 220 * 221 * We scan options and store the bits we find set. We succeed if 222 * 223 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear 224 * 225 * The code is sometimes optimized not to store additional variables. 226 */ 227 228static int 229flags_match(ipfw_insn *cmd, u_int8_t bits) 230{ 231 u_char want_clear; 232 bits = ~bits; 233 234 if ( ((cmd->arg1 & 0xff) & bits) != 0) 235 return 0; /* some bits we want set were clear */ 236 want_clear = (cmd->arg1 >> 8) & 0xff; 237 if ( (want_clear & bits) != want_clear) 238 return 0; /* some bits we want clear were set */ 239 return 1; 240} 241 242static int 243ipopts_match(struct ip *ip, ipfw_insn *cmd) 244{ 245 int optlen, bits = 0; 246 u_char *cp = (u_char *)(ip + 1); 247 int x = (ip->ip_hl << 2) - sizeof (struct ip); 248 249 for (; x > 0; x -= optlen, cp += optlen) { 250 int opt = cp[IPOPT_OPTVAL]; 251 252 if (opt == IPOPT_EOL) 253 break; 254 if (opt == IPOPT_NOP) 255 optlen = 1; 256 else { 257 optlen = cp[IPOPT_OLEN]; 258 if (optlen <= 0 || optlen > x) 259 return 0; /* invalid or truncated */ 260 } 261 switch (opt) { 262 263 default: 264 break; 265 266 case IPOPT_LSRR: 267 bits |= IP_FW_IPOPT_LSRR; 268 break; 269 270 case IPOPT_SSRR: 271 bits |= IP_FW_IPOPT_SSRR; 272 break; 273 274 case IPOPT_RR: 275 bits |= IP_FW_IPOPT_RR; 276 break; 277 278 case IPOPT_TS: 279 bits |= IP_FW_IPOPT_TS; 280 break; 281 } 282 } 283 return (flags_match(cmd, bits)); 284} 285 286static int 287tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd) 288{ 289 int optlen, bits = 0; 290 u_char *cp = (u_char *)(tcp + 1); 291 int x = (tcp->th_off << 2) - sizeof(struct tcphdr); 292 293 for (; x > 0; x -= optlen, cp += optlen) { 294 int opt = cp[0]; 295 if (opt == TCPOPT_EOL) 296 break; 297 if (opt == TCPOPT_NOP) 298 optlen = 1; 299 else { 300 optlen = cp[1]; 301 if (optlen <= 0) 302 break; 303 } 304 305 switch (opt) { 306 307 default: 308 break; 309 310 case TCPOPT_MAXSEG: 311 bits |= IP_FW_TCPOPT_MSS; 312 break; 313 314 case TCPOPT_WINDOW: 315 bits |= IP_FW_TCPOPT_WINDOW; 316 break; 317 318 case TCPOPT_SACK_PERMITTED: 319 case TCPOPT_SACK: 320 bits |= IP_FW_TCPOPT_SACK; 321 break; 322 323 case TCPOPT_TIMESTAMP: 324 bits |= IP_FW_TCPOPT_TS; 325 break; 326 327 } 328 } 329 return (flags_match(cmd, bits)); 330} 331 332static int 333iface_match(struct ifnet *ifp, ipfw_insn_if *cmd) 334{ 335 if (ifp == NULL) /* no iface with this packet, match fails */ 336 return 0; 337 /* Check by name or by IP address */ 338 if (cmd->name[0] != '\0') { /* match by name */ 339 /* Check name */ 340 if (cmd->p.glob) { 341 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0) 342 return(1); 343 } else { 344 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0) 345 return(1); 346 } 347 } else { 348 struct ifaddr *ia; 349 350 if_addr_rlock(ifp); 351 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 352 if (ia->ifa_addr->sa_family != AF_INET) 353 continue; 354 if (cmd->p.ip.s_addr == ((struct sockaddr_in *) 355 (ia->ifa_addr))->sin_addr.s_addr) { 356 if_addr_runlock(ifp); 357 return(1); /* match */ 358 } 359 } 360 if_addr_runlock(ifp); 361 } 362 return(0); /* no match, fail ... */ 363} 364 365/* 366 * The verify_path function checks if a route to the src exists and 367 * if it is reachable via ifp (when provided). 368 * 369 * The 'verrevpath' option checks that the interface that an IP packet 370 * arrives on is the same interface that traffic destined for the 371 * packet's source address would be routed out of. 372 * The 'versrcreach' option just checks that the source address is 373 * reachable via any route (except default) in the routing table. 374 * These two are a measure to block forged packets. This is also 375 * commonly known as "anti-spoofing" or Unicast Reverse Path 376 * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs 377 * is purposely reminiscent of the Cisco IOS command, 378 * 379 * ip verify unicast reverse-path 380 * ip verify unicast source reachable-via any 381 * 382 * which implements the same functionality. But note that the syntax 383 * is misleading, and the check may be performed on all IP packets 384 * whether unicast, multicast, or broadcast. 385 */ 386static int 387verify_path(struct in_addr src, struct ifnet *ifp, u_int fib) 388{ 389 struct route ro; 390 struct sockaddr_in *dst; 391 392 bzero(&ro, sizeof(ro)); 393 394 dst = (struct sockaddr_in *)&(ro.ro_dst); 395 dst->sin_family = AF_INET; 396 dst->sin_len = sizeof(*dst); 397 dst->sin_addr = src; 398 in_rtalloc_ign(&ro, 0, fib); 399 400 if (ro.ro_rt == NULL) 401 return 0; 402 403 /* 404 * If ifp is provided, check for equality with rtentry. 405 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 406 * in order to pass packets injected back by if_simloop(): 407 * if useloopback == 1 routing entry (via lo0) for our own address 408 * may exist, so we need to handle routing assymetry. 409 */ 410 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 411 RTFREE(ro.ro_rt); 412 return 0; 413 } 414 415 /* if no ifp provided, check if rtentry is not default route */ 416 if (ifp == NULL && 417 satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) { 418 RTFREE(ro.ro_rt); 419 return 0; 420 } 421 422 /* or if this is a blackhole/reject route */ 423 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 424 RTFREE(ro.ro_rt); 425 return 0; 426 } 427 428 /* found valid route */ 429 RTFREE(ro.ro_rt); 430 return 1; 431} 432 433#ifdef INET6 434/* 435 * ipv6 specific rules here... 436 */ 437static __inline int 438icmp6type_match (int type, ipfw_insn_u32 *cmd) 439{ 440 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) ); 441} 442 443static int 444flow6id_match( int curr_flow, ipfw_insn_u32 *cmd ) 445{ 446 int i; 447 for (i=0; i <= cmd->o.arg1; ++i ) 448 if (curr_flow == cmd->d[i] ) 449 return 1; 450 return 0; 451} 452 453/* support for IP6_*_ME opcodes */ 454static int 455search_ip6_addr_net (struct in6_addr * ip6_addr) 456{ 457 struct ifnet *mdc; 458 struct ifaddr *mdc2; 459 struct in6_ifaddr *fdm; 460 struct in6_addr copia; 461 462 TAILQ_FOREACH(mdc, &V_ifnet, if_link) { 463 if_addr_rlock(mdc); 464 TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) { 465 if (mdc2->ifa_addr->sa_family == AF_INET6) { 466 fdm = (struct in6_ifaddr *)mdc2; 467 copia = fdm->ia_addr.sin6_addr; 468 /* need for leaving scope_id in the sock_addr */ 469 in6_clearscope(&copia); 470 if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) { 471 if_addr_runlock(mdc); 472 return 1; 473 } 474 } 475 } 476 if_addr_runlock(mdc); 477 } 478 return 0; 479} 480 481static int 482verify_path6(struct in6_addr *src, struct ifnet *ifp) 483{ 484 struct route_in6 ro; 485 struct sockaddr_in6 *dst; 486 487 bzero(&ro, sizeof(ro)); 488 489 dst = (struct sockaddr_in6 * )&(ro.ro_dst); 490 dst->sin6_family = AF_INET6; 491 dst->sin6_len = sizeof(*dst); 492 dst->sin6_addr = *src; 493 /* XXX MRT 0 for ipv6 at this time */ 494 rtalloc_ign((struct route *)&ro, 0); 495 496 if (ro.ro_rt == NULL) 497 return 0; 498 499 /* 500 * if ifp is provided, check for equality with rtentry 501 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 502 * to support the case of sending packets to an address of our own. 503 * (where the former interface is the first argument of if_simloop() 504 * (=ifp), the latter is lo0) 505 */ 506 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 507 RTFREE(ro.ro_rt); 508 return 0; 509 } 510 511 /* if no ifp provided, check if rtentry is not default route */ 512 if (ifp == NULL && 513 IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) { 514 RTFREE(ro.ro_rt); 515 return 0; 516 } 517 518 /* or if this is a blackhole/reject route */ 519 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 520 RTFREE(ro.ro_rt); 521 return 0; 522 } 523 524 /* found valid route */ 525 RTFREE(ro.ro_rt); 526 return 1; 527 528} 529 530static int 531is_icmp6_query(int icmp6_type) 532{ 533 if ((icmp6_type <= ICMP6_MAXTYPE) && 534 (icmp6_type == ICMP6_ECHO_REQUEST || 535 icmp6_type == ICMP6_MEMBERSHIP_QUERY || 536 icmp6_type == ICMP6_WRUREQUEST || 537 icmp6_type == ICMP6_FQDN_QUERY || 538 icmp6_type == ICMP6_NI_QUERY)) 539 return (1); 540 541 return (0); 542} 543 544static void 545send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6) 546{ 547 struct mbuf *m; 548 549 m = args->m; 550 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) { 551 struct tcphdr *tcp; 552 tcp = (struct tcphdr *)((char *)ip6 + hlen); 553 554 if ((tcp->th_flags & TH_RST) == 0) { 555 struct mbuf *m0; 556 m0 = ipfw_send_pkt(args->m, &(args->f_id), 557 ntohl(tcp->th_seq), ntohl(tcp->th_ack), 558 tcp->th_flags | TH_RST); 559 if (m0 != NULL) 560 ip6_output(m0, NULL, NULL, 0, NULL, NULL, 561 NULL); 562 } 563 m_freem(m); 564 } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */ 565#if 0 566 /* 567 * Unlike above, the mbufs need to line up with the ip6 hdr, 568 * as the contents are read. We need to m_adj() the 569 * needed amount. 570 * The mbuf will however be thrown away so we can adjust it. 571 * Remember we did an m_pullup on it already so we 572 * can make some assumptions about contiguousness. 573 */ 574 if (args->L3offset) 575 m_adj(m, args->L3offset); 576#endif 577 icmp6_error(m, ICMP6_DST_UNREACH, code, 0); 578 } else 579 m_freem(m); 580 581 args->m = NULL; 582} 583 584#endif /* INET6 */ 585 586 587/* 588 * sends a reject message, consuming the mbuf passed as an argument. 589 */ 590static void 591send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip) 592{ 593 594#if 0 595 /* XXX When ip is not guaranteed to be at mtod() we will 596 * need to account for this */ 597 * The mbuf will however be thrown away so we can adjust it. 598 * Remember we did an m_pullup on it already so we 599 * can make some assumptions about contiguousness. 600 */ 601 if (args->L3offset) 602 m_adj(m, args->L3offset); 603#endif 604 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */ 605 /* We need the IP header in host order for icmp_error(). */ 606 if (args->eh != NULL) { 607 SET_HOST_IPLEN(ip); 608 } 609 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0); 610 } else if (args->f_id.proto == IPPROTO_TCP) { 611 struct tcphdr *const tcp = 612 L3HDR(struct tcphdr, mtod(args->m, struct ip *)); 613 if ( (tcp->th_flags & TH_RST) == 0) { 614 struct mbuf *m; 615 m = ipfw_send_pkt(args->m, &(args->f_id), 616 ntohl(tcp->th_seq), ntohl(tcp->th_ack), 617 tcp->th_flags | TH_RST); 618 if (m != NULL) 619 ip_output(m, NULL, NULL, 0, NULL, NULL); 620 } 621 m_freem(args->m); 622 } else 623 m_freem(args->m); 624 args->m = NULL; 625} 626 627/* 628 * Support for uid/gid/jail lookup. These tests are expensive 629 * (because we may need to look into the list of active sockets) 630 * so we cache the results. ugid_lookupp is 0 if we have not 631 * yet done a lookup, 1 if we succeeded, and -1 if we tried 632 * and failed. The function always returns the match value. 633 * We could actually spare the variable and use *uc, setting 634 * it to '(void *)check_uidgid if we have no info, NULL if 635 * we tried and failed, or any other value if successful. 636 */ 637static int 638check_uidgid(ipfw_insn_u32 *insn, int proto, struct ifnet *oif, 639 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip, 640 u_int16_t src_port, struct ucred **uc, int *ugid_lookupp, 641 struct inpcb *inp) 642{ 643 struct inpcbinfo *pi; 644 int wildcard; 645 struct inpcb *pcb; 646 int match; 647 648 /* 649 * Check to see if the UDP or TCP stack supplied us with 650 * the PCB. If so, rather then holding a lock and looking 651 * up the PCB, we can use the one that was supplied. 652 */ 653 if (inp && *ugid_lookupp == 0) { 654 INP_LOCK_ASSERT(inp); 655 if (inp->inp_socket != NULL) { 656 *uc = crhold(inp->inp_cred); 657 *ugid_lookupp = 1; 658 } else 659 *ugid_lookupp = -1; 660 } 661 /* 662 * If we have already been here and the packet has no 663 * PCB entry associated with it, then we can safely 664 * assume that this is a no match. 665 */ 666 if (*ugid_lookupp == -1) 667 return (0); 668 if (proto == IPPROTO_TCP) { 669 wildcard = 0; 670 pi = &V_tcbinfo; 671 } else if (proto == IPPROTO_UDP) { 672 wildcard = INPLOOKUP_WILDCARD; 673 pi = &V_udbinfo; 674 } else 675 return 0; 676 match = 0; 677 if (*ugid_lookupp == 0) { 678 INP_INFO_RLOCK(pi); 679 pcb = (oif) ? 680 in_pcblookup_hash(pi, 681 dst_ip, htons(dst_port), 682 src_ip, htons(src_port), 683 wildcard, oif) : 684 in_pcblookup_hash(pi, 685 src_ip, htons(src_port), 686 dst_ip, htons(dst_port), 687 wildcard, NULL); 688 if (pcb != NULL) { 689 *uc = crhold(pcb->inp_cred); 690 *ugid_lookupp = 1; 691 } 692 INP_INFO_RUNLOCK(pi); 693 if (*ugid_lookupp == 0) { 694 /* 695 * We tried and failed, set the variable to -1 696 * so we will not try again on this packet. 697 */ 698 *ugid_lookupp = -1; 699 return (0); 700 } 701 } 702 if (insn->o.opcode == O_UID) 703 match = ((*uc)->cr_uid == (uid_t)insn->d[0]); 704 else if (insn->o.opcode == O_GID) 705 match = groupmember((gid_t)insn->d[0], *uc); 706 else if (insn->o.opcode == O_JAIL) 707 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]); 708 return match; 709} 710 711/* 712 * Helper function to write the matching rule into args 713 */ 714static inline void 715set_match(struct ip_fw_args *args, int slot, 716 struct ip_fw_chain *chain) 717{ 718 args->chain_id = chain->id; 719 args->slot = slot + 1; /* we use 0 as a marker */ 720 args->rule_id = chain->map[slot]->id; 721 args->rulenum = chain->map[slot]->rulenum; 722} 723 724/* 725 * The main check routine for the firewall. 726 * 727 * All arguments are in args so we can modify them and return them 728 * back to the caller. 729 * 730 * Parameters: 731 * 732 * args->m (in/out) The packet; we set to NULL when/if we nuke it. 733 * Starts with the IP header. 734 * args->eh (in) Mac header if present, NULL for layer3 packet. 735 * args->L3offset Number of bytes bypassed if we came from L2. 736 * e.g. often sizeof(eh) ** NOTYET ** 737 * args->oif Outgoing interface, NULL if packet is incoming. 738 * The incoming interface is in the mbuf. (in) 739 * args->divert_rule (in/out) 740 * Skip up to the first rule past this rule number; 741 * upon return, non-zero port number for divert or tee. 742 * 743 * args->rule Pointer to the last matching rule (in/out) 744 * args->next_hop Socket we are forwarding to (out). 745 * args->f_id Addresses grabbed from the packet (out) 746 * args->cookie a cookie depending on rule action 747 * 748 * Return value: 749 * 750 * IP_FW_PASS the packet must be accepted 751 * IP_FW_DENY the packet must be dropped 752 * IP_FW_DIVERT divert packet, port in m_tag 753 * IP_FW_TEE tee packet, port in m_tag 754 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie 755 * IP_FW_NETGRAPH into netgraph, cookie args->cookie 756 * 757 */ 758int 759ipfw_chk(struct ip_fw_args *args) 760{ 761 762 /* 763 * Local variables holding state while processing a packet: 764 * 765 * IMPORTANT NOTE: to speed up the processing of rules, there 766 * are some assumption on the values of the variables, which 767 * are documented here. Should you change them, please check 768 * the implementation of the various instructions to make sure 769 * that they still work. 770 * 771 * args->eh The MAC header. It is non-null for a layer2 772 * packet, it is NULL for a layer-3 packet. 773 * **notyet** 774 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header. 775 * 776 * m | args->m Pointer to the mbuf, as received from the caller. 777 * It may change if ipfw_chk() does an m_pullup, or if it 778 * consumes the packet because it calls send_reject(). 779 * XXX This has to change, so that ipfw_chk() never modifies 780 * or consumes the buffer. 781 * ip is the beginning of the ip(4 or 6) header. 782 * Calculated by adding the L3offset to the start of data. 783 * (Until we start using L3offset, the packet is 784 * supposed to start with the ip header). 785 */ 786 struct mbuf *m = args->m; 787 struct ip *ip = mtod(m, struct ip *); 788 789 /* 790 * For rules which contain uid/gid or jail constraints, cache 791 * a copy of the users credentials after the pcb lookup has been 792 * executed. This will speed up the processing of rules with 793 * these types of constraints, as well as decrease contention 794 * on pcb related locks. 795 */ 796 struct ucred *ucred_cache = NULL; 797 int ucred_lookup = 0; 798 799 /* 800 * divinput_flags If non-zero, set to the IP_FW_DIVERT_*_FLAG 801 * associated with a packet input on a divert socket. This 802 * will allow to distinguish traffic and its direction when 803 * it originates from a divert socket. 804 */ 805 u_int divinput_flags = 0; 806 807 /* 808 * oif | args->oif If NULL, ipfw_chk has been called on the 809 * inbound path (ether_input, ip_input). 810 * If non-NULL, ipfw_chk has been called on the outbound path 811 * (ether_output, ip_output). 812 */ 813 struct ifnet *oif = args->oif; 814 815 int f_pos = 0; /* index of current rule in the array */ 816 int retval = 0; 817 818 /* 819 * hlen The length of the IP header. 820 */ 821 u_int hlen = 0; /* hlen >0 means we have an IP pkt */ 822 823 /* 824 * offset The offset of a fragment. offset != 0 means that 825 * we have a fragment at this offset of an IPv4 packet. 826 * offset == 0 means that (if this is an IPv4 packet) 827 * this is the first or only fragment. 828 * For IPv6 offset == 0 means there is no Fragment Header. 829 * If offset != 0 for IPv6 always use correct mask to 830 * get the correct offset because we add IP6F_MORE_FRAG 831 * to be able to dectect the first fragment which would 832 * otherwise have offset = 0. 833 */ 834 u_short offset = 0; 835 836 /* 837 * Local copies of addresses. They are only valid if we have 838 * an IP packet. 839 * 840 * proto The protocol. Set to 0 for non-ip packets, 841 * or to the protocol read from the packet otherwise. 842 * proto != 0 means that we have an IPv4 packet. 843 * 844 * src_port, dst_port port numbers, in HOST format. Only 845 * valid for TCP and UDP packets. 846 * 847 * src_ip, dst_ip ip addresses, in NETWORK format. 848 * Only valid for IPv4 packets. 849 */ 850 uint8_t proto; 851 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */ 852 struct in_addr src_ip, dst_ip; /* NOTE: network format */ 853 uint16_t iplen=0; 854 int pktlen; 855 uint16_t etype = 0; /* Host order stored ether type */ 856 857 /* 858 * dyn_dir = MATCH_UNKNOWN when rules unchecked, 859 * MATCH_NONE when checked and not matched (q = NULL), 860 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) 861 */ 862 int dyn_dir = MATCH_UNKNOWN; 863 ipfw_dyn_rule *q = NULL; 864 struct ip_fw_chain *chain = &V_layer3_chain; 865 struct m_tag *mtag; 866 867 /* 868 * We store in ulp a pointer to the upper layer protocol header. 869 * In the ipv4 case this is easy to determine from the header, 870 * but for ipv6 we might have some additional headers in the middle. 871 * ulp is NULL if not found. 872 */ 873 void *ulp = NULL; /* upper layer protocol pointer. */ 874 /* XXX ipv6 variables */ 875 int is_ipv6 = 0; 876 u_int16_t ext_hd = 0; /* bits vector for extension header filtering */ 877 /* end of ipv6 variables */ 878 int is_ipv4 = 0; 879 880 int done = 0; /* flag to exit the outer loop */ 881 882 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready)) 883 return (IP_FW_PASS); /* accept */ 884 885 dst_ip.s_addr = 0; /* make sure it is initialized */ 886 src_ip.s_addr = 0; /* make sure it is initialized */ 887 pktlen = m->m_pkthdr.len; 888 args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */ 889 proto = args->f_id.proto = 0; /* mark f_id invalid */ 890 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */ 891 892/* 893 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, 894 * then it sets p to point at the offset "len" in the mbuf. WARNING: the 895 * pointer might become stale after other pullups (but we never use it 896 * this way). 897 */ 898#define PULLUP_TO(_len, p, T) \ 899do { \ 900 int x = (_len) + sizeof(T); \ 901 if ((m)->m_len < x) { \ 902 args->m = m = m_pullup(m, x); \ 903 if (m == NULL) \ 904 goto pullup_failed; \ 905 } \ 906 p = (mtod(m, char *) + (_len)); \ 907} while (0) 908 909 /* 910 * if we have an ether header, 911 */ 912 if (args->eh) 913 etype = ntohs(args->eh->ether_type); 914 915 /* Identify IP packets and fill up variables. */ 916 if (pktlen >= sizeof(struct ip6_hdr) && 917 (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) { 918 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip; 919 is_ipv6 = 1; 920 args->f_id.addr_type = 6; 921 hlen = sizeof(struct ip6_hdr); 922 proto = ip6->ip6_nxt; 923 924 /* Search extension headers to find upper layer protocols */ 925 while (ulp == NULL) { 926 switch (proto) { 927 case IPPROTO_ICMPV6: 928 PULLUP_TO(hlen, ulp, struct icmp6_hdr); 929 args->f_id.flags = ICMP6(ulp)->icmp6_type; 930 break; 931 932 case IPPROTO_TCP: 933 PULLUP_TO(hlen, ulp, struct tcphdr); 934 dst_port = TCP(ulp)->th_dport; 935 src_port = TCP(ulp)->th_sport; 936 args->f_id.flags = TCP(ulp)->th_flags; 937 break; 938 939 case IPPROTO_SCTP: 940 PULLUP_TO(hlen, ulp, struct sctphdr); 941 src_port = SCTP(ulp)->src_port; 942 dst_port = SCTP(ulp)->dest_port; 943 break; 944 945 case IPPROTO_UDP: 946 PULLUP_TO(hlen, ulp, struct udphdr); 947 dst_port = UDP(ulp)->uh_dport; 948 src_port = UDP(ulp)->uh_sport; 949 break; 950 951 case IPPROTO_HOPOPTS: /* RFC 2460 */ 952 PULLUP_TO(hlen, ulp, struct ip6_hbh); 953 ext_hd |= EXT_HOPOPTS; 954 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 955 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 956 ulp = NULL; 957 break; 958 959 case IPPROTO_ROUTING: /* RFC 2460 */ 960 PULLUP_TO(hlen, ulp, struct ip6_rthdr); 961 switch (((struct ip6_rthdr *)ulp)->ip6r_type) { 962 case 0: 963 ext_hd |= EXT_RTHDR0; 964 break; 965 case 2: 966 ext_hd |= EXT_RTHDR2; 967 break; 968 default: 969 printf("IPFW2: IPV6 - Unknown Routing " 970 "Header type(%d)\n", 971 ((struct ip6_rthdr *)ulp)->ip6r_type); 972 if (V_fw_deny_unknown_exthdrs) 973 return (IP_FW_DENY); 974 break; 975 } 976 ext_hd |= EXT_ROUTING; 977 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3; 978 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; 979 ulp = NULL; 980 break; 981 982 case IPPROTO_FRAGMENT: /* RFC 2460 */ 983 PULLUP_TO(hlen, ulp, struct ip6_frag); 984 ext_hd |= EXT_FRAGMENT; 985 hlen += sizeof (struct ip6_frag); 986 proto = ((struct ip6_frag *)ulp)->ip6f_nxt; 987 offset = ((struct ip6_frag *)ulp)->ip6f_offlg & 988 IP6F_OFF_MASK; 989 /* Add IP6F_MORE_FRAG for offset of first 990 * fragment to be != 0. */ 991 offset |= ((struct ip6_frag *)ulp)->ip6f_offlg & 992 IP6F_MORE_FRAG; 993 if (offset == 0) { 994 printf("IPFW2: IPV6 - Invalid Fragment " 995 "Header\n"); 996 if (V_fw_deny_unknown_exthdrs) 997 return (IP_FW_DENY); 998 break; 999 } 1000 args->f_id.frag_id6 = 1001 ntohl(((struct ip6_frag *)ulp)->ip6f_ident); 1002 ulp = NULL; 1003 break; 1004 1005 case IPPROTO_DSTOPTS: /* RFC 2460 */ 1006 PULLUP_TO(hlen, ulp, struct ip6_hbh); 1007 ext_hd |= EXT_DSTOPTS; 1008 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 1009 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 1010 ulp = NULL; 1011 break; 1012 1013 case IPPROTO_AH: /* RFC 2402 */ 1014 PULLUP_TO(hlen, ulp, struct ip6_ext); 1015 ext_hd |= EXT_AH; 1016 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2; 1017 proto = ((struct ip6_ext *)ulp)->ip6e_nxt; 1018 ulp = NULL; 1019 break; 1020 1021 case IPPROTO_ESP: /* RFC 2406 */ 1022 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */ 1023 /* Anything past Seq# is variable length and 1024 * data past this ext. header is encrypted. */ 1025 ext_hd |= EXT_ESP; 1026 break; 1027 1028 case IPPROTO_NONE: /* RFC 2460 */ 1029 /* 1030 * Packet ends here, and IPv6 header has 1031 * already been pulled up. If ip6e_len!=0 1032 * then octets must be ignored. 1033 */ 1034 ulp = ip; /* non-NULL to get out of loop. */ 1035 break; 1036 1037 case IPPROTO_OSPFIGP: 1038 /* XXX OSPF header check? */ 1039 PULLUP_TO(hlen, ulp, struct ip6_ext); 1040 break; 1041 1042 case IPPROTO_PIM: 1043 /* XXX PIM header check? */ 1044 PULLUP_TO(hlen, ulp, struct pim); 1045 break; 1046 1047 case IPPROTO_CARP: 1048 PULLUP_TO(hlen, ulp, struct carp_header); 1049 if (((struct carp_header *)ulp)->carp_version != 1050 CARP_VERSION) 1051 return (IP_FW_DENY); 1052 if (((struct carp_header *)ulp)->carp_type != 1053 CARP_ADVERTISEMENT) 1054 return (IP_FW_DENY); 1055 break; 1056 1057 case IPPROTO_IPV6: /* RFC 2893 */ 1058 PULLUP_TO(hlen, ulp, struct ip6_hdr); 1059 break; 1060 1061 case IPPROTO_IPV4: /* RFC 2893 */ 1062 PULLUP_TO(hlen, ulp, struct ip); 1063 break; 1064 1065 default: 1066 printf("IPFW2: IPV6 - Unknown Extension " 1067 "Header(%d), ext_hd=%x\n", proto, ext_hd); 1068 if (V_fw_deny_unknown_exthdrs) 1069 return (IP_FW_DENY); 1070 PULLUP_TO(hlen, ulp, struct ip6_ext); 1071 break; 1072 } /*switch */ 1073 } 1074 ip = mtod(m, struct ip *); 1075 ip6 = (struct ip6_hdr *)ip; 1076 args->f_id.src_ip6 = ip6->ip6_src; 1077 args->f_id.dst_ip6 = ip6->ip6_dst; 1078 args->f_id.src_ip = 0; 1079 args->f_id.dst_ip = 0; 1080 args->f_id.flow_id6 = ntohl(ip6->ip6_flow); 1081 } else if (pktlen >= sizeof(struct ip) && 1082 (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) { 1083 is_ipv4 = 1; 1084 hlen = ip->ip_hl << 2; 1085 args->f_id.addr_type = 4; 1086 1087 /* 1088 * Collect parameters into local variables for faster matching. 1089 */ 1090 proto = ip->ip_p; 1091 src_ip = ip->ip_src; 1092 dst_ip = ip->ip_dst; 1093#ifndef HAVE_NET_IPLEN 1094 if (args->eh == NULL) { /* on l3 these are in host format */ 1095 offset = ip->ip_off & IP_OFFMASK; 1096 iplen = ip->ip_len; 1097 } else 1098#endif /* !HAVE_NET_IPLEN */ 1099 { /* otherwise they are in net format */ 1100 offset = ntohs(ip->ip_off) & IP_OFFMASK; 1101 iplen = ntohs(ip->ip_len); 1102 } 1103 pktlen = iplen < pktlen ? iplen : pktlen; 1104 1105 if (offset == 0) { 1106 switch (proto) { 1107 case IPPROTO_TCP: 1108 PULLUP_TO(hlen, ulp, struct tcphdr); 1109 dst_port = TCP(ulp)->th_dport; 1110 src_port = TCP(ulp)->th_sport; 1111 args->f_id.flags = TCP(ulp)->th_flags; 1112 break; 1113 1114 case IPPROTO_UDP: 1115 PULLUP_TO(hlen, ulp, struct udphdr); 1116 dst_port = UDP(ulp)->uh_dport; 1117 src_port = UDP(ulp)->uh_sport; 1118 break; 1119 1120 case IPPROTO_ICMP: 1121 PULLUP_TO(hlen, ulp, struct icmphdr); 1122 args->f_id.flags = ICMP(ulp)->icmp_type; 1123 break; 1124 1125 default: 1126 break; 1127 } 1128 } 1129 1130 ip = mtod(m, struct ip *); 1131 args->f_id.src_ip = ntohl(src_ip.s_addr); 1132 args->f_id.dst_ip = ntohl(dst_ip.s_addr); 1133 } 1134#undef PULLUP_TO 1135 if (proto) { /* we may have port numbers, store them */ 1136 args->f_id.proto = proto; 1137 args->f_id.src_port = src_port = ntohs(src_port); 1138 args->f_id.dst_port = dst_port = ntohs(dst_port); 1139 } 1140 1141 IPFW_RLOCK(chain); 1142 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */ 1143 IPFW_RUNLOCK(chain); 1144 return (IP_FW_PASS); /* accept */ 1145 } 1146 /* XXX divert should be handled same as other tags */ 1147 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 1148 if (args->slot) { 1149 /* 1150 * Packet has already been tagged as a result of a previous 1151 * match on rule args->rule aka args->rule_id (PIPE, QUEUE, 1152 * REASS, NETGRAPH and similar, never a skipto). 1153 * Validate the slot and continue from the next one 1154 * if still present, otherwise do a lookup. 1155 */ 1156 if (V_fw_one_pass) { 1157 IPFW_RUNLOCK(chain); 1158 return (IP_FW_PASS); 1159 } 1160 f_pos = (args->chain_id == chain->id) ? 1161 args->slot /* already incremented */ : 1162 ipfw_find_rule(chain, args->rulenum, args->rule_id+1); 1163 } else { 1164 /* 1165 * Find the starting rule. It can be either the first 1166 * one, or the one after divert_rule if asked so. 1167 */ 1168 int skipto = mtag ? divert_cookie(mtag) : 0; 1169 1170 f_pos = 0; 1171 if (args->eh == NULL && skipto != 0) { 1172 if (skipto >= IPFW_DEFAULT_RULE) { 1173 IPFW_RUNLOCK(chain); 1174 return (IP_FW_DENY); /* invalid */ 1175 } 1176 f_pos = ipfw_find_rule(chain, skipto+1, 0); 1177 } 1178 } 1179 /* reset divert rule to avoid confusion later */ 1180 if (mtag) { 1181 divinput_flags = divert_info(mtag) & 1182 (IP_FW_DIVERT_OUTPUT_FLAG | IP_FW_DIVERT_LOOPBACK_FLAG); 1183 m_tag_delete(m, mtag); 1184 } 1185 1186 /* 1187 * Now scan the rules, and parse microinstructions for each rule. 1188 * We have two nested loops and an inner switch. Sometimes we 1189 * need to break out of one or both loops, or re-enter one of 1190 * the loops with updated variables. Loop variables are: 1191 * 1192 * f_pos (outer loop) points to the current rule. 1193 * On output it points to the matching rule. 1194 * done (outer loop) is used as a flag to break the loop. 1195 * l (inner loop) residual length of current rule. 1196 * cmd points to the current microinstruction. 1197 * 1198 * We break the inner loop by setting l=0 and possibly 1199 * cmdlen=0 if we don't want to advance cmd. 1200 * We break the outer loop by setting done=1 1201 * We can restart the inner loop by setting l>0 and f_pos, f, cmd 1202 * as needed. 1203 */ 1204 for (; f_pos < chain->n_rules; f_pos++) { 1205 ipfw_insn *cmd; 1206 uint32_t tablearg = 0; 1207 int l, cmdlen, skip_or; /* skip rest of OR block */ 1208 struct ip_fw *f; 1209 1210 f = chain->map[f_pos]; 1211 if (V_set_disable & (1 << f->set) ) 1212 continue; 1213 1214 skip_or = 0; 1215 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; 1216 l -= cmdlen, cmd += cmdlen) { 1217 int match; 1218 1219 /* 1220 * check_body is a jump target used when we find a 1221 * CHECK_STATE, and need to jump to the body of 1222 * the target rule. 1223 */ 1224 1225/* check_body: */ 1226 cmdlen = F_LEN(cmd); 1227 /* 1228 * An OR block (insn_1 || .. || insn_n) has the 1229 * F_OR bit set in all but the last instruction. 1230 * The first match will set "skip_or", and cause 1231 * the following instructions to be skipped until 1232 * past the one with the F_OR bit clear. 1233 */ 1234 if (skip_or) { /* skip this instruction */ 1235 if ((cmd->len & F_OR) == 0) 1236 skip_or = 0; /* next one is good */ 1237 continue; 1238 } 1239 match = 0; /* set to 1 if we succeed */ 1240 1241 switch (cmd->opcode) { 1242 /* 1243 * The first set of opcodes compares the packet's 1244 * fields with some pattern, setting 'match' if a 1245 * match is found. At the end of the loop there is 1246 * logic to deal with F_NOT and F_OR flags associated 1247 * with the opcode. 1248 */ 1249 case O_NOP: 1250 match = 1; 1251 break; 1252 1253 case O_FORWARD_MAC: 1254 printf("ipfw: opcode %d unimplemented\n", 1255 cmd->opcode); 1256 break; 1257 1258 case O_GID: 1259 case O_UID: 1260 case O_JAIL: 1261 /* 1262 * We only check offset == 0 && proto != 0, 1263 * as this ensures that we have a 1264 * packet with the ports info. 1265 */ 1266 if (offset!=0) 1267 break; 1268 if (is_ipv6) /* XXX to be fixed later */ 1269 break; 1270 if (proto == IPPROTO_TCP || 1271 proto == IPPROTO_UDP) 1272 match = check_uidgid( 1273 (ipfw_insn_u32 *)cmd, 1274 proto, oif, 1275 dst_ip, dst_port, 1276 src_ip, src_port, &ucred_cache, 1277 &ucred_lookup, args->inp); 1278 break; 1279 1280 case O_RECV: 1281 match = iface_match(m->m_pkthdr.rcvif, 1282 (ipfw_insn_if *)cmd); 1283 break; 1284 1285 case O_XMIT: 1286 match = iface_match(oif, (ipfw_insn_if *)cmd); 1287 break; 1288 1289 case O_VIA: 1290 match = iface_match(oif ? oif : 1291 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd); 1292 break; 1293 1294 case O_MACADDR2: 1295 if (args->eh != NULL) { /* have MAC header */ 1296 u_int32_t *want = (u_int32_t *) 1297 ((ipfw_insn_mac *)cmd)->addr; 1298 u_int32_t *mask = (u_int32_t *) 1299 ((ipfw_insn_mac *)cmd)->mask; 1300 u_int32_t *hdr = (u_int32_t *)args->eh; 1301 1302 match = 1303 ( want[0] == (hdr[0] & mask[0]) && 1304 want[1] == (hdr[1] & mask[1]) && 1305 want[2] == (hdr[2] & mask[2]) ); 1306 } 1307 break; 1308 1309 case O_MAC_TYPE: 1310 if (args->eh != NULL) { 1311 u_int16_t *p = 1312 ((ipfw_insn_u16 *)cmd)->ports; 1313 int i; 1314 1315 for (i = cmdlen - 1; !match && i>0; 1316 i--, p += 2) 1317 match = (etype >= p[0] && 1318 etype <= p[1]); 1319 } 1320 break; 1321 1322 case O_FRAG: 1323 match = (offset != 0); 1324 break; 1325 1326 case O_IN: /* "out" is "not in" */ 1327 match = (oif == NULL); 1328 break; 1329 1330 case O_LAYER2: 1331 match = (args->eh != NULL); 1332 break; 1333 1334 case O_DIVERTED: 1335 match = (cmd->arg1 & 1 && divinput_flags & 1336 IP_FW_DIVERT_LOOPBACK_FLAG) || 1337 (cmd->arg1 & 2 && divinput_flags & 1338 IP_FW_DIVERT_OUTPUT_FLAG); 1339 break; 1340 1341 case O_PROTO: 1342 /* 1343 * We do not allow an arg of 0 so the 1344 * check of "proto" only suffices. 1345 */ 1346 match = (proto == cmd->arg1); 1347 break; 1348 1349 case O_IP_SRC: 1350 match = is_ipv4 && 1351 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1352 src_ip.s_addr); 1353 break; 1354 1355 case O_IP_SRC_LOOKUP: 1356 case O_IP_DST_LOOKUP: 1357 if (is_ipv4) { 1358 uint32_t key = 1359 (cmd->opcode == O_IP_DST_LOOKUP) ? 1360 dst_ip.s_addr : src_ip.s_addr; 1361 uint32_t v = 0; 1362 1363 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) { 1364 /* generic lookup. The key must be 1365 * in 32bit big-endian format. 1366 */ 1367 v = ((ipfw_insn_u32 *)cmd)->d[1]; 1368 if (v == 0) 1369 key = dst_ip.s_addr; 1370 else if (v == 1) 1371 key = src_ip.s_addr; 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_cache, 1387 &ucred_lookup, args->inp); 1388 if (v == 4 /* O_UID */) 1389 key = ucred_cache->cr_uid; 1390 else if (v == 5 /* O_JAIL */) 1391 key = ucred_cache->cr_prison->pr_id; 1392 key = htonl(key); 1393 } else 1394 break; 1395 } 1396 match = ipfw_lookup_table(chain, 1397 cmd->arg1, key, &v); 1398 if (!match) 1399 break; 1400 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) 1401 match = 1402 ((ipfw_insn_u32 *)cmd)->d[0] == v; 1403 else 1404 tablearg = v; 1405 } 1406 break; 1407 1408 case O_IP_SRC_MASK: 1409 case O_IP_DST_MASK: 1410 if (is_ipv4) { 1411 uint32_t a = 1412 (cmd->opcode == O_IP_DST_MASK) ? 1413 dst_ip.s_addr : src_ip.s_addr; 1414 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; 1415 int i = cmdlen-1; 1416 1417 for (; !match && i>0; i-= 2, p+= 2) 1418 match = (p[0] == (a & p[1])); 1419 } 1420 break; 1421 1422 case O_IP_SRC_ME: 1423 if (is_ipv4) { 1424 struct ifnet *tif; 1425 1426 INADDR_TO_IFP(src_ip, tif); 1427 match = (tif != NULL); 1428 } 1429 break; 1430 1431 case O_IP_DST_SET: 1432 case O_IP_SRC_SET: 1433 if (is_ipv4) { 1434 u_int32_t *d = (u_int32_t *)(cmd+1); 1435 u_int32_t addr = 1436 cmd->opcode == O_IP_DST_SET ? 1437 args->f_id.dst_ip : 1438 args->f_id.src_ip; 1439 1440 if (addr < d[0]) 1441 break; 1442 addr -= d[0]; /* subtract base */ 1443 match = (addr < cmd->arg1) && 1444 ( d[ 1 + (addr>>5)] & 1445 (1<<(addr & 0x1f)) ); 1446 } 1447 break; 1448 1449 case O_IP_DST: 1450 match = is_ipv4 && 1451 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1452 dst_ip.s_addr); 1453 break; 1454 1455 case O_IP_DST_ME: 1456 if (is_ipv4) { 1457 struct ifnet *tif; 1458 1459 INADDR_TO_IFP(dst_ip, tif); 1460 match = (tif != NULL); 1461 } 1462 break; 1463 1464 case O_IP_SRCPORT: 1465 case O_IP_DSTPORT: 1466 /* 1467 * offset == 0 && proto != 0 is enough 1468 * to guarantee that we have a 1469 * packet with port info. 1470 */ 1471 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP) 1472 && offset == 0) { 1473 u_int16_t x = 1474 (cmd->opcode == O_IP_SRCPORT) ? 1475 src_port : dst_port ; 1476 u_int16_t *p = 1477 ((ipfw_insn_u16 *)cmd)->ports; 1478 int i; 1479 1480 for (i = cmdlen - 1; !match && i>0; 1481 i--, p += 2) 1482 match = (x>=p[0] && x<=p[1]); 1483 } 1484 break; 1485 1486 case O_ICMPTYPE: 1487 match = (offset == 0 && proto==IPPROTO_ICMP && 1488 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); 1489 break; 1490 1491#ifdef INET6 1492 case O_ICMP6TYPE: 1493 match = is_ipv6 && offset == 0 && 1494 proto==IPPROTO_ICMPV6 && 1495 icmp6type_match( 1496 ICMP6(ulp)->icmp6_type, 1497 (ipfw_insn_u32 *)cmd); 1498 break; 1499#endif /* INET6 */ 1500 1501 case O_IPOPT: 1502 match = (is_ipv4 && 1503 ipopts_match(ip, cmd) ); 1504 break; 1505 1506 case O_IPVER: 1507 match = (is_ipv4 && 1508 cmd->arg1 == ip->ip_v); 1509 break; 1510 1511 case O_IPID: 1512 case O_IPLEN: 1513 case O_IPTTL: 1514 if (is_ipv4) { /* only for IP packets */ 1515 uint16_t x; 1516 uint16_t *p; 1517 int i; 1518 1519 if (cmd->opcode == O_IPLEN) 1520 x = iplen; 1521 else if (cmd->opcode == O_IPTTL) 1522 x = ip->ip_ttl; 1523 else /* must be IPID */ 1524 x = ntohs(ip->ip_id); 1525 if (cmdlen == 1) { 1526 match = (cmd->arg1 == x); 1527 break; 1528 } 1529 /* otherwise we have ranges */ 1530 p = ((ipfw_insn_u16 *)cmd)->ports; 1531 i = cmdlen - 1; 1532 for (; !match && i>0; i--, p += 2) 1533 match = (x >= p[0] && x <= p[1]); 1534 } 1535 break; 1536 1537 case O_IPPRECEDENCE: 1538 match = (is_ipv4 && 1539 (cmd->arg1 == (ip->ip_tos & 0xe0)) ); 1540 break; 1541 1542 case O_IPTOS: 1543 match = (is_ipv4 && 1544 flags_match(cmd, ip->ip_tos)); 1545 break; 1546 1547 case O_TCPDATALEN: 1548 if (proto == IPPROTO_TCP && offset == 0) { 1549 struct tcphdr *tcp; 1550 uint16_t x; 1551 uint16_t *p; 1552 int i; 1553 1554 tcp = TCP(ulp); 1555 x = iplen - 1556 ((ip->ip_hl + tcp->th_off) << 2); 1557 if (cmdlen == 1) { 1558 match = (cmd->arg1 == x); 1559 break; 1560 } 1561 /* otherwise we have ranges */ 1562 p = ((ipfw_insn_u16 *)cmd)->ports; 1563 i = cmdlen - 1; 1564 for (; !match && i>0; i--, p += 2) 1565 match = (x >= p[0] && x <= p[1]); 1566 } 1567 break; 1568 1569 case O_TCPFLAGS: 1570 match = (proto == IPPROTO_TCP && offset == 0 && 1571 flags_match(cmd, TCP(ulp)->th_flags)); 1572 break; 1573 1574 case O_TCPOPTS: 1575 match = (proto == IPPROTO_TCP && offset == 0 && 1576 tcpopts_match(TCP(ulp), cmd)); 1577 break; 1578 1579 case O_TCPSEQ: 1580 match = (proto == IPPROTO_TCP && offset == 0 && 1581 ((ipfw_insn_u32 *)cmd)->d[0] == 1582 TCP(ulp)->th_seq); 1583 break; 1584 1585 case O_TCPACK: 1586 match = (proto == IPPROTO_TCP && offset == 0 && 1587 ((ipfw_insn_u32 *)cmd)->d[0] == 1588 TCP(ulp)->th_ack); 1589 break; 1590 1591 case O_TCPWIN: 1592 match = (proto == IPPROTO_TCP && offset == 0 && 1593 cmd->arg1 == TCP(ulp)->th_win); 1594 break; 1595 1596 case O_ESTAB: 1597 /* reject packets which have SYN only */ 1598 /* XXX should i also check for TH_ACK ? */ 1599 match = (proto == IPPROTO_TCP && offset == 0 && 1600 (TCP(ulp)->th_flags & 1601 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); 1602 break; 1603 1604 case O_ALTQ: { 1605 struct pf_mtag *at; 1606 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 1607 1608 match = 1; 1609 at = pf_find_mtag(m); 1610 if (at != NULL && at->qid != 0) 1611 break; 1612 at = pf_get_mtag(m); 1613 if (at == NULL) { 1614 /* 1615 * Let the packet fall back to the 1616 * default ALTQ. 1617 */ 1618 break; 1619 } 1620 at->qid = altq->qid; 1621 if (is_ipv4) 1622 at->af = AF_INET; 1623 else 1624 at->af = AF_LINK; 1625 at->hdr = ip; 1626 break; 1627 } 1628 1629 case O_LOG: 1630 ipfw_log(f, hlen, args, m, 1631 oif, offset, tablearg, ip); 1632 match = 1; 1633 break; 1634 1635 case O_PROB: 1636 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); 1637 break; 1638 1639 case O_VERREVPATH: 1640 /* Outgoing packets automatically pass/match */ 1641 match = ((oif != NULL) || 1642 (m->m_pkthdr.rcvif == NULL) || 1643 ( 1644#ifdef INET6 1645 is_ipv6 ? 1646 verify_path6(&(args->f_id.src_ip6), 1647 m->m_pkthdr.rcvif) : 1648#endif 1649 verify_path(src_ip, m->m_pkthdr.rcvif, 1650 args->f_id.fib))); 1651 break; 1652 1653 case O_VERSRCREACH: 1654 /* Outgoing packets automatically pass/match */ 1655 match = (hlen > 0 && ((oif != NULL) || 1656#ifdef INET6 1657 is_ipv6 ? 1658 verify_path6(&(args->f_id.src_ip6), 1659 NULL) : 1660#endif 1661 verify_path(src_ip, NULL, args->f_id.fib))); 1662 break; 1663 1664 case O_ANTISPOOF: 1665 /* Outgoing packets automatically pass/match */ 1666 if (oif == NULL && hlen > 0 && 1667 ( (is_ipv4 && in_localaddr(src_ip)) 1668#ifdef INET6 1669 || (is_ipv6 && 1670 in6_localaddr(&(args->f_id.src_ip6))) 1671#endif 1672 )) 1673 match = 1674#ifdef INET6 1675 is_ipv6 ? verify_path6( 1676 &(args->f_id.src_ip6), 1677 m->m_pkthdr.rcvif) : 1678#endif 1679 verify_path(src_ip, 1680 m->m_pkthdr.rcvif, 1681 args->f_id.fib); 1682 else 1683 match = 1; 1684 break; 1685 1686 case O_IPSEC: 1687#ifdef IPSEC 1688 match = (m_tag_find(m, 1689 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); 1690#endif 1691 /* otherwise no match */ 1692 break; 1693 1694#ifdef INET6 1695 case O_IP6_SRC: 1696 match = is_ipv6 && 1697 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, 1698 &((ipfw_insn_ip6 *)cmd)->addr6); 1699 break; 1700 1701 case O_IP6_DST: 1702 match = is_ipv6 && 1703 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, 1704 &((ipfw_insn_ip6 *)cmd)->addr6); 1705 break; 1706 case O_IP6_SRC_MASK: 1707 case O_IP6_DST_MASK: 1708 if (is_ipv6) { 1709 int i = cmdlen - 1; 1710 struct in6_addr p; 1711 struct in6_addr *d = 1712 &((ipfw_insn_ip6 *)cmd)->addr6; 1713 1714 for (; !match && i > 0; d += 2, 1715 i -= F_INSN_SIZE(struct in6_addr) 1716 * 2) { 1717 p = (cmd->opcode == 1718 O_IP6_SRC_MASK) ? 1719 args->f_id.src_ip6: 1720 args->f_id.dst_ip6; 1721 APPLY_MASK(&p, &d[1]); 1722 match = 1723 IN6_ARE_ADDR_EQUAL(&d[0], 1724 &p); 1725 } 1726 } 1727 break; 1728 1729 case O_IP6_SRC_ME: 1730 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6); 1731 break; 1732 1733 case O_IP6_DST_ME: 1734 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6); 1735 break; 1736 1737 case O_FLOW6ID: 1738 match = is_ipv6 && 1739 flow6id_match(args->f_id.flow_id6, 1740 (ipfw_insn_u32 *) cmd); 1741 break; 1742 1743 case O_EXT_HDR: 1744 match = is_ipv6 && 1745 (ext_hd & ((ipfw_insn *) cmd)->arg1); 1746 break; 1747 1748 case O_IP6: 1749 match = is_ipv6; 1750 break; 1751#endif 1752 1753 case O_IP4: 1754 match = is_ipv4; 1755 break; 1756 1757 case O_TAG: { 1758 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1759 tablearg : cmd->arg1; 1760 1761 /* Packet is already tagged with this tag? */ 1762 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL); 1763 1764 /* We have `untag' action when F_NOT flag is 1765 * present. And we must remove this mtag from 1766 * mbuf and reset `match' to zero (`match' will 1767 * be inversed later). 1768 * Otherwise we should allocate new mtag and 1769 * push it into mbuf. 1770 */ 1771 if (cmd->len & F_NOT) { /* `untag' action */ 1772 if (mtag != NULL) 1773 m_tag_delete(m, mtag); 1774 } else if (mtag == NULL) { 1775 if ((mtag = m_tag_alloc(MTAG_IPFW, 1776 tag, 0, M_NOWAIT)) != NULL) 1777 m_tag_prepend(m, mtag); 1778 } 1779 match = (cmd->len & F_NOT) ? 0: 1; 1780 break; 1781 } 1782 1783 case O_FIB: /* try match the specified fib */ 1784 if (args->f_id.fib == cmd->arg1) 1785 match = 1; 1786 break; 1787 1788 case O_TAGGED: { 1789 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1790 tablearg : cmd->arg1; 1791 1792 if (cmdlen == 1) { 1793 match = m_tag_locate(m, MTAG_IPFW, 1794 tag, NULL) != NULL; 1795 break; 1796 } 1797 1798 /* we have ranges */ 1799 for (mtag = m_tag_first(m); 1800 mtag != NULL && !match; 1801 mtag = m_tag_next(m, mtag)) { 1802 uint16_t *p; 1803 int i; 1804 1805 if (mtag->m_tag_cookie != MTAG_IPFW) 1806 continue; 1807 1808 p = ((ipfw_insn_u16 *)cmd)->ports; 1809 i = cmdlen - 1; 1810 for(; !match && i > 0; i--, p += 2) 1811 match = 1812 mtag->m_tag_id >= p[0] && 1813 mtag->m_tag_id <= p[1]; 1814 } 1815 break; 1816 } 1817 1818 /* 1819 * The second set of opcodes represents 'actions', 1820 * i.e. the terminal part of a rule once the packet 1821 * matches all previous patterns. 1822 * Typically there is only one action for each rule, 1823 * and the opcode is stored at the end of the rule 1824 * (but there are exceptions -- see below). 1825 * 1826 * In general, here we set retval and terminate the 1827 * outer loop (would be a 'break 3' in some language, 1828 * but we need to set l=0, done=1) 1829 * 1830 * Exceptions: 1831 * O_COUNT and O_SKIPTO actions: 1832 * instead of terminating, we jump to the next rule 1833 * (setting l=0), or to the SKIPTO target (setting 1834 * f/f_len, cmd and l as needed), respectively. 1835 * 1836 * O_TAG, O_LOG and O_ALTQ action parameters: 1837 * perform some action and set match = 1; 1838 * 1839 * O_LIMIT and O_KEEP_STATE: these opcodes are 1840 * not real 'actions', and are stored right 1841 * before the 'action' part of the rule. 1842 * These opcodes try to install an entry in the 1843 * state tables; if successful, we continue with 1844 * the next opcode (match=1; break;), otherwise 1845 * the packet must be dropped (set retval, 1846 * break loops with l=0, done=1) 1847 * 1848 * O_PROBE_STATE and O_CHECK_STATE: these opcodes 1849 * cause a lookup of the state table, and a jump 1850 * to the 'action' part of the parent rule 1851 * if an entry is found, or 1852 * (CHECK_STATE only) a jump to the next rule if 1853 * the entry is not found. 1854 * The result of the lookup is cached so that 1855 * further instances of these opcodes become NOPs. 1856 * The jump to the next rule is done by setting 1857 * l=0, cmdlen=0. 1858 */ 1859 case O_LIMIT: 1860 case O_KEEP_STATE: 1861 if (ipfw_install_state(f, 1862 (ipfw_insn_limit *)cmd, args, tablearg)) { 1863 /* error or limit violation */ 1864 retval = IP_FW_DENY; 1865 l = 0; /* exit inner loop */ 1866 done = 1; /* exit outer loop */ 1867 } 1868 match = 1; 1869 break; 1870 1871 case O_PROBE_STATE: 1872 case O_CHECK_STATE: 1873 /* 1874 * dynamic rules are checked at the first 1875 * keep-state or check-state occurrence, 1876 * with the result being stored in dyn_dir. 1877 * The compiler introduces a PROBE_STATE 1878 * instruction for us when we have a 1879 * KEEP_STATE (because PROBE_STATE needs 1880 * to be run first). 1881 */ 1882 if (dyn_dir == MATCH_UNKNOWN && 1883 (q = ipfw_lookup_dyn_rule(&args->f_id, 1884 &dyn_dir, proto == IPPROTO_TCP ? 1885 TCP(ulp) : NULL)) 1886 != NULL) { 1887 /* 1888 * Found dynamic entry, update stats 1889 * and jump to the 'action' part of 1890 * the parent rule by setting 1891 * f, cmd, l and clearing cmdlen. 1892 */ 1893 q->pcnt++; 1894 q->bcnt += pktlen; 1895 /* XXX we would like to have f_pos 1896 * readily accessible in the dynamic 1897 * rule, instead of having to 1898 * lookup q->rule. 1899 */ 1900 f = q->rule; 1901 f_pos = ipfw_find_rule(chain, 1902 f->rulenum, f->id); 1903 cmd = ACTION_PTR(f); 1904 l = f->cmd_len - f->act_ofs; 1905 ipfw_dyn_unlock(); 1906 cmdlen = 0; 1907 match = 1; 1908 break; 1909 } 1910 /* 1911 * Dynamic entry not found. If CHECK_STATE, 1912 * skip to next rule, if PROBE_STATE just 1913 * ignore and continue with next opcode. 1914 */ 1915 if (cmd->opcode == O_CHECK_STATE) 1916 l = 0; /* exit inner loop */ 1917 match = 1; 1918 break; 1919 1920 case O_ACCEPT: 1921 retval = 0; /* accept */ 1922 l = 0; /* exit inner loop */ 1923 done = 1; /* exit outer loop */ 1924 break; 1925 1926 case O_PIPE: 1927 case O_QUEUE: 1928 set_match(args, f_pos, chain); 1929 args->cookie = (cmd->arg1 == IP_FW_TABLEARG) ? 1930 tablearg : cmd->arg1; 1931 if (cmd->opcode == O_QUEUE) 1932 args->cookie |= 0x80000000; 1933 retval = IP_FW_DUMMYNET; 1934 l = 0; /* exit inner loop */ 1935 done = 1; /* exit outer loop */ 1936 break; 1937 1938 case O_DIVERT: 1939 case O_TEE: 1940 if (args->eh) /* not on layer 2 */ 1941 break; 1942 /* otherwise this is terminal */ 1943 l = 0; /* exit inner loop */ 1944 done = 1; /* exit outer loop */ 1945 mtag = m_tag_get(PACKET_TAG_DIVERT, 1946 sizeof(struct divert_tag), 1947 M_NOWAIT); 1948 if (mtag == NULL) { 1949 retval = IP_FW_DENY; 1950 } else { 1951 struct divert_tag *dt; 1952 dt = (struct divert_tag *)(mtag+1); 1953 dt->cookie = f->rulenum; 1954 if (cmd->arg1 == IP_FW_TABLEARG) 1955 dt->info = tablearg; 1956 else 1957 dt->info = cmd->arg1; 1958 m_tag_prepend(m, mtag); 1959 retval = (cmd->opcode == O_DIVERT) ? 1960 IP_FW_DIVERT : IP_FW_TEE; 1961 } 1962 break; 1963 1964 case O_COUNT: 1965 f->pcnt++; /* update stats */ 1966 f->bcnt += pktlen; 1967 f->timestamp = time_uptime; 1968 l = 0; /* exit inner loop */ 1969 break; 1970 1971 case O_SKIPTO: 1972 f->pcnt++; /* update stats */ 1973 f->bcnt += pktlen; 1974 f->timestamp = time_uptime; 1975 /* If possible use cached f_pos (in f->next_rule), 1976 * whose version is written in f->next_rule 1977 * (horrible hacks to avoid changing the ABI). 1978 */ 1979 if (cmd->arg1 != IP_FW_TABLEARG && 1980 (uintptr_t)f->x_next == chain->id) { 1981 f_pos = (uintptr_t)f->next_rule; 1982 } else { 1983 int i = (cmd->arg1 == IP_FW_TABLEARG) ? 1984 tablearg : cmd->arg1; 1985 /* make sure we do not jump backward */ 1986 if (i <= f->rulenum) 1987 i = f->rulenum + 1; 1988 f_pos = ipfw_find_rule(chain, i, 0); 1989 /* update the cache */ 1990 if (cmd->arg1 != IP_FW_TABLEARG) { 1991 f->next_rule = 1992 (void *)(uintptr_t)f_pos; 1993 f->x_next = 1994 (void *)(uintptr_t)chain->id; 1995 } 1996 } 1997 /* 1998 * Skip disabled rules, and re-enter 1999 * the inner loop with the correct 2000 * f_pos, f, l and cmd. 2001 * Also clear cmdlen and skip_or 2002 */ 2003 for (; f_pos < chain->n_rules - 1 && 2004 (V_set_disable & 2005 (1 << chain->map[f_pos]->set)); 2006 f_pos++) 2007 ; 2008 /* prepare to enter the inner loop */ 2009 f = chain->map[f_pos]; 2010 l = f->cmd_len; 2011 cmd = f->cmd; 2012 match = 1; 2013 cmdlen = 0; 2014 skip_or = 0; 2015 break; 2016 2017 case O_REJECT: 2018 /* 2019 * Drop the packet and send a reject notice 2020 * if the packet is not ICMP (or is an ICMP 2021 * query), and it is not multicast/broadcast. 2022 */ 2023 if (hlen > 0 && is_ipv4 && offset == 0 && 2024 (proto != IPPROTO_ICMP || 2025 is_icmp_query(ICMP(ulp))) && 2026 !(m->m_flags & (M_BCAST|M_MCAST)) && 2027 !IN_MULTICAST(ntohl(dst_ip.s_addr))) { 2028 send_reject(args, cmd->arg1, iplen, ip); 2029 m = args->m; 2030 } 2031 /* FALLTHROUGH */ 2032#ifdef INET6 2033 case O_UNREACH6: 2034 if (hlen > 0 && is_ipv6 && 2035 ((offset & IP6F_OFF_MASK) == 0) && 2036 (proto != IPPROTO_ICMPV6 || 2037 (is_icmp6_query(args->f_id.flags) == 1)) && 2038 !(m->m_flags & (M_BCAST|M_MCAST)) && 2039 !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) { 2040 send_reject6( 2041 args, cmd->arg1, hlen, 2042 (struct ip6_hdr *)ip); 2043 m = args->m; 2044 } 2045 /* FALLTHROUGH */ 2046#endif 2047 case O_DENY: 2048 retval = IP_FW_DENY; 2049 l = 0; /* exit inner loop */ 2050 done = 1; /* exit outer loop */ 2051 break; 2052 2053 case O_FORWARD_IP: 2054 if (args->eh) /* not valid on layer2 pkts */ 2055 break; 2056 if (!q || dyn_dir == MATCH_FORWARD) { 2057 struct sockaddr_in *sa; 2058 sa = &(((ipfw_insn_sa *)cmd)->sa); 2059 if (sa->sin_addr.s_addr == INADDR_ANY) { 2060 bcopy(sa, &args->hopstore, 2061 sizeof(*sa)); 2062 args->hopstore.sin_addr.s_addr = 2063 htonl(tablearg); 2064 args->next_hop = &args->hopstore; 2065 } else { 2066 args->next_hop = sa; 2067 } 2068 } 2069 retval = IP_FW_PASS; 2070 l = 0; /* exit inner loop */ 2071 done = 1; /* exit outer loop */ 2072 break; 2073 2074 case O_NETGRAPH: 2075 case O_NGTEE: 2076 set_match(args, f_pos, chain); 2077 args->cookie = (cmd->arg1 == IP_FW_TABLEARG) ? 2078 tablearg : cmd->arg1; 2079 retval = (cmd->opcode == O_NETGRAPH) ? 2080 IP_FW_NETGRAPH : IP_FW_NGTEE; 2081 l = 0; /* exit inner loop */ 2082 done = 1; /* exit outer loop */ 2083 break; 2084 2085 case O_SETFIB: 2086 f->pcnt++; /* update stats */ 2087 f->bcnt += pktlen; 2088 f->timestamp = time_uptime; 2089 M_SETFIB(m, cmd->arg1); 2090 args->f_id.fib = cmd->arg1; 2091 l = 0; /* exit inner loop */ 2092 break; 2093 2094 case O_NAT: 2095 if (!IPFW_NAT_LOADED) { 2096 retval = IP_FW_DENY; 2097 } else { 2098 struct cfg_nat *t; 2099 int nat_id; 2100 2101 set_match(args, f_pos, chain); 2102 t = ((ipfw_insn_nat *)cmd)->nat; 2103 if (t == NULL) { 2104 nat_id = (cmd->arg1 == IP_FW_TABLEARG) ? 2105 tablearg : cmd->arg1; 2106 t = (*lookup_nat_ptr)(&chain->nat, nat_id); 2107 2108 if (t == NULL) { 2109 retval = IP_FW_DENY; 2110 l = 0; /* exit inner loop */ 2111 done = 1; /* exit outer loop */ 2112 break; 2113 } 2114 if (cmd->arg1 != IP_FW_TABLEARG) 2115 ((ipfw_insn_nat *)cmd)->nat = t; 2116 } 2117 retval = ipfw_nat_ptr(args, t, m); 2118 } 2119 l = 0; /* exit inner loop */ 2120 done = 1; /* exit outer loop */ 2121 break; 2122 2123 case O_REASS: { 2124 int ip_off; 2125 2126 f->pcnt++; 2127 f->bcnt += pktlen; 2128 l = 0; /* in any case exit inner loop */ 2129 2130#ifndef HAVE_NET_IPLEN 2131 if (args->eh == NULL) 2132 ip_off = ip->ip_off; 2133 else 2134#endif /* !HAVE_NET_IPLEN */ 2135 ip_off = ntohs(ip->ip_off); 2136 2137 /* if not fragmented, go to next rule */ 2138 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0) 2139 break; 2140 /* 2141 * ip_reass() expects len & off in host 2142 * byte order: fix them in case we come 2143 * from layer2. 2144 */ 2145 if (args->eh != NULL) { 2146 SET_HOST_IPLEN(ip); 2147 } 2148 2149 args->m = m = ip_reass(m); 2150 2151 /* 2152 * IP header checksum fixup after 2153 * reassembly and leave header 2154 * in network byte order. 2155 */ 2156 if (m == NULL) { /* fragment got swallowed */ 2157 retval = IP_FW_DENY; 2158 } else { /* good, packet complete */ 2159 int hlen; 2160 2161 ip = mtod(m, struct ip *); 2162 hlen = ip->ip_hl << 2; 2163 /* revert len. & off to net format if needed */ 2164 if (args->eh != NULL) { 2165 SET_NET_IPLEN(ip); 2166 } 2167 ip->ip_sum = 0; 2168 if (hlen == sizeof(struct ip)) 2169 ip->ip_sum = in_cksum_hdr(ip); 2170 else 2171 ip->ip_sum = in_cksum(m, hlen); 2172 retval = IP_FW_REASS; 2173 set_match(args, f_pos, chain); 2174 } 2175 done = 1; /* exit outer loop */ 2176 break; 2177 } 2178 2179 default: 2180 panic("-- unknown opcode %d\n", cmd->opcode); 2181 } /* end of switch() on opcodes */ 2182 /* 2183 * if we get here with l=0, then match is irrelevant. 2184 */ 2185 2186 if (cmd->len & F_NOT) 2187 match = !match; 2188 2189 if (match) { 2190 if (cmd->len & F_OR) 2191 skip_or = 1; 2192 } else { 2193 if (!(cmd->len & F_OR)) /* not an OR block, */ 2194 break; /* try next rule */ 2195 } 2196 2197 } /* end of inner loop, scan opcodes */ 2198 2199 if (done) 2200 break; 2201 2202/* next_rule:; */ /* try next rule */ 2203 2204 } /* end of outer for, scan rules */ 2205 2206 if (done) { 2207 struct ip_fw *rule = chain->map[f_pos]; 2208 /* Update statistics */ 2209 rule->pcnt++; 2210 rule->bcnt += pktlen; 2211 rule->timestamp = time_uptime; 2212 } else { 2213 retval = IP_FW_DENY; 2214 printf("ipfw: ouch!, skip past end of rules, denying packet\n"); 2215 } 2216 IPFW_RUNLOCK(chain); 2217 if (ucred_cache != NULL) 2218 crfree(ucred_cache); 2219 return (retval); 2220 2221pullup_failed: 2222 if (V_fw_verbose) 2223 printf("ipfw: pullup failed\n"); 2224 return (IP_FW_DENY); 2225} 2226 2227/* 2228 * Module and VNET glue 2229 */ 2230 2231/* 2232 * Stuff that must be initialised only on boot or module load 2233 */ 2234static int 2235ipfw_init(void) 2236{ 2237 int error = 0; 2238 2239 ipfw_dyn_attach(); 2240 /* 2241 * Only print out this stuff the first time around, 2242 * when called from the sysinit code. 2243 */ 2244 printf("ipfw2 " 2245#ifdef INET6 2246 "(+ipv6) " 2247#endif 2248 "initialized, divert %s, nat %s, " 2249 "rule-based forwarding " 2250#ifdef IPFIREWALL_FORWARD 2251 "enabled, " 2252#else 2253 "disabled, " 2254#endif 2255 "default to %s, logging ", 2256#ifdef IPDIVERT 2257 "enabled", 2258#else 2259 "loadable", 2260#endif 2261#ifdef IPFIREWALL_NAT 2262 "enabled", 2263#else 2264 "loadable", 2265#endif 2266 default_to_accept ? "accept" : "deny"); 2267 2268 /* 2269 * Note: V_xxx variables can be accessed here but the vnet specific 2270 * initializer may not have been called yet for the VIMAGE case. 2271 * Tuneables will have been processed. We will print out values for 2272 * the default vnet. 2273 * XXX This should all be rationalized AFTER 8.0 2274 */ 2275 if (V_fw_verbose == 0) 2276 printf("disabled\n"); 2277 else if (V_verbose_limit == 0) 2278 printf("unlimited\n"); 2279 else 2280 printf("limited to %d packets/entry by default\n", 2281 V_verbose_limit); 2282 2283 ipfw_log_bpf(1); /* init */ 2284 return (error); 2285} 2286 2287/* 2288 * Called for the removal of the last instance only on module unload. 2289 */ 2290static void 2291ipfw_destroy(void) 2292{ 2293 2294 ipfw_log_bpf(0); /* uninit */ 2295 ipfw_dyn_detach(); 2296 printf("IP firewall unloaded\n"); 2297} 2298 2299/* 2300 * Stuff that must be initialized for every instance 2301 * (including the first of course). 2302 */ 2303static int 2304vnet_ipfw_init(const void *unused) 2305{ 2306 int error; 2307 struct ip_fw *rule = NULL; 2308 struct ip_fw_chain *chain; 2309 2310 chain = &V_layer3_chain; 2311 2312 /* First set up some values that are compile time options */ 2313 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ 2314 V_fw_deny_unknown_exthdrs = 1; 2315#ifdef IPFIREWALL_VERBOSE 2316 V_fw_verbose = 1; 2317#endif 2318#ifdef IPFIREWALL_VERBOSE_LIMIT 2319 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT; 2320#endif 2321#ifdef IPFIREWALL_NAT 2322 LIST_INIT(&chain->nat); 2323#endif 2324 2325 /* insert the default rule and create the initial map */ 2326 chain->n_rules = 1; 2327 chain->static_len = sizeof(struct ip_fw); 2328 chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_NOWAIT | M_ZERO); 2329 if (chain->map) 2330 rule = malloc(chain->static_len, M_IPFW, M_NOWAIT | M_ZERO); 2331 if (rule == NULL) { 2332 if (chain->map) 2333 free(chain->map, M_IPFW); 2334 printf("ipfw2: ENOSPC initializing default rule " 2335 "(support disabled)\n"); 2336 return (ENOSPC); 2337 } 2338 error = ipfw_init_tables(chain); 2339 if (error) { 2340 panic("init_tables"); /* XXX Marko fix this ! */ 2341 } 2342 2343 /* fill and insert the default rule */ 2344 rule->act_ofs = 0; 2345 rule->rulenum = IPFW_DEFAULT_RULE; 2346 rule->cmd_len = 1; 2347 rule->set = RESVD_SET; 2348 rule->cmd[0].len = 1; 2349 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY; 2350 chain->rules = chain->default_rule = chain->map[0] = rule; 2351 chain->id = rule->id = 1; 2352 2353 IPFW_LOCK_INIT(chain); 2354 ipfw_dyn_init(); 2355 2356 /* First set up some values that are compile time options */ 2357 V_ipfw_vnet_ready = 1; /* Open for business */ 2358 2359 /* 2360 * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr) 2361 * and pfil hooks for ipv4 and ipv6. Even if the latter two fail 2362 * we still keep the module alive because the sockopt and 2363 * layer2 paths are still useful. 2364 * ipfw[6]_hook return 0 on success, ENOENT on failure, 2365 * so we can ignore the exact return value and just set a flag. 2366 * 2367 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so 2368 * changes in the underlying (per-vnet) variables trigger 2369 * immediate hook()/unhook() calls. 2370 * In layer2 we have the same behaviour, except that V_ether_ipfw 2371 * is checked on each packet because there are no pfil hooks. 2372 */ 2373 V_ip_fw_ctl_ptr = ipfw_ctl; 2374 V_ip_fw_chk_ptr = ipfw_chk; 2375 error = ipfw_attach_hooks(1); 2376 return (error); 2377} 2378 2379/* 2380 * Called for the removal of each instance. 2381 */ 2382static int 2383vnet_ipfw_uninit(const void *unused) 2384{ 2385 struct ip_fw *reap, *rule; 2386 struct ip_fw_chain *chain = &V_layer3_chain; 2387 int i; 2388 2389 V_ipfw_vnet_ready = 0; /* tell new callers to go away */ 2390 /* 2391 * disconnect from ipv4, ipv6, layer2 and sockopt. 2392 * Then grab, release and grab again the WLOCK so we make 2393 * sure the update is propagated and nobody will be in. 2394 */ 2395 (void)ipfw_attach_hooks(0 /* detach */); 2396 V_ip_fw_chk_ptr = NULL; 2397 V_ip_fw_ctl_ptr = NULL; 2398 IPFW_UH_WLOCK(chain); 2399 IPFW_UH_WUNLOCK(chain); 2400 IPFW_UH_WLOCK(chain); 2401 2402 IPFW_WLOCK(chain); 2403 IPFW_WUNLOCK(chain); 2404 IPFW_WLOCK(chain); 2405 2406 ipfw_dyn_uninit(0); /* run the callout_drain */ 2407 ipfw_flush_tables(chain); 2408 reap = NULL; 2409 for (i = 0; i < chain->n_rules; i++) { 2410 rule = chain->map[i]; 2411 rule->x_next = reap; 2412 reap = rule; 2413 } 2414 if (chain->map) 2415 free(chain->map, M_IPFW); 2416 IPFW_WUNLOCK(chain); 2417 IPFW_UH_WUNLOCK(chain); 2418 if (reap != NULL) 2419 ipfw_reap_rules(reap); 2420 IPFW_LOCK_DESTROY(chain); 2421 ipfw_dyn_uninit(1); /* free the remaining parts */ 2422 return 0; 2423} 2424 2425/* 2426 * Module event handler. 2427 * In general we have the choice of handling most of these events by the 2428 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to 2429 * use the SYSINIT handlers as they are more capable of expressing the 2430 * flow of control during module and vnet operations, so this is just 2431 * a skeleton. Note there is no SYSINIT equivalent of the module 2432 * SHUTDOWN handler, but we don't have anything to do in that case anyhow. 2433 */ 2434static int 2435ipfw_modevent(module_t mod, int type, void *unused) 2436{ 2437 int err = 0; 2438 2439 switch (type) { 2440 case MOD_LOAD: 2441 /* Called once at module load or 2442 * system boot if compiled in. */ 2443 break; 2444 case MOD_QUIESCE: 2445 /* Called before unload. May veto unloading. */ 2446 break; 2447 case MOD_UNLOAD: 2448 /* Called during unload. */ 2449 break; 2450 case MOD_SHUTDOWN: 2451 /* Called during system shutdown. */ 2452 break; 2453 default: 2454 err = EOPNOTSUPP; 2455 break; 2456 } 2457 return err; 2458} 2459 2460static moduledata_t ipfwmod = { 2461 "ipfw", 2462 ipfw_modevent, 2463 0 2464}; 2465 2466/* Define startup order. */ 2467#define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_IFATTACHDOMAIN 2468#define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */ 2469#define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */ 2470#define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */ 2471 2472DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER); 2473MODULE_VERSION(ipfw, 2); 2474/* should declare some dependencies here */ 2475 2476/* 2477 * Starting up. Done in order after ipfwmod() has been called. 2478 * VNET_SYSINIT is also called for each existing vnet and each new vnet. 2479 */ 2480SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2481 ipfw_init, NULL); 2482VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2483 vnet_ipfw_init, NULL); 2484 2485/* 2486 * Closing up shop. These are done in REVERSE ORDER, but still 2487 * after ipfwmod() has been called. Not called on reboot. 2488 * VNET_SYSUNINIT is also called for each exiting vnet as it exits. 2489 * or when the module is unloaded. 2490 */ 2491SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2492 ipfw_destroy, NULL); 2493VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2494 vnet_ipfw_uninit, NULL); 2495/* end of file */ 2496