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