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