1 /* $OpenBSD: pf_table.c,v 1.79 2008/10/08 06:24:50 mcbride Exp $ */
4 * Copyright (c) 2002 Cedric Berger
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include "opt_inet6.h"
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <netinet/ip_ipsp.h>
58 #include <net/pfvar.h>
60 #define ACCEPT_FLAGS(flags, oklist) \
62 if ((flags & ~(oklist)) & \
69 _copyin(const void *uaddr, void *kaddr, size_t len)
74 r = copyin(uaddr, kaddr, len);
81 _copyout(const void *uaddr, void *kaddr, size_t len)
86 r = copyout(uaddr, kaddr, len);
92 #define COPYIN(from, to, size, flags) \
93 ((flags & PFR_FLAG_USERIOCTL) ? \
94 _copyin((from), (to), (size)) : \
95 (bcopy((from), (to), (size)), 0))
97 #define COPYOUT(from, to, size, flags) \
98 ((flags & PFR_FLAG_USERIOCTL) ? \
99 _copyout((from), (to), (size)) : \
100 (bcopy((from), (to), (size)), 0))
103 #define COPYIN(from, to, size, flags) \
104 ((flags & PFR_FLAG_USERIOCTL) ? \
105 copyin((from), (to), (size)) : \
106 (bcopy((from), (to), (size)), 0))
108 #define COPYOUT(from, to, size, flags) \
109 ((flags & PFR_FLAG_USERIOCTL) ? \
110 copyout((from), (to), (size)) : \
111 (bcopy((from), (to), (size)), 0))
114 #define FILLIN_SIN(sin, addr) \
116 (sin).sin_len = sizeof(sin); \
117 (sin).sin_family = AF_INET; \
118 (sin).sin_addr = (addr); \
121 #define FILLIN_SIN6(sin6, addr) \
123 (sin6).sin6_len = sizeof(sin6); \
124 (sin6).sin6_family = AF_INET6; \
125 (sin6).sin6_addr = (addr); \
128 #define SWAP(type, a1, a2) \
135 #define SUNION2PF(su, af) (((af)==AF_INET) ? \
136 (struct pf_addr *)&(su)->sin.sin_addr : \
137 (struct pf_addr *)&(su)->sin6.sin6_addr)
139 #define AF_BITS(af) (((af)==AF_INET)?32:128)
140 #define ADDR_NETWORK(ad) ((ad)->pfra_net < AF_BITS((ad)->pfra_af))
141 #define KENTRY_NETWORK(ke) ((ke)->pfrke_net < AF_BITS((ke)->pfrke_af))
142 #define KENTRY_RNF_ROOT(ke) \
143 ((((struct radix_node *)(ke))->rn_flags & RNF_ROOT) != 0)
145 #define NO_ADDRESSES (-1)
146 #define ENQUEUE_UNMARKED_ONLY (1)
147 #define INVERT_NEG_FLAG (1)
149 struct pfr_walktree {
160 struct pfr_addr *pfrw1_addr;
161 struct pfr_astats *pfrw1_astats;
162 struct pfr_kentryworkq *pfrw1_workq;
163 struct pfr_kentry *pfrw1_kentry;
164 struct pfi_dynaddr *pfrw1_dyn;
169 #define pfrw_addr pfrw_1.pfrw1_addr
170 #define pfrw_astats pfrw_1.pfrw1_astats
171 #define pfrw_workq pfrw_1.pfrw1_workq
172 #define pfrw_kentry pfrw_1.pfrw1_kentry
173 #define pfrw_dyn pfrw_1.pfrw1_dyn
174 #define pfrw_cnt pfrw_free
176 #define senderr(e) do { rv = (e); goto _bad; } while (0)
179 VNET_DEFINE(uma_zone_t, pfr_ktable_pl);
180 VNET_DEFINE(uma_zone_t, pfr_kentry_pl);
181 VNET_DEFINE(uma_zone_t, pfr_kcounters_pl);
182 VNET_DEFINE(struct sockaddr_in, pfr_sin);
183 #define V_pfr_sin VNET(pfr_sin)
184 VNET_DEFINE(struct sockaddr_in6, pfr_sin6);
185 #define V_pfr_sin6 VNET(pfr_sin6)
186 VNET_DEFINE(union sockaddr_union, pfr_mask);
187 #define V_pfr_mask VNET(pfr_mask)
188 VNET_DEFINE(struct pf_addr, pfr_ffaddr);
189 #define V_pfr_ffaddr VNET(pfr_ffaddr)
191 struct pool pfr_ktable_pl;
192 struct pool pfr_kentry_pl;
193 struct pool pfr_kcounters_pl;
194 struct sockaddr_in pfr_sin;
195 struct sockaddr_in6 pfr_sin6;
196 union sockaddr_union pfr_mask;
197 struct pf_addr pfr_ffaddr;
200 void pfr_copyout_addr(struct pfr_addr *,
201 struct pfr_kentry *ke);
202 int pfr_validate_addr(struct pfr_addr *);
203 void pfr_enqueue_addrs(struct pfr_ktable *,
204 struct pfr_kentryworkq *, int *, int);
205 void pfr_mark_addrs(struct pfr_ktable *);
206 struct pfr_kentry *pfr_lookup_addr(struct pfr_ktable *,
207 struct pfr_addr *, int);
208 struct pfr_kentry *pfr_create_kentry(struct pfr_addr *, int);
209 void pfr_destroy_kentries(struct pfr_kentryworkq *);
210 void pfr_destroy_kentry(struct pfr_kentry *);
211 void pfr_insert_kentries(struct pfr_ktable *,
212 struct pfr_kentryworkq *, long);
213 void pfr_remove_kentries(struct pfr_ktable *,
214 struct pfr_kentryworkq *);
215 void pfr_clstats_kentries(struct pfr_kentryworkq *, long,
217 void pfr_reset_feedback(struct pfr_addr *, int, int);
218 void pfr_prepare_network(union sockaddr_union *, int, int);
219 int pfr_route_kentry(struct pfr_ktable *,
220 struct pfr_kentry *);
221 int pfr_unroute_kentry(struct pfr_ktable *,
222 struct pfr_kentry *);
223 int pfr_walktree(struct radix_node *, void *);
224 int pfr_validate_table(struct pfr_table *, int, int);
225 int pfr_fix_anchor(char *);
226 void pfr_commit_ktable(struct pfr_ktable *, long);
227 void pfr_insert_ktables(struct pfr_ktableworkq *);
228 void pfr_insert_ktable(struct pfr_ktable *);
229 void pfr_setflags_ktables(struct pfr_ktableworkq *);
230 void pfr_setflags_ktable(struct pfr_ktable *, int);
231 void pfr_clstats_ktables(struct pfr_ktableworkq *, long,
233 void pfr_clstats_ktable(struct pfr_ktable *, long, int);
234 struct pfr_ktable *pfr_create_ktable(struct pfr_table *, long, int, int);
235 void pfr_destroy_ktables(struct pfr_ktableworkq *, int);
236 void pfr_destroy_ktable(struct pfr_ktable *, int);
237 int pfr_ktable_compare(struct pfr_ktable *,
238 struct pfr_ktable *);
239 struct pfr_ktable *pfr_lookup_table(struct pfr_table *);
240 void pfr_clean_node_mask(struct pfr_ktable *,
241 struct pfr_kentryworkq *);
242 int pfr_table_count(struct pfr_table *, int);
243 int pfr_skip_table(struct pfr_table *,
244 struct pfr_ktable *, int);
245 struct pfr_kentry *pfr_kentry_byidx(struct pfr_ktable *, int, int);
247 RB_PROTOTYPE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
248 RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
250 struct pfr_ktablehead pfr_ktables;
251 struct pfr_table pfr_nulltable;
258 pool_init(&pfr_ktable_pl, sizeof(struct pfr_ktable), 0, 0, 0,
260 pool_init(&pfr_kentry_pl, sizeof(struct pfr_kentry), 0, 0, 0,
262 pool_init(&pfr_kcounters_pl, sizeof(struct pfr_kcounters), 0, 0, 0,
263 "pfrkcounters", NULL);
265 pfr_sin.sin_len = sizeof(pfr_sin);
266 pfr_sin.sin_family = AF_INET;
267 pfr_sin6.sin6_len = sizeof(pfr_sin6);
268 pfr_sin6.sin6_family = AF_INET6;
270 memset(&pfr_ffaddr, 0xff, sizeof(pfr_ffaddr));
272 V_pfr_sin.sin_len = sizeof(V_pfr_sin);
273 V_pfr_sin.sin_family = AF_INET;
274 V_pfr_sin6.sin6_len = sizeof(V_pfr_sin6);
275 V_pfr_sin6.sin6_family = AF_INET6;
277 memset(&V_pfr_ffaddr, 0xff, sizeof(V_pfr_ffaddr));
282 pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
284 struct pfr_ktable *kt;
285 struct pfr_kentryworkq workq;
288 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
289 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
291 kt = pfr_lookup_table(tbl);
292 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
294 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
296 pfr_enqueue_addrs(kt, &workq, ndel, 0);
298 if (!(flags & PFR_FLAG_DUMMY)) {
299 if (flags & PFR_FLAG_ATOMIC)
301 pfr_remove_kentries(kt, &workq);
302 if (flags & PFR_FLAG_ATOMIC)
305 printf("pfr_clr_addrs: corruption detected (%d).\n",
314 pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
315 int *nadd, int flags)
317 struct pfr_ktable *kt, *tmpkt;
318 struct pfr_kentryworkq workq;
319 struct pfr_kentry *p, *q;
321 int i, rv, s, xadd = 0;
322 long tzero = time_second;
324 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
326 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
328 kt = pfr_lookup_table(tbl);
329 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
331 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
333 tmpkt = pfr_create_ktable(&pfr_nulltable, 0, 0,
334 !(flags & PFR_FLAG_USERIOCTL));
338 for (i = 0; i < size; i++) {
339 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
341 if (pfr_validate_addr(&ad))
343 p = pfr_lookup_addr(kt, &ad, 1);
344 q = pfr_lookup_addr(tmpkt, &ad, 1);
345 if (flags & PFR_FLAG_FEEDBACK) {
347 ad.pfra_fback = PFR_FB_DUPLICATE;
349 ad.pfra_fback = PFR_FB_ADDED;
350 else if (p->pfrke_not != ad.pfra_not)
351 ad.pfra_fback = PFR_FB_CONFLICT;
353 ad.pfra_fback = PFR_FB_NONE;
355 if (p == NULL && q == NULL) {
356 p = pfr_create_kentry(&ad,
357 !(flags & PFR_FLAG_USERIOCTL));
360 if (pfr_route_kentry(tmpkt, p)) {
361 pfr_destroy_kentry(p);
362 ad.pfra_fback = PFR_FB_NONE;
364 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
368 if (flags & PFR_FLAG_FEEDBACK)
369 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
372 pfr_clean_node_mask(tmpkt, &workq);
373 if (!(flags & PFR_FLAG_DUMMY)) {
374 if (flags & PFR_FLAG_ATOMIC)
376 pfr_insert_kentries(kt, &workq, tzero);
377 if (flags & PFR_FLAG_ATOMIC)
380 pfr_destroy_kentries(&workq);
383 pfr_destroy_ktable(tmpkt, 0);
386 pfr_clean_node_mask(tmpkt, &workq);
387 pfr_destroy_kentries(&workq);
388 if (flags & PFR_FLAG_FEEDBACK)
389 pfr_reset_feedback(addr, size, flags);
390 pfr_destroy_ktable(tmpkt, 0);
395 pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
396 int *ndel, int flags)
398 struct pfr_ktable *kt;
399 struct pfr_kentryworkq workq;
400 struct pfr_kentry *p;
402 int i, rv, s, xdel = 0, log = 1;
404 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
406 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
408 kt = pfr_lookup_table(tbl);
409 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
411 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
414 * there are two algorithms to choose from here.
416 * n: number of addresses to delete
417 * N: number of addresses in the table
419 * one is O(N) and is better for large 'n'
420 * one is O(n*LOG(N)) and is better for small 'n'
422 * following code try to decide which one is best.
424 for (i = kt->pfrkt_cnt; i > 0; i >>= 1)
426 if (size > kt->pfrkt_cnt/log) {
427 /* full table scan */
430 /* iterate over addresses to delete */
431 for (i = 0; i < size; i++) {
432 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
434 if (pfr_validate_addr(&ad))
436 p = pfr_lookup_addr(kt, &ad, 1);
442 for (i = 0; i < size; i++) {
443 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
445 if (pfr_validate_addr(&ad))
447 p = pfr_lookup_addr(kt, &ad, 1);
448 if (flags & PFR_FLAG_FEEDBACK) {
450 ad.pfra_fback = PFR_FB_NONE;
451 else if (p->pfrke_not != ad.pfra_not)
452 ad.pfra_fback = PFR_FB_CONFLICT;
453 else if (p->pfrke_mark)
454 ad.pfra_fback = PFR_FB_DUPLICATE;
456 ad.pfra_fback = PFR_FB_DELETED;
458 if (p != NULL && p->pfrke_not == ad.pfra_not &&
461 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
464 if (flags & PFR_FLAG_FEEDBACK)
465 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
468 if (!(flags & PFR_FLAG_DUMMY)) {
469 if (flags & PFR_FLAG_ATOMIC)
471 pfr_remove_kentries(kt, &workq);
472 if (flags & PFR_FLAG_ATOMIC)
479 if (flags & PFR_FLAG_FEEDBACK)
480 pfr_reset_feedback(addr, size, flags);
485 pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
486 int *size2, int *nadd, int *ndel, int *nchange, int flags,
487 u_int32_t ignore_pfrt_flags)
489 struct pfr_ktable *kt, *tmpkt;
490 struct pfr_kentryworkq addq, delq, changeq;
491 struct pfr_kentry *p, *q;
493 int i, rv, s, xadd = 0, xdel = 0, xchange = 0;
494 long tzero = time_second;
496 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
498 if (pfr_validate_table(tbl, ignore_pfrt_flags, flags &
501 kt = pfr_lookup_table(tbl);
502 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
504 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
506 tmpkt = pfr_create_ktable(&pfr_nulltable, 0, 0,
507 !(flags & PFR_FLAG_USERIOCTL));
513 SLIST_INIT(&changeq);
514 for (i = 0; i < size; i++) {
515 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
517 if (pfr_validate_addr(&ad))
519 ad.pfra_fback = PFR_FB_NONE;
520 p = pfr_lookup_addr(kt, &ad, 1);
523 ad.pfra_fback = PFR_FB_DUPLICATE;
527 if (p->pfrke_not != ad.pfra_not) {
528 SLIST_INSERT_HEAD(&changeq, p, pfrke_workq);
529 ad.pfra_fback = PFR_FB_CHANGED;
533 q = pfr_lookup_addr(tmpkt, &ad, 1);
535 ad.pfra_fback = PFR_FB_DUPLICATE;
538 p = pfr_create_kentry(&ad,
539 !(flags & PFR_FLAG_USERIOCTL));
542 if (pfr_route_kentry(tmpkt, p)) {
543 pfr_destroy_kentry(p);
544 ad.pfra_fback = PFR_FB_NONE;
546 SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
547 ad.pfra_fback = PFR_FB_ADDED;
552 if (flags & PFR_FLAG_FEEDBACK)
553 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
556 pfr_enqueue_addrs(kt, &delq, &xdel, ENQUEUE_UNMARKED_ONLY);
557 if ((flags & PFR_FLAG_FEEDBACK) && *size2) {
558 if (*size2 < size+xdel) {
563 SLIST_FOREACH(p, &delq, pfrke_workq) {
564 pfr_copyout_addr(&ad, p);
565 ad.pfra_fback = PFR_FB_DELETED;
566 if (COPYOUT(&ad, addr+size+i, sizeof(ad), flags))
571 pfr_clean_node_mask(tmpkt, &addq);
572 if (!(flags & PFR_FLAG_DUMMY)) {
573 if (flags & PFR_FLAG_ATOMIC)
575 pfr_insert_kentries(kt, &addq, tzero);
576 pfr_remove_kentries(kt, &delq);
577 pfr_clstats_kentries(&changeq, tzero, INVERT_NEG_FLAG);
578 if (flags & PFR_FLAG_ATOMIC)
581 pfr_destroy_kentries(&addq);
588 if ((flags & PFR_FLAG_FEEDBACK) && size2)
590 pfr_destroy_ktable(tmpkt, 0);
593 pfr_clean_node_mask(tmpkt, &addq);
594 pfr_destroy_kentries(&addq);
595 if (flags & PFR_FLAG_FEEDBACK)
596 pfr_reset_feedback(addr, size, flags);
597 pfr_destroy_ktable(tmpkt, 0);
602 pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
603 int *nmatch, int flags)
605 struct pfr_ktable *kt;
606 struct pfr_kentry *p;
610 ACCEPT_FLAGS(flags, PFR_FLAG_REPLACE);
611 if (pfr_validate_table(tbl, 0, 0))
613 kt = pfr_lookup_table(tbl);
614 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
617 for (i = 0; i < size; i++) {
618 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
620 if (pfr_validate_addr(&ad))
622 if (ADDR_NETWORK(&ad))
624 p = pfr_lookup_addr(kt, &ad, 0);
625 if (flags & PFR_FLAG_REPLACE)
626 pfr_copyout_addr(&ad, p);
627 ad.pfra_fback = (p == NULL) ? PFR_FB_NONE :
628 (p->pfrke_not ? PFR_FB_NOTMATCH : PFR_FB_MATCH);
629 if (p != NULL && !p->pfrke_not)
631 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
640 pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
643 struct pfr_ktable *kt;
644 struct pfr_walktree w;
647 ACCEPT_FLAGS(flags, 0);
648 if (pfr_validate_table(tbl, 0, 0))
650 kt = pfr_lookup_table(tbl);
651 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
653 if (kt->pfrkt_cnt > *size) {
654 *size = kt->pfrkt_cnt;
658 bzero(&w, sizeof(w));
659 w.pfrw_op = PFRW_GET_ADDRS;
661 w.pfrw_free = kt->pfrkt_cnt;
662 w.pfrw_flags = flags;
664 rv = kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
666 rv = rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
670 rv = kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree,
673 rv = rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w);
679 printf("pfr_get_addrs: corruption detected (%d).\n",
683 *size = kt->pfrkt_cnt;
688 pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
691 struct pfr_ktable *kt;
692 struct pfr_walktree w;
693 struct pfr_kentryworkq workq;
695 long tzero = time_second;
697 /* XXX PFR_FLAG_CLSTATS disabled */
698 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC);
699 if (pfr_validate_table(tbl, 0, 0))
701 kt = pfr_lookup_table(tbl);
702 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
704 if (kt->pfrkt_cnt > *size) {
705 *size = kt->pfrkt_cnt;
709 bzero(&w, sizeof(w));
710 w.pfrw_op = PFRW_GET_ASTATS;
711 w.pfrw_astats = addr;
712 w.pfrw_free = kt->pfrkt_cnt;
713 w.pfrw_flags = flags;
714 if (flags & PFR_FLAG_ATOMIC)
717 rv = kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
719 rv = rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
723 rv = kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree,
726 rv = rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w);
728 if (!rv && (flags & PFR_FLAG_CLSTATS)) {
729 pfr_enqueue_addrs(kt, &workq, NULL, 0);
730 pfr_clstats_kentries(&workq, tzero, 0);
732 if (flags & PFR_FLAG_ATOMIC)
738 printf("pfr_get_astats: corruption detected (%d).\n",
742 *size = kt->pfrkt_cnt;
747 pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
748 int *nzero, int flags)
750 struct pfr_ktable *kt;
751 struct pfr_kentryworkq workq;
752 struct pfr_kentry *p;
754 int i, rv, s, xzero = 0;
756 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
758 if (pfr_validate_table(tbl, 0, 0))
760 kt = pfr_lookup_table(tbl);
761 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
764 for (i = 0; i < size; i++) {
765 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
767 if (pfr_validate_addr(&ad))
769 p = pfr_lookup_addr(kt, &ad, 1);
770 if (flags & PFR_FLAG_FEEDBACK) {
771 ad.pfra_fback = (p != NULL) ?
772 PFR_FB_CLEARED : PFR_FB_NONE;
773 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
777 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
782 if (!(flags & PFR_FLAG_DUMMY)) {
783 if (flags & PFR_FLAG_ATOMIC)
785 pfr_clstats_kentries(&workq, 0, 0);
786 if (flags & PFR_FLAG_ATOMIC)
793 if (flags & PFR_FLAG_FEEDBACK)
794 pfr_reset_feedback(addr, size, flags);
799 pfr_validate_addr(struct pfr_addr *ad)
803 switch (ad->pfra_af) {
806 if (ad->pfra_net > 32)
812 if (ad->pfra_net > 128)
819 if (ad->pfra_net < 128 &&
820 (((caddr_t)ad)[ad->pfra_net/8] & (0xFF >> (ad->pfra_net%8))))
822 for (i = (ad->pfra_net+7)/8; i < sizeof(ad->pfra_u); i++)
823 if (((caddr_t)ad)[i])
825 if (ad->pfra_not && ad->pfra_not != 1)
833 pfr_enqueue_addrs(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
834 int *naddr, int sweep)
836 struct pfr_walktree w;
839 bzero(&w, sizeof(w));
840 w.pfrw_op = sweep ? PFRW_SWEEP : PFRW_ENQUEUE;
841 w.pfrw_workq = workq;
842 if (kt->pfrkt_ip4 != NULL)
844 if (kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree,
847 if (rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w))
849 printf("pfr_enqueue_addrs: IPv4 walktree failed.\n");
850 if (kt->pfrkt_ip6 != NULL)
852 if (kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree,
855 if (rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w))
857 printf("pfr_enqueue_addrs: IPv6 walktree failed.\n");
863 pfr_mark_addrs(struct pfr_ktable *kt)
865 struct pfr_walktree w;
867 bzero(&w, sizeof(w));
868 w.pfrw_op = PFRW_MARK;
870 if (kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w))
872 if (rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w))
874 printf("pfr_mark_addrs: IPv4 walktree failed.\n");
876 if (kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree, &w))
878 if (rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w))
880 printf("pfr_mark_addrs: IPv6 walktree failed.\n");
885 pfr_lookup_addr(struct pfr_ktable *kt, struct pfr_addr *ad, int exact)
887 union sockaddr_union sa, mask;
889 struct radix_node_head *head = NULL;
891 struct radix_node_head *head;
893 struct pfr_kentry *ke;
896 bzero(&sa, sizeof(sa));
897 if (ad->pfra_af == AF_INET) {
898 FILLIN_SIN(sa.sin, ad->pfra_ip4addr);
899 head = kt->pfrkt_ip4;
900 } else if ( ad->pfra_af == AF_INET6 ) {
901 FILLIN_SIN6(sa.sin6, ad->pfra_ip6addr);
902 head = kt->pfrkt_ip6;
904 if (ADDR_NETWORK(ad)) {
905 pfr_prepare_network(&mask, ad->pfra_af, ad->pfra_net);
906 s = splsoftnet(); /* rn_lookup makes use of globals */
910 ke = (struct pfr_kentry *)rn_lookup(&sa, &mask, head);
912 if (ke && KENTRY_RNF_ROOT(ke))
915 ke = (struct pfr_kentry *)rn_match(&sa, head);
916 if (ke && KENTRY_RNF_ROOT(ke))
918 if (exact && ke && KENTRY_NETWORK(ke))
925 pfr_create_kentry(struct pfr_addr *ad, int intr)
927 struct pfr_kentry *ke;
930 ke = pool_get(&V_pfr_kentry_pl, PR_NOWAIT | PR_ZERO);
933 ke = pool_get(&pfr_kentry_pl, PR_NOWAIT | PR_ZERO);
935 ke = pool_get(&pfr_kentry_pl, PR_WAITOK|PR_ZERO|PR_LIMITFAIL);
940 if (ad->pfra_af == AF_INET)
941 FILLIN_SIN(ke->pfrke_sa.sin, ad->pfra_ip4addr);
942 else if (ad->pfra_af == AF_INET6)
943 FILLIN_SIN6(ke->pfrke_sa.sin6, ad->pfra_ip6addr);
944 ke->pfrke_af = ad->pfra_af;
945 ke->pfrke_net = ad->pfra_net;
946 ke->pfrke_not = ad->pfra_not;
951 pfr_destroy_kentries(struct pfr_kentryworkq *workq)
953 struct pfr_kentry *p, *q;
955 for (p = SLIST_FIRST(workq); p != NULL; p = q) {
956 q = SLIST_NEXT(p, pfrke_workq);
957 pfr_destroy_kentry(p);
962 pfr_destroy_kentry(struct pfr_kentry *ke)
964 if (ke->pfrke_counters)
966 pool_put(&V_pfr_kcounters_pl, ke->pfrke_counters);
967 pool_put(&V_pfr_kentry_pl, ke);
969 pool_put(&pfr_kcounters_pl, ke->pfrke_counters);
970 pool_put(&pfr_kentry_pl, ke);
975 pfr_insert_kentries(struct pfr_ktable *kt,
976 struct pfr_kentryworkq *workq, long tzero)
978 struct pfr_kentry *p;
981 SLIST_FOREACH(p, workq, pfrke_workq) {
982 rv = pfr_route_kentry(kt, p);
984 printf("pfr_insert_kentries: cannot route entry "
988 p->pfrke_tzero = tzero;
995 pfr_insert_kentry(struct pfr_ktable *kt, struct pfr_addr *ad, long tzero)
997 struct pfr_kentry *p;
1000 p = pfr_lookup_addr(kt, ad, 1);
1003 p = pfr_create_kentry(ad, 1);
1007 rv = pfr_route_kentry(kt, p);
1011 p->pfrke_tzero = tzero;
1018 pfr_remove_kentries(struct pfr_ktable *kt,
1019 struct pfr_kentryworkq *workq)
1021 struct pfr_kentry *p;
1024 SLIST_FOREACH(p, workq, pfrke_workq) {
1025 pfr_unroute_kentry(kt, p);
1029 pfr_destroy_kentries(workq);
1033 pfr_clean_node_mask(struct pfr_ktable *kt,
1034 struct pfr_kentryworkq *workq)
1036 struct pfr_kentry *p;
1038 SLIST_FOREACH(p, workq, pfrke_workq)
1039 pfr_unroute_kentry(kt, p);
1043 pfr_clstats_kentries(struct pfr_kentryworkq *workq, long tzero, int negchange)
1045 struct pfr_kentry *p;
1048 SLIST_FOREACH(p, workq, pfrke_workq) {
1051 p->pfrke_not = !p->pfrke_not;
1052 if (p->pfrke_counters) {
1054 pool_put(&V_pfr_kcounters_pl, p->pfrke_counters);
1056 pool_put(&pfr_kcounters_pl, p->pfrke_counters);
1058 p->pfrke_counters = NULL;
1061 p->pfrke_tzero = tzero;
1066 pfr_reset_feedback(struct pfr_addr *addr, int size, int flags)
1071 for (i = 0; i < size; i++) {
1072 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
1074 ad.pfra_fback = PFR_FB_NONE;
1075 if (COPYOUT(&ad, addr+i, sizeof(ad), flags))
1081 pfr_prepare_network(union sockaddr_union *sa, int af, int net)
1085 bzero(sa, sizeof(*sa));
1086 if (af == AF_INET) {
1087 sa->sin.sin_len = sizeof(sa->sin);
1088 sa->sin.sin_family = AF_INET;
1089 sa->sin.sin_addr.s_addr = net ? htonl(-1 << (32-net)) : 0;
1090 } else if (af == AF_INET6) {
1091 sa->sin6.sin6_len = sizeof(sa->sin6);
1092 sa->sin6.sin6_family = AF_INET6;
1093 for (i = 0; i < 4; i++) {
1095 sa->sin6.sin6_addr.s6_addr32[i] =
1096 net ? htonl(-1 << (32-net)) : 0;
1099 sa->sin6.sin6_addr.s6_addr32[i] = 0xFFFFFFFF;
1106 pfr_route_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
1108 union sockaddr_union mask;
1109 struct radix_node *rn;
1111 struct radix_node_head *head = NULL;
1113 struct radix_node_head *head;
1117 bzero(ke->pfrke_node, sizeof(ke->pfrke_node));
1118 if (ke->pfrke_af == AF_INET)
1119 head = kt->pfrkt_ip4;
1120 else if (ke->pfrke_af == AF_INET6)
1121 head = kt->pfrkt_ip6;
1127 if (KENTRY_NETWORK(ke)) {
1128 pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
1130 rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node);
1132 rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node, 0);
1136 rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node);
1138 rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node, 0);
1142 return (rn == NULL ? -1 : 0);
1146 pfr_unroute_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
1148 union sockaddr_union mask;
1149 struct radix_node *rn;
1151 struct radix_node_head *head = NULL;
1153 struct radix_node_head *head;
1157 if (ke->pfrke_af == AF_INET)
1158 head = kt->pfrkt_ip4;
1159 else if (ke->pfrke_af == AF_INET6)
1160 head = kt->pfrkt_ip6;
1166 if (KENTRY_NETWORK(ke)) {
1167 pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
1169 rn = rn_delete(&ke->pfrke_sa, &mask, head);
1171 rn = rn_delete(&ke->pfrke_sa, &mask, head, NULL);
1175 rn = rn_delete(&ke->pfrke_sa, NULL, head);
1177 rn = rn_delete(&ke->pfrke_sa, NULL, head, NULL);
1182 printf("pfr_unroute_kentry: delete failed.\n");
1189 pfr_copyout_addr(struct pfr_addr *ad, struct pfr_kentry *ke)
1191 bzero(ad, sizeof(*ad));
1194 ad->pfra_af = ke->pfrke_af;
1195 ad->pfra_net = ke->pfrke_net;
1196 ad->pfra_not = ke->pfrke_not;
1197 if (ad->pfra_af == AF_INET)
1198 ad->pfra_ip4addr = ke->pfrke_sa.sin.sin_addr;
1199 else if (ad->pfra_af == AF_INET6)
1200 ad->pfra_ip6addr = ke->pfrke_sa.sin6.sin6_addr;
1204 pfr_walktree(struct radix_node *rn, void *arg)
1206 struct pfr_kentry *ke = (struct pfr_kentry *)rn;
1207 struct pfr_walktree *w = arg;
1208 int s, flags = w->pfrw_flags;
1210 switch (w->pfrw_op) {
1219 SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq);
1222 case PFRW_GET_ADDRS:
1223 if (w->pfrw_free-- > 0) {
1226 pfr_copyout_addr(&ad, ke);
1227 if (copyout(&ad, w->pfrw_addr, sizeof(ad)))
1232 case PFRW_GET_ASTATS:
1233 if (w->pfrw_free-- > 0) {
1234 struct pfr_astats as;
1236 pfr_copyout_addr(&as.pfras_a, ke);
1239 if (ke->pfrke_counters) {
1240 bcopy(ke->pfrke_counters->pfrkc_packets,
1241 as.pfras_packets, sizeof(as.pfras_packets));
1242 bcopy(ke->pfrke_counters->pfrkc_bytes,
1243 as.pfras_bytes, sizeof(as.pfras_bytes));
1245 bzero(as.pfras_packets, sizeof(as.pfras_packets));
1246 bzero(as.pfras_bytes, sizeof(as.pfras_bytes));
1247 as.pfras_a.pfra_fback = PFR_FB_NOCOUNT;
1250 as.pfras_tzero = ke->pfrke_tzero;
1252 if (COPYOUT(&as, w->pfrw_astats, sizeof(as), flags))
1259 break; /* negative entries are ignored */
1260 if (!w->pfrw_cnt--) {
1261 w->pfrw_kentry = ke;
1262 return (1); /* finish search */
1265 case PFRW_DYNADDR_UPDATE:
1266 if (ke->pfrke_af == AF_INET) {
1267 if (w->pfrw_dyn->pfid_acnt4++ > 0)
1270 pfr_prepare_network(&V_pfr_mask, AF_INET, ke->pfrke_net);
1272 pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net);
1274 w->pfrw_dyn->pfid_addr4 = *SUNION2PF(
1275 &ke->pfrke_sa, AF_INET);
1276 w->pfrw_dyn->pfid_mask4 = *SUNION2PF(
1278 &V_pfr_mask, AF_INET);
1280 &pfr_mask, AF_INET);
1282 } else if (ke->pfrke_af == AF_INET6){
1283 if (w->pfrw_dyn->pfid_acnt6++ > 0)
1286 pfr_prepare_network(&V_pfr_mask, AF_INET6, ke->pfrke_net);
1288 pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net);
1290 w->pfrw_dyn->pfid_addr6 = *SUNION2PF(
1291 &ke->pfrke_sa, AF_INET6);
1292 w->pfrw_dyn->pfid_mask6 = *SUNION2PF(
1294 &V_pfr_mask, AF_INET6);
1296 &pfr_mask, AF_INET6);
1305 pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
1307 struct pfr_ktableworkq workq;
1308 struct pfr_ktable *p;
1311 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
1313 if (pfr_fix_anchor(filter->pfrt_anchor))
1315 if (pfr_table_count(filter, flags) < 0)
1319 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1320 if (pfr_skip_table(filter, p, flags))
1322 if (!strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR))
1324 if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE))
1326 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
1327 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1330 if (!(flags & PFR_FLAG_DUMMY)) {
1331 if (flags & PFR_FLAG_ATOMIC)
1333 pfr_setflags_ktables(&workq);
1334 if (flags & PFR_FLAG_ATOMIC)
1343 pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
1345 struct pfr_ktableworkq addq, changeq;
1346 struct pfr_ktable *p, *q, *r, key;
1347 int i, rv, s, xadd = 0;
1348 long tzero = time_second;
1350 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1352 SLIST_INIT(&changeq);
1353 for (i = 0; i < size; i++) {
1354 if (COPYIN(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t), flags))
1356 if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK,
1357 flags & PFR_FLAG_USERIOCTL))
1359 key.pfrkt_flags |= PFR_TFLAG_ACTIVE;
1360 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1362 p = pfr_create_ktable(&key.pfrkt_t, tzero, 1,
1363 !(flags & PFR_FLAG_USERIOCTL));
1366 SLIST_FOREACH(q, &addq, pfrkt_workq) {
1367 if (!pfr_ktable_compare(p, q))
1370 SLIST_INSERT_HEAD(&addq, p, pfrkt_workq);
1372 if (!key.pfrkt_anchor[0])
1375 /* find or create root table */
1376 bzero(key.pfrkt_anchor, sizeof(key.pfrkt_anchor));
1377 r = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1382 SLIST_FOREACH(q, &addq, pfrkt_workq) {
1383 if (!pfr_ktable_compare(&key, q)) {
1388 key.pfrkt_flags = 0;
1389 r = pfr_create_ktable(&key.pfrkt_t, 0, 1,
1390 !(flags & PFR_FLAG_USERIOCTL));
1393 SLIST_INSERT_HEAD(&addq, r, pfrkt_workq);
1395 } else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1396 SLIST_FOREACH(q, &changeq, pfrkt_workq)
1397 if (!pfr_ktable_compare(&key, q))
1399 p->pfrkt_nflags = (p->pfrkt_flags &
1400 ~PFR_TFLAG_USRMASK) | key.pfrkt_flags;
1401 SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq);
1407 if (!(flags & PFR_FLAG_DUMMY)) {
1408 if (flags & PFR_FLAG_ATOMIC)
1410 pfr_insert_ktables(&addq);
1411 pfr_setflags_ktables(&changeq);
1412 if (flags & PFR_FLAG_ATOMIC)
1415 pfr_destroy_ktables(&addq, 0);
1420 pfr_destroy_ktables(&addq, 0);
1425 pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags)
1427 struct pfr_ktableworkq workq;
1428 struct pfr_ktable *p, *q, key;
1431 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1433 for (i = 0; i < size; i++) {
1434 if (COPYIN(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t), flags))
1436 if (pfr_validate_table(&key.pfrkt_t, 0,
1437 flags & PFR_FLAG_USERIOCTL))
1439 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1440 if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1441 SLIST_FOREACH(q, &workq, pfrkt_workq)
1442 if (!pfr_ktable_compare(p, q))
1444 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
1445 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1452 if (!(flags & PFR_FLAG_DUMMY)) {
1453 if (flags & PFR_FLAG_ATOMIC)
1455 pfr_setflags_ktables(&workq);
1456 if (flags & PFR_FLAG_ATOMIC)
1465 pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
1468 struct pfr_ktable *p;
1471 ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
1472 if (pfr_fix_anchor(filter->pfrt_anchor))
1474 n = nn = pfr_table_count(filter, flags);
1481 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1482 if (pfr_skip_table(filter, p, flags))
1486 if (COPYOUT(&p->pfrkt_t, tbl++, sizeof(*tbl), flags))
1490 printf("pfr_get_tables: corruption detected (%d).\n", n);
1498 pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size,
1501 struct pfr_ktable *p;
1502 struct pfr_ktableworkq workq;
1504 long tzero = time_second;
1506 /* XXX PFR_FLAG_CLSTATS disabled */
1507 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_ALLRSETS);
1508 if (pfr_fix_anchor(filter->pfrt_anchor))
1510 n = nn = pfr_table_count(filter, flags);
1518 if (flags & PFR_FLAG_ATOMIC)
1520 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1521 if (pfr_skip_table(filter, p, flags))
1525 if (!(flags & PFR_FLAG_ATOMIC))
1527 if (COPYOUT(&p->pfrkt_ts, tbl++, sizeof(*tbl), flags)) {
1531 if (!(flags & PFR_FLAG_ATOMIC))
1533 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1535 if (flags & PFR_FLAG_CLSTATS)
1536 pfr_clstats_ktables(&workq, tzero,
1537 flags & PFR_FLAG_ADDRSTOO);
1538 if (flags & PFR_FLAG_ATOMIC)
1541 printf("pfr_get_tstats: corruption detected (%d).\n", n);
1549 pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
1551 struct pfr_ktableworkq workq;
1552 struct pfr_ktable *p, key;
1553 int i, s, xzero = 0;
1554 long tzero = time_second;
1556 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
1559 for (i = 0; i < size; i++) {
1560 if (COPYIN(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t), flags))
1562 if (pfr_validate_table(&key.pfrkt_t, 0, 0))
1564 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1566 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1570 if (!(flags & PFR_FLAG_DUMMY)) {
1571 if (flags & PFR_FLAG_ATOMIC)
1573 pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO);
1574 if (flags & PFR_FLAG_ATOMIC)
1583 pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag,
1584 int *nchange, int *ndel, int flags)
1586 struct pfr_ktableworkq workq;
1587 struct pfr_ktable *p, *q, key;
1588 int i, s, xchange = 0, xdel = 0;
1590 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1591 if ((setflag & ~PFR_TFLAG_USRMASK) ||
1592 (clrflag & ~PFR_TFLAG_USRMASK) ||
1593 (setflag & clrflag))
1596 for (i = 0; i < size; i++) {
1597 if (COPYIN(tbl+i, &key.pfrkt_t, sizeof(key.pfrkt_t), flags))
1599 if (pfr_validate_table(&key.pfrkt_t, 0,
1600 flags & PFR_FLAG_USERIOCTL))
1602 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1603 if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1604 p->pfrkt_nflags = (p->pfrkt_flags | setflag) &
1606 if (p->pfrkt_nflags == p->pfrkt_flags)
1608 SLIST_FOREACH(q, &workq, pfrkt_workq)
1609 if (!pfr_ktable_compare(p, q))
1611 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1612 if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) &&
1613 (clrflag & PFR_TFLAG_PERSIST) &&
1614 !(p->pfrkt_flags & PFR_TFLAG_REFERENCED))
1622 if (!(flags & PFR_FLAG_DUMMY)) {
1623 if (flags & PFR_FLAG_ATOMIC)
1625 pfr_setflags_ktables(&workq);
1626 if (flags & PFR_FLAG_ATOMIC)
1629 if (nchange != NULL)
1637 pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags)
1639 struct pfr_ktableworkq workq;
1640 struct pfr_ktable *p;
1641 struct pf_ruleset *rs;
1644 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1645 rs = pf_find_or_create_ruleset(trs->pfrt_anchor);
1649 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1650 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1651 pfr_skip_table(trs, p, 0))
1653 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
1654 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1657 if (!(flags & PFR_FLAG_DUMMY)) {
1658 pfr_setflags_ktables(&workq);
1660 *ticket = ++rs->tticket;
1663 pf_remove_if_empty_ruleset(rs);
1670 pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
1671 int *nadd, int *naddr, u_int32_t ticket, int flags)
1673 struct pfr_ktableworkq tableq;
1674 struct pfr_kentryworkq addrq;
1675 struct pfr_ktable *kt, *rt, *shadow, key;
1676 struct pfr_kentry *p;
1678 struct pf_ruleset *rs;
1679 int i, rv, xadd = 0, xaddr = 0;
1681 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
1682 if (size && !(flags & PFR_FLAG_ADDRSTOO))
1684 if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK,
1685 flags & PFR_FLAG_USERIOCTL))
1687 rs = pf_find_ruleset(tbl->pfrt_anchor);
1688 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1690 tbl->pfrt_flags |= PFR_TFLAG_INACTIVE;
1691 SLIST_INIT(&tableq);
1692 kt = RB_FIND(pfr_ktablehead, &pfr_ktables, (struct pfr_ktable *)tbl);
1694 kt = pfr_create_ktable(tbl, 0, 1,
1695 !(flags & PFR_FLAG_USERIOCTL));
1698 SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq);
1700 if (!tbl->pfrt_anchor[0])
1703 /* find or create root table */
1704 bzero(&key, sizeof(key));
1705 strlcpy(key.pfrkt_name, tbl->pfrt_name, sizeof(key.pfrkt_name));
1706 rt = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1708 kt->pfrkt_root = rt;
1711 rt = pfr_create_ktable(&key.pfrkt_t, 0, 1,
1712 !(flags & PFR_FLAG_USERIOCTL));
1714 pfr_destroy_ktables(&tableq, 0);
1717 SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq);
1718 kt->pfrkt_root = rt;
1719 } else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE))
1722 shadow = pfr_create_ktable(tbl, 0, 0, !(flags & PFR_FLAG_USERIOCTL));
1723 if (shadow == NULL) {
1724 pfr_destroy_ktables(&tableq, 0);
1728 for (i = 0; i < size; i++) {
1729 if (COPYIN(addr+i, &ad, sizeof(ad), flags))
1731 if (pfr_validate_addr(&ad))
1733 if (pfr_lookup_addr(shadow, &ad, 1) != NULL)
1735 p = pfr_create_kentry(&ad, 0);
1738 if (pfr_route_kentry(shadow, p)) {
1739 pfr_destroy_kentry(p);
1742 SLIST_INSERT_HEAD(&addrq, p, pfrke_workq);
1745 if (!(flags & PFR_FLAG_DUMMY)) {
1746 if (kt->pfrkt_shadow != NULL)
1747 pfr_destroy_ktable(kt->pfrkt_shadow, 1);
1748 kt->pfrkt_flags |= PFR_TFLAG_INACTIVE;
1749 pfr_insert_ktables(&tableq);
1750 shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ?
1751 xaddr : NO_ADDRESSES;
1752 kt->pfrkt_shadow = shadow;
1754 pfr_clean_node_mask(shadow, &addrq);
1755 pfr_destroy_ktable(shadow, 0);
1756 pfr_destroy_ktables(&tableq, 0);
1757 pfr_destroy_kentries(&addrq);
1765 pfr_destroy_ktable(shadow, 0);
1766 pfr_destroy_ktables(&tableq, 0);
1767 pfr_destroy_kentries(&addrq);
1772 pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags)
1774 struct pfr_ktableworkq workq;
1775 struct pfr_ktable *p;
1776 struct pf_ruleset *rs;
1779 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1780 rs = pf_find_ruleset(trs->pfrt_anchor);
1781 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1784 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1785 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1786 pfr_skip_table(trs, p, 0))
1788 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
1789 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1792 if (!(flags & PFR_FLAG_DUMMY)) {
1793 pfr_setflags_ktables(&workq);
1795 pf_remove_if_empty_ruleset(rs);
1803 pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd,
1804 int *nchange, int flags)
1806 struct pfr_ktable *p, *q;
1807 struct pfr_ktableworkq workq;
1808 struct pf_ruleset *rs;
1809 int s, xadd = 0, xchange = 0;
1810 long tzero = time_second;
1812 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1813 rs = pf_find_ruleset(trs->pfrt_anchor);
1814 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1818 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1819 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1820 pfr_skip_table(trs, p, 0))
1822 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1823 if (p->pfrkt_flags & PFR_TFLAG_ACTIVE)
1829 if (!(flags & PFR_FLAG_DUMMY)) {
1830 if (flags & PFR_FLAG_ATOMIC)
1832 for (p = SLIST_FIRST(&workq); p != NULL; p = q) {
1833 q = SLIST_NEXT(p, pfrkt_workq);
1834 pfr_commit_ktable(p, tzero);
1836 if (flags & PFR_FLAG_ATOMIC)
1839 pf_remove_if_empty_ruleset(rs);
1843 if (nchange != NULL)
1850 pfr_commit_ktable(struct pfr_ktable *kt, long tzero)
1852 struct pfr_ktable *shadow = kt->pfrkt_shadow;
1855 if (shadow->pfrkt_cnt == NO_ADDRESSES) {
1856 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
1857 pfr_clstats_ktable(kt, tzero, 1);
1858 } else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) {
1859 /* kt might contain addresses */
1860 struct pfr_kentryworkq addrq, addq, changeq, delq, garbageq;
1861 struct pfr_kentry *p, *q, *next;
1864 pfr_enqueue_addrs(shadow, &addrq, NULL, 0);
1867 SLIST_INIT(&changeq);
1869 SLIST_INIT(&garbageq);
1870 pfr_clean_node_mask(shadow, &addrq);
1871 for (p = SLIST_FIRST(&addrq); p != NULL; p = next) {
1872 next = SLIST_NEXT(p, pfrke_workq); /* XXX */
1873 pfr_copyout_addr(&ad, p);
1874 q = pfr_lookup_addr(kt, &ad, 1);
1876 if (q->pfrke_not != p->pfrke_not)
1877 SLIST_INSERT_HEAD(&changeq, q,
1880 SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq);
1882 p->pfrke_tzero = tzero;
1883 SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
1886 pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY);
1887 pfr_insert_kentries(kt, &addq, tzero);
1888 pfr_remove_kentries(kt, &delq);
1889 pfr_clstats_kentries(&changeq, tzero, INVERT_NEG_FLAG);
1890 pfr_destroy_kentries(&garbageq);
1892 /* kt cannot contain addresses */
1893 SWAP(struct radix_node_head *, kt->pfrkt_ip4,
1895 SWAP(struct radix_node_head *, kt->pfrkt_ip6,
1897 SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt);
1898 pfr_clstats_ktable(kt, tzero, 1);
1900 nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) |
1901 (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE)
1902 & ~PFR_TFLAG_INACTIVE;
1903 pfr_destroy_ktable(shadow, 0);
1904 kt->pfrkt_shadow = NULL;
1905 pfr_setflags_ktable(kt, nflags);
1909 pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved)
1913 if (!tbl->pfrt_name[0])
1915 if (no_reserved && !strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR))
1917 if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1])
1919 for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++)
1920 if (tbl->pfrt_name[i])
1922 if (pfr_fix_anchor(tbl->pfrt_anchor))
1924 if (tbl->pfrt_flags & ~allowedflags)
1930 * Rewrite anchors referenced by tables to remove slashes
1931 * and check for validity.
1934 pfr_fix_anchor(char *anchor)
1936 size_t siz = MAXPATHLEN;
1939 if (anchor[0] == '/') {
1945 while (*++path == '/')
1947 bcopy(path, anchor, siz - off);
1948 memset(anchor + siz - off, 0, off);
1950 if (anchor[siz - 1])
1952 for (i = strlen(anchor); i < siz; i++)
1959 pfr_table_count(struct pfr_table *filter, int flags)
1961 struct pf_ruleset *rs;
1963 if (flags & PFR_FLAG_ALLRSETS)
1964 return (pfr_ktable_cnt);
1965 if (filter->pfrt_anchor[0]) {
1966 rs = pf_find_ruleset(filter->pfrt_anchor);
1967 return ((rs != NULL) ? rs->tables : -1);
1969 return (pf_main_ruleset.tables);
1973 pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags)
1975 if (flags & PFR_FLAG_ALLRSETS)
1977 if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor))
1983 pfr_insert_ktables(struct pfr_ktableworkq *workq)
1985 struct pfr_ktable *p;
1987 SLIST_FOREACH(p, workq, pfrkt_workq)
1988 pfr_insert_ktable(p);
1992 pfr_insert_ktable(struct pfr_ktable *kt)
1994 RB_INSERT(pfr_ktablehead, &pfr_ktables, kt);
1996 if (kt->pfrkt_root != NULL)
1997 if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++)
1998 pfr_setflags_ktable(kt->pfrkt_root,
1999 kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR);
2003 pfr_setflags_ktables(struct pfr_ktableworkq *workq)
2005 struct pfr_ktable *p, *q;
2007 for (p = SLIST_FIRST(workq); p; p = q) {
2008 q = SLIST_NEXT(p, pfrkt_workq);
2009 pfr_setflags_ktable(p, p->pfrkt_nflags);
2014 pfr_setflags_ktable(struct pfr_ktable *kt, int newf)
2016 struct pfr_kentryworkq addrq;
2018 if (!(newf & PFR_TFLAG_REFERENCED) &&
2019 !(newf & PFR_TFLAG_PERSIST))
2020 newf &= ~PFR_TFLAG_ACTIVE;
2021 if (!(newf & PFR_TFLAG_ACTIVE))
2022 newf &= ~PFR_TFLAG_USRMASK;
2023 if (!(newf & PFR_TFLAG_SETMASK)) {
2024 RB_REMOVE(pfr_ktablehead, &pfr_ktables, kt);
2025 if (kt->pfrkt_root != NULL)
2026 if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR])
2027 pfr_setflags_ktable(kt->pfrkt_root,
2028 kt->pfrkt_root->pfrkt_flags &
2029 ~PFR_TFLAG_REFDANCHOR);
2030 pfr_destroy_ktable(kt, 1);
2034 if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) {
2035 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
2036 pfr_remove_kentries(kt, &addrq);
2038 if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) {
2039 pfr_destroy_ktable(kt->pfrkt_shadow, 1);
2040 kt->pfrkt_shadow = NULL;
2042 kt->pfrkt_flags = newf;
2046 pfr_clstats_ktables(struct pfr_ktableworkq *workq, long tzero, int recurse)
2048 struct pfr_ktable *p;
2050 SLIST_FOREACH(p, workq, pfrkt_workq)
2051 pfr_clstats_ktable(p, tzero, recurse);
2055 pfr_clstats_ktable(struct pfr_ktable *kt, long tzero, int recurse)
2057 struct pfr_kentryworkq addrq;
2061 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
2062 pfr_clstats_kentries(&addrq, tzero, 0);
2065 bzero(kt->pfrkt_packets, sizeof(kt->pfrkt_packets));
2066 bzero(kt->pfrkt_bytes, sizeof(kt->pfrkt_bytes));
2067 kt->pfrkt_match = kt->pfrkt_nomatch = 0;
2069 kt->pfrkt_tzero = tzero;
2073 pfr_create_ktable(struct pfr_table *tbl, long tzero, int attachruleset,
2076 struct pfr_ktable *kt;
2077 struct pf_ruleset *rs;
2080 kt = pool_get(&V_pfr_ktable_pl, PR_NOWAIT|PR_ZERO);
2083 kt = pool_get(&pfr_ktable_pl, PR_NOWAIT|PR_ZERO|PR_LIMITFAIL);
2085 kt = pool_get(&pfr_ktable_pl, PR_WAITOK|PR_ZERO|PR_LIMITFAIL);
2091 if (attachruleset) {
2092 rs = pf_find_or_create_ruleset(tbl->pfrt_anchor);
2094 pfr_destroy_ktable(kt, 0);
2101 if (!rn_inithead((void **)&kt->pfrkt_ip4,
2102 offsetof(struct sockaddr_in, sin_addr) * 8) ||
2103 !rn_inithead((void **)&kt->pfrkt_ip6,
2104 offsetof(struct sockaddr_in6, sin6_addr) * 8)) {
2105 pfr_destroy_ktable(kt, 0);
2108 kt->pfrkt_tzero = tzero;
2114 pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr)
2116 struct pfr_ktable *p, *q;
2118 for (p = SLIST_FIRST(workq); p; p = q) {
2119 q = SLIST_NEXT(p, pfrkt_workq);
2120 pfr_destroy_ktable(p, flushaddr);
2125 pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr)
2127 struct pfr_kentryworkq addrq;
2130 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
2131 pfr_clean_node_mask(kt, &addrq);
2132 pfr_destroy_kentries(&addrq);
2134 #if defined(__FreeBSD__) && (__FreeBSD_version >= 500100)
2135 if (kt->pfrkt_ip4 != NULL) {
2136 RADIX_NODE_HEAD_DESTROY(kt->pfrkt_ip4);
2137 free((caddr_t)kt->pfrkt_ip4, M_RTABLE);
2139 if (kt->pfrkt_ip6 != NULL) {
2140 RADIX_NODE_HEAD_DESTROY(kt->pfrkt_ip6);
2141 free((caddr_t)kt->pfrkt_ip6, M_RTABLE);
2144 if (kt->pfrkt_ip4 != NULL)
2145 free((caddr_t)kt->pfrkt_ip4, M_RTABLE);
2146 if (kt->pfrkt_ip6 != NULL)
2147 free((caddr_t)kt->pfrkt_ip6, M_RTABLE);
2149 if (kt->pfrkt_shadow != NULL)
2150 pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr);
2151 if (kt->pfrkt_rs != NULL) {
2152 kt->pfrkt_rs->tables--;
2153 pf_remove_if_empty_ruleset(kt->pfrkt_rs);
2156 pool_put(&V_pfr_ktable_pl, kt);
2158 pool_put(&pfr_ktable_pl, kt);
2163 pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q)
2167 if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE)))
2169 return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor));
2173 pfr_lookup_table(struct pfr_table *tbl)
2175 /* struct pfr_ktable start like a struct pfr_table */
2176 return (RB_FIND(pfr_ktablehead, &pfr_ktables,
2177 (struct pfr_ktable *)tbl));
2181 pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af)
2183 struct pfr_kentry *ke = NULL;
2186 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2187 kt = kt->pfrkt_root;
2188 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2195 V_pfr_sin.sin_addr.s_addr = a->addr32[0];
2196 ke = (struct pfr_kentry *)rn_match(&V_pfr_sin, kt->pfrkt_ip4);
2198 pfr_sin.sin_addr.s_addr = a->addr32[0];
2199 ke = (struct pfr_kentry *)rn_match(&pfr_sin, kt->pfrkt_ip4);
2201 if (ke && KENTRY_RNF_ROOT(ke))
2208 bcopy(a, &V_pfr_sin6.sin6_addr, sizeof(V_pfr_sin6.sin6_addr));
2209 ke = (struct pfr_kentry *)rn_match(&V_pfr_sin6, kt->pfrkt_ip6);
2211 bcopy(a, &pfr_sin6.sin6_addr, sizeof(pfr_sin6.sin6_addr));
2212 ke = (struct pfr_kentry *)rn_match(&pfr_sin6, kt->pfrkt_ip6);
2214 if (ke && KENTRY_RNF_ROOT(ke))
2219 match = (ke && !ke->pfrke_not);
2223 kt->pfrkt_nomatch++;
2228 pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af,
2229 u_int64_t len, int dir_out, int op_pass, int notrule)
2231 struct pfr_kentry *ke = NULL;
2233 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2234 kt = kt->pfrkt_root;
2235 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2242 V_pfr_sin.sin_addr.s_addr = a->addr32[0];
2243 ke = (struct pfr_kentry *)rn_match(&V_pfr_sin, kt->pfrkt_ip4);
2245 pfr_sin.sin_addr.s_addr = a->addr32[0];
2246 ke = (struct pfr_kentry *)rn_match(&pfr_sin, kt->pfrkt_ip4);
2248 if (ke && KENTRY_RNF_ROOT(ke))
2255 bcopy(a, &V_pfr_sin6.sin6_addr, sizeof(V_pfr_sin6.sin6_addr));
2256 ke = (struct pfr_kentry *)rn_match(&V_pfr_sin6, kt->pfrkt_ip6);
2258 bcopy(a, &pfr_sin6.sin6_addr, sizeof(pfr_sin6.sin6_addr));
2259 ke = (struct pfr_kentry *)rn_match(&pfr_sin6, kt->pfrkt_ip6);
2261 if (ke && KENTRY_RNF_ROOT(ke))
2268 if ((ke == NULL || ke->pfrke_not) != notrule) {
2269 if (op_pass != PFR_OP_PASS)
2270 printf("pfr_update_stats: assertion failed.\n");
2271 op_pass = PFR_OP_XPASS;
2273 kt->pfrkt_packets[dir_out][op_pass]++;
2274 kt->pfrkt_bytes[dir_out][op_pass] += len;
2275 if (ke != NULL && op_pass != PFR_OP_XPASS &&
2276 (kt->pfrkt_flags & PFR_TFLAG_COUNTERS)) {
2277 if (ke->pfrke_counters == NULL)
2279 ke->pfrke_counters = pool_get(&V_pfr_kcounters_pl,
2281 ke->pfrke_counters = pool_get(&pfr_kcounters_pl,
2283 PR_NOWAIT | PR_ZERO);
2284 if (ke->pfrke_counters != NULL) {
2285 ke->pfrke_counters->pfrkc_packets[dir_out][op_pass]++;
2286 ke->pfrke_counters->pfrkc_bytes[dir_out][op_pass] += len;
2292 pfr_attach_table(struct pf_ruleset *rs, char *name, int intr)
2294 struct pfr_ktable *kt, *rt;
2295 struct pfr_table tbl;
2296 struct pf_anchor *ac = rs->anchor;
2298 bzero(&tbl, sizeof(tbl));
2299 strlcpy(tbl.pfrt_name, name, sizeof(tbl.pfrt_name));
2301 strlcpy(tbl.pfrt_anchor, ac->path, sizeof(tbl.pfrt_anchor));
2302 kt = pfr_lookup_table(&tbl);
2304 kt = pfr_create_ktable(&tbl, time_second, 1, intr);
2308 bzero(tbl.pfrt_anchor, sizeof(tbl.pfrt_anchor));
2309 rt = pfr_lookup_table(&tbl);
2311 rt = pfr_create_ktable(&tbl, 0, 1, intr);
2313 pfr_destroy_ktable(kt, 0);
2316 pfr_insert_ktable(rt);
2318 kt->pfrkt_root = rt;
2320 pfr_insert_ktable(kt);
2322 if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++)
2323 pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED);
2328 pfr_detach_table(struct pfr_ktable *kt)
2330 if (kt->pfrkt_refcnt[PFR_REFCNT_RULE] <= 0)
2331 printf("pfr_detach_table: refcount = %d.\n",
2332 kt->pfrkt_refcnt[PFR_REFCNT_RULE]);
2333 else if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE])
2334 pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED);
2338 pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter,
2339 struct pf_addr **raddr, struct pf_addr **rmask, sa_family_t af)
2342 struct pfr_kentry *ke, *ke2 = NULL;
2343 struct pf_addr *addr = NULL;
2345 struct pfr_kentry *ke, *ke2;
2346 struct pf_addr *addr;
2348 union sockaddr_union mask;
2349 int idx = -1, use_counter = 0;
2353 addr = (struct pf_addr *)&V_pfr_sin.sin_addr;
2354 else if (af == AF_INET6)
2355 addr = (struct pf_addr *)&V_pfr_sin6.sin6_addr;
2358 addr = (struct pf_addr *)&pfr_sin.sin_addr;
2359 else if (af == AF_INET6)
2360 addr = (struct pf_addr *)&pfr_sin6.sin6_addr;
2362 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2363 kt = kt->pfrkt_root;
2364 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2369 if (counter != NULL && idx >= 0)
2375 ke = pfr_kentry_byidx(kt, idx, af);
2377 kt->pfrkt_nomatch++;
2381 pfr_prepare_network(&V_pfr_mask, af, ke->pfrke_net);
2383 pfr_prepare_network(&pfr_mask, af, ke->pfrke_net);
2385 *raddr = SUNION2PF(&ke->pfrke_sa, af);
2387 *rmask = SUNION2PF(&V_pfr_mask, af);
2389 *rmask = SUNION2PF(&pfr_mask, af);
2393 /* is supplied address within block? */
2394 if (!PF_MATCHA(0, *raddr, *rmask, counter, af)) {
2395 /* no, go to next block in table */
2400 PF_ACPY(addr, counter, af);
2402 /* use first address of block */
2403 PF_ACPY(addr, *raddr, af);
2406 if (!KENTRY_NETWORK(ke)) {
2407 /* this is a single IP address - no possible nested block */
2408 PF_ACPY(counter, addr, af);
2414 /* we don't want to use a nested block */
2417 ke2 = (struct pfr_kentry *)rn_match(&V_pfr_sin,
2419 else if (af == AF_INET6)
2420 ke2 = (struct pfr_kentry *)rn_match(&V_pfr_sin6,
2424 ke2 = (struct pfr_kentry *)rn_match(&pfr_sin,
2426 else if (af == AF_INET6)
2427 ke2 = (struct pfr_kentry *)rn_match(&pfr_sin6,
2430 /* no need to check KENTRY_RNF_ROOT() here */
2432 /* lookup return the same block - perfect */
2433 PF_ACPY(counter, addr, af);
2439 /* we need to increase the counter past the nested block */
2440 pfr_prepare_network(&mask, AF_INET, ke2->pfrke_net);
2442 PF_POOLMASK(addr, addr, SUNION2PF(&mask, af), &V_pfr_ffaddr, af);
2444 PF_POOLMASK(addr, addr, SUNION2PF(&mask, af), &pfr_ffaddr, af);
2447 if (!PF_MATCHA(0, *raddr, *rmask, addr, af)) {
2448 /* ok, we reached the end of our main block */
2449 /* go to next block in table */
2458 pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af)
2460 struct pfr_walktree w;
2462 bzero(&w, sizeof(w));
2463 w.pfrw_op = PFRW_POOL_GET;
2470 kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
2472 rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
2474 return (w.pfrw_kentry);
2479 kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree, &w);
2481 rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w);
2483 return (w.pfrw_kentry);
2491 pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn)
2493 struct pfr_walktree w;
2496 bzero(&w, sizeof(w));
2497 w.pfrw_op = PFRW_DYNADDR_UPDATE;
2501 dyn->pfid_acnt4 = 0;
2502 dyn->pfid_acnt6 = 0;
2503 if (!dyn->pfid_af || dyn->pfid_af == AF_INET)
2505 kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
2507 rn_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
2509 if (!dyn->pfid_af || dyn->pfid_af == AF_INET6)
2511 kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree, &w);
2513 rn_walktree(kt->pfrkt_ip6, pfr_walktree, &w);