]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/pf_table.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / net / pf_table.c
1 /*
2 * Copyright (c) 2007-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $apfw: pf_table.c,v 1.4 2008/08/27 00:01:32 jhw Exp $ */
30 /* $OpenBSD: pf_table.c,v 1.68 2006/05/02 10:08:45 dhartmei Exp $ */
31
32 /*
33 * Copyright (c) 2002 Cedric Berger
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * - Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * - Redistributions in binary form must reproduce the above
43 * copyright notice, this list of conditions and the following
44 * disclaimer in the documentation and/or other materials provided
45 * with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
50 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
51 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
53 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 *
60 */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/socket.h>
65 #include <sys/mbuf.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68
69 #include <net/if.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <net/radix.h>
73 #include <net/pfvar.h>
74
75 #define ACCEPT_FLAGS(flags, oklist) \
76 do { \
77 if ((flags & ~(oklist)) & \
78 PFR_FLAG_ALLMASK) \
79 return (EINVAL); \
80 } while (0)
81
82 #define COPYIN(from, to, size, flags) \
83 ((flags & PFR_FLAG_USERIOCTL) ? \
84 copyin((from), (to), (size)) : \
85 (bcopy((void *)(uintptr_t)(from), (to), (size)), 0))
86
87 #define COPYOUT(from, to, size, flags) \
88 ((flags & PFR_FLAG_USERIOCTL) ? \
89 copyout((from), (to), (size)) : \
90 (bcopy((from), (void *)(uintptr_t)(to), (size)), 0))
91
92 #define FILLIN_SIN(sin, addr) \
93 do { \
94 (sin).sin_len = sizeof (sin); \
95 (sin).sin_family = AF_INET; \
96 (sin).sin_addr = (addr); \
97 } while (0)
98
99 #define FILLIN_SIN6(sin6, addr) \
100 do { \
101 (sin6).sin6_len = sizeof (sin6); \
102 (sin6).sin6_family = AF_INET6; \
103 (sin6).sin6_addr = (addr); \
104 } while (0)
105
106 #define SWAP(type, a1, a2) \
107 do { \
108 type tmp = a1; \
109 a1 = a2; \
110 a2 = tmp; \
111 } while (0)
112
113 #define SUNION2PF(su, af) (((af) == AF_INET) ? \
114 (struct pf_addr *)&(su)->sin.sin_addr : \
115 (struct pf_addr *)&(su)->sin6.sin6_addr)
116
117 #define AF_BITS(af) (((af) == AF_INET) ? 32 : 128)
118 #define ADDR_NETWORK(ad) ((ad)->pfra_net < AF_BITS((ad)->pfra_af))
119 #define KENTRY_NETWORK(ke) ((ke)->pfrke_net < AF_BITS((ke)->pfrke_af))
120 #define KENTRY_RNF_ROOT(ke) \
121 ((((struct radix_node *)(ke))->rn_flags & RNF_ROOT) != 0)
122
123 #define NO_ADDRESSES (-1)
124 #define ENQUEUE_UNMARKED_ONLY (1)
125 #define INVERT_NEG_FLAG (1)
126
127 struct pfr_walktree {
128 enum pfrw_op {
129 PFRW_MARK,
130 PFRW_SWEEP,
131 PFRW_ENQUEUE,
132 PFRW_GET_ADDRS,
133 PFRW_GET_ASTATS,
134 PFRW_POOL_GET,
135 PFRW_DYNADDR_UPDATE
136 } pfrw_op;
137 union {
138 user_addr_t pfrw1_addr;
139 user_addr_t pfrw1_astats;
140 struct pfr_kentryworkq *pfrw1_workq;
141 struct pfr_kentry *pfrw1_kentry;
142 struct pfi_dynaddr *pfrw1_dyn;
143 } pfrw_1;
144 int pfrw_free;
145 int pfrw_flags;
146 };
147 #define pfrw_addr pfrw_1.pfrw1_addr
148 #define pfrw_astats pfrw_1.pfrw1_astats
149 #define pfrw_workq pfrw_1.pfrw1_workq
150 #define pfrw_kentry pfrw_1.pfrw1_kentry
151 #define pfrw_dyn pfrw_1.pfrw1_dyn
152 #define pfrw_cnt pfrw_free
153
154 #define senderr(e) do { rv = (e); goto _bad; } while (0)
155
156 struct pool pfr_ktable_pl;
157 struct pool pfr_kentry_pl;
158
159 static struct pool pfr_kentry_pl2;
160 static struct sockaddr_in pfr_sin;
161 static struct sockaddr_in6 pfr_sin6;
162 static union sockaddr_union pfr_mask;
163 static struct pf_addr pfr_ffaddr;
164
165 static void pfr_copyout_addr(struct pfr_addr *, struct pfr_kentry *ke);
166 static int pfr_validate_addr(struct pfr_addr *);
167 static void pfr_enqueue_addrs(struct pfr_ktable *, struct pfr_kentryworkq *,
168 int *, int);
169 static void pfr_mark_addrs(struct pfr_ktable *);
170 static struct pfr_kentry *pfr_lookup_addr(struct pfr_ktable *,
171 struct pfr_addr *, int);
172 static struct pfr_kentry *pfr_create_kentry(struct pfr_addr *, int);
173 static void pfr_destroy_kentries(struct pfr_kentryworkq *);
174 static void pfr_destroy_kentry(struct pfr_kentry *);
175 static void pfr_insert_kentries(struct pfr_ktable *,
176 struct pfr_kentryworkq *, u_int64_t);
177 static void pfr_remove_kentries(struct pfr_ktable *, struct pfr_kentryworkq *);
178 static void pfr_clstats_kentries(struct pfr_kentryworkq *, u_int64_t, int);
179 static void pfr_reset_feedback(user_addr_t, int, int);
180 static void pfr_prepare_network(union sockaddr_union *, int, int);
181 static int pfr_route_kentry(struct pfr_ktable *, struct pfr_kentry *);
182 static int pfr_unroute_kentry(struct pfr_ktable *, struct pfr_kentry *);
183 static int pfr_walktree(struct radix_node *, void *);
184 static int pfr_validate_table(struct pfr_table *, int, int);
185 static int pfr_fix_anchor(char *);
186 static void pfr_commit_ktable(struct pfr_ktable *, u_int64_t);
187 static void pfr_insert_ktables(struct pfr_ktableworkq *);
188 static void pfr_insert_ktable(struct pfr_ktable *);
189 static void pfr_setflags_ktables(struct pfr_ktableworkq *);
190 static void pfr_setflags_ktable(struct pfr_ktable *, int);
191 static void pfr_clstats_ktables(struct pfr_ktableworkq *, u_int64_t, int);
192 static void pfr_clstats_ktable(struct pfr_ktable *, u_int64_t, int);
193 static struct pfr_ktable *pfr_create_ktable(struct pfr_table *, u_int64_t, int);
194 static void pfr_destroy_ktables(struct pfr_ktableworkq *, int);
195 static void pfr_destroy_ktable(struct pfr_ktable *, int);
196 static int pfr_ktable_compare(struct pfr_ktable *, struct pfr_ktable *);
197 static struct pfr_ktable *pfr_lookup_table(struct pfr_table *);
198 static void pfr_clean_node_mask(struct pfr_ktable *, struct pfr_kentryworkq *);
199 static int pfr_table_count(struct pfr_table *, int);
200 static int pfr_skip_table(struct pfr_table *, struct pfr_ktable *, int);
201 static struct pfr_kentry *pfr_kentry_byidx(struct pfr_ktable *, int, int);
202
203 RB_PROTOTYPE_SC(static, pfr_ktablehead, pfr_ktable, pfrkt_tree,
204 pfr_ktable_compare);
205 RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
206
207 static struct pfr_ktablehead pfr_ktables;
208 static struct pfr_table pfr_nulltable;
209 static int pfr_ktable_cnt;
210
211 void
212 pfr_initialize(void)
213 {
214 pool_init(&pfr_ktable_pl, sizeof (struct pfr_ktable), 0, 0, 0,
215 "pfrktable", NULL);
216 pool_init(&pfr_kentry_pl, sizeof (struct pfr_kentry), 0, 0, 0,
217 "pfrkentry", NULL);
218 pool_init(&pfr_kentry_pl2, sizeof (struct pfr_kentry), 0, 0, 0,
219 "pfrkentry2", NULL);
220
221 pfr_sin.sin_len = sizeof (pfr_sin);
222 pfr_sin.sin_family = AF_INET;
223 pfr_sin6.sin6_len = sizeof (pfr_sin6);
224 pfr_sin6.sin6_family = AF_INET6;
225
226 memset(&pfr_ffaddr, 0xff, sizeof (pfr_ffaddr));
227 }
228
229 #if 0
230 void
231 pfr_destroy(void)
232 {
233 pool_destroy(&pfr_ktable_pl);
234 pool_destroy(&pfr_kentry_pl);
235 pool_destroy(&pfr_kentry_pl2);
236 }
237 #endif
238
239 int
240 pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
241 {
242 struct pfr_ktable *kt;
243 struct pfr_kentryworkq workq;
244
245 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
246 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
247 return (EINVAL);
248 kt = pfr_lookup_table(tbl);
249 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
250 return (ESRCH);
251 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
252 return (EPERM);
253 pfr_enqueue_addrs(kt, &workq, ndel, 0);
254
255 if (!(flags & PFR_FLAG_DUMMY)) {
256 pfr_remove_kentries(kt, &workq);
257 if (kt->pfrkt_cnt) {
258 printf("pfr_clr_addrs: corruption detected (%d).\n",
259 kt->pfrkt_cnt);
260 kt->pfrkt_cnt = 0;
261 }
262 }
263 return (0);
264 }
265
266 int
267 pfr_add_addrs(struct pfr_table *tbl, user_addr_t _addr, int size,
268 int *nadd, int flags)
269 {
270 struct pfr_ktable *kt, *tmpkt;
271 struct pfr_kentryworkq workq;
272 struct pfr_kentry *p, *q;
273 struct pfr_addr ad;
274 int i, rv, xadd = 0;
275 user_addr_t addr = _addr;
276 u_int64_t tzero = pf_calendar_time_second();
277
278 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
279 PFR_FLAG_FEEDBACK);
280 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
281 return (EINVAL);
282 kt = pfr_lookup_table(tbl);
283 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
284 return (ESRCH);
285 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
286 return (EPERM);
287 tmpkt = pfr_create_ktable(&pfr_nulltable, 0, 0);
288 if (tmpkt == NULL)
289 return (ENOMEM);
290 SLIST_INIT(&workq);
291 for (i = 0; i < size; i++, addr += sizeof (ad)) {
292 if (COPYIN(addr, &ad, sizeof (ad), flags))
293 senderr(EFAULT);
294 if (pfr_validate_addr(&ad))
295 senderr(EINVAL);
296 p = pfr_lookup_addr(kt, &ad, 1);
297 q = pfr_lookup_addr(tmpkt, &ad, 1);
298 if (flags & PFR_FLAG_FEEDBACK) {
299 if (q != NULL)
300 ad.pfra_fback = PFR_FB_DUPLICATE;
301 else if (p == NULL)
302 ad.pfra_fback = PFR_FB_ADDED;
303 else if (p->pfrke_not != ad.pfra_not)
304 ad.pfra_fback = PFR_FB_CONFLICT;
305 else
306 ad.pfra_fback = PFR_FB_NONE;
307 }
308 if (p == NULL && q == NULL) {
309 p = pfr_create_kentry(&ad,
310 !(flags & PFR_FLAG_USERIOCTL));
311 if (p == NULL)
312 senderr(ENOMEM);
313 if (pfr_route_kentry(tmpkt, p)) {
314 pfr_destroy_kentry(p);
315 ad.pfra_fback = PFR_FB_NONE;
316 } else {
317 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
318 xadd++;
319 }
320 }
321 if (flags & PFR_FLAG_FEEDBACK)
322 if (COPYOUT(&ad, addr, sizeof (ad), flags))
323 senderr(EFAULT);
324 }
325 pfr_clean_node_mask(tmpkt, &workq);
326 if (!(flags & PFR_FLAG_DUMMY)) {
327 pfr_insert_kentries(kt, &workq, tzero);
328 } else
329 pfr_destroy_kentries(&workq);
330 if (nadd != NULL)
331 *nadd = xadd;
332 pfr_destroy_ktable(tmpkt, 0);
333 return (0);
334 _bad:
335 pfr_clean_node_mask(tmpkt, &workq);
336 pfr_destroy_kentries(&workq);
337 if (flags & PFR_FLAG_FEEDBACK)
338 pfr_reset_feedback(_addr, size, flags);
339 pfr_destroy_ktable(tmpkt, 0);
340 return (rv);
341 }
342
343 int
344 pfr_del_addrs(struct pfr_table *tbl, user_addr_t _addr, int size,
345 int *ndel, int flags)
346 {
347 struct pfr_ktable *kt;
348 struct pfr_kentryworkq workq;
349 struct pfr_kentry *p;
350 struct pfr_addr ad;
351 user_addr_t addr = _addr;
352 int i, rv, xdel = 0, log = 1;
353
354 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
355 PFR_FLAG_FEEDBACK);
356 if (pfr_validate_table(tbl, 0, flags & PFR_FLAG_USERIOCTL))
357 return (EINVAL);
358 kt = pfr_lookup_table(tbl);
359 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
360 return (ESRCH);
361 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
362 return (EPERM);
363 /*
364 * there are two algorithms to choose from here.
365 * with:
366 * n: number of addresses to delete
367 * N: number of addresses in the table
368 *
369 * one is O(N) and is better for large 'n'
370 * one is O(n*LOG(N)) and is better for small 'n'
371 *
372 * following code try to decide which one is best.
373 */
374 for (i = kt->pfrkt_cnt; i > 0; i >>= 1)
375 log++;
376 if (size > kt->pfrkt_cnt/log) {
377 /* full table scan */
378 pfr_mark_addrs(kt);
379 } else {
380 /* iterate over addresses to delete */
381 for (i = 0; i < size; i++, addr += sizeof (ad)) {
382 if (COPYIN(addr, &ad, sizeof (ad), flags))
383 return (EFAULT);
384 if (pfr_validate_addr(&ad))
385 return (EINVAL);
386 p = pfr_lookup_addr(kt, &ad, 1);
387 if (p != NULL)
388 p->pfrke_mark = 0;
389 }
390 }
391 SLIST_INIT(&workq);
392 for (addr = _addr, i = 0; i < size; i++, addr += sizeof (ad)) {
393 if (COPYIN(addr, &ad, sizeof (ad), flags))
394 senderr(EFAULT);
395 if (pfr_validate_addr(&ad))
396 senderr(EINVAL);
397 p = pfr_lookup_addr(kt, &ad, 1);
398 if (flags & PFR_FLAG_FEEDBACK) {
399 if (p == NULL)
400 ad.pfra_fback = PFR_FB_NONE;
401 else if (p->pfrke_not != ad.pfra_not)
402 ad.pfra_fback = PFR_FB_CONFLICT;
403 else if (p->pfrke_mark)
404 ad.pfra_fback = PFR_FB_DUPLICATE;
405 else
406 ad.pfra_fback = PFR_FB_DELETED;
407 }
408 if (p != NULL && p->pfrke_not == ad.pfra_not &&
409 !p->pfrke_mark) {
410 p->pfrke_mark = 1;
411 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
412 xdel++;
413 }
414 if (flags & PFR_FLAG_FEEDBACK)
415 if (COPYOUT(&ad, addr, sizeof (ad), flags))
416 senderr(EFAULT);
417 }
418 if (!(flags & PFR_FLAG_DUMMY)) {
419 pfr_remove_kentries(kt, &workq);
420 }
421 if (ndel != NULL)
422 *ndel = xdel;
423 return (0);
424 _bad:
425 if (flags & PFR_FLAG_FEEDBACK)
426 pfr_reset_feedback(_addr, size, flags);
427 return (rv);
428 }
429
430 int
431 pfr_set_addrs(struct pfr_table *tbl, user_addr_t _addr, int size,
432 int *size2, int *nadd, int *ndel, int *nchange, int flags,
433 u_int32_t ignore_pfrt_flags)
434 {
435 struct pfr_ktable *kt, *tmpkt;
436 struct pfr_kentryworkq addq, delq, changeq;
437 struct pfr_kentry *p, *q;
438 struct pfr_addr ad;
439 user_addr_t addr = _addr;
440 int i, rv, xadd = 0, xdel = 0, xchange = 0;
441 u_int64_t tzero = pf_calendar_time_second();
442
443 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
444 PFR_FLAG_FEEDBACK);
445 if (pfr_validate_table(tbl, ignore_pfrt_flags, flags &
446 PFR_FLAG_USERIOCTL))
447 return (EINVAL);
448 kt = pfr_lookup_table(tbl);
449 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
450 return (ESRCH);
451 if (kt->pfrkt_flags & PFR_TFLAG_CONST)
452 return (EPERM);
453 tmpkt = pfr_create_ktable(&pfr_nulltable, 0, 0);
454 if (tmpkt == NULL)
455 return (ENOMEM);
456 pfr_mark_addrs(kt);
457 SLIST_INIT(&addq);
458 SLIST_INIT(&delq);
459 SLIST_INIT(&changeq);
460 for (i = 0; i < size; i++, addr += sizeof (ad)) {
461 if (COPYIN(addr, &ad, sizeof (ad), flags))
462 senderr(EFAULT);
463 if (pfr_validate_addr(&ad))
464 senderr(EINVAL);
465 ad.pfra_fback = PFR_FB_NONE;
466 p = pfr_lookup_addr(kt, &ad, 1);
467 if (p != NULL) {
468 if (p->pfrke_mark) {
469 ad.pfra_fback = PFR_FB_DUPLICATE;
470 goto _skip;
471 }
472 p->pfrke_mark = 1;
473 if (p->pfrke_not != ad.pfra_not) {
474 SLIST_INSERT_HEAD(&changeq, p, pfrke_workq);
475 ad.pfra_fback = PFR_FB_CHANGED;
476 xchange++;
477 }
478 } else {
479 q = pfr_lookup_addr(tmpkt, &ad, 1);
480 if (q != NULL) {
481 ad.pfra_fback = PFR_FB_DUPLICATE;
482 goto _skip;
483 }
484 p = pfr_create_kentry(&ad,
485 !(flags & PFR_FLAG_USERIOCTL));
486 if (p == NULL)
487 senderr(ENOMEM);
488 if (pfr_route_kentry(tmpkt, p)) {
489 pfr_destroy_kentry(p);
490 ad.pfra_fback = PFR_FB_NONE;
491 } else {
492 SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
493 ad.pfra_fback = PFR_FB_ADDED;
494 xadd++;
495 }
496 }
497 _skip:
498 if (flags & PFR_FLAG_FEEDBACK)
499 if (COPYOUT(&ad, addr, sizeof (ad), flags))
500 senderr(EFAULT);
501 }
502 pfr_enqueue_addrs(kt, &delq, &xdel, ENQUEUE_UNMARKED_ONLY);
503 if ((flags & PFR_FLAG_FEEDBACK) && *size2) {
504 if (*size2 < size+xdel) {
505 *size2 = size+xdel;
506 senderr(0);
507 }
508 i = 0;
509 addr = _addr + size;
510 SLIST_FOREACH(p, &delq, pfrke_workq) {
511 pfr_copyout_addr(&ad, p);
512 ad.pfra_fback = PFR_FB_DELETED;
513 if (COPYOUT(&ad, addr, sizeof (ad), flags))
514 senderr(EFAULT);
515 addr += sizeof (ad);
516 i++;
517 }
518 }
519 pfr_clean_node_mask(tmpkt, &addq);
520 if (!(flags & PFR_FLAG_DUMMY)) {
521 pfr_insert_kentries(kt, &addq, tzero);
522 pfr_remove_kentries(kt, &delq);
523 pfr_clstats_kentries(&changeq, tzero, INVERT_NEG_FLAG);
524 } else
525 pfr_destroy_kentries(&addq);
526 if (nadd != NULL)
527 *nadd = xadd;
528 if (ndel != NULL)
529 *ndel = xdel;
530 if (nchange != NULL)
531 *nchange = xchange;
532 if ((flags & PFR_FLAG_FEEDBACK) && size2)
533 *size2 = size+xdel;
534 pfr_destroy_ktable(tmpkt, 0);
535 return (0);
536 _bad:
537 pfr_clean_node_mask(tmpkt, &addq);
538 pfr_destroy_kentries(&addq);
539 if (flags & PFR_FLAG_FEEDBACK)
540 pfr_reset_feedback(_addr, size, flags);
541 pfr_destroy_ktable(tmpkt, 0);
542 return (rv);
543 }
544
545 int
546 pfr_tst_addrs(struct pfr_table *tbl, user_addr_t addr, int size,
547 int *nmatch, int flags)
548 {
549 struct pfr_ktable *kt;
550 struct pfr_kentry *p;
551 struct pfr_addr ad;
552 int i, xmatch = 0;
553
554 ACCEPT_FLAGS(flags, PFR_FLAG_REPLACE);
555 if (pfr_validate_table(tbl, 0, 0))
556 return (EINVAL);
557 kt = pfr_lookup_table(tbl);
558 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
559 return (ESRCH);
560
561 for (i = 0; i < size; i++, addr += sizeof (ad)) {
562 if (COPYIN(addr, &ad, sizeof (ad), flags))
563 return (EFAULT);
564 if (pfr_validate_addr(&ad))
565 return (EINVAL);
566 if (ADDR_NETWORK(&ad))
567 return (EINVAL);
568 p = pfr_lookup_addr(kt, &ad, 0);
569 if (flags & PFR_FLAG_REPLACE)
570 pfr_copyout_addr(&ad, p);
571 ad.pfra_fback = (p == NULL) ? PFR_FB_NONE :
572 (p->pfrke_not ? PFR_FB_NOTMATCH : PFR_FB_MATCH);
573 if (p != NULL && !p->pfrke_not)
574 xmatch++;
575 if (COPYOUT(&ad, addr, sizeof (ad), flags))
576 return (EFAULT);
577 }
578 if (nmatch != NULL)
579 *nmatch = xmatch;
580 return (0);
581 }
582
583 int
584 pfr_get_addrs(struct pfr_table *tbl, user_addr_t addr, int *size,
585 int flags)
586 {
587 struct pfr_ktable *kt;
588 struct pfr_walktree w;
589 int rv;
590
591 ACCEPT_FLAGS(flags, 0);
592 if (pfr_validate_table(tbl, 0, 0))
593 return (EINVAL);
594 kt = pfr_lookup_table(tbl);
595 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
596 return (ESRCH);
597 if (kt->pfrkt_cnt > *size) {
598 *size = kt->pfrkt_cnt;
599 return (0);
600 }
601
602 bzero(&w, sizeof (w));
603 w.pfrw_op = PFRW_GET_ADDRS;
604 w.pfrw_addr = addr;
605 w.pfrw_free = kt->pfrkt_cnt;
606 w.pfrw_flags = flags;
607 rv = kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
608 if (!rv)
609 rv = kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6,
610 pfr_walktree, &w);
611 if (rv)
612 return (rv);
613
614 if (w.pfrw_free) {
615 printf("pfr_get_addrs: corruption detected (%d).\n",
616 w.pfrw_free);
617 return (ENOTTY);
618 }
619 *size = kt->pfrkt_cnt;
620 return (0);
621 }
622
623 int
624 pfr_get_astats(struct pfr_table *tbl, user_addr_t addr, int *size,
625 int flags)
626 {
627 struct pfr_ktable *kt;
628 struct pfr_walktree w;
629 struct pfr_kentryworkq workq;
630 int rv;
631 u_int64_t tzero = pf_calendar_time_second();
632
633 /* XXX PFR_FLAG_CLSTATS disabled */
634 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC);
635 if (pfr_validate_table(tbl, 0, 0))
636 return (EINVAL);
637 kt = pfr_lookup_table(tbl);
638 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
639 return (ESRCH);
640 if (kt->pfrkt_cnt > *size) {
641 *size = kt->pfrkt_cnt;
642 return (0);
643 }
644
645 bzero(&w, sizeof (w));
646 w.pfrw_op = PFRW_GET_ASTATS;
647 w.pfrw_astats = addr;
648 w.pfrw_free = kt->pfrkt_cnt;
649 w.pfrw_flags = flags;
650 rv = kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w);
651 if (!rv)
652 rv = kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6,
653 pfr_walktree, &w);
654 if (!rv && (flags & PFR_FLAG_CLSTATS)) {
655 pfr_enqueue_addrs(kt, &workq, NULL, 0);
656 pfr_clstats_kentries(&workq, tzero, 0);
657 }
658 if (rv)
659 return (rv);
660
661 if (w.pfrw_free) {
662 printf("pfr_get_astats: corruption detected (%d).\n",
663 w.pfrw_free);
664 return (ENOTTY);
665 }
666 *size = kt->pfrkt_cnt;
667 return (0);
668 }
669
670 int
671 pfr_clr_astats(struct pfr_table *tbl, user_addr_t _addr, int size,
672 int *nzero, int flags)
673 {
674 struct pfr_ktable *kt;
675 struct pfr_kentryworkq workq;
676 struct pfr_kentry *p;
677 struct pfr_addr ad;
678 user_addr_t addr = _addr;
679 int i, rv, xzero = 0;
680
681 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
682 PFR_FLAG_FEEDBACK);
683 if (pfr_validate_table(tbl, 0, 0))
684 return (EINVAL);
685 kt = pfr_lookup_table(tbl);
686 if (kt == NULL || !(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
687 return (ESRCH);
688 SLIST_INIT(&workq);
689 for (i = 0; i < size; i++, addr += sizeof (ad)) {
690 if (COPYIN(addr, &ad, sizeof (ad), flags))
691 senderr(EFAULT);
692 if (pfr_validate_addr(&ad))
693 senderr(EINVAL);
694 p = pfr_lookup_addr(kt, &ad, 1);
695 if (flags & PFR_FLAG_FEEDBACK) {
696 ad.pfra_fback = (p != NULL) ?
697 PFR_FB_CLEARED : PFR_FB_NONE;
698 if (COPYOUT(&ad, addr, sizeof (ad), flags))
699 senderr(EFAULT);
700 }
701 if (p != NULL) {
702 SLIST_INSERT_HEAD(&workq, p, pfrke_workq);
703 xzero++;
704 }
705 }
706
707 if (!(flags & PFR_FLAG_DUMMY)) {
708 pfr_clstats_kentries(&workq, 0, 0);
709 }
710 if (nzero != NULL)
711 *nzero = xzero;
712 return (0);
713 _bad:
714 if (flags & PFR_FLAG_FEEDBACK)
715 pfr_reset_feedback(_addr, size, flags);
716 return (rv);
717 }
718
719 static int
720 pfr_validate_addr(struct pfr_addr *ad)
721 {
722 int i;
723
724 switch (ad->pfra_af) {
725 #if INET
726 case AF_INET:
727 if (ad->pfra_net > 32)
728 return (-1);
729 break;
730 #endif /* INET */
731 #if INET6
732 case AF_INET6:
733 if (ad->pfra_net > 128)
734 return (-1);
735 break;
736 #endif /* INET6 */
737 default:
738 return (-1);
739 }
740 if (ad->pfra_net < 128 &&
741 (((caddr_t)ad)[ad->pfra_net/8] & (0xFF >> (ad->pfra_net%8))))
742 return (-1);
743 for (i = (ad->pfra_net+7)/8; i < (int)sizeof (ad->pfra_u); i++)
744 if (((caddr_t)ad)[i])
745 return (-1);
746 if (ad->pfra_not && ad->pfra_not != 1)
747 return (-1);
748 if (ad->pfra_fback)
749 return (-1);
750 return (0);
751 }
752
753 static void
754 pfr_enqueue_addrs(struct pfr_ktable *kt, struct pfr_kentryworkq *workq,
755 int *naddr, int sweep)
756 {
757 struct pfr_walktree w;
758
759 SLIST_INIT(workq);
760 bzero(&w, sizeof (w));
761 w.pfrw_op = sweep ? PFRW_SWEEP : PFRW_ENQUEUE;
762 w.pfrw_workq = workq;
763 if (kt->pfrkt_ip4 != NULL)
764 if (kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4,
765 pfr_walktree, &w))
766 printf("pfr_enqueue_addrs: IPv4 walktree failed.\n");
767 if (kt->pfrkt_ip6 != NULL)
768 if (kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6,
769 pfr_walktree, &w))
770 printf("pfr_enqueue_addrs: IPv6 walktree failed.\n");
771 if (naddr != NULL)
772 *naddr = w.pfrw_cnt;
773 }
774
775 static void
776 pfr_mark_addrs(struct pfr_ktable *kt)
777 {
778 struct pfr_walktree w;
779
780 bzero(&w, sizeof (w));
781 w.pfrw_op = PFRW_MARK;
782 if (kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4, pfr_walktree, &w))
783 printf("pfr_mark_addrs: IPv4 walktree failed.\n");
784 if (kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6, pfr_walktree, &w))
785 printf("pfr_mark_addrs: IPv6 walktree failed.\n");
786 }
787
788
789 static struct pfr_kentry *
790 pfr_lookup_addr(struct pfr_ktable *kt, struct pfr_addr *ad, int exact)
791 {
792 union sockaddr_union sa, mask;
793 struct radix_node_head *head;
794 struct pfr_kentry *ke;
795
796 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
797
798 bzero(&sa, sizeof (sa));
799 if (ad->pfra_af == AF_INET) {
800 FILLIN_SIN(sa.sin, ad->pfra_ip4addr);
801 head = kt->pfrkt_ip4;
802 } else if (ad->pfra_af == AF_INET6) {
803 FILLIN_SIN6(sa.sin6, ad->pfra_ip6addr);
804 head = kt->pfrkt_ip6;
805 }
806 else
807 return NULL;
808 if (ADDR_NETWORK(ad)) {
809 pfr_prepare_network(&mask, ad->pfra_af, ad->pfra_net);
810 ke = (struct pfr_kentry *)rn_lookup(&sa, &mask, head);
811 if (ke && KENTRY_RNF_ROOT(ke))
812 ke = NULL;
813 } else {
814 ke = (struct pfr_kentry *)rn_match(&sa, head);
815 if (ke && KENTRY_RNF_ROOT(ke))
816 ke = NULL;
817 if (exact && ke && KENTRY_NETWORK(ke))
818 ke = NULL;
819 }
820 return (ke);
821 }
822
823 static struct pfr_kentry *
824 pfr_create_kentry(struct pfr_addr *ad, int intr)
825 {
826 struct pfr_kentry *ke;
827
828 if (intr)
829 ke = pool_get(&pfr_kentry_pl2, PR_WAITOK);
830 else
831 ke = pool_get(&pfr_kentry_pl, PR_WAITOK);
832 if (ke == NULL)
833 return (NULL);
834 bzero(ke, sizeof (*ke));
835
836 if (ad->pfra_af == AF_INET)
837 FILLIN_SIN(ke->pfrke_sa.sin, ad->pfra_ip4addr);
838 else if (ad->pfra_af == AF_INET6)
839 FILLIN_SIN6(ke->pfrke_sa.sin6, ad->pfra_ip6addr);
840 ke->pfrke_af = ad->pfra_af;
841 ke->pfrke_net = ad->pfra_net;
842 ke->pfrke_not = ad->pfra_not;
843 ke->pfrke_intrpool = intr;
844 return (ke);
845 }
846
847 static void
848 pfr_destroy_kentries(struct pfr_kentryworkq *workq)
849 {
850 struct pfr_kentry *p, *q;
851
852 for (p = SLIST_FIRST(workq); p != NULL; p = q) {
853 q = SLIST_NEXT(p, pfrke_workq);
854 pfr_destroy_kentry(p);
855 }
856 }
857
858 static void
859 pfr_destroy_kentry(struct pfr_kentry *ke)
860 {
861 if (ke->pfrke_intrpool)
862 pool_put(&pfr_kentry_pl2, ke);
863 else
864 pool_put(&pfr_kentry_pl, ke);
865 }
866
867 static void
868 pfr_insert_kentries(struct pfr_ktable *kt,
869 struct pfr_kentryworkq *workq, u_int64_t tzero)
870 {
871 struct pfr_kentry *p;
872 int rv, n = 0;
873
874 SLIST_FOREACH(p, workq, pfrke_workq) {
875 rv = pfr_route_kentry(kt, p);
876 if (rv) {
877 printf("pfr_insert_kentries: cannot route entry "
878 "(code=%d).\n", rv);
879 break;
880 }
881 p->pfrke_tzero = tzero;
882 n++;
883 }
884 kt->pfrkt_cnt += n;
885 }
886
887 int
888 pfr_insert_kentry(struct pfr_ktable *kt, struct pfr_addr *ad, u_int64_t tzero)
889 {
890 struct pfr_kentry *p;
891 int rv;
892
893 p = pfr_lookup_addr(kt, ad, 1);
894 if (p != NULL)
895 return (0);
896 p = pfr_create_kentry(ad, 1);
897 if (p == NULL)
898 return (EINVAL);
899
900 rv = pfr_route_kentry(kt, p);
901 if (rv)
902 return (rv);
903
904 p->pfrke_tzero = tzero;
905 kt->pfrkt_cnt++;
906
907 return (0);
908 }
909
910 static void
911 pfr_remove_kentries(struct pfr_ktable *kt,
912 struct pfr_kentryworkq *workq)
913 {
914 struct pfr_kentry *p;
915 int n = 0;
916
917 SLIST_FOREACH(p, workq, pfrke_workq) {
918 pfr_unroute_kentry(kt, p);
919 n++;
920 }
921 kt->pfrkt_cnt -= n;
922 pfr_destroy_kentries(workq);
923 }
924
925 static void
926 pfr_clean_node_mask(struct pfr_ktable *kt,
927 struct pfr_kentryworkq *workq)
928 {
929 struct pfr_kentry *p;
930
931 SLIST_FOREACH(p, workq, pfrke_workq)
932 pfr_unroute_kentry(kt, p);
933 }
934
935 static void
936 pfr_clstats_kentries(struct pfr_kentryworkq *workq, u_int64_t tzero,
937 int negchange)
938 {
939 struct pfr_kentry *p;
940
941 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
942
943 SLIST_FOREACH(p, workq, pfrke_workq) {
944 if (negchange)
945 p->pfrke_not = !p->pfrke_not;
946 bzero(p->pfrke_packets, sizeof (p->pfrke_packets));
947 bzero(p->pfrke_bytes, sizeof (p->pfrke_bytes));
948 p->pfrke_tzero = tzero;
949 }
950 }
951
952 static void
953 pfr_reset_feedback(user_addr_t addr, int size, int flags)
954 {
955 struct pfr_addr ad;
956 int i;
957
958 for (i = 0; i < size; i++, addr += sizeof (ad)) {
959 if (COPYIN(addr, &ad, sizeof (ad), flags))
960 break;
961 ad.pfra_fback = PFR_FB_NONE;
962 if (COPYOUT(&ad, addr, sizeof (ad), flags))
963 break;
964 }
965 }
966
967 static void
968 pfr_prepare_network(union sockaddr_union *sa, int af, int net)
969 {
970 int i;
971
972 bzero(sa, sizeof (*sa));
973 if (af == AF_INET) {
974 sa->sin.sin_len = sizeof (sa->sin);
975 sa->sin.sin_family = AF_INET;
976 sa->sin.sin_addr.s_addr = net ? htonl(-1 << (32-net)) : 0;
977 } else if (af == AF_INET6) {
978 sa->sin6.sin6_len = sizeof (sa->sin6);
979 sa->sin6.sin6_family = AF_INET6;
980 for (i = 0; i < 4; i++) {
981 if (net <= 32) {
982 sa->sin6.sin6_addr.s6_addr32[i] =
983 net ? htonl(-1 << (32-net)) : 0;
984 break;
985 }
986 sa->sin6.sin6_addr.s6_addr32[i] = 0xFFFFFFFF;
987 net -= 32;
988 }
989 }
990 }
991
992 static int
993 pfr_route_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
994 {
995 union sockaddr_union mask;
996 struct radix_node *rn;
997 struct radix_node_head *head;
998
999 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1000
1001 bzero(ke->pfrke_node, sizeof (ke->pfrke_node));
1002 if (ke->pfrke_af == AF_INET)
1003 head = kt->pfrkt_ip4;
1004 else if (ke->pfrke_af == AF_INET6)
1005 head = kt->pfrkt_ip6;
1006 else
1007 return (-1);
1008
1009 if (KENTRY_NETWORK(ke)) {
1010 pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
1011 rn = rn_addroute(&ke->pfrke_sa, &mask, head, ke->pfrke_node);
1012 } else
1013 rn = rn_addroute(&ke->pfrke_sa, NULL, head, ke->pfrke_node);
1014
1015 return (rn == NULL ? -1 : 0);
1016 }
1017
1018 static int
1019 pfr_unroute_kentry(struct pfr_ktable *kt, struct pfr_kentry *ke)
1020 {
1021 union sockaddr_union mask;
1022 struct radix_node *rn;
1023 struct radix_node_head *head;
1024
1025 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1026
1027 if (ke->pfrke_af == AF_INET)
1028 head = kt->pfrkt_ip4;
1029 else if (ke->pfrke_af == AF_INET6)
1030 head = kt->pfrkt_ip6;
1031 else
1032 return (-1);
1033
1034 if (KENTRY_NETWORK(ke)) {
1035 pfr_prepare_network(&mask, ke->pfrke_af, ke->pfrke_net);
1036 rn = rn_delete(&ke->pfrke_sa, &mask, head);
1037 } else
1038 rn = rn_delete(&ke->pfrke_sa, NULL, head);
1039
1040 if (rn == NULL) {
1041 printf("pfr_unroute_kentry: delete failed.\n");
1042 return (-1);
1043 }
1044 return (0);
1045 }
1046
1047 static void
1048 pfr_copyout_addr(struct pfr_addr *ad, struct pfr_kentry *ke)
1049 {
1050 bzero(ad, sizeof (*ad));
1051 if (ke == NULL)
1052 return;
1053 ad->pfra_af = ke->pfrke_af;
1054 ad->pfra_net = ke->pfrke_net;
1055 ad->pfra_not = ke->pfrke_not;
1056 if (ad->pfra_af == AF_INET)
1057 ad->pfra_ip4addr = ke->pfrke_sa.sin.sin_addr;
1058 else if (ad->pfra_af == AF_INET6)
1059 ad->pfra_ip6addr = ke->pfrke_sa.sin6.sin6_addr;
1060 }
1061
1062 static int
1063 pfr_walktree(struct radix_node *rn, void *arg)
1064 {
1065 struct pfr_kentry *ke = (struct pfr_kentry *)rn;
1066 struct pfr_walktree *w = arg;
1067 int flags = w->pfrw_flags;
1068
1069 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1070
1071 switch (w->pfrw_op) {
1072 case PFRW_MARK:
1073 ke->pfrke_mark = 0;
1074 break;
1075 case PFRW_SWEEP:
1076 if (ke->pfrke_mark)
1077 break;
1078 /* FALLTHROUGH */
1079 case PFRW_ENQUEUE:
1080 SLIST_INSERT_HEAD(w->pfrw_workq, ke, pfrke_workq);
1081 w->pfrw_cnt++;
1082 break;
1083 case PFRW_GET_ADDRS:
1084 if (w->pfrw_free-- > 0) {
1085 struct pfr_addr ad;
1086
1087 pfr_copyout_addr(&ad, ke);
1088 if (copyout(&ad, w->pfrw_addr, sizeof (ad)))
1089 return (EFAULT);
1090 w->pfrw_addr += sizeof (ad);
1091 }
1092 break;
1093 case PFRW_GET_ASTATS:
1094 if (w->pfrw_free-- > 0) {
1095 struct pfr_astats as;
1096
1097 pfr_copyout_addr(&as.pfras_a, ke);
1098
1099 bcopy(ke->pfrke_packets, as.pfras_packets,
1100 sizeof (as.pfras_packets));
1101 bcopy(ke->pfrke_bytes, as.pfras_bytes,
1102 sizeof (as.pfras_bytes));
1103 as.pfras_tzero = ke->pfrke_tzero;
1104
1105 if (COPYOUT(&as, w->pfrw_astats, sizeof (as), flags))
1106 return (EFAULT);
1107 w->pfrw_astats += sizeof (as);
1108 }
1109 break;
1110 case PFRW_POOL_GET:
1111 if (ke->pfrke_not)
1112 break; /* negative entries are ignored */
1113 if (!w->pfrw_cnt--) {
1114 w->pfrw_kentry = ke;
1115 return (1); /* finish search */
1116 }
1117 break;
1118 case PFRW_DYNADDR_UPDATE:
1119 if (ke->pfrke_af == AF_INET) {
1120 if (w->pfrw_dyn->pfid_acnt4++ > 0)
1121 break;
1122 pfr_prepare_network(&pfr_mask, AF_INET, ke->pfrke_net);
1123 w->pfrw_dyn->pfid_addr4 = *SUNION2PF(
1124 &ke->pfrke_sa, AF_INET);
1125 w->pfrw_dyn->pfid_mask4 = *SUNION2PF(
1126 &pfr_mask, AF_INET);
1127 } else if (ke->pfrke_af == AF_INET6) {
1128 if (w->pfrw_dyn->pfid_acnt6++ > 0)
1129 break;
1130 pfr_prepare_network(&pfr_mask, AF_INET6, ke->pfrke_net);
1131 w->pfrw_dyn->pfid_addr6 = *SUNION2PF(
1132 &ke->pfrke_sa, AF_INET6);
1133 w->pfrw_dyn->pfid_mask6 = *SUNION2PF(
1134 &pfr_mask, AF_INET6);
1135 }
1136 break;
1137 }
1138 return (0);
1139 }
1140
1141 int
1142 pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
1143 {
1144 struct pfr_ktableworkq workq;
1145 struct pfr_ktable *p;
1146 int xdel = 0;
1147
1148 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1149
1150 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
1151 PFR_FLAG_ALLRSETS);
1152 if (pfr_fix_anchor(filter->pfrt_anchor))
1153 return (EINVAL);
1154 if (pfr_table_count(filter, flags) < 0)
1155 return (ENOENT);
1156
1157 SLIST_INIT(&workq);
1158 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1159 if (pfr_skip_table(filter, p, flags))
1160 continue;
1161 if (strcmp(p->pfrkt_anchor, PF_RESERVED_ANCHOR) == 0)
1162 continue;
1163 if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE))
1164 continue;
1165 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
1166 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1167 xdel++;
1168 }
1169 if (!(flags & PFR_FLAG_DUMMY)) {
1170 pfr_setflags_ktables(&workq);
1171 }
1172 if (ndel != NULL)
1173 *ndel = xdel;
1174 return (0);
1175 }
1176
1177 int
1178 pfr_add_tables(user_addr_t tbl, int size, int *nadd, int flags)
1179 {
1180 struct pfr_ktableworkq addq, changeq;
1181 struct pfr_ktable *p, *q, *r, key;
1182 int i, rv, xadd = 0;
1183 u_int64_t tzero = pf_calendar_time_second();
1184
1185 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1186
1187 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1188 SLIST_INIT(&addq);
1189 SLIST_INIT(&changeq);
1190 for (i = 0; i < size; i++, tbl += sizeof (key.pfrkt_t)) {
1191 if (COPYIN(tbl, &key.pfrkt_t, sizeof (key.pfrkt_t), flags))
1192 senderr(EFAULT);
1193 if (pfr_validate_table(&key.pfrkt_t, PFR_TFLAG_USRMASK,
1194 flags & PFR_FLAG_USERIOCTL))
1195 senderr(EINVAL);
1196 key.pfrkt_flags |= PFR_TFLAG_ACTIVE;
1197 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1198 if (p == NULL) {
1199 p = pfr_create_ktable(&key.pfrkt_t, tzero, 1);
1200 if (p == NULL)
1201 senderr(ENOMEM);
1202 SLIST_FOREACH(q, &addq, pfrkt_workq) {
1203 if (!pfr_ktable_compare(p, q))
1204 goto _skip;
1205 }
1206 SLIST_INSERT_HEAD(&addq, p, pfrkt_workq);
1207 xadd++;
1208 if (!key.pfrkt_anchor[0])
1209 goto _skip;
1210
1211 /* find or create root table */
1212 bzero(key.pfrkt_anchor, sizeof (key.pfrkt_anchor));
1213 r = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1214 if (r != NULL) {
1215 p->pfrkt_root = r;
1216 goto _skip;
1217 }
1218 SLIST_FOREACH(q, &addq, pfrkt_workq) {
1219 if (!pfr_ktable_compare(&key, q)) {
1220 p->pfrkt_root = q;
1221 goto _skip;
1222 }
1223 }
1224 key.pfrkt_flags = 0;
1225 r = pfr_create_ktable(&key.pfrkt_t, 0, 1);
1226 if (r == NULL)
1227 senderr(ENOMEM);
1228 SLIST_INSERT_HEAD(&addq, r, pfrkt_workq);
1229 p->pfrkt_root = r;
1230 } else if (!(p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1231 SLIST_FOREACH(q, &changeq, pfrkt_workq)
1232 if (!pfr_ktable_compare(&key, q))
1233 goto _skip;
1234 p->pfrkt_nflags = (p->pfrkt_flags &
1235 ~PFR_TFLAG_USRMASK) | key.pfrkt_flags;
1236 SLIST_INSERT_HEAD(&changeq, p, pfrkt_workq);
1237 xadd++;
1238 }
1239 _skip:
1240 ;
1241 }
1242 if (!(flags & PFR_FLAG_DUMMY)) {
1243 pfr_insert_ktables(&addq);
1244 pfr_setflags_ktables(&changeq);
1245 } else
1246 pfr_destroy_ktables(&addq, 0);
1247 if (nadd != NULL)
1248 *nadd = xadd;
1249 return (0);
1250 _bad:
1251 pfr_destroy_ktables(&addq, 0);
1252 return (rv);
1253 }
1254
1255 int
1256 pfr_del_tables(user_addr_t tbl, int size, int *ndel, int flags)
1257 {
1258 struct pfr_ktableworkq workq;
1259 struct pfr_ktable *p, *q, key;
1260 int i, xdel = 0;
1261
1262 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1263
1264 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1265 SLIST_INIT(&workq);
1266 for (i = 0; i < size; i++, tbl += sizeof (key.pfrkt_t)) {
1267 if (COPYIN(tbl, &key.pfrkt_t, sizeof (key.pfrkt_t), flags))
1268 return (EFAULT);
1269 if (pfr_validate_table(&key.pfrkt_t, 0,
1270 flags & PFR_FLAG_USERIOCTL))
1271 return (EINVAL);
1272 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1273 if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1274 SLIST_FOREACH(q, &workq, pfrkt_workq)
1275 if (!pfr_ktable_compare(p, q))
1276 goto _skip;
1277 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_ACTIVE;
1278 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1279 xdel++;
1280 }
1281 _skip:
1282 ;
1283 }
1284
1285 if (!(flags & PFR_FLAG_DUMMY)) {
1286 pfr_setflags_ktables(&workq);
1287 }
1288 if (ndel != NULL)
1289 *ndel = xdel;
1290 return (0);
1291 }
1292
1293 int
1294 pfr_get_tables(struct pfr_table *filter, user_addr_t tbl, int *size,
1295 int flags)
1296 {
1297 struct pfr_ktable *p;
1298 int n, nn;
1299
1300 ACCEPT_FLAGS(flags, PFR_FLAG_ALLRSETS);
1301 if (pfr_fix_anchor(filter->pfrt_anchor))
1302 return (EINVAL);
1303 n = nn = pfr_table_count(filter, flags);
1304 if (n < 0)
1305 return (ENOENT);
1306 if (n > *size) {
1307 *size = n;
1308 return (0);
1309 }
1310 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1311 if (pfr_skip_table(filter, p, flags))
1312 continue;
1313 if (n-- <= 0)
1314 continue;
1315 if (COPYOUT(&p->pfrkt_t, tbl, sizeof (p->pfrkt_t), flags))
1316 return (EFAULT);
1317 tbl += sizeof (p->pfrkt_t);
1318 }
1319 if (n) {
1320 printf("pfr_get_tables: corruption detected (%d).\n", n);
1321 return (ENOTTY);
1322 }
1323 *size = nn;
1324 return (0);
1325 }
1326
1327 int
1328 pfr_get_tstats(struct pfr_table *filter, user_addr_t tbl, int *size,
1329 int flags)
1330 {
1331 struct pfr_ktable *p;
1332 struct pfr_ktableworkq workq;
1333 int n, nn;
1334 u_int64_t tzero = pf_calendar_time_second();
1335
1336 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1337
1338 /* XXX PFR_FLAG_CLSTATS disabled */
1339 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_ALLRSETS);
1340 if (pfr_fix_anchor(filter->pfrt_anchor))
1341 return (EINVAL);
1342 n = nn = pfr_table_count(filter, flags);
1343 if (n < 0)
1344 return (ENOENT);
1345 if (n > *size) {
1346 *size = n;
1347 return (0);
1348 }
1349 SLIST_INIT(&workq);
1350 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1351 if (pfr_skip_table(filter, p, flags))
1352 continue;
1353 if (n-- <= 0)
1354 continue;
1355 if (COPYOUT(&p->pfrkt_ts, tbl, sizeof (p->pfrkt_ts), flags)) {
1356 return (EFAULT);
1357 }
1358 tbl += sizeof (p->pfrkt_ts);
1359 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1360 }
1361 if (flags & PFR_FLAG_CLSTATS)
1362 pfr_clstats_ktables(&workq, tzero,
1363 flags & PFR_FLAG_ADDRSTOO);
1364 if (n) {
1365 printf("pfr_get_tstats: corruption detected (%d).\n", n);
1366 return (ENOTTY);
1367 }
1368 *size = nn;
1369 return (0);
1370 }
1371
1372 int
1373 pfr_clr_tstats(user_addr_t tbl, int size, int *nzero, int flags)
1374 {
1375 struct pfr_ktableworkq workq;
1376 struct pfr_ktable *p, key;
1377 int i, xzero = 0;
1378 u_int64_t tzero = pf_calendar_time_second();
1379
1380 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1381
1382 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY |
1383 PFR_FLAG_ADDRSTOO);
1384 SLIST_INIT(&workq);
1385 for (i = 0; i < size; i++, tbl += sizeof (key.pfrkt_t)) {
1386 if (COPYIN(tbl, &key.pfrkt_t, sizeof (key.pfrkt_t), flags))
1387 return (EFAULT);
1388 if (pfr_validate_table(&key.pfrkt_t, 0, 0))
1389 return (EINVAL);
1390 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1391 if (p != NULL) {
1392 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1393 xzero++;
1394 }
1395 }
1396 if (!(flags & PFR_FLAG_DUMMY)) {
1397 pfr_clstats_ktables(&workq, tzero, flags & PFR_FLAG_ADDRSTOO);
1398 }
1399 if (nzero != NULL)
1400 *nzero = xzero;
1401 return (0);
1402 }
1403
1404 int
1405 pfr_set_tflags(user_addr_t tbl, int size, int setflag, int clrflag,
1406 int *nchange, int *ndel, int flags)
1407 {
1408 struct pfr_ktableworkq workq;
1409 struct pfr_ktable *p, *q, key;
1410 int i, xchange = 0, xdel = 0;
1411
1412 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1413
1414 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1415 if ((setflag & ~PFR_TFLAG_USRMASK) ||
1416 (clrflag & ~PFR_TFLAG_USRMASK) ||
1417 (setflag & clrflag))
1418 return (EINVAL);
1419 SLIST_INIT(&workq);
1420 for (i = 0; i < size; i++, tbl += sizeof (key.pfrkt_t)) {
1421 if (COPYIN(tbl, &key.pfrkt_t, sizeof (key.pfrkt_t), flags))
1422 return (EFAULT);
1423 if (pfr_validate_table(&key.pfrkt_t, 0,
1424 flags & PFR_FLAG_USERIOCTL))
1425 return (EINVAL);
1426 p = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1427 if (p != NULL && (p->pfrkt_flags & PFR_TFLAG_ACTIVE)) {
1428 p->pfrkt_nflags = (p->pfrkt_flags | setflag) &
1429 ~clrflag;
1430 if (p->pfrkt_nflags == p->pfrkt_flags)
1431 goto _skip;
1432 SLIST_FOREACH(q, &workq, pfrkt_workq)
1433 if (!pfr_ktable_compare(p, q))
1434 goto _skip;
1435 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1436 if ((p->pfrkt_flags & PFR_TFLAG_PERSIST) &&
1437 (clrflag & PFR_TFLAG_PERSIST) &&
1438 !(p->pfrkt_flags & PFR_TFLAG_REFERENCED))
1439 xdel++;
1440 else
1441 xchange++;
1442 }
1443 _skip:
1444 ;
1445 }
1446 if (!(flags & PFR_FLAG_DUMMY)) {
1447 pfr_setflags_ktables(&workq);
1448 }
1449 if (nchange != NULL)
1450 *nchange = xchange;
1451 if (ndel != NULL)
1452 *ndel = xdel;
1453 return (0);
1454 }
1455
1456 int
1457 pfr_ina_begin(struct pfr_table *trs, u_int32_t *ticket, int *ndel, int flags)
1458 {
1459 struct pfr_ktableworkq workq;
1460 struct pfr_ktable *p;
1461 struct pf_ruleset *rs;
1462 int xdel = 0;
1463
1464 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1465
1466 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1467 rs = pf_find_or_create_ruleset(trs->pfrt_anchor);
1468 if (rs == NULL)
1469 return (ENOMEM);
1470 SLIST_INIT(&workq);
1471 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1472 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1473 pfr_skip_table(trs, p, 0))
1474 continue;
1475 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
1476 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1477 xdel++;
1478 }
1479 if (!(flags & PFR_FLAG_DUMMY)) {
1480 pfr_setflags_ktables(&workq);
1481 if (ticket != NULL)
1482 *ticket = ++rs->tticket;
1483 rs->topen = 1;
1484 } else
1485 pf_remove_if_empty_ruleset(rs);
1486 if (ndel != NULL)
1487 *ndel = xdel;
1488 return (0);
1489 }
1490
1491 int
1492 pfr_ina_define(struct pfr_table *tbl, user_addr_t addr, int size,
1493 int *nadd, int *naddr, u_int32_t ticket, int flags)
1494 {
1495 struct pfr_ktableworkq tableq;
1496 struct pfr_kentryworkq addrq;
1497 struct pfr_ktable *kt, *rt, *shadow, key;
1498 struct pfr_kentry *p;
1499 struct pfr_addr ad;
1500 struct pf_ruleset *rs;
1501 int i, rv, xadd = 0, xaddr = 0;
1502
1503 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1504
1505 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY | PFR_FLAG_ADDRSTOO);
1506 if (size && !(flags & PFR_FLAG_ADDRSTOO))
1507 return (EINVAL);
1508 if (pfr_validate_table(tbl, PFR_TFLAG_USRMASK,
1509 flags & PFR_FLAG_USERIOCTL))
1510 return (EINVAL);
1511 rs = pf_find_ruleset(tbl->pfrt_anchor);
1512 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1513 return (EBUSY);
1514 tbl->pfrt_flags |= PFR_TFLAG_INACTIVE;
1515 SLIST_INIT(&tableq);
1516 kt = RB_FIND(pfr_ktablehead, &pfr_ktables, (struct pfr_ktable *)tbl);
1517 if (kt == NULL) {
1518 kt = pfr_create_ktable(tbl, 0, 1);
1519 if (kt == NULL)
1520 return (ENOMEM);
1521 SLIST_INSERT_HEAD(&tableq, kt, pfrkt_workq);
1522 xadd++;
1523 if (!tbl->pfrt_anchor[0])
1524 goto _skip;
1525
1526 /* find or create root table */
1527 bzero(&key, sizeof (key));
1528 strlcpy(key.pfrkt_name, tbl->pfrt_name,
1529 sizeof (key.pfrkt_name));
1530 rt = RB_FIND(pfr_ktablehead, &pfr_ktables, &key);
1531 if (rt != NULL) {
1532 kt->pfrkt_root = rt;
1533 goto _skip;
1534 }
1535 rt = pfr_create_ktable(&key.pfrkt_t, 0, 1);
1536 if (rt == NULL) {
1537 pfr_destroy_ktables(&tableq, 0);
1538 return (ENOMEM);
1539 }
1540 SLIST_INSERT_HEAD(&tableq, rt, pfrkt_workq);
1541 kt->pfrkt_root = rt;
1542 } else if (!(kt->pfrkt_flags & PFR_TFLAG_INACTIVE))
1543 xadd++;
1544 _skip:
1545 shadow = pfr_create_ktable(tbl, 0, 0);
1546 if (shadow == NULL) {
1547 pfr_destroy_ktables(&tableq, 0);
1548 return (ENOMEM);
1549 }
1550 SLIST_INIT(&addrq);
1551 for (i = 0; i < size; i++, addr += sizeof (ad)) {
1552 if (COPYIN(addr, &ad, sizeof (ad), flags))
1553 senderr(EFAULT);
1554 if (pfr_validate_addr(&ad))
1555 senderr(EINVAL);
1556 if (pfr_lookup_addr(shadow, &ad, 1) != NULL)
1557 continue;
1558 p = pfr_create_kentry(&ad, 0);
1559 if (p == NULL)
1560 senderr(ENOMEM);
1561 if (pfr_route_kentry(shadow, p)) {
1562 pfr_destroy_kentry(p);
1563 continue;
1564 }
1565 SLIST_INSERT_HEAD(&addrq, p, pfrke_workq);
1566 xaddr++;
1567 }
1568 if (!(flags & PFR_FLAG_DUMMY)) {
1569 if (kt->pfrkt_shadow != NULL)
1570 pfr_destroy_ktable(kt->pfrkt_shadow, 1);
1571 kt->pfrkt_flags |= PFR_TFLAG_INACTIVE;
1572 pfr_insert_ktables(&tableq);
1573 shadow->pfrkt_cnt = (flags & PFR_FLAG_ADDRSTOO) ?
1574 xaddr : NO_ADDRESSES;
1575 kt->pfrkt_shadow = shadow;
1576 } else {
1577 pfr_clean_node_mask(shadow, &addrq);
1578 pfr_destroy_ktable(shadow, 0);
1579 pfr_destroy_ktables(&tableq, 0);
1580 pfr_destroy_kentries(&addrq);
1581 }
1582 if (nadd != NULL)
1583 *nadd = xadd;
1584 if (naddr != NULL)
1585 *naddr = xaddr;
1586 return (0);
1587 _bad:
1588 pfr_destroy_ktable(shadow, 0);
1589 pfr_destroy_ktables(&tableq, 0);
1590 pfr_destroy_kentries(&addrq);
1591 return (rv);
1592 }
1593
1594 int
1595 pfr_ina_rollback(struct pfr_table *trs, u_int32_t ticket, int *ndel, int flags)
1596 {
1597 struct pfr_ktableworkq workq;
1598 struct pfr_ktable *p;
1599 struct pf_ruleset *rs;
1600 int xdel = 0;
1601
1602 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1603
1604 ACCEPT_FLAGS(flags, PFR_FLAG_DUMMY);
1605 rs = pf_find_ruleset(trs->pfrt_anchor);
1606 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1607 return (0);
1608 SLIST_INIT(&workq);
1609 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1610 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1611 pfr_skip_table(trs, p, 0))
1612 continue;
1613 p->pfrkt_nflags = p->pfrkt_flags & ~PFR_TFLAG_INACTIVE;
1614 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1615 xdel++;
1616 }
1617 if (!(flags & PFR_FLAG_DUMMY)) {
1618 pfr_setflags_ktables(&workq);
1619 rs->topen = 0;
1620 pf_remove_if_empty_ruleset(rs);
1621 }
1622 if (ndel != NULL)
1623 *ndel = xdel;
1624 return (0);
1625 }
1626
1627 int
1628 pfr_ina_commit(struct pfr_table *trs, u_int32_t ticket, int *nadd,
1629 int *nchange, int flags)
1630 {
1631 struct pfr_ktable *p, *q;
1632 struct pfr_ktableworkq workq;
1633 struct pf_ruleset *rs;
1634 int xadd = 0, xchange = 0;
1635 u_int64_t tzero = pf_calendar_time_second();
1636
1637 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1638
1639 ACCEPT_FLAGS(flags, PFR_FLAG_ATOMIC | PFR_FLAG_DUMMY);
1640 rs = pf_find_ruleset(trs->pfrt_anchor);
1641 if (rs == NULL || !rs->topen || ticket != rs->tticket)
1642 return (EBUSY);
1643
1644 SLIST_INIT(&workq);
1645 RB_FOREACH(p, pfr_ktablehead, &pfr_ktables) {
1646 if (!(p->pfrkt_flags & PFR_TFLAG_INACTIVE) ||
1647 pfr_skip_table(trs, p, 0))
1648 continue;
1649 SLIST_INSERT_HEAD(&workq, p, pfrkt_workq);
1650 if (p->pfrkt_flags & PFR_TFLAG_ACTIVE)
1651 xchange++;
1652 else
1653 xadd++;
1654 }
1655
1656 if (!(flags & PFR_FLAG_DUMMY)) {
1657 for (p = SLIST_FIRST(&workq); p != NULL; p = q) {
1658 q = SLIST_NEXT(p, pfrkt_workq);
1659 pfr_commit_ktable(p, tzero);
1660 }
1661 rs->topen = 0;
1662 pf_remove_if_empty_ruleset(rs);
1663 }
1664 if (nadd != NULL)
1665 *nadd = xadd;
1666 if (nchange != NULL)
1667 *nchange = xchange;
1668
1669 return (0);
1670 }
1671
1672 static void
1673 pfr_commit_ktable(struct pfr_ktable *kt, u_int64_t tzero)
1674 {
1675 struct pfr_ktable *shadow = kt->pfrkt_shadow;
1676 int nflags;
1677
1678 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1679
1680 if (shadow->pfrkt_cnt == NO_ADDRESSES) {
1681 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
1682 pfr_clstats_ktable(kt, tzero, 1);
1683 } else if (kt->pfrkt_flags & PFR_TFLAG_ACTIVE) {
1684 /* kt might contain addresses */
1685 struct pfr_kentryworkq addrq, addq, changeq, delq, garbageq;
1686 struct pfr_kentry *p, *q, *next;
1687 struct pfr_addr ad;
1688
1689 pfr_enqueue_addrs(shadow, &addrq, NULL, 0);
1690 pfr_mark_addrs(kt);
1691 SLIST_INIT(&addq);
1692 SLIST_INIT(&changeq);
1693 SLIST_INIT(&delq);
1694 SLIST_INIT(&garbageq);
1695 pfr_clean_node_mask(shadow, &addrq);
1696 for (p = SLIST_FIRST(&addrq); p != NULL; p = next) {
1697 next = SLIST_NEXT(p, pfrke_workq); /* XXX */
1698 pfr_copyout_addr(&ad, p);
1699 q = pfr_lookup_addr(kt, &ad, 1);
1700 if (q != NULL) {
1701 if (q->pfrke_not != p->pfrke_not)
1702 SLIST_INSERT_HEAD(&changeq, q,
1703 pfrke_workq);
1704 q->pfrke_mark = 1;
1705 SLIST_INSERT_HEAD(&garbageq, p, pfrke_workq);
1706 } else {
1707 p->pfrke_tzero = tzero;
1708 SLIST_INSERT_HEAD(&addq, p, pfrke_workq);
1709 }
1710 }
1711 pfr_enqueue_addrs(kt, &delq, NULL, ENQUEUE_UNMARKED_ONLY);
1712 pfr_insert_kentries(kt, &addq, tzero);
1713 pfr_remove_kentries(kt, &delq);
1714 pfr_clstats_kentries(&changeq, tzero, INVERT_NEG_FLAG);
1715 pfr_destroy_kentries(&garbageq);
1716 } else {
1717 /* kt cannot contain addresses */
1718 SWAP(struct radix_node_head *, kt->pfrkt_ip4,
1719 shadow->pfrkt_ip4);
1720 SWAP(struct radix_node_head *, kt->pfrkt_ip6,
1721 shadow->pfrkt_ip6);
1722 SWAP(int, kt->pfrkt_cnt, shadow->pfrkt_cnt);
1723 pfr_clstats_ktable(kt, tzero, 1);
1724 }
1725 nflags = ((shadow->pfrkt_flags & PFR_TFLAG_USRMASK) |
1726 (kt->pfrkt_flags & PFR_TFLAG_SETMASK) | PFR_TFLAG_ACTIVE) &
1727 ~PFR_TFLAG_INACTIVE;
1728 pfr_destroy_ktable(shadow, 0);
1729 kt->pfrkt_shadow = NULL;
1730 pfr_setflags_ktable(kt, nflags);
1731 }
1732
1733 static int
1734 pfr_validate_table(struct pfr_table *tbl, int allowedflags, int no_reserved)
1735 {
1736 int i;
1737
1738 if (!tbl->pfrt_name[0])
1739 return (-1);
1740 if (no_reserved && strcmp(tbl->pfrt_anchor, PF_RESERVED_ANCHOR) == 0)
1741 return (-1);
1742 if (tbl->pfrt_name[PF_TABLE_NAME_SIZE-1])
1743 return (-1);
1744 for (i = strlen(tbl->pfrt_name); i < PF_TABLE_NAME_SIZE; i++)
1745 if (tbl->pfrt_name[i])
1746 return (-1);
1747 if (pfr_fix_anchor(tbl->pfrt_anchor))
1748 return (-1);
1749 if (tbl->pfrt_flags & ~allowedflags)
1750 return (-1);
1751 return (0);
1752 }
1753
1754 /*
1755 * Rewrite anchors referenced by tables to remove slashes
1756 * and check for validity.
1757 */
1758 static int
1759 pfr_fix_anchor(char *anchor)
1760 {
1761 size_t siz = MAXPATHLEN;
1762 int i;
1763
1764 if (anchor[0] == '/') {
1765 char *path;
1766 int off;
1767
1768 path = anchor;
1769 off = 1;
1770 while (*++path == '/')
1771 off++;
1772 bcopy(path, anchor, siz - off);
1773 memset(anchor + siz - off, 0, off);
1774 }
1775 if (anchor[siz - 1])
1776 return (-1);
1777 for (i = strlen(anchor); i < (int)siz; i++)
1778 if (anchor[i])
1779 return (-1);
1780 return (0);
1781 }
1782
1783 static int
1784 pfr_table_count(struct pfr_table *filter, int flags)
1785 {
1786 struct pf_ruleset *rs;
1787
1788 if (flags & PFR_FLAG_ALLRSETS)
1789 return (pfr_ktable_cnt);
1790 if (filter->pfrt_anchor[0]) {
1791 rs = pf_find_ruleset(filter->pfrt_anchor);
1792 return ((rs != NULL) ? rs->tables : -1);
1793 }
1794 return (pf_main_ruleset.tables);
1795 }
1796
1797 static int
1798 pfr_skip_table(struct pfr_table *filter, struct pfr_ktable *kt, int flags)
1799 {
1800 if (flags & PFR_FLAG_ALLRSETS)
1801 return (0);
1802 if (strcmp(filter->pfrt_anchor, kt->pfrkt_anchor))
1803 return (1);
1804 return (0);
1805 }
1806
1807 static void
1808 pfr_insert_ktables(struct pfr_ktableworkq *workq)
1809 {
1810 struct pfr_ktable *p;
1811
1812 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1813
1814 SLIST_FOREACH(p, workq, pfrkt_workq)
1815 pfr_insert_ktable(p);
1816 }
1817
1818 static void
1819 pfr_insert_ktable(struct pfr_ktable *kt)
1820 {
1821 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1822
1823 RB_INSERT(pfr_ktablehead, &pfr_ktables, kt);
1824 pfr_ktable_cnt++;
1825 if (kt->pfrkt_root != NULL)
1826 if (!kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR]++)
1827 pfr_setflags_ktable(kt->pfrkt_root,
1828 kt->pfrkt_root->pfrkt_flags|PFR_TFLAG_REFDANCHOR);
1829 }
1830
1831 static void
1832 pfr_setflags_ktables(struct pfr_ktableworkq *workq)
1833 {
1834 struct pfr_ktable *p, *q;
1835
1836 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1837
1838 for (p = SLIST_FIRST(workq); p; p = q) {
1839 q = SLIST_NEXT(p, pfrkt_workq);
1840 pfr_setflags_ktable(p, p->pfrkt_nflags);
1841 }
1842 }
1843
1844 static void
1845 pfr_setflags_ktable(struct pfr_ktable *kt, int newf)
1846 {
1847 struct pfr_kentryworkq addrq;
1848
1849 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1850
1851 if (!(newf & PFR_TFLAG_REFERENCED) &&
1852 !(newf & PFR_TFLAG_PERSIST))
1853 newf &= ~PFR_TFLAG_ACTIVE;
1854 if (!(newf & PFR_TFLAG_ACTIVE))
1855 newf &= ~PFR_TFLAG_USRMASK;
1856 if (!(newf & PFR_TFLAG_SETMASK)) {
1857 RB_REMOVE(pfr_ktablehead, &pfr_ktables, kt);
1858 if (kt->pfrkt_root != NULL)
1859 if (!--kt->pfrkt_root->pfrkt_refcnt[PFR_REFCNT_ANCHOR])
1860 pfr_setflags_ktable(kt->pfrkt_root,
1861 kt->pfrkt_root->pfrkt_flags &
1862 ~PFR_TFLAG_REFDANCHOR);
1863 pfr_destroy_ktable(kt, 1);
1864 pfr_ktable_cnt--;
1865 return;
1866 }
1867 if (!(newf & PFR_TFLAG_ACTIVE) && kt->pfrkt_cnt) {
1868 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
1869 pfr_remove_kentries(kt, &addrq);
1870 }
1871 if (!(newf & PFR_TFLAG_INACTIVE) && kt->pfrkt_shadow != NULL) {
1872 pfr_destroy_ktable(kt->pfrkt_shadow, 1);
1873 kt->pfrkt_shadow = NULL;
1874 }
1875 kt->pfrkt_flags = newf;
1876 }
1877
1878 static void
1879 pfr_clstats_ktables(struct pfr_ktableworkq *workq, u_int64_t tzero, int recurse)
1880 {
1881 struct pfr_ktable *p;
1882
1883 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1884
1885 SLIST_FOREACH(p, workq, pfrkt_workq)
1886 pfr_clstats_ktable(p, tzero, recurse);
1887 }
1888
1889 static void
1890 pfr_clstats_ktable(struct pfr_ktable *kt, u_int64_t tzero, int recurse)
1891 {
1892 struct pfr_kentryworkq addrq;
1893
1894 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1895
1896 if (recurse) {
1897 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
1898 pfr_clstats_kentries(&addrq, tzero, 0);
1899 }
1900 bzero(kt->pfrkt_packets, sizeof (kt->pfrkt_packets));
1901 bzero(kt->pfrkt_bytes, sizeof (kt->pfrkt_bytes));
1902 kt->pfrkt_match = kt->pfrkt_nomatch = 0;
1903 kt->pfrkt_tzero = tzero;
1904 }
1905
1906 static struct pfr_ktable *
1907 pfr_create_ktable(struct pfr_table *tbl, u_int64_t tzero, int attachruleset)
1908 {
1909 struct pfr_ktable *kt;
1910 struct pf_ruleset *rs;
1911
1912 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1913
1914 kt = pool_get(&pfr_ktable_pl, PR_WAITOK);
1915 if (kt == NULL)
1916 return (NULL);
1917 bzero(kt, sizeof (*kt));
1918 kt->pfrkt_t = *tbl;
1919
1920 if (attachruleset) {
1921 rs = pf_find_or_create_ruleset(tbl->pfrt_anchor);
1922 if (!rs) {
1923 pfr_destroy_ktable(kt, 0);
1924 return (NULL);
1925 }
1926 kt->pfrkt_rs = rs;
1927 rs->tables++;
1928 }
1929
1930 if (!rn_inithead((void **)&kt->pfrkt_ip4,
1931 offsetof(struct sockaddr_in, sin_addr) * 8) ||
1932 !rn_inithead((void **)&kt->pfrkt_ip6,
1933 offsetof(struct sockaddr_in6, sin6_addr) * 8)) {
1934 pfr_destroy_ktable(kt, 0);
1935 return (NULL);
1936 }
1937 kt->pfrkt_tzero = tzero;
1938
1939 return (kt);
1940 }
1941
1942 static void
1943 pfr_destroy_ktables(struct pfr_ktableworkq *workq, int flushaddr)
1944 {
1945 struct pfr_ktable *p, *q;
1946
1947 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1948
1949 for (p = SLIST_FIRST(workq); p; p = q) {
1950 q = SLIST_NEXT(p, pfrkt_workq);
1951 pfr_destroy_ktable(p, flushaddr);
1952 }
1953 }
1954
1955 static void
1956 pfr_destroy_ktable(struct pfr_ktable *kt, int flushaddr)
1957 {
1958 struct pfr_kentryworkq addrq;
1959
1960 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1961
1962 if (flushaddr) {
1963 pfr_enqueue_addrs(kt, &addrq, NULL, 0);
1964 pfr_clean_node_mask(kt, &addrq);
1965 pfr_destroy_kentries(&addrq);
1966 }
1967 if (kt->pfrkt_ip4 != NULL)
1968 _FREE((caddr_t)kt->pfrkt_ip4, M_RTABLE);
1969 if (kt->pfrkt_ip6 != NULL)
1970 _FREE((caddr_t)kt->pfrkt_ip6, M_RTABLE);
1971 if (kt->pfrkt_shadow != NULL)
1972 pfr_destroy_ktable(kt->pfrkt_shadow, flushaddr);
1973 if (kt->pfrkt_rs != NULL) {
1974 kt->pfrkt_rs->tables--;
1975 pf_remove_if_empty_ruleset(kt->pfrkt_rs);
1976 }
1977 pool_put(&pfr_ktable_pl, kt);
1978 }
1979
1980 static int
1981 pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q)
1982 {
1983 int d;
1984
1985 if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE)))
1986 return (d);
1987 return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor));
1988 }
1989
1990 static struct pfr_ktable *
1991 pfr_lookup_table(struct pfr_table *tbl)
1992 {
1993 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
1994
1995 /* struct pfr_ktable start like a struct pfr_table */
1996 return (RB_FIND(pfr_ktablehead, &pfr_ktables,
1997 (struct pfr_ktable *)tbl));
1998 }
1999
2000 int
2001 pfr_match_addr(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af)
2002 {
2003 struct pfr_kentry *ke = NULL;
2004 int match;
2005
2006 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2007
2008 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2009 kt = kt->pfrkt_root;
2010 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2011 return (0);
2012
2013 switch (af) {
2014 #if INET
2015 case AF_INET:
2016 pfr_sin.sin_addr.s_addr = a->addr32[0];
2017 ke = (struct pfr_kentry *)rn_match(&pfr_sin, kt->pfrkt_ip4);
2018 if (ke && KENTRY_RNF_ROOT(ke))
2019 ke = NULL;
2020 break;
2021 #endif /* INET */
2022 #if INET6
2023 case AF_INET6:
2024 bcopy(a, &pfr_sin6.sin6_addr, sizeof (pfr_sin6.sin6_addr));
2025 ke = (struct pfr_kentry *)rn_match(&pfr_sin6, kt->pfrkt_ip6);
2026 if (ke && KENTRY_RNF_ROOT(ke))
2027 ke = NULL;
2028 break;
2029 #endif /* INET6 */
2030 }
2031 match = (ke && !ke->pfrke_not);
2032 if (match)
2033 kt->pfrkt_match++;
2034 else
2035 kt->pfrkt_nomatch++;
2036 return (match);
2037 }
2038
2039 void
2040 pfr_update_stats(struct pfr_ktable *kt, struct pf_addr *a, sa_family_t af,
2041 u_int64_t len, int dir_out, int op_pass, int notrule)
2042 {
2043 struct pfr_kentry *ke = NULL;
2044
2045 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2046
2047 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2048 kt = kt->pfrkt_root;
2049 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2050 return;
2051
2052 switch (af) {
2053 #if INET
2054 case AF_INET:
2055 pfr_sin.sin_addr.s_addr = a->addr32[0];
2056 ke = (struct pfr_kentry *)rn_match(&pfr_sin, kt->pfrkt_ip4);
2057 if (ke && KENTRY_RNF_ROOT(ke))
2058 ke = NULL;
2059 break;
2060 #endif /* INET */
2061 #if INET6
2062 case AF_INET6:
2063 bcopy(a, &pfr_sin6.sin6_addr, sizeof (pfr_sin6.sin6_addr));
2064 ke = (struct pfr_kentry *)rn_match(&pfr_sin6, kt->pfrkt_ip6);
2065 if (ke && KENTRY_RNF_ROOT(ke))
2066 ke = NULL;
2067 break;
2068 #endif /* INET6 */
2069 default:
2070 ;
2071 }
2072 if ((ke == NULL || ke->pfrke_not) != notrule) {
2073 if (op_pass != PFR_OP_PASS)
2074 printf("pfr_update_stats: assertion failed.\n");
2075 op_pass = PFR_OP_XPASS;
2076 }
2077 kt->pfrkt_packets[dir_out][op_pass]++;
2078 kt->pfrkt_bytes[dir_out][op_pass] += len;
2079 if (ke != NULL && op_pass != PFR_OP_XPASS) {
2080 ke->pfrke_packets[dir_out][op_pass]++;
2081 ke->pfrke_bytes[dir_out][op_pass] += len;
2082 }
2083 }
2084
2085 struct pfr_ktable *
2086 pfr_attach_table(struct pf_ruleset *rs, char *name)
2087 {
2088 struct pfr_ktable *kt, *rt;
2089 struct pfr_table tbl;
2090 struct pf_anchor *ac = rs->anchor;
2091
2092 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2093
2094 bzero(&tbl, sizeof (tbl));
2095 strlcpy(tbl.pfrt_name, name, sizeof (tbl.pfrt_name));
2096 if (ac != NULL)
2097 strlcpy(tbl.pfrt_anchor, ac->path, sizeof (tbl.pfrt_anchor));
2098 kt = pfr_lookup_table(&tbl);
2099 if (kt == NULL) {
2100 kt = pfr_create_ktable(&tbl, pf_calendar_time_second(), 1);
2101 if (kt == NULL)
2102 return (NULL);
2103 if (ac != NULL) {
2104 bzero(tbl.pfrt_anchor, sizeof (tbl.pfrt_anchor));
2105 rt = pfr_lookup_table(&tbl);
2106 if (rt == NULL) {
2107 rt = pfr_create_ktable(&tbl, 0, 1);
2108 if (rt == NULL) {
2109 pfr_destroy_ktable(kt, 0);
2110 return (NULL);
2111 }
2112 pfr_insert_ktable(rt);
2113 }
2114 kt->pfrkt_root = rt;
2115 }
2116 pfr_insert_ktable(kt);
2117 }
2118 if (!kt->pfrkt_refcnt[PFR_REFCNT_RULE]++)
2119 pfr_setflags_ktable(kt, kt->pfrkt_flags|PFR_TFLAG_REFERENCED);
2120 return (kt);
2121 }
2122
2123 void
2124 pfr_detach_table(struct pfr_ktable *kt)
2125 {
2126 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2127
2128 if (kt->pfrkt_refcnt[PFR_REFCNT_RULE] <= 0)
2129 printf("pfr_detach_table: refcount = %d.\n",
2130 kt->pfrkt_refcnt[PFR_REFCNT_RULE]);
2131 else if (!--kt->pfrkt_refcnt[PFR_REFCNT_RULE])
2132 pfr_setflags_ktable(kt, kt->pfrkt_flags&~PFR_TFLAG_REFERENCED);
2133 }
2134
2135 int
2136 pfr_pool_get(struct pfr_ktable *kt, int *pidx, struct pf_addr *counter,
2137 struct pf_addr **raddr, struct pf_addr **rmask, sa_family_t af)
2138 {
2139 struct pfr_kentry *ke, *ke2;
2140 struct pf_addr *addr;
2141 union sockaddr_union mask;
2142 int idx = -1, use_counter = 0;
2143
2144 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2145
2146 if (af == AF_INET)
2147 addr = (struct pf_addr *)&pfr_sin.sin_addr;
2148 else if (af == AF_INET6)
2149 addr = (struct pf_addr *)&pfr_sin6.sin6_addr;
2150 else
2151 return (-1);
2152
2153 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE) && kt->pfrkt_root != NULL)
2154 kt = kt->pfrkt_root;
2155 if (!(kt->pfrkt_flags & PFR_TFLAG_ACTIVE))
2156 return (-1);
2157
2158 if (pidx != NULL)
2159 idx = *pidx;
2160 if (counter != NULL && idx >= 0)
2161 use_counter = 1;
2162 if (idx < 0)
2163 idx = 0;
2164
2165 _next_block:
2166 ke = pfr_kentry_byidx(kt, idx, af);
2167 if (ke == NULL) {
2168 kt->pfrkt_nomatch++;
2169 return (1);
2170 }
2171 pfr_prepare_network(&pfr_mask, af, ke->pfrke_net);
2172 *raddr = SUNION2PF(&ke->pfrke_sa, af);
2173 *rmask = SUNION2PF(&pfr_mask, af);
2174
2175 if (use_counter) {
2176 /* is supplied address within block? */
2177 if (!PF_MATCHA(0, *raddr, *rmask, counter, af)) {
2178 /* no, go to next block in table */
2179 idx++;
2180 use_counter = 0;
2181 goto _next_block;
2182 }
2183 PF_ACPY(addr, counter, af);
2184 } else {
2185 /* use first address of block */
2186 PF_ACPY(addr, *raddr, af);
2187 }
2188
2189 if (!KENTRY_NETWORK(ke)) {
2190 /* this is a single IP address - no possible nested block */
2191 PF_ACPY(counter, addr, af);
2192 *pidx = idx;
2193 kt->pfrkt_match++;
2194 return (0);
2195 }
2196 for (;;) {
2197 /* we don't want to use a nested block */
2198 if (af == AF_INET)
2199 ke2 = (struct pfr_kentry *)rn_match(&pfr_sin,
2200 kt->pfrkt_ip4);
2201 else if (af == AF_INET6)
2202 ke2 = (struct pfr_kentry *)rn_match(&pfr_sin6,
2203 kt->pfrkt_ip6);
2204 else
2205 return (-1); /* never happens */
2206 /* no need to check KENTRY_RNF_ROOT() here */
2207 if (ke2 == ke) {
2208 /* lookup return the same block - perfect */
2209 PF_ACPY(counter, addr, af);
2210 *pidx = idx;
2211 kt->pfrkt_match++;
2212 return (0);
2213 }
2214
2215 /* we need to increase the counter past the nested block */
2216 pfr_prepare_network(&mask, AF_INET, ke2->pfrke_net);
2217 PF_POOLMASK(addr, addr, SUNION2PF(&mask, af), &pfr_ffaddr, af);
2218 PF_AINC(addr, af);
2219 if (!PF_MATCHA(0, *raddr, *rmask, addr, af)) {
2220 /* ok, we reached the end of our main block */
2221 /* go to next block in table */
2222 idx++;
2223 use_counter = 0;
2224 goto _next_block;
2225 }
2226 }
2227 }
2228
2229 static struct pfr_kentry *
2230 pfr_kentry_byidx(struct pfr_ktable *kt, int idx, int af)
2231 {
2232 struct pfr_walktree w;
2233
2234 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2235
2236 bzero(&w, sizeof (w));
2237 w.pfrw_op = PFRW_POOL_GET;
2238 w.pfrw_cnt = idx;
2239
2240 switch (af) {
2241 #if INET
2242 case AF_INET:
2243 (void) kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4,
2244 pfr_walktree, &w);
2245 return (w.pfrw_kentry);
2246 #endif /* INET */
2247 #if INET6
2248 case AF_INET6:
2249 (void) kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6,
2250 pfr_walktree, &w);
2251 return (w.pfrw_kentry);
2252 #endif /* INET6 */
2253 default:
2254 return (NULL);
2255 }
2256 }
2257
2258 void
2259 pfr_dynaddr_update(struct pfr_ktable *kt, struct pfi_dynaddr *dyn)
2260 {
2261 struct pfr_walktree w;
2262
2263 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
2264
2265 bzero(&w, sizeof (w));
2266 w.pfrw_op = PFRW_DYNADDR_UPDATE;
2267 w.pfrw_dyn = dyn;
2268
2269 dyn->pfid_acnt4 = 0;
2270 dyn->pfid_acnt6 = 0;
2271 if (!dyn->pfid_af || dyn->pfid_af == AF_INET)
2272 (void) kt->pfrkt_ip4->rnh_walktree(kt->pfrkt_ip4,
2273 pfr_walktree, &w);
2274 if (!dyn->pfid_af || dyn->pfid_af == AF_INET6)
2275 (void) kt->pfrkt_ip6->rnh_walktree(kt->pfrkt_ip6,
2276 pfr_walktree, &w);
2277 }