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