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 1031*60a38abbSMark Johnston bzero(as, sizeof(*as)); 103259048686SKristof Provost pfr_copyout_addr(&as->pfras_a, ke); 103359048686SKristof Provost as->pfras_tzero = kc->pfrkc_tzero; 103459048686SKristof Provost 103559048686SKristof Provost if (! (w->pfrw_flags & PFR_TFLAG_COUNTERS)) { 103659048686SKristof Provost bzero(as->pfras_packets, sizeof(as->pfras_packets)); 103759048686SKristof Provost bzero(as->pfras_bytes, sizeof(as->pfras_bytes)); 103859048686SKristof Provost as->pfras_a.pfra_fback = PFR_FB_NOCOUNT; 103959048686SKristof Provost return; 104059048686SKristof Provost } 104159048686SKristof Provost 104259048686SKristof Provost for (dir = 0; dir < PFR_DIR_MAX; dir++) { 104359048686SKristof Provost for (op = 0; op < PFR_OP_ADDR_MAX; op ++) { 1044c1be8399SMark Johnston as->pfras_packets[dir][op] = counter_u64_fetch( 1045c1be8399SMark Johnston pfr_kentry_counter(kc, dir, op, PFR_TYPE_PACKETS)); 1046c1be8399SMark Johnston as->pfras_bytes[dir][op] = counter_u64_fetch( 1047c1be8399SMark Johnston pfr_kentry_counter(kc, dir, op, PFR_TYPE_BYTES)); 104859048686SKristof Provost } 104959048686SKristof Provost } 105059048686SKristof Provost } 105159048686SKristof Provost 10523b3a8eb9SGleb Smirnoff static int 10533b3a8eb9SGleb Smirnoff pfr_walktree(struct radix_node *rn, void *arg) 10543b3a8eb9SGleb Smirnoff { 10553b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = (struct pfr_kentry *)rn; 10563b3a8eb9SGleb Smirnoff struct pfr_walktree *w = arg; 10573b3a8eb9SGleb Smirnoff 10583b3a8eb9SGleb Smirnoff switch (w->pfrw_op) { 10593b3a8eb9SGleb Smirnoff case PFRW_MARK: 10603b3a8eb9SGleb Smirnoff ke->pfrke_mark = 0; 10613b3a8eb9SGleb Smirnoff break; 10623b3a8eb9SGleb Smirnoff case PFRW_SWEEP: 10633b3a8eb9SGleb Smirnoff if (ke->pfrke_mark) 10643b3a8eb9SGleb Smirnoff break; 10653b3a8eb9SGleb Smirnoff /* FALLTHROUGH */ 10663b3a8eb9SGleb Smirnoff case PFRW_ENQUEUE: 10673b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq); 10683b3a8eb9SGleb Smirnoff w->pfrw_cnt++; 10693b3a8eb9SGleb Smirnoff break; 10703b3a8eb9SGleb Smirnoff case PFRW_GET_ADDRS: 10713b3a8eb9SGleb Smirnoff if (w->pfrw_free-- > 0) { 10723b3a8eb9SGleb Smirnoff pfr_copyout_addr(w->pfrw_addr, ke); 10733b3a8eb9SGleb Smirnoff w->pfrw_addr++; 10743b3a8eb9SGleb Smirnoff } 10753b3a8eb9SGleb Smirnoff break; 10763b3a8eb9SGleb Smirnoff case PFRW_GET_ASTATS: 10773b3a8eb9SGleb Smirnoff if (w->pfrw_free-- > 0) { 10783b3a8eb9SGleb Smirnoff struct pfr_astats as; 10793b3a8eb9SGleb Smirnoff 108059048686SKristof Provost pfr_copyout_astats(&as, ke, w); 10813b3a8eb9SGleb Smirnoff 10823b3a8eb9SGleb Smirnoff bcopy(&as, w->pfrw_astats, sizeof(as)); 10833b3a8eb9SGleb Smirnoff w->pfrw_astats++; 10843b3a8eb9SGleb Smirnoff } 10853b3a8eb9SGleb Smirnoff break; 10863b3a8eb9SGleb Smirnoff case PFRW_POOL_GET: 10873b3a8eb9SGleb Smirnoff if (ke->pfrke_not) 10883b3a8eb9SGleb Smirnoff break; /* negative entries are ignored */ 10893b3a8eb9SGleb Smirnoff if (!w->pfrw_cnt--) { 10903b3a8eb9SGleb Smirnoff w->pfrw_kentry = ke; 10913b3a8eb9SGleb Smirnoff return (1); /* finish search */ 10923b3a8eb9SGleb Smirnoff } 10933b3a8eb9SGleb Smirnoff break; 10943b3a8eb9SGleb Smirnoff case PFRW_DYNADDR_UPDATE: 10953b3a8eb9SGleb Smirnoff { 10963b3a8eb9SGleb Smirnoff union sockaddr_union pfr_mask; 10973b3a8eb9SGleb Smirnoff 10983b3a8eb9SGleb Smirnoff if (ke->pfrke_af == AF_INET) { 10993b3a8eb9SGleb Smirnoff if (w->pfrw_dyn->pfid_acnt4++ > 0) 11003b3a8eb9SGleb Smirnoff break; 11013b3a8eb9SGleb Smirnoff pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net); 11023b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_addr4 = *SUNION2PF(&ke->pfrke_sa, 11033b3a8eb9SGleb Smirnoff AF_INET); 11043b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_mask4 = *SUNION2PF(&pfr_mask, 11053b3a8eb9SGleb Smirnoff AF_INET); 11063b3a8eb9SGleb Smirnoff } else if (ke->pfrke_af == AF_INET6){ 11073b3a8eb9SGleb Smirnoff if (w->pfrw_dyn->pfid_acnt6++ > 0) 11083b3a8eb9SGleb Smirnoff break; 11093b3a8eb9SGleb Smirnoff pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net); 11103b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_addr6 = *SUNION2PF(&ke->pfrke_sa, 11113b3a8eb9SGleb Smirnoff AF_INET6); 11123b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_mask6 = *SUNION2PF(&pfr_mask, 11133b3a8eb9SGleb Smirnoff AF_INET6); 11143b3a8eb9SGleb Smirnoff } 11153b3a8eb9SGleb Smirnoff break; 11163b3a8eb9SGleb Smirnoff } 11173b3a8eb9SGleb Smirnoff } 11183b3a8eb9SGleb Smirnoff return (0); 11193b3a8eb9SGleb Smirnoff } 11203b3a8eb9SGleb Smirnoff 11213b3a8eb9SGleb Smirnoff int 11223b3a8eb9SGleb Smirnoff pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags) 11233b3a8eb9SGleb Smirnoff { 11243b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 11253b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 11263b3a8eb9SGleb Smirnoff int xdel = 0; 11273b3a8eb9SGleb Smirnoff 11283b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ALLRSETS); 11293b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 11303b3a8eb9SGleb Smirnoff return (EINVAL); 11313b3a8eb9SGleb Smirnoff if (pfr_table_count(filter, flags) < 0) 11323b3a8eb9SGleb Smirnoff return (ENOENT); 11333b3a8eb9SGleb Smirnoff 11343b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 11351e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 11363b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 11373b3a8eb9SGleb Smirnoff continue; 11383b3a8eb9SGleb Smirnoff if (!strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR)) 11393b3a8eb9SGleb Smirnoff continue; 11403b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) 11413b3a8eb9SGleb Smirnoff continue; 11423b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE; 11433b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 11443b3a8eb9SGleb Smirnoff xdel++; 11453b3a8eb9SGleb Smirnoff } 11463b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 11473b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 11483b3a8eb9SGleb Smirnoff if (ndel != NULL) 11493b3a8eb9SGleb Smirnoff *ndel = xdel; 11503b3a8eb9SGleb Smirnoff return (0); 11513b3a8eb9SGleb Smirnoff } 11523b3a8eb9SGleb Smirnoff 11533b3a8eb9SGleb Smirnoff int 11543b3a8eb9SGleb Smirnoff pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags) 11553b3a8eb9SGleb Smirnoff { 11563b3a8eb9SGleb Smirnoff struct pfr_ktableworkq addq, changeq; 11573b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, *r, key; 11583b3a8eb9SGleb Smirnoff int i, rv, xadd = 0; 11593b3a8eb9SGleb Smirnoff long tzero = time_second; 11603b3a8eb9SGleb Smirnoff 11613b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 11623b3a8eb9SGleb Smirnoff SLIST_INIT(&addq); 11633b3a8eb9SGleb Smirnoff SLIST_INIT(&changeq); 11643b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 11653b3a8eb9SGleb Smirnoff bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 11663b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK, 11673b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 11683b3a8eb9SGleb Smirnoff senderr(EINVAL); 11693b3a8eb9SGleb Smirnoff key.pfrkt_flags |= PFR_TFLAG_ACTIVE; 11701e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 11713b3a8eb9SGleb Smirnoff if (p == NULL) { 11723b3a8eb9SGleb Smirnoff p = pfr_create_ktable(&key.pfrkt_t, tzero, 1); 11733b3a8eb9SGleb Smirnoff if (p == NULL) 11743b3a8eb9SGleb Smirnoff senderr(ENOMEM); 11753b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &addq, pfrkt_workq) { 1176b4b8fa33SKristof Provost if (!pfr_ktable_compare(p, q)) { 1177b4b8fa33SKristof Provost pfr_destroy_ktable(p, 0); 11783b3a8eb9SGleb Smirnoff goto _skip; 11793b3a8eb9SGleb Smirnoff } 1180b4b8fa33SKristof Provost } 11813b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, p, pfrkt_workq); 11823b3a8eb9SGleb Smirnoff xadd++; 11833b3a8eb9SGleb Smirnoff if (!key.pfrkt_anchor[0]) 11843b3a8eb9SGleb Smirnoff goto _skip; 11853b3a8eb9SGleb Smirnoff 11863b3a8eb9SGleb Smirnoff /* find or create root table */ 11873b3a8eb9SGleb Smirnoff bzero(key.pfrkt_anchor, sizeof(key.pfrkt_anchor)); 11881e9e3741SMarko Zec r = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 11893b3a8eb9SGleb Smirnoff if (r != NULL) { 11903b3a8eb9SGleb Smirnoff p->pfrkt_root = r; 11913b3a8eb9SGleb Smirnoff goto _skip; 11923b3a8eb9SGleb Smirnoff } 11933b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &addq, pfrkt_workq) { 11943b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(&key, q)) { 11953b3a8eb9SGleb Smirnoff p->pfrkt_root = q; 11963b3a8eb9SGleb Smirnoff goto _skip; 11973b3a8eb9SGleb Smirnoff } 11983b3a8eb9SGleb Smirnoff } 11993b3a8eb9SGleb Smirnoff key.pfrkt_flags = 0; 12003b3a8eb9SGleb Smirnoff r = pfr_create_ktable(&key.pfrkt_t, 0, 1); 12013b3a8eb9SGleb Smirnoff if (r == NULL) 12023b3a8eb9SGleb Smirnoff senderr(ENOMEM); 12033b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, r, pfrkt_workq); 12043b3a8eb9SGleb Smirnoff p->pfrkt_root = r; 12053b3a8eb9SGleb Smirnoff } else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 12063b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &changeq, pfrkt_workq) 12073b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(&key, q)) 12083b3a8eb9SGleb Smirnoff goto _skip; 12093b3a8eb9SGleb Smirnoff p->pfrkt_nflags = (p->pfrkt_flags & 12103b3a8eb9SGleb Smirnoff ~PFR_TFLAG_USRMASK) | key.pfrkt_flags; 12113b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq); 12123b3a8eb9SGleb Smirnoff xadd++; 12133b3a8eb9SGleb Smirnoff } 12143b3a8eb9SGleb Smirnoff _skip: 12153b3a8eb9SGleb Smirnoff ; 12163b3a8eb9SGleb Smirnoff } 12173b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 12183b3a8eb9SGleb Smirnoff pfr_insert_ktables(&addq); 12193b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&changeq); 12203b3a8eb9SGleb Smirnoff } else 12213b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&addq, 0); 12223b3a8eb9SGleb Smirnoff if (nadd != NULL) 12233b3a8eb9SGleb Smirnoff *nadd = xadd; 12243b3a8eb9SGleb Smirnoff return (0); 12253b3a8eb9SGleb Smirnoff _bad: 12263b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&addq, 0); 12273b3a8eb9SGleb Smirnoff return (rv); 12283b3a8eb9SGleb Smirnoff } 12293b3a8eb9SGleb Smirnoff 12303b3a8eb9SGleb Smirnoff int 12313b3a8eb9SGleb Smirnoff pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags) 12323b3a8eb9SGleb Smirnoff { 12333b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 12343b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, key; 12353b3a8eb9SGleb Smirnoff int i, xdel = 0; 12363b3a8eb9SGleb Smirnoff 12373b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 12383b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 12393b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 12403b3a8eb9SGleb Smirnoff bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 12413b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 12423b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 12433b3a8eb9SGleb Smirnoff return (EINVAL); 12441e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 12453b3a8eb9SGleb Smirnoff if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 12463b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &workq, pfrkt_workq) 12473b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(p, q)) 12483b3a8eb9SGleb Smirnoff goto _skip; 12493b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE; 12503b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 12513b3a8eb9SGleb Smirnoff xdel++; 12523b3a8eb9SGleb Smirnoff } 12533b3a8eb9SGleb Smirnoff _skip: 12543b3a8eb9SGleb Smirnoff ; 12553b3a8eb9SGleb Smirnoff } 12563b3a8eb9SGleb Smirnoff 12573b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 12583b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 12593b3a8eb9SGleb Smirnoff if (ndel != NULL) 12603b3a8eb9SGleb Smirnoff *ndel = xdel; 12613b3a8eb9SGleb Smirnoff return (0); 12623b3a8eb9SGleb Smirnoff } 12633b3a8eb9SGleb Smirnoff 12643b3a8eb9SGleb Smirnoff int 12653b3a8eb9SGleb Smirnoff pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size, 12663b3a8eb9SGleb Smirnoff int flags) 12673b3a8eb9SGleb Smirnoff { 12683b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 12693b3a8eb9SGleb Smirnoff int n, nn; 12703b3a8eb9SGleb Smirnoff 12713b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 12723b3a8eb9SGleb Smirnoff 12733b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS); 12743b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 12753b3a8eb9SGleb Smirnoff return (EINVAL); 12763b3a8eb9SGleb Smirnoff n = nn = pfr_table_count(filter, flags); 12773b3a8eb9SGleb Smirnoff if (n < 0) 12783b3a8eb9SGleb Smirnoff return (ENOENT); 12793b3a8eb9SGleb Smirnoff if (n > *size) { 12803b3a8eb9SGleb Smirnoff *size = n; 12813b3a8eb9SGleb Smirnoff return (0); 12823b3a8eb9SGleb Smirnoff } 12831e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 12843b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 12853b3a8eb9SGleb Smirnoff continue; 12863b3a8eb9SGleb Smirnoff if (n-- <= 0) 12873b3a8eb9SGleb Smirnoff continue; 12883b3a8eb9SGleb Smirnoff bcopy(&p->pfrkt_t, tbl++, sizeof(*tbl)); 12893b3a8eb9SGleb Smirnoff } 12903b3a8eb9SGleb Smirnoff 12913b3a8eb9SGleb Smirnoff KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n)); 12923b3a8eb9SGleb Smirnoff 12933b3a8eb9SGleb Smirnoff *size = nn; 12943b3a8eb9SGleb Smirnoff return (0); 12953b3a8eb9SGleb Smirnoff } 12963b3a8eb9SGleb Smirnoff 12973b3a8eb9SGleb Smirnoff int 12983b3a8eb9SGleb Smirnoff pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size, 12993b3a8eb9SGleb Smirnoff int flags) 13003b3a8eb9SGleb Smirnoff { 13013b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 13023b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 13033b3a8eb9SGleb Smirnoff int n, nn; 13043b3a8eb9SGleb Smirnoff long tzero = time_second; 130559048686SKristof Provost int pfr_dir, pfr_op; 13063b3a8eb9SGleb Smirnoff 13073b3a8eb9SGleb Smirnoff /* XXX PFR_FLAG_CLSTATS disabled */ 13083b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS); 13093b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 13103b3a8eb9SGleb Smirnoff return (EINVAL); 13113b3a8eb9SGleb Smirnoff n = nn = pfr_table_count(filter, flags); 13123b3a8eb9SGleb Smirnoff if (n < 0) 13133b3a8eb9SGleb Smirnoff return (ENOENT); 13143b3a8eb9SGleb Smirnoff if (n > *size) { 13153b3a8eb9SGleb Smirnoff *size = n; 13163b3a8eb9SGleb Smirnoff return (0); 13173b3a8eb9SGleb Smirnoff } 13183b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 13191e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 13203b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 13213b3a8eb9SGleb Smirnoff continue; 13223b3a8eb9SGleb Smirnoff if (n-- <= 0) 13233b3a8eb9SGleb Smirnoff continue; 132459048686SKristof Provost bcopy(&p->pfrkt_kts.pfrts_t, &tbl->pfrts_t, 132559048686SKristof Provost sizeof(struct pfr_table)); 132659048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 132759048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 132859048686SKristof Provost tbl->pfrts_packets[pfr_dir][pfr_op] = 132959048686SKristof Provost counter_u64_fetch( 133059048686SKristof Provost p->pfrkt_packets[pfr_dir][pfr_op]); 133159048686SKristof Provost tbl->pfrts_bytes[pfr_dir][pfr_op] = 133259048686SKristof Provost counter_u64_fetch( 133359048686SKristof Provost p->pfrkt_bytes[pfr_dir][pfr_op]); 133459048686SKristof Provost } 133559048686SKristof Provost } 133659048686SKristof Provost tbl->pfrts_match = counter_u64_fetch(p->pfrkt_match); 133759048686SKristof Provost tbl->pfrts_nomatch = counter_u64_fetch(p->pfrkt_nomatch); 133859048686SKristof Provost tbl->pfrts_tzero = p->pfrkt_tzero; 133959048686SKristof Provost tbl->pfrts_cnt = p->pfrkt_cnt; 134059048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_REFCNT_MAX; pfr_op++) 134159048686SKristof Provost tbl->pfrts_refcnt[pfr_op] = p->pfrkt_refcnt[pfr_op]; 134259048686SKristof Provost tbl++; 13433b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 13443b3a8eb9SGleb Smirnoff } 13453b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_CLSTATS) 13463b3a8eb9SGleb Smirnoff pfr_clstats_ktables(&workq, tzero, 13473b3a8eb9SGleb Smirnoff flags & PFR_FLAG_ADDRSTOO); 13483b3a8eb9SGleb Smirnoff 13493b3a8eb9SGleb Smirnoff KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n)); 13503b3a8eb9SGleb Smirnoff 13513b3a8eb9SGleb Smirnoff *size = nn; 13523b3a8eb9SGleb Smirnoff return (0); 13533b3a8eb9SGleb Smirnoff } 13543b3a8eb9SGleb Smirnoff 13553b3a8eb9SGleb Smirnoff int 13563b3a8eb9SGleb Smirnoff pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags) 13573b3a8eb9SGleb Smirnoff { 13583b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 13593b3a8eb9SGleb Smirnoff struct pfr_ktable *p, key; 13603b3a8eb9SGleb Smirnoff int i, xzero = 0; 13613b3a8eb9SGleb Smirnoff long tzero = time_second; 13623b3a8eb9SGleb Smirnoff 13633b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO); 13643b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 13653b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 13663b3a8eb9SGleb Smirnoff bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 13673b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 0)) 13683b3a8eb9SGleb Smirnoff return (EINVAL); 13691e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 13703b3a8eb9SGleb Smirnoff if (p != NULL) { 13713b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 13723b3a8eb9SGleb Smirnoff xzero++; 13733b3a8eb9SGleb Smirnoff } 13743b3a8eb9SGleb Smirnoff } 13753b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 13763b3a8eb9SGleb Smirnoff pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO); 13773b3a8eb9SGleb Smirnoff if (nzero != NULL) 13783b3a8eb9SGleb Smirnoff *nzero = xzero; 13793b3a8eb9SGleb Smirnoff return (0); 13803b3a8eb9SGleb Smirnoff } 13813b3a8eb9SGleb Smirnoff 13823b3a8eb9SGleb Smirnoff int 13833b3a8eb9SGleb Smirnoff pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag, 13843b3a8eb9SGleb Smirnoff int *nchange, int *ndel, int flags) 13853b3a8eb9SGleb Smirnoff { 13863b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 13873b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, key; 13883b3a8eb9SGleb Smirnoff int i, xchange = 0, xdel = 0; 13893b3a8eb9SGleb Smirnoff 13903b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 13913b3a8eb9SGleb Smirnoff if ((setflag & ~PFR_TFLAG_USRMASK) || 13923b3a8eb9SGleb Smirnoff (clrflag & ~PFR_TFLAG_USRMASK) || 13933b3a8eb9SGleb Smirnoff (setflag & clrflag)) 13943b3a8eb9SGleb Smirnoff return (EINVAL); 13953b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 13963b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 13973b3a8eb9SGleb Smirnoff bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 13983b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 13993b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 14003b3a8eb9SGleb Smirnoff return (EINVAL); 14011e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 14023b3a8eb9SGleb Smirnoff if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 14033b3a8eb9SGleb Smirnoff p->pfrkt_nflags = (p->pfrkt_flags | setflag) & 14043b3a8eb9SGleb Smirnoff ~clrflag; 14053b3a8eb9SGleb Smirnoff if (p->pfrkt_nflags == p->pfrkt_flags) 14063b3a8eb9SGleb Smirnoff goto _skip; 14073b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &workq, pfrkt_workq) 14083b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(p, q)) 14093b3a8eb9SGleb Smirnoff goto _skip; 14103b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 14113b3a8eb9SGleb Smirnoff if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) && 14123b3a8eb9SGleb Smirnoff (clrflag & PFR_TFLAG_PERSIST) && 14133b3a8eb9SGleb Smirnoff !(p->pfrkt_flags & PFR_TFLAG_REFERENCED)) 14143b3a8eb9SGleb Smirnoff xdel++; 14153b3a8eb9SGleb Smirnoff else 14163b3a8eb9SGleb Smirnoff xchange++; 14173b3a8eb9SGleb Smirnoff } 14183b3a8eb9SGleb Smirnoff _skip: 14193b3a8eb9SGleb Smirnoff ; 14203b3a8eb9SGleb Smirnoff } 14213b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 14223b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 14233b3a8eb9SGleb Smirnoff if (nchange != NULL) 14243b3a8eb9SGleb Smirnoff *nchange = xchange; 14253b3a8eb9SGleb Smirnoff if (ndel != NULL) 14263b3a8eb9SGleb Smirnoff *ndel = xdel; 14273b3a8eb9SGleb Smirnoff return (0); 14283b3a8eb9SGleb Smirnoff } 14293b3a8eb9SGleb Smirnoff 14303b3a8eb9SGleb Smirnoff int 14313b3a8eb9SGleb Smirnoff pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags) 14323b3a8eb9SGleb Smirnoff { 14333b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 14343b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 1435e86bddeaSKristof Provost struct pf_kruleset *rs; 14363b3a8eb9SGleb Smirnoff int xdel = 0; 14373b3a8eb9SGleb Smirnoff 14383b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1439e86bddeaSKristof Provost rs = pf_find_or_create_kruleset(trs->pfrt_anchor); 14403b3a8eb9SGleb Smirnoff if (rs == NULL) 14413b3a8eb9SGleb Smirnoff return (ENOMEM); 14423b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 14431e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 14443b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 14453b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 14463b3a8eb9SGleb Smirnoff continue; 14473b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE; 14483b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 14493b3a8eb9SGleb Smirnoff xdel++; 14503b3a8eb9SGleb Smirnoff } 14513b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 14523b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 14533b3a8eb9SGleb Smirnoff if (ticket != NULL) 14543b3a8eb9SGleb Smirnoff *ticket = ++rs->tticket; 14553b3a8eb9SGleb Smirnoff rs->topen = 1; 14563b3a8eb9SGleb Smirnoff } else 1457e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 14583b3a8eb9SGleb Smirnoff if (ndel != NULL) 14593b3a8eb9SGleb Smirnoff *ndel = xdel; 14603b3a8eb9SGleb Smirnoff return (0); 14613b3a8eb9SGleb Smirnoff } 14623b3a8eb9SGleb Smirnoff 14633b3a8eb9SGleb Smirnoff int 14643b3a8eb9SGleb Smirnoff pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size, 14653b3a8eb9SGleb Smirnoff int *nadd, int *naddr, u_int32_t ticket, int flags) 14663b3a8eb9SGleb Smirnoff { 14673b3a8eb9SGleb Smirnoff struct pfr_ktableworkq tableq; 14683b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 14693b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *rt, *shadow, key; 14703b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 14713b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 1472e86bddeaSKristof Provost struct pf_kruleset *rs; 14733b3a8eb9SGleb Smirnoff int i, rv, xadd = 0, xaddr = 0; 14743b3a8eb9SGleb Smirnoff 14753b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 14763b3a8eb9SGleb Smirnoff 14773b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO); 14783b3a8eb9SGleb Smirnoff if (size && !(flags & PFR_FLAG_ADDRSTOO)) 14793b3a8eb9SGleb Smirnoff return (EINVAL); 14803b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK, 14813b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 14823b3a8eb9SGleb Smirnoff return (EINVAL); 1483e86bddeaSKristof Provost rs = pf_find_kruleset(tbl->pfrt_anchor); 14843b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 14853b3a8eb9SGleb Smirnoff return (EBUSY); 14863b3a8eb9SGleb Smirnoff tbl->pfrt_flags |= PFR_TFLAG_INACTIVE; 14873b3a8eb9SGleb Smirnoff SLIST_INIT(&tableq); 14881e9e3741SMarko Zec kt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, (struct pfr_ktable *)tbl); 14893b3a8eb9SGleb Smirnoff if (kt == NULL) { 14903b3a8eb9SGleb Smirnoff kt = pfr_create_ktable(tbl, 0, 1); 14913b3a8eb9SGleb Smirnoff if (kt == NULL) 14923b3a8eb9SGleb Smirnoff return (ENOMEM); 14933b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq); 14943b3a8eb9SGleb Smirnoff xadd++; 14953b3a8eb9SGleb Smirnoff if (!tbl->pfrt_anchor[0]) 14963b3a8eb9SGleb Smirnoff goto _skip; 14973b3a8eb9SGleb Smirnoff 14983b3a8eb9SGleb Smirnoff /* find or create root table */ 14993b3a8eb9SGleb Smirnoff bzero(&key, sizeof(key)); 15003b3a8eb9SGleb Smirnoff strlcpy(key.pfrkt_name, tbl->pfrt_name, sizeof(key.pfrkt_name)); 15011e9e3741SMarko Zec rt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 15023b3a8eb9SGleb Smirnoff if (rt != NULL) { 15033b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 15043b3a8eb9SGleb Smirnoff goto _skip; 15053b3a8eb9SGleb Smirnoff } 15063b3a8eb9SGleb Smirnoff rt = pfr_create_ktable(&key.pfrkt_t, 0, 1); 15073b3a8eb9SGleb Smirnoff if (rt == NULL) { 15083b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15093b3a8eb9SGleb Smirnoff return (ENOMEM); 15103b3a8eb9SGleb Smirnoff } 15113b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq); 15123b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 15133b3a8eb9SGleb Smirnoff } else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE)) 15143b3a8eb9SGleb Smirnoff xadd++; 15153b3a8eb9SGleb Smirnoff _skip: 15163b3a8eb9SGleb Smirnoff shadow = pfr_create_ktable(tbl, 0, 0); 15173b3a8eb9SGleb Smirnoff if (shadow == NULL) { 15183b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15193b3a8eb9SGleb Smirnoff return (ENOMEM); 15203b3a8eb9SGleb Smirnoff } 15213b3a8eb9SGleb Smirnoff SLIST_INIT(&addrq); 15223b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 15233b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 15243b3a8eb9SGleb Smirnoff senderr(EINVAL); 15253b3a8eb9SGleb Smirnoff if (pfr_lookup_addr(shadow, ad, 1) != NULL) 15263b3a8eb9SGleb Smirnoff continue; 152721121f9bSMark Johnston p = pfr_create_kentry(ad, 152821121f9bSMark Johnston (shadow->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0); 15293b3a8eb9SGleb Smirnoff if (p == NULL) 15303b3a8eb9SGleb Smirnoff senderr(ENOMEM); 15313b3a8eb9SGleb Smirnoff if (pfr_route_kentry(shadow, p)) { 15323b3a8eb9SGleb Smirnoff pfr_destroy_kentry(p); 15333b3a8eb9SGleb Smirnoff continue; 15343b3a8eb9SGleb Smirnoff } 15353b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addrq, p, pfrke_workq); 15363b3a8eb9SGleb Smirnoff xaddr++; 15373b3a8eb9SGleb Smirnoff } 15383b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 15393b3a8eb9SGleb Smirnoff if (kt->pfrkt_shadow != NULL) 15403b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, 1); 15413b3a8eb9SGleb Smirnoff kt->pfrkt_flags |= PFR_TFLAG_INACTIVE; 15423b3a8eb9SGleb Smirnoff pfr_insert_ktables(&tableq); 15433b3a8eb9SGleb Smirnoff shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ? 15443b3a8eb9SGleb Smirnoff xaddr : NO_ADDRESSES; 15453b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = shadow; 15463b3a8eb9SGleb Smirnoff } else { 15473b3a8eb9SGleb Smirnoff pfr_clean_node_mask(shadow, &addrq); 15483b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 15493b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15503b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 15513b3a8eb9SGleb Smirnoff } 15523b3a8eb9SGleb Smirnoff if (nadd != NULL) 15533b3a8eb9SGleb Smirnoff *nadd = xadd; 15543b3a8eb9SGleb Smirnoff if (naddr != NULL) 15553b3a8eb9SGleb Smirnoff *naddr = xaddr; 15563b3a8eb9SGleb Smirnoff return (0); 15573b3a8eb9SGleb Smirnoff _bad: 15583b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 15593b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15603b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 15613b3a8eb9SGleb Smirnoff return (rv); 15623b3a8eb9SGleb Smirnoff } 15633b3a8eb9SGleb Smirnoff 15643b3a8eb9SGleb Smirnoff int 15653b3a8eb9SGleb Smirnoff pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags) 15663b3a8eb9SGleb Smirnoff { 15673b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 15683b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 1569e86bddeaSKristof Provost struct pf_kruleset *rs; 15703b3a8eb9SGleb Smirnoff int xdel = 0; 15713b3a8eb9SGleb Smirnoff 15723b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 15733b3a8eb9SGleb Smirnoff 15743b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1575e86bddeaSKristof Provost rs = pf_find_kruleset(trs->pfrt_anchor); 15763b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 15773b3a8eb9SGleb Smirnoff return (0); 15783b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 15791e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 15803b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 15813b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 15823b3a8eb9SGleb Smirnoff continue; 15833b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE; 15843b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 15853b3a8eb9SGleb Smirnoff xdel++; 15863b3a8eb9SGleb Smirnoff } 15873b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 15883b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 15893b3a8eb9SGleb Smirnoff rs->topen = 0; 1590e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 15913b3a8eb9SGleb Smirnoff } 15923b3a8eb9SGleb Smirnoff if (ndel != NULL) 15933b3a8eb9SGleb Smirnoff *ndel = xdel; 15943b3a8eb9SGleb Smirnoff return (0); 15953b3a8eb9SGleb Smirnoff } 15963b3a8eb9SGleb Smirnoff 15973b3a8eb9SGleb Smirnoff int 15983b3a8eb9SGleb Smirnoff pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd, 15993b3a8eb9SGleb Smirnoff int *nchange, int flags) 16003b3a8eb9SGleb Smirnoff { 16013b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 16023b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 1603e86bddeaSKristof Provost struct pf_kruleset *rs; 16043b3a8eb9SGleb Smirnoff int xadd = 0, xchange = 0; 16053b3a8eb9SGleb Smirnoff long tzero = time_second; 16063b3a8eb9SGleb Smirnoff 16073b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 16083b3a8eb9SGleb Smirnoff 16093b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1610e86bddeaSKristof Provost rs = pf_find_kruleset(trs->pfrt_anchor); 16113b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 16123b3a8eb9SGleb Smirnoff return (EBUSY); 16133b3a8eb9SGleb Smirnoff 16143b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 16151e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 16163b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 16173b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 16183b3a8eb9SGleb Smirnoff continue; 16193b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 16203b3a8eb9SGleb Smirnoff if (p->pfrkt_flags & PFR_TFLAG_ACTIVE) 16213b3a8eb9SGleb Smirnoff xchange++; 16223b3a8eb9SGleb Smirnoff else 16233b3a8eb9SGleb Smirnoff xadd++; 16243b3a8eb9SGleb Smirnoff } 16253b3a8eb9SGleb Smirnoff 16263b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 16273b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(&workq); p != NULL; p = q) { 16283b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 16293b3a8eb9SGleb Smirnoff pfr_commit_ktable(p, tzero); 16303b3a8eb9SGleb Smirnoff } 16313b3a8eb9SGleb Smirnoff rs->topen = 0; 1632e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 16333b3a8eb9SGleb Smirnoff } 16343b3a8eb9SGleb Smirnoff if (nadd != NULL) 16353b3a8eb9SGleb Smirnoff *nadd = xadd; 16363b3a8eb9SGleb Smirnoff if (nchange != NULL) 16373b3a8eb9SGleb Smirnoff *nchange = xchange; 16383b3a8eb9SGleb Smirnoff 16393b3a8eb9SGleb Smirnoff return (0); 16403b3a8eb9SGleb Smirnoff } 16413b3a8eb9SGleb Smirnoff 16423b3a8eb9SGleb Smirnoff static void 16433b3a8eb9SGleb Smirnoff pfr_commit_ktable(struct pfr_ktable *kt, long tzero) 16443b3a8eb9SGleb Smirnoff { 1645e6aed06fSMark Johnston counter_u64_t *pkc, *qkc; 16463b3a8eb9SGleb Smirnoff struct pfr_ktable *shadow = kt->pfrkt_shadow; 16473b3a8eb9SGleb Smirnoff int nflags; 16483b3a8eb9SGleb Smirnoff 16493b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 16503b3a8eb9SGleb Smirnoff 16513b3a8eb9SGleb Smirnoff if (shadow->pfrkt_cnt == NO_ADDRESSES) { 16523b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 16533b3a8eb9SGleb Smirnoff pfr_clstats_ktable(kt, tzero, 1); 16543b3a8eb9SGleb Smirnoff } else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) { 16553b3a8eb9SGleb Smirnoff /* kt might contain addresses */ 16563b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq, addq, changeq, delq, garbageq; 16573b3a8eb9SGleb Smirnoff struct pfr_kentry *p, *q, *next; 16583b3a8eb9SGleb Smirnoff struct pfr_addr ad; 16593b3a8eb9SGleb Smirnoff 16603b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(shadow, &addrq, NULL, 0); 16613b3a8eb9SGleb Smirnoff pfr_mark_addrs(kt); 16623b3a8eb9SGleb Smirnoff SLIST_INIT(&addq); 16633b3a8eb9SGleb Smirnoff SLIST_INIT(&changeq); 16643b3a8eb9SGleb Smirnoff SLIST_INIT(&delq); 16653b3a8eb9SGleb Smirnoff SLIST_INIT(&garbageq); 16663b3a8eb9SGleb Smirnoff pfr_clean_node_mask(shadow, &addrq); 1667e6aed06fSMark Johnston SLIST_FOREACH_SAFE(p, &addrq, pfrke_workq, next) { 16683b3a8eb9SGleb Smirnoff pfr_copyout_addr(&ad, p); 16693b3a8eb9SGleb Smirnoff q = pfr_lookup_addr(kt, &ad, 1); 16703b3a8eb9SGleb Smirnoff if (q != NULL) { 16713b3a8eb9SGleb Smirnoff if (q->pfrke_not != p->pfrke_not) 16723b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&changeq, q, 16733b3a8eb9SGleb Smirnoff pfrke_workq); 1674e6aed06fSMark Johnston pkc = &p->pfrke_counters.pfrkc_counters; 1675e6aed06fSMark Johnston qkc = &q->pfrke_counters.pfrkc_counters; 1676e6aed06fSMark Johnston if ((*pkc == NULL) != (*qkc == NULL)) 1677e6aed06fSMark Johnston SWAP(counter_u64_t, *pkc, *qkc); 16783b3a8eb9SGleb Smirnoff q->pfrke_mark = 1; 16793b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq); 16803b3a8eb9SGleb Smirnoff } else { 168159048686SKristof Provost p->pfrke_counters.pfrkc_tzero = tzero; 16823b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, p, pfrke_workq); 16833b3a8eb9SGleb Smirnoff } 16843b3a8eb9SGleb Smirnoff } 16853b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY); 16863b3a8eb9SGleb Smirnoff pfr_insert_kentries(kt, &addq, tzero); 16873b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &delq); 168821121f9bSMark Johnston pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG); 16893b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&garbageq); 16903b3a8eb9SGleb Smirnoff } else { 16913b3a8eb9SGleb Smirnoff /* kt cannot contain addresses */ 16923b3a8eb9SGleb Smirnoff SWAP(struct radix_node_head *, kt->pfrkt_ip4, 16933b3a8eb9SGleb Smirnoff shadow->pfrkt_ip4); 16943b3a8eb9SGleb Smirnoff SWAP(struct radix_node_head *, kt->pfrkt_ip6, 16953b3a8eb9SGleb Smirnoff shadow->pfrkt_ip6); 16963b3a8eb9SGleb Smirnoff SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt); 16973b3a8eb9SGleb Smirnoff pfr_clstats_ktable(kt, tzero, 1); 16983b3a8eb9SGleb Smirnoff } 16993b3a8eb9SGleb Smirnoff nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) | 17003b3a8eb9SGleb Smirnoff (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE) 17013b3a8eb9SGleb Smirnoff & ~PFR_TFLAG_INACTIVE; 17023b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 17033b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = NULL; 17043b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, nflags); 17053b3a8eb9SGleb Smirnoff } 17063b3a8eb9SGleb Smirnoff 17073b3a8eb9SGleb Smirnoff static int 17083b3a8eb9SGleb Smirnoff pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved) 17093b3a8eb9SGleb Smirnoff { 17103b3a8eb9SGleb Smirnoff int i; 17113b3a8eb9SGleb Smirnoff 17123b3a8eb9SGleb Smirnoff if (!tbl->pfrt_name[0]) 17133b3a8eb9SGleb Smirnoff return (-1); 17143b3a8eb9SGleb Smirnoff if (no_reserved && !strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR)) 17153b3a8eb9SGleb Smirnoff return (-1); 17163b3a8eb9SGleb Smirnoff if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1]) 17173b3a8eb9SGleb Smirnoff return (-1); 17183b3a8eb9SGleb Smirnoff for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++) 17193b3a8eb9SGleb Smirnoff if (tbl->pfrt_name[i]) 17203b3a8eb9SGleb Smirnoff return (-1); 17213b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(tbl->pfrt_anchor)) 17223b3a8eb9SGleb Smirnoff return (-1); 17233b3a8eb9SGleb Smirnoff if (tbl->pfrt_flags & ~allowedflags) 17243b3a8eb9SGleb Smirnoff return (-1); 17253b3a8eb9SGleb Smirnoff return (0); 17263b3a8eb9SGleb Smirnoff } 17273b3a8eb9SGleb Smirnoff 17283b3a8eb9SGleb Smirnoff /* 17293b3a8eb9SGleb Smirnoff * Rewrite anchors referenced by tables to remove slashes 17303b3a8eb9SGleb Smirnoff * and check for validity. 17313b3a8eb9SGleb Smirnoff */ 17323b3a8eb9SGleb Smirnoff static int 17333b3a8eb9SGleb Smirnoff pfr_fix_anchor(char *anchor) 17343b3a8eb9SGleb Smirnoff { 17353b3a8eb9SGleb Smirnoff size_t siz = MAXPATHLEN; 17363b3a8eb9SGleb Smirnoff int i; 17373b3a8eb9SGleb Smirnoff 17383b3a8eb9SGleb Smirnoff if (anchor[0] == '/') { 17393b3a8eb9SGleb Smirnoff char *path; 17403b3a8eb9SGleb Smirnoff int off; 17413b3a8eb9SGleb Smirnoff 17423b3a8eb9SGleb Smirnoff path = anchor; 17433b3a8eb9SGleb Smirnoff off = 1; 17443b3a8eb9SGleb Smirnoff while (*++path == '/') 17453b3a8eb9SGleb Smirnoff off++; 17463b3a8eb9SGleb Smirnoff bcopy(path, anchor, siz - off); 17473b3a8eb9SGleb Smirnoff memset(anchor + siz - off, 0, off); 17483b3a8eb9SGleb Smirnoff } 17493b3a8eb9SGleb Smirnoff if (anchor[siz - 1]) 17503b3a8eb9SGleb Smirnoff return (-1); 17513b3a8eb9SGleb Smirnoff for (i = strlen(anchor); i < siz; i++) 17523b3a8eb9SGleb Smirnoff if (anchor[i]) 17533b3a8eb9SGleb Smirnoff return (-1); 17543b3a8eb9SGleb Smirnoff return (0); 17553b3a8eb9SGleb Smirnoff } 17563b3a8eb9SGleb Smirnoff 1757adfe2f6aSKristof Provost int 17583b3a8eb9SGleb Smirnoff pfr_table_count(struct pfr_table *filter, int flags) 17593b3a8eb9SGleb Smirnoff { 1760e86bddeaSKristof Provost struct pf_kruleset *rs; 17613b3a8eb9SGleb Smirnoff 17623b3a8eb9SGleb Smirnoff PF_RULES_ASSERT(); 17633b3a8eb9SGleb Smirnoff 17643b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_ALLRSETS) 17651e9e3741SMarko Zec return (V_pfr_ktable_cnt); 17663b3a8eb9SGleb Smirnoff if (filter->pfrt_anchor[0]) { 1767e86bddeaSKristof Provost rs = pf_find_kruleset(filter->pfrt_anchor); 17683b3a8eb9SGleb Smirnoff return ((rs != NULL) ? rs->tables : -1); 17693b3a8eb9SGleb Smirnoff } 17703b3a8eb9SGleb Smirnoff return (pf_main_ruleset.tables); 17713b3a8eb9SGleb Smirnoff } 17723b3a8eb9SGleb Smirnoff 17733b3a8eb9SGleb Smirnoff static int 17743b3a8eb9SGleb Smirnoff pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags) 17753b3a8eb9SGleb Smirnoff { 17763b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_ALLRSETS) 17773b3a8eb9SGleb Smirnoff return (0); 17783b3a8eb9SGleb Smirnoff if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor)) 17793b3a8eb9SGleb Smirnoff return (1); 17803b3a8eb9SGleb Smirnoff return (0); 17813b3a8eb9SGleb Smirnoff } 17823b3a8eb9SGleb Smirnoff 17833b3a8eb9SGleb Smirnoff static void 17843b3a8eb9SGleb Smirnoff pfr_insert_ktables(struct pfr_ktableworkq *workq) 17853b3a8eb9SGleb Smirnoff { 17863b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 17873b3a8eb9SGleb Smirnoff 17883b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrkt_workq) 17893b3a8eb9SGleb Smirnoff pfr_insert_ktable(p); 17903b3a8eb9SGleb Smirnoff } 17913b3a8eb9SGleb Smirnoff 17923b3a8eb9SGleb Smirnoff static void 17933b3a8eb9SGleb Smirnoff pfr_insert_ktable(struct pfr_ktable *kt) 17943b3a8eb9SGleb Smirnoff { 17953b3a8eb9SGleb Smirnoff 17963b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 17973b3a8eb9SGleb Smirnoff 17981e9e3741SMarko Zec RB_INSERT(pfr_ktablehead, &V_pfr_ktables, kt); 17991e9e3741SMarko Zec V_pfr_ktable_cnt++; 18003b3a8eb9SGleb Smirnoff if (kt->pfrkt_root != NULL) 18013b3a8eb9SGleb Smirnoff if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++) 18023b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt->pfrkt_root, 18033b3a8eb9SGleb Smirnoff kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR); 18043b3a8eb9SGleb Smirnoff } 18053b3a8eb9SGleb Smirnoff 18063b3a8eb9SGleb Smirnoff static void 18073b3a8eb9SGleb Smirnoff pfr_setflags_ktables(struct pfr_ktableworkq *workq) 18083b3a8eb9SGleb Smirnoff { 18093b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 18103b3a8eb9SGleb Smirnoff 18113b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(workq); p; p = q) { 18123b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 18133b3a8eb9SGleb Smirnoff pfr_setflags_ktable(p, p->pfrkt_nflags); 18143b3a8eb9SGleb Smirnoff } 18153b3a8eb9SGleb Smirnoff } 18163b3a8eb9SGleb Smirnoff 18173b3a8eb9SGleb Smirnoff static void 18183b3a8eb9SGleb Smirnoff pfr_setflags_ktable(struct pfr_ktable *kt, int newf) 18193b3a8eb9SGleb Smirnoff { 18203b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 18213b3a8eb9SGleb Smirnoff 18223b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 18233b3a8eb9SGleb Smirnoff 18243b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_REFERENCED) && 182587e4ca37SKristof Provost !(newf & PFR_TFLAG_REFDANCHOR) && 18263b3a8eb9SGleb Smirnoff !(newf & PFR_TFLAG_PERSIST)) 18273b3a8eb9SGleb Smirnoff newf &= ~PFR_TFLAG_ACTIVE; 18283b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_ACTIVE)) 18293b3a8eb9SGleb Smirnoff newf &= ~PFR_TFLAG_USRMASK; 18303b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_SETMASK)) { 18311e9e3741SMarko Zec RB_REMOVE(pfr_ktablehead, &V_pfr_ktables, kt); 18323b3a8eb9SGleb Smirnoff if (kt->pfrkt_root != NULL) 18333b3a8eb9SGleb Smirnoff if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]) 18343b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt->pfrkt_root, 18353b3a8eb9SGleb Smirnoff kt->pfrkt_root->pfrkt_flags & 18363b3a8eb9SGleb Smirnoff ~PFR_TFLAG_REFDANCHOR); 18373b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 1); 18381e9e3741SMarko Zec V_pfr_ktable_cnt--; 18393b3a8eb9SGleb Smirnoff return; 18403b3a8eb9SGleb Smirnoff } 18413b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) { 18423b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 18433b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &addrq); 18443b3a8eb9SGleb Smirnoff } 18453b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) { 18463b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, 1); 18473b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = NULL; 18483b3a8eb9SGleb Smirnoff } 18493b3a8eb9SGleb Smirnoff kt->pfrkt_flags = newf; 18503b3a8eb9SGleb Smirnoff } 18513b3a8eb9SGleb Smirnoff 18523b3a8eb9SGleb Smirnoff static void 18533b3a8eb9SGleb Smirnoff pfr_clstats_ktables(struct pfr_ktableworkq *workq, long tzero, int recurse) 18543b3a8eb9SGleb Smirnoff { 18553b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 18563b3a8eb9SGleb Smirnoff 18573b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrkt_workq) 18583b3a8eb9SGleb Smirnoff pfr_clstats_ktable(p, tzero, recurse); 18593b3a8eb9SGleb Smirnoff } 18603b3a8eb9SGleb Smirnoff 18613b3a8eb9SGleb Smirnoff static void 18623b3a8eb9SGleb Smirnoff pfr_clstats_ktable(struct pfr_ktable *kt, long tzero, int recurse) 18633b3a8eb9SGleb Smirnoff { 18643b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 186559048686SKristof Provost int pfr_dir, pfr_op; 18663b3a8eb9SGleb Smirnoff 18673b3a8eb9SGleb Smirnoff if (recurse) { 18683b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 186921121f9bSMark Johnston pfr_clstats_kentries(kt, &addrq, tzero, 0); 18703b3a8eb9SGleb Smirnoff } 187159048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 187259048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 187359048686SKristof Provost counter_u64_zero(kt->pfrkt_packets[pfr_dir][pfr_op]); 187459048686SKristof Provost counter_u64_zero(kt->pfrkt_bytes[pfr_dir][pfr_op]); 187559048686SKristof Provost } 187659048686SKristof Provost } 187759048686SKristof Provost counter_u64_zero(kt->pfrkt_match); 187859048686SKristof Provost counter_u64_zero(kt->pfrkt_nomatch); 18793b3a8eb9SGleb Smirnoff kt->pfrkt_tzero = tzero; 18803b3a8eb9SGleb Smirnoff } 18813b3a8eb9SGleb Smirnoff 18823b3a8eb9SGleb Smirnoff static struct pfr_ktable * 18833b3a8eb9SGleb Smirnoff pfr_create_ktable(struct pfr_table *tbl, long tzero, int attachruleset) 18843b3a8eb9SGleb Smirnoff { 18853b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 1886e86bddeaSKristof Provost struct pf_kruleset *rs; 188759048686SKristof Provost int pfr_dir, pfr_op; 18883b3a8eb9SGleb Smirnoff 18893b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 18903b3a8eb9SGleb Smirnoff 18913b3a8eb9SGleb Smirnoff kt = malloc(sizeof(*kt), M_PFTABLE, M_NOWAIT|M_ZERO); 18923b3a8eb9SGleb Smirnoff if (kt == NULL) 18933b3a8eb9SGleb Smirnoff return (NULL); 18943b3a8eb9SGleb Smirnoff kt->pfrkt_t = *tbl; 18953b3a8eb9SGleb Smirnoff 18963b3a8eb9SGleb Smirnoff if (attachruleset) { 1897e86bddeaSKristof Provost rs = pf_find_or_create_kruleset(tbl->pfrt_anchor); 18983b3a8eb9SGleb Smirnoff if (!rs) { 18993b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 19003b3a8eb9SGleb Smirnoff return (NULL); 19013b3a8eb9SGleb Smirnoff } 19023b3a8eb9SGleb Smirnoff kt->pfrkt_rs = rs; 19033b3a8eb9SGleb Smirnoff rs->tables++; 19043b3a8eb9SGleb Smirnoff } 19053b3a8eb9SGleb Smirnoff 190659048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 190759048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 190859048686SKristof Provost kt->pfrkt_packets[pfr_dir][pfr_op] = 190959048686SKristof Provost counter_u64_alloc(M_NOWAIT); 191059048686SKristof Provost if (! kt->pfrkt_packets[pfr_dir][pfr_op]) { 191159048686SKristof Provost pfr_destroy_ktable(kt, 0); 191259048686SKristof Provost return (NULL); 191359048686SKristof Provost } 191459048686SKristof Provost kt->pfrkt_bytes[pfr_dir][pfr_op] = 191559048686SKristof Provost counter_u64_alloc(M_NOWAIT); 191659048686SKristof Provost if (! kt->pfrkt_bytes[pfr_dir][pfr_op]) { 191759048686SKristof Provost pfr_destroy_ktable(kt, 0); 191859048686SKristof Provost return (NULL); 191959048686SKristof Provost } 192059048686SKristof Provost } 192159048686SKristof Provost } 192259048686SKristof Provost kt->pfrkt_match = counter_u64_alloc(M_NOWAIT); 192359048686SKristof Provost if (! kt->pfrkt_match) { 192459048686SKristof Provost pfr_destroy_ktable(kt, 0); 192559048686SKristof Provost return (NULL); 192659048686SKristof Provost } 192759048686SKristof Provost 192859048686SKristof Provost kt->pfrkt_nomatch = counter_u64_alloc(M_NOWAIT); 192959048686SKristof Provost if (! kt->pfrkt_nomatch) { 193059048686SKristof Provost pfr_destroy_ktable(kt, 0); 193159048686SKristof Provost return (NULL); 193259048686SKristof Provost } 193359048686SKristof Provost 19343b3a8eb9SGleb Smirnoff if (!rn_inithead((void **)&kt->pfrkt_ip4, 19353b3a8eb9SGleb Smirnoff offsetof(struct sockaddr_in, sin_addr) * 8) || 19363b3a8eb9SGleb Smirnoff !rn_inithead((void **)&kt->pfrkt_ip6, 19373b3a8eb9SGleb Smirnoff offsetof(struct sockaddr_in6, sin6_addr) * 8)) { 19383b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 19393b3a8eb9SGleb Smirnoff return (NULL); 19403b3a8eb9SGleb Smirnoff } 19413b3a8eb9SGleb Smirnoff kt->pfrkt_tzero = tzero; 19423b3a8eb9SGleb Smirnoff 19433b3a8eb9SGleb Smirnoff return (kt); 19443b3a8eb9SGleb Smirnoff } 19453b3a8eb9SGleb Smirnoff 19463b3a8eb9SGleb Smirnoff static void 19473b3a8eb9SGleb Smirnoff pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr) 19483b3a8eb9SGleb Smirnoff { 19493b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 19503b3a8eb9SGleb Smirnoff 19513b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(workq); p; p = q) { 19523b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 19533b3a8eb9SGleb Smirnoff pfr_destroy_ktable(p, flushaddr); 19543b3a8eb9SGleb Smirnoff } 19553b3a8eb9SGleb Smirnoff } 19563b3a8eb9SGleb Smirnoff 19573b3a8eb9SGleb Smirnoff static void 19583b3a8eb9SGleb Smirnoff pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr) 19593b3a8eb9SGleb Smirnoff { 19603b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 196159048686SKristof Provost int pfr_dir, pfr_op; 19623b3a8eb9SGleb Smirnoff 19633b3a8eb9SGleb Smirnoff if (flushaddr) { 19643b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 19653b3a8eb9SGleb Smirnoff pfr_clean_node_mask(kt, &addrq); 19663b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 19673b3a8eb9SGleb Smirnoff } 196831f0d081SAlexander V. Chernikov if (kt->pfrkt_ip4 != NULL) 1969495a22b5SGleb Smirnoff rn_detachhead((void **)&kt->pfrkt_ip4); 197031f0d081SAlexander V. Chernikov if (kt->pfrkt_ip6 != NULL) 1971495a22b5SGleb Smirnoff rn_detachhead((void **)&kt->pfrkt_ip6); 19723b3a8eb9SGleb Smirnoff if (kt->pfrkt_shadow != NULL) 19733b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr); 19743b3a8eb9SGleb Smirnoff if (kt->pfrkt_rs != NULL) { 19753b3a8eb9SGleb Smirnoff kt->pfrkt_rs->tables--; 1976e86bddeaSKristof Provost pf_remove_if_empty_kruleset(kt->pfrkt_rs); 19773b3a8eb9SGleb Smirnoff } 197859048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 197959048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 198059048686SKristof Provost counter_u64_free(kt->pfrkt_packets[pfr_dir][pfr_op]); 198159048686SKristof Provost counter_u64_free(kt->pfrkt_bytes[pfr_dir][pfr_op]); 198259048686SKristof Provost } 198359048686SKristof Provost } 198459048686SKristof Provost counter_u64_free(kt->pfrkt_match); 198559048686SKristof Provost counter_u64_free(kt->pfrkt_nomatch); 198659048686SKristof Provost 19873b3a8eb9SGleb Smirnoff free(kt, M_PFTABLE); 19883b3a8eb9SGleb Smirnoff } 19893b3a8eb9SGleb Smirnoff 19903b3a8eb9SGleb Smirnoff static int 19913b3a8eb9SGleb Smirnoff pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q) 19923b3a8eb9SGleb Smirnoff { 19933b3a8eb9SGleb Smirnoff int d; 19943b3a8eb9SGleb Smirnoff 19953b3a8eb9SGleb Smirnoff if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE))) 19963b3a8eb9SGleb Smirnoff return (d); 19973b3a8eb9SGleb Smirnoff return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor)); 19983b3a8eb9SGleb Smirnoff } 19993b3a8eb9SGleb Smirnoff 20003b3a8eb9SGleb Smirnoff static struct pfr_ktable * 20013b3a8eb9SGleb Smirnoff pfr_lookup_table(struct pfr_table *tbl) 20023b3a8eb9SGleb Smirnoff { 20033b3a8eb9SGleb Smirnoff /* struct pfr_ktable start like a struct pfr_table */ 20041e9e3741SMarko Zec return (RB_FIND(pfr_ktablehead, &V_pfr_ktables, 20053b3a8eb9SGleb Smirnoff (struct pfr_ktable *)tbl)); 20063b3a8eb9SGleb Smirnoff } 20073b3a8eb9SGleb Smirnoff 20083b3a8eb9SGleb Smirnoff int 20093b3a8eb9SGleb Smirnoff pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af) 20103b3a8eb9SGleb Smirnoff { 20113b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = NULL; 20123b3a8eb9SGleb Smirnoff int match; 20133b3a8eb9SGleb Smirnoff 20143b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 20153b3a8eb9SGleb Smirnoff 20163b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 20173b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 20183b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 20193b3a8eb9SGleb Smirnoff return (0); 20203b3a8eb9SGleb Smirnoff 20213b3a8eb9SGleb Smirnoff switch (af) { 20223b3a8eb9SGleb Smirnoff #ifdef INET 20233b3a8eb9SGleb Smirnoff case AF_INET: 20243b3a8eb9SGleb Smirnoff { 20253b3a8eb9SGleb Smirnoff struct sockaddr_in sin; 20263b3a8eb9SGleb Smirnoff 20273b3a8eb9SGleb Smirnoff bzero(&sin, sizeof(sin)); 20283b3a8eb9SGleb Smirnoff sin.sin_len = sizeof(sin); 20293b3a8eb9SGleb Smirnoff sin.sin_family = AF_INET; 20303b3a8eb9SGleb Smirnoff sin.sin_addr.s_addr = a->addr32[0]; 203161eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh); 20323b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20333b3a8eb9SGleb Smirnoff ke = NULL; 20343b3a8eb9SGleb Smirnoff break; 20353b3a8eb9SGleb Smirnoff } 20363b3a8eb9SGleb Smirnoff #endif /* INET */ 20373b3a8eb9SGleb Smirnoff #ifdef INET6 20383b3a8eb9SGleb Smirnoff case AF_INET6: 20393b3a8eb9SGleb Smirnoff { 20403b3a8eb9SGleb Smirnoff struct sockaddr_in6 sin6; 20413b3a8eb9SGleb Smirnoff 20423b3a8eb9SGleb Smirnoff bzero(&sin6, sizeof(sin6)); 20433b3a8eb9SGleb Smirnoff sin6.sin6_len = sizeof(sin6); 20443b3a8eb9SGleb Smirnoff sin6.sin6_family = AF_INET6; 20453b3a8eb9SGleb Smirnoff bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 204661eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh); 20473b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20483b3a8eb9SGleb Smirnoff ke = NULL; 20493b3a8eb9SGleb Smirnoff break; 20503b3a8eb9SGleb Smirnoff } 20513b3a8eb9SGleb Smirnoff #endif /* INET6 */ 20523b3a8eb9SGleb Smirnoff } 20533b3a8eb9SGleb Smirnoff match = (ke && !ke->pfrke_not); 20543b3a8eb9SGleb Smirnoff if (match) 205559048686SKristof Provost counter_u64_add(kt->pfrkt_match, 1); 20563b3a8eb9SGleb Smirnoff else 205759048686SKristof Provost counter_u64_add(kt->pfrkt_nomatch, 1); 20583b3a8eb9SGleb Smirnoff return (match); 20593b3a8eb9SGleb Smirnoff } 20603b3a8eb9SGleb Smirnoff 20613b3a8eb9SGleb Smirnoff void 20623b3a8eb9SGleb Smirnoff pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af, 20633b3a8eb9SGleb Smirnoff u_int64_t len, int dir_out, int op_pass, int notrule) 20643b3a8eb9SGleb Smirnoff { 20653b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = NULL; 20663b3a8eb9SGleb Smirnoff 20673b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 20683b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 20693b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 20703b3a8eb9SGleb Smirnoff return; 20713b3a8eb9SGleb Smirnoff 20723b3a8eb9SGleb Smirnoff switch (af) { 20733b3a8eb9SGleb Smirnoff #ifdef INET 20743b3a8eb9SGleb Smirnoff case AF_INET: 20753b3a8eb9SGleb Smirnoff { 20763b3a8eb9SGleb Smirnoff struct sockaddr_in sin; 20773b3a8eb9SGleb Smirnoff 20787348c524SGleb Smirnoff bzero(&sin, sizeof(sin)); 20793b3a8eb9SGleb Smirnoff sin.sin_len = sizeof(sin); 20803b3a8eb9SGleb Smirnoff sin.sin_family = AF_INET; 20813b3a8eb9SGleb Smirnoff sin.sin_addr.s_addr = a->addr32[0]; 208261eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh); 20833b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20843b3a8eb9SGleb Smirnoff ke = NULL; 20853b3a8eb9SGleb Smirnoff break; 20863b3a8eb9SGleb Smirnoff } 20873b3a8eb9SGleb Smirnoff #endif /* INET */ 20883b3a8eb9SGleb Smirnoff #ifdef INET6 20893b3a8eb9SGleb Smirnoff case AF_INET6: 20903b3a8eb9SGleb Smirnoff { 20913b3a8eb9SGleb Smirnoff struct sockaddr_in6 sin6; 20923b3a8eb9SGleb Smirnoff 20937348c524SGleb Smirnoff bzero(&sin6, sizeof(sin6)); 20943b3a8eb9SGleb Smirnoff sin6.sin6_len = sizeof(sin6); 20953b3a8eb9SGleb Smirnoff sin6.sin6_family = AF_INET6; 20963b3a8eb9SGleb Smirnoff bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 209761eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh); 20983b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20993b3a8eb9SGleb Smirnoff ke = NULL; 21003b3a8eb9SGleb Smirnoff break; 21013b3a8eb9SGleb Smirnoff } 21023b3a8eb9SGleb Smirnoff #endif /* INET6 */ 21033b3a8eb9SGleb Smirnoff default: 21047348c524SGleb Smirnoff panic("%s: unknown address family %u", __func__, af); 21053b3a8eb9SGleb Smirnoff } 21063b3a8eb9SGleb Smirnoff if ((ke == NULL || ke->pfrke_not) != notrule) { 21073b3a8eb9SGleb Smirnoff if (op_pass != PFR_OP_PASS) 2108032dff66SKristof Provost DPFPRINTF(PF_DEBUG_URGENT, 2109032dff66SKristof Provost ("pfr_update_stats: assertion failed.\n")); 21103b3a8eb9SGleb Smirnoff op_pass = PFR_OP_XPASS; 21113b3a8eb9SGleb Smirnoff } 211259048686SKristof Provost counter_u64_add(kt->pfrkt_packets[dir_out][op_pass], 1); 211359048686SKristof Provost counter_u64_add(kt->pfrkt_bytes[dir_out][op_pass], len); 21143b3a8eb9SGleb Smirnoff if (ke != NULL && op_pass != PFR_OP_XPASS && 21153b3a8eb9SGleb Smirnoff (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) { 2116c1be8399SMark Johnston counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters, 2117c1be8399SMark Johnston dir_out, op_pass, PFR_TYPE_PACKETS), 1); 2118c1be8399SMark Johnston counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters, 2119c1be8399SMark Johnston dir_out, op_pass, PFR_TYPE_BYTES), len); 21203b3a8eb9SGleb Smirnoff } 21213b3a8eb9SGleb Smirnoff } 21223b3a8eb9SGleb Smirnoff 21233b3a8eb9SGleb Smirnoff struct pfr_ktable * 2124e86bddeaSKristof Provost pfr_attach_table(struct pf_kruleset *rs, char *name) 21253b3a8eb9SGleb Smirnoff { 21263b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *rt; 21273b3a8eb9SGleb Smirnoff struct pfr_table tbl; 2128e86bddeaSKristof Provost struct pf_kanchor *ac = rs->anchor; 21293b3a8eb9SGleb Smirnoff 21303b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 21313b3a8eb9SGleb Smirnoff 21323b3a8eb9SGleb Smirnoff bzero(&tbl, sizeof(tbl)); 21333b3a8eb9SGleb Smirnoff strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)); 21343b3a8eb9SGleb Smirnoff if (ac != NULL) 21353b3a8eb9SGleb Smirnoff strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor)); 21363b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(&tbl); 21373b3a8eb9SGleb Smirnoff if (kt == NULL) { 21383b3a8eb9SGleb Smirnoff kt = pfr_create_ktable(&tbl, time_second, 1); 21393b3a8eb9SGleb Smirnoff if (kt == NULL) 21403b3a8eb9SGleb Smirnoff return (NULL); 21413b3a8eb9SGleb Smirnoff if (ac != NULL) { 21423b3a8eb9SGleb Smirnoff bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor)); 21433b3a8eb9SGleb Smirnoff rt = pfr_lookup_table(&tbl); 21443b3a8eb9SGleb Smirnoff if (rt == NULL) { 21453b3a8eb9SGleb Smirnoff rt = pfr_create_ktable(&tbl, 0, 1); 21463b3a8eb9SGleb Smirnoff if (rt == NULL) { 21473b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 21483b3a8eb9SGleb Smirnoff return (NULL); 21493b3a8eb9SGleb Smirnoff } 21503b3a8eb9SGleb Smirnoff pfr_insert_ktable(rt); 21513b3a8eb9SGleb Smirnoff } 21523b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 21533b3a8eb9SGleb Smirnoff } 21543b3a8eb9SGleb Smirnoff pfr_insert_ktable(kt); 21553b3a8eb9SGleb Smirnoff } 21563b3a8eb9SGleb Smirnoff if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++) 21573b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED); 21583b3a8eb9SGleb Smirnoff return (kt); 21593b3a8eb9SGleb Smirnoff } 21603b3a8eb9SGleb Smirnoff 21613b3a8eb9SGleb Smirnoff void 21623b3a8eb9SGleb Smirnoff pfr_detach_table(struct pfr_ktable *kt) 21633b3a8eb9SGleb Smirnoff { 21643b3a8eb9SGleb Smirnoff 21653b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 21663b3a8eb9SGleb Smirnoff KASSERT(kt->pfrkt_refcnt[PFR_REFCNT_RULE] > 0, ("%s: refcount %d\n", 21673b3a8eb9SGleb Smirnoff __func__, kt->pfrkt_refcnt[PFR_REFCNT_RULE])); 21683b3a8eb9SGleb Smirnoff 21693b3a8eb9SGleb Smirnoff if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE]) 21703b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED); 21713b3a8eb9SGleb Smirnoff } 21723b3a8eb9SGleb Smirnoff 21733b3a8eb9SGleb Smirnoff int 21743b3a8eb9SGleb Smirnoff pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter, 21753b3a8eb9SGleb Smirnoff sa_family_t af) 21763b3a8eb9SGleb Smirnoff { 21773b3a8eb9SGleb Smirnoff struct pf_addr *addr, *cur, *mask; 21783b3a8eb9SGleb Smirnoff union sockaddr_union uaddr, umask; 21793b3a8eb9SGleb Smirnoff struct pfr_kentry *ke, *ke2 = NULL; 21803b3a8eb9SGleb Smirnoff int idx = -1, use_counter = 0; 21813b3a8eb9SGleb Smirnoff 21823b3a8eb9SGleb Smirnoff switch (af) { 21833b3a8eb9SGleb Smirnoff case AF_INET: 21843b3a8eb9SGleb Smirnoff uaddr.sin.sin_len = sizeof(struct sockaddr_in); 21853b3a8eb9SGleb Smirnoff uaddr.sin.sin_family = AF_INET; 21863b3a8eb9SGleb Smirnoff break; 21873b3a8eb9SGleb Smirnoff case AF_INET6: 21883b3a8eb9SGleb Smirnoff uaddr.sin6.sin6_len = sizeof(struct sockaddr_in6); 21893b3a8eb9SGleb Smirnoff uaddr.sin6.sin6_family = AF_INET6; 21903b3a8eb9SGleb Smirnoff break; 21913b3a8eb9SGleb Smirnoff } 21923b3a8eb9SGleb Smirnoff addr = SUNION2PF(&uaddr, af); 21933b3a8eb9SGleb Smirnoff 21943b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 21953b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 21963b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 21973b3a8eb9SGleb Smirnoff return (-1); 21983b3a8eb9SGleb Smirnoff 21993b3a8eb9SGleb Smirnoff if (pidx != NULL) 22003b3a8eb9SGleb Smirnoff idx = *pidx; 22013b3a8eb9SGleb Smirnoff if (counter != NULL && idx >= 0) 22023b3a8eb9SGleb Smirnoff use_counter = 1; 22033b3a8eb9SGleb Smirnoff if (idx < 0) 22043b3a8eb9SGleb Smirnoff idx = 0; 22053b3a8eb9SGleb Smirnoff 22063b3a8eb9SGleb Smirnoff _next_block: 22073b3a8eb9SGleb Smirnoff ke = pfr_kentry_byidx(kt, idx, af); 22083b3a8eb9SGleb Smirnoff if (ke == NULL) { 220959048686SKristof Provost counter_u64_add(kt->pfrkt_nomatch, 1); 22103b3a8eb9SGleb Smirnoff return (1); 22113b3a8eb9SGleb Smirnoff } 22123b3a8eb9SGleb Smirnoff pfr_prepare_network(&umask, af, ke->pfrke_net); 22133b3a8eb9SGleb Smirnoff cur = SUNION2PF(&ke->pfrke_sa, af); 22143b3a8eb9SGleb Smirnoff mask = SUNION2PF(&umask, af); 22153b3a8eb9SGleb Smirnoff 22163b3a8eb9SGleb Smirnoff if (use_counter) { 22173b3a8eb9SGleb Smirnoff /* is supplied address within block? */ 22183b3a8eb9SGleb Smirnoff if (!PF_MATCHA(0, cur, mask, counter, af)) { 22193b3a8eb9SGleb Smirnoff /* no, go to next block in table */ 22203b3a8eb9SGleb Smirnoff idx++; 22213b3a8eb9SGleb Smirnoff use_counter = 0; 22223b3a8eb9SGleb Smirnoff goto _next_block; 22233b3a8eb9SGleb Smirnoff } 22243b3a8eb9SGleb Smirnoff PF_ACPY(addr, counter, af); 22253b3a8eb9SGleb Smirnoff } else { 22263b3a8eb9SGleb Smirnoff /* use first address of block */ 22273b3a8eb9SGleb Smirnoff PF_ACPY(addr, cur, af); 22283b3a8eb9SGleb Smirnoff } 22293b3a8eb9SGleb Smirnoff 22303b3a8eb9SGleb Smirnoff if (!KENTRY_NETWORK(ke)) { 22313b3a8eb9SGleb Smirnoff /* this is a single IP address - no possible nested block */ 22323b3a8eb9SGleb Smirnoff PF_ACPY(counter, addr, af); 22333b3a8eb9SGleb Smirnoff *pidx = idx; 223459048686SKristof Provost counter_u64_add(kt->pfrkt_match, 1); 22353b3a8eb9SGleb Smirnoff return (0); 22363b3a8eb9SGleb Smirnoff } 22373b3a8eb9SGleb Smirnoff for (;;) { 22383b3a8eb9SGleb Smirnoff /* we don't want to use a nested block */ 22393b3a8eb9SGleb Smirnoff switch (af) { 22403b3a8eb9SGleb Smirnoff case AF_INET: 22413b3a8eb9SGleb Smirnoff ke2 = (struct pfr_kentry *)rn_match(&uaddr, 224261eee0e2SAlexander V. Chernikov &kt->pfrkt_ip4->rh); 22433b3a8eb9SGleb Smirnoff break; 22443b3a8eb9SGleb Smirnoff case AF_INET6: 22453b3a8eb9SGleb Smirnoff ke2 = (struct pfr_kentry *)rn_match(&uaddr, 224661eee0e2SAlexander V. Chernikov &kt->pfrkt_ip6->rh); 22473b3a8eb9SGleb Smirnoff break; 22483b3a8eb9SGleb Smirnoff } 22493b3a8eb9SGleb Smirnoff /* no need to check KENTRY_RNF_ROOT() here */ 22503b3a8eb9SGleb Smirnoff if (ke2 == ke) { 22513b3a8eb9SGleb Smirnoff /* lookup return the same block - perfect */ 22523b3a8eb9SGleb Smirnoff PF_ACPY(counter, addr, af); 22533b3a8eb9SGleb Smirnoff *pidx = idx; 225459048686SKristof Provost counter_u64_add(kt->pfrkt_match, 1); 22553b3a8eb9SGleb Smirnoff return (0); 22563b3a8eb9SGleb Smirnoff } 22573b3a8eb9SGleb Smirnoff 22583b3a8eb9SGleb Smirnoff /* we need to increase the counter past the nested block */ 22593b3a8eb9SGleb Smirnoff pfr_prepare_network(&umask, AF_INET, ke2->pfrke_net); 22603b3a8eb9SGleb Smirnoff PF_POOLMASK(addr, addr, SUNION2PF(&umask, af), &pfr_ffaddr, af); 22613b3a8eb9SGleb Smirnoff PF_AINC(addr, af); 22623b3a8eb9SGleb Smirnoff if (!PF_MATCHA(0, cur, mask, addr, af)) { 22633b3a8eb9SGleb Smirnoff /* ok, we reached the end of our main block */ 22643b3a8eb9SGleb Smirnoff /* go to next block in table */ 22653b3a8eb9SGleb Smirnoff idx++; 22663b3a8eb9SGleb Smirnoff use_counter = 0; 22673b3a8eb9SGleb Smirnoff goto _next_block; 22683b3a8eb9SGleb Smirnoff } 22693b3a8eb9SGleb Smirnoff } 22703b3a8eb9SGleb Smirnoff } 22713b3a8eb9SGleb Smirnoff 22723b3a8eb9SGleb Smirnoff static struct pfr_kentry * 22733b3a8eb9SGleb Smirnoff pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af) 22743b3a8eb9SGleb Smirnoff { 22753b3a8eb9SGleb Smirnoff struct pfr_walktree w; 22763b3a8eb9SGleb Smirnoff 22773b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 22783b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_POOL_GET; 22793b3a8eb9SGleb Smirnoff w.pfrw_cnt = idx; 22803b3a8eb9SGleb Smirnoff 22813b3a8eb9SGleb Smirnoff switch (af) { 22823b3a8eb9SGleb Smirnoff #ifdef INET 22833b3a8eb9SGleb Smirnoff case AF_INET: 228461eee0e2SAlexander V. Chernikov kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 22853b3a8eb9SGleb Smirnoff return (w.pfrw_kentry); 22863b3a8eb9SGleb Smirnoff #endif /* INET */ 22873b3a8eb9SGleb Smirnoff #ifdef INET6 22883b3a8eb9SGleb Smirnoff case AF_INET6: 228961eee0e2SAlexander V. Chernikov kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 22903b3a8eb9SGleb Smirnoff return (w.pfrw_kentry); 22913b3a8eb9SGleb Smirnoff #endif /* INET6 */ 22923b3a8eb9SGleb Smirnoff default: 22933b3a8eb9SGleb Smirnoff return (NULL); 22943b3a8eb9SGleb Smirnoff } 22953b3a8eb9SGleb Smirnoff } 22963b3a8eb9SGleb Smirnoff 22973b3a8eb9SGleb Smirnoff void 22983b3a8eb9SGleb Smirnoff pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn) 22993b3a8eb9SGleb Smirnoff { 23003b3a8eb9SGleb Smirnoff struct pfr_walktree w; 23013b3a8eb9SGleb Smirnoff 23023b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 23033b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_DYNADDR_UPDATE; 23043b3a8eb9SGleb Smirnoff w.pfrw_dyn = dyn; 23053b3a8eb9SGleb Smirnoff 23063b3a8eb9SGleb Smirnoff dyn->pfid_acnt4 = 0; 23073b3a8eb9SGleb Smirnoff dyn->pfid_acnt6 = 0; 23083b3a8eb9SGleb Smirnoff if (!dyn->pfid_af || dyn->pfid_af == AF_INET) 230961eee0e2SAlexander V. Chernikov kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 23103b3a8eb9SGleb Smirnoff if (!dyn->pfid_af || dyn->pfid_af == AF_INET6) 231161eee0e2SAlexander V. Chernikov kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 23123b3a8eb9SGleb Smirnoff } 2313