xref: /freebsd/sys/net/pfvar.h (revision 51a78dd2)
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
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
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
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
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
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
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
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
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
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
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
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
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
242defdcdd5SMateusz Guzik pf_counter_u64_critical_enter(void)
243defdcdd5SMateusz Guzik {
244defdcdd5SMateusz Guzik 
245defdcdd5SMateusz Guzik }
246defdcdd5SMateusz Guzik 
247defdcdd5SMateusz Guzik static inline void
248defdcdd5SMateusz Guzik pf_counter_u64_critical_exit(void)
249defdcdd5SMateusz Guzik {
250defdcdd5SMateusz Guzik 
251defdcdd5SMateusz Guzik }
252defdcdd5SMateusz Guzik 
253defdcdd5SMateusz Guzik static inline void
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
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
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
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
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];
10643b3a8eb9SGleb Smirnoff 	u_int32_t		 creation;
10653b3a8eb9SGleb Smirnoff 	u_int32_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;
10703b3a8eb9SGleb Smirnoff };
1071e6dd0e2eSMateusz Guzik 
1072e6dd0e2eSMateusz Guzik /*
10734bf98559SKajetan Staszkiewicz  * Size <= fits 11 objects per page on LP64. Try to not grow the struct beyond that.
1074e6dd0e2eSMateusz Guzik  */
10754bf98559SKajetan Staszkiewicz _Static_assert(sizeof(struct pf_kstate) <= 368, "pf_kstate size crosses 368 bytes");
1076e86bddeaSKristof Provost #endif
10773b3a8eb9SGleb Smirnoff 
10783b3a8eb9SGleb Smirnoff /*
10793b3a8eb9SGleb Smirnoff  * Unified state structures for pulling states out of the kernel
10803b3a8eb9SGleb Smirnoff  * used by pfsync(4) and the pf(4) ioctl.
10813b3a8eb9SGleb Smirnoff  */
10823b3a8eb9SGleb Smirnoff struct pfsync_state_scrub {
10833b3a8eb9SGleb Smirnoff 	u_int16_t	pfss_flags;
10843b3a8eb9SGleb Smirnoff 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
10853b3a8eb9SGleb Smirnoff #define PFSYNC_SCRUB_FLAG_VALID		0x01
10863b3a8eb9SGleb Smirnoff 	u_int8_t	scrub_flag;
10873b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
10883b3a8eb9SGleb Smirnoff } __packed;
10893b3a8eb9SGleb Smirnoff 
10903b3a8eb9SGleb Smirnoff struct pfsync_state_peer {
10913b3a8eb9SGleb Smirnoff 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
10923b3a8eb9SGleb Smirnoff 	u_int32_t	seqlo;		/* Max sequence number sent	*/
10933b3a8eb9SGleb Smirnoff 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
10943b3a8eb9SGleb Smirnoff 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
10953b3a8eb9SGleb Smirnoff 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
10963b3a8eb9SGleb Smirnoff 	u_int16_t	mss;		/* Maximum segment size option	*/
10973b3a8eb9SGleb Smirnoff 	u_int8_t	state;		/* active state level		*/
10983b3a8eb9SGleb Smirnoff 	u_int8_t	wscale;		/* window scaling factor	*/
10993b3a8eb9SGleb Smirnoff 	u_int8_t	pad[6];
11003b3a8eb9SGleb Smirnoff } __packed;
11013b3a8eb9SGleb Smirnoff 
11023b3a8eb9SGleb Smirnoff struct pfsync_state_key {
11033b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
11043b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
11053b3a8eb9SGleb Smirnoff };
11063b3a8eb9SGleb Smirnoff 
11074bf98559SKajetan Staszkiewicz struct pfsync_state_1301 {
11084bf98559SKajetan Staszkiewicz 	u_int64_t	 id;
11094bf98559SKajetan Staszkiewicz 	char		 ifname[IFNAMSIZ];
11104bf98559SKajetan Staszkiewicz 	struct pfsync_state_key	key[2];
11114bf98559SKajetan Staszkiewicz 	struct pfsync_state_peer src;
11124bf98559SKajetan Staszkiewicz 	struct pfsync_state_peer dst;
11134bf98559SKajetan Staszkiewicz 	struct pf_addr	 rt_addr;
11144bf98559SKajetan Staszkiewicz 	u_int32_t	 rule;
11154bf98559SKajetan Staszkiewicz 	u_int32_t	 anchor;
11164bf98559SKajetan Staszkiewicz 	u_int32_t	 nat_rule;
11174bf98559SKajetan Staszkiewicz 	u_int32_t	 creation;
11184bf98559SKajetan Staszkiewicz 	u_int32_t	 expire;
11194bf98559SKajetan Staszkiewicz 	u_int32_t	 packets[2][2];
11204bf98559SKajetan Staszkiewicz 	u_int32_t	 bytes[2][2];
11214bf98559SKajetan Staszkiewicz 	u_int32_t	 creatorid;
11224bf98559SKajetan Staszkiewicz 	sa_family_t	 af;
11234bf98559SKajetan Staszkiewicz 	u_int8_t	 proto;
11244bf98559SKajetan Staszkiewicz 	u_int8_t	 direction;
11254bf98559SKajetan Staszkiewicz 	u_int8_t	 __spare[2];
11264bf98559SKajetan Staszkiewicz 	u_int8_t	 log;
11274bf98559SKajetan Staszkiewicz 	u_int8_t	 state_flags;
11284bf98559SKajetan Staszkiewicz 	u_int8_t	 timeout;
11294bf98559SKajetan Staszkiewicz 	u_int8_t	 sync_flags;
11304bf98559SKajetan Staszkiewicz 	u_int8_t	 updates;
11314bf98559SKajetan Staszkiewicz } __packed;
11324bf98559SKajetan Staszkiewicz 
11334bf98559SKajetan Staszkiewicz struct pfsync_state_1400 {
11344bf98559SKajetan Staszkiewicz 	/* The beginning of the struct is compatible with previous versions */
11353b3a8eb9SGleb Smirnoff 	u_int64_t	 id;
11363b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
11373b3a8eb9SGleb Smirnoff 	struct pfsync_state_key	key[2];
11383b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer src;
11393b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer dst;
11403b3a8eb9SGleb Smirnoff 	struct pf_addr	 rt_addr;
11413b3a8eb9SGleb Smirnoff 	u_int32_t	 rule;
11423b3a8eb9SGleb Smirnoff 	u_int32_t	 anchor;
11433b3a8eb9SGleb Smirnoff 	u_int32_t	 nat_rule;
11443b3a8eb9SGleb Smirnoff 	u_int32_t	 creation;
11453b3a8eb9SGleb Smirnoff 	u_int32_t	 expire;
11463b3a8eb9SGleb Smirnoff 	u_int32_t	 packets[2][2];
11473b3a8eb9SGleb Smirnoff 	u_int32_t	 bytes[2][2];
11483b3a8eb9SGleb Smirnoff 	u_int32_t	 creatorid;
11493b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
11503b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
11513b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
115239282ef3SKajetan Staszkiewicz 	u_int16_t	 state_flags;
11533b3a8eb9SGleb Smirnoff 	u_int8_t	 log;
11544bf98559SKajetan Staszkiewicz 	u_int8_t	 __spare;
11553b3a8eb9SGleb Smirnoff 	u_int8_t	 timeout;
11563b3a8eb9SGleb Smirnoff 	u_int8_t	 sync_flags;
11573b3a8eb9SGleb Smirnoff 	u_int8_t	 updates;
11584bf98559SKajetan Staszkiewicz 	/* The rest is not */
11594bf98559SKajetan Staszkiewicz 	u_int16_t	 qid;
11604bf98559SKajetan Staszkiewicz 	u_int16_t	 pqid;
11614bf98559SKajetan Staszkiewicz 	u_int16_t	 dnpipe;
11624bf98559SKajetan Staszkiewicz 	u_int16_t	 dnrpipe;
11634bf98559SKajetan Staszkiewicz 	int32_t		 rtableid;
11644bf98559SKajetan Staszkiewicz 	u_int8_t	 min_ttl;
11654bf98559SKajetan Staszkiewicz 	u_int8_t	 set_tos;
11664bf98559SKajetan Staszkiewicz 	u_int16_t	 max_mss;
11674bf98559SKajetan Staszkiewicz 	u_int8_t	 set_prio[2];
11684bf98559SKajetan Staszkiewicz 	u_int8_t	 rt;
11694bf98559SKajetan Staszkiewicz 	char		 rt_ifname[IFNAMSIZ];
11704bf98559SKajetan Staszkiewicz 
11714bf98559SKajetan Staszkiewicz } __packed;
11724bf98559SKajetan Staszkiewicz 
11734bf98559SKajetan Staszkiewicz union pfsync_state_union {
11744bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301 pfs_1301;
11754bf98559SKajetan Staszkiewicz 	struct pfsync_state_1400 pfs_1400;
11763b3a8eb9SGleb Smirnoff } __packed;
11773b3a8eb9SGleb Smirnoff 
11783b3a8eb9SGleb Smirnoff #ifdef _KERNEL
11793b3a8eb9SGleb Smirnoff /* pfsync */
11804bf98559SKajetan Staszkiewicz typedef int		pfsync_state_import_t(union pfsync_state_union *, int, int);
1181211cddf9SKristof Provost typedef	void		pfsync_insert_state_t(struct pf_kstate *);
1182211cddf9SKristof Provost typedef	void		pfsync_update_state_t(struct pf_kstate *);
1183211cddf9SKristof Provost typedef	void		pfsync_delete_state_t(struct pf_kstate *);
11843b3a8eb9SGleb Smirnoff typedef void		pfsync_clear_states_t(u_int32_t, const char *);
1185211cddf9SKristof Provost typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *);
1186fbbf436dSKristof Provost typedef void		pfsync_detach_ifnet_t(struct ifnet *);
11873b3a8eb9SGleb Smirnoff 
11885f6cf24eSKristof Provost VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
11895f6cf24eSKristof Provost #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
11905f6cf24eSKristof Provost VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
11915f6cf24eSKristof Provost #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
11925f6cf24eSKristof Provost VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
11935f6cf24eSKristof Provost #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
11945f6cf24eSKristof Provost VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
11955f6cf24eSKristof Provost #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
11965f6cf24eSKristof Provost VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
11975f6cf24eSKristof Provost #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
11985f6cf24eSKristof Provost VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
11995f6cf24eSKristof Provost #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
1200fbbf436dSKristof Provost extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
12013b3a8eb9SGleb Smirnoff 
12024bf98559SKajetan Staszkiewicz void			pfsync_state_export(union pfsync_state_union *,
12034bf98559SKajetan Staszkiewicz 			    struct pf_kstate *, int);
1204c6bf20a2SKristof Provost void			pf_state_export(struct pf_state_export *,
1205c6bf20a2SKristof Provost 			    struct pf_kstate *);
12063b3a8eb9SGleb Smirnoff 
12073b3a8eb9SGleb Smirnoff /* pflog */
1208e86bddeaSKristof Provost struct pf_kruleset;
12093b3a8eb9SGleb Smirnoff struct pf_pdesc;
1210320c1116SKristof Provost typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
1211f2064dd1SKajetan Staszkiewicz     u_int8_t, struct pf_krule *, struct pf_krule *, struct pf_kruleset *,
1212f2064dd1SKajetan Staszkiewicz     struct pf_pdesc *, int);
12133b3a8eb9SGleb Smirnoff extern pflog_packet_t		*pflog_packet_ptr;
12143b3a8eb9SGleb Smirnoff 
12153b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
12163b3a8eb9SGleb Smirnoff 
12173b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_SRCNODE	0x04
12183b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_NATSRCNODE	0x08
12193b3a8eb9SGleb Smirnoff 
12203b3a8eb9SGleb Smirnoff /* for copies to/from network byte order */
12213b3a8eb9SGleb Smirnoff /* ioctl interface also uses network byte order */
12223b3a8eb9SGleb Smirnoff #define pf_state_peer_hton(s,d) do {		\
12233b3a8eb9SGleb Smirnoff 	(d)->seqlo = htonl((s)->seqlo);		\
12243b3a8eb9SGleb Smirnoff 	(d)->seqhi = htonl((s)->seqhi);		\
12253b3a8eb9SGleb Smirnoff 	(d)->seqdiff = htonl((s)->seqdiff);	\
12263b3a8eb9SGleb Smirnoff 	(d)->max_win = htons((s)->max_win);	\
12273b3a8eb9SGleb Smirnoff 	(d)->mss = htons((s)->mss);		\
12283b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
12293b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
12303b3a8eb9SGleb Smirnoff 	if ((s)->scrub) {						\
12313b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_flags = 				\
12323b3a8eb9SGleb Smirnoff 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
12333b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
12343b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
12353b3a8eb9SGleb Smirnoff 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
12363b3a8eb9SGleb Smirnoff 	}								\
12373b3a8eb9SGleb Smirnoff } while (0)
12383b3a8eb9SGleb Smirnoff 
12393b3a8eb9SGleb Smirnoff #define pf_state_peer_ntoh(s,d) do {		\
12403b3a8eb9SGleb Smirnoff 	(d)->seqlo = ntohl((s)->seqlo);		\
12413b3a8eb9SGleb Smirnoff 	(d)->seqhi = ntohl((s)->seqhi);		\
12423b3a8eb9SGleb Smirnoff 	(d)->seqdiff = ntohl((s)->seqdiff);	\
12433b3a8eb9SGleb Smirnoff 	(d)->max_win = ntohs((s)->max_win);	\
12443b3a8eb9SGleb Smirnoff 	(d)->mss = ntohs((s)->mss);		\
12453b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
12463b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
12473b3a8eb9SGleb Smirnoff 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
12483b3a8eb9SGleb Smirnoff 	    (d)->scrub != NULL) {					\
12493b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_flags =				\
12503b3a8eb9SGleb Smirnoff 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
12513b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
12523b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
12533b3a8eb9SGleb Smirnoff 	}								\
12543b3a8eb9SGleb Smirnoff } while (0)
12553b3a8eb9SGleb Smirnoff 
12563b3a8eb9SGleb Smirnoff #define pf_state_counter_hton(s,d) do {				\
12573b3a8eb9SGleb Smirnoff 	d[0] = htonl((s>>32)&0xffffffff);			\
12583b3a8eb9SGleb Smirnoff 	d[1] = htonl(s&0xffffffff);				\
12593b3a8eb9SGleb Smirnoff } while (0)
12603b3a8eb9SGleb Smirnoff 
12613b3a8eb9SGleb Smirnoff #define pf_state_counter_from_pfsync(s)				\
12623b3a8eb9SGleb Smirnoff 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
12633b3a8eb9SGleb Smirnoff 
12643b3a8eb9SGleb Smirnoff #define pf_state_counter_ntoh(s,d) do {				\
12653b3a8eb9SGleb Smirnoff 	d = ntohl(s[0]);					\
12663b3a8eb9SGleb Smirnoff 	d = d<<32;						\
12673b3a8eb9SGleb Smirnoff 	d += ntohl(s[1]);					\
12683b3a8eb9SGleb Smirnoff } while (0)
12693b3a8eb9SGleb Smirnoff 
1270e86bddeaSKristof Provost TAILQ_HEAD(pf_krulequeue, pf_krule);
12713b3a8eb9SGleb Smirnoff 
1272e86bddeaSKristof Provost struct pf_kanchor;
12733b3a8eb9SGleb Smirnoff 
1274e86bddeaSKristof Provost struct pf_kruleset {
12753b3a8eb9SGleb Smirnoff 	struct {
1276e86bddeaSKristof Provost 		struct pf_krulequeue	 queues[2];
12773b3a8eb9SGleb Smirnoff 		struct {
1278e86bddeaSKristof Provost 			struct pf_krulequeue	*ptr;
1279e86bddeaSKristof Provost 			struct pf_krule		**ptr_array;
12803b3a8eb9SGleb Smirnoff 			u_int32_t		 rcount;
12813b3a8eb9SGleb Smirnoff 			u_int32_t		 ticket;
12823b3a8eb9SGleb Smirnoff 			int			 open;
1283bd7762c8SMateusz Guzik 			struct pf_krule_global 	 *tree;
12843b3a8eb9SGleb Smirnoff 		}			 active, inactive;
12853b3a8eb9SGleb Smirnoff 	}			 rules[PF_RULESET_MAX];
1286e86bddeaSKristof Provost 	struct pf_kanchor	*anchor;
12873b3a8eb9SGleb Smirnoff 	u_int32_t		 tticket;
12883b3a8eb9SGleb Smirnoff 	int			 tables;
12893b3a8eb9SGleb Smirnoff 	int			 topen;
12903b3a8eb9SGleb Smirnoff };
12913b3a8eb9SGleb Smirnoff 
1292e86bddeaSKristof Provost RB_HEAD(pf_kanchor_global, pf_kanchor);
1293e86bddeaSKristof Provost RB_HEAD(pf_kanchor_node, pf_kanchor);
1294e86bddeaSKristof Provost struct pf_kanchor {
1295e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_global;
1296e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_node;
1297e86bddeaSKristof Provost 	struct pf_kanchor	*parent;
1298e86bddeaSKristof Provost 	struct pf_kanchor_node	 children;
12993b3a8eb9SGleb Smirnoff 	char			 name[PF_ANCHOR_NAME_SIZE];
13003b3a8eb9SGleb Smirnoff 	char			 path[MAXPATHLEN];
1301e86bddeaSKristof Provost 	struct pf_kruleset	 ruleset;
13023b3a8eb9SGleb Smirnoff 	int			 refcnt;	/* anchor rules */
13033b3a8eb9SGleb Smirnoff };
1304e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
1305e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
13063b3a8eb9SGleb Smirnoff 
13073b3a8eb9SGleb Smirnoff #define PF_RESERVED_ANCHOR	"_pf"
13083b3a8eb9SGleb Smirnoff 
13093b3a8eb9SGleb Smirnoff #define PFR_TFLAG_PERSIST	0x00000001
13103b3a8eb9SGleb Smirnoff #define PFR_TFLAG_CONST		0x00000002
13113b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ACTIVE	0x00000004
13123b3a8eb9SGleb Smirnoff #define PFR_TFLAG_INACTIVE	0x00000008
13133b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFERENCED	0x00000010
13143b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFDANCHOR	0x00000020
13153b3a8eb9SGleb Smirnoff #define PFR_TFLAG_COUNTERS	0x00000040
13163b3a8eb9SGleb Smirnoff /* Adjust masks below when adding flags. */
13173b3a8eb9SGleb Smirnoff #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
13183b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
13193b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
13203b3a8eb9SGleb Smirnoff #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
13213b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
13223b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
13233b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR)
13243b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
13253b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
13263b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_ACTIVE	| \
13273b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
13283b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
13293b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR	| \
13303b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
13313b3a8eb9SGleb Smirnoff 
1332e86bddeaSKristof Provost struct pf_kanchor_stackframe;
1333c5131afeSKristof Provost struct pf_keth_anchor_stackframe;
13341d6139c0SGleb Smirnoff 
13353b3a8eb9SGleb Smirnoff struct pfr_table {
13363b3a8eb9SGleb Smirnoff 	char			 pfrt_anchor[MAXPATHLEN];
13373b3a8eb9SGleb Smirnoff 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
13383b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrt_flags;
13393b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrt_fback;
13403b3a8eb9SGleb Smirnoff };
13413b3a8eb9SGleb Smirnoff 
13423b3a8eb9SGleb Smirnoff enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
13433b3a8eb9SGleb Smirnoff 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
13443b3a8eb9SGleb Smirnoff 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
13453b3a8eb9SGleb Smirnoff 
13463b3a8eb9SGleb Smirnoff struct pfr_addr {
13473b3a8eb9SGleb Smirnoff 	union {
13483b3a8eb9SGleb Smirnoff 		struct in_addr	 _pfra_ip4addr;
13493b3a8eb9SGleb Smirnoff 		struct in6_addr	 _pfra_ip6addr;
13503b3a8eb9SGleb Smirnoff 	}		 pfra_u;
13513b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_af;
13523b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_net;
13533b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_not;
13543b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_fback;
13553b3a8eb9SGleb Smirnoff };
13563b3a8eb9SGleb Smirnoff #define	pfra_ip4addr	pfra_u._pfra_ip4addr
13573b3a8eb9SGleb Smirnoff #define	pfra_ip6addr	pfra_u._pfra_ip6addr
13583b3a8eb9SGleb Smirnoff 
13593b3a8eb9SGleb Smirnoff enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
13603b3a8eb9SGleb Smirnoff enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1361c1be8399SMark Johnston enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
1362c1be8399SMark Johnston #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
13633b3a8eb9SGleb Smirnoff #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
13643b3a8eb9SGleb Smirnoff 
13653b3a8eb9SGleb Smirnoff struct pfr_astats {
13663b3a8eb9SGleb Smirnoff 	struct pfr_addr	 pfras_a;
13673b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
13683b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
13693b3a8eb9SGleb Smirnoff 	long		 pfras_tzero;
13703b3a8eb9SGleb Smirnoff };
13713b3a8eb9SGleb Smirnoff 
13723b3a8eb9SGleb Smirnoff enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
13733b3a8eb9SGleb Smirnoff 
13743b3a8eb9SGleb Smirnoff struct pfr_tstats {
13753b3a8eb9SGleb Smirnoff 	struct pfr_table pfrts_t;
13763b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
13773b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
13783b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_match;
13793b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_nomatch;
13803b3a8eb9SGleb Smirnoff 	long		 pfrts_tzero;
13813b3a8eb9SGleb Smirnoff 	int		 pfrts_cnt;
13823b3a8eb9SGleb Smirnoff 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
13833b3a8eb9SGleb Smirnoff };
138459048686SKristof Provost 
1385f92c21a2SMateusz Guzik #ifdef _KERNEL
1386f92c21a2SMateusz Guzik 
1387f92c21a2SMateusz Guzik struct pfr_kstate_counter {
1388f92c21a2SMateusz Guzik 	counter_u64_t	pkc_pcpu;
1389f92c21a2SMateusz Guzik 	u_int64_t	pkc_zero;
1390f92c21a2SMateusz Guzik };
1391f92c21a2SMateusz Guzik 
1392f92c21a2SMateusz Guzik static inline int
1393f92c21a2SMateusz Guzik pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
1394f92c21a2SMateusz Guzik {
1395f92c21a2SMateusz Guzik 
1396f92c21a2SMateusz Guzik 	pfrc->pkc_zero = 0;
1397f92c21a2SMateusz Guzik 	pfrc->pkc_pcpu = counter_u64_alloc(flags);
1398f92c21a2SMateusz Guzik 	if (pfrc->pkc_pcpu == NULL)
1399f92c21a2SMateusz Guzik 		return (ENOMEM);
1400f92c21a2SMateusz Guzik 	return (0);
1401f92c21a2SMateusz Guzik }
1402f92c21a2SMateusz Guzik 
1403f92c21a2SMateusz Guzik static inline void
1404f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
1405f92c21a2SMateusz Guzik {
1406f92c21a2SMateusz Guzik 
1407f92c21a2SMateusz Guzik 	counter_u64_free(pfrc->pkc_pcpu);
1408f92c21a2SMateusz Guzik }
1409f92c21a2SMateusz Guzik 
1410f92c21a2SMateusz Guzik static inline u_int64_t
1411f92c21a2SMateusz Guzik pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
1412f92c21a2SMateusz Guzik {
1413f92c21a2SMateusz Guzik 	u_int64_t c;
1414f92c21a2SMateusz Guzik 
1415f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1416f92c21a2SMateusz Guzik 	c -= pfrc->pkc_zero;
1417f92c21a2SMateusz Guzik 	return (c);
1418f92c21a2SMateusz Guzik }
1419f92c21a2SMateusz Guzik 
1420f92c21a2SMateusz Guzik static inline void
1421f92c21a2SMateusz Guzik pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
1422f92c21a2SMateusz Guzik {
1423f92c21a2SMateusz Guzik 	u_int64_t c;
1424f92c21a2SMateusz Guzik 
1425f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1426f92c21a2SMateusz Guzik 	pfrc->pkc_zero = c;
1427f92c21a2SMateusz Guzik }
1428f92c21a2SMateusz Guzik 
1429f92c21a2SMateusz Guzik static inline void
1430f92c21a2SMateusz Guzik pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
1431f92c21a2SMateusz Guzik {
1432f92c21a2SMateusz Guzik 
1433f92c21a2SMateusz Guzik 	counter_u64_add(pfrc->pkc_pcpu, n);
1434f92c21a2SMateusz Guzik }
1435f92c21a2SMateusz Guzik 
143659048686SKristof Provost struct pfr_ktstats {
143759048686SKristof Provost 	struct pfr_table pfrts_t;
1438f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1439f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1440f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_match;
1441f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_nomatch;
144259048686SKristof Provost 	long		 pfrkts_tzero;
144359048686SKristof Provost 	int		 pfrkts_cnt;
144459048686SKristof Provost 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
144559048686SKristof Provost };
1446f92c21a2SMateusz Guzik 
1447f92c21a2SMateusz Guzik #endif /* _KERNEL */
1448f92c21a2SMateusz Guzik 
14493b3a8eb9SGleb Smirnoff #define	pfrts_name	pfrts_t.pfrt_name
14503b3a8eb9SGleb Smirnoff #define pfrts_flags	pfrts_t.pfrt_flags
14513b3a8eb9SGleb Smirnoff 
14523b3a8eb9SGleb Smirnoff #ifndef _SOCKADDR_UNION_DEFINED
14533b3a8eb9SGleb Smirnoff #define	_SOCKADDR_UNION_DEFINED
14543b3a8eb9SGleb Smirnoff union sockaddr_union {
14553b3a8eb9SGleb Smirnoff 	struct sockaddr		sa;
14563b3a8eb9SGleb Smirnoff 	struct sockaddr_in	sin;
14573b3a8eb9SGleb Smirnoff 	struct sockaddr_in6	sin6;
14583b3a8eb9SGleb Smirnoff };
14593b3a8eb9SGleb Smirnoff #endif /* _SOCKADDR_UNION_DEFINED */
14603b3a8eb9SGleb Smirnoff 
14613b3a8eb9SGleb Smirnoff struct pfr_kcounters {
1462c1be8399SMark Johnston 	counter_u64_t		 pfrkc_counters;
146359048686SKristof Provost 	long			 pfrkc_tzero;
14643b3a8eb9SGleb Smirnoff };
1465c1be8399SMark Johnston #define	pfr_kentry_counter(kc, dir, op, t)		\
1466c1be8399SMark Johnston 	((kc)->pfrkc_counters +				\
1467c1be8399SMark Johnston 	    (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
14683b3a8eb9SGleb Smirnoff 
1469e86bddeaSKristof Provost #ifdef _KERNEL
14703b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
14713b3a8eb9SGleb Smirnoff struct pfr_kentry {
14723b3a8eb9SGleb Smirnoff 	struct radix_node	 pfrke_node[2];
14733b3a8eb9SGleb Smirnoff 	union sockaddr_union	 pfrke_sa;
14743b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
147559048686SKristof Provost 	struct pfr_kcounters	 pfrke_counters;
14763b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_af;
14773b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_net;
14783b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_not;
14793b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_mark;
14803b3a8eb9SGleb Smirnoff };
14813b3a8eb9SGleb Smirnoff 
14823b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
14833b3a8eb9SGleb Smirnoff RB_HEAD(pfr_ktablehead, pfr_ktable);
14843b3a8eb9SGleb Smirnoff struct pfr_ktable {
148559048686SKristof Provost 	struct pfr_ktstats	 pfrkt_kts;
14863b3a8eb9SGleb Smirnoff 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
14873b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
14883b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip4;
14893b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip6;
14903b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_shadow;
14913b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_root;
1492e86bddeaSKristof Provost 	struct pf_kruleset	*pfrkt_rs;
14933b3a8eb9SGleb Smirnoff 	long			 pfrkt_larg;
14943b3a8eb9SGleb Smirnoff 	int			 pfrkt_nflags;
14953b3a8eb9SGleb Smirnoff };
149659048686SKristof Provost #define pfrkt_t		pfrkt_kts.pfrts_t
14973b3a8eb9SGleb Smirnoff #define pfrkt_name	pfrkt_t.pfrt_name
14983b3a8eb9SGleb Smirnoff #define pfrkt_anchor	pfrkt_t.pfrt_anchor
14993b3a8eb9SGleb Smirnoff #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
15003b3a8eb9SGleb Smirnoff #define pfrkt_flags	pfrkt_t.pfrt_flags
150159048686SKristof Provost #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
150259048686SKristof Provost #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
150359048686SKristof Provost #define pfrkt_packets	pfrkt_kts.pfrkts_packets
150459048686SKristof Provost #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
150559048686SKristof Provost #define pfrkt_match	pfrkt_kts.pfrkts_match
150659048686SKristof Provost #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
150759048686SKristof Provost #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1508e86bddeaSKristof Provost #endif
15093b3a8eb9SGleb Smirnoff 
1510320c1116SKristof Provost #ifdef _KERNEL
1511320c1116SKristof Provost struct pfi_kkif {
15123b3a8eb9SGleb Smirnoff 	char				 pfik_name[IFNAMSIZ];
15133b3a8eb9SGleb Smirnoff 	union {
1514320c1116SKristof Provost 		RB_ENTRY(pfi_kkif)	 _pfik_tree;
1515320c1116SKristof Provost 		LIST_ENTRY(pfi_kkif)	 _pfik_list;
15163b3a8eb9SGleb Smirnoff 	} _pfik_glue;
15173b3a8eb9SGleb Smirnoff #define	pfik_tree	_pfik_glue._pfik_tree
15183b3a8eb9SGleb Smirnoff #define	pfik_list	_pfik_glue._pfik_list
1519d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_packets[2][2][2];
1520d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_bytes[2][2][2];
15213b3a8eb9SGleb Smirnoff 	u_int32_t			 pfik_tzero;
15223b3a8eb9SGleb Smirnoff 	u_int				 pfik_flags;
15233b3a8eb9SGleb Smirnoff 	struct ifnet			*pfik_ifp;
15243b3a8eb9SGleb Smirnoff 	struct ifg_group		*pfik_group;
15253b3a8eb9SGleb Smirnoff 	u_int				 pfik_rulerefs;
15263b3a8eb9SGleb Smirnoff 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1527d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
1528d40d4b3eSMateusz Guzik 	LIST_ENTRY(pfi_kkif)		 pfik_allkiflist;
1529d40d4b3eSMateusz Guzik #endif
15303b3a8eb9SGleb Smirnoff };
1531320c1116SKristof Provost #endif
15323b3a8eb9SGleb Smirnoff 
15333b3a8eb9SGleb Smirnoff #define	PFI_IFLAG_REFS		0x0001	/* has state references */
15343b3a8eb9SGleb Smirnoff #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
15353b3a8eb9SGleb Smirnoff 
153629698ed9SKristof Provost #ifdef _KERNEL
153710aa9ddbSKristof Provost struct pf_sctp_multihome_job;
153810aa9ddbSKristof Provost TAILQ_HEAD(pf_sctp_multihome_jobs, pf_sctp_multihome_job);
153910aa9ddbSKristof Provost 
15403b3a8eb9SGleb Smirnoff struct pf_pdesc {
15413b3a8eb9SGleb Smirnoff 	struct {
15423b3a8eb9SGleb Smirnoff 		int	 done;
15433b3a8eb9SGleb Smirnoff 		uid_t	 uid;
15443b3a8eb9SGleb Smirnoff 		gid_t	 gid;
15453b3a8eb9SGleb Smirnoff 	}		 lookup;
15463b3a8eb9SGleb Smirnoff 	u_int64_t	 tot_len;	/* Make Mickey money */
1547d38630f6SKristof Provost 	union pf_headers {
1548d38630f6SKristof Provost 		struct tcphdr		tcp;
1549d38630f6SKristof Provost 		struct udphdr		udp;
1550010ee43fSKristof Provost 		struct sctphdr		sctp;
1551d38630f6SKristof Provost 		struct icmp		icmp;
15523b3a8eb9SGleb Smirnoff #ifdef INET6
1553d38630f6SKristof Provost 		struct icmp6_hdr	icmp6;
15543b3a8eb9SGleb Smirnoff #endif /* INET6 */
1555d38630f6SKristof Provost 		char any[0];
15563b3a8eb9SGleb Smirnoff 	} hdr;
15573b3a8eb9SGleb Smirnoff 
1558e86bddeaSKristof Provost 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
15593b3a8eb9SGleb Smirnoff 	struct pf_addr	*src;		/* src address */
15603b3a8eb9SGleb Smirnoff 	struct pf_addr	*dst;		/* dst address */
15613b3a8eb9SGleb Smirnoff 	u_int16_t *sport;
15623b3a8eb9SGleb Smirnoff 	u_int16_t *dport;
15633b3a8eb9SGleb Smirnoff 	struct pf_mtag	*pf_mtag;
1564ef950daaSKristof Provost 	struct pf_rule_actions	act;
15653b3a8eb9SGleb Smirnoff 
15663b3a8eb9SGleb Smirnoff 	u_int32_t	 p_len;		/* total length of payload */
15673b3a8eb9SGleb Smirnoff 
15683b3a8eb9SGleb Smirnoff 	u_int16_t	*ip_sum;
15693b3a8eb9SGleb Smirnoff 	u_int16_t	*proto_sum;
15703b3a8eb9SGleb Smirnoff 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
15713b3a8eb9SGleb Smirnoff 					 * state code. Easier than tags */
15723b3a8eb9SGleb Smirnoff #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
15733b3a8eb9SGleb Smirnoff #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
15743b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
15753b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
15763b3a8eb9SGleb Smirnoff 	u_int8_t	 tos;
15773b3a8eb9SGleb Smirnoff 	u_int8_t	 dir;		/* direction */
15783b3a8eb9SGleb Smirnoff 	u_int8_t	 sidx;		/* key index for source */
15793b3a8eb9SGleb Smirnoff 	u_int8_t	 didx;		/* key index for destination */
1580010ee43fSKristof Provost #define PFDESC_SCTP_INIT	0x0001
1581010ee43fSKristof Provost #define PFDESC_SCTP_INIT_ACK	0x0002
1582010ee43fSKristof Provost #define PFDESC_SCTP_COOKIE	0x0004
1583010ee43fSKristof Provost #define PFDESC_SCTP_ABORT	0x0008
1584010ee43fSKristof Provost #define PFDESC_SCTP_SHUTDOWN	0x0010
1585010ee43fSKristof Provost #define PFDESC_SCTP_SHUTDOWN_COMPLETE	0x0020
1586010ee43fSKristof Provost #define PFDESC_SCTP_DATA	0x0040
158710aa9ddbSKristof Provost #define PFDESC_SCTP_ASCONF	0x0080
158810aa9ddbSKristof Provost #define PFDESC_SCTP_OTHER	0x0100
158951a78dd2SKristof Provost #define PFDESC_SCTP_ADD_IP	0x0200
1590010ee43fSKristof Provost 	u_int16_t	 sctp_flags;
1591d1bc1e9eSKristof Provost 	u_int32_t	 sctp_initiate_tag;
159210aa9ddbSKristof Provost 
159310aa9ddbSKristof Provost 	struct pf_sctp_multihome_jobs	sctp_multihome_jobs;
15943b3a8eb9SGleb Smirnoff };
159510aa9ddbSKristof Provost 
159610aa9ddbSKristof Provost struct pf_sctp_multihome_job {
159710aa9ddbSKristof Provost 	TAILQ_ENTRY(pf_sctp_multihome_job)	next;
159810aa9ddbSKristof Provost 	struct pf_pdesc				 pd;
159910aa9ddbSKristof Provost 	struct pf_addr				 src;
160010aa9ddbSKristof Provost 	struct pf_addr				 dst;
160110aa9ddbSKristof Provost 	struct mbuf				*m;
160210aa9ddbSKristof Provost };
160310aa9ddbSKristof Provost 
160429698ed9SKristof Provost #endif
16053b3a8eb9SGleb Smirnoff 
16063b3a8eb9SGleb Smirnoff /* flags for RDR options */
16073b3a8eb9SGleb Smirnoff #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
16083b3a8eb9SGleb Smirnoff #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
16093b3a8eb9SGleb Smirnoff 
16103b3a8eb9SGleb Smirnoff /* UDP state enumeration */
16113b3a8eb9SGleb Smirnoff #define PFUDPS_NO_TRAFFIC	0
16123b3a8eb9SGleb Smirnoff #define PFUDPS_SINGLE		1
16133b3a8eb9SGleb Smirnoff #define PFUDPS_MULTIPLE		2
16143b3a8eb9SGleb Smirnoff 
16153b3a8eb9SGleb Smirnoff #define PFUDPS_NSTATES		3	/* number of state levels */
16163b3a8eb9SGleb Smirnoff 
16173b3a8eb9SGleb Smirnoff #define PFUDPS_NAMES { \
16183b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
16193b3a8eb9SGleb Smirnoff 	"SINGLE", \
16203b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
16213b3a8eb9SGleb Smirnoff 	NULL \
16223b3a8eb9SGleb Smirnoff }
16233b3a8eb9SGleb Smirnoff 
16243b3a8eb9SGleb Smirnoff /* Other protocol state enumeration */
16253b3a8eb9SGleb Smirnoff #define PFOTHERS_NO_TRAFFIC	0
16263b3a8eb9SGleb Smirnoff #define PFOTHERS_SINGLE		1
16273b3a8eb9SGleb Smirnoff #define PFOTHERS_MULTIPLE	2
16283b3a8eb9SGleb Smirnoff 
16293b3a8eb9SGleb Smirnoff #define PFOTHERS_NSTATES	3	/* number of state levels */
16303b3a8eb9SGleb Smirnoff 
16313b3a8eb9SGleb Smirnoff #define PFOTHERS_NAMES { \
16323b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
16333b3a8eb9SGleb Smirnoff 	"SINGLE", \
16343b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
16353b3a8eb9SGleb Smirnoff 	NULL \
16363b3a8eb9SGleb Smirnoff }
16373b3a8eb9SGleb Smirnoff 
16383b3a8eb9SGleb Smirnoff #define ACTION_SET(a, x) \
16393b3a8eb9SGleb Smirnoff 	do { \
16403b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
16413b3a8eb9SGleb Smirnoff 			*(a) = (x); \
16423b3a8eb9SGleb Smirnoff 	} while (0)
16433b3a8eb9SGleb Smirnoff 
16443b3a8eb9SGleb Smirnoff #define REASON_SET(a, x) \
16453b3a8eb9SGleb Smirnoff 	do { \
16463b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
16473b3a8eb9SGleb Smirnoff 			*(a) = (x); \
16483b3a8eb9SGleb Smirnoff 		if (x < PFRES_MAX) \
1649a9572d8fSGleb Smirnoff 			counter_u64_add(V_pf_status.counters[x], 1); \
16503b3a8eb9SGleb Smirnoff 	} while (0)
16513b3a8eb9SGleb Smirnoff 
16528e1864edSKristof Provost enum pf_syncookies_mode {
16538e1864edSKristof Provost 	PF_SYNCOOKIES_NEVER = 0,
16548e1864edSKristof Provost 	PF_SYNCOOKIES_ALWAYS = 1,
1655bf863718SKristof Provost 	PF_SYNCOOKIES_ADAPTIVE = 2,
1656bf863718SKristof Provost 	PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE
16578e1864edSKristof Provost };
16588e1864edSKristof Provost 
16595062afffSKristof Provost #define	PF_SYNCOOKIES_HIWATPCT	25
16605062afffSKristof Provost #define	PF_SYNCOOKIES_LOWATPCT	(PF_SYNCOOKIES_HIWATPCT / 2)
16615062afffSKristof Provost 
1662d9cc6ea2SMateusz Guzik #ifdef _KERNEL
1663a9572d8fSGleb Smirnoff struct pf_kstatus {
1664a9572d8fSGleb Smirnoff 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
16654cab80a8SKristof Provost 	counter_u64_t	lcounters[KLCNT_MAX]; /* limit counters */
1666fc4c42ceSMateusz Guzik 	struct pf_counter_u64	fcounters[FCNT_MAX]; /* state operation counters */
1667a9572d8fSGleb Smirnoff 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1668a9572d8fSGleb Smirnoff 	uint32_t	states;
1669a9572d8fSGleb Smirnoff 	uint32_t	src_nodes;
1670a9572d8fSGleb Smirnoff 	uint32_t	running;
1671a9572d8fSGleb Smirnoff 	uint32_t	since;
1672a9572d8fSGleb Smirnoff 	uint32_t	debug;
1673a9572d8fSGleb Smirnoff 	uint32_t	hostid;
16743b3a8eb9SGleb Smirnoff 	char		ifname[IFNAMSIZ];
1675a9572d8fSGleb Smirnoff 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
167642ec75f8SKristof Provost 	bool		keep_counters;
16778e1864edSKristof Provost 	enum pf_syncookies_mode	syncookies_mode;
16788e1864edSKristof Provost 	bool		syncookies_active;
1679bf863718SKristof Provost 	uint64_t	syncookies_inflight[2];
1680bf863718SKristof Provost 	uint32_t	states_halfopen;
168139282ef3SKajetan Staszkiewicz 	uint32_t	reass;
16823b3a8eb9SGleb Smirnoff };
1683d9cc6ea2SMateusz Guzik #endif
16843b3a8eb9SGleb Smirnoff 
16853b3a8eb9SGleb Smirnoff struct pf_divert {
16863b3a8eb9SGleb Smirnoff 	union {
16873b3a8eb9SGleb Smirnoff 		struct in_addr	ipv4;
16883b3a8eb9SGleb Smirnoff 		struct in6_addr	ipv6;
16893b3a8eb9SGleb Smirnoff 	}		addr;
16903b3a8eb9SGleb Smirnoff 	u_int16_t	port;
16913b3a8eb9SGleb Smirnoff };
16923b3a8eb9SGleb Smirnoff 
16933b3a8eb9SGleb Smirnoff #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
16943b3a8eb9SGleb Smirnoff #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
16953b3a8eb9SGleb Smirnoff 
16963b3a8eb9SGleb Smirnoff /*
1697fd2ea405SKristof Provost  * Limit the length of the fragment queue traversal.  Remember
1698fd2ea405SKristof Provost  * search entry points based on the fragment offset.
1699fd2ea405SKristof Provost  */
1700fd2ea405SKristof Provost #define PF_FRAG_ENTRY_POINTS		16
1701fd2ea405SKristof Provost 
1702fd2ea405SKristof Provost /*
1703790194cdSKristof Provost  * The number of entries in the fragment queue must be limited
1704ef88adc5SGordon Bergling  * to avoid DoS by linear searching.  Instead of a global limit,
1705790194cdSKristof Provost  * use a limit per entry point.  For large packets these sum up.
1706790194cdSKristof Provost  */
1707790194cdSKristof Provost #define PF_FRAG_ENTRY_LIMIT		64
1708790194cdSKristof Provost 
1709790194cdSKristof Provost /*
17103b3a8eb9SGleb Smirnoff  * ioctl parameter structures
17113b3a8eb9SGleb Smirnoff  */
17123b3a8eb9SGleb Smirnoff 
17133b3a8eb9SGleb Smirnoff struct pfioc_pooladdr {
17143b3a8eb9SGleb Smirnoff 	u_int32_t		 action;
17153b3a8eb9SGleb Smirnoff 	u_int32_t		 ticket;
17163b3a8eb9SGleb Smirnoff 	u_int32_t		 nr;
17173b3a8eb9SGleb Smirnoff 	u_int32_t		 r_num;
17183b3a8eb9SGleb Smirnoff 	u_int8_t		 r_action;
17193b3a8eb9SGleb Smirnoff 	u_int8_t		 r_last;
17203b3a8eb9SGleb Smirnoff 	u_int8_t		 af;
17213b3a8eb9SGleb Smirnoff 	char			 anchor[MAXPATHLEN];
17223b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	 addr;
17233b3a8eb9SGleb Smirnoff };
17243b3a8eb9SGleb Smirnoff 
17253b3a8eb9SGleb Smirnoff struct pfioc_rule {
17263b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
17273b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
17283b3a8eb9SGleb Smirnoff 	u_int32_t	 pool_ticket;
17293b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
17303b3a8eb9SGleb Smirnoff 	char		 anchor[MAXPATHLEN];
17313b3a8eb9SGleb Smirnoff 	char		 anchor_call[MAXPATHLEN];
17323b3a8eb9SGleb Smirnoff 	struct pf_rule	 rule;
17333b3a8eb9SGleb Smirnoff };
17343b3a8eb9SGleb Smirnoff 
17353b3a8eb9SGleb Smirnoff struct pfioc_natlook {
17363b3a8eb9SGleb Smirnoff 	struct pf_addr	 saddr;
17373b3a8eb9SGleb Smirnoff 	struct pf_addr	 daddr;
17383b3a8eb9SGleb Smirnoff 	struct pf_addr	 rsaddr;
17393b3a8eb9SGleb Smirnoff 	struct pf_addr	 rdaddr;
17403b3a8eb9SGleb Smirnoff 	u_int16_t	 sport;
17413b3a8eb9SGleb Smirnoff 	u_int16_t	 dport;
17423b3a8eb9SGleb Smirnoff 	u_int16_t	 rsport;
17433b3a8eb9SGleb Smirnoff 	u_int16_t	 rdport;
17443b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
17453b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
17463b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
17473b3a8eb9SGleb Smirnoff };
17483b3a8eb9SGleb Smirnoff 
17493b3a8eb9SGleb Smirnoff struct pfioc_state {
17504bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301	state;
17513b3a8eb9SGleb Smirnoff };
17523b3a8eb9SGleb Smirnoff 
17533b3a8eb9SGleb Smirnoff struct pfioc_src_node_kill {
17543b3a8eb9SGleb Smirnoff 	sa_family_t psnk_af;
17553b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_src;
17563b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_dst;
17573b3a8eb9SGleb Smirnoff 	u_int		    psnk_killed;
17583b3a8eb9SGleb Smirnoff };
17593b3a8eb9SGleb Smirnoff 
17607606a45dSKristof Provost #ifdef _KERNEL
17617606a45dSKristof Provost struct pf_kstate_kill {
17627606a45dSKristof Provost 	struct pf_state_cmp	psk_pfcmp;
17637606a45dSKristof Provost 	sa_family_t		psk_af;
17647606a45dSKristof Provost 	int			psk_proto;
17657606a45dSKristof Provost 	struct pf_rule_addr	psk_src;
17667606a45dSKristof Provost 	struct pf_rule_addr	psk_dst;
1767abbcba9cSKristof Provost 	struct pf_rule_addr	psk_rt_addr;
17687606a45dSKristof Provost 	char			psk_ifname[IFNAMSIZ];
17697606a45dSKristof Provost 	char			psk_label[PF_RULE_LABEL_SIZE];
17707606a45dSKristof Provost 	u_int			psk_killed;
177193abcf17SKristof Provost 	bool			psk_kill_match;
17727606a45dSKristof Provost };
17737606a45dSKristof Provost #endif
17747606a45dSKristof Provost 
17753b3a8eb9SGleb Smirnoff struct pfioc_state_kill {
17763b3a8eb9SGleb Smirnoff 	struct pf_state_cmp	psk_pfcmp;
17773b3a8eb9SGleb Smirnoff 	sa_family_t		psk_af;
17783b3a8eb9SGleb Smirnoff 	int			psk_proto;
17793b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_src;
17803b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_dst;
17813b3a8eb9SGleb Smirnoff 	char			psk_ifname[IFNAMSIZ];
17823b3a8eb9SGleb Smirnoff 	char			psk_label[PF_RULE_LABEL_SIZE];
17833b3a8eb9SGleb Smirnoff 	u_int			psk_killed;
17843b3a8eb9SGleb Smirnoff };
17853b3a8eb9SGleb Smirnoff 
17863b3a8eb9SGleb Smirnoff struct pfioc_states {
17873b3a8eb9SGleb Smirnoff 	int	ps_len;
17883b3a8eb9SGleb Smirnoff 	union {
1789c4a32455SKristof Provost 		void				*ps_buf;
17904bf98559SKajetan Staszkiewicz 		struct pfsync_state_1301	*ps_states;
17917b676698SKristof Provost 	};
17923b3a8eb9SGleb Smirnoff };
17933b3a8eb9SGleb Smirnoff 
1794c6bf20a2SKristof Provost struct pfioc_states_v2 {
1795c6bf20a2SKristof Provost 	int		ps_len;
1796c6bf20a2SKristof Provost 	uint64_t	ps_req_version;
1797c6bf20a2SKristof Provost 	union {
1798c4a32455SKristof Provost 		void			*ps_buf;
17997b676698SKristof Provost 		struct pf_state_export	*ps_states;
18007b676698SKristof Provost 	};
1801c6bf20a2SKristof Provost };
1802c6bf20a2SKristof Provost 
18033b3a8eb9SGleb Smirnoff struct pfioc_src_nodes {
18043b3a8eb9SGleb Smirnoff 	int	psn_len;
18053b3a8eb9SGleb Smirnoff 	union {
1806c4a32455SKristof Provost 		void		*psn_buf;
18077b676698SKristof Provost 		struct pf_src_node	*psn_src_nodes;
18087b676698SKristof Provost 	};
18093b3a8eb9SGleb Smirnoff };
18103b3a8eb9SGleb Smirnoff 
18113b3a8eb9SGleb Smirnoff struct pfioc_if {
18123b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
18133b3a8eb9SGleb Smirnoff };
18143b3a8eb9SGleb Smirnoff 
18153b3a8eb9SGleb Smirnoff struct pfioc_tm {
18163b3a8eb9SGleb Smirnoff 	int		 timeout;
18173b3a8eb9SGleb Smirnoff 	int		 seconds;
18183b3a8eb9SGleb Smirnoff };
18193b3a8eb9SGleb Smirnoff 
18203b3a8eb9SGleb Smirnoff struct pfioc_limit {
18213b3a8eb9SGleb Smirnoff 	int		 index;
18223b3a8eb9SGleb Smirnoff 	unsigned	 limit;
18233b3a8eb9SGleb Smirnoff };
18243b3a8eb9SGleb Smirnoff 
1825249cc75fSPatrick Kelsey struct pfioc_altq_v0 {
18263b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
18273b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
18283b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
1829249cc75fSPatrick Kelsey 	struct pf_altq_v0 altq;
18303b3a8eb9SGleb Smirnoff };
18313b3a8eb9SGleb Smirnoff 
1832249cc75fSPatrick Kelsey struct pfioc_altq_v1 {
1833249cc75fSPatrick Kelsey 	u_int32_t	 action;
1834249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1835249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1836249cc75fSPatrick Kelsey 	/*
1837249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1838249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1839249cc75fSPatrick Kelsey 	 */
1840249cc75fSPatrick Kelsey 	u_int32_t	 version;
1841249cc75fSPatrick Kelsey 	struct pf_altq_v1 altq;
1842249cc75fSPatrick Kelsey };
1843249cc75fSPatrick Kelsey 
1844249cc75fSPatrick Kelsey /*
1845249cc75fSPatrick Kelsey  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1846249cc75fSPatrick Kelsey  * the latest version of struct pf_altq_vX as it has that struct as a
1847249cc75fSPatrick Kelsey  * member.
1848249cc75fSPatrick Kelsey  */
1849249cc75fSPatrick Kelsey #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1850249cc75fSPatrick Kelsey 
1851249cc75fSPatrick Kelsey struct pfioc_qstats_v0 {
18523b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
18533b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
18543b3a8eb9SGleb Smirnoff 	void		*buf;
18553b3a8eb9SGleb Smirnoff 	int		 nbytes;
18563b3a8eb9SGleb Smirnoff 	u_int8_t	 scheduler;
18573b3a8eb9SGleb Smirnoff };
18583b3a8eb9SGleb Smirnoff 
1859249cc75fSPatrick Kelsey struct pfioc_qstats_v1 {
1860249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1861249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1862249cc75fSPatrick Kelsey 	void		*buf;
1863249cc75fSPatrick Kelsey 	int		 nbytes;
1864249cc75fSPatrick Kelsey 	u_int8_t	 scheduler;
1865249cc75fSPatrick Kelsey 	/*
1866249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1867249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1868249cc75fSPatrick Kelsey 	 */
1869249cc75fSPatrick Kelsey 	u_int32_t	 version;  /* Requested version of stats struct */
1870249cc75fSPatrick Kelsey };
1871249cc75fSPatrick Kelsey 
1872249cc75fSPatrick Kelsey /* Latest version of struct pfioc_qstats_vX */
1873249cc75fSPatrick Kelsey #define PFIOC_QSTATS_VERSION	1
1874249cc75fSPatrick Kelsey 
18753b3a8eb9SGleb Smirnoff struct pfioc_ruleset {
18763b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
18773b3a8eb9SGleb Smirnoff 	char		 path[MAXPATHLEN];
18783b3a8eb9SGleb Smirnoff 	char		 name[PF_ANCHOR_NAME_SIZE];
18793b3a8eb9SGleb Smirnoff };
18803b3a8eb9SGleb Smirnoff 
18813b3a8eb9SGleb Smirnoff #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
18823b3a8eb9SGleb Smirnoff #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1883e732e742SKristof Provost #define PF_RULESET_ETH		(PF_RULESET_MAX+2)
18843b3a8eb9SGleb Smirnoff struct pfioc_trans {
18853b3a8eb9SGleb Smirnoff 	int		 size;	/* number of elements */
18863b3a8eb9SGleb Smirnoff 	int		 esize; /* size of each element in bytes */
18873b3a8eb9SGleb Smirnoff 	struct pfioc_trans_e {
18883b3a8eb9SGleb Smirnoff 		int		rs_num;
18893b3a8eb9SGleb Smirnoff 		char		anchor[MAXPATHLEN];
18903b3a8eb9SGleb Smirnoff 		u_int32_t	ticket;
18913b3a8eb9SGleb Smirnoff 	}		*array;
18923b3a8eb9SGleb Smirnoff };
18933b3a8eb9SGleb Smirnoff 
18943b3a8eb9SGleb Smirnoff #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
18953b3a8eb9SGleb Smirnoff #define PFR_FLAG_DUMMY		0x00000002
18963b3a8eb9SGleb Smirnoff #define PFR_FLAG_FEEDBACK	0x00000004
18973b3a8eb9SGleb Smirnoff #define PFR_FLAG_CLSTATS	0x00000008
18983b3a8eb9SGleb Smirnoff #define PFR_FLAG_ADDRSTOO	0x00000010
18993b3a8eb9SGleb Smirnoff #define PFR_FLAG_REPLACE	0x00000020
19003b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLRSETS	0x00000040
19013b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLMASK	0x0000007F
19023b3a8eb9SGleb Smirnoff #ifdef _KERNEL
19033b3a8eb9SGleb Smirnoff #define PFR_FLAG_USERIOCTL	0x10000000
19043b3a8eb9SGleb Smirnoff #endif
19053b3a8eb9SGleb Smirnoff 
19063b3a8eb9SGleb Smirnoff struct pfioc_table {
19073b3a8eb9SGleb Smirnoff 	struct pfr_table	 pfrio_table;
19083b3a8eb9SGleb Smirnoff 	void			*pfrio_buffer;
19093b3a8eb9SGleb Smirnoff 	int			 pfrio_esize;
19103b3a8eb9SGleb Smirnoff 	int			 pfrio_size;
19113b3a8eb9SGleb Smirnoff 	int			 pfrio_size2;
19123b3a8eb9SGleb Smirnoff 	int			 pfrio_nadd;
19133b3a8eb9SGleb Smirnoff 	int			 pfrio_ndel;
19143b3a8eb9SGleb Smirnoff 	int			 pfrio_nchange;
19153b3a8eb9SGleb Smirnoff 	int			 pfrio_flags;
19163b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrio_ticket;
19173b3a8eb9SGleb Smirnoff };
19183b3a8eb9SGleb Smirnoff #define	pfrio_exists	pfrio_nadd
19193b3a8eb9SGleb Smirnoff #define	pfrio_nzero	pfrio_nadd
19203b3a8eb9SGleb Smirnoff #define	pfrio_nmatch	pfrio_nadd
19213b3a8eb9SGleb Smirnoff #define pfrio_naddr	pfrio_size2
19223b3a8eb9SGleb Smirnoff #define pfrio_setflag	pfrio_size2
19233b3a8eb9SGleb Smirnoff #define pfrio_clrflag	pfrio_nadd
19243b3a8eb9SGleb Smirnoff 
19253b3a8eb9SGleb Smirnoff struct pfioc_iface {
19263b3a8eb9SGleb Smirnoff 	char	 pfiio_name[IFNAMSIZ];
19273b3a8eb9SGleb Smirnoff 	void	*pfiio_buffer;
19283b3a8eb9SGleb Smirnoff 	int	 pfiio_esize;
19293b3a8eb9SGleb Smirnoff 	int	 pfiio_size;
19303b3a8eb9SGleb Smirnoff 	int	 pfiio_nzero;
19313b3a8eb9SGleb Smirnoff 	int	 pfiio_flags;
19323b3a8eb9SGleb Smirnoff };
19333b3a8eb9SGleb Smirnoff 
19343b3a8eb9SGleb Smirnoff /*
19353b3a8eb9SGleb Smirnoff  * ioctl operations
19363b3a8eb9SGleb Smirnoff  */
19373b3a8eb9SGleb Smirnoff 
19383b3a8eb9SGleb Smirnoff #define DIOCSTART	_IO  ('D',  1)
19393b3a8eb9SGleb Smirnoff #define DIOCSTOP	_IO  ('D',  2)
19403b3a8eb9SGleb Smirnoff #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
19415c62ededSKristof Provost #define DIOCADDRULENV	_IOWR('D',  4, struct pfioc_nv)
19423b3a8eb9SGleb Smirnoff #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
1943d710367dSKristof Provost #define DIOCGETRULENV	_IOWR('D',  7, struct pfioc_nv)
19443b3a8eb9SGleb Smirnoff /* XXX cut 8 - 17 */
19457606a45dSKristof Provost #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv)
1946cc535c95SWill Andrews #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
19471732afaaSKristof Provost #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv)
19483b3a8eb9SGleb Smirnoff #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
19492b10cf85SKristof Provost #define DIOCGETSTATUSNV	_IOWR('D', 21, struct pfioc_nv)
19503b3a8eb9SGleb Smirnoff #define DIOCCLRSTATUS	_IO  ('D', 22)
19513b3a8eb9SGleb Smirnoff #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
19523b3a8eb9SGleb Smirnoff #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
19533b3a8eb9SGleb Smirnoff #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1954cc535c95SWill Andrews #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
19553b3a8eb9SGleb Smirnoff /* XXX cut 26 - 28 */
19563b3a8eb9SGleb Smirnoff #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
19573b3a8eb9SGleb Smirnoff #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1958cc535c95SWill Andrews #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1959cc535c95SWill Andrews #define DIOCCLRRULECTRS	_IO  ('D', 38)
19603b3a8eb9SGleb Smirnoff #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
19613b3a8eb9SGleb Smirnoff #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
1962e989530aSKristof Provost #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv)
19633b3a8eb9SGleb Smirnoff #define DIOCSTARTALTQ	_IO  ('D', 42)
19643b3a8eb9SGleb Smirnoff #define DIOCSTOPALTQ	_IO  ('D', 43)
1965249cc75fSPatrick Kelsey #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1966249cc75fSPatrick Kelsey #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1967249cc75fSPatrick Kelsey #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1968249cc75fSPatrick Kelsey #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1969249cc75fSPatrick Kelsey #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1970249cc75fSPatrick Kelsey #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1971249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1972249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1973249cc75fSPatrick Kelsey #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1974249cc75fSPatrick Kelsey #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
19753b3a8eb9SGleb Smirnoff #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
19763b3a8eb9SGleb Smirnoff #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
19773b3a8eb9SGleb Smirnoff #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
19783b3a8eb9SGleb Smirnoff #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1979cc535c95SWill Andrews #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
19803b3a8eb9SGleb Smirnoff /* XXX cut 55 - 57 */
19813b3a8eb9SGleb Smirnoff #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
19823b3a8eb9SGleb Smirnoff #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
19833b3a8eb9SGleb Smirnoff #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
19843b3a8eb9SGleb Smirnoff #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
19853b3a8eb9SGleb Smirnoff #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
19863b3a8eb9SGleb Smirnoff #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
19873b3a8eb9SGleb Smirnoff #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
19883b3a8eb9SGleb Smirnoff #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
19893b3a8eb9SGleb Smirnoff #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
19903b3a8eb9SGleb Smirnoff #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
19913b3a8eb9SGleb Smirnoff #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
19923b3a8eb9SGleb Smirnoff #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
19933b3a8eb9SGleb Smirnoff #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
19943b3a8eb9SGleb Smirnoff #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
19953b3a8eb9SGleb Smirnoff #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
19963b3a8eb9SGleb Smirnoff #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
19973b3a8eb9SGleb Smirnoff #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
19983b3a8eb9SGleb Smirnoff #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
19993b3a8eb9SGleb Smirnoff #define	DIOCOSFPFLUSH	_IO('D', 78)
20003b3a8eb9SGleb Smirnoff #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
20013b3a8eb9SGleb Smirnoff #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
20023b3a8eb9SGleb Smirnoff #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
20033b3a8eb9SGleb Smirnoff #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
20043b3a8eb9SGleb Smirnoff #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
20053b3a8eb9SGleb Smirnoff #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
20063b3a8eb9SGleb Smirnoff #define	DIOCCLRSRCNODES	_IO('D', 85)
20073b3a8eb9SGleb Smirnoff #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
20083b3a8eb9SGleb Smirnoff #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
20093b3a8eb9SGleb Smirnoff #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
20103b3a8eb9SGleb Smirnoff #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
20113b3a8eb9SGleb Smirnoff #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
2012047c4e36SKristof Provost #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
2013047c4e36SKristof Provost #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
2014c6bf20a2SKristof Provost #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2)
20158e1864edSKristof Provost #define	DIOCGETSYNCOOKIES	_IOWR('D', 94, struct pfioc_nv)
20168e1864edSKristof Provost #define	DIOCSETSYNCOOKIES	_IOWR('D', 95, struct pfioc_nv)
2017047c4e36SKristof Provost #define	DIOCKEEPCOUNTERS	_IOWR('D', 96, struct pfioc_nv)
20188e492101SKristof Provost #define	DIOCKEEPCOUNTERS_FREEBSD13	_IOWR('D', 92, struct pfioc_nv)
2019e732e742SKristof Provost #define	DIOCADDETHRULE		_IOWR('D', 97, struct pfioc_nv)
2020e732e742SKristof Provost #define	DIOCGETETHRULE		_IOWR('D', 98, struct pfioc_nv)
2021e732e742SKristof Provost #define	DIOCGETETHRULES		_IOWR('D', 99, struct pfioc_nv)
20229bb06778SKristof Provost #define	DIOCGETETHRULESETS	_IOWR('D', 100, struct pfioc_nv)
20239bb06778SKristof Provost #define	DIOCGETETHRULESET	_IOWR('D', 101, struct pfioc_nv)
202439282ef3SKajetan Staszkiewicz #define DIOCSETREASS		_IOWR('D', 102, u_int32_t)
202542ec75f8SKristof Provost 
2026249cc75fSPatrick Kelsey struct pf_ifspeed_v0 {
20273b3a8eb9SGleb Smirnoff 	char			ifname[IFNAMSIZ];
20283b3a8eb9SGleb Smirnoff 	u_int32_t		baudrate;
20293b3a8eb9SGleb Smirnoff };
2030249cc75fSPatrick Kelsey 
2031249cc75fSPatrick Kelsey struct pf_ifspeed_v1 {
2032249cc75fSPatrick Kelsey 	char			ifname[IFNAMSIZ];
2033249cc75fSPatrick Kelsey 	u_int32_t		baudrate32;
2034249cc75fSPatrick Kelsey 	/* layout identical to struct pf_ifspeed_v0 up to this point */
2035249cc75fSPatrick Kelsey 	u_int64_t		baudrate;
2036249cc75fSPatrick Kelsey };
2037249cc75fSPatrick Kelsey 
2038249cc75fSPatrick Kelsey /* Latest version of struct pf_ifspeed_vX */
2039249cc75fSPatrick Kelsey #define PF_IFSPEED_VERSION	1
2040249cc75fSPatrick Kelsey 
2041249cc75fSPatrick Kelsey /*
2042249cc75fSPatrick Kelsey  * Compatibility and convenience macros
2043249cc75fSPatrick Kelsey  */
2044249cc75fSPatrick Kelsey #ifndef _KERNEL
2045249cc75fSPatrick Kelsey #ifdef PFIOC_USE_LATEST
2046249cc75fSPatrick Kelsey /*
2047249cc75fSPatrick Kelsey  * Maintaining in-tree consumers of the ioctl interface is easier when that
2048249cc75fSPatrick Kelsey  * code can be written in terms old names that refer to the latest interface
2049249cc75fSPatrick Kelsey  * version as that reduces the required changes in the consumers to those
2050249cc75fSPatrick Kelsey  * that are functionally necessary to accommodate a new interface version.
2051249cc75fSPatrick Kelsey  */
2052249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
2053249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
2054249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
2055249cc75fSPatrick Kelsey 
2056249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
2057249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
2058249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
2059249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
2060249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
2061249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
2062249cc75fSPatrick Kelsey #else
2063249cc75fSPatrick Kelsey /*
2064249cc75fSPatrick Kelsey  * When building out-of-tree code that is written for the old interface,
2065249cc75fSPatrick Kelsey  * such as may exist in ports for example, resolve the old struct tags and
2066249cc75fSPatrick Kelsey  * ioctl command names to the v0 versions.
2067249cc75fSPatrick Kelsey  */
2068249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
2069249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
2070249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
2071249cc75fSPatrick Kelsey 
2072249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
2073249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
2074249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
2075249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
2076249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
2077249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
2078249cc75fSPatrick Kelsey #endif /* PFIOC_USE_LATEST */
2079249cc75fSPatrick Kelsey #endif /* _KERNEL */
20803b3a8eb9SGleb Smirnoff 
20813b3a8eb9SGleb Smirnoff #ifdef _KERNEL
208217ad7334SKristof Provost LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
20833b3a8eb9SGleb Smirnoff struct pf_srchash {
208417ad7334SKristof Provost 	struct pf_ksrc_node_list		nodes;
20853b3a8eb9SGleb Smirnoff 	struct mtx			lock;
20863b3a8eb9SGleb Smirnoff };
20873b3a8eb9SGleb Smirnoff 
20883b3a8eb9SGleb Smirnoff struct pf_keyhash {
20893b3a8eb9SGleb Smirnoff 	LIST_HEAD(, pf_state_key)	keys;
20903b3a8eb9SGleb Smirnoff 	struct mtx			lock;
20913b3a8eb9SGleb Smirnoff };
20923b3a8eb9SGleb Smirnoff 
20933b3a8eb9SGleb Smirnoff struct pf_idhash {
2094211cddf9SKristof Provost 	LIST_HEAD(, pf_kstate)		states;
20953b3a8eb9SGleb Smirnoff 	struct mtx			lock;
20963b3a8eb9SGleb Smirnoff };
20973b3a8eb9SGleb Smirnoff 
2098231e83d3SKristof Provost extern u_long		pf_ioctl_maxcount;
20996828cc99SGleb Smirnoff extern u_long		pf_hashmask;
21006828cc99SGleb Smirnoff extern u_long		pf_srchashmask;
210191e0f2d2SKristof Provost #define	PF_HASHSIZ	(131072)
2102bf56a3feSKristof Provost #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
21033b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
21043b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_idhash *, pf_idhash);
21053b3a8eb9SGleb Smirnoff #define V_pf_keyhash	VNET(pf_keyhash)
21063b3a8eb9SGleb Smirnoff #define	V_pf_idhash	VNET(pf_idhash)
21073b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_srchash *, pf_srchash);
21083b3a8eb9SGleb Smirnoff #define	V_pf_srchash	VNET(pf_srchash)
21093b3a8eb9SGleb Smirnoff 
21106828cc99SGleb Smirnoff #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
21113b3a8eb9SGleb Smirnoff 
21123b3a8eb9SGleb Smirnoff VNET_DECLARE(void *, pf_swi_cookie);
21133b3a8eb9SGleb Smirnoff #define V_pf_swi_cookie	VNET(pf_swi_cookie)
2114cecfaf9bSKristof Provost VNET_DECLARE(struct intr_event *, pf_swi_ie);
2115cecfaf9bSKristof Provost #define	V_pf_swi_ie	VNET(pf_swi_ie)
21163b3a8eb9SGleb Smirnoff 
2117133935d2SKristof Provost VNET_DECLARE(struct unrhdr64, pf_stateid);
21183b3a8eb9SGleb Smirnoff #define	V_pf_stateid	VNET(pf_stateid)
21193b3a8eb9SGleb Smirnoff 
21203b3a8eb9SGleb Smirnoff TAILQ_HEAD(pf_altqqueue, pf_altq);
21218f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
21223b3a8eb9SGleb Smirnoff #define	V_pf_altqs			 VNET(pf_altqs)
2123320c1116SKristof Provost VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
21243b3a8eb9SGleb Smirnoff #define	V_pf_pabuf			 VNET(pf_pabuf)
21253b3a8eb9SGleb Smirnoff 
21263b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
21273b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
21283b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
21293b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
21303b3a8eb9SGleb Smirnoff VNET_DECLARE(int,			 altqs_inactive_open);
21313b3a8eb9SGleb Smirnoff #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
21323b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_pabuf);
21333b3a8eb9SGleb Smirnoff #define	V_ticket_pabuf			 VNET(ticket_pabuf)
21343b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
21353b3a8eb9SGleb Smirnoff #define	V_pf_altqs_active		 VNET(pf_altqs_active)
21368f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
21378f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
21383b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
21393b3a8eb9SGleb Smirnoff #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
21408f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
21418f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
21423b3a8eb9SGleb Smirnoff 
2143e86bddeaSKristof Provost VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
21443b3a8eb9SGleb Smirnoff #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
21453b3a8eb9SGleb Smirnoff 
2146d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
2147d40d4b3eSMateusz Guzik LIST_HEAD(allkiflist_head, pfi_kkif);
2148d40d4b3eSMateusz Guzik VNET_DECLARE(struct allkiflist_head, pf_allkiflist);
2149d40d4b3eSMateusz Guzik #define V_pf_allkiflist     VNET(pf_allkiflist)
2150d40d4b3eSMateusz Guzik VNET_DECLARE(size_t, pf_allkifcount);
2151d40d4b3eSMateusz Guzik #define V_pf_allkifcount     VNET(pf_allkifcount)
2152d40d4b3eSMateusz Guzik VNET_DECLARE(struct pfi_kkif *, pf_kifmarker);
2153d40d4b3eSMateusz Guzik #define V_pf_kifmarker     VNET(pf_kifmarker)
215402cf67ccSMateusz Guzik 
215502cf67ccSMateusz Guzik LIST_HEAD(allrulelist_head, pf_krule);
215602cf67ccSMateusz Guzik VNET_DECLARE(struct allrulelist_head, pf_allrulelist);
215702cf67ccSMateusz Guzik #define V_pf_allrulelist     VNET(pf_allrulelist)
215802cf67ccSMateusz Guzik VNET_DECLARE(size_t, pf_allrulecount);
215902cf67ccSMateusz Guzik #define V_pf_allrulecount     VNET(pf_allrulecount)
216002cf67ccSMateusz Guzik VNET_DECLARE(struct pf_krule *, pf_rulemarker);
216102cf67ccSMateusz Guzik #define V_pf_rulemarker     VNET(pf_rulemarker)
2162d40d4b3eSMateusz Guzik #endif
2163d40d4b3eSMateusz Guzik 
2164efc6c51fSGleb Smirnoff void				 pf_initialize(void);
21657e92ce73SMartin Matuska void				 pf_mtag_initialize(void);
21667e92ce73SMartin Matuska void				 pf_mtag_cleanup(void);
21673b3a8eb9SGleb Smirnoff void				 pf_cleanup(void);
21683b3a8eb9SGleb Smirnoff 
21693b3a8eb9SGleb Smirnoff struct pf_mtag			*pf_get_mtag(struct mbuf *);
21703b3a8eb9SGleb Smirnoff 
2171e86bddeaSKristof Provost extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
21723b3a8eb9SGleb Smirnoff #ifdef ALTQ
21733b3a8eb9SGleb Smirnoff extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
21743b3a8eb9SGleb Smirnoff #endif
21753b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_z);
21763b3a8eb9SGleb Smirnoff #define	V_pf_state_z		 VNET(pf_state_z)
21773b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
21783b3a8eb9SGleb Smirnoff #define	V_pf_state_key_z	 VNET(pf_state_key_z)
21793b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
21803b3a8eb9SGleb Smirnoff #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
21813b3a8eb9SGleb Smirnoff 
21823b3a8eb9SGleb Smirnoff extern void			 pf_purge_thread(void *);
2183a0429b54SBjoern A. Zeeb extern void			 pf_unload_vnet_purge(void);
21843b3a8eb9SGleb Smirnoff extern void			 pf_intr(void *);
21853b3a8eb9SGleb Smirnoff extern void			 pf_purge_expired_src_nodes(void);
21863b3a8eb9SGleb Smirnoff 
21878f3d786cSMateusz Guzik extern int			 pf_unlink_state(struct pf_kstate *);
2188320c1116SKristof Provost extern int			 pf_state_insert(struct pfi_kkif *,
2189d0fdf2b2SKristof Provost 				    struct pfi_kkif *,
21903b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
21913b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
2192211cddf9SKristof Provost 				    struct pf_kstate *);
2193211cddf9SKristof Provost extern struct pf_kstate		*pf_alloc_state(int);
2194211cddf9SKristof Provost extern void			 pf_free_state(struct pf_kstate *);
21953b3a8eb9SGleb Smirnoff 
21963b3a8eb9SGleb Smirnoff static __inline void
2197211cddf9SKristof Provost pf_ref_state(struct pf_kstate *s)
21983b3a8eb9SGleb Smirnoff {
21993b3a8eb9SGleb Smirnoff 
22003b3a8eb9SGleb Smirnoff 	refcount_acquire(&s->refs);
22013b3a8eb9SGleb Smirnoff }
22023b3a8eb9SGleb Smirnoff 
22033b3a8eb9SGleb Smirnoff static __inline int
2204211cddf9SKristof Provost pf_release_state(struct pf_kstate *s)
22053b3a8eb9SGleb Smirnoff {
22063b3a8eb9SGleb Smirnoff 
22073b3a8eb9SGleb Smirnoff 	if (refcount_release(&s->refs)) {
22083b3a8eb9SGleb Smirnoff 		pf_free_state(s);
22093b3a8eb9SGleb Smirnoff 		return (1);
22103b3a8eb9SGleb Smirnoff 	} else
22113b3a8eb9SGleb Smirnoff 		return (0);
22123b3a8eb9SGleb Smirnoff }
22133b3a8eb9SGleb Smirnoff 
2214d09388d0SMateusz Guzik static __inline int
2215211cddf9SKristof Provost pf_release_staten(struct pf_kstate *s, u_int n)
2216d09388d0SMateusz Guzik {
2217d09388d0SMateusz Guzik 
2218d09388d0SMateusz Guzik 	if (refcount_releasen(&s->refs, n)) {
2219d09388d0SMateusz Guzik 		pf_free_state(s);
2220d09388d0SMateusz Guzik 		return (1);
2221d09388d0SMateusz Guzik 	} else
2222d09388d0SMateusz Guzik 		return (0);
2223d09388d0SMateusz Guzik }
2224d09388d0SMateusz Guzik 
2225211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t);
2226211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_all(struct pf_state_key_cmp *,
22273b3a8eb9SGleb Smirnoff 				    u_int, int *);
222819d6e29bSMateusz Guzik extern bool			pf_find_state_all_exists(struct pf_state_key_cmp *,
222919d6e29bSMateusz Guzik 				    u_int);
223017ad7334SKristof Provost extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
2231db0a2bfdSKajetan Staszkiewicz 				    struct pf_krule *, sa_family_t,
2232db0a2bfdSKajetan Staszkiewicz 				    struct pf_srchash **, bool);
223317ad7334SKristof Provost extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
223417ad7334SKristof Provost extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
2235211cddf9SKristof Provost extern void			 pf_print_state(struct pf_kstate *);
223607d9bc07SGleb Smirnoff extern void			 pf_print_flags(u_int8_t);
2237ba94bf28SKristof Provost extern int			 pf_addr_wrap_neq(struct pf_addr_wrap *,
2238ba94bf28SKristof Provost 				    struct pf_addr_wrap *);
22393b3a8eb9SGleb Smirnoff extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
22403b3a8eb9SGleb Smirnoff 				    u_int8_t);
2241c110fc49SKristof Provost extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
2242c110fc49SKristof Provost 				    u_int16_t, u_int16_t, u_int8_t);
22433b3a8eb9SGleb Smirnoff 
22443b3a8eb9SGleb Smirnoff VNET_DECLARE(struct ifnet *,		 sync_ifp);
22453b3a8eb9SGleb Smirnoff #define	V_sync_ifp		 	 VNET(sync_ifp);
2246e86bddeaSKristof Provost VNET_DECLARE(struct pf_krule,		 pf_default_rule);
22473b3a8eb9SGleb Smirnoff #define	V_pf_default_rule		  VNET(pf_default_rule)
22483b3a8eb9SGleb Smirnoff extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
22498216f1a9SKristof Provost 				    sa_family_t);
2250e86bddeaSKristof Provost void				pf_free_rule(struct pf_krule *);
22513b3a8eb9SGleb Smirnoff 
2252e732e742SKristof Provost int	pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
22533b3a8eb9SGleb Smirnoff #ifdef INET
22549925aee0SKristof Provost int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *,
22559925aee0SKristof Provost 	    struct pf_rule_actions *);
2256f2064dd1SKajetan Staszkiewicz int	pf_normalize_ip(struct mbuf **, struct pfi_kkif *, u_short *,
22570324938aSGleb Smirnoff 	    struct pf_pdesc *);
22583b3a8eb9SGleb Smirnoff #endif /* INET */
22593b3a8eb9SGleb Smirnoff 
22603b3a8eb9SGleb Smirnoff #ifdef INET6
22619925aee0SKristof Provost int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *,
22629925aee0SKristof Provost 	    struct pf_rule_actions *);
2263f2064dd1SKajetan Staszkiewicz int	pf_normalize_ip6(struct mbuf **, struct pfi_kkif *, u_short *,
22640324938aSGleb Smirnoff 	    struct pf_pdesc *);
22653b3a8eb9SGleb Smirnoff void	pf_poolmask(struct pf_addr *, struct pf_addr*,
22668216f1a9SKristof Provost 	    struct pf_addr *, struct pf_addr *, sa_family_t);
22673b3a8eb9SGleb Smirnoff void	pf_addr_inc(struct pf_addr *, sa_family_t);
2268b52b61c0SKristof Provost int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *, bool);
22693b3a8eb9SGleb Smirnoff #endif /* INET6 */
22703b3a8eb9SGleb Smirnoff 
227110aa9ddbSKristof Provost int	pf_multihome_scan_init(struct mbuf *, int, int, struct pf_pdesc *,
227210aa9ddbSKristof Provost 	    struct pfi_kkif *);
227310aa9ddbSKristof Provost int	pf_multihome_scan_asconf(struct mbuf *, int, int, struct pf_pdesc *,
227410aa9ddbSKristof Provost 	    struct pfi_kkif *);
227510aa9ddbSKristof Provost 
2276211cddf9SKristof Provost u_int32_t	pf_new_isn(struct pf_kstate *);
22773b3a8eb9SGleb Smirnoff void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
22783b3a8eb9SGleb Smirnoff 	    sa_family_t);
22793b3a8eb9SGleb Smirnoff void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2280c110fc49SKristof Provost void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
2281c110fc49SKristof Provost 	    u_int8_t);
2282c110fc49SKristof Provost void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
2283c3f69af0SKristof Provost void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
2284c3f69af0SKristof Provost 	    bool, u_int8_t);
2285c3f69af0SKristof Provost void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
2286c3f69af0SKristof Provost     bool, u_int8_t);
2287211cddf9SKristof Provost void	pf_send_deferred_syn(struct pf_kstate *);
22883b3a8eb9SGleb Smirnoff int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
22893b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
22903b3a8eb9SGleb Smirnoff int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
22913b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
22923b3a8eb9SGleb Smirnoff int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
22933b3a8eb9SGleb Smirnoff 
2294efc6c51fSGleb Smirnoff void	pf_normalize_init(void);
22953b3a8eb9SGleb Smirnoff void	pf_normalize_cleanup(void);
2296f2064dd1SKajetan Staszkiewicz int	pf_normalize_tcp(struct pfi_kkif *, struct mbuf *, int, int, void *,
22973b3a8eb9SGleb Smirnoff 	    struct pf_pdesc *);
2298211cddf9SKristof Provost void	pf_normalize_tcp_cleanup(struct pf_kstate *);
22993b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
23003b3a8eb9SGleb Smirnoff 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
23013b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
2302211cddf9SKristof Provost 	    u_short *, struct tcphdr *, struct pf_kstate *,
23033b3a8eb9SGleb Smirnoff 	    struct pf_state_peer *, struct pf_state_peer *, int *);
230451a78dd2SKristof Provost int	pf_normalize_sctp_init(struct mbuf *, int, struct pf_pdesc *,
230551a78dd2SKristof Provost 	    struct pf_state_peer *, struct pf_state_peer *);
2306010ee43fSKristof Provost int	pf_normalize_sctp(int, struct pfi_kkif *, struct mbuf *, int,
2307010ee43fSKristof Provost 	    int, void *, struct pf_pdesc *);
23083b3a8eb9SGleb Smirnoff u_int32_t
2309211cddf9SKristof Provost 	pf_state_expires(const struct pf_kstate *);
23103b3a8eb9SGleb Smirnoff void	pf_purge_expired_fragments(void);
23115d0020d6SKristof Provost void	pf_purge_fragments(uint32_t);
2312320c1116SKristof Provost int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
23133b3a8eb9SGleb Smirnoff 	    int);
2314f2064dd1SKajetan Staszkiewicz int	pf_socket_lookup(struct pf_pdesc *, struct mbuf *);
23153b3a8eb9SGleb Smirnoff struct pf_state_key *pf_alloc_state_key(int);
23163b3a8eb9SGleb Smirnoff void	pfr_initialize(void);
23173b3a8eb9SGleb Smirnoff void	pfr_cleanup(void);
23183b3a8eb9SGleb Smirnoff int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
23193b3a8eb9SGleb Smirnoff void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
23203b3a8eb9SGleb Smirnoff 	    u_int64_t, int, int, int);
23213b3a8eb9SGleb Smirnoff int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
23223b3a8eb9SGleb Smirnoff void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
23233b3a8eb9SGleb Smirnoff struct pfr_ktable *
2324e86bddeaSKristof Provost 	pfr_attach_table(struct pf_kruleset *, char *);
2325812839e5SKristof Provost struct pfr_ktable *
2326812839e5SKristof Provost 	pfr_eth_attach_table(struct pf_keth_ruleset *, char *);
23273b3a8eb9SGleb Smirnoff void	pfr_detach_table(struct pfr_ktable *);
23283b3a8eb9SGleb Smirnoff int	pfr_clr_tables(struct pfr_table *, int *, int);
23293b3a8eb9SGleb Smirnoff int	pfr_add_tables(struct pfr_table *, int, int *, int);
23303b3a8eb9SGleb Smirnoff int	pfr_del_tables(struct pfr_table *, int, int *, int);
2331adfe2f6aSKristof Provost int	pfr_table_count(struct pfr_table *, int);
23323b3a8eb9SGleb Smirnoff int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
23333b3a8eb9SGleb Smirnoff int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
23343b3a8eb9SGleb Smirnoff int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
23353b3a8eb9SGleb Smirnoff int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
23363b3a8eb9SGleb Smirnoff int	pfr_clr_addrs(struct pfr_table *, int *, int);
23373b3a8eb9SGleb Smirnoff int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
23383b3a8eb9SGleb Smirnoff int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23393b3a8eb9SGleb Smirnoff 	    int);
23403b3a8eb9SGleb Smirnoff int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23413b3a8eb9SGleb Smirnoff 	    int);
23423b3a8eb9SGleb Smirnoff int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23433b3a8eb9SGleb Smirnoff 	    int *, int *, int *, int, u_int32_t);
23443b3a8eb9SGleb Smirnoff int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
23453b3a8eb9SGleb Smirnoff int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
23463b3a8eb9SGleb Smirnoff int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
23473b3a8eb9SGleb Smirnoff 	    int);
23483b3a8eb9SGleb Smirnoff int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
23493b3a8eb9SGleb Smirnoff 	    int);
23503b3a8eb9SGleb Smirnoff int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
23513b3a8eb9SGleb Smirnoff int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
23523b3a8eb9SGleb Smirnoff int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
23533b3a8eb9SGleb Smirnoff int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
23543b3a8eb9SGleb Smirnoff 	    int *, u_int32_t, int);
23553b3a8eb9SGleb Smirnoff 
23563b3a8eb9SGleb Smirnoff MALLOC_DECLARE(PFI_MTYPE);
2357320c1116SKristof Provost VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
23583b3a8eb9SGleb Smirnoff #define	V_pfi_all	 		 VNET(pfi_all)
23593b3a8eb9SGleb Smirnoff 
2360efc6c51fSGleb Smirnoff void		 pfi_initialize(void);
2361a0429b54SBjoern A. Zeeb void		 pfi_initialize_vnet(void);
23623b3a8eb9SGleb Smirnoff void		 pfi_cleanup(void);
2363a0429b54SBjoern A. Zeeb void		 pfi_cleanup_vnet(void);
2364320c1116SKristof Provost void		 pfi_kkif_ref(struct pfi_kkif *);
2365320c1116SKristof Provost void		 pfi_kkif_unref(struct pfi_kkif *);
2366320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_find(const char *);
2367320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
2368320c1116SKristof Provost int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
2369320c1116SKristof Provost void		 pfi_kkif_purge(void);
23703b3a8eb9SGleb Smirnoff int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
23713b3a8eb9SGleb Smirnoff 		    sa_family_t);
23723b3a8eb9SGleb Smirnoff int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
23733b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
23743b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
23753b3a8eb9SGleb Smirnoff void		 pfi_update_status(const char *, struct pf_status *);
23763b3a8eb9SGleb Smirnoff void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
23773b3a8eb9SGleb Smirnoff int		 pfi_set_flags(const char *, int);
23783b3a8eb9SGleb Smirnoff int		 pfi_clear_flags(const char *, int);
23793b3a8eb9SGleb Smirnoff 
2380e86bddeaSKristof Provost int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
23813b3a8eb9SGleb Smirnoff int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
238260048052SGleb Smirnoff int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
238360048052SGleb Smirnoff 		    sa_family_t);
23843b3a8eb9SGleb Smirnoff 
23858e1864edSKristof Provost u_int16_t	 pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t);
23868e1864edSKristof Provost u_int8_t	 pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t);
23878e1864edSKristof Provost struct mbuf 	*pf_build_tcp(const struct pf_krule *, sa_family_t,
23888e1864edSKristof Provost 		    const struct pf_addr *, const struct pf_addr *,
23898e1864edSKristof Provost 		    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
23907dc3be36SKajetan Staszkiewicz 		    u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool,
23917dc3be36SKajetan Staszkiewicz 		    u_int16_t, u_int16_t, int);
23928e1864edSKristof Provost void		 pf_send_tcp(const struct pf_krule *, sa_family_t,
23938e1864edSKristof Provost 			    const struct pf_addr *, const struct pf_addr *,
23948e1864edSKristof Provost 			    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
23957dc3be36SKajetan Staszkiewicz 			    u_int8_t, u_int16_t, u_int16_t, u_int8_t, bool,
23967dc3be36SKajetan Staszkiewicz 			    u_int16_t, u_int16_t, int);
23978e1864edSKristof Provost 
23988e1864edSKristof Provost void			 pf_syncookies_init(void);
239932271c4dSKristof Provost void			 pf_syncookies_cleanup(void);
24008e1864edSKristof Provost int			 pf_get_syncookies(struct pfioc_nv *);
24018e1864edSKristof Provost int			 pf_set_syncookies(struct pfioc_nv *);
24028e1864edSKristof Provost int			 pf_synflood_check(struct pf_pdesc *);
24038e1864edSKristof Provost void			 pf_syncookie_send(struct mbuf *m, int off,
24048e1864edSKristof Provost 			    struct pf_pdesc *);
24059c041b45SKristof Provost bool			 pf_syncookie_check(struct pf_pdesc *);
24068e1864edSKristof Provost u_int8_t		 pf_syncookie_validate(struct pf_pdesc *);
24078e1864edSKristof Provost struct mbuf *		 pf_syncookie_recreate_syn(uint8_t, int,
24088e1864edSKristof Provost 			    struct pf_pdesc *);
24098e1864edSKristof Provost 
2410a9572d8fSGleb Smirnoff VNET_DECLARE(struct pf_kstatus, pf_status);
24113b3a8eb9SGleb Smirnoff #define	V_pf_status	VNET(pf_status)
24123b3a8eb9SGleb Smirnoff 
24133b3a8eb9SGleb Smirnoff struct pf_limit {
24143b3a8eb9SGleb Smirnoff 	uma_zone_t	zone;
24153b3a8eb9SGleb Smirnoff 	u_int		limit;
24163b3a8eb9SGleb Smirnoff };
24173b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
24183b3a8eb9SGleb Smirnoff #define	V_pf_limits VNET(pf_limits)
24193b3a8eb9SGleb Smirnoff 
24203b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
24213b3a8eb9SGleb Smirnoff 
24223b3a8eb9SGleb Smirnoff #ifdef _KERNEL
2423e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
24243b3a8eb9SGleb Smirnoff #define	V_pf_anchors				 VNET(pf_anchors)
2425e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
24263b3a8eb9SGleb Smirnoff #define	V_pf_main_anchor			 VNET(pf_main_anchor)
2427c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor_global,	 pf_keth_anchors);
2428c5131afeSKristof Provost #define	V_pf_keth_anchors			 VNET(pf_keth_anchors)
24293b3a8eb9SGleb Smirnoff #define pf_main_ruleset	V_pf_main_anchor.ruleset
24303b3a8eb9SGleb Smirnoff 
2431c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor,		 pf_main_keth_anchor);
2432c5131afeSKristof Provost #define V_pf_main_keth_anchor			 VNET(pf_main_keth_anchor)
2433c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_ruleset*,		 pf_keth);
2434e732e742SKristof Provost #define	V_pf_keth				 VNET(pf_keth)
2435e732e742SKristof Provost 
2436e86bddeaSKristof Provost void			 pf_init_kruleset(struct pf_kruleset *);
2437c5131afeSKristof Provost void			 pf_init_keth(struct pf_keth_ruleset *);
2438e86bddeaSKristof Provost int			 pf_kanchor_setup(struct pf_krule *,
2439e86bddeaSKristof Provost 			    const struct pf_kruleset *, const char *);
2440d710367dSKristof Provost int			 pf_kanchor_nvcopyout(const struct pf_kruleset *,
2441d710367dSKristof Provost 			    const struct pf_krule *, nvlist_t *);
2442e86bddeaSKristof Provost int			 pf_kanchor_copyout(const struct pf_kruleset *,
2443e86bddeaSKristof Provost 			    const struct pf_krule *, struct pfioc_rule *);
2444e86bddeaSKristof Provost void			 pf_kanchor_remove(struct pf_krule *);
2445e86bddeaSKristof Provost void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
2446e86bddeaSKristof Provost struct pf_kruleset	*pf_find_kruleset(const char *);
2447e86bddeaSKristof Provost struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
24483b3a8eb9SGleb Smirnoff void			 pf_rs_initialize(void);
24495e9dae8eSKristof Provost 
2450c5131afeSKristof Provost 
2451773e3a71SMark Johnston struct pf_krule		*pf_krule_alloc(void);
2452c5131afeSKristof Provost 
2453c5131afeSKristof Provost void			 pf_remove_if_empty_keth_ruleset(
2454c5131afeSKristof Provost 			    struct pf_keth_ruleset *);
2455c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_keth_ruleset(const char *);
2456c5131afeSKristof Provost struct pf_keth_anchor	*pf_find_keth_anchor(const char *);
2457c5131afeSKristof Provost int			 pf_keth_anchor_setup(struct pf_keth_rule *,
2458c5131afeSKristof Provost 			    const struct pf_keth_ruleset *, const char *);
2459c5131afeSKristof Provost int			 pf_keth_anchor_nvcopyout(
2460c5131afeSKristof Provost 			    const struct pf_keth_ruleset *,
2461c5131afeSKristof Provost 			    const struct pf_keth_rule *, nvlist_t *);
2462c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_or_create_keth_ruleset(const char *);
2463c5131afeSKristof Provost void			 pf_keth_anchor_remove(struct pf_keth_rule *);
2464c5131afeSKristof Provost 
24655e9dae8eSKristof Provost void			 pf_krule_free(struct pf_krule *);
2466e86bddeaSKristof Provost #endif
24673b3a8eb9SGleb Smirnoff 
24683b3a8eb9SGleb Smirnoff /* The fingerprint functions can be linked into userland programs (tcpdump) */
24693b3a8eb9SGleb Smirnoff int	pf_osfp_add(struct pf_osfp_ioctl *);
24703b3a8eb9SGleb Smirnoff #ifdef _KERNEL
24713b3a8eb9SGleb Smirnoff struct pf_osfp_enlist *
24723b3a8eb9SGleb Smirnoff 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
24733b3a8eb9SGleb Smirnoff 	    const struct tcphdr *);
24743b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
24753b3a8eb9SGleb Smirnoff void	pf_osfp_flush(void);
24763b3a8eb9SGleb Smirnoff int	pf_osfp_get(struct pf_osfp_ioctl *);
24773b3a8eb9SGleb Smirnoff int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
24783b3a8eb9SGleb Smirnoff 
24793b3a8eb9SGleb Smirnoff #ifdef _KERNEL
24808216f1a9SKristof Provost void			 pf_print_host(struct pf_addr *, u_int16_t, sa_family_t);
248107d9bc07SGleb Smirnoff 
2482e86bddeaSKristof Provost void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
2483e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2484e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2485e86bddeaSKristof Provost int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
2486e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2487e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2488c5131afeSKristof Provost void			 pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *,
2489c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2490c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2491c5131afeSKristof Provost 			    int *);
2492c5131afeSKristof Provost int			 pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *,
2493c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2494c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2495c5131afeSKristof Provost 			    int *);
24963b3a8eb9SGleb Smirnoff 
249716303d2bSKajetan Staszkiewicz u_short			 pf_map_addr(u_int8_t, struct pf_krule *,
24983b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
2499d10de21fSKajetan Staszkiewicz 			    struct pfi_kkif **nkif, struct pf_addr *,
2500d10de21fSKajetan Staszkiewicz 			    struct pf_ksrc_node **);
2501e86bddeaSKristof Provost struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
2502f2064dd1SKajetan Staszkiewicz 			    int, struct pfi_kkif *, struct pf_ksrc_node **,
25033b3a8eb9SGleb Smirnoff 			    struct pf_state_key **, struct pf_state_key **,
25043b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
2505e86bddeaSKristof Provost 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
25063b3a8eb9SGleb Smirnoff 
25073b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
25083b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, u_int16_t);
25093b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
25106b4ed16dSKajetan Staszkiewicz void			 pf_rule_to_actions(struct pf_krule *,
25116b4ed16dSKajetan Staszkiewicz 			    struct pf_rule_actions *);
251239282ef3SKajetan Staszkiewicz int			 pf_normalize_mss(struct mbuf *m, int off,
25136b4ed16dSKajetan Staszkiewicz 			    struct pf_pdesc *pd);
251439282ef3SKajetan Staszkiewicz #ifdef INET
25156b4ed16dSKajetan Staszkiewicz void	pf_scrub_ip(struct mbuf **, struct pf_pdesc *);
251639282ef3SKajetan Staszkiewicz #endif	/* INET */
251739282ef3SKajetan Staszkiewicz #ifdef INET6
25186b4ed16dSKajetan Staszkiewicz void	pf_scrub_ip6(struct mbuf **, struct pf_pdesc *);
251939282ef3SKajetan Staszkiewicz #endif	/* INET6 */
252039282ef3SKajetan Staszkiewicz 
252126c841e2SKristof Provost struct pfi_kkif		*pf_kkif_create(int);
252226c841e2SKristof Provost void			 pf_kkif_free(struct pfi_kkif *);
25235a3b9507SKristof Provost void			 pf_kkif_zero(struct pfi_kkif *);
25243b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
25253b3a8eb9SGleb Smirnoff 
25263b3a8eb9SGleb Smirnoff #endif /* _NET_PFVAR_H_ */
2527