pf.c revision 265008
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: stable/10/sys/netpfil/pf/pf.c 265008 2014-04-27 09:05:34Z mm $"); 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); 112223637SbzVNET_DEFINE(struct pf_status, 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 206240233Sglebiusstatic void pf_change_ap(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 void pf_set_rt_ifp(struct pf_state *, 269126258Smlaier struct pf_addr *); 270240233Sglebiusstatic int pf_check_proto_cksum(struct mbuf *, int, int, 271126258Smlaier u_int8_t, sa_family_t); 272240233Sglebiusstatic void pf_print_state_parts(struct pf_state *, 273223637Sbz struct pf_state_key *, struct pf_state_key *); 274240233Sglebiusstatic int pf_addr_wrap_neq(struct pf_addr_wrap *, 275126258Smlaier struct pf_addr_wrap *); 276240233Sglebiusstatic struct pf_state *pf_find_state(struct pfi_kif *, 277240233Sglebius struct pf_state_key_cmp *, u_int); 278240233Sglebiusstatic int pf_src_connlimit(struct pf_state **); 279264454Smmstatic void pf_overload_task(void *v, int pending); 280240233Sglebiusstatic int pf_insert_src_node(struct pf_src_node **, 281240233Sglebius struct pf_rule *, struct pf_addr *, sa_family_t); 282241039Sglebiusstatic u_int pf_purge_expired_states(u_int, int); 283240233Sglebiusstatic void pf_purge_unlinked_rules(void); 284265008Smmstatic int pf_mtag_uminit(void *, int, int); 285240233Sglebiusstatic void pf_mtag_free(struct m_tag *); 286240233Sglebius#ifdef INET 287240233Sglebiusstatic void pf_route(struct mbuf **, struct pf_rule *, int, 288240233Sglebius struct ifnet *, struct pf_state *, 289240233Sglebius struct pf_pdesc *); 290240233Sglebius#endif /* INET */ 291240233Sglebius#ifdef INET6 292240233Sglebiusstatic void pf_change_a6(struct pf_addr *, u_int16_t *, 293240233Sglebius struct pf_addr *, u_int8_t); 294240233Sglebiusstatic void pf_route6(struct mbuf **, struct pf_rule *, int, 295240233Sglebius struct ifnet *, struct pf_state *, 296240233Sglebius struct pf_pdesc *); 297240233Sglebius#endif /* INET6 */ 298126258Smlaier 299126261Smlaierint in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len); 300126258Smlaier 301223637SbzVNET_DECLARE(int, pf_end_threads); 302171168Smlaier 303240233SglebiusVNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 304145836Smlaier 305240233Sglebius#define PACKET_LOOPED(pd) ((pd)->pf_mtag && \ 306240233Sglebius (pd)->pf_mtag->flags & PF_PACKET_LOOPED) 307126258Smlaier 308240233Sglebius#define STATE_LOOKUP(i, k, d, s, pd) \ 309126258Smlaier do { \ 310240233Sglebius (s) = pf_find_state((i), (k), (d)); \ 311261018Sglebius if ((s) == NULL) \ 312126258Smlaier return (PF_DROP); \ 313240233Sglebius if (PACKET_LOOPED(pd)) \ 314126258Smlaier return (PF_PASS); \ 315240233Sglebius if ((d) == PF_OUT && \ 316223637Sbz (((s)->rule.ptr->rt == PF_ROUTETO && \ 317223637Sbz (s)->rule.ptr->direction == PF_OUT) || \ 318223637Sbz ((s)->rule.ptr->rt == PF_REPLYTO && \ 319223637Sbz (s)->rule.ptr->direction == PF_IN)) && \ 320223637Sbz (s)->rt_kif != NULL && \ 321240233Sglebius (s)->rt_kif != (i)) \ 322223637Sbz return (PF_PASS); \ 323126258Smlaier } while (0) 324126258Smlaier 325223637Sbz#define BOUND_IFACE(r, k) \ 326223637Sbz ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : V_pfi_all 327126258Smlaier 328263029Sglebius#define STATE_INC_COUNTERS(s) \ 329263029Sglebius do { \ 330263029Sglebius counter_u64_add(s->rule.ptr->states_cur, 1); \ 331263029Sglebius counter_u64_add(s->rule.ptr->states_tot, 1); \ 332263029Sglebius if (s->anchor.ptr != NULL) { \ 333263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, 1); \ 334263029Sglebius counter_u64_add(s->anchor.ptr->states_tot, 1); \ 335263029Sglebius } \ 336263029Sglebius if (s->nat_rule.ptr != NULL) { \ 337263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, 1);\ 338263029Sglebius counter_u64_add(s->nat_rule.ptr->states_tot, 1);\ 339263029Sglebius } \ 340145836Smlaier } while (0) 341145836Smlaier 342263029Sglebius#define STATE_DEC_COUNTERS(s) \ 343263029Sglebius do { \ 344263029Sglebius if (s->nat_rule.ptr != NULL) \ 345263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, -1);\ 346263029Sglebius if (s->anchor.ptr != NULL) \ 347263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, -1); \ 348263029Sglebius counter_u64_add(s->rule.ptr->states_cur, -1); \ 349145836Smlaier } while (0) 350145836Smlaier 351240233Sglebiusstatic MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures"); 352240233SglebiusVNET_DEFINE(struct pf_keyhash *, pf_keyhash); 353240233SglebiusVNET_DEFINE(struct pf_idhash *, pf_idhash); 354240233SglebiusVNET_DEFINE(u_long, pf_hashmask); 355240233SglebiusVNET_DEFINE(struct pf_srchash *, pf_srchash); 356240233SglebiusVNET_DEFINE(u_long, pf_srchashmask); 357223637Sbz 358240233SglebiusSYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW, 0, "pf(4)"); 359223637Sbz 360240233SglebiusVNET_DEFINE(u_long, pf_hashsize); 361240233Sglebius#define V_pf_hashsize VNET(pf_hashsize) 362240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_RDTUN, 363240233Sglebius &VNET_NAME(pf_hashsize), 0, "Size of pf(4) states hashtable"); 364130613Smlaier 365240233SglebiusVNET_DEFINE(u_long, pf_srchashsize); 366240233Sglebius#define V_pf_srchashsize VNET(pf_srchashsize) 367240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_RDTUN, 368240233Sglebius &VNET_NAME(pf_srchashsize), 0, "Size of pf(4) source nodes hashtable"); 369171168Smlaier 370240233SglebiusVNET_DEFINE(void *, pf_swi_cookie); 371130613Smlaier 372240233SglebiusVNET_DEFINE(uint32_t, pf_hashseed); 373240233Sglebius#define V_pf_hashseed VNET(pf_hashseed) 374240233Sglebius 375240233Sglebiusstatic __inline uint32_t 376240233Sglebiuspf_hashkey(struct pf_state_key *sk) 377126258Smlaier{ 378240233Sglebius uint32_t h; 379126258Smlaier 380240233Sglebius h = jenkins_hash32((uint32_t *)sk, 381240233Sglebius sizeof(struct pf_state_key_cmp)/sizeof(uint32_t), 382240233Sglebius V_pf_hashseed); 383240233Sglebius 384240233Sglebius return (h & V_pf_hashmask); 385130613Smlaier} 386130613Smlaier 387240736Sglebiusstatic __inline uint32_t 388240736Sglebiuspf_hashsrc(struct pf_addr *addr, sa_family_t af) 389240736Sglebius{ 390240736Sglebius uint32_t h; 391240736Sglebius 392240736Sglebius switch (af) { 393240736Sglebius case AF_INET: 394240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v4, 395240736Sglebius sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed); 396240736Sglebius break; 397240736Sglebius case AF_INET6: 398240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v6, 399240736Sglebius sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed); 400240736Sglebius break; 401240736Sglebius default: 402240736Sglebius panic("%s: unknown address family %u", __func__, af); 403240736Sglebius } 404240736Sglebius 405240736Sglebius return (h & V_pf_srchashmask); 406240736Sglebius} 407240736Sglebius 408126258Smlaier#ifdef INET6 409126258Smlaiervoid 410126258Smlaierpf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af) 411126258Smlaier{ 412126258Smlaier switch (af) { 413126258Smlaier#ifdef INET 414126258Smlaier case AF_INET: 415126258Smlaier dst->addr32[0] = src->addr32[0]; 416126258Smlaier break; 417126258Smlaier#endif /* INET */ 418126258Smlaier case AF_INET6: 419126258Smlaier dst->addr32[0] = src->addr32[0]; 420126258Smlaier dst->addr32[1] = src->addr32[1]; 421126258Smlaier dst->addr32[2] = src->addr32[2]; 422126258Smlaier dst->addr32[3] = src->addr32[3]; 423126258Smlaier break; 424126258Smlaier } 425126258Smlaier} 426145836Smlaier#endif /* INET6 */ 427126258Smlaier 428240233Sglebiusstatic void 429145836Smlaierpf_init_threshold(struct pf_threshold *threshold, 430145836Smlaier u_int32_t limit, u_int32_t seconds) 431145836Smlaier{ 432145836Smlaier threshold->limit = limit * PF_THRESHOLD_MULT; 433145836Smlaier threshold->seconds = seconds; 434145836Smlaier threshold->count = 0; 435240233Sglebius threshold->last = time_uptime; 436145836Smlaier} 437145836Smlaier 438240233Sglebiusstatic void 439145836Smlaierpf_add_threshold(struct pf_threshold *threshold) 440145836Smlaier{ 441240233Sglebius u_int32_t t = time_uptime, diff = t - threshold->last; 442145836Smlaier 443145836Smlaier if (diff >= threshold->seconds) 444145836Smlaier threshold->count = 0; 445145836Smlaier else 446145836Smlaier threshold->count -= threshold->count * diff / 447145836Smlaier threshold->seconds; 448145836Smlaier threshold->count += PF_THRESHOLD_MULT; 449145836Smlaier threshold->last = t; 450145836Smlaier} 451145836Smlaier 452240233Sglebiusstatic int 453145836Smlaierpf_check_threshold(struct pf_threshold *threshold) 454145836Smlaier{ 455145836Smlaier return (threshold->count > threshold->limit); 456145836Smlaier} 457145836Smlaier 458240233Sglebiusstatic int 459145836Smlaierpf_src_connlimit(struct pf_state **state) 460145836Smlaier{ 461240811Sglebius struct pf_overload_entry *pfoe; 462145836Smlaier int bad = 0; 463145836Smlaier 464240233Sglebius PF_STATE_LOCK_ASSERT(*state); 465240233Sglebius 466145836Smlaier (*state)->src_node->conn++; 467171168Smlaier (*state)->src.tcp_est = 1; 468145836Smlaier pf_add_threshold(&(*state)->src_node->conn_rate); 469145836Smlaier 470145836Smlaier if ((*state)->rule.ptr->max_src_conn && 471145836Smlaier (*state)->rule.ptr->max_src_conn < 472145836Smlaier (*state)->src_node->conn) { 473223637Sbz V_pf_status.lcounters[LCNT_SRCCONN]++; 474145836Smlaier bad++; 475145836Smlaier } 476145836Smlaier 477145836Smlaier if ((*state)->rule.ptr->max_src_conn_rate.limit && 478145836Smlaier pf_check_threshold(&(*state)->src_node->conn_rate)) { 479223637Sbz V_pf_status.lcounters[LCNT_SRCCONNRATE]++; 480145836Smlaier bad++; 481145836Smlaier } 482145836Smlaier 483145836Smlaier if (!bad) 484145836Smlaier return (0); 485145836Smlaier 486240233Sglebius /* Kill this state. */ 487240233Sglebius (*state)->timeout = PFTM_PURGE; 488240233Sglebius (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 489145836Smlaier 490240233Sglebius if ((*state)->rule.ptr->overload_tbl == NULL) 491240233Sglebius return (1); 492145836Smlaier 493240811Sglebius /* Schedule overloading and flushing task. */ 494240811Sglebius pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT); 495240811Sglebius if (pfoe == NULL) 496240233Sglebius return (1); /* too bad :( */ 497240233Sglebius 498240811Sglebius bcopy(&(*state)->src_node->addr, &pfoe->addr, sizeof(pfoe->addr)); 499240811Sglebius pfoe->af = (*state)->key[PF_SK_WIRE]->af; 500240811Sglebius pfoe->rule = (*state)->rule.ptr; 501240811Sglebius pfoe->dir = (*state)->direction; 502240811Sglebius PF_OVERLOADQ_LOCK(); 503240811Sglebius SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next); 504240811Sglebius PF_OVERLOADQ_UNLOCK(); 505240811Sglebius taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask); 506240233Sglebius 507240233Sglebius return (1); 508240233Sglebius} 509240233Sglebius 510240233Sglebiusstatic void 511264454Smmpf_overload_task(void *v, int pending) 512240233Sglebius{ 513240811Sglebius struct pf_overload_head queue; 514240811Sglebius struct pfr_addr p; 515240811Sglebius struct pf_overload_entry *pfoe, *pfoe1; 516240233Sglebius uint32_t killed = 0; 517240233Sglebius 518264454Smm CURVNET_SET((struct vnet *)v); 519264454Smm 520240811Sglebius PF_OVERLOADQ_LOCK(); 521264454Smm queue = V_pf_overloadqueue; 522264454Smm SLIST_INIT(&V_pf_overloadqueue); 523240811Sglebius PF_OVERLOADQ_UNLOCK(); 524240233Sglebius 525240811Sglebius bzero(&p, sizeof(p)); 526240811Sglebius SLIST_FOREACH(pfoe, &queue, next) { 527240811Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_TABLE]++; 528240811Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) { 529240811Sglebius printf("%s: blocking address ", __func__); 530240811Sglebius pf_print_host(&pfoe->addr, 0, pfoe->af); 531240811Sglebius printf("\n"); 532240811Sglebius } 533240233Sglebius 534240811Sglebius p.pfra_af = pfoe->af; 535240811Sglebius switch (pfoe->af) { 536240811Sglebius#ifdef INET 537240811Sglebius case AF_INET: 538240811Sglebius p.pfra_net = 32; 539240811Sglebius p.pfra_ip4addr = pfoe->addr.v4; 540240811Sglebius break; 541240811Sglebius#endif 542240811Sglebius#ifdef INET6 543240811Sglebius case AF_INET6: 544240811Sglebius p.pfra_net = 128; 545240811Sglebius p.pfra_ip6addr = pfoe->addr.v6; 546240811Sglebius break; 547240811Sglebius#endif 548240811Sglebius } 549240811Sglebius 550240811Sglebius PF_RULES_WLOCK(); 551240811Sglebius pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second); 552240811Sglebius PF_RULES_WUNLOCK(); 553240811Sglebius } 554240811Sglebius 555240811Sglebius /* 556240811Sglebius * Remove those entries, that don't need flushing. 557240811Sglebius */ 558240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 559240811Sglebius if (pfoe->rule->flush == 0) { 560240811Sglebius SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next); 561240811Sglebius free(pfoe, M_PFTEMP); 562240811Sglebius } else 563240811Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH]++; 564240811Sglebius 565240811Sglebius /* If nothing to flush, return. */ 566264454Smm if (SLIST_EMPTY(&queue)) { 567264454Smm CURVNET_RESTORE(); 568240811Sglebius return; 569264454Smm } 570240811Sglebius 571240233Sglebius for (int i = 0; i <= V_pf_hashmask; i++) { 572240233Sglebius struct pf_idhash *ih = &V_pf_idhash[i]; 573240233Sglebius struct pf_state_key *sk; 574240233Sglebius struct pf_state *s; 575240233Sglebius 576240233Sglebius PF_HASHROW_LOCK(ih); 577240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 578240233Sglebius sk = s->key[PF_SK_WIRE]; 579240811Sglebius SLIST_FOREACH(pfoe, &queue, next) 580240811Sglebius if (sk->af == pfoe->af && 581240811Sglebius ((pfoe->rule->flush & PF_FLUSH_GLOBAL) || 582240811Sglebius pfoe->rule == s->rule.ptr) && 583240811Sglebius ((pfoe->dir == PF_OUT && 584240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) || 585240811Sglebius (pfoe->dir == PF_IN && 586240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) { 587240233Sglebius s->timeout = PFTM_PURGE; 588240233Sglebius s->src.state = s->dst.state = TCPS_CLOSED; 589240233Sglebius killed++; 590145836Smlaier } 591145836Smlaier } 592240233Sglebius PF_HASHROW_UNLOCK(ih); 593145836Smlaier } 594240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 595240811Sglebius free(pfoe, M_PFTEMP); 596240233Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) 597240233Sglebius printf("%s: %u states killed", __func__, killed); 598264454Smm 599264454Smm CURVNET_RESTORE(); 600240233Sglebius} 601145836Smlaier 602240233Sglebius/* 603240233Sglebius * Can return locked on failure, so that we can consistently 604240233Sglebius * allocate and insert a new one. 605240233Sglebius */ 606240233Sglebiusstruct pf_src_node * 607240233Sglebiuspf_find_src_node(struct pf_addr *src, struct pf_rule *rule, sa_family_t af, 608240233Sglebius int returnlocked) 609240233Sglebius{ 610240233Sglebius struct pf_srchash *sh; 611240233Sglebius struct pf_src_node *n; 612240233Sglebius 613240233Sglebius V_pf_status.scounters[SCNT_SRC_NODE_SEARCH]++; 614240233Sglebius 615240233Sglebius sh = &V_pf_srchash[pf_hashsrc(src, af)]; 616240233Sglebius PF_HASHROW_LOCK(sh); 617240233Sglebius LIST_FOREACH(n, &sh->nodes, entry) 618240233Sglebius if (n->rule.ptr == rule && n->af == af && 619240233Sglebius ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) || 620240233Sglebius (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0))) 621240233Sglebius break; 622240233Sglebius if (n != NULL || returnlocked == 0) 623240233Sglebius PF_HASHROW_UNLOCK(sh); 624240233Sglebius 625240233Sglebius return (n); 626145836Smlaier} 627145836Smlaier 628240233Sglebiusstatic int 629130613Smlaierpf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule, 630130613Smlaier struct pf_addr *src, sa_family_t af) 631126258Smlaier{ 632126258Smlaier 633240233Sglebius KASSERT((rule->rule_flag & PFRULE_RULESRCTRACK || 634240233Sglebius rule->rpool.opts & PF_POOL_STICKYADDR), 635240233Sglebius ("%s for non-tracking rule %p", __func__, rule)); 636240233Sglebius 637240233Sglebius if (*sn == NULL) 638240233Sglebius *sn = pf_find_src_node(src, rule, af, 1); 639240233Sglebius 640130613Smlaier if (*sn == NULL) { 641240233Sglebius struct pf_srchash *sh = &V_pf_srchash[pf_hashsrc(src, af)]; 642240233Sglebius 643240233Sglebius PF_HASHROW_ASSERT(sh); 644240233Sglebius 645130613Smlaier if (!rule->max_src_nodes || 646263029Sglebius counter_u64_fetch(rule->src_nodes) < rule->max_src_nodes) 647240233Sglebius (*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO); 648145836Smlaier else 649223637Sbz V_pf_status.lcounters[LCNT_SRCNODES]++; 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); 668223637Sbz V_pf_status.scounters[SCNT_SRC_NODE_INSERT]++; 669223637Sbz V_pf_status.src_nodes++; 670130613Smlaier } else { 671130613Smlaier if (rule->max_src_states && 672145836Smlaier (*sn)->states >= rule->max_src_states) { 673223637Sbz V_pf_status.lcounters[LCNT_SRCSTATES]++; 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); 692240737Sglebius V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++; 693240737Sglebius V_pf_status.src_nodes--; 694261019Sglebius} 695240737Sglebius 696261019Sglebiusvoid 697261019Sglebiuspf_unlink_src_node(struct pf_src_node *src) 698261019Sglebius{ 699261019Sglebius struct pf_srchash *sh; 700261019Sglebius 701261019Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 702261019Sglebius PF_HASHROW_LOCK(sh); 703261019Sglebius pf_unlink_src_node_locked(src); 704261019Sglebius PF_HASHROW_UNLOCK(sh); 705240233Sglebius} 706240233Sglebius 707261019Sglebiusstatic void 708261019Sglebiuspf_free_src_node(struct pf_src_node *sn) 709261019Sglebius{ 710261019Sglebius 711261019Sglebius KASSERT(sn->states == 0, ("%s: %p has refs", __func__, sn)); 712261019Sglebius uma_zfree(V_pf_sources_z, sn); 713261019Sglebius} 714261019Sglebius 715261019Sglebiusu_int 716261019Sglebiuspf_free_src_nodes(struct pf_src_node_list *head) 717261019Sglebius{ 718261019Sglebius struct pf_src_node *sn, *tmp; 719261019Sglebius u_int count = 0; 720261019Sglebius 721261019Sglebius LIST_FOREACH_SAFE(sn, head, entry, tmp) { 722261019Sglebius pf_free_src_node(sn); 723261019Sglebius count++; 724261019Sglebius } 725261019Sglebius 726261019Sglebius return (count); 727261019Sglebius} 728261019Sglebius 729240233Sglebiusvoid 730265008Smmpf_mtag_initialize() 731265008Smm{ 732265008Smm 733265008Smm pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) + 734265008Smm sizeof(struct pf_mtag), NULL, NULL, pf_mtag_uminit, NULL, 735265008Smm UMA_ALIGN_PTR, 0); 736265008Smm} 737265008Smm 738265008Smm/* Per-vnet data storage structures initialization. */ 739265008Smmvoid 740240233Sglebiuspf_initialize() 741223637Sbz{ 742240233Sglebius struct pf_keyhash *kh; 743240233Sglebius struct pf_idhash *ih; 744240233Sglebius struct pf_srchash *sh; 745240233Sglebius u_int i; 746223637Sbz 747240233Sglebius TUNABLE_ULONG_FETCH("net.pf.states_hashsize", &V_pf_hashsize); 748240233Sglebius if (V_pf_hashsize == 0 || !powerof2(V_pf_hashsize)) 749240233Sglebius V_pf_hashsize = PF_HASHSIZ; 750240233Sglebius TUNABLE_ULONG_FETCH("net.pf.source_nodes_hashsize", &V_pf_srchashsize); 751240233Sglebius if (V_pf_srchashsize == 0 || !powerof2(V_pf_srchashsize)) 752240233Sglebius V_pf_srchashsize = PF_HASHSIZ / 4; 753240233Sglebius 754240233Sglebius V_pf_hashseed = arc4random(); 755240233Sglebius 756240233Sglebius /* States and state keys storage. */ 757240233Sglebius V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_state), 758240233Sglebius NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 759240233Sglebius V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z; 760240233Sglebius uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT); 761244347Spjd uma_zone_set_warning(V_pf_state_z, "PF states limit reached"); 762240233Sglebius 763240233Sglebius V_pf_state_key_z = uma_zcreate("pf state keys", 764240233Sglebius sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL, 765240233Sglebius UMA_ALIGN_PTR, 0); 766240233Sglebius V_pf_keyhash = malloc(V_pf_hashsize * sizeof(struct pf_keyhash), 767240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 768240233Sglebius V_pf_idhash = malloc(V_pf_hashsize * sizeof(struct pf_idhash), 769240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 770240233Sglebius V_pf_hashmask = V_pf_hashsize - 1; 771240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 772240233Sglebius i++, kh++, ih++) { 773251681Sglebius mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK); 774240233Sglebius mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF); 775223637Sbz } 776223637Sbz 777240233Sglebius /* Source nodes. */ 778240233Sglebius V_pf_sources_z = uma_zcreate("pf source nodes", 779240233Sglebius sizeof(struct pf_src_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 780240233Sglebius 0); 781240233Sglebius V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z; 782240233Sglebius uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT); 783244347Spjd uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached"); 784240233Sglebius V_pf_srchash = malloc(V_pf_srchashsize * sizeof(struct pf_srchash), 785240233Sglebius M_PFHASH, M_WAITOK|M_ZERO); 786240233Sglebius V_pf_srchashmask = V_pf_srchashsize - 1; 787240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) 788240233Sglebius mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF); 789223637Sbz 790240233Sglebius /* ALTQ */ 791240233Sglebius TAILQ_INIT(&V_pf_altqs[0]); 792240233Sglebius TAILQ_INIT(&V_pf_altqs[1]); 793240233Sglebius TAILQ_INIT(&V_pf_pabuf); 794240233Sglebius V_pf_altqs_active = &V_pf_altqs[0]; 795240233Sglebius V_pf_altqs_inactive = &V_pf_altqs[1]; 796240233Sglebius 797240233Sglebius 798240811Sglebius /* Send & overload+flush queues. */ 799240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 800240811Sglebius SLIST_INIT(&V_pf_overloadqueue); 801264454Smm TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, curvnet); 802240233Sglebius mtx_init(&pf_sendqueue_mtx, "pf send queue", NULL, MTX_DEF); 803240811Sglebius mtx_init(&pf_overloadqueue_mtx, "pf overload/flush queue", NULL, 804240811Sglebius MTX_DEF); 805240233Sglebius 806240233Sglebius /* Unlinked, but may be referenced rules. */ 807240233Sglebius TAILQ_INIT(&V_pf_unlinked_rules); 808240233Sglebius mtx_init(&pf_unlnkdrules_mtx, "pf unlinked rules", NULL, MTX_DEF); 809223637Sbz} 810223637Sbz 811240233Sglebiusvoid 812265008Smmpf_mtag_cleanup() 813265008Smm{ 814265008Smm 815265008Smm uma_zdestroy(pf_mtag_z); 816265008Smm} 817265008Smm 818265008Smmvoid 819240233Sglebiuspf_cleanup() 820223637Sbz{ 821240233Sglebius struct pf_keyhash *kh; 822240233Sglebius struct pf_idhash *ih; 823240233Sglebius struct pf_srchash *sh; 824240233Sglebius struct pf_send_entry *pfse, *next; 825240233Sglebius u_int i; 826223637Sbz 827240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 828240233Sglebius i++, kh++, ih++) { 829240233Sglebius KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty", 830240233Sglebius __func__)); 831240233Sglebius KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty", 832240233Sglebius __func__)); 833240233Sglebius mtx_destroy(&kh->lock); 834240233Sglebius mtx_destroy(&ih->lock); 835240233Sglebius } 836240233Sglebius free(V_pf_keyhash, M_PFHASH); 837240233Sglebius free(V_pf_idhash, M_PFHASH); 838240233Sglebius 839240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 840240233Sglebius KASSERT(LIST_EMPTY(&sh->nodes), 841240233Sglebius ("%s: source node hash not empty", __func__)); 842240233Sglebius mtx_destroy(&sh->lock); 843240233Sglebius } 844240233Sglebius free(V_pf_srchash, M_PFHASH); 845240233Sglebius 846240233Sglebius STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) { 847240233Sglebius m_freem(pfse->pfse_m); 848240233Sglebius free(pfse, M_PFTEMP); 849240233Sglebius } 850240233Sglebius 851240233Sglebius mtx_destroy(&pf_sendqueue_mtx); 852240811Sglebius mtx_destroy(&pf_overloadqueue_mtx); 853240233Sglebius mtx_destroy(&pf_unlnkdrules_mtx); 854240233Sglebius 855240233Sglebius uma_zdestroy(V_pf_sources_z); 856240233Sglebius uma_zdestroy(V_pf_state_z); 857240233Sglebius uma_zdestroy(V_pf_state_key_z); 858240233Sglebius} 859240233Sglebius 860240233Sglebiusstatic int 861265008Smmpf_mtag_uminit(void *mem, int size, int how) 862240233Sglebius{ 863240233Sglebius struct m_tag *t; 864240233Sglebius 865240233Sglebius t = (struct m_tag *)mem; 866240233Sglebius t->m_tag_cookie = MTAG_ABI_COMPAT; 867240233Sglebius t->m_tag_id = PACKET_TAG_PF; 868240233Sglebius t->m_tag_len = sizeof(struct pf_mtag); 869240233Sglebius t->m_tag_free = pf_mtag_free; 870240233Sglebius 871223637Sbz return (0); 872223637Sbz} 873223637Sbz 874240233Sglebiusstatic void 875240233Sglebiuspf_mtag_free(struct m_tag *t) 876130613Smlaier{ 877223637Sbz 878265008Smm uma_zfree(pf_mtag_z, t); 879240233Sglebius} 880223637Sbz 881240233Sglebiusstruct pf_mtag * 882240233Sglebiuspf_get_mtag(struct mbuf *m) 883240233Sglebius{ 884240233Sglebius struct m_tag *mtag; 885240233Sglebius 886240233Sglebius if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL) 887240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 888240233Sglebius 889265008Smm mtag = uma_zalloc(pf_mtag_z, M_NOWAIT); 890240233Sglebius if (mtag == NULL) 891240233Sglebius return (NULL); 892240233Sglebius bzero(mtag + 1, sizeof(struct pf_mtag)); 893240233Sglebius m_tag_prepend(m, mtag); 894240233Sglebius 895240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 896240233Sglebius} 897240233Sglebius 898240233Sglebiusstatic int 899240233Sglebiuspf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks, 900240233Sglebius struct pf_state *s) 901240233Sglebius{ 902251681Sglebius struct pf_keyhash *khs, *khw, *kh; 903240233Sglebius struct pf_state_key *sk, *cur; 904240233Sglebius struct pf_state *si, *olds = NULL; 905240233Sglebius int idx; 906240233Sglebius 907240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 908240233Sglebius KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__)); 909240233Sglebius KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__)); 910240233Sglebius 911240233Sglebius /* 912251681Sglebius * We need to lock hash slots of both keys. To avoid deadlock 913251681Sglebius * we always lock the slot with lower address first. Unlock order 914251681Sglebius * isn't important. 915251681Sglebius * 916251681Sglebius * We also need to lock ID hash slot before dropping key 917251681Sglebius * locks. On success we return with ID hash slot locked. 918251681Sglebius */ 919251681Sglebius 920251681Sglebius if (skw == sks) { 921251681Sglebius khs = khw = &V_pf_keyhash[pf_hashkey(skw)]; 922251681Sglebius PF_HASHROW_LOCK(khs); 923251681Sglebius } else { 924251681Sglebius khs = &V_pf_keyhash[pf_hashkey(sks)]; 925251681Sglebius khw = &V_pf_keyhash[pf_hashkey(skw)]; 926251681Sglebius if (khs == khw) { 927251681Sglebius PF_HASHROW_LOCK(khs); 928251681Sglebius } else if (khs < khw) { 929251681Sglebius PF_HASHROW_LOCK(khs); 930251681Sglebius PF_HASHROW_LOCK(khw); 931251681Sglebius } else { 932251681Sglebius PF_HASHROW_LOCK(khw); 933251681Sglebius PF_HASHROW_LOCK(khs); 934251681Sglebius } 935251681Sglebius } 936251681Sglebius 937251681Sglebius#define KEYS_UNLOCK() do { \ 938251681Sglebius if (khs != khw) { \ 939251681Sglebius PF_HASHROW_UNLOCK(khs); \ 940251681Sglebius PF_HASHROW_UNLOCK(khw); \ 941251681Sglebius } else \ 942251681Sglebius PF_HASHROW_UNLOCK(khs); \ 943251681Sglebius} while (0) 944251681Sglebius 945251681Sglebius /* 946240233Sglebius * First run: start with wire key. 947240233Sglebius */ 948240233Sglebius sk = skw; 949251681Sglebius kh = khw; 950240233Sglebius idx = PF_SK_WIRE; 951240233Sglebius 952240233Sglebiuskeyattach: 953240233Sglebius LIST_FOREACH(cur, &kh->keys, entry) 954240233Sglebius if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0) 955240233Sglebius break; 956240233Sglebius 957240233Sglebius if (cur != NULL) { 958240233Sglebius /* Key exists. Check for same kif, if none, add to key. */ 959240233Sglebius TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) { 960240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)]; 961240233Sglebius 962240233Sglebius PF_HASHROW_LOCK(ih); 963240233Sglebius if (si->kif == s->kif && 964240233Sglebius si->direction == s->direction) { 965223637Sbz if (sk->proto == IPPROTO_TCP && 966240233Sglebius si->src.state >= TCPS_FIN_WAIT_2 && 967240233Sglebius si->dst.state >= TCPS_FIN_WAIT_2) { 968251681Sglebius /* 969251681Sglebius * New state matches an old >FIN_WAIT_2 970251681Sglebius * state. We can't drop key hash locks, 971251681Sglebius * thus we can't unlink it properly. 972251681Sglebius * 973251681Sglebius * As a workaround we drop it into 974251681Sglebius * TCPS_CLOSED state, schedule purge 975251681Sglebius * ASAP and push it into the very end 976251681Sglebius * of the slot TAILQ, so that it won't 977251681Sglebius * conflict with our new state. 978251681Sglebius */ 979240233Sglebius si->src.state = si->dst.state = 980223637Sbz TCPS_CLOSED; 981251681Sglebius si->timeout = PFTM_PURGE; 982240233Sglebius olds = si; 983223637Sbz } else { 984223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 985223637Sbz printf("pf: %s key attach " 986223637Sbz "failed on %s: ", 987223637Sbz (idx == PF_SK_WIRE) ? 988223637Sbz "wire" : "stack", 989223637Sbz s->kif->pfik_name); 990223637Sbz pf_print_state_parts(s, 991223637Sbz (idx == PF_SK_WIRE) ? 992223637Sbz sk : NULL, 993223637Sbz (idx == PF_SK_STACK) ? 994223637Sbz sk : NULL); 995223637Sbz printf(", existing: "); 996240233Sglebius pf_print_state_parts(si, 997223637Sbz (idx == PF_SK_WIRE) ? 998223637Sbz sk : NULL, 999223637Sbz (idx == PF_SK_STACK) ? 1000223637Sbz sk : NULL); 1001223637Sbz printf("\n"); 1002223637Sbz } 1003240233Sglebius PF_HASHROW_UNLOCK(ih); 1004251681Sglebius KEYS_UNLOCK(); 1005240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1006240233Sglebius if (idx == PF_SK_STACK) 1007240233Sglebius pf_detach_state(s); 1008250522Sglebius return (EEXIST); /* collision! */ 1009223637Sbz } 1010223637Sbz } 1011240233Sglebius PF_HASHROW_UNLOCK(ih); 1012240233Sglebius } 1013240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1014223637Sbz s->key[idx] = cur; 1015240233Sglebius } else { 1016240233Sglebius LIST_INSERT_HEAD(&kh->keys, sk, entry); 1017223637Sbz s->key[idx] = sk; 1018126258Smlaier } 1019126258Smlaier 1020240233Sglebiusstateattach: 1021240233Sglebius /* List is sorted, if-bound states before floating. */ 1022223637Sbz if (s->kif == V_pfi_all) 1023240233Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]); 1024223637Sbz else 1025240233Sglebius TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]); 1026223637Sbz 1027251681Sglebius if (olds) { 1028251681Sglebius TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]); 1029251681Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds, 1030251681Sglebius key_list[idx]); 1031251681Sglebius olds = NULL; 1032251681Sglebius } 1033251681Sglebius 1034240233Sglebius /* 1035240233Sglebius * Attach done. See how should we (or should not?) 1036240233Sglebius * attach a second key. 1037240233Sglebius */ 1038240233Sglebius if (sks == skw) { 1039240233Sglebius s->key[PF_SK_STACK] = s->key[PF_SK_WIRE]; 1040240233Sglebius idx = PF_SK_STACK; 1041240233Sglebius sks = NULL; 1042240233Sglebius goto stateattach; 1043240233Sglebius } else if (sks != NULL) { 1044240233Sglebius /* 1045240233Sglebius * Continue attaching with stack key. 1046240233Sglebius */ 1047240233Sglebius sk = sks; 1048251681Sglebius kh = khs; 1049240233Sglebius idx = PF_SK_STACK; 1050240233Sglebius sks = NULL; 1051240233Sglebius goto keyattach; 1052240233Sglebius } 1053240233Sglebius 1054251681Sglebius PF_STATE_LOCK(s); 1055251681Sglebius KEYS_UNLOCK(); 1056251681Sglebius 1057240233Sglebius KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL, 1058240233Sglebius ("%s failure", __func__)); 1059240233Sglebius 1060223637Sbz return (0); 1061251681Sglebius#undef KEYS_UNLOCK 1062223637Sbz} 1063223637Sbz 1064240233Sglebiusstatic void 1065223637Sbzpf_detach_state(struct pf_state *s) 1066223637Sbz{ 1067240233Sglebius struct pf_state_key *sks = s->key[PF_SK_STACK]; 1068240233Sglebius struct pf_keyhash *kh; 1069223637Sbz 1070240233Sglebius if (sks != NULL) { 1071240233Sglebius kh = &V_pf_keyhash[pf_hashkey(sks)]; 1072240233Sglebius PF_HASHROW_LOCK(kh); 1073240233Sglebius if (s->key[PF_SK_STACK] != NULL) 1074240233Sglebius pf_state_key_detach(s, PF_SK_STACK); 1075240233Sglebius /* 1076240233Sglebius * If both point to same key, then we are done. 1077240233Sglebius */ 1078240233Sglebius if (sks == s->key[PF_SK_WIRE]) { 1079240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1080240233Sglebius PF_HASHROW_UNLOCK(kh); 1081240233Sglebius return; 1082240233Sglebius } 1083240233Sglebius PF_HASHROW_UNLOCK(kh); 1084240233Sglebius } 1085223637Sbz 1086240233Sglebius if (s->key[PF_SK_WIRE] != NULL) { 1087240233Sglebius kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])]; 1088240233Sglebius PF_HASHROW_LOCK(kh); 1089240233Sglebius if (s->key[PF_SK_WIRE] != NULL) 1090240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1091240233Sglebius PF_HASHROW_UNLOCK(kh); 1092240233Sglebius } 1093223637Sbz} 1094223637Sbz 1095240233Sglebiusstatic void 1096223637Sbzpf_state_key_detach(struct pf_state *s, int idx) 1097223637Sbz{ 1098240233Sglebius struct pf_state_key *sk = s->key[idx]; 1099240233Sglebius#ifdef INVARIANTS 1100240233Sglebius struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)]; 1101223637Sbz 1102240233Sglebius PF_HASHROW_ASSERT(kh); 1103223637Sbz#endif 1104240233Sglebius TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]); 1105240233Sglebius s->key[idx] = NULL; 1106223637Sbz 1107240233Sglebius if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) { 1108240233Sglebius LIST_REMOVE(sk, entry); 1109240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1110223637Sbz } 1111223637Sbz} 1112223637Sbz 1113240233Sglebiusstatic int 1114240233Sglebiuspf_state_key_ctor(void *mem, int size, void *arg, int flags) 1115240233Sglebius{ 1116240233Sglebius struct pf_state_key *sk = mem; 1117240233Sglebius 1118240233Sglebius bzero(sk, sizeof(struct pf_state_key_cmp)); 1119240233Sglebius TAILQ_INIT(&sk->states[PF_SK_WIRE]); 1120240233Sglebius TAILQ_INIT(&sk->states[PF_SK_STACK]); 1121240233Sglebius 1122240233Sglebius return (0); 1123240233Sglebius} 1124240233Sglebius 1125223637Sbzstruct pf_state_key * 1126240233Sglebiuspf_state_key_setup(struct pf_pdesc *pd, struct pf_addr *saddr, 1127240233Sglebius struct pf_addr *daddr, u_int16_t sport, u_int16_t dport) 1128223637Sbz{ 1129240233Sglebius struct pf_state_key *sk; 1130223637Sbz 1131240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1132240233Sglebius if (sk == NULL) 1133223637Sbz return (NULL); 1134223637Sbz 1135240233Sglebius PF_ACPY(&sk->addr[pd->sidx], saddr, pd->af); 1136240233Sglebius PF_ACPY(&sk->addr[pd->didx], daddr, pd->af); 1137240233Sglebius sk->port[pd->sidx] = sport; 1138240233Sglebius sk->port[pd->didx] = dport; 1139240233Sglebius sk->proto = pd->proto; 1140240233Sglebius sk->af = pd->af; 1141240233Sglebius 1142223637Sbz return (sk); 1143223637Sbz} 1144223637Sbz 1145240233Sglebiusstruct pf_state_key * 1146240233Sglebiuspf_state_key_clone(struct pf_state_key *orig) 1147223637Sbz{ 1148240233Sglebius struct pf_state_key *sk; 1149223637Sbz 1150240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1151240233Sglebius if (sk == NULL) 1152240233Sglebius return (NULL); 1153223637Sbz 1154240233Sglebius bcopy(orig, sk, sizeof(struct pf_state_key_cmp)); 1155223637Sbz 1156240233Sglebius return (sk); 1157223637Sbz} 1158223637Sbz 1159223637Sbzint 1160223637Sbzpf_state_insert(struct pfi_kif *kif, struct pf_state_key *skw, 1161223637Sbz struct pf_state_key *sks, struct pf_state *s) 1162223637Sbz{ 1163240233Sglebius struct pf_idhash *ih; 1164240233Sglebius struct pf_state *cur; 1165250522Sglebius int error; 1166223637Sbz 1167240233Sglebius KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]), 1168240233Sglebius ("%s: sks not pristine", __func__)); 1169240233Sglebius KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]), 1170240233Sglebius ("%s: skw not pristine", __func__)); 1171240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1172240233Sglebius 1173223637Sbz s->kif = kif; 1174223637Sbz 1175223637Sbz if (s->id == 0 && s->creatorid == 0) { 1176240233Sglebius /* XXX: should be atomic, but probability of collision low */ 1177240233Sglebius if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID) 1178240233Sglebius V_pf_stateid[curcpu] = 1; 1179240233Sglebius s->id |= (uint64_t )curcpu << PFID_CPUSHIFT; 1180240233Sglebius s->id = htobe64(s->id); 1181223637Sbz s->creatorid = V_pf_status.hostid; 1182130613Smlaier } 1183240233Sglebius 1184251681Sglebius /* Returns with ID locked on success. */ 1185250522Sglebius if ((error = pf_state_key_attach(skw, sks, s)) != 0) 1186250522Sglebius return (error); 1187244184Sglebius 1188240233Sglebius ih = &V_pf_idhash[PF_IDHASH(s)]; 1189251681Sglebius PF_HASHROW_ASSERT(ih); 1190240233Sglebius LIST_FOREACH(cur, &ih->states, entry) 1191240233Sglebius if (cur->id == s->id && cur->creatorid == s->creatorid) 1192240233Sglebius break; 1193240233Sglebius 1194240233Sglebius if (cur != NULL) { 1195240233Sglebius PF_HASHROW_UNLOCK(ih); 1196223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 1197250521Sglebius printf("pf: state ID collision: " 1198250312Sglebius "id: %016llx creatorid: %08x\n", 1199240233Sglebius (unsigned long long)be64toh(s->id), 1200240233Sglebius ntohl(s->creatorid)); 1201130613Smlaier } 1202223637Sbz pf_detach_state(s); 1203250522Sglebius return (EEXIST); 1204130613Smlaier } 1205240233Sglebius LIST_INSERT_HEAD(&ih->states, s, entry); 1206240233Sglebius /* One for keys, one for ID hash. */ 1207240233Sglebius refcount_init(&s->refs, 2); 1208240233Sglebius 1209223637Sbz V_pf_status.fcounters[FCNT_STATE_INSERT]++; 1210223637Sbz if (pfsync_insert_state_ptr != NULL) 1211223637Sbz pfsync_insert_state_ptr(s); 1212240233Sglebius 1213240233Sglebius /* Returns locked. */ 1214126258Smlaier return (0); 1215126258Smlaier} 1216126258Smlaier 1217240233Sglebius/* 1218240233Sglebius * Find state by ID: returns with locked row on success. 1219240233Sglebius */ 1220223637Sbzstruct pf_state * 1221240233Sglebiuspf_find_state_byid(uint64_t id, uint32_t creatorid) 1222223637Sbz{ 1223240233Sglebius struct pf_idhash *ih; 1224240233Sglebius struct pf_state *s; 1225240233Sglebius 1226223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1227223637Sbz 1228240233Sglebius ih = &V_pf_idhash[(be64toh(id) % (V_pf_hashmask + 1))]; 1229223637Sbz 1230240233Sglebius PF_HASHROW_LOCK(ih); 1231240233Sglebius LIST_FOREACH(s, &ih->states, entry) 1232240233Sglebius if (s->id == id && s->creatorid == creatorid) 1233240233Sglebius break; 1234223637Sbz 1235240233Sglebius if (s == NULL) 1236240233Sglebius PF_HASHROW_UNLOCK(ih); 1237240233Sglebius 1238240233Sglebius return (s); 1239223637Sbz} 1240223637Sbz 1241240233Sglebius/* 1242240233Sglebius * Find state by key. 1243240233Sglebius * Returns with ID hash slot locked on success. 1244240233Sglebius */ 1245240233Sglebiusstatic struct pf_state * 1246240233Sglebiuspf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int dir) 1247223637Sbz{ 1248240233Sglebius struct pf_keyhash *kh; 1249223637Sbz struct pf_state_key *sk; 1250240233Sglebius struct pf_state *s; 1251240233Sglebius int idx; 1252223637Sbz 1253223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1254223637Sbz 1255240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1256240233Sglebius 1257240233Sglebius PF_HASHROW_LOCK(kh); 1258240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1259240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1260240233Sglebius break; 1261240233Sglebius if (sk == NULL) { 1262240233Sglebius PF_HASHROW_UNLOCK(kh); 1263240233Sglebius return (NULL); 1264223637Sbz } 1265223637Sbz 1266240233Sglebius idx = (dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK); 1267223637Sbz 1268240233Sglebius /* List is sorted, if-bound states before floating ones. */ 1269240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) 1270240233Sglebius if (s->kif == V_pfi_all || s->kif == kif) { 1271240233Sglebius PF_STATE_LOCK(s); 1272240233Sglebius PF_HASHROW_UNLOCK(kh); 1273261018Sglebius if (s->timeout >= PFTM_MAX) { 1274240233Sglebius /* 1275261018Sglebius * State is either being processed by 1276261018Sglebius * pf_unlink_state() in an other thread, or 1277261018Sglebius * is scheduled for immediate expiry. 1278240233Sglebius */ 1279240233Sglebius PF_STATE_UNLOCK(s); 1280240233Sglebius return (NULL); 1281240233Sglebius } 1282240233Sglebius return (s); 1283240233Sglebius } 1284240233Sglebius PF_HASHROW_UNLOCK(kh); 1285223637Sbz 1286223637Sbz return (NULL); 1287223637Sbz} 1288223637Sbz 1289223637Sbzstruct pf_state * 1290223637Sbzpf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more) 1291223637Sbz{ 1292240233Sglebius struct pf_keyhash *kh; 1293223637Sbz struct pf_state_key *sk; 1294240233Sglebius struct pf_state *s, *ret = NULL; 1295240233Sglebius int idx, inout = 0; 1296223637Sbz 1297223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1298223637Sbz 1299240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1300223637Sbz 1301240233Sglebius PF_HASHROW_LOCK(kh); 1302240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1303240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1304240233Sglebius break; 1305240233Sglebius if (sk == NULL) { 1306240233Sglebius PF_HASHROW_UNLOCK(kh); 1307240233Sglebius return (NULL); 1308223637Sbz } 1309240233Sglebius switch (dir) { 1310240233Sglebius case PF_IN: 1311240233Sglebius idx = PF_SK_WIRE; 1312240233Sglebius break; 1313240233Sglebius case PF_OUT: 1314240233Sglebius idx = PF_SK_STACK; 1315240233Sglebius break; 1316240233Sglebius case PF_INOUT: 1317240233Sglebius idx = PF_SK_WIRE; 1318240233Sglebius inout = 1; 1319240233Sglebius break; 1320240233Sglebius default: 1321240233Sglebius panic("%s: dir %u", __func__, dir); 1322240233Sglebius } 1323240233Sglebiussecond_run: 1324240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 1325240233Sglebius if (more == NULL) { 1326240233Sglebius PF_HASHROW_UNLOCK(kh); 1327240233Sglebius return (s); 1328240233Sglebius } 1329240233Sglebius 1330240233Sglebius if (ret) 1331240233Sglebius (*more)++; 1332240233Sglebius else 1333240233Sglebius ret = s; 1334240233Sglebius } 1335240233Sglebius if (inout == 1) { 1336240233Sglebius inout = 0; 1337240233Sglebius idx = PF_SK_STACK; 1338240233Sglebius goto second_run; 1339240233Sglebius } 1340240233Sglebius PF_HASHROW_UNLOCK(kh); 1341240233Sglebius 1342240233Sglebius return (ret); 1343223637Sbz} 1344223637Sbz 1345223637Sbz/* END state table stuff */ 1346223637Sbz 1347240233Sglebiusstatic void 1348240233Sglebiuspf_send(struct pf_send_entry *pfse) 1349240233Sglebius{ 1350223637Sbz 1351240233Sglebius PF_SENDQ_LOCK(); 1352240233Sglebius STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next); 1353240233Sglebius PF_SENDQ_UNLOCK(); 1354240233Sglebius swi_sched(V_pf_swi_cookie, 0); 1355240233Sglebius} 1356240233Sglebius 1357126258Smlaiervoid 1358240233Sglebiuspf_intr(void *v) 1359240233Sglebius{ 1360240233Sglebius struct pf_send_head queue; 1361240233Sglebius struct pf_send_entry *pfse, *next; 1362240233Sglebius 1363240233Sglebius CURVNET_SET((struct vnet *)v); 1364240233Sglebius 1365240233Sglebius PF_SENDQ_LOCK(); 1366240233Sglebius queue = V_pf_sendqueue; 1367240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 1368240233Sglebius PF_SENDQ_UNLOCK(); 1369240233Sglebius 1370240233Sglebius STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) { 1371240233Sglebius switch (pfse->pfse_type) { 1372240233Sglebius#ifdef INET 1373240233Sglebius case PFSE_IP: 1374240233Sglebius ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL); 1375240233Sglebius break; 1376240233Sglebius case PFSE_ICMP: 1377240233Sglebius icmp_error(pfse->pfse_m, pfse->pfse_icmp_type, 1378240233Sglebius pfse->pfse_icmp_code, 0, pfse->pfse_icmp_mtu); 1379240233Sglebius break; 1380240233Sglebius#endif /* INET */ 1381240233Sglebius#ifdef INET6 1382240233Sglebius case PFSE_IP6: 1383240233Sglebius ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL, 1384240233Sglebius NULL); 1385240233Sglebius break; 1386240233Sglebius case PFSE_ICMP6: 1387240233Sglebius icmp6_error(pfse->pfse_m, pfse->pfse_icmp_type, 1388240233Sglebius pfse->pfse_icmp_code, pfse->pfse_icmp_mtu); 1389240233Sglebius break; 1390240233Sglebius#endif /* INET6 */ 1391240233Sglebius default: 1392240233Sglebius panic("%s: unknown type", __func__); 1393240233Sglebius } 1394240233Sglebius free(pfse, M_PFTEMP); 1395240233Sglebius } 1396240233Sglebius CURVNET_RESTORE(); 1397240233Sglebius} 1398240233Sglebius 1399240233Sglebiusvoid 1400171168Smlaierpf_purge_thread(void *v) 1401126258Smlaier{ 1402241039Sglebius u_int idx = 0; 1403171168Smlaier 1404223637Sbz CURVNET_SET((struct vnet *)v); 1405223637Sbz 1406171168Smlaier for (;;) { 1407240233Sglebius PF_RULES_RLOCK(); 1408240233Sglebius rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftm", hz / 10); 1409171168Smlaier 1410226527Sbz if (V_pf_end_threads) { 1411240233Sglebius /* 1412240233Sglebius * To cleanse up all kifs and rules we need 1413240233Sglebius * two runs: first one clears reference flags, 1414240233Sglebius * then pf_purge_expired_states() doesn't 1415240233Sglebius * raise them, and then second run frees. 1416240233Sglebius */ 1417240233Sglebius PF_RULES_RUNLOCK(); 1418240233Sglebius pf_purge_unlinked_rules(); 1419240233Sglebius pfi_kif_purge(); 1420171168Smlaier 1421240233Sglebius /* 1422240233Sglebius * Now purge everything. 1423240233Sglebius */ 1424241039Sglebius pf_purge_expired_states(0, V_pf_hashmask); 1425226527Sbz pf_purge_expired_fragments(); 1426240233Sglebius pf_purge_expired_src_nodes(); 1427240233Sglebius 1428240233Sglebius /* 1429240233Sglebius * Now all kifs & rules should be unreferenced, 1430240233Sglebius * thus should be successfully freed. 1431240233Sglebius */ 1432240233Sglebius pf_purge_unlinked_rules(); 1433240233Sglebius pfi_kif_purge(); 1434240233Sglebius 1435240233Sglebius /* 1436240233Sglebius * Announce success and exit. 1437240233Sglebius */ 1438240233Sglebius PF_RULES_RLOCK(); 1439226527Sbz V_pf_end_threads++; 1440240233Sglebius PF_RULES_RUNLOCK(); 1441226527Sbz wakeup(pf_purge_thread); 1442226527Sbz kproc_exit(0); 1443226527Sbz } 1444240233Sglebius PF_RULES_RUNLOCK(); 1445126258Smlaier 1446240233Sglebius /* Process 1/interval fraction of the state table every run. */ 1447241039Sglebius idx = pf_purge_expired_states(idx, V_pf_hashmask / 1448240233Sglebius (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10)); 1449196372Smlaier 1450240233Sglebius /* Purge other expired types every PFTM_INTERVAL seconds. */ 1451241039Sglebius if (idx == 0) { 1452240233Sglebius /* 1453240233Sglebius * Order is important: 1454240233Sglebius * - states and src nodes reference rules 1455240233Sglebius * - states and rules reference kifs 1456240233Sglebius */ 1457171168Smlaier pf_purge_expired_fragments(); 1458240233Sglebius pf_purge_expired_src_nodes(); 1459240233Sglebius pf_purge_unlinked_rules(); 1460240233Sglebius pfi_kif_purge(); 1461171168Smlaier } 1462171168Smlaier } 1463240233Sglebius /* not reached */ 1464223637Sbz CURVNET_RESTORE(); 1465126258Smlaier} 1466126258Smlaier 1467126258Smlaieru_int32_t 1468126258Smlaierpf_state_expires(const struct pf_state *state) 1469126258Smlaier{ 1470126258Smlaier u_int32_t timeout; 1471126258Smlaier u_int32_t start; 1472126258Smlaier u_int32_t end; 1473126258Smlaier u_int32_t states; 1474126258Smlaier 1475126258Smlaier /* handle all PFTM_* > PFTM_MAX here */ 1476126258Smlaier if (state->timeout == PFTM_PURGE) 1477240233Sglebius return (time_uptime); 1478171168Smlaier KASSERT(state->timeout != PFTM_UNLINKED, 1479171168Smlaier ("pf_state_expires: timeout == PFTM_UNLINKED")); 1480240233Sglebius KASSERT((state->timeout < PFTM_MAX), 1481126261Smlaier ("pf_state_expires: timeout > PFTM_MAX")); 1482126258Smlaier timeout = state->rule.ptr->timeout[state->timeout]; 1483126258Smlaier if (!timeout) 1484223637Sbz timeout = V_pf_default_rule.timeout[state->timeout]; 1485126258Smlaier start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START]; 1486126258Smlaier if (start) { 1487126258Smlaier end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END]; 1488263029Sglebius states = counter_u64_fetch(state->rule.ptr->states_cur); 1489126258Smlaier } else { 1490223637Sbz start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START]; 1491223637Sbz end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END]; 1492223637Sbz states = V_pf_status.states; 1493126258Smlaier } 1494126258Smlaier if (end && states > start && start < end) { 1495126258Smlaier if (states < end) 1496126258Smlaier return (state->expire + timeout * (end - states) / 1497126258Smlaier (end - start)); 1498126258Smlaier else 1499240233Sglebius return (time_uptime); 1500126258Smlaier } 1501126258Smlaier return (state->expire + timeout); 1502126258Smlaier} 1503126258Smlaier 1504126258Smlaiervoid 1505240233Sglebiuspf_purge_expired_src_nodes() 1506126258Smlaier{ 1507261019Sglebius struct pf_src_node_list freelist; 1508240233Sglebius struct pf_srchash *sh; 1509240233Sglebius struct pf_src_node *cur, *next; 1510240233Sglebius int i; 1511126258Smlaier 1512261019Sglebius LIST_INIT(&freelist); 1513240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 1514240233Sglebius PF_HASHROW_LOCK(sh); 1515240233Sglebius LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next) 1516261018Sglebius if (cur->states == 0 && cur->expire <= time_uptime) { 1517261019Sglebius pf_unlink_src_node_locked(cur); 1518261019Sglebius LIST_INSERT_HEAD(&freelist, cur, entry); 1519240233Sglebius } else if (cur->rule.ptr != NULL) 1520240233Sglebius cur->rule.ptr->rule_flag |= PFRULE_REFS; 1521240233Sglebius PF_HASHROW_UNLOCK(sh); 1522223637Sbz } 1523261019Sglebius 1524261019Sglebius pf_free_src_nodes(&freelist); 1525130613Smlaier} 1526126258Smlaier 1527240233Sglebiusstatic void 1528130613Smlaierpf_src_tree_remove_state(struct pf_state *s) 1529130613Smlaier{ 1530130613Smlaier u_int32_t timeout; 1531126258Smlaier 1532130613Smlaier if (s->src_node != NULL) { 1533223637Sbz if (s->src.tcp_est) 1534223637Sbz --s->src_node->conn; 1535261018Sglebius if (--s->src_node->states == 0) { 1536130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1537130613Smlaier if (!timeout) 1538130613Smlaier timeout = 1539223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1540240233Sglebius s->src_node->expire = time_uptime + timeout; 1541130613Smlaier } 1542130613Smlaier } 1543130613Smlaier if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) { 1544261018Sglebius if (--s->nat_src_node->states == 0) { 1545130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1546130613Smlaier if (!timeout) 1547130613Smlaier timeout = 1548223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1549240233Sglebius s->nat_src_node->expire = time_uptime + timeout; 1550130613Smlaier } 1551130613Smlaier } 1552130613Smlaier s->src_node = s->nat_src_node = NULL; 1553130613Smlaier} 1554126258Smlaier 1555240233Sglebius/* 1556240233Sglebius * Unlink and potentilly free a state. Function may be 1557240233Sglebius * called with ID hash row locked, but always returns 1558240233Sglebius * unlocked, since it needs to go through key hash locking. 1559240233Sglebius */ 1560240233Sglebiusint 1561240233Sglebiuspf_unlink_state(struct pf_state *s, u_int flags) 1562145836Smlaier{ 1563240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; 1564223637Sbz 1565240233Sglebius if ((flags & PF_ENTER_LOCKED) == 0) 1566240233Sglebius PF_HASHROW_LOCK(ih); 1567240233Sglebius else 1568240233Sglebius PF_HASHROW_ASSERT(ih); 1569240233Sglebius 1570240233Sglebius if (s->timeout == PFTM_UNLINKED) { 1571240233Sglebius /* 1572240233Sglebius * State is being processed 1573240233Sglebius * by pf_unlink_state() in 1574240233Sglebius * an other thread. 1575240233Sglebius */ 1576240233Sglebius PF_HASHROW_UNLOCK(ih); 1577240233Sglebius return (0); /* XXXGL: undefined actually */ 1578240233Sglebius } 1579240233Sglebius 1580240233Sglebius if (s->src.state == PF_TCPS_PROXY_DST) { 1581223637Sbz /* XXX wire key the right one? */ 1582240233Sglebius pf_send_tcp(NULL, s->rule.ptr, s->key[PF_SK_WIRE]->af, 1583240233Sglebius &s->key[PF_SK_WIRE]->addr[1], 1584240233Sglebius &s->key[PF_SK_WIRE]->addr[0], 1585240233Sglebius s->key[PF_SK_WIRE]->port[1], 1586240233Sglebius s->key[PF_SK_WIRE]->port[0], 1587240233Sglebius s->src.seqhi, s->src.seqlo + 1, 1588240233Sglebius TH_RST|TH_ACK, 0, 0, 0, 1, s->tag, NULL); 1589171168Smlaier } 1590240233Sglebius 1591240233Sglebius LIST_REMOVE(s, entry); 1592240233Sglebius pf_src_tree_remove_state(s); 1593240233Sglebius 1594223637Sbz if (pfsync_delete_state_ptr != NULL) 1595240233Sglebius pfsync_delete_state_ptr(s); 1596240233Sglebius 1597263029Sglebius STATE_DEC_COUNTERS(s); 1598243944Sglebius 1599243941Sglebius s->timeout = PFTM_UNLINKED; 1600243941Sglebius 1601243941Sglebius PF_HASHROW_UNLOCK(ih); 1602243941Sglebius 1603240233Sglebius pf_detach_state(s); 1604240233Sglebius refcount_release(&s->refs); 1605240233Sglebius 1606240233Sglebius return (pf_release_state(s)); 1607171168Smlaier} 1608171168Smlaier 1609171168Smlaiervoid 1610171168Smlaierpf_free_state(struct pf_state *cur) 1611171168Smlaier{ 1612223637Sbz 1613240233Sglebius KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 1614240233Sglebius KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 1615240233Sglebius cur->timeout)); 1616243944Sglebius 1617145836Smlaier pf_normalize_tcp_cleanup(cur); 1618240233Sglebius uma_zfree(V_pf_state_z, cur); 1619223637Sbz V_pf_status.fcounters[FCNT_STATE_REMOVALS]++; 1620145836Smlaier} 1621145836Smlaier 1622240233Sglebius/* 1623240233Sglebius * Called only from pf_purge_thread(), thus serialized. 1624240233Sglebius */ 1625241039Sglebiusstatic u_int 1626241039Sglebiuspf_purge_expired_states(u_int i, int maxcheck) 1627130613Smlaier{ 1628240233Sglebius struct pf_idhash *ih; 1629240233Sglebius struct pf_state *s; 1630240233Sglebius 1631240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1632240233Sglebius 1633240233Sglebius /* 1634240233Sglebius * Go through hash and unlink states that expire now. 1635240233Sglebius */ 1636240233Sglebius while (maxcheck > 0) { 1637240233Sglebius 1638240233Sglebius ih = &V_pf_idhash[i]; 1639240233Sglebiusrelock: 1640240233Sglebius PF_HASHROW_LOCK(ih); 1641240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 1642240233Sglebius if (pf_state_expires(s) <= time_uptime) { 1643240233Sglebius V_pf_status.states -= 1644240233Sglebius pf_unlink_state(s, PF_ENTER_LOCKED); 1645240233Sglebius goto relock; 1646171168Smlaier } 1647240233Sglebius s->rule.ptr->rule_flag |= PFRULE_REFS; 1648240233Sglebius if (s->nat_rule.ptr != NULL) 1649240233Sglebius s->nat_rule.ptr->rule_flag |= PFRULE_REFS; 1650240233Sglebius if (s->anchor.ptr != NULL) 1651240233Sglebius s->anchor.ptr->rule_flag |= PFRULE_REFS; 1652240233Sglebius s->kif->pfik_flags |= PFI_IFLAG_REFS; 1653240233Sglebius if (s->rt_kif) 1654240233Sglebius s->rt_kif->pfik_flags |= PFI_IFLAG_REFS; 1655171168Smlaier } 1656240233Sglebius PF_HASHROW_UNLOCK(ih); 1657241039Sglebius 1658241039Sglebius /* Return when we hit end of hash. */ 1659241039Sglebius if (++i > V_pf_hashmask) { 1660241039Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1661241039Sglebius return (0); 1662241039Sglebius } 1663241039Sglebius 1664240233Sglebius maxcheck--; 1665126258Smlaier } 1666171168Smlaier 1667240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1668196372Smlaier 1669241039Sglebius return (i); 1670126258Smlaier} 1671126258Smlaier 1672240233Sglebiusstatic void 1673240233Sglebiuspf_purge_unlinked_rules() 1674126258Smlaier{ 1675240233Sglebius struct pf_rulequeue tmpq; 1676240233Sglebius struct pf_rule *r, *r1; 1677126258Smlaier 1678240233Sglebius /* 1679240811Sglebius * If we have overloading task pending, then we'd 1680240811Sglebius * better skip purging this time. There is a tiny 1681240811Sglebius * probability that overloading task references 1682240811Sglebius * an already unlinked rule. 1683240811Sglebius */ 1684240811Sglebius PF_OVERLOADQ_LOCK(); 1685240811Sglebius if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 1686240811Sglebius PF_OVERLOADQ_UNLOCK(); 1687240811Sglebius return; 1688240811Sglebius } 1689240811Sglebius PF_OVERLOADQ_UNLOCK(); 1690240811Sglebius 1691240811Sglebius /* 1692240233Sglebius * Do naive mark-and-sweep garbage collecting of old rules. 1693240233Sglebius * Reference flag is raised by pf_purge_expired_states() 1694240233Sglebius * and pf_purge_expired_src_nodes(). 1695240233Sglebius * 1696240233Sglebius * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 1697240233Sglebius * use a temporary queue. 1698240233Sglebius */ 1699240233Sglebius TAILQ_INIT(&tmpq); 1700240233Sglebius PF_UNLNKDRULES_LOCK(); 1701240233Sglebius TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 1702240233Sglebius if (!(r->rule_flag & PFRULE_REFS)) { 1703240233Sglebius TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 1704240233Sglebius TAILQ_INSERT_TAIL(&tmpq, r, entries); 1705240233Sglebius } else 1706240233Sglebius r->rule_flag &= ~PFRULE_REFS; 1707240233Sglebius } 1708240233Sglebius PF_UNLNKDRULES_UNLOCK(); 1709126258Smlaier 1710240233Sglebius if (!TAILQ_EMPTY(&tmpq)) { 1711240233Sglebius PF_RULES_WLOCK(); 1712240233Sglebius TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 1713240233Sglebius TAILQ_REMOVE(&tmpq, r, entries); 1714240233Sglebius pf_free_rule(r); 1715240233Sglebius } 1716240233Sglebius PF_RULES_WUNLOCK(); 1717240233Sglebius } 1718126258Smlaier} 1719126258Smlaier 1720126258Smlaiervoid 1721126258Smlaierpf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 1722126258Smlaier{ 1723126258Smlaier switch (af) { 1724126258Smlaier#ifdef INET 1725126258Smlaier case AF_INET: { 1726126258Smlaier u_int32_t a = ntohl(addr->addr32[0]); 1727126258Smlaier printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 1728126258Smlaier (a>>8)&255, a&255); 1729126258Smlaier if (p) { 1730126258Smlaier p = ntohs(p); 1731126258Smlaier printf(":%u", p); 1732126258Smlaier } 1733126258Smlaier break; 1734126258Smlaier } 1735126258Smlaier#endif /* INET */ 1736126258Smlaier#ifdef INET6 1737126258Smlaier case AF_INET6: { 1738126258Smlaier u_int16_t b; 1739223637Sbz u_int8_t i, curstart, curend, maxstart, maxend; 1740223637Sbz curstart = curend = maxstart = maxend = 255; 1741126258Smlaier for (i = 0; i < 8; i++) { 1742126258Smlaier if (!addr->addr16[i]) { 1743126258Smlaier if (curstart == 255) 1744126258Smlaier curstart = i; 1745223637Sbz curend = i; 1746126258Smlaier } else { 1747223637Sbz if ((curend - curstart) > 1748223637Sbz (maxend - maxstart)) { 1749223637Sbz maxstart = curstart; 1750223637Sbz maxend = curend; 1751126258Smlaier } 1752223637Sbz curstart = curend = 255; 1753126258Smlaier } 1754126258Smlaier } 1755223637Sbz if ((curend - curstart) > 1756223637Sbz (maxend - maxstart)) { 1757223637Sbz maxstart = curstart; 1758223637Sbz maxend = curend; 1759223637Sbz } 1760126258Smlaier for (i = 0; i < 8; i++) { 1761126258Smlaier if (i >= maxstart && i <= maxend) { 1762223637Sbz if (i == 0) 1763223637Sbz printf(":"); 1764223637Sbz if (i == maxend) 1765223637Sbz printf(":"); 1766126258Smlaier } else { 1767126258Smlaier b = ntohs(addr->addr16[i]); 1768126258Smlaier printf("%x", b); 1769126258Smlaier if (i < 7) 1770126258Smlaier printf(":"); 1771126258Smlaier } 1772126258Smlaier } 1773126258Smlaier if (p) { 1774126258Smlaier p = ntohs(p); 1775126258Smlaier printf("[%u]", p); 1776126258Smlaier } 1777126258Smlaier break; 1778126258Smlaier } 1779126258Smlaier#endif /* INET6 */ 1780126258Smlaier } 1781126258Smlaier} 1782126258Smlaier 1783126258Smlaiervoid 1784126258Smlaierpf_print_state(struct pf_state *s) 1785126258Smlaier{ 1786223637Sbz pf_print_state_parts(s, NULL, NULL); 1787223637Sbz} 1788223637Sbz 1789240233Sglebiusstatic void 1790223637Sbzpf_print_state_parts(struct pf_state *s, 1791223637Sbz struct pf_state_key *skwp, struct pf_state_key *sksp) 1792223637Sbz{ 1793223637Sbz struct pf_state_key *skw, *sks; 1794223637Sbz u_int8_t proto, dir; 1795223637Sbz 1796223637Sbz /* Do our best to fill these, but they're skipped if NULL */ 1797223637Sbz skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 1798223637Sbz sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 1799223637Sbz proto = skw ? skw->proto : (sks ? sks->proto : 0); 1800223637Sbz dir = s ? s->direction : 0; 1801223637Sbz 1802223637Sbz switch (proto) { 1803223637Sbz case IPPROTO_IPV4: 1804223637Sbz printf("IPv4"); 1805223637Sbz break; 1806223637Sbz case IPPROTO_IPV6: 1807223637Sbz printf("IPv6"); 1808223637Sbz break; 1809126258Smlaier case IPPROTO_TCP: 1810223637Sbz printf("TCP"); 1811126258Smlaier break; 1812126258Smlaier case IPPROTO_UDP: 1813223637Sbz printf("UDP"); 1814126258Smlaier break; 1815126258Smlaier case IPPROTO_ICMP: 1816223637Sbz printf("ICMP"); 1817126258Smlaier break; 1818126258Smlaier case IPPROTO_ICMPV6: 1819223637Sbz printf("ICMPv6"); 1820126258Smlaier break; 1821126258Smlaier default: 1822223637Sbz printf("%u", skw->proto); 1823126258Smlaier break; 1824126258Smlaier } 1825223637Sbz switch (dir) { 1826223637Sbz case PF_IN: 1827223637Sbz printf(" in"); 1828223637Sbz break; 1829223637Sbz case PF_OUT: 1830223637Sbz printf(" out"); 1831223637Sbz break; 1832223637Sbz } 1833223637Sbz if (skw) { 1834223637Sbz printf(" wire: "); 1835223637Sbz pf_print_host(&skw->addr[0], skw->port[0], skw->af); 1836223637Sbz printf(" "); 1837223637Sbz pf_print_host(&skw->addr[1], skw->port[1], skw->af); 1838223637Sbz } 1839223637Sbz if (sks) { 1840223637Sbz printf(" stack: "); 1841223637Sbz if (sks != skw) { 1842223637Sbz pf_print_host(&sks->addr[0], sks->port[0], sks->af); 1843223637Sbz printf(" "); 1844223637Sbz pf_print_host(&sks->addr[1], sks->port[1], sks->af); 1845223637Sbz } else 1846223637Sbz printf("-"); 1847223637Sbz } 1848223637Sbz if (s) { 1849223637Sbz if (proto == IPPROTO_TCP) { 1850223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1851223637Sbz s->src.seqlo, s->src.seqhi, 1852223637Sbz s->src.max_win, s->src.seqdiff); 1853223637Sbz if (s->src.wscale && s->dst.wscale) 1854223637Sbz printf(" wscale=%u", 1855223637Sbz s->src.wscale & PF_WSCALE_MASK); 1856223637Sbz printf("]"); 1857223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1858223637Sbz s->dst.seqlo, s->dst.seqhi, 1859223637Sbz s->dst.max_win, s->dst.seqdiff); 1860223637Sbz if (s->src.wscale && s->dst.wscale) 1861223637Sbz printf(" wscale=%u", 1862223637Sbz s->dst.wscale & PF_WSCALE_MASK); 1863223637Sbz printf("]"); 1864223637Sbz } 1865223637Sbz printf(" %u:%u", s->src.state, s->dst.state); 1866223637Sbz } 1867126258Smlaier} 1868126258Smlaier 1869126258Smlaiervoid 1870126258Smlaierpf_print_flags(u_int8_t f) 1871126258Smlaier{ 1872126258Smlaier if (f) 1873126258Smlaier printf(" "); 1874126258Smlaier if (f & TH_FIN) 1875126258Smlaier printf("F"); 1876126258Smlaier if (f & TH_SYN) 1877126258Smlaier printf("S"); 1878126258Smlaier if (f & TH_RST) 1879126258Smlaier printf("R"); 1880126258Smlaier if (f & TH_PUSH) 1881126258Smlaier printf("P"); 1882126258Smlaier if (f & TH_ACK) 1883126258Smlaier printf("A"); 1884126258Smlaier if (f & TH_URG) 1885126258Smlaier printf("U"); 1886126258Smlaier if (f & TH_ECE) 1887126258Smlaier printf("E"); 1888126258Smlaier if (f & TH_CWR) 1889126258Smlaier printf("W"); 1890126258Smlaier} 1891126258Smlaier 1892126258Smlaier#define PF_SET_SKIP_STEPS(i) \ 1893126258Smlaier do { \ 1894126258Smlaier while (head[i] != cur) { \ 1895126258Smlaier head[i]->skip[i].ptr = cur; \ 1896126258Smlaier head[i] = TAILQ_NEXT(head[i], entries); \ 1897126258Smlaier } \ 1898126258Smlaier } while (0) 1899126258Smlaier 1900126258Smlaiervoid 1901126258Smlaierpf_calc_skip_steps(struct pf_rulequeue *rules) 1902126258Smlaier{ 1903126258Smlaier struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT]; 1904126258Smlaier int i; 1905126258Smlaier 1906126258Smlaier cur = TAILQ_FIRST(rules); 1907126258Smlaier prev = cur; 1908126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1909126258Smlaier head[i] = cur; 1910126258Smlaier while (cur != NULL) { 1911126258Smlaier 1912130613Smlaier if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 1913126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_IFP); 1914126258Smlaier if (cur->direction != prev->direction) 1915126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DIR); 1916126258Smlaier if (cur->af != prev->af) 1917126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_AF); 1918126258Smlaier if (cur->proto != prev->proto) 1919126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 1920145836Smlaier if (cur->src.neg != prev->src.neg || 1921126258Smlaier pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 1922126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 1923126258Smlaier if (cur->src.port[0] != prev->src.port[0] || 1924126258Smlaier cur->src.port[1] != prev->src.port[1] || 1925126258Smlaier cur->src.port_op != prev->src.port_op) 1926126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 1927145836Smlaier if (cur->dst.neg != prev->dst.neg || 1928126258Smlaier pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 1929126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 1930126258Smlaier if (cur->dst.port[0] != prev->dst.port[0] || 1931126258Smlaier cur->dst.port[1] != prev->dst.port[1] || 1932126258Smlaier cur->dst.port_op != prev->dst.port_op) 1933126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 1934126258Smlaier 1935126258Smlaier prev = cur; 1936126258Smlaier cur = TAILQ_NEXT(cur, entries); 1937126258Smlaier } 1938126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1939126258Smlaier PF_SET_SKIP_STEPS(i); 1940126258Smlaier} 1941126258Smlaier 1942240233Sglebiusstatic int 1943126258Smlaierpf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 1944126258Smlaier{ 1945126258Smlaier if (aw1->type != aw2->type) 1946126258Smlaier return (1); 1947126258Smlaier switch (aw1->type) { 1948126258Smlaier case PF_ADDR_ADDRMASK: 1949223637Sbz case PF_ADDR_RANGE: 1950126258Smlaier if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0)) 1951126258Smlaier return (1); 1952126258Smlaier if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0)) 1953126258Smlaier return (1); 1954126258Smlaier return (0); 1955126258Smlaier case PF_ADDR_DYNIFTL: 1956130613Smlaier return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 1957126258Smlaier case PF_ADDR_NOROUTE: 1958171168Smlaier case PF_ADDR_URPFFAILED: 1959126258Smlaier return (0); 1960126258Smlaier case PF_ADDR_TABLE: 1961126258Smlaier return (aw1->p.tbl != aw2->p.tbl); 1962126258Smlaier default: 1963126258Smlaier printf("invalid address type: %d\n", aw1->type); 1964126258Smlaier return (1); 1965126258Smlaier } 1966126258Smlaier} 1967126258Smlaier 1968126258Smlaieru_int16_t 1969126258Smlaierpf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 1970126258Smlaier{ 1971126258Smlaier u_int32_t l; 1972126258Smlaier 1973126258Smlaier if (udp && !cksum) 1974126258Smlaier return (0x0000); 1975126258Smlaier l = cksum + old - new; 1976126258Smlaier l = (l >> 16) + (l & 65535); 1977126258Smlaier l = l & 65535; 1978126258Smlaier if (udp && !l) 1979126258Smlaier return (0xFFFF); 1980126258Smlaier return (l); 1981126258Smlaier} 1982126258Smlaier 1983240233Sglebiusstatic void 1984126258Smlaierpf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc, 1985126258Smlaier struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af) 1986126258Smlaier{ 1987126258Smlaier struct pf_addr ao; 1988126258Smlaier u_int16_t po = *p; 1989126258Smlaier 1990126258Smlaier PF_ACPY(&ao, a, af); 1991126258Smlaier PF_ACPY(a, an, af); 1992126258Smlaier 1993126258Smlaier *p = pn; 1994126258Smlaier 1995126258Smlaier switch (af) { 1996126258Smlaier#ifdef INET 1997126258Smlaier case AF_INET: 1998126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 1999126258Smlaier ao.addr16[0], an->addr16[0], 0), 2000126258Smlaier ao.addr16[1], an->addr16[1], 0); 2001126258Smlaier *p = pn; 2002126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 2003126258Smlaier ao.addr16[0], an->addr16[0], u), 2004126258Smlaier ao.addr16[1], an->addr16[1], u), 2005126258Smlaier po, pn, u); 2006126258Smlaier break; 2007126258Smlaier#endif /* INET */ 2008126258Smlaier#ifdef INET6 2009126258Smlaier case AF_INET6: 2010126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2011126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2012126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 2013126258Smlaier ao.addr16[0], an->addr16[0], u), 2014126258Smlaier ao.addr16[1], an->addr16[1], u), 2015126258Smlaier ao.addr16[2], an->addr16[2], u), 2016126258Smlaier ao.addr16[3], an->addr16[3], u), 2017126258Smlaier ao.addr16[4], an->addr16[4], u), 2018126258Smlaier ao.addr16[5], an->addr16[5], u), 2019126258Smlaier ao.addr16[6], an->addr16[6], u), 2020126258Smlaier ao.addr16[7], an->addr16[7], u), 2021126258Smlaier po, pn, u); 2022126258Smlaier break; 2023126258Smlaier#endif /* INET6 */ 2024126258Smlaier } 2025126258Smlaier} 2026126258Smlaier 2027126258Smlaier 2028126258Smlaier/* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 2029126258Smlaiervoid 2030126258Smlaierpf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 2031126258Smlaier{ 2032126258Smlaier u_int32_t ao; 2033126258Smlaier 2034126258Smlaier memcpy(&ao, a, sizeof(ao)); 2035126258Smlaier memcpy(a, &an, sizeof(u_int32_t)); 2036126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 2037126258Smlaier ao % 65536, an % 65536, u); 2038126258Smlaier} 2039126258Smlaier 2040126258Smlaier#ifdef INET6 2041240233Sglebiusstatic void 2042126258Smlaierpf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 2043126258Smlaier{ 2044126258Smlaier struct pf_addr ao; 2045126258Smlaier 2046126258Smlaier PF_ACPY(&ao, a, AF_INET6); 2047126258Smlaier PF_ACPY(a, an, AF_INET6); 2048126258Smlaier 2049126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2050126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2051126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*c, 2052126258Smlaier ao.addr16[0], an->addr16[0], u), 2053126258Smlaier ao.addr16[1], an->addr16[1], u), 2054126258Smlaier ao.addr16[2], an->addr16[2], u), 2055126258Smlaier ao.addr16[3], an->addr16[3], u), 2056126258Smlaier ao.addr16[4], an->addr16[4], u), 2057126258Smlaier ao.addr16[5], an->addr16[5], u), 2058126258Smlaier ao.addr16[6], an->addr16[6], u), 2059126258Smlaier ao.addr16[7], an->addr16[7], u); 2060126258Smlaier} 2061126258Smlaier#endif /* INET6 */ 2062126258Smlaier 2063240233Sglebiusstatic void 2064126258Smlaierpf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 2065126258Smlaier struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 2066126258Smlaier u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 2067126258Smlaier{ 2068126258Smlaier struct pf_addr oia, ooa; 2069126258Smlaier 2070126258Smlaier PF_ACPY(&oia, ia, af); 2071223637Sbz if (oa) 2072223637Sbz PF_ACPY(&ooa, oa, af); 2073126258Smlaier 2074126258Smlaier /* Change inner protocol port, fix inner protocol checksum. */ 2075126258Smlaier if (ip != NULL) { 2076126258Smlaier u_int16_t oip = *ip; 2077223637Sbz u_int32_t opc; 2078126258Smlaier 2079126258Smlaier if (pc != NULL) 2080126258Smlaier opc = *pc; 2081126258Smlaier *ip = np; 2082126258Smlaier if (pc != NULL) 2083126258Smlaier *pc = pf_cksum_fixup(*pc, oip, *ip, u); 2084126258Smlaier *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 2085126258Smlaier if (pc != NULL) 2086126258Smlaier *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 2087126258Smlaier } 2088126258Smlaier /* Change inner ip address, fix inner ip and icmp checksums. */ 2089126258Smlaier PF_ACPY(ia, na, af); 2090126258Smlaier switch (af) { 2091126258Smlaier#ifdef INET 2092126258Smlaier case AF_INET: { 2093126258Smlaier u_int32_t oh2c = *h2c; 2094126258Smlaier 2095126258Smlaier *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 2096126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2097126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2098126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2099126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2100126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2101126258Smlaier *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 2102126258Smlaier break; 2103126258Smlaier } 2104126258Smlaier#endif /* INET */ 2105126258Smlaier#ifdef INET6 2106126258Smlaier case AF_INET6: 2107126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2108126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2109126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*ic, 2110126258Smlaier oia.addr16[0], ia->addr16[0], u), 2111126258Smlaier oia.addr16[1], ia->addr16[1], u), 2112126258Smlaier oia.addr16[2], ia->addr16[2], u), 2113126258Smlaier oia.addr16[3], ia->addr16[3], u), 2114126258Smlaier oia.addr16[4], ia->addr16[4], u), 2115126258Smlaier oia.addr16[5], ia->addr16[5], u), 2116126258Smlaier oia.addr16[6], ia->addr16[6], u), 2117126258Smlaier oia.addr16[7], ia->addr16[7], u); 2118126258Smlaier break; 2119126258Smlaier#endif /* INET6 */ 2120126258Smlaier } 2121223637Sbz /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 2122223637Sbz if (oa) { 2123223637Sbz PF_ACPY(oa, na, af); 2124223637Sbz switch (af) { 2125126258Smlaier#ifdef INET 2126223637Sbz case AF_INET: 2127223637Sbz *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 2128223637Sbz ooa.addr16[0], oa->addr16[0], 0), 2129223637Sbz ooa.addr16[1], oa->addr16[1], 0); 2130223637Sbz break; 2131126258Smlaier#endif /* INET */ 2132126258Smlaier#ifdef INET6 2133223637Sbz case AF_INET6: 2134223637Sbz *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2135223637Sbz pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2136223637Sbz pf_cksum_fixup(pf_cksum_fixup(*ic, 2137223637Sbz ooa.addr16[0], oa->addr16[0], u), 2138223637Sbz ooa.addr16[1], oa->addr16[1], u), 2139223637Sbz ooa.addr16[2], oa->addr16[2], u), 2140223637Sbz ooa.addr16[3], oa->addr16[3], u), 2141223637Sbz ooa.addr16[4], oa->addr16[4], u), 2142223637Sbz ooa.addr16[5], oa->addr16[5], u), 2143223637Sbz ooa.addr16[6], oa->addr16[6], u), 2144223637Sbz ooa.addr16[7], oa->addr16[7], u); 2145223637Sbz break; 2146126258Smlaier#endif /* INET6 */ 2147223637Sbz } 2148126258Smlaier } 2149126258Smlaier} 2150126258Smlaier 2151171168Smlaier 2152171168Smlaier/* 2153171168Smlaier * Need to modulate the sequence numbers in the TCP SACK option 2154171168Smlaier * (credits to Krzysztof Pfaff for report and patch) 2155171168Smlaier */ 2156240233Sglebiusstatic int 2157171168Smlaierpf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd, 2158171168Smlaier struct tcphdr *th, struct pf_state_peer *dst) 2159171168Smlaier{ 2160171168Smlaier int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen; 2161171168Smlaier u_int8_t opts[TCP_MAXOLEN], *opt = opts; 2162171168Smlaier int copyback = 0, i, olen; 2163171168Smlaier struct sackblk sack; 2164171168Smlaier 2165223637Sbz#define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2) 2166171168Smlaier if (hlen < TCPOLEN_SACKLEN || 2167171168Smlaier !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af)) 2168171168Smlaier return 0; 2169171168Smlaier 2170171168Smlaier while (hlen >= TCPOLEN_SACKLEN) { 2171171168Smlaier olen = opt[1]; 2172171168Smlaier switch (*opt) { 2173171168Smlaier case TCPOPT_EOL: /* FALLTHROUGH */ 2174171168Smlaier case TCPOPT_NOP: 2175171168Smlaier opt++; 2176171168Smlaier hlen--; 2177171168Smlaier break; 2178171168Smlaier case TCPOPT_SACK: 2179171168Smlaier if (olen > hlen) 2180171168Smlaier olen = hlen; 2181171168Smlaier if (olen >= TCPOLEN_SACKLEN) { 2182171168Smlaier for (i = 2; i + TCPOLEN_SACK <= olen; 2183171168Smlaier i += TCPOLEN_SACK) { 2184171168Smlaier memcpy(&sack, &opt[i], sizeof(sack)); 2185171168Smlaier pf_change_a(&sack.start, &th->th_sum, 2186171168Smlaier htonl(ntohl(sack.start) - 2187171168Smlaier dst->seqdiff), 0); 2188171168Smlaier pf_change_a(&sack.end, &th->th_sum, 2189171168Smlaier htonl(ntohl(sack.end) - 2190171168Smlaier dst->seqdiff), 0); 2191171168Smlaier memcpy(&opt[i], &sack, sizeof(sack)); 2192171168Smlaier } 2193171168Smlaier copyback = 1; 2194171168Smlaier } 2195171168Smlaier /* FALLTHROUGH */ 2196171168Smlaier default: 2197171168Smlaier if (olen < 2) 2198171168Smlaier olen = 2; 2199171168Smlaier hlen -= olen; 2200171168Smlaier opt += olen; 2201171168Smlaier } 2202171168Smlaier } 2203171168Smlaier 2204171168Smlaier if (copyback) 2205171168Smlaier m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts); 2206171168Smlaier return (copyback); 2207171168Smlaier} 2208171168Smlaier 2209240233Sglebiusstatic void 2210162238Scsjppf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af, 2211126258Smlaier const struct pf_addr *saddr, const struct pf_addr *daddr, 2212126258Smlaier u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 2213145836Smlaier u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag, 2214240233Sglebius u_int16_t rtag, struct ifnet *ifp) 2215126258Smlaier{ 2216240233Sglebius struct pf_send_entry *pfse; 2217126258Smlaier struct mbuf *m; 2218171168Smlaier int len, tlen; 2219126258Smlaier#ifdef INET 2220240233Sglebius struct ip *h = NULL; 2221126258Smlaier#endif /* INET */ 2222126258Smlaier#ifdef INET6 2223240233Sglebius struct ip6_hdr *h6 = NULL; 2224126258Smlaier#endif /* INET6 */ 2225171168Smlaier struct tcphdr *th; 2226171168Smlaier char *opt; 2227223637Sbz struct pf_mtag *pf_mtag; 2228126258Smlaier 2229171168Smlaier len = 0; 2230171168Smlaier th = NULL; 2231171168Smlaier 2232126258Smlaier /* maximum segment size tcp option */ 2233126258Smlaier tlen = sizeof(struct tcphdr); 2234126258Smlaier if (mss) 2235126258Smlaier tlen += 4; 2236126258Smlaier 2237126258Smlaier switch (af) { 2238126258Smlaier#ifdef INET 2239126258Smlaier case AF_INET: 2240126258Smlaier len = sizeof(struct ip) + tlen; 2241126258Smlaier break; 2242126258Smlaier#endif /* INET */ 2243126258Smlaier#ifdef INET6 2244126258Smlaier case AF_INET6: 2245126258Smlaier len = sizeof(struct ip6_hdr) + tlen; 2246126258Smlaier break; 2247126258Smlaier#endif /* INET6 */ 2248240233Sglebius default: 2249240233Sglebius panic("%s: unsupported af %d", __func__, af); 2250126258Smlaier } 2251126258Smlaier 2252240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2253240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2254240233Sglebius if (pfse == NULL) 2255132280Smlaier return; 2256248324Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 2257240233Sglebius if (m == NULL) { 2258240233Sglebius free(pfse, M_PFTEMP); 2259240233Sglebius return; 2260240233Sglebius } 2261162238Scsjp#ifdef MAC 2262223637Sbz mac_netinet_firewall_send(m); 2263162238Scsjp#endif 2264171168Smlaier if ((pf_mtag = pf_get_mtag(m)) == NULL) { 2265240233Sglebius free(pfse, M_PFTEMP); 2266171168Smlaier m_freem(m); 2267171168Smlaier return; 2268171168Smlaier } 2269171168Smlaier if (tag) 2270145836Smlaier m->m_flags |= M_SKIP_FIREWALL; 2271223637Sbz pf_mtag->tag = rtag; 2272145836Smlaier 2273171168Smlaier if (r != NULL && r->rtableid >= 0) 2274178888Sjulian M_SETFIB(m, r->rtableid); 2275223637Sbz 2276126258Smlaier#ifdef ALTQ 2277126258Smlaier if (r != NULL && r->qid) { 2278171168Smlaier pf_mtag->qid = r->qid; 2279223637Sbz 2280171168Smlaier /* add hints for ecn */ 2281171168Smlaier pf_mtag->hdr = mtod(m, struct ip *); 2282126258Smlaier } 2283145836Smlaier#endif /* ALTQ */ 2284126258Smlaier m->m_data += max_linkhdr; 2285126258Smlaier m->m_pkthdr.len = m->m_len = len; 2286126258Smlaier m->m_pkthdr.rcvif = NULL; 2287126258Smlaier bzero(m->m_data, len); 2288126258Smlaier switch (af) { 2289126258Smlaier#ifdef INET 2290126258Smlaier case AF_INET: 2291126258Smlaier h = mtod(m, struct ip *); 2292126258Smlaier 2293126258Smlaier /* IP header fields included in the TCP checksum */ 2294126258Smlaier h->ip_p = IPPROTO_TCP; 2295126258Smlaier h->ip_len = htons(tlen); 2296126258Smlaier h->ip_src.s_addr = saddr->v4.s_addr; 2297126258Smlaier h->ip_dst.s_addr = daddr->v4.s_addr; 2298126258Smlaier 2299126258Smlaier th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 2300126258Smlaier break; 2301126258Smlaier#endif /* INET */ 2302126258Smlaier#ifdef INET6 2303126258Smlaier case AF_INET6: 2304126258Smlaier h6 = mtod(m, struct ip6_hdr *); 2305126258Smlaier 2306126258Smlaier /* IP header fields included in the TCP checksum */ 2307126258Smlaier h6->ip6_nxt = IPPROTO_TCP; 2308126258Smlaier h6->ip6_plen = htons(tlen); 2309126258Smlaier memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 2310126258Smlaier memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 2311126258Smlaier 2312126258Smlaier th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 2313126258Smlaier break; 2314126258Smlaier#endif /* INET6 */ 2315126258Smlaier } 2316126258Smlaier 2317126258Smlaier /* TCP header */ 2318126258Smlaier th->th_sport = sport; 2319126258Smlaier th->th_dport = dport; 2320126258Smlaier th->th_seq = htonl(seq); 2321126258Smlaier th->th_ack = htonl(ack); 2322126258Smlaier th->th_off = tlen >> 2; 2323126258Smlaier th->th_flags = flags; 2324126258Smlaier th->th_win = htons(win); 2325126258Smlaier 2326126258Smlaier if (mss) { 2327126258Smlaier opt = (char *)(th + 1); 2328126258Smlaier opt[0] = TCPOPT_MAXSEG; 2329126258Smlaier opt[1] = 4; 2330126258Smlaier HTONS(mss); 2331126258Smlaier bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2); 2332126258Smlaier } 2333126258Smlaier 2334126258Smlaier switch (af) { 2335126258Smlaier#ifdef INET 2336126258Smlaier case AF_INET: 2337126258Smlaier /* TCP checksum */ 2338126258Smlaier th->th_sum = in_cksum(m, len); 2339126258Smlaier 2340126258Smlaier /* Finish the IP header */ 2341126258Smlaier h->ip_v = 4; 2342126258Smlaier h->ip_hl = sizeof(*h) >> 2; 2343126258Smlaier h->ip_tos = IPTOS_LOWDELAY; 2344241913Sglebius h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 2345241913Sglebius h->ip_len = htons(len); 2346223637Sbz h->ip_ttl = ttl ? ttl : V_ip_defttl; 2347126258Smlaier h->ip_sum = 0; 2348145836Smlaier 2349240233Sglebius pfse->pfse_type = PFSE_IP; 2350126258Smlaier break; 2351126258Smlaier#endif /* INET */ 2352126258Smlaier#ifdef INET6 2353126258Smlaier case AF_INET6: 2354126258Smlaier /* TCP checksum */ 2355126258Smlaier th->th_sum = in6_cksum(m, IPPROTO_TCP, 2356126258Smlaier sizeof(struct ip6_hdr), tlen); 2357126258Smlaier 2358126258Smlaier h6->ip6_vfc |= IPV6_VERSION; 2359126258Smlaier h6->ip6_hlim = IPV6_DEFHLIM; 2360126258Smlaier 2361240233Sglebius pfse->pfse_type = PFSE_IP6; 2362126258Smlaier break; 2363126258Smlaier#endif /* INET6 */ 2364126258Smlaier } 2365240233Sglebius pfse->pfse_m = m; 2366240233Sglebius pf_send(pfse); 2367126258Smlaier} 2368126258Smlaier 2369223637Sbzstatic void 2370126258Smlaierpf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af, 2371126258Smlaier struct pf_rule *r) 2372126258Smlaier{ 2373240233Sglebius struct pf_send_entry *pfse; 2374240233Sglebius struct mbuf *m0; 2375223637Sbz struct pf_mtag *pf_mtag; 2376126258Smlaier 2377240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2378240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2379240233Sglebius if (pfse == NULL) 2380132280Smlaier return; 2381240233Sglebius 2382240233Sglebius if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 2383240233Sglebius free(pfse, M_PFTEMP); 2384223637Sbz return; 2385240233Sglebius } 2386223637Sbz 2387240233Sglebius if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 2388240233Sglebius free(pfse, M_PFTEMP); 2389126258Smlaier return; 2390240233Sglebius } 2391171168Smlaier /* XXX: revisit */ 2392171168Smlaier m0->m_flags |= M_SKIP_FIREWALL; 2393126258Smlaier 2394171168Smlaier if (r->rtableid >= 0) 2395178888Sjulian M_SETFIB(m0, r->rtableid); 2396171168Smlaier 2397126258Smlaier#ifdef ALTQ 2398126258Smlaier if (r->qid) { 2399171168Smlaier pf_mtag->qid = r->qid; 2400171168Smlaier /* add hints for ecn */ 2401171168Smlaier pf_mtag->hdr = mtod(m0, struct ip *); 2402126258Smlaier } 2403145836Smlaier#endif /* ALTQ */ 2404126258Smlaier 2405126258Smlaier switch (af) { 2406126258Smlaier#ifdef INET 2407126258Smlaier case AF_INET: 2408240233Sglebius pfse->pfse_type = PFSE_ICMP; 2409126258Smlaier break; 2410126258Smlaier#endif /* INET */ 2411126258Smlaier#ifdef INET6 2412126258Smlaier case AF_INET6: 2413240233Sglebius pfse->pfse_type = PFSE_ICMP6; 2414126258Smlaier break; 2415126258Smlaier#endif /* INET6 */ 2416126258Smlaier } 2417240233Sglebius pfse->pfse_m = m0; 2418240233Sglebius pfse->pfse_icmp_type = type; 2419240233Sglebius pfse->pfse_icmp_code = code; 2420240233Sglebius pf_send(pfse); 2421126258Smlaier} 2422126258Smlaier 2423126258Smlaier/* 2424126258Smlaier * Return 1 if the addresses a and b match (with mask m), otherwise return 0. 2425126258Smlaier * If n is 0, they match if they are equal. If n is != 0, they match if they 2426126258Smlaier * are different. 2427126258Smlaier */ 2428126258Smlaierint 2429126258Smlaierpf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, 2430126258Smlaier struct pf_addr *b, sa_family_t af) 2431126258Smlaier{ 2432126258Smlaier int match = 0; 2433126258Smlaier 2434126258Smlaier switch (af) { 2435126258Smlaier#ifdef INET 2436126258Smlaier case AF_INET: 2437126258Smlaier if ((a->addr32[0] & m->addr32[0]) == 2438126258Smlaier (b->addr32[0] & m->addr32[0])) 2439126258Smlaier match++; 2440126258Smlaier break; 2441126258Smlaier#endif /* INET */ 2442126258Smlaier#ifdef INET6 2443126258Smlaier case AF_INET6: 2444126258Smlaier if (((a->addr32[0] & m->addr32[0]) == 2445126258Smlaier (b->addr32[0] & m->addr32[0])) && 2446126258Smlaier ((a->addr32[1] & m->addr32[1]) == 2447126258Smlaier (b->addr32[1] & m->addr32[1])) && 2448126258Smlaier ((a->addr32[2] & m->addr32[2]) == 2449126258Smlaier (b->addr32[2] & m->addr32[2])) && 2450126258Smlaier ((a->addr32[3] & m->addr32[3]) == 2451126258Smlaier (b->addr32[3] & m->addr32[3]))) 2452126258Smlaier match++; 2453126258Smlaier break; 2454126258Smlaier#endif /* INET6 */ 2455126258Smlaier } 2456126258Smlaier if (match) { 2457126258Smlaier if (n) 2458126258Smlaier return (0); 2459126258Smlaier else 2460126258Smlaier return (1); 2461126258Smlaier } else { 2462126258Smlaier if (n) 2463126258Smlaier return (1); 2464126258Smlaier else 2465126258Smlaier return (0); 2466126258Smlaier } 2467126258Smlaier} 2468126258Smlaier 2469223637Sbz/* 2470223637Sbz * Return 1 if b <= a <= e, otherwise return 0. 2471223637Sbz */ 2472126258Smlaierint 2473223637Sbzpf_match_addr_range(struct pf_addr *b, struct pf_addr *e, 2474223637Sbz struct pf_addr *a, sa_family_t af) 2475223637Sbz{ 2476223637Sbz switch (af) { 2477223637Sbz#ifdef INET 2478223637Sbz case AF_INET: 2479223637Sbz if ((a->addr32[0] < b->addr32[0]) || 2480223637Sbz (a->addr32[0] > e->addr32[0])) 2481223637Sbz return (0); 2482223637Sbz break; 2483223637Sbz#endif /* INET */ 2484223637Sbz#ifdef INET6 2485223637Sbz case AF_INET6: { 2486223637Sbz int i; 2487223637Sbz 2488223637Sbz /* check a >= b */ 2489223637Sbz for (i = 0; i < 4; ++i) 2490223637Sbz if (a->addr32[i] > b->addr32[i]) 2491223637Sbz break; 2492223637Sbz else if (a->addr32[i] < b->addr32[i]) 2493223637Sbz return (0); 2494223637Sbz /* check a <= e */ 2495223637Sbz for (i = 0; i < 4; ++i) 2496223637Sbz if (a->addr32[i] < e->addr32[i]) 2497223637Sbz break; 2498223637Sbz else if (a->addr32[i] > e->addr32[i]) 2499223637Sbz return (0); 2500223637Sbz break; 2501223637Sbz } 2502223637Sbz#endif /* INET6 */ 2503223637Sbz } 2504223637Sbz return (1); 2505223637Sbz} 2506223637Sbz 2507240233Sglebiusstatic int 2508126258Smlaierpf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 2509126258Smlaier{ 2510126258Smlaier switch (op) { 2511126258Smlaier case PF_OP_IRG: 2512126258Smlaier return ((p > a1) && (p < a2)); 2513126258Smlaier case PF_OP_XRG: 2514126258Smlaier return ((p < a1) || (p > a2)); 2515126258Smlaier case PF_OP_RRG: 2516126258Smlaier return ((p >= a1) && (p <= a2)); 2517126258Smlaier case PF_OP_EQ: 2518126258Smlaier return (p == a1); 2519126258Smlaier case PF_OP_NE: 2520126258Smlaier return (p != a1); 2521126258Smlaier case PF_OP_LT: 2522126258Smlaier return (p < a1); 2523126258Smlaier case PF_OP_LE: 2524126258Smlaier return (p <= a1); 2525126258Smlaier case PF_OP_GT: 2526126258Smlaier return (p > a1); 2527126258Smlaier case PF_OP_GE: 2528126258Smlaier return (p >= a1); 2529126258Smlaier } 2530126258Smlaier return (0); /* never reached */ 2531126258Smlaier} 2532126258Smlaier 2533126258Smlaierint 2534126258Smlaierpf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 2535126258Smlaier{ 2536126258Smlaier NTOHS(a1); 2537126258Smlaier NTOHS(a2); 2538126258Smlaier NTOHS(p); 2539126258Smlaier return (pf_match(op, a1, a2, p)); 2540126258Smlaier} 2541126258Smlaier 2542240233Sglebiusstatic int 2543126258Smlaierpf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 2544126258Smlaier{ 2545126258Smlaier if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2546126258Smlaier return (0); 2547126258Smlaier return (pf_match(op, a1, a2, u)); 2548126258Smlaier} 2549126258Smlaier 2550240233Sglebiusstatic int 2551126258Smlaierpf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 2552126258Smlaier{ 2553126258Smlaier if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2554126258Smlaier return (0); 2555126258Smlaier return (pf_match(op, a1, a2, g)); 2556126258Smlaier} 2557126258Smlaier 2558223637Sbzint 2559240233Sglebiuspf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag, int mtag) 2560171168Smlaier{ 2561171168Smlaier if (*tag == -1) 2562240233Sglebius *tag = mtag; 2563171168Smlaier 2564126258Smlaier return ((!r->match_tag_not && r->match_tag == *tag) || 2565126258Smlaier (r->match_tag_not && r->match_tag != *tag)); 2566126258Smlaier} 2567126258Smlaier 2568126258Smlaierint 2569240233Sglebiuspf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag) 2570126258Smlaier{ 2571126258Smlaier 2572240233Sglebius KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 2573126258Smlaier 2574240233Sglebius if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL)) 2575240233Sglebius return (ENOMEM); 2576240233Sglebius 2577240233Sglebius pd->pf_mtag->tag = tag; 2578240233Sglebius 2579126258Smlaier return (0); 2580126258Smlaier} 2581126258Smlaier 2582240641Sglebius#define PF_ANCHOR_STACKSIZE 32 2583240641Sglebiusstruct pf_anchor_stackframe { 2584240641Sglebius struct pf_ruleset *rs; 2585240641Sglebius struct pf_rule *r; /* XXX: + match bit */ 2586240641Sglebius struct pf_anchor *child; 2587240641Sglebius}; 2588240641Sglebius 2589240641Sglebius/* 2590240641Sglebius * XXX: We rely on malloc(9) returning pointer aligned addresses. 2591240641Sglebius */ 2592240641Sglebius#define PF_ANCHORSTACK_MATCH 0x00000001 2593240641Sglebius#define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 2594240641Sglebius 2595240641Sglebius#define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 2596240641Sglebius#define PF_ANCHOR_RULE(f) (struct pf_rule *) \ 2597240641Sglebius ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 2598240641Sglebius#define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 2599240641Sglebius ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 2600240641Sglebius} while (0) 2601240641Sglebius 2602223637Sbzvoid 2603240641Sglebiuspf_step_into_anchor(struct pf_anchor_stackframe *stack, int *depth, 2604240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2605240641Sglebius int *match) 2606145836Smlaier{ 2607145836Smlaier struct pf_anchor_stackframe *f; 2608126258Smlaier 2609240233Sglebius PF_RULES_RASSERT(); 2610240233Sglebius 2611171168Smlaier if (match) 2612171168Smlaier *match = 0; 2613240641Sglebius if (*depth >= PF_ANCHOR_STACKSIZE) { 2614240641Sglebius printf("%s: anchor stack overflow on %s\n", 2615240641Sglebius __func__, (*r)->anchor->name); 2616145836Smlaier *r = TAILQ_NEXT(*r, entries); 2617145836Smlaier return; 2618145836Smlaier } else if (*depth == 0 && a != NULL) 2619145836Smlaier *a = *r; 2620240641Sglebius f = stack + (*depth)++; 2621145836Smlaier f->rs = *rs; 2622145836Smlaier f->r = *r; 2623145836Smlaier if ((*r)->anchor_wildcard) { 2624240641Sglebius struct pf_anchor_node *parent = &(*r)->anchor->children; 2625240641Sglebius 2626240641Sglebius if ((f->child = RB_MIN(pf_anchor_node, parent)) == NULL) { 2627145836Smlaier *r = NULL; 2628145836Smlaier return; 2629145836Smlaier } 2630145836Smlaier *rs = &f->child->ruleset; 2631145836Smlaier } else { 2632145836Smlaier f->child = NULL; 2633145836Smlaier *rs = &(*r)->anchor->ruleset; 2634145836Smlaier } 2635145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2636145836Smlaier} 2637126258Smlaier 2638171168Smlaierint 2639240641Sglebiuspf_step_out_of_anchor(struct pf_anchor_stackframe *stack, int *depth, 2640240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2641240641Sglebius int *match) 2642145836Smlaier{ 2643145836Smlaier struct pf_anchor_stackframe *f; 2644240641Sglebius struct pf_rule *fr; 2645171168Smlaier int quick = 0; 2646145836Smlaier 2647240233Sglebius PF_RULES_RASSERT(); 2648240233Sglebius 2649145836Smlaier do { 2650145836Smlaier if (*depth <= 0) 2651145836Smlaier break; 2652240641Sglebius f = stack + *depth - 1; 2653240641Sglebius fr = PF_ANCHOR_RULE(f); 2654240641Sglebius if (f->child != NULL) { 2655240641Sglebius struct pf_anchor_node *parent; 2656240641Sglebius 2657240641Sglebius /* 2658240641Sglebius * This block traverses through 2659240641Sglebius * a wildcard anchor. 2660240641Sglebius */ 2661240641Sglebius parent = &fr->anchor->children; 2662240641Sglebius if (match != NULL && *match) { 2663240641Sglebius /* 2664240641Sglebius * If any of "*" matched, then 2665240641Sglebius * "foo/ *" matched, mark frame 2666240641Sglebius * appropriately. 2667240641Sglebius */ 2668240641Sglebius PF_ANCHOR_SET_MATCH(f); 2669171168Smlaier *match = 0; 2670171168Smlaier } 2671240641Sglebius f->child = RB_NEXT(pf_anchor_node, parent, f->child); 2672145836Smlaier if (f->child != NULL) { 2673145836Smlaier *rs = &f->child->ruleset; 2674145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2675145836Smlaier if (*r == NULL) 2676145836Smlaier continue; 2677145836Smlaier else 2678145836Smlaier break; 2679145836Smlaier } 2680145836Smlaier } 2681145836Smlaier (*depth)--; 2682145836Smlaier if (*depth == 0 && a != NULL) 2683145836Smlaier *a = NULL; 2684145836Smlaier *rs = f->rs; 2685240641Sglebius if (PF_ANCHOR_MATCH(f) || (match != NULL && *match)) 2686240641Sglebius quick = fr->quick; 2687240641Sglebius *r = TAILQ_NEXT(fr, entries); 2688145836Smlaier } while (*r == NULL); 2689171168Smlaier 2690171168Smlaier return (quick); 2691145836Smlaier} 2692145836Smlaier 2693126258Smlaier#ifdef INET6 2694126258Smlaiervoid 2695126258Smlaierpf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 2696126258Smlaier struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 2697126258Smlaier{ 2698126258Smlaier switch (af) { 2699126258Smlaier#ifdef INET 2700126258Smlaier case AF_INET: 2701126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2702126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2703126258Smlaier break; 2704126258Smlaier#endif /* INET */ 2705126258Smlaier case AF_INET6: 2706126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2707126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2708126258Smlaier naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 2709126258Smlaier ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 2710126258Smlaier naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 2711126258Smlaier ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 2712126258Smlaier naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 2713126258Smlaier ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 2714126258Smlaier break; 2715126258Smlaier } 2716126258Smlaier} 2717126258Smlaier 2718126258Smlaiervoid 2719130613Smlaierpf_addr_inc(struct pf_addr *addr, sa_family_t af) 2720126258Smlaier{ 2721126258Smlaier switch (af) { 2722126258Smlaier#ifdef INET 2723126258Smlaier case AF_INET: 2724126258Smlaier addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 2725126258Smlaier break; 2726126258Smlaier#endif /* INET */ 2727126258Smlaier case AF_INET6: 2728126258Smlaier if (addr->addr32[3] == 0xffffffff) { 2729126258Smlaier addr->addr32[3] = 0; 2730126258Smlaier if (addr->addr32[2] == 0xffffffff) { 2731126258Smlaier addr->addr32[2] = 0; 2732126258Smlaier if (addr->addr32[1] == 0xffffffff) { 2733126258Smlaier addr->addr32[1] = 0; 2734126258Smlaier addr->addr32[0] = 2735126258Smlaier htonl(ntohl(addr->addr32[0]) + 1); 2736126258Smlaier } else 2737126258Smlaier addr->addr32[1] = 2738126258Smlaier htonl(ntohl(addr->addr32[1]) + 1); 2739126258Smlaier } else 2740126258Smlaier addr->addr32[2] = 2741126258Smlaier htonl(ntohl(addr->addr32[2]) + 1); 2742126258Smlaier } else 2743126258Smlaier addr->addr32[3] = 2744126258Smlaier htonl(ntohl(addr->addr32[3]) + 1); 2745126258Smlaier break; 2746126258Smlaier } 2747126258Smlaier} 2748126258Smlaier#endif /* INET6 */ 2749126258Smlaier 2750126258Smlaierint 2751240233Sglebiuspf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m) 2752126258Smlaier{ 2753126258Smlaier struct pf_addr *saddr, *daddr; 2754126258Smlaier u_int16_t sport, dport; 2755126261Smlaier struct inpcbinfo *pi; 2756126258Smlaier struct inpcb *inp; 2757126258Smlaier 2758171168Smlaier pd->lookup.uid = UID_MAX; 2759171168Smlaier pd->lookup.gid = GID_MAX; 2760223637Sbz 2761130613Smlaier switch (pd->proto) { 2762126258Smlaier case IPPROTO_TCP: 2763171168Smlaier if (pd->hdr.tcp == NULL) 2764171168Smlaier return (-1); 2765126258Smlaier sport = pd->hdr.tcp->th_sport; 2766126258Smlaier dport = pd->hdr.tcp->th_dport; 2767181803Sbz pi = &V_tcbinfo; 2768126258Smlaier break; 2769126258Smlaier case IPPROTO_UDP: 2770171168Smlaier if (pd->hdr.udp == NULL) 2771171168Smlaier return (-1); 2772126258Smlaier sport = pd->hdr.udp->uh_sport; 2773126258Smlaier dport = pd->hdr.udp->uh_dport; 2774181803Sbz pi = &V_udbinfo; 2775126258Smlaier break; 2776126258Smlaier default: 2777171168Smlaier return (-1); 2778126258Smlaier } 2779126258Smlaier if (direction == PF_IN) { 2780126258Smlaier saddr = pd->src; 2781126258Smlaier daddr = pd->dst; 2782126258Smlaier } else { 2783126258Smlaier u_int16_t p; 2784126258Smlaier 2785126258Smlaier p = sport; 2786126258Smlaier sport = dport; 2787126258Smlaier dport = p; 2788126258Smlaier saddr = pd->dst; 2789126258Smlaier daddr = pd->src; 2790126258Smlaier } 2791130613Smlaier switch (pd->af) { 2792145836Smlaier#ifdef INET 2793126258Smlaier case AF_INET: 2794240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 2795240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2796126261Smlaier if (inp == NULL) { 2797240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 2798222488Srwatson daddr->v4, dport, INPLOOKUP_WILDCARD | 2799240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2800222488Srwatson if (inp == NULL) 2801171168Smlaier return (-1); 2802126261Smlaier } 2803126258Smlaier break; 2804145836Smlaier#endif /* INET */ 2805126258Smlaier#ifdef INET6 2806126258Smlaier case AF_INET6: 2807240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 2808240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2809126261Smlaier if (inp == NULL) { 2810240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 2811222488Srwatson &daddr->v6, dport, INPLOOKUP_WILDCARD | 2812240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2813222488Srwatson if (inp == NULL) 2814171168Smlaier return (-1); 2815126261Smlaier } 2816126258Smlaier break; 2817126258Smlaier#endif /* INET6 */ 2818126258Smlaier 2819126258Smlaier default: 2820171168Smlaier return (-1); 2821126258Smlaier } 2822222488Srwatson INP_RLOCK_ASSERT(inp); 2823183606Sbz pd->lookup.uid = inp->inp_cred->cr_uid; 2824183606Sbz pd->lookup.gid = inp->inp_cred->cr_groups[0]; 2825222488Srwatson INP_RUNLOCK(inp); 2826240233Sglebius 2827126258Smlaier return (1); 2828126258Smlaier} 2829126258Smlaier 2830240233Sglebiusstatic u_int8_t 2831126258Smlaierpf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2832126258Smlaier{ 2833126258Smlaier int hlen; 2834126258Smlaier u_int8_t hdr[60]; 2835126258Smlaier u_int8_t *opt, optlen; 2836126258Smlaier u_int8_t wscale = 0; 2837126258Smlaier 2838126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2839126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2840126258Smlaier return (0); 2841126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2842126258Smlaier return (0); 2843126258Smlaier opt = hdr + sizeof(struct tcphdr); 2844126258Smlaier hlen -= sizeof(struct tcphdr); 2845126258Smlaier while (hlen >= 3) { 2846126258Smlaier switch (*opt) { 2847126258Smlaier case TCPOPT_EOL: 2848126258Smlaier case TCPOPT_NOP: 2849126258Smlaier ++opt; 2850126258Smlaier --hlen; 2851126258Smlaier break; 2852126258Smlaier case TCPOPT_WINDOW: 2853126258Smlaier wscale = opt[2]; 2854126258Smlaier if (wscale > TCP_MAX_WINSHIFT) 2855126258Smlaier wscale = TCP_MAX_WINSHIFT; 2856126258Smlaier wscale |= PF_WSCALE_FLAG; 2857130613Smlaier /* FALLTHROUGH */ 2858126258Smlaier default: 2859126258Smlaier optlen = opt[1]; 2860126258Smlaier if (optlen < 2) 2861126258Smlaier optlen = 2; 2862126258Smlaier hlen -= optlen; 2863126258Smlaier opt += optlen; 2864130613Smlaier break; 2865126258Smlaier } 2866126258Smlaier } 2867126258Smlaier return (wscale); 2868126258Smlaier} 2869126258Smlaier 2870240233Sglebiusstatic u_int16_t 2871126258Smlaierpf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2872126258Smlaier{ 2873126258Smlaier int hlen; 2874126258Smlaier u_int8_t hdr[60]; 2875126258Smlaier u_int8_t *opt, optlen; 2876181803Sbz u_int16_t mss = V_tcp_mssdflt; 2877126258Smlaier 2878126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2879126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2880126258Smlaier return (0); 2881126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2882126258Smlaier return (0); 2883126258Smlaier opt = hdr + sizeof(struct tcphdr); 2884126258Smlaier hlen -= sizeof(struct tcphdr); 2885126258Smlaier while (hlen >= TCPOLEN_MAXSEG) { 2886126258Smlaier switch (*opt) { 2887126258Smlaier case TCPOPT_EOL: 2888126258Smlaier case TCPOPT_NOP: 2889126258Smlaier ++opt; 2890126258Smlaier --hlen; 2891126258Smlaier break; 2892126258Smlaier case TCPOPT_MAXSEG: 2893126258Smlaier bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2); 2894145030Sglebius NTOHS(mss); 2895130613Smlaier /* FALLTHROUGH */ 2896126258Smlaier default: 2897126258Smlaier optlen = opt[1]; 2898126258Smlaier if (optlen < 2) 2899126258Smlaier optlen = 2; 2900126258Smlaier hlen -= optlen; 2901126258Smlaier opt += optlen; 2902130613Smlaier break; 2903126258Smlaier } 2904126258Smlaier } 2905126258Smlaier return (mss); 2906126258Smlaier} 2907126258Smlaier 2908240233Sglebiusstatic u_int16_t 2909231852Sbzpf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 2910126258Smlaier{ 2911126258Smlaier#ifdef INET 2912126258Smlaier struct sockaddr_in *dst; 2913126258Smlaier struct route ro; 2914126258Smlaier#endif /* INET */ 2915126258Smlaier#ifdef INET6 2916126258Smlaier struct sockaddr_in6 *dst6; 2917126258Smlaier struct route_in6 ro6; 2918126258Smlaier#endif /* INET6 */ 2919126258Smlaier struct rtentry *rt = NULL; 2920223637Sbz int hlen = 0; 2921181803Sbz u_int16_t mss = V_tcp_mssdflt; 2922126258Smlaier 2923126258Smlaier switch (af) { 2924126258Smlaier#ifdef INET 2925126258Smlaier case AF_INET: 2926126258Smlaier hlen = sizeof(struct ip); 2927126258Smlaier bzero(&ro, sizeof(ro)); 2928126258Smlaier dst = (struct sockaddr_in *)&ro.ro_dst; 2929126258Smlaier dst->sin_family = AF_INET; 2930126258Smlaier dst->sin_len = sizeof(*dst); 2931126258Smlaier dst->sin_addr = addr->v4; 2932231852Sbz in_rtalloc_ign(&ro, 0, rtableid); 2933126258Smlaier rt = ro.ro_rt; 2934126258Smlaier break; 2935126258Smlaier#endif /* INET */ 2936126258Smlaier#ifdef INET6 2937126258Smlaier case AF_INET6: 2938126258Smlaier hlen = sizeof(struct ip6_hdr); 2939126258Smlaier bzero(&ro6, sizeof(ro6)); 2940126258Smlaier dst6 = (struct sockaddr_in6 *)&ro6.ro_dst; 2941126258Smlaier dst6->sin6_family = AF_INET6; 2942126258Smlaier dst6->sin6_len = sizeof(*dst6); 2943126258Smlaier dst6->sin6_addr = addr->v6; 2944231852Sbz in6_rtalloc_ign(&ro6, 0, rtableid); 2945126258Smlaier rt = ro6.ro_rt; 2946126258Smlaier break; 2947126258Smlaier#endif /* INET6 */ 2948126258Smlaier } 2949126258Smlaier 2950126258Smlaier if (rt && rt->rt_ifp) { 2951126258Smlaier mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr); 2952181803Sbz mss = max(V_tcp_mssdflt, mss); 2953126258Smlaier RTFREE(rt); 2954126258Smlaier } 2955126258Smlaier mss = min(mss, offer); 2956126258Smlaier mss = max(mss, 64); /* sanity - at least max opt space */ 2957126258Smlaier return (mss); 2958126258Smlaier} 2959126258Smlaier 2960240233Sglebiusstatic void 2961126258Smlaierpf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr) 2962126258Smlaier{ 2963126258Smlaier struct pf_rule *r = s->rule.ptr; 2964223637Sbz struct pf_src_node *sn = NULL; 2965126258Smlaier 2966130613Smlaier s->rt_kif = NULL; 2967126258Smlaier if (!r->rt || r->rt == PF_FASTROUTE) 2968126258Smlaier return; 2969223637Sbz switch (s->key[PF_SK_WIRE]->af) { 2970126258Smlaier#ifdef INET 2971126258Smlaier case AF_INET: 2972223637Sbz pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL, &sn); 2973130613Smlaier s->rt_kif = r->rpool.cur->kif; 2974126258Smlaier break; 2975126258Smlaier#endif /* INET */ 2976126258Smlaier#ifdef INET6 2977126258Smlaier case AF_INET6: 2978223637Sbz pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL, &sn); 2979130613Smlaier s->rt_kif = r->rpool.cur->kif; 2980126258Smlaier break; 2981126258Smlaier#endif /* INET6 */ 2982126258Smlaier } 2983126258Smlaier} 2984126258Smlaier 2985240233Sglebiusstatic u_int32_t 2986223637Sbzpf_tcp_iss(struct pf_pdesc *pd) 2987223637Sbz{ 2988223637Sbz MD5_CTX ctx; 2989223637Sbz u_int32_t digest[4]; 2990223637Sbz 2991223637Sbz if (V_pf_tcp_secret_init == 0) { 2992223637Sbz read_random(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 2993223637Sbz MD5Init(&V_pf_tcp_secret_ctx); 2994223637Sbz MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 2995223637Sbz sizeof(V_pf_tcp_secret)); 2996223637Sbz V_pf_tcp_secret_init = 1; 2997223637Sbz } 2998223637Sbz 2999223637Sbz ctx = V_pf_tcp_secret_ctx; 3000223637Sbz 3001223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short)); 3002223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short)); 3003223637Sbz if (pd->af == AF_INET6) { 3004223637Sbz MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr)); 3005223637Sbz MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr)); 3006223637Sbz } else { 3007223637Sbz MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr)); 3008223637Sbz MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr)); 3009223637Sbz } 3010223637Sbz MD5Final((u_char *)digest, &ctx); 3011223637Sbz V_pf_tcp_iss_off += 4096; 3012223637Sbz#define ISN_RANDOM_INCREMENT (4096 - 1) 3013223637Sbz return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 3014223637Sbz V_pf_tcp_iss_off); 3015223637Sbz#undef ISN_RANDOM_INCREMENT 3016223637Sbz} 3017223637Sbz 3018240233Sglebiusstatic int 3019223637Sbzpf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction, 3020240233Sglebius struct pfi_kif *kif, struct mbuf *m, int off, struct pf_pdesc *pd, 3021240233Sglebius struct pf_rule **am, struct pf_ruleset **rsm, struct inpcb *inp) 3022126258Smlaier{ 3023130613Smlaier struct pf_rule *nr = NULL; 3024240233Sglebius struct pf_addr * const saddr = pd->src; 3025240233Sglebius struct pf_addr * const daddr = pd->dst; 3026126258Smlaier sa_family_t af = pd->af; 3027126258Smlaier struct pf_rule *r, *a = NULL; 3028126258Smlaier struct pf_ruleset *ruleset = NULL; 3029130613Smlaier struct pf_src_node *nsn = NULL; 3030223637Sbz struct tcphdr *th = pd->hdr.tcp; 3031223637Sbz struct pf_state_key *sk = NULL, *nk = NULL; 3032126258Smlaier u_short reason; 3033223637Sbz int rewrite = 0, hdrlen = 0; 3034171168Smlaier int tag = -1, rtableid = -1; 3035145836Smlaier int asd = 0; 3036171168Smlaier int match = 0; 3037223637Sbz int state_icmp = 0; 3038223637Sbz u_int16_t sport = 0, dport = 0; 3039223637Sbz u_int16_t bproto_sum = 0, bip_sum = 0; 3040223637Sbz u_int8_t icmptype = 0, icmpcode = 0; 3041240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3042126258Smlaier 3043240233Sglebius PF_RULES_RASSERT(); 3044223637Sbz 3045240233Sglebius if (inp != NULL) { 3046240233Sglebius INP_LOCK_ASSERT(inp); 3047240233Sglebius pd->lookup.uid = inp->inp_cred->cr_uid; 3048240233Sglebius pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3049240233Sglebius pd->lookup.done = 1; 3050145836Smlaier } 3051145836Smlaier 3052223637Sbz switch (pd->proto) { 3053223637Sbz case IPPROTO_TCP: 3054223637Sbz sport = th->th_sport; 3055223637Sbz dport = th->th_dport; 3056223637Sbz hdrlen = sizeof(*th); 3057223637Sbz break; 3058223637Sbz case IPPROTO_UDP: 3059223637Sbz sport = pd->hdr.udp->uh_sport; 3060223637Sbz dport = pd->hdr.udp->uh_dport; 3061223637Sbz hdrlen = sizeof(*pd->hdr.udp); 3062223637Sbz break; 3063223637Sbz#ifdef INET 3064223637Sbz case IPPROTO_ICMP: 3065223637Sbz if (pd->af != AF_INET) 3066223637Sbz break; 3067223637Sbz sport = dport = pd->hdr.icmp->icmp_id; 3068223637Sbz hdrlen = sizeof(*pd->hdr.icmp); 3069223637Sbz icmptype = pd->hdr.icmp->icmp_type; 3070223637Sbz icmpcode = pd->hdr.icmp->icmp_code; 3071223637Sbz 3072223637Sbz if (icmptype == ICMP_UNREACH || 3073223637Sbz icmptype == ICMP_SOURCEQUENCH || 3074223637Sbz icmptype == ICMP_REDIRECT || 3075223637Sbz icmptype == ICMP_TIMXCEED || 3076223637Sbz icmptype == ICMP_PARAMPROB) 3077223637Sbz state_icmp++; 3078223637Sbz break; 3079223637Sbz#endif /* INET */ 3080223637Sbz#ifdef INET6 3081223637Sbz case IPPROTO_ICMPV6: 3082223637Sbz if (af != AF_INET6) 3083223637Sbz break; 3084223637Sbz sport = dport = pd->hdr.icmp6->icmp6_id; 3085223637Sbz hdrlen = sizeof(*pd->hdr.icmp6); 3086223637Sbz icmptype = pd->hdr.icmp6->icmp6_type; 3087223637Sbz icmpcode = pd->hdr.icmp6->icmp6_code; 3088223637Sbz 3089223637Sbz if (icmptype == ICMP6_DST_UNREACH || 3090223637Sbz icmptype == ICMP6_PACKET_TOO_BIG || 3091223637Sbz icmptype == ICMP6_TIME_EXCEEDED || 3092223637Sbz icmptype == ICMP6_PARAM_PROB) 3093223637Sbz state_icmp++; 3094223637Sbz break; 3095223637Sbz#endif /* INET6 */ 3096223637Sbz default: 3097223637Sbz sport = dport = hdrlen = 0; 3098223637Sbz break; 3099223637Sbz } 3100223637Sbz 3101126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3102126258Smlaier 3103223637Sbz /* check packet for BINAT/NAT/RDR */ 3104240233Sglebius if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk, 3105240641Sglebius &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) { 3106240233Sglebius KASSERT(sk != NULL, ("%s: null sk", __func__)); 3107240233Sglebius KASSERT(nk != NULL, ("%s: null nk", __func__)); 3108223637Sbz 3109223637Sbz if (pd->ip_sum) 3110223637Sbz bip_sum = *pd->ip_sum; 3111223637Sbz 3112223637Sbz switch (pd->proto) { 3113223637Sbz case IPPROTO_TCP: 3114223637Sbz bproto_sum = th->th_sum; 3115223637Sbz pd->proto_sum = &th->th_sum; 3116223637Sbz 3117223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3118223637Sbz nk->port[pd->sidx] != sport) { 3119223637Sbz pf_change_ap(saddr, &th->th_sport, pd->ip_sum, 3120223637Sbz &th->th_sum, &nk->addr[pd->sidx], 3121223637Sbz nk->port[pd->sidx], 0, af); 3122223637Sbz pd->sport = &th->th_sport; 3123223637Sbz sport = th->th_sport; 3124223637Sbz } 3125223637Sbz 3126223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3127223637Sbz nk->port[pd->didx] != dport) { 3128223637Sbz pf_change_ap(daddr, &th->th_dport, pd->ip_sum, 3129223637Sbz &th->th_sum, &nk->addr[pd->didx], 3130223637Sbz nk->port[pd->didx], 0, af); 3131223637Sbz dport = th->th_dport; 3132223637Sbz pd->dport = &th->th_dport; 3133223637Sbz } 3134126258Smlaier rewrite++; 3135223637Sbz break; 3136223637Sbz case IPPROTO_UDP: 3137223637Sbz bproto_sum = pd->hdr.udp->uh_sum; 3138223637Sbz pd->proto_sum = &pd->hdr.udp->uh_sum; 3139223637Sbz 3140223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3141223637Sbz nk->port[pd->sidx] != sport) { 3142223637Sbz pf_change_ap(saddr, &pd->hdr.udp->uh_sport, 3143223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3144223637Sbz &nk->addr[pd->sidx], 3145223637Sbz nk->port[pd->sidx], 1, af); 3146223637Sbz sport = pd->hdr.udp->uh_sport; 3147223637Sbz pd->sport = &pd->hdr.udp->uh_sport; 3148223637Sbz } 3149223637Sbz 3150223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3151223637Sbz nk->port[pd->didx] != dport) { 3152223637Sbz pf_change_ap(daddr, &pd->hdr.udp->uh_dport, 3153223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3154223637Sbz &nk->addr[pd->didx], 3155223637Sbz nk->port[pd->didx], 1, af); 3156223637Sbz dport = pd->hdr.udp->uh_dport; 3157223637Sbz pd->dport = &pd->hdr.udp->uh_dport; 3158223637Sbz } 3159223637Sbz rewrite++; 3160223637Sbz break; 3161223637Sbz#ifdef INET 3162223637Sbz case IPPROTO_ICMP: 3163223637Sbz nk->port[0] = nk->port[1]; 3164223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET)) 3165223637Sbz pf_change_a(&saddr->v4.s_addr, pd->ip_sum, 3166223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3167223637Sbz 3168223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET)) 3169223637Sbz pf_change_a(&daddr->v4.s_addr, pd->ip_sum, 3170223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3171223637Sbz 3172223637Sbz if (nk->port[1] != pd->hdr.icmp->icmp_id) { 3173223637Sbz pd->hdr.icmp->icmp_cksum = pf_cksum_fixup( 3174223637Sbz pd->hdr.icmp->icmp_cksum, sport, 3175223637Sbz nk->port[1], 0); 3176223637Sbz pd->hdr.icmp->icmp_id = nk->port[1]; 3177223637Sbz pd->sport = &pd->hdr.icmp->icmp_id; 3178223637Sbz } 3179223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 3180223637Sbz break; 3181223637Sbz#endif /* INET */ 3182223637Sbz#ifdef INET6 3183223637Sbz case IPPROTO_ICMPV6: 3184223637Sbz nk->port[0] = nk->port[1]; 3185223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6)) 3186223637Sbz pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum, 3187223637Sbz &nk->addr[pd->sidx], 0); 3188223637Sbz 3189223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6)) 3190223637Sbz pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum, 3191223637Sbz &nk->addr[pd->didx], 0); 3192223637Sbz rewrite++; 3193223637Sbz break; 3194223637Sbz#endif /* INET */ 3195223637Sbz default: 3196223637Sbz switch (af) { 3197223637Sbz#ifdef INET 3198223637Sbz case AF_INET: 3199223637Sbz if (PF_ANEQ(saddr, 3200223637Sbz &nk->addr[pd->sidx], AF_INET)) 3201223637Sbz pf_change_a(&saddr->v4.s_addr, 3202223637Sbz pd->ip_sum, 3203223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3204223637Sbz 3205223637Sbz if (PF_ANEQ(daddr, 3206223637Sbz &nk->addr[pd->didx], AF_INET)) 3207223637Sbz pf_change_a(&daddr->v4.s_addr, 3208223637Sbz pd->ip_sum, 3209223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3210223637Sbz break; 3211223637Sbz#endif /* INET */ 3212223637Sbz#ifdef INET6 3213223637Sbz case AF_INET6: 3214223637Sbz if (PF_ANEQ(saddr, 3215223637Sbz &nk->addr[pd->sidx], AF_INET6)) 3216223637Sbz PF_ACPY(saddr, &nk->addr[pd->sidx], af); 3217223637Sbz 3218223637Sbz if (PF_ANEQ(daddr, 3219223637Sbz &nk->addr[pd->didx], AF_INET6)) 3220223637Sbz PF_ACPY(saddr, &nk->addr[pd->didx], af); 3221223637Sbz break; 3222223637Sbz#endif /* INET */ 3223223637Sbz } 3224223637Sbz break; 3225126258Smlaier } 3226223637Sbz if (nr->natpass) 3227223637Sbz r = NULL; 3228223637Sbz pd->nat_rule = nr; 3229126258Smlaier } 3230126258Smlaier 3231126258Smlaier while (r != NULL) { 3232126258Smlaier r->evaluations++; 3233171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3234126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3235126258Smlaier else if (r->direction && r->direction != direction) 3236126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3237126258Smlaier else if (r->af && r->af != af) 3238126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3239223637Sbz else if (r->proto && r->proto != pd->proto) 3240126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3241171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, saddr, af, 3242231852Sbz r->src.neg, kif, M_GETFIB(m))) 3243126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3244223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3245126258Smlaier else if (r->src.port_op && !pf_match_port(r->src.port_op, 3246223637Sbz r->src.port[0], r->src.port[1], sport)) 3247126258Smlaier r = r->skip[PF_SKIP_SRC_PORT].ptr; 3248171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, 3249231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3250126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3251223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3252126258Smlaier else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 3253223637Sbz r->dst.port[0], r->dst.port[1], dport)) 3254126258Smlaier r = r->skip[PF_SKIP_DST_PORT].ptr; 3255223637Sbz /* icmp only. type always 0 in other cases */ 3256223637Sbz else if (r->type && r->type != icmptype + 1) 3257223637Sbz r = TAILQ_NEXT(r, entries); 3258223637Sbz /* icmp only. type always 0 in other cases */ 3259223637Sbz else if (r->code && r->code != icmpcode + 1) 3260223637Sbz r = TAILQ_NEXT(r, entries); 3261171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3262126258Smlaier r = TAILQ_NEXT(r, entries); 3263126258Smlaier else if (r->rule_flag & PFRULE_FRAGMENT) 3264126258Smlaier r = TAILQ_NEXT(r, entries); 3265223637Sbz else if (pd->proto == IPPROTO_TCP && 3266223637Sbz (r->flagset & th->th_flags) != r->flags) 3267126258Smlaier r = TAILQ_NEXT(r, entries); 3268223637Sbz /* tcp/udp only. uid.op always 0 in other cases */ 3269171168Smlaier else if (r->uid.op && (pd->lookup.done || (pd->lookup.done = 3270240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3271126258Smlaier !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 3272171168Smlaier pd->lookup.uid)) 3273126258Smlaier r = TAILQ_NEXT(r, entries); 3274223637Sbz /* tcp/udp only. gid.op always 0 in other cases */ 3275171168Smlaier else if (r->gid.op && (pd->lookup.done || (pd->lookup.done = 3276240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3277126258Smlaier !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 3278171168Smlaier pd->lookup.gid)) 3279126258Smlaier r = TAILQ_NEXT(r, entries); 3280223637Sbz else if (r->prob && 3281223637Sbz r->prob <= arc4random()) 3282126258Smlaier r = TAILQ_NEXT(r, entries); 3283240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3284240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3285126258Smlaier r = TAILQ_NEXT(r, entries); 3286223637Sbz else if (r->os_fingerprint != PF_OSFP_ANY && 3287223637Sbz (pd->proto != IPPROTO_TCP || !pf_osfp_match( 3288223637Sbz pf_osfp_fingerprint(pd, m, off, th), 3289223637Sbz r->os_fingerprint))) 3290126258Smlaier r = TAILQ_NEXT(r, entries); 3291126258Smlaier else { 3292126258Smlaier if (r->tag) 3293126258Smlaier tag = r->tag; 3294171168Smlaier if (r->rtableid >= 0) 3295171168Smlaier rtableid = r->rtableid; 3296126258Smlaier if (r->anchor == NULL) { 3297171168Smlaier match = 1; 3298126258Smlaier *rm = r; 3299126258Smlaier *am = a; 3300126258Smlaier *rsm = ruleset; 3301126258Smlaier if ((*rm)->quick) 3302126258Smlaier break; 3303126258Smlaier r = TAILQ_NEXT(r, entries); 3304126258Smlaier } else 3305240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3306240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3307240641Sglebius &match); 3308126258Smlaier } 3309240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3310240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3311171168Smlaier break; 3312126258Smlaier } 3313126258Smlaier r = *rm; 3314126258Smlaier a = *am; 3315126258Smlaier ruleset = *rsm; 3316126258Smlaier 3317126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3318126258Smlaier 3319223637Sbz if (r->log || (nr != NULL && nr->log)) { 3320126258Smlaier if (rewrite) 3321223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3322240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, 3323240233Sglebius ruleset, pd, 1); 3324126258Smlaier } 3325126258Smlaier 3326126258Smlaier if ((r->action == PF_DROP) && 3327126258Smlaier ((r->rule_flag & PFRULE_RETURNRST) || 3328126258Smlaier (r->rule_flag & PFRULE_RETURNICMP) || 3329126258Smlaier (r->rule_flag & PFRULE_RETURN))) { 3330126258Smlaier /* undo NAT changes, if they have taken place */ 3331130613Smlaier if (nr != NULL) { 3332223637Sbz PF_ACPY(saddr, &sk->addr[pd->sidx], af); 3333223637Sbz PF_ACPY(daddr, &sk->addr[pd->didx], af); 3334223637Sbz if (pd->sport) 3335223637Sbz *pd->sport = sk->port[pd->sidx]; 3336223637Sbz if (pd->dport) 3337223637Sbz *pd->dport = sk->port[pd->didx]; 3338223637Sbz if (pd->proto_sum) 3339223637Sbz *pd->proto_sum = bproto_sum; 3340223637Sbz if (pd->ip_sum) 3341223637Sbz *pd->ip_sum = bip_sum; 3342223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3343126258Smlaier } 3344223637Sbz if (pd->proto == IPPROTO_TCP && 3345223637Sbz ((r->rule_flag & PFRULE_RETURNRST) || 3346126258Smlaier (r->rule_flag & PFRULE_RETURN)) && 3347126258Smlaier !(th->th_flags & TH_RST)) { 3348223637Sbz u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 3349223637Sbz int len = 0; 3350223637Sbz#ifdef INET 3351223637Sbz struct ip *h4; 3352223637Sbz#endif 3353223637Sbz#ifdef INET6 3354223637Sbz struct ip6_hdr *h6; 3355223637Sbz#endif 3356126258Smlaier 3357223637Sbz switch (af) { 3358223637Sbz#ifdef INET 3359223637Sbz case AF_INET: 3360223637Sbz h4 = mtod(m, struct ip *); 3361223637Sbz len = ntohs(h4->ip_len) - off; 3362223637Sbz break; 3363223637Sbz#endif 3364223637Sbz#ifdef INET6 3365223637Sbz case AF_INET6: 3366223637Sbz h6 = mtod(m, struct ip6_hdr *); 3367223637Sbz len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); 3368223637Sbz break; 3369223637Sbz#endif 3370223637Sbz } 3371223637Sbz 3372223637Sbz if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) 3373223637Sbz REASON_SET(&reason, PFRES_PROTCKSUM); 3374223637Sbz else { 3375223637Sbz if (th->th_flags & TH_SYN) 3376223637Sbz ack++; 3377223637Sbz if (th->th_flags & TH_FIN) 3378223637Sbz ack++; 3379223637Sbz pf_send_tcp(m, r, af, pd->dst, 3380223637Sbz pd->src, th->th_dport, th->th_sport, 3381223637Sbz ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 3382240233Sglebius r->return_ttl, 1, 0, kif->pfik_ifp); 3383223637Sbz } 3384223637Sbz } else if (pd->proto != IPPROTO_ICMP && af == AF_INET && 3385223637Sbz r->return_icmp) 3386126258Smlaier pf_send_icmp(m, r->return_icmp >> 8, 3387126258Smlaier r->return_icmp & 255, af, r); 3388223637Sbz else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && 3389223637Sbz r->return_icmp6) 3390126258Smlaier pf_send_icmp(m, r->return_icmp6 >> 8, 3391126258Smlaier r->return_icmp6 & 255, af, r); 3392126258Smlaier } 3393126258Smlaier 3394126258Smlaier if (r->action == PF_DROP) 3395223637Sbz goto cleanup; 3396126258Smlaier 3397240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3398126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3399223637Sbz goto cleanup; 3400126258Smlaier } 3401240233Sglebius if (rtableid >= 0) 3402240233Sglebius M_SETFIB(m, rtableid); 3403126258Smlaier 3404223637Sbz if (!state_icmp && (r->keep_state || nr != NULL || 3405223637Sbz (pd->flags & PFDESC_TCP_NORM))) { 3406223637Sbz int action; 3407240233Sglebius action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, 3408240233Sglebius sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, 3409240233Sglebius hdrlen); 3410223637Sbz if (action != PF_PASS) 3411223637Sbz return (action); 3412223637Sbz } else { 3413223637Sbz if (sk != NULL) 3414240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3415223637Sbz if (nk != NULL) 3416240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3417223637Sbz } 3418126258Smlaier 3419223637Sbz /* copy back packet headers if we performed NAT operations */ 3420223637Sbz if (rewrite) 3421223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3422130613Smlaier 3423240233Sglebius if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 3424240233Sglebius direction == PF_OUT && 3425240233Sglebius pfsync_defer_ptr != NULL && pfsync_defer_ptr(*sm, m)) 3426223637Sbz /* 3427223637Sbz * We want the state created, but we dont 3428223637Sbz * want to send this in case a partner 3429223637Sbz * firewall has to know about it to allow 3430223637Sbz * replies through it. 3431223637Sbz */ 3432240233Sglebius return (PF_DEFER); 3433223637Sbz 3434223637Sbz return (PF_PASS); 3435223637Sbz 3436130613Smlaiercleanup: 3437223637Sbz if (sk != NULL) 3438240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3439223637Sbz if (nk != NULL) 3440240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3441223637Sbz return (PF_DROP); 3442223637Sbz} 3443126258Smlaier 3444240233Sglebiusstatic int 3445223637Sbzpf_create_state(struct pf_rule *r, struct pf_rule *nr, struct pf_rule *a, 3446240233Sglebius struct pf_pdesc *pd, struct pf_src_node *nsn, struct pf_state_key *nk, 3447240233Sglebius struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport, 3448240233Sglebius u_int16_t dport, int *rewrite, struct pfi_kif *kif, struct pf_state **sm, 3449240233Sglebius int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen) 3450223637Sbz{ 3451223637Sbz struct pf_state *s = NULL; 3452223637Sbz struct pf_src_node *sn = NULL; 3453223637Sbz struct tcphdr *th = pd->hdr.tcp; 3454223637Sbz u_int16_t mss = V_tcp_mssdflt; 3455223637Sbz u_short reason; 3456223637Sbz 3457223637Sbz /* check maximums */ 3458263029Sglebius if (r->max_states && 3459263029Sglebius (counter_u64_fetch(r->states_cur) >= r->max_states)) { 3460223637Sbz V_pf_status.lcounters[LCNT_STATES]++; 3461223637Sbz REASON_SET(&reason, PFRES_MAXSTATES); 3462223637Sbz return (PF_DROP); 3463223637Sbz } 3464223637Sbz /* src node for filter rule */ 3465223637Sbz if ((r->rule_flag & PFRULE_SRCTRACK || 3466223637Sbz r->rpool.opts & PF_POOL_STICKYADDR) && 3467223637Sbz pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) { 3468223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3469223637Sbz goto csfailed; 3470223637Sbz } 3471223637Sbz /* src node for translation rule */ 3472223637Sbz if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) && 3473223637Sbz pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) { 3474223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3475223637Sbz goto csfailed; 3476223637Sbz } 3477240233Sglebius s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO); 3478223637Sbz if (s == NULL) { 3479223637Sbz REASON_SET(&reason, PFRES_MEMORY); 3480223637Sbz goto csfailed; 3481223637Sbz } 3482223637Sbz s->rule.ptr = r; 3483223637Sbz s->nat_rule.ptr = nr; 3484223637Sbz s->anchor.ptr = a; 3485223637Sbz STATE_INC_COUNTERS(s); 3486223637Sbz if (r->allow_opts) 3487223637Sbz s->state_flags |= PFSTATE_ALLOWOPTS; 3488223637Sbz if (r->rule_flag & PFRULE_STATESLOPPY) 3489223637Sbz s->state_flags |= PFSTATE_SLOPPY; 3490223637Sbz s->log = r->log & PF_LOG_ALL; 3491223637Sbz s->sync_state = PFSYNC_S_NONE; 3492223637Sbz if (nr != NULL) 3493223637Sbz s->log |= nr->log & PF_LOG_ALL; 3494223637Sbz switch (pd->proto) { 3495223637Sbz case IPPROTO_TCP: 3496126258Smlaier s->src.seqlo = ntohl(th->th_seq); 3497223637Sbz s->src.seqhi = s->src.seqlo + pd->p_len + 1; 3498126258Smlaier if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN && 3499126258Smlaier r->keep_state == PF_STATE_MODULATE) { 3500126258Smlaier /* Generate sequence number modulator */ 3501223637Sbz if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 3502223637Sbz 0) 3503223637Sbz s->src.seqdiff = 1; 3504126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, 3505126258Smlaier htonl(s->src.seqlo + s->src.seqdiff), 0); 3506223637Sbz *rewrite = 1; 3507126258Smlaier } else 3508126258Smlaier s->src.seqdiff = 0; 3509126258Smlaier if (th->th_flags & TH_SYN) { 3510126258Smlaier s->src.seqhi++; 3511223637Sbz s->src.wscale = pf_get_wscale(m, off, 3512223637Sbz th->th_off, pd->af); 3513126258Smlaier } 3514126258Smlaier s->src.max_win = MAX(ntohs(th->th_win), 1); 3515126258Smlaier if (s->src.wscale & PF_WSCALE_MASK) { 3516126258Smlaier /* Remove scale factor from initial window */ 3517126258Smlaier int win = s->src.max_win; 3518126258Smlaier win += 1 << (s->src.wscale & PF_WSCALE_MASK); 3519126258Smlaier s->src.max_win = (win - 1) >> 3520126258Smlaier (s->src.wscale & PF_WSCALE_MASK); 3521126258Smlaier } 3522126258Smlaier if (th->th_flags & TH_FIN) 3523126258Smlaier s->src.seqhi++; 3524126258Smlaier s->dst.seqhi = 1; 3525126258Smlaier s->dst.max_win = 1; 3526126258Smlaier s->src.state = TCPS_SYN_SENT; 3527126258Smlaier s->dst.state = TCPS_CLOSED; 3528126258Smlaier s->timeout = PFTM_TCP_FIRST_PACKET; 3529223637Sbz break; 3530223637Sbz case IPPROTO_UDP: 3531223637Sbz s->src.state = PFUDPS_SINGLE; 3532223637Sbz s->dst.state = PFUDPS_NO_TRAFFIC; 3533223637Sbz s->timeout = PFTM_UDP_FIRST_PACKET; 3534223637Sbz break; 3535223637Sbz case IPPROTO_ICMP: 3536223637Sbz#ifdef INET6 3537223637Sbz case IPPROTO_ICMPV6: 3538223637Sbz#endif 3539223637Sbz s->timeout = PFTM_ICMP_FIRST_PACKET; 3540223637Sbz break; 3541223637Sbz default: 3542223637Sbz s->src.state = PFOTHERS_SINGLE; 3543223637Sbz s->dst.state = PFOTHERS_NO_TRAFFIC; 3544223637Sbz s->timeout = PFTM_OTHER_FIRST_PACKET; 3545223637Sbz } 3546223637Sbz 3547240233Sglebius s->creation = time_uptime; 3548240233Sglebius s->expire = time_uptime; 3549223637Sbz 3550223637Sbz if (sn != NULL) { 3551223637Sbz s->src_node = sn; 3552223637Sbz s->src_node->states++; 3553223637Sbz } 3554223637Sbz if (nsn != NULL) { 3555223637Sbz /* XXX We only modify one side for now. */ 3556223637Sbz PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af); 3557223637Sbz s->nat_src_node = nsn; 3558223637Sbz s->nat_src_node->states++; 3559223637Sbz } 3560223637Sbz if (pd->proto == IPPROTO_TCP) { 3561126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m, 3562126258Smlaier off, pd, th, &s->src, &s->dst)) { 3563126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3564130613Smlaier pf_src_tree_remove_state(s); 3565145836Smlaier STATE_DEC_COUNTERS(s); 3566240233Sglebius uma_zfree(V_pf_state_z, s); 3567126258Smlaier return (PF_DROP); 3568126258Smlaier } 3569126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub && 3570145836Smlaier pf_normalize_tcp_stateful(m, off, pd, &reason, th, s, 3571223637Sbz &s->src, &s->dst, rewrite)) { 3572145836Smlaier /* This really shouldn't happen!!! */ 3573145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 3574145836Smlaier ("pf_normalize_tcp_stateful failed on first pkt")); 3575126258Smlaier pf_normalize_tcp_cleanup(s); 3576130613Smlaier pf_src_tree_remove_state(s); 3577145836Smlaier STATE_DEC_COUNTERS(s); 3578240233Sglebius uma_zfree(V_pf_state_z, s); 3579223637Sbz return (PF_DROP); 3580126258Smlaier } 3581126258Smlaier } 3582223637Sbz s->direction = pd->dir; 3583126258Smlaier 3584240233Sglebius /* 3585240233Sglebius * sk/nk could already been setup by pf_get_translation(). 3586240233Sglebius */ 3587240233Sglebius if (nr == NULL) { 3588240233Sglebius KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p", 3589240233Sglebius __func__, nr, sk, nk)); 3590240233Sglebius sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport); 3591240233Sglebius if (sk == NULL) 3592240233Sglebius goto csfailed; 3593240233Sglebius nk = sk; 3594240233Sglebius } else 3595240233Sglebius KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p", 3596240233Sglebius __func__, nr, sk, nk)); 3597126258Smlaier 3598240233Sglebius /* Swap sk/nk for PF_OUT. */ 3599240233Sglebius if (pf_state_insert(BOUND_IFACE(r, kif), 3600240233Sglebius (pd->dir == PF_IN) ? sk : nk, 3601240233Sglebius (pd->dir == PF_IN) ? nk : sk, s)) { 3602223637Sbz if (pd->proto == IPPROTO_TCP) 3603223637Sbz pf_normalize_tcp_cleanup(s); 3604223637Sbz REASON_SET(&reason, PFRES_STATEINS); 3605223637Sbz pf_src_tree_remove_state(s); 3606223637Sbz STATE_DEC_COUNTERS(s); 3607240233Sglebius uma_zfree(V_pf_state_z, s); 3608223637Sbz return (PF_DROP); 3609223637Sbz } else 3610223637Sbz *sm = s; 3611126258Smlaier 3612223637Sbz pf_set_rt_ifp(s, pd->src); /* needs s->state_key set */ 3613240233Sglebius if (tag > 0) 3614223637Sbz s->tag = tag; 3615223637Sbz if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) == 3616223637Sbz TH_SYN && r->keep_state == PF_STATE_SYNPROXY) { 3617223637Sbz s->src.state = PF_TCPS_PROXY_SRC; 3618223637Sbz /* undo NAT changes, if they have taken place */ 3619223637Sbz if (nr != NULL) { 3620223637Sbz struct pf_state_key *skt = s->key[PF_SK_WIRE]; 3621223637Sbz if (pd->dir == PF_OUT) 3622223637Sbz skt = s->key[PF_SK_STACK]; 3623223637Sbz PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af); 3624223637Sbz PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af); 3625223637Sbz if (pd->sport) 3626223637Sbz *pd->sport = skt->port[pd->sidx]; 3627223637Sbz if (pd->dport) 3628223637Sbz *pd->dport = skt->port[pd->didx]; 3629223637Sbz if (pd->proto_sum) 3630223637Sbz *pd->proto_sum = bproto_sum; 3631223637Sbz if (pd->ip_sum) 3632223637Sbz *pd->ip_sum = bip_sum; 3633223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3634223637Sbz } 3635223637Sbz s->src.seqhi = htonl(arc4random()); 3636223637Sbz /* Find mss option */ 3637231852Sbz int rtid = M_GETFIB(m); 3638223637Sbz mss = pf_get_mss(m, off, th->th_off, pd->af); 3639231852Sbz mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 3640231852Sbz mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 3641223637Sbz s->src.mss = mss; 3642223637Sbz pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport, 3643223637Sbz th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 3644240233Sglebius TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL); 3645223637Sbz REASON_SET(&reason, PFRES_SYNPROXY); 3646223637Sbz return (PF_SYNPROXY_DROP); 3647171168Smlaier } 3648165631Smlaier 3649223637Sbz return (PF_PASS); 3650126258Smlaier 3651223637Sbzcsfailed: 3652223637Sbz if (sk != NULL) 3653240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3654223637Sbz if (nk != NULL) 3655240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3656223637Sbz 3657261019Sglebius if (sn != NULL && sn->states == 0 && sn->expire == 0) { 3658261019Sglebius pf_unlink_src_node(sn); 3659261019Sglebius pf_free_src_node(sn); 3660261019Sglebius } 3661240737Sglebius 3662261019Sglebius if (nsn != sn && nsn != NULL && nsn->states == 0 && nsn->expire == 0) { 3663261019Sglebius pf_unlink_src_node(nsn); 3664261019Sglebius pf_free_src_node(nsn); 3665261019Sglebius } 3666240737Sglebius 3667223637Sbz return (PF_DROP); 3668126258Smlaier} 3669126258Smlaier 3670240233Sglebiusstatic int 3671130613Smlaierpf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif, 3672126258Smlaier struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am, 3673126258Smlaier struct pf_ruleset **rsm) 3674126258Smlaier{ 3675126258Smlaier struct pf_rule *r, *a = NULL; 3676126258Smlaier struct pf_ruleset *ruleset = NULL; 3677126258Smlaier sa_family_t af = pd->af; 3678126258Smlaier u_short reason; 3679126258Smlaier int tag = -1; 3680145836Smlaier int asd = 0; 3681171168Smlaier int match = 0; 3682240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3683126258Smlaier 3684240233Sglebius PF_RULES_RASSERT(); 3685240233Sglebius 3686126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3687126258Smlaier while (r != NULL) { 3688126258Smlaier r->evaluations++; 3689171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3690126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3691126258Smlaier else if (r->direction && r->direction != direction) 3692126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3693126258Smlaier else if (r->af && r->af != af) 3694126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3695126258Smlaier else if (r->proto && r->proto != pd->proto) 3696126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3697171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 3698231852Sbz r->src.neg, kif, M_GETFIB(m))) 3699126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3700171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 3701231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3702126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3703171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3704126258Smlaier r = TAILQ_NEXT(r, entries); 3705173815Smlaier else if (r->os_fingerprint != PF_OSFP_ANY) 3706126258Smlaier r = TAILQ_NEXT(r, entries); 3707173815Smlaier else if (pd->proto == IPPROTO_UDP && 3708173815Smlaier (r->src.port_op || r->dst.port_op)) 3709173815Smlaier r = TAILQ_NEXT(r, entries); 3710173815Smlaier else if (pd->proto == IPPROTO_TCP && 3711173815Smlaier (r->src.port_op || r->dst.port_op || r->flagset)) 3712173815Smlaier r = TAILQ_NEXT(r, entries); 3713173815Smlaier else if ((pd->proto == IPPROTO_ICMP || 3714173815Smlaier pd->proto == IPPROTO_ICMPV6) && 3715173815Smlaier (r->type || r->code)) 3716173815Smlaier r = TAILQ_NEXT(r, entries); 3717223637Sbz else if (r->prob && r->prob <= 3718223637Sbz (arc4random() % (UINT_MAX - 1) + 1)) 3719126258Smlaier r = TAILQ_NEXT(r, entries); 3720240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3721240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3722126258Smlaier r = TAILQ_NEXT(r, entries); 3723126258Smlaier else { 3724126258Smlaier if (r->anchor == NULL) { 3725171168Smlaier match = 1; 3726126258Smlaier *rm = r; 3727126258Smlaier *am = a; 3728126258Smlaier *rsm = ruleset; 3729126258Smlaier if ((*rm)->quick) 3730126258Smlaier break; 3731126258Smlaier r = TAILQ_NEXT(r, entries); 3732126258Smlaier } else 3733240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3734240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3735240641Sglebius &match); 3736126258Smlaier } 3737240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3738240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3739171168Smlaier break; 3740126258Smlaier } 3741126258Smlaier r = *rm; 3742126258Smlaier a = *am; 3743126258Smlaier ruleset = *rsm; 3744126258Smlaier 3745126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3746130613Smlaier 3747126258Smlaier if (r->log) 3748240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd, 3749240233Sglebius 1); 3750126258Smlaier 3751126258Smlaier if (r->action != PF_PASS) 3752126258Smlaier return (PF_DROP); 3753126258Smlaier 3754240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3755126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3756126258Smlaier return (PF_DROP); 3757126258Smlaier } 3758126258Smlaier 3759126258Smlaier return (PF_PASS); 3760126258Smlaier} 3761126258Smlaier 3762240233Sglebiusstatic int 3763200930Sdelphijpf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst, 3764200930Sdelphij struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off, 3765200930Sdelphij struct pf_pdesc *pd, u_short *reason, int *copyback) 3766126258Smlaier{ 3767223637Sbz struct tcphdr *th = pd->hdr.tcp; 3768223637Sbz u_int16_t win = ntohs(th->th_win); 3769223637Sbz u_int32_t ack, end, seq, orig_seq; 3770223637Sbz u_int8_t sws, dws; 3771223637Sbz int ackskew; 3772126258Smlaier 3773126258Smlaier if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) { 3774126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3775126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3776126258Smlaier } else 3777126258Smlaier sws = dws = 0; 3778126258Smlaier 3779126258Smlaier /* 3780126258Smlaier * Sequence tracking algorithm from Guido van Rooij's paper: 3781126258Smlaier * http://www.madison-gurkha.com/publications/tcp_filtering/ 3782126258Smlaier * tcp_filtering.ps 3783126258Smlaier */ 3784126258Smlaier 3785145836Smlaier orig_seq = seq = ntohl(th->th_seq); 3786126258Smlaier if (src->seqlo == 0) { 3787126258Smlaier /* First packet from this end. Set its state */ 3788126258Smlaier 3789126258Smlaier if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) && 3790126258Smlaier src->scrub == NULL) { 3791126258Smlaier if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) { 3792126258Smlaier REASON_SET(reason, PFRES_MEMORY); 3793126258Smlaier return (PF_DROP); 3794126258Smlaier } 3795126258Smlaier } 3796126258Smlaier 3797126258Smlaier /* Deferred generation of sequence number modulator */ 3798126258Smlaier if (dst->seqdiff && !src->seqdiff) { 3799223637Sbz /* use random iss for the TCP server */ 3800223637Sbz while ((src->seqdiff = arc4random() - seq) == 0) 3801126258Smlaier ; 3802126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3803126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3804126258Smlaier src->seqdiff), 0); 3805126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3806200930Sdelphij *copyback = 1; 3807126258Smlaier } else { 3808126258Smlaier ack = ntohl(th->th_ack); 3809126258Smlaier } 3810126258Smlaier 3811126258Smlaier end = seq + pd->p_len; 3812126258Smlaier if (th->th_flags & TH_SYN) { 3813126258Smlaier end++; 3814126258Smlaier if (dst->wscale & PF_WSCALE_FLAG) { 3815126258Smlaier src->wscale = pf_get_wscale(m, off, th->th_off, 3816126258Smlaier pd->af); 3817126258Smlaier if (src->wscale & PF_WSCALE_FLAG) { 3818126258Smlaier /* Remove scale factor from initial 3819126258Smlaier * window */ 3820126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3821126258Smlaier win = ((u_int32_t)win + (1 << sws) - 1) 3822126258Smlaier >> sws; 3823126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3824126258Smlaier } else { 3825126258Smlaier /* fixup other window */ 3826126258Smlaier dst->max_win <<= dst->wscale & 3827126258Smlaier PF_WSCALE_MASK; 3828126258Smlaier /* in case of a retrans SYN|ACK */ 3829126258Smlaier dst->wscale = 0; 3830126258Smlaier } 3831126258Smlaier } 3832126258Smlaier } 3833126258Smlaier if (th->th_flags & TH_FIN) 3834126258Smlaier end++; 3835126258Smlaier 3836126258Smlaier src->seqlo = seq; 3837126258Smlaier if (src->state < TCPS_SYN_SENT) 3838126258Smlaier src->state = TCPS_SYN_SENT; 3839126258Smlaier 3840126258Smlaier /* 3841126258Smlaier * May need to slide the window (seqhi may have been set by 3842126258Smlaier * the crappy stack check or if we picked up the connection 3843126258Smlaier * after establishment) 3844126258Smlaier */ 3845126258Smlaier if (src->seqhi == 1 || 3846126258Smlaier SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 3847126258Smlaier src->seqhi = end + MAX(1, dst->max_win << dws); 3848126258Smlaier if (win > src->max_win) 3849126258Smlaier src->max_win = win; 3850126258Smlaier 3851126258Smlaier } else { 3852126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3853126258Smlaier if (src->seqdiff) { 3854126258Smlaier /* Modulate sequence numbers */ 3855126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3856126258Smlaier src->seqdiff), 0); 3857126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3858200930Sdelphij *copyback = 1; 3859126258Smlaier } 3860126258Smlaier end = seq + pd->p_len; 3861126258Smlaier if (th->th_flags & TH_SYN) 3862126258Smlaier end++; 3863126258Smlaier if (th->th_flags & TH_FIN) 3864126258Smlaier end++; 3865126258Smlaier } 3866126258Smlaier 3867126258Smlaier if ((th->th_flags & TH_ACK) == 0) { 3868126258Smlaier /* Let it pass through the ack skew check */ 3869126258Smlaier ack = dst->seqlo; 3870126258Smlaier } else if ((ack == 0 && 3871126258Smlaier (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 3872126258Smlaier /* broken tcp stacks do not set ack */ 3873126258Smlaier (dst->state < TCPS_SYN_SENT)) { 3874126258Smlaier /* 3875126258Smlaier * Many stacks (ours included) will set the ACK number in an 3876126258Smlaier * FIN|ACK if the SYN times out -- no sequence to ACK. 3877126258Smlaier */ 3878126258Smlaier ack = dst->seqlo; 3879126258Smlaier } 3880126258Smlaier 3881126258Smlaier if (seq == end) { 3882126258Smlaier /* Ease sequencing restrictions on no data packets */ 3883126258Smlaier seq = src->seqlo; 3884126258Smlaier end = seq; 3885126258Smlaier } 3886126258Smlaier 3887126258Smlaier ackskew = dst->seqlo - ack; 3888126258Smlaier 3889171168Smlaier 3890171168Smlaier /* 3891171168Smlaier * Need to demodulate the sequence numbers in any TCP SACK options 3892171168Smlaier * (Selective ACK). We could optionally validate the SACK values 3893171168Smlaier * against the current ACK window, either forwards or backwards, but 3894171168Smlaier * I'm not confident that SACK has been implemented properly 3895171168Smlaier * everywhere. It wouldn't surprise me if several stacks accidently 3896171168Smlaier * SACK too far backwards of previously ACKed data. There really aren't 3897171168Smlaier * any security implications of bad SACKing unless the target stack 3898171168Smlaier * doesn't validate the option length correctly. Someone trying to 3899171168Smlaier * spoof into a TCP connection won't bother blindly sending SACK 3900171168Smlaier * options anyway. 3901171168Smlaier */ 3902171168Smlaier if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 3903171168Smlaier if (pf_modulate_sack(m, off, pd, th, dst)) 3904200930Sdelphij *copyback = 1; 3905171168Smlaier } 3906171168Smlaier 3907171168Smlaier 3908223637Sbz#define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 3909126258Smlaier if (SEQ_GEQ(src->seqhi, end) && 3910126258Smlaier /* Last octet inside other's window space */ 3911126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 3912126258Smlaier /* Retrans: not more than one window back */ 3913126258Smlaier (ackskew >= -MAXACKWINDOW) && 3914126258Smlaier /* Acking not more than one reassembled fragment backwards */ 3915145836Smlaier (ackskew <= (MAXACKWINDOW << sws)) && 3916126258Smlaier /* Acking not more than one window forward */ 3917145836Smlaier ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo || 3918223637Sbz (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) || 3919223637Sbz (pd->flags & PFDESC_IP_REAS) == 0)) { 3920171168Smlaier /* Require an exact/+1 sequence match on resets when possible */ 3921126258Smlaier 3922145836Smlaier if (dst->scrub || src->scrub) { 3923145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 3924200930Sdelphij *state, src, dst, copyback)) 3925145836Smlaier return (PF_DROP); 3926145836Smlaier } 3927145836Smlaier 3928126258Smlaier /* update max window */ 3929126258Smlaier if (src->max_win < win) 3930126258Smlaier src->max_win = win; 3931126258Smlaier /* synchronize sequencing */ 3932126258Smlaier if (SEQ_GT(end, src->seqlo)) 3933126258Smlaier src->seqlo = end; 3934126258Smlaier /* slide the window of what the other end can send */ 3935126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 3936126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 3937126258Smlaier 3938126258Smlaier 3939126258Smlaier /* update states */ 3940126258Smlaier if (th->th_flags & TH_SYN) 3941126258Smlaier if (src->state < TCPS_SYN_SENT) 3942126258Smlaier src->state = TCPS_SYN_SENT; 3943126258Smlaier if (th->th_flags & TH_FIN) 3944126258Smlaier if (src->state < TCPS_CLOSING) 3945126258Smlaier src->state = TCPS_CLOSING; 3946126258Smlaier if (th->th_flags & TH_ACK) { 3947145836Smlaier if (dst->state == TCPS_SYN_SENT) { 3948126258Smlaier dst->state = TCPS_ESTABLISHED; 3949145836Smlaier if (src->state == TCPS_ESTABLISHED && 3950145836Smlaier (*state)->src_node != NULL && 3951145836Smlaier pf_src_connlimit(state)) { 3952145836Smlaier REASON_SET(reason, PFRES_SRCLIMIT); 3953145836Smlaier return (PF_DROP); 3954145836Smlaier } 3955145836Smlaier } else if (dst->state == TCPS_CLOSING) 3956126258Smlaier dst->state = TCPS_FIN_WAIT_2; 3957126258Smlaier } 3958126258Smlaier if (th->th_flags & TH_RST) 3959126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 3960126258Smlaier 3961126258Smlaier /* update expire time */ 3962240233Sglebius (*state)->expire = time_uptime; 3963126258Smlaier if (src->state >= TCPS_FIN_WAIT_2 && 3964126258Smlaier dst->state >= TCPS_FIN_WAIT_2) 3965126258Smlaier (*state)->timeout = PFTM_TCP_CLOSED; 3966171168Smlaier else if (src->state >= TCPS_CLOSING && 3967171168Smlaier dst->state >= TCPS_CLOSING) 3968126258Smlaier (*state)->timeout = PFTM_TCP_FIN_WAIT; 3969126258Smlaier else if (src->state < TCPS_ESTABLISHED || 3970126258Smlaier dst->state < TCPS_ESTABLISHED) 3971126258Smlaier (*state)->timeout = PFTM_TCP_OPENING; 3972126258Smlaier else if (src->state >= TCPS_CLOSING || 3973126258Smlaier dst->state >= TCPS_CLOSING) 3974126258Smlaier (*state)->timeout = PFTM_TCP_CLOSING; 3975126258Smlaier else 3976126258Smlaier (*state)->timeout = PFTM_TCP_ESTABLISHED; 3977126258Smlaier 3978126258Smlaier /* Fall through to PASS packet */ 3979126258Smlaier 3980126258Smlaier } else if ((dst->state < TCPS_SYN_SENT || 3981126258Smlaier dst->state >= TCPS_FIN_WAIT_2 || 3982126258Smlaier src->state >= TCPS_FIN_WAIT_2) && 3983126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) && 3984126258Smlaier /* Within a window forward of the originating packet */ 3985126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 3986126258Smlaier /* Within a window backward of the originating packet */ 3987126258Smlaier 3988126258Smlaier /* 3989126258Smlaier * This currently handles three situations: 3990126258Smlaier * 1) Stupid stacks will shotgun SYNs before their peer 3991126258Smlaier * replies. 3992126258Smlaier * 2) When PF catches an already established stream (the 3993126258Smlaier * firewall rebooted, the state table was flushed, routes 3994126258Smlaier * changed...) 3995126258Smlaier * 3) Packets get funky immediately after the connection 3996126258Smlaier * closes (this should catch Solaris spurious ACK|FINs 3997126258Smlaier * that web servers like to spew after a close) 3998126258Smlaier * 3999126258Smlaier * This must be a little more careful than the above code 4000126258Smlaier * since packet floods will also be caught here. We don't 4001126258Smlaier * update the TTL here to mitigate the damage of a packet 4002126258Smlaier * flood and so the same code can handle awkward establishment 4003126258Smlaier * and a loosened connection close. 4004126258Smlaier * In the establishment case, a correct peer response will 4005126258Smlaier * validate the connection, go through the normal state code 4006126258Smlaier * and keep updating the state TTL. 4007126258Smlaier */ 4008126258Smlaier 4009223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4010126258Smlaier printf("pf: loose state match: "); 4011126258Smlaier pf_print_state(*state); 4012126258Smlaier pf_print_flags(th->th_flags); 4013171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4014223637Sbz "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 4015223637Sbz pd->p_len, ackskew, (unsigned long long)(*state)->packets[0], 4016223637Sbz (unsigned long long)(*state)->packets[1], 4017223637Sbz pd->dir == PF_IN ? "in" : "out", 4018223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4019126258Smlaier } 4020126258Smlaier 4021145836Smlaier if (dst->scrub || src->scrub) { 4022145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 4023200930Sdelphij *state, src, dst, copyback)) 4024145836Smlaier return (PF_DROP); 4025145836Smlaier } 4026145836Smlaier 4027126258Smlaier /* update max window */ 4028126258Smlaier if (src->max_win < win) 4029126258Smlaier src->max_win = win; 4030126258Smlaier /* synchronize sequencing */ 4031126258Smlaier if (SEQ_GT(end, src->seqlo)) 4032126258Smlaier src->seqlo = end; 4033126258Smlaier /* slide the window of what the other end can send */ 4034126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 4035126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 4036126258Smlaier 4037126258Smlaier /* 4038126258Smlaier * Cannot set dst->seqhi here since this could be a shotgunned 4039126258Smlaier * SYN and not an already established connection. 4040126258Smlaier */ 4041126258Smlaier 4042126258Smlaier if (th->th_flags & TH_FIN) 4043126258Smlaier if (src->state < TCPS_CLOSING) 4044126258Smlaier src->state = TCPS_CLOSING; 4045126258Smlaier if (th->th_flags & TH_RST) 4046126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 4047126258Smlaier 4048126258Smlaier /* Fall through to PASS packet */ 4049126258Smlaier 4050126258Smlaier } else { 4051126258Smlaier if ((*state)->dst.state == TCPS_SYN_SENT && 4052126258Smlaier (*state)->src.state == TCPS_SYN_SENT) { 4053126258Smlaier /* Send RST for state mismatches during handshake */ 4054145836Smlaier if (!(th->th_flags & TH_RST)) 4055223637Sbz pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4056126258Smlaier pd->dst, pd->src, th->th_dport, 4057145836Smlaier th->th_sport, ntohl(th->th_ack), 0, 4058145836Smlaier TH_RST, 0, 0, 4059171168Smlaier (*state)->rule.ptr->return_ttl, 1, 0, 4060240233Sglebius kif->pfik_ifp); 4061126258Smlaier src->seqlo = 0; 4062126258Smlaier src->seqhi = 1; 4063126258Smlaier src->max_win = 1; 4064223637Sbz } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 4065126258Smlaier printf("pf: BAD state: "); 4066126258Smlaier pf_print_state(*state); 4067126258Smlaier pf_print_flags(th->th_flags); 4068171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4069171168Smlaier "pkts=%llu:%llu dir=%s,%s\n", 4070171168Smlaier seq, orig_seq, ack, pd->p_len, ackskew, 4071171168Smlaier (unsigned long long)(*state)->packets[0], 4072171168Smlaier (unsigned long long)(*state)->packets[1], 4073223637Sbz pd->dir == PF_IN ? "in" : "out", 4074223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4075126258Smlaier printf("pf: State failure on: %c %c %c %c | %c %c\n", 4076126258Smlaier SEQ_GEQ(src->seqhi, end) ? ' ' : '1', 4077126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 4078126258Smlaier ' ': '2', 4079126258Smlaier (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 4080126258Smlaier (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 4081126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5', 4082126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 4083126258Smlaier } 4084145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4085126258Smlaier return (PF_DROP); 4086126258Smlaier } 4087126258Smlaier 4088200930Sdelphij return (PF_PASS); 4089200930Sdelphij} 4090126258Smlaier 4091240233Sglebiusstatic int 4092200930Sdelphijpf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst, 4093200930Sdelphij struct pf_state **state, struct pf_pdesc *pd, u_short *reason) 4094200930Sdelphij{ 4095200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4096200930Sdelphij 4097200930Sdelphij if (th->th_flags & TH_SYN) 4098200930Sdelphij if (src->state < TCPS_SYN_SENT) 4099200930Sdelphij src->state = TCPS_SYN_SENT; 4100200930Sdelphij if (th->th_flags & TH_FIN) 4101200930Sdelphij if (src->state < TCPS_CLOSING) 4102200930Sdelphij src->state = TCPS_CLOSING; 4103200930Sdelphij if (th->th_flags & TH_ACK) { 4104200930Sdelphij if (dst->state == TCPS_SYN_SENT) { 4105200930Sdelphij dst->state = TCPS_ESTABLISHED; 4106200930Sdelphij if (src->state == TCPS_ESTABLISHED && 4107200930Sdelphij (*state)->src_node != NULL && 4108200930Sdelphij pf_src_connlimit(state)) { 4109200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4110200930Sdelphij return (PF_DROP); 4111200930Sdelphij } 4112200930Sdelphij } else if (dst->state == TCPS_CLOSING) { 4113200930Sdelphij dst->state = TCPS_FIN_WAIT_2; 4114200930Sdelphij } else if (src->state == TCPS_SYN_SENT && 4115200930Sdelphij dst->state < TCPS_SYN_SENT) { 4116200930Sdelphij /* 4117200930Sdelphij * Handle a special sloppy case where we only see one 4118200930Sdelphij * half of the connection. If there is a ACK after 4119200930Sdelphij * the initial SYN without ever seeing a packet from 4120200930Sdelphij * the destination, set the connection to established. 4121200930Sdelphij */ 4122200930Sdelphij dst->state = src->state = TCPS_ESTABLISHED; 4123200930Sdelphij if ((*state)->src_node != NULL && 4124200930Sdelphij pf_src_connlimit(state)) { 4125200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4126200930Sdelphij return (PF_DROP); 4127200930Sdelphij } 4128200930Sdelphij } else if (src->state == TCPS_CLOSING && 4129200930Sdelphij dst->state == TCPS_ESTABLISHED && 4130200930Sdelphij dst->seqlo == 0) { 4131200930Sdelphij /* 4132200930Sdelphij * Handle the closing of half connections where we 4133200930Sdelphij * don't see the full bidirectional FIN/ACK+ACK 4134200930Sdelphij * handshake. 4135200930Sdelphij */ 4136200930Sdelphij dst->state = TCPS_CLOSING; 4137200930Sdelphij } 4138200930Sdelphij } 4139200930Sdelphij if (th->th_flags & TH_RST) 4140200930Sdelphij src->state = dst->state = TCPS_TIME_WAIT; 4141200930Sdelphij 4142200930Sdelphij /* update expire time */ 4143240233Sglebius (*state)->expire = time_uptime; 4144200930Sdelphij if (src->state >= TCPS_FIN_WAIT_2 && 4145200930Sdelphij dst->state >= TCPS_FIN_WAIT_2) 4146200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSED; 4147200930Sdelphij else if (src->state >= TCPS_CLOSING && 4148200930Sdelphij dst->state >= TCPS_CLOSING) 4149200930Sdelphij (*state)->timeout = PFTM_TCP_FIN_WAIT; 4150200930Sdelphij else if (src->state < TCPS_ESTABLISHED || 4151200930Sdelphij dst->state < TCPS_ESTABLISHED) 4152200930Sdelphij (*state)->timeout = PFTM_TCP_OPENING; 4153200930Sdelphij else if (src->state >= TCPS_CLOSING || 4154200930Sdelphij dst->state >= TCPS_CLOSING) 4155200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSING; 4156200930Sdelphij else 4157200930Sdelphij (*state)->timeout = PFTM_TCP_ESTABLISHED; 4158200930Sdelphij 4159200930Sdelphij return (PF_PASS); 4160200930Sdelphij} 4161200930Sdelphij 4162240233Sglebiusstatic int 4163200930Sdelphijpf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif, 4164200930Sdelphij struct mbuf *m, int off, void *h, struct pf_pdesc *pd, 4165200930Sdelphij u_short *reason) 4166200930Sdelphij{ 4167223637Sbz struct pf_state_key_cmp key; 4168200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4169200930Sdelphij int copyback = 0; 4170200930Sdelphij struct pf_state_peer *src, *dst; 4171223637Sbz struct pf_state_key *sk; 4172200930Sdelphij 4173240233Sglebius bzero(&key, sizeof(key)); 4174200930Sdelphij key.af = pd->af; 4175200930Sdelphij key.proto = IPPROTO_TCP; 4176223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4177223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4178223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4179223637Sbz key.port[0] = th->th_sport; 4180223637Sbz key.port[1] = th->th_dport; 4181223637Sbz } else { /* stack side, reverse */ 4182223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4183223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4184223637Sbz key.port[1] = th->th_sport; 4185223637Sbz key.port[0] = th->th_dport; 4186200930Sdelphij } 4187200930Sdelphij 4188240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4189200930Sdelphij 4190200930Sdelphij if (direction == (*state)->direction) { 4191200930Sdelphij src = &(*state)->src; 4192200930Sdelphij dst = &(*state)->dst; 4193200930Sdelphij } else { 4194200930Sdelphij src = &(*state)->dst; 4195200930Sdelphij dst = &(*state)->src; 4196200930Sdelphij } 4197200930Sdelphij 4198223637Sbz sk = (*state)->key[pd->didx]; 4199223637Sbz 4200200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_SRC) { 4201200930Sdelphij if (direction != (*state)->direction) { 4202200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4203200930Sdelphij return (PF_SYNPROXY_DROP); 4204200930Sdelphij } 4205200930Sdelphij if (th->th_flags & TH_SYN) { 4206200930Sdelphij if (ntohl(th->th_seq) != (*state)->src.seqlo) { 4207200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4208200930Sdelphij return (PF_DROP); 4209200930Sdelphij } 4210200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4211200930Sdelphij pd->src, th->th_dport, th->th_sport, 4212200930Sdelphij (*state)->src.seqhi, ntohl(th->th_seq) + 1, 4213240233Sglebius TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL); 4214200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4215200930Sdelphij return (PF_SYNPROXY_DROP); 4216200930Sdelphij } else if (!(th->th_flags & TH_ACK) || 4217200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4218200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4219200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4220200930Sdelphij return (PF_DROP); 4221200930Sdelphij } else if ((*state)->src_node != NULL && 4222200930Sdelphij pf_src_connlimit(state)) { 4223200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4224200930Sdelphij return (PF_DROP); 4225200930Sdelphij } else 4226200930Sdelphij (*state)->src.state = PF_TCPS_PROXY_DST; 4227200930Sdelphij } 4228200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_DST) { 4229200930Sdelphij if (direction == (*state)->direction) { 4230200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) || 4231200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4232200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4233200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4234200930Sdelphij return (PF_DROP); 4235200930Sdelphij } 4236200930Sdelphij (*state)->src.max_win = MAX(ntohs(th->th_win), 1); 4237200930Sdelphij if ((*state)->dst.seqhi == 1) 4238200930Sdelphij (*state)->dst.seqhi = htonl(arc4random()); 4239200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4240223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4241223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4242200930Sdelphij (*state)->dst.seqhi, 0, TH_SYN, 0, 4243240233Sglebius (*state)->src.mss, 0, 0, (*state)->tag, NULL); 4244200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4245200930Sdelphij return (PF_SYNPROXY_DROP); 4246200930Sdelphij } else if (((th->th_flags & (TH_SYN|TH_ACK)) != 4247200930Sdelphij (TH_SYN|TH_ACK)) || 4248200930Sdelphij (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) { 4249200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4250200930Sdelphij return (PF_DROP); 4251200930Sdelphij } else { 4252200930Sdelphij (*state)->dst.max_win = MAX(ntohs(th->th_win), 1); 4253200930Sdelphij (*state)->dst.seqlo = ntohl(th->th_seq); 4254200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4255200930Sdelphij pd->src, th->th_dport, th->th_sport, 4256200930Sdelphij ntohl(th->th_ack), ntohl(th->th_seq) + 1, 4257200930Sdelphij TH_ACK, (*state)->src.max_win, 0, 0, 0, 4258240233Sglebius (*state)->tag, NULL); 4259200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4260223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4261223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4262200930Sdelphij (*state)->src.seqhi + 1, (*state)->src.seqlo + 1, 4263240233Sglebius TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL); 4264200930Sdelphij (*state)->src.seqdiff = (*state)->dst.seqhi - 4265200930Sdelphij (*state)->src.seqlo; 4266200930Sdelphij (*state)->dst.seqdiff = (*state)->src.seqhi - 4267200930Sdelphij (*state)->dst.seqlo; 4268200930Sdelphij (*state)->src.seqhi = (*state)->src.seqlo + 4269200930Sdelphij (*state)->dst.max_win; 4270200930Sdelphij (*state)->dst.seqhi = (*state)->dst.seqlo + 4271200930Sdelphij (*state)->src.max_win; 4272200930Sdelphij (*state)->src.wscale = (*state)->dst.wscale = 0; 4273200930Sdelphij (*state)->src.state = (*state)->dst.state = 4274200930Sdelphij TCPS_ESTABLISHED; 4275200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4276200930Sdelphij return (PF_SYNPROXY_DROP); 4277200930Sdelphij } 4278200930Sdelphij } 4279200930Sdelphij 4280200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) && 4281200930Sdelphij dst->state >= TCPS_FIN_WAIT_2 && 4282200930Sdelphij src->state >= TCPS_FIN_WAIT_2) { 4283223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4284200930Sdelphij printf("pf: state reuse "); 4285200930Sdelphij pf_print_state(*state); 4286200930Sdelphij pf_print_flags(th->th_flags); 4287200930Sdelphij printf("\n"); 4288200930Sdelphij } 4289200930Sdelphij /* XXX make sure it's the same direction ?? */ 4290200930Sdelphij (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 4291240233Sglebius pf_unlink_state(*state, PF_ENTER_LOCKED); 4292200930Sdelphij *state = NULL; 4293200930Sdelphij return (PF_DROP); 4294200930Sdelphij } 4295200930Sdelphij 4296200930Sdelphij if ((*state)->state_flags & PFSTATE_SLOPPY) { 4297200930Sdelphij if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP) 4298200930Sdelphij return (PF_DROP); 4299200930Sdelphij } else { 4300200930Sdelphij if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason, 4301200930Sdelphij ©back) == PF_DROP) 4302200930Sdelphij return (PF_DROP); 4303200930Sdelphij } 4304200930Sdelphij 4305126258Smlaier /* translate source/destination address, if necessary */ 4306223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4307223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4308223637Sbz 4309223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4310223637Sbz nk->port[pd->sidx] != th->th_sport) 4311126258Smlaier pf_change_ap(pd->src, &th->th_sport, pd->ip_sum, 4312223637Sbz &th->th_sum, &nk->addr[pd->sidx], 4313223637Sbz nk->port[pd->sidx], 0, pd->af); 4314223637Sbz 4315223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4316223637Sbz nk->port[pd->didx] != th->th_dport) 4317126258Smlaier pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum, 4318223637Sbz &th->th_sum, &nk->addr[pd->didx], 4319223637Sbz nk->port[pd->didx], 0, pd->af); 4320223637Sbz copyback = 1; 4321126258Smlaier } 4322126258Smlaier 4323223637Sbz /* Copyback sequence modulation or stateful scrub changes if needed */ 4324223637Sbz if (copyback) 4325223637Sbz m_copyback(m, off, sizeof(*th), (caddr_t)th); 4326223637Sbz 4327126258Smlaier return (PF_PASS); 4328126258Smlaier} 4329126258Smlaier 4330240233Sglebiusstatic int 4331130613Smlaierpf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif, 4332130613Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd) 4333126258Smlaier{ 4334126258Smlaier struct pf_state_peer *src, *dst; 4335223637Sbz struct pf_state_key_cmp key; 4336126258Smlaier struct udphdr *uh = pd->hdr.udp; 4337126258Smlaier 4338240233Sglebius bzero(&key, sizeof(key)); 4339126258Smlaier key.af = pd->af; 4340126258Smlaier key.proto = IPPROTO_UDP; 4341223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4342223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4343223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4344223637Sbz key.port[0] = uh->uh_sport; 4345223637Sbz key.port[1] = uh->uh_dport; 4346223637Sbz } else { /* stack side, reverse */ 4347223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4348223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4349223637Sbz key.port[1] = uh->uh_sport; 4350223637Sbz key.port[0] = uh->uh_dport; 4351130613Smlaier } 4352126258Smlaier 4353240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4354126258Smlaier 4355126258Smlaier if (direction == (*state)->direction) { 4356126258Smlaier src = &(*state)->src; 4357126258Smlaier dst = &(*state)->dst; 4358126258Smlaier } else { 4359126258Smlaier src = &(*state)->dst; 4360126258Smlaier dst = &(*state)->src; 4361126258Smlaier } 4362126258Smlaier 4363126258Smlaier /* update states */ 4364126258Smlaier if (src->state < PFUDPS_SINGLE) 4365126258Smlaier src->state = PFUDPS_SINGLE; 4366126258Smlaier if (dst->state == PFUDPS_SINGLE) 4367126258Smlaier dst->state = PFUDPS_MULTIPLE; 4368126258Smlaier 4369126258Smlaier /* update expire time */ 4370240233Sglebius (*state)->expire = time_uptime; 4371126258Smlaier if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 4372126258Smlaier (*state)->timeout = PFTM_UDP_MULTIPLE; 4373126258Smlaier else 4374126258Smlaier (*state)->timeout = PFTM_UDP_SINGLE; 4375126258Smlaier 4376126258Smlaier /* translate source/destination address, if necessary */ 4377223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4378223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4379223637Sbz 4380223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4381223637Sbz nk->port[pd->sidx] != uh->uh_sport) 4382126258Smlaier pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum, 4383223637Sbz &uh->uh_sum, &nk->addr[pd->sidx], 4384223637Sbz nk->port[pd->sidx], 1, pd->af); 4385223637Sbz 4386223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4387223637Sbz nk->port[pd->didx] != uh->uh_dport) 4388126258Smlaier pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum, 4389223637Sbz &uh->uh_sum, &nk->addr[pd->didx], 4390223637Sbz nk->port[pd->didx], 1, pd->af); 4391126261Smlaier m_copyback(m, off, sizeof(*uh), (caddr_t)uh); 4392126258Smlaier } 4393126258Smlaier 4394126258Smlaier return (PF_PASS); 4395126258Smlaier} 4396126258Smlaier 4397240233Sglebiusstatic int 4398130613Smlaierpf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif, 4399145836Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason) 4400126258Smlaier{ 4401223637Sbz struct pf_addr *saddr = pd->src, *daddr = pd->dst; 4402223637Sbz u_int16_t icmpid = 0, *icmpsum; 4403223637Sbz u_int8_t icmptype; 4404130613Smlaier int state_icmp = 0; 4405223637Sbz struct pf_state_key_cmp key; 4406126258Smlaier 4407240233Sglebius bzero(&key, sizeof(key)); 4408126258Smlaier switch (pd->proto) { 4409126258Smlaier#ifdef INET 4410126258Smlaier case IPPROTO_ICMP: 4411126258Smlaier icmptype = pd->hdr.icmp->icmp_type; 4412126258Smlaier icmpid = pd->hdr.icmp->icmp_id; 4413126258Smlaier icmpsum = &pd->hdr.icmp->icmp_cksum; 4414126258Smlaier 4415126258Smlaier if (icmptype == ICMP_UNREACH || 4416126258Smlaier icmptype == ICMP_SOURCEQUENCH || 4417126258Smlaier icmptype == ICMP_REDIRECT || 4418126258Smlaier icmptype == ICMP_TIMXCEED || 4419126258Smlaier icmptype == ICMP_PARAMPROB) 4420126258Smlaier state_icmp++; 4421126258Smlaier break; 4422126258Smlaier#endif /* INET */ 4423126258Smlaier#ifdef INET6 4424126258Smlaier case IPPROTO_ICMPV6: 4425126258Smlaier icmptype = pd->hdr.icmp6->icmp6_type; 4426126258Smlaier icmpid = pd->hdr.icmp6->icmp6_id; 4427126258Smlaier icmpsum = &pd->hdr.icmp6->icmp6_cksum; 4428126258Smlaier 4429126258Smlaier if (icmptype == ICMP6_DST_UNREACH || 4430126258Smlaier icmptype == ICMP6_PACKET_TOO_BIG || 4431126258Smlaier icmptype == ICMP6_TIME_EXCEEDED || 4432126258Smlaier icmptype == ICMP6_PARAM_PROB) 4433126258Smlaier state_icmp++; 4434126258Smlaier break; 4435126258Smlaier#endif /* INET6 */ 4436126258Smlaier } 4437126258Smlaier 4438126258Smlaier if (!state_icmp) { 4439126258Smlaier 4440126258Smlaier /* 4441126258Smlaier * ICMP query/reply message not related to a TCP/UDP packet. 4442126258Smlaier * Search for an ICMP state. 4443126258Smlaier */ 4444126258Smlaier key.af = pd->af; 4445126258Smlaier key.proto = pd->proto; 4446223637Sbz key.port[0] = key.port[1] = icmpid; 4447223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4448223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4449223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4450223637Sbz } else { /* stack side, reverse */ 4451223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4452223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4453130613Smlaier } 4454126258Smlaier 4455240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4456126258Smlaier 4457240233Sglebius (*state)->expire = time_uptime; 4458126258Smlaier (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 4459126258Smlaier 4460126258Smlaier /* translate source/destination address, if necessary */ 4461223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4462223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4463223637Sbz 4464223637Sbz switch (pd->af) { 4465126258Smlaier#ifdef INET 4466223637Sbz case AF_INET: 4467223637Sbz if (PF_ANEQ(pd->src, 4468223637Sbz &nk->addr[pd->sidx], AF_INET)) 4469126258Smlaier pf_change_a(&saddr->v4.s_addr, 4470126258Smlaier pd->ip_sum, 4471223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 4472223637Sbz 4473223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], 4474223637Sbz AF_INET)) 4475223637Sbz pf_change_a(&daddr->v4.s_addr, 4476223637Sbz pd->ip_sum, 4477223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 4478223637Sbz 4479223637Sbz if (nk->port[0] != 4480223637Sbz pd->hdr.icmp->icmp_id) { 4481149884Smlaier pd->hdr.icmp->icmp_cksum = 4482149884Smlaier pf_cksum_fixup( 4483149884Smlaier pd->hdr.icmp->icmp_cksum, icmpid, 4484223637Sbz nk->port[pd->sidx], 0); 4485149884Smlaier pd->hdr.icmp->icmp_id = 4486223637Sbz nk->port[pd->sidx]; 4487223637Sbz } 4488223637Sbz 4489223637Sbz m_copyback(m, off, ICMP_MINLEN, 4490240233Sglebius (caddr_t )pd->hdr.icmp); 4491223637Sbz break; 4492126258Smlaier#endif /* INET */ 4493126258Smlaier#ifdef INET6 4494223637Sbz case AF_INET6: 4495223637Sbz if (PF_ANEQ(pd->src, 4496223637Sbz &nk->addr[pd->sidx], AF_INET6)) 4497126258Smlaier pf_change_a6(saddr, 4498126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4499223637Sbz &nk->addr[pd->sidx], 0); 4500223637Sbz 4501223637Sbz if (PF_ANEQ(pd->dst, 4502223637Sbz &nk->addr[pd->didx], AF_INET6)) 4503126258Smlaier pf_change_a6(daddr, 4504126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4505223637Sbz &nk->addr[pd->didx], 0); 4506223637Sbz 4507240233Sglebius m_copyback(m, off, sizeof(struct icmp6_hdr), 4508240233Sglebius (caddr_t )pd->hdr.icmp6); 4509223637Sbz break; 4510126258Smlaier#endif /* INET6 */ 4511126258Smlaier } 4512126258Smlaier } 4513126258Smlaier return (PF_PASS); 4514126258Smlaier 4515126258Smlaier } else { 4516126258Smlaier /* 4517126258Smlaier * ICMP error message in response to a TCP/UDP packet. 4518126258Smlaier * Extract the inner TCP/UDP header and search for that state. 4519126258Smlaier */ 4520126258Smlaier 4521126258Smlaier struct pf_pdesc pd2; 4522223637Sbz bzero(&pd2, sizeof pd2); 4523126258Smlaier#ifdef INET 4524126258Smlaier struct ip h2; 4525126258Smlaier#endif /* INET */ 4526126258Smlaier#ifdef INET6 4527126258Smlaier struct ip6_hdr h2_6; 4528126258Smlaier int terminal = 0; 4529126258Smlaier#endif /* INET6 */ 4530223637Sbz int ipoff2 = 0; 4531223637Sbz int off2 = 0; 4532126258Smlaier 4533126258Smlaier pd2.af = pd->af; 4534223637Sbz /* Payload packet is from the opposite direction. */ 4535223637Sbz pd2.sidx = (direction == PF_IN) ? 1 : 0; 4536223637Sbz pd2.didx = (direction == PF_IN) ? 0 : 1; 4537126258Smlaier switch (pd->af) { 4538126258Smlaier#ifdef INET 4539126258Smlaier case AF_INET: 4540126258Smlaier /* offset of h2 in mbuf chain */ 4541126258Smlaier ipoff2 = off + ICMP_MINLEN; 4542126258Smlaier 4543126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2), 4544145836Smlaier NULL, reason, pd2.af)) { 4545126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4546126258Smlaier ("pf: ICMP error message too short " 4547126258Smlaier "(ip)\n")); 4548126258Smlaier return (PF_DROP); 4549126258Smlaier } 4550126258Smlaier /* 4551126258Smlaier * ICMP error messages don't refer to non-first 4552126258Smlaier * fragments 4553126258Smlaier */ 4554145836Smlaier if (h2.ip_off & htons(IP_OFFMASK)) { 4555145836Smlaier REASON_SET(reason, PFRES_FRAG); 4556126258Smlaier return (PF_DROP); 4557145836Smlaier } 4558126258Smlaier 4559126258Smlaier /* offset of protocol header that follows h2 */ 4560126258Smlaier off2 = ipoff2 + (h2.ip_hl << 2); 4561126258Smlaier 4562126258Smlaier pd2.proto = h2.ip_p; 4563126258Smlaier pd2.src = (struct pf_addr *)&h2.ip_src; 4564126258Smlaier pd2.dst = (struct pf_addr *)&h2.ip_dst; 4565126258Smlaier pd2.ip_sum = &h2.ip_sum; 4566126258Smlaier break; 4567126258Smlaier#endif /* INET */ 4568126258Smlaier#ifdef INET6 4569126258Smlaier case AF_INET6: 4570126258Smlaier ipoff2 = off + sizeof(struct icmp6_hdr); 4571126258Smlaier 4572126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6), 4573145836Smlaier NULL, reason, pd2.af)) { 4574126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4575126258Smlaier ("pf: ICMP error message too short " 4576126258Smlaier "(ip6)\n")); 4577126258Smlaier return (PF_DROP); 4578126258Smlaier } 4579126258Smlaier pd2.proto = h2_6.ip6_nxt; 4580126258Smlaier pd2.src = (struct pf_addr *)&h2_6.ip6_src; 4581126258Smlaier pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 4582126258Smlaier pd2.ip_sum = NULL; 4583126258Smlaier off2 = ipoff2 + sizeof(h2_6); 4584126258Smlaier do { 4585126258Smlaier switch (pd2.proto) { 4586126258Smlaier case IPPROTO_FRAGMENT: 4587126258Smlaier /* 4588126258Smlaier * ICMPv6 error messages for 4589126258Smlaier * non-first fragments 4590126258Smlaier */ 4591145836Smlaier REASON_SET(reason, PFRES_FRAG); 4592126258Smlaier return (PF_DROP); 4593126258Smlaier case IPPROTO_AH: 4594126258Smlaier case IPPROTO_HOPOPTS: 4595126258Smlaier case IPPROTO_ROUTING: 4596126258Smlaier case IPPROTO_DSTOPTS: { 4597126258Smlaier /* get next header and header length */ 4598126258Smlaier struct ip6_ext opt6; 4599126258Smlaier 4600126258Smlaier if (!pf_pull_hdr(m, off2, &opt6, 4601145836Smlaier sizeof(opt6), NULL, reason, 4602145836Smlaier pd2.af)) { 4603126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4604126258Smlaier ("pf: ICMPv6 short opt\n")); 4605126258Smlaier return (PF_DROP); 4606126258Smlaier } 4607126258Smlaier if (pd2.proto == IPPROTO_AH) 4608126258Smlaier off2 += (opt6.ip6e_len + 2) * 4; 4609126258Smlaier else 4610126258Smlaier off2 += (opt6.ip6e_len + 1) * 8; 4611126258Smlaier pd2.proto = opt6.ip6e_nxt; 4612126258Smlaier /* goto the next header */ 4613126258Smlaier break; 4614126258Smlaier } 4615126258Smlaier default: 4616126258Smlaier terminal++; 4617126258Smlaier break; 4618126258Smlaier } 4619126258Smlaier } while (!terminal); 4620126258Smlaier break; 4621126258Smlaier#endif /* INET6 */ 4622126258Smlaier } 4623126258Smlaier 4624126258Smlaier switch (pd2.proto) { 4625126258Smlaier case IPPROTO_TCP: { 4626126258Smlaier struct tcphdr th; 4627126258Smlaier u_int32_t seq; 4628126258Smlaier struct pf_state_peer *src, *dst; 4629126258Smlaier u_int8_t dws; 4630128129Smlaier int copyback = 0; 4631126258Smlaier 4632126258Smlaier /* 4633126258Smlaier * Only the first 8 bytes of the TCP header can be 4634126258Smlaier * expected. Don't access any TCP header fields after 4635126258Smlaier * th_seq, an ackskew test is not possible. 4636126258Smlaier */ 4637145836Smlaier if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason, 4638145836Smlaier pd2.af)) { 4639126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4640126258Smlaier ("pf: ICMP error message too short " 4641126258Smlaier "(tcp)\n")); 4642126258Smlaier return (PF_DROP); 4643126258Smlaier } 4644126258Smlaier 4645126258Smlaier key.af = pd2.af; 4646126258Smlaier key.proto = IPPROTO_TCP; 4647223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4648223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4649223637Sbz key.port[pd2.sidx] = th.th_sport; 4650223637Sbz key.port[pd2.didx] = th.th_dport; 4651126258Smlaier 4652240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4653126258Smlaier 4654126258Smlaier if (direction == (*state)->direction) { 4655126258Smlaier src = &(*state)->dst; 4656126258Smlaier dst = &(*state)->src; 4657126258Smlaier } else { 4658126258Smlaier src = &(*state)->src; 4659126258Smlaier dst = &(*state)->dst; 4660126258Smlaier } 4661126258Smlaier 4662171929Sdhartmei if (src->wscale && dst->wscale) 4663126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 4664126258Smlaier else 4665126258Smlaier dws = 0; 4666126258Smlaier 4667126258Smlaier /* Demodulate sequence number */ 4668126258Smlaier seq = ntohl(th.th_seq) - src->seqdiff; 4669128129Smlaier if (src->seqdiff) { 4670128129Smlaier pf_change_a(&th.th_seq, icmpsum, 4671126258Smlaier htonl(seq), 0); 4672128129Smlaier copyback = 1; 4673128129Smlaier } 4674126258Smlaier 4675200930Sdelphij if (!((*state)->state_flags & PFSTATE_SLOPPY) && 4676200930Sdelphij (!SEQ_GEQ(src->seqhi, seq) || 4677200930Sdelphij !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 4678223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4679126258Smlaier printf("pf: BAD ICMP %d:%d ", 4680126258Smlaier icmptype, pd->hdr.icmp->icmp_code); 4681126258Smlaier pf_print_host(pd->src, 0, pd->af); 4682126258Smlaier printf(" -> "); 4683126258Smlaier pf_print_host(pd->dst, 0, pd->af); 4684126258Smlaier printf(" state: "); 4685126258Smlaier pf_print_state(*state); 4686126258Smlaier printf(" seq=%u\n", seq); 4687126258Smlaier } 4688145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4689126258Smlaier return (PF_DROP); 4690223637Sbz } else { 4691223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4692223637Sbz printf("pf: OK ICMP %d:%d ", 4693223637Sbz icmptype, pd->hdr.icmp->icmp_code); 4694223637Sbz pf_print_host(pd->src, 0, pd->af); 4695223637Sbz printf(" -> "); 4696223637Sbz pf_print_host(pd->dst, 0, pd->af); 4697223637Sbz printf(" state: "); 4698223637Sbz pf_print_state(*state); 4699223637Sbz printf(" seq=%u\n", seq); 4700223637Sbz } 4701126258Smlaier } 4702126258Smlaier 4703223637Sbz /* translate source/destination address, if necessary */ 4704223637Sbz if ((*state)->key[PF_SK_WIRE] != 4705223637Sbz (*state)->key[PF_SK_STACK]) { 4706223637Sbz struct pf_state_key *nk = 4707223637Sbz (*state)->key[pd->didx]; 4708223637Sbz 4709223637Sbz if (PF_ANEQ(pd2.src, 4710223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4711223637Sbz nk->port[pd2.sidx] != th.th_sport) 4712126258Smlaier pf_change_icmp(pd2.src, &th.th_sport, 4713223637Sbz daddr, &nk->addr[pd2.sidx], 4714223637Sbz nk->port[pd2.sidx], NULL, 4715126258Smlaier pd2.ip_sum, icmpsum, 4716126258Smlaier pd->ip_sum, 0, pd2.af); 4717223637Sbz 4718223637Sbz if (PF_ANEQ(pd2.dst, 4719223637Sbz &nk->addr[pd2.didx], pd2.af) || 4720223637Sbz nk->port[pd2.didx] != th.th_dport) 4721126258Smlaier pf_change_icmp(pd2.dst, &th.th_dport, 4722223637Sbz NULL, /* XXX Inbound NAT? */ 4723223637Sbz &nk->addr[pd2.didx], 4724223637Sbz nk->port[pd2.didx], NULL, 4725126258Smlaier pd2.ip_sum, icmpsum, 4726126258Smlaier pd->ip_sum, 0, pd2.af); 4727128129Smlaier copyback = 1; 4728128129Smlaier } 4729128129Smlaier 4730128129Smlaier if (copyback) { 4731126258Smlaier switch (pd2.af) { 4732126258Smlaier#ifdef INET 4733126258Smlaier case AF_INET: 4734126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4735240233Sglebius (caddr_t )pd->hdr.icmp); 4736126258Smlaier m_copyback(m, ipoff2, sizeof(h2), 4737240233Sglebius (caddr_t )&h2); 4738126258Smlaier break; 4739126258Smlaier#endif /* INET */ 4740126258Smlaier#ifdef INET6 4741126258Smlaier case AF_INET6: 4742126258Smlaier m_copyback(m, off, 4743126258Smlaier sizeof(struct icmp6_hdr), 4744240233Sglebius (caddr_t )pd->hdr.icmp6); 4745126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4746240233Sglebius (caddr_t )&h2_6); 4747126258Smlaier break; 4748126258Smlaier#endif /* INET6 */ 4749126258Smlaier } 4750126261Smlaier m_copyback(m, off2, 8, (caddr_t)&th); 4751126258Smlaier } 4752126258Smlaier 4753126258Smlaier return (PF_PASS); 4754126258Smlaier break; 4755126258Smlaier } 4756126258Smlaier case IPPROTO_UDP: { 4757126258Smlaier struct udphdr uh; 4758126258Smlaier 4759126258Smlaier if (!pf_pull_hdr(m, off2, &uh, sizeof(uh), 4760145836Smlaier NULL, reason, pd2.af)) { 4761126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4762126258Smlaier ("pf: ICMP error message too short " 4763126258Smlaier "(udp)\n")); 4764126258Smlaier return (PF_DROP); 4765126258Smlaier } 4766126258Smlaier 4767126258Smlaier key.af = pd2.af; 4768126258Smlaier key.proto = IPPROTO_UDP; 4769223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4770223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4771223637Sbz key.port[pd2.sidx] = uh.uh_sport; 4772223637Sbz key.port[pd2.didx] = uh.uh_dport; 4773126258Smlaier 4774240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4775126258Smlaier 4776223637Sbz /* translate source/destination address, if necessary */ 4777223637Sbz if ((*state)->key[PF_SK_WIRE] != 4778223637Sbz (*state)->key[PF_SK_STACK]) { 4779223637Sbz struct pf_state_key *nk = 4780223637Sbz (*state)->key[pd->didx]; 4781223637Sbz 4782223637Sbz if (PF_ANEQ(pd2.src, 4783223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4784223637Sbz nk->port[pd2.sidx] != uh.uh_sport) 4785126258Smlaier pf_change_icmp(pd2.src, &uh.uh_sport, 4786223637Sbz daddr, &nk->addr[pd2.sidx], 4787223637Sbz nk->port[pd2.sidx], &uh.uh_sum, 4788126258Smlaier pd2.ip_sum, icmpsum, 4789126258Smlaier pd->ip_sum, 1, pd2.af); 4790223637Sbz 4791223637Sbz if (PF_ANEQ(pd2.dst, 4792223637Sbz &nk->addr[pd2.didx], pd2.af) || 4793223637Sbz nk->port[pd2.didx] != uh.uh_dport) 4794126258Smlaier pf_change_icmp(pd2.dst, &uh.uh_dport, 4795223637Sbz NULL, /* XXX Inbound NAT? */ 4796223637Sbz &nk->addr[pd2.didx], 4797223637Sbz nk->port[pd2.didx], &uh.uh_sum, 4798126258Smlaier pd2.ip_sum, icmpsum, 4799126258Smlaier pd->ip_sum, 1, pd2.af); 4800223637Sbz 4801126258Smlaier switch (pd2.af) { 4802126258Smlaier#ifdef INET 4803126258Smlaier case AF_INET: 4804126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4805240233Sglebius (caddr_t )pd->hdr.icmp); 4806223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4807126258Smlaier break; 4808126258Smlaier#endif /* INET */ 4809126258Smlaier#ifdef INET6 4810126258Smlaier case AF_INET6: 4811126258Smlaier m_copyback(m, off, 4812126258Smlaier sizeof(struct icmp6_hdr), 4813240233Sglebius (caddr_t )pd->hdr.icmp6); 4814126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4815240233Sglebius (caddr_t )&h2_6); 4816126258Smlaier break; 4817126258Smlaier#endif /* INET6 */ 4818126258Smlaier } 4819223637Sbz m_copyback(m, off2, sizeof(uh), (caddr_t)&uh); 4820126258Smlaier } 4821126258Smlaier return (PF_PASS); 4822126258Smlaier break; 4823126258Smlaier } 4824126258Smlaier#ifdef INET 4825126258Smlaier case IPPROTO_ICMP: { 4826126258Smlaier struct icmp iih; 4827126258Smlaier 4828126258Smlaier if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN, 4829145836Smlaier NULL, reason, pd2.af)) { 4830126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4831126258Smlaier ("pf: ICMP error message too short i" 4832126258Smlaier "(icmp)\n")); 4833126258Smlaier return (PF_DROP); 4834126258Smlaier } 4835126258Smlaier 4836126258Smlaier key.af = pd2.af; 4837126258Smlaier key.proto = IPPROTO_ICMP; 4838223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4839223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4840223637Sbz key.port[0] = key.port[1] = iih.icmp_id; 4841126258Smlaier 4842240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4843126258Smlaier 4844223637Sbz /* translate source/destination address, if necessary */ 4845223637Sbz if ((*state)->key[PF_SK_WIRE] != 4846223637Sbz (*state)->key[PF_SK_STACK]) { 4847223637Sbz struct pf_state_key *nk = 4848223637Sbz (*state)->key[pd->didx]; 4849223637Sbz 4850223637Sbz if (PF_ANEQ(pd2.src, 4851223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4852223637Sbz nk->port[pd2.sidx] != iih.icmp_id) 4853126258Smlaier pf_change_icmp(pd2.src, &iih.icmp_id, 4854223637Sbz daddr, &nk->addr[pd2.sidx], 4855223637Sbz nk->port[pd2.sidx], NULL, 4856126258Smlaier pd2.ip_sum, icmpsum, 4857126258Smlaier pd->ip_sum, 0, AF_INET); 4858223637Sbz 4859223637Sbz if (PF_ANEQ(pd2.dst, 4860223637Sbz &nk->addr[pd2.didx], pd2.af) || 4861223637Sbz nk->port[pd2.didx] != iih.icmp_id) 4862126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp_id, 4863223637Sbz NULL, /* XXX Inbound NAT? */ 4864223637Sbz &nk->addr[pd2.didx], 4865223637Sbz nk->port[pd2.didx], NULL, 4866126258Smlaier pd2.ip_sum, icmpsum, 4867126258Smlaier pd->ip_sum, 0, AF_INET); 4868223637Sbz 4869223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 4870223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4871223637Sbz m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih); 4872126258Smlaier } 4873126258Smlaier return (PF_PASS); 4874126258Smlaier break; 4875126258Smlaier } 4876126258Smlaier#endif /* INET */ 4877126258Smlaier#ifdef INET6 4878126258Smlaier case IPPROTO_ICMPV6: { 4879126258Smlaier struct icmp6_hdr iih; 4880126258Smlaier 4881126258Smlaier if (!pf_pull_hdr(m, off2, &iih, 4882145836Smlaier sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 4883126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4884126258Smlaier ("pf: ICMP error message too short " 4885126258Smlaier "(icmp6)\n")); 4886126258Smlaier return (PF_DROP); 4887126258Smlaier } 4888126258Smlaier 4889126258Smlaier key.af = pd2.af; 4890126258Smlaier key.proto = IPPROTO_ICMPV6; 4891223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4892223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4893223637Sbz key.port[0] = key.port[1] = iih.icmp6_id; 4894126258Smlaier 4895240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4896126258Smlaier 4897223637Sbz /* translate source/destination address, if necessary */ 4898223637Sbz if ((*state)->key[PF_SK_WIRE] != 4899223637Sbz (*state)->key[PF_SK_STACK]) { 4900223637Sbz struct pf_state_key *nk = 4901223637Sbz (*state)->key[pd->didx]; 4902223637Sbz 4903223637Sbz if (PF_ANEQ(pd2.src, 4904223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4905223637Sbz nk->port[pd2.sidx] != iih.icmp6_id) 4906126258Smlaier pf_change_icmp(pd2.src, &iih.icmp6_id, 4907223637Sbz daddr, &nk->addr[pd2.sidx], 4908223637Sbz nk->port[pd2.sidx], NULL, 4909126258Smlaier pd2.ip_sum, icmpsum, 4910126258Smlaier pd->ip_sum, 0, AF_INET6); 4911223637Sbz 4912223637Sbz if (PF_ANEQ(pd2.dst, 4913223637Sbz &nk->addr[pd2.didx], pd2.af) || 4914223637Sbz nk->port[pd2.didx] != iih.icmp6_id) 4915126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp6_id, 4916223637Sbz NULL, /* XXX Inbound NAT? */ 4917223637Sbz &nk->addr[pd2.didx], 4918223637Sbz nk->port[pd2.didx], NULL, 4919126258Smlaier pd2.ip_sum, icmpsum, 4920126258Smlaier pd->ip_sum, 0, AF_INET6); 4921223637Sbz 4922126258Smlaier m_copyback(m, off, sizeof(struct icmp6_hdr), 4923126261Smlaier (caddr_t)pd->hdr.icmp6); 4924223637Sbz m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 4925126258Smlaier m_copyback(m, off2, sizeof(struct icmp6_hdr), 4926126261Smlaier (caddr_t)&iih); 4927126258Smlaier } 4928126258Smlaier return (PF_PASS); 4929126258Smlaier break; 4930126258Smlaier } 4931126258Smlaier#endif /* INET6 */ 4932126258Smlaier default: { 4933126258Smlaier key.af = pd2.af; 4934126258Smlaier key.proto = pd2.proto; 4935223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4936223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4937223637Sbz key.port[0] = key.port[1] = 0; 4938126258Smlaier 4939240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4940126258Smlaier 4941223637Sbz /* translate source/destination address, if necessary */ 4942223637Sbz if ((*state)->key[PF_SK_WIRE] != 4943223637Sbz (*state)->key[PF_SK_STACK]) { 4944223637Sbz struct pf_state_key *nk = 4945223637Sbz (*state)->key[pd->didx]; 4946223637Sbz 4947223637Sbz if (PF_ANEQ(pd2.src, 4948223637Sbz &nk->addr[pd2.sidx], pd2.af)) 4949223637Sbz pf_change_icmp(pd2.src, NULL, daddr, 4950223637Sbz &nk->addr[pd2.sidx], 0, NULL, 4951126258Smlaier pd2.ip_sum, icmpsum, 4952126258Smlaier pd->ip_sum, 0, pd2.af); 4953223637Sbz 4954223637Sbz if (PF_ANEQ(pd2.dst, 4955223637Sbz &nk->addr[pd2.didx], pd2.af)) 4956223637Sbz pf_change_icmp(pd2.src, NULL, 4957223637Sbz NULL, /* XXX Inbound NAT? */ 4958223637Sbz &nk->addr[pd2.didx], 0, NULL, 4959126258Smlaier pd2.ip_sum, icmpsum, 4960126258Smlaier pd->ip_sum, 0, pd2.af); 4961223637Sbz 4962126258Smlaier switch (pd2.af) { 4963126258Smlaier#ifdef INET 4964126258Smlaier case AF_INET: 4965126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4966126261Smlaier (caddr_t)pd->hdr.icmp); 4967223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4968126258Smlaier break; 4969126258Smlaier#endif /* INET */ 4970126258Smlaier#ifdef INET6 4971126258Smlaier case AF_INET6: 4972126258Smlaier m_copyback(m, off, 4973126258Smlaier sizeof(struct icmp6_hdr), 4974240233Sglebius (caddr_t )pd->hdr.icmp6); 4975126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4976240233Sglebius (caddr_t )&h2_6); 4977126258Smlaier break; 4978126258Smlaier#endif /* INET6 */ 4979126258Smlaier } 4980126258Smlaier } 4981126258Smlaier return (PF_PASS); 4982126258Smlaier break; 4983126258Smlaier } 4984126258Smlaier } 4985126258Smlaier } 4986126258Smlaier} 4987126258Smlaier 4988240233Sglebiusstatic int 4989130613Smlaierpf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif, 4990223637Sbz struct mbuf *m, struct pf_pdesc *pd) 4991126258Smlaier{ 4992126258Smlaier struct pf_state_peer *src, *dst; 4993223637Sbz struct pf_state_key_cmp key; 4994126258Smlaier 4995240233Sglebius bzero(&key, sizeof(key)); 4996126258Smlaier key.af = pd->af; 4997126258Smlaier key.proto = pd->proto; 4998130613Smlaier if (direction == PF_IN) { 4999223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 5000223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 5001223637Sbz key.port[0] = key.port[1] = 0; 5002130613Smlaier } else { 5003223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 5004223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 5005223637Sbz key.port[1] = key.port[0] = 0; 5006130613Smlaier } 5007126258Smlaier 5008240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 5009126258Smlaier 5010126258Smlaier if (direction == (*state)->direction) { 5011126258Smlaier src = &(*state)->src; 5012126258Smlaier dst = &(*state)->dst; 5013126258Smlaier } else { 5014126258Smlaier src = &(*state)->dst; 5015126258Smlaier dst = &(*state)->src; 5016126258Smlaier } 5017126258Smlaier 5018126258Smlaier /* update states */ 5019126258Smlaier if (src->state < PFOTHERS_SINGLE) 5020126258Smlaier src->state = PFOTHERS_SINGLE; 5021126258Smlaier if (dst->state == PFOTHERS_SINGLE) 5022126258Smlaier dst->state = PFOTHERS_MULTIPLE; 5023126258Smlaier 5024126258Smlaier /* update expire time */ 5025240233Sglebius (*state)->expire = time_uptime; 5026126258Smlaier if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 5027126258Smlaier (*state)->timeout = PFTM_OTHER_MULTIPLE; 5028126258Smlaier else 5029126258Smlaier (*state)->timeout = PFTM_OTHER_SINGLE; 5030126258Smlaier 5031126258Smlaier /* translate source/destination address, if necessary */ 5032223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 5033223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 5034223637Sbz 5035240233Sglebius KASSERT(nk, ("%s: nk is null", __func__)); 5036240233Sglebius KASSERT(pd, ("%s: pd is null", __func__)); 5037240233Sglebius KASSERT(pd->src, ("%s: pd->src is null", __func__)); 5038240233Sglebius KASSERT(pd->dst, ("%s: pd->dst is null", __func__)); 5039223637Sbz switch (pd->af) { 5040126258Smlaier#ifdef INET 5041223637Sbz case AF_INET: 5042223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5043126258Smlaier pf_change_a(&pd->src->v4.s_addr, 5044223637Sbz pd->ip_sum, 5045223637Sbz nk->addr[pd->sidx].v4.s_addr, 5046126258Smlaier 0); 5047223637Sbz 5048223637Sbz 5049223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5050126258Smlaier pf_change_a(&pd->dst->v4.s_addr, 5051223637Sbz pd->ip_sum, 5052223637Sbz nk->addr[pd->didx].v4.s_addr, 5053126258Smlaier 0); 5054223637Sbz 5055126258Smlaier break; 5056126258Smlaier#endif /* INET */ 5057126258Smlaier#ifdef INET6 5058223637Sbz case AF_INET6: 5059223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5060223637Sbz PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af); 5061223637Sbz 5062223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5063223637Sbz PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af); 5064126258Smlaier#endif /* INET6 */ 5065223637Sbz } 5066126258Smlaier } 5067126258Smlaier return (PF_PASS); 5068126258Smlaier} 5069126258Smlaier 5070126258Smlaier/* 5071126258Smlaier * ipoff and off are measured from the start of the mbuf chain. 5072126258Smlaier * h must be at "ipoff" on the mbuf chain. 5073126258Smlaier */ 5074126258Smlaiervoid * 5075126258Smlaierpf_pull_hdr(struct mbuf *m, int off, void *p, int len, 5076126258Smlaier u_short *actionp, u_short *reasonp, sa_family_t af) 5077126258Smlaier{ 5078126258Smlaier switch (af) { 5079126258Smlaier#ifdef INET 5080126258Smlaier case AF_INET: { 5081126258Smlaier struct ip *h = mtod(m, struct ip *); 5082126258Smlaier u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 5083126258Smlaier 5084126258Smlaier if (fragoff) { 5085126258Smlaier if (fragoff >= len) 5086126258Smlaier ACTION_SET(actionp, PF_PASS); 5087126258Smlaier else { 5088126258Smlaier ACTION_SET(actionp, PF_DROP); 5089126258Smlaier REASON_SET(reasonp, PFRES_FRAG); 5090126258Smlaier } 5091126258Smlaier return (NULL); 5092126258Smlaier } 5093130613Smlaier if (m->m_pkthdr.len < off + len || 5094130613Smlaier ntohs(h->ip_len) < off + len) { 5095126258Smlaier ACTION_SET(actionp, PF_DROP); 5096126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5097126258Smlaier return (NULL); 5098126258Smlaier } 5099126258Smlaier break; 5100126258Smlaier } 5101126258Smlaier#endif /* INET */ 5102126258Smlaier#ifdef INET6 5103126258Smlaier case AF_INET6: { 5104126258Smlaier struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 5105126258Smlaier 5106126258Smlaier if (m->m_pkthdr.len < off + len || 5107126258Smlaier (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) < 5108126258Smlaier (unsigned)(off + len)) { 5109126258Smlaier ACTION_SET(actionp, PF_DROP); 5110126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5111126258Smlaier return (NULL); 5112126258Smlaier } 5113126258Smlaier break; 5114126258Smlaier } 5115126258Smlaier#endif /* INET6 */ 5116126258Smlaier } 5117126258Smlaier m_copydata(m, off, len, p); 5118126258Smlaier return (p); 5119126258Smlaier} 5120126258Smlaier 5121126258Smlaierint 5122231852Sbzpf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif, 5123231852Sbz int rtableid) 5124126258Smlaier{ 5125223637Sbz#ifdef RADIX_MPATH 5126223637Sbz struct radix_node_head *rnh; 5127223637Sbz#endif 5128126258Smlaier struct sockaddr_in *dst; 5129171168Smlaier int ret = 1; 5130171168Smlaier int check_mpath; 5131145836Smlaier#ifdef INET6 5132145836Smlaier struct sockaddr_in6 *dst6; 5133145836Smlaier struct route_in6 ro; 5134145836Smlaier#else 5135126258Smlaier struct route ro; 5136145836Smlaier#endif 5137171168Smlaier struct radix_node *rn; 5138171168Smlaier struct rtentry *rt; 5139171168Smlaier struct ifnet *ifp; 5140126258Smlaier 5141171168Smlaier check_mpath = 0; 5142223637Sbz#ifdef RADIX_MPATH 5143223637Sbz /* XXX: stick to table 0 for now */ 5144223637Sbz rnh = rt_tables_get_rnh(0, af); 5145223637Sbz if (rnh != NULL && rn_mpath_capable(rnh)) 5146223637Sbz check_mpath = 1; 5147223637Sbz#endif 5148126258Smlaier bzero(&ro, sizeof(ro)); 5149145836Smlaier switch (af) { 5150145836Smlaier case AF_INET: 5151145836Smlaier dst = satosin(&ro.ro_dst); 5152145836Smlaier dst->sin_family = AF_INET; 5153145836Smlaier dst->sin_len = sizeof(*dst); 5154145836Smlaier dst->sin_addr = addr->v4; 5155145836Smlaier break; 5156145836Smlaier#ifdef INET6 5157145836Smlaier case AF_INET6: 5158223637Sbz /* 5159223637Sbz * Skip check for addresses with embedded interface scope, 5160223637Sbz * as they would always match anyway. 5161223637Sbz */ 5162223637Sbz if (IN6_IS_SCOPE_EMBED(&addr->v6)) 5163223637Sbz goto out; 5164145836Smlaier dst6 = (struct sockaddr_in6 *)&ro.ro_dst; 5165145836Smlaier dst6->sin6_family = AF_INET6; 5166145836Smlaier dst6->sin6_len = sizeof(*dst6); 5167145836Smlaier dst6->sin6_addr = addr->v6; 5168145836Smlaier break; 5169145836Smlaier#endif /* INET6 */ 5170145836Smlaier default: 5171145836Smlaier return (0); 5172145836Smlaier } 5173145836Smlaier 5174171168Smlaier /* Skip checks for ipsec interfaces */ 5175171168Smlaier if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 5176171168Smlaier goto out; 5177171168Smlaier 5178231852Sbz switch (af) { 5179231852Sbz#ifdef INET6 5180231852Sbz case AF_INET6: 5181231852Sbz in6_rtalloc_ign(&ro, 0, rtableid); 5182231852Sbz break; 5183231852Sbz#endif 5184222529Sbz#ifdef INET 5185231852Sbz case AF_INET: 5186231852Sbz in_rtalloc_ign((struct route *)&ro, 0, rtableid); 5187231852Sbz break; 5188222529Sbz#endif 5189231852Sbz default: 5190231852Sbz rtalloc_ign((struct route *)&ro, 0); /* No/default FIB. */ 5191231852Sbz break; 5192231852Sbz } 5193126258Smlaier 5194126258Smlaier if (ro.ro_rt != NULL) { 5195171168Smlaier /* No interface given, this is a no-route check */ 5196171168Smlaier if (kif == NULL) 5197171168Smlaier goto out; 5198171168Smlaier 5199171168Smlaier if (kif->pfik_ifp == NULL) { 5200171168Smlaier ret = 0; 5201171168Smlaier goto out; 5202171168Smlaier } 5203171168Smlaier 5204171168Smlaier /* Perform uRPF check if passed input interface */ 5205171168Smlaier ret = 0; 5206171168Smlaier rn = (struct radix_node *)ro.ro_rt; 5207171168Smlaier do { 5208171168Smlaier rt = (struct rtentry *)rn; 5209240233Sglebius ifp = rt->rt_ifp; 5210171168Smlaier 5211171168Smlaier if (kif->pfik_ifp == ifp) 5212171168Smlaier ret = 1; 5213223637Sbz#ifdef RADIX_MPATH 5214171168Smlaier rn = rn_mpath_next(rn); 5215171168Smlaier#endif 5216171168Smlaier } while (check_mpath == 1 && rn != NULL && ret == 0); 5217171168Smlaier } else 5218171168Smlaier ret = 0; 5219171168Smlaierout: 5220171168Smlaier if (ro.ro_rt != NULL) 5221126258Smlaier RTFREE(ro.ro_rt); 5222171168Smlaier return (ret); 5223145836Smlaier} 5224145836Smlaier 5225222529Sbz#ifdef INET 5226240233Sglebiusstatic void 5227126258Smlaierpf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5228171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5229126258Smlaier{ 5230126258Smlaier struct mbuf *m0, *m1; 5231240233Sglebius struct sockaddr_in dst; 5232126258Smlaier struct ip *ip; 5233126258Smlaier struct ifnet *ifp = NULL; 5234126258Smlaier struct pf_addr naddr; 5235130613Smlaier struct pf_src_node *sn = NULL; 5236126258Smlaier int error = 0; 5237242161Sglebius uint16_t ip_len, ip_off; 5238126258Smlaier 5239240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5240240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5241240233Sglebius __func__)); 5242126258Smlaier 5243240233Sglebius if ((pd->pf_mtag == NULL && 5244240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5245240233Sglebius pd->pf_mtag->routed++ > 3) { 5246171168Smlaier m0 = *m; 5247171168Smlaier *m = NULL; 5248240233Sglebius goto bad_locked; 5249132303Smlaier } 5250132303Smlaier 5251126258Smlaier if (r->rt == PF_DUPTO) { 5252240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5253240233Sglebius if (s) 5254240233Sglebius PF_STATE_UNLOCK(s); 5255126258Smlaier return; 5256240233Sglebius } 5257126258Smlaier } else { 5258240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5259240233Sglebius if (s) 5260240233Sglebius PF_STATE_UNLOCK(s); 5261126258Smlaier return; 5262240233Sglebius } 5263126258Smlaier m0 = *m; 5264126258Smlaier } 5265126258Smlaier 5266126258Smlaier ip = mtod(m0, struct ip *); 5267126258Smlaier 5268240233Sglebius bzero(&dst, sizeof(dst)); 5269240233Sglebius dst.sin_family = AF_INET; 5270240233Sglebius dst.sin_len = sizeof(dst); 5271240233Sglebius dst.sin_addr = ip->ip_dst; 5272126258Smlaier 5273126258Smlaier if (r->rt == PF_FASTROUTE) { 5274240233Sglebius struct rtentry *rt; 5275240233Sglebius 5276240233Sglebius if (s) 5277240233Sglebius PF_STATE_UNLOCK(s); 5278240233Sglebius rt = rtalloc1_fib(sintosa(&dst), 0, 0, M_GETFIB(m0)); 5279240233Sglebius if (rt == NULL) { 5280196039Srwatson KMOD_IPSTAT_INC(ips_noroute); 5281240233Sglebius error = EHOSTUNREACH; 5282126258Smlaier goto bad; 5283126258Smlaier } 5284126258Smlaier 5285240233Sglebius ifp = rt->rt_ifp; 5286263478Sglebius counter_u64_add(rt->rt_pksent, 1); 5287126258Smlaier 5288240233Sglebius if (rt->rt_flags & RTF_GATEWAY) 5289240233Sglebius bcopy(satosin(rt->rt_gateway), &dst, sizeof(dst)); 5290240233Sglebius RTFREE_LOCKED(rt); 5291126258Smlaier } else { 5292145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5293145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5294240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5295240233Sglebius goto bad_locked; 5296145836Smlaier } 5297126258Smlaier if (s == NULL) { 5298130613Smlaier pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src, 5299130613Smlaier &naddr, NULL, &sn); 5300126258Smlaier if (!PF_AZERO(&naddr, AF_INET)) 5301240233Sglebius dst.sin_addr.s_addr = naddr.v4.s_addr; 5302130613Smlaier ifp = r->rpool.cur->kif ? 5303130613Smlaier r->rpool.cur->kif->pfik_ifp : NULL; 5304126258Smlaier } else { 5305126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET)) 5306240233Sglebius dst.sin_addr.s_addr = 5307126258Smlaier s->rt_addr.v4.s_addr; 5308130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5309240233Sglebius PF_STATE_UNLOCK(s); 5310126258Smlaier } 5311126258Smlaier } 5312126258Smlaier if (ifp == NULL) 5313126258Smlaier goto bad; 5314126258Smlaier 5315130639Smlaier if (oifp != ifp) { 5316145836Smlaier if (pf_test(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5317126258Smlaier goto bad; 5318126258Smlaier else if (m0 == NULL) 5319126258Smlaier goto done; 5320145836Smlaier if (m0->m_len < sizeof(struct ip)) { 5321145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5322240233Sglebius ("%s: m0->m_len < sizeof(struct ip)\n", __func__)); 5323145836Smlaier goto bad; 5324145836Smlaier } 5325126258Smlaier ip = mtod(m0, struct ip *); 5326126258Smlaier } 5327126258Smlaier 5328240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5329240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5330240233Sglebius 5331241344Sglebius ip_len = ntohs(ip->ip_len); 5332241344Sglebius ip_off = ntohs(ip->ip_off); 5333240233Sglebius 5334240233Sglebius /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 5335126261Smlaier m0->m_pkthdr.csum_flags |= CSUM_IP; 5336242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 5337126261Smlaier in_delayed_cksum(m0); 5338242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 5339126261Smlaier } 5340240233Sglebius#ifdef SCTP 5341242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5342240233Sglebius sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 5343242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5344240233Sglebius } 5345240233Sglebius#endif 5346126261Smlaier 5347130613Smlaier /* 5348240233Sglebius * If small enough for interface, or the interface will take 5349240233Sglebius * care of the fragmentation for us, we can just send directly. 5350130613Smlaier */ 5351241344Sglebius if (ip_len <= ifp->if_mtu || 5352240233Sglebius (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 5353241344Sglebius ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 5354223637Sbz ip->ip_sum = 0; 5355242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 5356126258Smlaier ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 5357242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_IP; 5358242161Sglebius } 5359254523Sandre m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 5360240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5361126258Smlaier goto done; 5362126258Smlaier } 5363223637Sbz 5364240233Sglebius /* Balk when DF bit is set or the interface didn't support TSO. */ 5365241344Sglebius if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 5366240233Sglebius error = EMSGSIZE; 5367196039Srwatson KMOD_IPSTAT_INC(ips_cantfrag); 5368126258Smlaier if (r->rt != PF_DUPTO) { 5369126258Smlaier icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, 5370145886Smlaier ifp->if_mtu); 5371126258Smlaier goto done; 5372126258Smlaier } else 5373126258Smlaier goto bad; 5374126258Smlaier } 5375126258Smlaier 5376242161Sglebius error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 5377240233Sglebius if (error) 5378126258Smlaier goto bad; 5379126258Smlaier 5380240233Sglebius for (; m0; m0 = m1) { 5381126258Smlaier m1 = m0->m_nextpkt; 5382240233Sglebius m0->m_nextpkt = NULL; 5383126261Smlaier if (error == 0) { 5384254523Sandre m_clrprotoflags(m0); 5385240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5386126261Smlaier } else 5387126258Smlaier m_freem(m0); 5388126258Smlaier } 5389126258Smlaier 5390126258Smlaier if (error == 0) 5391196039Srwatson KMOD_IPSTAT_INC(ips_fragmented); 5392126258Smlaier 5393126258Smlaierdone: 5394126258Smlaier if (r->rt != PF_DUPTO) 5395126258Smlaier *m = NULL; 5396126258Smlaier return; 5397126258Smlaier 5398240233Sglebiusbad_locked: 5399240233Sglebius if (s) 5400240233Sglebius PF_STATE_UNLOCK(s); 5401126258Smlaierbad: 5402126258Smlaier m_freem(m0); 5403126258Smlaier goto done; 5404126258Smlaier} 5405126258Smlaier#endif /* INET */ 5406126258Smlaier 5407126258Smlaier#ifdef INET6 5408240233Sglebiusstatic void 5409126258Smlaierpf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5410171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5411126258Smlaier{ 5412126258Smlaier struct mbuf *m0; 5413240233Sglebius struct sockaddr_in6 dst; 5414126258Smlaier struct ip6_hdr *ip6; 5415126258Smlaier struct ifnet *ifp = NULL; 5416126258Smlaier struct pf_addr naddr; 5417130613Smlaier struct pf_src_node *sn = NULL; 5418126258Smlaier 5419240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5420240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5421240233Sglebius __func__)); 5422126258Smlaier 5423240233Sglebius if ((pd->pf_mtag == NULL && 5424240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5425240233Sglebius pd->pf_mtag->routed++ > 3) { 5426171168Smlaier m0 = *m; 5427171168Smlaier *m = NULL; 5428240233Sglebius goto bad_locked; 5429132303Smlaier } 5430132303Smlaier 5431126258Smlaier if (r->rt == PF_DUPTO) { 5432240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5433240233Sglebius if (s) 5434240233Sglebius PF_STATE_UNLOCK(s); 5435126258Smlaier return; 5436240233Sglebius } 5437126258Smlaier } else { 5438240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5439240233Sglebius if (s) 5440240233Sglebius PF_STATE_UNLOCK(s); 5441126258Smlaier return; 5442240233Sglebius } 5443126258Smlaier m0 = *m; 5444126258Smlaier } 5445126258Smlaier 5446126258Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5447126258Smlaier 5448240233Sglebius bzero(&dst, sizeof(dst)); 5449240233Sglebius dst.sin6_family = AF_INET6; 5450240233Sglebius dst.sin6_len = sizeof(dst); 5451240233Sglebius dst.sin6_addr = ip6->ip6_dst; 5452126258Smlaier 5453171168Smlaier /* Cheat. XXX why only in the v6 case??? */ 5454126258Smlaier if (r->rt == PF_FASTROUTE) { 5455240233Sglebius if (s) 5456240233Sglebius PF_STATE_UNLOCK(s); 5457132280Smlaier m0->m_flags |= M_SKIP_FIREWALL; 5458126261Smlaier ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL); 5459126258Smlaier return; 5460126258Smlaier } 5461126258Smlaier 5462145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5463145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5464240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5465240233Sglebius goto bad_locked; 5466145836Smlaier } 5467126258Smlaier if (s == NULL) { 5468130613Smlaier pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src, 5469130613Smlaier &naddr, NULL, &sn); 5470126258Smlaier if (!PF_AZERO(&naddr, AF_INET6)) 5471240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5472126258Smlaier &naddr, AF_INET6); 5473130613Smlaier ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; 5474126258Smlaier } else { 5475126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET6)) 5476240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5477126258Smlaier &s->rt_addr, AF_INET6); 5478130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5479126258Smlaier } 5480240233Sglebius 5481240233Sglebius if (s) 5482240233Sglebius PF_STATE_UNLOCK(s); 5483240233Sglebius 5484126258Smlaier if (ifp == NULL) 5485126258Smlaier goto bad; 5486126258Smlaier 5487126258Smlaier if (oifp != ifp) { 5488145836Smlaier if (pf_test6(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5489132303Smlaier goto bad; 5490132303Smlaier else if (m0 == NULL) 5491132303Smlaier goto done; 5492145836Smlaier if (m0->m_len < sizeof(struct ip6_hdr)) { 5493145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5494240233Sglebius ("%s: m0->m_len < sizeof(struct ip6_hdr)\n", 5495240233Sglebius __func__)); 5496145836Smlaier goto bad; 5497145836Smlaier } 5498132303Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5499126258Smlaier } 5500126258Smlaier 5501240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5502240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5503240233Sglebius 5504126258Smlaier /* 5505126258Smlaier * If the packet is too large for the outgoing interface, 5506126258Smlaier * send back an icmp6 error. 5507126258Smlaier */ 5508240233Sglebius if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 5509240233Sglebius dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 5510240233Sglebius if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) 5511240233Sglebius nd6_output(ifp, ifp, m0, &dst, NULL); 5512240233Sglebius else { 5513126258Smlaier in6_ifstat_inc(ifp, ifs6_in_toobig); 5514126258Smlaier if (r->rt != PF_DUPTO) 5515126258Smlaier icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); 5516126258Smlaier else 5517126258Smlaier goto bad; 5518126258Smlaier } 5519126258Smlaier 5520126258Smlaierdone: 5521126258Smlaier if (r->rt != PF_DUPTO) 5522126258Smlaier *m = NULL; 5523126258Smlaier return; 5524126258Smlaier 5525240233Sglebiusbad_locked: 5526240233Sglebius if (s) 5527240233Sglebius PF_STATE_UNLOCK(s); 5528126258Smlaierbad: 5529126258Smlaier m_freem(m0); 5530126258Smlaier goto done; 5531126258Smlaier} 5532126258Smlaier#endif /* INET6 */ 5533126258Smlaier 5534126258Smlaier/* 5535132566Smlaier * FreeBSD supports cksum offloads for the following drivers. 5536137413Sru * em(4), fxp(4), ixgb(4), lge(4), ndis(4), nge(4), re(4), 5537132566Smlaier * ti(4), txp(4), xl(4) 5538132566Smlaier * 5539132566Smlaier * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 5540132566Smlaier * network driver performed cksum including pseudo header, need to verify 5541132566Smlaier * csum_data 5542132566Smlaier * CSUM_DATA_VALID : 5543132566Smlaier * network driver performed cksum, needs to additional pseudo header 5544132566Smlaier * cksum computation with partial csum_data(i.e. lack of H/W support for 5545132566Smlaier * pseudo header, for instance hme(4), sk(4) and possibly gem(4)) 5546132566Smlaier * 5547132566Smlaier * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 5548132566Smlaier * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 5549132566Smlaier * TCP/UDP layer. 5550132566Smlaier * Also, set csum_data to 0xffff to force cksum validation. 5551126261Smlaier */ 5552240233Sglebiusstatic int 5553126261Smlaierpf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 5554126261Smlaier{ 5555126261Smlaier u_int16_t sum = 0; 5556126261Smlaier int hw_assist = 0; 5557126261Smlaier struct ip *ip; 5558126261Smlaier 5559126261Smlaier if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 5560126261Smlaier return (1); 5561126261Smlaier if (m->m_pkthdr.len < off + len) 5562126261Smlaier return (1); 5563126261Smlaier 5564126261Smlaier switch (p) { 5565126261Smlaier case IPPROTO_TCP: 5566126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5567126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5568126261Smlaier sum = m->m_pkthdr.csum_data; 5569126261Smlaier } else { 5570223637Sbz ip = mtod(m, struct ip *); 5571126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5572240233Sglebius ip->ip_dst.s_addr, htonl((u_short)len + 5573223637Sbz m->m_pkthdr.csum_data + IPPROTO_TCP)); 5574126261Smlaier } 5575126261Smlaier sum ^= 0xffff; 5576126261Smlaier ++hw_assist; 5577126261Smlaier } 5578126261Smlaier break; 5579126261Smlaier case IPPROTO_UDP: 5580126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5581126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5582126261Smlaier sum = m->m_pkthdr.csum_data; 5583126261Smlaier } else { 5584223637Sbz ip = mtod(m, struct ip *); 5585126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5586223637Sbz ip->ip_dst.s_addr, htonl((u_short)len + 5587223637Sbz m->m_pkthdr.csum_data + IPPROTO_UDP)); 5588126261Smlaier } 5589126261Smlaier sum ^= 0xffff; 5590126261Smlaier ++hw_assist; 5591223637Sbz } 5592126261Smlaier break; 5593126261Smlaier case IPPROTO_ICMP: 5594126261Smlaier#ifdef INET6 5595126261Smlaier case IPPROTO_ICMPV6: 5596126261Smlaier#endif /* INET6 */ 5597126261Smlaier break; 5598126261Smlaier default: 5599126261Smlaier return (1); 5600126261Smlaier } 5601126261Smlaier 5602126261Smlaier if (!hw_assist) { 5603126261Smlaier switch (af) { 5604126261Smlaier case AF_INET: 5605126261Smlaier if (p == IPPROTO_ICMP) { 5606126261Smlaier if (m->m_len < off) 5607126261Smlaier return (1); 5608126261Smlaier m->m_data += off; 5609126261Smlaier m->m_len -= off; 5610126261Smlaier sum = in_cksum(m, len); 5611126261Smlaier m->m_data -= off; 5612126261Smlaier m->m_len += off; 5613126261Smlaier } else { 5614126261Smlaier if (m->m_len < sizeof(struct ip)) 5615126261Smlaier return (1); 5616126261Smlaier sum = in4_cksum(m, p, off, len); 5617126261Smlaier } 5618126261Smlaier break; 5619126261Smlaier#ifdef INET6 5620126261Smlaier case AF_INET6: 5621126261Smlaier if (m->m_len < sizeof(struct ip6_hdr)) 5622126261Smlaier return (1); 5623126261Smlaier sum = in6_cksum(m, p, off, len); 5624126261Smlaier break; 5625126261Smlaier#endif /* INET6 */ 5626126261Smlaier default: 5627126261Smlaier return (1); 5628126261Smlaier } 5629126261Smlaier } 5630126261Smlaier if (sum) { 5631126261Smlaier switch (p) { 5632126261Smlaier case IPPROTO_TCP: 5633183550Szec { 5634196039Srwatson KMOD_TCPSTAT_INC(tcps_rcvbadsum); 5635126261Smlaier break; 5636183550Szec } 5637126261Smlaier case IPPROTO_UDP: 5638183550Szec { 5639196039Srwatson KMOD_UDPSTAT_INC(udps_badsum); 5640126261Smlaier break; 5641183550Szec } 5642222529Sbz#ifdef INET 5643126261Smlaier case IPPROTO_ICMP: 5644183550Szec { 5645196039Srwatson KMOD_ICMPSTAT_INC(icps_checksum); 5646126261Smlaier break; 5647183550Szec } 5648222529Sbz#endif 5649126261Smlaier#ifdef INET6 5650126261Smlaier case IPPROTO_ICMPV6: 5651183550Szec { 5652196039Srwatson KMOD_ICMP6STAT_INC(icp6s_checksum); 5653126261Smlaier break; 5654183550Szec } 5655126261Smlaier#endif /* INET6 */ 5656126261Smlaier } 5657126261Smlaier return (1); 5658132566Smlaier } else { 5659132566Smlaier if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 5660132566Smlaier m->m_pkthdr.csum_flags |= 5661132566Smlaier (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 5662132566Smlaier m->m_pkthdr.csum_data = 0xffff; 5663132566Smlaier } 5664126261Smlaier } 5665126261Smlaier return (0); 5666126261Smlaier} 5667223637Sbz 5668126258Smlaier 5669145836Smlaier#ifdef INET 5670126258Smlaierint 5671240233Sglebiuspf_test(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5672126258Smlaier{ 5673130613Smlaier struct pfi_kif *kif; 5674130613Smlaier u_short action, reason = 0, log = 0; 5675130613Smlaier struct mbuf *m = *m0; 5676223637Sbz struct ip *h = NULL; 5677223637Sbz struct m_tag *ipfwtag; 5678223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5679130613Smlaier struct pf_state *s = NULL; 5680130613Smlaier struct pf_ruleset *ruleset = NULL; 5681130613Smlaier struct pf_pdesc pd; 5682130613Smlaier int off, dirndx, pqid = 0; 5683126258Smlaier 5684240233Sglebius M_ASSERTPKTHDR(m); 5685240233Sglebius 5686223637Sbz if (!V_pf_status.running) 5687171168Smlaier return (PF_PASS); 5688126258Smlaier 5689171168Smlaier memset(&pd, 0, sizeof(pd)); 5690145836Smlaier 5691240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 5692240233Sglebius 5693130613Smlaier if (kif == NULL) { 5694145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5695145836Smlaier ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname)); 5696130613Smlaier return (PF_DROP); 5697130613Smlaier } 5698223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 5699145836Smlaier return (PF_PASS); 5700130613Smlaier 5701240233Sglebius if (m->m_flags & M_SKIP_FIREWALL) 5702240233Sglebius return (PF_PASS); 5703126258Smlaier 5704240233Sglebius pd.pf_mtag = pf_find_mtag(m); 5705240233Sglebius 5706240233Sglebius PF_RULES_RLOCK(); 5707240233Sglebius 5708223637Sbz if (ip_divert_ptr != NULL && 5709223637Sbz ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) { 5710223637Sbz struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1); 5711223637Sbz if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) { 5712240233Sglebius if (pd.pf_mtag == NULL && 5713240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5714240233Sglebius action = PF_DROP; 5715240233Sglebius goto done; 5716240233Sglebius } 5717223637Sbz pd.pf_mtag->flags |= PF_PACKET_LOOPED; 5718223637Sbz m_tag_delete(m, ipfwtag); 5719223637Sbz } 5720240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) { 5721223637Sbz m->m_flags |= M_FASTFWD_OURS; 5722223637Sbz pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT; 5723223637Sbz } 5724240233Sglebius } else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) { 5725240233Sglebius /* We do IP header normalization and packet reassembly here */ 5726126258Smlaier action = PF_DROP; 5727126258Smlaier goto done; 5728126258Smlaier } 5729223637Sbz m = *m0; /* pf_normalize messes with m0 */ 5730126258Smlaier h = mtod(m, struct ip *); 5731126258Smlaier 5732126258Smlaier off = h->ip_hl << 2; 5733240233Sglebius if (off < (int)sizeof(struct ip)) { 5734126258Smlaier action = PF_DROP; 5735126258Smlaier REASON_SET(&reason, PFRES_SHORT); 5736126258Smlaier log = 1; 5737126258Smlaier goto done; 5738126258Smlaier } 5739126258Smlaier 5740126258Smlaier pd.src = (struct pf_addr *)&h->ip_src; 5741126258Smlaier pd.dst = (struct pf_addr *)&h->ip_dst; 5742223637Sbz pd.sport = pd.dport = NULL; 5743126258Smlaier pd.ip_sum = &h->ip_sum; 5744223637Sbz pd.proto_sum = NULL; 5745126258Smlaier pd.proto = h->ip_p; 5746223637Sbz pd.dir = dir; 5747223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 5748223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 5749126258Smlaier pd.af = AF_INET; 5750126258Smlaier pd.tos = h->ip_tos; 5751126258Smlaier pd.tot_len = ntohs(h->ip_len); 5752126258Smlaier 5753126258Smlaier /* handle fragments that didn't get reassembled by normalization */ 5754126258Smlaier if (h->ip_off & htons(IP_MF | IP_OFFMASK)) { 5755130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 5756126258Smlaier &pd, &a, &ruleset); 5757126258Smlaier goto done; 5758126258Smlaier } 5759126258Smlaier 5760126258Smlaier switch (h->ip_p) { 5761126258Smlaier 5762126258Smlaier case IPPROTO_TCP: { 5763126258Smlaier struct tcphdr th; 5764126258Smlaier 5765126258Smlaier pd.hdr.tcp = &th; 5766126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 5767126258Smlaier &action, &reason, AF_INET)) { 5768126258Smlaier log = action != PF_PASS; 5769126258Smlaier goto done; 5770126258Smlaier } 5771126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 5772126258Smlaier if ((th.th_flags & TH_ACK) && pd.p_len == 0) 5773126258Smlaier pqid = 1; 5774130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 5775126258Smlaier if (action == PF_DROP) 5776130613Smlaier goto done; 5777130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 5778126258Smlaier &reason); 5779126258Smlaier if (action == PF_PASS) { 5780223637Sbz if (pfsync_update_state_ptr != NULL) 5781223637Sbz pfsync_update_state_ptr(s); 5782126258Smlaier r = s->rule.ptr; 5783130613Smlaier a = s->anchor.ptr; 5784126258Smlaier log = s->log; 5785126258Smlaier } else if (s == NULL) 5786240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5787240233Sglebius &a, &ruleset, inp); 5788126258Smlaier break; 5789126258Smlaier } 5790126258Smlaier 5791126258Smlaier case IPPROTO_UDP: { 5792126258Smlaier struct udphdr uh; 5793126258Smlaier 5794126258Smlaier pd.hdr.udp = &uh; 5795126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 5796126258Smlaier &action, &reason, AF_INET)) { 5797126258Smlaier log = action != PF_PASS; 5798126258Smlaier goto done; 5799126258Smlaier } 5800130613Smlaier if (uh.uh_dport == 0 || 5801130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 5802130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 5803130613Smlaier action = PF_DROP; 5804171168Smlaier REASON_SET(&reason, PFRES_SHORT); 5805130613Smlaier goto done; 5806130613Smlaier } 5807130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 5808126258Smlaier if (action == PF_PASS) { 5809223637Sbz if (pfsync_update_state_ptr != NULL) 5810223637Sbz pfsync_update_state_ptr(s); 5811126258Smlaier r = s->rule.ptr; 5812126258Smlaier a = s->anchor.ptr; 5813126258Smlaier log = s->log; 5814126258Smlaier } else if (s == NULL) 5815240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5816240233Sglebius &a, &ruleset, inp); 5817126258Smlaier break; 5818126258Smlaier } 5819126258Smlaier 5820126258Smlaier case IPPROTO_ICMP: { 5821126258Smlaier struct icmp ih; 5822126258Smlaier 5823126258Smlaier pd.hdr.icmp = &ih; 5824126258Smlaier if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN, 5825126258Smlaier &action, &reason, AF_INET)) { 5826126258Smlaier log = action != PF_PASS; 5827126258Smlaier goto done; 5828126258Smlaier } 5829145836Smlaier action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd, 5830145836Smlaier &reason); 5831126258Smlaier if (action == PF_PASS) { 5832223637Sbz if (pfsync_update_state_ptr != NULL) 5833223637Sbz pfsync_update_state_ptr(s); 5834126258Smlaier r = s->rule.ptr; 5835126258Smlaier a = s->anchor.ptr; 5836126258Smlaier log = s->log; 5837126258Smlaier } else if (s == NULL) 5838240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5839240233Sglebius &a, &ruleset, inp); 5840126258Smlaier break; 5841126258Smlaier } 5842126258Smlaier 5843223637Sbz#ifdef INET6 5844223637Sbz case IPPROTO_ICMPV6: { 5845223637Sbz action = PF_DROP; 5846223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5847223637Sbz ("pf: dropping IPv4 packet with ICMPv6 payload\n")); 5848223637Sbz goto done; 5849223637Sbz } 5850223637Sbz#endif 5851223637Sbz 5852126258Smlaier default: 5853223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 5854126258Smlaier if (action == PF_PASS) { 5855223637Sbz if (pfsync_update_state_ptr != NULL) 5856223637Sbz pfsync_update_state_ptr(s); 5857126258Smlaier r = s->rule.ptr; 5858126258Smlaier a = s->anchor.ptr; 5859126258Smlaier log = s->log; 5860126258Smlaier } else if (s == NULL) 5861240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5862240233Sglebius &a, &ruleset, inp); 5863126258Smlaier break; 5864126258Smlaier } 5865126258Smlaier 5866126258Smlaierdone: 5867240233Sglebius PF_RULES_RUNLOCK(); 5868126258Smlaier if (action == PF_PASS && h->ip_hl > 5 && 5869200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 5870126258Smlaier action = PF_DROP; 5871145836Smlaier REASON_SET(&reason, PFRES_IPOPTIONS); 5872126258Smlaier log = 1; 5873126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 5874126258Smlaier ("pf: dropping packet with ip options\n")); 5875126258Smlaier } 5876126258Smlaier 5877240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 5878240233Sglebius action = PF_DROP; 5879240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5880240233Sglebius } 5881240233Sglebius if (r->rtableid >= 0) 5882240233Sglebius M_SETFIB(m, r->rtableid); 5883145836Smlaier 5884126258Smlaier#ifdef ALTQ 5885126258Smlaier if (action == PF_PASS && r->qid) { 5886240233Sglebius if (pd.pf_mtag == NULL && 5887240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5888240233Sglebius action = PF_DROP; 5889240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5890240233Sglebius } 5891171168Smlaier if (pqid || (pd.tos & IPTOS_LOWDELAY)) 5892171168Smlaier pd.pf_mtag->qid = r->pqid; 5893171168Smlaier else 5894171168Smlaier pd.pf_mtag->qid = r->qid; 5895171168Smlaier /* add hints for ecn */ 5896171168Smlaier pd.pf_mtag->hdr = h; 5897223637Sbz 5898126258Smlaier } 5899145836Smlaier#endif /* ALTQ */ 5900126258Smlaier 5901130613Smlaier /* 5902130613Smlaier * connections redirected to loopback should not match sockets 5903130613Smlaier * bound specifically to loopback due to security implications, 5904130613Smlaier * see tcp_input() and in_pcblookup_listen(). 5905130613Smlaier */ 5906130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 5907130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 5908130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 5909130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 5910171168Smlaier (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 5911223637Sbz m->m_flags |= M_SKIP_FIREWALL; 5912171168Smlaier 5913240233Sglebius if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL && 5914240233Sglebius !PACKET_LOOPED(&pd)) { 5915223637Sbz 5916223637Sbz ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 5917240233Sglebius sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 5918223637Sbz if (ipfwtag != NULL) { 5919225171Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->info = 5920225171Sbz ntohs(r->divert.port); 5921223637Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir; 5922223637Sbz 5923240233Sglebius if (s) 5924240233Sglebius PF_STATE_UNLOCK(s); 5925240233Sglebius 5926223637Sbz m_tag_prepend(m, ipfwtag); 5927223637Sbz if (m->m_flags & M_FASTFWD_OURS) { 5928240233Sglebius if (pd.pf_mtag == NULL && 5929240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5930240233Sglebius action = PF_DROP; 5931240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5932240233Sglebius log = 1; 5933240233Sglebius DPFPRINTF(PF_DEBUG_MISC, 5934240233Sglebius ("pf: failed to allocate tag\n")); 5935240233Sglebius } 5936223637Sbz pd.pf_mtag->flags |= PF_FASTFWD_OURS_PRESENT; 5937223637Sbz m->m_flags &= ~M_FASTFWD_OURS; 5938223637Sbz } 5939240233Sglebius ip_divert_ptr(*m0, dir == PF_IN ? DIR_IN : DIR_OUT); 5940240233Sglebius *m0 = NULL; 5941223637Sbz 5942223637Sbz return (action); 5943223637Sbz } else { 5944223637Sbz /* XXX: ipfw has the same behaviour! */ 5945223637Sbz action = PF_DROP; 5946223637Sbz REASON_SET(&reason, PFRES_MEMORY); 5947223637Sbz log = 1; 5948223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5949223637Sbz ("pf: failed to allocate divert tag\n")); 5950223637Sbz } 5951223637Sbz } 5952223637Sbz 5953171168Smlaier if (log) { 5954171168Smlaier struct pf_rule *lr; 5955171168Smlaier 5956171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 5957171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 5958171168Smlaier lr = s->nat_rule.ptr; 5959171168Smlaier else 5960171168Smlaier lr = r; 5961240233Sglebius PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd, 5962240233Sglebius (s == NULL)); 5963130613Smlaier } 5964130613Smlaier 5965130613Smlaier kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 5966130613Smlaier kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++; 5967130613Smlaier 5968130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 5969171168Smlaier dirndx = (dir == PF_OUT); 5970171168Smlaier r->packets[dirndx]++; 5971171168Smlaier r->bytes[dirndx] += pd.tot_len; 5972130613Smlaier if (a != NULL) { 5973171168Smlaier a->packets[dirndx]++; 5974171168Smlaier a->bytes[dirndx] += pd.tot_len; 5975130613Smlaier } 5976130613Smlaier if (s != NULL) { 5977130613Smlaier if (s->nat_rule.ptr != NULL) { 5978171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 5979171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 5980130613Smlaier } 5981130613Smlaier if (s->src_node != NULL) { 5982171168Smlaier s->src_node->packets[dirndx]++; 5983171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 5984130613Smlaier } 5985130613Smlaier if (s->nat_src_node != NULL) { 5986171168Smlaier s->nat_src_node->packets[dirndx]++; 5987171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 5988130613Smlaier } 5989171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 5990171168Smlaier s->packets[dirndx]++; 5991171168Smlaier s->bytes[dirndx] += pd.tot_len; 5992130613Smlaier } 5993130613Smlaier tr = r; 5994130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 5995223637Sbz if (nr != NULL && r == &V_pf_default_rule) 5996223637Sbz tr = nr; 5997130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 5998223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 5999223637Sbz (s == NULL) ? pd.src : 6000223637Sbz &s->key[(s->direction == PF_IN)]-> 6001223637Sbz addr[(s->direction == PF_OUT)], 6002223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6003223637Sbz r->action == PF_PASS, tr->src.neg); 6004130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6005223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6006223637Sbz (s == NULL) ? pd.dst : 6007223637Sbz &s->key[(s->direction == PF_IN)]-> 6008223637Sbz addr[(s->direction == PF_IN)], 6009223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6010223637Sbz r->action == PF_PASS, tr->dst.neg); 6011130613Smlaier } 6012130613Smlaier 6013223637Sbz switch (action) { 6014223637Sbz case PF_SYNPROXY_DROP: 6015126258Smlaier m_freem(*m0); 6016223637Sbz case PF_DEFER: 6017126258Smlaier *m0 = NULL; 6018126258Smlaier action = PF_PASS; 6019223637Sbz break; 6020223637Sbz default: 6021240233Sglebius /* pf_route() returns unlocked. */ 6022240233Sglebius if (r->rt) { 6023223637Sbz pf_route(m0, r, dir, kif->pfik_ifp, s, &pd); 6024240233Sglebius return (action); 6025240233Sglebius } 6026223637Sbz break; 6027223637Sbz } 6028240233Sglebius if (s) 6029240233Sglebius PF_STATE_UNLOCK(s); 6030240233Sglebius 6031126258Smlaier return (action); 6032126258Smlaier} 6033126258Smlaier#endif /* INET */ 6034126258Smlaier 6035126258Smlaier#ifdef INET6 6036126258Smlaierint 6037240233Sglebiuspf_test6(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 6038126258Smlaier{ 6039130613Smlaier struct pfi_kif *kif; 6040130613Smlaier u_short action, reason = 0, log = 0; 6041171168Smlaier struct mbuf *m = *m0, *n = NULL; 6042223637Sbz struct ip6_hdr *h = NULL; 6043223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 6044130613Smlaier struct pf_state *s = NULL; 6045130613Smlaier struct pf_ruleset *ruleset = NULL; 6046130613Smlaier struct pf_pdesc pd; 6047169843Sdhartmei int off, terminal = 0, dirndx, rh_cnt = 0; 6048126258Smlaier 6049240233Sglebius M_ASSERTPKTHDR(m); 6050240233Sglebius 6051240233Sglebius if (!V_pf_status.running) 6052126258Smlaier return (PF_PASS); 6053126258Smlaier 6054171168Smlaier memset(&pd, 0, sizeof(pd)); 6055240233Sglebius pd.pf_mtag = pf_find_mtag(m); 6056145836Smlaier 6057240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED) 6058240233Sglebius return (PF_PASS); 6059240233Sglebius 6060240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 6061130613Smlaier if (kif == NULL) { 6062145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 6063145836Smlaier ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname)); 6064130613Smlaier return (PF_DROP); 6065130613Smlaier } 6066223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 6067145836Smlaier return (PF_PASS); 6068130613Smlaier 6069240233Sglebius PF_RULES_RLOCK(); 6070223637Sbz 6071126258Smlaier /* We do IP header normalization and packet reassembly here */ 6072145836Smlaier if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) { 6073126258Smlaier action = PF_DROP; 6074126258Smlaier goto done; 6075126258Smlaier } 6076223637Sbz m = *m0; /* pf_normalize messes with m0 */ 6077126258Smlaier h = mtod(m, struct ip6_hdr *); 6078126258Smlaier 6079169843Sdhartmei#if 1 6080169843Sdhartmei /* 6081169843Sdhartmei * we do not support jumbogram yet. if we keep going, zero ip6_plen 6082169843Sdhartmei * will do something bad, so drop the packet for now. 6083169843Sdhartmei */ 6084169843Sdhartmei if (htons(h->ip6_plen) == 0) { 6085169843Sdhartmei action = PF_DROP; 6086169843Sdhartmei REASON_SET(&reason, PFRES_NORM); /*XXX*/ 6087169843Sdhartmei goto done; 6088169843Sdhartmei } 6089169843Sdhartmei#endif 6090169843Sdhartmei 6091126258Smlaier pd.src = (struct pf_addr *)&h->ip6_src; 6092126258Smlaier pd.dst = (struct pf_addr *)&h->ip6_dst; 6093223637Sbz pd.sport = pd.dport = NULL; 6094126258Smlaier pd.ip_sum = NULL; 6095223637Sbz pd.proto_sum = NULL; 6096223637Sbz pd.dir = dir; 6097223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 6098223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 6099126258Smlaier pd.af = AF_INET6; 6100126258Smlaier pd.tos = 0; 6101126258Smlaier pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 6102126258Smlaier 6103126258Smlaier off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr); 6104126258Smlaier pd.proto = h->ip6_nxt; 6105126258Smlaier do { 6106126258Smlaier switch (pd.proto) { 6107126258Smlaier case IPPROTO_FRAGMENT: 6108130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 6109126258Smlaier &pd, &a, &ruleset); 6110126258Smlaier if (action == PF_DROP) 6111126258Smlaier REASON_SET(&reason, PFRES_FRAG); 6112126258Smlaier goto done; 6113169843Sdhartmei case IPPROTO_ROUTING: { 6114169843Sdhartmei struct ip6_rthdr rthdr; 6115169843Sdhartmei 6116169843Sdhartmei if (rh_cnt++) { 6117169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6118169843Sdhartmei ("pf: IPv6 more than one rthdr\n")); 6119169843Sdhartmei action = PF_DROP; 6120169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6121169843Sdhartmei log = 1; 6122169843Sdhartmei goto done; 6123169843Sdhartmei } 6124169843Sdhartmei if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL, 6125169843Sdhartmei &reason, pd.af)) { 6126169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6127169843Sdhartmei ("pf: IPv6 short rthdr\n")); 6128169843Sdhartmei action = PF_DROP; 6129169843Sdhartmei REASON_SET(&reason, PFRES_SHORT); 6130169843Sdhartmei log = 1; 6131169843Sdhartmei goto done; 6132169843Sdhartmei } 6133169843Sdhartmei if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 6134169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6135169843Sdhartmei ("pf: IPv6 rthdr0\n")); 6136169843Sdhartmei action = PF_DROP; 6137169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6138169843Sdhartmei log = 1; 6139169843Sdhartmei goto done; 6140169843Sdhartmei } 6141223637Sbz /* FALLTHROUGH */ 6142169843Sdhartmei } 6143126258Smlaier case IPPROTO_AH: 6144126258Smlaier case IPPROTO_HOPOPTS: 6145126258Smlaier case IPPROTO_DSTOPTS: { 6146126258Smlaier /* get next header and header length */ 6147126258Smlaier struct ip6_ext opt6; 6148126258Smlaier 6149126258Smlaier if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6), 6150145836Smlaier NULL, &reason, pd.af)) { 6151126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 6152126258Smlaier ("pf: IPv6 short opt\n")); 6153126258Smlaier action = PF_DROP; 6154126258Smlaier log = 1; 6155126258Smlaier goto done; 6156126258Smlaier } 6157126258Smlaier if (pd.proto == IPPROTO_AH) 6158126258Smlaier off += (opt6.ip6e_len + 2) * 4; 6159126258Smlaier else 6160126258Smlaier off += (opt6.ip6e_len + 1) * 8; 6161126258Smlaier pd.proto = opt6.ip6e_nxt; 6162126258Smlaier /* goto the next header */ 6163126258Smlaier break; 6164126258Smlaier } 6165126258Smlaier default: 6166126258Smlaier terminal++; 6167126258Smlaier break; 6168126258Smlaier } 6169126258Smlaier } while (!terminal); 6170126258Smlaier 6171171168Smlaier /* if there's no routing header, use unmodified mbuf for checksumming */ 6172171168Smlaier if (!n) 6173171168Smlaier n = m; 6174171168Smlaier 6175126258Smlaier switch (pd.proto) { 6176126258Smlaier 6177126258Smlaier case IPPROTO_TCP: { 6178126258Smlaier struct tcphdr th; 6179126258Smlaier 6180126258Smlaier pd.hdr.tcp = &th; 6181126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 6182126258Smlaier &action, &reason, AF_INET6)) { 6183126258Smlaier log = action != PF_PASS; 6184126258Smlaier goto done; 6185126258Smlaier } 6186126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 6187130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6188126258Smlaier if (action == PF_DROP) 6189130613Smlaier goto done; 6190130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6191126258Smlaier &reason); 6192126258Smlaier if (action == PF_PASS) { 6193223637Sbz if (pfsync_update_state_ptr != NULL) 6194223637Sbz pfsync_update_state_ptr(s); 6195126258Smlaier r = s->rule.ptr; 6196130613Smlaier a = s->anchor.ptr; 6197126258Smlaier log = s->log; 6198126258Smlaier } else if (s == NULL) 6199240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6200240233Sglebius &a, &ruleset, inp); 6201126258Smlaier break; 6202126258Smlaier } 6203126258Smlaier 6204126258Smlaier case IPPROTO_UDP: { 6205126258Smlaier struct udphdr uh; 6206126258Smlaier 6207126258Smlaier pd.hdr.udp = &uh; 6208126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 6209126258Smlaier &action, &reason, AF_INET6)) { 6210126258Smlaier log = action != PF_PASS; 6211126258Smlaier goto done; 6212126258Smlaier } 6213130613Smlaier if (uh.uh_dport == 0 || 6214130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 6215130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 6216130613Smlaier action = PF_DROP; 6217171168Smlaier REASON_SET(&reason, PFRES_SHORT); 6218130613Smlaier goto done; 6219130613Smlaier } 6220130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6221126258Smlaier if (action == PF_PASS) { 6222223637Sbz if (pfsync_update_state_ptr != NULL) 6223223637Sbz pfsync_update_state_ptr(s); 6224126258Smlaier r = s->rule.ptr; 6225130613Smlaier a = s->anchor.ptr; 6226126258Smlaier log = s->log; 6227126258Smlaier } else if (s == NULL) 6228240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6229240233Sglebius &a, &ruleset, inp); 6230126258Smlaier break; 6231126258Smlaier } 6232126258Smlaier 6233223637Sbz case IPPROTO_ICMP: { 6234223637Sbz action = PF_DROP; 6235223637Sbz DPFPRINTF(PF_DEBUG_MISC, 6236223637Sbz ("pf: dropping IPv6 packet with ICMPv4 payload\n")); 6237223637Sbz goto done; 6238223637Sbz } 6239223637Sbz 6240126258Smlaier case IPPROTO_ICMPV6: { 6241126258Smlaier struct icmp6_hdr ih; 6242126258Smlaier 6243126258Smlaier pd.hdr.icmp6 = &ih; 6244126258Smlaier if (!pf_pull_hdr(m, off, &ih, sizeof(ih), 6245126258Smlaier &action, &reason, AF_INET6)) { 6246126258Smlaier log = action != PF_PASS; 6247126258Smlaier goto done; 6248126258Smlaier } 6249130613Smlaier action = pf_test_state_icmp(&s, dir, kif, 6250145836Smlaier m, off, h, &pd, &reason); 6251126258Smlaier if (action == PF_PASS) { 6252223637Sbz if (pfsync_update_state_ptr != NULL) 6253223637Sbz pfsync_update_state_ptr(s); 6254126258Smlaier r = s->rule.ptr; 6255130613Smlaier a = s->anchor.ptr; 6256126258Smlaier log = s->log; 6257126258Smlaier } else if (s == NULL) 6258240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6259240233Sglebius &a, &ruleset, inp); 6260126258Smlaier break; 6261126258Smlaier } 6262126258Smlaier 6263126258Smlaier default: 6264223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 6265130613Smlaier if (action == PF_PASS) { 6266223637Sbz if (pfsync_update_state_ptr != NULL) 6267223637Sbz pfsync_update_state_ptr(s); 6268130613Smlaier r = s->rule.ptr; 6269130613Smlaier a = s->anchor.ptr; 6270130613Smlaier log = s->log; 6271130613Smlaier } else if (s == NULL) 6272240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6273240233Sglebius &a, &ruleset, inp); 6274126258Smlaier break; 6275126258Smlaier } 6276126258Smlaier 6277126258Smlaierdone: 6278240233Sglebius PF_RULES_RUNLOCK(); 6279223637Sbz if (n != m) { 6280223637Sbz m_freem(n); 6281223637Sbz n = NULL; 6282223637Sbz } 6283223637Sbz 6284169843Sdhartmei /* handle dangerous IPv6 extension headers. */ 6285169843Sdhartmei if (action == PF_PASS && rh_cnt && 6286200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6287169843Sdhartmei action = PF_DROP; 6288169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6289169843Sdhartmei log = 1; 6290169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6291169843Sdhartmei ("pf: dropping packet with dangerous v6 headers\n")); 6292169843Sdhartmei } 6293126258Smlaier 6294240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6295240233Sglebius action = PF_DROP; 6296240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6297240233Sglebius } 6298240233Sglebius if (r->rtableid >= 0) 6299240233Sglebius M_SETFIB(m, r->rtableid); 6300145836Smlaier 6301126258Smlaier#ifdef ALTQ 6302126258Smlaier if (action == PF_PASS && r->qid) { 6303240233Sglebius if (pd.pf_mtag == NULL && 6304240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6305240233Sglebius action = PF_DROP; 6306240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6307240233Sglebius } 6308171168Smlaier if (pd.tos & IPTOS_LOWDELAY) 6309171168Smlaier pd.pf_mtag->qid = r->pqid; 6310171168Smlaier else 6311171168Smlaier pd.pf_mtag->qid = r->qid; 6312171168Smlaier /* add hints for ecn */ 6313171168Smlaier pd.pf_mtag->hdr = h; 6314126258Smlaier } 6315145836Smlaier#endif /* ALTQ */ 6316126258Smlaier 6317130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6318130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6319130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 6320130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 6321171168Smlaier IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) 6322223637Sbz m->m_flags |= M_SKIP_FIREWALL; 6323171168Smlaier 6324223637Sbz /* XXX: Anybody working on it?! */ 6325223637Sbz if (r->divert.port) 6326223637Sbz printf("pf: divert(9) is not supported for IPv6\n"); 6327223637Sbz 6328171168Smlaier if (log) { 6329171168Smlaier struct pf_rule *lr; 6330171168Smlaier 6331171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 6332171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 6333171168Smlaier lr = s->nat_rule.ptr; 6334171168Smlaier else 6335171168Smlaier lr = r; 6336240233Sglebius PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset, 6337240233Sglebius &pd, (s == NULL)); 6338130613Smlaier } 6339130613Smlaier 6340130613Smlaier kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 6341130613Smlaier kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++; 6342130613Smlaier 6343130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 6344171168Smlaier dirndx = (dir == PF_OUT); 6345171168Smlaier r->packets[dirndx]++; 6346171168Smlaier r->bytes[dirndx] += pd.tot_len; 6347130613Smlaier if (a != NULL) { 6348171168Smlaier a->packets[dirndx]++; 6349171168Smlaier a->bytes[dirndx] += pd.tot_len; 6350130613Smlaier } 6351130613Smlaier if (s != NULL) { 6352130613Smlaier if (s->nat_rule.ptr != NULL) { 6353171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 6354171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 6355130613Smlaier } 6356130613Smlaier if (s->src_node != NULL) { 6357171168Smlaier s->src_node->packets[dirndx]++; 6358171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 6359130613Smlaier } 6360130613Smlaier if (s->nat_src_node != NULL) { 6361171168Smlaier s->nat_src_node->packets[dirndx]++; 6362171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 6363130613Smlaier } 6364171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 6365171168Smlaier s->packets[dirndx]++; 6366171168Smlaier s->bytes[dirndx] += pd.tot_len; 6367130613Smlaier } 6368130613Smlaier tr = r; 6369130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6370223637Sbz if (nr != NULL && r == &V_pf_default_rule) 6371223637Sbz tr = nr; 6372130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 6373223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 6374223637Sbz (s == NULL) ? pd.src : 6375223637Sbz &s->key[(s->direction == PF_IN)]->addr[0], 6376223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6377223637Sbz r->action == PF_PASS, tr->src.neg); 6378130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6379223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6380223637Sbz (s == NULL) ? pd.dst : 6381223637Sbz &s->key[(s->direction == PF_IN)]->addr[1], 6382223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6383223637Sbz r->action == PF_PASS, tr->dst.neg); 6384130613Smlaier } 6385130613Smlaier 6386223637Sbz switch (action) { 6387223637Sbz case PF_SYNPROXY_DROP: 6388126258Smlaier m_freem(*m0); 6389223637Sbz case PF_DEFER: 6390126258Smlaier *m0 = NULL; 6391126258Smlaier action = PF_PASS; 6392223637Sbz break; 6393223637Sbz default: 6394240233Sglebius /* pf_route6() returns unlocked. */ 6395240233Sglebius if (r->rt) { 6396223637Sbz pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd); 6397240233Sglebius return (action); 6398240233Sglebius } 6399223637Sbz break; 6400223637Sbz } 6401126258Smlaier 6402240233Sglebius if (s) 6403240233Sglebius PF_STATE_UNLOCK(s); 6404240233Sglebius 6405126258Smlaier return (action); 6406126258Smlaier} 6407126258Smlaier#endif /* INET6 */ 6408