xref: /freebsd/sys/net/pfvar.h (revision c5131afe)
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 $
329e8c4accSGleb Smirnoff  *	$FreeBSD$
333b3a8eb9SGleb Smirnoff  */
343b3a8eb9SGleb Smirnoff 
353b3a8eb9SGleb Smirnoff #ifndef _NET_PFVAR_H_
363b3a8eb9SGleb Smirnoff #define _NET_PFVAR_H_
373b3a8eb9SGleb Smirnoff 
383b3a8eb9SGleb Smirnoff #include <sys/param.h>
393b3a8eb9SGleb Smirnoff #include <sys/queue.h>
4048278b88SGleb Smirnoff #include <sys/counter.h>
41455969d3SKristof Provost #include <sys/cpuset.h>
4220c4899aSKristof Provost #include <sys/epoch.h>
438ec07310SGleb Smirnoff #include <sys/malloc.h>
44d710367dSKristof Provost #include <sys/nv.h>
453b3a8eb9SGleb Smirnoff #include <sys/refcount.h>
46ec7b47fcSKristof Provost #include <sys/sdt.h>
478f2ac656SPatrick Kelsey #include <sys/sysctl.h>
48defdcdd5SMateusz Guzik #include <sys/smp.h>
49d25c25dcSKristof Provost #include <sys/lock.h>
50455969d3SKristof Provost #include <sys/rmlock.h>
513b3a8eb9SGleb Smirnoff #include <sys/tree.h>
52defdcdd5SMateusz Guzik #include <sys/seqc.h>
538ec07310SGleb Smirnoff #include <vm/uma.h>
543b3a8eb9SGleb Smirnoff 
55c658610bSKristof Provost #include <net/if.h>
56e732e742SKristof Provost #include <net/ethernet.h>
573b3a8eb9SGleb Smirnoff #include <net/radix.h>
583b3a8eb9SGleb Smirnoff #include <netinet/in.h>
59d38630f6SKristof Provost #ifdef _KERNEL
60d38630f6SKristof Provost #include <netinet/ip.h>
61d38630f6SKristof Provost #include <netinet/tcp.h>
62d38630f6SKristof Provost #include <netinet/udp.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 
2898f2ac656SPatrick Kelsey SYSCTL_DECL(_net_pf);
2908f2ac656SPatrick Kelsey MALLOC_DECLARE(M_PFHASH);
2918f2ac656SPatrick Kelsey 
292ec7b47fcSKristof Provost SDT_PROVIDER_DECLARE(pf);
293ec7b47fcSKristof Provost 
2943b3a8eb9SGleb Smirnoff struct pfi_dynaddr {
2953b3a8eb9SGleb Smirnoff 	TAILQ_ENTRY(pfi_dynaddr)	 entry;
2963b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_addr4;
2973b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_mask4;
2983b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_addr6;
2993b3a8eb9SGleb Smirnoff 	struct pf_addr			 pfid_mask6;
3003b3a8eb9SGleb Smirnoff 	struct pfr_ktable		*pfid_kt;
301320c1116SKristof Provost 	struct pfi_kkif			*pfid_kif;
3023b3a8eb9SGleb Smirnoff 	int				 pfid_net;	/* mask or 128 */
3033b3a8eb9SGleb Smirnoff 	int				 pfid_acnt4;	/* address count IPv4 */
3043b3a8eb9SGleb Smirnoff 	int				 pfid_acnt6;	/* address count IPv6 */
3053b3a8eb9SGleb Smirnoff 	sa_family_t			 pfid_af;	/* rule af */
3063b3a8eb9SGleb Smirnoff 	u_int8_t			 pfid_iflags;	/* PFI_AFLAG_* */
3073b3a8eb9SGleb Smirnoff };
3083b3a8eb9SGleb Smirnoff 
3093b3a8eb9SGleb Smirnoff /*
3103b3a8eb9SGleb Smirnoff  * Address manipulation macros
3113b3a8eb9SGleb Smirnoff  */
3123b3a8eb9SGleb Smirnoff #define	HTONL(x)	(x) = htonl((__uint32_t)(x))
3133b3a8eb9SGleb Smirnoff #define	HTONS(x)	(x) = htons((__uint16_t)(x))
3143b3a8eb9SGleb Smirnoff #define	NTOHL(x)	(x) = ntohl((__uint32_t)(x))
3153b3a8eb9SGleb Smirnoff #define	NTOHS(x)	(x) = ntohs((__uint16_t)(x))
3163b3a8eb9SGleb Smirnoff 
3173b3a8eb9SGleb Smirnoff #define	PF_NAME		"pf"
3183b3a8eb9SGleb Smirnoff 
3193b3a8eb9SGleb Smirnoff #define	PF_HASHROW_ASSERT(h)	mtx_assert(&(h)->lock, MA_OWNED)
3203b3a8eb9SGleb Smirnoff #define	PF_HASHROW_LOCK(h)	mtx_lock(&(h)->lock)
3213b3a8eb9SGleb Smirnoff #define	PF_HASHROW_UNLOCK(h)	mtx_unlock(&(h)->lock)
3223b3a8eb9SGleb Smirnoff 
323907257d6SMateusz Guzik #ifdef INVARIANTS
3243b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK(s)						\
3253b3a8eb9SGleb Smirnoff 	do {								\
326907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
327907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
328907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
329907257d6SMateusz Guzik 		mtx_lock(_s->lock);					\
3303b3a8eb9SGleb Smirnoff 	} while (0)
3313b3a8eb9SGleb Smirnoff #define	PF_STATE_UNLOCK(s)						\
3323b3a8eb9SGleb Smirnoff 	do {								\
333907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
334907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
335907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
336907257d6SMateusz Guzik 		mtx_unlock(_s->lock);					\
3373b3a8eb9SGleb Smirnoff 	} while (0)
338907257d6SMateusz Guzik #else
339907257d6SMateusz Guzik #define	PF_STATE_LOCK(s)	mtx_lock(s->lock)
340907257d6SMateusz Guzik #define	PF_STATE_UNLOCK(s)	mtx_unlock(s->lock)
341907257d6SMateusz Guzik #endif
3423b3a8eb9SGleb Smirnoff 
3433b3a8eb9SGleb Smirnoff #ifdef INVARIANTS
3443b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK_ASSERT(s)						\
3453b3a8eb9SGleb Smirnoff 	do {								\
346907257d6SMateusz Guzik 		struct pf_kstate *_s = (s);				\
347907257d6SMateusz Guzik 		struct pf_idhash *_ih = &V_pf_idhash[PF_IDHASH(_s)];	\
348907257d6SMateusz Guzik 		MPASS(_s->lock == &_ih->lock);				\
3493b3a8eb9SGleb Smirnoff 		PF_HASHROW_ASSERT(_ih);					\
3503b3a8eb9SGleb Smirnoff 	} while (0)
3513b3a8eb9SGleb Smirnoff #else /* !INVARIANTS */
3523b3a8eb9SGleb Smirnoff #define	PF_STATE_LOCK_ASSERT(s)		do {} while (0)
3533b3a8eb9SGleb Smirnoff #endif /* INVARIANTS */
3543b3a8eb9SGleb Smirnoff 
355f649cff5SMateusz Guzik extern struct mtx_padalign pf_unlnkdrules_mtx;
3563b3a8eb9SGleb Smirnoff #define	PF_UNLNKDRULES_LOCK()	mtx_lock(&pf_unlnkdrules_mtx)
3573b3a8eb9SGleb Smirnoff #define	PF_UNLNKDRULES_UNLOCK()	mtx_unlock(&pf_unlnkdrules_mtx)
3583b3a8eb9SGleb Smirnoff 
359455969d3SKristof Provost extern struct rmlock pf_rules_lock;
360455969d3SKristof Provost #define	PF_RULES_RLOCK_TRACKER	struct rm_priotracker _pf_rules_tracker
361455969d3SKristof Provost #define	PF_RULES_RLOCK()	rm_rlock(&pf_rules_lock, &_pf_rules_tracker)
362455969d3SKristof Provost #define	PF_RULES_RUNLOCK()	rm_runlock(&pf_rules_lock, &_pf_rules_tracker)
363455969d3SKristof Provost #define	PF_RULES_WLOCK()	rm_wlock(&pf_rules_lock)
364455969d3SKristof Provost #define	PF_RULES_WUNLOCK()	rm_wunlock(&pf_rules_lock)
365dc1ab04eSMateusz Guzik #define	PF_RULES_WOWNED()	rm_wowned(&pf_rules_lock)
366455969d3SKristof Provost #define	PF_RULES_ASSERT()	rm_assert(&pf_rules_lock, RA_LOCKED)
367455969d3SKristof Provost #define	PF_RULES_RASSERT()	rm_assert(&pf_rules_lock, RA_RLOCKED)
368455969d3SKristof Provost #define	PF_RULES_WASSERT()	rm_assert(&pf_rules_lock, RA_WLOCKED)
3693b3a8eb9SGleb Smirnoff 
370f649cff5SMateusz Guzik extern struct mtx_padalign pf_table_stats_lock;
371dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_LOCK()	mtx_lock(&pf_table_stats_lock)
372dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_UNLOCK()	mtx_unlock(&pf_table_stats_lock)
373dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_OWNED()	mtx_owned(&pf_table_stats_lock)
374dc1ab04eSMateusz Guzik #define	PF_TABLE_STATS_ASSERT()	mtx_assert(&pf_rules_lock, MA_OWNED)
375dc1ab04eSMateusz Guzik 
3762f8fb3a8SKristof Provost extern struct sx pf_end_lock;
3772f8fb3a8SKristof Provost 
3783b3a8eb9SGleb Smirnoff #define	PF_MODVER	1
3793b3a8eb9SGleb Smirnoff #define	PFLOG_MODVER	1
3803b3a8eb9SGleb Smirnoff #define	PFSYNC_MODVER	1
3813b3a8eb9SGleb Smirnoff 
3823b3a8eb9SGleb Smirnoff #define	PFLOG_MINVER	1
3833b3a8eb9SGleb Smirnoff #define	PFLOG_PREFVER	PFLOG_MODVER
3843b3a8eb9SGleb Smirnoff #define	PFLOG_MAXVER	1
3853b3a8eb9SGleb Smirnoff #define	PFSYNC_MINVER	1
3863b3a8eb9SGleb Smirnoff #define	PFSYNC_PREFVER	PFSYNC_MODVER
3873b3a8eb9SGleb Smirnoff #define	PFSYNC_MAXVER	1
3883b3a8eb9SGleb Smirnoff 
3893b3a8eb9SGleb Smirnoff #ifdef INET
3903b3a8eb9SGleb Smirnoff #ifndef INET6
3913b3a8eb9SGleb Smirnoff #define	PF_INET_ONLY
3923b3a8eb9SGleb Smirnoff #endif /* ! INET6 */
3933b3a8eb9SGleb Smirnoff #endif /* INET */
3943b3a8eb9SGleb Smirnoff 
3953b3a8eb9SGleb Smirnoff #ifdef INET6
3963b3a8eb9SGleb Smirnoff #ifndef INET
3973b3a8eb9SGleb Smirnoff #define	PF_INET6_ONLY
3983b3a8eb9SGleb Smirnoff #endif /* ! INET */
3993b3a8eb9SGleb Smirnoff #endif /* INET6 */
4003b3a8eb9SGleb Smirnoff 
4013b3a8eb9SGleb Smirnoff #ifdef INET
4023b3a8eb9SGleb Smirnoff #ifdef INET6
4033b3a8eb9SGleb Smirnoff #define	PF_INET_INET6
4043b3a8eb9SGleb Smirnoff #endif /* INET6 */
4053b3a8eb9SGleb Smirnoff #endif /* INET */
4063b3a8eb9SGleb Smirnoff 
4073b3a8eb9SGleb Smirnoff #else
4083b3a8eb9SGleb Smirnoff 
4093b3a8eb9SGleb Smirnoff #define	PF_INET_INET6
4103b3a8eb9SGleb Smirnoff 
4113b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
4123b3a8eb9SGleb Smirnoff 
4133b3a8eb9SGleb Smirnoff /* Both IPv4 and IPv6 */
4143b3a8eb9SGleb Smirnoff #ifdef PF_INET_INET6
4153b3a8eb9SGleb Smirnoff 
4163b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
4173b3a8eb9SGleb Smirnoff 	((c == AF_INET && (a)->addr32[0] == (b)->addr32[0]) || \
4186332e4ccSGeorge V. Neville-Neil 	(c == AF_INET6 && (a)->addr32[3] == (b)->addr32[3] && \
4193b3a8eb9SGleb Smirnoff 	(a)->addr32[2] == (b)->addr32[2] && \
4203b3a8eb9SGleb Smirnoff 	(a)->addr32[1] == (b)->addr32[1] && \
4213b3a8eb9SGleb Smirnoff 	(a)->addr32[0] == (b)->addr32[0])) \
4223b3a8eb9SGleb Smirnoff 
4233b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
4247d762423SKristof Provost 	((c == AF_INET && (a)->addr32[0] != (b)->addr32[0]) || \
42526022843SKristof Provost 	(c == AF_INET6 && ((a)->addr32[0] != (b)->addr32[0] || \
4263b3a8eb9SGleb Smirnoff 	(a)->addr32[1] != (b)->addr32[1] || \
4276332e4ccSGeorge V. Neville-Neil 	(a)->addr32[2] != (b)->addr32[2] || \
42826022843SKristof Provost 	(a)->addr32[3] != (b)->addr32[3]))) \
4293b3a8eb9SGleb Smirnoff 
4303b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
4313b3a8eb9SGleb Smirnoff 	((c == AF_INET && !(a)->addr32[0]) || \
4326332e4ccSGeorge V. Neville-Neil 	(c == AF_INET6 && !(a)->addr32[0] && !(a)->addr32[1] && \
4333b3a8eb9SGleb Smirnoff 	!(a)->addr32[2] && !(a)->addr32[3] )) \
4343b3a8eb9SGleb Smirnoff 
4353b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
4363b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
4373b3a8eb9SGleb Smirnoff 
4383b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
4393b3a8eb9SGleb Smirnoff 	pf_addrcpy(a, b, f)
4403b3a8eb9SGleb Smirnoff 
4413b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
4423b3a8eb9SGleb Smirnoff 	pf_addr_inc(a, f)
4433b3a8eb9SGleb Smirnoff 
4443b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
4453b3a8eb9SGleb Smirnoff 	pf_poolmask(a, b, c, d, f)
4463b3a8eb9SGleb Smirnoff 
4473b3a8eb9SGleb Smirnoff #else
4483b3a8eb9SGleb Smirnoff 
4493b3a8eb9SGleb Smirnoff /* Just IPv6 */
4503b3a8eb9SGleb Smirnoff 
4513b3a8eb9SGleb Smirnoff #ifdef PF_INET6_ONLY
4523b3a8eb9SGleb Smirnoff 
4533b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
4543b3a8eb9SGleb Smirnoff 	((a)->addr32[3] == (b)->addr32[3] && \
4553b3a8eb9SGleb Smirnoff 	(a)->addr32[2] == (b)->addr32[2] && \
4563b3a8eb9SGleb Smirnoff 	(a)->addr32[1] == (b)->addr32[1] && \
4573b3a8eb9SGleb Smirnoff 	(a)->addr32[0] == (b)->addr32[0]) \
4583b3a8eb9SGleb Smirnoff 
4593b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
4603b3a8eb9SGleb Smirnoff 	((a)->addr32[3] != (b)->addr32[3] || \
4613b3a8eb9SGleb Smirnoff 	(a)->addr32[2] != (b)->addr32[2] || \
4623b3a8eb9SGleb Smirnoff 	(a)->addr32[1] != (b)->addr32[1] || \
4633b3a8eb9SGleb Smirnoff 	(a)->addr32[0] != (b)->addr32[0]) \
4643b3a8eb9SGleb Smirnoff 
4653b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
4663b3a8eb9SGleb Smirnoff 	(!(a)->addr32[0] && \
4673b3a8eb9SGleb Smirnoff 	!(a)->addr32[1] && \
4683b3a8eb9SGleb Smirnoff 	!(a)->addr32[2] && \
4693b3a8eb9SGleb Smirnoff 	!(a)->addr32[3] ) \
4703b3a8eb9SGleb Smirnoff 
4713b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
4723b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
4733b3a8eb9SGleb Smirnoff 
4743b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
4753b3a8eb9SGleb Smirnoff 	pf_addrcpy(a, b, f)
4763b3a8eb9SGleb Smirnoff 
4773b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
4783b3a8eb9SGleb Smirnoff 	pf_addr_inc(a, f)
4793b3a8eb9SGleb Smirnoff 
4803b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
4813b3a8eb9SGleb Smirnoff 	pf_poolmask(a, b, c, d, f)
4823b3a8eb9SGleb Smirnoff 
4833b3a8eb9SGleb Smirnoff #else
4843b3a8eb9SGleb Smirnoff 
4853b3a8eb9SGleb Smirnoff /* Just IPv4 */
4863b3a8eb9SGleb Smirnoff #ifdef PF_INET_ONLY
4873b3a8eb9SGleb Smirnoff 
4883b3a8eb9SGleb Smirnoff #define PF_AEQ(a, b, c) \
4893b3a8eb9SGleb Smirnoff 	((a)->addr32[0] == (b)->addr32[0])
4903b3a8eb9SGleb Smirnoff 
4913b3a8eb9SGleb Smirnoff #define PF_ANEQ(a, b, c) \
4923b3a8eb9SGleb Smirnoff 	((a)->addr32[0] != (b)->addr32[0])
4933b3a8eb9SGleb Smirnoff 
4943b3a8eb9SGleb Smirnoff #define PF_AZERO(a, c) \
4953b3a8eb9SGleb Smirnoff 	(!(a)->addr32[0])
4963b3a8eb9SGleb Smirnoff 
4973b3a8eb9SGleb Smirnoff #define PF_MATCHA(n, a, m, b, f) \
4983b3a8eb9SGleb Smirnoff 	pf_match_addr(n, a, m, b, f)
4993b3a8eb9SGleb Smirnoff 
5003b3a8eb9SGleb Smirnoff #define PF_ACPY(a, b, f) \
5013b3a8eb9SGleb Smirnoff 	(a)->v4.s_addr = (b)->v4.s_addr
5023b3a8eb9SGleb Smirnoff 
5033b3a8eb9SGleb Smirnoff #define PF_AINC(a, f) \
5043b3a8eb9SGleb Smirnoff 	do { \
5053b3a8eb9SGleb Smirnoff 		(a)->addr32[0] = htonl(ntohl((a)->addr32[0]) + 1); \
5063b3a8eb9SGleb Smirnoff 	} while (0)
5073b3a8eb9SGleb Smirnoff 
5083b3a8eb9SGleb Smirnoff #define PF_POOLMASK(a, b, c, d, f) \
5093b3a8eb9SGleb Smirnoff 	do { \
5103b3a8eb9SGleb Smirnoff 		(a)->addr32[0] = ((b)->addr32[0] & (c)->addr32[0]) | \
5113b3a8eb9SGleb Smirnoff 		(((c)->addr32[0] ^ 0xffffffff ) & (d)->addr32[0]); \
5123b3a8eb9SGleb Smirnoff 	} while (0)
5133b3a8eb9SGleb Smirnoff 
5143b3a8eb9SGleb Smirnoff #endif /* PF_INET_ONLY */
5153b3a8eb9SGleb Smirnoff #endif /* PF_INET6_ONLY */
5163b3a8eb9SGleb Smirnoff #endif /* PF_INET_INET6 */
5173b3a8eb9SGleb Smirnoff 
5183b3a8eb9SGleb Smirnoff /*
5193b3a8eb9SGleb Smirnoff  * XXX callers not FIB-aware in our version of pf yet.
5203b3a8eb9SGleb Smirnoff  * OpenBSD fixed it later it seems, 2010/05/07 13:33:16 claudio.
5213b3a8eb9SGleb Smirnoff  */
5223b3a8eb9SGleb Smirnoff #define	PF_MISMATCHAW(aw, x, af, neg, ifp, rtid)			\
5233b3a8eb9SGleb Smirnoff 	(								\
5243b3a8eb9SGleb Smirnoff 		(((aw)->type == PF_ADDR_NOROUTE &&			\
5253b3a8eb9SGleb Smirnoff 		    pf_routable((x), (af), NULL, (rtid))) ||		\
5263b3a8eb9SGleb Smirnoff 		(((aw)->type == PF_ADDR_URPFFAILED && (ifp) != NULL &&	\
5273b3a8eb9SGleb Smirnoff 		    pf_routable((x), (af), (ifp), (rtid))) ||		\
5283b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_TABLE &&				\
5293b3a8eb9SGleb Smirnoff 		    !pfr_match_addr((aw)->p.tbl, (x), (af))) ||		\
5303b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_DYNIFTL &&			\
5313b3a8eb9SGleb Smirnoff 		    !pfi_match_addr((aw)->p.dyn, (x), (af))) ||		\
5323b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_RANGE &&				\
5333b3a8eb9SGleb Smirnoff 		    !pf_match_addr_range(&(aw)->v.a.addr,		\
5343b3a8eb9SGleb Smirnoff 		    &(aw)->v.a.mask, (x), (af))) ||			\
5353b3a8eb9SGleb Smirnoff 		((aw)->type == PF_ADDR_ADDRMASK &&			\
5363b3a8eb9SGleb Smirnoff 		    !PF_AZERO(&(aw)->v.a.mask, (af)) &&			\
5373b3a8eb9SGleb Smirnoff 		    !PF_MATCHA(0, &(aw)->v.a.addr,			\
5383b3a8eb9SGleb Smirnoff 		    &(aw)->v.a.mask, (x), (af))))) !=			\
5393b3a8eb9SGleb Smirnoff 		(neg)							\
5403b3a8eb9SGleb Smirnoff 	)
5413b3a8eb9SGleb Smirnoff 
542c3f69af0SKristof Provost #define PF_ALGNMNT(off) (((off) % 2) == 0)
543c3f69af0SKristof Provost 
54417ad7334SKristof Provost #ifdef _KERNEL
545e86bddeaSKristof Provost 
546320c1116SKristof Provost struct pf_kpooladdr {
547320c1116SKristof Provost 	struct pf_addr_wrap		 addr;
548320c1116SKristof Provost 	TAILQ_ENTRY(pf_kpooladdr)	 entries;
549320c1116SKristof Provost 	char				 ifname[IFNAMSIZ];
550320c1116SKristof Provost 	struct pfi_kkif			*kif;
551320c1116SKristof Provost };
552320c1116SKristof Provost 
553320c1116SKristof Provost TAILQ_HEAD(pf_kpalist, pf_kpooladdr);
554320c1116SKristof Provost 
555320c1116SKristof Provost struct pf_kpool {
5565f5e32f1SKristof Provost 	struct mtx		 mtx;
557320c1116SKristof Provost 	struct pf_kpalist	 list;
558320c1116SKristof Provost 	struct pf_kpooladdr	*cur;
559320c1116SKristof Provost 	struct pf_poolhashkey	 key;
560320c1116SKristof Provost 	struct pf_addr		 counter;
5612aa21096SKurosawa Takahiro 	struct pf_mape_portset	 mape;
562320c1116SKristof Provost 	int			 tblidx;
563320c1116SKristof Provost 	u_int16_t		 proxy_port[2];
564320c1116SKristof Provost 	u_int8_t		 opts;
565320c1116SKristof Provost };
566320c1116SKristof Provost 
567ef950daaSKristof Provost struct pf_rule_actions {
568b64f7ce9SKristof Provost 	uint16_t	 qid;
569b64f7ce9SKristof Provost 	uint16_t	 pqid;
57063b3c1c7SKristof Provost 	uint16_t	 dnpipe;
57163b3c1c7SKristof Provost 	uint16_t	 dnrpipe;	/* Reverse direction pipe */
57263b3c1c7SKristof Provost 	uint32_t	 flags;
573ef950daaSKristof Provost };
574ef950daaSKristof Provost 
575e732e742SKristof Provost union pf_keth_rule_ptr {
576e732e742SKristof Provost 	struct pf_keth_rule	*ptr;
577e732e742SKristof Provost 	uint32_t		nr;
578e732e742SKristof Provost };
579e732e742SKristof Provost 
580e732e742SKristof Provost struct pf_keth_rule_addr {
581e732e742SKristof Provost 	uint8_t	addr[ETHER_ADDR_LEN];
582e732e742SKristof Provost 	bool neg;
583e732e742SKristof Provost 	uint8_t	isset;
584e732e742SKristof Provost };
585e732e742SKristof Provost 
586c5131afeSKristof Provost struct pf_keth_anchor;
587c5131afeSKristof Provost 
588c5131afeSKristof Provost TAILQ_HEAD(pf_keth_ruleq, pf_keth_rule);
589c5131afeSKristof Provost 
590c5131afeSKristof Provost struct pf_keth_ruleset {
591c5131afeSKristof Provost 	struct pf_keth_ruleq		 rules[2];
592c5131afeSKristof Provost 	struct pf_keth_rules {
593c5131afeSKristof Provost 		struct pf_keth_ruleq	*rules;
594c5131afeSKristof Provost 		int			 open;
595c5131afeSKristof Provost 		uint32_t		 ticket;
596c5131afeSKristof Provost 	} active, inactive;
597c5131afeSKristof Provost 	struct epoch_context	 epoch_ctx;
598c5131afeSKristof Provost 	struct vnet		*vnet;
599c5131afeSKristof Provost 	struct pf_keth_anchor	*anchor;
600c5131afeSKristof Provost };
601c5131afeSKristof Provost 
602c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_global, pf_keth_anchor);
603c5131afeSKristof Provost RB_HEAD(pf_keth_anchor_node, pf_keth_anchor);
604c5131afeSKristof Provost struct pf_keth_anchor {
605c5131afeSKristof Provost 	RB_ENTRY(pf_keth_anchor)	 entry_node;
606c5131afeSKristof Provost 	RB_ENTRY(pf_keth_anchor)	 entry_global;
607c5131afeSKristof Provost 	struct pf_keth_anchor		*parent;
608c5131afeSKristof Provost 	struct pf_keth_anchor_node	 children;
609c5131afeSKristof Provost 	char				 name[PF_ANCHOR_NAME_SIZE];
610c5131afeSKristof Provost 	char				 path[MAXPATHLEN];
611c5131afeSKristof Provost 	struct pf_keth_ruleset		 ruleset;
612c5131afeSKristof Provost 	int				 refcnt;	/* anchor rules */
613c5131afeSKristof Provost 	uint8_t				 anchor_relative;
614c5131afeSKristof Provost 	uint8_t				 anchor_wildcard;
615c5131afeSKristof Provost };
616c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_node, pf_keth_anchor, entry_node,
617c5131afeSKristof Provost     pf_keth_anchor_compare);
618c5131afeSKristof Provost RB_PROTOTYPE(pf_keth_anchor_global, pf_keth_anchor, entry_global,
619c5131afeSKristof Provost     pf_keth_anchor_compare);
620c5131afeSKristof Provost 
621e732e742SKristof Provost struct pf_keth_rule {
622e732e742SKristof Provost #define PFE_SKIP_IFP		0
623e732e742SKristof Provost #define PFE_SKIP_DIR		1
624e732e742SKristof Provost #define PFE_SKIP_PROTO		2
625e732e742SKristof Provost #define PFE_SKIP_SRC_ADDR	3
626e732e742SKristof Provost #define PFE_SKIP_DST_ADDR	4
627e732e742SKristof Provost #define PFE_SKIP_COUNT		5
628e732e742SKristof Provost 	union pf_keth_rule_ptr	 skip[PFE_SKIP_COUNT];
629e732e742SKristof Provost 
630e732e742SKristof Provost 	TAILQ_ENTRY(pf_keth_rule)	entries;
631e732e742SKristof Provost 
632c5131afeSKristof Provost 	struct pf_keth_anchor	*anchor;
633c5131afeSKristof Provost 	u_int8_t		 anchor_relative;
634c5131afeSKristof Provost 	u_int8_t		 anchor_wildcard;
635c5131afeSKristof Provost 
636e732e742SKristof Provost 	uint32_t		 nr;
637e732e742SKristof Provost 
638e732e742SKristof Provost 	bool			 quick;
639e732e742SKristof Provost 
640e732e742SKristof Provost 	/* Filter */
641e732e742SKristof Provost 	char			 ifname[IFNAMSIZ];
642e732e742SKristof Provost 	struct pfi_kkif		*kif;
643e732e742SKristof Provost 	bool			 ifnot;
644e732e742SKristof Provost 	uint8_t			 direction;
645e732e742SKristof Provost 	uint16_t		 proto;
646e732e742SKristof Provost 	struct pf_keth_rule_addr src, dst;
647e732e742SKristof Provost 
648e732e742SKristof Provost 	/* Stats */
649e732e742SKristof Provost 	counter_u64_t		 evaluations;
650e732e742SKristof Provost 	counter_u64_t		 packets[2];
651e732e742SKristof Provost 	counter_u64_t		 bytes[2];
652e732e742SKristof Provost 
653e732e742SKristof Provost 	/* Action */
654e732e742SKristof Provost 	char			 qname[PF_QNAME_SIZE];
655e732e742SKristof Provost 	int			 qid;
656e732e742SKristof Provost 	char			 tagname[PF_TAG_NAME_SIZE];
657e732e742SKristof Provost 	uint16_t		 tag;
658e732e742SKristof Provost 	uint8_t			 action;
659fb330f39SKristof Provost 	uint16_t		 dnpipe;
660fb330f39SKristof Provost 	uint32_t		 dnflags;
661e732e742SKristof Provost };
662e732e742SKristof Provost 
663e86bddeaSKristof Provost union pf_krule_ptr {
664e86bddeaSKristof Provost 	struct pf_krule		*ptr;
665e86bddeaSKristof Provost 	u_int32_t		 nr;
666e86bddeaSKristof Provost };
667e86bddeaSKristof Provost 
668e86bddeaSKristof Provost struct pf_krule {
669e86bddeaSKristof Provost 	struct pf_rule_addr	 src;
670e86bddeaSKristof Provost 	struct pf_rule_addr	 dst;
671e86bddeaSKristof Provost 	union pf_krule_ptr	 skip[PF_SKIP_COUNT];
6726fcc8e04SKristof Provost 	char			 label[PF_RULE_MAX_LABEL_COUNT][PF_RULE_LABEL_SIZE];
67376c5eeccSKristof Provost 	uint32_t		 ridentifier;
674e86bddeaSKristof Provost 	char			 ifname[IFNAMSIZ];
675e86bddeaSKristof Provost 	char			 qname[PF_QNAME_SIZE];
676e86bddeaSKristof Provost 	char			 pqname[PF_QNAME_SIZE];
677e86bddeaSKristof Provost 	char			 tagname[PF_TAG_NAME_SIZE];
678e86bddeaSKristof Provost 	char			 match_tagname[PF_TAG_NAME_SIZE];
679e86bddeaSKristof Provost 
680e86bddeaSKristof Provost 	char			 overload_tblname[PF_TABLE_NAME_SIZE];
681e86bddeaSKristof Provost 
682e86bddeaSKristof Provost 	TAILQ_ENTRY(pf_krule)	 entries;
683320c1116SKristof Provost 	struct pf_kpool		 rpool;
684e86bddeaSKristof Provost 
68502cf67ccSMateusz Guzik 	struct pf_counter_u64	 evaluations;
68602cf67ccSMateusz Guzik 	struct pf_counter_u64	 packets[2];
68702cf67ccSMateusz Guzik 	struct pf_counter_u64	 bytes[2];
688e86bddeaSKristof Provost 
689320c1116SKristof Provost 	struct pfi_kkif		*kif;
690e86bddeaSKristof Provost 	struct pf_kanchor	*anchor;
691e86bddeaSKristof Provost 	struct pfr_ktable	*overload_tbl;
692e86bddeaSKristof Provost 
693e86bddeaSKristof Provost 	pf_osfp_t		 os_fingerprint;
694e86bddeaSKristof Provost 
695e86bddeaSKristof Provost 	int			 rtableid;
696e86bddeaSKristof Provost 	u_int32_t		 timeout[PFTM_MAX];
697e86bddeaSKristof Provost 	u_int32_t		 max_states;
698e86bddeaSKristof Provost 	u_int32_t		 max_src_nodes;
699e86bddeaSKristof Provost 	u_int32_t		 max_src_states;
700e86bddeaSKristof Provost 	u_int32_t		 max_src_conn;
701e86bddeaSKristof Provost 	struct {
702e86bddeaSKristof Provost 		u_int32_t		limit;
703e86bddeaSKristof Provost 		u_int32_t		seconds;
704e86bddeaSKristof Provost 	}			 max_src_conn_rate;
705b64f7ce9SKristof Provost 	u_int16_t		 qid;
706b64f7ce9SKristof Provost 	u_int16_t		 pqid;
70763b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
70863b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
70963b3c1c7SKristof Provost 	u_int32_t		 free_flags;
710e86bddeaSKristof Provost 	u_int32_t		 nr;
711e86bddeaSKristof Provost 	u_int32_t		 prob;
712e86bddeaSKristof Provost 	uid_t			 cuid;
713e86bddeaSKristof Provost 	pid_t			 cpid;
714e86bddeaSKristof Provost 
715e86bddeaSKristof Provost 	counter_u64_t		 states_cur;
716e86bddeaSKristof Provost 	counter_u64_t		 states_tot;
717e86bddeaSKristof Provost 	counter_u64_t		 src_nodes;
718e86bddeaSKristof Provost 
719e86bddeaSKristof Provost 	u_int16_t		 return_icmp;
720e86bddeaSKristof Provost 	u_int16_t		 return_icmp6;
721e86bddeaSKristof Provost 	u_int16_t		 max_mss;
722e86bddeaSKristof Provost 	u_int16_t		 tag;
723e86bddeaSKristof Provost 	u_int16_t		 match_tag;
724e86bddeaSKristof Provost 	u_int16_t		 scrub_flags;
725e86bddeaSKristof Provost 
726e86bddeaSKristof Provost 	struct pf_rule_uid	 uid;
727e86bddeaSKristof Provost 	struct pf_rule_gid	 gid;
728e86bddeaSKristof Provost 
729e86bddeaSKristof Provost 	u_int32_t		 rule_flag;
7304f1f67e8SKristof Provost 	uint32_t		 rule_ref;
731e86bddeaSKristof Provost 	u_int8_t		 action;
732e86bddeaSKristof Provost 	u_int8_t		 direction;
733e86bddeaSKristof Provost 	u_int8_t		 log;
734e86bddeaSKristof Provost 	u_int8_t		 logif;
735e86bddeaSKristof Provost 	u_int8_t		 quick;
736e86bddeaSKristof Provost 	u_int8_t		 ifnot;
737e86bddeaSKristof Provost 	u_int8_t		 match_tag_not;
738e86bddeaSKristof Provost 	u_int8_t		 natpass;
739e86bddeaSKristof Provost 
740e86bddeaSKristof Provost 	u_int8_t		 keep_state;
741e86bddeaSKristof Provost 	sa_family_t		 af;
742e86bddeaSKristof Provost 	u_int8_t		 proto;
743e86bddeaSKristof Provost 	u_int8_t		 type;
744e86bddeaSKristof Provost 	u_int8_t		 code;
745e86bddeaSKristof Provost 	u_int8_t		 flags;
746e86bddeaSKristof Provost 	u_int8_t		 flagset;
747e86bddeaSKristof Provost 	u_int8_t		 min_ttl;
748e86bddeaSKristof Provost 	u_int8_t		 allow_opts;
749e86bddeaSKristof Provost 	u_int8_t		 rt;
750e86bddeaSKristof Provost 	u_int8_t		 return_ttl;
751e86bddeaSKristof Provost 	u_int8_t		 tos;
752e86bddeaSKristof Provost 	u_int8_t		 set_tos;
753e86bddeaSKristof Provost 	u_int8_t		 anchor_relative;
754e86bddeaSKristof Provost 	u_int8_t		 anchor_wildcard;
755e86bddeaSKristof Provost 
756e86bddeaSKristof Provost 	u_int8_t		 flush;
757e86bddeaSKristof Provost 	u_int8_t		 prio;
758e86bddeaSKristof Provost 	u_int8_t		 set_prio[2];
759e86bddeaSKristof Provost 
760e86bddeaSKristof Provost 	struct {
761e86bddeaSKristof Provost 		struct pf_addr		addr;
762e86bddeaSKristof Provost 		u_int16_t		port;
763e86bddeaSKristof Provost 	}			divert;
76402cf67ccSMateusz Guzik 
76502cf67ccSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
76602cf67ccSMateusz Guzik 	LIST_ENTRY(pf_krule)	 allrulelist;
76702cf67ccSMateusz Guzik 	bool			 allrulelinked;
76802cf67ccSMateusz Guzik #endif
769e86bddeaSKristof Provost };
770e86bddeaSKristof Provost 
77117ad7334SKristof Provost struct pf_ksrc_node {
77217ad7334SKristof Provost 	LIST_ENTRY(pf_ksrc_node) entry;
7733b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr;
7743b3a8eb9SGleb Smirnoff 	struct pf_addr	 raddr;
775e86bddeaSKristof Provost 	union pf_krule_ptr rule;
776320c1116SKristof Provost 	struct pfi_kkif	*kif;
777fbbf270eSKristof Provost 	counter_u64_t	 bytes[2];
778fbbf270eSKristof Provost 	counter_u64_t	 packets[2];
7793b3a8eb9SGleb Smirnoff 	u_int32_t	 states;
7803b3a8eb9SGleb Smirnoff 	u_int32_t	 conn;
7813b3a8eb9SGleb Smirnoff 	struct pf_threshold	conn_rate;
7823b3a8eb9SGleb Smirnoff 	u_int32_t	 creation;
7833b3a8eb9SGleb Smirnoff 	u_int32_t	 expire;
7843b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
7853b3a8eb9SGleb Smirnoff 	u_int8_t	 ruletype;
7863b3a8eb9SGleb Smirnoff };
78717ad7334SKristof Provost #endif
7883b3a8eb9SGleb Smirnoff 
7893b3a8eb9SGleb Smirnoff struct pf_state_scrub {
7903b3a8eb9SGleb Smirnoff 	struct timeval	pfss_last;	/* time received last packet	*/
7913b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsecr;	/* last echoed timestamp	*/
7923b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsval;	/* largest timestamp		*/
7933b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_tsval0;	/* original timestamp		*/
7943b3a8eb9SGleb Smirnoff 	u_int16_t	pfss_flags;
7953b3a8eb9SGleb Smirnoff #define PFSS_TIMESTAMP	0x0001		/* modulate timestamp		*/
7963b3a8eb9SGleb Smirnoff #define PFSS_PAWS	0x0010		/* stricter PAWS checks		*/
7973b3a8eb9SGleb Smirnoff #define PFSS_PAWS_IDLED	0x0020		/* was idle too long.  no PAWS	*/
7983b3a8eb9SGleb Smirnoff #define PFSS_DATA_TS	0x0040		/* timestamp on data packets	*/
7993b3a8eb9SGleb Smirnoff #define PFSS_DATA_NOTS	0x0080		/* no timestamp on data packets	*/
8003b3a8eb9SGleb Smirnoff 	u_int8_t	pfss_ttl;	/* stashed TTL			*/
8013b3a8eb9SGleb Smirnoff 	u_int8_t	pad;
8023b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_ts_mod;	/* timestamp modulation		*/
8033b3a8eb9SGleb Smirnoff };
8043b3a8eb9SGleb Smirnoff 
8053b3a8eb9SGleb Smirnoff struct pf_state_host {
8063b3a8eb9SGleb Smirnoff 	struct pf_addr	addr;
8073b3a8eb9SGleb Smirnoff 	u_int16_t	port;
8083b3a8eb9SGleb Smirnoff 	u_int16_t	pad;
8093b3a8eb9SGleb Smirnoff };
8103b3a8eb9SGleb Smirnoff 
8113b3a8eb9SGleb Smirnoff struct pf_state_peer {
8123b3a8eb9SGleb Smirnoff 	struct pf_state_scrub	*scrub;	/* state is scrubbed		*/
8133b3a8eb9SGleb Smirnoff 	u_int32_t	seqlo;		/* Max sequence number sent	*/
8143b3a8eb9SGleb Smirnoff 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
8153b3a8eb9SGleb Smirnoff 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
8163b3a8eb9SGleb Smirnoff 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
8173b3a8eb9SGleb Smirnoff 	u_int16_t	mss;		/* Maximum segment size option	*/
8183b3a8eb9SGleb Smirnoff 	u_int8_t	state;		/* active state level		*/
8193b3a8eb9SGleb Smirnoff 	u_int8_t	wscale;		/* window scaling factor	*/
8203b3a8eb9SGleb Smirnoff 	u_int8_t	tcp_est;	/* Did we reach TCPS_ESTABLISHED */
8213b3a8eb9SGleb Smirnoff 	u_int8_t	pad[1];
8223b3a8eb9SGleb Smirnoff };
8233b3a8eb9SGleb Smirnoff 
8243b3a8eb9SGleb Smirnoff /* Keep synced with struct pf_state_key. */
8253b3a8eb9SGleb Smirnoff struct pf_state_key_cmp {
8263b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
8273b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
8283b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
8293b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
8303b3a8eb9SGleb Smirnoff 	u_int8_t	 pad[2];
8313b3a8eb9SGleb Smirnoff };
8323b3a8eb9SGleb Smirnoff 
8333b3a8eb9SGleb Smirnoff struct pf_state_key {
8343b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
8353b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
8363b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
8373b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
8383b3a8eb9SGleb Smirnoff 	u_int8_t	 pad[2];
8393b3a8eb9SGleb Smirnoff 
8403b3a8eb9SGleb Smirnoff 	LIST_ENTRY(pf_state_key) entry;
841211cddf9SKristof Provost 	TAILQ_HEAD(, pf_kstate)	 states[2];
8423b3a8eb9SGleb Smirnoff };
8433b3a8eb9SGleb Smirnoff 
844211cddf9SKristof Provost /* Keep synced with struct pf_kstate. */
8453b3a8eb9SGleb Smirnoff struct pf_state_cmp {
8463b3a8eb9SGleb Smirnoff 	u_int64_t		 id;
8473b3a8eb9SGleb Smirnoff 	u_int32_t		 creatorid;
8483b3a8eb9SGleb Smirnoff 	u_int8_t		 direction;
8493b3a8eb9SGleb Smirnoff 	u_int8_t		 pad[3];
8503b3a8eb9SGleb Smirnoff };
8513b3a8eb9SGleb Smirnoff 
852e86bddeaSKristof Provost #define	PFSTATE_ALLOWOPTS	0x01
853e86bddeaSKristof Provost #define	PFSTATE_SLOPPY		0x02
854e86bddeaSKristof Provost /*  was	PFSTATE_PFLOW		0x04 */
855e86bddeaSKristof Provost #define	PFSTATE_NOSYNC		0x08
856e86bddeaSKristof Provost #define	PFSTATE_ACK		0x10
85763b3c1c7SKristof Provost #define	PFRULE_DN_IS_PIPE	0x40
85863b3c1c7SKristof Provost #define	PFRULE_DN_IS_QUEUE	0x80
859e86bddeaSKristof Provost #define	PFSTATE_SETPRIO		0x0200
860e86bddeaSKristof Provost #define	PFSTATE_SETMASK   (PFSTATE_SETPRIO)
861e86bddeaSKristof Provost 
862c6bf20a2SKristof Provost struct pf_state_scrub_export {
863c6bf20a2SKristof Provost 	uint16_t	pfss_flags;
864c6bf20a2SKristof Provost 	uint8_t		pfss_ttl;	/* stashed TTL		*/
865c6bf20a2SKristof Provost #define PF_SCRUB_FLAG_VALID		0x01
866c6bf20a2SKristof Provost 	uint8_t		scrub_flag;
867c6bf20a2SKristof Provost 	uint32_t	pfss_ts_mod;	/* timestamp modulation	*/
868c6bf20a2SKristof Provost };
869c6bf20a2SKristof Provost 
870c6bf20a2SKristof Provost struct pf_state_key_export {
871c6bf20a2SKristof Provost 	struct pf_addr	 addr[2];
872c6bf20a2SKristof Provost 	uint16_t	 port[2];
873c6bf20a2SKristof Provost };
874c6bf20a2SKristof Provost 
875c6bf20a2SKristof Provost struct pf_state_peer_export {
876c6bf20a2SKristof Provost 	struct pf_state_scrub_export	scrub;	/* state is scrubbed	*/
877c6bf20a2SKristof Provost 	uint32_t	seqlo;		/* Max sequence number sent	*/
878c6bf20a2SKristof Provost 	uint32_t	seqhi;		/* Max the other end ACKd + win	*/
879c6bf20a2SKristof Provost 	uint32_t	seqdiff;	/* Sequence number modulator	*/
880c6bf20a2SKristof Provost 	uint16_t	max_win;	/* largest window (pre scaling)	*/
881c6bf20a2SKristof Provost 	uint16_t	mss;		/* Maximum segment size option	*/
882c6bf20a2SKristof Provost 	uint8_t		state;		/* active state level		*/
883c6bf20a2SKristof Provost 	uint8_t		wscale;		/* window scaling factor	*/
884c6bf20a2SKristof Provost 	uint8_t		dummy[6];
885c6bf20a2SKristof Provost };
886c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_peer_export) == 32, "size incorrect");
887c6bf20a2SKristof Provost 
888c6bf20a2SKristof Provost struct pf_state_export {
889c6bf20a2SKristof Provost 	uint64_t	 version;
890c6bf20a2SKristof Provost #define	PF_STATE_VERSION	20210706
891c6bf20a2SKristof Provost 	uint64_t	 id;
892c6bf20a2SKristof Provost 	char		 ifname[IFNAMSIZ];
893c6bf20a2SKristof Provost 	char		 orig_ifname[IFNAMSIZ];
894c6bf20a2SKristof Provost 	struct pf_state_key_export	 key[2];
895c6bf20a2SKristof Provost 	struct pf_state_peer_export	 src;
896c6bf20a2SKristof Provost 	struct pf_state_peer_export	 dst;
897c6bf20a2SKristof Provost 	struct pf_addr	 rt_addr;
898c6bf20a2SKristof Provost 	uint32_t	 rule;
899c6bf20a2SKristof Provost 	uint32_t	 anchor;
900c6bf20a2SKristof Provost 	uint32_t	 nat_rule;
901c6bf20a2SKristof Provost 	uint32_t	 creation;
902c6bf20a2SKristof Provost 	uint32_t	 expire;
903c6bf20a2SKristof Provost 	uint32_t	 spare0;
904c6bf20a2SKristof Provost 	uint64_t	 packets[2];
905c6bf20a2SKristof Provost 	uint64_t	 bytes[2];
906c6bf20a2SKristof Provost 	uint32_t	 creatorid;
907c6bf20a2SKristof Provost 	uint32_t	 spare1;
908c6bf20a2SKristof Provost 	sa_family_t	 af;
909c6bf20a2SKristof Provost 	uint8_t		 proto;
910c6bf20a2SKristof Provost 	uint8_t		 direction;
911c6bf20a2SKristof Provost 	uint8_t		 log;
912c6bf20a2SKristof Provost 	uint8_t		 state_flags;
913c6bf20a2SKristof Provost 	uint8_t		 timeout;
914c6bf20a2SKristof Provost 	uint8_t		 sync_flags;
915c6bf20a2SKristof Provost 	uint8_t		 updates;
916c6bf20a2SKristof Provost 
917c6bf20a2SKristof Provost 	uint8_t		 spare[112];
918c6bf20a2SKristof Provost };
919c6bf20a2SKristof Provost _Static_assert(sizeof(struct pf_state_export) == 384, "size incorrect");
920c6bf20a2SKristof Provost 
921e86bddeaSKristof Provost #ifdef _KERNEL
922211cddf9SKristof Provost struct pf_kstate {
923f9aa757dSMateusz Guzik 	/*
924f9aa757dSMateusz Guzik 	 * Area shared with pf_state_cmp
925f9aa757dSMateusz Guzik 	 */
9263b3a8eb9SGleb Smirnoff 	u_int64_t		 id;
9273b3a8eb9SGleb Smirnoff 	u_int32_t		 creatorid;
9283b3a8eb9SGleb Smirnoff 	u_int8_t		 direction;
9293b3a8eb9SGleb Smirnoff 	u_int8_t		 pad[3];
930f9aa757dSMateusz Guzik 	/*
931f9aa757dSMateusz Guzik 	 * end of the area
932f9aa757dSMateusz Guzik 	 */
9333b3a8eb9SGleb Smirnoff 
9349009d36aSMateusz Guzik 	u_int8_t		 state_flags;
9359009d36aSMateusz Guzik 	u_int8_t		 timeout;
9369009d36aSMateusz Guzik 	u_int8_t		 sync_state; /* PFSYNC_S_x */
9379009d36aSMateusz Guzik 	u_int8_t		 sync_updates; /* XXX */
9383b3a8eb9SGleb Smirnoff 	u_int			 refs;
939907257d6SMateusz Guzik 	struct mtx		*lock;
940211cddf9SKristof Provost 	TAILQ_ENTRY(pf_kstate)	 sync_list;
941211cddf9SKristof Provost 	TAILQ_ENTRY(pf_kstate)	 key_list[2];
942211cddf9SKristof Provost 	LIST_ENTRY(pf_kstate)	 entry;
9433b3a8eb9SGleb Smirnoff 	struct pf_state_peer	 src;
9443b3a8eb9SGleb Smirnoff 	struct pf_state_peer	 dst;
945e86bddeaSKristof Provost 	union pf_krule_ptr	 rule;
946e86bddeaSKristof Provost 	union pf_krule_ptr	 anchor;
947e86bddeaSKristof Provost 	union pf_krule_ptr	 nat_rule;
9483b3a8eb9SGleb Smirnoff 	struct pf_addr		 rt_addr;
9493b3a8eb9SGleb Smirnoff 	struct pf_state_key	*key[2];	/* addresses stack and wire  */
950320c1116SKristof Provost 	struct pfi_kkif		*kif;
951d0fdf2b2SKristof Provost 	struct pfi_kkif		*orig_kif;	/* The real kif, even if we're a floating state (i.e. if == V_pfi_all). */
952320c1116SKristof Provost 	struct pfi_kkif		*rt_kif;
95317ad7334SKristof Provost 	struct pf_ksrc_node	*src_node;
95417ad7334SKristof Provost 	struct pf_ksrc_node	*nat_src_node;
95555cc305dSMateusz Guzik 	u_int64_t		 packets[2];
95655cc305dSMateusz Guzik 	u_int64_t		 bytes[2];
9573b3a8eb9SGleb Smirnoff 	u_int32_t		 creation;
9583b3a8eb9SGleb Smirnoff 	u_int32_t	 	 expire;
9593b3a8eb9SGleb Smirnoff 	u_int32_t		 pfsync_time;
960b64f7ce9SKristof Provost 	u_int16_t                qid;
961b64f7ce9SKristof Provost 	u_int16_t                pqid;
96263b3c1c7SKristof Provost 	u_int16_t		 dnpipe;
96363b3c1c7SKristof Provost 	u_int16_t		 dnrpipe;
9643b3a8eb9SGleb Smirnoff 	u_int16_t		 tag;
9653b3a8eb9SGleb Smirnoff 	u_int8_t		 log;
9663b3a8eb9SGleb Smirnoff };
967e6dd0e2eSMateusz Guzik 
968e6dd0e2eSMateusz Guzik /*
969e6dd0e2eSMateusz Guzik  * Size <= fits 13 objects per page on LP64. Try to not grow the struct beyond that.
970e6dd0e2eSMateusz Guzik  */
971211cddf9SKristof Provost _Static_assert(sizeof(struct pf_kstate) <= 312, "pf_kstate size crosses 312 bytes");
972e86bddeaSKristof Provost #endif
9733b3a8eb9SGleb Smirnoff 
9743b3a8eb9SGleb Smirnoff /*
9753b3a8eb9SGleb Smirnoff  * Unified state structures for pulling states out of the kernel
9763b3a8eb9SGleb Smirnoff  * used by pfsync(4) and the pf(4) ioctl.
9773b3a8eb9SGleb Smirnoff  */
9783b3a8eb9SGleb Smirnoff struct pfsync_state_scrub {
9793b3a8eb9SGleb Smirnoff 	u_int16_t	pfss_flags;
9803b3a8eb9SGleb Smirnoff 	u_int8_t	pfss_ttl;	/* stashed TTL		*/
9813b3a8eb9SGleb Smirnoff #define PFSYNC_SCRUB_FLAG_VALID		0x01
9823b3a8eb9SGleb Smirnoff 	u_int8_t	scrub_flag;
9833b3a8eb9SGleb Smirnoff 	u_int32_t	pfss_ts_mod;	/* timestamp modulation	*/
9843b3a8eb9SGleb Smirnoff } __packed;
9853b3a8eb9SGleb Smirnoff 
9863b3a8eb9SGleb Smirnoff struct pfsync_state_peer {
9873b3a8eb9SGleb Smirnoff 	struct pfsync_state_scrub scrub;	/* state is scrubbed	*/
9883b3a8eb9SGleb Smirnoff 	u_int32_t	seqlo;		/* Max sequence number sent	*/
9893b3a8eb9SGleb Smirnoff 	u_int32_t	seqhi;		/* Max the other end ACKd + win	*/
9903b3a8eb9SGleb Smirnoff 	u_int32_t	seqdiff;	/* Sequence number modulator	*/
9913b3a8eb9SGleb Smirnoff 	u_int16_t	max_win;	/* largest window (pre scaling)	*/
9923b3a8eb9SGleb Smirnoff 	u_int16_t	mss;		/* Maximum segment size option	*/
9933b3a8eb9SGleb Smirnoff 	u_int8_t	state;		/* active state level		*/
9943b3a8eb9SGleb Smirnoff 	u_int8_t	wscale;		/* window scaling factor	*/
9953b3a8eb9SGleb Smirnoff 	u_int8_t	pad[6];
9963b3a8eb9SGleb Smirnoff } __packed;
9973b3a8eb9SGleb Smirnoff 
9983b3a8eb9SGleb Smirnoff struct pfsync_state_key {
9993b3a8eb9SGleb Smirnoff 	struct pf_addr	 addr[2];
10003b3a8eb9SGleb Smirnoff 	u_int16_t	 port[2];
10013b3a8eb9SGleb Smirnoff };
10023b3a8eb9SGleb Smirnoff 
10033b3a8eb9SGleb Smirnoff struct pfsync_state {
10043b3a8eb9SGleb Smirnoff 	u_int64_t	 id;
10053b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
10063b3a8eb9SGleb Smirnoff 	struct pfsync_state_key	key[2];
10073b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer src;
10083b3a8eb9SGleb Smirnoff 	struct pfsync_state_peer dst;
10093b3a8eb9SGleb Smirnoff 	struct pf_addr	 rt_addr;
10103b3a8eb9SGleb Smirnoff 	u_int32_t	 rule;
10113b3a8eb9SGleb Smirnoff 	u_int32_t	 anchor;
10123b3a8eb9SGleb Smirnoff 	u_int32_t	 nat_rule;
10133b3a8eb9SGleb Smirnoff 	u_int32_t	 creation;
10143b3a8eb9SGleb Smirnoff 	u_int32_t	 expire;
10153b3a8eb9SGleb Smirnoff 	u_int32_t	 packets[2][2];
10163b3a8eb9SGleb Smirnoff 	u_int32_t	 bytes[2][2];
10173b3a8eb9SGleb Smirnoff 	u_int32_t	 creatorid;
10183b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
10193b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
10203b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
10213b3a8eb9SGleb Smirnoff 	u_int8_t	 __spare[2];
10223b3a8eb9SGleb Smirnoff 	u_int8_t	 log;
10233b3a8eb9SGleb Smirnoff 	u_int8_t	 state_flags;
10243b3a8eb9SGleb Smirnoff 	u_int8_t	 timeout;
10253b3a8eb9SGleb Smirnoff 	u_int8_t	 sync_flags;
10263b3a8eb9SGleb Smirnoff 	u_int8_t	 updates;
10273b3a8eb9SGleb Smirnoff } __packed;
10283b3a8eb9SGleb Smirnoff 
10293b3a8eb9SGleb Smirnoff #ifdef _KERNEL
10303b3a8eb9SGleb Smirnoff /* pfsync */
10313b3a8eb9SGleb Smirnoff typedef int		pfsync_state_import_t(struct pfsync_state *, u_int8_t);
1032211cddf9SKristof Provost typedef	void		pfsync_insert_state_t(struct pf_kstate *);
1033211cddf9SKristof Provost typedef	void		pfsync_update_state_t(struct pf_kstate *);
1034211cddf9SKristof Provost typedef	void		pfsync_delete_state_t(struct pf_kstate *);
10353b3a8eb9SGleb Smirnoff typedef void		pfsync_clear_states_t(u_int32_t, const char *);
1036211cddf9SKristof Provost typedef int		pfsync_defer_t(struct pf_kstate *, struct mbuf *);
1037fbbf436dSKristof Provost typedef void		pfsync_detach_ifnet_t(struct ifnet *);
10383b3a8eb9SGleb Smirnoff 
10395f6cf24eSKristof Provost VNET_DECLARE(pfsync_state_import_t *, pfsync_state_import_ptr);
10405f6cf24eSKristof Provost #define V_pfsync_state_import_ptr	VNET(pfsync_state_import_ptr)
10415f6cf24eSKristof Provost VNET_DECLARE(pfsync_insert_state_t *, pfsync_insert_state_ptr);
10425f6cf24eSKristof Provost #define V_pfsync_insert_state_ptr	VNET(pfsync_insert_state_ptr)
10435f6cf24eSKristof Provost VNET_DECLARE(pfsync_update_state_t *, pfsync_update_state_ptr);
10445f6cf24eSKristof Provost #define V_pfsync_update_state_ptr	VNET(pfsync_update_state_ptr)
10455f6cf24eSKristof Provost VNET_DECLARE(pfsync_delete_state_t *, pfsync_delete_state_ptr);
10465f6cf24eSKristof Provost #define V_pfsync_delete_state_ptr	VNET(pfsync_delete_state_ptr)
10475f6cf24eSKristof Provost VNET_DECLARE(pfsync_clear_states_t *, pfsync_clear_states_ptr);
10485f6cf24eSKristof Provost #define V_pfsync_clear_states_ptr	VNET(pfsync_clear_states_ptr)
10495f6cf24eSKristof Provost VNET_DECLARE(pfsync_defer_t *, pfsync_defer_ptr);
10505f6cf24eSKristof Provost #define V_pfsync_defer_ptr		VNET(pfsync_defer_ptr)
1051fbbf436dSKristof Provost extern pfsync_detach_ifnet_t	*pfsync_detach_ifnet_ptr;
10523b3a8eb9SGleb Smirnoff 
10533b3a8eb9SGleb Smirnoff void			pfsync_state_export(struct pfsync_state *,
1054211cddf9SKristof Provost 			    struct pf_kstate *);
1055c6bf20a2SKristof Provost void			pf_state_export(struct pf_state_export *,
1056c6bf20a2SKristof Provost 			    struct pf_kstate *);
10573b3a8eb9SGleb Smirnoff 
10583b3a8eb9SGleb Smirnoff /* pflog */
1059e86bddeaSKristof Provost struct pf_kruleset;
10603b3a8eb9SGleb Smirnoff struct pf_pdesc;
1061320c1116SKristof Provost typedef int pflog_packet_t(struct pfi_kkif *, struct mbuf *, sa_family_t,
1062e86bddeaSKristof Provost     u_int8_t, u_int8_t, struct pf_krule *, struct pf_krule *,
1063e86bddeaSKristof Provost     struct pf_kruleset *, struct pf_pdesc *, int);
10643b3a8eb9SGleb Smirnoff extern pflog_packet_t		*pflog_packet_ptr;
10653b3a8eb9SGleb Smirnoff 
10663b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
10673b3a8eb9SGleb Smirnoff 
10683b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_SRCNODE	0x04
10693b3a8eb9SGleb Smirnoff #define	PFSYNC_FLAG_NATSRCNODE	0x08
10703b3a8eb9SGleb Smirnoff 
10713b3a8eb9SGleb Smirnoff /* for copies to/from network byte order */
10723b3a8eb9SGleb Smirnoff /* ioctl interface also uses network byte order */
10733b3a8eb9SGleb Smirnoff #define pf_state_peer_hton(s,d) do {		\
10743b3a8eb9SGleb Smirnoff 	(d)->seqlo = htonl((s)->seqlo);		\
10753b3a8eb9SGleb Smirnoff 	(d)->seqhi = htonl((s)->seqhi);		\
10763b3a8eb9SGleb Smirnoff 	(d)->seqdiff = htonl((s)->seqdiff);	\
10773b3a8eb9SGleb Smirnoff 	(d)->max_win = htons((s)->max_win);	\
10783b3a8eb9SGleb Smirnoff 	(d)->mss = htons((s)->mss);		\
10793b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
10803b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
10813b3a8eb9SGleb Smirnoff 	if ((s)->scrub) {						\
10823b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_flags = 				\
10833b3a8eb9SGleb Smirnoff 		    htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP);	\
10843b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl;		\
10853b3a8eb9SGleb Smirnoff 		(d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
10863b3a8eb9SGleb Smirnoff 		(d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID;	\
10873b3a8eb9SGleb Smirnoff 	}								\
10883b3a8eb9SGleb Smirnoff } while (0)
10893b3a8eb9SGleb Smirnoff 
10903b3a8eb9SGleb Smirnoff #define pf_state_peer_ntoh(s,d) do {		\
10913b3a8eb9SGleb Smirnoff 	(d)->seqlo = ntohl((s)->seqlo);		\
10923b3a8eb9SGleb Smirnoff 	(d)->seqhi = ntohl((s)->seqhi);		\
10933b3a8eb9SGleb Smirnoff 	(d)->seqdiff = ntohl((s)->seqdiff);	\
10943b3a8eb9SGleb Smirnoff 	(d)->max_win = ntohs((s)->max_win);	\
10953b3a8eb9SGleb Smirnoff 	(d)->mss = ntohs((s)->mss);		\
10963b3a8eb9SGleb Smirnoff 	(d)->state = (s)->state;		\
10973b3a8eb9SGleb Smirnoff 	(d)->wscale = (s)->wscale;		\
10983b3a8eb9SGleb Smirnoff 	if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && 	\
10993b3a8eb9SGleb Smirnoff 	    (d)->scrub != NULL) {					\
11003b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_flags =				\
11013b3a8eb9SGleb Smirnoff 		    ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP;	\
11023b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl;		\
11033b3a8eb9SGleb Smirnoff 		(d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
11043b3a8eb9SGleb Smirnoff 	}								\
11053b3a8eb9SGleb Smirnoff } while (0)
11063b3a8eb9SGleb Smirnoff 
11073b3a8eb9SGleb Smirnoff #define pf_state_counter_hton(s,d) do {				\
11083b3a8eb9SGleb Smirnoff 	d[0] = htonl((s>>32)&0xffffffff);			\
11093b3a8eb9SGleb Smirnoff 	d[1] = htonl(s&0xffffffff);				\
11103b3a8eb9SGleb Smirnoff } while (0)
11113b3a8eb9SGleb Smirnoff 
11123b3a8eb9SGleb Smirnoff #define pf_state_counter_from_pfsync(s)				\
11133b3a8eb9SGleb Smirnoff 	(((u_int64_t)(s[0])<<32) | (u_int64_t)(s[1]))
11143b3a8eb9SGleb Smirnoff 
11153b3a8eb9SGleb Smirnoff #define pf_state_counter_ntoh(s,d) do {				\
11163b3a8eb9SGleb Smirnoff 	d = ntohl(s[0]);					\
11173b3a8eb9SGleb Smirnoff 	d = d<<32;						\
11183b3a8eb9SGleb Smirnoff 	d += ntohl(s[1]);					\
11193b3a8eb9SGleb Smirnoff } while (0)
11203b3a8eb9SGleb Smirnoff 
1121e86bddeaSKristof Provost TAILQ_HEAD(pf_krulequeue, pf_krule);
11223b3a8eb9SGleb Smirnoff 
1123e86bddeaSKristof Provost struct pf_kanchor;
11243b3a8eb9SGleb Smirnoff 
1125e86bddeaSKristof Provost struct pf_kruleset {
11263b3a8eb9SGleb Smirnoff 	struct {
1127e86bddeaSKristof Provost 		struct pf_krulequeue	 queues[2];
11283b3a8eb9SGleb Smirnoff 		struct {
1129e86bddeaSKristof Provost 			struct pf_krulequeue	*ptr;
1130e86bddeaSKristof Provost 			struct pf_krule		**ptr_array;
11313b3a8eb9SGleb Smirnoff 			u_int32_t		 rcount;
11323b3a8eb9SGleb Smirnoff 			u_int32_t		 ticket;
11333b3a8eb9SGleb Smirnoff 			int			 open;
11343b3a8eb9SGleb Smirnoff 		}			 active, inactive;
11353b3a8eb9SGleb Smirnoff 	}			 rules[PF_RULESET_MAX];
1136e86bddeaSKristof Provost 	struct pf_kanchor	*anchor;
11373b3a8eb9SGleb Smirnoff 	u_int32_t		 tticket;
11383b3a8eb9SGleb Smirnoff 	int			 tables;
11393b3a8eb9SGleb Smirnoff 	int			 topen;
11403b3a8eb9SGleb Smirnoff };
11413b3a8eb9SGleb Smirnoff 
1142e86bddeaSKristof Provost RB_HEAD(pf_kanchor_global, pf_kanchor);
1143e86bddeaSKristof Provost RB_HEAD(pf_kanchor_node, pf_kanchor);
1144e86bddeaSKristof Provost struct pf_kanchor {
1145e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_global;
1146e86bddeaSKristof Provost 	RB_ENTRY(pf_kanchor)	 entry_node;
1147e86bddeaSKristof Provost 	struct pf_kanchor	*parent;
1148e86bddeaSKristof Provost 	struct pf_kanchor_node	 children;
11493b3a8eb9SGleb Smirnoff 	char			 name[PF_ANCHOR_NAME_SIZE];
11503b3a8eb9SGleb Smirnoff 	char			 path[MAXPATHLEN];
1151e86bddeaSKristof Provost 	struct pf_kruleset	 ruleset;
11523b3a8eb9SGleb Smirnoff 	int			 refcnt;	/* anchor rules */
11533b3a8eb9SGleb Smirnoff };
1154e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_global, pf_kanchor, entry_global, pf_anchor_compare);
1155e86bddeaSKristof Provost RB_PROTOTYPE(pf_kanchor_node, pf_kanchor, entry_node, pf_kanchor_compare);
11563b3a8eb9SGleb Smirnoff 
11573b3a8eb9SGleb Smirnoff #define PF_RESERVED_ANCHOR	"_pf"
11583b3a8eb9SGleb Smirnoff 
11593b3a8eb9SGleb Smirnoff #define PFR_TFLAG_PERSIST	0x00000001
11603b3a8eb9SGleb Smirnoff #define PFR_TFLAG_CONST		0x00000002
11613b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ACTIVE	0x00000004
11623b3a8eb9SGleb Smirnoff #define PFR_TFLAG_INACTIVE	0x00000008
11633b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFERENCED	0x00000010
11643b3a8eb9SGleb Smirnoff #define PFR_TFLAG_REFDANCHOR	0x00000020
11653b3a8eb9SGleb Smirnoff #define PFR_TFLAG_COUNTERS	0x00000040
11663b3a8eb9SGleb Smirnoff /* Adjust masks below when adding flags. */
11673b3a8eb9SGleb Smirnoff #define PFR_TFLAG_USRMASK	(PFR_TFLAG_PERSIST	| \
11683b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
11693b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
11703b3a8eb9SGleb Smirnoff #define PFR_TFLAG_SETMASK	(PFR_TFLAG_ACTIVE	| \
11713b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
11723b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
11733b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR)
11743b3a8eb9SGleb Smirnoff #define PFR_TFLAG_ALLMASK	(PFR_TFLAG_PERSIST	| \
11753b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_CONST	| \
11763b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_ACTIVE	| \
11773b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_INACTIVE	| \
11783b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFERENCED	| \
11793b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_REFDANCHOR	| \
11803b3a8eb9SGleb Smirnoff 				 PFR_TFLAG_COUNTERS)
11813b3a8eb9SGleb Smirnoff 
1182e86bddeaSKristof Provost struct pf_kanchor_stackframe;
1183c5131afeSKristof Provost struct pf_keth_anchor_stackframe;
11841d6139c0SGleb Smirnoff 
11853b3a8eb9SGleb Smirnoff struct pfr_table {
11863b3a8eb9SGleb Smirnoff 	char			 pfrt_anchor[MAXPATHLEN];
11873b3a8eb9SGleb Smirnoff 	char			 pfrt_name[PF_TABLE_NAME_SIZE];
11883b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrt_flags;
11893b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrt_fback;
11903b3a8eb9SGleb Smirnoff };
11913b3a8eb9SGleb Smirnoff 
11923b3a8eb9SGleb Smirnoff enum { PFR_FB_NONE, PFR_FB_MATCH, PFR_FB_ADDED, PFR_FB_DELETED,
11933b3a8eb9SGleb Smirnoff 	PFR_FB_CHANGED, PFR_FB_CLEARED, PFR_FB_DUPLICATE,
11943b3a8eb9SGleb Smirnoff 	PFR_FB_NOTMATCH, PFR_FB_CONFLICT, PFR_FB_NOCOUNT, PFR_FB_MAX };
11953b3a8eb9SGleb Smirnoff 
11963b3a8eb9SGleb Smirnoff struct pfr_addr {
11973b3a8eb9SGleb Smirnoff 	union {
11983b3a8eb9SGleb Smirnoff 		struct in_addr	 _pfra_ip4addr;
11993b3a8eb9SGleb Smirnoff 		struct in6_addr	 _pfra_ip6addr;
12003b3a8eb9SGleb Smirnoff 	}		 pfra_u;
12013b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_af;
12023b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_net;
12033b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_not;
12043b3a8eb9SGleb Smirnoff 	u_int8_t	 pfra_fback;
12053b3a8eb9SGleb Smirnoff };
12063b3a8eb9SGleb Smirnoff #define	pfra_ip4addr	pfra_u._pfra_ip4addr
12073b3a8eb9SGleb Smirnoff #define	pfra_ip6addr	pfra_u._pfra_ip6addr
12083b3a8eb9SGleb Smirnoff 
12093b3a8eb9SGleb Smirnoff enum { PFR_DIR_IN, PFR_DIR_OUT, PFR_DIR_MAX };
12103b3a8eb9SGleb Smirnoff enum { PFR_OP_BLOCK, PFR_OP_PASS, PFR_OP_ADDR_MAX, PFR_OP_TABLE_MAX };
1211c1be8399SMark Johnston enum { PFR_TYPE_PACKETS, PFR_TYPE_BYTES, PFR_TYPE_MAX };
1212c1be8399SMark Johnston #define	PFR_NUM_COUNTERS	(PFR_DIR_MAX * PFR_OP_ADDR_MAX * PFR_TYPE_MAX)
12133b3a8eb9SGleb Smirnoff #define PFR_OP_XPASS	PFR_OP_ADDR_MAX
12143b3a8eb9SGleb Smirnoff 
12153b3a8eb9SGleb Smirnoff struct pfr_astats {
12163b3a8eb9SGleb Smirnoff 	struct pfr_addr	 pfras_a;
12173b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_packets[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
12183b3a8eb9SGleb Smirnoff 	u_int64_t	 pfras_bytes[PFR_DIR_MAX][PFR_OP_ADDR_MAX];
12193b3a8eb9SGleb Smirnoff 	long		 pfras_tzero;
12203b3a8eb9SGleb Smirnoff };
12213b3a8eb9SGleb Smirnoff 
12223b3a8eb9SGleb Smirnoff enum { PFR_REFCNT_RULE, PFR_REFCNT_ANCHOR, PFR_REFCNT_MAX };
12233b3a8eb9SGleb Smirnoff 
12243b3a8eb9SGleb Smirnoff struct pfr_tstats {
12253b3a8eb9SGleb Smirnoff 	struct pfr_table pfrts_t;
12263b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
12273b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
12283b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_match;
12293b3a8eb9SGleb Smirnoff 	u_int64_t	 pfrts_nomatch;
12303b3a8eb9SGleb Smirnoff 	long		 pfrts_tzero;
12313b3a8eb9SGleb Smirnoff 	int		 pfrts_cnt;
12323b3a8eb9SGleb Smirnoff 	int		 pfrts_refcnt[PFR_REFCNT_MAX];
12333b3a8eb9SGleb Smirnoff };
123459048686SKristof Provost 
1235f92c21a2SMateusz Guzik #ifdef _KERNEL
1236f92c21a2SMateusz Guzik 
1237f92c21a2SMateusz Guzik struct pfr_kstate_counter {
1238f92c21a2SMateusz Guzik 	counter_u64_t	pkc_pcpu;
1239f92c21a2SMateusz Guzik 	u_int64_t	pkc_zero;
1240f92c21a2SMateusz Guzik };
1241f92c21a2SMateusz Guzik 
1242f92c21a2SMateusz Guzik static inline int
1243f92c21a2SMateusz Guzik pfr_kstate_counter_init(struct pfr_kstate_counter *pfrc, int flags)
1244f92c21a2SMateusz Guzik {
1245f92c21a2SMateusz Guzik 
1246f92c21a2SMateusz Guzik 	pfrc->pkc_zero = 0;
1247f92c21a2SMateusz Guzik 	pfrc->pkc_pcpu = counter_u64_alloc(flags);
1248f92c21a2SMateusz Guzik 	if (pfrc->pkc_pcpu == NULL)
1249f92c21a2SMateusz Guzik 		return (ENOMEM);
1250f92c21a2SMateusz Guzik 	return (0);
1251f92c21a2SMateusz Guzik }
1252f92c21a2SMateusz Guzik 
1253f92c21a2SMateusz Guzik static inline void
1254f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(struct pfr_kstate_counter *pfrc)
1255f92c21a2SMateusz Guzik {
1256f92c21a2SMateusz Guzik 
1257f92c21a2SMateusz Guzik 	counter_u64_free(pfrc->pkc_pcpu);
1258f92c21a2SMateusz Guzik }
1259f92c21a2SMateusz Guzik 
1260f92c21a2SMateusz Guzik static inline u_int64_t
1261f92c21a2SMateusz Guzik pfr_kstate_counter_fetch(struct pfr_kstate_counter *pfrc)
1262f92c21a2SMateusz Guzik {
1263f92c21a2SMateusz Guzik 	u_int64_t c;
1264f92c21a2SMateusz Guzik 
1265f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1266f92c21a2SMateusz Guzik 	c -= pfrc->pkc_zero;
1267f92c21a2SMateusz Guzik 	return (c);
1268f92c21a2SMateusz Guzik }
1269f92c21a2SMateusz Guzik 
1270f92c21a2SMateusz Guzik static inline void
1271f92c21a2SMateusz Guzik pfr_kstate_counter_zero(struct pfr_kstate_counter *pfrc)
1272f92c21a2SMateusz Guzik {
1273f92c21a2SMateusz Guzik 	u_int64_t c;
1274f92c21a2SMateusz Guzik 
1275f92c21a2SMateusz Guzik 	c = counter_u64_fetch(pfrc->pkc_pcpu);
1276f92c21a2SMateusz Guzik 	pfrc->pkc_zero = c;
1277f92c21a2SMateusz Guzik }
1278f92c21a2SMateusz Guzik 
1279f92c21a2SMateusz Guzik static inline void
1280f92c21a2SMateusz Guzik pfr_kstate_counter_add(struct pfr_kstate_counter *pfrc, int64_t n)
1281f92c21a2SMateusz Guzik {
1282f92c21a2SMateusz Guzik 
1283f92c21a2SMateusz Guzik 	counter_u64_add(pfrc->pkc_pcpu, n);
1284f92c21a2SMateusz Guzik }
1285f92c21a2SMateusz Guzik 
128659048686SKristof Provost struct pfr_ktstats {
128759048686SKristof Provost 	struct pfr_table pfrts_t;
1288f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_packets[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1289f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_bytes[PFR_DIR_MAX][PFR_OP_TABLE_MAX];
1290f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_match;
1291f92c21a2SMateusz Guzik 	struct pfr_kstate_counter	 pfrkts_nomatch;
129259048686SKristof Provost 	long		 pfrkts_tzero;
129359048686SKristof Provost 	int		 pfrkts_cnt;
129459048686SKristof Provost 	int		 pfrkts_refcnt[PFR_REFCNT_MAX];
129559048686SKristof Provost };
1296f92c21a2SMateusz Guzik 
1297f92c21a2SMateusz Guzik #endif /* _KERNEL */
1298f92c21a2SMateusz Guzik 
12993b3a8eb9SGleb Smirnoff #define	pfrts_name	pfrts_t.pfrt_name
13003b3a8eb9SGleb Smirnoff #define pfrts_flags	pfrts_t.pfrt_flags
13013b3a8eb9SGleb Smirnoff 
13023b3a8eb9SGleb Smirnoff #ifndef _SOCKADDR_UNION_DEFINED
13033b3a8eb9SGleb Smirnoff #define	_SOCKADDR_UNION_DEFINED
13043b3a8eb9SGleb Smirnoff union sockaddr_union {
13053b3a8eb9SGleb Smirnoff 	struct sockaddr		sa;
13063b3a8eb9SGleb Smirnoff 	struct sockaddr_in	sin;
13073b3a8eb9SGleb Smirnoff 	struct sockaddr_in6	sin6;
13083b3a8eb9SGleb Smirnoff };
13093b3a8eb9SGleb Smirnoff #endif /* _SOCKADDR_UNION_DEFINED */
13103b3a8eb9SGleb Smirnoff 
13113b3a8eb9SGleb Smirnoff struct pfr_kcounters {
1312c1be8399SMark Johnston 	counter_u64_t		 pfrkc_counters;
131359048686SKristof Provost 	long			 pfrkc_tzero;
13143b3a8eb9SGleb Smirnoff };
1315c1be8399SMark Johnston #define	pfr_kentry_counter(kc, dir, op, t)		\
1316c1be8399SMark Johnston 	((kc)->pfrkc_counters +				\
1317c1be8399SMark Johnston 	    (dir) * PFR_OP_ADDR_MAX * PFR_TYPE_MAX + (op) * PFR_TYPE_MAX + (t))
13183b3a8eb9SGleb Smirnoff 
1319e86bddeaSKristof Provost #ifdef _KERNEL
13203b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_kentryworkq, pfr_kentry);
13213b3a8eb9SGleb Smirnoff struct pfr_kentry {
13223b3a8eb9SGleb Smirnoff 	struct radix_node	 pfrke_node[2];
13233b3a8eb9SGleb Smirnoff 	union sockaddr_union	 pfrke_sa;
13243b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_kentry)	 pfrke_workq;
132559048686SKristof Provost 	struct pfr_kcounters	 pfrke_counters;
13263b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_af;
13273b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_net;
13283b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_not;
13293b3a8eb9SGleb Smirnoff 	u_int8_t		 pfrke_mark;
13303b3a8eb9SGleb Smirnoff };
13313b3a8eb9SGleb Smirnoff 
13323b3a8eb9SGleb Smirnoff SLIST_HEAD(pfr_ktableworkq, pfr_ktable);
13333b3a8eb9SGleb Smirnoff RB_HEAD(pfr_ktablehead, pfr_ktable);
13343b3a8eb9SGleb Smirnoff struct pfr_ktable {
133559048686SKristof Provost 	struct pfr_ktstats	 pfrkt_kts;
13363b3a8eb9SGleb Smirnoff 	RB_ENTRY(pfr_ktable)	 pfrkt_tree;
13373b3a8eb9SGleb Smirnoff 	SLIST_ENTRY(pfr_ktable)	 pfrkt_workq;
13383b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip4;
13393b3a8eb9SGleb Smirnoff 	struct radix_node_head	*pfrkt_ip6;
13403b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_shadow;
13413b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*pfrkt_root;
1342e86bddeaSKristof Provost 	struct pf_kruleset	*pfrkt_rs;
13433b3a8eb9SGleb Smirnoff 	long			 pfrkt_larg;
13443b3a8eb9SGleb Smirnoff 	int			 pfrkt_nflags;
13453b3a8eb9SGleb Smirnoff };
134659048686SKristof Provost #define pfrkt_t		pfrkt_kts.pfrts_t
13473b3a8eb9SGleb Smirnoff #define pfrkt_name	pfrkt_t.pfrt_name
13483b3a8eb9SGleb Smirnoff #define pfrkt_anchor	pfrkt_t.pfrt_anchor
13493b3a8eb9SGleb Smirnoff #define pfrkt_ruleset	pfrkt_t.pfrt_ruleset
13503b3a8eb9SGleb Smirnoff #define pfrkt_flags	pfrkt_t.pfrt_flags
135159048686SKristof Provost #define pfrkt_cnt	pfrkt_kts.pfrkts_cnt
135259048686SKristof Provost #define pfrkt_refcnt	pfrkt_kts.pfrkts_refcnt
135359048686SKristof Provost #define pfrkt_packets	pfrkt_kts.pfrkts_packets
135459048686SKristof Provost #define pfrkt_bytes	pfrkt_kts.pfrkts_bytes
135559048686SKristof Provost #define pfrkt_match	pfrkt_kts.pfrkts_match
135659048686SKristof Provost #define pfrkt_nomatch	pfrkt_kts.pfrkts_nomatch
135759048686SKristof Provost #define pfrkt_tzero	pfrkt_kts.pfrkts_tzero
1358e86bddeaSKristof Provost #endif
13593b3a8eb9SGleb Smirnoff 
1360320c1116SKristof Provost #ifdef _KERNEL
1361320c1116SKristof Provost struct pfi_kkif {
13623b3a8eb9SGleb Smirnoff 	char				 pfik_name[IFNAMSIZ];
13633b3a8eb9SGleb Smirnoff 	union {
1364320c1116SKristof Provost 		RB_ENTRY(pfi_kkif)	 _pfik_tree;
1365320c1116SKristof Provost 		LIST_ENTRY(pfi_kkif)	 _pfik_list;
13663b3a8eb9SGleb Smirnoff 	} _pfik_glue;
13673b3a8eb9SGleb Smirnoff #define	pfik_tree	_pfik_glue._pfik_tree
13683b3a8eb9SGleb Smirnoff #define	pfik_list	_pfik_glue._pfik_list
1369d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_packets[2][2][2];
1370d40d4b3eSMateusz Guzik 	struct pf_counter_u64		 pfik_bytes[2][2][2];
13713b3a8eb9SGleb Smirnoff 	u_int32_t			 pfik_tzero;
13723b3a8eb9SGleb Smirnoff 	u_int				 pfik_flags;
13733b3a8eb9SGleb Smirnoff 	struct ifnet			*pfik_ifp;
13743b3a8eb9SGleb Smirnoff 	struct ifg_group		*pfik_group;
13753b3a8eb9SGleb Smirnoff 	u_int				 pfik_rulerefs;
13763b3a8eb9SGleb Smirnoff 	TAILQ_HEAD(, pfi_dynaddr)	 pfik_dynaddrs;
1377d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
1378d40d4b3eSMateusz Guzik 	LIST_ENTRY(pfi_kkif)		 pfik_allkiflist;
1379d40d4b3eSMateusz Guzik #endif
13803b3a8eb9SGleb Smirnoff };
1381320c1116SKristof Provost #endif
13823b3a8eb9SGleb Smirnoff 
13833b3a8eb9SGleb Smirnoff #define	PFI_IFLAG_REFS		0x0001	/* has state references */
13843b3a8eb9SGleb Smirnoff #define PFI_IFLAG_SKIP		0x0100	/* skip filtering on interface */
13853b3a8eb9SGleb Smirnoff 
138629698ed9SKristof Provost #ifdef _KERNEL
13873b3a8eb9SGleb Smirnoff struct pf_pdesc {
13883b3a8eb9SGleb Smirnoff 	struct {
13893b3a8eb9SGleb Smirnoff 		int	 done;
13903b3a8eb9SGleb Smirnoff 		uid_t	 uid;
13913b3a8eb9SGleb Smirnoff 		gid_t	 gid;
13923b3a8eb9SGleb Smirnoff 	}		 lookup;
13933b3a8eb9SGleb Smirnoff 	u_int64_t	 tot_len;	/* Make Mickey money */
1394d38630f6SKristof Provost 	union pf_headers {
1395d38630f6SKristof Provost 		struct tcphdr		tcp;
1396d38630f6SKristof Provost 		struct udphdr		udp;
1397d38630f6SKristof Provost 		struct icmp		icmp;
13983b3a8eb9SGleb Smirnoff #ifdef INET6
1399d38630f6SKristof Provost 		struct icmp6_hdr	icmp6;
14003b3a8eb9SGleb Smirnoff #endif /* INET6 */
1401d38630f6SKristof Provost 		char any[0];
14023b3a8eb9SGleb Smirnoff 	} hdr;
14033b3a8eb9SGleb Smirnoff 
1404e86bddeaSKristof Provost 	struct pf_krule	*nat_rule;	/* nat/rdr rule applied to packet */
14053b3a8eb9SGleb Smirnoff 	struct pf_addr	*src;		/* src address */
14063b3a8eb9SGleb Smirnoff 	struct pf_addr	*dst;		/* dst address */
14073b3a8eb9SGleb Smirnoff 	u_int16_t *sport;
14083b3a8eb9SGleb Smirnoff 	u_int16_t *dport;
14093b3a8eb9SGleb Smirnoff 	struct pf_mtag	*pf_mtag;
1410ef950daaSKristof Provost 	struct pf_rule_actions	act;
14113b3a8eb9SGleb Smirnoff 
14123b3a8eb9SGleb Smirnoff 	u_int32_t	 p_len;		/* total length of payload */
14133b3a8eb9SGleb Smirnoff 
14143b3a8eb9SGleb Smirnoff 	u_int16_t	*ip_sum;
14153b3a8eb9SGleb Smirnoff 	u_int16_t	*proto_sum;
14163b3a8eb9SGleb Smirnoff 	u_int16_t	 flags;		/* Let SCRUB trigger behavior in
14173b3a8eb9SGleb Smirnoff 					 * state code. Easier than tags */
14183b3a8eb9SGleb Smirnoff #define PFDESC_TCP_NORM	0x0001		/* TCP shall be statefully scrubbed */
14193b3a8eb9SGleb Smirnoff #define PFDESC_IP_REAS	0x0002		/* IP frags would've been reassembled */
14203b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
14213b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
14223b3a8eb9SGleb Smirnoff 	u_int8_t	 tos;
14233b3a8eb9SGleb Smirnoff 	u_int8_t	 dir;		/* direction */
14243b3a8eb9SGleb Smirnoff 	u_int8_t	 sidx;		/* key index for source */
14253b3a8eb9SGleb Smirnoff 	u_int8_t	 didx;		/* key index for destination */
14263b3a8eb9SGleb Smirnoff };
142729698ed9SKristof Provost #endif
14283b3a8eb9SGleb Smirnoff 
14293b3a8eb9SGleb Smirnoff /* flags for RDR options */
14303b3a8eb9SGleb Smirnoff #define PF_DPORT_RANGE	0x01		/* Dest port uses range */
14313b3a8eb9SGleb Smirnoff #define PF_RPORT_RANGE	0x02		/* RDR'ed port uses range */
14323b3a8eb9SGleb Smirnoff 
14333b3a8eb9SGleb Smirnoff /* UDP state enumeration */
14343b3a8eb9SGleb Smirnoff #define PFUDPS_NO_TRAFFIC	0
14353b3a8eb9SGleb Smirnoff #define PFUDPS_SINGLE		1
14363b3a8eb9SGleb Smirnoff #define PFUDPS_MULTIPLE		2
14373b3a8eb9SGleb Smirnoff 
14383b3a8eb9SGleb Smirnoff #define PFUDPS_NSTATES		3	/* number of state levels */
14393b3a8eb9SGleb Smirnoff 
14403b3a8eb9SGleb Smirnoff #define PFUDPS_NAMES { \
14413b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
14423b3a8eb9SGleb Smirnoff 	"SINGLE", \
14433b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
14443b3a8eb9SGleb Smirnoff 	NULL \
14453b3a8eb9SGleb Smirnoff }
14463b3a8eb9SGleb Smirnoff 
14473b3a8eb9SGleb Smirnoff /* Other protocol state enumeration */
14483b3a8eb9SGleb Smirnoff #define PFOTHERS_NO_TRAFFIC	0
14493b3a8eb9SGleb Smirnoff #define PFOTHERS_SINGLE		1
14503b3a8eb9SGleb Smirnoff #define PFOTHERS_MULTIPLE	2
14513b3a8eb9SGleb Smirnoff 
14523b3a8eb9SGleb Smirnoff #define PFOTHERS_NSTATES	3	/* number of state levels */
14533b3a8eb9SGleb Smirnoff 
14543b3a8eb9SGleb Smirnoff #define PFOTHERS_NAMES { \
14553b3a8eb9SGleb Smirnoff 	"NO_TRAFFIC", \
14563b3a8eb9SGleb Smirnoff 	"SINGLE", \
14573b3a8eb9SGleb Smirnoff 	"MULTIPLE", \
14583b3a8eb9SGleb Smirnoff 	NULL \
14593b3a8eb9SGleb Smirnoff }
14603b3a8eb9SGleb Smirnoff 
14613b3a8eb9SGleb Smirnoff #define ACTION_SET(a, x) \
14623b3a8eb9SGleb Smirnoff 	do { \
14633b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
14643b3a8eb9SGleb Smirnoff 			*(a) = (x); \
14653b3a8eb9SGleb Smirnoff 	} while (0)
14663b3a8eb9SGleb Smirnoff 
14673b3a8eb9SGleb Smirnoff #define REASON_SET(a, x) \
14683b3a8eb9SGleb Smirnoff 	do { \
14693b3a8eb9SGleb Smirnoff 		if ((a) != NULL) \
14703b3a8eb9SGleb Smirnoff 			*(a) = (x); \
14713b3a8eb9SGleb Smirnoff 		if (x < PFRES_MAX) \
1472a9572d8fSGleb Smirnoff 			counter_u64_add(V_pf_status.counters[x], 1); \
14733b3a8eb9SGleb Smirnoff 	} while (0)
14743b3a8eb9SGleb Smirnoff 
14758e1864edSKristof Provost enum pf_syncookies_mode {
14768e1864edSKristof Provost 	PF_SYNCOOKIES_NEVER = 0,
14778e1864edSKristof Provost 	PF_SYNCOOKIES_ALWAYS = 1,
1478bf863718SKristof Provost 	PF_SYNCOOKIES_ADAPTIVE = 2,
1479bf863718SKristof Provost 	PF_SYNCOOKIES_MODE_MAX = PF_SYNCOOKIES_ADAPTIVE
14808e1864edSKristof Provost };
14818e1864edSKristof Provost 
14825062afffSKristof Provost #define	PF_SYNCOOKIES_HIWATPCT	25
14835062afffSKristof Provost #define	PF_SYNCOOKIES_LOWATPCT	(PF_SYNCOOKIES_HIWATPCT / 2)
14845062afffSKristof Provost 
1485d9cc6ea2SMateusz Guzik #ifdef _KERNEL
1486a9572d8fSGleb Smirnoff struct pf_kstatus {
1487a9572d8fSGleb Smirnoff 	counter_u64_t	counters[PFRES_MAX]; /* reason for passing/dropping */
14884cab80a8SKristof Provost 	counter_u64_t	lcounters[KLCNT_MAX]; /* limit counters */
1489fc4c42ceSMateusz Guzik 	struct pf_counter_u64	fcounters[FCNT_MAX]; /* state operation counters */
1490a9572d8fSGleb Smirnoff 	counter_u64_t	scounters[SCNT_MAX]; /* src_node operation counters */
1491a9572d8fSGleb Smirnoff 	uint32_t	states;
1492a9572d8fSGleb Smirnoff 	uint32_t	src_nodes;
1493a9572d8fSGleb Smirnoff 	uint32_t	running;
1494a9572d8fSGleb Smirnoff 	uint32_t	since;
1495a9572d8fSGleb Smirnoff 	uint32_t	debug;
1496a9572d8fSGleb Smirnoff 	uint32_t	hostid;
14973b3a8eb9SGleb Smirnoff 	char		ifname[IFNAMSIZ];
1498a9572d8fSGleb Smirnoff 	uint8_t		pf_chksum[PF_MD5_DIGEST_LENGTH];
149942ec75f8SKristof Provost 	bool		keep_counters;
15008e1864edSKristof Provost 	enum pf_syncookies_mode	syncookies_mode;
15018e1864edSKristof Provost 	bool		syncookies_active;
1502bf863718SKristof Provost 	uint64_t	syncookies_inflight[2];
1503bf863718SKristof Provost 	uint32_t	states_halfopen;
15043b3a8eb9SGleb Smirnoff };
1505d9cc6ea2SMateusz Guzik #endif
15063b3a8eb9SGleb Smirnoff 
15073b3a8eb9SGleb Smirnoff struct pf_divert {
15083b3a8eb9SGleb Smirnoff 	union {
15093b3a8eb9SGleb Smirnoff 		struct in_addr	ipv4;
15103b3a8eb9SGleb Smirnoff 		struct in6_addr	ipv6;
15113b3a8eb9SGleb Smirnoff 	}		addr;
15123b3a8eb9SGleb Smirnoff 	u_int16_t	port;
15133b3a8eb9SGleb Smirnoff };
15143b3a8eb9SGleb Smirnoff 
15153b3a8eb9SGleb Smirnoff #define PFFRAG_FRENT_HIWAT	5000	/* Number of fragment entries */
15163b3a8eb9SGleb Smirnoff #define PFR_KENTRY_HIWAT	200000	/* Number of table entries */
15173b3a8eb9SGleb Smirnoff 
15183b3a8eb9SGleb Smirnoff /*
1519fd2ea405SKristof Provost  * Limit the length of the fragment queue traversal.  Remember
1520fd2ea405SKristof Provost  * search entry points based on the fragment offset.
1521fd2ea405SKristof Provost  */
1522fd2ea405SKristof Provost #define PF_FRAG_ENTRY_POINTS		16
1523fd2ea405SKristof Provost 
1524fd2ea405SKristof Provost /*
1525790194cdSKristof Provost  * The number of entries in the fragment queue must be limited
1526790194cdSKristof Provost  * to avoid DoS by linear seaching.  Instead of a global limit,
1527790194cdSKristof Provost  * use a limit per entry point.  For large packets these sum up.
1528790194cdSKristof Provost  */
1529790194cdSKristof Provost #define PF_FRAG_ENTRY_LIMIT		64
1530790194cdSKristof Provost 
1531790194cdSKristof Provost /*
15323b3a8eb9SGleb Smirnoff  * ioctl parameter structures
15333b3a8eb9SGleb Smirnoff  */
15343b3a8eb9SGleb Smirnoff 
15353b3a8eb9SGleb Smirnoff struct pfioc_pooladdr {
15363b3a8eb9SGleb Smirnoff 	u_int32_t		 action;
15373b3a8eb9SGleb Smirnoff 	u_int32_t		 ticket;
15383b3a8eb9SGleb Smirnoff 	u_int32_t		 nr;
15393b3a8eb9SGleb Smirnoff 	u_int32_t		 r_num;
15403b3a8eb9SGleb Smirnoff 	u_int8_t		 r_action;
15413b3a8eb9SGleb Smirnoff 	u_int8_t		 r_last;
15423b3a8eb9SGleb Smirnoff 	u_int8_t		 af;
15433b3a8eb9SGleb Smirnoff 	char			 anchor[MAXPATHLEN];
15443b3a8eb9SGleb Smirnoff 	struct pf_pooladdr	 addr;
15453b3a8eb9SGleb Smirnoff };
15463b3a8eb9SGleb Smirnoff 
15473b3a8eb9SGleb Smirnoff struct pfioc_rule {
15483b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
15493b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
15503b3a8eb9SGleb Smirnoff 	u_int32_t	 pool_ticket;
15513b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
15523b3a8eb9SGleb Smirnoff 	char		 anchor[MAXPATHLEN];
15533b3a8eb9SGleb Smirnoff 	char		 anchor_call[MAXPATHLEN];
15543b3a8eb9SGleb Smirnoff 	struct pf_rule	 rule;
15553b3a8eb9SGleb Smirnoff };
15563b3a8eb9SGleb Smirnoff 
15573b3a8eb9SGleb Smirnoff struct pfioc_natlook {
15583b3a8eb9SGleb Smirnoff 	struct pf_addr	 saddr;
15593b3a8eb9SGleb Smirnoff 	struct pf_addr	 daddr;
15603b3a8eb9SGleb Smirnoff 	struct pf_addr	 rsaddr;
15613b3a8eb9SGleb Smirnoff 	struct pf_addr	 rdaddr;
15623b3a8eb9SGleb Smirnoff 	u_int16_t	 sport;
15633b3a8eb9SGleb Smirnoff 	u_int16_t	 dport;
15643b3a8eb9SGleb Smirnoff 	u_int16_t	 rsport;
15653b3a8eb9SGleb Smirnoff 	u_int16_t	 rdport;
15663b3a8eb9SGleb Smirnoff 	sa_family_t	 af;
15673b3a8eb9SGleb Smirnoff 	u_int8_t	 proto;
15683b3a8eb9SGleb Smirnoff 	u_int8_t	 direction;
15693b3a8eb9SGleb Smirnoff };
15703b3a8eb9SGleb Smirnoff 
15713b3a8eb9SGleb Smirnoff struct pfioc_state {
15723b3a8eb9SGleb Smirnoff 	struct pfsync_state	state;
15733b3a8eb9SGleb Smirnoff };
15743b3a8eb9SGleb Smirnoff 
15753b3a8eb9SGleb Smirnoff struct pfioc_src_node_kill {
15763b3a8eb9SGleb Smirnoff 	sa_family_t psnk_af;
15773b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_src;
15783b3a8eb9SGleb Smirnoff 	struct pf_rule_addr psnk_dst;
15793b3a8eb9SGleb Smirnoff 	u_int		    psnk_killed;
15803b3a8eb9SGleb Smirnoff };
15813b3a8eb9SGleb Smirnoff 
15827606a45dSKristof Provost #ifdef _KERNEL
15837606a45dSKristof Provost struct pf_kstate_kill {
15847606a45dSKristof Provost 	struct pf_state_cmp	psk_pfcmp;
15857606a45dSKristof Provost 	sa_family_t		psk_af;
15867606a45dSKristof Provost 	int			psk_proto;
15877606a45dSKristof Provost 	struct pf_rule_addr	psk_src;
15887606a45dSKristof Provost 	struct pf_rule_addr	psk_dst;
1589abbcba9cSKristof Provost 	struct pf_rule_addr	psk_rt_addr;
15907606a45dSKristof Provost 	char			psk_ifname[IFNAMSIZ];
15917606a45dSKristof Provost 	char			psk_label[PF_RULE_LABEL_SIZE];
15927606a45dSKristof Provost 	u_int			psk_killed;
159393abcf17SKristof Provost 	bool			psk_kill_match;
15947606a45dSKristof Provost };
15957606a45dSKristof Provost #endif
15967606a45dSKristof Provost 
15973b3a8eb9SGleb Smirnoff struct pfioc_state_kill {
15983b3a8eb9SGleb Smirnoff 	struct pf_state_cmp	psk_pfcmp;
15993b3a8eb9SGleb Smirnoff 	sa_family_t		psk_af;
16003b3a8eb9SGleb Smirnoff 	int			psk_proto;
16013b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_src;
16023b3a8eb9SGleb Smirnoff 	struct pf_rule_addr	psk_dst;
16033b3a8eb9SGleb Smirnoff 	char			psk_ifname[IFNAMSIZ];
16043b3a8eb9SGleb Smirnoff 	char			psk_label[PF_RULE_LABEL_SIZE];
16053b3a8eb9SGleb Smirnoff 	u_int			psk_killed;
16063b3a8eb9SGleb Smirnoff };
16073b3a8eb9SGleb Smirnoff 
16083b3a8eb9SGleb Smirnoff struct pfioc_states {
16093b3a8eb9SGleb Smirnoff 	int	ps_len;
16103b3a8eb9SGleb Smirnoff 	union {
16113b3a8eb9SGleb Smirnoff 		caddr_t			 psu_buf;
16123b3a8eb9SGleb Smirnoff 		struct pfsync_state	*psu_states;
16133b3a8eb9SGleb Smirnoff 	} ps_u;
16143b3a8eb9SGleb Smirnoff #define ps_buf		ps_u.psu_buf
16153b3a8eb9SGleb Smirnoff #define ps_states	ps_u.psu_states
16163b3a8eb9SGleb Smirnoff };
16173b3a8eb9SGleb Smirnoff 
1618c6bf20a2SKristof Provost struct pfioc_states_v2 {
1619c6bf20a2SKristof Provost 	int		ps_len;
1620c6bf20a2SKristof Provost 	uint64_t	ps_req_version;
1621c6bf20a2SKristof Provost 	union {
1622c6bf20a2SKristof Provost 		caddr_t			 psu_buf;
1623c6bf20a2SKristof Provost 		struct pf_state_export	*psu_states;
1624c6bf20a2SKristof Provost 	} ps_u;
1625c6bf20a2SKristof Provost #define ps_buf		ps_u.psu_buf
1626c6bf20a2SKristof Provost #define ps_states	ps_u.psu_states
1627c6bf20a2SKristof Provost };
1628c6bf20a2SKristof Provost 
16293b3a8eb9SGleb Smirnoff struct pfioc_src_nodes {
16303b3a8eb9SGleb Smirnoff 	int	psn_len;
16313b3a8eb9SGleb Smirnoff 	union {
16323b3a8eb9SGleb Smirnoff 		caddr_t		 psu_buf;
16333b3a8eb9SGleb Smirnoff 		struct pf_src_node	*psu_src_nodes;
16343b3a8eb9SGleb Smirnoff 	} psn_u;
16353b3a8eb9SGleb Smirnoff #define psn_buf		psn_u.psu_buf
16363b3a8eb9SGleb Smirnoff #define psn_src_nodes	psn_u.psu_src_nodes
16373b3a8eb9SGleb Smirnoff };
16383b3a8eb9SGleb Smirnoff 
16393b3a8eb9SGleb Smirnoff struct pfioc_if {
16403b3a8eb9SGleb Smirnoff 	char		 ifname[IFNAMSIZ];
16413b3a8eb9SGleb Smirnoff };
16423b3a8eb9SGleb Smirnoff 
16433b3a8eb9SGleb Smirnoff struct pfioc_tm {
16443b3a8eb9SGleb Smirnoff 	int		 timeout;
16453b3a8eb9SGleb Smirnoff 	int		 seconds;
16463b3a8eb9SGleb Smirnoff };
16473b3a8eb9SGleb Smirnoff 
16483b3a8eb9SGleb Smirnoff struct pfioc_limit {
16493b3a8eb9SGleb Smirnoff 	int		 index;
16503b3a8eb9SGleb Smirnoff 	unsigned	 limit;
16513b3a8eb9SGleb Smirnoff };
16523b3a8eb9SGleb Smirnoff 
1653249cc75fSPatrick Kelsey struct pfioc_altq_v0 {
16543b3a8eb9SGleb Smirnoff 	u_int32_t	 action;
16553b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
16563b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
1657249cc75fSPatrick Kelsey 	struct pf_altq_v0 altq;
16583b3a8eb9SGleb Smirnoff };
16593b3a8eb9SGleb Smirnoff 
1660249cc75fSPatrick Kelsey struct pfioc_altq_v1 {
1661249cc75fSPatrick Kelsey 	u_int32_t	 action;
1662249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1663249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1664249cc75fSPatrick Kelsey 	/*
1665249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1666249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1667249cc75fSPatrick Kelsey 	 */
1668249cc75fSPatrick Kelsey 	u_int32_t	 version;
1669249cc75fSPatrick Kelsey 	struct pf_altq_v1 altq;
1670249cc75fSPatrick Kelsey };
1671249cc75fSPatrick Kelsey 
1672249cc75fSPatrick Kelsey /*
1673249cc75fSPatrick Kelsey  * Latest version of struct pfioc_altq_vX.  This must move in lock-step with
1674249cc75fSPatrick Kelsey  * the latest version of struct pf_altq_vX as it has that struct as a
1675249cc75fSPatrick Kelsey  * member.
1676249cc75fSPatrick Kelsey  */
1677249cc75fSPatrick Kelsey #define PFIOC_ALTQ_VERSION	PF_ALTQ_VERSION
1678249cc75fSPatrick Kelsey 
1679249cc75fSPatrick Kelsey struct pfioc_qstats_v0 {
16803b3a8eb9SGleb Smirnoff 	u_int32_t	 ticket;
16813b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
16823b3a8eb9SGleb Smirnoff 	void		*buf;
16833b3a8eb9SGleb Smirnoff 	int		 nbytes;
16843b3a8eb9SGleb Smirnoff 	u_int8_t	 scheduler;
16853b3a8eb9SGleb Smirnoff };
16863b3a8eb9SGleb Smirnoff 
1687249cc75fSPatrick Kelsey struct pfioc_qstats_v1 {
1688249cc75fSPatrick Kelsey 	u_int32_t	 ticket;
1689249cc75fSPatrick Kelsey 	u_int32_t	 nr;
1690249cc75fSPatrick Kelsey 	void		*buf;
1691249cc75fSPatrick Kelsey 	int		 nbytes;
1692249cc75fSPatrick Kelsey 	u_int8_t	 scheduler;
1693249cc75fSPatrick Kelsey 	/*
1694249cc75fSPatrick Kelsey 	 * Placed here so code that only uses the above parameters can be
1695249cc75fSPatrick Kelsey 	 * written entirely in terms of the v0 or v1 type.
1696249cc75fSPatrick Kelsey 	 */
1697249cc75fSPatrick Kelsey 	u_int32_t	 version;  /* Requested version of stats struct */
1698249cc75fSPatrick Kelsey };
1699249cc75fSPatrick Kelsey 
1700249cc75fSPatrick Kelsey /* Latest version of struct pfioc_qstats_vX */
1701249cc75fSPatrick Kelsey #define PFIOC_QSTATS_VERSION	1
1702249cc75fSPatrick Kelsey 
17033b3a8eb9SGleb Smirnoff struct pfioc_ruleset {
17043b3a8eb9SGleb Smirnoff 	u_int32_t	 nr;
17053b3a8eb9SGleb Smirnoff 	char		 path[MAXPATHLEN];
17063b3a8eb9SGleb Smirnoff 	char		 name[PF_ANCHOR_NAME_SIZE];
17073b3a8eb9SGleb Smirnoff };
17083b3a8eb9SGleb Smirnoff 
17093b3a8eb9SGleb Smirnoff #define PF_RULESET_ALTQ		(PF_RULESET_MAX)
17103b3a8eb9SGleb Smirnoff #define PF_RULESET_TABLE	(PF_RULESET_MAX+1)
1711e732e742SKristof Provost #define PF_RULESET_ETH		(PF_RULESET_MAX+2)
17123b3a8eb9SGleb Smirnoff struct pfioc_trans {
17133b3a8eb9SGleb Smirnoff 	int		 size;	/* number of elements */
17143b3a8eb9SGleb Smirnoff 	int		 esize; /* size of each element in bytes */
17153b3a8eb9SGleb Smirnoff 	struct pfioc_trans_e {
17163b3a8eb9SGleb Smirnoff 		int		rs_num;
17173b3a8eb9SGleb Smirnoff 		char		anchor[MAXPATHLEN];
17183b3a8eb9SGleb Smirnoff 		u_int32_t	ticket;
17193b3a8eb9SGleb Smirnoff 	}		*array;
17203b3a8eb9SGleb Smirnoff };
17213b3a8eb9SGleb Smirnoff 
17223b3a8eb9SGleb Smirnoff #define PFR_FLAG_ATOMIC		0x00000001	/* unused */
17233b3a8eb9SGleb Smirnoff #define PFR_FLAG_DUMMY		0x00000002
17243b3a8eb9SGleb Smirnoff #define PFR_FLAG_FEEDBACK	0x00000004
17253b3a8eb9SGleb Smirnoff #define PFR_FLAG_CLSTATS	0x00000008
17263b3a8eb9SGleb Smirnoff #define PFR_FLAG_ADDRSTOO	0x00000010
17273b3a8eb9SGleb Smirnoff #define PFR_FLAG_REPLACE	0x00000020
17283b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLRSETS	0x00000040
17293b3a8eb9SGleb Smirnoff #define PFR_FLAG_ALLMASK	0x0000007F
17303b3a8eb9SGleb Smirnoff #ifdef _KERNEL
17313b3a8eb9SGleb Smirnoff #define PFR_FLAG_USERIOCTL	0x10000000
17323b3a8eb9SGleb Smirnoff #endif
17333b3a8eb9SGleb Smirnoff 
17343b3a8eb9SGleb Smirnoff struct pfioc_table {
17353b3a8eb9SGleb Smirnoff 	struct pfr_table	 pfrio_table;
17363b3a8eb9SGleb Smirnoff 	void			*pfrio_buffer;
17373b3a8eb9SGleb Smirnoff 	int			 pfrio_esize;
17383b3a8eb9SGleb Smirnoff 	int			 pfrio_size;
17393b3a8eb9SGleb Smirnoff 	int			 pfrio_size2;
17403b3a8eb9SGleb Smirnoff 	int			 pfrio_nadd;
17413b3a8eb9SGleb Smirnoff 	int			 pfrio_ndel;
17423b3a8eb9SGleb Smirnoff 	int			 pfrio_nchange;
17433b3a8eb9SGleb Smirnoff 	int			 pfrio_flags;
17443b3a8eb9SGleb Smirnoff 	u_int32_t		 pfrio_ticket;
17453b3a8eb9SGleb Smirnoff };
17463b3a8eb9SGleb Smirnoff #define	pfrio_exists	pfrio_nadd
17473b3a8eb9SGleb Smirnoff #define	pfrio_nzero	pfrio_nadd
17483b3a8eb9SGleb Smirnoff #define	pfrio_nmatch	pfrio_nadd
17493b3a8eb9SGleb Smirnoff #define pfrio_naddr	pfrio_size2
17503b3a8eb9SGleb Smirnoff #define pfrio_setflag	pfrio_size2
17513b3a8eb9SGleb Smirnoff #define pfrio_clrflag	pfrio_nadd
17523b3a8eb9SGleb Smirnoff 
17533b3a8eb9SGleb Smirnoff struct pfioc_iface {
17543b3a8eb9SGleb Smirnoff 	char	 pfiio_name[IFNAMSIZ];
17553b3a8eb9SGleb Smirnoff 	void	*pfiio_buffer;
17563b3a8eb9SGleb Smirnoff 	int	 pfiio_esize;
17573b3a8eb9SGleb Smirnoff 	int	 pfiio_size;
17583b3a8eb9SGleb Smirnoff 	int	 pfiio_nzero;
17593b3a8eb9SGleb Smirnoff 	int	 pfiio_flags;
17603b3a8eb9SGleb Smirnoff };
17613b3a8eb9SGleb Smirnoff 
17623b3a8eb9SGleb Smirnoff /*
17633b3a8eb9SGleb Smirnoff  * ioctl operations
17643b3a8eb9SGleb Smirnoff  */
17653b3a8eb9SGleb Smirnoff 
17663b3a8eb9SGleb Smirnoff #define DIOCSTART	_IO  ('D',  1)
17673b3a8eb9SGleb Smirnoff #define DIOCSTOP	_IO  ('D',  2)
17683b3a8eb9SGleb Smirnoff #define DIOCADDRULE	_IOWR('D',  4, struct pfioc_rule)
17695c62ededSKristof Provost #define DIOCADDRULENV	_IOWR('D',  4, struct pfioc_nv)
17703b3a8eb9SGleb Smirnoff #define DIOCGETRULES	_IOWR('D',  6, struct pfioc_rule)
17713b3a8eb9SGleb Smirnoff #define DIOCGETRULE	_IOWR('D',  7, struct pfioc_rule)
1772d710367dSKristof Provost #define DIOCGETRULENV	_IOWR('D',  7, struct pfioc_nv)
17733b3a8eb9SGleb Smirnoff /* XXX cut 8 - 17 */
17743b3a8eb9SGleb Smirnoff #define DIOCCLRSTATES	_IOWR('D', 18, struct pfioc_state_kill)
17757606a45dSKristof Provost #define DIOCCLRSTATESNV	_IOWR('D', 18, struct pfioc_nv)
1776cc535c95SWill Andrews #define DIOCGETSTATE	_IOWR('D', 19, struct pfioc_state)
17771732afaaSKristof Provost #define DIOCGETSTATENV	_IOWR('D', 19, struct pfioc_nv)
17783b3a8eb9SGleb Smirnoff #define DIOCSETSTATUSIF _IOWR('D', 20, struct pfioc_if)
17793b3a8eb9SGleb Smirnoff #define DIOCGETSTATUS	_IOWR('D', 21, struct pf_status)
17802b10cf85SKristof Provost #define DIOCGETSTATUSNV	_IOWR('D', 21, struct pfioc_nv)
17813b3a8eb9SGleb Smirnoff #define DIOCCLRSTATUS	_IO  ('D', 22)
17823b3a8eb9SGleb Smirnoff #define DIOCNATLOOK	_IOWR('D', 23, struct pfioc_natlook)
17833b3a8eb9SGleb Smirnoff #define DIOCSETDEBUG	_IOWR('D', 24, u_int32_t)
17843b3a8eb9SGleb Smirnoff #define DIOCGETSTATES	_IOWR('D', 25, struct pfioc_states)
1785cc535c95SWill Andrews #define DIOCCHANGERULE	_IOWR('D', 26, struct pfioc_rule)
17863b3a8eb9SGleb Smirnoff /* XXX cut 26 - 28 */
17873b3a8eb9SGleb Smirnoff #define DIOCSETTIMEOUT	_IOWR('D', 29, struct pfioc_tm)
17883b3a8eb9SGleb Smirnoff #define DIOCGETTIMEOUT	_IOWR('D', 30, struct pfioc_tm)
1789cc535c95SWill Andrews #define DIOCADDSTATE	_IOWR('D', 37, struct pfioc_state)
1790cc535c95SWill Andrews #define DIOCCLRRULECTRS	_IO  ('D', 38)
17913b3a8eb9SGleb Smirnoff #define DIOCGETLIMIT	_IOWR('D', 39, struct pfioc_limit)
17923b3a8eb9SGleb Smirnoff #define DIOCSETLIMIT	_IOWR('D', 40, struct pfioc_limit)
17933b3a8eb9SGleb Smirnoff #define DIOCKILLSTATES	_IOWR('D', 41, struct pfioc_state_kill)
1794e989530aSKristof Provost #define DIOCKILLSTATESNV	_IOWR('D', 41, struct pfioc_nv)
17953b3a8eb9SGleb Smirnoff #define DIOCSTARTALTQ	_IO  ('D', 42)
17963b3a8eb9SGleb Smirnoff #define DIOCSTOPALTQ	_IO  ('D', 43)
1797249cc75fSPatrick Kelsey #define DIOCADDALTQV0	_IOWR('D', 45, struct pfioc_altq_v0)
1798249cc75fSPatrick Kelsey #define DIOCADDALTQV1	_IOWR('D', 45, struct pfioc_altq_v1)
1799249cc75fSPatrick Kelsey #define DIOCGETALTQSV0	_IOWR('D', 47, struct pfioc_altq_v0)
1800249cc75fSPatrick Kelsey #define DIOCGETALTQSV1	_IOWR('D', 47, struct pfioc_altq_v1)
1801249cc75fSPatrick Kelsey #define DIOCGETALTQV0	_IOWR('D', 48, struct pfioc_altq_v0)
1802249cc75fSPatrick Kelsey #define DIOCGETALTQV1	_IOWR('D', 48, struct pfioc_altq_v1)
1803249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV0 _IOWR('D', 49, struct pfioc_altq_v0)
1804249cc75fSPatrick Kelsey #define DIOCCHANGEALTQV1 _IOWR('D', 49, struct pfioc_altq_v1)
1805249cc75fSPatrick Kelsey #define DIOCGETQSTATSV0	_IOWR('D', 50, struct pfioc_qstats_v0)
1806249cc75fSPatrick Kelsey #define DIOCGETQSTATSV1	_IOWR('D', 50, struct pfioc_qstats_v1)
18073b3a8eb9SGleb Smirnoff #define DIOCBEGINADDRS	_IOWR('D', 51, struct pfioc_pooladdr)
18083b3a8eb9SGleb Smirnoff #define DIOCADDADDR	_IOWR('D', 52, struct pfioc_pooladdr)
18093b3a8eb9SGleb Smirnoff #define DIOCGETADDRS	_IOWR('D', 53, struct pfioc_pooladdr)
18103b3a8eb9SGleb Smirnoff #define DIOCGETADDR	_IOWR('D', 54, struct pfioc_pooladdr)
1811cc535c95SWill Andrews #define DIOCCHANGEADDR	_IOWR('D', 55, struct pfioc_pooladdr)
18123b3a8eb9SGleb Smirnoff /* XXX cut 55 - 57 */
18133b3a8eb9SGleb Smirnoff #define	DIOCGETRULESETS	_IOWR('D', 58, struct pfioc_ruleset)
18143b3a8eb9SGleb Smirnoff #define	DIOCGETRULESET	_IOWR('D', 59, struct pfioc_ruleset)
18153b3a8eb9SGleb Smirnoff #define	DIOCRCLRTABLES	_IOWR('D', 60, struct pfioc_table)
18163b3a8eb9SGleb Smirnoff #define	DIOCRADDTABLES	_IOWR('D', 61, struct pfioc_table)
18173b3a8eb9SGleb Smirnoff #define	DIOCRDELTABLES	_IOWR('D', 62, struct pfioc_table)
18183b3a8eb9SGleb Smirnoff #define	DIOCRGETTABLES	_IOWR('D', 63, struct pfioc_table)
18193b3a8eb9SGleb Smirnoff #define	DIOCRGETTSTATS	_IOWR('D', 64, struct pfioc_table)
18203b3a8eb9SGleb Smirnoff #define DIOCRCLRTSTATS	_IOWR('D', 65, struct pfioc_table)
18213b3a8eb9SGleb Smirnoff #define	DIOCRCLRADDRS	_IOWR('D', 66, struct pfioc_table)
18223b3a8eb9SGleb Smirnoff #define	DIOCRADDADDRS	_IOWR('D', 67, struct pfioc_table)
18233b3a8eb9SGleb Smirnoff #define	DIOCRDELADDRS	_IOWR('D', 68, struct pfioc_table)
18243b3a8eb9SGleb Smirnoff #define	DIOCRSETADDRS	_IOWR('D', 69, struct pfioc_table)
18253b3a8eb9SGleb Smirnoff #define	DIOCRGETADDRS	_IOWR('D', 70, struct pfioc_table)
18263b3a8eb9SGleb Smirnoff #define	DIOCRGETASTATS	_IOWR('D', 71, struct pfioc_table)
18273b3a8eb9SGleb Smirnoff #define	DIOCRCLRASTATS	_IOWR('D', 72, struct pfioc_table)
18283b3a8eb9SGleb Smirnoff #define	DIOCRTSTADDRS	_IOWR('D', 73, struct pfioc_table)
18293b3a8eb9SGleb Smirnoff #define	DIOCRSETTFLAGS	_IOWR('D', 74, struct pfioc_table)
18303b3a8eb9SGleb Smirnoff #define	DIOCRINADEFINE	_IOWR('D', 77, struct pfioc_table)
18313b3a8eb9SGleb Smirnoff #define	DIOCOSFPFLUSH	_IO('D', 78)
18323b3a8eb9SGleb Smirnoff #define	DIOCOSFPADD	_IOWR('D', 79, struct pf_osfp_ioctl)
18333b3a8eb9SGleb Smirnoff #define	DIOCOSFPGET	_IOWR('D', 80, struct pf_osfp_ioctl)
18343b3a8eb9SGleb Smirnoff #define	DIOCXBEGIN	_IOWR('D', 81, struct pfioc_trans)
18353b3a8eb9SGleb Smirnoff #define	DIOCXCOMMIT	_IOWR('D', 82, struct pfioc_trans)
18363b3a8eb9SGleb Smirnoff #define	DIOCXROLLBACK	_IOWR('D', 83, struct pfioc_trans)
18373b3a8eb9SGleb Smirnoff #define	DIOCGETSRCNODES	_IOWR('D', 84, struct pfioc_src_nodes)
18383b3a8eb9SGleb Smirnoff #define	DIOCCLRSRCNODES	_IO('D', 85)
18393b3a8eb9SGleb Smirnoff #define	DIOCSETHOSTID	_IOWR('D', 86, u_int32_t)
18403b3a8eb9SGleb Smirnoff #define	DIOCIGETIFACES	_IOWR('D', 87, struct pfioc_iface)
18413b3a8eb9SGleb Smirnoff #define	DIOCSETIFFLAG	_IOWR('D', 89, struct pfioc_iface)
18423b3a8eb9SGleb Smirnoff #define	DIOCCLRIFFLAG	_IOWR('D', 90, struct pfioc_iface)
18433b3a8eb9SGleb Smirnoff #define	DIOCKILLSRCNODES	_IOWR('D', 91, struct pfioc_src_node_kill)
1844047c4e36SKristof Provost #define	DIOCGIFSPEEDV0	_IOWR('D', 92, struct pf_ifspeed_v0)
1845047c4e36SKristof Provost #define	DIOCGIFSPEEDV1	_IOWR('D', 92, struct pf_ifspeed_v1)
1846c6bf20a2SKristof Provost #define DIOCGETSTATESV2	_IOWR('D', 93, struct pfioc_states_v2)
18478e1864edSKristof Provost #define	DIOCGETSYNCOOKIES	_IOWR('D', 94, struct pfioc_nv)
18488e1864edSKristof Provost #define	DIOCSETSYNCOOKIES	_IOWR('D', 95, struct pfioc_nv)
1849047c4e36SKristof Provost #define	DIOCKEEPCOUNTERS	_IOWR('D', 96, struct pfioc_nv)
18508e492101SKristof Provost #define	DIOCKEEPCOUNTERS_FREEBSD13	_IOWR('D', 92, struct pfioc_nv)
1851e732e742SKristof Provost #define	DIOCADDETHRULE		_IOWR('D', 97, struct pfioc_nv)
1852e732e742SKristof Provost #define	DIOCGETETHRULE		_IOWR('D', 98, struct pfioc_nv)
1853e732e742SKristof Provost #define	DIOCGETETHRULES		_IOWR('D', 99, struct pfioc_nv)
185442ec75f8SKristof Provost 
1855249cc75fSPatrick Kelsey struct pf_ifspeed_v0 {
18563b3a8eb9SGleb Smirnoff 	char			ifname[IFNAMSIZ];
18573b3a8eb9SGleb Smirnoff 	u_int32_t		baudrate;
18583b3a8eb9SGleb Smirnoff };
1859249cc75fSPatrick Kelsey 
1860249cc75fSPatrick Kelsey struct pf_ifspeed_v1 {
1861249cc75fSPatrick Kelsey 	char			ifname[IFNAMSIZ];
1862249cc75fSPatrick Kelsey 	u_int32_t		baudrate32;
1863249cc75fSPatrick Kelsey 	/* layout identical to struct pf_ifspeed_v0 up to this point */
1864249cc75fSPatrick Kelsey 	u_int64_t		baudrate;
1865249cc75fSPatrick Kelsey };
1866249cc75fSPatrick Kelsey 
1867249cc75fSPatrick Kelsey /* Latest version of struct pf_ifspeed_vX */
1868249cc75fSPatrick Kelsey #define PF_IFSPEED_VERSION	1
1869249cc75fSPatrick Kelsey 
1870249cc75fSPatrick Kelsey /*
1871249cc75fSPatrick Kelsey  * Compatibility and convenience macros
1872249cc75fSPatrick Kelsey  */
1873249cc75fSPatrick Kelsey #ifndef _KERNEL
1874249cc75fSPatrick Kelsey #ifdef PFIOC_USE_LATEST
1875249cc75fSPatrick Kelsey /*
1876249cc75fSPatrick Kelsey  * Maintaining in-tree consumers of the ioctl interface is easier when that
1877249cc75fSPatrick Kelsey  * code can be written in terms old names that refer to the latest interface
1878249cc75fSPatrick Kelsey  * version as that reduces the required changes in the consumers to those
1879249cc75fSPatrick Kelsey  * that are functionally necessary to accommodate a new interface version.
1880249cc75fSPatrick Kelsey  */
1881249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, PFIOC_ALTQ_VERSION)
1882249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, PFIOC_QSTATS_VERSION)
1883249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, PF_IFSPEED_VERSION)
1884249cc75fSPatrick Kelsey 
1885249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, PFIOC_ALTQ_VERSION)
1886249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, PFIOC_ALTQ_VERSION)
1887249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, PFIOC_ALTQ_VERSION)
1888249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, PFIOC_ALTQ_VERSION)
1889249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, PFIOC_QSTATS_VERSION)
1890249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, PF_IFSPEED_VERSION)
1891249cc75fSPatrick Kelsey #else
1892249cc75fSPatrick Kelsey /*
1893249cc75fSPatrick Kelsey  * When building out-of-tree code that is written for the old interface,
1894249cc75fSPatrick Kelsey  * such as may exist in ports for example, resolve the old struct tags and
1895249cc75fSPatrick Kelsey  * ioctl command names to the v0 versions.
1896249cc75fSPatrick Kelsey  */
1897249cc75fSPatrick Kelsey #define	pfioc_altq	__CONCAT(pfioc_altq_v, 0)
1898249cc75fSPatrick Kelsey #define	pfioc_qstats	__CONCAT(pfioc_qstats_v, 0)
1899249cc75fSPatrick Kelsey #define	pf_ifspeed	__CONCAT(pf_ifspeed_v, 0)
1900249cc75fSPatrick Kelsey 
1901249cc75fSPatrick Kelsey #define	DIOCADDALTQ	__CONCAT(DIOCADDALTQV, 0)
1902249cc75fSPatrick Kelsey #define	DIOCGETALTQS	__CONCAT(DIOCGETALTQSV, 0)
1903249cc75fSPatrick Kelsey #define	DIOCGETALTQ	__CONCAT(DIOCGETALTQV, 0)
1904249cc75fSPatrick Kelsey #define	DIOCCHANGEALTQ	__CONCAT(DIOCCHANGEALTQV, 0)
1905249cc75fSPatrick Kelsey #define	DIOCGETQSTATS	__CONCAT(DIOCGETQSTATSV, 0)
1906249cc75fSPatrick Kelsey #define	DIOCGIFSPEED	__CONCAT(DIOCGIFSPEEDV, 0)
1907249cc75fSPatrick Kelsey #endif /* PFIOC_USE_LATEST */
1908249cc75fSPatrick Kelsey #endif /* _KERNEL */
19093b3a8eb9SGleb Smirnoff 
19103b3a8eb9SGleb Smirnoff #ifdef _KERNEL
191117ad7334SKristof Provost LIST_HEAD(pf_ksrc_node_list, pf_ksrc_node);
19123b3a8eb9SGleb Smirnoff struct pf_srchash {
191317ad7334SKristof Provost 	struct pf_ksrc_node_list		nodes;
19143b3a8eb9SGleb Smirnoff 	struct mtx			lock;
19153b3a8eb9SGleb Smirnoff };
19163b3a8eb9SGleb Smirnoff 
19173b3a8eb9SGleb Smirnoff struct pf_keyhash {
19183b3a8eb9SGleb Smirnoff 	LIST_HEAD(, pf_state_key)	keys;
19193b3a8eb9SGleb Smirnoff 	struct mtx			lock;
19203b3a8eb9SGleb Smirnoff };
19213b3a8eb9SGleb Smirnoff 
19223b3a8eb9SGleb Smirnoff struct pf_idhash {
1923211cddf9SKristof Provost 	LIST_HEAD(, pf_kstate)		states;
19243b3a8eb9SGleb Smirnoff 	struct mtx			lock;
19253b3a8eb9SGleb Smirnoff };
19263b3a8eb9SGleb Smirnoff 
1927231e83d3SKristof Provost extern u_long		pf_ioctl_maxcount;
19286828cc99SGleb Smirnoff extern u_long		pf_hashmask;
19296828cc99SGleb Smirnoff extern u_long		pf_srchashmask;
193091e0f2d2SKristof Provost #define	PF_HASHSIZ	(131072)
1931bf56a3feSKristof Provost #define	PF_SRCHASHSIZ	(PF_HASHSIZ/4)
19323b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_keyhash *, pf_keyhash);
19333b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_idhash *, pf_idhash);
19343b3a8eb9SGleb Smirnoff #define V_pf_keyhash	VNET(pf_keyhash)
19353b3a8eb9SGleb Smirnoff #define	V_pf_idhash	VNET(pf_idhash)
19363b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_srchash *, pf_srchash);
19373b3a8eb9SGleb Smirnoff #define	V_pf_srchash	VNET(pf_srchash)
19383b3a8eb9SGleb Smirnoff 
19396828cc99SGleb Smirnoff #define PF_IDHASH(s)	(be64toh((s)->id) % (pf_hashmask + 1))
19403b3a8eb9SGleb Smirnoff 
19413b3a8eb9SGleb Smirnoff VNET_DECLARE(void *, pf_swi_cookie);
19423b3a8eb9SGleb Smirnoff #define V_pf_swi_cookie	VNET(pf_swi_cookie)
1943cecfaf9bSKristof Provost VNET_DECLARE(struct intr_event *, pf_swi_ie);
1944cecfaf9bSKristof Provost #define	V_pf_swi_ie	VNET(pf_swi_ie)
19453b3a8eb9SGleb Smirnoff 
19463b3a8eb9SGleb Smirnoff VNET_DECLARE(uint64_t, pf_stateid[MAXCPU]);
19473b3a8eb9SGleb Smirnoff #define	V_pf_stateid	VNET(pf_stateid)
19483b3a8eb9SGleb Smirnoff 
19493b3a8eb9SGleb Smirnoff TAILQ_HEAD(pf_altqqueue, pf_altq);
19508f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue,	 pf_altqs[4]);
19513b3a8eb9SGleb Smirnoff #define	V_pf_altqs			 VNET(pf_altqs)
1952320c1116SKristof Provost VNET_DECLARE(struct pf_kpalist,		 pf_pabuf);
19533b3a8eb9SGleb Smirnoff #define	V_pf_pabuf			 VNET(pf_pabuf)
19543b3a8eb9SGleb Smirnoff 
19553b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_active);
19563b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_active		 VNET(ticket_altqs_active)
19573b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_altqs_inactive);
19583b3a8eb9SGleb Smirnoff #define	V_ticket_altqs_inactive		 VNET(ticket_altqs_inactive)
19593b3a8eb9SGleb Smirnoff VNET_DECLARE(int,			 altqs_inactive_open);
19603b3a8eb9SGleb Smirnoff #define	V_altqs_inactive_open		 VNET(altqs_inactive_open)
19613b3a8eb9SGleb Smirnoff VNET_DECLARE(u_int32_t,			 ticket_pabuf);
19623b3a8eb9SGleb Smirnoff #define	V_ticket_pabuf			 VNET(ticket_pabuf)
19633b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_active);
19643b3a8eb9SGleb Smirnoff #define	V_pf_altqs_active		 VNET(pf_altqs_active)
19658f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_active);
19668f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_active		 VNET(pf_altq_ifs_active)
19673b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_altqqueue *,	 pf_altqs_inactive);
19683b3a8eb9SGleb Smirnoff #define	V_pf_altqs_inactive		 VNET(pf_altqs_inactive)
19698f2ac656SPatrick Kelsey VNET_DECLARE(struct pf_altqqueue *,	 pf_altq_ifs_inactive);
19708f2ac656SPatrick Kelsey #define	V_pf_altq_ifs_inactive		 VNET(pf_altq_ifs_inactive)
19713b3a8eb9SGleb Smirnoff 
1972e86bddeaSKristof Provost VNET_DECLARE(struct pf_krulequeue, pf_unlinked_rules);
19733b3a8eb9SGleb Smirnoff #define	V_pf_unlinked_rules	VNET(pf_unlinked_rules)
19743b3a8eb9SGleb Smirnoff 
1975d40d4b3eSMateusz Guzik #ifdef PF_WANT_32_TO_64_COUNTER
1976d40d4b3eSMateusz Guzik LIST_HEAD(allkiflist_head, pfi_kkif);
1977d40d4b3eSMateusz Guzik VNET_DECLARE(struct allkiflist_head, pf_allkiflist);
1978d40d4b3eSMateusz Guzik #define V_pf_allkiflist     VNET(pf_allkiflist)
1979d40d4b3eSMateusz Guzik VNET_DECLARE(size_t, pf_allkifcount);
1980d40d4b3eSMateusz Guzik #define V_pf_allkifcount     VNET(pf_allkifcount)
1981d40d4b3eSMateusz Guzik VNET_DECLARE(struct pfi_kkif *, pf_kifmarker);
1982d40d4b3eSMateusz Guzik #define V_pf_kifmarker     VNET(pf_kifmarker)
198302cf67ccSMateusz Guzik 
198402cf67ccSMateusz Guzik LIST_HEAD(allrulelist_head, pf_krule);
198502cf67ccSMateusz Guzik VNET_DECLARE(struct allrulelist_head, pf_allrulelist);
198602cf67ccSMateusz Guzik #define V_pf_allrulelist     VNET(pf_allrulelist)
198702cf67ccSMateusz Guzik VNET_DECLARE(size_t, pf_allrulecount);
198802cf67ccSMateusz Guzik #define V_pf_allrulecount     VNET(pf_allrulecount)
198902cf67ccSMateusz Guzik VNET_DECLARE(struct pf_krule *, pf_rulemarker);
199002cf67ccSMateusz Guzik #define V_pf_rulemarker     VNET(pf_rulemarker)
1991d40d4b3eSMateusz Guzik #endif
1992d40d4b3eSMateusz Guzik 
1993efc6c51fSGleb Smirnoff void				 pf_initialize(void);
19947e92ce73SMartin Matuska void				 pf_mtag_initialize(void);
19957e92ce73SMartin Matuska void				 pf_mtag_cleanup(void);
19963b3a8eb9SGleb Smirnoff void				 pf_cleanup(void);
19973b3a8eb9SGleb Smirnoff 
19983b3a8eb9SGleb Smirnoff struct pf_mtag			*pf_get_mtag(struct mbuf *);
19993b3a8eb9SGleb Smirnoff 
2000e86bddeaSKristof Provost extern void			 pf_calc_skip_steps(struct pf_krulequeue *);
20013b3a8eb9SGleb Smirnoff #ifdef ALTQ
20023b3a8eb9SGleb Smirnoff extern	void			 pf_altq_ifnet_event(struct ifnet *, int);
20033b3a8eb9SGleb Smirnoff #endif
20043b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_z);
20053b3a8eb9SGleb Smirnoff #define	V_pf_state_z		 VNET(pf_state_z)
20063b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_key_z);
20073b3a8eb9SGleb Smirnoff #define	V_pf_state_key_z	 VNET(pf_state_key_z)
20083b3a8eb9SGleb Smirnoff VNET_DECLARE(uma_zone_t,	 pf_state_scrub_z);
20093b3a8eb9SGleb Smirnoff #define	V_pf_state_scrub_z	 VNET(pf_state_scrub_z)
20103b3a8eb9SGleb Smirnoff 
20113b3a8eb9SGleb Smirnoff extern void			 pf_purge_thread(void *);
2012a0429b54SBjoern A. Zeeb extern void			 pf_unload_vnet_purge(void);
20133b3a8eb9SGleb Smirnoff extern void			 pf_intr(void *);
20143b3a8eb9SGleb Smirnoff extern void			 pf_purge_expired_src_nodes(void);
20153b3a8eb9SGleb Smirnoff 
20168f3d786cSMateusz Guzik extern int			 pf_unlink_state(struct pf_kstate *);
2017320c1116SKristof Provost extern int			 pf_state_insert(struct pfi_kkif *,
2018d0fdf2b2SKristof Provost 				    struct pfi_kkif *,
20193b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
20203b3a8eb9SGleb Smirnoff 				    struct pf_state_key *,
2021211cddf9SKristof Provost 				    struct pf_kstate *);
2022211cddf9SKristof Provost extern struct pf_kstate		*pf_alloc_state(int);
2023211cddf9SKristof Provost extern void			 pf_free_state(struct pf_kstate *);
20243b3a8eb9SGleb Smirnoff 
20253b3a8eb9SGleb Smirnoff static __inline void
2026211cddf9SKristof Provost pf_ref_state(struct pf_kstate *s)
20273b3a8eb9SGleb Smirnoff {
20283b3a8eb9SGleb Smirnoff 
20293b3a8eb9SGleb Smirnoff 	refcount_acquire(&s->refs);
20303b3a8eb9SGleb Smirnoff }
20313b3a8eb9SGleb Smirnoff 
20323b3a8eb9SGleb Smirnoff static __inline int
2033211cddf9SKristof Provost pf_release_state(struct pf_kstate *s)
20343b3a8eb9SGleb Smirnoff {
20353b3a8eb9SGleb Smirnoff 
20363b3a8eb9SGleb Smirnoff 	if (refcount_release(&s->refs)) {
20373b3a8eb9SGleb Smirnoff 		pf_free_state(s);
20383b3a8eb9SGleb Smirnoff 		return (1);
20393b3a8eb9SGleb Smirnoff 	} else
20403b3a8eb9SGleb Smirnoff 		return (0);
20413b3a8eb9SGleb Smirnoff }
20423b3a8eb9SGleb Smirnoff 
2043d09388d0SMateusz Guzik static __inline int
2044211cddf9SKristof Provost pf_release_staten(struct pf_kstate *s, u_int n)
2045d09388d0SMateusz Guzik {
2046d09388d0SMateusz Guzik 
2047d09388d0SMateusz Guzik 	if (refcount_releasen(&s->refs, n)) {
2048d09388d0SMateusz Guzik 		pf_free_state(s);
2049d09388d0SMateusz Guzik 		return (1);
2050d09388d0SMateusz Guzik 	} else
2051d09388d0SMateusz Guzik 		return (0);
2052d09388d0SMateusz Guzik }
2053d09388d0SMateusz Guzik 
2054211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_byid(uint64_t, uint32_t);
2055211cddf9SKristof Provost extern struct pf_kstate		*pf_find_state_all(struct pf_state_key_cmp *,
20563b3a8eb9SGleb Smirnoff 				    u_int, int *);
205719d6e29bSMateusz Guzik extern bool			pf_find_state_all_exists(struct pf_state_key_cmp *,
205819d6e29bSMateusz Guzik 				    u_int);
205917ad7334SKristof Provost extern struct pf_ksrc_node	*pf_find_src_node(struct pf_addr *,
2060e86bddeaSKristof Provost 				    struct pf_krule *, sa_family_t, int);
206117ad7334SKristof Provost extern void			 pf_unlink_src_node(struct pf_ksrc_node *);
206217ad7334SKristof Provost extern u_int			 pf_free_src_nodes(struct pf_ksrc_node_list *);
2063211cddf9SKristof Provost extern void			 pf_print_state(struct pf_kstate *);
206407d9bc07SGleb Smirnoff extern void			 pf_print_flags(u_int8_t);
20653b3a8eb9SGleb Smirnoff extern u_int16_t		 pf_cksum_fixup(u_int16_t, u_int16_t, u_int16_t,
20663b3a8eb9SGleb Smirnoff 				    u_int8_t);
2067c110fc49SKristof Provost extern u_int16_t		 pf_proto_cksum_fixup(struct mbuf *, u_int16_t,
2068c110fc49SKristof Provost 				    u_int16_t, u_int16_t, u_int8_t);
20693b3a8eb9SGleb Smirnoff 
20703b3a8eb9SGleb Smirnoff VNET_DECLARE(struct ifnet *,		 sync_ifp);
20713b3a8eb9SGleb Smirnoff #define	V_sync_ifp		 	 VNET(sync_ifp);
2072e86bddeaSKristof Provost VNET_DECLARE(struct pf_krule,		 pf_default_rule);
20733b3a8eb9SGleb Smirnoff #define	V_pf_default_rule		  VNET(pf_default_rule)
20743b3a8eb9SGleb Smirnoff extern void			 pf_addrcpy(struct pf_addr *, struct pf_addr *,
20753b3a8eb9SGleb Smirnoff 				    u_int8_t);
2076e86bddeaSKristof Provost void				pf_free_rule(struct pf_krule *);
20773b3a8eb9SGleb Smirnoff 
2078e732e742SKristof Provost int	pf_test_eth(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
20793b3a8eb9SGleb Smirnoff #ifdef INET
2080effaab88SKristof Provost int	pf_test(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
2081320c1116SKristof Provost int	pf_normalize_ip(struct mbuf **, int, struct pfi_kkif *, u_short *,
20820324938aSGleb Smirnoff 	    struct pf_pdesc *);
20833b3a8eb9SGleb Smirnoff #endif /* INET */
20843b3a8eb9SGleb Smirnoff 
20853b3a8eb9SGleb Smirnoff #ifdef INET6
2086effaab88SKristof Provost int	pf_test6(int, int, struct ifnet *, struct mbuf **, struct inpcb *);
2087320c1116SKristof Provost int	pf_normalize_ip6(struct mbuf **, int, struct pfi_kkif *, u_short *,
20880324938aSGleb Smirnoff 	    struct pf_pdesc *);
20893b3a8eb9SGleb Smirnoff void	pf_poolmask(struct pf_addr *, struct pf_addr*,
20903b3a8eb9SGleb Smirnoff 	    struct pf_addr *, struct pf_addr *, u_int8_t);
20913b3a8eb9SGleb Smirnoff void	pf_addr_inc(struct pf_addr *, sa_family_t);
20928dc98c2aSGleb Smirnoff int	pf_refragment6(struct ifnet *, struct mbuf **, struct m_tag *);
20933b3a8eb9SGleb Smirnoff #endif /* INET6 */
20943b3a8eb9SGleb Smirnoff 
2095211cddf9SKristof Provost u_int32_t	pf_new_isn(struct pf_kstate *);
20963b3a8eb9SGleb Smirnoff void   *pf_pull_hdr(struct mbuf *, int, void *, int, u_short *, u_short *,
20973b3a8eb9SGleb Smirnoff 	    sa_family_t);
20983b3a8eb9SGleb Smirnoff void	pf_change_a(void *, u_int16_t *, u_int32_t, u_int8_t);
2099c110fc49SKristof Provost void	pf_change_proto_a(struct mbuf *, void *, u_int16_t *, u_int32_t,
2100c110fc49SKristof Provost 	    u_int8_t);
2101c110fc49SKristof Provost void	pf_change_tcp_a(struct mbuf *, void *, u_int16_t *, u_int32_t);
2102c3f69af0SKristof Provost void	pf_patch_16_unaligned(struct mbuf *, u_int16_t *, void *, u_int16_t,
2103c3f69af0SKristof Provost 	    bool, u_int8_t);
2104c3f69af0SKristof Provost void	pf_patch_32_unaligned(struct mbuf *, u_int16_t *, void *, u_int32_t,
2105c3f69af0SKristof Provost     bool, u_int8_t);
2106211cddf9SKristof Provost void	pf_send_deferred_syn(struct pf_kstate *);
21073b3a8eb9SGleb Smirnoff int	pf_match_addr(u_int8_t, struct pf_addr *, struct pf_addr *,
21083b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
21093b3a8eb9SGleb Smirnoff int	pf_match_addr_range(struct pf_addr *, struct pf_addr *,
21103b3a8eb9SGleb Smirnoff 	    struct pf_addr *, sa_family_t);
21113b3a8eb9SGleb Smirnoff int	pf_match_port(u_int8_t, u_int16_t, u_int16_t, u_int16_t);
21123b3a8eb9SGleb Smirnoff 
2113efc6c51fSGleb Smirnoff void	pf_normalize_init(void);
21143b3a8eb9SGleb Smirnoff void	pf_normalize_cleanup(void);
2115320c1116SKristof Provost int	pf_normalize_tcp(int, struct pfi_kkif *, struct mbuf *, int, int, void *,
21163b3a8eb9SGleb Smirnoff 	    struct pf_pdesc *);
2117211cddf9SKristof Provost void	pf_normalize_tcp_cleanup(struct pf_kstate *);
21183b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_init(struct mbuf *, int, struct pf_pdesc *,
21193b3a8eb9SGleb Smirnoff 	    struct tcphdr *, struct pf_state_peer *, struct pf_state_peer *);
21203b3a8eb9SGleb Smirnoff int	pf_normalize_tcp_stateful(struct mbuf *, int, struct pf_pdesc *,
2121211cddf9SKristof Provost 	    u_short *, struct tcphdr *, struct pf_kstate *,
21223b3a8eb9SGleb Smirnoff 	    struct pf_state_peer *, struct pf_state_peer *, int *);
21233b3a8eb9SGleb Smirnoff u_int32_t
2124211cddf9SKristof Provost 	pf_state_expires(const struct pf_kstate *);
21253b3a8eb9SGleb Smirnoff void	pf_purge_expired_fragments(void);
21265d0020d6SKristof Provost void	pf_purge_fragments(uint32_t);
2127320c1116SKristof Provost int	pf_routable(struct pf_addr *addr, sa_family_t af, struct pfi_kkif *,
21283b3a8eb9SGleb Smirnoff 	    int);
21293b3a8eb9SGleb Smirnoff int	pf_socket_lookup(int, struct pf_pdesc *, struct mbuf *);
21303b3a8eb9SGleb Smirnoff struct pf_state_key *pf_alloc_state_key(int);
21313b3a8eb9SGleb Smirnoff void	pfr_initialize(void);
21323b3a8eb9SGleb Smirnoff void	pfr_cleanup(void);
21333b3a8eb9SGleb Smirnoff int	pfr_match_addr(struct pfr_ktable *, struct pf_addr *, sa_family_t);
21343b3a8eb9SGleb Smirnoff void	pfr_update_stats(struct pfr_ktable *, struct pf_addr *, sa_family_t,
21353b3a8eb9SGleb Smirnoff 	    u_int64_t, int, int, int);
21363b3a8eb9SGleb Smirnoff int	pfr_pool_get(struct pfr_ktable *, int *, struct pf_addr *, sa_family_t);
21373b3a8eb9SGleb Smirnoff void	pfr_dynaddr_update(struct pfr_ktable *, struct pfi_dynaddr *);
21383b3a8eb9SGleb Smirnoff struct pfr_ktable *
2139e86bddeaSKristof Provost 	pfr_attach_table(struct pf_kruleset *, char *);
21403b3a8eb9SGleb Smirnoff void	pfr_detach_table(struct pfr_ktable *);
21413b3a8eb9SGleb Smirnoff int	pfr_clr_tables(struct pfr_table *, int *, int);
21423b3a8eb9SGleb Smirnoff int	pfr_add_tables(struct pfr_table *, int, int *, int);
21433b3a8eb9SGleb Smirnoff int	pfr_del_tables(struct pfr_table *, int, int *, int);
2144adfe2f6aSKristof Provost int	pfr_table_count(struct pfr_table *, int);
21453b3a8eb9SGleb Smirnoff int	pfr_get_tables(struct pfr_table *, struct pfr_table *, int *, int);
21463b3a8eb9SGleb Smirnoff int	pfr_get_tstats(struct pfr_table *, struct pfr_tstats *, int *, int);
21473b3a8eb9SGleb Smirnoff int	pfr_clr_tstats(struct pfr_table *, int, int *, int);
21483b3a8eb9SGleb Smirnoff int	pfr_set_tflags(struct pfr_table *, int, int, int, int *, int *, int);
21493b3a8eb9SGleb Smirnoff int	pfr_clr_addrs(struct pfr_table *, int *, int);
21503b3a8eb9SGleb Smirnoff int	pfr_insert_kentry(struct pfr_ktable *, struct pfr_addr *, long);
21513b3a8eb9SGleb Smirnoff int	pfr_add_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
21523b3a8eb9SGleb Smirnoff 	    int);
21533b3a8eb9SGleb Smirnoff int	pfr_del_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
21543b3a8eb9SGleb Smirnoff 	    int);
21553b3a8eb9SGleb Smirnoff int	pfr_set_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
21563b3a8eb9SGleb Smirnoff 	    int *, int *, int *, int, u_int32_t);
21573b3a8eb9SGleb Smirnoff int	pfr_get_addrs(struct pfr_table *, struct pfr_addr *, int *, int);
21583b3a8eb9SGleb Smirnoff int	pfr_get_astats(struct pfr_table *, struct pfr_astats *, int *, int);
21593b3a8eb9SGleb Smirnoff int	pfr_clr_astats(struct pfr_table *, struct pfr_addr *, int, int *,
21603b3a8eb9SGleb Smirnoff 	    int);
21613b3a8eb9SGleb Smirnoff int	pfr_tst_addrs(struct pfr_table *, struct pfr_addr *, int, int *,
21623b3a8eb9SGleb Smirnoff 	    int);
21633b3a8eb9SGleb Smirnoff int	pfr_ina_begin(struct pfr_table *, u_int32_t *, int *, int);
21643b3a8eb9SGleb Smirnoff int	pfr_ina_rollback(struct pfr_table *, u_int32_t, int *, int);
21653b3a8eb9SGleb Smirnoff int	pfr_ina_commit(struct pfr_table *, u_int32_t, int *, int *, int);
21663b3a8eb9SGleb Smirnoff int	pfr_ina_define(struct pfr_table *, struct pfr_addr *, int, int *,
21673b3a8eb9SGleb Smirnoff 	    int *, u_int32_t, int);
21683b3a8eb9SGleb Smirnoff 
21693b3a8eb9SGleb Smirnoff MALLOC_DECLARE(PFI_MTYPE);
2170320c1116SKristof Provost VNET_DECLARE(struct pfi_kkif *,		 pfi_all);
21713b3a8eb9SGleb Smirnoff #define	V_pfi_all	 		 VNET(pfi_all)
21723b3a8eb9SGleb Smirnoff 
2173efc6c51fSGleb Smirnoff void		 pfi_initialize(void);
2174a0429b54SBjoern A. Zeeb void		 pfi_initialize_vnet(void);
21753b3a8eb9SGleb Smirnoff void		 pfi_cleanup(void);
2176a0429b54SBjoern A. Zeeb void		 pfi_cleanup_vnet(void);
2177320c1116SKristof Provost void		 pfi_kkif_ref(struct pfi_kkif *);
2178320c1116SKristof Provost void		 pfi_kkif_unref(struct pfi_kkif *);
2179320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_find(const char *);
2180320c1116SKristof Provost struct pfi_kkif	*pfi_kkif_attach(struct pfi_kkif *, const char *);
2181320c1116SKristof Provost int		 pfi_kkif_match(struct pfi_kkif *, struct pfi_kkif *);
2182320c1116SKristof Provost void		 pfi_kkif_purge(void);
21833b3a8eb9SGleb Smirnoff int		 pfi_match_addr(struct pfi_dynaddr *, struct pf_addr *,
21843b3a8eb9SGleb Smirnoff 		    sa_family_t);
21853b3a8eb9SGleb Smirnoff int		 pfi_dynaddr_setup(struct pf_addr_wrap *, sa_family_t);
21863b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_remove(struct pfi_dynaddr *);
21873b3a8eb9SGleb Smirnoff void		 pfi_dynaddr_copyout(struct pf_addr_wrap *);
21883b3a8eb9SGleb Smirnoff void		 pfi_update_status(const char *, struct pf_status *);
21893b3a8eb9SGleb Smirnoff void		 pfi_get_ifaces(const char *, struct pfi_kif *, int *);
21903b3a8eb9SGleb Smirnoff int		 pfi_set_flags(const char *, int);
21913b3a8eb9SGleb Smirnoff int		 pfi_clear_flags(const char *, int);
21923b3a8eb9SGleb Smirnoff 
2193e86bddeaSKristof Provost int		 pf_match_tag(struct mbuf *, struct pf_krule *, int *, int);
21943b3a8eb9SGleb Smirnoff int		 pf_tag_packet(struct mbuf *, struct pf_pdesc *, int);
219560048052SGleb Smirnoff int		 pf_addr_cmp(struct pf_addr *, struct pf_addr *,
219660048052SGleb Smirnoff 		    sa_family_t);
21973b3a8eb9SGleb Smirnoff 
21988e1864edSKristof Provost u_int16_t	 pf_get_mss(struct mbuf *, int, u_int16_t, sa_family_t);
21998e1864edSKristof Provost u_int8_t	 pf_get_wscale(struct mbuf *, int, u_int16_t, sa_family_t);
22008e1864edSKristof Provost struct mbuf 	*pf_build_tcp(const struct pf_krule *, sa_family_t,
22018e1864edSKristof Provost 		    const struct pf_addr *, const struct pf_addr *,
22028e1864edSKristof Provost 		    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
22038e1864edSKristof Provost 		    u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
22048e1864edSKristof Provost 		    u_int16_t);
22058e1864edSKristof Provost void		 pf_send_tcp(const struct pf_krule *, sa_family_t,
22068e1864edSKristof Provost 			    const struct pf_addr *, const struct pf_addr *,
22078e1864edSKristof Provost 			    u_int16_t, u_int16_t, u_int32_t, u_int32_t,
22088e1864edSKristof Provost 			    u_int8_t, u_int16_t, u_int16_t, u_int8_t, int,
22098e1864edSKristof Provost 			    u_int16_t);
22108e1864edSKristof Provost 
22118e1864edSKristof Provost void			 pf_syncookies_init(void);
221232271c4dSKristof Provost void			 pf_syncookies_cleanup(void);
22138e1864edSKristof Provost int			 pf_get_syncookies(struct pfioc_nv *);
22148e1864edSKristof Provost int			 pf_set_syncookies(struct pfioc_nv *);
22158e1864edSKristof Provost int			 pf_synflood_check(struct pf_pdesc *);
22168e1864edSKristof Provost void			 pf_syncookie_send(struct mbuf *m, int off,
22178e1864edSKristof Provost 			    struct pf_pdesc *);
22188e1864edSKristof Provost u_int8_t		 pf_syncookie_validate(struct pf_pdesc *);
22198e1864edSKristof Provost struct mbuf *		 pf_syncookie_recreate_syn(uint8_t, int,
22208e1864edSKristof Provost 			    struct pf_pdesc *);
22218e1864edSKristof Provost 
2222a9572d8fSGleb Smirnoff VNET_DECLARE(struct pf_kstatus, pf_status);
22233b3a8eb9SGleb Smirnoff #define	V_pf_status	VNET(pf_status)
22243b3a8eb9SGleb Smirnoff 
22253b3a8eb9SGleb Smirnoff struct pf_limit {
22263b3a8eb9SGleb Smirnoff 	uma_zone_t	zone;
22273b3a8eb9SGleb Smirnoff 	u_int		limit;
22283b3a8eb9SGleb Smirnoff };
22293b3a8eb9SGleb Smirnoff VNET_DECLARE(struct pf_limit, pf_limits[PF_LIMIT_MAX]);
22303b3a8eb9SGleb Smirnoff #define	V_pf_limits VNET(pf_limits)
22313b3a8eb9SGleb Smirnoff 
22323b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
22333b3a8eb9SGleb Smirnoff 
22343b3a8eb9SGleb Smirnoff #ifdef _KERNEL
2235e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor_global,		 pf_anchors);
22363b3a8eb9SGleb Smirnoff #define	V_pf_anchors				 VNET(pf_anchors)
2237e86bddeaSKristof Provost VNET_DECLARE(struct pf_kanchor,			 pf_main_anchor);
22383b3a8eb9SGleb Smirnoff #define	V_pf_main_anchor			 VNET(pf_main_anchor)
2239c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor_global,	 pf_keth_anchors);
2240c5131afeSKristof Provost #define	V_pf_keth_anchors			 VNET(pf_keth_anchors)
22413b3a8eb9SGleb Smirnoff #define pf_main_ruleset	V_pf_main_anchor.ruleset
22423b3a8eb9SGleb Smirnoff 
2243c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_anchor,		 pf_main_keth_anchor);
2244c5131afeSKristof Provost #define V_pf_main_keth_anchor			 VNET(pf_main_keth_anchor)
2245c5131afeSKristof Provost VNET_DECLARE(struct pf_keth_ruleset*,		 pf_keth);
2246e732e742SKristof Provost #define	V_pf_keth				 VNET(pf_keth)
2247e732e742SKristof Provost 
2248e86bddeaSKristof Provost void			 pf_init_kruleset(struct pf_kruleset *);
2249c5131afeSKristof Provost void			 pf_init_keth(struct pf_keth_ruleset *);
2250e86bddeaSKristof Provost int			 pf_kanchor_setup(struct pf_krule *,
2251e86bddeaSKristof Provost 			    const struct pf_kruleset *, const char *);
2252d710367dSKristof Provost int			 pf_kanchor_nvcopyout(const struct pf_kruleset *,
2253d710367dSKristof Provost 			    const struct pf_krule *, nvlist_t *);
2254e86bddeaSKristof Provost int			 pf_kanchor_copyout(const struct pf_kruleset *,
2255e86bddeaSKristof Provost 			    const struct pf_krule *, struct pfioc_rule *);
2256e86bddeaSKristof Provost void			 pf_kanchor_remove(struct pf_krule *);
2257e86bddeaSKristof Provost void			 pf_remove_if_empty_kruleset(struct pf_kruleset *);
2258e86bddeaSKristof Provost struct pf_kruleset	*pf_find_kruleset(const char *);
2259e86bddeaSKristof Provost struct pf_kruleset	*pf_find_or_create_kruleset(const char *);
22603b3a8eb9SGleb Smirnoff void			 pf_rs_initialize(void);
22615e9dae8eSKristof Provost 
2262c5131afeSKristof Provost 
2263773e3a71SMark Johnston struct pf_krule		*pf_krule_alloc(void);
2264c5131afeSKristof Provost 
2265c5131afeSKristof Provost void			 pf_remove_if_empty_keth_ruleset(
2266c5131afeSKristof Provost 			    struct pf_keth_ruleset *);
2267c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_keth_ruleset(const char *);
2268c5131afeSKristof Provost struct pf_keth_anchor	*pf_find_keth_anchor(const char *);
2269c5131afeSKristof Provost int			 pf_keth_anchor_setup(struct pf_keth_rule *,
2270c5131afeSKristof Provost 			    const struct pf_keth_ruleset *, const char *);
2271c5131afeSKristof Provost int			 pf_keth_anchor_nvcopyout(
2272c5131afeSKristof Provost 			    const struct pf_keth_ruleset *,
2273c5131afeSKristof Provost 			    const struct pf_keth_rule *, nvlist_t *);
2274c5131afeSKristof Provost struct pf_keth_ruleset	*pf_find_or_create_keth_ruleset(const char *);
2275c5131afeSKristof Provost void			 pf_keth_anchor_remove(struct pf_keth_rule *);
2276c5131afeSKristof Provost 
22775e9dae8eSKristof Provost void			 pf_krule_free(struct pf_krule *);
2278e86bddeaSKristof Provost #endif
22793b3a8eb9SGleb Smirnoff 
22803b3a8eb9SGleb Smirnoff /* The fingerprint functions can be linked into userland programs (tcpdump) */
22813b3a8eb9SGleb Smirnoff int	pf_osfp_add(struct pf_osfp_ioctl *);
22823b3a8eb9SGleb Smirnoff #ifdef _KERNEL
22833b3a8eb9SGleb Smirnoff struct pf_osfp_enlist *
22843b3a8eb9SGleb Smirnoff 	pf_osfp_fingerprint(struct pf_pdesc *, struct mbuf *, int,
22853b3a8eb9SGleb Smirnoff 	    const struct tcphdr *);
22863b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
22873b3a8eb9SGleb Smirnoff void	pf_osfp_flush(void);
22883b3a8eb9SGleb Smirnoff int	pf_osfp_get(struct pf_osfp_ioctl *);
22893b3a8eb9SGleb Smirnoff int	pf_osfp_match(struct pf_osfp_enlist *, pf_osfp_t);
22903b3a8eb9SGleb Smirnoff 
22913b3a8eb9SGleb Smirnoff #ifdef _KERNEL
229207d9bc07SGleb Smirnoff void			 pf_print_host(struct pf_addr *, u_int16_t, u_int8_t);
229307d9bc07SGleb Smirnoff 
2294e86bddeaSKristof Provost void			 pf_step_into_anchor(struct pf_kanchor_stackframe *, int *,
2295e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2296e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2297e86bddeaSKristof Provost int			 pf_step_out_of_anchor(struct pf_kanchor_stackframe *, int *,
2298e86bddeaSKristof Provost 			    struct pf_kruleset **, int, struct pf_krule **,
2299e86bddeaSKristof Provost 			    struct pf_krule **, int *);
2300c5131afeSKristof Provost void			 pf_step_into_keth_anchor(struct pf_keth_anchor_stackframe *,
2301c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2302c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2303c5131afeSKristof Provost 			    int *);
2304c5131afeSKristof Provost int			 pf_step_out_of_keth_anchor(struct pf_keth_anchor_stackframe *,
2305c5131afeSKristof Provost 			    int *, struct pf_keth_ruleset **,
2306c5131afeSKristof Provost 			    struct pf_keth_rule **, struct pf_keth_rule **,
2307c5131afeSKristof Provost 			    int *);
23083b3a8eb9SGleb Smirnoff 
2309e86bddeaSKristof Provost int			 pf_map_addr(u_int8_t, struct pf_krule *,
23103b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
231117ad7334SKristof Provost 			    struct pf_addr *, struct pf_ksrc_node **);
2312e86bddeaSKristof Provost struct pf_krule		*pf_get_translation(struct pf_pdesc *, struct mbuf *,
2313320c1116SKristof Provost 			    int, int, struct pfi_kkif *, struct pf_ksrc_node **,
23143b3a8eb9SGleb Smirnoff 			    struct pf_state_key **, struct pf_state_key **,
23153b3a8eb9SGleb Smirnoff 			    struct pf_addr *, struct pf_addr *,
2316e86bddeaSKristof Provost 			    uint16_t, uint16_t, struct pf_kanchor_stackframe *);
23173b3a8eb9SGleb Smirnoff 
23183b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_setup(struct pf_pdesc *, struct pf_addr *,
23193b3a8eb9SGleb Smirnoff 			    struct pf_addr *, u_int16_t, u_int16_t);
23203b3a8eb9SGleb Smirnoff struct pf_state_key	*pf_state_key_clone(struct pf_state_key *);
232126c841e2SKristof Provost 
232226c841e2SKristof Provost struct pfi_kkif		*pf_kkif_create(int);
232326c841e2SKristof Provost void			 pf_kkif_free(struct pfi_kkif *);
23245a3b9507SKristof Provost void			 pf_kkif_zero(struct pfi_kkif *);
23253b3a8eb9SGleb Smirnoff #endif /* _KERNEL */
23263b3a8eb9SGleb Smirnoff 
23273b3a8eb9SGleb Smirnoff #endif /* _NET_PFVAR_H_ */
2328