pf.c revision 270576
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 270576 2014-08-25 15:49:41Z glebius $"); 40126261Smlaier 41240233Sglebius#include "opt_inet.h" 42240233Sglebius#include "opt_inet6.h" 43126261Smlaier#include "opt_bpf.h" 44126261Smlaier#include "opt_pf.h" 45153110Sru 46126258Smlaier#include <sys/param.h> 47240233Sglebius#include <sys/bus.h> 48240233Sglebius#include <sys/endian.h> 49240233Sglebius#include <sys/hash.h> 50240233Sglebius#include <sys/interrupt.h> 51240233Sglebius#include <sys/kernel.h> 52240233Sglebius#include <sys/kthread.h> 53240233Sglebius#include <sys/limits.h> 54126258Smlaier#include <sys/mbuf.h> 55240233Sglebius#include <sys/md5.h> 56240233Sglebius#include <sys/random.h> 57240233Sglebius#include <sys/refcount.h> 58126258Smlaier#include <sys/socket.h> 59126261Smlaier#include <sys/sysctl.h> 60240233Sglebius#include <sys/taskqueue.h> 61240233Sglebius#include <sys/ucred.h> 62126258Smlaier 63126258Smlaier#include <net/if.h> 64126258Smlaier#include <net/if_types.h> 65126258Smlaier#include <net/route.h> 66171168Smlaier#include <net/radix_mpath.h> 67240233Sglebius#include <net/vnet.h> 68126258Smlaier 69240233Sglebius#include <net/pfvar.h> 70240233Sglebius#include <net/if_pflog.h> 71240233Sglebius#include <net/if_pfsync.h> 72240233Sglebius 73240233Sglebius#include <netinet/in_pcb.h> 74126258Smlaier#include <netinet/in_var.h> 75126258Smlaier#include <netinet/ip.h> 76240233Sglebius#include <netinet/ip_fw.h> 77240233Sglebius#include <netinet/ip_icmp.h> 78240233Sglebius#include <netinet/icmp_var.h> 79126258Smlaier#include <netinet/ip_var.h> 80126258Smlaier#include <netinet/tcp.h> 81240233Sglebius#include <netinet/tcp_fsm.h> 82126258Smlaier#include <netinet/tcp_seq.h> 83126258Smlaier#include <netinet/tcp_timer.h> 84126258Smlaier#include <netinet/tcp_var.h> 85240233Sglebius#include <netinet/udp.h> 86126258Smlaier#include <netinet/udp_var.h> 87126258Smlaier 88240494Sglebius#include <netpfil/ipfw/ip_fw_private.h> /* XXX: only for DIR_IN/DIR_OUT */ 89240494Sglebius 90126258Smlaier#ifdef INET6 91126258Smlaier#include <netinet/ip6.h> 92126258Smlaier#include <netinet/icmp6.h> 93126258Smlaier#include <netinet6/nd6.h> 94126261Smlaier#include <netinet6/ip6_var.h> 95126261Smlaier#include <netinet6/in6_pcb.h> 96126258Smlaier#endif /* INET6 */ 97126258Smlaier 98126261Smlaier#include <machine/in_cksum.h> 99163606Srwatson#include <security/mac/mac_framework.h> 100126258Smlaier 101223637Sbz#define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 102126258Smlaier 103126258Smlaier/* 104126258Smlaier * Global variables 105126258Smlaier */ 106126258Smlaier 107223637Sbz/* state tables */ 108223637SbzVNET_DEFINE(struct pf_altqqueue, pf_altqs[2]); 109223637SbzVNET_DEFINE(struct pf_palist, pf_pabuf); 110223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_active); 111223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_inactive); 112270574SglebiusVNET_DEFINE(struct pf_kstatus, pf_status); 113223637Sbz 114223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_active); 115223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_inactive); 116223637SbzVNET_DEFINE(int, altqs_inactive_open); 117223637SbzVNET_DEFINE(u_int32_t, ticket_pabuf); 118223637Sbz 119223637SbzVNET_DEFINE(MD5_CTX, pf_tcp_secret_ctx); 120223637Sbz#define V_pf_tcp_secret_ctx VNET(pf_tcp_secret_ctx) 121223637SbzVNET_DEFINE(u_char, pf_tcp_secret[16]); 122223637Sbz#define V_pf_tcp_secret VNET(pf_tcp_secret) 123223637SbzVNET_DEFINE(int, pf_tcp_secret_init); 124223637Sbz#define V_pf_tcp_secret_init VNET(pf_tcp_secret_init) 125223637SbzVNET_DEFINE(int, pf_tcp_iss_off); 126223637Sbz#define V_pf_tcp_iss_off VNET(pf_tcp_iss_off) 127223637Sbz 128240233Sglebius/* 129240233Sglebius * Queue for pf_intr() sends. 130240233Sglebius */ 131240233Sglebiusstatic MALLOC_DEFINE(M_PFTEMP, "pf_temp", "pf(4) temporary allocations"); 132240233Sglebiusstruct pf_send_entry { 133240233Sglebius STAILQ_ENTRY(pf_send_entry) pfse_next; 134240233Sglebius struct mbuf *pfse_m; 135240233Sglebius enum { 136240233Sglebius PFSE_IP, 137240233Sglebius PFSE_IP6, 138240233Sglebius PFSE_ICMP, 139240233Sglebius PFSE_ICMP6, 140240233Sglebius } pfse_type; 141240233Sglebius union { 142240233Sglebius struct route ro; 143240233Sglebius struct { 144240233Sglebius int type; 145240233Sglebius int code; 146240233Sglebius int mtu; 147240233Sglebius } icmpopts; 148240233Sglebius } u; 149240233Sglebius#define pfse_ro u.ro 150240233Sglebius#define pfse_icmp_type u.icmpopts.type 151240233Sglebius#define pfse_icmp_code u.icmpopts.code 152240233Sglebius#define pfse_icmp_mtu u.icmpopts.mtu 153240233Sglebius}; 154223637Sbz 155240233SglebiusSTAILQ_HEAD(pf_send_head, pf_send_entry); 156240233Sglebiusstatic VNET_DEFINE(struct pf_send_head, pf_sendqueue); 157240233Sglebius#define V_pf_sendqueue VNET(pf_sendqueue) 158126258Smlaier 159240233Sglebiusstatic struct mtx pf_sendqueue_mtx; 160240233Sglebius#define PF_SENDQ_LOCK() mtx_lock(&pf_sendqueue_mtx) 161240233Sglebius#define PF_SENDQ_UNLOCK() mtx_unlock(&pf_sendqueue_mtx) 162126258Smlaier 163240233Sglebius/* 164240811Sglebius * Queue for pf_overload_task() tasks. 165240233Sglebius */ 166240811Sglebiusstruct pf_overload_entry { 167240811Sglebius SLIST_ENTRY(pf_overload_entry) next; 168240233Sglebius struct pf_addr addr; 169240233Sglebius sa_family_t af; 170240233Sglebius uint8_t dir; 171240811Sglebius struct pf_rule *rule; 172240233Sglebius}; 173223637Sbz 174240811SglebiusSLIST_HEAD(pf_overload_head, pf_overload_entry); 175240811Sglebiusstatic VNET_DEFINE(struct pf_overload_head, pf_overloadqueue); 176240811Sglebius#define V_pf_overloadqueue VNET(pf_overloadqueue) 177240811Sglebiusstatic VNET_DEFINE(struct task, pf_overloadtask); 178240811Sglebius#define V_pf_overloadtask VNET(pf_overloadtask) 179126261Smlaier 180240811Sglebiusstatic struct mtx pf_overloadqueue_mtx; 181240811Sglebius#define PF_OVERLOADQ_LOCK() mtx_lock(&pf_overloadqueue_mtx) 182240811Sglebius#define PF_OVERLOADQ_UNLOCK() mtx_unlock(&pf_overloadqueue_mtx) 183126258Smlaier 184240233SglebiusVNET_DEFINE(struct pf_rulequeue, pf_unlinked_rules); 185240233Sglebiusstruct mtx pf_unlnkdrules_mtx; 186240233Sglebius 187240233Sglebiusstatic VNET_DEFINE(uma_zone_t, pf_sources_z); 188240233Sglebius#define V_pf_sources_z VNET(pf_sources_z) 189265008Smmuma_zone_t pf_mtag_z; 190240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_z); 191240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_key_z); 192240233Sglebius 193240233SglebiusVNET_DEFINE(uint64_t, pf_stateid[MAXCPU]); 194240233Sglebius#define PFID_CPUBITS 8 195240233Sglebius#define PFID_CPUSHIFT (sizeof(uint64_t) * NBBY - PFID_CPUBITS) 196240233Sglebius#define PFID_CPUMASK ((uint64_t)((1 << PFID_CPUBITS) - 1) << PFID_CPUSHIFT) 197240233Sglebius#define PFID_MAXID (~PFID_CPUMASK) 198240233SglebiusCTASSERT((1 << PFID_CPUBITS) > MAXCPU); 199240233Sglebius 200240233Sglebiusstatic void pf_src_tree_remove_state(struct pf_state *); 201240233Sglebiusstatic void pf_init_threshold(struct pf_threshold *, u_int32_t, 202145836Smlaier u_int32_t); 203240233Sglebiusstatic void pf_add_threshold(struct pf_threshold *); 204240233Sglebiusstatic int pf_check_threshold(struct pf_threshold *); 205145836Smlaier 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 int pf_check_proto_cksum(struct mbuf *, int, int, 269126258Smlaier u_int8_t, sa_family_t); 270240233Sglebiusstatic void pf_print_state_parts(struct pf_state *, 271223637Sbz struct pf_state_key *, struct pf_state_key *); 272240233Sglebiusstatic int pf_addr_wrap_neq(struct pf_addr_wrap *, 273126258Smlaier struct pf_addr_wrap *); 274240233Sglebiusstatic struct pf_state *pf_find_state(struct pfi_kif *, 275240233Sglebius struct pf_state_key_cmp *, u_int); 276240233Sglebiusstatic int pf_src_connlimit(struct pf_state **); 277264454Smmstatic void pf_overload_task(void *v, int pending); 278240233Sglebiusstatic int pf_insert_src_node(struct pf_src_node **, 279240233Sglebius struct pf_rule *, struct pf_addr *, sa_family_t); 280241039Sglebiusstatic u_int pf_purge_expired_states(u_int, int); 281240233Sglebiusstatic void pf_purge_unlinked_rules(void); 282265008Smmstatic int pf_mtag_uminit(void *, int, int); 283240233Sglebiusstatic void pf_mtag_free(struct m_tag *); 284240233Sglebius#ifdef INET 285240233Sglebiusstatic void pf_route(struct mbuf **, struct pf_rule *, int, 286240233Sglebius struct ifnet *, struct pf_state *, 287240233Sglebius struct pf_pdesc *); 288240233Sglebius#endif /* INET */ 289240233Sglebius#ifdef INET6 290240233Sglebiusstatic void pf_change_a6(struct pf_addr *, u_int16_t *, 291240233Sglebius struct pf_addr *, u_int8_t); 292240233Sglebiusstatic void pf_route6(struct mbuf **, struct pf_rule *, int, 293240233Sglebius struct ifnet *, struct pf_state *, 294240233Sglebius struct pf_pdesc *); 295240233Sglebius#endif /* INET6 */ 296126258Smlaier 297126261Smlaierint in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len); 298126258Smlaier 299223637SbzVNET_DECLARE(int, pf_end_threads); 300171168Smlaier 301240233SglebiusVNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 302145836Smlaier 303240233Sglebius#define PACKET_LOOPED(pd) ((pd)->pf_mtag && \ 304240233Sglebius (pd)->pf_mtag->flags & PF_PACKET_LOOPED) 305126258Smlaier 306240233Sglebius#define STATE_LOOKUP(i, k, d, s, pd) \ 307126258Smlaier do { \ 308240233Sglebius (s) = pf_find_state((i), (k), (d)); \ 309261018Sglebius if ((s) == NULL) \ 310126258Smlaier return (PF_DROP); \ 311240233Sglebius if (PACKET_LOOPED(pd)) \ 312126258Smlaier return (PF_PASS); \ 313240233Sglebius if ((d) == PF_OUT && \ 314223637Sbz (((s)->rule.ptr->rt == PF_ROUTETO && \ 315223637Sbz (s)->rule.ptr->direction == PF_OUT) || \ 316223637Sbz ((s)->rule.ptr->rt == PF_REPLYTO && \ 317223637Sbz (s)->rule.ptr->direction == PF_IN)) && \ 318223637Sbz (s)->rt_kif != NULL && \ 319240233Sglebius (s)->rt_kif != (i)) \ 320223637Sbz return (PF_PASS); \ 321126258Smlaier } while (0) 322126258Smlaier 323223637Sbz#define BOUND_IFACE(r, k) \ 324223637Sbz ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : V_pfi_all 325126258Smlaier 326263029Sglebius#define STATE_INC_COUNTERS(s) \ 327263029Sglebius do { \ 328263029Sglebius counter_u64_add(s->rule.ptr->states_cur, 1); \ 329263029Sglebius counter_u64_add(s->rule.ptr->states_tot, 1); \ 330263029Sglebius if (s->anchor.ptr != NULL) { \ 331263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, 1); \ 332263029Sglebius counter_u64_add(s->anchor.ptr->states_tot, 1); \ 333263029Sglebius } \ 334263029Sglebius if (s->nat_rule.ptr != NULL) { \ 335263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, 1);\ 336263029Sglebius counter_u64_add(s->nat_rule.ptr->states_tot, 1);\ 337263029Sglebius } \ 338145836Smlaier } while (0) 339145836Smlaier 340263029Sglebius#define STATE_DEC_COUNTERS(s) \ 341263029Sglebius do { \ 342263029Sglebius if (s->nat_rule.ptr != NULL) \ 343263029Sglebius counter_u64_add(s->nat_rule.ptr->states_cur, -1);\ 344263029Sglebius if (s->anchor.ptr != NULL) \ 345263029Sglebius counter_u64_add(s->anchor.ptr->states_cur, -1); \ 346263029Sglebius counter_u64_add(s->rule.ptr->states_cur, -1); \ 347145836Smlaier } while (0) 348145836Smlaier 349240233Sglebiusstatic MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures"); 350240233SglebiusVNET_DEFINE(struct pf_keyhash *, pf_keyhash); 351240233SglebiusVNET_DEFINE(struct pf_idhash *, pf_idhash); 352240233SglebiusVNET_DEFINE(u_long, pf_hashmask); 353240233SglebiusVNET_DEFINE(struct pf_srchash *, pf_srchash); 354240233SglebiusVNET_DEFINE(u_long, pf_srchashmask); 355223637Sbz 356240233SglebiusSYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW, 0, "pf(4)"); 357223637Sbz 358240233SglebiusVNET_DEFINE(u_long, pf_hashsize); 359240233Sglebius#define V_pf_hashsize VNET(pf_hashsize) 360240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_RDTUN, 361240233Sglebius &VNET_NAME(pf_hashsize), 0, "Size of pf(4) states hashtable"); 362130613Smlaier 363240233SglebiusVNET_DEFINE(u_long, pf_srchashsize); 364240233Sglebius#define V_pf_srchashsize VNET(pf_srchashsize) 365240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_RDTUN, 366240233Sglebius &VNET_NAME(pf_srchashsize), 0, "Size of pf(4) source nodes hashtable"); 367171168Smlaier 368240233SglebiusVNET_DEFINE(void *, pf_swi_cookie); 369130613Smlaier 370240233SglebiusVNET_DEFINE(uint32_t, pf_hashseed); 371240233Sglebius#define V_pf_hashseed VNET(pf_hashseed) 372240233Sglebius 373240233Sglebiusstatic __inline uint32_t 374240233Sglebiuspf_hashkey(struct pf_state_key *sk) 375126258Smlaier{ 376240233Sglebius uint32_t h; 377126258Smlaier 378240233Sglebius h = jenkins_hash32((uint32_t *)sk, 379240233Sglebius sizeof(struct pf_state_key_cmp)/sizeof(uint32_t), 380240233Sglebius V_pf_hashseed); 381240233Sglebius 382240233Sglebius return (h & V_pf_hashmask); 383130613Smlaier} 384130613Smlaier 385240736Sglebiusstatic __inline uint32_t 386240736Sglebiuspf_hashsrc(struct pf_addr *addr, sa_family_t af) 387240736Sglebius{ 388240736Sglebius uint32_t h; 389240736Sglebius 390240736Sglebius switch (af) { 391240736Sglebius case AF_INET: 392240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v4, 393240736Sglebius sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed); 394240736Sglebius break; 395240736Sglebius case AF_INET6: 396240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v6, 397240736Sglebius sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed); 398240736Sglebius break; 399240736Sglebius default: 400240736Sglebius panic("%s: unknown address family %u", __func__, af); 401240736Sglebius } 402240736Sglebius 403240736Sglebius return (h & V_pf_srchashmask); 404240736Sglebius} 405240736Sglebius 406126258Smlaier#ifdef INET6 407126258Smlaiervoid 408126258Smlaierpf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af) 409126258Smlaier{ 410126258Smlaier switch (af) { 411126258Smlaier#ifdef INET 412126258Smlaier case AF_INET: 413126258Smlaier dst->addr32[0] = src->addr32[0]; 414126258Smlaier break; 415126258Smlaier#endif /* INET */ 416126258Smlaier case AF_INET6: 417126258Smlaier dst->addr32[0] = src->addr32[0]; 418126258Smlaier dst->addr32[1] = src->addr32[1]; 419126258Smlaier dst->addr32[2] = src->addr32[2]; 420126258Smlaier dst->addr32[3] = src->addr32[3]; 421126258Smlaier break; 422126258Smlaier } 423126258Smlaier} 424145836Smlaier#endif /* INET6 */ 425126258Smlaier 426240233Sglebiusstatic void 427145836Smlaierpf_init_threshold(struct pf_threshold *threshold, 428145836Smlaier u_int32_t limit, u_int32_t seconds) 429145836Smlaier{ 430145836Smlaier threshold->limit = limit * PF_THRESHOLD_MULT; 431145836Smlaier threshold->seconds = seconds; 432145836Smlaier threshold->count = 0; 433240233Sglebius threshold->last = time_uptime; 434145836Smlaier} 435145836Smlaier 436240233Sglebiusstatic void 437145836Smlaierpf_add_threshold(struct pf_threshold *threshold) 438145836Smlaier{ 439240233Sglebius u_int32_t t = time_uptime, diff = t - threshold->last; 440145836Smlaier 441145836Smlaier if (diff >= threshold->seconds) 442145836Smlaier threshold->count = 0; 443145836Smlaier else 444145836Smlaier threshold->count -= threshold->count * diff / 445145836Smlaier threshold->seconds; 446145836Smlaier threshold->count += PF_THRESHOLD_MULT; 447145836Smlaier threshold->last = t; 448145836Smlaier} 449145836Smlaier 450240233Sglebiusstatic int 451145836Smlaierpf_check_threshold(struct pf_threshold *threshold) 452145836Smlaier{ 453145836Smlaier return (threshold->count > threshold->limit); 454145836Smlaier} 455145836Smlaier 456240233Sglebiusstatic int 457145836Smlaierpf_src_connlimit(struct pf_state **state) 458145836Smlaier{ 459240811Sglebius struct pf_overload_entry *pfoe; 460145836Smlaier int bad = 0; 461145836Smlaier 462240233Sglebius PF_STATE_LOCK_ASSERT(*state); 463240233Sglebius 464145836Smlaier (*state)->src_node->conn++; 465171168Smlaier (*state)->src.tcp_est = 1; 466145836Smlaier pf_add_threshold(&(*state)->src_node->conn_rate); 467145836Smlaier 468145836Smlaier if ((*state)->rule.ptr->max_src_conn && 469145836Smlaier (*state)->rule.ptr->max_src_conn < 470145836Smlaier (*state)->src_node->conn) { 471270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONN], 1); 472145836Smlaier bad++; 473145836Smlaier } 474145836Smlaier 475145836Smlaier if ((*state)->rule.ptr->max_src_conn_rate.limit && 476145836Smlaier pf_check_threshold(&(*state)->src_node->conn_rate)) { 477270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCCONNRATE], 1); 478145836Smlaier bad++; 479145836Smlaier } 480145836Smlaier 481145836Smlaier if (!bad) 482145836Smlaier return (0); 483145836Smlaier 484240233Sglebius /* Kill this state. */ 485240233Sglebius (*state)->timeout = PFTM_PURGE; 486240233Sglebius (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 487145836Smlaier 488240233Sglebius if ((*state)->rule.ptr->overload_tbl == NULL) 489240233Sglebius return (1); 490145836Smlaier 491240811Sglebius /* Schedule overloading and flushing task. */ 492240811Sglebius pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT); 493240811Sglebius if (pfoe == NULL) 494240233Sglebius return (1); /* too bad :( */ 495240233Sglebius 496240811Sglebius bcopy(&(*state)->src_node->addr, &pfoe->addr, sizeof(pfoe->addr)); 497240811Sglebius pfoe->af = (*state)->key[PF_SK_WIRE]->af; 498240811Sglebius pfoe->rule = (*state)->rule.ptr; 499240811Sglebius pfoe->dir = (*state)->direction; 500240811Sglebius PF_OVERLOADQ_LOCK(); 501240811Sglebius SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next); 502240811Sglebius PF_OVERLOADQ_UNLOCK(); 503240811Sglebius taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask); 504240233Sglebius 505240233Sglebius return (1); 506240233Sglebius} 507240233Sglebius 508240233Sglebiusstatic void 509264454Smmpf_overload_task(void *v, int pending) 510240233Sglebius{ 511240811Sglebius struct pf_overload_head queue; 512240811Sglebius struct pfr_addr p; 513240811Sglebius struct pf_overload_entry *pfoe, *pfoe1; 514240233Sglebius uint32_t killed = 0; 515240233Sglebius 516264454Smm CURVNET_SET((struct vnet *)v); 517264454Smm 518240811Sglebius PF_OVERLOADQ_LOCK(); 519264454Smm queue = V_pf_overloadqueue; 520264454Smm SLIST_INIT(&V_pf_overloadqueue); 521240811Sglebius PF_OVERLOADQ_UNLOCK(); 522240233Sglebius 523240811Sglebius bzero(&p, sizeof(p)); 524240811Sglebius SLIST_FOREACH(pfoe, &queue, next) { 525270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_OVERLOAD_TABLE], 1); 526240811Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) { 527240811Sglebius printf("%s: blocking address ", __func__); 528240811Sglebius pf_print_host(&pfoe->addr, 0, pfoe->af); 529240811Sglebius printf("\n"); 530240811Sglebius } 531240233Sglebius 532240811Sglebius p.pfra_af = pfoe->af; 533240811Sglebius switch (pfoe->af) { 534240811Sglebius#ifdef INET 535240811Sglebius case AF_INET: 536240811Sglebius p.pfra_net = 32; 537240811Sglebius p.pfra_ip4addr = pfoe->addr.v4; 538240811Sglebius break; 539240811Sglebius#endif 540240811Sglebius#ifdef INET6 541240811Sglebius case AF_INET6: 542240811Sglebius p.pfra_net = 128; 543240811Sglebius p.pfra_ip6addr = pfoe->addr.v6; 544240811Sglebius break; 545240811Sglebius#endif 546240811Sglebius } 547240811Sglebius 548240811Sglebius PF_RULES_WLOCK(); 549240811Sglebius pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second); 550240811Sglebius PF_RULES_WUNLOCK(); 551240811Sglebius } 552240811Sglebius 553240811Sglebius /* 554240811Sglebius * Remove those entries, that don't need flushing. 555240811Sglebius */ 556240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 557240811Sglebius if (pfoe->rule->flush == 0) { 558240811Sglebius SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next); 559240811Sglebius free(pfoe, M_PFTEMP); 560240811Sglebius } else 561270574Sglebius counter_u64_add( 562270574Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH], 1); 563240811Sglebius 564240811Sglebius /* If nothing to flush, return. */ 565264454Smm if (SLIST_EMPTY(&queue)) { 566264454Smm CURVNET_RESTORE(); 567240811Sglebius return; 568264454Smm } 569240811Sglebius 570240233Sglebius for (int i = 0; i <= V_pf_hashmask; i++) { 571240233Sglebius struct pf_idhash *ih = &V_pf_idhash[i]; 572240233Sglebius struct pf_state_key *sk; 573240233Sglebius struct pf_state *s; 574240233Sglebius 575240233Sglebius PF_HASHROW_LOCK(ih); 576240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 577240233Sglebius sk = s->key[PF_SK_WIRE]; 578240811Sglebius SLIST_FOREACH(pfoe, &queue, next) 579240811Sglebius if (sk->af == pfoe->af && 580240811Sglebius ((pfoe->rule->flush & PF_FLUSH_GLOBAL) || 581240811Sglebius pfoe->rule == s->rule.ptr) && 582240811Sglebius ((pfoe->dir == PF_OUT && 583240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) || 584240811Sglebius (pfoe->dir == PF_IN && 585240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) { 586240233Sglebius s->timeout = PFTM_PURGE; 587240233Sglebius s->src.state = s->dst.state = TCPS_CLOSED; 588240233Sglebius killed++; 589145836Smlaier } 590145836Smlaier } 591240233Sglebius PF_HASHROW_UNLOCK(ih); 592145836Smlaier } 593240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 594240811Sglebius free(pfoe, M_PFTEMP); 595240233Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) 596240233Sglebius printf("%s: %u states killed", __func__, killed); 597264454Smm 598264454Smm CURVNET_RESTORE(); 599240233Sglebius} 600145836Smlaier 601240233Sglebius/* 602240233Sglebius * Can return locked on failure, so that we can consistently 603240233Sglebius * allocate and insert a new one. 604240233Sglebius */ 605240233Sglebiusstruct pf_src_node * 606240233Sglebiuspf_find_src_node(struct pf_addr *src, struct pf_rule *rule, sa_family_t af, 607240233Sglebius int returnlocked) 608240233Sglebius{ 609240233Sglebius struct pf_srchash *sh; 610240233Sglebius struct pf_src_node *n; 611240233Sglebius 612270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_SEARCH], 1); 613240233Sglebius 614240233Sglebius sh = &V_pf_srchash[pf_hashsrc(src, af)]; 615240233Sglebius PF_HASHROW_LOCK(sh); 616240233Sglebius LIST_FOREACH(n, &sh->nodes, entry) 617240233Sglebius if (n->rule.ptr == rule && n->af == af && 618240233Sglebius ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) || 619240233Sglebius (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0))) 620240233Sglebius break; 621240233Sglebius if (n != NULL || returnlocked == 0) 622240233Sglebius PF_HASHROW_UNLOCK(sh); 623240233Sglebius 624240233Sglebius return (n); 625145836Smlaier} 626145836Smlaier 627240233Sglebiusstatic int 628130613Smlaierpf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule, 629130613Smlaier struct pf_addr *src, sa_family_t af) 630126258Smlaier{ 631126258Smlaier 632240233Sglebius KASSERT((rule->rule_flag & PFRULE_RULESRCTRACK || 633240233Sglebius rule->rpool.opts & PF_POOL_STICKYADDR), 634240233Sglebius ("%s for non-tracking rule %p", __func__, rule)); 635240233Sglebius 636240233Sglebius if (*sn == NULL) 637240233Sglebius *sn = pf_find_src_node(src, rule, af, 1); 638240233Sglebius 639130613Smlaier if (*sn == NULL) { 640240233Sglebius struct pf_srchash *sh = &V_pf_srchash[pf_hashsrc(src, af)]; 641240233Sglebius 642240233Sglebius PF_HASHROW_ASSERT(sh); 643240233Sglebius 644130613Smlaier if (!rule->max_src_nodes || 645263029Sglebius counter_u64_fetch(rule->src_nodes) < rule->max_src_nodes) 646240233Sglebius (*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO); 647145836Smlaier else 648270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCNODES], 649270574Sglebius 1); 650240233Sglebius if ((*sn) == NULL) { 651240233Sglebius PF_HASHROW_UNLOCK(sh); 652130613Smlaier return (-1); 653240233Sglebius } 654145836Smlaier 655145836Smlaier pf_init_threshold(&(*sn)->conn_rate, 656145836Smlaier rule->max_src_conn_rate.limit, 657145836Smlaier rule->max_src_conn_rate.seconds); 658145836Smlaier 659130613Smlaier (*sn)->af = af; 660240233Sglebius (*sn)->rule.ptr = rule; 661130613Smlaier PF_ACPY(&(*sn)->addr, src, af); 662240233Sglebius LIST_INSERT_HEAD(&sh->nodes, *sn, entry); 663240233Sglebius (*sn)->creation = time_uptime; 664130613Smlaier (*sn)->ruletype = rule->action; 665130613Smlaier if ((*sn)->rule.ptr != NULL) 666263029Sglebius counter_u64_add((*sn)->rule.ptr->src_nodes, 1); 667240233Sglebius PF_HASHROW_UNLOCK(sh); 668270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_INSERT], 1); 669130613Smlaier } else { 670130613Smlaier if (rule->max_src_states && 671145836Smlaier (*sn)->states >= rule->max_src_states) { 672270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_SRCSTATES], 673270574Sglebius 1); 674130613Smlaier return (-1); 675145836Smlaier } 676130613Smlaier } 677130613Smlaier return (0); 678130613Smlaier} 679126258Smlaier 680261019Sglebiusvoid 681261019Sglebiuspf_unlink_src_node_locked(struct pf_src_node *src) 682240233Sglebius{ 683261019Sglebius#ifdef INVARIANTS 684240233Sglebius struct pf_srchash *sh; 685223637Sbz 686240233Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 687261019Sglebius PF_HASHROW_ASSERT(sh); 688261019Sglebius#endif 689240233Sglebius LIST_REMOVE(src, entry); 690261019Sglebius if (src->rule.ptr) 691263029Sglebius counter_u64_add(src->rule.ptr->src_nodes, -1); 692270574Sglebius counter_u64_add(V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS], 1); 693261019Sglebius} 694240737Sglebius 695261019Sglebiusvoid 696261019Sglebiuspf_unlink_src_node(struct pf_src_node *src) 697261019Sglebius{ 698261019Sglebius struct pf_srchash *sh; 699261019Sglebius 700261019Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 701261019Sglebius PF_HASHROW_LOCK(sh); 702261019Sglebius pf_unlink_src_node_locked(src); 703261019Sglebius PF_HASHROW_UNLOCK(sh); 704240233Sglebius} 705240233Sglebius 706261019Sglebiusstatic void 707261019Sglebiuspf_free_src_node(struct pf_src_node *sn) 708261019Sglebius{ 709261019Sglebius 710261019Sglebius KASSERT(sn->states == 0, ("%s: %p has refs", __func__, sn)); 711261019Sglebius uma_zfree(V_pf_sources_z, sn); 712261019Sglebius} 713261019Sglebius 714261019Sglebiusu_int 715261019Sglebiuspf_free_src_nodes(struct pf_src_node_list *head) 716261019Sglebius{ 717261019Sglebius struct pf_src_node *sn, *tmp; 718261019Sglebius u_int count = 0; 719261019Sglebius 720261019Sglebius LIST_FOREACH_SAFE(sn, head, entry, tmp) { 721261019Sglebius pf_free_src_node(sn); 722261019Sglebius count++; 723261019Sglebius } 724261019Sglebius 725261019Sglebius return (count); 726261019Sglebius} 727261019Sglebius 728240233Sglebiusvoid 729265008Smmpf_mtag_initialize() 730265008Smm{ 731265008Smm 732265008Smm pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) + 733265008Smm sizeof(struct pf_mtag), NULL, NULL, pf_mtag_uminit, NULL, 734265008Smm UMA_ALIGN_PTR, 0); 735265008Smm} 736265008Smm 737265008Smm/* Per-vnet data storage structures initialization. */ 738265008Smmvoid 739240233Sglebiuspf_initialize() 740223637Sbz{ 741240233Sglebius struct pf_keyhash *kh; 742240233Sglebius struct pf_idhash *ih; 743240233Sglebius struct pf_srchash *sh; 744240233Sglebius u_int i; 745223637Sbz 746240233Sglebius TUNABLE_ULONG_FETCH("net.pf.states_hashsize", &V_pf_hashsize); 747240233Sglebius if (V_pf_hashsize == 0 || !powerof2(V_pf_hashsize)) 748240233Sglebius V_pf_hashsize = PF_HASHSIZ; 749240233Sglebius TUNABLE_ULONG_FETCH("net.pf.source_nodes_hashsize", &V_pf_srchashsize); 750240233Sglebius if (V_pf_srchashsize == 0 || !powerof2(V_pf_srchashsize)) 751240233Sglebius V_pf_srchashsize = PF_HASHSIZ / 4; 752240233Sglebius 753240233Sglebius V_pf_hashseed = arc4random(); 754240233Sglebius 755240233Sglebius /* States and state keys storage. */ 756240233Sglebius V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_state), 757240233Sglebius NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 758240233Sglebius V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z; 759240233Sglebius uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT); 760244347Spjd uma_zone_set_warning(V_pf_state_z, "PF states limit reached"); 761240233Sglebius 762240233Sglebius V_pf_state_key_z = uma_zcreate("pf state keys", 763240233Sglebius sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL, 764240233Sglebius UMA_ALIGN_PTR, 0); 765240233Sglebius V_pf_keyhash = malloc(V_pf_hashsize * sizeof(struct pf_keyhash), 766240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 767240233Sglebius V_pf_idhash = malloc(V_pf_hashsize * sizeof(struct pf_idhash), 768240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 769240233Sglebius V_pf_hashmask = V_pf_hashsize - 1; 770240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 771240233Sglebius i++, kh++, ih++) { 772251681Sglebius mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK); 773240233Sglebius mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF); 774223637Sbz } 775223637Sbz 776240233Sglebius /* Source nodes. */ 777240233Sglebius V_pf_sources_z = uma_zcreate("pf source nodes", 778240233Sglebius sizeof(struct pf_src_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 779240233Sglebius 0); 780240233Sglebius V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z; 781240233Sglebius uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT); 782244347Spjd uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached"); 783240233Sglebius V_pf_srchash = malloc(V_pf_srchashsize * sizeof(struct pf_srchash), 784240233Sglebius M_PFHASH, M_WAITOK|M_ZERO); 785240233Sglebius V_pf_srchashmask = V_pf_srchashsize - 1; 786240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) 787240233Sglebius mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF); 788223637Sbz 789240233Sglebius /* ALTQ */ 790240233Sglebius TAILQ_INIT(&V_pf_altqs[0]); 791240233Sglebius TAILQ_INIT(&V_pf_altqs[1]); 792240233Sglebius TAILQ_INIT(&V_pf_pabuf); 793240233Sglebius V_pf_altqs_active = &V_pf_altqs[0]; 794240233Sglebius V_pf_altqs_inactive = &V_pf_altqs[1]; 795240233Sglebius 796240233Sglebius 797240811Sglebius /* Send & overload+flush queues. */ 798240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 799240811Sglebius SLIST_INIT(&V_pf_overloadqueue); 800264454Smm TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, curvnet); 801240233Sglebius mtx_init(&pf_sendqueue_mtx, "pf send queue", NULL, MTX_DEF); 802240811Sglebius mtx_init(&pf_overloadqueue_mtx, "pf overload/flush queue", NULL, 803240811Sglebius MTX_DEF); 804240233Sglebius 805240233Sglebius /* Unlinked, but may be referenced rules. */ 806240233Sglebius TAILQ_INIT(&V_pf_unlinked_rules); 807240233Sglebius mtx_init(&pf_unlnkdrules_mtx, "pf unlinked rules", NULL, MTX_DEF); 808223637Sbz} 809223637Sbz 810240233Sglebiusvoid 811265008Smmpf_mtag_cleanup() 812265008Smm{ 813265008Smm 814265008Smm uma_zdestroy(pf_mtag_z); 815265008Smm} 816265008Smm 817265008Smmvoid 818240233Sglebiuspf_cleanup() 819223637Sbz{ 820240233Sglebius struct pf_keyhash *kh; 821240233Sglebius struct pf_idhash *ih; 822240233Sglebius struct pf_srchash *sh; 823240233Sglebius struct pf_send_entry *pfse, *next; 824240233Sglebius u_int i; 825223637Sbz 826240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 827240233Sglebius i++, kh++, ih++) { 828240233Sglebius KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty", 829240233Sglebius __func__)); 830240233Sglebius KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty", 831240233Sglebius __func__)); 832240233Sglebius mtx_destroy(&kh->lock); 833240233Sglebius mtx_destroy(&ih->lock); 834240233Sglebius } 835240233Sglebius free(V_pf_keyhash, M_PFHASH); 836240233Sglebius free(V_pf_idhash, M_PFHASH); 837240233Sglebius 838240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 839240233Sglebius KASSERT(LIST_EMPTY(&sh->nodes), 840240233Sglebius ("%s: source node hash not empty", __func__)); 841240233Sglebius mtx_destroy(&sh->lock); 842240233Sglebius } 843240233Sglebius free(V_pf_srchash, M_PFHASH); 844240233Sglebius 845240233Sglebius STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) { 846240233Sglebius m_freem(pfse->pfse_m); 847240233Sglebius free(pfse, M_PFTEMP); 848240233Sglebius } 849240233Sglebius 850240233Sglebius mtx_destroy(&pf_sendqueue_mtx); 851240811Sglebius mtx_destroy(&pf_overloadqueue_mtx); 852240233Sglebius mtx_destroy(&pf_unlnkdrules_mtx); 853240233Sglebius 854240233Sglebius uma_zdestroy(V_pf_sources_z); 855240233Sglebius uma_zdestroy(V_pf_state_z); 856240233Sglebius uma_zdestroy(V_pf_state_key_z); 857240233Sglebius} 858240233Sglebius 859240233Sglebiusstatic int 860265008Smmpf_mtag_uminit(void *mem, int size, int how) 861240233Sglebius{ 862240233Sglebius struct m_tag *t; 863240233Sglebius 864240233Sglebius t = (struct m_tag *)mem; 865240233Sglebius t->m_tag_cookie = MTAG_ABI_COMPAT; 866240233Sglebius t->m_tag_id = PACKET_TAG_PF; 867240233Sglebius t->m_tag_len = sizeof(struct pf_mtag); 868240233Sglebius t->m_tag_free = pf_mtag_free; 869240233Sglebius 870223637Sbz return (0); 871223637Sbz} 872223637Sbz 873240233Sglebiusstatic void 874240233Sglebiuspf_mtag_free(struct m_tag *t) 875130613Smlaier{ 876223637Sbz 877265008Smm uma_zfree(pf_mtag_z, t); 878240233Sglebius} 879223637Sbz 880240233Sglebiusstruct pf_mtag * 881240233Sglebiuspf_get_mtag(struct mbuf *m) 882240233Sglebius{ 883240233Sglebius struct m_tag *mtag; 884240233Sglebius 885240233Sglebius if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL) 886240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 887240233Sglebius 888265008Smm mtag = uma_zalloc(pf_mtag_z, M_NOWAIT); 889240233Sglebius if (mtag == NULL) 890240233Sglebius return (NULL); 891240233Sglebius bzero(mtag + 1, sizeof(struct pf_mtag)); 892240233Sglebius m_tag_prepend(m, mtag); 893240233Sglebius 894240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 895240233Sglebius} 896240233Sglebius 897240233Sglebiusstatic int 898240233Sglebiuspf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks, 899240233Sglebius struct pf_state *s) 900240233Sglebius{ 901251681Sglebius struct pf_keyhash *khs, *khw, *kh; 902240233Sglebius struct pf_state_key *sk, *cur; 903240233Sglebius struct pf_state *si, *olds = NULL; 904240233Sglebius int idx; 905240233Sglebius 906240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 907240233Sglebius KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__)); 908240233Sglebius KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__)); 909240233Sglebius 910240233Sglebius /* 911251681Sglebius * We need to lock hash slots of both keys. To avoid deadlock 912251681Sglebius * we always lock the slot with lower address first. Unlock order 913251681Sglebius * isn't important. 914251681Sglebius * 915251681Sglebius * We also need to lock ID hash slot before dropping key 916251681Sglebius * locks. On success we return with ID hash slot locked. 917251681Sglebius */ 918251681Sglebius 919251681Sglebius if (skw == sks) { 920251681Sglebius khs = khw = &V_pf_keyhash[pf_hashkey(skw)]; 921251681Sglebius PF_HASHROW_LOCK(khs); 922251681Sglebius } else { 923251681Sglebius khs = &V_pf_keyhash[pf_hashkey(sks)]; 924251681Sglebius khw = &V_pf_keyhash[pf_hashkey(skw)]; 925251681Sglebius if (khs == khw) { 926251681Sglebius PF_HASHROW_LOCK(khs); 927251681Sglebius } else if (khs < khw) { 928251681Sglebius PF_HASHROW_LOCK(khs); 929251681Sglebius PF_HASHROW_LOCK(khw); 930251681Sglebius } else { 931251681Sglebius PF_HASHROW_LOCK(khw); 932251681Sglebius PF_HASHROW_LOCK(khs); 933251681Sglebius } 934251681Sglebius } 935251681Sglebius 936251681Sglebius#define KEYS_UNLOCK() do { \ 937251681Sglebius if (khs != khw) { \ 938251681Sglebius PF_HASHROW_UNLOCK(khs); \ 939251681Sglebius PF_HASHROW_UNLOCK(khw); \ 940251681Sglebius } else \ 941251681Sglebius PF_HASHROW_UNLOCK(khs); \ 942251681Sglebius} while (0) 943251681Sglebius 944251681Sglebius /* 945240233Sglebius * First run: start with wire key. 946240233Sglebius */ 947240233Sglebius sk = skw; 948251681Sglebius kh = khw; 949240233Sglebius idx = PF_SK_WIRE; 950240233Sglebius 951240233Sglebiuskeyattach: 952240233Sglebius LIST_FOREACH(cur, &kh->keys, entry) 953240233Sglebius if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0) 954240233Sglebius break; 955240233Sglebius 956240233Sglebius if (cur != NULL) { 957240233Sglebius /* Key exists. Check for same kif, if none, add to key. */ 958240233Sglebius TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) { 959240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)]; 960240233Sglebius 961240233Sglebius PF_HASHROW_LOCK(ih); 962240233Sglebius if (si->kif == s->kif && 963240233Sglebius si->direction == s->direction) { 964223637Sbz if (sk->proto == IPPROTO_TCP && 965240233Sglebius si->src.state >= TCPS_FIN_WAIT_2 && 966240233Sglebius si->dst.state >= TCPS_FIN_WAIT_2) { 967251681Sglebius /* 968251681Sglebius * New state matches an old >FIN_WAIT_2 969251681Sglebius * state. We can't drop key hash locks, 970251681Sglebius * thus we can't unlink it properly. 971251681Sglebius * 972251681Sglebius * As a workaround we drop it into 973251681Sglebius * TCPS_CLOSED state, schedule purge 974251681Sglebius * ASAP and push it into the very end 975251681Sglebius * of the slot TAILQ, so that it won't 976251681Sglebius * conflict with our new state. 977251681Sglebius */ 978240233Sglebius si->src.state = si->dst.state = 979223637Sbz TCPS_CLOSED; 980251681Sglebius si->timeout = PFTM_PURGE; 981240233Sglebius olds = si; 982223637Sbz } else { 983223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 984223637Sbz printf("pf: %s key attach " 985223637Sbz "failed on %s: ", 986223637Sbz (idx == PF_SK_WIRE) ? 987223637Sbz "wire" : "stack", 988223637Sbz s->kif->pfik_name); 989223637Sbz pf_print_state_parts(s, 990223637Sbz (idx == PF_SK_WIRE) ? 991223637Sbz sk : NULL, 992223637Sbz (idx == PF_SK_STACK) ? 993223637Sbz sk : NULL); 994223637Sbz printf(", existing: "); 995240233Sglebius pf_print_state_parts(si, 996223637Sbz (idx == PF_SK_WIRE) ? 997223637Sbz sk : NULL, 998223637Sbz (idx == PF_SK_STACK) ? 999223637Sbz sk : NULL); 1000223637Sbz printf("\n"); 1001223637Sbz } 1002240233Sglebius PF_HASHROW_UNLOCK(ih); 1003251681Sglebius KEYS_UNLOCK(); 1004240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1005240233Sglebius if (idx == PF_SK_STACK) 1006240233Sglebius pf_detach_state(s); 1007250522Sglebius return (EEXIST); /* collision! */ 1008223637Sbz } 1009223637Sbz } 1010240233Sglebius PF_HASHROW_UNLOCK(ih); 1011240233Sglebius } 1012240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1013223637Sbz s->key[idx] = cur; 1014240233Sglebius } else { 1015240233Sglebius LIST_INSERT_HEAD(&kh->keys, sk, entry); 1016223637Sbz s->key[idx] = sk; 1017126258Smlaier } 1018126258Smlaier 1019240233Sglebiusstateattach: 1020240233Sglebius /* List is sorted, if-bound states before floating. */ 1021223637Sbz if (s->kif == V_pfi_all) 1022240233Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]); 1023223637Sbz else 1024240233Sglebius TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]); 1025223637Sbz 1026251681Sglebius if (olds) { 1027251681Sglebius TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]); 1028251681Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds, 1029251681Sglebius key_list[idx]); 1030251681Sglebius olds = NULL; 1031251681Sglebius } 1032251681Sglebius 1033240233Sglebius /* 1034240233Sglebius * Attach done. See how should we (or should not?) 1035240233Sglebius * attach a second key. 1036240233Sglebius */ 1037240233Sglebius if (sks == skw) { 1038240233Sglebius s->key[PF_SK_STACK] = s->key[PF_SK_WIRE]; 1039240233Sglebius idx = PF_SK_STACK; 1040240233Sglebius sks = NULL; 1041240233Sglebius goto stateattach; 1042240233Sglebius } else if (sks != NULL) { 1043240233Sglebius /* 1044240233Sglebius * Continue attaching with stack key. 1045240233Sglebius */ 1046240233Sglebius sk = sks; 1047251681Sglebius kh = khs; 1048240233Sglebius idx = PF_SK_STACK; 1049240233Sglebius sks = NULL; 1050240233Sglebius goto keyattach; 1051240233Sglebius } 1052240233Sglebius 1053251681Sglebius PF_STATE_LOCK(s); 1054251681Sglebius KEYS_UNLOCK(); 1055251681Sglebius 1056240233Sglebius KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL, 1057240233Sglebius ("%s failure", __func__)); 1058240233Sglebius 1059223637Sbz return (0); 1060251681Sglebius#undef KEYS_UNLOCK 1061223637Sbz} 1062223637Sbz 1063240233Sglebiusstatic void 1064223637Sbzpf_detach_state(struct pf_state *s) 1065223637Sbz{ 1066240233Sglebius struct pf_state_key *sks = s->key[PF_SK_STACK]; 1067240233Sglebius struct pf_keyhash *kh; 1068223637Sbz 1069240233Sglebius if (sks != NULL) { 1070240233Sglebius kh = &V_pf_keyhash[pf_hashkey(sks)]; 1071240233Sglebius PF_HASHROW_LOCK(kh); 1072240233Sglebius if (s->key[PF_SK_STACK] != NULL) 1073240233Sglebius pf_state_key_detach(s, PF_SK_STACK); 1074240233Sglebius /* 1075240233Sglebius * If both point to same key, then we are done. 1076240233Sglebius */ 1077240233Sglebius if (sks == s->key[PF_SK_WIRE]) { 1078240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1079240233Sglebius PF_HASHROW_UNLOCK(kh); 1080240233Sglebius return; 1081240233Sglebius } 1082240233Sglebius PF_HASHROW_UNLOCK(kh); 1083240233Sglebius } 1084223637Sbz 1085240233Sglebius if (s->key[PF_SK_WIRE] != NULL) { 1086240233Sglebius kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])]; 1087240233Sglebius PF_HASHROW_LOCK(kh); 1088240233Sglebius if (s->key[PF_SK_WIRE] != NULL) 1089240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1090240233Sglebius PF_HASHROW_UNLOCK(kh); 1091240233Sglebius } 1092223637Sbz} 1093223637Sbz 1094240233Sglebiusstatic void 1095223637Sbzpf_state_key_detach(struct pf_state *s, int idx) 1096223637Sbz{ 1097240233Sglebius struct pf_state_key *sk = s->key[idx]; 1098240233Sglebius#ifdef INVARIANTS 1099240233Sglebius struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)]; 1100223637Sbz 1101240233Sglebius PF_HASHROW_ASSERT(kh); 1102223637Sbz#endif 1103240233Sglebius TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]); 1104240233Sglebius s->key[idx] = NULL; 1105223637Sbz 1106240233Sglebius if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) { 1107240233Sglebius LIST_REMOVE(sk, entry); 1108240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1109223637Sbz } 1110223637Sbz} 1111223637Sbz 1112240233Sglebiusstatic int 1113240233Sglebiuspf_state_key_ctor(void *mem, int size, void *arg, int flags) 1114240233Sglebius{ 1115240233Sglebius struct pf_state_key *sk = mem; 1116240233Sglebius 1117240233Sglebius bzero(sk, sizeof(struct pf_state_key_cmp)); 1118240233Sglebius TAILQ_INIT(&sk->states[PF_SK_WIRE]); 1119240233Sglebius TAILQ_INIT(&sk->states[PF_SK_STACK]); 1120240233Sglebius 1121240233Sglebius return (0); 1122240233Sglebius} 1123240233Sglebius 1124223637Sbzstruct pf_state_key * 1125240233Sglebiuspf_state_key_setup(struct pf_pdesc *pd, struct pf_addr *saddr, 1126240233Sglebius struct pf_addr *daddr, u_int16_t sport, u_int16_t dport) 1127223637Sbz{ 1128240233Sglebius struct pf_state_key *sk; 1129223637Sbz 1130240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1131240233Sglebius if (sk == NULL) 1132223637Sbz return (NULL); 1133223637Sbz 1134240233Sglebius PF_ACPY(&sk->addr[pd->sidx], saddr, pd->af); 1135240233Sglebius PF_ACPY(&sk->addr[pd->didx], daddr, pd->af); 1136240233Sglebius sk->port[pd->sidx] = sport; 1137240233Sglebius sk->port[pd->didx] = dport; 1138240233Sglebius sk->proto = pd->proto; 1139240233Sglebius sk->af = pd->af; 1140240233Sglebius 1141223637Sbz return (sk); 1142223637Sbz} 1143223637Sbz 1144240233Sglebiusstruct pf_state_key * 1145240233Sglebiuspf_state_key_clone(struct pf_state_key *orig) 1146223637Sbz{ 1147240233Sglebius struct pf_state_key *sk; 1148223637Sbz 1149240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1150240233Sglebius if (sk == NULL) 1151240233Sglebius return (NULL); 1152223637Sbz 1153240233Sglebius bcopy(orig, sk, sizeof(struct pf_state_key_cmp)); 1154223637Sbz 1155240233Sglebius return (sk); 1156223637Sbz} 1157223637Sbz 1158223637Sbzint 1159223637Sbzpf_state_insert(struct pfi_kif *kif, struct pf_state_key *skw, 1160223637Sbz struct pf_state_key *sks, struct pf_state *s) 1161223637Sbz{ 1162240233Sglebius struct pf_idhash *ih; 1163240233Sglebius struct pf_state *cur; 1164250522Sglebius int error; 1165223637Sbz 1166240233Sglebius KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]), 1167240233Sglebius ("%s: sks not pristine", __func__)); 1168240233Sglebius KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]), 1169240233Sglebius ("%s: skw not pristine", __func__)); 1170240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1171240233Sglebius 1172223637Sbz s->kif = kif; 1173223637Sbz 1174223637Sbz if (s->id == 0 && s->creatorid == 0) { 1175240233Sglebius /* XXX: should be atomic, but probability of collision low */ 1176240233Sglebius if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID) 1177240233Sglebius V_pf_stateid[curcpu] = 1; 1178240233Sglebius s->id |= (uint64_t )curcpu << PFID_CPUSHIFT; 1179240233Sglebius s->id = htobe64(s->id); 1180223637Sbz s->creatorid = V_pf_status.hostid; 1181130613Smlaier } 1182240233Sglebius 1183251681Sglebius /* Returns with ID locked on success. */ 1184250522Sglebius if ((error = pf_state_key_attach(skw, sks, s)) != 0) 1185250522Sglebius return (error); 1186244184Sglebius 1187240233Sglebius ih = &V_pf_idhash[PF_IDHASH(s)]; 1188251681Sglebius PF_HASHROW_ASSERT(ih); 1189240233Sglebius LIST_FOREACH(cur, &ih->states, entry) 1190240233Sglebius if (cur->id == s->id && cur->creatorid == s->creatorid) 1191240233Sglebius break; 1192240233Sglebius 1193240233Sglebius if (cur != NULL) { 1194240233Sglebius PF_HASHROW_UNLOCK(ih); 1195223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 1196250521Sglebius printf("pf: state ID collision: " 1197250312Sglebius "id: %016llx creatorid: %08x\n", 1198240233Sglebius (unsigned long long)be64toh(s->id), 1199240233Sglebius ntohl(s->creatorid)); 1200130613Smlaier } 1201223637Sbz pf_detach_state(s); 1202250522Sglebius return (EEXIST); 1203130613Smlaier } 1204240233Sglebius LIST_INSERT_HEAD(&ih->states, s, entry); 1205240233Sglebius /* One for keys, one for ID hash. */ 1206240233Sglebius refcount_init(&s->refs, 2); 1207240233Sglebius 1208270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_INSERT], 1); 1209223637Sbz if (pfsync_insert_state_ptr != NULL) 1210223637Sbz pfsync_insert_state_ptr(s); 1211240233Sglebius 1212240233Sglebius /* Returns locked. */ 1213126258Smlaier return (0); 1214126258Smlaier} 1215126258Smlaier 1216240233Sglebius/* 1217240233Sglebius * Find state by ID: returns with locked row on success. 1218240233Sglebius */ 1219223637Sbzstruct pf_state * 1220240233Sglebiuspf_find_state_byid(uint64_t id, uint32_t creatorid) 1221223637Sbz{ 1222240233Sglebius struct pf_idhash *ih; 1223240233Sglebius struct pf_state *s; 1224240233Sglebius 1225270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1226223637Sbz 1227240233Sglebius ih = &V_pf_idhash[(be64toh(id) % (V_pf_hashmask + 1))]; 1228223637Sbz 1229240233Sglebius PF_HASHROW_LOCK(ih); 1230240233Sglebius LIST_FOREACH(s, &ih->states, entry) 1231240233Sglebius if (s->id == id && s->creatorid == creatorid) 1232240233Sglebius break; 1233223637Sbz 1234240233Sglebius if (s == NULL) 1235240233Sglebius PF_HASHROW_UNLOCK(ih); 1236240233Sglebius 1237240233Sglebius return (s); 1238223637Sbz} 1239223637Sbz 1240240233Sglebius/* 1241240233Sglebius * Find state by key. 1242240233Sglebius * Returns with ID hash slot locked on success. 1243240233Sglebius */ 1244240233Sglebiusstatic struct pf_state * 1245240233Sglebiuspf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int dir) 1246223637Sbz{ 1247240233Sglebius struct pf_keyhash *kh; 1248223637Sbz struct pf_state_key *sk; 1249240233Sglebius struct pf_state *s; 1250240233Sglebius int idx; 1251223637Sbz 1252270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1253223637Sbz 1254240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1255240233Sglebius 1256240233Sglebius PF_HASHROW_LOCK(kh); 1257240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1258240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1259240233Sglebius break; 1260240233Sglebius if (sk == NULL) { 1261240233Sglebius PF_HASHROW_UNLOCK(kh); 1262240233Sglebius return (NULL); 1263223637Sbz } 1264223637Sbz 1265240233Sglebius idx = (dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK); 1266223637Sbz 1267240233Sglebius /* List is sorted, if-bound states before floating ones. */ 1268240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) 1269240233Sglebius if (s->kif == V_pfi_all || s->kif == kif) { 1270240233Sglebius PF_STATE_LOCK(s); 1271240233Sglebius PF_HASHROW_UNLOCK(kh); 1272261018Sglebius if (s->timeout >= PFTM_MAX) { 1273240233Sglebius /* 1274261018Sglebius * State is either being processed by 1275261018Sglebius * pf_unlink_state() in an other thread, or 1276261018Sglebius * is scheduled for immediate expiry. 1277240233Sglebius */ 1278240233Sglebius PF_STATE_UNLOCK(s); 1279240233Sglebius return (NULL); 1280240233Sglebius } 1281240233Sglebius return (s); 1282240233Sglebius } 1283240233Sglebius PF_HASHROW_UNLOCK(kh); 1284223637Sbz 1285223637Sbz return (NULL); 1286223637Sbz} 1287223637Sbz 1288223637Sbzstruct pf_state * 1289223637Sbzpf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more) 1290223637Sbz{ 1291240233Sglebius struct pf_keyhash *kh; 1292223637Sbz struct pf_state_key *sk; 1293240233Sglebius struct pf_state *s, *ret = NULL; 1294240233Sglebius int idx, inout = 0; 1295223637Sbz 1296270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_SEARCH], 1); 1297223637Sbz 1298240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1299223637Sbz 1300240233Sglebius PF_HASHROW_LOCK(kh); 1301240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1302240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1303240233Sglebius break; 1304240233Sglebius if (sk == NULL) { 1305240233Sglebius PF_HASHROW_UNLOCK(kh); 1306240233Sglebius return (NULL); 1307223637Sbz } 1308240233Sglebius switch (dir) { 1309240233Sglebius case PF_IN: 1310240233Sglebius idx = PF_SK_WIRE; 1311240233Sglebius break; 1312240233Sglebius case PF_OUT: 1313240233Sglebius idx = PF_SK_STACK; 1314240233Sglebius break; 1315240233Sglebius case PF_INOUT: 1316240233Sglebius idx = PF_SK_WIRE; 1317240233Sglebius inout = 1; 1318240233Sglebius break; 1319240233Sglebius default: 1320240233Sglebius panic("%s: dir %u", __func__, dir); 1321240233Sglebius } 1322240233Sglebiussecond_run: 1323240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 1324240233Sglebius if (more == NULL) { 1325240233Sglebius PF_HASHROW_UNLOCK(kh); 1326240233Sglebius return (s); 1327240233Sglebius } 1328240233Sglebius 1329240233Sglebius if (ret) 1330240233Sglebius (*more)++; 1331240233Sglebius else 1332240233Sglebius ret = s; 1333240233Sglebius } 1334240233Sglebius if (inout == 1) { 1335240233Sglebius inout = 0; 1336240233Sglebius idx = PF_SK_STACK; 1337240233Sglebius goto second_run; 1338240233Sglebius } 1339240233Sglebius PF_HASHROW_UNLOCK(kh); 1340240233Sglebius 1341240233Sglebius return (ret); 1342223637Sbz} 1343223637Sbz 1344223637Sbz/* END state table stuff */ 1345223637Sbz 1346240233Sglebiusstatic void 1347240233Sglebiuspf_send(struct pf_send_entry *pfse) 1348240233Sglebius{ 1349223637Sbz 1350240233Sglebius PF_SENDQ_LOCK(); 1351240233Sglebius STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next); 1352240233Sglebius PF_SENDQ_UNLOCK(); 1353240233Sglebius swi_sched(V_pf_swi_cookie, 0); 1354240233Sglebius} 1355240233Sglebius 1356126258Smlaiervoid 1357240233Sglebiuspf_intr(void *v) 1358240233Sglebius{ 1359240233Sglebius struct pf_send_head queue; 1360240233Sglebius struct pf_send_entry *pfse, *next; 1361240233Sglebius 1362240233Sglebius CURVNET_SET((struct vnet *)v); 1363240233Sglebius 1364240233Sglebius PF_SENDQ_LOCK(); 1365240233Sglebius queue = V_pf_sendqueue; 1366240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 1367240233Sglebius PF_SENDQ_UNLOCK(); 1368240233Sglebius 1369240233Sglebius STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) { 1370240233Sglebius switch (pfse->pfse_type) { 1371240233Sglebius#ifdef INET 1372240233Sglebius case PFSE_IP: 1373240233Sglebius ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL); 1374240233Sglebius break; 1375240233Sglebius case PFSE_ICMP: 1376240233Sglebius icmp_error(pfse->pfse_m, pfse->pfse_icmp_type, 1377240233Sglebius pfse->pfse_icmp_code, 0, pfse->pfse_icmp_mtu); 1378240233Sglebius break; 1379240233Sglebius#endif /* INET */ 1380240233Sglebius#ifdef INET6 1381240233Sglebius case PFSE_IP6: 1382240233Sglebius ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL, 1383240233Sglebius NULL); 1384240233Sglebius break; 1385240233Sglebius case PFSE_ICMP6: 1386240233Sglebius icmp6_error(pfse->pfse_m, pfse->pfse_icmp_type, 1387240233Sglebius pfse->pfse_icmp_code, pfse->pfse_icmp_mtu); 1388240233Sglebius break; 1389240233Sglebius#endif /* INET6 */ 1390240233Sglebius default: 1391240233Sglebius panic("%s: unknown type", __func__); 1392240233Sglebius } 1393240233Sglebius free(pfse, M_PFTEMP); 1394240233Sglebius } 1395240233Sglebius CURVNET_RESTORE(); 1396240233Sglebius} 1397240233Sglebius 1398240233Sglebiusvoid 1399171168Smlaierpf_purge_thread(void *v) 1400126258Smlaier{ 1401241039Sglebius u_int idx = 0; 1402171168Smlaier 1403223637Sbz CURVNET_SET((struct vnet *)v); 1404223637Sbz 1405171168Smlaier for (;;) { 1406240233Sglebius PF_RULES_RLOCK(); 1407240233Sglebius rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftm", hz / 10); 1408171168Smlaier 1409226527Sbz if (V_pf_end_threads) { 1410240233Sglebius /* 1411240233Sglebius * To cleanse up all kifs and rules we need 1412240233Sglebius * two runs: first one clears reference flags, 1413240233Sglebius * then pf_purge_expired_states() doesn't 1414240233Sglebius * raise them, and then second run frees. 1415240233Sglebius */ 1416240233Sglebius PF_RULES_RUNLOCK(); 1417240233Sglebius pf_purge_unlinked_rules(); 1418240233Sglebius pfi_kif_purge(); 1419171168Smlaier 1420240233Sglebius /* 1421240233Sglebius * Now purge everything. 1422240233Sglebius */ 1423241039Sglebius pf_purge_expired_states(0, V_pf_hashmask); 1424226527Sbz pf_purge_expired_fragments(); 1425240233Sglebius pf_purge_expired_src_nodes(); 1426240233Sglebius 1427240233Sglebius /* 1428240233Sglebius * Now all kifs & rules should be unreferenced, 1429240233Sglebius * thus should be successfully freed. 1430240233Sglebius */ 1431240233Sglebius pf_purge_unlinked_rules(); 1432240233Sglebius pfi_kif_purge(); 1433240233Sglebius 1434240233Sglebius /* 1435240233Sglebius * Announce success and exit. 1436240233Sglebius */ 1437240233Sglebius PF_RULES_RLOCK(); 1438226527Sbz V_pf_end_threads++; 1439240233Sglebius PF_RULES_RUNLOCK(); 1440226527Sbz wakeup(pf_purge_thread); 1441226527Sbz kproc_exit(0); 1442226527Sbz } 1443240233Sglebius PF_RULES_RUNLOCK(); 1444126258Smlaier 1445240233Sglebius /* Process 1/interval fraction of the state table every run. */ 1446241039Sglebius idx = pf_purge_expired_states(idx, V_pf_hashmask / 1447240233Sglebius (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10)); 1448196372Smlaier 1449240233Sglebius /* Purge other expired types every PFTM_INTERVAL seconds. */ 1450241039Sglebius if (idx == 0) { 1451240233Sglebius /* 1452240233Sglebius * Order is important: 1453240233Sglebius * - states and src nodes reference rules 1454240233Sglebius * - states and rules reference kifs 1455240233Sglebius */ 1456171168Smlaier pf_purge_expired_fragments(); 1457240233Sglebius pf_purge_expired_src_nodes(); 1458240233Sglebius pf_purge_unlinked_rules(); 1459240233Sglebius pfi_kif_purge(); 1460171168Smlaier } 1461171168Smlaier } 1462240233Sglebius /* not reached */ 1463223637Sbz CURVNET_RESTORE(); 1464126258Smlaier} 1465126258Smlaier 1466126258Smlaieru_int32_t 1467126258Smlaierpf_state_expires(const struct pf_state *state) 1468126258Smlaier{ 1469126258Smlaier u_int32_t timeout; 1470126258Smlaier u_int32_t start; 1471126258Smlaier u_int32_t end; 1472126258Smlaier u_int32_t states; 1473126258Smlaier 1474126258Smlaier /* handle all PFTM_* > PFTM_MAX here */ 1475126258Smlaier if (state->timeout == PFTM_PURGE) 1476240233Sglebius return (time_uptime); 1477171168Smlaier KASSERT(state->timeout != PFTM_UNLINKED, 1478171168Smlaier ("pf_state_expires: timeout == PFTM_UNLINKED")); 1479240233Sglebius KASSERT((state->timeout < PFTM_MAX), 1480126261Smlaier ("pf_state_expires: timeout > PFTM_MAX")); 1481126258Smlaier timeout = state->rule.ptr->timeout[state->timeout]; 1482126258Smlaier if (!timeout) 1483223637Sbz timeout = V_pf_default_rule.timeout[state->timeout]; 1484126258Smlaier start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START]; 1485126258Smlaier if (start) { 1486126258Smlaier end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END]; 1487263029Sglebius states = counter_u64_fetch(state->rule.ptr->states_cur); 1488126258Smlaier } else { 1489223637Sbz start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START]; 1490223637Sbz end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END]; 1491223637Sbz states = V_pf_status.states; 1492126258Smlaier } 1493126258Smlaier if (end && states > start && start < end) { 1494126258Smlaier if (states < end) 1495126258Smlaier return (state->expire + timeout * (end - states) / 1496126258Smlaier (end - start)); 1497126258Smlaier else 1498240233Sglebius return (time_uptime); 1499126258Smlaier } 1500126258Smlaier return (state->expire + timeout); 1501126258Smlaier} 1502126258Smlaier 1503126258Smlaiervoid 1504240233Sglebiuspf_purge_expired_src_nodes() 1505126258Smlaier{ 1506261019Sglebius struct pf_src_node_list freelist; 1507240233Sglebius struct pf_srchash *sh; 1508240233Sglebius struct pf_src_node *cur, *next; 1509240233Sglebius int i; 1510126258Smlaier 1511261019Sglebius LIST_INIT(&freelist); 1512240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 1513240233Sglebius PF_HASHROW_LOCK(sh); 1514240233Sglebius LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next) 1515261018Sglebius if (cur->states == 0 && cur->expire <= time_uptime) { 1516261019Sglebius pf_unlink_src_node_locked(cur); 1517261019Sglebius LIST_INSERT_HEAD(&freelist, cur, entry); 1518240233Sglebius } else if (cur->rule.ptr != NULL) 1519240233Sglebius cur->rule.ptr->rule_flag |= PFRULE_REFS; 1520240233Sglebius PF_HASHROW_UNLOCK(sh); 1521223637Sbz } 1522261019Sglebius 1523261019Sglebius pf_free_src_nodes(&freelist); 1524270574Sglebius 1525270574Sglebius V_pf_status.src_nodes = uma_zone_get_cur(V_pf_sources_z); 1526130613Smlaier} 1527126258Smlaier 1528240233Sglebiusstatic void 1529130613Smlaierpf_src_tree_remove_state(struct pf_state *s) 1530130613Smlaier{ 1531130613Smlaier u_int32_t timeout; 1532126258Smlaier 1533130613Smlaier if (s->src_node != NULL) { 1534223637Sbz if (s->src.tcp_est) 1535223637Sbz --s->src_node->conn; 1536261018Sglebius if (--s->src_node->states == 0) { 1537130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1538130613Smlaier if (!timeout) 1539130613Smlaier timeout = 1540223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1541240233Sglebius s->src_node->expire = time_uptime + timeout; 1542130613Smlaier } 1543130613Smlaier } 1544130613Smlaier if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) { 1545261018Sglebius if (--s->nat_src_node->states == 0) { 1546130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1547130613Smlaier if (!timeout) 1548130613Smlaier timeout = 1549223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1550240233Sglebius s->nat_src_node->expire = time_uptime + timeout; 1551130613Smlaier } 1552130613Smlaier } 1553130613Smlaier s->src_node = s->nat_src_node = NULL; 1554130613Smlaier} 1555126258Smlaier 1556240233Sglebius/* 1557240233Sglebius * Unlink and potentilly free a state. Function may be 1558240233Sglebius * called with ID hash row locked, but always returns 1559240233Sglebius * unlocked, since it needs to go through key hash locking. 1560240233Sglebius */ 1561240233Sglebiusint 1562240233Sglebiuspf_unlink_state(struct pf_state *s, u_int flags) 1563145836Smlaier{ 1564240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; 1565223637Sbz 1566240233Sglebius if ((flags & PF_ENTER_LOCKED) == 0) 1567240233Sglebius PF_HASHROW_LOCK(ih); 1568240233Sglebius else 1569240233Sglebius PF_HASHROW_ASSERT(ih); 1570240233Sglebius 1571240233Sglebius if (s->timeout == PFTM_UNLINKED) { 1572240233Sglebius /* 1573240233Sglebius * State is being processed 1574240233Sglebius * by pf_unlink_state() in 1575240233Sglebius * an other thread. 1576240233Sglebius */ 1577240233Sglebius PF_HASHROW_UNLOCK(ih); 1578240233Sglebius return (0); /* XXXGL: undefined actually */ 1579240233Sglebius } 1580240233Sglebius 1581240233Sglebius if (s->src.state == PF_TCPS_PROXY_DST) { 1582223637Sbz /* XXX wire key the right one? */ 1583240233Sglebius pf_send_tcp(NULL, s->rule.ptr, s->key[PF_SK_WIRE]->af, 1584240233Sglebius &s->key[PF_SK_WIRE]->addr[1], 1585240233Sglebius &s->key[PF_SK_WIRE]->addr[0], 1586240233Sglebius s->key[PF_SK_WIRE]->port[1], 1587240233Sglebius s->key[PF_SK_WIRE]->port[0], 1588240233Sglebius s->src.seqhi, s->src.seqlo + 1, 1589240233Sglebius TH_RST|TH_ACK, 0, 0, 0, 1, s->tag, NULL); 1590171168Smlaier } 1591240233Sglebius 1592240233Sglebius LIST_REMOVE(s, entry); 1593240233Sglebius pf_src_tree_remove_state(s); 1594240233Sglebius 1595223637Sbz if (pfsync_delete_state_ptr != NULL) 1596240233Sglebius pfsync_delete_state_ptr(s); 1597240233Sglebius 1598263029Sglebius STATE_DEC_COUNTERS(s); 1599243944Sglebius 1600243941Sglebius s->timeout = PFTM_UNLINKED; 1601243941Sglebius 1602243941Sglebius PF_HASHROW_UNLOCK(ih); 1603243941Sglebius 1604240233Sglebius pf_detach_state(s); 1605240233Sglebius refcount_release(&s->refs); 1606240233Sglebius 1607240233Sglebius return (pf_release_state(s)); 1608171168Smlaier} 1609171168Smlaier 1610171168Smlaiervoid 1611171168Smlaierpf_free_state(struct pf_state *cur) 1612171168Smlaier{ 1613223637Sbz 1614240233Sglebius KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 1615240233Sglebius KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 1616240233Sglebius cur->timeout)); 1617243944Sglebius 1618145836Smlaier pf_normalize_tcp_cleanup(cur); 1619240233Sglebius uma_zfree(V_pf_state_z, cur); 1620270574Sglebius counter_u64_add(V_pf_status.fcounters[FCNT_STATE_REMOVALS], 1); 1621145836Smlaier} 1622145836Smlaier 1623240233Sglebius/* 1624240233Sglebius * Called only from pf_purge_thread(), thus serialized. 1625240233Sglebius */ 1626241039Sglebiusstatic u_int 1627241039Sglebiuspf_purge_expired_states(u_int i, int maxcheck) 1628130613Smlaier{ 1629240233Sglebius struct pf_idhash *ih; 1630240233Sglebius struct pf_state *s; 1631240233Sglebius 1632240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1633240233Sglebius 1634240233Sglebius /* 1635240233Sglebius * Go through hash and unlink states that expire now. 1636240233Sglebius */ 1637240233Sglebius while (maxcheck > 0) { 1638240233Sglebius 1639240233Sglebius ih = &V_pf_idhash[i]; 1640240233Sglebiusrelock: 1641240233Sglebius PF_HASHROW_LOCK(ih); 1642240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 1643240233Sglebius if (pf_state_expires(s) <= time_uptime) { 1644240233Sglebius V_pf_status.states -= 1645240233Sglebius pf_unlink_state(s, PF_ENTER_LOCKED); 1646240233Sglebius goto relock; 1647171168Smlaier } 1648240233Sglebius s->rule.ptr->rule_flag |= PFRULE_REFS; 1649240233Sglebius if (s->nat_rule.ptr != NULL) 1650240233Sglebius s->nat_rule.ptr->rule_flag |= PFRULE_REFS; 1651240233Sglebius if (s->anchor.ptr != NULL) 1652240233Sglebius s->anchor.ptr->rule_flag |= PFRULE_REFS; 1653240233Sglebius s->kif->pfik_flags |= PFI_IFLAG_REFS; 1654240233Sglebius if (s->rt_kif) 1655240233Sglebius s->rt_kif->pfik_flags |= PFI_IFLAG_REFS; 1656171168Smlaier } 1657240233Sglebius PF_HASHROW_UNLOCK(ih); 1658241039Sglebius 1659241039Sglebius /* Return when we hit end of hash. */ 1660241039Sglebius if (++i > V_pf_hashmask) { 1661241039Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1662241039Sglebius return (0); 1663241039Sglebius } 1664241039Sglebius 1665240233Sglebius maxcheck--; 1666126258Smlaier } 1667171168Smlaier 1668240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1669196372Smlaier 1670241039Sglebius return (i); 1671126258Smlaier} 1672126258Smlaier 1673240233Sglebiusstatic void 1674240233Sglebiuspf_purge_unlinked_rules() 1675126258Smlaier{ 1676240233Sglebius struct pf_rulequeue tmpq; 1677240233Sglebius struct pf_rule *r, *r1; 1678126258Smlaier 1679240233Sglebius /* 1680240811Sglebius * If we have overloading task pending, then we'd 1681240811Sglebius * better skip purging this time. There is a tiny 1682240811Sglebius * probability that overloading task references 1683240811Sglebius * an already unlinked rule. 1684240811Sglebius */ 1685240811Sglebius PF_OVERLOADQ_LOCK(); 1686240811Sglebius if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 1687240811Sglebius PF_OVERLOADQ_UNLOCK(); 1688240811Sglebius return; 1689240811Sglebius } 1690240811Sglebius PF_OVERLOADQ_UNLOCK(); 1691240811Sglebius 1692240811Sglebius /* 1693240233Sglebius * Do naive mark-and-sweep garbage collecting of old rules. 1694240233Sglebius * Reference flag is raised by pf_purge_expired_states() 1695240233Sglebius * and pf_purge_expired_src_nodes(). 1696240233Sglebius * 1697240233Sglebius * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 1698240233Sglebius * use a temporary queue. 1699240233Sglebius */ 1700240233Sglebius TAILQ_INIT(&tmpq); 1701240233Sglebius PF_UNLNKDRULES_LOCK(); 1702240233Sglebius TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 1703240233Sglebius if (!(r->rule_flag & PFRULE_REFS)) { 1704240233Sglebius TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 1705240233Sglebius TAILQ_INSERT_TAIL(&tmpq, r, entries); 1706240233Sglebius } else 1707240233Sglebius r->rule_flag &= ~PFRULE_REFS; 1708240233Sglebius } 1709240233Sglebius PF_UNLNKDRULES_UNLOCK(); 1710126258Smlaier 1711240233Sglebius if (!TAILQ_EMPTY(&tmpq)) { 1712240233Sglebius PF_RULES_WLOCK(); 1713240233Sglebius TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 1714240233Sglebius TAILQ_REMOVE(&tmpq, r, entries); 1715240233Sglebius pf_free_rule(r); 1716240233Sglebius } 1717240233Sglebius PF_RULES_WUNLOCK(); 1718240233Sglebius } 1719126258Smlaier} 1720126258Smlaier 1721126258Smlaiervoid 1722126258Smlaierpf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 1723126258Smlaier{ 1724126258Smlaier switch (af) { 1725126258Smlaier#ifdef INET 1726126258Smlaier case AF_INET: { 1727126258Smlaier u_int32_t a = ntohl(addr->addr32[0]); 1728126258Smlaier printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 1729126258Smlaier (a>>8)&255, a&255); 1730126258Smlaier if (p) { 1731126258Smlaier p = ntohs(p); 1732126258Smlaier printf(":%u", p); 1733126258Smlaier } 1734126258Smlaier break; 1735126258Smlaier } 1736126258Smlaier#endif /* INET */ 1737126258Smlaier#ifdef INET6 1738126258Smlaier case AF_INET6: { 1739126258Smlaier u_int16_t b; 1740223637Sbz u_int8_t i, curstart, curend, maxstart, maxend; 1741223637Sbz curstart = curend = maxstart = maxend = 255; 1742126258Smlaier for (i = 0; i < 8; i++) { 1743126258Smlaier if (!addr->addr16[i]) { 1744126258Smlaier if (curstart == 255) 1745126258Smlaier curstart = i; 1746223637Sbz curend = i; 1747126258Smlaier } else { 1748223637Sbz if ((curend - curstart) > 1749223637Sbz (maxend - maxstart)) { 1750223637Sbz maxstart = curstart; 1751223637Sbz maxend = curend; 1752126258Smlaier } 1753223637Sbz curstart = curend = 255; 1754126258Smlaier } 1755126258Smlaier } 1756223637Sbz if ((curend - curstart) > 1757223637Sbz (maxend - maxstart)) { 1758223637Sbz maxstart = curstart; 1759223637Sbz maxend = curend; 1760223637Sbz } 1761126258Smlaier for (i = 0; i < 8; i++) { 1762126258Smlaier if (i >= maxstart && i <= maxend) { 1763223637Sbz if (i == 0) 1764223637Sbz printf(":"); 1765223637Sbz if (i == maxend) 1766223637Sbz printf(":"); 1767126258Smlaier } else { 1768126258Smlaier b = ntohs(addr->addr16[i]); 1769126258Smlaier printf("%x", b); 1770126258Smlaier if (i < 7) 1771126258Smlaier printf(":"); 1772126258Smlaier } 1773126258Smlaier } 1774126258Smlaier if (p) { 1775126258Smlaier p = ntohs(p); 1776126258Smlaier printf("[%u]", p); 1777126258Smlaier } 1778126258Smlaier break; 1779126258Smlaier } 1780126258Smlaier#endif /* INET6 */ 1781126258Smlaier } 1782126258Smlaier} 1783126258Smlaier 1784126258Smlaiervoid 1785126258Smlaierpf_print_state(struct pf_state *s) 1786126258Smlaier{ 1787223637Sbz pf_print_state_parts(s, NULL, NULL); 1788223637Sbz} 1789223637Sbz 1790240233Sglebiusstatic void 1791223637Sbzpf_print_state_parts(struct pf_state *s, 1792223637Sbz struct pf_state_key *skwp, struct pf_state_key *sksp) 1793223637Sbz{ 1794223637Sbz struct pf_state_key *skw, *sks; 1795223637Sbz u_int8_t proto, dir; 1796223637Sbz 1797223637Sbz /* Do our best to fill these, but they're skipped if NULL */ 1798223637Sbz skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 1799223637Sbz sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 1800223637Sbz proto = skw ? skw->proto : (sks ? sks->proto : 0); 1801223637Sbz dir = s ? s->direction : 0; 1802223637Sbz 1803223637Sbz switch (proto) { 1804223637Sbz case IPPROTO_IPV4: 1805223637Sbz printf("IPv4"); 1806223637Sbz break; 1807223637Sbz case IPPROTO_IPV6: 1808223637Sbz printf("IPv6"); 1809223637Sbz break; 1810126258Smlaier case IPPROTO_TCP: 1811223637Sbz printf("TCP"); 1812126258Smlaier break; 1813126258Smlaier case IPPROTO_UDP: 1814223637Sbz printf("UDP"); 1815126258Smlaier break; 1816126258Smlaier case IPPROTO_ICMP: 1817223637Sbz printf("ICMP"); 1818126258Smlaier break; 1819126258Smlaier case IPPROTO_ICMPV6: 1820223637Sbz printf("ICMPv6"); 1821126258Smlaier break; 1822126258Smlaier default: 1823223637Sbz printf("%u", skw->proto); 1824126258Smlaier break; 1825126258Smlaier } 1826223637Sbz switch (dir) { 1827223637Sbz case PF_IN: 1828223637Sbz printf(" in"); 1829223637Sbz break; 1830223637Sbz case PF_OUT: 1831223637Sbz printf(" out"); 1832223637Sbz break; 1833223637Sbz } 1834223637Sbz if (skw) { 1835223637Sbz printf(" wire: "); 1836223637Sbz pf_print_host(&skw->addr[0], skw->port[0], skw->af); 1837223637Sbz printf(" "); 1838223637Sbz pf_print_host(&skw->addr[1], skw->port[1], skw->af); 1839223637Sbz } 1840223637Sbz if (sks) { 1841223637Sbz printf(" stack: "); 1842223637Sbz if (sks != skw) { 1843223637Sbz pf_print_host(&sks->addr[0], sks->port[0], sks->af); 1844223637Sbz printf(" "); 1845223637Sbz pf_print_host(&sks->addr[1], sks->port[1], sks->af); 1846223637Sbz } else 1847223637Sbz printf("-"); 1848223637Sbz } 1849223637Sbz if (s) { 1850223637Sbz if (proto == IPPROTO_TCP) { 1851223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1852223637Sbz s->src.seqlo, s->src.seqhi, 1853223637Sbz s->src.max_win, s->src.seqdiff); 1854223637Sbz if (s->src.wscale && s->dst.wscale) 1855223637Sbz printf(" wscale=%u", 1856223637Sbz s->src.wscale & PF_WSCALE_MASK); 1857223637Sbz printf("]"); 1858223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1859223637Sbz s->dst.seqlo, s->dst.seqhi, 1860223637Sbz s->dst.max_win, s->dst.seqdiff); 1861223637Sbz if (s->src.wscale && s->dst.wscale) 1862223637Sbz printf(" wscale=%u", 1863223637Sbz s->dst.wscale & PF_WSCALE_MASK); 1864223637Sbz printf("]"); 1865223637Sbz } 1866223637Sbz printf(" %u:%u", s->src.state, s->dst.state); 1867223637Sbz } 1868126258Smlaier} 1869126258Smlaier 1870126258Smlaiervoid 1871126258Smlaierpf_print_flags(u_int8_t f) 1872126258Smlaier{ 1873126258Smlaier if (f) 1874126258Smlaier printf(" "); 1875126258Smlaier if (f & TH_FIN) 1876126258Smlaier printf("F"); 1877126258Smlaier if (f & TH_SYN) 1878126258Smlaier printf("S"); 1879126258Smlaier if (f & TH_RST) 1880126258Smlaier printf("R"); 1881126258Smlaier if (f & TH_PUSH) 1882126258Smlaier printf("P"); 1883126258Smlaier if (f & TH_ACK) 1884126258Smlaier printf("A"); 1885126258Smlaier if (f & TH_URG) 1886126258Smlaier printf("U"); 1887126258Smlaier if (f & TH_ECE) 1888126258Smlaier printf("E"); 1889126258Smlaier if (f & TH_CWR) 1890126258Smlaier printf("W"); 1891126258Smlaier} 1892126258Smlaier 1893126258Smlaier#define PF_SET_SKIP_STEPS(i) \ 1894126258Smlaier do { \ 1895126258Smlaier while (head[i] != cur) { \ 1896126258Smlaier head[i]->skip[i].ptr = cur; \ 1897126258Smlaier head[i] = TAILQ_NEXT(head[i], entries); \ 1898126258Smlaier } \ 1899126258Smlaier } while (0) 1900126258Smlaier 1901126258Smlaiervoid 1902126258Smlaierpf_calc_skip_steps(struct pf_rulequeue *rules) 1903126258Smlaier{ 1904126258Smlaier struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT]; 1905126258Smlaier int i; 1906126258Smlaier 1907126258Smlaier cur = TAILQ_FIRST(rules); 1908126258Smlaier prev = cur; 1909126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1910126258Smlaier head[i] = cur; 1911126258Smlaier while (cur != NULL) { 1912126258Smlaier 1913130613Smlaier if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 1914126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_IFP); 1915126258Smlaier if (cur->direction != prev->direction) 1916126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DIR); 1917126258Smlaier if (cur->af != prev->af) 1918126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_AF); 1919126258Smlaier if (cur->proto != prev->proto) 1920126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 1921145836Smlaier if (cur->src.neg != prev->src.neg || 1922126258Smlaier pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 1923126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 1924126258Smlaier if (cur->src.port[0] != prev->src.port[0] || 1925126258Smlaier cur->src.port[1] != prev->src.port[1] || 1926126258Smlaier cur->src.port_op != prev->src.port_op) 1927126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 1928145836Smlaier if (cur->dst.neg != prev->dst.neg || 1929126258Smlaier pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 1930126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 1931126258Smlaier if (cur->dst.port[0] != prev->dst.port[0] || 1932126258Smlaier cur->dst.port[1] != prev->dst.port[1] || 1933126258Smlaier cur->dst.port_op != prev->dst.port_op) 1934126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 1935126258Smlaier 1936126258Smlaier prev = cur; 1937126258Smlaier cur = TAILQ_NEXT(cur, entries); 1938126258Smlaier } 1939126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1940126258Smlaier PF_SET_SKIP_STEPS(i); 1941126258Smlaier} 1942126258Smlaier 1943240233Sglebiusstatic int 1944126258Smlaierpf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 1945126258Smlaier{ 1946126258Smlaier if (aw1->type != aw2->type) 1947126258Smlaier return (1); 1948126258Smlaier switch (aw1->type) { 1949126258Smlaier case PF_ADDR_ADDRMASK: 1950223637Sbz case PF_ADDR_RANGE: 1951126258Smlaier if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0)) 1952126258Smlaier return (1); 1953126258Smlaier if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0)) 1954126258Smlaier return (1); 1955126258Smlaier return (0); 1956126258Smlaier case PF_ADDR_DYNIFTL: 1957130613Smlaier return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 1958126258Smlaier case PF_ADDR_NOROUTE: 1959171168Smlaier case PF_ADDR_URPFFAILED: 1960126258Smlaier return (0); 1961126258Smlaier case PF_ADDR_TABLE: 1962126258Smlaier return (aw1->p.tbl != aw2->p.tbl); 1963126258Smlaier default: 1964126258Smlaier printf("invalid address type: %d\n", aw1->type); 1965126258Smlaier return (1); 1966126258Smlaier } 1967126258Smlaier} 1968126258Smlaier 1969126258Smlaieru_int16_t 1970126258Smlaierpf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 1971126258Smlaier{ 1972126258Smlaier u_int32_t l; 1973126258Smlaier 1974126258Smlaier if (udp && !cksum) 1975126258Smlaier return (0x0000); 1976126258Smlaier l = cksum + old - new; 1977126258Smlaier l = (l >> 16) + (l & 65535); 1978126258Smlaier l = l & 65535; 1979126258Smlaier if (udp && !l) 1980126258Smlaier return (0xFFFF); 1981126258Smlaier return (l); 1982126258Smlaier} 1983126258Smlaier 1984240233Sglebiusstatic void 1985126258Smlaierpf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc, 1986126258Smlaier struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af) 1987126258Smlaier{ 1988126258Smlaier struct pf_addr ao; 1989126258Smlaier u_int16_t po = *p; 1990126258Smlaier 1991126258Smlaier PF_ACPY(&ao, a, af); 1992126258Smlaier PF_ACPY(a, an, af); 1993126258Smlaier 1994126258Smlaier *p = pn; 1995126258Smlaier 1996126258Smlaier switch (af) { 1997126258Smlaier#ifdef INET 1998126258Smlaier case AF_INET: 1999126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2000126258Smlaier ao.addr16[0], an->addr16[0], 0), 2001126258Smlaier ao.addr16[1], an->addr16[1], 0); 2002126258Smlaier *p = pn; 2003126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 2004126258Smlaier ao.addr16[0], an->addr16[0], u), 2005126258Smlaier ao.addr16[1], an->addr16[1], u), 2006126258Smlaier po, pn, u); 2007126258Smlaier break; 2008126258Smlaier#endif /* INET */ 2009126258Smlaier#ifdef INET6 2010126258Smlaier case AF_INET6: 2011126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2012126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2013126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 2014126258Smlaier ao.addr16[0], an->addr16[0], u), 2015126258Smlaier ao.addr16[1], an->addr16[1], u), 2016126258Smlaier ao.addr16[2], an->addr16[2], u), 2017126258Smlaier ao.addr16[3], an->addr16[3], u), 2018126258Smlaier ao.addr16[4], an->addr16[4], u), 2019126258Smlaier ao.addr16[5], an->addr16[5], u), 2020126258Smlaier ao.addr16[6], an->addr16[6], u), 2021126258Smlaier ao.addr16[7], an->addr16[7], u), 2022126258Smlaier po, pn, u); 2023126258Smlaier break; 2024126258Smlaier#endif /* INET6 */ 2025126258Smlaier } 2026126258Smlaier} 2027126258Smlaier 2028126258Smlaier 2029126258Smlaier/* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 2030126258Smlaiervoid 2031126258Smlaierpf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 2032126258Smlaier{ 2033126258Smlaier u_int32_t ao; 2034126258Smlaier 2035126258Smlaier memcpy(&ao, a, sizeof(ao)); 2036126258Smlaier memcpy(a, &an, sizeof(u_int32_t)); 2037126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 2038126258Smlaier ao % 65536, an % 65536, u); 2039126258Smlaier} 2040126258Smlaier 2041126258Smlaier#ifdef INET6 2042240233Sglebiusstatic void 2043126258Smlaierpf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 2044126258Smlaier{ 2045126258Smlaier struct pf_addr ao; 2046126258Smlaier 2047126258Smlaier PF_ACPY(&ao, a, AF_INET6); 2048126258Smlaier PF_ACPY(a, an, AF_INET6); 2049126258Smlaier 2050126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2051126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2052126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*c, 2053126258Smlaier ao.addr16[0], an->addr16[0], u), 2054126258Smlaier ao.addr16[1], an->addr16[1], u), 2055126258Smlaier ao.addr16[2], an->addr16[2], u), 2056126258Smlaier ao.addr16[3], an->addr16[3], u), 2057126258Smlaier ao.addr16[4], an->addr16[4], u), 2058126258Smlaier ao.addr16[5], an->addr16[5], u), 2059126258Smlaier ao.addr16[6], an->addr16[6], u), 2060126258Smlaier ao.addr16[7], an->addr16[7], u); 2061126258Smlaier} 2062126258Smlaier#endif /* INET6 */ 2063126258Smlaier 2064240233Sglebiusstatic void 2065126258Smlaierpf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 2066126258Smlaier struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 2067126258Smlaier u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 2068126258Smlaier{ 2069126258Smlaier struct pf_addr oia, ooa; 2070126258Smlaier 2071126258Smlaier PF_ACPY(&oia, ia, af); 2072223637Sbz if (oa) 2073223637Sbz PF_ACPY(&ooa, oa, af); 2074126258Smlaier 2075126258Smlaier /* Change inner protocol port, fix inner protocol checksum. */ 2076126258Smlaier if (ip != NULL) { 2077126258Smlaier u_int16_t oip = *ip; 2078223637Sbz u_int32_t opc; 2079126258Smlaier 2080126258Smlaier if (pc != NULL) 2081126258Smlaier opc = *pc; 2082126258Smlaier *ip = np; 2083126258Smlaier if (pc != NULL) 2084126258Smlaier *pc = pf_cksum_fixup(*pc, oip, *ip, u); 2085126258Smlaier *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 2086126258Smlaier if (pc != NULL) 2087126258Smlaier *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 2088126258Smlaier } 2089126258Smlaier /* Change inner ip address, fix inner ip and icmp checksums. */ 2090126258Smlaier PF_ACPY(ia, na, af); 2091126258Smlaier switch (af) { 2092126258Smlaier#ifdef INET 2093126258Smlaier case AF_INET: { 2094126258Smlaier u_int32_t oh2c = *h2c; 2095126258Smlaier 2096126258Smlaier *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 2097126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2098126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2099126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2100126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2101126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2102126258Smlaier *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 2103126258Smlaier break; 2104126258Smlaier } 2105126258Smlaier#endif /* INET */ 2106126258Smlaier#ifdef INET6 2107126258Smlaier case AF_INET6: 2108126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2109126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2110126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*ic, 2111126258Smlaier oia.addr16[0], ia->addr16[0], u), 2112126258Smlaier oia.addr16[1], ia->addr16[1], u), 2113126258Smlaier oia.addr16[2], ia->addr16[2], u), 2114126258Smlaier oia.addr16[3], ia->addr16[3], u), 2115126258Smlaier oia.addr16[4], ia->addr16[4], u), 2116126258Smlaier oia.addr16[5], ia->addr16[5], u), 2117126258Smlaier oia.addr16[6], ia->addr16[6], u), 2118126258Smlaier oia.addr16[7], ia->addr16[7], u); 2119126258Smlaier break; 2120126258Smlaier#endif /* INET6 */ 2121126258Smlaier } 2122223637Sbz /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 2123223637Sbz if (oa) { 2124223637Sbz PF_ACPY(oa, na, af); 2125223637Sbz switch (af) { 2126126258Smlaier#ifdef INET 2127223637Sbz case AF_INET: 2128223637Sbz *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 2129223637Sbz ooa.addr16[0], oa->addr16[0], 0), 2130223637Sbz ooa.addr16[1], oa->addr16[1], 0); 2131223637Sbz break; 2132126258Smlaier#endif /* INET */ 2133126258Smlaier#ifdef INET6 2134223637Sbz case AF_INET6: 2135223637Sbz *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2136223637Sbz pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2137223637Sbz pf_cksum_fixup(pf_cksum_fixup(*ic, 2138223637Sbz ooa.addr16[0], oa->addr16[0], u), 2139223637Sbz ooa.addr16[1], oa->addr16[1], u), 2140223637Sbz ooa.addr16[2], oa->addr16[2], u), 2141223637Sbz ooa.addr16[3], oa->addr16[3], u), 2142223637Sbz ooa.addr16[4], oa->addr16[4], u), 2143223637Sbz ooa.addr16[5], oa->addr16[5], u), 2144223637Sbz ooa.addr16[6], oa->addr16[6], u), 2145223637Sbz ooa.addr16[7], oa->addr16[7], u); 2146223637Sbz break; 2147126258Smlaier#endif /* INET6 */ 2148223637Sbz } 2149126258Smlaier } 2150126258Smlaier} 2151126258Smlaier 2152171168Smlaier 2153171168Smlaier/* 2154171168Smlaier * Need to modulate the sequence numbers in the TCP SACK option 2155171168Smlaier * (credits to Krzysztof Pfaff for report and patch) 2156171168Smlaier */ 2157240233Sglebiusstatic int 2158171168Smlaierpf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd, 2159171168Smlaier struct tcphdr *th, struct pf_state_peer *dst) 2160171168Smlaier{ 2161171168Smlaier int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen; 2162171168Smlaier u_int8_t opts[TCP_MAXOLEN], *opt = opts; 2163171168Smlaier int copyback = 0, i, olen; 2164171168Smlaier struct sackblk sack; 2165171168Smlaier 2166223637Sbz#define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2) 2167171168Smlaier if (hlen < TCPOLEN_SACKLEN || 2168171168Smlaier !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af)) 2169171168Smlaier return 0; 2170171168Smlaier 2171171168Smlaier while (hlen >= TCPOLEN_SACKLEN) { 2172171168Smlaier olen = opt[1]; 2173171168Smlaier switch (*opt) { 2174171168Smlaier case TCPOPT_EOL: /* FALLTHROUGH */ 2175171168Smlaier case TCPOPT_NOP: 2176171168Smlaier opt++; 2177171168Smlaier hlen--; 2178171168Smlaier break; 2179171168Smlaier case TCPOPT_SACK: 2180171168Smlaier if (olen > hlen) 2181171168Smlaier olen = hlen; 2182171168Smlaier if (olen >= TCPOLEN_SACKLEN) { 2183171168Smlaier for (i = 2; i + TCPOLEN_SACK <= olen; 2184171168Smlaier i += TCPOLEN_SACK) { 2185171168Smlaier memcpy(&sack, &opt[i], sizeof(sack)); 2186171168Smlaier pf_change_a(&sack.start, &th->th_sum, 2187171168Smlaier htonl(ntohl(sack.start) - 2188171168Smlaier dst->seqdiff), 0); 2189171168Smlaier pf_change_a(&sack.end, &th->th_sum, 2190171168Smlaier htonl(ntohl(sack.end) - 2191171168Smlaier dst->seqdiff), 0); 2192171168Smlaier memcpy(&opt[i], &sack, sizeof(sack)); 2193171168Smlaier } 2194171168Smlaier copyback = 1; 2195171168Smlaier } 2196171168Smlaier /* FALLTHROUGH */ 2197171168Smlaier default: 2198171168Smlaier if (olen < 2) 2199171168Smlaier olen = 2; 2200171168Smlaier hlen -= olen; 2201171168Smlaier opt += olen; 2202171168Smlaier } 2203171168Smlaier } 2204171168Smlaier 2205171168Smlaier if (copyback) 2206171168Smlaier m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts); 2207171168Smlaier return (copyback); 2208171168Smlaier} 2209171168Smlaier 2210240233Sglebiusstatic void 2211162238Scsjppf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af, 2212126258Smlaier const struct pf_addr *saddr, const struct pf_addr *daddr, 2213126258Smlaier u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 2214145836Smlaier u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag, 2215240233Sglebius u_int16_t rtag, struct ifnet *ifp) 2216126258Smlaier{ 2217240233Sglebius struct pf_send_entry *pfse; 2218126258Smlaier struct mbuf *m; 2219171168Smlaier int len, tlen; 2220126258Smlaier#ifdef INET 2221240233Sglebius struct ip *h = NULL; 2222126258Smlaier#endif /* INET */ 2223126258Smlaier#ifdef INET6 2224240233Sglebius struct ip6_hdr *h6 = NULL; 2225126258Smlaier#endif /* INET6 */ 2226171168Smlaier struct tcphdr *th; 2227171168Smlaier char *opt; 2228223637Sbz struct pf_mtag *pf_mtag; 2229126258Smlaier 2230171168Smlaier len = 0; 2231171168Smlaier th = NULL; 2232171168Smlaier 2233126258Smlaier /* maximum segment size tcp option */ 2234126258Smlaier tlen = sizeof(struct tcphdr); 2235126258Smlaier if (mss) 2236126258Smlaier tlen += 4; 2237126258Smlaier 2238126258Smlaier switch (af) { 2239126258Smlaier#ifdef INET 2240126258Smlaier case AF_INET: 2241126258Smlaier len = sizeof(struct ip) + tlen; 2242126258Smlaier break; 2243126258Smlaier#endif /* INET */ 2244126258Smlaier#ifdef INET6 2245126258Smlaier case AF_INET6: 2246126258Smlaier len = sizeof(struct ip6_hdr) + tlen; 2247126258Smlaier break; 2248126258Smlaier#endif /* INET6 */ 2249240233Sglebius default: 2250240233Sglebius panic("%s: unsupported af %d", __func__, af); 2251126258Smlaier } 2252126258Smlaier 2253240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2254240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2255240233Sglebius if (pfse == NULL) 2256132280Smlaier return; 2257248324Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 2258240233Sglebius if (m == NULL) { 2259240233Sglebius free(pfse, M_PFTEMP); 2260240233Sglebius return; 2261240233Sglebius } 2262162238Scsjp#ifdef MAC 2263223637Sbz mac_netinet_firewall_send(m); 2264162238Scsjp#endif 2265171168Smlaier if ((pf_mtag = pf_get_mtag(m)) == NULL) { 2266240233Sglebius free(pfse, M_PFTEMP); 2267171168Smlaier m_freem(m); 2268171168Smlaier return; 2269171168Smlaier } 2270171168Smlaier if (tag) 2271145836Smlaier m->m_flags |= M_SKIP_FIREWALL; 2272223637Sbz pf_mtag->tag = rtag; 2273145836Smlaier 2274171168Smlaier if (r != NULL && r->rtableid >= 0) 2275178888Sjulian M_SETFIB(m, r->rtableid); 2276223637Sbz 2277126258Smlaier#ifdef ALTQ 2278126258Smlaier if (r != NULL && r->qid) { 2279171168Smlaier pf_mtag->qid = r->qid; 2280223637Sbz 2281171168Smlaier /* add hints for ecn */ 2282171168Smlaier pf_mtag->hdr = mtod(m, struct ip *); 2283126258Smlaier } 2284145836Smlaier#endif /* ALTQ */ 2285126258Smlaier m->m_data += max_linkhdr; 2286126258Smlaier m->m_pkthdr.len = m->m_len = len; 2287126258Smlaier m->m_pkthdr.rcvif = NULL; 2288126258Smlaier bzero(m->m_data, len); 2289126258Smlaier switch (af) { 2290126258Smlaier#ifdef INET 2291126258Smlaier case AF_INET: 2292126258Smlaier h = mtod(m, struct ip *); 2293126258Smlaier 2294126258Smlaier /* IP header fields included in the TCP checksum */ 2295126258Smlaier h->ip_p = IPPROTO_TCP; 2296126258Smlaier h->ip_len = htons(tlen); 2297126258Smlaier h->ip_src.s_addr = saddr->v4.s_addr; 2298126258Smlaier h->ip_dst.s_addr = daddr->v4.s_addr; 2299126258Smlaier 2300126258Smlaier th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 2301126258Smlaier break; 2302126258Smlaier#endif /* INET */ 2303126258Smlaier#ifdef INET6 2304126258Smlaier case AF_INET6: 2305126258Smlaier h6 = mtod(m, struct ip6_hdr *); 2306126258Smlaier 2307126258Smlaier /* IP header fields included in the TCP checksum */ 2308126258Smlaier h6->ip6_nxt = IPPROTO_TCP; 2309126258Smlaier h6->ip6_plen = htons(tlen); 2310126258Smlaier memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 2311126258Smlaier memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 2312126258Smlaier 2313126258Smlaier th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 2314126258Smlaier break; 2315126258Smlaier#endif /* INET6 */ 2316126258Smlaier } 2317126258Smlaier 2318126258Smlaier /* TCP header */ 2319126258Smlaier th->th_sport = sport; 2320126258Smlaier th->th_dport = dport; 2321126258Smlaier th->th_seq = htonl(seq); 2322126258Smlaier th->th_ack = htonl(ack); 2323126258Smlaier th->th_off = tlen >> 2; 2324126258Smlaier th->th_flags = flags; 2325126258Smlaier th->th_win = htons(win); 2326126258Smlaier 2327126258Smlaier if (mss) { 2328126258Smlaier opt = (char *)(th + 1); 2329126258Smlaier opt[0] = TCPOPT_MAXSEG; 2330126258Smlaier opt[1] = 4; 2331126258Smlaier HTONS(mss); 2332126258Smlaier bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2); 2333126258Smlaier } 2334126258Smlaier 2335126258Smlaier switch (af) { 2336126258Smlaier#ifdef INET 2337126258Smlaier case AF_INET: 2338126258Smlaier /* TCP checksum */ 2339126258Smlaier th->th_sum = in_cksum(m, len); 2340126258Smlaier 2341126258Smlaier /* Finish the IP header */ 2342126258Smlaier h->ip_v = 4; 2343126258Smlaier h->ip_hl = sizeof(*h) >> 2; 2344126258Smlaier h->ip_tos = IPTOS_LOWDELAY; 2345241913Sglebius h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 2346241913Sglebius h->ip_len = htons(len); 2347223637Sbz h->ip_ttl = ttl ? ttl : V_ip_defttl; 2348126258Smlaier h->ip_sum = 0; 2349145836Smlaier 2350240233Sglebius pfse->pfse_type = PFSE_IP; 2351126258Smlaier break; 2352126258Smlaier#endif /* INET */ 2353126258Smlaier#ifdef INET6 2354126258Smlaier case AF_INET6: 2355126258Smlaier /* TCP checksum */ 2356126258Smlaier th->th_sum = in6_cksum(m, IPPROTO_TCP, 2357126258Smlaier sizeof(struct ip6_hdr), tlen); 2358126258Smlaier 2359126258Smlaier h6->ip6_vfc |= IPV6_VERSION; 2360126258Smlaier h6->ip6_hlim = IPV6_DEFHLIM; 2361126258Smlaier 2362240233Sglebius pfse->pfse_type = PFSE_IP6; 2363126258Smlaier break; 2364126258Smlaier#endif /* INET6 */ 2365126258Smlaier } 2366240233Sglebius pfse->pfse_m = m; 2367240233Sglebius pf_send(pfse); 2368126258Smlaier} 2369126258Smlaier 2370223637Sbzstatic void 2371126258Smlaierpf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af, 2372126258Smlaier struct pf_rule *r) 2373126258Smlaier{ 2374240233Sglebius struct pf_send_entry *pfse; 2375240233Sglebius struct mbuf *m0; 2376223637Sbz struct pf_mtag *pf_mtag; 2377126258Smlaier 2378240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2379240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2380240233Sglebius if (pfse == NULL) 2381132280Smlaier return; 2382240233Sglebius 2383240233Sglebius if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 2384240233Sglebius free(pfse, M_PFTEMP); 2385223637Sbz return; 2386240233Sglebius } 2387223637Sbz 2388240233Sglebius if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 2389240233Sglebius free(pfse, M_PFTEMP); 2390126258Smlaier return; 2391240233Sglebius } 2392171168Smlaier /* XXX: revisit */ 2393171168Smlaier m0->m_flags |= M_SKIP_FIREWALL; 2394126258Smlaier 2395171168Smlaier if (r->rtableid >= 0) 2396178888Sjulian M_SETFIB(m0, r->rtableid); 2397171168Smlaier 2398126258Smlaier#ifdef ALTQ 2399126258Smlaier if (r->qid) { 2400171168Smlaier pf_mtag->qid = r->qid; 2401171168Smlaier /* add hints for ecn */ 2402171168Smlaier pf_mtag->hdr = mtod(m0, struct ip *); 2403126258Smlaier } 2404145836Smlaier#endif /* ALTQ */ 2405126258Smlaier 2406126258Smlaier switch (af) { 2407126258Smlaier#ifdef INET 2408126258Smlaier case AF_INET: 2409240233Sglebius pfse->pfse_type = PFSE_ICMP; 2410126258Smlaier break; 2411126258Smlaier#endif /* INET */ 2412126258Smlaier#ifdef INET6 2413126258Smlaier case AF_INET6: 2414240233Sglebius pfse->pfse_type = PFSE_ICMP6; 2415126258Smlaier break; 2416126258Smlaier#endif /* INET6 */ 2417126258Smlaier } 2418240233Sglebius pfse->pfse_m = m0; 2419240233Sglebius pfse->pfse_icmp_type = type; 2420240233Sglebius pfse->pfse_icmp_code = code; 2421240233Sglebius pf_send(pfse); 2422126258Smlaier} 2423126258Smlaier 2424126258Smlaier/* 2425126258Smlaier * Return 1 if the addresses a and b match (with mask m), otherwise return 0. 2426126258Smlaier * If n is 0, they match if they are equal. If n is != 0, they match if they 2427126258Smlaier * are different. 2428126258Smlaier */ 2429126258Smlaierint 2430126258Smlaierpf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, 2431126258Smlaier struct pf_addr *b, sa_family_t af) 2432126258Smlaier{ 2433126258Smlaier int match = 0; 2434126258Smlaier 2435126258Smlaier switch (af) { 2436126258Smlaier#ifdef INET 2437126258Smlaier case AF_INET: 2438126258Smlaier if ((a->addr32[0] & m->addr32[0]) == 2439126258Smlaier (b->addr32[0] & m->addr32[0])) 2440126258Smlaier match++; 2441126258Smlaier break; 2442126258Smlaier#endif /* INET */ 2443126258Smlaier#ifdef INET6 2444126258Smlaier case AF_INET6: 2445126258Smlaier if (((a->addr32[0] & m->addr32[0]) == 2446126258Smlaier (b->addr32[0] & m->addr32[0])) && 2447126258Smlaier ((a->addr32[1] & m->addr32[1]) == 2448126258Smlaier (b->addr32[1] & m->addr32[1])) && 2449126258Smlaier ((a->addr32[2] & m->addr32[2]) == 2450126258Smlaier (b->addr32[2] & m->addr32[2])) && 2451126258Smlaier ((a->addr32[3] & m->addr32[3]) == 2452126258Smlaier (b->addr32[3] & m->addr32[3]))) 2453126258Smlaier match++; 2454126258Smlaier break; 2455126258Smlaier#endif /* INET6 */ 2456126258Smlaier } 2457126258Smlaier if (match) { 2458126258Smlaier if (n) 2459126258Smlaier return (0); 2460126258Smlaier else 2461126258Smlaier return (1); 2462126258Smlaier } else { 2463126258Smlaier if (n) 2464126258Smlaier return (1); 2465126258Smlaier else 2466126258Smlaier return (0); 2467126258Smlaier } 2468126258Smlaier} 2469126258Smlaier 2470223637Sbz/* 2471223637Sbz * Return 1 if b <= a <= e, otherwise return 0. 2472223637Sbz */ 2473126258Smlaierint 2474223637Sbzpf_match_addr_range(struct pf_addr *b, struct pf_addr *e, 2475223637Sbz struct pf_addr *a, sa_family_t af) 2476223637Sbz{ 2477223637Sbz switch (af) { 2478223637Sbz#ifdef INET 2479223637Sbz case AF_INET: 2480223637Sbz if ((a->addr32[0] < b->addr32[0]) || 2481223637Sbz (a->addr32[0] > e->addr32[0])) 2482223637Sbz return (0); 2483223637Sbz break; 2484223637Sbz#endif /* INET */ 2485223637Sbz#ifdef INET6 2486223637Sbz case AF_INET6: { 2487223637Sbz int i; 2488223637Sbz 2489223637Sbz /* check a >= b */ 2490223637Sbz for (i = 0; i < 4; ++i) 2491223637Sbz if (a->addr32[i] > b->addr32[i]) 2492223637Sbz break; 2493223637Sbz else if (a->addr32[i] < b->addr32[i]) 2494223637Sbz return (0); 2495223637Sbz /* check a <= e */ 2496223637Sbz for (i = 0; i < 4; ++i) 2497223637Sbz if (a->addr32[i] < e->addr32[i]) 2498223637Sbz break; 2499223637Sbz else if (a->addr32[i] > e->addr32[i]) 2500223637Sbz return (0); 2501223637Sbz break; 2502223637Sbz } 2503223637Sbz#endif /* INET6 */ 2504223637Sbz } 2505223637Sbz return (1); 2506223637Sbz} 2507223637Sbz 2508240233Sglebiusstatic int 2509126258Smlaierpf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 2510126258Smlaier{ 2511126258Smlaier switch (op) { 2512126258Smlaier case PF_OP_IRG: 2513126258Smlaier return ((p > a1) && (p < a2)); 2514126258Smlaier case PF_OP_XRG: 2515126258Smlaier return ((p < a1) || (p > a2)); 2516126258Smlaier case PF_OP_RRG: 2517126258Smlaier return ((p >= a1) && (p <= a2)); 2518126258Smlaier case PF_OP_EQ: 2519126258Smlaier return (p == a1); 2520126258Smlaier case PF_OP_NE: 2521126258Smlaier return (p != a1); 2522126258Smlaier case PF_OP_LT: 2523126258Smlaier return (p < a1); 2524126258Smlaier case PF_OP_LE: 2525126258Smlaier return (p <= a1); 2526126258Smlaier case PF_OP_GT: 2527126258Smlaier return (p > a1); 2528126258Smlaier case PF_OP_GE: 2529126258Smlaier return (p >= a1); 2530126258Smlaier } 2531126258Smlaier return (0); /* never reached */ 2532126258Smlaier} 2533126258Smlaier 2534126258Smlaierint 2535126258Smlaierpf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 2536126258Smlaier{ 2537126258Smlaier NTOHS(a1); 2538126258Smlaier NTOHS(a2); 2539126258Smlaier NTOHS(p); 2540126258Smlaier return (pf_match(op, a1, a2, p)); 2541126258Smlaier} 2542126258Smlaier 2543240233Sglebiusstatic int 2544126258Smlaierpf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 2545126258Smlaier{ 2546126258Smlaier if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2547126258Smlaier return (0); 2548126258Smlaier return (pf_match(op, a1, a2, u)); 2549126258Smlaier} 2550126258Smlaier 2551240233Sglebiusstatic int 2552126258Smlaierpf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 2553126258Smlaier{ 2554126258Smlaier if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2555126258Smlaier return (0); 2556126258Smlaier return (pf_match(op, a1, a2, g)); 2557126258Smlaier} 2558126258Smlaier 2559223637Sbzint 2560240233Sglebiuspf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag, int mtag) 2561171168Smlaier{ 2562171168Smlaier if (*tag == -1) 2563240233Sglebius *tag = mtag; 2564171168Smlaier 2565126258Smlaier return ((!r->match_tag_not && r->match_tag == *tag) || 2566126258Smlaier (r->match_tag_not && r->match_tag != *tag)); 2567126258Smlaier} 2568126258Smlaier 2569126258Smlaierint 2570240233Sglebiuspf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag) 2571126258Smlaier{ 2572126258Smlaier 2573240233Sglebius KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 2574126258Smlaier 2575240233Sglebius if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL)) 2576240233Sglebius return (ENOMEM); 2577240233Sglebius 2578240233Sglebius pd->pf_mtag->tag = tag; 2579240233Sglebius 2580126258Smlaier return (0); 2581126258Smlaier} 2582126258Smlaier 2583240641Sglebius#define PF_ANCHOR_STACKSIZE 32 2584240641Sglebiusstruct pf_anchor_stackframe { 2585240641Sglebius struct pf_ruleset *rs; 2586240641Sglebius struct pf_rule *r; /* XXX: + match bit */ 2587240641Sglebius struct pf_anchor *child; 2588240641Sglebius}; 2589240641Sglebius 2590240641Sglebius/* 2591240641Sglebius * XXX: We rely on malloc(9) returning pointer aligned addresses. 2592240641Sglebius */ 2593240641Sglebius#define PF_ANCHORSTACK_MATCH 0x00000001 2594240641Sglebius#define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 2595240641Sglebius 2596240641Sglebius#define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 2597240641Sglebius#define PF_ANCHOR_RULE(f) (struct pf_rule *) \ 2598240641Sglebius ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 2599240641Sglebius#define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 2600240641Sglebius ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 2601240641Sglebius} while (0) 2602240641Sglebius 2603223637Sbzvoid 2604240641Sglebiuspf_step_into_anchor(struct pf_anchor_stackframe *stack, int *depth, 2605240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2606240641Sglebius int *match) 2607145836Smlaier{ 2608145836Smlaier struct pf_anchor_stackframe *f; 2609126258Smlaier 2610240233Sglebius PF_RULES_RASSERT(); 2611240233Sglebius 2612171168Smlaier if (match) 2613171168Smlaier *match = 0; 2614240641Sglebius if (*depth >= PF_ANCHOR_STACKSIZE) { 2615240641Sglebius printf("%s: anchor stack overflow on %s\n", 2616240641Sglebius __func__, (*r)->anchor->name); 2617145836Smlaier *r = TAILQ_NEXT(*r, entries); 2618145836Smlaier return; 2619145836Smlaier } else if (*depth == 0 && a != NULL) 2620145836Smlaier *a = *r; 2621240641Sglebius f = stack + (*depth)++; 2622145836Smlaier f->rs = *rs; 2623145836Smlaier f->r = *r; 2624145836Smlaier if ((*r)->anchor_wildcard) { 2625240641Sglebius struct pf_anchor_node *parent = &(*r)->anchor->children; 2626240641Sglebius 2627240641Sglebius if ((f->child = RB_MIN(pf_anchor_node, parent)) == NULL) { 2628145836Smlaier *r = NULL; 2629145836Smlaier return; 2630145836Smlaier } 2631145836Smlaier *rs = &f->child->ruleset; 2632145836Smlaier } else { 2633145836Smlaier f->child = NULL; 2634145836Smlaier *rs = &(*r)->anchor->ruleset; 2635145836Smlaier } 2636145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2637145836Smlaier} 2638126258Smlaier 2639171168Smlaierint 2640240641Sglebiuspf_step_out_of_anchor(struct pf_anchor_stackframe *stack, int *depth, 2641240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2642240641Sglebius int *match) 2643145836Smlaier{ 2644145836Smlaier struct pf_anchor_stackframe *f; 2645240641Sglebius struct pf_rule *fr; 2646171168Smlaier int quick = 0; 2647145836Smlaier 2648240233Sglebius PF_RULES_RASSERT(); 2649240233Sglebius 2650145836Smlaier do { 2651145836Smlaier if (*depth <= 0) 2652145836Smlaier break; 2653240641Sglebius f = stack + *depth - 1; 2654240641Sglebius fr = PF_ANCHOR_RULE(f); 2655240641Sglebius if (f->child != NULL) { 2656240641Sglebius struct pf_anchor_node *parent; 2657240641Sglebius 2658240641Sglebius /* 2659240641Sglebius * This block traverses through 2660240641Sglebius * a wildcard anchor. 2661240641Sglebius */ 2662240641Sglebius parent = &fr->anchor->children; 2663240641Sglebius if (match != NULL && *match) { 2664240641Sglebius /* 2665240641Sglebius * If any of "*" matched, then 2666240641Sglebius * "foo/ *" matched, mark frame 2667240641Sglebius * appropriately. 2668240641Sglebius */ 2669240641Sglebius PF_ANCHOR_SET_MATCH(f); 2670171168Smlaier *match = 0; 2671171168Smlaier } 2672240641Sglebius f->child = RB_NEXT(pf_anchor_node, parent, f->child); 2673145836Smlaier if (f->child != NULL) { 2674145836Smlaier *rs = &f->child->ruleset; 2675145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2676145836Smlaier if (*r == NULL) 2677145836Smlaier continue; 2678145836Smlaier else 2679145836Smlaier break; 2680145836Smlaier } 2681145836Smlaier } 2682145836Smlaier (*depth)--; 2683145836Smlaier if (*depth == 0 && a != NULL) 2684145836Smlaier *a = NULL; 2685145836Smlaier *rs = f->rs; 2686240641Sglebius if (PF_ANCHOR_MATCH(f) || (match != NULL && *match)) 2687240641Sglebius quick = fr->quick; 2688240641Sglebius *r = TAILQ_NEXT(fr, entries); 2689145836Smlaier } while (*r == NULL); 2690171168Smlaier 2691171168Smlaier return (quick); 2692145836Smlaier} 2693145836Smlaier 2694126258Smlaier#ifdef INET6 2695126258Smlaiervoid 2696126258Smlaierpf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 2697126258Smlaier struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 2698126258Smlaier{ 2699126258Smlaier switch (af) { 2700126258Smlaier#ifdef INET 2701126258Smlaier case AF_INET: 2702126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2703126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2704126258Smlaier break; 2705126258Smlaier#endif /* INET */ 2706126258Smlaier case AF_INET6: 2707126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2708126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2709126258Smlaier naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 2710126258Smlaier ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 2711126258Smlaier naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 2712126258Smlaier ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 2713126258Smlaier naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 2714126258Smlaier ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 2715126258Smlaier break; 2716126258Smlaier } 2717126258Smlaier} 2718126258Smlaier 2719126258Smlaiervoid 2720130613Smlaierpf_addr_inc(struct pf_addr *addr, sa_family_t af) 2721126258Smlaier{ 2722126258Smlaier switch (af) { 2723126258Smlaier#ifdef INET 2724126258Smlaier case AF_INET: 2725126258Smlaier addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 2726126258Smlaier break; 2727126258Smlaier#endif /* INET */ 2728126258Smlaier case AF_INET6: 2729126258Smlaier if (addr->addr32[3] == 0xffffffff) { 2730126258Smlaier addr->addr32[3] = 0; 2731126258Smlaier if (addr->addr32[2] == 0xffffffff) { 2732126258Smlaier addr->addr32[2] = 0; 2733126258Smlaier if (addr->addr32[1] == 0xffffffff) { 2734126258Smlaier addr->addr32[1] = 0; 2735126258Smlaier addr->addr32[0] = 2736126258Smlaier htonl(ntohl(addr->addr32[0]) + 1); 2737126258Smlaier } else 2738126258Smlaier addr->addr32[1] = 2739126258Smlaier htonl(ntohl(addr->addr32[1]) + 1); 2740126258Smlaier } else 2741126258Smlaier addr->addr32[2] = 2742126258Smlaier htonl(ntohl(addr->addr32[2]) + 1); 2743126258Smlaier } else 2744126258Smlaier addr->addr32[3] = 2745126258Smlaier htonl(ntohl(addr->addr32[3]) + 1); 2746126258Smlaier break; 2747126258Smlaier } 2748126258Smlaier} 2749126258Smlaier#endif /* INET6 */ 2750126258Smlaier 2751126258Smlaierint 2752240233Sglebiuspf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m) 2753126258Smlaier{ 2754126258Smlaier struct pf_addr *saddr, *daddr; 2755126258Smlaier u_int16_t sport, dport; 2756126261Smlaier struct inpcbinfo *pi; 2757126258Smlaier struct inpcb *inp; 2758126258Smlaier 2759171168Smlaier pd->lookup.uid = UID_MAX; 2760171168Smlaier pd->lookup.gid = GID_MAX; 2761223637Sbz 2762130613Smlaier switch (pd->proto) { 2763126258Smlaier case IPPROTO_TCP: 2764171168Smlaier if (pd->hdr.tcp == NULL) 2765171168Smlaier return (-1); 2766126258Smlaier sport = pd->hdr.tcp->th_sport; 2767126258Smlaier dport = pd->hdr.tcp->th_dport; 2768181803Sbz pi = &V_tcbinfo; 2769126258Smlaier break; 2770126258Smlaier case IPPROTO_UDP: 2771171168Smlaier if (pd->hdr.udp == NULL) 2772171168Smlaier return (-1); 2773126258Smlaier sport = pd->hdr.udp->uh_sport; 2774126258Smlaier dport = pd->hdr.udp->uh_dport; 2775181803Sbz pi = &V_udbinfo; 2776126258Smlaier break; 2777126258Smlaier default: 2778171168Smlaier return (-1); 2779126258Smlaier } 2780126258Smlaier if (direction == PF_IN) { 2781126258Smlaier saddr = pd->src; 2782126258Smlaier daddr = pd->dst; 2783126258Smlaier } else { 2784126258Smlaier u_int16_t p; 2785126258Smlaier 2786126258Smlaier p = sport; 2787126258Smlaier sport = dport; 2788126258Smlaier dport = p; 2789126258Smlaier saddr = pd->dst; 2790126258Smlaier daddr = pd->src; 2791126258Smlaier } 2792130613Smlaier switch (pd->af) { 2793145836Smlaier#ifdef INET 2794126258Smlaier case AF_INET: 2795240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 2796240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2797126261Smlaier if (inp == NULL) { 2798240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 2799222488Srwatson daddr->v4, dport, INPLOOKUP_WILDCARD | 2800240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2801222488Srwatson if (inp == NULL) 2802171168Smlaier return (-1); 2803126261Smlaier } 2804126258Smlaier break; 2805145836Smlaier#endif /* INET */ 2806126258Smlaier#ifdef INET6 2807126258Smlaier case AF_INET6: 2808240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 2809240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2810126261Smlaier if (inp == NULL) { 2811240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 2812222488Srwatson &daddr->v6, dport, INPLOOKUP_WILDCARD | 2813240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2814222488Srwatson if (inp == NULL) 2815171168Smlaier return (-1); 2816126261Smlaier } 2817126258Smlaier break; 2818126258Smlaier#endif /* INET6 */ 2819126258Smlaier 2820126258Smlaier default: 2821171168Smlaier return (-1); 2822126258Smlaier } 2823222488Srwatson INP_RLOCK_ASSERT(inp); 2824183606Sbz pd->lookup.uid = inp->inp_cred->cr_uid; 2825183606Sbz pd->lookup.gid = inp->inp_cred->cr_groups[0]; 2826222488Srwatson INP_RUNLOCK(inp); 2827240233Sglebius 2828126258Smlaier return (1); 2829126258Smlaier} 2830126258Smlaier 2831240233Sglebiusstatic u_int8_t 2832126258Smlaierpf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2833126258Smlaier{ 2834126258Smlaier int hlen; 2835126258Smlaier u_int8_t hdr[60]; 2836126258Smlaier u_int8_t *opt, optlen; 2837126258Smlaier u_int8_t wscale = 0; 2838126258Smlaier 2839126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2840126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2841126258Smlaier return (0); 2842126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2843126258Smlaier return (0); 2844126258Smlaier opt = hdr + sizeof(struct tcphdr); 2845126258Smlaier hlen -= sizeof(struct tcphdr); 2846126258Smlaier while (hlen >= 3) { 2847126258Smlaier switch (*opt) { 2848126258Smlaier case TCPOPT_EOL: 2849126258Smlaier case TCPOPT_NOP: 2850126258Smlaier ++opt; 2851126258Smlaier --hlen; 2852126258Smlaier break; 2853126258Smlaier case TCPOPT_WINDOW: 2854126258Smlaier wscale = opt[2]; 2855126258Smlaier if (wscale > TCP_MAX_WINSHIFT) 2856126258Smlaier wscale = TCP_MAX_WINSHIFT; 2857126258Smlaier wscale |= PF_WSCALE_FLAG; 2858130613Smlaier /* FALLTHROUGH */ 2859126258Smlaier default: 2860126258Smlaier optlen = opt[1]; 2861126258Smlaier if (optlen < 2) 2862126258Smlaier optlen = 2; 2863126258Smlaier hlen -= optlen; 2864126258Smlaier opt += optlen; 2865130613Smlaier break; 2866126258Smlaier } 2867126258Smlaier } 2868126258Smlaier return (wscale); 2869126258Smlaier} 2870126258Smlaier 2871240233Sglebiusstatic u_int16_t 2872126258Smlaierpf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2873126258Smlaier{ 2874126258Smlaier int hlen; 2875126258Smlaier u_int8_t hdr[60]; 2876126258Smlaier u_int8_t *opt, optlen; 2877181803Sbz u_int16_t mss = V_tcp_mssdflt; 2878126258Smlaier 2879126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2880126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2881126258Smlaier return (0); 2882126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2883126258Smlaier return (0); 2884126258Smlaier opt = hdr + sizeof(struct tcphdr); 2885126258Smlaier hlen -= sizeof(struct tcphdr); 2886126258Smlaier while (hlen >= TCPOLEN_MAXSEG) { 2887126258Smlaier switch (*opt) { 2888126258Smlaier case TCPOPT_EOL: 2889126258Smlaier case TCPOPT_NOP: 2890126258Smlaier ++opt; 2891126258Smlaier --hlen; 2892126258Smlaier break; 2893126258Smlaier case TCPOPT_MAXSEG: 2894126258Smlaier bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2); 2895145030Sglebius NTOHS(mss); 2896130613Smlaier /* FALLTHROUGH */ 2897126258Smlaier default: 2898126258Smlaier optlen = opt[1]; 2899126258Smlaier if (optlen < 2) 2900126258Smlaier optlen = 2; 2901126258Smlaier hlen -= optlen; 2902126258Smlaier opt += optlen; 2903130613Smlaier break; 2904126258Smlaier } 2905126258Smlaier } 2906126258Smlaier return (mss); 2907126258Smlaier} 2908126258Smlaier 2909240233Sglebiusstatic u_int16_t 2910231852Sbzpf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 2911126258Smlaier{ 2912126258Smlaier#ifdef INET 2913126258Smlaier struct sockaddr_in *dst; 2914126258Smlaier struct route ro; 2915126258Smlaier#endif /* INET */ 2916126258Smlaier#ifdef INET6 2917126258Smlaier struct sockaddr_in6 *dst6; 2918126258Smlaier struct route_in6 ro6; 2919126258Smlaier#endif /* INET6 */ 2920126258Smlaier struct rtentry *rt = NULL; 2921223637Sbz int hlen = 0; 2922181803Sbz u_int16_t mss = V_tcp_mssdflt; 2923126258Smlaier 2924126258Smlaier switch (af) { 2925126258Smlaier#ifdef INET 2926126258Smlaier case AF_INET: 2927126258Smlaier hlen = sizeof(struct ip); 2928126258Smlaier bzero(&ro, sizeof(ro)); 2929126258Smlaier dst = (struct sockaddr_in *)&ro.ro_dst; 2930126258Smlaier dst->sin_family = AF_INET; 2931126258Smlaier dst->sin_len = sizeof(*dst); 2932126258Smlaier dst->sin_addr = addr->v4; 2933231852Sbz in_rtalloc_ign(&ro, 0, rtableid); 2934126258Smlaier rt = ro.ro_rt; 2935126258Smlaier break; 2936126258Smlaier#endif /* INET */ 2937126258Smlaier#ifdef INET6 2938126258Smlaier case AF_INET6: 2939126258Smlaier hlen = sizeof(struct ip6_hdr); 2940126258Smlaier bzero(&ro6, sizeof(ro6)); 2941126258Smlaier dst6 = (struct sockaddr_in6 *)&ro6.ro_dst; 2942126258Smlaier dst6->sin6_family = AF_INET6; 2943126258Smlaier dst6->sin6_len = sizeof(*dst6); 2944126258Smlaier dst6->sin6_addr = addr->v6; 2945231852Sbz in6_rtalloc_ign(&ro6, 0, rtableid); 2946126258Smlaier rt = ro6.ro_rt; 2947126258Smlaier break; 2948126258Smlaier#endif /* INET6 */ 2949126258Smlaier } 2950126258Smlaier 2951126258Smlaier if (rt && rt->rt_ifp) { 2952126258Smlaier mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr); 2953181803Sbz mss = max(V_tcp_mssdflt, mss); 2954126258Smlaier RTFREE(rt); 2955126258Smlaier } 2956126258Smlaier mss = min(mss, offer); 2957126258Smlaier mss = max(mss, 64); /* sanity - at least max opt space */ 2958126258Smlaier return (mss); 2959126258Smlaier} 2960126258Smlaier 2961240233Sglebiusstatic u_int32_t 2962223637Sbzpf_tcp_iss(struct pf_pdesc *pd) 2963223637Sbz{ 2964223637Sbz MD5_CTX ctx; 2965223637Sbz u_int32_t digest[4]; 2966223637Sbz 2967223637Sbz if (V_pf_tcp_secret_init == 0) { 2968223637Sbz read_random(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 2969223637Sbz MD5Init(&V_pf_tcp_secret_ctx); 2970223637Sbz MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 2971223637Sbz sizeof(V_pf_tcp_secret)); 2972223637Sbz V_pf_tcp_secret_init = 1; 2973223637Sbz } 2974223637Sbz 2975223637Sbz ctx = V_pf_tcp_secret_ctx; 2976223637Sbz 2977223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short)); 2978223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short)); 2979223637Sbz if (pd->af == AF_INET6) { 2980223637Sbz MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr)); 2981223637Sbz MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr)); 2982223637Sbz } else { 2983223637Sbz MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr)); 2984223637Sbz MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr)); 2985223637Sbz } 2986223637Sbz MD5Final((u_char *)digest, &ctx); 2987223637Sbz V_pf_tcp_iss_off += 4096; 2988223637Sbz#define ISN_RANDOM_INCREMENT (4096 - 1) 2989223637Sbz return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 2990223637Sbz V_pf_tcp_iss_off); 2991223637Sbz#undef ISN_RANDOM_INCREMENT 2992223637Sbz} 2993223637Sbz 2994240233Sglebiusstatic int 2995223637Sbzpf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction, 2996240233Sglebius struct pfi_kif *kif, struct mbuf *m, int off, struct pf_pdesc *pd, 2997240233Sglebius struct pf_rule **am, struct pf_ruleset **rsm, struct inpcb *inp) 2998126258Smlaier{ 2999130613Smlaier struct pf_rule *nr = NULL; 3000240233Sglebius struct pf_addr * const saddr = pd->src; 3001240233Sglebius struct pf_addr * const daddr = pd->dst; 3002126258Smlaier sa_family_t af = pd->af; 3003126258Smlaier struct pf_rule *r, *a = NULL; 3004126258Smlaier struct pf_ruleset *ruleset = NULL; 3005130613Smlaier struct pf_src_node *nsn = NULL; 3006223637Sbz struct tcphdr *th = pd->hdr.tcp; 3007223637Sbz struct pf_state_key *sk = NULL, *nk = NULL; 3008126258Smlaier u_short reason; 3009223637Sbz int rewrite = 0, hdrlen = 0; 3010171168Smlaier int tag = -1, rtableid = -1; 3011145836Smlaier int asd = 0; 3012171168Smlaier int match = 0; 3013223637Sbz int state_icmp = 0; 3014223637Sbz u_int16_t sport = 0, dport = 0; 3015223637Sbz u_int16_t bproto_sum = 0, bip_sum = 0; 3016223637Sbz u_int8_t icmptype = 0, icmpcode = 0; 3017240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3018126258Smlaier 3019240233Sglebius PF_RULES_RASSERT(); 3020223637Sbz 3021240233Sglebius if (inp != NULL) { 3022240233Sglebius INP_LOCK_ASSERT(inp); 3023240233Sglebius pd->lookup.uid = inp->inp_cred->cr_uid; 3024240233Sglebius pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3025240233Sglebius pd->lookup.done = 1; 3026145836Smlaier } 3027145836Smlaier 3028223637Sbz switch (pd->proto) { 3029223637Sbz case IPPROTO_TCP: 3030223637Sbz sport = th->th_sport; 3031223637Sbz dport = th->th_dport; 3032223637Sbz hdrlen = sizeof(*th); 3033223637Sbz break; 3034223637Sbz case IPPROTO_UDP: 3035223637Sbz sport = pd->hdr.udp->uh_sport; 3036223637Sbz dport = pd->hdr.udp->uh_dport; 3037223637Sbz hdrlen = sizeof(*pd->hdr.udp); 3038223637Sbz break; 3039223637Sbz#ifdef INET 3040223637Sbz case IPPROTO_ICMP: 3041223637Sbz if (pd->af != AF_INET) 3042223637Sbz break; 3043223637Sbz sport = dport = pd->hdr.icmp->icmp_id; 3044223637Sbz hdrlen = sizeof(*pd->hdr.icmp); 3045223637Sbz icmptype = pd->hdr.icmp->icmp_type; 3046223637Sbz icmpcode = pd->hdr.icmp->icmp_code; 3047223637Sbz 3048223637Sbz if (icmptype == ICMP_UNREACH || 3049223637Sbz icmptype == ICMP_SOURCEQUENCH || 3050223637Sbz icmptype == ICMP_REDIRECT || 3051223637Sbz icmptype == ICMP_TIMXCEED || 3052223637Sbz icmptype == ICMP_PARAMPROB) 3053223637Sbz state_icmp++; 3054223637Sbz break; 3055223637Sbz#endif /* INET */ 3056223637Sbz#ifdef INET6 3057223637Sbz case IPPROTO_ICMPV6: 3058223637Sbz if (af != AF_INET6) 3059223637Sbz break; 3060223637Sbz sport = dport = pd->hdr.icmp6->icmp6_id; 3061223637Sbz hdrlen = sizeof(*pd->hdr.icmp6); 3062223637Sbz icmptype = pd->hdr.icmp6->icmp6_type; 3063223637Sbz icmpcode = pd->hdr.icmp6->icmp6_code; 3064223637Sbz 3065223637Sbz if (icmptype == ICMP6_DST_UNREACH || 3066223637Sbz icmptype == ICMP6_PACKET_TOO_BIG || 3067223637Sbz icmptype == ICMP6_TIME_EXCEEDED || 3068223637Sbz icmptype == ICMP6_PARAM_PROB) 3069223637Sbz state_icmp++; 3070223637Sbz break; 3071223637Sbz#endif /* INET6 */ 3072223637Sbz default: 3073223637Sbz sport = dport = hdrlen = 0; 3074223637Sbz break; 3075223637Sbz } 3076223637Sbz 3077126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3078126258Smlaier 3079223637Sbz /* check packet for BINAT/NAT/RDR */ 3080240233Sglebius if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk, 3081240641Sglebius &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) { 3082240233Sglebius KASSERT(sk != NULL, ("%s: null sk", __func__)); 3083240233Sglebius KASSERT(nk != NULL, ("%s: null nk", __func__)); 3084223637Sbz 3085223637Sbz if (pd->ip_sum) 3086223637Sbz bip_sum = *pd->ip_sum; 3087223637Sbz 3088223637Sbz switch (pd->proto) { 3089223637Sbz case IPPROTO_TCP: 3090223637Sbz bproto_sum = th->th_sum; 3091223637Sbz pd->proto_sum = &th->th_sum; 3092223637Sbz 3093223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3094223637Sbz nk->port[pd->sidx] != sport) { 3095223637Sbz pf_change_ap(saddr, &th->th_sport, pd->ip_sum, 3096223637Sbz &th->th_sum, &nk->addr[pd->sidx], 3097223637Sbz nk->port[pd->sidx], 0, af); 3098223637Sbz pd->sport = &th->th_sport; 3099223637Sbz sport = th->th_sport; 3100223637Sbz } 3101223637Sbz 3102223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3103223637Sbz nk->port[pd->didx] != dport) { 3104223637Sbz pf_change_ap(daddr, &th->th_dport, pd->ip_sum, 3105223637Sbz &th->th_sum, &nk->addr[pd->didx], 3106223637Sbz nk->port[pd->didx], 0, af); 3107223637Sbz dport = th->th_dport; 3108223637Sbz pd->dport = &th->th_dport; 3109223637Sbz } 3110126258Smlaier rewrite++; 3111223637Sbz break; 3112223637Sbz case IPPROTO_UDP: 3113223637Sbz bproto_sum = pd->hdr.udp->uh_sum; 3114223637Sbz pd->proto_sum = &pd->hdr.udp->uh_sum; 3115223637Sbz 3116223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3117223637Sbz nk->port[pd->sidx] != sport) { 3118223637Sbz pf_change_ap(saddr, &pd->hdr.udp->uh_sport, 3119223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3120223637Sbz &nk->addr[pd->sidx], 3121223637Sbz nk->port[pd->sidx], 1, af); 3122223637Sbz sport = pd->hdr.udp->uh_sport; 3123223637Sbz pd->sport = &pd->hdr.udp->uh_sport; 3124223637Sbz } 3125223637Sbz 3126223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3127223637Sbz nk->port[pd->didx] != dport) { 3128223637Sbz pf_change_ap(daddr, &pd->hdr.udp->uh_dport, 3129223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3130223637Sbz &nk->addr[pd->didx], 3131223637Sbz nk->port[pd->didx], 1, af); 3132223637Sbz dport = pd->hdr.udp->uh_dport; 3133223637Sbz pd->dport = &pd->hdr.udp->uh_dport; 3134223637Sbz } 3135223637Sbz rewrite++; 3136223637Sbz break; 3137223637Sbz#ifdef INET 3138223637Sbz case IPPROTO_ICMP: 3139223637Sbz nk->port[0] = nk->port[1]; 3140223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET)) 3141223637Sbz pf_change_a(&saddr->v4.s_addr, pd->ip_sum, 3142223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3143223637Sbz 3144223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET)) 3145223637Sbz pf_change_a(&daddr->v4.s_addr, pd->ip_sum, 3146223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3147223637Sbz 3148223637Sbz if (nk->port[1] != pd->hdr.icmp->icmp_id) { 3149223637Sbz pd->hdr.icmp->icmp_cksum = pf_cksum_fixup( 3150223637Sbz pd->hdr.icmp->icmp_cksum, sport, 3151223637Sbz nk->port[1], 0); 3152223637Sbz pd->hdr.icmp->icmp_id = nk->port[1]; 3153223637Sbz pd->sport = &pd->hdr.icmp->icmp_id; 3154223637Sbz } 3155223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 3156223637Sbz break; 3157223637Sbz#endif /* INET */ 3158223637Sbz#ifdef INET6 3159223637Sbz case IPPROTO_ICMPV6: 3160223637Sbz nk->port[0] = nk->port[1]; 3161223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6)) 3162223637Sbz pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum, 3163223637Sbz &nk->addr[pd->sidx], 0); 3164223637Sbz 3165223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6)) 3166223637Sbz pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum, 3167223637Sbz &nk->addr[pd->didx], 0); 3168223637Sbz rewrite++; 3169223637Sbz break; 3170223637Sbz#endif /* INET */ 3171223637Sbz default: 3172223637Sbz switch (af) { 3173223637Sbz#ifdef INET 3174223637Sbz case AF_INET: 3175223637Sbz if (PF_ANEQ(saddr, 3176223637Sbz &nk->addr[pd->sidx], AF_INET)) 3177223637Sbz pf_change_a(&saddr->v4.s_addr, 3178223637Sbz pd->ip_sum, 3179223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3180223637Sbz 3181223637Sbz if (PF_ANEQ(daddr, 3182223637Sbz &nk->addr[pd->didx], AF_INET)) 3183223637Sbz pf_change_a(&daddr->v4.s_addr, 3184223637Sbz pd->ip_sum, 3185223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3186223637Sbz break; 3187223637Sbz#endif /* INET */ 3188223637Sbz#ifdef INET6 3189223637Sbz case AF_INET6: 3190223637Sbz if (PF_ANEQ(saddr, 3191223637Sbz &nk->addr[pd->sidx], AF_INET6)) 3192223637Sbz PF_ACPY(saddr, &nk->addr[pd->sidx], af); 3193223637Sbz 3194223637Sbz if (PF_ANEQ(daddr, 3195223637Sbz &nk->addr[pd->didx], AF_INET6)) 3196223637Sbz PF_ACPY(saddr, &nk->addr[pd->didx], af); 3197223637Sbz break; 3198223637Sbz#endif /* INET */ 3199223637Sbz } 3200223637Sbz break; 3201126258Smlaier } 3202223637Sbz if (nr->natpass) 3203223637Sbz r = NULL; 3204223637Sbz pd->nat_rule = nr; 3205126258Smlaier } 3206126258Smlaier 3207126258Smlaier while (r != NULL) { 3208126258Smlaier r->evaluations++; 3209171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3210126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3211126258Smlaier else if (r->direction && r->direction != direction) 3212126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3213126258Smlaier else if (r->af && r->af != af) 3214126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3215223637Sbz else if (r->proto && r->proto != pd->proto) 3216126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3217171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, saddr, af, 3218231852Sbz r->src.neg, kif, M_GETFIB(m))) 3219126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3220223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3221126258Smlaier else if (r->src.port_op && !pf_match_port(r->src.port_op, 3222223637Sbz r->src.port[0], r->src.port[1], sport)) 3223126258Smlaier r = r->skip[PF_SKIP_SRC_PORT].ptr; 3224171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, 3225231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3226126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3227223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3228126258Smlaier else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 3229223637Sbz r->dst.port[0], r->dst.port[1], dport)) 3230126258Smlaier r = r->skip[PF_SKIP_DST_PORT].ptr; 3231223637Sbz /* icmp only. type always 0 in other cases */ 3232223637Sbz else if (r->type && r->type != icmptype + 1) 3233223637Sbz r = TAILQ_NEXT(r, entries); 3234223637Sbz /* icmp only. type always 0 in other cases */ 3235223637Sbz else if (r->code && r->code != icmpcode + 1) 3236223637Sbz r = TAILQ_NEXT(r, entries); 3237171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3238126258Smlaier r = TAILQ_NEXT(r, entries); 3239126258Smlaier else if (r->rule_flag & PFRULE_FRAGMENT) 3240126258Smlaier r = TAILQ_NEXT(r, entries); 3241223637Sbz else if (pd->proto == IPPROTO_TCP && 3242223637Sbz (r->flagset & th->th_flags) != r->flags) 3243126258Smlaier r = TAILQ_NEXT(r, entries); 3244223637Sbz /* tcp/udp only. uid.op always 0 in other cases */ 3245171168Smlaier else if (r->uid.op && (pd->lookup.done || (pd->lookup.done = 3246240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3247126258Smlaier !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 3248171168Smlaier pd->lookup.uid)) 3249126258Smlaier r = TAILQ_NEXT(r, entries); 3250223637Sbz /* tcp/udp only. gid.op always 0 in other cases */ 3251171168Smlaier else if (r->gid.op && (pd->lookup.done || (pd->lookup.done = 3252240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3253126258Smlaier !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 3254171168Smlaier pd->lookup.gid)) 3255126258Smlaier r = TAILQ_NEXT(r, entries); 3256223637Sbz else if (r->prob && 3257223637Sbz r->prob <= arc4random()) 3258126258Smlaier r = TAILQ_NEXT(r, entries); 3259240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3260240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3261126258Smlaier r = TAILQ_NEXT(r, entries); 3262223637Sbz else if (r->os_fingerprint != PF_OSFP_ANY && 3263223637Sbz (pd->proto != IPPROTO_TCP || !pf_osfp_match( 3264223637Sbz pf_osfp_fingerprint(pd, m, off, th), 3265223637Sbz r->os_fingerprint))) 3266126258Smlaier r = TAILQ_NEXT(r, entries); 3267126258Smlaier else { 3268126258Smlaier if (r->tag) 3269126258Smlaier tag = r->tag; 3270171168Smlaier if (r->rtableid >= 0) 3271171168Smlaier rtableid = r->rtableid; 3272126258Smlaier if (r->anchor == NULL) { 3273171168Smlaier match = 1; 3274126258Smlaier *rm = r; 3275126258Smlaier *am = a; 3276126258Smlaier *rsm = ruleset; 3277126258Smlaier if ((*rm)->quick) 3278126258Smlaier break; 3279126258Smlaier r = TAILQ_NEXT(r, entries); 3280126258Smlaier } else 3281240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3282240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3283240641Sglebius &match); 3284126258Smlaier } 3285240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3286240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3287171168Smlaier break; 3288126258Smlaier } 3289126258Smlaier r = *rm; 3290126258Smlaier a = *am; 3291126258Smlaier ruleset = *rsm; 3292126258Smlaier 3293126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3294126258Smlaier 3295223637Sbz if (r->log || (nr != NULL && nr->log)) { 3296126258Smlaier if (rewrite) 3297223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3298240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, 3299240233Sglebius ruleset, pd, 1); 3300126258Smlaier } 3301126258Smlaier 3302126258Smlaier if ((r->action == PF_DROP) && 3303126258Smlaier ((r->rule_flag & PFRULE_RETURNRST) || 3304126258Smlaier (r->rule_flag & PFRULE_RETURNICMP) || 3305126258Smlaier (r->rule_flag & PFRULE_RETURN))) { 3306126258Smlaier /* undo NAT changes, if they have taken place */ 3307130613Smlaier if (nr != NULL) { 3308223637Sbz PF_ACPY(saddr, &sk->addr[pd->sidx], af); 3309223637Sbz PF_ACPY(daddr, &sk->addr[pd->didx], af); 3310223637Sbz if (pd->sport) 3311223637Sbz *pd->sport = sk->port[pd->sidx]; 3312223637Sbz if (pd->dport) 3313223637Sbz *pd->dport = sk->port[pd->didx]; 3314223637Sbz if (pd->proto_sum) 3315223637Sbz *pd->proto_sum = bproto_sum; 3316223637Sbz if (pd->ip_sum) 3317223637Sbz *pd->ip_sum = bip_sum; 3318223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3319126258Smlaier } 3320223637Sbz if (pd->proto == IPPROTO_TCP && 3321223637Sbz ((r->rule_flag & PFRULE_RETURNRST) || 3322126258Smlaier (r->rule_flag & PFRULE_RETURN)) && 3323126258Smlaier !(th->th_flags & TH_RST)) { 3324223637Sbz u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 3325223637Sbz int len = 0; 3326223637Sbz#ifdef INET 3327223637Sbz struct ip *h4; 3328223637Sbz#endif 3329223637Sbz#ifdef INET6 3330223637Sbz struct ip6_hdr *h6; 3331223637Sbz#endif 3332126258Smlaier 3333223637Sbz switch (af) { 3334223637Sbz#ifdef INET 3335223637Sbz case AF_INET: 3336223637Sbz h4 = mtod(m, struct ip *); 3337223637Sbz len = ntohs(h4->ip_len) - off; 3338223637Sbz break; 3339223637Sbz#endif 3340223637Sbz#ifdef INET6 3341223637Sbz case AF_INET6: 3342223637Sbz h6 = mtod(m, struct ip6_hdr *); 3343223637Sbz len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); 3344223637Sbz break; 3345223637Sbz#endif 3346223637Sbz } 3347223637Sbz 3348223637Sbz if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) 3349223637Sbz REASON_SET(&reason, PFRES_PROTCKSUM); 3350223637Sbz else { 3351223637Sbz if (th->th_flags & TH_SYN) 3352223637Sbz ack++; 3353223637Sbz if (th->th_flags & TH_FIN) 3354223637Sbz ack++; 3355223637Sbz pf_send_tcp(m, r, af, pd->dst, 3356223637Sbz pd->src, th->th_dport, th->th_sport, 3357223637Sbz ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 3358240233Sglebius r->return_ttl, 1, 0, kif->pfik_ifp); 3359223637Sbz } 3360223637Sbz } else if (pd->proto != IPPROTO_ICMP && af == AF_INET && 3361223637Sbz r->return_icmp) 3362126258Smlaier pf_send_icmp(m, r->return_icmp >> 8, 3363126258Smlaier r->return_icmp & 255, af, r); 3364223637Sbz else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && 3365223637Sbz r->return_icmp6) 3366126258Smlaier pf_send_icmp(m, r->return_icmp6 >> 8, 3367126258Smlaier r->return_icmp6 & 255, af, r); 3368126258Smlaier } 3369126258Smlaier 3370126258Smlaier if (r->action == PF_DROP) 3371223637Sbz goto cleanup; 3372126258Smlaier 3373240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3374126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3375223637Sbz goto cleanup; 3376126258Smlaier } 3377240233Sglebius if (rtableid >= 0) 3378240233Sglebius M_SETFIB(m, rtableid); 3379126258Smlaier 3380223637Sbz if (!state_icmp && (r->keep_state || nr != NULL || 3381223637Sbz (pd->flags & PFDESC_TCP_NORM))) { 3382223637Sbz int action; 3383240233Sglebius action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, 3384240233Sglebius sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, 3385240233Sglebius hdrlen); 3386223637Sbz if (action != PF_PASS) 3387223637Sbz return (action); 3388223637Sbz } else { 3389223637Sbz if (sk != NULL) 3390240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3391223637Sbz if (nk != NULL) 3392240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3393223637Sbz } 3394126258Smlaier 3395223637Sbz /* copy back packet headers if we performed NAT operations */ 3396223637Sbz if (rewrite) 3397223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3398130613Smlaier 3399240233Sglebius if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 3400240233Sglebius direction == PF_OUT && 3401240233Sglebius pfsync_defer_ptr != NULL && pfsync_defer_ptr(*sm, m)) 3402223637Sbz /* 3403223637Sbz * We want the state created, but we dont 3404223637Sbz * want to send this in case a partner 3405223637Sbz * firewall has to know about it to allow 3406223637Sbz * replies through it. 3407223637Sbz */ 3408240233Sglebius return (PF_DEFER); 3409223637Sbz 3410223637Sbz return (PF_PASS); 3411223637Sbz 3412130613Smlaiercleanup: 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 return (PF_DROP); 3418223637Sbz} 3419126258Smlaier 3420240233Sglebiusstatic int 3421223637Sbzpf_create_state(struct pf_rule *r, struct pf_rule *nr, struct pf_rule *a, 3422240233Sglebius struct pf_pdesc *pd, struct pf_src_node *nsn, struct pf_state_key *nk, 3423240233Sglebius struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport, 3424240233Sglebius u_int16_t dport, int *rewrite, struct pfi_kif *kif, struct pf_state **sm, 3425240233Sglebius int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen) 3426223637Sbz{ 3427223637Sbz struct pf_state *s = NULL; 3428223637Sbz struct pf_src_node *sn = NULL; 3429223637Sbz struct tcphdr *th = pd->hdr.tcp; 3430223637Sbz u_int16_t mss = V_tcp_mssdflt; 3431223637Sbz u_short reason; 3432223637Sbz 3433223637Sbz /* check maximums */ 3434263029Sglebius if (r->max_states && 3435263029Sglebius (counter_u64_fetch(r->states_cur) >= r->max_states)) { 3436270574Sglebius counter_u64_add(V_pf_status.lcounters[LCNT_STATES], 1); 3437223637Sbz REASON_SET(&reason, PFRES_MAXSTATES); 3438223637Sbz return (PF_DROP); 3439223637Sbz } 3440223637Sbz /* src node for filter rule */ 3441223637Sbz if ((r->rule_flag & PFRULE_SRCTRACK || 3442223637Sbz r->rpool.opts & PF_POOL_STICKYADDR) && 3443223637Sbz pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) { 3444223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3445223637Sbz goto csfailed; 3446223637Sbz } 3447223637Sbz /* src node for translation rule */ 3448223637Sbz if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) && 3449223637Sbz pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) { 3450223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3451223637Sbz goto csfailed; 3452223637Sbz } 3453240233Sglebius s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO); 3454223637Sbz if (s == NULL) { 3455223637Sbz REASON_SET(&reason, PFRES_MEMORY); 3456223637Sbz goto csfailed; 3457223637Sbz } 3458223637Sbz s->rule.ptr = r; 3459223637Sbz s->nat_rule.ptr = nr; 3460223637Sbz s->anchor.ptr = a; 3461223637Sbz STATE_INC_COUNTERS(s); 3462223637Sbz if (r->allow_opts) 3463223637Sbz s->state_flags |= PFSTATE_ALLOWOPTS; 3464223637Sbz if (r->rule_flag & PFRULE_STATESLOPPY) 3465223637Sbz s->state_flags |= PFSTATE_SLOPPY; 3466223637Sbz s->log = r->log & PF_LOG_ALL; 3467223637Sbz s->sync_state = PFSYNC_S_NONE; 3468223637Sbz if (nr != NULL) 3469223637Sbz s->log |= nr->log & PF_LOG_ALL; 3470223637Sbz switch (pd->proto) { 3471223637Sbz case IPPROTO_TCP: 3472126258Smlaier s->src.seqlo = ntohl(th->th_seq); 3473223637Sbz s->src.seqhi = s->src.seqlo + pd->p_len + 1; 3474126258Smlaier if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN && 3475126258Smlaier r->keep_state == PF_STATE_MODULATE) { 3476126258Smlaier /* Generate sequence number modulator */ 3477223637Sbz if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 3478223637Sbz 0) 3479223637Sbz s->src.seqdiff = 1; 3480126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, 3481126258Smlaier htonl(s->src.seqlo + s->src.seqdiff), 0); 3482223637Sbz *rewrite = 1; 3483126258Smlaier } else 3484126258Smlaier s->src.seqdiff = 0; 3485126258Smlaier if (th->th_flags & TH_SYN) { 3486126258Smlaier s->src.seqhi++; 3487223637Sbz s->src.wscale = pf_get_wscale(m, off, 3488223637Sbz th->th_off, pd->af); 3489126258Smlaier } 3490126258Smlaier s->src.max_win = MAX(ntohs(th->th_win), 1); 3491126258Smlaier if (s->src.wscale & PF_WSCALE_MASK) { 3492126258Smlaier /* Remove scale factor from initial window */ 3493126258Smlaier int win = s->src.max_win; 3494126258Smlaier win += 1 << (s->src.wscale & PF_WSCALE_MASK); 3495126258Smlaier s->src.max_win = (win - 1) >> 3496126258Smlaier (s->src.wscale & PF_WSCALE_MASK); 3497126258Smlaier } 3498126258Smlaier if (th->th_flags & TH_FIN) 3499126258Smlaier s->src.seqhi++; 3500126258Smlaier s->dst.seqhi = 1; 3501126258Smlaier s->dst.max_win = 1; 3502126258Smlaier s->src.state = TCPS_SYN_SENT; 3503126258Smlaier s->dst.state = TCPS_CLOSED; 3504126258Smlaier s->timeout = PFTM_TCP_FIRST_PACKET; 3505223637Sbz break; 3506223637Sbz case IPPROTO_UDP: 3507223637Sbz s->src.state = PFUDPS_SINGLE; 3508223637Sbz s->dst.state = PFUDPS_NO_TRAFFIC; 3509223637Sbz s->timeout = PFTM_UDP_FIRST_PACKET; 3510223637Sbz break; 3511223637Sbz case IPPROTO_ICMP: 3512223637Sbz#ifdef INET6 3513223637Sbz case IPPROTO_ICMPV6: 3514223637Sbz#endif 3515223637Sbz s->timeout = PFTM_ICMP_FIRST_PACKET; 3516223637Sbz break; 3517223637Sbz default: 3518223637Sbz s->src.state = PFOTHERS_SINGLE; 3519223637Sbz s->dst.state = PFOTHERS_NO_TRAFFIC; 3520223637Sbz s->timeout = PFTM_OTHER_FIRST_PACKET; 3521223637Sbz } 3522223637Sbz 3523270576Sglebius if (r->rt && r->rt != PF_FASTROUTE) { 3524270576Sglebius struct pf_src_node *sn = NULL; 3525270576Sglebius 3526270576Sglebius if (pf_map_addr(pd->af, r, pd->src, &s->rt_addr, NULL, &sn)) { 3527270576Sglebius REASON_SET(&reason, PFRES_MAPFAILED); 3528270576Sglebius pf_src_tree_remove_state(s); 3529270576Sglebius STATE_DEC_COUNTERS(s); 3530270576Sglebius uma_zfree(V_pf_state_z, s); 3531270576Sglebius goto csfailed; 3532270576Sglebius } 3533270576Sglebius s->rt_kif = r->rpool.cur->kif; 3534270576Sglebius } 3535270576Sglebius 3536240233Sglebius s->creation = time_uptime; 3537240233Sglebius s->expire = time_uptime; 3538223637Sbz 3539223637Sbz if (sn != NULL) { 3540223637Sbz s->src_node = sn; 3541223637Sbz s->src_node->states++; 3542223637Sbz } 3543223637Sbz if (nsn != NULL) { 3544223637Sbz /* XXX We only modify one side for now. */ 3545223637Sbz PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af); 3546223637Sbz s->nat_src_node = nsn; 3547223637Sbz s->nat_src_node->states++; 3548223637Sbz } 3549223637Sbz if (pd->proto == IPPROTO_TCP) { 3550126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m, 3551126258Smlaier off, pd, th, &s->src, &s->dst)) { 3552126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3553130613Smlaier pf_src_tree_remove_state(s); 3554145836Smlaier STATE_DEC_COUNTERS(s); 3555240233Sglebius uma_zfree(V_pf_state_z, s); 3556126258Smlaier return (PF_DROP); 3557126258Smlaier } 3558126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub && 3559145836Smlaier pf_normalize_tcp_stateful(m, off, pd, &reason, th, s, 3560223637Sbz &s->src, &s->dst, rewrite)) { 3561145836Smlaier /* This really shouldn't happen!!! */ 3562145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 3563145836Smlaier ("pf_normalize_tcp_stateful failed on first pkt")); 3564126258Smlaier pf_normalize_tcp_cleanup(s); 3565130613Smlaier pf_src_tree_remove_state(s); 3566145836Smlaier STATE_DEC_COUNTERS(s); 3567240233Sglebius uma_zfree(V_pf_state_z, s); 3568223637Sbz return (PF_DROP); 3569126258Smlaier } 3570126258Smlaier } 3571223637Sbz s->direction = pd->dir; 3572126258Smlaier 3573240233Sglebius /* 3574240233Sglebius * sk/nk could already been setup by pf_get_translation(). 3575240233Sglebius */ 3576240233Sglebius if (nr == NULL) { 3577240233Sglebius KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p", 3578240233Sglebius __func__, nr, sk, nk)); 3579240233Sglebius sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport); 3580240233Sglebius if (sk == NULL) 3581240233Sglebius goto csfailed; 3582240233Sglebius nk = sk; 3583240233Sglebius } else 3584240233Sglebius KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p", 3585240233Sglebius __func__, nr, sk, nk)); 3586126258Smlaier 3587240233Sglebius /* Swap sk/nk for PF_OUT. */ 3588240233Sglebius if (pf_state_insert(BOUND_IFACE(r, kif), 3589240233Sglebius (pd->dir == PF_IN) ? sk : nk, 3590240233Sglebius (pd->dir == PF_IN) ? nk : sk, s)) { 3591223637Sbz if (pd->proto == IPPROTO_TCP) 3592223637Sbz pf_normalize_tcp_cleanup(s); 3593223637Sbz REASON_SET(&reason, PFRES_STATEINS); 3594223637Sbz pf_src_tree_remove_state(s); 3595223637Sbz STATE_DEC_COUNTERS(s); 3596240233Sglebius uma_zfree(V_pf_state_z, s); 3597223637Sbz return (PF_DROP); 3598223637Sbz } else 3599223637Sbz *sm = s; 3600126258Smlaier 3601240233Sglebius if (tag > 0) 3602223637Sbz s->tag = tag; 3603223637Sbz if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) == 3604223637Sbz TH_SYN && r->keep_state == PF_STATE_SYNPROXY) { 3605223637Sbz s->src.state = PF_TCPS_PROXY_SRC; 3606223637Sbz /* undo NAT changes, if they have taken place */ 3607223637Sbz if (nr != NULL) { 3608223637Sbz struct pf_state_key *skt = s->key[PF_SK_WIRE]; 3609223637Sbz if (pd->dir == PF_OUT) 3610223637Sbz skt = s->key[PF_SK_STACK]; 3611223637Sbz PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af); 3612223637Sbz PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af); 3613223637Sbz if (pd->sport) 3614223637Sbz *pd->sport = skt->port[pd->sidx]; 3615223637Sbz if (pd->dport) 3616223637Sbz *pd->dport = skt->port[pd->didx]; 3617223637Sbz if (pd->proto_sum) 3618223637Sbz *pd->proto_sum = bproto_sum; 3619223637Sbz if (pd->ip_sum) 3620223637Sbz *pd->ip_sum = bip_sum; 3621223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3622223637Sbz } 3623223637Sbz s->src.seqhi = htonl(arc4random()); 3624223637Sbz /* Find mss option */ 3625231852Sbz int rtid = M_GETFIB(m); 3626223637Sbz mss = pf_get_mss(m, off, th->th_off, pd->af); 3627231852Sbz mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 3628231852Sbz mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 3629223637Sbz s->src.mss = mss; 3630223637Sbz pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport, 3631223637Sbz th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 3632240233Sglebius TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL); 3633223637Sbz REASON_SET(&reason, PFRES_SYNPROXY); 3634223637Sbz return (PF_SYNPROXY_DROP); 3635171168Smlaier } 3636165631Smlaier 3637223637Sbz return (PF_PASS); 3638126258Smlaier 3639223637Sbzcsfailed: 3640223637Sbz if (sk != NULL) 3641240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3642223637Sbz if (nk != NULL) 3643240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3644223637Sbz 3645261019Sglebius if (sn != NULL && sn->states == 0 && sn->expire == 0) { 3646261019Sglebius pf_unlink_src_node(sn); 3647261019Sglebius pf_free_src_node(sn); 3648261019Sglebius } 3649240737Sglebius 3650261019Sglebius if (nsn != sn && nsn != NULL && nsn->states == 0 && nsn->expire == 0) { 3651261019Sglebius pf_unlink_src_node(nsn); 3652261019Sglebius pf_free_src_node(nsn); 3653261019Sglebius } 3654240737Sglebius 3655223637Sbz return (PF_DROP); 3656126258Smlaier} 3657126258Smlaier 3658240233Sglebiusstatic int 3659130613Smlaierpf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif, 3660126258Smlaier struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am, 3661126258Smlaier struct pf_ruleset **rsm) 3662126258Smlaier{ 3663126258Smlaier struct pf_rule *r, *a = NULL; 3664126258Smlaier struct pf_ruleset *ruleset = NULL; 3665126258Smlaier sa_family_t af = pd->af; 3666126258Smlaier u_short reason; 3667126258Smlaier int tag = -1; 3668145836Smlaier int asd = 0; 3669171168Smlaier int match = 0; 3670240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3671126258Smlaier 3672240233Sglebius PF_RULES_RASSERT(); 3673240233Sglebius 3674126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3675126258Smlaier while (r != NULL) { 3676126258Smlaier r->evaluations++; 3677171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3678126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3679126258Smlaier else if (r->direction && r->direction != direction) 3680126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3681126258Smlaier else if (r->af && r->af != af) 3682126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3683126258Smlaier else if (r->proto && r->proto != pd->proto) 3684126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3685171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 3686231852Sbz r->src.neg, kif, M_GETFIB(m))) 3687126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3688171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 3689231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3690126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3691171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3692126258Smlaier r = TAILQ_NEXT(r, entries); 3693173815Smlaier else if (r->os_fingerprint != PF_OSFP_ANY) 3694126258Smlaier r = TAILQ_NEXT(r, entries); 3695173815Smlaier else if (pd->proto == IPPROTO_UDP && 3696173815Smlaier (r->src.port_op || r->dst.port_op)) 3697173815Smlaier r = TAILQ_NEXT(r, entries); 3698173815Smlaier else if (pd->proto == IPPROTO_TCP && 3699173815Smlaier (r->src.port_op || r->dst.port_op || r->flagset)) 3700173815Smlaier r = TAILQ_NEXT(r, entries); 3701173815Smlaier else if ((pd->proto == IPPROTO_ICMP || 3702173815Smlaier pd->proto == IPPROTO_ICMPV6) && 3703173815Smlaier (r->type || r->code)) 3704173815Smlaier r = TAILQ_NEXT(r, entries); 3705223637Sbz else if (r->prob && r->prob <= 3706223637Sbz (arc4random() % (UINT_MAX - 1) + 1)) 3707126258Smlaier r = TAILQ_NEXT(r, entries); 3708240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3709240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3710126258Smlaier r = TAILQ_NEXT(r, entries); 3711126258Smlaier else { 3712126258Smlaier if (r->anchor == NULL) { 3713171168Smlaier match = 1; 3714126258Smlaier *rm = r; 3715126258Smlaier *am = a; 3716126258Smlaier *rsm = ruleset; 3717126258Smlaier if ((*rm)->quick) 3718126258Smlaier break; 3719126258Smlaier r = TAILQ_NEXT(r, entries); 3720126258Smlaier } else 3721240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3722240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3723240641Sglebius &match); 3724126258Smlaier } 3725240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3726240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3727171168Smlaier break; 3728126258Smlaier } 3729126258Smlaier r = *rm; 3730126258Smlaier a = *am; 3731126258Smlaier ruleset = *rsm; 3732126258Smlaier 3733126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3734130613Smlaier 3735126258Smlaier if (r->log) 3736240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd, 3737240233Sglebius 1); 3738126258Smlaier 3739126258Smlaier if (r->action != PF_PASS) 3740126258Smlaier return (PF_DROP); 3741126258Smlaier 3742240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3743126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3744126258Smlaier return (PF_DROP); 3745126258Smlaier } 3746126258Smlaier 3747126258Smlaier return (PF_PASS); 3748126258Smlaier} 3749126258Smlaier 3750240233Sglebiusstatic int 3751200930Sdelphijpf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst, 3752200930Sdelphij struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off, 3753200930Sdelphij struct pf_pdesc *pd, u_short *reason, int *copyback) 3754126258Smlaier{ 3755223637Sbz struct tcphdr *th = pd->hdr.tcp; 3756223637Sbz u_int16_t win = ntohs(th->th_win); 3757223637Sbz u_int32_t ack, end, seq, orig_seq; 3758223637Sbz u_int8_t sws, dws; 3759223637Sbz int ackskew; 3760126258Smlaier 3761126258Smlaier if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) { 3762126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3763126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3764126258Smlaier } else 3765126258Smlaier sws = dws = 0; 3766126258Smlaier 3767126258Smlaier /* 3768126258Smlaier * Sequence tracking algorithm from Guido van Rooij's paper: 3769126258Smlaier * http://www.madison-gurkha.com/publications/tcp_filtering/ 3770126258Smlaier * tcp_filtering.ps 3771126258Smlaier */ 3772126258Smlaier 3773145836Smlaier orig_seq = seq = ntohl(th->th_seq); 3774126258Smlaier if (src->seqlo == 0) { 3775126258Smlaier /* First packet from this end. Set its state */ 3776126258Smlaier 3777126258Smlaier if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) && 3778126258Smlaier src->scrub == NULL) { 3779126258Smlaier if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) { 3780126258Smlaier REASON_SET(reason, PFRES_MEMORY); 3781126258Smlaier return (PF_DROP); 3782126258Smlaier } 3783126258Smlaier } 3784126258Smlaier 3785126258Smlaier /* Deferred generation of sequence number modulator */ 3786126258Smlaier if (dst->seqdiff && !src->seqdiff) { 3787223637Sbz /* use random iss for the TCP server */ 3788223637Sbz while ((src->seqdiff = arc4random() - seq) == 0) 3789126258Smlaier ; 3790126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3791126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3792126258Smlaier src->seqdiff), 0); 3793126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3794200930Sdelphij *copyback = 1; 3795126258Smlaier } else { 3796126258Smlaier ack = ntohl(th->th_ack); 3797126258Smlaier } 3798126258Smlaier 3799126258Smlaier end = seq + pd->p_len; 3800126258Smlaier if (th->th_flags & TH_SYN) { 3801126258Smlaier end++; 3802126258Smlaier if (dst->wscale & PF_WSCALE_FLAG) { 3803126258Smlaier src->wscale = pf_get_wscale(m, off, th->th_off, 3804126258Smlaier pd->af); 3805126258Smlaier if (src->wscale & PF_WSCALE_FLAG) { 3806126258Smlaier /* Remove scale factor from initial 3807126258Smlaier * window */ 3808126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3809126258Smlaier win = ((u_int32_t)win + (1 << sws) - 1) 3810126258Smlaier >> sws; 3811126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3812126258Smlaier } else { 3813126258Smlaier /* fixup other window */ 3814126258Smlaier dst->max_win <<= dst->wscale & 3815126258Smlaier PF_WSCALE_MASK; 3816126258Smlaier /* in case of a retrans SYN|ACK */ 3817126258Smlaier dst->wscale = 0; 3818126258Smlaier } 3819126258Smlaier } 3820126258Smlaier } 3821126258Smlaier if (th->th_flags & TH_FIN) 3822126258Smlaier end++; 3823126258Smlaier 3824126258Smlaier src->seqlo = seq; 3825126258Smlaier if (src->state < TCPS_SYN_SENT) 3826126258Smlaier src->state = TCPS_SYN_SENT; 3827126258Smlaier 3828126258Smlaier /* 3829126258Smlaier * May need to slide the window (seqhi may have been set by 3830126258Smlaier * the crappy stack check or if we picked up the connection 3831126258Smlaier * after establishment) 3832126258Smlaier */ 3833126258Smlaier if (src->seqhi == 1 || 3834126258Smlaier SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 3835126258Smlaier src->seqhi = end + MAX(1, dst->max_win << dws); 3836126258Smlaier if (win > src->max_win) 3837126258Smlaier src->max_win = win; 3838126258Smlaier 3839126258Smlaier } else { 3840126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3841126258Smlaier if (src->seqdiff) { 3842126258Smlaier /* Modulate sequence numbers */ 3843126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3844126258Smlaier src->seqdiff), 0); 3845126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3846200930Sdelphij *copyback = 1; 3847126258Smlaier } 3848126258Smlaier end = seq + pd->p_len; 3849126258Smlaier if (th->th_flags & TH_SYN) 3850126258Smlaier end++; 3851126258Smlaier if (th->th_flags & TH_FIN) 3852126258Smlaier end++; 3853126258Smlaier } 3854126258Smlaier 3855126258Smlaier if ((th->th_flags & TH_ACK) == 0) { 3856126258Smlaier /* Let it pass through the ack skew check */ 3857126258Smlaier ack = dst->seqlo; 3858126258Smlaier } else if ((ack == 0 && 3859126258Smlaier (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 3860126258Smlaier /* broken tcp stacks do not set ack */ 3861126258Smlaier (dst->state < TCPS_SYN_SENT)) { 3862126258Smlaier /* 3863126258Smlaier * Many stacks (ours included) will set the ACK number in an 3864126258Smlaier * FIN|ACK if the SYN times out -- no sequence to ACK. 3865126258Smlaier */ 3866126258Smlaier ack = dst->seqlo; 3867126258Smlaier } 3868126258Smlaier 3869126258Smlaier if (seq == end) { 3870126258Smlaier /* Ease sequencing restrictions on no data packets */ 3871126258Smlaier seq = src->seqlo; 3872126258Smlaier end = seq; 3873126258Smlaier } 3874126258Smlaier 3875126258Smlaier ackskew = dst->seqlo - ack; 3876126258Smlaier 3877171168Smlaier 3878171168Smlaier /* 3879171168Smlaier * Need to demodulate the sequence numbers in any TCP SACK options 3880171168Smlaier * (Selective ACK). We could optionally validate the SACK values 3881171168Smlaier * against the current ACK window, either forwards or backwards, but 3882171168Smlaier * I'm not confident that SACK has been implemented properly 3883171168Smlaier * everywhere. It wouldn't surprise me if several stacks accidently 3884171168Smlaier * SACK too far backwards of previously ACKed data. There really aren't 3885171168Smlaier * any security implications of bad SACKing unless the target stack 3886171168Smlaier * doesn't validate the option length correctly. Someone trying to 3887171168Smlaier * spoof into a TCP connection won't bother blindly sending SACK 3888171168Smlaier * options anyway. 3889171168Smlaier */ 3890171168Smlaier if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 3891171168Smlaier if (pf_modulate_sack(m, off, pd, th, dst)) 3892200930Sdelphij *copyback = 1; 3893171168Smlaier } 3894171168Smlaier 3895171168Smlaier 3896223637Sbz#define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 3897126258Smlaier if (SEQ_GEQ(src->seqhi, end) && 3898126258Smlaier /* Last octet inside other's window space */ 3899126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 3900126258Smlaier /* Retrans: not more than one window back */ 3901126258Smlaier (ackskew >= -MAXACKWINDOW) && 3902126258Smlaier /* Acking not more than one reassembled fragment backwards */ 3903145836Smlaier (ackskew <= (MAXACKWINDOW << sws)) && 3904126258Smlaier /* Acking not more than one window forward */ 3905145836Smlaier ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo || 3906223637Sbz (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) || 3907223637Sbz (pd->flags & PFDESC_IP_REAS) == 0)) { 3908171168Smlaier /* Require an exact/+1 sequence match on resets when possible */ 3909126258Smlaier 3910145836Smlaier if (dst->scrub || src->scrub) { 3911145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 3912200930Sdelphij *state, src, dst, copyback)) 3913145836Smlaier return (PF_DROP); 3914145836Smlaier } 3915145836Smlaier 3916126258Smlaier /* update max window */ 3917126258Smlaier if (src->max_win < win) 3918126258Smlaier src->max_win = win; 3919126258Smlaier /* synchronize sequencing */ 3920126258Smlaier if (SEQ_GT(end, src->seqlo)) 3921126258Smlaier src->seqlo = end; 3922126258Smlaier /* slide the window of what the other end can send */ 3923126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 3924126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 3925126258Smlaier 3926126258Smlaier 3927126258Smlaier /* update states */ 3928126258Smlaier if (th->th_flags & TH_SYN) 3929126258Smlaier if (src->state < TCPS_SYN_SENT) 3930126258Smlaier src->state = TCPS_SYN_SENT; 3931126258Smlaier if (th->th_flags & TH_FIN) 3932126258Smlaier if (src->state < TCPS_CLOSING) 3933126258Smlaier src->state = TCPS_CLOSING; 3934126258Smlaier if (th->th_flags & TH_ACK) { 3935145836Smlaier if (dst->state == TCPS_SYN_SENT) { 3936126258Smlaier dst->state = TCPS_ESTABLISHED; 3937145836Smlaier if (src->state == TCPS_ESTABLISHED && 3938145836Smlaier (*state)->src_node != NULL && 3939145836Smlaier pf_src_connlimit(state)) { 3940145836Smlaier REASON_SET(reason, PFRES_SRCLIMIT); 3941145836Smlaier return (PF_DROP); 3942145836Smlaier } 3943145836Smlaier } else if (dst->state == TCPS_CLOSING) 3944126258Smlaier dst->state = TCPS_FIN_WAIT_2; 3945126258Smlaier } 3946126258Smlaier if (th->th_flags & TH_RST) 3947126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 3948126258Smlaier 3949126258Smlaier /* update expire time */ 3950240233Sglebius (*state)->expire = time_uptime; 3951126258Smlaier if (src->state >= TCPS_FIN_WAIT_2 && 3952126258Smlaier dst->state >= TCPS_FIN_WAIT_2) 3953126258Smlaier (*state)->timeout = PFTM_TCP_CLOSED; 3954171168Smlaier else if (src->state >= TCPS_CLOSING && 3955171168Smlaier dst->state >= TCPS_CLOSING) 3956126258Smlaier (*state)->timeout = PFTM_TCP_FIN_WAIT; 3957126258Smlaier else if (src->state < TCPS_ESTABLISHED || 3958126258Smlaier dst->state < TCPS_ESTABLISHED) 3959126258Smlaier (*state)->timeout = PFTM_TCP_OPENING; 3960126258Smlaier else if (src->state >= TCPS_CLOSING || 3961126258Smlaier dst->state >= TCPS_CLOSING) 3962126258Smlaier (*state)->timeout = PFTM_TCP_CLOSING; 3963126258Smlaier else 3964126258Smlaier (*state)->timeout = PFTM_TCP_ESTABLISHED; 3965126258Smlaier 3966126258Smlaier /* Fall through to PASS packet */ 3967126258Smlaier 3968126258Smlaier } else if ((dst->state < TCPS_SYN_SENT || 3969126258Smlaier dst->state >= TCPS_FIN_WAIT_2 || 3970126258Smlaier src->state >= TCPS_FIN_WAIT_2) && 3971126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) && 3972126258Smlaier /* Within a window forward of the originating packet */ 3973126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 3974126258Smlaier /* Within a window backward of the originating packet */ 3975126258Smlaier 3976126258Smlaier /* 3977126258Smlaier * This currently handles three situations: 3978126258Smlaier * 1) Stupid stacks will shotgun SYNs before their peer 3979126258Smlaier * replies. 3980126258Smlaier * 2) When PF catches an already established stream (the 3981126258Smlaier * firewall rebooted, the state table was flushed, routes 3982126258Smlaier * changed...) 3983126258Smlaier * 3) Packets get funky immediately after the connection 3984126258Smlaier * closes (this should catch Solaris spurious ACK|FINs 3985126258Smlaier * that web servers like to spew after a close) 3986126258Smlaier * 3987126258Smlaier * This must be a little more careful than the above code 3988126258Smlaier * since packet floods will also be caught here. We don't 3989126258Smlaier * update the TTL here to mitigate the damage of a packet 3990126258Smlaier * flood and so the same code can handle awkward establishment 3991126258Smlaier * and a loosened connection close. 3992126258Smlaier * In the establishment case, a correct peer response will 3993126258Smlaier * validate the connection, go through the normal state code 3994126258Smlaier * and keep updating the state TTL. 3995126258Smlaier */ 3996126258Smlaier 3997223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 3998126258Smlaier printf("pf: loose state match: "); 3999126258Smlaier pf_print_state(*state); 4000126258Smlaier pf_print_flags(th->th_flags); 4001171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4002223637Sbz "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 4003223637Sbz pd->p_len, ackskew, (unsigned long long)(*state)->packets[0], 4004223637Sbz (unsigned long long)(*state)->packets[1], 4005223637Sbz pd->dir == PF_IN ? "in" : "out", 4006223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4007126258Smlaier } 4008126258Smlaier 4009145836Smlaier if (dst->scrub || src->scrub) { 4010145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 4011200930Sdelphij *state, src, dst, copyback)) 4012145836Smlaier return (PF_DROP); 4013145836Smlaier } 4014145836Smlaier 4015126258Smlaier /* update max window */ 4016126258Smlaier if (src->max_win < win) 4017126258Smlaier src->max_win = win; 4018126258Smlaier /* synchronize sequencing */ 4019126258Smlaier if (SEQ_GT(end, src->seqlo)) 4020126258Smlaier src->seqlo = end; 4021126258Smlaier /* slide the window of what the other end can send */ 4022126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 4023126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 4024126258Smlaier 4025126258Smlaier /* 4026126258Smlaier * Cannot set dst->seqhi here since this could be a shotgunned 4027126258Smlaier * SYN and not an already established connection. 4028126258Smlaier */ 4029126258Smlaier 4030126258Smlaier if (th->th_flags & TH_FIN) 4031126258Smlaier if (src->state < TCPS_CLOSING) 4032126258Smlaier src->state = TCPS_CLOSING; 4033126258Smlaier if (th->th_flags & TH_RST) 4034126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 4035126258Smlaier 4036126258Smlaier /* Fall through to PASS packet */ 4037126258Smlaier 4038126258Smlaier } else { 4039126258Smlaier if ((*state)->dst.state == TCPS_SYN_SENT && 4040126258Smlaier (*state)->src.state == TCPS_SYN_SENT) { 4041126258Smlaier /* Send RST for state mismatches during handshake */ 4042145836Smlaier if (!(th->th_flags & TH_RST)) 4043223637Sbz pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4044126258Smlaier pd->dst, pd->src, th->th_dport, 4045145836Smlaier th->th_sport, ntohl(th->th_ack), 0, 4046145836Smlaier TH_RST, 0, 0, 4047171168Smlaier (*state)->rule.ptr->return_ttl, 1, 0, 4048240233Sglebius kif->pfik_ifp); 4049126258Smlaier src->seqlo = 0; 4050126258Smlaier src->seqhi = 1; 4051126258Smlaier src->max_win = 1; 4052223637Sbz } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 4053126258Smlaier printf("pf: BAD state: "); 4054126258Smlaier pf_print_state(*state); 4055126258Smlaier pf_print_flags(th->th_flags); 4056171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4057171168Smlaier "pkts=%llu:%llu dir=%s,%s\n", 4058171168Smlaier seq, orig_seq, ack, pd->p_len, ackskew, 4059171168Smlaier (unsigned long long)(*state)->packets[0], 4060171168Smlaier (unsigned long long)(*state)->packets[1], 4061223637Sbz pd->dir == PF_IN ? "in" : "out", 4062223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4063126258Smlaier printf("pf: State failure on: %c %c %c %c | %c %c\n", 4064126258Smlaier SEQ_GEQ(src->seqhi, end) ? ' ' : '1', 4065126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 4066126258Smlaier ' ': '2', 4067126258Smlaier (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 4068126258Smlaier (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 4069126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5', 4070126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 4071126258Smlaier } 4072145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4073126258Smlaier return (PF_DROP); 4074126258Smlaier } 4075126258Smlaier 4076200930Sdelphij return (PF_PASS); 4077200930Sdelphij} 4078126258Smlaier 4079240233Sglebiusstatic int 4080200930Sdelphijpf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst, 4081200930Sdelphij struct pf_state **state, struct pf_pdesc *pd, u_short *reason) 4082200930Sdelphij{ 4083200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4084200930Sdelphij 4085200930Sdelphij if (th->th_flags & TH_SYN) 4086200930Sdelphij if (src->state < TCPS_SYN_SENT) 4087200930Sdelphij src->state = TCPS_SYN_SENT; 4088200930Sdelphij if (th->th_flags & TH_FIN) 4089200930Sdelphij if (src->state < TCPS_CLOSING) 4090200930Sdelphij src->state = TCPS_CLOSING; 4091200930Sdelphij if (th->th_flags & TH_ACK) { 4092200930Sdelphij if (dst->state == TCPS_SYN_SENT) { 4093200930Sdelphij dst->state = TCPS_ESTABLISHED; 4094200930Sdelphij if (src->state == TCPS_ESTABLISHED && 4095200930Sdelphij (*state)->src_node != NULL && 4096200930Sdelphij pf_src_connlimit(state)) { 4097200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4098200930Sdelphij return (PF_DROP); 4099200930Sdelphij } 4100200930Sdelphij } else if (dst->state == TCPS_CLOSING) { 4101200930Sdelphij dst->state = TCPS_FIN_WAIT_2; 4102200930Sdelphij } else if (src->state == TCPS_SYN_SENT && 4103200930Sdelphij dst->state < TCPS_SYN_SENT) { 4104200930Sdelphij /* 4105200930Sdelphij * Handle a special sloppy case where we only see one 4106200930Sdelphij * half of the connection. If there is a ACK after 4107200930Sdelphij * the initial SYN without ever seeing a packet from 4108200930Sdelphij * the destination, set the connection to established. 4109200930Sdelphij */ 4110200930Sdelphij dst->state = src->state = TCPS_ESTABLISHED; 4111200930Sdelphij if ((*state)->src_node != NULL && 4112200930Sdelphij pf_src_connlimit(state)) { 4113200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4114200930Sdelphij return (PF_DROP); 4115200930Sdelphij } 4116200930Sdelphij } else if (src->state == TCPS_CLOSING && 4117200930Sdelphij dst->state == TCPS_ESTABLISHED && 4118200930Sdelphij dst->seqlo == 0) { 4119200930Sdelphij /* 4120200930Sdelphij * Handle the closing of half connections where we 4121200930Sdelphij * don't see the full bidirectional FIN/ACK+ACK 4122200930Sdelphij * handshake. 4123200930Sdelphij */ 4124200930Sdelphij dst->state = TCPS_CLOSING; 4125200930Sdelphij } 4126200930Sdelphij } 4127200930Sdelphij if (th->th_flags & TH_RST) 4128200930Sdelphij src->state = dst->state = TCPS_TIME_WAIT; 4129200930Sdelphij 4130200930Sdelphij /* update expire time */ 4131240233Sglebius (*state)->expire = time_uptime; 4132200930Sdelphij if (src->state >= TCPS_FIN_WAIT_2 && 4133200930Sdelphij dst->state >= TCPS_FIN_WAIT_2) 4134200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSED; 4135200930Sdelphij else if (src->state >= TCPS_CLOSING && 4136200930Sdelphij dst->state >= TCPS_CLOSING) 4137200930Sdelphij (*state)->timeout = PFTM_TCP_FIN_WAIT; 4138200930Sdelphij else if (src->state < TCPS_ESTABLISHED || 4139200930Sdelphij dst->state < TCPS_ESTABLISHED) 4140200930Sdelphij (*state)->timeout = PFTM_TCP_OPENING; 4141200930Sdelphij else if (src->state >= TCPS_CLOSING || 4142200930Sdelphij dst->state >= TCPS_CLOSING) 4143200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSING; 4144200930Sdelphij else 4145200930Sdelphij (*state)->timeout = PFTM_TCP_ESTABLISHED; 4146200930Sdelphij 4147200930Sdelphij return (PF_PASS); 4148200930Sdelphij} 4149200930Sdelphij 4150240233Sglebiusstatic int 4151200930Sdelphijpf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif, 4152200930Sdelphij struct mbuf *m, int off, void *h, struct pf_pdesc *pd, 4153200930Sdelphij u_short *reason) 4154200930Sdelphij{ 4155223637Sbz struct pf_state_key_cmp key; 4156200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4157200930Sdelphij int copyback = 0; 4158200930Sdelphij struct pf_state_peer *src, *dst; 4159223637Sbz struct pf_state_key *sk; 4160200930Sdelphij 4161240233Sglebius bzero(&key, sizeof(key)); 4162200930Sdelphij key.af = pd->af; 4163200930Sdelphij key.proto = IPPROTO_TCP; 4164223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4165223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4166223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4167223637Sbz key.port[0] = th->th_sport; 4168223637Sbz key.port[1] = th->th_dport; 4169223637Sbz } else { /* stack side, reverse */ 4170223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4171223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4172223637Sbz key.port[1] = th->th_sport; 4173223637Sbz key.port[0] = th->th_dport; 4174200930Sdelphij } 4175200930Sdelphij 4176240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4177200930Sdelphij 4178200930Sdelphij if (direction == (*state)->direction) { 4179200930Sdelphij src = &(*state)->src; 4180200930Sdelphij dst = &(*state)->dst; 4181200930Sdelphij } else { 4182200930Sdelphij src = &(*state)->dst; 4183200930Sdelphij dst = &(*state)->src; 4184200930Sdelphij } 4185200930Sdelphij 4186223637Sbz sk = (*state)->key[pd->didx]; 4187223637Sbz 4188200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_SRC) { 4189200930Sdelphij if (direction != (*state)->direction) { 4190200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4191200930Sdelphij return (PF_SYNPROXY_DROP); 4192200930Sdelphij } 4193200930Sdelphij if (th->th_flags & TH_SYN) { 4194200930Sdelphij if (ntohl(th->th_seq) != (*state)->src.seqlo) { 4195200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4196200930Sdelphij return (PF_DROP); 4197200930Sdelphij } 4198200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4199200930Sdelphij pd->src, th->th_dport, th->th_sport, 4200200930Sdelphij (*state)->src.seqhi, ntohl(th->th_seq) + 1, 4201240233Sglebius TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL); 4202200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4203200930Sdelphij return (PF_SYNPROXY_DROP); 4204200930Sdelphij } else if (!(th->th_flags & TH_ACK) || 4205200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4206200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4207200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4208200930Sdelphij return (PF_DROP); 4209200930Sdelphij } else if ((*state)->src_node != NULL && 4210200930Sdelphij pf_src_connlimit(state)) { 4211200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4212200930Sdelphij return (PF_DROP); 4213200930Sdelphij } else 4214200930Sdelphij (*state)->src.state = PF_TCPS_PROXY_DST; 4215200930Sdelphij } 4216200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_DST) { 4217200930Sdelphij if (direction == (*state)->direction) { 4218200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) || 4219200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4220200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4221200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4222200930Sdelphij return (PF_DROP); 4223200930Sdelphij } 4224200930Sdelphij (*state)->src.max_win = MAX(ntohs(th->th_win), 1); 4225200930Sdelphij if ((*state)->dst.seqhi == 1) 4226200930Sdelphij (*state)->dst.seqhi = htonl(arc4random()); 4227200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4228223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4229223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4230200930Sdelphij (*state)->dst.seqhi, 0, TH_SYN, 0, 4231240233Sglebius (*state)->src.mss, 0, 0, (*state)->tag, NULL); 4232200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4233200930Sdelphij return (PF_SYNPROXY_DROP); 4234200930Sdelphij } else if (((th->th_flags & (TH_SYN|TH_ACK)) != 4235200930Sdelphij (TH_SYN|TH_ACK)) || 4236200930Sdelphij (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) { 4237200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4238200930Sdelphij return (PF_DROP); 4239200930Sdelphij } else { 4240200930Sdelphij (*state)->dst.max_win = MAX(ntohs(th->th_win), 1); 4241200930Sdelphij (*state)->dst.seqlo = ntohl(th->th_seq); 4242200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4243200930Sdelphij pd->src, th->th_dport, th->th_sport, 4244200930Sdelphij ntohl(th->th_ack), ntohl(th->th_seq) + 1, 4245200930Sdelphij TH_ACK, (*state)->src.max_win, 0, 0, 0, 4246240233Sglebius (*state)->tag, NULL); 4247200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4248223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4249223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4250200930Sdelphij (*state)->src.seqhi + 1, (*state)->src.seqlo + 1, 4251240233Sglebius TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL); 4252200930Sdelphij (*state)->src.seqdiff = (*state)->dst.seqhi - 4253200930Sdelphij (*state)->src.seqlo; 4254200930Sdelphij (*state)->dst.seqdiff = (*state)->src.seqhi - 4255200930Sdelphij (*state)->dst.seqlo; 4256200930Sdelphij (*state)->src.seqhi = (*state)->src.seqlo + 4257200930Sdelphij (*state)->dst.max_win; 4258200930Sdelphij (*state)->dst.seqhi = (*state)->dst.seqlo + 4259200930Sdelphij (*state)->src.max_win; 4260200930Sdelphij (*state)->src.wscale = (*state)->dst.wscale = 0; 4261200930Sdelphij (*state)->src.state = (*state)->dst.state = 4262200930Sdelphij TCPS_ESTABLISHED; 4263200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4264200930Sdelphij return (PF_SYNPROXY_DROP); 4265200930Sdelphij } 4266200930Sdelphij } 4267200930Sdelphij 4268200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) && 4269200930Sdelphij dst->state >= TCPS_FIN_WAIT_2 && 4270200930Sdelphij src->state >= TCPS_FIN_WAIT_2) { 4271223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4272200930Sdelphij printf("pf: state reuse "); 4273200930Sdelphij pf_print_state(*state); 4274200930Sdelphij pf_print_flags(th->th_flags); 4275200930Sdelphij printf("\n"); 4276200930Sdelphij } 4277200930Sdelphij /* XXX make sure it's the same direction ?? */ 4278200930Sdelphij (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 4279240233Sglebius pf_unlink_state(*state, PF_ENTER_LOCKED); 4280200930Sdelphij *state = NULL; 4281200930Sdelphij return (PF_DROP); 4282200930Sdelphij } 4283200930Sdelphij 4284200930Sdelphij if ((*state)->state_flags & PFSTATE_SLOPPY) { 4285200930Sdelphij if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP) 4286200930Sdelphij return (PF_DROP); 4287200930Sdelphij } else { 4288200930Sdelphij if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason, 4289200930Sdelphij ©back) == PF_DROP) 4290200930Sdelphij return (PF_DROP); 4291200930Sdelphij } 4292200930Sdelphij 4293126258Smlaier /* translate source/destination address, if necessary */ 4294223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4295223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4296223637Sbz 4297223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4298223637Sbz nk->port[pd->sidx] != th->th_sport) 4299126258Smlaier pf_change_ap(pd->src, &th->th_sport, pd->ip_sum, 4300223637Sbz &th->th_sum, &nk->addr[pd->sidx], 4301223637Sbz nk->port[pd->sidx], 0, pd->af); 4302223637Sbz 4303223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4304223637Sbz nk->port[pd->didx] != th->th_dport) 4305126258Smlaier pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum, 4306223637Sbz &th->th_sum, &nk->addr[pd->didx], 4307223637Sbz nk->port[pd->didx], 0, pd->af); 4308223637Sbz copyback = 1; 4309126258Smlaier } 4310126258Smlaier 4311223637Sbz /* Copyback sequence modulation or stateful scrub changes if needed */ 4312223637Sbz if (copyback) 4313223637Sbz m_copyback(m, off, sizeof(*th), (caddr_t)th); 4314223637Sbz 4315126258Smlaier return (PF_PASS); 4316126258Smlaier} 4317126258Smlaier 4318240233Sglebiusstatic int 4319130613Smlaierpf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif, 4320130613Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd) 4321126258Smlaier{ 4322126258Smlaier struct pf_state_peer *src, *dst; 4323223637Sbz struct pf_state_key_cmp key; 4324126258Smlaier struct udphdr *uh = pd->hdr.udp; 4325126258Smlaier 4326240233Sglebius bzero(&key, sizeof(key)); 4327126258Smlaier key.af = pd->af; 4328126258Smlaier key.proto = IPPROTO_UDP; 4329223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4330223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4331223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4332223637Sbz key.port[0] = uh->uh_sport; 4333223637Sbz key.port[1] = uh->uh_dport; 4334223637Sbz } else { /* stack side, reverse */ 4335223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4336223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4337223637Sbz key.port[1] = uh->uh_sport; 4338223637Sbz key.port[0] = uh->uh_dport; 4339130613Smlaier } 4340126258Smlaier 4341240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4342126258Smlaier 4343126258Smlaier if (direction == (*state)->direction) { 4344126258Smlaier src = &(*state)->src; 4345126258Smlaier dst = &(*state)->dst; 4346126258Smlaier } else { 4347126258Smlaier src = &(*state)->dst; 4348126258Smlaier dst = &(*state)->src; 4349126258Smlaier } 4350126258Smlaier 4351126258Smlaier /* update states */ 4352126258Smlaier if (src->state < PFUDPS_SINGLE) 4353126258Smlaier src->state = PFUDPS_SINGLE; 4354126258Smlaier if (dst->state == PFUDPS_SINGLE) 4355126258Smlaier dst->state = PFUDPS_MULTIPLE; 4356126258Smlaier 4357126258Smlaier /* update expire time */ 4358240233Sglebius (*state)->expire = time_uptime; 4359126258Smlaier if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 4360126258Smlaier (*state)->timeout = PFTM_UDP_MULTIPLE; 4361126258Smlaier else 4362126258Smlaier (*state)->timeout = PFTM_UDP_SINGLE; 4363126258Smlaier 4364126258Smlaier /* translate source/destination address, if necessary */ 4365223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4366223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4367223637Sbz 4368223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4369223637Sbz nk->port[pd->sidx] != uh->uh_sport) 4370126258Smlaier pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum, 4371223637Sbz &uh->uh_sum, &nk->addr[pd->sidx], 4372223637Sbz nk->port[pd->sidx], 1, pd->af); 4373223637Sbz 4374223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4375223637Sbz nk->port[pd->didx] != uh->uh_dport) 4376126258Smlaier pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum, 4377223637Sbz &uh->uh_sum, &nk->addr[pd->didx], 4378223637Sbz nk->port[pd->didx], 1, pd->af); 4379126261Smlaier m_copyback(m, off, sizeof(*uh), (caddr_t)uh); 4380126258Smlaier } 4381126258Smlaier 4382126258Smlaier return (PF_PASS); 4383126258Smlaier} 4384126258Smlaier 4385240233Sglebiusstatic int 4386130613Smlaierpf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif, 4387145836Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason) 4388126258Smlaier{ 4389223637Sbz struct pf_addr *saddr = pd->src, *daddr = pd->dst; 4390223637Sbz u_int16_t icmpid = 0, *icmpsum; 4391223637Sbz u_int8_t icmptype; 4392130613Smlaier int state_icmp = 0; 4393223637Sbz struct pf_state_key_cmp key; 4394126258Smlaier 4395240233Sglebius bzero(&key, sizeof(key)); 4396126258Smlaier switch (pd->proto) { 4397126258Smlaier#ifdef INET 4398126258Smlaier case IPPROTO_ICMP: 4399126258Smlaier icmptype = pd->hdr.icmp->icmp_type; 4400126258Smlaier icmpid = pd->hdr.icmp->icmp_id; 4401126258Smlaier icmpsum = &pd->hdr.icmp->icmp_cksum; 4402126258Smlaier 4403126258Smlaier if (icmptype == ICMP_UNREACH || 4404126258Smlaier icmptype == ICMP_SOURCEQUENCH || 4405126258Smlaier icmptype == ICMP_REDIRECT || 4406126258Smlaier icmptype == ICMP_TIMXCEED || 4407126258Smlaier icmptype == ICMP_PARAMPROB) 4408126258Smlaier state_icmp++; 4409126258Smlaier break; 4410126258Smlaier#endif /* INET */ 4411126258Smlaier#ifdef INET6 4412126258Smlaier case IPPROTO_ICMPV6: 4413126258Smlaier icmptype = pd->hdr.icmp6->icmp6_type; 4414126258Smlaier icmpid = pd->hdr.icmp6->icmp6_id; 4415126258Smlaier icmpsum = &pd->hdr.icmp6->icmp6_cksum; 4416126258Smlaier 4417126258Smlaier if (icmptype == ICMP6_DST_UNREACH || 4418126258Smlaier icmptype == ICMP6_PACKET_TOO_BIG || 4419126258Smlaier icmptype == ICMP6_TIME_EXCEEDED || 4420126258Smlaier icmptype == ICMP6_PARAM_PROB) 4421126258Smlaier state_icmp++; 4422126258Smlaier break; 4423126258Smlaier#endif /* INET6 */ 4424126258Smlaier } 4425126258Smlaier 4426126258Smlaier if (!state_icmp) { 4427126258Smlaier 4428126258Smlaier /* 4429126258Smlaier * ICMP query/reply message not related to a TCP/UDP packet. 4430126258Smlaier * Search for an ICMP state. 4431126258Smlaier */ 4432126258Smlaier key.af = pd->af; 4433126258Smlaier key.proto = pd->proto; 4434223637Sbz key.port[0] = key.port[1] = icmpid; 4435223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4436223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4437223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4438223637Sbz } else { /* stack side, reverse */ 4439223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4440223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4441130613Smlaier } 4442126258Smlaier 4443240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4444126258Smlaier 4445240233Sglebius (*state)->expire = time_uptime; 4446126258Smlaier (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 4447126258Smlaier 4448126258Smlaier /* translate source/destination address, if necessary */ 4449223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4450223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4451223637Sbz 4452223637Sbz switch (pd->af) { 4453126258Smlaier#ifdef INET 4454223637Sbz case AF_INET: 4455223637Sbz if (PF_ANEQ(pd->src, 4456223637Sbz &nk->addr[pd->sidx], AF_INET)) 4457126258Smlaier pf_change_a(&saddr->v4.s_addr, 4458126258Smlaier pd->ip_sum, 4459223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 4460223637Sbz 4461223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], 4462223637Sbz AF_INET)) 4463223637Sbz pf_change_a(&daddr->v4.s_addr, 4464223637Sbz pd->ip_sum, 4465223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 4466223637Sbz 4467223637Sbz if (nk->port[0] != 4468223637Sbz pd->hdr.icmp->icmp_id) { 4469149884Smlaier pd->hdr.icmp->icmp_cksum = 4470149884Smlaier pf_cksum_fixup( 4471149884Smlaier pd->hdr.icmp->icmp_cksum, icmpid, 4472223637Sbz nk->port[pd->sidx], 0); 4473149884Smlaier pd->hdr.icmp->icmp_id = 4474223637Sbz nk->port[pd->sidx]; 4475223637Sbz } 4476223637Sbz 4477223637Sbz m_copyback(m, off, ICMP_MINLEN, 4478240233Sglebius (caddr_t )pd->hdr.icmp); 4479223637Sbz break; 4480126258Smlaier#endif /* INET */ 4481126258Smlaier#ifdef INET6 4482223637Sbz case AF_INET6: 4483223637Sbz if (PF_ANEQ(pd->src, 4484223637Sbz &nk->addr[pd->sidx], AF_INET6)) 4485126258Smlaier pf_change_a6(saddr, 4486126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4487223637Sbz &nk->addr[pd->sidx], 0); 4488223637Sbz 4489223637Sbz if (PF_ANEQ(pd->dst, 4490223637Sbz &nk->addr[pd->didx], AF_INET6)) 4491126258Smlaier pf_change_a6(daddr, 4492126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4493223637Sbz &nk->addr[pd->didx], 0); 4494223637Sbz 4495240233Sglebius m_copyback(m, off, sizeof(struct icmp6_hdr), 4496240233Sglebius (caddr_t )pd->hdr.icmp6); 4497223637Sbz break; 4498126258Smlaier#endif /* INET6 */ 4499126258Smlaier } 4500126258Smlaier } 4501126258Smlaier return (PF_PASS); 4502126258Smlaier 4503126258Smlaier } else { 4504126258Smlaier /* 4505126258Smlaier * ICMP error message in response to a TCP/UDP packet. 4506126258Smlaier * Extract the inner TCP/UDP header and search for that state. 4507126258Smlaier */ 4508126258Smlaier 4509126258Smlaier struct pf_pdesc pd2; 4510223637Sbz bzero(&pd2, sizeof pd2); 4511126258Smlaier#ifdef INET 4512126258Smlaier struct ip h2; 4513126258Smlaier#endif /* INET */ 4514126258Smlaier#ifdef INET6 4515126258Smlaier struct ip6_hdr h2_6; 4516126258Smlaier int terminal = 0; 4517126258Smlaier#endif /* INET6 */ 4518223637Sbz int ipoff2 = 0; 4519223637Sbz int off2 = 0; 4520126258Smlaier 4521126258Smlaier pd2.af = pd->af; 4522223637Sbz /* Payload packet is from the opposite direction. */ 4523223637Sbz pd2.sidx = (direction == PF_IN) ? 1 : 0; 4524223637Sbz pd2.didx = (direction == PF_IN) ? 0 : 1; 4525126258Smlaier switch (pd->af) { 4526126258Smlaier#ifdef INET 4527126258Smlaier case AF_INET: 4528126258Smlaier /* offset of h2 in mbuf chain */ 4529126258Smlaier ipoff2 = off + ICMP_MINLEN; 4530126258Smlaier 4531126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2), 4532145836Smlaier NULL, reason, pd2.af)) { 4533126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4534126258Smlaier ("pf: ICMP error message too short " 4535126258Smlaier "(ip)\n")); 4536126258Smlaier return (PF_DROP); 4537126258Smlaier } 4538126258Smlaier /* 4539126258Smlaier * ICMP error messages don't refer to non-first 4540126258Smlaier * fragments 4541126258Smlaier */ 4542145836Smlaier if (h2.ip_off & htons(IP_OFFMASK)) { 4543145836Smlaier REASON_SET(reason, PFRES_FRAG); 4544126258Smlaier return (PF_DROP); 4545145836Smlaier } 4546126258Smlaier 4547126258Smlaier /* offset of protocol header that follows h2 */ 4548126258Smlaier off2 = ipoff2 + (h2.ip_hl << 2); 4549126258Smlaier 4550126258Smlaier pd2.proto = h2.ip_p; 4551126258Smlaier pd2.src = (struct pf_addr *)&h2.ip_src; 4552126258Smlaier pd2.dst = (struct pf_addr *)&h2.ip_dst; 4553126258Smlaier pd2.ip_sum = &h2.ip_sum; 4554126258Smlaier break; 4555126258Smlaier#endif /* INET */ 4556126258Smlaier#ifdef INET6 4557126258Smlaier case AF_INET6: 4558126258Smlaier ipoff2 = off + sizeof(struct icmp6_hdr); 4559126258Smlaier 4560126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6), 4561145836Smlaier NULL, reason, pd2.af)) { 4562126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4563126258Smlaier ("pf: ICMP error message too short " 4564126258Smlaier "(ip6)\n")); 4565126258Smlaier return (PF_DROP); 4566126258Smlaier } 4567126258Smlaier pd2.proto = h2_6.ip6_nxt; 4568126258Smlaier pd2.src = (struct pf_addr *)&h2_6.ip6_src; 4569126258Smlaier pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 4570126258Smlaier pd2.ip_sum = NULL; 4571126258Smlaier off2 = ipoff2 + sizeof(h2_6); 4572126258Smlaier do { 4573126258Smlaier switch (pd2.proto) { 4574126258Smlaier case IPPROTO_FRAGMENT: 4575126258Smlaier /* 4576126258Smlaier * ICMPv6 error messages for 4577126258Smlaier * non-first fragments 4578126258Smlaier */ 4579145836Smlaier REASON_SET(reason, PFRES_FRAG); 4580126258Smlaier return (PF_DROP); 4581126258Smlaier case IPPROTO_AH: 4582126258Smlaier case IPPROTO_HOPOPTS: 4583126258Smlaier case IPPROTO_ROUTING: 4584126258Smlaier case IPPROTO_DSTOPTS: { 4585126258Smlaier /* get next header and header length */ 4586126258Smlaier struct ip6_ext opt6; 4587126258Smlaier 4588126258Smlaier if (!pf_pull_hdr(m, off2, &opt6, 4589145836Smlaier sizeof(opt6), NULL, reason, 4590145836Smlaier pd2.af)) { 4591126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4592126258Smlaier ("pf: ICMPv6 short opt\n")); 4593126258Smlaier return (PF_DROP); 4594126258Smlaier } 4595126258Smlaier if (pd2.proto == IPPROTO_AH) 4596126258Smlaier off2 += (opt6.ip6e_len + 2) * 4; 4597126258Smlaier else 4598126258Smlaier off2 += (opt6.ip6e_len + 1) * 8; 4599126258Smlaier pd2.proto = opt6.ip6e_nxt; 4600126258Smlaier /* goto the next header */ 4601126258Smlaier break; 4602126258Smlaier } 4603126258Smlaier default: 4604126258Smlaier terminal++; 4605126258Smlaier break; 4606126258Smlaier } 4607126258Smlaier } while (!terminal); 4608126258Smlaier break; 4609126258Smlaier#endif /* INET6 */ 4610126258Smlaier } 4611126258Smlaier 4612126258Smlaier switch (pd2.proto) { 4613126258Smlaier case IPPROTO_TCP: { 4614126258Smlaier struct tcphdr th; 4615126258Smlaier u_int32_t seq; 4616126258Smlaier struct pf_state_peer *src, *dst; 4617126258Smlaier u_int8_t dws; 4618128129Smlaier int copyback = 0; 4619126258Smlaier 4620126258Smlaier /* 4621126258Smlaier * Only the first 8 bytes of the TCP header can be 4622126258Smlaier * expected. Don't access any TCP header fields after 4623126258Smlaier * th_seq, an ackskew test is not possible. 4624126258Smlaier */ 4625145836Smlaier if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason, 4626145836Smlaier pd2.af)) { 4627126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4628126258Smlaier ("pf: ICMP error message too short " 4629126258Smlaier "(tcp)\n")); 4630126258Smlaier return (PF_DROP); 4631126258Smlaier } 4632126258Smlaier 4633126258Smlaier key.af = pd2.af; 4634126258Smlaier key.proto = IPPROTO_TCP; 4635223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4636223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4637223637Sbz key.port[pd2.sidx] = th.th_sport; 4638223637Sbz key.port[pd2.didx] = th.th_dport; 4639126258Smlaier 4640240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4641126258Smlaier 4642126258Smlaier if (direction == (*state)->direction) { 4643126258Smlaier src = &(*state)->dst; 4644126258Smlaier dst = &(*state)->src; 4645126258Smlaier } else { 4646126258Smlaier src = &(*state)->src; 4647126258Smlaier dst = &(*state)->dst; 4648126258Smlaier } 4649126258Smlaier 4650171929Sdhartmei if (src->wscale && dst->wscale) 4651126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 4652126258Smlaier else 4653126258Smlaier dws = 0; 4654126258Smlaier 4655126258Smlaier /* Demodulate sequence number */ 4656126258Smlaier seq = ntohl(th.th_seq) - src->seqdiff; 4657128129Smlaier if (src->seqdiff) { 4658128129Smlaier pf_change_a(&th.th_seq, icmpsum, 4659126258Smlaier htonl(seq), 0); 4660128129Smlaier copyback = 1; 4661128129Smlaier } 4662126258Smlaier 4663200930Sdelphij if (!((*state)->state_flags & PFSTATE_SLOPPY) && 4664200930Sdelphij (!SEQ_GEQ(src->seqhi, seq) || 4665200930Sdelphij !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 4666223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4667126258Smlaier printf("pf: BAD ICMP %d:%d ", 4668126258Smlaier icmptype, pd->hdr.icmp->icmp_code); 4669126258Smlaier pf_print_host(pd->src, 0, pd->af); 4670126258Smlaier printf(" -> "); 4671126258Smlaier pf_print_host(pd->dst, 0, pd->af); 4672126258Smlaier printf(" state: "); 4673126258Smlaier pf_print_state(*state); 4674126258Smlaier printf(" seq=%u\n", seq); 4675126258Smlaier } 4676145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4677126258Smlaier return (PF_DROP); 4678223637Sbz } else { 4679223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4680223637Sbz printf("pf: OK ICMP %d:%d ", 4681223637Sbz icmptype, pd->hdr.icmp->icmp_code); 4682223637Sbz pf_print_host(pd->src, 0, pd->af); 4683223637Sbz printf(" -> "); 4684223637Sbz pf_print_host(pd->dst, 0, pd->af); 4685223637Sbz printf(" state: "); 4686223637Sbz pf_print_state(*state); 4687223637Sbz printf(" seq=%u\n", seq); 4688223637Sbz } 4689126258Smlaier } 4690126258Smlaier 4691223637Sbz /* translate source/destination address, if necessary */ 4692223637Sbz if ((*state)->key[PF_SK_WIRE] != 4693223637Sbz (*state)->key[PF_SK_STACK]) { 4694223637Sbz struct pf_state_key *nk = 4695223637Sbz (*state)->key[pd->didx]; 4696223637Sbz 4697223637Sbz if (PF_ANEQ(pd2.src, 4698223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4699223637Sbz nk->port[pd2.sidx] != th.th_sport) 4700126258Smlaier pf_change_icmp(pd2.src, &th.th_sport, 4701223637Sbz daddr, &nk->addr[pd2.sidx], 4702223637Sbz nk->port[pd2.sidx], NULL, 4703126258Smlaier pd2.ip_sum, icmpsum, 4704126258Smlaier pd->ip_sum, 0, pd2.af); 4705223637Sbz 4706223637Sbz if (PF_ANEQ(pd2.dst, 4707223637Sbz &nk->addr[pd2.didx], pd2.af) || 4708223637Sbz nk->port[pd2.didx] != th.th_dport) 4709126258Smlaier pf_change_icmp(pd2.dst, &th.th_dport, 4710223637Sbz NULL, /* XXX Inbound NAT? */ 4711223637Sbz &nk->addr[pd2.didx], 4712223637Sbz nk->port[pd2.didx], NULL, 4713126258Smlaier pd2.ip_sum, icmpsum, 4714126258Smlaier pd->ip_sum, 0, pd2.af); 4715128129Smlaier copyback = 1; 4716128129Smlaier } 4717128129Smlaier 4718128129Smlaier if (copyback) { 4719126258Smlaier switch (pd2.af) { 4720126258Smlaier#ifdef INET 4721126258Smlaier case AF_INET: 4722126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4723240233Sglebius (caddr_t )pd->hdr.icmp); 4724126258Smlaier m_copyback(m, ipoff2, sizeof(h2), 4725240233Sglebius (caddr_t )&h2); 4726126258Smlaier break; 4727126258Smlaier#endif /* INET */ 4728126258Smlaier#ifdef INET6 4729126258Smlaier case AF_INET6: 4730126258Smlaier m_copyback(m, off, 4731126258Smlaier sizeof(struct icmp6_hdr), 4732240233Sglebius (caddr_t )pd->hdr.icmp6); 4733126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4734240233Sglebius (caddr_t )&h2_6); 4735126258Smlaier break; 4736126258Smlaier#endif /* INET6 */ 4737126258Smlaier } 4738126261Smlaier m_copyback(m, off2, 8, (caddr_t)&th); 4739126258Smlaier } 4740126258Smlaier 4741126258Smlaier return (PF_PASS); 4742126258Smlaier break; 4743126258Smlaier } 4744126258Smlaier case IPPROTO_UDP: { 4745126258Smlaier struct udphdr uh; 4746126258Smlaier 4747126258Smlaier if (!pf_pull_hdr(m, off2, &uh, sizeof(uh), 4748145836Smlaier NULL, reason, pd2.af)) { 4749126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4750126258Smlaier ("pf: ICMP error message too short " 4751126258Smlaier "(udp)\n")); 4752126258Smlaier return (PF_DROP); 4753126258Smlaier } 4754126258Smlaier 4755126258Smlaier key.af = pd2.af; 4756126258Smlaier key.proto = IPPROTO_UDP; 4757223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4758223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4759223637Sbz key.port[pd2.sidx] = uh.uh_sport; 4760223637Sbz key.port[pd2.didx] = uh.uh_dport; 4761126258Smlaier 4762240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4763126258Smlaier 4764223637Sbz /* translate source/destination address, if necessary */ 4765223637Sbz if ((*state)->key[PF_SK_WIRE] != 4766223637Sbz (*state)->key[PF_SK_STACK]) { 4767223637Sbz struct pf_state_key *nk = 4768223637Sbz (*state)->key[pd->didx]; 4769223637Sbz 4770223637Sbz if (PF_ANEQ(pd2.src, 4771223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4772223637Sbz nk->port[pd2.sidx] != uh.uh_sport) 4773126258Smlaier pf_change_icmp(pd2.src, &uh.uh_sport, 4774223637Sbz daddr, &nk->addr[pd2.sidx], 4775223637Sbz nk->port[pd2.sidx], &uh.uh_sum, 4776126258Smlaier pd2.ip_sum, icmpsum, 4777126258Smlaier pd->ip_sum, 1, pd2.af); 4778223637Sbz 4779223637Sbz if (PF_ANEQ(pd2.dst, 4780223637Sbz &nk->addr[pd2.didx], pd2.af) || 4781223637Sbz nk->port[pd2.didx] != uh.uh_dport) 4782126258Smlaier pf_change_icmp(pd2.dst, &uh.uh_dport, 4783223637Sbz NULL, /* XXX Inbound NAT? */ 4784223637Sbz &nk->addr[pd2.didx], 4785223637Sbz nk->port[pd2.didx], &uh.uh_sum, 4786126258Smlaier pd2.ip_sum, icmpsum, 4787126258Smlaier pd->ip_sum, 1, pd2.af); 4788223637Sbz 4789126258Smlaier switch (pd2.af) { 4790126258Smlaier#ifdef INET 4791126258Smlaier case AF_INET: 4792126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4793240233Sglebius (caddr_t )pd->hdr.icmp); 4794223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4795126258Smlaier break; 4796126258Smlaier#endif /* INET */ 4797126258Smlaier#ifdef INET6 4798126258Smlaier case AF_INET6: 4799126258Smlaier m_copyback(m, off, 4800126258Smlaier sizeof(struct icmp6_hdr), 4801240233Sglebius (caddr_t )pd->hdr.icmp6); 4802126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4803240233Sglebius (caddr_t )&h2_6); 4804126258Smlaier break; 4805126258Smlaier#endif /* INET6 */ 4806126258Smlaier } 4807223637Sbz m_copyback(m, off2, sizeof(uh), (caddr_t)&uh); 4808126258Smlaier } 4809126258Smlaier return (PF_PASS); 4810126258Smlaier break; 4811126258Smlaier } 4812126258Smlaier#ifdef INET 4813126258Smlaier case IPPROTO_ICMP: { 4814126258Smlaier struct icmp iih; 4815126258Smlaier 4816126258Smlaier if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN, 4817145836Smlaier NULL, reason, pd2.af)) { 4818126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4819126258Smlaier ("pf: ICMP error message too short i" 4820126258Smlaier "(icmp)\n")); 4821126258Smlaier return (PF_DROP); 4822126258Smlaier } 4823126258Smlaier 4824126258Smlaier key.af = pd2.af; 4825126258Smlaier key.proto = IPPROTO_ICMP; 4826223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4827223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4828223637Sbz key.port[0] = key.port[1] = iih.icmp_id; 4829126258Smlaier 4830240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4831126258Smlaier 4832223637Sbz /* translate source/destination address, if necessary */ 4833223637Sbz if ((*state)->key[PF_SK_WIRE] != 4834223637Sbz (*state)->key[PF_SK_STACK]) { 4835223637Sbz struct pf_state_key *nk = 4836223637Sbz (*state)->key[pd->didx]; 4837223637Sbz 4838223637Sbz if (PF_ANEQ(pd2.src, 4839223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4840223637Sbz nk->port[pd2.sidx] != iih.icmp_id) 4841126258Smlaier pf_change_icmp(pd2.src, &iih.icmp_id, 4842223637Sbz daddr, &nk->addr[pd2.sidx], 4843223637Sbz nk->port[pd2.sidx], NULL, 4844126258Smlaier pd2.ip_sum, icmpsum, 4845126258Smlaier pd->ip_sum, 0, AF_INET); 4846223637Sbz 4847223637Sbz if (PF_ANEQ(pd2.dst, 4848223637Sbz &nk->addr[pd2.didx], pd2.af) || 4849223637Sbz nk->port[pd2.didx] != iih.icmp_id) 4850126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp_id, 4851223637Sbz NULL, /* XXX Inbound NAT? */ 4852223637Sbz &nk->addr[pd2.didx], 4853223637Sbz nk->port[pd2.didx], NULL, 4854126258Smlaier pd2.ip_sum, icmpsum, 4855126258Smlaier pd->ip_sum, 0, AF_INET); 4856223637Sbz 4857223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 4858223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4859223637Sbz m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih); 4860126258Smlaier } 4861126258Smlaier return (PF_PASS); 4862126258Smlaier break; 4863126258Smlaier } 4864126258Smlaier#endif /* INET */ 4865126258Smlaier#ifdef INET6 4866126258Smlaier case IPPROTO_ICMPV6: { 4867126258Smlaier struct icmp6_hdr iih; 4868126258Smlaier 4869126258Smlaier if (!pf_pull_hdr(m, off2, &iih, 4870145836Smlaier sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 4871126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4872126258Smlaier ("pf: ICMP error message too short " 4873126258Smlaier "(icmp6)\n")); 4874126258Smlaier return (PF_DROP); 4875126258Smlaier } 4876126258Smlaier 4877126258Smlaier key.af = pd2.af; 4878126258Smlaier key.proto = IPPROTO_ICMPV6; 4879223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4880223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4881223637Sbz key.port[0] = key.port[1] = iih.icmp6_id; 4882126258Smlaier 4883240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4884126258Smlaier 4885223637Sbz /* translate source/destination address, if necessary */ 4886223637Sbz if ((*state)->key[PF_SK_WIRE] != 4887223637Sbz (*state)->key[PF_SK_STACK]) { 4888223637Sbz struct pf_state_key *nk = 4889223637Sbz (*state)->key[pd->didx]; 4890223637Sbz 4891223637Sbz if (PF_ANEQ(pd2.src, 4892223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4893223637Sbz nk->port[pd2.sidx] != iih.icmp6_id) 4894126258Smlaier pf_change_icmp(pd2.src, &iih.icmp6_id, 4895223637Sbz daddr, &nk->addr[pd2.sidx], 4896223637Sbz nk->port[pd2.sidx], NULL, 4897126258Smlaier pd2.ip_sum, icmpsum, 4898126258Smlaier pd->ip_sum, 0, AF_INET6); 4899223637Sbz 4900223637Sbz if (PF_ANEQ(pd2.dst, 4901223637Sbz &nk->addr[pd2.didx], pd2.af) || 4902223637Sbz nk->port[pd2.didx] != iih.icmp6_id) 4903126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp6_id, 4904223637Sbz NULL, /* XXX Inbound NAT? */ 4905223637Sbz &nk->addr[pd2.didx], 4906223637Sbz nk->port[pd2.didx], NULL, 4907126258Smlaier pd2.ip_sum, icmpsum, 4908126258Smlaier pd->ip_sum, 0, AF_INET6); 4909223637Sbz 4910126258Smlaier m_copyback(m, off, sizeof(struct icmp6_hdr), 4911126261Smlaier (caddr_t)pd->hdr.icmp6); 4912223637Sbz m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 4913126258Smlaier m_copyback(m, off2, sizeof(struct icmp6_hdr), 4914126261Smlaier (caddr_t)&iih); 4915126258Smlaier } 4916126258Smlaier return (PF_PASS); 4917126258Smlaier break; 4918126258Smlaier } 4919126258Smlaier#endif /* INET6 */ 4920126258Smlaier default: { 4921126258Smlaier key.af = pd2.af; 4922126258Smlaier key.proto = pd2.proto; 4923223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4924223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4925223637Sbz key.port[0] = key.port[1] = 0; 4926126258Smlaier 4927240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4928126258Smlaier 4929223637Sbz /* translate source/destination address, if necessary */ 4930223637Sbz if ((*state)->key[PF_SK_WIRE] != 4931223637Sbz (*state)->key[PF_SK_STACK]) { 4932223637Sbz struct pf_state_key *nk = 4933223637Sbz (*state)->key[pd->didx]; 4934223637Sbz 4935223637Sbz if (PF_ANEQ(pd2.src, 4936223637Sbz &nk->addr[pd2.sidx], pd2.af)) 4937223637Sbz pf_change_icmp(pd2.src, NULL, daddr, 4938223637Sbz &nk->addr[pd2.sidx], 0, NULL, 4939126258Smlaier pd2.ip_sum, icmpsum, 4940126258Smlaier pd->ip_sum, 0, pd2.af); 4941223637Sbz 4942223637Sbz if (PF_ANEQ(pd2.dst, 4943223637Sbz &nk->addr[pd2.didx], pd2.af)) 4944223637Sbz pf_change_icmp(pd2.src, NULL, 4945223637Sbz NULL, /* XXX Inbound NAT? */ 4946223637Sbz &nk->addr[pd2.didx], 0, NULL, 4947126258Smlaier pd2.ip_sum, icmpsum, 4948126258Smlaier pd->ip_sum, 0, pd2.af); 4949223637Sbz 4950126258Smlaier switch (pd2.af) { 4951126258Smlaier#ifdef INET 4952126258Smlaier case AF_INET: 4953126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4954126261Smlaier (caddr_t)pd->hdr.icmp); 4955223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4956126258Smlaier break; 4957126258Smlaier#endif /* INET */ 4958126258Smlaier#ifdef INET6 4959126258Smlaier case AF_INET6: 4960126258Smlaier m_copyback(m, off, 4961126258Smlaier sizeof(struct icmp6_hdr), 4962240233Sglebius (caddr_t )pd->hdr.icmp6); 4963126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4964240233Sglebius (caddr_t )&h2_6); 4965126258Smlaier break; 4966126258Smlaier#endif /* INET6 */ 4967126258Smlaier } 4968126258Smlaier } 4969126258Smlaier return (PF_PASS); 4970126258Smlaier break; 4971126258Smlaier } 4972126258Smlaier } 4973126258Smlaier } 4974126258Smlaier} 4975126258Smlaier 4976240233Sglebiusstatic int 4977130613Smlaierpf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif, 4978223637Sbz struct mbuf *m, struct pf_pdesc *pd) 4979126258Smlaier{ 4980126258Smlaier struct pf_state_peer *src, *dst; 4981223637Sbz struct pf_state_key_cmp key; 4982126258Smlaier 4983240233Sglebius bzero(&key, sizeof(key)); 4984126258Smlaier key.af = pd->af; 4985126258Smlaier key.proto = pd->proto; 4986130613Smlaier if (direction == PF_IN) { 4987223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4988223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4989223637Sbz key.port[0] = key.port[1] = 0; 4990130613Smlaier } else { 4991223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4992223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4993223637Sbz key.port[1] = key.port[0] = 0; 4994130613Smlaier } 4995126258Smlaier 4996240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4997126258Smlaier 4998126258Smlaier if (direction == (*state)->direction) { 4999126258Smlaier src = &(*state)->src; 5000126258Smlaier dst = &(*state)->dst; 5001126258Smlaier } else { 5002126258Smlaier src = &(*state)->dst; 5003126258Smlaier dst = &(*state)->src; 5004126258Smlaier } 5005126258Smlaier 5006126258Smlaier /* update states */ 5007126258Smlaier if (src->state < PFOTHERS_SINGLE) 5008126258Smlaier src->state = PFOTHERS_SINGLE; 5009126258Smlaier if (dst->state == PFOTHERS_SINGLE) 5010126258Smlaier dst->state = PFOTHERS_MULTIPLE; 5011126258Smlaier 5012126258Smlaier /* update expire time */ 5013240233Sglebius (*state)->expire = time_uptime; 5014126258Smlaier if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 5015126258Smlaier (*state)->timeout = PFTM_OTHER_MULTIPLE; 5016126258Smlaier else 5017126258Smlaier (*state)->timeout = PFTM_OTHER_SINGLE; 5018126258Smlaier 5019126258Smlaier /* translate source/destination address, if necessary */ 5020223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 5021223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 5022223637Sbz 5023240233Sglebius KASSERT(nk, ("%s: nk is null", __func__)); 5024240233Sglebius KASSERT(pd, ("%s: pd is null", __func__)); 5025240233Sglebius KASSERT(pd->src, ("%s: pd->src is null", __func__)); 5026240233Sglebius KASSERT(pd->dst, ("%s: pd->dst is null", __func__)); 5027223637Sbz switch (pd->af) { 5028126258Smlaier#ifdef INET 5029223637Sbz case AF_INET: 5030223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5031126258Smlaier pf_change_a(&pd->src->v4.s_addr, 5032223637Sbz pd->ip_sum, 5033223637Sbz nk->addr[pd->sidx].v4.s_addr, 5034126258Smlaier 0); 5035223637Sbz 5036223637Sbz 5037223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5038126258Smlaier pf_change_a(&pd->dst->v4.s_addr, 5039223637Sbz pd->ip_sum, 5040223637Sbz nk->addr[pd->didx].v4.s_addr, 5041126258Smlaier 0); 5042223637Sbz 5043126258Smlaier break; 5044126258Smlaier#endif /* INET */ 5045126258Smlaier#ifdef INET6 5046223637Sbz case AF_INET6: 5047223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5048223637Sbz PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af); 5049223637Sbz 5050223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5051223637Sbz PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af); 5052126258Smlaier#endif /* INET6 */ 5053223637Sbz } 5054126258Smlaier } 5055126258Smlaier return (PF_PASS); 5056126258Smlaier} 5057126258Smlaier 5058126258Smlaier/* 5059126258Smlaier * ipoff and off are measured from the start of the mbuf chain. 5060126258Smlaier * h must be at "ipoff" on the mbuf chain. 5061126258Smlaier */ 5062126258Smlaiervoid * 5063126258Smlaierpf_pull_hdr(struct mbuf *m, int off, void *p, int len, 5064126258Smlaier u_short *actionp, u_short *reasonp, sa_family_t af) 5065126258Smlaier{ 5066126258Smlaier switch (af) { 5067126258Smlaier#ifdef INET 5068126258Smlaier case AF_INET: { 5069126258Smlaier struct ip *h = mtod(m, struct ip *); 5070126258Smlaier u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 5071126258Smlaier 5072126258Smlaier if (fragoff) { 5073126258Smlaier if (fragoff >= len) 5074126258Smlaier ACTION_SET(actionp, PF_PASS); 5075126258Smlaier else { 5076126258Smlaier ACTION_SET(actionp, PF_DROP); 5077126258Smlaier REASON_SET(reasonp, PFRES_FRAG); 5078126258Smlaier } 5079126258Smlaier return (NULL); 5080126258Smlaier } 5081130613Smlaier if (m->m_pkthdr.len < off + len || 5082130613Smlaier ntohs(h->ip_len) < off + len) { 5083126258Smlaier ACTION_SET(actionp, PF_DROP); 5084126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5085126258Smlaier return (NULL); 5086126258Smlaier } 5087126258Smlaier break; 5088126258Smlaier } 5089126258Smlaier#endif /* INET */ 5090126258Smlaier#ifdef INET6 5091126258Smlaier case AF_INET6: { 5092126258Smlaier struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 5093126258Smlaier 5094126258Smlaier if (m->m_pkthdr.len < off + len || 5095126258Smlaier (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) < 5096126258Smlaier (unsigned)(off + len)) { 5097126258Smlaier ACTION_SET(actionp, PF_DROP); 5098126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5099126258Smlaier return (NULL); 5100126258Smlaier } 5101126258Smlaier break; 5102126258Smlaier } 5103126258Smlaier#endif /* INET6 */ 5104126258Smlaier } 5105126258Smlaier m_copydata(m, off, len, p); 5106126258Smlaier return (p); 5107126258Smlaier} 5108126258Smlaier 5109126258Smlaierint 5110231852Sbzpf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif, 5111231852Sbz int rtableid) 5112126258Smlaier{ 5113223637Sbz#ifdef RADIX_MPATH 5114223637Sbz struct radix_node_head *rnh; 5115223637Sbz#endif 5116126258Smlaier struct sockaddr_in *dst; 5117171168Smlaier int ret = 1; 5118171168Smlaier int check_mpath; 5119145836Smlaier#ifdef INET6 5120145836Smlaier struct sockaddr_in6 *dst6; 5121145836Smlaier struct route_in6 ro; 5122145836Smlaier#else 5123126258Smlaier struct route ro; 5124145836Smlaier#endif 5125171168Smlaier struct radix_node *rn; 5126171168Smlaier struct rtentry *rt; 5127171168Smlaier struct ifnet *ifp; 5128126258Smlaier 5129171168Smlaier check_mpath = 0; 5130223637Sbz#ifdef RADIX_MPATH 5131223637Sbz /* XXX: stick to table 0 for now */ 5132223637Sbz rnh = rt_tables_get_rnh(0, af); 5133223637Sbz if (rnh != NULL && rn_mpath_capable(rnh)) 5134223637Sbz check_mpath = 1; 5135223637Sbz#endif 5136126258Smlaier bzero(&ro, sizeof(ro)); 5137145836Smlaier switch (af) { 5138145836Smlaier case AF_INET: 5139145836Smlaier dst = satosin(&ro.ro_dst); 5140145836Smlaier dst->sin_family = AF_INET; 5141145836Smlaier dst->sin_len = sizeof(*dst); 5142145836Smlaier dst->sin_addr = addr->v4; 5143145836Smlaier break; 5144145836Smlaier#ifdef INET6 5145145836Smlaier case AF_INET6: 5146223637Sbz /* 5147223637Sbz * Skip check for addresses with embedded interface scope, 5148223637Sbz * as they would always match anyway. 5149223637Sbz */ 5150223637Sbz if (IN6_IS_SCOPE_EMBED(&addr->v6)) 5151223637Sbz goto out; 5152145836Smlaier dst6 = (struct sockaddr_in6 *)&ro.ro_dst; 5153145836Smlaier dst6->sin6_family = AF_INET6; 5154145836Smlaier dst6->sin6_len = sizeof(*dst6); 5155145836Smlaier dst6->sin6_addr = addr->v6; 5156145836Smlaier break; 5157145836Smlaier#endif /* INET6 */ 5158145836Smlaier default: 5159145836Smlaier return (0); 5160145836Smlaier } 5161145836Smlaier 5162171168Smlaier /* Skip checks for ipsec interfaces */ 5163171168Smlaier if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 5164171168Smlaier goto out; 5165171168Smlaier 5166231852Sbz switch (af) { 5167231852Sbz#ifdef INET6 5168231852Sbz case AF_INET6: 5169231852Sbz in6_rtalloc_ign(&ro, 0, rtableid); 5170231852Sbz break; 5171231852Sbz#endif 5172222529Sbz#ifdef INET 5173231852Sbz case AF_INET: 5174231852Sbz in_rtalloc_ign((struct route *)&ro, 0, rtableid); 5175231852Sbz break; 5176222529Sbz#endif 5177231852Sbz default: 5178231852Sbz rtalloc_ign((struct route *)&ro, 0); /* No/default FIB. */ 5179231852Sbz break; 5180231852Sbz } 5181126258Smlaier 5182126258Smlaier if (ro.ro_rt != NULL) { 5183171168Smlaier /* No interface given, this is a no-route check */ 5184171168Smlaier if (kif == NULL) 5185171168Smlaier goto out; 5186171168Smlaier 5187171168Smlaier if (kif->pfik_ifp == NULL) { 5188171168Smlaier ret = 0; 5189171168Smlaier goto out; 5190171168Smlaier } 5191171168Smlaier 5192171168Smlaier /* Perform uRPF check if passed input interface */ 5193171168Smlaier ret = 0; 5194171168Smlaier rn = (struct radix_node *)ro.ro_rt; 5195171168Smlaier do { 5196171168Smlaier rt = (struct rtentry *)rn; 5197240233Sglebius ifp = rt->rt_ifp; 5198171168Smlaier 5199171168Smlaier if (kif->pfik_ifp == ifp) 5200171168Smlaier ret = 1; 5201223637Sbz#ifdef RADIX_MPATH 5202171168Smlaier rn = rn_mpath_next(rn); 5203171168Smlaier#endif 5204171168Smlaier } while (check_mpath == 1 && rn != NULL && ret == 0); 5205171168Smlaier } else 5206171168Smlaier ret = 0; 5207171168Smlaierout: 5208171168Smlaier if (ro.ro_rt != NULL) 5209126258Smlaier RTFREE(ro.ro_rt); 5210171168Smlaier return (ret); 5211145836Smlaier} 5212145836Smlaier 5213222529Sbz#ifdef INET 5214240233Sglebiusstatic void 5215126258Smlaierpf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5216171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5217126258Smlaier{ 5218126258Smlaier struct mbuf *m0, *m1; 5219240233Sglebius struct sockaddr_in dst; 5220126258Smlaier struct ip *ip; 5221126258Smlaier struct ifnet *ifp = NULL; 5222126258Smlaier struct pf_addr naddr; 5223130613Smlaier struct pf_src_node *sn = NULL; 5224126258Smlaier int error = 0; 5225242161Sglebius uint16_t ip_len, ip_off; 5226126258Smlaier 5227240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5228240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5229240233Sglebius __func__)); 5230126258Smlaier 5231240233Sglebius if ((pd->pf_mtag == NULL && 5232240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5233240233Sglebius pd->pf_mtag->routed++ > 3) { 5234171168Smlaier m0 = *m; 5235171168Smlaier *m = NULL; 5236240233Sglebius goto bad_locked; 5237132303Smlaier } 5238132303Smlaier 5239126258Smlaier if (r->rt == PF_DUPTO) { 5240240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5241240233Sglebius if (s) 5242240233Sglebius PF_STATE_UNLOCK(s); 5243126258Smlaier return; 5244240233Sglebius } 5245126258Smlaier } else { 5246240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5247240233Sglebius if (s) 5248240233Sglebius PF_STATE_UNLOCK(s); 5249126258Smlaier return; 5250240233Sglebius } 5251126258Smlaier m0 = *m; 5252126258Smlaier } 5253126258Smlaier 5254126258Smlaier ip = mtod(m0, struct ip *); 5255126258Smlaier 5256240233Sglebius bzero(&dst, sizeof(dst)); 5257240233Sglebius dst.sin_family = AF_INET; 5258240233Sglebius dst.sin_len = sizeof(dst); 5259240233Sglebius dst.sin_addr = ip->ip_dst; 5260126258Smlaier 5261126258Smlaier if (r->rt == PF_FASTROUTE) { 5262240233Sglebius struct rtentry *rt; 5263240233Sglebius 5264240233Sglebius if (s) 5265240233Sglebius PF_STATE_UNLOCK(s); 5266240233Sglebius rt = rtalloc1_fib(sintosa(&dst), 0, 0, M_GETFIB(m0)); 5267240233Sglebius if (rt == NULL) { 5268196039Srwatson KMOD_IPSTAT_INC(ips_noroute); 5269240233Sglebius error = EHOSTUNREACH; 5270126258Smlaier goto bad; 5271126258Smlaier } 5272126258Smlaier 5273240233Sglebius ifp = rt->rt_ifp; 5274263478Sglebius counter_u64_add(rt->rt_pksent, 1); 5275126258Smlaier 5276240233Sglebius if (rt->rt_flags & RTF_GATEWAY) 5277240233Sglebius bcopy(satosin(rt->rt_gateway), &dst, sizeof(dst)); 5278240233Sglebius RTFREE_LOCKED(rt); 5279126258Smlaier } else { 5280145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5281145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5282240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5283240233Sglebius goto bad_locked; 5284145836Smlaier } 5285126258Smlaier if (s == NULL) { 5286130613Smlaier pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src, 5287130613Smlaier &naddr, NULL, &sn); 5288126258Smlaier if (!PF_AZERO(&naddr, AF_INET)) 5289240233Sglebius dst.sin_addr.s_addr = naddr.v4.s_addr; 5290130613Smlaier ifp = r->rpool.cur->kif ? 5291130613Smlaier r->rpool.cur->kif->pfik_ifp : NULL; 5292126258Smlaier } else { 5293126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET)) 5294240233Sglebius dst.sin_addr.s_addr = 5295126258Smlaier s->rt_addr.v4.s_addr; 5296130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5297240233Sglebius PF_STATE_UNLOCK(s); 5298126258Smlaier } 5299126258Smlaier } 5300126258Smlaier if (ifp == NULL) 5301126258Smlaier goto bad; 5302126258Smlaier 5303130639Smlaier if (oifp != ifp) { 5304145836Smlaier if (pf_test(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5305126258Smlaier goto bad; 5306126258Smlaier else if (m0 == NULL) 5307126258Smlaier goto done; 5308145836Smlaier if (m0->m_len < sizeof(struct ip)) { 5309145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5310240233Sglebius ("%s: m0->m_len < sizeof(struct ip)\n", __func__)); 5311145836Smlaier goto bad; 5312145836Smlaier } 5313126258Smlaier ip = mtod(m0, struct ip *); 5314126258Smlaier } 5315126258Smlaier 5316240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5317240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5318240233Sglebius 5319241344Sglebius ip_len = ntohs(ip->ip_len); 5320241344Sglebius ip_off = ntohs(ip->ip_off); 5321240233Sglebius 5322240233Sglebius /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 5323126261Smlaier m0->m_pkthdr.csum_flags |= CSUM_IP; 5324242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 5325126261Smlaier in_delayed_cksum(m0); 5326242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 5327126261Smlaier } 5328240233Sglebius#ifdef SCTP 5329242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5330240233Sglebius sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 5331242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5332240233Sglebius } 5333240233Sglebius#endif 5334126261Smlaier 5335130613Smlaier /* 5336240233Sglebius * If small enough for interface, or the interface will take 5337240233Sglebius * care of the fragmentation for us, we can just send directly. 5338130613Smlaier */ 5339241344Sglebius if (ip_len <= ifp->if_mtu || 5340240233Sglebius (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 5341241344Sglebius ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 5342223637Sbz ip->ip_sum = 0; 5343242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 5344126258Smlaier ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 5345242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_IP; 5346242161Sglebius } 5347254523Sandre m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 5348240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5349126258Smlaier goto done; 5350126258Smlaier } 5351223637Sbz 5352240233Sglebius /* Balk when DF bit is set or the interface didn't support TSO. */ 5353241344Sglebius if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 5354240233Sglebius error = EMSGSIZE; 5355196039Srwatson KMOD_IPSTAT_INC(ips_cantfrag); 5356126258Smlaier if (r->rt != PF_DUPTO) { 5357126258Smlaier icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, 5358145886Smlaier ifp->if_mtu); 5359126258Smlaier goto done; 5360126258Smlaier } else 5361126258Smlaier goto bad; 5362126258Smlaier } 5363126258Smlaier 5364242161Sglebius error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 5365240233Sglebius if (error) 5366126258Smlaier goto bad; 5367126258Smlaier 5368240233Sglebius for (; m0; m0 = m1) { 5369126258Smlaier m1 = m0->m_nextpkt; 5370240233Sglebius m0->m_nextpkt = NULL; 5371126261Smlaier if (error == 0) { 5372254523Sandre m_clrprotoflags(m0); 5373240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5374126261Smlaier } else 5375126258Smlaier m_freem(m0); 5376126258Smlaier } 5377126258Smlaier 5378126258Smlaier if (error == 0) 5379196039Srwatson KMOD_IPSTAT_INC(ips_fragmented); 5380126258Smlaier 5381126258Smlaierdone: 5382126258Smlaier if (r->rt != PF_DUPTO) 5383126258Smlaier *m = NULL; 5384126258Smlaier return; 5385126258Smlaier 5386240233Sglebiusbad_locked: 5387240233Sglebius if (s) 5388240233Sglebius PF_STATE_UNLOCK(s); 5389126258Smlaierbad: 5390126258Smlaier m_freem(m0); 5391126258Smlaier goto done; 5392126258Smlaier} 5393126258Smlaier#endif /* INET */ 5394126258Smlaier 5395126258Smlaier#ifdef INET6 5396240233Sglebiusstatic void 5397126258Smlaierpf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5398171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5399126258Smlaier{ 5400126258Smlaier struct mbuf *m0; 5401240233Sglebius struct sockaddr_in6 dst; 5402126258Smlaier struct ip6_hdr *ip6; 5403126258Smlaier struct ifnet *ifp = NULL; 5404126258Smlaier struct pf_addr naddr; 5405130613Smlaier struct pf_src_node *sn = NULL; 5406126258Smlaier 5407240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5408240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5409240233Sglebius __func__)); 5410126258Smlaier 5411240233Sglebius if ((pd->pf_mtag == NULL && 5412240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5413240233Sglebius pd->pf_mtag->routed++ > 3) { 5414171168Smlaier m0 = *m; 5415171168Smlaier *m = NULL; 5416240233Sglebius goto bad_locked; 5417132303Smlaier } 5418132303Smlaier 5419126258Smlaier if (r->rt == PF_DUPTO) { 5420240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5421240233Sglebius if (s) 5422240233Sglebius PF_STATE_UNLOCK(s); 5423126258Smlaier return; 5424240233Sglebius } 5425126258Smlaier } else { 5426240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5427240233Sglebius if (s) 5428240233Sglebius PF_STATE_UNLOCK(s); 5429126258Smlaier return; 5430240233Sglebius } 5431126258Smlaier m0 = *m; 5432126258Smlaier } 5433126258Smlaier 5434126258Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5435126258Smlaier 5436240233Sglebius bzero(&dst, sizeof(dst)); 5437240233Sglebius dst.sin6_family = AF_INET6; 5438240233Sglebius dst.sin6_len = sizeof(dst); 5439240233Sglebius dst.sin6_addr = ip6->ip6_dst; 5440126258Smlaier 5441171168Smlaier /* Cheat. XXX why only in the v6 case??? */ 5442126258Smlaier if (r->rt == PF_FASTROUTE) { 5443240233Sglebius if (s) 5444240233Sglebius PF_STATE_UNLOCK(s); 5445132280Smlaier m0->m_flags |= M_SKIP_FIREWALL; 5446126261Smlaier ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL); 5447126258Smlaier return; 5448126258Smlaier } 5449126258Smlaier 5450145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5451145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5452240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5453240233Sglebius goto bad_locked; 5454145836Smlaier } 5455126258Smlaier if (s == NULL) { 5456130613Smlaier pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src, 5457130613Smlaier &naddr, NULL, &sn); 5458126258Smlaier if (!PF_AZERO(&naddr, AF_INET6)) 5459240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5460126258Smlaier &naddr, AF_INET6); 5461130613Smlaier ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; 5462126258Smlaier } else { 5463126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET6)) 5464240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5465126258Smlaier &s->rt_addr, AF_INET6); 5466130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5467126258Smlaier } 5468240233Sglebius 5469240233Sglebius if (s) 5470240233Sglebius PF_STATE_UNLOCK(s); 5471240233Sglebius 5472126258Smlaier if (ifp == NULL) 5473126258Smlaier goto bad; 5474126258Smlaier 5475126258Smlaier if (oifp != ifp) { 5476145836Smlaier if (pf_test6(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5477132303Smlaier goto bad; 5478132303Smlaier else if (m0 == NULL) 5479132303Smlaier goto done; 5480145836Smlaier if (m0->m_len < sizeof(struct ip6_hdr)) { 5481145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5482240233Sglebius ("%s: m0->m_len < sizeof(struct ip6_hdr)\n", 5483240233Sglebius __func__)); 5484145836Smlaier goto bad; 5485145836Smlaier } 5486132303Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5487126258Smlaier } 5488126258Smlaier 5489240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5490240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5491240233Sglebius 5492126258Smlaier /* 5493126258Smlaier * If the packet is too large for the outgoing interface, 5494126258Smlaier * send back an icmp6 error. 5495126258Smlaier */ 5496240233Sglebius if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 5497240233Sglebius dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 5498240233Sglebius if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) 5499240233Sglebius nd6_output(ifp, ifp, m0, &dst, NULL); 5500240233Sglebius else { 5501126258Smlaier in6_ifstat_inc(ifp, ifs6_in_toobig); 5502126258Smlaier if (r->rt != PF_DUPTO) 5503126258Smlaier icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); 5504126258Smlaier else 5505126258Smlaier goto bad; 5506126258Smlaier } 5507126258Smlaier 5508126258Smlaierdone: 5509126258Smlaier if (r->rt != PF_DUPTO) 5510126258Smlaier *m = NULL; 5511126258Smlaier return; 5512126258Smlaier 5513240233Sglebiusbad_locked: 5514240233Sglebius if (s) 5515240233Sglebius PF_STATE_UNLOCK(s); 5516126258Smlaierbad: 5517126258Smlaier m_freem(m0); 5518126258Smlaier goto done; 5519126258Smlaier} 5520126258Smlaier#endif /* INET6 */ 5521126258Smlaier 5522126258Smlaier/* 5523132566Smlaier * FreeBSD supports cksum offloads for the following drivers. 5524137413Sru * em(4), fxp(4), ixgb(4), lge(4), ndis(4), nge(4), re(4), 5525132566Smlaier * ti(4), txp(4), xl(4) 5526132566Smlaier * 5527132566Smlaier * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 5528132566Smlaier * network driver performed cksum including pseudo header, need to verify 5529132566Smlaier * csum_data 5530132566Smlaier * CSUM_DATA_VALID : 5531132566Smlaier * network driver performed cksum, needs to additional pseudo header 5532132566Smlaier * cksum computation with partial csum_data(i.e. lack of H/W support for 5533132566Smlaier * pseudo header, for instance hme(4), sk(4) and possibly gem(4)) 5534132566Smlaier * 5535132566Smlaier * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 5536132566Smlaier * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 5537132566Smlaier * TCP/UDP layer. 5538132566Smlaier * Also, set csum_data to 0xffff to force cksum validation. 5539126261Smlaier */ 5540240233Sglebiusstatic int 5541126261Smlaierpf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 5542126261Smlaier{ 5543126261Smlaier u_int16_t sum = 0; 5544126261Smlaier int hw_assist = 0; 5545126261Smlaier struct ip *ip; 5546126261Smlaier 5547126261Smlaier if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 5548126261Smlaier return (1); 5549126261Smlaier if (m->m_pkthdr.len < off + len) 5550126261Smlaier return (1); 5551126261Smlaier 5552126261Smlaier switch (p) { 5553126261Smlaier case IPPROTO_TCP: 5554126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5555126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5556126261Smlaier sum = m->m_pkthdr.csum_data; 5557126261Smlaier } else { 5558223637Sbz ip = mtod(m, struct ip *); 5559126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5560240233Sglebius ip->ip_dst.s_addr, htonl((u_short)len + 5561223637Sbz m->m_pkthdr.csum_data + IPPROTO_TCP)); 5562126261Smlaier } 5563126261Smlaier sum ^= 0xffff; 5564126261Smlaier ++hw_assist; 5565126261Smlaier } 5566126261Smlaier break; 5567126261Smlaier case IPPROTO_UDP: 5568126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5569126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5570126261Smlaier sum = m->m_pkthdr.csum_data; 5571126261Smlaier } else { 5572223637Sbz ip = mtod(m, struct ip *); 5573126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5574223637Sbz ip->ip_dst.s_addr, htonl((u_short)len + 5575223637Sbz m->m_pkthdr.csum_data + IPPROTO_UDP)); 5576126261Smlaier } 5577126261Smlaier sum ^= 0xffff; 5578126261Smlaier ++hw_assist; 5579223637Sbz } 5580126261Smlaier break; 5581126261Smlaier case IPPROTO_ICMP: 5582126261Smlaier#ifdef INET6 5583126261Smlaier case IPPROTO_ICMPV6: 5584126261Smlaier#endif /* INET6 */ 5585126261Smlaier break; 5586126261Smlaier default: 5587126261Smlaier return (1); 5588126261Smlaier } 5589126261Smlaier 5590126261Smlaier if (!hw_assist) { 5591126261Smlaier switch (af) { 5592126261Smlaier case AF_INET: 5593126261Smlaier if (p == IPPROTO_ICMP) { 5594126261Smlaier if (m->m_len < off) 5595126261Smlaier return (1); 5596126261Smlaier m->m_data += off; 5597126261Smlaier m->m_len -= off; 5598126261Smlaier sum = in_cksum(m, len); 5599126261Smlaier m->m_data -= off; 5600126261Smlaier m->m_len += off; 5601126261Smlaier } else { 5602126261Smlaier if (m->m_len < sizeof(struct ip)) 5603126261Smlaier return (1); 5604126261Smlaier sum = in4_cksum(m, p, off, len); 5605126261Smlaier } 5606126261Smlaier break; 5607126261Smlaier#ifdef INET6 5608126261Smlaier case AF_INET6: 5609126261Smlaier if (m->m_len < sizeof(struct ip6_hdr)) 5610126261Smlaier return (1); 5611126261Smlaier sum = in6_cksum(m, p, off, len); 5612126261Smlaier break; 5613126261Smlaier#endif /* INET6 */ 5614126261Smlaier default: 5615126261Smlaier return (1); 5616126261Smlaier } 5617126261Smlaier } 5618126261Smlaier if (sum) { 5619126261Smlaier switch (p) { 5620126261Smlaier case IPPROTO_TCP: 5621183550Szec { 5622196039Srwatson KMOD_TCPSTAT_INC(tcps_rcvbadsum); 5623126261Smlaier break; 5624183550Szec } 5625126261Smlaier case IPPROTO_UDP: 5626183550Szec { 5627196039Srwatson KMOD_UDPSTAT_INC(udps_badsum); 5628126261Smlaier break; 5629183550Szec } 5630222529Sbz#ifdef INET 5631126261Smlaier case IPPROTO_ICMP: 5632183550Szec { 5633196039Srwatson KMOD_ICMPSTAT_INC(icps_checksum); 5634126261Smlaier break; 5635183550Szec } 5636222529Sbz#endif 5637126261Smlaier#ifdef INET6 5638126261Smlaier case IPPROTO_ICMPV6: 5639183550Szec { 5640196039Srwatson KMOD_ICMP6STAT_INC(icp6s_checksum); 5641126261Smlaier break; 5642183550Szec } 5643126261Smlaier#endif /* INET6 */ 5644126261Smlaier } 5645126261Smlaier return (1); 5646132566Smlaier } else { 5647132566Smlaier if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 5648132566Smlaier m->m_pkthdr.csum_flags |= 5649132566Smlaier (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 5650132566Smlaier m->m_pkthdr.csum_data = 0xffff; 5651132566Smlaier } 5652126261Smlaier } 5653126261Smlaier return (0); 5654126261Smlaier} 5655223637Sbz 5656126258Smlaier 5657145836Smlaier#ifdef INET 5658126258Smlaierint 5659240233Sglebiuspf_test(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5660126258Smlaier{ 5661130613Smlaier struct pfi_kif *kif; 5662130613Smlaier u_short action, reason = 0, log = 0; 5663130613Smlaier struct mbuf *m = *m0; 5664223637Sbz struct ip *h = NULL; 5665223637Sbz struct m_tag *ipfwtag; 5666223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5667130613Smlaier struct pf_state *s = NULL; 5668130613Smlaier struct pf_ruleset *ruleset = NULL; 5669130613Smlaier struct pf_pdesc pd; 5670130613Smlaier int off, dirndx, pqid = 0; 5671126258Smlaier 5672240233Sglebius M_ASSERTPKTHDR(m); 5673240233Sglebius 5674223637Sbz if (!V_pf_status.running) 5675171168Smlaier return (PF_PASS); 5676126258Smlaier 5677171168Smlaier memset(&pd, 0, sizeof(pd)); 5678145836Smlaier 5679240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 5680240233Sglebius 5681130613Smlaier if (kif == NULL) { 5682145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5683145836Smlaier ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname)); 5684130613Smlaier return (PF_DROP); 5685130613Smlaier } 5686223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 5687145836Smlaier return (PF_PASS); 5688130613Smlaier 5689240233Sglebius if (m->m_flags & M_SKIP_FIREWALL) 5690240233Sglebius return (PF_PASS); 5691126258Smlaier 5692240233Sglebius pd.pf_mtag = pf_find_mtag(m); 5693240233Sglebius 5694240233Sglebius PF_RULES_RLOCK(); 5695240233Sglebius 5696223637Sbz if (ip_divert_ptr != NULL && 5697223637Sbz ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) { 5698223637Sbz struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1); 5699223637Sbz if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) { 5700240233Sglebius if (pd.pf_mtag == NULL && 5701240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5702240233Sglebius action = PF_DROP; 5703240233Sglebius goto done; 5704240233Sglebius } 5705223637Sbz pd.pf_mtag->flags |= PF_PACKET_LOOPED; 5706223637Sbz m_tag_delete(m, ipfwtag); 5707223637Sbz } 5708240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) { 5709223637Sbz m->m_flags |= M_FASTFWD_OURS; 5710223637Sbz pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT; 5711223637Sbz } 5712240233Sglebius } else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) { 5713240233Sglebius /* We do IP header normalization and packet reassembly here */ 5714126258Smlaier action = PF_DROP; 5715126258Smlaier goto done; 5716126258Smlaier } 5717223637Sbz m = *m0; /* pf_normalize messes with m0 */ 5718126258Smlaier h = mtod(m, struct ip *); 5719126258Smlaier 5720126258Smlaier off = h->ip_hl << 2; 5721240233Sglebius if (off < (int)sizeof(struct ip)) { 5722126258Smlaier action = PF_DROP; 5723126258Smlaier REASON_SET(&reason, PFRES_SHORT); 5724126258Smlaier log = 1; 5725126258Smlaier goto done; 5726126258Smlaier } 5727126258Smlaier 5728126258Smlaier pd.src = (struct pf_addr *)&h->ip_src; 5729126258Smlaier pd.dst = (struct pf_addr *)&h->ip_dst; 5730223637Sbz pd.sport = pd.dport = NULL; 5731126258Smlaier pd.ip_sum = &h->ip_sum; 5732223637Sbz pd.proto_sum = NULL; 5733126258Smlaier pd.proto = h->ip_p; 5734223637Sbz pd.dir = dir; 5735223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 5736223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 5737126258Smlaier pd.af = AF_INET; 5738126258Smlaier pd.tos = h->ip_tos; 5739126258Smlaier pd.tot_len = ntohs(h->ip_len); 5740126258Smlaier 5741126258Smlaier /* handle fragments that didn't get reassembled by normalization */ 5742126258Smlaier if (h->ip_off & htons(IP_MF | IP_OFFMASK)) { 5743130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 5744126258Smlaier &pd, &a, &ruleset); 5745126258Smlaier goto done; 5746126258Smlaier } 5747126258Smlaier 5748126258Smlaier switch (h->ip_p) { 5749126258Smlaier 5750126258Smlaier case IPPROTO_TCP: { 5751126258Smlaier struct tcphdr th; 5752126258Smlaier 5753126258Smlaier pd.hdr.tcp = &th; 5754126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 5755126258Smlaier &action, &reason, AF_INET)) { 5756126258Smlaier log = action != PF_PASS; 5757126258Smlaier goto done; 5758126258Smlaier } 5759126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 5760126258Smlaier if ((th.th_flags & TH_ACK) && pd.p_len == 0) 5761126258Smlaier pqid = 1; 5762130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 5763126258Smlaier if (action == PF_DROP) 5764130613Smlaier goto done; 5765130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 5766126258Smlaier &reason); 5767126258Smlaier if (action == PF_PASS) { 5768223637Sbz if (pfsync_update_state_ptr != NULL) 5769223637Sbz pfsync_update_state_ptr(s); 5770126258Smlaier r = s->rule.ptr; 5771130613Smlaier a = s->anchor.ptr; 5772126258Smlaier log = s->log; 5773126258Smlaier } else if (s == NULL) 5774240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5775240233Sglebius &a, &ruleset, inp); 5776126258Smlaier break; 5777126258Smlaier } 5778126258Smlaier 5779126258Smlaier case IPPROTO_UDP: { 5780126258Smlaier struct udphdr uh; 5781126258Smlaier 5782126258Smlaier pd.hdr.udp = &uh; 5783126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 5784126258Smlaier &action, &reason, AF_INET)) { 5785126258Smlaier log = action != PF_PASS; 5786126258Smlaier goto done; 5787126258Smlaier } 5788130613Smlaier if (uh.uh_dport == 0 || 5789130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 5790130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 5791130613Smlaier action = PF_DROP; 5792171168Smlaier REASON_SET(&reason, PFRES_SHORT); 5793130613Smlaier goto done; 5794130613Smlaier } 5795130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 5796126258Smlaier if (action == PF_PASS) { 5797223637Sbz if (pfsync_update_state_ptr != NULL) 5798223637Sbz pfsync_update_state_ptr(s); 5799126258Smlaier r = s->rule.ptr; 5800126258Smlaier a = s->anchor.ptr; 5801126258Smlaier log = s->log; 5802126258Smlaier } else if (s == NULL) 5803240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5804240233Sglebius &a, &ruleset, inp); 5805126258Smlaier break; 5806126258Smlaier } 5807126258Smlaier 5808126258Smlaier case IPPROTO_ICMP: { 5809126258Smlaier struct icmp ih; 5810126258Smlaier 5811126258Smlaier pd.hdr.icmp = &ih; 5812126258Smlaier if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN, 5813126258Smlaier &action, &reason, AF_INET)) { 5814126258Smlaier log = action != PF_PASS; 5815126258Smlaier goto done; 5816126258Smlaier } 5817145836Smlaier action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd, 5818145836Smlaier &reason); 5819126258Smlaier if (action == PF_PASS) { 5820223637Sbz if (pfsync_update_state_ptr != NULL) 5821223637Sbz pfsync_update_state_ptr(s); 5822126258Smlaier r = s->rule.ptr; 5823126258Smlaier a = s->anchor.ptr; 5824126258Smlaier log = s->log; 5825126258Smlaier } else if (s == NULL) 5826240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5827240233Sglebius &a, &ruleset, inp); 5828126258Smlaier break; 5829126258Smlaier } 5830126258Smlaier 5831223637Sbz#ifdef INET6 5832223637Sbz case IPPROTO_ICMPV6: { 5833223637Sbz action = PF_DROP; 5834223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5835223637Sbz ("pf: dropping IPv4 packet with ICMPv6 payload\n")); 5836223637Sbz goto done; 5837223637Sbz } 5838223637Sbz#endif 5839223637Sbz 5840126258Smlaier default: 5841223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 5842126258Smlaier if (action == PF_PASS) { 5843223637Sbz if (pfsync_update_state_ptr != NULL) 5844223637Sbz pfsync_update_state_ptr(s); 5845126258Smlaier r = s->rule.ptr; 5846126258Smlaier a = s->anchor.ptr; 5847126258Smlaier log = s->log; 5848126258Smlaier } else if (s == NULL) 5849240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5850240233Sglebius &a, &ruleset, inp); 5851126258Smlaier break; 5852126258Smlaier } 5853126258Smlaier 5854126258Smlaierdone: 5855240233Sglebius PF_RULES_RUNLOCK(); 5856126258Smlaier if (action == PF_PASS && h->ip_hl > 5 && 5857200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 5858126258Smlaier action = PF_DROP; 5859145836Smlaier REASON_SET(&reason, PFRES_IPOPTIONS); 5860126258Smlaier log = 1; 5861126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 5862126258Smlaier ("pf: dropping packet with ip options\n")); 5863126258Smlaier } 5864126258Smlaier 5865240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 5866240233Sglebius action = PF_DROP; 5867240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5868240233Sglebius } 5869240233Sglebius if (r->rtableid >= 0) 5870240233Sglebius M_SETFIB(m, r->rtableid); 5871145836Smlaier 5872126258Smlaier#ifdef ALTQ 5873126258Smlaier if (action == PF_PASS && r->qid) { 5874240233Sglebius if (pd.pf_mtag == NULL && 5875240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5876240233Sglebius action = PF_DROP; 5877240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5878240233Sglebius } 5879171168Smlaier if (pqid || (pd.tos & IPTOS_LOWDELAY)) 5880171168Smlaier pd.pf_mtag->qid = r->pqid; 5881171168Smlaier else 5882171168Smlaier pd.pf_mtag->qid = r->qid; 5883171168Smlaier /* add hints for ecn */ 5884171168Smlaier pd.pf_mtag->hdr = h; 5885223637Sbz 5886126258Smlaier } 5887145836Smlaier#endif /* ALTQ */ 5888126258Smlaier 5889130613Smlaier /* 5890130613Smlaier * connections redirected to loopback should not match sockets 5891130613Smlaier * bound specifically to loopback due to security implications, 5892130613Smlaier * see tcp_input() and in_pcblookup_listen(). 5893130613Smlaier */ 5894130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 5895130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 5896130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 5897130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 5898171168Smlaier (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 5899223637Sbz m->m_flags |= M_SKIP_FIREWALL; 5900171168Smlaier 5901240233Sglebius if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL && 5902240233Sglebius !PACKET_LOOPED(&pd)) { 5903223637Sbz 5904223637Sbz ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 5905240233Sglebius sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 5906223637Sbz if (ipfwtag != NULL) { 5907225171Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->info = 5908225171Sbz ntohs(r->divert.port); 5909223637Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir; 5910223637Sbz 5911240233Sglebius if (s) 5912240233Sglebius PF_STATE_UNLOCK(s); 5913240233Sglebius 5914223637Sbz m_tag_prepend(m, ipfwtag); 5915223637Sbz if (m->m_flags & M_FASTFWD_OURS) { 5916240233Sglebius if (pd.pf_mtag == NULL && 5917240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5918240233Sglebius action = PF_DROP; 5919240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5920240233Sglebius log = 1; 5921240233Sglebius DPFPRINTF(PF_DEBUG_MISC, 5922240233Sglebius ("pf: failed to allocate tag\n")); 5923240233Sglebius } 5924223637Sbz pd.pf_mtag->flags |= PF_FASTFWD_OURS_PRESENT; 5925223637Sbz m->m_flags &= ~M_FASTFWD_OURS; 5926223637Sbz } 5927240233Sglebius ip_divert_ptr(*m0, dir == PF_IN ? DIR_IN : DIR_OUT); 5928240233Sglebius *m0 = NULL; 5929223637Sbz 5930223637Sbz return (action); 5931223637Sbz } else { 5932223637Sbz /* XXX: ipfw has the same behaviour! */ 5933223637Sbz action = PF_DROP; 5934223637Sbz REASON_SET(&reason, PFRES_MEMORY); 5935223637Sbz log = 1; 5936223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5937223637Sbz ("pf: failed to allocate divert tag\n")); 5938223637Sbz } 5939223637Sbz } 5940223637Sbz 5941171168Smlaier if (log) { 5942171168Smlaier struct pf_rule *lr; 5943171168Smlaier 5944171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 5945171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 5946171168Smlaier lr = s->nat_rule.ptr; 5947171168Smlaier else 5948171168Smlaier lr = r; 5949240233Sglebius PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd, 5950240233Sglebius (s == NULL)); 5951130613Smlaier } 5952130613Smlaier 5953130613Smlaier kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 5954130613Smlaier kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++; 5955130613Smlaier 5956130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 5957171168Smlaier dirndx = (dir == PF_OUT); 5958171168Smlaier r->packets[dirndx]++; 5959171168Smlaier r->bytes[dirndx] += pd.tot_len; 5960130613Smlaier if (a != NULL) { 5961171168Smlaier a->packets[dirndx]++; 5962171168Smlaier a->bytes[dirndx] += pd.tot_len; 5963130613Smlaier } 5964130613Smlaier if (s != NULL) { 5965130613Smlaier if (s->nat_rule.ptr != NULL) { 5966171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 5967171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 5968130613Smlaier } 5969130613Smlaier if (s->src_node != NULL) { 5970171168Smlaier s->src_node->packets[dirndx]++; 5971171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 5972130613Smlaier } 5973130613Smlaier if (s->nat_src_node != NULL) { 5974171168Smlaier s->nat_src_node->packets[dirndx]++; 5975171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 5976130613Smlaier } 5977171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 5978171168Smlaier s->packets[dirndx]++; 5979171168Smlaier s->bytes[dirndx] += pd.tot_len; 5980130613Smlaier } 5981130613Smlaier tr = r; 5982130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 5983223637Sbz if (nr != NULL && r == &V_pf_default_rule) 5984223637Sbz tr = nr; 5985130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 5986223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 5987223637Sbz (s == NULL) ? pd.src : 5988223637Sbz &s->key[(s->direction == PF_IN)]-> 5989223637Sbz addr[(s->direction == PF_OUT)], 5990223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 5991223637Sbz r->action == PF_PASS, tr->src.neg); 5992130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 5993223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 5994223637Sbz (s == NULL) ? pd.dst : 5995223637Sbz &s->key[(s->direction == PF_IN)]-> 5996223637Sbz addr[(s->direction == PF_IN)], 5997223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 5998223637Sbz r->action == PF_PASS, tr->dst.neg); 5999130613Smlaier } 6000130613Smlaier 6001223637Sbz switch (action) { 6002223637Sbz case PF_SYNPROXY_DROP: 6003126258Smlaier m_freem(*m0); 6004223637Sbz case PF_DEFER: 6005126258Smlaier *m0 = NULL; 6006126258Smlaier action = PF_PASS; 6007223637Sbz break; 6008223637Sbz default: 6009240233Sglebius /* pf_route() returns unlocked. */ 6010240233Sglebius if (r->rt) { 6011223637Sbz pf_route(m0, r, dir, kif->pfik_ifp, s, &pd); 6012240233Sglebius return (action); 6013240233Sglebius } 6014223637Sbz break; 6015223637Sbz } 6016240233Sglebius if (s) 6017240233Sglebius PF_STATE_UNLOCK(s); 6018240233Sglebius 6019126258Smlaier return (action); 6020126258Smlaier} 6021126258Smlaier#endif /* INET */ 6022126258Smlaier 6023126258Smlaier#ifdef INET6 6024126258Smlaierint 6025240233Sglebiuspf_test6(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 6026126258Smlaier{ 6027130613Smlaier struct pfi_kif *kif; 6028130613Smlaier u_short action, reason = 0, log = 0; 6029171168Smlaier struct mbuf *m = *m0, *n = NULL; 6030223637Sbz struct ip6_hdr *h = NULL; 6031223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 6032130613Smlaier struct pf_state *s = NULL; 6033130613Smlaier struct pf_ruleset *ruleset = NULL; 6034130613Smlaier struct pf_pdesc pd; 6035169843Sdhartmei int off, terminal = 0, dirndx, rh_cnt = 0; 6036126258Smlaier 6037240233Sglebius M_ASSERTPKTHDR(m); 6038240233Sglebius 6039240233Sglebius if (!V_pf_status.running) 6040126258Smlaier return (PF_PASS); 6041126258Smlaier 6042171168Smlaier memset(&pd, 0, sizeof(pd)); 6043240233Sglebius pd.pf_mtag = pf_find_mtag(m); 6044145836Smlaier 6045240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED) 6046240233Sglebius return (PF_PASS); 6047240233Sglebius 6048240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 6049130613Smlaier if (kif == NULL) { 6050145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 6051145836Smlaier ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname)); 6052130613Smlaier return (PF_DROP); 6053130613Smlaier } 6054223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 6055145836Smlaier return (PF_PASS); 6056130613Smlaier 6057270575Sglebius if (m->m_flags & M_SKIP_FIREWALL) 6058270575Sglebius return (PF_PASS); 6059270575Sglebius 6060240233Sglebius PF_RULES_RLOCK(); 6061223637Sbz 6062126258Smlaier /* We do IP header normalization and packet reassembly here */ 6063145836Smlaier if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) { 6064126258Smlaier action = PF_DROP; 6065126258Smlaier goto done; 6066126258Smlaier } 6067223637Sbz m = *m0; /* pf_normalize messes with m0 */ 6068126258Smlaier h = mtod(m, struct ip6_hdr *); 6069126258Smlaier 6070169843Sdhartmei#if 1 6071169843Sdhartmei /* 6072169843Sdhartmei * we do not support jumbogram yet. if we keep going, zero ip6_plen 6073169843Sdhartmei * will do something bad, so drop the packet for now. 6074169843Sdhartmei */ 6075169843Sdhartmei if (htons(h->ip6_plen) == 0) { 6076169843Sdhartmei action = PF_DROP; 6077169843Sdhartmei REASON_SET(&reason, PFRES_NORM); /*XXX*/ 6078169843Sdhartmei goto done; 6079169843Sdhartmei } 6080169843Sdhartmei#endif 6081169843Sdhartmei 6082126258Smlaier pd.src = (struct pf_addr *)&h->ip6_src; 6083126258Smlaier pd.dst = (struct pf_addr *)&h->ip6_dst; 6084223637Sbz pd.sport = pd.dport = NULL; 6085126258Smlaier pd.ip_sum = NULL; 6086223637Sbz pd.proto_sum = NULL; 6087223637Sbz pd.dir = dir; 6088223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 6089223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 6090126258Smlaier pd.af = AF_INET6; 6091126258Smlaier pd.tos = 0; 6092126258Smlaier pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 6093126258Smlaier 6094126258Smlaier off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr); 6095126258Smlaier pd.proto = h->ip6_nxt; 6096126258Smlaier do { 6097126258Smlaier switch (pd.proto) { 6098126258Smlaier case IPPROTO_FRAGMENT: 6099130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 6100126258Smlaier &pd, &a, &ruleset); 6101126258Smlaier if (action == PF_DROP) 6102126258Smlaier REASON_SET(&reason, PFRES_FRAG); 6103126258Smlaier goto done; 6104169843Sdhartmei case IPPROTO_ROUTING: { 6105169843Sdhartmei struct ip6_rthdr rthdr; 6106169843Sdhartmei 6107169843Sdhartmei if (rh_cnt++) { 6108169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6109169843Sdhartmei ("pf: IPv6 more than one rthdr\n")); 6110169843Sdhartmei action = PF_DROP; 6111169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6112169843Sdhartmei log = 1; 6113169843Sdhartmei goto done; 6114169843Sdhartmei } 6115169843Sdhartmei if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL, 6116169843Sdhartmei &reason, pd.af)) { 6117169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6118169843Sdhartmei ("pf: IPv6 short rthdr\n")); 6119169843Sdhartmei action = PF_DROP; 6120169843Sdhartmei REASON_SET(&reason, PFRES_SHORT); 6121169843Sdhartmei log = 1; 6122169843Sdhartmei goto done; 6123169843Sdhartmei } 6124169843Sdhartmei if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 6125169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6126169843Sdhartmei ("pf: IPv6 rthdr0\n")); 6127169843Sdhartmei action = PF_DROP; 6128169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6129169843Sdhartmei log = 1; 6130169843Sdhartmei goto done; 6131169843Sdhartmei } 6132223637Sbz /* FALLTHROUGH */ 6133169843Sdhartmei } 6134126258Smlaier case IPPROTO_AH: 6135126258Smlaier case IPPROTO_HOPOPTS: 6136126258Smlaier case IPPROTO_DSTOPTS: { 6137126258Smlaier /* get next header and header length */ 6138126258Smlaier struct ip6_ext opt6; 6139126258Smlaier 6140126258Smlaier if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6), 6141145836Smlaier NULL, &reason, pd.af)) { 6142126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 6143126258Smlaier ("pf: IPv6 short opt\n")); 6144126258Smlaier action = PF_DROP; 6145126258Smlaier log = 1; 6146126258Smlaier goto done; 6147126258Smlaier } 6148126258Smlaier if (pd.proto == IPPROTO_AH) 6149126258Smlaier off += (opt6.ip6e_len + 2) * 4; 6150126258Smlaier else 6151126258Smlaier off += (opt6.ip6e_len + 1) * 8; 6152126258Smlaier pd.proto = opt6.ip6e_nxt; 6153126258Smlaier /* goto the next header */ 6154126258Smlaier break; 6155126258Smlaier } 6156126258Smlaier default: 6157126258Smlaier terminal++; 6158126258Smlaier break; 6159126258Smlaier } 6160126258Smlaier } while (!terminal); 6161126258Smlaier 6162171168Smlaier /* if there's no routing header, use unmodified mbuf for checksumming */ 6163171168Smlaier if (!n) 6164171168Smlaier n = m; 6165171168Smlaier 6166126258Smlaier switch (pd.proto) { 6167126258Smlaier 6168126258Smlaier case IPPROTO_TCP: { 6169126258Smlaier struct tcphdr th; 6170126258Smlaier 6171126258Smlaier pd.hdr.tcp = &th; 6172126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 6173126258Smlaier &action, &reason, AF_INET6)) { 6174126258Smlaier log = action != PF_PASS; 6175126258Smlaier goto done; 6176126258Smlaier } 6177126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 6178130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6179126258Smlaier if (action == PF_DROP) 6180130613Smlaier goto done; 6181130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6182126258Smlaier &reason); 6183126258Smlaier if (action == PF_PASS) { 6184223637Sbz if (pfsync_update_state_ptr != NULL) 6185223637Sbz pfsync_update_state_ptr(s); 6186126258Smlaier r = s->rule.ptr; 6187130613Smlaier a = s->anchor.ptr; 6188126258Smlaier log = s->log; 6189126258Smlaier } else if (s == NULL) 6190240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6191240233Sglebius &a, &ruleset, inp); 6192126258Smlaier break; 6193126258Smlaier } 6194126258Smlaier 6195126258Smlaier case IPPROTO_UDP: { 6196126258Smlaier struct udphdr uh; 6197126258Smlaier 6198126258Smlaier pd.hdr.udp = &uh; 6199126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 6200126258Smlaier &action, &reason, AF_INET6)) { 6201126258Smlaier log = action != PF_PASS; 6202126258Smlaier goto done; 6203126258Smlaier } 6204130613Smlaier if (uh.uh_dport == 0 || 6205130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 6206130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 6207130613Smlaier action = PF_DROP; 6208171168Smlaier REASON_SET(&reason, PFRES_SHORT); 6209130613Smlaier goto done; 6210130613Smlaier } 6211130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6212126258Smlaier if (action == PF_PASS) { 6213223637Sbz if (pfsync_update_state_ptr != NULL) 6214223637Sbz pfsync_update_state_ptr(s); 6215126258Smlaier r = s->rule.ptr; 6216130613Smlaier a = s->anchor.ptr; 6217126258Smlaier log = s->log; 6218126258Smlaier } else if (s == NULL) 6219240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6220240233Sglebius &a, &ruleset, inp); 6221126258Smlaier break; 6222126258Smlaier } 6223126258Smlaier 6224223637Sbz case IPPROTO_ICMP: { 6225223637Sbz action = PF_DROP; 6226223637Sbz DPFPRINTF(PF_DEBUG_MISC, 6227223637Sbz ("pf: dropping IPv6 packet with ICMPv4 payload\n")); 6228223637Sbz goto done; 6229223637Sbz } 6230223637Sbz 6231126258Smlaier case IPPROTO_ICMPV6: { 6232126258Smlaier struct icmp6_hdr ih; 6233126258Smlaier 6234126258Smlaier pd.hdr.icmp6 = &ih; 6235126258Smlaier if (!pf_pull_hdr(m, off, &ih, sizeof(ih), 6236126258Smlaier &action, &reason, AF_INET6)) { 6237126258Smlaier log = action != PF_PASS; 6238126258Smlaier goto done; 6239126258Smlaier } 6240130613Smlaier action = pf_test_state_icmp(&s, dir, kif, 6241145836Smlaier m, off, h, &pd, &reason); 6242126258Smlaier if (action == PF_PASS) { 6243223637Sbz if (pfsync_update_state_ptr != NULL) 6244223637Sbz pfsync_update_state_ptr(s); 6245126258Smlaier r = s->rule.ptr; 6246130613Smlaier a = s->anchor.ptr; 6247126258Smlaier log = s->log; 6248126258Smlaier } else if (s == NULL) 6249240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6250240233Sglebius &a, &ruleset, inp); 6251126258Smlaier break; 6252126258Smlaier } 6253126258Smlaier 6254126258Smlaier default: 6255223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 6256130613Smlaier if (action == PF_PASS) { 6257223637Sbz if (pfsync_update_state_ptr != NULL) 6258223637Sbz pfsync_update_state_ptr(s); 6259130613Smlaier r = s->rule.ptr; 6260130613Smlaier a = s->anchor.ptr; 6261130613Smlaier log = s->log; 6262130613Smlaier } else if (s == NULL) 6263240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6264240233Sglebius &a, &ruleset, inp); 6265126258Smlaier break; 6266126258Smlaier } 6267126258Smlaier 6268126258Smlaierdone: 6269240233Sglebius PF_RULES_RUNLOCK(); 6270223637Sbz if (n != m) { 6271223637Sbz m_freem(n); 6272223637Sbz n = NULL; 6273223637Sbz } 6274223637Sbz 6275169843Sdhartmei /* handle dangerous IPv6 extension headers. */ 6276169843Sdhartmei if (action == PF_PASS && rh_cnt && 6277200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6278169843Sdhartmei action = PF_DROP; 6279169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6280169843Sdhartmei log = 1; 6281169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6282169843Sdhartmei ("pf: dropping packet with dangerous v6 headers\n")); 6283169843Sdhartmei } 6284126258Smlaier 6285240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6286240233Sglebius action = PF_DROP; 6287240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6288240233Sglebius } 6289240233Sglebius if (r->rtableid >= 0) 6290240233Sglebius M_SETFIB(m, r->rtableid); 6291145836Smlaier 6292126258Smlaier#ifdef ALTQ 6293126258Smlaier if (action == PF_PASS && r->qid) { 6294240233Sglebius if (pd.pf_mtag == NULL && 6295240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6296240233Sglebius action = PF_DROP; 6297240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6298240233Sglebius } 6299171168Smlaier if (pd.tos & IPTOS_LOWDELAY) 6300171168Smlaier pd.pf_mtag->qid = r->pqid; 6301171168Smlaier else 6302171168Smlaier pd.pf_mtag->qid = r->qid; 6303171168Smlaier /* add hints for ecn */ 6304171168Smlaier pd.pf_mtag->hdr = h; 6305126258Smlaier } 6306145836Smlaier#endif /* ALTQ */ 6307126258Smlaier 6308130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6309130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6310130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 6311130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 6312171168Smlaier IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) 6313223637Sbz m->m_flags |= M_SKIP_FIREWALL; 6314171168Smlaier 6315223637Sbz /* XXX: Anybody working on it?! */ 6316223637Sbz if (r->divert.port) 6317223637Sbz printf("pf: divert(9) is not supported for IPv6\n"); 6318223637Sbz 6319171168Smlaier if (log) { 6320171168Smlaier struct pf_rule *lr; 6321171168Smlaier 6322171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 6323171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 6324171168Smlaier lr = s->nat_rule.ptr; 6325171168Smlaier else 6326171168Smlaier lr = r; 6327240233Sglebius PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset, 6328240233Sglebius &pd, (s == NULL)); 6329130613Smlaier } 6330130613Smlaier 6331130613Smlaier kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 6332130613Smlaier kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++; 6333130613Smlaier 6334130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 6335171168Smlaier dirndx = (dir == PF_OUT); 6336171168Smlaier r->packets[dirndx]++; 6337171168Smlaier r->bytes[dirndx] += pd.tot_len; 6338130613Smlaier if (a != NULL) { 6339171168Smlaier a->packets[dirndx]++; 6340171168Smlaier a->bytes[dirndx] += pd.tot_len; 6341130613Smlaier } 6342130613Smlaier if (s != NULL) { 6343130613Smlaier if (s->nat_rule.ptr != NULL) { 6344171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 6345171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 6346130613Smlaier } 6347130613Smlaier if (s->src_node != NULL) { 6348171168Smlaier s->src_node->packets[dirndx]++; 6349171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 6350130613Smlaier } 6351130613Smlaier if (s->nat_src_node != NULL) { 6352171168Smlaier s->nat_src_node->packets[dirndx]++; 6353171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 6354130613Smlaier } 6355171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 6356171168Smlaier s->packets[dirndx]++; 6357171168Smlaier s->bytes[dirndx] += pd.tot_len; 6358130613Smlaier } 6359130613Smlaier tr = r; 6360130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6361223637Sbz if (nr != NULL && r == &V_pf_default_rule) 6362223637Sbz tr = nr; 6363130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 6364223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 6365223637Sbz (s == NULL) ? pd.src : 6366223637Sbz &s->key[(s->direction == PF_IN)]->addr[0], 6367223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6368223637Sbz r->action == PF_PASS, tr->src.neg); 6369130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6370223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6371223637Sbz (s == NULL) ? pd.dst : 6372223637Sbz &s->key[(s->direction == PF_IN)]->addr[1], 6373223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6374223637Sbz r->action == PF_PASS, tr->dst.neg); 6375130613Smlaier } 6376130613Smlaier 6377223637Sbz switch (action) { 6378223637Sbz case PF_SYNPROXY_DROP: 6379126258Smlaier m_freem(*m0); 6380223637Sbz case PF_DEFER: 6381126258Smlaier *m0 = NULL; 6382126258Smlaier action = PF_PASS; 6383223637Sbz break; 6384223637Sbz default: 6385240233Sglebius /* pf_route6() returns unlocked. */ 6386240233Sglebius if (r->rt) { 6387223637Sbz pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd); 6388240233Sglebius return (action); 6389240233Sglebius } 6390223637Sbz break; 6391223637Sbz } 6392126258Smlaier 6393240233Sglebius if (s) 6394240233Sglebius PF_STATE_UNLOCK(s); 6395240233Sglebius 6396126258Smlaier return (action); 6397126258Smlaier} 6398126258Smlaier#endif /* INET6 */ 6399