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, 106b21826bfSKristof Provost PFRW_DYNADDR_UPDATE, 107b21826bfSKristof Provost PFRW_COUNTERS 1083b3a8eb9SGleb Smirnoff } pfrw_op; 1093b3a8eb9SGleb Smirnoff union { 1103b3a8eb9SGleb Smirnoff struct pfr_addr *pfrw1_addr; 1113b3a8eb9SGleb Smirnoff struct pfr_astats *pfrw1_astats; 1123b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *pfrw1_workq; 1133b3a8eb9SGleb Smirnoff struct pfr_kentry *pfrw1_kentry; 1143b3a8eb9SGleb Smirnoff struct pfi_dynaddr *pfrw1_dyn; 1153b3a8eb9SGleb Smirnoff } pfrw_1; 1163b3a8eb9SGleb Smirnoff int pfrw_free; 11759048686SKristof Provost int pfrw_flags; 1183b3a8eb9SGleb Smirnoff }; 1193b3a8eb9SGleb Smirnoff #define pfrw_addr pfrw_1.pfrw1_addr 1203b3a8eb9SGleb Smirnoff #define pfrw_astats pfrw_1.pfrw1_astats 1213b3a8eb9SGleb Smirnoff #define pfrw_workq pfrw_1.pfrw1_workq 1223b3a8eb9SGleb Smirnoff #define pfrw_kentry pfrw_1.pfrw1_kentry 1233b3a8eb9SGleb Smirnoff #define pfrw_dyn pfrw_1.pfrw1_dyn 1243b3a8eb9SGleb Smirnoff #define pfrw_cnt pfrw_free 1253b3a8eb9SGleb Smirnoff 1263b3a8eb9SGleb Smirnoff #define senderr(e) do { rv = (e); goto _bad; } while (0) 1273b3a8eb9SGleb Smirnoff 1283b3a8eb9SGleb Smirnoff static MALLOC_DEFINE(M_PFTABLE, "pf_table", "pf(4) tables structures"); 1295f901c92SAndrew Turner VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_z); 1303b3a8eb9SGleb Smirnoff #define V_pfr_kentry_z VNET(pfr_kentry_z) 131c1be8399SMark Johnston VNET_DEFINE_STATIC(uma_zone_t, pfr_kentry_counter_z); 132c1be8399SMark Johnston #define V_pfr_kentry_counter_z VNET(pfr_kentry_counter_z) 1333b3a8eb9SGleb Smirnoff 1343b3a8eb9SGleb Smirnoff static struct pf_addr pfr_ffaddr = { 1353b3a8eb9SGleb Smirnoff .addr32 = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff } 1363b3a8eb9SGleb Smirnoff }; 1373b3a8eb9SGleb Smirnoff 13859048686SKristof Provost static void pfr_copyout_astats(struct pfr_astats *, 13959048686SKristof Provost const struct pfr_kentry *, 14059048686SKristof Provost const struct pfr_walktree *); 1413b3a8eb9SGleb Smirnoff static void pfr_copyout_addr(struct pfr_addr *, 14259048686SKristof Provost const struct pfr_kentry *ke); 1433b3a8eb9SGleb Smirnoff static int pfr_validate_addr(struct pfr_addr *); 1443b3a8eb9SGleb Smirnoff static void pfr_enqueue_addrs(struct pfr_ktable *, 1453b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *, int *, int); 1463b3a8eb9SGleb Smirnoff static void pfr_mark_addrs(struct pfr_ktable *); 1473b3a8eb9SGleb Smirnoff static struct pfr_kentry 1483b3a8eb9SGleb Smirnoff *pfr_lookup_addr(struct pfr_ktable *, 1493b3a8eb9SGleb Smirnoff struct pfr_addr *, int); 15021121f9bSMark Johnston static struct pfr_kentry *pfr_create_kentry(struct pfr_addr *, bool); 1513b3a8eb9SGleb Smirnoff static void pfr_destroy_kentries(struct pfr_kentryworkq *); 1523b3a8eb9SGleb Smirnoff static void pfr_destroy_kentry(struct pfr_kentry *); 1533b3a8eb9SGleb Smirnoff static void pfr_insert_kentries(struct pfr_ktable *, 1543b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *, long); 1553b3a8eb9SGleb Smirnoff static void pfr_remove_kentries(struct pfr_ktable *, 1563b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *); 15721121f9bSMark Johnston static void pfr_clstats_kentries(struct pfr_ktable *, 15821121f9bSMark Johnston struct pfr_kentryworkq *, long, int); 1593b3a8eb9SGleb Smirnoff static void pfr_reset_feedback(struct pfr_addr *, int); 1603b3a8eb9SGleb Smirnoff static void pfr_prepare_network(union sockaddr_union *, int, int); 1613b3a8eb9SGleb Smirnoff static int pfr_route_kentry(struct pfr_ktable *, 1623b3a8eb9SGleb Smirnoff struct pfr_kentry *); 1633b3a8eb9SGleb Smirnoff static int pfr_unroute_kentry(struct pfr_ktable *, 1643b3a8eb9SGleb Smirnoff struct pfr_kentry *); 1653b3a8eb9SGleb Smirnoff static int pfr_walktree(struct radix_node *, void *); 1663b3a8eb9SGleb Smirnoff static int pfr_validate_table(struct pfr_table *, int, int); 1673b3a8eb9SGleb Smirnoff static int pfr_fix_anchor(char *); 1683b3a8eb9SGleb Smirnoff static void pfr_commit_ktable(struct pfr_ktable *, long); 1693b3a8eb9SGleb Smirnoff static void pfr_insert_ktables(struct pfr_ktableworkq *); 1703b3a8eb9SGleb Smirnoff static void pfr_insert_ktable(struct pfr_ktable *); 1713b3a8eb9SGleb Smirnoff static void pfr_setflags_ktables(struct pfr_ktableworkq *); 1723b3a8eb9SGleb Smirnoff static void pfr_setflags_ktable(struct pfr_ktable *, int); 1733b3a8eb9SGleb Smirnoff static void pfr_clstats_ktables(struct pfr_ktableworkq *, long, 1743b3a8eb9SGleb Smirnoff int); 1753b3a8eb9SGleb Smirnoff static void pfr_clstats_ktable(struct pfr_ktable *, long, int); 1763b3a8eb9SGleb Smirnoff static struct pfr_ktable 1773b3a8eb9SGleb Smirnoff *pfr_create_ktable(struct pfr_table *, long, int); 1783b3a8eb9SGleb Smirnoff static void pfr_destroy_ktables(struct pfr_ktableworkq *, int); 1793b3a8eb9SGleb Smirnoff static void pfr_destroy_ktable(struct pfr_ktable *, int); 1803b3a8eb9SGleb Smirnoff static int pfr_ktable_compare(struct pfr_ktable *, 1813b3a8eb9SGleb Smirnoff struct pfr_ktable *); 1823b3a8eb9SGleb Smirnoff static struct pfr_ktable 1833b3a8eb9SGleb Smirnoff *pfr_lookup_table(struct pfr_table *); 1843b3a8eb9SGleb Smirnoff static void pfr_clean_node_mask(struct pfr_ktable *, 1853b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *); 1863b3a8eb9SGleb Smirnoff static int pfr_skip_table(struct pfr_table *, 1873b3a8eb9SGleb Smirnoff struct pfr_ktable *, int); 1883b3a8eb9SGleb Smirnoff static struct pfr_kentry 1893b3a8eb9SGleb Smirnoff *pfr_kentry_byidx(struct pfr_ktable *, int, int); 1903b3a8eb9SGleb Smirnoff 1913b3a8eb9SGleb Smirnoff static RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare); 1923b3a8eb9SGleb Smirnoff static RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare); 1933b3a8eb9SGleb Smirnoff 1945f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_ktablehead, pfr_ktables); 1951e9e3741SMarko Zec #define V_pfr_ktables VNET(pfr_ktables) 1961e9e3741SMarko Zec 1975f901c92SAndrew Turner VNET_DEFINE_STATIC(struct pfr_table, pfr_nulltable); 1981e9e3741SMarko Zec #define V_pfr_nulltable VNET(pfr_nulltable) 1991e9e3741SMarko Zec 2005f901c92SAndrew Turner VNET_DEFINE_STATIC(int, pfr_ktable_cnt); 2011e9e3741SMarko Zec #define V_pfr_ktable_cnt VNET(pfr_ktable_cnt) 2023b3a8eb9SGleb Smirnoff 2033b3a8eb9SGleb Smirnoff void 2043b3a8eb9SGleb Smirnoff pfr_initialize(void) 2053b3a8eb9SGleb Smirnoff { 2063b3a8eb9SGleb Smirnoff 207c1be8399SMark Johnston V_pfr_kentry_counter_z = uma_zcreate("pf table entry counters", 208c1be8399SMark Johnston PFR_NUM_COUNTERS * sizeof(uint64_t), NULL, NULL, NULL, NULL, 209c1be8399SMark Johnston UMA_ALIGN_PTR, UMA_ZONE_PCPU); 2103b3a8eb9SGleb Smirnoff V_pfr_kentry_z = uma_zcreate("pf table entries", 2113b3a8eb9SGleb Smirnoff sizeof(struct pfr_kentry), NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 2123b3a8eb9SGleb Smirnoff 0); 2137d1ab866SMark Johnston uma_zone_set_max(V_pfr_kentry_z, PFR_KENTRY_HIWAT); 2143b3a8eb9SGleb Smirnoff V_pf_limits[PF_LIMIT_TABLE_ENTRIES].zone = V_pfr_kentry_z; 2153b3a8eb9SGleb Smirnoff V_pf_limits[PF_LIMIT_TABLE_ENTRIES].limit = PFR_KENTRY_HIWAT; 2163b3a8eb9SGleb Smirnoff } 2173b3a8eb9SGleb Smirnoff 2183b3a8eb9SGleb Smirnoff void 2193b3a8eb9SGleb Smirnoff pfr_cleanup(void) 2203b3a8eb9SGleb Smirnoff { 2213b3a8eb9SGleb Smirnoff 2223b3a8eb9SGleb Smirnoff uma_zdestroy(V_pfr_kentry_z); 223c1be8399SMark Johnston uma_zdestroy(V_pfr_kentry_counter_z); 2243b3a8eb9SGleb Smirnoff } 2253b3a8eb9SGleb Smirnoff 2263b3a8eb9SGleb Smirnoff int 2273b3a8eb9SGleb Smirnoff pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags) 2283b3a8eb9SGleb Smirnoff { 2293b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 2303b3a8eb9SGleb Smirnoff struct pfr_kentryworkq workq; 2313b3a8eb9SGleb Smirnoff 2323b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 2333b3a8eb9SGleb Smirnoff 2343b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 2353b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL)) 2363b3a8eb9SGleb Smirnoff return (EINVAL); 2373b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 2383b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 2393b3a8eb9SGleb Smirnoff return (ESRCH); 2403b3a8eb9SGleb Smirnoff if (kt->pfrkt_flags & PFR_TFLAG_CONST) 2413b3a8eb9SGleb Smirnoff return (EPERM); 2423b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &workq, ndel, 0); 2433b3a8eb9SGleb Smirnoff 2443b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 2453b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &workq); 2463b3a8eb9SGleb Smirnoff KASSERT(kt->pfrkt_cnt == 0, ("%s: non-null pfrkt_cnt", __func__)); 2473b3a8eb9SGleb Smirnoff } 2483b3a8eb9SGleb Smirnoff return (0); 2493b3a8eb9SGleb Smirnoff } 2503b3a8eb9SGleb Smirnoff 2513b3a8eb9SGleb Smirnoff int 2523b3a8eb9SGleb Smirnoff pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 2533b3a8eb9SGleb Smirnoff int *nadd, int flags) 2543b3a8eb9SGleb Smirnoff { 2553b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *tmpkt; 2563b3a8eb9SGleb Smirnoff struct pfr_kentryworkq workq; 2573b3a8eb9SGleb Smirnoff struct pfr_kentry *p, *q; 2583b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 2593b3a8eb9SGleb Smirnoff int i, rv, xadd = 0; 2603b3a8eb9SGleb Smirnoff long tzero = time_second; 2613b3a8eb9SGleb Smirnoff 2623b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 2633b3a8eb9SGleb Smirnoff 2643b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK); 2653b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL)) 2663b3a8eb9SGleb Smirnoff return (EINVAL); 2673b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 2683b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 2693b3a8eb9SGleb Smirnoff return (ESRCH); 2703b3a8eb9SGleb Smirnoff if (kt->pfrkt_flags & PFR_TFLAG_CONST) 2713b3a8eb9SGleb Smirnoff return (EPERM); 2721e9e3741SMarko Zec tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0); 2733b3a8eb9SGleb Smirnoff if (tmpkt == NULL) 2743b3a8eb9SGleb Smirnoff return (ENOMEM); 2753b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 2763b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 2773b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 2783b3a8eb9SGleb Smirnoff senderr(EINVAL); 2793b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 1); 2803b3a8eb9SGleb Smirnoff q = pfr_lookup_addr(tmpkt, ad, 1); 2813b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) { 2823b3a8eb9SGleb Smirnoff if (q != NULL) 2833b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_DUPLICATE; 2843b3a8eb9SGleb Smirnoff else if (p == NULL) 2853b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_ADDED; 2863b3a8eb9SGleb Smirnoff else if (p->pfrke_not != ad->pfra_not) 2873b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_CONFLICT; 2883b3a8eb9SGleb Smirnoff else 2893b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_NONE; 2903b3a8eb9SGleb Smirnoff } 2913b3a8eb9SGleb Smirnoff if (p == NULL && q == NULL) { 29221121f9bSMark Johnston p = pfr_create_kentry(ad, 29321121f9bSMark Johnston (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0); 2943b3a8eb9SGleb Smirnoff if (p == NULL) 2953b3a8eb9SGleb Smirnoff senderr(ENOMEM); 2963b3a8eb9SGleb Smirnoff if (pfr_route_kentry(tmpkt, p)) { 2973b3a8eb9SGleb Smirnoff pfr_destroy_kentry(p); 2983b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_NONE; 2993b3a8eb9SGleb Smirnoff } else { 3003b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrke_workq); 3013b3a8eb9SGleb Smirnoff xadd++; 3023b3a8eb9SGleb Smirnoff } 3033b3a8eb9SGleb Smirnoff } 3043b3a8eb9SGleb Smirnoff } 3053b3a8eb9SGleb Smirnoff pfr_clean_node_mask(tmpkt, &workq); 3063b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 3073b3a8eb9SGleb Smirnoff pfr_insert_kentries(kt, &workq, tzero); 3083b3a8eb9SGleb Smirnoff else 3093b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&workq); 3103b3a8eb9SGleb Smirnoff if (nadd != NULL) 3113b3a8eb9SGleb Smirnoff *nadd = xadd; 3123b3a8eb9SGleb Smirnoff pfr_destroy_ktable(tmpkt, 0); 3133b3a8eb9SGleb Smirnoff return (0); 3143b3a8eb9SGleb Smirnoff _bad: 3153b3a8eb9SGleb Smirnoff pfr_clean_node_mask(tmpkt, &workq); 3163b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&workq); 3173b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) 3183b3a8eb9SGleb Smirnoff pfr_reset_feedback(addr, size); 3193b3a8eb9SGleb Smirnoff pfr_destroy_ktable(tmpkt, 0); 3203b3a8eb9SGleb Smirnoff return (rv); 3213b3a8eb9SGleb Smirnoff } 3223b3a8eb9SGleb Smirnoff 3233b3a8eb9SGleb Smirnoff int 3243b3a8eb9SGleb Smirnoff pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 3253b3a8eb9SGleb Smirnoff int *ndel, int flags) 3263b3a8eb9SGleb Smirnoff { 3273b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 3283b3a8eb9SGleb Smirnoff struct pfr_kentryworkq workq; 3293b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 3303b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 3313b3a8eb9SGleb Smirnoff int i, rv, xdel = 0, log = 1; 3323b3a8eb9SGleb Smirnoff 3333b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 3343b3a8eb9SGleb Smirnoff 3353b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK); 3363b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL)) 3373b3a8eb9SGleb Smirnoff return (EINVAL); 3383b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 3393b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 3403b3a8eb9SGleb Smirnoff return (ESRCH); 3413b3a8eb9SGleb Smirnoff if (kt->pfrkt_flags & PFR_TFLAG_CONST) 3423b3a8eb9SGleb Smirnoff return (EPERM); 3433b3a8eb9SGleb Smirnoff /* 3443b3a8eb9SGleb Smirnoff * there are two algorithms to choose from here. 3453b3a8eb9SGleb Smirnoff * with: 3463b3a8eb9SGleb Smirnoff * n: number of addresses to delete 3473b3a8eb9SGleb Smirnoff * N: number of addresses in the table 3483b3a8eb9SGleb Smirnoff * 3493b3a8eb9SGleb Smirnoff * one is O(N) and is better for large 'n' 3503b3a8eb9SGleb Smirnoff * one is O(n*LOG(N)) and is better for small 'n' 3513b3a8eb9SGleb Smirnoff * 3523b3a8eb9SGleb Smirnoff * following code try to decide which one is best. 3533b3a8eb9SGleb Smirnoff */ 3543b3a8eb9SGleb Smirnoff for (i = kt->pfrkt_cnt; i > 0; i >>= 1) 3553b3a8eb9SGleb Smirnoff log++; 3563b3a8eb9SGleb Smirnoff if (size > kt->pfrkt_cnt/log) { 3573b3a8eb9SGleb Smirnoff /* full table scan */ 3583b3a8eb9SGleb Smirnoff pfr_mark_addrs(kt); 3593b3a8eb9SGleb Smirnoff } else { 3603b3a8eb9SGleb Smirnoff /* iterate over addresses to delete */ 3613b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 3623b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 3633b3a8eb9SGleb Smirnoff return (EINVAL); 3643b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 1); 3653b3a8eb9SGleb Smirnoff if (p != NULL) 3663b3a8eb9SGleb Smirnoff p->pfrke_mark = 0; 3673b3a8eb9SGleb Smirnoff } 3683b3a8eb9SGleb Smirnoff } 3693b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 3703b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 3713b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 3723b3a8eb9SGleb Smirnoff senderr(EINVAL); 3733b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 1); 3743b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) { 3753b3a8eb9SGleb Smirnoff if (p == NULL) 3763b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_NONE; 3773b3a8eb9SGleb Smirnoff else if (p->pfrke_not != ad->pfra_not) 3783b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_CONFLICT; 3793b3a8eb9SGleb Smirnoff else if (p->pfrke_mark) 3803b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_DUPLICATE; 3813b3a8eb9SGleb Smirnoff else 3823b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_DELETED; 3833b3a8eb9SGleb Smirnoff } 3843b3a8eb9SGleb Smirnoff if (p != NULL && p->pfrke_not == ad->pfra_not && 3853b3a8eb9SGleb Smirnoff !p->pfrke_mark) { 3863b3a8eb9SGleb Smirnoff p->pfrke_mark = 1; 3873b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrke_workq); 3883b3a8eb9SGleb Smirnoff xdel++; 3893b3a8eb9SGleb Smirnoff } 3903b3a8eb9SGleb Smirnoff } 3913b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 3923b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &workq); 3933b3a8eb9SGleb Smirnoff if (ndel != NULL) 3943b3a8eb9SGleb Smirnoff *ndel = xdel; 3953b3a8eb9SGleb Smirnoff return (0); 3963b3a8eb9SGleb Smirnoff _bad: 3973b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) 3983b3a8eb9SGleb Smirnoff pfr_reset_feedback(addr, size); 3993b3a8eb9SGleb Smirnoff return (rv); 4003b3a8eb9SGleb Smirnoff } 4013b3a8eb9SGleb Smirnoff 4023b3a8eb9SGleb Smirnoff int 4033b3a8eb9SGleb Smirnoff pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 4043b3a8eb9SGleb Smirnoff int *size2, int *nadd, int *ndel, int *nchange, int flags, 4053b3a8eb9SGleb Smirnoff u_int32_t ignore_pfrt_flags) 4063b3a8eb9SGleb Smirnoff { 4073b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *tmpkt; 4083b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addq, delq, changeq; 4093b3a8eb9SGleb Smirnoff struct pfr_kentry *p, *q; 4103b3a8eb9SGleb Smirnoff struct pfr_addr ad; 4113b3a8eb9SGleb Smirnoff int i, rv, xadd = 0, xdel = 0, xchange = 0; 4123b3a8eb9SGleb Smirnoff long tzero = time_second; 4133b3a8eb9SGleb Smirnoff 4143b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 4153b3a8eb9SGleb Smirnoff 4163b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK); 4173b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, ignore_pfrt_flags, flags & 4183b3a8eb9SGleb Smirnoff PFR_FLAG_USERIOCTL)) 4193b3a8eb9SGleb Smirnoff return (EINVAL); 4203b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 4213b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 4223b3a8eb9SGleb Smirnoff return (ESRCH); 4233b3a8eb9SGleb Smirnoff if (kt->pfrkt_flags & PFR_TFLAG_CONST) 4243b3a8eb9SGleb Smirnoff return (EPERM); 4251e9e3741SMarko Zec tmpkt = pfr_create_ktable(&V_pfr_nulltable, 0, 0); 4263b3a8eb9SGleb Smirnoff if (tmpkt == NULL) 4273b3a8eb9SGleb Smirnoff return (ENOMEM); 4283b3a8eb9SGleb Smirnoff pfr_mark_addrs(kt); 4293b3a8eb9SGleb Smirnoff SLIST_INIT(&addq); 4303b3a8eb9SGleb Smirnoff SLIST_INIT(&delq); 4313b3a8eb9SGleb Smirnoff SLIST_INIT(&changeq); 4323b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 4333b3a8eb9SGleb Smirnoff /* 4343b3a8eb9SGleb Smirnoff * XXXGL: undertand pf_if usage of this function 4353b3a8eb9SGleb Smirnoff * and make ad a moving pointer 4363b3a8eb9SGleb Smirnoff */ 4373b3a8eb9SGleb Smirnoff bcopy(addr + i, &ad, sizeof(ad)); 4383b3a8eb9SGleb Smirnoff if (pfr_validate_addr(&ad)) 4393b3a8eb9SGleb Smirnoff senderr(EINVAL); 4403b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_NONE; 4413b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, &ad, 1); 4423b3a8eb9SGleb Smirnoff if (p != NULL) { 4433b3a8eb9SGleb Smirnoff if (p->pfrke_mark) { 4443b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_DUPLICATE; 4453b3a8eb9SGleb Smirnoff goto _skip; 4463b3a8eb9SGleb Smirnoff } 4473b3a8eb9SGleb Smirnoff p->pfrke_mark = 1; 4483b3a8eb9SGleb Smirnoff if (p->pfrke_not != ad.pfra_not) { 4493b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&changeq, p, pfrke_workq); 4503b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_CHANGED; 4513b3a8eb9SGleb Smirnoff xchange++; 4523b3a8eb9SGleb Smirnoff } 4533b3a8eb9SGleb Smirnoff } else { 4543b3a8eb9SGleb Smirnoff q = pfr_lookup_addr(tmpkt, &ad, 1); 4553b3a8eb9SGleb Smirnoff if (q != NULL) { 4563b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_DUPLICATE; 4573b3a8eb9SGleb Smirnoff goto _skip; 4583b3a8eb9SGleb Smirnoff } 45921121f9bSMark Johnston p = pfr_create_kentry(&ad, 46021121f9bSMark Johnston (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0); 4613b3a8eb9SGleb Smirnoff if (p == NULL) 4623b3a8eb9SGleb Smirnoff senderr(ENOMEM); 4633b3a8eb9SGleb Smirnoff if (pfr_route_kentry(tmpkt, p)) { 4643b3a8eb9SGleb Smirnoff pfr_destroy_kentry(p); 4653b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_NONE; 4663b3a8eb9SGleb Smirnoff } else { 4673b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, p, pfrke_workq); 4683b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_ADDED; 4693b3a8eb9SGleb Smirnoff xadd++; 4703b3a8eb9SGleb Smirnoff } 4713b3a8eb9SGleb Smirnoff } 4723b3a8eb9SGleb Smirnoff _skip: 4733b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) 4743b3a8eb9SGleb Smirnoff bcopy(&ad, addr + i, sizeof(ad)); 4753b3a8eb9SGleb Smirnoff } 4763b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &delq, &xdel, ENQUEUE_UNMARKED_ONLY); 4773b3a8eb9SGleb Smirnoff if ((flags & PFR_FLAG_FEEDBACK) && *size2) { 4783b3a8eb9SGleb Smirnoff if (*size2 < size+xdel) { 4793b3a8eb9SGleb Smirnoff *size2 = size+xdel; 4803b3a8eb9SGleb Smirnoff senderr(0); 4813b3a8eb9SGleb Smirnoff } 4823b3a8eb9SGleb Smirnoff i = 0; 4833b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, &delq, pfrke_workq) { 4843b3a8eb9SGleb Smirnoff pfr_copyout_addr(&ad, p); 4853b3a8eb9SGleb Smirnoff ad.pfra_fback = PFR_FB_DELETED; 4863b3a8eb9SGleb Smirnoff bcopy(&ad, addr + size + i, sizeof(ad)); 4873b3a8eb9SGleb Smirnoff i++; 4883b3a8eb9SGleb Smirnoff } 4893b3a8eb9SGleb Smirnoff } 4903b3a8eb9SGleb Smirnoff pfr_clean_node_mask(tmpkt, &addq); 4913b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 4923b3a8eb9SGleb Smirnoff pfr_insert_kentries(kt, &addq, tzero); 4933b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &delq); 49421121f9bSMark Johnston pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG); 4953b3a8eb9SGleb Smirnoff } else 4963b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addq); 4973b3a8eb9SGleb Smirnoff if (nadd != NULL) 4983b3a8eb9SGleb Smirnoff *nadd = xadd; 4993b3a8eb9SGleb Smirnoff if (ndel != NULL) 5003b3a8eb9SGleb Smirnoff *ndel = xdel; 5013b3a8eb9SGleb Smirnoff if (nchange != NULL) 5023b3a8eb9SGleb Smirnoff *nchange = xchange; 5033b3a8eb9SGleb Smirnoff if ((flags & PFR_FLAG_FEEDBACK) && size2) 5043b3a8eb9SGleb Smirnoff *size2 = size+xdel; 5053b3a8eb9SGleb Smirnoff pfr_destroy_ktable(tmpkt, 0); 5063b3a8eb9SGleb Smirnoff return (0); 5073b3a8eb9SGleb Smirnoff _bad: 5083b3a8eb9SGleb Smirnoff pfr_clean_node_mask(tmpkt, &addq); 5093b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addq); 5103b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) 5113b3a8eb9SGleb Smirnoff pfr_reset_feedback(addr, size); 5123b3a8eb9SGleb Smirnoff pfr_destroy_ktable(tmpkt, 0); 5133b3a8eb9SGleb Smirnoff return (rv); 5143b3a8eb9SGleb Smirnoff } 5153b3a8eb9SGleb Smirnoff 5163b3a8eb9SGleb Smirnoff int 5173b3a8eb9SGleb Smirnoff pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 5183b3a8eb9SGleb Smirnoff int *nmatch, int flags) 5193b3a8eb9SGleb Smirnoff { 5203b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 5213b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 5223b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 5233b3a8eb9SGleb Smirnoff int i, xmatch = 0; 5243b3a8eb9SGleb Smirnoff 5253b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 5263b3a8eb9SGleb Smirnoff 5273b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_REPLACE); 5283b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, 0)) 5293b3a8eb9SGleb Smirnoff return (EINVAL); 5303b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 5313b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 5323b3a8eb9SGleb Smirnoff return (ESRCH); 5333b3a8eb9SGleb Smirnoff 5343b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 5353b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 5363b3a8eb9SGleb Smirnoff return (EINVAL); 5373b3a8eb9SGleb Smirnoff if (ADDR_NETWORK(ad)) 5383b3a8eb9SGleb Smirnoff return (EINVAL); 5393b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 0); 5403b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_REPLACE) 5413b3a8eb9SGleb Smirnoff pfr_copyout_addr(ad, p); 5423b3a8eb9SGleb Smirnoff ad->pfra_fback = (p == NULL) ? PFR_FB_NONE : 5433b3a8eb9SGleb Smirnoff (p->pfrke_not ? PFR_FB_NOTMATCH : PFR_FB_MATCH); 5443b3a8eb9SGleb Smirnoff if (p != NULL && !p->pfrke_not) 5453b3a8eb9SGleb Smirnoff xmatch++; 5463b3a8eb9SGleb Smirnoff } 5473b3a8eb9SGleb Smirnoff if (nmatch != NULL) 5483b3a8eb9SGleb Smirnoff *nmatch = xmatch; 5493b3a8eb9SGleb Smirnoff return (0); 5503b3a8eb9SGleb Smirnoff } 5513b3a8eb9SGleb Smirnoff 5523b3a8eb9SGleb Smirnoff int 5533b3a8eb9SGleb Smirnoff pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size, 5543b3a8eb9SGleb Smirnoff int flags) 5553b3a8eb9SGleb Smirnoff { 5563b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 5573b3a8eb9SGleb Smirnoff struct pfr_walktree w; 5583b3a8eb9SGleb Smirnoff int rv; 5593b3a8eb9SGleb Smirnoff 5603b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 5613b3a8eb9SGleb Smirnoff 5623b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, 0); 5633b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, 0)) 5643b3a8eb9SGleb Smirnoff return (EINVAL); 5653b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 5663b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 5673b3a8eb9SGleb Smirnoff return (ESRCH); 5683b3a8eb9SGleb Smirnoff if (kt->pfrkt_cnt > *size) { 5693b3a8eb9SGleb Smirnoff *size = kt->pfrkt_cnt; 5703b3a8eb9SGleb Smirnoff return (0); 5713b3a8eb9SGleb Smirnoff } 5723b3a8eb9SGleb Smirnoff 5733b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 5743b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_GET_ADDRS; 5753b3a8eb9SGleb Smirnoff w.pfrw_addr = addr; 5763b3a8eb9SGleb Smirnoff w.pfrw_free = kt->pfrkt_cnt; 57761eee0e2SAlexander V. Chernikov rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 5783b3a8eb9SGleb Smirnoff if (!rv) 57961eee0e2SAlexander V. Chernikov rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, 58061eee0e2SAlexander V. Chernikov pfr_walktree, &w); 5813b3a8eb9SGleb Smirnoff if (rv) 5823b3a8eb9SGleb Smirnoff return (rv); 5833b3a8eb9SGleb Smirnoff 5843b3a8eb9SGleb Smirnoff KASSERT(w.pfrw_free == 0, ("%s: corruption detected (%d)", __func__, 5853b3a8eb9SGleb Smirnoff w.pfrw_free)); 5863b3a8eb9SGleb Smirnoff 5873b3a8eb9SGleb Smirnoff *size = kt->pfrkt_cnt; 5883b3a8eb9SGleb Smirnoff return (0); 5893b3a8eb9SGleb Smirnoff } 5903b3a8eb9SGleb Smirnoff 5913b3a8eb9SGleb Smirnoff int 5923b3a8eb9SGleb Smirnoff pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size, 5933b3a8eb9SGleb Smirnoff int flags) 5943b3a8eb9SGleb Smirnoff { 5953b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 5963b3a8eb9SGleb Smirnoff struct pfr_walktree w; 5973b3a8eb9SGleb Smirnoff struct pfr_kentryworkq workq; 5983b3a8eb9SGleb Smirnoff int rv; 5993b3a8eb9SGleb Smirnoff long tzero = time_second; 6003b3a8eb9SGleb Smirnoff 6013b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 6023b3a8eb9SGleb Smirnoff 6033b3a8eb9SGleb Smirnoff /* XXX PFR_FLAG_CLSTATS disabled */ 6043b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, 0); 6053b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, 0)) 6063b3a8eb9SGleb Smirnoff return (EINVAL); 6073b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 6083b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 6093b3a8eb9SGleb Smirnoff return (ESRCH); 6103b3a8eb9SGleb Smirnoff if (kt->pfrkt_cnt > *size) { 6113b3a8eb9SGleb Smirnoff *size = kt->pfrkt_cnt; 6123b3a8eb9SGleb Smirnoff return (0); 6133b3a8eb9SGleb Smirnoff } 6143b3a8eb9SGleb Smirnoff 6153b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 6163b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_GET_ASTATS; 6173b3a8eb9SGleb Smirnoff w.pfrw_astats = addr; 6183b3a8eb9SGleb Smirnoff w.pfrw_free = kt->pfrkt_cnt; 61959048686SKristof Provost /* 62059048686SKristof Provost * Flags below are for backward compatibility. It was possible to have 62159048686SKristof Provost * a table without per-entry counters. Now they are always allocated, 62259048686SKristof Provost * we just discard data when reading it if table is not configured to 62359048686SKristof Provost * have counters. 62459048686SKristof Provost */ 62559048686SKristof Provost w.pfrw_flags = kt->pfrkt_flags; 62661eee0e2SAlexander V. Chernikov rv = kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 6273b3a8eb9SGleb Smirnoff if (!rv) 62861eee0e2SAlexander V. Chernikov rv = kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, 62961eee0e2SAlexander V. Chernikov pfr_walktree, &w); 6303b3a8eb9SGleb Smirnoff if (!rv && (flags & PFR_FLAG_CLSTATS)) { 6313b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &workq, NULL, 0); 63221121f9bSMark Johnston pfr_clstats_kentries(kt, &workq, tzero, 0); 6333b3a8eb9SGleb Smirnoff } 6343b3a8eb9SGleb Smirnoff if (rv) 6353b3a8eb9SGleb Smirnoff return (rv); 6363b3a8eb9SGleb Smirnoff 6373b3a8eb9SGleb Smirnoff if (w.pfrw_free) { 6383b3a8eb9SGleb Smirnoff printf("pfr_get_astats: corruption detected (%d).\n", 6393b3a8eb9SGleb Smirnoff w.pfrw_free); 6403b3a8eb9SGleb Smirnoff return (ENOTTY); 6413b3a8eb9SGleb Smirnoff } 6423b3a8eb9SGleb Smirnoff *size = kt->pfrkt_cnt; 6433b3a8eb9SGleb Smirnoff return (0); 6443b3a8eb9SGleb Smirnoff } 6453b3a8eb9SGleb Smirnoff 6463b3a8eb9SGleb Smirnoff int 6473b3a8eb9SGleb Smirnoff pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size, 6483b3a8eb9SGleb Smirnoff int *nzero, int flags) 6493b3a8eb9SGleb Smirnoff { 6503b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 6513b3a8eb9SGleb Smirnoff struct pfr_kentryworkq workq; 6523b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 6533b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 6543b3a8eb9SGleb Smirnoff int i, rv, xzero = 0; 6553b3a8eb9SGleb Smirnoff 6563b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 6573b3a8eb9SGleb Smirnoff 6583b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_FEEDBACK); 6593b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, 0, 0)) 6603b3a8eb9SGleb Smirnoff return (EINVAL); 6613b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(tbl); 6623b3a8eb9SGleb Smirnoff if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 6633b3a8eb9SGleb Smirnoff return (ESRCH); 6643b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 6653b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 6663b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 6673b3a8eb9SGleb Smirnoff senderr(EINVAL); 6683b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 1); 6693b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) { 6703b3a8eb9SGleb Smirnoff ad->pfra_fback = (p != NULL) ? 6713b3a8eb9SGleb Smirnoff PFR_FB_CLEARED : PFR_FB_NONE; 6723b3a8eb9SGleb Smirnoff } 6733b3a8eb9SGleb Smirnoff if (p != NULL) { 6743b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrke_workq); 6753b3a8eb9SGleb Smirnoff xzero++; 6763b3a8eb9SGleb Smirnoff } 6773b3a8eb9SGleb Smirnoff } 6783b3a8eb9SGleb Smirnoff 6793b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 68021121f9bSMark Johnston pfr_clstats_kentries(kt, &workq, 0, 0); 6813b3a8eb9SGleb Smirnoff if (nzero != NULL) 6823b3a8eb9SGleb Smirnoff *nzero = xzero; 6833b3a8eb9SGleb Smirnoff return (0); 6843b3a8eb9SGleb Smirnoff _bad: 6853b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_FEEDBACK) 6863b3a8eb9SGleb Smirnoff pfr_reset_feedback(addr, size); 6873b3a8eb9SGleb Smirnoff return (rv); 6883b3a8eb9SGleb Smirnoff } 6893b3a8eb9SGleb Smirnoff 6903b3a8eb9SGleb Smirnoff static int 6913b3a8eb9SGleb Smirnoff pfr_validate_addr(struct pfr_addr *ad) 6923b3a8eb9SGleb Smirnoff { 6933b3a8eb9SGleb Smirnoff int i; 6943b3a8eb9SGleb Smirnoff 6953b3a8eb9SGleb Smirnoff switch (ad->pfra_af) { 6963b3a8eb9SGleb Smirnoff #ifdef INET 6973b3a8eb9SGleb Smirnoff case AF_INET: 6983b3a8eb9SGleb Smirnoff if (ad->pfra_net > 32) 6993b3a8eb9SGleb Smirnoff return (-1); 7003b3a8eb9SGleb Smirnoff break; 7013b3a8eb9SGleb Smirnoff #endif /* INET */ 7023b3a8eb9SGleb Smirnoff #ifdef INET6 7033b3a8eb9SGleb Smirnoff case AF_INET6: 7043b3a8eb9SGleb Smirnoff if (ad->pfra_net > 128) 7053b3a8eb9SGleb Smirnoff return (-1); 7063b3a8eb9SGleb Smirnoff break; 7073b3a8eb9SGleb Smirnoff #endif /* INET6 */ 7083b3a8eb9SGleb Smirnoff default: 7093b3a8eb9SGleb Smirnoff return (-1); 7103b3a8eb9SGleb Smirnoff } 7113b3a8eb9SGleb Smirnoff if (ad->pfra_net < 128 && 7123b3a8eb9SGleb Smirnoff (((caddr_t)ad)[ad->pfra_net/8] & (0xFF >> (ad->pfra_net%8)))) 7133b3a8eb9SGleb Smirnoff return (-1); 7143b3a8eb9SGleb Smirnoff for (i = (ad->pfra_net+7)/8; i < sizeof(ad->pfra_u); i++) 7153b3a8eb9SGleb Smirnoff if (((caddr_t)ad)[i]) 7163b3a8eb9SGleb Smirnoff return (-1); 7173b3a8eb9SGleb Smirnoff if (ad->pfra_not && ad->pfra_not != 1) 7183b3a8eb9SGleb Smirnoff return (-1); 7193b3a8eb9SGleb Smirnoff if (ad->pfra_fback) 7203b3a8eb9SGleb Smirnoff return (-1); 7213b3a8eb9SGleb Smirnoff return (0); 7223b3a8eb9SGleb Smirnoff } 7233b3a8eb9SGleb Smirnoff 7243b3a8eb9SGleb Smirnoff static void 7253b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(struct pfr_ktable *kt, struct pfr_kentryworkq *workq, 7263b3a8eb9SGleb Smirnoff int *naddr, int sweep) 7273b3a8eb9SGleb Smirnoff { 7283b3a8eb9SGleb Smirnoff struct pfr_walktree w; 7293b3a8eb9SGleb Smirnoff 7303b3a8eb9SGleb Smirnoff SLIST_INIT(workq); 7313b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 7323b3a8eb9SGleb Smirnoff w.pfrw_op = sweep ? PFRW_SWEEP : PFRW_ENQUEUE; 7333b3a8eb9SGleb Smirnoff w.pfrw_workq = workq; 7343b3a8eb9SGleb Smirnoff if (kt->pfrkt_ip4 != NULL) 73561eee0e2SAlexander V. Chernikov if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, 73661eee0e2SAlexander V. Chernikov pfr_walktree, &w)) 7373b3a8eb9SGleb Smirnoff printf("pfr_enqueue_addrs: IPv4 walktree failed.\n"); 7383b3a8eb9SGleb Smirnoff if (kt->pfrkt_ip6 != NULL) 73961eee0e2SAlexander V. Chernikov if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, 74061eee0e2SAlexander V. Chernikov pfr_walktree, &w)) 7413b3a8eb9SGleb Smirnoff printf("pfr_enqueue_addrs: IPv6 walktree failed.\n"); 7423b3a8eb9SGleb Smirnoff if (naddr != NULL) 7433b3a8eb9SGleb Smirnoff *naddr = w.pfrw_cnt; 7443b3a8eb9SGleb Smirnoff } 7453b3a8eb9SGleb Smirnoff 7463b3a8eb9SGleb Smirnoff static void 7473b3a8eb9SGleb Smirnoff pfr_mark_addrs(struct pfr_ktable *kt) 7483b3a8eb9SGleb Smirnoff { 7493b3a8eb9SGleb Smirnoff struct pfr_walktree w; 7503b3a8eb9SGleb Smirnoff 7513b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 7523b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_MARK; 75361eee0e2SAlexander V. Chernikov if (kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w)) 7543b3a8eb9SGleb Smirnoff printf("pfr_mark_addrs: IPv4 walktree failed.\n"); 75561eee0e2SAlexander V. Chernikov if (kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w)) 7563b3a8eb9SGleb Smirnoff printf("pfr_mark_addrs: IPv6 walktree failed.\n"); 7573b3a8eb9SGleb Smirnoff } 7583b3a8eb9SGleb Smirnoff 7593b3a8eb9SGleb Smirnoff static struct pfr_kentry * 7603b3a8eb9SGleb Smirnoff pfr_lookup_addr(struct pfr_ktable *kt, struct pfr_addr *ad, int exact) 7613b3a8eb9SGleb Smirnoff { 7623b3a8eb9SGleb Smirnoff union sockaddr_union sa, mask; 76361eee0e2SAlexander V. Chernikov struct radix_head *head = NULL; 7643b3a8eb9SGleb Smirnoff struct pfr_kentry *ke; 7653b3a8eb9SGleb Smirnoff 76629bdd62cSGleb Smirnoff PF_RULES_ASSERT(); 76729bdd62cSGleb Smirnoff 7683b3a8eb9SGleb Smirnoff bzero(&sa, sizeof(sa)); 7693b3a8eb9SGleb Smirnoff if (ad->pfra_af == AF_INET) { 7703b3a8eb9SGleb Smirnoff FILLIN_SIN(sa.sin, ad->pfra_ip4addr); 77161eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip4->rh; 7723b3a8eb9SGleb Smirnoff } else if ( ad->pfra_af == AF_INET6 ) { 7733b3a8eb9SGleb Smirnoff FILLIN_SIN6(sa.sin6, ad->pfra_ip6addr); 77461eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip6->rh; 7753b3a8eb9SGleb Smirnoff } 7763b3a8eb9SGleb Smirnoff if (ADDR_NETWORK(ad)) { 7773b3a8eb9SGleb Smirnoff pfr_prepare_network(&mask, ad->pfra_af, ad->pfra_net); 7783b3a8eb9SGleb Smirnoff ke = (struct pfr_kentry *)rn_lookup(&sa, &mask, head); 7793b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 7803b3a8eb9SGleb Smirnoff ke = NULL; 7813b3a8eb9SGleb Smirnoff } else { 7823b3a8eb9SGleb Smirnoff ke = (struct pfr_kentry *)rn_match(&sa, head); 7833b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 7843b3a8eb9SGleb Smirnoff ke = NULL; 7853b3a8eb9SGleb Smirnoff if (exact && ke && KENTRY_NETWORK(ke)) 7863b3a8eb9SGleb Smirnoff ke = NULL; 7873b3a8eb9SGleb Smirnoff } 7883b3a8eb9SGleb Smirnoff return (ke); 7893b3a8eb9SGleb Smirnoff } 7903b3a8eb9SGleb Smirnoff 7913b3a8eb9SGleb Smirnoff static struct pfr_kentry * 79221121f9bSMark Johnston pfr_create_kentry(struct pfr_addr *ad, bool counters) 7933b3a8eb9SGleb Smirnoff { 7943b3a8eb9SGleb Smirnoff struct pfr_kentry *ke; 795c1be8399SMark Johnston counter_u64_t c; 7963b3a8eb9SGleb Smirnoff 7973b3a8eb9SGleb Smirnoff ke = uma_zalloc(V_pfr_kentry_z, M_NOWAIT | M_ZERO); 7983b3a8eb9SGleb Smirnoff if (ke == NULL) 7993b3a8eb9SGleb Smirnoff return (NULL); 8003b3a8eb9SGleb Smirnoff 8013b3a8eb9SGleb Smirnoff if (ad->pfra_af == AF_INET) 8023b3a8eb9SGleb Smirnoff FILLIN_SIN(ke->pfrke_sa.sin, ad->pfra_ip4addr); 8033b3a8eb9SGleb Smirnoff else if (ad->pfra_af == AF_INET6) 8043b3a8eb9SGleb Smirnoff FILLIN_SIN6(ke->pfrke_sa.sin6, ad->pfra_ip6addr); 8053b3a8eb9SGleb Smirnoff ke->pfrke_af = ad->pfra_af; 8063b3a8eb9SGleb Smirnoff ke->pfrke_net = ad->pfra_net; 8073b3a8eb9SGleb Smirnoff ke->pfrke_not = ad->pfra_not; 80821121f9bSMark Johnston ke->pfrke_counters.pfrkc_tzero = 0; 809c1be8399SMark Johnston if (counters) { 810c1be8399SMark Johnston c = uma_zalloc_pcpu(V_pfr_kentry_counter_z, M_NOWAIT | M_ZERO); 811c1be8399SMark Johnston if (c == NULL) { 81259048686SKristof Provost pfr_destroy_kentry(ke); 81359048686SKristof Provost return (NULL); 81459048686SKristof Provost } 815c1be8399SMark Johnston ke->pfrke_counters.pfrkc_counters = c; 81659048686SKristof Provost } 8173b3a8eb9SGleb Smirnoff return (ke); 8183b3a8eb9SGleb Smirnoff } 8193b3a8eb9SGleb Smirnoff 8203b3a8eb9SGleb Smirnoff static void 8213b3a8eb9SGleb Smirnoff pfr_destroy_kentries(struct pfr_kentryworkq *workq) 8223b3a8eb9SGleb Smirnoff { 8233b3a8eb9SGleb Smirnoff struct pfr_kentry *p, *q; 8243b3a8eb9SGleb Smirnoff 8253b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(workq); p != NULL; p = q) { 8263b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrke_workq); 8273b3a8eb9SGleb Smirnoff pfr_destroy_kentry(p); 8283b3a8eb9SGleb Smirnoff } 8293b3a8eb9SGleb Smirnoff } 8303b3a8eb9SGleb Smirnoff 8313b3a8eb9SGleb Smirnoff static void 832c1be8399SMark Johnston pfr_destroy_kentry(struct pfr_kentry *ke) 83359048686SKristof Provost { 83421121f9bSMark Johnston counter_u64_t c; 83521121f9bSMark Johnston 836c1be8399SMark Johnston if ((c = ke->pfrke_counters.pfrkc_counters) != NULL) 837c1be8399SMark Johnston uma_zfree_pcpu(V_pfr_kentry_counter_z, c); 8383b3a8eb9SGleb Smirnoff uma_zfree(V_pfr_kentry_z, ke); 8393b3a8eb9SGleb Smirnoff } 8403b3a8eb9SGleb Smirnoff 8413b3a8eb9SGleb Smirnoff static void 8423b3a8eb9SGleb Smirnoff pfr_insert_kentries(struct pfr_ktable *kt, 8433b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *workq, long tzero) 8443b3a8eb9SGleb Smirnoff { 8453b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 8463b3a8eb9SGleb Smirnoff int rv, n = 0; 8473b3a8eb9SGleb Smirnoff 8483b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrke_workq) { 8493b3a8eb9SGleb Smirnoff rv = pfr_route_kentry(kt, p); 8503b3a8eb9SGleb Smirnoff if (rv) { 8513b3a8eb9SGleb Smirnoff printf("pfr_insert_kentries: cannot route entry " 8523b3a8eb9SGleb Smirnoff "(code=%d).\n", rv); 8533b3a8eb9SGleb Smirnoff break; 8543b3a8eb9SGleb Smirnoff } 85559048686SKristof Provost p->pfrke_counters.pfrkc_tzero = tzero; 8563b3a8eb9SGleb Smirnoff n++; 8573b3a8eb9SGleb Smirnoff } 8583b3a8eb9SGleb Smirnoff kt->pfrkt_cnt += n; 8593b3a8eb9SGleb Smirnoff } 8603b3a8eb9SGleb Smirnoff 8613b3a8eb9SGleb Smirnoff int 8623b3a8eb9SGleb Smirnoff pfr_insert_kentry(struct pfr_ktable *kt, struct pfr_addr *ad, long tzero) 8633b3a8eb9SGleb Smirnoff { 8643b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 8653b3a8eb9SGleb Smirnoff int rv; 8663b3a8eb9SGleb Smirnoff 8673b3a8eb9SGleb Smirnoff p = pfr_lookup_addr(kt, ad, 1); 8683b3a8eb9SGleb Smirnoff if (p != NULL) 8693b3a8eb9SGleb Smirnoff return (0); 87021121f9bSMark Johnston p = pfr_create_kentry(ad, (kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0); 8713b3a8eb9SGleb Smirnoff if (p == NULL) 872e706fd3aSGleb Smirnoff return (ENOMEM); 8733b3a8eb9SGleb Smirnoff 8743b3a8eb9SGleb Smirnoff rv = pfr_route_kentry(kt, p); 8753b3a8eb9SGleb Smirnoff if (rv) 8763b3a8eb9SGleb Smirnoff return (rv); 8773b3a8eb9SGleb Smirnoff 87859048686SKristof Provost p->pfrke_counters.pfrkc_tzero = tzero; 8793b3a8eb9SGleb Smirnoff kt->pfrkt_cnt++; 8803b3a8eb9SGleb Smirnoff 8813b3a8eb9SGleb Smirnoff return (0); 8823b3a8eb9SGleb Smirnoff } 8833b3a8eb9SGleb Smirnoff 8843b3a8eb9SGleb Smirnoff static void 8853b3a8eb9SGleb Smirnoff pfr_remove_kentries(struct pfr_ktable *kt, 8863b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *workq) 8873b3a8eb9SGleb Smirnoff { 8883b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 8893b3a8eb9SGleb Smirnoff int n = 0; 8903b3a8eb9SGleb Smirnoff 8913b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrke_workq) { 8923b3a8eb9SGleb Smirnoff pfr_unroute_kentry(kt, p); 8933b3a8eb9SGleb Smirnoff n++; 8943b3a8eb9SGleb Smirnoff } 8953b3a8eb9SGleb Smirnoff kt->pfrkt_cnt -= n; 8963b3a8eb9SGleb Smirnoff pfr_destroy_kentries(workq); 8973b3a8eb9SGleb Smirnoff } 8983b3a8eb9SGleb Smirnoff 8993b3a8eb9SGleb Smirnoff static void 9003b3a8eb9SGleb Smirnoff pfr_clean_node_mask(struct pfr_ktable *kt, 9013b3a8eb9SGleb Smirnoff struct pfr_kentryworkq *workq) 9023b3a8eb9SGleb Smirnoff { 9033b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 9043b3a8eb9SGleb Smirnoff 9053b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrke_workq) 9063b3a8eb9SGleb Smirnoff pfr_unroute_kentry(kt, p); 9073b3a8eb9SGleb Smirnoff } 9083b3a8eb9SGleb Smirnoff 9093b3a8eb9SGleb Smirnoff static void 91021121f9bSMark Johnston pfr_clstats_kentries(struct pfr_ktable *kt, struct pfr_kentryworkq *workq, 91121121f9bSMark Johnston long tzero, int negchange) 9123b3a8eb9SGleb Smirnoff { 9133b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 914c1be8399SMark Johnston int i; 9153b3a8eb9SGleb Smirnoff 9163b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrke_workq) { 9173b3a8eb9SGleb Smirnoff if (negchange) 9183b3a8eb9SGleb Smirnoff p->pfrke_not = !p->pfrke_not; 91921121f9bSMark Johnston if ((kt->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0) 920c1be8399SMark Johnston for (i = 0; i < PFR_NUM_COUNTERS; i++) 921c1be8399SMark Johnston counter_u64_zero( 922c1be8399SMark Johnston p->pfrke_counters.pfrkc_counters + i); 92359048686SKristof Provost p->pfrke_counters.pfrkc_tzero = tzero; 9243b3a8eb9SGleb Smirnoff } 9253b3a8eb9SGleb Smirnoff } 9263b3a8eb9SGleb Smirnoff 9273b3a8eb9SGleb Smirnoff static void 9283b3a8eb9SGleb Smirnoff pfr_reset_feedback(struct pfr_addr *addr, int size) 9293b3a8eb9SGleb Smirnoff { 9303b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 9313b3a8eb9SGleb Smirnoff int i; 9323b3a8eb9SGleb Smirnoff 9333b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) 9343b3a8eb9SGleb Smirnoff ad->pfra_fback = PFR_FB_NONE; 9353b3a8eb9SGleb Smirnoff } 9363b3a8eb9SGleb Smirnoff 9373b3a8eb9SGleb Smirnoff static void 9383b3a8eb9SGleb Smirnoff pfr_prepare_network(union sockaddr_union *sa, int af, int net) 9393b3a8eb9SGleb Smirnoff { 9403b3a8eb9SGleb Smirnoff int i; 9413b3a8eb9SGleb Smirnoff 9423b3a8eb9SGleb Smirnoff bzero(sa, sizeof(*sa)); 9433b3a8eb9SGleb Smirnoff if (af == AF_INET) { 9443b3a8eb9SGleb Smirnoff sa->sin.sin_len = sizeof(sa->sin); 9453b3a8eb9SGleb Smirnoff sa->sin.sin_family = AF_INET; 9463b3a8eb9SGleb Smirnoff sa->sin.sin_addr.s_addr = net ? htonl(-1 << (32-net)) : 0; 9473b3a8eb9SGleb Smirnoff } else if (af == AF_INET6) { 9483b3a8eb9SGleb Smirnoff sa->sin6.sin6_len = sizeof(sa->sin6); 9493b3a8eb9SGleb Smirnoff sa->sin6.sin6_family = AF_INET6; 9503b3a8eb9SGleb Smirnoff for (i = 0; i < 4; i++) { 9513b3a8eb9SGleb Smirnoff if (net <= 32) { 9523b3a8eb9SGleb Smirnoff sa->sin6.sin6_addr.s6_addr32[i] = 9533b3a8eb9SGleb Smirnoff net ? htonl(-1 << (32-net)) : 0; 9543b3a8eb9SGleb Smirnoff break; 9553b3a8eb9SGleb Smirnoff } 9563b3a8eb9SGleb Smirnoff sa->sin6.sin6_addr.s6_addr32[i] = 0xFFFFFFFF; 9573b3a8eb9SGleb Smirnoff net -= 32; 9583b3a8eb9SGleb Smirnoff } 9593b3a8eb9SGleb Smirnoff } 9603b3a8eb9SGleb Smirnoff } 9613b3a8eb9SGleb Smirnoff 9623b3a8eb9SGleb Smirnoff static int 9633b3a8eb9SGleb Smirnoff pfr_route_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke) 9643b3a8eb9SGleb Smirnoff { 9653b3a8eb9SGleb Smirnoff union sockaddr_union mask; 9663b3a8eb9SGleb Smirnoff struct radix_node *rn; 96761eee0e2SAlexander V. Chernikov struct radix_head *head = NULL; 9683b3a8eb9SGleb Smirnoff 96929bdd62cSGleb Smirnoff PF_RULES_WASSERT(); 97029bdd62cSGleb Smirnoff 9713b3a8eb9SGleb Smirnoff bzero(ke->pfrke_node, sizeof(ke->pfrke_node)); 9723b3a8eb9SGleb Smirnoff if (ke->pfrke_af == AF_INET) 97361eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip4->rh; 9743b3a8eb9SGleb Smirnoff else if (ke->pfrke_af == AF_INET6) 97561eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip6->rh; 9763b3a8eb9SGleb Smirnoff 9773b3a8eb9SGleb Smirnoff if (KENTRY_NETWORK(ke)) { 9783b3a8eb9SGleb Smirnoff pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net); 9793b3a8eb9SGleb Smirnoff rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node); 9803b3a8eb9SGleb Smirnoff } else 9813b3a8eb9SGleb Smirnoff rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node); 9823b3a8eb9SGleb Smirnoff 9833b3a8eb9SGleb Smirnoff return (rn == NULL ? -1 : 0); 9843b3a8eb9SGleb Smirnoff } 9853b3a8eb9SGleb Smirnoff 9863b3a8eb9SGleb Smirnoff static int 9873b3a8eb9SGleb Smirnoff pfr_unroute_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke) 9883b3a8eb9SGleb Smirnoff { 9893b3a8eb9SGleb Smirnoff union sockaddr_union mask; 9903b3a8eb9SGleb Smirnoff struct radix_node *rn; 99161eee0e2SAlexander V. Chernikov struct radix_head *head = NULL; 9923b3a8eb9SGleb Smirnoff 9933b3a8eb9SGleb Smirnoff if (ke->pfrke_af == AF_INET) 99461eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip4->rh; 9953b3a8eb9SGleb Smirnoff else if (ke->pfrke_af == AF_INET6) 99661eee0e2SAlexander V. Chernikov head = &kt->pfrkt_ip6->rh; 9973b3a8eb9SGleb Smirnoff 9983b3a8eb9SGleb Smirnoff if (KENTRY_NETWORK(ke)) { 9993b3a8eb9SGleb Smirnoff pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net); 10003b3a8eb9SGleb Smirnoff rn = rn_delete(&ke->pfrke_sa, &mask, head); 10013b3a8eb9SGleb Smirnoff } else 10023b3a8eb9SGleb Smirnoff rn = rn_delete(&ke->pfrke_sa, NULL, head); 10033b3a8eb9SGleb Smirnoff 10043b3a8eb9SGleb Smirnoff if (rn == NULL) { 10053b3a8eb9SGleb Smirnoff printf("pfr_unroute_kentry: delete failed.\n"); 10063b3a8eb9SGleb Smirnoff return (-1); 10073b3a8eb9SGleb Smirnoff } 10083b3a8eb9SGleb Smirnoff return (0); 10093b3a8eb9SGleb Smirnoff } 10103b3a8eb9SGleb Smirnoff 10113b3a8eb9SGleb Smirnoff static void 101259048686SKristof Provost pfr_copyout_addr(struct pfr_addr *ad, const struct pfr_kentry *ke) 10133b3a8eb9SGleb Smirnoff { 10143b3a8eb9SGleb Smirnoff bzero(ad, sizeof(*ad)); 10153b3a8eb9SGleb Smirnoff if (ke == NULL) 10163b3a8eb9SGleb Smirnoff return; 10173b3a8eb9SGleb Smirnoff ad->pfra_af = ke->pfrke_af; 10183b3a8eb9SGleb Smirnoff ad->pfra_net = ke->pfrke_net; 10193b3a8eb9SGleb Smirnoff ad->pfra_not = ke->pfrke_not; 10203b3a8eb9SGleb Smirnoff if (ad->pfra_af == AF_INET) 10213b3a8eb9SGleb Smirnoff ad->pfra_ip4addr = ke->pfrke_sa.sin.sin_addr; 10223b3a8eb9SGleb Smirnoff else if (ad->pfra_af == AF_INET6) 10233b3a8eb9SGleb Smirnoff ad->pfra_ip6addr = ke->pfrke_sa.sin6.sin6_addr; 10243b3a8eb9SGleb Smirnoff } 10253b3a8eb9SGleb Smirnoff 102659048686SKristof Provost static void 102759048686SKristof Provost pfr_copyout_astats(struct pfr_astats *as, const struct pfr_kentry *ke, 102859048686SKristof Provost const struct pfr_walktree *w) 102959048686SKristof Provost { 103059048686SKristof Provost int dir, op; 103159048686SKristof Provost const struct pfr_kcounters *kc = &ke->pfrke_counters; 103259048686SKristof Provost 103360a38abbSMark Johnston bzero(as, sizeof(*as)); 103459048686SKristof Provost pfr_copyout_addr(&as->pfras_a, ke); 103559048686SKristof Provost as->pfras_tzero = kc->pfrkc_tzero; 103659048686SKristof Provost 1037b21826bfSKristof Provost if (! (w->pfrw_flags & PFR_TFLAG_COUNTERS) || 1038b21826bfSKristof Provost kc->pfrkc_counters == NULL) { 103959048686SKristof Provost bzero(as->pfras_packets, sizeof(as->pfras_packets)); 104059048686SKristof Provost bzero(as->pfras_bytes, sizeof(as->pfras_bytes)); 104159048686SKristof Provost as->pfras_a.pfra_fback = PFR_FB_NOCOUNT; 104259048686SKristof Provost return; 104359048686SKristof Provost } 104459048686SKristof Provost 104559048686SKristof Provost for (dir = 0; dir < PFR_DIR_MAX; dir++) { 104659048686SKristof Provost for (op = 0; op < PFR_OP_ADDR_MAX; op ++) { 1047c1be8399SMark Johnston as->pfras_packets[dir][op] = counter_u64_fetch( 1048c1be8399SMark Johnston pfr_kentry_counter(kc, dir, op, PFR_TYPE_PACKETS)); 1049c1be8399SMark Johnston as->pfras_bytes[dir][op] = counter_u64_fetch( 1050c1be8399SMark Johnston pfr_kentry_counter(kc, dir, op, PFR_TYPE_BYTES)); 105159048686SKristof Provost } 105259048686SKristof Provost } 105359048686SKristof Provost } 105459048686SKristof Provost 10553b3a8eb9SGleb Smirnoff static int 10563b3a8eb9SGleb Smirnoff pfr_walktree(struct radix_node *rn, void *arg) 10573b3a8eb9SGleb Smirnoff { 10583b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = (struct pfr_kentry *)rn; 10593b3a8eb9SGleb Smirnoff struct pfr_walktree *w = arg; 10603b3a8eb9SGleb Smirnoff 10613b3a8eb9SGleb Smirnoff switch (w->pfrw_op) { 10623b3a8eb9SGleb Smirnoff case PFRW_MARK: 10633b3a8eb9SGleb Smirnoff ke->pfrke_mark = 0; 10643b3a8eb9SGleb Smirnoff break; 10653b3a8eb9SGleb Smirnoff case PFRW_SWEEP: 10663b3a8eb9SGleb Smirnoff if (ke->pfrke_mark) 10673b3a8eb9SGleb Smirnoff break; 10683b3a8eb9SGleb Smirnoff /* FALLTHROUGH */ 10693b3a8eb9SGleb Smirnoff case PFRW_ENQUEUE: 10703b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq); 10713b3a8eb9SGleb Smirnoff w->pfrw_cnt++; 10723b3a8eb9SGleb Smirnoff break; 10733b3a8eb9SGleb Smirnoff case PFRW_GET_ADDRS: 10743b3a8eb9SGleb Smirnoff if (w->pfrw_free-- > 0) { 10753b3a8eb9SGleb Smirnoff pfr_copyout_addr(w->pfrw_addr, ke); 10763b3a8eb9SGleb Smirnoff w->pfrw_addr++; 10773b3a8eb9SGleb Smirnoff } 10783b3a8eb9SGleb Smirnoff break; 10793b3a8eb9SGleb Smirnoff case PFRW_GET_ASTATS: 10803b3a8eb9SGleb Smirnoff if (w->pfrw_free-- > 0) { 10813b3a8eb9SGleb Smirnoff struct pfr_astats as; 10823b3a8eb9SGleb Smirnoff 108359048686SKristof Provost pfr_copyout_astats(&as, ke, w); 10843b3a8eb9SGleb Smirnoff 10853b3a8eb9SGleb Smirnoff bcopy(&as, w->pfrw_astats, sizeof(as)); 10863b3a8eb9SGleb Smirnoff w->pfrw_astats++; 10873b3a8eb9SGleb Smirnoff } 10883b3a8eb9SGleb Smirnoff break; 10893b3a8eb9SGleb Smirnoff case PFRW_POOL_GET: 10903b3a8eb9SGleb Smirnoff if (ke->pfrke_not) 10913b3a8eb9SGleb Smirnoff break; /* negative entries are ignored */ 10923b3a8eb9SGleb Smirnoff if (!w->pfrw_cnt--) { 10933b3a8eb9SGleb Smirnoff w->pfrw_kentry = ke; 10943b3a8eb9SGleb Smirnoff return (1); /* finish search */ 10953b3a8eb9SGleb Smirnoff } 10963b3a8eb9SGleb Smirnoff break; 10973b3a8eb9SGleb Smirnoff case PFRW_DYNADDR_UPDATE: 10983b3a8eb9SGleb Smirnoff { 10993b3a8eb9SGleb Smirnoff union sockaddr_union pfr_mask; 11003b3a8eb9SGleb Smirnoff 11013b3a8eb9SGleb Smirnoff if (ke->pfrke_af == AF_INET) { 11023b3a8eb9SGleb Smirnoff if (w->pfrw_dyn->pfid_acnt4++ > 0) 11033b3a8eb9SGleb Smirnoff break; 11043b3a8eb9SGleb Smirnoff pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net); 11053b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_addr4 = *SUNION2PF(&ke->pfrke_sa, 11063b3a8eb9SGleb Smirnoff AF_INET); 11073b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_mask4 = *SUNION2PF(&pfr_mask, 11083b3a8eb9SGleb Smirnoff AF_INET); 11093b3a8eb9SGleb Smirnoff } else if (ke->pfrke_af == AF_INET6){ 11103b3a8eb9SGleb Smirnoff if (w->pfrw_dyn->pfid_acnt6++ > 0) 11113b3a8eb9SGleb Smirnoff break; 11123b3a8eb9SGleb Smirnoff pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net); 11133b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_addr6 = *SUNION2PF(&ke->pfrke_sa, 11143b3a8eb9SGleb Smirnoff AF_INET6); 11153b3a8eb9SGleb Smirnoff w->pfrw_dyn->pfid_mask6 = *SUNION2PF(&pfr_mask, 11163b3a8eb9SGleb Smirnoff AF_INET6); 11173b3a8eb9SGleb Smirnoff } 11183b3a8eb9SGleb Smirnoff break; 11193b3a8eb9SGleb Smirnoff } 1120b21826bfSKristof Provost case PFRW_COUNTERS: 1121b21826bfSKristof Provost { 1122b21826bfSKristof Provost if (w->pfrw_flags & PFR_TFLAG_COUNTERS) { 1123b21826bfSKristof Provost if (ke->pfrke_counters.pfrkc_counters != NULL) 1124b21826bfSKristof Provost break; 1125b21826bfSKristof Provost ke->pfrke_counters.pfrkc_counters = 1126b21826bfSKristof Provost uma_zalloc_pcpu(V_pfr_kentry_counter_z, 1127b21826bfSKristof Provost M_NOWAIT | M_ZERO); 1128b21826bfSKristof Provost } else { 1129b21826bfSKristof Provost uma_zfree_pcpu(V_pfr_kentry_counter_z, 1130b21826bfSKristof Provost ke->pfrke_counters.pfrkc_counters); 1131b21826bfSKristof Provost ke->pfrke_counters.pfrkc_counters = NULL; 1132b21826bfSKristof Provost } 1133b21826bfSKristof Provost break; 1134b21826bfSKristof Provost } 11353b3a8eb9SGleb Smirnoff } 11363b3a8eb9SGleb Smirnoff return (0); 11373b3a8eb9SGleb Smirnoff } 11383b3a8eb9SGleb Smirnoff 11393b3a8eb9SGleb Smirnoff int 11403b3a8eb9SGleb Smirnoff pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags) 11413b3a8eb9SGleb Smirnoff { 11423b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 11433b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 11443b3a8eb9SGleb Smirnoff int xdel = 0; 11453b3a8eb9SGleb Smirnoff 11463b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ALLRSETS); 11473b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 11483b3a8eb9SGleb Smirnoff return (EINVAL); 11493b3a8eb9SGleb Smirnoff if (pfr_table_count(filter, flags) < 0) 11503b3a8eb9SGleb Smirnoff return (ENOENT); 11513b3a8eb9SGleb Smirnoff 11523b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 11531e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 11543b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 11553b3a8eb9SGleb Smirnoff continue; 11563b3a8eb9SGleb Smirnoff if (!strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR)) 11573b3a8eb9SGleb Smirnoff continue; 11583b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) 11593b3a8eb9SGleb Smirnoff continue; 11603b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE; 11613b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 11623b3a8eb9SGleb Smirnoff xdel++; 11633b3a8eb9SGleb Smirnoff } 11643b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 11653b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 11663b3a8eb9SGleb Smirnoff if (ndel != NULL) 11673b3a8eb9SGleb Smirnoff *ndel = xdel; 11683b3a8eb9SGleb Smirnoff return (0); 11693b3a8eb9SGleb Smirnoff } 11703b3a8eb9SGleb Smirnoff 11713b3a8eb9SGleb Smirnoff int 11723b3a8eb9SGleb Smirnoff pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags) 11733b3a8eb9SGleb Smirnoff { 11743b3a8eb9SGleb Smirnoff struct pfr_ktableworkq addq, changeq; 11753b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, *r, key; 11763b3a8eb9SGleb Smirnoff int i, rv, xadd = 0; 11773b3a8eb9SGleb Smirnoff long tzero = time_second; 11783b3a8eb9SGleb Smirnoff 11793b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 11803b3a8eb9SGleb Smirnoff SLIST_INIT(&addq); 11813b3a8eb9SGleb Smirnoff SLIST_INIT(&changeq); 11823b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 11833b3a8eb9SGleb Smirnoff bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 11843b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK, 11853b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 11863b3a8eb9SGleb Smirnoff senderr(EINVAL); 11873b3a8eb9SGleb Smirnoff key.pfrkt_flags |= PFR_TFLAG_ACTIVE; 11881e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 11893b3a8eb9SGleb Smirnoff if (p == NULL) { 11903b3a8eb9SGleb Smirnoff p = pfr_create_ktable(&key.pfrkt_t, tzero, 1); 11913b3a8eb9SGleb Smirnoff if (p == NULL) 11923b3a8eb9SGleb Smirnoff senderr(ENOMEM); 11933b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &addq, pfrkt_workq) { 1194b4b8fa33SKristof Provost if (!pfr_ktable_compare(p, q)) { 1195b4b8fa33SKristof Provost pfr_destroy_ktable(p, 0); 11963b3a8eb9SGleb Smirnoff goto _skip; 11973b3a8eb9SGleb Smirnoff } 1198b4b8fa33SKristof Provost } 11993b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, p, pfrkt_workq); 12003b3a8eb9SGleb Smirnoff xadd++; 12013b3a8eb9SGleb Smirnoff if (!key.pfrkt_anchor[0]) 12023b3a8eb9SGleb Smirnoff goto _skip; 12033b3a8eb9SGleb Smirnoff 12043b3a8eb9SGleb Smirnoff /* find or create root table */ 12053b3a8eb9SGleb Smirnoff bzero(key.pfrkt_anchor, sizeof(key.pfrkt_anchor)); 12061e9e3741SMarko Zec r = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 12073b3a8eb9SGleb Smirnoff if (r != NULL) { 12083b3a8eb9SGleb Smirnoff p->pfrkt_root = r; 12093b3a8eb9SGleb Smirnoff goto _skip; 12103b3a8eb9SGleb Smirnoff } 12113b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &addq, pfrkt_workq) { 12123b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(&key, q)) { 12133b3a8eb9SGleb Smirnoff p->pfrkt_root = q; 12143b3a8eb9SGleb Smirnoff goto _skip; 12153b3a8eb9SGleb Smirnoff } 12163b3a8eb9SGleb Smirnoff } 12173b3a8eb9SGleb Smirnoff key.pfrkt_flags = 0; 12183b3a8eb9SGleb Smirnoff r = pfr_create_ktable(&key.pfrkt_t, 0, 1); 12193b3a8eb9SGleb Smirnoff if (r == NULL) 12203b3a8eb9SGleb Smirnoff senderr(ENOMEM); 12213b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, r, pfrkt_workq); 12223b3a8eb9SGleb Smirnoff p->pfrkt_root = r; 12233b3a8eb9SGleb Smirnoff } else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 12243b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &changeq, pfrkt_workq) 12253b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(&key, q)) 12263b3a8eb9SGleb Smirnoff goto _skip; 12273b3a8eb9SGleb Smirnoff p->pfrkt_nflags = (p->pfrkt_flags & 12283b3a8eb9SGleb Smirnoff ~PFR_TFLAG_USRMASK) | key.pfrkt_flags; 12293b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq); 12303b3a8eb9SGleb Smirnoff xadd++; 12313b3a8eb9SGleb Smirnoff } 12323b3a8eb9SGleb Smirnoff _skip: 12333b3a8eb9SGleb Smirnoff ; 12343b3a8eb9SGleb Smirnoff } 12353b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 12363b3a8eb9SGleb Smirnoff pfr_insert_ktables(&addq); 12373b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&changeq); 12383b3a8eb9SGleb Smirnoff } else 12393b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&addq, 0); 12403b3a8eb9SGleb Smirnoff if (nadd != NULL) 12413b3a8eb9SGleb Smirnoff *nadd = xadd; 12423b3a8eb9SGleb Smirnoff return (0); 12433b3a8eb9SGleb Smirnoff _bad: 12443b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&addq, 0); 12453b3a8eb9SGleb Smirnoff return (rv); 12463b3a8eb9SGleb Smirnoff } 12473b3a8eb9SGleb Smirnoff 12483b3a8eb9SGleb Smirnoff int 12493b3a8eb9SGleb Smirnoff pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags) 12503b3a8eb9SGleb Smirnoff { 12513b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 12523b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, key; 12533b3a8eb9SGleb Smirnoff int i, xdel = 0; 12543b3a8eb9SGleb Smirnoff 12553b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 12563b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 12573b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 12583b3a8eb9SGleb Smirnoff bcopy(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 12593b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 12603b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 12613b3a8eb9SGleb Smirnoff return (EINVAL); 12621e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 12633b3a8eb9SGleb Smirnoff if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 12643b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &workq, pfrkt_workq) 12653b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(p, q)) 12663b3a8eb9SGleb Smirnoff goto _skip; 12673b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE; 12683b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 12693b3a8eb9SGleb Smirnoff xdel++; 12703b3a8eb9SGleb Smirnoff } 12713b3a8eb9SGleb Smirnoff _skip: 12723b3a8eb9SGleb Smirnoff ; 12733b3a8eb9SGleb Smirnoff } 12743b3a8eb9SGleb Smirnoff 12753b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 12763b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 12773b3a8eb9SGleb Smirnoff if (ndel != NULL) 12783b3a8eb9SGleb Smirnoff *ndel = xdel; 12793b3a8eb9SGleb Smirnoff return (0); 12803b3a8eb9SGleb Smirnoff } 12813b3a8eb9SGleb Smirnoff 12823b3a8eb9SGleb Smirnoff int 12833b3a8eb9SGleb Smirnoff pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size, 12843b3a8eb9SGleb Smirnoff int flags) 12853b3a8eb9SGleb Smirnoff { 12863b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 12873b3a8eb9SGleb Smirnoff int n, nn; 12883b3a8eb9SGleb Smirnoff 12893b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 12903b3a8eb9SGleb Smirnoff 12913b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS); 12923b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 12933b3a8eb9SGleb Smirnoff return (EINVAL); 12943b3a8eb9SGleb Smirnoff n = nn = pfr_table_count(filter, flags); 12953b3a8eb9SGleb Smirnoff if (n < 0) 12963b3a8eb9SGleb Smirnoff return (ENOENT); 12973b3a8eb9SGleb Smirnoff if (n > *size) { 12983b3a8eb9SGleb Smirnoff *size = n; 12993b3a8eb9SGleb Smirnoff return (0); 13003b3a8eb9SGleb Smirnoff } 13011e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 13023b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 13033b3a8eb9SGleb Smirnoff continue; 13043b3a8eb9SGleb Smirnoff if (n-- <= 0) 13053b3a8eb9SGleb Smirnoff continue; 13063b3a8eb9SGleb Smirnoff bcopy(&p->pfrkt_t, tbl++, sizeof(*tbl)); 13073b3a8eb9SGleb Smirnoff } 13083b3a8eb9SGleb Smirnoff 13093b3a8eb9SGleb Smirnoff KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n)); 13103b3a8eb9SGleb Smirnoff 13113b3a8eb9SGleb Smirnoff *size = nn; 13123b3a8eb9SGleb Smirnoff return (0); 13133b3a8eb9SGleb Smirnoff } 13143b3a8eb9SGleb Smirnoff 13153b3a8eb9SGleb Smirnoff int 13163b3a8eb9SGleb Smirnoff pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size, 13173b3a8eb9SGleb Smirnoff int flags) 13183b3a8eb9SGleb Smirnoff { 13193b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 13203b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 13213b3a8eb9SGleb Smirnoff int n, nn; 13223b3a8eb9SGleb Smirnoff long tzero = time_second; 132359048686SKristof Provost int pfr_dir, pfr_op; 13243b3a8eb9SGleb Smirnoff 13253b3a8eb9SGleb Smirnoff /* XXX PFR_FLAG_CLSTATS disabled */ 13263b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS); 13273b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(filter->pfrt_anchor)) 13283b3a8eb9SGleb Smirnoff return (EINVAL); 13293b3a8eb9SGleb Smirnoff n = nn = pfr_table_count(filter, flags); 13303b3a8eb9SGleb Smirnoff if (n < 0) 13313b3a8eb9SGleb Smirnoff return (ENOENT); 13323b3a8eb9SGleb Smirnoff if (n > *size) { 13333b3a8eb9SGleb Smirnoff *size = n; 13343b3a8eb9SGleb Smirnoff return (0); 13353b3a8eb9SGleb Smirnoff } 13363b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 13371e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 13383b3a8eb9SGleb Smirnoff if (pfr_skip_table(filter, p, flags)) 13393b3a8eb9SGleb Smirnoff continue; 13403b3a8eb9SGleb Smirnoff if (n-- <= 0) 13413b3a8eb9SGleb Smirnoff continue; 134259048686SKristof Provost bcopy(&p->pfrkt_kts.pfrts_t, &tbl->pfrts_t, 134359048686SKristof Provost sizeof(struct pfr_table)); 134459048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 134559048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 134659048686SKristof Provost tbl->pfrts_packets[pfr_dir][pfr_op] = 1347f92c21a2SMateusz Guzik pfr_kstate_counter_fetch( 1348f92c21a2SMateusz Guzik &p->pfrkt_packets[pfr_dir][pfr_op]); 134959048686SKristof Provost tbl->pfrts_bytes[pfr_dir][pfr_op] = 1350f92c21a2SMateusz Guzik pfr_kstate_counter_fetch( 1351f92c21a2SMateusz Guzik &p->pfrkt_bytes[pfr_dir][pfr_op]); 135259048686SKristof Provost } 135359048686SKristof Provost } 1354f92c21a2SMateusz Guzik tbl->pfrts_match = pfr_kstate_counter_fetch(&p->pfrkt_match); 1355f92c21a2SMateusz Guzik tbl->pfrts_nomatch = pfr_kstate_counter_fetch(&p->pfrkt_nomatch); 135659048686SKristof Provost tbl->pfrts_tzero = p->pfrkt_tzero; 135759048686SKristof Provost tbl->pfrts_cnt = p->pfrkt_cnt; 135859048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_REFCNT_MAX; pfr_op++) 135959048686SKristof Provost tbl->pfrts_refcnt[pfr_op] = p->pfrkt_refcnt[pfr_op]; 136059048686SKristof Provost tbl++; 13613b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 13623b3a8eb9SGleb Smirnoff } 13633b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_CLSTATS) 13643b3a8eb9SGleb Smirnoff pfr_clstats_ktables(&workq, tzero, 13653b3a8eb9SGleb Smirnoff flags & PFR_FLAG_ADDRSTOO); 13663b3a8eb9SGleb Smirnoff 13673b3a8eb9SGleb Smirnoff KASSERT(n == 0, ("%s: corruption detected (%d)", __func__, n)); 13683b3a8eb9SGleb Smirnoff 13693b3a8eb9SGleb Smirnoff *size = nn; 13703b3a8eb9SGleb Smirnoff return (0); 13713b3a8eb9SGleb Smirnoff } 13723b3a8eb9SGleb Smirnoff 13733b3a8eb9SGleb Smirnoff int 13743b3a8eb9SGleb Smirnoff pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags) 13753b3a8eb9SGleb Smirnoff { 13763b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 13773b3a8eb9SGleb Smirnoff struct pfr_ktable *p, key; 13783b3a8eb9SGleb Smirnoff int i, xzero = 0; 13793b3a8eb9SGleb Smirnoff long tzero = time_second; 13803b3a8eb9SGleb Smirnoff 13813b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO); 13823b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 13833b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 13843b3a8eb9SGleb Smirnoff bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 13853b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 0)) 13863b3a8eb9SGleb Smirnoff return (EINVAL); 13871e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 13883b3a8eb9SGleb Smirnoff if (p != NULL) { 13893b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 13903b3a8eb9SGleb Smirnoff xzero++; 13913b3a8eb9SGleb Smirnoff } 13923b3a8eb9SGleb Smirnoff } 13933b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 13943b3a8eb9SGleb Smirnoff pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO); 13953b3a8eb9SGleb Smirnoff if (nzero != NULL) 13963b3a8eb9SGleb Smirnoff *nzero = xzero; 13973b3a8eb9SGleb Smirnoff return (0); 13983b3a8eb9SGleb Smirnoff } 13993b3a8eb9SGleb Smirnoff 14003b3a8eb9SGleb Smirnoff int 14013b3a8eb9SGleb Smirnoff pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag, 14023b3a8eb9SGleb Smirnoff int *nchange, int *ndel, int flags) 14033b3a8eb9SGleb Smirnoff { 14043b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 14053b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q, key; 14063b3a8eb9SGleb Smirnoff int i, xchange = 0, xdel = 0; 14073b3a8eb9SGleb Smirnoff 14083b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 14093b3a8eb9SGleb Smirnoff if ((setflag & ~PFR_TFLAG_USRMASK) || 14103b3a8eb9SGleb Smirnoff (clrflag & ~PFR_TFLAG_USRMASK) || 14113b3a8eb9SGleb Smirnoff (setflag & clrflag)) 14123b3a8eb9SGleb Smirnoff return (EINVAL); 14133b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 14143b3a8eb9SGleb Smirnoff for (i = 0; i < size; i++) { 14153b3a8eb9SGleb Smirnoff bcopy(tbl + i, &key.pfrkt_t, sizeof(key.pfrkt_t)); 14163b3a8eb9SGleb Smirnoff if (pfr_validate_table(&key.pfrkt_t, 0, 14173b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 14183b3a8eb9SGleb Smirnoff return (EINVAL); 14191e9e3741SMarko Zec p = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 14203b3a8eb9SGleb Smirnoff if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) { 14213b3a8eb9SGleb Smirnoff p->pfrkt_nflags = (p->pfrkt_flags | setflag) & 14223b3a8eb9SGleb Smirnoff ~clrflag; 14233b3a8eb9SGleb Smirnoff if (p->pfrkt_nflags == p->pfrkt_flags) 14243b3a8eb9SGleb Smirnoff goto _skip; 14253b3a8eb9SGleb Smirnoff SLIST_FOREACH(q, &workq, pfrkt_workq) 14263b3a8eb9SGleb Smirnoff if (!pfr_ktable_compare(p, q)) 14273b3a8eb9SGleb Smirnoff goto _skip; 14283b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 14293b3a8eb9SGleb Smirnoff if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) && 14303b3a8eb9SGleb Smirnoff (clrflag & PFR_TFLAG_PERSIST) && 14313b3a8eb9SGleb Smirnoff !(p->pfrkt_flags & PFR_TFLAG_REFERENCED)) 14323b3a8eb9SGleb Smirnoff xdel++; 14333b3a8eb9SGleb Smirnoff else 14343b3a8eb9SGleb Smirnoff xchange++; 14353b3a8eb9SGleb Smirnoff } 14363b3a8eb9SGleb Smirnoff _skip: 14373b3a8eb9SGleb Smirnoff ; 14383b3a8eb9SGleb Smirnoff } 14393b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) 14403b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 14413b3a8eb9SGleb Smirnoff if (nchange != NULL) 14423b3a8eb9SGleb Smirnoff *nchange = xchange; 14433b3a8eb9SGleb Smirnoff if (ndel != NULL) 14443b3a8eb9SGleb Smirnoff *ndel = xdel; 14453b3a8eb9SGleb Smirnoff return (0); 14463b3a8eb9SGleb Smirnoff } 14473b3a8eb9SGleb Smirnoff 14483b3a8eb9SGleb Smirnoff int 14493b3a8eb9SGleb Smirnoff pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags) 14503b3a8eb9SGleb Smirnoff { 14513b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 14523b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 1453e86bddeaSKristof Provost struct pf_kruleset *rs; 14543b3a8eb9SGleb Smirnoff int xdel = 0; 14553b3a8eb9SGleb Smirnoff 14563b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1457e86bddeaSKristof Provost rs = pf_find_or_create_kruleset(trs->pfrt_anchor); 14583b3a8eb9SGleb Smirnoff if (rs == NULL) 14593b3a8eb9SGleb Smirnoff return (ENOMEM); 14603b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 14611e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 14623b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 14633b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 14643b3a8eb9SGleb Smirnoff continue; 14653b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE; 14663b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 14673b3a8eb9SGleb Smirnoff xdel++; 14683b3a8eb9SGleb Smirnoff } 14693b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 14703b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 14713b3a8eb9SGleb Smirnoff if (ticket != NULL) 14723b3a8eb9SGleb Smirnoff *ticket = ++rs->tticket; 14733b3a8eb9SGleb Smirnoff rs->topen = 1; 14743b3a8eb9SGleb Smirnoff } else 1475e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 14763b3a8eb9SGleb Smirnoff if (ndel != NULL) 14773b3a8eb9SGleb Smirnoff *ndel = xdel; 14783b3a8eb9SGleb Smirnoff return (0); 14793b3a8eb9SGleb Smirnoff } 14803b3a8eb9SGleb Smirnoff 14813b3a8eb9SGleb Smirnoff int 14823b3a8eb9SGleb Smirnoff pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size, 14833b3a8eb9SGleb Smirnoff int *nadd, int *naddr, u_int32_t ticket, int flags) 14843b3a8eb9SGleb Smirnoff { 14853b3a8eb9SGleb Smirnoff struct pfr_ktableworkq tableq; 14863b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 14873b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *rt, *shadow, key; 14883b3a8eb9SGleb Smirnoff struct pfr_kentry *p; 14893b3a8eb9SGleb Smirnoff struct pfr_addr *ad; 1490e86bddeaSKristof Provost struct pf_kruleset *rs; 14913b3a8eb9SGleb Smirnoff int i, rv, xadd = 0, xaddr = 0; 14923b3a8eb9SGleb Smirnoff 14933b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 14943b3a8eb9SGleb Smirnoff 14953b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO); 14963b3a8eb9SGleb Smirnoff if (size && !(flags & PFR_FLAG_ADDRSTOO)) 14973b3a8eb9SGleb Smirnoff return (EINVAL); 14983b3a8eb9SGleb Smirnoff if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK, 14993b3a8eb9SGleb Smirnoff flags & PFR_FLAG_USERIOCTL)) 15003b3a8eb9SGleb Smirnoff return (EINVAL); 1501e86bddeaSKristof Provost rs = pf_find_kruleset(tbl->pfrt_anchor); 15023b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 15033b3a8eb9SGleb Smirnoff return (EBUSY); 15043b3a8eb9SGleb Smirnoff tbl->pfrt_flags |= PFR_TFLAG_INACTIVE; 15053b3a8eb9SGleb Smirnoff SLIST_INIT(&tableq); 15061e9e3741SMarko Zec kt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, (struct pfr_ktable *)tbl); 15073b3a8eb9SGleb Smirnoff if (kt == NULL) { 15083b3a8eb9SGleb Smirnoff kt = pfr_create_ktable(tbl, 0, 1); 15093b3a8eb9SGleb Smirnoff if (kt == NULL) 15103b3a8eb9SGleb Smirnoff return (ENOMEM); 15113b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq); 15123b3a8eb9SGleb Smirnoff xadd++; 15133b3a8eb9SGleb Smirnoff if (!tbl->pfrt_anchor[0]) 15143b3a8eb9SGleb Smirnoff goto _skip; 15153b3a8eb9SGleb Smirnoff 15163b3a8eb9SGleb Smirnoff /* find or create root table */ 15173b3a8eb9SGleb Smirnoff bzero(&key, sizeof(key)); 15183b3a8eb9SGleb Smirnoff strlcpy(key.pfrkt_name, tbl->pfrt_name, sizeof(key.pfrkt_name)); 15191e9e3741SMarko Zec rt = RB_FIND(pfr_ktablehead, &V_pfr_ktables, &key); 15203b3a8eb9SGleb Smirnoff if (rt != NULL) { 15213b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 15223b3a8eb9SGleb Smirnoff goto _skip; 15233b3a8eb9SGleb Smirnoff } 15243b3a8eb9SGleb Smirnoff rt = pfr_create_ktable(&key.pfrkt_t, 0, 1); 15253b3a8eb9SGleb Smirnoff if (rt == NULL) { 15263b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15273b3a8eb9SGleb Smirnoff return (ENOMEM); 15283b3a8eb9SGleb Smirnoff } 15293b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq); 15303b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 15313b3a8eb9SGleb Smirnoff } else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE)) 15323b3a8eb9SGleb Smirnoff xadd++; 15333b3a8eb9SGleb Smirnoff _skip: 15343b3a8eb9SGleb Smirnoff shadow = pfr_create_ktable(tbl, 0, 0); 15353b3a8eb9SGleb Smirnoff if (shadow == NULL) { 15363b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15373b3a8eb9SGleb Smirnoff return (ENOMEM); 15383b3a8eb9SGleb Smirnoff } 15393b3a8eb9SGleb Smirnoff SLIST_INIT(&addrq); 15403b3a8eb9SGleb Smirnoff for (i = 0, ad = addr; i < size; i++, ad++) { 15413b3a8eb9SGleb Smirnoff if (pfr_validate_addr(ad)) 15423b3a8eb9SGleb Smirnoff senderr(EINVAL); 15433b3a8eb9SGleb Smirnoff if (pfr_lookup_addr(shadow, ad, 1) != NULL) 15443b3a8eb9SGleb Smirnoff continue; 154521121f9bSMark Johnston p = pfr_create_kentry(ad, 154621121f9bSMark Johnston (shadow->pfrkt_flags & PFR_TFLAG_COUNTERS) != 0); 15473b3a8eb9SGleb Smirnoff if (p == NULL) 15483b3a8eb9SGleb Smirnoff senderr(ENOMEM); 15493b3a8eb9SGleb Smirnoff if (pfr_route_kentry(shadow, p)) { 15503b3a8eb9SGleb Smirnoff pfr_destroy_kentry(p); 15513b3a8eb9SGleb Smirnoff continue; 15523b3a8eb9SGleb Smirnoff } 15533b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addrq, p, pfrke_workq); 15543b3a8eb9SGleb Smirnoff xaddr++; 15553b3a8eb9SGleb Smirnoff } 15563b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 15573b3a8eb9SGleb Smirnoff if (kt->pfrkt_shadow != NULL) 15583b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, 1); 15593b3a8eb9SGleb Smirnoff kt->pfrkt_flags |= PFR_TFLAG_INACTIVE; 15603b3a8eb9SGleb Smirnoff pfr_insert_ktables(&tableq); 15613b3a8eb9SGleb Smirnoff shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ? 15623b3a8eb9SGleb Smirnoff xaddr : NO_ADDRESSES; 15633b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = shadow; 15643b3a8eb9SGleb Smirnoff } else { 15653b3a8eb9SGleb Smirnoff pfr_clean_node_mask(shadow, &addrq); 15663b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 15673b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15683b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 15693b3a8eb9SGleb Smirnoff } 15703b3a8eb9SGleb Smirnoff if (nadd != NULL) 15713b3a8eb9SGleb Smirnoff *nadd = xadd; 15723b3a8eb9SGleb Smirnoff if (naddr != NULL) 15733b3a8eb9SGleb Smirnoff *naddr = xaddr; 15743b3a8eb9SGleb Smirnoff return (0); 15753b3a8eb9SGleb Smirnoff _bad: 15763b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 15773b3a8eb9SGleb Smirnoff pfr_destroy_ktables(&tableq, 0); 15783b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 15793b3a8eb9SGleb Smirnoff return (rv); 15803b3a8eb9SGleb Smirnoff } 15813b3a8eb9SGleb Smirnoff 15823b3a8eb9SGleb Smirnoff int 15833b3a8eb9SGleb Smirnoff pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags) 15843b3a8eb9SGleb Smirnoff { 15853b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 15863b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 1587e86bddeaSKristof Provost struct pf_kruleset *rs; 15883b3a8eb9SGleb Smirnoff int xdel = 0; 15893b3a8eb9SGleb Smirnoff 15903b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 15913b3a8eb9SGleb Smirnoff 15923b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1593e86bddeaSKristof Provost rs = pf_find_kruleset(trs->pfrt_anchor); 15943b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 15953b3a8eb9SGleb Smirnoff return (0); 15963b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 15971e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 15983b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 15993b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 16003b3a8eb9SGleb Smirnoff continue; 16013b3a8eb9SGleb Smirnoff p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE; 16023b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 16033b3a8eb9SGleb Smirnoff xdel++; 16043b3a8eb9SGleb Smirnoff } 16053b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 16063b3a8eb9SGleb Smirnoff pfr_setflags_ktables(&workq); 16073b3a8eb9SGleb Smirnoff rs->topen = 0; 1608e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 16093b3a8eb9SGleb Smirnoff } 16103b3a8eb9SGleb Smirnoff if (ndel != NULL) 16113b3a8eb9SGleb Smirnoff *ndel = xdel; 16123b3a8eb9SGleb Smirnoff return (0); 16133b3a8eb9SGleb Smirnoff } 16143b3a8eb9SGleb Smirnoff 16153b3a8eb9SGleb Smirnoff int 16163b3a8eb9SGleb Smirnoff pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd, 16173b3a8eb9SGleb Smirnoff int *nchange, int flags) 16183b3a8eb9SGleb Smirnoff { 16193b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 16203b3a8eb9SGleb Smirnoff struct pfr_ktableworkq workq; 1621e86bddeaSKristof Provost struct pf_kruleset *rs; 16223b3a8eb9SGleb Smirnoff int xadd = 0, xchange = 0; 16233b3a8eb9SGleb Smirnoff long tzero = time_second; 16243b3a8eb9SGleb Smirnoff 16253b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 16263b3a8eb9SGleb Smirnoff 16273b3a8eb9SGleb Smirnoff ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY); 1628e86bddeaSKristof Provost rs = pf_find_kruleset(trs->pfrt_anchor); 16293b3a8eb9SGleb Smirnoff if (rs == NULL || !rs->topen || ticket != rs->tticket) 16303b3a8eb9SGleb Smirnoff return (EBUSY); 16313b3a8eb9SGleb Smirnoff 16323b3a8eb9SGleb Smirnoff SLIST_INIT(&workq); 16331e9e3741SMarko Zec RB_FOREACH(p, pfr_ktablehead, &V_pfr_ktables) { 16343b3a8eb9SGleb Smirnoff if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) || 16353b3a8eb9SGleb Smirnoff pfr_skip_table(trs, p, 0)) 16363b3a8eb9SGleb Smirnoff continue; 16373b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&workq, p, pfrkt_workq); 16383b3a8eb9SGleb Smirnoff if (p->pfrkt_flags & PFR_TFLAG_ACTIVE) 16393b3a8eb9SGleb Smirnoff xchange++; 16403b3a8eb9SGleb Smirnoff else 16413b3a8eb9SGleb Smirnoff xadd++; 16423b3a8eb9SGleb Smirnoff } 16433b3a8eb9SGleb Smirnoff 16443b3a8eb9SGleb Smirnoff if (!(flags & PFR_FLAG_DUMMY)) { 16453b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(&workq); p != NULL; p = q) { 16463b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 16473b3a8eb9SGleb Smirnoff pfr_commit_ktable(p, tzero); 16483b3a8eb9SGleb Smirnoff } 16493b3a8eb9SGleb Smirnoff rs->topen = 0; 1650e86bddeaSKristof Provost pf_remove_if_empty_kruleset(rs); 16513b3a8eb9SGleb Smirnoff } 16523b3a8eb9SGleb Smirnoff if (nadd != NULL) 16533b3a8eb9SGleb Smirnoff *nadd = xadd; 16543b3a8eb9SGleb Smirnoff if (nchange != NULL) 16553b3a8eb9SGleb Smirnoff *nchange = xchange; 16563b3a8eb9SGleb Smirnoff 16573b3a8eb9SGleb Smirnoff return (0); 16583b3a8eb9SGleb Smirnoff } 16593b3a8eb9SGleb Smirnoff 16603b3a8eb9SGleb Smirnoff static void 16613b3a8eb9SGleb Smirnoff pfr_commit_ktable(struct pfr_ktable *kt, long tzero) 16623b3a8eb9SGleb Smirnoff { 1663e6aed06fSMark Johnston counter_u64_t *pkc, *qkc; 16643b3a8eb9SGleb Smirnoff struct pfr_ktable *shadow = kt->pfrkt_shadow; 16653b3a8eb9SGleb Smirnoff int nflags; 16663b3a8eb9SGleb Smirnoff 16673b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 16683b3a8eb9SGleb Smirnoff 16693b3a8eb9SGleb Smirnoff if (shadow->pfrkt_cnt == NO_ADDRESSES) { 16703b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 16713b3a8eb9SGleb Smirnoff pfr_clstats_ktable(kt, tzero, 1); 16723b3a8eb9SGleb Smirnoff } else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) { 16733b3a8eb9SGleb Smirnoff /* kt might contain addresses */ 16743b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq, addq, changeq, delq, garbageq; 16753b3a8eb9SGleb Smirnoff struct pfr_kentry *p, *q, *next; 16763b3a8eb9SGleb Smirnoff struct pfr_addr ad; 16773b3a8eb9SGleb Smirnoff 16783b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(shadow, &addrq, NULL, 0); 16793b3a8eb9SGleb Smirnoff pfr_mark_addrs(kt); 16803b3a8eb9SGleb Smirnoff SLIST_INIT(&addq); 16813b3a8eb9SGleb Smirnoff SLIST_INIT(&changeq); 16823b3a8eb9SGleb Smirnoff SLIST_INIT(&delq); 16833b3a8eb9SGleb Smirnoff SLIST_INIT(&garbageq); 16843b3a8eb9SGleb Smirnoff pfr_clean_node_mask(shadow, &addrq); 1685e6aed06fSMark Johnston SLIST_FOREACH_SAFE(p, &addrq, pfrke_workq, next) { 16863b3a8eb9SGleb Smirnoff pfr_copyout_addr(&ad, p); 16873b3a8eb9SGleb Smirnoff q = pfr_lookup_addr(kt, &ad, 1); 16883b3a8eb9SGleb Smirnoff if (q != NULL) { 16893b3a8eb9SGleb Smirnoff if (q->pfrke_not != p->pfrke_not) 16903b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&changeq, q, 16913b3a8eb9SGleb Smirnoff pfrke_workq); 1692e6aed06fSMark Johnston pkc = &p->pfrke_counters.pfrkc_counters; 1693e6aed06fSMark Johnston qkc = &q->pfrke_counters.pfrkc_counters; 1694e6aed06fSMark Johnston if ((*pkc == NULL) != (*qkc == NULL)) 1695e6aed06fSMark Johnston SWAP(counter_u64_t, *pkc, *qkc); 16963b3a8eb9SGleb Smirnoff q->pfrke_mark = 1; 16973b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq); 16983b3a8eb9SGleb Smirnoff } else { 169959048686SKristof Provost p->pfrke_counters.pfrkc_tzero = tzero; 17003b3a8eb9SGleb Smirnoff SLIST_INSERT_HEAD(&addq, p, pfrke_workq); 17013b3a8eb9SGleb Smirnoff } 17023b3a8eb9SGleb Smirnoff } 17033b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY); 17043b3a8eb9SGleb Smirnoff pfr_insert_kentries(kt, &addq, tzero); 17053b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &delq); 170621121f9bSMark Johnston pfr_clstats_kentries(kt, &changeq, tzero, INVERT_NEG_FLAG); 17073b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&garbageq); 17083b3a8eb9SGleb Smirnoff } else { 17093b3a8eb9SGleb Smirnoff /* kt cannot contain addresses */ 17103b3a8eb9SGleb Smirnoff SWAP(struct radix_node_head *, kt->pfrkt_ip4, 17113b3a8eb9SGleb Smirnoff shadow->pfrkt_ip4); 17123b3a8eb9SGleb Smirnoff SWAP(struct radix_node_head *, kt->pfrkt_ip6, 17133b3a8eb9SGleb Smirnoff shadow->pfrkt_ip6); 17143b3a8eb9SGleb Smirnoff SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt); 17153b3a8eb9SGleb Smirnoff pfr_clstats_ktable(kt, tzero, 1); 17163b3a8eb9SGleb Smirnoff } 17173b3a8eb9SGleb Smirnoff nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) | 17183b3a8eb9SGleb Smirnoff (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE) 17193b3a8eb9SGleb Smirnoff & ~PFR_TFLAG_INACTIVE; 17203b3a8eb9SGleb Smirnoff pfr_destroy_ktable(shadow, 0); 17213b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = NULL; 17223b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, nflags); 17233b3a8eb9SGleb Smirnoff } 17243b3a8eb9SGleb Smirnoff 17253b3a8eb9SGleb Smirnoff static int 17263b3a8eb9SGleb Smirnoff pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved) 17273b3a8eb9SGleb Smirnoff { 17283b3a8eb9SGleb Smirnoff int i; 17293b3a8eb9SGleb Smirnoff 17303b3a8eb9SGleb Smirnoff if (!tbl->pfrt_name[0]) 17313b3a8eb9SGleb Smirnoff return (-1); 17323b3a8eb9SGleb Smirnoff if (no_reserved && !strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR)) 17333b3a8eb9SGleb Smirnoff return (-1); 17343b3a8eb9SGleb Smirnoff if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1]) 17353b3a8eb9SGleb Smirnoff return (-1); 17363b3a8eb9SGleb Smirnoff for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++) 17373b3a8eb9SGleb Smirnoff if (tbl->pfrt_name[i]) 17383b3a8eb9SGleb Smirnoff return (-1); 17393b3a8eb9SGleb Smirnoff if (pfr_fix_anchor(tbl->pfrt_anchor)) 17403b3a8eb9SGleb Smirnoff return (-1); 17413b3a8eb9SGleb Smirnoff if (tbl->pfrt_flags & ~allowedflags) 17423b3a8eb9SGleb Smirnoff return (-1); 17433b3a8eb9SGleb Smirnoff return (0); 17443b3a8eb9SGleb Smirnoff } 17453b3a8eb9SGleb Smirnoff 17463b3a8eb9SGleb Smirnoff /* 17473b3a8eb9SGleb Smirnoff * Rewrite anchors referenced by tables to remove slashes 17483b3a8eb9SGleb Smirnoff * and check for validity. 17493b3a8eb9SGleb Smirnoff */ 17503b3a8eb9SGleb Smirnoff static int 17513b3a8eb9SGleb Smirnoff pfr_fix_anchor(char *anchor) 17523b3a8eb9SGleb Smirnoff { 17533b3a8eb9SGleb Smirnoff size_t siz = MAXPATHLEN; 17543b3a8eb9SGleb Smirnoff int i; 17553b3a8eb9SGleb Smirnoff 17563b3a8eb9SGleb Smirnoff if (anchor[0] == '/') { 17573b3a8eb9SGleb Smirnoff char *path; 17583b3a8eb9SGleb Smirnoff int off; 17593b3a8eb9SGleb Smirnoff 17603b3a8eb9SGleb Smirnoff path = anchor; 17613b3a8eb9SGleb Smirnoff off = 1; 17623b3a8eb9SGleb Smirnoff while (*++path == '/') 17633b3a8eb9SGleb Smirnoff off++; 17643b3a8eb9SGleb Smirnoff bcopy(path, anchor, siz - off); 17653b3a8eb9SGleb Smirnoff memset(anchor + siz - off, 0, off); 17663b3a8eb9SGleb Smirnoff } 17673b3a8eb9SGleb Smirnoff if (anchor[siz - 1]) 17683b3a8eb9SGleb Smirnoff return (-1); 17693b3a8eb9SGleb Smirnoff for (i = strlen(anchor); i < siz; i++) 17703b3a8eb9SGleb Smirnoff if (anchor[i]) 17713b3a8eb9SGleb Smirnoff return (-1); 17723b3a8eb9SGleb Smirnoff return (0); 17733b3a8eb9SGleb Smirnoff } 17743b3a8eb9SGleb Smirnoff 1775adfe2f6aSKristof Provost int 17763b3a8eb9SGleb Smirnoff pfr_table_count(struct pfr_table *filter, int flags) 17773b3a8eb9SGleb Smirnoff { 1778e86bddeaSKristof Provost struct pf_kruleset *rs; 17793b3a8eb9SGleb Smirnoff 17803b3a8eb9SGleb Smirnoff PF_RULES_ASSERT(); 17813b3a8eb9SGleb Smirnoff 17823b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_ALLRSETS) 17831e9e3741SMarko Zec return (V_pfr_ktable_cnt); 17843b3a8eb9SGleb Smirnoff if (filter->pfrt_anchor[0]) { 1785e86bddeaSKristof Provost rs = pf_find_kruleset(filter->pfrt_anchor); 17863b3a8eb9SGleb Smirnoff return ((rs != NULL) ? rs->tables : -1); 17873b3a8eb9SGleb Smirnoff } 17883b3a8eb9SGleb Smirnoff return (pf_main_ruleset.tables); 17893b3a8eb9SGleb Smirnoff } 17903b3a8eb9SGleb Smirnoff 17913b3a8eb9SGleb Smirnoff static int 17923b3a8eb9SGleb Smirnoff pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags) 17933b3a8eb9SGleb Smirnoff { 17943b3a8eb9SGleb Smirnoff if (flags & PFR_FLAG_ALLRSETS) 17953b3a8eb9SGleb Smirnoff return (0); 17963b3a8eb9SGleb Smirnoff if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor)) 17973b3a8eb9SGleb Smirnoff return (1); 17983b3a8eb9SGleb Smirnoff return (0); 17993b3a8eb9SGleb Smirnoff } 18003b3a8eb9SGleb Smirnoff 18013b3a8eb9SGleb Smirnoff static void 18023b3a8eb9SGleb Smirnoff pfr_insert_ktables(struct pfr_ktableworkq *workq) 18033b3a8eb9SGleb Smirnoff { 18043b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 18053b3a8eb9SGleb Smirnoff 18063b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrkt_workq) 18073b3a8eb9SGleb Smirnoff pfr_insert_ktable(p); 18083b3a8eb9SGleb Smirnoff } 18093b3a8eb9SGleb Smirnoff 18103b3a8eb9SGleb Smirnoff static void 18113b3a8eb9SGleb Smirnoff pfr_insert_ktable(struct pfr_ktable *kt) 18123b3a8eb9SGleb Smirnoff { 18133b3a8eb9SGleb Smirnoff 18143b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 18153b3a8eb9SGleb Smirnoff 18161e9e3741SMarko Zec RB_INSERT(pfr_ktablehead, &V_pfr_ktables, kt); 18171e9e3741SMarko Zec V_pfr_ktable_cnt++; 18183b3a8eb9SGleb Smirnoff if (kt->pfrkt_root != NULL) 18193b3a8eb9SGleb Smirnoff if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++) 18203b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt->pfrkt_root, 18213b3a8eb9SGleb Smirnoff kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR); 18223b3a8eb9SGleb Smirnoff } 18233b3a8eb9SGleb Smirnoff 18243b3a8eb9SGleb Smirnoff static void 18253b3a8eb9SGleb Smirnoff pfr_setflags_ktables(struct pfr_ktableworkq *workq) 18263b3a8eb9SGleb Smirnoff { 18273b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 18283b3a8eb9SGleb Smirnoff 18293b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(workq); p; p = q) { 18303b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 18313b3a8eb9SGleb Smirnoff pfr_setflags_ktable(p, p->pfrkt_nflags); 18323b3a8eb9SGleb Smirnoff } 18333b3a8eb9SGleb Smirnoff } 18343b3a8eb9SGleb Smirnoff 18353b3a8eb9SGleb Smirnoff static void 18363b3a8eb9SGleb Smirnoff pfr_setflags_ktable(struct pfr_ktable *kt, int newf) 18373b3a8eb9SGleb Smirnoff { 18383b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 1839b21826bfSKristof Provost struct pfr_walktree w; 18403b3a8eb9SGleb Smirnoff 18413b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 18423b3a8eb9SGleb Smirnoff 18433b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_REFERENCED) && 184487e4ca37SKristof Provost !(newf & PFR_TFLAG_REFDANCHOR) && 18453b3a8eb9SGleb Smirnoff !(newf & PFR_TFLAG_PERSIST)) 18463b3a8eb9SGleb Smirnoff newf &= ~PFR_TFLAG_ACTIVE; 18473b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_ACTIVE)) 18483b3a8eb9SGleb Smirnoff newf &= ~PFR_TFLAG_USRMASK; 18493b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_SETMASK)) { 18501e9e3741SMarko Zec RB_REMOVE(pfr_ktablehead, &V_pfr_ktables, kt); 18513b3a8eb9SGleb Smirnoff if (kt->pfrkt_root != NULL) 18523b3a8eb9SGleb Smirnoff if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]) 18533b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt->pfrkt_root, 18543b3a8eb9SGleb Smirnoff kt->pfrkt_root->pfrkt_flags & 18553b3a8eb9SGleb Smirnoff ~PFR_TFLAG_REFDANCHOR); 18563b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 1); 18571e9e3741SMarko Zec V_pfr_ktable_cnt--; 18583b3a8eb9SGleb Smirnoff return; 18593b3a8eb9SGleb Smirnoff } 1860b21826bfSKristof Provost if (newf & PFR_TFLAG_COUNTERS && ! (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) { 1861b21826bfSKristof Provost bzero(&w, sizeof(w)); 1862b21826bfSKristof Provost w.pfrw_op = PFRW_COUNTERS; 1863b21826bfSKristof Provost w.pfrw_flags |= PFR_TFLAG_COUNTERS; 1864b21826bfSKristof Provost kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 1865b21826bfSKristof Provost kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 1866b21826bfSKristof Provost } 1867b21826bfSKristof Provost if (! (newf & PFR_TFLAG_COUNTERS) && (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) { 1868b21826bfSKristof Provost bzero(&w, sizeof(w)); 1869b21826bfSKristof Provost w.pfrw_op = PFRW_COUNTERS; 1870b21826bfSKristof Provost w.pfrw_flags |= 0; 1871b21826bfSKristof Provost kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 1872b21826bfSKristof Provost kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 1873b21826bfSKristof Provost } 18743b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) { 18753b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 18763b3a8eb9SGleb Smirnoff pfr_remove_kentries(kt, &addrq); 18773b3a8eb9SGleb Smirnoff } 18783b3a8eb9SGleb Smirnoff if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) { 18793b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, 1); 18803b3a8eb9SGleb Smirnoff kt->pfrkt_shadow = NULL; 18813b3a8eb9SGleb Smirnoff } 18823b3a8eb9SGleb Smirnoff kt->pfrkt_flags = newf; 18833b3a8eb9SGleb Smirnoff } 18843b3a8eb9SGleb Smirnoff 18853b3a8eb9SGleb Smirnoff static void 18863b3a8eb9SGleb Smirnoff pfr_clstats_ktables(struct pfr_ktableworkq *workq, long tzero, int recurse) 18873b3a8eb9SGleb Smirnoff { 18883b3a8eb9SGleb Smirnoff struct pfr_ktable *p; 18893b3a8eb9SGleb Smirnoff 18903b3a8eb9SGleb Smirnoff SLIST_FOREACH(p, workq, pfrkt_workq) 18913b3a8eb9SGleb Smirnoff pfr_clstats_ktable(p, tzero, recurse); 18923b3a8eb9SGleb Smirnoff } 18933b3a8eb9SGleb Smirnoff 18943b3a8eb9SGleb Smirnoff static void 18953b3a8eb9SGleb Smirnoff pfr_clstats_ktable(struct pfr_ktable *kt, long tzero, int recurse) 18963b3a8eb9SGleb Smirnoff { 18973b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 189859048686SKristof Provost int pfr_dir, pfr_op; 18993b3a8eb9SGleb Smirnoff 1900dc1ab04eSMateusz Guzik MPASS(PF_TABLE_STATS_OWNED() || PF_RULES_WOWNED()); 1901dc1ab04eSMateusz Guzik 19023b3a8eb9SGleb Smirnoff if (recurse) { 19033b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 190421121f9bSMark Johnston pfr_clstats_kentries(kt, &addrq, tzero, 0); 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 ++) { 1908f92c21a2SMateusz Guzik pfr_kstate_counter_zero(&kt->pfrkt_packets[pfr_dir][pfr_op]); 1909f92c21a2SMateusz Guzik pfr_kstate_counter_zero(&kt->pfrkt_bytes[pfr_dir][pfr_op]); 191059048686SKristof Provost } 191159048686SKristof Provost } 1912f92c21a2SMateusz Guzik pfr_kstate_counter_zero(&kt->pfrkt_match); 1913f92c21a2SMateusz Guzik pfr_kstate_counter_zero(&kt->pfrkt_nomatch); 19143b3a8eb9SGleb Smirnoff kt->pfrkt_tzero = tzero; 19153b3a8eb9SGleb Smirnoff } 19163b3a8eb9SGleb Smirnoff 19173b3a8eb9SGleb Smirnoff static struct pfr_ktable * 19183b3a8eb9SGleb Smirnoff pfr_create_ktable(struct pfr_table *tbl, long tzero, int attachruleset) 19193b3a8eb9SGleb Smirnoff { 19203b3a8eb9SGleb Smirnoff struct pfr_ktable *kt; 1921e86bddeaSKristof Provost struct pf_kruleset *rs; 192259048686SKristof Provost int pfr_dir, pfr_op; 19233b3a8eb9SGleb Smirnoff 19243b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 19253b3a8eb9SGleb Smirnoff 19263b3a8eb9SGleb Smirnoff kt = malloc(sizeof(*kt), M_PFTABLE, M_NOWAIT|M_ZERO); 19273b3a8eb9SGleb Smirnoff if (kt == NULL) 19283b3a8eb9SGleb Smirnoff return (NULL); 19293b3a8eb9SGleb Smirnoff kt->pfrkt_t = *tbl; 19303b3a8eb9SGleb Smirnoff 19313b3a8eb9SGleb Smirnoff if (attachruleset) { 1932e86bddeaSKristof Provost rs = pf_find_or_create_kruleset(tbl->pfrt_anchor); 19333b3a8eb9SGleb Smirnoff if (!rs) { 19343b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 19353b3a8eb9SGleb Smirnoff return (NULL); 19363b3a8eb9SGleb Smirnoff } 19373b3a8eb9SGleb Smirnoff kt->pfrkt_rs = rs; 19383b3a8eb9SGleb Smirnoff rs->tables++; 19393b3a8eb9SGleb Smirnoff } 19403b3a8eb9SGleb Smirnoff 194159048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 194259048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 1943f92c21a2SMateusz Guzik if (pfr_kstate_counter_init( 1944f92c21a2SMateusz Guzik &kt->pfrkt_packets[pfr_dir][pfr_op], M_NOWAIT) != 0) { 194559048686SKristof Provost pfr_destroy_ktable(kt, 0); 194659048686SKristof Provost return (NULL); 194759048686SKristof Provost } 1948f92c21a2SMateusz Guzik if (pfr_kstate_counter_init( 1949f92c21a2SMateusz Guzik &kt->pfrkt_bytes[pfr_dir][pfr_op], M_NOWAIT) != 0) { 195059048686SKristof Provost pfr_destroy_ktable(kt, 0); 195159048686SKristof Provost return (NULL); 195259048686SKristof Provost } 195359048686SKristof Provost } 195459048686SKristof Provost } 1955f92c21a2SMateusz Guzik if (pfr_kstate_counter_init(&kt->pfrkt_match, M_NOWAIT) != 0) { 195659048686SKristof Provost pfr_destroy_ktable(kt, 0); 195759048686SKristof Provost return (NULL); 195859048686SKristof Provost } 195959048686SKristof Provost 1960f92c21a2SMateusz Guzik if (pfr_kstate_counter_init(&kt->pfrkt_nomatch, M_NOWAIT) != 0) { 196159048686SKristof Provost pfr_destroy_ktable(kt, 0); 196259048686SKristof Provost return (NULL); 196359048686SKristof Provost } 196459048686SKristof Provost 19653b3a8eb9SGleb Smirnoff if (!rn_inithead((void **)&kt->pfrkt_ip4, 19663b3a8eb9SGleb Smirnoff offsetof(struct sockaddr_in, sin_addr) * 8) || 19673b3a8eb9SGleb Smirnoff !rn_inithead((void **)&kt->pfrkt_ip6, 19683b3a8eb9SGleb Smirnoff offsetof(struct sockaddr_in6, sin6_addr) * 8)) { 19693b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 19703b3a8eb9SGleb Smirnoff return (NULL); 19713b3a8eb9SGleb Smirnoff } 19723b3a8eb9SGleb Smirnoff kt->pfrkt_tzero = tzero; 19733b3a8eb9SGleb Smirnoff 19743b3a8eb9SGleb Smirnoff return (kt); 19753b3a8eb9SGleb Smirnoff } 19763b3a8eb9SGleb Smirnoff 19773b3a8eb9SGleb Smirnoff static void 19783b3a8eb9SGleb Smirnoff pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr) 19793b3a8eb9SGleb Smirnoff { 19803b3a8eb9SGleb Smirnoff struct pfr_ktable *p, *q; 19813b3a8eb9SGleb Smirnoff 19823b3a8eb9SGleb Smirnoff for (p = SLIST_FIRST(workq); p; p = q) { 19833b3a8eb9SGleb Smirnoff q = SLIST_NEXT(p, pfrkt_workq); 19843b3a8eb9SGleb Smirnoff pfr_destroy_ktable(p, flushaddr); 19853b3a8eb9SGleb Smirnoff } 19863b3a8eb9SGleb Smirnoff } 19873b3a8eb9SGleb Smirnoff 19883b3a8eb9SGleb Smirnoff static void 19893b3a8eb9SGleb Smirnoff pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr) 19903b3a8eb9SGleb Smirnoff { 19913b3a8eb9SGleb Smirnoff struct pfr_kentryworkq addrq; 199259048686SKristof Provost int pfr_dir, pfr_op; 19933b3a8eb9SGleb Smirnoff 19943b3a8eb9SGleb Smirnoff if (flushaddr) { 19953b3a8eb9SGleb Smirnoff pfr_enqueue_addrs(kt, &addrq, NULL, 0); 19963b3a8eb9SGleb Smirnoff pfr_clean_node_mask(kt, &addrq); 19973b3a8eb9SGleb Smirnoff pfr_destroy_kentries(&addrq); 19983b3a8eb9SGleb Smirnoff } 199931f0d081SAlexander V. Chernikov if (kt->pfrkt_ip4 != NULL) 2000495a22b5SGleb Smirnoff rn_detachhead((void **)&kt->pfrkt_ip4); 200131f0d081SAlexander V. Chernikov if (kt->pfrkt_ip6 != NULL) 2002495a22b5SGleb Smirnoff rn_detachhead((void **)&kt->pfrkt_ip6); 20033b3a8eb9SGleb Smirnoff if (kt->pfrkt_shadow != NULL) 20043b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr); 20053b3a8eb9SGleb Smirnoff if (kt->pfrkt_rs != NULL) { 20063b3a8eb9SGleb Smirnoff kt->pfrkt_rs->tables--; 2007e86bddeaSKristof Provost pf_remove_if_empty_kruleset(kt->pfrkt_rs); 20083b3a8eb9SGleb Smirnoff } 200959048686SKristof Provost for (pfr_dir = 0; pfr_dir < PFR_DIR_MAX; pfr_dir ++) { 201059048686SKristof Provost for (pfr_op = 0; pfr_op < PFR_OP_TABLE_MAX; pfr_op ++) { 2011f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(&kt->pfrkt_packets[pfr_dir][pfr_op]); 2012f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(&kt->pfrkt_bytes[pfr_dir][pfr_op]); 201359048686SKristof Provost } 201459048686SKristof Provost } 2015f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(&kt->pfrkt_match); 2016f92c21a2SMateusz Guzik pfr_kstate_counter_deinit(&kt->pfrkt_nomatch); 201759048686SKristof Provost 20183b3a8eb9SGleb Smirnoff free(kt, M_PFTABLE); 20193b3a8eb9SGleb Smirnoff } 20203b3a8eb9SGleb Smirnoff 20213b3a8eb9SGleb Smirnoff static int 20223b3a8eb9SGleb Smirnoff pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q) 20233b3a8eb9SGleb Smirnoff { 20243b3a8eb9SGleb Smirnoff int d; 20253b3a8eb9SGleb Smirnoff 20263b3a8eb9SGleb Smirnoff if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE))) 20273b3a8eb9SGleb Smirnoff return (d); 20283b3a8eb9SGleb Smirnoff return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor)); 20293b3a8eb9SGleb Smirnoff } 20303b3a8eb9SGleb Smirnoff 20313b3a8eb9SGleb Smirnoff static struct pfr_ktable * 20323b3a8eb9SGleb Smirnoff pfr_lookup_table(struct pfr_table *tbl) 20333b3a8eb9SGleb Smirnoff { 20343b3a8eb9SGleb Smirnoff /* struct pfr_ktable start like a struct pfr_table */ 20351e9e3741SMarko Zec return (RB_FIND(pfr_ktablehead, &V_pfr_ktables, 20363b3a8eb9SGleb Smirnoff (struct pfr_ktable *)tbl)); 20373b3a8eb9SGleb Smirnoff } 20383b3a8eb9SGleb Smirnoff 20393b3a8eb9SGleb Smirnoff int 20403b3a8eb9SGleb Smirnoff pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af) 20413b3a8eb9SGleb Smirnoff { 20423b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = NULL; 20433b3a8eb9SGleb Smirnoff int match; 20443b3a8eb9SGleb Smirnoff 20453b3a8eb9SGleb Smirnoff PF_RULES_RASSERT(); 20463b3a8eb9SGleb Smirnoff 20473b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 20483b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 20493b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 20503b3a8eb9SGleb Smirnoff return (0); 20513b3a8eb9SGleb Smirnoff 20523b3a8eb9SGleb Smirnoff switch (af) { 20533b3a8eb9SGleb Smirnoff #ifdef INET 20543b3a8eb9SGleb Smirnoff case AF_INET: 20553b3a8eb9SGleb Smirnoff { 20563b3a8eb9SGleb Smirnoff struct sockaddr_in sin; 20573b3a8eb9SGleb Smirnoff 20583b3a8eb9SGleb Smirnoff bzero(&sin, sizeof(sin)); 20593b3a8eb9SGleb Smirnoff sin.sin_len = sizeof(sin); 20603b3a8eb9SGleb Smirnoff sin.sin_family = AF_INET; 20613b3a8eb9SGleb Smirnoff sin.sin_addr.s_addr = a->addr32[0]; 206261eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh); 20633b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20643b3a8eb9SGleb Smirnoff ke = NULL; 20653b3a8eb9SGleb Smirnoff break; 20663b3a8eb9SGleb Smirnoff } 20673b3a8eb9SGleb Smirnoff #endif /* INET */ 20683b3a8eb9SGleb Smirnoff #ifdef INET6 20693b3a8eb9SGleb Smirnoff case AF_INET6: 20703b3a8eb9SGleb Smirnoff { 20713b3a8eb9SGleb Smirnoff struct sockaddr_in6 sin6; 20723b3a8eb9SGleb Smirnoff 20733b3a8eb9SGleb Smirnoff bzero(&sin6, sizeof(sin6)); 20743b3a8eb9SGleb Smirnoff sin6.sin6_len = sizeof(sin6); 20753b3a8eb9SGleb Smirnoff sin6.sin6_family = AF_INET6; 20763b3a8eb9SGleb Smirnoff bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 207761eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh); 20783b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 20793b3a8eb9SGleb Smirnoff ke = NULL; 20803b3a8eb9SGleb Smirnoff break; 20813b3a8eb9SGleb Smirnoff } 20823b3a8eb9SGleb Smirnoff #endif /* INET6 */ 20833b3a8eb9SGleb Smirnoff } 20843b3a8eb9SGleb Smirnoff match = (ke && !ke->pfrke_not); 20853b3a8eb9SGleb Smirnoff if (match) 2086f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_match, 1); 20873b3a8eb9SGleb Smirnoff else 2088f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_nomatch, 1); 20893b3a8eb9SGleb Smirnoff return (match); 20903b3a8eb9SGleb Smirnoff } 20913b3a8eb9SGleb Smirnoff 20923b3a8eb9SGleb Smirnoff void 20933b3a8eb9SGleb Smirnoff pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af, 20943b3a8eb9SGleb Smirnoff u_int64_t len, int dir_out, int op_pass, int notrule) 20953b3a8eb9SGleb Smirnoff { 20963b3a8eb9SGleb Smirnoff struct pfr_kentry *ke = NULL; 20973b3a8eb9SGleb Smirnoff 20983b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 20993b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 21003b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 21013b3a8eb9SGleb Smirnoff return; 21023b3a8eb9SGleb Smirnoff 21033b3a8eb9SGleb Smirnoff switch (af) { 21043b3a8eb9SGleb Smirnoff #ifdef INET 21053b3a8eb9SGleb Smirnoff case AF_INET: 21063b3a8eb9SGleb Smirnoff { 21073b3a8eb9SGleb Smirnoff struct sockaddr_in sin; 21083b3a8eb9SGleb Smirnoff 21097348c524SGleb Smirnoff bzero(&sin, sizeof(sin)); 21103b3a8eb9SGleb Smirnoff sin.sin_len = sizeof(sin); 21113b3a8eb9SGleb Smirnoff sin.sin_family = AF_INET; 21123b3a8eb9SGleb Smirnoff sin.sin_addr.s_addr = a->addr32[0]; 211361eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin, &kt->pfrkt_ip4->rh); 21143b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 21153b3a8eb9SGleb Smirnoff ke = NULL; 21163b3a8eb9SGleb Smirnoff break; 21173b3a8eb9SGleb Smirnoff } 21183b3a8eb9SGleb Smirnoff #endif /* INET */ 21193b3a8eb9SGleb Smirnoff #ifdef INET6 21203b3a8eb9SGleb Smirnoff case AF_INET6: 21213b3a8eb9SGleb Smirnoff { 21223b3a8eb9SGleb Smirnoff struct sockaddr_in6 sin6; 21233b3a8eb9SGleb Smirnoff 21247348c524SGleb Smirnoff bzero(&sin6, sizeof(sin6)); 21253b3a8eb9SGleb Smirnoff sin6.sin6_len = sizeof(sin6); 21263b3a8eb9SGleb Smirnoff sin6.sin6_family = AF_INET6; 21273b3a8eb9SGleb Smirnoff bcopy(a, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); 212861eee0e2SAlexander V. Chernikov ke = (struct pfr_kentry *)rn_match(&sin6, &kt->pfrkt_ip6->rh); 21293b3a8eb9SGleb Smirnoff if (ke && KENTRY_RNF_ROOT(ke)) 21303b3a8eb9SGleb Smirnoff ke = NULL; 21313b3a8eb9SGleb Smirnoff break; 21323b3a8eb9SGleb Smirnoff } 21333b3a8eb9SGleb Smirnoff #endif /* INET6 */ 21343b3a8eb9SGleb Smirnoff default: 21357348c524SGleb Smirnoff panic("%s: unknown address family %u", __func__, af); 21363b3a8eb9SGleb Smirnoff } 21373b3a8eb9SGleb Smirnoff if ((ke == NULL || ke->pfrke_not) != notrule) { 21383b3a8eb9SGleb Smirnoff if (op_pass != PFR_OP_PASS) 2139032dff66SKristof Provost DPFPRINTF(PF_DEBUG_URGENT, 2140032dff66SKristof Provost ("pfr_update_stats: assertion failed.\n")); 21413b3a8eb9SGleb Smirnoff op_pass = PFR_OP_XPASS; 21423b3a8eb9SGleb Smirnoff } 2143f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_packets[dir_out][op_pass], 1); 2144f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_bytes[dir_out][op_pass], len); 21453b3a8eb9SGleb Smirnoff if (ke != NULL && op_pass != PFR_OP_XPASS && 21463b3a8eb9SGleb Smirnoff (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) { 2147c1be8399SMark Johnston counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters, 2148c1be8399SMark Johnston dir_out, op_pass, PFR_TYPE_PACKETS), 1); 2149c1be8399SMark Johnston counter_u64_add(pfr_kentry_counter(&ke->pfrke_counters, 2150c1be8399SMark Johnston dir_out, op_pass, PFR_TYPE_BYTES), len); 21513b3a8eb9SGleb Smirnoff } 21523b3a8eb9SGleb Smirnoff } 21533b3a8eb9SGleb Smirnoff 21543b3a8eb9SGleb Smirnoff struct pfr_ktable * 2155*812839e5SKristof Provost pfr_eth_attach_table(struct pf_keth_ruleset *rs, char *name) 2156*812839e5SKristof Provost { 2157*812839e5SKristof Provost struct pfr_ktable *kt, *rt; 2158*812839e5SKristof Provost struct pfr_table tbl; 2159*812839e5SKristof Provost struct pf_keth_anchor *ac = rs->anchor; 2160*812839e5SKristof Provost 2161*812839e5SKristof Provost PF_RULES_WASSERT(); 2162*812839e5SKristof Provost 2163*812839e5SKristof Provost bzero(&tbl, sizeof(tbl)); 2164*812839e5SKristof Provost strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)); 2165*812839e5SKristof Provost if (ac != NULL) 2166*812839e5SKristof Provost strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor)); 2167*812839e5SKristof Provost kt = pfr_lookup_table(&tbl); 2168*812839e5SKristof Provost if (kt == NULL) { 2169*812839e5SKristof Provost kt = pfr_create_ktable(&tbl, time_second, 1); 2170*812839e5SKristof Provost if (kt == NULL) 2171*812839e5SKristof Provost return (NULL); 2172*812839e5SKristof Provost if (ac != NULL) { 2173*812839e5SKristof Provost bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor)); 2174*812839e5SKristof Provost rt = pfr_lookup_table(&tbl); 2175*812839e5SKristof Provost if (rt == NULL) { 2176*812839e5SKristof Provost rt = pfr_create_ktable(&tbl, 0, 1); 2177*812839e5SKristof Provost if (rt == NULL) { 2178*812839e5SKristof Provost pfr_destroy_ktable(kt, 0); 2179*812839e5SKristof Provost return (NULL); 2180*812839e5SKristof Provost } 2181*812839e5SKristof Provost pfr_insert_ktable(rt); 2182*812839e5SKristof Provost } 2183*812839e5SKristof Provost kt->pfrkt_root = rt; 2184*812839e5SKristof Provost } 2185*812839e5SKristof Provost pfr_insert_ktable(kt); 2186*812839e5SKristof Provost } 2187*812839e5SKristof Provost if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++) 2188*812839e5SKristof Provost pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED); 2189*812839e5SKristof Provost return (kt); 2190*812839e5SKristof Provost } 2191*812839e5SKristof Provost 2192*812839e5SKristof Provost struct pfr_ktable * 2193e86bddeaSKristof Provost pfr_attach_table(struct pf_kruleset *rs, char *name) 21943b3a8eb9SGleb Smirnoff { 21953b3a8eb9SGleb Smirnoff struct pfr_ktable *kt, *rt; 21963b3a8eb9SGleb Smirnoff struct pfr_table tbl; 2197e86bddeaSKristof Provost struct pf_kanchor *ac = rs->anchor; 21983b3a8eb9SGleb Smirnoff 21993b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 22003b3a8eb9SGleb Smirnoff 22013b3a8eb9SGleb Smirnoff bzero(&tbl, sizeof(tbl)); 22023b3a8eb9SGleb Smirnoff strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name)); 22033b3a8eb9SGleb Smirnoff if (ac != NULL) 22043b3a8eb9SGleb Smirnoff strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor)); 22053b3a8eb9SGleb Smirnoff kt = pfr_lookup_table(&tbl); 22063b3a8eb9SGleb Smirnoff if (kt == NULL) { 22073b3a8eb9SGleb Smirnoff kt = pfr_create_ktable(&tbl, time_second, 1); 22083b3a8eb9SGleb Smirnoff if (kt == NULL) 22093b3a8eb9SGleb Smirnoff return (NULL); 22103b3a8eb9SGleb Smirnoff if (ac != NULL) { 22113b3a8eb9SGleb Smirnoff bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor)); 22123b3a8eb9SGleb Smirnoff rt = pfr_lookup_table(&tbl); 22133b3a8eb9SGleb Smirnoff if (rt == NULL) { 22143b3a8eb9SGleb Smirnoff rt = pfr_create_ktable(&tbl, 0, 1); 22153b3a8eb9SGleb Smirnoff if (rt == NULL) { 22163b3a8eb9SGleb Smirnoff pfr_destroy_ktable(kt, 0); 22173b3a8eb9SGleb Smirnoff return (NULL); 22183b3a8eb9SGleb Smirnoff } 22193b3a8eb9SGleb Smirnoff pfr_insert_ktable(rt); 22203b3a8eb9SGleb Smirnoff } 22213b3a8eb9SGleb Smirnoff kt->pfrkt_root = rt; 22223b3a8eb9SGleb Smirnoff } 22233b3a8eb9SGleb Smirnoff pfr_insert_ktable(kt); 22243b3a8eb9SGleb Smirnoff } 22253b3a8eb9SGleb Smirnoff if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++) 22263b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED); 22273b3a8eb9SGleb Smirnoff return (kt); 22283b3a8eb9SGleb Smirnoff } 22293b3a8eb9SGleb Smirnoff 22303b3a8eb9SGleb Smirnoff void 22313b3a8eb9SGleb Smirnoff pfr_detach_table(struct pfr_ktable *kt) 22323b3a8eb9SGleb Smirnoff { 22333b3a8eb9SGleb Smirnoff 22343b3a8eb9SGleb Smirnoff PF_RULES_WASSERT(); 22353b3a8eb9SGleb Smirnoff KASSERT(kt->pfrkt_refcnt[PFR_REFCNT_RULE] > 0, ("%s: refcount %d\n", 22363b3a8eb9SGleb Smirnoff __func__, kt->pfrkt_refcnt[PFR_REFCNT_RULE])); 22373b3a8eb9SGleb Smirnoff 22383b3a8eb9SGleb Smirnoff if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE]) 22393b3a8eb9SGleb Smirnoff pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED); 22403b3a8eb9SGleb Smirnoff } 22413b3a8eb9SGleb Smirnoff 22423b3a8eb9SGleb Smirnoff int 22433b3a8eb9SGleb Smirnoff pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter, 22443b3a8eb9SGleb Smirnoff sa_family_t af) 22453b3a8eb9SGleb Smirnoff { 22463b3a8eb9SGleb Smirnoff struct pf_addr *addr, *cur, *mask; 22473b3a8eb9SGleb Smirnoff union sockaddr_union uaddr, umask; 22483b3a8eb9SGleb Smirnoff struct pfr_kentry *ke, *ke2 = NULL; 22493b3a8eb9SGleb Smirnoff int idx = -1, use_counter = 0; 22503b3a8eb9SGleb Smirnoff 22518cceacc0SKristof Provost MPASS(pidx != NULL); 22528cceacc0SKristof Provost 22533b3a8eb9SGleb Smirnoff switch (af) { 22543b3a8eb9SGleb Smirnoff case AF_INET: 22553b3a8eb9SGleb Smirnoff uaddr.sin.sin_len = sizeof(struct sockaddr_in); 22563b3a8eb9SGleb Smirnoff uaddr.sin.sin_family = AF_INET; 22573b3a8eb9SGleb Smirnoff break; 22583b3a8eb9SGleb Smirnoff case AF_INET6: 22593b3a8eb9SGleb Smirnoff uaddr.sin6.sin6_len = sizeof(struct sockaddr_in6); 22603b3a8eb9SGleb Smirnoff uaddr.sin6.sin6_family = AF_INET6; 22613b3a8eb9SGleb Smirnoff break; 22623b3a8eb9SGleb Smirnoff } 22633b3a8eb9SGleb Smirnoff addr = SUNION2PF(&uaddr, af); 22643b3a8eb9SGleb Smirnoff 22653b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL) 22663b3a8eb9SGleb Smirnoff kt = kt->pfrkt_root; 22673b3a8eb9SGleb Smirnoff if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE)) 22683b3a8eb9SGleb Smirnoff return (-1); 22693b3a8eb9SGleb Smirnoff 22703b3a8eb9SGleb Smirnoff idx = *pidx; 22713b3a8eb9SGleb Smirnoff if (counter != NULL && idx >= 0) 22723b3a8eb9SGleb Smirnoff use_counter = 1; 22733b3a8eb9SGleb Smirnoff if (idx < 0) 22743b3a8eb9SGleb Smirnoff idx = 0; 22753b3a8eb9SGleb Smirnoff 22763b3a8eb9SGleb Smirnoff _next_block: 22773b3a8eb9SGleb Smirnoff ke = pfr_kentry_byidx(kt, idx, af); 22783b3a8eb9SGleb Smirnoff if (ke == NULL) { 2279f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_nomatch, 1); 22803b3a8eb9SGleb Smirnoff return (1); 22813b3a8eb9SGleb Smirnoff } 22823b3a8eb9SGleb Smirnoff pfr_prepare_network(&umask, af, ke->pfrke_net); 22833b3a8eb9SGleb Smirnoff cur = SUNION2PF(&ke->pfrke_sa, af); 22843b3a8eb9SGleb Smirnoff mask = SUNION2PF(&umask, af); 22853b3a8eb9SGleb Smirnoff 22863b3a8eb9SGleb Smirnoff if (use_counter) { 22873b3a8eb9SGleb Smirnoff /* is supplied address within block? */ 22883b3a8eb9SGleb Smirnoff if (!PF_MATCHA(0, cur, mask, counter, af)) { 22893b3a8eb9SGleb Smirnoff /* no, go to next block in table */ 22903b3a8eb9SGleb Smirnoff idx++; 22913b3a8eb9SGleb Smirnoff use_counter = 0; 22923b3a8eb9SGleb Smirnoff goto _next_block; 22933b3a8eb9SGleb Smirnoff } 22943b3a8eb9SGleb Smirnoff PF_ACPY(addr, counter, af); 22953b3a8eb9SGleb Smirnoff } else { 22963b3a8eb9SGleb Smirnoff /* use first address of block */ 22973b3a8eb9SGleb Smirnoff PF_ACPY(addr, cur, af); 22983b3a8eb9SGleb Smirnoff } 22993b3a8eb9SGleb Smirnoff 23003b3a8eb9SGleb Smirnoff if (!KENTRY_NETWORK(ke)) { 23013b3a8eb9SGleb Smirnoff /* this is a single IP address - no possible nested block */ 23023b3a8eb9SGleb Smirnoff PF_ACPY(counter, addr, af); 23033b3a8eb9SGleb Smirnoff *pidx = idx; 2304f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_match, 1); 23053b3a8eb9SGleb Smirnoff return (0); 23063b3a8eb9SGleb Smirnoff } 23073b3a8eb9SGleb Smirnoff for (;;) { 23083b3a8eb9SGleb Smirnoff /* we don't want to use a nested block */ 23093b3a8eb9SGleb Smirnoff switch (af) { 23103b3a8eb9SGleb Smirnoff case AF_INET: 23113b3a8eb9SGleb Smirnoff ke2 = (struct pfr_kentry *)rn_match(&uaddr, 231261eee0e2SAlexander V. Chernikov &kt->pfrkt_ip4->rh); 23133b3a8eb9SGleb Smirnoff break; 23143b3a8eb9SGleb Smirnoff case AF_INET6: 23153b3a8eb9SGleb Smirnoff ke2 = (struct pfr_kentry *)rn_match(&uaddr, 231661eee0e2SAlexander V. Chernikov &kt->pfrkt_ip6->rh); 23173b3a8eb9SGleb Smirnoff break; 23183b3a8eb9SGleb Smirnoff } 23193b3a8eb9SGleb Smirnoff /* no need to check KENTRY_RNF_ROOT() here */ 23203b3a8eb9SGleb Smirnoff if (ke2 == ke) { 23213b3a8eb9SGleb Smirnoff /* lookup return the same block - perfect */ 23223b3a8eb9SGleb Smirnoff PF_ACPY(counter, addr, af); 23233b3a8eb9SGleb Smirnoff *pidx = idx; 2324f92c21a2SMateusz Guzik pfr_kstate_counter_add(&kt->pfrkt_match, 1); 23253b3a8eb9SGleb Smirnoff return (0); 23263b3a8eb9SGleb Smirnoff } 23273b3a8eb9SGleb Smirnoff 23283b3a8eb9SGleb Smirnoff /* we need to increase the counter past the nested block */ 23293b3a8eb9SGleb Smirnoff pfr_prepare_network(&umask, AF_INET, ke2->pfrke_net); 23303b3a8eb9SGleb Smirnoff PF_POOLMASK(addr, addr, SUNION2PF(&umask, af), &pfr_ffaddr, af); 23313b3a8eb9SGleb Smirnoff PF_AINC(addr, af); 23323b3a8eb9SGleb Smirnoff if (!PF_MATCHA(0, cur, mask, addr, af)) { 23333b3a8eb9SGleb Smirnoff /* ok, we reached the end of our main block */ 23343b3a8eb9SGleb Smirnoff /* go to next block in table */ 23353b3a8eb9SGleb Smirnoff idx++; 23363b3a8eb9SGleb Smirnoff use_counter = 0; 23373b3a8eb9SGleb Smirnoff goto _next_block; 23383b3a8eb9SGleb Smirnoff } 23393b3a8eb9SGleb Smirnoff } 23403b3a8eb9SGleb Smirnoff } 23413b3a8eb9SGleb Smirnoff 23423b3a8eb9SGleb Smirnoff static struct pfr_kentry * 23433b3a8eb9SGleb Smirnoff pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af) 23443b3a8eb9SGleb Smirnoff { 23453b3a8eb9SGleb Smirnoff struct pfr_walktree w; 23463b3a8eb9SGleb Smirnoff 23473b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 23483b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_POOL_GET; 23493b3a8eb9SGleb Smirnoff w.pfrw_cnt = idx; 23503b3a8eb9SGleb Smirnoff 23513b3a8eb9SGleb Smirnoff switch (af) { 23523b3a8eb9SGleb Smirnoff #ifdef INET 23533b3a8eb9SGleb Smirnoff case AF_INET: 235461eee0e2SAlexander V. Chernikov kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 23553b3a8eb9SGleb Smirnoff return (w.pfrw_kentry); 23563b3a8eb9SGleb Smirnoff #endif /* INET */ 23573b3a8eb9SGleb Smirnoff #ifdef INET6 23583b3a8eb9SGleb Smirnoff case AF_INET6: 235961eee0e2SAlexander V. Chernikov kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 23603b3a8eb9SGleb Smirnoff return (w.pfrw_kentry); 23613b3a8eb9SGleb Smirnoff #endif /* INET6 */ 23623b3a8eb9SGleb Smirnoff default: 23633b3a8eb9SGleb Smirnoff return (NULL); 23643b3a8eb9SGleb Smirnoff } 23653b3a8eb9SGleb Smirnoff } 23663b3a8eb9SGleb Smirnoff 23673b3a8eb9SGleb Smirnoff void 23683b3a8eb9SGleb Smirnoff pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn) 23693b3a8eb9SGleb Smirnoff { 23703b3a8eb9SGleb Smirnoff struct pfr_walktree w; 23713b3a8eb9SGleb Smirnoff 23723b3a8eb9SGleb Smirnoff bzero(&w, sizeof(w)); 23733b3a8eb9SGleb Smirnoff w.pfrw_op = PFRW_DYNADDR_UPDATE; 23743b3a8eb9SGleb Smirnoff w.pfrw_dyn = dyn; 23753b3a8eb9SGleb Smirnoff 23763b3a8eb9SGleb Smirnoff dyn->pfid_acnt4 = 0; 23773b3a8eb9SGleb Smirnoff dyn->pfid_acnt6 = 0; 23783b3a8eb9SGleb Smirnoff if (!dyn->pfid_af || dyn->pfid_af == AF_INET) 237961eee0e2SAlexander V. Chernikov kt->pfrkt_ip4->rnh_walktree(&kt->pfrkt_ip4->rh, pfr_walktree, &w); 23803b3a8eb9SGleb Smirnoff if (!dyn->pfid_af || dyn->pfid_af == AF_INET6) 238161eee0e2SAlexander V. Chernikov kt->pfrkt_ip6->rnh_walktree(&kt->pfrkt_ip6->rh, pfr_walktree, &w); 23823b3a8eb9SGleb Smirnoff } 2383