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 struct ifnet *oif; 1084 int lookupflags; 1085 int match; 1086 1087 id = &args->f_id; 1088 inp = args->inp; 1089 oif = args->oif; 1090 1091 /* 1092 * Check to see if the UDP or TCP stack supplied us with 1093 * the PCB. If so, rather then holding a lock and looking 1094 * up the PCB, we can use the one that was supplied. 1095 */ 1096 if (inp && *ugid_lookupp == 0) { 1097 INP_LOCK_ASSERT(inp); 1098 if (inp->inp_socket != NULL) { 1099 *uc = crhold(inp->inp_cred); 1100 *ugid_lookupp = 1; 1101 } else 1102 *ugid_lookupp = -1; 1103 } 1104 /* 1105 * If we have already been here and the packet has no 1106 * PCB entry associated with it, then we can safely 1107 * assume that this is a no match. 1108 */ 1109 if (*ugid_lookupp == -1) 1110 return (0); 1111 if (id->proto == IPPROTO_TCP) { 1112 lookupflags = 0; 1113 pi = &V_tcbinfo; 1114 } else if (id->proto == IPPROTO_UDP) { 1115 lookupflags = INPLOOKUP_WILDCARD; 1116 pi = &V_udbinfo; 1117 } else if (id->proto == IPPROTO_UDPLITE) { 1118 lookupflags = INPLOOKUP_WILDCARD; 1119 pi = &V_ulitecbinfo; 1120 } else 1121 return 0; 1122 lookupflags |= INPLOOKUP_RLOCKPCB; 1123 match = 0; 1124 if (*ugid_lookupp == 0) { 1125 if (id->addr_type == 6) { 1126 #ifdef INET6 1127 if (oif == NULL) 1128 pcb = in6_pcblookup_mbuf(pi, 1129 &id->src_ip6, htons(id->src_port), 1130 &id->dst_ip6, htons(id->dst_port), 1131 lookupflags, oif, args->m); 1132 else 1133 pcb = in6_pcblookup_mbuf(pi, 1134 &id->dst_ip6, htons(id->dst_port), 1135 &id->src_ip6, htons(id->src_port), 1136 lookupflags, oif, args->m); 1137 #else 1138 *ugid_lookupp = -1; 1139 return (0); 1140 #endif 1141 } else { 1142 src_ip.s_addr = htonl(id->src_ip); 1143 dst_ip.s_addr = htonl(id->dst_ip); 1144 if (oif == NULL) 1145 pcb = in_pcblookup_mbuf(pi, 1146 src_ip, htons(id->src_port), 1147 dst_ip, htons(id->dst_port), 1148 lookupflags, oif, args->m); 1149 else 1150 pcb = in_pcblookup_mbuf(pi, 1151 dst_ip, htons(id->dst_port), 1152 src_ip, htons(id->src_port), 1153 lookupflags, oif, args->m); 1154 } 1155 if (pcb != NULL) { 1156 INP_RLOCK_ASSERT(pcb); 1157 *uc = crhold(pcb->inp_cred); 1158 *ugid_lookupp = 1; 1159 INP_RUNLOCK(pcb); 1160 } 1161 if (*ugid_lookupp == 0) { 1162 /* 1163 * We tried and failed, set the variable to -1 1164 * so we will not try again on this packet. 1165 */ 1166 *ugid_lookupp = -1; 1167 return (0); 1168 } 1169 } 1170 if (insn->o.opcode == O_UID) 1171 match = ((*uc)->cr_uid == (uid_t)insn->d[0]); 1172 else if (insn->o.opcode == O_GID) 1173 match = groupmember((gid_t)insn->d[0], *uc); 1174 else if (insn->o.opcode == O_JAIL) 1175 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]); 1176 return (match); 1177 #endif /* __FreeBSD__ */ 1178 #endif /* not supported in userspace */ 1179 } 1180 1181 /* 1182 * Helper function to set args with info on the rule after the matching 1183 * one. slot is precise, whereas we guess rule_id as they are 1184 * assigned sequentially. 1185 */ 1186 static inline void 1187 set_match(struct ip_fw_args *args, int slot, 1188 struct ip_fw_chain *chain) 1189 { 1190 args->rule.chain_id = chain->id; 1191 args->rule.slot = slot + 1; /* we use 0 as a marker */ 1192 args->rule.rule_id = 1 + chain->map[slot]->id; 1193 args->rule.rulenum = chain->map[slot]->rulenum; 1194 args->flags |= IPFW_ARGS_REF; 1195 } 1196 1197 #ifndef LINEAR_SKIPTO 1198 /* 1199 * Helper function to enable cached rule lookups using 1200 * cached_id and cached_pos fields in ipfw rule. 1201 */ 1202 static int 1203 jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num, 1204 int tablearg, int jump_backwards) 1205 { 1206 int f_pos; 1207 1208 /* If possible use cached f_pos (in f->cached_pos), 1209 * whose version is written in f->cached_id 1210 * (horrible hacks to avoid changing the ABI). 1211 */ 1212 if (num != IP_FW_TARG && f->cached_id == chain->id) 1213 f_pos = f->cached_pos; 1214 else { 1215 int i = IP_FW_ARG_TABLEARG(chain, num, skipto); 1216 /* make sure we do not jump backward */ 1217 if (jump_backwards == 0 && i <= f->rulenum) 1218 i = f->rulenum + 1; 1219 if (chain->idxmap != NULL) 1220 f_pos = chain->idxmap[i]; 1221 else 1222 f_pos = ipfw_find_rule(chain, i, 0); 1223 /* update the cache */ 1224 if (num != IP_FW_TARG) { 1225 f->cached_id = chain->id; 1226 f->cached_pos = f_pos; 1227 } 1228 } 1229 1230 return (f_pos); 1231 } 1232 #else 1233 /* 1234 * Helper function to enable real fast rule lookups. 1235 */ 1236 static int 1237 jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num, 1238 int tablearg, int jump_backwards) 1239 { 1240 int f_pos; 1241 1242 num = IP_FW_ARG_TABLEARG(chain, num, skipto); 1243 /* make sure we do not jump backward */ 1244 if (jump_backwards == 0 && num <= f->rulenum) 1245 num = f->rulenum + 1; 1246 f_pos = chain->idxmap[num]; 1247 1248 return (f_pos); 1249 } 1250 #endif 1251 1252 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f) 1253 /* 1254 * The main check routine for the firewall. 1255 * 1256 * All arguments are in args so we can modify them and return them 1257 * back to the caller. 1258 * 1259 * Parameters: 1260 * 1261 * args->m (in/out) The packet; we set to NULL when/if we nuke it. 1262 * Starts with the IP header. 1263 * args->eh (in) Mac header if present, NULL for layer3 packet. 1264 * args->L3offset Number of bytes bypassed if we came from L2. 1265 * e.g. often sizeof(eh) ** NOTYET ** 1266 * args->oif Outgoing interface, NULL if packet is incoming. 1267 * The incoming interface is in the mbuf. (in) 1268 * args->divert_rule (in/out) 1269 * Skip up to the first rule past this rule number; 1270 * upon return, non-zero port number for divert or tee. 1271 * 1272 * args->rule Pointer to the last matching rule (in/out) 1273 * args->next_hop Socket we are forwarding to (out). 1274 * args->next_hop6 IPv6 next hop we are forwarding to (out). 1275 * args->f_id Addresses grabbed from the packet (out) 1276 * args->rule.info a cookie depending on rule action 1277 * 1278 * Return value: 1279 * 1280 * IP_FW_PASS the packet must be accepted 1281 * IP_FW_DENY the packet must be dropped 1282 * IP_FW_DIVERT divert packet, port in m_tag 1283 * IP_FW_TEE tee packet, port in m_tag 1284 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie 1285 * IP_FW_NETGRAPH into netgraph, cookie args->cookie 1286 * args->rule contains the matching rule, 1287 * args->rule.info has additional information. 1288 * 1289 */ 1290 int 1291 ipfw_chk(struct ip_fw_args *args) 1292 { 1293 1294 /* 1295 * Local variables holding state while processing a packet: 1296 * 1297 * IMPORTANT NOTE: to speed up the processing of rules, there 1298 * are some assumption on the values of the variables, which 1299 * are documented here. Should you change them, please check 1300 * the implementation of the various instructions to make sure 1301 * that they still work. 1302 * 1303 * args->eh The MAC header. It is non-null for a layer2 1304 * packet, it is NULL for a layer-3 packet. 1305 * **notyet** 1306 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header. 1307 * 1308 * m | args->m Pointer to the mbuf, as received from the caller. 1309 * It may change if ipfw_chk() does an m_pullup, or if it 1310 * consumes the packet because it calls send_reject(). 1311 * XXX This has to change, so that ipfw_chk() never modifies 1312 * or consumes the buffer. 1313 * ip is the beginning of the ip(4 or 6) header. 1314 * Calculated by adding the L3offset to the start of data. 1315 * (Until we start using L3offset, the packet is 1316 * supposed to start with the ip header). 1317 */ 1318 struct mbuf *m = args->m; 1319 struct ip *ip = mtod(m, struct ip *); 1320 1321 /* 1322 * For rules which contain uid/gid or jail constraints, cache 1323 * a copy of the users credentials after the pcb lookup has been 1324 * executed. This will speed up the processing of rules with 1325 * these types of constraints, as well as decrease contention 1326 * on pcb related locks. 1327 */ 1328 #ifndef __FreeBSD__ 1329 struct bsd_ucred ucred_cache; 1330 #else 1331 struct ucred *ucred_cache = NULL; 1332 #endif 1333 int ucred_lookup = 0; 1334 1335 /* 1336 * oif | args->oif If NULL, ipfw_chk has been called on the 1337 * inbound path (ether_input, ip_input). 1338 * If non-NULL, ipfw_chk has been called on the outbound path 1339 * (ether_output, ip_output). 1340 */ 1341 struct ifnet *oif = args->oif; 1342 1343 int f_pos = 0; /* index of current rule in the array */ 1344 int retval = 0; 1345 1346 /* 1347 * hlen The length of the IP header. 1348 */ 1349 u_int hlen = 0; /* hlen >0 means we have an IP pkt */ 1350 1351 /* 1352 * offset The offset of a fragment. offset != 0 means that 1353 * we have a fragment at this offset of an IPv4 packet. 1354 * offset == 0 means that (if this is an IPv4 packet) 1355 * this is the first or only fragment. 1356 * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header 1357 * or there is a single packet fragment (fragment header added 1358 * without needed). We will treat a single packet fragment as if 1359 * there was no fragment header (or log/block depending on the 1360 * V_fw_permit_single_frag6 sysctl setting). 1361 */ 1362 u_short offset = 0; 1363 u_short ip6f_mf = 0; 1364 1365 /* 1366 * Local copies of addresses. They are only valid if we have 1367 * an IP packet. 1368 * 1369 * proto The protocol. Set to 0 for non-ip packets, 1370 * or to the protocol read from the packet otherwise. 1371 * proto != 0 means that we have an IPv4 packet. 1372 * 1373 * src_port, dst_port port numbers, in HOST format. Only 1374 * valid for TCP and UDP packets. 1375 * 1376 * src_ip, dst_ip ip addresses, in NETWORK format. 1377 * Only valid for IPv4 packets. 1378 */ 1379 uint8_t proto; 1380 uint16_t src_port, dst_port; /* NOTE: host format */ 1381 struct in_addr src_ip, dst_ip; /* NOTE: network format */ 1382 int iplen = 0; 1383 int pktlen; 1384 uint16_t etype; /* Host order stored ether type */ 1385 1386 struct ipfw_dyn_info dyn_info; 1387 struct ip_fw *q = NULL; 1388 struct ip_fw_chain *chain = &V_layer3_chain; 1389 1390 /* 1391 * We store in ulp a pointer to the upper layer protocol header. 1392 * In the ipv4 case this is easy to determine from the header, 1393 * but for ipv6 we might have some additional headers in the middle. 1394 * ulp is NULL if not found. 1395 */ 1396 void *ulp = NULL; /* upper layer protocol pointer. */ 1397 1398 /* XXX ipv6 variables */ 1399 int is_ipv6 = 0; 1400 uint8_t icmp6_type = 0; 1401 uint16_t ext_hd = 0; /* bits vector for extension header filtering */ 1402 /* end of ipv6 variables */ 1403 1404 int is_ipv4 = 0; 1405 1406 int done = 0; /* flag to exit the outer loop */ 1407 IPFW_RLOCK_TRACKER; 1408 1409 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready)) 1410 return (IP_FW_PASS); /* accept */ 1411 1412 dst_ip.s_addr = 0; /* make sure it is initialized */ 1413 src_ip.s_addr = 0; /* make sure it is initialized */ 1414 src_port = dst_port = 0; 1415 pktlen = m->m_pkthdr.len; 1416 1417 DYN_INFO_INIT(&dyn_info); 1418 /* 1419 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, 1420 * then it sets p to point at the offset "len" in the mbuf. WARNING: the 1421 * pointer might become stale after other pullups (but we never use it 1422 * this way). 1423 */ 1424 #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T)) 1425 #define PULLUP_LEN(_len, p, T) \ 1426 do { \ 1427 int x = (_len) + T; \ 1428 if ((m)->m_len < x) { \ 1429 args->m = m = m_pullup(m, x); \ 1430 if (m == NULL) \ 1431 goto pullup_failed; \ 1432 } \ 1433 p = (mtod(m, char *) + (_len)); \ 1434 } while (0) 1435 1436 /* 1437 * if we have an ether header, 1438 */ 1439 if (args->flags & IPFW_ARGS_ETHER) 1440 etype = ntohs(args->eh->ether_type); 1441 else 1442 etype = 0; 1443 1444 /* Identify IP packets and fill up variables. */ 1445 if (pktlen >= sizeof(struct ip6_hdr) && 1446 (etype == 0 || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) { 1447 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip; 1448 1449 is_ipv6 = 1; 1450 hlen = sizeof(struct ip6_hdr); 1451 proto = ip6->ip6_nxt; 1452 /* Search extension headers to find upper layer protocols */ 1453 while (ulp == NULL && offset == 0) { 1454 switch (proto) { 1455 case IPPROTO_ICMPV6: 1456 PULLUP_TO(hlen, ulp, struct icmp6_hdr); 1457 icmp6_type = ICMP6(ulp)->icmp6_type; 1458 break; 1459 1460 case IPPROTO_TCP: 1461 PULLUP_TO(hlen, ulp, struct tcphdr); 1462 dst_port = TCP(ulp)->th_dport; 1463 src_port = TCP(ulp)->th_sport; 1464 /* save flags for dynamic rules */ 1465 args->f_id._flags = TCP(ulp)->th_flags; 1466 break; 1467 1468 case IPPROTO_SCTP: 1469 if (pktlen >= hlen + sizeof(struct sctphdr) + 1470 sizeof(struct sctp_chunkhdr) + 1471 offsetof(struct sctp_init, a_rwnd)) 1472 PULLUP_LEN(hlen, ulp, 1473 sizeof(struct sctphdr) + 1474 sizeof(struct sctp_chunkhdr) + 1475 offsetof(struct sctp_init, a_rwnd)); 1476 else if (pktlen >= hlen + sizeof(struct sctphdr)) 1477 PULLUP_LEN(hlen, ulp, pktlen - hlen); 1478 else 1479 PULLUP_LEN(hlen, ulp, 1480 sizeof(struct sctphdr)); 1481 src_port = SCTP(ulp)->src_port; 1482 dst_port = SCTP(ulp)->dest_port; 1483 break; 1484 1485 case IPPROTO_UDP: 1486 case IPPROTO_UDPLITE: 1487 PULLUP_TO(hlen, ulp, struct udphdr); 1488 dst_port = UDP(ulp)->uh_dport; 1489 src_port = UDP(ulp)->uh_sport; 1490 break; 1491 1492 case IPPROTO_HOPOPTS: /* RFC 2460 */ 1493 PULLUP_TO(hlen, ulp, struct ip6_hbh); 1494 ext_hd |= EXT_HOPOPTS; 1495 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 1496 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 1497 ulp = NULL; 1498 break; 1499 1500 case IPPROTO_ROUTING: /* RFC 2460 */ 1501 PULLUP_TO(hlen, ulp, struct ip6_rthdr); 1502 switch (((struct ip6_rthdr *)ulp)->ip6r_type) { 1503 case 0: 1504 ext_hd |= EXT_RTHDR0; 1505 break; 1506 case 2: 1507 ext_hd |= EXT_RTHDR2; 1508 break; 1509 default: 1510 if (V_fw_verbose) 1511 printf("IPFW2: IPV6 - Unknown " 1512 "Routing Header type(%d)\n", 1513 ((struct ip6_rthdr *) 1514 ulp)->ip6r_type); 1515 if (V_fw_deny_unknown_exthdrs) 1516 return (IP_FW_DENY); 1517 break; 1518 } 1519 ext_hd |= EXT_ROUTING; 1520 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3; 1521 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; 1522 ulp = NULL; 1523 break; 1524 1525 case IPPROTO_FRAGMENT: /* RFC 2460 */ 1526 PULLUP_TO(hlen, ulp, struct ip6_frag); 1527 ext_hd |= EXT_FRAGMENT; 1528 hlen += sizeof (struct ip6_frag); 1529 proto = ((struct ip6_frag *)ulp)->ip6f_nxt; 1530 offset = ((struct ip6_frag *)ulp)->ip6f_offlg & 1531 IP6F_OFF_MASK; 1532 ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg & 1533 IP6F_MORE_FRAG; 1534 if (V_fw_permit_single_frag6 == 0 && 1535 offset == 0 && ip6f_mf == 0) { 1536 if (V_fw_verbose) 1537 printf("IPFW2: IPV6 - Invalid " 1538 "Fragment Header\n"); 1539 if (V_fw_deny_unknown_exthdrs) 1540 return (IP_FW_DENY); 1541 break; 1542 } 1543 args->f_id.extra = 1544 ntohl(((struct ip6_frag *)ulp)->ip6f_ident); 1545 ulp = NULL; 1546 break; 1547 1548 case IPPROTO_DSTOPTS: /* RFC 2460 */ 1549 PULLUP_TO(hlen, ulp, struct ip6_hbh); 1550 ext_hd |= EXT_DSTOPTS; 1551 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 1552 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 1553 ulp = NULL; 1554 break; 1555 1556 case IPPROTO_AH: /* RFC 2402 */ 1557 PULLUP_TO(hlen, ulp, struct ip6_ext); 1558 ext_hd |= EXT_AH; 1559 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2; 1560 proto = ((struct ip6_ext *)ulp)->ip6e_nxt; 1561 ulp = NULL; 1562 break; 1563 1564 case IPPROTO_ESP: /* RFC 2406 */ 1565 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */ 1566 /* Anything past Seq# is variable length and 1567 * data past this ext. header is encrypted. */ 1568 ext_hd |= EXT_ESP; 1569 break; 1570 1571 case IPPROTO_NONE: /* RFC 2460 */ 1572 /* 1573 * Packet ends here, and IPv6 header has 1574 * already been pulled up. If ip6e_len!=0 1575 * then octets must be ignored. 1576 */ 1577 ulp = ip; /* non-NULL to get out of loop. */ 1578 break; 1579 1580 case IPPROTO_OSPFIGP: 1581 /* XXX OSPF header check? */ 1582 PULLUP_TO(hlen, ulp, struct ip6_ext); 1583 break; 1584 1585 case IPPROTO_PIM: 1586 /* XXX PIM header check? */ 1587 PULLUP_TO(hlen, ulp, struct pim); 1588 break; 1589 1590 case IPPROTO_GRE: /* RFC 1701 */ 1591 /* XXX GRE header check? */ 1592 PULLUP_TO(hlen, ulp, struct grehdr); 1593 break; 1594 1595 case IPPROTO_CARP: 1596 PULLUP_TO(hlen, ulp, offsetof( 1597 struct carp_header, carp_counter)); 1598 if (CARP_ADVERTISEMENT != 1599 ((struct carp_header *)ulp)->carp_type) 1600 return (IP_FW_DENY); 1601 break; 1602 1603 case IPPROTO_IPV6: /* RFC 2893 */ 1604 PULLUP_TO(hlen, ulp, struct ip6_hdr); 1605 break; 1606 1607 case IPPROTO_IPV4: /* RFC 2893 */ 1608 PULLUP_TO(hlen, ulp, struct ip); 1609 break; 1610 1611 default: 1612 if (V_fw_verbose) 1613 printf("IPFW2: IPV6 - Unknown " 1614 "Extension Header(%d), ext_hd=%x\n", 1615 proto, ext_hd); 1616 if (V_fw_deny_unknown_exthdrs) 1617 return (IP_FW_DENY); 1618 PULLUP_TO(hlen, ulp, struct ip6_ext); 1619 break; 1620 } /*switch */ 1621 } 1622 ip = mtod(m, struct ip *); 1623 ip6 = (struct ip6_hdr *)ip; 1624 args->f_id.addr_type = 6; 1625 args->f_id.src_ip6 = ip6->ip6_src; 1626 args->f_id.dst_ip6 = ip6->ip6_dst; 1627 args->f_id.flow_id6 = ntohl(ip6->ip6_flow); 1628 iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6); 1629 } else if (pktlen >= sizeof(struct ip) && 1630 (etype == 0 || etype == ETHERTYPE_IP) && ip->ip_v == 4) { 1631 is_ipv4 = 1; 1632 hlen = ip->ip_hl << 2; 1633 /* 1634 * Collect parameters into local variables for faster 1635 * matching. 1636 */ 1637 proto = ip->ip_p; 1638 src_ip = ip->ip_src; 1639 dst_ip = ip->ip_dst; 1640 offset = ntohs(ip->ip_off) & IP_OFFMASK; 1641 iplen = ntohs(ip->ip_len); 1642 1643 if (offset == 0) { 1644 switch (proto) { 1645 case IPPROTO_TCP: 1646 PULLUP_TO(hlen, ulp, struct tcphdr); 1647 dst_port = TCP(ulp)->th_dport; 1648 src_port = TCP(ulp)->th_sport; 1649 /* save flags for dynamic rules */ 1650 args->f_id._flags = TCP(ulp)->th_flags; 1651 break; 1652 1653 case IPPROTO_SCTP: 1654 if (pktlen >= hlen + sizeof(struct sctphdr) + 1655 sizeof(struct sctp_chunkhdr) + 1656 offsetof(struct sctp_init, a_rwnd)) 1657 PULLUP_LEN(hlen, ulp, 1658 sizeof(struct sctphdr) + 1659 sizeof(struct sctp_chunkhdr) + 1660 offsetof(struct sctp_init, a_rwnd)); 1661 else if (pktlen >= hlen + sizeof(struct sctphdr)) 1662 PULLUP_LEN(hlen, ulp, pktlen - hlen); 1663 else 1664 PULLUP_LEN(hlen, ulp, 1665 sizeof(struct sctphdr)); 1666 src_port = SCTP(ulp)->src_port; 1667 dst_port = SCTP(ulp)->dest_port; 1668 break; 1669 1670 case IPPROTO_UDP: 1671 case IPPROTO_UDPLITE: 1672 PULLUP_TO(hlen, ulp, struct udphdr); 1673 dst_port = UDP(ulp)->uh_dport; 1674 src_port = UDP(ulp)->uh_sport; 1675 break; 1676 1677 case IPPROTO_ICMP: 1678 PULLUP_TO(hlen, ulp, struct icmphdr); 1679 //args->f_id.flags = ICMP(ulp)->icmp_type; 1680 break; 1681 1682 default: 1683 break; 1684 } 1685 } 1686 1687 ip = mtod(m, struct ip *); 1688 args->f_id.addr_type = 4; 1689 args->f_id.src_ip = ntohl(src_ip.s_addr); 1690 args->f_id.dst_ip = ntohl(dst_ip.s_addr); 1691 } else { 1692 proto = 0; 1693 dst_ip.s_addr = src_ip.s_addr = 0; 1694 1695 args->f_id.addr_type = 1; /* XXX */ 1696 } 1697 #undef PULLUP_TO 1698 pktlen = iplen < pktlen ? iplen: pktlen; 1699 1700 /* Properly initialize the rest of f_id */ 1701 args->f_id.proto = proto; 1702 args->f_id.src_port = src_port = ntohs(src_port); 1703 args->f_id.dst_port = dst_port = ntohs(dst_port); 1704 args->f_id.fib = M_GETFIB(m); 1705 1706 IPFW_PF_RLOCK(chain); 1707 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */ 1708 IPFW_PF_RUNLOCK(chain); 1709 return (IP_FW_PASS); /* accept */ 1710 } 1711 if (args->flags & IPFW_ARGS_REF) { 1712 /* 1713 * Packet has already been tagged as a result of a previous 1714 * match on rule args->rule aka args->rule_id (PIPE, QUEUE, 1715 * REASS, NETGRAPH, DIVERT/TEE...) 1716 * Validate the slot and continue from the next one 1717 * if still present, otherwise do a lookup. 1718 */ 1719 f_pos = (args->rule.chain_id == chain->id) ? 1720 args->rule.slot : 1721 ipfw_find_rule(chain, args->rule.rulenum, 1722 args->rule.rule_id); 1723 } else { 1724 f_pos = 0; 1725 } 1726 1727 /* 1728 * Now scan the rules, and parse microinstructions for each rule. 1729 * We have two nested loops and an inner switch. Sometimes we 1730 * need to break out of one or both loops, or re-enter one of 1731 * the loops with updated variables. Loop variables are: 1732 * 1733 * f_pos (outer loop) points to the current rule. 1734 * On output it points to the matching rule. 1735 * done (outer loop) is used as a flag to break the loop. 1736 * l (inner loop) residual length of current rule. 1737 * cmd points to the current microinstruction. 1738 * 1739 * We break the inner loop by setting l=0 and possibly 1740 * cmdlen=0 if we don't want to advance cmd. 1741 * We break the outer loop by setting done=1 1742 * We can restart the inner loop by setting l>0 and f_pos, f, cmd 1743 * as needed. 1744 */ 1745 for (; f_pos < chain->n_rules; f_pos++) { 1746 ipfw_insn *cmd; 1747 uint32_t tablearg = 0; 1748 int l, cmdlen, skip_or; /* skip rest of OR block */ 1749 struct ip_fw *f; 1750 1751 f = chain->map[f_pos]; 1752 if (V_set_disable & (1 << f->set) ) 1753 continue; 1754 1755 skip_or = 0; 1756 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; 1757 l -= cmdlen, cmd += cmdlen) { 1758 int match; 1759 1760 /* 1761 * check_body is a jump target used when we find a 1762 * CHECK_STATE, and need to jump to the body of 1763 * the target rule. 1764 */ 1765 1766 /* check_body: */ 1767 cmdlen = F_LEN(cmd); 1768 /* 1769 * An OR block (insn_1 || .. || insn_n) has the 1770 * F_OR bit set in all but the last instruction. 1771 * The first match will set "skip_or", and cause 1772 * the following instructions to be skipped until 1773 * past the one with the F_OR bit clear. 1774 */ 1775 if (skip_or) { /* skip this instruction */ 1776 if ((cmd->len & F_OR) == 0) 1777 skip_or = 0; /* next one is good */ 1778 continue; 1779 } 1780 match = 0; /* set to 1 if we succeed */ 1781 1782 switch (cmd->opcode) { 1783 /* 1784 * The first set of opcodes compares the packet's 1785 * fields with some pattern, setting 'match' if a 1786 * match is found. At the end of the loop there is 1787 * logic to deal with F_NOT and F_OR flags associated 1788 * with the opcode. 1789 */ 1790 case O_NOP: 1791 match = 1; 1792 break; 1793 1794 case O_FORWARD_MAC: 1795 printf("ipfw: opcode %d unimplemented\n", 1796 cmd->opcode); 1797 break; 1798 1799 case O_GID: 1800 case O_UID: 1801 case O_JAIL: 1802 /* 1803 * We only check offset == 0 && proto != 0, 1804 * as this ensures that we have a 1805 * packet with the ports info. 1806 */ 1807 if (offset != 0) 1808 break; 1809 if (proto == IPPROTO_TCP || 1810 proto == IPPROTO_UDP || 1811 proto == IPPROTO_UDPLITE) 1812 match = check_uidgid( 1813 (ipfw_insn_u32 *)cmd, 1814 args, &ucred_lookup, 1815 #ifdef __FreeBSD__ 1816 &ucred_cache); 1817 #else 1818 (void *)&ucred_cache); 1819 #endif 1820 break; 1821 1822 case O_RECV: 1823 match = iface_match(m->m_pkthdr.rcvif, 1824 (ipfw_insn_if *)cmd, chain, &tablearg); 1825 break; 1826 1827 case O_XMIT: 1828 match = iface_match(oif, (ipfw_insn_if *)cmd, 1829 chain, &tablearg); 1830 break; 1831 1832 case O_VIA: 1833 match = iface_match(oif ? oif : 1834 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd, 1835 chain, &tablearg); 1836 break; 1837 1838 case O_MACADDR2: 1839 if (args->flags & IPFW_ARGS_ETHER) { 1840 u_int32_t *want = (u_int32_t *) 1841 ((ipfw_insn_mac *)cmd)->addr; 1842 u_int32_t *mask = (u_int32_t *) 1843 ((ipfw_insn_mac *)cmd)->mask; 1844 u_int32_t *hdr = (u_int32_t *)args->eh; 1845 1846 match = 1847 ( want[0] == (hdr[0] & mask[0]) && 1848 want[1] == (hdr[1] & mask[1]) && 1849 want[2] == (hdr[2] & mask[2]) ); 1850 } 1851 break; 1852 1853 case O_MAC_TYPE: 1854 if (args->flags & IPFW_ARGS_ETHER) { 1855 u_int16_t *p = 1856 ((ipfw_insn_u16 *)cmd)->ports; 1857 int i; 1858 1859 for (i = cmdlen - 1; !match && i>0; 1860 i--, p += 2) 1861 match = (etype >= p[0] && 1862 etype <= p[1]); 1863 } 1864 break; 1865 1866 case O_FRAG: 1867 match = (offset != 0); 1868 break; 1869 1870 case O_IN: /* "out" is "not in" */ 1871 match = (oif == NULL); 1872 break; 1873 1874 case O_LAYER2: 1875 match = (args->flags & IPFW_ARGS_ETHER); 1876 break; 1877 1878 case O_DIVERTED: 1879 if ((args->flags & IPFW_ARGS_REF) == 0) 1880 break; 1881 /* 1882 * For diverted packets, args->rule.info 1883 * contains the divert port (in host format) 1884 * reason and direction. 1885 */ 1886 match = ((args->rule.info & IPFW_IS_MASK) == 1887 IPFW_IS_DIVERT) && ( 1888 ((args->rule.info & IPFW_INFO_IN) ? 1889 1: 2) & cmd->arg1); 1890 break; 1891 1892 case O_PROTO: 1893 /* 1894 * We do not allow an arg of 0 so the 1895 * check of "proto" only suffices. 1896 */ 1897 match = (proto == cmd->arg1); 1898 break; 1899 1900 case O_IP_SRC: 1901 match = is_ipv4 && 1902 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1903 src_ip.s_addr); 1904 break; 1905 1906 case O_IP_DST_LOOKUP: 1907 { 1908 void *pkey; 1909 uint32_t vidx, key; 1910 uint16_t keylen; 1911 1912 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) { 1913 /* Determine lookup key type */ 1914 vidx = ((ipfw_insn_u32 *)cmd)->d[1]; 1915 if (vidx != 4 /* uid */ && 1916 vidx != 5 /* jail */ && 1917 is_ipv6 == 0 && is_ipv4 == 0) 1918 break; 1919 /* Determine key length */ 1920 if (vidx == 0 /* dst-ip */ || 1921 vidx == 1 /* src-ip */) 1922 keylen = is_ipv6 ? 1923 sizeof(struct in6_addr): 1924 sizeof(in_addr_t); 1925 else { 1926 keylen = sizeof(key); 1927 pkey = &key; 1928 } 1929 if (vidx == 0 /* dst-ip */) 1930 pkey = is_ipv4 ? (void *)&dst_ip: 1931 (void *)&args->f_id.dst_ip6; 1932 else if (vidx == 1 /* src-ip */) 1933 pkey = is_ipv4 ? (void *)&src_ip: 1934 (void *)&args->f_id.src_ip6; 1935 else if (vidx == 6 /* dscp */) { 1936 if (is_ipv4) 1937 key = ip->ip_tos >> 2; 1938 else { 1939 key = args->f_id.flow_id6; 1940 key = (key & 0x0f) << 2 | 1941 (key & 0xf000) >> 14; 1942 } 1943 key &= 0x3f; 1944 } else if (vidx == 2 /* dst-port */ || 1945 vidx == 3 /* src-port */) { 1946 /* Skip fragments */ 1947 if (offset != 0) 1948 break; 1949 /* Skip proto without ports */ 1950 if (proto != IPPROTO_TCP && 1951 proto != IPPROTO_UDP && 1952 proto != IPPROTO_UDPLITE && 1953 proto != IPPROTO_SCTP) 1954 break; 1955 if (vidx == 2 /* dst-port */) 1956 key = dst_port; 1957 else 1958 key = src_port; 1959 } 1960 #ifndef USERSPACE 1961 else if (vidx == 4 /* uid */ || 1962 vidx == 5 /* jail */) { 1963 check_uidgid( 1964 (ipfw_insn_u32 *)cmd, 1965 args, &ucred_lookup, 1966 #ifdef __FreeBSD__ 1967 &ucred_cache); 1968 if (vidx == 4 /* uid */) 1969 key = ucred_cache->cr_uid; 1970 else if (vidx == 5 /* jail */) 1971 key = ucred_cache->cr_prison->pr_id; 1972 #else /* !__FreeBSD__ */ 1973 (void *)&ucred_cache); 1974 if (vidx == 4 /* uid */) 1975 key = ucred_cache.uid; 1976 else if (vidx == 5 /* jail */) 1977 key = ucred_cache.xid; 1978 #endif /* !__FreeBSD__ */ 1979 } 1980 #endif /* !USERSPACE */ 1981 else 1982 break; 1983 match = ipfw_lookup_table(chain, 1984 cmd->arg1, keylen, pkey, &vidx); 1985 if (!match) 1986 break; 1987 tablearg = vidx; 1988 break; 1989 } 1990 /* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */ 1991 /* FALLTHROUGH */ 1992 } 1993 case O_IP_SRC_LOOKUP: 1994 { 1995 void *pkey; 1996 uint32_t vidx; 1997 uint16_t keylen; 1998 1999 if (is_ipv4) { 2000 keylen = sizeof(in_addr_t); 2001 if (cmd->opcode == O_IP_DST_LOOKUP) 2002 pkey = &dst_ip; 2003 else 2004 pkey = &src_ip; 2005 } else if (is_ipv6) { 2006 keylen = sizeof(struct in6_addr); 2007 if (cmd->opcode == O_IP_DST_LOOKUP) 2008 pkey = &args->f_id.dst_ip6; 2009 else 2010 pkey = &args->f_id.src_ip6; 2011 } else 2012 break; 2013 match = ipfw_lookup_table(chain, cmd->arg1, 2014 keylen, pkey, &vidx); 2015 if (!match) 2016 break; 2017 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) { 2018 match = ((ipfw_insn_u32 *)cmd)->d[0] == 2019 TARG_VAL(chain, vidx, tag); 2020 if (!match) 2021 break; 2022 } 2023 tablearg = vidx; 2024 break; 2025 } 2026 2027 case O_IP_FLOW_LOOKUP: 2028 { 2029 uint32_t v = 0; 2030 match = ipfw_lookup_table(chain, 2031 cmd->arg1, 0, &args->f_id, &v); 2032 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) 2033 match = ((ipfw_insn_u32 *)cmd)->d[0] == 2034 TARG_VAL(chain, v, tag); 2035 if (match) 2036 tablearg = v; 2037 } 2038 break; 2039 case O_IP_SRC_MASK: 2040 case O_IP_DST_MASK: 2041 if (is_ipv4) { 2042 uint32_t a = 2043 (cmd->opcode == O_IP_DST_MASK) ? 2044 dst_ip.s_addr : src_ip.s_addr; 2045 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; 2046 int i = cmdlen-1; 2047 2048 for (; !match && i>0; i-= 2, p+= 2) 2049 match = (p[0] == (a & p[1])); 2050 } 2051 break; 2052 2053 case O_IP_SRC_ME: 2054 if (is_ipv4) { 2055 match = in_localip(src_ip); 2056 break; 2057 } 2058 #ifdef INET6 2059 /* FALLTHROUGH */ 2060 case O_IP6_SRC_ME: 2061 match = is_ipv6 && 2062 ipfw_localip6(&args->f_id.src_ip6); 2063 #endif 2064 break; 2065 2066 case O_IP_DST_SET: 2067 case O_IP_SRC_SET: 2068 if (is_ipv4) { 2069 u_int32_t *d = (u_int32_t *)(cmd+1); 2070 u_int32_t addr = 2071 cmd->opcode == O_IP_DST_SET ? 2072 args->f_id.dst_ip : 2073 args->f_id.src_ip; 2074 2075 if (addr < d[0]) 2076 break; 2077 addr -= d[0]; /* subtract base */ 2078 match = (addr < cmd->arg1) && 2079 ( d[ 1 + (addr>>5)] & 2080 (1<<(addr & 0x1f)) ); 2081 } 2082 break; 2083 2084 case O_IP_DST: 2085 match = is_ipv4 && 2086 (((ipfw_insn_ip *)cmd)->addr.s_addr == 2087 dst_ip.s_addr); 2088 break; 2089 2090 case O_IP_DST_ME: 2091 if (is_ipv4) { 2092 match = in_localip(dst_ip); 2093 break; 2094 } 2095 #ifdef INET6 2096 /* FALLTHROUGH */ 2097 case O_IP6_DST_ME: 2098 match = is_ipv6 && 2099 ipfw_localip6(&args->f_id.dst_ip6); 2100 #endif 2101 break; 2102 2103 2104 case O_IP_SRCPORT: 2105 case O_IP_DSTPORT: 2106 /* 2107 * offset == 0 && proto != 0 is enough 2108 * to guarantee that we have a 2109 * packet with port info. 2110 */ 2111 if ((proto == IPPROTO_UDP || 2112 proto == IPPROTO_UDPLITE || 2113 proto == IPPROTO_TCP || 2114 proto == IPPROTO_SCTP) && offset == 0) { 2115 u_int16_t x = 2116 (cmd->opcode == O_IP_SRCPORT) ? 2117 src_port : dst_port ; 2118 u_int16_t *p = 2119 ((ipfw_insn_u16 *)cmd)->ports; 2120 int i; 2121 2122 for (i = cmdlen - 1; !match && i>0; 2123 i--, p += 2) 2124 match = (x>=p[0] && x<=p[1]); 2125 } 2126 break; 2127 2128 case O_ICMPTYPE: 2129 match = (offset == 0 && proto==IPPROTO_ICMP && 2130 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); 2131 break; 2132 2133 #ifdef INET6 2134 case O_ICMP6TYPE: 2135 match = is_ipv6 && offset == 0 && 2136 proto==IPPROTO_ICMPV6 && 2137 icmp6type_match( 2138 ICMP6(ulp)->icmp6_type, 2139 (ipfw_insn_u32 *)cmd); 2140 break; 2141 #endif /* INET6 */ 2142 2143 case O_IPOPT: 2144 match = (is_ipv4 && 2145 ipopts_match(ip, cmd) ); 2146 break; 2147 2148 case O_IPVER: 2149 match = (is_ipv4 && 2150 cmd->arg1 == ip->ip_v); 2151 break; 2152 2153 case O_IPID: 2154 case O_IPLEN: 2155 case O_IPTTL: 2156 if (is_ipv4) { /* only for IP packets */ 2157 uint16_t x; 2158 uint16_t *p; 2159 int i; 2160 2161 if (cmd->opcode == O_IPLEN) 2162 x = iplen; 2163 else if (cmd->opcode == O_IPTTL) 2164 x = ip->ip_ttl; 2165 else /* must be IPID */ 2166 x = ntohs(ip->ip_id); 2167 if (cmdlen == 1) { 2168 match = (cmd->arg1 == x); 2169 break; 2170 } 2171 /* otherwise we have ranges */ 2172 p = ((ipfw_insn_u16 *)cmd)->ports; 2173 i = cmdlen - 1; 2174 for (; !match && i>0; i--, p += 2) 2175 match = (x >= p[0] && x <= p[1]); 2176 } 2177 break; 2178 2179 case O_IPPRECEDENCE: 2180 match = (is_ipv4 && 2181 (cmd->arg1 == (ip->ip_tos & 0xe0)) ); 2182 break; 2183 2184 case O_IPTOS: 2185 match = (is_ipv4 && 2186 flags_match(cmd, ip->ip_tos)); 2187 break; 2188 2189 case O_DSCP: 2190 { 2191 uint32_t *p; 2192 uint16_t x; 2193 2194 p = ((ipfw_insn_u32 *)cmd)->d; 2195 2196 if (is_ipv4) 2197 x = ip->ip_tos >> 2; 2198 else if (is_ipv6) { 2199 uint8_t *v; 2200 v = &((struct ip6_hdr *)ip)->ip6_vfc; 2201 x = (*v & 0x0F) << 2; 2202 v++; 2203 x |= *v >> 6; 2204 } else 2205 break; 2206 2207 /* DSCP bitmask is stored as low_u32 high_u32 */ 2208 if (x >= 32) 2209 match = *(p + 1) & (1 << (x - 32)); 2210 else 2211 match = *p & (1 << x); 2212 } 2213 break; 2214 2215 case O_TCPDATALEN: 2216 if (proto == IPPROTO_TCP && offset == 0) { 2217 struct tcphdr *tcp; 2218 uint16_t x; 2219 uint16_t *p; 2220 int i; 2221 #ifdef INET6 2222 if (is_ipv6) { 2223 struct ip6_hdr *ip6; 2224 2225 ip6 = (struct ip6_hdr *)ip; 2226 if (ip6->ip6_plen == 0) { 2227 /* 2228 * Jumbo payload is not 2229 * supported by this 2230 * opcode. 2231 */ 2232 break; 2233 } 2234 x = iplen - hlen; 2235 } else 2236 #endif /* INET6 */ 2237 x = iplen - (ip->ip_hl << 2); 2238 tcp = TCP(ulp); 2239 x -= tcp->th_off << 2; 2240 if (cmdlen == 1) { 2241 match = (cmd->arg1 == x); 2242 break; 2243 } 2244 /* otherwise we have ranges */ 2245 p = ((ipfw_insn_u16 *)cmd)->ports; 2246 i = cmdlen - 1; 2247 for (; !match && i>0; i--, p += 2) 2248 match = (x >= p[0] && x <= p[1]); 2249 } 2250 break; 2251 2252 case O_TCPFLAGS: 2253 match = (proto == IPPROTO_TCP && offset == 0 && 2254 flags_match(cmd, TCP(ulp)->th_flags)); 2255 break; 2256 2257 case O_TCPOPTS: 2258 if (proto == IPPROTO_TCP && offset == 0 && ulp){ 2259 PULLUP_LEN(hlen, ulp, 2260 (TCP(ulp)->th_off << 2)); 2261 match = tcpopts_match(TCP(ulp), cmd); 2262 } 2263 break; 2264 2265 case O_TCPSEQ: 2266 match = (proto == IPPROTO_TCP && offset == 0 && 2267 ((ipfw_insn_u32 *)cmd)->d[0] == 2268 TCP(ulp)->th_seq); 2269 break; 2270 2271 case O_TCPACK: 2272 match = (proto == IPPROTO_TCP && offset == 0 && 2273 ((ipfw_insn_u32 *)cmd)->d[0] == 2274 TCP(ulp)->th_ack); 2275 break; 2276 2277 case O_TCPWIN: 2278 if (proto == IPPROTO_TCP && offset == 0) { 2279 uint16_t x; 2280 uint16_t *p; 2281 int i; 2282 2283 x = ntohs(TCP(ulp)->th_win); 2284 if (cmdlen == 1) { 2285 match = (cmd->arg1 == x); 2286 break; 2287 } 2288 /* Otherwise we have ranges. */ 2289 p = ((ipfw_insn_u16 *)cmd)->ports; 2290 i = cmdlen - 1; 2291 for (; !match && i > 0; i--, p += 2) 2292 match = (x >= p[0] && x <= p[1]); 2293 } 2294 break; 2295 2296 case O_ESTAB: 2297 /* reject packets which have SYN only */ 2298 /* XXX should i also check for TH_ACK ? */ 2299 match = (proto == IPPROTO_TCP && offset == 0 && 2300 (TCP(ulp)->th_flags & 2301 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); 2302 break; 2303 2304 case O_ALTQ: { 2305 struct pf_mtag *at; 2306 struct m_tag *mtag; 2307 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 2308 2309 /* 2310 * ALTQ uses mbuf tags from another 2311 * packet filtering system - pf(4). 2312 * We allocate a tag in its format 2313 * and fill it in, pretending to be pf(4). 2314 */ 2315 match = 1; 2316 at = pf_find_mtag(m); 2317 if (at != NULL && at->qid != 0) 2318 break; 2319 mtag = m_tag_get(PACKET_TAG_PF, 2320 sizeof(struct pf_mtag), M_NOWAIT | M_ZERO); 2321 if (mtag == NULL) { 2322 /* 2323 * Let the packet fall back to the 2324 * default ALTQ. 2325 */ 2326 break; 2327 } 2328 m_tag_prepend(m, mtag); 2329 at = (struct pf_mtag *)(mtag + 1); 2330 at->qid = altq->qid; 2331 at->hdr = ip; 2332 break; 2333 } 2334 2335 case O_LOG: 2336 ipfw_log(chain, f, hlen, args, m, 2337 oif, offset | ip6f_mf, tablearg, ip); 2338 match = 1; 2339 break; 2340 2341 case O_PROB: 2342 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); 2343 break; 2344 2345 case O_VERREVPATH: 2346 /* Outgoing packets automatically pass/match */ 2347 match = ((oif != NULL) || 2348 (m->m_pkthdr.rcvif == NULL) || 2349 ( 2350 #ifdef INET6 2351 is_ipv6 ? 2352 verify_path6(&(args->f_id.src_ip6), 2353 m->m_pkthdr.rcvif, args->f_id.fib) : 2354 #endif 2355 verify_path(src_ip, m->m_pkthdr.rcvif, 2356 args->f_id.fib))); 2357 break; 2358 2359 case O_VERSRCREACH: 2360 /* Outgoing packets automatically pass/match */ 2361 match = (hlen > 0 && ((oif != NULL) || ( 2362 #ifdef INET6 2363 is_ipv6 ? 2364 verify_path6(&(args->f_id.src_ip6), 2365 NULL, args->f_id.fib) : 2366 #endif 2367 verify_path(src_ip, NULL, args->f_id.fib)))); 2368 break; 2369 2370 case O_ANTISPOOF: 2371 /* Outgoing packets automatically pass/match */ 2372 if (oif == NULL && hlen > 0 && 2373 ( (is_ipv4 && in_localaddr(src_ip)) 2374 #ifdef INET6 2375 || (is_ipv6 && 2376 in6_localaddr(&(args->f_id.src_ip6))) 2377 #endif 2378 )) 2379 match = 2380 #ifdef INET6 2381 is_ipv6 ? verify_path6( 2382 &(args->f_id.src_ip6), 2383 m->m_pkthdr.rcvif, 2384 args->f_id.fib) : 2385 #endif 2386 verify_path(src_ip, 2387 m->m_pkthdr.rcvif, 2388 args->f_id.fib); 2389 else 2390 match = 1; 2391 break; 2392 2393 case O_IPSEC: 2394 match = (m_tag_find(m, 2395 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); 2396 /* otherwise no match */ 2397 break; 2398 2399 #ifdef INET6 2400 case O_IP6_SRC: 2401 match = is_ipv6 && 2402 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, 2403 &((ipfw_insn_ip6 *)cmd)->addr6); 2404 break; 2405 2406 case O_IP6_DST: 2407 match = is_ipv6 && 2408 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, 2409 &((ipfw_insn_ip6 *)cmd)->addr6); 2410 break; 2411 case O_IP6_SRC_MASK: 2412 case O_IP6_DST_MASK: 2413 if (is_ipv6) { 2414 int i = cmdlen - 1; 2415 struct in6_addr p; 2416 struct in6_addr *d = 2417 &((ipfw_insn_ip6 *)cmd)->addr6; 2418 2419 for (; !match && i > 0; d += 2, 2420 i -= F_INSN_SIZE(struct in6_addr) 2421 * 2) { 2422 p = (cmd->opcode == 2423 O_IP6_SRC_MASK) ? 2424 args->f_id.src_ip6: 2425 args->f_id.dst_ip6; 2426 APPLY_MASK(&p, &d[1]); 2427 match = 2428 IN6_ARE_ADDR_EQUAL(&d[0], 2429 &p); 2430 } 2431 } 2432 break; 2433 2434 case O_FLOW6ID: 2435 match = is_ipv6 && 2436 flow6id_match(args->f_id.flow_id6, 2437 (ipfw_insn_u32 *) cmd); 2438 break; 2439 2440 case O_EXT_HDR: 2441 match = is_ipv6 && 2442 (ext_hd & ((ipfw_insn *) cmd)->arg1); 2443 break; 2444 2445 case O_IP6: 2446 match = is_ipv6; 2447 break; 2448 #endif 2449 2450 case O_IP4: 2451 match = is_ipv4; 2452 break; 2453 2454 case O_TAG: { 2455 struct m_tag *mtag; 2456 uint32_t tag = TARG(cmd->arg1, tag); 2457 2458 /* Packet is already tagged with this tag? */ 2459 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL); 2460 2461 /* We have `untag' action when F_NOT flag is 2462 * present. And we must remove this mtag from 2463 * mbuf and reset `match' to zero (`match' will 2464 * be inversed later). 2465 * Otherwise we should allocate new mtag and 2466 * push it into mbuf. 2467 */ 2468 if (cmd->len & F_NOT) { /* `untag' action */ 2469 if (mtag != NULL) 2470 m_tag_delete(m, mtag); 2471 match = 0; 2472 } else { 2473 if (mtag == NULL) { 2474 mtag = m_tag_alloc( MTAG_IPFW, 2475 tag, 0, M_NOWAIT); 2476 if (mtag != NULL) 2477 m_tag_prepend(m, mtag); 2478 } 2479 match = 1; 2480 } 2481 break; 2482 } 2483 2484 case O_FIB: /* try match the specified fib */ 2485 if (args->f_id.fib == cmd->arg1) 2486 match = 1; 2487 break; 2488 2489 case O_SOCKARG: { 2490 #ifndef USERSPACE /* not supported in userspace */ 2491 struct inpcb *inp = args->inp; 2492 struct inpcbinfo *pi; 2493 2494 if (is_ipv6) /* XXX can we remove this ? */ 2495 break; 2496 2497 if (proto == IPPROTO_TCP) 2498 pi = &V_tcbinfo; 2499 else if (proto == IPPROTO_UDP) 2500 pi = &V_udbinfo; 2501 else if (proto == IPPROTO_UDPLITE) 2502 pi = &V_ulitecbinfo; 2503 else 2504 break; 2505 2506 /* 2507 * XXXRW: so_user_cookie should almost 2508 * certainly be inp_user_cookie? 2509 */ 2510 2511 /* For incoming packet, lookup up the 2512 inpcb using the src/dest ip/port tuple */ 2513 if (inp == NULL) { 2514 inp = in_pcblookup(pi, 2515 src_ip, htons(src_port), 2516 dst_ip, htons(dst_port), 2517 INPLOOKUP_RLOCKPCB, NULL); 2518 if (inp != NULL) { 2519 tablearg = 2520 inp->inp_socket->so_user_cookie; 2521 if (tablearg) 2522 match = 1; 2523 INP_RUNLOCK(inp); 2524 } 2525 } else { 2526 if (inp->inp_socket) { 2527 tablearg = 2528 inp->inp_socket->so_user_cookie; 2529 if (tablearg) 2530 match = 1; 2531 } 2532 } 2533 #endif /* !USERSPACE */ 2534 break; 2535 } 2536 2537 case O_TAGGED: { 2538 struct m_tag *mtag; 2539 uint32_t tag = TARG(cmd->arg1, tag); 2540 2541 if (cmdlen == 1) { 2542 match = m_tag_locate(m, MTAG_IPFW, 2543 tag, NULL) != NULL; 2544 break; 2545 } 2546 2547 /* we have ranges */ 2548 for (mtag = m_tag_first(m); 2549 mtag != NULL && !match; 2550 mtag = m_tag_next(m, mtag)) { 2551 uint16_t *p; 2552 int i; 2553 2554 if (mtag->m_tag_cookie != MTAG_IPFW) 2555 continue; 2556 2557 p = ((ipfw_insn_u16 *)cmd)->ports; 2558 i = cmdlen - 1; 2559 for(; !match && i > 0; i--, p += 2) 2560 match = 2561 mtag->m_tag_id >= p[0] && 2562 mtag->m_tag_id <= p[1]; 2563 } 2564 break; 2565 } 2566 2567 /* 2568 * The second set of opcodes represents 'actions', 2569 * i.e. the terminal part of a rule once the packet 2570 * matches all previous patterns. 2571 * Typically there is only one action for each rule, 2572 * and the opcode is stored at the end of the rule 2573 * (but there are exceptions -- see below). 2574 * 2575 * In general, here we set retval and terminate the 2576 * outer loop (would be a 'break 3' in some language, 2577 * but we need to set l=0, done=1) 2578 * 2579 * Exceptions: 2580 * O_COUNT and O_SKIPTO actions: 2581 * instead of terminating, we jump to the next rule 2582 * (setting l=0), or to the SKIPTO target (setting 2583 * f/f_len, cmd and l as needed), respectively. 2584 * 2585 * O_TAG, O_LOG and O_ALTQ action parameters: 2586 * perform some action and set match = 1; 2587 * 2588 * O_LIMIT and O_KEEP_STATE: these opcodes are 2589 * not real 'actions', and are stored right 2590 * before the 'action' part of the rule (one 2591 * exception is O_SKIP_ACTION which could be 2592 * between these opcodes and 'action' one). 2593 * These opcodes try to install an entry in the 2594 * state tables; if successful, we continue with 2595 * the next opcode (match=1; break;), otherwise 2596 * the packet must be dropped (set retval, 2597 * break loops with l=0, done=1) 2598 * 2599 * O_PROBE_STATE and O_CHECK_STATE: these opcodes 2600 * cause a lookup of the state table, and a jump 2601 * to the 'action' part of the parent rule 2602 * if an entry is found, or 2603 * (CHECK_STATE only) a jump to the next rule if 2604 * the entry is not found. 2605 * The result of the lookup is cached so that 2606 * further instances of these opcodes become NOPs. 2607 * The jump to the next rule is done by setting 2608 * l=0, cmdlen=0. 2609 * 2610 * O_SKIP_ACTION: this opcode is not a real 'action' 2611 * either, and is stored right before the 'action' 2612 * part of the rule, right after the O_KEEP_STATE 2613 * opcode. It causes match failure so the real 2614 * 'action' could be executed only if the rule 2615 * is checked via dynamic rule from the state 2616 * table, as in such case execution starts 2617 * from the true 'action' opcode directly. 2618 * 2619 */ 2620 case O_LIMIT: 2621 case O_KEEP_STATE: 2622 if (ipfw_dyn_install_state(chain, f, 2623 (ipfw_insn_limit *)cmd, args, ulp, 2624 pktlen, &dyn_info, tablearg)) { 2625 /* error or limit violation */ 2626 retval = IP_FW_DENY; 2627 l = 0; /* exit inner loop */ 2628 done = 1; /* exit outer loop */ 2629 } 2630 match = 1; 2631 break; 2632 2633 case O_PROBE_STATE: 2634 case O_CHECK_STATE: 2635 /* 2636 * dynamic rules are checked at the first 2637 * keep-state or check-state occurrence, 2638 * with the result being stored in dyn_info. 2639 * The compiler introduces a PROBE_STATE 2640 * instruction for us when we have a 2641 * KEEP_STATE (because PROBE_STATE needs 2642 * to be run first). 2643 */ 2644 if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) && 2645 (q = ipfw_dyn_lookup_state(args, ulp, 2646 pktlen, cmd, &dyn_info)) != NULL) { 2647 /* 2648 * Found dynamic entry, jump to the 2649 * 'action' part of the parent rule 2650 * by setting f, cmd, l and clearing 2651 * cmdlen. 2652 */ 2653 f = q; 2654 f_pos = dyn_info.f_pos; 2655 cmd = ACTION_PTR(f); 2656 l = f->cmd_len - f->act_ofs; 2657 cmdlen = 0; 2658 match = 1; 2659 break; 2660 } 2661 /* 2662 * Dynamic entry not found. If CHECK_STATE, 2663 * skip to next rule, if PROBE_STATE just 2664 * ignore and continue with next opcode. 2665 */ 2666 if (cmd->opcode == O_CHECK_STATE) 2667 l = 0; /* exit inner loop */ 2668 match = 1; 2669 break; 2670 2671 case O_SKIP_ACTION: 2672 match = 0; /* skip to the next rule */ 2673 l = 0; /* exit inner loop */ 2674 break; 2675 2676 case O_ACCEPT: 2677 retval = 0; /* accept */ 2678 l = 0; /* exit inner loop */ 2679 done = 1; /* exit outer loop */ 2680 break; 2681 2682 case O_PIPE: 2683 case O_QUEUE: 2684 set_match(args, f_pos, chain); 2685 args->rule.info = TARG(cmd->arg1, pipe); 2686 if (cmd->opcode == O_PIPE) 2687 args->rule.info |= IPFW_IS_PIPE; 2688 if (V_fw_one_pass) 2689 args->rule.info |= IPFW_ONEPASS; 2690 retval = IP_FW_DUMMYNET; 2691 l = 0; /* exit inner loop */ 2692 done = 1; /* exit outer loop */ 2693 break; 2694 2695 case O_DIVERT: 2696 case O_TEE: 2697 if (args->flags & IPFW_ARGS_ETHER) 2698 break; /* not on layer 2 */ 2699 /* otherwise this is terminal */ 2700 l = 0; /* exit inner loop */ 2701 done = 1; /* exit outer loop */ 2702 retval = (cmd->opcode == O_DIVERT) ? 2703 IP_FW_DIVERT : IP_FW_TEE; 2704 set_match(args, f_pos, chain); 2705 args->rule.info = TARG(cmd->arg1, divert); 2706 break; 2707 2708 case O_COUNT: 2709 IPFW_INC_RULE_COUNTER(f, pktlen); 2710 l = 0; /* exit inner loop */ 2711 break; 2712 2713 case O_SKIPTO: 2714 IPFW_INC_RULE_COUNTER(f, pktlen); 2715 f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0); 2716 /* 2717 * Skip disabled rules, and re-enter 2718 * the inner loop with the correct 2719 * f_pos, f, l and cmd. 2720 * Also clear cmdlen and skip_or 2721 */ 2722 for (; f_pos < chain->n_rules - 1 && 2723 (V_set_disable & 2724 (1 << chain->map[f_pos]->set)); 2725 f_pos++) 2726 ; 2727 /* Re-enter the inner loop at the skipto rule. */ 2728 f = chain->map[f_pos]; 2729 l = f->cmd_len; 2730 cmd = f->cmd; 2731 match = 1; 2732 cmdlen = 0; 2733 skip_or = 0; 2734 continue; 2735 break; /* not reached */ 2736 2737 case O_CALLRETURN: { 2738 /* 2739 * Implementation of `subroutine' call/return, 2740 * in the stack carried in an mbuf tag. This 2741 * is different from `skipto' in that any call 2742 * address is possible (`skipto' must prevent 2743 * backward jumps to avoid endless loops). 2744 * We have `return' action when F_NOT flag is 2745 * present. The `m_tag_id' field is used as 2746 * stack pointer. 2747 */ 2748 struct m_tag *mtag; 2749 uint16_t jmpto, *stack; 2750 2751 #define IS_CALL ((cmd->len & F_NOT) == 0) 2752 #define IS_RETURN ((cmd->len & F_NOT) != 0) 2753 /* 2754 * Hand-rolled version of m_tag_locate() with 2755 * wildcard `type'. 2756 * If not already tagged, allocate new tag. 2757 */ 2758 mtag = m_tag_first(m); 2759 while (mtag != NULL) { 2760 if (mtag->m_tag_cookie == 2761 MTAG_IPFW_CALL) 2762 break; 2763 mtag = m_tag_next(m, mtag); 2764 } 2765 if (mtag == NULL && IS_CALL) { 2766 mtag = m_tag_alloc(MTAG_IPFW_CALL, 0, 2767 IPFW_CALLSTACK_SIZE * 2768 sizeof(uint16_t), M_NOWAIT); 2769 if (mtag != NULL) 2770 m_tag_prepend(m, mtag); 2771 } 2772 2773 /* 2774 * On error both `call' and `return' just 2775 * continue with next rule. 2776 */ 2777 if (IS_RETURN && (mtag == NULL || 2778 mtag->m_tag_id == 0)) { 2779 l = 0; /* exit inner loop */ 2780 break; 2781 } 2782 if (IS_CALL && (mtag == NULL || 2783 mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) { 2784 printf("ipfw: call stack error, " 2785 "go to next rule\n"); 2786 l = 0; /* exit inner loop */ 2787 break; 2788 } 2789 2790 IPFW_INC_RULE_COUNTER(f, pktlen); 2791 stack = (uint16_t *)(mtag + 1); 2792 2793 /* 2794 * The `call' action may use cached f_pos 2795 * (in f->next_rule), whose version is written 2796 * in f->next_rule. 2797 * The `return' action, however, doesn't have 2798 * fixed jump address in cmd->arg1 and can't use 2799 * cache. 2800 */ 2801 if (IS_CALL) { 2802 stack[mtag->m_tag_id] = f->rulenum; 2803 mtag->m_tag_id++; 2804 f_pos = JUMP(chain, f, cmd->arg1, 2805 tablearg, 1); 2806 } else { /* `return' action */ 2807 mtag->m_tag_id--; 2808 jmpto = stack[mtag->m_tag_id] + 1; 2809 f_pos = ipfw_find_rule(chain, jmpto, 0); 2810 } 2811 2812 /* 2813 * Skip disabled rules, and re-enter 2814 * the inner loop with the correct 2815 * f_pos, f, l and cmd. 2816 * Also clear cmdlen and skip_or 2817 */ 2818 for (; f_pos < chain->n_rules - 1 && 2819 (V_set_disable & 2820 (1 << chain->map[f_pos]->set)); f_pos++) 2821 ; 2822 /* Re-enter the inner loop at the dest rule. */ 2823 f = chain->map[f_pos]; 2824 l = f->cmd_len; 2825 cmd = f->cmd; 2826 cmdlen = 0; 2827 skip_or = 0; 2828 continue; 2829 break; /* NOTREACHED */ 2830 } 2831 #undef IS_CALL 2832 #undef IS_RETURN 2833 2834 case O_REJECT: 2835 /* 2836 * Drop the packet and send a reject notice 2837 * if the packet is not ICMP (or is an ICMP 2838 * query), and it is not multicast/broadcast. 2839 */ 2840 if (hlen > 0 && is_ipv4 && offset == 0 && 2841 (proto != IPPROTO_ICMP || 2842 is_icmp_query(ICMP(ulp))) && 2843 !(m->m_flags & (M_BCAST|M_MCAST)) && 2844 !IN_MULTICAST(ntohl(dst_ip.s_addr))) { 2845 send_reject(args, cmd->arg1, iplen, ip); 2846 m = args->m; 2847 } 2848 /* FALLTHROUGH */ 2849 #ifdef INET6 2850 case O_UNREACH6: 2851 if (hlen > 0 && is_ipv6 && 2852 ((offset & IP6F_OFF_MASK) == 0) && 2853 (proto != IPPROTO_ICMPV6 || 2854 (is_icmp6_query(icmp6_type) == 1)) && 2855 !(m->m_flags & (M_BCAST|M_MCAST)) && 2856 !IN6_IS_ADDR_MULTICAST( 2857 &args->f_id.dst_ip6)) { 2858 send_reject6(args, 2859 cmd->opcode == O_REJECT ? 2860 map_icmp_unreach(cmd->arg1): 2861 cmd->arg1, hlen, 2862 (struct ip6_hdr *)ip); 2863 m = args->m; 2864 } 2865 /* FALLTHROUGH */ 2866 #endif 2867 case O_DENY: 2868 retval = IP_FW_DENY; 2869 l = 0; /* exit inner loop */ 2870 done = 1; /* exit outer loop */ 2871 break; 2872 2873 case O_FORWARD_IP: 2874 if (args->flags & IPFW_ARGS_ETHER) 2875 break; /* not valid on layer2 pkts */ 2876 if (q != f || 2877 dyn_info.direction == MATCH_FORWARD) { 2878 struct sockaddr_in *sa; 2879 2880 sa = &(((ipfw_insn_sa *)cmd)->sa); 2881 if (sa->sin_addr.s_addr == INADDR_ANY) { 2882 #ifdef INET6 2883 /* 2884 * We use O_FORWARD_IP opcode for 2885 * fwd rule with tablearg, but tables 2886 * now support IPv6 addresses. And 2887 * when we are inspecting IPv6 packet, 2888 * we can use nh6 field from 2889 * table_value as next_hop6 address. 2890 */ 2891 if (is_ipv6) { 2892 struct ip_fw_nh6 *nh6; 2893 2894 args->flags |= IPFW_ARGS_NH6; 2895 nh6 = &args->hopstore6; 2896 nh6->sin6_addr = TARG_VAL( 2897 chain, tablearg, nh6); 2898 nh6->sin6_port = sa->sin_port; 2899 nh6->sin6_scope_id = TARG_VAL( 2900 chain, tablearg, zoneid); 2901 } else 2902 #endif 2903 { 2904 args->flags |= IPFW_ARGS_NH4; 2905 args->hopstore.sin_port = 2906 sa->sin_port; 2907 sa = &args->hopstore; 2908 sa->sin_family = AF_INET; 2909 sa->sin_len = sizeof(*sa); 2910 sa->sin_addr.s_addr = htonl( 2911 TARG_VAL(chain, tablearg, 2912 nh4)); 2913 } 2914 } else { 2915 args->flags |= IPFW_ARGS_NH4PTR; 2916 args->next_hop = sa; 2917 } 2918 } 2919 retval = IP_FW_PASS; 2920 l = 0; /* exit inner loop */ 2921 done = 1; /* exit outer loop */ 2922 break; 2923 2924 #ifdef INET6 2925 case O_FORWARD_IP6: 2926 if (args->flags & IPFW_ARGS_ETHER) 2927 break; /* not valid on layer2 pkts */ 2928 if (q != f || 2929 dyn_info.direction == MATCH_FORWARD) { 2930 struct sockaddr_in6 *sin6; 2931 2932 sin6 = &(((ipfw_insn_sa6 *)cmd)->sa); 2933 args->flags |= IPFW_ARGS_NH6PTR; 2934 args->next_hop6 = sin6; 2935 } 2936 retval = IP_FW_PASS; 2937 l = 0; /* exit inner loop */ 2938 done = 1; /* exit outer loop */ 2939 break; 2940 #endif 2941 2942 case O_NETGRAPH: 2943 case O_NGTEE: 2944 set_match(args, f_pos, chain); 2945 args->rule.info = TARG(cmd->arg1, netgraph); 2946 if (V_fw_one_pass) 2947 args->rule.info |= IPFW_ONEPASS; 2948 retval = (cmd->opcode == O_NETGRAPH) ? 2949 IP_FW_NETGRAPH : IP_FW_NGTEE; 2950 l = 0; /* exit inner loop */ 2951 done = 1; /* exit outer loop */ 2952 break; 2953 2954 case O_SETFIB: { 2955 uint32_t fib; 2956 2957 IPFW_INC_RULE_COUNTER(f, pktlen); 2958 fib = TARG(cmd->arg1, fib) & 0x7FFF; 2959 if (fib >= rt_numfibs) 2960 fib = 0; 2961 M_SETFIB(m, fib); 2962 args->f_id.fib = fib; /* XXX */ 2963 l = 0; /* exit inner loop */ 2964 break; 2965 } 2966 2967 case O_SETDSCP: { 2968 uint16_t code; 2969 2970 code = TARG(cmd->arg1, dscp) & 0x3F; 2971 l = 0; /* exit inner loop */ 2972 if (is_ipv4) { 2973 uint16_t old; 2974 2975 old = *(uint16_t *)ip; 2976 ip->ip_tos = (code << 2) | 2977 (ip->ip_tos & 0x03); 2978 ip->ip_sum = cksum_adjust(ip->ip_sum, 2979 old, *(uint16_t *)ip); 2980 } else if (is_ipv6) { 2981 uint8_t *v; 2982 2983 v = &((struct ip6_hdr *)ip)->ip6_vfc; 2984 *v = (*v & 0xF0) | (code >> 2); 2985 v++; 2986 *v = (*v & 0x3F) | ((code & 0x03) << 6); 2987 } else 2988 break; 2989 2990 IPFW_INC_RULE_COUNTER(f, pktlen); 2991 break; 2992 } 2993 2994 case O_NAT: 2995 l = 0; /* exit inner loop */ 2996 done = 1; /* exit outer loop */ 2997 /* 2998 * Ensure that we do not invoke NAT handler for 2999 * non IPv4 packets. Libalias expects only IPv4. 3000 */ 3001 if (!is_ipv4 || !IPFW_NAT_LOADED) { 3002 retval = IP_FW_DENY; 3003 break; 3004 } 3005 3006 struct cfg_nat *t; 3007 int nat_id; 3008 3009 args->rule.info = 0; 3010 set_match(args, f_pos, chain); 3011 /* Check if this is 'global' nat rule */ 3012 if (cmd->arg1 == IP_FW_NAT44_GLOBAL) { 3013 retval = ipfw_nat_ptr(args, NULL, m); 3014 break; 3015 } 3016 t = ((ipfw_insn_nat *)cmd)->nat; 3017 if (t == NULL) { 3018 nat_id = TARG(cmd->arg1, nat); 3019 t = (*lookup_nat_ptr)(&chain->nat, nat_id); 3020 3021 if (t == NULL) { 3022 retval = IP_FW_DENY; 3023 break; 3024 } 3025 if (cmd->arg1 != IP_FW_TARG) 3026 ((ipfw_insn_nat *)cmd)->nat = t; 3027 } 3028 retval = ipfw_nat_ptr(args, t, m); 3029 break; 3030 3031 case O_REASS: { 3032 int ip_off; 3033 3034 l = 0; /* in any case exit inner loop */ 3035 if (is_ipv6) /* IPv6 is not supported yet */ 3036 break; 3037 IPFW_INC_RULE_COUNTER(f, pktlen); 3038 ip_off = ntohs(ip->ip_off); 3039 3040 /* if not fragmented, go to next rule */ 3041 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0) 3042 break; 3043 3044 args->m = m = ip_reass(m); 3045 3046 /* 3047 * do IP header checksum fixup. 3048 */ 3049 if (m == NULL) { /* fragment got swallowed */ 3050 retval = IP_FW_DENY; 3051 } else { /* good, packet complete */ 3052 int hlen; 3053 3054 ip = mtod(m, struct ip *); 3055 hlen = ip->ip_hl << 2; 3056 ip->ip_sum = 0; 3057 if (hlen == sizeof(struct ip)) 3058 ip->ip_sum = in_cksum_hdr(ip); 3059 else 3060 ip->ip_sum = in_cksum(m, hlen); 3061 retval = IP_FW_REASS; 3062 args->rule.info = 0; 3063 set_match(args, f_pos, chain); 3064 } 3065 done = 1; /* exit outer loop */ 3066 break; 3067 } 3068 case O_EXTERNAL_ACTION: 3069 l = 0; /* in any case exit inner loop */ 3070 retval = ipfw_run_eaction(chain, args, 3071 cmd, &done); 3072 /* 3073 * If both @retval and @done are zero, 3074 * consider this as rule matching and 3075 * update counters. 3076 */ 3077 if (retval == 0 && done == 0) { 3078 IPFW_INC_RULE_COUNTER(f, pktlen); 3079 /* 3080 * Reset the result of the last 3081 * dynamic state lookup. 3082 * External action can change 3083 * @args content, and it may be 3084 * used for new state lookup later. 3085 */ 3086 DYN_INFO_INIT(&dyn_info); 3087 } 3088 break; 3089 3090 default: 3091 panic("-- unknown opcode %d\n", cmd->opcode); 3092 } /* end of switch() on opcodes */ 3093 /* 3094 * if we get here with l=0, then match is irrelevant. 3095 */ 3096 3097 if (cmd->len & F_NOT) 3098 match = !match; 3099 3100 if (match) { 3101 if (cmd->len & F_OR) 3102 skip_or = 1; 3103 } else { 3104 if (!(cmd->len & F_OR)) /* not an OR block, */ 3105 break; /* try next rule */ 3106 } 3107 3108 } /* end of inner loop, scan opcodes */ 3109 #undef PULLUP_LEN 3110 3111 if (done) 3112 break; 3113 3114 /* next_rule:; */ /* try next rule */ 3115 3116 } /* end of outer for, scan rules */ 3117 3118 if (done) { 3119 struct ip_fw *rule = chain->map[f_pos]; 3120 /* Update statistics */ 3121 IPFW_INC_RULE_COUNTER(rule, pktlen); 3122 } else { 3123 retval = IP_FW_DENY; 3124 printf("ipfw: ouch!, skip past end of rules, denying packet\n"); 3125 } 3126 IPFW_PF_RUNLOCK(chain); 3127 #ifdef __FreeBSD__ 3128 if (ucred_cache != NULL) 3129 crfree(ucred_cache); 3130 #endif 3131 return (retval); 3132 3133 pullup_failed: 3134 if (V_fw_verbose) 3135 printf("ipfw: pullup failed\n"); 3136 return (IP_FW_DENY); 3137 } 3138 3139 /* 3140 * Set maximum number of tables that can be used in given VNET ipfw instance. 3141 */ 3142 #ifdef SYSCTL_NODE 3143 static int 3144 sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS) 3145 { 3146 int error; 3147 unsigned int ntables; 3148 3149 ntables = V_fw_tables_max; 3150 3151 error = sysctl_handle_int(oidp, &ntables, 0, req); 3152 /* Read operation or some error */ 3153 if ((error != 0) || (req->newptr == NULL)) 3154 return (error); 3155 3156 return (ipfw_resize_tables(&V_layer3_chain, ntables)); 3157 } 3158 3159 /* 3160 * Switches table namespace between global and per-set. 3161 */ 3162 static int 3163 sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS) 3164 { 3165 int error; 3166 unsigned int sets; 3167 3168 sets = V_fw_tables_sets; 3169 3170 error = sysctl_handle_int(oidp, &sets, 0, req); 3171 /* Read operation or some error */ 3172 if ((error != 0) || (req->newptr == NULL)) 3173 return (error); 3174 3175 return (ipfw_switch_tables_namespace(&V_layer3_chain, sets)); 3176 } 3177 #endif 3178 3179 /* 3180 * Module and VNET glue 3181 */ 3182 3183 /* 3184 * Stuff that must be initialised only on boot or module load 3185 */ 3186 static int 3187 ipfw_init(void) 3188 { 3189 int error = 0; 3190 3191 /* 3192 * Only print out this stuff the first time around, 3193 * when called from the sysinit code. 3194 */ 3195 printf("ipfw2 " 3196 #ifdef INET6 3197 "(+ipv6) " 3198 #endif 3199 "initialized, divert %s, nat %s, " 3200 "default to %s, logging ", 3201 #ifdef IPDIVERT 3202 "enabled", 3203 #else 3204 "loadable", 3205 #endif 3206 #ifdef IPFIREWALL_NAT 3207 "enabled", 3208 #else 3209 "loadable", 3210 #endif 3211 default_to_accept ? "accept" : "deny"); 3212 3213 /* 3214 * Note: V_xxx variables can be accessed here but the vnet specific 3215 * initializer may not have been called yet for the VIMAGE case. 3216 * Tuneables will have been processed. We will print out values for 3217 * the default vnet. 3218 * XXX This should all be rationalized AFTER 8.0 3219 */ 3220 if (V_fw_verbose == 0) 3221 printf("disabled\n"); 3222 else if (V_verbose_limit == 0) 3223 printf("unlimited\n"); 3224 else 3225 printf("limited to %d packets/entry by default\n", 3226 V_verbose_limit); 3227 3228 /* Check user-supplied table count for validness */ 3229 if (default_fw_tables > IPFW_TABLES_MAX) 3230 default_fw_tables = IPFW_TABLES_MAX; 3231 3232 ipfw_init_sopt_handler(); 3233 ipfw_init_obj_rewriter(); 3234 ipfw_iface_init(); 3235 return (error); 3236 } 3237 3238 /* 3239 * Called for the removal of the last instance only on module unload. 3240 */ 3241 static void 3242 ipfw_destroy(void) 3243 { 3244 3245 ipfw_iface_destroy(); 3246 ipfw_destroy_sopt_handler(); 3247 ipfw_destroy_obj_rewriter(); 3248 printf("IP firewall unloaded\n"); 3249 } 3250 3251 /* 3252 * Stuff that must be initialized for every instance 3253 * (including the first of course). 3254 */ 3255 static int 3256 vnet_ipfw_init(const void *unused) 3257 { 3258 int error, first; 3259 struct ip_fw *rule = NULL; 3260 struct ip_fw_chain *chain; 3261 3262 chain = &V_layer3_chain; 3263 3264 first = IS_DEFAULT_VNET(curvnet) ? 1 : 0; 3265 3266 /* First set up some values that are compile time options */ 3267 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ 3268 V_fw_deny_unknown_exthdrs = 1; 3269 #ifdef IPFIREWALL_VERBOSE 3270 V_fw_verbose = 1; 3271 #endif 3272 #ifdef IPFIREWALL_VERBOSE_LIMIT 3273 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT; 3274 #endif 3275 #ifdef IPFIREWALL_NAT 3276 LIST_INIT(&chain->nat); 3277 #endif 3278 3279 /* Init shared services hash table */ 3280 ipfw_init_srv(chain); 3281 3282 ipfw_init_counters(); 3283 /* Set initial number of tables */ 3284 V_fw_tables_max = default_fw_tables; 3285 error = ipfw_init_tables(chain, first); 3286 if (error) { 3287 printf("ipfw2: setting up tables failed\n"); 3288 free(chain->map, M_IPFW); 3289 free(rule, M_IPFW); 3290 return (ENOSPC); 3291 } 3292 3293 IPFW_LOCK_INIT(chain); 3294 3295 /* fill and insert the default rule */ 3296 rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw)); 3297 rule->cmd_len = 1; 3298 rule->cmd[0].len = 1; 3299 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY; 3300 chain->default_rule = rule; 3301 ipfw_add_protected_rule(chain, rule, 0); 3302 3303 ipfw_dyn_init(chain); 3304 ipfw_eaction_init(chain, first); 3305 #ifdef LINEAR_SKIPTO 3306 ipfw_init_skipto_cache(chain); 3307 #endif 3308 ipfw_bpf_init(first); 3309 3310 /* First set up some values that are compile time options */ 3311 V_ipfw_vnet_ready = 1; /* Open for business */ 3312 3313 /* 3314 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6. 3315 * Even if the latter two fail we still keep the module alive 3316 * because the sockopt and layer2 paths are still useful. 3317 * ipfw[6]_hook return 0 on success, ENOENT on failure, 3318 * so we can ignore the exact return value and just set a flag. 3319 * 3320 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so 3321 * changes in the underlying (per-vnet) variables trigger 3322 * immediate hook()/unhook() calls. 3323 * In layer2 we have the same behaviour, except that V_ether_ipfw 3324 * is checked on each packet because there are no pfil hooks. 3325 */ 3326 V_ip_fw_ctl_ptr = ipfw_ctl3; 3327 error = ipfw_attach_hooks(1); 3328 return (error); 3329 } 3330 3331 /* 3332 * Called for the removal of each instance. 3333 */ 3334 static int 3335 vnet_ipfw_uninit(const void *unused) 3336 { 3337 struct ip_fw *reap; 3338 struct ip_fw_chain *chain = &V_layer3_chain; 3339 int i, last; 3340 3341 V_ipfw_vnet_ready = 0; /* tell new callers to go away */ 3342 /* 3343 * disconnect from ipv4, ipv6, layer2 and sockopt. 3344 * Then grab, release and grab again the WLOCK so we make 3345 * sure the update is propagated and nobody will be in. 3346 */ 3347 (void)ipfw_attach_hooks(0 /* detach */); 3348 V_ip_fw_ctl_ptr = NULL; 3349 3350 last = IS_DEFAULT_VNET(curvnet) ? 1 : 0; 3351 3352 IPFW_UH_WLOCK(chain); 3353 IPFW_UH_WUNLOCK(chain); 3354 3355 ipfw_dyn_uninit(0); /* run the callout_drain */ 3356 3357 IPFW_UH_WLOCK(chain); 3358 3359 reap = NULL; 3360 IPFW_WLOCK(chain); 3361 for (i = 0; i < chain->n_rules; i++) 3362 ipfw_reap_add(chain, &reap, chain->map[i]); 3363 free(chain->map, M_IPFW); 3364 #ifdef LINEAR_SKIPTO 3365 ipfw_destroy_skipto_cache(chain); 3366 #endif 3367 IPFW_WUNLOCK(chain); 3368 IPFW_UH_WUNLOCK(chain); 3369 ipfw_destroy_tables(chain, last); 3370 ipfw_eaction_uninit(chain, last); 3371 if (reap != NULL) 3372 ipfw_reap_rules(reap); 3373 vnet_ipfw_iface_destroy(chain); 3374 ipfw_destroy_srv(chain); 3375 IPFW_LOCK_DESTROY(chain); 3376 ipfw_dyn_uninit(1); /* free the remaining parts */ 3377 ipfw_destroy_counters(); 3378 ipfw_bpf_uninit(last); 3379 return (0); 3380 } 3381 3382 /* 3383 * Module event handler. 3384 * In general we have the choice of handling most of these events by the 3385 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to 3386 * use the SYSINIT handlers as they are more capable of expressing the 3387 * flow of control during module and vnet operations, so this is just 3388 * a skeleton. Note there is no SYSINIT equivalent of the module 3389 * SHUTDOWN handler, but we don't have anything to do in that case anyhow. 3390 */ 3391 static int 3392 ipfw_modevent(module_t mod, int type, void *unused) 3393 { 3394 int err = 0; 3395 3396 switch (type) { 3397 case MOD_LOAD: 3398 /* Called once at module load or 3399 * system boot if compiled in. */ 3400 break; 3401 case MOD_QUIESCE: 3402 /* Called before unload. May veto unloading. */ 3403 break; 3404 case MOD_UNLOAD: 3405 /* Called during unload. */ 3406 break; 3407 case MOD_SHUTDOWN: 3408 /* Called during system shutdown. */ 3409 break; 3410 default: 3411 err = EOPNOTSUPP; 3412 break; 3413 } 3414 return err; 3415 } 3416 3417 static moduledata_t ipfwmod = { 3418 "ipfw", 3419 ipfw_modevent, 3420 0 3421 }; 3422 3423 /* Define startup order. */ 3424 #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL 3425 #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */ 3426 #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */ 3427 #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */ 3428 3429 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER); 3430 FEATURE(ipfw_ctl3, "ipfw new sockopt calls"); 3431 MODULE_VERSION(ipfw, 3); 3432 /* should declare some dependencies here */ 3433 3434 /* 3435 * Starting up. Done in order after ipfwmod() has been called. 3436 * VNET_SYSINIT is also called for each existing vnet and each new vnet. 3437 */ 3438 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 3439 ipfw_init, NULL); 3440 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 3441 vnet_ipfw_init, NULL); 3442 3443 /* 3444 * Closing up shop. These are done in REVERSE ORDER, but still 3445 * after ipfwmod() has been called. Not called on reboot. 3446 * VNET_SYSUNINIT is also called for each exiting vnet as it exits. 3447 * or when the module is unloaded. 3448 */ 3449 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 3450 ipfw_destroy, NULL); 3451 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 3452 vnet_ipfw_uninit, NULL); 3453 /* end of file */ 3454