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