xref: /freebsd/sys/netpfil/pf/pf_table.c (revision e86bddea9fe62d5093a1942cf21950b3c5ca62e5)
1d8aa10ccSGleb Smirnoff /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
363b3a8eb9SGleb Smirnoff 
373b3a8eb9SGleb Smirnoff #include "opt_inet.h"
383b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
393b3a8eb9SGleb Smirnoff 
403b3a8eb9SGleb Smirnoff #include <sys/param.h>
413b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
423b3a8eb9SGleb Smirnoff #include <sys/lock.h>
433b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
44eedc7fd9SGleb Smirnoff #include <sys/mbuf.h>
453b3a8eb9SGleb Smirnoff #include <sys/mutex.h>
463b3a8eb9SGleb Smirnoff #include <sys/refcount.h>
473b3a8eb9SGleb Smirnoff #include <sys/socket.h>
483b3a8eb9SGleb Smirnoff #include <vm/uma.h>
493b3a8eb9SGleb Smirnoff 
503b3a8eb9SGleb Smirnoff #include <net/if.h>
513b3a8eb9SGleb Smirnoff #include <net/vnet.h>
523b3a8eb9SGleb Smirnoff #include <net/pfvar.h>
533b3a8eb9SGleb Smirnoff 
54032dff66SKristof Provost #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
55032dff66SKristof Provost 
563b3a8eb9SGleb Smirnoff #define	ACCEPT_FLAGS(flags, oklist)		\
573b3a8eb9SGleb Smirnoff 	do {					\
583b3a8eb9SGleb Smirnoff 		if ((flags & ~(oklist)) &	\
593b3a8eb9SGleb Smirnoff 		    PFR_FLAG_ALLMASK)		\
603b3a8eb9SGleb Smirnoff 			return (EINVAL);	\
613b3a8eb9SGleb Smirnoff 	} while (0)
623b3a8eb9SGleb Smirnoff 
633b3a8eb9SGleb Smirnoff #define	FILLIN_SIN(sin, addr)			\
643b3a8eb9SGleb Smirnoff 	do {					\
653b3a8eb9SGleb Smirnoff 		(sin).sin_len = sizeof(sin);	\
663b3a8eb9SGleb Smirnoff 		(sin).sin_family = AF_INET;	\
673b3a8eb9SGleb Smirnoff 		(sin).sin_addr = (addr);	\
683b3a8eb9SGleb Smirnoff 	} while (0)
693b3a8eb9SGleb Smirnoff 
703b3a8eb9SGleb Smirnoff #define	FILLIN_SIN6(sin6, addr)			\
713b3a8eb9SGleb Smirnoff 	do {					\
723b3a8eb9SGleb Smirnoff 		(sin6).sin6_len = sizeof(sin6);	\
733b3a8eb9SGleb Smirnoff 		(sin6).sin6_family = AF_INET6;	\
743b3a8eb9SGleb Smirnoff 		(sin6).sin6_addr = (addr);	\
753b3a8eb9SGleb Smirnoff 	} while (0)
763b3a8eb9SGleb Smirnoff 
773b3a8eb9SGleb Smirnoff #define	SWAP(type, a1, a2)			\
783b3a8eb9SGleb Smirnoff 	do {					\
793b3a8eb9SGleb Smirnoff 		type tmp = a1;			\
803b3a8eb9SGleb Smirnoff 		a1 = a2;			\
813b3a8eb9SGleb Smirnoff 		a2 = tmp;			\
823b3a8eb9SGleb Smirnoff 	} while (0)
833b3a8eb9SGleb Smirnoff 
843b3a8eb9SGleb Smirnoff #define	SUNION2PF(su, af) (((af)==AF_INET) ?	\
853b3a8eb9SGleb Smirnoff     (struct pf_addr *)&(su)->sin.sin_addr :	\
863b3a8eb9SGleb Smirnoff     (struct pf_addr *)&(su)->sin6.sin6_addr)
873b3a8eb9SGleb Smirnoff 
883b3a8eb9SGleb Smirnoff #define	AF_BITS(af)		(((af)==AF_INET)?32:128)
893b3a8eb9SGleb Smirnoff #define	ADDR_NETWORK(ad)	((ad)->pfra_net < AF_BITS((ad)->pfra_af))
903b3a8eb9SGleb Smirnoff #define	KENTRY_NETWORK(ke)	((ke)->pfrke_net < AF_BITS((ke)->pfrke_af))
913b3a8eb9SGleb Smirnoff #define	KENTRY_RNF_ROOT(ke) \
923b3a8eb9SGleb Smirnoff 		((((struct radix_node *)(ke))->rn_flags & RNF_ROOT) != 0)
933b3a8eb9SGleb Smirnoff 
943b3a8eb9SGleb Smirnoff #define	NO_ADDRESSES		(-1)
953b3a8eb9SGleb Smirnoff #define	ENQUEUE_UNMARKED_ONLY	(1)
963b3a8eb9SGleb Smirnoff #define	INVERT_NEG_FLAG		(1)
973b3a8eb9SGleb Smirnoff 
983b3a8eb9SGleb Smirnoff struct pfr_walktree {
993b3a8eb9SGleb Smirnoff 	enum pfrw_op {
1003b3a8eb9SGleb Smirnoff 		PFRW_MARK,
1013b3a8eb9SGleb Smirnoff 		PFRW_SWEEP,
1023b3a8eb9SGleb Smirnoff 		PFRW_ENQUEUE,
1033b3a8eb9SGleb Smirnoff 		PFRW_GET_ADDRS,
1043b3a8eb9SGleb Smirnoff 		PFRW_GET_ASTATS,
1053b3a8eb9SGleb Smirnoff 		PFRW_POOL_GET,
1063b3a8eb9SGleb Smirnoff 		PFRW_DYNADDR_UPDATE
1073b3a8eb9SGleb Smirnoff 	}	 pfrw_op;
1083b3a8eb9SGleb Smirnoff 	union {
1093b3a8eb9SGleb Smirnoff 		struct pfr_addr		*pfrw1_addr;
1103b3a8eb9SGleb Smirnoff 		struct pfr_astats	*pfrw1_astats;
1113b3a8eb9SGleb Smirnoff 		struct pfr_kentryworkq	*pfrw1_workq;
1123b3a8eb9SGleb Smirnoff 		struct pfr_kentry	*pfrw1_kentry;
1133b3a8eb9SGleb Smirnoff 		struct pfi_dynaddr	*pfrw1_dyn;
1143b3a8eb9SGleb Smirnoff 	}	 pfrw_1;
1153b3a8eb9SGleb Smirnoff 	int	 pfrw_free;
11659048686SKristof Provost 	int	 pfrw_flags;
1173b3a8eb9SGleb Smirnoff };
1183b3a8eb9SGleb Smirnoff #define	pfrw_addr	pfrw_1.pfrw1_addr
1193b3a8eb9SGleb Smirnoff #define	pfrw_astats	pfrw_1.pfrw1_astats
1203b3a8eb9SGleb Smirnoff #define	pfrw_workq	pfrw_1.pfrw1_workq
1213b3a8eb9SGleb Smirnoff #define	pfrw_kentry	pfrw_1.pfrw1_kentry
1223b3a8eb9SGleb Smirnoff #define	pfrw_dyn	pfrw_1.pfrw1_dyn
1233b3a8eb9SGleb Smirnoff #define	pfrw_cnt	pfrw_free
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff #define	senderr(e)	do { rv = (e); goto _bad; } while (0)
1263b3a8eb9SGleb Smirnoff 
1273b3a8eb9SGleb Smirnoff static MALLOC_DEFINE(M_PFTABLE, "pf_table", "pf(4) tables structures");
1285f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_z);
1293b3a8eb9SGleb Smirnoff #define	V_pfr_kentry_z		VNET(pfr_kentry_z)
130c1be8399SMark Johnston VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_counter_z);
131c1be8399SMark Johnston #define	V_pfr_kentry_counter_z	VNET(pfr_kentry_counter_z)
1323b3a8eb9SGleb Smirnoff 
1333b3a8eb9SGleb Smirnoff static struct pf_addr	 pfr_ffaddr = {
1343b3a8eb9SGleb Smirnoff 	.addr32 = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
1353b3a8eb9SGleb Smirnoff };
1363b3a8eb9SGleb Smirnoff 
13759048686SKristof Provost static void		 pfr_copyout_astats(struct pfr_astats *,
13859048686SKristof Provost 			    const struct pfr_kentry *,
13959048686SKristof Provost 			    const struct pfr_walktree *);
1403b3a8eb9SGleb Smirnoff static void		 pfr_copyout_addr(struct pfr_addr *,
14159048686SKristof Provost 			    const struct pfr_kentry *ke);
1423b3a8eb9SGleb Smirnoff static int		 pfr_validate_addr(struct pfr_addr *);
1433b3a8eb9SGleb Smirnoff static void		 pfr_enqueue_addrs(struct pfr_ktable *,
1443b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *, int *, int);
1453b3a8eb9SGleb Smirnoff static void		 pfr_mark_addrs(struct pfr_ktable *);
1463b3a8eb9SGleb Smirnoff static struct pfr_kentry
1473b3a8eb9SGleb Smirnoff 			*pfr_lookup_addr(struct pfr_ktable *,
1483b3a8eb9SGleb Smirnoff 			    struct pfr_addr *, int);
14921121f9bSMark Johnston static struct pfr_kentry *pfr_create_kentry(struct pfr_addr *, bool);
1503b3a8eb9SGleb Smirnoff static void		 pfr_destroy_kentries(struct pfr_kentryworkq *);
1513b3a8eb9SGleb Smirnoff static void		 pfr_destroy_kentry(struct pfr_kentry *);
1523b3a8eb9SGleb Smirnoff static void		 pfr_insert_kentries(struct pfr_ktable *,
1533b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *, long);
1543b3a8eb9SGleb Smirnoff static void		 pfr_remove_kentries(struct pfr_ktable *,
1553b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *);
15621121f9bSMark Johnston static void		 pfr_clstats_kentries(struct pfr_ktable *,
15721121f9bSMark Johnston 			    struct pfr_kentryworkq *, long, int);
1583b3a8eb9SGleb Smirnoff static void		 pfr_reset_feedback(struct pfr_addr *, int);
1593b3a8eb9SGleb Smirnoff static void		 pfr_prepare_network(union sockaddr_union *, int, int);
1603b3a8eb9SGleb Smirnoff static int		 pfr_route_kentry(struct pfr_ktable *,
1613b3a8eb9SGleb Smirnoff 			    struct pfr_kentry *);
1623b3a8eb9SGleb Smirnoff static int		 pfr_unroute_kentry(struct pfr_ktable *,
1633b3a8eb9SGleb Smirnoff 			    struct pfr_kentry *);
1643b3a8eb9SGleb Smirnoff static int		 pfr_walktree(struct radix_node *, void *);
1653b3a8eb9SGleb Smirnoff static int		 pfr_validate_table(struct pfr_table *, int, int);
1663b3a8eb9SGleb Smirnoff static int		 pfr_fix_anchor(char *);
1673b3a8eb9SGleb Smirnoff static void		 pfr_commit_ktable(struct pfr_ktable *, long);
1683b3a8eb9SGleb Smirnoff static void		 pfr_insert_ktables(struct pfr_ktableworkq *);
1693b3a8eb9SGleb Smirnoff static void		 pfr_insert_ktable(struct pfr_ktable *);
1703b3a8eb9SGleb Smirnoff static void		 pfr_setflags_ktables(struct pfr_ktableworkq *);
1713b3a8eb9SGleb Smirnoff static void		 pfr_setflags_ktable(struct pfr_ktable *, int);
1723b3a8eb9SGleb Smirnoff static void		 pfr_clstats_ktables(struct pfr_ktableworkq *, long,
1733b3a8eb9SGleb Smirnoff 			    int);
1743b3a8eb9SGleb Smirnoff static void		 pfr_clstats_ktable(struct pfr_ktable *, long, int);
1753b3a8eb9SGleb Smirnoff static struct pfr_ktable
1763b3a8eb9SGleb Smirnoff 			*pfr_create_ktable(struct pfr_table *, long, int);
1773b3a8eb9SGleb Smirnoff static void		 pfr_destroy_ktables(struct pfr_ktableworkq *, int);
1783b3a8eb9SGleb Smirnoff static void		 pfr_destroy_ktable(struct pfr_ktable *, int);
1793b3a8eb9SGleb Smirnoff static int		 pfr_ktable_compare(struct pfr_ktable *,
1803b3a8eb9SGleb Smirnoff 			    struct pfr_ktable *);
1813b3a8eb9SGleb Smirnoff static struct pfr_ktable
1823b3a8eb9SGleb Smirnoff 			*pfr_lookup_table(struct pfr_table *);
1833b3a8eb9SGleb Smirnoff static void		 pfr_clean_node_mask(struct pfr_ktable *,
1843b3a8eb9SGleb Smirnoff 			    struct pfr_kentryworkq *);
1853b3a8eb9SGleb Smirnoff static int		 pfr_skip_table(struct pfr_table *,
1863b3a8eb9SGleb Smirnoff 			    struct pfr_ktable *, int);
1873b3a8eb9SGleb Smirnoff static struct pfr_kentry
1883b3a8eb9SGleb Smirnoff 			*pfr_kentry_byidx(struct pfr_ktable *, int, int);
1893b3a8eb9SGleb Smirnoff 
1903b3a8eb9SGleb Smirnoff static RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
1913b3a8eb9SGleb Smirnoff static RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
1923b3a8eb9SGleb Smirnoff 
1935f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_ktablehead, pfr_ktables);
1941e9e3741SMarko Zec #define	V_pfr_ktables	VNET(pfr_ktables)
1951e9e3741SMarko Zec 
1965f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_table, pfr_nulltable);
1971e9e3741SMarko Zec #define	V_pfr_nulltable	VNET(pfr_nulltable)
1981e9e3741SMarko Zec 
1995f901c92SAndrew Turner VNET_DEFINE_STATIC(int, pfr_ktable_cnt);
2001e9e3741SMarko Zec #define V_pfr_ktable_cnt	VNET(pfr_ktable_cnt)
2013b3a8eb9SGleb Smirnoff 
2023b3a8eb9SGleb Smirnoff void
2033b3a8eb9SGleb Smirnoff pfr_initialize(void)
2043b3a8eb9SGleb Smirnoff {
2053b3a8eb9SGleb Smirnoff 
206c1be8399SMark Johnston 	V_pfr_kentry_counter_z = uma_zcreate("pf table entry counters",
207c1be8399SMark Johnston 	    PFR_NUM_COUNTERS * sizeof(uint64_t), NULL, NULL, NULL, NULL,
208c1be8399SMark Johnston 	    UMA_ALIGN_PTR, UMA_ZONE_PCPU);
2093b3a8eb9SGleb Smirnoff 	V_pfr_kentry_z = uma_zcreate("pf table entries",
2103b3a8eb9SGleb Smirnoff 	    sizeof(struct pfr_kentry), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
2113b3a8eb9SGleb Smirnoff 	    0);
2123b3a8eb9SGleb Smirnoff 	V_pf_limits[PF_LIMIT_TABLE_ENTRIES].zone = V_pfr_kentry_z;
2133b3a8eb9SGleb Smirnoff 	V_pf_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT;
2143b3a8eb9SGleb Smirnoff }
2153b3a8eb9SGleb Smirnoff 
2163b3a8eb9SGleb Smirnoff void
2173b3a8eb9SGleb Smirnoff pfr_cleanup(void)
2183b3a8eb9SGleb Smirnoff {
2193b3a8eb9SGleb Smirnoff 
2203b3a8eb9SGleb Smirnoff 	uma_zdestroy(V_pfr_kentry_z);
221c1be8399SMark Johnston 	uma_zdestroy(V_pfr_kentry_counter_z);
2223b3a8eb9SGleb Smirnoff }
2233b3a8eb9SGleb Smirnoff 
2243b3a8eb9SGleb Smirnoff int
2253b3a8eb9SGleb Smirnoff pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
2263b3a8eb9SGleb Smirnoff {
2273b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
2283b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
2293b3a8eb9SGleb Smirnoff 
2303b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
2313b3a8eb9SGleb Smirnoff 
2323b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
2333b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
2343b3a8eb9SGleb Smirnoff 		return (EINVAL);
2353b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
2363b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2373b3a8eb9SGleb Smirnoff 		return (ESRCH);
2383b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
2393b3a8eb9SGleb Smirnoff 		return (EPERM);
2403b3a8eb9SGleb Smirnoff 	pfr_enqueue_addrs(kt, &workq, ndel, 0);
2413b3a8eb9SGleb Smirnoff 
2423b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
2433b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &workq);
2443b3a8eb9SGleb Smirnoff 		KASSERT(kt->pfrkt_cnt == 0, ("%s: non-null pfrkt_cnt", __func__));
2453b3a8eb9SGleb Smirnoff 	}
2463b3a8eb9SGleb Smirnoff 	return (0);
2473b3a8eb9SGleb Smirnoff }
2483b3a8eb9SGleb Smirnoff 
2493b3a8eb9SGleb Smirnoff int
2503b3a8eb9SGleb Smirnoff pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
2513b3a8eb9SGleb Smirnoff     int *nadd, int flags)
2523b3a8eb9SGleb Smirnoff {
2533b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *tmpkt;
2543b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
2553b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
2563b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
2573b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0;
2583b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
2593b3a8eb9SGleb Smirnoff 
2603b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
2613b3a8eb9SGleb Smirnoff 
2623b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
2633b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
2643b3a8eb9SGleb Smirnoff 		return (EINVAL);
2653b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
2663b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2673b3a8eb9SGleb Smirnoff 		return (ESRCH);
2683b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
2693b3a8eb9SGleb Smirnoff 		return (EPERM);
2701e9e3741SMarko Zec 	tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0);
2713b3a8eb9SGleb Smirnoff 	if (tmpkt == NULL)
2723b3a8eb9SGleb Smirnoff 		return (ENOMEM);
2733b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
2743b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
2753b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
2763b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
2773b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
2783b3a8eb9SGleb Smirnoff 		q = pfr_lookup_addr(tmpkt, ad, 1);
2793b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
2803b3a8eb9SGleb Smirnoff 			if (q != NULL)
2813b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DUPLICATE;
2823b3a8eb9SGleb Smirnoff 			else if (p == NULL)
2833b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_ADDED;
2843b3a8eb9SGleb Smirnoff 			else if (p->pfrke_not != ad->pfra_not)
2853b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_CONFLICT;
2863b3a8eb9SGleb Smirnoff 			else
2873b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
2883b3a8eb9SGleb Smirnoff 		}
2893b3a8eb9SGleb Smirnoff 		if (p == NULL && q == NULL) {
29021121f9bSMark Johnston 			p = pfr_create_kentry(ad,
29121121f9bSMark Johnston 			    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
2923b3a8eb9SGleb Smirnoff 			if (p == NULL)
2933b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
2943b3a8eb9SGleb Smirnoff 			if (pfr_route_kentry(tmpkt, p)) {
2953b3a8eb9SGleb Smirnoff 				pfr_destroy_kentry(p);
2963b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
2973b3a8eb9SGleb Smirnoff 			} else {
2983b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
2993b3a8eb9SGleb Smirnoff 				xadd++;
3003b3a8eb9SGleb Smirnoff 			}
3013b3a8eb9SGleb Smirnoff 		}
3023b3a8eb9SGleb Smirnoff 	}
3033b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &workq);
3043b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
3053b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &workq, tzero);
3063b3a8eb9SGleb Smirnoff 	else
3073b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&workq);
3083b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
3093b3a8eb9SGleb Smirnoff 		*nadd = xadd;
3103b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
3113b3a8eb9SGleb Smirnoff 	return (0);
3123b3a8eb9SGleb Smirnoff _bad:
3133b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &workq);
3143b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&workq);
3153b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
3163b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
3173b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
3183b3a8eb9SGleb Smirnoff 	return (rv);
3193b3a8eb9SGleb Smirnoff }
3203b3a8eb9SGleb Smirnoff 
3213b3a8eb9SGleb Smirnoff int
3223b3a8eb9SGleb Smirnoff pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
3233b3a8eb9SGleb Smirnoff     int *ndel, int flags)
3243b3a8eb9SGleb Smirnoff {
3253b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
3263b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
3273b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
3283b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
3293b3a8eb9SGleb Smirnoff 	int			 i, rv, xdel = 0, log = 1;
3303b3a8eb9SGleb Smirnoff 
3313b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
3323b3a8eb9SGleb Smirnoff 
3333b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
3343b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
3353b3a8eb9SGleb Smirnoff 		return (EINVAL);
3363b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
3373b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
3383b3a8eb9SGleb Smirnoff 		return (ESRCH);
3393b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
3403b3a8eb9SGleb Smirnoff 		return (EPERM);
3413b3a8eb9SGleb Smirnoff 	/*
3423b3a8eb9SGleb Smirnoff 	 * there are two algorithms to choose from here.
3433b3a8eb9SGleb Smirnoff 	 * with:
3443b3a8eb9SGleb Smirnoff 	 *   n: number of addresses to delete
3453b3a8eb9SGleb Smirnoff 	 *   N: number of addresses in the table
3463b3a8eb9SGleb Smirnoff 	 *
3473b3a8eb9SGleb Smirnoff 	 * one is O(N) and is better for large 'n'
3483b3a8eb9SGleb Smirnoff 	 * one is O(n*LOG(N)) and is better for small 'n'
3493b3a8eb9SGleb Smirnoff 	 *
3503b3a8eb9SGleb Smirnoff 	 * following code try to decide which one is best.
3513b3a8eb9SGleb Smirnoff 	 */
3523b3a8eb9SGleb Smirnoff 	for (i = kt->pfrkt_cnt; i > 0; i >>= 1)
3533b3a8eb9SGleb Smirnoff 		log++;
3543b3a8eb9SGleb Smirnoff 	if (size > kt->pfrkt_cnt/log) {
3553b3a8eb9SGleb Smirnoff 		/* full table scan */
3563b3a8eb9SGleb Smirnoff 		pfr_mark_addrs(kt);
3573b3a8eb9SGleb Smirnoff 	} else {
3583b3a8eb9SGleb Smirnoff 		/* iterate over addresses to delete */
3593b3a8eb9SGleb Smirnoff 		for (i = 0, ad = addr; i < size; i++, ad++) {
3603b3a8eb9SGleb Smirnoff 			if (pfr_validate_addr(ad))
3613b3a8eb9SGleb Smirnoff 				return (EINVAL);
3623b3a8eb9SGleb Smirnoff 			p = pfr_lookup_addr(kt, ad, 1);
3633b3a8eb9SGleb Smirnoff 			if (p != NULL)
3643b3a8eb9SGleb Smirnoff 				p->pfrke_mark = 0;
3653b3a8eb9SGleb Smirnoff 		}
3663b3a8eb9SGleb Smirnoff 	}
3673b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
3683b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
3693b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
3703b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
3713b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
3723b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
3733b3a8eb9SGleb Smirnoff 			if (p == NULL)
3743b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_NONE;
3753b3a8eb9SGleb Smirnoff 			else if (p->pfrke_not != ad->pfra_not)
3763b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_CONFLICT;
3773b3a8eb9SGleb Smirnoff 			else if (p->pfrke_mark)
3783b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DUPLICATE;
3793b3a8eb9SGleb Smirnoff 			else
3803b3a8eb9SGleb Smirnoff 				ad->pfra_fback = PFR_FB_DELETED;
3813b3a8eb9SGleb Smirnoff 		}
3823b3a8eb9SGleb Smirnoff 		if (p != NULL && p->pfrke_not == ad->pfra_not &&
3833b3a8eb9SGleb Smirnoff 		    !p->pfrke_mark) {
3843b3a8eb9SGleb Smirnoff 			p->pfrke_mark = 1;
3853b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
3863b3a8eb9SGleb Smirnoff 			xdel++;
3873b3a8eb9SGleb Smirnoff 		}
3883b3a8eb9SGleb Smirnoff 	}
3893b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
3903b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &workq);
3913b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
3923b3a8eb9SGleb Smirnoff 		*ndel = xdel;
3933b3a8eb9SGleb Smirnoff 	return (0);
3943b3a8eb9SGleb Smirnoff _bad:
3953b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
3963b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
3973b3a8eb9SGleb Smirnoff 	return (rv);
3983b3a8eb9SGleb Smirnoff }
3993b3a8eb9SGleb Smirnoff 
4003b3a8eb9SGleb Smirnoff int
4013b3a8eb9SGleb Smirnoff pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
4023b3a8eb9SGleb Smirnoff     int *size2, int *nadd, int *ndel, int *nchange, int flags,
4033b3a8eb9SGleb Smirnoff     u_int32_t ignore_pfrt_flags)
4043b3a8eb9SGleb Smirnoff {
4053b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *tmpkt;
4063b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addq, delq, changeq;
4073b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
4083b3a8eb9SGleb Smirnoff 	struct pfr_addr		 ad;
4093b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0, xdel = 0, xchange = 0;
4103b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
4113b3a8eb9SGleb Smirnoff 
4123b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
4133b3a8eb9SGleb Smirnoff 
4143b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
4153b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, ignore_pfrt_flags, flags &
4163b3a8eb9SGleb Smirnoff 	    PFR_FLAG_USERIOCTL))
4173b3a8eb9SGleb Smirnoff 		return (EINVAL);
4183b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
4193b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
4203b3a8eb9SGleb Smirnoff 		return (ESRCH);
4213b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_flags & PFR_TFLAG_CONST)
4223b3a8eb9SGleb Smirnoff 		return (EPERM);
4231e9e3741SMarko Zec 	tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0);
4243b3a8eb9SGleb Smirnoff 	if (tmpkt == NULL)
4253b3a8eb9SGleb Smirnoff 		return (ENOMEM);
4263b3a8eb9SGleb Smirnoff 	pfr_mark_addrs(kt);
4273b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addq);
4283b3a8eb9SGleb Smirnoff 	SLIST_INIT(&delq);
4293b3a8eb9SGleb Smirnoff 	SLIST_INIT(&changeq);
4303b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
4313b3a8eb9SGleb Smirnoff 		/*
4323b3a8eb9SGleb Smirnoff 		 * XXXGL: undertand pf_if usage of this function
4333b3a8eb9SGleb Smirnoff 		 * and make ad a moving pointer
4343b3a8eb9SGleb Smirnoff 		 */
4353b3a8eb9SGleb Smirnoff 		bcopy(addr + i, &ad, sizeof(ad));
4363b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(&ad))
4373b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
4383b3a8eb9SGleb Smirnoff 		ad.pfra_fback = PFR_FB_NONE;
4393b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, &ad, 1);
4403b3a8eb9SGleb Smirnoff 		if (p != NULL) {
4413b3a8eb9SGleb Smirnoff 			if (p->pfrke_mark) {
4423b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_DUPLICATE;
4433b3a8eb9SGleb Smirnoff 				goto _skip;
4443b3a8eb9SGleb Smirnoff 			}
4453b3a8eb9SGleb Smirnoff 			p->pfrke_mark = 1;
4463b3a8eb9SGleb Smirnoff 			if (p->pfrke_not != ad.pfra_not) {
4473b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&changeq, p, pfrke_workq);
4483b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_CHANGED;
4493b3a8eb9SGleb Smirnoff 				xchange++;
4503b3a8eb9SGleb Smirnoff 			}
4513b3a8eb9SGleb Smirnoff 		} else {
4523b3a8eb9SGleb Smirnoff 			q = pfr_lookup_addr(tmpkt, &ad, 1);
4533b3a8eb9SGleb Smirnoff 			if (q != NULL) {
4543b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_DUPLICATE;
4553b3a8eb9SGleb Smirnoff 				goto _skip;
4563b3a8eb9SGleb Smirnoff 			}
45721121f9bSMark Johnston 			p = pfr_create_kentry(&ad,
45821121f9bSMark Johnston 			    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
4593b3a8eb9SGleb Smirnoff 			if (p == NULL)
4603b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
4613b3a8eb9SGleb Smirnoff 			if (pfr_route_kentry(tmpkt, p)) {
4623b3a8eb9SGleb Smirnoff 				pfr_destroy_kentry(p);
4633b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_NONE;
4643b3a8eb9SGleb Smirnoff 			} else {
4653b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
4663b3a8eb9SGleb Smirnoff 				ad.pfra_fback = PFR_FB_ADDED;
4673b3a8eb9SGleb Smirnoff 				xadd++;
4683b3a8eb9SGleb Smirnoff 			}
4693b3a8eb9SGleb Smirnoff 		}
4703b3a8eb9SGleb Smirnoff _skip:
4713b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK)
4723b3a8eb9SGleb Smirnoff 			bcopy(&ad, addr + i, sizeof(ad));
4733b3a8eb9SGleb Smirnoff 	}
4743b3a8eb9SGleb Smirnoff 	pfr_enqueue_addrs(kt, &delq, &xdel, ENQUEUE_UNMARKED_ONLY);
4753b3a8eb9SGleb Smirnoff 	if ((flags & PFR_FLAG_FEEDBACK) && *size2) {
4763b3a8eb9SGleb Smirnoff 		if (*size2 < size+xdel) {
4773b3a8eb9SGleb Smirnoff 			*size2 = size+xdel;
4783b3a8eb9SGleb Smirnoff 			senderr(0);
4793b3a8eb9SGleb Smirnoff 		}
4803b3a8eb9SGleb Smirnoff 		i = 0;
4813b3a8eb9SGleb Smirnoff 		SLIST_FOREACH(p, &delq, pfrke_workq) {
4823b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(&ad, p);
4833b3a8eb9SGleb Smirnoff 			ad.pfra_fback = PFR_FB_DELETED;
4843b3a8eb9SGleb Smirnoff 			bcopy(&ad, addr + size + i, sizeof(ad));
4853b3a8eb9SGleb Smirnoff 			i++;
4863b3a8eb9SGleb Smirnoff 		}
4873b3a8eb9SGleb Smirnoff 	}
4883b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &addq);
4893b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
4903b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &addq, tzero);
4913b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &delq);
49221121f9bSMark Johnston 		pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG);
4933b3a8eb9SGleb Smirnoff 	} else
4943b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addq);
4953b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
4963b3a8eb9SGleb Smirnoff 		*nadd = xadd;
4973b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
4983b3a8eb9SGleb Smirnoff 		*ndel = xdel;
4993b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
5003b3a8eb9SGleb Smirnoff 		*nchange = xchange;
5013b3a8eb9SGleb Smirnoff 	if ((flags & PFR_FLAG_FEEDBACK) && size2)
5023b3a8eb9SGleb Smirnoff 		*size2 = size+xdel;
5033b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
5043b3a8eb9SGleb Smirnoff 	return (0);
5053b3a8eb9SGleb Smirnoff _bad:
5063b3a8eb9SGleb Smirnoff 	pfr_clean_node_mask(tmpkt, &addq);
5073b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&addq);
5083b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
5093b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
5103b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(tmpkt, 0);
5113b3a8eb9SGleb Smirnoff 	return (rv);
5123b3a8eb9SGleb Smirnoff }
5133b3a8eb9SGleb Smirnoff 
5143b3a8eb9SGleb Smirnoff int
5153b3a8eb9SGleb Smirnoff pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
5163b3a8eb9SGleb Smirnoff 	int *nmatch, int flags)
5173b3a8eb9SGleb Smirnoff {
5183b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5193b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
5203b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
5213b3a8eb9SGleb Smirnoff 	int			 i, xmatch = 0;
5223b3a8eb9SGleb Smirnoff 
5233b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5243b3a8eb9SGleb Smirnoff 
5253b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_REPLACE);
5263b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
5273b3a8eb9SGleb Smirnoff 		return (EINVAL);
5283b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
5293b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
5303b3a8eb9SGleb Smirnoff 		return (ESRCH);
5313b3a8eb9SGleb Smirnoff 
5323b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
5333b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
5343b3a8eb9SGleb Smirnoff 			return (EINVAL);
5353b3a8eb9SGleb Smirnoff 		if (ADDR_NETWORK(ad))
5363b3a8eb9SGleb Smirnoff 			return (EINVAL);
5373b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 0);
5383b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_REPLACE)
5393b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(ad, p);
5403b3a8eb9SGleb Smirnoff 		ad->pfra_fback = (p == NULL) ? PFR_FB_NONE :
5413b3a8eb9SGleb Smirnoff 		    (p->pfrke_not ? PFR_FB_NOTMATCH : PFR_FB_MATCH);
5423b3a8eb9SGleb Smirnoff 		if (p != NULL && !p->pfrke_not)
5433b3a8eb9SGleb Smirnoff 			xmatch++;
5443b3a8eb9SGleb Smirnoff 	}
5453b3a8eb9SGleb Smirnoff 	if (nmatch != NULL)
5463b3a8eb9SGleb Smirnoff 		*nmatch = xmatch;
5473b3a8eb9SGleb Smirnoff 	return (0);
5483b3a8eb9SGleb Smirnoff }
5493b3a8eb9SGleb Smirnoff 
5503b3a8eb9SGleb Smirnoff int
5513b3a8eb9SGleb Smirnoff pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
5523b3a8eb9SGleb Smirnoff 	int flags)
5533b3a8eb9SGleb Smirnoff {
5543b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5553b3a8eb9SGleb Smirnoff 	struct pfr_walktree	 w;
5563b3a8eb9SGleb Smirnoff 	int			 rv;
5573b3a8eb9SGleb Smirnoff 
5583b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
5593b3a8eb9SGleb Smirnoff 
5603b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, 0);
5613b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
5623b3a8eb9SGleb Smirnoff 		return (EINVAL);
5633b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
5643b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
5653b3a8eb9SGleb Smirnoff 		return (ESRCH);
5663b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_cnt > *size) {
5673b3a8eb9SGleb Smirnoff 		*size = kt->pfrkt_cnt;
5683b3a8eb9SGleb Smirnoff 		return (0);
5693b3a8eb9SGleb Smirnoff 	}
5703b3a8eb9SGleb Smirnoff 
5713b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
5723b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_GET_ADDRS;
5733b3a8eb9SGleb Smirnoff 	w.pfrw_addr = addr;
5743b3a8eb9SGleb Smirnoff 	w.pfrw_free = kt->pfrkt_cnt;
57561eee0e2SAlexander V. Chernikov 	rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
5763b3a8eb9SGleb Smirnoff 	if (!rv)
57761eee0e2SAlexander V. Chernikov 		rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
57861eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w);
5793b3a8eb9SGleb Smirnoff 	if (rv)
5803b3a8eb9SGleb Smirnoff 		return (rv);
5813b3a8eb9SGleb Smirnoff 
5823b3a8eb9SGleb Smirnoff 	KASSERT(w.pfrw_free == 0, ("%s: corruption detected (%d)", __func__,
5833b3a8eb9SGleb Smirnoff 	    w.pfrw_free));
5843b3a8eb9SGleb Smirnoff 
5853b3a8eb9SGleb Smirnoff 	*size = kt->pfrkt_cnt;
5863b3a8eb9SGleb Smirnoff 	return (0);
5873b3a8eb9SGleb Smirnoff }
5883b3a8eb9SGleb Smirnoff 
5893b3a8eb9SGleb Smirnoff int
5903b3a8eb9SGleb Smirnoff pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
5913b3a8eb9SGleb Smirnoff 	int flags)
5923b3a8eb9SGleb Smirnoff {
5933b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
5943b3a8eb9SGleb Smirnoff 	struct pfr_walktree	 w;
5953b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
5963b3a8eb9SGleb Smirnoff 	int			 rv;
5973b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
5983b3a8eb9SGleb Smirnoff 
5993b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
6003b3a8eb9SGleb Smirnoff 
6013b3a8eb9SGleb Smirnoff 	/* XXX PFR_FLAG_CLSTATS disabled */
6023b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, 0);
6033b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
6043b3a8eb9SGleb Smirnoff 		return (EINVAL);
6053b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
6063b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
6073b3a8eb9SGleb Smirnoff 		return (ESRCH);
6083b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_cnt > *size) {
6093b3a8eb9SGleb Smirnoff 		*size = kt->pfrkt_cnt;
6103b3a8eb9SGleb Smirnoff 		return (0);
6113b3a8eb9SGleb Smirnoff 	}
6123b3a8eb9SGleb Smirnoff 
6133b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
6143b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_GET_ASTATS;
6153b3a8eb9SGleb Smirnoff 	w.pfrw_astats = addr;
6163b3a8eb9SGleb Smirnoff 	w.pfrw_free = kt->pfrkt_cnt;
61759048686SKristof Provost 	/*
61859048686SKristof Provost 	 * Flags below are for backward compatibility. It was possible to have
61959048686SKristof Provost 	 * a table without per-entry counters. Now they are always allocated,
62059048686SKristof Provost 	 * we just discard data when reading it if table is not configured to
62159048686SKristof Provost 	 * have counters.
62259048686SKristof Provost 	 */
62359048686SKristof Provost 	w.pfrw_flags = kt->pfrkt_flags;
62461eee0e2SAlexander V. Chernikov 	rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
6253b3a8eb9SGleb Smirnoff 	if (!rv)
62661eee0e2SAlexander V. Chernikov 		rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
62761eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w);
6283b3a8eb9SGleb Smirnoff 	if (!rv && (flags & PFR_FLAG_CLSTATS)) {
6293b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &workq, NULL, 0);
63021121f9bSMark Johnston 		pfr_clstats_kentries(kt, &workq, tzero, 0);
6313b3a8eb9SGleb Smirnoff 	}
6323b3a8eb9SGleb Smirnoff 	if (rv)
6333b3a8eb9SGleb Smirnoff 		return (rv);
6343b3a8eb9SGleb Smirnoff 
6353b3a8eb9SGleb Smirnoff 	if (w.pfrw_free) {
6363b3a8eb9SGleb Smirnoff 		printf("pfr_get_astats: corruption detected (%d).\n",
6373b3a8eb9SGleb Smirnoff 		    w.pfrw_free);
6383b3a8eb9SGleb Smirnoff 		return (ENOTTY);
6393b3a8eb9SGleb Smirnoff 	}
6403b3a8eb9SGleb Smirnoff 	*size = kt->pfrkt_cnt;
6413b3a8eb9SGleb Smirnoff 	return (0);
6423b3a8eb9SGleb Smirnoff }
6433b3a8eb9SGleb Smirnoff 
6443b3a8eb9SGleb Smirnoff int
6453b3a8eb9SGleb Smirnoff pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
6463b3a8eb9SGleb Smirnoff     int *nzero, int flags)
6473b3a8eb9SGleb Smirnoff {
6483b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
6493b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 workq;
6503b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
6513b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
6523b3a8eb9SGleb Smirnoff 	int			 i, rv, xzero = 0;
6533b3a8eb9SGleb Smirnoff 
6543b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
6553b3a8eb9SGleb Smirnoff 
6563b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK);
6573b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, 0, 0))
6583b3a8eb9SGleb Smirnoff 		return (EINVAL);
6593b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(tbl);
6603b3a8eb9SGleb Smirnoff 	if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
6613b3a8eb9SGleb Smirnoff 		return (ESRCH);
6623b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
6633b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
6643b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
6653b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
6663b3a8eb9SGleb Smirnoff 		p = pfr_lookup_addr(kt, ad, 1);
6673b3a8eb9SGleb Smirnoff 		if (flags & PFR_FLAG_FEEDBACK) {
6683b3a8eb9SGleb Smirnoff 			ad->pfra_fback = (p != NULL) ?
6693b3a8eb9SGleb Smirnoff 			    PFR_FB_CLEARED : PFR_FB_NONE;
6703b3a8eb9SGleb Smirnoff 		}
6713b3a8eb9SGleb Smirnoff 		if (p != NULL) {
6723b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
6733b3a8eb9SGleb Smirnoff 			xzero++;
6743b3a8eb9SGleb Smirnoff 		}
6753b3a8eb9SGleb Smirnoff 	}
6763b3a8eb9SGleb Smirnoff 
6773b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
67821121f9bSMark Johnston 		pfr_clstats_kentries(kt, &workq, 0, 0);
6793b3a8eb9SGleb Smirnoff 	if (nzero != NULL)
6803b3a8eb9SGleb Smirnoff 		*nzero = xzero;
6813b3a8eb9SGleb Smirnoff 	return (0);
6823b3a8eb9SGleb Smirnoff _bad:
6833b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_FEEDBACK)
6843b3a8eb9SGleb Smirnoff 		pfr_reset_feedback(addr, size);
6853b3a8eb9SGleb Smirnoff 	return (rv);
6863b3a8eb9SGleb Smirnoff }
6873b3a8eb9SGleb Smirnoff 
6883b3a8eb9SGleb Smirnoff static int
6893b3a8eb9SGleb Smirnoff pfr_validate_addr(struct pfr_addr *ad)
6903b3a8eb9SGleb Smirnoff {
6913b3a8eb9SGleb Smirnoff 	int i;
6923b3a8eb9SGleb Smirnoff 
6933b3a8eb9SGleb Smirnoff 	switch (ad->pfra_af) {
6943b3a8eb9SGleb Smirnoff #ifdef INET
6953b3a8eb9SGleb Smirnoff 	case AF_INET:
6963b3a8eb9SGleb Smirnoff 		if (ad->pfra_net > 32)
6973b3a8eb9SGleb Smirnoff 			return (-1);
6983b3a8eb9SGleb Smirnoff 		break;
6993b3a8eb9SGleb Smirnoff #endif /* INET */
7003b3a8eb9SGleb Smirnoff #ifdef INET6
7013b3a8eb9SGleb Smirnoff 	case AF_INET6:
7023b3a8eb9SGleb Smirnoff 		if (ad->pfra_net > 128)
7033b3a8eb9SGleb Smirnoff 			return (-1);
7043b3a8eb9SGleb Smirnoff 		break;
7053b3a8eb9SGleb Smirnoff #endif /* INET6 */
7063b3a8eb9SGleb Smirnoff 	default:
7073b3a8eb9SGleb Smirnoff 		return (-1);
7083b3a8eb9SGleb Smirnoff 	}
7093b3a8eb9SGleb Smirnoff 	if (ad->pfra_net < 128 &&
7103b3a8eb9SGleb Smirnoff 		(((caddr_t)ad)[ad->pfra_net/8] & (0xFF >> (ad->pfra_net%8))))
7113b3a8eb9SGleb Smirnoff 			return (-1);
7123b3a8eb9SGleb Smirnoff 	for (i = (ad->pfra_net+7)/8; i < sizeof(ad->pfra_u); i++)
7133b3a8eb9SGleb Smirnoff 		if (((caddr_t)ad)[i])
7143b3a8eb9SGleb Smirnoff 			return (-1);
7153b3a8eb9SGleb Smirnoff 	if (ad->pfra_not && ad->pfra_not != 1)
7163b3a8eb9SGleb Smirnoff 		return (-1);
7173b3a8eb9SGleb Smirnoff 	if (ad->pfra_fback)
7183b3a8eb9SGleb Smirnoff 		return (-1);
7193b3a8eb9SGleb Smirnoff 	return (0);
7203b3a8eb9SGleb Smirnoff }
7213b3a8eb9SGleb Smirnoff 
7223b3a8eb9SGleb Smirnoff static void
7233b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
7243b3a8eb9SGleb Smirnoff 	int *naddr, int sweep)
7253b3a8eb9SGleb Smirnoff {
7263b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
7273b3a8eb9SGleb Smirnoff 
7283b3a8eb9SGleb Smirnoff 	SLIST_INIT(workq);
7293b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
7303b3a8eb9SGleb Smirnoff 	w.pfrw_op = sweep ? PFRW_SWEEP : PFRW_ENQUEUE;
7313b3a8eb9SGleb Smirnoff 	w.pfrw_workq = workq;
7323b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_ip4 != NULL)
73361eee0e2SAlexander V. Chernikov 		if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh,
73461eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w))
7353b3a8eb9SGleb Smirnoff 			printf("pfr_enqueue_addrs: IPv4 walktree failed.\n");
7363b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_ip6 != NULL)
73761eee0e2SAlexander V. Chernikov 		if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh,
73861eee0e2SAlexander V. Chernikov 		    pfr_walktree, &w))
7393b3a8eb9SGleb Smirnoff 			printf("pfr_enqueue_addrs: IPv6 walktree failed.\n");
7403b3a8eb9SGleb Smirnoff 	if (naddr != NULL)
7413b3a8eb9SGleb Smirnoff 		*naddr = w.pfrw_cnt;
7423b3a8eb9SGleb Smirnoff }
7433b3a8eb9SGleb Smirnoff 
7443b3a8eb9SGleb Smirnoff static void
7453b3a8eb9SGleb Smirnoff pfr_mark_addrs(struct pfr_ktable *kt)
7463b3a8eb9SGleb Smirnoff {
7473b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
7483b3a8eb9SGleb Smirnoff 
7493b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
7503b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_MARK;
75161eee0e2SAlexander V. Chernikov 	if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w))
7523b3a8eb9SGleb Smirnoff 		printf("pfr_mark_addrs: IPv4 walktree failed.\n");
75361eee0e2SAlexander V. Chernikov 	if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w))
7543b3a8eb9SGleb Smirnoff 		printf("pfr_mark_addrs: IPv6 walktree failed.\n");
7553b3a8eb9SGleb Smirnoff }
7563b3a8eb9SGleb Smirnoff 
7573b3a8eb9SGleb Smirnoff static struct pfr_kentry *
7583b3a8eb9SGleb Smirnoff pfr_lookup_addr(struct pfr_ktable *kt, struct pfr_addr *ad, int exact)
7593b3a8eb9SGleb Smirnoff {
7603b3a8eb9SGleb Smirnoff 	union sockaddr_union	 sa, mask;
76161eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
7623b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke;
7633b3a8eb9SGleb Smirnoff 
76429bdd62cSGleb Smirnoff 	PF_RULES_ASSERT();
76529bdd62cSGleb Smirnoff 
7663b3a8eb9SGleb Smirnoff 	bzero(&sa, sizeof(sa));
7673b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET) {
7683b3a8eb9SGleb Smirnoff 		FILLIN_SIN(sa.sin, ad->pfra_ip4addr);
76961eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
7703b3a8eb9SGleb Smirnoff 	} else if ( ad->pfra_af == AF_INET6 ) {
7713b3a8eb9SGleb Smirnoff 		FILLIN_SIN6(sa.sin6, ad->pfra_ip6addr);
77261eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
7733b3a8eb9SGleb Smirnoff 	}
7743b3a8eb9SGleb Smirnoff 	if (ADDR_NETWORK(ad)) {
7753b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ad->pfra_af, ad->pfra_net);
7763b3a8eb9SGleb Smirnoff 		ke = (struct pfr_kentry *)rn_lookup(&sa, &mask, head);
7773b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
7783b3a8eb9SGleb Smirnoff 			ke = NULL;
7793b3a8eb9SGleb Smirnoff 	} else {
7803b3a8eb9SGleb Smirnoff 		ke = (struct pfr_kentry *)rn_match(&sa, head);
7813b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
7823b3a8eb9SGleb Smirnoff 			ke = NULL;
7833b3a8eb9SGleb Smirnoff 		if (exact && ke && KENTRY_NETWORK(ke))
7843b3a8eb9SGleb Smirnoff 			ke = NULL;
7853b3a8eb9SGleb Smirnoff 	}
7863b3a8eb9SGleb Smirnoff 	return (ke);
7873b3a8eb9SGleb Smirnoff }
7883b3a8eb9SGleb Smirnoff 
7893b3a8eb9SGleb Smirnoff static struct pfr_kentry *
79021121f9bSMark Johnston pfr_create_kentry(struct pfr_addr *ad, bool counters)
7913b3a8eb9SGleb Smirnoff {
7923b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke;
793c1be8399SMark Johnston 	counter_u64_t		 c;
7943b3a8eb9SGleb Smirnoff 
7953b3a8eb9SGleb Smirnoff 	ke = uma_zalloc(V_pfr_kentry_z, M_NOWAIT | M_ZERO);
7963b3a8eb9SGleb Smirnoff 	if (ke == NULL)
7973b3a8eb9SGleb Smirnoff 		return (NULL);
7983b3a8eb9SGleb Smirnoff 
7993b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET)
8003b3a8eb9SGleb Smirnoff 		FILLIN_SIN(ke->pfrke_sa.sin, ad->pfra_ip4addr);
8013b3a8eb9SGleb Smirnoff 	else if (ad->pfra_af == AF_INET6)
8023b3a8eb9SGleb Smirnoff 		FILLIN_SIN6(ke->pfrke_sa.sin6, ad->pfra_ip6addr);
8033b3a8eb9SGleb Smirnoff 	ke->pfrke_af = ad->pfra_af;
8043b3a8eb9SGleb Smirnoff 	ke->pfrke_net = ad->pfra_net;
8053b3a8eb9SGleb Smirnoff 	ke->pfrke_not = ad->pfra_not;
80621121f9bSMark Johnston 	ke->pfrke_counters.pfrkc_tzero = 0;
807c1be8399SMark Johnston 	if (counters) {
808c1be8399SMark Johnston 		c = uma_zalloc_pcpu(V_pfr_kentry_counter_z, M_NOWAIT | M_ZERO);
809c1be8399SMark Johnston 		if (c == NULL) {
81059048686SKristof Provost 			pfr_destroy_kentry(ke);
81159048686SKristof Provost 			return (NULL);
81259048686SKristof Provost 		}
813c1be8399SMark Johnston 		ke->pfrke_counters.pfrkc_counters = c;
81459048686SKristof Provost 	}
8153b3a8eb9SGleb Smirnoff 	return (ke);
8163b3a8eb9SGleb Smirnoff }
8173b3a8eb9SGleb Smirnoff 
8183b3a8eb9SGleb Smirnoff static void
8193b3a8eb9SGleb Smirnoff pfr_destroy_kentries(struct pfr_kentryworkq *workq)
8203b3a8eb9SGleb Smirnoff {
8213b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p, *q;
8223b3a8eb9SGleb Smirnoff 
8233b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p != NULL; p = q) {
8243b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrke_workq);
8253b3a8eb9SGleb Smirnoff 		pfr_destroy_kentry(p);
8263b3a8eb9SGleb Smirnoff 	}
8273b3a8eb9SGleb Smirnoff }
8283b3a8eb9SGleb Smirnoff 
8293b3a8eb9SGleb Smirnoff static void
830c1be8399SMark Johnston pfr_destroy_kentry(struct pfr_kentry *ke)
83159048686SKristof Provost {
83221121f9bSMark Johnston 	counter_u64_t c;
83321121f9bSMark Johnston 
834c1be8399SMark Johnston 	if ((c = ke->pfrke_counters.pfrkc_counters) != NULL)
835c1be8399SMark Johnston 		uma_zfree_pcpu(V_pfr_kentry_counter_z, c);
8363b3a8eb9SGleb Smirnoff 	uma_zfree(V_pfr_kentry_z, ke);
8373b3a8eb9SGleb Smirnoff }
8383b3a8eb9SGleb Smirnoff 
8393b3a8eb9SGleb Smirnoff static void
8403b3a8eb9SGleb Smirnoff pfr_insert_kentries(struct pfr_ktable *kt,
8413b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq, long tzero)
8423b3a8eb9SGleb Smirnoff {
8433b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8443b3a8eb9SGleb Smirnoff 	int			 rv, n = 0;
8453b3a8eb9SGleb Smirnoff 
8463b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
8473b3a8eb9SGleb Smirnoff 		rv = pfr_route_kentry(kt, p);
8483b3a8eb9SGleb Smirnoff 		if (rv) {
8493b3a8eb9SGleb Smirnoff 			printf("pfr_insert_kentries: cannot route entry "
8503b3a8eb9SGleb Smirnoff 			    "(code=%d).\n", rv);
8513b3a8eb9SGleb Smirnoff 			break;
8523b3a8eb9SGleb Smirnoff 		}
85359048686SKristof Provost 		p->pfrke_counters.pfrkc_tzero = tzero;
8543b3a8eb9SGleb Smirnoff 		n++;
8553b3a8eb9SGleb Smirnoff 	}
8563b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt += n;
8573b3a8eb9SGleb Smirnoff }
8583b3a8eb9SGleb Smirnoff 
8593b3a8eb9SGleb Smirnoff int
8603b3a8eb9SGleb Smirnoff pfr_insert_kentry(struct pfr_ktable *kt, struct pfr_addr *ad, long tzero)
8613b3a8eb9SGleb Smirnoff {
8623b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8633b3a8eb9SGleb Smirnoff 	int			 rv;
8643b3a8eb9SGleb Smirnoff 
8653b3a8eb9SGleb Smirnoff 	p = pfr_lookup_addr(kt, ad, 1);
8663b3a8eb9SGleb Smirnoff 	if (p != NULL)
8673b3a8eb9SGleb Smirnoff 		return (0);
86821121f9bSMark Johnston 	p = pfr_create_kentry(ad, (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
8693b3a8eb9SGleb Smirnoff 	if (p == NULL)
870e706fd3aSGleb Smirnoff 		return (ENOMEM);
8713b3a8eb9SGleb Smirnoff 
8723b3a8eb9SGleb Smirnoff 	rv = pfr_route_kentry(kt, p);
8733b3a8eb9SGleb Smirnoff 	if (rv)
8743b3a8eb9SGleb Smirnoff 		return (rv);
8753b3a8eb9SGleb Smirnoff 
87659048686SKristof Provost 	p->pfrke_counters.pfrkc_tzero = tzero;
8773b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt++;
8783b3a8eb9SGleb Smirnoff 
8793b3a8eb9SGleb Smirnoff 	return (0);
8803b3a8eb9SGleb Smirnoff }
8813b3a8eb9SGleb Smirnoff 
8823b3a8eb9SGleb Smirnoff static void
8833b3a8eb9SGleb Smirnoff pfr_remove_kentries(struct pfr_ktable *kt,
8843b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq)
8853b3a8eb9SGleb Smirnoff {
8863b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
8873b3a8eb9SGleb Smirnoff 	int			 n = 0;
8883b3a8eb9SGleb Smirnoff 
8893b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
8903b3a8eb9SGleb Smirnoff 		pfr_unroute_kentry(kt, p);
8913b3a8eb9SGleb Smirnoff 		n++;
8923b3a8eb9SGleb Smirnoff 	}
8933b3a8eb9SGleb Smirnoff 	kt->pfrkt_cnt -= n;
8943b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(workq);
8953b3a8eb9SGleb Smirnoff }
8963b3a8eb9SGleb Smirnoff 
8973b3a8eb9SGleb Smirnoff static void
8983b3a8eb9SGleb Smirnoff pfr_clean_node_mask(struct pfr_ktable *kt,
8993b3a8eb9SGleb Smirnoff     struct pfr_kentryworkq *workq)
9003b3a8eb9SGleb Smirnoff {
9013b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
9023b3a8eb9SGleb Smirnoff 
9033b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq)
9043b3a8eb9SGleb Smirnoff 		pfr_unroute_kentry(kt, p);
9053b3a8eb9SGleb Smirnoff }
9063b3a8eb9SGleb Smirnoff 
9073b3a8eb9SGleb Smirnoff static void
90821121f9bSMark Johnston pfr_clstats_kentries(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
90921121f9bSMark Johnston     long tzero, int negchange)
9103b3a8eb9SGleb Smirnoff {
9113b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
912c1be8399SMark Johnston 	int			 i;
9133b3a8eb9SGleb Smirnoff 
9143b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrke_workq) {
9153b3a8eb9SGleb Smirnoff 		if (negchange)
9163b3a8eb9SGleb Smirnoff 			p->pfrke_not = !p->pfrke_not;
91721121f9bSMark Johnston 		if ((kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0)
918c1be8399SMark Johnston 			for (i = 0; i < PFR_NUM_COUNTERS; i++)
919c1be8399SMark Johnston 				counter_u64_zero(
920c1be8399SMark Johnston 				    p->pfrke_counters.pfrkc_counters + i);
92159048686SKristof Provost 		p->pfrke_counters.pfrkc_tzero = tzero;
9223b3a8eb9SGleb Smirnoff 	}
9233b3a8eb9SGleb Smirnoff }
9243b3a8eb9SGleb Smirnoff 
9253b3a8eb9SGleb Smirnoff static void
9263b3a8eb9SGleb Smirnoff pfr_reset_feedback(struct pfr_addr *addr, int size)
9273b3a8eb9SGleb Smirnoff {
9283b3a8eb9SGleb Smirnoff 	struct pfr_addr	*ad;
9293b3a8eb9SGleb Smirnoff 	int		i;
9303b3a8eb9SGleb Smirnoff 
9313b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++)
9323b3a8eb9SGleb Smirnoff 		ad->pfra_fback = PFR_FB_NONE;
9333b3a8eb9SGleb Smirnoff }
9343b3a8eb9SGleb Smirnoff 
9353b3a8eb9SGleb Smirnoff static void
9363b3a8eb9SGleb Smirnoff pfr_prepare_network(union sockaddr_union *sa, int af, int net)
9373b3a8eb9SGleb Smirnoff {
9383b3a8eb9SGleb Smirnoff 	int	i;
9393b3a8eb9SGleb Smirnoff 
9403b3a8eb9SGleb Smirnoff 	bzero(sa, sizeof(*sa));
9413b3a8eb9SGleb Smirnoff 	if (af == AF_INET) {
9423b3a8eb9SGleb Smirnoff 		sa->sin.sin_len = sizeof(sa->sin);
9433b3a8eb9SGleb Smirnoff 		sa->sin.sin_family = AF_INET;
9443b3a8eb9SGleb Smirnoff 		sa->sin.sin_addr.s_addr = net ? htonl(-1 << (32-net)) : 0;
9453b3a8eb9SGleb Smirnoff 	} else if (af == AF_INET6) {
9463b3a8eb9SGleb Smirnoff 		sa->sin6.sin6_len = sizeof(sa->sin6);
9473b3a8eb9SGleb Smirnoff 		sa->sin6.sin6_family = AF_INET6;
9483b3a8eb9SGleb Smirnoff 		for (i = 0; i < 4; i++) {
9493b3a8eb9SGleb Smirnoff 			if (net <= 32) {
9503b3a8eb9SGleb Smirnoff 				sa->sin6.sin6_addr.s6_addr32[i] =
9513b3a8eb9SGleb Smirnoff 				    net ? htonl(-1 << (32-net)) : 0;
9523b3a8eb9SGleb Smirnoff 				break;
9533b3a8eb9SGleb Smirnoff 			}
9543b3a8eb9SGleb Smirnoff 			sa->sin6.sin6_addr.s6_addr32[i] = 0xFFFFFFFF;
9553b3a8eb9SGleb Smirnoff 			net -= 32;
9563b3a8eb9SGleb Smirnoff 		}
9573b3a8eb9SGleb Smirnoff 	}
9583b3a8eb9SGleb Smirnoff }
9593b3a8eb9SGleb Smirnoff 
9603b3a8eb9SGleb Smirnoff static int
9613b3a8eb9SGleb Smirnoff pfr_route_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
9623b3a8eb9SGleb Smirnoff {
9633b3a8eb9SGleb Smirnoff 	union sockaddr_union	 mask;
9643b3a8eb9SGleb Smirnoff 	struct radix_node	*rn;
96561eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
9663b3a8eb9SGleb Smirnoff 
96729bdd62cSGleb Smirnoff 	PF_RULES_WASSERT();
96829bdd62cSGleb Smirnoff 
9693b3a8eb9SGleb Smirnoff 	bzero(ke->pfrke_node, sizeof(ke->pfrke_node));
9703b3a8eb9SGleb Smirnoff 	if (ke->pfrke_af == AF_INET)
97161eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
9723b3a8eb9SGleb Smirnoff 	else if (ke->pfrke_af == AF_INET6)
97361eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
9743b3a8eb9SGleb Smirnoff 
9753b3a8eb9SGleb Smirnoff 	if (KENTRY_NETWORK(ke)) {
9763b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
9773b3a8eb9SGleb Smirnoff 		rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node);
9783b3a8eb9SGleb Smirnoff 	} else
9793b3a8eb9SGleb Smirnoff 		rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node);
9803b3a8eb9SGleb Smirnoff 
9813b3a8eb9SGleb Smirnoff 	return (rn == NULL ? -1 : 0);
9823b3a8eb9SGleb Smirnoff }
9833b3a8eb9SGleb Smirnoff 
9843b3a8eb9SGleb Smirnoff static int
9853b3a8eb9SGleb Smirnoff pfr_unroute_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
9863b3a8eb9SGleb Smirnoff {
9873b3a8eb9SGleb Smirnoff 	union sockaddr_union	 mask;
9883b3a8eb9SGleb Smirnoff 	struct radix_node	*rn;
98961eee0e2SAlexander V. Chernikov 	struct radix_head	*head = NULL;
9903b3a8eb9SGleb Smirnoff 
9913b3a8eb9SGleb Smirnoff 	if (ke->pfrke_af == AF_INET)
99261eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip4->rh;
9933b3a8eb9SGleb Smirnoff 	else if (ke->pfrke_af == AF_INET6)
99461eee0e2SAlexander V. Chernikov 		head = &kt->pfrkt_ip6->rh;
9953b3a8eb9SGleb Smirnoff 
9963b3a8eb9SGleb Smirnoff 	if (KENTRY_NETWORK(ke)) {
9973b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
9983b3a8eb9SGleb Smirnoff 		rn = rn_delete(&ke->pfrke_sa, &mask, head);
9993b3a8eb9SGleb Smirnoff 	} else
10003b3a8eb9SGleb Smirnoff 		rn = rn_delete(&ke->pfrke_sa, NULL, head);
10013b3a8eb9SGleb Smirnoff 
10023b3a8eb9SGleb Smirnoff 	if (rn == NULL) {
10033b3a8eb9SGleb Smirnoff 		printf("pfr_unroute_kentry: delete failed.\n");
10043b3a8eb9SGleb Smirnoff 		return (-1);
10053b3a8eb9SGleb Smirnoff 	}
10063b3a8eb9SGleb Smirnoff 	return (0);
10073b3a8eb9SGleb Smirnoff }
10083b3a8eb9SGleb Smirnoff 
10093b3a8eb9SGleb Smirnoff static void
101059048686SKristof Provost pfr_copyout_addr(struct pfr_addr *ad, const struct pfr_kentry *ke)
10113b3a8eb9SGleb Smirnoff {
10123b3a8eb9SGleb Smirnoff 	bzero(ad, sizeof(*ad));
10133b3a8eb9SGleb Smirnoff 	if (ke == NULL)
10143b3a8eb9SGleb Smirnoff 		return;
10153b3a8eb9SGleb Smirnoff 	ad->pfra_af = ke->pfrke_af;
10163b3a8eb9SGleb Smirnoff 	ad->pfra_net = ke->pfrke_net;
10173b3a8eb9SGleb Smirnoff 	ad->pfra_not = ke->pfrke_not;
10183b3a8eb9SGleb Smirnoff 	if (ad->pfra_af == AF_INET)
10193b3a8eb9SGleb Smirnoff 		ad->pfra_ip4addr = ke->pfrke_sa.sin.sin_addr;
10203b3a8eb9SGleb Smirnoff 	else if (ad->pfra_af == AF_INET6)
10213b3a8eb9SGleb Smirnoff 		ad->pfra_ip6addr = ke->pfrke_sa.sin6.sin6_addr;
10223b3a8eb9SGleb Smirnoff }
10233b3a8eb9SGleb Smirnoff 
102459048686SKristof Provost static void
102559048686SKristof Provost pfr_copyout_astats(struct pfr_astats *as, const struct pfr_kentry *ke,
102659048686SKristof Provost     const struct pfr_walktree *w)
102759048686SKristof Provost {
102859048686SKristof Provost 	int dir, op;
102959048686SKristof Provost 	const struct pfr_kcounters *kc = &ke->pfrke_counters;
103059048686SKristof Provost 
103159048686SKristof Provost 	pfr_copyout_addr(&as->pfras_a, ke);
103259048686SKristof Provost 	as->pfras_tzero = kc->pfrkc_tzero;
103359048686SKristof Provost 
103459048686SKristof Provost 	if (! (w->pfrw_flags & PFR_TFLAG_COUNTERS)) {
103559048686SKristof Provost 		bzero(as->pfras_packets, sizeof(as->pfras_packets));
103659048686SKristof Provost 		bzero(as->pfras_bytes, sizeof(as->pfras_bytes));
103759048686SKristof Provost 		as->pfras_a.pfra_fback = PFR_FB_NOCOUNT;
103859048686SKristof Provost 		return;
103959048686SKristof Provost 	}
104059048686SKristof Provost 
104159048686SKristof Provost 	for (dir = 0; dir < PFR_DIR_MAX; dir++) {
104259048686SKristof Provost 		for (op = 0; op < PFR_OP_ADDR_MAX; op ++) {
1043c1be8399SMark Johnston 			as->pfras_packets[dir][op] = counter_u64_fetch(
1044c1be8399SMark Johnston 			    pfr_kentry_counter(kc, dir, op, PFR_TYPE_PACKETS));
1045c1be8399SMark Johnston 			as->pfras_bytes[dir][op] = counter_u64_fetch(
1046c1be8399SMark Johnston 			    pfr_kentry_counter(kc, dir, op, PFR_TYPE_BYTES));
104759048686SKristof Provost 		}
104859048686SKristof Provost 	}
104959048686SKristof Provost }
105059048686SKristof Provost 
10513b3a8eb9SGleb Smirnoff static int
10523b3a8eb9SGleb Smirnoff pfr_walktree(struct radix_node *rn, void *arg)
10533b3a8eb9SGleb Smirnoff {
10543b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = (struct pfr_kentry *)rn;
10553b3a8eb9SGleb Smirnoff 	struct pfr_walktree	*w = arg;
10563b3a8eb9SGleb Smirnoff 
10573b3a8eb9SGleb Smirnoff 	switch (w->pfrw_op) {
10583b3a8eb9SGleb Smirnoff 	case PFRW_MARK:
10593b3a8eb9SGleb Smirnoff 		ke->pfrke_mark = 0;
10603b3a8eb9SGleb Smirnoff 		break;
10613b3a8eb9SGleb Smirnoff 	case PFRW_SWEEP:
10623b3a8eb9SGleb Smirnoff 		if (ke->pfrke_mark)
10633b3a8eb9SGleb Smirnoff 			break;
10643b3a8eb9SGleb Smirnoff 		/* FALLTHROUGH */
10653b3a8eb9SGleb Smirnoff 	case PFRW_ENQUEUE:
10663b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq);
10673b3a8eb9SGleb Smirnoff 		w->pfrw_cnt++;
10683b3a8eb9SGleb Smirnoff 		break;
10693b3a8eb9SGleb Smirnoff 	case PFRW_GET_ADDRS:
10703b3a8eb9SGleb Smirnoff 		if (w->pfrw_free-- > 0) {
10713b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(w->pfrw_addr, ke);
10723b3a8eb9SGleb Smirnoff 			w->pfrw_addr++;
10733b3a8eb9SGleb Smirnoff 		}
10743b3a8eb9SGleb Smirnoff 		break;
10753b3a8eb9SGleb Smirnoff 	case PFRW_GET_ASTATS:
10763b3a8eb9SGleb Smirnoff 		if (w->pfrw_free-- > 0) {
10773b3a8eb9SGleb Smirnoff 			struct pfr_astats as;
10783b3a8eb9SGleb Smirnoff 
107959048686SKristof Provost 			pfr_copyout_astats(&as, ke, w);
10803b3a8eb9SGleb Smirnoff 
10813b3a8eb9SGleb Smirnoff 			bcopy(&as, w->pfrw_astats, sizeof(as));
10823b3a8eb9SGleb Smirnoff 			w->pfrw_astats++;
10833b3a8eb9SGleb Smirnoff 		}
10843b3a8eb9SGleb Smirnoff 		break;
10853b3a8eb9SGleb Smirnoff 	case PFRW_POOL_GET:
10863b3a8eb9SGleb Smirnoff 		if (ke->pfrke_not)
10873b3a8eb9SGleb Smirnoff 			break; /* negative entries are ignored */
10883b3a8eb9SGleb Smirnoff 		if (!w->pfrw_cnt--) {
10893b3a8eb9SGleb Smirnoff 			w->pfrw_kentry = ke;
10903b3a8eb9SGleb Smirnoff 			return (1); /* finish search */
10913b3a8eb9SGleb Smirnoff 		}
10923b3a8eb9SGleb Smirnoff 		break;
10933b3a8eb9SGleb Smirnoff 	case PFRW_DYNADDR_UPDATE:
10943b3a8eb9SGleb Smirnoff 	    {
10953b3a8eb9SGleb Smirnoff 		union sockaddr_union	pfr_mask;
10963b3a8eb9SGleb Smirnoff 
10973b3a8eb9SGleb Smirnoff 		if (ke->pfrke_af == AF_INET) {
10983b3a8eb9SGleb Smirnoff 			if (w->pfrw_dyn->pfid_acnt4++ > 0)
10993b3a8eb9SGleb Smirnoff 				break;
11003b3a8eb9SGleb Smirnoff 			pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net);
11013b3a8eb9SGleb Smirnoff 			w->pfrw_dyn->pfid_addr4 = *SUNION2PF(&ke->pfrke_sa,
11023b3a8eb9SGleb Smirnoff 			    AF_INET);
11033b3a8eb9SGleb Smirnoff 			w->pfrw_dyn->pfid_mask4 = *SUNION2PF(&pfr_mask,
11043b3a8eb9SGleb Smirnoff 			    AF_INET);
11053b3a8eb9SGleb Smirnoff 		} else if (ke->pfrke_af == AF_INET6){
11063b3a8eb9SGleb Smirnoff 			if (w->pfrw_dyn->pfid_acnt6++ > 0)
11073b3a8eb9SGleb Smirnoff 				break;
11083b3a8eb9SGleb Smirnoff 			pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net);
11093b3a8eb9SGleb Smirnoff 			w->pfrw_dyn->pfid_addr6 = *SUNION2PF(&ke->pfrke_sa,
11103b3a8eb9SGleb Smirnoff 			    AF_INET6);
11113b3a8eb9SGleb Smirnoff 			w->pfrw_dyn->pfid_mask6 = *SUNION2PF(&pfr_mask,
11123b3a8eb9SGleb Smirnoff 			    AF_INET6);
11133b3a8eb9SGleb Smirnoff 		}
11143b3a8eb9SGleb Smirnoff 		break;
11153b3a8eb9SGleb Smirnoff 	    }
11163b3a8eb9SGleb Smirnoff 	}
11173b3a8eb9SGleb Smirnoff 	return (0);
11183b3a8eb9SGleb Smirnoff }
11193b3a8eb9SGleb Smirnoff 
11203b3a8eb9SGleb Smirnoff int
11213b3a8eb9SGleb Smirnoff pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
11223b3a8eb9SGleb Smirnoff {
11233b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
11243b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
11253b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
11263b3a8eb9SGleb Smirnoff 
11273b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ALLRSETS);
11283b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
11293b3a8eb9SGleb Smirnoff 		return (EINVAL);
11303b3a8eb9SGleb Smirnoff 	if (pfr_table_count(filter, flags) < 0)
11313b3a8eb9SGleb Smirnoff 		return (ENOENT);
11323b3a8eb9SGleb Smirnoff 
11333b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
11341e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
11353b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
11363b3a8eb9SGleb Smirnoff 			continue;
11373b3a8eb9SGleb Smirnoff 		if (!strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR))
11383b3a8eb9SGleb Smirnoff 			continue;
11393b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE))
11403b3a8eb9SGleb Smirnoff 			continue;
11413b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
11423b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
11433b3a8eb9SGleb Smirnoff 		xdel++;
11443b3a8eb9SGleb Smirnoff 	}
11453b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
11463b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
11473b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
11483b3a8eb9SGleb Smirnoff 		*ndel = xdel;
11493b3a8eb9SGleb Smirnoff 	return (0);
11503b3a8eb9SGleb Smirnoff }
11513b3a8eb9SGleb Smirnoff 
11523b3a8eb9SGleb Smirnoff int
11533b3a8eb9SGleb Smirnoff pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
11543b3a8eb9SGleb Smirnoff {
11553b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 addq, changeq;
11563b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, *r, key;
11573b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0;
11583b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
11593b3a8eb9SGleb Smirnoff 
11603b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
11613b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addq);
11623b3a8eb9SGleb Smirnoff 	SLIST_INIT(&changeq);
11633b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
11643b3a8eb9SGleb Smirnoff 		bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t));
11653b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK,
11663b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
11673b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
11683b3a8eb9SGleb Smirnoff 		key.pfrkt_flags |= PFR_TFLAG_ACTIVE;
11691e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
11703b3a8eb9SGleb Smirnoff 		if (p == NULL) {
11713b3a8eb9SGleb Smirnoff 			p = pfr_create_ktable(&key.pfrkt_t, tzero, 1);
11723b3a8eb9SGleb Smirnoff 			if (p == NULL)
11733b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
11743b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &addq, pfrkt_workq) {
1175b4b8fa33SKristof Provost 				if (!pfr_ktable_compare(p, q)) {
1176b4b8fa33SKristof Provost 					pfr_destroy_ktable(p, 0);
11773b3a8eb9SGleb Smirnoff 					goto _skip;
11783b3a8eb9SGleb Smirnoff 				}
1179b4b8fa33SKristof Provost 			}
11803b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&addq, p, pfrkt_workq);
11813b3a8eb9SGleb Smirnoff 			xadd++;
11823b3a8eb9SGleb Smirnoff 			if (!key.pfrkt_anchor[0])
11833b3a8eb9SGleb Smirnoff 				goto _skip;
11843b3a8eb9SGleb Smirnoff 
11853b3a8eb9SGleb Smirnoff 			/* find or create root table */
11863b3a8eb9SGleb Smirnoff 			bzero(key.pfrkt_anchor, sizeof(key.pfrkt_anchor));
11871e9e3741SMarko Zec 			r = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
11883b3a8eb9SGleb Smirnoff 			if (r != NULL) {
11893b3a8eb9SGleb Smirnoff 				p->pfrkt_root = r;
11903b3a8eb9SGleb Smirnoff 				goto _skip;
11913b3a8eb9SGleb Smirnoff 			}
11923b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &addq, pfrkt_workq) {
11933b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(&key, q)) {
11943b3a8eb9SGleb Smirnoff 					p->pfrkt_root = q;
11953b3a8eb9SGleb Smirnoff 					goto _skip;
11963b3a8eb9SGleb Smirnoff 				}
11973b3a8eb9SGleb Smirnoff 			}
11983b3a8eb9SGleb Smirnoff 			key.pfrkt_flags = 0;
11993b3a8eb9SGleb Smirnoff 			r = pfr_create_ktable(&key.pfrkt_t, 0, 1);
12003b3a8eb9SGleb Smirnoff 			if (r == NULL)
12013b3a8eb9SGleb Smirnoff 				senderr(ENOMEM);
12023b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&addq, r, pfrkt_workq);
12033b3a8eb9SGleb Smirnoff 			p->pfrkt_root = r;
12043b3a8eb9SGleb Smirnoff 		} else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
12053b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &changeq, pfrkt_workq)
12063b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(&key, q))
12073b3a8eb9SGleb Smirnoff 					goto _skip;
12083b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = (p->pfrkt_flags &
12093b3a8eb9SGleb Smirnoff 			    ~PFR_TFLAG_USRMASK) | key.pfrkt_flags;
12103b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq);
12113b3a8eb9SGleb Smirnoff 			xadd++;
12123b3a8eb9SGleb Smirnoff 		}
12133b3a8eb9SGleb Smirnoff _skip:
12143b3a8eb9SGleb Smirnoff 	;
12153b3a8eb9SGleb Smirnoff 	}
12163b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
12173b3a8eb9SGleb Smirnoff 		pfr_insert_ktables(&addq);
12183b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&changeq);
12193b3a8eb9SGleb Smirnoff 	} else
12203b3a8eb9SGleb Smirnoff 		 pfr_destroy_ktables(&addq, 0);
12213b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
12223b3a8eb9SGleb Smirnoff 		*nadd = xadd;
12233b3a8eb9SGleb Smirnoff 	return (0);
12243b3a8eb9SGleb Smirnoff _bad:
12253b3a8eb9SGleb Smirnoff 	pfr_destroy_ktables(&addq, 0);
12263b3a8eb9SGleb Smirnoff 	return (rv);
12273b3a8eb9SGleb Smirnoff }
12283b3a8eb9SGleb Smirnoff 
12293b3a8eb9SGleb Smirnoff int
12303b3a8eb9SGleb Smirnoff pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags)
12313b3a8eb9SGleb Smirnoff {
12323b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
12333b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, key;
12343b3a8eb9SGleb Smirnoff 	int			 i, xdel = 0;
12353b3a8eb9SGleb Smirnoff 
12363b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
12373b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
12383b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
12393b3a8eb9SGleb Smirnoff 		bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t));
12403b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0,
12413b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
12423b3a8eb9SGleb Smirnoff 			return (EINVAL);
12431e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
12443b3a8eb9SGleb Smirnoff 		if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
12453b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &workq, pfrkt_workq)
12463b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(p, q))
12473b3a8eb9SGleb Smirnoff 					goto _skip;
12483b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
12493b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
12503b3a8eb9SGleb Smirnoff 			xdel++;
12513b3a8eb9SGleb Smirnoff 		}
12523b3a8eb9SGleb Smirnoff _skip:
12533b3a8eb9SGleb Smirnoff 	;
12543b3a8eb9SGleb Smirnoff 	}
12553b3a8eb9SGleb Smirnoff 
12563b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
12573b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
12583b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
12593b3a8eb9SGleb Smirnoff 		*ndel = xdel;
12603b3a8eb9SGleb Smirnoff 	return (0);
12613b3a8eb9SGleb Smirnoff }
12623b3a8eb9SGleb Smirnoff 
12633b3a8eb9SGleb Smirnoff int
12643b3a8eb9SGleb Smirnoff pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
12653b3a8eb9SGleb Smirnoff 	int flags)
12663b3a8eb9SGleb Smirnoff {
12673b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
12683b3a8eb9SGleb Smirnoff 	int			 n, nn;
12693b3a8eb9SGleb Smirnoff 
12703b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
12713b3a8eb9SGleb Smirnoff 
12723b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
12733b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
12743b3a8eb9SGleb Smirnoff 		return (EINVAL);
12753b3a8eb9SGleb Smirnoff 	n = nn = pfr_table_count(filter, flags);
12763b3a8eb9SGleb Smirnoff 	if (n < 0)
12773b3a8eb9SGleb Smirnoff 		return (ENOENT);
12783b3a8eb9SGleb Smirnoff 	if (n > *size) {
12793b3a8eb9SGleb Smirnoff 		*size = n;
12803b3a8eb9SGleb Smirnoff 		return (0);
12813b3a8eb9SGleb Smirnoff 	}
12821e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
12833b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
12843b3a8eb9SGleb Smirnoff 			continue;
12853b3a8eb9SGleb Smirnoff 		if (n-- <= 0)
12863b3a8eb9SGleb Smirnoff 			continue;
12873b3a8eb9SGleb Smirnoff 		bcopy(&p->pfrkt_t, tbl++, sizeof(*tbl));
12883b3a8eb9SGleb Smirnoff 	}
12893b3a8eb9SGleb Smirnoff 
12903b3a8eb9SGleb Smirnoff 	KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n));
12913b3a8eb9SGleb Smirnoff 
12923b3a8eb9SGleb Smirnoff 	*size = nn;
12933b3a8eb9SGleb Smirnoff 	return (0);
12943b3a8eb9SGleb Smirnoff }
12953b3a8eb9SGleb Smirnoff 
12963b3a8eb9SGleb Smirnoff int
12973b3a8eb9SGleb Smirnoff pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size,
12983b3a8eb9SGleb Smirnoff 	int flags)
12993b3a8eb9SGleb Smirnoff {
13003b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
13013b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
13023b3a8eb9SGleb Smirnoff 	int			 n, nn;
13033b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
130459048686SKristof Provost 	int			 pfr_dir, pfr_op;
13053b3a8eb9SGleb Smirnoff 
13063b3a8eb9SGleb Smirnoff 	/* XXX PFR_FLAG_CLSTATS disabled */
13073b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
13083b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(filter->pfrt_anchor))
13093b3a8eb9SGleb Smirnoff 		return (EINVAL);
13103b3a8eb9SGleb Smirnoff 	n = nn = pfr_table_count(filter, flags);
13113b3a8eb9SGleb Smirnoff 	if (n < 0)
13123b3a8eb9SGleb Smirnoff 		return (ENOENT);
13133b3a8eb9SGleb Smirnoff 	if (n > *size) {
13143b3a8eb9SGleb Smirnoff 		*size = n;
13153b3a8eb9SGleb Smirnoff 		return (0);
13163b3a8eb9SGleb Smirnoff 	}
13173b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
13181e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
13193b3a8eb9SGleb Smirnoff 		if (pfr_skip_table(filter, p, flags))
13203b3a8eb9SGleb Smirnoff 			continue;
13213b3a8eb9SGleb Smirnoff 		if (n-- <= 0)
13223b3a8eb9SGleb Smirnoff 			continue;
132359048686SKristof Provost 		bcopy(&p->pfrkt_kts.pfrts_t, &tbl->pfrts_t,
132459048686SKristof Provost 		    sizeof(struct pfr_table));
132559048686SKristof Provost 		for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
132659048686SKristof Provost 			for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
132759048686SKristof Provost 				tbl->pfrts_packets[pfr_dir][pfr_op] =
132859048686SKristof Provost 				    counter_u64_fetch(
132959048686SKristof Provost 					p->pfrkt_packets[pfr_dir][pfr_op]);
133059048686SKristof Provost 				tbl->pfrts_bytes[pfr_dir][pfr_op] =
133159048686SKristof Provost 				    counter_u64_fetch(
133259048686SKristof Provost 					p->pfrkt_bytes[pfr_dir][pfr_op]);
133359048686SKristof Provost 			}
133459048686SKristof Provost 		}
133559048686SKristof Provost 		tbl->pfrts_match = counter_u64_fetch(p->pfrkt_match);
133659048686SKristof Provost 		tbl->pfrts_nomatch = counter_u64_fetch(p->pfrkt_nomatch);
133759048686SKristof Provost 		tbl->pfrts_tzero = p->pfrkt_tzero;
133859048686SKristof Provost 		tbl->pfrts_cnt = p->pfrkt_cnt;
133959048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_REFCNT_MAX; pfr_op++)
134059048686SKristof Provost 			tbl->pfrts_refcnt[pfr_op] = p->pfrkt_refcnt[pfr_op];
134159048686SKristof Provost 		tbl++;
13423b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
13433b3a8eb9SGleb Smirnoff 	}
13443b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_CLSTATS)
13453b3a8eb9SGleb Smirnoff 		pfr_clstats_ktables(&workq, tzero,
13463b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_ADDRSTOO);
13473b3a8eb9SGleb Smirnoff 
13483b3a8eb9SGleb Smirnoff 	KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n));
13493b3a8eb9SGleb Smirnoff 
13503b3a8eb9SGleb Smirnoff 	*size = nn;
13513b3a8eb9SGleb Smirnoff 	return (0);
13523b3a8eb9SGleb Smirnoff }
13533b3a8eb9SGleb Smirnoff 
13543b3a8eb9SGleb Smirnoff int
13553b3a8eb9SGleb Smirnoff pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
13563b3a8eb9SGleb Smirnoff {
13573b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
13583b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, key;
13593b3a8eb9SGleb Smirnoff 	int			 i, xzero = 0;
13603b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
13613b3a8eb9SGleb Smirnoff 
13623b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
13633b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
13643b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
13653b3a8eb9SGleb Smirnoff 		bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t));
13663b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0, 0))
13673b3a8eb9SGleb Smirnoff 			return (EINVAL);
13681e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
13693b3a8eb9SGleb Smirnoff 		if (p != NULL) {
13703b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
13713b3a8eb9SGleb Smirnoff 			xzero++;
13723b3a8eb9SGleb Smirnoff 		}
13733b3a8eb9SGleb Smirnoff 	}
13743b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
13753b3a8eb9SGleb Smirnoff 		pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO);
13763b3a8eb9SGleb Smirnoff 	if (nzero != NULL)
13773b3a8eb9SGleb Smirnoff 		*nzero = xzero;
13783b3a8eb9SGleb Smirnoff 	return (0);
13793b3a8eb9SGleb Smirnoff }
13803b3a8eb9SGleb Smirnoff 
13813b3a8eb9SGleb Smirnoff int
13823b3a8eb9SGleb Smirnoff pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag,
13833b3a8eb9SGleb Smirnoff 	int *nchange, int *ndel, int flags)
13843b3a8eb9SGleb Smirnoff {
13853b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
13863b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q, key;
13873b3a8eb9SGleb Smirnoff 	int			 i, xchange = 0, xdel = 0;
13883b3a8eb9SGleb Smirnoff 
13893b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
13903b3a8eb9SGleb Smirnoff 	if ((setflag & ~PFR_TFLAG_USRMASK) ||
13913b3a8eb9SGleb Smirnoff 	    (clrflag & ~PFR_TFLAG_USRMASK) ||
13923b3a8eb9SGleb Smirnoff 	    (setflag & clrflag))
13933b3a8eb9SGleb Smirnoff 		return (EINVAL);
13943b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
13953b3a8eb9SGleb Smirnoff 	for (i = 0; i < size; i++) {
13963b3a8eb9SGleb Smirnoff 		bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t));
13973b3a8eb9SGleb Smirnoff 		if (pfr_validate_table(&key.pfrkt_t, 0,
13983b3a8eb9SGleb Smirnoff 		    flags & PFR_FLAG_USERIOCTL))
13993b3a8eb9SGleb Smirnoff 			return (EINVAL);
14001e9e3741SMarko Zec 		p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
14013b3a8eb9SGleb Smirnoff 		if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
14023b3a8eb9SGleb Smirnoff 			p->pfrkt_nflags = (p->pfrkt_flags | setflag) &
14033b3a8eb9SGleb Smirnoff 			    ~clrflag;
14043b3a8eb9SGleb Smirnoff 			if (p->pfrkt_nflags == p->pfrkt_flags)
14053b3a8eb9SGleb Smirnoff 				goto _skip;
14063b3a8eb9SGleb Smirnoff 			SLIST_FOREACH(q, &workq, pfrkt_workq)
14073b3a8eb9SGleb Smirnoff 				if (!pfr_ktable_compare(p, q))
14083b3a8eb9SGleb Smirnoff 					goto _skip;
14093b3a8eb9SGleb Smirnoff 			SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
14103b3a8eb9SGleb Smirnoff 			if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) &&
14113b3a8eb9SGleb Smirnoff 			    (clrflag & PFR_TFLAG_PERSIST) &&
14123b3a8eb9SGleb Smirnoff 			    !(p->pfrkt_flags & PFR_TFLAG_REFERENCED))
14133b3a8eb9SGleb Smirnoff 				xdel++;
14143b3a8eb9SGleb Smirnoff 			else
14153b3a8eb9SGleb Smirnoff 				xchange++;
14163b3a8eb9SGleb Smirnoff 		}
14173b3a8eb9SGleb Smirnoff _skip:
14183b3a8eb9SGleb Smirnoff 	;
14193b3a8eb9SGleb Smirnoff 	}
14203b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY))
14213b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
14223b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
14233b3a8eb9SGleb Smirnoff 		*nchange = xchange;
14243b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
14253b3a8eb9SGleb Smirnoff 		*ndel = xdel;
14263b3a8eb9SGleb Smirnoff 	return (0);
14273b3a8eb9SGleb Smirnoff }
14283b3a8eb9SGleb Smirnoff 
14293b3a8eb9SGleb Smirnoff int
14303b3a8eb9SGleb Smirnoff pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags)
14313b3a8eb9SGleb Smirnoff {
14323b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
14333b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
1434*e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
14353b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
14363b3a8eb9SGleb Smirnoff 
14373b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1438*e86bddeaSKristof Provost 	rs = pf_find_or_create_kruleset(trs->pfrt_anchor);
14393b3a8eb9SGleb Smirnoff 	if (rs == NULL)
14403b3a8eb9SGleb Smirnoff 		return (ENOMEM);
14413b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
14421e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
14433b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
14443b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
14453b3a8eb9SGleb Smirnoff 			continue;
14463b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
14473b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
14483b3a8eb9SGleb Smirnoff 		xdel++;
14493b3a8eb9SGleb Smirnoff 	}
14503b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
14513b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
14523b3a8eb9SGleb Smirnoff 		if (ticket != NULL)
14533b3a8eb9SGleb Smirnoff 			*ticket = ++rs->tticket;
14543b3a8eb9SGleb Smirnoff 		rs->topen = 1;
14553b3a8eb9SGleb Smirnoff 	} else
1456*e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
14573b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
14583b3a8eb9SGleb Smirnoff 		*ndel = xdel;
14593b3a8eb9SGleb Smirnoff 	return (0);
14603b3a8eb9SGleb Smirnoff }
14613b3a8eb9SGleb Smirnoff 
14623b3a8eb9SGleb Smirnoff int
14633b3a8eb9SGleb Smirnoff pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
14643b3a8eb9SGleb Smirnoff     int *nadd, int *naddr, u_int32_t ticket, int flags)
14653b3a8eb9SGleb Smirnoff {
14663b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 tableq;
14673b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
14683b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *rt, *shadow, key;
14693b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*p;
14703b3a8eb9SGleb Smirnoff 	struct pfr_addr		*ad;
1471*e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
14723b3a8eb9SGleb Smirnoff 	int			 i, rv, xadd = 0, xaddr = 0;
14733b3a8eb9SGleb Smirnoff 
14743b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
14753b3a8eb9SGleb Smirnoff 
14763b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
14773b3a8eb9SGleb Smirnoff 	if (size && !(flags & PFR_FLAG_ADDRSTOO))
14783b3a8eb9SGleb Smirnoff 		return (EINVAL);
14793b3a8eb9SGleb Smirnoff 	if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK,
14803b3a8eb9SGleb Smirnoff 	    flags & PFR_FLAG_USERIOCTL))
14813b3a8eb9SGleb Smirnoff 		return (EINVAL);
1482*e86bddeaSKristof Provost 	rs = pf_find_kruleset(tbl->pfrt_anchor);
14833b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
14843b3a8eb9SGleb Smirnoff 		return (EBUSY);
14853b3a8eb9SGleb Smirnoff 	tbl->pfrt_flags |= PFR_TFLAG_INACTIVE;
14863b3a8eb9SGleb Smirnoff 	SLIST_INIT(&tableq);
14871e9e3741SMarko Zec 	kt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, (struct pfr_ktable *)tbl);
14883b3a8eb9SGleb Smirnoff 	if (kt == NULL) {
14893b3a8eb9SGleb Smirnoff 		kt = pfr_create_ktable(tbl, 0, 1);
14903b3a8eb9SGleb Smirnoff 		if (kt == NULL)
14913b3a8eb9SGleb Smirnoff 			return (ENOMEM);
14923b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq);
14933b3a8eb9SGleb Smirnoff 		xadd++;
14943b3a8eb9SGleb Smirnoff 		if (!tbl->pfrt_anchor[0])
14953b3a8eb9SGleb Smirnoff 			goto _skip;
14963b3a8eb9SGleb Smirnoff 
14973b3a8eb9SGleb Smirnoff 		/* find or create root table */
14983b3a8eb9SGleb Smirnoff 		bzero(&key, sizeof(key));
14993b3a8eb9SGleb Smirnoff 		strlcpy(key.pfrkt_name, tbl->pfrt_name, sizeof(key.pfrkt_name));
15001e9e3741SMarko Zec 		rt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key);
15013b3a8eb9SGleb Smirnoff 		if (rt != NULL) {
15023b3a8eb9SGleb Smirnoff 			kt->pfrkt_root = rt;
15033b3a8eb9SGleb Smirnoff 			goto _skip;
15043b3a8eb9SGleb Smirnoff 		}
15053b3a8eb9SGleb Smirnoff 		rt = pfr_create_ktable(&key.pfrkt_t, 0, 1);
15063b3a8eb9SGleb Smirnoff 		if (rt == NULL) {
15073b3a8eb9SGleb Smirnoff 			pfr_destroy_ktables(&tableq, 0);
15083b3a8eb9SGleb Smirnoff 			return (ENOMEM);
15093b3a8eb9SGleb Smirnoff 		}
15103b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq);
15113b3a8eb9SGleb Smirnoff 		kt->pfrkt_root = rt;
15123b3a8eb9SGleb Smirnoff 	} else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE))
15133b3a8eb9SGleb Smirnoff 		xadd++;
15143b3a8eb9SGleb Smirnoff _skip:
15153b3a8eb9SGleb Smirnoff 	shadow = pfr_create_ktable(tbl, 0, 0);
15163b3a8eb9SGleb Smirnoff 	if (shadow == NULL) {
15173b3a8eb9SGleb Smirnoff 		pfr_destroy_ktables(&tableq, 0);
15183b3a8eb9SGleb Smirnoff 		return (ENOMEM);
15193b3a8eb9SGleb Smirnoff 	}
15203b3a8eb9SGleb Smirnoff 	SLIST_INIT(&addrq);
15213b3a8eb9SGleb Smirnoff 	for (i = 0, ad = addr; i < size; i++, ad++) {
15223b3a8eb9SGleb Smirnoff 		if (pfr_validate_addr(ad))
15233b3a8eb9SGleb Smirnoff 			senderr(EINVAL);
15243b3a8eb9SGleb Smirnoff 		if (pfr_lookup_addr(shadow, ad, 1) != NULL)
15253b3a8eb9SGleb Smirnoff 			continue;
152621121f9bSMark Johnston 		p = pfr_create_kentry(ad,
152721121f9bSMark Johnston 		    (shadow->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0);
15283b3a8eb9SGleb Smirnoff 		if (p == NULL)
15293b3a8eb9SGleb Smirnoff 			senderr(ENOMEM);
15303b3a8eb9SGleb Smirnoff 		if (pfr_route_kentry(shadow, p)) {
15313b3a8eb9SGleb Smirnoff 			pfr_destroy_kentry(p);
15323b3a8eb9SGleb Smirnoff 			continue;
15333b3a8eb9SGleb Smirnoff 		}
15343b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&addrq, p, pfrke_workq);
15353b3a8eb9SGleb Smirnoff 		xaddr++;
15363b3a8eb9SGleb Smirnoff 	}
15373b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
15383b3a8eb9SGleb Smirnoff 		if (kt->pfrkt_shadow != NULL)
15393b3a8eb9SGleb Smirnoff 			pfr_destroy_ktable(kt->pfrkt_shadow, 1);
15403b3a8eb9SGleb Smirnoff 		kt->pfrkt_flags |= PFR_TFLAG_INACTIVE;
15413b3a8eb9SGleb Smirnoff 		pfr_insert_ktables(&tableq);
15423b3a8eb9SGleb Smirnoff 		shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ?
15433b3a8eb9SGleb Smirnoff 		    xaddr : NO_ADDRESSES;
15443b3a8eb9SGleb Smirnoff 		kt->pfrkt_shadow = shadow;
15453b3a8eb9SGleb Smirnoff 	} else {
15463b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(shadow, &addrq);
15473b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(shadow, 0);
15483b3a8eb9SGleb Smirnoff 		pfr_destroy_ktables(&tableq, 0);
15493b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addrq);
15503b3a8eb9SGleb Smirnoff 	}
15513b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
15523b3a8eb9SGleb Smirnoff 		*nadd = xadd;
15533b3a8eb9SGleb Smirnoff 	if (naddr != NULL)
15543b3a8eb9SGleb Smirnoff 		*naddr = xaddr;
15553b3a8eb9SGleb Smirnoff 	return (0);
15563b3a8eb9SGleb Smirnoff _bad:
15573b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(shadow, 0);
15583b3a8eb9SGleb Smirnoff 	pfr_destroy_ktables(&tableq, 0);
15593b3a8eb9SGleb Smirnoff 	pfr_destroy_kentries(&addrq);
15603b3a8eb9SGleb Smirnoff 	return (rv);
15613b3a8eb9SGleb Smirnoff }
15623b3a8eb9SGleb Smirnoff 
15633b3a8eb9SGleb Smirnoff int
15643b3a8eb9SGleb Smirnoff pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags)
15653b3a8eb9SGleb Smirnoff {
15663b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
15673b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
1568*e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
15693b3a8eb9SGleb Smirnoff 	int			 xdel = 0;
15703b3a8eb9SGleb Smirnoff 
15713b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
15723b3a8eb9SGleb Smirnoff 
15733b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1574*e86bddeaSKristof Provost 	rs = pf_find_kruleset(trs->pfrt_anchor);
15753b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
15763b3a8eb9SGleb Smirnoff 		return (0);
15773b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
15781e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
15793b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
15803b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
15813b3a8eb9SGleb Smirnoff 			continue;
15823b3a8eb9SGleb Smirnoff 		p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
15833b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
15843b3a8eb9SGleb Smirnoff 		xdel++;
15853b3a8eb9SGleb Smirnoff 	}
15863b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
15873b3a8eb9SGleb Smirnoff 		pfr_setflags_ktables(&workq);
15883b3a8eb9SGleb Smirnoff 		rs->topen = 0;
1589*e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
15903b3a8eb9SGleb Smirnoff 	}
15913b3a8eb9SGleb Smirnoff 	if (ndel != NULL)
15923b3a8eb9SGleb Smirnoff 		*ndel = xdel;
15933b3a8eb9SGleb Smirnoff 	return (0);
15943b3a8eb9SGleb Smirnoff }
15953b3a8eb9SGleb Smirnoff 
15963b3a8eb9SGleb Smirnoff int
15973b3a8eb9SGleb Smirnoff pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd,
15983b3a8eb9SGleb Smirnoff     int *nchange, int flags)
15993b3a8eb9SGleb Smirnoff {
16003b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
16013b3a8eb9SGleb Smirnoff 	struct pfr_ktableworkq	 workq;
1602*e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
16033b3a8eb9SGleb Smirnoff 	int			 xadd = 0, xchange = 0;
16043b3a8eb9SGleb Smirnoff 	long			 tzero = time_second;
16053b3a8eb9SGleb Smirnoff 
16063b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
16073b3a8eb9SGleb Smirnoff 
16083b3a8eb9SGleb Smirnoff 	ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1609*e86bddeaSKristof Provost 	rs = pf_find_kruleset(trs->pfrt_anchor);
16103b3a8eb9SGleb Smirnoff 	if (rs == NULL || !rs->topen || ticket != rs->tticket)
16113b3a8eb9SGleb Smirnoff 		return (EBUSY);
16123b3a8eb9SGleb Smirnoff 
16133b3a8eb9SGleb Smirnoff 	SLIST_INIT(&workq);
16141e9e3741SMarko Zec 	RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) {
16153b3a8eb9SGleb Smirnoff 		if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
16163b3a8eb9SGleb Smirnoff 		    pfr_skip_table(trs, p, 0))
16173b3a8eb9SGleb Smirnoff 			continue;
16183b3a8eb9SGleb Smirnoff 		SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
16193b3a8eb9SGleb Smirnoff 		if (p->pfrkt_flags & PFR_TFLAG_ACTIVE)
16203b3a8eb9SGleb Smirnoff 			xchange++;
16213b3a8eb9SGleb Smirnoff 		else
16223b3a8eb9SGleb Smirnoff 			xadd++;
16233b3a8eb9SGleb Smirnoff 	}
16243b3a8eb9SGleb Smirnoff 
16253b3a8eb9SGleb Smirnoff 	if (!(flags & PFR_FLAG_DUMMY)) {
16263b3a8eb9SGleb Smirnoff 		for (p = SLIST_FIRST(&workq); p != NULL; p = q) {
16273b3a8eb9SGleb Smirnoff 			q = SLIST_NEXT(p, pfrkt_workq);
16283b3a8eb9SGleb Smirnoff 			pfr_commit_ktable(p, tzero);
16293b3a8eb9SGleb Smirnoff 		}
16303b3a8eb9SGleb Smirnoff 		rs->topen = 0;
1631*e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(rs);
16323b3a8eb9SGleb Smirnoff 	}
16333b3a8eb9SGleb Smirnoff 	if (nadd != NULL)
16343b3a8eb9SGleb Smirnoff 		*nadd = xadd;
16353b3a8eb9SGleb Smirnoff 	if (nchange != NULL)
16363b3a8eb9SGleb Smirnoff 		*nchange = xchange;
16373b3a8eb9SGleb Smirnoff 
16383b3a8eb9SGleb Smirnoff 	return (0);
16393b3a8eb9SGleb Smirnoff }
16403b3a8eb9SGleb Smirnoff 
16413b3a8eb9SGleb Smirnoff static void
16423b3a8eb9SGleb Smirnoff pfr_commit_ktable(struct pfr_ktable *kt, long tzero)
16433b3a8eb9SGleb Smirnoff {
1644e6aed06fSMark Johnston 	counter_u64_t		*pkc, *qkc;
16453b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*shadow = kt->pfrkt_shadow;
16463b3a8eb9SGleb Smirnoff 	int			 nflags;
16473b3a8eb9SGleb Smirnoff 
16483b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
16493b3a8eb9SGleb Smirnoff 
16503b3a8eb9SGleb Smirnoff 	if (shadow->pfrkt_cnt == NO_ADDRESSES) {
16513b3a8eb9SGleb Smirnoff 		if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
16523b3a8eb9SGleb Smirnoff 			pfr_clstats_ktable(kt, tzero, 1);
16533b3a8eb9SGleb Smirnoff 	} else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) {
16543b3a8eb9SGleb Smirnoff 		/* kt might contain addresses */
16553b3a8eb9SGleb Smirnoff 		struct pfr_kentryworkq	 addrq, addq, changeq, delq, garbageq;
16563b3a8eb9SGleb Smirnoff 		struct pfr_kentry	*p, *q, *next;
16573b3a8eb9SGleb Smirnoff 		struct pfr_addr		 ad;
16583b3a8eb9SGleb Smirnoff 
16593b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(shadow, &addrq, NULL, 0);
16603b3a8eb9SGleb Smirnoff 		pfr_mark_addrs(kt);
16613b3a8eb9SGleb Smirnoff 		SLIST_INIT(&addq);
16623b3a8eb9SGleb Smirnoff 		SLIST_INIT(&changeq);
16633b3a8eb9SGleb Smirnoff 		SLIST_INIT(&delq);
16643b3a8eb9SGleb Smirnoff 		SLIST_INIT(&garbageq);
16653b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(shadow, &addrq);
1666e6aed06fSMark Johnston 		SLIST_FOREACH_SAFE(p, &addrq, pfrke_workq, next) {
16673b3a8eb9SGleb Smirnoff 			pfr_copyout_addr(&ad, p);
16683b3a8eb9SGleb Smirnoff 			q = pfr_lookup_addr(kt, &ad, 1);
16693b3a8eb9SGleb Smirnoff 			if (q != NULL) {
16703b3a8eb9SGleb Smirnoff 				if (q->pfrke_not != p->pfrke_not)
16713b3a8eb9SGleb Smirnoff 					SLIST_INSERT_HEAD(&changeq, q,
16723b3a8eb9SGleb Smirnoff 					    pfrke_workq);
1673e6aed06fSMark Johnston 				pkc = &p->pfrke_counters.pfrkc_counters;
1674e6aed06fSMark Johnston 				qkc = &q->pfrke_counters.pfrkc_counters;
1675e6aed06fSMark Johnston 				if ((*pkc == NULL) != (*qkc == NULL))
1676e6aed06fSMark Johnston 					SWAP(counter_u64_t, *pkc, *qkc);
16773b3a8eb9SGleb Smirnoff 				q->pfrke_mark = 1;
16783b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq);
16793b3a8eb9SGleb Smirnoff 			} else {
168059048686SKristof Provost 				p->pfrke_counters.pfrkc_tzero = tzero;
16813b3a8eb9SGleb Smirnoff 				SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
16823b3a8eb9SGleb Smirnoff 			}
16833b3a8eb9SGleb Smirnoff 		}
16843b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY);
16853b3a8eb9SGleb Smirnoff 		pfr_insert_kentries(kt, &addq, tzero);
16863b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &delq);
168721121f9bSMark Johnston 		pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG);
16883b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&garbageq);
16893b3a8eb9SGleb Smirnoff 	} else {
16903b3a8eb9SGleb Smirnoff 		/* kt cannot contain addresses */
16913b3a8eb9SGleb Smirnoff 		SWAP(struct radix_node_head *, kt->pfrkt_ip4,
16923b3a8eb9SGleb Smirnoff 		    shadow->pfrkt_ip4);
16933b3a8eb9SGleb Smirnoff 		SWAP(struct radix_node_head *, kt->pfrkt_ip6,
16943b3a8eb9SGleb Smirnoff 		    shadow->pfrkt_ip6);
16953b3a8eb9SGleb Smirnoff 		SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt);
16963b3a8eb9SGleb Smirnoff 		pfr_clstats_ktable(kt, tzero, 1);
16973b3a8eb9SGleb Smirnoff 	}
16983b3a8eb9SGleb Smirnoff 	nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) |
16993b3a8eb9SGleb Smirnoff 	    (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE)
17003b3a8eb9SGleb Smirnoff 		& ~PFR_TFLAG_INACTIVE;
17013b3a8eb9SGleb Smirnoff 	pfr_destroy_ktable(shadow, 0);
17023b3a8eb9SGleb Smirnoff 	kt->pfrkt_shadow = NULL;
17033b3a8eb9SGleb Smirnoff 	pfr_setflags_ktable(kt, nflags);
17043b3a8eb9SGleb Smirnoff }
17053b3a8eb9SGleb Smirnoff 
17063b3a8eb9SGleb Smirnoff static int
17073b3a8eb9SGleb Smirnoff pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved)
17083b3a8eb9SGleb Smirnoff {
17093b3a8eb9SGleb Smirnoff 	int i;
17103b3a8eb9SGleb Smirnoff 
17113b3a8eb9SGleb Smirnoff 	if (!tbl->pfrt_name[0])
17123b3a8eb9SGleb Smirnoff 		return (-1);
17133b3a8eb9SGleb Smirnoff 	if (no_reserved && !strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR))
17143b3a8eb9SGleb Smirnoff 		 return (-1);
17153b3a8eb9SGleb Smirnoff 	if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1])
17163b3a8eb9SGleb Smirnoff 		return (-1);
17173b3a8eb9SGleb Smirnoff 	for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++)
17183b3a8eb9SGleb Smirnoff 		if (tbl->pfrt_name[i])
17193b3a8eb9SGleb Smirnoff 			return (-1);
17203b3a8eb9SGleb Smirnoff 	if (pfr_fix_anchor(tbl->pfrt_anchor))
17213b3a8eb9SGleb Smirnoff 		return (-1);
17223b3a8eb9SGleb Smirnoff 	if (tbl->pfrt_flags & ~allowedflags)
17233b3a8eb9SGleb Smirnoff 		return (-1);
17243b3a8eb9SGleb Smirnoff 	return (0);
17253b3a8eb9SGleb Smirnoff }
17263b3a8eb9SGleb Smirnoff 
17273b3a8eb9SGleb Smirnoff /*
17283b3a8eb9SGleb Smirnoff  * Rewrite anchors referenced by tables to remove slashes
17293b3a8eb9SGleb Smirnoff  * and check for validity.
17303b3a8eb9SGleb Smirnoff  */
17313b3a8eb9SGleb Smirnoff static int
17323b3a8eb9SGleb Smirnoff pfr_fix_anchor(char *anchor)
17333b3a8eb9SGleb Smirnoff {
17343b3a8eb9SGleb Smirnoff 	size_t siz = MAXPATHLEN;
17353b3a8eb9SGleb Smirnoff 	int i;
17363b3a8eb9SGleb Smirnoff 
17373b3a8eb9SGleb Smirnoff 	if (anchor[0] == '/') {
17383b3a8eb9SGleb Smirnoff 		char *path;
17393b3a8eb9SGleb Smirnoff 		int off;
17403b3a8eb9SGleb Smirnoff 
17413b3a8eb9SGleb Smirnoff 		path = anchor;
17423b3a8eb9SGleb Smirnoff 		off = 1;
17433b3a8eb9SGleb Smirnoff 		while (*++path == '/')
17443b3a8eb9SGleb Smirnoff 			off++;
17453b3a8eb9SGleb Smirnoff 		bcopy(path, anchor, siz - off);
17463b3a8eb9SGleb Smirnoff 		memset(anchor + siz - off, 0, off);
17473b3a8eb9SGleb Smirnoff 	}
17483b3a8eb9SGleb Smirnoff 	if (anchor[siz - 1])
17493b3a8eb9SGleb Smirnoff 		return (-1);
17503b3a8eb9SGleb Smirnoff 	for (i = strlen(anchor); i < siz; i++)
17513b3a8eb9SGleb Smirnoff 		if (anchor[i])
17523b3a8eb9SGleb Smirnoff 			return (-1);
17533b3a8eb9SGleb Smirnoff 	return (0);
17543b3a8eb9SGleb Smirnoff }
17553b3a8eb9SGleb Smirnoff 
1756adfe2f6aSKristof Provost int
17573b3a8eb9SGleb Smirnoff pfr_table_count(struct pfr_table *filter, int flags)
17583b3a8eb9SGleb Smirnoff {
1759*e86bddeaSKristof Provost 	struct pf_kruleset *rs;
17603b3a8eb9SGleb Smirnoff 
17613b3a8eb9SGleb Smirnoff 	PF_RULES_ASSERT();
17623b3a8eb9SGleb Smirnoff 
17633b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_ALLRSETS)
17641e9e3741SMarko Zec 		return (V_pfr_ktable_cnt);
17653b3a8eb9SGleb Smirnoff 	if (filter->pfrt_anchor[0]) {
1766*e86bddeaSKristof Provost 		rs = pf_find_kruleset(filter->pfrt_anchor);
17673b3a8eb9SGleb Smirnoff 		return ((rs != NULL) ? rs->tables : -1);
17683b3a8eb9SGleb Smirnoff 	}
17693b3a8eb9SGleb Smirnoff 	return (pf_main_ruleset.tables);
17703b3a8eb9SGleb Smirnoff }
17713b3a8eb9SGleb Smirnoff 
17723b3a8eb9SGleb Smirnoff static int
17733b3a8eb9SGleb Smirnoff pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags)
17743b3a8eb9SGleb Smirnoff {
17753b3a8eb9SGleb Smirnoff 	if (flags & PFR_FLAG_ALLRSETS)
17763b3a8eb9SGleb Smirnoff 		return (0);
17773b3a8eb9SGleb Smirnoff 	if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor))
17783b3a8eb9SGleb Smirnoff 		return (1);
17793b3a8eb9SGleb Smirnoff 	return (0);
17803b3a8eb9SGleb Smirnoff }
17813b3a8eb9SGleb Smirnoff 
17823b3a8eb9SGleb Smirnoff static void
17833b3a8eb9SGleb Smirnoff pfr_insert_ktables(struct pfr_ktableworkq *workq)
17843b3a8eb9SGleb Smirnoff {
17853b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
17863b3a8eb9SGleb Smirnoff 
17873b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrkt_workq)
17883b3a8eb9SGleb Smirnoff 		pfr_insert_ktable(p);
17893b3a8eb9SGleb Smirnoff }
17903b3a8eb9SGleb Smirnoff 
17913b3a8eb9SGleb Smirnoff static void
17923b3a8eb9SGleb Smirnoff pfr_insert_ktable(struct pfr_ktable *kt)
17933b3a8eb9SGleb Smirnoff {
17943b3a8eb9SGleb Smirnoff 
17953b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
17963b3a8eb9SGleb Smirnoff 
17971e9e3741SMarko Zec 	RB_INSERT(pfr_ktablehead, &V_pfr_ktables, kt);
17981e9e3741SMarko Zec 	V_pfr_ktable_cnt++;
17993b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_root != NULL)
18003b3a8eb9SGleb Smirnoff 		if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++)
18013b3a8eb9SGleb Smirnoff 			pfr_setflags_ktable(kt->pfrkt_root,
18023b3a8eb9SGleb Smirnoff 			    kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR);
18033b3a8eb9SGleb Smirnoff }
18043b3a8eb9SGleb Smirnoff 
18053b3a8eb9SGleb Smirnoff static void
18063b3a8eb9SGleb Smirnoff pfr_setflags_ktables(struct pfr_ktableworkq *workq)
18073b3a8eb9SGleb Smirnoff {
18083b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
18093b3a8eb9SGleb Smirnoff 
18103b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p; p = q) {
18113b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrkt_workq);
18123b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(p, p->pfrkt_nflags);
18133b3a8eb9SGleb Smirnoff 	}
18143b3a8eb9SGleb Smirnoff }
18153b3a8eb9SGleb Smirnoff 
18163b3a8eb9SGleb Smirnoff static void
18173b3a8eb9SGleb Smirnoff pfr_setflags_ktable(struct pfr_ktable *kt, int newf)
18183b3a8eb9SGleb Smirnoff {
18193b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	addrq;
18203b3a8eb9SGleb Smirnoff 
18213b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
18223b3a8eb9SGleb Smirnoff 
18233b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_REFERENCED) &&
182487e4ca37SKristof Provost 	    !(newf & PFR_TFLAG_REFDANCHOR) &&
18253b3a8eb9SGleb Smirnoff 	    !(newf & PFR_TFLAG_PERSIST))
18263b3a8eb9SGleb Smirnoff 		newf &= ~PFR_TFLAG_ACTIVE;
18273b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_ACTIVE))
18283b3a8eb9SGleb Smirnoff 		newf &= ~PFR_TFLAG_USRMASK;
18293b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_SETMASK)) {
18301e9e3741SMarko Zec 		RB_REMOVE(pfr_ktablehead, &V_pfr_ktables, kt);
18313b3a8eb9SGleb Smirnoff 		if (kt->pfrkt_root != NULL)
18323b3a8eb9SGleb Smirnoff 			if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR])
18333b3a8eb9SGleb Smirnoff 				pfr_setflags_ktable(kt->pfrkt_root,
18343b3a8eb9SGleb Smirnoff 				    kt->pfrkt_root->pfrkt_flags &
18353b3a8eb9SGleb Smirnoff 					~PFR_TFLAG_REFDANCHOR);
18363b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt, 1);
18371e9e3741SMarko Zec 		V_pfr_ktable_cnt--;
18383b3a8eb9SGleb Smirnoff 		return;
18393b3a8eb9SGleb Smirnoff 	}
18403b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) {
18413b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
18423b3a8eb9SGleb Smirnoff 		pfr_remove_kentries(kt, &addrq);
18433b3a8eb9SGleb Smirnoff 	}
18443b3a8eb9SGleb Smirnoff 	if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) {
18453b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt->pfrkt_shadow, 1);
18463b3a8eb9SGleb Smirnoff 		kt->pfrkt_shadow = NULL;
18473b3a8eb9SGleb Smirnoff 	}
18483b3a8eb9SGleb Smirnoff 	kt->pfrkt_flags = newf;
18493b3a8eb9SGleb Smirnoff }
18503b3a8eb9SGleb Smirnoff 
18513b3a8eb9SGleb Smirnoff static void
18523b3a8eb9SGleb Smirnoff pfr_clstats_ktables(struct pfr_ktableworkq *workq, long tzero, int recurse)
18533b3a8eb9SGleb Smirnoff {
18543b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p;
18553b3a8eb9SGleb Smirnoff 
18563b3a8eb9SGleb Smirnoff 	SLIST_FOREACH(p, workq, pfrkt_workq)
18573b3a8eb9SGleb Smirnoff 		pfr_clstats_ktable(p, tzero, recurse);
18583b3a8eb9SGleb Smirnoff }
18593b3a8eb9SGleb Smirnoff 
18603b3a8eb9SGleb Smirnoff static void
18613b3a8eb9SGleb Smirnoff pfr_clstats_ktable(struct pfr_ktable *kt, long tzero, int recurse)
18623b3a8eb9SGleb Smirnoff {
18633b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
186459048686SKristof Provost 	int			 pfr_dir, pfr_op;
18653b3a8eb9SGleb Smirnoff 
18663b3a8eb9SGleb Smirnoff 	if (recurse) {
18673b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
186821121f9bSMark Johnston 		pfr_clstats_kentries(kt, &addrq, tzero, 0);
18693b3a8eb9SGleb Smirnoff 	}
187059048686SKristof Provost 	for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
187159048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
187259048686SKristof Provost 			counter_u64_zero(kt->pfrkt_packets[pfr_dir][pfr_op]);
187359048686SKristof Provost 			counter_u64_zero(kt->pfrkt_bytes[pfr_dir][pfr_op]);
187459048686SKristof Provost 		}
187559048686SKristof Provost 	}
187659048686SKristof Provost 	counter_u64_zero(kt->pfrkt_match);
187759048686SKristof Provost 	counter_u64_zero(kt->pfrkt_nomatch);
18783b3a8eb9SGleb Smirnoff 	kt->pfrkt_tzero = tzero;
18793b3a8eb9SGleb Smirnoff }
18803b3a8eb9SGleb Smirnoff 
18813b3a8eb9SGleb Smirnoff static struct pfr_ktable *
18823b3a8eb9SGleb Smirnoff pfr_create_ktable(struct pfr_table *tbl, long tzero, int attachruleset)
18833b3a8eb9SGleb Smirnoff {
18843b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt;
1885*e86bddeaSKristof Provost 	struct pf_kruleset	*rs;
188659048686SKristof Provost 	int			 pfr_dir, pfr_op;
18873b3a8eb9SGleb Smirnoff 
18883b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
18893b3a8eb9SGleb Smirnoff 
18903b3a8eb9SGleb Smirnoff 	kt = malloc(sizeof(*kt), M_PFTABLE, M_NOWAIT|M_ZERO);
18913b3a8eb9SGleb Smirnoff 	if (kt == NULL)
18923b3a8eb9SGleb Smirnoff 		return (NULL);
18933b3a8eb9SGleb Smirnoff 	kt->pfrkt_t = *tbl;
18943b3a8eb9SGleb Smirnoff 
18953b3a8eb9SGleb Smirnoff 	if (attachruleset) {
1896*e86bddeaSKristof Provost 		rs = pf_find_or_create_kruleset(tbl->pfrt_anchor);
18973b3a8eb9SGleb Smirnoff 		if (!rs) {
18983b3a8eb9SGleb Smirnoff 			pfr_destroy_ktable(kt, 0);
18993b3a8eb9SGleb Smirnoff 			return (NULL);
19003b3a8eb9SGleb Smirnoff 		}
19013b3a8eb9SGleb Smirnoff 		kt->pfrkt_rs = rs;
19023b3a8eb9SGleb Smirnoff 		rs->tables++;
19033b3a8eb9SGleb Smirnoff 	}
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 ++) {
190759048686SKristof Provost 			kt->pfrkt_packets[pfr_dir][pfr_op] =
190859048686SKristof Provost 			    counter_u64_alloc(M_NOWAIT);
190959048686SKristof Provost 			if (! kt->pfrkt_packets[pfr_dir][pfr_op]) {
191059048686SKristof Provost 				pfr_destroy_ktable(kt, 0);
191159048686SKristof Provost 				return (NULL);
191259048686SKristof Provost 			}
191359048686SKristof Provost 			kt->pfrkt_bytes[pfr_dir][pfr_op] =
191459048686SKristof Provost 			    counter_u64_alloc(M_NOWAIT);
191559048686SKristof Provost 			if (! kt->pfrkt_bytes[pfr_dir][pfr_op]) {
191659048686SKristof Provost 				pfr_destroy_ktable(kt, 0);
191759048686SKristof Provost 				return (NULL);
191859048686SKristof Provost 			}
191959048686SKristof Provost 		}
192059048686SKristof Provost 	}
192159048686SKristof Provost 	kt->pfrkt_match = counter_u64_alloc(M_NOWAIT);
192259048686SKristof Provost 	if (! kt->pfrkt_match) {
192359048686SKristof Provost 		pfr_destroy_ktable(kt, 0);
192459048686SKristof Provost 		return (NULL);
192559048686SKristof Provost 	}
192659048686SKristof Provost 
192759048686SKristof Provost 	kt->pfrkt_nomatch = counter_u64_alloc(M_NOWAIT);
192859048686SKristof Provost 	if (! kt->pfrkt_nomatch) {
192959048686SKristof Provost 		pfr_destroy_ktable(kt, 0);
193059048686SKristof Provost 		return (NULL);
193159048686SKristof Provost 	}
193259048686SKristof Provost 
19333b3a8eb9SGleb Smirnoff 	if (!rn_inithead((void **)&kt->pfrkt_ip4,
19343b3a8eb9SGleb Smirnoff 	    offsetof(struct sockaddr_in, sin_addr) * 8) ||
19353b3a8eb9SGleb Smirnoff 	    !rn_inithead((void **)&kt->pfrkt_ip6,
19363b3a8eb9SGleb Smirnoff 	    offsetof(struct sockaddr_in6, sin6_addr) * 8)) {
19373b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt, 0);
19383b3a8eb9SGleb Smirnoff 		return (NULL);
19393b3a8eb9SGleb Smirnoff 	}
19403b3a8eb9SGleb Smirnoff 	kt->pfrkt_tzero = tzero;
19413b3a8eb9SGleb Smirnoff 
19423b3a8eb9SGleb Smirnoff 	return (kt);
19433b3a8eb9SGleb Smirnoff }
19443b3a8eb9SGleb Smirnoff 
19453b3a8eb9SGleb Smirnoff static void
19463b3a8eb9SGleb Smirnoff pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr)
19473b3a8eb9SGleb Smirnoff {
19483b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*p, *q;
19493b3a8eb9SGleb Smirnoff 
19503b3a8eb9SGleb Smirnoff 	for (p = SLIST_FIRST(workq); p; p = q) {
19513b3a8eb9SGleb Smirnoff 		q = SLIST_NEXT(p, pfrkt_workq);
19523b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(p, flushaddr);
19533b3a8eb9SGleb Smirnoff 	}
19543b3a8eb9SGleb Smirnoff }
19553b3a8eb9SGleb Smirnoff 
19563b3a8eb9SGleb Smirnoff static void
19573b3a8eb9SGleb Smirnoff pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr)
19583b3a8eb9SGleb Smirnoff {
19593b3a8eb9SGleb Smirnoff 	struct pfr_kentryworkq	 addrq;
196059048686SKristof Provost 	int			 pfr_dir, pfr_op;
19613b3a8eb9SGleb Smirnoff 
19623b3a8eb9SGleb Smirnoff 	if (flushaddr) {
19633b3a8eb9SGleb Smirnoff 		pfr_enqueue_addrs(kt, &addrq, NULL, 0);
19643b3a8eb9SGleb Smirnoff 		pfr_clean_node_mask(kt, &addrq);
19653b3a8eb9SGleb Smirnoff 		pfr_destroy_kentries(&addrq);
19663b3a8eb9SGleb Smirnoff 	}
196731f0d081SAlexander V. Chernikov 	if (kt->pfrkt_ip4 != NULL)
1968495a22b5SGleb Smirnoff 		rn_detachhead((void **)&kt->pfrkt_ip4);
196931f0d081SAlexander V. Chernikov 	if (kt->pfrkt_ip6 != NULL)
1970495a22b5SGleb Smirnoff 		rn_detachhead((void **)&kt->pfrkt_ip6);
19713b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_shadow != NULL)
19723b3a8eb9SGleb Smirnoff 		pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr);
19733b3a8eb9SGleb Smirnoff 	if (kt->pfrkt_rs != NULL) {
19743b3a8eb9SGleb Smirnoff 		kt->pfrkt_rs->tables--;
1975*e86bddeaSKristof Provost 		pf_remove_if_empty_kruleset(kt->pfrkt_rs);
19763b3a8eb9SGleb Smirnoff 	}
197759048686SKristof Provost 	for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) {
197859048686SKristof Provost 		for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) {
197959048686SKristof Provost 			counter_u64_free(kt->pfrkt_packets[pfr_dir][pfr_op]);
198059048686SKristof Provost 			counter_u64_free(kt->pfrkt_bytes[pfr_dir][pfr_op]);
198159048686SKristof Provost 		}
198259048686SKristof Provost 	}
198359048686SKristof Provost 	counter_u64_free(kt->pfrkt_match);
198459048686SKristof Provost 	counter_u64_free(kt->pfrkt_nomatch);
198559048686SKristof Provost 
19863b3a8eb9SGleb Smirnoff 	free(kt, M_PFTABLE);
19873b3a8eb9SGleb Smirnoff }
19883b3a8eb9SGleb Smirnoff 
19893b3a8eb9SGleb Smirnoff static int
19903b3a8eb9SGleb Smirnoff pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q)
19913b3a8eb9SGleb Smirnoff {
19923b3a8eb9SGleb Smirnoff 	int d;
19933b3a8eb9SGleb Smirnoff 
19943b3a8eb9SGleb Smirnoff 	if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE)))
19953b3a8eb9SGleb Smirnoff 		return (d);
19963b3a8eb9SGleb Smirnoff 	return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor));
19973b3a8eb9SGleb Smirnoff }
19983b3a8eb9SGleb Smirnoff 
19993b3a8eb9SGleb Smirnoff static struct pfr_ktable *
20003b3a8eb9SGleb Smirnoff pfr_lookup_table(struct pfr_table *tbl)
20013b3a8eb9SGleb Smirnoff {
20023b3a8eb9SGleb Smirnoff 	/* struct pfr_ktable start like a struct pfr_table */
20031e9e3741SMarko Zec 	return (RB_FIND(pfr_ktablehead, &V_pfr_ktables,
20043b3a8eb9SGleb Smirnoff 	    (struct pfr_ktable *)tbl));
20053b3a8eb9SGleb Smirnoff }
20063b3a8eb9SGleb Smirnoff 
20073b3a8eb9SGleb Smirnoff int
20083b3a8eb9SGleb Smirnoff pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af)
20093b3a8eb9SGleb Smirnoff {
20103b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = NULL;
20113b3a8eb9SGleb Smirnoff 	int			 match;
20123b3a8eb9SGleb Smirnoff 
20133b3a8eb9SGleb Smirnoff 	PF_RULES_RASSERT();
20143b3a8eb9SGleb Smirnoff 
20153b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
20163b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
20173b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
20183b3a8eb9SGleb Smirnoff 		return (0);
20193b3a8eb9SGleb Smirnoff 
20203b3a8eb9SGleb Smirnoff 	switch (af) {
20213b3a8eb9SGleb Smirnoff #ifdef INET
20223b3a8eb9SGleb Smirnoff 	case AF_INET:
20233b3a8eb9SGleb Smirnoff 	    {
20243b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
20253b3a8eb9SGleb Smirnoff 
20263b3a8eb9SGleb Smirnoff 		bzero(&sin, sizeof(sin));
20273b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
20283b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
20293b3a8eb9SGleb Smirnoff 		sin.sin_addr.s_addr = a->addr32[0];
203061eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh);
20313b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20323b3a8eb9SGleb Smirnoff 			ke = NULL;
20333b3a8eb9SGleb Smirnoff 		break;
20343b3a8eb9SGleb Smirnoff 	    }
20353b3a8eb9SGleb Smirnoff #endif /* INET */
20363b3a8eb9SGleb Smirnoff #ifdef INET6
20373b3a8eb9SGleb Smirnoff 	case AF_INET6:
20383b3a8eb9SGleb Smirnoff 	    {
20393b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
20403b3a8eb9SGleb Smirnoff 
20413b3a8eb9SGleb Smirnoff 		bzero(&sin6, sizeof(sin6));
20423b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
20433b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
20443b3a8eb9SGleb Smirnoff 		bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
204561eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh);
20463b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20473b3a8eb9SGleb Smirnoff 			ke = NULL;
20483b3a8eb9SGleb Smirnoff 		break;
20493b3a8eb9SGleb Smirnoff 	    }
20503b3a8eb9SGleb Smirnoff #endif /* INET6 */
20513b3a8eb9SGleb Smirnoff 	}
20523b3a8eb9SGleb Smirnoff 	match = (ke && !ke->pfrke_not);
20533b3a8eb9SGleb Smirnoff 	if (match)
205459048686SKristof Provost 		counter_u64_add(kt->pfrkt_match, 1);
20553b3a8eb9SGleb Smirnoff 	else
205659048686SKristof Provost 		counter_u64_add(kt->pfrkt_nomatch, 1);
20573b3a8eb9SGleb Smirnoff 	return (match);
20583b3a8eb9SGleb Smirnoff }
20593b3a8eb9SGleb Smirnoff 
20603b3a8eb9SGleb Smirnoff void
20613b3a8eb9SGleb Smirnoff pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af,
20623b3a8eb9SGleb Smirnoff     u_int64_t len, int dir_out, int op_pass, int notrule)
20633b3a8eb9SGleb Smirnoff {
20643b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke = NULL;
20653b3a8eb9SGleb Smirnoff 
20663b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
20673b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
20683b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
20693b3a8eb9SGleb Smirnoff 		return;
20703b3a8eb9SGleb Smirnoff 
20713b3a8eb9SGleb Smirnoff 	switch (af) {
20723b3a8eb9SGleb Smirnoff #ifdef INET
20733b3a8eb9SGleb Smirnoff 	case AF_INET:
20743b3a8eb9SGleb Smirnoff 	    {
20753b3a8eb9SGleb Smirnoff 		struct sockaddr_in sin;
20763b3a8eb9SGleb Smirnoff 
20777348c524SGleb Smirnoff 		bzero(&sin, sizeof(sin));
20783b3a8eb9SGleb Smirnoff 		sin.sin_len = sizeof(sin);
20793b3a8eb9SGleb Smirnoff 		sin.sin_family = AF_INET;
20803b3a8eb9SGleb Smirnoff 		sin.sin_addr.s_addr = a->addr32[0];
208161eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh);
20823b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20833b3a8eb9SGleb Smirnoff 			ke = NULL;
20843b3a8eb9SGleb Smirnoff 		break;
20853b3a8eb9SGleb Smirnoff 	    }
20863b3a8eb9SGleb Smirnoff #endif /* INET */
20873b3a8eb9SGleb Smirnoff #ifdef INET6
20883b3a8eb9SGleb Smirnoff 	case AF_INET6:
20893b3a8eb9SGleb Smirnoff 	    {
20903b3a8eb9SGleb Smirnoff 		struct sockaddr_in6 sin6;
20913b3a8eb9SGleb Smirnoff 
20927348c524SGleb Smirnoff 		bzero(&sin6, sizeof(sin6));
20933b3a8eb9SGleb Smirnoff 		sin6.sin6_len = sizeof(sin6);
20943b3a8eb9SGleb Smirnoff 		sin6.sin6_family = AF_INET6;
20953b3a8eb9SGleb Smirnoff 		bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
209661eee0e2SAlexander V. Chernikov 		ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh);
20973b3a8eb9SGleb Smirnoff 		if (ke && KENTRY_RNF_ROOT(ke))
20983b3a8eb9SGleb Smirnoff 			ke = NULL;
20993b3a8eb9SGleb Smirnoff 		break;
21003b3a8eb9SGleb Smirnoff 	    }
21013b3a8eb9SGleb Smirnoff #endif /* INET6 */
21023b3a8eb9SGleb Smirnoff 	default:
21037348c524SGleb Smirnoff 		panic("%s: unknown address family %u", __func__, af);
21043b3a8eb9SGleb Smirnoff 	}
21053b3a8eb9SGleb Smirnoff 	if ((ke == NULL || ke->pfrke_not) != notrule) {
21063b3a8eb9SGleb Smirnoff 		if (op_pass != PFR_OP_PASS)
2107032dff66SKristof Provost 			DPFPRINTF(PF_DEBUG_URGENT,
2108032dff66SKristof Provost 			    ("pfr_update_stats: assertion failed.\n"));
21093b3a8eb9SGleb Smirnoff 		op_pass = PFR_OP_XPASS;
21103b3a8eb9SGleb Smirnoff 	}
211159048686SKristof Provost 	counter_u64_add(kt->pfrkt_packets[dir_out][op_pass], 1);
211259048686SKristof Provost 	counter_u64_add(kt->pfrkt_bytes[dir_out][op_pass], len);
21133b3a8eb9SGleb Smirnoff 	if (ke != NULL && op_pass != PFR_OP_XPASS &&
21143b3a8eb9SGleb Smirnoff 	    (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) {
2115c1be8399SMark Johnston 		counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters,
2116c1be8399SMark Johnston 		    dir_out, op_pass, PFR_TYPE_PACKETS), 1);
2117c1be8399SMark Johnston 		counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters,
2118c1be8399SMark Johnston 		    dir_out, op_pass, PFR_TYPE_BYTES), len);
21193b3a8eb9SGleb Smirnoff 	}
21203b3a8eb9SGleb Smirnoff }
21213b3a8eb9SGleb Smirnoff 
21223b3a8eb9SGleb Smirnoff struct pfr_ktable *
2123*e86bddeaSKristof Provost pfr_attach_table(struct pf_kruleset *rs, char *name)
21243b3a8eb9SGleb Smirnoff {
21253b3a8eb9SGleb Smirnoff 	struct pfr_ktable	*kt, *rt;
21263b3a8eb9SGleb Smirnoff 	struct pfr_table	 tbl;
2127*e86bddeaSKristof Provost 	struct pf_kanchor	*ac = rs->anchor;
21283b3a8eb9SGleb Smirnoff 
21293b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
21303b3a8eb9SGleb Smirnoff 
21313b3a8eb9SGleb Smirnoff 	bzero(&tbl, sizeof(tbl));
21323b3a8eb9SGleb Smirnoff 	strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name));
21333b3a8eb9SGleb Smirnoff 	if (ac != NULL)
21343b3a8eb9SGleb Smirnoff 		strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor));
21353b3a8eb9SGleb Smirnoff 	kt = pfr_lookup_table(&tbl);
21363b3a8eb9SGleb Smirnoff 	if (kt == NULL) {
21373b3a8eb9SGleb Smirnoff 		kt = pfr_create_ktable(&tbl, time_second, 1);
21383b3a8eb9SGleb Smirnoff 		if (kt == NULL)
21393b3a8eb9SGleb Smirnoff 			return (NULL);
21403b3a8eb9SGleb Smirnoff 		if (ac != NULL) {
21413b3a8eb9SGleb Smirnoff 			bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor));
21423b3a8eb9SGleb Smirnoff 			rt = pfr_lookup_table(&tbl);
21433b3a8eb9SGleb Smirnoff 			if (rt == NULL) {
21443b3a8eb9SGleb Smirnoff 				rt = pfr_create_ktable(&tbl, 0, 1);
21453b3a8eb9SGleb Smirnoff 				if (rt == NULL) {
21463b3a8eb9SGleb Smirnoff 					pfr_destroy_ktable(kt, 0);
21473b3a8eb9SGleb Smirnoff 					return (NULL);
21483b3a8eb9SGleb Smirnoff 				}
21493b3a8eb9SGleb Smirnoff 				pfr_insert_ktable(rt);
21503b3a8eb9SGleb Smirnoff 			}
21513b3a8eb9SGleb Smirnoff 			kt->pfrkt_root = rt;
21523b3a8eb9SGleb Smirnoff 		}
21533b3a8eb9SGleb Smirnoff 		pfr_insert_ktable(kt);
21543b3a8eb9SGleb Smirnoff 	}
21553b3a8eb9SGleb Smirnoff 	if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++)
21563b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED);
21573b3a8eb9SGleb Smirnoff 	return (kt);
21583b3a8eb9SGleb Smirnoff }
21593b3a8eb9SGleb Smirnoff 
21603b3a8eb9SGleb Smirnoff void
21613b3a8eb9SGleb Smirnoff pfr_detach_table(struct pfr_ktable *kt)
21623b3a8eb9SGleb Smirnoff {
21633b3a8eb9SGleb Smirnoff 
21643b3a8eb9SGleb Smirnoff 	PF_RULES_WASSERT();
21653b3a8eb9SGleb Smirnoff 	KASSERT(kt->pfrkt_refcnt[PFR_REFCNT_RULE] > 0, ("%s: refcount %d\n",
21663b3a8eb9SGleb Smirnoff 	    __func__, kt->pfrkt_refcnt[PFR_REFCNT_RULE]));
21673b3a8eb9SGleb Smirnoff 
21683b3a8eb9SGleb Smirnoff 	if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE])
21693b3a8eb9SGleb Smirnoff 		pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED);
21703b3a8eb9SGleb Smirnoff }
21713b3a8eb9SGleb Smirnoff 
21723b3a8eb9SGleb Smirnoff int
21733b3a8eb9SGleb Smirnoff pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter,
21743b3a8eb9SGleb Smirnoff     sa_family_t af)
21753b3a8eb9SGleb Smirnoff {
21763b3a8eb9SGleb Smirnoff 	struct pf_addr		 *addr, *cur, *mask;
21773b3a8eb9SGleb Smirnoff 	union sockaddr_union	 uaddr, umask;
21783b3a8eb9SGleb Smirnoff 	struct pfr_kentry	*ke, *ke2 = NULL;
21793b3a8eb9SGleb Smirnoff 	int			 idx = -1, use_counter = 0;
21803b3a8eb9SGleb Smirnoff 
21813b3a8eb9SGleb Smirnoff 	switch (af) {
21823b3a8eb9SGleb Smirnoff 	case AF_INET:
21833b3a8eb9SGleb Smirnoff 		uaddr.sin.sin_len = sizeof(struct sockaddr_in);
21843b3a8eb9SGleb Smirnoff 		uaddr.sin.sin_family = AF_INET;
21853b3a8eb9SGleb Smirnoff 		break;
21863b3a8eb9SGleb Smirnoff 	case AF_INET6:
21873b3a8eb9SGleb Smirnoff 		uaddr.sin6.sin6_len = sizeof(struct sockaddr_in6);
21883b3a8eb9SGleb Smirnoff 		uaddr.sin6.sin6_family = AF_INET6;
21893b3a8eb9SGleb Smirnoff 		break;
21903b3a8eb9SGleb Smirnoff 	}
21913b3a8eb9SGleb Smirnoff 	addr = SUNION2PF(&uaddr, af);
21923b3a8eb9SGleb Smirnoff 
21933b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
21943b3a8eb9SGleb Smirnoff 		kt = kt->pfrkt_root;
21953b3a8eb9SGleb Smirnoff 	if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
21963b3a8eb9SGleb Smirnoff 		return (-1);
21973b3a8eb9SGleb Smirnoff 
21983b3a8eb9SGleb Smirnoff 	if (pidx != NULL)
21993b3a8eb9SGleb Smirnoff 		idx = *pidx;
22003b3a8eb9SGleb Smirnoff 	if (counter != NULL && idx >= 0)
22013b3a8eb9SGleb Smirnoff 		use_counter = 1;
22023b3a8eb9SGleb Smirnoff 	if (idx < 0)
22033b3a8eb9SGleb Smirnoff 		idx = 0;
22043b3a8eb9SGleb Smirnoff 
22053b3a8eb9SGleb Smirnoff _next_block:
22063b3a8eb9SGleb Smirnoff 	ke = pfr_kentry_byidx(kt, idx, af);
22073b3a8eb9SGleb Smirnoff 	if (ke == NULL) {
220859048686SKristof Provost 		counter_u64_add(kt->pfrkt_nomatch, 1);
22093b3a8eb9SGleb Smirnoff 		return (1);
22103b3a8eb9SGleb Smirnoff 	}
22113b3a8eb9SGleb Smirnoff 	pfr_prepare_network(&umask, af, ke->pfrke_net);
22123b3a8eb9SGleb Smirnoff 	cur = SUNION2PF(&ke->pfrke_sa, af);
22133b3a8eb9SGleb Smirnoff 	mask = SUNION2PF(&umask, af);
22143b3a8eb9SGleb Smirnoff 
22153b3a8eb9SGleb Smirnoff 	if (use_counter) {
22163b3a8eb9SGleb Smirnoff 		/* is supplied address within block? */
22173b3a8eb9SGleb Smirnoff 		if (!PF_MATCHA(0, cur, mask, counter, af)) {
22183b3a8eb9SGleb Smirnoff 			/* no, go to next block in table */
22193b3a8eb9SGleb Smirnoff 			idx++;
22203b3a8eb9SGleb Smirnoff 			use_counter = 0;
22213b3a8eb9SGleb Smirnoff 			goto _next_block;
22223b3a8eb9SGleb Smirnoff 		}
22233b3a8eb9SGleb Smirnoff 		PF_ACPY(addr, counter, af);
22243b3a8eb9SGleb Smirnoff 	} else {
22253b3a8eb9SGleb Smirnoff 		/* use first address of block */
22263b3a8eb9SGleb Smirnoff 		PF_ACPY(addr, cur, af);
22273b3a8eb9SGleb Smirnoff 	}
22283b3a8eb9SGleb Smirnoff 
22293b3a8eb9SGleb Smirnoff 	if (!KENTRY_NETWORK(ke)) {
22303b3a8eb9SGleb Smirnoff 		/* this is a single IP address - no possible nested block */
22313b3a8eb9SGleb Smirnoff 		PF_ACPY(counter, addr, af);
22323b3a8eb9SGleb Smirnoff 		*pidx = idx;
223359048686SKristof Provost 		counter_u64_add(kt->pfrkt_match, 1);
22343b3a8eb9SGleb Smirnoff 		return (0);
22353b3a8eb9SGleb Smirnoff 	}
22363b3a8eb9SGleb Smirnoff 	for (;;) {
22373b3a8eb9SGleb Smirnoff 		/* we don't want to use a nested block */
22383b3a8eb9SGleb Smirnoff 		switch (af) {
22393b3a8eb9SGleb Smirnoff 		case AF_INET:
22403b3a8eb9SGleb Smirnoff 			ke2 = (struct pfr_kentry *)rn_match(&uaddr,
224161eee0e2SAlexander V. Chernikov 			    &kt->pfrkt_ip4->rh);
22423b3a8eb9SGleb Smirnoff 			break;
22433b3a8eb9SGleb Smirnoff 		case AF_INET6:
22443b3a8eb9SGleb Smirnoff 			ke2 = (struct pfr_kentry *)rn_match(&uaddr,
224561eee0e2SAlexander V. Chernikov 			    &kt->pfrkt_ip6->rh);
22463b3a8eb9SGleb Smirnoff 			break;
22473b3a8eb9SGleb Smirnoff 		}
22483b3a8eb9SGleb Smirnoff 		/* no need to check KENTRY_RNF_ROOT() here */
22493b3a8eb9SGleb Smirnoff 		if (ke2 == ke) {
22503b3a8eb9SGleb Smirnoff 			/* lookup return the same block - perfect */
22513b3a8eb9SGleb Smirnoff 			PF_ACPY(counter, addr, af);
22523b3a8eb9SGleb Smirnoff 			*pidx = idx;
225359048686SKristof Provost 			counter_u64_add(kt->pfrkt_match, 1);
22543b3a8eb9SGleb Smirnoff 			return (0);
22553b3a8eb9SGleb Smirnoff 		}
22563b3a8eb9SGleb Smirnoff 
22573b3a8eb9SGleb Smirnoff 		/* we need to increase the counter past the nested block */
22583b3a8eb9SGleb Smirnoff 		pfr_prepare_network(&umask, AF_INET, ke2->pfrke_net);
22593b3a8eb9SGleb Smirnoff 		PF_POOLMASK(addr, addr, SUNION2PF(&umask, af), &pfr_ffaddr, af);
22603b3a8eb9SGleb Smirnoff 		PF_AINC(addr, af);
22613b3a8eb9SGleb Smirnoff 		if (!PF_MATCHA(0, cur, mask, addr, af)) {
22623b3a8eb9SGleb Smirnoff 			/* ok, we reached the end of our main block */
22633b3a8eb9SGleb Smirnoff 			/* go to next block in table */
22643b3a8eb9SGleb Smirnoff 			idx++;
22653b3a8eb9SGleb Smirnoff 			use_counter = 0;
22663b3a8eb9SGleb Smirnoff 			goto _next_block;
22673b3a8eb9SGleb Smirnoff 		}
22683b3a8eb9SGleb Smirnoff 	}
22693b3a8eb9SGleb Smirnoff }
22703b3a8eb9SGleb Smirnoff 
22713b3a8eb9SGleb Smirnoff static struct pfr_kentry *
22723b3a8eb9SGleb Smirnoff pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af)
22733b3a8eb9SGleb Smirnoff {
22743b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
22753b3a8eb9SGleb Smirnoff 
22763b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
22773b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_POOL_GET;
22783b3a8eb9SGleb Smirnoff 	w.pfrw_cnt = idx;
22793b3a8eb9SGleb Smirnoff 
22803b3a8eb9SGleb Smirnoff 	switch (af) {
22813b3a8eb9SGleb Smirnoff #ifdef INET
22823b3a8eb9SGleb Smirnoff 	case AF_INET:
228361eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
22843b3a8eb9SGleb Smirnoff 		return (w.pfrw_kentry);
22853b3a8eb9SGleb Smirnoff #endif /* INET */
22863b3a8eb9SGleb Smirnoff #ifdef INET6
22873b3a8eb9SGleb Smirnoff 	case AF_INET6:
228861eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
22893b3a8eb9SGleb Smirnoff 		return (w.pfrw_kentry);
22903b3a8eb9SGleb Smirnoff #endif /* INET6 */
22913b3a8eb9SGleb Smirnoff 	default:
22923b3a8eb9SGleb Smirnoff 		return (NULL);
22933b3a8eb9SGleb Smirnoff 	}
22943b3a8eb9SGleb Smirnoff }
22953b3a8eb9SGleb Smirnoff 
22963b3a8eb9SGleb Smirnoff void
22973b3a8eb9SGleb Smirnoff pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn)
22983b3a8eb9SGleb Smirnoff {
22993b3a8eb9SGleb Smirnoff 	struct pfr_walktree	w;
23003b3a8eb9SGleb Smirnoff 
23013b3a8eb9SGleb Smirnoff 	bzero(&w, sizeof(w));
23023b3a8eb9SGleb Smirnoff 	w.pfrw_op = PFRW_DYNADDR_UPDATE;
23033b3a8eb9SGleb Smirnoff 	w.pfrw_dyn = dyn;
23043b3a8eb9SGleb Smirnoff 
23053b3a8eb9SGleb Smirnoff 	dyn->pfid_acnt4 = 0;
23063b3a8eb9SGleb Smirnoff 	dyn->pfid_acnt6 = 0;
23073b3a8eb9SGleb Smirnoff 	if (!dyn->pfid_af || dyn->pfid_af == AF_INET)
230861eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w);
23093b3a8eb9SGleb Smirnoff 	if (!dyn->pfid_af || dyn->pfid_af == AF_INET6)
231061eee0e2SAlexander V. Chernikov 		kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w);
23113b3a8eb9SGleb Smirnoff }
2312