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