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