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