xref: /freebsd-src/sys/netpfil/pf/pf_table.c (revision d90854a60a919fc574d1786486f85394f53ce2dd)
1d8aa10ccSGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2002 Cedric Berger
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  *
31d8aa10ccSGleb Smirnoff  *	$OpenBSD: pf_table.c,v 1.79 2008/10/08 06:24:50 mcbride Exp $
323b3a8eb9SGleb Smirnoff  */
333b3a8eb9SGleb Smirnoff 
343b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
353b3a8eb9SGleb Smirnoff #include "opt_inet.h"
363b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
373b3a8eb9SGleb Smirnoff 
383b3a8eb9SGleb Smirnoff #include <sys/param.h>
393b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
403b3a8eb9SGleb Smirnoff #include <sys/lock.h>
413b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
42eedc7fd9SGleb Smirnoff #include <sys/mbuf.h>
433b3a8eb9SGleb Smirnoff #include <sys/mutex.h>
443b3a8eb9SGleb Smirnoff #include <sys/refcount.h>
453b3a8eb9SGleb Smirnoff #include <sys/socket.h>
463b3a8eb9SGleb Smirnoff #include <vm/uma.h>
473b3a8eb9SGleb Smirnoff 
483b3a8eb9SGleb Smirnoff #include <net/if.h>
493b3a8eb9SGleb Smirnoff #include <net/vnet.h>
503b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
513b3a8eb9SGleb Smirnoff 
52032dff66SKristof Provost #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
53032dff66SKristof Provost 
543b3a8eb9SGleb Smirnoff #define	ACCEPT_FLAGS(flags, oklist)		\
553b3a8eb9SGleb Smirnoff 	do {					\
563b3a8eb9SGleb Smirnoff 		if ((flags & ~(oklist)) &	\
573b3a8eb9SGleb Smirnoff 		    PFR_FLAG_ALLMASK)		\
583b3a8eb9SGleb Smirnoff 			return (EINVAL);	\
593b3a8eb9SGleb Smirnoff 	} while (0)
603b3a8eb9SGleb Smirnoff 
613b3a8eb9SGleb Smirnoff #define	FILLIN_SIN(sin, addr)			\
623b3a8eb9SGleb Smirnoff 	do {					\
633b3a8eb9SGleb Smirnoff 		(sin).sin_len = sizeof(sin);	\
643b3a8eb9SGleb Smirnoff 		(sin).sin_family = AF_INET;	\
653b3a8eb9SGleb Smirnoff 		(sin).sin_addr = (addr);	\
663b3a8eb9SGleb Smirnoff 	} while (0)
673b3a8eb9SGleb Smirnoff 
683b3a8eb9SGleb Smirnoff #define	FILLIN_SIN6(sin6, addr)			\
693b3a8eb9SGleb Smirnoff 	do {					\
703b3a8eb9SGleb Smirnoff 		(sin6).sin6_len = sizeof(sin6);	\
713b3a8eb9SGleb Smirnoff 		(sin6).sin6_family = AF_INET6;	\
723b3a8eb9SGleb Smirnoff 		(sin6).sin6_addr = (addr);	\
733b3a8eb9SGleb Smirnoff 	} while (0)
743b3a8eb9SGleb Smirnoff 
753b3a8eb9SGleb Smirnoff #define	SWAP(type, a1, a2)			\
763b3a8eb9SGleb Smirnoff 	do {					\
773b3a8eb9SGleb Smirnoff 		type tmp = a1;			\
783b3a8eb9SGleb Smirnoff 		a1 = a2;			\
793b3a8eb9SGleb Smirnoff 		a2 = tmp;			\
803b3a8eb9SGleb Smirnoff 	} while (0)
813b3a8eb9SGleb Smirnoff 
823b3a8eb9SGleb Smirnoff #define	AF_BITS(af)		(((af)==AF_INET)?32:128)
833b3a8eb9SGleb Smirnoff #define	ADDR_NETWORK(ad)	((ad)->pfra_net < AF_BITS((ad)->pfra_af))
843b3a8eb9SGleb Smirnoff #define	KENTRY_NETWORK(ke)	((ke)->pfrke_net < AF_BITS((ke)->pfrke_af))
853b3a8eb9SGleb Smirnoff #define	KENTRY_RNF_ROOT(ke) \
863b3a8eb9SGleb Smirnoff 		((((struct radix_node *)(ke))->rn_flags & RNF_ROOT) != 0)
873b3a8eb9SGleb Smirnoff 
883b3a8eb9SGleb Smirnoff #define	NO_ADDRESSES		(-1)
893b3a8eb9SGleb Smirnoff #define	ENQUEUE_UNMARKED_ONLY	(1)
903b3a8eb9SGleb Smirnoff #define	INVERT_NEG_FLAG		(1)
913b3a8eb9SGleb Smirnoff 
923b3a8eb9SGleb Smirnoff struct pfr_walktree {
933b3a8eb9SGleb Smirnoff 	enum pfrw_op {
943b3a8eb9SGleb Smirnoff 		PFRW_MARK,
953b3a8eb9SGleb Smirnoff 		PFRW_SWEEP,
963b3a8eb9SGleb Smirnoff 		PFRW_ENQUEUE,
973b3a8eb9SGleb Smirnoff 		PFRW_GET_ADDRS,
983b3a8eb9SGleb Smirnoff 		PFRW_GET_ASTATS,
993b3a8eb9SGleb Smirnoff 		PFRW_POOL_GET,
100b21826bfSKristof Provost 		PFRW_DYNADDR_UPDATE,
101b21826bfSKristof Provost 		PFRW_COUNTERS
1023b3a8eb9SGleb Smirnoff 	}	 pfrw_op;
1033b3a8eb9SGleb Smirnoff 	union {
1047b676698SKristof Provost 		struct pfr_addr		*pfrw_addr;
1057b676698SKristof Provost 		struct pfr_astats	*pfrw_astats;
1067b676698SKristof Provost 		struct pfr_kentryworkq	*pfrw_workq;
1077b676698SKristof Provost 		struct pfr_kentry	*pfrw_kentry;
1087b676698SKristof Provost 		struct pfi_dynaddr	*pfrw_dyn;
1097b676698SKristof Provost 	};
1103b3a8eb9SGleb Smirnoff 	int	 pfrw_free;
11159048686SKristof Provost 	int	 pfrw_flags;
1123b3a8eb9SGleb Smirnoff };
1133b3a8eb9SGleb Smirnoff 
1143b3a8eb9SGleb Smirnoff #define	senderr(e)	do { rv = (e); goto _bad; } while (0)
1153b3a8eb9SGleb Smirnoff 
1163b3a8eb9SGleb Smirnoff static MALLOC_DEFINE(M_PFTABLE, "pf_table", "pf(4) tables structures");
1175f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_z);
1183b3a8eb9SGleb Smirnoff #define	V_pfr_kentry_z		VNET(pfr_kentry_z)
119c1be8399SMark Johnston VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_counter_z);
120c1be8399SMark Johnston #define	V_pfr_kentry_counter_z	VNET(pfr_kentry_counter_z)
1213b3a8eb9SGleb Smirnoff 
1223b3a8eb9SGleb Smirnoff static struct pf_addr	 pfr_ffaddr = {
1233b3a8eb9SGleb Smirnoff 	.addr32 = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1243b3a8eb9SGleb Smirnoff };
1253b3a8eb9SGleb Smirnoff 
12659048686SKristof Provost static void		 pfr_copyout_astats(struct pfr_astats *,
12759048686SKristof Provost 			    const struct pfr_kentry *,
12859048686SKristof Provost 			    const struct pfr_walktree *);
1293b3a8eb9SGleb Smirnoff static void		 pfr_copyout_addr(struct pfr_addr *,
13059048686SKristof Provost 			    const struct pfr_kentry *ke);
1313b3a8eb9SGleb Smirnoff static int		 pfr_validate_addr(struct pfr_addr *);
1323b3a8eb9SGleb Smirnoff static void		 pfr_enqueue_addrs(struct pfr_ktable *,
1333b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *, int *, int);
1343b3a8eb9SGleb Smirnoff static void		 pfr_mark_addrs(struct pfr_ktable *);
1353b3a8eb9SGleb Smirnoff static struct pfr_kentry
1363b3a8eb9SGleb Smirnoff 			*pfr_lookup_addr(struct pfr_ktable *,
1373b3a8eb9SGleb Smirnoff 			    struct pfr_addr *, int);
13821121f9bSMark Johnston static struct pfr_kentry *pfr_create_kentry(struct pfr_addr *, bool);
1393b3a8eb9SGleb Smirnoff static void		 pfr_destroy_kentries(struct pfr_kentryworkq *);
1403b3a8eb9SGleb Smirnoff static void		 pfr_destroy_kentry(struct pfr_kentry *);
1413b3a8eb9SGleb Smirnoff static void		 pfr_insert_kentries(struct pfr_ktable *,
1423b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *, long);
1433b3a8eb9SGleb Smirnoff static void		 pfr_remove_kentries(struct pfr_ktable *,
1443b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *);
14521121f9bSMark Johnston static void		 pfr_clstats_kentries(struct pfr_ktable *,
14621121f9bSMark Johnston 			    struct pfr_kentryworkq *, long, int);
1473b3a8eb9SGleb Smirnoff static void		 pfr_reset_feedback(struct pfr_addr *, int);
1483b3a8eb9SGleb Smirnoff static void		 pfr_prepare_network(union sockaddr_union *, int, int);
1493b3a8eb9SGleb Smirnoff static int		 pfr_route_kentry(struct pfr_ktable *,
1503b3a8eb9SGleb Smirnoff 			    struct pfr_kentry *);
1513b3a8eb9SGleb Smirnoff static int		 pfr_unroute_kentry(struct pfr_ktable *,
1523b3a8eb9SGleb Smirnoff 			    struct pfr_kentry *);
1533b3a8eb9SGleb Smirnoff static int		 pfr_walktree(struct radix_node *, void *);
1543b3a8eb9SGleb Smirnoff static int		 pfr_validate_table(struct pfr_table *, int, int);
1553b3a8eb9SGleb Smirnoff static int		 pfr_fix_anchor(char *);
1563b3a8eb9SGleb Smirnoff static void		 pfr_commit_ktable(struct pfr_ktable *, long);
1573b3a8eb9SGleb Smirnoff static void		 pfr_insert_ktables(struct pfr_ktableworkq *);
1583b3a8eb9SGleb Smirnoff static void		 pfr_insert_ktable(struct pfr_ktable *);
1593b3a8eb9SGleb Smirnoff static void		 pfr_setflags_ktables(struct pfr_ktableworkq *);
1603b3a8eb9SGleb Smirnoff static void		 pfr_setflags_ktable(struct pfr_ktable *, int);
1613b3a8eb9SGleb Smirnoff static void		 pfr_clstats_ktables(struct pfr_ktableworkq *, long,
1623b3a8eb9SGleb Smirnoff 			    int);
1633b3a8eb9SGleb Smirnoff static void		 pfr_clstats_ktable(struct pfr_ktable *, long, int);
1643b3a8eb9SGleb Smirnoff static struct pfr_ktable
1653b3a8eb9SGleb Smirnoff 			*pfr_create_ktable(struct pfr_table *, long, int);
1663b3a8eb9SGleb Smirnoff static void		 pfr_destroy_ktables(struct pfr_ktableworkq *, int);
1673b3a8eb9SGleb Smirnoff static void		 pfr_destroy_ktable(struct pfr_ktable *, int);
1683b3a8eb9SGleb Smirnoff static int		 pfr_ktable_compare(struct pfr_ktable *,
1693b3a8eb9SGleb Smirnoff 			    struct pfr_ktable *);
1703b3a8eb9SGleb Smirnoff static struct pfr_ktable
1713b3a8eb9SGleb Smirnoff 			*pfr_lookup_table(struct pfr_table *);
1723b3a8eb9SGleb Smirnoff static void		 pfr_clean_node_mask(struct pfr_ktable *,
1733b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *);
1743b3a8eb9SGleb Smirnoff static int		 pfr_skip_table(struct pfr_table *,
1753b3a8eb9SGleb Smirnoff 			    struct pfr_ktable *, int);
1763b3a8eb9SGleb Smirnoff static struct pfr_kentry
1773b3a8eb9SGleb Smirnoff 			*pfr_kentry_byidx(struct pfr_ktable *, int, int);
1783b3a8eb9SGleb Smirnoff 
1793b3a8eb9SGleb Smirnoff static RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
1803b3a8eb9SGleb Smirnoff static RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
1813b3a8eb9SGleb Smirnoff 
1825f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_ktablehead, pfr_ktables);
1831e9e3741SMarko Zec #define	V_pfr_ktables	VNET(pfr_ktables)
1841e9e3741SMarko Zec 
1855f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_table, pfr_nulltable);
1861e9e3741SMarko Zec #define	V_pfr_nulltable	VNET(pfr_nulltable)
1871e9e3741SMarko Zec 
1885f901c92SAndrew Turner VNET_DEFINE_STATIC(int, pfr_ktable_cnt);
1891e9e3741SMarko Zec #define V_pfr_ktable_cnt	VNET(pfr_ktable_cnt)
1903b3a8eb9SGleb Smirnoff 
1913b3a8eb9SGleb Smirnoff void
1923b3a8eb9SGleb Smirnoff pfr_initialize(void)
1933b3a8eb9SGleb Smirnoff {
1943b3a8eb9SGleb Smirnoff 
195c1be8399SMark Johnston 	V_pfr_kentry_counter_z = uma_zcreate("pf table entry counters",
196c1be8399SMark Johnston 	    PFR_NUM_COUNTERS * sizeof(uint64_t), NULL, NULL, NULL, NULL,
197c1be8399SMark Johnston 	    UMA_ALIGN_PTR, UMA_ZONE_PCPU);
1983b3a8eb9SGleb Smirnoff 	V_pfr_kentry_z = uma_zcreate("pf table entries",
1993b3a8eb9SGleb Smirnoff 	    sizeof(struct pfr_kentry), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
2003b3a8eb9SGleb Smirnoff 	    0);
2017d1ab866SMark Johnston 	uma_zone_set_max(V_pfr_kentry_z, PFR_KENTRY_HIWAT);
2023b3a8eb9SGleb Smirnoff 	V_pf_limits[PF_LIMIT_TABLE_ENTRIES].zone = V_pfr_kentry_z;
2033b3a8eb9SGleb Smirnoff 	V_pf_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
2043b3a8eb9SGleb Smirnoff }
2053b3a8eb9SGleb Smirnoff 
2063b3a8eb9SGleb Smirnoff void
2073b3a8eb9SGleb Smirnoff pfr_cleanup(void)
2083b3a8eb9SGleb Smirnoff {
2093b3a8eb9SGleb Smirnoff 
2103b3a8eb9SGleb Smirnoff 	uma_zdestroy(V_pfr_kentry_z);
211c1be8399SMark Johnston 	uma_zdestroy(V_pfr_kentry_counter_z);
2123b3a8eb9SGleb Smirnoff }
2133b3a8eb9SGleb Smirnoff 
2143b3a8eb9SGleb Smirnoff int
2153b3a8eb9SGleb Smirnoff pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
2163b3a8eb9SGleb Smirnoff {
2173b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
2183b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
2193b3a8eb9SGleb Smirnoff 
2203b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
2213b3a8eb9SGleb Smirnoff 
2223b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
2233b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
2243b3a8eb9SGleb Smirnoff 		return (EINVAL);
2253b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
2263b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2273b3a8eb9SGleb Smirnoff 		return (ESRCH);
2283b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
2293b3a8eb9SGleb Smirnoff 		return (EPERM);
2303b3a8eb9SGleb Smirnoff 	pfr_enqueue_addrs(kt, &workq, ndel, 0);
2313b3a8eb9SGleb Smirnoff 
2323b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
2333b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &workq);
2343b3a8eb9SGleb Smirnoff 		KASSERT(kt->pfrkt_cnt == 0, ("%s: non-null pfrkt_cnt", __func__));
2353b3a8eb9SGleb Smirnoff 	}
2363b3a8eb9SGleb Smirnoff 	return (0);
2373b3a8eb9SGleb Smirnoff }
2383b3a8eb9SGleb Smirnoff 
2393b3a8eb9SGleb Smirnoff int
2403b3a8eb9SGleb Smirnoff pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
2413b3a8eb9SGleb Smirnoff     int *nadd, int flags)
2423b3a8eb9SGleb Smirnoff {
2433b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *tmpkt;
2443b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
2453b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
2463b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
2473b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0;
2483b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
2493b3a8eb9SGleb Smirnoff 
2503b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
2513b3a8eb9SGleb Smirnoff 
2523b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
2533b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
2543b3a8eb9SGleb Smirnoff 		return (EINVAL);
2553b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
2563b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2573b3a8eb9SGleb Smirnoff 		return (ESRCH);
2583b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
2593b3a8eb9SGleb Smirnoff 		return (EPERM);
2601e9e3741SMarko Zec 	tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0);
2613b3a8eb9SGleb Smirnoff 	if (tmpkt == NULL)
2623b3a8eb9SGleb Smirnoff 		return (ENOMEM);
2633b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
2643b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
2653b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
2663b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
2673b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
2683b3a8eb9SGleb Smirnoff 		q = pfr_lookup_addr(tmpkt, ad, 1);
2693b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
2703b3a8eb9SGleb Smirnoff 			if (q != NULL)
2713b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DUPLICATE;
2723b3a8eb9SGleb Smirnoff 			else if (p == NULL)
2733b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_ADDED;
2743b3a8eb9SGleb Smirnoff 			else if (p->pfrke_not != ad->pfra_not)
2753b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_CONFLICT;
2763b3a8eb9SGleb Smirnoff 			else
2773b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
2783b3a8eb9SGleb Smirnoff 		}
2793b3a8eb9SGleb Smirnoff 		if (p == NULL && q == NULL) {
28021121f9bSMark Johnston 			p = pfr_create_kentry(ad,
28121121f9bSMark Johnston 			    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
2823b3a8eb9SGleb Smirnoff 			if (p == NULL)
2833b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
2843b3a8eb9SGleb Smirnoff 			if (pfr_route_kentry(tmpkt, p)) {
2853b3a8eb9SGleb Smirnoff 				pfr_destroy_kentry(p);
2863b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
2873b3a8eb9SGleb Smirnoff 			} else {
2883b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
2893b3a8eb9SGleb Smirnoff 				xadd++;
2903b3a8eb9SGleb Smirnoff 			}
2913b3a8eb9SGleb Smirnoff 		}
2923b3a8eb9SGleb Smirnoff 	}
2933b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &workq);
2943b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
2953b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &workq, tzero);
2963b3a8eb9SGleb Smirnoff 	else
2973b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&workq);
2983b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
2993b3a8eb9SGleb Smirnoff 		*nadd = xadd;
3003b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
3013b3a8eb9SGleb Smirnoff 	return (0);
3023b3a8eb9SGleb Smirnoff _bad:
3033b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &workq);
3043b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&workq);
3053b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
3063b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
3073b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
3083b3a8eb9SGleb Smirnoff 	return (rv);
3093b3a8eb9SGleb Smirnoff }
3103b3a8eb9SGleb Smirnoff 
3113b3a8eb9SGleb Smirnoff int
3123b3a8eb9SGleb Smirnoff pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
3133b3a8eb9SGleb Smirnoff     int *ndel, int flags)
3143b3a8eb9SGleb Smirnoff {
3153b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
3163b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
3173b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
3183b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
3193b3a8eb9SGleb Smirnoff 	int			 i, rv, xdel = 0, log = 1;
3203b3a8eb9SGleb Smirnoff 
3213b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
3223b3a8eb9SGleb Smirnoff 
3233b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
3243b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
3253b3a8eb9SGleb Smirnoff 		return (EINVAL);
3263b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
3273b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
3283b3a8eb9SGleb Smirnoff 		return (ESRCH);
3293b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
3303b3a8eb9SGleb Smirnoff 		return (EPERM);
3313b3a8eb9SGleb Smirnoff 	/*
3323b3a8eb9SGleb Smirnoff 	 * there are two algorithms to choose from here.
3333b3a8eb9SGleb Smirnoff 	 * with:
3343b3a8eb9SGleb Smirnoff 	 *   n: number of addresses to delete
3353b3a8eb9SGleb Smirnoff 	 *   N: number of addresses in the table
3363b3a8eb9SGleb Smirnoff 	 *
3373b3a8eb9SGleb Smirnoff 	 * one is O(N) and is better for large 'n'
3383b3a8eb9SGleb Smirnoff 	 * one is O(n*LOG(N)) and is better for small 'n'
3393b3a8eb9SGleb Smirnoff 	 *
3403b3a8eb9SGleb Smirnoff 	 * following code try to decide which one is best.
3413b3a8eb9SGleb Smirnoff 	 */
3423b3a8eb9SGleb Smirnoff 	for (i = kt->pfrkt_cnt; i > 0; i >>= 1)
3433b3a8eb9SGleb Smirnoff 		log++;
3443b3a8eb9SGleb Smirnoff 	if (size > kt->pfrkt_cnt/log) {
3453b3a8eb9SGleb Smirnoff 		/* full table scan */
3463b3a8eb9SGleb Smirnoff 		pfr_mark_addrs(kt);
3473b3a8eb9SGleb Smirnoff 	} else {
3483b3a8eb9SGleb Smirnoff 		/* iterate over addresses to delete */
3493b3a8eb9SGleb Smirnoff 		for (i = 0, ad = addr; i < size; i++, ad++) {
3503b3a8eb9SGleb Smirnoff 			if (pfr_validate_addr(ad))
3513b3a8eb9SGleb Smirnoff 				return (EINVAL);
3523b3a8eb9SGleb Smirnoff 			p = pfr_lookup_addr(kt, ad, 1);
3533b3a8eb9SGleb Smirnoff 			if (p != NULL)
3543b3a8eb9SGleb Smirnoff 				p->pfrke_mark = 0;
3553b3a8eb9SGleb Smirnoff 		}
3563b3a8eb9SGleb Smirnoff 	}
3573b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
3583b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
3593b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
3603b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
3613b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
3623b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
3633b3a8eb9SGleb Smirnoff 			if (p == NULL)
3643b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
3653b3a8eb9SGleb Smirnoff 			else if (p->pfrke_not != ad->pfra_not)
3663b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_CONFLICT;
3673b3a8eb9SGleb Smirnoff 			else if (p->pfrke_mark)
3683b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DUPLICATE;
3693b3a8eb9SGleb Smirnoff 			else
3703b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DELETED;
3713b3a8eb9SGleb Smirnoff 		}
3723b3a8eb9SGleb Smirnoff 		if (p != NULL && p->pfrke_not == ad->pfra_not &&
3733b3a8eb9SGleb Smirnoff 		    !p->pfrke_mark) {
3743b3a8eb9SGleb Smirnoff 			p->pfrke_mark = 1;
3753b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
3763b3a8eb9SGleb Smirnoff 			xdel++;
3773b3a8eb9SGleb Smirnoff 		}
3783b3a8eb9SGleb Smirnoff 	}
3793b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
3803b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &workq);
3813b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
3823b3a8eb9SGleb Smirnoff 		*ndel = xdel;
3833b3a8eb9SGleb Smirnoff 	return (0);
3843b3a8eb9SGleb Smirnoff _bad:
3853b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
3863b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
3873b3a8eb9SGleb Smirnoff 	return (rv);
3883b3a8eb9SGleb Smirnoff }
3893b3a8eb9SGleb Smirnoff 
3903b3a8eb9SGleb Smirnoff int
3913b3a8eb9SGleb Smirnoff pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
3923b3a8eb9SGleb Smirnoff     int *size2, int *nadd, int *ndel, int *nchange, int flags,
3933b3a8eb9SGleb Smirnoff     u_int32_t ignore_pfrt_flags)
3943b3a8eb9SGleb Smirnoff {
3953b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *tmpkt;
3963b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addq, delq, changeq;
3973b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
3983b3a8eb9SGleb Smirnoff 	struct pfr_addr		 ad;
3993b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0, xdel = 0, xchange = 0;
4003b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
4013b3a8eb9SGleb Smirnoff 
4023b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
4033b3a8eb9SGleb Smirnoff 
4043b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
4053b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, ignore_pfrt_flags, flags &
4063b3a8eb9SGleb Smirnoff 	    PFR_FLAG_USERIOCTL))
4073b3a8eb9SGleb Smirnoff 		return (EINVAL);
4083b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
4093b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
4103b3a8eb9SGleb Smirnoff 		return (ESRCH);
4113b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
4123b3a8eb9SGleb Smirnoff 		return (EPERM);
4131e9e3741SMarko Zec 	tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0);
4143b3a8eb9SGleb Smirnoff 	if (tmpkt == NULL)
4153b3a8eb9SGleb Smirnoff 		return (ENOMEM);
4163b3a8eb9SGleb Smirnoff 	pfr_mark_addrs(kt);
4173b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addq);
4183b3a8eb9SGleb Smirnoff 	SLIST_INIT(&delq);
4193b3a8eb9SGleb Smirnoff 	SLIST_INIT(&changeq);
4203b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
4213b3a8eb9SGleb Smirnoff 		/*
4223b3a8eb9SGleb Smirnoff 		 * XXXGL: undertand pf_if usage of this function
4233b3a8eb9SGleb Smirnoff 		 * and make ad a moving pointer
4243b3a8eb9SGleb Smirnoff 		 */
4253b3a8eb9SGleb Smirnoff 		bcopy(addr + i, &ad, sizeof(ad));
4263b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(&ad))
4273b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
4283b3a8eb9SGleb Smirnoff 		ad.pfra_fback = PFR_FB_NONE;
4293b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, &ad, 1);
4303b3a8eb9SGleb Smirnoff 		if (p != NULL) {
4313b3a8eb9SGleb Smirnoff 			if (p->pfrke_mark) {
4323b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_DUPLICATE;
4333b3a8eb9SGleb Smirnoff 				goto _skip;
4343b3a8eb9SGleb Smirnoff 			}
4353b3a8eb9SGleb Smirnoff 			p->pfrke_mark = 1;
4363b3a8eb9SGleb Smirnoff 			if (p->pfrke_not != ad.pfra_not) {
4373b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&changeq, p, pfrke_workq);
4383b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_CHANGED;
4393b3a8eb9SGleb Smirnoff 				xchange++;
4403b3a8eb9SGleb Smirnoff 			}
4413b3a8eb9SGleb Smirnoff 		} else {
4423b3a8eb9SGleb Smirnoff 			q = pfr_lookup_addr(tmpkt, &ad, 1);
4433b3a8eb9SGleb Smirnoff 			if (q != NULL) {
4443b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_DUPLICATE;
4453b3a8eb9SGleb Smirnoff 				goto _skip;
4463b3a8eb9SGleb Smirnoff 			}
44721121f9bSMark Johnston 			p = pfr_create_kentry(&ad,
44821121f9bSMark Johnston 			    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
4493b3a8eb9SGleb Smirnoff 			if (p == NULL)
4503b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
4513b3a8eb9SGleb Smirnoff 			if (pfr_route_kentry(tmpkt, p)) {
4523b3a8eb9SGleb Smirnoff 				pfr_destroy_kentry(p);
4533b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_NONE;
4543b3a8eb9SGleb Smirnoff 			} else {
4553b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
4563b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_ADDED;
4573b3a8eb9SGleb Smirnoff 				xadd++;
4583b3a8eb9SGleb Smirnoff 			}
4593b3a8eb9SGleb Smirnoff 		}
4603b3a8eb9SGleb Smirnoff _skip:
4613b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK)
4623b3a8eb9SGleb Smirnoff 			bcopy(&ad, addr + i, sizeof(ad));
4633b3a8eb9SGleb Smirnoff 	}
4643b3a8eb9SGleb Smirnoff 	pfr_enqueue_addrs(kt, &delq, &xdel, ENQUEUE_UNMARKED_ONLY);
4653b3a8eb9SGleb Smirnoff 	if ((flags & PFR_FLAG_FEEDBACK) && *size2) {
4663b3a8eb9SGleb Smirnoff 		if (*size2 < size+xdel) {
4673b3a8eb9SGleb Smirnoff 			*size2 = size+xdel;
4683b3a8eb9SGleb Smirnoff 			senderr(0);
4693b3a8eb9SGleb Smirnoff 		}
4703b3a8eb9SGleb Smirnoff 		i = 0;
4713b3a8eb9SGleb Smirnoff 		SLIST_FOREACH(p, &delq, pfrke_workq) {
4723b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(&ad, p);
4733b3a8eb9SGleb Smirnoff 			ad.pfra_fback = PFR_FB_DELETED;
4743b3a8eb9SGleb Smirnoff 			bcopy(&ad, addr + size + i, sizeof(ad));
4753b3a8eb9SGleb Smirnoff 			i++;
4763b3a8eb9SGleb Smirnoff 		}
4773b3a8eb9SGleb Smirnoff 	}
4783b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &addq);
4793b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
4803b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &addq, tzero);
4813b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &delq);
48221121f9bSMark Johnston 		pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG);
4833b3a8eb9SGleb Smirnoff 	} else
4843b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addq);
4853b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
4863b3a8eb9SGleb Smirnoff 		*nadd = xadd;
4873b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
4883b3a8eb9SGleb Smirnoff 		*ndel = xdel;
4893b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
4903b3a8eb9SGleb Smirnoff 		*nchange = xchange;
4913b3a8eb9SGleb Smirnoff 	if ((flags & PFR_FLAG_FEEDBACK) && size2)
4923b3a8eb9SGleb Smirnoff 		*size2 = size+xdel;
4933b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
4943b3a8eb9SGleb Smirnoff 	return (0);
4953b3a8eb9SGleb Smirnoff _bad:
4963b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &addq);
4973b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&addq);
4983b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
4993b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
5003b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
5013b3a8eb9SGleb Smirnoff 	return (rv);
5023b3a8eb9SGleb Smirnoff }
5033b3a8eb9SGleb Smirnoff 
5043b3a8eb9SGleb Smirnoff int
5053b3a8eb9SGleb Smirnoff pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
5063b3a8eb9SGleb Smirnoff 	int *nmatch, int flags)
5073b3a8eb9SGleb Smirnoff {
5083b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5093b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
5103b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
5113b3a8eb9SGleb Smirnoff 	int			 i, xmatch = 0;
5123b3a8eb9SGleb Smirnoff 
5133b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5143b3a8eb9SGleb Smirnoff 
5153b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_REPLACE);
5163b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
5173b3a8eb9SGleb Smirnoff 		return (EINVAL);
5183b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
5193b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
5203b3a8eb9SGleb Smirnoff 		return (ESRCH);
5213b3a8eb9SGleb Smirnoff 
5223b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
5233b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
5243b3a8eb9SGleb Smirnoff 			return (EINVAL);
5253b3a8eb9SGleb Smirnoff 		if (ADDR_NETWORK(ad))
5263b3a8eb9SGleb Smirnoff 			return (EINVAL);
5273b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 0);
5283b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_REPLACE)
5293b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(ad, p);
5303b3a8eb9SGleb Smirnoff 		ad->pfra_fback = (p == NULL) ? PFR_FB_NONE :
5313b3a8eb9SGleb Smirnoff 		    (p->pfrke_not ? PFR_FB_NOTMATCH : PFR_FB_MATCH);
5323b3a8eb9SGleb Smirnoff 		if (p != NULL && !p->pfrke_not)
5333b3a8eb9SGleb Smirnoff 			xmatch++;
5343b3a8eb9SGleb Smirnoff 	}
5353b3a8eb9SGleb Smirnoff 	if (nmatch != NULL)
5363b3a8eb9SGleb Smirnoff 		*nmatch = xmatch;
5373b3a8eb9SGleb Smirnoff 	return (0);
5383b3a8eb9SGleb Smirnoff }
5393b3a8eb9SGleb Smirnoff 
5403b3a8eb9SGleb Smirnoff int
5413b3a8eb9SGleb Smirnoff pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
5423b3a8eb9SGleb Smirnoff 	int flags)
5433b3a8eb9SGleb Smirnoff {
5443b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5453b3a8eb9SGleb Smirnoff 	struct pfr_walktree	 w;
5463b3a8eb9SGleb Smirnoff 	int			 rv;
5473b3a8eb9SGleb Smirnoff 
5483b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5493b3a8eb9SGleb Smirnoff 
5503b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, 0);
5513b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
5523b3a8eb9SGleb Smirnoff 		return (EINVAL);
5533b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
5543b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
5553b3a8eb9SGleb Smirnoff 		return (ESRCH);
5563b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_cnt > *size) {
5573b3a8eb9SGleb Smirnoff 		*size = kt->pfrkt_cnt;
5583b3a8eb9SGleb Smirnoff 		return (0);
5593b3a8eb9SGleb Smirnoff 	}
5603b3a8eb9SGleb Smirnoff 
5613b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
5623b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_GET_ADDRS;
5633b3a8eb9SGleb Smirnoff 	w.pfrw_addr = addr;
5643b3a8eb9SGleb Smirnoff 	w.pfrw_free = kt->pfrkt_cnt;
56561eee0e2SAlexander V. Chernikov 	rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
5663b3a8eb9SGleb Smirnoff 	if (!rv)
56761eee0e2SAlexander V. Chernikov 		rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
56861eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w);
5693b3a8eb9SGleb Smirnoff 	if (rv)
5703b3a8eb9SGleb Smirnoff 		return (rv);
5713b3a8eb9SGleb Smirnoff 
5723b3a8eb9SGleb Smirnoff 	KASSERT(w.pfrw_free == 0, ("%s: corruption detected (%d)", __func__,
5733b3a8eb9SGleb Smirnoff 	    w.pfrw_free));
5743b3a8eb9SGleb Smirnoff 
5753b3a8eb9SGleb Smirnoff 	*size = kt->pfrkt_cnt;
5763b3a8eb9SGleb Smirnoff 	return (0);
5773b3a8eb9SGleb Smirnoff }
5783b3a8eb9SGleb Smirnoff 
5793b3a8eb9SGleb Smirnoff int
5803b3a8eb9SGleb Smirnoff pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
5813b3a8eb9SGleb Smirnoff 	int flags)
5823b3a8eb9SGleb Smirnoff {
5833b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5843b3a8eb9SGleb Smirnoff 	struct pfr_walktree	 w;
5853b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
5863b3a8eb9SGleb Smirnoff 	int			 rv;
5873b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
5883b3a8eb9SGleb Smirnoff 
5893b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5903b3a8eb9SGleb Smirnoff 
5913b3a8eb9SGleb Smirnoff 	/* XXX PFR_FLAG_CLSTATS disabled */
5923b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, 0);
5933b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
5943b3a8eb9SGleb Smirnoff 		return (EINVAL);
5953b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
5963b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
5973b3a8eb9SGleb Smirnoff 		return (ESRCH);
5983b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_cnt > *size) {
5993b3a8eb9SGleb Smirnoff 		*size = kt->pfrkt_cnt;
6003b3a8eb9SGleb Smirnoff 		return (0);
6013b3a8eb9SGleb Smirnoff 	}
6023b3a8eb9SGleb Smirnoff 
6033b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
6043b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_GET_ASTATS;
6053b3a8eb9SGleb Smirnoff 	w.pfrw_astats = addr;
6063b3a8eb9SGleb Smirnoff 	w.pfrw_free = kt->pfrkt_cnt;
60759048686SKristof Provost 	/*
60859048686SKristof Provost 	 * Flags below are for backward compatibility. It was possible to have
60959048686SKristof Provost 	 * a table without per-entry counters. Now they are always allocated,
61059048686SKristof Provost 	 * we just discard data when reading it if table is not configured to
61159048686SKristof Provost 	 * have counters.
61259048686SKristof Provost 	 */
61359048686SKristof Provost 	w.pfrw_flags = kt->pfrkt_flags;
61461eee0e2SAlexander V. Chernikov 	rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
6153b3a8eb9SGleb Smirnoff 	if (!rv)
61661eee0e2SAlexander V. Chernikov 		rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
61761eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w);
6183b3a8eb9SGleb Smirnoff 	if (!rv && (flags & PFR_FLAG_CLSTATS)) {
6193b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &workq, NULL, 0);
62021121f9bSMark Johnston 		pfr_clstats_kentries(kt, &workq, tzero, 0);
6213b3a8eb9SGleb Smirnoff 	}
6223b3a8eb9SGleb Smirnoff 	if (rv)
6233b3a8eb9SGleb Smirnoff 		return (rv);
6243b3a8eb9SGleb Smirnoff 
6253b3a8eb9SGleb Smirnoff 	if (w.pfrw_free) {
6263b3a8eb9SGleb Smirnoff 		printf("pfr_get_astats: corruption detected (%d).\n",
6273b3a8eb9SGleb Smirnoff 		    w.pfrw_free);
6283b3a8eb9SGleb Smirnoff 		return (ENOTTY);
6293b3a8eb9SGleb Smirnoff 	}
6303b3a8eb9SGleb Smirnoff 	*size = kt->pfrkt_cnt;
6313b3a8eb9SGleb Smirnoff 	return (0);
6323b3a8eb9SGleb Smirnoff }
6333b3a8eb9SGleb Smirnoff 
6343b3a8eb9SGleb Smirnoff int
6353b3a8eb9SGleb Smirnoff pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
6363b3a8eb9SGleb Smirnoff     int *nzero, int flags)
6373b3a8eb9SGleb Smirnoff {
6383b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
6393b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
6403b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
6413b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
6423b3a8eb9SGleb Smirnoff 	int			 i, rv, xzero = 0;
6433b3a8eb9SGleb Smirnoff 
6443b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
6453b3a8eb9SGleb Smirnoff 
6463b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
6473b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
6483b3a8eb9SGleb Smirnoff 		return (EINVAL);
6493b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
6503b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
6513b3a8eb9SGleb Smirnoff 		return (ESRCH);
6523b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
6533b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
6543b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
6553b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
6563b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
6573b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
6583b3a8eb9SGleb Smirnoff 			ad->pfra_fback = (p != NULL) ?
6593b3a8eb9SGleb Smirnoff 			    PFR_FB_CLEARED : PFR_FB_NONE;
6603b3a8eb9SGleb Smirnoff 		}
6613b3a8eb9SGleb Smirnoff 		if (p != NULL) {
6623b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
6633b3a8eb9SGleb Smirnoff 			xzero++;
6643b3a8eb9SGleb Smirnoff 		}
6653b3a8eb9SGleb Smirnoff 	}
6663b3a8eb9SGleb Smirnoff 
6673b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
6681a5dc6eeSKajetan Staszkiewicz 		pfr_clstats_kentries(kt, &workq, time_second, 0);
6693b3a8eb9SGleb Smirnoff 	if (nzero != NULL)
6703b3a8eb9SGleb Smirnoff 		*nzero = xzero;
6713b3a8eb9SGleb Smirnoff 	return (0);
6723b3a8eb9SGleb Smirnoff _bad:
6733b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
6743b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
6753b3a8eb9SGleb Smirnoff 	return (rv);
6763b3a8eb9SGleb Smirnoff }
6773b3a8eb9SGleb Smirnoff 
6783b3a8eb9SGleb Smirnoff static int
6793b3a8eb9SGleb Smirnoff pfr_validate_addr(struct pfr_addr *ad)
6803b3a8eb9SGleb Smirnoff {
6813b3a8eb9SGleb Smirnoff 	int i;
6823b3a8eb9SGleb Smirnoff 
6833b3a8eb9SGleb Smirnoff 	switch (ad->pfra_af) {
6843b3a8eb9SGleb Smirnoff #ifdef INET
6853b3a8eb9SGleb Smirnoff 	case AF_INET:
6863b3a8eb9SGleb Smirnoff 		if (ad->pfra_net > 32)
6873b3a8eb9SGleb Smirnoff 			return (-1);
6883b3a8eb9SGleb Smirnoff 		break;
6893b3a8eb9SGleb Smirnoff #endif /* INET */
6903b3a8eb9SGleb Smirnoff #ifdef INET6
6913b3a8eb9SGleb Smirnoff 	case AF_INET6:
6923b3a8eb9SGleb Smirnoff 		if (ad->pfra_net > 128)
6933b3a8eb9SGleb Smirnoff 			return (-1);
6943b3a8eb9SGleb Smirnoff 		break;
6953b3a8eb9SGleb Smirnoff #endif /* INET6 */
6963b3a8eb9SGleb Smirnoff 	default:
6973b3a8eb9SGleb Smirnoff 		return (-1);
6983b3a8eb9SGleb Smirnoff 	}
6993b3a8eb9SGleb Smirnoff 	if (ad->pfra_net < 128 &&
7003b3a8eb9SGleb Smirnoff 		(((caddr_t)ad)[ad->pfra_net/8] & (0xFF >> (ad->pfra_net%8))))
7013b3a8eb9SGleb Smirnoff 			return (-1);
7023b3a8eb9SGleb Smirnoff 	for (i = (ad->pfra_net+7)/8; i < sizeof(ad->pfra_u); i++)
7033b3a8eb9SGleb Smirnoff 		if (((caddr_t)ad)[i])
7043b3a8eb9SGleb Smirnoff 			return (-1);
7053b3a8eb9SGleb Smirnoff 	if (ad->pfra_not && ad->pfra_not != 1)
7063b3a8eb9SGleb Smirnoff 		return (-1);
7073b3a8eb9SGleb Smirnoff 	if (ad->pfra_fback)
7083b3a8eb9SGleb Smirnoff 		return (-1);
7093b3a8eb9SGleb Smirnoff 	return (0);
7103b3a8eb9SGleb Smirnoff }
7113b3a8eb9SGleb Smirnoff 
7123b3a8eb9SGleb Smirnoff static void
7133b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
7143b3a8eb9SGleb Smirnoff 	int *naddr, int sweep)
7153b3a8eb9SGleb Smirnoff {
7163b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
7173b3a8eb9SGleb Smirnoff 
7183b3a8eb9SGleb Smirnoff 	SLIST_INIT(workq);
7193b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
7203b3a8eb9SGleb Smirnoff 	w.pfrw_op = sweep ? PFRW_SWEEP : PFRW_ENQUEUE;
7213b3a8eb9SGleb Smirnoff 	w.pfrw_workq = workq;
7223b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_ip4 != NULL)
72361eee0e2SAlexander V. Chernikov 		if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh,
72461eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w))
7253b3a8eb9SGleb Smirnoff 			printf("pfr_enqueue_addrs: IPv4 walktree failed.\n");
7263b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_ip6 != NULL)
72761eee0e2SAlexander V. Chernikov 		if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
72861eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w))
7293b3a8eb9SGleb Smirnoff 			printf("pfr_enqueue_addrs: IPv6 walktree failed.\n");
7303b3a8eb9SGleb Smirnoff 	if (naddr != NULL)
7317b676698SKristof Provost 		*naddr = w.pfrw_free;
7323b3a8eb9SGleb Smirnoff }
7333b3a8eb9SGleb Smirnoff 
7343b3a8eb9SGleb Smirnoff static void
7353b3a8eb9SGleb Smirnoff pfr_mark_addrs(struct pfr_ktable *kt)
7363b3a8eb9SGleb Smirnoff {
7373b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
7383b3a8eb9SGleb Smirnoff 
7393b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
7403b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_MARK;
74161eee0e2SAlexander V. Chernikov 	if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w))
7423b3a8eb9SGleb Smirnoff 		printf("pfr_mark_addrs: IPv4 walktree failed.\n");
74361eee0e2SAlexander V. Chernikov 	if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w))
7443b3a8eb9SGleb Smirnoff 		printf("pfr_mark_addrs: IPv6 walktree failed.\n");
7453b3a8eb9SGleb Smirnoff }
7463b3a8eb9SGleb Smirnoff 
7473b3a8eb9SGleb Smirnoff static struct pfr_kentry *
7483b3a8eb9SGleb Smirnoff pfr_lookup_addr(struct pfr_ktable *kt, struct pfr_addr *ad, int exact)
7493b3a8eb9SGleb Smirnoff {
7503b3a8eb9SGleb Smirnoff 	union sockaddr_union	 sa, mask;
75161eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
7523b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke;
7533b3a8eb9SGleb Smirnoff 
75429bdd62cSGleb Smirnoff 	PF_RULES_ASSERT();
75529bdd62cSGleb Smirnoff 
7563b3a8eb9SGleb Smirnoff 	bzero(&sa, sizeof(sa));
7573b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET) {
7583b3a8eb9SGleb Smirnoff 		FILLIN_SIN(sa.sin, ad->pfra_ip4addr);
75961eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
7603b3a8eb9SGleb Smirnoff 	} else if ( ad->pfra_af == AF_INET6 ) {
7613b3a8eb9SGleb Smirnoff 		FILLIN_SIN6(sa.sin6, ad->pfra_ip6addr);
76261eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
7633b3a8eb9SGleb Smirnoff 	}
7643b3a8eb9SGleb Smirnoff 	if (ADDR_NETWORK(ad)) {
7653b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ad->pfra_af, ad->pfra_net);
7663b3a8eb9SGleb Smirnoff 		ke = (struct pfr_kentry *)rn_lookup(&sa, &mask, head);
7673b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
7683b3a8eb9SGleb Smirnoff 			ke = NULL;
7693b3a8eb9SGleb Smirnoff 	} else {
7703b3a8eb9SGleb Smirnoff 		ke = (struct pfr_kentry *)rn_match(&sa, head);
7713b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
7723b3a8eb9SGleb Smirnoff 			ke = NULL;
7733b3a8eb9SGleb Smirnoff 		if (exact && ke && KENTRY_NETWORK(ke))
7743b3a8eb9SGleb Smirnoff 			ke = NULL;
7753b3a8eb9SGleb Smirnoff 	}
7763b3a8eb9SGleb Smirnoff 	return (ke);
7773b3a8eb9SGleb Smirnoff }
7783b3a8eb9SGleb Smirnoff 
7793b3a8eb9SGleb Smirnoff static struct pfr_kentry *
78021121f9bSMark Johnston pfr_create_kentry(struct pfr_addr *ad, bool counters)
7813b3a8eb9SGleb Smirnoff {
7823b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke;
783c1be8399SMark Johnston 	counter_u64_t		 c;
7843b3a8eb9SGleb Smirnoff 
7853b3a8eb9SGleb Smirnoff 	ke = uma_zalloc(V_pfr_kentry_z, M_NOWAIT | M_ZERO);
7863b3a8eb9SGleb Smirnoff 	if (ke == NULL)
7873b3a8eb9SGleb Smirnoff 		return (NULL);
7883b3a8eb9SGleb Smirnoff 
7893b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET)
7903b3a8eb9SGleb Smirnoff 		FILLIN_SIN(ke->pfrke_sa.sin, ad->pfra_ip4addr);
7913b3a8eb9SGleb Smirnoff 	else if (ad->pfra_af == AF_INET6)
7923b3a8eb9SGleb Smirnoff 		FILLIN_SIN6(ke->pfrke_sa.sin6, ad->pfra_ip6addr);
7933b3a8eb9SGleb Smirnoff 	ke->pfrke_af = ad->pfra_af;
7943b3a8eb9SGleb Smirnoff 	ke->pfrke_net = ad->pfra_net;
7953b3a8eb9SGleb Smirnoff 	ke->pfrke_not = ad->pfra_not;
79621121f9bSMark Johnston 	ke->pfrke_counters.pfrkc_tzero = 0;
797c1be8399SMark Johnston 	if (counters) {
798c1be8399SMark Johnston 		c = uma_zalloc_pcpu(V_pfr_kentry_counter_z, M_NOWAIT | M_ZERO);
799c1be8399SMark Johnston 		if (c == NULL) {
80059048686SKristof Provost 			pfr_destroy_kentry(ke);
80159048686SKristof Provost 			return (NULL);
80259048686SKristof Provost 		}
803c1be8399SMark Johnston 		ke->pfrke_counters.pfrkc_counters = c;
80459048686SKristof Provost 	}
8053b3a8eb9SGleb Smirnoff 	return (ke);
8063b3a8eb9SGleb Smirnoff }
8073b3a8eb9SGleb Smirnoff 
8083b3a8eb9SGleb Smirnoff static void
8093b3a8eb9SGleb Smirnoff pfr_destroy_kentries(struct pfr_kentryworkq *workq)
8103b3a8eb9SGleb Smirnoff {
8113b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
8123b3a8eb9SGleb Smirnoff 
8133b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p != NULL; p = q) {
8143b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrke_workq);
8153b3a8eb9SGleb Smirnoff 		pfr_destroy_kentry(p);
8163b3a8eb9SGleb Smirnoff 	}
8173b3a8eb9SGleb Smirnoff }
8183b3a8eb9SGleb Smirnoff 
8193b3a8eb9SGleb Smirnoff static void
820c1be8399SMark Johnston pfr_destroy_kentry(struct pfr_kentry *ke)
82159048686SKristof Provost {
82221121f9bSMark Johnston 	counter_u64_t c;
82321121f9bSMark Johnston 
824c1be8399SMark Johnston 	if ((c = ke->pfrke_counters.pfrkc_counters) != NULL)
825c1be8399SMark Johnston 		uma_zfree_pcpu(V_pfr_kentry_counter_z, c);
8263b3a8eb9SGleb Smirnoff 	uma_zfree(V_pfr_kentry_z, ke);
8273b3a8eb9SGleb Smirnoff }
8283b3a8eb9SGleb Smirnoff 
8293b3a8eb9SGleb Smirnoff static void
8303b3a8eb9SGleb Smirnoff pfr_insert_kentries(struct pfr_ktable *kt,
8313b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq, long tzero)
8323b3a8eb9SGleb Smirnoff {
8333b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8343b3a8eb9SGleb Smirnoff 	int			 rv, n = 0;
8353b3a8eb9SGleb Smirnoff 
8363b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
8373b3a8eb9SGleb Smirnoff 		rv = pfr_route_kentry(kt, p);
8383b3a8eb9SGleb Smirnoff 		if (rv) {
8393b3a8eb9SGleb Smirnoff 			printf("pfr_insert_kentries: cannot route entry "
8403b3a8eb9SGleb Smirnoff 			    "(code=%d).\n", rv);
8413b3a8eb9SGleb Smirnoff 			break;
8423b3a8eb9SGleb Smirnoff 		}
84359048686SKristof Provost 		p->pfrke_counters.pfrkc_tzero = tzero;
8443b3a8eb9SGleb Smirnoff 		n++;
8453b3a8eb9SGleb Smirnoff 	}
8463b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt += n;
8473b3a8eb9SGleb Smirnoff }
8483b3a8eb9SGleb Smirnoff 
8493b3a8eb9SGleb Smirnoff int
8503b3a8eb9SGleb Smirnoff pfr_insert_kentry(struct pfr_ktable *kt, struct pfr_addr *ad, long tzero)
8513b3a8eb9SGleb Smirnoff {
8523b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8533b3a8eb9SGleb Smirnoff 	int			 rv;
8543b3a8eb9SGleb Smirnoff 
8553b3a8eb9SGleb Smirnoff 	p = pfr_lookup_addr(kt, ad, 1);
8563b3a8eb9SGleb Smirnoff 	if (p != NULL)
8573b3a8eb9SGleb Smirnoff 		return (0);
85821121f9bSMark Johnston 	p = pfr_create_kentry(ad, (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
8593b3a8eb9SGleb Smirnoff 	if (p == NULL)
860e706fd3aSGleb Smirnoff 		return (ENOMEM);
8613b3a8eb9SGleb Smirnoff 
8623b3a8eb9SGleb Smirnoff 	rv = pfr_route_kentry(kt, p);
8633b3a8eb9SGleb Smirnoff 	if (rv)
8643b3a8eb9SGleb Smirnoff 		return (rv);
8653b3a8eb9SGleb Smirnoff 
86659048686SKristof Provost 	p->pfrke_counters.pfrkc_tzero = tzero;
8673b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt++;
8683b3a8eb9SGleb Smirnoff 
8693b3a8eb9SGleb Smirnoff 	return (0);
8703b3a8eb9SGleb Smirnoff }
8713b3a8eb9SGleb Smirnoff 
8723b3a8eb9SGleb Smirnoff static void
8733b3a8eb9SGleb Smirnoff pfr_remove_kentries(struct pfr_ktable *kt,
8743b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq)
8753b3a8eb9SGleb Smirnoff {
8763b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8773b3a8eb9SGleb Smirnoff 	int			 n = 0;
8783b3a8eb9SGleb Smirnoff 
8793b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
8803b3a8eb9SGleb Smirnoff 		pfr_unroute_kentry(kt, p);
8813b3a8eb9SGleb Smirnoff 		n++;
8823b3a8eb9SGleb Smirnoff 	}
8833b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt -= n;
8843b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(workq);
8853b3a8eb9SGleb Smirnoff }
8863b3a8eb9SGleb Smirnoff 
8873b3a8eb9SGleb Smirnoff static void
8883b3a8eb9SGleb Smirnoff pfr_clean_node_mask(struct pfr_ktable *kt,
8893b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq)
8903b3a8eb9SGleb Smirnoff {
8913b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8923b3a8eb9SGleb Smirnoff 
8933b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq)
8943b3a8eb9SGleb Smirnoff 		pfr_unroute_kentry(kt, p);
8953b3a8eb9SGleb Smirnoff }
8963b3a8eb9SGleb Smirnoff 
8973b3a8eb9SGleb Smirnoff static void
89821121f9bSMark Johnston pfr_clstats_kentries(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
89921121f9bSMark Johnston     long tzero, int negchange)
9003b3a8eb9SGleb Smirnoff {
9013b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
902c1be8399SMark Johnston 	int			 i;
9033b3a8eb9SGleb Smirnoff 
9043b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
9053b3a8eb9SGleb Smirnoff 		if (negchange)
9063b3a8eb9SGleb Smirnoff 			p->pfrke_not = !p->pfrke_not;
90721121f9bSMark Johnston 		if ((kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0)
908c1be8399SMark Johnston 			for (i = 0; i < PFR_NUM_COUNTERS; i++)
909c1be8399SMark Johnston 				counter_u64_zero(
910c1be8399SMark Johnston 				    p->pfrke_counters.pfrkc_counters + i);
91159048686SKristof Provost 		p->pfrke_counters.pfrkc_tzero = tzero;
9123b3a8eb9SGleb Smirnoff 	}
9133b3a8eb9SGleb Smirnoff }
9143b3a8eb9SGleb Smirnoff 
9153b3a8eb9SGleb Smirnoff static void
9163b3a8eb9SGleb Smirnoff pfr_reset_feedback(struct pfr_addr *addr, int size)
9173b3a8eb9SGleb Smirnoff {
9183b3a8eb9SGleb Smirnoff 	struct pfr_addr	*ad;
9193b3a8eb9SGleb Smirnoff 	int		i;
9203b3a8eb9SGleb Smirnoff 
9213b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++)
9223b3a8eb9SGleb Smirnoff 		ad->pfra_fback = PFR_FB_NONE;
9233b3a8eb9SGleb Smirnoff }
9243b3a8eb9SGleb Smirnoff 
9253b3a8eb9SGleb Smirnoff static void
9263b3a8eb9SGleb Smirnoff pfr_prepare_network(union sockaddr_union *sa, int af, int net)
9273b3a8eb9SGleb Smirnoff {
9283b3a8eb9SGleb Smirnoff 	int	i;
9293b3a8eb9SGleb Smirnoff 
9303b3a8eb9SGleb Smirnoff 	bzero(sa, sizeof(*sa));
9313b3a8eb9SGleb Smirnoff 	if (af == AF_INET) {
9323b3a8eb9SGleb Smirnoff 		sa->sin.sin_len = sizeof(sa->sin);
9333b3a8eb9SGleb Smirnoff 		sa->sin.sin_family = AF_INET;
9343b3a8eb9SGleb Smirnoff 		sa->sin.sin_addr.s_addr = net ? htonl(-1 << (32-net)) : 0;
9353b3a8eb9SGleb Smirnoff 	} else if (af == AF_INET6) {
9363b3a8eb9SGleb Smirnoff 		sa->sin6.sin6_len = sizeof(sa->sin6);
9373b3a8eb9SGleb Smirnoff 		sa->sin6.sin6_family = AF_INET6;
9383b3a8eb9SGleb Smirnoff 		for (i = 0; i < 4; i++) {
9393b3a8eb9SGleb Smirnoff 			if (net <= 32) {
9403b3a8eb9SGleb Smirnoff 				sa->sin6.sin6_addr.s6_addr32[i] =
9413b3a8eb9SGleb Smirnoff 				    net ? htonl(-1 << (32-net)) : 0;
9423b3a8eb9SGleb Smirnoff 				break;
9433b3a8eb9SGleb Smirnoff 			}
9443b3a8eb9SGleb Smirnoff 			sa->sin6.sin6_addr.s6_addr32[i] = 0xFFFFFFFF;
9453b3a8eb9SGleb Smirnoff 			net -= 32;
9463b3a8eb9SGleb Smirnoff 		}
9473b3a8eb9SGleb Smirnoff 	}
9483b3a8eb9SGleb Smirnoff }
9493b3a8eb9SGleb Smirnoff 
9503b3a8eb9SGleb Smirnoff static int
9513b3a8eb9SGleb Smirnoff pfr_route_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
9523b3a8eb9SGleb Smirnoff {
9533b3a8eb9SGleb Smirnoff 	union sockaddr_union	 mask;
9543b3a8eb9SGleb Smirnoff 	struct radix_node	*rn;
95561eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
9563b3a8eb9SGleb Smirnoff 
95729bdd62cSGleb Smirnoff 	PF_RULES_WASSERT();
95829bdd62cSGleb Smirnoff 
9593b3a8eb9SGleb Smirnoff 	bzero(ke->pfrke_node, sizeof(ke->pfrke_node));
9603b3a8eb9SGleb Smirnoff 	if (ke->pfrke_af == AF_INET)
96161eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
9623b3a8eb9SGleb Smirnoff 	else if (ke->pfrke_af == AF_INET6)
96361eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
9643b3a8eb9SGleb Smirnoff 
9653b3a8eb9SGleb Smirnoff 	if (KENTRY_NETWORK(ke)) {
9663b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
9673b3a8eb9SGleb Smirnoff 		rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node);
9683b3a8eb9SGleb Smirnoff 	} else
9693b3a8eb9SGleb Smirnoff 		rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node);
9703b3a8eb9SGleb Smirnoff 
9713b3a8eb9SGleb Smirnoff 	return (rn == NULL ? -1 : 0);
9723b3a8eb9SGleb Smirnoff }
9733b3a8eb9SGleb Smirnoff 
9743b3a8eb9SGleb Smirnoff static int
9753b3a8eb9SGleb Smirnoff pfr_unroute_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
9763b3a8eb9SGleb Smirnoff {
9773b3a8eb9SGleb Smirnoff 	union sockaddr_union	 mask;
9783b3a8eb9SGleb Smirnoff 	struct radix_node	*rn;
97961eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
9803b3a8eb9SGleb Smirnoff 
9813b3a8eb9SGleb Smirnoff 	if (ke->pfrke_af == AF_INET)
98261eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
9833b3a8eb9SGleb Smirnoff 	else if (ke->pfrke_af == AF_INET6)
98461eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
9853b3a8eb9SGleb Smirnoff 
9863b3a8eb9SGleb Smirnoff 	if (KENTRY_NETWORK(ke)) {
9873b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
9883b3a8eb9SGleb Smirnoff 		rn = rn_delete(&ke->pfrke_sa, &mask, head);
9893b3a8eb9SGleb Smirnoff 	} else
9903b3a8eb9SGleb Smirnoff 		rn = rn_delete(&ke->pfrke_sa, NULL, head);
9913b3a8eb9SGleb Smirnoff 
9923b3a8eb9SGleb Smirnoff 	if (rn == NULL) {
9933b3a8eb9SGleb Smirnoff 		printf("pfr_unroute_kentry: delete failed.\n");
9943b3a8eb9SGleb Smirnoff 		return (-1);
9953b3a8eb9SGleb Smirnoff 	}
9963b3a8eb9SGleb Smirnoff 	return (0);
9973b3a8eb9SGleb Smirnoff }
9983b3a8eb9SGleb Smirnoff 
9993b3a8eb9SGleb Smirnoff static void
100059048686SKristof Provost pfr_copyout_addr(struct pfr_addr *ad, const struct pfr_kentry *ke)
10013b3a8eb9SGleb Smirnoff {
10023b3a8eb9SGleb Smirnoff 	bzero(ad, sizeof(*ad));
10033b3a8eb9SGleb Smirnoff 	if (ke == NULL)
10043b3a8eb9SGleb Smirnoff 		return;
10053b3a8eb9SGleb Smirnoff 	ad->pfra_af = ke->pfrke_af;
10063b3a8eb9SGleb Smirnoff 	ad->pfra_net = ke->pfrke_net;
10073b3a8eb9SGleb Smirnoff 	ad->pfra_not = ke->pfrke_not;
10083b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET)
10093b3a8eb9SGleb Smirnoff 		ad->pfra_ip4addr = ke->pfrke_sa.sin.sin_addr;
10103b3a8eb9SGleb Smirnoff 	else if (ad->pfra_af == AF_INET6)
10113b3a8eb9SGleb Smirnoff 		ad->pfra_ip6addr = ke->pfrke_sa.sin6.sin6_addr;
10123b3a8eb9SGleb Smirnoff }
10133b3a8eb9SGleb Smirnoff 
101459048686SKristof Provost static void
101559048686SKristof Provost pfr_copyout_astats(struct pfr_astats *as, const struct pfr_kentry *ke,
101659048686SKristof Provost     const struct pfr_walktree *w)
101759048686SKristof Provost {
101859048686SKristof Provost 	int dir, op;
101959048686SKristof Provost 	const struct pfr_kcounters *kc = &ke->pfrke_counters;
102059048686SKristof Provost 
102160a38abbSMark Johnston 	bzero(as, sizeof(*as));
102259048686SKristof Provost 	pfr_copyout_addr(&as->pfras_a, ke);
102359048686SKristof Provost 	as->pfras_tzero = kc->pfrkc_tzero;
102459048686SKristof Provost 
1025b21826bfSKristof Provost 	if (! (w->pfrw_flags & PFR_TFLAG_COUNTERS) ||
1026b21826bfSKristof Provost 	    kc->pfrkc_counters == NULL) {
102759048686SKristof Provost 		bzero(as->pfras_packets, sizeof(as->pfras_packets));
102859048686SKristof Provost 		bzero(as->pfras_bytes, sizeof(as->pfras_bytes));
102959048686SKristof Provost 		as->pfras_a.pfra_fback = PFR_FB_NOCOUNT;
103059048686SKristof Provost 		return;
103159048686SKristof Provost 	}
103259048686SKristof Provost 
103359048686SKristof Provost 	for (dir = 0; dir < PFR_DIR_MAX; dir++) {
103459048686SKristof Provost 		for (op = 0; op < PFR_OP_ADDR_MAX; op ++) {
1035c1be8399SMark Johnston 			as->pfras_packets[dir][op] = counter_u64_fetch(
1036c1be8399SMark Johnston 			    pfr_kentry_counter(kc, dir, op, PFR_TYPE_PACKETS));
1037c1be8399SMark Johnston 			as->pfras_bytes[dir][op] = counter_u64_fetch(
1038c1be8399SMark Johnston 			    pfr_kentry_counter(kc, dir, op, PFR_TYPE_BYTES));
103959048686SKristof Provost 		}
104059048686SKristof Provost 	}
104159048686SKristof Provost }
104259048686SKristof Provost 
10438ca12190SKristof Provost static void
10448ca12190SKristof Provost pfr_sockaddr_to_pf_addr(const union sockaddr_union *sa, struct pf_addr *a)
10458ca12190SKristof Provost {
10468ca12190SKristof Provost 	switch (sa->sa.sa_family) {
10478ca12190SKristof Provost 	case AF_INET:
10488ca12190SKristof Provost 		memcpy(&a->v4, &sa->sin.sin_addr, sizeof(a->v4));
10498ca12190SKristof Provost 		break;
10508ca12190SKristof Provost 	case AF_INET6:
10518ca12190SKristof Provost 		memcpy(&a->v6, &sa->sin6.sin6_addr, sizeof(a->v6));
10528ca12190SKristof Provost 		break;
10538ca12190SKristof Provost 	default:
10548ca12190SKristof Provost 		panic("Unknown AF");
10558ca12190SKristof Provost 	}
10568ca12190SKristof Provost }
10578ca12190SKristof Provost 
10583b3a8eb9SGleb Smirnoff static int
10593b3a8eb9SGleb Smirnoff pfr_walktree(struct radix_node *rn, void *arg)
10603b3a8eb9SGleb Smirnoff {
10613b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = (struct pfr_kentry *)rn;
10623b3a8eb9SGleb Smirnoff 	struct pfr_walktree	*w = arg;
10633b3a8eb9SGleb Smirnoff 
10643b3a8eb9SGleb Smirnoff 	switch (w->pfrw_op) {
10653b3a8eb9SGleb Smirnoff 	case PFRW_MARK:
10663b3a8eb9SGleb Smirnoff 		ke->pfrke_mark = 0;
10673b3a8eb9SGleb Smirnoff 		break;
10683b3a8eb9SGleb Smirnoff 	case PFRW_SWEEP:
10693b3a8eb9SGleb Smirnoff 		if (ke->pfrke_mark)
10703b3a8eb9SGleb Smirnoff 			break;
10713b3a8eb9SGleb Smirnoff 		/* FALLTHROUGH */
10723b3a8eb9SGleb Smirnoff 	case PFRW_ENQUEUE:
10733b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq);
10747b676698SKristof Provost 		w->pfrw_free++;
10753b3a8eb9SGleb Smirnoff 		break;
10763b3a8eb9SGleb Smirnoff 	case PFRW_GET_ADDRS:
10773b3a8eb9SGleb Smirnoff 		if (w->pfrw_free-- > 0) {
10783b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(w->pfrw_addr, ke);
10793b3a8eb9SGleb Smirnoff 			w->pfrw_addr++;
10803b3a8eb9SGleb Smirnoff 		}
10813b3a8eb9SGleb Smirnoff 		break;
10823b3a8eb9SGleb Smirnoff 	case PFRW_GET_ASTATS:
10833b3a8eb9SGleb Smirnoff 		if (w->pfrw_free-- > 0) {
10843b3a8eb9SGleb Smirnoff 			struct pfr_astats as;
10853b3a8eb9SGleb Smirnoff 
108659048686SKristof Provost 			pfr_copyout_astats(&as, ke, w);
10873b3a8eb9SGleb Smirnoff 
10883b3a8eb9SGleb Smirnoff 			bcopy(&as, w->pfrw_astats, sizeof(as));
10893b3a8eb9SGleb Smirnoff 			w->pfrw_astats++;
10903b3a8eb9SGleb Smirnoff 		}
10913b3a8eb9SGleb Smirnoff 		break;
10923b3a8eb9SGleb Smirnoff 	case PFRW_POOL_GET:
10933b3a8eb9SGleb Smirnoff 		if (ke->pfrke_not)
10943b3a8eb9SGleb Smirnoff 			break; /* negative entries are ignored */
10957b676698SKristof Provost 		if (!w->pfrw_free--) {
10963b3a8eb9SGleb Smirnoff 			w->pfrw_kentry = ke;
10973b3a8eb9SGleb Smirnoff 			return (1); /* finish search */
10983b3a8eb9SGleb Smirnoff 		}
10993b3a8eb9SGleb Smirnoff 		break;
11003b3a8eb9SGleb Smirnoff 	case PFRW_DYNADDR_UPDATE:
11013b3a8eb9SGleb Smirnoff 	    {
11023b3a8eb9SGleb Smirnoff 		union sockaddr_union	pfr_mask;
11033b3a8eb9SGleb Smirnoff 
11043b3a8eb9SGleb Smirnoff 		if (ke->pfrke_af == AF_INET) {
11053b3a8eb9SGleb Smirnoff 			if (w->pfrw_dyn->pfid_acnt4++ > 0)
11063b3a8eb9SGleb Smirnoff 				break;
11073b3a8eb9SGleb Smirnoff 			pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net);
11088ca12190SKristof Provost 			pfr_sockaddr_to_pf_addr(&ke->pfrke_sa, &w->pfrw_dyn->pfid_addr4);
11098ca12190SKristof Provost 			pfr_sockaddr_to_pf_addr(&pfr_mask, &w->pfrw_dyn->pfid_mask4);
11103b3a8eb9SGleb Smirnoff 		} else if (ke->pfrke_af == AF_INET6){
11113b3a8eb9SGleb Smirnoff 			if (w->pfrw_dyn->pfid_acnt6++ > 0)
11123b3a8eb9SGleb Smirnoff 				break;
11133b3a8eb9SGleb Smirnoff 			pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net);
11148ca12190SKristof Provost 			pfr_sockaddr_to_pf_addr(&ke->pfrke_sa, &w->pfrw_dyn->pfid_addr6);
11158ca12190SKristof Provost 			pfr_sockaddr_to_pf_addr(&pfr_mask, &w->pfrw_dyn->pfid_mask6);
11163b3a8eb9SGleb Smirnoff 		}
11173b3a8eb9SGleb Smirnoff 		break;
11183b3a8eb9SGleb Smirnoff 	    }
1119b21826bfSKristof Provost 	case PFRW_COUNTERS:
1120b21826bfSKristof Provost 	    {
1121b21826bfSKristof Provost 		if (w->pfrw_flags & PFR_TFLAG_COUNTERS) {
1122b21826bfSKristof Provost 			if (ke->pfrke_counters.pfrkc_counters != NULL)
1123b21826bfSKristof Provost 				break;
1124b21826bfSKristof Provost 			ke->pfrke_counters.pfrkc_counters =
1125b21826bfSKristof Provost 			    uma_zalloc_pcpu(V_pfr_kentry_counter_z,
1126b21826bfSKristof Provost 			    M_NOWAIT | M_ZERO);
1127b21826bfSKristof Provost 		} else {
1128b21826bfSKristof Provost 			uma_zfree_pcpu(V_pfr_kentry_counter_z,
1129b21826bfSKristof Provost 			    ke->pfrke_counters.pfrkc_counters);
1130b21826bfSKristof Provost 			ke->pfrke_counters.pfrkc_counters = NULL;
1131b21826bfSKristof Provost 		}
1132b21826bfSKristof Provost 		break;
1133b21826bfSKristof Provost 	    }
11343b3a8eb9SGleb Smirnoff 	}
11353b3a8eb9SGleb Smirnoff 	return (0);
11363b3a8eb9SGleb Smirnoff }
11373b3a8eb9SGleb Smirnoff 
11383b3a8eb9SGleb Smirnoff int
11393b3a8eb9SGleb Smirnoff pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
11403b3a8eb9SGleb Smirnoff {
11413b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
11423b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
11433b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
11443b3a8eb9SGleb Smirnoff 
11453b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ALLRSETS);
11463b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
11473b3a8eb9SGleb Smirnoff 		return (EINVAL);
11483b3a8eb9SGleb Smirnoff 	if (pfr_table_count(filter, flags) < 0)
11493b3a8eb9SGleb Smirnoff 		return (ENOENT);
11503b3a8eb9SGleb Smirnoff 
11513b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
11521e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
11533b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
11543b3a8eb9SGleb Smirnoff 			continue;
11553b3a8eb9SGleb Smirnoff 		if (!strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR))
11563b3a8eb9SGleb Smirnoff 			continue;
11573b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE))
11583b3a8eb9SGleb Smirnoff 			continue;
11593b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
11603b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
11613b3a8eb9SGleb Smirnoff 		xdel++;
11623b3a8eb9SGleb Smirnoff 	}
11633b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
11643b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
11653b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
11663b3a8eb9SGleb Smirnoff 		*ndel = xdel;
11673b3a8eb9SGleb Smirnoff 	return (0);
11683b3a8eb9SGleb Smirnoff }
11693b3a8eb9SGleb Smirnoff 
11703b3a8eb9SGleb Smirnoff int
11713b3a8eb9SGleb Smirnoff pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
11723b3a8eb9SGleb Smirnoff {
11733b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 addq, changeq;
11743b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, *r, key;
11753b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0;
11763b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
11773b3a8eb9SGleb Smirnoff 
11783b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
11793b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addq);
11803b3a8eb9SGleb Smirnoff 	SLIST_INIT(&changeq);
11813b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
11823b3a8eb9SGleb Smirnoff 		bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t));
11833b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK,
11843b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
11853b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
11863b3a8eb9SGleb Smirnoff 		key.pfrkt_flags |= PFR_TFLAG_ACTIVE;
11871e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
11883b3a8eb9SGleb Smirnoff 		if (p == NULL) {
11893b3a8eb9SGleb Smirnoff 			p = pfr_create_ktable(&key.pfrkt_t, tzero, 1);
11903b3a8eb9SGleb Smirnoff 			if (p == NULL)
11913b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
11923b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &addq, pfrkt_workq) {
1193b4b8fa33SKristof Provost 				if (!pfr_ktable_compare(p, q)) {
1194b4b8fa33SKristof Provost 					pfr_destroy_ktable(p, 0);
11953b3a8eb9SGleb Smirnoff 					goto _skip;
11963b3a8eb9SGleb Smirnoff 				}
1197b4b8fa33SKristof Provost 			}
11983b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&addq, p, pfrkt_workq);
11993b3a8eb9SGleb Smirnoff 			xadd++;
12003b3a8eb9SGleb Smirnoff 			if (!key.pfrkt_anchor[0])
12013b3a8eb9SGleb Smirnoff 				goto _skip;
12023b3a8eb9SGleb Smirnoff 
12033b3a8eb9SGleb Smirnoff 			/* find or create root table */
12043b3a8eb9SGleb Smirnoff 			bzero(key.pfrkt_anchor, sizeof(key.pfrkt_anchor));
12051e9e3741SMarko Zec 			r = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
12063b3a8eb9SGleb Smirnoff 			if (r != NULL) {
12073b3a8eb9SGleb Smirnoff 				p->pfrkt_root = r;
12083b3a8eb9SGleb Smirnoff 				goto _skip;
12093b3a8eb9SGleb Smirnoff 			}
12103b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &addq, pfrkt_workq) {
12113b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(&key, q)) {
12123b3a8eb9SGleb Smirnoff 					p->pfrkt_root = q;
12133b3a8eb9SGleb Smirnoff 					goto _skip;
12143b3a8eb9SGleb Smirnoff 				}
12153b3a8eb9SGleb Smirnoff 			}
12163b3a8eb9SGleb Smirnoff 			key.pfrkt_flags = 0;
12173b3a8eb9SGleb Smirnoff 			r = pfr_create_ktable(&key.pfrkt_t, 0, 1);
12183b3a8eb9SGleb Smirnoff 			if (r == NULL)
12193b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
12203b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&addq, r, pfrkt_workq);
12213b3a8eb9SGleb Smirnoff 			p->pfrkt_root = r;
12223b3a8eb9SGleb Smirnoff 		} else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
12233b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &changeq, pfrkt_workq)
12243b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(&key, q))
12253b3a8eb9SGleb Smirnoff 					goto _skip;
12263b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = (p->pfrkt_flags &
12273b3a8eb9SGleb Smirnoff 			    ~PFR_TFLAG_USRMASK) | key.pfrkt_flags;
12283b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq);
12293b3a8eb9SGleb Smirnoff 			xadd++;
12303b3a8eb9SGleb Smirnoff 		}
12313b3a8eb9SGleb Smirnoff _skip:
12323b3a8eb9SGleb Smirnoff 	;
12333b3a8eb9SGleb Smirnoff 	}
12343b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
12353b3a8eb9SGleb Smirnoff 		pfr_insert_ktables(&addq);
12363b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&changeq);
12373b3a8eb9SGleb Smirnoff 	} else
12383b3a8eb9SGleb Smirnoff 		 pfr_destroy_ktables(&addq, 0);
12393b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
12403b3a8eb9SGleb Smirnoff 		*nadd = xadd;
12413b3a8eb9SGleb Smirnoff 	return (0);
12423b3a8eb9SGleb Smirnoff _bad:
12433b3a8eb9SGleb Smirnoff 	pfr_destroy_ktables(&addq, 0);
12443b3a8eb9SGleb Smirnoff 	return (rv);
12453b3a8eb9SGleb Smirnoff }
12463b3a8eb9SGleb Smirnoff 
12473b3a8eb9SGleb Smirnoff int
12483b3a8eb9SGleb Smirnoff pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags)
12493b3a8eb9SGleb Smirnoff {
12503b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
12513b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, key;
12523b3a8eb9SGleb Smirnoff 	int			 i, xdel = 0;
12533b3a8eb9SGleb Smirnoff 
12543b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
12553b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
12563b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
12573b3a8eb9SGleb Smirnoff 		bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t));
12583b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0,
12593b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
12603b3a8eb9SGleb Smirnoff 			return (EINVAL);
12611e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
12623b3a8eb9SGleb Smirnoff 		if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
12633b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &workq, pfrkt_workq)
12643b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(p, q))
12653b3a8eb9SGleb Smirnoff 					goto _skip;
12663b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
12673b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
12683b3a8eb9SGleb Smirnoff 			xdel++;
12693b3a8eb9SGleb Smirnoff 		}
12703b3a8eb9SGleb Smirnoff _skip:
12713b3a8eb9SGleb Smirnoff 	;
12723b3a8eb9SGleb Smirnoff 	}
12733b3a8eb9SGleb Smirnoff 
12743b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
12753b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
12763b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
12773b3a8eb9SGleb Smirnoff 		*ndel = xdel;
12783b3a8eb9SGleb Smirnoff 	return (0);
12793b3a8eb9SGleb Smirnoff }
12803b3a8eb9SGleb Smirnoff 
12813b3a8eb9SGleb Smirnoff int
12823b3a8eb9SGleb Smirnoff pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
12833b3a8eb9SGleb Smirnoff 	int flags)
12843b3a8eb9SGleb Smirnoff {
12853b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
12863b3a8eb9SGleb Smirnoff 	int			 n, nn;
12873b3a8eb9SGleb Smirnoff 
12883b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
12893b3a8eb9SGleb Smirnoff 
12903b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
12913b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
12923b3a8eb9SGleb Smirnoff 		return (EINVAL);
12933b3a8eb9SGleb Smirnoff 	n = nn = pfr_table_count(filter, flags);
12943b3a8eb9SGleb Smirnoff 	if (n < 0)
12953b3a8eb9SGleb Smirnoff 		return (ENOENT);
12963b3a8eb9SGleb Smirnoff 	if (n > *size) {
12973b3a8eb9SGleb Smirnoff 		*size = n;
12983b3a8eb9SGleb Smirnoff 		return (0);
12993b3a8eb9SGleb Smirnoff 	}
13001e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
13013b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
13023b3a8eb9SGleb Smirnoff 			continue;
13033b3a8eb9SGleb Smirnoff 		if (n-- <= 0)
13043b3a8eb9SGleb Smirnoff 			continue;
13053b3a8eb9SGleb Smirnoff 		bcopy(&p->pfrkt_t, tbl++, sizeof(*tbl));
13063b3a8eb9SGleb Smirnoff 	}
13073b3a8eb9SGleb Smirnoff 
13083b3a8eb9SGleb Smirnoff 	KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n));
13093b3a8eb9SGleb Smirnoff 
13103b3a8eb9SGleb Smirnoff 	*size = nn;
13113b3a8eb9SGleb Smirnoff 	return (0);
13123b3a8eb9SGleb Smirnoff }
13133b3a8eb9SGleb Smirnoff 
13143b3a8eb9SGleb Smirnoff int
13153b3a8eb9SGleb Smirnoff pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size,
13163b3a8eb9SGleb Smirnoff 	int flags)
13173b3a8eb9SGleb Smirnoff {
13183b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
13193b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
13203b3a8eb9SGleb Smirnoff 	int			 n, nn;
13213b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
132259048686SKristof Provost 	int			 pfr_dir, pfr_op;
13233b3a8eb9SGleb Smirnoff 
13243b3a8eb9SGleb Smirnoff 	/* XXX PFR_FLAG_CLSTATS disabled */
13253b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
13263b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
13273b3a8eb9SGleb Smirnoff 		return (EINVAL);
13283b3a8eb9SGleb Smirnoff 	n = nn = pfr_table_count(filter, flags);
13293b3a8eb9SGleb Smirnoff 	if (n < 0)
13303b3a8eb9SGleb Smirnoff 		return (ENOENT);
13313b3a8eb9SGleb Smirnoff 	if (n > *size) {
13323b3a8eb9SGleb Smirnoff 		*size = n;
13333b3a8eb9SGleb Smirnoff 		return (0);
13343b3a8eb9SGleb Smirnoff 	}
13353b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
13361e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
13373b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
13383b3a8eb9SGleb Smirnoff 			continue;
13393b3a8eb9SGleb Smirnoff 		if (n-- <= 0)
13403b3a8eb9SGleb Smirnoff 			continue;
134159048686SKristof Provost 		bcopy(&p->pfrkt_kts.pfrts_t, &tbl->pfrts_t,
134259048686SKristof Provost 		    sizeof(struct pfr_table));
134359048686SKristof Provost 		for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
134459048686SKristof Provost 			for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
134559048686SKristof Provost 				tbl->pfrts_packets[pfr_dir][pfr_op] =
1346f92c21a2SMateusz Guzik 				    pfr_kstate_counter_fetch(
1347f92c21a2SMateusz Guzik 					&p->pfrkt_packets[pfr_dir][pfr_op]);
134859048686SKristof Provost 				tbl->pfrts_bytes[pfr_dir][pfr_op] =
1349f92c21a2SMateusz Guzik 				    pfr_kstate_counter_fetch(
1350f92c21a2SMateusz Guzik 					&p->pfrkt_bytes[pfr_dir][pfr_op]);
135159048686SKristof Provost 			}
135259048686SKristof Provost 		}
1353f92c21a2SMateusz Guzik 		tbl->pfrts_match = pfr_kstate_counter_fetch(&p->pfrkt_match);
1354f92c21a2SMateusz Guzik 		tbl->pfrts_nomatch = pfr_kstate_counter_fetch(&p->pfrkt_nomatch);
135559048686SKristof Provost 		tbl->pfrts_tzero = p->pfrkt_tzero;
135659048686SKristof Provost 		tbl->pfrts_cnt = p->pfrkt_cnt;
135759048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_REFCNT_MAX; pfr_op++)
135859048686SKristof Provost 			tbl->pfrts_refcnt[pfr_op] = p->pfrkt_refcnt[pfr_op];
135959048686SKristof Provost 		tbl++;
13603b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
13613b3a8eb9SGleb Smirnoff 	}
13623b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_CLSTATS)
13633b3a8eb9SGleb Smirnoff 		pfr_clstats_ktables(&workq, tzero,
13643b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_ADDRSTOO);
13653b3a8eb9SGleb Smirnoff 
13663b3a8eb9SGleb Smirnoff 	KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n));
13673b3a8eb9SGleb Smirnoff 
13683b3a8eb9SGleb Smirnoff 	*size = nn;
13693b3a8eb9SGleb Smirnoff 	return (0);
13703b3a8eb9SGleb Smirnoff }
13713b3a8eb9SGleb Smirnoff 
13723b3a8eb9SGleb Smirnoff int
13733b3a8eb9SGleb Smirnoff pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
13743b3a8eb9SGleb Smirnoff {
13753b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
13763b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, key;
13773b3a8eb9SGleb Smirnoff 	int			 i, xzero = 0;
13783b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
13793b3a8eb9SGleb Smirnoff 
13803b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
13813b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
13823b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
13833b3a8eb9SGleb Smirnoff 		bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t));
13843b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0, 0))
13853b3a8eb9SGleb Smirnoff 			return (EINVAL);
13861e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
13873b3a8eb9SGleb Smirnoff 		if (p != NULL) {
13883b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
13893b3a8eb9SGleb Smirnoff 			xzero++;
13903b3a8eb9SGleb Smirnoff 		}
13913b3a8eb9SGleb Smirnoff 	}
13923b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
13933b3a8eb9SGleb Smirnoff 		pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO);
13943b3a8eb9SGleb Smirnoff 	if (nzero != NULL)
13953b3a8eb9SGleb Smirnoff 		*nzero = xzero;
13963b3a8eb9SGleb Smirnoff 	return (0);
13973b3a8eb9SGleb Smirnoff }
13983b3a8eb9SGleb Smirnoff 
13993b3a8eb9SGleb Smirnoff int
14003b3a8eb9SGleb Smirnoff pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag,
14013b3a8eb9SGleb Smirnoff 	int *nchange, int *ndel, int flags)
14023b3a8eb9SGleb Smirnoff {
14033b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
14043b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, key;
14053b3a8eb9SGleb Smirnoff 	int			 i, xchange = 0, xdel = 0;
14063b3a8eb9SGleb Smirnoff 
14073b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
14083b3a8eb9SGleb Smirnoff 	if ((setflag & ~PFR_TFLAG_USRMASK) ||
14093b3a8eb9SGleb Smirnoff 	    (clrflag & ~PFR_TFLAG_USRMASK) ||
14103b3a8eb9SGleb Smirnoff 	    (setflag & clrflag))
14113b3a8eb9SGleb Smirnoff 		return (EINVAL);
14123b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
14133b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
14143b3a8eb9SGleb Smirnoff 		bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t));
14153b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0,
14163b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
14173b3a8eb9SGleb Smirnoff 			return (EINVAL);
14181e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
14193b3a8eb9SGleb Smirnoff 		if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
14203b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = (p->pfrkt_flags | setflag) &
14213b3a8eb9SGleb Smirnoff 			    ~clrflag;
14223b3a8eb9SGleb Smirnoff 			if (p->pfrkt_nflags == p->pfrkt_flags)
14233b3a8eb9SGleb Smirnoff 				goto _skip;
14243b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &workq, pfrkt_workq)
14253b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(p, q))
14263b3a8eb9SGleb Smirnoff 					goto _skip;
14273b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
14283b3a8eb9SGleb Smirnoff 			if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) &&
14293b3a8eb9SGleb Smirnoff 			    (clrflag & PFR_TFLAG_PERSIST) &&
14303b3a8eb9SGleb Smirnoff 			    !(p->pfrkt_flags & PFR_TFLAG_REFERENCED))
14313b3a8eb9SGleb Smirnoff 				xdel++;
14323b3a8eb9SGleb Smirnoff 			else
14333b3a8eb9SGleb Smirnoff 				xchange++;
14343b3a8eb9SGleb Smirnoff 		}
14353b3a8eb9SGleb Smirnoff _skip:
14363b3a8eb9SGleb Smirnoff 	;
14373b3a8eb9SGleb Smirnoff 	}
14383b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
14393b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
14403b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
14413b3a8eb9SGleb Smirnoff 		*nchange = xchange;
14423b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
14433b3a8eb9SGleb Smirnoff 		*ndel = xdel;
14443b3a8eb9SGleb Smirnoff 	return (0);
14453b3a8eb9SGleb Smirnoff }
14463b3a8eb9SGleb Smirnoff 
14473b3a8eb9SGleb Smirnoff int
14483b3a8eb9SGleb Smirnoff pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags)
14493b3a8eb9SGleb Smirnoff {
14503b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
14513b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
1452e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
14533b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
14543b3a8eb9SGleb Smirnoff 
14553b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1456e86bddeaSKristof Provost 	rs = pf_find_or_create_kruleset(trs->pfrt_anchor);
14573b3a8eb9SGleb Smirnoff 	if (rs == NULL)
14583b3a8eb9SGleb Smirnoff 		return (ENOMEM);
14593b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
14601e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
14613b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
14623b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
14633b3a8eb9SGleb Smirnoff 			continue;
14643b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
14653b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
14663b3a8eb9SGleb Smirnoff 		xdel++;
14673b3a8eb9SGleb Smirnoff 	}
14683b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
14693b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
14703b3a8eb9SGleb Smirnoff 		if (ticket != NULL)
14713b3a8eb9SGleb Smirnoff 			*ticket = ++rs->tticket;
14723b3a8eb9SGleb Smirnoff 		rs->topen = 1;
14733b3a8eb9SGleb Smirnoff 	} else
1474e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
14753b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
14763b3a8eb9SGleb Smirnoff 		*ndel = xdel;
14773b3a8eb9SGleb Smirnoff 	return (0);
14783b3a8eb9SGleb Smirnoff }
14793b3a8eb9SGleb Smirnoff 
14803b3a8eb9SGleb Smirnoff int
14813b3a8eb9SGleb Smirnoff pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
14823b3a8eb9SGleb Smirnoff     int *nadd, int *naddr, u_int32_t ticket, int flags)
14833b3a8eb9SGleb Smirnoff {
14843b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 tableq;
14853b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
14863b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *rt, *shadow, key;
14873b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
14883b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
1489e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
14903b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0, xaddr = 0;
14913b3a8eb9SGleb Smirnoff 
14923b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
14933b3a8eb9SGleb Smirnoff 
14943b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
14953b3a8eb9SGleb Smirnoff 	if (size && !(flags & PFR_FLAG_ADDRSTOO))
14963b3a8eb9SGleb Smirnoff 		return (EINVAL);
14973b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK,
14983b3a8eb9SGleb Smirnoff 	    flags & PFR_FLAG_USERIOCTL))
14993b3a8eb9SGleb Smirnoff 		return (EINVAL);
1500e86bddeaSKristof Provost 	rs = pf_find_kruleset(tbl->pfrt_anchor);
15013b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
15023b3a8eb9SGleb Smirnoff 		return (EBUSY);
15033b3a8eb9SGleb Smirnoff 	tbl->pfrt_flags |= PFR_TFLAG_INACTIVE;
15043b3a8eb9SGleb Smirnoff 	SLIST_INIT(&tableq);
15051e9e3741SMarko Zec 	kt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, (struct pfr_ktable *)tbl);
15063b3a8eb9SGleb Smirnoff 	if (kt == NULL) {
15073b3a8eb9SGleb Smirnoff 		kt = pfr_create_ktable(tbl, 0, 1);
15083b3a8eb9SGleb Smirnoff 		if (kt == NULL)
15093b3a8eb9SGleb Smirnoff 			return (ENOMEM);
15103b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq);
15113b3a8eb9SGleb Smirnoff 		xadd++;
15123b3a8eb9SGleb Smirnoff 		if (!tbl->pfrt_anchor[0])
15133b3a8eb9SGleb Smirnoff 			goto _skip;
15143b3a8eb9SGleb Smirnoff 
15153b3a8eb9SGleb Smirnoff 		/* find or create root table */
15163b3a8eb9SGleb Smirnoff 		bzero(&key, sizeof(key));
15173b3a8eb9SGleb Smirnoff 		strlcpy(key.pfrkt_name, tbl->pfrt_name, sizeof(key.pfrkt_name));
15181e9e3741SMarko Zec 		rt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
15193b3a8eb9SGleb Smirnoff 		if (rt != NULL) {
15203b3a8eb9SGleb Smirnoff 			kt->pfrkt_root = rt;
15213b3a8eb9SGleb Smirnoff 			goto _skip;
15223b3a8eb9SGleb Smirnoff 		}
15233b3a8eb9SGleb Smirnoff 		rt = pfr_create_ktable(&key.pfrkt_t, 0, 1);
15243b3a8eb9SGleb Smirnoff 		if (rt == NULL) {
15253b3a8eb9SGleb Smirnoff 			pfr_destroy_ktables(&tableq, 0);
15263b3a8eb9SGleb Smirnoff 			return (ENOMEM);
15273b3a8eb9SGleb Smirnoff 		}
15283b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq);
15293b3a8eb9SGleb Smirnoff 		kt->pfrkt_root = rt;
15303b3a8eb9SGleb Smirnoff 	} else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE))
15313b3a8eb9SGleb Smirnoff 		xadd++;
15323b3a8eb9SGleb Smirnoff _skip:
15333b3a8eb9SGleb Smirnoff 	shadow = pfr_create_ktable(tbl, 0, 0);
15343b3a8eb9SGleb Smirnoff 	if (shadow == NULL) {
15353b3a8eb9SGleb Smirnoff 		pfr_destroy_ktables(&tableq, 0);
15363b3a8eb9SGleb Smirnoff 		return (ENOMEM);
15373b3a8eb9SGleb Smirnoff 	}
15383b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addrq);
15393b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
15403b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
15413b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
15423b3a8eb9SGleb Smirnoff 		if (pfr_lookup_addr(shadow, ad, 1) != NULL)
15433b3a8eb9SGleb Smirnoff 			continue;
154421121f9bSMark Johnston 		p = pfr_create_kentry(ad,
154521121f9bSMark Johnston 		    (shadow->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
15463b3a8eb9SGleb Smirnoff 		if (p == NULL)
15473b3a8eb9SGleb Smirnoff 			senderr(ENOMEM);
15483b3a8eb9SGleb Smirnoff 		if (pfr_route_kentry(shadow, p)) {
15493b3a8eb9SGleb Smirnoff 			pfr_destroy_kentry(p);
15503b3a8eb9SGleb Smirnoff 			continue;
15513b3a8eb9SGleb Smirnoff 		}
15523b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&addrq, p, pfrke_workq);
15533b3a8eb9SGleb Smirnoff 		xaddr++;
15543b3a8eb9SGleb Smirnoff 	}
15553b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
15563b3a8eb9SGleb Smirnoff 		if (kt->pfrkt_shadow != NULL)
15573b3a8eb9SGleb Smirnoff 			pfr_destroy_ktable(kt->pfrkt_shadow, 1);
15583b3a8eb9SGleb Smirnoff 		kt->pfrkt_flags |= PFR_TFLAG_INACTIVE;
15593b3a8eb9SGleb Smirnoff 		pfr_insert_ktables(&tableq);
15603b3a8eb9SGleb Smirnoff 		shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ?
15613b3a8eb9SGleb Smirnoff 		    xaddr : NO_ADDRESSES;
15623b3a8eb9SGleb Smirnoff 		kt->pfrkt_shadow = shadow;
15633b3a8eb9SGleb Smirnoff 	} else {
15643b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(shadow, &addrq);
15653b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(shadow, 0);
15663b3a8eb9SGleb Smirnoff 		pfr_destroy_ktables(&tableq, 0);
15673b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addrq);
15683b3a8eb9SGleb Smirnoff 	}
15693b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
15703b3a8eb9SGleb Smirnoff 		*nadd = xadd;
15713b3a8eb9SGleb Smirnoff 	if (naddr != NULL)
15723b3a8eb9SGleb Smirnoff 		*naddr = xaddr;
15733b3a8eb9SGleb Smirnoff 	return (0);
15743b3a8eb9SGleb Smirnoff _bad:
15753b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(shadow, 0);
15763b3a8eb9SGleb Smirnoff 	pfr_destroy_ktables(&tableq, 0);
15773b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&addrq);
15783b3a8eb9SGleb Smirnoff 	return (rv);
15793b3a8eb9SGleb Smirnoff }
15803b3a8eb9SGleb Smirnoff 
15813b3a8eb9SGleb Smirnoff int
15823b3a8eb9SGleb Smirnoff pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags)
15833b3a8eb9SGleb Smirnoff {
15843b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
15853b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
1586e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
15873b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
15883b3a8eb9SGleb Smirnoff 
15893b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
15903b3a8eb9SGleb Smirnoff 
15913b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1592e86bddeaSKristof Provost 	rs = pf_find_kruleset(trs->pfrt_anchor);
15933b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
15943b3a8eb9SGleb Smirnoff 		return (0);
15953b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
15961e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
15973b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
15983b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
15993b3a8eb9SGleb Smirnoff 			continue;
16003b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
16013b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
16023b3a8eb9SGleb Smirnoff 		xdel++;
16033b3a8eb9SGleb Smirnoff 	}
16043b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
16053b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
16063b3a8eb9SGleb Smirnoff 		rs->topen = 0;
1607e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
16083b3a8eb9SGleb Smirnoff 	}
16093b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
16103b3a8eb9SGleb Smirnoff 		*ndel = xdel;
16113b3a8eb9SGleb Smirnoff 	return (0);
16123b3a8eb9SGleb Smirnoff }
16133b3a8eb9SGleb Smirnoff 
16143b3a8eb9SGleb Smirnoff int
16153b3a8eb9SGleb Smirnoff pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd,
16163b3a8eb9SGleb Smirnoff     int *nchange, int flags)
16173b3a8eb9SGleb Smirnoff {
16183b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
16193b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
1620e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
16213b3a8eb9SGleb Smirnoff 	int			 xadd = 0, xchange = 0;
16223b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
16233b3a8eb9SGleb Smirnoff 
16243b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
16253b3a8eb9SGleb Smirnoff 
16263b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1627e86bddeaSKristof Provost 	rs = pf_find_kruleset(trs->pfrt_anchor);
16283b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
16293b3a8eb9SGleb Smirnoff 		return (EBUSY);
16303b3a8eb9SGleb Smirnoff 
16313b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
16321e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
16333b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
16343b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
16353b3a8eb9SGleb Smirnoff 			continue;
16363b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
16373b3a8eb9SGleb Smirnoff 		if (p->pfrkt_flags & PFR_TFLAG_ACTIVE)
16383b3a8eb9SGleb Smirnoff 			xchange++;
16393b3a8eb9SGleb Smirnoff 		else
16403b3a8eb9SGleb Smirnoff 			xadd++;
16413b3a8eb9SGleb Smirnoff 	}
16423b3a8eb9SGleb Smirnoff 
16433b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
16443b3a8eb9SGleb Smirnoff 		for (p = SLIST_FIRST(&workq); p != NULL; p = q) {
16453b3a8eb9SGleb Smirnoff 			q = SLIST_NEXT(p, pfrkt_workq);
16463b3a8eb9SGleb Smirnoff 			pfr_commit_ktable(p, tzero);
16473b3a8eb9SGleb Smirnoff 		}
16483b3a8eb9SGleb Smirnoff 		rs->topen = 0;
1649e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
16503b3a8eb9SGleb Smirnoff 	}
16513b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
16523b3a8eb9SGleb Smirnoff 		*nadd = xadd;
16533b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
16543b3a8eb9SGleb Smirnoff 		*nchange = xchange;
16553b3a8eb9SGleb Smirnoff 
16563b3a8eb9SGleb Smirnoff 	return (0);
16573b3a8eb9SGleb Smirnoff }
16583b3a8eb9SGleb Smirnoff 
16593b3a8eb9SGleb Smirnoff static void
16603b3a8eb9SGleb Smirnoff pfr_commit_ktable(struct pfr_ktable *kt, long tzero)
16613b3a8eb9SGleb Smirnoff {
1662e6aed06fSMark Johnston 	counter_u64_t		*pkc, *qkc;
16633b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*shadow = kt->pfrkt_shadow;
16643b3a8eb9SGleb Smirnoff 	int			 nflags;
16653b3a8eb9SGleb Smirnoff 
16663b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
16673b3a8eb9SGleb Smirnoff 
16683b3a8eb9SGleb Smirnoff 	if (shadow->pfrkt_cnt == NO_ADDRESSES) {
16693b3a8eb9SGleb Smirnoff 		if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
16703b3a8eb9SGleb Smirnoff 			pfr_clstats_ktable(kt, tzero, 1);
16713b3a8eb9SGleb Smirnoff 	} else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) {
16723b3a8eb9SGleb Smirnoff 		/* kt might contain addresses */
16733b3a8eb9SGleb Smirnoff 		struct pfr_kentryworkq	 addrq, addq, changeq, delq, garbageq;
16743b3a8eb9SGleb Smirnoff 		struct pfr_kentry	*p, *q, *next;
16753b3a8eb9SGleb Smirnoff 		struct pfr_addr		 ad;
16763b3a8eb9SGleb Smirnoff 
16773b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(shadow, &addrq, NULL, 0);
16783b3a8eb9SGleb Smirnoff 		pfr_mark_addrs(kt);
16793b3a8eb9SGleb Smirnoff 		SLIST_INIT(&addq);
16803b3a8eb9SGleb Smirnoff 		SLIST_INIT(&changeq);
16813b3a8eb9SGleb Smirnoff 		SLIST_INIT(&delq);
16823b3a8eb9SGleb Smirnoff 		SLIST_INIT(&garbageq);
16833b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(shadow, &addrq);
1684e6aed06fSMark Johnston 		SLIST_FOREACH_SAFE(p, &addrq, pfrke_workq, next) {
16853b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(&ad, p);
16863b3a8eb9SGleb Smirnoff 			q = pfr_lookup_addr(kt, &ad, 1);
16873b3a8eb9SGleb Smirnoff 			if (q != NULL) {
16883b3a8eb9SGleb Smirnoff 				if (q->pfrke_not != p->pfrke_not)
16893b3a8eb9SGleb Smirnoff 					SLIST_INSERT_HEAD(&changeq, q,
16903b3a8eb9SGleb Smirnoff 					    pfrke_workq);
1691e6aed06fSMark Johnston 				pkc = &p->pfrke_counters.pfrkc_counters;
1692e6aed06fSMark Johnston 				qkc = &q->pfrke_counters.pfrkc_counters;
1693e6aed06fSMark Johnston 				if ((*pkc == NULL) != (*qkc == NULL))
1694e6aed06fSMark Johnston 					SWAP(counter_u64_t, *pkc, *qkc);
16953b3a8eb9SGleb Smirnoff 				q->pfrke_mark = 1;
16963b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq);
16973b3a8eb9SGleb Smirnoff 			} else {
169859048686SKristof Provost 				p->pfrke_counters.pfrkc_tzero = tzero;
16993b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
17003b3a8eb9SGleb Smirnoff 			}
17013b3a8eb9SGleb Smirnoff 		}
17023b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY);
17033b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &addq, tzero);
17043b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &delq);
170521121f9bSMark Johnston 		pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG);
17063b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&garbageq);
17073b3a8eb9SGleb Smirnoff 	} else {
17083b3a8eb9SGleb Smirnoff 		/* kt cannot contain addresses */
17093b3a8eb9SGleb Smirnoff 		SWAP(struct radix_node_head *, kt->pfrkt_ip4,
17103b3a8eb9SGleb Smirnoff 		    shadow->pfrkt_ip4);
17113b3a8eb9SGleb Smirnoff 		SWAP(struct radix_node_head *, kt->pfrkt_ip6,
17123b3a8eb9SGleb Smirnoff 		    shadow->pfrkt_ip6);
17133b3a8eb9SGleb Smirnoff 		SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt);
17143b3a8eb9SGleb Smirnoff 		pfr_clstats_ktable(kt, tzero, 1);
17153b3a8eb9SGleb Smirnoff 	}
17163b3a8eb9SGleb Smirnoff 	nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) |
17173b3a8eb9SGleb Smirnoff 	    (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE)
17183b3a8eb9SGleb Smirnoff 		& ~PFR_TFLAG_INACTIVE;
17193b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(shadow, 0);
17203b3a8eb9SGleb Smirnoff 	kt->pfrkt_shadow = NULL;
17213b3a8eb9SGleb Smirnoff 	pfr_setflags_ktable(kt, nflags);
17223b3a8eb9SGleb Smirnoff }
17233b3a8eb9SGleb Smirnoff 
17243b3a8eb9SGleb Smirnoff static int
17253b3a8eb9SGleb Smirnoff pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved)
17263b3a8eb9SGleb Smirnoff {
17273b3a8eb9SGleb Smirnoff 	int i;
17283b3a8eb9SGleb Smirnoff 
17293b3a8eb9SGleb Smirnoff 	if (!tbl->pfrt_name[0])
17303b3a8eb9SGleb Smirnoff 		return (-1);
17313b3a8eb9SGleb Smirnoff 	if (no_reserved && !strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR))
17323b3a8eb9SGleb Smirnoff 		 return (-1);
17333b3a8eb9SGleb Smirnoff 	if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1])
17343b3a8eb9SGleb Smirnoff 		return (-1);
17353b3a8eb9SGleb Smirnoff 	for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++)
17363b3a8eb9SGleb Smirnoff 		if (tbl->pfrt_name[i])
17373b3a8eb9SGleb Smirnoff 			return (-1);
17383b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(tbl->pfrt_anchor))
17393b3a8eb9SGleb Smirnoff 		return (-1);
17403b3a8eb9SGleb Smirnoff 	if (tbl->pfrt_flags & ~allowedflags)
17413b3a8eb9SGleb Smirnoff 		return (-1);
17423b3a8eb9SGleb Smirnoff 	return (0);
17433b3a8eb9SGleb Smirnoff }
17443b3a8eb9SGleb Smirnoff 
17453b3a8eb9SGleb Smirnoff /*
17463b3a8eb9SGleb Smirnoff  * Rewrite anchors referenced by tables to remove slashes
17473b3a8eb9SGleb Smirnoff  * and check for validity.
17483b3a8eb9SGleb Smirnoff  */
17493b3a8eb9SGleb Smirnoff static int
17503b3a8eb9SGleb Smirnoff pfr_fix_anchor(char *anchor)
17513b3a8eb9SGleb Smirnoff {
17523b3a8eb9SGleb Smirnoff 	size_t siz = MAXPATHLEN;
17533b3a8eb9SGleb Smirnoff 	int i;
17543b3a8eb9SGleb Smirnoff 
17553b3a8eb9SGleb Smirnoff 	if (anchor[0] == '/') {
17563b3a8eb9SGleb Smirnoff 		char *path;
17573b3a8eb9SGleb Smirnoff 		int off;
17583b3a8eb9SGleb Smirnoff 
17593b3a8eb9SGleb Smirnoff 		path = anchor;
17603b3a8eb9SGleb Smirnoff 		off = 1;
17613b3a8eb9SGleb Smirnoff 		while (*++path == '/')
17623b3a8eb9SGleb Smirnoff 			off++;
17633b3a8eb9SGleb Smirnoff 		bcopy(path, anchor, siz - off);
17643b3a8eb9SGleb Smirnoff 		memset(anchor + siz - off, 0, off);
17653b3a8eb9SGleb Smirnoff 	}
17663b3a8eb9SGleb Smirnoff 	if (anchor[siz - 1])
17673b3a8eb9SGleb Smirnoff 		return (-1);
17683b3a8eb9SGleb Smirnoff 	for (i = strlen(anchor); i < siz; i++)
17693b3a8eb9SGleb Smirnoff 		if (anchor[i])
17703b3a8eb9SGleb Smirnoff 			return (-1);
17713b3a8eb9SGleb Smirnoff 	return (0);
17723b3a8eb9SGleb Smirnoff }
17733b3a8eb9SGleb Smirnoff 
1774adfe2f6aSKristof Provost int
17753b3a8eb9SGleb Smirnoff pfr_table_count(struct pfr_table *filter, int flags)
17763b3a8eb9SGleb Smirnoff {
1777e86bddeaSKristof Provost 	struct pf_kruleset *rs;
17783b3a8eb9SGleb Smirnoff 
17793b3a8eb9SGleb Smirnoff 	PF_RULES_ASSERT();
17803b3a8eb9SGleb Smirnoff 
17813b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_ALLRSETS)
17821e9e3741SMarko Zec 		return (V_pfr_ktable_cnt);
17833b3a8eb9SGleb Smirnoff 	if (filter->pfrt_anchor[0]) {
1784e86bddeaSKristof Provost 		rs = pf_find_kruleset(filter->pfrt_anchor);
17853b3a8eb9SGleb Smirnoff 		return ((rs != NULL) ? rs->tables : -1);
17863b3a8eb9SGleb Smirnoff 	}
17873b3a8eb9SGleb Smirnoff 	return (pf_main_ruleset.tables);
17883b3a8eb9SGleb Smirnoff }
17893b3a8eb9SGleb Smirnoff 
17903b3a8eb9SGleb Smirnoff static int
17913b3a8eb9SGleb Smirnoff pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags)
17923b3a8eb9SGleb Smirnoff {
17933b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_ALLRSETS)
17943b3a8eb9SGleb Smirnoff 		return (0);
17953b3a8eb9SGleb Smirnoff 	if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor))
17963b3a8eb9SGleb Smirnoff 		return (1);
17973b3a8eb9SGleb Smirnoff 	return (0);
17983b3a8eb9SGleb Smirnoff }
17993b3a8eb9SGleb Smirnoff 
18003b3a8eb9SGleb Smirnoff static void
18013b3a8eb9SGleb Smirnoff pfr_insert_ktables(struct pfr_ktableworkq *workq)
18023b3a8eb9SGleb Smirnoff {
18033b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
18043b3a8eb9SGleb Smirnoff 
18053b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrkt_workq)
18063b3a8eb9SGleb Smirnoff 		pfr_insert_ktable(p);
18073b3a8eb9SGleb Smirnoff }
18083b3a8eb9SGleb Smirnoff 
18093b3a8eb9SGleb Smirnoff static void
18103b3a8eb9SGleb Smirnoff pfr_insert_ktable(struct pfr_ktable *kt)
18113b3a8eb9SGleb Smirnoff {
18123b3a8eb9SGleb Smirnoff 
18133b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
18143b3a8eb9SGleb Smirnoff 
18151e9e3741SMarko Zec 	RB_INSERT(pfr_ktablehead, &V_pfr_ktables, kt);
18161e9e3741SMarko Zec 	V_pfr_ktable_cnt++;
18173b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_root != NULL)
18183b3a8eb9SGleb Smirnoff 		if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++)
18193b3a8eb9SGleb Smirnoff 			pfr_setflags_ktable(kt->pfrkt_root,
18203b3a8eb9SGleb Smirnoff 			    kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR);
18213b3a8eb9SGleb Smirnoff }
18223b3a8eb9SGleb Smirnoff 
18233b3a8eb9SGleb Smirnoff static void
18243b3a8eb9SGleb Smirnoff pfr_setflags_ktables(struct pfr_ktableworkq *workq)
18253b3a8eb9SGleb Smirnoff {
18263b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
18273b3a8eb9SGleb Smirnoff 
18283b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p; p = q) {
18293b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrkt_workq);
18303b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(p, p->pfrkt_nflags);
18313b3a8eb9SGleb Smirnoff 	}
18323b3a8eb9SGleb Smirnoff }
18333b3a8eb9SGleb Smirnoff 
18343b3a8eb9SGleb Smirnoff static void
18353b3a8eb9SGleb Smirnoff pfr_setflags_ktable(struct pfr_ktable *kt, int newf)
18363b3a8eb9SGleb Smirnoff {
18373b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	addrq;
1838b21826bfSKristof Provost 	struct pfr_walktree	w;
18393b3a8eb9SGleb Smirnoff 
18403b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
18413b3a8eb9SGleb Smirnoff 
18423b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_REFERENCED) &&
184387e4ca37SKristof Provost 	    !(newf & PFR_TFLAG_REFDANCHOR) &&
18443b3a8eb9SGleb Smirnoff 	    !(newf & PFR_TFLAG_PERSIST))
18453b3a8eb9SGleb Smirnoff 		newf &= ~PFR_TFLAG_ACTIVE;
18463b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_ACTIVE))
18473b3a8eb9SGleb Smirnoff 		newf &= ~PFR_TFLAG_USRMASK;
18483b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_SETMASK)) {
18491e9e3741SMarko Zec 		RB_REMOVE(pfr_ktablehead, &V_pfr_ktables, kt);
18503b3a8eb9SGleb Smirnoff 		if (kt->pfrkt_root != NULL)
18513b3a8eb9SGleb Smirnoff 			if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR])
18523b3a8eb9SGleb Smirnoff 				pfr_setflags_ktable(kt->pfrkt_root,
18533b3a8eb9SGleb Smirnoff 				    kt->pfrkt_root->pfrkt_flags &
18543b3a8eb9SGleb Smirnoff 					~PFR_TFLAG_REFDANCHOR);
18553b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt, 1);
18561e9e3741SMarko Zec 		V_pfr_ktable_cnt--;
18573b3a8eb9SGleb Smirnoff 		return;
18583b3a8eb9SGleb Smirnoff 	}
1859b21826bfSKristof Provost 	if (newf & PFR_TFLAG_COUNTERS && ! (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) {
1860b21826bfSKristof Provost 		bzero(&w, sizeof(w));
1861b21826bfSKristof Provost 		w.pfrw_op = PFRW_COUNTERS;
1862b21826bfSKristof Provost 		w.pfrw_flags |= PFR_TFLAG_COUNTERS;
1863b21826bfSKristof Provost 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
1864b21826bfSKristof Provost 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
1865b21826bfSKristof Provost 	}
1866b21826bfSKristof Provost 	if (! (newf & PFR_TFLAG_COUNTERS) && (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) {
1867b21826bfSKristof Provost 		bzero(&w, sizeof(w));
1868b21826bfSKristof Provost 		w.pfrw_op = PFRW_COUNTERS;
1869b21826bfSKristof Provost 		w.pfrw_flags |= 0;
1870b21826bfSKristof Provost 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
1871b21826bfSKristof Provost 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
1872b21826bfSKristof Provost 	}
18733b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) {
18743b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
18753b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &addrq);
18763b3a8eb9SGleb Smirnoff 	}
18773b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) {
18783b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt->pfrkt_shadow, 1);
18793b3a8eb9SGleb Smirnoff 		kt->pfrkt_shadow = NULL;
18803b3a8eb9SGleb Smirnoff 	}
18813b3a8eb9SGleb Smirnoff 	kt->pfrkt_flags = newf;
18823b3a8eb9SGleb Smirnoff }
18833b3a8eb9SGleb Smirnoff 
18843b3a8eb9SGleb Smirnoff static void
18853b3a8eb9SGleb Smirnoff pfr_clstats_ktables(struct pfr_ktableworkq *workq, long tzero, int recurse)
18863b3a8eb9SGleb Smirnoff {
18873b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
18883b3a8eb9SGleb Smirnoff 
18893b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrkt_workq)
18903b3a8eb9SGleb Smirnoff 		pfr_clstats_ktable(p, tzero, recurse);
18913b3a8eb9SGleb Smirnoff }
18923b3a8eb9SGleb Smirnoff 
18933b3a8eb9SGleb Smirnoff static void
18943b3a8eb9SGleb Smirnoff pfr_clstats_ktable(struct pfr_ktable *kt, long tzero, int recurse)
18953b3a8eb9SGleb Smirnoff {
18963b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
189759048686SKristof Provost 	int			 pfr_dir, pfr_op;
18983b3a8eb9SGleb Smirnoff 
1899dc1ab04eSMateusz Guzik 	MPASS(PF_TABLE_STATS_OWNED() || PF_RULES_WOWNED());
1900dc1ab04eSMateusz Guzik 
19013b3a8eb9SGleb Smirnoff 	if (recurse) {
19023b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
190321121f9bSMark Johnston 		pfr_clstats_kentries(kt, &addrq, tzero, 0);
19043b3a8eb9SGleb Smirnoff 	}
190559048686SKristof Provost 	for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
190659048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
1907f92c21a2SMateusz Guzik 			pfr_kstate_counter_zero(&kt->pfrkt_packets[pfr_dir][pfr_op]);
1908f92c21a2SMateusz Guzik 			pfr_kstate_counter_zero(&kt->pfrkt_bytes[pfr_dir][pfr_op]);
190959048686SKristof Provost 		}
191059048686SKristof Provost 	}
1911f92c21a2SMateusz Guzik 	pfr_kstate_counter_zero(&kt->pfrkt_match);
1912f92c21a2SMateusz Guzik 	pfr_kstate_counter_zero(&kt->pfrkt_nomatch);
19133b3a8eb9SGleb Smirnoff 	kt->pfrkt_tzero = tzero;
19143b3a8eb9SGleb Smirnoff }
19153b3a8eb9SGleb Smirnoff 
19163b3a8eb9SGleb Smirnoff static struct pfr_ktable *
19173b3a8eb9SGleb Smirnoff pfr_create_ktable(struct pfr_table *tbl, long tzero, int attachruleset)
19183b3a8eb9SGleb Smirnoff {
19193b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
1920e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
192159048686SKristof Provost 	int			 pfr_dir, pfr_op;
19223b3a8eb9SGleb Smirnoff 
19233b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
19243b3a8eb9SGleb Smirnoff 
19253b3a8eb9SGleb Smirnoff 	kt = malloc(sizeof(*kt), M_PFTABLE, M_NOWAIT|M_ZERO);
19263b3a8eb9SGleb Smirnoff 	if (kt == NULL)
19273b3a8eb9SGleb Smirnoff 		return (NULL);
19283b3a8eb9SGleb Smirnoff 	kt->pfrkt_t = *tbl;
19293b3a8eb9SGleb Smirnoff 
19303b3a8eb9SGleb Smirnoff 	if (attachruleset) {
1931e86bddeaSKristof Provost 		rs = pf_find_or_create_kruleset(tbl->pfrt_anchor);
19323b3a8eb9SGleb Smirnoff 		if (!rs) {
19333b3a8eb9SGleb Smirnoff 			pfr_destroy_ktable(kt, 0);
19343b3a8eb9SGleb Smirnoff 			return (NULL);
19353b3a8eb9SGleb Smirnoff 		}
19363b3a8eb9SGleb Smirnoff 		kt->pfrkt_rs = rs;
19373b3a8eb9SGleb Smirnoff 		rs->tables++;
19383b3a8eb9SGleb Smirnoff 	}
19393b3a8eb9SGleb Smirnoff 
194059048686SKristof Provost 	for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
194159048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
1942f92c21a2SMateusz Guzik 			if (pfr_kstate_counter_init(
1943f92c21a2SMateusz Guzik 			    &kt->pfrkt_packets[pfr_dir][pfr_op], M_NOWAIT) != 0) {
194459048686SKristof Provost 				pfr_destroy_ktable(kt, 0);
194559048686SKristof Provost 				return (NULL);
194659048686SKristof Provost 			}
1947f92c21a2SMateusz Guzik 			if (pfr_kstate_counter_init(
1948f92c21a2SMateusz Guzik 			    &kt->pfrkt_bytes[pfr_dir][pfr_op], M_NOWAIT) != 0) {
194959048686SKristof Provost 				pfr_destroy_ktable(kt, 0);
195059048686SKristof Provost 				return (NULL);
195159048686SKristof Provost 			}
195259048686SKristof Provost 		}
195359048686SKristof Provost 	}
1954f92c21a2SMateusz Guzik 	if (pfr_kstate_counter_init(&kt->pfrkt_match, M_NOWAIT) != 0) {
195559048686SKristof Provost 		pfr_destroy_ktable(kt, 0);
195659048686SKristof Provost 		return (NULL);
195759048686SKristof Provost 	}
195859048686SKristof Provost 
1959f92c21a2SMateusz Guzik 	if (pfr_kstate_counter_init(&kt->pfrkt_nomatch, M_NOWAIT) != 0) {
196059048686SKristof Provost 		pfr_destroy_ktable(kt, 0);
196159048686SKristof Provost 		return (NULL);
196259048686SKristof Provost 	}
196359048686SKristof Provost 
19643b3a8eb9SGleb Smirnoff 	if (!rn_inithead((void **)&kt->pfrkt_ip4,
19653b3a8eb9SGleb Smirnoff 	    offsetof(struct sockaddr_in, sin_addr) * 8) ||
19663b3a8eb9SGleb Smirnoff 	    !rn_inithead((void **)&kt->pfrkt_ip6,
19673b3a8eb9SGleb Smirnoff 	    offsetof(struct sockaddr_in6, sin6_addr) * 8)) {
19683b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt, 0);
19693b3a8eb9SGleb Smirnoff 		return (NULL);
19703b3a8eb9SGleb Smirnoff 	}
19713b3a8eb9SGleb Smirnoff 	kt->pfrkt_tzero = tzero;
19723b3a8eb9SGleb Smirnoff 
19733b3a8eb9SGleb Smirnoff 	return (kt);
19743b3a8eb9SGleb Smirnoff }
19753b3a8eb9SGleb Smirnoff 
19763b3a8eb9SGleb Smirnoff static void
19773b3a8eb9SGleb Smirnoff pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr)
19783b3a8eb9SGleb Smirnoff {
19793b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
19803b3a8eb9SGleb Smirnoff 
19813b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p; p = q) {
19823b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrkt_workq);
19833b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(p, flushaddr);
19843b3a8eb9SGleb Smirnoff 	}
19853b3a8eb9SGleb Smirnoff }
19863b3a8eb9SGleb Smirnoff 
19873b3a8eb9SGleb Smirnoff static void
19883b3a8eb9SGleb Smirnoff pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr)
19893b3a8eb9SGleb Smirnoff {
19903b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
199159048686SKristof Provost 	int			 pfr_dir, pfr_op;
19923b3a8eb9SGleb Smirnoff 
19933b3a8eb9SGleb Smirnoff 	if (flushaddr) {
19943b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
19953b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(kt, &addrq);
19963b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addrq);
19973b3a8eb9SGleb Smirnoff 	}
199831f0d081SAlexander V. Chernikov 	if (kt->pfrkt_ip4 != NULL)
1999495a22b5SGleb Smirnoff 		rn_detachhead((void **)&kt->pfrkt_ip4);
200031f0d081SAlexander V. Chernikov 	if (kt->pfrkt_ip6 != NULL)
2001495a22b5SGleb Smirnoff 		rn_detachhead((void **)&kt->pfrkt_ip6);
20023b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_shadow != NULL)
20033b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr);
20043b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_rs != NULL) {
20053b3a8eb9SGleb Smirnoff 		kt->pfrkt_rs->tables--;
2006e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(kt->pfrkt_rs);
20073b3a8eb9SGleb Smirnoff 	}
200859048686SKristof Provost 	for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
200959048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
2010f92c21a2SMateusz Guzik 			pfr_kstate_counter_deinit(&kt->pfrkt_packets[pfr_dir][pfr_op]);
2011f92c21a2SMateusz Guzik 			pfr_kstate_counter_deinit(&kt->pfrkt_bytes[pfr_dir][pfr_op]);
201259048686SKristof Provost 		}
201359048686SKristof Provost 	}
2014f92c21a2SMateusz Guzik 	pfr_kstate_counter_deinit(&kt->pfrkt_match);
2015f92c21a2SMateusz Guzik 	pfr_kstate_counter_deinit(&kt->pfrkt_nomatch);
201659048686SKristof Provost 
20173b3a8eb9SGleb Smirnoff 	free(kt, M_PFTABLE);
20183b3a8eb9SGleb Smirnoff }
20193b3a8eb9SGleb Smirnoff 
20203b3a8eb9SGleb Smirnoff static int
20213b3a8eb9SGleb Smirnoff pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q)
20223b3a8eb9SGleb Smirnoff {
20233b3a8eb9SGleb Smirnoff 	int d;
20243b3a8eb9SGleb Smirnoff 
20253b3a8eb9SGleb Smirnoff 	if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE)))
20263b3a8eb9SGleb Smirnoff 		return (d);
20273b3a8eb9SGleb Smirnoff 	return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor));
20283b3a8eb9SGleb Smirnoff }
20293b3a8eb9SGleb Smirnoff 
20303b3a8eb9SGleb Smirnoff static struct pfr_ktable *
20313b3a8eb9SGleb Smirnoff pfr_lookup_table(struct pfr_table *tbl)
20323b3a8eb9SGleb Smirnoff {
20333b3a8eb9SGleb Smirnoff 	/* struct pfr_ktable start like a struct pfr_table */
20341e9e3741SMarko Zec 	return (RB_FIND(pfr_ktablehead, &V_pfr_ktables,
20353b3a8eb9SGleb Smirnoff 	    (struct pfr_ktable *)tbl));
20363b3a8eb9SGleb Smirnoff }
20373b3a8eb9SGleb Smirnoff 
20383b3a8eb9SGleb Smirnoff int
20393b3a8eb9SGleb Smirnoff pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af)
20403b3a8eb9SGleb Smirnoff {
20413b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = NULL;
20423b3a8eb9SGleb Smirnoff 	int			 match;
20433b3a8eb9SGleb Smirnoff 
20443b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
20453b3a8eb9SGleb Smirnoff 
20463b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
20473b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
20483b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
20493b3a8eb9SGleb Smirnoff 		return (0);
20503b3a8eb9SGleb Smirnoff 
20513b3a8eb9SGleb Smirnoff 	switch (af) {
20523b3a8eb9SGleb Smirnoff #ifdef INET
20533b3a8eb9SGleb Smirnoff 	case AF_INET:
20543b3a8eb9SGleb Smirnoff 	    {
20553b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
20563b3a8eb9SGleb Smirnoff 
20573b3a8eb9SGleb Smirnoff 		bzero(&sin, sizeof(sin));
20583b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
20593b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
20603b3a8eb9SGleb Smirnoff 		sin.sin_addr.s_addr = a->addr32[0];
206161eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh);
20623b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20633b3a8eb9SGleb Smirnoff 			ke = NULL;
20643b3a8eb9SGleb Smirnoff 		break;
20653b3a8eb9SGleb Smirnoff 	    }
20663b3a8eb9SGleb Smirnoff #endif /* INET */
20673b3a8eb9SGleb Smirnoff #ifdef INET6
20683b3a8eb9SGleb Smirnoff 	case AF_INET6:
20693b3a8eb9SGleb Smirnoff 	    {
20703b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
20713b3a8eb9SGleb Smirnoff 
20723b3a8eb9SGleb Smirnoff 		bzero(&sin6, sizeof(sin6));
20733b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
20743b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
20753b3a8eb9SGleb Smirnoff 		bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
207661eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh);
20773b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20783b3a8eb9SGleb Smirnoff 			ke = NULL;
20793b3a8eb9SGleb Smirnoff 		break;
20803b3a8eb9SGleb Smirnoff 	    }
20813b3a8eb9SGleb Smirnoff #endif /* INET6 */
20823b3a8eb9SGleb Smirnoff 	}
20833b3a8eb9SGleb Smirnoff 	match = (ke && !ke->pfrke_not);
20843b3a8eb9SGleb Smirnoff 	if (match)
2085f92c21a2SMateusz Guzik 		pfr_kstate_counter_add(&kt->pfrkt_match, 1);
20863b3a8eb9SGleb Smirnoff 	else
2087f92c21a2SMateusz Guzik 		pfr_kstate_counter_add(&kt->pfrkt_nomatch, 1);
20883b3a8eb9SGleb Smirnoff 	return (match);
20893b3a8eb9SGleb Smirnoff }
20903b3a8eb9SGleb Smirnoff 
20913b3a8eb9SGleb Smirnoff void
20923b3a8eb9SGleb Smirnoff pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af,
20933b3a8eb9SGleb Smirnoff     u_int64_t len, int dir_out, int op_pass, int notrule)
20943b3a8eb9SGleb Smirnoff {
20953b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = NULL;
20963b3a8eb9SGleb Smirnoff 
20973b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
20983b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
20993b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
21003b3a8eb9SGleb Smirnoff 		return;
21013b3a8eb9SGleb Smirnoff 
21023b3a8eb9SGleb Smirnoff 	switch (af) {
21033b3a8eb9SGleb Smirnoff #ifdef INET
21043b3a8eb9SGleb Smirnoff 	case AF_INET:
21053b3a8eb9SGleb Smirnoff 	    {
21063b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
21073b3a8eb9SGleb Smirnoff 
21087348c524SGleb Smirnoff 		bzero(&sin, sizeof(sin));
21093b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
21103b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
21113b3a8eb9SGleb Smirnoff 		sin.sin_addr.s_addr = a->addr32[0];
211261eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh);
21133b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
21143b3a8eb9SGleb Smirnoff 			ke = NULL;
21153b3a8eb9SGleb Smirnoff 		break;
21163b3a8eb9SGleb Smirnoff 	    }
21173b3a8eb9SGleb Smirnoff #endif /* INET */
21183b3a8eb9SGleb Smirnoff #ifdef INET6
21193b3a8eb9SGleb Smirnoff 	case AF_INET6:
21203b3a8eb9SGleb Smirnoff 	    {
21213b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
21223b3a8eb9SGleb Smirnoff 
21237348c524SGleb Smirnoff 		bzero(&sin6, sizeof(sin6));
21243b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
21253b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
21263b3a8eb9SGleb Smirnoff 		bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
212761eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh);
21283b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
21293b3a8eb9SGleb Smirnoff 			ke = NULL;
21303b3a8eb9SGleb Smirnoff 		break;
21313b3a8eb9SGleb Smirnoff 	    }
21323b3a8eb9SGleb Smirnoff #endif /* INET6 */
21333b3a8eb9SGleb Smirnoff 	default:
21347348c524SGleb Smirnoff 		panic("%s: unknown address family %u", __func__, af);
21353b3a8eb9SGleb Smirnoff 	}
21363b3a8eb9SGleb Smirnoff 	if ((ke == NULL || ke->pfrke_not) != notrule) {
21373b3a8eb9SGleb Smirnoff 		if (op_pass != PFR_OP_PASS)
2138032dff66SKristof Provost 			DPFPRINTF(PF_DEBUG_URGENT,
2139032dff66SKristof Provost 			    ("pfr_update_stats: assertion failed.\n"));
21403b3a8eb9SGleb Smirnoff 		op_pass = PFR_OP_XPASS;
21413b3a8eb9SGleb Smirnoff 	}
2142f92c21a2SMateusz Guzik 	pfr_kstate_counter_add(&kt->pfrkt_packets[dir_out][op_pass], 1);
2143f92c21a2SMateusz Guzik 	pfr_kstate_counter_add(&kt->pfrkt_bytes[dir_out][op_pass], len);
21443b3a8eb9SGleb Smirnoff 	if (ke != NULL && op_pass != PFR_OP_XPASS &&
21453b3a8eb9SGleb Smirnoff 	    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) {
2146c1be8399SMark Johnston 		counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters,
2147c1be8399SMark Johnston 		    dir_out, op_pass, PFR_TYPE_PACKETS), 1);
2148c1be8399SMark Johnston 		counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters,
2149c1be8399SMark Johnston 		    dir_out, op_pass, PFR_TYPE_BYTES), len);
21503b3a8eb9SGleb Smirnoff 	}
21513b3a8eb9SGleb Smirnoff }
21523b3a8eb9SGleb Smirnoff 
21533b3a8eb9SGleb Smirnoff struct pfr_ktable *
2154812839e5SKristof Provost pfr_eth_attach_table(struct pf_keth_ruleset *rs, char *name)
2155812839e5SKristof Provost {
2156812839e5SKristof Provost 	struct pfr_ktable	*kt, *rt;
2157812839e5SKristof Provost 	struct pfr_table	 tbl;
2158812839e5SKristof Provost 	struct pf_keth_anchor	*ac = rs->anchor;
2159812839e5SKristof Provost 
2160812839e5SKristof Provost 	PF_RULES_WASSERT();
2161812839e5SKristof Provost 
2162812839e5SKristof Provost 	bzero(&tbl, sizeof(tbl));
2163812839e5SKristof Provost 	strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name));
2164812839e5SKristof Provost 	if (ac != NULL)
2165812839e5SKristof Provost 		strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor));
2166812839e5SKristof Provost 	kt = pfr_lookup_table(&tbl);
2167812839e5SKristof Provost 	if (kt == NULL) {
2168812839e5SKristof Provost 		kt = pfr_create_ktable(&tbl, time_second, 1);
2169812839e5SKristof Provost 		if (kt == NULL)
2170812839e5SKristof Provost 			return (NULL);
2171812839e5SKristof Provost 		if (ac != NULL) {
2172812839e5SKristof Provost 			bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor));
2173812839e5SKristof Provost 			rt = pfr_lookup_table(&tbl);
2174812839e5SKristof Provost 			if (rt == NULL) {
2175812839e5SKristof Provost 				rt = pfr_create_ktable(&tbl, 0, 1);
2176812839e5SKristof Provost 				if (rt == NULL) {
2177812839e5SKristof Provost 					pfr_destroy_ktable(kt, 0);
2178812839e5SKristof Provost 					return (NULL);
2179812839e5SKristof Provost 				}
2180812839e5SKristof Provost 				pfr_insert_ktable(rt);
2181812839e5SKristof Provost 			}
2182812839e5SKristof Provost 			kt->pfrkt_root = rt;
2183812839e5SKristof Provost 		}
2184812839e5SKristof Provost 		pfr_insert_ktable(kt);
2185812839e5SKristof Provost 	}
2186812839e5SKristof Provost 	if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++)
2187812839e5SKristof Provost 		pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED);
2188812839e5SKristof Provost 	return (kt);
2189812839e5SKristof Provost }
2190812839e5SKristof Provost 
2191812839e5SKristof Provost struct pfr_ktable *
2192e86bddeaSKristof Provost pfr_attach_table(struct pf_kruleset *rs, char *name)
21933b3a8eb9SGleb Smirnoff {
21943b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *rt;
21953b3a8eb9SGleb Smirnoff 	struct pfr_table	 tbl;
2196e86bddeaSKristof Provost 	struct pf_kanchor	*ac = rs->anchor;
21973b3a8eb9SGleb Smirnoff 
21983b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
21993b3a8eb9SGleb Smirnoff 
22003b3a8eb9SGleb Smirnoff 	bzero(&tbl, sizeof(tbl));
22013b3a8eb9SGleb Smirnoff 	strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name));
22023b3a8eb9SGleb Smirnoff 	if (ac != NULL)
22033b3a8eb9SGleb Smirnoff 		strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor));
22043b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(&tbl);
22053b3a8eb9SGleb Smirnoff 	if (kt == NULL) {
22063b3a8eb9SGleb Smirnoff 		kt = pfr_create_ktable(&tbl, time_second, 1);
22073b3a8eb9SGleb Smirnoff 		if (kt == NULL)
22083b3a8eb9SGleb Smirnoff 			return (NULL);
22093b3a8eb9SGleb Smirnoff 		if (ac != NULL) {
22103b3a8eb9SGleb Smirnoff 			bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor));
22113b3a8eb9SGleb Smirnoff 			rt = pfr_lookup_table(&tbl);
22123b3a8eb9SGleb Smirnoff 			if (rt == NULL) {
22133b3a8eb9SGleb Smirnoff 				rt = pfr_create_ktable(&tbl, 0, 1);
22143b3a8eb9SGleb Smirnoff 				if (rt == NULL) {
22153b3a8eb9SGleb Smirnoff 					pfr_destroy_ktable(kt, 0);
22163b3a8eb9SGleb Smirnoff 					return (NULL);
22173b3a8eb9SGleb Smirnoff 				}
22183b3a8eb9SGleb Smirnoff 				pfr_insert_ktable(rt);
22193b3a8eb9SGleb Smirnoff 			}
22203b3a8eb9SGleb Smirnoff 			kt->pfrkt_root = rt;
22213b3a8eb9SGleb Smirnoff 		}
22223b3a8eb9SGleb Smirnoff 		pfr_insert_ktable(kt);
22233b3a8eb9SGleb Smirnoff 	}
22243b3a8eb9SGleb Smirnoff 	if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++)
22253b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED);
22263b3a8eb9SGleb Smirnoff 	return (kt);
22273b3a8eb9SGleb Smirnoff }
22283b3a8eb9SGleb Smirnoff 
22293b3a8eb9SGleb Smirnoff void
22303b3a8eb9SGleb Smirnoff pfr_detach_table(struct pfr_ktable *kt)
22313b3a8eb9SGleb Smirnoff {
22323b3a8eb9SGleb Smirnoff 
22333b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
22343b3a8eb9SGleb Smirnoff 	KASSERT(kt->pfrkt_refcnt[PFR_REFCNT_RULE] > 0, ("%s: refcount %d\n",
22353b3a8eb9SGleb Smirnoff 	    __func__, kt->pfrkt_refcnt[PFR_REFCNT_RULE]));
22363b3a8eb9SGleb Smirnoff 
22373b3a8eb9SGleb Smirnoff 	if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE])
22383b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED);
22393b3a8eb9SGleb Smirnoff }
22403b3a8eb9SGleb Smirnoff 
22413b3a8eb9SGleb Smirnoff int
22423b3a8eb9SGleb Smirnoff pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter,
22437d381d0aSKristof Provost     sa_family_t af, pf_addr_filter_func_t filter)
22443b3a8eb9SGleb Smirnoff {
224567b65598SKristof Provost 	struct pf_addr		*addr, cur, mask, umask_addr;
22463b3a8eb9SGleb Smirnoff 	union sockaddr_union	 uaddr, umask;
22473b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke, *ke2 = NULL;
22487d0f8cd9SKristof Provost 	int			 startidx, idx = -1, loop = 0, use_counter = 0;
22493b3a8eb9SGleb Smirnoff 
22508cceacc0SKristof Provost 	MPASS(pidx != NULL);
2251efc64d02SKristof Provost 	MPASS(counter != NULL);
22528cceacc0SKristof Provost 
22533b3a8eb9SGleb Smirnoff 	switch (af) {
22543b3a8eb9SGleb Smirnoff 	case AF_INET:
22553b3a8eb9SGleb Smirnoff 		uaddr.sin.sin_len = sizeof(struct sockaddr_in);
22563b3a8eb9SGleb Smirnoff 		uaddr.sin.sin_family = AF_INET;
225767b65598SKristof Provost 		addr = (struct pf_addr *)&uaddr.sin.sin_addr;
22583b3a8eb9SGleb Smirnoff 		break;
22593b3a8eb9SGleb Smirnoff 	case AF_INET6:
22603b3a8eb9SGleb Smirnoff 		uaddr.sin6.sin6_len = sizeof(struct sockaddr_in6);
22613b3a8eb9SGleb Smirnoff 		uaddr.sin6.sin6_family = AF_INET6;
226267b65598SKristof Provost 		addr = (struct pf_addr *)&uaddr.sin6.sin6_addr;
22633b3a8eb9SGleb Smirnoff 		break;
22643b3a8eb9SGleb Smirnoff 	}
22653b3a8eb9SGleb Smirnoff 
22663b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
22673b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
22683b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
22693b3a8eb9SGleb Smirnoff 		return (-1);
22703b3a8eb9SGleb Smirnoff 
22713b3a8eb9SGleb Smirnoff 	idx = *pidx;
2272*d90854a6SKristof Provost 	if (idx < 0 || idx >= kt->pfrkt_cnt)
22733b3a8eb9SGleb Smirnoff 		idx = 0;
2274*d90854a6SKristof Provost 	else if (counter != NULL)
2275*d90854a6SKristof Provost 		use_counter = 1;
22767d0f8cd9SKristof Provost 	startidx = idx;
22773b3a8eb9SGleb Smirnoff 
22783b3a8eb9SGleb Smirnoff _next_block:
22797d0f8cd9SKristof Provost 	if (loop && startidx == idx) {
2280f92c21a2SMateusz Guzik 		pfr_kstate_counter_add(&kt->pfrkt_nomatch, 1);
22813b3a8eb9SGleb Smirnoff 		return (1);
22823b3a8eb9SGleb Smirnoff 	}
22837d0f8cd9SKristof Provost 
22847d0f8cd9SKristof Provost 	ke = pfr_kentry_byidx(kt, idx, af);
22857d0f8cd9SKristof Provost 	if (ke == NULL) {
22867d0f8cd9SKristof Provost 		/* we don't have this idx, try looping */
22877d0f8cd9SKristof Provost 		if (loop || (ke = pfr_kentry_byidx(kt, 0, af)) == NULL) {
22887d0f8cd9SKristof Provost 			pfr_kstate_counter_add(&kt->pfrkt_nomatch, 1);
22897d0f8cd9SKristof Provost 			return (1);
22907d0f8cd9SKristof Provost 		}
22917d0f8cd9SKristof Provost 		idx = 0;
22927d0f8cd9SKristof Provost 		loop++;
22937d0f8cd9SKristof Provost 	}
22943b3a8eb9SGleb Smirnoff 	pfr_prepare_network(&umask, af, ke->pfrke_net);
22958ca12190SKristof Provost 	pfr_sockaddr_to_pf_addr(&ke->pfrke_sa, &cur);
22968ca12190SKristof Provost 	pfr_sockaddr_to_pf_addr(&umask, &mask);
22973b3a8eb9SGleb Smirnoff 
22987d0f8cd9SKristof Provost 	if (use_counter && !PF_AZERO(counter, af)) {
22993b3a8eb9SGleb Smirnoff 		/* is supplied address within block? */
23008ca12190SKristof Provost 		if (!PF_MATCHA(0, &cur, &mask, counter, af)) {
23013b3a8eb9SGleb Smirnoff 			/* no, go to next block in table */
23023b3a8eb9SGleb Smirnoff 			idx++;
23033b3a8eb9SGleb Smirnoff 			use_counter = 0;
23043b3a8eb9SGleb Smirnoff 			goto _next_block;
23053b3a8eb9SGleb Smirnoff 		}
230667b65598SKristof Provost 		PF_ACPY(addr, counter, af);
23073b3a8eb9SGleb Smirnoff 	} else {
23083b3a8eb9SGleb Smirnoff 		/* use first address of block */
230967b65598SKristof Provost 		PF_ACPY(addr, &cur, af);
23103b3a8eb9SGleb Smirnoff 	}
23113b3a8eb9SGleb Smirnoff 
23123b3a8eb9SGleb Smirnoff 	if (!KENTRY_NETWORK(ke)) {
23133b3a8eb9SGleb Smirnoff 		/* this is a single IP address - no possible nested block */
231467b65598SKristof Provost 		if (filter && filter(af, addr)) {
23157d381d0aSKristof Provost 			idx++;
23167d381d0aSKristof Provost 			goto _next_block;
23177d381d0aSKristof Provost 		}
231867b65598SKristof Provost 		PF_ACPY(counter, addr, af);
23193b3a8eb9SGleb Smirnoff 		*pidx = idx;
2320f92c21a2SMateusz Guzik 		pfr_kstate_counter_add(&kt->pfrkt_match, 1);
23213b3a8eb9SGleb Smirnoff 		return (0);
23223b3a8eb9SGleb Smirnoff 	}
23233b3a8eb9SGleb Smirnoff 	for (;;) {
23243b3a8eb9SGleb Smirnoff 		/* we don't want to use a nested block */
23253b3a8eb9SGleb Smirnoff 		switch (af) {
23263b3a8eb9SGleb Smirnoff 		case AF_INET:
23273b3a8eb9SGleb Smirnoff 			ke2 = (struct pfr_kentry *)rn_match(&uaddr,
232861eee0e2SAlexander V. Chernikov 			    &kt->pfrkt_ip4->rh);
23293b3a8eb9SGleb Smirnoff 			break;
23303b3a8eb9SGleb Smirnoff 		case AF_INET6:
23313b3a8eb9SGleb Smirnoff 			ke2 = (struct pfr_kentry *)rn_match(&uaddr,
233261eee0e2SAlexander V. Chernikov 			    &kt->pfrkt_ip6->rh);
23333b3a8eb9SGleb Smirnoff 			break;
23343b3a8eb9SGleb Smirnoff 		}
23353b3a8eb9SGleb Smirnoff 		/* no need to check KENTRY_RNF_ROOT() here */
23363b3a8eb9SGleb Smirnoff 		if (ke2 == ke) {
23373b3a8eb9SGleb Smirnoff 			/* lookup return the same block - perfect */
233867b65598SKristof Provost 			if (filter && filter(af, addr))
23397d381d0aSKristof Provost 				goto _next_entry;
234067b65598SKristof Provost 			PF_ACPY(counter, addr, af);
23413b3a8eb9SGleb Smirnoff 			*pidx = idx;
2342f92c21a2SMateusz Guzik 			pfr_kstate_counter_add(&kt->pfrkt_match, 1);
23433b3a8eb9SGleb Smirnoff 			return (0);
23443b3a8eb9SGleb Smirnoff 		}
23453b3a8eb9SGleb Smirnoff 
23467d381d0aSKristof Provost _next_entry:
23473b3a8eb9SGleb Smirnoff 		/* we need to increase the counter past the nested block */
23483b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&umask, AF_INET, ke2->pfrke_net);
23498ca12190SKristof Provost 		pfr_sockaddr_to_pf_addr(&umask, &umask_addr);
235067b65598SKristof Provost 		PF_POOLMASK(addr, addr, &umask_addr, &pfr_ffaddr, af);
235167b65598SKristof Provost 		PF_AINC(addr, af);
235267b65598SKristof Provost 		if (!PF_MATCHA(0, &cur, &mask, addr, af)) {
23533b3a8eb9SGleb Smirnoff 			/* ok, we reached the end of our main block */
23543b3a8eb9SGleb Smirnoff 			/* go to next block in table */
23553b3a8eb9SGleb Smirnoff 			idx++;
23563b3a8eb9SGleb Smirnoff 			use_counter = 0;
23573b3a8eb9SGleb Smirnoff 			goto _next_block;
23583b3a8eb9SGleb Smirnoff 		}
23593b3a8eb9SGleb Smirnoff 	}
23603b3a8eb9SGleb Smirnoff }
23613b3a8eb9SGleb Smirnoff 
23623b3a8eb9SGleb Smirnoff static struct pfr_kentry *
23633b3a8eb9SGleb Smirnoff pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af)
23643b3a8eb9SGleb Smirnoff {
23653b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
23663b3a8eb9SGleb Smirnoff 
23673b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
23683b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_POOL_GET;
23697b676698SKristof Provost 	w.pfrw_free = idx;
23703b3a8eb9SGleb Smirnoff 
23713b3a8eb9SGleb Smirnoff 	switch (af) {
23723b3a8eb9SGleb Smirnoff #ifdef INET
23733b3a8eb9SGleb Smirnoff 	case AF_INET:
237461eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
23753b3a8eb9SGleb Smirnoff 		return (w.pfrw_kentry);
23763b3a8eb9SGleb Smirnoff #endif /* INET */
23773b3a8eb9SGleb Smirnoff #ifdef INET6
23783b3a8eb9SGleb Smirnoff 	case AF_INET6:
237961eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
23803b3a8eb9SGleb Smirnoff 		return (w.pfrw_kentry);
23813b3a8eb9SGleb Smirnoff #endif /* INET6 */
23823b3a8eb9SGleb Smirnoff 	default:
23833b3a8eb9SGleb Smirnoff 		return (NULL);
23843b3a8eb9SGleb Smirnoff 	}
23853b3a8eb9SGleb Smirnoff }
23863b3a8eb9SGleb Smirnoff 
23873b3a8eb9SGleb Smirnoff void
23883b3a8eb9SGleb Smirnoff pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn)
23893b3a8eb9SGleb Smirnoff {
23903b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
23913b3a8eb9SGleb Smirnoff 
23923b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
23933b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_DYNADDR_UPDATE;
23943b3a8eb9SGleb Smirnoff 	w.pfrw_dyn = dyn;
23953b3a8eb9SGleb Smirnoff 
23963b3a8eb9SGleb Smirnoff 	dyn->pfid_acnt4 = 0;
23973b3a8eb9SGleb Smirnoff 	dyn->pfid_acnt6 = 0;
23983b3a8eb9SGleb Smirnoff 	if (!dyn->pfid_af || dyn->pfid_af == AF_INET)
239961eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
24003b3a8eb9SGleb Smirnoff 	if (!dyn->pfid_af || dyn->pfid_af == AF_INET6)
240161eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
24023b3a8eb9SGleb Smirnoff }
2403