1244769Sglebius/*- 2126258Smlaier * Copyright (c) 2001 Daniel Hartmeier 3223637Sbz * Copyright (c) 2002 - 2008 Henning Brauer 4244769Sglebius * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 5126258Smlaier * All rights reserved. 6126258Smlaier * 7126258Smlaier * Redistribution and use in source and binary forms, with or without 8126258Smlaier * modification, are permitted provided that the following conditions 9126258Smlaier * are met: 10126258Smlaier * 11126258Smlaier * - Redistributions of source code must retain the above copyright 12126258Smlaier * notice, this list of conditions and the following disclaimer. 13126258Smlaier * - Redistributions in binary form must reproduce the above 14126258Smlaier * copyright notice, this list of conditions and the following 15126258Smlaier * disclaimer in the documentation and/or other materials provided 16126258Smlaier * with the distribution. 17126258Smlaier * 18126258Smlaier * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19126258Smlaier * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20126258Smlaier * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21126258Smlaier * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22126258Smlaier * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23126258Smlaier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24126258Smlaier * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25126258Smlaier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26126258Smlaier * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27126258Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28126258Smlaier * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29126258Smlaier * POSSIBILITY OF SUCH DAMAGE. 30126258Smlaier * 31126258Smlaier * Effort sponsored in part by the Defense Advanced Research Projects 32126258Smlaier * Agency (DARPA) and Air Force Research Laboratory, Air Force 33126258Smlaier * Materiel Command, USAF, under agreement number F30602-01-2-0537. 34126258Smlaier * 35244769Sglebius * $OpenBSD: pf.c,v 1.634 2009/02/27 12:37:45 henning Exp $ 36126258Smlaier */ 37126258Smlaier 38240233Sglebius#include <sys/cdefs.h> 39171168Smlaier__FBSDID("$FreeBSD$"); 40126261Smlaier 41240233Sglebius#include "opt_inet.h" 42240233Sglebius#include "opt_inet6.h" 43126261Smlaier#include "opt_bpf.h" 44126261Smlaier#include "opt_pf.h" 45153110Sru 46126258Smlaier#include <sys/param.h> 47240233Sglebius#include <sys/bus.h> 48240233Sglebius#include <sys/endian.h> 49240233Sglebius#include <sys/hash.h> 50240233Sglebius#include <sys/interrupt.h> 51240233Sglebius#include <sys/kernel.h> 52240233Sglebius#include <sys/kthread.h> 53240233Sglebius#include <sys/limits.h> 54126258Smlaier#include <sys/mbuf.h> 55240233Sglebius#include <sys/md5.h> 56240233Sglebius#include <sys/random.h> 57240233Sglebius#include <sys/refcount.h> 58126258Smlaier#include <sys/socket.h> 59126261Smlaier#include <sys/sysctl.h> 60240233Sglebius#include <sys/taskqueue.h> 61240233Sglebius#include <sys/ucred.h> 62126258Smlaier 63126258Smlaier#include <net/if.h> 64126258Smlaier#include <net/if_types.h> 65126258Smlaier#include <net/route.h> 66171168Smlaier#include <net/radix_mpath.h> 67240233Sglebius#include <net/vnet.h> 68126258Smlaier 69240233Sglebius#include <net/pfvar.h> 70240233Sglebius#include <net/if_pflog.h> 71240233Sglebius#include <net/if_pfsync.h> 72240233Sglebius 73240233Sglebius#include <netinet/in_pcb.h> 74126258Smlaier#include <netinet/in_var.h> 75126258Smlaier#include <netinet/ip.h> 76240233Sglebius#include <netinet/ip_fw.h> 77240233Sglebius#include <netinet/ip_icmp.h> 78240233Sglebius#include <netinet/icmp_var.h> 79126258Smlaier#include <netinet/ip_var.h> 80126258Smlaier#include <netinet/tcp.h> 81240233Sglebius#include <netinet/tcp_fsm.h> 82126258Smlaier#include <netinet/tcp_seq.h> 83126258Smlaier#include <netinet/tcp_timer.h> 84126258Smlaier#include <netinet/tcp_var.h> 85240233Sglebius#include <netinet/udp.h> 86126258Smlaier#include <netinet/udp_var.h> 87126258Smlaier 88240494Sglebius#include <netpfil/ipfw/ip_fw_private.h> /* XXX: only for DIR_IN/DIR_OUT */ 89240494Sglebius 90126258Smlaier#ifdef INET6 91126258Smlaier#include <netinet/ip6.h> 92126258Smlaier#include <netinet/icmp6.h> 93126258Smlaier#include <netinet6/nd6.h> 94126261Smlaier#include <netinet6/ip6_var.h> 95126261Smlaier#include <netinet6/in6_pcb.h> 96126258Smlaier#endif /* INET6 */ 97126258Smlaier 98126261Smlaier#include <machine/in_cksum.h> 99163606Srwatson#include <security/mac/mac_framework.h> 100126258Smlaier 101223637Sbz#define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 102126258Smlaier 103126258Smlaier/* 104126258Smlaier * Global variables 105126258Smlaier */ 106126258Smlaier 107223637Sbz/* state tables */ 108223637SbzVNET_DEFINE(struct pf_altqqueue, pf_altqs[2]); 109223637SbzVNET_DEFINE(struct pf_palist, pf_pabuf); 110223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_active); 111223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_inactive); 112270574SglebiusVNET_DEFINE(struct pf_kstatus, pf_status); 113223637Sbz 114223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_active); 115223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_inactive); 116223637SbzVNET_DEFINE(int, altqs_inactive_open); 117223637SbzVNET_DEFINE(u_int32_t, ticket_pabuf); 118223637Sbz 119223637SbzVNET_DEFINE(MD5_CTX, pf_tcp_secret_ctx); 120223637Sbz#define V_pf_tcp_secret_ctx VNET(pf_tcp_secret_ctx) 121223637SbzVNET_DEFINE(u_char, pf_tcp_secret[16]); 122223637Sbz#define V_pf_tcp_secret VNET(pf_tcp_secret) 123223637SbzVNET_DEFINE(int, pf_tcp_secret_init); 124223637Sbz#define V_pf_tcp_secret_init VNET(pf_tcp_secret_init) 125223637SbzVNET_DEFINE(int, pf_tcp_iss_off); 126223637Sbz#define V_pf_tcp_iss_off VNET(pf_tcp_iss_off) 127223637Sbz 128240233Sglebius/* 129240233Sglebius * Queue for pf_intr() sends. 130240233Sglebius */ 131240233Sglebiusstatic MALLOC_DEFINE(M_PFTEMP, "pf_temp", "pf(4) temporary allocations"); 132240233Sglebiusstruct pf_send_entry { 133240233Sglebius STAILQ_ENTRY(pf_send_entry) pfse_next; 134240233Sglebius struct mbuf *pfse_m; 135240233Sglebius enum { 136240233Sglebius PFSE_IP, 137240233Sglebius PFSE_IP6, 138240233Sglebius PFSE_ICMP, 139240233Sglebius PFSE_ICMP6, 140240233Sglebius } pfse_type; 141240233Sglebius union { 142240233Sglebius struct route ro; 143240233Sglebius struct { 144240233Sglebius int type; 145240233Sglebius int code; 146240233Sglebius int mtu; 147240233Sglebius } icmpopts; 148240233Sglebius } u; 149240233Sglebius#define pfse_ro u.ro 150240233Sglebius#define pfse_icmp_type u.icmpopts.type 151240233Sglebius#define pfse_icmp_code u.icmpopts.code 152240233Sglebius#define pfse_icmp_mtu u.icmpopts.mtu 153240233Sglebius}; 154223637Sbz 155240233SglebiusSTAILQ_HEAD(pf_send_head, pf_send_entry); 156240233Sglebiusstatic VNET_DEFINE(struct pf_send_head, pf_sendqueue); 157240233Sglebius#define V_pf_sendqueue VNET(pf_sendqueue) 158126258Smlaier 159240233Sglebiusstatic struct mtx pf_sendqueue_mtx; 160240233Sglebius#define PF_SENDQ_LOCK() mtx_lock(&pf_sendqueue_mtx) 161240233Sglebius#define PF_SENDQ_UNLOCK() mtx_unlock(&pf_sendqueue_mtx) 162126258Smlaier 163240233Sglebius/* 164240811Sglebius * Queue for pf_overload_task() tasks. 165240233Sglebius */ 166240811Sglebiusstruct pf_overload_entry { 167240811Sglebius SLIST_ENTRY(pf_overload_entry) next; 168240233Sglebius struct pf_addr addr; 169240233Sglebius sa_family_t af; 170240233Sglebius uint8_t dir; 171240811Sglebius struct pf_rule *rule; 172240233Sglebius}; 173223637Sbz 174240811SglebiusSLIST_HEAD(pf_overload_head, pf_overload_entry); 175240811Sglebiusstatic VNET_DEFINE(struct pf_overload_head, pf_overloadqueue); 176240811Sglebius#define V_pf_overloadqueue VNET(pf_overloadqueue) 177240811Sglebiusstatic VNET_DEFINE(struct task, pf_overloadtask); 178240811Sglebius#define V_pf_overloadtask VNET(pf_overloadtask) 179126261Smlaier 180240811Sglebiusstatic struct mtx pf_overloadqueue_mtx; 181240811Sglebius#define PF_OVERLOADQ_LOCK() mtx_lock(&pf_overloadqueue_mtx) 182240811Sglebius#define PF_OVERLOADQ_UNLOCK() mtx_unlock(&pf_overloadqueue_mtx) 183126258Smlaier 184240233SglebiusVNET_DEFINE(struct pf_rulequeue, pf_unlinked_rules); 185240233Sglebiusstruct mtx pf_unlnkdrules_mtx; 186240233Sglebius 187240233Sglebiusstatic VNET_DEFINE(uma_zone_t, pf_sources_z); 188240233Sglebius#define V_pf_sources_z VNET(pf_sources_z) 189265008Smmuma_zone_t pf_mtag_z; 190240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_z); 191240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_key_z); 192240233Sglebius 193240233SglebiusVNET_DEFINE(uint64_t, pf_stateid[MAXCPU]); 194240233Sglebius#define PFID_CPUBITS 8 195240233Sglebius#define PFID_CPUSHIFT (sizeof(uint64_t) * NBBY - PFID_CPUBITS) 196240233Sglebius#define PFID_CPUMASK ((uint64_t)((1 << PFID_CPUBITS) - 1) << PFID_CPUSHIFT) 197240233Sglebius#define PFID_MAXID (~PFID_CPUMASK) 198240233SglebiusCTASSERT((1 << PFID_CPUBITS) > MAXCPU); 199240233Sglebius 200240233Sglebiusstatic void pf_src_tree_remove_state(struct pf_state *); 201240233Sglebiusstatic void pf_init_threshold(struct pf_threshold *, u_int32_t, 202145836Smlaier u_int32_t); 203240233Sglebiusstatic void pf_add_threshold(struct pf_threshold *); 204240233Sglebiusstatic int pf_check_threshold(struct pf_threshold *); 205145836Smlaier 206293894Sglebiusstatic void pf_change_ap(struct mbuf *, struct pf_addr *, u_int16_t *, 207126258Smlaier u_int16_t *, u_int16_t *, struct pf_addr *, 208126258Smlaier u_int16_t, u_int8_t, sa_family_t); 209240233Sglebiusstatic int pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *, 210171168Smlaier struct tcphdr *, struct pf_state_peer *); 211240233Sglebiusstatic void pf_change_icmp(struct pf_addr *, u_int16_t *, 212126258Smlaier struct pf_addr *, struct pf_addr *, u_int16_t, 213126258Smlaier u_int16_t *, u_int16_t *, u_int16_t *, 214126258Smlaier u_int16_t *, u_int8_t, sa_family_t); 215240233Sglebiusstatic void pf_send_tcp(struct mbuf *, 216162238Scsjp const struct pf_rule *, sa_family_t, 217126258Smlaier const struct pf_addr *, const struct pf_addr *, 218126258Smlaier u_int16_t, u_int16_t, u_int32_t, u_int32_t, 219145836Smlaier u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 220240233Sglebius u_int16_t, struct ifnet *); 221223637Sbzstatic void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t, 222126258Smlaier sa_family_t, struct pf_rule *); 223240233Sglebiusstatic void pf_detach_state(struct pf_state *); 224240233Sglebiusstatic int pf_state_key_attach(struct pf_state_key *, 225240233Sglebius struct pf_state_key *, struct pf_state *); 226240233Sglebiusstatic void pf_state_key_detach(struct pf_state *, int); 227240233Sglebiusstatic int pf_state_key_ctor(void *, int, void *, int); 228240233Sglebiusstatic u_int32_t pf_tcp_iss(struct pf_pdesc *); 229240233Sglebiusstatic int pf_test_rule(struct pf_rule **, struct pf_state **, 230130613Smlaier int, struct pfi_kif *, struct mbuf *, int, 231240233Sglebius struct pf_pdesc *, struct pf_rule **, 232240233Sglebius struct pf_ruleset **, struct inpcb *); 233240233Sglebiusstatic int pf_create_state(struct pf_rule *, struct pf_rule *, 234223637Sbz struct pf_rule *, struct pf_pdesc *, 235223637Sbz struct pf_src_node *, struct pf_state_key *, 236223637Sbz struct pf_state_key *, struct mbuf *, int, 237223637Sbz u_int16_t, u_int16_t, int *, struct pfi_kif *, 238223637Sbz struct pf_state **, int, u_int16_t, u_int16_t, 239223637Sbz int); 240240233Sglebiusstatic int pf_test_fragment(struct pf_rule **, int, 241130613Smlaier struct pfi_kif *, struct mbuf *, void *, 242126258Smlaier struct pf_pdesc *, struct pf_rule **, 243126258Smlaier struct pf_ruleset **); 244240233Sglebiusstatic int pf_tcp_track_full(struct pf_state_peer *, 245200930Sdelphij struct pf_state_peer *, struct pf_state **, 246200930Sdelphij struct pfi_kif *, struct mbuf *, int, 247200930Sdelphij struct pf_pdesc *, u_short *, int *); 248240233Sglebiusstatic int pf_tcp_track_sloppy(struct pf_state_peer *, 249200930Sdelphij struct pf_state_peer *, struct pf_state **, 250200930Sdelphij struct pf_pdesc *, u_short *); 251240233Sglebiusstatic int pf_test_state_tcp(struct pf_state **, int, 252130613Smlaier struct pfi_kif *, struct mbuf *, int, 253126258Smlaier void *, struct pf_pdesc *, u_short *); 254240233Sglebiusstatic int pf_test_state_udp(struct pf_state **, int, 255130613Smlaier struct pfi_kif *, struct mbuf *, int, 256126258Smlaier void *, struct pf_pdesc *); 257240233Sglebiusstatic int pf_test_state_icmp(struct pf_state **, int, 258130613Smlaier struct pfi_kif *, struct mbuf *, int, 259145836Smlaier void *, struct pf_pdesc *, u_short *); 260240233Sglebiusstatic int pf_test_state_other(struct pf_state **, int, 261223637Sbz struct pfi_kif *, struct mbuf *, struct pf_pdesc *); 262240233Sglebiusstatic u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t, 263126258Smlaier sa_family_t); 264240233Sglebiusstatic u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t, 265126258Smlaier sa_family_t); 266240233Sglebiusstatic u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t, 267231852Sbz int, u_int16_t); 268240233Sglebiusstatic int pf_check_proto_cksum(struct mbuf *, int, int, 269126258Smlaier u_int8_t, sa_family_t); 270240233Sglebiusstatic void pf_print_state_parts(struct pf_state *, 271223637Sbz struct pf_state_key *, struct pf_state_key *); 272240233Sglebiusstatic int pf_addr_wrap_neq(struct pf_addr_wrap *, 273126258Smlaier struct pf_addr_wrap *); 274240233Sglebiusstatic struct pf_state *pf_find_state(struct pfi_kif *, 275240233Sglebius struct pf_state_key_cmp *, u_int); 276240233Sglebiusstatic int pf_src_connlimit(struct pf_state **); 277264454Smmstatic void pf_overload_task(void *v, int pending); 278240233Sglebiusstatic int pf_insert_src_node(struct pf_src_node **, 279240233Sglebius struct pf_rule *, struct pf_addr *, sa_family_t); 280241039Sglebiusstatic u_int pf_purge_expired_states(u_int, int); 281240233Sglebiusstatic void pf_purge_unlinked_rules(void); 282265008Smmstatic int pf_mtag_uminit(void *, int, int); 283240233Sglebiusstatic void pf_mtag_free(struct m_tag *); 284240233Sglebius#ifdef INET 285240233Sglebiusstatic void pf_route(struct mbuf **, struct pf_rule *, int, 286240233Sglebius struct ifnet *, struct pf_state *, 287240233Sglebius struct pf_pdesc *); 288240233Sglebius#endif /* INET */ 289240233Sglebius#ifdef INET6 290240233Sglebiusstatic void pf_change_a6(struct pf_addr *, u_int16_t *, 291240233Sglebius struct pf_addr *, u_int8_t); 292240233Sglebiusstatic void pf_route6(struct mbuf **, struct pf_rule *, int, 293240233Sglebius struct ifnet *, struct pf_state *, 294240233Sglebius struct pf_pdesc *); 295240233Sglebius#endif /* INET6 */ 296126258Smlaier 297126261Smlaierint in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len); 298126258Smlaier 299223637SbzVNET_DECLARE(int, pf_end_threads); 300171168Smlaier 301240233SglebiusVNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 302145836Smlaier 303240233Sglebius#define PACKET_LOOPED(pd) ((pd)->pf_mtag && \ 304240233Sglebius (pd)->pf_mtag->flags & PF_PACKET_LOOPED) 305126258Smlaier 306240233Sglebius#define STATE_LOOKUP(i, k, d, s, pd) \ 307126258Smlaier do { \ 308240233Sglebius (s) = pf_find_state((i), (k), (d)); \ 309261018Sglebius if ((s) == NULL) \ 310126258Smlaier return (PF_DROP); \ 311240233Sglebius if (PACKET_LOOPED(pd)) \ 312126258Smlaier return (PF_PASS); \ 313240233Sglebius if ((d) == PF_OUT && \ 314223637Sbz (((s)->rule.ptr->rt == PF_ROUTETO && \ 315223637Sbz (s)->rule.ptr->direction == PF_OUT) || \ 316223637Sbz ((s)->rule.ptr->rt == PF_REPLYTO && \ 317223637Sbz (s)->rule.ptr->direction == PF_IN)) && \ 318223637Sbz (s)->rt_kif != NULL && \ 319240233Sglebius (s)->rt_kif != (i)) \ 320223637Sbz return (PF_PASS); \ 321126258Smlaier } while (0) 322126258Smlaier 323223637Sbz#define BOUND_IFACE(r, k) \ 324223637Sbz ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : V_pfi_all 325126258Smlaier 326263029Sglebius#define STATE_INC_COUNTERS(s) \ 327263029Sglebius do { \ 328263029Sglebius counter_u64_add(s->rule.ptr->states_cur, 1); \ 329263029Sglebius counter_u64_add(s->rule.ptr->states_tot, 1); \ 330263029Sglebius if (s->anchor.ptr != NULL) { \ 331263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, 1); \ 332263029Sglebius counter_u64_add(s->anchor.ptr->states_tot, 1); \ 333263029Sglebius } \ 334263029Sglebius if (s->nat_rule.ptr != NULL) { \ 335263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, 1);\ 336263029Sglebius counter_u64_add(s->nat_rule.ptr->states_tot, 1);\ 337263029Sglebius } \ 338145836Smlaier } while (0) 339145836Smlaier 340263029Sglebius#define STATE_DEC_COUNTERS(s) \ 341263029Sglebius do { \ 342263029Sglebius if (s->nat_rule.ptr != NULL) \ 343263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, -1);\ 344263029Sglebius if (s->anchor.ptr != NULL) \ 345263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, -1); \ 346263029Sglebius counter_u64_add(s->rule.ptr->states_cur, -1); \ 347145836Smlaier } while (0) 348145836Smlaier 349240233Sglebiusstatic MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures"); 350240233SglebiusVNET_DEFINE(struct pf_keyhash *, pf_keyhash); 351240233SglebiusVNET_DEFINE(struct pf_idhash *, pf_idhash); 352240233SglebiusVNET_DEFINE(u_long, pf_hashmask); 353240233SglebiusVNET_DEFINE(struct pf_srchash *, pf_srchash); 354240233SglebiusVNET_DEFINE(u_long, pf_srchashmask); 355223637Sbz 356240233SglebiusSYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW, 0, "pf(4)"); 357223637Sbz 358240233SglebiusVNET_DEFINE(u_long, pf_hashsize); 359240233Sglebius#define V_pf_hashsize VNET(pf_hashsize) 360240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_RDTUN, 361240233Sglebius &VNET_NAME(pf_hashsize), 0, "Size of pf(4) states hashtable"); 362130613Smlaier 363240233SglebiusVNET_DEFINE(u_long, pf_srchashsize); 364240233Sglebius#define V_pf_srchashsize VNET(pf_srchashsize) 365240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_RDTUN, 366240233Sglebius &VNET_NAME(pf_srchashsize), 0, "Size of pf(4) source nodes hashtable"); 367171168Smlaier 368240233SglebiusVNET_DEFINE(void *, pf_swi_cookie); 369130613Smlaier 370240233SglebiusVNET_DEFINE(uint32_t, pf_hashseed); 371240233Sglebius#define V_pf_hashseed VNET(pf_hashseed) 372240233Sglebius 373240233Sglebiusstatic __inline uint32_t 374240233Sglebiuspf_hashkey(struct pf_state_key *sk) 375126258Smlaier{ 376240233Sglebius uint32_t h; 377126258Smlaier 378240233Sglebius h = jenkins_hash32((uint32_t *)sk, 379240233Sglebius sizeof(struct pf_state_key_cmp)/sizeof(uint32_t), 380240233Sglebius V_pf_hashseed); 381240233Sglebius 382240233Sglebius return (h & V_pf_hashmask); 383130613Smlaier} 384130613Smlaier 385240736Sglebiusstatic __inline uint32_t 386240736Sglebiuspf_hashsrc(struct pf_addr *addr, sa_family_t af) 387240736Sglebius{ 388240736Sglebius uint32_t h; 389240736Sglebius 390240736Sglebius switch (af) { 391240736Sglebius case AF_INET: 392240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v4, 393240736Sglebius sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed); 394240736Sglebius break; 395240736Sglebius case AF_INET6: 396240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v6, 397240736Sglebius sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed); 398240736Sglebius break; 399240736Sglebius default: 400240736Sglebius panic("%s: unknown address family %u", __func__, af); 401240736Sglebius } 402240736Sglebius 403240736Sglebius return (h & V_pf_srchashmask); 404240736Sglebius} 405240736Sglebius 406126258Smlaier#ifdef INET6 407126258Smlaiervoid 408126258Smlaierpf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af) 409126258Smlaier{ 410126258Smlaier switch (af) { 411126258Smlaier#ifdef INET 412126258Smlaier case AF_INET: 413126258Smlaier dst->addr32[0] = src->addr32[0]; 414126258Smlaier break; 415126258Smlaier#endif /* INET */ 416126258Smlaier case AF_INET6: 417126258Smlaier dst->addr32[0] = src->addr32[0]; 418126258Smlaier dst->addr32[1] = src->addr32[1]; 419126258Smlaier dst->addr32[2] = src->addr32[2]; 420126258Smlaier dst->addr32[3] = src->addr32[3]; 421126258Smlaier break; 422126258Smlaier } 423126258Smlaier} 424145836Smlaier#endif /* INET6 */ 425126258Smlaier 426240233Sglebiusstatic void 427145836Smlaierpf_init_threshold(struct pf_threshold *threshold, 428145836Smlaier u_int32_t limit, u_int32_t seconds) 429145836Smlaier{ 430145836Smlaier threshold->limit = limit * PF_THRESHOLD_MULT; 431145836Smlaier threshold->seconds = seconds; 432145836Smlaier threshold->count = 0; 433240233Sglebius threshold->last = time_uptime; 434145836Smlaier} 435145836Smlaier 436240233Sglebiusstatic void 437145836Smlaierpf_add_threshold(struct pf_threshold *threshold) 438145836Smlaier{ 439240233Sglebius u_int32_t t = time_uptime, diff = t - threshold->last; 440145836Smlaier 441145836Smlaier if (diff >= threshold->seconds) 442145836Smlaier threshold->count = 0; 443145836Smlaier else 444145836Smlaier threshold->count -= threshold->count * diff / 445145836Smlaier threshold->seconds; 446145836Smlaier threshold->count += PF_THRESHOLD_MULT; 447145836Smlaier threshold->last = t; 448145836Smlaier} 449145836Smlaier 450240233Sglebiusstatic int 451145836Smlaierpf_check_threshold(struct pf_threshold *threshold) 452145836Smlaier{ 453145836Smlaier return (threshold->count > threshold->limit); 454145836Smlaier} 455145836Smlaier 456240233Sglebiusstatic int 457145836Smlaierpf_src_connlimit(struct pf_state **state) 458145836Smlaier{ 459240811Sglebius struct pf_overload_entry *pfoe; 460145836Smlaier int bad = 0; 461145836Smlaier 462240233Sglebius PF_STATE_LOCK_ASSERT(*state); 463240233Sglebius 464145836Smlaier (*state)->src_node->conn++; 465171168Smlaier (*state)->src.tcp_est = 1; 466145836Smlaier pf_add_threshold(&(*state)->src_node->conn_rate); 467145836Smlaier 468145836Smlaier if ((*state)->rule.ptr->max_src_conn && 469145836Smlaier (*state)->rule.ptr->max_src_conn < 470145836Smlaier (*state)->src_node->conn) { 471270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONN], 1); 472145836Smlaier bad++; 473145836Smlaier } 474145836Smlaier 475145836Smlaier if ((*state)->rule.ptr->max_src_conn_rate.limit && 476145836Smlaier pf_check_threshold(&(*state)->src_node->conn_rate)) { 477270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONNRATE], 1); 478145836Smlaier bad++; 479145836Smlaier } 480145836Smlaier 481145836Smlaier if (!bad) 482145836Smlaier return (0); 483145836Smlaier 484240233Sglebius /* Kill this state. */ 485240233Sglebius (*state)->timeout = PFTM_PURGE; 486240233Sglebius (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 487145836Smlaier 488240233Sglebius if ((*state)->rule.ptr->overload_tbl == NULL) 489240233Sglebius return (1); 490145836Smlaier 491240811Sglebius /* Schedule overloading and flushing task. */ 492240811Sglebius pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT); 493240811Sglebius if (pfoe == NULL) 494240233Sglebius return (1); /* too bad :( */ 495240233Sglebius 496240811Sglebius bcopy(&(*state)->src_node->addr, &pfoe->addr, sizeof(pfoe->addr)); 497240811Sglebius pfoe->af = (*state)->key[PF_SK_WIRE]->af; 498240811Sglebius pfoe->rule = (*state)->rule.ptr; 499240811Sglebius pfoe->dir = (*state)->direction; 500240811Sglebius PF_OVERLOADQ_LOCK(); 501240811Sglebius SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next); 502240811Sglebius PF_OVERLOADQ_UNLOCK(); 503240811Sglebius taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask); 504240233Sglebius 505240233Sglebius return (1); 506240233Sglebius} 507240233Sglebius 508240233Sglebiusstatic void 509264454Smmpf_overload_task(void *v, int pending) 510240233Sglebius{ 511240811Sglebius struct pf_overload_head queue; 512240811Sglebius struct pfr_addr p; 513240811Sglebius struct pf_overload_entry *pfoe, *pfoe1; 514240233Sglebius uint32_t killed = 0; 515240233Sglebius 516264454Smm CURVNET_SET((struct vnet *)v); 517264454Smm 518240811Sglebius PF_OVERLOADQ_LOCK(); 519264454Smm queue = V_pf_overloadqueue; 520264454Smm SLIST_INIT(&V_pf_overloadqueue); 521240811Sglebius PF_OVERLOADQ_UNLOCK(); 522240233Sglebius 523240811Sglebius bzero(&p, sizeof(p)); 524240811Sglebius SLIST_FOREACH(pfoe, &queue, next) { 525270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_OVERLOAD_TABLE], 1); 526240811Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) { 527240811Sglebius printf("%s: blocking address ", __func__); 528240811Sglebius pf_print_host(&pfoe->addr, 0, pfoe->af); 529240811Sglebius printf("\n"); 530240811Sglebius } 531240233Sglebius 532240811Sglebius p.pfra_af = pfoe->af; 533240811Sglebius switch (pfoe->af) { 534240811Sglebius#ifdef INET 535240811Sglebius case AF_INET: 536240811Sglebius p.pfra_net = 32; 537240811Sglebius p.pfra_ip4addr = pfoe->addr.v4; 538240811Sglebius break; 539240811Sglebius#endif 540240811Sglebius#ifdef INET6 541240811Sglebius case AF_INET6: 542240811Sglebius p.pfra_net = 128; 543240811Sglebius p.pfra_ip6addr = pfoe->addr.v6; 544240811Sglebius break; 545240811Sglebius#endif 546240811Sglebius } 547240811Sglebius 548240811Sglebius PF_RULES_WLOCK(); 549240811Sglebius pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second); 550240811Sglebius PF_RULES_WUNLOCK(); 551240811Sglebius } 552240811Sglebius 553240811Sglebius /* 554240811Sglebius * Remove those entries, that don't need flushing. 555240811Sglebius */ 556240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 557240811Sglebius if (pfoe->rule->flush == 0) { 558240811Sglebius SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next); 559240811Sglebius free(pfoe, M_PFTEMP); 560240811Sglebius } else 561270574Sglebius counter_u64_add( 562270574Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH], 1); 563240811Sglebius 564240811Sglebius /* If nothing to flush, return. */ 565264454Smm if (SLIST_EMPTY(&queue)) { 566264454Smm CURVNET_RESTORE(); 567240811Sglebius return; 568264454Smm } 569240811Sglebius 570240233Sglebius for (int i = 0; i <= V_pf_hashmask; i++) { 571240233Sglebius struct pf_idhash *ih = &V_pf_idhash[i]; 572240233Sglebius struct pf_state_key *sk; 573240233Sglebius struct pf_state *s; 574240233Sglebius 575240233Sglebius PF_HASHROW_LOCK(ih); 576240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 577240233Sglebius sk = s->key[PF_SK_WIRE]; 578240811Sglebius SLIST_FOREACH(pfoe, &queue, next) 579240811Sglebius if (sk->af == pfoe->af && 580240811Sglebius ((pfoe->rule->flush & PF_FLUSH_GLOBAL) || 581240811Sglebius pfoe->rule == s->rule.ptr) && 582240811Sglebius ((pfoe->dir == PF_OUT && 583240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) || 584240811Sglebius (pfoe->dir == PF_IN && 585240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) { 586240233Sglebius s->timeout = PFTM_PURGE; 587240233Sglebius s->src.state = s->dst.state = TCPS_CLOSED; 588240233Sglebius killed++; 589145836Smlaier } 590145836Smlaier } 591240233Sglebius PF_HASHROW_UNLOCK(ih); 592145836Smlaier } 593240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 594240811Sglebius free(pfoe, M_PFTEMP); 595240233Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) 596240233Sglebius printf("%s: %u states killed", __func__, killed); 597264454Smm 598264454Smm CURVNET_RESTORE(); 599240233Sglebius} 600145836Smlaier 601240233Sglebius/* 602240233Sglebius * Can return locked on failure, so that we can consistently 603240233Sglebius * allocate and insert a new one. 604240233Sglebius */ 605240233Sglebiusstruct pf_src_node * 606240233Sglebiuspf_find_src_node(struct pf_addr *src, struct pf_rule *rule, sa_family_t af, 607240233Sglebius int returnlocked) 608240233Sglebius{ 609240233Sglebius struct pf_srchash *sh; 610240233Sglebius struct pf_src_node *n; 611240233Sglebius 612270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_SEARCH], 1); 613240233Sglebius 614240233Sglebius sh = &V_pf_srchash[pf_hashsrc(src, af)]; 615240233Sglebius PF_HASHROW_LOCK(sh); 616240233Sglebius LIST_FOREACH(n, &sh->nodes, entry) 617240233Sglebius if (n->rule.ptr == rule && n->af == af && 618240233Sglebius ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) || 619240233Sglebius (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0))) 620240233Sglebius break; 621240233Sglebius if (n != NULL || returnlocked == 0) 622240233Sglebius PF_HASHROW_UNLOCK(sh); 623240233Sglebius 624240233Sglebius return (n); 625145836Smlaier} 626145836Smlaier 627240233Sglebiusstatic int 628130613Smlaierpf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule, 629130613Smlaier struct pf_addr *src, sa_family_t af) 630126258Smlaier{ 631126258Smlaier 632240233Sglebius KASSERT((rule->rule_flag & PFRULE_RULESRCTRACK || 633240233Sglebius rule->rpool.opts & PF_POOL_STICKYADDR), 634240233Sglebius ("%s for non-tracking rule %p", __func__, rule)); 635240233Sglebius 636240233Sglebius if (*sn == NULL) 637240233Sglebius *sn = pf_find_src_node(src, rule, af, 1); 638240233Sglebius 639130613Smlaier if (*sn == NULL) { 640240233Sglebius struct pf_srchash *sh = &V_pf_srchash[pf_hashsrc(src, af)]; 641240233Sglebius 642240233Sglebius PF_HASHROW_ASSERT(sh); 643240233Sglebius 644130613Smlaier if (!rule->max_src_nodes || 645263029Sglebius counter_u64_fetch(rule->src_nodes) < rule->max_src_nodes) 646240233Sglebius (*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO); 647145836Smlaier else 648270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCNODES], 649270574Sglebius 1); 650240233Sglebius if ((*sn) == NULL) { 651240233Sglebius PF_HASHROW_UNLOCK(sh); 652130613Smlaier return (-1); 653240233Sglebius } 654145836Smlaier 655145836Smlaier pf_init_threshold(&(*sn)->conn_rate, 656145836Smlaier rule->max_src_conn_rate.limit, 657145836Smlaier rule->max_src_conn_rate.seconds); 658145836Smlaier 659130613Smlaier (*sn)->af = af; 660240233Sglebius (*sn)->rule.ptr = rule; 661130613Smlaier PF_ACPY(&(*sn)->addr, src, af); 662240233Sglebius LIST_INSERT_HEAD(&sh->nodes, *sn, entry); 663240233Sglebius (*sn)->creation = time_uptime; 664130613Smlaier (*sn)->ruletype = rule->action; 665130613Smlaier if ((*sn)->rule.ptr != NULL) 666263029Sglebius counter_u64_add((*sn)->rule.ptr->src_nodes, 1); 667240233Sglebius PF_HASHROW_UNLOCK(sh); 668270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_INSERT], 1); 669130613Smlaier } else { 670130613Smlaier if (rule->max_src_states && 671145836Smlaier (*sn)->states >= rule->max_src_states) { 672270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCSTATES], 673270574Sglebius 1); 674130613Smlaier return (-1); 675145836Smlaier } 676130613Smlaier } 677130613Smlaier return (0); 678130613Smlaier} 679126258Smlaier 680261019Sglebiusvoid 681261019Sglebiuspf_unlink_src_node_locked(struct pf_src_node *src) 682240233Sglebius{ 683261019Sglebius#ifdef INVARIANTS 684240233Sglebius struct pf_srchash *sh; 685223637Sbz 686240233Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 687261019Sglebius PF_HASHROW_ASSERT(sh); 688261019Sglebius#endif 689240233Sglebius LIST_REMOVE(src, entry); 690261019Sglebius if (src->rule.ptr) 691263029Sglebius counter_u64_add(src->rule.ptr->src_nodes, -1); 692270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1); 693261019Sglebius} 694240737Sglebius 695261019Sglebiusvoid 696261019Sglebiuspf_unlink_src_node(struct pf_src_node *src) 697261019Sglebius{ 698261019Sglebius struct pf_srchash *sh; 699261019Sglebius 700261019Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 701261019Sglebius PF_HASHROW_LOCK(sh); 702261019Sglebius pf_unlink_src_node_locked(src); 703261019Sglebius PF_HASHROW_UNLOCK(sh); 704240233Sglebius} 705240233Sglebius 706261019Sglebiusstatic void 707261019Sglebiuspf_free_src_node(struct pf_src_node *sn) 708261019Sglebius{ 709261019Sglebius 710261019Sglebius KASSERT(sn->states == 0, ("%s: %p has refs", __func__, sn)); 711261019Sglebius uma_zfree(V_pf_sources_z, sn); 712261019Sglebius} 713261019Sglebius 714261019Sglebiusu_int 715261019Sglebiuspf_free_src_nodes(struct pf_src_node_list *head) 716261019Sglebius{ 717261019Sglebius struct pf_src_node *sn, *tmp; 718261019Sglebius u_int count = 0; 719261019Sglebius 720261019Sglebius LIST_FOREACH_SAFE(sn, head, entry, tmp) { 721261019Sglebius pf_free_src_node(sn); 722261019Sglebius count++; 723261019Sglebius } 724261019Sglebius 725261019Sglebius return (count); 726261019Sglebius} 727261019Sglebius 728240233Sglebiusvoid 729265008Smmpf_mtag_initialize() 730265008Smm{ 731265008Smm 732265008Smm pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) + 733265008Smm sizeof(struct pf_mtag), NULL, NULL, pf_mtag_uminit, NULL, 734265008Smm UMA_ALIGN_PTR, 0); 735265008Smm} 736265008Smm 737265008Smm/* Per-vnet data storage structures initialization. */ 738265008Smmvoid 739240233Sglebiuspf_initialize() 740223637Sbz{ 741240233Sglebius struct pf_keyhash *kh; 742240233Sglebius struct pf_idhash *ih; 743240233Sglebius struct pf_srchash *sh; 744240233Sglebius u_int i; 745223637Sbz 746240233Sglebius TUNABLE_ULONG_FETCH("net.pf.states_hashsize", &V_pf_hashsize); 747240233Sglebius if (V_pf_hashsize == 0 || !powerof2(V_pf_hashsize)) 748240233Sglebius V_pf_hashsize = PF_HASHSIZ; 749240233Sglebius TUNABLE_ULONG_FETCH("net.pf.source_nodes_hashsize", &V_pf_srchashsize); 750240233Sglebius if (V_pf_srchashsize == 0 || !powerof2(V_pf_srchashsize)) 751240233Sglebius V_pf_srchashsize = PF_HASHSIZ / 4; 752240233Sglebius 753240233Sglebius V_pf_hashseed = arc4random(); 754240233Sglebius 755240233Sglebius /* States and state keys storage. */ 756240233Sglebius V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_state), 757240233Sglebius NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 758240233Sglebius V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z; 759240233Sglebius uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT); 760244347Spjd uma_zone_set_warning(V_pf_state_z, "PF states limit reached"); 761240233Sglebius 762240233Sglebius V_pf_state_key_z = uma_zcreate("pf state keys", 763240233Sglebius sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL, 764240233Sglebius UMA_ALIGN_PTR, 0); 765240233Sglebius V_pf_keyhash = malloc(V_pf_hashsize * sizeof(struct pf_keyhash), 766240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 767240233Sglebius V_pf_idhash = malloc(V_pf_hashsize * sizeof(struct pf_idhash), 768240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 769240233Sglebius V_pf_hashmask = V_pf_hashsize - 1; 770240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 771240233Sglebius i++, kh++, ih++) { 772251681Sglebius mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK); 773240233Sglebius mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF); 774223637Sbz } 775223637Sbz 776240233Sglebius /* Source nodes. */ 777240233Sglebius V_pf_sources_z = uma_zcreate("pf source nodes", 778240233Sglebius sizeof(struct pf_src_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 779240233Sglebius 0); 780240233Sglebius V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z; 781240233Sglebius uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT); 782244347Spjd uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached"); 783240233Sglebius V_pf_srchash = malloc(V_pf_srchashsize * sizeof(struct pf_srchash), 784240233Sglebius M_PFHASH, M_WAITOK|M_ZERO); 785240233Sglebius V_pf_srchashmask = V_pf_srchashsize - 1; 786240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) 787240233Sglebius mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF); 788223637Sbz 789240233Sglebius /* ALTQ */ 790240233Sglebius TAILQ_INIT(&V_pf_altqs[0]); 791240233Sglebius TAILQ_INIT(&V_pf_altqs[1]); 792240233Sglebius TAILQ_INIT(&V_pf_pabuf); 793240233Sglebius V_pf_altqs_active = &V_pf_altqs[0]; 794240233Sglebius V_pf_altqs_inactive = &V_pf_altqs[1]; 795240233Sglebius 796240233Sglebius 797240811Sglebius /* Send & overload+flush queues. */ 798240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 799240811Sglebius SLIST_INIT(&V_pf_overloadqueue); 800264454Smm TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, curvnet); 801240233Sglebius mtx_init(&pf_sendqueue_mtx, "pf send queue", NULL, MTX_DEF); 802240811Sglebius mtx_init(&pf_overloadqueue_mtx, "pf overload/flush queue", NULL, 803240811Sglebius MTX_DEF); 804240233Sglebius 805240233Sglebius /* Unlinked, but may be referenced rules. */ 806240233Sglebius TAILQ_INIT(&V_pf_unlinked_rules); 807240233Sglebius mtx_init(&pf_unlnkdrules_mtx, "pf unlinked rules", NULL, MTX_DEF); 808223637Sbz} 809223637Sbz 810240233Sglebiusvoid 811265008Smmpf_mtag_cleanup() 812265008Smm{ 813265008Smm 814265008Smm uma_zdestroy(pf_mtag_z); 815265008Smm} 816265008Smm 817265008Smmvoid 818240233Sglebiuspf_cleanup() 819223637Sbz{ 820240233Sglebius struct pf_keyhash *kh; 821240233Sglebius struct pf_idhash *ih; 822240233Sglebius struct pf_srchash *sh; 823240233Sglebius struct pf_send_entry *pfse, *next; 824240233Sglebius u_int i; 825223637Sbz 826240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 827240233Sglebius i++, kh++, ih++) { 828240233Sglebius KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty", 829240233Sglebius __func__)); 830240233Sglebius KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty", 831240233Sglebius __func__)); 832240233Sglebius mtx_destroy(&kh->lock); 833240233Sglebius mtx_destroy(&ih->lock); 834240233Sglebius } 835240233Sglebius free(V_pf_keyhash, M_PFHASH); 836240233Sglebius free(V_pf_idhash, M_PFHASH); 837240233Sglebius 838240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 839240233Sglebius KASSERT(LIST_EMPTY(&sh->nodes), 840240233Sglebius ("%s: source node hash not empty", __func__)); 841240233Sglebius mtx_destroy(&sh->lock); 842240233Sglebius } 843240233Sglebius free(V_pf_srchash, M_PFHASH); 844240233Sglebius 845240233Sglebius STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) { 846240233Sglebius m_freem(pfse->pfse_m); 847240233Sglebius free(pfse, M_PFTEMP); 848240233Sglebius } 849240233Sglebius 850240233Sglebius mtx_destroy(&pf_sendqueue_mtx); 851240811Sglebius mtx_destroy(&pf_overloadqueue_mtx); 852240233Sglebius mtx_destroy(&pf_unlnkdrules_mtx); 853240233Sglebius 854240233Sglebius uma_zdestroy(V_pf_sources_z); 855240233Sglebius uma_zdestroy(V_pf_state_z); 856240233Sglebius uma_zdestroy(V_pf_state_key_z); 857240233Sglebius} 858240233Sglebius 859240233Sglebiusstatic int 860265008Smmpf_mtag_uminit(void *mem, int size, int how) 861240233Sglebius{ 862240233Sglebius struct m_tag *t; 863240233Sglebius 864240233Sglebius t = (struct m_tag *)mem; 865240233Sglebius t->m_tag_cookie = MTAG_ABI_COMPAT; 866240233Sglebius t->m_tag_id = PACKET_TAG_PF; 867240233Sglebius t->m_tag_len = sizeof(struct pf_mtag); 868240233Sglebius t->m_tag_free = pf_mtag_free; 869240233Sglebius 870223637Sbz return (0); 871223637Sbz} 872223637Sbz 873240233Sglebiusstatic void 874240233Sglebiuspf_mtag_free(struct m_tag *t) 875130613Smlaier{ 876223637Sbz 877265008Smm uma_zfree(pf_mtag_z, t); 878240233Sglebius} 879223637Sbz 880240233Sglebiusstruct pf_mtag * 881240233Sglebiuspf_get_mtag(struct mbuf *m) 882240233Sglebius{ 883240233Sglebius struct m_tag *mtag; 884240233Sglebius 885240233Sglebius if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL) 886240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 887240233Sglebius 888265008Smm mtag = uma_zalloc(pf_mtag_z, M_NOWAIT); 889240233Sglebius if (mtag == NULL) 890240233Sglebius return (NULL); 891240233Sglebius bzero(mtag + 1, sizeof(struct pf_mtag)); 892240233Sglebius m_tag_prepend(m, mtag); 893240233Sglebius 894240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 895240233Sglebius} 896240233Sglebius 897240233Sglebiusstatic int 898240233Sglebiuspf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks, 899240233Sglebius struct pf_state *s) 900240233Sglebius{ 901251681Sglebius struct pf_keyhash *khs, *khw, *kh; 902240233Sglebius struct pf_state_key *sk, *cur; 903240233Sglebius struct pf_state *si, *olds = NULL; 904240233Sglebius int idx; 905240233Sglebius 906240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 907240233Sglebius KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__)); 908240233Sglebius KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__)); 909240233Sglebius 910240233Sglebius /* 911251681Sglebius * We need to lock hash slots of both keys. To avoid deadlock 912251681Sglebius * we always lock the slot with lower address first. Unlock order 913251681Sglebius * isn't important. 914251681Sglebius * 915251681Sglebius * We also need to lock ID hash slot before dropping key 916251681Sglebius * locks. On success we return with ID hash slot locked. 917251681Sglebius */ 918251681Sglebius 919251681Sglebius if (skw == sks) { 920251681Sglebius khs = khw = &V_pf_keyhash[pf_hashkey(skw)]; 921251681Sglebius PF_HASHROW_LOCK(khs); 922251681Sglebius } else { 923251681Sglebius khs = &V_pf_keyhash[pf_hashkey(sks)]; 924251681Sglebius khw = &V_pf_keyhash[pf_hashkey(skw)]; 925251681Sglebius if (khs == khw) { 926251681Sglebius PF_HASHROW_LOCK(khs); 927251681Sglebius } else if (khs < khw) { 928251681Sglebius PF_HASHROW_LOCK(khs); 929251681Sglebius PF_HASHROW_LOCK(khw); 930251681Sglebius } else { 931251681Sglebius PF_HASHROW_LOCK(khw); 932251681Sglebius PF_HASHROW_LOCK(khs); 933251681Sglebius } 934251681Sglebius } 935251681Sglebius 936251681Sglebius#define KEYS_UNLOCK() do { \ 937251681Sglebius if (khs != khw) { \ 938251681Sglebius PF_HASHROW_UNLOCK(khs); \ 939251681Sglebius PF_HASHROW_UNLOCK(khw); \ 940251681Sglebius } else \ 941251681Sglebius PF_HASHROW_UNLOCK(khs); \ 942251681Sglebius} while (0) 943251681Sglebius 944251681Sglebius /* 945240233Sglebius * First run: start with wire key. 946240233Sglebius */ 947240233Sglebius sk = skw; 948251681Sglebius kh = khw; 949240233Sglebius idx = PF_SK_WIRE; 950240233Sglebius 951240233Sglebiuskeyattach: 952240233Sglebius LIST_FOREACH(cur, &kh->keys, entry) 953240233Sglebius if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0) 954240233Sglebius break; 955240233Sglebius 956240233Sglebius if (cur != NULL) { 957240233Sglebius /* Key exists. Check for same kif, if none, add to key. */ 958240233Sglebius TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) { 959240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)]; 960240233Sglebius 961240233Sglebius PF_HASHROW_LOCK(ih); 962240233Sglebius if (si->kif == s->kif && 963240233Sglebius si->direction == s->direction) { 964223637Sbz if (sk->proto == IPPROTO_TCP && 965240233Sglebius si->src.state >= TCPS_FIN_WAIT_2 && 966240233Sglebius si->dst.state >= TCPS_FIN_WAIT_2) { 967251681Sglebius /* 968251681Sglebius * New state matches an old >FIN_WAIT_2 969251681Sglebius * state. We can't drop key hash locks, 970251681Sglebius * thus we can't unlink it properly. 971251681Sglebius * 972251681Sglebius * As a workaround we drop it into 973251681Sglebius * TCPS_CLOSED state, schedule purge 974251681Sglebius * ASAP and push it into the very end 975251681Sglebius * of the slot TAILQ, so that it won't 976251681Sglebius * conflict with our new state. 977251681Sglebius */ 978240233Sglebius si->src.state = si->dst.state = 979223637Sbz TCPS_CLOSED; 980251681Sglebius si->timeout = PFTM_PURGE; 981240233Sglebius olds = si; 982223637Sbz } else { 983223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 984223637Sbz printf("pf: %s key attach " 985223637Sbz "failed on %s: ", 986223637Sbz (idx == PF_SK_WIRE) ? 987223637Sbz "wire" : "stack", 988223637Sbz s->kif->pfik_name); 989223637Sbz pf_print_state_parts(s, 990223637Sbz (idx == PF_SK_WIRE) ? 991223637Sbz sk : NULL, 992223637Sbz (idx == PF_SK_STACK) ? 993223637Sbz sk : NULL); 994223637Sbz printf(", existing: "); 995240233Sglebius pf_print_state_parts(si, 996223637Sbz (idx == PF_SK_WIRE) ? 997223637Sbz sk : NULL, 998223637Sbz (idx == PF_SK_STACK) ? 999223637Sbz sk : NULL); 1000223637Sbz printf("\n"); 1001223637Sbz } 1002240233Sglebius PF_HASHROW_UNLOCK(ih); 1003251681Sglebius KEYS_UNLOCK(); 1004240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1005240233Sglebius if (idx == PF_SK_STACK) 1006240233Sglebius pf_detach_state(s); 1007250522Sglebius return (EEXIST); /* collision! */ 1008223637Sbz } 1009223637Sbz } 1010240233Sglebius PF_HASHROW_UNLOCK(ih); 1011240233Sglebius } 1012240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1013223637Sbz s->key[idx] = cur; 1014240233Sglebius } else { 1015240233Sglebius LIST_INSERT_HEAD(&kh->keys, sk, entry); 1016223637Sbz s->key[idx] = sk; 1017126258Smlaier } 1018126258Smlaier 1019240233Sglebiusstateattach: 1020240233Sglebius /* List is sorted, if-bound states before floating. */ 1021223637Sbz if (s->kif == V_pfi_all) 1022240233Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]); 1023223637Sbz else 1024240233Sglebius TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]); 1025223637Sbz 1026251681Sglebius if (olds) { 1027251681Sglebius TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]); 1028251681Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds, 1029251681Sglebius key_list[idx]); 1030251681Sglebius olds = NULL; 1031251681Sglebius } 1032251681Sglebius 1033240233Sglebius /* 1034240233Sglebius * Attach done. See how should we (or should not?) 1035240233Sglebius * attach a second key. 1036240233Sglebius */ 1037240233Sglebius if (sks == skw) { 1038240233Sglebius s->key[PF_SK_STACK] = s->key[PF_SK_WIRE]; 1039240233Sglebius idx = PF_SK_STACK; 1040240233Sglebius sks = NULL; 1041240233Sglebius goto stateattach; 1042240233Sglebius } else if (sks != NULL) { 1043240233Sglebius /* 1044240233Sglebius * Continue attaching with stack key. 1045240233Sglebius */ 1046240233Sglebius sk = sks; 1047251681Sglebius kh = khs; 1048240233Sglebius idx = PF_SK_STACK; 1049240233Sglebius sks = NULL; 1050240233Sglebius goto keyattach; 1051240233Sglebius } 1052240233Sglebius 1053251681Sglebius PF_STATE_LOCK(s); 1054251681Sglebius KEYS_UNLOCK(); 1055251681Sglebius 1056240233Sglebius KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL, 1057240233Sglebius ("%s failure", __func__)); 1058240233Sglebius 1059223637Sbz return (0); 1060251681Sglebius#undef KEYS_UNLOCK 1061223637Sbz} 1062223637Sbz 1063240233Sglebiusstatic void 1064223637Sbzpf_detach_state(struct pf_state *s) 1065223637Sbz{ 1066240233Sglebius struct pf_state_key *sks = s->key[PF_SK_STACK]; 1067240233Sglebius struct pf_keyhash *kh; 1068223637Sbz 1069240233Sglebius if (sks != NULL) { 1070240233Sglebius kh = &V_pf_keyhash[pf_hashkey(sks)]; 1071240233Sglebius PF_HASHROW_LOCK(kh); 1072240233Sglebius if (s->key[PF_SK_STACK] != NULL) 1073240233Sglebius pf_state_key_detach(s, PF_SK_STACK); 1074240233Sglebius /* 1075240233Sglebius * If both point to same key, then we are done. 1076240233Sglebius */ 1077240233Sglebius if (sks == s->key[PF_SK_WIRE]) { 1078240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1079240233Sglebius PF_HASHROW_UNLOCK(kh); 1080240233Sglebius return; 1081240233Sglebius } 1082240233Sglebius PF_HASHROW_UNLOCK(kh); 1083240233Sglebius } 1084223637Sbz 1085240233Sglebius if (s->key[PF_SK_WIRE] != NULL) { 1086240233Sglebius kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])]; 1087240233Sglebius PF_HASHROW_LOCK(kh); 1088240233Sglebius if (s->key[PF_SK_WIRE] != NULL) 1089240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1090240233Sglebius PF_HASHROW_UNLOCK(kh); 1091240233Sglebius } 1092223637Sbz} 1093223637Sbz 1094240233Sglebiusstatic void 1095223637Sbzpf_state_key_detach(struct pf_state *s, int idx) 1096223637Sbz{ 1097240233Sglebius struct pf_state_key *sk = s->key[idx]; 1098240233Sglebius#ifdef INVARIANTS 1099240233Sglebius struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)]; 1100223637Sbz 1101240233Sglebius PF_HASHROW_ASSERT(kh); 1102223637Sbz#endif 1103240233Sglebius TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]); 1104240233Sglebius s->key[idx] = NULL; 1105223637Sbz 1106240233Sglebius if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) { 1107240233Sglebius LIST_REMOVE(sk, entry); 1108240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1109223637Sbz } 1110223637Sbz} 1111223637Sbz 1112240233Sglebiusstatic int 1113240233Sglebiuspf_state_key_ctor(void *mem, int size, void *arg, int flags) 1114240233Sglebius{ 1115240233Sglebius struct pf_state_key *sk = mem; 1116240233Sglebius 1117240233Sglebius bzero(sk, sizeof(struct pf_state_key_cmp)); 1118240233Sglebius TAILQ_INIT(&sk->states[PF_SK_WIRE]); 1119240233Sglebius TAILQ_INIT(&sk->states[PF_SK_STACK]); 1120240233Sglebius 1121240233Sglebius return (0); 1122240233Sglebius} 1123240233Sglebius 1124223637Sbzstruct pf_state_key * 1125240233Sglebiuspf_state_key_setup(struct pf_pdesc *pd, struct pf_addr *saddr, 1126240233Sglebius struct pf_addr *daddr, u_int16_t sport, u_int16_t dport) 1127223637Sbz{ 1128240233Sglebius struct pf_state_key *sk; 1129223637Sbz 1130240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1131240233Sglebius if (sk == NULL) 1132223637Sbz return (NULL); 1133223637Sbz 1134240233Sglebius PF_ACPY(&sk->addr[pd->sidx], saddr, pd->af); 1135240233Sglebius PF_ACPY(&sk->addr[pd->didx], daddr, pd->af); 1136240233Sglebius sk->port[pd->sidx] = sport; 1137240233Sglebius sk->port[pd->didx] = dport; 1138240233Sglebius sk->proto = pd->proto; 1139240233Sglebius sk->af = pd->af; 1140240233Sglebius 1141223637Sbz return (sk); 1142223637Sbz} 1143223637Sbz 1144240233Sglebiusstruct pf_state_key * 1145240233Sglebiuspf_state_key_clone(struct pf_state_key *orig) 1146223637Sbz{ 1147240233Sglebius struct pf_state_key *sk; 1148223637Sbz 1149240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1150240233Sglebius if (sk == NULL) 1151240233Sglebius return (NULL); 1152223637Sbz 1153240233Sglebius bcopy(orig, sk, sizeof(struct pf_state_key_cmp)); 1154223637Sbz 1155240233Sglebius return (sk); 1156223637Sbz} 1157223637Sbz 1158223637Sbzint 1159223637Sbzpf_state_insert(struct pfi_kif *kif, struct pf_state_key *skw, 1160223637Sbz struct pf_state_key *sks, struct pf_state *s) 1161223637Sbz{ 1162240233Sglebius struct pf_idhash *ih; 1163240233Sglebius struct pf_state *cur; 1164250522Sglebius int error; 1165223637Sbz 1166240233Sglebius KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]), 1167240233Sglebius ("%s: sks not pristine", __func__)); 1168240233Sglebius KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]), 1169240233Sglebius ("%s: skw not pristine", __func__)); 1170240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1171240233Sglebius 1172223637Sbz s->kif = kif; 1173223637Sbz 1174223637Sbz if (s->id == 0 && s->creatorid == 0) { 1175240233Sglebius /* XXX: should be atomic, but probability of collision low */ 1176240233Sglebius if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID) 1177240233Sglebius V_pf_stateid[curcpu] = 1; 1178240233Sglebius s->id |= (uint64_t )curcpu << PFID_CPUSHIFT; 1179240233Sglebius s->id = htobe64(s->id); 1180223637Sbz s->creatorid = V_pf_status.hostid; 1181130613Smlaier } 1182240233Sglebius 1183251681Sglebius /* Returns with ID locked on success. */ 1184250522Sglebius if ((error = pf_state_key_attach(skw, sks, s)) != 0) 1185250522Sglebius return (error); 1186244184Sglebius 1187240233Sglebius ih = &V_pf_idhash[PF_IDHASH(s)]; 1188251681Sglebius PF_HASHROW_ASSERT(ih); 1189240233Sglebius LIST_FOREACH(cur, &ih->states, entry) 1190240233Sglebius if (cur->id == s->id && cur->creatorid == s->creatorid) 1191240233Sglebius break; 1192240233Sglebius 1193240233Sglebius if (cur != NULL) { 1194240233Sglebius PF_HASHROW_UNLOCK(ih); 1195223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 1196250521Sglebius printf("pf: state ID collision: " 1197250312Sglebius "id: %016llx creatorid: %08x\n", 1198240233Sglebius (unsigned long long)be64toh(s->id), 1199240233Sglebius ntohl(s->creatorid)); 1200130613Smlaier } 1201223637Sbz pf_detach_state(s); 1202250522Sglebius return (EEXIST); 1203130613Smlaier } 1204240233Sglebius LIST_INSERT_HEAD(&ih->states, s, entry); 1205240233Sglebius /* One for keys, one for ID hash. */ 1206240233Sglebius refcount_init(&s->refs, 2); 1207240233Sglebius 1208270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_INSERT], 1); 1209223637Sbz if (pfsync_insert_state_ptr != NULL) 1210223637Sbz pfsync_insert_state_ptr(s); 1211240233Sglebius 1212240233Sglebius /* Returns locked. */ 1213126258Smlaier return (0); 1214126258Smlaier} 1215126258Smlaier 1216240233Sglebius/* 1217240233Sglebius * Find state by ID: returns with locked row on success. 1218240233Sglebius */ 1219223637Sbzstruct pf_state * 1220240233Sglebiuspf_find_state_byid(uint64_t id, uint32_t creatorid) 1221223637Sbz{ 1222240233Sglebius struct pf_idhash *ih; 1223240233Sglebius struct pf_state *s; 1224240233Sglebius 1225270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1226223637Sbz 1227240233Sglebius ih = &V_pf_idhash[(be64toh(id) % (V_pf_hashmask + 1))]; 1228223637Sbz 1229240233Sglebius PF_HASHROW_LOCK(ih); 1230240233Sglebius LIST_FOREACH(s, &ih->states, entry) 1231240233Sglebius if (s->id == id && s->creatorid == creatorid) 1232240233Sglebius break; 1233223637Sbz 1234240233Sglebius if (s == NULL) 1235240233Sglebius PF_HASHROW_UNLOCK(ih); 1236240233Sglebius 1237240233Sglebius return (s); 1238223637Sbz} 1239223637Sbz 1240240233Sglebius/* 1241240233Sglebius * Find state by key. 1242240233Sglebius * Returns with ID hash slot locked on success. 1243240233Sglebius */ 1244240233Sglebiusstatic struct pf_state * 1245240233Sglebiuspf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int dir) 1246223637Sbz{ 1247240233Sglebius struct pf_keyhash *kh; 1248223637Sbz struct pf_state_key *sk; 1249240233Sglebius struct pf_state *s; 1250240233Sglebius int idx; 1251223637Sbz 1252270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1253223637Sbz 1254240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1255240233Sglebius 1256240233Sglebius PF_HASHROW_LOCK(kh); 1257240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1258240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1259240233Sglebius break; 1260240233Sglebius if (sk == NULL) { 1261240233Sglebius PF_HASHROW_UNLOCK(kh); 1262240233Sglebius return (NULL); 1263223637Sbz } 1264223637Sbz 1265240233Sglebius idx = (dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK); 1266223637Sbz 1267240233Sglebius /* List is sorted, if-bound states before floating ones. */ 1268240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) 1269240233Sglebius if (s->kif == V_pfi_all || s->kif == kif) { 1270240233Sglebius PF_STATE_LOCK(s); 1271240233Sglebius PF_HASHROW_UNLOCK(kh); 1272261018Sglebius if (s->timeout >= PFTM_MAX) { 1273240233Sglebius /* 1274261018Sglebius * State is either being processed by 1275261018Sglebius * pf_unlink_state() in an other thread, or 1276261018Sglebius * is scheduled for immediate expiry. 1277240233Sglebius */ 1278240233Sglebius PF_STATE_UNLOCK(s); 1279240233Sglebius return (NULL); 1280240233Sglebius } 1281240233Sglebius return (s); 1282240233Sglebius } 1283240233Sglebius PF_HASHROW_UNLOCK(kh); 1284223637Sbz 1285223637Sbz return (NULL); 1286223637Sbz} 1287223637Sbz 1288223637Sbzstruct pf_state * 1289223637Sbzpf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more) 1290223637Sbz{ 1291240233Sglebius struct pf_keyhash *kh; 1292223637Sbz struct pf_state_key *sk; 1293240233Sglebius struct pf_state *s, *ret = NULL; 1294240233Sglebius int idx, inout = 0; 1295223637Sbz 1296270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1297223637Sbz 1298240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1299223637Sbz 1300240233Sglebius PF_HASHROW_LOCK(kh); 1301240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1302240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1303240233Sglebius break; 1304240233Sglebius if (sk == NULL) { 1305240233Sglebius PF_HASHROW_UNLOCK(kh); 1306240233Sglebius return (NULL); 1307223637Sbz } 1308240233Sglebius switch (dir) { 1309240233Sglebius case PF_IN: 1310240233Sglebius idx = PF_SK_WIRE; 1311240233Sglebius break; 1312240233Sglebius case PF_OUT: 1313240233Sglebius idx = PF_SK_STACK; 1314240233Sglebius break; 1315240233Sglebius case PF_INOUT: 1316240233Sglebius idx = PF_SK_WIRE; 1317240233Sglebius inout = 1; 1318240233Sglebius break; 1319240233Sglebius default: 1320240233Sglebius panic("%s: dir %u", __func__, dir); 1321240233Sglebius } 1322240233Sglebiussecond_run: 1323240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 1324240233Sglebius if (more == NULL) { 1325240233Sglebius PF_HASHROW_UNLOCK(kh); 1326240233Sglebius return (s); 1327240233Sglebius } 1328240233Sglebius 1329240233Sglebius if (ret) 1330240233Sglebius (*more)++; 1331240233Sglebius else 1332240233Sglebius ret = s; 1333240233Sglebius } 1334240233Sglebius if (inout == 1) { 1335240233Sglebius inout = 0; 1336240233Sglebius idx = PF_SK_STACK; 1337240233Sglebius goto second_run; 1338240233Sglebius } 1339240233Sglebius PF_HASHROW_UNLOCK(kh); 1340240233Sglebius 1341240233Sglebius return (ret); 1342223637Sbz} 1343223637Sbz 1344223637Sbz/* END state table stuff */ 1345223637Sbz 1346240233Sglebiusstatic void 1347240233Sglebiuspf_send(struct pf_send_entry *pfse) 1348240233Sglebius{ 1349223637Sbz 1350240233Sglebius PF_SENDQ_LOCK(); 1351240233Sglebius STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next); 1352240233Sglebius PF_SENDQ_UNLOCK(); 1353240233Sglebius swi_sched(V_pf_swi_cookie, 0); 1354240233Sglebius} 1355240233Sglebius 1356126258Smlaiervoid 1357240233Sglebiuspf_intr(void *v) 1358240233Sglebius{ 1359240233Sglebius struct pf_send_head queue; 1360240233Sglebius struct pf_send_entry *pfse, *next; 1361240233Sglebius 1362240233Sglebius CURVNET_SET((struct vnet *)v); 1363240233Sglebius 1364240233Sglebius PF_SENDQ_LOCK(); 1365240233Sglebius queue = V_pf_sendqueue; 1366240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 1367240233Sglebius PF_SENDQ_UNLOCK(); 1368240233Sglebius 1369240233Sglebius STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) { 1370240233Sglebius switch (pfse->pfse_type) { 1371240233Sglebius#ifdef INET 1372240233Sglebius case PFSE_IP: 1373240233Sglebius ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL); 1374240233Sglebius break; 1375240233Sglebius case PFSE_ICMP: 1376240233Sglebius icmp_error(pfse->pfse_m, pfse->pfse_icmp_type, 1377240233Sglebius pfse->pfse_icmp_code, 0, pfse->pfse_icmp_mtu); 1378240233Sglebius break; 1379240233Sglebius#endif /* INET */ 1380240233Sglebius#ifdef INET6 1381240233Sglebius case PFSE_IP6: 1382240233Sglebius ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL, 1383240233Sglebius NULL); 1384240233Sglebius break; 1385240233Sglebius case PFSE_ICMP6: 1386240233Sglebius icmp6_error(pfse->pfse_m, pfse->pfse_icmp_type, 1387240233Sglebius pfse->pfse_icmp_code, pfse->pfse_icmp_mtu); 1388240233Sglebius break; 1389240233Sglebius#endif /* INET6 */ 1390240233Sglebius default: 1391240233Sglebius panic("%s: unknown type", __func__); 1392240233Sglebius } 1393240233Sglebius free(pfse, M_PFTEMP); 1394240233Sglebius } 1395240233Sglebius CURVNET_RESTORE(); 1396240233Sglebius} 1397240233Sglebius 1398240233Sglebiusvoid 1399171168Smlaierpf_purge_thread(void *v) 1400126258Smlaier{ 1401241039Sglebius u_int idx = 0; 1402171168Smlaier 1403223637Sbz CURVNET_SET((struct vnet *)v); 1404223637Sbz 1405171168Smlaier for (;;) { 1406240233Sglebius PF_RULES_RLOCK(); 1407240233Sglebius rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftm", hz / 10); 1408171168Smlaier 1409226527Sbz if (V_pf_end_threads) { 1410240233Sglebius /* 1411240233Sglebius * To cleanse up all kifs and rules we need 1412240233Sglebius * two runs: first one clears reference flags, 1413240233Sglebius * then pf_purge_expired_states() doesn't 1414240233Sglebius * raise them, and then second run frees. 1415240233Sglebius */ 1416240233Sglebius PF_RULES_RUNLOCK(); 1417240233Sglebius pf_purge_unlinked_rules(); 1418240233Sglebius pfi_kif_purge(); 1419171168Smlaier 1420240233Sglebius /* 1421240233Sglebius * Now purge everything. 1422240233Sglebius */ 1423241039Sglebius pf_purge_expired_states(0, V_pf_hashmask); 1424226527Sbz pf_purge_expired_fragments(); 1425240233Sglebius pf_purge_expired_src_nodes(); 1426240233Sglebius 1427240233Sglebius /* 1428240233Sglebius * Now all kifs & rules should be unreferenced, 1429240233Sglebius * thus should be successfully freed. 1430240233Sglebius */ 1431240233Sglebius pf_purge_unlinked_rules(); 1432240233Sglebius pfi_kif_purge(); 1433240233Sglebius 1434240233Sglebius /* 1435240233Sglebius * Announce success and exit. 1436240233Sglebius */ 1437240233Sglebius PF_RULES_RLOCK(); 1438226527Sbz V_pf_end_threads++; 1439240233Sglebius PF_RULES_RUNLOCK(); 1440226527Sbz wakeup(pf_purge_thread); 1441226527Sbz kproc_exit(0); 1442226527Sbz } 1443240233Sglebius PF_RULES_RUNLOCK(); 1444126258Smlaier 1445240233Sglebius /* Process 1/interval fraction of the state table every run. */ 1446241039Sglebius idx = pf_purge_expired_states(idx, V_pf_hashmask / 1447240233Sglebius (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10)); 1448196372Smlaier 1449240233Sglebius /* Purge other expired types every PFTM_INTERVAL seconds. */ 1450241039Sglebius if (idx == 0) { 1451240233Sglebius /* 1452240233Sglebius * Order is important: 1453240233Sglebius * - states and src nodes reference rules 1454240233Sglebius * - states and rules reference kifs 1455240233Sglebius */ 1456171168Smlaier pf_purge_expired_fragments(); 1457240233Sglebius pf_purge_expired_src_nodes(); 1458240233Sglebius pf_purge_unlinked_rules(); 1459240233Sglebius pfi_kif_purge(); 1460171168Smlaier } 1461171168Smlaier } 1462240233Sglebius /* not reached */ 1463223637Sbz CURVNET_RESTORE(); 1464126258Smlaier} 1465126258Smlaier 1466126258Smlaieru_int32_t 1467126258Smlaierpf_state_expires(const struct pf_state *state) 1468126258Smlaier{ 1469126258Smlaier u_int32_t timeout; 1470126258Smlaier u_int32_t start; 1471126258Smlaier u_int32_t end; 1472126258Smlaier u_int32_t states; 1473126258Smlaier 1474126258Smlaier /* handle all PFTM_* > PFTM_MAX here */ 1475126258Smlaier if (state->timeout == PFTM_PURGE) 1476240233Sglebius return (time_uptime); 1477171168Smlaier KASSERT(state->timeout != PFTM_UNLINKED, 1478171168Smlaier ("pf_state_expires: timeout == PFTM_UNLINKED")); 1479240233Sglebius KASSERT((state->timeout < PFTM_MAX), 1480126261Smlaier ("pf_state_expires: timeout > PFTM_MAX")); 1481126258Smlaier timeout = state->rule.ptr->timeout[state->timeout]; 1482126258Smlaier if (!timeout) 1483223637Sbz timeout = V_pf_default_rule.timeout[state->timeout]; 1484126258Smlaier start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START]; 1485126258Smlaier if (start) { 1486126258Smlaier end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END]; 1487263029Sglebius states = counter_u64_fetch(state->rule.ptr->states_cur); 1488126258Smlaier } else { 1489223637Sbz start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START]; 1490223637Sbz end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END]; 1491223637Sbz states = V_pf_status.states; 1492126258Smlaier } 1493126258Smlaier if (end && states > start && start < end) { 1494126258Smlaier if (states < end) 1495126258Smlaier return (state->expire + timeout * (end - states) / 1496126258Smlaier (end - start)); 1497126258Smlaier else 1498240233Sglebius return (time_uptime); 1499126258Smlaier } 1500126258Smlaier return (state->expire + timeout); 1501126258Smlaier} 1502126258Smlaier 1503126258Smlaiervoid 1504240233Sglebiuspf_purge_expired_src_nodes() 1505126258Smlaier{ 1506261019Sglebius struct pf_src_node_list freelist; 1507240233Sglebius struct pf_srchash *sh; 1508240233Sglebius struct pf_src_node *cur, *next; 1509240233Sglebius int i; 1510126258Smlaier 1511261019Sglebius LIST_INIT(&freelist); 1512240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 1513240233Sglebius PF_HASHROW_LOCK(sh); 1514240233Sglebius LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next) 1515261018Sglebius if (cur->states == 0 && cur->expire <= time_uptime) { 1516261019Sglebius pf_unlink_src_node_locked(cur); 1517261019Sglebius LIST_INSERT_HEAD(&freelist, cur, entry); 1518240233Sglebius } else if (cur->rule.ptr != NULL) 1519240233Sglebius cur->rule.ptr->rule_flag |= PFRULE_REFS; 1520240233Sglebius PF_HASHROW_UNLOCK(sh); 1521223637Sbz } 1522261019Sglebius 1523261019Sglebius pf_free_src_nodes(&freelist); 1524270574Sglebius 1525270574Sglebius V_pf_status.src_nodes = uma_zone_get_cur(V_pf_sources_z); 1526130613Smlaier} 1527126258Smlaier 1528240233Sglebiusstatic void 1529130613Smlaierpf_src_tree_remove_state(struct pf_state *s) 1530130613Smlaier{ 1531130613Smlaier u_int32_t timeout; 1532126258Smlaier 1533130613Smlaier if (s->src_node != NULL) { 1534223637Sbz if (s->src.tcp_est) 1535223637Sbz --s->src_node->conn; 1536261018Sglebius if (--s->src_node->states == 0) { 1537130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1538130613Smlaier if (!timeout) 1539130613Smlaier timeout = 1540223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1541240233Sglebius s->src_node->expire = time_uptime + timeout; 1542130613Smlaier } 1543130613Smlaier } 1544130613Smlaier if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) { 1545261018Sglebius if (--s->nat_src_node->states == 0) { 1546130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1547130613Smlaier if (!timeout) 1548130613Smlaier timeout = 1549223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1550240233Sglebius s->nat_src_node->expire = time_uptime + timeout; 1551130613Smlaier } 1552130613Smlaier } 1553130613Smlaier s->src_node = s->nat_src_node = NULL; 1554130613Smlaier} 1555126258Smlaier 1556240233Sglebius/* 1557240233Sglebius * Unlink and potentilly free a state. Function may be 1558240233Sglebius * called with ID hash row locked, but always returns 1559240233Sglebius * unlocked, since it needs to go through key hash locking. 1560240233Sglebius */ 1561240233Sglebiusint 1562240233Sglebiuspf_unlink_state(struct pf_state *s, u_int flags) 1563145836Smlaier{ 1564240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; 1565223637Sbz 1566240233Sglebius if ((flags & PF_ENTER_LOCKED) == 0) 1567240233Sglebius PF_HASHROW_LOCK(ih); 1568240233Sglebius else 1569240233Sglebius PF_HASHROW_ASSERT(ih); 1570240233Sglebius 1571240233Sglebius if (s->timeout == PFTM_UNLINKED) { 1572240233Sglebius /* 1573240233Sglebius * State is being processed 1574240233Sglebius * by pf_unlink_state() in 1575240233Sglebius * an other thread. 1576240233Sglebius */ 1577240233Sglebius PF_HASHROW_UNLOCK(ih); 1578240233Sglebius return (0); /* XXXGL: undefined actually */ 1579240233Sglebius } 1580240233Sglebius 1581240233Sglebius if (s->src.state == PF_TCPS_PROXY_DST) { 1582223637Sbz /* XXX wire key the right one? */ 1583240233Sglebius pf_send_tcp(NULL, s->rule.ptr, s->key[PF_SK_WIRE]->af, 1584240233Sglebius &s->key[PF_SK_WIRE]->addr[1], 1585240233Sglebius &s->key[PF_SK_WIRE]->addr[0], 1586240233Sglebius s->key[PF_SK_WIRE]->port[1], 1587240233Sglebius s->key[PF_SK_WIRE]->port[0], 1588240233Sglebius s->src.seqhi, s->src.seqlo + 1, 1589240233Sglebius TH_RST|TH_ACK, 0, 0, 0, 1, s->tag, NULL); 1590171168Smlaier } 1591240233Sglebius 1592240233Sglebius LIST_REMOVE(s, entry); 1593240233Sglebius pf_src_tree_remove_state(s); 1594240233Sglebius 1595223637Sbz if (pfsync_delete_state_ptr != NULL) 1596240233Sglebius pfsync_delete_state_ptr(s); 1597240233Sglebius 1598263029Sglebius STATE_DEC_COUNTERS(s); 1599243944Sglebius 1600243941Sglebius s->timeout = PFTM_UNLINKED; 1601243941Sglebius 1602243941Sglebius PF_HASHROW_UNLOCK(ih); 1603243941Sglebius 1604240233Sglebius pf_detach_state(s); 1605240233Sglebius refcount_release(&s->refs); 1606240233Sglebius 1607240233Sglebius return (pf_release_state(s)); 1608171168Smlaier} 1609171168Smlaier 1610171168Smlaiervoid 1611171168Smlaierpf_free_state(struct pf_state *cur) 1612171168Smlaier{ 1613223637Sbz 1614240233Sglebius KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 1615240233Sglebius KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 1616240233Sglebius cur->timeout)); 1617243944Sglebius 1618145836Smlaier pf_normalize_tcp_cleanup(cur); 1619240233Sglebius uma_zfree(V_pf_state_z, cur); 1620270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_REMOVALS], 1); 1621145836Smlaier} 1622145836Smlaier 1623240233Sglebius/* 1624240233Sglebius * Called only from pf_purge_thread(), thus serialized. 1625240233Sglebius */ 1626241039Sglebiusstatic u_int 1627241039Sglebiuspf_purge_expired_states(u_int i, int maxcheck) 1628130613Smlaier{ 1629240233Sglebius struct pf_idhash *ih; 1630240233Sglebius struct pf_state *s; 1631240233Sglebius 1632240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1633240233Sglebius 1634240233Sglebius /* 1635240233Sglebius * Go through hash and unlink states that expire now. 1636240233Sglebius */ 1637240233Sglebius while (maxcheck > 0) { 1638240233Sglebius 1639240233Sglebius ih = &V_pf_idhash[i]; 1640240233Sglebiusrelock: 1641240233Sglebius PF_HASHROW_LOCK(ih); 1642240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 1643240233Sglebius if (pf_state_expires(s) <= time_uptime) { 1644240233Sglebius V_pf_status.states -= 1645240233Sglebius pf_unlink_state(s, PF_ENTER_LOCKED); 1646240233Sglebius goto relock; 1647171168Smlaier } 1648240233Sglebius s->rule.ptr->rule_flag |= PFRULE_REFS; 1649240233Sglebius if (s->nat_rule.ptr != NULL) 1650240233Sglebius s->nat_rule.ptr->rule_flag |= PFRULE_REFS; 1651240233Sglebius if (s->anchor.ptr != NULL) 1652240233Sglebius s->anchor.ptr->rule_flag |= PFRULE_REFS; 1653240233Sglebius s->kif->pfik_flags |= PFI_IFLAG_REFS; 1654240233Sglebius if (s->rt_kif) 1655240233Sglebius s->rt_kif->pfik_flags |= PFI_IFLAG_REFS; 1656171168Smlaier } 1657240233Sglebius PF_HASHROW_UNLOCK(ih); 1658241039Sglebius 1659241039Sglebius /* Return when we hit end of hash. */ 1660241039Sglebius if (++i > V_pf_hashmask) { 1661241039Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1662241039Sglebius return (0); 1663241039Sglebius } 1664241039Sglebius 1665240233Sglebius maxcheck--; 1666126258Smlaier } 1667171168Smlaier 1668240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1669196372Smlaier 1670241039Sglebius return (i); 1671126258Smlaier} 1672126258Smlaier 1673240233Sglebiusstatic void 1674240233Sglebiuspf_purge_unlinked_rules() 1675126258Smlaier{ 1676240233Sglebius struct pf_rulequeue tmpq; 1677240233Sglebius struct pf_rule *r, *r1; 1678126258Smlaier 1679240233Sglebius /* 1680240811Sglebius * If we have overloading task pending, then we'd 1681240811Sglebius * better skip purging this time. There is a tiny 1682240811Sglebius * probability that overloading task references 1683240811Sglebius * an already unlinked rule. 1684240811Sglebius */ 1685240811Sglebius PF_OVERLOADQ_LOCK(); 1686240811Sglebius if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 1687240811Sglebius PF_OVERLOADQ_UNLOCK(); 1688240811Sglebius return; 1689240811Sglebius } 1690240811Sglebius PF_OVERLOADQ_UNLOCK(); 1691240811Sglebius 1692240811Sglebius /* 1693240233Sglebius * Do naive mark-and-sweep garbage collecting of old rules. 1694240233Sglebius * Reference flag is raised by pf_purge_expired_states() 1695240233Sglebius * and pf_purge_expired_src_nodes(). 1696240233Sglebius * 1697240233Sglebius * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 1698240233Sglebius * use a temporary queue. 1699240233Sglebius */ 1700240233Sglebius TAILQ_INIT(&tmpq); 1701240233Sglebius PF_UNLNKDRULES_LOCK(); 1702240233Sglebius TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 1703240233Sglebius if (!(r->rule_flag & PFRULE_REFS)) { 1704240233Sglebius TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 1705240233Sglebius TAILQ_INSERT_TAIL(&tmpq, r, entries); 1706240233Sglebius } else 1707240233Sglebius r->rule_flag &= ~PFRULE_REFS; 1708240233Sglebius } 1709240233Sglebius PF_UNLNKDRULES_UNLOCK(); 1710126258Smlaier 1711240233Sglebius if (!TAILQ_EMPTY(&tmpq)) { 1712240233Sglebius PF_RULES_WLOCK(); 1713240233Sglebius TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 1714240233Sglebius TAILQ_REMOVE(&tmpq, r, entries); 1715240233Sglebius pf_free_rule(r); 1716240233Sglebius } 1717240233Sglebius PF_RULES_WUNLOCK(); 1718240233Sglebius } 1719126258Smlaier} 1720126258Smlaier 1721126258Smlaiervoid 1722126258Smlaierpf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 1723126258Smlaier{ 1724126258Smlaier switch (af) { 1725126258Smlaier#ifdef INET 1726126258Smlaier case AF_INET: { 1727126258Smlaier u_int32_t a = ntohl(addr->addr32[0]); 1728126258Smlaier printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 1729126258Smlaier (a>>8)&255, a&255); 1730126258Smlaier if (p) { 1731126258Smlaier p = ntohs(p); 1732126258Smlaier printf(":%u", p); 1733126258Smlaier } 1734126258Smlaier break; 1735126258Smlaier } 1736126258Smlaier#endif /* INET */ 1737126258Smlaier#ifdef INET6 1738126258Smlaier case AF_INET6: { 1739126258Smlaier u_int16_t b; 1740223637Sbz u_int8_t i, curstart, curend, maxstart, maxend; 1741223637Sbz curstart = curend = maxstart = maxend = 255; 1742126258Smlaier for (i = 0; i < 8; i++) { 1743126258Smlaier if (!addr->addr16[i]) { 1744126258Smlaier if (curstart == 255) 1745126258Smlaier curstart = i; 1746223637Sbz curend = i; 1747126258Smlaier } else { 1748223637Sbz if ((curend - curstart) > 1749223637Sbz (maxend - maxstart)) { 1750223637Sbz maxstart = curstart; 1751223637Sbz maxend = curend; 1752126258Smlaier } 1753223637Sbz curstart = curend = 255; 1754126258Smlaier } 1755126258Smlaier } 1756223637Sbz if ((curend - curstart) > 1757223637Sbz (maxend - maxstart)) { 1758223637Sbz maxstart = curstart; 1759223637Sbz maxend = curend; 1760223637Sbz } 1761126258Smlaier for (i = 0; i < 8; i++) { 1762126258Smlaier if (i >= maxstart && i <= maxend) { 1763223637Sbz if (i == 0) 1764223637Sbz printf(":"); 1765223637Sbz if (i == maxend) 1766223637Sbz printf(":"); 1767126258Smlaier } else { 1768126258Smlaier b = ntohs(addr->addr16[i]); 1769126258Smlaier printf("%x", b); 1770126258Smlaier if (i < 7) 1771126258Smlaier printf(":"); 1772126258Smlaier } 1773126258Smlaier } 1774126258Smlaier if (p) { 1775126258Smlaier p = ntohs(p); 1776126258Smlaier printf("[%u]", p); 1777126258Smlaier } 1778126258Smlaier break; 1779126258Smlaier } 1780126258Smlaier#endif /* INET6 */ 1781126258Smlaier } 1782126258Smlaier} 1783126258Smlaier 1784126258Smlaiervoid 1785126258Smlaierpf_print_state(struct pf_state *s) 1786126258Smlaier{ 1787223637Sbz pf_print_state_parts(s, NULL, NULL); 1788223637Sbz} 1789223637Sbz 1790240233Sglebiusstatic void 1791223637Sbzpf_print_state_parts(struct pf_state *s, 1792223637Sbz struct pf_state_key *skwp, struct pf_state_key *sksp) 1793223637Sbz{ 1794223637Sbz struct pf_state_key *skw, *sks; 1795223637Sbz u_int8_t proto, dir; 1796223637Sbz 1797223637Sbz /* Do our best to fill these, but they're skipped if NULL */ 1798223637Sbz skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 1799223637Sbz sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 1800223637Sbz proto = skw ? skw->proto : (sks ? sks->proto : 0); 1801223637Sbz dir = s ? s->direction : 0; 1802223637Sbz 1803223637Sbz switch (proto) { 1804223637Sbz case IPPROTO_IPV4: 1805223637Sbz printf("IPv4"); 1806223637Sbz break; 1807223637Sbz case IPPROTO_IPV6: 1808223637Sbz printf("IPv6"); 1809223637Sbz break; 1810126258Smlaier case IPPROTO_TCP: 1811223637Sbz printf("TCP"); 1812126258Smlaier break; 1813126258Smlaier case IPPROTO_UDP: 1814223637Sbz printf("UDP"); 1815126258Smlaier break; 1816126258Smlaier case IPPROTO_ICMP: 1817223637Sbz printf("ICMP"); 1818126258Smlaier break; 1819126258Smlaier case IPPROTO_ICMPV6: 1820223637Sbz printf("ICMPv6"); 1821126258Smlaier break; 1822126258Smlaier default: 1823223637Sbz printf("%u", skw->proto); 1824126258Smlaier break; 1825126258Smlaier } 1826223637Sbz switch (dir) { 1827223637Sbz case PF_IN: 1828223637Sbz printf(" in"); 1829223637Sbz break; 1830223637Sbz case PF_OUT: 1831223637Sbz printf(" out"); 1832223637Sbz break; 1833223637Sbz } 1834223637Sbz if (skw) { 1835223637Sbz printf(" wire: "); 1836223637Sbz pf_print_host(&skw->addr[0], skw->port[0], skw->af); 1837223637Sbz printf(" "); 1838223637Sbz pf_print_host(&skw->addr[1], skw->port[1], skw->af); 1839223637Sbz } 1840223637Sbz if (sks) { 1841223637Sbz printf(" stack: "); 1842223637Sbz if (sks != skw) { 1843223637Sbz pf_print_host(&sks->addr[0], sks->port[0], sks->af); 1844223637Sbz printf(" "); 1845223637Sbz pf_print_host(&sks->addr[1], sks->port[1], sks->af); 1846223637Sbz } else 1847223637Sbz printf("-"); 1848223637Sbz } 1849223637Sbz if (s) { 1850223637Sbz if (proto == IPPROTO_TCP) { 1851223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1852223637Sbz s->src.seqlo, s->src.seqhi, 1853223637Sbz s->src.max_win, s->src.seqdiff); 1854223637Sbz if (s->src.wscale && s->dst.wscale) 1855223637Sbz printf(" wscale=%u", 1856223637Sbz s->src.wscale & PF_WSCALE_MASK); 1857223637Sbz printf("]"); 1858223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1859223637Sbz s->dst.seqlo, s->dst.seqhi, 1860223637Sbz s->dst.max_win, s->dst.seqdiff); 1861223637Sbz if (s->src.wscale && s->dst.wscale) 1862223637Sbz printf(" wscale=%u", 1863223637Sbz s->dst.wscale & PF_WSCALE_MASK); 1864223637Sbz printf("]"); 1865223637Sbz } 1866223637Sbz printf(" %u:%u", s->src.state, s->dst.state); 1867223637Sbz } 1868126258Smlaier} 1869126258Smlaier 1870126258Smlaiervoid 1871126258Smlaierpf_print_flags(u_int8_t f) 1872126258Smlaier{ 1873126258Smlaier if (f) 1874126258Smlaier printf(" "); 1875126258Smlaier if (f & TH_FIN) 1876126258Smlaier printf("F"); 1877126258Smlaier if (f & TH_SYN) 1878126258Smlaier printf("S"); 1879126258Smlaier if (f & TH_RST) 1880126258Smlaier printf("R"); 1881126258Smlaier if (f & TH_PUSH) 1882126258Smlaier printf("P"); 1883126258Smlaier if (f & TH_ACK) 1884126258Smlaier printf("A"); 1885126258Smlaier if (f & TH_URG) 1886126258Smlaier printf("U"); 1887126258Smlaier if (f & TH_ECE) 1888126258Smlaier printf("E"); 1889126258Smlaier if (f & TH_CWR) 1890126258Smlaier printf("W"); 1891126258Smlaier} 1892126258Smlaier 1893126258Smlaier#define PF_SET_SKIP_STEPS(i) \ 1894126258Smlaier do { \ 1895126258Smlaier while (head[i] != cur) { \ 1896126258Smlaier head[i]->skip[i].ptr = cur; \ 1897126258Smlaier head[i] = TAILQ_NEXT(head[i], entries); \ 1898126258Smlaier } \ 1899126258Smlaier } while (0) 1900126258Smlaier 1901126258Smlaiervoid 1902126258Smlaierpf_calc_skip_steps(struct pf_rulequeue *rules) 1903126258Smlaier{ 1904126258Smlaier struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT]; 1905126258Smlaier int i; 1906126258Smlaier 1907126258Smlaier cur = TAILQ_FIRST(rules); 1908126258Smlaier prev = cur; 1909126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1910126258Smlaier head[i] = cur; 1911126258Smlaier while (cur != NULL) { 1912126258Smlaier 1913130613Smlaier if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 1914126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_IFP); 1915126258Smlaier if (cur->direction != prev->direction) 1916126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DIR); 1917126258Smlaier if (cur->af != prev->af) 1918126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_AF); 1919126258Smlaier if (cur->proto != prev->proto) 1920126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 1921145836Smlaier if (cur->src.neg != prev->src.neg || 1922126258Smlaier pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 1923126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 1924126258Smlaier if (cur->src.port[0] != prev->src.port[0] || 1925126258Smlaier cur->src.port[1] != prev->src.port[1] || 1926126258Smlaier cur->src.port_op != prev->src.port_op) 1927126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 1928145836Smlaier if (cur->dst.neg != prev->dst.neg || 1929126258Smlaier pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 1930126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 1931126258Smlaier if (cur->dst.port[0] != prev->dst.port[0] || 1932126258Smlaier cur->dst.port[1] != prev->dst.port[1] || 1933126258Smlaier cur->dst.port_op != prev->dst.port_op) 1934126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 1935126258Smlaier 1936126258Smlaier prev = cur; 1937126258Smlaier cur = TAILQ_NEXT(cur, entries); 1938126258Smlaier } 1939126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1940126258Smlaier PF_SET_SKIP_STEPS(i); 1941126258Smlaier} 1942126258Smlaier 1943240233Sglebiusstatic int 1944126258Smlaierpf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 1945126258Smlaier{ 1946126258Smlaier if (aw1->type != aw2->type) 1947126258Smlaier return (1); 1948126258Smlaier switch (aw1->type) { 1949126258Smlaier case PF_ADDR_ADDRMASK: 1950223637Sbz case PF_ADDR_RANGE: 1951126258Smlaier if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0)) 1952126258Smlaier return (1); 1953126258Smlaier if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0)) 1954126258Smlaier return (1); 1955126258Smlaier return (0); 1956126258Smlaier case PF_ADDR_DYNIFTL: 1957130613Smlaier return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 1958126258Smlaier case PF_ADDR_NOROUTE: 1959171168Smlaier case PF_ADDR_URPFFAILED: 1960126258Smlaier return (0); 1961126258Smlaier case PF_ADDR_TABLE: 1962126258Smlaier return (aw1->p.tbl != aw2->p.tbl); 1963126258Smlaier default: 1964126258Smlaier printf("invalid address type: %d\n", aw1->type); 1965126258Smlaier return (1); 1966126258Smlaier } 1967126258Smlaier} 1968126258Smlaier 1969293894Sglebius/** 1970293894Sglebius * Checksum updates are a little complicated because the checksum in the TCP/UDP 1971293894Sglebius * header isn't always a full checksum. In some cases (i.e. output) it's a 1972293894Sglebius * pseudo-header checksum, which is a partial checksum over src/dst IP 1973293894Sglebius * addresses, protocol number and length. 1974293894Sglebius * 1975293894Sglebius * That means we have the following cases: 1976293894Sglebius * * Input or forwarding: we don't have TSO, the checksum fields are full 1977293894Sglebius * checksums, we need to update the checksum whenever we change anything. 1978293894Sglebius * * Output (i.e. the checksum is a pseudo-header checksum): 1979293894Sglebius * x The field being updated is src/dst address or affects the length of 1980293894Sglebius * the packet. We need to update the pseudo-header checksum (note that this 1981293894Sglebius * checksum is not ones' complement). 1982293894Sglebius * x Some other field is being modified (e.g. src/dst port numbers): We 1983293894Sglebius * don't have to update anything. 1984293894Sglebius **/ 1985126258Smlaieru_int16_t 1986126258Smlaierpf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 1987126258Smlaier{ 1988126258Smlaier u_int32_t l; 1989126258Smlaier 1990126258Smlaier if (udp && !cksum) 1991126258Smlaier return (0x0000); 1992126258Smlaier l = cksum + old - new; 1993126258Smlaier l = (l >> 16) + (l & 65535); 1994126258Smlaier l = l & 65535; 1995126258Smlaier if (udp && !l) 1996126258Smlaier return (0xFFFF); 1997126258Smlaier return (l); 1998126258Smlaier} 1999126258Smlaier 2000293894Sglebiusu_int16_t 2001293894Sglebiuspf_proto_cksum_fixup(struct mbuf *m, u_int16_t cksum, u_int16_t old, 2002293894Sglebius u_int16_t new, u_int8_t udp) 2003293894Sglebius{ 2004293894Sglebius if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 2005293894Sglebius return (cksum); 2006293894Sglebius 2007293894Sglebius return (pf_cksum_fixup(cksum, old, new, udp)); 2008293894Sglebius} 2009293894Sglebius 2010240233Sglebiusstatic void 2011293894Sglebiuspf_change_ap(struct mbuf *m, struct pf_addr *a, u_int16_t *p, u_int16_t *ic, 2012293894Sglebius u_int16_t *pc, struct pf_addr *an, u_int16_t pn, u_int8_t u, 2013293894Sglebius sa_family_t af) 2014126258Smlaier{ 2015126258Smlaier struct pf_addr ao; 2016126258Smlaier u_int16_t po = *p; 2017126258Smlaier 2018126258Smlaier PF_ACPY(&ao, a, af); 2019126258Smlaier PF_ACPY(a, an, af); 2020126258Smlaier 2021293894Sglebius if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6)) 2022293894Sglebius *pc = ~*pc; 2023293894Sglebius 2024126258Smlaier *p = pn; 2025126258Smlaier 2026126258Smlaier switch (af) { 2027126258Smlaier#ifdef INET 2028126258Smlaier case AF_INET: 2029126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2030126258Smlaier ao.addr16[0], an->addr16[0], 0), 2031126258Smlaier ao.addr16[1], an->addr16[1], 0); 2032126258Smlaier *p = pn; 2033293894Sglebius 2034293894Sglebius *pc = pf_cksum_fixup(pf_cksum_fixup(*pc, 2035126258Smlaier ao.addr16[0], an->addr16[0], u), 2036293894Sglebius ao.addr16[1], an->addr16[1], u); 2037293894Sglebius 2038293894Sglebius *pc = pf_proto_cksum_fixup(m, *pc, po, pn, u); 2039126258Smlaier break; 2040126258Smlaier#endif /* INET */ 2041126258Smlaier#ifdef INET6 2042126258Smlaier case AF_INET6: 2043126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2044126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2045293894Sglebius pf_cksum_fixup(pf_cksum_fixup(*pc, 2046126258Smlaier ao.addr16[0], an->addr16[0], u), 2047126258Smlaier ao.addr16[1], an->addr16[1], u), 2048126258Smlaier ao.addr16[2], an->addr16[2], u), 2049126258Smlaier ao.addr16[3], an->addr16[3], u), 2050126258Smlaier ao.addr16[4], an->addr16[4], u), 2051126258Smlaier ao.addr16[5], an->addr16[5], u), 2052126258Smlaier ao.addr16[6], an->addr16[6], u), 2053293894Sglebius ao.addr16[7], an->addr16[7], u); 2054293894Sglebius 2055293894Sglebius *pc = pf_proto_cksum_fixup(m, *pc, po, pn, u); 2056126258Smlaier break; 2057126258Smlaier#endif /* INET6 */ 2058126258Smlaier } 2059293894Sglebius 2060293894Sglebius if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | 2061293894Sglebius CSUM_DELAY_DATA_IPV6)) { 2062293894Sglebius *pc = ~*pc; 2063293894Sglebius if (! *pc) 2064293894Sglebius *pc = 0xffff; 2065293894Sglebius } 2066126258Smlaier} 2067126258Smlaier 2068126258Smlaier/* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 2069126258Smlaiervoid 2070126258Smlaierpf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 2071126258Smlaier{ 2072126258Smlaier u_int32_t ao; 2073126258Smlaier 2074126258Smlaier memcpy(&ao, a, sizeof(ao)); 2075126258Smlaier memcpy(a, &an, sizeof(u_int32_t)); 2076126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 2077126258Smlaier ao % 65536, an % 65536, u); 2078126258Smlaier} 2079126258Smlaier 2080293894Sglebiusvoid 2081293894Sglebiuspf_change_proto_a(struct mbuf *m, void *a, u_int16_t *c, u_int32_t an, u_int8_t udp) 2082293894Sglebius{ 2083293894Sglebius u_int32_t ao; 2084293894Sglebius 2085293894Sglebius memcpy(&ao, a, sizeof(ao)); 2086293894Sglebius memcpy(a, &an, sizeof(u_int32_t)); 2087293894Sglebius 2088293894Sglebius *c = pf_proto_cksum_fixup(m, 2089293894Sglebius pf_proto_cksum_fixup(m, *c, ao / 65536, an / 65536, udp), 2090293894Sglebius ao % 65536, an % 65536, udp); 2091293894Sglebius} 2092293894Sglebius 2093126258Smlaier#ifdef INET6 2094240233Sglebiusstatic void 2095126258Smlaierpf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 2096126258Smlaier{ 2097126258Smlaier struct pf_addr ao; 2098126258Smlaier 2099126258Smlaier PF_ACPY(&ao, a, AF_INET6); 2100126258Smlaier PF_ACPY(a, an, AF_INET6); 2101126258Smlaier 2102126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2103126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2104126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*c, 2105126258Smlaier ao.addr16[0], an->addr16[0], u), 2106126258Smlaier ao.addr16[1], an->addr16[1], u), 2107126258Smlaier ao.addr16[2], an->addr16[2], u), 2108126258Smlaier ao.addr16[3], an->addr16[3], u), 2109126258Smlaier ao.addr16[4], an->addr16[4], u), 2110126258Smlaier ao.addr16[5], an->addr16[5], u), 2111126258Smlaier ao.addr16[6], an->addr16[6], u), 2112126258Smlaier ao.addr16[7], an->addr16[7], u); 2113126258Smlaier} 2114126258Smlaier#endif /* INET6 */ 2115126258Smlaier 2116240233Sglebiusstatic void 2117126258Smlaierpf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 2118126258Smlaier struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 2119126258Smlaier u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 2120126258Smlaier{ 2121126258Smlaier struct pf_addr oia, ooa; 2122126258Smlaier 2123126258Smlaier PF_ACPY(&oia, ia, af); 2124223637Sbz if (oa) 2125223637Sbz PF_ACPY(&ooa, oa, af); 2126126258Smlaier 2127126258Smlaier /* Change inner protocol port, fix inner protocol checksum. */ 2128126258Smlaier if (ip != NULL) { 2129126258Smlaier u_int16_t oip = *ip; 2130223637Sbz u_int32_t opc; 2131126258Smlaier 2132126258Smlaier if (pc != NULL) 2133126258Smlaier opc = *pc; 2134126258Smlaier *ip = np; 2135126258Smlaier if (pc != NULL) 2136126258Smlaier *pc = pf_cksum_fixup(*pc, oip, *ip, u); 2137126258Smlaier *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 2138126258Smlaier if (pc != NULL) 2139126258Smlaier *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 2140126258Smlaier } 2141126258Smlaier /* Change inner ip address, fix inner ip and icmp checksums. */ 2142126258Smlaier PF_ACPY(ia, na, af); 2143126258Smlaier switch (af) { 2144126258Smlaier#ifdef INET 2145126258Smlaier case AF_INET: { 2146126258Smlaier u_int32_t oh2c = *h2c; 2147126258Smlaier 2148126258Smlaier *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 2149126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2150126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2151126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2152126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2153126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2154126258Smlaier *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 2155126258Smlaier break; 2156126258Smlaier } 2157126258Smlaier#endif /* INET */ 2158126258Smlaier#ifdef INET6 2159126258Smlaier case AF_INET6: 2160126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2161126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2162126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*ic, 2163126258Smlaier oia.addr16[0], ia->addr16[0], u), 2164126258Smlaier oia.addr16[1], ia->addr16[1], u), 2165126258Smlaier oia.addr16[2], ia->addr16[2], u), 2166126258Smlaier oia.addr16[3], ia->addr16[3], u), 2167126258Smlaier oia.addr16[4], ia->addr16[4], u), 2168126258Smlaier oia.addr16[5], ia->addr16[5], u), 2169126258Smlaier oia.addr16[6], ia->addr16[6], u), 2170126258Smlaier oia.addr16[7], ia->addr16[7], u); 2171126258Smlaier break; 2172126258Smlaier#endif /* INET6 */ 2173126258Smlaier } 2174223637Sbz /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 2175223637Sbz if (oa) { 2176223637Sbz PF_ACPY(oa, na, af); 2177223637Sbz switch (af) { 2178126258Smlaier#ifdef INET 2179223637Sbz case AF_INET: 2180223637Sbz *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 2181223637Sbz ooa.addr16[0], oa->addr16[0], 0), 2182223637Sbz ooa.addr16[1], oa->addr16[1], 0); 2183223637Sbz break; 2184126258Smlaier#endif /* INET */ 2185126258Smlaier#ifdef INET6 2186223637Sbz case AF_INET6: 2187223637Sbz *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2188223637Sbz pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2189223637Sbz pf_cksum_fixup(pf_cksum_fixup(*ic, 2190223637Sbz ooa.addr16[0], oa->addr16[0], u), 2191223637Sbz ooa.addr16[1], oa->addr16[1], u), 2192223637Sbz ooa.addr16[2], oa->addr16[2], u), 2193223637Sbz ooa.addr16[3], oa->addr16[3], u), 2194223637Sbz ooa.addr16[4], oa->addr16[4], u), 2195223637Sbz ooa.addr16[5], oa->addr16[5], u), 2196223637Sbz ooa.addr16[6], oa->addr16[6], u), 2197223637Sbz ooa.addr16[7], oa->addr16[7], u); 2198223637Sbz break; 2199126258Smlaier#endif /* INET6 */ 2200223637Sbz } 2201126258Smlaier } 2202126258Smlaier} 2203126258Smlaier 2204171168Smlaier 2205171168Smlaier/* 2206171168Smlaier * Need to modulate the sequence numbers in the TCP SACK option 2207171168Smlaier * (credits to Krzysztof Pfaff for report and patch) 2208171168Smlaier */ 2209240233Sglebiusstatic int 2210171168Smlaierpf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd, 2211171168Smlaier struct tcphdr *th, struct pf_state_peer *dst) 2212171168Smlaier{ 2213171168Smlaier int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen; 2214171168Smlaier u_int8_t opts[TCP_MAXOLEN], *opt = opts; 2215171168Smlaier int copyback = 0, i, olen; 2216171168Smlaier struct sackblk sack; 2217171168Smlaier 2218223637Sbz#define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2) 2219171168Smlaier if (hlen < TCPOLEN_SACKLEN || 2220171168Smlaier !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af)) 2221171168Smlaier return 0; 2222171168Smlaier 2223171168Smlaier while (hlen >= TCPOLEN_SACKLEN) { 2224171168Smlaier olen = opt[1]; 2225171168Smlaier switch (*opt) { 2226171168Smlaier case TCPOPT_EOL: /* FALLTHROUGH */ 2227171168Smlaier case TCPOPT_NOP: 2228171168Smlaier opt++; 2229171168Smlaier hlen--; 2230171168Smlaier break; 2231171168Smlaier case TCPOPT_SACK: 2232171168Smlaier if (olen > hlen) 2233171168Smlaier olen = hlen; 2234171168Smlaier if (olen >= TCPOLEN_SACKLEN) { 2235171168Smlaier for (i = 2; i + TCPOLEN_SACK <= olen; 2236171168Smlaier i += TCPOLEN_SACK) { 2237171168Smlaier memcpy(&sack, &opt[i], sizeof(sack)); 2238293894Sglebius pf_change_proto_a(m, &sack.start, &th->th_sum, 2239293894Sglebius htonl(ntohl(sack.start) - dst->seqdiff), 0); 2240293894Sglebius pf_change_proto_a(m, &sack.end, &th->th_sum, 2241293894Sglebius htonl(ntohl(sack.end) - dst->seqdiff), 0); 2242171168Smlaier memcpy(&opt[i], &sack, sizeof(sack)); 2243171168Smlaier } 2244171168Smlaier copyback = 1; 2245171168Smlaier } 2246171168Smlaier /* FALLTHROUGH */ 2247171168Smlaier default: 2248171168Smlaier if (olen < 2) 2249171168Smlaier olen = 2; 2250171168Smlaier hlen -= olen; 2251171168Smlaier opt += olen; 2252171168Smlaier } 2253171168Smlaier } 2254171168Smlaier 2255171168Smlaier if (copyback) 2256171168Smlaier m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts); 2257171168Smlaier return (copyback); 2258171168Smlaier} 2259171168Smlaier 2260240233Sglebiusstatic void 2261162238Scsjppf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af, 2262126258Smlaier const struct pf_addr *saddr, const struct pf_addr *daddr, 2263126258Smlaier u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 2264145836Smlaier u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag, 2265240233Sglebius u_int16_t rtag, struct ifnet *ifp) 2266126258Smlaier{ 2267240233Sglebius struct pf_send_entry *pfse; 2268126258Smlaier struct mbuf *m; 2269171168Smlaier int len, tlen; 2270126258Smlaier#ifdef INET 2271240233Sglebius struct ip *h = NULL; 2272126258Smlaier#endif /* INET */ 2273126258Smlaier#ifdef INET6 2274240233Sglebius struct ip6_hdr *h6 = NULL; 2275126258Smlaier#endif /* INET6 */ 2276171168Smlaier struct tcphdr *th; 2277171168Smlaier char *opt; 2278223637Sbz struct pf_mtag *pf_mtag; 2279126258Smlaier 2280171168Smlaier len = 0; 2281171168Smlaier th = NULL; 2282171168Smlaier 2283126258Smlaier /* maximum segment size tcp option */ 2284126258Smlaier tlen = sizeof(struct tcphdr); 2285126258Smlaier if (mss) 2286126258Smlaier tlen += 4; 2287126258Smlaier 2288126258Smlaier switch (af) { 2289126258Smlaier#ifdef INET 2290126258Smlaier case AF_INET: 2291126258Smlaier len = sizeof(struct ip) + tlen; 2292126258Smlaier break; 2293126258Smlaier#endif /* INET */ 2294126258Smlaier#ifdef INET6 2295126258Smlaier case AF_INET6: 2296126258Smlaier len = sizeof(struct ip6_hdr) + tlen; 2297126258Smlaier break; 2298126258Smlaier#endif /* INET6 */ 2299240233Sglebius default: 2300240233Sglebius panic("%s: unsupported af %d", __func__, af); 2301126258Smlaier } 2302126258Smlaier 2303240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2304240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2305240233Sglebius if (pfse == NULL) 2306132280Smlaier return; 2307248324Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 2308240233Sglebius if (m == NULL) { 2309240233Sglebius free(pfse, M_PFTEMP); 2310240233Sglebius return; 2311240233Sglebius } 2312162238Scsjp#ifdef MAC 2313223637Sbz mac_netinet_firewall_send(m); 2314162238Scsjp#endif 2315171168Smlaier if ((pf_mtag = pf_get_mtag(m)) == NULL) { 2316240233Sglebius free(pfse, M_PFTEMP); 2317171168Smlaier m_freem(m); 2318171168Smlaier return; 2319171168Smlaier } 2320171168Smlaier if (tag) 2321145836Smlaier m->m_flags |= M_SKIP_FIREWALL; 2322223637Sbz pf_mtag->tag = rtag; 2323145836Smlaier 2324171168Smlaier if (r != NULL && r->rtableid >= 0) 2325178888Sjulian M_SETFIB(m, r->rtableid); 2326223637Sbz 2327126258Smlaier#ifdef ALTQ 2328126258Smlaier if (r != NULL && r->qid) { 2329171168Smlaier pf_mtag->qid = r->qid; 2330223637Sbz 2331171168Smlaier /* add hints for ecn */ 2332171168Smlaier pf_mtag->hdr = mtod(m, struct ip *); 2333126258Smlaier } 2334145836Smlaier#endif /* ALTQ */ 2335126258Smlaier m->m_data += max_linkhdr; 2336126258Smlaier m->m_pkthdr.len = m->m_len = len; 2337126258Smlaier m->m_pkthdr.rcvif = NULL; 2338126258Smlaier bzero(m->m_data, len); 2339126258Smlaier switch (af) { 2340126258Smlaier#ifdef INET 2341126258Smlaier case AF_INET: 2342126258Smlaier h = mtod(m, struct ip *); 2343126258Smlaier 2344126258Smlaier /* IP header fields included in the TCP checksum */ 2345126258Smlaier h->ip_p = IPPROTO_TCP; 2346126258Smlaier h->ip_len = htons(tlen); 2347126258Smlaier h->ip_src.s_addr = saddr->v4.s_addr; 2348126258Smlaier h->ip_dst.s_addr = daddr->v4.s_addr; 2349126258Smlaier 2350126258Smlaier th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 2351126258Smlaier break; 2352126258Smlaier#endif /* INET */ 2353126258Smlaier#ifdef INET6 2354126258Smlaier case AF_INET6: 2355126258Smlaier h6 = mtod(m, struct ip6_hdr *); 2356126258Smlaier 2357126258Smlaier /* IP header fields included in the TCP checksum */ 2358126258Smlaier h6->ip6_nxt = IPPROTO_TCP; 2359126258Smlaier h6->ip6_plen = htons(tlen); 2360126258Smlaier memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 2361126258Smlaier memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 2362126258Smlaier 2363126258Smlaier th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 2364126258Smlaier break; 2365126258Smlaier#endif /* INET6 */ 2366126258Smlaier } 2367126258Smlaier 2368126258Smlaier /* TCP header */ 2369126258Smlaier th->th_sport = sport; 2370126258Smlaier th->th_dport = dport; 2371126258Smlaier th->th_seq = htonl(seq); 2372126258Smlaier th->th_ack = htonl(ack); 2373126258Smlaier th->th_off = tlen >> 2; 2374126258Smlaier th->th_flags = flags; 2375126258Smlaier th->th_win = htons(win); 2376126258Smlaier 2377126258Smlaier if (mss) { 2378126258Smlaier opt = (char *)(th + 1); 2379126258Smlaier opt[0] = TCPOPT_MAXSEG; 2380126258Smlaier opt[1] = 4; 2381126258Smlaier HTONS(mss); 2382126258Smlaier bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2); 2383126258Smlaier } 2384126258Smlaier 2385126258Smlaier switch (af) { 2386126258Smlaier#ifdef INET 2387126258Smlaier case AF_INET: 2388126258Smlaier /* TCP checksum */ 2389126258Smlaier th->th_sum = in_cksum(m, len); 2390126258Smlaier 2391126258Smlaier /* Finish the IP header */ 2392126258Smlaier h->ip_v = 4; 2393126258Smlaier h->ip_hl = sizeof(*h) >> 2; 2394126258Smlaier h->ip_tos = IPTOS_LOWDELAY; 2395241913Sglebius h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 2396241913Sglebius h->ip_len = htons(len); 2397223637Sbz h->ip_ttl = ttl ? ttl : V_ip_defttl; 2398126258Smlaier h->ip_sum = 0; 2399145836Smlaier 2400240233Sglebius pfse->pfse_type = PFSE_IP; 2401126258Smlaier break; 2402126258Smlaier#endif /* INET */ 2403126258Smlaier#ifdef INET6 2404126258Smlaier case AF_INET6: 2405126258Smlaier /* TCP checksum */ 2406126258Smlaier th->th_sum = in6_cksum(m, IPPROTO_TCP, 2407126258Smlaier sizeof(struct ip6_hdr), tlen); 2408126258Smlaier 2409126258Smlaier h6->ip6_vfc |= IPV6_VERSION; 2410126258Smlaier h6->ip6_hlim = IPV6_DEFHLIM; 2411126258Smlaier 2412240233Sglebius pfse->pfse_type = PFSE_IP6; 2413126258Smlaier break; 2414126258Smlaier#endif /* INET6 */ 2415126258Smlaier } 2416240233Sglebius pfse->pfse_m = m; 2417240233Sglebius pf_send(pfse); 2418126258Smlaier} 2419126258Smlaier 2420223637Sbzstatic void 2421126258Smlaierpf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af, 2422126258Smlaier struct pf_rule *r) 2423126258Smlaier{ 2424240233Sglebius struct pf_send_entry *pfse; 2425240233Sglebius struct mbuf *m0; 2426223637Sbz struct pf_mtag *pf_mtag; 2427126258Smlaier 2428240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2429240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2430240233Sglebius if (pfse == NULL) 2431132280Smlaier return; 2432240233Sglebius 2433240233Sglebius if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 2434240233Sglebius free(pfse, M_PFTEMP); 2435223637Sbz return; 2436240233Sglebius } 2437223637Sbz 2438240233Sglebius if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 2439240233Sglebius free(pfse, M_PFTEMP); 2440126258Smlaier return; 2441240233Sglebius } 2442171168Smlaier /* XXX: revisit */ 2443171168Smlaier m0->m_flags |= M_SKIP_FIREWALL; 2444126258Smlaier 2445171168Smlaier if (r->rtableid >= 0) 2446178888Sjulian M_SETFIB(m0, r->rtableid); 2447171168Smlaier 2448126258Smlaier#ifdef ALTQ 2449126258Smlaier if (r->qid) { 2450171168Smlaier pf_mtag->qid = r->qid; 2451171168Smlaier /* add hints for ecn */ 2452171168Smlaier pf_mtag->hdr = mtod(m0, struct ip *); 2453126258Smlaier } 2454145836Smlaier#endif /* ALTQ */ 2455126258Smlaier 2456126258Smlaier switch (af) { 2457126258Smlaier#ifdef INET 2458126258Smlaier case AF_INET: 2459240233Sglebius pfse->pfse_type = PFSE_ICMP; 2460126258Smlaier break; 2461126258Smlaier#endif /* INET */ 2462126258Smlaier#ifdef INET6 2463126258Smlaier case AF_INET6: 2464240233Sglebius pfse->pfse_type = PFSE_ICMP6; 2465126258Smlaier break; 2466126258Smlaier#endif /* INET6 */ 2467126258Smlaier } 2468240233Sglebius pfse->pfse_m = m0; 2469240233Sglebius pfse->pfse_icmp_type = type; 2470240233Sglebius pfse->pfse_icmp_code = code; 2471240233Sglebius pf_send(pfse); 2472126258Smlaier} 2473126258Smlaier 2474126258Smlaier/* 2475126258Smlaier * Return 1 if the addresses a and b match (with mask m), otherwise return 0. 2476126258Smlaier * If n is 0, they match if they are equal. If n is != 0, they match if they 2477126258Smlaier * are different. 2478126258Smlaier */ 2479126258Smlaierint 2480126258Smlaierpf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, 2481126258Smlaier struct pf_addr *b, sa_family_t af) 2482126258Smlaier{ 2483126258Smlaier int match = 0; 2484126258Smlaier 2485126258Smlaier switch (af) { 2486126258Smlaier#ifdef INET 2487126258Smlaier case AF_INET: 2488126258Smlaier if ((a->addr32[0] & m->addr32[0]) == 2489126258Smlaier (b->addr32[0] & m->addr32[0])) 2490126258Smlaier match++; 2491126258Smlaier break; 2492126258Smlaier#endif /* INET */ 2493126258Smlaier#ifdef INET6 2494126258Smlaier case AF_INET6: 2495126258Smlaier if (((a->addr32[0] & m->addr32[0]) == 2496126258Smlaier (b->addr32[0] & m->addr32[0])) && 2497126258Smlaier ((a->addr32[1] & m->addr32[1]) == 2498126258Smlaier (b->addr32[1] & m->addr32[1])) && 2499126258Smlaier ((a->addr32[2] & m->addr32[2]) == 2500126258Smlaier (b->addr32[2] & m->addr32[2])) && 2501126258Smlaier ((a->addr32[3] & m->addr32[3]) == 2502126258Smlaier (b->addr32[3] & m->addr32[3]))) 2503126258Smlaier match++; 2504126258Smlaier break; 2505126258Smlaier#endif /* INET6 */ 2506126258Smlaier } 2507126258Smlaier if (match) { 2508126258Smlaier if (n) 2509126258Smlaier return (0); 2510126258Smlaier else 2511126258Smlaier return (1); 2512126258Smlaier } else { 2513126258Smlaier if (n) 2514126258Smlaier return (1); 2515126258Smlaier else 2516126258Smlaier return (0); 2517126258Smlaier } 2518126258Smlaier} 2519126258Smlaier 2520223637Sbz/* 2521223637Sbz * Return 1 if b <= a <= e, otherwise return 0. 2522223637Sbz */ 2523126258Smlaierint 2524223637Sbzpf_match_addr_range(struct pf_addr *b, struct pf_addr *e, 2525223637Sbz struct pf_addr *a, sa_family_t af) 2526223637Sbz{ 2527223637Sbz switch (af) { 2528223637Sbz#ifdef INET 2529223637Sbz case AF_INET: 2530223637Sbz if ((a->addr32[0] < b->addr32[0]) || 2531223637Sbz (a->addr32[0] > e->addr32[0])) 2532223637Sbz return (0); 2533223637Sbz break; 2534223637Sbz#endif /* INET */ 2535223637Sbz#ifdef INET6 2536223637Sbz case AF_INET6: { 2537223637Sbz int i; 2538223637Sbz 2539223637Sbz /* check a >= b */ 2540223637Sbz for (i = 0; i < 4; ++i) 2541223637Sbz if (a->addr32[i] > b->addr32[i]) 2542223637Sbz break; 2543223637Sbz else if (a->addr32[i] < b->addr32[i]) 2544223637Sbz return (0); 2545223637Sbz /* check a <= e */ 2546223637Sbz for (i = 0; i < 4; ++i) 2547223637Sbz if (a->addr32[i] < e->addr32[i]) 2548223637Sbz break; 2549223637Sbz else if (a->addr32[i] > e->addr32[i]) 2550223637Sbz return (0); 2551223637Sbz break; 2552223637Sbz } 2553223637Sbz#endif /* INET6 */ 2554223637Sbz } 2555223637Sbz return (1); 2556223637Sbz} 2557223637Sbz 2558240233Sglebiusstatic int 2559126258Smlaierpf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 2560126258Smlaier{ 2561126258Smlaier switch (op) { 2562126258Smlaier case PF_OP_IRG: 2563126258Smlaier return ((p > a1) && (p < a2)); 2564126258Smlaier case PF_OP_XRG: 2565126258Smlaier return ((p < a1) || (p > a2)); 2566126258Smlaier case PF_OP_RRG: 2567126258Smlaier return ((p >= a1) && (p <= a2)); 2568126258Smlaier case PF_OP_EQ: 2569126258Smlaier return (p == a1); 2570126258Smlaier case PF_OP_NE: 2571126258Smlaier return (p != a1); 2572126258Smlaier case PF_OP_LT: 2573126258Smlaier return (p < a1); 2574126258Smlaier case PF_OP_LE: 2575126258Smlaier return (p <= a1); 2576126258Smlaier case PF_OP_GT: 2577126258Smlaier return (p > a1); 2578126258Smlaier case PF_OP_GE: 2579126258Smlaier return (p >= a1); 2580126258Smlaier } 2581126258Smlaier return (0); /* never reached */ 2582126258Smlaier} 2583126258Smlaier 2584126258Smlaierint 2585126258Smlaierpf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 2586126258Smlaier{ 2587126258Smlaier NTOHS(a1); 2588126258Smlaier NTOHS(a2); 2589126258Smlaier NTOHS(p); 2590126258Smlaier return (pf_match(op, a1, a2, p)); 2591126258Smlaier} 2592126258Smlaier 2593240233Sglebiusstatic int 2594126258Smlaierpf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 2595126258Smlaier{ 2596126258Smlaier if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2597126258Smlaier return (0); 2598126258Smlaier return (pf_match(op, a1, a2, u)); 2599126258Smlaier} 2600126258Smlaier 2601240233Sglebiusstatic int 2602126258Smlaierpf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 2603126258Smlaier{ 2604126258Smlaier if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2605126258Smlaier return (0); 2606126258Smlaier return (pf_match(op, a1, a2, g)); 2607126258Smlaier} 2608126258Smlaier 2609223637Sbzint 2610240233Sglebiuspf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag, int mtag) 2611171168Smlaier{ 2612171168Smlaier if (*tag == -1) 2613240233Sglebius *tag = mtag; 2614171168Smlaier 2615126258Smlaier return ((!r->match_tag_not && r->match_tag == *tag) || 2616126258Smlaier (r->match_tag_not && r->match_tag != *tag)); 2617126258Smlaier} 2618126258Smlaier 2619126258Smlaierint 2620240233Sglebiuspf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag) 2621126258Smlaier{ 2622126258Smlaier 2623240233Sglebius KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 2624126258Smlaier 2625240233Sglebius if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL)) 2626240233Sglebius return (ENOMEM); 2627240233Sglebius 2628240233Sglebius pd->pf_mtag->tag = tag; 2629240233Sglebius 2630126258Smlaier return (0); 2631126258Smlaier} 2632126258Smlaier 2633240641Sglebius#define PF_ANCHOR_STACKSIZE 32 2634240641Sglebiusstruct pf_anchor_stackframe { 2635240641Sglebius struct pf_ruleset *rs; 2636240641Sglebius struct pf_rule *r; /* XXX: + match bit */ 2637240641Sglebius struct pf_anchor *child; 2638240641Sglebius}; 2639240641Sglebius 2640240641Sglebius/* 2641240641Sglebius * XXX: We rely on malloc(9) returning pointer aligned addresses. 2642240641Sglebius */ 2643240641Sglebius#define PF_ANCHORSTACK_MATCH 0x00000001 2644240641Sglebius#define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 2645240641Sglebius 2646240641Sglebius#define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 2647240641Sglebius#define PF_ANCHOR_RULE(f) (struct pf_rule *) \ 2648240641Sglebius ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 2649240641Sglebius#define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 2650240641Sglebius ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 2651240641Sglebius} while (0) 2652240641Sglebius 2653223637Sbzvoid 2654240641Sglebiuspf_step_into_anchor(struct pf_anchor_stackframe *stack, int *depth, 2655240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2656240641Sglebius int *match) 2657145836Smlaier{ 2658145836Smlaier struct pf_anchor_stackframe *f; 2659126258Smlaier 2660240233Sglebius PF_RULES_RASSERT(); 2661240233Sglebius 2662171168Smlaier if (match) 2663171168Smlaier *match = 0; 2664240641Sglebius if (*depth >= PF_ANCHOR_STACKSIZE) { 2665240641Sglebius printf("%s: anchor stack overflow on %s\n", 2666240641Sglebius __func__, (*r)->anchor->name); 2667145836Smlaier *r = TAILQ_NEXT(*r, entries); 2668145836Smlaier return; 2669145836Smlaier } else if (*depth == 0 && a != NULL) 2670145836Smlaier *a = *r; 2671240641Sglebius f = stack + (*depth)++; 2672145836Smlaier f->rs = *rs; 2673145836Smlaier f->r = *r; 2674145836Smlaier if ((*r)->anchor_wildcard) { 2675240641Sglebius struct pf_anchor_node *parent = &(*r)->anchor->children; 2676240641Sglebius 2677240641Sglebius if ((f->child = RB_MIN(pf_anchor_node, parent)) == NULL) { 2678145836Smlaier *r = NULL; 2679145836Smlaier return; 2680145836Smlaier } 2681145836Smlaier *rs = &f->child->ruleset; 2682145836Smlaier } else { 2683145836Smlaier f->child = NULL; 2684145836Smlaier *rs = &(*r)->anchor->ruleset; 2685145836Smlaier } 2686145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2687145836Smlaier} 2688126258Smlaier 2689171168Smlaierint 2690240641Sglebiuspf_step_out_of_anchor(struct pf_anchor_stackframe *stack, int *depth, 2691240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2692240641Sglebius int *match) 2693145836Smlaier{ 2694145836Smlaier struct pf_anchor_stackframe *f; 2695240641Sglebius struct pf_rule *fr; 2696171168Smlaier int quick = 0; 2697145836Smlaier 2698240233Sglebius PF_RULES_RASSERT(); 2699240233Sglebius 2700145836Smlaier do { 2701145836Smlaier if (*depth <= 0) 2702145836Smlaier break; 2703240641Sglebius f = stack + *depth - 1; 2704240641Sglebius fr = PF_ANCHOR_RULE(f); 2705240641Sglebius if (f->child != NULL) { 2706240641Sglebius struct pf_anchor_node *parent; 2707240641Sglebius 2708240641Sglebius /* 2709240641Sglebius * This block traverses through 2710240641Sglebius * a wildcard anchor. 2711240641Sglebius */ 2712240641Sglebius parent = &fr->anchor->children; 2713240641Sglebius if (match != NULL && *match) { 2714240641Sglebius /* 2715240641Sglebius * If any of "*" matched, then 2716240641Sglebius * "foo/ *" matched, mark frame 2717240641Sglebius * appropriately. 2718240641Sglebius */ 2719240641Sglebius PF_ANCHOR_SET_MATCH(f); 2720171168Smlaier *match = 0; 2721171168Smlaier } 2722240641Sglebius f->child = RB_NEXT(pf_anchor_node, parent, f->child); 2723145836Smlaier if (f->child != NULL) { 2724145836Smlaier *rs = &f->child->ruleset; 2725145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2726145836Smlaier if (*r == NULL) 2727145836Smlaier continue; 2728145836Smlaier else 2729145836Smlaier break; 2730145836Smlaier } 2731145836Smlaier } 2732145836Smlaier (*depth)--; 2733145836Smlaier if (*depth == 0 && a != NULL) 2734145836Smlaier *a = NULL; 2735145836Smlaier *rs = f->rs; 2736240641Sglebius if (PF_ANCHOR_MATCH(f) || (match != NULL && *match)) 2737240641Sglebius quick = fr->quick; 2738240641Sglebius *r = TAILQ_NEXT(fr, entries); 2739145836Smlaier } while (*r == NULL); 2740171168Smlaier 2741171168Smlaier return (quick); 2742145836Smlaier} 2743145836Smlaier 2744126258Smlaier#ifdef INET6 2745126258Smlaiervoid 2746126258Smlaierpf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 2747126258Smlaier struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 2748126258Smlaier{ 2749126258Smlaier switch (af) { 2750126258Smlaier#ifdef INET 2751126258Smlaier case AF_INET: 2752126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2753126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2754126258Smlaier break; 2755126258Smlaier#endif /* INET */ 2756126258Smlaier case AF_INET6: 2757126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2758126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2759126258Smlaier naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 2760126258Smlaier ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 2761126258Smlaier naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 2762126258Smlaier ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 2763126258Smlaier naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 2764126258Smlaier ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 2765126258Smlaier break; 2766126258Smlaier } 2767126258Smlaier} 2768126258Smlaier 2769126258Smlaiervoid 2770130613Smlaierpf_addr_inc(struct pf_addr *addr, sa_family_t af) 2771126258Smlaier{ 2772126258Smlaier switch (af) { 2773126258Smlaier#ifdef INET 2774126258Smlaier case AF_INET: 2775126258Smlaier addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 2776126258Smlaier break; 2777126258Smlaier#endif /* INET */ 2778126258Smlaier case AF_INET6: 2779126258Smlaier if (addr->addr32[3] == 0xffffffff) { 2780126258Smlaier addr->addr32[3] = 0; 2781126258Smlaier if (addr->addr32[2] == 0xffffffff) { 2782126258Smlaier addr->addr32[2] = 0; 2783126258Smlaier if (addr->addr32[1] == 0xffffffff) { 2784126258Smlaier addr->addr32[1] = 0; 2785126258Smlaier addr->addr32[0] = 2786126258Smlaier htonl(ntohl(addr->addr32[0]) + 1); 2787126258Smlaier } else 2788126258Smlaier addr->addr32[1] = 2789126258Smlaier htonl(ntohl(addr->addr32[1]) + 1); 2790126258Smlaier } else 2791126258Smlaier addr->addr32[2] = 2792126258Smlaier htonl(ntohl(addr->addr32[2]) + 1); 2793126258Smlaier } else 2794126258Smlaier addr->addr32[3] = 2795126258Smlaier htonl(ntohl(addr->addr32[3]) + 1); 2796126258Smlaier break; 2797126258Smlaier } 2798126258Smlaier} 2799126258Smlaier#endif /* INET6 */ 2800126258Smlaier 2801126258Smlaierint 2802240233Sglebiuspf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m) 2803126258Smlaier{ 2804126258Smlaier struct pf_addr *saddr, *daddr; 2805126258Smlaier u_int16_t sport, dport; 2806126261Smlaier struct inpcbinfo *pi; 2807126258Smlaier struct inpcb *inp; 2808126258Smlaier 2809171168Smlaier pd->lookup.uid = UID_MAX; 2810171168Smlaier pd->lookup.gid = GID_MAX; 2811223637Sbz 2812130613Smlaier switch (pd->proto) { 2813126258Smlaier case IPPROTO_TCP: 2814171168Smlaier if (pd->hdr.tcp == NULL) 2815171168Smlaier return (-1); 2816126258Smlaier sport = pd->hdr.tcp->th_sport; 2817126258Smlaier dport = pd->hdr.tcp->th_dport; 2818181803Sbz pi = &V_tcbinfo; 2819126258Smlaier break; 2820126258Smlaier case IPPROTO_UDP: 2821171168Smlaier if (pd->hdr.udp == NULL) 2822171168Smlaier return (-1); 2823126258Smlaier sport = pd->hdr.udp->uh_sport; 2824126258Smlaier dport = pd->hdr.udp->uh_dport; 2825181803Sbz pi = &V_udbinfo; 2826126258Smlaier break; 2827126258Smlaier default: 2828171168Smlaier return (-1); 2829126258Smlaier } 2830126258Smlaier if (direction == PF_IN) { 2831126258Smlaier saddr = pd->src; 2832126258Smlaier daddr = pd->dst; 2833126258Smlaier } else { 2834126258Smlaier u_int16_t p; 2835126258Smlaier 2836126258Smlaier p = sport; 2837126258Smlaier sport = dport; 2838126258Smlaier dport = p; 2839126258Smlaier saddr = pd->dst; 2840126258Smlaier daddr = pd->src; 2841126258Smlaier } 2842130613Smlaier switch (pd->af) { 2843145836Smlaier#ifdef INET 2844126258Smlaier case AF_INET: 2845240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 2846240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2847126261Smlaier if (inp == NULL) { 2848240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 2849222488Srwatson daddr->v4, dport, INPLOOKUP_WILDCARD | 2850240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2851222488Srwatson if (inp == NULL) 2852171168Smlaier return (-1); 2853126261Smlaier } 2854126258Smlaier break; 2855145836Smlaier#endif /* INET */ 2856126258Smlaier#ifdef INET6 2857126258Smlaier case AF_INET6: 2858240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 2859240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2860126261Smlaier if (inp == NULL) { 2861240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 2862222488Srwatson &daddr->v6, dport, INPLOOKUP_WILDCARD | 2863240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2864222488Srwatson if (inp == NULL) 2865171168Smlaier return (-1); 2866126261Smlaier } 2867126258Smlaier break; 2868126258Smlaier#endif /* INET6 */ 2869126258Smlaier 2870126258Smlaier default: 2871171168Smlaier return (-1); 2872126258Smlaier } 2873222488Srwatson INP_RLOCK_ASSERT(inp); 2874183606Sbz pd->lookup.uid = inp->inp_cred->cr_uid; 2875183606Sbz pd->lookup.gid = inp->inp_cred->cr_groups[0]; 2876222488Srwatson INP_RUNLOCK(inp); 2877240233Sglebius 2878126258Smlaier return (1); 2879126258Smlaier} 2880126258Smlaier 2881240233Sglebiusstatic u_int8_t 2882126258Smlaierpf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2883126258Smlaier{ 2884126258Smlaier int hlen; 2885126258Smlaier u_int8_t hdr[60]; 2886126258Smlaier u_int8_t *opt, optlen; 2887126258Smlaier u_int8_t wscale = 0; 2888126258Smlaier 2889126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2890126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2891126258Smlaier return (0); 2892126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2893126258Smlaier return (0); 2894126258Smlaier opt = hdr + sizeof(struct tcphdr); 2895126258Smlaier hlen -= sizeof(struct tcphdr); 2896126258Smlaier while (hlen >= 3) { 2897126258Smlaier switch (*opt) { 2898126258Smlaier case TCPOPT_EOL: 2899126258Smlaier case TCPOPT_NOP: 2900126258Smlaier ++opt; 2901126258Smlaier --hlen; 2902126258Smlaier break; 2903126258Smlaier case TCPOPT_WINDOW: 2904126258Smlaier wscale = opt[2]; 2905126258Smlaier if (wscale > TCP_MAX_WINSHIFT) 2906126258Smlaier wscale = TCP_MAX_WINSHIFT; 2907126258Smlaier wscale |= PF_WSCALE_FLAG; 2908130613Smlaier /* FALLTHROUGH */ 2909126258Smlaier default: 2910126258Smlaier optlen = opt[1]; 2911126258Smlaier if (optlen < 2) 2912126258Smlaier optlen = 2; 2913126258Smlaier hlen -= optlen; 2914126258Smlaier opt += optlen; 2915130613Smlaier break; 2916126258Smlaier } 2917126258Smlaier } 2918126258Smlaier return (wscale); 2919126258Smlaier} 2920126258Smlaier 2921240233Sglebiusstatic u_int16_t 2922126258Smlaierpf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2923126258Smlaier{ 2924126258Smlaier int hlen; 2925126258Smlaier u_int8_t hdr[60]; 2926126258Smlaier u_int8_t *opt, optlen; 2927181803Sbz u_int16_t mss = V_tcp_mssdflt; 2928126258Smlaier 2929126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2930126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2931126258Smlaier return (0); 2932126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2933126258Smlaier return (0); 2934126258Smlaier opt = hdr + sizeof(struct tcphdr); 2935126258Smlaier hlen -= sizeof(struct tcphdr); 2936126258Smlaier while (hlen >= TCPOLEN_MAXSEG) { 2937126258Smlaier switch (*opt) { 2938126258Smlaier case TCPOPT_EOL: 2939126258Smlaier case TCPOPT_NOP: 2940126258Smlaier ++opt; 2941126258Smlaier --hlen; 2942126258Smlaier break; 2943126258Smlaier case TCPOPT_MAXSEG: 2944126258Smlaier bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2); 2945145030Sglebius NTOHS(mss); 2946130613Smlaier /* FALLTHROUGH */ 2947126258Smlaier default: 2948126258Smlaier optlen = opt[1]; 2949126258Smlaier if (optlen < 2) 2950126258Smlaier optlen = 2; 2951126258Smlaier hlen -= optlen; 2952126258Smlaier opt += optlen; 2953130613Smlaier break; 2954126258Smlaier } 2955126258Smlaier } 2956126258Smlaier return (mss); 2957126258Smlaier} 2958126258Smlaier 2959240233Sglebiusstatic u_int16_t 2960231852Sbzpf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 2961126258Smlaier{ 2962126258Smlaier#ifdef INET 2963126258Smlaier struct sockaddr_in *dst; 2964126258Smlaier struct route ro; 2965126258Smlaier#endif /* INET */ 2966126258Smlaier#ifdef INET6 2967126258Smlaier struct sockaddr_in6 *dst6; 2968126258Smlaier struct route_in6 ro6; 2969126258Smlaier#endif /* INET6 */ 2970126258Smlaier struct rtentry *rt = NULL; 2971223637Sbz int hlen = 0; 2972181803Sbz u_int16_t mss = V_tcp_mssdflt; 2973126258Smlaier 2974126258Smlaier switch (af) { 2975126258Smlaier#ifdef INET 2976126258Smlaier case AF_INET: 2977126258Smlaier hlen = sizeof(struct ip); 2978126258Smlaier bzero(&ro, sizeof(ro)); 2979126258Smlaier dst = (struct sockaddr_in *)&ro.ro_dst; 2980126258Smlaier dst->sin_family = AF_INET; 2981126258Smlaier dst->sin_len = sizeof(*dst); 2982126258Smlaier dst->sin_addr = addr->v4; 2983231852Sbz in_rtalloc_ign(&ro, 0, rtableid); 2984126258Smlaier rt = ro.ro_rt; 2985126258Smlaier break; 2986126258Smlaier#endif /* INET */ 2987126258Smlaier#ifdef INET6 2988126258Smlaier case AF_INET6: 2989126258Smlaier hlen = sizeof(struct ip6_hdr); 2990126258Smlaier bzero(&ro6, sizeof(ro6)); 2991126258Smlaier dst6 = (struct sockaddr_in6 *)&ro6.ro_dst; 2992126258Smlaier dst6->sin6_family = AF_INET6; 2993126258Smlaier dst6->sin6_len = sizeof(*dst6); 2994126258Smlaier dst6->sin6_addr = addr->v6; 2995231852Sbz in6_rtalloc_ign(&ro6, 0, rtableid); 2996126258Smlaier rt = ro6.ro_rt; 2997126258Smlaier break; 2998126258Smlaier#endif /* INET6 */ 2999126258Smlaier } 3000126258Smlaier 3001126258Smlaier if (rt && rt->rt_ifp) { 3002126258Smlaier mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr); 3003181803Sbz mss = max(V_tcp_mssdflt, mss); 3004126258Smlaier RTFREE(rt); 3005126258Smlaier } 3006126258Smlaier mss = min(mss, offer); 3007126258Smlaier mss = max(mss, 64); /* sanity - at least max opt space */ 3008126258Smlaier return (mss); 3009126258Smlaier} 3010126258Smlaier 3011240233Sglebiusstatic u_int32_t 3012223637Sbzpf_tcp_iss(struct pf_pdesc *pd) 3013223637Sbz{ 3014223637Sbz MD5_CTX ctx; 3015223637Sbz u_int32_t digest[4]; 3016223637Sbz 3017223637Sbz if (V_pf_tcp_secret_init == 0) { 3018223637Sbz read_random(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 3019223637Sbz MD5Init(&V_pf_tcp_secret_ctx); 3020223637Sbz MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 3021223637Sbz sizeof(V_pf_tcp_secret)); 3022223637Sbz V_pf_tcp_secret_init = 1; 3023223637Sbz } 3024223637Sbz 3025223637Sbz ctx = V_pf_tcp_secret_ctx; 3026223637Sbz 3027223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short)); 3028223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short)); 3029223637Sbz if (pd->af == AF_INET6) { 3030223637Sbz MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr)); 3031223637Sbz MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr)); 3032223637Sbz } else { 3033223637Sbz MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr)); 3034223637Sbz MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr)); 3035223637Sbz } 3036223637Sbz MD5Final((u_char *)digest, &ctx); 3037223637Sbz V_pf_tcp_iss_off += 4096; 3038223637Sbz#define ISN_RANDOM_INCREMENT (4096 - 1) 3039223637Sbz return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 3040223637Sbz V_pf_tcp_iss_off); 3041223637Sbz#undef ISN_RANDOM_INCREMENT 3042223637Sbz} 3043223637Sbz 3044240233Sglebiusstatic int 3045223637Sbzpf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction, 3046240233Sglebius struct pfi_kif *kif, struct mbuf *m, int off, struct pf_pdesc *pd, 3047240233Sglebius struct pf_rule **am, struct pf_ruleset **rsm, struct inpcb *inp) 3048126258Smlaier{ 3049130613Smlaier struct pf_rule *nr = NULL; 3050240233Sglebius struct pf_addr * const saddr = pd->src; 3051240233Sglebius struct pf_addr * const daddr = pd->dst; 3052126258Smlaier sa_family_t af = pd->af; 3053126258Smlaier struct pf_rule *r, *a = NULL; 3054126258Smlaier struct pf_ruleset *ruleset = NULL; 3055130613Smlaier struct pf_src_node *nsn = NULL; 3056223637Sbz struct tcphdr *th = pd->hdr.tcp; 3057223637Sbz struct pf_state_key *sk = NULL, *nk = NULL; 3058126258Smlaier u_short reason; 3059223637Sbz int rewrite = 0, hdrlen = 0; 3060171168Smlaier int tag = -1, rtableid = -1; 3061145836Smlaier int asd = 0; 3062171168Smlaier int match = 0; 3063223637Sbz int state_icmp = 0; 3064223637Sbz u_int16_t sport = 0, dport = 0; 3065223637Sbz u_int16_t bproto_sum = 0, bip_sum = 0; 3066223637Sbz u_int8_t icmptype = 0, icmpcode = 0; 3067240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3068126258Smlaier 3069240233Sglebius PF_RULES_RASSERT(); 3070223637Sbz 3071240233Sglebius if (inp != NULL) { 3072240233Sglebius INP_LOCK_ASSERT(inp); 3073240233Sglebius pd->lookup.uid = inp->inp_cred->cr_uid; 3074240233Sglebius pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3075240233Sglebius pd->lookup.done = 1; 3076145836Smlaier } 3077145836Smlaier 3078223637Sbz switch (pd->proto) { 3079223637Sbz case IPPROTO_TCP: 3080223637Sbz sport = th->th_sport; 3081223637Sbz dport = th->th_dport; 3082223637Sbz hdrlen = sizeof(*th); 3083223637Sbz break; 3084223637Sbz case IPPROTO_UDP: 3085223637Sbz sport = pd->hdr.udp->uh_sport; 3086223637Sbz dport = pd->hdr.udp->uh_dport; 3087223637Sbz hdrlen = sizeof(*pd->hdr.udp); 3088223637Sbz break; 3089223637Sbz#ifdef INET 3090223637Sbz case IPPROTO_ICMP: 3091223637Sbz if (pd->af != AF_INET) 3092223637Sbz break; 3093223637Sbz sport = dport = pd->hdr.icmp->icmp_id; 3094223637Sbz hdrlen = sizeof(*pd->hdr.icmp); 3095223637Sbz icmptype = pd->hdr.icmp->icmp_type; 3096223637Sbz icmpcode = pd->hdr.icmp->icmp_code; 3097223637Sbz 3098223637Sbz if (icmptype == ICMP_UNREACH || 3099223637Sbz icmptype == ICMP_SOURCEQUENCH || 3100223637Sbz icmptype == ICMP_REDIRECT || 3101223637Sbz icmptype == ICMP_TIMXCEED || 3102223637Sbz icmptype == ICMP_PARAMPROB) 3103223637Sbz state_icmp++; 3104223637Sbz break; 3105223637Sbz#endif /* INET */ 3106223637Sbz#ifdef INET6 3107223637Sbz case IPPROTO_ICMPV6: 3108223637Sbz if (af != AF_INET6) 3109223637Sbz break; 3110223637Sbz sport = dport = pd->hdr.icmp6->icmp6_id; 3111223637Sbz hdrlen = sizeof(*pd->hdr.icmp6); 3112223637Sbz icmptype = pd->hdr.icmp6->icmp6_type; 3113223637Sbz icmpcode = pd->hdr.icmp6->icmp6_code; 3114223637Sbz 3115223637Sbz if (icmptype == ICMP6_DST_UNREACH || 3116223637Sbz icmptype == ICMP6_PACKET_TOO_BIG || 3117223637Sbz icmptype == ICMP6_TIME_EXCEEDED || 3118223637Sbz icmptype == ICMP6_PARAM_PROB) 3119223637Sbz state_icmp++; 3120223637Sbz break; 3121223637Sbz#endif /* INET6 */ 3122223637Sbz default: 3123223637Sbz sport = dport = hdrlen = 0; 3124223637Sbz break; 3125223637Sbz } 3126223637Sbz 3127126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3128126258Smlaier 3129223637Sbz /* check packet for BINAT/NAT/RDR */ 3130240233Sglebius if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk, 3131240641Sglebius &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) { 3132240233Sglebius KASSERT(sk != NULL, ("%s: null sk", __func__)); 3133240233Sglebius KASSERT(nk != NULL, ("%s: null nk", __func__)); 3134223637Sbz 3135223637Sbz if (pd->ip_sum) 3136223637Sbz bip_sum = *pd->ip_sum; 3137223637Sbz 3138223637Sbz switch (pd->proto) { 3139223637Sbz case IPPROTO_TCP: 3140223637Sbz bproto_sum = th->th_sum; 3141223637Sbz pd->proto_sum = &th->th_sum; 3142223637Sbz 3143223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3144223637Sbz nk->port[pd->sidx] != sport) { 3145293894Sglebius pf_change_ap(m, saddr, &th->th_sport, pd->ip_sum, 3146223637Sbz &th->th_sum, &nk->addr[pd->sidx], 3147223637Sbz nk->port[pd->sidx], 0, af); 3148223637Sbz pd->sport = &th->th_sport; 3149223637Sbz sport = th->th_sport; 3150223637Sbz } 3151223637Sbz 3152223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3153223637Sbz nk->port[pd->didx] != dport) { 3154293894Sglebius pf_change_ap(m, daddr, &th->th_dport, pd->ip_sum, 3155223637Sbz &th->th_sum, &nk->addr[pd->didx], 3156223637Sbz nk->port[pd->didx], 0, af); 3157223637Sbz dport = th->th_dport; 3158223637Sbz pd->dport = &th->th_dport; 3159223637Sbz } 3160126258Smlaier rewrite++; 3161223637Sbz break; 3162223637Sbz case IPPROTO_UDP: 3163223637Sbz bproto_sum = pd->hdr.udp->uh_sum; 3164223637Sbz pd->proto_sum = &pd->hdr.udp->uh_sum; 3165223637Sbz 3166223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3167223637Sbz nk->port[pd->sidx] != sport) { 3168293894Sglebius pf_change_ap(m, saddr, &pd->hdr.udp->uh_sport, 3169223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3170223637Sbz &nk->addr[pd->sidx], 3171223637Sbz nk->port[pd->sidx], 1, af); 3172223637Sbz sport = pd->hdr.udp->uh_sport; 3173223637Sbz pd->sport = &pd->hdr.udp->uh_sport; 3174223637Sbz } 3175223637Sbz 3176223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3177223637Sbz nk->port[pd->didx] != dport) { 3178293894Sglebius pf_change_ap(m, daddr, &pd->hdr.udp->uh_dport, 3179223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3180223637Sbz &nk->addr[pd->didx], 3181223637Sbz nk->port[pd->didx], 1, af); 3182223637Sbz dport = pd->hdr.udp->uh_dport; 3183223637Sbz pd->dport = &pd->hdr.udp->uh_dport; 3184223637Sbz } 3185223637Sbz rewrite++; 3186223637Sbz break; 3187223637Sbz#ifdef INET 3188223637Sbz case IPPROTO_ICMP: 3189223637Sbz nk->port[0] = nk->port[1]; 3190223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET)) 3191223637Sbz pf_change_a(&saddr->v4.s_addr, pd->ip_sum, 3192223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3193223637Sbz 3194223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET)) 3195223637Sbz pf_change_a(&daddr->v4.s_addr, pd->ip_sum, 3196223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3197223637Sbz 3198223637Sbz if (nk->port[1] != pd->hdr.icmp->icmp_id) { 3199223637Sbz pd->hdr.icmp->icmp_cksum = pf_cksum_fixup( 3200223637Sbz pd->hdr.icmp->icmp_cksum, sport, 3201223637Sbz nk->port[1], 0); 3202223637Sbz pd->hdr.icmp->icmp_id = nk->port[1]; 3203223637Sbz pd->sport = &pd->hdr.icmp->icmp_id; 3204223637Sbz } 3205223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 3206223637Sbz break; 3207223637Sbz#endif /* INET */ 3208223637Sbz#ifdef INET6 3209223637Sbz case IPPROTO_ICMPV6: 3210223637Sbz nk->port[0] = nk->port[1]; 3211223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6)) 3212223637Sbz pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum, 3213223637Sbz &nk->addr[pd->sidx], 0); 3214223637Sbz 3215223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6)) 3216223637Sbz pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum, 3217223637Sbz &nk->addr[pd->didx], 0); 3218223637Sbz rewrite++; 3219223637Sbz break; 3220223637Sbz#endif /* INET */ 3221223637Sbz default: 3222223637Sbz switch (af) { 3223223637Sbz#ifdef INET 3224223637Sbz case AF_INET: 3225223637Sbz if (PF_ANEQ(saddr, 3226223637Sbz &nk->addr[pd->sidx], AF_INET)) 3227223637Sbz pf_change_a(&saddr->v4.s_addr, 3228223637Sbz pd->ip_sum, 3229223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3230223637Sbz 3231223637Sbz if (PF_ANEQ(daddr, 3232223637Sbz &nk->addr[pd->didx], AF_INET)) 3233223637Sbz pf_change_a(&daddr->v4.s_addr, 3234223637Sbz pd->ip_sum, 3235223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3236223637Sbz break; 3237223637Sbz#endif /* INET */ 3238223637Sbz#ifdef INET6 3239223637Sbz case AF_INET6: 3240223637Sbz if (PF_ANEQ(saddr, 3241223637Sbz &nk->addr[pd->sidx], AF_INET6)) 3242223637Sbz PF_ACPY(saddr, &nk->addr[pd->sidx], af); 3243223637Sbz 3244223637Sbz if (PF_ANEQ(daddr, 3245223637Sbz &nk->addr[pd->didx], AF_INET6)) 3246223637Sbz PF_ACPY(saddr, &nk->addr[pd->didx], af); 3247223637Sbz break; 3248223637Sbz#endif /* INET */ 3249223637Sbz } 3250223637Sbz break; 3251126258Smlaier } 3252223637Sbz if (nr->natpass) 3253223637Sbz r = NULL; 3254223637Sbz pd->nat_rule = nr; 3255126258Smlaier } 3256126258Smlaier 3257126258Smlaier while (r != NULL) { 3258126258Smlaier r->evaluations++; 3259171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3260126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3261126258Smlaier else if (r->direction && r->direction != direction) 3262126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3263126258Smlaier else if (r->af && r->af != af) 3264126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3265223637Sbz else if (r->proto && r->proto != pd->proto) 3266126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3267171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, saddr, af, 3268231852Sbz r->src.neg, kif, M_GETFIB(m))) 3269126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3270223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3271126258Smlaier else if (r->src.port_op && !pf_match_port(r->src.port_op, 3272223637Sbz r->src.port[0], r->src.port[1], sport)) 3273126258Smlaier r = r->skip[PF_SKIP_SRC_PORT].ptr; 3274171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, 3275231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3276126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3277223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3278126258Smlaier else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 3279223637Sbz r->dst.port[0], r->dst.port[1], dport)) 3280126258Smlaier r = r->skip[PF_SKIP_DST_PORT].ptr; 3281223637Sbz /* icmp only. type always 0 in other cases */ 3282223637Sbz else if (r->type && r->type != icmptype + 1) 3283223637Sbz r = TAILQ_NEXT(r, entries); 3284223637Sbz /* icmp only. type always 0 in other cases */ 3285223637Sbz else if (r->code && r->code != icmpcode + 1) 3286223637Sbz r = TAILQ_NEXT(r, entries); 3287171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3288126258Smlaier r = TAILQ_NEXT(r, entries); 3289126258Smlaier else if (r->rule_flag & PFRULE_FRAGMENT) 3290126258Smlaier r = TAILQ_NEXT(r, entries); 3291223637Sbz else if (pd->proto == IPPROTO_TCP && 3292223637Sbz (r->flagset & th->th_flags) != r->flags) 3293126258Smlaier r = TAILQ_NEXT(r, entries); 3294223637Sbz /* tcp/udp only. uid.op always 0 in other cases */ 3295171168Smlaier else if (r->uid.op && (pd->lookup.done || (pd->lookup.done = 3296240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3297126258Smlaier !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 3298171168Smlaier pd->lookup.uid)) 3299126258Smlaier r = TAILQ_NEXT(r, entries); 3300223637Sbz /* tcp/udp only. gid.op always 0 in other cases */ 3301171168Smlaier else if (r->gid.op && (pd->lookup.done || (pd->lookup.done = 3302240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3303126258Smlaier !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 3304171168Smlaier pd->lookup.gid)) 3305126258Smlaier r = TAILQ_NEXT(r, entries); 3306223637Sbz else if (r->prob && 3307223637Sbz r->prob <= arc4random()) 3308126258Smlaier r = TAILQ_NEXT(r, entries); 3309240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3310240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3311126258Smlaier r = TAILQ_NEXT(r, entries); 3312223637Sbz else if (r->os_fingerprint != PF_OSFP_ANY && 3313223637Sbz (pd->proto != IPPROTO_TCP || !pf_osfp_match( 3314223637Sbz pf_osfp_fingerprint(pd, m, off, th), 3315223637Sbz r->os_fingerprint))) 3316126258Smlaier r = TAILQ_NEXT(r, entries); 3317126258Smlaier else { 3318126258Smlaier if (r->tag) 3319126258Smlaier tag = r->tag; 3320171168Smlaier if (r->rtableid >= 0) 3321171168Smlaier rtableid = r->rtableid; 3322126258Smlaier if (r->anchor == NULL) { 3323171168Smlaier match = 1; 3324126258Smlaier *rm = r; 3325126258Smlaier *am = a; 3326126258Smlaier *rsm = ruleset; 3327126258Smlaier if ((*rm)->quick) 3328126258Smlaier break; 3329126258Smlaier r = TAILQ_NEXT(r, entries); 3330126258Smlaier } else 3331240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3332240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3333240641Sglebius &match); 3334126258Smlaier } 3335240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3336240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3337171168Smlaier break; 3338126258Smlaier } 3339126258Smlaier r = *rm; 3340126258Smlaier a = *am; 3341126258Smlaier ruleset = *rsm; 3342126258Smlaier 3343126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3344126258Smlaier 3345223637Sbz if (r->log || (nr != NULL && nr->log)) { 3346126258Smlaier if (rewrite) 3347223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3348240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, 3349240233Sglebius ruleset, pd, 1); 3350126258Smlaier } 3351126258Smlaier 3352126258Smlaier if ((r->action == PF_DROP) && 3353126258Smlaier ((r->rule_flag & PFRULE_RETURNRST) || 3354126258Smlaier (r->rule_flag & PFRULE_RETURNICMP) || 3355126258Smlaier (r->rule_flag & PFRULE_RETURN))) { 3356126258Smlaier /* undo NAT changes, if they have taken place */ 3357130613Smlaier if (nr != NULL) { 3358223637Sbz PF_ACPY(saddr, &sk->addr[pd->sidx], af); 3359223637Sbz PF_ACPY(daddr, &sk->addr[pd->didx], af); 3360223637Sbz if (pd->sport) 3361223637Sbz *pd->sport = sk->port[pd->sidx]; 3362223637Sbz if (pd->dport) 3363223637Sbz *pd->dport = sk->port[pd->didx]; 3364223637Sbz if (pd->proto_sum) 3365223637Sbz *pd->proto_sum = bproto_sum; 3366223637Sbz if (pd->ip_sum) 3367223637Sbz *pd->ip_sum = bip_sum; 3368223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3369126258Smlaier } 3370223637Sbz if (pd->proto == IPPROTO_TCP && 3371223637Sbz ((r->rule_flag & PFRULE_RETURNRST) || 3372126258Smlaier (r->rule_flag & PFRULE_RETURN)) && 3373126258Smlaier !(th->th_flags & TH_RST)) { 3374223637Sbz u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 3375223637Sbz int len = 0; 3376223637Sbz#ifdef INET 3377223637Sbz struct ip *h4; 3378223637Sbz#endif 3379223637Sbz#ifdef INET6 3380223637Sbz struct ip6_hdr *h6; 3381223637Sbz#endif 3382126258Smlaier 3383223637Sbz switch (af) { 3384223637Sbz#ifdef INET 3385223637Sbz case AF_INET: 3386223637Sbz h4 = mtod(m, struct ip *); 3387223637Sbz len = ntohs(h4->ip_len) - off; 3388223637Sbz break; 3389223637Sbz#endif 3390223637Sbz#ifdef INET6 3391223637Sbz case AF_INET6: 3392223637Sbz h6 = mtod(m, struct ip6_hdr *); 3393223637Sbz len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); 3394223637Sbz break; 3395223637Sbz#endif 3396223637Sbz } 3397223637Sbz 3398223637Sbz if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) 3399223637Sbz REASON_SET(&reason, PFRES_PROTCKSUM); 3400223637Sbz else { 3401223637Sbz if (th->th_flags & TH_SYN) 3402223637Sbz ack++; 3403223637Sbz if (th->th_flags & TH_FIN) 3404223637Sbz ack++; 3405223637Sbz pf_send_tcp(m, r, af, pd->dst, 3406223637Sbz pd->src, th->th_dport, th->th_sport, 3407223637Sbz ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 3408240233Sglebius r->return_ttl, 1, 0, kif->pfik_ifp); 3409223637Sbz } 3410223637Sbz } else if (pd->proto != IPPROTO_ICMP && af == AF_INET && 3411223637Sbz r->return_icmp) 3412126258Smlaier pf_send_icmp(m, r->return_icmp >> 8, 3413126258Smlaier r->return_icmp & 255, af, r); 3414223637Sbz else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && 3415223637Sbz r->return_icmp6) 3416126258Smlaier pf_send_icmp(m, r->return_icmp6 >> 8, 3417126258Smlaier r->return_icmp6 & 255, af, r); 3418126258Smlaier } 3419126258Smlaier 3420126258Smlaier if (r->action == PF_DROP) 3421223637Sbz goto cleanup; 3422126258Smlaier 3423240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3424126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3425223637Sbz goto cleanup; 3426126258Smlaier } 3427240233Sglebius if (rtableid >= 0) 3428240233Sglebius M_SETFIB(m, rtableid); 3429126258Smlaier 3430223637Sbz if (!state_icmp && (r->keep_state || nr != NULL || 3431223637Sbz (pd->flags & PFDESC_TCP_NORM))) { 3432223637Sbz int action; 3433240233Sglebius action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, 3434240233Sglebius sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, 3435240233Sglebius hdrlen); 3436223637Sbz if (action != PF_PASS) 3437223637Sbz return (action); 3438223637Sbz } else { 3439223637Sbz if (sk != NULL) 3440240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3441223637Sbz if (nk != NULL) 3442240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3443223637Sbz } 3444126258Smlaier 3445223637Sbz /* copy back packet headers if we performed NAT operations */ 3446223637Sbz if (rewrite) 3447223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3448130613Smlaier 3449240233Sglebius if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 3450240233Sglebius direction == PF_OUT && 3451240233Sglebius pfsync_defer_ptr != NULL && pfsync_defer_ptr(*sm, m)) 3452223637Sbz /* 3453223637Sbz * We want the state created, but we dont 3454223637Sbz * want to send this in case a partner 3455223637Sbz * firewall has to know about it to allow 3456223637Sbz * replies through it. 3457223637Sbz */ 3458240233Sglebius return (PF_DEFER); 3459223637Sbz 3460223637Sbz return (PF_PASS); 3461223637Sbz 3462130613Smlaiercleanup: 3463223637Sbz if (sk != NULL) 3464240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3465223637Sbz if (nk != NULL) 3466240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3467223637Sbz return (PF_DROP); 3468223637Sbz} 3469126258Smlaier 3470240233Sglebiusstatic int 3471223637Sbzpf_create_state(struct pf_rule *r, struct pf_rule *nr, struct pf_rule *a, 3472240233Sglebius struct pf_pdesc *pd, struct pf_src_node *nsn, struct pf_state_key *nk, 3473240233Sglebius struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport, 3474240233Sglebius u_int16_t dport, int *rewrite, struct pfi_kif *kif, struct pf_state **sm, 3475240233Sglebius int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen) 3476223637Sbz{ 3477223637Sbz struct pf_state *s = NULL; 3478223637Sbz struct pf_src_node *sn = NULL; 3479223637Sbz struct tcphdr *th = pd->hdr.tcp; 3480223637Sbz u_int16_t mss = V_tcp_mssdflt; 3481223637Sbz u_short reason; 3482223637Sbz 3483223637Sbz /* check maximums */ 3484263029Sglebius if (r->max_states && 3485263029Sglebius (counter_u64_fetch(r->states_cur) >= r->max_states)) { 3486270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_STATES], 1); 3487223637Sbz REASON_SET(&reason, PFRES_MAXSTATES); 3488223637Sbz return (PF_DROP); 3489223637Sbz } 3490223637Sbz /* src node for filter rule */ 3491223637Sbz if ((r->rule_flag & PFRULE_SRCTRACK || 3492223637Sbz r->rpool.opts & PF_POOL_STICKYADDR) && 3493223637Sbz pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) { 3494223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3495223637Sbz goto csfailed; 3496223637Sbz } 3497223637Sbz /* src node for translation rule */ 3498223637Sbz if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) && 3499223637Sbz pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) { 3500223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3501223637Sbz goto csfailed; 3502223637Sbz } 3503240233Sglebius s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO); 3504223637Sbz if (s == NULL) { 3505223637Sbz REASON_SET(&reason, PFRES_MEMORY); 3506223637Sbz goto csfailed; 3507223637Sbz } 3508223637Sbz s->rule.ptr = r; 3509223637Sbz s->nat_rule.ptr = nr; 3510223637Sbz s->anchor.ptr = a; 3511223637Sbz STATE_INC_COUNTERS(s); 3512223637Sbz if (r->allow_opts) 3513223637Sbz s->state_flags |= PFSTATE_ALLOWOPTS; 3514223637Sbz if (r->rule_flag & PFRULE_STATESLOPPY) 3515223637Sbz s->state_flags |= PFSTATE_SLOPPY; 3516223637Sbz s->log = r->log & PF_LOG_ALL; 3517223637Sbz s->sync_state = PFSYNC_S_NONE; 3518223637Sbz if (nr != NULL) 3519223637Sbz s->log |= nr->log & PF_LOG_ALL; 3520223637Sbz switch (pd->proto) { 3521223637Sbz case IPPROTO_TCP: 3522126258Smlaier s->src.seqlo = ntohl(th->th_seq); 3523223637Sbz s->src.seqhi = s->src.seqlo + pd->p_len + 1; 3524126258Smlaier if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN && 3525126258Smlaier r->keep_state == PF_STATE_MODULATE) { 3526126258Smlaier /* Generate sequence number modulator */ 3527223637Sbz if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 3528223637Sbz 0) 3529223637Sbz s->src.seqdiff = 1; 3530293894Sglebius pf_change_proto_a(m, &th->th_seq, &th->th_sum, 3531126258Smlaier htonl(s->src.seqlo + s->src.seqdiff), 0); 3532223637Sbz *rewrite = 1; 3533126258Smlaier } else 3534126258Smlaier s->src.seqdiff = 0; 3535126258Smlaier if (th->th_flags & TH_SYN) { 3536126258Smlaier s->src.seqhi++; 3537223637Sbz s->src.wscale = pf_get_wscale(m, off, 3538223637Sbz th->th_off, pd->af); 3539126258Smlaier } 3540126258Smlaier s->src.max_win = MAX(ntohs(th->th_win), 1); 3541126258Smlaier if (s->src.wscale & PF_WSCALE_MASK) { 3542126258Smlaier /* Remove scale factor from initial window */ 3543126258Smlaier int win = s->src.max_win; 3544126258Smlaier win += 1 << (s->src.wscale & PF_WSCALE_MASK); 3545126258Smlaier s->src.max_win = (win - 1) >> 3546126258Smlaier (s->src.wscale & PF_WSCALE_MASK); 3547126258Smlaier } 3548126258Smlaier if (th->th_flags & TH_FIN) 3549126258Smlaier s->src.seqhi++; 3550126258Smlaier s->dst.seqhi = 1; 3551126258Smlaier s->dst.max_win = 1; 3552126258Smlaier s->src.state = TCPS_SYN_SENT; 3553126258Smlaier s->dst.state = TCPS_CLOSED; 3554126258Smlaier s->timeout = PFTM_TCP_FIRST_PACKET; 3555223637Sbz break; 3556223637Sbz case IPPROTO_UDP: 3557223637Sbz s->src.state = PFUDPS_SINGLE; 3558223637Sbz s->dst.state = PFUDPS_NO_TRAFFIC; 3559223637Sbz s->timeout = PFTM_UDP_FIRST_PACKET; 3560223637Sbz break; 3561223637Sbz case IPPROTO_ICMP: 3562223637Sbz#ifdef INET6 3563223637Sbz case IPPROTO_ICMPV6: 3564223637Sbz#endif 3565223637Sbz s->timeout = PFTM_ICMP_FIRST_PACKET; 3566223637Sbz break; 3567223637Sbz default: 3568223637Sbz s->src.state = PFOTHERS_SINGLE; 3569223637Sbz s->dst.state = PFOTHERS_NO_TRAFFIC; 3570223637Sbz s->timeout = PFTM_OTHER_FIRST_PACKET; 3571223637Sbz } 3572223637Sbz 3573270576Sglebius if (r->rt && r->rt != PF_FASTROUTE) { 3574270576Sglebius if (pf_map_addr(pd->af, r, pd->src, &s->rt_addr, NULL, &sn)) { 3575270925Sglebius REASON_SET(&reason, PFRES_BADSTATE); 3576270576Sglebius pf_src_tree_remove_state(s); 3577270576Sglebius STATE_DEC_COUNTERS(s); 3578270576Sglebius uma_zfree(V_pf_state_z, s); 3579270576Sglebius goto csfailed; 3580270576Sglebius } 3581270576Sglebius s->rt_kif = r->rpool.cur->kif; 3582270576Sglebius } 3583270576Sglebius 3584240233Sglebius s->creation = time_uptime; 3585240233Sglebius s->expire = time_uptime; 3586223637Sbz 3587223637Sbz if (sn != NULL) { 3588223637Sbz s->src_node = sn; 3589223637Sbz s->src_node->states++; 3590223637Sbz } 3591223637Sbz if (nsn != NULL) { 3592223637Sbz /* XXX We only modify one side for now. */ 3593223637Sbz PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af); 3594223637Sbz s->nat_src_node = nsn; 3595223637Sbz s->nat_src_node->states++; 3596223637Sbz } 3597223637Sbz if (pd->proto == IPPROTO_TCP) { 3598126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m, 3599126258Smlaier off, pd, th, &s->src, &s->dst)) { 3600126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3601130613Smlaier pf_src_tree_remove_state(s); 3602145836Smlaier STATE_DEC_COUNTERS(s); 3603240233Sglebius uma_zfree(V_pf_state_z, s); 3604126258Smlaier return (PF_DROP); 3605126258Smlaier } 3606126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub && 3607145836Smlaier pf_normalize_tcp_stateful(m, off, pd, &reason, th, s, 3608223637Sbz &s->src, &s->dst, rewrite)) { 3609145836Smlaier /* This really shouldn't happen!!! */ 3610145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 3611145836Smlaier ("pf_normalize_tcp_stateful failed on first pkt")); 3612126258Smlaier pf_normalize_tcp_cleanup(s); 3613130613Smlaier pf_src_tree_remove_state(s); 3614145836Smlaier STATE_DEC_COUNTERS(s); 3615240233Sglebius uma_zfree(V_pf_state_z, s); 3616223637Sbz return (PF_DROP); 3617126258Smlaier } 3618126258Smlaier } 3619223637Sbz s->direction = pd->dir; 3620126258Smlaier 3621240233Sglebius /* 3622240233Sglebius * sk/nk could already been setup by pf_get_translation(). 3623240233Sglebius */ 3624240233Sglebius if (nr == NULL) { 3625240233Sglebius KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p", 3626240233Sglebius __func__, nr, sk, nk)); 3627240233Sglebius sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport); 3628240233Sglebius if (sk == NULL) 3629240233Sglebius goto csfailed; 3630240233Sglebius nk = sk; 3631240233Sglebius } else 3632240233Sglebius KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p", 3633240233Sglebius __func__, nr, sk, nk)); 3634126258Smlaier 3635240233Sglebius /* Swap sk/nk for PF_OUT. */ 3636240233Sglebius if (pf_state_insert(BOUND_IFACE(r, kif), 3637240233Sglebius (pd->dir == PF_IN) ? sk : nk, 3638240233Sglebius (pd->dir == PF_IN) ? nk : sk, s)) { 3639223637Sbz if (pd->proto == IPPROTO_TCP) 3640223637Sbz pf_normalize_tcp_cleanup(s); 3641223637Sbz REASON_SET(&reason, PFRES_STATEINS); 3642223637Sbz pf_src_tree_remove_state(s); 3643223637Sbz STATE_DEC_COUNTERS(s); 3644240233Sglebius uma_zfree(V_pf_state_z, s); 3645223637Sbz return (PF_DROP); 3646223637Sbz } else 3647223637Sbz *sm = s; 3648126258Smlaier 3649240233Sglebius if (tag > 0) 3650223637Sbz s->tag = tag; 3651223637Sbz if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) == 3652223637Sbz TH_SYN && r->keep_state == PF_STATE_SYNPROXY) { 3653223637Sbz s->src.state = PF_TCPS_PROXY_SRC; 3654223637Sbz /* undo NAT changes, if they have taken place */ 3655223637Sbz if (nr != NULL) { 3656223637Sbz struct pf_state_key *skt = s->key[PF_SK_WIRE]; 3657223637Sbz if (pd->dir == PF_OUT) 3658223637Sbz skt = s->key[PF_SK_STACK]; 3659223637Sbz PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af); 3660223637Sbz PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af); 3661223637Sbz if (pd->sport) 3662223637Sbz *pd->sport = skt->port[pd->sidx]; 3663223637Sbz if (pd->dport) 3664223637Sbz *pd->dport = skt->port[pd->didx]; 3665223637Sbz if (pd->proto_sum) 3666223637Sbz *pd->proto_sum = bproto_sum; 3667223637Sbz if (pd->ip_sum) 3668223637Sbz *pd->ip_sum = bip_sum; 3669223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3670223637Sbz } 3671223637Sbz s->src.seqhi = htonl(arc4random()); 3672223637Sbz /* Find mss option */ 3673231852Sbz int rtid = M_GETFIB(m); 3674223637Sbz mss = pf_get_mss(m, off, th->th_off, pd->af); 3675231852Sbz mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 3676231852Sbz mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 3677223637Sbz s->src.mss = mss; 3678223637Sbz pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport, 3679223637Sbz th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 3680240233Sglebius TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL); 3681223637Sbz REASON_SET(&reason, PFRES_SYNPROXY); 3682223637Sbz return (PF_SYNPROXY_DROP); 3683171168Smlaier } 3684165631Smlaier 3685223637Sbz return (PF_PASS); 3686126258Smlaier 3687223637Sbzcsfailed: 3688223637Sbz if (sk != NULL) 3689240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3690223637Sbz if (nk != NULL) 3691240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3692223637Sbz 3693261019Sglebius if (sn != NULL && sn->states == 0 && sn->expire == 0) { 3694261019Sglebius pf_unlink_src_node(sn); 3695261019Sglebius pf_free_src_node(sn); 3696261019Sglebius } 3697240737Sglebius 3698261019Sglebius if (nsn != sn && nsn != NULL && nsn->states == 0 && nsn->expire == 0) { 3699261019Sglebius pf_unlink_src_node(nsn); 3700261019Sglebius pf_free_src_node(nsn); 3701261019Sglebius } 3702240737Sglebius 3703223637Sbz return (PF_DROP); 3704126258Smlaier} 3705126258Smlaier 3706240233Sglebiusstatic int 3707130613Smlaierpf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif, 3708126258Smlaier struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am, 3709126258Smlaier struct pf_ruleset **rsm) 3710126258Smlaier{ 3711126258Smlaier struct pf_rule *r, *a = NULL; 3712126258Smlaier struct pf_ruleset *ruleset = NULL; 3713126258Smlaier sa_family_t af = pd->af; 3714126258Smlaier u_short reason; 3715126258Smlaier int tag = -1; 3716145836Smlaier int asd = 0; 3717171168Smlaier int match = 0; 3718240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3719126258Smlaier 3720240233Sglebius PF_RULES_RASSERT(); 3721240233Sglebius 3722126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3723126258Smlaier while (r != NULL) { 3724126258Smlaier r->evaluations++; 3725171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3726126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3727126258Smlaier else if (r->direction && r->direction != direction) 3728126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3729126258Smlaier else if (r->af && r->af != af) 3730126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3731126258Smlaier else if (r->proto && r->proto != pd->proto) 3732126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3733171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 3734231852Sbz r->src.neg, kif, M_GETFIB(m))) 3735126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3736171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 3737231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3738126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3739171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3740126258Smlaier r = TAILQ_NEXT(r, entries); 3741173815Smlaier else if (r->os_fingerprint != PF_OSFP_ANY) 3742126258Smlaier r = TAILQ_NEXT(r, entries); 3743173815Smlaier else if (pd->proto == IPPROTO_UDP && 3744173815Smlaier (r->src.port_op || r->dst.port_op)) 3745173815Smlaier r = TAILQ_NEXT(r, entries); 3746173815Smlaier else if (pd->proto == IPPROTO_TCP && 3747173815Smlaier (r->src.port_op || r->dst.port_op || r->flagset)) 3748173815Smlaier r = TAILQ_NEXT(r, entries); 3749173815Smlaier else if ((pd->proto == IPPROTO_ICMP || 3750173815Smlaier pd->proto == IPPROTO_ICMPV6) && 3751173815Smlaier (r->type || r->code)) 3752173815Smlaier r = TAILQ_NEXT(r, entries); 3753223637Sbz else if (r->prob && r->prob <= 3754223637Sbz (arc4random() % (UINT_MAX - 1) + 1)) 3755126258Smlaier r = TAILQ_NEXT(r, entries); 3756240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3757240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3758126258Smlaier r = TAILQ_NEXT(r, entries); 3759126258Smlaier else { 3760126258Smlaier if (r->anchor == NULL) { 3761171168Smlaier match = 1; 3762126258Smlaier *rm = r; 3763126258Smlaier *am = a; 3764126258Smlaier *rsm = ruleset; 3765126258Smlaier if ((*rm)->quick) 3766126258Smlaier break; 3767126258Smlaier r = TAILQ_NEXT(r, entries); 3768126258Smlaier } else 3769240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3770240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3771240641Sglebius &match); 3772126258Smlaier } 3773240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3774240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3775171168Smlaier break; 3776126258Smlaier } 3777126258Smlaier r = *rm; 3778126258Smlaier a = *am; 3779126258Smlaier ruleset = *rsm; 3780126258Smlaier 3781126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3782130613Smlaier 3783126258Smlaier if (r->log) 3784240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd, 3785240233Sglebius 1); 3786126258Smlaier 3787126258Smlaier if (r->action != PF_PASS) 3788126258Smlaier return (PF_DROP); 3789126258Smlaier 3790240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3791126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3792126258Smlaier return (PF_DROP); 3793126258Smlaier } 3794126258Smlaier 3795126258Smlaier return (PF_PASS); 3796126258Smlaier} 3797126258Smlaier 3798240233Sglebiusstatic int 3799200930Sdelphijpf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst, 3800200930Sdelphij struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off, 3801200930Sdelphij struct pf_pdesc *pd, u_short *reason, int *copyback) 3802126258Smlaier{ 3803223637Sbz struct tcphdr *th = pd->hdr.tcp; 3804223637Sbz u_int16_t win = ntohs(th->th_win); 3805223637Sbz u_int32_t ack, end, seq, orig_seq; 3806223637Sbz u_int8_t sws, dws; 3807223637Sbz int ackskew; 3808126258Smlaier 3809126258Smlaier if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) { 3810126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3811126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3812126258Smlaier } else 3813126258Smlaier sws = dws = 0; 3814126258Smlaier 3815126258Smlaier /* 3816126258Smlaier * Sequence tracking algorithm from Guido van Rooij's paper: 3817126258Smlaier * http://www.madison-gurkha.com/publications/tcp_filtering/ 3818126258Smlaier * tcp_filtering.ps 3819126258Smlaier */ 3820126258Smlaier 3821145836Smlaier orig_seq = seq = ntohl(th->th_seq); 3822126258Smlaier if (src->seqlo == 0) { 3823126258Smlaier /* First packet from this end. Set its state */ 3824126258Smlaier 3825126258Smlaier if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) && 3826126258Smlaier src->scrub == NULL) { 3827126258Smlaier if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) { 3828126258Smlaier REASON_SET(reason, PFRES_MEMORY); 3829126258Smlaier return (PF_DROP); 3830126258Smlaier } 3831126258Smlaier } 3832126258Smlaier 3833126258Smlaier /* Deferred generation of sequence number modulator */ 3834126258Smlaier if (dst->seqdiff && !src->seqdiff) { 3835223637Sbz /* use random iss for the TCP server */ 3836223637Sbz while ((src->seqdiff = arc4random() - seq) == 0) 3837126258Smlaier ; 3838126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3839293894Sglebius pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq + 3840126258Smlaier src->seqdiff), 0); 3841293894Sglebius pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0); 3842200930Sdelphij *copyback = 1; 3843126258Smlaier } else { 3844126258Smlaier ack = ntohl(th->th_ack); 3845126258Smlaier } 3846126258Smlaier 3847126258Smlaier end = seq + pd->p_len; 3848126258Smlaier if (th->th_flags & TH_SYN) { 3849126258Smlaier end++; 3850126258Smlaier if (dst->wscale & PF_WSCALE_FLAG) { 3851126258Smlaier src->wscale = pf_get_wscale(m, off, th->th_off, 3852126258Smlaier pd->af); 3853126258Smlaier if (src->wscale & PF_WSCALE_FLAG) { 3854126258Smlaier /* Remove scale factor from initial 3855126258Smlaier * window */ 3856126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3857126258Smlaier win = ((u_int32_t)win + (1 << sws) - 1) 3858126258Smlaier >> sws; 3859126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3860126258Smlaier } else { 3861126258Smlaier /* fixup other window */ 3862126258Smlaier dst->max_win <<= dst->wscale & 3863126258Smlaier PF_WSCALE_MASK; 3864126258Smlaier /* in case of a retrans SYN|ACK */ 3865126258Smlaier dst->wscale = 0; 3866126258Smlaier } 3867126258Smlaier } 3868126258Smlaier } 3869126258Smlaier if (th->th_flags & TH_FIN) 3870126258Smlaier end++; 3871126258Smlaier 3872126258Smlaier src->seqlo = seq; 3873126258Smlaier if (src->state < TCPS_SYN_SENT) 3874126258Smlaier src->state = TCPS_SYN_SENT; 3875126258Smlaier 3876126258Smlaier /* 3877126258Smlaier * May need to slide the window (seqhi may have been set by 3878126258Smlaier * the crappy stack check or if we picked up the connection 3879126258Smlaier * after establishment) 3880126258Smlaier */ 3881126258Smlaier if (src->seqhi == 1 || 3882126258Smlaier SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 3883126258Smlaier src->seqhi = end + MAX(1, dst->max_win << dws); 3884126258Smlaier if (win > src->max_win) 3885126258Smlaier src->max_win = win; 3886126258Smlaier 3887126258Smlaier } else { 3888126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3889126258Smlaier if (src->seqdiff) { 3890126258Smlaier /* Modulate sequence numbers */ 3891293894Sglebius pf_change_proto_a(m, &th->th_seq, &th->th_sum, htonl(seq + 3892126258Smlaier src->seqdiff), 0); 3893293894Sglebius pf_change_proto_a(m, &th->th_ack, &th->th_sum, htonl(ack), 0); 3894200930Sdelphij *copyback = 1; 3895126258Smlaier } 3896126258Smlaier end = seq + pd->p_len; 3897126258Smlaier if (th->th_flags & TH_SYN) 3898126258Smlaier end++; 3899126258Smlaier if (th->th_flags & TH_FIN) 3900126258Smlaier end++; 3901126258Smlaier } 3902126258Smlaier 3903126258Smlaier if ((th->th_flags & TH_ACK) == 0) { 3904126258Smlaier /* Let it pass through the ack skew check */ 3905126258Smlaier ack = dst->seqlo; 3906126258Smlaier } else if ((ack == 0 && 3907126258Smlaier (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 3908126258Smlaier /* broken tcp stacks do not set ack */ 3909126258Smlaier (dst->state < TCPS_SYN_SENT)) { 3910126258Smlaier /* 3911126258Smlaier * Many stacks (ours included) will set the ACK number in an 3912126258Smlaier * FIN|ACK if the SYN times out -- no sequence to ACK. 3913126258Smlaier */ 3914126258Smlaier ack = dst->seqlo; 3915126258Smlaier } 3916126258Smlaier 3917126258Smlaier if (seq == end) { 3918126258Smlaier /* Ease sequencing restrictions on no data packets */ 3919126258Smlaier seq = src->seqlo; 3920126258Smlaier end = seq; 3921126258Smlaier } 3922126258Smlaier 3923126258Smlaier ackskew = dst->seqlo - ack; 3924126258Smlaier 3925171168Smlaier 3926171168Smlaier /* 3927171168Smlaier * Need to demodulate the sequence numbers in any TCP SACK options 3928171168Smlaier * (Selective ACK). We could optionally validate the SACK values 3929171168Smlaier * against the current ACK window, either forwards or backwards, but 3930171168Smlaier * I'm not confident that SACK has been implemented properly 3931171168Smlaier * everywhere. It wouldn't surprise me if several stacks accidently 3932171168Smlaier * SACK too far backwards of previously ACKed data. There really aren't 3933171168Smlaier * any security implications of bad SACKing unless the target stack 3934171168Smlaier * doesn't validate the option length correctly. Someone trying to 3935171168Smlaier * spoof into a TCP connection won't bother blindly sending SACK 3936171168Smlaier * options anyway. 3937171168Smlaier */ 3938171168Smlaier if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 3939171168Smlaier if (pf_modulate_sack(m, off, pd, th, dst)) 3940200930Sdelphij *copyback = 1; 3941171168Smlaier } 3942171168Smlaier 3943171168Smlaier 3944223637Sbz#define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 3945126258Smlaier if (SEQ_GEQ(src->seqhi, end) && 3946126258Smlaier /* Last octet inside other's window space */ 3947126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 3948126258Smlaier /* Retrans: not more than one window back */ 3949126258Smlaier (ackskew >= -MAXACKWINDOW) && 3950126258Smlaier /* Acking not more than one reassembled fragment backwards */ 3951145836Smlaier (ackskew <= (MAXACKWINDOW << sws)) && 3952126258Smlaier /* Acking not more than one window forward */ 3953145836Smlaier ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo || 3954223637Sbz (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) || 3955223637Sbz (pd->flags & PFDESC_IP_REAS) == 0)) { 3956171168Smlaier /* Require an exact/+1 sequence match on resets when possible */ 3957126258Smlaier 3958145836Smlaier if (dst->scrub || src->scrub) { 3959145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 3960200930Sdelphij *state, src, dst, copyback)) 3961145836Smlaier return (PF_DROP); 3962145836Smlaier } 3963145836Smlaier 3964126258Smlaier /* update max window */ 3965126258Smlaier if (src->max_win < win) 3966126258Smlaier src->max_win = win; 3967126258Smlaier /* synchronize sequencing */ 3968126258Smlaier if (SEQ_GT(end, src->seqlo)) 3969126258Smlaier src->seqlo = end; 3970126258Smlaier /* slide the window of what the other end can send */ 3971126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 3972126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 3973126258Smlaier 3974126258Smlaier 3975126258Smlaier /* update states */ 3976126258Smlaier if (th->th_flags & TH_SYN) 3977126258Smlaier if (src->state < TCPS_SYN_SENT) 3978126258Smlaier src->state = TCPS_SYN_SENT; 3979126258Smlaier if (th->th_flags & TH_FIN) 3980126258Smlaier if (src->state < TCPS_CLOSING) 3981126258Smlaier src->state = TCPS_CLOSING; 3982126258Smlaier if (th->th_flags & TH_ACK) { 3983145836Smlaier if (dst->state == TCPS_SYN_SENT) { 3984126258Smlaier dst->state = TCPS_ESTABLISHED; 3985145836Smlaier if (src->state == TCPS_ESTABLISHED && 3986145836Smlaier (*state)->src_node != NULL && 3987145836Smlaier pf_src_connlimit(state)) { 3988145836Smlaier REASON_SET(reason, PFRES_SRCLIMIT); 3989145836Smlaier return (PF_DROP); 3990145836Smlaier } 3991145836Smlaier } else if (dst->state == TCPS_CLOSING) 3992126258Smlaier dst->state = TCPS_FIN_WAIT_2; 3993126258Smlaier } 3994126258Smlaier if (th->th_flags & TH_RST) 3995126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 3996126258Smlaier 3997126258Smlaier /* update expire time */ 3998240233Sglebius (*state)->expire = time_uptime; 3999126258Smlaier if (src->state >= TCPS_FIN_WAIT_2 && 4000126258Smlaier dst->state >= TCPS_FIN_WAIT_2) 4001126258Smlaier (*state)->timeout = PFTM_TCP_CLOSED; 4002171168Smlaier else if (src->state >= TCPS_CLOSING && 4003171168Smlaier dst->state >= TCPS_CLOSING) 4004126258Smlaier (*state)->timeout = PFTM_TCP_FIN_WAIT; 4005126258Smlaier else if (src->state < TCPS_ESTABLISHED || 4006126258Smlaier dst->state < TCPS_ESTABLISHED) 4007126258Smlaier (*state)->timeout = PFTM_TCP_OPENING; 4008126258Smlaier else if (src->state >= TCPS_CLOSING || 4009126258Smlaier dst->state >= TCPS_CLOSING) 4010126258Smlaier (*state)->timeout = PFTM_TCP_CLOSING; 4011126258Smlaier else 4012126258Smlaier (*state)->timeout = PFTM_TCP_ESTABLISHED; 4013126258Smlaier 4014126258Smlaier /* Fall through to PASS packet */ 4015126258Smlaier 4016126258Smlaier } else if ((dst->state < TCPS_SYN_SENT || 4017126258Smlaier dst->state >= TCPS_FIN_WAIT_2 || 4018126258Smlaier src->state >= TCPS_FIN_WAIT_2) && 4019126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) && 4020126258Smlaier /* Within a window forward of the originating packet */ 4021126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 4022126258Smlaier /* Within a window backward of the originating packet */ 4023126258Smlaier 4024126258Smlaier /* 4025126258Smlaier * This currently handles three situations: 4026126258Smlaier * 1) Stupid stacks will shotgun SYNs before their peer 4027126258Smlaier * replies. 4028126258Smlaier * 2) When PF catches an already established stream (the 4029126258Smlaier * firewall rebooted, the state table was flushed, routes 4030126258Smlaier * changed...) 4031126258Smlaier * 3) Packets get funky immediately after the connection 4032126258Smlaier * closes (this should catch Solaris spurious ACK|FINs 4033126258Smlaier * that web servers like to spew after a close) 4034126258Smlaier * 4035126258Smlaier * This must be a little more careful than the above code 4036126258Smlaier * since packet floods will also be caught here. We don't 4037126258Smlaier * update the TTL here to mitigate the damage of a packet 4038126258Smlaier * flood and so the same code can handle awkward establishment 4039126258Smlaier * and a loosened connection close. 4040126258Smlaier * In the establishment case, a correct peer response will 4041126258Smlaier * validate the connection, go through the normal state code 4042126258Smlaier * and keep updating the state TTL. 4043126258Smlaier */ 4044126258Smlaier 4045223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4046126258Smlaier printf("pf: loose state match: "); 4047126258Smlaier pf_print_state(*state); 4048126258Smlaier pf_print_flags(th->th_flags); 4049171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4050223637Sbz "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 4051223637Sbz pd->p_len, ackskew, (unsigned long long)(*state)->packets[0], 4052223637Sbz (unsigned long long)(*state)->packets[1], 4053223637Sbz pd->dir == PF_IN ? "in" : "out", 4054223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4055126258Smlaier } 4056126258Smlaier 4057145836Smlaier if (dst->scrub || src->scrub) { 4058145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 4059200930Sdelphij *state, src, dst, copyback)) 4060145836Smlaier return (PF_DROP); 4061145836Smlaier } 4062145836Smlaier 4063126258Smlaier /* update max window */ 4064126258Smlaier if (src->max_win < win) 4065126258Smlaier src->max_win = win; 4066126258Smlaier /* synchronize sequencing */ 4067126258Smlaier if (SEQ_GT(end, src->seqlo)) 4068126258Smlaier src->seqlo = end; 4069126258Smlaier /* slide the window of what the other end can send */ 4070126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 4071126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 4072126258Smlaier 4073126258Smlaier /* 4074126258Smlaier * Cannot set dst->seqhi here since this could be a shotgunned 4075126258Smlaier * SYN and not an already established connection. 4076126258Smlaier */ 4077126258Smlaier 4078126258Smlaier if (th->th_flags & TH_FIN) 4079126258Smlaier if (src->state < TCPS_CLOSING) 4080126258Smlaier src->state = TCPS_CLOSING; 4081126258Smlaier if (th->th_flags & TH_RST) 4082126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 4083126258Smlaier 4084126258Smlaier /* Fall through to PASS packet */ 4085126258Smlaier 4086126258Smlaier } else { 4087126258Smlaier if ((*state)->dst.state == TCPS_SYN_SENT && 4088126258Smlaier (*state)->src.state == TCPS_SYN_SENT) { 4089126258Smlaier /* Send RST for state mismatches during handshake */ 4090145836Smlaier if (!(th->th_flags & TH_RST)) 4091223637Sbz pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4092126258Smlaier pd->dst, pd->src, th->th_dport, 4093145836Smlaier th->th_sport, ntohl(th->th_ack), 0, 4094145836Smlaier TH_RST, 0, 0, 4095171168Smlaier (*state)->rule.ptr->return_ttl, 1, 0, 4096240233Sglebius kif->pfik_ifp); 4097126258Smlaier src->seqlo = 0; 4098126258Smlaier src->seqhi = 1; 4099126258Smlaier src->max_win = 1; 4100223637Sbz } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 4101126258Smlaier printf("pf: BAD state: "); 4102126258Smlaier pf_print_state(*state); 4103126258Smlaier pf_print_flags(th->th_flags); 4104171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4105171168Smlaier "pkts=%llu:%llu dir=%s,%s\n", 4106171168Smlaier seq, orig_seq, ack, pd->p_len, ackskew, 4107171168Smlaier (unsigned long long)(*state)->packets[0], 4108171168Smlaier (unsigned long long)(*state)->packets[1], 4109223637Sbz pd->dir == PF_IN ? "in" : "out", 4110223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4111126258Smlaier printf("pf: State failure on: %c %c %c %c | %c %c\n", 4112126258Smlaier SEQ_GEQ(src->seqhi, end) ? ' ' : '1', 4113126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 4114126258Smlaier ' ': '2', 4115126258Smlaier (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 4116126258Smlaier (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 4117126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5', 4118126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 4119126258Smlaier } 4120145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4121126258Smlaier return (PF_DROP); 4122126258Smlaier } 4123126258Smlaier 4124200930Sdelphij return (PF_PASS); 4125200930Sdelphij} 4126126258Smlaier 4127240233Sglebiusstatic int 4128200930Sdelphijpf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst, 4129200930Sdelphij struct pf_state **state, struct pf_pdesc *pd, u_short *reason) 4130200930Sdelphij{ 4131200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4132200930Sdelphij 4133200930Sdelphij if (th->th_flags & TH_SYN) 4134200930Sdelphij if (src->state < TCPS_SYN_SENT) 4135200930Sdelphij src->state = TCPS_SYN_SENT; 4136200930Sdelphij if (th->th_flags & TH_FIN) 4137200930Sdelphij if (src->state < TCPS_CLOSING) 4138200930Sdelphij src->state = TCPS_CLOSING; 4139200930Sdelphij if (th->th_flags & TH_ACK) { 4140200930Sdelphij if (dst->state == TCPS_SYN_SENT) { 4141200930Sdelphij dst->state = TCPS_ESTABLISHED; 4142200930Sdelphij if (src->state == TCPS_ESTABLISHED && 4143200930Sdelphij (*state)->src_node != NULL && 4144200930Sdelphij pf_src_connlimit(state)) { 4145200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4146200930Sdelphij return (PF_DROP); 4147200930Sdelphij } 4148200930Sdelphij } else if (dst->state == TCPS_CLOSING) { 4149200930Sdelphij dst->state = TCPS_FIN_WAIT_2; 4150200930Sdelphij } else if (src->state == TCPS_SYN_SENT && 4151200930Sdelphij dst->state < TCPS_SYN_SENT) { 4152200930Sdelphij /* 4153200930Sdelphij * Handle a special sloppy case where we only see one 4154200930Sdelphij * half of the connection. If there is a ACK after 4155200930Sdelphij * the initial SYN without ever seeing a packet from 4156200930Sdelphij * the destination, set the connection to established. 4157200930Sdelphij */ 4158200930Sdelphij dst->state = src->state = TCPS_ESTABLISHED; 4159200930Sdelphij if ((*state)->src_node != NULL && 4160200930Sdelphij pf_src_connlimit(state)) { 4161200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4162200930Sdelphij return (PF_DROP); 4163200930Sdelphij } 4164200930Sdelphij } else if (src->state == TCPS_CLOSING && 4165200930Sdelphij dst->state == TCPS_ESTABLISHED && 4166200930Sdelphij dst->seqlo == 0) { 4167200930Sdelphij /* 4168200930Sdelphij * Handle the closing of half connections where we 4169200930Sdelphij * don't see the full bidirectional FIN/ACK+ACK 4170200930Sdelphij * handshake. 4171200930Sdelphij */ 4172200930Sdelphij dst->state = TCPS_CLOSING; 4173200930Sdelphij } 4174200930Sdelphij } 4175200930Sdelphij if (th->th_flags & TH_RST) 4176200930Sdelphij src->state = dst->state = TCPS_TIME_WAIT; 4177200930Sdelphij 4178200930Sdelphij /* update expire time */ 4179240233Sglebius (*state)->expire = time_uptime; 4180200930Sdelphij if (src->state >= TCPS_FIN_WAIT_2 && 4181200930Sdelphij dst->state >= TCPS_FIN_WAIT_2) 4182200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSED; 4183200930Sdelphij else if (src->state >= TCPS_CLOSING && 4184200930Sdelphij dst->state >= TCPS_CLOSING) 4185200930Sdelphij (*state)->timeout = PFTM_TCP_FIN_WAIT; 4186200930Sdelphij else if (src->state < TCPS_ESTABLISHED || 4187200930Sdelphij dst->state < TCPS_ESTABLISHED) 4188200930Sdelphij (*state)->timeout = PFTM_TCP_OPENING; 4189200930Sdelphij else if (src->state >= TCPS_CLOSING || 4190200930Sdelphij dst->state >= TCPS_CLOSING) 4191200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSING; 4192200930Sdelphij else 4193200930Sdelphij (*state)->timeout = PFTM_TCP_ESTABLISHED; 4194200930Sdelphij 4195200930Sdelphij return (PF_PASS); 4196200930Sdelphij} 4197200930Sdelphij 4198240233Sglebiusstatic int 4199200930Sdelphijpf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif, 4200200930Sdelphij struct mbuf *m, int off, void *h, struct pf_pdesc *pd, 4201200930Sdelphij u_short *reason) 4202200930Sdelphij{ 4203223637Sbz struct pf_state_key_cmp key; 4204200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4205200930Sdelphij int copyback = 0; 4206200930Sdelphij struct pf_state_peer *src, *dst; 4207223637Sbz struct pf_state_key *sk; 4208200930Sdelphij 4209240233Sglebius bzero(&key, sizeof(key)); 4210200930Sdelphij key.af = pd->af; 4211200930Sdelphij key.proto = IPPROTO_TCP; 4212223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4213223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4214223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4215223637Sbz key.port[0] = th->th_sport; 4216223637Sbz key.port[1] = th->th_dport; 4217223637Sbz } else { /* stack side, reverse */ 4218223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4219223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4220223637Sbz key.port[1] = th->th_sport; 4221223637Sbz key.port[0] = th->th_dport; 4222200930Sdelphij } 4223200930Sdelphij 4224240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4225200930Sdelphij 4226200930Sdelphij if (direction == (*state)->direction) { 4227200930Sdelphij src = &(*state)->src; 4228200930Sdelphij dst = &(*state)->dst; 4229200930Sdelphij } else { 4230200930Sdelphij src = &(*state)->dst; 4231200930Sdelphij dst = &(*state)->src; 4232200930Sdelphij } 4233200930Sdelphij 4234223637Sbz sk = (*state)->key[pd->didx]; 4235223637Sbz 4236200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_SRC) { 4237200930Sdelphij if (direction != (*state)->direction) { 4238200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4239200930Sdelphij return (PF_SYNPROXY_DROP); 4240200930Sdelphij } 4241200930Sdelphij if (th->th_flags & TH_SYN) { 4242200930Sdelphij if (ntohl(th->th_seq) != (*state)->src.seqlo) { 4243200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4244200930Sdelphij return (PF_DROP); 4245200930Sdelphij } 4246200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4247200930Sdelphij pd->src, th->th_dport, th->th_sport, 4248200930Sdelphij (*state)->src.seqhi, ntohl(th->th_seq) + 1, 4249240233Sglebius TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL); 4250200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4251200930Sdelphij return (PF_SYNPROXY_DROP); 4252200930Sdelphij } else if (!(th->th_flags & TH_ACK) || 4253200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4254200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4255200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4256200930Sdelphij return (PF_DROP); 4257200930Sdelphij } else if ((*state)->src_node != NULL && 4258200930Sdelphij pf_src_connlimit(state)) { 4259200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4260200930Sdelphij return (PF_DROP); 4261200930Sdelphij } else 4262200930Sdelphij (*state)->src.state = PF_TCPS_PROXY_DST; 4263200930Sdelphij } 4264200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_DST) { 4265200930Sdelphij if (direction == (*state)->direction) { 4266200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) || 4267200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4268200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4269200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4270200930Sdelphij return (PF_DROP); 4271200930Sdelphij } 4272200930Sdelphij (*state)->src.max_win = MAX(ntohs(th->th_win), 1); 4273200930Sdelphij if ((*state)->dst.seqhi == 1) 4274200930Sdelphij (*state)->dst.seqhi = htonl(arc4random()); 4275200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4276223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4277223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4278200930Sdelphij (*state)->dst.seqhi, 0, TH_SYN, 0, 4279240233Sglebius (*state)->src.mss, 0, 0, (*state)->tag, NULL); 4280200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4281200930Sdelphij return (PF_SYNPROXY_DROP); 4282200930Sdelphij } else if (((th->th_flags & (TH_SYN|TH_ACK)) != 4283200930Sdelphij (TH_SYN|TH_ACK)) || 4284200930Sdelphij (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) { 4285200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4286200930Sdelphij return (PF_DROP); 4287200930Sdelphij } else { 4288200930Sdelphij (*state)->dst.max_win = MAX(ntohs(th->th_win), 1); 4289200930Sdelphij (*state)->dst.seqlo = ntohl(th->th_seq); 4290200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4291200930Sdelphij pd->src, th->th_dport, th->th_sport, 4292200930Sdelphij ntohl(th->th_ack), ntohl(th->th_seq) + 1, 4293200930Sdelphij TH_ACK, (*state)->src.max_win, 0, 0, 0, 4294240233Sglebius (*state)->tag, NULL); 4295200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4296223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4297223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4298200930Sdelphij (*state)->src.seqhi + 1, (*state)->src.seqlo + 1, 4299240233Sglebius TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL); 4300200930Sdelphij (*state)->src.seqdiff = (*state)->dst.seqhi - 4301200930Sdelphij (*state)->src.seqlo; 4302200930Sdelphij (*state)->dst.seqdiff = (*state)->src.seqhi - 4303200930Sdelphij (*state)->dst.seqlo; 4304200930Sdelphij (*state)->src.seqhi = (*state)->src.seqlo + 4305200930Sdelphij (*state)->dst.max_win; 4306200930Sdelphij (*state)->dst.seqhi = (*state)->dst.seqlo + 4307200930Sdelphij (*state)->src.max_win; 4308200930Sdelphij (*state)->src.wscale = (*state)->dst.wscale = 0; 4309200930Sdelphij (*state)->src.state = (*state)->dst.state = 4310200930Sdelphij TCPS_ESTABLISHED; 4311200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4312200930Sdelphij return (PF_SYNPROXY_DROP); 4313200930Sdelphij } 4314200930Sdelphij } 4315200930Sdelphij 4316200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) && 4317200930Sdelphij dst->state >= TCPS_FIN_WAIT_2 && 4318200930Sdelphij src->state >= TCPS_FIN_WAIT_2) { 4319223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4320200930Sdelphij printf("pf: state reuse "); 4321200930Sdelphij pf_print_state(*state); 4322200930Sdelphij pf_print_flags(th->th_flags); 4323200930Sdelphij printf("\n"); 4324200930Sdelphij } 4325200930Sdelphij /* XXX make sure it's the same direction ?? */ 4326200930Sdelphij (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 4327240233Sglebius pf_unlink_state(*state, PF_ENTER_LOCKED); 4328200930Sdelphij *state = NULL; 4329200930Sdelphij return (PF_DROP); 4330200930Sdelphij } 4331200930Sdelphij 4332200930Sdelphij if ((*state)->state_flags & PFSTATE_SLOPPY) { 4333200930Sdelphij if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP) 4334200930Sdelphij return (PF_DROP); 4335200930Sdelphij } else { 4336200930Sdelphij if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason, 4337200930Sdelphij ©back) == PF_DROP) 4338200930Sdelphij return (PF_DROP); 4339200930Sdelphij } 4340200930Sdelphij 4341126258Smlaier /* translate source/destination address, if necessary */ 4342223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4343223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4344223637Sbz 4345223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4346223637Sbz nk->port[pd->sidx] != th->th_sport) 4347293894Sglebius pf_change_ap(m, pd->src, &th->th_sport, 4348293894Sglebius pd->ip_sum, &th->th_sum, &nk->addr[pd->sidx], 4349223637Sbz nk->port[pd->sidx], 0, pd->af); 4350223637Sbz 4351223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4352223637Sbz nk->port[pd->didx] != th->th_dport) 4353293894Sglebius pf_change_ap(m, pd->dst, &th->th_dport, 4354293894Sglebius pd->ip_sum, &th->th_sum, &nk->addr[pd->didx], 4355223637Sbz nk->port[pd->didx], 0, pd->af); 4356223637Sbz copyback = 1; 4357126258Smlaier } 4358126258Smlaier 4359223637Sbz /* Copyback sequence modulation or stateful scrub changes if needed */ 4360223637Sbz if (copyback) 4361223637Sbz m_copyback(m, off, sizeof(*th), (caddr_t)th); 4362223637Sbz 4363126258Smlaier return (PF_PASS); 4364126258Smlaier} 4365126258Smlaier 4366240233Sglebiusstatic int 4367130613Smlaierpf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif, 4368130613Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd) 4369126258Smlaier{ 4370126258Smlaier struct pf_state_peer *src, *dst; 4371223637Sbz struct pf_state_key_cmp key; 4372126258Smlaier struct udphdr *uh = pd->hdr.udp; 4373126258Smlaier 4374240233Sglebius bzero(&key, sizeof(key)); 4375126258Smlaier key.af = pd->af; 4376126258Smlaier key.proto = IPPROTO_UDP; 4377223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4378223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4379223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4380223637Sbz key.port[0] = uh->uh_sport; 4381223637Sbz key.port[1] = uh->uh_dport; 4382223637Sbz } else { /* stack side, reverse */ 4383223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4384223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4385223637Sbz key.port[1] = uh->uh_sport; 4386223637Sbz key.port[0] = uh->uh_dport; 4387130613Smlaier } 4388126258Smlaier 4389240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4390126258Smlaier 4391126258Smlaier if (direction == (*state)->direction) { 4392126258Smlaier src = &(*state)->src; 4393126258Smlaier dst = &(*state)->dst; 4394126258Smlaier } else { 4395126258Smlaier src = &(*state)->dst; 4396126258Smlaier dst = &(*state)->src; 4397126258Smlaier } 4398126258Smlaier 4399126258Smlaier /* update states */ 4400126258Smlaier if (src->state < PFUDPS_SINGLE) 4401126258Smlaier src->state = PFUDPS_SINGLE; 4402126258Smlaier if (dst->state == PFUDPS_SINGLE) 4403126258Smlaier dst->state = PFUDPS_MULTIPLE; 4404126258Smlaier 4405126258Smlaier /* update expire time */ 4406240233Sglebius (*state)->expire = time_uptime; 4407126258Smlaier if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 4408126258Smlaier (*state)->timeout = PFTM_UDP_MULTIPLE; 4409126258Smlaier else 4410126258Smlaier (*state)->timeout = PFTM_UDP_SINGLE; 4411126258Smlaier 4412126258Smlaier /* translate source/destination address, if necessary */ 4413223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4414223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4415223637Sbz 4416223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4417223637Sbz nk->port[pd->sidx] != uh->uh_sport) 4418293894Sglebius pf_change_ap(m, pd->src, &uh->uh_sport, pd->ip_sum, 4419223637Sbz &uh->uh_sum, &nk->addr[pd->sidx], 4420223637Sbz nk->port[pd->sidx], 1, pd->af); 4421223637Sbz 4422223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4423223637Sbz nk->port[pd->didx] != uh->uh_dport) 4424293894Sglebius pf_change_ap(m, pd->dst, &uh->uh_dport, pd->ip_sum, 4425223637Sbz &uh->uh_sum, &nk->addr[pd->didx], 4426223637Sbz nk->port[pd->didx], 1, pd->af); 4427126261Smlaier m_copyback(m, off, sizeof(*uh), (caddr_t)uh); 4428126258Smlaier } 4429126258Smlaier 4430126258Smlaier return (PF_PASS); 4431126258Smlaier} 4432126258Smlaier 4433240233Sglebiusstatic int 4434130613Smlaierpf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif, 4435145836Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason) 4436126258Smlaier{ 4437223637Sbz struct pf_addr *saddr = pd->src, *daddr = pd->dst; 4438223637Sbz u_int16_t icmpid = 0, *icmpsum; 4439223637Sbz u_int8_t icmptype; 4440130613Smlaier int state_icmp = 0; 4441223637Sbz struct pf_state_key_cmp key; 4442126258Smlaier 4443240233Sglebius bzero(&key, sizeof(key)); 4444126258Smlaier switch (pd->proto) { 4445126258Smlaier#ifdef INET 4446126258Smlaier case IPPROTO_ICMP: 4447126258Smlaier icmptype = pd->hdr.icmp->icmp_type; 4448126258Smlaier icmpid = pd->hdr.icmp->icmp_id; 4449126258Smlaier icmpsum = &pd->hdr.icmp->icmp_cksum; 4450126258Smlaier 4451126258Smlaier if (icmptype == ICMP_UNREACH || 4452126258Smlaier icmptype == ICMP_SOURCEQUENCH || 4453126258Smlaier icmptype == ICMP_REDIRECT || 4454126258Smlaier icmptype == ICMP_TIMXCEED || 4455126258Smlaier icmptype == ICMP_PARAMPROB) 4456126258Smlaier state_icmp++; 4457126258Smlaier break; 4458126258Smlaier#endif /* INET */ 4459126258Smlaier#ifdef INET6 4460126258Smlaier case IPPROTO_ICMPV6: 4461126258Smlaier icmptype = pd->hdr.icmp6->icmp6_type; 4462126258Smlaier icmpid = pd->hdr.icmp6->icmp6_id; 4463126258Smlaier icmpsum = &pd->hdr.icmp6->icmp6_cksum; 4464126258Smlaier 4465126258Smlaier if (icmptype == ICMP6_DST_UNREACH || 4466126258Smlaier icmptype == ICMP6_PACKET_TOO_BIG || 4467126258Smlaier icmptype == ICMP6_TIME_EXCEEDED || 4468126258Smlaier icmptype == ICMP6_PARAM_PROB) 4469126258Smlaier state_icmp++; 4470126258Smlaier break; 4471126258Smlaier#endif /* INET6 */ 4472126258Smlaier } 4473126258Smlaier 4474126258Smlaier if (!state_icmp) { 4475126258Smlaier 4476126258Smlaier /* 4477126258Smlaier * ICMP query/reply message not related to a TCP/UDP packet. 4478126258Smlaier * Search for an ICMP state. 4479126258Smlaier */ 4480126258Smlaier key.af = pd->af; 4481126258Smlaier key.proto = pd->proto; 4482223637Sbz key.port[0] = key.port[1] = icmpid; 4483223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4484223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4485223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4486223637Sbz } else { /* stack side, reverse */ 4487223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4488223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4489130613Smlaier } 4490126258Smlaier 4491240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4492126258Smlaier 4493240233Sglebius (*state)->expire = time_uptime; 4494126258Smlaier (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 4495126258Smlaier 4496126258Smlaier /* translate source/destination address, if necessary */ 4497223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4498223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4499223637Sbz 4500223637Sbz switch (pd->af) { 4501126258Smlaier#ifdef INET 4502223637Sbz case AF_INET: 4503223637Sbz if (PF_ANEQ(pd->src, 4504223637Sbz &nk->addr[pd->sidx], AF_INET)) 4505126258Smlaier pf_change_a(&saddr->v4.s_addr, 4506126258Smlaier pd->ip_sum, 4507223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 4508223637Sbz 4509223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], 4510223637Sbz AF_INET)) 4511223637Sbz pf_change_a(&daddr->v4.s_addr, 4512223637Sbz pd->ip_sum, 4513223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 4514223637Sbz 4515223637Sbz if (nk->port[0] != 4516223637Sbz pd->hdr.icmp->icmp_id) { 4517149884Smlaier pd->hdr.icmp->icmp_cksum = 4518149884Smlaier pf_cksum_fixup( 4519149884Smlaier pd->hdr.icmp->icmp_cksum, icmpid, 4520223637Sbz nk->port[pd->sidx], 0); 4521149884Smlaier pd->hdr.icmp->icmp_id = 4522223637Sbz nk->port[pd->sidx]; 4523223637Sbz } 4524223637Sbz 4525223637Sbz m_copyback(m, off, ICMP_MINLEN, 4526240233Sglebius (caddr_t )pd->hdr.icmp); 4527223637Sbz break; 4528126258Smlaier#endif /* INET */ 4529126258Smlaier#ifdef INET6 4530223637Sbz case AF_INET6: 4531223637Sbz if (PF_ANEQ(pd->src, 4532223637Sbz &nk->addr[pd->sidx], AF_INET6)) 4533126258Smlaier pf_change_a6(saddr, 4534126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4535223637Sbz &nk->addr[pd->sidx], 0); 4536223637Sbz 4537223637Sbz if (PF_ANEQ(pd->dst, 4538223637Sbz &nk->addr[pd->didx], AF_INET6)) 4539126258Smlaier pf_change_a6(daddr, 4540126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4541223637Sbz &nk->addr[pd->didx], 0); 4542223637Sbz 4543240233Sglebius m_copyback(m, off, sizeof(struct icmp6_hdr), 4544240233Sglebius (caddr_t )pd->hdr.icmp6); 4545223637Sbz break; 4546126258Smlaier#endif /* INET6 */ 4547126258Smlaier } 4548126258Smlaier } 4549126258Smlaier return (PF_PASS); 4550126258Smlaier 4551126258Smlaier } else { 4552126258Smlaier /* 4553126258Smlaier * ICMP error message in response to a TCP/UDP packet. 4554126258Smlaier * Extract the inner TCP/UDP header and search for that state. 4555126258Smlaier */ 4556126258Smlaier 4557126258Smlaier struct pf_pdesc pd2; 4558223637Sbz bzero(&pd2, sizeof pd2); 4559126258Smlaier#ifdef INET 4560126258Smlaier struct ip h2; 4561126258Smlaier#endif /* INET */ 4562126258Smlaier#ifdef INET6 4563126258Smlaier struct ip6_hdr h2_6; 4564126258Smlaier int terminal = 0; 4565126258Smlaier#endif /* INET6 */ 4566223637Sbz int ipoff2 = 0; 4567223637Sbz int off2 = 0; 4568126258Smlaier 4569126258Smlaier pd2.af = pd->af; 4570223637Sbz /* Payload packet is from the opposite direction. */ 4571223637Sbz pd2.sidx = (direction == PF_IN) ? 1 : 0; 4572223637Sbz pd2.didx = (direction == PF_IN) ? 0 : 1; 4573126258Smlaier switch (pd->af) { 4574126258Smlaier#ifdef INET 4575126258Smlaier case AF_INET: 4576126258Smlaier /* offset of h2 in mbuf chain */ 4577126258Smlaier ipoff2 = off + ICMP_MINLEN; 4578126258Smlaier 4579126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2), 4580145836Smlaier NULL, reason, pd2.af)) { 4581126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4582126258Smlaier ("pf: ICMP error message too short " 4583126258Smlaier "(ip)\n")); 4584126258Smlaier return (PF_DROP); 4585126258Smlaier } 4586126258Smlaier /* 4587126258Smlaier * ICMP error messages don't refer to non-first 4588126258Smlaier * fragments 4589126258Smlaier */ 4590145836Smlaier if (h2.ip_off & htons(IP_OFFMASK)) { 4591145836Smlaier REASON_SET(reason, PFRES_FRAG); 4592126258Smlaier return (PF_DROP); 4593145836Smlaier } 4594126258Smlaier 4595126258Smlaier /* offset of protocol header that follows h2 */ 4596126258Smlaier off2 = ipoff2 + (h2.ip_hl << 2); 4597126258Smlaier 4598126258Smlaier pd2.proto = h2.ip_p; 4599126258Smlaier pd2.src = (struct pf_addr *)&h2.ip_src; 4600126258Smlaier pd2.dst = (struct pf_addr *)&h2.ip_dst; 4601126258Smlaier pd2.ip_sum = &h2.ip_sum; 4602126258Smlaier break; 4603126258Smlaier#endif /* INET */ 4604126258Smlaier#ifdef INET6 4605126258Smlaier case AF_INET6: 4606126258Smlaier ipoff2 = off + sizeof(struct icmp6_hdr); 4607126258Smlaier 4608126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6), 4609145836Smlaier NULL, reason, pd2.af)) { 4610126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4611126258Smlaier ("pf: ICMP error message too short " 4612126258Smlaier "(ip6)\n")); 4613126258Smlaier return (PF_DROP); 4614126258Smlaier } 4615126258Smlaier pd2.proto = h2_6.ip6_nxt; 4616126258Smlaier pd2.src = (struct pf_addr *)&h2_6.ip6_src; 4617126258Smlaier pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 4618126258Smlaier pd2.ip_sum = NULL; 4619126258Smlaier off2 = ipoff2 + sizeof(h2_6); 4620126258Smlaier do { 4621126258Smlaier switch (pd2.proto) { 4622126258Smlaier case IPPROTO_FRAGMENT: 4623126258Smlaier /* 4624126258Smlaier * ICMPv6 error messages for 4625126258Smlaier * non-first fragments 4626126258Smlaier */ 4627145836Smlaier REASON_SET(reason, PFRES_FRAG); 4628126258Smlaier return (PF_DROP); 4629126258Smlaier case IPPROTO_AH: 4630126258Smlaier case IPPROTO_HOPOPTS: 4631126258Smlaier case IPPROTO_ROUTING: 4632126258Smlaier case IPPROTO_DSTOPTS: { 4633126258Smlaier /* get next header and header length */ 4634126258Smlaier struct ip6_ext opt6; 4635126258Smlaier 4636126258Smlaier if (!pf_pull_hdr(m, off2, &opt6, 4637145836Smlaier sizeof(opt6), NULL, reason, 4638145836Smlaier pd2.af)) { 4639126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4640126258Smlaier ("pf: ICMPv6 short opt\n")); 4641126258Smlaier return (PF_DROP); 4642126258Smlaier } 4643126258Smlaier if (pd2.proto == IPPROTO_AH) 4644126258Smlaier off2 += (opt6.ip6e_len + 2) * 4; 4645126258Smlaier else 4646126258Smlaier off2 += (opt6.ip6e_len + 1) * 8; 4647126258Smlaier pd2.proto = opt6.ip6e_nxt; 4648126258Smlaier /* goto the next header */ 4649126258Smlaier break; 4650126258Smlaier } 4651126258Smlaier default: 4652126258Smlaier terminal++; 4653126258Smlaier break; 4654126258Smlaier } 4655126258Smlaier } while (!terminal); 4656126258Smlaier break; 4657126258Smlaier#endif /* INET6 */ 4658126258Smlaier } 4659126258Smlaier 4660126258Smlaier switch (pd2.proto) { 4661126258Smlaier case IPPROTO_TCP: { 4662126258Smlaier struct tcphdr th; 4663126258Smlaier u_int32_t seq; 4664126258Smlaier struct pf_state_peer *src, *dst; 4665126258Smlaier u_int8_t dws; 4666128129Smlaier int copyback = 0; 4667126258Smlaier 4668126258Smlaier /* 4669126258Smlaier * Only the first 8 bytes of the TCP header can be 4670126258Smlaier * expected. Don't access any TCP header fields after 4671126258Smlaier * th_seq, an ackskew test is not possible. 4672126258Smlaier */ 4673145836Smlaier if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason, 4674145836Smlaier pd2.af)) { 4675126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4676126258Smlaier ("pf: ICMP error message too short " 4677126258Smlaier "(tcp)\n")); 4678126258Smlaier return (PF_DROP); 4679126258Smlaier } 4680126258Smlaier 4681126258Smlaier key.af = pd2.af; 4682126258Smlaier key.proto = IPPROTO_TCP; 4683223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4684223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4685223637Sbz key.port[pd2.sidx] = th.th_sport; 4686223637Sbz key.port[pd2.didx] = th.th_dport; 4687126258Smlaier 4688240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4689126258Smlaier 4690126258Smlaier if (direction == (*state)->direction) { 4691126258Smlaier src = &(*state)->dst; 4692126258Smlaier dst = &(*state)->src; 4693126258Smlaier } else { 4694126258Smlaier src = &(*state)->src; 4695126258Smlaier dst = &(*state)->dst; 4696126258Smlaier } 4697126258Smlaier 4698171929Sdhartmei if (src->wscale && dst->wscale) 4699126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 4700126258Smlaier else 4701126258Smlaier dws = 0; 4702126258Smlaier 4703126258Smlaier /* Demodulate sequence number */ 4704126258Smlaier seq = ntohl(th.th_seq) - src->seqdiff; 4705128129Smlaier if (src->seqdiff) { 4706128129Smlaier pf_change_a(&th.th_seq, icmpsum, 4707126258Smlaier htonl(seq), 0); 4708128129Smlaier copyback = 1; 4709128129Smlaier } 4710126258Smlaier 4711200930Sdelphij if (!((*state)->state_flags & PFSTATE_SLOPPY) && 4712200930Sdelphij (!SEQ_GEQ(src->seqhi, seq) || 4713200930Sdelphij !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 4714223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4715126258Smlaier printf("pf: BAD ICMP %d:%d ", 4716126258Smlaier icmptype, pd->hdr.icmp->icmp_code); 4717126258Smlaier pf_print_host(pd->src, 0, pd->af); 4718126258Smlaier printf(" -> "); 4719126258Smlaier pf_print_host(pd->dst, 0, pd->af); 4720126258Smlaier printf(" state: "); 4721126258Smlaier pf_print_state(*state); 4722126258Smlaier printf(" seq=%u\n", seq); 4723126258Smlaier } 4724145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4725126258Smlaier return (PF_DROP); 4726223637Sbz } else { 4727223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4728223637Sbz printf("pf: OK ICMP %d:%d ", 4729223637Sbz icmptype, pd->hdr.icmp->icmp_code); 4730223637Sbz pf_print_host(pd->src, 0, pd->af); 4731223637Sbz printf(" -> "); 4732223637Sbz pf_print_host(pd->dst, 0, pd->af); 4733223637Sbz printf(" state: "); 4734223637Sbz pf_print_state(*state); 4735223637Sbz printf(" seq=%u\n", seq); 4736223637Sbz } 4737126258Smlaier } 4738126258Smlaier 4739223637Sbz /* translate source/destination address, if necessary */ 4740223637Sbz if ((*state)->key[PF_SK_WIRE] != 4741223637Sbz (*state)->key[PF_SK_STACK]) { 4742223637Sbz struct pf_state_key *nk = 4743223637Sbz (*state)->key[pd->didx]; 4744223637Sbz 4745223637Sbz if (PF_ANEQ(pd2.src, 4746223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4747223637Sbz nk->port[pd2.sidx] != th.th_sport) 4748126258Smlaier pf_change_icmp(pd2.src, &th.th_sport, 4749223637Sbz daddr, &nk->addr[pd2.sidx], 4750223637Sbz nk->port[pd2.sidx], NULL, 4751126258Smlaier pd2.ip_sum, icmpsum, 4752126258Smlaier pd->ip_sum, 0, pd2.af); 4753223637Sbz 4754223637Sbz if (PF_ANEQ(pd2.dst, 4755223637Sbz &nk->addr[pd2.didx], pd2.af) || 4756223637Sbz nk->port[pd2.didx] != th.th_dport) 4757126258Smlaier pf_change_icmp(pd2.dst, &th.th_dport, 4758223637Sbz NULL, /* XXX Inbound NAT? */ 4759223637Sbz &nk->addr[pd2.didx], 4760223637Sbz nk->port[pd2.didx], NULL, 4761126258Smlaier pd2.ip_sum, icmpsum, 4762126258Smlaier pd->ip_sum, 0, pd2.af); 4763128129Smlaier copyback = 1; 4764128129Smlaier } 4765128129Smlaier 4766128129Smlaier if (copyback) { 4767126258Smlaier switch (pd2.af) { 4768126258Smlaier#ifdef INET 4769126258Smlaier case AF_INET: 4770126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4771240233Sglebius (caddr_t )pd->hdr.icmp); 4772126258Smlaier m_copyback(m, ipoff2, sizeof(h2), 4773240233Sglebius (caddr_t )&h2); 4774126258Smlaier break; 4775126258Smlaier#endif /* INET */ 4776126258Smlaier#ifdef INET6 4777126258Smlaier case AF_INET6: 4778126258Smlaier m_copyback(m, off, 4779126258Smlaier sizeof(struct icmp6_hdr), 4780240233Sglebius (caddr_t )pd->hdr.icmp6); 4781126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4782240233Sglebius (caddr_t )&h2_6); 4783126258Smlaier break; 4784126258Smlaier#endif /* INET6 */ 4785126258Smlaier } 4786126261Smlaier m_copyback(m, off2, 8, (caddr_t)&th); 4787126258Smlaier } 4788126258Smlaier 4789126258Smlaier return (PF_PASS); 4790126258Smlaier break; 4791126258Smlaier } 4792126258Smlaier case IPPROTO_UDP: { 4793126258Smlaier struct udphdr uh; 4794126258Smlaier 4795126258Smlaier if (!pf_pull_hdr(m, off2, &uh, sizeof(uh), 4796145836Smlaier NULL, reason, pd2.af)) { 4797126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4798126258Smlaier ("pf: ICMP error message too short " 4799126258Smlaier "(udp)\n")); 4800126258Smlaier return (PF_DROP); 4801126258Smlaier } 4802126258Smlaier 4803126258Smlaier key.af = pd2.af; 4804126258Smlaier key.proto = IPPROTO_UDP; 4805223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4806223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4807223637Sbz key.port[pd2.sidx] = uh.uh_sport; 4808223637Sbz key.port[pd2.didx] = uh.uh_dport; 4809126258Smlaier 4810240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4811126258Smlaier 4812223637Sbz /* translate source/destination address, if necessary */ 4813223637Sbz if ((*state)->key[PF_SK_WIRE] != 4814223637Sbz (*state)->key[PF_SK_STACK]) { 4815223637Sbz struct pf_state_key *nk = 4816223637Sbz (*state)->key[pd->didx]; 4817223637Sbz 4818223637Sbz if (PF_ANEQ(pd2.src, 4819223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4820223637Sbz nk->port[pd2.sidx] != uh.uh_sport) 4821126258Smlaier pf_change_icmp(pd2.src, &uh.uh_sport, 4822223637Sbz daddr, &nk->addr[pd2.sidx], 4823223637Sbz nk->port[pd2.sidx], &uh.uh_sum, 4824126258Smlaier pd2.ip_sum, icmpsum, 4825126258Smlaier pd->ip_sum, 1, pd2.af); 4826223637Sbz 4827223637Sbz if (PF_ANEQ(pd2.dst, 4828223637Sbz &nk->addr[pd2.didx], pd2.af) || 4829223637Sbz nk->port[pd2.didx] != uh.uh_dport) 4830126258Smlaier pf_change_icmp(pd2.dst, &uh.uh_dport, 4831223637Sbz NULL, /* XXX Inbound NAT? */ 4832223637Sbz &nk->addr[pd2.didx], 4833223637Sbz nk->port[pd2.didx], &uh.uh_sum, 4834126258Smlaier pd2.ip_sum, icmpsum, 4835126258Smlaier pd->ip_sum, 1, pd2.af); 4836223637Sbz 4837126258Smlaier switch (pd2.af) { 4838126258Smlaier#ifdef INET 4839126258Smlaier case AF_INET: 4840126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4841240233Sglebius (caddr_t )pd->hdr.icmp); 4842223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4843126258Smlaier break; 4844126258Smlaier#endif /* INET */ 4845126258Smlaier#ifdef INET6 4846126258Smlaier case AF_INET6: 4847126258Smlaier m_copyback(m, off, 4848126258Smlaier sizeof(struct icmp6_hdr), 4849240233Sglebius (caddr_t )pd->hdr.icmp6); 4850126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4851240233Sglebius (caddr_t )&h2_6); 4852126258Smlaier break; 4853126258Smlaier#endif /* INET6 */ 4854126258Smlaier } 4855223637Sbz m_copyback(m, off2, sizeof(uh), (caddr_t)&uh); 4856126258Smlaier } 4857126258Smlaier return (PF_PASS); 4858126258Smlaier break; 4859126258Smlaier } 4860126258Smlaier#ifdef INET 4861126258Smlaier case IPPROTO_ICMP: { 4862126258Smlaier struct icmp iih; 4863126258Smlaier 4864126258Smlaier if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN, 4865145836Smlaier NULL, reason, pd2.af)) { 4866126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4867126258Smlaier ("pf: ICMP error message too short i" 4868126258Smlaier "(icmp)\n")); 4869126258Smlaier return (PF_DROP); 4870126258Smlaier } 4871126258Smlaier 4872126258Smlaier key.af = pd2.af; 4873126258Smlaier key.proto = IPPROTO_ICMP; 4874223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4875223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4876223637Sbz key.port[0] = key.port[1] = iih.icmp_id; 4877126258Smlaier 4878240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4879126258Smlaier 4880223637Sbz /* translate source/destination address, if necessary */ 4881223637Sbz if ((*state)->key[PF_SK_WIRE] != 4882223637Sbz (*state)->key[PF_SK_STACK]) { 4883223637Sbz struct pf_state_key *nk = 4884223637Sbz (*state)->key[pd->didx]; 4885223637Sbz 4886223637Sbz if (PF_ANEQ(pd2.src, 4887223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4888223637Sbz nk->port[pd2.sidx] != iih.icmp_id) 4889126258Smlaier pf_change_icmp(pd2.src, &iih.icmp_id, 4890223637Sbz daddr, &nk->addr[pd2.sidx], 4891223637Sbz nk->port[pd2.sidx], NULL, 4892126258Smlaier pd2.ip_sum, icmpsum, 4893126258Smlaier pd->ip_sum, 0, AF_INET); 4894223637Sbz 4895223637Sbz if (PF_ANEQ(pd2.dst, 4896223637Sbz &nk->addr[pd2.didx], pd2.af) || 4897223637Sbz nk->port[pd2.didx] != iih.icmp_id) 4898126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp_id, 4899223637Sbz NULL, /* XXX Inbound NAT? */ 4900223637Sbz &nk->addr[pd2.didx], 4901223637Sbz nk->port[pd2.didx], NULL, 4902126258Smlaier pd2.ip_sum, icmpsum, 4903126258Smlaier pd->ip_sum, 0, AF_INET); 4904223637Sbz 4905223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 4906223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4907223637Sbz m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih); 4908126258Smlaier } 4909126258Smlaier return (PF_PASS); 4910126258Smlaier break; 4911126258Smlaier } 4912126258Smlaier#endif /* INET */ 4913126258Smlaier#ifdef INET6 4914126258Smlaier case IPPROTO_ICMPV6: { 4915126258Smlaier struct icmp6_hdr iih; 4916126258Smlaier 4917126258Smlaier if (!pf_pull_hdr(m, off2, &iih, 4918145836Smlaier sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 4919126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4920126258Smlaier ("pf: ICMP error message too short " 4921126258Smlaier "(icmp6)\n")); 4922126258Smlaier return (PF_DROP); 4923126258Smlaier } 4924126258Smlaier 4925126258Smlaier key.af = pd2.af; 4926126258Smlaier key.proto = IPPROTO_ICMPV6; 4927223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4928223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4929223637Sbz key.port[0] = key.port[1] = iih.icmp6_id; 4930126258Smlaier 4931240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4932126258Smlaier 4933223637Sbz /* translate source/destination address, if necessary */ 4934223637Sbz if ((*state)->key[PF_SK_WIRE] != 4935223637Sbz (*state)->key[PF_SK_STACK]) { 4936223637Sbz struct pf_state_key *nk = 4937223637Sbz (*state)->key[pd->didx]; 4938223637Sbz 4939223637Sbz if (PF_ANEQ(pd2.src, 4940223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4941223637Sbz nk->port[pd2.sidx] != iih.icmp6_id) 4942126258Smlaier pf_change_icmp(pd2.src, &iih.icmp6_id, 4943223637Sbz daddr, &nk->addr[pd2.sidx], 4944223637Sbz nk->port[pd2.sidx], NULL, 4945126258Smlaier pd2.ip_sum, icmpsum, 4946126258Smlaier pd->ip_sum, 0, AF_INET6); 4947223637Sbz 4948223637Sbz if (PF_ANEQ(pd2.dst, 4949223637Sbz &nk->addr[pd2.didx], pd2.af) || 4950223637Sbz nk->port[pd2.didx] != iih.icmp6_id) 4951126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp6_id, 4952223637Sbz NULL, /* XXX Inbound NAT? */ 4953223637Sbz &nk->addr[pd2.didx], 4954223637Sbz nk->port[pd2.didx], NULL, 4955126258Smlaier pd2.ip_sum, icmpsum, 4956126258Smlaier pd->ip_sum, 0, AF_INET6); 4957223637Sbz 4958126258Smlaier m_copyback(m, off, sizeof(struct icmp6_hdr), 4959126261Smlaier (caddr_t)pd->hdr.icmp6); 4960223637Sbz m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 4961126258Smlaier m_copyback(m, off2, sizeof(struct icmp6_hdr), 4962126261Smlaier (caddr_t)&iih); 4963126258Smlaier } 4964126258Smlaier return (PF_PASS); 4965126258Smlaier break; 4966126258Smlaier } 4967126258Smlaier#endif /* INET6 */ 4968126258Smlaier default: { 4969126258Smlaier key.af = pd2.af; 4970126258Smlaier key.proto = pd2.proto; 4971223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4972223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4973223637Sbz key.port[0] = key.port[1] = 0; 4974126258Smlaier 4975240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4976126258Smlaier 4977223637Sbz /* translate source/destination address, if necessary */ 4978223637Sbz if ((*state)->key[PF_SK_WIRE] != 4979223637Sbz (*state)->key[PF_SK_STACK]) { 4980223637Sbz struct pf_state_key *nk = 4981223637Sbz (*state)->key[pd->didx]; 4982223637Sbz 4983223637Sbz if (PF_ANEQ(pd2.src, 4984223637Sbz &nk->addr[pd2.sidx], pd2.af)) 4985223637Sbz pf_change_icmp(pd2.src, NULL, daddr, 4986223637Sbz &nk->addr[pd2.sidx], 0, NULL, 4987126258Smlaier pd2.ip_sum, icmpsum, 4988126258Smlaier pd->ip_sum, 0, pd2.af); 4989223637Sbz 4990223637Sbz if (PF_ANEQ(pd2.dst, 4991223637Sbz &nk->addr[pd2.didx], pd2.af)) 4992223637Sbz pf_change_icmp(pd2.src, NULL, 4993223637Sbz NULL, /* XXX Inbound NAT? */ 4994223637Sbz &nk->addr[pd2.didx], 0, NULL, 4995126258Smlaier pd2.ip_sum, icmpsum, 4996126258Smlaier pd->ip_sum, 0, pd2.af); 4997223637Sbz 4998126258Smlaier switch (pd2.af) { 4999126258Smlaier#ifdef INET 5000126258Smlaier case AF_INET: 5001126258Smlaier m_copyback(m, off, ICMP_MINLEN, 5002126261Smlaier (caddr_t)pd->hdr.icmp); 5003223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 5004126258Smlaier break; 5005126258Smlaier#endif /* INET */ 5006126258Smlaier#ifdef INET6 5007126258Smlaier case AF_INET6: 5008126258Smlaier m_copyback(m, off, 5009126258Smlaier sizeof(struct icmp6_hdr), 5010240233Sglebius (caddr_t )pd->hdr.icmp6); 5011126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 5012240233Sglebius (caddr_t )&h2_6); 5013126258Smlaier break; 5014126258Smlaier#endif /* INET6 */ 5015126258Smlaier } 5016126258Smlaier } 5017126258Smlaier return (PF_PASS); 5018126258Smlaier break; 5019126258Smlaier } 5020126258Smlaier } 5021126258Smlaier } 5022126258Smlaier} 5023126258Smlaier 5024240233Sglebiusstatic int 5025130613Smlaierpf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif, 5026223637Sbz struct mbuf *m, struct pf_pdesc *pd) 5027126258Smlaier{ 5028126258Smlaier struct pf_state_peer *src, *dst; 5029223637Sbz struct pf_state_key_cmp key; 5030126258Smlaier 5031240233Sglebius bzero(&key, sizeof(key)); 5032126258Smlaier key.af = pd->af; 5033126258Smlaier key.proto = pd->proto; 5034130613Smlaier if (direction == PF_IN) { 5035223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 5036223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 5037223637Sbz key.port[0] = key.port[1] = 0; 5038130613Smlaier } else { 5039223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 5040223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 5041223637Sbz key.port[1] = key.port[0] = 0; 5042130613Smlaier } 5043126258Smlaier 5044240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 5045126258Smlaier 5046126258Smlaier if (direction == (*state)->direction) { 5047126258Smlaier src = &(*state)->src; 5048126258Smlaier dst = &(*state)->dst; 5049126258Smlaier } else { 5050126258Smlaier src = &(*state)->dst; 5051126258Smlaier dst = &(*state)->src; 5052126258Smlaier } 5053126258Smlaier 5054126258Smlaier /* update states */ 5055126258Smlaier if (src->state < PFOTHERS_SINGLE) 5056126258Smlaier src->state = PFOTHERS_SINGLE; 5057126258Smlaier if (dst->state == PFOTHERS_SINGLE) 5058126258Smlaier dst->state = PFOTHERS_MULTIPLE; 5059126258Smlaier 5060126258Smlaier /* update expire time */ 5061240233Sglebius (*state)->expire = time_uptime; 5062126258Smlaier if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 5063126258Smlaier (*state)->timeout = PFTM_OTHER_MULTIPLE; 5064126258Smlaier else 5065126258Smlaier (*state)->timeout = PFTM_OTHER_SINGLE; 5066126258Smlaier 5067126258Smlaier /* translate source/destination address, if necessary */ 5068223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 5069223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 5070223637Sbz 5071240233Sglebius KASSERT(nk, ("%s: nk is null", __func__)); 5072240233Sglebius KASSERT(pd, ("%s: pd is null", __func__)); 5073240233Sglebius KASSERT(pd->src, ("%s: pd->src is null", __func__)); 5074240233Sglebius KASSERT(pd->dst, ("%s: pd->dst is null", __func__)); 5075223637Sbz switch (pd->af) { 5076126258Smlaier#ifdef INET 5077223637Sbz case AF_INET: 5078223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5079126258Smlaier pf_change_a(&pd->src->v4.s_addr, 5080223637Sbz pd->ip_sum, 5081223637Sbz nk->addr[pd->sidx].v4.s_addr, 5082126258Smlaier 0); 5083223637Sbz 5084223637Sbz 5085223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5086126258Smlaier pf_change_a(&pd->dst->v4.s_addr, 5087223637Sbz pd->ip_sum, 5088223637Sbz nk->addr[pd->didx].v4.s_addr, 5089126258Smlaier 0); 5090223637Sbz 5091126258Smlaier break; 5092126258Smlaier#endif /* INET */ 5093126258Smlaier#ifdef INET6 5094223637Sbz case AF_INET6: 5095223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5096223637Sbz PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af); 5097223637Sbz 5098223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5099223637Sbz PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af); 5100126258Smlaier#endif /* INET6 */ 5101223637Sbz } 5102126258Smlaier } 5103126258Smlaier return (PF_PASS); 5104126258Smlaier} 5105126258Smlaier 5106126258Smlaier/* 5107126258Smlaier * ipoff and off are measured from the start of the mbuf chain. 5108126258Smlaier * h must be at "ipoff" on the mbuf chain. 5109126258Smlaier */ 5110126258Smlaiervoid * 5111126258Smlaierpf_pull_hdr(struct mbuf *m, int off, void *p, int len, 5112126258Smlaier u_short *actionp, u_short *reasonp, sa_family_t af) 5113126258Smlaier{ 5114126258Smlaier switch (af) { 5115126258Smlaier#ifdef INET 5116126258Smlaier case AF_INET: { 5117126258Smlaier struct ip *h = mtod(m, struct ip *); 5118126258Smlaier u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 5119126258Smlaier 5120126258Smlaier if (fragoff) { 5121126258Smlaier if (fragoff >= len) 5122126258Smlaier ACTION_SET(actionp, PF_PASS); 5123126258Smlaier else { 5124126258Smlaier ACTION_SET(actionp, PF_DROP); 5125126258Smlaier REASON_SET(reasonp, PFRES_FRAG); 5126126258Smlaier } 5127126258Smlaier return (NULL); 5128126258Smlaier } 5129130613Smlaier if (m->m_pkthdr.len < off + len || 5130130613Smlaier ntohs(h->ip_len) < off + len) { 5131126258Smlaier ACTION_SET(actionp, PF_DROP); 5132126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5133126258Smlaier return (NULL); 5134126258Smlaier } 5135126258Smlaier break; 5136126258Smlaier } 5137126258Smlaier#endif /* INET */ 5138126258Smlaier#ifdef INET6 5139126258Smlaier case AF_INET6: { 5140126258Smlaier struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 5141126258Smlaier 5142126258Smlaier if (m->m_pkthdr.len < off + len || 5143126258Smlaier (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) < 5144126258Smlaier (unsigned)(off + len)) { 5145126258Smlaier ACTION_SET(actionp, PF_DROP); 5146126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5147126258Smlaier return (NULL); 5148126258Smlaier } 5149126258Smlaier break; 5150126258Smlaier } 5151126258Smlaier#endif /* INET6 */ 5152126258Smlaier } 5153126258Smlaier m_copydata(m, off, len, p); 5154126258Smlaier return (p); 5155126258Smlaier} 5156126258Smlaier 5157126258Smlaierint 5158231852Sbzpf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif, 5159231852Sbz int rtableid) 5160126258Smlaier{ 5161223637Sbz#ifdef RADIX_MPATH 5162223637Sbz struct radix_node_head *rnh; 5163223637Sbz#endif 5164126258Smlaier struct sockaddr_in *dst; 5165171168Smlaier int ret = 1; 5166171168Smlaier int check_mpath; 5167145836Smlaier#ifdef INET6 5168145836Smlaier struct sockaddr_in6 *dst6; 5169145836Smlaier struct route_in6 ro; 5170145836Smlaier#else 5171126258Smlaier struct route ro; 5172145836Smlaier#endif 5173171168Smlaier struct radix_node *rn; 5174171168Smlaier struct rtentry *rt; 5175171168Smlaier struct ifnet *ifp; 5176126258Smlaier 5177171168Smlaier check_mpath = 0; 5178223637Sbz#ifdef RADIX_MPATH 5179223637Sbz /* XXX: stick to table 0 for now */ 5180223637Sbz rnh = rt_tables_get_rnh(0, af); 5181223637Sbz if (rnh != NULL && rn_mpath_capable(rnh)) 5182223637Sbz check_mpath = 1; 5183223637Sbz#endif 5184126258Smlaier bzero(&ro, sizeof(ro)); 5185145836Smlaier switch (af) { 5186145836Smlaier case AF_INET: 5187145836Smlaier dst = satosin(&ro.ro_dst); 5188145836Smlaier dst->sin_family = AF_INET; 5189145836Smlaier dst->sin_len = sizeof(*dst); 5190145836Smlaier dst->sin_addr = addr->v4; 5191145836Smlaier break; 5192145836Smlaier#ifdef INET6 5193145836Smlaier case AF_INET6: 5194223637Sbz /* 5195223637Sbz * Skip check for addresses with embedded interface scope, 5196223637Sbz * as they would always match anyway. 5197223637Sbz */ 5198223637Sbz if (IN6_IS_SCOPE_EMBED(&addr->v6)) 5199223637Sbz goto out; 5200145836Smlaier dst6 = (struct sockaddr_in6 *)&ro.ro_dst; 5201145836Smlaier dst6->sin6_family = AF_INET6; 5202145836Smlaier dst6->sin6_len = sizeof(*dst6); 5203145836Smlaier dst6->sin6_addr = addr->v6; 5204145836Smlaier break; 5205145836Smlaier#endif /* INET6 */ 5206145836Smlaier default: 5207145836Smlaier return (0); 5208145836Smlaier } 5209145836Smlaier 5210171168Smlaier /* Skip checks for ipsec interfaces */ 5211171168Smlaier if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 5212171168Smlaier goto out; 5213171168Smlaier 5214231852Sbz switch (af) { 5215231852Sbz#ifdef INET6 5216231852Sbz case AF_INET6: 5217231852Sbz in6_rtalloc_ign(&ro, 0, rtableid); 5218231852Sbz break; 5219231852Sbz#endif 5220222529Sbz#ifdef INET 5221231852Sbz case AF_INET: 5222231852Sbz in_rtalloc_ign((struct route *)&ro, 0, rtableid); 5223231852Sbz break; 5224222529Sbz#endif 5225231852Sbz default: 5226231852Sbz rtalloc_ign((struct route *)&ro, 0); /* No/default FIB. */ 5227231852Sbz break; 5228231852Sbz } 5229126258Smlaier 5230126258Smlaier if (ro.ro_rt != NULL) { 5231171168Smlaier /* No interface given, this is a no-route check */ 5232171168Smlaier if (kif == NULL) 5233171168Smlaier goto out; 5234171168Smlaier 5235171168Smlaier if (kif->pfik_ifp == NULL) { 5236171168Smlaier ret = 0; 5237171168Smlaier goto out; 5238171168Smlaier } 5239171168Smlaier 5240171168Smlaier /* Perform uRPF check if passed input interface */ 5241171168Smlaier ret = 0; 5242171168Smlaier rn = (struct radix_node *)ro.ro_rt; 5243171168Smlaier do { 5244171168Smlaier rt = (struct rtentry *)rn; 5245240233Sglebius ifp = rt->rt_ifp; 5246171168Smlaier 5247171168Smlaier if (kif->pfik_ifp == ifp) 5248171168Smlaier ret = 1; 5249223637Sbz#ifdef RADIX_MPATH 5250171168Smlaier rn = rn_mpath_next(rn); 5251171168Smlaier#endif 5252171168Smlaier } while (check_mpath == 1 && rn != NULL && ret == 0); 5253171168Smlaier } else 5254171168Smlaier ret = 0; 5255171168Smlaierout: 5256171168Smlaier if (ro.ro_rt != NULL) 5257126258Smlaier RTFREE(ro.ro_rt); 5258171168Smlaier return (ret); 5259145836Smlaier} 5260145836Smlaier 5261222529Sbz#ifdef INET 5262240233Sglebiusstatic void 5263126258Smlaierpf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5264171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5265126258Smlaier{ 5266126258Smlaier struct mbuf *m0, *m1; 5267240233Sglebius struct sockaddr_in dst; 5268126258Smlaier struct ip *ip; 5269126258Smlaier struct ifnet *ifp = NULL; 5270126258Smlaier struct pf_addr naddr; 5271130613Smlaier struct pf_src_node *sn = NULL; 5272126258Smlaier int error = 0; 5273242161Sglebius uint16_t ip_len, ip_off; 5274126258Smlaier 5275240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5276240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5277240233Sglebius __func__)); 5278126258Smlaier 5279240233Sglebius if ((pd->pf_mtag == NULL && 5280240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5281240233Sglebius pd->pf_mtag->routed++ > 3) { 5282171168Smlaier m0 = *m; 5283171168Smlaier *m = NULL; 5284240233Sglebius goto bad_locked; 5285132303Smlaier } 5286132303Smlaier 5287126258Smlaier if (r->rt == PF_DUPTO) { 5288240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5289240233Sglebius if (s) 5290240233Sglebius PF_STATE_UNLOCK(s); 5291126258Smlaier return; 5292240233Sglebius } 5293126258Smlaier } else { 5294240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5295240233Sglebius if (s) 5296240233Sglebius PF_STATE_UNLOCK(s); 5297126258Smlaier return; 5298240233Sglebius } 5299126258Smlaier m0 = *m; 5300126258Smlaier } 5301126258Smlaier 5302126258Smlaier ip = mtod(m0, struct ip *); 5303126258Smlaier 5304240233Sglebius bzero(&dst, sizeof(dst)); 5305240233Sglebius dst.sin_family = AF_INET; 5306240233Sglebius dst.sin_len = sizeof(dst); 5307240233Sglebius dst.sin_addr = ip->ip_dst; 5308126258Smlaier 5309126258Smlaier if (r->rt == PF_FASTROUTE) { 5310240233Sglebius struct rtentry *rt; 5311240233Sglebius 5312240233Sglebius if (s) 5313240233Sglebius PF_STATE_UNLOCK(s); 5314240233Sglebius rt = rtalloc1_fib(sintosa(&dst), 0, 0, M_GETFIB(m0)); 5315240233Sglebius if (rt == NULL) { 5316196039Srwatson KMOD_IPSTAT_INC(ips_noroute); 5317240233Sglebius error = EHOSTUNREACH; 5318126258Smlaier goto bad; 5319126258Smlaier } 5320126258Smlaier 5321240233Sglebius ifp = rt->rt_ifp; 5322263478Sglebius counter_u64_add(rt->rt_pksent, 1); 5323126258Smlaier 5324240233Sglebius if (rt->rt_flags & RTF_GATEWAY) 5325240233Sglebius bcopy(satosin(rt->rt_gateway), &dst, sizeof(dst)); 5326240233Sglebius RTFREE_LOCKED(rt); 5327126258Smlaier } else { 5328145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5329145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5330240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5331240233Sglebius goto bad_locked; 5332145836Smlaier } 5333126258Smlaier if (s == NULL) { 5334130613Smlaier pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src, 5335130613Smlaier &naddr, NULL, &sn); 5336126258Smlaier if (!PF_AZERO(&naddr, AF_INET)) 5337240233Sglebius dst.sin_addr.s_addr = naddr.v4.s_addr; 5338130613Smlaier ifp = r->rpool.cur->kif ? 5339130613Smlaier r->rpool.cur->kif->pfik_ifp : NULL; 5340126258Smlaier } else { 5341126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET)) 5342240233Sglebius dst.sin_addr.s_addr = 5343126258Smlaier s->rt_addr.v4.s_addr; 5344130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5345240233Sglebius PF_STATE_UNLOCK(s); 5346126258Smlaier } 5347126258Smlaier } 5348126258Smlaier if (ifp == NULL) 5349126258Smlaier goto bad; 5350126258Smlaier 5351130639Smlaier if (oifp != ifp) { 5352145836Smlaier if (pf_test(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5353126258Smlaier goto bad; 5354126258Smlaier else if (m0 == NULL) 5355126258Smlaier goto done; 5356145836Smlaier if (m0->m_len < sizeof(struct ip)) { 5357145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5358240233Sglebius ("%s: m0->m_len < sizeof(struct ip)\n", __func__)); 5359145836Smlaier goto bad; 5360145836Smlaier } 5361126258Smlaier ip = mtod(m0, struct ip *); 5362126258Smlaier } 5363126258Smlaier 5364240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5365240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5366240233Sglebius 5367241344Sglebius ip_len = ntohs(ip->ip_len); 5368241344Sglebius ip_off = ntohs(ip->ip_off); 5369240233Sglebius 5370240233Sglebius /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 5371126261Smlaier m0->m_pkthdr.csum_flags |= CSUM_IP; 5372242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 5373126261Smlaier in_delayed_cksum(m0); 5374242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 5375126261Smlaier } 5376240233Sglebius#ifdef SCTP 5377242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5378240233Sglebius sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 5379242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5380240233Sglebius } 5381240233Sglebius#endif 5382126261Smlaier 5383130613Smlaier /* 5384240233Sglebius * If small enough for interface, or the interface will take 5385240233Sglebius * care of the fragmentation for us, we can just send directly. 5386130613Smlaier */ 5387241344Sglebius if (ip_len <= ifp->if_mtu || 5388240233Sglebius (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 5389241344Sglebius ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 5390223637Sbz ip->ip_sum = 0; 5391242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 5392126258Smlaier ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 5393242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_IP; 5394242161Sglebius } 5395254523Sandre m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 5396240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5397126258Smlaier goto done; 5398126258Smlaier } 5399223637Sbz 5400240233Sglebius /* Balk when DF bit is set or the interface didn't support TSO. */ 5401241344Sglebius if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 5402240233Sglebius error = EMSGSIZE; 5403196039Srwatson KMOD_IPSTAT_INC(ips_cantfrag); 5404126258Smlaier if (r->rt != PF_DUPTO) { 5405126258Smlaier icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, 5406145886Smlaier ifp->if_mtu); 5407126258Smlaier goto done; 5408126258Smlaier } else 5409126258Smlaier goto bad; 5410126258Smlaier } 5411126258Smlaier 5412242161Sglebius error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 5413240233Sglebius if (error) 5414126258Smlaier goto bad; 5415126258Smlaier 5416240233Sglebius for (; m0; m0 = m1) { 5417126258Smlaier m1 = m0->m_nextpkt; 5418240233Sglebius m0->m_nextpkt = NULL; 5419126261Smlaier if (error == 0) { 5420254523Sandre m_clrprotoflags(m0); 5421240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5422126261Smlaier } else 5423126258Smlaier m_freem(m0); 5424126258Smlaier } 5425126258Smlaier 5426126258Smlaier if (error == 0) 5427196039Srwatson KMOD_IPSTAT_INC(ips_fragmented); 5428126258Smlaier 5429126258Smlaierdone: 5430126258Smlaier if (r->rt != PF_DUPTO) 5431126258Smlaier *m = NULL; 5432126258Smlaier return; 5433126258Smlaier 5434240233Sglebiusbad_locked: 5435240233Sglebius if (s) 5436240233Sglebius PF_STATE_UNLOCK(s); 5437126258Smlaierbad: 5438126258Smlaier m_freem(m0); 5439126258Smlaier goto done; 5440126258Smlaier} 5441126258Smlaier#endif /* INET */ 5442126258Smlaier 5443126258Smlaier#ifdef INET6 5444240233Sglebiusstatic void 5445126258Smlaierpf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5446171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5447126258Smlaier{ 5448126258Smlaier struct mbuf *m0; 5449240233Sglebius struct sockaddr_in6 dst; 5450126258Smlaier struct ip6_hdr *ip6; 5451126258Smlaier struct ifnet *ifp = NULL; 5452126258Smlaier struct pf_addr naddr; 5453130613Smlaier struct pf_src_node *sn = NULL; 5454126258Smlaier 5455240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5456240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5457240233Sglebius __func__)); 5458126258Smlaier 5459240233Sglebius if ((pd->pf_mtag == NULL && 5460240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5461240233Sglebius pd->pf_mtag->routed++ > 3) { 5462171168Smlaier m0 = *m; 5463171168Smlaier *m = NULL; 5464240233Sglebius goto bad_locked; 5465132303Smlaier } 5466132303Smlaier 5467126258Smlaier if (r->rt == PF_DUPTO) { 5468240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5469240233Sglebius if (s) 5470240233Sglebius PF_STATE_UNLOCK(s); 5471126258Smlaier return; 5472240233Sglebius } 5473126258Smlaier } else { 5474240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5475240233Sglebius if (s) 5476240233Sglebius PF_STATE_UNLOCK(s); 5477126258Smlaier return; 5478240233Sglebius } 5479126258Smlaier m0 = *m; 5480126258Smlaier } 5481126258Smlaier 5482126258Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5483126258Smlaier 5484240233Sglebius bzero(&dst, sizeof(dst)); 5485240233Sglebius dst.sin6_family = AF_INET6; 5486240233Sglebius dst.sin6_len = sizeof(dst); 5487240233Sglebius dst.sin6_addr = ip6->ip6_dst; 5488126258Smlaier 5489171168Smlaier /* Cheat. XXX why only in the v6 case??? */ 5490126258Smlaier if (r->rt == PF_FASTROUTE) { 5491240233Sglebius if (s) 5492240233Sglebius PF_STATE_UNLOCK(s); 5493132280Smlaier m0->m_flags |= M_SKIP_FIREWALL; 5494126261Smlaier ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL); 5495126258Smlaier return; 5496126258Smlaier } 5497126258Smlaier 5498145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5499145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5500240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5501240233Sglebius goto bad_locked; 5502145836Smlaier } 5503126258Smlaier if (s == NULL) { 5504130613Smlaier pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src, 5505130613Smlaier &naddr, NULL, &sn); 5506126258Smlaier if (!PF_AZERO(&naddr, AF_INET6)) 5507240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5508126258Smlaier &naddr, AF_INET6); 5509130613Smlaier ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; 5510126258Smlaier } else { 5511126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET6)) 5512240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5513126258Smlaier &s->rt_addr, AF_INET6); 5514130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5515126258Smlaier } 5516240233Sglebius 5517240233Sglebius if (s) 5518240233Sglebius PF_STATE_UNLOCK(s); 5519240233Sglebius 5520126258Smlaier if (ifp == NULL) 5521126258Smlaier goto bad; 5522126258Smlaier 5523126258Smlaier if (oifp != ifp) { 5524145836Smlaier if (pf_test6(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5525132303Smlaier goto bad; 5526132303Smlaier else if (m0 == NULL) 5527132303Smlaier goto done; 5528145836Smlaier if (m0->m_len < sizeof(struct ip6_hdr)) { 5529145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5530240233Sglebius ("%s: m0->m_len < sizeof(struct ip6_hdr)\n", 5531240233Sglebius __func__)); 5532145836Smlaier goto bad; 5533145836Smlaier } 5534132303Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5535126258Smlaier } 5536126258Smlaier 5537240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5538240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5539240233Sglebius 5540293894Sglebius if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6 & 5541293894Sglebius ~ifp->if_hwassist) { 5542293894Sglebius uint32_t plen = m0->m_pkthdr.len - sizeof(*ip6); 5543293894Sglebius in6_delayed_cksum(m0, plen, sizeof(struct ip6_hdr)); 5544293894Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6; 5545293894Sglebius } 5546293894Sglebius 5547126258Smlaier /* 5548126258Smlaier * If the packet is too large for the outgoing interface, 5549126258Smlaier * send back an icmp6 error. 5550126258Smlaier */ 5551240233Sglebius if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 5552240233Sglebius dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 5553240233Sglebius if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) 5554240233Sglebius nd6_output(ifp, ifp, m0, &dst, NULL); 5555240233Sglebius else { 5556126258Smlaier in6_ifstat_inc(ifp, ifs6_in_toobig); 5557126258Smlaier if (r->rt != PF_DUPTO) 5558126258Smlaier icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); 5559126258Smlaier else 5560126258Smlaier goto bad; 5561126258Smlaier } 5562126258Smlaier 5563126258Smlaierdone: 5564126258Smlaier if (r->rt != PF_DUPTO) 5565126258Smlaier *m = NULL; 5566126258Smlaier return; 5567126258Smlaier 5568240233Sglebiusbad_locked: 5569240233Sglebius if (s) 5570240233Sglebius PF_STATE_UNLOCK(s); 5571126258Smlaierbad: 5572126258Smlaier m_freem(m0); 5573126258Smlaier goto done; 5574126258Smlaier} 5575126258Smlaier#endif /* INET6 */ 5576126258Smlaier 5577126258Smlaier/* 5578132566Smlaier * FreeBSD supports cksum offloads for the following drivers. 5579137413Sru * em(4), fxp(4), ixgb(4), lge(4), ndis(4), nge(4), re(4), 5580132566Smlaier * ti(4), txp(4), xl(4) 5581132566Smlaier * 5582132566Smlaier * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 5583132566Smlaier * network driver performed cksum including pseudo header, need to verify 5584132566Smlaier * csum_data 5585132566Smlaier * CSUM_DATA_VALID : 5586132566Smlaier * network driver performed cksum, needs to additional pseudo header 5587132566Smlaier * cksum computation with partial csum_data(i.e. lack of H/W support for 5588132566Smlaier * pseudo header, for instance hme(4), sk(4) and possibly gem(4)) 5589132566Smlaier * 5590132566Smlaier * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 5591132566Smlaier * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 5592132566Smlaier * TCP/UDP layer. 5593132566Smlaier * Also, set csum_data to 0xffff to force cksum validation. 5594126261Smlaier */ 5595240233Sglebiusstatic int 5596126261Smlaierpf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 5597126261Smlaier{ 5598126261Smlaier u_int16_t sum = 0; 5599126261Smlaier int hw_assist = 0; 5600126261Smlaier struct ip *ip; 5601126261Smlaier 5602126261Smlaier if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 5603126261Smlaier return (1); 5604126261Smlaier if (m->m_pkthdr.len < off + len) 5605126261Smlaier return (1); 5606126261Smlaier 5607126261Smlaier switch (p) { 5608126261Smlaier case IPPROTO_TCP: 5609126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5610126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5611126261Smlaier sum = m->m_pkthdr.csum_data; 5612126261Smlaier } else { 5613223637Sbz ip = mtod(m, struct ip *); 5614126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5615240233Sglebius ip->ip_dst.s_addr, htonl((u_short)len + 5616223637Sbz m->m_pkthdr.csum_data + IPPROTO_TCP)); 5617126261Smlaier } 5618126261Smlaier sum ^= 0xffff; 5619126261Smlaier ++hw_assist; 5620126261Smlaier } 5621126261Smlaier break; 5622126261Smlaier case IPPROTO_UDP: 5623126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5624126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5625126261Smlaier sum = m->m_pkthdr.csum_data; 5626126261Smlaier } else { 5627223637Sbz ip = mtod(m, struct ip *); 5628126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5629223637Sbz ip->ip_dst.s_addr, htonl((u_short)len + 5630223637Sbz m->m_pkthdr.csum_data + IPPROTO_UDP)); 5631126261Smlaier } 5632126261Smlaier sum ^= 0xffff; 5633126261Smlaier ++hw_assist; 5634223637Sbz } 5635126261Smlaier break; 5636126261Smlaier case IPPROTO_ICMP: 5637126261Smlaier#ifdef INET6 5638126261Smlaier case IPPROTO_ICMPV6: 5639126261Smlaier#endif /* INET6 */ 5640126261Smlaier break; 5641126261Smlaier default: 5642126261Smlaier return (1); 5643126261Smlaier } 5644126261Smlaier 5645126261Smlaier if (!hw_assist) { 5646126261Smlaier switch (af) { 5647126261Smlaier case AF_INET: 5648126261Smlaier if (p == IPPROTO_ICMP) { 5649126261Smlaier if (m->m_len < off) 5650126261Smlaier return (1); 5651126261Smlaier m->m_data += off; 5652126261Smlaier m->m_len -= off; 5653126261Smlaier sum = in_cksum(m, len); 5654126261Smlaier m->m_data -= off; 5655126261Smlaier m->m_len += off; 5656126261Smlaier } else { 5657126261Smlaier if (m->m_len < sizeof(struct ip)) 5658126261Smlaier return (1); 5659126261Smlaier sum = in4_cksum(m, p, off, len); 5660126261Smlaier } 5661126261Smlaier break; 5662126261Smlaier#ifdef INET6 5663126261Smlaier case AF_INET6: 5664126261Smlaier if (m->m_len < sizeof(struct ip6_hdr)) 5665126261Smlaier return (1); 5666126261Smlaier sum = in6_cksum(m, p, off, len); 5667126261Smlaier break; 5668126261Smlaier#endif /* INET6 */ 5669126261Smlaier default: 5670126261Smlaier return (1); 5671126261Smlaier } 5672126261Smlaier } 5673126261Smlaier if (sum) { 5674126261Smlaier switch (p) { 5675126261Smlaier case IPPROTO_TCP: 5676183550Szec { 5677196039Srwatson KMOD_TCPSTAT_INC(tcps_rcvbadsum); 5678126261Smlaier break; 5679183550Szec } 5680126261Smlaier case IPPROTO_UDP: 5681183550Szec { 5682196039Srwatson KMOD_UDPSTAT_INC(udps_badsum); 5683126261Smlaier break; 5684183550Szec } 5685222529Sbz#ifdef INET 5686126261Smlaier case IPPROTO_ICMP: 5687183550Szec { 5688196039Srwatson KMOD_ICMPSTAT_INC(icps_checksum); 5689126261Smlaier break; 5690183550Szec } 5691222529Sbz#endif 5692126261Smlaier#ifdef INET6 5693126261Smlaier case IPPROTO_ICMPV6: 5694183550Szec { 5695196039Srwatson KMOD_ICMP6STAT_INC(icp6s_checksum); 5696126261Smlaier break; 5697183550Szec } 5698126261Smlaier#endif /* INET6 */ 5699126261Smlaier } 5700126261Smlaier return (1); 5701132566Smlaier } else { 5702132566Smlaier if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 5703132566Smlaier m->m_pkthdr.csum_flags |= 5704132566Smlaier (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 5705132566Smlaier m->m_pkthdr.csum_data = 0xffff; 5706132566Smlaier } 5707126261Smlaier } 5708126261Smlaier return (0); 5709126261Smlaier} 5710223637Sbz 5711126258Smlaier 5712145836Smlaier#ifdef INET 5713126258Smlaierint 5714240233Sglebiuspf_test(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5715126258Smlaier{ 5716130613Smlaier struct pfi_kif *kif; 5717130613Smlaier u_short action, reason = 0, log = 0; 5718130613Smlaier struct mbuf *m = *m0; 5719223637Sbz struct ip *h = NULL; 5720223637Sbz struct m_tag *ipfwtag; 5721223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5722130613Smlaier struct pf_state *s = NULL; 5723130613Smlaier struct pf_ruleset *ruleset = NULL; 5724130613Smlaier struct pf_pdesc pd; 5725130613Smlaier int off, dirndx, pqid = 0; 5726126258Smlaier 5727240233Sglebius M_ASSERTPKTHDR(m); 5728240233Sglebius 5729223637Sbz if (!V_pf_status.running) 5730171168Smlaier return (PF_PASS); 5731126258Smlaier 5732171168Smlaier memset(&pd, 0, sizeof(pd)); 5733145836Smlaier 5734240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 5735240233Sglebius 5736130613Smlaier if (kif == NULL) { 5737145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5738145836Smlaier ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname)); 5739130613Smlaier return (PF_DROP); 5740130613Smlaier } 5741223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 5742145836Smlaier return (PF_PASS); 5743130613Smlaier 5744240233Sglebius if (m->m_flags & M_SKIP_FIREWALL) 5745240233Sglebius return (PF_PASS); 5746126258Smlaier 5747240233Sglebius pd.pf_mtag = pf_find_mtag(m); 5748240233Sglebius 5749240233Sglebius PF_RULES_RLOCK(); 5750240233Sglebius 5751223637Sbz if (ip_divert_ptr != NULL && 5752223637Sbz ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) { 5753223637Sbz struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1); 5754223637Sbz if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) { 5755240233Sglebius if (pd.pf_mtag == NULL && 5756240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5757240233Sglebius action = PF_DROP; 5758240233Sglebius goto done; 5759240233Sglebius } 5760223637Sbz pd.pf_mtag->flags |= PF_PACKET_LOOPED; 5761223637Sbz m_tag_delete(m, ipfwtag); 5762223637Sbz } 5763240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) { 5764223637Sbz m->m_flags |= M_FASTFWD_OURS; 5765223637Sbz pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT; 5766223637Sbz } 5767240233Sglebius } else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) { 5768240233Sglebius /* We do IP header normalization and packet reassembly here */ 5769126258Smlaier action = PF_DROP; 5770126258Smlaier goto done; 5771126258Smlaier } 5772223637Sbz m = *m0; /* pf_normalize messes with m0 */ 5773126258Smlaier h = mtod(m, struct ip *); 5774126258Smlaier 5775126258Smlaier off = h->ip_hl << 2; 5776240233Sglebius if (off < (int)sizeof(struct ip)) { 5777126258Smlaier action = PF_DROP; 5778126258Smlaier REASON_SET(&reason, PFRES_SHORT); 5779126258Smlaier log = 1; 5780126258Smlaier goto done; 5781126258Smlaier } 5782126258Smlaier 5783126258Smlaier pd.src = (struct pf_addr *)&h->ip_src; 5784126258Smlaier pd.dst = (struct pf_addr *)&h->ip_dst; 5785223637Sbz pd.sport = pd.dport = NULL; 5786126258Smlaier pd.ip_sum = &h->ip_sum; 5787223637Sbz pd.proto_sum = NULL; 5788126258Smlaier pd.proto = h->ip_p; 5789223637Sbz pd.dir = dir; 5790223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 5791223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 5792126258Smlaier pd.af = AF_INET; 5793126258Smlaier pd.tos = h->ip_tos; 5794126258Smlaier pd.tot_len = ntohs(h->ip_len); 5795126258Smlaier 5796126258Smlaier /* handle fragments that didn't get reassembled by normalization */ 5797126258Smlaier if (h->ip_off & htons(IP_MF | IP_OFFMASK)) { 5798130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 5799126258Smlaier &pd, &a, &ruleset); 5800126258Smlaier goto done; 5801126258Smlaier } 5802126258Smlaier 5803126258Smlaier switch (h->ip_p) { 5804126258Smlaier 5805126258Smlaier case IPPROTO_TCP: { 5806126258Smlaier struct tcphdr th; 5807126258Smlaier 5808126258Smlaier pd.hdr.tcp = &th; 5809126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 5810126258Smlaier &action, &reason, AF_INET)) { 5811126258Smlaier log = action != PF_PASS; 5812126258Smlaier goto done; 5813126258Smlaier } 5814126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 5815126258Smlaier if ((th.th_flags & TH_ACK) && pd.p_len == 0) 5816126258Smlaier pqid = 1; 5817130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 5818126258Smlaier if (action == PF_DROP) 5819130613Smlaier goto done; 5820130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 5821126258Smlaier &reason); 5822126258Smlaier if (action == PF_PASS) { 5823223637Sbz if (pfsync_update_state_ptr != NULL) 5824223637Sbz pfsync_update_state_ptr(s); 5825126258Smlaier r = s->rule.ptr; 5826130613Smlaier a = s->anchor.ptr; 5827126258Smlaier log = s->log; 5828126258Smlaier } else if (s == NULL) 5829240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5830240233Sglebius &a, &ruleset, inp); 5831126258Smlaier break; 5832126258Smlaier } 5833126258Smlaier 5834126258Smlaier case IPPROTO_UDP: { 5835126258Smlaier struct udphdr uh; 5836126258Smlaier 5837126258Smlaier pd.hdr.udp = &uh; 5838126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 5839126258Smlaier &action, &reason, AF_INET)) { 5840126258Smlaier log = action != PF_PASS; 5841126258Smlaier goto done; 5842126258Smlaier } 5843130613Smlaier if (uh.uh_dport == 0 || 5844130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 5845130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 5846130613Smlaier action = PF_DROP; 5847171168Smlaier REASON_SET(&reason, PFRES_SHORT); 5848130613Smlaier goto done; 5849130613Smlaier } 5850130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 5851126258Smlaier if (action == PF_PASS) { 5852223637Sbz if (pfsync_update_state_ptr != NULL) 5853223637Sbz pfsync_update_state_ptr(s); 5854126258Smlaier r = s->rule.ptr; 5855126258Smlaier a = s->anchor.ptr; 5856126258Smlaier log = s->log; 5857126258Smlaier } else if (s == NULL) 5858240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5859240233Sglebius &a, &ruleset, inp); 5860126258Smlaier break; 5861126258Smlaier } 5862126258Smlaier 5863126258Smlaier case IPPROTO_ICMP: { 5864126258Smlaier struct icmp ih; 5865126258Smlaier 5866126258Smlaier pd.hdr.icmp = &ih; 5867126258Smlaier if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN, 5868126258Smlaier &action, &reason, AF_INET)) { 5869126258Smlaier log = action != PF_PASS; 5870126258Smlaier goto done; 5871126258Smlaier } 5872145836Smlaier action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd, 5873145836Smlaier &reason); 5874126258Smlaier if (action == PF_PASS) { 5875223637Sbz if (pfsync_update_state_ptr != NULL) 5876223637Sbz pfsync_update_state_ptr(s); 5877126258Smlaier r = s->rule.ptr; 5878126258Smlaier a = s->anchor.ptr; 5879126258Smlaier log = s->log; 5880126258Smlaier } else if (s == NULL) 5881240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5882240233Sglebius &a, &ruleset, inp); 5883126258Smlaier break; 5884126258Smlaier } 5885126258Smlaier 5886223637Sbz#ifdef INET6 5887223637Sbz case IPPROTO_ICMPV6: { 5888223637Sbz action = PF_DROP; 5889223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5890223637Sbz ("pf: dropping IPv4 packet with ICMPv6 payload\n")); 5891223637Sbz goto done; 5892223637Sbz } 5893223637Sbz#endif 5894223637Sbz 5895126258Smlaier default: 5896223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 5897126258Smlaier if (action == PF_PASS) { 5898223637Sbz if (pfsync_update_state_ptr != NULL) 5899223637Sbz pfsync_update_state_ptr(s); 5900126258Smlaier r = s->rule.ptr; 5901126258Smlaier a = s->anchor.ptr; 5902126258Smlaier log = s->log; 5903126258Smlaier } else if (s == NULL) 5904240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5905240233Sglebius &a, &ruleset, inp); 5906126258Smlaier break; 5907126258Smlaier } 5908126258Smlaier 5909126258Smlaierdone: 5910240233Sglebius PF_RULES_RUNLOCK(); 5911126258Smlaier if (action == PF_PASS && h->ip_hl > 5 && 5912200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 5913126258Smlaier action = PF_DROP; 5914145836Smlaier REASON_SET(&reason, PFRES_IPOPTIONS); 5915126258Smlaier log = 1; 5916126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 5917126258Smlaier ("pf: dropping packet with ip options\n")); 5918126258Smlaier } 5919126258Smlaier 5920240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 5921240233Sglebius action = PF_DROP; 5922240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5923240233Sglebius } 5924240233Sglebius if (r->rtableid >= 0) 5925240233Sglebius M_SETFIB(m, r->rtableid); 5926145836Smlaier 5927126258Smlaier#ifdef ALTQ 5928126258Smlaier if (action == PF_PASS && r->qid) { 5929240233Sglebius if (pd.pf_mtag == NULL && 5930240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5931240233Sglebius action = PF_DROP; 5932240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5933240233Sglebius } 5934171168Smlaier if (pqid || (pd.tos & IPTOS_LOWDELAY)) 5935171168Smlaier pd.pf_mtag->qid = r->pqid; 5936171168Smlaier else 5937171168Smlaier pd.pf_mtag->qid = r->qid; 5938171168Smlaier /* add hints for ecn */ 5939171168Smlaier pd.pf_mtag->hdr = h; 5940223637Sbz 5941126258Smlaier } 5942145836Smlaier#endif /* ALTQ */ 5943126258Smlaier 5944130613Smlaier /* 5945130613Smlaier * connections redirected to loopback should not match sockets 5946130613Smlaier * bound specifically to loopback due to security implications, 5947130613Smlaier * see tcp_input() and in_pcblookup_listen(). 5948130613Smlaier */ 5949130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 5950130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 5951130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 5952130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 5953171168Smlaier (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 5954223637Sbz m->m_flags |= M_SKIP_FIREWALL; 5955171168Smlaier 5956240233Sglebius if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL && 5957240233Sglebius !PACKET_LOOPED(&pd)) { 5958223637Sbz 5959223637Sbz ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 5960240233Sglebius sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 5961223637Sbz if (ipfwtag != NULL) { 5962225171Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->info = 5963225171Sbz ntohs(r->divert.port); 5964223637Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir; 5965223637Sbz 5966240233Sglebius if (s) 5967240233Sglebius PF_STATE_UNLOCK(s); 5968240233Sglebius 5969223637Sbz m_tag_prepend(m, ipfwtag); 5970223637Sbz if (m->m_flags & M_FASTFWD_OURS) { 5971240233Sglebius if (pd.pf_mtag == NULL && 5972240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5973240233Sglebius action = PF_DROP; 5974240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5975240233Sglebius log = 1; 5976240233Sglebius DPFPRINTF(PF_DEBUG_MISC, 5977240233Sglebius ("pf: failed to allocate tag\n")); 5978240233Sglebius } 5979223637Sbz pd.pf_mtag->flags |= PF_FASTFWD_OURS_PRESENT; 5980223637Sbz m->m_flags &= ~M_FASTFWD_OURS; 5981223637Sbz } 5982240233Sglebius ip_divert_ptr(*m0, dir == PF_IN ? DIR_IN : DIR_OUT); 5983240233Sglebius *m0 = NULL; 5984223637Sbz 5985223637Sbz return (action); 5986223637Sbz } else { 5987223637Sbz /* XXX: ipfw has the same behaviour! */ 5988223637Sbz action = PF_DROP; 5989223637Sbz REASON_SET(&reason, PFRES_MEMORY); 5990223637Sbz log = 1; 5991223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5992223637Sbz ("pf: failed to allocate divert tag\n")); 5993223637Sbz } 5994223637Sbz } 5995223637Sbz 5996171168Smlaier if (log) { 5997171168Smlaier struct pf_rule *lr; 5998171168Smlaier 5999171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 6000171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 6001171168Smlaier lr = s->nat_rule.ptr; 6002171168Smlaier else 6003171168Smlaier lr = r; 6004240233Sglebius PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd, 6005240233Sglebius (s == NULL)); 6006130613Smlaier } 6007130613Smlaier 6008130613Smlaier kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 6009130613Smlaier kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++; 6010130613Smlaier 6011130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 6012171168Smlaier dirndx = (dir == PF_OUT); 6013171168Smlaier r->packets[dirndx]++; 6014171168Smlaier r->bytes[dirndx] += pd.tot_len; 6015130613Smlaier if (a != NULL) { 6016171168Smlaier a->packets[dirndx]++; 6017171168Smlaier a->bytes[dirndx] += pd.tot_len; 6018130613Smlaier } 6019130613Smlaier if (s != NULL) { 6020130613Smlaier if (s->nat_rule.ptr != NULL) { 6021171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 6022171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 6023130613Smlaier } 6024130613Smlaier if (s->src_node != NULL) { 6025171168Smlaier s->src_node->packets[dirndx]++; 6026171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 6027130613Smlaier } 6028130613Smlaier if (s->nat_src_node != NULL) { 6029171168Smlaier s->nat_src_node->packets[dirndx]++; 6030171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 6031130613Smlaier } 6032171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 6033171168Smlaier s->packets[dirndx]++; 6034171168Smlaier s->bytes[dirndx] += pd.tot_len; 6035130613Smlaier } 6036130613Smlaier tr = r; 6037130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6038223637Sbz if (nr != NULL && r == &V_pf_default_rule) 6039223637Sbz tr = nr; 6040130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 6041223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 6042223637Sbz (s == NULL) ? pd.src : 6043223637Sbz &s->key[(s->direction == PF_IN)]-> 6044223637Sbz addr[(s->direction == PF_OUT)], 6045223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6046223637Sbz r->action == PF_PASS, tr->src.neg); 6047130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6048223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6049223637Sbz (s == NULL) ? pd.dst : 6050223637Sbz &s->key[(s->direction == PF_IN)]-> 6051223637Sbz addr[(s->direction == PF_IN)], 6052223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6053223637Sbz r->action == PF_PASS, tr->dst.neg); 6054130613Smlaier } 6055130613Smlaier 6056223637Sbz switch (action) { 6057223637Sbz case PF_SYNPROXY_DROP: 6058126258Smlaier m_freem(*m0); 6059223637Sbz case PF_DEFER: 6060126258Smlaier *m0 = NULL; 6061126258Smlaier action = PF_PASS; 6062223637Sbz break; 6063271306Sglebius case PF_DROP: 6064271306Sglebius m_freem(*m0); 6065271306Sglebius *m0 = NULL; 6066271306Sglebius break; 6067223637Sbz default: 6068240233Sglebius /* pf_route() returns unlocked. */ 6069240233Sglebius if (r->rt) { 6070223637Sbz pf_route(m0, r, dir, kif->pfik_ifp, s, &pd); 6071240233Sglebius return (action); 6072240233Sglebius } 6073223637Sbz break; 6074223637Sbz } 6075240233Sglebius if (s) 6076240233Sglebius PF_STATE_UNLOCK(s); 6077240233Sglebius 6078126258Smlaier return (action); 6079126258Smlaier} 6080126258Smlaier#endif /* INET */ 6081126258Smlaier 6082126258Smlaier#ifdef INET6 6083126258Smlaierint 6084240233Sglebiuspf_test6(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 6085126258Smlaier{ 6086130613Smlaier struct pfi_kif *kif; 6087130613Smlaier u_short action, reason = 0, log = 0; 6088171168Smlaier struct mbuf *m = *m0, *n = NULL; 6089223637Sbz struct ip6_hdr *h = NULL; 6090223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 6091130613Smlaier struct pf_state *s = NULL; 6092130613Smlaier struct pf_ruleset *ruleset = NULL; 6093130613Smlaier struct pf_pdesc pd; 6094169843Sdhartmei int off, terminal = 0, dirndx, rh_cnt = 0; 6095126258Smlaier 6096240233Sglebius M_ASSERTPKTHDR(m); 6097240233Sglebius 6098240233Sglebius if (!V_pf_status.running) 6099126258Smlaier return (PF_PASS); 6100126258Smlaier 6101171168Smlaier memset(&pd, 0, sizeof(pd)); 6102240233Sglebius pd.pf_mtag = pf_find_mtag(m); 6103145836Smlaier 6104240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED) 6105240233Sglebius return (PF_PASS); 6106240233Sglebius 6107240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 6108130613Smlaier if (kif == NULL) { 6109145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 6110145836Smlaier ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname)); 6111130613Smlaier return (PF_DROP); 6112130613Smlaier } 6113223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 6114145836Smlaier return (PF_PASS); 6115130613Smlaier 6116270575Sglebius if (m->m_flags & M_SKIP_FIREWALL) 6117270575Sglebius return (PF_PASS); 6118270575Sglebius 6119240233Sglebius PF_RULES_RLOCK(); 6120223637Sbz 6121126258Smlaier /* We do IP header normalization and packet reassembly here */ 6122145836Smlaier if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) { 6123126258Smlaier action = PF_DROP; 6124126258Smlaier goto done; 6125126258Smlaier } 6126223637Sbz m = *m0; /* pf_normalize messes with m0 */ 6127126258Smlaier h = mtod(m, struct ip6_hdr *); 6128126258Smlaier 6129169843Sdhartmei#if 1 6130169843Sdhartmei /* 6131169843Sdhartmei * we do not support jumbogram yet. if we keep going, zero ip6_plen 6132169843Sdhartmei * will do something bad, so drop the packet for now. 6133169843Sdhartmei */ 6134169843Sdhartmei if (htons(h->ip6_plen) == 0) { 6135169843Sdhartmei action = PF_DROP; 6136169843Sdhartmei REASON_SET(&reason, PFRES_NORM); /*XXX*/ 6137169843Sdhartmei goto done; 6138169843Sdhartmei } 6139169843Sdhartmei#endif 6140169843Sdhartmei 6141126258Smlaier pd.src = (struct pf_addr *)&h->ip6_src; 6142126258Smlaier pd.dst = (struct pf_addr *)&h->ip6_dst; 6143223637Sbz pd.sport = pd.dport = NULL; 6144126258Smlaier pd.ip_sum = NULL; 6145223637Sbz pd.proto_sum = NULL; 6146223637Sbz pd.dir = dir; 6147223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 6148223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 6149126258Smlaier pd.af = AF_INET6; 6150126258Smlaier pd.tos = 0; 6151126258Smlaier pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 6152126258Smlaier 6153126258Smlaier off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr); 6154126258Smlaier pd.proto = h->ip6_nxt; 6155126258Smlaier do { 6156126258Smlaier switch (pd.proto) { 6157126258Smlaier case IPPROTO_FRAGMENT: 6158130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 6159126258Smlaier &pd, &a, &ruleset); 6160126258Smlaier if (action == PF_DROP) 6161126258Smlaier REASON_SET(&reason, PFRES_FRAG); 6162126258Smlaier goto done; 6163169843Sdhartmei case IPPROTO_ROUTING: { 6164169843Sdhartmei struct ip6_rthdr rthdr; 6165169843Sdhartmei 6166169843Sdhartmei if (rh_cnt++) { 6167169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6168169843Sdhartmei ("pf: IPv6 more than one rthdr\n")); 6169169843Sdhartmei action = PF_DROP; 6170169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6171169843Sdhartmei log = 1; 6172169843Sdhartmei goto done; 6173169843Sdhartmei } 6174169843Sdhartmei if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL, 6175169843Sdhartmei &reason, pd.af)) { 6176169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6177169843Sdhartmei ("pf: IPv6 short rthdr\n")); 6178169843Sdhartmei action = PF_DROP; 6179169843Sdhartmei REASON_SET(&reason, PFRES_SHORT); 6180169843Sdhartmei log = 1; 6181169843Sdhartmei goto done; 6182169843Sdhartmei } 6183169843Sdhartmei if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 6184169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6185169843Sdhartmei ("pf: IPv6 rthdr0\n")); 6186169843Sdhartmei action = PF_DROP; 6187169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6188169843Sdhartmei log = 1; 6189169843Sdhartmei goto done; 6190169843Sdhartmei } 6191223637Sbz /* FALLTHROUGH */ 6192169843Sdhartmei } 6193126258Smlaier case IPPROTO_AH: 6194126258Smlaier case IPPROTO_HOPOPTS: 6195126258Smlaier case IPPROTO_DSTOPTS: { 6196126258Smlaier /* get next header and header length */ 6197126258Smlaier struct ip6_ext opt6; 6198126258Smlaier 6199126258Smlaier if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6), 6200145836Smlaier NULL, &reason, pd.af)) { 6201126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 6202126258Smlaier ("pf: IPv6 short opt\n")); 6203126258Smlaier action = PF_DROP; 6204126258Smlaier log = 1; 6205126258Smlaier goto done; 6206126258Smlaier } 6207126258Smlaier if (pd.proto == IPPROTO_AH) 6208126258Smlaier off += (opt6.ip6e_len + 2) * 4; 6209126258Smlaier else 6210126258Smlaier off += (opt6.ip6e_len + 1) * 8; 6211126258Smlaier pd.proto = opt6.ip6e_nxt; 6212126258Smlaier /* goto the next header */ 6213126258Smlaier break; 6214126258Smlaier } 6215126258Smlaier default: 6216126258Smlaier terminal++; 6217126258Smlaier break; 6218126258Smlaier } 6219126258Smlaier } while (!terminal); 6220126258Smlaier 6221171168Smlaier /* if there's no routing header, use unmodified mbuf for checksumming */ 6222171168Smlaier if (!n) 6223171168Smlaier n = m; 6224171168Smlaier 6225126258Smlaier switch (pd.proto) { 6226126258Smlaier 6227126258Smlaier case IPPROTO_TCP: { 6228126258Smlaier struct tcphdr th; 6229126258Smlaier 6230126258Smlaier pd.hdr.tcp = &th; 6231126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 6232126258Smlaier &action, &reason, AF_INET6)) { 6233126258Smlaier log = action != PF_PASS; 6234126258Smlaier goto done; 6235126258Smlaier } 6236126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 6237130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6238126258Smlaier if (action == PF_DROP) 6239130613Smlaier goto done; 6240130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6241126258Smlaier &reason); 6242126258Smlaier if (action == PF_PASS) { 6243223637Sbz if (pfsync_update_state_ptr != NULL) 6244223637Sbz pfsync_update_state_ptr(s); 6245126258Smlaier r = s->rule.ptr; 6246130613Smlaier a = s->anchor.ptr; 6247126258Smlaier log = s->log; 6248126258Smlaier } else if (s == NULL) 6249240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6250240233Sglebius &a, &ruleset, inp); 6251126258Smlaier break; 6252126258Smlaier } 6253126258Smlaier 6254126258Smlaier case IPPROTO_UDP: { 6255126258Smlaier struct udphdr uh; 6256126258Smlaier 6257126258Smlaier pd.hdr.udp = &uh; 6258126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 6259126258Smlaier &action, &reason, AF_INET6)) { 6260126258Smlaier log = action != PF_PASS; 6261126258Smlaier goto done; 6262126258Smlaier } 6263130613Smlaier if (uh.uh_dport == 0 || 6264130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 6265130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 6266130613Smlaier action = PF_DROP; 6267171168Smlaier REASON_SET(&reason, PFRES_SHORT); 6268130613Smlaier goto done; 6269130613Smlaier } 6270130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6271126258Smlaier if (action == PF_PASS) { 6272223637Sbz if (pfsync_update_state_ptr != NULL) 6273223637Sbz pfsync_update_state_ptr(s); 6274126258Smlaier r = s->rule.ptr; 6275130613Smlaier a = s->anchor.ptr; 6276126258Smlaier log = s->log; 6277126258Smlaier } else if (s == NULL) 6278240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6279240233Sglebius &a, &ruleset, inp); 6280126258Smlaier break; 6281126258Smlaier } 6282126258Smlaier 6283223637Sbz case IPPROTO_ICMP: { 6284223637Sbz action = PF_DROP; 6285223637Sbz DPFPRINTF(PF_DEBUG_MISC, 6286223637Sbz ("pf: dropping IPv6 packet with ICMPv4 payload\n")); 6287223637Sbz goto done; 6288223637Sbz } 6289223637Sbz 6290126258Smlaier case IPPROTO_ICMPV6: { 6291126258Smlaier struct icmp6_hdr ih; 6292126258Smlaier 6293126258Smlaier pd.hdr.icmp6 = &ih; 6294126258Smlaier if (!pf_pull_hdr(m, off, &ih, sizeof(ih), 6295126258Smlaier &action, &reason, AF_INET6)) { 6296126258Smlaier log = action != PF_PASS; 6297126258Smlaier goto done; 6298126258Smlaier } 6299130613Smlaier action = pf_test_state_icmp(&s, dir, kif, 6300145836Smlaier m, off, h, &pd, &reason); 6301126258Smlaier if (action == PF_PASS) { 6302223637Sbz if (pfsync_update_state_ptr != NULL) 6303223637Sbz pfsync_update_state_ptr(s); 6304126258Smlaier r = s->rule.ptr; 6305130613Smlaier a = s->anchor.ptr; 6306126258Smlaier log = s->log; 6307126258Smlaier } else if (s == NULL) 6308240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6309240233Sglebius &a, &ruleset, inp); 6310126258Smlaier break; 6311126258Smlaier } 6312126258Smlaier 6313126258Smlaier default: 6314223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 6315130613Smlaier if (action == PF_PASS) { 6316223637Sbz if (pfsync_update_state_ptr != NULL) 6317223637Sbz pfsync_update_state_ptr(s); 6318130613Smlaier r = s->rule.ptr; 6319130613Smlaier a = s->anchor.ptr; 6320130613Smlaier log = s->log; 6321130613Smlaier } else if (s == NULL) 6322240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6323240233Sglebius &a, &ruleset, inp); 6324126258Smlaier break; 6325126258Smlaier } 6326126258Smlaier 6327126258Smlaierdone: 6328240233Sglebius PF_RULES_RUNLOCK(); 6329223637Sbz if (n != m) { 6330223637Sbz m_freem(n); 6331223637Sbz n = NULL; 6332223637Sbz } 6333223637Sbz 6334169843Sdhartmei /* handle dangerous IPv6 extension headers. */ 6335169843Sdhartmei if (action == PF_PASS && rh_cnt && 6336200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6337169843Sdhartmei action = PF_DROP; 6338169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6339169843Sdhartmei log = 1; 6340169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6341169843Sdhartmei ("pf: dropping packet with dangerous v6 headers\n")); 6342169843Sdhartmei } 6343126258Smlaier 6344240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6345240233Sglebius action = PF_DROP; 6346240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6347240233Sglebius } 6348240233Sglebius if (r->rtableid >= 0) 6349240233Sglebius M_SETFIB(m, r->rtableid); 6350145836Smlaier 6351126258Smlaier#ifdef ALTQ 6352126258Smlaier if (action == PF_PASS && r->qid) { 6353240233Sglebius if (pd.pf_mtag == NULL && 6354240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6355240233Sglebius action = PF_DROP; 6356240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6357240233Sglebius } 6358171168Smlaier if (pd.tos & IPTOS_LOWDELAY) 6359171168Smlaier pd.pf_mtag->qid = r->pqid; 6360171168Smlaier else 6361171168Smlaier pd.pf_mtag->qid = r->qid; 6362171168Smlaier /* add hints for ecn */ 6363171168Smlaier pd.pf_mtag->hdr = h; 6364126258Smlaier } 6365145836Smlaier#endif /* ALTQ */ 6366126258Smlaier 6367130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6368130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6369130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 6370130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 6371171168Smlaier IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) 6372223637Sbz m->m_flags |= M_SKIP_FIREWALL; 6373171168Smlaier 6374223637Sbz /* XXX: Anybody working on it?! */ 6375223637Sbz if (r->divert.port) 6376223637Sbz printf("pf: divert(9) is not supported for IPv6\n"); 6377223637Sbz 6378171168Smlaier if (log) { 6379171168Smlaier struct pf_rule *lr; 6380171168Smlaier 6381171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 6382171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 6383171168Smlaier lr = s->nat_rule.ptr; 6384171168Smlaier else 6385171168Smlaier lr = r; 6386240233Sglebius PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset, 6387240233Sglebius &pd, (s == NULL)); 6388130613Smlaier } 6389130613Smlaier 6390130613Smlaier kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 6391130613Smlaier kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++; 6392130613Smlaier 6393130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 6394171168Smlaier dirndx = (dir == PF_OUT); 6395171168Smlaier r->packets[dirndx]++; 6396171168Smlaier r->bytes[dirndx] += pd.tot_len; 6397130613Smlaier if (a != NULL) { 6398171168Smlaier a->packets[dirndx]++; 6399171168Smlaier a->bytes[dirndx] += pd.tot_len; 6400130613Smlaier } 6401130613Smlaier if (s != NULL) { 6402130613Smlaier if (s->nat_rule.ptr != NULL) { 6403171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 6404171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 6405130613Smlaier } 6406130613Smlaier if (s->src_node != NULL) { 6407171168Smlaier s->src_node->packets[dirndx]++; 6408171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 6409130613Smlaier } 6410130613Smlaier if (s->nat_src_node != NULL) { 6411171168Smlaier s->nat_src_node->packets[dirndx]++; 6412171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 6413130613Smlaier } 6414171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 6415171168Smlaier s->packets[dirndx]++; 6416171168Smlaier s->bytes[dirndx] += pd.tot_len; 6417130613Smlaier } 6418130613Smlaier tr = r; 6419130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6420223637Sbz if (nr != NULL && r == &V_pf_default_rule) 6421223637Sbz tr = nr; 6422130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 6423223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 6424223637Sbz (s == NULL) ? pd.src : 6425223637Sbz &s->key[(s->direction == PF_IN)]->addr[0], 6426223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6427223637Sbz r->action == PF_PASS, tr->src.neg); 6428130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6429223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6430223637Sbz (s == NULL) ? pd.dst : 6431223637Sbz &s->key[(s->direction == PF_IN)]->addr[1], 6432223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6433223637Sbz r->action == PF_PASS, tr->dst.neg); 6434130613Smlaier } 6435130613Smlaier 6436223637Sbz switch (action) { 6437223637Sbz case PF_SYNPROXY_DROP: 6438126258Smlaier m_freem(*m0); 6439223637Sbz case PF_DEFER: 6440126258Smlaier *m0 = NULL; 6441126258Smlaier action = PF_PASS; 6442223637Sbz break; 6443271306Sglebius case PF_DROP: 6444271306Sglebius m_freem(*m0); 6445271306Sglebius *m0 = NULL; 6446271306Sglebius break; 6447223637Sbz default: 6448240233Sglebius /* pf_route6() returns unlocked. */ 6449240233Sglebius if (r->rt) { 6450223637Sbz pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd); 6451240233Sglebius return (action); 6452240233Sglebius } 6453223637Sbz break; 6454223637Sbz } 6455126258Smlaier 6456240233Sglebius if (s) 6457240233Sglebius PF_STATE_UNLOCK(s); 6458240233Sglebius 6459126258Smlaier return (action); 6460126258Smlaier} 6461126258Smlaier#endif /* INET6 */ 6462