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