ip_fw2.c revision 200590
1/*- 2 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw2.c 200590 2009-12-15 21:24:12Z luigi $"); 28 29#define DEB(x) 30#define DDB(x) x 31 32/* 33 * Implement IP packet firewall (new version) 34 */ 35 36#if !defined(KLD_MODULE) 37#include "opt_ipfw.h" 38#include "opt_ipdivert.h" 39#include "opt_ipdn.h" 40#include "opt_inet.h" 41#ifndef INET 42#error IPFIREWALL requires INET. 43#endif /* INET */ 44#endif 45#include "opt_inet6.h" 46#include "opt_ipsec.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/condvar.h> 51#include <sys/eventhandler.h> 52#include <sys/malloc.h> 53#include <sys/mbuf.h> 54#include <sys/kernel.h> 55#include <sys/lock.h> 56#include <sys/jail.h> 57#include <sys/module.h> 58#include <sys/priv.h> 59#include <sys/proc.h> 60#include <sys/rwlock.h> 61#include <sys/socket.h> 62#include <sys/socketvar.h> 63#include <sys/sysctl.h> 64#include <sys/syslog.h> 65#include <sys/ucred.h> 66#include <net/ethernet.h> /* for ETHERTYPE_IP */ 67#include <net/if.h> 68#include <net/route.h> 69#include <net/pf_mtag.h> 70#include <net/vnet.h> 71 72#include <netinet/in.h> 73#include <netinet/in_var.h> 74#include <netinet/in_pcb.h> 75#include <netinet/ip.h> 76#include <netinet/ip_var.h> 77#include <netinet/ip_icmp.h> 78#include <netinet/ip_fw.h> 79#include <netinet/ipfw/ip_fw_private.h> 80#include <netinet/ip_divert.h> 81#include <netinet/ip_dummynet.h> 82#include <netinet/ip_carp.h> 83#include <netinet/pim.h> 84#include <netinet/tcp_var.h> 85#include <netinet/udp.h> 86#include <netinet/udp_var.h> 87#include <netinet/sctp.h> 88 89#include <netgraph/ng_ipfw.h> 90 91#include <netinet/ip6.h> 92#include <netinet/icmp6.h> 93#ifdef INET6 94#include <netinet6/scope6_var.h> 95#include <netinet6/ip6_var.h> 96#endif 97 98#include <machine/in_cksum.h> /* XXX for in_cksum */ 99 100#ifdef MAC 101#include <security/mac/mac_framework.h> 102#endif 103 104static VNET_DEFINE(int, ipfw_vnet_ready) = 0; 105#define V_ipfw_vnet_ready VNET(ipfw_vnet_ready) 106/* 107 * set_disable contains one bit per set value (0..31). 108 * If the bit is set, all rules with the corresponding set 109 * are disabled. Set RESVD_SET(31) is reserved for the default rule 110 * and rules that are not deleted by the flush command, 111 * and CANNOT be disabled. 112 * Rules in set RESVD_SET can only be deleted explicitly. 113 */ 114VNET_DEFINE(u_int32_t, set_disable); 115VNET_DEFINE(int, fw_verbose); 116 117#define V_set_disable VNET(set_disable) 118#define V_verbose_limit VNET(verbose_limit) 119 120#ifdef IPFIREWALL_DEFAULT_TO_ACCEPT 121static int default_to_accept = 1; 122#else 123static int default_to_accept; 124#endif 125 126struct ip_fw *ip_fw_default_rule; 127 128/* 129 * list of rules for layer 3 130 */ 131VNET_DEFINE(struct ip_fw_chain, layer3_chain); 132 133MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's"); 134ipfw_nat_t *ipfw_nat_ptr = NULL; 135struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int); 136ipfw_nat_cfg_t *ipfw_nat_cfg_ptr; 137ipfw_nat_cfg_t *ipfw_nat_del_ptr; 138ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr; 139ipfw_nat_cfg_t *ipfw_nat_get_log_ptr; 140 141struct table_entry { 142 struct radix_node rn[2]; 143 struct sockaddr_in addr, mask; 144 u_int32_t value; 145}; 146 147static VNET_DEFINE(int, autoinc_step); 148#define V_autoinc_step VNET(autoinc_step) 149static VNET_DEFINE(int, fw_deny_unknown_exthdrs); 150#define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs) 151 152extern int ipfw_chg_hook(SYSCTL_HANDLER_ARGS); 153 154#ifdef SYSCTL_NODE 155SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 156SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable, 157 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0, 158 ipfw_chg_hook, "I", "Enable ipfw"); 159SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, 160 CTLFLAG_RW, &VNET_NAME(autoinc_step), 0, 161 "Rule number auto-increment step"); 162SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass, 163 CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0, 164 "Only do a single pass through ipfw when using dummynet(4)"); 165SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose, 166 CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0, 167 "Log matches to ipfw rules"); 168SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, 169 CTLFLAG_RW, &VNET_NAME(verbose_limit), 0, 170 "Set upper limit of matches of ipfw rules logged"); 171SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD, 172 NULL, IPFW_DEFAULT_RULE, 173 "The default/max possible rule number."); 174SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, tables_max, CTLFLAG_RD, 175 NULL, IPFW_TABLES_MAX, 176 "The maximum number of tables."); 177SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN, 178 &default_to_accept, 0, 179 "Make the default rule accept all packets."); 180TUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept); 181 182#ifdef INET6 183SYSCTL_DECL(_net_inet6_ip6); 184SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); 185SYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable, 186 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0, 187 ipfw_chg_hook, "I", "Enable ipfw+6"); 188SYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs, 189 CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0, 190 "Deny packets with unknown IPv6 Extension Headers"); 191#endif /* INET6 */ 192 193#endif /* SYSCTL_NODE */ 194 195 196/* 197 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 198 * Other macros just cast void * into the appropriate type 199 */ 200#define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 201#define TCP(p) ((struct tcphdr *)(p)) 202#define SCTP(p) ((struct sctphdr *)(p)) 203#define UDP(p) ((struct udphdr *)(p)) 204#define ICMP(p) ((struct icmphdr *)(p)) 205#define ICMP6(p) ((struct icmp6_hdr *)(p)) 206 207static __inline int 208icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd) 209{ 210 int type = icmp->icmp_type; 211 212 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) ); 213} 214 215#define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \ 216 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) ) 217 218static int 219is_icmp_query(struct icmphdr *icmp) 220{ 221 int type = icmp->icmp_type; 222 223 return (type <= ICMP_MAXTYPE && (TT & (1<<type)) ); 224} 225#undef TT 226 227/* 228 * The following checks use two arrays of 8 or 16 bits to store the 229 * bits that we want set or clear, respectively. They are in the 230 * low and high half of cmd->arg1 or cmd->d[0]. 231 * 232 * We scan options and store the bits we find set. We succeed if 233 * 234 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear 235 * 236 * The code is sometimes optimized not to store additional variables. 237 */ 238 239static int 240flags_match(ipfw_insn *cmd, u_int8_t bits) 241{ 242 u_char want_clear; 243 bits = ~bits; 244 245 if ( ((cmd->arg1 & 0xff) & bits) != 0) 246 return 0; /* some bits we want set were clear */ 247 want_clear = (cmd->arg1 >> 8) & 0xff; 248 if ( (want_clear & bits) != want_clear) 249 return 0; /* some bits we want clear were set */ 250 return 1; 251} 252 253static int 254ipopts_match(struct ip *ip, ipfw_insn *cmd) 255{ 256 int optlen, bits = 0; 257 u_char *cp = (u_char *)(ip + 1); 258 int x = (ip->ip_hl << 2) - sizeof (struct ip); 259 260 for (; x > 0; x -= optlen, cp += optlen) { 261 int opt = cp[IPOPT_OPTVAL]; 262 263 if (opt == IPOPT_EOL) 264 break; 265 if (opt == IPOPT_NOP) 266 optlen = 1; 267 else { 268 optlen = cp[IPOPT_OLEN]; 269 if (optlen <= 0 || optlen > x) 270 return 0; /* invalid or truncated */ 271 } 272 switch (opt) { 273 274 default: 275 break; 276 277 case IPOPT_LSRR: 278 bits |= IP_FW_IPOPT_LSRR; 279 break; 280 281 case IPOPT_SSRR: 282 bits |= IP_FW_IPOPT_SSRR; 283 break; 284 285 case IPOPT_RR: 286 bits |= IP_FW_IPOPT_RR; 287 break; 288 289 case IPOPT_TS: 290 bits |= IP_FW_IPOPT_TS; 291 break; 292 } 293 } 294 return (flags_match(cmd, bits)); 295} 296 297static int 298tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd) 299{ 300 int optlen, bits = 0; 301 u_char *cp = (u_char *)(tcp + 1); 302 int x = (tcp->th_off << 2) - sizeof(struct tcphdr); 303 304 for (; x > 0; x -= optlen, cp += optlen) { 305 int opt = cp[0]; 306 if (opt == TCPOPT_EOL) 307 break; 308 if (opt == TCPOPT_NOP) 309 optlen = 1; 310 else { 311 optlen = cp[1]; 312 if (optlen <= 0) 313 break; 314 } 315 316 switch (opt) { 317 318 default: 319 break; 320 321 case TCPOPT_MAXSEG: 322 bits |= IP_FW_TCPOPT_MSS; 323 break; 324 325 case TCPOPT_WINDOW: 326 bits |= IP_FW_TCPOPT_WINDOW; 327 break; 328 329 case TCPOPT_SACK_PERMITTED: 330 case TCPOPT_SACK: 331 bits |= IP_FW_TCPOPT_SACK; 332 break; 333 334 case TCPOPT_TIMESTAMP: 335 bits |= IP_FW_TCPOPT_TS; 336 break; 337 338 } 339 } 340 return (flags_match(cmd, bits)); 341} 342 343static int 344iface_match(struct ifnet *ifp, ipfw_insn_if *cmd) 345{ 346 if (ifp == NULL) /* no iface with this packet, match fails */ 347 return 0; 348 /* Check by name or by IP address */ 349 if (cmd->name[0] != '\0') { /* match by name */ 350 /* Check name */ 351 if (cmd->p.glob) { 352 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0) 353 return(1); 354 } else { 355 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0) 356 return(1); 357 } 358 } else { 359 struct ifaddr *ia; 360 361 if_addr_rlock(ifp); 362 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 363 if (ia->ifa_addr->sa_family != AF_INET) 364 continue; 365 if (cmd->p.ip.s_addr == ((struct sockaddr_in *) 366 (ia->ifa_addr))->sin_addr.s_addr) { 367 if_addr_runlock(ifp); 368 return(1); /* match */ 369 } 370 } 371 if_addr_runlock(ifp); 372 } 373 return(0); /* no match, fail ... */ 374} 375 376/* 377 * The verify_path function checks if a route to the src exists and 378 * if it is reachable via ifp (when provided). 379 * 380 * The 'verrevpath' option checks that the interface that an IP packet 381 * arrives on is the same interface that traffic destined for the 382 * packet's source address would be routed out of. The 'versrcreach' 383 * option just checks that the source address is reachable via any route 384 * (except default) in the routing table. These two are a measure to block 385 * forged packets. This is also commonly known as "anti-spoofing" or Unicast 386 * Reverse Path Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs 387 * is purposely reminiscent of the Cisco IOS command, 388 * 389 * ip verify unicast reverse-path 390 * ip verify unicast source reachable-via any 391 * 392 * which implements the same functionality. But note that syntax is 393 * misleading. The check may be performed on all IP packets whether unicast, 394 * multicast, or broadcast. 395 */ 396static int 397verify_path(struct in_addr src, struct ifnet *ifp, u_int fib) 398{ 399 struct route ro; 400 struct sockaddr_in *dst; 401 402 bzero(&ro, sizeof(ro)); 403 404 dst = (struct sockaddr_in *)&(ro.ro_dst); 405 dst->sin_family = AF_INET; 406 dst->sin_len = sizeof(*dst); 407 dst->sin_addr = src; 408 in_rtalloc_ign(&ro, 0, fib); 409 410 if (ro.ro_rt == NULL) 411 return 0; 412 413 /* 414 * If ifp is provided, check for equality with rtentry. 415 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 416 * in order to pass packets injected back by if_simloop(): 417 * if useloopback == 1 routing entry (via lo0) for our own address 418 * may exist, so we need to handle routing assymetry. 419 */ 420 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 421 RTFREE(ro.ro_rt); 422 return 0; 423 } 424 425 /* if no ifp provided, check if rtentry is not default route */ 426 if (ifp == NULL && 427 satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) { 428 RTFREE(ro.ro_rt); 429 return 0; 430 } 431 432 /* or if this is a blackhole/reject route */ 433 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 434 RTFREE(ro.ro_rt); 435 return 0; 436 } 437 438 /* found valid route */ 439 RTFREE(ro.ro_rt); 440 return 1; 441} 442 443#ifdef INET6 444/* 445 * ipv6 specific rules here... 446 */ 447static __inline int 448icmp6type_match (int type, ipfw_insn_u32 *cmd) 449{ 450 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) ); 451} 452 453static int 454flow6id_match( int curr_flow, ipfw_insn_u32 *cmd ) 455{ 456 int i; 457 for (i=0; i <= cmd->o.arg1; ++i ) 458 if (curr_flow == cmd->d[i] ) 459 return 1; 460 return 0; 461} 462 463/* support for IP6_*_ME opcodes */ 464static int 465search_ip6_addr_net (struct in6_addr * ip6_addr) 466{ 467 struct ifnet *mdc; 468 struct ifaddr *mdc2; 469 struct in6_ifaddr *fdm; 470 struct in6_addr copia; 471 472 TAILQ_FOREACH(mdc, &V_ifnet, if_link) { 473 if_addr_rlock(mdc); 474 TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) { 475 if (mdc2->ifa_addr->sa_family == AF_INET6) { 476 fdm = (struct in6_ifaddr *)mdc2; 477 copia = fdm->ia_addr.sin6_addr; 478 /* need for leaving scope_id in the sock_addr */ 479 in6_clearscope(&copia); 480 if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) { 481 if_addr_runlock(mdc); 482 return 1; 483 } 484 } 485 } 486 if_addr_runlock(mdc); 487 } 488 return 0; 489} 490 491static int 492verify_path6(struct in6_addr *src, struct ifnet *ifp) 493{ 494 struct route_in6 ro; 495 struct sockaddr_in6 *dst; 496 497 bzero(&ro, sizeof(ro)); 498 499 dst = (struct sockaddr_in6 * )&(ro.ro_dst); 500 dst->sin6_family = AF_INET6; 501 dst->sin6_len = sizeof(*dst); 502 dst->sin6_addr = *src; 503 /* XXX MRT 0 for ipv6 at this time */ 504 rtalloc_ign((struct route *)&ro, 0); 505 506 if (ro.ro_rt == NULL) 507 return 0; 508 509 /* 510 * if ifp is provided, check for equality with rtentry 511 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, 512 * to support the case of sending packets to an address of our own. 513 * (where the former interface is the first argument of if_simloop() 514 * (=ifp), the latter is lo0) 515 */ 516 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) { 517 RTFREE(ro.ro_rt); 518 return 0; 519 } 520 521 /* if no ifp provided, check if rtentry is not default route */ 522 if (ifp == NULL && 523 IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) { 524 RTFREE(ro.ro_rt); 525 return 0; 526 } 527 528 /* or if this is a blackhole/reject route */ 529 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 530 RTFREE(ro.ro_rt); 531 return 0; 532 } 533 534 /* found valid route */ 535 RTFREE(ro.ro_rt); 536 return 1; 537 538} 539static int 540is_icmp6_query(int icmp6_type) 541{ 542 if ((icmp6_type <= ICMP6_MAXTYPE) && 543 (icmp6_type == ICMP6_ECHO_REQUEST || 544 icmp6_type == ICMP6_MEMBERSHIP_QUERY || 545 icmp6_type == ICMP6_WRUREQUEST || 546 icmp6_type == ICMP6_FQDN_QUERY || 547 icmp6_type == ICMP6_NI_QUERY)) 548 return (1); 549 550 return (0); 551} 552 553static void 554send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6) 555{ 556 struct mbuf *m; 557 558 m = args->m; 559 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) { 560 struct tcphdr *tcp; 561 tcp = (struct tcphdr *)((char *)ip6 + hlen); 562 563 if ((tcp->th_flags & TH_RST) == 0) { 564 struct mbuf *m0; 565 m0 = send_pkt(args->m, &(args->f_id), 566 ntohl(tcp->th_seq), ntohl(tcp->th_ack), 567 tcp->th_flags | TH_RST); 568 if (m0 != NULL) 569 ip6_output(m0, NULL, NULL, 0, NULL, NULL, 570 NULL); 571 } 572 m_freem(m); 573 } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */ 574#if 0 575 /* 576 * Unlike above, the mbufs need to line up with the ip6 hdr, 577 * as the contents are read. We need to m_adj() the 578 * needed amount. 579 * The mbuf will however be thrown away so we can adjust it. 580 * Remember we did an m_pullup on it already so we 581 * can make some assumptions about contiguousness. 582 */ 583 if (args->L3offset) 584 m_adj(m, args->L3offset); 585#endif 586 icmp6_error(m, ICMP6_DST_UNREACH, code, 0); 587 } else 588 m_freem(m); 589 590 args->m = NULL; 591} 592 593#endif /* INET6 */ 594 595 596/* 597 * sends a reject message, consuming the mbuf passed as an argument. 598 */ 599static void 600send_reject(struct ip_fw_args *args, int code, int ip_len, struct ip *ip) 601{ 602 603#if 0 604 /* XXX When ip is not guaranteed to be at mtod() we will 605 * need to account for this */ 606 * The mbuf will however be thrown away so we can adjust it. 607 * Remember we did an m_pullup on it already so we 608 * can make some assumptions about contiguousness. 609 */ 610 if (args->L3offset) 611 m_adj(m, args->L3offset); 612#endif 613 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */ 614 /* We need the IP header in host order for icmp_error(). */ 615 if (args->eh != NULL) { 616 ip->ip_len = ntohs(ip->ip_len); 617 ip->ip_off = ntohs(ip->ip_off); 618 } 619 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0); 620 } else if (args->f_id.proto == IPPROTO_TCP) { 621 struct tcphdr *const tcp = 622 L3HDR(struct tcphdr, mtod(args->m, struct ip *)); 623 if ( (tcp->th_flags & TH_RST) == 0) { 624 struct mbuf *m; 625 m = send_pkt(args->m, &(args->f_id), 626 ntohl(tcp->th_seq), ntohl(tcp->th_ack), 627 tcp->th_flags | TH_RST); 628 if (m != NULL) 629 ip_output(m, NULL, NULL, 0, NULL, NULL); 630 } 631 m_freem(args->m); 632 } else 633 m_freem(args->m); 634 args->m = NULL; 635} 636 637/** 638 * 639 * Given an ip_fw *, lookup_next_rule will return a pointer 640 * to the next rule, which can be either the jump 641 * target (for skipto instructions) or the next one in the list (in 642 * all other cases including a missing jump target). 643 * The result is also written in the "next_rule" field of the rule. 644 * Backward jumps are not allowed, so start looking from the next 645 * rule... 646 * 647 * This never returns NULL -- in case we do not have an exact match, 648 * the next rule is returned. When the ruleset is changed, 649 * pointers are flushed so we are always correct. 650 */ 651 652static struct ip_fw * 653lookup_next_rule(struct ip_fw *me, u_int32_t tablearg) 654{ 655 struct ip_fw *rule = NULL; 656 ipfw_insn *cmd; 657 u_int16_t rulenum; 658 659 /* look for action, in case it is a skipto */ 660 cmd = ACTION_PTR(me); 661 if (cmd->opcode == O_LOG) 662 cmd += F_LEN(cmd); 663 if (cmd->opcode == O_ALTQ) 664 cmd += F_LEN(cmd); 665 if (cmd->opcode == O_TAG) 666 cmd += F_LEN(cmd); 667 if (cmd->opcode == O_SKIPTO ) { 668 if (tablearg != 0) { 669 rulenum = (u_int16_t)tablearg; 670 } else { 671 rulenum = cmd->arg1; 672 } 673 for (rule = me->next; rule ; rule = rule->next) { 674 if (rule->rulenum >= rulenum) { 675 break; 676 } 677 } 678 } 679 if (rule == NULL) /* failure or not a skipto */ 680 rule = me->next; 681 me->next_rule = rule; 682 return rule; 683} 684 685static int 686check_uidgid(ipfw_insn_u32 *insn, int proto, struct ifnet *oif, 687 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip, 688 u_int16_t src_port, struct ucred **uc, int *ugid_lookupp, 689 struct inpcb *inp) 690{ 691 struct inpcbinfo *pi; 692 int wildcard; 693 struct inpcb *pcb; 694 int match; 695 696 /* 697 * Check to see if the UDP or TCP stack supplied us with 698 * the PCB. If so, rather then holding a lock and looking 699 * up the PCB, we can use the one that was supplied. 700 */ 701 if (inp && *ugid_lookupp == 0) { 702 INP_LOCK_ASSERT(inp); 703 if (inp->inp_socket != NULL) { 704 *uc = crhold(inp->inp_cred); 705 *ugid_lookupp = 1; 706 } else 707 *ugid_lookupp = -1; 708 } 709 /* 710 * If we have already been here and the packet has no 711 * PCB entry associated with it, then we can safely 712 * assume that this is a no match. 713 */ 714 if (*ugid_lookupp == -1) 715 return (0); 716 if (proto == IPPROTO_TCP) { 717 wildcard = 0; 718 pi = &V_tcbinfo; 719 } else if (proto == IPPROTO_UDP) { 720 wildcard = INPLOOKUP_WILDCARD; 721 pi = &V_udbinfo; 722 } else 723 return 0; 724 match = 0; 725 if (*ugid_lookupp == 0) { 726 INP_INFO_RLOCK(pi); 727 pcb = (oif) ? 728 in_pcblookup_hash(pi, 729 dst_ip, htons(dst_port), 730 src_ip, htons(src_port), 731 wildcard, oif) : 732 in_pcblookup_hash(pi, 733 src_ip, htons(src_port), 734 dst_ip, htons(dst_port), 735 wildcard, NULL); 736 if (pcb != NULL) { 737 *uc = crhold(pcb->inp_cred); 738 *ugid_lookupp = 1; 739 } 740 INP_INFO_RUNLOCK(pi); 741 if (*ugid_lookupp == 0) { 742 /* 743 * If the lookup did not yield any results, there 744 * is no sense in coming back and trying again. So 745 * we can set lookup to -1 and ensure that we wont 746 * bother the pcb system again. 747 */ 748 *ugid_lookupp = -1; 749 return (0); 750 } 751 } 752 if (insn->o.opcode == O_UID) 753 match = ((*uc)->cr_uid == (uid_t)insn->d[0]); 754 else if (insn->o.opcode == O_GID) 755 match = groupmember((gid_t)insn->d[0], *uc); 756 else if (insn->o.opcode == O_JAIL) 757 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]); 758 return match; 759} 760 761/* 762 * The main check routine for the firewall. 763 * 764 * All arguments are in args so we can modify them and return them 765 * back to the caller. 766 * 767 * Parameters: 768 * 769 * args->m (in/out) The packet; we set to NULL when/if we nuke it. 770 * Starts with the IP header. 771 * args->eh (in) Mac header if present, or NULL for layer3 packet. 772 * args->L3offset Number of bytes bypassed if we came from L2. 773 * e.g. often sizeof(eh) ** NOTYET ** 774 * args->oif Outgoing interface, or NULL if packet is incoming. 775 * The incoming interface is in the mbuf. (in) 776 * args->divert_rule (in/out) 777 * Skip up to the first rule past this rule number; 778 * upon return, non-zero port number for divert or tee. 779 * 780 * args->rule Pointer to the last matching rule (in/out) 781 * args->next_hop Socket we are forwarding to (out). 782 * args->f_id Addresses grabbed from the packet (out) 783 * args->cookie a cookie depending on rule action 784 * 785 * Return value: 786 * 787 * IP_FW_PASS the packet must be accepted 788 * IP_FW_DENY the packet must be dropped 789 * IP_FW_DIVERT divert packet, port in m_tag 790 * IP_FW_TEE tee packet, port in m_tag 791 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie 792 * IP_FW_NETGRAPH into netgraph, cookie args->cookie 793 * 794 */ 795int 796ipfw_chk(struct ip_fw_args *args) 797{ 798 799 /* 800 * Local variables holding state during the processing of a packet: 801 * 802 * IMPORTANT NOTE: to speed up the processing of rules, there 803 * are some assumption on the values of the variables, which 804 * are documented here. Should you change them, please check 805 * the implementation of the various instructions to make sure 806 * that they still work. 807 * 808 * args->eh The MAC header. It is non-null for a layer2 809 * packet, it is NULL for a layer-3 packet. 810 * **notyet** 811 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header. 812 * 813 * m | args->m Pointer to the mbuf, as received from the caller. 814 * It may change if ipfw_chk() does an m_pullup, or if it 815 * consumes the packet because it calls send_reject(). 816 * XXX This has to change, so that ipfw_chk() never modifies 817 * or consumes the buffer. 818 * ip is the beginning of the ip(4 or 6) header. 819 * Calculated by adding the L3offset to the start of data. 820 * (Until we start using L3offset, the packet is 821 * supposed to start with the ip header). 822 */ 823 struct mbuf *m = args->m; 824 struct ip *ip = mtod(m, struct ip *); 825 826 /* 827 * For rules which contain uid/gid or jail constraints, cache 828 * a copy of the users credentials after the pcb lookup has been 829 * executed. This will speed up the processing of rules with 830 * these types of constraints, as well as decrease contention 831 * on pcb related locks. 832 */ 833 struct ucred *ucred_cache = NULL; 834 int ucred_lookup = 0; 835 836 /* 837 * divinput_flags If non-zero, set to the IP_FW_DIVERT_*_FLAG 838 * associated with a packet input on a divert socket. This 839 * will allow to distinguish traffic and its direction when 840 * it originates from a divert socket. 841 */ 842 u_int divinput_flags = 0; 843 844 /* 845 * oif | args->oif If NULL, ipfw_chk has been called on the 846 * inbound path (ether_input, ip_input). 847 * If non-NULL, ipfw_chk has been called on the outbound path 848 * (ether_output, ip_output). 849 */ 850 struct ifnet *oif = args->oif; 851 852 struct ip_fw *f = NULL; /* matching rule */ 853 int retval = 0; 854 855 /* 856 * hlen The length of the IP header. 857 */ 858 u_int hlen = 0; /* hlen >0 means we have an IP pkt */ 859 860 /* 861 * offset The offset of a fragment. offset != 0 means that 862 * we have a fragment at this offset of an IPv4 packet. 863 * offset == 0 means that (if this is an IPv4 packet) 864 * this is the first or only fragment. 865 * For IPv6 offset == 0 means there is no Fragment Header. 866 * If offset != 0 for IPv6 always use correct mask to 867 * get the correct offset because we add IP6F_MORE_FRAG 868 * to be able to dectect the first fragment which would 869 * otherwise have offset = 0. 870 */ 871 u_short offset = 0; 872 873 /* 874 * Local copies of addresses. They are only valid if we have 875 * an IP packet. 876 * 877 * proto The protocol. Set to 0 for non-ip packets, 878 * or to the protocol read from the packet otherwise. 879 * proto != 0 means that we have an IPv4 packet. 880 * 881 * src_port, dst_port port numbers, in HOST format. Only 882 * valid for TCP and UDP packets. 883 * 884 * src_ip, dst_ip ip addresses, in NETWORK format. 885 * Only valid for IPv4 packets. 886 */ 887 u_int8_t proto; 888 u_int16_t src_port = 0, dst_port = 0; /* NOTE: host format */ 889 struct in_addr src_ip, dst_ip; /* NOTE: network format */ 890 u_int16_t ip_len=0; 891 int pktlen; 892 u_int16_t etype = 0; /* Host order stored ether type */ 893 894 /* 895 * dyn_dir = MATCH_UNKNOWN when rules unchecked, 896 * MATCH_NONE when checked and not matched (q = NULL), 897 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) 898 */ 899 int dyn_dir = MATCH_UNKNOWN; 900 ipfw_dyn_rule *q = NULL; 901 struct ip_fw_chain *chain = &V_layer3_chain; 902 struct m_tag *mtag; 903 904 /* 905 * We store in ulp a pointer to the upper layer protocol header. 906 * In the ipv4 case this is easy to determine from the header, 907 * but for ipv6 we might have some additional headers in the middle. 908 * ulp is NULL if not found. 909 */ 910 void *ulp = NULL; /* upper layer protocol pointer. */ 911 /* XXX ipv6 variables */ 912 int is_ipv6 = 0; 913 u_int16_t ext_hd = 0; /* bits vector for extension header filtering */ 914 /* end of ipv6 variables */ 915 int is_ipv4 = 0; 916 917 int done = 0; /* flag to exit the outer loop */ 918 919 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready)) 920 return (IP_FW_PASS); /* accept */ 921 922 dst_ip.s_addr = 0; /* make sure it is initialized */ 923 src_ip.s_addr = 0; /* make sure it is initialized */ 924 pktlen = m->m_pkthdr.len; 925 args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */ 926 proto = args->f_id.proto = 0; /* mark f_id invalid */ 927 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */ 928 929/* 930 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, 931 * then it sets p to point at the offset "len" in the mbuf. WARNING: the 932 * pointer might become stale after other pullups (but we never use it 933 * this way). 934 */ 935#define PULLUP_TO(_len, p, T) \ 936do { \ 937 int x = (_len) + sizeof(T); \ 938 if ((m)->m_len < x) { \ 939 args->m = m = m_pullup(m, x); \ 940 if (m == NULL) \ 941 goto pullup_failed; \ 942 } \ 943 p = (mtod(m, char *) + (_len)); \ 944} while (0) 945 946 /* 947 * if we have an ether header, 948 */ 949 if (args->eh) 950 etype = ntohs(args->eh->ether_type); 951 952 /* Identify IP packets and fill up variables. */ 953 if (pktlen >= sizeof(struct ip6_hdr) && 954 (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) { 955 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip; 956 is_ipv6 = 1; 957 args->f_id.addr_type = 6; 958 hlen = sizeof(struct ip6_hdr); 959 proto = ip6->ip6_nxt; 960 961 /* Search extension headers to find upper layer protocols */ 962 while (ulp == NULL) { 963 switch (proto) { 964 case IPPROTO_ICMPV6: 965 PULLUP_TO(hlen, ulp, struct icmp6_hdr); 966 args->f_id.flags = ICMP6(ulp)->icmp6_type; 967 break; 968 969 case IPPROTO_TCP: 970 PULLUP_TO(hlen, ulp, struct tcphdr); 971 dst_port = TCP(ulp)->th_dport; 972 src_port = TCP(ulp)->th_sport; 973 args->f_id.flags = TCP(ulp)->th_flags; 974 break; 975 976 case IPPROTO_SCTP: 977 PULLUP_TO(hlen, ulp, struct sctphdr); 978 src_port = SCTP(ulp)->src_port; 979 dst_port = SCTP(ulp)->dest_port; 980 break; 981 982 case IPPROTO_UDP: 983 PULLUP_TO(hlen, ulp, struct udphdr); 984 dst_port = UDP(ulp)->uh_dport; 985 src_port = UDP(ulp)->uh_sport; 986 break; 987 988 case IPPROTO_HOPOPTS: /* RFC 2460 */ 989 PULLUP_TO(hlen, ulp, struct ip6_hbh); 990 ext_hd |= EXT_HOPOPTS; 991 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 992 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 993 ulp = NULL; 994 break; 995 996 case IPPROTO_ROUTING: /* RFC 2460 */ 997 PULLUP_TO(hlen, ulp, struct ip6_rthdr); 998 switch (((struct ip6_rthdr *)ulp)->ip6r_type) { 999 case 0: 1000 ext_hd |= EXT_RTHDR0; 1001 break; 1002 case 2: 1003 ext_hd |= EXT_RTHDR2; 1004 break; 1005 default: 1006 printf("IPFW2: IPV6 - Unknown Routing " 1007 "Header type(%d)\n", 1008 ((struct ip6_rthdr *)ulp)->ip6r_type); 1009 if (V_fw_deny_unknown_exthdrs) 1010 return (IP_FW_DENY); 1011 break; 1012 } 1013 ext_hd |= EXT_ROUTING; 1014 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3; 1015 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; 1016 ulp = NULL; 1017 break; 1018 1019 case IPPROTO_FRAGMENT: /* RFC 2460 */ 1020 PULLUP_TO(hlen, ulp, struct ip6_frag); 1021 ext_hd |= EXT_FRAGMENT; 1022 hlen += sizeof (struct ip6_frag); 1023 proto = ((struct ip6_frag *)ulp)->ip6f_nxt; 1024 offset = ((struct ip6_frag *)ulp)->ip6f_offlg & 1025 IP6F_OFF_MASK; 1026 /* Add IP6F_MORE_FRAG for offset of first 1027 * fragment to be != 0. */ 1028 offset |= ((struct ip6_frag *)ulp)->ip6f_offlg & 1029 IP6F_MORE_FRAG; 1030 if (offset == 0) { 1031 printf("IPFW2: IPV6 - Invalid Fragment " 1032 "Header\n"); 1033 if (V_fw_deny_unknown_exthdrs) 1034 return (IP_FW_DENY); 1035 break; 1036 } 1037 args->f_id.frag_id6 = 1038 ntohl(((struct ip6_frag *)ulp)->ip6f_ident); 1039 ulp = NULL; 1040 break; 1041 1042 case IPPROTO_DSTOPTS: /* RFC 2460 */ 1043 PULLUP_TO(hlen, ulp, struct ip6_hbh); 1044 ext_hd |= EXT_DSTOPTS; 1045 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; 1046 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; 1047 ulp = NULL; 1048 break; 1049 1050 case IPPROTO_AH: /* RFC 2402 */ 1051 PULLUP_TO(hlen, ulp, struct ip6_ext); 1052 ext_hd |= EXT_AH; 1053 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2; 1054 proto = ((struct ip6_ext *)ulp)->ip6e_nxt; 1055 ulp = NULL; 1056 break; 1057 1058 case IPPROTO_ESP: /* RFC 2406 */ 1059 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */ 1060 /* Anything past Seq# is variable length and 1061 * data past this ext. header is encrypted. */ 1062 ext_hd |= EXT_ESP; 1063 break; 1064 1065 case IPPROTO_NONE: /* RFC 2460 */ 1066 /* 1067 * Packet ends here, and IPv6 header has 1068 * already been pulled up. If ip6e_len!=0 1069 * then octets must be ignored. 1070 */ 1071 ulp = ip; /* non-NULL to get out of loop. */ 1072 break; 1073 1074 case IPPROTO_OSPFIGP: 1075 /* XXX OSPF header check? */ 1076 PULLUP_TO(hlen, ulp, struct ip6_ext); 1077 break; 1078 1079 case IPPROTO_PIM: 1080 /* XXX PIM header check? */ 1081 PULLUP_TO(hlen, ulp, struct pim); 1082 break; 1083 1084 case IPPROTO_CARP: 1085 PULLUP_TO(hlen, ulp, struct carp_header); 1086 if (((struct carp_header *)ulp)->carp_version != 1087 CARP_VERSION) 1088 return (IP_FW_DENY); 1089 if (((struct carp_header *)ulp)->carp_type != 1090 CARP_ADVERTISEMENT) 1091 return (IP_FW_DENY); 1092 break; 1093 1094 case IPPROTO_IPV6: /* RFC 2893 */ 1095 PULLUP_TO(hlen, ulp, struct ip6_hdr); 1096 break; 1097 1098 case IPPROTO_IPV4: /* RFC 2893 */ 1099 PULLUP_TO(hlen, ulp, struct ip); 1100 break; 1101 1102 default: 1103 printf("IPFW2: IPV6 - Unknown Extension " 1104 "Header(%d), ext_hd=%x\n", proto, ext_hd); 1105 if (V_fw_deny_unknown_exthdrs) 1106 return (IP_FW_DENY); 1107 PULLUP_TO(hlen, ulp, struct ip6_ext); 1108 break; 1109 } /*switch */ 1110 } 1111 ip = mtod(m, struct ip *); 1112 ip6 = (struct ip6_hdr *)ip; 1113 args->f_id.src_ip6 = ip6->ip6_src; 1114 args->f_id.dst_ip6 = ip6->ip6_dst; 1115 args->f_id.src_ip = 0; 1116 args->f_id.dst_ip = 0; 1117 args->f_id.flow_id6 = ntohl(ip6->ip6_flow); 1118 } else if (pktlen >= sizeof(struct ip) && 1119 (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) { 1120 is_ipv4 = 1; 1121 hlen = ip->ip_hl << 2; 1122 args->f_id.addr_type = 4; 1123 1124 /* 1125 * Collect parameters into local variables for faster matching. 1126 */ 1127 proto = ip->ip_p; 1128 src_ip = ip->ip_src; 1129 dst_ip = ip->ip_dst; 1130 if (args->eh != NULL) { /* layer 2 packets are as on the wire */ 1131 offset = ntohs(ip->ip_off) & IP_OFFMASK; 1132 ip_len = ntohs(ip->ip_len); 1133 } else { 1134 offset = ip->ip_off & IP_OFFMASK; 1135 ip_len = ip->ip_len; 1136 } 1137 pktlen = ip_len < pktlen ? ip_len : pktlen; 1138 1139 if (offset == 0) { 1140 switch (proto) { 1141 case IPPROTO_TCP: 1142 PULLUP_TO(hlen, ulp, struct tcphdr); 1143 dst_port = TCP(ulp)->th_dport; 1144 src_port = TCP(ulp)->th_sport; 1145 args->f_id.flags = TCP(ulp)->th_flags; 1146 break; 1147 1148 case IPPROTO_UDP: 1149 PULLUP_TO(hlen, ulp, struct udphdr); 1150 dst_port = UDP(ulp)->uh_dport; 1151 src_port = UDP(ulp)->uh_sport; 1152 break; 1153 1154 case IPPROTO_ICMP: 1155 PULLUP_TO(hlen, ulp, struct icmphdr); 1156 args->f_id.flags = ICMP(ulp)->icmp_type; 1157 break; 1158 1159 default: 1160 break; 1161 } 1162 } 1163 1164 ip = mtod(m, struct ip *); 1165 args->f_id.src_ip = ntohl(src_ip.s_addr); 1166 args->f_id.dst_ip = ntohl(dst_ip.s_addr); 1167 } 1168#undef PULLUP_TO 1169 if (proto) { /* we may have port numbers, store them */ 1170 args->f_id.proto = proto; 1171 args->f_id.src_port = src_port = ntohs(src_port); 1172 args->f_id.dst_port = dst_port = ntohs(dst_port); 1173 } 1174 1175 IPFW_RLOCK(chain); 1176 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */ 1177 IPFW_RUNLOCK(chain); 1178 return (IP_FW_PASS); /* accept */ 1179 } 1180 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 1181 if (args->rule) { 1182 /* 1183 * Packet has already been tagged. Look for the next rule 1184 * to restart processing. Make sure that args->rule still 1185 * exists and not changed. 1186 * If fw_one_pass != 0 then just accept it. 1187 * XXX should not happen here, but optimized out in 1188 * the caller. 1189 */ 1190 if (V_fw_one_pass) { 1191 IPFW_RUNLOCK(chain); 1192 return (IP_FW_PASS); 1193 } 1194 if (chain->id != args->chain_id) { 1195 for (f = chain->rules; f != NULL; f = f->next) 1196 if (f == args->rule && f->id == args->rule_id) 1197 break; 1198 1199 if (f != NULL) 1200 f = f->next_rule; 1201 else 1202 f = ip_fw_default_rule; 1203 } else 1204 f = args->rule->next_rule; 1205 1206 if (f == NULL) 1207 f = lookup_next_rule(args->rule, 0); 1208 } else { 1209 /* 1210 * Find the starting rule. It can be either the first 1211 * one, or the one after divert_rule if asked so. 1212 */ 1213 int skipto = mtag ? divert_cookie(mtag) : 0; 1214 1215 f = chain->rules; 1216 if (args->eh == NULL && skipto != 0) { 1217 if (skipto >= IPFW_DEFAULT_RULE) { 1218 IPFW_RUNLOCK(chain); 1219 return (IP_FW_DENY); /* invalid */ 1220 } 1221 while (f && f->rulenum <= skipto) 1222 f = f->next; 1223 if (f == NULL) { /* drop packet */ 1224 IPFW_RUNLOCK(chain); 1225 return (IP_FW_DENY); 1226 } 1227 } 1228 } 1229 /* reset divert rule to avoid confusion later */ 1230 if (mtag) { 1231 divinput_flags = divert_info(mtag) & 1232 (IP_FW_DIVERT_OUTPUT_FLAG | IP_FW_DIVERT_LOOPBACK_FLAG); 1233 m_tag_delete(m, mtag); 1234 } 1235 1236 /* 1237 * Now scan the rules, and parse microinstructions for each rule. 1238 * We have two nested loops and an inner switch. Sometimes we 1239 * need to break out of one or both loops, or re-enter one of 1240 * the loops with updated variables. Loop variables are: 1241 * 1242 * f (outer loop) points to the current rule. 1243 * On output it points to the matching rule. 1244 * done (outer loop) is used as a flag to break the loop. 1245 * l (inner loop) residual length of current rule. 1246 * cmd points to the current microinstruction. 1247 * 1248 * We break the inner loop by setting l=0 and possibly 1249 * cmdlen=0 if we don't want to advance cmd. 1250 * We break the outer loop by setting done=1 1251 * We can restart the inner loop by setting l>0 and f, cmd 1252 * as needed. 1253 */ 1254 for (; f; f = f->next) { 1255 ipfw_insn *cmd; 1256 uint32_t tablearg = 0; 1257 int l, cmdlen, skip_or; /* skip rest of OR block */ 1258 1259/* again: */ 1260 if (V_set_disable & (1 << f->set) ) 1261 continue; 1262 1263 skip_or = 0; 1264 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; 1265 l -= cmdlen, cmd += cmdlen) { 1266 int match; 1267 1268 /* 1269 * check_body is a jump target used when we find a 1270 * CHECK_STATE, and need to jump to the body of 1271 * the target rule. 1272 */ 1273 1274/* check_body: */ 1275 cmdlen = F_LEN(cmd); 1276 /* 1277 * An OR block (insn_1 || .. || insn_n) has the 1278 * F_OR bit set in all but the last instruction. 1279 * The first match will set "skip_or", and cause 1280 * the following instructions to be skipped until 1281 * past the one with the F_OR bit clear. 1282 */ 1283 if (skip_or) { /* skip this instruction */ 1284 if ((cmd->len & F_OR) == 0) 1285 skip_or = 0; /* next one is good */ 1286 continue; 1287 } 1288 match = 0; /* set to 1 if we succeed */ 1289 1290 switch (cmd->opcode) { 1291 /* 1292 * The first set of opcodes compares the packet's 1293 * fields with some pattern, setting 'match' if a 1294 * match is found. At the end of the loop there is 1295 * logic to deal with F_NOT and F_OR flags associated 1296 * with the opcode. 1297 */ 1298 case O_NOP: 1299 match = 1; 1300 break; 1301 1302 case O_FORWARD_MAC: 1303 printf("ipfw: opcode %d unimplemented\n", 1304 cmd->opcode); 1305 break; 1306 1307 case O_GID: 1308 case O_UID: 1309 case O_JAIL: 1310 /* 1311 * We only check offset == 0 && proto != 0, 1312 * as this ensures that we have a 1313 * packet with the ports info. 1314 */ 1315 if (offset!=0) 1316 break; 1317 if (is_ipv6) /* XXX to be fixed later */ 1318 break; 1319 if (proto == IPPROTO_TCP || 1320 proto == IPPROTO_UDP) 1321 match = check_uidgid( 1322 (ipfw_insn_u32 *)cmd, 1323 proto, oif, 1324 dst_ip, dst_port, 1325 src_ip, src_port, &ucred_cache, 1326 &ucred_lookup, args->inp); 1327 break; 1328 1329 case O_RECV: 1330 match = iface_match(m->m_pkthdr.rcvif, 1331 (ipfw_insn_if *)cmd); 1332 break; 1333 1334 case O_XMIT: 1335 match = iface_match(oif, (ipfw_insn_if *)cmd); 1336 break; 1337 1338 case O_VIA: 1339 match = iface_match(oif ? oif : 1340 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd); 1341 break; 1342 1343 case O_MACADDR2: 1344 if (args->eh != NULL) { /* have MAC header */ 1345 u_int32_t *want = (u_int32_t *) 1346 ((ipfw_insn_mac *)cmd)->addr; 1347 u_int32_t *mask = (u_int32_t *) 1348 ((ipfw_insn_mac *)cmd)->mask; 1349 u_int32_t *hdr = (u_int32_t *)args->eh; 1350 1351 match = 1352 ( want[0] == (hdr[0] & mask[0]) && 1353 want[1] == (hdr[1] & mask[1]) && 1354 want[2] == (hdr[2] & mask[2]) ); 1355 } 1356 break; 1357 1358 case O_MAC_TYPE: 1359 if (args->eh != NULL) { 1360 u_int16_t *p = 1361 ((ipfw_insn_u16 *)cmd)->ports; 1362 int i; 1363 1364 for (i = cmdlen - 1; !match && i>0; 1365 i--, p += 2) 1366 match = (etype >= p[0] && 1367 etype <= p[1]); 1368 } 1369 break; 1370 1371 case O_FRAG: 1372 match = (offset != 0); 1373 break; 1374 1375 case O_IN: /* "out" is "not in" */ 1376 match = (oif == NULL); 1377 break; 1378 1379 case O_LAYER2: 1380 match = (args->eh != NULL); 1381 break; 1382 1383 case O_DIVERTED: 1384 match = (cmd->arg1 & 1 && divinput_flags & 1385 IP_FW_DIVERT_LOOPBACK_FLAG) || 1386 (cmd->arg1 & 2 && divinput_flags & 1387 IP_FW_DIVERT_OUTPUT_FLAG); 1388 break; 1389 1390 case O_PROTO: 1391 /* 1392 * We do not allow an arg of 0 so the 1393 * check of "proto" only suffices. 1394 */ 1395 match = (proto == cmd->arg1); 1396 break; 1397 1398 case O_IP_SRC: 1399 match = is_ipv4 && 1400 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1401 src_ip.s_addr); 1402 break; 1403 1404 case O_IP_SRC_LOOKUP: 1405 case O_IP_DST_LOOKUP: 1406 if (is_ipv4) { 1407 uint32_t a = 1408 (cmd->opcode == O_IP_DST_LOOKUP) ? 1409 dst_ip.s_addr : src_ip.s_addr; 1410 uint32_t v = 0; 1411 1412 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) { 1413 /* generic lookup */ 1414 v = ((ipfw_insn_u32 *)cmd)->d[1]; 1415 if (v == 0) 1416 a = dst_ip.s_addr; 1417 else if (v == 1) 1418 a = src_ip.s_addr; 1419 else if (offset != 0) 1420 break; 1421 else if (proto != IPPROTO_TCP && 1422 proto != IPPROTO_UDP) 1423 break; 1424 else if (v == 2) 1425 a = dst_port; 1426 else if (v == 3) 1427 a = src_port; 1428 else if (v == 4 || v == 5) { 1429 check_uidgid( 1430 (ipfw_insn_u32 *)cmd, 1431 proto, oif, 1432 dst_ip, dst_port, 1433 src_ip, src_port, &ucred_cache, 1434 &ucred_lookup, args->inp); 1435 if (v == 4 /* O_UID */) 1436 a = ucred_cache->cr_uid; 1437 else if (v == 5 /* O_JAIL */) 1438 a = ucred_cache->cr_prison->pr_id; 1439 } else 1440 break; 1441 } 1442 match = ipfw_lookup_table(chain, cmd->arg1, a, 1443 &v); 1444 if (!match) 1445 break; 1446 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) 1447 match = 1448 ((ipfw_insn_u32 *)cmd)->d[0] == v; 1449 else 1450 tablearg = v; 1451 } 1452 break; 1453 1454 case O_IP_SRC_MASK: 1455 case O_IP_DST_MASK: 1456 if (is_ipv4) { 1457 uint32_t a = 1458 (cmd->opcode == O_IP_DST_MASK) ? 1459 dst_ip.s_addr : src_ip.s_addr; 1460 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; 1461 int i = cmdlen-1; 1462 1463 for (; !match && i>0; i-= 2, p+= 2) 1464 match = (p[0] == (a & p[1])); 1465 } 1466 break; 1467 1468 case O_IP_SRC_ME: 1469 if (is_ipv4) { 1470 struct ifnet *tif; 1471 1472 INADDR_TO_IFP(src_ip, tif); 1473 match = (tif != NULL); 1474 } 1475 break; 1476 1477 case O_IP_DST_SET: 1478 case O_IP_SRC_SET: 1479 if (is_ipv4) { 1480 u_int32_t *d = (u_int32_t *)(cmd+1); 1481 u_int32_t addr = 1482 cmd->opcode == O_IP_DST_SET ? 1483 args->f_id.dst_ip : 1484 args->f_id.src_ip; 1485 1486 if (addr < d[0]) 1487 break; 1488 addr -= d[0]; /* subtract base */ 1489 match = (addr < cmd->arg1) && 1490 ( d[ 1 + (addr>>5)] & 1491 (1<<(addr & 0x1f)) ); 1492 } 1493 break; 1494 1495 case O_IP_DST: 1496 match = is_ipv4 && 1497 (((ipfw_insn_ip *)cmd)->addr.s_addr == 1498 dst_ip.s_addr); 1499 break; 1500 1501 case O_IP_DST_ME: 1502 if (is_ipv4) { 1503 struct ifnet *tif; 1504 1505 INADDR_TO_IFP(dst_ip, tif); 1506 match = (tif != NULL); 1507 } 1508 break; 1509 1510 case O_IP_SRCPORT: 1511 case O_IP_DSTPORT: 1512 /* 1513 * offset == 0 && proto != 0 is enough 1514 * to guarantee that we have a 1515 * packet with port info. 1516 */ 1517 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP) 1518 && offset == 0) { 1519 u_int16_t x = 1520 (cmd->opcode == O_IP_SRCPORT) ? 1521 src_port : dst_port ; 1522 u_int16_t *p = 1523 ((ipfw_insn_u16 *)cmd)->ports; 1524 int i; 1525 1526 for (i = cmdlen - 1; !match && i>0; 1527 i--, p += 2) 1528 match = (x>=p[0] && x<=p[1]); 1529 } 1530 break; 1531 1532 case O_ICMPTYPE: 1533 match = (offset == 0 && proto==IPPROTO_ICMP && 1534 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); 1535 break; 1536 1537#ifdef INET6 1538 case O_ICMP6TYPE: 1539 match = is_ipv6 && offset == 0 && 1540 proto==IPPROTO_ICMPV6 && 1541 icmp6type_match( 1542 ICMP6(ulp)->icmp6_type, 1543 (ipfw_insn_u32 *)cmd); 1544 break; 1545#endif /* INET6 */ 1546 1547 case O_IPOPT: 1548 match = (is_ipv4 && 1549 ipopts_match(ip, cmd) ); 1550 break; 1551 1552 case O_IPVER: 1553 match = (is_ipv4 && 1554 cmd->arg1 == ip->ip_v); 1555 break; 1556 1557 case O_IPID: 1558 case O_IPLEN: 1559 case O_IPTTL: 1560 if (is_ipv4) { /* only for IP packets */ 1561 uint16_t x; 1562 uint16_t *p; 1563 int i; 1564 1565 if (cmd->opcode == O_IPLEN) 1566 x = ip_len; 1567 else if (cmd->opcode == O_IPTTL) 1568 x = ip->ip_ttl; 1569 else /* must be IPID */ 1570 x = ntohs(ip->ip_id); 1571 if (cmdlen == 1) { 1572 match = (cmd->arg1 == x); 1573 break; 1574 } 1575 /* otherwise we have ranges */ 1576 p = ((ipfw_insn_u16 *)cmd)->ports; 1577 i = cmdlen - 1; 1578 for (; !match && i>0; i--, p += 2) 1579 match = (x >= p[0] && x <= p[1]); 1580 } 1581 break; 1582 1583 case O_IPPRECEDENCE: 1584 match = (is_ipv4 && 1585 (cmd->arg1 == (ip->ip_tos & 0xe0)) ); 1586 break; 1587 1588 case O_IPTOS: 1589 match = (is_ipv4 && 1590 flags_match(cmd, ip->ip_tos)); 1591 break; 1592 1593 case O_TCPDATALEN: 1594 if (proto == IPPROTO_TCP && offset == 0) { 1595 struct tcphdr *tcp; 1596 uint16_t x; 1597 uint16_t *p; 1598 int i; 1599 1600 tcp = TCP(ulp); 1601 x = ip_len - 1602 ((ip->ip_hl + tcp->th_off) << 2); 1603 if (cmdlen == 1) { 1604 match = (cmd->arg1 == x); 1605 break; 1606 } 1607 /* otherwise we have ranges */ 1608 p = ((ipfw_insn_u16 *)cmd)->ports; 1609 i = cmdlen - 1; 1610 for (; !match && i>0; i--, p += 2) 1611 match = (x >= p[0] && x <= p[1]); 1612 } 1613 break; 1614 1615 case O_TCPFLAGS: 1616 match = (proto == IPPROTO_TCP && offset == 0 && 1617 flags_match(cmd, TCP(ulp)->th_flags)); 1618 break; 1619 1620 case O_TCPOPTS: 1621 match = (proto == IPPROTO_TCP && offset == 0 && 1622 tcpopts_match(TCP(ulp), cmd)); 1623 break; 1624 1625 case O_TCPSEQ: 1626 match = (proto == IPPROTO_TCP && offset == 0 && 1627 ((ipfw_insn_u32 *)cmd)->d[0] == 1628 TCP(ulp)->th_seq); 1629 break; 1630 1631 case O_TCPACK: 1632 match = (proto == IPPROTO_TCP && offset == 0 && 1633 ((ipfw_insn_u32 *)cmd)->d[0] == 1634 TCP(ulp)->th_ack); 1635 break; 1636 1637 case O_TCPWIN: 1638 match = (proto == IPPROTO_TCP && offset == 0 && 1639 cmd->arg1 == TCP(ulp)->th_win); 1640 break; 1641 1642 case O_ESTAB: 1643 /* reject packets which have SYN only */ 1644 /* XXX should i also check for TH_ACK ? */ 1645 match = (proto == IPPROTO_TCP && offset == 0 && 1646 (TCP(ulp)->th_flags & 1647 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); 1648 break; 1649 1650 case O_ALTQ: { 1651 struct pf_mtag *at; 1652 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 1653 1654 match = 1; 1655 at = pf_find_mtag(m); 1656 if (at != NULL && at->qid != 0) 1657 break; 1658 at = pf_get_mtag(m); 1659 if (at == NULL) { 1660 /* 1661 * Let the packet fall back to the 1662 * default ALTQ. 1663 */ 1664 break; 1665 } 1666 at->qid = altq->qid; 1667 if (is_ipv4) 1668 at->af = AF_INET; 1669 else 1670 at->af = AF_LINK; 1671 at->hdr = ip; 1672 break; 1673 } 1674 1675 case O_LOG: 1676 if (V_fw_verbose) 1677 ipfw_log(f, hlen, args, m, 1678 oif, offset, tablearg, ip); 1679 match = 1; 1680 break; 1681 1682 case O_PROB: 1683 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); 1684 break; 1685 1686 case O_VERREVPATH: 1687 /* Outgoing packets automatically pass/match */ 1688 match = ((oif != NULL) || 1689 (m->m_pkthdr.rcvif == NULL) || 1690 ( 1691#ifdef INET6 1692 is_ipv6 ? 1693 verify_path6(&(args->f_id.src_ip6), 1694 m->m_pkthdr.rcvif) : 1695#endif 1696 verify_path(src_ip, m->m_pkthdr.rcvif, 1697 args->f_id.fib))); 1698 break; 1699 1700 case O_VERSRCREACH: 1701 /* Outgoing packets automatically pass/match */ 1702 match = (hlen > 0 && ((oif != NULL) || 1703#ifdef INET6 1704 is_ipv6 ? 1705 verify_path6(&(args->f_id.src_ip6), 1706 NULL) : 1707#endif 1708 verify_path(src_ip, NULL, args->f_id.fib))); 1709 break; 1710 1711 case O_ANTISPOOF: 1712 /* Outgoing packets automatically pass/match */ 1713 if (oif == NULL && hlen > 0 && 1714 ( (is_ipv4 && in_localaddr(src_ip)) 1715#ifdef INET6 1716 || (is_ipv6 && 1717 in6_localaddr(&(args->f_id.src_ip6))) 1718#endif 1719 )) 1720 match = 1721#ifdef INET6 1722 is_ipv6 ? verify_path6( 1723 &(args->f_id.src_ip6), 1724 m->m_pkthdr.rcvif) : 1725#endif 1726 verify_path(src_ip, 1727 m->m_pkthdr.rcvif, 1728 args->f_id.fib); 1729 else 1730 match = 1; 1731 break; 1732 1733 case O_IPSEC: 1734#ifdef IPSEC 1735 match = (m_tag_find(m, 1736 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); 1737#endif 1738 /* otherwise no match */ 1739 break; 1740 1741#ifdef INET6 1742 case O_IP6_SRC: 1743 match = is_ipv6 && 1744 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, 1745 &((ipfw_insn_ip6 *)cmd)->addr6); 1746 break; 1747 1748 case O_IP6_DST: 1749 match = is_ipv6 && 1750 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, 1751 &((ipfw_insn_ip6 *)cmd)->addr6); 1752 break; 1753 case O_IP6_SRC_MASK: 1754 case O_IP6_DST_MASK: 1755 if (is_ipv6) { 1756 int i = cmdlen - 1; 1757 struct in6_addr p; 1758 struct in6_addr *d = 1759 &((ipfw_insn_ip6 *)cmd)->addr6; 1760 1761 for (; !match && i > 0; d += 2, 1762 i -= F_INSN_SIZE(struct in6_addr) 1763 * 2) { 1764 p = (cmd->opcode == 1765 O_IP6_SRC_MASK) ? 1766 args->f_id.src_ip6: 1767 args->f_id.dst_ip6; 1768 APPLY_MASK(&p, &d[1]); 1769 match = 1770 IN6_ARE_ADDR_EQUAL(&d[0], 1771 &p); 1772 } 1773 } 1774 break; 1775 1776 case O_IP6_SRC_ME: 1777 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6); 1778 break; 1779 1780 case O_IP6_DST_ME: 1781 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6); 1782 break; 1783 1784 case O_FLOW6ID: 1785 match = is_ipv6 && 1786 flow6id_match(args->f_id.flow_id6, 1787 (ipfw_insn_u32 *) cmd); 1788 break; 1789 1790 case O_EXT_HDR: 1791 match = is_ipv6 && 1792 (ext_hd & ((ipfw_insn *) cmd)->arg1); 1793 break; 1794 1795 case O_IP6: 1796 match = is_ipv6; 1797 break; 1798#endif 1799 1800 case O_IP4: 1801 match = is_ipv4; 1802 break; 1803 1804 case O_TAG: { 1805 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1806 tablearg : cmd->arg1; 1807 1808 /* Packet is already tagged with this tag? */ 1809 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL); 1810 1811 /* We have `untag' action when F_NOT flag is 1812 * present. And we must remove this mtag from 1813 * mbuf and reset `match' to zero (`match' will 1814 * be inversed later). 1815 * Otherwise we should allocate new mtag and 1816 * push it into mbuf. 1817 */ 1818 if (cmd->len & F_NOT) { /* `untag' action */ 1819 if (mtag != NULL) 1820 m_tag_delete(m, mtag); 1821 } else if (mtag == NULL) { 1822 if ((mtag = m_tag_alloc(MTAG_IPFW, 1823 tag, 0, M_NOWAIT)) != NULL) 1824 m_tag_prepend(m, mtag); 1825 } 1826 match = (cmd->len & F_NOT) ? 0: 1; 1827 break; 1828 } 1829 1830 case O_FIB: /* try match the specified fib */ 1831 if (args->f_id.fib == cmd->arg1) 1832 match = 1; 1833 break; 1834 1835 case O_TAGGED: { 1836 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ? 1837 tablearg : cmd->arg1; 1838 1839 if (cmdlen == 1) { 1840 match = m_tag_locate(m, MTAG_IPFW, 1841 tag, NULL) != NULL; 1842 break; 1843 } 1844 1845 /* we have ranges */ 1846 for (mtag = m_tag_first(m); 1847 mtag != NULL && !match; 1848 mtag = m_tag_next(m, mtag)) { 1849 uint16_t *p; 1850 int i; 1851 1852 if (mtag->m_tag_cookie != MTAG_IPFW) 1853 continue; 1854 1855 p = ((ipfw_insn_u16 *)cmd)->ports; 1856 i = cmdlen - 1; 1857 for(; !match && i > 0; i--, p += 2) 1858 match = 1859 mtag->m_tag_id >= p[0] && 1860 mtag->m_tag_id <= p[1]; 1861 } 1862 break; 1863 } 1864 1865 /* 1866 * The second set of opcodes represents 'actions', 1867 * i.e. the terminal part of a rule once the packet 1868 * matches all previous patterns. 1869 * Typically there is only one action for each rule, 1870 * and the opcode is stored at the end of the rule 1871 * (but there are exceptions -- see below). 1872 * 1873 * In general, here we set retval and terminate the 1874 * outer loop (would be a 'break 3' in some language, 1875 * but we need to set l=0, done=1) 1876 * 1877 * Exceptions: 1878 * O_COUNT and O_SKIPTO actions: 1879 * instead of terminating, we jump to the next rule 1880 * (setting l=0), or to the SKIPTO target (by 1881 * setting f, cmd and l as needed), respectively. 1882 * 1883 * O_TAG, O_LOG and O_ALTQ action parameters: 1884 * perform some action and set match = 1; 1885 * 1886 * O_LIMIT and O_KEEP_STATE: these opcodes are 1887 * not real 'actions', and are stored right 1888 * before the 'action' part of the rule. 1889 * These opcodes try to install an entry in the 1890 * state tables; if successful, we continue with 1891 * the next opcode (match=1; break;), otherwise 1892 * the packet must be dropped (set retval, 1893 * break loops with l=0, done=1) 1894 * 1895 * O_PROBE_STATE and O_CHECK_STATE: these opcodes 1896 * cause a lookup of the state table, and a jump 1897 * to the 'action' part of the parent rule 1898 * if an entry is found, or 1899 * (CHECK_STATE only) a jump to the next rule if 1900 * the entry is not found. 1901 * The result of the lookup is cached so that 1902 * further instances of these opcodes become NOPs. 1903 * The jump to the next rule is done by setting 1904 * l=0, cmdlen=0. 1905 */ 1906 case O_LIMIT: 1907 case O_KEEP_STATE: 1908 if (install_state(f, 1909 (ipfw_insn_limit *)cmd, args, tablearg)) { 1910 /* error or limit violation */ 1911 retval = IP_FW_DENY; 1912 l = 0; /* exit inner loop */ 1913 done = 1; /* exit outer loop */ 1914 } 1915 match = 1; 1916 break; 1917 1918 case O_PROBE_STATE: 1919 case O_CHECK_STATE: 1920 /* 1921 * dynamic rules are checked at the first 1922 * keep-state or check-state occurrence, 1923 * with the result being stored in dyn_dir. 1924 * The compiler introduces a PROBE_STATE 1925 * instruction for us when we have a 1926 * KEEP_STATE (because PROBE_STATE needs 1927 * to be run first). 1928 */ 1929 if (dyn_dir == MATCH_UNKNOWN && 1930 (q = lookup_dyn_rule(&args->f_id, 1931 &dyn_dir, proto == IPPROTO_TCP ? 1932 TCP(ulp) : NULL)) 1933 != NULL) { 1934 /* 1935 * Found dynamic entry, update stats 1936 * and jump to the 'action' part of 1937 * the parent rule by setting 1938 * f, cmd, l and clearing cmdlen. 1939 */ 1940 q->pcnt++; 1941 q->bcnt += pktlen; 1942 f = q->rule; 1943 cmd = ACTION_PTR(f); 1944 l = f->cmd_len - f->act_ofs; 1945 ipfw_dyn_unlock(); 1946 cmdlen = 0; 1947 match = 1; 1948 break; 1949 } 1950 /* 1951 * Dynamic entry not found. If CHECK_STATE, 1952 * skip to next rule, if PROBE_STATE just 1953 * ignore and continue with next opcode. 1954 */ 1955 if (cmd->opcode == O_CHECK_STATE) 1956 l = 0; /* exit inner loop */ 1957 match = 1; 1958 break; 1959 1960 case O_ACCEPT: 1961 retval = 0; /* accept */ 1962 l = 0; /* exit inner loop */ 1963 done = 1; /* exit outer loop */ 1964 break; 1965 1966 case O_PIPE: 1967 case O_QUEUE: 1968 args->rule = f; /* report matching rule */ 1969 args->rule_id = f->id; 1970 args->chain_id = chain->id; 1971 if (cmd->arg1 == IP_FW_TABLEARG) 1972 args->cookie = tablearg; 1973 else 1974 args->cookie = cmd->arg1; 1975 retval = IP_FW_DUMMYNET; 1976 l = 0; /* exit inner loop */ 1977 done = 1; /* exit outer loop */ 1978 break; 1979 1980 case O_DIVERT: 1981 case O_TEE: 1982 if (args->eh) /* not on layer 2 */ 1983 break; 1984 /* otherwise this is terminal */ 1985 l = 0; /* exit inner loop */ 1986 done = 1; /* exit outer loop */ 1987 mtag = m_tag_get(PACKET_TAG_DIVERT, 1988 sizeof(struct divert_tag), 1989 M_NOWAIT); 1990 if (mtag == NULL) { 1991 retval = IP_FW_DENY; 1992 } else { 1993 struct divert_tag *dt; 1994 dt = (struct divert_tag *)(mtag+1); 1995 dt->cookie = f->rulenum; 1996 if (cmd->arg1 == IP_FW_TABLEARG) 1997 dt->info = tablearg; 1998 else 1999 dt->info = cmd->arg1; 2000 m_tag_prepend(m, mtag); 2001 retval = (cmd->opcode == O_DIVERT) ? 2002 IP_FW_DIVERT : IP_FW_TEE; 2003 } 2004 break; 2005 2006 case O_COUNT: 2007 case O_SKIPTO: 2008 f->pcnt++; /* update stats */ 2009 f->bcnt += pktlen; 2010 f->timestamp = time_uptime; 2011 if (cmd->opcode == O_COUNT) { 2012 l = 0; /* exit inner loop */ 2013 break; 2014 } 2015 /* handle skipto */ 2016 if (cmd->arg1 == IP_FW_TABLEARG) { 2017 f = lookup_next_rule(f, tablearg); 2018 } else { 2019 if (f->next_rule == NULL) 2020 lookup_next_rule(f, 0); 2021 f = f->next_rule; 2022 } 2023 /* 2024 * Skip disabled rules, and 2025 * re-enter the inner loop 2026 * with the correct f, l and cmd. 2027 * Also clear cmdlen and skip_or 2028 */ 2029 while (f && (V_set_disable & (1 << f->set))) 2030 f = f->next; 2031 if (f) { /* found a valid rule */ 2032 l = f->cmd_len; 2033 cmd = f->cmd; 2034 } else { 2035 l = 0; /* exit inner loop */ 2036 } 2037 match = 1; 2038 cmdlen = 0; 2039 skip_or = 0; 2040 break; 2041 2042 case O_REJECT: 2043 /* 2044 * Drop the packet and send a reject notice 2045 * if the packet is not ICMP (or is an ICMP 2046 * query), and it is not multicast/broadcast. 2047 */ 2048 if (hlen > 0 && is_ipv4 && offset == 0 && 2049 (proto != IPPROTO_ICMP || 2050 is_icmp_query(ICMP(ulp))) && 2051 !(m->m_flags & (M_BCAST|M_MCAST)) && 2052 !IN_MULTICAST(ntohl(dst_ip.s_addr))) { 2053 send_reject(args, cmd->arg1, ip_len, ip); 2054 m = args->m; 2055 } 2056 /* FALLTHROUGH */ 2057#ifdef INET6 2058 case O_UNREACH6: 2059 if (hlen > 0 && is_ipv6 && 2060 ((offset & IP6F_OFF_MASK) == 0) && 2061 (proto != IPPROTO_ICMPV6 || 2062 (is_icmp6_query(args->f_id.flags) == 1)) && 2063 !(m->m_flags & (M_BCAST|M_MCAST)) && 2064 !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) { 2065 send_reject6( 2066 args, cmd->arg1, hlen, 2067 (struct ip6_hdr *)ip); 2068 m = args->m; 2069 } 2070 /* FALLTHROUGH */ 2071#endif 2072 case O_DENY: 2073 retval = IP_FW_DENY; 2074 l = 0; /* exit inner loop */ 2075 done = 1; /* exit outer loop */ 2076 break; 2077 2078 case O_FORWARD_IP: 2079 if (args->eh) /* not valid on layer2 pkts */ 2080 break; 2081 if (!q || dyn_dir == MATCH_FORWARD) { 2082 struct sockaddr_in *sa; 2083 sa = &(((ipfw_insn_sa *)cmd)->sa); 2084 if (sa->sin_addr.s_addr == INADDR_ANY) { 2085 bcopy(sa, &args->hopstore, 2086 sizeof(*sa)); 2087 args->hopstore.sin_addr.s_addr = 2088 htonl(tablearg); 2089 args->next_hop = &args->hopstore; 2090 } else { 2091 args->next_hop = sa; 2092 } 2093 } 2094 retval = IP_FW_PASS; 2095 l = 0; /* exit inner loop */ 2096 done = 1; /* exit outer loop */ 2097 break; 2098 2099 case O_NETGRAPH: 2100 case O_NGTEE: 2101 args->rule = f; /* report matching rule */ 2102 args->rule_id = f->id; 2103 args->chain_id = chain->id; 2104 if (cmd->arg1 == IP_FW_TABLEARG) 2105 args->cookie = tablearg; 2106 else 2107 args->cookie = cmd->arg1; 2108 retval = (cmd->opcode == O_NETGRAPH) ? 2109 IP_FW_NETGRAPH : IP_FW_NGTEE; 2110 l = 0; /* exit inner loop */ 2111 done = 1; /* exit outer loop */ 2112 break; 2113 2114 case O_SETFIB: 2115 f->pcnt++; /* update stats */ 2116 f->bcnt += pktlen; 2117 f->timestamp = time_uptime; 2118 M_SETFIB(m, cmd->arg1); 2119 args->f_id.fib = cmd->arg1; 2120 l = 0; /* exit inner loop */ 2121 break; 2122 2123 case O_NAT: 2124 if (!IPFW_NAT_LOADED) { 2125 retval = IP_FW_DENY; 2126 } else { 2127 struct cfg_nat *t; 2128 int nat_id; 2129 2130 args->rule = f; /* Report matching rule. */ 2131 args->rule_id = f->id; 2132 args->chain_id = chain->id; 2133 t = ((ipfw_insn_nat *)cmd)->nat; 2134 if (t == NULL) { 2135 nat_id = (cmd->arg1 == IP_FW_TABLEARG) ? 2136 tablearg : cmd->arg1; 2137 t = (*lookup_nat_ptr)(&V_layer3_chain.nat, nat_id); 2138 2139 if (t == NULL) { 2140 retval = IP_FW_DENY; 2141 l = 0; /* exit inner loop */ 2142 done = 1; /* exit outer loop */ 2143 break; 2144 } 2145 if (cmd->arg1 != IP_FW_TABLEARG) 2146 ((ipfw_insn_nat *)cmd)->nat = t; 2147 } 2148 retval = ipfw_nat_ptr(args, t, m); 2149 } 2150 l = 0; /* exit inner loop */ 2151 done = 1; /* exit outer loop */ 2152 break; 2153 2154 case O_REASS: { 2155 int ip_off; 2156 2157 f->pcnt++; 2158 f->bcnt += pktlen; 2159 l = 0; /* in any case exit inner loop */ 2160 2161 ip_off = (args->eh != NULL) ? 2162 ntohs(ip->ip_off) : ip->ip_off; 2163 /* if not fragmented, go to next rule */ 2164 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0) 2165 break; 2166 /* 2167 * ip_reass() expects len & off in host 2168 * byte order: fix them in case we come 2169 * from layer2. 2170 */ 2171 if (args->eh != NULL) { 2172 ip->ip_len = ntohs(ip->ip_len); 2173 ip->ip_off = ntohs(ip->ip_off); 2174 } 2175 2176 args->m = m = ip_reass(m); 2177 2178 /* 2179 * IP header checksum fixup after 2180 * reassembly and leave header 2181 * in network byte order. 2182 */ 2183 if (m == NULL) { /* fragment got swallowed */ 2184 retval = IP_FW_DENY; 2185 } else { /* good, packet complete */ 2186 int hlen; 2187 2188 ip = mtod(m, struct ip *); 2189 hlen = ip->ip_hl << 2; 2190 /* revert len & off for layer2 pkts */ 2191 if (args->eh != NULL) 2192 ip->ip_len = htons(ip->ip_len); 2193 ip->ip_sum = 0; 2194 if (hlen == sizeof(struct ip)) 2195 ip->ip_sum = in_cksum_hdr(ip); 2196 else 2197 ip->ip_sum = in_cksum(m, hlen); 2198 retval = IP_FW_REASS; 2199 args->rule = f; 2200 args->rule_id = f->id; 2201 args->chain_id = chain->id; 2202 } 2203 done = 1; /* exit outer loop */ 2204 break; 2205 } 2206 2207 default: 2208 panic("-- unknown opcode %d\n", cmd->opcode); 2209 } /* end of switch() on opcodes */ 2210 /* 2211 * if we get here with l=0, then match is irrelevant. 2212 */ 2213 2214 if (cmd->len & F_NOT) 2215 match = !match; 2216 2217 if (match) { 2218 if (cmd->len & F_OR) 2219 skip_or = 1; 2220 } else { 2221 if (!(cmd->len & F_OR)) /* not an OR block, */ 2222 break; /* try next rule */ 2223 } 2224 2225 } /* end of inner loop, scan opcodes */ 2226 2227 if (done) 2228 break; 2229 2230/* next_rule:; */ /* try next rule */ 2231 2232 } /* end of outer for, scan rules */ 2233 2234 if (done) { 2235 /* Update statistics */ 2236 f->pcnt++; 2237 f->bcnt += pktlen; 2238 f->timestamp = time_uptime; 2239 } else { 2240 retval = IP_FW_DENY; 2241 printf("ipfw: ouch!, skip past end of rules, denying packet\n"); 2242 } 2243 IPFW_RUNLOCK(chain); 2244 if (ucred_cache != NULL) 2245 crfree(ucred_cache); 2246 return (retval); 2247 2248pullup_failed: 2249 if (V_fw_verbose) 2250 printf("ipfw: pullup failed\n"); 2251 return (IP_FW_DENY); 2252} 2253 2254/**************** 2255 * Stuff that must be initialised only on boot or module load 2256 */ 2257static int 2258ipfw_init(void) 2259{ 2260 int error = 0; 2261 2262 ipfw_dyn_attach(); 2263 /* 2264 * Only print out this stuff the first time around, 2265 * when called from the sysinit code. 2266 */ 2267 printf("ipfw2 " 2268#ifdef INET6 2269 "(+ipv6) " 2270#endif 2271 "initialized, divert %s, nat %s, " 2272 "rule-based forwarding " 2273#ifdef IPFIREWALL_FORWARD 2274 "enabled, " 2275#else 2276 "disabled, " 2277#endif 2278 "default to %s, logging ", 2279#ifdef IPDIVERT 2280 "enabled", 2281#else 2282 "loadable", 2283#endif 2284#ifdef IPFIREWALL_NAT 2285 "enabled", 2286#else 2287 "loadable", 2288#endif 2289 default_to_accept ? "accept" : "deny"); 2290 2291 /* 2292 * Note: V_xxx variables can be accessed here but the vnet specific 2293 * initializer may not have been called yet for the VIMAGE case. 2294 * Tuneables will have been processed. We will print out values for 2295 * the default vnet. 2296 * XXX This should all be rationalized AFTER 8.0 2297 */ 2298 if (V_fw_verbose == 0) 2299 printf("disabled\n"); 2300 else if (V_verbose_limit == 0) 2301 printf("unlimited\n"); 2302 else 2303 printf("limited to %d packets/entry by default\n", 2304 V_verbose_limit); 2305 2306 return (error); 2307} 2308 2309/********************** 2310 * Called for the removal of the last instance only on module unload. 2311 */ 2312static void 2313ipfw_destroy(void) 2314{ 2315 2316 ipfw_dyn_detach(); 2317 printf("IP firewall unloaded\n"); 2318} 2319 2320/**************** 2321 * Stuff that must be initialized for every instance 2322 * (including the first of course). 2323 */ 2324static int 2325vnet_ipfw_init(const void *unused) 2326{ 2327 int error; 2328 struct ip_fw default_rule; 2329 2330 /* First set up some values that are compile time options */ 2331#ifdef IPFIREWALL_VERBOSE 2332 V_fw_verbose = 1; 2333#endif 2334#ifdef IPFIREWALL_VERBOSE_LIMIT 2335 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT; 2336#endif 2337 2338 error = ipfw_init_tables(&V_layer3_chain); 2339 if (error) { 2340 panic("init_tables"); /* XXX Marko fix this ! */ 2341 } 2342#ifdef IPFIREWALL_NAT 2343 LIST_INIT(&V_layer3_chain.nat); 2344#endif 2345 2346 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ 2347 2348 2349 V_fw_deny_unknown_exthdrs = 1; 2350 2351 V_layer3_chain.rules = NULL; 2352 IPFW_LOCK_INIT(&V_layer3_chain); 2353 2354 bzero(&default_rule, sizeof default_rule); 2355 default_rule.act_ofs = 0; 2356 default_rule.rulenum = IPFW_DEFAULT_RULE; 2357 default_rule.cmd_len = 1; 2358 default_rule.set = RESVD_SET; 2359 default_rule.cmd[0].len = 1; 2360 default_rule.cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY; 2361 error = ipfw_add_rule(&V_layer3_chain, &default_rule); 2362 2363 if (error != 0) { 2364 printf("ipfw2: error %u initializing default rule " 2365 "(support disabled)\n", error); 2366 IPFW_LOCK_DESTROY(&V_layer3_chain); 2367 printf("leaving ipfw_iattach (1) with error %d\n", error); 2368 return (error); 2369 } 2370 2371 ip_fw_default_rule = V_layer3_chain.rules; 2372 2373 ipfw_dyn_init(); 2374 2375 /* First set up some values that are compile time options */ 2376 V_ipfw_vnet_ready = 1; /* Open for business */ 2377 2378 /* 2379 * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr) 2380 * and pfil hooks for ipv4 and ipv6. Even if the latter two fail 2381 * we still keep the module alive because the sockopt and 2382 * layer2 paths are still useful. 2383 * ipfw[6]_hook return 0 on success, ENOENT on failure, 2384 * so we can ignore the exact return value and just set a flag. 2385 * 2386 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so 2387 * changes in the underlying (per-vnet) variables trigger 2388 * immediate hook()/unhook() calls. 2389 * In layer2 we have the same behaviour, except that V_ether_ipfw 2390 * is checked on each packet because there are no pfil hooks. 2391 */ 2392 V_ip_fw_ctl_ptr = ipfw_ctl; 2393 V_ip_fw_chk_ptr = ipfw_chk; 2394 if (V_fw_enable && ipfw_hook() != 0) { 2395 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */ 2396 printf("ipfw_hook() error\n"); 2397 } 2398#ifdef INET6 2399 if (V_fw6_enable && ipfw6_hook() != 0) { 2400 error = ENOENT; 2401 printf("ipfw6_hook() error\n"); 2402 } 2403#endif 2404 return (error); 2405} 2406 2407/*********************** 2408 * Called for the removal of each instance. 2409 */ 2410static int 2411vnet_ipfw_uninit(const void *unused) 2412{ 2413 struct ip_fw *reap; 2414 2415 V_ipfw_vnet_ready = 0; /* tell new callers to go away */ 2416 /* 2417 * disconnect from ipv4, ipv6, layer2 and sockopt. 2418 * Then grab, release and grab again the WLOCK so we make 2419 * sure the update is propagated and nobody will be in. 2420 */ 2421 ipfw_unhook(); 2422#ifdef INET6 2423 ipfw6_unhook(); 2424#endif 2425 V_ip_fw_chk_ptr = NULL; 2426 V_ip_fw_ctl_ptr = NULL; 2427 2428 IPFW_WLOCK(&V_layer3_chain); 2429 /* We wait on the wlock here until the last user leaves */ 2430 IPFW_WUNLOCK(&V_layer3_chain); 2431 IPFW_WLOCK(&V_layer3_chain); 2432 2433 ipfw_dyn_uninit(0); /* run the callout_drain */ 2434 ipfw_flush_tables(&V_layer3_chain); 2435 V_layer3_chain.reap = NULL; 2436 ipfw_free_chain(&V_layer3_chain, 1 /* kill default rule */); 2437 reap = V_layer3_chain.reap; 2438 V_layer3_chain.reap = NULL; 2439 IPFW_WUNLOCK(&V_layer3_chain); 2440 if (reap != NULL) 2441 ipfw_reap_rules(reap); 2442 IPFW_LOCK_DESTROY(&V_layer3_chain); 2443 ipfw_dyn_uninit(1); /* free the remaining parts */ 2444 return 0; 2445} 2446 2447/* 2448 * Module event handler. 2449 * In general we have the choice of handling most of these events by the 2450 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to 2451 * use the SYSINIT handlers as they are more capable of expressing the 2452 * flow of control during module and vnet operations, so this is just 2453 * a skeleton. Note there is no SYSINIT equivalent of the module 2454 * SHUTDOWN handler, but we don't have anything to do in that case anyhow. 2455 */ 2456static int 2457ipfw_modevent(module_t mod, int type, void *unused) 2458{ 2459 int err = 0; 2460 2461 switch (type) { 2462 case MOD_LOAD: 2463 /* Called once at module load or 2464 * system boot if compiled in. */ 2465 break; 2466 case MOD_QUIESCE: 2467 /* Called before unload. May veto unloading. */ 2468 break; 2469 case MOD_UNLOAD: 2470 /* Called during unload. */ 2471 break; 2472 case MOD_SHUTDOWN: 2473 /* Called during system shutdown. */ 2474 break; 2475 default: 2476 err = EOPNOTSUPP; 2477 break; 2478 } 2479 return err; 2480} 2481 2482static moduledata_t ipfwmod = { 2483 "ipfw", 2484 ipfw_modevent, 2485 0 2486}; 2487 2488/* Define startup order. */ 2489#define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_IFATTACHDOMAIN 2490#define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */ 2491#define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */ 2492#define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */ 2493 2494DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER); 2495MODULE_VERSION(ipfw, 2); 2496/* should declare some dependencies here */ 2497 2498/* 2499 * Starting up. Done in order after ipfwmod() has been called. 2500 * VNET_SYSINIT is also called for each existing vnet and each new vnet. 2501 */ 2502SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2503 ipfw_init, NULL); 2504VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2505 vnet_ipfw_init, NULL); 2506 2507/* 2508 * Closing up shop. These are done in REVERSE ORDER, but still 2509 * after ipfwmod() has been called. Not called on reboot. 2510 * VNET_SYSUNINIT is also called for each exiting vnet as it exits. 2511 * or when the module is unloaded. 2512 */ 2513SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, 2514 ipfw_destroy, NULL); 2515VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, 2516 vnet_ipfw_uninit, NULL); 2517 2518