ip_fw2.c revision 145565
1/*- 2 * Copyright (c) 2002 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 * $FreeBSD: head/sys/netinet/ip_fw2.c 145565 2005-04-26 18:10:21Z brooks $ 26 */ 27 28#define DEB(x) 29#define DDB(x) x 30 31/* 32 * Implement IP packet firewall (new version) 33 */ 34 35#if !defined(KLD_MODULE) 36#include "opt_ipfw.h" 37#include "opt_ipdn.h" 38#include "opt_inet.h" 39#include "opt_inet6.h" 40#include "opt_ipsec.h" 41#ifndef INET 42#error IPFIREWALL requires INET. 43#endif /* INET */ 44#endif 45 46#define IPFW2 1 47#if IPFW2 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/condvar.h> 51#include <sys/malloc.h> 52#include <sys/mbuf.h> 53#include <sys/kernel.h> 54#include <sys/jail.h> 55#include <sys/module.h> 56#include <sys/proc.h> 57#include <sys/socket.h> 58#include <sys/socketvar.h> 59#include <sys/sysctl.h> 60#include <sys/syslog.h> 61#include <sys/ucred.h> 62#include <net/if.h> 63#include <net/radix.h> 64#include <net/route.h> 65#include <netinet/in.h> 66#include <netinet/in_systm.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/ip_divert.h> 74#include <netinet/ip_dummynet.h> 75#include <netinet/tcp.h> 76#include <netinet/tcp_timer.h> 77#include <netinet/tcp_var.h> 78#include <netinet/tcpip.h> 79#include <netinet/udp.h> 80#include <netinet/udp_var.h> 81 82#include <netgraph/ng_ipfw.h> 83 84#include <altq/if_altq.h> 85 86#ifdef IPSEC 87#include <netinet6/ipsec.h> 88#endif 89 90#include <netinet/ip6.h> 91#include <netinet/icmp6.h> 92 93#include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */ 94 95#include <machine/in_cksum.h> /* XXX for in_cksum */ 96 97/* 98 * set_disable contains one bit per set value (0..31). 99 * If the bit is set, all rules with the corresponding set 100 * are disabled. Set RESVD_SET(31) is reserved for the default rule 101 * and rules that are not deleted by the flush command, 102 * and CANNOT be disabled. 103 * Rules in set RESVD_SET can only be deleted explicitly. 104 */ 105static u_int32_t set_disable; 106 107static int fw_verbose; 108static int verbose_limit; 109 110static struct callout ipfw_timeout; 111static uma_zone_t ipfw_dyn_rule_zone; 112#define IPFW_DEFAULT_RULE 65535 113 114/* 115 * Data structure to cache our ucred related 116 * information. This structure only gets used if 117 * the user specified UID/GID based constraints in 118 * a firewall rule. 119 */ 120struct ip_fw_ugid { 121 gid_t fw_groups[NGROUPS]; 122 int fw_ngroups; 123 uid_t fw_uid; 124 int fw_prid; 125}; 126 127struct ip_fw_chain { 128 struct ip_fw *rules; /* list of rules */ 129 struct ip_fw *reap; /* list of rules to reap */ 130 struct mtx mtx; /* lock guarding rule list */ 131 int busy_count; /* busy count for rw locks */ 132 int want_write; 133 struct cv cv; 134}; 135#define IPFW_LOCK_INIT(_chain) \ 136 mtx_init(&(_chain)->mtx, "IPFW static rules", NULL, \ 137 MTX_DEF | MTX_RECURSE) 138#define IPFW_LOCK_DESTROY(_chain) mtx_destroy(&(_chain)->mtx) 139#define IPFW_WLOCK_ASSERT(_chain) do { \ 140 mtx_assert(&(_chain)->mtx, MA_OWNED); \ 141 NET_ASSERT_GIANT(); \ 142} while (0) 143 144static __inline void 145IPFW_RLOCK(struct ip_fw_chain *chain) 146{ 147 mtx_lock(&chain->mtx); 148 chain->busy_count++; 149 mtx_unlock(&chain->mtx); 150} 151 152static __inline void 153IPFW_RUNLOCK(struct ip_fw_chain *chain) 154{ 155 mtx_lock(&chain->mtx); 156 chain->busy_count--; 157 if (chain->busy_count == 0 && chain->want_write) 158 cv_signal(&chain->cv); 159 mtx_unlock(&chain->mtx); 160} 161 162static __inline void 163IPFW_WLOCK(struct ip_fw_chain *chain) 164{ 165 mtx_lock(&chain->mtx); 166 chain->want_write++; 167 while (chain->busy_count > 0) 168 cv_wait(&chain->cv, &chain->mtx); 169} 170 171static __inline void 172IPFW_WUNLOCK(struct ip_fw_chain *chain) 173{ 174 chain->want_write--; 175 cv_signal(&chain->cv); 176 mtx_unlock(&chain->mtx); 177} 178 179/* 180 * list of rules for layer 3 181 */ 182static struct ip_fw_chain layer3_chain; 183 184MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's"); 185MALLOC_DEFINE(M_IPFW_TBL, "ipfw_tbl", "IpFw tables"); 186 187struct table_entry { 188 struct radix_node rn[2]; 189 struct sockaddr_in addr, mask; 190 u_int32_t value; 191}; 192 193#define IPFW_TABLES_MAX 128 194static struct { 195 struct radix_node_head *rnh; 196 int modified; 197} ipfw_tables[IPFW_TABLES_MAX]; 198 199static int fw_debug = 1; 200static int autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ 201 202#ifdef SYSCTL_NODE 203SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 204SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, enable, 205 CTLFLAG_RW | CTLFLAG_SECURE3, 206 &fw_enable, 0, "Enable ipfw"); 207SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLFLAG_RW, 208 &autoinc_step, 0, "Rule number autincrement step"); 209SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass, 210 CTLFLAG_RW | CTLFLAG_SECURE3, 211 &fw_one_pass, 0, 212 "Only do a single pass through ipfw when using dummynet(4)"); 213SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW, 214 &fw_debug, 0, "Enable printing of debug ip_fw statements"); 215SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, 216 CTLFLAG_RW | CTLFLAG_SECURE3, 217 &fw_verbose, 0, "Log matches to ipfw rules"); 218SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, 219 &verbose_limit, 0, "Set upper limit of matches of ipfw rules logged"); 220 221/* 222 * Description of dynamic rules. 223 * 224 * Dynamic rules are stored in lists accessed through a hash table 225 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can 226 * be modified through the sysctl variable dyn_buckets which is 227 * updated when the table becomes empty. 228 * 229 * XXX currently there is only one list, ipfw_dyn. 230 * 231 * When a packet is received, its address fields are first masked 232 * with the mask defined for the rule, then hashed, then matched 233 * against the entries in the corresponding list. 234 * Dynamic rules can be used for different purposes: 235 * + stateful rules; 236 * + enforcing limits on the number of sessions; 237 * + in-kernel NAT (not implemented yet) 238 * 239 * The lifetime of dynamic rules is regulated by dyn_*_lifetime, 240 * measured in seconds and depending on the flags. 241 * 242 * The total number of dynamic rules is stored in dyn_count. 243 * The max number of dynamic rules is dyn_max. When we reach 244 * the maximum number of rules we do not create anymore. This is 245 * done to avoid consuming too much memory, but also too much 246 * time when searching on each packet (ideally, we should try instead 247 * to put a limit on the length of the list on each bucket...). 248 * 249 * Each dynamic rule holds a pointer to the parent ipfw rule so 250 * we know what action to perform. Dynamic rules are removed when 251 * the parent rule is deleted. XXX we should make them survive. 252 * 253 * There are some limitations with dynamic rules -- we do not 254 * obey the 'randomized match', and we do not do multiple 255 * passes through the firewall. XXX check the latter!!! 256 */ 257static ipfw_dyn_rule **ipfw_dyn_v = NULL; 258static u_int32_t dyn_buckets = 256; /* must be power of 2 */ 259static u_int32_t curr_dyn_buckets = 256; /* must be power of 2 */ 260 261static struct mtx ipfw_dyn_mtx; /* mutex guarding dynamic rules */ 262#define IPFW_DYN_LOCK_INIT() \ 263 mtx_init(&ipfw_dyn_mtx, "IPFW dynamic rules", NULL, MTX_DEF) 264#define IPFW_DYN_LOCK_DESTROY() mtx_destroy(&ipfw_dyn_mtx) 265#define IPFW_DYN_LOCK() mtx_lock(&ipfw_dyn_mtx) 266#define IPFW_DYN_UNLOCK() mtx_unlock(&ipfw_dyn_mtx) 267#define IPFW_DYN_LOCK_ASSERT() mtx_assert(&ipfw_dyn_mtx, MA_OWNED) 268 269/* 270 * Timeouts for various events in handing dynamic rules. 271 */ 272static u_int32_t dyn_ack_lifetime = 300; 273static u_int32_t dyn_syn_lifetime = 20; 274static u_int32_t dyn_fin_lifetime = 1; 275static u_int32_t dyn_rst_lifetime = 1; 276static u_int32_t dyn_udp_lifetime = 10; 277static u_int32_t dyn_short_lifetime = 5; 278 279/* 280 * Keepalives are sent if dyn_keepalive is set. They are sent every 281 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval 282 * seconds of lifetime of a rule. 283 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower 284 * than dyn_keepalive_period. 285 */ 286 287static u_int32_t dyn_keepalive_interval = 20; 288static u_int32_t dyn_keepalive_period = 5; 289static u_int32_t dyn_keepalive = 1; /* do send keepalives */ 290 291static u_int32_t static_count; /* # of static rules */ 292static u_int32_t static_len; /* size in bytes of static rules */ 293static u_int32_t dyn_count; /* # of dynamic rules */ 294static u_int32_t dyn_max = 4096; /* max # of dynamic rules */ 295 296SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLFLAG_RW, 297 &dyn_buckets, 0, "Number of dyn. buckets"); 298SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD, 299 &curr_dyn_buckets, 0, "Current Number of dyn. buckets"); 300SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD, 301 &dyn_count, 0, "Number of dyn. rules"); 302SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW, 303 &dyn_max, 0, "Max number of dyn. rules"); 304SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD, 305 &static_count, 0, "Number of static rules"); 306SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW, 307 &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks"); 308SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW, 309 &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn"); 310SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime, CTLFLAG_RW, 311 &dyn_fin_lifetime, 0, "Lifetime of dyn. rules for fin"); 312SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime, CTLFLAG_RW, 313 &dyn_rst_lifetime, 0, "Lifetime of dyn. rules for rst"); 314SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW, 315 &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP"); 316SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW, 317 &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations"); 318SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, CTLFLAG_RW, 319 &dyn_keepalive, 0, "Enable keepalives for dyn. rules"); 320 321#endif /* SYSCTL_NODE */ 322 323 324/* 325 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 326 * Other macros just cast void * into the appropriate type 327 */ 328#define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 329#define TCP(p) ((struct tcphdr *)(p)) 330#define UDP(p) ((struct udphdr *)(p)) 331#define ICMP(p) ((struct icmphdr *)(p)) 332#define ICMP6(p) ((struct icmp6_hdr *)(p)) 333 334static __inline int 335icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd) 336{ 337 int type = icmp->icmp_type; 338 339 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) ); 340} 341 342#define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \ 343 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) ) 344 345static int 346is_icmp_query(struct icmphdr *icmp) 347{ 348 int type = icmp->icmp_type; 349 350 return (type <= ICMP_MAXTYPE && (TT & (1<<type)) ); 351} 352#undef TT 353 354/* 355 * The following checks use two arrays of 8 or 16 bits to store the 356 * bits that we want set or clear, respectively. They are in the 357 * low and high half of cmd->arg1 or cmd->d[0]. 358 * 359 * We scan options and store the bits we find set. We succeed if 360 * 361 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear 362 * 363 * The code is sometimes optimized not to store additional variables. 364 */ 365 366static int 367flags_match(ipfw_insn *cmd, u_int8_t bits) 368{ 369 u_char want_clear; 370 bits = ~bits; 371 372 if ( ((cmd->arg1 & 0xff) & bits) != 0) 373 return 0; /* some bits we want set were clear */ 374 want_clear = (cmd->arg1 >> 8) & 0xff; 375 if ( (want_clear & bits) != want_clear) 376 return 0; /* some bits we want clear were set */ 377 return 1; 378} 379 380static int 381ipopts_match(struct ip *ip, ipfw_insn *cmd) 382{ 383 int optlen, bits = 0; 384 u_char *cp = (u_char *)(ip + 1); 385 int x = (ip->ip_hl << 2) - sizeof (struct ip); 386 387 for (; x > 0; x -= optlen, cp += optlen) { 388 int opt = cp[IPOPT_OPTVAL]; 389 390 if (opt == IPOPT_EOL) 391 break; 392 if (opt == IPOPT_NOP) 393 optlen = 1; 394 else { 395 optlen = cp[IPOPT_OLEN]; 396 if (optlen <= 0 || optlen > x) 397 return 0; /* invalid or truncated */ 398 } 399 switch (opt) { 400 401 default: 402 break; 403 404 case IPOPT_LSRR: 405 bits |= IP_FW_IPOPT_LSRR; 406 break; 407 408 case IPOPT_SSRR: 409 bits |= IP_FW_IPOPT_SSRR; 410 break; 411 412 case IPOPT_RR: 413 bits |= IP_FW_IPOPT_RR; 414 break; 415 416 case IPOPT_TS: 417 bits |= IP_FW_IPOPT_TS; 418 break; 419 } 420 } 421 return (flags_match(cmd, bits)); 422} 423 424static int 425tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd) 426{ 427 int optlen, bits = 0; 428 u_char *cp = (u_char *)(tcp + 1); 429 int x = (tcp->th_off << 2) - sizeof(struct tcphdr); 430 431 for (; x > 0; x -= optlen, cp += optlen) { 432 int opt = cp[0]; 433 if (opt == TCPOPT_EOL) 434 break; 435 if (opt == TCPOPT_NOP) 436 optlen = 1; 437 else { 438 optlen = cp[1]; 439 if (optlen <= 0) 440 break; 441 } 442 443 switch (opt) { 444 445 default: 446 break; 447 448 case TCPOPT_MAXSEG: 449 bits |= IP_FW_TCPOPT_MSS; 450 break; 451 452 case TCPOPT_WINDOW: 453 bits |= IP_FW_TCPOPT_WINDOW; 454 break; 455 456 case TCPOPT_SACK_PERMITTED: 457 case TCPOPT_SACK: 458 bits |= IP_FW_TCPOPT_SACK; 459 break; 460 461 case TCPOPT_TIMESTAMP: 462 bits |= IP_FW_TCPOPT_TS; 463 break; 464 465 } 466 } 467 return (flags_match(cmd, bits)); 468} 469 470static int 471iface_match(struct ifnet *ifp, ipfw_insn_if *cmd) 472{ 473 if (ifp == NULL) /* no iface with this packet, match fails */ 474 return 0; 475 /* Check by name or by IP address */ 476 if (cmd->name[0] != '\0') { /* match by name */ 477 /* Check name */ 478 if (cmd->p.glob) { 479 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0) 480 return(1); 481 } else { 482 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0) 483 return(1); 484 } 485 } else { 486 struct ifaddr *ia; 487 488 /* XXX lock? */ 489 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 490 if (ia->ifa_addr == NULL) 491 continue; 492 if (ia->ifa_addr->sa_family != AF_INET) 493 continue; 494 if (cmd->p.ip.s_addr == ((struct sockaddr_in *) 495 (ia->ifa_addr))->sin_addr.s_addr) 496 return(1); /* match */ 497 } 498 } 499 return(0); /* no match, fail ... */ 500} 501 502/* 503 * The verify_path function checks if a route to the src exists and 504 * if it is reachable via ifp (when provided). 505 * 506 * The 'verrevpath' option checks that the interface that an IP packet 507 * arrives on is the same interface that traffic destined for the 508 * packet's source address would be routed out of. The 'versrcreach' 509 * option just checks that the source address is reachable via any route 510 * (except default) in the routing table. These two are a measure to block 511 * forged packets. This is also commonly known as "anti-spoofing" or Unicast 512 * Reverse Path Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs 513 * is purposely reminiscent of the Cisco IOS command, 514 * 515 * ip verify unicast reverse-path 516 * ip verify unicast source reachable-via any 517 * 518 * which implements the same functionality. But note that syntax is 519 * misleading. The check may be performed on all IP packets whether unicast, 520 * multicast, or broadcast. 521 */ 522static int 523verify_path(struct in_addr src, struct ifnet *ifp) 524{ 525 struct route ro; 526 struct sockaddr_in *dst; 527 528 bzero(&ro, sizeof(ro)); 529 530 dst = (struct sockaddr_in *)&(ro.ro_dst); 531 dst->sin_family = AF_INET; 532 dst->sin_len = sizeof(*dst); 533 dst->sin_addr = src; 534 rtalloc_ign(&ro, RTF_CLONING); 535 536 if (ro.ro_rt == NULL) 537 return 0; 538 539 /* if ifp is provided, check for equality with rtentry */ 540 if (ifp != NULL && ro.ro_rt->rt_ifp != ifp) { 541 RTFREE(ro.ro_rt); 542 return 0; 543 } 544 545 /* if no ifp provided, check if rtentry is not default route */ 546 if (ifp == NULL && 547 satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) { 548 RTFREE(ro.ro_rt); 549 return 0; 550 } 551 552 /* or if this is a blackhole/reject route */ 553 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 554 RTFREE(ro.ro_rt); 555 return 0; 556 } 557 558 /* found valid route */ 559 RTFREE(ro.ro_rt); 560 return 1; 561} 562 563#ifdef INET6 564/* 565 * ipv6 specific rules here... 566 */ 567static __inline int 568icmp6type_match (int type, ipfw_insn_u32 *cmd) 569{ 570 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) ); 571} 572 573static int 574flow6id_match( int curr_flow, ipfw_insn_u32 *cmd ) 575{ 576 int i; 577 for (i=0; i <= cmd->o.arg1; ++i ) 578 if (curr_flow == cmd->d[i] ) 579 return 1; 580 return 0; 581} 582 583/* support for IP6_*_ME opcodes */ 584static int 585search_ip6_addr_net (struct in6_addr * ip6_addr) 586{ 587 struct ifnet *mdc; 588 struct ifaddr *mdc2; 589 struct in6_ifaddr *fdm; 590 struct in6_addr copia; 591 592 TAILQ_FOREACH(mdc, &ifnet, if_link) 593 for (mdc2 = mdc->if_addrlist.tqh_first; mdc2; 594 mdc2 = mdc2->ifa_list.tqe_next) { 595 if (!mdc2->ifa_addr) 596 continue; 597 if (mdc2->ifa_addr->sa_family == AF_INET6) { 598 fdm = (struct in6_ifaddr *)mdc2; 599 copia = fdm->ia_addr.sin6_addr; 600 /* need for leaving scope_id in the sock_addr */ 601 in6_clearscope(&copia); 602 if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) 603 return 1; 604 } 605 } 606 return 0; 607} 608 609static int 610verify_rev_path6(struct in6_addr *src, struct ifnet *ifp) 611{ 612 static struct route_in6 ro; 613 struct sockaddr_in6 *dst; 614 615 dst = (struct sockaddr_in6 * )&(ro.ro_dst); 616 617 if ( !(IN6_ARE_ADDR_EQUAL (src, &dst->sin6_addr) )) { 618 bzero(dst, sizeof(*dst)); 619 dst->sin6_family = AF_INET6; 620 dst->sin6_len = sizeof(*dst); 621 dst->sin6_addr = *src; 622 rtalloc_ign((struct route *)&ro, RTF_CLONING); 623 } 624 if ((ro.ro_rt == NULL) || (ifp == NULL) || 625 (ro.ro_rt->rt_ifp->if_index != ifp->if_index)) 626 return 0; 627 return 1; 628} 629static __inline int 630hash_packet6(struct ipfw_flow_id *id) 631{ 632 u_int32_t i; 633 i= (id->dst_ip6.__u6_addr.__u6_addr32[0]) ^ 634 (id->dst_ip6.__u6_addr.__u6_addr32[1]) ^ 635 (id->dst_ip6.__u6_addr.__u6_addr32[2]) ^ 636 (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^ 637 (id->dst_port) ^ (id->src_port) ^ (id->flow_id6); 638 return i; 639} 640/* end of ipv6 opcodes */ 641 642#endif /* INET6 */ 643 644static u_int64_t norule_counter; /* counter for ipfw_log(NULL...) */ 645 646#define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0 647#define SNP(buf) buf, sizeof(buf) 648 649/* 650 * We enter here when we have a rule with O_LOG. 651 * XXX this function alone takes about 2Kbytes of code! 652 */ 653static void 654ipfw_log(struct ip_fw *f, u_int hlen, struct ether_header *eh, 655 struct mbuf *m, struct ifnet *oif) 656{ 657 char *action; 658 int limit_reached = 0; 659 char action2[40], proto[48], fragment[28]; 660 661 fragment[0] = '\0'; 662 proto[0] = '\0'; 663 664 if (f == NULL) { /* bogus pkt */ 665 if (verbose_limit != 0 && norule_counter >= verbose_limit) 666 return; 667 norule_counter++; 668 if (norule_counter == verbose_limit) 669 limit_reached = verbose_limit; 670 action = "Refuse"; 671 } else { /* O_LOG is the first action, find the real one */ 672 ipfw_insn *cmd = ACTION_PTR(f); 673 ipfw_insn_log *l = (ipfw_insn_log *)cmd; 674 675 if (l->max_log != 0 && l->log_left == 0) 676 return; 677 l->log_left--; 678 if (l->log_left == 0) 679 limit_reached = l->max_log; 680 cmd += F_LEN(cmd); /* point to first action */ 681 if (cmd->opcode == O_ALTQ) { 682 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 683 684 snprintf(SNPARGS(action2, 0), "Altq %d", 685 altq->qid); 686 cmd += F_LEN(cmd); 687 } 688 if (cmd->opcode == O_PROB) 689 cmd += F_LEN(cmd); 690 691 action = action2; 692 switch (cmd->opcode) { 693 case O_DENY: 694 action = "Deny"; 695 break; 696 697 case O_REJECT: 698 if (cmd->arg1==ICMP_REJECT_RST) 699 action = "Reset"; 700 else if (cmd->arg1==ICMP_UNREACH_HOST) 701 action = "Reject"; 702 else 703 snprintf(SNPARGS(action2, 0), "Unreach %d", 704 cmd->arg1); 705 break; 706 707 case O_ACCEPT: 708 action = "Accept"; 709 break; 710 case O_COUNT: 711 action = "Count"; 712 break; 713 case O_DIVERT: 714 snprintf(SNPARGS(action2, 0), "Divert %d", 715 cmd->arg1); 716 break; 717 case O_TEE: 718 snprintf(SNPARGS(action2, 0), "Tee %d", 719 cmd->arg1); 720 break; 721 case O_SKIPTO: 722 snprintf(SNPARGS(action2, 0), "SkipTo %d", 723 cmd->arg1); 724 break; 725 case O_PIPE: 726 snprintf(SNPARGS(action2, 0), "Pipe %d", 727 cmd->arg1); 728 break; 729 case O_QUEUE: 730 snprintf(SNPARGS(action2, 0), "Queue %d", 731 cmd->arg1); 732 break; 733 case O_FORWARD_IP: { 734 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd; 735 int len; 736 737 len = snprintf(SNPARGS(action2, 0), "Forward to %s", 738 inet_ntoa(sa->sa.sin_addr)); 739 if (sa->sa.sin_port) 740 snprintf(SNPARGS(action2, len), ":%d", 741 sa->sa.sin_port); 742 } 743 break; 744 case O_NETGRAPH: 745 snprintf(SNPARGS(action2, 0), "Netgraph %d", 746 cmd->arg1); 747 break; 748 case O_NGTEE: 749 snprintf(SNPARGS(action2, 0), "Ngtee %d", 750 cmd->arg1); 751 break; 752 default: 753 action = "UNKNOWN"; 754 break; 755 } 756 } 757 758 if (hlen == 0) { /* non-ip */ 759 snprintf(SNPARGS(proto, 0), "MAC"); 760 } else { 761 struct ip *ip = mtod(m, struct ip *); 762 /* these three are all aliases to the same thing */ 763 struct icmphdr *const icmp = L3HDR(struct icmphdr, ip); 764 struct tcphdr *const tcp = (struct tcphdr *)icmp; 765 struct udphdr *const udp = (struct udphdr *)icmp; 766 767 int ip_off, offset, ip_len; 768 769 int len; 770 771 if (eh != NULL) { /* layer 2 packets are as on the wire */ 772 ip_off = ntohs(ip->ip_off); 773 ip_len = ntohs(ip->ip_len); 774 } else { 775 ip_off = ip->ip_off; 776 ip_len = ip->ip_len; 777 } 778 offset = ip_off & IP_OFFMASK; 779 switch (ip->ip_p) { 780 case IPPROTO_TCP: 781 len = snprintf(SNPARGS(proto, 0), "TCP %s", 782 inet_ntoa(ip->ip_src)); 783 if (offset == 0) 784 snprintf(SNPARGS(proto, len), ":%d %s:%d", 785 ntohs(tcp->th_sport), 786 inet_ntoa(ip->ip_dst), 787 ntohs(tcp->th_dport)); 788 else 789 snprintf(SNPARGS(proto, len), " %s", 790 inet_ntoa(ip->ip_dst)); 791 break; 792 793 case IPPROTO_UDP: 794 len = snprintf(SNPARGS(proto, 0), "UDP %s", 795 inet_ntoa(ip->ip_src)); 796 if (offset == 0) 797 snprintf(SNPARGS(proto, len), ":%d %s:%d", 798 ntohs(udp->uh_sport), 799 inet_ntoa(ip->ip_dst), 800 ntohs(udp->uh_dport)); 801 else 802 snprintf(SNPARGS(proto, len), " %s", 803 inet_ntoa(ip->ip_dst)); 804 break; 805 806 case IPPROTO_ICMP: 807 if (offset == 0) 808 len = snprintf(SNPARGS(proto, 0), 809 "ICMP:%u.%u ", 810 icmp->icmp_type, icmp->icmp_code); 811 else 812 len = snprintf(SNPARGS(proto, 0), "ICMP "); 813 len += snprintf(SNPARGS(proto, len), "%s", 814 inet_ntoa(ip->ip_src)); 815 snprintf(SNPARGS(proto, len), " %s", 816 inet_ntoa(ip->ip_dst)); 817 break; 818 819 default: 820 len = snprintf(SNPARGS(proto, 0), "P:%d %s", ip->ip_p, 821 inet_ntoa(ip->ip_src)); 822 snprintf(SNPARGS(proto, len), " %s", 823 inet_ntoa(ip->ip_dst)); 824 break; 825 } 826 827 if (ip_off & (IP_MF | IP_OFFMASK)) 828 snprintf(SNPARGS(fragment, 0), " (frag %d:%d@%d%s)", 829 ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2), 830 offset << 3, 831 (ip_off & IP_MF) ? "+" : ""); 832 } 833 if (oif || m->m_pkthdr.rcvif) 834 log(LOG_SECURITY | LOG_INFO, 835 "ipfw: %d %s %s %s via %s%s\n", 836 f ? f->rulenum : -1, 837 action, proto, oif ? "out" : "in", 838 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname, 839 fragment); 840 else 841 log(LOG_SECURITY | LOG_INFO, 842 "ipfw: %d %s %s [no if info]%s\n", 843 f ? f->rulenum : -1, 844 action, proto, fragment); 845 if (limit_reached) 846 log(LOG_SECURITY | LOG_NOTICE, 847 "ipfw: limit %d reached on entry %d\n", 848 limit_reached, f ? f->rulenum : -1); 849} 850 851/* 852 * IMPORTANT: the hash function for dynamic rules must be commutative 853 * in source and destination (ip,port), because rules are bidirectional 854 * and we want to find both in the same bucket. 855 */ 856static __inline int 857hash_packet(struct ipfw_flow_id *id) 858{ 859 u_int32_t i; 860 861#ifdef INET6 862 if (IS_IP6_FLOW_ID(id)) 863 i = hash_packet6(id); 864 else 865#endif /* INET6 */ 866 i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port); 867 i &= (curr_dyn_buckets - 1); 868 return i; 869} 870 871/** 872 * unlink a dynamic rule from a chain. prev is a pointer to 873 * the previous one, q is a pointer to the rule to delete, 874 * head is a pointer to the head of the queue. 875 * Modifies q and potentially also head. 876 */ 877#define UNLINK_DYN_RULE(prev, head, q) { \ 878 ipfw_dyn_rule *old_q = q; \ 879 \ 880 /* remove a refcount to the parent */ \ 881 if (q->dyn_type == O_LIMIT) \ 882 q->parent->count--; \ 883 DEB(printf("ipfw: unlink entry 0x%08x %d -> 0x%08x %d, %d left\n",\ 884 (q->id.src_ip), (q->id.src_port), \ 885 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); ) \ 886 if (prev != NULL) \ 887 prev->next = q = q->next; \ 888 else \ 889 head = q = q->next; \ 890 dyn_count--; \ 891 uma_zfree(ipfw_dyn_rule_zone, old_q); } 892 893#define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0) 894 895/** 896 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL. 897 * 898 * If keep_me == NULL, rules are deleted even if not expired, 899 * otherwise only expired rules are removed. 900 * 901 * The value of the second parameter is also used to point to identify 902 * a rule we absolutely do not want to remove (e.g. because we are 903 * holding a reference to it -- this is the case with O_LIMIT_PARENT 904 * rules). The pointer is only used for comparison, so any non-null 905 * value will do. 906 */ 907static void 908remove_dyn_rule(struct ip_fw *rule, ipfw_dyn_rule *keep_me) 909{ 910 static u_int32_t last_remove = 0; 911 912#define FORCE (keep_me == NULL) 913 914 ipfw_dyn_rule *prev, *q; 915 int i, pass = 0, max_pass = 0; 916 917 IPFW_DYN_LOCK_ASSERT(); 918 919 if (ipfw_dyn_v == NULL || dyn_count == 0) 920 return; 921 /* do not expire more than once per second, it is useless */ 922 if (!FORCE && last_remove == time_second) 923 return; 924 last_remove = time_second; 925 926 /* 927 * because O_LIMIT refer to parent rules, during the first pass only 928 * remove child and mark any pending LIMIT_PARENT, and remove 929 * them in a second pass. 930 */ 931next_pass: 932 for (i = 0 ; i < curr_dyn_buckets ; i++) { 933 for (prev=NULL, q = ipfw_dyn_v[i] ; q ; ) { 934 /* 935 * Logic can become complex here, so we split tests. 936 */ 937 if (q == keep_me) 938 goto next; 939 if (rule != NULL && rule != q->rule) 940 goto next; /* not the one we are looking for */ 941 if (q->dyn_type == O_LIMIT_PARENT) { 942 /* 943 * handle parent in the second pass, 944 * record we need one. 945 */ 946 max_pass = 1; 947 if (pass == 0) 948 goto next; 949 if (FORCE && q->count != 0 ) { 950 /* XXX should not happen! */ 951 printf("ipfw: OUCH! cannot remove rule," 952 " count %d\n", q->count); 953 } 954 } else { 955 if (!FORCE && 956 !TIME_LEQ( q->expire, time_second )) 957 goto next; 958 } 959 if (q->dyn_type != O_LIMIT_PARENT || !q->count) { 960 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q); 961 continue; 962 } 963next: 964 prev=q; 965 q=q->next; 966 } 967 } 968 if (pass++ < max_pass) 969 goto next_pass; 970} 971 972 973/** 974 * lookup a dynamic rule. 975 */ 976static ipfw_dyn_rule * 977lookup_dyn_rule_locked(struct ipfw_flow_id *pkt, int *match_direction, 978 struct tcphdr *tcp) 979{ 980 /* 981 * stateful ipfw extensions. 982 * Lookup into dynamic session queue 983 */ 984#define MATCH_REVERSE 0 985#define MATCH_FORWARD 1 986#define MATCH_NONE 2 987#define MATCH_UNKNOWN 3 988 int i, dir = MATCH_NONE; 989 ipfw_dyn_rule *prev, *q=NULL; 990 991 IPFW_DYN_LOCK_ASSERT(); 992 993 if (ipfw_dyn_v == NULL) 994 goto done; /* not found */ 995 i = hash_packet( pkt ); 996 for (prev=NULL, q = ipfw_dyn_v[i] ; q != NULL ; ) { 997 if (q->dyn_type == O_LIMIT_PARENT && q->count) 998 goto next; 999 if (TIME_LEQ( q->expire, time_second)) { /* expire entry */ 1000 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q); 1001 continue; 1002 } 1003 if (pkt->proto == q->id.proto && 1004 q->dyn_type != O_LIMIT_PARENT) { 1005 if (IS_IP6_FLOW_ID(pkt)) { 1006 if (IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6), 1007 &(q->id.src_ip6)) && 1008 IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6), 1009 &(q->id.dst_ip6)) && 1010 pkt->src_port == q->id.src_port && 1011 pkt->dst_port == q->id.dst_port ) { 1012 dir = MATCH_FORWARD; 1013 break; 1014 } 1015 if (IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6), 1016 &(q->id.dst_ip6)) && 1017 IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6), 1018 &(q->id.src_ip6)) && 1019 pkt->src_port == q->id.dst_port && 1020 pkt->dst_port == q->id.src_port ) { 1021 dir = MATCH_REVERSE; 1022 break; 1023 } 1024 } else { 1025 if (pkt->src_ip == q->id.src_ip && 1026 pkt->dst_ip == q->id.dst_ip && 1027 pkt->src_port == q->id.src_port && 1028 pkt->dst_port == q->id.dst_port ) { 1029 dir = MATCH_FORWARD; 1030 break; 1031 } 1032 if (pkt->src_ip == q->id.dst_ip && 1033 pkt->dst_ip == q->id.src_ip && 1034 pkt->src_port == q->id.dst_port && 1035 pkt->dst_port == q->id.src_port ) { 1036 dir = MATCH_REVERSE; 1037 break; 1038 } 1039 } 1040 } 1041next: 1042 prev = q; 1043 q = q->next; 1044 } 1045 if (q == NULL) 1046 goto done; /* q = NULL, not found */ 1047 1048 if ( prev != NULL) { /* found and not in front */ 1049 prev->next = q->next; 1050 q->next = ipfw_dyn_v[i]; 1051 ipfw_dyn_v[i] = q; 1052 } 1053 if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */ 1054 u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST); 1055 1056#define BOTH_SYN (TH_SYN | (TH_SYN << 8)) 1057#define BOTH_FIN (TH_FIN | (TH_FIN << 8)) 1058 q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8); 1059 switch (q->state) { 1060 case TH_SYN: /* opening */ 1061 q->expire = time_second + dyn_syn_lifetime; 1062 break; 1063 1064 case BOTH_SYN: /* move to established */ 1065 case BOTH_SYN | TH_FIN : /* one side tries to close */ 1066 case BOTH_SYN | (TH_FIN << 8) : 1067 if (tcp) { 1068#define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0) 1069 u_int32_t ack = ntohl(tcp->th_ack); 1070 if (dir == MATCH_FORWARD) { 1071 if (q->ack_fwd == 0 || _SEQ_GE(ack, q->ack_fwd)) 1072 q->ack_fwd = ack; 1073 else { /* ignore out-of-sequence */ 1074 break; 1075 } 1076 } else { 1077 if (q->ack_rev == 0 || _SEQ_GE(ack, q->ack_rev)) 1078 q->ack_rev = ack; 1079 else { /* ignore out-of-sequence */ 1080 break; 1081 } 1082 } 1083 } 1084 q->expire = time_second + dyn_ack_lifetime; 1085 break; 1086 1087 case BOTH_SYN | BOTH_FIN: /* both sides closed */ 1088 if (dyn_fin_lifetime >= dyn_keepalive_period) 1089 dyn_fin_lifetime = dyn_keepalive_period - 1; 1090 q->expire = time_second + dyn_fin_lifetime; 1091 break; 1092 1093 default: 1094#if 0 1095 /* 1096 * reset or some invalid combination, but can also 1097 * occur if we use keep-state the wrong way. 1098 */ 1099 if ( (q->state & ((TH_RST << 8)|TH_RST)) == 0) 1100 printf("invalid state: 0x%x\n", q->state); 1101#endif 1102 if (dyn_rst_lifetime >= dyn_keepalive_period) 1103 dyn_rst_lifetime = dyn_keepalive_period - 1; 1104 q->expire = time_second + dyn_rst_lifetime; 1105 break; 1106 } 1107 } else if (pkt->proto == IPPROTO_UDP) { 1108 q->expire = time_second + dyn_udp_lifetime; 1109 } else { 1110 /* other protocols */ 1111 q->expire = time_second + dyn_short_lifetime; 1112 } 1113done: 1114 if (match_direction) 1115 *match_direction = dir; 1116 return q; 1117} 1118 1119static ipfw_dyn_rule * 1120lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction, 1121 struct tcphdr *tcp) 1122{ 1123 ipfw_dyn_rule *q; 1124 1125 IPFW_DYN_LOCK(); 1126 q = lookup_dyn_rule_locked(pkt, match_direction, tcp); 1127 if (q == NULL) 1128 IPFW_DYN_UNLOCK(); 1129 /* NB: return table locked when q is not NULL */ 1130 return q; 1131} 1132 1133static void 1134realloc_dynamic_table(void) 1135{ 1136 IPFW_DYN_LOCK_ASSERT(); 1137 1138 /* 1139 * Try reallocation, make sure we have a power of 2 and do 1140 * not allow more than 64k entries. In case of overflow, 1141 * default to 1024. 1142 */ 1143 1144 if (dyn_buckets > 65536) 1145 dyn_buckets = 1024; 1146 if ((dyn_buckets & (dyn_buckets-1)) != 0) { /* not a power of 2 */ 1147 dyn_buckets = curr_dyn_buckets; /* reset */ 1148 return; 1149 } 1150 curr_dyn_buckets = dyn_buckets; 1151 if (ipfw_dyn_v != NULL) 1152 free(ipfw_dyn_v, M_IPFW); 1153 for (;;) { 1154 ipfw_dyn_v = malloc(curr_dyn_buckets * sizeof(ipfw_dyn_rule *), 1155 M_IPFW, M_NOWAIT | M_ZERO); 1156 if (ipfw_dyn_v != NULL || curr_dyn_buckets <= 2) 1157 break; 1158 curr_dyn_buckets /= 2; 1159 } 1160} 1161 1162/** 1163 * Install state of type 'type' for a dynamic session. 1164 * The hash table contains two type of rules: 1165 * - regular rules (O_KEEP_STATE) 1166 * - rules for sessions with limited number of sess per user 1167 * (O_LIMIT). When they are created, the parent is 1168 * increased by 1, and decreased on delete. In this case, 1169 * the third parameter is the parent rule and not the chain. 1170 * - "parent" rules for the above (O_LIMIT_PARENT). 1171 */ 1172static ipfw_dyn_rule * 1173add_dyn_rule(struct ipfw_flow_id *id, u_int8_t dyn_type, struct ip_fw *rule) 1174{ 1175 ipfw_dyn_rule *r; 1176 int i; 1177 1178 IPFW_DYN_LOCK_ASSERT(); 1179 1180 if (ipfw_dyn_v == NULL || 1181 (dyn_count == 0 && dyn_buckets != curr_dyn_buckets)) { 1182 realloc_dynamic_table(); 1183 if (ipfw_dyn_v == NULL) 1184 return NULL; /* failed ! */ 1185 } 1186 i = hash_packet(id); 1187 1188 r = uma_zalloc(ipfw_dyn_rule_zone, M_NOWAIT | M_ZERO); 1189 if (r == NULL) { 1190 printf ("ipfw: sorry cannot allocate state\n"); 1191 return NULL; 1192 } 1193 1194 /* increase refcount on parent, and set pointer */ 1195 if (dyn_type == O_LIMIT) { 1196 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule; 1197 if ( parent->dyn_type != O_LIMIT_PARENT) 1198 panic("invalid parent"); 1199 parent->count++; 1200 r->parent = parent; 1201 rule = parent->rule; 1202 } 1203 1204 r->id = *id; 1205 r->expire = time_second + dyn_syn_lifetime; 1206 r->rule = rule; 1207 r->dyn_type = dyn_type; 1208 r->pcnt = r->bcnt = 0; 1209 r->count = 0; 1210 1211 r->bucket = i; 1212 r->next = ipfw_dyn_v[i]; 1213 ipfw_dyn_v[i] = r; 1214 dyn_count++; 1215 DEB(printf("ipfw: add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n", 1216 dyn_type, 1217 (r->id.src_ip), (r->id.src_port), 1218 (r->id.dst_ip), (r->id.dst_port), 1219 dyn_count ); ) 1220 return r; 1221} 1222 1223/** 1224 * lookup dynamic parent rule using pkt and rule as search keys. 1225 * If the lookup fails, then install one. 1226 */ 1227static ipfw_dyn_rule * 1228lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule) 1229{ 1230 ipfw_dyn_rule *q; 1231 int i; 1232 1233 IPFW_DYN_LOCK_ASSERT(); 1234 1235 if (ipfw_dyn_v) { 1236 int is_v6 = IS_IP6_FLOW_ID(pkt); 1237 i = hash_packet( pkt ); 1238 for (q = ipfw_dyn_v[i] ; q != NULL ; q=q->next) 1239 if (q->dyn_type == O_LIMIT_PARENT && 1240 rule== q->rule && 1241 pkt->proto == q->id.proto && 1242 pkt->src_port == q->id.src_port && 1243 pkt->dst_port == q->id.dst_port && 1244 ( 1245 (is_v6 && 1246 IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6), 1247 &(q->id.src_ip6)) && 1248 IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6), 1249 &(q->id.dst_ip6))) || 1250 (!is_v6 && 1251 pkt->src_ip == q->id.src_ip && 1252 pkt->dst_ip == q->id.dst_ip) 1253 ) 1254 ) { 1255 q->expire = time_second + dyn_short_lifetime; 1256 DEB(printf("ipfw: lookup_dyn_parent found 0x%p\n",q);) 1257 return q; 1258 } 1259 } 1260 return add_dyn_rule(pkt, O_LIMIT_PARENT, rule); 1261} 1262 1263/** 1264 * Install dynamic state for rule type cmd->o.opcode 1265 * 1266 * Returns 1 (failure) if state is not installed because of errors or because 1267 * session limitations are enforced. 1268 */ 1269static int 1270install_state(struct ip_fw *rule, ipfw_insn_limit *cmd, 1271 struct ip_fw_args *args) 1272{ 1273 static int last_log; 1274 1275 ipfw_dyn_rule *q; 1276 1277 DEB(printf("ipfw: install state type %d 0x%08x %u -> 0x%08x %u\n", 1278 cmd->o.opcode, 1279 (args->f_id.src_ip), (args->f_id.src_port), 1280 (args->f_id.dst_ip), (args->f_id.dst_port) );) 1281 1282 IPFW_DYN_LOCK(); 1283 1284 q = lookup_dyn_rule_locked(&args->f_id, NULL, NULL); 1285 1286 if (q != NULL) { /* should never occur */ 1287 if (last_log != time_second) { 1288 last_log = time_second; 1289 printf("ipfw: install_state: entry already present, done\n"); 1290 } 1291 IPFW_DYN_UNLOCK(); 1292 return 0; 1293 } 1294 1295 if (dyn_count >= dyn_max) 1296 /* 1297 * Run out of slots, try to remove any expired rule. 1298 */ 1299 remove_dyn_rule(NULL, (ipfw_dyn_rule *)1); 1300 1301 if (dyn_count >= dyn_max) { 1302 if (last_log != time_second) { 1303 last_log = time_second; 1304 printf("ipfw: install_state: Too many dynamic rules\n"); 1305 } 1306 IPFW_DYN_UNLOCK(); 1307 return 1; /* cannot install, notify caller */ 1308 } 1309 1310 switch (cmd->o.opcode) { 1311 case O_KEEP_STATE: /* bidir rule */ 1312 add_dyn_rule(&args->f_id, O_KEEP_STATE, rule); 1313 break; 1314 1315 case O_LIMIT: /* limit number of sessions */ 1316 { 1317 u_int16_t limit_mask = cmd->limit_mask; 1318 struct ipfw_flow_id id; 1319 ipfw_dyn_rule *parent; 1320 1321 DEB(printf("ipfw: installing dyn-limit rule %d\n", 1322 cmd->conn_limit);) 1323 1324 id.dst_ip = id.src_ip = 0; 1325 id.dst_port = id.src_port = 0; 1326 id.proto = args->f_id.proto; 1327 1328 if (IS_IP6_FLOW_ID (&(args->f_id))) { 1329 if (limit_mask & DYN_SRC_ADDR) 1330 id.src_ip6 = args->f_id.src_ip6; 1331 if (limit_mask & DYN_DST_ADDR) 1332 id.dst_ip6 = args->f_id.dst_ip6; 1333 } else { 1334 if (limit_mask & DYN_SRC_ADDR) 1335 id.src_ip = args->f_id.src_ip; 1336 if (limit_mask & DYN_DST_ADDR) 1337 id.dst_ip = args->f_id.dst_ip; 1338 } 1339 if (limit_mask & DYN_SRC_PORT) 1340 id.src_port = args->f_id.src_port; 1341 if (limit_mask & DYN_DST_PORT) 1342 id.dst_port = args->f_id.dst_port; 1343 parent = lookup_dyn_parent(&id, rule); 1344 if (parent == NULL) { 1345 printf("ipfw: add parent failed\n"); 1346 return 1; 1347 } 1348 if (parent->count >= cmd->conn_limit) { 1349 /* 1350 * See if we can remove some expired rule. 1351 */ 1352 remove_dyn_rule(rule, parent); 1353 if (parent->count >= cmd->conn_limit) { 1354 if (fw_verbose && last_log != time_second) { 1355 last_log = time_second; 1356 log(LOG_SECURITY | LOG_DEBUG, 1357 "drop session, too many entries\n"); 1358 } 1359 IPFW_DYN_UNLOCK(); 1360 return 1; 1361 } 1362 } 1363 add_dyn_rule(&args->f_id, O_LIMIT, (struct ip_fw *)parent); 1364 } 1365 break; 1366 default: 1367 printf("ipfw: unknown dynamic rule type %u\n", cmd->o.opcode); 1368 IPFW_DYN_UNLOCK(); 1369 return 1; 1370 } 1371 lookup_dyn_rule_locked(&args->f_id, NULL, NULL); /* XXX just set lifetime */ 1372 IPFW_DYN_UNLOCK(); 1373 return 0; 1374} 1375 1376/* 1377 * Transmit a TCP packet, containing either a RST or a keepalive. 1378 * When flags & TH_RST, we are sending a RST packet, because of a 1379 * "reset" action matched the packet. 1380 * Otherwise we are sending a keepalive, and flags & TH_ 1381 */ 1382static void 1383send_pkt(struct ipfw_flow_id *id, u_int32_t seq, u_int32_t ack, int flags) 1384{ 1385 struct mbuf *m; 1386 struct ip *ip; 1387 struct tcphdr *tcp; 1388 1389 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1390 if (m == 0) 1391 return; 1392 m->m_pkthdr.rcvif = (struct ifnet *)0; 1393 m->m_pkthdr.len = m->m_len = sizeof(struct ip) + sizeof(struct tcphdr); 1394 m->m_data += max_linkhdr; 1395 1396 ip = mtod(m, struct ip *); 1397 bzero(ip, m->m_len); 1398 tcp = (struct tcphdr *)(ip + 1); /* no IP options */ 1399 ip->ip_p = IPPROTO_TCP; 1400 tcp->th_off = 5; 1401 /* 1402 * Assume we are sending a RST (or a keepalive in the reverse 1403 * direction), swap src and destination addresses and ports. 1404 */ 1405 ip->ip_src.s_addr = htonl(id->dst_ip); 1406 ip->ip_dst.s_addr = htonl(id->src_ip); 1407 tcp->th_sport = htons(id->dst_port); 1408 tcp->th_dport = htons(id->src_port); 1409 if (flags & TH_RST) { /* we are sending a RST */ 1410 if (flags & TH_ACK) { 1411 tcp->th_seq = htonl(ack); 1412 tcp->th_ack = htonl(0); 1413 tcp->th_flags = TH_RST; 1414 } else { 1415 if (flags & TH_SYN) 1416 seq++; 1417 tcp->th_seq = htonl(0); 1418 tcp->th_ack = htonl(seq); 1419 tcp->th_flags = TH_RST | TH_ACK; 1420 } 1421 } else { 1422 /* 1423 * We are sending a keepalive. flags & TH_SYN determines 1424 * the direction, forward if set, reverse if clear. 1425 * NOTE: seq and ack are always assumed to be correct 1426 * as set by the caller. This may be confusing... 1427 */ 1428 if (flags & TH_SYN) { 1429 /* 1430 * we have to rewrite the correct addresses! 1431 */ 1432 ip->ip_dst.s_addr = htonl(id->dst_ip); 1433 ip->ip_src.s_addr = htonl(id->src_ip); 1434 tcp->th_dport = htons(id->dst_port); 1435 tcp->th_sport = htons(id->src_port); 1436 } 1437 tcp->th_seq = htonl(seq); 1438 tcp->th_ack = htonl(ack); 1439 tcp->th_flags = TH_ACK; 1440 } 1441 /* 1442 * set ip_len to the payload size so we can compute 1443 * the tcp checksum on the pseudoheader 1444 * XXX check this, could save a couple of words ? 1445 */ 1446 ip->ip_len = htons(sizeof(struct tcphdr)); 1447 tcp->th_sum = in_cksum(m, m->m_pkthdr.len); 1448 /* 1449 * now fill fields left out earlier 1450 */ 1451 ip->ip_ttl = ip_defttl; 1452 ip->ip_len = m->m_pkthdr.len; 1453 m->m_flags |= M_SKIP_FIREWALL; 1454 ip_output(m, NULL, NULL, 0, NULL, NULL); 1455} 1456 1457/* 1458 * sends a reject message, consuming the mbuf passed as an argument. 1459 */ 1460static void 1461send_reject(struct ip_fw_args *args, int code, int offset, int ip_len) 1462{ 1463 1464 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */ 1465 /* We need the IP header in host order for icmp_error(). */ 1466 if (args->eh != NULL) { 1467 struct ip *ip = mtod(args->m, struct ip *); 1468 ip->ip_len = ntohs(ip->ip_len); 1469 ip->ip_off = ntohs(ip->ip_off); 1470 } 1471 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0); 1472 } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) { 1473 struct tcphdr *const tcp = 1474 L3HDR(struct tcphdr, mtod(args->m, struct ip *)); 1475 if ( (tcp->th_flags & TH_RST) == 0) 1476 send_pkt(&(args->f_id), ntohl(tcp->th_seq), 1477 ntohl(tcp->th_ack), 1478 tcp->th_flags | TH_RST); 1479 m_freem(args->m); 1480 } else 1481 m_freem(args->m); 1482 args->m = NULL; 1483} 1484 1485/** 1486 * 1487 * Given an ip_fw *, lookup_next_rule will return a pointer 1488 * to the next rule, which can be either the jump 1489 * target (for skipto instructions) or the next one in the list (in 1490 * all other cases including a missing jump target). 1491 * The result is also written in the "next_rule" field of the rule. 1492 * Backward jumps are not allowed, so start looking from the next 1493 * rule... 1494 * 1495 * This never returns NULL -- in case we do not have an exact match, 1496 * the next rule is returned. When the ruleset is changed, 1497 * pointers are flushed so we are always correct. 1498 */ 1499 1500static struct ip_fw * 1501lookup_next_rule(struct ip_fw *me) 1502{ 1503 struct ip_fw *rule = NULL; 1504 ipfw_insn *cmd; 1505 1506 /* look for action, in case it is a skipto */ 1507 cmd = ACTION_PTR(me); 1508 if (cmd->opcode == O_LOG) 1509 cmd += F_LEN(cmd); 1510 if (cmd->opcode == O_ALTQ) 1511 cmd += F_LEN(cmd); 1512 if ( cmd->opcode == O_SKIPTO ) 1513 for (rule = me->next; rule ; rule = rule->next) 1514 if (rule->rulenum >= cmd->arg1) 1515 break; 1516 if (rule == NULL) /* failure or not a skipto */ 1517 rule = me->next; 1518 me->next_rule = rule; 1519 return rule; 1520} 1521 1522static void 1523init_tables(void) 1524{ 1525 int i; 1526 1527 for (i = 0; i < IPFW_TABLES_MAX; i++) { 1528 rn_inithead((void **)&ipfw_tables[i].rnh, 32); 1529 ipfw_tables[i].modified = 1; 1530 } 1531} 1532 1533static int 1534add_table_entry(u_int16_t tbl, in_addr_t addr, u_int8_t mlen, u_int32_t value) 1535{ 1536 struct radix_node_head *rnh; 1537 struct table_entry *ent; 1538 1539 if (tbl >= IPFW_TABLES_MAX) 1540 return (EINVAL); 1541 rnh = ipfw_tables[tbl].rnh; 1542 ent = malloc(sizeof(*ent), M_IPFW_TBL, M_NOWAIT | M_ZERO); 1543 if (ent == NULL) 1544 return (ENOMEM); 1545 ent->value = value; 1546 ent->addr.sin_len = ent->mask.sin_len = 8; 1547 ent->mask.sin_addr.s_addr = htonl(mlen ? ~((1 << (32 - mlen)) - 1) : 0); 1548 ent->addr.sin_addr.s_addr = addr & ent->mask.sin_addr.s_addr; 1549 RADIX_NODE_HEAD_LOCK(rnh); 1550 if (rnh->rnh_addaddr(&ent->addr, &ent->mask, rnh, (void *)ent) == 1551 NULL) { 1552 RADIX_NODE_HEAD_UNLOCK(rnh); 1553 free(ent, M_IPFW_TBL); 1554 return (EEXIST); 1555 } 1556 ipfw_tables[tbl].modified = 1; 1557 RADIX_NODE_HEAD_UNLOCK(rnh); 1558 return (0); 1559} 1560 1561static int 1562del_table_entry(u_int16_t tbl, in_addr_t addr, u_int8_t mlen) 1563{ 1564 struct radix_node_head *rnh; 1565 struct table_entry *ent; 1566 struct sockaddr_in sa, mask; 1567 1568 if (tbl >= IPFW_TABLES_MAX) 1569 return (EINVAL); 1570 rnh = ipfw_tables[tbl].rnh; 1571 sa.sin_len = mask.sin_len = 8; 1572 mask.sin_addr.s_addr = htonl(mlen ? ~((1 << (32 - mlen)) - 1) : 0); 1573 sa.sin_addr.s_addr = addr & mask.sin_addr.s_addr; 1574 RADIX_NODE_HEAD_LOCK(rnh); 1575 ent = (struct table_entry *)rnh->rnh_deladdr(&sa, &mask, rnh); 1576 if (ent == NULL) { 1577 RADIX_NODE_HEAD_UNLOCK(rnh); 1578 return (ESRCH); 1579 } 1580 ipfw_tables[tbl].modified = 1; 1581 RADIX_NODE_HEAD_UNLOCK(rnh); 1582 free(ent, M_IPFW_TBL); 1583 return (0); 1584} 1585 1586static int 1587flush_table_entry(struct radix_node *rn, void *arg) 1588{ 1589 struct radix_node_head * const rnh = arg; 1590 struct table_entry *ent; 1591 1592 ent = (struct table_entry *) 1593 rnh->rnh_deladdr(rn->rn_key, rn->rn_mask, rnh); 1594 if (ent != NULL) 1595 free(ent, M_IPFW_TBL); 1596 return (0); 1597} 1598 1599static int 1600flush_table(u_int16_t tbl) 1601{ 1602 struct radix_node_head *rnh; 1603 1604 if (tbl >= IPFW_TABLES_MAX) 1605 return (EINVAL); 1606 rnh = ipfw_tables[tbl].rnh; 1607 RADIX_NODE_HEAD_LOCK(rnh); 1608 rnh->rnh_walktree(rnh, flush_table_entry, rnh); 1609 ipfw_tables[tbl].modified = 1; 1610 RADIX_NODE_HEAD_UNLOCK(rnh); 1611 return (0); 1612} 1613 1614static void 1615flush_tables(void) 1616{ 1617 u_int16_t tbl; 1618 1619 for (tbl = 0; tbl < IPFW_TABLES_MAX; tbl++) 1620 flush_table(tbl); 1621} 1622 1623static int 1624lookup_table(u_int16_t tbl, in_addr_t addr, u_int32_t *val) 1625{ 1626 struct radix_node_head *rnh; 1627 struct table_entry *ent; 1628 struct sockaddr_in sa; 1629 static in_addr_t last_addr; 1630 static int last_tbl; 1631 static int last_match; 1632 static u_int32_t last_value; 1633 1634 if (tbl >= IPFW_TABLES_MAX) 1635 return (0); 1636 if (tbl == last_tbl && addr == last_addr && 1637 !ipfw_tables[tbl].modified) { 1638 if (last_match) 1639 *val = last_value; 1640 return (last_match); 1641 } 1642 rnh = ipfw_tables[tbl].rnh; 1643 sa.sin_len = 8; 1644 sa.sin_addr.s_addr = addr; 1645 RADIX_NODE_HEAD_LOCK(rnh); 1646 ipfw_tables[tbl].modified = 0; 1647 ent = (struct table_entry *)(rnh->rnh_lookup(&sa, NULL, rnh)); 1648 RADIX_NODE_HEAD_UNLOCK(rnh); 1649 last_addr = addr; 1650 last_tbl = tbl; 1651 if (ent != NULL) { 1652 last_value = *val = ent->value; 1653 last_match = 1; 1654 return (1); 1655 } 1656 last_match = 0; 1657 return (0); 1658} 1659 1660static int 1661count_table_entry(struct radix_node *rn, void *arg) 1662{ 1663 u_int32_t * const cnt = arg; 1664 1665 (*cnt)++; 1666 return (0); 1667} 1668 1669static int 1670count_table(u_int32_t tbl, u_int32_t *cnt) 1671{ 1672 struct radix_node_head *rnh; 1673 1674 if (tbl >= IPFW_TABLES_MAX) 1675 return (EINVAL); 1676 rnh = ipfw_tables[tbl].rnh; 1677 *cnt = 0; 1678 RADIX_NODE_HEAD_LOCK(rnh); 1679 rnh->rnh_walktree(rnh, count_table_entry, cnt); 1680 RADIX_NODE_HEAD_UNLOCK(rnh); 1681 return (0); 1682} 1683 1684static int 1685dump_table_entry(struct radix_node *rn, void *arg) 1686{ 1687 struct table_entry * const n = (struct table_entry *)rn; 1688 ipfw_table * const tbl = arg; 1689 ipfw_table_entry *ent; 1690 1691 if (tbl->cnt == tbl->size) 1692 return (1); 1693 ent = &tbl->ent[tbl->cnt]; 1694 ent->tbl = tbl->tbl; 1695 if (in_nullhost(n->mask.sin_addr)) 1696 ent->masklen = 0; 1697 else 1698 ent->masklen = 33 - ffs(ntohl(n->mask.sin_addr.s_addr)); 1699 ent->addr = n->addr.sin_addr.s_addr; 1700 ent->value = n->value; 1701 tbl->cnt++; 1702 return (0); 1703} 1704 1705static int 1706dump_table(ipfw_table *tbl) 1707{ 1708 struct radix_node_head *rnh; 1709 1710 if (tbl->tbl >= IPFW_TABLES_MAX) 1711 return (EINVAL); 1712 rnh = ipfw_tables[tbl->tbl].rnh; 1713 tbl->cnt = 0; 1714 RADIX_NODE_HEAD_LOCK(rnh); 1715 rnh->rnh_walktree(rnh, dump_table_entry, tbl); 1716 RADIX_NODE_HEAD_UNLOCK(rnh); 1717 return (0); 1718} 1719 1720static void 1721fill_ugid_cache(struct inpcb *inp, struct ip_fw_ugid *ugp) 1722{ 1723 struct ucred *cr; 1724 1725 if (inp->inp_socket != NULL) { 1726 cr = inp->inp_socket->so_cred; 1727 ugp->fw_prid = jailed(cr) ? 1728 cr->cr_prison->pr_id : -1; 1729 ugp->fw_uid = cr->cr_uid; 1730 ugp->fw_ngroups = cr->cr_ngroups; 1731 bcopy(cr->cr_groups, ugp->fw_groups, 1732 sizeof(ugp->fw_groups)); 1733 } 1734} 1735 1736static int 1737check_uidgid(ipfw_insn_u32 *insn, 1738 int proto, struct ifnet *oif, 1739 struct in_addr dst_ip, u_int16_t dst_port, 1740 struct in_addr src_ip, u_int16_t src_port, 1741 struct ip_fw_ugid *ugp, int *lookup, struct inpcb *inp) 1742{ 1743 struct inpcbinfo *pi; 1744 int wildcard; 1745 struct inpcb *pcb; 1746 int match; 1747 gid_t *gp; 1748 1749 /* 1750 * Check to see if the UDP or TCP stack supplied us with 1751 * the PCB. If so, rather then holding a lock and looking 1752 * up the PCB, we can use the one that was supplied. 1753 */ 1754 if (inp && *lookup == 0) { 1755 INP_LOCK_ASSERT(inp); 1756 if (inp->inp_socket != NULL) { 1757 fill_ugid_cache(inp, ugp); 1758 *lookup = 1; 1759 } 1760 } 1761 /* 1762 * If we have already been here and the packet has no 1763 * PCB entry associated with it, then we can safely 1764 * assume that this is a no match. 1765 */ 1766 if (*lookup == -1) 1767 return (0); 1768 if (proto == IPPROTO_TCP) { 1769 wildcard = 0; 1770 pi = &tcbinfo; 1771 } else if (proto == IPPROTO_UDP) { 1772 wildcard = 1; 1773 pi = &udbinfo; 1774 } else 1775 return 0; 1776 match = 0; 1777 if (*lookup == 0) { 1778 INP_INFO_RLOCK(pi); 1779 pcb = (oif) ? 1780 in_pcblookup_hash(pi, 1781 dst_ip, htons(dst_port), 1782 src_ip, htons(src_port), 1783 wildcard, oif) : 1784 in_pcblookup_hash(pi, 1785 src_ip, htons(src_port), 1786 dst_ip, htons(dst_port), 1787 wildcard, NULL); 1788 if (pcb != NULL) { 1789 INP_LOCK(pcb); 1790 if (pcb->inp_socket != NULL) { 1791 fill_ugid_cache(pcb, ugp); 1792 *lookup = 1; 1793 } 1794 INP_UNLOCK(pcb); 1795 } 1796 INP_INFO_RUNLOCK(pi); 1797 if (*lookup == 0) { 1798 /* 1799 * If the lookup did not yield any results, there 1800 * is no sense in coming back and trying again. So 1801 * we can set lookup to -1 and ensure that we wont 1802 * bother the pcb system again. 1803 */ 1804 *lookup = -1; 1805 return (0); 1806 } 1807 } 1808 if (insn->o.opcode == O_UID) 1809 match = (ugp->fw_uid == (uid_t)insn->d[0]); 1810 else if (insn->o.opcode == O_GID) { 1811 for (gp = ugp->fw_groups; 1812 gp < &ugp->fw_groups[ugp->fw_ngroups]; gp++) 1813 if (*gp == (gid_t)insn->d[0]) { 1814 match = 1; 1815 break; 1816 } 1817 } else if (insn->o.opcode == O_JAIL) 1818 match = (ugp->fw_prid == (int)insn->d[0]); 1819 return match; 1820} 1821 1822/* 1823 * The main check routine for the firewall. 1824 * 1825 * All arguments are in args so we can modify them and return them 1826 * back to the caller. 1827 * 1828 * Parameters: 1829 * 1830 * args->m (in/out) The packet; we set to NULL when/if we nuke it. 1831 * Starts with the IP header. 1832 * args->eh (in) Mac header if present, or NULL for layer3 packet. 1833 * args->oif Outgoing interface, or NULL if packet is incoming. 1834 * The incoming interface is in the mbuf. (in) 1835 * args->divert_rule (in/out) 1836 * Skip up to the first rule past this rule number; 1837 * upon return, non-zero port number for divert or tee. 1838 * 1839 * args->rule Pointer to the last matching rule (in/out) 1840 * args->next_hop Socket we are forwarding to (out). 1841 * args->f_id Addresses grabbed from the packet (out) 1842 * args->cookie a cookie depending on rule action 1843 * 1844 * Return value: 1845 * 1846 * IP_FW_PASS the packet must be accepted 1847 * IP_FW_DENY the packet must be dropped 1848 * IP_FW_DIVERT divert packet, port in m_tag 1849 * IP_FW_TEE tee packet, port in m_tag 1850 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie 1851 * IP_FW_NETGRAPH into netgraph, cookie args->cookie 1852 * 1853 */ 1854 1855int 1856ipfw_chk(struct ip_fw_args *args) 1857{ 1858 /* 1859 * Local variables hold state during the processing of a packet. 1860 * 1861 * IMPORTANT NOTE: to speed up the processing of rules, there 1862 * are some assumption on the values of the variables, which 1863 * are documented here. Should you change them, please check 1864 * the implementation of the various instructions to make sure 1865 * that they still work. 1866 * 1867 * args->eh The MAC header. It is non-null for a layer2 1868 * packet, it is NULL for a layer-3 packet. 1869 * 1870 * m | args->m Pointer to the mbuf, as received from the caller. 1871 * It may change if ipfw_chk() does an m_pullup, or if it 1872 * consumes the packet because it calls send_reject(). 1873 * XXX This has to change, so that ipfw_chk() never modifies 1874 * or consumes the buffer. 1875 * ip is simply an alias of the value of m, and it is kept 1876 * in sync with it (the packet is supposed to start with 1877 * the ip header). 1878 */ 1879 struct mbuf *m = args->m; 1880 struct ip *ip = mtod(m, struct ip *); 1881 1882 /* 1883 * For rules which contain uid/gid or jail constraints, cache 1884 * a copy of the users credentials after the pcb lookup has been 1885 * executed. This will speed up the processing of rules with 1886 * these types of constraints, as well as decrease contention 1887 * on pcb related locks. 1888 */ 1889 struct ip_fw_ugid fw_ugid_cache; 1890 int ugid_lookup = 0; 1891 1892 /* 1893 * divinput_flags If non-zero, set to the IP_FW_DIVERT_*_FLAG 1894 * associated with a packet input on a divert socket. This 1895 * will allow to distinguish traffic and its direction when 1896 * it originates from a divert socket. 1897 */ 1898 u_int divinput_flags = 0; 1899 1900 /* 1901 * oif | args->oif If NULL, ipfw_chk has been called on the 1902 * inbound path (ether_input, bdg_forward, ip_input). 1903 * If non-NULL, ipfw_chk has been called on the outbound path 1904 * (ether_output, ip_output). 1905 */ 1906 struct ifnet *oif = args->oif; 1907 1908 struct ip_fw *f = NULL; /* matching rule */ 1909 int retval = 0; 1910 1911 /* 1912 * hlen The length of the IPv4 header. 1913 * hlen >0 means we have an IPv4 packet. 1914 */ 1915 u_int hlen = 0; /* hlen >0 means we have an IP pkt */ 1916 1917 /* 1918 * offset The offset of a fragment. offset != 0 means that 1919 * we have a fragment at this offset of an IPv4 packet. 1920 * offset == 0 means that (if this is an IPv4 packet) 1921 * this is the first or only fragment. 1922 */ 1923 u_short offset = 0; 1924 1925 /* 1926 * Local copies of addresses. They are only valid if we have 1927 * an IP packet. 1928 * 1929 * proto The protocol. Set to 0 for non-ip packets, 1930 * or to the protocol read from the packet otherwise. 1931 * proto != 0 means that we have an IPv4 packet. 1932 * 1933 * src_port, dst_port port numbers, in HOST format. Only 1934 * valid for TCP and UDP packets. 1935 * 1936 * src_ip, dst_ip ip addresses, in NETWORK format. 1937 * Only valid for IPv4 packets. 1938 */ 1939 u_int8_t proto; 1940 u_int16_t src_port = 0, dst_port = 0; /* NOTE: host format */ 1941 struct in_addr src_ip, dst_ip; /* NOTE: network format */ 1942 u_int16_t ip_len=0; 1943 int pktlen; 1944 1945 /* 1946 * dyn_dir = MATCH_UNKNOWN when rules unchecked, 1947 * MATCH_NONE when checked and not matched (q = NULL), 1948 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) 1949 */ 1950 int dyn_dir = MATCH_UNKNOWN; 1951 ipfw_dyn_rule *q = NULL; 1952 struct ip_fw_chain *chain = &layer3_chain; 1953 struct m_tag *mtag; 1954 1955 /* 1956 * We store in ulp a pointer to the upper layer protocol header. 1957 * In the ipv4 case this is easy to determine from the header, 1958 * but for ipv6 we might have some additional headers in the middle. 1959 * ulp is NULL if not found. 1960 */ 1961 void *ulp = NULL; /* upper layer protocol pointer. */ 1962 /* XXX ipv6 variables */ 1963 int is_ipv6 = 0; 1964 u_int16_t ext_hd = 0; /* bits vector for extension header filtering */ 1965 /* end of ipv6 variables */ 1966 1967 if (m->m_flags & M_SKIP_FIREWALL) 1968 return (IP_FW_PASS); /* accept */ 1969 1970 pktlen = m->m_pkthdr.len; 1971 proto = args->f_id.proto = 0; /* mark f_id invalid */ 1972 1973/* 1974 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, 1975 * then it sets p to point at the offset "len" in the mbuf. WARNING: the 1976 * pointer might become stale after other pullups (but we never use it 1977 * this way). 1978 */ 1979#define PULLUP_TO(len, p, T) \ 1980do { \ 1981 int x = (len) + sizeof(T); \ 1982 if ((m)->m_len < x) { \ 1983 args->m = m = m_pullup(m, x); \ 1984 if (m == NULL) \ 1985 goto pullup_failed; \ 1986 } \ 1987 p = (mtod(m, char *) + (len)); \ 1988} while (0) 1989 1990 /* Identify IP packets and fill up variables. */ 1991 if (pktlen >= sizeof(struct ip6_hdr) && 1992 (args->eh == NULL || ntohs(args->eh->ether_type)==ETHERTYPE_IPV6) && 1993 mtod(m, struct ip *)->ip_v == 6) { 1994 is_ipv6 = 1; 1995 args->f_id.addr_type = 6; 1996 hlen = sizeof(struct ip6_hdr); 1997 proto = mtod(m, struct ip6_hdr *)->ip6_nxt; 1998 1999 /* Search extension headers to find upper layer protocols */ 2000 while (ulp == NULL) { 2001 switch (proto) { 2002 case IPPROTO_ICMPV6: 2003 PULLUP_TO(hlen, ulp, struct icmp6_hdr); 2004 args->f_id.flags = ICMP6(ulp)->icmp6_type; 2005 break; 2006 2007 case IPPROTO_TCP: 2008 PULLUP_TO(hlen, ulp, struct tcphdr); 2009 dst_port = TCP(ulp)->th_dport; 2010 src_port = TCP(ulp)->th_sport; 2011 args->f_id.flags = TCP(ulp)->th_flags; 2012 break; 2013 2014 case IPPROTO_UDP: 2015 PULLUP_TO(hlen, ulp, struct udphdr); 2016 dst_port = UDP(ulp)->uh_dport; 2017 src_port = UDP(ulp)->uh_sport; 2018 break; 2019 2020 case IPPROTO_HOPOPTS: 2021 PULLUP_TO(hlen, ulp, struct ip6_hbh); 2022 ext_hd |= EXT_HOPOPTS; 2023 hlen += sizeof(struct ip6_hbh); 2024 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 2025 ulp = NULL; 2026 break; 2027 2028 case IPPROTO_ROUTING: 2029 PULLUP_TO(hlen, ulp, struct ip6_rthdr); 2030 ext_hd |= EXT_ROUTING; 2031 hlen += sizeof(struct ip6_rthdr); 2032 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; 2033 ulp = NULL; 2034 break; 2035 2036 case IPPROTO_FRAGMENT: 2037 PULLUP_TO(hlen, ulp, struct ip6_frag); 2038 ext_hd |= EXT_FRAGMENT; 2039 hlen += sizeof (struct ip6_frag); 2040 proto = ((struct ip6_frag *)ulp)->ip6f_nxt; 2041 offset = 1; 2042 ulp = NULL; /* XXX is it correct ? */ 2043 break; 2044 2045 case IPPROTO_AH: 2046 case IPPROTO_NONE: 2047 case IPPROTO_ESP: 2048 PULLUP_TO(hlen, ulp, struct ip6_ext); 2049 if (proto == IPPROTO_AH) 2050 ext_hd |= EXT_AH; 2051 else if (proto == IPPROTO_ESP) 2052 ext_hd |= EXT_ESP; 2053 hlen += ((struct ip6_ext *)ulp)->ip6e_len + 2054 sizeof (struct ip6_ext); 2055 proto = ((struct ip6_ext *)ulp)->ip6e_nxt; 2056 ulp = NULL; 2057 break; 2058 2059 default: 2060 printf( "IPFW2: IPV6 - Unknown Extension Header (%d)\n", 2061 proto); 2062 return 0; /* deny */ 2063 break; 2064 } /*switch */ 2065 } 2066 args->f_id.src_ip6 = mtod(m,struct ip6_hdr *)->ip6_src; 2067 args->f_id.dst_ip6 = mtod(m,struct ip6_hdr *)->ip6_dst; 2068 args->f_id.src_ip = 0; 2069 args->f_id.dst_ip = 0; 2070 args->f_id.flow_id6 = ntohs(mtod(m, struct ip6_hdr *)->ip6_flow); 2071 /* hlen != 0 is used to detect ipv4 packets, so clear it now */ 2072 hlen = 0; 2073 } else if (pktlen >= sizeof(struct ip) && 2074 (args->eh == NULL || ntohs(args->eh->ether_type) == ETHERTYPE_IP) && 2075 mtod(m, struct ip *)->ip_v == 4) { 2076 ip = mtod(m, struct ip *); 2077 hlen = ip->ip_hl << 2; 2078 args->f_id.addr_type = 4; 2079 2080 /* 2081 * Collect parameters into local variables for faster matching. 2082 */ 2083 proto = ip->ip_p; 2084 src_ip = ip->ip_src; 2085 dst_ip = ip->ip_dst; 2086 if (args->eh != NULL) { /* layer 2 packets are as on the wire */ 2087 offset = ntohs(ip->ip_off) & IP_OFFMASK; 2088 ip_len = ntohs(ip->ip_len); 2089 } else { 2090 offset = ip->ip_off & IP_OFFMASK; 2091 ip_len = ip->ip_len; 2092 } 2093 pktlen = ip_len < pktlen ? ip_len : pktlen; 2094 2095 if (offset == 0) { 2096 switch (proto) { 2097 case IPPROTO_TCP: 2098 PULLUP_TO(hlen, ulp, struct tcphdr); 2099 dst_port = TCP(ulp)->th_dport; 2100 src_port = TCP(ulp)->th_sport; 2101 args->f_id.flags = TCP(ulp)->th_flags; 2102 break; 2103 2104 case IPPROTO_UDP: 2105 PULLUP_TO(hlen, ulp, struct udphdr); 2106 dst_port = UDP(ulp)->uh_dport; 2107 src_port = UDP(ulp)->uh_sport; 2108 break; 2109 2110 case IPPROTO_ICMP: 2111 PULLUP_TO(hlen, ulp, struct icmphdr); 2112 args->f_id.flags = ICMP(ulp)->icmp_type; 2113 break; 2114 2115 default: 2116 break; 2117 } 2118 } 2119 2120 args->f_id.src_ip = ntohl(src_ip.s_addr); 2121 args->f_id.dst_ip = ntohl(dst_ip.s_addr); 2122 } 2123#undef PULLUP_TO 2124 if (proto) { /* we may have port numbers, store them */ 2125 args->f_id.proto = proto; 2126 args->f_id.src_port = src_port = ntohs(src_port); 2127 args->f_id.dst_port = dst_port = ntohs(dst_port); 2128 } 2129 2130 IPFW_RLOCK(chain); 2131 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 2132 if (args->rule) { 2133 /* 2134 * Packet has already been tagged. Look for the next rule 2135 * to restart processing. 2136 * 2137 * If fw_one_pass != 0 then just accept it. 2138 * XXX should not happen here, but optimized out in 2139 * the caller. 2140 */ 2141 if (fw_one_pass) { 2142 IPFW_RUNLOCK(chain); 2143 return (IP_FW_PASS); 2144 } 2145 2146 f = args->rule->next_rule; 2147 if (f == NULL) 2148 f = lookup_next_rule(args->rule); 2149 } else { 2150 /* 2151 * Find the starting rule. It can be either the first 2152 * one, or the one after divert_rule if asked so. 2153 */ 2154 int skipto = mtag ? divert_cookie(mtag) : 0; 2155 2156 f = chain->rules; 2157 if (args->eh == NULL && skipto != 0) { 2158 if (skipto >= IPFW_DEFAULT_RULE) { 2159 IPFW_RUNLOCK(chain); 2160 return (IP_FW_DENY); /* invalid */ 2161 } 2162 while (f && f->rulenum <= skipto) 2163 f = f->next; 2164 if (f == NULL) { /* drop packet */ 2165 IPFW_RUNLOCK(chain); 2166 return (IP_FW_DENY); 2167 } 2168 } 2169 } 2170 /* reset divert rule to avoid confusion later */ 2171 if (mtag) { 2172 divinput_flags = divert_info(mtag) & 2173 (IP_FW_DIVERT_OUTPUT_FLAG | IP_FW_DIVERT_LOOPBACK_FLAG); 2174 m_tag_delete(m, mtag); 2175 } 2176 2177 /* 2178 * Now scan the rules, and parse microinstructions for each rule. 2179 */ 2180 for (; f; f = f->next) { 2181 int l, cmdlen; 2182 ipfw_insn *cmd; 2183 int skip_or; /* skip rest of OR block */ 2184 2185again: 2186 if (set_disable & (1 << f->set) ) 2187 continue; 2188 2189 skip_or = 0; 2190 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; 2191 l -= cmdlen, cmd += cmdlen) { 2192 int match; 2193 2194 /* 2195 * check_body is a jump target used when we find a 2196 * CHECK_STATE, and need to jump to the body of 2197 * the target rule. 2198 */ 2199 2200check_body: 2201 cmdlen = F_LEN(cmd); 2202 /* 2203 * An OR block (insn_1 || .. || insn_n) has the 2204 * F_OR bit set in all but the last instruction. 2205 * The first match will set "skip_or", and cause 2206 * the following instructions to be skipped until 2207 * past the one with the F_OR bit clear. 2208 */ 2209 if (skip_or) { /* skip this instruction */ 2210 if ((cmd->len & F_OR) == 0) 2211 skip_or = 0; /* next one is good */ 2212 continue; 2213 } 2214 match = 0; /* set to 1 if we succeed */ 2215 2216 switch (cmd->opcode) { 2217 /* 2218 * The first set of opcodes compares the packet's 2219 * fields with some pattern, setting 'match' if a 2220 * match is found. At the end of the loop there is 2221 * logic to deal with F_NOT and F_OR flags associated 2222 * with the opcode. 2223 */ 2224 case O_NOP: 2225 match = 1; 2226 break; 2227 2228 case O_FORWARD_MAC: 2229 printf("ipfw: opcode %d unimplemented\n", 2230 cmd->opcode); 2231 break; 2232 2233 case O_GID: 2234 case O_UID: 2235 case O_JAIL: 2236 /* 2237 * We only check offset == 0 && proto != 0, 2238 * as this ensures that we have a 2239 * packet with the ports info. 2240 */ 2241 if (offset!=0) 2242 break; 2243 if (is_ipv6) /* XXX to be fixed later */ 2244 break; 2245 if (proto == IPPROTO_TCP || 2246 proto == IPPROTO_UDP) 2247 match = check_uidgid( 2248 (ipfw_insn_u32 *)cmd, 2249 proto, oif, 2250 dst_ip, dst_port, 2251 src_ip, src_port, &fw_ugid_cache, 2252 &ugid_lookup, args->inp); 2253 break; 2254 2255 case O_RECV: 2256 match = iface_match(m->m_pkthdr.rcvif, 2257 (ipfw_insn_if *)cmd); 2258 break; 2259 2260 case O_XMIT: 2261 match = iface_match(oif, (ipfw_insn_if *)cmd); 2262 break; 2263 2264 case O_VIA: 2265 match = iface_match(oif ? oif : 2266 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd); 2267 break; 2268 2269 case O_MACADDR2: 2270 if (args->eh != NULL) { /* have MAC header */ 2271 u_int32_t *want = (u_int32_t *) 2272 ((ipfw_insn_mac *)cmd)->addr; 2273 u_int32_t *mask = (u_int32_t *) 2274 ((ipfw_insn_mac *)cmd)->mask; 2275 u_int32_t *hdr = (u_int32_t *)args->eh; 2276 2277 match = 2278 ( want[0] == (hdr[0] & mask[0]) && 2279 want[1] == (hdr[1] & mask[1]) && 2280 want[2] == (hdr[2] & mask[2]) ); 2281 } 2282 break; 2283 2284 case O_MAC_TYPE: 2285 if (args->eh != NULL) { 2286 u_int16_t t = 2287 ntohs(args->eh->ether_type); 2288 u_int16_t *p = 2289 ((ipfw_insn_u16 *)cmd)->ports; 2290 int i; 2291 2292 for (i = cmdlen - 1; !match && i>0; 2293 i--, p += 2) 2294 match = (t>=p[0] && t<=p[1]); 2295 } 2296 break; 2297 2298 case O_FRAG: 2299 match = (offset != 0); 2300 break; 2301 2302 case O_IN: /* "out" is "not in" */ 2303 match = (oif == NULL); 2304 break; 2305 2306 case O_LAYER2: 2307 match = (args->eh != NULL); 2308 break; 2309 2310 case O_DIVERTED: 2311 match = (cmd->arg1 & 1 && divinput_flags & 2312 IP_FW_DIVERT_LOOPBACK_FLAG) || 2313 (cmd->arg1 & 2 && divinput_flags & 2314 IP_FW_DIVERT_OUTPUT_FLAG); 2315 break; 2316 2317 case O_PROTO: 2318 /* 2319 * We do not allow an arg of 0 so the 2320 * check of "proto" only suffices. 2321 */ 2322 match = (proto == cmd->arg1); 2323 break; 2324 2325 case O_IP_SRC: 2326 match = (hlen > 0 && 2327 ((ipfw_insn_ip *)cmd)->addr.s_addr == 2328 src_ip.s_addr); 2329 break; 2330 2331 case O_IP_SRC_LOOKUP: 2332 case O_IP_DST_LOOKUP: 2333 if (hlen > 0) { 2334 uint32_t a = 2335 (cmd->opcode == O_IP_DST_LOOKUP) ? 2336 dst_ip.s_addr : src_ip.s_addr; 2337 uint32_t v; 2338 2339 match = lookup_table(cmd->arg1, a, &v); 2340 if (!match) 2341 break; 2342 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) 2343 match = 2344 ((ipfw_insn_u32 *)cmd)->d[0] == v; 2345 } 2346 break; 2347 2348 case O_IP_SRC_MASK: 2349 case O_IP_DST_MASK: 2350 if (hlen > 0) { 2351 uint32_t a = 2352 (cmd->opcode == O_IP_DST_MASK) ? 2353 dst_ip.s_addr : src_ip.s_addr; 2354 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; 2355 int i = cmdlen-1; 2356 2357 for (; !match && i>0; i-= 2, p+= 2) 2358 match = (p[0] == (a & p[1])); 2359 } 2360 break; 2361 2362 case O_IP_SRC_ME: 2363 if (hlen > 0) { 2364 struct ifnet *tif; 2365 2366 INADDR_TO_IFP(src_ip, tif); 2367 match = (tif != NULL); 2368 } 2369 break; 2370 2371 case O_IP_DST_SET: 2372 case O_IP_SRC_SET: 2373 if (hlen > 0) { 2374 u_int32_t *d = (u_int32_t *)(cmd+1); 2375 u_int32_t addr = 2376 cmd->opcode == O_IP_DST_SET ? 2377 args->f_id.dst_ip : 2378 args->f_id.src_ip; 2379 2380 if (addr < d[0]) 2381 break; 2382 addr -= d[0]; /* subtract base */ 2383 match = (addr < cmd->arg1) && 2384 ( d[ 1 + (addr>>5)] & 2385 (1<<(addr & 0x1f)) ); 2386 } 2387 break; 2388 2389 case O_IP_DST: 2390 match = (hlen > 0 && 2391 ((ipfw_insn_ip *)cmd)->addr.s_addr == 2392 dst_ip.s_addr); 2393 break; 2394 2395 case O_IP_DST_ME: 2396 if (hlen > 0) { 2397 struct ifnet *tif; 2398 2399 INADDR_TO_IFP(dst_ip, tif); 2400 match = (tif != NULL); 2401 } 2402 break; 2403 2404 case O_IP_SRCPORT: 2405 case O_IP_DSTPORT: 2406 /* 2407 * offset == 0 && proto != 0 is enough 2408 * to guarantee that we have a 2409 * packet with port info. 2410 */ 2411 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP) 2412 && offset == 0) { 2413 u_int16_t x = 2414 (cmd->opcode == O_IP_SRCPORT) ? 2415 src_port : dst_port ; 2416 u_int16_t *p = 2417 ((ipfw_insn_u16 *)cmd)->ports; 2418 int i; 2419 2420 for (i = cmdlen - 1; !match && i>0; 2421 i--, p += 2) 2422 match = (x>=p[0] && x<=p[1]); 2423 } 2424 break; 2425 2426 case O_ICMPTYPE: 2427 match = (offset == 0 && proto==IPPROTO_ICMP && 2428 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); 2429 break; 2430 2431#ifdef INET6 2432 case O_ICMP6TYPE: 2433 match = is_ipv6 && offset == 0 && 2434 proto==IPPROTO_ICMPV6 && 2435 icmp6type_match( 2436 ICMP6(ulp)->icmp6_type, 2437 (ipfw_insn_u32 *)cmd); 2438 break; 2439#endif /* INET6 */ 2440 2441 case O_IPOPT: 2442 match = (hlen > 0 && 2443 ipopts_match(mtod(m, struct ip *), cmd) ); 2444 break; 2445 2446 case O_IPVER: 2447 match = (hlen > 0 && 2448 cmd->arg1 == mtod(m, struct ip *)->ip_v); 2449 break; 2450 2451 case O_IPID: 2452 case O_IPLEN: 2453 case O_IPTTL: 2454 if (hlen > 0) { /* only for IP packets */ 2455 uint16_t x; 2456 uint16_t *p; 2457 int i; 2458 2459 if (cmd->opcode == O_IPLEN) 2460 x = ip_len; 2461 else if (cmd->opcode == O_IPTTL) 2462 x = mtod(m, struct ip *)->ip_ttl; 2463 else /* must be IPID */ 2464 x = ntohs(mtod(m, struct ip *)->ip_id); 2465 if (cmdlen == 1) { 2466 match = (cmd->arg1 == x); 2467 break; 2468 } 2469 /* otherwise we have ranges */ 2470 p = ((ipfw_insn_u16 *)cmd)->ports; 2471 i = cmdlen - 1; 2472 for (; !match && i>0; i--, p += 2) 2473 match = (x >= p[0] && x <= p[1]); 2474 } 2475 break; 2476 2477 case O_IPPRECEDENCE: 2478 match = (hlen > 0 && 2479 (cmd->arg1 == (mtod(m, struct ip *)->ip_tos & 0xe0)) ); 2480 break; 2481 2482 case O_IPTOS: 2483 match = (hlen > 0 && 2484 flags_match(cmd, mtod(m, struct ip *)->ip_tos)); 2485 break; 2486 2487 case O_TCPDATALEN: 2488 if (proto == IPPROTO_TCP && offset == 0) { 2489 struct tcphdr *tcp; 2490 uint16_t x; 2491 uint16_t *p; 2492 int i; 2493 2494 tcp = TCP(ulp); 2495 x = ip_len - 2496 ((ip->ip_hl + tcp->th_off) << 2); 2497 if (cmdlen == 1) { 2498 match = (cmd->arg1 == x); 2499 break; 2500 } 2501 /* otherwise we have ranges */ 2502 p = ((ipfw_insn_u16 *)cmd)->ports; 2503 i = cmdlen - 1; 2504 for (; !match && i>0; i--, p += 2) 2505 match = (x >= p[0] && x <= p[1]); 2506 } 2507 break; 2508 2509 case O_TCPFLAGS: 2510 match = (proto == IPPROTO_TCP && offset == 0 && 2511 flags_match(cmd, TCP(ulp)->th_flags)); 2512 break; 2513 2514 case O_TCPOPTS: 2515 match = (proto == IPPROTO_TCP && offset == 0 && 2516 tcpopts_match(TCP(ulp), cmd)); 2517 break; 2518 2519 case O_TCPSEQ: 2520 match = (proto == IPPROTO_TCP && offset == 0 && 2521 ((ipfw_insn_u32 *)cmd)->d[0] == 2522 TCP(ulp)->th_seq); 2523 break; 2524 2525 case O_TCPACK: 2526 match = (proto == IPPROTO_TCP && offset == 0 && 2527 ((ipfw_insn_u32 *)cmd)->d[0] == 2528 TCP(ulp)->th_ack); 2529 break; 2530 2531 case O_TCPWIN: 2532 match = (proto == IPPROTO_TCP && offset == 0 && 2533 cmd->arg1 == TCP(ulp)->th_win); 2534 break; 2535 2536 case O_ESTAB: 2537 /* reject packets which have SYN only */ 2538 /* XXX should i also check for TH_ACK ? */ 2539 match = (proto == IPPROTO_TCP && offset == 0 && 2540 (TCP(ulp)->th_flags & 2541 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); 2542 break; 2543 2544 case O_ALTQ: { 2545 struct altq_tag *at; 2546 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 2547 2548 match = 1; 2549 mtag = m_tag_get(PACKET_TAG_PF_QID, 2550 sizeof(struct altq_tag), 2551 M_NOWAIT); 2552 if (mtag == NULL) { 2553 /* 2554 * Let the packet fall back to the 2555 * default ALTQ. 2556 */ 2557 break; 2558 } 2559 at = (struct altq_tag *)(mtag+1); 2560 at->qid = altq->qid; 2561 if (hlen != 0) 2562 at->af = AF_INET; 2563 else 2564 at->af = AF_LINK; 2565 at->hdr = ip; 2566 m_tag_prepend(m, mtag); 2567 break; 2568 } 2569 2570 case O_LOG: 2571 if (fw_verbose) 2572 ipfw_log(f, hlen, args->eh, m, oif); 2573 match = 1; 2574 break; 2575 2576 case O_PROB: 2577 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); 2578 break; 2579 2580 case O_VERREVPATH: 2581 /* Outgoing packets automatically pass/match */ 2582 /* XXX BED: verify_path was verify_rev_path in the diff... */ 2583 match = ((oif != NULL) || 2584 (m->m_pkthdr.rcvif == NULL) || 2585 ( 2586#ifdef INET6 2587 is_ipv6 ? 2588 verify_rev_path6(&(args->f_id.src_ip6), 2589 m->m_pkthdr.rcvif) : 2590#endif 2591 verify_path(src_ip, m->m_pkthdr.rcvif))); 2592 break; 2593 2594 case O_VERSRCREACH: 2595 /* Outgoing packets automatically pass/match */ 2596 match = (hlen > 0 && ((oif != NULL) || 2597 verify_path(src_ip, NULL))); 2598 break; 2599 2600 case O_ANTISPOOF: 2601 /* Outgoing packets automatically pass/match */ 2602 if (oif == NULL && hlen > 0 && 2603 in_localaddr(src_ip)) 2604 match = verify_path(src_ip, 2605 m->m_pkthdr.rcvif); 2606 else 2607 match = 1; 2608 break; 2609 2610 case O_IPSEC: 2611#ifdef FAST_IPSEC 2612 match = (m_tag_find(m, 2613 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); 2614#endif 2615#ifdef IPSEC 2616 match = (ipsec_getnhist(m) != 0); 2617#endif 2618 /* otherwise no match */ 2619 break; 2620 2621 case O_IP6_SRC: 2622 match = is_ipv6 && 2623 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, 2624 &((ipfw_insn_ip6 *)cmd)->addr6); 2625 break; 2626 2627 case O_IP6_DST: 2628 match = is_ipv6 && 2629 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, 2630 &((ipfw_insn_ip6 *)cmd)->addr6); 2631 break; 2632 case O_IP6_SRC_MASK: 2633 if (is_ipv6) { 2634 ipfw_insn_ip6 *te = (ipfw_insn_ip6 *)cmd; 2635 struct in6_addr p = args->f_id.src_ip6; 2636 2637 APPLY_MASK(&p, &te->mask6); 2638 match = IN6_ARE_ADDR_EQUAL(&te->addr6, &p); 2639 } 2640 break; 2641 2642 case O_IP6_DST_MASK: 2643 if (is_ipv6) { 2644 ipfw_insn_ip6 *te = (ipfw_insn_ip6 *)cmd; 2645 struct in6_addr p = args->f_id.dst_ip6; 2646 2647 APPLY_MASK(&p, &te->mask6); 2648 match = IN6_ARE_ADDR_EQUAL(&te->addr6, &p); 2649 } 2650 break; 2651 2652#ifdef INET6 2653 case O_IP6_SRC_ME: 2654 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6); 2655 break; 2656 2657 case O_IP6_DST_ME: 2658 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6); 2659 break; 2660 2661 case O_FLOW6ID: 2662 match = is_ipv6 && 2663 flow6id_match(args->f_id.flow_id6, 2664 (ipfw_insn_u32 *) cmd); 2665 break; 2666 2667 case O_EXT_HDR: 2668 match = is_ipv6 && 2669 (ext_hd & ((ipfw_insn *) cmd)->arg1); 2670 break; 2671 2672 case O_IP6: 2673 match = is_ipv6; 2674 break; 2675#endif 2676 2677 /* 2678 * The second set of opcodes represents 'actions', 2679 * i.e. the terminal part of a rule once the packet 2680 * matches all previous patterns. 2681 * Typically there is only one action for each rule, 2682 * and the opcode is stored at the end of the rule 2683 * (but there are exceptions -- see below). 2684 * 2685 * In general, here we set retval and terminate the 2686 * outer loop (would be a 'break 3' in some language, 2687 * but we need to do a 'goto done'). 2688 * 2689 * Exceptions: 2690 * O_COUNT and O_SKIPTO actions: 2691 * instead of terminating, we jump to the next rule 2692 * ('goto next_rule', equivalent to a 'break 2'), 2693 * or to the SKIPTO target ('goto again' after 2694 * having set f, cmd and l), respectively. 2695 * 2696 * O_LOG and O_ALTQ action parameters: 2697 * perform some action and set match = 1; 2698 * 2699 * O_LIMIT and O_KEEP_STATE: these opcodes are 2700 * not real 'actions', and are stored right 2701 * before the 'action' part of the rule. 2702 * These opcodes try to install an entry in the 2703 * state tables; if successful, we continue with 2704 * the next opcode (match=1; break;), otherwise 2705 * the packet * must be dropped 2706 * ('goto done' after setting retval); 2707 * 2708 * O_PROBE_STATE and O_CHECK_STATE: these opcodes 2709 * cause a lookup of the state table, and a jump 2710 * to the 'action' part of the parent rule 2711 * ('goto check_body') if an entry is found, or 2712 * (CHECK_STATE only) a jump to the next rule if 2713 * the entry is not found ('goto next_rule'). 2714 * The result of the lookup is cached to make 2715 * further instances of these opcodes are 2716 * effectively NOPs. 2717 */ 2718 case O_LIMIT: 2719 case O_KEEP_STATE: 2720 if (install_state(f, 2721 (ipfw_insn_limit *)cmd, args)) { 2722 retval = IP_FW_DENY; 2723 goto done; /* error/limit violation */ 2724 } 2725 match = 1; 2726 break; 2727 2728 case O_PROBE_STATE: 2729 case O_CHECK_STATE: 2730 /* 2731 * dynamic rules are checked at the first 2732 * keep-state or check-state occurrence, 2733 * with the result being stored in dyn_dir. 2734 * The compiler introduces a PROBE_STATE 2735 * instruction for us when we have a 2736 * KEEP_STATE (because PROBE_STATE needs 2737 * to be run first). 2738 */ 2739 if (dyn_dir == MATCH_UNKNOWN && 2740 (q = lookup_dyn_rule(&args->f_id, 2741 &dyn_dir, proto == IPPROTO_TCP ? 2742 TCP(ulp) : NULL)) 2743 != NULL) { 2744 /* 2745 * Found dynamic entry, update stats 2746 * and jump to the 'action' part of 2747 * the parent rule. 2748 */ 2749 q->pcnt++; 2750 q->bcnt += pktlen; 2751 f = q->rule; 2752 cmd = ACTION_PTR(f); 2753 l = f->cmd_len - f->act_ofs; 2754 IPFW_DYN_UNLOCK(); 2755 goto check_body; 2756 } 2757 /* 2758 * Dynamic entry not found. If CHECK_STATE, 2759 * skip to next rule, if PROBE_STATE just 2760 * ignore and continue with next opcode. 2761 */ 2762 if (cmd->opcode == O_CHECK_STATE) 2763 goto next_rule; 2764 match = 1; 2765 break; 2766 2767 case O_ACCEPT: 2768 retval = 0; /* accept */ 2769 goto done; 2770 2771 case O_PIPE: 2772 case O_QUEUE: 2773 args->rule = f; /* report matching rule */ 2774 args->cookie = cmd->arg1; 2775 retval = IP_FW_DUMMYNET; 2776 goto done; 2777 2778 case O_DIVERT: 2779 case O_TEE: { 2780 struct divert_tag *dt; 2781 2782 if (args->eh) /* not on layer 2 */ 2783 break; 2784 mtag = m_tag_get(PACKET_TAG_DIVERT, 2785 sizeof(struct divert_tag), 2786 M_NOWAIT); 2787 if (mtag == NULL) { 2788 /* XXX statistic */ 2789 /* drop packet */ 2790 IPFW_RUNLOCK(chain); 2791 return (IP_FW_DENY); 2792 } 2793 dt = (struct divert_tag *)(mtag+1); 2794 dt->cookie = f->rulenum; 2795 dt->info = cmd->arg1; 2796 m_tag_prepend(m, mtag); 2797 retval = (cmd->opcode == O_DIVERT) ? 2798 IP_FW_DIVERT : IP_FW_TEE; 2799 goto done; 2800 } 2801 2802 case O_COUNT: 2803 case O_SKIPTO: 2804 f->pcnt++; /* update stats */ 2805 f->bcnt += pktlen; 2806 f->timestamp = time_second; 2807 if (cmd->opcode == O_COUNT) 2808 goto next_rule; 2809 /* handle skipto */ 2810 if (f->next_rule == NULL) 2811 lookup_next_rule(f); 2812 f = f->next_rule; 2813 goto again; 2814 2815 case O_REJECT: 2816 /* 2817 * Drop the packet and send a reject notice 2818 * if the packet is not ICMP (or is an ICMP 2819 * query), and it is not multicast/broadcast. 2820 */ 2821 if (hlen > 0 && 2822 (proto != IPPROTO_ICMP || 2823 is_icmp_query(ICMP(ulp))) && 2824 !(m->m_flags & (M_BCAST|M_MCAST)) && 2825 !IN_MULTICAST(ntohl(dst_ip.s_addr))) { 2826 send_reject(args, cmd->arg1, 2827 offset,ip_len); 2828 m = args->m; 2829 } 2830 /* FALLTHROUGH */ 2831 case O_DENY: 2832 retval = IP_FW_DENY; 2833 goto done; 2834 2835 case O_FORWARD_IP: 2836 if (args->eh) /* not valid on layer2 pkts */ 2837 break; 2838 if (!q || dyn_dir == MATCH_FORWARD) 2839 args->next_hop = 2840 &((ipfw_insn_sa *)cmd)->sa; 2841 retval = IP_FW_PASS; 2842 goto done; 2843 2844 case O_NETGRAPH: 2845 case O_NGTEE: 2846 args->rule = f; /* report matching rule */ 2847 args->cookie = cmd->arg1; 2848 retval = (cmd->opcode == O_NETGRAPH) ? 2849 IP_FW_NETGRAPH : IP_FW_NGTEE; 2850 goto done; 2851 2852 default: 2853 panic("-- unknown opcode %d\n", cmd->opcode); 2854 } /* end of switch() on opcodes */ 2855 2856 if (cmd->len & F_NOT) 2857 match = !match; 2858 2859 if (match) { 2860 if (cmd->len & F_OR) 2861 skip_or = 1; 2862 } else { 2863 if (!(cmd->len & F_OR)) /* not an OR block, */ 2864 break; /* try next rule */ 2865 } 2866 2867 } /* end of inner for, scan opcodes */ 2868 2869next_rule:; /* try next rule */ 2870 2871 } /* end of outer for, scan rules */ 2872 printf("ipfw: ouch!, skip past end of rules, denying packet\n"); 2873 IPFW_RUNLOCK(chain); 2874 return (IP_FW_DENY); 2875 2876done: 2877 /* Update statistics */ 2878 f->pcnt++; 2879 f->bcnt += pktlen; 2880 f->timestamp = time_second; 2881 IPFW_RUNLOCK(chain); 2882 return (retval); 2883 2884pullup_failed: 2885 if (fw_verbose) 2886 printf("ipfw: pullup failed\n"); 2887 return (IP_FW_DENY); 2888} 2889 2890/* 2891 * When a rule is added/deleted, clear the next_rule pointers in all rules. 2892 * These will be reconstructed on the fly as packets are matched. 2893 */ 2894static void 2895flush_rule_ptrs(struct ip_fw_chain *chain) 2896{ 2897 struct ip_fw *rule; 2898 2899 IPFW_WLOCK_ASSERT(chain); 2900 2901 for (rule = chain->rules; rule; rule = rule->next) 2902 rule->next_rule = NULL; 2903} 2904 2905/* 2906 * When pipes/queues are deleted, clear the "pipe_ptr" pointer to a given 2907 * pipe/queue, or to all of them (match == NULL). 2908 */ 2909void 2910flush_pipe_ptrs(struct dn_flow_set *match) 2911{ 2912 struct ip_fw *rule; 2913 2914 IPFW_WLOCK(&layer3_chain); 2915 for (rule = layer3_chain.rules; rule; rule = rule->next) { 2916 ipfw_insn_pipe *cmd = (ipfw_insn_pipe *)ACTION_PTR(rule); 2917 2918 if (cmd->o.opcode != O_PIPE && cmd->o.opcode != O_QUEUE) 2919 continue; 2920 /* 2921 * XXX Use bcmp/bzero to handle pipe_ptr to overcome 2922 * possible alignment problems on 64-bit architectures. 2923 * This code is seldom used so we do not worry too 2924 * much about efficiency. 2925 */ 2926 if (match == NULL || 2927 !bcmp(&cmd->pipe_ptr, &match, sizeof(match)) ) 2928 bzero(&cmd->pipe_ptr, sizeof(cmd->pipe_ptr)); 2929 } 2930 IPFW_WUNLOCK(&layer3_chain); 2931} 2932 2933/* 2934 * Add a new rule to the list. Copy the rule into a malloc'ed area, then 2935 * possibly create a rule number and add the rule to the list. 2936 * Update the rule_number in the input struct so the caller knows it as well. 2937 */ 2938static int 2939add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule) 2940{ 2941 struct ip_fw *rule, *f, *prev; 2942 int l = RULESIZE(input_rule); 2943 2944 if (chain->rules == NULL && input_rule->rulenum != IPFW_DEFAULT_RULE) 2945 return (EINVAL); 2946 2947 rule = malloc(l, M_IPFW, M_NOWAIT | M_ZERO); 2948 if (rule == NULL) 2949 return (ENOSPC); 2950 2951 bcopy(input_rule, rule, l); 2952 2953 rule->next = NULL; 2954 rule->next_rule = NULL; 2955 2956 rule->pcnt = 0; 2957 rule->bcnt = 0; 2958 rule->timestamp = 0; 2959 2960 IPFW_WLOCK(chain); 2961 2962 if (chain->rules == NULL) { /* default rule */ 2963 chain->rules = rule; 2964 goto done; 2965 } 2966 2967 /* 2968 * If rulenum is 0, find highest numbered rule before the 2969 * default rule, and add autoinc_step 2970 */ 2971 if (autoinc_step < 1) 2972 autoinc_step = 1; 2973 else if (autoinc_step > 1000) 2974 autoinc_step = 1000; 2975 if (rule->rulenum == 0) { 2976 /* 2977 * locate the highest numbered rule before default 2978 */ 2979 for (f = chain->rules; f; f = f->next) { 2980 if (f->rulenum == IPFW_DEFAULT_RULE) 2981 break; 2982 rule->rulenum = f->rulenum; 2983 } 2984 if (rule->rulenum < IPFW_DEFAULT_RULE - autoinc_step) 2985 rule->rulenum += autoinc_step; 2986 input_rule->rulenum = rule->rulenum; 2987 } 2988 2989 /* 2990 * Now insert the new rule in the right place in the sorted list. 2991 */ 2992 for (prev = NULL, f = chain->rules; f; prev = f, f = f->next) { 2993 if (f->rulenum > rule->rulenum) { /* found the location */ 2994 if (prev) { 2995 rule->next = f; 2996 prev->next = rule; 2997 } else { /* head insert */ 2998 rule->next = chain->rules; 2999 chain->rules = rule; 3000 } 3001 break; 3002 } 3003 } 3004 flush_rule_ptrs(chain); 3005done: 3006 static_count++; 3007 static_len += l; 3008 IPFW_WUNLOCK(chain); 3009 DEB(printf("ipfw: installed rule %d, static count now %d\n", 3010 rule->rulenum, static_count);) 3011 return (0); 3012} 3013 3014/** 3015 * Remove a static rule (including derived * dynamic rules) 3016 * and place it on the ``reap list'' for later reclamation. 3017 * The caller is in charge of clearing rule pointers to avoid 3018 * dangling pointers. 3019 * @return a pointer to the next entry. 3020 * Arguments are not checked, so they better be correct. 3021 */ 3022static struct ip_fw * 3023remove_rule(struct ip_fw_chain *chain, struct ip_fw *rule, struct ip_fw *prev) 3024{ 3025 struct ip_fw *n; 3026 int l = RULESIZE(rule); 3027 3028 IPFW_WLOCK_ASSERT(chain); 3029 3030 n = rule->next; 3031 IPFW_DYN_LOCK(); 3032 remove_dyn_rule(rule, NULL /* force removal */); 3033 IPFW_DYN_UNLOCK(); 3034 if (prev == NULL) 3035 chain->rules = n; 3036 else 3037 prev->next = n; 3038 static_count--; 3039 static_len -= l; 3040 3041 rule->next = chain->reap; 3042 chain->reap = rule; 3043 3044 return n; 3045} 3046 3047/** 3048 * Reclaim storage associated with a list of rules. This is 3049 * typically the list created using remove_rule. 3050 */ 3051static void 3052reap_rules(struct ip_fw *head) 3053{ 3054 struct ip_fw *rule; 3055 3056 while ((rule = head) != NULL) { 3057 head = head->next; 3058 if (DUMMYNET_LOADED) 3059 ip_dn_ruledel_ptr(rule); 3060 free(rule, M_IPFW); 3061 } 3062} 3063 3064/* 3065 * Remove all rules from a chain (except rules in set RESVD_SET 3066 * unless kill_default = 1). The caller is responsible for 3067 * reclaiming storage for the rules left in chain->reap. 3068 */ 3069static void 3070free_chain(struct ip_fw_chain *chain, int kill_default) 3071{ 3072 struct ip_fw *prev, *rule; 3073 3074 IPFW_WLOCK_ASSERT(chain); 3075 3076 flush_rule_ptrs(chain); /* more efficient to do outside the loop */ 3077 for (prev = NULL, rule = chain->rules; rule ; ) 3078 if (kill_default || rule->set != RESVD_SET) 3079 rule = remove_rule(chain, rule, prev); 3080 else { 3081 prev = rule; 3082 rule = rule->next; 3083 } 3084} 3085 3086/** 3087 * Remove all rules with given number, and also do set manipulation. 3088 * Assumes chain != NULL && *chain != NULL. 3089 * 3090 * The argument is an u_int32_t. The low 16 bit are the rule or set number, 3091 * the next 8 bits are the new set, the top 8 bits are the command: 3092 * 3093 * 0 delete rules with given number 3094 * 1 delete rules with given set number 3095 * 2 move rules with given number to new set 3096 * 3 move rules with given set number to new set 3097 * 4 swap sets with given numbers 3098 */ 3099static int 3100del_entry(struct ip_fw_chain *chain, u_int32_t arg) 3101{ 3102 struct ip_fw *prev = NULL, *rule; 3103 u_int16_t rulenum; /* rule or old_set */ 3104 u_int8_t cmd, new_set; 3105 3106 rulenum = arg & 0xffff; 3107 cmd = (arg >> 24) & 0xff; 3108 new_set = (arg >> 16) & 0xff; 3109 3110 if (cmd > 4) 3111 return EINVAL; 3112 if (new_set > RESVD_SET) 3113 return EINVAL; 3114 if (cmd == 0 || cmd == 2) { 3115 if (rulenum >= IPFW_DEFAULT_RULE) 3116 return EINVAL; 3117 } else { 3118 if (rulenum > RESVD_SET) /* old_set */ 3119 return EINVAL; 3120 } 3121 3122 IPFW_WLOCK(chain); 3123 rule = chain->rules; 3124 chain->reap = NULL; 3125 switch (cmd) { 3126 case 0: /* delete rules with given number */ 3127 /* 3128 * locate first rule to delete 3129 */ 3130 for (; rule->rulenum < rulenum; prev = rule, rule = rule->next) 3131 ; 3132 if (rule->rulenum != rulenum) { 3133 IPFW_WUNLOCK(chain); 3134 return EINVAL; 3135 } 3136 3137 /* 3138 * flush pointers outside the loop, then delete all matching 3139 * rules. prev remains the same throughout the cycle. 3140 */ 3141 flush_rule_ptrs(chain); 3142 while (rule->rulenum == rulenum) 3143 rule = remove_rule(chain, rule, prev); 3144 break; 3145 3146 case 1: /* delete all rules with given set number */ 3147 flush_rule_ptrs(chain); 3148 rule = chain->rules; 3149 while (rule->rulenum < IPFW_DEFAULT_RULE) 3150 if (rule->set == rulenum) 3151 rule = remove_rule(chain, rule, prev); 3152 else { 3153 prev = rule; 3154 rule = rule->next; 3155 } 3156 break; 3157 3158 case 2: /* move rules with given number to new set */ 3159 rule = chain->rules; 3160 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next) 3161 if (rule->rulenum == rulenum) 3162 rule->set = new_set; 3163 break; 3164 3165 case 3: /* move rules with given set number to new set */ 3166 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next) 3167 if (rule->set == rulenum) 3168 rule->set = new_set; 3169 break; 3170 3171 case 4: /* swap two sets */ 3172 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next) 3173 if (rule->set == rulenum) 3174 rule->set = new_set; 3175 else if (rule->set == new_set) 3176 rule->set = rulenum; 3177 break; 3178 } 3179 /* 3180 * Look for rules to reclaim. We grab the list before 3181 * releasing the lock then reclaim them w/o the lock to 3182 * avoid a LOR with dummynet. 3183 */ 3184 rule = chain->reap; 3185 chain->reap = NULL; 3186 IPFW_WUNLOCK(chain); 3187 if (rule) 3188 reap_rules(rule); 3189 return 0; 3190} 3191 3192/* 3193 * Clear counters for a specific rule. 3194 * The enclosing "table" is assumed locked. 3195 */ 3196static void 3197clear_counters(struct ip_fw *rule, int log_only) 3198{ 3199 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule); 3200 3201 if (log_only == 0) { 3202 rule->bcnt = rule->pcnt = 0; 3203 rule->timestamp = 0; 3204 } 3205 if (l->o.opcode == O_LOG) 3206 l->log_left = l->max_log; 3207} 3208 3209/** 3210 * Reset some or all counters on firewall rules. 3211 * @arg frwl is null to clear all entries, or contains a specific 3212 * rule number. 3213 * @arg log_only is 1 if we only want to reset logs, zero otherwise. 3214 */ 3215static int 3216zero_entry(struct ip_fw_chain *chain, int rulenum, int log_only) 3217{ 3218 struct ip_fw *rule; 3219 char *msg; 3220 3221 IPFW_WLOCK(chain); 3222 if (rulenum == 0) { 3223 norule_counter = 0; 3224 for (rule = chain->rules; rule; rule = rule->next) 3225 clear_counters(rule, log_only); 3226 msg = log_only ? "ipfw: All logging counts reset.\n" : 3227 "ipfw: Accounting cleared.\n"; 3228 } else { 3229 int cleared = 0; 3230 /* 3231 * We can have multiple rules with the same number, so we 3232 * need to clear them all. 3233 */ 3234 for (rule = chain->rules; rule; rule = rule->next) 3235 if (rule->rulenum == rulenum) { 3236 while (rule && rule->rulenum == rulenum) { 3237 clear_counters(rule, log_only); 3238 rule = rule->next; 3239 } 3240 cleared = 1; 3241 break; 3242 } 3243 if (!cleared) { /* we did not find any matching rules */ 3244 IPFW_WUNLOCK(chain); 3245 return (EINVAL); 3246 } 3247 msg = log_only ? "ipfw: Entry %d logging count reset.\n" : 3248 "ipfw: Entry %d cleared.\n"; 3249 } 3250 IPFW_WUNLOCK(chain); 3251 3252 if (fw_verbose) 3253 log(LOG_SECURITY | LOG_NOTICE, msg, rulenum); 3254 return (0); 3255} 3256 3257/* 3258 * Check validity of the structure before insert. 3259 * Fortunately rules are simple, so this mostly need to check rule sizes. 3260 */ 3261static int 3262check_ipfw_struct(struct ip_fw *rule, int size) 3263{ 3264 int l, cmdlen = 0; 3265 int have_action=0; 3266 ipfw_insn *cmd; 3267 3268 if (size < sizeof(*rule)) { 3269 printf("ipfw: rule too short\n"); 3270 return (EINVAL); 3271 } 3272 /* first, check for valid size */ 3273 l = RULESIZE(rule); 3274 if (l != size) { 3275 printf("ipfw: size mismatch (have %d want %d)\n", size, l); 3276 return (EINVAL); 3277 } 3278 if (rule->act_ofs >= rule->cmd_len) { 3279 printf("ipfw: bogus action offset (%u > %u)\n", 3280 rule->act_ofs, rule->cmd_len - 1); 3281 return (EINVAL); 3282 } 3283 /* 3284 * Now go for the individual checks. Very simple ones, basically only 3285 * instruction sizes. 3286 */ 3287 for (l = rule->cmd_len, cmd = rule->cmd ; 3288 l > 0 ; l -= cmdlen, cmd += cmdlen) { 3289 cmdlen = F_LEN(cmd); 3290 if (cmdlen > l) { 3291 printf("ipfw: opcode %d size truncated\n", 3292 cmd->opcode); 3293 return EINVAL; 3294 } 3295 DEB(printf("ipfw: opcode %d\n", cmd->opcode);) 3296 switch (cmd->opcode) { 3297 case O_PROBE_STATE: 3298 case O_KEEP_STATE: 3299 case O_PROTO: 3300 case O_IP_SRC_ME: 3301 case O_IP_DST_ME: 3302 case O_LAYER2: 3303 case O_IN: 3304 case O_FRAG: 3305 case O_DIVERTED: 3306 case O_IPOPT: 3307 case O_IPTOS: 3308 case O_IPPRECEDENCE: 3309 case O_IPVER: 3310 case O_TCPWIN: 3311 case O_TCPFLAGS: 3312 case O_TCPOPTS: 3313 case O_ESTAB: 3314 case O_VERREVPATH: 3315 case O_VERSRCREACH: 3316 case O_ANTISPOOF: 3317 case O_IPSEC: 3318 case O_IP6_SRC_ME: 3319 case O_IP6_DST_ME: 3320 case O_EXT_HDR: 3321 case O_IP6: 3322 if (cmdlen != F_INSN_SIZE(ipfw_insn)) 3323 goto bad_size; 3324 break; 3325 3326 case O_UID: 3327 case O_GID: 3328 case O_JAIL: 3329 case O_IP_SRC: 3330 case O_IP_DST: 3331 case O_TCPSEQ: 3332 case O_TCPACK: 3333 case O_PROB: 3334 case O_ICMPTYPE: 3335 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32)) 3336 goto bad_size; 3337 break; 3338 3339 case O_LIMIT: 3340 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit)) 3341 goto bad_size; 3342 break; 3343 3344 case O_LOG: 3345 if (cmdlen != F_INSN_SIZE(ipfw_insn_log)) 3346 goto bad_size; 3347 3348 ((ipfw_insn_log *)cmd)->log_left = 3349 ((ipfw_insn_log *)cmd)->max_log; 3350 3351 break; 3352 3353 case O_IP_SRC_MASK: 3354 case O_IP_DST_MASK: 3355 /* only odd command lengths */ 3356 if ( !(cmdlen & 1) || cmdlen > 31) 3357 goto bad_size; 3358 break; 3359 3360 case O_IP_SRC_SET: 3361 case O_IP_DST_SET: 3362 if (cmd->arg1 == 0 || cmd->arg1 > 256) { 3363 printf("ipfw: invalid set size %d\n", 3364 cmd->arg1); 3365 return EINVAL; 3366 } 3367 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 3368 (cmd->arg1+31)/32 ) 3369 goto bad_size; 3370 break; 3371 3372 case O_IP_SRC_LOOKUP: 3373 case O_IP_DST_LOOKUP: 3374 if (cmd->arg1 >= IPFW_TABLES_MAX) { 3375 printf("ipfw: invalid table number %d\n", 3376 cmd->arg1); 3377 return (EINVAL); 3378 } 3379 if (cmdlen != F_INSN_SIZE(ipfw_insn) && 3380 cmdlen != F_INSN_SIZE(ipfw_insn_u32)) 3381 goto bad_size; 3382 break; 3383 3384 case O_MACADDR2: 3385 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac)) 3386 goto bad_size; 3387 break; 3388 3389 case O_NOP: 3390 case O_IPID: 3391 case O_IPTTL: 3392 case O_IPLEN: 3393 case O_TCPDATALEN: 3394 if (cmdlen < 1 || cmdlen > 31) 3395 goto bad_size; 3396 break; 3397 3398 case O_MAC_TYPE: 3399 case O_IP_SRCPORT: 3400 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */ 3401 if (cmdlen < 2 || cmdlen > 31) 3402 goto bad_size; 3403 break; 3404 3405 case O_RECV: 3406 case O_XMIT: 3407 case O_VIA: 3408 if (cmdlen != F_INSN_SIZE(ipfw_insn_if)) 3409 goto bad_size; 3410 break; 3411 3412 case O_ALTQ: 3413 if (cmdlen != F_INSN_SIZE(ipfw_insn_altq)) 3414 goto bad_size; 3415 break; 3416 3417 case O_PIPE: 3418 case O_QUEUE: 3419 if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe)) 3420 goto bad_size; 3421 goto check_action; 3422 3423 case O_FORWARD_IP: 3424#ifdef IPFIREWALL_FORWARD 3425 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa)) 3426 goto bad_size; 3427 goto check_action; 3428#else 3429 return EINVAL; 3430#endif 3431 3432 case O_DIVERT: 3433 case O_TEE: 3434 if (ip_divert_ptr == NULL) 3435 return EINVAL; 3436 else 3437 goto check_size; 3438 case O_NETGRAPH: 3439 case O_NGTEE: 3440 if (!NG_IPFW_LOADED) 3441 return EINVAL; 3442 else 3443 goto check_size; 3444 case O_FORWARD_MAC: /* XXX not implemented yet */ 3445 case O_CHECK_STATE: 3446 case O_COUNT: 3447 case O_ACCEPT: 3448 case O_DENY: 3449 case O_REJECT: 3450 case O_SKIPTO: 3451check_size: 3452 if (cmdlen != F_INSN_SIZE(ipfw_insn)) 3453 goto bad_size; 3454check_action: 3455 if (have_action) { 3456 printf("ipfw: opcode %d, multiple actions" 3457 " not allowed\n", 3458 cmd->opcode); 3459 return EINVAL; 3460 } 3461 have_action = 1; 3462 if (l != cmdlen) { 3463 printf("ipfw: opcode %d, action must be" 3464 " last opcode\n", 3465 cmd->opcode); 3466 return EINVAL; 3467 } 3468 break; 3469 case O_IP6_SRC: 3470 case O_IP6_DST: 3471 if (cmdlen != F_INSN_SIZE(struct in6_addr) + 3472 F_INSN_SIZE(ipfw_insn)) 3473 goto bad_size; 3474 break; 3475 3476 case O_FLOW6ID: 3477 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 3478 ((ipfw_insn_u32 *)cmd)->o.arg1) 3479 goto bad_size; 3480 break; 3481 3482 case O_IP6_SRC_MASK: 3483 case O_IP6_DST_MASK: 3484 if ( !(cmdlen & 1) || cmdlen > 127) 3485 goto bad_size; 3486 break; 3487 case O_ICMP6TYPE: 3488 if( cmdlen != F_INSN_SIZE( ipfw_insn_icmp6 ) ) 3489 goto bad_size; 3490 break; 3491 3492 default: 3493 printf("ipfw: opcode %d, unknown opcode\n", 3494 cmd->opcode); 3495 return EINVAL; 3496 } 3497 } 3498 if (have_action == 0) { 3499 printf("ipfw: missing action\n"); 3500 return EINVAL; 3501 } 3502 return 0; 3503 3504bad_size: 3505 printf("ipfw: opcode %d size %d wrong\n", 3506 cmd->opcode, cmdlen); 3507 return EINVAL; 3508} 3509 3510/* 3511 * Copy the static and dynamic rules to the supplied buffer 3512 * and return the amount of space actually used. 3513 */ 3514static size_t 3515ipfw_getrules(struct ip_fw_chain *chain, void *buf, size_t space) 3516{ 3517 char *bp = buf; 3518 char *ep = bp + space; 3519 struct ip_fw *rule; 3520 int i; 3521 3522 /* XXX this can take a long time and locking will block packet flow */ 3523 IPFW_RLOCK(chain); 3524 for (rule = chain->rules; rule ; rule = rule->next) { 3525 /* 3526 * Verify the entry fits in the buffer in case the 3527 * rules changed between calculating buffer space and 3528 * now. This would be better done using a generation 3529 * number but should suffice for now. 3530 */ 3531 i = RULESIZE(rule); 3532 if (bp + i <= ep) { 3533 bcopy(rule, bp, i); 3534 bcopy(&set_disable, &(((struct ip_fw *)bp)->next_rule), 3535 sizeof(set_disable)); 3536 bp += i; 3537 } 3538 } 3539 IPFW_RUNLOCK(chain); 3540 if (ipfw_dyn_v) { 3541 ipfw_dyn_rule *p, *last = NULL; 3542 3543 IPFW_DYN_LOCK(); 3544 for (i = 0 ; i < curr_dyn_buckets; i++) 3545 for (p = ipfw_dyn_v[i] ; p != NULL; p = p->next) { 3546 if (bp + sizeof *p <= ep) { 3547 ipfw_dyn_rule *dst = 3548 (ipfw_dyn_rule *)bp; 3549 bcopy(p, dst, sizeof *p); 3550 bcopy(&(p->rule->rulenum), &(dst->rule), 3551 sizeof(p->rule->rulenum)); 3552 /* 3553 * store a non-null value in "next". 3554 * The userland code will interpret a 3555 * NULL here as a marker 3556 * for the last dynamic rule. 3557 */ 3558 bcopy(&dst, &dst->next, sizeof(dst)); 3559 last = dst; 3560 dst->expire = 3561 TIME_LEQ(dst->expire, time_second) ? 3562 0 : dst->expire - time_second ; 3563 bp += sizeof(ipfw_dyn_rule); 3564 } 3565 } 3566 IPFW_DYN_UNLOCK(); 3567 if (last != NULL) /* mark last dynamic rule */ 3568 bzero(&last->next, sizeof(last)); 3569 } 3570 return (bp - (char *)buf); 3571} 3572 3573 3574/** 3575 * {set|get}sockopt parser. 3576 */ 3577static int 3578ipfw_ctl(struct sockopt *sopt) 3579{ 3580#define RULE_MAXSIZE (256*sizeof(u_int32_t)) 3581 int error, rule_num; 3582 size_t size; 3583 struct ip_fw *buf, *rule; 3584 u_int32_t rulenum[2]; 3585 3586 error = suser(sopt->sopt_td); 3587 if (error) 3588 return (error); 3589 3590 /* 3591 * Disallow modifications in really-really secure mode, but still allow 3592 * the logging counters to be reset. 3593 */ 3594 if (sopt->sopt_name == IP_FW_ADD || 3595 (sopt->sopt_dir == SOPT_SET && sopt->sopt_name != IP_FW_RESETLOG)) { 3596#if __FreeBSD_version >= 500034 3597 error = securelevel_ge(sopt->sopt_td->td_ucred, 3); 3598 if (error) 3599 return (error); 3600#else /* FreeBSD 4.x */ 3601 if (securelevel >= 3) 3602 return (EPERM); 3603#endif 3604 } 3605 3606 error = 0; 3607 3608 switch (sopt->sopt_name) { 3609 case IP_FW_GET: 3610 /* 3611 * pass up a copy of the current rules. Static rules 3612 * come first (the last of which has number IPFW_DEFAULT_RULE), 3613 * followed by a possibly empty list of dynamic rule. 3614 * The last dynamic rule has NULL in the "next" field. 3615 * 3616 * Note that the calculated size is used to bound the 3617 * amount of data returned to the user. The rule set may 3618 * change between calculating the size and returning the 3619 * data in which case we'll just return what fits. 3620 */ 3621 size = static_len; /* size of static rules */ 3622 if (ipfw_dyn_v) /* add size of dyn.rules */ 3623 size += (dyn_count * sizeof(ipfw_dyn_rule)); 3624 3625 /* 3626 * XXX todo: if the user passes a short length just to know 3627 * how much room is needed, do not bother filling up the 3628 * buffer, just jump to the sooptcopyout. 3629 */ 3630 buf = malloc(size, M_TEMP, M_WAITOK); 3631 error = sooptcopyout(sopt, buf, 3632 ipfw_getrules(&layer3_chain, buf, size)); 3633 free(buf, M_TEMP); 3634 break; 3635 3636 case IP_FW_FLUSH: 3637 /* 3638 * Normally we cannot release the lock on each iteration. 3639 * We could do it here only because we start from the head all 3640 * the times so there is no risk of missing some entries. 3641 * On the other hand, the risk is that we end up with 3642 * a very inconsistent ruleset, so better keep the lock 3643 * around the whole cycle. 3644 * 3645 * XXX this code can be improved by resetting the head of 3646 * the list to point to the default rule, and then freeing 3647 * the old list without the need for a lock. 3648 */ 3649 3650 IPFW_WLOCK(&layer3_chain); 3651 layer3_chain.reap = NULL; 3652 free_chain(&layer3_chain, 0 /* keep default rule */); 3653 rule = layer3_chain.reap, layer3_chain.reap = NULL; 3654 IPFW_WUNLOCK(&layer3_chain); 3655 if (layer3_chain.reap != NULL) 3656 reap_rules(rule); 3657 break; 3658 3659 case IP_FW_ADD: 3660 rule = malloc(RULE_MAXSIZE, M_TEMP, M_WAITOK); 3661 error = sooptcopyin(sopt, rule, RULE_MAXSIZE, 3662 sizeof(struct ip_fw) ); 3663 if (error == 0) 3664 error = check_ipfw_struct(rule, sopt->sopt_valsize); 3665 if (error == 0) { 3666 error = add_rule(&layer3_chain, rule); 3667 size = RULESIZE(rule); 3668 if (!error && sopt->sopt_dir == SOPT_GET) 3669 error = sooptcopyout(sopt, rule, size); 3670 } 3671 free(rule, M_TEMP); 3672 break; 3673 3674 case IP_FW_DEL: 3675 /* 3676 * IP_FW_DEL is used for deleting single rules or sets, 3677 * and (ab)used to atomically manipulate sets. Argument size 3678 * is used to distinguish between the two: 3679 * sizeof(u_int32_t) 3680 * delete single rule or set of rules, 3681 * or reassign rules (or sets) to a different set. 3682 * 2*sizeof(u_int32_t) 3683 * atomic disable/enable sets. 3684 * first u_int32_t contains sets to be disabled, 3685 * second u_int32_t contains sets to be enabled. 3686 */ 3687 error = sooptcopyin(sopt, rulenum, 3688 2*sizeof(u_int32_t), sizeof(u_int32_t)); 3689 if (error) 3690 break; 3691 size = sopt->sopt_valsize; 3692 if (size == sizeof(u_int32_t)) /* delete or reassign */ 3693 error = del_entry(&layer3_chain, rulenum[0]); 3694 else if (size == 2*sizeof(u_int32_t)) /* set enable/disable */ 3695 set_disable = 3696 (set_disable | rulenum[0]) & ~rulenum[1] & 3697 ~(1<<RESVD_SET); /* set RESVD_SET always enabled */ 3698 else 3699 error = EINVAL; 3700 break; 3701 3702 case IP_FW_ZERO: 3703 case IP_FW_RESETLOG: /* argument is an int, the rule number */ 3704 rule_num = 0; 3705 if (sopt->sopt_val != 0) { 3706 error = sooptcopyin(sopt, &rule_num, 3707 sizeof(int), sizeof(int)); 3708 if (error) 3709 break; 3710 } 3711 error = zero_entry(&layer3_chain, rule_num, 3712 sopt->sopt_name == IP_FW_RESETLOG); 3713 break; 3714 3715 case IP_FW_TABLE_ADD: 3716 { 3717 ipfw_table_entry ent; 3718 3719 error = sooptcopyin(sopt, &ent, 3720 sizeof(ent), sizeof(ent)); 3721 if (error) 3722 break; 3723 error = add_table_entry(ent.tbl, ent.addr, 3724 ent.masklen, ent.value); 3725 } 3726 break; 3727 3728 case IP_FW_TABLE_DEL: 3729 { 3730 ipfw_table_entry ent; 3731 3732 error = sooptcopyin(sopt, &ent, 3733 sizeof(ent), sizeof(ent)); 3734 if (error) 3735 break; 3736 error = del_table_entry(ent.tbl, ent.addr, ent.masklen); 3737 } 3738 break; 3739 3740 case IP_FW_TABLE_FLUSH: 3741 { 3742 u_int16_t tbl; 3743 3744 error = sooptcopyin(sopt, &tbl, 3745 sizeof(tbl), sizeof(tbl)); 3746 if (error) 3747 break; 3748 error = flush_table(tbl); 3749 } 3750 break; 3751 3752 case IP_FW_TABLE_GETSIZE: 3753 { 3754 u_int32_t tbl, cnt; 3755 3756 if ((error = sooptcopyin(sopt, &tbl, sizeof(tbl), 3757 sizeof(tbl)))) 3758 break; 3759 if ((error = count_table(tbl, &cnt))) 3760 break; 3761 error = sooptcopyout(sopt, &cnt, sizeof(cnt)); 3762 } 3763 break; 3764 3765 case IP_FW_TABLE_LIST: 3766 { 3767 ipfw_table *tbl; 3768 3769 if (sopt->sopt_valsize < sizeof(*tbl)) { 3770 error = EINVAL; 3771 break; 3772 } 3773 size = sopt->sopt_valsize; 3774 tbl = malloc(size, M_TEMP, M_WAITOK); 3775 if (tbl == NULL) { 3776 error = ENOMEM; 3777 break; 3778 } 3779 error = sooptcopyin(sopt, tbl, size, sizeof(*tbl)); 3780 if (error) { 3781 free(tbl, M_TEMP); 3782 break; 3783 } 3784 tbl->size = (size - sizeof(*tbl)) / 3785 sizeof(ipfw_table_entry); 3786 error = dump_table(tbl); 3787 if (error) { 3788 free(tbl, M_TEMP); 3789 break; 3790 } 3791 error = sooptcopyout(sopt, tbl, size); 3792 free(tbl, M_TEMP); 3793 } 3794 break; 3795 3796 default: 3797 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name); 3798 error = EINVAL; 3799 } 3800 3801 return (error); 3802#undef RULE_MAXSIZE 3803} 3804 3805/** 3806 * dummynet needs a reference to the default rule, because rules can be 3807 * deleted while packets hold a reference to them. When this happens, 3808 * dummynet changes the reference to the default rule (it could well be a 3809 * NULL pointer, but this way we do not need to check for the special 3810 * case, plus here he have info on the default behaviour). 3811 */ 3812struct ip_fw *ip_fw_default_rule; 3813 3814/* 3815 * This procedure is only used to handle keepalives. It is invoked 3816 * every dyn_keepalive_period 3817 */ 3818static void 3819ipfw_tick(void * __unused unused) 3820{ 3821 int i; 3822 ipfw_dyn_rule *q; 3823 3824 if (dyn_keepalive == 0 || ipfw_dyn_v == NULL || dyn_count == 0) 3825 goto done; 3826 3827 IPFW_DYN_LOCK(); 3828 for (i = 0 ; i < curr_dyn_buckets ; i++) { 3829 for (q = ipfw_dyn_v[i] ; q ; q = q->next ) { 3830 if (q->dyn_type == O_LIMIT_PARENT) 3831 continue; 3832 if (q->id.proto != IPPROTO_TCP) 3833 continue; 3834 if ( (q->state & BOTH_SYN) != BOTH_SYN) 3835 continue; 3836 if (TIME_LEQ( time_second+dyn_keepalive_interval, 3837 q->expire)) 3838 continue; /* too early */ 3839 if (TIME_LEQ(q->expire, time_second)) 3840 continue; /* too late, rule expired */ 3841 3842 send_pkt(&(q->id), q->ack_rev - 1, q->ack_fwd, TH_SYN); 3843 send_pkt(&(q->id), q->ack_fwd - 1, q->ack_rev, 0); 3844 } 3845 } 3846 IPFW_DYN_UNLOCK(); 3847done: 3848 callout_reset(&ipfw_timeout, dyn_keepalive_period*hz, ipfw_tick, NULL); 3849} 3850 3851int 3852ipfw_init(void) 3853{ 3854 struct ip_fw default_rule; 3855 int error; 3856 3857 layer3_chain.rules = NULL; 3858 layer3_chain.want_write = 0; 3859 layer3_chain.busy_count = 0; 3860 cv_init(&layer3_chain.cv, "Condition variable for IPFW rw locks"); 3861 IPFW_LOCK_INIT(&layer3_chain); 3862 ipfw_dyn_rule_zone = uma_zcreate("IPFW dynamic rule zone", 3863 sizeof(ipfw_dyn_rule), NULL, NULL, NULL, NULL, 3864 UMA_ALIGN_PTR, 0); 3865 IPFW_DYN_LOCK_INIT(); 3866 callout_init(&ipfw_timeout, NET_CALLOUT_MPSAFE); 3867 3868 bzero(&default_rule, sizeof default_rule); 3869 3870 default_rule.act_ofs = 0; 3871 default_rule.rulenum = IPFW_DEFAULT_RULE; 3872 default_rule.cmd_len = 1; 3873 default_rule.set = RESVD_SET; 3874 3875 default_rule.cmd[0].len = 1; 3876 default_rule.cmd[0].opcode = 3877#ifdef IPFIREWALL_DEFAULT_TO_ACCEPT 3878 1 ? O_ACCEPT : 3879#endif 3880 O_DENY; 3881 3882 error = add_rule(&layer3_chain, &default_rule); 3883 if (error != 0) { 3884 printf("ipfw2: error %u initializing default rule " 3885 "(support disabled)\n", error); 3886 IPFW_DYN_LOCK_DESTROY(); 3887 IPFW_LOCK_DESTROY(&layer3_chain); 3888 return (error); 3889 } 3890 3891 ip_fw_default_rule = layer3_chain.rules; 3892 printf("ipfw2 (+ipv6) initialized, divert %s, " 3893 "rule-based forwarding " 3894#ifdef IPFIREWALL_FORWARD 3895 "enabled, " 3896#else 3897 "disabled, " 3898#endif 3899 "default to %s, logging ", 3900#ifdef IPDIVERT 3901 "enabled", 3902#else 3903 "loadable", 3904#endif 3905 default_rule.cmd[0].opcode == O_ACCEPT ? "accept" : "deny"); 3906 3907#ifdef IPFIREWALL_VERBOSE 3908 fw_verbose = 1; 3909#endif 3910#ifdef IPFIREWALL_VERBOSE_LIMIT 3911 verbose_limit = IPFIREWALL_VERBOSE_LIMIT; 3912#endif 3913 if (fw_verbose == 0) 3914 printf("disabled\n"); 3915 else if (verbose_limit == 0) 3916 printf("unlimited\n"); 3917 else 3918 printf("limited to %d packets/entry by default\n", 3919 verbose_limit); 3920 3921 init_tables(); 3922 ip_fw_ctl_ptr = ipfw_ctl; 3923 ip_fw_chk_ptr = ipfw_chk; 3924 callout_reset(&ipfw_timeout, hz, ipfw_tick, NULL); 3925 3926 return (0); 3927} 3928 3929void 3930ipfw_destroy(void) 3931{ 3932 struct ip_fw *reap; 3933 3934 ip_fw_chk_ptr = NULL; 3935 ip_fw_ctl_ptr = NULL; 3936 callout_drain(&ipfw_timeout); 3937 IPFW_WLOCK(&layer3_chain); 3938 layer3_chain.reap = NULL; 3939 free_chain(&layer3_chain, 1 /* kill default rule */); 3940 reap = layer3_chain.reap, layer3_chain.reap = NULL; 3941 IPFW_WUNLOCK(&layer3_chain); 3942 if (reap != NULL) 3943 reap_rules(reap); 3944 flush_tables(); 3945 IPFW_DYN_LOCK_DESTROY(); 3946 uma_zdestroy(ipfw_dyn_rule_zone); 3947 IPFW_LOCK_DESTROY(&layer3_chain); 3948 printf("IP firewall unloaded\n"); 3949} 3950 3951#endif /* IPFW2 */ 3952