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