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