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