1244769Sglebius/*- 2126258Smlaier * Copyright (c) 2001 Daniel Hartmeier 3223637Sbz * Copyright (c) 2002 - 2008 Henning Brauer 4244769Sglebius * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 5126258Smlaier * All rights reserved. 6126258Smlaier * 7126258Smlaier * Redistribution and use in source and binary forms, with or without 8126258Smlaier * modification, are permitted provided that the following conditions 9126258Smlaier * are met: 10126258Smlaier * 11126258Smlaier * - Redistributions of source code must retain the above copyright 12126258Smlaier * notice, this list of conditions and the following disclaimer. 13126258Smlaier * - Redistributions in binary form must reproduce the above 14126258Smlaier * copyright notice, this list of conditions and the following 15126258Smlaier * disclaimer in the documentation and/or other materials provided 16126258Smlaier * with the distribution. 17126258Smlaier * 18126258Smlaier * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19126258Smlaier * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20126258Smlaier * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21126258Smlaier * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22126258Smlaier * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23126258Smlaier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24126258Smlaier * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25126258Smlaier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26126258Smlaier * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27126258Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28126258Smlaier * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29126258Smlaier * POSSIBILITY OF SUCH DAMAGE. 30126258Smlaier * 31126258Smlaier * Effort sponsored in part by the Defense Advanced Research Projects 32126258Smlaier * Agency (DARPA) and Air Force Research Laboratory, Air Force 33126258Smlaier * Materiel Command, USAF, under agreement number F30602-01-2-0537. 34126258Smlaier * 35244769Sglebius * $OpenBSD: pf.c,v 1.634 2009/02/27 12:37:45 henning Exp $ 36126258Smlaier */ 37126258Smlaier 38240233Sglebius#include <sys/cdefs.h> 39171168Smlaier__FBSDID("$FreeBSD$"); 40126261Smlaier 41240233Sglebius#include "opt_inet.h" 42240233Sglebius#include "opt_inet6.h" 43126261Smlaier#include "opt_bpf.h" 44126261Smlaier#include "opt_pf.h" 45153110Sru 46126258Smlaier#include <sys/param.h> 47240233Sglebius#include <sys/bus.h> 48240233Sglebius#include <sys/endian.h> 49240233Sglebius#include <sys/hash.h> 50240233Sglebius#include <sys/interrupt.h> 51240233Sglebius#include <sys/kernel.h> 52240233Sglebius#include <sys/kthread.h> 53240233Sglebius#include <sys/limits.h> 54126258Smlaier#include <sys/mbuf.h> 55240233Sglebius#include <sys/md5.h> 56240233Sglebius#include <sys/random.h> 57240233Sglebius#include <sys/refcount.h> 58126258Smlaier#include <sys/socket.h> 59126261Smlaier#include <sys/sysctl.h> 60240233Sglebius#include <sys/taskqueue.h> 61240233Sglebius#include <sys/ucred.h> 62126258Smlaier 63126258Smlaier#include <net/if.h> 64126258Smlaier#include <net/if_types.h> 65126258Smlaier#include <net/route.h> 66171168Smlaier#include <net/radix_mpath.h> 67240233Sglebius#include <net/vnet.h> 68126258Smlaier 69240233Sglebius#include <net/pfvar.h> 70240233Sglebius#include <net/pf_mtag.h> 71240233Sglebius#include <net/if_pflog.h> 72240233Sglebius#include <net/if_pfsync.h> 73240233Sglebius 74240233Sglebius#include <netinet/in_pcb.h> 75126258Smlaier#include <netinet/in_var.h> 76126258Smlaier#include <netinet/ip.h> 77240233Sglebius#include <netinet/ip_fw.h> 78240233Sglebius#include <netinet/ip_icmp.h> 79240233Sglebius#include <netinet/icmp_var.h> 80126258Smlaier#include <netinet/ip_var.h> 81126258Smlaier#include <netinet/tcp.h> 82240233Sglebius#include <netinet/tcp_fsm.h> 83126258Smlaier#include <netinet/tcp_seq.h> 84126258Smlaier#include <netinet/tcp_timer.h> 85126258Smlaier#include <netinet/tcp_var.h> 86240233Sglebius#include <netinet/udp.h> 87126258Smlaier#include <netinet/udp_var.h> 88126258Smlaier 89240494Sglebius#include <netpfil/ipfw/ip_fw_private.h> /* XXX: only for DIR_IN/DIR_OUT */ 90240494Sglebius 91126258Smlaier#ifdef INET6 92126258Smlaier#include <netinet/ip6.h> 93126258Smlaier#include <netinet/icmp6.h> 94126258Smlaier#include <netinet6/nd6.h> 95126261Smlaier#include <netinet6/ip6_var.h> 96126261Smlaier#include <netinet6/in6_pcb.h> 97126258Smlaier#endif /* INET6 */ 98126258Smlaier 99126261Smlaier#include <machine/in_cksum.h> 100163606Srwatson#include <security/mac/mac_framework.h> 101126258Smlaier 102223637Sbz#define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x 103126258Smlaier 104126258Smlaier/* 105126258Smlaier * Global variables 106126258Smlaier */ 107126258Smlaier 108223637Sbz/* state tables */ 109223637SbzVNET_DEFINE(struct pf_altqqueue, pf_altqs[2]); 110223637SbzVNET_DEFINE(struct pf_palist, pf_pabuf); 111223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_active); 112223637SbzVNET_DEFINE(struct pf_altqqueue *, pf_altqs_inactive); 113223637SbzVNET_DEFINE(struct pf_status, pf_status); 114223637Sbz 115223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_active); 116223637SbzVNET_DEFINE(u_int32_t, ticket_altqs_inactive); 117223637SbzVNET_DEFINE(int, altqs_inactive_open); 118223637SbzVNET_DEFINE(u_int32_t, ticket_pabuf); 119223637Sbz 120223637SbzVNET_DEFINE(MD5_CTX, pf_tcp_secret_ctx); 121223637Sbz#define V_pf_tcp_secret_ctx VNET(pf_tcp_secret_ctx) 122223637SbzVNET_DEFINE(u_char, pf_tcp_secret[16]); 123223637Sbz#define V_pf_tcp_secret VNET(pf_tcp_secret) 124223637SbzVNET_DEFINE(int, pf_tcp_secret_init); 125223637Sbz#define V_pf_tcp_secret_init VNET(pf_tcp_secret_init) 126223637SbzVNET_DEFINE(int, pf_tcp_iss_off); 127223637Sbz#define V_pf_tcp_iss_off VNET(pf_tcp_iss_off) 128223637Sbz 129240233Sglebius/* 130240233Sglebius * Queue for pf_intr() sends. 131240233Sglebius */ 132240233Sglebiusstatic MALLOC_DEFINE(M_PFTEMP, "pf_temp", "pf(4) temporary allocations"); 133240233Sglebiusstruct pf_send_entry { 134240233Sglebius STAILQ_ENTRY(pf_send_entry) pfse_next; 135240233Sglebius struct mbuf *pfse_m; 136240233Sglebius enum { 137240233Sglebius PFSE_IP, 138240233Sglebius PFSE_IP6, 139240233Sglebius PFSE_ICMP, 140240233Sglebius PFSE_ICMP6, 141240233Sglebius } pfse_type; 142240233Sglebius union { 143240233Sglebius struct route ro; 144240233Sglebius struct { 145240233Sglebius int type; 146240233Sglebius int code; 147240233Sglebius int mtu; 148240233Sglebius } icmpopts; 149240233Sglebius } u; 150240233Sglebius#define pfse_ro u.ro 151240233Sglebius#define pfse_icmp_type u.icmpopts.type 152240233Sglebius#define pfse_icmp_code u.icmpopts.code 153240233Sglebius#define pfse_icmp_mtu u.icmpopts.mtu 154240233Sglebius}; 155223637Sbz 156240233SglebiusSTAILQ_HEAD(pf_send_head, pf_send_entry); 157240233Sglebiusstatic VNET_DEFINE(struct pf_send_head, pf_sendqueue); 158240233Sglebius#define V_pf_sendqueue VNET(pf_sendqueue) 159126258Smlaier 160240233Sglebiusstatic struct mtx pf_sendqueue_mtx; 161240233Sglebius#define PF_SENDQ_LOCK() mtx_lock(&pf_sendqueue_mtx) 162240233Sglebius#define PF_SENDQ_UNLOCK() mtx_unlock(&pf_sendqueue_mtx) 163126258Smlaier 164240233Sglebius/* 165240811Sglebius * Queue for pf_overload_task() tasks. 166240233Sglebius */ 167240811Sglebiusstruct pf_overload_entry { 168240811Sglebius SLIST_ENTRY(pf_overload_entry) next; 169240233Sglebius struct pf_addr addr; 170240233Sglebius sa_family_t af; 171240233Sglebius uint8_t dir; 172240811Sglebius struct pf_rule *rule; 173240233Sglebius}; 174223637Sbz 175240811SglebiusSLIST_HEAD(pf_overload_head, pf_overload_entry); 176240811Sglebiusstatic VNET_DEFINE(struct pf_overload_head, pf_overloadqueue); 177240811Sglebius#define V_pf_overloadqueue VNET(pf_overloadqueue) 178240811Sglebiusstatic VNET_DEFINE(struct task, pf_overloadtask); 179240811Sglebius#define V_pf_overloadtask VNET(pf_overloadtask) 180126261Smlaier 181240811Sglebiusstatic struct mtx pf_overloadqueue_mtx; 182240811Sglebius#define PF_OVERLOADQ_LOCK() mtx_lock(&pf_overloadqueue_mtx) 183240811Sglebius#define PF_OVERLOADQ_UNLOCK() mtx_unlock(&pf_overloadqueue_mtx) 184126258Smlaier 185240233SglebiusVNET_DEFINE(struct pf_rulequeue, pf_unlinked_rules); 186240233Sglebiusstruct mtx pf_unlnkdrules_mtx; 187240233Sglebius 188240233Sglebiusstatic VNET_DEFINE(uma_zone_t, pf_sources_z); 189240233Sglebius#define V_pf_sources_z VNET(pf_sources_z) 190240233Sglebiusstatic VNET_DEFINE(uma_zone_t, pf_mtag_z); 191240233Sglebius#define V_pf_mtag_z VNET(pf_mtag_z) 192240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_z); 193240233SglebiusVNET_DEFINE(uma_zone_t, pf_state_key_z); 194240233Sglebius 195240233SglebiusVNET_DEFINE(uint64_t, pf_stateid[MAXCPU]); 196240233Sglebius#define PFID_CPUBITS 8 197240233Sglebius#define PFID_CPUSHIFT (sizeof(uint64_t) * NBBY - PFID_CPUBITS) 198240233Sglebius#define PFID_CPUMASK ((uint64_t)((1 << PFID_CPUBITS) - 1) << PFID_CPUSHIFT) 199240233Sglebius#define PFID_MAXID (~PFID_CPUMASK) 200240233SglebiusCTASSERT((1 << PFID_CPUBITS) > MAXCPU); 201240233Sglebius 202240233Sglebiusstatic void pf_src_tree_remove_state(struct pf_state *); 203240233Sglebiusstatic void pf_init_threshold(struct pf_threshold *, u_int32_t, 204145836Smlaier u_int32_t); 205240233Sglebiusstatic void pf_add_threshold(struct pf_threshold *); 206240233Sglebiusstatic int pf_check_threshold(struct pf_threshold *); 207145836Smlaier 208240233Sglebiusstatic void pf_change_ap(struct pf_addr *, u_int16_t *, 209126258Smlaier u_int16_t *, u_int16_t *, struct pf_addr *, 210126258Smlaier u_int16_t, u_int8_t, sa_family_t); 211240233Sglebiusstatic int pf_modulate_sack(struct mbuf *, int, struct pf_pdesc *, 212171168Smlaier struct tcphdr *, struct pf_state_peer *); 213240233Sglebiusstatic void pf_change_icmp(struct pf_addr *, u_int16_t *, 214126258Smlaier struct pf_addr *, struct pf_addr *, u_int16_t, 215126258Smlaier u_int16_t *, u_int16_t *, u_int16_t *, 216126258Smlaier u_int16_t *, u_int8_t, sa_family_t); 217240233Sglebiusstatic void pf_send_tcp(struct mbuf *, 218162238Scsjp const struct pf_rule *, sa_family_t, 219126258Smlaier const struct pf_addr *, const struct pf_addr *, 220126258Smlaier u_int16_t, u_int16_t, u_int32_t, u_int32_t, 221145836Smlaier u_int8_t, u_int16_t, u_int16_t, u_int8_t, int, 222240233Sglebius u_int16_t, struct ifnet *); 223223637Sbzstatic void pf_send_icmp(struct mbuf *, u_int8_t, u_int8_t, 224126258Smlaier sa_family_t, struct pf_rule *); 225240233Sglebiusstatic void pf_detach_state(struct pf_state *); 226240233Sglebiusstatic int pf_state_key_attach(struct pf_state_key *, 227240233Sglebius struct pf_state_key *, struct pf_state *); 228240233Sglebiusstatic void pf_state_key_detach(struct pf_state *, int); 229240233Sglebiusstatic int pf_state_key_ctor(void *, int, void *, int); 230240233Sglebiusstatic u_int32_t pf_tcp_iss(struct pf_pdesc *); 231240233Sglebiusstatic int pf_test_rule(struct pf_rule **, struct pf_state **, 232130613Smlaier int, struct pfi_kif *, struct mbuf *, int, 233240233Sglebius struct pf_pdesc *, struct pf_rule **, 234240233Sglebius struct pf_ruleset **, struct inpcb *); 235240233Sglebiusstatic int pf_create_state(struct pf_rule *, struct pf_rule *, 236223637Sbz struct pf_rule *, struct pf_pdesc *, 237223637Sbz struct pf_src_node *, struct pf_state_key *, 238223637Sbz struct pf_state_key *, struct mbuf *, int, 239223637Sbz u_int16_t, u_int16_t, int *, struct pfi_kif *, 240223637Sbz struct pf_state **, int, u_int16_t, u_int16_t, 241223637Sbz int); 242240233Sglebiusstatic int pf_test_fragment(struct pf_rule **, int, 243130613Smlaier struct pfi_kif *, struct mbuf *, void *, 244126258Smlaier struct pf_pdesc *, struct pf_rule **, 245126258Smlaier struct pf_ruleset **); 246240233Sglebiusstatic int pf_tcp_track_full(struct pf_state_peer *, 247200930Sdelphij struct pf_state_peer *, struct pf_state **, 248200930Sdelphij struct pfi_kif *, struct mbuf *, int, 249200930Sdelphij struct pf_pdesc *, u_short *, int *); 250240233Sglebiusstatic int pf_tcp_track_sloppy(struct pf_state_peer *, 251200930Sdelphij struct pf_state_peer *, struct pf_state **, 252200930Sdelphij struct pf_pdesc *, u_short *); 253240233Sglebiusstatic int pf_test_state_tcp(struct pf_state **, int, 254130613Smlaier struct pfi_kif *, struct mbuf *, int, 255126258Smlaier void *, struct pf_pdesc *, u_short *); 256240233Sglebiusstatic int pf_test_state_udp(struct pf_state **, int, 257130613Smlaier struct pfi_kif *, struct mbuf *, int, 258126258Smlaier void *, struct pf_pdesc *); 259240233Sglebiusstatic int pf_test_state_icmp(struct pf_state **, int, 260130613Smlaier struct pfi_kif *, struct mbuf *, int, 261145836Smlaier void *, struct pf_pdesc *, u_short *); 262240233Sglebiusstatic int pf_test_state_other(struct pf_state **, int, 263223637Sbz struct pfi_kif *, struct mbuf *, struct pf_pdesc *); 264240233Sglebiusstatic u_int8_t pf_get_wscale(struct mbuf *, int, u_int16_t, 265126258Smlaier sa_family_t); 266240233Sglebiusstatic u_int16_t pf_get_mss(struct mbuf *, int, u_int16_t, 267126258Smlaier sa_family_t); 268240233Sglebiusstatic u_int16_t pf_calc_mss(struct pf_addr *, sa_family_t, 269231852Sbz int, u_int16_t); 270240233Sglebiusstatic void pf_set_rt_ifp(struct pf_state *, 271126258Smlaier struct pf_addr *); 272240233Sglebiusstatic int pf_check_proto_cksum(struct mbuf *, int, int, 273126258Smlaier u_int8_t, sa_family_t); 274240233Sglebiusstatic void pf_print_state_parts(struct pf_state *, 275223637Sbz struct pf_state_key *, struct pf_state_key *); 276240233Sglebiusstatic int pf_addr_wrap_neq(struct pf_addr_wrap *, 277126258Smlaier struct pf_addr_wrap *); 278240233Sglebiusstatic struct pf_state *pf_find_state(struct pfi_kif *, 279240233Sglebius struct pf_state_key_cmp *, u_int); 280240233Sglebiusstatic int pf_src_connlimit(struct pf_state **); 281240811Sglebiusstatic void pf_overload_task(void *c, int pending); 282240233Sglebiusstatic int pf_insert_src_node(struct pf_src_node **, 283240233Sglebius struct pf_rule *, struct pf_addr *, sa_family_t); 284241039Sglebiusstatic u_int pf_purge_expired_states(u_int, int); 285240233Sglebiusstatic void pf_purge_unlinked_rules(void); 286240233Sglebiusstatic int pf_mtag_init(void *, int, int); 287240233Sglebiusstatic void pf_mtag_free(struct m_tag *); 288240233Sglebius#ifdef INET 289240233Sglebiusstatic void pf_route(struct mbuf **, struct pf_rule *, int, 290240233Sglebius struct ifnet *, struct pf_state *, 291240233Sglebius struct pf_pdesc *); 292240233Sglebius#endif /* INET */ 293240233Sglebius#ifdef INET6 294240233Sglebiusstatic void pf_change_a6(struct pf_addr *, u_int16_t *, 295240233Sglebius struct pf_addr *, u_int8_t); 296240233Sglebiusstatic void pf_route6(struct mbuf **, struct pf_rule *, int, 297240233Sglebius struct ifnet *, struct pf_state *, 298240233Sglebius struct pf_pdesc *); 299240233Sglebius#endif /* INET6 */ 300126258Smlaier 301126261Smlaierint in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len); 302126258Smlaier 303223637SbzVNET_DECLARE(int, pf_end_threads); 304171168Smlaier 305240233SglebiusVNET_DEFINE(struct pf_limit, pf_limits[PF_LIMIT_MAX]); 306145836Smlaier 307240233Sglebius#define PACKET_LOOPED(pd) ((pd)->pf_mtag && \ 308240233Sglebius (pd)->pf_mtag->flags & PF_PACKET_LOOPED) 309126258Smlaier 310240233Sglebius#define STATE_LOOKUP(i, k, d, s, pd) \ 311126258Smlaier do { \ 312240233Sglebius (s) = pf_find_state((i), (k), (d)); \ 313240233Sglebius if ((s) == NULL || (s)->timeout == PFTM_PURGE) \ 314126258Smlaier return (PF_DROP); \ 315240233Sglebius if (PACKET_LOOPED(pd)) \ 316126258Smlaier return (PF_PASS); \ 317240233Sglebius if ((d) == PF_OUT && \ 318223637Sbz (((s)->rule.ptr->rt == PF_ROUTETO && \ 319223637Sbz (s)->rule.ptr->direction == PF_OUT) || \ 320223637Sbz ((s)->rule.ptr->rt == PF_REPLYTO && \ 321223637Sbz (s)->rule.ptr->direction == PF_IN)) && \ 322223637Sbz (s)->rt_kif != NULL && \ 323240233Sglebius (s)->rt_kif != (i)) \ 324223637Sbz return (PF_PASS); \ 325126258Smlaier } while (0) 326126258Smlaier 327223637Sbz#define BOUND_IFACE(r, k) \ 328223637Sbz ((r)->rule_flag & PFRULE_IFBOUND) ? (k) : V_pfi_all 329126258Smlaier 330223637Sbz#define STATE_INC_COUNTERS(s) \ 331145836Smlaier do { \ 332223637Sbz s->rule.ptr->states_cur++; \ 333223637Sbz s->rule.ptr->states_tot++; \ 334223637Sbz if (s->anchor.ptr != NULL) { \ 335223637Sbz s->anchor.ptr->states_cur++; \ 336223637Sbz s->anchor.ptr->states_tot++; \ 337223637Sbz } \ 338223637Sbz if (s->nat_rule.ptr != NULL) { \ 339223637Sbz s->nat_rule.ptr->states_cur++; \ 340223637Sbz s->nat_rule.ptr->states_tot++; \ 341223637Sbz } \ 342145836Smlaier } while (0) 343145836Smlaier 344223637Sbz#define STATE_DEC_COUNTERS(s) \ 345145836Smlaier do { \ 346145836Smlaier if (s->nat_rule.ptr != NULL) \ 347223637Sbz s->nat_rule.ptr->states_cur--; \ 348145836Smlaier if (s->anchor.ptr != NULL) \ 349223637Sbz s->anchor.ptr->states_cur--; \ 350223637Sbz s->rule.ptr->states_cur--; \ 351145836Smlaier } while (0) 352145836Smlaier 353240233Sglebiusstatic MALLOC_DEFINE(M_PFHASH, "pf_hash", "pf(4) hash header structures"); 354240233SglebiusVNET_DEFINE(struct pf_keyhash *, pf_keyhash); 355240233SglebiusVNET_DEFINE(struct pf_idhash *, pf_idhash); 356240233SglebiusVNET_DEFINE(u_long, pf_hashmask); 357240233SglebiusVNET_DEFINE(struct pf_srchash *, pf_srchash); 358240233SglebiusVNET_DEFINE(u_long, pf_srchashmask); 359223637Sbz 360240233SglebiusSYSCTL_NODE(_net, OID_AUTO, pf, CTLFLAG_RW, 0, "pf(4)"); 361223637Sbz 362240233SglebiusVNET_DEFINE(u_long, pf_hashsize); 363240233Sglebius#define V_pf_hashsize VNET(pf_hashsize) 364240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, states_hashsize, CTLFLAG_RDTUN, 365240233Sglebius &VNET_NAME(pf_hashsize), 0, "Size of pf(4) states hashtable"); 366130613Smlaier 367240233SglebiusVNET_DEFINE(u_long, pf_srchashsize); 368240233Sglebius#define V_pf_srchashsize VNET(pf_srchashsize) 369240233SglebiusSYSCTL_VNET_UINT(_net_pf, OID_AUTO, source_nodes_hashsize, CTLFLAG_RDTUN, 370240233Sglebius &VNET_NAME(pf_srchashsize), 0, "Size of pf(4) source nodes hashtable"); 371171168Smlaier 372240233SglebiusVNET_DEFINE(void *, pf_swi_cookie); 373130613Smlaier 374240233SglebiusVNET_DEFINE(uint32_t, pf_hashseed); 375240233Sglebius#define V_pf_hashseed VNET(pf_hashseed) 376240233Sglebius 377240233Sglebiusstatic __inline uint32_t 378240233Sglebiuspf_hashkey(struct pf_state_key *sk) 379126258Smlaier{ 380240233Sglebius uint32_t h; 381126258Smlaier 382240233Sglebius h = jenkins_hash32((uint32_t *)sk, 383240233Sglebius sizeof(struct pf_state_key_cmp)/sizeof(uint32_t), 384240233Sglebius V_pf_hashseed); 385240233Sglebius 386240233Sglebius return (h & V_pf_hashmask); 387130613Smlaier} 388130613Smlaier 389240736Sglebiusstatic __inline uint32_t 390240736Sglebiuspf_hashsrc(struct pf_addr *addr, sa_family_t af) 391240736Sglebius{ 392240736Sglebius uint32_t h; 393240736Sglebius 394240736Sglebius switch (af) { 395240736Sglebius case AF_INET: 396240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v4, 397240736Sglebius sizeof(addr->v4)/sizeof(uint32_t), V_pf_hashseed); 398240736Sglebius break; 399240736Sglebius case AF_INET6: 400240736Sglebius h = jenkins_hash32((uint32_t *)&addr->v6, 401240736Sglebius sizeof(addr->v6)/sizeof(uint32_t), V_pf_hashseed); 402240736Sglebius break; 403240736Sglebius default: 404240736Sglebius panic("%s: unknown address family %u", __func__, af); 405240736Sglebius } 406240736Sglebius 407240736Sglebius return (h & V_pf_srchashmask); 408240736Sglebius} 409240736Sglebius 410126258Smlaier#ifdef INET6 411126258Smlaiervoid 412126258Smlaierpf_addrcpy(struct pf_addr *dst, struct pf_addr *src, sa_family_t af) 413126258Smlaier{ 414126258Smlaier switch (af) { 415126258Smlaier#ifdef INET 416126258Smlaier case AF_INET: 417126258Smlaier dst->addr32[0] = src->addr32[0]; 418126258Smlaier break; 419126258Smlaier#endif /* INET */ 420126258Smlaier case AF_INET6: 421126258Smlaier dst->addr32[0] = src->addr32[0]; 422126258Smlaier dst->addr32[1] = src->addr32[1]; 423126258Smlaier dst->addr32[2] = src->addr32[2]; 424126258Smlaier dst->addr32[3] = src->addr32[3]; 425126258Smlaier break; 426126258Smlaier } 427126258Smlaier} 428145836Smlaier#endif /* INET6 */ 429126258Smlaier 430240233Sglebiusstatic void 431145836Smlaierpf_init_threshold(struct pf_threshold *threshold, 432145836Smlaier u_int32_t limit, u_int32_t seconds) 433145836Smlaier{ 434145836Smlaier threshold->limit = limit * PF_THRESHOLD_MULT; 435145836Smlaier threshold->seconds = seconds; 436145836Smlaier threshold->count = 0; 437240233Sglebius threshold->last = time_uptime; 438145836Smlaier} 439145836Smlaier 440240233Sglebiusstatic void 441145836Smlaierpf_add_threshold(struct pf_threshold *threshold) 442145836Smlaier{ 443240233Sglebius u_int32_t t = time_uptime, diff = t - threshold->last; 444145836Smlaier 445145836Smlaier if (diff >= threshold->seconds) 446145836Smlaier threshold->count = 0; 447145836Smlaier else 448145836Smlaier threshold->count -= threshold->count * diff / 449145836Smlaier threshold->seconds; 450145836Smlaier threshold->count += PF_THRESHOLD_MULT; 451145836Smlaier threshold->last = t; 452145836Smlaier} 453145836Smlaier 454240233Sglebiusstatic int 455145836Smlaierpf_check_threshold(struct pf_threshold *threshold) 456145836Smlaier{ 457145836Smlaier return (threshold->count > threshold->limit); 458145836Smlaier} 459145836Smlaier 460240233Sglebiusstatic int 461145836Smlaierpf_src_connlimit(struct pf_state **state) 462145836Smlaier{ 463240811Sglebius struct pf_overload_entry *pfoe; 464145836Smlaier int bad = 0; 465145836Smlaier 466240233Sglebius PF_STATE_LOCK_ASSERT(*state); 467240233Sglebius 468145836Smlaier (*state)->src_node->conn++; 469171168Smlaier (*state)->src.tcp_est = 1; 470145836Smlaier pf_add_threshold(&(*state)->src_node->conn_rate); 471145836Smlaier 472145836Smlaier if ((*state)->rule.ptr->max_src_conn && 473145836Smlaier (*state)->rule.ptr->max_src_conn < 474145836Smlaier (*state)->src_node->conn) { 475223637Sbz V_pf_status.lcounters[LCNT_SRCCONN]++; 476145836Smlaier bad++; 477145836Smlaier } 478145836Smlaier 479145836Smlaier if ((*state)->rule.ptr->max_src_conn_rate.limit && 480145836Smlaier pf_check_threshold(&(*state)->src_node->conn_rate)) { 481223637Sbz V_pf_status.lcounters[LCNT_SRCCONNRATE]++; 482145836Smlaier bad++; 483145836Smlaier } 484145836Smlaier 485145836Smlaier if (!bad) 486145836Smlaier return (0); 487145836Smlaier 488240233Sglebius /* Kill this state. */ 489240233Sglebius (*state)->timeout = PFTM_PURGE; 490240233Sglebius (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 491145836Smlaier 492240233Sglebius if ((*state)->rule.ptr->overload_tbl == NULL) 493240233Sglebius return (1); 494145836Smlaier 495240811Sglebius /* Schedule overloading and flushing task. */ 496240811Sglebius pfoe = malloc(sizeof(*pfoe), M_PFTEMP, M_NOWAIT); 497240811Sglebius if (pfoe == NULL) 498240233Sglebius return (1); /* too bad :( */ 499240233Sglebius 500240811Sglebius bcopy(&(*state)->src_node->addr, &pfoe->addr, sizeof(pfoe->addr)); 501240811Sglebius pfoe->af = (*state)->key[PF_SK_WIRE]->af; 502240811Sglebius pfoe->rule = (*state)->rule.ptr; 503240811Sglebius pfoe->dir = (*state)->direction; 504240811Sglebius PF_OVERLOADQ_LOCK(); 505240811Sglebius SLIST_INSERT_HEAD(&V_pf_overloadqueue, pfoe, next); 506240811Sglebius PF_OVERLOADQ_UNLOCK(); 507240811Sglebius taskqueue_enqueue(taskqueue_swi, &V_pf_overloadtask); 508240233Sglebius 509240233Sglebius return (1); 510240233Sglebius} 511240233Sglebius 512240233Sglebiusstatic void 513240811Sglebiuspf_overload_task(void *c, int pending) 514240233Sglebius{ 515240811Sglebius struct pf_overload_head queue; 516240811Sglebius struct pfr_addr p; 517240811Sglebius struct pf_overload_entry *pfoe, *pfoe1; 518240233Sglebius uint32_t killed = 0; 519240233Sglebius 520240811Sglebius PF_OVERLOADQ_LOCK(); 521240811Sglebius queue = *(struct pf_overload_head *)c; 522240811Sglebius SLIST_INIT((struct pf_overload_head *)c); 523240811Sglebius PF_OVERLOADQ_UNLOCK(); 524240233Sglebius 525240811Sglebius bzero(&p, sizeof(p)); 526240811Sglebius SLIST_FOREACH(pfoe, &queue, next) { 527240811Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_TABLE]++; 528240811Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) { 529240811Sglebius printf("%s: blocking address ", __func__); 530240811Sglebius pf_print_host(&pfoe->addr, 0, pfoe->af); 531240811Sglebius printf("\n"); 532240811Sglebius } 533240233Sglebius 534240811Sglebius p.pfra_af = pfoe->af; 535240811Sglebius switch (pfoe->af) { 536240811Sglebius#ifdef INET 537240811Sglebius case AF_INET: 538240811Sglebius p.pfra_net = 32; 539240811Sglebius p.pfra_ip4addr = pfoe->addr.v4; 540240811Sglebius break; 541240811Sglebius#endif 542240811Sglebius#ifdef INET6 543240811Sglebius case AF_INET6: 544240811Sglebius p.pfra_net = 128; 545240811Sglebius p.pfra_ip6addr = pfoe->addr.v6; 546240811Sglebius break; 547240811Sglebius#endif 548240811Sglebius } 549240811Sglebius 550240811Sglebius PF_RULES_WLOCK(); 551240811Sglebius pfr_insert_kentry(pfoe->rule->overload_tbl, &p, time_second); 552240811Sglebius PF_RULES_WUNLOCK(); 553240811Sglebius } 554240811Sglebius 555240811Sglebius /* 556240811Sglebius * Remove those entries, that don't need flushing. 557240811Sglebius */ 558240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 559240811Sglebius if (pfoe->rule->flush == 0) { 560240811Sglebius SLIST_REMOVE(&queue, pfoe, pf_overload_entry, next); 561240811Sglebius free(pfoe, M_PFTEMP); 562240811Sglebius } else 563240811Sglebius V_pf_status.lcounters[LCNT_OVERLOAD_FLUSH]++; 564240811Sglebius 565240811Sglebius /* If nothing to flush, return. */ 566240811Sglebius if (SLIST_EMPTY(&queue)) 567240811Sglebius return; 568240811Sglebius 569240233Sglebius for (int i = 0; i <= V_pf_hashmask; i++) { 570240233Sglebius struct pf_idhash *ih = &V_pf_idhash[i]; 571240233Sglebius struct pf_state_key *sk; 572240233Sglebius struct pf_state *s; 573240233Sglebius 574240233Sglebius PF_HASHROW_LOCK(ih); 575240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 576240233Sglebius sk = s->key[PF_SK_WIRE]; 577240811Sglebius SLIST_FOREACH(pfoe, &queue, next) 578240811Sglebius if (sk->af == pfoe->af && 579240811Sglebius ((pfoe->rule->flush & PF_FLUSH_GLOBAL) || 580240811Sglebius pfoe->rule == s->rule.ptr) && 581240811Sglebius ((pfoe->dir == PF_OUT && 582240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[1], sk->af)) || 583240811Sglebius (pfoe->dir == PF_IN && 584240811Sglebius PF_AEQ(&pfoe->addr, &sk->addr[0], sk->af)))) { 585240233Sglebius s->timeout = PFTM_PURGE; 586240233Sglebius s->src.state = s->dst.state = TCPS_CLOSED; 587240233Sglebius killed++; 588145836Smlaier } 589145836Smlaier } 590240233Sglebius PF_HASHROW_UNLOCK(ih); 591145836Smlaier } 592240811Sglebius SLIST_FOREACH_SAFE(pfoe, &queue, next, pfoe1) 593240811Sglebius free(pfoe, M_PFTEMP); 594240233Sglebius if (V_pf_status.debug >= PF_DEBUG_MISC) 595240233Sglebius printf("%s: %u states killed", __func__, killed); 596240233Sglebius} 597145836Smlaier 598240233Sglebius/* 599240233Sglebius * Can return locked on failure, so that we can consistently 600240233Sglebius * allocate and insert a new one. 601240233Sglebius */ 602240233Sglebiusstruct pf_src_node * 603240233Sglebiuspf_find_src_node(struct pf_addr *src, struct pf_rule *rule, sa_family_t af, 604240233Sglebius int returnlocked) 605240233Sglebius{ 606240233Sglebius struct pf_srchash *sh; 607240233Sglebius struct pf_src_node *n; 608240233Sglebius 609240233Sglebius V_pf_status.scounters[SCNT_SRC_NODE_SEARCH]++; 610240233Sglebius 611240233Sglebius sh = &V_pf_srchash[pf_hashsrc(src, af)]; 612240233Sglebius PF_HASHROW_LOCK(sh); 613240233Sglebius LIST_FOREACH(n, &sh->nodes, entry) 614240233Sglebius if (n->rule.ptr == rule && n->af == af && 615240233Sglebius ((af == AF_INET && n->addr.v4.s_addr == src->v4.s_addr) || 616240233Sglebius (af == AF_INET6 && bcmp(&n->addr, src, sizeof(*src)) == 0))) 617240233Sglebius break; 618240233Sglebius if (n != NULL || returnlocked == 0) 619240233Sglebius PF_HASHROW_UNLOCK(sh); 620240233Sglebius 621240233Sglebius return (n); 622145836Smlaier} 623145836Smlaier 624240233Sglebiusstatic int 625130613Smlaierpf_insert_src_node(struct pf_src_node **sn, struct pf_rule *rule, 626130613Smlaier struct pf_addr *src, sa_family_t af) 627126258Smlaier{ 628126258Smlaier 629240233Sglebius KASSERT((rule->rule_flag & PFRULE_RULESRCTRACK || 630240233Sglebius rule->rpool.opts & PF_POOL_STICKYADDR), 631240233Sglebius ("%s for non-tracking rule %p", __func__, rule)); 632240233Sglebius 633240233Sglebius if (*sn == NULL) 634240233Sglebius *sn = pf_find_src_node(src, rule, af, 1); 635240233Sglebius 636130613Smlaier if (*sn == NULL) { 637240233Sglebius struct pf_srchash *sh = &V_pf_srchash[pf_hashsrc(src, af)]; 638240233Sglebius 639240233Sglebius PF_HASHROW_ASSERT(sh); 640240233Sglebius 641130613Smlaier if (!rule->max_src_nodes || 642130613Smlaier rule->src_nodes < rule->max_src_nodes) 643240233Sglebius (*sn) = uma_zalloc(V_pf_sources_z, M_NOWAIT | M_ZERO); 644145836Smlaier else 645223637Sbz V_pf_status.lcounters[LCNT_SRCNODES]++; 646240233Sglebius if ((*sn) == NULL) { 647240233Sglebius PF_HASHROW_UNLOCK(sh); 648130613Smlaier return (-1); 649240233Sglebius } 650145836Smlaier 651145836Smlaier pf_init_threshold(&(*sn)->conn_rate, 652145836Smlaier rule->max_src_conn_rate.limit, 653145836Smlaier rule->max_src_conn_rate.seconds); 654145836Smlaier 655130613Smlaier (*sn)->af = af; 656240233Sglebius (*sn)->rule.ptr = rule; 657130613Smlaier PF_ACPY(&(*sn)->addr, src, af); 658240233Sglebius LIST_INSERT_HEAD(&sh->nodes, *sn, entry); 659240233Sglebius (*sn)->creation = time_uptime; 660130613Smlaier (*sn)->ruletype = rule->action; 661130613Smlaier if ((*sn)->rule.ptr != NULL) 662130613Smlaier (*sn)->rule.ptr->src_nodes++; 663240233Sglebius PF_HASHROW_UNLOCK(sh); 664223637Sbz V_pf_status.scounters[SCNT_SRC_NODE_INSERT]++; 665223637Sbz V_pf_status.src_nodes++; 666130613Smlaier } else { 667130613Smlaier if (rule->max_src_states && 668145836Smlaier (*sn)->states >= rule->max_src_states) { 669223637Sbz V_pf_status.lcounters[LCNT_SRCSTATES]++; 670130613Smlaier return (-1); 671145836Smlaier } 672130613Smlaier } 673130613Smlaier return (0); 674130613Smlaier} 675126258Smlaier 676240233Sglebiusstatic void 677240233Sglebiuspf_remove_src_node(struct pf_src_node *src) 678240233Sglebius{ 679240233Sglebius struct pf_srchash *sh; 680223637Sbz 681240233Sglebius sh = &V_pf_srchash[pf_hashsrc(&src->addr, src->af)]; 682240233Sglebius PF_HASHROW_LOCK(sh); 683240233Sglebius LIST_REMOVE(src, entry); 684240233Sglebius PF_HASHROW_UNLOCK(sh); 685240737Sglebius 686240737Sglebius V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++; 687240737Sglebius V_pf_status.src_nodes--; 688240737Sglebius 689240737Sglebius uma_zfree(V_pf_sources_z, src); 690240233Sglebius} 691240233Sglebius 692240233Sglebius/* Data storage structures initialization. */ 693240233Sglebiusvoid 694240233Sglebiuspf_initialize() 695223637Sbz{ 696240233Sglebius struct pf_keyhash *kh; 697240233Sglebius struct pf_idhash *ih; 698240233Sglebius struct pf_srchash *sh; 699240233Sglebius u_int i; 700223637Sbz 701240233Sglebius TUNABLE_ULONG_FETCH("net.pf.states_hashsize", &V_pf_hashsize); 702240233Sglebius if (V_pf_hashsize == 0 || !powerof2(V_pf_hashsize)) 703240233Sglebius V_pf_hashsize = PF_HASHSIZ; 704240233Sglebius TUNABLE_ULONG_FETCH("net.pf.source_nodes_hashsize", &V_pf_srchashsize); 705240233Sglebius if (V_pf_srchashsize == 0 || !powerof2(V_pf_srchashsize)) 706240233Sglebius V_pf_srchashsize = PF_HASHSIZ / 4; 707240233Sglebius 708240233Sglebius V_pf_hashseed = arc4random(); 709240233Sglebius 710240233Sglebius /* States and state keys storage. */ 711240233Sglebius V_pf_state_z = uma_zcreate("pf states", sizeof(struct pf_state), 712240233Sglebius NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 713240233Sglebius V_pf_limits[PF_LIMIT_STATES].zone = V_pf_state_z; 714240233Sglebius uma_zone_set_max(V_pf_state_z, PFSTATE_HIWAT); 715244347Spjd uma_zone_set_warning(V_pf_state_z, "PF states limit reached"); 716240233Sglebius 717240233Sglebius V_pf_state_key_z = uma_zcreate("pf state keys", 718240233Sglebius sizeof(struct pf_state_key), pf_state_key_ctor, NULL, NULL, NULL, 719240233Sglebius UMA_ALIGN_PTR, 0); 720240233Sglebius V_pf_keyhash = malloc(V_pf_hashsize * sizeof(struct pf_keyhash), 721240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 722240233Sglebius V_pf_idhash = malloc(V_pf_hashsize * sizeof(struct pf_idhash), 723240233Sglebius M_PFHASH, M_WAITOK | M_ZERO); 724240233Sglebius V_pf_hashmask = V_pf_hashsize - 1; 725240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 726240233Sglebius i++, kh++, ih++) { 727251681Sglebius mtx_init(&kh->lock, "pf_keyhash", NULL, MTX_DEF | MTX_DUPOK); 728240233Sglebius mtx_init(&ih->lock, "pf_idhash", NULL, MTX_DEF); 729223637Sbz } 730223637Sbz 731240233Sglebius /* Source nodes. */ 732240233Sglebius V_pf_sources_z = uma_zcreate("pf source nodes", 733240233Sglebius sizeof(struct pf_src_node), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 734240233Sglebius 0); 735240233Sglebius V_pf_limits[PF_LIMIT_SRC_NODES].zone = V_pf_sources_z; 736240233Sglebius uma_zone_set_max(V_pf_sources_z, PFSNODE_HIWAT); 737244347Spjd uma_zone_set_warning(V_pf_sources_z, "PF source nodes limit reached"); 738240233Sglebius V_pf_srchash = malloc(V_pf_srchashsize * sizeof(struct pf_srchash), 739240233Sglebius M_PFHASH, M_WAITOK|M_ZERO); 740240233Sglebius V_pf_srchashmask = V_pf_srchashsize - 1; 741240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) 742240233Sglebius mtx_init(&sh->lock, "pf_srchash", NULL, MTX_DEF); 743223637Sbz 744240233Sglebius /* ALTQ */ 745240233Sglebius TAILQ_INIT(&V_pf_altqs[0]); 746240233Sglebius TAILQ_INIT(&V_pf_altqs[1]); 747240233Sglebius TAILQ_INIT(&V_pf_pabuf); 748240233Sglebius V_pf_altqs_active = &V_pf_altqs[0]; 749240233Sglebius V_pf_altqs_inactive = &V_pf_altqs[1]; 750240233Sglebius 751240233Sglebius /* Mbuf tags */ 752240233Sglebius V_pf_mtag_z = uma_zcreate("pf mtags", sizeof(struct m_tag) + 753240233Sglebius sizeof(struct pf_mtag), NULL, NULL, pf_mtag_init, NULL, 754240233Sglebius UMA_ALIGN_PTR, 0); 755240233Sglebius 756240811Sglebius /* Send & overload+flush queues. */ 757240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 758240811Sglebius SLIST_INIT(&V_pf_overloadqueue); 759240811Sglebius TASK_INIT(&V_pf_overloadtask, 0, pf_overload_task, &V_pf_overloadqueue); 760240233Sglebius mtx_init(&pf_sendqueue_mtx, "pf send queue", NULL, MTX_DEF); 761240811Sglebius mtx_init(&pf_overloadqueue_mtx, "pf overload/flush queue", NULL, 762240811Sglebius MTX_DEF); 763240233Sglebius 764240233Sglebius /* Unlinked, but may be referenced rules. */ 765240233Sglebius TAILQ_INIT(&V_pf_unlinked_rules); 766240233Sglebius mtx_init(&pf_unlnkdrules_mtx, "pf unlinked rules", NULL, MTX_DEF); 767223637Sbz} 768223637Sbz 769240233Sglebiusvoid 770240233Sglebiuspf_cleanup() 771223637Sbz{ 772240233Sglebius struct pf_keyhash *kh; 773240233Sglebius struct pf_idhash *ih; 774240233Sglebius struct pf_srchash *sh; 775240233Sglebius struct pf_send_entry *pfse, *next; 776240233Sglebius u_int i; 777223637Sbz 778240233Sglebius for (i = 0, kh = V_pf_keyhash, ih = V_pf_idhash; i <= V_pf_hashmask; 779240233Sglebius i++, kh++, ih++) { 780240233Sglebius KASSERT(LIST_EMPTY(&kh->keys), ("%s: key hash not empty", 781240233Sglebius __func__)); 782240233Sglebius KASSERT(LIST_EMPTY(&ih->states), ("%s: id hash not empty", 783240233Sglebius __func__)); 784240233Sglebius mtx_destroy(&kh->lock); 785240233Sglebius mtx_destroy(&ih->lock); 786240233Sglebius } 787240233Sglebius free(V_pf_keyhash, M_PFHASH); 788240233Sglebius free(V_pf_idhash, M_PFHASH); 789240233Sglebius 790240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 791240233Sglebius KASSERT(LIST_EMPTY(&sh->nodes), 792240233Sglebius ("%s: source node hash not empty", __func__)); 793240233Sglebius mtx_destroy(&sh->lock); 794240233Sglebius } 795240233Sglebius free(V_pf_srchash, M_PFHASH); 796240233Sglebius 797240233Sglebius STAILQ_FOREACH_SAFE(pfse, &V_pf_sendqueue, pfse_next, next) { 798240233Sglebius m_freem(pfse->pfse_m); 799240233Sglebius free(pfse, M_PFTEMP); 800240233Sglebius } 801240233Sglebius 802240233Sglebius mtx_destroy(&pf_sendqueue_mtx); 803240811Sglebius mtx_destroy(&pf_overloadqueue_mtx); 804240233Sglebius mtx_destroy(&pf_unlnkdrules_mtx); 805240233Sglebius 806240233Sglebius uma_zdestroy(V_pf_mtag_z); 807240233Sglebius uma_zdestroy(V_pf_sources_z); 808240233Sglebius uma_zdestroy(V_pf_state_z); 809240233Sglebius uma_zdestroy(V_pf_state_key_z); 810240233Sglebius} 811240233Sglebius 812240233Sglebiusstatic int 813240233Sglebiuspf_mtag_init(void *mem, int size, int how) 814240233Sglebius{ 815240233Sglebius struct m_tag *t; 816240233Sglebius 817240233Sglebius t = (struct m_tag *)mem; 818240233Sglebius t->m_tag_cookie = MTAG_ABI_COMPAT; 819240233Sglebius t->m_tag_id = PACKET_TAG_PF; 820240233Sglebius t->m_tag_len = sizeof(struct pf_mtag); 821240233Sglebius t->m_tag_free = pf_mtag_free; 822240233Sglebius 823223637Sbz return (0); 824223637Sbz} 825223637Sbz 826240233Sglebiusstatic void 827240233Sglebiuspf_mtag_free(struct m_tag *t) 828130613Smlaier{ 829223637Sbz 830240233Sglebius uma_zfree(V_pf_mtag_z, t); 831240233Sglebius} 832223637Sbz 833240233Sglebiusstruct pf_mtag * 834240233Sglebiuspf_get_mtag(struct mbuf *m) 835240233Sglebius{ 836240233Sglebius struct m_tag *mtag; 837240233Sglebius 838240233Sglebius if ((mtag = m_tag_find(m, PACKET_TAG_PF, NULL)) != NULL) 839240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 840240233Sglebius 841240233Sglebius mtag = uma_zalloc(V_pf_mtag_z, M_NOWAIT); 842240233Sglebius if (mtag == NULL) 843240233Sglebius return (NULL); 844240233Sglebius bzero(mtag + 1, sizeof(struct pf_mtag)); 845240233Sglebius m_tag_prepend(m, mtag); 846240233Sglebius 847240233Sglebius return ((struct pf_mtag *)(mtag + 1)); 848240233Sglebius} 849240233Sglebius 850240233Sglebiusstatic int 851240233Sglebiuspf_state_key_attach(struct pf_state_key *skw, struct pf_state_key *sks, 852240233Sglebius struct pf_state *s) 853240233Sglebius{ 854251681Sglebius struct pf_keyhash *khs, *khw, *kh; 855240233Sglebius struct pf_state_key *sk, *cur; 856240233Sglebius struct pf_state *si, *olds = NULL; 857240233Sglebius int idx; 858240233Sglebius 859240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 860240233Sglebius KASSERT(s->key[PF_SK_WIRE] == NULL, ("%s: state has key", __func__)); 861240233Sglebius KASSERT(s->key[PF_SK_STACK] == NULL, ("%s: state has key", __func__)); 862240233Sglebius 863240233Sglebius /* 864251681Sglebius * We need to lock hash slots of both keys. To avoid deadlock 865251681Sglebius * we always lock the slot with lower address first. Unlock order 866251681Sglebius * isn't important. 867251681Sglebius * 868251681Sglebius * We also need to lock ID hash slot before dropping key 869251681Sglebius * locks. On success we return with ID hash slot locked. 870251681Sglebius */ 871251681Sglebius 872251681Sglebius if (skw == sks) { 873251681Sglebius khs = khw = &V_pf_keyhash[pf_hashkey(skw)]; 874251681Sglebius PF_HASHROW_LOCK(khs); 875251681Sglebius } else { 876251681Sglebius khs = &V_pf_keyhash[pf_hashkey(sks)]; 877251681Sglebius khw = &V_pf_keyhash[pf_hashkey(skw)]; 878251681Sglebius if (khs == khw) { 879251681Sglebius PF_HASHROW_LOCK(khs); 880251681Sglebius } else if (khs < khw) { 881251681Sglebius PF_HASHROW_LOCK(khs); 882251681Sglebius PF_HASHROW_LOCK(khw); 883251681Sglebius } else { 884251681Sglebius PF_HASHROW_LOCK(khw); 885251681Sglebius PF_HASHROW_LOCK(khs); 886251681Sglebius } 887251681Sglebius } 888251681Sglebius 889251681Sglebius#define KEYS_UNLOCK() do { \ 890251681Sglebius if (khs != khw) { \ 891251681Sglebius PF_HASHROW_UNLOCK(khs); \ 892251681Sglebius PF_HASHROW_UNLOCK(khw); \ 893251681Sglebius } else \ 894251681Sglebius PF_HASHROW_UNLOCK(khs); \ 895251681Sglebius} while (0) 896251681Sglebius 897251681Sglebius /* 898240233Sglebius * First run: start with wire key. 899240233Sglebius */ 900240233Sglebius sk = skw; 901251681Sglebius kh = khw; 902240233Sglebius idx = PF_SK_WIRE; 903240233Sglebius 904240233Sglebiuskeyattach: 905240233Sglebius LIST_FOREACH(cur, &kh->keys, entry) 906240233Sglebius if (bcmp(cur, sk, sizeof(struct pf_state_key_cmp)) == 0) 907240233Sglebius break; 908240233Sglebius 909240233Sglebius if (cur != NULL) { 910240233Sglebius /* Key exists. Check for same kif, if none, add to key. */ 911240233Sglebius TAILQ_FOREACH(si, &cur->states[idx], key_list[idx]) { 912240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(si)]; 913240233Sglebius 914240233Sglebius PF_HASHROW_LOCK(ih); 915240233Sglebius if (si->kif == s->kif && 916240233Sglebius si->direction == s->direction) { 917223637Sbz if (sk->proto == IPPROTO_TCP && 918240233Sglebius si->src.state >= TCPS_FIN_WAIT_2 && 919240233Sglebius si->dst.state >= TCPS_FIN_WAIT_2) { 920251681Sglebius /* 921251681Sglebius * New state matches an old >FIN_WAIT_2 922251681Sglebius * state. We can't drop key hash locks, 923251681Sglebius * thus we can't unlink it properly. 924251681Sglebius * 925251681Sglebius * As a workaround we drop it into 926251681Sglebius * TCPS_CLOSED state, schedule purge 927251681Sglebius * ASAP and push it into the very end 928251681Sglebius * of the slot TAILQ, so that it won't 929251681Sglebius * conflict with our new state. 930251681Sglebius */ 931240233Sglebius si->src.state = si->dst.state = 932223637Sbz TCPS_CLOSED; 933251681Sglebius si->timeout = PFTM_PURGE; 934240233Sglebius olds = si; 935223637Sbz } else { 936223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 937223637Sbz printf("pf: %s key attach " 938223637Sbz "failed on %s: ", 939223637Sbz (idx == PF_SK_WIRE) ? 940223637Sbz "wire" : "stack", 941223637Sbz s->kif->pfik_name); 942223637Sbz pf_print_state_parts(s, 943223637Sbz (idx == PF_SK_WIRE) ? 944223637Sbz sk : NULL, 945223637Sbz (idx == PF_SK_STACK) ? 946223637Sbz sk : NULL); 947223637Sbz printf(", existing: "); 948240233Sglebius pf_print_state_parts(si, 949223637Sbz (idx == PF_SK_WIRE) ? 950223637Sbz sk : NULL, 951223637Sbz (idx == PF_SK_STACK) ? 952223637Sbz sk : NULL); 953223637Sbz printf("\n"); 954223637Sbz } 955240233Sglebius PF_HASHROW_UNLOCK(ih); 956251681Sglebius KEYS_UNLOCK(); 957240233Sglebius uma_zfree(V_pf_state_key_z, sk); 958240233Sglebius if (idx == PF_SK_STACK) 959240233Sglebius pf_detach_state(s); 960250522Sglebius return (EEXIST); /* collision! */ 961223637Sbz } 962223637Sbz } 963240233Sglebius PF_HASHROW_UNLOCK(ih); 964240233Sglebius } 965240233Sglebius uma_zfree(V_pf_state_key_z, sk); 966223637Sbz s->key[idx] = cur; 967240233Sglebius } else { 968240233Sglebius LIST_INSERT_HEAD(&kh->keys, sk, entry); 969223637Sbz s->key[idx] = sk; 970126258Smlaier } 971126258Smlaier 972240233Sglebiusstateattach: 973240233Sglebius /* List is sorted, if-bound states before floating. */ 974223637Sbz if (s->kif == V_pfi_all) 975240233Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], s, key_list[idx]); 976223637Sbz else 977240233Sglebius TAILQ_INSERT_HEAD(&s->key[idx]->states[idx], s, key_list[idx]); 978223637Sbz 979251681Sglebius if (olds) { 980251681Sglebius TAILQ_REMOVE(&s->key[idx]->states[idx], olds, key_list[idx]); 981251681Sglebius TAILQ_INSERT_TAIL(&s->key[idx]->states[idx], olds, 982251681Sglebius key_list[idx]); 983251681Sglebius olds = NULL; 984251681Sglebius } 985251681Sglebius 986240233Sglebius /* 987240233Sglebius * Attach done. See how should we (or should not?) 988240233Sglebius * attach a second key. 989240233Sglebius */ 990240233Sglebius if (sks == skw) { 991240233Sglebius s->key[PF_SK_STACK] = s->key[PF_SK_WIRE]; 992240233Sglebius idx = PF_SK_STACK; 993240233Sglebius sks = NULL; 994240233Sglebius goto stateattach; 995240233Sglebius } else if (sks != NULL) { 996240233Sglebius /* 997240233Sglebius * Continue attaching with stack key. 998240233Sglebius */ 999240233Sglebius sk = sks; 1000251681Sglebius kh = khs; 1001240233Sglebius idx = PF_SK_STACK; 1002240233Sglebius sks = NULL; 1003240233Sglebius goto keyattach; 1004240233Sglebius } 1005240233Sglebius 1006251681Sglebius PF_STATE_LOCK(s); 1007251681Sglebius KEYS_UNLOCK(); 1008251681Sglebius 1009240233Sglebius KASSERT(s->key[PF_SK_WIRE] != NULL && s->key[PF_SK_STACK] != NULL, 1010240233Sglebius ("%s failure", __func__)); 1011240233Sglebius 1012223637Sbz return (0); 1013251681Sglebius#undef KEYS_UNLOCK 1014223637Sbz} 1015223637Sbz 1016240233Sglebiusstatic void 1017223637Sbzpf_detach_state(struct pf_state *s) 1018223637Sbz{ 1019240233Sglebius struct pf_state_key *sks = s->key[PF_SK_STACK]; 1020240233Sglebius struct pf_keyhash *kh; 1021223637Sbz 1022240233Sglebius if (sks != NULL) { 1023240233Sglebius kh = &V_pf_keyhash[pf_hashkey(sks)]; 1024240233Sglebius PF_HASHROW_LOCK(kh); 1025240233Sglebius if (s->key[PF_SK_STACK] != NULL) 1026240233Sglebius pf_state_key_detach(s, PF_SK_STACK); 1027240233Sglebius /* 1028240233Sglebius * If both point to same key, then we are done. 1029240233Sglebius */ 1030240233Sglebius if (sks == s->key[PF_SK_WIRE]) { 1031240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1032240233Sglebius PF_HASHROW_UNLOCK(kh); 1033240233Sglebius return; 1034240233Sglebius } 1035240233Sglebius PF_HASHROW_UNLOCK(kh); 1036240233Sglebius } 1037223637Sbz 1038240233Sglebius if (s->key[PF_SK_WIRE] != NULL) { 1039240233Sglebius kh = &V_pf_keyhash[pf_hashkey(s->key[PF_SK_WIRE])]; 1040240233Sglebius PF_HASHROW_LOCK(kh); 1041240233Sglebius if (s->key[PF_SK_WIRE] != NULL) 1042240233Sglebius pf_state_key_detach(s, PF_SK_WIRE); 1043240233Sglebius PF_HASHROW_UNLOCK(kh); 1044240233Sglebius } 1045223637Sbz} 1046223637Sbz 1047240233Sglebiusstatic void 1048223637Sbzpf_state_key_detach(struct pf_state *s, int idx) 1049223637Sbz{ 1050240233Sglebius struct pf_state_key *sk = s->key[idx]; 1051240233Sglebius#ifdef INVARIANTS 1052240233Sglebius struct pf_keyhash *kh = &V_pf_keyhash[pf_hashkey(sk)]; 1053223637Sbz 1054240233Sglebius PF_HASHROW_ASSERT(kh); 1055223637Sbz#endif 1056240233Sglebius TAILQ_REMOVE(&sk->states[idx], s, key_list[idx]); 1057240233Sglebius s->key[idx] = NULL; 1058223637Sbz 1059240233Sglebius if (TAILQ_EMPTY(&sk->states[0]) && TAILQ_EMPTY(&sk->states[1])) { 1060240233Sglebius LIST_REMOVE(sk, entry); 1061240233Sglebius uma_zfree(V_pf_state_key_z, sk); 1062223637Sbz } 1063223637Sbz} 1064223637Sbz 1065240233Sglebiusstatic int 1066240233Sglebiuspf_state_key_ctor(void *mem, int size, void *arg, int flags) 1067240233Sglebius{ 1068240233Sglebius struct pf_state_key *sk = mem; 1069240233Sglebius 1070240233Sglebius bzero(sk, sizeof(struct pf_state_key_cmp)); 1071240233Sglebius TAILQ_INIT(&sk->states[PF_SK_WIRE]); 1072240233Sglebius TAILQ_INIT(&sk->states[PF_SK_STACK]); 1073240233Sglebius 1074240233Sglebius return (0); 1075240233Sglebius} 1076240233Sglebius 1077223637Sbzstruct pf_state_key * 1078240233Sglebiuspf_state_key_setup(struct pf_pdesc *pd, struct pf_addr *saddr, 1079240233Sglebius struct pf_addr *daddr, u_int16_t sport, u_int16_t dport) 1080223637Sbz{ 1081240233Sglebius struct pf_state_key *sk; 1082223637Sbz 1083240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1084240233Sglebius if (sk == NULL) 1085223637Sbz return (NULL); 1086223637Sbz 1087240233Sglebius PF_ACPY(&sk->addr[pd->sidx], saddr, pd->af); 1088240233Sglebius PF_ACPY(&sk->addr[pd->didx], daddr, pd->af); 1089240233Sglebius sk->port[pd->sidx] = sport; 1090240233Sglebius sk->port[pd->didx] = dport; 1091240233Sglebius sk->proto = pd->proto; 1092240233Sglebius sk->af = pd->af; 1093240233Sglebius 1094223637Sbz return (sk); 1095223637Sbz} 1096223637Sbz 1097240233Sglebiusstruct pf_state_key * 1098240233Sglebiuspf_state_key_clone(struct pf_state_key *orig) 1099223637Sbz{ 1100240233Sglebius struct pf_state_key *sk; 1101223637Sbz 1102240233Sglebius sk = uma_zalloc(V_pf_state_key_z, M_NOWAIT); 1103240233Sglebius if (sk == NULL) 1104240233Sglebius return (NULL); 1105223637Sbz 1106240233Sglebius bcopy(orig, sk, sizeof(struct pf_state_key_cmp)); 1107223637Sbz 1108240233Sglebius return (sk); 1109223637Sbz} 1110223637Sbz 1111223637Sbzint 1112223637Sbzpf_state_insert(struct pfi_kif *kif, struct pf_state_key *skw, 1113223637Sbz struct pf_state_key *sks, struct pf_state *s) 1114223637Sbz{ 1115240233Sglebius struct pf_idhash *ih; 1116240233Sglebius struct pf_state *cur; 1117250522Sglebius int error; 1118223637Sbz 1119240233Sglebius KASSERT(TAILQ_EMPTY(&sks->states[0]) && TAILQ_EMPTY(&sks->states[1]), 1120240233Sglebius ("%s: sks not pristine", __func__)); 1121240233Sglebius KASSERT(TAILQ_EMPTY(&skw->states[0]) && TAILQ_EMPTY(&skw->states[1]), 1122240233Sglebius ("%s: skw not pristine", __func__)); 1123240233Sglebius KASSERT(s->refs == 0, ("%s: state not pristine", __func__)); 1124240233Sglebius 1125223637Sbz s->kif = kif; 1126223637Sbz 1127223637Sbz if (s->id == 0 && s->creatorid == 0) { 1128240233Sglebius /* XXX: should be atomic, but probability of collision low */ 1129240233Sglebius if ((s->id = V_pf_stateid[curcpu]++) == PFID_MAXID) 1130240233Sglebius V_pf_stateid[curcpu] = 1; 1131240233Sglebius s->id |= (uint64_t )curcpu << PFID_CPUSHIFT; 1132240233Sglebius s->id = htobe64(s->id); 1133223637Sbz s->creatorid = V_pf_status.hostid; 1134130613Smlaier } 1135240233Sglebius 1136251681Sglebius /* Returns with ID locked on success. */ 1137250522Sglebius if ((error = pf_state_key_attach(skw, sks, s)) != 0) 1138250522Sglebius return (error); 1139244184Sglebius 1140240233Sglebius ih = &V_pf_idhash[PF_IDHASH(s)]; 1141251681Sglebius PF_HASHROW_ASSERT(ih); 1142240233Sglebius LIST_FOREACH(cur, &ih->states, entry) 1143240233Sglebius if (cur->id == s->id && cur->creatorid == s->creatorid) 1144240233Sglebius break; 1145240233Sglebius 1146240233Sglebius if (cur != NULL) { 1147240233Sglebius PF_HASHROW_UNLOCK(ih); 1148223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 1149250521Sglebius printf("pf: state ID collision: " 1150250312Sglebius "id: %016llx creatorid: %08x\n", 1151240233Sglebius (unsigned long long)be64toh(s->id), 1152240233Sglebius ntohl(s->creatorid)); 1153130613Smlaier } 1154223637Sbz pf_detach_state(s); 1155250522Sglebius return (EEXIST); 1156130613Smlaier } 1157240233Sglebius LIST_INSERT_HEAD(&ih->states, s, entry); 1158240233Sglebius /* One for keys, one for ID hash. */ 1159240233Sglebius refcount_init(&s->refs, 2); 1160240233Sglebius 1161223637Sbz V_pf_status.fcounters[FCNT_STATE_INSERT]++; 1162223637Sbz if (pfsync_insert_state_ptr != NULL) 1163223637Sbz pfsync_insert_state_ptr(s); 1164240233Sglebius 1165240233Sglebius /* Returns locked. */ 1166126258Smlaier return (0); 1167126258Smlaier} 1168126258Smlaier 1169240233Sglebius/* 1170240233Sglebius * Find state by ID: returns with locked row on success. 1171240233Sglebius */ 1172223637Sbzstruct pf_state * 1173240233Sglebiuspf_find_state_byid(uint64_t id, uint32_t creatorid) 1174223637Sbz{ 1175240233Sglebius struct pf_idhash *ih; 1176240233Sglebius struct pf_state *s; 1177240233Sglebius 1178223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1179223637Sbz 1180240233Sglebius ih = &V_pf_idhash[(be64toh(id) % (V_pf_hashmask + 1))]; 1181223637Sbz 1182240233Sglebius PF_HASHROW_LOCK(ih); 1183240233Sglebius LIST_FOREACH(s, &ih->states, entry) 1184240233Sglebius if (s->id == id && s->creatorid == creatorid) 1185240233Sglebius break; 1186223637Sbz 1187240233Sglebius if (s == NULL) 1188240233Sglebius PF_HASHROW_UNLOCK(ih); 1189240233Sglebius 1190240233Sglebius return (s); 1191223637Sbz} 1192223637Sbz 1193240233Sglebius/* 1194240233Sglebius * Find state by key. 1195240233Sglebius * Returns with ID hash slot locked on success. 1196240233Sglebius */ 1197240233Sglebiusstatic struct pf_state * 1198240233Sglebiuspf_find_state(struct pfi_kif *kif, struct pf_state_key_cmp *key, u_int dir) 1199223637Sbz{ 1200240233Sglebius struct pf_keyhash *kh; 1201223637Sbz struct pf_state_key *sk; 1202240233Sglebius struct pf_state *s; 1203240233Sglebius int idx; 1204223637Sbz 1205223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1206223637Sbz 1207240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1208240233Sglebius 1209240233Sglebius PF_HASHROW_LOCK(kh); 1210240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1211240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1212240233Sglebius break; 1213240233Sglebius if (sk == NULL) { 1214240233Sglebius PF_HASHROW_UNLOCK(kh); 1215240233Sglebius return (NULL); 1216223637Sbz } 1217223637Sbz 1218240233Sglebius idx = (dir == PF_IN ? PF_SK_WIRE : PF_SK_STACK); 1219223637Sbz 1220240233Sglebius /* List is sorted, if-bound states before floating ones. */ 1221240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) 1222240233Sglebius if (s->kif == V_pfi_all || s->kif == kif) { 1223240233Sglebius PF_STATE_LOCK(s); 1224240233Sglebius PF_HASHROW_UNLOCK(kh); 1225240233Sglebius if (s->timeout == PFTM_UNLINKED) { 1226240233Sglebius /* 1227240233Sglebius * State is being processed 1228240233Sglebius * by pf_unlink_state() in 1229240233Sglebius * an other thread. 1230240233Sglebius */ 1231240233Sglebius PF_STATE_UNLOCK(s); 1232240233Sglebius return (NULL); 1233240233Sglebius } 1234240233Sglebius return (s); 1235240233Sglebius } 1236240233Sglebius PF_HASHROW_UNLOCK(kh); 1237223637Sbz 1238223637Sbz return (NULL); 1239223637Sbz} 1240223637Sbz 1241223637Sbzstruct pf_state * 1242223637Sbzpf_find_state_all(struct pf_state_key_cmp *key, u_int dir, int *more) 1243223637Sbz{ 1244240233Sglebius struct pf_keyhash *kh; 1245223637Sbz struct pf_state_key *sk; 1246240233Sglebius struct pf_state *s, *ret = NULL; 1247240233Sglebius int idx, inout = 0; 1248223637Sbz 1249223637Sbz V_pf_status.fcounters[FCNT_STATE_SEARCH]++; 1250223637Sbz 1251240233Sglebius kh = &V_pf_keyhash[pf_hashkey((struct pf_state_key *)key)]; 1252223637Sbz 1253240233Sglebius PF_HASHROW_LOCK(kh); 1254240233Sglebius LIST_FOREACH(sk, &kh->keys, entry) 1255240233Sglebius if (bcmp(sk, key, sizeof(struct pf_state_key_cmp)) == 0) 1256240233Sglebius break; 1257240233Sglebius if (sk == NULL) { 1258240233Sglebius PF_HASHROW_UNLOCK(kh); 1259240233Sglebius return (NULL); 1260223637Sbz } 1261240233Sglebius switch (dir) { 1262240233Sglebius case PF_IN: 1263240233Sglebius idx = PF_SK_WIRE; 1264240233Sglebius break; 1265240233Sglebius case PF_OUT: 1266240233Sglebius idx = PF_SK_STACK; 1267240233Sglebius break; 1268240233Sglebius case PF_INOUT: 1269240233Sglebius idx = PF_SK_WIRE; 1270240233Sglebius inout = 1; 1271240233Sglebius break; 1272240233Sglebius default: 1273240233Sglebius panic("%s: dir %u", __func__, dir); 1274240233Sglebius } 1275240233Sglebiussecond_run: 1276240233Sglebius TAILQ_FOREACH(s, &sk->states[idx], key_list[idx]) { 1277240233Sglebius if (more == NULL) { 1278240233Sglebius PF_HASHROW_UNLOCK(kh); 1279240233Sglebius return (s); 1280240233Sglebius } 1281240233Sglebius 1282240233Sglebius if (ret) 1283240233Sglebius (*more)++; 1284240233Sglebius else 1285240233Sglebius ret = s; 1286240233Sglebius } 1287240233Sglebius if (inout == 1) { 1288240233Sglebius inout = 0; 1289240233Sglebius idx = PF_SK_STACK; 1290240233Sglebius goto second_run; 1291240233Sglebius } 1292240233Sglebius PF_HASHROW_UNLOCK(kh); 1293240233Sglebius 1294240233Sglebius return (ret); 1295223637Sbz} 1296223637Sbz 1297223637Sbz/* END state table stuff */ 1298223637Sbz 1299240233Sglebiusstatic void 1300240233Sglebiuspf_send(struct pf_send_entry *pfse) 1301240233Sglebius{ 1302223637Sbz 1303240233Sglebius PF_SENDQ_LOCK(); 1304240233Sglebius STAILQ_INSERT_TAIL(&V_pf_sendqueue, pfse, pfse_next); 1305240233Sglebius PF_SENDQ_UNLOCK(); 1306240233Sglebius swi_sched(V_pf_swi_cookie, 0); 1307240233Sglebius} 1308240233Sglebius 1309126258Smlaiervoid 1310240233Sglebiuspf_intr(void *v) 1311240233Sglebius{ 1312240233Sglebius struct pf_send_head queue; 1313240233Sglebius struct pf_send_entry *pfse, *next; 1314240233Sglebius 1315240233Sglebius CURVNET_SET((struct vnet *)v); 1316240233Sglebius 1317240233Sglebius PF_SENDQ_LOCK(); 1318240233Sglebius queue = V_pf_sendqueue; 1319240233Sglebius STAILQ_INIT(&V_pf_sendqueue); 1320240233Sglebius PF_SENDQ_UNLOCK(); 1321240233Sglebius 1322240233Sglebius STAILQ_FOREACH_SAFE(pfse, &queue, pfse_next, next) { 1323240233Sglebius switch (pfse->pfse_type) { 1324240233Sglebius#ifdef INET 1325240233Sglebius case PFSE_IP: 1326240233Sglebius ip_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL); 1327240233Sglebius break; 1328240233Sglebius case PFSE_ICMP: 1329240233Sglebius icmp_error(pfse->pfse_m, pfse->pfse_icmp_type, 1330240233Sglebius pfse->pfse_icmp_code, 0, pfse->pfse_icmp_mtu); 1331240233Sglebius break; 1332240233Sglebius#endif /* INET */ 1333240233Sglebius#ifdef INET6 1334240233Sglebius case PFSE_IP6: 1335240233Sglebius ip6_output(pfse->pfse_m, NULL, NULL, 0, NULL, NULL, 1336240233Sglebius NULL); 1337240233Sglebius break; 1338240233Sglebius case PFSE_ICMP6: 1339240233Sglebius icmp6_error(pfse->pfse_m, pfse->pfse_icmp_type, 1340240233Sglebius pfse->pfse_icmp_code, pfse->pfse_icmp_mtu); 1341240233Sglebius break; 1342240233Sglebius#endif /* INET6 */ 1343240233Sglebius default: 1344240233Sglebius panic("%s: unknown type", __func__); 1345240233Sglebius } 1346240233Sglebius free(pfse, M_PFTEMP); 1347240233Sglebius } 1348240233Sglebius CURVNET_RESTORE(); 1349240233Sglebius} 1350240233Sglebius 1351240233Sglebiusvoid 1352171168Smlaierpf_purge_thread(void *v) 1353126258Smlaier{ 1354241039Sglebius u_int idx = 0; 1355171168Smlaier 1356223637Sbz CURVNET_SET((struct vnet *)v); 1357223637Sbz 1358171168Smlaier for (;;) { 1359240233Sglebius PF_RULES_RLOCK(); 1360240233Sglebius rw_sleep(pf_purge_thread, &pf_rules_lock, 0, "pftm", hz / 10); 1361171168Smlaier 1362226527Sbz if (V_pf_end_threads) { 1363240233Sglebius /* 1364240233Sglebius * To cleanse up all kifs and rules we need 1365240233Sglebius * two runs: first one clears reference flags, 1366240233Sglebius * then pf_purge_expired_states() doesn't 1367240233Sglebius * raise them, and then second run frees. 1368240233Sglebius */ 1369240233Sglebius PF_RULES_RUNLOCK(); 1370240233Sglebius pf_purge_unlinked_rules(); 1371240233Sglebius pfi_kif_purge(); 1372171168Smlaier 1373240233Sglebius /* 1374240233Sglebius * Now purge everything. 1375240233Sglebius */ 1376241039Sglebius pf_purge_expired_states(0, V_pf_hashmask); 1377226527Sbz pf_purge_expired_fragments(); 1378240233Sglebius pf_purge_expired_src_nodes(); 1379240233Sglebius 1380240233Sglebius /* 1381240233Sglebius * Now all kifs & rules should be unreferenced, 1382240233Sglebius * thus should be successfully freed. 1383240233Sglebius */ 1384240233Sglebius pf_purge_unlinked_rules(); 1385240233Sglebius pfi_kif_purge(); 1386240233Sglebius 1387240233Sglebius /* 1388240233Sglebius * Announce success and exit. 1389240233Sglebius */ 1390240233Sglebius PF_RULES_RLOCK(); 1391226527Sbz V_pf_end_threads++; 1392240233Sglebius PF_RULES_RUNLOCK(); 1393226527Sbz wakeup(pf_purge_thread); 1394226527Sbz kproc_exit(0); 1395226527Sbz } 1396240233Sglebius PF_RULES_RUNLOCK(); 1397126258Smlaier 1398240233Sglebius /* Process 1/interval fraction of the state table every run. */ 1399241039Sglebius idx = pf_purge_expired_states(idx, V_pf_hashmask / 1400240233Sglebius (V_pf_default_rule.timeout[PFTM_INTERVAL] * 10)); 1401196372Smlaier 1402240233Sglebius /* Purge other expired types every PFTM_INTERVAL seconds. */ 1403241039Sglebius if (idx == 0) { 1404240233Sglebius /* 1405240233Sglebius * Order is important: 1406240233Sglebius * - states and src nodes reference rules 1407240233Sglebius * - states and rules reference kifs 1408240233Sglebius */ 1409171168Smlaier pf_purge_expired_fragments(); 1410240233Sglebius pf_purge_expired_src_nodes(); 1411240233Sglebius pf_purge_unlinked_rules(); 1412240233Sglebius pfi_kif_purge(); 1413171168Smlaier } 1414171168Smlaier } 1415240233Sglebius /* not reached */ 1416223637Sbz CURVNET_RESTORE(); 1417126258Smlaier} 1418126258Smlaier 1419126258Smlaieru_int32_t 1420126258Smlaierpf_state_expires(const struct pf_state *state) 1421126258Smlaier{ 1422126258Smlaier u_int32_t timeout; 1423126258Smlaier u_int32_t start; 1424126258Smlaier u_int32_t end; 1425126258Smlaier u_int32_t states; 1426126258Smlaier 1427126258Smlaier /* handle all PFTM_* > PFTM_MAX here */ 1428126258Smlaier if (state->timeout == PFTM_PURGE) 1429240233Sglebius return (time_uptime); 1430126258Smlaier if (state->timeout == PFTM_UNTIL_PACKET) 1431126258Smlaier return (0); 1432171168Smlaier KASSERT(state->timeout != PFTM_UNLINKED, 1433171168Smlaier ("pf_state_expires: timeout == PFTM_UNLINKED")); 1434240233Sglebius KASSERT((state->timeout < PFTM_MAX), 1435126261Smlaier ("pf_state_expires: timeout > PFTM_MAX")); 1436126258Smlaier timeout = state->rule.ptr->timeout[state->timeout]; 1437126258Smlaier if (!timeout) 1438223637Sbz timeout = V_pf_default_rule.timeout[state->timeout]; 1439126258Smlaier start = state->rule.ptr->timeout[PFTM_ADAPTIVE_START]; 1440126258Smlaier if (start) { 1441126258Smlaier end = state->rule.ptr->timeout[PFTM_ADAPTIVE_END]; 1442240233Sglebius states = state->rule.ptr->states_cur; /* XXXGL */ 1443126258Smlaier } else { 1444223637Sbz start = V_pf_default_rule.timeout[PFTM_ADAPTIVE_START]; 1445223637Sbz end = V_pf_default_rule.timeout[PFTM_ADAPTIVE_END]; 1446223637Sbz states = V_pf_status.states; 1447126258Smlaier } 1448126258Smlaier if (end && states > start && start < end) { 1449126258Smlaier if (states < end) 1450126258Smlaier return (state->expire + timeout * (end - states) / 1451126258Smlaier (end - start)); 1452126258Smlaier else 1453240233Sglebius return (time_uptime); 1454126258Smlaier } 1455126258Smlaier return (state->expire + timeout); 1456126258Smlaier} 1457126258Smlaier 1458126258Smlaiervoid 1459240233Sglebiuspf_purge_expired_src_nodes() 1460126258Smlaier{ 1461240233Sglebius struct pf_srchash *sh; 1462240233Sglebius struct pf_src_node *cur, *next; 1463240233Sglebius int i; 1464126258Smlaier 1465240233Sglebius for (i = 0, sh = V_pf_srchash; i <= V_pf_srchashmask; i++, sh++) { 1466240233Sglebius PF_HASHROW_LOCK(sh); 1467240233Sglebius LIST_FOREACH_SAFE(cur, &sh->nodes, entry, next) 1468240233Sglebius if (cur->states <= 0 && cur->expire <= time_uptime) { 1469240233Sglebius if (cur->rule.ptr != NULL) 1470223637Sbz cur->rule.ptr->src_nodes--; 1471240233Sglebius LIST_REMOVE(cur, entry); 1472223637Sbz V_pf_status.scounters[SCNT_SRC_NODE_REMOVALS]++; 1473223637Sbz V_pf_status.src_nodes--; 1474240233Sglebius uma_zfree(V_pf_sources_z, cur); 1475240233Sglebius } else if (cur->rule.ptr != NULL) 1476240233Sglebius cur->rule.ptr->rule_flag |= PFRULE_REFS; 1477240233Sglebius PF_HASHROW_UNLOCK(sh); 1478223637Sbz } 1479130613Smlaier} 1480126258Smlaier 1481240233Sglebiusstatic void 1482130613Smlaierpf_src_tree_remove_state(struct pf_state *s) 1483130613Smlaier{ 1484130613Smlaier u_int32_t timeout; 1485126258Smlaier 1486130613Smlaier if (s->src_node != NULL) { 1487223637Sbz if (s->src.tcp_est) 1488223637Sbz --s->src_node->conn; 1489130613Smlaier if (--s->src_node->states <= 0) { 1490130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1491130613Smlaier if (!timeout) 1492130613Smlaier timeout = 1493223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1494240233Sglebius s->src_node->expire = time_uptime + timeout; 1495130613Smlaier } 1496130613Smlaier } 1497130613Smlaier if (s->nat_src_node != s->src_node && s->nat_src_node != NULL) { 1498130613Smlaier if (--s->nat_src_node->states <= 0) { 1499130613Smlaier timeout = s->rule.ptr->timeout[PFTM_SRC_NODE]; 1500130613Smlaier if (!timeout) 1501130613Smlaier timeout = 1502223637Sbz V_pf_default_rule.timeout[PFTM_SRC_NODE]; 1503240233Sglebius s->nat_src_node->expire = time_uptime + timeout; 1504130613Smlaier } 1505130613Smlaier } 1506130613Smlaier s->src_node = s->nat_src_node = NULL; 1507130613Smlaier} 1508126258Smlaier 1509240233Sglebius/* 1510240233Sglebius * Unlink and potentilly free a state. Function may be 1511240233Sglebius * called with ID hash row locked, but always returns 1512240233Sglebius * unlocked, since it needs to go through key hash locking. 1513240233Sglebius */ 1514240233Sglebiusint 1515240233Sglebiuspf_unlink_state(struct pf_state *s, u_int flags) 1516145836Smlaier{ 1517240233Sglebius struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; 1518223637Sbz 1519240233Sglebius if ((flags & PF_ENTER_LOCKED) == 0) 1520240233Sglebius PF_HASHROW_LOCK(ih); 1521240233Sglebius else 1522240233Sglebius PF_HASHROW_ASSERT(ih); 1523240233Sglebius 1524240233Sglebius if (s->timeout == PFTM_UNLINKED) { 1525240233Sglebius /* 1526240233Sglebius * State is being processed 1527240233Sglebius * by pf_unlink_state() in 1528240233Sglebius * an other thread. 1529240233Sglebius */ 1530240233Sglebius PF_HASHROW_UNLOCK(ih); 1531240233Sglebius return (0); /* XXXGL: undefined actually */ 1532240233Sglebius } 1533240233Sglebius 1534240233Sglebius if (s->src.state == PF_TCPS_PROXY_DST) { 1535223637Sbz /* XXX wire key the right one? */ 1536240233Sglebius pf_send_tcp(NULL, s->rule.ptr, s->key[PF_SK_WIRE]->af, 1537240233Sglebius &s->key[PF_SK_WIRE]->addr[1], 1538240233Sglebius &s->key[PF_SK_WIRE]->addr[0], 1539240233Sglebius s->key[PF_SK_WIRE]->port[1], 1540240233Sglebius s->key[PF_SK_WIRE]->port[0], 1541240233Sglebius s->src.seqhi, s->src.seqlo + 1, 1542240233Sglebius TH_RST|TH_ACK, 0, 0, 0, 1, s->tag, NULL); 1543171168Smlaier } 1544240233Sglebius 1545240233Sglebius LIST_REMOVE(s, entry); 1546240233Sglebius pf_src_tree_remove_state(s); 1547240233Sglebius 1548223637Sbz if (pfsync_delete_state_ptr != NULL) 1549240233Sglebius pfsync_delete_state_ptr(s); 1550240233Sglebius 1551243944Sglebius --s->rule.ptr->states_cur; 1552243944Sglebius if (s->nat_rule.ptr != NULL) 1553243944Sglebius --s->nat_rule.ptr->states_cur; 1554243944Sglebius if (s->anchor.ptr != NULL) 1555243944Sglebius --s->anchor.ptr->states_cur; 1556243944Sglebius 1557243941Sglebius s->timeout = PFTM_UNLINKED; 1558243941Sglebius 1559243941Sglebius PF_HASHROW_UNLOCK(ih); 1560243941Sglebius 1561240233Sglebius pf_detach_state(s); 1562240233Sglebius refcount_release(&s->refs); 1563240233Sglebius 1564240233Sglebius return (pf_release_state(s)); 1565171168Smlaier} 1566171168Smlaier 1567171168Smlaiervoid 1568171168Smlaierpf_free_state(struct pf_state *cur) 1569171168Smlaier{ 1570223637Sbz 1571240233Sglebius KASSERT(cur->refs == 0, ("%s: %p has refs", __func__, cur)); 1572240233Sglebius KASSERT(cur->timeout == PFTM_UNLINKED, ("%s: timeout %u", __func__, 1573240233Sglebius cur->timeout)); 1574243944Sglebius 1575145836Smlaier pf_normalize_tcp_cleanup(cur); 1576240233Sglebius uma_zfree(V_pf_state_z, cur); 1577223637Sbz V_pf_status.fcounters[FCNT_STATE_REMOVALS]++; 1578145836Smlaier} 1579145836Smlaier 1580240233Sglebius/* 1581240233Sglebius * Called only from pf_purge_thread(), thus serialized. 1582240233Sglebius */ 1583241039Sglebiusstatic u_int 1584241039Sglebiuspf_purge_expired_states(u_int i, int maxcheck) 1585130613Smlaier{ 1586240233Sglebius struct pf_idhash *ih; 1587240233Sglebius struct pf_state *s; 1588240233Sglebius 1589240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1590240233Sglebius 1591240233Sglebius /* 1592240233Sglebius * Go through hash and unlink states that expire now. 1593240233Sglebius */ 1594240233Sglebius while (maxcheck > 0) { 1595240233Sglebius 1596240233Sglebius ih = &V_pf_idhash[i]; 1597240233Sglebiusrelock: 1598240233Sglebius PF_HASHROW_LOCK(ih); 1599240233Sglebius LIST_FOREACH(s, &ih->states, entry) { 1600240233Sglebius if (pf_state_expires(s) <= time_uptime) { 1601240233Sglebius V_pf_status.states -= 1602240233Sglebius pf_unlink_state(s, PF_ENTER_LOCKED); 1603240233Sglebius goto relock; 1604171168Smlaier } 1605240233Sglebius s->rule.ptr->rule_flag |= PFRULE_REFS; 1606240233Sglebius if (s->nat_rule.ptr != NULL) 1607240233Sglebius s->nat_rule.ptr->rule_flag |= PFRULE_REFS; 1608240233Sglebius if (s->anchor.ptr != NULL) 1609240233Sglebius s->anchor.ptr->rule_flag |= PFRULE_REFS; 1610240233Sglebius s->kif->pfik_flags |= PFI_IFLAG_REFS; 1611240233Sglebius if (s->rt_kif) 1612240233Sglebius s->rt_kif->pfik_flags |= PFI_IFLAG_REFS; 1613171168Smlaier } 1614240233Sglebius PF_HASHROW_UNLOCK(ih); 1615241039Sglebius 1616241039Sglebius /* Return when we hit end of hash. */ 1617241039Sglebius if (++i > V_pf_hashmask) { 1618241039Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1619241039Sglebius return (0); 1620241039Sglebius } 1621241039Sglebius 1622240233Sglebius maxcheck--; 1623126258Smlaier } 1624171168Smlaier 1625240233Sglebius V_pf_status.states = uma_zone_get_cur(V_pf_state_z); 1626196372Smlaier 1627241039Sglebius return (i); 1628126258Smlaier} 1629126258Smlaier 1630240233Sglebiusstatic void 1631240233Sglebiuspf_purge_unlinked_rules() 1632126258Smlaier{ 1633240233Sglebius struct pf_rulequeue tmpq; 1634240233Sglebius struct pf_rule *r, *r1; 1635126258Smlaier 1636240233Sglebius /* 1637240811Sglebius * If we have overloading task pending, then we'd 1638240811Sglebius * better skip purging this time. There is a tiny 1639240811Sglebius * probability that overloading task references 1640240811Sglebius * an already unlinked rule. 1641240811Sglebius */ 1642240811Sglebius PF_OVERLOADQ_LOCK(); 1643240811Sglebius if (!SLIST_EMPTY(&V_pf_overloadqueue)) { 1644240811Sglebius PF_OVERLOADQ_UNLOCK(); 1645240811Sglebius return; 1646240811Sglebius } 1647240811Sglebius PF_OVERLOADQ_UNLOCK(); 1648240811Sglebius 1649240811Sglebius /* 1650240233Sglebius * Do naive mark-and-sweep garbage collecting of old rules. 1651240233Sglebius * Reference flag is raised by pf_purge_expired_states() 1652240233Sglebius * and pf_purge_expired_src_nodes(). 1653240233Sglebius * 1654240233Sglebius * To avoid LOR between PF_UNLNKDRULES_LOCK/PF_RULES_WLOCK, 1655240233Sglebius * use a temporary queue. 1656240233Sglebius */ 1657240233Sglebius TAILQ_INIT(&tmpq); 1658240233Sglebius PF_UNLNKDRULES_LOCK(); 1659240233Sglebius TAILQ_FOREACH_SAFE(r, &V_pf_unlinked_rules, entries, r1) { 1660240233Sglebius if (!(r->rule_flag & PFRULE_REFS)) { 1661240233Sglebius TAILQ_REMOVE(&V_pf_unlinked_rules, r, entries); 1662240233Sglebius TAILQ_INSERT_TAIL(&tmpq, r, entries); 1663240233Sglebius } else 1664240233Sglebius r->rule_flag &= ~PFRULE_REFS; 1665240233Sglebius } 1666240233Sglebius PF_UNLNKDRULES_UNLOCK(); 1667126258Smlaier 1668240233Sglebius if (!TAILQ_EMPTY(&tmpq)) { 1669240233Sglebius PF_RULES_WLOCK(); 1670240233Sglebius TAILQ_FOREACH_SAFE(r, &tmpq, entries, r1) { 1671240233Sglebius TAILQ_REMOVE(&tmpq, r, entries); 1672240233Sglebius pf_free_rule(r); 1673240233Sglebius } 1674240233Sglebius PF_RULES_WUNLOCK(); 1675240233Sglebius } 1676126258Smlaier} 1677126258Smlaier 1678126258Smlaiervoid 1679126258Smlaierpf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) 1680126258Smlaier{ 1681126258Smlaier switch (af) { 1682126258Smlaier#ifdef INET 1683126258Smlaier case AF_INET: { 1684126258Smlaier u_int32_t a = ntohl(addr->addr32[0]); 1685126258Smlaier printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, 1686126258Smlaier (a>>8)&255, a&255); 1687126258Smlaier if (p) { 1688126258Smlaier p = ntohs(p); 1689126258Smlaier printf(":%u", p); 1690126258Smlaier } 1691126258Smlaier break; 1692126258Smlaier } 1693126258Smlaier#endif /* INET */ 1694126258Smlaier#ifdef INET6 1695126258Smlaier case AF_INET6: { 1696126258Smlaier u_int16_t b; 1697223637Sbz u_int8_t i, curstart, curend, maxstart, maxend; 1698223637Sbz curstart = curend = maxstart = maxend = 255; 1699126258Smlaier for (i = 0; i < 8; i++) { 1700126258Smlaier if (!addr->addr16[i]) { 1701126258Smlaier if (curstart == 255) 1702126258Smlaier curstart = i; 1703223637Sbz curend = i; 1704126258Smlaier } else { 1705223637Sbz if ((curend - curstart) > 1706223637Sbz (maxend - maxstart)) { 1707223637Sbz maxstart = curstart; 1708223637Sbz maxend = curend; 1709126258Smlaier } 1710223637Sbz curstart = curend = 255; 1711126258Smlaier } 1712126258Smlaier } 1713223637Sbz if ((curend - curstart) > 1714223637Sbz (maxend - maxstart)) { 1715223637Sbz maxstart = curstart; 1716223637Sbz maxend = curend; 1717223637Sbz } 1718126258Smlaier for (i = 0; i < 8; i++) { 1719126258Smlaier if (i >= maxstart && i <= maxend) { 1720223637Sbz if (i == 0) 1721223637Sbz printf(":"); 1722223637Sbz if (i == maxend) 1723223637Sbz printf(":"); 1724126258Smlaier } else { 1725126258Smlaier b = ntohs(addr->addr16[i]); 1726126258Smlaier printf("%x", b); 1727126258Smlaier if (i < 7) 1728126258Smlaier printf(":"); 1729126258Smlaier } 1730126258Smlaier } 1731126258Smlaier if (p) { 1732126258Smlaier p = ntohs(p); 1733126258Smlaier printf("[%u]", p); 1734126258Smlaier } 1735126258Smlaier break; 1736126258Smlaier } 1737126258Smlaier#endif /* INET6 */ 1738126258Smlaier } 1739126258Smlaier} 1740126258Smlaier 1741126258Smlaiervoid 1742126258Smlaierpf_print_state(struct pf_state *s) 1743126258Smlaier{ 1744223637Sbz pf_print_state_parts(s, NULL, NULL); 1745223637Sbz} 1746223637Sbz 1747240233Sglebiusstatic void 1748223637Sbzpf_print_state_parts(struct pf_state *s, 1749223637Sbz struct pf_state_key *skwp, struct pf_state_key *sksp) 1750223637Sbz{ 1751223637Sbz struct pf_state_key *skw, *sks; 1752223637Sbz u_int8_t proto, dir; 1753223637Sbz 1754223637Sbz /* Do our best to fill these, but they're skipped if NULL */ 1755223637Sbz skw = skwp ? skwp : (s ? s->key[PF_SK_WIRE] : NULL); 1756223637Sbz sks = sksp ? sksp : (s ? s->key[PF_SK_STACK] : NULL); 1757223637Sbz proto = skw ? skw->proto : (sks ? sks->proto : 0); 1758223637Sbz dir = s ? s->direction : 0; 1759223637Sbz 1760223637Sbz switch (proto) { 1761223637Sbz case IPPROTO_IPV4: 1762223637Sbz printf("IPv4"); 1763223637Sbz break; 1764223637Sbz case IPPROTO_IPV6: 1765223637Sbz printf("IPv6"); 1766223637Sbz break; 1767126258Smlaier case IPPROTO_TCP: 1768223637Sbz printf("TCP"); 1769126258Smlaier break; 1770126258Smlaier case IPPROTO_UDP: 1771223637Sbz printf("UDP"); 1772126258Smlaier break; 1773126258Smlaier case IPPROTO_ICMP: 1774223637Sbz printf("ICMP"); 1775126258Smlaier break; 1776126258Smlaier case IPPROTO_ICMPV6: 1777223637Sbz printf("ICMPv6"); 1778126258Smlaier break; 1779126258Smlaier default: 1780223637Sbz printf("%u", skw->proto); 1781126258Smlaier break; 1782126258Smlaier } 1783223637Sbz switch (dir) { 1784223637Sbz case PF_IN: 1785223637Sbz printf(" in"); 1786223637Sbz break; 1787223637Sbz case PF_OUT: 1788223637Sbz printf(" out"); 1789223637Sbz break; 1790223637Sbz } 1791223637Sbz if (skw) { 1792223637Sbz printf(" wire: "); 1793223637Sbz pf_print_host(&skw->addr[0], skw->port[0], skw->af); 1794223637Sbz printf(" "); 1795223637Sbz pf_print_host(&skw->addr[1], skw->port[1], skw->af); 1796223637Sbz } 1797223637Sbz if (sks) { 1798223637Sbz printf(" stack: "); 1799223637Sbz if (sks != skw) { 1800223637Sbz pf_print_host(&sks->addr[0], sks->port[0], sks->af); 1801223637Sbz printf(" "); 1802223637Sbz pf_print_host(&sks->addr[1], sks->port[1], sks->af); 1803223637Sbz } else 1804223637Sbz printf("-"); 1805223637Sbz } 1806223637Sbz if (s) { 1807223637Sbz if (proto == IPPROTO_TCP) { 1808223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1809223637Sbz s->src.seqlo, s->src.seqhi, 1810223637Sbz s->src.max_win, s->src.seqdiff); 1811223637Sbz if (s->src.wscale && s->dst.wscale) 1812223637Sbz printf(" wscale=%u", 1813223637Sbz s->src.wscale & PF_WSCALE_MASK); 1814223637Sbz printf("]"); 1815223637Sbz printf(" [lo=%u high=%u win=%u modulator=%u", 1816223637Sbz s->dst.seqlo, s->dst.seqhi, 1817223637Sbz s->dst.max_win, s->dst.seqdiff); 1818223637Sbz if (s->src.wscale && s->dst.wscale) 1819223637Sbz printf(" wscale=%u", 1820223637Sbz s->dst.wscale & PF_WSCALE_MASK); 1821223637Sbz printf("]"); 1822223637Sbz } 1823223637Sbz printf(" %u:%u", s->src.state, s->dst.state); 1824223637Sbz } 1825126258Smlaier} 1826126258Smlaier 1827126258Smlaiervoid 1828126258Smlaierpf_print_flags(u_int8_t f) 1829126258Smlaier{ 1830126258Smlaier if (f) 1831126258Smlaier printf(" "); 1832126258Smlaier if (f & TH_FIN) 1833126258Smlaier printf("F"); 1834126258Smlaier if (f & TH_SYN) 1835126258Smlaier printf("S"); 1836126258Smlaier if (f & TH_RST) 1837126258Smlaier printf("R"); 1838126258Smlaier if (f & TH_PUSH) 1839126258Smlaier printf("P"); 1840126258Smlaier if (f & TH_ACK) 1841126258Smlaier printf("A"); 1842126258Smlaier if (f & TH_URG) 1843126258Smlaier printf("U"); 1844126258Smlaier if (f & TH_ECE) 1845126258Smlaier printf("E"); 1846126258Smlaier if (f & TH_CWR) 1847126258Smlaier printf("W"); 1848126258Smlaier} 1849126258Smlaier 1850126258Smlaier#define PF_SET_SKIP_STEPS(i) \ 1851126258Smlaier do { \ 1852126258Smlaier while (head[i] != cur) { \ 1853126258Smlaier head[i]->skip[i].ptr = cur; \ 1854126258Smlaier head[i] = TAILQ_NEXT(head[i], entries); \ 1855126258Smlaier } \ 1856126258Smlaier } while (0) 1857126258Smlaier 1858126258Smlaiervoid 1859126258Smlaierpf_calc_skip_steps(struct pf_rulequeue *rules) 1860126258Smlaier{ 1861126258Smlaier struct pf_rule *cur, *prev, *head[PF_SKIP_COUNT]; 1862126258Smlaier int i; 1863126258Smlaier 1864126258Smlaier cur = TAILQ_FIRST(rules); 1865126258Smlaier prev = cur; 1866126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1867126258Smlaier head[i] = cur; 1868126258Smlaier while (cur != NULL) { 1869126258Smlaier 1870130613Smlaier if (cur->kif != prev->kif || cur->ifnot != prev->ifnot) 1871126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_IFP); 1872126258Smlaier if (cur->direction != prev->direction) 1873126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DIR); 1874126258Smlaier if (cur->af != prev->af) 1875126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_AF); 1876126258Smlaier if (cur->proto != prev->proto) 1877126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_PROTO); 1878145836Smlaier if (cur->src.neg != prev->src.neg || 1879126258Smlaier pf_addr_wrap_neq(&cur->src.addr, &prev->src.addr)) 1880126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_ADDR); 1881126258Smlaier if (cur->src.port[0] != prev->src.port[0] || 1882126258Smlaier cur->src.port[1] != prev->src.port[1] || 1883126258Smlaier cur->src.port_op != prev->src.port_op) 1884126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_SRC_PORT); 1885145836Smlaier if (cur->dst.neg != prev->dst.neg || 1886126258Smlaier pf_addr_wrap_neq(&cur->dst.addr, &prev->dst.addr)) 1887126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_ADDR); 1888126258Smlaier if (cur->dst.port[0] != prev->dst.port[0] || 1889126258Smlaier cur->dst.port[1] != prev->dst.port[1] || 1890126258Smlaier cur->dst.port_op != prev->dst.port_op) 1891126258Smlaier PF_SET_SKIP_STEPS(PF_SKIP_DST_PORT); 1892126258Smlaier 1893126258Smlaier prev = cur; 1894126258Smlaier cur = TAILQ_NEXT(cur, entries); 1895126258Smlaier } 1896126258Smlaier for (i = 0; i < PF_SKIP_COUNT; ++i) 1897126258Smlaier PF_SET_SKIP_STEPS(i); 1898126258Smlaier} 1899126258Smlaier 1900240233Sglebiusstatic int 1901126258Smlaierpf_addr_wrap_neq(struct pf_addr_wrap *aw1, struct pf_addr_wrap *aw2) 1902126258Smlaier{ 1903126258Smlaier if (aw1->type != aw2->type) 1904126258Smlaier return (1); 1905126258Smlaier switch (aw1->type) { 1906126258Smlaier case PF_ADDR_ADDRMASK: 1907223637Sbz case PF_ADDR_RANGE: 1908126258Smlaier if (PF_ANEQ(&aw1->v.a.addr, &aw2->v.a.addr, 0)) 1909126258Smlaier return (1); 1910126258Smlaier if (PF_ANEQ(&aw1->v.a.mask, &aw2->v.a.mask, 0)) 1911126258Smlaier return (1); 1912126258Smlaier return (0); 1913126258Smlaier case PF_ADDR_DYNIFTL: 1914130613Smlaier return (aw1->p.dyn->pfid_kt != aw2->p.dyn->pfid_kt); 1915126258Smlaier case PF_ADDR_NOROUTE: 1916171168Smlaier case PF_ADDR_URPFFAILED: 1917126258Smlaier return (0); 1918126258Smlaier case PF_ADDR_TABLE: 1919126258Smlaier return (aw1->p.tbl != aw2->p.tbl); 1920126258Smlaier default: 1921126258Smlaier printf("invalid address type: %d\n", aw1->type); 1922126258Smlaier return (1); 1923126258Smlaier } 1924126258Smlaier} 1925126258Smlaier 1926126258Smlaieru_int16_t 1927126258Smlaierpf_cksum_fixup(u_int16_t cksum, u_int16_t old, u_int16_t new, u_int8_t udp) 1928126258Smlaier{ 1929126258Smlaier u_int32_t l; 1930126258Smlaier 1931126258Smlaier if (udp && !cksum) 1932126258Smlaier return (0x0000); 1933126258Smlaier l = cksum + old - new; 1934126258Smlaier l = (l >> 16) + (l & 65535); 1935126258Smlaier l = l & 65535; 1936126258Smlaier if (udp && !l) 1937126258Smlaier return (0xFFFF); 1938126258Smlaier return (l); 1939126258Smlaier} 1940126258Smlaier 1941240233Sglebiusstatic void 1942126258Smlaierpf_change_ap(struct pf_addr *a, u_int16_t *p, u_int16_t *ic, u_int16_t *pc, 1943126258Smlaier struct pf_addr *an, u_int16_t pn, u_int8_t u, sa_family_t af) 1944126258Smlaier{ 1945126258Smlaier struct pf_addr ao; 1946126258Smlaier u_int16_t po = *p; 1947126258Smlaier 1948126258Smlaier PF_ACPY(&ao, a, af); 1949126258Smlaier PF_ACPY(a, an, af); 1950126258Smlaier 1951126258Smlaier *p = pn; 1952126258Smlaier 1953126258Smlaier switch (af) { 1954126258Smlaier#ifdef INET 1955126258Smlaier case AF_INET: 1956126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 1957126258Smlaier ao.addr16[0], an->addr16[0], 0), 1958126258Smlaier ao.addr16[1], an->addr16[1], 0); 1959126258Smlaier *p = pn; 1960126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 1961126258Smlaier ao.addr16[0], an->addr16[0], u), 1962126258Smlaier ao.addr16[1], an->addr16[1], u), 1963126258Smlaier po, pn, u); 1964126258Smlaier break; 1965126258Smlaier#endif /* INET */ 1966126258Smlaier#ifdef INET6 1967126258Smlaier case AF_INET6: 1968126258Smlaier *pc = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 1969126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 1970126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup(*pc, 1971126258Smlaier ao.addr16[0], an->addr16[0], u), 1972126258Smlaier ao.addr16[1], an->addr16[1], u), 1973126258Smlaier ao.addr16[2], an->addr16[2], u), 1974126258Smlaier ao.addr16[3], an->addr16[3], u), 1975126258Smlaier ao.addr16[4], an->addr16[4], u), 1976126258Smlaier ao.addr16[5], an->addr16[5], u), 1977126258Smlaier ao.addr16[6], an->addr16[6], u), 1978126258Smlaier ao.addr16[7], an->addr16[7], u), 1979126258Smlaier po, pn, u); 1980126258Smlaier break; 1981126258Smlaier#endif /* INET6 */ 1982126258Smlaier } 1983126258Smlaier} 1984126258Smlaier 1985126258Smlaier 1986126258Smlaier/* Changes a u_int32_t. Uses a void * so there are no align restrictions */ 1987126258Smlaiervoid 1988126258Smlaierpf_change_a(void *a, u_int16_t *c, u_int32_t an, u_int8_t u) 1989126258Smlaier{ 1990126258Smlaier u_int32_t ao; 1991126258Smlaier 1992126258Smlaier memcpy(&ao, a, sizeof(ao)); 1993126258Smlaier memcpy(a, &an, sizeof(u_int32_t)); 1994126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(*c, ao / 65536, an / 65536, u), 1995126258Smlaier ao % 65536, an % 65536, u); 1996126258Smlaier} 1997126258Smlaier 1998126258Smlaier#ifdef INET6 1999240233Sglebiusstatic void 2000126258Smlaierpf_change_a6(struct pf_addr *a, u_int16_t *c, struct pf_addr *an, u_int8_t u) 2001126258Smlaier{ 2002126258Smlaier struct pf_addr ao; 2003126258Smlaier 2004126258Smlaier PF_ACPY(&ao, a, AF_INET6); 2005126258Smlaier PF_ACPY(a, an, AF_INET6); 2006126258Smlaier 2007126258Smlaier *c = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2008126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2009126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*c, 2010126258Smlaier ao.addr16[0], an->addr16[0], u), 2011126258Smlaier ao.addr16[1], an->addr16[1], u), 2012126258Smlaier ao.addr16[2], an->addr16[2], u), 2013126258Smlaier ao.addr16[3], an->addr16[3], u), 2014126258Smlaier ao.addr16[4], an->addr16[4], u), 2015126258Smlaier ao.addr16[5], an->addr16[5], u), 2016126258Smlaier ao.addr16[6], an->addr16[6], u), 2017126258Smlaier ao.addr16[7], an->addr16[7], u); 2018126258Smlaier} 2019126258Smlaier#endif /* INET6 */ 2020126258Smlaier 2021240233Sglebiusstatic void 2022126258Smlaierpf_change_icmp(struct pf_addr *ia, u_int16_t *ip, struct pf_addr *oa, 2023126258Smlaier struct pf_addr *na, u_int16_t np, u_int16_t *pc, u_int16_t *h2c, 2024126258Smlaier u_int16_t *ic, u_int16_t *hc, u_int8_t u, sa_family_t af) 2025126258Smlaier{ 2026126258Smlaier struct pf_addr oia, ooa; 2027126258Smlaier 2028126258Smlaier PF_ACPY(&oia, ia, af); 2029223637Sbz if (oa) 2030223637Sbz PF_ACPY(&ooa, oa, af); 2031126258Smlaier 2032126258Smlaier /* Change inner protocol port, fix inner protocol checksum. */ 2033126258Smlaier if (ip != NULL) { 2034126258Smlaier u_int16_t oip = *ip; 2035223637Sbz u_int32_t opc; 2036126258Smlaier 2037126258Smlaier if (pc != NULL) 2038126258Smlaier opc = *pc; 2039126258Smlaier *ip = np; 2040126258Smlaier if (pc != NULL) 2041126258Smlaier *pc = pf_cksum_fixup(*pc, oip, *ip, u); 2042126258Smlaier *ic = pf_cksum_fixup(*ic, oip, *ip, 0); 2043126258Smlaier if (pc != NULL) 2044126258Smlaier *ic = pf_cksum_fixup(*ic, opc, *pc, 0); 2045126258Smlaier } 2046126258Smlaier /* Change inner ip address, fix inner ip and icmp checksums. */ 2047126258Smlaier PF_ACPY(ia, na, af); 2048126258Smlaier switch (af) { 2049126258Smlaier#ifdef INET 2050126258Smlaier case AF_INET: { 2051126258Smlaier u_int32_t oh2c = *h2c; 2052126258Smlaier 2053126258Smlaier *h2c = pf_cksum_fixup(pf_cksum_fixup(*h2c, 2054126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2055126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2056126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(*ic, 2057126258Smlaier oia.addr16[0], ia->addr16[0], 0), 2058126258Smlaier oia.addr16[1], ia->addr16[1], 0); 2059126258Smlaier *ic = pf_cksum_fixup(*ic, oh2c, *h2c, 0); 2060126258Smlaier break; 2061126258Smlaier } 2062126258Smlaier#endif /* INET */ 2063126258Smlaier#ifdef INET6 2064126258Smlaier case AF_INET6: 2065126258Smlaier *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2066126258Smlaier pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2067126258Smlaier pf_cksum_fixup(pf_cksum_fixup(*ic, 2068126258Smlaier oia.addr16[0], ia->addr16[0], u), 2069126258Smlaier oia.addr16[1], ia->addr16[1], u), 2070126258Smlaier oia.addr16[2], ia->addr16[2], u), 2071126258Smlaier oia.addr16[3], ia->addr16[3], u), 2072126258Smlaier oia.addr16[4], ia->addr16[4], u), 2073126258Smlaier oia.addr16[5], ia->addr16[5], u), 2074126258Smlaier oia.addr16[6], ia->addr16[6], u), 2075126258Smlaier oia.addr16[7], ia->addr16[7], u); 2076126258Smlaier break; 2077126258Smlaier#endif /* INET6 */ 2078126258Smlaier } 2079223637Sbz /* Outer ip address, fix outer ip or icmpv6 checksum, if necessary. */ 2080223637Sbz if (oa) { 2081223637Sbz PF_ACPY(oa, na, af); 2082223637Sbz switch (af) { 2083126258Smlaier#ifdef INET 2084223637Sbz case AF_INET: 2085223637Sbz *hc = pf_cksum_fixup(pf_cksum_fixup(*hc, 2086223637Sbz ooa.addr16[0], oa->addr16[0], 0), 2087223637Sbz ooa.addr16[1], oa->addr16[1], 0); 2088223637Sbz break; 2089126258Smlaier#endif /* INET */ 2090126258Smlaier#ifdef INET6 2091223637Sbz case AF_INET6: 2092223637Sbz *ic = pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2093223637Sbz pf_cksum_fixup(pf_cksum_fixup(pf_cksum_fixup( 2094223637Sbz pf_cksum_fixup(pf_cksum_fixup(*ic, 2095223637Sbz ooa.addr16[0], oa->addr16[0], u), 2096223637Sbz ooa.addr16[1], oa->addr16[1], u), 2097223637Sbz ooa.addr16[2], oa->addr16[2], u), 2098223637Sbz ooa.addr16[3], oa->addr16[3], u), 2099223637Sbz ooa.addr16[4], oa->addr16[4], u), 2100223637Sbz ooa.addr16[5], oa->addr16[5], u), 2101223637Sbz ooa.addr16[6], oa->addr16[6], u), 2102223637Sbz ooa.addr16[7], oa->addr16[7], u); 2103223637Sbz break; 2104126258Smlaier#endif /* INET6 */ 2105223637Sbz } 2106126258Smlaier } 2107126258Smlaier} 2108126258Smlaier 2109171168Smlaier 2110171168Smlaier/* 2111171168Smlaier * Need to modulate the sequence numbers in the TCP SACK option 2112171168Smlaier * (credits to Krzysztof Pfaff for report and patch) 2113171168Smlaier */ 2114240233Sglebiusstatic int 2115171168Smlaierpf_modulate_sack(struct mbuf *m, int off, struct pf_pdesc *pd, 2116171168Smlaier struct tcphdr *th, struct pf_state_peer *dst) 2117171168Smlaier{ 2118171168Smlaier int hlen = (th->th_off << 2) - sizeof(*th), thoptlen = hlen; 2119171168Smlaier u_int8_t opts[TCP_MAXOLEN], *opt = opts; 2120171168Smlaier int copyback = 0, i, olen; 2121171168Smlaier struct sackblk sack; 2122171168Smlaier 2123223637Sbz#define TCPOLEN_SACKLEN (TCPOLEN_SACK + 2) 2124171168Smlaier if (hlen < TCPOLEN_SACKLEN || 2125171168Smlaier !pf_pull_hdr(m, off + sizeof(*th), opts, hlen, NULL, NULL, pd->af)) 2126171168Smlaier return 0; 2127171168Smlaier 2128171168Smlaier while (hlen >= TCPOLEN_SACKLEN) { 2129171168Smlaier olen = opt[1]; 2130171168Smlaier switch (*opt) { 2131171168Smlaier case TCPOPT_EOL: /* FALLTHROUGH */ 2132171168Smlaier case TCPOPT_NOP: 2133171168Smlaier opt++; 2134171168Smlaier hlen--; 2135171168Smlaier break; 2136171168Smlaier case TCPOPT_SACK: 2137171168Smlaier if (olen > hlen) 2138171168Smlaier olen = hlen; 2139171168Smlaier if (olen >= TCPOLEN_SACKLEN) { 2140171168Smlaier for (i = 2; i + TCPOLEN_SACK <= olen; 2141171168Smlaier i += TCPOLEN_SACK) { 2142171168Smlaier memcpy(&sack, &opt[i], sizeof(sack)); 2143171168Smlaier pf_change_a(&sack.start, &th->th_sum, 2144171168Smlaier htonl(ntohl(sack.start) - 2145171168Smlaier dst->seqdiff), 0); 2146171168Smlaier pf_change_a(&sack.end, &th->th_sum, 2147171168Smlaier htonl(ntohl(sack.end) - 2148171168Smlaier dst->seqdiff), 0); 2149171168Smlaier memcpy(&opt[i], &sack, sizeof(sack)); 2150171168Smlaier } 2151171168Smlaier copyback = 1; 2152171168Smlaier } 2153171168Smlaier /* FALLTHROUGH */ 2154171168Smlaier default: 2155171168Smlaier if (olen < 2) 2156171168Smlaier olen = 2; 2157171168Smlaier hlen -= olen; 2158171168Smlaier opt += olen; 2159171168Smlaier } 2160171168Smlaier } 2161171168Smlaier 2162171168Smlaier if (copyback) 2163171168Smlaier m_copyback(m, off + sizeof(*th), thoptlen, (caddr_t)opts); 2164171168Smlaier return (copyback); 2165171168Smlaier} 2166171168Smlaier 2167240233Sglebiusstatic void 2168162238Scsjppf_send_tcp(struct mbuf *replyto, const struct pf_rule *r, sa_family_t af, 2169126258Smlaier const struct pf_addr *saddr, const struct pf_addr *daddr, 2170126258Smlaier u_int16_t sport, u_int16_t dport, u_int32_t seq, u_int32_t ack, 2171145836Smlaier u_int8_t flags, u_int16_t win, u_int16_t mss, u_int8_t ttl, int tag, 2172240233Sglebius u_int16_t rtag, struct ifnet *ifp) 2173126258Smlaier{ 2174240233Sglebius struct pf_send_entry *pfse; 2175126258Smlaier struct mbuf *m; 2176171168Smlaier int len, tlen; 2177126258Smlaier#ifdef INET 2178240233Sglebius struct ip *h = NULL; 2179126258Smlaier#endif /* INET */ 2180126258Smlaier#ifdef INET6 2181240233Sglebius struct ip6_hdr *h6 = NULL; 2182126258Smlaier#endif /* INET6 */ 2183171168Smlaier struct tcphdr *th; 2184171168Smlaier char *opt; 2185223637Sbz struct pf_mtag *pf_mtag; 2186126258Smlaier 2187171168Smlaier len = 0; 2188171168Smlaier th = NULL; 2189171168Smlaier 2190126258Smlaier /* maximum segment size tcp option */ 2191126258Smlaier tlen = sizeof(struct tcphdr); 2192126258Smlaier if (mss) 2193126258Smlaier tlen += 4; 2194126258Smlaier 2195126258Smlaier switch (af) { 2196126258Smlaier#ifdef INET 2197126258Smlaier case AF_INET: 2198126258Smlaier len = sizeof(struct ip) + tlen; 2199126258Smlaier break; 2200126258Smlaier#endif /* INET */ 2201126258Smlaier#ifdef INET6 2202126258Smlaier case AF_INET6: 2203126258Smlaier len = sizeof(struct ip6_hdr) + tlen; 2204126258Smlaier break; 2205126258Smlaier#endif /* INET6 */ 2206240233Sglebius default: 2207240233Sglebius panic("%s: unsupported af %d", __func__, af); 2208126258Smlaier } 2209126258Smlaier 2210240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2211240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2212240233Sglebius if (pfse == NULL) 2213132280Smlaier return; 2214248324Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 2215240233Sglebius if (m == NULL) { 2216240233Sglebius free(pfse, M_PFTEMP); 2217240233Sglebius return; 2218240233Sglebius } 2219162238Scsjp#ifdef MAC 2220223637Sbz mac_netinet_firewall_send(m); 2221162238Scsjp#endif 2222171168Smlaier if ((pf_mtag = pf_get_mtag(m)) == NULL) { 2223240233Sglebius free(pfse, M_PFTEMP); 2224171168Smlaier m_freem(m); 2225171168Smlaier return; 2226171168Smlaier } 2227171168Smlaier if (tag) 2228145836Smlaier m->m_flags |= M_SKIP_FIREWALL; 2229223637Sbz pf_mtag->tag = rtag; 2230145836Smlaier 2231171168Smlaier if (r != NULL && r->rtableid >= 0) 2232178888Sjulian M_SETFIB(m, r->rtableid); 2233223637Sbz 2234126258Smlaier#ifdef ALTQ 2235126258Smlaier if (r != NULL && r->qid) { 2236171168Smlaier pf_mtag->qid = r->qid; 2237223637Sbz 2238171168Smlaier /* add hints for ecn */ 2239171168Smlaier pf_mtag->hdr = mtod(m, struct ip *); 2240126258Smlaier } 2241145836Smlaier#endif /* ALTQ */ 2242126258Smlaier m->m_data += max_linkhdr; 2243126258Smlaier m->m_pkthdr.len = m->m_len = len; 2244126258Smlaier m->m_pkthdr.rcvif = NULL; 2245126258Smlaier bzero(m->m_data, len); 2246126258Smlaier switch (af) { 2247126258Smlaier#ifdef INET 2248126258Smlaier case AF_INET: 2249126258Smlaier h = mtod(m, struct ip *); 2250126258Smlaier 2251126258Smlaier /* IP header fields included in the TCP checksum */ 2252126258Smlaier h->ip_p = IPPROTO_TCP; 2253126258Smlaier h->ip_len = htons(tlen); 2254126258Smlaier h->ip_src.s_addr = saddr->v4.s_addr; 2255126258Smlaier h->ip_dst.s_addr = daddr->v4.s_addr; 2256126258Smlaier 2257126258Smlaier th = (struct tcphdr *)((caddr_t)h + sizeof(struct ip)); 2258126258Smlaier break; 2259126258Smlaier#endif /* INET */ 2260126258Smlaier#ifdef INET6 2261126258Smlaier case AF_INET6: 2262126258Smlaier h6 = mtod(m, struct ip6_hdr *); 2263126258Smlaier 2264126258Smlaier /* IP header fields included in the TCP checksum */ 2265126258Smlaier h6->ip6_nxt = IPPROTO_TCP; 2266126258Smlaier h6->ip6_plen = htons(tlen); 2267126258Smlaier memcpy(&h6->ip6_src, &saddr->v6, sizeof(struct in6_addr)); 2268126258Smlaier memcpy(&h6->ip6_dst, &daddr->v6, sizeof(struct in6_addr)); 2269126258Smlaier 2270126258Smlaier th = (struct tcphdr *)((caddr_t)h6 + sizeof(struct ip6_hdr)); 2271126258Smlaier break; 2272126258Smlaier#endif /* INET6 */ 2273126258Smlaier } 2274126258Smlaier 2275126258Smlaier /* TCP header */ 2276126258Smlaier th->th_sport = sport; 2277126258Smlaier th->th_dport = dport; 2278126258Smlaier th->th_seq = htonl(seq); 2279126258Smlaier th->th_ack = htonl(ack); 2280126258Smlaier th->th_off = tlen >> 2; 2281126258Smlaier th->th_flags = flags; 2282126258Smlaier th->th_win = htons(win); 2283126258Smlaier 2284126258Smlaier if (mss) { 2285126258Smlaier opt = (char *)(th + 1); 2286126258Smlaier opt[0] = TCPOPT_MAXSEG; 2287126258Smlaier opt[1] = 4; 2288126258Smlaier HTONS(mss); 2289126258Smlaier bcopy((caddr_t)&mss, (caddr_t)(opt + 2), 2); 2290126258Smlaier } 2291126258Smlaier 2292126258Smlaier switch (af) { 2293126258Smlaier#ifdef INET 2294126258Smlaier case AF_INET: 2295126258Smlaier /* TCP checksum */ 2296126258Smlaier th->th_sum = in_cksum(m, len); 2297126258Smlaier 2298126258Smlaier /* Finish the IP header */ 2299126258Smlaier h->ip_v = 4; 2300126258Smlaier h->ip_hl = sizeof(*h) >> 2; 2301126258Smlaier h->ip_tos = IPTOS_LOWDELAY; 2302241913Sglebius h->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0); 2303241913Sglebius h->ip_len = htons(len); 2304223637Sbz h->ip_ttl = ttl ? ttl : V_ip_defttl; 2305126258Smlaier h->ip_sum = 0; 2306145836Smlaier 2307240233Sglebius pfse->pfse_type = PFSE_IP; 2308126258Smlaier break; 2309126258Smlaier#endif /* INET */ 2310126258Smlaier#ifdef INET6 2311126258Smlaier case AF_INET6: 2312126258Smlaier /* TCP checksum */ 2313126258Smlaier th->th_sum = in6_cksum(m, IPPROTO_TCP, 2314126258Smlaier sizeof(struct ip6_hdr), tlen); 2315126258Smlaier 2316126258Smlaier h6->ip6_vfc |= IPV6_VERSION; 2317126258Smlaier h6->ip6_hlim = IPV6_DEFHLIM; 2318126258Smlaier 2319240233Sglebius pfse->pfse_type = PFSE_IP6; 2320126258Smlaier break; 2321126258Smlaier#endif /* INET6 */ 2322126258Smlaier } 2323240233Sglebius pfse->pfse_m = m; 2324240233Sglebius pf_send(pfse); 2325126258Smlaier} 2326126258Smlaier 2327223637Sbzstatic void 2328126258Smlaierpf_send_icmp(struct mbuf *m, u_int8_t type, u_int8_t code, sa_family_t af, 2329126258Smlaier struct pf_rule *r) 2330126258Smlaier{ 2331240233Sglebius struct pf_send_entry *pfse; 2332240233Sglebius struct mbuf *m0; 2333223637Sbz struct pf_mtag *pf_mtag; 2334126258Smlaier 2335240233Sglebius /* Allocate outgoing queue entry, mbuf and mbuf tag. */ 2336240233Sglebius pfse = malloc(sizeof(*pfse), M_PFTEMP, M_NOWAIT); 2337240233Sglebius if (pfse == NULL) 2338132280Smlaier return; 2339240233Sglebius 2340240233Sglebius if ((m0 = m_copypacket(m, M_NOWAIT)) == NULL) { 2341240233Sglebius free(pfse, M_PFTEMP); 2342223637Sbz return; 2343240233Sglebius } 2344223637Sbz 2345240233Sglebius if ((pf_mtag = pf_get_mtag(m0)) == NULL) { 2346240233Sglebius free(pfse, M_PFTEMP); 2347126258Smlaier return; 2348240233Sglebius } 2349171168Smlaier /* XXX: revisit */ 2350171168Smlaier m0->m_flags |= M_SKIP_FIREWALL; 2351126258Smlaier 2352171168Smlaier if (r->rtableid >= 0) 2353178888Sjulian M_SETFIB(m0, r->rtableid); 2354171168Smlaier 2355126258Smlaier#ifdef ALTQ 2356126258Smlaier if (r->qid) { 2357171168Smlaier pf_mtag->qid = r->qid; 2358171168Smlaier /* add hints for ecn */ 2359171168Smlaier pf_mtag->hdr = mtod(m0, struct ip *); 2360126258Smlaier } 2361145836Smlaier#endif /* ALTQ */ 2362126258Smlaier 2363126258Smlaier switch (af) { 2364126258Smlaier#ifdef INET 2365126258Smlaier case AF_INET: 2366240233Sglebius pfse->pfse_type = PFSE_ICMP; 2367126258Smlaier break; 2368126258Smlaier#endif /* INET */ 2369126258Smlaier#ifdef INET6 2370126258Smlaier case AF_INET6: 2371240233Sglebius pfse->pfse_type = PFSE_ICMP6; 2372126258Smlaier break; 2373126258Smlaier#endif /* INET6 */ 2374126258Smlaier } 2375240233Sglebius pfse->pfse_m = m0; 2376240233Sglebius pfse->pfse_icmp_type = type; 2377240233Sglebius pfse->pfse_icmp_code = code; 2378240233Sglebius pf_send(pfse); 2379126258Smlaier} 2380126258Smlaier 2381126258Smlaier/* 2382126258Smlaier * Return 1 if the addresses a and b match (with mask m), otherwise return 0. 2383126258Smlaier * If n is 0, they match if they are equal. If n is != 0, they match if they 2384126258Smlaier * are different. 2385126258Smlaier */ 2386126258Smlaierint 2387126258Smlaierpf_match_addr(u_int8_t n, struct pf_addr *a, struct pf_addr *m, 2388126258Smlaier struct pf_addr *b, sa_family_t af) 2389126258Smlaier{ 2390126258Smlaier int match = 0; 2391126258Smlaier 2392126258Smlaier switch (af) { 2393126258Smlaier#ifdef INET 2394126258Smlaier case AF_INET: 2395126258Smlaier if ((a->addr32[0] & m->addr32[0]) == 2396126258Smlaier (b->addr32[0] & m->addr32[0])) 2397126258Smlaier match++; 2398126258Smlaier break; 2399126258Smlaier#endif /* INET */ 2400126258Smlaier#ifdef INET6 2401126258Smlaier case AF_INET6: 2402126258Smlaier if (((a->addr32[0] & m->addr32[0]) == 2403126258Smlaier (b->addr32[0] & m->addr32[0])) && 2404126258Smlaier ((a->addr32[1] & m->addr32[1]) == 2405126258Smlaier (b->addr32[1] & m->addr32[1])) && 2406126258Smlaier ((a->addr32[2] & m->addr32[2]) == 2407126258Smlaier (b->addr32[2] & m->addr32[2])) && 2408126258Smlaier ((a->addr32[3] & m->addr32[3]) == 2409126258Smlaier (b->addr32[3] & m->addr32[3]))) 2410126258Smlaier match++; 2411126258Smlaier break; 2412126258Smlaier#endif /* INET6 */ 2413126258Smlaier } 2414126258Smlaier if (match) { 2415126258Smlaier if (n) 2416126258Smlaier return (0); 2417126258Smlaier else 2418126258Smlaier return (1); 2419126258Smlaier } else { 2420126258Smlaier if (n) 2421126258Smlaier return (1); 2422126258Smlaier else 2423126258Smlaier return (0); 2424126258Smlaier } 2425126258Smlaier} 2426126258Smlaier 2427223637Sbz/* 2428223637Sbz * Return 1 if b <= a <= e, otherwise return 0. 2429223637Sbz */ 2430126258Smlaierint 2431223637Sbzpf_match_addr_range(struct pf_addr *b, struct pf_addr *e, 2432223637Sbz struct pf_addr *a, sa_family_t af) 2433223637Sbz{ 2434223637Sbz switch (af) { 2435223637Sbz#ifdef INET 2436223637Sbz case AF_INET: 2437223637Sbz if ((a->addr32[0] < b->addr32[0]) || 2438223637Sbz (a->addr32[0] > e->addr32[0])) 2439223637Sbz return (0); 2440223637Sbz break; 2441223637Sbz#endif /* INET */ 2442223637Sbz#ifdef INET6 2443223637Sbz case AF_INET6: { 2444223637Sbz int i; 2445223637Sbz 2446223637Sbz /* check a >= b */ 2447223637Sbz for (i = 0; i < 4; ++i) 2448223637Sbz if (a->addr32[i] > b->addr32[i]) 2449223637Sbz break; 2450223637Sbz else if (a->addr32[i] < b->addr32[i]) 2451223637Sbz return (0); 2452223637Sbz /* check a <= e */ 2453223637Sbz for (i = 0; i < 4; ++i) 2454223637Sbz if (a->addr32[i] < e->addr32[i]) 2455223637Sbz break; 2456223637Sbz else if (a->addr32[i] > e->addr32[i]) 2457223637Sbz return (0); 2458223637Sbz break; 2459223637Sbz } 2460223637Sbz#endif /* INET6 */ 2461223637Sbz } 2462223637Sbz return (1); 2463223637Sbz} 2464223637Sbz 2465240233Sglebiusstatic int 2466126258Smlaierpf_match(u_int8_t op, u_int32_t a1, u_int32_t a2, u_int32_t p) 2467126258Smlaier{ 2468126258Smlaier switch (op) { 2469126258Smlaier case PF_OP_IRG: 2470126258Smlaier return ((p > a1) && (p < a2)); 2471126258Smlaier case PF_OP_XRG: 2472126258Smlaier return ((p < a1) || (p > a2)); 2473126258Smlaier case PF_OP_RRG: 2474126258Smlaier return ((p >= a1) && (p <= a2)); 2475126258Smlaier case PF_OP_EQ: 2476126258Smlaier return (p == a1); 2477126258Smlaier case PF_OP_NE: 2478126258Smlaier return (p != a1); 2479126258Smlaier case PF_OP_LT: 2480126258Smlaier return (p < a1); 2481126258Smlaier case PF_OP_LE: 2482126258Smlaier return (p <= a1); 2483126258Smlaier case PF_OP_GT: 2484126258Smlaier return (p > a1); 2485126258Smlaier case PF_OP_GE: 2486126258Smlaier return (p >= a1); 2487126258Smlaier } 2488126258Smlaier return (0); /* never reached */ 2489126258Smlaier} 2490126258Smlaier 2491126258Smlaierint 2492126258Smlaierpf_match_port(u_int8_t op, u_int16_t a1, u_int16_t a2, u_int16_t p) 2493126258Smlaier{ 2494126258Smlaier NTOHS(a1); 2495126258Smlaier NTOHS(a2); 2496126258Smlaier NTOHS(p); 2497126258Smlaier return (pf_match(op, a1, a2, p)); 2498126258Smlaier} 2499126258Smlaier 2500240233Sglebiusstatic int 2501126258Smlaierpf_match_uid(u_int8_t op, uid_t a1, uid_t a2, uid_t u) 2502126258Smlaier{ 2503126258Smlaier if (u == UID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2504126258Smlaier return (0); 2505126258Smlaier return (pf_match(op, a1, a2, u)); 2506126258Smlaier} 2507126258Smlaier 2508240233Sglebiusstatic int 2509126258Smlaierpf_match_gid(u_int8_t op, gid_t a1, gid_t a2, gid_t g) 2510126258Smlaier{ 2511126258Smlaier if (g == GID_MAX && op != PF_OP_EQ && op != PF_OP_NE) 2512126258Smlaier return (0); 2513126258Smlaier return (pf_match(op, a1, a2, g)); 2514126258Smlaier} 2515126258Smlaier 2516223637Sbzint 2517240233Sglebiuspf_match_tag(struct mbuf *m, struct pf_rule *r, int *tag, int mtag) 2518171168Smlaier{ 2519171168Smlaier if (*tag == -1) 2520240233Sglebius *tag = mtag; 2521171168Smlaier 2522126258Smlaier return ((!r->match_tag_not && r->match_tag == *tag) || 2523126258Smlaier (r->match_tag_not && r->match_tag != *tag)); 2524126258Smlaier} 2525126258Smlaier 2526126258Smlaierint 2527240233Sglebiuspf_tag_packet(struct mbuf *m, struct pf_pdesc *pd, int tag) 2528126258Smlaier{ 2529126258Smlaier 2530240233Sglebius KASSERT(tag > 0, ("%s: tag %d", __func__, tag)); 2531126258Smlaier 2532240233Sglebius if (pd->pf_mtag == NULL && ((pd->pf_mtag = pf_get_mtag(m)) == NULL)) 2533240233Sglebius return (ENOMEM); 2534240233Sglebius 2535240233Sglebius pd->pf_mtag->tag = tag; 2536240233Sglebius 2537126258Smlaier return (0); 2538126258Smlaier} 2539126258Smlaier 2540240641Sglebius#define PF_ANCHOR_STACKSIZE 32 2541240641Sglebiusstruct pf_anchor_stackframe { 2542240641Sglebius struct pf_ruleset *rs; 2543240641Sglebius struct pf_rule *r; /* XXX: + match bit */ 2544240641Sglebius struct pf_anchor *child; 2545240641Sglebius}; 2546240641Sglebius 2547240641Sglebius/* 2548240641Sglebius * XXX: We rely on malloc(9) returning pointer aligned addresses. 2549240641Sglebius */ 2550240641Sglebius#define PF_ANCHORSTACK_MATCH 0x00000001 2551240641Sglebius#define PF_ANCHORSTACK_MASK (PF_ANCHORSTACK_MATCH) 2552240641Sglebius 2553240641Sglebius#define PF_ANCHOR_MATCH(f) ((uintptr_t)(f)->r & PF_ANCHORSTACK_MATCH) 2554240641Sglebius#define PF_ANCHOR_RULE(f) (struct pf_rule *) \ 2555240641Sglebius ((uintptr_t)(f)->r & ~PF_ANCHORSTACK_MASK) 2556240641Sglebius#define PF_ANCHOR_SET_MATCH(f) do { (f)->r = (void *) \ 2557240641Sglebius ((uintptr_t)(f)->r | PF_ANCHORSTACK_MATCH); \ 2558240641Sglebius} while (0) 2559240641Sglebius 2560223637Sbzvoid 2561240641Sglebiuspf_step_into_anchor(struct pf_anchor_stackframe *stack, int *depth, 2562240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2563240641Sglebius int *match) 2564145836Smlaier{ 2565145836Smlaier struct pf_anchor_stackframe *f; 2566126258Smlaier 2567240233Sglebius PF_RULES_RASSERT(); 2568240233Sglebius 2569171168Smlaier if (match) 2570171168Smlaier *match = 0; 2571240641Sglebius if (*depth >= PF_ANCHOR_STACKSIZE) { 2572240641Sglebius printf("%s: anchor stack overflow on %s\n", 2573240641Sglebius __func__, (*r)->anchor->name); 2574145836Smlaier *r = TAILQ_NEXT(*r, entries); 2575145836Smlaier return; 2576145836Smlaier } else if (*depth == 0 && a != NULL) 2577145836Smlaier *a = *r; 2578240641Sglebius f = stack + (*depth)++; 2579145836Smlaier f->rs = *rs; 2580145836Smlaier f->r = *r; 2581145836Smlaier if ((*r)->anchor_wildcard) { 2582240641Sglebius struct pf_anchor_node *parent = &(*r)->anchor->children; 2583240641Sglebius 2584240641Sglebius if ((f->child = RB_MIN(pf_anchor_node, parent)) == NULL) { 2585145836Smlaier *r = NULL; 2586145836Smlaier return; 2587145836Smlaier } 2588145836Smlaier *rs = &f->child->ruleset; 2589145836Smlaier } else { 2590145836Smlaier f->child = NULL; 2591145836Smlaier *rs = &(*r)->anchor->ruleset; 2592145836Smlaier } 2593145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2594145836Smlaier} 2595126258Smlaier 2596171168Smlaierint 2597240641Sglebiuspf_step_out_of_anchor(struct pf_anchor_stackframe *stack, int *depth, 2598240641Sglebius struct pf_ruleset **rs, int n, struct pf_rule **r, struct pf_rule **a, 2599240641Sglebius int *match) 2600145836Smlaier{ 2601145836Smlaier struct pf_anchor_stackframe *f; 2602240641Sglebius struct pf_rule *fr; 2603171168Smlaier int quick = 0; 2604145836Smlaier 2605240233Sglebius PF_RULES_RASSERT(); 2606240233Sglebius 2607145836Smlaier do { 2608145836Smlaier if (*depth <= 0) 2609145836Smlaier break; 2610240641Sglebius f = stack + *depth - 1; 2611240641Sglebius fr = PF_ANCHOR_RULE(f); 2612240641Sglebius if (f->child != NULL) { 2613240641Sglebius struct pf_anchor_node *parent; 2614240641Sglebius 2615240641Sglebius /* 2616240641Sglebius * This block traverses through 2617240641Sglebius * a wildcard anchor. 2618240641Sglebius */ 2619240641Sglebius parent = &fr->anchor->children; 2620240641Sglebius if (match != NULL && *match) { 2621240641Sglebius /* 2622240641Sglebius * If any of "*" matched, then 2623240641Sglebius * "foo/ *" matched, mark frame 2624240641Sglebius * appropriately. 2625240641Sglebius */ 2626240641Sglebius PF_ANCHOR_SET_MATCH(f); 2627171168Smlaier *match = 0; 2628171168Smlaier } 2629240641Sglebius f->child = RB_NEXT(pf_anchor_node, parent, f->child); 2630145836Smlaier if (f->child != NULL) { 2631145836Smlaier *rs = &f->child->ruleset; 2632145836Smlaier *r = TAILQ_FIRST((*rs)->rules[n].active.ptr); 2633145836Smlaier if (*r == NULL) 2634145836Smlaier continue; 2635145836Smlaier else 2636145836Smlaier break; 2637145836Smlaier } 2638145836Smlaier } 2639145836Smlaier (*depth)--; 2640145836Smlaier if (*depth == 0 && a != NULL) 2641145836Smlaier *a = NULL; 2642145836Smlaier *rs = f->rs; 2643240641Sglebius if (PF_ANCHOR_MATCH(f) || (match != NULL && *match)) 2644240641Sglebius quick = fr->quick; 2645240641Sglebius *r = TAILQ_NEXT(fr, entries); 2646145836Smlaier } while (*r == NULL); 2647171168Smlaier 2648171168Smlaier return (quick); 2649145836Smlaier} 2650145836Smlaier 2651126258Smlaier#ifdef INET6 2652126258Smlaiervoid 2653126258Smlaierpf_poolmask(struct pf_addr *naddr, struct pf_addr *raddr, 2654126258Smlaier struct pf_addr *rmask, struct pf_addr *saddr, sa_family_t af) 2655126258Smlaier{ 2656126258Smlaier switch (af) { 2657126258Smlaier#ifdef INET 2658126258Smlaier case AF_INET: 2659126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2660126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2661126258Smlaier break; 2662126258Smlaier#endif /* INET */ 2663126258Smlaier case AF_INET6: 2664126258Smlaier naddr->addr32[0] = (raddr->addr32[0] & rmask->addr32[0]) | 2665126258Smlaier ((rmask->addr32[0] ^ 0xffffffff ) & saddr->addr32[0]); 2666126258Smlaier naddr->addr32[1] = (raddr->addr32[1] & rmask->addr32[1]) | 2667126258Smlaier ((rmask->addr32[1] ^ 0xffffffff ) & saddr->addr32[1]); 2668126258Smlaier naddr->addr32[2] = (raddr->addr32[2] & rmask->addr32[2]) | 2669126258Smlaier ((rmask->addr32[2] ^ 0xffffffff ) & saddr->addr32[2]); 2670126258Smlaier naddr->addr32[3] = (raddr->addr32[3] & rmask->addr32[3]) | 2671126258Smlaier ((rmask->addr32[3] ^ 0xffffffff ) & saddr->addr32[3]); 2672126258Smlaier break; 2673126258Smlaier } 2674126258Smlaier} 2675126258Smlaier 2676126258Smlaiervoid 2677130613Smlaierpf_addr_inc(struct pf_addr *addr, sa_family_t af) 2678126258Smlaier{ 2679126258Smlaier switch (af) { 2680126258Smlaier#ifdef INET 2681126258Smlaier case AF_INET: 2682126258Smlaier addr->addr32[0] = htonl(ntohl(addr->addr32[0]) + 1); 2683126258Smlaier break; 2684126258Smlaier#endif /* INET */ 2685126258Smlaier case AF_INET6: 2686126258Smlaier if (addr->addr32[3] == 0xffffffff) { 2687126258Smlaier addr->addr32[3] = 0; 2688126258Smlaier if (addr->addr32[2] == 0xffffffff) { 2689126258Smlaier addr->addr32[2] = 0; 2690126258Smlaier if (addr->addr32[1] == 0xffffffff) { 2691126258Smlaier addr->addr32[1] = 0; 2692126258Smlaier addr->addr32[0] = 2693126258Smlaier htonl(ntohl(addr->addr32[0]) + 1); 2694126258Smlaier } else 2695126258Smlaier addr->addr32[1] = 2696126258Smlaier htonl(ntohl(addr->addr32[1]) + 1); 2697126258Smlaier } else 2698126258Smlaier addr->addr32[2] = 2699126258Smlaier htonl(ntohl(addr->addr32[2]) + 1); 2700126258Smlaier } else 2701126258Smlaier addr->addr32[3] = 2702126258Smlaier htonl(ntohl(addr->addr32[3]) + 1); 2703126258Smlaier break; 2704126258Smlaier } 2705126258Smlaier} 2706126258Smlaier#endif /* INET6 */ 2707126258Smlaier 2708126258Smlaierint 2709240233Sglebiuspf_socket_lookup(int direction, struct pf_pdesc *pd, struct mbuf *m) 2710126258Smlaier{ 2711126258Smlaier struct pf_addr *saddr, *daddr; 2712126258Smlaier u_int16_t sport, dport; 2713126261Smlaier struct inpcbinfo *pi; 2714126258Smlaier struct inpcb *inp; 2715126258Smlaier 2716171168Smlaier pd->lookup.uid = UID_MAX; 2717171168Smlaier pd->lookup.gid = GID_MAX; 2718223637Sbz 2719130613Smlaier switch (pd->proto) { 2720126258Smlaier case IPPROTO_TCP: 2721171168Smlaier if (pd->hdr.tcp == NULL) 2722171168Smlaier return (-1); 2723126258Smlaier sport = pd->hdr.tcp->th_sport; 2724126258Smlaier dport = pd->hdr.tcp->th_dport; 2725181803Sbz pi = &V_tcbinfo; 2726126258Smlaier break; 2727126258Smlaier case IPPROTO_UDP: 2728171168Smlaier if (pd->hdr.udp == NULL) 2729171168Smlaier return (-1); 2730126258Smlaier sport = pd->hdr.udp->uh_sport; 2731126258Smlaier dport = pd->hdr.udp->uh_dport; 2732181803Sbz pi = &V_udbinfo; 2733126258Smlaier break; 2734126258Smlaier default: 2735171168Smlaier return (-1); 2736126258Smlaier } 2737126258Smlaier if (direction == PF_IN) { 2738126258Smlaier saddr = pd->src; 2739126258Smlaier daddr = pd->dst; 2740126258Smlaier } else { 2741126258Smlaier u_int16_t p; 2742126258Smlaier 2743126258Smlaier p = sport; 2744126258Smlaier sport = dport; 2745126258Smlaier dport = p; 2746126258Smlaier saddr = pd->dst; 2747126258Smlaier daddr = pd->src; 2748126258Smlaier } 2749130613Smlaier switch (pd->af) { 2750145836Smlaier#ifdef INET 2751126258Smlaier case AF_INET: 2752240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, daddr->v4, 2753240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2754126261Smlaier if (inp == NULL) { 2755240233Sglebius inp = in_pcblookup_mbuf(pi, saddr->v4, sport, 2756222488Srwatson daddr->v4, dport, INPLOOKUP_WILDCARD | 2757240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2758222488Srwatson if (inp == NULL) 2759171168Smlaier return (-1); 2760126261Smlaier } 2761126258Smlaier break; 2762145836Smlaier#endif /* INET */ 2763126258Smlaier#ifdef INET6 2764126258Smlaier case AF_INET6: 2765240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, &daddr->v6, 2766240233Sglebius dport, INPLOOKUP_RLOCKPCB, NULL, m); 2767126261Smlaier if (inp == NULL) { 2768240233Sglebius inp = in6_pcblookup_mbuf(pi, &saddr->v6, sport, 2769222488Srwatson &daddr->v6, dport, INPLOOKUP_WILDCARD | 2770240233Sglebius INPLOOKUP_RLOCKPCB, NULL, m); 2771222488Srwatson if (inp == NULL) 2772171168Smlaier return (-1); 2773126261Smlaier } 2774126258Smlaier break; 2775126258Smlaier#endif /* INET6 */ 2776126258Smlaier 2777126258Smlaier default: 2778171168Smlaier return (-1); 2779126258Smlaier } 2780222488Srwatson INP_RLOCK_ASSERT(inp); 2781183606Sbz pd->lookup.uid = inp->inp_cred->cr_uid; 2782183606Sbz pd->lookup.gid = inp->inp_cred->cr_groups[0]; 2783222488Srwatson INP_RUNLOCK(inp); 2784240233Sglebius 2785126258Smlaier return (1); 2786126258Smlaier} 2787126258Smlaier 2788240233Sglebiusstatic u_int8_t 2789126258Smlaierpf_get_wscale(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2790126258Smlaier{ 2791126258Smlaier int hlen; 2792126258Smlaier u_int8_t hdr[60]; 2793126258Smlaier u_int8_t *opt, optlen; 2794126258Smlaier u_int8_t wscale = 0; 2795126258Smlaier 2796126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2797126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2798126258Smlaier return (0); 2799126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2800126258Smlaier return (0); 2801126258Smlaier opt = hdr + sizeof(struct tcphdr); 2802126258Smlaier hlen -= sizeof(struct tcphdr); 2803126258Smlaier while (hlen >= 3) { 2804126258Smlaier switch (*opt) { 2805126258Smlaier case TCPOPT_EOL: 2806126258Smlaier case TCPOPT_NOP: 2807126258Smlaier ++opt; 2808126258Smlaier --hlen; 2809126258Smlaier break; 2810126258Smlaier case TCPOPT_WINDOW: 2811126258Smlaier wscale = opt[2]; 2812126258Smlaier if (wscale > TCP_MAX_WINSHIFT) 2813126258Smlaier wscale = TCP_MAX_WINSHIFT; 2814126258Smlaier wscale |= PF_WSCALE_FLAG; 2815130613Smlaier /* FALLTHROUGH */ 2816126258Smlaier default: 2817126258Smlaier optlen = opt[1]; 2818126258Smlaier if (optlen < 2) 2819126258Smlaier optlen = 2; 2820126258Smlaier hlen -= optlen; 2821126258Smlaier opt += optlen; 2822130613Smlaier break; 2823126258Smlaier } 2824126258Smlaier } 2825126258Smlaier return (wscale); 2826126258Smlaier} 2827126258Smlaier 2828240233Sglebiusstatic u_int16_t 2829126258Smlaierpf_get_mss(struct mbuf *m, int off, u_int16_t th_off, sa_family_t af) 2830126258Smlaier{ 2831126258Smlaier int hlen; 2832126258Smlaier u_int8_t hdr[60]; 2833126258Smlaier u_int8_t *opt, optlen; 2834181803Sbz u_int16_t mss = V_tcp_mssdflt; 2835126258Smlaier 2836126258Smlaier hlen = th_off << 2; /* hlen <= sizeof(hdr) */ 2837126258Smlaier if (hlen <= sizeof(struct tcphdr)) 2838126258Smlaier return (0); 2839126258Smlaier if (!pf_pull_hdr(m, off, hdr, hlen, NULL, NULL, af)) 2840126258Smlaier return (0); 2841126258Smlaier opt = hdr + sizeof(struct tcphdr); 2842126258Smlaier hlen -= sizeof(struct tcphdr); 2843126258Smlaier while (hlen >= TCPOLEN_MAXSEG) { 2844126258Smlaier switch (*opt) { 2845126258Smlaier case TCPOPT_EOL: 2846126258Smlaier case TCPOPT_NOP: 2847126258Smlaier ++opt; 2848126258Smlaier --hlen; 2849126258Smlaier break; 2850126258Smlaier case TCPOPT_MAXSEG: 2851126258Smlaier bcopy((caddr_t)(opt + 2), (caddr_t)&mss, 2); 2852145030Sglebius NTOHS(mss); 2853130613Smlaier /* FALLTHROUGH */ 2854126258Smlaier default: 2855126258Smlaier optlen = opt[1]; 2856126258Smlaier if (optlen < 2) 2857126258Smlaier optlen = 2; 2858126258Smlaier hlen -= optlen; 2859126258Smlaier opt += optlen; 2860130613Smlaier break; 2861126258Smlaier } 2862126258Smlaier } 2863126258Smlaier return (mss); 2864126258Smlaier} 2865126258Smlaier 2866240233Sglebiusstatic u_int16_t 2867231852Sbzpf_calc_mss(struct pf_addr *addr, sa_family_t af, int rtableid, u_int16_t offer) 2868126258Smlaier{ 2869126258Smlaier#ifdef INET 2870126258Smlaier struct sockaddr_in *dst; 2871126258Smlaier struct route ro; 2872126258Smlaier#endif /* INET */ 2873126258Smlaier#ifdef INET6 2874126258Smlaier struct sockaddr_in6 *dst6; 2875126258Smlaier struct route_in6 ro6; 2876126258Smlaier#endif /* INET6 */ 2877126258Smlaier struct rtentry *rt = NULL; 2878223637Sbz int hlen = 0; 2879181803Sbz u_int16_t mss = V_tcp_mssdflt; 2880126258Smlaier 2881126258Smlaier switch (af) { 2882126258Smlaier#ifdef INET 2883126258Smlaier case AF_INET: 2884126258Smlaier hlen = sizeof(struct ip); 2885126258Smlaier bzero(&ro, sizeof(ro)); 2886126258Smlaier dst = (struct sockaddr_in *)&ro.ro_dst; 2887126258Smlaier dst->sin_family = AF_INET; 2888126258Smlaier dst->sin_len = sizeof(*dst); 2889126258Smlaier dst->sin_addr = addr->v4; 2890231852Sbz in_rtalloc_ign(&ro, 0, rtableid); 2891126258Smlaier rt = ro.ro_rt; 2892126258Smlaier break; 2893126258Smlaier#endif /* INET */ 2894126258Smlaier#ifdef INET6 2895126258Smlaier case AF_INET6: 2896126258Smlaier hlen = sizeof(struct ip6_hdr); 2897126258Smlaier bzero(&ro6, sizeof(ro6)); 2898126258Smlaier dst6 = (struct sockaddr_in6 *)&ro6.ro_dst; 2899126258Smlaier dst6->sin6_family = AF_INET6; 2900126258Smlaier dst6->sin6_len = sizeof(*dst6); 2901126258Smlaier dst6->sin6_addr = addr->v6; 2902231852Sbz in6_rtalloc_ign(&ro6, 0, rtableid); 2903126258Smlaier rt = ro6.ro_rt; 2904126258Smlaier break; 2905126258Smlaier#endif /* INET6 */ 2906126258Smlaier } 2907126258Smlaier 2908126258Smlaier if (rt && rt->rt_ifp) { 2909126258Smlaier mss = rt->rt_ifp->if_mtu - hlen - sizeof(struct tcphdr); 2910181803Sbz mss = max(V_tcp_mssdflt, mss); 2911126258Smlaier RTFREE(rt); 2912126258Smlaier } 2913126258Smlaier mss = min(mss, offer); 2914126258Smlaier mss = max(mss, 64); /* sanity - at least max opt space */ 2915126258Smlaier return (mss); 2916126258Smlaier} 2917126258Smlaier 2918240233Sglebiusstatic void 2919126258Smlaierpf_set_rt_ifp(struct pf_state *s, struct pf_addr *saddr) 2920126258Smlaier{ 2921126258Smlaier struct pf_rule *r = s->rule.ptr; 2922223637Sbz struct pf_src_node *sn = NULL; 2923126258Smlaier 2924130613Smlaier s->rt_kif = NULL; 2925126258Smlaier if (!r->rt || r->rt == PF_FASTROUTE) 2926126258Smlaier return; 2927223637Sbz switch (s->key[PF_SK_WIRE]->af) { 2928126258Smlaier#ifdef INET 2929126258Smlaier case AF_INET: 2930223637Sbz pf_map_addr(AF_INET, r, saddr, &s->rt_addr, NULL, &sn); 2931130613Smlaier s->rt_kif = r->rpool.cur->kif; 2932126258Smlaier break; 2933126258Smlaier#endif /* INET */ 2934126258Smlaier#ifdef INET6 2935126258Smlaier case AF_INET6: 2936223637Sbz pf_map_addr(AF_INET6, r, saddr, &s->rt_addr, NULL, &sn); 2937130613Smlaier s->rt_kif = r->rpool.cur->kif; 2938126258Smlaier break; 2939126258Smlaier#endif /* INET6 */ 2940126258Smlaier } 2941126258Smlaier} 2942126258Smlaier 2943240233Sglebiusstatic u_int32_t 2944223637Sbzpf_tcp_iss(struct pf_pdesc *pd) 2945223637Sbz{ 2946223637Sbz MD5_CTX ctx; 2947223637Sbz u_int32_t digest[4]; 2948223637Sbz 2949223637Sbz if (V_pf_tcp_secret_init == 0) { 2950223637Sbz read_random(&V_pf_tcp_secret, sizeof(V_pf_tcp_secret)); 2951223637Sbz MD5Init(&V_pf_tcp_secret_ctx); 2952223637Sbz MD5Update(&V_pf_tcp_secret_ctx, V_pf_tcp_secret, 2953223637Sbz sizeof(V_pf_tcp_secret)); 2954223637Sbz V_pf_tcp_secret_init = 1; 2955223637Sbz } 2956223637Sbz 2957223637Sbz ctx = V_pf_tcp_secret_ctx; 2958223637Sbz 2959223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_sport, sizeof(u_short)); 2960223637Sbz MD5Update(&ctx, (char *)&pd->hdr.tcp->th_dport, sizeof(u_short)); 2961223637Sbz if (pd->af == AF_INET6) { 2962223637Sbz MD5Update(&ctx, (char *)&pd->src->v6, sizeof(struct in6_addr)); 2963223637Sbz MD5Update(&ctx, (char *)&pd->dst->v6, sizeof(struct in6_addr)); 2964223637Sbz } else { 2965223637Sbz MD5Update(&ctx, (char *)&pd->src->v4, sizeof(struct in_addr)); 2966223637Sbz MD5Update(&ctx, (char *)&pd->dst->v4, sizeof(struct in_addr)); 2967223637Sbz } 2968223637Sbz MD5Final((u_char *)digest, &ctx); 2969223637Sbz V_pf_tcp_iss_off += 4096; 2970223637Sbz#define ISN_RANDOM_INCREMENT (4096 - 1) 2971223637Sbz return (digest[0] + (arc4random() & ISN_RANDOM_INCREMENT) + 2972223637Sbz V_pf_tcp_iss_off); 2973223637Sbz#undef ISN_RANDOM_INCREMENT 2974223637Sbz} 2975223637Sbz 2976240233Sglebiusstatic int 2977223637Sbzpf_test_rule(struct pf_rule **rm, struct pf_state **sm, int direction, 2978240233Sglebius struct pfi_kif *kif, struct mbuf *m, int off, struct pf_pdesc *pd, 2979240233Sglebius struct pf_rule **am, struct pf_ruleset **rsm, struct inpcb *inp) 2980126258Smlaier{ 2981130613Smlaier struct pf_rule *nr = NULL; 2982240233Sglebius struct pf_addr * const saddr = pd->src; 2983240233Sglebius struct pf_addr * const daddr = pd->dst; 2984126258Smlaier sa_family_t af = pd->af; 2985126258Smlaier struct pf_rule *r, *a = NULL; 2986126258Smlaier struct pf_ruleset *ruleset = NULL; 2987130613Smlaier struct pf_src_node *nsn = NULL; 2988223637Sbz struct tcphdr *th = pd->hdr.tcp; 2989223637Sbz struct pf_state_key *sk = NULL, *nk = NULL; 2990126258Smlaier u_short reason; 2991223637Sbz int rewrite = 0, hdrlen = 0; 2992171168Smlaier int tag = -1, rtableid = -1; 2993145836Smlaier int asd = 0; 2994171168Smlaier int match = 0; 2995223637Sbz int state_icmp = 0; 2996223637Sbz u_int16_t sport = 0, dport = 0; 2997223637Sbz u_int16_t bproto_sum = 0, bip_sum = 0; 2998223637Sbz u_int8_t icmptype = 0, icmpcode = 0; 2999240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3000126258Smlaier 3001240233Sglebius PF_RULES_RASSERT(); 3002223637Sbz 3003240233Sglebius if (inp != NULL) { 3004240233Sglebius INP_LOCK_ASSERT(inp); 3005240233Sglebius pd->lookup.uid = inp->inp_cred->cr_uid; 3006240233Sglebius pd->lookup.gid = inp->inp_cred->cr_groups[0]; 3007240233Sglebius pd->lookup.done = 1; 3008145836Smlaier } 3009145836Smlaier 3010223637Sbz switch (pd->proto) { 3011223637Sbz case IPPROTO_TCP: 3012223637Sbz sport = th->th_sport; 3013223637Sbz dport = th->th_dport; 3014223637Sbz hdrlen = sizeof(*th); 3015223637Sbz break; 3016223637Sbz case IPPROTO_UDP: 3017223637Sbz sport = pd->hdr.udp->uh_sport; 3018223637Sbz dport = pd->hdr.udp->uh_dport; 3019223637Sbz hdrlen = sizeof(*pd->hdr.udp); 3020223637Sbz break; 3021223637Sbz#ifdef INET 3022223637Sbz case IPPROTO_ICMP: 3023223637Sbz if (pd->af != AF_INET) 3024223637Sbz break; 3025223637Sbz sport = dport = pd->hdr.icmp->icmp_id; 3026223637Sbz hdrlen = sizeof(*pd->hdr.icmp); 3027223637Sbz icmptype = pd->hdr.icmp->icmp_type; 3028223637Sbz icmpcode = pd->hdr.icmp->icmp_code; 3029223637Sbz 3030223637Sbz if (icmptype == ICMP_UNREACH || 3031223637Sbz icmptype == ICMP_SOURCEQUENCH || 3032223637Sbz icmptype == ICMP_REDIRECT || 3033223637Sbz icmptype == ICMP_TIMXCEED || 3034223637Sbz icmptype == ICMP_PARAMPROB) 3035223637Sbz state_icmp++; 3036223637Sbz break; 3037223637Sbz#endif /* INET */ 3038223637Sbz#ifdef INET6 3039223637Sbz case IPPROTO_ICMPV6: 3040223637Sbz if (af != AF_INET6) 3041223637Sbz break; 3042223637Sbz sport = dport = pd->hdr.icmp6->icmp6_id; 3043223637Sbz hdrlen = sizeof(*pd->hdr.icmp6); 3044223637Sbz icmptype = pd->hdr.icmp6->icmp6_type; 3045223637Sbz icmpcode = pd->hdr.icmp6->icmp6_code; 3046223637Sbz 3047223637Sbz if (icmptype == ICMP6_DST_UNREACH || 3048223637Sbz icmptype == ICMP6_PACKET_TOO_BIG || 3049223637Sbz icmptype == ICMP6_TIME_EXCEEDED || 3050223637Sbz icmptype == ICMP6_PARAM_PROB) 3051223637Sbz state_icmp++; 3052223637Sbz break; 3053223637Sbz#endif /* INET6 */ 3054223637Sbz default: 3055223637Sbz sport = dport = hdrlen = 0; 3056223637Sbz break; 3057223637Sbz } 3058223637Sbz 3059126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3060126258Smlaier 3061223637Sbz /* check packet for BINAT/NAT/RDR */ 3062240233Sglebius if ((nr = pf_get_translation(pd, m, off, direction, kif, &nsn, &sk, 3063240641Sglebius &nk, saddr, daddr, sport, dport, anchor_stack)) != NULL) { 3064240233Sglebius KASSERT(sk != NULL, ("%s: null sk", __func__)); 3065240233Sglebius KASSERT(nk != NULL, ("%s: null nk", __func__)); 3066223637Sbz 3067223637Sbz if (pd->ip_sum) 3068223637Sbz bip_sum = *pd->ip_sum; 3069223637Sbz 3070223637Sbz switch (pd->proto) { 3071223637Sbz case IPPROTO_TCP: 3072223637Sbz bproto_sum = th->th_sum; 3073223637Sbz pd->proto_sum = &th->th_sum; 3074223637Sbz 3075223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3076223637Sbz nk->port[pd->sidx] != sport) { 3077223637Sbz pf_change_ap(saddr, &th->th_sport, pd->ip_sum, 3078223637Sbz &th->th_sum, &nk->addr[pd->sidx], 3079223637Sbz nk->port[pd->sidx], 0, af); 3080223637Sbz pd->sport = &th->th_sport; 3081223637Sbz sport = th->th_sport; 3082223637Sbz } 3083223637Sbz 3084223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3085223637Sbz nk->port[pd->didx] != dport) { 3086223637Sbz pf_change_ap(daddr, &th->th_dport, pd->ip_sum, 3087223637Sbz &th->th_sum, &nk->addr[pd->didx], 3088223637Sbz nk->port[pd->didx], 0, af); 3089223637Sbz dport = th->th_dport; 3090223637Sbz pd->dport = &th->th_dport; 3091223637Sbz } 3092126258Smlaier rewrite++; 3093223637Sbz break; 3094223637Sbz case IPPROTO_UDP: 3095223637Sbz bproto_sum = pd->hdr.udp->uh_sum; 3096223637Sbz pd->proto_sum = &pd->hdr.udp->uh_sum; 3097223637Sbz 3098223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], af) || 3099223637Sbz nk->port[pd->sidx] != sport) { 3100223637Sbz pf_change_ap(saddr, &pd->hdr.udp->uh_sport, 3101223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3102223637Sbz &nk->addr[pd->sidx], 3103223637Sbz nk->port[pd->sidx], 1, af); 3104223637Sbz sport = pd->hdr.udp->uh_sport; 3105223637Sbz pd->sport = &pd->hdr.udp->uh_sport; 3106223637Sbz } 3107223637Sbz 3108223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], af) || 3109223637Sbz nk->port[pd->didx] != dport) { 3110223637Sbz pf_change_ap(daddr, &pd->hdr.udp->uh_dport, 3111223637Sbz pd->ip_sum, &pd->hdr.udp->uh_sum, 3112223637Sbz &nk->addr[pd->didx], 3113223637Sbz nk->port[pd->didx], 1, af); 3114223637Sbz dport = pd->hdr.udp->uh_dport; 3115223637Sbz pd->dport = &pd->hdr.udp->uh_dport; 3116223637Sbz } 3117223637Sbz rewrite++; 3118223637Sbz break; 3119223637Sbz#ifdef INET 3120223637Sbz case IPPROTO_ICMP: 3121223637Sbz nk->port[0] = nk->port[1]; 3122223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET)) 3123223637Sbz pf_change_a(&saddr->v4.s_addr, pd->ip_sum, 3124223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3125223637Sbz 3126223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET)) 3127223637Sbz pf_change_a(&daddr->v4.s_addr, pd->ip_sum, 3128223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3129223637Sbz 3130223637Sbz if (nk->port[1] != pd->hdr.icmp->icmp_id) { 3131223637Sbz pd->hdr.icmp->icmp_cksum = pf_cksum_fixup( 3132223637Sbz pd->hdr.icmp->icmp_cksum, sport, 3133223637Sbz nk->port[1], 0); 3134223637Sbz pd->hdr.icmp->icmp_id = nk->port[1]; 3135223637Sbz pd->sport = &pd->hdr.icmp->icmp_id; 3136223637Sbz } 3137223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 3138223637Sbz break; 3139223637Sbz#endif /* INET */ 3140223637Sbz#ifdef INET6 3141223637Sbz case IPPROTO_ICMPV6: 3142223637Sbz nk->port[0] = nk->port[1]; 3143223637Sbz if (PF_ANEQ(saddr, &nk->addr[pd->sidx], AF_INET6)) 3144223637Sbz pf_change_a6(saddr, &pd->hdr.icmp6->icmp6_cksum, 3145223637Sbz &nk->addr[pd->sidx], 0); 3146223637Sbz 3147223637Sbz if (PF_ANEQ(daddr, &nk->addr[pd->didx], AF_INET6)) 3148223637Sbz pf_change_a6(daddr, &pd->hdr.icmp6->icmp6_cksum, 3149223637Sbz &nk->addr[pd->didx], 0); 3150223637Sbz rewrite++; 3151223637Sbz break; 3152223637Sbz#endif /* INET */ 3153223637Sbz default: 3154223637Sbz switch (af) { 3155223637Sbz#ifdef INET 3156223637Sbz case AF_INET: 3157223637Sbz if (PF_ANEQ(saddr, 3158223637Sbz &nk->addr[pd->sidx], AF_INET)) 3159223637Sbz pf_change_a(&saddr->v4.s_addr, 3160223637Sbz pd->ip_sum, 3161223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 3162223637Sbz 3163223637Sbz if (PF_ANEQ(daddr, 3164223637Sbz &nk->addr[pd->didx], AF_INET)) 3165223637Sbz pf_change_a(&daddr->v4.s_addr, 3166223637Sbz pd->ip_sum, 3167223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 3168223637Sbz break; 3169223637Sbz#endif /* INET */ 3170223637Sbz#ifdef INET6 3171223637Sbz case AF_INET6: 3172223637Sbz if (PF_ANEQ(saddr, 3173223637Sbz &nk->addr[pd->sidx], AF_INET6)) 3174223637Sbz PF_ACPY(saddr, &nk->addr[pd->sidx], af); 3175223637Sbz 3176223637Sbz if (PF_ANEQ(daddr, 3177223637Sbz &nk->addr[pd->didx], AF_INET6)) 3178223637Sbz PF_ACPY(saddr, &nk->addr[pd->didx], af); 3179223637Sbz break; 3180223637Sbz#endif /* INET */ 3181223637Sbz } 3182223637Sbz break; 3183126258Smlaier } 3184223637Sbz if (nr->natpass) 3185223637Sbz r = NULL; 3186223637Sbz pd->nat_rule = nr; 3187126258Smlaier } 3188126258Smlaier 3189126258Smlaier while (r != NULL) { 3190126258Smlaier r->evaluations++; 3191171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3192126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3193126258Smlaier else if (r->direction && r->direction != direction) 3194126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3195126258Smlaier else if (r->af && r->af != af) 3196126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3197223637Sbz else if (r->proto && r->proto != pd->proto) 3198126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3199171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, saddr, af, 3200231852Sbz r->src.neg, kif, M_GETFIB(m))) 3201126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3202223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3203126258Smlaier else if (r->src.port_op && !pf_match_port(r->src.port_op, 3204223637Sbz r->src.port[0], r->src.port[1], sport)) 3205126258Smlaier r = r->skip[PF_SKIP_SRC_PORT].ptr; 3206171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, daddr, af, 3207231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3208126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3209223637Sbz /* tcp/udp only. port_op always 0 in other cases */ 3210126258Smlaier else if (r->dst.port_op && !pf_match_port(r->dst.port_op, 3211223637Sbz r->dst.port[0], r->dst.port[1], dport)) 3212126258Smlaier r = r->skip[PF_SKIP_DST_PORT].ptr; 3213223637Sbz /* icmp only. type always 0 in other cases */ 3214223637Sbz else if (r->type && r->type != icmptype + 1) 3215223637Sbz r = TAILQ_NEXT(r, entries); 3216223637Sbz /* icmp only. type always 0 in other cases */ 3217223637Sbz else if (r->code && r->code != icmpcode + 1) 3218223637Sbz r = TAILQ_NEXT(r, entries); 3219171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3220126258Smlaier r = TAILQ_NEXT(r, entries); 3221126258Smlaier else if (r->rule_flag & PFRULE_FRAGMENT) 3222126258Smlaier r = TAILQ_NEXT(r, entries); 3223223637Sbz else if (pd->proto == IPPROTO_TCP && 3224223637Sbz (r->flagset & th->th_flags) != r->flags) 3225126258Smlaier r = TAILQ_NEXT(r, entries); 3226223637Sbz /* tcp/udp only. uid.op always 0 in other cases */ 3227171168Smlaier else if (r->uid.op && (pd->lookup.done || (pd->lookup.done = 3228240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3229126258Smlaier !pf_match_uid(r->uid.op, r->uid.uid[0], r->uid.uid[1], 3230171168Smlaier pd->lookup.uid)) 3231126258Smlaier r = TAILQ_NEXT(r, entries); 3232223637Sbz /* tcp/udp only. gid.op always 0 in other cases */ 3233171168Smlaier else if (r->gid.op && (pd->lookup.done || (pd->lookup.done = 3234240233Sglebius pf_socket_lookup(direction, pd, m), 1)) && 3235126258Smlaier !pf_match_gid(r->gid.op, r->gid.gid[0], r->gid.gid[1], 3236171168Smlaier pd->lookup.gid)) 3237126258Smlaier r = TAILQ_NEXT(r, entries); 3238223637Sbz else if (r->prob && 3239223637Sbz r->prob <= arc4random()) 3240126258Smlaier r = TAILQ_NEXT(r, entries); 3241240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3242240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3243126258Smlaier r = TAILQ_NEXT(r, entries); 3244223637Sbz else if (r->os_fingerprint != PF_OSFP_ANY && 3245223637Sbz (pd->proto != IPPROTO_TCP || !pf_osfp_match( 3246223637Sbz pf_osfp_fingerprint(pd, m, off, th), 3247223637Sbz r->os_fingerprint))) 3248126258Smlaier r = TAILQ_NEXT(r, entries); 3249126258Smlaier else { 3250126258Smlaier if (r->tag) 3251126258Smlaier tag = r->tag; 3252171168Smlaier if (r->rtableid >= 0) 3253171168Smlaier rtableid = r->rtableid; 3254126258Smlaier if (r->anchor == NULL) { 3255171168Smlaier match = 1; 3256126258Smlaier *rm = r; 3257126258Smlaier *am = a; 3258126258Smlaier *rsm = ruleset; 3259126258Smlaier if ((*rm)->quick) 3260126258Smlaier break; 3261126258Smlaier r = TAILQ_NEXT(r, entries); 3262126258Smlaier } else 3263240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3264240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3265240641Sglebius &match); 3266126258Smlaier } 3267240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3268240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3269171168Smlaier break; 3270126258Smlaier } 3271126258Smlaier r = *rm; 3272126258Smlaier a = *am; 3273126258Smlaier ruleset = *rsm; 3274126258Smlaier 3275126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3276126258Smlaier 3277223637Sbz if (r->log || (nr != NULL && nr->log)) { 3278126258Smlaier if (rewrite) 3279223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3280240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r->log ? r : nr, a, 3281240233Sglebius ruleset, pd, 1); 3282126258Smlaier } 3283126258Smlaier 3284126258Smlaier if ((r->action == PF_DROP) && 3285126258Smlaier ((r->rule_flag & PFRULE_RETURNRST) || 3286126258Smlaier (r->rule_flag & PFRULE_RETURNICMP) || 3287126258Smlaier (r->rule_flag & PFRULE_RETURN))) { 3288126258Smlaier /* undo NAT changes, if they have taken place */ 3289130613Smlaier if (nr != NULL) { 3290223637Sbz PF_ACPY(saddr, &sk->addr[pd->sidx], af); 3291223637Sbz PF_ACPY(daddr, &sk->addr[pd->didx], af); 3292223637Sbz if (pd->sport) 3293223637Sbz *pd->sport = sk->port[pd->sidx]; 3294223637Sbz if (pd->dport) 3295223637Sbz *pd->dport = sk->port[pd->didx]; 3296223637Sbz if (pd->proto_sum) 3297223637Sbz *pd->proto_sum = bproto_sum; 3298223637Sbz if (pd->ip_sum) 3299223637Sbz *pd->ip_sum = bip_sum; 3300223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3301126258Smlaier } 3302223637Sbz if (pd->proto == IPPROTO_TCP && 3303223637Sbz ((r->rule_flag & PFRULE_RETURNRST) || 3304126258Smlaier (r->rule_flag & PFRULE_RETURN)) && 3305126258Smlaier !(th->th_flags & TH_RST)) { 3306223637Sbz u_int32_t ack = ntohl(th->th_seq) + pd->p_len; 3307223637Sbz int len = 0; 3308223637Sbz#ifdef INET 3309223637Sbz struct ip *h4; 3310223637Sbz#endif 3311223637Sbz#ifdef INET6 3312223637Sbz struct ip6_hdr *h6; 3313223637Sbz#endif 3314126258Smlaier 3315223637Sbz switch (af) { 3316223637Sbz#ifdef INET 3317223637Sbz case AF_INET: 3318223637Sbz h4 = mtod(m, struct ip *); 3319223637Sbz len = ntohs(h4->ip_len) - off; 3320223637Sbz break; 3321223637Sbz#endif 3322223637Sbz#ifdef INET6 3323223637Sbz case AF_INET6: 3324223637Sbz h6 = mtod(m, struct ip6_hdr *); 3325223637Sbz len = ntohs(h6->ip6_plen) - (off - sizeof(*h6)); 3326223637Sbz break; 3327223637Sbz#endif 3328223637Sbz } 3329223637Sbz 3330223637Sbz if (pf_check_proto_cksum(m, off, len, IPPROTO_TCP, af)) 3331223637Sbz REASON_SET(&reason, PFRES_PROTCKSUM); 3332223637Sbz else { 3333223637Sbz if (th->th_flags & TH_SYN) 3334223637Sbz ack++; 3335223637Sbz if (th->th_flags & TH_FIN) 3336223637Sbz ack++; 3337223637Sbz pf_send_tcp(m, r, af, pd->dst, 3338223637Sbz pd->src, th->th_dport, th->th_sport, 3339223637Sbz ntohl(th->th_ack), ack, TH_RST|TH_ACK, 0, 0, 3340240233Sglebius r->return_ttl, 1, 0, kif->pfik_ifp); 3341223637Sbz } 3342223637Sbz } else if (pd->proto != IPPROTO_ICMP && af == AF_INET && 3343223637Sbz r->return_icmp) 3344126258Smlaier pf_send_icmp(m, r->return_icmp >> 8, 3345126258Smlaier r->return_icmp & 255, af, r); 3346223637Sbz else if (pd->proto != IPPROTO_ICMPV6 && af == AF_INET6 && 3347223637Sbz r->return_icmp6) 3348126258Smlaier pf_send_icmp(m, r->return_icmp6 >> 8, 3349126258Smlaier r->return_icmp6 & 255, af, r); 3350126258Smlaier } 3351126258Smlaier 3352126258Smlaier if (r->action == PF_DROP) 3353223637Sbz goto cleanup; 3354126258Smlaier 3355240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3356126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3357223637Sbz goto cleanup; 3358126258Smlaier } 3359240233Sglebius if (rtableid >= 0) 3360240233Sglebius M_SETFIB(m, rtableid); 3361126258Smlaier 3362223637Sbz if (!state_icmp && (r->keep_state || nr != NULL || 3363223637Sbz (pd->flags & PFDESC_TCP_NORM))) { 3364223637Sbz int action; 3365240233Sglebius action = pf_create_state(r, nr, a, pd, nsn, nk, sk, m, off, 3366240233Sglebius sport, dport, &rewrite, kif, sm, tag, bproto_sum, bip_sum, 3367240233Sglebius hdrlen); 3368223637Sbz if (action != PF_PASS) 3369223637Sbz return (action); 3370223637Sbz } else { 3371223637Sbz if (sk != NULL) 3372240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3373223637Sbz if (nk != NULL) 3374240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3375223637Sbz } 3376126258Smlaier 3377223637Sbz /* copy back packet headers if we performed NAT operations */ 3378223637Sbz if (rewrite) 3379223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3380130613Smlaier 3381240233Sglebius if (*sm != NULL && !((*sm)->state_flags & PFSTATE_NOSYNC) && 3382240233Sglebius direction == PF_OUT && 3383240233Sglebius pfsync_defer_ptr != NULL && pfsync_defer_ptr(*sm, m)) 3384223637Sbz /* 3385223637Sbz * We want the state created, but we dont 3386223637Sbz * want to send this in case a partner 3387223637Sbz * firewall has to know about it to allow 3388223637Sbz * replies through it. 3389223637Sbz */ 3390240233Sglebius return (PF_DEFER); 3391223637Sbz 3392223637Sbz return (PF_PASS); 3393223637Sbz 3394130613Smlaiercleanup: 3395223637Sbz if (sk != NULL) 3396240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3397223637Sbz if (nk != NULL) 3398240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3399223637Sbz return (PF_DROP); 3400223637Sbz} 3401126258Smlaier 3402240233Sglebiusstatic int 3403223637Sbzpf_create_state(struct pf_rule *r, struct pf_rule *nr, struct pf_rule *a, 3404240233Sglebius struct pf_pdesc *pd, struct pf_src_node *nsn, struct pf_state_key *nk, 3405240233Sglebius struct pf_state_key *sk, struct mbuf *m, int off, u_int16_t sport, 3406240233Sglebius u_int16_t dport, int *rewrite, struct pfi_kif *kif, struct pf_state **sm, 3407240233Sglebius int tag, u_int16_t bproto_sum, u_int16_t bip_sum, int hdrlen) 3408223637Sbz{ 3409223637Sbz struct pf_state *s = NULL; 3410223637Sbz struct pf_src_node *sn = NULL; 3411223637Sbz struct tcphdr *th = pd->hdr.tcp; 3412223637Sbz u_int16_t mss = V_tcp_mssdflt; 3413223637Sbz u_short reason; 3414223637Sbz 3415223637Sbz /* check maximums */ 3416223637Sbz if (r->max_states && (r->states_cur >= r->max_states)) { 3417223637Sbz V_pf_status.lcounters[LCNT_STATES]++; 3418223637Sbz REASON_SET(&reason, PFRES_MAXSTATES); 3419223637Sbz return (PF_DROP); 3420223637Sbz } 3421223637Sbz /* src node for filter rule */ 3422223637Sbz if ((r->rule_flag & PFRULE_SRCTRACK || 3423223637Sbz r->rpool.opts & PF_POOL_STICKYADDR) && 3424223637Sbz pf_insert_src_node(&sn, r, pd->src, pd->af) != 0) { 3425223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3426223637Sbz goto csfailed; 3427223637Sbz } 3428223637Sbz /* src node for translation rule */ 3429223637Sbz if (nr != NULL && (nr->rpool.opts & PF_POOL_STICKYADDR) && 3430223637Sbz pf_insert_src_node(&nsn, nr, &sk->addr[pd->sidx], pd->af)) { 3431223637Sbz REASON_SET(&reason, PFRES_SRCLIMIT); 3432223637Sbz goto csfailed; 3433223637Sbz } 3434240233Sglebius s = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO); 3435223637Sbz if (s == NULL) { 3436223637Sbz REASON_SET(&reason, PFRES_MEMORY); 3437223637Sbz goto csfailed; 3438223637Sbz } 3439223637Sbz s->rule.ptr = r; 3440223637Sbz s->nat_rule.ptr = nr; 3441223637Sbz s->anchor.ptr = a; 3442223637Sbz STATE_INC_COUNTERS(s); 3443223637Sbz if (r->allow_opts) 3444223637Sbz s->state_flags |= PFSTATE_ALLOWOPTS; 3445223637Sbz if (r->rule_flag & PFRULE_STATESLOPPY) 3446223637Sbz s->state_flags |= PFSTATE_SLOPPY; 3447223637Sbz s->log = r->log & PF_LOG_ALL; 3448223637Sbz s->sync_state = PFSYNC_S_NONE; 3449223637Sbz if (nr != NULL) 3450223637Sbz s->log |= nr->log & PF_LOG_ALL; 3451223637Sbz switch (pd->proto) { 3452223637Sbz case IPPROTO_TCP: 3453126258Smlaier s->src.seqlo = ntohl(th->th_seq); 3454223637Sbz s->src.seqhi = s->src.seqlo + pd->p_len + 1; 3455126258Smlaier if ((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN && 3456126258Smlaier r->keep_state == PF_STATE_MODULATE) { 3457126258Smlaier /* Generate sequence number modulator */ 3458223637Sbz if ((s->src.seqdiff = pf_tcp_iss(pd) - s->src.seqlo) == 3459223637Sbz 0) 3460223637Sbz s->src.seqdiff = 1; 3461126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, 3462126258Smlaier htonl(s->src.seqlo + s->src.seqdiff), 0); 3463223637Sbz *rewrite = 1; 3464126258Smlaier } else 3465126258Smlaier s->src.seqdiff = 0; 3466126258Smlaier if (th->th_flags & TH_SYN) { 3467126258Smlaier s->src.seqhi++; 3468223637Sbz s->src.wscale = pf_get_wscale(m, off, 3469223637Sbz th->th_off, pd->af); 3470126258Smlaier } 3471126258Smlaier s->src.max_win = MAX(ntohs(th->th_win), 1); 3472126258Smlaier if (s->src.wscale & PF_WSCALE_MASK) { 3473126258Smlaier /* Remove scale factor from initial window */ 3474126258Smlaier int win = s->src.max_win; 3475126258Smlaier win += 1 << (s->src.wscale & PF_WSCALE_MASK); 3476126258Smlaier s->src.max_win = (win - 1) >> 3477126258Smlaier (s->src.wscale & PF_WSCALE_MASK); 3478126258Smlaier } 3479126258Smlaier if (th->th_flags & TH_FIN) 3480126258Smlaier s->src.seqhi++; 3481126258Smlaier s->dst.seqhi = 1; 3482126258Smlaier s->dst.max_win = 1; 3483126258Smlaier s->src.state = TCPS_SYN_SENT; 3484126258Smlaier s->dst.state = TCPS_CLOSED; 3485126258Smlaier s->timeout = PFTM_TCP_FIRST_PACKET; 3486223637Sbz break; 3487223637Sbz case IPPROTO_UDP: 3488223637Sbz s->src.state = PFUDPS_SINGLE; 3489223637Sbz s->dst.state = PFUDPS_NO_TRAFFIC; 3490223637Sbz s->timeout = PFTM_UDP_FIRST_PACKET; 3491223637Sbz break; 3492223637Sbz case IPPROTO_ICMP: 3493223637Sbz#ifdef INET6 3494223637Sbz case IPPROTO_ICMPV6: 3495223637Sbz#endif 3496223637Sbz s->timeout = PFTM_ICMP_FIRST_PACKET; 3497223637Sbz break; 3498223637Sbz default: 3499223637Sbz s->src.state = PFOTHERS_SINGLE; 3500223637Sbz s->dst.state = PFOTHERS_NO_TRAFFIC; 3501223637Sbz s->timeout = PFTM_OTHER_FIRST_PACKET; 3502223637Sbz } 3503223637Sbz 3504240233Sglebius s->creation = time_uptime; 3505240233Sglebius s->expire = time_uptime; 3506223637Sbz 3507223637Sbz if (sn != NULL) { 3508223637Sbz s->src_node = sn; 3509223637Sbz s->src_node->states++; 3510223637Sbz } 3511223637Sbz if (nsn != NULL) { 3512223637Sbz /* XXX We only modify one side for now. */ 3513223637Sbz PF_ACPY(&nsn->raddr, &nk->addr[1], pd->af); 3514223637Sbz s->nat_src_node = nsn; 3515223637Sbz s->nat_src_node->states++; 3516223637Sbz } 3517223637Sbz if (pd->proto == IPPROTO_TCP) { 3518126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && pf_normalize_tcp_init(m, 3519126258Smlaier off, pd, th, &s->src, &s->dst)) { 3520126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3521130613Smlaier pf_src_tree_remove_state(s); 3522145836Smlaier STATE_DEC_COUNTERS(s); 3523240233Sglebius uma_zfree(V_pf_state_z, s); 3524126258Smlaier return (PF_DROP); 3525126258Smlaier } 3526126258Smlaier if ((pd->flags & PFDESC_TCP_NORM) && s->src.scrub && 3527145836Smlaier pf_normalize_tcp_stateful(m, off, pd, &reason, th, s, 3528223637Sbz &s->src, &s->dst, rewrite)) { 3529145836Smlaier /* This really shouldn't happen!!! */ 3530145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 3531145836Smlaier ("pf_normalize_tcp_stateful failed on first pkt")); 3532126258Smlaier pf_normalize_tcp_cleanup(s); 3533130613Smlaier pf_src_tree_remove_state(s); 3534145836Smlaier STATE_DEC_COUNTERS(s); 3535240233Sglebius uma_zfree(V_pf_state_z, s); 3536223637Sbz return (PF_DROP); 3537126258Smlaier } 3538126258Smlaier } 3539223637Sbz s->direction = pd->dir; 3540126258Smlaier 3541240233Sglebius /* 3542240233Sglebius * sk/nk could already been setup by pf_get_translation(). 3543240233Sglebius */ 3544240233Sglebius if (nr == NULL) { 3545240233Sglebius KASSERT((sk == NULL && nk == NULL), ("%s: nr %p sk %p, nk %p", 3546240233Sglebius __func__, nr, sk, nk)); 3547240233Sglebius sk = pf_state_key_setup(pd, pd->src, pd->dst, sport, dport); 3548240233Sglebius if (sk == NULL) 3549240233Sglebius goto csfailed; 3550240233Sglebius nk = sk; 3551240233Sglebius } else 3552240233Sglebius KASSERT((sk != NULL && nk != NULL), ("%s: nr %p sk %p, nk %p", 3553240233Sglebius __func__, nr, sk, nk)); 3554126258Smlaier 3555240233Sglebius /* Swap sk/nk for PF_OUT. */ 3556240233Sglebius if (pf_state_insert(BOUND_IFACE(r, kif), 3557240233Sglebius (pd->dir == PF_IN) ? sk : nk, 3558240233Sglebius (pd->dir == PF_IN) ? nk : sk, s)) { 3559223637Sbz if (pd->proto == IPPROTO_TCP) 3560223637Sbz pf_normalize_tcp_cleanup(s); 3561223637Sbz REASON_SET(&reason, PFRES_STATEINS); 3562223637Sbz pf_src_tree_remove_state(s); 3563223637Sbz STATE_DEC_COUNTERS(s); 3564240233Sglebius uma_zfree(V_pf_state_z, s); 3565223637Sbz return (PF_DROP); 3566223637Sbz } else 3567223637Sbz *sm = s; 3568126258Smlaier 3569223637Sbz pf_set_rt_ifp(s, pd->src); /* needs s->state_key set */ 3570240233Sglebius if (tag > 0) 3571223637Sbz s->tag = tag; 3572223637Sbz if (pd->proto == IPPROTO_TCP && (th->th_flags & (TH_SYN|TH_ACK)) == 3573223637Sbz TH_SYN && r->keep_state == PF_STATE_SYNPROXY) { 3574223637Sbz s->src.state = PF_TCPS_PROXY_SRC; 3575223637Sbz /* undo NAT changes, if they have taken place */ 3576223637Sbz if (nr != NULL) { 3577223637Sbz struct pf_state_key *skt = s->key[PF_SK_WIRE]; 3578223637Sbz if (pd->dir == PF_OUT) 3579223637Sbz skt = s->key[PF_SK_STACK]; 3580223637Sbz PF_ACPY(pd->src, &skt->addr[pd->sidx], pd->af); 3581223637Sbz PF_ACPY(pd->dst, &skt->addr[pd->didx], pd->af); 3582223637Sbz if (pd->sport) 3583223637Sbz *pd->sport = skt->port[pd->sidx]; 3584223637Sbz if (pd->dport) 3585223637Sbz *pd->dport = skt->port[pd->didx]; 3586223637Sbz if (pd->proto_sum) 3587223637Sbz *pd->proto_sum = bproto_sum; 3588223637Sbz if (pd->ip_sum) 3589223637Sbz *pd->ip_sum = bip_sum; 3590223637Sbz m_copyback(m, off, hdrlen, pd->hdr.any); 3591223637Sbz } 3592223637Sbz s->src.seqhi = htonl(arc4random()); 3593223637Sbz /* Find mss option */ 3594231852Sbz int rtid = M_GETFIB(m); 3595223637Sbz mss = pf_get_mss(m, off, th->th_off, pd->af); 3596231852Sbz mss = pf_calc_mss(pd->src, pd->af, rtid, mss); 3597231852Sbz mss = pf_calc_mss(pd->dst, pd->af, rtid, mss); 3598223637Sbz s->src.mss = mss; 3599223637Sbz pf_send_tcp(NULL, r, pd->af, pd->dst, pd->src, th->th_dport, 3600223637Sbz th->th_sport, s->src.seqhi, ntohl(th->th_seq) + 1, 3601240233Sglebius TH_SYN|TH_ACK, 0, s->src.mss, 0, 1, 0, NULL); 3602223637Sbz REASON_SET(&reason, PFRES_SYNPROXY); 3603223637Sbz return (PF_SYNPROXY_DROP); 3604171168Smlaier } 3605165631Smlaier 3606223637Sbz return (PF_PASS); 3607126258Smlaier 3608223637Sbzcsfailed: 3609223637Sbz if (sk != NULL) 3610240233Sglebius uma_zfree(V_pf_state_key_z, sk); 3611223637Sbz if (nk != NULL) 3612240233Sglebius uma_zfree(V_pf_state_key_z, nk); 3613223637Sbz 3614240737Sglebius if (sn != NULL && sn->states == 0 && sn->expire == 0) 3615240233Sglebius pf_remove_src_node(sn); 3616240737Sglebius 3617240737Sglebius if (nsn != sn && nsn != NULL && nsn->states == 0 && nsn->expire == 0) 3618240233Sglebius pf_remove_src_node(nsn); 3619240737Sglebius 3620223637Sbz return (PF_DROP); 3621126258Smlaier} 3622126258Smlaier 3623240233Sglebiusstatic int 3624130613Smlaierpf_test_fragment(struct pf_rule **rm, int direction, struct pfi_kif *kif, 3625126258Smlaier struct mbuf *m, void *h, struct pf_pdesc *pd, struct pf_rule **am, 3626126258Smlaier struct pf_ruleset **rsm) 3627126258Smlaier{ 3628126258Smlaier struct pf_rule *r, *a = NULL; 3629126258Smlaier struct pf_ruleset *ruleset = NULL; 3630126258Smlaier sa_family_t af = pd->af; 3631126258Smlaier u_short reason; 3632126258Smlaier int tag = -1; 3633145836Smlaier int asd = 0; 3634171168Smlaier int match = 0; 3635240641Sglebius struct pf_anchor_stackframe anchor_stack[PF_ANCHOR_STACKSIZE]; 3636126258Smlaier 3637240233Sglebius PF_RULES_RASSERT(); 3638240233Sglebius 3639126258Smlaier r = TAILQ_FIRST(pf_main_ruleset.rules[PF_RULESET_FILTER].active.ptr); 3640126258Smlaier while (r != NULL) { 3641126258Smlaier r->evaluations++; 3642171168Smlaier if (pfi_kif_match(r->kif, kif) == r->ifnot) 3643126258Smlaier r = r->skip[PF_SKIP_IFP].ptr; 3644126258Smlaier else if (r->direction && r->direction != direction) 3645126258Smlaier r = r->skip[PF_SKIP_DIR].ptr; 3646126258Smlaier else if (r->af && r->af != af) 3647126258Smlaier r = r->skip[PF_SKIP_AF].ptr; 3648126258Smlaier else if (r->proto && r->proto != pd->proto) 3649126258Smlaier r = r->skip[PF_SKIP_PROTO].ptr; 3650171168Smlaier else if (PF_MISMATCHAW(&r->src.addr, pd->src, af, 3651231852Sbz r->src.neg, kif, M_GETFIB(m))) 3652126258Smlaier r = r->skip[PF_SKIP_SRC_ADDR].ptr; 3653171168Smlaier else if (PF_MISMATCHAW(&r->dst.addr, pd->dst, af, 3654231852Sbz r->dst.neg, NULL, M_GETFIB(m))) 3655126258Smlaier r = r->skip[PF_SKIP_DST_ADDR].ptr; 3656171168Smlaier else if (r->tos && !(r->tos == pd->tos)) 3657126258Smlaier r = TAILQ_NEXT(r, entries); 3658173815Smlaier else if (r->os_fingerprint != PF_OSFP_ANY) 3659126258Smlaier r = TAILQ_NEXT(r, entries); 3660173815Smlaier else if (pd->proto == IPPROTO_UDP && 3661173815Smlaier (r->src.port_op || r->dst.port_op)) 3662173815Smlaier r = TAILQ_NEXT(r, entries); 3663173815Smlaier else if (pd->proto == IPPROTO_TCP && 3664173815Smlaier (r->src.port_op || r->dst.port_op || r->flagset)) 3665173815Smlaier r = TAILQ_NEXT(r, entries); 3666173815Smlaier else if ((pd->proto == IPPROTO_ICMP || 3667173815Smlaier pd->proto == IPPROTO_ICMPV6) && 3668173815Smlaier (r->type || r->code)) 3669173815Smlaier r = TAILQ_NEXT(r, entries); 3670223637Sbz else if (r->prob && r->prob <= 3671223637Sbz (arc4random() % (UINT_MAX - 1) + 1)) 3672126258Smlaier r = TAILQ_NEXT(r, entries); 3673240233Sglebius else if (r->match_tag && !pf_match_tag(m, r, &tag, 3674240233Sglebius pd->pf_mtag ? pd->pf_mtag->tag : 0)) 3675126258Smlaier r = TAILQ_NEXT(r, entries); 3676126258Smlaier else { 3677126258Smlaier if (r->anchor == NULL) { 3678171168Smlaier match = 1; 3679126258Smlaier *rm = r; 3680126258Smlaier *am = a; 3681126258Smlaier *rsm = ruleset; 3682126258Smlaier if ((*rm)->quick) 3683126258Smlaier break; 3684126258Smlaier r = TAILQ_NEXT(r, entries); 3685126258Smlaier } else 3686240641Sglebius pf_step_into_anchor(anchor_stack, &asd, 3687240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, 3688240641Sglebius &match); 3689126258Smlaier } 3690240641Sglebius if (r == NULL && pf_step_out_of_anchor(anchor_stack, &asd, 3691240641Sglebius &ruleset, PF_RULESET_FILTER, &r, &a, &match)) 3692171168Smlaier break; 3693126258Smlaier } 3694126258Smlaier r = *rm; 3695126258Smlaier a = *am; 3696126258Smlaier ruleset = *rsm; 3697126258Smlaier 3698126258Smlaier REASON_SET(&reason, PFRES_MATCH); 3699130613Smlaier 3700126258Smlaier if (r->log) 3701240233Sglebius PFLOG_PACKET(kif, m, af, direction, reason, r, a, ruleset, pd, 3702240233Sglebius 1); 3703126258Smlaier 3704126258Smlaier if (r->action != PF_PASS) 3705126258Smlaier return (PF_DROP); 3706126258Smlaier 3707240233Sglebius if (tag > 0 && pf_tag_packet(m, pd, tag)) { 3708126258Smlaier REASON_SET(&reason, PFRES_MEMORY); 3709126258Smlaier return (PF_DROP); 3710126258Smlaier } 3711126258Smlaier 3712126258Smlaier return (PF_PASS); 3713126258Smlaier} 3714126258Smlaier 3715240233Sglebiusstatic int 3716200930Sdelphijpf_tcp_track_full(struct pf_state_peer *src, struct pf_state_peer *dst, 3717200930Sdelphij struct pf_state **state, struct pfi_kif *kif, struct mbuf *m, int off, 3718200930Sdelphij struct pf_pdesc *pd, u_short *reason, int *copyback) 3719126258Smlaier{ 3720223637Sbz struct tcphdr *th = pd->hdr.tcp; 3721223637Sbz u_int16_t win = ntohs(th->th_win); 3722223637Sbz u_int32_t ack, end, seq, orig_seq; 3723223637Sbz u_int8_t sws, dws; 3724223637Sbz int ackskew; 3725126258Smlaier 3726126258Smlaier if (src->wscale && dst->wscale && !(th->th_flags & TH_SYN)) { 3727126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3728126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3729126258Smlaier } else 3730126258Smlaier sws = dws = 0; 3731126258Smlaier 3732126258Smlaier /* 3733126258Smlaier * Sequence tracking algorithm from Guido van Rooij's paper: 3734126258Smlaier * http://www.madison-gurkha.com/publications/tcp_filtering/ 3735126258Smlaier * tcp_filtering.ps 3736126258Smlaier */ 3737126258Smlaier 3738145836Smlaier orig_seq = seq = ntohl(th->th_seq); 3739126258Smlaier if (src->seqlo == 0) { 3740126258Smlaier /* First packet from this end. Set its state */ 3741126258Smlaier 3742126258Smlaier if ((pd->flags & PFDESC_TCP_NORM || dst->scrub) && 3743126258Smlaier src->scrub == NULL) { 3744126258Smlaier if (pf_normalize_tcp_init(m, off, pd, th, src, dst)) { 3745126258Smlaier REASON_SET(reason, PFRES_MEMORY); 3746126258Smlaier return (PF_DROP); 3747126258Smlaier } 3748126258Smlaier } 3749126258Smlaier 3750126258Smlaier /* Deferred generation of sequence number modulator */ 3751126258Smlaier if (dst->seqdiff && !src->seqdiff) { 3752223637Sbz /* use random iss for the TCP server */ 3753223637Sbz while ((src->seqdiff = arc4random() - seq) == 0) 3754126258Smlaier ; 3755126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3756126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3757126258Smlaier src->seqdiff), 0); 3758126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3759200930Sdelphij *copyback = 1; 3760126258Smlaier } else { 3761126258Smlaier ack = ntohl(th->th_ack); 3762126258Smlaier } 3763126258Smlaier 3764126258Smlaier end = seq + pd->p_len; 3765126258Smlaier if (th->th_flags & TH_SYN) { 3766126258Smlaier end++; 3767126258Smlaier if (dst->wscale & PF_WSCALE_FLAG) { 3768126258Smlaier src->wscale = pf_get_wscale(m, off, th->th_off, 3769126258Smlaier pd->af); 3770126258Smlaier if (src->wscale & PF_WSCALE_FLAG) { 3771126258Smlaier /* Remove scale factor from initial 3772126258Smlaier * window */ 3773126258Smlaier sws = src->wscale & PF_WSCALE_MASK; 3774126258Smlaier win = ((u_int32_t)win + (1 << sws) - 1) 3775126258Smlaier >> sws; 3776126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 3777126258Smlaier } else { 3778126258Smlaier /* fixup other window */ 3779126258Smlaier dst->max_win <<= dst->wscale & 3780126258Smlaier PF_WSCALE_MASK; 3781126258Smlaier /* in case of a retrans SYN|ACK */ 3782126258Smlaier dst->wscale = 0; 3783126258Smlaier } 3784126258Smlaier } 3785126258Smlaier } 3786126258Smlaier if (th->th_flags & TH_FIN) 3787126258Smlaier end++; 3788126258Smlaier 3789126258Smlaier src->seqlo = seq; 3790126258Smlaier if (src->state < TCPS_SYN_SENT) 3791126258Smlaier src->state = TCPS_SYN_SENT; 3792126258Smlaier 3793126258Smlaier /* 3794126258Smlaier * May need to slide the window (seqhi may have been set by 3795126258Smlaier * the crappy stack check or if we picked up the connection 3796126258Smlaier * after establishment) 3797126258Smlaier */ 3798126258Smlaier if (src->seqhi == 1 || 3799126258Smlaier SEQ_GEQ(end + MAX(1, dst->max_win << dws), src->seqhi)) 3800126258Smlaier src->seqhi = end + MAX(1, dst->max_win << dws); 3801126258Smlaier if (win > src->max_win) 3802126258Smlaier src->max_win = win; 3803126258Smlaier 3804126258Smlaier } else { 3805126258Smlaier ack = ntohl(th->th_ack) - dst->seqdiff; 3806126258Smlaier if (src->seqdiff) { 3807126258Smlaier /* Modulate sequence numbers */ 3808126258Smlaier pf_change_a(&th->th_seq, &th->th_sum, htonl(seq + 3809126258Smlaier src->seqdiff), 0); 3810126258Smlaier pf_change_a(&th->th_ack, &th->th_sum, htonl(ack), 0); 3811200930Sdelphij *copyback = 1; 3812126258Smlaier } 3813126258Smlaier end = seq + pd->p_len; 3814126258Smlaier if (th->th_flags & TH_SYN) 3815126258Smlaier end++; 3816126258Smlaier if (th->th_flags & TH_FIN) 3817126258Smlaier end++; 3818126258Smlaier } 3819126258Smlaier 3820126258Smlaier if ((th->th_flags & TH_ACK) == 0) { 3821126258Smlaier /* Let it pass through the ack skew check */ 3822126258Smlaier ack = dst->seqlo; 3823126258Smlaier } else if ((ack == 0 && 3824126258Smlaier (th->th_flags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) || 3825126258Smlaier /* broken tcp stacks do not set ack */ 3826126258Smlaier (dst->state < TCPS_SYN_SENT)) { 3827126258Smlaier /* 3828126258Smlaier * Many stacks (ours included) will set the ACK number in an 3829126258Smlaier * FIN|ACK if the SYN times out -- no sequence to ACK. 3830126258Smlaier */ 3831126258Smlaier ack = dst->seqlo; 3832126258Smlaier } 3833126258Smlaier 3834126258Smlaier if (seq == end) { 3835126258Smlaier /* Ease sequencing restrictions on no data packets */ 3836126258Smlaier seq = src->seqlo; 3837126258Smlaier end = seq; 3838126258Smlaier } 3839126258Smlaier 3840126258Smlaier ackskew = dst->seqlo - ack; 3841126258Smlaier 3842171168Smlaier 3843171168Smlaier /* 3844171168Smlaier * Need to demodulate the sequence numbers in any TCP SACK options 3845171168Smlaier * (Selective ACK). We could optionally validate the SACK values 3846171168Smlaier * against the current ACK window, either forwards or backwards, but 3847171168Smlaier * I'm not confident that SACK has been implemented properly 3848171168Smlaier * everywhere. It wouldn't surprise me if several stacks accidently 3849171168Smlaier * SACK too far backwards of previously ACKed data. There really aren't 3850171168Smlaier * any security implications of bad SACKing unless the target stack 3851171168Smlaier * doesn't validate the option length correctly. Someone trying to 3852171168Smlaier * spoof into a TCP connection won't bother blindly sending SACK 3853171168Smlaier * options anyway. 3854171168Smlaier */ 3855171168Smlaier if (dst->seqdiff && (th->th_off << 2) > sizeof(struct tcphdr)) { 3856171168Smlaier if (pf_modulate_sack(m, off, pd, th, dst)) 3857200930Sdelphij *copyback = 1; 3858171168Smlaier } 3859171168Smlaier 3860171168Smlaier 3861223637Sbz#define MAXACKWINDOW (0xffff + 1500) /* 1500 is an arbitrary fudge factor */ 3862126258Smlaier if (SEQ_GEQ(src->seqhi, end) && 3863126258Smlaier /* Last octet inside other's window space */ 3864126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) && 3865126258Smlaier /* Retrans: not more than one window back */ 3866126258Smlaier (ackskew >= -MAXACKWINDOW) && 3867126258Smlaier /* Acking not more than one reassembled fragment backwards */ 3868145836Smlaier (ackskew <= (MAXACKWINDOW << sws)) && 3869126258Smlaier /* Acking not more than one window forward */ 3870145836Smlaier ((th->th_flags & TH_RST) == 0 || orig_seq == src->seqlo || 3871223637Sbz (orig_seq == src->seqlo + 1) || (orig_seq + 1 == src->seqlo) || 3872223637Sbz (pd->flags & PFDESC_IP_REAS) == 0)) { 3873171168Smlaier /* Require an exact/+1 sequence match on resets when possible */ 3874126258Smlaier 3875145836Smlaier if (dst->scrub || src->scrub) { 3876145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 3877200930Sdelphij *state, src, dst, copyback)) 3878145836Smlaier return (PF_DROP); 3879145836Smlaier } 3880145836Smlaier 3881126258Smlaier /* update max window */ 3882126258Smlaier if (src->max_win < win) 3883126258Smlaier src->max_win = win; 3884126258Smlaier /* synchronize sequencing */ 3885126258Smlaier if (SEQ_GT(end, src->seqlo)) 3886126258Smlaier src->seqlo = end; 3887126258Smlaier /* slide the window of what the other end can send */ 3888126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 3889126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 3890126258Smlaier 3891126258Smlaier 3892126258Smlaier /* update states */ 3893126258Smlaier if (th->th_flags & TH_SYN) 3894126258Smlaier if (src->state < TCPS_SYN_SENT) 3895126258Smlaier src->state = TCPS_SYN_SENT; 3896126258Smlaier if (th->th_flags & TH_FIN) 3897126258Smlaier if (src->state < TCPS_CLOSING) 3898126258Smlaier src->state = TCPS_CLOSING; 3899126258Smlaier if (th->th_flags & TH_ACK) { 3900145836Smlaier if (dst->state == TCPS_SYN_SENT) { 3901126258Smlaier dst->state = TCPS_ESTABLISHED; 3902145836Smlaier if (src->state == TCPS_ESTABLISHED && 3903145836Smlaier (*state)->src_node != NULL && 3904145836Smlaier pf_src_connlimit(state)) { 3905145836Smlaier REASON_SET(reason, PFRES_SRCLIMIT); 3906145836Smlaier return (PF_DROP); 3907145836Smlaier } 3908145836Smlaier } else if (dst->state == TCPS_CLOSING) 3909126258Smlaier dst->state = TCPS_FIN_WAIT_2; 3910126258Smlaier } 3911126258Smlaier if (th->th_flags & TH_RST) 3912126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 3913126258Smlaier 3914126258Smlaier /* update expire time */ 3915240233Sglebius (*state)->expire = time_uptime; 3916126258Smlaier if (src->state >= TCPS_FIN_WAIT_2 && 3917126258Smlaier dst->state >= TCPS_FIN_WAIT_2) 3918126258Smlaier (*state)->timeout = PFTM_TCP_CLOSED; 3919171168Smlaier else if (src->state >= TCPS_CLOSING && 3920171168Smlaier dst->state >= TCPS_CLOSING) 3921126258Smlaier (*state)->timeout = PFTM_TCP_FIN_WAIT; 3922126258Smlaier else if (src->state < TCPS_ESTABLISHED || 3923126258Smlaier dst->state < TCPS_ESTABLISHED) 3924126258Smlaier (*state)->timeout = PFTM_TCP_OPENING; 3925126258Smlaier else if (src->state >= TCPS_CLOSING || 3926126258Smlaier dst->state >= TCPS_CLOSING) 3927126258Smlaier (*state)->timeout = PFTM_TCP_CLOSING; 3928126258Smlaier else 3929126258Smlaier (*state)->timeout = PFTM_TCP_ESTABLISHED; 3930126258Smlaier 3931126258Smlaier /* Fall through to PASS packet */ 3932126258Smlaier 3933126258Smlaier } else if ((dst->state < TCPS_SYN_SENT || 3934126258Smlaier dst->state >= TCPS_FIN_WAIT_2 || 3935126258Smlaier src->state >= TCPS_FIN_WAIT_2) && 3936126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) && 3937126258Smlaier /* Within a window forward of the originating packet */ 3938126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW)) { 3939126258Smlaier /* Within a window backward of the originating packet */ 3940126258Smlaier 3941126258Smlaier /* 3942126258Smlaier * This currently handles three situations: 3943126258Smlaier * 1) Stupid stacks will shotgun SYNs before their peer 3944126258Smlaier * replies. 3945126258Smlaier * 2) When PF catches an already established stream (the 3946126258Smlaier * firewall rebooted, the state table was flushed, routes 3947126258Smlaier * changed...) 3948126258Smlaier * 3) Packets get funky immediately after the connection 3949126258Smlaier * closes (this should catch Solaris spurious ACK|FINs 3950126258Smlaier * that web servers like to spew after a close) 3951126258Smlaier * 3952126258Smlaier * This must be a little more careful than the above code 3953126258Smlaier * since packet floods will also be caught here. We don't 3954126258Smlaier * update the TTL here to mitigate the damage of a packet 3955126258Smlaier * flood and so the same code can handle awkward establishment 3956126258Smlaier * and a loosened connection close. 3957126258Smlaier * In the establishment case, a correct peer response will 3958126258Smlaier * validate the connection, go through the normal state code 3959126258Smlaier * and keep updating the state TTL. 3960126258Smlaier */ 3961126258Smlaier 3962223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 3963126258Smlaier printf("pf: loose state match: "); 3964126258Smlaier pf_print_state(*state); 3965126258Smlaier pf_print_flags(th->th_flags); 3966171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 3967223637Sbz "pkts=%llu:%llu dir=%s,%s\n", seq, orig_seq, ack, 3968223637Sbz pd->p_len, ackskew, (unsigned long long)(*state)->packets[0], 3969223637Sbz (unsigned long long)(*state)->packets[1], 3970223637Sbz pd->dir == PF_IN ? "in" : "out", 3971223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 3972126258Smlaier } 3973126258Smlaier 3974145836Smlaier if (dst->scrub || src->scrub) { 3975145836Smlaier if (pf_normalize_tcp_stateful(m, off, pd, reason, th, 3976200930Sdelphij *state, src, dst, copyback)) 3977145836Smlaier return (PF_DROP); 3978145836Smlaier } 3979145836Smlaier 3980126258Smlaier /* update max window */ 3981126258Smlaier if (src->max_win < win) 3982126258Smlaier src->max_win = win; 3983126258Smlaier /* synchronize sequencing */ 3984126258Smlaier if (SEQ_GT(end, src->seqlo)) 3985126258Smlaier src->seqlo = end; 3986126258Smlaier /* slide the window of what the other end can send */ 3987126258Smlaier if (SEQ_GEQ(ack + (win << sws), dst->seqhi)) 3988126258Smlaier dst->seqhi = ack + MAX((win << sws), 1); 3989126258Smlaier 3990126258Smlaier /* 3991126258Smlaier * Cannot set dst->seqhi here since this could be a shotgunned 3992126258Smlaier * SYN and not an already established connection. 3993126258Smlaier */ 3994126258Smlaier 3995126258Smlaier if (th->th_flags & TH_FIN) 3996126258Smlaier if (src->state < TCPS_CLOSING) 3997126258Smlaier src->state = TCPS_CLOSING; 3998126258Smlaier if (th->th_flags & TH_RST) 3999126258Smlaier src->state = dst->state = TCPS_TIME_WAIT; 4000126258Smlaier 4001126258Smlaier /* Fall through to PASS packet */ 4002126258Smlaier 4003126258Smlaier } else { 4004126258Smlaier if ((*state)->dst.state == TCPS_SYN_SENT && 4005126258Smlaier (*state)->src.state == TCPS_SYN_SENT) { 4006126258Smlaier /* Send RST for state mismatches during handshake */ 4007145836Smlaier if (!(th->th_flags & TH_RST)) 4008223637Sbz pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4009126258Smlaier pd->dst, pd->src, th->th_dport, 4010145836Smlaier th->th_sport, ntohl(th->th_ack), 0, 4011145836Smlaier TH_RST, 0, 0, 4012171168Smlaier (*state)->rule.ptr->return_ttl, 1, 0, 4013240233Sglebius kif->pfik_ifp); 4014126258Smlaier src->seqlo = 0; 4015126258Smlaier src->seqhi = 1; 4016126258Smlaier src->max_win = 1; 4017223637Sbz } else if (V_pf_status.debug >= PF_DEBUG_MISC) { 4018126258Smlaier printf("pf: BAD state: "); 4019126258Smlaier pf_print_state(*state); 4020126258Smlaier pf_print_flags(th->th_flags); 4021171168Smlaier printf(" seq=%u (%u) ack=%u len=%u ackskew=%d " 4022171168Smlaier "pkts=%llu:%llu dir=%s,%s\n", 4023171168Smlaier seq, orig_seq, ack, pd->p_len, ackskew, 4024171168Smlaier (unsigned long long)(*state)->packets[0], 4025171168Smlaier (unsigned long long)(*state)->packets[1], 4026223637Sbz pd->dir == PF_IN ? "in" : "out", 4027223637Sbz pd->dir == (*state)->direction ? "fwd" : "rev"); 4028126258Smlaier printf("pf: State failure on: %c %c %c %c | %c %c\n", 4029126258Smlaier SEQ_GEQ(src->seqhi, end) ? ' ' : '1', 4030126258Smlaier SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)) ? 4031126258Smlaier ' ': '2', 4032126258Smlaier (ackskew >= -MAXACKWINDOW) ? ' ' : '3', 4033126258Smlaier (ackskew <= (MAXACKWINDOW << sws)) ? ' ' : '4', 4034126258Smlaier SEQ_GEQ(src->seqhi + MAXACKWINDOW, end) ?' ' :'5', 4035126258Smlaier SEQ_GEQ(seq, src->seqlo - MAXACKWINDOW) ?' ' :'6'); 4036126258Smlaier } 4037145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4038126258Smlaier return (PF_DROP); 4039126258Smlaier } 4040126258Smlaier 4041200930Sdelphij return (PF_PASS); 4042200930Sdelphij} 4043126258Smlaier 4044240233Sglebiusstatic int 4045200930Sdelphijpf_tcp_track_sloppy(struct pf_state_peer *src, struct pf_state_peer *dst, 4046200930Sdelphij struct pf_state **state, struct pf_pdesc *pd, u_short *reason) 4047200930Sdelphij{ 4048200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4049200930Sdelphij 4050200930Sdelphij if (th->th_flags & TH_SYN) 4051200930Sdelphij if (src->state < TCPS_SYN_SENT) 4052200930Sdelphij src->state = TCPS_SYN_SENT; 4053200930Sdelphij if (th->th_flags & TH_FIN) 4054200930Sdelphij if (src->state < TCPS_CLOSING) 4055200930Sdelphij src->state = TCPS_CLOSING; 4056200930Sdelphij if (th->th_flags & TH_ACK) { 4057200930Sdelphij if (dst->state == TCPS_SYN_SENT) { 4058200930Sdelphij dst->state = TCPS_ESTABLISHED; 4059200930Sdelphij if (src->state == TCPS_ESTABLISHED && 4060200930Sdelphij (*state)->src_node != NULL && 4061200930Sdelphij pf_src_connlimit(state)) { 4062200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4063200930Sdelphij return (PF_DROP); 4064200930Sdelphij } 4065200930Sdelphij } else if (dst->state == TCPS_CLOSING) { 4066200930Sdelphij dst->state = TCPS_FIN_WAIT_2; 4067200930Sdelphij } else if (src->state == TCPS_SYN_SENT && 4068200930Sdelphij dst->state < TCPS_SYN_SENT) { 4069200930Sdelphij /* 4070200930Sdelphij * Handle a special sloppy case where we only see one 4071200930Sdelphij * half of the connection. If there is a ACK after 4072200930Sdelphij * the initial SYN without ever seeing a packet from 4073200930Sdelphij * the destination, set the connection to established. 4074200930Sdelphij */ 4075200930Sdelphij dst->state = src->state = TCPS_ESTABLISHED; 4076200930Sdelphij if ((*state)->src_node != NULL && 4077200930Sdelphij pf_src_connlimit(state)) { 4078200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4079200930Sdelphij return (PF_DROP); 4080200930Sdelphij } 4081200930Sdelphij } else if (src->state == TCPS_CLOSING && 4082200930Sdelphij dst->state == TCPS_ESTABLISHED && 4083200930Sdelphij dst->seqlo == 0) { 4084200930Sdelphij /* 4085200930Sdelphij * Handle the closing of half connections where we 4086200930Sdelphij * don't see the full bidirectional FIN/ACK+ACK 4087200930Sdelphij * handshake. 4088200930Sdelphij */ 4089200930Sdelphij dst->state = TCPS_CLOSING; 4090200930Sdelphij } 4091200930Sdelphij } 4092200930Sdelphij if (th->th_flags & TH_RST) 4093200930Sdelphij src->state = dst->state = TCPS_TIME_WAIT; 4094200930Sdelphij 4095200930Sdelphij /* update expire time */ 4096240233Sglebius (*state)->expire = time_uptime; 4097200930Sdelphij if (src->state >= TCPS_FIN_WAIT_2 && 4098200930Sdelphij dst->state >= TCPS_FIN_WAIT_2) 4099200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSED; 4100200930Sdelphij else if (src->state >= TCPS_CLOSING && 4101200930Sdelphij dst->state >= TCPS_CLOSING) 4102200930Sdelphij (*state)->timeout = PFTM_TCP_FIN_WAIT; 4103200930Sdelphij else if (src->state < TCPS_ESTABLISHED || 4104200930Sdelphij dst->state < TCPS_ESTABLISHED) 4105200930Sdelphij (*state)->timeout = PFTM_TCP_OPENING; 4106200930Sdelphij else if (src->state >= TCPS_CLOSING || 4107200930Sdelphij dst->state >= TCPS_CLOSING) 4108200930Sdelphij (*state)->timeout = PFTM_TCP_CLOSING; 4109200930Sdelphij else 4110200930Sdelphij (*state)->timeout = PFTM_TCP_ESTABLISHED; 4111200930Sdelphij 4112200930Sdelphij return (PF_PASS); 4113200930Sdelphij} 4114200930Sdelphij 4115240233Sglebiusstatic int 4116200930Sdelphijpf_test_state_tcp(struct pf_state **state, int direction, struct pfi_kif *kif, 4117200930Sdelphij struct mbuf *m, int off, void *h, struct pf_pdesc *pd, 4118200930Sdelphij u_short *reason) 4119200930Sdelphij{ 4120223637Sbz struct pf_state_key_cmp key; 4121200930Sdelphij struct tcphdr *th = pd->hdr.tcp; 4122200930Sdelphij int copyback = 0; 4123200930Sdelphij struct pf_state_peer *src, *dst; 4124223637Sbz struct pf_state_key *sk; 4125200930Sdelphij 4126240233Sglebius bzero(&key, sizeof(key)); 4127200930Sdelphij key.af = pd->af; 4128200930Sdelphij key.proto = IPPROTO_TCP; 4129223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4130223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4131223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4132223637Sbz key.port[0] = th->th_sport; 4133223637Sbz key.port[1] = th->th_dport; 4134223637Sbz } else { /* stack side, reverse */ 4135223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4136223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4137223637Sbz key.port[1] = th->th_sport; 4138223637Sbz key.port[0] = th->th_dport; 4139200930Sdelphij } 4140200930Sdelphij 4141240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4142200930Sdelphij 4143200930Sdelphij if (direction == (*state)->direction) { 4144200930Sdelphij src = &(*state)->src; 4145200930Sdelphij dst = &(*state)->dst; 4146200930Sdelphij } else { 4147200930Sdelphij src = &(*state)->dst; 4148200930Sdelphij dst = &(*state)->src; 4149200930Sdelphij } 4150200930Sdelphij 4151223637Sbz sk = (*state)->key[pd->didx]; 4152223637Sbz 4153200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_SRC) { 4154200930Sdelphij if (direction != (*state)->direction) { 4155200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4156200930Sdelphij return (PF_SYNPROXY_DROP); 4157200930Sdelphij } 4158200930Sdelphij if (th->th_flags & TH_SYN) { 4159200930Sdelphij if (ntohl(th->th_seq) != (*state)->src.seqlo) { 4160200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4161200930Sdelphij return (PF_DROP); 4162200930Sdelphij } 4163200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4164200930Sdelphij pd->src, th->th_dport, th->th_sport, 4165200930Sdelphij (*state)->src.seqhi, ntohl(th->th_seq) + 1, 4166240233Sglebius TH_SYN|TH_ACK, 0, (*state)->src.mss, 0, 1, 0, NULL); 4167200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4168200930Sdelphij return (PF_SYNPROXY_DROP); 4169200930Sdelphij } else if (!(th->th_flags & TH_ACK) || 4170200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4171200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4172200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4173200930Sdelphij return (PF_DROP); 4174200930Sdelphij } else if ((*state)->src_node != NULL && 4175200930Sdelphij pf_src_connlimit(state)) { 4176200930Sdelphij REASON_SET(reason, PFRES_SRCLIMIT); 4177200930Sdelphij return (PF_DROP); 4178200930Sdelphij } else 4179200930Sdelphij (*state)->src.state = PF_TCPS_PROXY_DST; 4180200930Sdelphij } 4181200930Sdelphij if ((*state)->src.state == PF_TCPS_PROXY_DST) { 4182200930Sdelphij if (direction == (*state)->direction) { 4183200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) != TH_ACK) || 4184200930Sdelphij (ntohl(th->th_ack) != (*state)->src.seqhi + 1) || 4185200930Sdelphij (ntohl(th->th_seq) != (*state)->src.seqlo + 1)) { 4186200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4187200930Sdelphij return (PF_DROP); 4188200930Sdelphij } 4189200930Sdelphij (*state)->src.max_win = MAX(ntohs(th->th_win), 1); 4190200930Sdelphij if ((*state)->dst.seqhi == 1) 4191200930Sdelphij (*state)->dst.seqhi = htonl(arc4random()); 4192200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4193223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4194223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4195200930Sdelphij (*state)->dst.seqhi, 0, TH_SYN, 0, 4196240233Sglebius (*state)->src.mss, 0, 0, (*state)->tag, NULL); 4197200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4198200930Sdelphij return (PF_SYNPROXY_DROP); 4199200930Sdelphij } else if (((th->th_flags & (TH_SYN|TH_ACK)) != 4200200930Sdelphij (TH_SYN|TH_ACK)) || 4201200930Sdelphij (ntohl(th->th_ack) != (*state)->dst.seqhi + 1)) { 4202200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4203200930Sdelphij return (PF_DROP); 4204200930Sdelphij } else { 4205200930Sdelphij (*state)->dst.max_win = MAX(ntohs(th->th_win), 1); 4206200930Sdelphij (*state)->dst.seqlo = ntohl(th->th_seq); 4207200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, pd->dst, 4208200930Sdelphij pd->src, th->th_dport, th->th_sport, 4209200930Sdelphij ntohl(th->th_ack), ntohl(th->th_seq) + 1, 4210200930Sdelphij TH_ACK, (*state)->src.max_win, 0, 0, 0, 4211240233Sglebius (*state)->tag, NULL); 4212200930Sdelphij pf_send_tcp(NULL, (*state)->rule.ptr, pd->af, 4213223637Sbz &sk->addr[pd->sidx], &sk->addr[pd->didx], 4214223637Sbz sk->port[pd->sidx], sk->port[pd->didx], 4215200930Sdelphij (*state)->src.seqhi + 1, (*state)->src.seqlo + 1, 4216240233Sglebius TH_ACK, (*state)->dst.max_win, 0, 0, 1, 0, NULL); 4217200930Sdelphij (*state)->src.seqdiff = (*state)->dst.seqhi - 4218200930Sdelphij (*state)->src.seqlo; 4219200930Sdelphij (*state)->dst.seqdiff = (*state)->src.seqhi - 4220200930Sdelphij (*state)->dst.seqlo; 4221200930Sdelphij (*state)->src.seqhi = (*state)->src.seqlo + 4222200930Sdelphij (*state)->dst.max_win; 4223200930Sdelphij (*state)->dst.seqhi = (*state)->dst.seqlo + 4224200930Sdelphij (*state)->src.max_win; 4225200930Sdelphij (*state)->src.wscale = (*state)->dst.wscale = 0; 4226200930Sdelphij (*state)->src.state = (*state)->dst.state = 4227200930Sdelphij TCPS_ESTABLISHED; 4228200930Sdelphij REASON_SET(reason, PFRES_SYNPROXY); 4229200930Sdelphij return (PF_SYNPROXY_DROP); 4230200930Sdelphij } 4231200930Sdelphij } 4232200930Sdelphij 4233200930Sdelphij if (((th->th_flags & (TH_SYN|TH_ACK)) == TH_SYN) && 4234200930Sdelphij dst->state >= TCPS_FIN_WAIT_2 && 4235200930Sdelphij src->state >= TCPS_FIN_WAIT_2) { 4236223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4237200930Sdelphij printf("pf: state reuse "); 4238200930Sdelphij pf_print_state(*state); 4239200930Sdelphij pf_print_flags(th->th_flags); 4240200930Sdelphij printf("\n"); 4241200930Sdelphij } 4242200930Sdelphij /* XXX make sure it's the same direction ?? */ 4243200930Sdelphij (*state)->src.state = (*state)->dst.state = TCPS_CLOSED; 4244240233Sglebius pf_unlink_state(*state, PF_ENTER_LOCKED); 4245200930Sdelphij *state = NULL; 4246200930Sdelphij return (PF_DROP); 4247200930Sdelphij } 4248200930Sdelphij 4249200930Sdelphij if ((*state)->state_flags & PFSTATE_SLOPPY) { 4250200930Sdelphij if (pf_tcp_track_sloppy(src, dst, state, pd, reason) == PF_DROP) 4251200930Sdelphij return (PF_DROP); 4252200930Sdelphij } else { 4253200930Sdelphij if (pf_tcp_track_full(src, dst, state, kif, m, off, pd, reason, 4254200930Sdelphij ©back) == PF_DROP) 4255200930Sdelphij return (PF_DROP); 4256200930Sdelphij } 4257200930Sdelphij 4258126258Smlaier /* translate source/destination address, if necessary */ 4259223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4260223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4261223637Sbz 4262223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4263223637Sbz nk->port[pd->sidx] != th->th_sport) 4264126258Smlaier pf_change_ap(pd->src, &th->th_sport, pd->ip_sum, 4265223637Sbz &th->th_sum, &nk->addr[pd->sidx], 4266223637Sbz nk->port[pd->sidx], 0, pd->af); 4267223637Sbz 4268223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4269223637Sbz nk->port[pd->didx] != th->th_dport) 4270126258Smlaier pf_change_ap(pd->dst, &th->th_dport, pd->ip_sum, 4271223637Sbz &th->th_sum, &nk->addr[pd->didx], 4272223637Sbz nk->port[pd->didx], 0, pd->af); 4273223637Sbz copyback = 1; 4274126258Smlaier } 4275126258Smlaier 4276223637Sbz /* Copyback sequence modulation or stateful scrub changes if needed */ 4277223637Sbz if (copyback) 4278223637Sbz m_copyback(m, off, sizeof(*th), (caddr_t)th); 4279223637Sbz 4280126258Smlaier return (PF_PASS); 4281126258Smlaier} 4282126258Smlaier 4283240233Sglebiusstatic int 4284130613Smlaierpf_test_state_udp(struct pf_state **state, int direction, struct pfi_kif *kif, 4285130613Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd) 4286126258Smlaier{ 4287126258Smlaier struct pf_state_peer *src, *dst; 4288223637Sbz struct pf_state_key_cmp key; 4289126258Smlaier struct udphdr *uh = pd->hdr.udp; 4290126258Smlaier 4291240233Sglebius bzero(&key, sizeof(key)); 4292126258Smlaier key.af = pd->af; 4293126258Smlaier key.proto = IPPROTO_UDP; 4294223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4295223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4296223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4297223637Sbz key.port[0] = uh->uh_sport; 4298223637Sbz key.port[1] = uh->uh_dport; 4299223637Sbz } else { /* stack side, reverse */ 4300223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4301223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4302223637Sbz key.port[1] = uh->uh_sport; 4303223637Sbz key.port[0] = uh->uh_dport; 4304130613Smlaier } 4305126258Smlaier 4306240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4307126258Smlaier 4308126258Smlaier if (direction == (*state)->direction) { 4309126258Smlaier src = &(*state)->src; 4310126258Smlaier dst = &(*state)->dst; 4311126258Smlaier } else { 4312126258Smlaier src = &(*state)->dst; 4313126258Smlaier dst = &(*state)->src; 4314126258Smlaier } 4315126258Smlaier 4316126258Smlaier /* update states */ 4317126258Smlaier if (src->state < PFUDPS_SINGLE) 4318126258Smlaier src->state = PFUDPS_SINGLE; 4319126258Smlaier if (dst->state == PFUDPS_SINGLE) 4320126258Smlaier dst->state = PFUDPS_MULTIPLE; 4321126258Smlaier 4322126258Smlaier /* update expire time */ 4323240233Sglebius (*state)->expire = time_uptime; 4324126258Smlaier if (src->state == PFUDPS_MULTIPLE && dst->state == PFUDPS_MULTIPLE) 4325126258Smlaier (*state)->timeout = PFTM_UDP_MULTIPLE; 4326126258Smlaier else 4327126258Smlaier (*state)->timeout = PFTM_UDP_SINGLE; 4328126258Smlaier 4329126258Smlaier /* translate source/destination address, if necessary */ 4330223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4331223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4332223637Sbz 4333223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], pd->af) || 4334223637Sbz nk->port[pd->sidx] != uh->uh_sport) 4335126258Smlaier pf_change_ap(pd->src, &uh->uh_sport, pd->ip_sum, 4336223637Sbz &uh->uh_sum, &nk->addr[pd->sidx], 4337223637Sbz nk->port[pd->sidx], 1, pd->af); 4338223637Sbz 4339223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], pd->af) || 4340223637Sbz nk->port[pd->didx] != uh->uh_dport) 4341126258Smlaier pf_change_ap(pd->dst, &uh->uh_dport, pd->ip_sum, 4342223637Sbz &uh->uh_sum, &nk->addr[pd->didx], 4343223637Sbz nk->port[pd->didx], 1, pd->af); 4344126261Smlaier m_copyback(m, off, sizeof(*uh), (caddr_t)uh); 4345126258Smlaier } 4346126258Smlaier 4347126258Smlaier return (PF_PASS); 4348126258Smlaier} 4349126258Smlaier 4350240233Sglebiusstatic int 4351130613Smlaierpf_test_state_icmp(struct pf_state **state, int direction, struct pfi_kif *kif, 4352145836Smlaier struct mbuf *m, int off, void *h, struct pf_pdesc *pd, u_short *reason) 4353126258Smlaier{ 4354223637Sbz struct pf_addr *saddr = pd->src, *daddr = pd->dst; 4355223637Sbz u_int16_t icmpid = 0, *icmpsum; 4356223637Sbz u_int8_t icmptype; 4357130613Smlaier int state_icmp = 0; 4358223637Sbz struct pf_state_key_cmp key; 4359126258Smlaier 4360240233Sglebius bzero(&key, sizeof(key)); 4361126258Smlaier switch (pd->proto) { 4362126258Smlaier#ifdef INET 4363126258Smlaier case IPPROTO_ICMP: 4364126258Smlaier icmptype = pd->hdr.icmp->icmp_type; 4365126258Smlaier icmpid = pd->hdr.icmp->icmp_id; 4366126258Smlaier icmpsum = &pd->hdr.icmp->icmp_cksum; 4367126258Smlaier 4368126258Smlaier if (icmptype == ICMP_UNREACH || 4369126258Smlaier icmptype == ICMP_SOURCEQUENCH || 4370126258Smlaier icmptype == ICMP_REDIRECT || 4371126258Smlaier icmptype == ICMP_TIMXCEED || 4372126258Smlaier icmptype == ICMP_PARAMPROB) 4373126258Smlaier state_icmp++; 4374126258Smlaier break; 4375126258Smlaier#endif /* INET */ 4376126258Smlaier#ifdef INET6 4377126258Smlaier case IPPROTO_ICMPV6: 4378126258Smlaier icmptype = pd->hdr.icmp6->icmp6_type; 4379126258Smlaier icmpid = pd->hdr.icmp6->icmp6_id; 4380126258Smlaier icmpsum = &pd->hdr.icmp6->icmp6_cksum; 4381126258Smlaier 4382126258Smlaier if (icmptype == ICMP6_DST_UNREACH || 4383126258Smlaier icmptype == ICMP6_PACKET_TOO_BIG || 4384126258Smlaier icmptype == ICMP6_TIME_EXCEEDED || 4385126258Smlaier icmptype == ICMP6_PARAM_PROB) 4386126258Smlaier state_icmp++; 4387126258Smlaier break; 4388126258Smlaier#endif /* INET6 */ 4389126258Smlaier } 4390126258Smlaier 4391126258Smlaier if (!state_icmp) { 4392126258Smlaier 4393126258Smlaier /* 4394126258Smlaier * ICMP query/reply message not related to a TCP/UDP packet. 4395126258Smlaier * Search for an ICMP state. 4396126258Smlaier */ 4397126258Smlaier key.af = pd->af; 4398126258Smlaier key.proto = pd->proto; 4399223637Sbz key.port[0] = key.port[1] = icmpid; 4400223637Sbz if (direction == PF_IN) { /* wire side, straight */ 4401223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4402223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4403223637Sbz } else { /* stack side, reverse */ 4404223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4405223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4406130613Smlaier } 4407126258Smlaier 4408240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4409126258Smlaier 4410240233Sglebius (*state)->expire = time_uptime; 4411126258Smlaier (*state)->timeout = PFTM_ICMP_ERROR_REPLY; 4412126258Smlaier 4413126258Smlaier /* translate source/destination address, if necessary */ 4414223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4415223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4416223637Sbz 4417223637Sbz switch (pd->af) { 4418126258Smlaier#ifdef INET 4419223637Sbz case AF_INET: 4420223637Sbz if (PF_ANEQ(pd->src, 4421223637Sbz &nk->addr[pd->sidx], AF_INET)) 4422126258Smlaier pf_change_a(&saddr->v4.s_addr, 4423126258Smlaier pd->ip_sum, 4424223637Sbz nk->addr[pd->sidx].v4.s_addr, 0); 4425223637Sbz 4426223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], 4427223637Sbz AF_INET)) 4428223637Sbz pf_change_a(&daddr->v4.s_addr, 4429223637Sbz pd->ip_sum, 4430223637Sbz nk->addr[pd->didx].v4.s_addr, 0); 4431223637Sbz 4432223637Sbz if (nk->port[0] != 4433223637Sbz pd->hdr.icmp->icmp_id) { 4434149884Smlaier pd->hdr.icmp->icmp_cksum = 4435149884Smlaier pf_cksum_fixup( 4436149884Smlaier pd->hdr.icmp->icmp_cksum, icmpid, 4437223637Sbz nk->port[pd->sidx], 0); 4438149884Smlaier pd->hdr.icmp->icmp_id = 4439223637Sbz nk->port[pd->sidx]; 4440223637Sbz } 4441223637Sbz 4442223637Sbz m_copyback(m, off, ICMP_MINLEN, 4443240233Sglebius (caddr_t )pd->hdr.icmp); 4444223637Sbz break; 4445126258Smlaier#endif /* INET */ 4446126258Smlaier#ifdef INET6 4447223637Sbz case AF_INET6: 4448223637Sbz if (PF_ANEQ(pd->src, 4449223637Sbz &nk->addr[pd->sidx], AF_INET6)) 4450126258Smlaier pf_change_a6(saddr, 4451126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4452223637Sbz &nk->addr[pd->sidx], 0); 4453223637Sbz 4454223637Sbz if (PF_ANEQ(pd->dst, 4455223637Sbz &nk->addr[pd->didx], AF_INET6)) 4456126258Smlaier pf_change_a6(daddr, 4457126258Smlaier &pd->hdr.icmp6->icmp6_cksum, 4458223637Sbz &nk->addr[pd->didx], 0); 4459223637Sbz 4460240233Sglebius m_copyback(m, off, sizeof(struct icmp6_hdr), 4461240233Sglebius (caddr_t )pd->hdr.icmp6); 4462223637Sbz break; 4463126258Smlaier#endif /* INET6 */ 4464126258Smlaier } 4465126258Smlaier } 4466126258Smlaier return (PF_PASS); 4467126258Smlaier 4468126258Smlaier } else { 4469126258Smlaier /* 4470126258Smlaier * ICMP error message in response to a TCP/UDP packet. 4471126258Smlaier * Extract the inner TCP/UDP header and search for that state. 4472126258Smlaier */ 4473126258Smlaier 4474126258Smlaier struct pf_pdesc pd2; 4475223637Sbz bzero(&pd2, sizeof pd2); 4476126258Smlaier#ifdef INET 4477126258Smlaier struct ip h2; 4478126258Smlaier#endif /* INET */ 4479126258Smlaier#ifdef INET6 4480126258Smlaier struct ip6_hdr h2_6; 4481126258Smlaier int terminal = 0; 4482126258Smlaier#endif /* INET6 */ 4483223637Sbz int ipoff2 = 0; 4484223637Sbz int off2 = 0; 4485126258Smlaier 4486126258Smlaier pd2.af = pd->af; 4487223637Sbz /* Payload packet is from the opposite direction. */ 4488223637Sbz pd2.sidx = (direction == PF_IN) ? 1 : 0; 4489223637Sbz pd2.didx = (direction == PF_IN) ? 0 : 1; 4490126258Smlaier switch (pd->af) { 4491126258Smlaier#ifdef INET 4492126258Smlaier case AF_INET: 4493126258Smlaier /* offset of h2 in mbuf chain */ 4494126258Smlaier ipoff2 = off + ICMP_MINLEN; 4495126258Smlaier 4496126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2, sizeof(h2), 4497145836Smlaier NULL, reason, pd2.af)) { 4498126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4499126258Smlaier ("pf: ICMP error message too short " 4500126258Smlaier "(ip)\n")); 4501126258Smlaier return (PF_DROP); 4502126258Smlaier } 4503126258Smlaier /* 4504126258Smlaier * ICMP error messages don't refer to non-first 4505126258Smlaier * fragments 4506126258Smlaier */ 4507145836Smlaier if (h2.ip_off & htons(IP_OFFMASK)) { 4508145836Smlaier REASON_SET(reason, PFRES_FRAG); 4509126258Smlaier return (PF_DROP); 4510145836Smlaier } 4511126258Smlaier 4512126258Smlaier /* offset of protocol header that follows h2 */ 4513126258Smlaier off2 = ipoff2 + (h2.ip_hl << 2); 4514126258Smlaier 4515126258Smlaier pd2.proto = h2.ip_p; 4516126258Smlaier pd2.src = (struct pf_addr *)&h2.ip_src; 4517126258Smlaier pd2.dst = (struct pf_addr *)&h2.ip_dst; 4518126258Smlaier pd2.ip_sum = &h2.ip_sum; 4519126258Smlaier break; 4520126258Smlaier#endif /* INET */ 4521126258Smlaier#ifdef INET6 4522126258Smlaier case AF_INET6: 4523126258Smlaier ipoff2 = off + sizeof(struct icmp6_hdr); 4524126258Smlaier 4525126258Smlaier if (!pf_pull_hdr(m, ipoff2, &h2_6, sizeof(h2_6), 4526145836Smlaier NULL, reason, pd2.af)) { 4527126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4528126258Smlaier ("pf: ICMP error message too short " 4529126258Smlaier "(ip6)\n")); 4530126258Smlaier return (PF_DROP); 4531126258Smlaier } 4532126258Smlaier pd2.proto = h2_6.ip6_nxt; 4533126258Smlaier pd2.src = (struct pf_addr *)&h2_6.ip6_src; 4534126258Smlaier pd2.dst = (struct pf_addr *)&h2_6.ip6_dst; 4535126258Smlaier pd2.ip_sum = NULL; 4536126258Smlaier off2 = ipoff2 + sizeof(h2_6); 4537126258Smlaier do { 4538126258Smlaier switch (pd2.proto) { 4539126258Smlaier case IPPROTO_FRAGMENT: 4540126258Smlaier /* 4541126258Smlaier * ICMPv6 error messages for 4542126258Smlaier * non-first fragments 4543126258Smlaier */ 4544145836Smlaier REASON_SET(reason, PFRES_FRAG); 4545126258Smlaier return (PF_DROP); 4546126258Smlaier case IPPROTO_AH: 4547126258Smlaier case IPPROTO_HOPOPTS: 4548126258Smlaier case IPPROTO_ROUTING: 4549126258Smlaier case IPPROTO_DSTOPTS: { 4550126258Smlaier /* get next header and header length */ 4551126258Smlaier struct ip6_ext opt6; 4552126258Smlaier 4553126258Smlaier if (!pf_pull_hdr(m, off2, &opt6, 4554145836Smlaier sizeof(opt6), NULL, reason, 4555145836Smlaier pd2.af)) { 4556126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4557126258Smlaier ("pf: ICMPv6 short opt\n")); 4558126258Smlaier return (PF_DROP); 4559126258Smlaier } 4560126258Smlaier if (pd2.proto == IPPROTO_AH) 4561126258Smlaier off2 += (opt6.ip6e_len + 2) * 4; 4562126258Smlaier else 4563126258Smlaier off2 += (opt6.ip6e_len + 1) * 8; 4564126258Smlaier pd2.proto = opt6.ip6e_nxt; 4565126258Smlaier /* goto the next header */ 4566126258Smlaier break; 4567126258Smlaier } 4568126258Smlaier default: 4569126258Smlaier terminal++; 4570126258Smlaier break; 4571126258Smlaier } 4572126258Smlaier } while (!terminal); 4573126258Smlaier break; 4574126258Smlaier#endif /* INET6 */ 4575126258Smlaier } 4576126258Smlaier 4577126258Smlaier switch (pd2.proto) { 4578126258Smlaier case IPPROTO_TCP: { 4579126258Smlaier struct tcphdr th; 4580126258Smlaier u_int32_t seq; 4581126258Smlaier struct pf_state_peer *src, *dst; 4582126258Smlaier u_int8_t dws; 4583128129Smlaier int copyback = 0; 4584126258Smlaier 4585126258Smlaier /* 4586126258Smlaier * Only the first 8 bytes of the TCP header can be 4587126258Smlaier * expected. Don't access any TCP header fields after 4588126258Smlaier * th_seq, an ackskew test is not possible. 4589126258Smlaier */ 4590145836Smlaier if (!pf_pull_hdr(m, off2, &th, 8, NULL, reason, 4591145836Smlaier pd2.af)) { 4592126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4593126258Smlaier ("pf: ICMP error message too short " 4594126258Smlaier "(tcp)\n")); 4595126258Smlaier return (PF_DROP); 4596126258Smlaier } 4597126258Smlaier 4598126258Smlaier key.af = pd2.af; 4599126258Smlaier key.proto = IPPROTO_TCP; 4600223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4601223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4602223637Sbz key.port[pd2.sidx] = th.th_sport; 4603223637Sbz key.port[pd2.didx] = th.th_dport; 4604126258Smlaier 4605240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4606126258Smlaier 4607126258Smlaier if (direction == (*state)->direction) { 4608126258Smlaier src = &(*state)->dst; 4609126258Smlaier dst = &(*state)->src; 4610126258Smlaier } else { 4611126258Smlaier src = &(*state)->src; 4612126258Smlaier dst = &(*state)->dst; 4613126258Smlaier } 4614126258Smlaier 4615171929Sdhartmei if (src->wscale && dst->wscale) 4616126258Smlaier dws = dst->wscale & PF_WSCALE_MASK; 4617126258Smlaier else 4618126258Smlaier dws = 0; 4619126258Smlaier 4620126258Smlaier /* Demodulate sequence number */ 4621126258Smlaier seq = ntohl(th.th_seq) - src->seqdiff; 4622128129Smlaier if (src->seqdiff) { 4623128129Smlaier pf_change_a(&th.th_seq, icmpsum, 4624126258Smlaier htonl(seq), 0); 4625128129Smlaier copyback = 1; 4626128129Smlaier } 4627126258Smlaier 4628200930Sdelphij if (!((*state)->state_flags & PFSTATE_SLOPPY) && 4629200930Sdelphij (!SEQ_GEQ(src->seqhi, seq) || 4630200930Sdelphij !SEQ_GEQ(seq, src->seqlo - (dst->max_win << dws)))) { 4631223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4632126258Smlaier printf("pf: BAD ICMP %d:%d ", 4633126258Smlaier icmptype, pd->hdr.icmp->icmp_code); 4634126258Smlaier pf_print_host(pd->src, 0, pd->af); 4635126258Smlaier printf(" -> "); 4636126258Smlaier pf_print_host(pd->dst, 0, pd->af); 4637126258Smlaier printf(" state: "); 4638126258Smlaier pf_print_state(*state); 4639126258Smlaier printf(" seq=%u\n", seq); 4640126258Smlaier } 4641145836Smlaier REASON_SET(reason, PFRES_BADSTATE); 4642126258Smlaier return (PF_DROP); 4643223637Sbz } else { 4644223637Sbz if (V_pf_status.debug >= PF_DEBUG_MISC) { 4645223637Sbz printf("pf: OK ICMP %d:%d ", 4646223637Sbz icmptype, pd->hdr.icmp->icmp_code); 4647223637Sbz pf_print_host(pd->src, 0, pd->af); 4648223637Sbz printf(" -> "); 4649223637Sbz pf_print_host(pd->dst, 0, pd->af); 4650223637Sbz printf(" state: "); 4651223637Sbz pf_print_state(*state); 4652223637Sbz printf(" seq=%u\n", seq); 4653223637Sbz } 4654126258Smlaier } 4655126258Smlaier 4656223637Sbz /* translate source/destination address, if necessary */ 4657223637Sbz if ((*state)->key[PF_SK_WIRE] != 4658223637Sbz (*state)->key[PF_SK_STACK]) { 4659223637Sbz struct pf_state_key *nk = 4660223637Sbz (*state)->key[pd->didx]; 4661223637Sbz 4662223637Sbz if (PF_ANEQ(pd2.src, 4663223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4664223637Sbz nk->port[pd2.sidx] != th.th_sport) 4665126258Smlaier pf_change_icmp(pd2.src, &th.th_sport, 4666223637Sbz daddr, &nk->addr[pd2.sidx], 4667223637Sbz nk->port[pd2.sidx], NULL, 4668126258Smlaier pd2.ip_sum, icmpsum, 4669126258Smlaier pd->ip_sum, 0, pd2.af); 4670223637Sbz 4671223637Sbz if (PF_ANEQ(pd2.dst, 4672223637Sbz &nk->addr[pd2.didx], pd2.af) || 4673223637Sbz nk->port[pd2.didx] != th.th_dport) 4674126258Smlaier pf_change_icmp(pd2.dst, &th.th_dport, 4675223637Sbz NULL, /* XXX Inbound NAT? */ 4676223637Sbz &nk->addr[pd2.didx], 4677223637Sbz nk->port[pd2.didx], NULL, 4678126258Smlaier pd2.ip_sum, icmpsum, 4679126258Smlaier pd->ip_sum, 0, pd2.af); 4680128129Smlaier copyback = 1; 4681128129Smlaier } 4682128129Smlaier 4683128129Smlaier if (copyback) { 4684126258Smlaier switch (pd2.af) { 4685126258Smlaier#ifdef INET 4686126258Smlaier case AF_INET: 4687126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4688240233Sglebius (caddr_t )pd->hdr.icmp); 4689126258Smlaier m_copyback(m, ipoff2, sizeof(h2), 4690240233Sglebius (caddr_t )&h2); 4691126258Smlaier break; 4692126258Smlaier#endif /* INET */ 4693126258Smlaier#ifdef INET6 4694126258Smlaier case AF_INET6: 4695126258Smlaier m_copyback(m, off, 4696126258Smlaier sizeof(struct icmp6_hdr), 4697240233Sglebius (caddr_t )pd->hdr.icmp6); 4698126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4699240233Sglebius (caddr_t )&h2_6); 4700126258Smlaier break; 4701126258Smlaier#endif /* INET6 */ 4702126258Smlaier } 4703126261Smlaier m_copyback(m, off2, 8, (caddr_t)&th); 4704126258Smlaier } 4705126258Smlaier 4706126258Smlaier return (PF_PASS); 4707126258Smlaier break; 4708126258Smlaier } 4709126258Smlaier case IPPROTO_UDP: { 4710126258Smlaier struct udphdr uh; 4711126258Smlaier 4712126258Smlaier if (!pf_pull_hdr(m, off2, &uh, sizeof(uh), 4713145836Smlaier NULL, reason, pd2.af)) { 4714126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4715126258Smlaier ("pf: ICMP error message too short " 4716126258Smlaier "(udp)\n")); 4717126258Smlaier return (PF_DROP); 4718126258Smlaier } 4719126258Smlaier 4720126258Smlaier key.af = pd2.af; 4721126258Smlaier key.proto = IPPROTO_UDP; 4722223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4723223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4724223637Sbz key.port[pd2.sidx] = uh.uh_sport; 4725223637Sbz key.port[pd2.didx] = uh.uh_dport; 4726126258Smlaier 4727240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4728126258Smlaier 4729223637Sbz /* translate source/destination address, if necessary */ 4730223637Sbz if ((*state)->key[PF_SK_WIRE] != 4731223637Sbz (*state)->key[PF_SK_STACK]) { 4732223637Sbz struct pf_state_key *nk = 4733223637Sbz (*state)->key[pd->didx]; 4734223637Sbz 4735223637Sbz if (PF_ANEQ(pd2.src, 4736223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4737223637Sbz nk->port[pd2.sidx] != uh.uh_sport) 4738126258Smlaier pf_change_icmp(pd2.src, &uh.uh_sport, 4739223637Sbz daddr, &nk->addr[pd2.sidx], 4740223637Sbz nk->port[pd2.sidx], &uh.uh_sum, 4741126258Smlaier pd2.ip_sum, icmpsum, 4742126258Smlaier pd->ip_sum, 1, pd2.af); 4743223637Sbz 4744223637Sbz if (PF_ANEQ(pd2.dst, 4745223637Sbz &nk->addr[pd2.didx], pd2.af) || 4746223637Sbz nk->port[pd2.didx] != uh.uh_dport) 4747126258Smlaier pf_change_icmp(pd2.dst, &uh.uh_dport, 4748223637Sbz NULL, /* XXX Inbound NAT? */ 4749223637Sbz &nk->addr[pd2.didx], 4750223637Sbz nk->port[pd2.didx], &uh.uh_sum, 4751126258Smlaier pd2.ip_sum, icmpsum, 4752126258Smlaier pd->ip_sum, 1, pd2.af); 4753223637Sbz 4754126258Smlaier switch (pd2.af) { 4755126258Smlaier#ifdef INET 4756126258Smlaier case AF_INET: 4757126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4758240233Sglebius (caddr_t )pd->hdr.icmp); 4759223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4760126258Smlaier break; 4761126258Smlaier#endif /* INET */ 4762126258Smlaier#ifdef INET6 4763126258Smlaier case AF_INET6: 4764126258Smlaier m_copyback(m, off, 4765126258Smlaier sizeof(struct icmp6_hdr), 4766240233Sglebius (caddr_t )pd->hdr.icmp6); 4767126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4768240233Sglebius (caddr_t )&h2_6); 4769126258Smlaier break; 4770126258Smlaier#endif /* INET6 */ 4771126258Smlaier } 4772223637Sbz m_copyback(m, off2, sizeof(uh), (caddr_t)&uh); 4773126258Smlaier } 4774126258Smlaier return (PF_PASS); 4775126258Smlaier break; 4776126258Smlaier } 4777126258Smlaier#ifdef INET 4778126258Smlaier case IPPROTO_ICMP: { 4779126258Smlaier struct icmp iih; 4780126258Smlaier 4781126258Smlaier if (!pf_pull_hdr(m, off2, &iih, ICMP_MINLEN, 4782145836Smlaier NULL, reason, pd2.af)) { 4783126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4784126258Smlaier ("pf: ICMP error message too short i" 4785126258Smlaier "(icmp)\n")); 4786126258Smlaier return (PF_DROP); 4787126258Smlaier } 4788126258Smlaier 4789126258Smlaier key.af = pd2.af; 4790126258Smlaier key.proto = IPPROTO_ICMP; 4791223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4792223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4793223637Sbz key.port[0] = key.port[1] = iih.icmp_id; 4794126258Smlaier 4795240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4796126258Smlaier 4797223637Sbz /* translate source/destination address, if necessary */ 4798223637Sbz if ((*state)->key[PF_SK_WIRE] != 4799223637Sbz (*state)->key[PF_SK_STACK]) { 4800223637Sbz struct pf_state_key *nk = 4801223637Sbz (*state)->key[pd->didx]; 4802223637Sbz 4803223637Sbz if (PF_ANEQ(pd2.src, 4804223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4805223637Sbz nk->port[pd2.sidx] != iih.icmp_id) 4806126258Smlaier pf_change_icmp(pd2.src, &iih.icmp_id, 4807223637Sbz daddr, &nk->addr[pd2.sidx], 4808223637Sbz nk->port[pd2.sidx], NULL, 4809126258Smlaier pd2.ip_sum, icmpsum, 4810126258Smlaier pd->ip_sum, 0, AF_INET); 4811223637Sbz 4812223637Sbz if (PF_ANEQ(pd2.dst, 4813223637Sbz &nk->addr[pd2.didx], pd2.af) || 4814223637Sbz nk->port[pd2.didx] != iih.icmp_id) 4815126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp_id, 4816223637Sbz NULL, /* XXX Inbound NAT? */ 4817223637Sbz &nk->addr[pd2.didx], 4818223637Sbz nk->port[pd2.didx], NULL, 4819126258Smlaier pd2.ip_sum, icmpsum, 4820126258Smlaier pd->ip_sum, 0, AF_INET); 4821223637Sbz 4822223637Sbz m_copyback(m, off, ICMP_MINLEN, (caddr_t)pd->hdr.icmp); 4823223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4824223637Sbz m_copyback(m, off2, ICMP_MINLEN, (caddr_t)&iih); 4825126258Smlaier } 4826126258Smlaier return (PF_PASS); 4827126258Smlaier break; 4828126258Smlaier } 4829126258Smlaier#endif /* INET */ 4830126258Smlaier#ifdef INET6 4831126258Smlaier case IPPROTO_ICMPV6: { 4832126258Smlaier struct icmp6_hdr iih; 4833126258Smlaier 4834126258Smlaier if (!pf_pull_hdr(m, off2, &iih, 4835145836Smlaier sizeof(struct icmp6_hdr), NULL, reason, pd2.af)) { 4836126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 4837126258Smlaier ("pf: ICMP error message too short " 4838126258Smlaier "(icmp6)\n")); 4839126258Smlaier return (PF_DROP); 4840126258Smlaier } 4841126258Smlaier 4842126258Smlaier key.af = pd2.af; 4843126258Smlaier key.proto = IPPROTO_ICMPV6; 4844223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4845223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4846223637Sbz key.port[0] = key.port[1] = iih.icmp6_id; 4847126258Smlaier 4848240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4849126258Smlaier 4850223637Sbz /* translate source/destination address, if necessary */ 4851223637Sbz if ((*state)->key[PF_SK_WIRE] != 4852223637Sbz (*state)->key[PF_SK_STACK]) { 4853223637Sbz struct pf_state_key *nk = 4854223637Sbz (*state)->key[pd->didx]; 4855223637Sbz 4856223637Sbz if (PF_ANEQ(pd2.src, 4857223637Sbz &nk->addr[pd2.sidx], pd2.af) || 4858223637Sbz nk->port[pd2.sidx] != iih.icmp6_id) 4859126258Smlaier pf_change_icmp(pd2.src, &iih.icmp6_id, 4860223637Sbz daddr, &nk->addr[pd2.sidx], 4861223637Sbz nk->port[pd2.sidx], NULL, 4862126258Smlaier pd2.ip_sum, icmpsum, 4863126258Smlaier pd->ip_sum, 0, AF_INET6); 4864223637Sbz 4865223637Sbz if (PF_ANEQ(pd2.dst, 4866223637Sbz &nk->addr[pd2.didx], pd2.af) || 4867223637Sbz nk->port[pd2.didx] != iih.icmp6_id) 4868126258Smlaier pf_change_icmp(pd2.dst, &iih.icmp6_id, 4869223637Sbz NULL, /* XXX Inbound NAT? */ 4870223637Sbz &nk->addr[pd2.didx], 4871223637Sbz nk->port[pd2.didx], NULL, 4872126258Smlaier pd2.ip_sum, icmpsum, 4873126258Smlaier pd->ip_sum, 0, AF_INET6); 4874223637Sbz 4875126258Smlaier m_copyback(m, off, sizeof(struct icmp6_hdr), 4876126261Smlaier (caddr_t)pd->hdr.icmp6); 4877223637Sbz m_copyback(m, ipoff2, sizeof(h2_6), (caddr_t)&h2_6); 4878126258Smlaier m_copyback(m, off2, sizeof(struct icmp6_hdr), 4879126261Smlaier (caddr_t)&iih); 4880126258Smlaier } 4881126258Smlaier return (PF_PASS); 4882126258Smlaier break; 4883126258Smlaier } 4884126258Smlaier#endif /* INET6 */ 4885126258Smlaier default: { 4886126258Smlaier key.af = pd2.af; 4887126258Smlaier key.proto = pd2.proto; 4888223637Sbz PF_ACPY(&key.addr[pd2.sidx], pd2.src, key.af); 4889223637Sbz PF_ACPY(&key.addr[pd2.didx], pd2.dst, key.af); 4890223637Sbz key.port[0] = key.port[1] = 0; 4891126258Smlaier 4892240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4893126258Smlaier 4894223637Sbz /* translate source/destination address, if necessary */ 4895223637Sbz if ((*state)->key[PF_SK_WIRE] != 4896223637Sbz (*state)->key[PF_SK_STACK]) { 4897223637Sbz struct pf_state_key *nk = 4898223637Sbz (*state)->key[pd->didx]; 4899223637Sbz 4900223637Sbz if (PF_ANEQ(pd2.src, 4901223637Sbz &nk->addr[pd2.sidx], pd2.af)) 4902223637Sbz pf_change_icmp(pd2.src, NULL, daddr, 4903223637Sbz &nk->addr[pd2.sidx], 0, NULL, 4904126258Smlaier pd2.ip_sum, icmpsum, 4905126258Smlaier pd->ip_sum, 0, pd2.af); 4906223637Sbz 4907223637Sbz if (PF_ANEQ(pd2.dst, 4908223637Sbz &nk->addr[pd2.didx], pd2.af)) 4909223637Sbz pf_change_icmp(pd2.src, NULL, 4910223637Sbz NULL, /* XXX Inbound NAT? */ 4911223637Sbz &nk->addr[pd2.didx], 0, NULL, 4912126258Smlaier pd2.ip_sum, icmpsum, 4913126258Smlaier pd->ip_sum, 0, pd2.af); 4914223637Sbz 4915126258Smlaier switch (pd2.af) { 4916126258Smlaier#ifdef INET 4917126258Smlaier case AF_INET: 4918126258Smlaier m_copyback(m, off, ICMP_MINLEN, 4919126261Smlaier (caddr_t)pd->hdr.icmp); 4920223637Sbz m_copyback(m, ipoff2, sizeof(h2), (caddr_t)&h2); 4921126258Smlaier break; 4922126258Smlaier#endif /* INET */ 4923126258Smlaier#ifdef INET6 4924126258Smlaier case AF_INET6: 4925126258Smlaier m_copyback(m, off, 4926126258Smlaier sizeof(struct icmp6_hdr), 4927240233Sglebius (caddr_t )pd->hdr.icmp6); 4928126258Smlaier m_copyback(m, ipoff2, sizeof(h2_6), 4929240233Sglebius (caddr_t )&h2_6); 4930126258Smlaier break; 4931126258Smlaier#endif /* INET6 */ 4932126258Smlaier } 4933126258Smlaier } 4934126258Smlaier return (PF_PASS); 4935126258Smlaier break; 4936126258Smlaier } 4937126258Smlaier } 4938126258Smlaier } 4939126258Smlaier} 4940126258Smlaier 4941240233Sglebiusstatic int 4942130613Smlaierpf_test_state_other(struct pf_state **state, int direction, struct pfi_kif *kif, 4943223637Sbz struct mbuf *m, struct pf_pdesc *pd) 4944126258Smlaier{ 4945126258Smlaier struct pf_state_peer *src, *dst; 4946223637Sbz struct pf_state_key_cmp key; 4947126258Smlaier 4948240233Sglebius bzero(&key, sizeof(key)); 4949126258Smlaier key.af = pd->af; 4950126258Smlaier key.proto = pd->proto; 4951130613Smlaier if (direction == PF_IN) { 4952223637Sbz PF_ACPY(&key.addr[0], pd->src, key.af); 4953223637Sbz PF_ACPY(&key.addr[1], pd->dst, key.af); 4954223637Sbz key.port[0] = key.port[1] = 0; 4955130613Smlaier } else { 4956223637Sbz PF_ACPY(&key.addr[1], pd->src, key.af); 4957223637Sbz PF_ACPY(&key.addr[0], pd->dst, key.af); 4958223637Sbz key.port[1] = key.port[0] = 0; 4959130613Smlaier } 4960126258Smlaier 4961240233Sglebius STATE_LOOKUP(kif, &key, direction, *state, pd); 4962126258Smlaier 4963126258Smlaier if (direction == (*state)->direction) { 4964126258Smlaier src = &(*state)->src; 4965126258Smlaier dst = &(*state)->dst; 4966126258Smlaier } else { 4967126258Smlaier src = &(*state)->dst; 4968126258Smlaier dst = &(*state)->src; 4969126258Smlaier } 4970126258Smlaier 4971126258Smlaier /* update states */ 4972126258Smlaier if (src->state < PFOTHERS_SINGLE) 4973126258Smlaier src->state = PFOTHERS_SINGLE; 4974126258Smlaier if (dst->state == PFOTHERS_SINGLE) 4975126258Smlaier dst->state = PFOTHERS_MULTIPLE; 4976126258Smlaier 4977126258Smlaier /* update expire time */ 4978240233Sglebius (*state)->expire = time_uptime; 4979126258Smlaier if (src->state == PFOTHERS_MULTIPLE && dst->state == PFOTHERS_MULTIPLE) 4980126258Smlaier (*state)->timeout = PFTM_OTHER_MULTIPLE; 4981126258Smlaier else 4982126258Smlaier (*state)->timeout = PFTM_OTHER_SINGLE; 4983126258Smlaier 4984126258Smlaier /* translate source/destination address, if necessary */ 4985223637Sbz if ((*state)->key[PF_SK_WIRE] != (*state)->key[PF_SK_STACK]) { 4986223637Sbz struct pf_state_key *nk = (*state)->key[pd->didx]; 4987223637Sbz 4988240233Sglebius KASSERT(nk, ("%s: nk is null", __func__)); 4989240233Sglebius KASSERT(pd, ("%s: pd is null", __func__)); 4990240233Sglebius KASSERT(pd->src, ("%s: pd->src is null", __func__)); 4991240233Sglebius KASSERT(pd->dst, ("%s: pd->dst is null", __func__)); 4992223637Sbz switch (pd->af) { 4993126258Smlaier#ifdef INET 4994223637Sbz case AF_INET: 4995223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 4996126258Smlaier pf_change_a(&pd->src->v4.s_addr, 4997223637Sbz pd->ip_sum, 4998223637Sbz nk->addr[pd->sidx].v4.s_addr, 4999126258Smlaier 0); 5000223637Sbz 5001223637Sbz 5002223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5003126258Smlaier pf_change_a(&pd->dst->v4.s_addr, 5004223637Sbz pd->ip_sum, 5005223637Sbz nk->addr[pd->didx].v4.s_addr, 5006126258Smlaier 0); 5007223637Sbz 5008126258Smlaier break; 5009126258Smlaier#endif /* INET */ 5010126258Smlaier#ifdef INET6 5011223637Sbz case AF_INET6: 5012223637Sbz if (PF_ANEQ(pd->src, &nk->addr[pd->sidx], AF_INET)) 5013223637Sbz PF_ACPY(pd->src, &nk->addr[pd->sidx], pd->af); 5014223637Sbz 5015223637Sbz if (PF_ANEQ(pd->dst, &nk->addr[pd->didx], AF_INET)) 5016223637Sbz PF_ACPY(pd->dst, &nk->addr[pd->didx], pd->af); 5017126258Smlaier#endif /* INET6 */ 5018223637Sbz } 5019126258Smlaier } 5020126258Smlaier return (PF_PASS); 5021126258Smlaier} 5022126258Smlaier 5023126258Smlaier/* 5024126258Smlaier * ipoff and off are measured from the start of the mbuf chain. 5025126258Smlaier * h must be at "ipoff" on the mbuf chain. 5026126258Smlaier */ 5027126258Smlaiervoid * 5028126258Smlaierpf_pull_hdr(struct mbuf *m, int off, void *p, int len, 5029126258Smlaier u_short *actionp, u_short *reasonp, sa_family_t af) 5030126258Smlaier{ 5031126258Smlaier switch (af) { 5032126258Smlaier#ifdef INET 5033126258Smlaier case AF_INET: { 5034126258Smlaier struct ip *h = mtod(m, struct ip *); 5035126258Smlaier u_int16_t fragoff = (ntohs(h->ip_off) & IP_OFFMASK) << 3; 5036126258Smlaier 5037126258Smlaier if (fragoff) { 5038126258Smlaier if (fragoff >= len) 5039126258Smlaier ACTION_SET(actionp, PF_PASS); 5040126258Smlaier else { 5041126258Smlaier ACTION_SET(actionp, PF_DROP); 5042126258Smlaier REASON_SET(reasonp, PFRES_FRAG); 5043126258Smlaier } 5044126258Smlaier return (NULL); 5045126258Smlaier } 5046130613Smlaier if (m->m_pkthdr.len < off + len || 5047130613Smlaier ntohs(h->ip_len) < off + len) { 5048126258Smlaier ACTION_SET(actionp, PF_DROP); 5049126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5050126258Smlaier return (NULL); 5051126258Smlaier } 5052126258Smlaier break; 5053126258Smlaier } 5054126258Smlaier#endif /* INET */ 5055126258Smlaier#ifdef INET6 5056126258Smlaier case AF_INET6: { 5057126258Smlaier struct ip6_hdr *h = mtod(m, struct ip6_hdr *); 5058126258Smlaier 5059126258Smlaier if (m->m_pkthdr.len < off + len || 5060126258Smlaier (ntohs(h->ip6_plen) + sizeof(struct ip6_hdr)) < 5061126258Smlaier (unsigned)(off + len)) { 5062126258Smlaier ACTION_SET(actionp, PF_DROP); 5063126258Smlaier REASON_SET(reasonp, PFRES_SHORT); 5064126258Smlaier return (NULL); 5065126258Smlaier } 5066126258Smlaier break; 5067126258Smlaier } 5068126258Smlaier#endif /* INET6 */ 5069126258Smlaier } 5070126258Smlaier m_copydata(m, off, len, p); 5071126258Smlaier return (p); 5072126258Smlaier} 5073126258Smlaier 5074126258Smlaierint 5075231852Sbzpf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kif *kif, 5076231852Sbz int rtableid) 5077126258Smlaier{ 5078223637Sbz#ifdef RADIX_MPATH 5079223637Sbz struct radix_node_head *rnh; 5080223637Sbz#endif 5081126258Smlaier struct sockaddr_in *dst; 5082171168Smlaier int ret = 1; 5083171168Smlaier int check_mpath; 5084145836Smlaier#ifdef INET6 5085145836Smlaier struct sockaddr_in6 *dst6; 5086145836Smlaier struct route_in6 ro; 5087145836Smlaier#else 5088126258Smlaier struct route ro; 5089145836Smlaier#endif 5090171168Smlaier struct radix_node *rn; 5091171168Smlaier struct rtentry *rt; 5092171168Smlaier struct ifnet *ifp; 5093126258Smlaier 5094171168Smlaier check_mpath = 0; 5095223637Sbz#ifdef RADIX_MPATH 5096223637Sbz /* XXX: stick to table 0 for now */ 5097223637Sbz rnh = rt_tables_get_rnh(0, af); 5098223637Sbz if (rnh != NULL && rn_mpath_capable(rnh)) 5099223637Sbz check_mpath = 1; 5100223637Sbz#endif 5101126258Smlaier bzero(&ro, sizeof(ro)); 5102145836Smlaier switch (af) { 5103145836Smlaier case AF_INET: 5104145836Smlaier dst = satosin(&ro.ro_dst); 5105145836Smlaier dst->sin_family = AF_INET; 5106145836Smlaier dst->sin_len = sizeof(*dst); 5107145836Smlaier dst->sin_addr = addr->v4; 5108145836Smlaier break; 5109145836Smlaier#ifdef INET6 5110145836Smlaier case AF_INET6: 5111223637Sbz /* 5112223637Sbz * Skip check for addresses with embedded interface scope, 5113223637Sbz * as they would always match anyway. 5114223637Sbz */ 5115223637Sbz if (IN6_IS_SCOPE_EMBED(&addr->v6)) 5116223637Sbz goto out; 5117145836Smlaier dst6 = (struct sockaddr_in6 *)&ro.ro_dst; 5118145836Smlaier dst6->sin6_family = AF_INET6; 5119145836Smlaier dst6->sin6_len = sizeof(*dst6); 5120145836Smlaier dst6->sin6_addr = addr->v6; 5121145836Smlaier break; 5122145836Smlaier#endif /* INET6 */ 5123145836Smlaier default: 5124145836Smlaier return (0); 5125145836Smlaier } 5126145836Smlaier 5127171168Smlaier /* Skip checks for ipsec interfaces */ 5128171168Smlaier if (kif != NULL && kif->pfik_ifp->if_type == IFT_ENC) 5129171168Smlaier goto out; 5130171168Smlaier 5131231852Sbz switch (af) { 5132231852Sbz#ifdef INET6 5133231852Sbz case AF_INET6: 5134231852Sbz in6_rtalloc_ign(&ro, 0, rtableid); 5135231852Sbz break; 5136231852Sbz#endif 5137222529Sbz#ifdef INET 5138231852Sbz case AF_INET: 5139231852Sbz in_rtalloc_ign((struct route *)&ro, 0, rtableid); 5140231852Sbz break; 5141222529Sbz#endif 5142231852Sbz default: 5143231852Sbz rtalloc_ign((struct route *)&ro, 0); /* No/default FIB. */ 5144231852Sbz break; 5145231852Sbz } 5146126258Smlaier 5147126258Smlaier if (ro.ro_rt != NULL) { 5148171168Smlaier /* No interface given, this is a no-route check */ 5149171168Smlaier if (kif == NULL) 5150171168Smlaier goto out; 5151171168Smlaier 5152171168Smlaier if (kif->pfik_ifp == NULL) { 5153171168Smlaier ret = 0; 5154171168Smlaier goto out; 5155171168Smlaier } 5156171168Smlaier 5157171168Smlaier /* Perform uRPF check if passed input interface */ 5158171168Smlaier ret = 0; 5159171168Smlaier rn = (struct radix_node *)ro.ro_rt; 5160171168Smlaier do { 5161171168Smlaier rt = (struct rtentry *)rn; 5162240233Sglebius ifp = rt->rt_ifp; 5163171168Smlaier 5164171168Smlaier if (kif->pfik_ifp == ifp) 5165171168Smlaier ret = 1; 5166223637Sbz#ifdef RADIX_MPATH 5167171168Smlaier rn = rn_mpath_next(rn); 5168171168Smlaier#endif 5169171168Smlaier } while (check_mpath == 1 && rn != NULL && ret == 0); 5170171168Smlaier } else 5171171168Smlaier ret = 0; 5172171168Smlaierout: 5173171168Smlaier if (ro.ro_rt != NULL) 5174126258Smlaier RTFREE(ro.ro_rt); 5175171168Smlaier return (ret); 5176145836Smlaier} 5177145836Smlaier 5178222529Sbz#ifdef INET 5179240233Sglebiusstatic void 5180126258Smlaierpf_route(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5181171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5182126258Smlaier{ 5183126258Smlaier struct mbuf *m0, *m1; 5184240233Sglebius struct sockaddr_in dst; 5185126258Smlaier struct ip *ip; 5186126258Smlaier struct ifnet *ifp = NULL; 5187126258Smlaier struct pf_addr naddr; 5188130613Smlaier struct pf_src_node *sn = NULL; 5189126258Smlaier int error = 0; 5190242161Sglebius uint16_t ip_len, ip_off; 5191126258Smlaier 5192240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5193240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5194240233Sglebius __func__)); 5195126258Smlaier 5196240233Sglebius if ((pd->pf_mtag == NULL && 5197240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5198240233Sglebius pd->pf_mtag->routed++ > 3) { 5199171168Smlaier m0 = *m; 5200171168Smlaier *m = NULL; 5201240233Sglebius goto bad_locked; 5202132303Smlaier } 5203132303Smlaier 5204126258Smlaier if (r->rt == PF_DUPTO) { 5205240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5206240233Sglebius if (s) 5207240233Sglebius PF_STATE_UNLOCK(s); 5208126258Smlaier return; 5209240233Sglebius } 5210126258Smlaier } else { 5211240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5212240233Sglebius if (s) 5213240233Sglebius PF_STATE_UNLOCK(s); 5214126258Smlaier return; 5215240233Sglebius } 5216126258Smlaier m0 = *m; 5217126258Smlaier } 5218126258Smlaier 5219126258Smlaier ip = mtod(m0, struct ip *); 5220126258Smlaier 5221240233Sglebius bzero(&dst, sizeof(dst)); 5222240233Sglebius dst.sin_family = AF_INET; 5223240233Sglebius dst.sin_len = sizeof(dst); 5224240233Sglebius dst.sin_addr = ip->ip_dst; 5225126258Smlaier 5226126258Smlaier if (r->rt == PF_FASTROUTE) { 5227240233Sglebius struct rtentry *rt; 5228240233Sglebius 5229240233Sglebius if (s) 5230240233Sglebius PF_STATE_UNLOCK(s); 5231240233Sglebius rt = rtalloc1_fib(sintosa(&dst), 0, 0, M_GETFIB(m0)); 5232240233Sglebius if (rt == NULL) { 5233240233Sglebius RTFREE_LOCKED(rt); 5234196039Srwatson KMOD_IPSTAT_INC(ips_noroute); 5235240233Sglebius error = EHOSTUNREACH; 5236126258Smlaier goto bad; 5237126258Smlaier } 5238126258Smlaier 5239240233Sglebius ifp = rt->rt_ifp; 5240240233Sglebius rt->rt_rmx.rmx_pksent++; 5241126258Smlaier 5242240233Sglebius if (rt->rt_flags & RTF_GATEWAY) 5243240233Sglebius bcopy(satosin(rt->rt_gateway), &dst, sizeof(dst)); 5244240233Sglebius RTFREE_LOCKED(rt); 5245126258Smlaier } else { 5246145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5247145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5248240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5249240233Sglebius goto bad_locked; 5250145836Smlaier } 5251126258Smlaier if (s == NULL) { 5252130613Smlaier pf_map_addr(AF_INET, r, (struct pf_addr *)&ip->ip_src, 5253130613Smlaier &naddr, NULL, &sn); 5254126258Smlaier if (!PF_AZERO(&naddr, AF_INET)) 5255240233Sglebius dst.sin_addr.s_addr = naddr.v4.s_addr; 5256130613Smlaier ifp = r->rpool.cur->kif ? 5257130613Smlaier r->rpool.cur->kif->pfik_ifp : NULL; 5258126258Smlaier } else { 5259126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET)) 5260240233Sglebius dst.sin_addr.s_addr = 5261126258Smlaier s->rt_addr.v4.s_addr; 5262130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5263240233Sglebius PF_STATE_UNLOCK(s); 5264126258Smlaier } 5265126258Smlaier } 5266126258Smlaier if (ifp == NULL) 5267126258Smlaier goto bad; 5268126258Smlaier 5269130639Smlaier if (oifp != ifp) { 5270145836Smlaier if (pf_test(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5271126258Smlaier goto bad; 5272126258Smlaier else if (m0 == NULL) 5273126258Smlaier goto done; 5274145836Smlaier if (m0->m_len < sizeof(struct ip)) { 5275145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5276240233Sglebius ("%s: m0->m_len < sizeof(struct ip)\n", __func__)); 5277145836Smlaier goto bad; 5278145836Smlaier } 5279126258Smlaier ip = mtod(m0, struct ip *); 5280126258Smlaier } 5281126258Smlaier 5282240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5283240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5284240233Sglebius 5285241344Sglebius ip_len = ntohs(ip->ip_len); 5286241344Sglebius ip_off = ntohs(ip->ip_off); 5287240233Sglebius 5288240233Sglebius /* Copied from FreeBSD 10.0-CURRENT ip_output. */ 5289126261Smlaier m0->m_pkthdr.csum_flags |= CSUM_IP; 5290242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 5291126261Smlaier in_delayed_cksum(m0); 5292242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 5293126261Smlaier } 5294240233Sglebius#ifdef SCTP 5295242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 5296240233Sglebius sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 5297242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 5298240233Sglebius } 5299240233Sglebius#endif 5300126261Smlaier 5301130613Smlaier /* 5302240233Sglebius * If small enough for interface, or the interface will take 5303240233Sglebius * care of the fragmentation for us, we can just send directly. 5304130613Smlaier */ 5305241344Sglebius if (ip_len <= ifp->if_mtu || 5306240233Sglebius (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 5307241344Sglebius ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 5308223637Sbz ip->ip_sum = 0; 5309242161Sglebius if (m0->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 5310126258Smlaier ip->ip_sum = in_cksum(m0, ip->ip_hl << 2); 5311242161Sglebius m0->m_pkthdr.csum_flags &= ~CSUM_IP; 5312242161Sglebius } 5313254523Sandre m_clrprotoflags(m0); /* Avoid confusing lower layers. */ 5314240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5315126258Smlaier goto done; 5316126258Smlaier } 5317223637Sbz 5318240233Sglebius /* Balk when DF bit is set or the interface didn't support TSO. */ 5319241344Sglebius if ((ip_off & IP_DF) || (m0->m_pkthdr.csum_flags & CSUM_TSO)) { 5320240233Sglebius error = EMSGSIZE; 5321196039Srwatson KMOD_IPSTAT_INC(ips_cantfrag); 5322126258Smlaier if (r->rt != PF_DUPTO) { 5323126258Smlaier icmp_error(m0, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, 5324145886Smlaier ifp->if_mtu); 5325126258Smlaier goto done; 5326126258Smlaier } else 5327126258Smlaier goto bad; 5328126258Smlaier } 5329126258Smlaier 5330242161Sglebius error = ip_fragment(ip, &m0, ifp->if_mtu, ifp->if_hwassist); 5331240233Sglebius if (error) 5332126258Smlaier goto bad; 5333126258Smlaier 5334240233Sglebius for (; m0; m0 = m1) { 5335126258Smlaier m1 = m0->m_nextpkt; 5336240233Sglebius m0->m_nextpkt = NULL; 5337126261Smlaier if (error == 0) { 5338254523Sandre m_clrprotoflags(m0); 5339240233Sglebius error = (*ifp->if_output)(ifp, m0, sintosa(&dst), NULL); 5340126261Smlaier } else 5341126258Smlaier m_freem(m0); 5342126258Smlaier } 5343126258Smlaier 5344126258Smlaier if (error == 0) 5345196039Srwatson KMOD_IPSTAT_INC(ips_fragmented); 5346126258Smlaier 5347126258Smlaierdone: 5348126258Smlaier if (r->rt != PF_DUPTO) 5349126258Smlaier *m = NULL; 5350126258Smlaier return; 5351126258Smlaier 5352240233Sglebiusbad_locked: 5353240233Sglebius if (s) 5354240233Sglebius PF_STATE_UNLOCK(s); 5355126258Smlaierbad: 5356126258Smlaier m_freem(m0); 5357126258Smlaier goto done; 5358126258Smlaier} 5359126258Smlaier#endif /* INET */ 5360126258Smlaier 5361126258Smlaier#ifdef INET6 5362240233Sglebiusstatic void 5363126258Smlaierpf_route6(struct mbuf **m, struct pf_rule *r, int dir, struct ifnet *oifp, 5364171168Smlaier struct pf_state *s, struct pf_pdesc *pd) 5365126258Smlaier{ 5366126258Smlaier struct mbuf *m0; 5367240233Sglebius struct sockaddr_in6 dst; 5368126258Smlaier struct ip6_hdr *ip6; 5369126258Smlaier struct ifnet *ifp = NULL; 5370126258Smlaier struct pf_addr naddr; 5371130613Smlaier struct pf_src_node *sn = NULL; 5372126258Smlaier 5373240233Sglebius KASSERT(m && *m && r && oifp, ("%s: invalid parameters", __func__)); 5374240233Sglebius KASSERT(dir == PF_IN || dir == PF_OUT, ("%s: invalid direction", 5375240233Sglebius __func__)); 5376126258Smlaier 5377240233Sglebius if ((pd->pf_mtag == NULL && 5378240233Sglebius ((pd->pf_mtag = pf_get_mtag(*m)) == NULL)) || 5379240233Sglebius pd->pf_mtag->routed++ > 3) { 5380171168Smlaier m0 = *m; 5381171168Smlaier *m = NULL; 5382240233Sglebius goto bad_locked; 5383132303Smlaier } 5384132303Smlaier 5385126258Smlaier if (r->rt == PF_DUPTO) { 5386240233Sglebius if ((m0 = m_dup(*m, M_NOWAIT)) == NULL) { 5387240233Sglebius if (s) 5388240233Sglebius PF_STATE_UNLOCK(s); 5389126258Smlaier return; 5390240233Sglebius } 5391126258Smlaier } else { 5392240233Sglebius if ((r->rt == PF_REPLYTO) == (r->direction == dir)) { 5393240233Sglebius if (s) 5394240233Sglebius PF_STATE_UNLOCK(s); 5395126258Smlaier return; 5396240233Sglebius } 5397126258Smlaier m0 = *m; 5398126258Smlaier } 5399126258Smlaier 5400126258Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5401126258Smlaier 5402240233Sglebius bzero(&dst, sizeof(dst)); 5403240233Sglebius dst.sin6_family = AF_INET6; 5404240233Sglebius dst.sin6_len = sizeof(dst); 5405240233Sglebius dst.sin6_addr = ip6->ip6_dst; 5406126258Smlaier 5407171168Smlaier /* Cheat. XXX why only in the v6 case??? */ 5408126258Smlaier if (r->rt == PF_FASTROUTE) { 5409240233Sglebius if (s) 5410240233Sglebius PF_STATE_UNLOCK(s); 5411132280Smlaier m0->m_flags |= M_SKIP_FIREWALL; 5412126261Smlaier ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL); 5413126258Smlaier return; 5414126258Smlaier } 5415126258Smlaier 5416145836Smlaier if (TAILQ_EMPTY(&r->rpool.list)) { 5417145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5418240233Sglebius ("%s: TAILQ_EMPTY(&r->rpool.list)\n", __func__)); 5419240233Sglebius goto bad_locked; 5420145836Smlaier } 5421126258Smlaier if (s == NULL) { 5422130613Smlaier pf_map_addr(AF_INET6, r, (struct pf_addr *)&ip6->ip6_src, 5423130613Smlaier &naddr, NULL, &sn); 5424126258Smlaier if (!PF_AZERO(&naddr, AF_INET6)) 5425240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5426126258Smlaier &naddr, AF_INET6); 5427130613Smlaier ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; 5428126258Smlaier } else { 5429126258Smlaier if (!PF_AZERO(&s->rt_addr, AF_INET6)) 5430240233Sglebius PF_ACPY((struct pf_addr *)&dst.sin6_addr, 5431126258Smlaier &s->rt_addr, AF_INET6); 5432130613Smlaier ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; 5433126258Smlaier } 5434240233Sglebius 5435240233Sglebius if (s) 5436240233Sglebius PF_STATE_UNLOCK(s); 5437240233Sglebius 5438126258Smlaier if (ifp == NULL) 5439126258Smlaier goto bad; 5440126258Smlaier 5441126258Smlaier if (oifp != ifp) { 5442145836Smlaier if (pf_test6(PF_OUT, ifp, &m0, NULL) != PF_PASS) 5443132303Smlaier goto bad; 5444132303Smlaier else if (m0 == NULL) 5445132303Smlaier goto done; 5446145836Smlaier if (m0->m_len < sizeof(struct ip6_hdr)) { 5447145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5448240233Sglebius ("%s: m0->m_len < sizeof(struct ip6_hdr)\n", 5449240233Sglebius __func__)); 5450145836Smlaier goto bad; 5451145836Smlaier } 5452132303Smlaier ip6 = mtod(m0, struct ip6_hdr *); 5453126258Smlaier } 5454126258Smlaier 5455240233Sglebius if (ifp->if_flags & IFF_LOOPBACK) 5456240233Sglebius m0->m_flags |= M_SKIP_FIREWALL; 5457240233Sglebius 5458126258Smlaier /* 5459126258Smlaier * If the packet is too large for the outgoing interface, 5460126258Smlaier * send back an icmp6 error. 5461126258Smlaier */ 5462240233Sglebius if (IN6_IS_SCOPE_EMBED(&dst.sin6_addr)) 5463240233Sglebius dst.sin6_addr.s6_addr16[1] = htons(ifp->if_index); 5464240233Sglebius if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) 5465240233Sglebius nd6_output(ifp, ifp, m0, &dst, NULL); 5466240233Sglebius else { 5467126258Smlaier in6_ifstat_inc(ifp, ifs6_in_toobig); 5468126258Smlaier if (r->rt != PF_DUPTO) 5469126258Smlaier icmp6_error(m0, ICMP6_PACKET_TOO_BIG, 0, ifp->if_mtu); 5470126258Smlaier else 5471126258Smlaier goto bad; 5472126258Smlaier } 5473126258Smlaier 5474126258Smlaierdone: 5475126258Smlaier if (r->rt != PF_DUPTO) 5476126258Smlaier *m = NULL; 5477126258Smlaier return; 5478126258Smlaier 5479240233Sglebiusbad_locked: 5480240233Sglebius if (s) 5481240233Sglebius PF_STATE_UNLOCK(s); 5482126258Smlaierbad: 5483126258Smlaier m_freem(m0); 5484126258Smlaier goto done; 5485126258Smlaier} 5486126258Smlaier#endif /* INET6 */ 5487126258Smlaier 5488126258Smlaier/* 5489132566Smlaier * FreeBSD supports cksum offloads for the following drivers. 5490137413Sru * em(4), fxp(4), ixgb(4), lge(4), ndis(4), nge(4), re(4), 5491132566Smlaier * ti(4), txp(4), xl(4) 5492132566Smlaier * 5493132566Smlaier * CSUM_DATA_VALID | CSUM_PSEUDO_HDR : 5494132566Smlaier * network driver performed cksum including pseudo header, need to verify 5495132566Smlaier * csum_data 5496132566Smlaier * CSUM_DATA_VALID : 5497132566Smlaier * network driver performed cksum, needs to additional pseudo header 5498132566Smlaier * cksum computation with partial csum_data(i.e. lack of H/W support for 5499132566Smlaier * pseudo header, for instance hme(4), sk(4) and possibly gem(4)) 5500132566Smlaier * 5501132566Smlaier * After validating the cksum of packet, set both flag CSUM_DATA_VALID and 5502132566Smlaier * CSUM_PSEUDO_HDR in order to avoid recomputation of the cksum in upper 5503132566Smlaier * TCP/UDP layer. 5504132566Smlaier * Also, set csum_data to 0xffff to force cksum validation. 5505126261Smlaier */ 5506240233Sglebiusstatic int 5507126261Smlaierpf_check_proto_cksum(struct mbuf *m, int off, int len, u_int8_t p, sa_family_t af) 5508126261Smlaier{ 5509126261Smlaier u_int16_t sum = 0; 5510126261Smlaier int hw_assist = 0; 5511126261Smlaier struct ip *ip; 5512126261Smlaier 5513126261Smlaier if (off < sizeof(struct ip) || len < sizeof(struct udphdr)) 5514126261Smlaier return (1); 5515126261Smlaier if (m->m_pkthdr.len < off + len) 5516126261Smlaier return (1); 5517126261Smlaier 5518126261Smlaier switch (p) { 5519126261Smlaier case IPPROTO_TCP: 5520126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5521126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5522126261Smlaier sum = m->m_pkthdr.csum_data; 5523126261Smlaier } else { 5524223637Sbz ip = mtod(m, struct ip *); 5525126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5526240233Sglebius ip->ip_dst.s_addr, htonl((u_short)len + 5527223637Sbz m->m_pkthdr.csum_data + IPPROTO_TCP)); 5528126261Smlaier } 5529126261Smlaier sum ^= 0xffff; 5530126261Smlaier ++hw_assist; 5531126261Smlaier } 5532126261Smlaier break; 5533126261Smlaier case IPPROTO_UDP: 5534126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 5535126261Smlaier if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { 5536126261Smlaier sum = m->m_pkthdr.csum_data; 5537126261Smlaier } else { 5538223637Sbz ip = mtod(m, struct ip *); 5539126261Smlaier sum = in_pseudo(ip->ip_src.s_addr, 5540223637Sbz ip->ip_dst.s_addr, htonl((u_short)len + 5541223637Sbz m->m_pkthdr.csum_data + IPPROTO_UDP)); 5542126261Smlaier } 5543126261Smlaier sum ^= 0xffff; 5544126261Smlaier ++hw_assist; 5545223637Sbz } 5546126261Smlaier break; 5547126261Smlaier case IPPROTO_ICMP: 5548126261Smlaier#ifdef INET6 5549126261Smlaier case IPPROTO_ICMPV6: 5550126261Smlaier#endif /* INET6 */ 5551126261Smlaier break; 5552126261Smlaier default: 5553126261Smlaier return (1); 5554126261Smlaier } 5555126261Smlaier 5556126261Smlaier if (!hw_assist) { 5557126261Smlaier switch (af) { 5558126261Smlaier case AF_INET: 5559126261Smlaier if (p == IPPROTO_ICMP) { 5560126261Smlaier if (m->m_len < off) 5561126261Smlaier return (1); 5562126261Smlaier m->m_data += off; 5563126261Smlaier m->m_len -= off; 5564126261Smlaier sum = in_cksum(m, len); 5565126261Smlaier m->m_data -= off; 5566126261Smlaier m->m_len += off; 5567126261Smlaier } else { 5568126261Smlaier if (m->m_len < sizeof(struct ip)) 5569126261Smlaier return (1); 5570126261Smlaier sum = in4_cksum(m, p, off, len); 5571126261Smlaier } 5572126261Smlaier break; 5573126261Smlaier#ifdef INET6 5574126261Smlaier case AF_INET6: 5575126261Smlaier if (m->m_len < sizeof(struct ip6_hdr)) 5576126261Smlaier return (1); 5577126261Smlaier sum = in6_cksum(m, p, off, len); 5578126261Smlaier break; 5579126261Smlaier#endif /* INET6 */ 5580126261Smlaier default: 5581126261Smlaier return (1); 5582126261Smlaier } 5583126261Smlaier } 5584126261Smlaier if (sum) { 5585126261Smlaier switch (p) { 5586126261Smlaier case IPPROTO_TCP: 5587183550Szec { 5588196039Srwatson KMOD_TCPSTAT_INC(tcps_rcvbadsum); 5589126261Smlaier break; 5590183550Szec } 5591126261Smlaier case IPPROTO_UDP: 5592183550Szec { 5593196039Srwatson KMOD_UDPSTAT_INC(udps_badsum); 5594126261Smlaier break; 5595183550Szec } 5596222529Sbz#ifdef INET 5597126261Smlaier case IPPROTO_ICMP: 5598183550Szec { 5599196039Srwatson KMOD_ICMPSTAT_INC(icps_checksum); 5600126261Smlaier break; 5601183550Szec } 5602222529Sbz#endif 5603126261Smlaier#ifdef INET6 5604126261Smlaier case IPPROTO_ICMPV6: 5605183550Szec { 5606196039Srwatson KMOD_ICMP6STAT_INC(icp6s_checksum); 5607126261Smlaier break; 5608183550Szec } 5609126261Smlaier#endif /* INET6 */ 5610126261Smlaier } 5611126261Smlaier return (1); 5612132566Smlaier } else { 5613132566Smlaier if (p == IPPROTO_TCP || p == IPPROTO_UDP) { 5614132566Smlaier m->m_pkthdr.csum_flags |= 5615132566Smlaier (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 5616132566Smlaier m->m_pkthdr.csum_data = 0xffff; 5617132566Smlaier } 5618126261Smlaier } 5619126261Smlaier return (0); 5620126261Smlaier} 5621223637Sbz 5622126258Smlaier 5623145836Smlaier#ifdef INET 5624126258Smlaierint 5625240233Sglebiuspf_test(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5626126258Smlaier{ 5627130613Smlaier struct pfi_kif *kif; 5628130613Smlaier u_short action, reason = 0, log = 0; 5629130613Smlaier struct mbuf *m = *m0; 5630223637Sbz struct ip *h = NULL; 5631223637Sbz struct m_tag *ipfwtag; 5632223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5633130613Smlaier struct pf_state *s = NULL; 5634130613Smlaier struct pf_ruleset *ruleset = NULL; 5635130613Smlaier struct pf_pdesc pd; 5636130613Smlaier int off, dirndx, pqid = 0; 5637126258Smlaier 5638240233Sglebius M_ASSERTPKTHDR(m); 5639240233Sglebius 5640223637Sbz if (!V_pf_status.running) 5641171168Smlaier return (PF_PASS); 5642126258Smlaier 5643171168Smlaier memset(&pd, 0, sizeof(pd)); 5644145836Smlaier 5645240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 5646240233Sglebius 5647130613Smlaier if (kif == NULL) { 5648145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 5649145836Smlaier ("pf_test: kif == NULL, if_xname %s\n", ifp->if_xname)); 5650130613Smlaier return (PF_DROP); 5651130613Smlaier } 5652223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 5653145836Smlaier return (PF_PASS); 5654130613Smlaier 5655240233Sglebius if (m->m_flags & M_SKIP_FIREWALL) 5656240233Sglebius return (PF_PASS); 5657126258Smlaier 5658240233Sglebius pd.pf_mtag = pf_find_mtag(m); 5659240233Sglebius 5660240233Sglebius PF_RULES_RLOCK(); 5661240233Sglebius 5662223637Sbz if (ip_divert_ptr != NULL && 5663223637Sbz ((ipfwtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL)) != NULL)) { 5664223637Sbz struct ipfw_rule_ref *rr = (struct ipfw_rule_ref *)(ipfwtag+1); 5665223637Sbz if (rr->info & IPFW_IS_DIVERT && rr->rulenum == 0) { 5666240233Sglebius if (pd.pf_mtag == NULL && 5667240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5668240233Sglebius action = PF_DROP; 5669240233Sglebius goto done; 5670240233Sglebius } 5671223637Sbz pd.pf_mtag->flags |= PF_PACKET_LOOPED; 5672223637Sbz m_tag_delete(m, ipfwtag); 5673223637Sbz } 5674240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_FASTFWD_OURS_PRESENT) { 5675223637Sbz m->m_flags |= M_FASTFWD_OURS; 5676223637Sbz pd.pf_mtag->flags &= ~PF_FASTFWD_OURS_PRESENT; 5677223637Sbz } 5678240233Sglebius } else if (pf_normalize_ip(m0, dir, kif, &reason, &pd) != PF_PASS) { 5679240233Sglebius /* We do IP header normalization and packet reassembly here */ 5680126258Smlaier action = PF_DROP; 5681126258Smlaier goto done; 5682126258Smlaier } 5683223637Sbz m = *m0; /* pf_normalize messes with m0 */ 5684126258Smlaier h = mtod(m, struct ip *); 5685126258Smlaier 5686126258Smlaier off = h->ip_hl << 2; 5687240233Sglebius if (off < (int)sizeof(struct ip)) { 5688126258Smlaier action = PF_DROP; 5689126258Smlaier REASON_SET(&reason, PFRES_SHORT); 5690126258Smlaier log = 1; 5691126258Smlaier goto done; 5692126258Smlaier } 5693126258Smlaier 5694126258Smlaier pd.src = (struct pf_addr *)&h->ip_src; 5695126258Smlaier pd.dst = (struct pf_addr *)&h->ip_dst; 5696223637Sbz pd.sport = pd.dport = NULL; 5697126258Smlaier pd.ip_sum = &h->ip_sum; 5698223637Sbz pd.proto_sum = NULL; 5699126258Smlaier pd.proto = h->ip_p; 5700223637Sbz pd.dir = dir; 5701223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 5702223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 5703126258Smlaier pd.af = AF_INET; 5704126258Smlaier pd.tos = h->ip_tos; 5705126258Smlaier pd.tot_len = ntohs(h->ip_len); 5706126258Smlaier 5707126258Smlaier /* handle fragments that didn't get reassembled by normalization */ 5708126258Smlaier if (h->ip_off & htons(IP_MF | IP_OFFMASK)) { 5709130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 5710126258Smlaier &pd, &a, &ruleset); 5711126258Smlaier goto done; 5712126258Smlaier } 5713126258Smlaier 5714126258Smlaier switch (h->ip_p) { 5715126258Smlaier 5716126258Smlaier case IPPROTO_TCP: { 5717126258Smlaier struct tcphdr th; 5718126258Smlaier 5719126258Smlaier pd.hdr.tcp = &th; 5720126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 5721126258Smlaier &action, &reason, AF_INET)) { 5722126258Smlaier log = action != PF_PASS; 5723126258Smlaier goto done; 5724126258Smlaier } 5725126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 5726126258Smlaier if ((th.th_flags & TH_ACK) && pd.p_len == 0) 5727126258Smlaier pqid = 1; 5728130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 5729126258Smlaier if (action == PF_DROP) 5730130613Smlaier goto done; 5731130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 5732126258Smlaier &reason); 5733126258Smlaier if (action == PF_PASS) { 5734223637Sbz if (pfsync_update_state_ptr != NULL) 5735223637Sbz pfsync_update_state_ptr(s); 5736126258Smlaier r = s->rule.ptr; 5737130613Smlaier a = s->anchor.ptr; 5738126258Smlaier log = s->log; 5739126258Smlaier } else if (s == NULL) 5740240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5741240233Sglebius &a, &ruleset, inp); 5742126258Smlaier break; 5743126258Smlaier } 5744126258Smlaier 5745126258Smlaier case IPPROTO_UDP: { 5746126258Smlaier struct udphdr uh; 5747126258Smlaier 5748126258Smlaier pd.hdr.udp = &uh; 5749126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 5750126258Smlaier &action, &reason, AF_INET)) { 5751126258Smlaier log = action != PF_PASS; 5752126258Smlaier goto done; 5753126258Smlaier } 5754130613Smlaier if (uh.uh_dport == 0 || 5755130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 5756130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 5757130613Smlaier action = PF_DROP; 5758171168Smlaier REASON_SET(&reason, PFRES_SHORT); 5759130613Smlaier goto done; 5760130613Smlaier } 5761130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 5762126258Smlaier if (action == PF_PASS) { 5763223637Sbz if (pfsync_update_state_ptr != NULL) 5764223637Sbz pfsync_update_state_ptr(s); 5765126258Smlaier r = s->rule.ptr; 5766126258Smlaier a = s->anchor.ptr; 5767126258Smlaier log = s->log; 5768126258Smlaier } else if (s == NULL) 5769240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5770240233Sglebius &a, &ruleset, inp); 5771126258Smlaier break; 5772126258Smlaier } 5773126258Smlaier 5774126258Smlaier case IPPROTO_ICMP: { 5775126258Smlaier struct icmp ih; 5776126258Smlaier 5777126258Smlaier pd.hdr.icmp = &ih; 5778126258Smlaier if (!pf_pull_hdr(m, off, &ih, ICMP_MINLEN, 5779126258Smlaier &action, &reason, AF_INET)) { 5780126258Smlaier log = action != PF_PASS; 5781126258Smlaier goto done; 5782126258Smlaier } 5783145836Smlaier action = pf_test_state_icmp(&s, dir, kif, m, off, h, &pd, 5784145836Smlaier &reason); 5785126258Smlaier if (action == PF_PASS) { 5786223637Sbz if (pfsync_update_state_ptr != NULL) 5787223637Sbz pfsync_update_state_ptr(s); 5788126258Smlaier r = s->rule.ptr; 5789126258Smlaier a = s->anchor.ptr; 5790126258Smlaier log = s->log; 5791126258Smlaier } else if (s == NULL) 5792240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5793240233Sglebius &a, &ruleset, inp); 5794126258Smlaier break; 5795126258Smlaier } 5796126258Smlaier 5797223637Sbz#ifdef INET6 5798223637Sbz case IPPROTO_ICMPV6: { 5799223637Sbz action = PF_DROP; 5800223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5801223637Sbz ("pf: dropping IPv4 packet with ICMPv6 payload\n")); 5802223637Sbz goto done; 5803223637Sbz } 5804223637Sbz#endif 5805223637Sbz 5806126258Smlaier default: 5807223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 5808126258Smlaier if (action == PF_PASS) { 5809223637Sbz if (pfsync_update_state_ptr != NULL) 5810223637Sbz pfsync_update_state_ptr(s); 5811126258Smlaier r = s->rule.ptr; 5812126258Smlaier a = s->anchor.ptr; 5813126258Smlaier log = s->log; 5814126258Smlaier } else if (s == NULL) 5815240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 5816240233Sglebius &a, &ruleset, inp); 5817126258Smlaier break; 5818126258Smlaier } 5819126258Smlaier 5820126258Smlaierdone: 5821240233Sglebius PF_RULES_RUNLOCK(); 5822126258Smlaier if (action == PF_PASS && h->ip_hl > 5 && 5823200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 5824126258Smlaier action = PF_DROP; 5825145836Smlaier REASON_SET(&reason, PFRES_IPOPTIONS); 5826126258Smlaier log = 1; 5827126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 5828126258Smlaier ("pf: dropping packet with ip options\n")); 5829126258Smlaier } 5830126258Smlaier 5831240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 5832240233Sglebius action = PF_DROP; 5833240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5834240233Sglebius } 5835240233Sglebius if (r->rtableid >= 0) 5836240233Sglebius M_SETFIB(m, r->rtableid); 5837145836Smlaier 5838126258Smlaier#ifdef ALTQ 5839126258Smlaier if (action == PF_PASS && r->qid) { 5840240233Sglebius if (pd.pf_mtag == NULL && 5841240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5842240233Sglebius action = PF_DROP; 5843240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5844240233Sglebius } 5845171168Smlaier if (pqid || (pd.tos & IPTOS_LOWDELAY)) 5846171168Smlaier pd.pf_mtag->qid = r->pqid; 5847171168Smlaier else 5848171168Smlaier pd.pf_mtag->qid = r->qid; 5849171168Smlaier /* add hints for ecn */ 5850171168Smlaier pd.pf_mtag->hdr = h; 5851223637Sbz 5852126258Smlaier } 5853145836Smlaier#endif /* ALTQ */ 5854126258Smlaier 5855130613Smlaier /* 5856130613Smlaier * connections redirected to loopback should not match sockets 5857130613Smlaier * bound specifically to loopback due to security implications, 5858130613Smlaier * see tcp_input() and in_pcblookup_listen(). 5859130613Smlaier */ 5860130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 5861130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 5862130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 5863130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 5864171168Smlaier (ntohl(pd.dst->v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 5865223637Sbz m->m_flags |= M_SKIP_FIREWALL; 5866171168Smlaier 5867240233Sglebius if (action == PF_PASS && r->divert.port && ip_divert_ptr != NULL && 5868240233Sglebius !PACKET_LOOPED(&pd)) { 5869223637Sbz 5870223637Sbz ipfwtag = m_tag_alloc(MTAG_IPFW_RULE, 0, 5871240233Sglebius sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO); 5872223637Sbz if (ipfwtag != NULL) { 5873225171Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->info = 5874225171Sbz ntohs(r->divert.port); 5875223637Sbz ((struct ipfw_rule_ref *)(ipfwtag+1))->rulenum = dir; 5876223637Sbz 5877240233Sglebius if (s) 5878240233Sglebius PF_STATE_UNLOCK(s); 5879240233Sglebius 5880223637Sbz m_tag_prepend(m, ipfwtag); 5881223637Sbz if (m->m_flags & M_FASTFWD_OURS) { 5882240233Sglebius if (pd.pf_mtag == NULL && 5883240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 5884240233Sglebius action = PF_DROP; 5885240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 5886240233Sglebius log = 1; 5887240233Sglebius DPFPRINTF(PF_DEBUG_MISC, 5888240233Sglebius ("pf: failed to allocate tag\n")); 5889240233Sglebius } 5890223637Sbz pd.pf_mtag->flags |= PF_FASTFWD_OURS_PRESENT; 5891223637Sbz m->m_flags &= ~M_FASTFWD_OURS; 5892223637Sbz } 5893240233Sglebius ip_divert_ptr(*m0, dir == PF_IN ? DIR_IN : DIR_OUT); 5894240233Sglebius *m0 = NULL; 5895223637Sbz 5896223637Sbz return (action); 5897223637Sbz } else { 5898223637Sbz /* XXX: ipfw has the same behaviour! */ 5899223637Sbz action = PF_DROP; 5900223637Sbz REASON_SET(&reason, PFRES_MEMORY); 5901223637Sbz log = 1; 5902223637Sbz DPFPRINTF(PF_DEBUG_MISC, 5903223637Sbz ("pf: failed to allocate divert tag\n")); 5904223637Sbz } 5905223637Sbz } 5906223637Sbz 5907171168Smlaier if (log) { 5908171168Smlaier struct pf_rule *lr; 5909171168Smlaier 5910171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 5911171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 5912171168Smlaier lr = s->nat_rule.ptr; 5913171168Smlaier else 5914171168Smlaier lr = r; 5915240233Sglebius PFLOG_PACKET(kif, m, AF_INET, dir, reason, lr, a, ruleset, &pd, 5916240233Sglebius (s == NULL)); 5917130613Smlaier } 5918130613Smlaier 5919130613Smlaier kif->pfik_bytes[0][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 5920130613Smlaier kif->pfik_packets[0][dir == PF_OUT][action != PF_PASS]++; 5921130613Smlaier 5922130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 5923171168Smlaier dirndx = (dir == PF_OUT); 5924171168Smlaier r->packets[dirndx]++; 5925171168Smlaier r->bytes[dirndx] += pd.tot_len; 5926130613Smlaier if (a != NULL) { 5927171168Smlaier a->packets[dirndx]++; 5928171168Smlaier a->bytes[dirndx] += pd.tot_len; 5929130613Smlaier } 5930130613Smlaier if (s != NULL) { 5931130613Smlaier if (s->nat_rule.ptr != NULL) { 5932171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 5933171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 5934130613Smlaier } 5935130613Smlaier if (s->src_node != NULL) { 5936171168Smlaier s->src_node->packets[dirndx]++; 5937171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 5938130613Smlaier } 5939130613Smlaier if (s->nat_src_node != NULL) { 5940171168Smlaier s->nat_src_node->packets[dirndx]++; 5941171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 5942130613Smlaier } 5943171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 5944171168Smlaier s->packets[dirndx]++; 5945171168Smlaier s->bytes[dirndx] += pd.tot_len; 5946130613Smlaier } 5947130613Smlaier tr = r; 5948130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 5949223637Sbz if (nr != NULL && r == &V_pf_default_rule) 5950223637Sbz tr = nr; 5951130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 5952223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 5953223637Sbz (s == NULL) ? pd.src : 5954223637Sbz &s->key[(s->direction == PF_IN)]-> 5955223637Sbz addr[(s->direction == PF_OUT)], 5956223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 5957223637Sbz r->action == PF_PASS, tr->src.neg); 5958130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 5959223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 5960223637Sbz (s == NULL) ? pd.dst : 5961223637Sbz &s->key[(s->direction == PF_IN)]-> 5962223637Sbz addr[(s->direction == PF_IN)], 5963223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 5964223637Sbz r->action == PF_PASS, tr->dst.neg); 5965130613Smlaier } 5966130613Smlaier 5967223637Sbz switch (action) { 5968223637Sbz case PF_SYNPROXY_DROP: 5969126258Smlaier m_freem(*m0); 5970223637Sbz case PF_DEFER: 5971126258Smlaier *m0 = NULL; 5972126258Smlaier action = PF_PASS; 5973223637Sbz break; 5974223637Sbz default: 5975240233Sglebius /* pf_route() returns unlocked. */ 5976240233Sglebius if (r->rt) { 5977223637Sbz pf_route(m0, r, dir, kif->pfik_ifp, s, &pd); 5978240233Sglebius return (action); 5979240233Sglebius } 5980223637Sbz break; 5981223637Sbz } 5982240233Sglebius if (s) 5983240233Sglebius PF_STATE_UNLOCK(s); 5984240233Sglebius 5985126258Smlaier return (action); 5986126258Smlaier} 5987126258Smlaier#endif /* INET */ 5988126258Smlaier 5989126258Smlaier#ifdef INET6 5990126258Smlaierint 5991240233Sglebiuspf_test6(int dir, struct ifnet *ifp, struct mbuf **m0, struct inpcb *inp) 5992126258Smlaier{ 5993130613Smlaier struct pfi_kif *kif; 5994130613Smlaier u_short action, reason = 0, log = 0; 5995171168Smlaier struct mbuf *m = *m0, *n = NULL; 5996223637Sbz struct ip6_hdr *h = NULL; 5997223637Sbz struct pf_rule *a = NULL, *r = &V_pf_default_rule, *tr, *nr; 5998130613Smlaier struct pf_state *s = NULL; 5999130613Smlaier struct pf_ruleset *ruleset = NULL; 6000130613Smlaier struct pf_pdesc pd; 6001169843Sdhartmei int off, terminal = 0, dirndx, rh_cnt = 0; 6002126258Smlaier 6003240233Sglebius M_ASSERTPKTHDR(m); 6004240233Sglebius 6005240233Sglebius if (!V_pf_status.running) 6006126258Smlaier return (PF_PASS); 6007126258Smlaier 6008171168Smlaier memset(&pd, 0, sizeof(pd)); 6009240233Sglebius pd.pf_mtag = pf_find_mtag(m); 6010145836Smlaier 6011240233Sglebius if (pd.pf_mtag && pd.pf_mtag->flags & PF_TAG_GENERATED) 6012240233Sglebius return (PF_PASS); 6013240233Sglebius 6014240233Sglebius kif = (struct pfi_kif *)ifp->if_pf_kif; 6015130613Smlaier if (kif == NULL) { 6016145836Smlaier DPFPRINTF(PF_DEBUG_URGENT, 6017145836Smlaier ("pf_test6: kif == NULL, if_xname %s\n", ifp->if_xname)); 6018130613Smlaier return (PF_DROP); 6019130613Smlaier } 6020223637Sbz if (kif->pfik_flags & PFI_IFLAG_SKIP) 6021145836Smlaier return (PF_PASS); 6022130613Smlaier 6023240233Sglebius PF_RULES_RLOCK(); 6024223637Sbz 6025126258Smlaier /* We do IP header normalization and packet reassembly here */ 6026145836Smlaier if (pf_normalize_ip6(m0, dir, kif, &reason, &pd) != PF_PASS) { 6027126258Smlaier action = PF_DROP; 6028126258Smlaier goto done; 6029126258Smlaier } 6030223637Sbz m = *m0; /* pf_normalize messes with m0 */ 6031126258Smlaier h = mtod(m, struct ip6_hdr *); 6032126258Smlaier 6033169843Sdhartmei#if 1 6034169843Sdhartmei /* 6035169843Sdhartmei * we do not support jumbogram yet. if we keep going, zero ip6_plen 6036169843Sdhartmei * will do something bad, so drop the packet for now. 6037169843Sdhartmei */ 6038169843Sdhartmei if (htons(h->ip6_plen) == 0) { 6039169843Sdhartmei action = PF_DROP; 6040169843Sdhartmei REASON_SET(&reason, PFRES_NORM); /*XXX*/ 6041169843Sdhartmei goto done; 6042169843Sdhartmei } 6043169843Sdhartmei#endif 6044169843Sdhartmei 6045126258Smlaier pd.src = (struct pf_addr *)&h->ip6_src; 6046126258Smlaier pd.dst = (struct pf_addr *)&h->ip6_dst; 6047223637Sbz pd.sport = pd.dport = NULL; 6048126258Smlaier pd.ip_sum = NULL; 6049223637Sbz pd.proto_sum = NULL; 6050223637Sbz pd.dir = dir; 6051223637Sbz pd.sidx = (dir == PF_IN) ? 0 : 1; 6052223637Sbz pd.didx = (dir == PF_IN) ? 1 : 0; 6053126258Smlaier pd.af = AF_INET6; 6054126258Smlaier pd.tos = 0; 6055126258Smlaier pd.tot_len = ntohs(h->ip6_plen) + sizeof(struct ip6_hdr); 6056126258Smlaier 6057126258Smlaier off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr); 6058126258Smlaier pd.proto = h->ip6_nxt; 6059126258Smlaier do { 6060126258Smlaier switch (pd.proto) { 6061126258Smlaier case IPPROTO_FRAGMENT: 6062130613Smlaier action = pf_test_fragment(&r, dir, kif, m, h, 6063126258Smlaier &pd, &a, &ruleset); 6064126258Smlaier if (action == PF_DROP) 6065126258Smlaier REASON_SET(&reason, PFRES_FRAG); 6066126258Smlaier goto done; 6067169843Sdhartmei case IPPROTO_ROUTING: { 6068169843Sdhartmei struct ip6_rthdr rthdr; 6069169843Sdhartmei 6070169843Sdhartmei if (rh_cnt++) { 6071169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6072169843Sdhartmei ("pf: IPv6 more than one rthdr\n")); 6073169843Sdhartmei action = PF_DROP; 6074169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6075169843Sdhartmei log = 1; 6076169843Sdhartmei goto done; 6077169843Sdhartmei } 6078169843Sdhartmei if (!pf_pull_hdr(m, off, &rthdr, sizeof(rthdr), NULL, 6079169843Sdhartmei &reason, pd.af)) { 6080169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6081169843Sdhartmei ("pf: IPv6 short rthdr\n")); 6082169843Sdhartmei action = PF_DROP; 6083169843Sdhartmei REASON_SET(&reason, PFRES_SHORT); 6084169843Sdhartmei log = 1; 6085169843Sdhartmei goto done; 6086169843Sdhartmei } 6087169843Sdhartmei if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) { 6088169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6089169843Sdhartmei ("pf: IPv6 rthdr0\n")); 6090169843Sdhartmei action = PF_DROP; 6091169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6092169843Sdhartmei log = 1; 6093169843Sdhartmei goto done; 6094169843Sdhartmei } 6095223637Sbz /* FALLTHROUGH */ 6096169843Sdhartmei } 6097126258Smlaier case IPPROTO_AH: 6098126258Smlaier case IPPROTO_HOPOPTS: 6099126258Smlaier case IPPROTO_DSTOPTS: { 6100126258Smlaier /* get next header and header length */ 6101126258Smlaier struct ip6_ext opt6; 6102126258Smlaier 6103126258Smlaier if (!pf_pull_hdr(m, off, &opt6, sizeof(opt6), 6104145836Smlaier NULL, &reason, pd.af)) { 6105126258Smlaier DPFPRINTF(PF_DEBUG_MISC, 6106126258Smlaier ("pf: IPv6 short opt\n")); 6107126258Smlaier action = PF_DROP; 6108126258Smlaier log = 1; 6109126258Smlaier goto done; 6110126258Smlaier } 6111126258Smlaier if (pd.proto == IPPROTO_AH) 6112126258Smlaier off += (opt6.ip6e_len + 2) * 4; 6113126258Smlaier else 6114126258Smlaier off += (opt6.ip6e_len + 1) * 8; 6115126258Smlaier pd.proto = opt6.ip6e_nxt; 6116126258Smlaier /* goto the next header */ 6117126258Smlaier break; 6118126258Smlaier } 6119126258Smlaier default: 6120126258Smlaier terminal++; 6121126258Smlaier break; 6122126258Smlaier } 6123126258Smlaier } while (!terminal); 6124126258Smlaier 6125171168Smlaier /* if there's no routing header, use unmodified mbuf for checksumming */ 6126171168Smlaier if (!n) 6127171168Smlaier n = m; 6128171168Smlaier 6129126258Smlaier switch (pd.proto) { 6130126258Smlaier 6131126258Smlaier case IPPROTO_TCP: { 6132126258Smlaier struct tcphdr th; 6133126258Smlaier 6134126258Smlaier pd.hdr.tcp = &th; 6135126258Smlaier if (!pf_pull_hdr(m, off, &th, sizeof(th), 6136126258Smlaier &action, &reason, AF_INET6)) { 6137126258Smlaier log = action != PF_PASS; 6138126258Smlaier goto done; 6139126258Smlaier } 6140126258Smlaier pd.p_len = pd.tot_len - off - (th.th_off << 2); 6141130613Smlaier action = pf_normalize_tcp(dir, kif, m, 0, off, h, &pd); 6142126258Smlaier if (action == PF_DROP) 6143130613Smlaier goto done; 6144130613Smlaier action = pf_test_state_tcp(&s, dir, kif, m, off, h, &pd, 6145126258Smlaier &reason); 6146126258Smlaier if (action == PF_PASS) { 6147223637Sbz if (pfsync_update_state_ptr != NULL) 6148223637Sbz pfsync_update_state_ptr(s); 6149126258Smlaier r = s->rule.ptr; 6150130613Smlaier a = s->anchor.ptr; 6151126258Smlaier log = s->log; 6152126258Smlaier } else if (s == NULL) 6153240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6154240233Sglebius &a, &ruleset, inp); 6155126258Smlaier break; 6156126258Smlaier } 6157126258Smlaier 6158126258Smlaier case IPPROTO_UDP: { 6159126258Smlaier struct udphdr uh; 6160126258Smlaier 6161126258Smlaier pd.hdr.udp = &uh; 6162126258Smlaier if (!pf_pull_hdr(m, off, &uh, sizeof(uh), 6163126258Smlaier &action, &reason, AF_INET6)) { 6164126258Smlaier log = action != PF_PASS; 6165126258Smlaier goto done; 6166126258Smlaier } 6167130613Smlaier if (uh.uh_dport == 0 || 6168130613Smlaier ntohs(uh.uh_ulen) > m->m_pkthdr.len - off || 6169130613Smlaier ntohs(uh.uh_ulen) < sizeof(struct udphdr)) { 6170130613Smlaier action = PF_DROP; 6171171168Smlaier REASON_SET(&reason, PFRES_SHORT); 6172130613Smlaier goto done; 6173130613Smlaier } 6174130613Smlaier action = pf_test_state_udp(&s, dir, kif, m, off, h, &pd); 6175126258Smlaier if (action == PF_PASS) { 6176223637Sbz if (pfsync_update_state_ptr != NULL) 6177223637Sbz pfsync_update_state_ptr(s); 6178126258Smlaier r = s->rule.ptr; 6179130613Smlaier a = s->anchor.ptr; 6180126258Smlaier log = s->log; 6181126258Smlaier } else if (s == NULL) 6182240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6183240233Sglebius &a, &ruleset, inp); 6184126258Smlaier break; 6185126258Smlaier } 6186126258Smlaier 6187223637Sbz case IPPROTO_ICMP: { 6188223637Sbz action = PF_DROP; 6189223637Sbz DPFPRINTF(PF_DEBUG_MISC, 6190223637Sbz ("pf: dropping IPv6 packet with ICMPv4 payload\n")); 6191223637Sbz goto done; 6192223637Sbz } 6193223637Sbz 6194126258Smlaier case IPPROTO_ICMPV6: { 6195126258Smlaier struct icmp6_hdr ih; 6196126258Smlaier 6197126258Smlaier pd.hdr.icmp6 = &ih; 6198126258Smlaier if (!pf_pull_hdr(m, off, &ih, sizeof(ih), 6199126258Smlaier &action, &reason, AF_INET6)) { 6200126258Smlaier log = action != PF_PASS; 6201126258Smlaier goto done; 6202126258Smlaier } 6203130613Smlaier action = pf_test_state_icmp(&s, dir, kif, 6204145836Smlaier m, off, h, &pd, &reason); 6205126258Smlaier if (action == PF_PASS) { 6206223637Sbz if (pfsync_update_state_ptr != NULL) 6207223637Sbz pfsync_update_state_ptr(s); 6208126258Smlaier r = s->rule.ptr; 6209130613Smlaier a = s->anchor.ptr; 6210126258Smlaier log = s->log; 6211126258Smlaier } else if (s == NULL) 6212240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6213240233Sglebius &a, &ruleset, inp); 6214126258Smlaier break; 6215126258Smlaier } 6216126258Smlaier 6217126258Smlaier default: 6218223637Sbz action = pf_test_state_other(&s, dir, kif, m, &pd); 6219130613Smlaier if (action == PF_PASS) { 6220223637Sbz if (pfsync_update_state_ptr != NULL) 6221223637Sbz pfsync_update_state_ptr(s); 6222130613Smlaier r = s->rule.ptr; 6223130613Smlaier a = s->anchor.ptr; 6224130613Smlaier log = s->log; 6225130613Smlaier } else if (s == NULL) 6226240233Sglebius action = pf_test_rule(&r, &s, dir, kif, m, off, &pd, 6227240233Sglebius &a, &ruleset, inp); 6228126258Smlaier break; 6229126258Smlaier } 6230126258Smlaier 6231126258Smlaierdone: 6232240233Sglebius PF_RULES_RUNLOCK(); 6233223637Sbz if (n != m) { 6234223637Sbz m_freem(n); 6235223637Sbz n = NULL; 6236223637Sbz } 6237223637Sbz 6238169843Sdhartmei /* handle dangerous IPv6 extension headers. */ 6239169843Sdhartmei if (action == PF_PASS && rh_cnt && 6240200930Sdelphij !((s && s->state_flags & PFSTATE_ALLOWOPTS) || r->allow_opts)) { 6241169843Sdhartmei action = PF_DROP; 6242169843Sdhartmei REASON_SET(&reason, PFRES_IPOPTIONS); 6243169843Sdhartmei log = 1; 6244169843Sdhartmei DPFPRINTF(PF_DEBUG_MISC, 6245169843Sdhartmei ("pf: dropping packet with dangerous v6 headers\n")); 6246169843Sdhartmei } 6247126258Smlaier 6248240233Sglebius if (s && s->tag > 0 && pf_tag_packet(m, &pd, s->tag)) { 6249240233Sglebius action = PF_DROP; 6250240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6251240233Sglebius } 6252240233Sglebius if (r->rtableid >= 0) 6253240233Sglebius M_SETFIB(m, r->rtableid); 6254145836Smlaier 6255126258Smlaier#ifdef ALTQ 6256126258Smlaier if (action == PF_PASS && r->qid) { 6257240233Sglebius if (pd.pf_mtag == NULL && 6258240233Sglebius ((pd.pf_mtag = pf_get_mtag(m)) == NULL)) { 6259240233Sglebius action = PF_DROP; 6260240233Sglebius REASON_SET(&reason, PFRES_MEMORY); 6261240233Sglebius } 6262171168Smlaier if (pd.tos & IPTOS_LOWDELAY) 6263171168Smlaier pd.pf_mtag->qid = r->pqid; 6264171168Smlaier else 6265171168Smlaier pd.pf_mtag->qid = r->qid; 6266171168Smlaier /* add hints for ecn */ 6267171168Smlaier pd.pf_mtag->hdr = h; 6268126258Smlaier } 6269145836Smlaier#endif /* ALTQ */ 6270126258Smlaier 6271130613Smlaier if (dir == PF_IN && action == PF_PASS && (pd.proto == IPPROTO_TCP || 6272130613Smlaier pd.proto == IPPROTO_UDP) && s != NULL && s->nat_rule.ptr != NULL && 6273130613Smlaier (s->nat_rule.ptr->action == PF_RDR || 6274130613Smlaier s->nat_rule.ptr->action == PF_BINAT) && 6275171168Smlaier IN6_IS_ADDR_LOOPBACK(&pd.dst->v6)) 6276223637Sbz m->m_flags |= M_SKIP_FIREWALL; 6277171168Smlaier 6278223637Sbz /* XXX: Anybody working on it?! */ 6279223637Sbz if (r->divert.port) 6280223637Sbz printf("pf: divert(9) is not supported for IPv6\n"); 6281223637Sbz 6282171168Smlaier if (log) { 6283171168Smlaier struct pf_rule *lr; 6284171168Smlaier 6285171168Smlaier if (s != NULL && s->nat_rule.ptr != NULL && 6286171168Smlaier s->nat_rule.ptr->log & PF_LOG_ALL) 6287171168Smlaier lr = s->nat_rule.ptr; 6288171168Smlaier else 6289171168Smlaier lr = r; 6290240233Sglebius PFLOG_PACKET(kif, m, AF_INET6, dir, reason, lr, a, ruleset, 6291240233Sglebius &pd, (s == NULL)); 6292130613Smlaier } 6293130613Smlaier 6294130613Smlaier kif->pfik_bytes[1][dir == PF_OUT][action != PF_PASS] += pd.tot_len; 6295130613Smlaier kif->pfik_packets[1][dir == PF_OUT][action != PF_PASS]++; 6296130613Smlaier 6297130613Smlaier if (action == PF_PASS || r->action == PF_DROP) { 6298171168Smlaier dirndx = (dir == PF_OUT); 6299171168Smlaier r->packets[dirndx]++; 6300171168Smlaier r->bytes[dirndx] += pd.tot_len; 6301130613Smlaier if (a != NULL) { 6302171168Smlaier a->packets[dirndx]++; 6303171168Smlaier a->bytes[dirndx] += pd.tot_len; 6304130613Smlaier } 6305130613Smlaier if (s != NULL) { 6306130613Smlaier if (s->nat_rule.ptr != NULL) { 6307171168Smlaier s->nat_rule.ptr->packets[dirndx]++; 6308171168Smlaier s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; 6309130613Smlaier } 6310130613Smlaier if (s->src_node != NULL) { 6311171168Smlaier s->src_node->packets[dirndx]++; 6312171168Smlaier s->src_node->bytes[dirndx] += pd.tot_len; 6313130613Smlaier } 6314130613Smlaier if (s->nat_src_node != NULL) { 6315171168Smlaier s->nat_src_node->packets[dirndx]++; 6316171168Smlaier s->nat_src_node->bytes[dirndx] += pd.tot_len; 6317130613Smlaier } 6318171168Smlaier dirndx = (dir == s->direction) ? 0 : 1; 6319171168Smlaier s->packets[dirndx]++; 6320171168Smlaier s->bytes[dirndx] += pd.tot_len; 6321130613Smlaier } 6322130613Smlaier tr = r; 6323130613Smlaier nr = (s != NULL) ? s->nat_rule.ptr : pd.nat_rule; 6324223637Sbz if (nr != NULL && r == &V_pf_default_rule) 6325223637Sbz tr = nr; 6326130613Smlaier if (tr->src.addr.type == PF_ADDR_TABLE) 6327223637Sbz pfr_update_stats(tr->src.addr.p.tbl, 6328223637Sbz (s == NULL) ? pd.src : 6329223637Sbz &s->key[(s->direction == PF_IN)]->addr[0], 6330223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6331223637Sbz r->action == PF_PASS, tr->src.neg); 6332130613Smlaier if (tr->dst.addr.type == PF_ADDR_TABLE) 6333223637Sbz pfr_update_stats(tr->dst.addr.p.tbl, 6334223637Sbz (s == NULL) ? pd.dst : 6335223637Sbz &s->key[(s->direction == PF_IN)]->addr[1], 6336223637Sbz pd.af, pd.tot_len, dir == PF_OUT, 6337223637Sbz r->action == PF_PASS, tr->dst.neg); 6338130613Smlaier } 6339130613Smlaier 6340223637Sbz switch (action) { 6341223637Sbz case PF_SYNPROXY_DROP: 6342126258Smlaier m_freem(*m0); 6343223637Sbz case PF_DEFER: 6344126258Smlaier *m0 = NULL; 6345126258Smlaier action = PF_PASS; 6346223637Sbz break; 6347223637Sbz default: 6348240233Sglebius /* pf_route6() returns unlocked. */ 6349240233Sglebius if (r->rt) { 6350223637Sbz pf_route6(m0, r, dir, kif->pfik_ifp, s, &pd); 6351240233Sglebius return (action); 6352240233Sglebius } 6353223637Sbz break; 6354223637Sbz } 6355126258Smlaier 6356240233Sglebius if (s) 6357240233Sglebius PF_STATE_UNLOCK(s); 6358240233Sglebius 6359126258Smlaier return (action); 6360126258Smlaier} 6361126258Smlaier#endif /* INET6 */ 6362