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