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