xref: /freebsd/sys/net/pfvar.h (revision 706d465d)
1fe267a55SPedro F. Giffuni /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
53b3a8eb9SGleb Smirnoff  * All rights reserved.
63b3a8eb9SGleb Smirnoff  *
73b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
83b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
93b3a8eb9SGleb Smirnoff  * are met:
103b3a8eb9SGleb Smirnoff  *
113b3a8eb9SGleb Smirnoff  *    - Redistributions of source code must retain the above copyright
123b3a8eb9SGleb Smirnoff  *      notice, this list of conditions and the following disclaimer.
133b3a8eb9SGleb Smirnoff  *    - Redistributions in binary form must reproduce the above
143b3a8eb9SGleb Smirnoff  *      copyright notice, this list of conditions and the following
153b3a8eb9SGleb Smirnoff  *      disclaimer in the documentation and/or other materials provided
163b3a8eb9SGleb Smirnoff  *      with the distribution.
173b3a8eb9SGleb Smirnoff  *
183b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193b3a8eb9SGleb Smirnoff  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203b3a8eb9SGleb Smirnoff  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
213b3a8eb9SGleb Smirnoff  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
223b3a8eb9SGleb Smirnoff  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
233b3a8eb9SGleb Smirnoff  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
243b3a8eb9SGleb Smirnoff  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
253b3a8eb9SGleb Smirnoff  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
263b3a8eb9SGleb Smirnoff  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
283b3a8eb9SGleb Smirnoff  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293b3a8eb9SGleb Smirnoff  * POSSIBILITY OF SUCH DAMAGE.
303b3a8eb9SGleb Smirnoff  *
319ed8bbbdSGleb Smirnoff  *	$OpenBSD: pfvar.h,v 1.282 2009/01/29 15:12:28 pyr Exp $
323b3a8eb9SGleb Smirnoff  */
333b3a8eb9SGleb Smirnoff 
343b3a8eb9SGleb Smirnoff #ifndef _NET_PFVAR_H_
353b3a8eb9SGleb Smirnoff #define _NET_PFVAR_H_
363b3a8eb9SGleb Smirnoff 
373b3a8eb9SGleb Smirnoff #include <sys/param.h>
383b3a8eb9SGleb Smirnoff #include <sys/queue.h>
3948278b88SGleb Smirnoff #include <sys/counter.h>
40455969d3SKristof Provost #include <sys/cpuset.h>
4120c4899aSKristof Provost #include <sys/epoch.h>
428ec07310SGleb Smirnoff #include <sys/malloc.h>
43d710367dSKristof Provost #include <sys/nv.h>
443b3a8eb9SGleb Smirnoff #include <sys/refcount.h>
45ec7b47fcSKristof Provost #include <sys/sdt.h>
468f2ac656SPatrick Kelsey #include <sys/sysctl.h>
47defdcdd5SMateusz Guzik #include <sys/smp.h>
48d25c25dcSKristof Provost #include <sys/lock.h>
49455969d3SKristof Provost #include <sys/rmlock.h>
503b3a8eb9SGleb Smirnoff #include <sys/tree.h>
51defdcdd5SMateusz Guzik #include <sys/seqc.h>
528ec07310SGleb Smirnoff #include <vm/uma.h>
533b3a8eb9SGleb Smirnoff 
54c658610bSKristof Provost #include <net/if.h>
55e732e742SKristof Provost #include <net/ethernet.h>
563b3a8eb9SGleb Smirnoff #include <net/radix.h>
573b3a8eb9SGleb Smirnoff #include <netinet/in.h>
58d38630f6SKristof Provost #ifdef _KERNEL
59d38630f6SKristof Provost #include <netinet/ip.h>
60d38630f6SKristof Provost #include <netinet/tcp.h>
61d38630f6SKristof Provost #include <netinet/udp.h>
62010ee43fSKristof Provost #include <netinet/sctp.h>
63d38630f6SKristof Provost #include <netinet/ip_icmp.h>
64d38630f6SKristof Provost #include <netinet/icmp6.h>
65d38630f6SKristof Provost #endif
663b3a8eb9SGleb Smirnoff 
6775bf2db3SGleb Smirnoff #include <netpfil/pf/pf.h>
6875bf2db3SGleb Smirnoff #include <netpfil/pf/pf_altq.h>
6975bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h>
703b3a8eb9SGleb Smirnoff 
713b3a8eb9SGleb Smirnoff #ifdef _KERNEL
723b3a8eb9SGleb Smirnoff 
73defdcdd5SMateusz Guzik #if defined(__arm__)
74defdcdd5SMateusz Guzik #define PF_WANT_32_TO_64_COUNTER
75defdcdd5SMateusz Guzik #endif
76defdcdd5SMateusz Guzik 
77defdcdd5SMateusz Guzik /*
78defdcdd5SMateusz Guzik  * A hybrid of 32-bit and 64-bit counters which can be used on platforms where
79defdcdd5SMateusz Guzik  * counter(9) is very expensive.
80defdcdd5SMateusz Guzik  *
81defdcdd5SMateusz Guzik  * As 32-bit counters are expected to overflow, a periodic job sums them up to
82defdcdd5SMateusz Guzik  * a saved 64-bit state. Fetching the value still walks all CPUs to get the most
83defdcdd5SMateusz Guzik  * current snapshot.
84defdcdd5SMateusz Guzik  */
85defdcdd5SMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
86defdcdd5SMateusz Guzik struct pf_counter_u64_pcpu {
87defdcdd5SMateusz Guzik 	u_int32_t current;
88defdcdd5SMateusz Guzik 	u_int32_t snapshot;
89defdcdd5SMateusz Guzik };
90defdcdd5SMateusz Guzik 
91defdcdd5SMateusz Guzik struct pf_counter_u64 {
92defdcdd5SMateusz Guzik 	struct pf_counter_u64_pcpu *pfcu64_pcpu;
93defdcdd5SMateusz Guzik 	u_int64_t pfcu64_value;
94defdcdd5SMateusz Guzik 	seqc_t	pfcu64_seqc;
95defdcdd5SMateusz Guzik };
96defdcdd5SMateusz Guzik 
97defdcdd5SMateusz Guzik static inline int
pf_counter_u64_init(struct pf_counter_u64 * pfcu64,int flags)98defdcdd5SMateusz Guzik pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
99defdcdd5SMateusz Guzik {
100defdcdd5SMateusz Guzik 
101defdcdd5SMateusz Guzik 	pfcu64->pfcu64_value = 0;
102defdcdd5SMateusz Guzik 	pfcu64->pfcu64_seqc = 0;
103defdcdd5SMateusz Guzik 	pfcu64->pfcu64_pcpu = uma_zalloc_pcpu(pcpu_zone_8, flags | M_ZERO);
104defdcdd5SMateusz Guzik 	if (__predict_false(pfcu64->pfcu64_pcpu == NULL))
105defdcdd5SMateusz Guzik 		return (ENOMEM);
106defdcdd5SMateusz Guzik 	return (0);
107defdcdd5SMateusz Guzik }
108defdcdd5SMateusz Guzik 
109defdcdd5SMateusz Guzik static inline void
pf_counter_u64_deinit(struct pf_counter_u64 * pfcu64)110defdcdd5SMateusz Guzik pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
111defdcdd5SMateusz Guzik {
112defdcdd5SMateusz Guzik 
113defdcdd5SMateusz Guzik 	uma_zfree_pcpu(pcpu_zone_8, pfcu64->pfcu64_pcpu);
114defdcdd5SMateusz Guzik }
115defdcdd5SMateusz Guzik 
116defdcdd5SMateusz Guzik static inline void
pf_counter_u64_critical_enter(void)117defdcdd5SMateusz Guzik pf_counter_u64_critical_enter(void)
118defdcdd5SMateusz Guzik {
119defdcdd5SMateusz Guzik 
120defdcdd5SMateusz Guzik 	critical_enter();
121defdcdd5SMateusz Guzik }
122defdcdd5SMateusz Guzik 
123defdcdd5SMateusz Guzik static inline void
pf_counter_u64_critical_exit(void)124defdcdd5SMateusz Guzik pf_counter_u64_critical_exit(void)
125defdcdd5SMateusz Guzik {
126defdcdd5SMateusz Guzik 
127defdcdd5SMateusz Guzik 	critical_exit();
128defdcdd5SMateusz Guzik }
129defdcdd5SMateusz Guzik 
130defdcdd5SMateusz Guzik static inline void
pf_counter_u64_add_protected(struct pf_counter_u64 * pfcu64,uint32_t n)131defdcdd5SMateusz Guzik pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
132defdcdd5SMateusz Guzik {
133defdcdd5SMateusz Guzik 	struct pf_counter_u64_pcpu *pcpu;
134defdcdd5SMateusz Guzik 	u_int32_t val;
135defdcdd5SMateusz Guzik 
136defdcdd5SMateusz Guzik 	MPASS(curthread->td_critnest > 0);
137defdcdd5SMateusz Guzik 	pcpu = zpcpu_get(pfcu64->pfcu64_pcpu);
138defdcdd5SMateusz Guzik 	val = atomic_load_int(&pcpu->current);
139defdcdd5SMateusz Guzik 	atomic_store_int(&pcpu->current, val + n);
140defdcdd5SMateusz Guzik }
141defdcdd5SMateusz Guzik 
142defdcdd5SMateusz Guzik static inline void
pf_counter_u64_add(struct pf_counter_u64 * pfcu64,uint32_t n)143defdcdd5SMateusz Guzik pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
144defdcdd5SMateusz Guzik {
145defdcdd5SMateusz Guzik 
146defdcdd5SMateusz Guzik 	critical_enter();
147defdcdd5SMateusz Guzik 	pf_counter_u64_add_protected(pfcu64, n);
148defdcdd5SMateusz Guzik 	critical_exit();
149defdcdd5SMateusz Guzik }
150defdcdd5SMateusz Guzik 
151defdcdd5SMateusz Guzik static inline u_int64_t
pf_counter_u64_periodic(struct pf_counter_u64 * pfcu64)152defdcdd5SMateusz Guzik pf_counter_u64_periodic(struct pf_counter_u64 *pfcu64)
153defdcdd5SMateusz Guzik {
154defdcdd5SMateusz Guzik 	struct pf_counter_u64_pcpu *pcpu;
155defdcdd5SMateusz Guzik 	u_int64_t sum;
156defdcdd5SMateusz Guzik 	u_int32_t val;
157defdcdd5SMateusz Guzik 	int cpu;
158defdcdd5SMateusz Guzik 
159defdcdd5SMateusz Guzik 	MPASS(curthread->td_critnest > 0);
160defdcdd5SMateusz Guzik 	seqc_write_begin(&pfcu64->pfcu64_seqc);
161defdcdd5SMateusz Guzik 	sum = pfcu64->pfcu64_value;
162defdcdd5SMateusz Guzik 	CPU_FOREACH(cpu) {
163defdcdd5SMateusz Guzik 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
164defdcdd5SMateusz Guzik 		val = atomic_load_int(&pcpu->current);
165defdcdd5SMateusz Guzik 		sum += (uint32_t)(val - pcpu->snapshot);
166defdcdd5SMateusz Guzik 		pcpu->snapshot = val;
167defdcdd5SMateusz Guzik 	}
168defdcdd5SMateusz Guzik 	pfcu64->pfcu64_value = sum;
169defdcdd5SMateusz Guzik 	seqc_write_end(&pfcu64->pfcu64_seqc);
170defdcdd5SMateusz Guzik 	return (sum);
171defdcdd5SMateusz Guzik }
172defdcdd5SMateusz Guzik 
173defdcdd5SMateusz Guzik static inline u_int64_t
pf_counter_u64_fetch(const struct pf_counter_u64 * pfcu64)174773e3a71SMark Johnston pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64)
175defdcdd5SMateusz Guzik {
176defdcdd5SMateusz Guzik 	struct pf_counter_u64_pcpu *pcpu;
177defdcdd5SMateusz Guzik 	u_int64_t sum;
178defdcdd5SMateusz Guzik 	seqc_t seqc;
179defdcdd5SMateusz Guzik 	int cpu;
180defdcdd5SMateusz Guzik 
181defdcdd5SMateusz Guzik 	for (;;) {
182defdcdd5SMateusz Guzik 		seqc = seqc_read(&pfcu64->pfcu64_seqc);
183defdcdd5SMateusz Guzik 		sum = 0;
184defdcdd5SMateusz Guzik 		CPU_FOREACH(cpu) {
185defdcdd5SMateusz Guzik 			pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
186defdcdd5SMateusz Guzik 			sum += (uint32_t)(atomic_load_int(&pcpu->current) -pcpu->snapshot);
187defdcdd5SMateusz Guzik 		}
188defdcdd5SMateusz Guzik 		sum += pfcu64->pfcu64_value;
189defdcdd5SMateusz Guzik 		if (seqc_consistent(&pfcu64->pfcu64_seqc, seqc))
190defdcdd5SMateusz Guzik 			break;
191defdcdd5SMateusz Guzik 	}
192defdcdd5SMateusz Guzik 	return (sum);
193defdcdd5SMateusz Guzik }
194defdcdd5SMateusz Guzik 
195defdcdd5SMateusz Guzik static inline void
pf_counter_u64_zero_protected(struct pf_counter_u64 * pfcu64)196defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
197defdcdd5SMateusz Guzik {
198defdcdd5SMateusz Guzik 	struct pf_counter_u64_pcpu *pcpu;
199defdcdd5SMateusz Guzik 	int cpu;
200defdcdd5SMateusz Guzik 
201defdcdd5SMateusz Guzik 	MPASS(curthread->td_critnest > 0);
202defdcdd5SMateusz Guzik 	seqc_write_begin(&pfcu64->pfcu64_seqc);
203defdcdd5SMateusz Guzik 	CPU_FOREACH(cpu) {
204defdcdd5SMateusz Guzik 		pcpu = zpcpu_get_cpu(pfcu64->pfcu64_pcpu, cpu);
205defdcdd5SMateusz Guzik 		pcpu->snapshot = atomic_load_int(&pcpu->current);
206defdcdd5SMateusz Guzik 	}
207defdcdd5SMateusz Guzik 	pfcu64->pfcu64_value = 0;
208defdcdd5SMateusz Guzik 	seqc_write_end(&pfcu64->pfcu64_seqc);
209defdcdd5SMateusz Guzik }
210defdcdd5SMateusz Guzik 
211defdcdd5SMateusz Guzik static inline void
pf_counter_u64_zero(struct pf_counter_u64 * pfcu64)212defdcdd5SMateusz Guzik pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
213defdcdd5SMateusz Guzik {
214defdcdd5SMateusz Guzik 
215defdcdd5SMateusz Guzik 	critical_enter();
216defdcdd5SMateusz Guzik 	pf_counter_u64_zero_protected(pfcu64);
217defdcdd5SMateusz Guzik 	critical_exit();
218defdcdd5SMateusz Guzik }
219defdcdd5SMateusz Guzik #else
220defdcdd5SMateusz Guzik struct pf_counter_u64 {
221defdcdd5SMateusz Guzik 	counter_u64_t counter;
222defdcdd5SMateusz Guzik };
223defdcdd5SMateusz Guzik 
224defdcdd5SMateusz Guzik static inline int
pf_counter_u64_init(struct pf_counter_u64 * pfcu64,int flags)225defdcdd5SMateusz Guzik pf_counter_u64_init(struct pf_counter_u64 *pfcu64, int flags)
226defdcdd5SMateusz Guzik {
227defdcdd5SMateusz Guzik 
228defdcdd5SMateusz Guzik 	pfcu64->counter = counter_u64_alloc(flags);
229defdcdd5SMateusz Guzik 	if (__predict_false(pfcu64->counter == NULL))
230defdcdd5SMateusz Guzik 		return (ENOMEM);
231defdcdd5SMateusz Guzik 	return (0);
232defdcdd5SMateusz Guzik }
233defdcdd5SMateusz Guzik 
234defdcdd5SMateusz Guzik static inline void
pf_counter_u64_deinit(struct pf_counter_u64 * pfcu64)235defdcdd5SMateusz Guzik pf_counter_u64_deinit(struct pf_counter_u64 *pfcu64)
236defdcdd5SMateusz Guzik {
237defdcdd5SMateusz Guzik 
238defdcdd5SMateusz Guzik 	counter_u64_free(pfcu64->counter);
239defdcdd5SMateusz Guzik }
240defdcdd5SMateusz Guzik 
241defdcdd5SMateusz Guzik static inline void
pf_counter_u64_critical_enter(void)242defdcdd5SMateusz Guzik pf_counter_u64_critical_enter(void)
243defdcdd5SMateusz Guzik {
244defdcdd5SMateusz Guzik 
245defdcdd5SMateusz Guzik }
246defdcdd5SMateusz Guzik 
247defdcdd5SMateusz Guzik static inline void
pf_counter_u64_critical_exit(void)248defdcdd5SMateusz Guzik pf_counter_u64_critical_exit(void)
249defdcdd5SMateusz Guzik {
250defdcdd5SMateusz Guzik 
251defdcdd5SMateusz Guzik }
252defdcdd5SMateusz Guzik 
253defdcdd5SMateusz Guzik static inline void
pf_counter_u64_add_protected(struct pf_counter_u64 * pfcu64,uint32_t n)254defdcdd5SMateusz Guzik pf_counter_u64_add_protected(struct pf_counter_u64 *pfcu64, uint32_t n)
255defdcdd5SMateusz Guzik {
256defdcdd5SMateusz Guzik 
257defdcdd5SMateusz Guzik 	counter_u64_add(pfcu64->counter, n);
258defdcdd5SMateusz Guzik }
259defdcdd5SMateusz Guzik 
260defdcdd5SMateusz Guzik static inline void
pf_counter_u64_add(struct pf_counter_u64 * pfcu64,uint32_t n)261defdcdd5SMateusz Guzik pf_counter_u64_add(struct pf_counter_u64 *pfcu64, uint32_t n)
262defdcdd5SMateusz Guzik {
263defdcdd5SMateusz Guzik 
264defdcdd5SMateusz Guzik 	pf_counter_u64_add_protected(pfcu64, n);
265defdcdd5SMateusz Guzik }
266defdcdd5SMateusz Guzik 
267defdcdd5SMateusz Guzik static inline u_int64_t
pf_counter_u64_fetch(const struct pf_counter_u64 * pfcu64)268773e3a71SMark Johnston pf_counter_u64_fetch(const struct pf_counter_u64 *pfcu64)
269defdcdd5SMateusz Guzik {
270defdcdd5SMateusz Guzik 
271defdcdd5SMateusz Guzik 	return (counter_u64_fetch(pfcu64->counter));
272defdcdd5SMateusz Guzik }
273defdcdd5SMateusz Guzik 
274defdcdd5SMateusz Guzik static inline void
pf_counter_u64_zero_protected(struct pf_counter_u64 * pfcu64)275defdcdd5SMateusz Guzik pf_counter_u64_zero_protected(struct pf_counter_u64 *pfcu64)
276defdcdd5SMateusz Guzik {
277defdcdd5SMateusz Guzik 
278defdcdd5SMateusz Guzik 	counter_u64_zero(pfcu64->counter);
279defdcdd5SMateusz Guzik }
280defdcdd5SMateusz Guzik 
281defdcdd5SMateusz Guzik static inline void
pf_counter_u64_zero(struct pf_counter_u64 * pfcu64)282defdcdd5SMateusz Guzik pf_counter_u64_zero(struct pf_counter_u64 *pfcu64)
283defdcdd5SMateusz Guzik {
284defdcdd5SMateusz Guzik 
285defdcdd5SMateusz Guzik 	pf_counter_u64_zero_protected(pfcu64);
286defdcdd5SMateusz Guzik }
287defdcdd5SMateusz Guzik #endif
288defdcdd5SMateusz Guzik 
2890abcc1d2SReid Linnemann #define pf_get_timestamp(prule)({					\
2900abcc1d2SReid Linnemann 	uint32_t _ts = 0;						\
2910abcc1d2SReid Linnemann 	uint32_t __ts;							\
2920abcc1d2SReid Linnemann 	int cpu;							\
2930abcc1d2SReid Linnemann 	CPU_FOREACH(cpu) {						\
2940abcc1d2SReid Linnemann 		__ts = *zpcpu_get_cpu(prule->timestamp, cpu);		\
2950abcc1d2SReid Linnemann 		if (__ts > _ts)						\
2960abcc1d2SReid Linnemann 			_ts = __ts;					\
2970abcc1d2SReid Linnemann 	}								\
2980abcc1d2SReid Linnemann 	_ts;								\
2990abcc1d2SReid Linnemann })
3000abcc1d2SReid Linnemann 
3010abcc1d2SReid Linnemann #define pf_update_timestamp(prule)					\
3020abcc1d2SReid Linnemann 	do {								\
3030abcc1d2SReid Linnemann 		critical_enter();					\
3040abcc1d2SReid Linnemann 		*zpcpu_get((prule)->timestamp) = time_second;		\
3050abcc1d2SReid Linnemann 		critical_exit();					\
3060abcc1d2SReid Linnemann 	} while (0)
3070abcc1d2SReid Linnemann 
3081d090028SKristof Provost #define pf_timestamp_pcpu_zone	(sizeof(time_t) == 4 ? pcpu_zone_4 : pcpu_zone_8)
3091d090028SKristof Provost _Static_assert(sizeof(time_t) == 4 || sizeof(time_t) == 8, "unexpected time_t size");
3100abcc1d2SReid Linnemann 
3118f2ac656SPatrick Kelsey SYSCTL_DECL(_net_pf);
3128f2ac656SPatrick Kelsey MALLOC_DECLARE(M_PFHASH);
31339282ef3SKajetan Staszkiewicz MALLOC_DECLARE(M_PF_RULE_ITEM);
3148f2ac656SPatrick Kelsey 
315ec7b47fcSKristof Provost SDT_PROVIDER_DECLARE(pf);
316ec7b47fcSKristof Provost 
3173b3a8eb9SGleb Smirnoff struct pfi_dynaddr {
3183b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
3193b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_addr4;
3203b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_mask4;
3213b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_addr6;
3223b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_mask6;
3233b3a8eb9SGleb Smirnoff 	struct pfr_ktable		*pfid_kt;
324320c1116SKristof Provost 	struct pfi_kkif			*pfid_kif;
3253b3a8eb9SGleb Smirnoff 	int				 pfid_net;	/* mask or 128 */
3263b3a8eb9SGleb Smirnoff 	int				 pfid_acnt4;	/* address count IPv4 */
3273b3a8eb9SGleb Smirnoff 	int				 pfid_acnt6;	/* address count IPv6 */
3283b3a8eb9SGleb Smirnoff 	sa_family_t			 pfid_af;	/* rule af */
3293b3a8eb9SGleb Smirnoff 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
3303b3a8eb9SGleb Smirnoff };
3313b3a8eb9SGleb Smirnoff 
3323b3a8eb9SGleb Smirnoff /*
3333b3a8eb9SGleb Smirnoff  * Address manipulation macros
3343b3a8eb9SGleb Smirnoff  */
3353b3a8eb9SGleb Smirnoff #define	HTONL(x)	(x) = htonl((__uint32_t)(x))
3363b3a8eb9SGleb Smirnoff #define	HTONS(x)	(x) = htons((__uint16_t)(x))
3373b3a8eb9SGleb Smirnoff #define	NTOHL(x)	(x) = ntohl((__uint32_t)(x))
3383b3a8eb9SGleb Smirnoff #define	NTOHS(x)	(x) = ntohs((__uint16_t)(x))
3393b3a8eb9SGleb Smirnoff 
3403b3a8eb9SGleb Smirnoff #define	PF_NAME		"pf"
3413b3a8eb9SGleb Smirnoff 
3423b3a8eb9SGleb Smirnoff #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED)
3433b3a8eb9SGleb Smirnoff #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock)
3443b3a8eb9SGleb Smirnoff #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock)
3453b3a8eb9SGleb Smirnoff 
346907257d6SMateusz Guzik #ifdef INVARIANTS
3473b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK(s)						\
3483b3a8eb9SGleb Smirnoff 	do {								\
349907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
350907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
351907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
352907257d6SMateusz Guzik 		mtx_lock(_s->lock);					\
3533b3a8eb9SGleb Smirnoff 	} while (0)
3543b3a8eb9SGleb Smirnoff #define	PF_STATE_UNLOCK(s)						\
3553b3a8eb9SGleb Smirnoff 	do {								\
356907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
357907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
358907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
359907257d6SMateusz Guzik 		mtx_unlock(_s->lock);					\
3603b3a8eb9SGleb Smirnoff 	} while (0)
361907257d6SMateusz Guzik #else
362907257d6SMateusz Guzik #define	PF_STATE_LOCK(s)	mtx_lock(s->lock)
363907257d6SMateusz Guzik #define	PF_STATE_UNLOCK(s)	mtx_unlock(s->lock)
364907257d6SMateusz Guzik #endif
3653b3a8eb9SGleb Smirnoff 
3663b3a8eb9SGleb Smirnoff #ifdef INVARIANTS
3673b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK_ASSERT(s)						\
3683b3a8eb9SGleb Smirnoff 	do {								\
369907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
370907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
371907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
3723b3a8eb9SGleb Smirnoff 		PF_HASHROW_ASSERT(_ih);					\
3733b3a8eb9SGleb Smirnoff 	} while (0)
3743b3a8eb9SGleb Smirnoff #else /* !INVARIANTS */
3753b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0)
3763b3a8eb9SGleb Smirnoff #endif /* INVARIANTS */
3773b3a8eb9SGleb Smirnoff 
378db0a2bfdSKajetan Staszkiewicz #ifdef INVARIANTS
379db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_LOCK(sn)						\
380db0a2bfdSKajetan Staszkiewicz 	do {								\
381db0a2bfdSKajetan Staszkiewicz 		struct pf_ksrc_node *_sn = (sn);			\
382db0a2bfdSKajetan Staszkiewicz 		struct pf_srchash *_sh = &V_pf_srchash[			\
383db0a2bfdSKajetan Staszkiewicz 		    pf_hashsrc(&_sn->addr, _sn->af)];			\
384db0a2bfdSKajetan Staszkiewicz 		MPASS(_sn->lock == &_sh->lock);				\
385db0a2bfdSKajetan Staszkiewicz 		mtx_lock(_sn->lock);					\
386db0a2bfdSKajetan Staszkiewicz 	} while (0)
387db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_UNLOCK(sn)						\
388db0a2bfdSKajetan Staszkiewicz 	do {								\
389db0a2bfdSKajetan Staszkiewicz 		struct pf_ksrc_node *_sn = (sn);			\
390db0a2bfdSKajetan Staszkiewicz 		struct pf_srchash *_sh = &V_pf_srchash[			\
391db0a2bfdSKajetan Staszkiewicz 		    pf_hashsrc(&_sn->addr, _sn->af)];			\
392db0a2bfdSKajetan Staszkiewicz 		MPASS(_sn->lock == &_sh->lock);				\
393db0a2bfdSKajetan Staszkiewicz 		mtx_unlock(_sn->lock);					\
394db0a2bfdSKajetan Staszkiewicz 	} while (0)
395db0a2bfdSKajetan Staszkiewicz #else
396db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_LOCK(sn)	mtx_lock((sn)->lock)
397db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_UNLOCK(sn)	mtx_unlock((sn)->lock)
398db0a2bfdSKajetan Staszkiewicz #endif
399db0a2bfdSKajetan Staszkiewicz 
400db0a2bfdSKajetan Staszkiewicz #ifdef INVARIANTS
401db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_LOCK_ASSERT(sn)					\
402db0a2bfdSKajetan Staszkiewicz 	do {								\
403db0a2bfdSKajetan Staszkiewicz 		struct pf_ksrc_node *_sn = (sn);			\
404db0a2bfdSKajetan Staszkiewicz 		struct pf_srchash *_sh = &V_pf_srchash[			\
405db0a2bfdSKajetan Staszkiewicz 		    pf_hashsrc(&_sn->addr, _sn->af)];			\
406db0a2bfdSKajetan Staszkiewicz 		MPASS(_sn->lock == &_sh->lock);				\
407db0a2bfdSKajetan Staszkiewicz 		PF_HASHROW_ASSERT(_sh);					\
408db0a2bfdSKajetan Staszkiewicz 	} while (0)
409db0a2bfdSKajetan Staszkiewicz #else /* !INVARIANTS */
410db0a2bfdSKajetan Staszkiewicz #define	PF_SRC_NODE_LOCK_ASSERT(sn)		do {} while (0)
411db0a2bfdSKajetan Staszkiewicz #endif /* INVARIANTS */
412db0a2bfdSKajetan Staszkiewicz 
413f649cff5SMateusz Guzik extern struct mtx_padalign pf_unlnkdrules_mtx;
4143b3a8eb9SGleb Smirnoff #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx)
4153b3a8eb9SGleb Smirnoff #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx)
416f11b6505SMateusz Guzik #define	PF_UNLNKDRULES_ASSERT()	mtx_assert(&pf_unlnkdrules_mtx, MA_OWNED)
4173b3a8eb9SGleb Smirnoff 
41893f8c38cSMateusz Guzik extern struct sx pf_config_lock;
41993f8c38cSMateusz Guzik #define	PF_CONFIG_LOCK()	sx_xlock(&pf_config_lock)
42093f8c38cSMateusz Guzik #define	PF_CONFIG_UNLOCK()	sx_xunlock(&pf_config_lock)
42193f8c38cSMateusz Guzik #define	PF_CONFIG_ASSERT()	sx_assert(&pf_config_lock, SA_XLOCKED)
42293f8c38cSMateusz Guzik 
4232e6cdfe2SKristof Provost VNET_DECLARE(struct rmlock, pf_rules_lock);
4242e6cdfe2SKristof Provost #define	V_pf_rules_lock		VNET(pf_rules_lock)
4252e6cdfe2SKristof Provost 
426455969d3SKristof Provost #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker
4272e6cdfe2SKristof Provost #define	PF_RULES_RLOCK()	rm_rlock(&V_pf_rules_lock, &_pf_rules_tracker)
4282e6cdfe2SKristof Provost #define	PF_RULES_RUNLOCK()	rm_runlock(&V_pf_rules_lock, &_pf_rules_tracker)
4292e6cdfe2SKristof Provost #define	PF_RULES_WLOCK()	rm_wlock(&V_pf_rules_lock)
4302e6cdfe2SKristof Provost #define	PF_RULES_WUNLOCK()	rm_wunlock(&V_pf_rules_lock)
4312e6cdfe2SKristof Provost #define	PF_RULES_WOWNED()	rm_wowned(&V_pf_rules_lock)
4322e6cdfe2SKristof Provost #define	PF_RULES_ASSERT()	rm_assert(&V_pf_rules_lock, RA_LOCKED)
4332e6cdfe2SKristof Provost #define	PF_RULES_RASSERT()	rm_assert(&V_pf_rules_lock, RA_RLOCKED)
4342e6cdfe2SKristof Provost #define	PF_RULES_WASSERT()	rm_assert(&V_pf_rules_lock, RA_WLOCKED)
4353b3a8eb9SGleb Smirnoff 
436f649cff5SMateusz Guzik extern struct mtx_padalign pf_table_stats_lock;
437dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_LOCK()	mtx_lock(&pf_table_stats_lock)
438dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_UNLOCK()	mtx_unlock(&pf_table_stats_lock)
439dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_OWNED()	mtx_owned(&pf_table_stats_lock)
440af94d8ccSKristof Provost #define	PF_TABLE_STATS_ASSERT()	mtx_assert(&pf_table_stats_lock, MA_OWNED)
441dc1ab04eSMateusz Guzik 
4422f8fb3a8SKristof Provost extern struct sx pf_end_lock;
4432f8fb3a8SKristof Provost 
4443b3a8eb9SGleb Smirnoff #define	PF_MODVER	1
4453b3a8eb9SGleb Smirnoff #define	PFLOG_MODVER	1
4463b3a8eb9SGleb Smirnoff #define	PFSYNC_MODVER	1
4473b3a8eb9SGleb Smirnoff 
4483b3a8eb9SGleb Smirnoff #define	PFLOG_MINVER	1
4493b3a8eb9SGleb Smirnoff #define	PFLOG_PREFVER	PFLOG_MODVER
4503b3a8eb9SGleb Smirnoff #define	PFLOG_MAXVER	1
4513b3a8eb9SGleb Smirnoff #define	PFSYNC_MINVER	1
4523b3a8eb9SGleb Smirnoff #define	PFSYNC_PREFVER	PFSYNC_MODVER
4533b3a8eb9SGleb Smirnoff #define	PFSYNC_MAXVER	1
4543b3a8eb9SGleb Smirnoff 
4553b3a8eb9SGleb Smirnoff #ifdef INET
4563b3a8eb9SGleb Smirnoff #ifndef INET6
4573b3a8eb9SGleb Smirnoff #define	PF_INET_ONLY
4583b3a8eb9SGleb Smirnoff #endif /* ! INET6 */
4593b3a8eb9SGleb Smirnoff #endif /* INET */
4603b3a8eb9SGleb Smirnoff 
4613b3a8eb9SGleb Smirnoff #ifdef INET6
4623b3a8eb9SGleb Smirnoff #ifndef INET
4633b3a8eb9SGleb Smirnoff #define	PF_INET6_ONLY
4643b3a8eb9SGleb Smirnoff #endif /* ! INET */
4653b3a8eb9SGleb Smirnoff #endif /* INET6 */
4663b3a8eb9SGleb Smirnoff 
4673b3a8eb9SGleb Smirnoff #ifdef INET
4683b3a8eb9SGleb Smirnoff #ifdef INET6
4693b3a8eb9SGleb Smirnoff #define	PF_INET_INET6
4703b3a8eb9SGleb Smirnoff #endif /* INET6 */
4713b3a8eb9SGleb Smirnoff #endif /* INET */
4723b3a8eb9SGleb Smirnoff 
4733b3a8eb9SGleb Smirnoff #else
4743b3a8eb9SGleb Smirnoff 
4753b3a8eb9SGleb Smirnoff #define	PF_INET_INET6
4763b3a8eb9SGleb Smirnoff 
4773b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
4783b3a8eb9SGleb Smirnoff 
4793b3a8eb9SGleb Smirnoff /* Both IPv4 and IPv6 */
4803b3a8eb9SGleb Smirnoff #ifdef PF_INET_INET6
4813b3a8eb9SGleb Smirnoff 
4823b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
4833b3a8eb9SGleb Smirnoff 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
4846332e4ccSGeorge V. Neville-Neil 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
4853b3a8eb9SGleb Smirnoff 	(a)->addr32[2] == (b)->addr32[2] && \
4863b3a8eb9SGleb Smirnoff 	(a)->addr32[1] == (b)->addr32[1] && \
4873b3a8eb9SGleb Smirnoff 	(a)->addr32[0] == (b)->addr32[0])) \
4883b3a8eb9SGleb Smirnoff 
4893b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
4907d762423SKristof Provost 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
49126022843SKristof Provost 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
4923b3a8eb9SGleb Smirnoff 	(a)->addr32[1] != (b)->addr32[1] || \
4936332e4ccSGeorge V. Neville-Neil 	(a)->addr32[2] != (b)->addr32[2] || \
49426022843SKristof Provost 	(a)->addr32[3] != (b)->addr32[3]))) \
4953b3a8eb9SGleb Smirnoff 
4963b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
4973b3a8eb9SGleb Smirnoff 	((c == AF_INET && !(a)->addr32[0]) || \
4986332e4ccSGeorge V. Neville-Neil 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
4993b3a8eb9SGleb Smirnoff 	!(a)->addr32[2] && !(a)->addr32[3] )) \
5003b3a8eb9SGleb Smirnoff 
5013b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
5023b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
5033b3a8eb9SGleb Smirnoff 
5043b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
5053b3a8eb9SGleb Smirnoff 	pf_addrcpy(a, b, f)
5063b3a8eb9SGleb Smirnoff 
5073b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
5083b3a8eb9SGleb Smirnoff 	pf_addr_inc(a, f)
5093b3a8eb9SGleb Smirnoff 
5103b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
5113b3a8eb9SGleb Smirnoff 	pf_poolmask(a, b, c, d, f)
5123b3a8eb9SGleb Smirnoff 
5133b3a8eb9SGleb Smirnoff #else
5143b3a8eb9SGleb Smirnoff 
5153b3a8eb9SGleb Smirnoff /* Just IPv6 */
5163b3a8eb9SGleb Smirnoff 
5173b3a8eb9SGleb Smirnoff #ifdef PF_INET6_ONLY
5183b3a8eb9SGleb Smirnoff 
5193b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
5203b3a8eb9SGleb Smirnoff 	((a)->addr32[3] == (b)->addr32[3] && \
5213b3a8eb9SGleb Smirnoff 	(a)->addr32[2] == (b)->addr32[2] && \
5223b3a8eb9SGleb Smirnoff 	(a)->addr32[1] == (b)->addr32[1] && \
5233b3a8eb9SGleb Smirnoff 	(a)->addr32[0] == (b)->addr32[0]) \
5243b3a8eb9SGleb Smirnoff 
5253b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
5263b3a8eb9SGleb Smirnoff 	((a)->addr32[3] != (b)->addr32[3] || \
5273b3a8eb9SGleb Smirnoff 	(a)->addr32[2] != (b)->addr32[2] || \
5283b3a8eb9SGleb Smirnoff 	(a)->addr32[1] != (b)->addr32[1] || \
5293b3a8eb9SGleb Smirnoff 	(a)->addr32[0] != (b)->addr32[0]) \
5303b3a8eb9SGleb Smirnoff 
5313b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
5323b3a8eb9SGleb Smirnoff 	(!(a)->addr32[0] && \
5333b3a8eb9SGleb Smirnoff 	!(a)->addr32[1] && \
5343b3a8eb9SGleb Smirnoff 	!(a)->addr32[2] && \
5353b3a8eb9SGleb Smirnoff 	!(a)->addr32[3] ) \
5363b3a8eb9SGleb Smirnoff 
5373b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
5383b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
5393b3a8eb9SGleb Smirnoff 
5403b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
5413b3a8eb9SGleb Smirnoff 	pf_addrcpy(a, b, f)
5423b3a8eb9SGleb Smirnoff 
5433b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
5443b3a8eb9SGleb Smirnoff 	pf_addr_inc(a, f)
5453b3a8eb9SGleb Smirnoff 
5463b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
5473b3a8eb9SGleb Smirnoff 	pf_poolmask(a, b, c, d, f)
5483b3a8eb9SGleb Smirnoff 
5493b3a8eb9SGleb Smirnoff #else
5503b3a8eb9SGleb Smirnoff 
5513b3a8eb9SGleb Smirnoff /* Just IPv4 */
5523b3a8eb9SGleb Smirnoff #ifdef PF_INET_ONLY
5533b3a8eb9SGleb Smirnoff 
5543b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
5553b3a8eb9SGleb Smirnoff 	((a)->addr32[0] == (b)->addr32[0])
5563b3a8eb9SGleb Smirnoff 
5573b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
5583b3a8eb9SGleb Smirnoff 	((a)->addr32[0] != (b)->addr32[0])
5593b3a8eb9SGleb Smirnoff 
5603b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
5613b3a8eb9SGleb Smirnoff 	(!(a)->addr32[0])
5623b3a8eb9SGleb Smirnoff 
5633b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
5643b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
5653b3a8eb9SGleb Smirnoff 
5663b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
5673b3a8eb9SGleb Smirnoff 	(a)->v4.s_addr = (b)->v4.s_addr
5683b3a8eb9SGleb Smirnoff 
5693b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
5703b3a8eb9SGleb Smirnoff 	do { \
5713b3a8eb9SGleb Smirnoff 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
5723b3a8eb9SGleb Smirnoff 	} while (0)
5733b3a8eb9SGleb Smirnoff 
5743b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
5753b3a8eb9SGleb Smirnoff 	do { \
5763b3a8eb9SGleb Smirnoff 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
5773b3a8eb9SGleb Smirnoff 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
5783b3a8eb9SGleb Smirnoff 	} while (0)
5793b3a8eb9SGleb Smirnoff 
5803b3a8eb9SGleb Smirnoff #endif /* PF_INET_ONLY */
5813b3a8eb9SGleb Smirnoff #endif /* PF_INET6_ONLY */
5823b3a8eb9SGleb Smirnoff #endif /* PF_INET_INET6 */
5833b3a8eb9SGleb Smirnoff 
5843b3a8eb9SGleb Smirnoff /*
5853b3a8eb9SGleb Smirnoff  * XXX callers not FIB-aware in our version of pf yet.
5863b3a8eb9SGleb Smirnoff  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
5873b3a8eb9SGleb Smirnoff  */
5883b3a8eb9SGleb Smirnoff #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\
5893b3a8eb9SGleb Smirnoff 	(								\
5903b3a8eb9SGleb Smirnoff 		(((aw)->type == PF_ADDR_NOROUTE &&			\
5913b3a8eb9SGleb Smirnoff 		    pf_routable((x), (af), NULL, (rtid))) ||		\
5923b3a8eb9SGleb Smirnoff 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
5933b3a8eb9SGleb Smirnoff 		    pf_routable((x), (af), (ifp), (rtid))) ||		\
5943b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_TABLE &&				\
5953b3a8eb9SGleb Smirnoff 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
5963b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_DYNIFTL &&			\
5973b3a8eb9SGleb Smirnoff 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\
5983b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_RANGE &&				\
5993b3a8eb9SGleb Smirnoff 		    !pf_match_addr_range(&(aw)->v.a.addr,		\
6003b3a8eb9SGleb Smirnoff 		    &(aw)->v.a.mask, (x), (af))) ||			\
6013b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_ADDRMASK &&			\
6023b3a8eb9SGleb Smirnoff 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
6033b3a8eb9SGleb Smirnoff 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
6043b3a8eb9SGleb Smirnoff 		    &(aw)->v.a.mask, (x), (af))))) !=			\
6053b3a8eb9SGleb Smirnoff 		(neg)							\
6063b3a8eb9SGleb Smirnoff 	)
6073b3a8eb9SGleb Smirnoff 
608c3f69af0SKristof Provost #define PF_ALGNMNT(off) (((off) % 2) == 0)
609c3f69af0SKristof Provost 
61017ad7334SKristof Provost #ifdef _KERNEL
611e86bddeaSKristof Provost 
612320c1116SKristof Provost struct pf_kpooladdr {
613320c1116SKristof Provost 	struct pf_addr_wrap		 addr;
614320c1116SKristof Provost 	TAILQ_ENTRY(pf_kpooladdr)	 entries;
615320c1116SKristof Provost 	char				 ifname[IFNAMSIZ];
616320c1116SKristof Provost 	struct pfi_kkif			*kif;
617320c1116SKristof Provost };
618320c1116SKristof Provost 
619320c1116SKristof Provost TAILQ_HEAD(pf_kpalist, pf_kpooladdr);
620320c1116SKristof Provost 
621320c1116SKristof Provost struct pf_kpool {
6225f5e32f1SKristof Provost 	struct mtx		 mtx;
623320c1116SKristof Provost 	struct pf_kpalist	 list;
624320c1116SKristof Provost 	struct pf_kpooladdr	*cur;
625320c1116SKristof Provost 	struct pf_poolhashkey	 key;
626320c1116SKristof Provost 	struct pf_addr		 counter;
6272aa21096SKurosawa Takahiro 	struct pf_mape_portset	 mape;
628320c1116SKristof Provost 	int			 tblidx;
629320c1116SKristof Provost 	u_int16_t		 proxy_port[2];
630320c1116SKristof Provost 	u_int8_t		 opts;
631320c1116SKristof Provost };
632320c1116SKristof Provost 
633ef950daaSKristof Provost struct pf_rule_actions {
6348c23afdbSKajetan Staszkiewicz 	int32_t		 rtableid;
635b64f7ce9SKristof Provost 	uint16_t	 qid;
636b64f7ce9SKristof Provost 	uint16_t	 pqid;
63739282ef3SKajetan Staszkiewicz 	uint16_t	 max_mss;
63839282ef3SKajetan Staszkiewicz 	uint8_t		 log;
63939282ef3SKajetan Staszkiewicz 	uint8_t		 set_tos;
64039282ef3SKajetan Staszkiewicz 	uint8_t		 min_ttl;
64163b3c1c7SKristof Provost 	uint16_t	 dnpipe;
64263b3c1c7SKristof Provost 	uint16_t	 dnrpipe;	/* Reverse direction pipe */
64363b3c1c7SKristof Provost 	uint32_t	 flags;
6444bf98559SKajetan Staszkiewicz 	uint8_t		 set_prio[2];
645ef950daaSKristof Provost };
646ef950daaSKristof Provost 
647e732e742SKristof Provost union pf_keth_rule_ptr {
648e732e742SKristof Provost 	struct pf_keth_rule	*ptr;
649e732e742SKristof Provost 	uint32_t		nr;
650e732e742SKristof Provost };
651e732e742SKristof Provost 
652e732e742SKristof Provost struct pf_keth_rule_addr {
653e732e742SKristof Provost 	uint8_t	addr[ETHER_ADDR_LEN];
654b590f17aSKristof Provost 	uint8_t	mask[ETHER_ADDR_LEN];
655e732e742SKristof Provost 	bool neg;
656e732e742SKristof Provost 	uint8_t	isset;
657e732e742SKristof Provost };
658e732e742SKristof Provost 
659c5131afeSKristof Provost struct pf_keth_anchor;
660c5131afeSKristof Provost 
661c5131afeSKristof Provost TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule);
662c5131afeSKristof Provost 
663c5131afeSKristof Provost struct pf_keth_ruleset {
664c5131afeSKristof Provost 	struct pf_keth_ruleq		 rules[2];
665c5131afeSKristof Provost 	struct pf_keth_rules {
666c5131afeSKristof Provost 		struct pf_keth_ruleq	*rules;
667c5131afeSKristof Provost 		int			 open;
668c5131afeSKristof Provost 		uint32_t		 ticket;
669c5131afeSKristof Provost 	} active, inactive;
670c5131afeSKristof Provost 	struct epoch_context	 epoch_ctx;
671c5131afeSKristof Provost 	struct vnet		*vnet;
672c5131afeSKristof Provost 	struct pf_keth_anchor	*anchor;
673c5131afeSKristof Provost };
674c5131afeSKristof Provost 
675c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_global, pf_keth_anchor);
676c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_node, pf_keth_anchor);
677c5131afeSKristof Provost struct pf_keth_anchor {
678c5131afeSKristof Provost 	RB_ENTRY(pf_keth_anchor)	 entry_node;
679c5131afeSKristof Provost 	RB_ENTRY(pf_keth_anchor)	 entry_global;
680c5131afeSKristof Provost 	struct pf_keth_anchor		*parent;
681c5131afeSKristof Provost 	struct pf_keth_anchor_node	 children;
682c5131afeSKristof Provost 	char				 name[PF_ANCHOR_NAME_SIZE];
683c5131afeSKristof Provost 	char				 path[MAXPATHLEN];
684c5131afeSKristof Provost 	struct pf_keth_ruleset		 ruleset;
685c5131afeSKristof Provost 	int				 refcnt;	/* anchor rules */
686c5131afeSKristof Provost 	uint8_t				 anchor_relative;
687c5131afeSKristof Provost 	uint8_t				 anchor_wildcard;
688c5131afeSKristof Provost };
689c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node,
690c5131afeSKristof Provost     pf_keth_anchor_compare);
691c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global,
692c5131afeSKristof Provost     pf_keth_anchor_compare);
693c5131afeSKristof Provost 
694e732e742SKristof Provost struct pf_keth_rule {
695e732e742SKristof Provost #define PFE_SKIP_IFP		0
696e732e742SKristof Provost #define PFE_SKIP_DIR		1
697e732e742SKristof Provost #define PFE_SKIP_PROTO		2
698e732e742SKristof Provost #define PFE_SKIP_SRC_ADDR	3
699e732e742SKristof Provost #define PFE_SKIP_DST_ADDR	4
700ba94bf28SKristof Provost #define PFE_SKIP_SRC_IP_ADDR	5
701ba94bf28SKristof Provost #define PFE_SKIP_DST_IP_ADDR	6
702ba94bf28SKristof Provost #define PFE_SKIP_COUNT		7
703e732e742SKristof Provost 	union pf_keth_rule_ptr	 skip[PFE_SKIP_COUNT];
704e732e742SKristof Provost 
705e732e742SKristof Provost 	TAILQ_ENTRY(pf_keth_rule)	entries;
706e732e742SKristof Provost 
707c5131afeSKristof Provost 	struct pf_keth_anchor	*anchor;
708c5131afeSKristof Provost 	u_int8_t		 anchor_relative;
709c5131afeSKristof Provost 	u_int8_t		 anchor_wildcard;
710c5131afeSKristof Provost 
711e732e742SKristof Provost 	uint32_t		 nr;
712e732e742SKristof Provost 
713e732e742SKristof Provost 	bool			 quick;
714e732e742SKristof Provost 
715e732e742SKristof Provost 	/* Filter */
716e732e742SKristof Provost 	char			 ifname[IFNAMSIZ];
717e732e742SKristof Provost 	struct pfi_kkif		*kif;
718e732e742SKristof Provost 	bool			 ifnot;
719e732e742SKristof Provost 	uint8_t			 direction;
720e732e742SKristof Provost 	uint16_t		 proto;
721e732e742SKristof Provost 	struct pf_keth_rule_addr src, dst;
7228a42005dSKristof Provost 	struct pf_rule_addr	 ipsrc, ipdst;
7231f61367fSKristof Provost 	char			 match_tagname[PF_TAG_NAME_SIZE];
7241f61367fSKristof Provost 	uint16_t		 match_tag;
7251f61367fSKristof Provost 	bool			 match_tag_not;
7261f61367fSKristof Provost 
727e732e742SKristof Provost 
728e732e742SKristof Provost 	/* Stats */
729e732e742SKristof Provost 	counter_u64_t		 evaluations;
730e732e742SKristof Provost 	counter_u64_t		 packets[2];
731e732e742SKristof Provost 	counter_u64_t		 bytes[2];
7321d090028SKristof Provost 	time_t			*timestamp;
733e732e742SKristof Provost 
734e732e742SKristof Provost 	/* Action */
735e732e742SKristof Provost 	char			 qname[PF_QNAME_SIZE];
736e732e742SKristof Provost 	int			 qid;
737e732e742SKristof Provost 	char			 tagname[PF_TAG_NAME_SIZE];
738e732e742SKristof Provost 	uint16_t		 tag;
7398a8af942SKristof Provost 	char			 bridge_to_name[IFNAMSIZ];
7408a8af942SKristof Provost 	struct pfi_kkif		*bridge_to;
741e732e742SKristof Provost 	uint8_t			 action;
742fb330f39SKristof Provost 	uint16_t		 dnpipe;
743fb330f39SKristof Provost 	uint32_t		 dnflags;
744ef661d4aSChristian McDonald 
745ef661d4aSChristian McDonald 	char			label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
746ef661d4aSChristian McDonald 	uint32_t		ridentifier;
747e732e742SKristof Provost };
748e732e742SKristof Provost 
749e86bddeaSKristof Provost union pf_krule_ptr {
750e86bddeaSKristof Provost 	struct pf_krule		*ptr;
751e86bddeaSKristof Provost 	u_int32_t		 nr;
752e86bddeaSKristof Provost };
753e86bddeaSKristof Provost 
754bd7762c8SMateusz Guzik RB_HEAD(pf_krule_global, pf_krule);
755bd7762c8SMateusz Guzik RB_PROTOTYPE(pf_krule_global, pf_krule, entry_global, pf_krule_compare);
756bd7762c8SMateusz Guzik 
757e86bddeaSKristof Provost struct pf_krule {
758e86bddeaSKristof Provost 	struct pf_rule_addr	 src;
759e86bddeaSKristof Provost 	struct pf_rule_addr	 dst;
760e86bddeaSKristof Provost 	union pf_krule_ptr	 skip[PF_SKIP_COUNT];
7616fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
76276c5eeccSKristof Provost 	uint32_t		 ridentifier;
763e86bddeaSKristof Provost 	char			 ifname[IFNAMSIZ];
764e86bddeaSKristof Provost 	char			 qname[PF_QNAME_SIZE];
765e86bddeaSKristof Provost 	char			 pqname[PF_QNAME_SIZE];
766e86bddeaSKristof Provost 	char			 tagname[PF_TAG_NAME_SIZE];
767e86bddeaSKristof Provost 	char			 match_tagname[PF_TAG_NAME_SIZE];
768e86bddeaSKristof Provost 
769e86bddeaSKristof Provost 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
770e86bddeaSKristof Provost 
771e86bddeaSKristof Provost 	TAILQ_ENTRY(pf_krule)	 entries;
772320c1116SKristof Provost 	struct pf_kpool		 rpool;
773e86bddeaSKristof Provost 
77402cf67ccSMateusz Guzik 	struct pf_counter_u64	 evaluations;
77502cf67ccSMateusz Guzik 	struct pf_counter_u64	 packets[2];
77602cf67ccSMateusz Guzik 	struct pf_counter_u64	 bytes[2];
7771d090028SKristof Provost 	time_t			*timestamp;
778e86bddeaSKristof Provost 
779320c1116SKristof Provost 	struct pfi_kkif		*kif;
780e86bddeaSKristof Provost 	struct pf_kanchor	*anchor;
781e86bddeaSKristof Provost 	struct pfr_ktable	*overload_tbl;
782e86bddeaSKristof Provost 
783e86bddeaSKristof Provost 	pf_osfp_t		 os_fingerprint;
784e86bddeaSKristof Provost 
7858c23afdbSKajetan Staszkiewicz 	int32_t			 rtableid;
786e86bddeaSKristof Provost 	u_int32_t		 timeout[PFTM_MAX];
787e86bddeaSKristof Provost 	u_int32_t		 max_states;
788e86bddeaSKristof Provost 	u_int32_t		 max_src_nodes;
789e86bddeaSKristof Provost 	u_int32_t		 max_src_states;
790e86bddeaSKristof Provost 	u_int32_t		 max_src_conn;
791e86bddeaSKristof Provost 	struct {
792e86bddeaSKristof Provost 		u_int32_t		limit;
793e86bddeaSKristof Provost 		u_int32_t		seconds;
794e86bddeaSKristof Provost 	}			 max_src_conn_rate;
795b64f7ce9SKristof Provost 	u_int16_t		 qid;
796b64f7ce9SKristof Provost 	u_int16_t		 pqid;
79763b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
79863b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
79963b3c1c7SKristof Provost 	u_int32_t		 free_flags;
800e86bddeaSKristof Provost 	u_int32_t		 nr;
801e86bddeaSKristof Provost 	u_int32_t		 prob;
802e86bddeaSKristof Provost 	uid_t			 cuid;
803e86bddeaSKristof Provost 	pid_t			 cpid;
804e86bddeaSKristof Provost 
805e86bddeaSKristof Provost 	counter_u64_t		 states_cur;
806e86bddeaSKristof Provost 	counter_u64_t		 states_tot;
807e86bddeaSKristof Provost 	counter_u64_t		 src_nodes;
808e86bddeaSKristof Provost 
809e86bddeaSKristof Provost 	u_int16_t		 return_icmp;
810e86bddeaSKristof Provost 	u_int16_t		 return_icmp6;
811e86bddeaSKristof Provost 	u_int16_t		 max_mss;
812e86bddeaSKristof Provost 	u_int16_t		 tag;
813e86bddeaSKristof Provost 	u_int16_t		 match_tag;
814e86bddeaSKristof Provost 	u_int16_t		 scrub_flags;
815e86bddeaSKristof Provost 
816e86bddeaSKristof Provost 	struct pf_rule_uid	 uid;
817e86bddeaSKristof Provost 	struct pf_rule_gid	 gid;
818e86bddeaSKristof Provost 
819e86bddeaSKristof Provost 	u_int32_t		 rule_flag;
8204f1f67e8SKristof Provost 	uint32_t		 rule_ref;
821e86bddeaSKristof Provost 	u_int8_t		 action;
822e86bddeaSKristof Provost 	u_int8_t		 direction;
823e86bddeaSKristof Provost 	u_int8_t		 log;
824e86bddeaSKristof Provost 	u_int8_t		 logif;
825e86bddeaSKristof Provost 	u_int8_t		 quick;
826e86bddeaSKristof Provost 	u_int8_t		 ifnot;
827e86bddeaSKristof Provost 	u_int8_t		 match_tag_not;
828e86bddeaSKristof Provost 	u_int8_t		 natpass;
829e86bddeaSKristof Provost 
830e86bddeaSKristof Provost 	u_int8_t		 keep_state;
831e86bddeaSKristof Provost 	sa_family_t		 af;
832e86bddeaSKristof Provost 	u_int8_t		 proto;
833e86bddeaSKristof Provost 	u_int8_t		 type;
834e86bddeaSKristof Provost 	u_int8_t		 code;
835e86bddeaSKristof Provost 	u_int8_t		 flags;
836e86bddeaSKristof Provost 	u_int8_t		 flagset;
837e86bddeaSKristof Provost 	u_int8_t		 min_ttl;
838e86bddeaSKristof Provost 	u_int8_t		 allow_opts;
839e86bddeaSKristof Provost 	u_int8_t		 rt;
840e86bddeaSKristof Provost 	u_int8_t		 return_ttl;
841e86bddeaSKristof Provost 	u_int8_t		 tos;
842e86bddeaSKristof Provost 	u_int8_t		 set_tos;
843e86bddeaSKristof Provost 	u_int8_t		 anchor_relative;
844e86bddeaSKristof Provost 	u_int8_t		 anchor_wildcard;
845e86bddeaSKristof Provost 
846e86bddeaSKristof Provost 	u_int8_t		 flush;
847e86bddeaSKristof Provost 	u_int8_t		 prio;
848e86bddeaSKristof Provost 	u_int8_t		 set_prio[2];
849e86bddeaSKristof Provost 
850e86bddeaSKristof Provost 	struct {
851e86bddeaSKristof Provost 		struct pf_addr		addr;
852e86bddeaSKristof Provost 		u_int16_t		port;
853e86bddeaSKristof Provost 	}			divert;
8541a3e98a5SMateusz Guzik 	u_int8_t		 md5sum[PF_MD5_DIGEST_LENGTH];
855bd7762c8SMateusz Guzik 	RB_ENTRY(pf_krule)	 entry_global;
85602cf67ccSMateusz Guzik 
85702cf67ccSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
85802cf67ccSMateusz Guzik 	LIST_ENTRY(pf_krule)	 allrulelist;
85902cf67ccSMateusz Guzik 	bool			 allrulelinked;
86002cf67ccSMateusz Guzik #endif
861e86bddeaSKristof Provost };
862e86bddeaSKristof Provost 
86339282ef3SKajetan Staszkiewicz struct pf_krule_item {
86439282ef3SKajetan Staszkiewicz 	SLIST_ENTRY(pf_krule_item)	 entry;
86539282ef3SKajetan Staszkiewicz 	struct pf_krule			*r;
86639282ef3SKajetan Staszkiewicz };
86739282ef3SKajetan Staszkiewicz 
86839282ef3SKajetan Staszkiewicz SLIST_HEAD(pf_krule_slist, pf_krule_item);
86939282ef3SKajetan Staszkiewicz 
87017ad7334SKristof Provost struct pf_ksrc_node {
87117ad7334SKristof Provost 	LIST_ENTRY(pf_ksrc_node) entry;
8723b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr;
8733b3a8eb9SGleb Smirnoff 	struct pf_addr	 raddr;
87439282ef3SKajetan Staszkiewicz 	struct pf_krule_slist	 match_rules;
875e86bddeaSKristof Provost 	union pf_krule_ptr rule;
876d10de21fSKajetan Staszkiewicz 	struct pfi_kkif	*rkif;
877fbbf270eSKristof Provost 	counter_u64_t	 bytes[2];
878fbbf270eSKristof Provost 	counter_u64_t	 packets[2];
8793b3a8eb9SGleb Smirnoff 	u_int32_t	 states;
8803b3a8eb9SGleb Smirnoff 	u_int32_t	 conn;
8813b3a8eb9SGleb Smirnoff 	struct pf_threshold	conn_rate;
8823b3a8eb9SGleb Smirnoff 	u_int32_t	 creation;
8833b3a8eb9SGleb Smirnoff 	u_int32_t	 expire;
8843b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
8853b3a8eb9SGleb Smirnoff 	u_int8_t	 ruletype;
886db0a2bfdSKajetan Staszkiewicz 	struct mtx	*lock;
8873b3a8eb9SGleb Smirnoff };
88817ad7334SKristof Provost #endif
8893b3a8eb9SGleb Smirnoff 
8903b3a8eb9SGleb Smirnoff struct pf_state_scrub {
8913b3a8eb9SGleb Smirnoff 	struct timeval	pfss_last;	/* time received last packet	*/
8923b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
8933b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
8943b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
8953b3a8eb9SGleb Smirnoff 	u_int16_t	pfss_flags;
8963b3a8eb9SGleb Smirnoff #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
8973b3a8eb9SGleb Smirnoff #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
8983b3a8eb9SGleb Smirnoff #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
8993b3a8eb9SGleb Smirnoff #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
9003b3a8eb9SGleb Smirnoff #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
9013b3a8eb9SGleb Smirnoff 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
9023b3a8eb9SGleb Smirnoff 	u_int8_t	pad;
90351a78dd2SKristof Provost 	union {
9043b3a8eb9SGleb Smirnoff 		u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
90551a78dd2SKristof Provost 		u_int32_t	pfss_v_tag;	/* SCTP verification tag	*/
90651a78dd2SKristof Provost 	};
9073b3a8eb9SGleb Smirnoff };
9083b3a8eb9SGleb Smirnoff 
9093b3a8eb9SGleb Smirnoff struct pf_state_host {
9103b3a8eb9SGleb Smirnoff 	struct pf_addr	addr;
9113b3a8eb9SGleb Smirnoff 	u_int16_t	port;
9123b3a8eb9SGleb Smirnoff 	u_int16_t	pad;
9133b3a8eb9SGleb Smirnoff };
9143b3a8eb9SGleb Smirnoff 
9153b3a8eb9SGleb Smirnoff struct pf_state_peer {
9163b3a8eb9SGleb Smirnoff 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
9173b3a8eb9SGleb Smirnoff 	u_int32_t	seqlo;		/* Max sequence number sent	*/
9183b3a8eb9SGleb Smirnoff 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
9193b3a8eb9SGleb Smirnoff 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
9203b3a8eb9SGleb Smirnoff 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
9213b3a8eb9SGleb Smirnoff 	u_int16_t	mss;		/* Maximum segment size option	*/
9223b3a8eb9SGleb Smirnoff 	u_int8_t	state;		/* active state level		*/
9233b3a8eb9SGleb Smirnoff 	u_int8_t	wscale;		/* window scaling factor	*/
9243b3a8eb9SGleb Smirnoff 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
9253b3a8eb9SGleb Smirnoff 	u_int8_t	pad[1];
9263b3a8eb9SGleb Smirnoff };
9273b3a8eb9SGleb Smirnoff 
9283b3a8eb9SGleb Smirnoff /* Keep synced with struct pf_state_key. */
9293b3a8eb9SGleb Smirnoff struct pf_state_key_cmp {
9303b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
9313b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
9323b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
9333b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
9343b3a8eb9SGleb Smirnoff 	u_int8_t	 pad[2];
9353b3a8eb9SGleb Smirnoff };
9363b3a8eb9SGleb Smirnoff 
9373b3a8eb9SGleb Smirnoff struct pf_state_key {
9383b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
9393b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
9403b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
9413b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
9423b3a8eb9SGleb Smirnoff 	u_int8_t	 pad[2];
9433b3a8eb9SGleb Smirnoff 
9443b3a8eb9SGleb Smirnoff 	LIST_ENTRY(pf_state_key) entry;
945211cddf9SKristof Provost 	TAILQ_HEAD(, pf_kstate)	 states[2];
9463b3a8eb9SGleb Smirnoff };
9473b3a8eb9SGleb Smirnoff 
948211cddf9SKristof Provost /* Keep synced with struct pf_kstate. */
9493b3a8eb9SGleb Smirnoff struct pf_state_cmp {
9503b3a8eb9SGleb Smirnoff 	u_int64_t		 id;
9513b3a8eb9SGleb Smirnoff 	u_int32_t		 creatorid;
9523b3a8eb9SGleb Smirnoff 	u_int8_t		 direction;
9533b3a8eb9SGleb Smirnoff 	u_int8_t		 pad[3];
9543b3a8eb9SGleb Smirnoff };
9553b3a8eb9SGleb Smirnoff 
956c6bf20a2SKristof Provost struct pf_state_scrub_export {
957c6bf20a2SKristof Provost 	uint16_t	pfss_flags;
958c6bf20a2SKristof Provost 	uint8_t		pfss_ttl;	/* stashed TTL		*/
959c6bf20a2SKristof Provost #define PF_SCRUB_FLAG_VALID		0x01
960c6bf20a2SKristof Provost 	uint8_t		scrub_flag;
961c6bf20a2SKristof Provost 	uint32_t	pfss_ts_mod;	/* timestamp modulation	*/
962c6bf20a2SKristof Provost };
963c6bf20a2SKristof Provost 
964c6bf20a2SKristof Provost struct pf_state_key_export {
965c6bf20a2SKristof Provost 	struct pf_addr	 addr[2];
966c6bf20a2SKristof Provost 	uint16_t	 port[2];
967c6bf20a2SKristof Provost };
968c6bf20a2SKristof Provost 
969c6bf20a2SKristof Provost struct pf_state_peer_export {
970c6bf20a2SKristof Provost 	struct pf_state_scrub_export	scrub;	/* state is scrubbed	*/
971c6bf20a2SKristof Provost 	uint32_t	seqlo;		/* Max sequence number sent	*/
972c6bf20a2SKristof Provost 	uint32_t	seqhi;		/* Max the other end ACKd + win	*/
973c6bf20a2SKristof Provost 	uint32_t	seqdiff;	/* Sequence number modulator	*/
974c6bf20a2SKristof Provost 	uint16_t	max_win;	/* largest window (pre scaling)	*/
975c6bf20a2SKristof Provost 	uint16_t	mss;		/* Maximum segment size option	*/
976c6bf20a2SKristof Provost 	uint8_t		state;		/* active state level		*/
977c6bf20a2SKristof Provost 	uint8_t		wscale;		/* window scaling factor	*/
978c6bf20a2SKristof Provost 	uint8_t		dummy[6];
979c6bf20a2SKristof Provost };
980c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect");
981c6bf20a2SKristof Provost 
982c6bf20a2SKristof Provost struct pf_state_export {
983c6bf20a2SKristof Provost 	uint64_t	 version;
984c45d6b0eSKajetan Staszkiewicz #define	PF_STATE_VERSION	20230404
985c6bf20a2SKristof Provost 	uint64_t	 id;
986c6bf20a2SKristof Provost 	char		 ifname[IFNAMSIZ];
987c6bf20a2SKristof Provost 	char		 orig_ifname[IFNAMSIZ];
988c6bf20a2SKristof Provost 	struct pf_state_key_export	 key[2];
989c6bf20a2SKristof Provost 	struct pf_state_peer_export	 src;
990c6bf20a2SKristof Provost 	struct pf_state_peer_export	 dst;
991c6bf20a2SKristof Provost 	struct pf_addr	 rt_addr;
992c6bf20a2SKristof Provost 	uint32_t	 rule;
993c6bf20a2SKristof Provost 	uint32_t	 anchor;
994c6bf20a2SKristof Provost 	uint32_t	 nat_rule;
995c6bf20a2SKristof Provost 	uint32_t	 creation;
996c6bf20a2SKristof Provost 	uint32_t	 expire;
997c6bf20a2SKristof Provost 	uint32_t	 spare0;
998c6bf20a2SKristof Provost 	uint64_t	 packets[2];
999c6bf20a2SKristof Provost 	uint64_t	 bytes[2];
1000c6bf20a2SKristof Provost 	uint32_t	 creatorid;
1001c6bf20a2SKristof Provost 	uint32_t	 spare1;
1002c6bf20a2SKristof Provost 	sa_family_t	 af;
1003c6bf20a2SKristof Provost 	uint8_t		 proto;
1004c6bf20a2SKristof Provost 	uint8_t		 direction;
1005c6bf20a2SKristof Provost 	uint8_t		 log;
100639282ef3SKajetan Staszkiewicz 	uint8_t		 state_flags_compat;
1007c6bf20a2SKristof Provost 	uint8_t		 timeout;
1008c6bf20a2SKristof Provost 	uint8_t		 sync_flags;
1009c6bf20a2SKristof Provost 	uint8_t		 updates;
101039282ef3SKajetan Staszkiewicz 	uint16_t	 state_flags;
1011c45d6b0eSKajetan Staszkiewicz 	uint16_t	 qid;
1012c45d6b0eSKajetan Staszkiewicz 	uint16_t	 pqid;
1013c45d6b0eSKajetan Staszkiewicz 	uint16_t	 dnpipe;
1014c45d6b0eSKajetan Staszkiewicz 	uint16_t	 dnrpipe;
1015c45d6b0eSKajetan Staszkiewicz 	int32_t		 rtableid;
1016c45d6b0eSKajetan Staszkiewicz 	uint8_t		 min_ttl;
1017c45d6b0eSKajetan Staszkiewicz 	uint8_t		 set_tos;
1018c45d6b0eSKajetan Staszkiewicz 	uint16_t	 max_mss;
1019c45d6b0eSKajetan Staszkiewicz 	uint8_t		 set_prio[2];
1020c45d6b0eSKajetan Staszkiewicz 	uint8_t		 rt;
1021c45d6b0eSKajetan Staszkiewicz 	char		 rt_ifname[IFNAMSIZ];
1022c6bf20a2SKristof Provost 
1023c45d6b0eSKajetan Staszkiewicz 	uint8_t		 spare[72];
1024c6bf20a2SKristof Provost };
1025c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect");
1026c6bf20a2SKristof Provost 
1027e86bddeaSKristof Provost #ifdef _KERNEL
1028211cddf9SKristof Provost struct pf_kstate {
1029f9aa757dSMateusz Guzik 	/*
1030f9aa757dSMateusz Guzik 	 * Area shared with pf_state_cmp
1031f9aa757dSMateusz Guzik 	 */
10323b3a8eb9SGleb Smirnoff 	u_int64_t		 id;
10333b3a8eb9SGleb Smirnoff 	u_int32_t		 creatorid;
10343b3a8eb9SGleb Smirnoff 	u_int8_t		 direction;
10353b3a8eb9SGleb Smirnoff 	u_int8_t		 pad[3];
1036f9aa757dSMateusz Guzik 	/*
1037f9aa757dSMateusz Guzik 	 * end of the area
1038f9aa757dSMateusz Guzik 	 */
10393b3a8eb9SGleb Smirnoff 
104039282ef3SKajetan Staszkiewicz 	u_int16_t		 state_flags;
10419009d36aSMateusz Guzik 	u_int8_t		 timeout;
10429009d36aSMateusz Guzik 	u_int8_t		 sync_state; /* PFSYNC_S_x */
10439009d36aSMateusz Guzik 	u_int8_t		 sync_updates; /* XXX */
10443b3a8eb9SGleb Smirnoff 	u_int			 refs;
1045907257d6SMateusz Guzik 	struct mtx		*lock;
1046211cddf9SKristof Provost 	TAILQ_ENTRY(pf_kstate)	 sync_list;
1047211cddf9SKristof Provost 	TAILQ_ENTRY(pf_kstate)	 key_list[2];
1048211cddf9SKristof Provost 	LIST_ENTRY(pf_kstate)	 entry;
10493b3a8eb9SGleb Smirnoff 	struct pf_state_peer	 src;
10503b3a8eb9SGleb Smirnoff 	struct pf_state_peer	 dst;
105139282ef3SKajetan Staszkiewicz 	struct pf_krule_slist	 match_rules;
1052e86bddeaSKristof Provost 	union pf_krule_ptr	 rule;
1053e86bddeaSKristof Provost 	union pf_krule_ptr	 anchor;
1054e86bddeaSKristof Provost 	union pf_krule_ptr	 nat_rule;
10553b3a8eb9SGleb Smirnoff 	struct pf_addr		 rt_addr;
10563b3a8eb9SGleb Smirnoff 	struct pf_state_key	*key[2];	/* addresses stack and wire  */
1057320c1116SKristof Provost 	struct pfi_kkif		*kif;
1058d0fdf2b2SKristof Provost 	struct pfi_kkif		*orig_kif;	/* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */
1059320c1116SKristof Provost 	struct pfi_kkif		*rt_kif;
106017ad7334SKristof Provost 	struct pf_ksrc_node	*src_node;
106117ad7334SKristof Provost 	struct pf_ksrc_node	*nat_src_node;
106255cc305dSMateusz Guzik 	u_int64_t		 packets[2];
106355cc305dSMateusz Guzik 	u_int64_t		 bytes[2];
106404932601SKristof Provost 	u_int64_t		 creation;
106504932601SKristof Provost 	u_int64_t	 	 expire;
10663b3a8eb9SGleb Smirnoff 	u_int32_t		 pfsync_time;
10676b4ed16dSKajetan Staszkiewicz 	struct pf_rule_actions	 act;
10683b3a8eb9SGleb Smirnoff 	u_int16_t		 tag;
10694bf98559SKajetan Staszkiewicz 	u_int8_t		 rt;
1070f92d9b1aSKristof Provost 	u_int16_t		 if_index_in;
1071f92d9b1aSKristof Provost 	u_int16_t		 if_index_out;
10723b3a8eb9SGleb Smirnoff };
1073e6dd0e2eSMateusz Guzik 
1074e6dd0e2eSMateusz Guzik /*
10754bf98559SKajetan Staszkiewicz  * Size <= fits 11 objects per page on LP64. Try to not grow the struct beyond that.
1076e6dd0e2eSMateusz Guzik  */
1077f92d9b1aSKristof Provost _Static_assert(sizeof(struct pf_kstate) <= 372, "pf_kstate size crosses 372 bytes");
1078e86bddeaSKristof Provost #endif
10793b3a8eb9SGleb Smirnoff 
10803b3a8eb9SGleb Smirnoff /*
10813b3a8eb9SGleb Smirnoff  * Unified state structures for pulling states out of the kernel
10823b3a8eb9SGleb Smirnoff  * used by pfsync(4) and the pf(4) ioctl.
10833b3a8eb9SGleb Smirnoff  */
10843b3a8eb9SGleb Smirnoff struct pfsync_state_scrub {
10853b3a8eb9SGleb Smirnoff 	u_int16_t	pfss_flags;
10863b3a8eb9SGleb Smirnoff 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
10873b3a8eb9SGleb Smirnoff #define PFSYNC_SCRUB_FLAG_VALID		0x01
10883b3a8eb9SGleb Smirnoff 	u_int8_t	scrub_flag;
10893b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
10903b3a8eb9SGleb Smirnoff } __packed;
10913b3a8eb9SGleb Smirnoff 
10923b3a8eb9SGleb Smirnoff struct pfsync_state_peer {
10933b3a8eb9SGleb Smirnoff 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
10943b3a8eb9SGleb Smirnoff 	u_int32_t	seqlo;		/* Max sequence number sent	*/
10953b3a8eb9SGleb Smirnoff 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
10963b3a8eb9SGleb Smirnoff 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
10973b3a8eb9SGleb Smirnoff 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
10983b3a8eb9SGleb Smirnoff 	u_int16_t	mss;		/* Maximum segment size option	*/
10993b3a8eb9SGleb Smirnoff 	u_int8_t	state;		/* active state level		*/
11003b3a8eb9SGleb Smirnoff 	u_int8_t	wscale;		/* window scaling factor	*/
11013b3a8eb9SGleb Smirnoff 	u_int8_t	pad[6];
11023b3a8eb9SGleb Smirnoff } __packed;
11033b3a8eb9SGleb Smirnoff 
11043b3a8eb9SGleb Smirnoff struct pfsync_state_key {
11053b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
11063b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
11073b3a8eb9SGleb Smirnoff };
11083b3a8eb9SGleb Smirnoff 
11094bf98559SKajetan Staszkiewicz struct pfsync_state_1301 {
11104bf98559SKajetan Staszkiewicz 	u_int64_t	 id;
11114bf98559SKajetan Staszkiewicz 	char		 ifname[IFNAMSIZ];
11124bf98559SKajetan Staszkiewicz 	struct pfsync_state_key	key[2];
11134bf98559SKajetan Staszkiewicz 	struct pfsync_state_peer src;
11144bf98559SKajetan Staszkiewicz 	struct pfsync_state_peer dst;
11154bf98559SKajetan Staszkiewicz 	struct pf_addr	 rt_addr;
11164bf98559SKajetan Staszkiewicz 	u_int32_t	 rule;
11174bf98559SKajetan Staszkiewicz 	u_int32_t	 anchor;
11184bf98559SKajetan Staszkiewicz 	u_int32_t	 nat_rule;
11194bf98559SKajetan Staszkiewicz 	u_int32_t	 creation;
11204bf98559SKajetan Staszkiewicz 	u_int32_t	 expire;
11214bf98559SKajetan Staszkiewicz 	u_int32_t	 packets[2][2];
11224bf98559SKajetan Staszkiewicz 	u_int32_t	 bytes[2][2];
11234bf98559SKajetan Staszkiewicz 	u_int32_t	 creatorid;
11244bf98559SKajetan Staszkiewicz 	sa_family_t	 af;
11254bf98559SKajetan Staszkiewicz 	u_int8_t	 proto;
11264bf98559SKajetan Staszkiewicz 	u_int8_t	 direction;
11274bf98559SKajetan Staszkiewicz 	u_int8_t	 __spare[2];
11284bf98559SKajetan Staszkiewicz 	u_int8_t	 log;
11294bf98559SKajetan Staszkiewicz 	u_int8_t	 state_flags;
11304bf98559SKajetan Staszkiewicz 	u_int8_t	 timeout;
11314bf98559SKajetan Staszkiewicz 	u_int8_t	 sync_flags;
11324bf98559SKajetan Staszkiewicz 	u_int8_t	 updates;
11334bf98559SKajetan Staszkiewicz } __packed;
11344bf98559SKajetan Staszkiewicz 
11354bf98559SKajetan Staszkiewicz struct pfsync_state_1400 {
11364bf98559SKajetan Staszkiewicz 	/* The beginning of the struct is compatible with previous versions */
11373b3a8eb9SGleb Smirnoff 	u_int64_t	 id;
11383b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
11393b3a8eb9SGleb Smirnoff 	struct pfsync_state_key	key[2];
11403b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer src;
11413b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer dst;
11423b3a8eb9SGleb Smirnoff 	struct pf_addr	 rt_addr;
11433b3a8eb9SGleb Smirnoff 	u_int32_t	 rule;
11443b3a8eb9SGleb Smirnoff 	u_int32_t	 anchor;
11453b3a8eb9SGleb Smirnoff 	u_int32_t	 nat_rule;
11463b3a8eb9SGleb Smirnoff 	u_int32_t	 creation;
11473b3a8eb9SGleb Smirnoff 	u_int32_t	 expire;
11483b3a8eb9SGleb Smirnoff 	u_int32_t	 packets[2][2];
11493b3a8eb9SGleb Smirnoff 	u_int32_t	 bytes[2][2];
11503b3a8eb9SGleb Smirnoff 	u_int32_t	 creatorid;
11513b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
11523b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
11533b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
115439282ef3SKajetan Staszkiewicz 	u_int16_t	 state_flags;
11553b3a8eb9SGleb Smirnoff 	u_int8_t	 log;
11564bf98559SKajetan Staszkiewicz 	u_int8_t	 __spare;
11573b3a8eb9SGleb Smirnoff 	u_int8_t	 timeout;
11583b3a8eb9SGleb Smirnoff 	u_int8_t	 sync_flags;
11593b3a8eb9SGleb Smirnoff 	u_int8_t	 updates;
11604bf98559SKajetan Staszkiewicz 	/* The rest is not */
11614bf98559SKajetan Staszkiewicz 	u_int16_t	 qid;
11624bf98559SKajetan Staszkiewicz 	u_int16_t	 pqid;
11634bf98559SKajetan Staszkiewicz 	u_int16_t	 dnpipe;
11644bf98559SKajetan Staszkiewicz 	u_int16_t	 dnrpipe;
11654bf98559SKajetan Staszkiewicz 	int32_t		 rtableid;
11664bf98559SKajetan Staszkiewicz 	u_int8_t	 min_ttl;
11674bf98559SKajetan Staszkiewicz 	u_int8_t	 set_tos;
11684bf98559SKajetan Staszkiewicz 	u_int16_t	 max_mss;
11694bf98559SKajetan Staszkiewicz 	u_int8_t	 set_prio[2];
11704bf98559SKajetan Staszkiewicz 	u_int8_t	 rt;
11714bf98559SKajetan Staszkiewicz 	char		 rt_ifname[IFNAMSIZ];
11724bf98559SKajetan Staszkiewicz 
11734bf98559SKajetan Staszkiewicz } __packed;
11744bf98559SKajetan Staszkiewicz 
11754bf98559SKajetan Staszkiewicz union pfsync_state_union {
11764bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301 pfs_1301;
11774bf98559SKajetan Staszkiewicz 	struct pfsync_state_1400 pfs_1400;
11783b3a8eb9SGleb Smirnoff } __packed;
11793b3a8eb9SGleb Smirnoff 
11803b3a8eb9SGleb Smirnoff #ifdef _KERNEL
11813b3a8eb9SGleb Smirnoff /* pfsync */
11824bf98559SKajetan Staszkiewicz typedef int		pfsync_state_import_t(union pfsync_state_union *, int, int);
1183211cddf9SKristof Provost typedef	void		pfsync_insert_state_t(struct pf_kstate *);
1184211cddf9SKristof Provost typedef	void		pfsync_update_state_t(struct pf_kstate *);
1185211cddf9SKristof Provost typedef	void		pfsync_delete_state_t(struct pf_kstate *);
11863b3a8eb9SGleb Smirnoff typedef void		pfsync_clear_states_t(u_int32_t, const char *);
1187211cddf9SKristof Provost typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *);
1188fbbf436dSKristof Provost typedef void		pfsync_detach_ifnet_t(struct ifnet *);
1189baf9b6d0SKristof Provost typedef void		pflow_export_state_t(const struct pf_kstate *);
11903b3a8eb9SGleb Smirnoff 
11915f6cf24eSKristof Provost VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
11925f6cf24eSKristof Provost #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
11935f6cf24eSKristof Provost VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
11945f6cf24eSKristof Provost #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
11955f6cf24eSKristof Provost VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
11965f6cf24eSKristof Provost #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
11975f6cf24eSKristof Provost VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
11985f6cf24eSKristof Provost #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
11995f6cf24eSKristof Provost VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
12005f6cf24eSKristof Provost #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
12015f6cf24eSKristof Provost VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
12025f6cf24eSKristof Provost #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
1203baf9b6d0SKristof Provost VNET_DECLARE(pflow_export_state_t *,	pflow_export_state_ptr);
1204baf9b6d0SKristof Provost #define V_pflow_export_state_ptr	VNET(pflow_export_state_ptr)
1205fbbf436dSKristof Provost extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
12063b3a8eb9SGleb Smirnoff 
12074bf98559SKajetan Staszkiewicz void			pfsync_state_export(union pfsync_state_union *,
12084bf98559SKajetan Staszkiewicz 			    struct pf_kstate *, int);
1209c6bf20a2SKristof Provost void			pf_state_export(struct pf_state_export *,
1210c6bf20a2SKristof Provost 			    struct pf_kstate *);
12113b3a8eb9SGleb Smirnoff 
12123b3a8eb9SGleb Smirnoff /* pflog */
1213e86bddeaSKristof Provost struct pf_kruleset;
12143b3a8eb9SGleb Smirnoff struct pf_pdesc;
1215320c1116SKristof Provost typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
1216948e8413SKristof Provost     uint8_t, u_int8_t, struct pf_krule *, struct pf_krule *, struct pf_kruleset *,
1217f2064dd1SKajetan Staszkiewicz     struct pf_pdesc *, int);
12183b3a8eb9SGleb Smirnoff extern pflog_packet_t		*pflog_packet_ptr;
12193b3a8eb9SGleb Smirnoff 
12203b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
12213b3a8eb9SGleb Smirnoff 
12223b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_SRCNODE	0x04
12233b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_NATSRCNODE	0x08
12243b3a8eb9SGleb Smirnoff 
12253b3a8eb9SGleb Smirnoff /* for copies to/from network byte order */
12263b3a8eb9SGleb Smirnoff /* ioctl interface also uses network byte order */
12273b3a8eb9SGleb Smirnoff #define pf_state_peer_hton(s,d) do {		\
12283b3a8eb9SGleb Smirnoff 	(d)->seqlo = htonl((s)->seqlo);		\
12293b3a8eb9SGleb Smirnoff 	(d)->seqhi = htonl((s)->seqhi);		\
12303b3a8eb9SGleb Smirnoff 	(d)->seqdiff = htonl((s)->seqdiff);	\
12313b3a8eb9SGleb Smirnoff 	(d)->max_win = htons((s)->max_win);	\
12323b3a8eb9SGleb Smirnoff 	(d)->mss = htons((s)->mss);		\
12333b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
12343b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
12353b3a8eb9SGleb Smirnoff 	if ((s)->scrub) {						\
12363b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_flags = 				\
12373b3a8eb9SGleb Smirnoff 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
12383b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
12393b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
12403b3a8eb9SGleb Smirnoff 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
12413b3a8eb9SGleb Smirnoff 	}								\
12423b3a8eb9SGleb Smirnoff } while (0)
12433b3a8eb9SGleb Smirnoff 
12443b3a8eb9SGleb Smirnoff #define pf_state_peer_ntoh(s,d) do {		\
12453b3a8eb9SGleb Smirnoff 	(d)->seqlo = ntohl((s)->seqlo);		\
12463b3a8eb9SGleb Smirnoff 	(d)->seqhi = ntohl((s)->seqhi);		\
12473b3a8eb9SGleb Smirnoff 	(d)->seqdiff = ntohl((s)->seqdiff);	\
12483b3a8eb9SGleb Smirnoff 	(d)->max_win = ntohs((s)->max_win);	\
12493b3a8eb9SGleb Smirnoff 	(d)->mss = ntohs((s)->mss);		\
12503b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
12513b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
12523b3a8eb9SGleb Smirnoff 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
12533b3a8eb9SGleb Smirnoff 	    (d)->scrub != NULL) {					\
12543b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_flags =				\
12553b3a8eb9SGleb Smirnoff 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
12563b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
12573b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
12583b3a8eb9SGleb Smirnoff 	}								\
12593b3a8eb9SGleb Smirnoff } while (0)
12603b3a8eb9SGleb Smirnoff 
12613b3a8eb9SGleb Smirnoff #define pf_state_counter_hton(s,d) do {				\
12623b3a8eb9SGleb Smirnoff 	d[0] = htonl((s>>32)&0xffffffff);			\
12633b3a8eb9SGleb Smirnoff 	d[1] = htonl(s&0xffffffff);				\
12643b3a8eb9SGleb Smirnoff } while (0)
12653b3a8eb9SGleb Smirnoff 
12663b3a8eb9SGleb Smirnoff #define pf_state_counter_from_pfsync(s)				\
12673b3a8eb9SGleb Smirnoff 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
12683b3a8eb9SGleb Smirnoff 
12693b3a8eb9SGleb Smirnoff #define pf_state_counter_ntoh(s,d) do {				\
12703b3a8eb9SGleb Smirnoff 	d = ntohl(s[0]);					\
12713b3a8eb9SGleb Smirnoff 	d = d<<32;						\
12723b3a8eb9SGleb Smirnoff 	d += ntohl(s[1]);					\
12733b3a8eb9SGleb Smirnoff } while (0)
12743b3a8eb9SGleb Smirnoff 
1275e86bddeaSKristof Provost TAILQ_HEAD(pf_krulequeue, pf_krule);
12763b3a8eb9SGleb Smirnoff 
1277e86bddeaSKristof Provost struct pf_kanchor;
12783b3a8eb9SGleb Smirnoff 
1279e86bddeaSKristof Provost struct pf_kruleset {
12803b3a8eb9SGleb Smirnoff 	struct {
1281e86bddeaSKristof Provost 		struct pf_krulequeue	 queues[2];
12823b3a8eb9SGleb Smirnoff 		struct {
1283e86bddeaSKristof Provost 			struct pf_krulequeue	*ptr;
1284e86bddeaSKristof Provost 			struct pf_krule		**ptr_array;
12853b3a8eb9SGleb Smirnoff 			u_int32_t		 rcount;
12863b3a8eb9SGleb Smirnoff 			u_int32_t		 ticket;
12873b3a8eb9SGleb Smirnoff 			int			 open;
1288bd7762c8SMateusz Guzik 			struct pf_krule_global 	 *tree;
12893b3a8eb9SGleb Smirnoff 		}			 active, inactive;
12903b3a8eb9SGleb Smirnoff 	}			 rules[PF_RULESET_MAX];
1291e86bddeaSKristof Provost 	struct pf_kanchor	*anchor;
12923b3a8eb9SGleb Smirnoff 	u_int32_t		 tticket;
12933b3a8eb9SGleb Smirnoff 	int			 tables;
12943b3a8eb9SGleb Smirnoff 	int			 topen;
12953b3a8eb9SGleb Smirnoff };
12963b3a8eb9SGleb Smirnoff 
1297e86bddeaSKristof Provost RB_HEAD(pf_kanchor_global, pf_kanchor);
1298e86bddeaSKristof Provost RB_HEAD(pf_kanchor_node, pf_kanchor);
1299e86bddeaSKristof Provost struct pf_kanchor {
1300e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_global;
1301e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_node;
1302e86bddeaSKristof Provost 	struct pf_kanchor	*parent;
1303e86bddeaSKristof Provost 	struct pf_kanchor_node	 children;
13043b3a8eb9SGleb Smirnoff 	char			 name[PF_ANCHOR_NAME_SIZE];
13053b3a8eb9SGleb Smirnoff 	char			 path[MAXPATHLEN];
1306e86bddeaSKristof Provost 	struct pf_kruleset	 ruleset;
13073b3a8eb9SGleb Smirnoff 	int			 refcnt;	/* anchor rules */
13083b3a8eb9SGleb Smirnoff };
1309e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
1310e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
13113b3a8eb9SGleb Smirnoff 
13123b3a8eb9SGleb Smirnoff #define PF_RESERVED_ANCHOR	"_pf"
13133b3a8eb9SGleb Smirnoff 
13143b3a8eb9SGleb Smirnoff #define PFR_TFLAG_PERSIST	0x00000001
13153b3a8eb9SGleb Smirnoff #define PFR_TFLAG_CONST		0x00000002
13163b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ACTIVE	0x00000004
13173b3a8eb9SGleb Smirnoff #define PFR_TFLAG_INACTIVE	0x00000008
13183b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFERENCED	0x00000010
13193b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFDANCHOR	0x00000020
13203b3a8eb9SGleb Smirnoff #define PFR_TFLAG_COUNTERS	0x00000040
13213b3a8eb9SGleb Smirnoff /* Adjust masks below when adding flags. */
13223b3a8eb9SGleb Smirnoff #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
13233b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
13243b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
13253b3a8eb9SGleb Smirnoff #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
13263b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
13273b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
13283b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR)
13293b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
13303b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
13313b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_ACTIVE	| \
13323b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
13333b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
13343b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR	| \
13353b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
13363b3a8eb9SGleb Smirnoff 
1337e86bddeaSKristof Provost struct pf_kanchor_stackframe;
1338c5131afeSKristof Provost struct pf_keth_anchor_stackframe;
13391d6139c0SGleb Smirnoff 
13403b3a8eb9SGleb Smirnoff struct pfr_table {
13413b3a8eb9SGleb Smirnoff 	char			 pfrt_anchor[MAXPATHLEN];
13423b3a8eb9SGleb Smirnoff 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
13433b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrt_flags;
13443b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrt_fback;
13453b3a8eb9SGleb Smirnoff };
13463b3a8eb9SGleb Smirnoff 
13473b3a8eb9SGleb Smirnoff enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
13483b3a8eb9SGleb Smirnoff 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
13493b3a8eb9SGleb Smirnoff 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
13503b3a8eb9SGleb Smirnoff 
13513b3a8eb9SGleb Smirnoff struct pfr_addr {
13523b3a8eb9SGleb Smirnoff 	union {
13533b3a8eb9SGleb Smirnoff 		struct in_addr	 _pfra_ip4addr;
13543b3a8eb9SGleb Smirnoff 		struct in6_addr	 _pfra_ip6addr;
13553b3a8eb9SGleb Smirnoff 	}		 pfra_u;
13563b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_af;
13573b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_net;
13583b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_not;
13593b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_fback;
13603b3a8eb9SGleb Smirnoff };
13613b3a8eb9SGleb Smirnoff #define	pfra_ip4addr	pfra_u._pfra_ip4addr
13623b3a8eb9SGleb Smirnoff #define	pfra_ip6addr	pfra_u._pfra_ip6addr
13633b3a8eb9SGleb Smirnoff 
13643b3a8eb9SGleb Smirnoff enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
13653b3a8eb9SGleb Smirnoff enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1366c1be8399SMark Johnston enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
1367c1be8399SMark Johnston #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
13683b3a8eb9SGleb Smirnoff #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
13693b3a8eb9SGleb Smirnoff 
13703b3a8eb9SGleb Smirnoff struct pfr_astats {
13713b3a8eb9SGleb Smirnoff 	struct pfr_addr	 pfras_a;
13723b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
13733b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
13743b3a8eb9SGleb Smirnoff 	long		 pfras_tzero;
13753b3a8eb9SGleb Smirnoff };
13763b3a8eb9SGleb Smirnoff 
13773b3a8eb9SGleb Smirnoff enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
13783b3a8eb9SGleb Smirnoff 
13793b3a8eb9SGleb Smirnoff struct pfr_tstats {
13803b3a8eb9SGleb Smirnoff 	struct pfr_table pfrts_t;
13813b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
13823b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
13833b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_match;
13843b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_nomatch;
13853b3a8eb9SGleb Smirnoff 	long		 pfrts_tzero;
13863b3a8eb9SGleb Smirnoff 	int		 pfrts_cnt;
13873b3a8eb9SGleb Smirnoff 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
13883b3a8eb9SGleb Smirnoff };
138959048686SKristof Provost 
1390f92c21a2SMateusz Guzik #ifdef _KERNEL
1391f92c21a2SMateusz Guzik 
1392f92c21a2SMateusz Guzik struct pfr_kstate_counter {
1393f92c21a2SMateusz Guzik 	counter_u64_t	pkc_pcpu;
1394f92c21a2SMateusz Guzik 	u_int64_t	pkc_zero;
1395f92c21a2SMateusz Guzik };
1396f92c21a2SMateusz Guzik 
1397f92c21a2SMateusz Guzik static inline int
pfr_kstate_counter_init(struct pfr_kstate_counter * pfrc,int flags)1398f92c21a2SMateusz Guzik pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
1399f92c21a2SMateusz Guzik {
1400f92c21a2SMateusz Guzik 
1401f92c21a2SMateusz Guzik 	pfrc->pkc_zero = 0;
1402f92c21a2SMateusz Guzik 	pfrc->pkc_pcpu = counter_u64_alloc(flags);
1403f92c21a2SMateusz Guzik 	if (pfrc->pkc_pcpu == NULL)
1404f92c21a2SMateusz Guzik 		return (ENOMEM);
1405f92c21a2SMateusz Guzik 	return (0);
1406f92c21a2SMateusz Guzik }
1407f92c21a2SMateusz Guzik 
1408f92c21a2SMateusz Guzik static inline void
pfr_kstate_counter_deinit(struct pfr_kstate_counter * pfrc)1409f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
1410f92c21a2SMateusz Guzik {
1411f92c21a2SMateusz Guzik 
1412f92c21a2SMateusz Guzik 	counter_u64_free(pfrc->pkc_pcpu);
1413f92c21a2SMateusz Guzik }
1414f92c21a2SMateusz Guzik 
1415f92c21a2SMateusz Guzik static inline u_int64_t
pfr_kstate_counter_fetch(struct pfr_kstate_counter * pfrc)1416f92c21a2SMateusz Guzik pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
1417f92c21a2SMateusz Guzik {
1418f92c21a2SMateusz Guzik 	u_int64_t c;
1419f92c21a2SMateusz Guzik 
1420f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1421f92c21a2SMateusz Guzik 	c -= pfrc->pkc_zero;
1422f92c21a2SMateusz Guzik 	return (c);
1423f92c21a2SMateusz Guzik }
1424f92c21a2SMateusz Guzik 
1425f92c21a2SMateusz Guzik static inline void
pfr_kstate_counter_zero(struct pfr_kstate_counter * pfrc)1426f92c21a2SMateusz Guzik pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
1427f92c21a2SMateusz Guzik {
1428f92c21a2SMateusz Guzik 	u_int64_t c;
1429f92c21a2SMateusz Guzik 
1430f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1431f92c21a2SMateusz Guzik 	pfrc->pkc_zero = c;
1432f92c21a2SMateusz Guzik }
1433f92c21a2SMateusz Guzik 
1434f92c21a2SMateusz Guzik static inline void
pfr_kstate_counter_add(struct pfr_kstate_counter * pfrc,int64_t n)1435f92c21a2SMateusz Guzik pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
1436f92c21a2SMateusz Guzik {
1437f92c21a2SMateusz Guzik 
1438f92c21a2SMateusz Guzik 	counter_u64_add(pfrc->pkc_pcpu, n);
1439f92c21a2SMateusz Guzik }
1440f92c21a2SMateusz Guzik 
144159048686SKristof Provost struct pfr_ktstats {
144259048686SKristof Provost 	struct pfr_table pfrts_t;
1443f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1444f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1445f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_match;
1446f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_nomatch;
144759048686SKristof Provost 	long		 pfrkts_tzero;
144859048686SKristof Provost 	int		 pfrkts_cnt;
144959048686SKristof Provost 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
145059048686SKristof Provost };
1451f92c21a2SMateusz Guzik 
1452f92c21a2SMateusz Guzik #endif /* _KERNEL */
1453f92c21a2SMateusz Guzik 
14543b3a8eb9SGleb Smirnoff #define	pfrts_name	pfrts_t.pfrt_name
14553b3a8eb9SGleb Smirnoff #define pfrts_flags	pfrts_t.pfrt_flags
14563b3a8eb9SGleb Smirnoff 
14573b3a8eb9SGleb Smirnoff #ifndef _SOCKADDR_UNION_DEFINED
14583b3a8eb9SGleb Smirnoff #define	_SOCKADDR_UNION_DEFINED
14593b3a8eb9SGleb Smirnoff union sockaddr_union {
14603b3a8eb9SGleb Smirnoff 	struct sockaddr		sa;
14613b3a8eb9SGleb Smirnoff 	struct sockaddr_in	sin;
14623b3a8eb9SGleb Smirnoff 	struct sockaddr_in6	sin6;
14633b3a8eb9SGleb Smirnoff };
14643b3a8eb9SGleb Smirnoff #endif /* _SOCKADDR_UNION_DEFINED */
14653b3a8eb9SGleb Smirnoff 
14663b3a8eb9SGleb Smirnoff struct pfr_kcounters {
1467c1be8399SMark Johnston 	counter_u64_t		 pfrkc_counters;
146859048686SKristof Provost 	long			 pfrkc_tzero;
14693b3a8eb9SGleb Smirnoff };
1470c1be8399SMark Johnston #define	pfr_kentry_counter(kc, dir, op, t)		\
1471c1be8399SMark Johnston 	((kc)->pfrkc_counters +				\
1472c1be8399SMark Johnston 	    (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
14733b3a8eb9SGleb Smirnoff 
1474e86bddeaSKristof Provost #ifdef _KERNEL
14753b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
14763b3a8eb9SGleb Smirnoff struct pfr_kentry {
14773b3a8eb9SGleb Smirnoff 	struct radix_node	 pfrke_node[2];
14783b3a8eb9SGleb Smirnoff 	union sockaddr_union	 pfrke_sa;
14793b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
148059048686SKristof Provost 	struct pfr_kcounters	 pfrke_counters;
14813b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_af;
14823b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_net;
14833b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_not;
14843b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_mark;
14853b3a8eb9SGleb Smirnoff };
14863b3a8eb9SGleb Smirnoff 
14873b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
14883b3a8eb9SGleb Smirnoff RB_HEAD(pfr_ktablehead, pfr_ktable);
14893b3a8eb9SGleb Smirnoff struct pfr_ktable {
149059048686SKristof Provost 	struct pfr_ktstats	 pfrkt_kts;
14913b3a8eb9SGleb Smirnoff 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
14923b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
14933b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip4;
14943b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip6;
14953b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_shadow;
14963b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_root;
1497e86bddeaSKristof Provost 	struct pf_kruleset	*pfrkt_rs;
14983b3a8eb9SGleb Smirnoff 	long			 pfrkt_larg;
14993b3a8eb9SGleb Smirnoff 	int			 pfrkt_nflags;
15003b3a8eb9SGleb Smirnoff };
150159048686SKristof Provost #define pfrkt_t		pfrkt_kts.pfrts_t
15023b3a8eb9SGleb Smirnoff #define pfrkt_name	pfrkt_t.pfrt_name
15033b3a8eb9SGleb Smirnoff #define pfrkt_anchor	pfrkt_t.pfrt_anchor
15043b3a8eb9SGleb Smirnoff #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
15053b3a8eb9SGleb Smirnoff #define pfrkt_flags	pfrkt_t.pfrt_flags
150659048686SKristof Provost #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
150759048686SKristof Provost #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
150859048686SKristof Provost #define pfrkt_packets	pfrkt_kts.pfrkts_packets
150959048686SKristof Provost #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
151059048686SKristof Provost #define pfrkt_match	pfrkt_kts.pfrkts_match
151159048686SKristof Provost #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
151259048686SKristof Provost #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1513e86bddeaSKristof Provost #endif
15143b3a8eb9SGleb Smirnoff 
1515320c1116SKristof Provost #ifdef _KERNEL
1516320c1116SKristof Provost struct pfi_kkif {
15173b3a8eb9SGleb Smirnoff 	char				 pfik_name[IFNAMSIZ];
15183b3a8eb9SGleb Smirnoff 	union {
1519320c1116SKristof Provost 		RB_ENTRY(pfi_kkif)	 _pfik_tree;
1520320c1116SKristof Provost 		LIST_ENTRY(pfi_kkif)	 _pfik_list;
15213b3a8eb9SGleb Smirnoff 	} _pfik_glue;
15223b3a8eb9SGleb Smirnoff #define	pfik_tree	_pfik_glue._pfik_tree
15233b3a8eb9SGleb Smirnoff #define	pfik_list	_pfik_glue._pfik_list
1524d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_packets[2][2][2];
1525d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_bytes[2][2][2];
15263b3a8eb9SGleb Smirnoff 	u_int32_t			 pfik_tzero;
15273b3a8eb9SGleb Smirnoff 	u_int				 pfik_flags;
15283b3a8eb9SGleb Smirnoff 	struct ifnet			*pfik_ifp;
15293b3a8eb9SGleb Smirnoff 	struct ifg_group		*pfik_group;
15303b3a8eb9SGleb Smirnoff 	u_int				 pfik_rulerefs;
15313b3a8eb9SGleb Smirnoff 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1532d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
1533d40d4b3eSMateusz Guzik 	LIST_ENTRY(pfi_kkif)		 pfik_allkiflist;
1534d40d4b3eSMateusz Guzik #endif
15353b3a8eb9SGleb Smirnoff };
1536320c1116SKristof Provost #endif
15373b3a8eb9SGleb Smirnoff 
15383b3a8eb9SGleb Smirnoff #define	PFI_IFLAG_REFS		0x0001	/* has state references */
15393b3a8eb9SGleb Smirnoff #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
15403b3a8eb9SGleb Smirnoff 
154129698ed9SKristof Provost #ifdef _KERNEL
154210aa9ddbSKristof Provost struct pf_sctp_multihome_job;
154310aa9ddbSKristof Provost TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job);
154410aa9ddbSKristof Provost 
15453b3a8eb9SGleb Smirnoff struct pf_pdesc {
15463b3a8eb9SGleb Smirnoff 	struct {
15473b3a8eb9SGleb Smirnoff 		int	 done;
15483b3a8eb9SGleb Smirnoff 		uid_t	 uid;
15493b3a8eb9SGleb Smirnoff 		gid_t	 gid;
15503b3a8eb9SGleb Smirnoff 	}		 lookup;
15513b3a8eb9SGleb Smirnoff 	u_int64_t	 tot_len;	/* Make Mickey money */
1552d38630f6SKristof Provost 	union pf_headers {
1553d38630f6SKristof Provost 		struct tcphdr		tcp;
1554d38630f6SKristof Provost 		struct udphdr		udp;
1555010ee43fSKristof Provost 		struct sctphdr		sctp;
1556d38630f6SKristof Provost 		struct icmp		icmp;
15573b3a8eb9SGleb Smirnoff #ifdef INET6
1558d38630f6SKristof Provost 		struct icmp6_hdr	icmp6;
15593b3a8eb9SGleb Smirnoff #endif /* INET6 */
1560d38630f6SKristof Provost 		char any[0];
15613b3a8eb9SGleb Smirnoff 	} hdr;
15623b3a8eb9SGleb Smirnoff 
1563e86bddeaSKristof Provost 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
15643b3a8eb9SGleb Smirnoff 	struct pf_addr	*src;		/* src address */
15653b3a8eb9SGleb Smirnoff 	struct pf_addr	*dst;		/* dst address */
15663b3a8eb9SGleb Smirnoff 	u_int16_t *sport;
15673b3a8eb9SGleb Smirnoff 	u_int16_t *dport;
15683b3a8eb9SGleb Smirnoff 	struct pf_mtag	*pf_mtag;
1569ef950daaSKristof Provost 	struct pf_rule_actions	act;
15703b3a8eb9SGleb Smirnoff 
15713b3a8eb9SGleb Smirnoff 	u_int32_t	 p_len;		/* total length of payload */
15723b3a8eb9SGleb Smirnoff 
15733b3a8eb9SGleb Smirnoff 	u_int16_t	*ip_sum;
15743b3a8eb9SGleb Smirnoff 	u_int16_t	*proto_sum;
15753b3a8eb9SGleb Smirnoff 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
15763b3a8eb9SGleb Smirnoff 					 * state code. Easier than tags */
15773b3a8eb9SGleb Smirnoff #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
15783b3a8eb9SGleb Smirnoff #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
15793b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
15803b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
15813b3a8eb9SGleb Smirnoff 	u_int8_t	 tos;
15823b3a8eb9SGleb Smirnoff 	u_int8_t	 dir;		/* direction */
15833b3a8eb9SGleb Smirnoff 	u_int8_t	 sidx;		/* key index for source */
15843b3a8eb9SGleb Smirnoff 	u_int8_t	 didx;		/* key index for destination */
1585010ee43fSKristof Provost #define PFDESC_SCTP_INIT	0x0001
1586010ee43fSKristof Provost #define PFDESC_SCTP_INIT_ACK	0x0002
1587010ee43fSKristof Provost #define PFDESC_SCTP_COOKIE	0x0004
15887093414cSKristof Provost #define PFDESC_SCTP_COOKIE_ACK	0x0008
15897093414cSKristof Provost #define PFDESC_SCTP_ABORT	0x0010
15907093414cSKristof Provost #define PFDESC_SCTP_SHUTDOWN	0x0020
15917093414cSKristof Provost #define PFDESC_SCTP_SHUTDOWN_COMPLETE	0x0040
15927093414cSKristof Provost #define PFDESC_SCTP_DATA	0x0080
15937093414cSKristof Provost #define PFDESC_SCTP_ASCONF	0x0100
15947093414cSKristof Provost #define PFDESC_SCTP_HEARTBEAT	0x0200
15957093414cSKristof Provost #define PFDESC_SCTP_HEARTBEAT_ACK	0x0400
15967093414cSKristof Provost #define PFDESC_SCTP_OTHER	0x0800
15977093414cSKristof Provost #define PFDESC_SCTP_ADD_IP	0x1000
1598010ee43fSKristof Provost 	u_int16_t	 sctp_flags;
1599d1bc1e9eSKristof Provost 	u_int32_t	 sctp_initiate_tag;
160010aa9ddbSKristof Provost 
160110aa9ddbSKristof Provost 	struct pf_sctp_multihome_jobs	sctp_multihome_jobs;
16023b3a8eb9SGleb Smirnoff };
160310aa9ddbSKristof Provost 
160410aa9ddbSKristof Provost struct pf_sctp_multihome_job {
160510aa9ddbSKristof Provost 	TAILQ_ENTRY(pf_sctp_multihome_job)	next;
160610aa9ddbSKristof Provost 	struct pf_pdesc				 pd;
160710aa9ddbSKristof Provost 	struct pf_addr				 src;
160810aa9ddbSKristof Provost 	struct pf_addr				 dst;
160910aa9ddbSKristof Provost 	struct mbuf				*m;
16104d3af82fSKristof Provost 	int					 op;
161110aa9ddbSKristof Provost };
161210aa9ddbSKristof Provost 
161329698ed9SKristof Provost #endif
16143b3a8eb9SGleb Smirnoff 
16153b3a8eb9SGleb Smirnoff /* flags for RDR options */
16163b3a8eb9SGleb Smirnoff #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
16173b3a8eb9SGleb Smirnoff #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
16183b3a8eb9SGleb Smirnoff 
16193b3a8eb9SGleb Smirnoff /* UDP state enumeration */
16203b3a8eb9SGleb Smirnoff #define PFUDPS_NO_TRAFFIC	0
16213b3a8eb9SGleb Smirnoff #define PFUDPS_SINGLE		1
16223b3a8eb9SGleb Smirnoff #define PFUDPS_MULTIPLE		2
16233b3a8eb9SGleb Smirnoff 
16243b3a8eb9SGleb Smirnoff #define PFUDPS_NSTATES		3	/* number of state levels */
16253b3a8eb9SGleb Smirnoff 
16263b3a8eb9SGleb Smirnoff #define PFUDPS_NAMES { \
16273b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
16283b3a8eb9SGleb Smirnoff 	"SINGLE", \
16293b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
16303b3a8eb9SGleb Smirnoff 	NULL \
16313b3a8eb9SGleb Smirnoff }
16323b3a8eb9SGleb Smirnoff 
16333b3a8eb9SGleb Smirnoff /* Other protocol state enumeration */
16343b3a8eb9SGleb Smirnoff #define PFOTHERS_NO_TRAFFIC	0
16353b3a8eb9SGleb Smirnoff #define PFOTHERS_SINGLE		1
16363b3a8eb9SGleb Smirnoff #define PFOTHERS_MULTIPLE	2
16373b3a8eb9SGleb Smirnoff 
16383b3a8eb9SGleb Smirnoff #define PFOTHERS_NSTATES	3	/* number of state levels */
16393b3a8eb9SGleb Smirnoff 
16403b3a8eb9SGleb Smirnoff #define PFOTHERS_NAMES { \
16413b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
16423b3a8eb9SGleb Smirnoff 	"SINGLE", \
16433b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
16443b3a8eb9SGleb Smirnoff 	NULL \
16453b3a8eb9SGleb Smirnoff }
16463b3a8eb9SGleb Smirnoff 
16473b3a8eb9SGleb Smirnoff #define ACTION_SET(a, x) \
16483b3a8eb9SGleb Smirnoff 	do { \
16493b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
16503b3a8eb9SGleb Smirnoff 			*(a) = (x); \
16513b3a8eb9SGleb Smirnoff 	} while (0)
16523b3a8eb9SGleb Smirnoff 
16533b3a8eb9SGleb Smirnoff #define REASON_SET(a, x) \
16543b3a8eb9SGleb Smirnoff 	do { \
16553b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
16563b3a8eb9SGleb Smirnoff 			*(a) = (x); \
16573b3a8eb9SGleb Smirnoff 		if (x < PFRES_MAX) \
1658a9572d8fSGleb Smirnoff 			counter_u64_add(V_pf_status.counters[x], 1); \
16593b3a8eb9SGleb Smirnoff 	} while (0)
16603b3a8eb9SGleb Smirnoff 
16618e1864edSKristof Provost enum pf_syncookies_mode {
16628e1864edSKristof Provost 	PF_SYNCOOKIES_NEVER = 0,
16638e1864edSKristof Provost 	PF_SYNCOOKIES_ALWAYS = 1,
1664bf863718SKristof Provost 	PF_SYNCOOKIES_ADAPTIVE = 2,
1665bf863718SKristof Provost 	PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE
16668e1864edSKristof Provost };
16678e1864edSKristof Provost 
16685062afffSKristof Provost #define	PF_SYNCOOKIES_HIWATPCT	25
16695062afffSKristof Provost #define	PF_SYNCOOKIES_LOWATPCT	(PF_SYNCOOKIES_HIWATPCT / 2)
16705062afffSKristof Provost 
1671d9cc6ea2SMateusz Guzik #ifdef _KERNEL
1672a9572d8fSGleb Smirnoff struct pf_kstatus {
1673a9572d8fSGleb Smirnoff 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
16744cab80a8SKristof Provost 	counter_u64_t	lcounters[KLCNT_MAX]; /* limit counters */
1675fc4c42ceSMateusz Guzik 	struct pf_counter_u64	fcounters[FCNT_MAX]; /* state operation counters */
1676a9572d8fSGleb Smirnoff 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1677a9572d8fSGleb Smirnoff 	uint32_t	states;
1678a9572d8fSGleb Smirnoff 	uint32_t	src_nodes;
1679a9572d8fSGleb Smirnoff 	uint32_t	running;
1680a9572d8fSGleb Smirnoff 	uint32_t	since;
1681a9572d8fSGleb Smirnoff 	uint32_t	debug;
1682a9572d8fSGleb Smirnoff 	uint32_t	hostid;
16833b3a8eb9SGleb Smirnoff 	char		ifname[IFNAMSIZ];
1684a9572d8fSGleb Smirnoff 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
168542ec75f8SKristof Provost 	bool		keep_counters;
16868e1864edSKristof Provost 	enum pf_syncookies_mode	syncookies_mode;
16878e1864edSKristof Provost 	bool		syncookies_active;
1688bf863718SKristof Provost 	uint64_t	syncookies_inflight[2];
1689bf863718SKristof Provost 	uint32_t	states_halfopen;
169039282ef3SKajetan Staszkiewicz 	uint32_t	reass;
16913b3a8eb9SGleb Smirnoff };
1692d9cc6ea2SMateusz Guzik #endif
16933b3a8eb9SGleb Smirnoff 
16943b3a8eb9SGleb Smirnoff struct pf_divert {
16953b3a8eb9SGleb Smirnoff 	union {
16963b3a8eb9SGleb Smirnoff 		struct in_addr	ipv4;
16973b3a8eb9SGleb Smirnoff 		struct in6_addr	ipv6;
16983b3a8eb9SGleb Smirnoff 	}		addr;
16993b3a8eb9SGleb Smirnoff 	u_int16_t	port;
17003b3a8eb9SGleb Smirnoff };
17013b3a8eb9SGleb Smirnoff 
17023b3a8eb9SGleb Smirnoff #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
17033b3a8eb9SGleb Smirnoff #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
17043b3a8eb9SGleb Smirnoff 
17053b3a8eb9SGleb Smirnoff /*
1706fd2ea405SKristof Provost  * Limit the length of the fragment queue traversal.  Remember
1707fd2ea405SKristof Provost  * search entry points based on the fragment offset.
1708fd2ea405SKristof Provost  */
1709fd2ea405SKristof Provost #define PF_FRAG_ENTRY_POINTS		16
1710fd2ea405SKristof Provost 
1711fd2ea405SKristof Provost /*
1712790194cdSKristof Provost  * The number of entries in the fragment queue must be limited
1713ef88adc5SGordon Bergling  * to avoid DoS by linear searching.  Instead of a global limit,
1714790194cdSKristof Provost  * use a limit per entry point.  For large packets these sum up.
1715790194cdSKristof Provost  */
1716790194cdSKristof Provost #define PF_FRAG_ENTRY_LIMIT		64
1717790194cdSKristof Provost 
1718790194cdSKristof Provost /*
17193b3a8eb9SGleb Smirnoff  * ioctl parameter structures
17203b3a8eb9SGleb Smirnoff  */
17213b3a8eb9SGleb Smirnoff 
17223b3a8eb9SGleb Smirnoff struct pfioc_pooladdr {
17233b3a8eb9SGleb Smirnoff 	u_int32_t		 action;
17243b3a8eb9SGleb Smirnoff 	u_int32_t		 ticket;
17253b3a8eb9SGleb Smirnoff 	u_int32_t		 nr;
17263b3a8eb9SGleb Smirnoff 	u_int32_t		 r_num;
17273b3a8eb9SGleb Smirnoff 	u_int8_t		 r_action;
17283b3a8eb9SGleb Smirnoff 	u_int8_t		 r_last;
17293b3a8eb9SGleb Smirnoff 	u_int8_t		 af;
17303b3a8eb9SGleb Smirnoff 	char			 anchor[MAXPATHLEN];
17313b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	 addr;
17323b3a8eb9SGleb Smirnoff };
17333b3a8eb9SGleb Smirnoff 
17343b3a8eb9SGleb Smirnoff struct pfioc_rule {
17353b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
17363b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
17373b3a8eb9SGleb Smirnoff 	u_int32_t	 pool_ticket;
17383b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
17393b3a8eb9SGleb Smirnoff 	char		 anchor[MAXPATHLEN];
17403b3a8eb9SGleb Smirnoff 	char		 anchor_call[MAXPATHLEN];
17413b3a8eb9SGleb Smirnoff 	struct pf_rule	 rule;
17423b3a8eb9SGleb Smirnoff };
17433b3a8eb9SGleb Smirnoff 
17443b3a8eb9SGleb Smirnoff struct pfioc_natlook {
17453b3a8eb9SGleb Smirnoff 	struct pf_addr	 saddr;
17463b3a8eb9SGleb Smirnoff 	struct pf_addr	 daddr;
17473b3a8eb9SGleb Smirnoff 	struct pf_addr	 rsaddr;
17483b3a8eb9SGleb Smirnoff 	struct pf_addr	 rdaddr;
17493b3a8eb9SGleb Smirnoff 	u_int16_t	 sport;
17503b3a8eb9SGleb Smirnoff 	u_int16_t	 dport;
17513b3a8eb9SGleb Smirnoff 	u_int16_t	 rsport;
17523b3a8eb9SGleb Smirnoff 	u_int16_t	 rdport;
17533b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
17543b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
17553b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
17563b3a8eb9SGleb Smirnoff };
17573b3a8eb9SGleb Smirnoff 
17583b3a8eb9SGleb Smirnoff struct pfioc_state {
17594bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301	state;
17603b3a8eb9SGleb Smirnoff };
17613b3a8eb9SGleb Smirnoff 
17623b3a8eb9SGleb Smirnoff struct pfioc_src_node_kill {
17633b3a8eb9SGleb Smirnoff 	sa_family_t psnk_af;
17643b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_src;
17653b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_dst;
17663b3a8eb9SGleb Smirnoff 	u_int		    psnk_killed;
17673b3a8eb9SGleb Smirnoff };
17683b3a8eb9SGleb Smirnoff 
17697606a45dSKristof Provost #ifdef _KERNEL
17707606a45dSKristof Provost struct pf_kstate_kill {
17717606a45dSKristof Provost 	struct pf_state_cmp	psk_pfcmp;
17727606a45dSKristof Provost 	sa_family_t		psk_af;
17737606a45dSKristof Provost 	int			psk_proto;
17747606a45dSKristof Provost 	struct pf_rule_addr	psk_src;
17757606a45dSKristof Provost 	struct pf_rule_addr	psk_dst;
1776abbcba9cSKristof Provost 	struct pf_rule_addr	psk_rt_addr;
17777606a45dSKristof Provost 	char			psk_ifname[IFNAMSIZ];
17787606a45dSKristof Provost 	char			psk_label[PF_RULE_LABEL_SIZE];
17797606a45dSKristof Provost 	u_int			psk_killed;
178093abcf17SKristof Provost 	bool			psk_kill_match;
17814f337550SKristof Provost 	bool			psk_nat;
17827606a45dSKristof Provost };
17837606a45dSKristof Provost #endif
17847606a45dSKristof Provost 
17853b3a8eb9SGleb Smirnoff struct pfioc_state_kill {
17863b3a8eb9SGleb Smirnoff 	struct pf_state_cmp	psk_pfcmp;
17873b3a8eb9SGleb Smirnoff 	sa_family_t		psk_af;
17883b3a8eb9SGleb Smirnoff 	int			psk_proto;
17893b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_src;
17903b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_dst;
17913b3a8eb9SGleb Smirnoff 	char			psk_ifname[IFNAMSIZ];
17923b3a8eb9SGleb Smirnoff 	char			psk_label[PF_RULE_LABEL_SIZE];
17933b3a8eb9SGleb Smirnoff 	u_int			psk_killed;
17943b3a8eb9SGleb Smirnoff };
17953b3a8eb9SGleb Smirnoff 
17963b3a8eb9SGleb Smirnoff struct pfioc_states {
17973b3a8eb9SGleb Smirnoff 	int	ps_len;
17983b3a8eb9SGleb Smirnoff 	union {
1799c4a32455SKristof Provost 		void				*ps_buf;
18004bf98559SKajetan Staszkiewicz 		struct pfsync_state_1301	*ps_states;
18017b676698SKristof Provost 	};
18023b3a8eb9SGleb Smirnoff };
18033b3a8eb9SGleb Smirnoff 
1804c6bf20a2SKristof Provost struct pfioc_states_v2 {
1805c6bf20a2SKristof Provost 	int		ps_len;
1806c6bf20a2SKristof Provost 	uint64_t	ps_req_version;
1807c6bf20a2SKristof Provost 	union {
1808c4a32455SKristof Provost 		void			*ps_buf;
18097b676698SKristof Provost 		struct pf_state_export	*ps_states;
18107b676698SKristof Provost 	};
1811c6bf20a2SKristof Provost };
1812c6bf20a2SKristof Provost 
18133b3a8eb9SGleb Smirnoff struct pfioc_src_nodes {
18143b3a8eb9SGleb Smirnoff 	int	psn_len;
18153b3a8eb9SGleb Smirnoff 	union {
1816c4a32455SKristof Provost 		void		*psn_buf;
18177b676698SKristof Provost 		struct pf_src_node	*psn_src_nodes;
18187b676698SKristof Provost 	};
18193b3a8eb9SGleb Smirnoff };
18203b3a8eb9SGleb Smirnoff 
18213b3a8eb9SGleb Smirnoff struct pfioc_if {
18223b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
18233b3a8eb9SGleb Smirnoff };
18243b3a8eb9SGleb Smirnoff 
18253b3a8eb9SGleb Smirnoff struct pfioc_tm {
18263b3a8eb9SGleb Smirnoff 	int		 timeout;
18273b3a8eb9SGleb Smirnoff 	int		 seconds;
18283b3a8eb9SGleb Smirnoff };
18293b3a8eb9SGleb Smirnoff 
18303b3a8eb9SGleb Smirnoff struct pfioc_limit {
18313b3a8eb9SGleb Smirnoff 	int		 index;
18323b3a8eb9SGleb Smirnoff 	unsigned	 limit;
18333b3a8eb9SGleb Smirnoff };
18343b3a8eb9SGleb Smirnoff 
1835249cc75fSPatrick Kelsey struct pfioc_altq_v0 {
18363b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
18373b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
18383b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
1839249cc75fSPatrick Kelsey 	struct pf_altq_v0 altq;
18403b3a8eb9SGleb Smirnoff };
18413b3a8eb9SGleb Smirnoff 
1842249cc75fSPatrick Kelsey struct pfioc_altq_v1 {
1843249cc75fSPatrick Kelsey 	u_int32_t	 action;
1844249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1845249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1846249cc75fSPatrick Kelsey 	/*
1847249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1848249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1849249cc75fSPatrick Kelsey 	 */
1850249cc75fSPatrick Kelsey 	u_int32_t	 version;
1851249cc75fSPatrick Kelsey 	struct pf_altq_v1 altq;
1852249cc75fSPatrick Kelsey };
1853249cc75fSPatrick Kelsey 
1854249cc75fSPatrick Kelsey /*
1855249cc75fSPatrick Kelsey  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1856249cc75fSPatrick Kelsey  * the latest version of struct pf_altq_vX as it has that struct as a
1857249cc75fSPatrick Kelsey  * member.
1858249cc75fSPatrick Kelsey  */
1859249cc75fSPatrick Kelsey #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1860249cc75fSPatrick Kelsey 
1861249cc75fSPatrick Kelsey struct pfioc_qstats_v0 {
18623b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
18633b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
18643b3a8eb9SGleb Smirnoff 	void		*buf;
18653b3a8eb9SGleb Smirnoff 	int		 nbytes;
18663b3a8eb9SGleb Smirnoff 	u_int8_t	 scheduler;
18673b3a8eb9SGleb Smirnoff };
18683b3a8eb9SGleb Smirnoff 
1869249cc75fSPatrick Kelsey struct pfioc_qstats_v1 {
1870249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1871249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1872249cc75fSPatrick Kelsey 	void		*buf;
1873249cc75fSPatrick Kelsey 	int		 nbytes;
1874249cc75fSPatrick Kelsey 	u_int8_t	 scheduler;
1875249cc75fSPatrick Kelsey 	/*
1876249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1877249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1878249cc75fSPatrick Kelsey 	 */
1879249cc75fSPatrick Kelsey 	u_int32_t	 version;  /* Requested version of stats struct */
1880249cc75fSPatrick Kelsey };
1881249cc75fSPatrick Kelsey 
1882249cc75fSPatrick Kelsey /* Latest version of struct pfioc_qstats_vX */
1883249cc75fSPatrick Kelsey #define PFIOC_QSTATS_VERSION	1
1884249cc75fSPatrick Kelsey 
18853b3a8eb9SGleb Smirnoff struct pfioc_ruleset {
18863b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
18873b3a8eb9SGleb Smirnoff 	char		 path[MAXPATHLEN];
18883b3a8eb9SGleb Smirnoff 	char		 name[PF_ANCHOR_NAME_SIZE];
18893b3a8eb9SGleb Smirnoff };
18903b3a8eb9SGleb Smirnoff 
18913b3a8eb9SGleb Smirnoff #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
18923b3a8eb9SGleb Smirnoff #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1893e732e742SKristof Provost #define PF_RULESET_ETH		(PF_RULESET_MAX+2)
18943b3a8eb9SGleb Smirnoff struct pfioc_trans {
18953b3a8eb9SGleb Smirnoff 	int		 size;	/* number of elements */
18963b3a8eb9SGleb Smirnoff 	int		 esize; /* size of each element in bytes */
18973b3a8eb9SGleb Smirnoff 	struct pfioc_trans_e {
18983b3a8eb9SGleb Smirnoff 		int		rs_num;
18993b3a8eb9SGleb Smirnoff 		char		anchor[MAXPATHLEN];
19003b3a8eb9SGleb Smirnoff 		u_int32_t	ticket;
19013b3a8eb9SGleb Smirnoff 	}		*array;
19023b3a8eb9SGleb Smirnoff };
19033b3a8eb9SGleb Smirnoff 
19043b3a8eb9SGleb Smirnoff #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
19053b3a8eb9SGleb Smirnoff #define PFR_FLAG_DUMMY		0x00000002
19063b3a8eb9SGleb Smirnoff #define PFR_FLAG_FEEDBACK	0x00000004
19073b3a8eb9SGleb Smirnoff #define PFR_FLAG_CLSTATS	0x00000008
19083b3a8eb9SGleb Smirnoff #define PFR_FLAG_ADDRSTOO	0x00000010
19093b3a8eb9SGleb Smirnoff #define PFR_FLAG_REPLACE	0x00000020
19103b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLRSETS	0x00000040
19113b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLMASK	0x0000007F
19123b3a8eb9SGleb Smirnoff #ifdef _KERNEL
19133b3a8eb9SGleb Smirnoff #define PFR_FLAG_USERIOCTL	0x10000000
19143b3a8eb9SGleb Smirnoff #endif
19153b3a8eb9SGleb Smirnoff 
19163b3a8eb9SGleb Smirnoff struct pfioc_table {
19173b3a8eb9SGleb Smirnoff 	struct pfr_table	 pfrio_table;
19183b3a8eb9SGleb Smirnoff 	void			*pfrio_buffer;
19193b3a8eb9SGleb Smirnoff 	int			 pfrio_esize;
19203b3a8eb9SGleb Smirnoff 	int			 pfrio_size;
19213b3a8eb9SGleb Smirnoff 	int			 pfrio_size2;
19223b3a8eb9SGleb Smirnoff 	int			 pfrio_nadd;
19233b3a8eb9SGleb Smirnoff 	int			 pfrio_ndel;
19243b3a8eb9SGleb Smirnoff 	int			 pfrio_nchange;
19253b3a8eb9SGleb Smirnoff 	int			 pfrio_flags;
19263b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrio_ticket;
19273b3a8eb9SGleb Smirnoff };
19283b3a8eb9SGleb Smirnoff #define	pfrio_exists	pfrio_nadd
19293b3a8eb9SGleb Smirnoff #define	pfrio_nzero	pfrio_nadd
19303b3a8eb9SGleb Smirnoff #define	pfrio_nmatch	pfrio_nadd
19313b3a8eb9SGleb Smirnoff #define pfrio_naddr	pfrio_size2
19323b3a8eb9SGleb Smirnoff #define pfrio_setflag	pfrio_size2
19333b3a8eb9SGleb Smirnoff #define pfrio_clrflag	pfrio_nadd
19343b3a8eb9SGleb Smirnoff 
19353b3a8eb9SGleb Smirnoff struct pfioc_iface {
19363b3a8eb9SGleb Smirnoff 	char	 pfiio_name[IFNAMSIZ];
19373b3a8eb9SGleb Smirnoff 	void	*pfiio_buffer;
19383b3a8eb9SGleb Smirnoff 	int	 pfiio_esize;
19393b3a8eb9SGleb Smirnoff 	int	 pfiio_size;
19403b3a8eb9SGleb Smirnoff 	int	 pfiio_nzero;
19413b3a8eb9SGleb Smirnoff 	int	 pfiio_flags;
19423b3a8eb9SGleb Smirnoff };
19433b3a8eb9SGleb Smirnoff 
19443b3a8eb9SGleb Smirnoff /*
19453b3a8eb9SGleb Smirnoff  * ioctl operations
19463b3a8eb9SGleb Smirnoff  */
19473b3a8eb9SGleb Smirnoff 
19483b3a8eb9SGleb Smirnoff #define DIOCSTART	_IO  ('D',  1)
19493b3a8eb9SGleb Smirnoff #define DIOCSTOP	_IO  ('D',  2)
19503b3a8eb9SGleb Smirnoff #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
19515c62ededSKristof Provost #define DIOCADDRULENV	_IOWR('D',  4, struct pfioc_nv)
19523b3a8eb9SGleb Smirnoff #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1953d710367dSKristof Provost #define DIOCGETRULENV	_IOWR('D',  7, struct pfioc_nv)
19543b3a8eb9SGleb Smirnoff /* XXX cut 8 - 17 */
19557606a45dSKristof Provost #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv)
1956cc535c95SWill Andrews #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
19571732afaaSKristof Provost #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv)
19583b3a8eb9SGleb Smirnoff #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
19592b10cf85SKristof Provost #define DIOCGETSTATUSNV	_IOWR('D', 21, struct pfioc_nv)
19603b3a8eb9SGleb Smirnoff #define DIOCCLRSTATUS	_IO  ('D', 22)
19613b3a8eb9SGleb Smirnoff #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
19623b3a8eb9SGleb Smirnoff #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
1963586fae3cSKristof Provost #ifdef COMPAT_FREEBSD14
19643b3a8eb9SGleb Smirnoff #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1965586fae3cSKristof Provost #endif
1966cc535c95SWill Andrews #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
19673b3a8eb9SGleb Smirnoff /* XXX cut 26 - 28 */
19683b3a8eb9SGleb Smirnoff #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
19693b3a8eb9SGleb Smirnoff #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1970cc535c95SWill Andrews #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1971cc535c95SWill Andrews #define DIOCCLRRULECTRS	_IO  ('D', 38)
19723b3a8eb9SGleb Smirnoff #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
19733b3a8eb9SGleb Smirnoff #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1974e989530aSKristof Provost #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv)
19753b3a8eb9SGleb Smirnoff #define DIOCSTARTALTQ	_IO  ('D', 42)
19763b3a8eb9SGleb Smirnoff #define DIOCSTOPALTQ	_IO  ('D', 43)
1977249cc75fSPatrick Kelsey #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1978249cc75fSPatrick Kelsey #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1979249cc75fSPatrick Kelsey #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1980249cc75fSPatrick Kelsey #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1981249cc75fSPatrick Kelsey #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1982249cc75fSPatrick Kelsey #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1983249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1984249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1985249cc75fSPatrick Kelsey #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1986249cc75fSPatrick Kelsey #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
19873b3a8eb9SGleb Smirnoff #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
19883b3a8eb9SGleb Smirnoff #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
19893b3a8eb9SGleb Smirnoff #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
19903b3a8eb9SGleb Smirnoff #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1991cc535c95SWill Andrews #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
19923b3a8eb9SGleb Smirnoff /* XXX cut 55 - 57 */
19933b3a8eb9SGleb Smirnoff #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
19943b3a8eb9SGleb Smirnoff #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
19953b3a8eb9SGleb Smirnoff #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
19963b3a8eb9SGleb Smirnoff #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
19973b3a8eb9SGleb Smirnoff #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
19983b3a8eb9SGleb Smirnoff #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
19993b3a8eb9SGleb Smirnoff #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
20003b3a8eb9SGleb Smirnoff #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
20013b3a8eb9SGleb Smirnoff #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
20023b3a8eb9SGleb Smirnoff #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
20033b3a8eb9SGleb Smirnoff #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
20043b3a8eb9SGleb Smirnoff #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
20053b3a8eb9SGleb Smirnoff #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
20063b3a8eb9SGleb Smirnoff #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
20073b3a8eb9SGleb Smirnoff #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
20083b3a8eb9SGleb Smirnoff #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
20093b3a8eb9SGleb Smirnoff #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
20103b3a8eb9SGleb Smirnoff #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
20113b3a8eb9SGleb Smirnoff #define	DIOCOSFPFLUSH	_IO('D', 78)
20123b3a8eb9SGleb Smirnoff #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
20133b3a8eb9SGleb Smirnoff #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
20143b3a8eb9SGleb Smirnoff #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
20153b3a8eb9SGleb Smirnoff #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
20163b3a8eb9SGleb Smirnoff #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
20173b3a8eb9SGleb Smirnoff #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
20183b3a8eb9SGleb Smirnoff #define	DIOCCLRSRCNODES	_IO('D', 85)
20193b3a8eb9SGleb Smirnoff #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
20203b3a8eb9SGleb Smirnoff #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
20213b3a8eb9SGleb Smirnoff #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
20223b3a8eb9SGleb Smirnoff #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
20233b3a8eb9SGleb Smirnoff #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
2024047c4e36SKristof Provost #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
2025047c4e36SKristof Provost #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
2026586fae3cSKristof Provost #ifdef COMPAT_FREEBSD14
2027c6bf20a2SKristof Provost #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2)
2028586fae3cSKristof Provost #endif
20298e1864edSKristof Provost #define	DIOCGETSYNCOOKIES	_IOWR('D', 94, struct pfioc_nv)
20308e1864edSKristof Provost #define	DIOCSETSYNCOOKIES	_IOWR('D', 95, struct pfioc_nv)
2031047c4e36SKristof Provost #define	DIOCKEEPCOUNTERS	_IOWR('D', 96, struct pfioc_nv)
20328e492101SKristof Provost #define	DIOCKEEPCOUNTERS_FREEBSD13	_IOWR('D', 92, struct pfioc_nv)
2033e732e742SKristof Provost #define	DIOCADDETHRULE		_IOWR('D', 97, struct pfioc_nv)
2034e732e742SKristof Provost #define	DIOCGETETHRULE		_IOWR('D', 98, struct pfioc_nv)
2035e732e742SKristof Provost #define	DIOCGETETHRULES		_IOWR('D', 99, struct pfioc_nv)
20369bb06778SKristof Provost #define	DIOCGETETHRULESETS	_IOWR('D', 100, struct pfioc_nv)
20379bb06778SKristof Provost #define	DIOCGETETHRULESET	_IOWR('D', 101, struct pfioc_nv)
203839282ef3SKajetan Staszkiewicz #define DIOCSETREASS		_IOWR('D', 102, u_int32_t)
203942ec75f8SKristof Provost 
2040249cc75fSPatrick Kelsey struct pf_ifspeed_v0 {
20413b3a8eb9SGleb Smirnoff 	char			ifname[IFNAMSIZ];
20423b3a8eb9SGleb Smirnoff 	u_int32_t		baudrate;
20433b3a8eb9SGleb Smirnoff };
2044249cc75fSPatrick Kelsey 
2045249cc75fSPatrick Kelsey struct pf_ifspeed_v1 {
2046249cc75fSPatrick Kelsey 	char			ifname[IFNAMSIZ];
2047249cc75fSPatrick Kelsey 	u_int32_t		baudrate32;
2048249cc75fSPatrick Kelsey 	/* layout identical to struct pf_ifspeed_v0 up to this point */
2049249cc75fSPatrick Kelsey 	u_int64_t		baudrate;
2050249cc75fSPatrick Kelsey };
2051249cc75fSPatrick Kelsey 
2052249cc75fSPatrick Kelsey /* Latest version of struct pf_ifspeed_vX */
2053249cc75fSPatrick Kelsey #define PF_IFSPEED_VERSION	1
2054249cc75fSPatrick Kelsey 
2055249cc75fSPatrick Kelsey /*
2056249cc75fSPatrick Kelsey  * Compatibility and convenience macros
2057249cc75fSPatrick Kelsey  */
2058249cc75fSPatrick Kelsey #ifndef _KERNEL
2059249cc75fSPatrick Kelsey #ifdef PFIOC_USE_LATEST
2060249cc75fSPatrick Kelsey /*
2061249cc75fSPatrick Kelsey  * Maintaining in-tree consumers of the ioctl interface is easier when that
2062249cc75fSPatrick Kelsey  * code can be written in terms old names that refer to the latest interface
2063249cc75fSPatrick Kelsey  * version as that reduces the required changes in the consumers to those
2064249cc75fSPatrick Kelsey  * that are functionally necessary to accommodate a new interface version.
2065249cc75fSPatrick Kelsey  */
2066249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
2067249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
2068249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
2069249cc75fSPatrick Kelsey 
2070249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
2071249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
2072249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
2073249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
2074249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
2075249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
2076249cc75fSPatrick Kelsey #else
2077249cc75fSPatrick Kelsey /*
2078249cc75fSPatrick Kelsey  * When building out-of-tree code that is written for the old interface,
2079249cc75fSPatrick Kelsey  * such as may exist in ports for example, resolve the old struct tags and
2080249cc75fSPatrick Kelsey  * ioctl command names to the v0 versions.
2081249cc75fSPatrick Kelsey  */
2082249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
2083249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
2084249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
2085249cc75fSPatrick Kelsey 
2086249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
2087249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
2088249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
2089249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
2090249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
2091249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
2092249cc75fSPatrick Kelsey #endif /* PFIOC_USE_LATEST */
2093249cc75fSPatrick Kelsey #endif /* _KERNEL */
20943b3a8eb9SGleb Smirnoff 
20953b3a8eb9SGleb Smirnoff #ifdef _KERNEL
209617ad7334SKristof Provost LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
20973b3a8eb9SGleb Smirnoff struct pf_srchash {
209817ad7334SKristof Provost 	struct pf_ksrc_node_list		nodes;
20993b3a8eb9SGleb Smirnoff 	struct mtx			lock;
21003b3a8eb9SGleb Smirnoff };
21013b3a8eb9SGleb Smirnoff 
21023b3a8eb9SGleb Smirnoff struct pf_keyhash {
21033b3a8eb9SGleb Smirnoff 	LIST_HEAD(, pf_state_key)	keys;
21043b3a8eb9SGleb Smirnoff 	struct mtx			lock;
21053b3a8eb9SGleb Smirnoff };
21063b3a8eb9SGleb Smirnoff 
21073b3a8eb9SGleb Smirnoff struct pf_idhash {
2108211cddf9SKristof Provost 	LIST_HEAD(, pf_kstate)		states;
21093b3a8eb9SGleb Smirnoff 	struct mtx			lock;
21103b3a8eb9SGleb Smirnoff };
21113b3a8eb9SGleb Smirnoff 
2112231e83d3SKristof Provost extern u_long		pf_ioctl_maxcount;
21136828cc99SGleb Smirnoff extern u_long		pf_hashmask;
21146828cc99SGleb Smirnoff extern u_long		pf_srchashmask;
211591e0f2d2SKristof Provost #define	PF_HASHSIZ	(131072)
2116bf56a3feSKristof Provost #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
21173b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
21183b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_idhash *, pf_idhash);
21193b3a8eb9SGleb Smirnoff #define V_pf_keyhash	VNET(pf_keyhash)
21203b3a8eb9SGleb Smirnoff #define	V_pf_idhash	VNET(pf_idhash)
21213b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_srchash *, pf_srchash);
21223b3a8eb9SGleb Smirnoff #define	V_pf_srchash	VNET(pf_srchash)
21233b3a8eb9SGleb Smirnoff 
21246828cc99SGleb Smirnoff #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
21253b3a8eb9SGleb Smirnoff 
21263b3a8eb9SGleb Smirnoff VNET_DECLARE(void *, pf_swi_cookie);
21273b3a8eb9SGleb Smirnoff #define V_pf_swi_cookie	VNET(pf_swi_cookie)
2128cecfaf9bSKristof Provost VNET_DECLARE(struct intr_event *, pf_swi_ie);
2129cecfaf9bSKristof Provost #define	V_pf_swi_ie	VNET(pf_swi_ie)
21303b3a8eb9SGleb Smirnoff 
2131133935d2SKristof Provost VNET_DECLARE(struct unrhdr64, pf_stateid);
21323b3a8eb9SGleb Smirnoff #define	V_pf_stateid	VNET(pf_stateid)
21333b3a8eb9SGleb Smirnoff 
21343b3a8eb9SGleb Smirnoff TAILQ_HEAD(pf_altqqueue, pf_altq);
21358f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
21363b3a8eb9SGleb Smirnoff #define	V_pf_altqs			 VNET(pf_altqs)
2137320c1116SKristof Provost VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
21383b3a8eb9SGleb Smirnoff #define	V_pf_pabuf			 VNET(pf_pabuf)
21393b3a8eb9SGleb Smirnoff 
21403b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
21413b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
21423b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
21433b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
21443b3a8eb9SGleb Smirnoff VNET_DECLARE(int,			 altqs_inactive_open);
21453b3a8eb9SGleb Smirnoff #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
21463b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_pabuf);
21473b3a8eb9SGleb Smirnoff #define	V_ticket_pabuf			 VNET(ticket_pabuf)
21483b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
21493b3a8eb9SGleb Smirnoff #define	V_pf_altqs_active		 VNET(pf_altqs_active)
21508f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
21518f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
21523b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
21533b3a8eb9SGleb Smirnoff #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
21548f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
21558f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
21563b3a8eb9SGleb Smirnoff 
2157e86bddeaSKristof Provost VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
21583b3a8eb9SGleb Smirnoff #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
21593b3a8eb9SGleb Smirnoff 
2160d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
2161d40d4b3eSMateusz Guzik LIST_HEAD(allkiflist_head, pfi_kkif);
2162d40d4b3eSMateusz Guzik VNET_DECLARE(struct allkiflist_head, pf_allkiflist);
2163d40d4b3eSMateusz Guzik #define V_pf_allkiflist     VNET(pf_allkiflist)
2164d40d4b3eSMateusz Guzik VNET_DECLARE(size_t, pf_allkifcount);
2165d40d4b3eSMateusz Guzik #define V_pf_allkifcount     VNET(pf_allkifcount)
2166d40d4b3eSMateusz Guzik VNET_DECLARE(struct pfi_kkif *, pf_kifmarker);
2167d40d4b3eSMateusz Guzik #define V_pf_kifmarker     VNET(pf_kifmarker)
216802cf67ccSMateusz Guzik 
216902cf67ccSMateusz Guzik LIST_HEAD(allrulelist_head, pf_krule);
217002cf67ccSMateusz Guzik VNET_DECLARE(struct allrulelist_head, pf_allrulelist);
217102cf67ccSMateusz Guzik #define V_pf_allrulelist     VNET(pf_allrulelist)
217202cf67ccSMateusz Guzik VNET_DECLARE(size_t, pf_allrulecount);
217302cf67ccSMateusz Guzik #define V_pf_allrulecount     VNET(pf_allrulecount)
217402cf67ccSMateusz Guzik VNET_DECLARE(struct pf_krule *, pf_rulemarker);
217502cf67ccSMateusz Guzik #define V_pf_rulemarker     VNET(pf_rulemarker)
2176d40d4b3eSMateusz Guzik #endif
2177d40d4b3eSMateusz Guzik 
217881647eb6SKristof Provost int				 pf_start(void);
217981647eb6SKristof Provost int				 pf_stop(void);
2180efc6c51fSGleb Smirnoff void				 pf_initialize(void);
21817e92ce73SMartin Matuska void				 pf_mtag_initialize(void);
21827e92ce73SMartin Matuska void				 pf_mtag_cleanup(void);
21833b3a8eb9SGleb Smirnoff void				 pf_cleanup(void);
21843b3a8eb9SGleb Smirnoff 
21853b3a8eb9SGleb Smirnoff struct pf_mtag			*pf_get_mtag(struct mbuf *);
21863b3a8eb9SGleb Smirnoff 
2187e86bddeaSKristof Provost extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
21883b3a8eb9SGleb Smirnoff #ifdef ALTQ
21893b3a8eb9SGleb Smirnoff extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
21903b3a8eb9SGleb Smirnoff #endif
21913b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_z);
21923b3a8eb9SGleb Smirnoff #define	V_pf_state_z		 VNET(pf_state_z)
21933b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
21943b3a8eb9SGleb Smirnoff #define	V_pf_state_key_z	 VNET(pf_state_key_z)
21953b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
21963b3a8eb9SGleb Smirnoff #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
21973b3a8eb9SGleb Smirnoff 
21983b3a8eb9SGleb Smirnoff extern void			 pf_purge_thread(void *);
2199a0429b54SBjoern A. Zeeb extern void			 pf_unload_vnet_purge(void);
22003b3a8eb9SGleb Smirnoff extern void			 pf_intr(void *);
22013b3a8eb9SGleb Smirnoff extern void			 pf_purge_expired_src_nodes(void);
22023b3a8eb9SGleb Smirnoff 
22038f3d786cSMateusz Guzik extern int			 pf_unlink_state(struct pf_kstate *);
2204320c1116SKristof Provost extern int			 pf_state_insert(struct pfi_kkif *,
2205d0fdf2b2SKristof Provost 				    struct pfi_kkif *,
22063b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
22073b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
2208211cddf9SKristof Provost 				    struct pf_kstate *);
2209211cddf9SKristof Provost extern struct pf_kstate		*pf_alloc_state(int);
2210211cddf9SKristof Provost extern void			 pf_free_state(struct pf_kstate *);
2211706d465dSKristof Provost extern void			 pf_killstates(struct pf_kstate_kill *,
2212706d465dSKristof Provost 				    unsigned int *);
2213706d465dSKristof Provost extern unsigned int		 pf_clear_states(const struct pf_kstate_kill *);
22143b3a8eb9SGleb Smirnoff 
22153b3a8eb9SGleb Smirnoff static __inline void
pf_ref_state(struct pf_kstate * s)2216211cddf9SKristof Provost pf_ref_state(struct pf_kstate *s)
22173b3a8eb9SGleb Smirnoff {
22183b3a8eb9SGleb Smirnoff 
22193b3a8eb9SGleb Smirnoff 	refcount_acquire(&s->refs);
22203b3a8eb9SGleb Smirnoff }
22213b3a8eb9SGleb Smirnoff 
22223b3a8eb9SGleb Smirnoff static __inline int
pf_release_state(struct pf_kstate * s)2223211cddf9SKristof Provost pf_release_state(struct pf_kstate *s)
22243b3a8eb9SGleb Smirnoff {
22253b3a8eb9SGleb Smirnoff 
22263b3a8eb9SGleb Smirnoff 	if (refcount_release(&s->refs)) {
22273b3a8eb9SGleb Smirnoff 		pf_free_state(s);
22283b3a8eb9SGleb Smirnoff 		return (1);
22293b3a8eb9SGleb Smirnoff 	} else
22303b3a8eb9SGleb Smirnoff 		return (0);
22313b3a8eb9SGleb Smirnoff }
22323b3a8eb9SGleb Smirnoff 
2233d09388d0SMateusz Guzik static __inline int
pf_release_staten(struct pf_kstate * s,u_int n)2234211cddf9SKristof Provost pf_release_staten(struct pf_kstate *s, u_int n)
2235d09388d0SMateusz Guzik {
2236d09388d0SMateusz Guzik 
2237d09388d0SMateusz Guzik 	if (refcount_releasen(&s->refs, n)) {
2238d09388d0SMateusz Guzik 		pf_free_state(s);
2239d09388d0SMateusz Guzik 		return (1);
2240d09388d0SMateusz Guzik 	} else
2241d09388d0SMateusz Guzik 		return (0);
2242d09388d0SMateusz Guzik }
2243d09388d0SMateusz Guzik 
224404932601SKristof Provost static __inline uint64_t
pf_get_uptime(void)224504932601SKristof Provost pf_get_uptime(void)
224604932601SKristof Provost {
224704932601SKristof Provost 	struct timeval t;
224804932601SKristof Provost 	microuptime(&t);
224904932601SKristof Provost 	return ((t.tv_sec * 1000) + (t.tv_usec / 1000));
225004932601SKristof Provost }
225104932601SKristof Provost 
225204932601SKristof Provost static __inline uint64_t
pf_get_time(void)225304932601SKristof Provost pf_get_time(void)
225404932601SKristof Provost {
225504932601SKristof Provost 	struct timeval t;
225604932601SKristof Provost 	microtime(&t);
225704932601SKristof Provost 	return ((t.tv_sec * 1000) + (t.tv_usec / 1000));
225804932601SKristof Provost }
225904932601SKristof Provost 
2260211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t);
2261211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_all(struct pf_state_key_cmp *,
22623b3a8eb9SGleb Smirnoff 				    u_int, int *);
226319d6e29bSMateusz Guzik extern bool			pf_find_state_all_exists(struct pf_state_key_cmp *,
226419d6e29bSMateusz Guzik 				    u_int);
226517ad7334SKristof Provost extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
2266db0a2bfdSKajetan Staszkiewicz 				    struct pf_krule *, sa_family_t,
2267db0a2bfdSKajetan Staszkiewicz 				    struct pf_srchash **, bool);
226817ad7334SKristof Provost extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
226917ad7334SKristof Provost extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
2270211cddf9SKristof Provost extern void			 pf_print_state(struct pf_kstate *);
227107d9bc07SGleb Smirnoff extern void			 pf_print_flags(u_int8_t);
2272ba94bf28SKristof Provost extern int			 pf_addr_wrap_neq(struct pf_addr_wrap *,
2273ba94bf28SKristof Provost 				    struct pf_addr_wrap *);
22743b3a8eb9SGleb Smirnoff extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
22753b3a8eb9SGleb Smirnoff 				    u_int8_t);
2276c110fc49SKristof Provost extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
2277c110fc49SKristof Provost 				    u_int16_t, u_int16_t, u_int8_t);
22783b3a8eb9SGleb Smirnoff 
22793b3a8eb9SGleb Smirnoff VNET_DECLARE(struct ifnet *,		 sync_ifp);
22803b3a8eb9SGleb Smirnoff #define	V_sync_ifp		 	 VNET(sync_ifp);
2281e86bddeaSKristof Provost VNET_DECLARE(struct pf_krule,		 pf_default_rule);
22823b3a8eb9SGleb Smirnoff #define	V_pf_default_rule		  VNET(pf_default_rule)
22833b3a8eb9SGleb Smirnoff extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
22848216f1a9SKristof Provost 				    sa_family_t);
2285e86bddeaSKristof Provost void				pf_free_rule(struct pf_krule *);
22863b3a8eb9SGleb Smirnoff 
2287e732e742SKristof Provost int	pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
22883b3a8eb9SGleb Smirnoff #ifdef INET
22899925aee0SKristof Provost int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *,
22909925aee0SKristof Provost 	    struct pf_rule_actions *);
2291f2064dd1SKajetan Staszkiewicz int	pf_normalize_ip(struct mbuf **, struct pfi_kkif *, u_short *,
22920324938aSGleb Smirnoff 	    struct pf_pdesc *);
22933b3a8eb9SGleb Smirnoff #endif /* INET */
22943b3a8eb9SGleb Smirnoff 
22953b3a8eb9SGleb Smirnoff #ifdef INET6
22969925aee0SKristof Provost int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *,
22979925aee0SKristof Provost 	    struct pf_rule_actions *);
2298f2064dd1SKajetan Staszkiewicz int	pf_normalize_ip6(struct mbuf **, struct pfi_kkif *, u_short *,
22990324938aSGleb Smirnoff 	    struct pf_pdesc *);
23003b3a8eb9SGleb Smirnoff void	pf_poolmask(struct pf_addr *, struct pf_addr*,
23018216f1a9SKristof Provost 	    struct pf_addr *, struct pf_addr *, sa_family_t);
23023b3a8eb9SGleb Smirnoff void	pf_addr_inc(struct pf_addr *, sa_family_t);
230354c62e3eSKristof Provost int	pf_max_frag_size(struct mbuf *);
2304b52b61c0SKristof Provost int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, bool);
23053b3a8eb9SGleb Smirnoff #endif /* INET6 */
23063b3a8eb9SGleb Smirnoff 
230710aa9ddbSKristof Provost int	pf_multihome_scan_init(struct mbuf *, int, int, struct pf_pdesc *,
230810aa9ddbSKristof Provost 	    struct pfi_kkif *);
230910aa9ddbSKristof Provost int	pf_multihome_scan_asconf(struct mbuf *, int, int, struct pf_pdesc *,
231010aa9ddbSKristof Provost 	    struct pfi_kkif *);
231110aa9ddbSKristof Provost 
2312211cddf9SKristof Provost u_int32_t	pf_new_isn(struct pf_kstate *);
23133b3a8eb9SGleb Smirnoff void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
23143b3a8eb9SGleb Smirnoff 	    sa_family_t);
23153b3a8eb9SGleb Smirnoff void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2316c110fc49SKristof Provost void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
2317c110fc49SKristof Provost 	    u_int8_t);
2318c110fc49SKristof Provost void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
2319c3f69af0SKristof Provost void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
2320c3f69af0SKristof Provost 	    bool, u_int8_t);
2321c3f69af0SKristof Provost void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
2322c3f69af0SKristof Provost     bool, u_int8_t);
2323211cddf9SKristof Provost void	pf_send_deferred_syn(struct pf_kstate *);
23243b3a8eb9SGleb Smirnoff int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
23253b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
23263b3a8eb9SGleb Smirnoff int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
23273b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
23283b3a8eb9SGleb Smirnoff int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
23293b3a8eb9SGleb Smirnoff 
2330efc6c51fSGleb Smirnoff void	pf_normalize_init(void);
23313b3a8eb9SGleb Smirnoff void	pf_normalize_cleanup(void);
2332f2064dd1SKajetan Staszkiewicz int	pf_normalize_tcp(struct pfi_kkif *, struct mbuf *, int, int, void *,
23333b3a8eb9SGleb Smirnoff 	    struct pf_pdesc *);
2334211cddf9SKristof Provost void	pf_normalize_tcp_cleanup(struct pf_kstate *);
23353b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
23363b3a8eb9SGleb Smirnoff 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
23373b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
2338211cddf9SKristof Provost 	    u_short *, struct tcphdr *, struct pf_kstate *,
23393b3a8eb9SGleb Smirnoff 	    struct pf_state_peer *, struct pf_state_peer *, int *);
234051a78dd2SKristof Provost int	pf_normalize_sctp_init(struct mbuf *, int, struct pf_pdesc *,
234151a78dd2SKristof Provost 	    struct pf_state_peer *, struct pf_state_peer *);
2342010ee43fSKristof Provost int	pf_normalize_sctp(int, struct pfi_kkif *, struct mbuf *, int,
2343010ee43fSKristof Provost 	    int, void *, struct pf_pdesc *);
23443b3a8eb9SGleb Smirnoff u_int32_t
2345211cddf9SKristof Provost 	pf_state_expires(const struct pf_kstate *);
23463b3a8eb9SGleb Smirnoff void	pf_purge_expired_fragments(void);
23475d0020d6SKristof Provost void	pf_purge_fragments(uint32_t);
2348320c1116SKristof Provost int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
23493b3a8eb9SGleb Smirnoff 	    int);
2350f2064dd1SKajetan Staszkiewicz int	pf_socket_lookup(struct pf_pdesc *, struct mbuf *);
23513b3a8eb9SGleb Smirnoff struct pf_state_key *pf_alloc_state_key(int);
23523b3a8eb9SGleb Smirnoff void	pfr_initialize(void);
23533b3a8eb9SGleb Smirnoff void	pfr_cleanup(void);
23543b3a8eb9SGleb Smirnoff int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
23553b3a8eb9SGleb Smirnoff void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
23563b3a8eb9SGleb Smirnoff 	    u_int64_t, int, int, int);
23573b3a8eb9SGleb Smirnoff int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
23583b3a8eb9SGleb Smirnoff void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
23593b3a8eb9SGleb Smirnoff struct pfr_ktable *
2360e86bddeaSKristof Provost 	pfr_attach_table(struct pf_kruleset *, char *);
2361812839e5SKristof Provost struct pfr_ktable *
2362812839e5SKristof Provost 	pfr_eth_attach_table(struct pf_keth_ruleset *, char *);
23633b3a8eb9SGleb Smirnoff void	pfr_detach_table(struct pfr_ktable *);
23643b3a8eb9SGleb Smirnoff int	pfr_clr_tables(struct pfr_table *, int *, int);
23653b3a8eb9SGleb Smirnoff int	pfr_add_tables(struct pfr_table *, int, int *, int);
23663b3a8eb9SGleb Smirnoff int	pfr_del_tables(struct pfr_table *, int, int *, int);
2367adfe2f6aSKristof Provost int	pfr_table_count(struct pfr_table *, int);
23683b3a8eb9SGleb Smirnoff int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
23693b3a8eb9SGleb Smirnoff int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
23703b3a8eb9SGleb Smirnoff int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
23713b3a8eb9SGleb Smirnoff int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
23723b3a8eb9SGleb Smirnoff int	pfr_clr_addrs(struct pfr_table *, int *, int);
23733b3a8eb9SGleb Smirnoff int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
23743b3a8eb9SGleb Smirnoff int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23753b3a8eb9SGleb Smirnoff 	    int);
23763b3a8eb9SGleb Smirnoff int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23773b3a8eb9SGleb Smirnoff 	    int);
23783b3a8eb9SGleb Smirnoff int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23793b3a8eb9SGleb Smirnoff 	    int *, int *, int *, int, u_int32_t);
23803b3a8eb9SGleb Smirnoff int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
23813b3a8eb9SGleb Smirnoff int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
23823b3a8eb9SGleb Smirnoff int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
23833b3a8eb9SGleb Smirnoff 	    int);
23843b3a8eb9SGleb Smirnoff int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23853b3a8eb9SGleb Smirnoff 	    int);
23863b3a8eb9SGleb Smirnoff int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
23873b3a8eb9SGleb Smirnoff int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
23883b3a8eb9SGleb Smirnoff int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
23893b3a8eb9SGleb Smirnoff int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
23903b3a8eb9SGleb Smirnoff 	    int *, u_int32_t, int);
23913b3a8eb9SGleb Smirnoff 
23923b3a8eb9SGleb Smirnoff MALLOC_DECLARE(PFI_MTYPE);
2393320c1116SKristof Provost VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
23943b3a8eb9SGleb Smirnoff #define	V_pfi_all	 		 VNET(pfi_all)
23953b3a8eb9SGleb Smirnoff 
2396efc6c51fSGleb Smirnoff void		 pfi_initialize(void);
2397a0429b54SBjoern A. Zeeb void		 pfi_initialize_vnet(void);
23983b3a8eb9SGleb Smirnoff void		 pfi_cleanup(void);
2399a0429b54SBjoern A. Zeeb void		 pfi_cleanup_vnet(void);
2400320c1116SKristof Provost void		 pfi_kkif_ref(struct pfi_kkif *);
2401320c1116SKristof Provost void		 pfi_kkif_unref(struct pfi_kkif *);
2402320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_find(const char *);
2403320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
2404320c1116SKristof Provost int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
2405320c1116SKristof Provost void		 pfi_kkif_purge(void);
24063b3a8eb9SGleb Smirnoff int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
24073b3a8eb9SGleb Smirnoff 		    sa_family_t);
24083b3a8eb9SGleb Smirnoff int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
24093b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
24103b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
24113b3a8eb9SGleb Smirnoff void		 pfi_update_status(const char *, struct pf_status *);
24123b3a8eb9SGleb Smirnoff void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
24133b3a8eb9SGleb Smirnoff int		 pfi_set_flags(const char *, int);
24143b3a8eb9SGleb Smirnoff int		 pfi_clear_flags(const char *, int);
24153b3a8eb9SGleb Smirnoff 
2416e86bddeaSKristof Provost int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
24173b3a8eb9SGleb Smirnoff int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
241860048052SGleb Smirnoff int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
241960048052SGleb Smirnoff 		    sa_family_t);
24203b3a8eb9SGleb Smirnoff 
24218e1864edSKristof Provost u_int16_t	 pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t);
24228e1864edSKristof Provost u_int8_t	 pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t);
24238e1864edSKristof Provost struct mbuf 	*pf_build_tcp(const struct pf_krule *, sa_family_t,
24248e1864edSKristof Provost 		    const struct pf_addr *, const struct pf_addr *,
24258e1864edSKristof Provost 		    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
24267dc3be36SKajetan Staszkiewicz 		    u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool,
24277dc3be36SKajetan Staszkiewicz 		    u_int16_t, u_int16_t, int);
24288e1864edSKristof Provost void		 pf_send_tcp(const struct pf_krule *, sa_family_t,
24298e1864edSKristof Provost 			    const struct pf_addr *, const struct pf_addr *,
24308e1864edSKristof Provost 			    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
24317dc3be36SKajetan Staszkiewicz 			    u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool,
24327dc3be36SKajetan Staszkiewicz 			    u_int16_t, u_int16_t, int);
24338e1864edSKristof Provost 
24348e1864edSKristof Provost void			 pf_syncookies_init(void);
243532271c4dSKristof Provost void			 pf_syncookies_cleanup(void);
24368e1864edSKristof Provost int			 pf_get_syncookies(struct pfioc_nv *);
24378e1864edSKristof Provost int			 pf_set_syncookies(struct pfioc_nv *);
24388e1864edSKristof Provost int			 pf_synflood_check(struct pf_pdesc *);
24398e1864edSKristof Provost void			 pf_syncookie_send(struct mbuf *m, int off,
24408e1864edSKristof Provost 			    struct pf_pdesc *);
24419c041b45SKristof Provost bool			 pf_syncookie_check(struct pf_pdesc *);
24428e1864edSKristof Provost u_int8_t		 pf_syncookie_validate(struct pf_pdesc *);
24438e1864edSKristof Provost struct mbuf *		 pf_syncookie_recreate_syn(uint8_t, int,
24448e1864edSKristof Provost 			    struct pf_pdesc *);
24458e1864edSKristof Provost 
2446a9572d8fSGleb Smirnoff VNET_DECLARE(struct pf_kstatus, pf_status);
24473b3a8eb9SGleb Smirnoff #define	V_pf_status	VNET(pf_status)
24483b3a8eb9SGleb Smirnoff 
24493b3a8eb9SGleb Smirnoff struct pf_limit {
24503b3a8eb9SGleb Smirnoff 	uma_zone_t	zone;
24513b3a8eb9SGleb Smirnoff 	u_int		limit;
24523b3a8eb9SGleb Smirnoff };
24533b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
24543b3a8eb9SGleb Smirnoff #define	V_pf_limits VNET(pf_limits)
24553b3a8eb9SGleb Smirnoff 
24563b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
24573b3a8eb9SGleb Smirnoff 
24583b3a8eb9SGleb Smirnoff #ifdef _KERNEL
2459e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
24603b3a8eb9SGleb Smirnoff #define	V_pf_anchors				 VNET(pf_anchors)
2461e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
24623b3a8eb9SGleb Smirnoff #define	V_pf_main_anchor			 VNET(pf_main_anchor)
2463c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor_global,	 pf_keth_anchors);
2464c5131afeSKristof Provost #define	V_pf_keth_anchors			 VNET(pf_keth_anchors)
24653b3a8eb9SGleb Smirnoff #define pf_main_ruleset	V_pf_main_anchor.ruleset
24663b3a8eb9SGleb Smirnoff 
2467c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor,		 pf_main_keth_anchor);
2468c5131afeSKristof Provost #define V_pf_main_keth_anchor			 VNET(pf_main_keth_anchor)
2469c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_ruleset*,		 pf_keth);
2470e732e742SKristof Provost #define	V_pf_keth				 VNET(pf_keth)
2471e732e742SKristof Provost 
2472e86bddeaSKristof Provost void			 pf_init_kruleset(struct pf_kruleset *);
2473c5131afeSKristof Provost void			 pf_init_keth(struct pf_keth_ruleset *);
2474e86bddeaSKristof Provost int			 pf_kanchor_setup(struct pf_krule *,
2475e86bddeaSKristof Provost 			    const struct pf_kruleset *, const char *);
2476777a4702SKristof Provost int			 pf_kanchor_copyout(const struct pf_kruleset *,
2477777a4702SKristof Provost 			    const struct pf_krule *, char *);
2478d710367dSKristof Provost int			 pf_kanchor_nvcopyout(const struct pf_kruleset *,
2479d710367dSKristof Provost 			    const struct pf_krule *, nvlist_t *);
2480e86bddeaSKristof Provost void			 pf_kanchor_remove(struct pf_krule *);
2481e86bddeaSKristof Provost void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
2482e86bddeaSKristof Provost struct pf_kruleset	*pf_find_kruleset(const char *);
2483e86bddeaSKristof Provost struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
24843b3a8eb9SGleb Smirnoff void			 pf_rs_initialize(void);
24855e9dae8eSKristof Provost 
2486c5131afeSKristof Provost 
2487773e3a71SMark Johnston struct pf_krule		*pf_krule_alloc(void);
2488c5131afeSKristof Provost 
2489c5131afeSKristof Provost void			 pf_remove_if_empty_keth_ruleset(
2490c5131afeSKristof Provost 			    struct pf_keth_ruleset *);
2491c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_keth_ruleset(const char *);
2492c5131afeSKristof Provost struct pf_keth_anchor	*pf_find_keth_anchor(const char *);
2493c5131afeSKristof Provost int			 pf_keth_anchor_setup(struct pf_keth_rule *,
2494c5131afeSKristof Provost 			    const struct pf_keth_ruleset *, const char *);
2495c5131afeSKristof Provost int			 pf_keth_anchor_nvcopyout(
2496c5131afeSKristof Provost 			    const struct pf_keth_ruleset *,
2497c5131afeSKristof Provost 			    const struct pf_keth_rule *, nvlist_t *);
2498c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_or_create_keth_ruleset(const char *);
2499c5131afeSKristof Provost void			 pf_keth_anchor_remove(struct pf_keth_rule *);
2500c5131afeSKristof Provost 
250144f323ecSKristof Provost int			 pf_ioctl_getrules(struct pfioc_rule *);
2502ffbf2595SKristof Provost int			 pf_ioctl_addrule(struct pf_krule *, uint32_t,
2503ffbf2595SKristof Provost 			    uint32_t, const char *, const char *, uid_t uid,
2504ffbf2595SKristof Provost 			    pid_t);
2505ffbf2595SKristof Provost 
25065e9dae8eSKristof Provost void			 pf_krule_free(struct pf_krule *);
2507777a4702SKristof Provost void			 pf_krule_clear_counters(struct pf_krule *);
2508e86bddeaSKristof Provost #endif
25093b3a8eb9SGleb Smirnoff 
25103b3a8eb9SGleb Smirnoff /* The fingerprint functions can be linked into userland programs (tcpdump) */
25113b3a8eb9SGleb Smirnoff int	pf_osfp_add(struct pf_osfp_ioctl *);
25123b3a8eb9SGleb Smirnoff #ifdef _KERNEL
25133b3a8eb9SGleb Smirnoff struct pf_osfp_enlist *
25143b3a8eb9SGleb Smirnoff 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
25153b3a8eb9SGleb Smirnoff 	    const struct tcphdr *);
25163b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
25173b3a8eb9SGleb Smirnoff void	pf_osfp_flush(void);
25183b3a8eb9SGleb Smirnoff int	pf_osfp_get(struct pf_osfp_ioctl *);
25193b3a8eb9SGleb Smirnoff int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
25203b3a8eb9SGleb Smirnoff 
25213b3a8eb9SGleb Smirnoff #ifdef _KERNEL
25228216f1a9SKristof Provost void			 pf_print_host(struct pf_addr *, u_int16_t, sa_family_t);
252307d9bc07SGleb Smirnoff 
2524e86bddeaSKristof Provost void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
2525e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2526e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2527e86bddeaSKristof Provost int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
2528e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2529e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2530c5131afeSKristof Provost void			 pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *,
2531c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2532c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2533c5131afeSKristof Provost 			    int *);
2534c5131afeSKristof Provost int			 pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *,
2535c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2536c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2537c5131afeSKristof Provost 			    int *);
25383b3a8eb9SGleb Smirnoff 
253916303d2bSKajetan Staszkiewicz u_short			 pf_map_addr(u_int8_t, struct pf_krule *,
25403b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
2541d10de21fSKajetan Staszkiewicz 			    struct pfi_kkif **nkif, struct pf_addr *,
2542d10de21fSKajetan Staszkiewicz 			    struct pf_ksrc_node **);
2543e86bddeaSKristof Provost struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
2544f2064dd1SKajetan Staszkiewicz 			    int, struct pfi_kkif *, struct pf_ksrc_node **,
25453b3a8eb9SGleb Smirnoff 			    struct pf_state_key **, struct pf_state_key **,
25463b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
2547e86bddeaSKristof Provost 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
25483b3a8eb9SGleb Smirnoff 
25493b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
25503b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, u_int16_t);
25513b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
25526b4ed16dSKajetan Staszkiewicz void			 pf_rule_to_actions(struct pf_krule *,
25536b4ed16dSKajetan Staszkiewicz 			    struct pf_rule_actions *);
255439282ef3SKajetan Staszkiewicz int			 pf_normalize_mss(struct mbuf *m, int off,
25556b4ed16dSKajetan Staszkiewicz 			    struct pf_pdesc *pd);
255639282ef3SKajetan Staszkiewicz #ifdef INET
25576b4ed16dSKajetan Staszkiewicz void	pf_scrub_ip(struct mbuf **, struct pf_pdesc *);
255839282ef3SKajetan Staszkiewicz #endif	/* INET */
255939282ef3SKajetan Staszkiewicz #ifdef INET6
25606b4ed16dSKajetan Staszkiewicz void	pf_scrub_ip6(struct mbuf **, struct pf_pdesc *);
256139282ef3SKajetan Staszkiewicz #endif	/* INET6 */
256239282ef3SKajetan Staszkiewicz 
256326c841e2SKristof Provost struct pfi_kkif		*pf_kkif_create(int);
256426c841e2SKristof Provost void			 pf_kkif_free(struct pfi_kkif *);
25655a3b9507SKristof Provost void			 pf_kkif_zero(struct pfi_kkif *);
25663b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
25673b3a8eb9SGleb Smirnoff 
25683b3a8eb9SGleb Smirnoff #endif /* _NET_PFVAR_H_ */
2569