xref: /freebsd/sys/netpfil/pf/pf_if.c (revision 358c5f5c0899339a005ef2c68ef166601bb9dca9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 Daniel Hartmeier
5  * Copyright (c) 2003 Cedric Berger
6  * Copyright (c) 2005 Henning Brauer <henning@openbsd.org>
7  * Copyright (c) 2005 Ryan McBride <mcbride@openbsd.org>
8  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  *    - Redistributions of source code must retain the above copyright
16  *      notice, this list of conditions and the following disclaimer.
17  *    - Redistributions in binary form must reproduce the above
18  *      copyright notice, this list of conditions and the following
19  *      disclaimer in the documentation and/or other materials provided
20  *      with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *	$OpenBSD: pf_if.c,v 1.54 2008/06/14 16:55:28 mk Exp $
36  */
37 
38 #include <sys/cdefs.h>
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/eventhandler.h>
45 #include <sys/lock.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_private.h>
52 #include <net/vnet.h>
53 #include <net/pfvar.h>
54 #include <net/route.h>
55 
56 VNET_DEFINE(struct pfi_kkif *,	 pfi_all);
57 VNET_DEFINE_STATIC(long, pfi_update);
58 #define	V_pfi_update	VNET(pfi_update)
59 #define PFI_BUFFER_MAX	0x10000
60 
61 VNET_DECLARE(int, pf_vnet_active);
62 #define V_pf_vnet_active	VNET(pf_vnet_active)
63 
64 VNET_DEFINE_STATIC(struct pfr_addr *, pfi_buffer);
65 VNET_DEFINE_STATIC(int, pfi_buffer_cnt);
66 VNET_DEFINE_STATIC(int,	pfi_buffer_max);
67 #define	V_pfi_buffer		 VNET(pfi_buffer)
68 #define	V_pfi_buffer_cnt	 VNET(pfi_buffer_cnt)
69 #define	V_pfi_buffer_max	 VNET(pfi_buffer_max)
70 
71 #ifdef PF_WANT_32_TO_64_COUNTER
72 VNET_DEFINE(struct allkiflist_head, pf_allkiflist);
73 VNET_DEFINE(size_t, pf_allkifcount);
74 VNET_DEFINE(struct pfi_kkif *, pf_kifmarker);
75 #endif
76 
77 eventhandler_tag	 pfi_attach_cookie;
78 eventhandler_tag	 pfi_detach_cookie;
79 eventhandler_tag	 pfi_attach_group_cookie;
80 eventhandler_tag	 pfi_change_group_cookie;
81 eventhandler_tag	 pfi_detach_group_cookie;
82 eventhandler_tag	 pfi_ifaddr_event_cookie;
83 
84 static void	 pfi_attach_ifnet(struct ifnet *, struct pfi_kkif *);
85 static void	 pfi_attach_ifgroup(struct ifg_group *, struct pfi_kkif *);
86 
87 static void	 pfi_kkif_update(struct pfi_kkif *);
88 static void	 pfi_dynaddr_update(struct pfi_dynaddr *dyn);
89 static void	 pfi_table_update(struct pfr_ktable *, struct pfi_kkif *, int,
90 		    int);
91 static void	 pfi_instance_add(struct ifnet *, int, int);
92 static void	 pfi_address_add(struct sockaddr *, int, int);
93 static int	 pfi_kkif_compare(struct pfi_kkif *, struct pfi_kkif *);
94 static int	 pfi_skip_if(const char *, struct pfi_kkif *);
95 static int	 pfi_unmask(void *);
96 static void	 pfi_attach_ifnet_event(void * __unused, struct ifnet *);
97 static void	 pfi_detach_ifnet_event(void * __unused, struct ifnet *);
98 static void	 pfi_attach_group_event(void * __unused, struct ifg_group *);
99 static void	 pfi_change_group_event(void * __unused, char *);
100 static void	 pfi_detach_group_event(void * __unused, struct ifg_group *);
101 static void	 pfi_ifaddr_event(void * __unused, struct ifnet *);
102 
103 RB_HEAD(pfi_ifhead, pfi_kkif);
104 static RB_PROTOTYPE(pfi_ifhead, pfi_kkif, pfik_tree, pfi_kkif_compare);
105 static RB_GENERATE(pfi_ifhead, pfi_kkif, pfik_tree, pfi_kkif_compare);
106 VNET_DEFINE_STATIC(struct pfi_ifhead, pfi_ifs);
107 #define	V_pfi_ifs	VNET(pfi_ifs)
108 
109 #define	PFI_BUFFER_MAX		0x10000
110 MALLOC_DEFINE(PFI_MTYPE, "pf_ifnet", "pf(4) interface database");
111 
112 LIST_HEAD(pfi_list, pfi_kkif);
113 VNET_DEFINE_STATIC(struct pfi_list, pfi_unlinked_kifs);
114 #define	V_pfi_unlinked_kifs	VNET(pfi_unlinked_kifs)
115 static struct mtx pfi_unlnkdkifs_mtx;
116 MTX_SYSINIT(pfi_unlnkdkifs_mtx, &pfi_unlnkdkifs_mtx, "pf unlinked interfaces",
117     MTX_DEF);
118 
119 void
pfi_initialize_vnet(void)120 pfi_initialize_vnet(void)
121 {
122 	struct pfi_list kifs = LIST_HEAD_INITIALIZER();
123 	struct epoch_tracker et;
124 	struct pfi_kkif *kif;
125 	struct ifg_group *ifg;
126 	struct ifnet *ifp;
127 	int nkifs;
128 
129 	V_pfi_buffer_max = 64;
130 	V_pfi_buffer = malloc(V_pfi_buffer_max * sizeof(*V_pfi_buffer),
131 	    PFI_MTYPE, M_WAITOK);
132 
133 	nkifs = 1;	/* one for V_pfi_all */
134 	IFNET_RLOCK();
135 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
136 		nkifs++;
137 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
138 		nkifs++;
139 
140 	for (int n = 0; n < nkifs; n++) {
141 		kif = pf_kkif_create(M_WAITOK);
142 		LIST_INSERT_HEAD(&kifs, kif, pfik_list);
143 	}
144 
145 	NET_EPOCH_ENTER(et);
146 	PF_RULES_WLOCK();
147 	kif = LIST_FIRST(&kifs);
148 	LIST_REMOVE(kif, pfik_list);
149 	V_pfi_all = pfi_kkif_attach(kif, IFG_ALL);
150 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) {
151 		kif = LIST_FIRST(&kifs);
152 		LIST_REMOVE(kif, pfik_list);
153 		pfi_attach_ifgroup(ifg, kif);
154 	}
155 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
156 		kif = LIST_FIRST(&kifs);
157 		LIST_REMOVE(kif, pfik_list);
158 		pfi_attach_ifnet(ifp, kif);
159 	}
160 	PF_RULES_WUNLOCK();
161 	NET_EPOCH_EXIT(et);
162 	IFNET_RUNLOCK();
163 
164 	MPASS(LIST_EMPTY(&kifs));
165 }
166 
167 void
pfi_initialize(void)168 pfi_initialize(void)
169 {
170 
171 	pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event,
172 	    pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
173 	pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event,
174 	    pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
175 	pfi_attach_group_cookie = EVENTHANDLER_REGISTER(group_attach_event,
176 	    pfi_attach_group_event, NULL, EVENTHANDLER_PRI_ANY);
177 	pfi_change_group_cookie = EVENTHANDLER_REGISTER(group_change_event,
178 	    pfi_change_group_event, NULL, EVENTHANDLER_PRI_ANY);
179 	pfi_detach_group_cookie = EVENTHANDLER_REGISTER(group_detach_event,
180 	    pfi_detach_group_event, NULL, EVENTHANDLER_PRI_ANY);
181 	pfi_ifaddr_event_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
182 	    pfi_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
183 }
184 
185 void
pfi_cleanup_vnet(void)186 pfi_cleanup_vnet(void)
187 {
188 	struct pfi_kkif *kif;
189 
190 	PF_RULES_WASSERT();
191 
192 	V_pfi_all = NULL;
193 	while ((kif = RB_MIN(pfi_ifhead, &V_pfi_ifs))) {
194 		RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif);
195 		if (kif->pfik_group)
196 			kif->pfik_group->ifg_pf_kif = NULL;
197 		if (kif->pfik_ifp) {
198 			if_rele(kif->pfik_ifp);
199 			kif->pfik_ifp->if_pf_kif = NULL;
200 		}
201 		pf_kkif_free(kif);
202 	}
203 
204 	mtx_lock(&pfi_unlnkdkifs_mtx);
205 	while ((kif = LIST_FIRST(&V_pfi_unlinked_kifs))) {
206 		LIST_REMOVE(kif, pfik_list);
207 		pf_kkif_free(kif);
208 	}
209 	mtx_unlock(&pfi_unlnkdkifs_mtx);
210 
211 	free(V_pfi_buffer, PFI_MTYPE);
212 }
213 
214 void
pfi_cleanup(void)215 pfi_cleanup(void)
216 {
217 
218 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie);
219 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie);
220 	EVENTHANDLER_DEREGISTER(group_attach_event, pfi_attach_group_cookie);
221 	EVENTHANDLER_DEREGISTER(group_change_event, pfi_change_group_cookie);
222 	EVENTHANDLER_DEREGISTER(group_detach_event, pfi_detach_group_cookie);
223 	EVENTHANDLER_DEREGISTER(ifaddr_event, pfi_ifaddr_event_cookie);
224 }
225 
226 struct pfi_kkif*
pf_kkif_create(int flags)227 pf_kkif_create(int flags)
228 {
229 	struct pfi_kkif *kif;
230 #ifdef PF_WANT_32_TO_64_COUNTER
231 	bool wowned;
232 #endif
233 
234 	kif = malloc(sizeof(*kif), PFI_MTYPE, flags | M_ZERO);
235 	if (! kif)
236 		return (kif);
237 
238 	for (int i = 0; i < 2; i++) {
239 		for (int j = 0; j < 2; j++) {
240 			for (int k = 0; k < 2; k++) {
241 				if (pf_counter_u64_init(&kif->pfik_packets[i][j][k], flags) != 0) {
242 					pf_kkif_free(kif);
243 					return (NULL);
244 				}
245 
246 				if (pf_counter_u64_init(&kif->pfik_bytes[i][j][k], flags) != 0) {
247 					pf_kkif_free(kif);
248 					return (NULL);
249 				}
250 			}
251 		}
252 	}
253 
254 #ifdef PF_WANT_32_TO_64_COUNTER
255 	wowned = PF_RULES_WOWNED();
256 	if (!wowned)
257 		PF_RULES_WLOCK();
258 	LIST_INSERT_HEAD(&V_pf_allkiflist, kif, pfik_allkiflist);
259 	V_pf_allkifcount++;
260 	if (!wowned)
261 		PF_RULES_WUNLOCK();
262 #endif
263 
264 	return (kif);
265 }
266 
267 void
pf_kkif_free(struct pfi_kkif * kif)268 pf_kkif_free(struct pfi_kkif *kif)
269 {
270 #ifdef PF_WANT_32_TO_64_COUNTER
271 	bool wowned;
272 #endif
273 
274 	if (! kif)
275 		return;
276 
277 #ifdef INVARIANTS
278 	if (kif->pfik_ifp) {
279 		struct ifnet *ifp = kif->pfik_ifp;
280 		MPASS(ifp->if_pf_kif == NULL || ifp->if_pf_kif == kif);
281 	}
282 #endif
283 
284 #ifdef PF_WANT_32_TO_64_COUNTER
285 	wowned = PF_RULES_WOWNED();
286 	if (!wowned)
287 		PF_RULES_WLOCK();
288 	LIST_REMOVE(kif, pfik_allkiflist);
289 	V_pf_allkifcount--;
290 	if (!wowned)
291 		PF_RULES_WUNLOCK();
292 #endif
293 
294 	for (int i = 0; i < 2; i++) {
295 		for (int j = 0; j < 2; j++) {
296 			for (int k = 0; k < 2; k++) {
297 				pf_counter_u64_deinit(&kif->pfik_packets[i][j][k]);
298 				pf_counter_u64_deinit(&kif->pfik_bytes[i][j][k]);
299 			}
300 		}
301 	}
302 
303 	free(kif, PFI_MTYPE);
304 }
305 
306 void
pf_kkif_zero(struct pfi_kkif * kif)307 pf_kkif_zero(struct pfi_kkif *kif)
308 {
309 
310 	for (int i = 0; i < 2; i++) {
311 		for (int j = 0; j < 2; j++) {
312 			for (int k = 0; k < 2; k++) {
313 				pf_counter_u64_zero(&kif->pfik_packets[i][j][k]);
314 				pf_counter_u64_zero(&kif->pfik_bytes[i][j][k]);
315 			}
316 		}
317 	}
318 	kif->pfik_tzero = time_second;
319 }
320 
321 struct pfi_kkif *
pfi_kkif_find(const char * kif_name)322 pfi_kkif_find(const char *kif_name)
323 {
324 	struct pfi_kif_cmp s;
325 
326 	PF_RULES_ASSERT();
327 
328 	memset(&s, 0, sizeof(s));
329 	strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
330 
331 	return (RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kkif *)&s));
332 }
333 
334 struct pfi_kkif *
pfi_kkif_attach(struct pfi_kkif * kif,const char * kif_name)335 pfi_kkif_attach(struct pfi_kkif *kif, const char *kif_name)
336 {
337 	struct pfi_kkif *kif1;
338 
339 	PF_RULES_WASSERT();
340 	KASSERT(kif != NULL, ("%s: null kif", __func__));
341 
342 	kif1 = pfi_kkif_find(kif_name);
343 	if (kif1 != NULL) {
344 		pf_kkif_free(kif);
345 		return (kif1);
346 	}
347 
348 	pf_kkif_zero(kif);
349 	strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
350 	/*
351 	 * It seems that the value of time_second is in unintialzied state
352 	 * when pf sets interface statistics clear time in boot phase if pf
353 	 * was statically linked to kernel. Instead of setting the bogus
354 	 * time value have pfi_get_ifaces handle this case. In
355 	 * pfi_get_ifaces it uses time_second if it sees the time is 0.
356 	 */
357 	kif->pfik_tzero = time_second > 1 ? time_second : 0;
358 	TAILQ_INIT(&kif->pfik_dynaddrs);
359 
360 	RB_INSERT(pfi_ifhead, &V_pfi_ifs, kif);
361 
362 	return (kif);
363 }
364 
365 void
pfi_kkif_ref(struct pfi_kkif * kif)366 pfi_kkif_ref(struct pfi_kkif *kif)
367 {
368 
369 	PF_RULES_WASSERT();
370 	kif->pfik_rulerefs++;
371 }
372 
373 static void
pfi_kkif_remove_if_unref(struct pfi_kkif * kif)374 pfi_kkif_remove_if_unref(struct pfi_kkif *kif)
375 {
376 
377 	PF_RULES_WASSERT();
378 
379 	if (kif->pfik_rulerefs > 0)
380 		return;
381 
382 	/* kif referencing an existing ifnet or group or holding flags should
383 	 * exist. */
384 	if (kif->pfik_ifp != NULL || kif->pfik_group != NULL ||
385 	    kif == V_pfi_all || kif->pfik_flags != 0)
386 		return;
387 
388 	/*
389 	 * We can get here in at least two distinct paths:
390 	 * - when the struct ifnet is removed, via pfi_detach_ifnet_event()
391 	 * - when a rule referencing us is removed, via pfi_kkif_unref().
392 	 * These two events can race against each other, leading us to free this kif
393 	 * twice. That leads to a loop in V_pfi_unlinked_kifs, and an eventual
394 	 * deadlock.
395 	 *
396 	 * Avoid this by making sure we only ever insert the kif into
397 	 * V_pfi_unlinked_kifs once.
398 	 * If we don't find it in V_pfi_ifs it's already been removed. Check that it
399 	 * exists in V_pfi_unlinked_kifs.
400 	 */
401 	if (! RB_FIND(pfi_ifhead, &V_pfi_ifs, kif)) {
402 #ifdef INVARIANTS
403 		struct pfi_kkif *tmp;
404 		bool found = false;
405 		mtx_lock(&pfi_unlnkdkifs_mtx);
406 		LIST_FOREACH(tmp, &V_pfi_unlinked_kifs, pfik_list) {
407 			if (tmp == kif) {
408 				found = true;
409 				break;
410 			}
411 		}
412 		mtx_unlock(&pfi_unlnkdkifs_mtx);
413 		MPASS(found);
414 #endif
415 		return;
416 	}
417 	RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif);
418 
419 	kif->pfik_flags |= PFI_IFLAG_REFS;
420 
421 	mtx_lock(&pfi_unlnkdkifs_mtx);
422 	LIST_INSERT_HEAD(&V_pfi_unlinked_kifs, kif, pfik_list);
423 	mtx_unlock(&pfi_unlnkdkifs_mtx);
424 }
425 
426 void
pfi_kkif_unref(struct pfi_kkif * kif)427 pfi_kkif_unref(struct pfi_kkif *kif)
428 {
429 
430 	PF_RULES_WASSERT();
431 	KASSERT(kif->pfik_rulerefs > 0, ("%s: %p has zero refs", __func__, kif));
432 
433 	kif->pfik_rulerefs--;
434 
435 	pfi_kkif_remove_if_unref(kif);
436 }
437 
438 void
pfi_kkif_purge(void)439 pfi_kkif_purge(void)
440 {
441 	struct pfi_kkif *kif, *kif1;
442 
443 	/*
444 	 * Do naive mark-and-sweep garbage collecting of old kifs.
445 	 * Reference flag is raised by pf_purge_expired_states().
446 	 */
447 	mtx_lock(&pfi_unlnkdkifs_mtx);
448 	LIST_FOREACH_SAFE(kif, &V_pfi_unlinked_kifs, pfik_list, kif1) {
449 		if (!(kif->pfik_flags & PFI_IFLAG_REFS)) {
450 			LIST_REMOVE(kif, pfik_list);
451 			pf_kkif_free(kif);
452 		} else
453 			kif->pfik_flags &= ~PFI_IFLAG_REFS;
454 	}
455 	mtx_unlock(&pfi_unlnkdkifs_mtx);
456 }
457 
458 int
pfi_kkif_match(struct pfi_kkif * rule_kif,struct pfi_kkif * packet_kif)459 pfi_kkif_match(struct pfi_kkif *rule_kif, struct pfi_kkif *packet_kif)
460 {
461 	struct ifg_list	*p;
462 
463 	NET_EPOCH_ASSERT();
464 
465 	MPASS(packet_kif != NULL);
466 	MPASS(packet_kif->pfik_ifp != NULL);
467 
468 	if (rule_kif == NULL || rule_kif == packet_kif)
469 		return (1);
470 
471 	if (rule_kif->pfik_group != NULL) {
472 		CK_STAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
473 			if (p->ifgl_group == rule_kif->pfik_group)
474 				return (1);
475 	}
476 
477 	return (0);
478 }
479 
480 static void
pfi_attach_ifnet(struct ifnet * ifp,struct pfi_kkif * kif)481 pfi_attach_ifnet(struct ifnet *ifp, struct pfi_kkif *kif)
482 {
483 
484 	PF_RULES_WASSERT();
485 
486 	V_pfi_update++;
487 	kif = pfi_kkif_attach(kif, ifp->if_xname);
488 	if_ref(ifp);
489 	kif->pfik_ifp = ifp;
490 	ifp->if_pf_kif = kif;
491 	pfi_kkif_update(kif);
492 }
493 
494 static void
pfi_attach_ifgroup(struct ifg_group * ifg,struct pfi_kkif * kif)495 pfi_attach_ifgroup(struct ifg_group *ifg, struct pfi_kkif *kif)
496 {
497 
498 	PF_RULES_WASSERT();
499 
500 	V_pfi_update++;
501 	kif = pfi_kkif_attach(kif, ifg->ifg_group);
502 	kif->pfik_group = ifg;
503 	ifg->ifg_pf_kif = kif;
504 }
505 
506 int
pfi_match_addr(struct pfi_dynaddr * dyn,struct pf_addr * a,sa_family_t af)507 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
508 {
509 	switch (af) {
510 #ifdef INET
511 	case AF_INET:
512 		switch (dyn->pfid_acnt4) {
513 		case 0:
514 			return (0);
515 		case 1:
516 			return (PF_MATCHA(0, &dyn->pfid_addr4,
517 			    &dyn->pfid_mask4, a, AF_INET));
518 		default:
519 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
520 		}
521 		break;
522 #endif /* INET */
523 #ifdef INET6
524 	case AF_INET6:
525 		switch (dyn->pfid_acnt6) {
526 		case 0:
527 			return (0);
528 		case 1:
529 			return (PF_MATCHA(0, &dyn->pfid_addr6,
530 			    &dyn->pfid_mask6, a, AF_INET6));
531 		default:
532 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
533 		}
534 		break;
535 #endif /* INET6 */
536 	default:
537 		return (0);
538 	}
539 }
540 
541 int
pfi_dynaddr_setup(struct pf_addr_wrap * aw,sa_family_t af)542 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
543 {
544 	struct epoch_tracker	 et;
545 	struct pfi_dynaddr	*dyn;
546 	char			 tblname[PF_TABLE_NAME_SIZE];
547 	struct pf_kruleset	*ruleset = NULL;
548 	struct pfi_kkif		*kif;
549 	int			 rv = 0;
550 
551 	PF_RULES_WASSERT();
552 	KASSERT(aw->type == PF_ADDR_DYNIFTL, ("%s: type %u",
553 	    __func__, aw->type));
554 	KASSERT(aw->p.dyn == NULL, ("%s: dyn is %p", __func__, aw->p.dyn));
555 
556 	if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL)
557 		return (ENOMEM);
558 
559 	if ((kif = pf_kkif_create(M_NOWAIT)) == NULL) {
560 		free(dyn, PFI_MTYPE);
561 		return (ENOMEM);
562 	}
563 
564 	if (!strcmp(aw->v.ifname, "self"))
565 		dyn->pfid_kif = pfi_kkif_attach(kif, IFG_ALL);
566 	else
567 		dyn->pfid_kif = pfi_kkif_attach(kif, aw->v.ifname);
568 	kif = NULL;
569 	pfi_kkif_ref(dyn->pfid_kif);
570 
571 	dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
572 	if (af == AF_INET && dyn->pfid_net == 32)
573 		dyn->pfid_net = 128;
574 	strlcpy(tblname, aw->v.ifname, sizeof(tblname));
575 	if (aw->iflags & PFI_AFLAG_NETWORK)
576 		strlcat(tblname, ":network", sizeof(tblname));
577 	if (aw->iflags & PFI_AFLAG_BROADCAST)
578 		strlcat(tblname, ":broadcast", sizeof(tblname));
579 	if (aw->iflags & PFI_AFLAG_PEER)
580 		strlcat(tblname, ":peer", sizeof(tblname));
581 	if (aw->iflags & PFI_AFLAG_NOALIAS)
582 		strlcat(tblname, ":0", sizeof(tblname));
583 	if (dyn->pfid_net != 128)
584 		snprintf(tblname + strlen(tblname),
585 		    sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
586 	if ((ruleset = pf_find_or_create_kruleset(PF_RESERVED_ANCHOR)) == NULL) {
587 		rv = ENOMEM;
588 		goto _bad;
589 	}
590 
591 	if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
592 		rv = ENOMEM;
593 		goto _bad;
594 	}
595 
596 	dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
597 	dyn->pfid_iflags = aw->iflags;
598 	dyn->pfid_af = af;
599 
600 	TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
601 	aw->p.dyn = dyn;
602 	NET_EPOCH_ENTER(et);
603 	pfi_kkif_update(dyn->pfid_kif);
604 	NET_EPOCH_EXIT(et);
605 
606 	return (0);
607 
608 _bad:
609 	if (dyn->pfid_kt != NULL)
610 		pfr_detach_table(dyn->pfid_kt);
611 	if (ruleset != NULL)
612 		pf_remove_if_empty_kruleset(ruleset);
613 	pfi_kkif_unref(dyn->pfid_kif);
614 	free(dyn, PFI_MTYPE);
615 
616 	return (rv);
617 }
618 
619 static void
pfi_kkif_update(struct pfi_kkif * kif)620 pfi_kkif_update(struct pfi_kkif *kif)
621 {
622 	struct ifg_list		*ifgl;
623 	struct ifg_member	*ifgm;
624 	struct pfi_dynaddr	*p;
625 	struct pfi_kkif		*tmpkif;
626 
627 	NET_EPOCH_ASSERT();
628 	PF_RULES_WASSERT();
629 
630 	/* update all dynaddr */
631 	TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
632 		pfi_dynaddr_update(p);
633 
634 	/* Apply group flags to new members. */
635 	if (kif->pfik_group != NULL) {
636 		CK_STAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members,
637 		    ifgm_next) {
638 			tmpkif = (struct pfi_kkif *)ifgm->ifgm_ifp->if_pf_kif;
639 			if (tmpkif == NULL)
640 				continue;
641 
642 			tmpkif->pfik_flags |= kif->pfik_flags;
643 		}
644 	}
645 
646 	/* again for all groups kif is member of */
647 	if (kif->pfik_ifp != NULL) {
648 		CK_STAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
649 			pfi_kkif_update((struct pfi_kkif *)
650 			    ifgl->ifgl_group->ifg_pf_kif);
651 	}
652 }
653 
654 static void
pfi_dynaddr_update(struct pfi_dynaddr * dyn)655 pfi_dynaddr_update(struct pfi_dynaddr *dyn)
656 {
657 	struct pfi_kkif		*kif;
658 	struct pfr_ktable	*kt;
659 
660 	PF_RULES_WASSERT();
661 	KASSERT(dyn && dyn->pfid_kif && dyn->pfid_kt,
662 	    ("%s: bad argument", __func__));
663 
664 	kif = dyn->pfid_kif;
665 	kt = dyn->pfid_kt;
666 
667 	if (kt->pfrkt_larg != V_pfi_update) {
668 		/* this table needs to be brought up-to-date */
669 		pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
670 		kt->pfrkt_larg = V_pfi_update;
671 	}
672 	pfr_dynaddr_update(kt, dyn);
673 }
674 
675 static void
pfi_table_update(struct pfr_ktable * kt,struct pfi_kkif * kif,int net,int flags)676 pfi_table_update(struct pfr_ktable *kt, struct pfi_kkif *kif, int net, int flags)
677 {
678 	int			 e, size2 = 0;
679 	struct ifg_member	*ifgm;
680 
681 	NET_EPOCH_ASSERT();
682 
683 	V_pfi_buffer_cnt = 0;
684 
685 	if (kif->pfik_ifp != NULL)
686 		pfi_instance_add(kif->pfik_ifp, net, flags);
687 	else if (kif->pfik_group != NULL) {
688 		CK_STAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
689 			pfi_instance_add(ifgm->ifgm_ifp, net, flags);
690 	}
691 
692 	if ((e = pfr_set_addrs(&kt->pfrkt_t, V_pfi_buffer, V_pfi_buffer_cnt, &size2,
693 	    NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
694 		printf("%s: cannot set %d new addresses into table %s: %d\n",
695 		    __func__, V_pfi_buffer_cnt, kt->pfrkt_name, e);
696 }
697 
698 static void
pfi_instance_add(struct ifnet * ifp,int net,int flags)699 pfi_instance_add(struct ifnet *ifp, int net, int flags)
700 {
701 	struct ifaddr	*ia;
702 	int		 got4 = 0, got6 = 0;
703 	int		 net2, af;
704 
705 	NET_EPOCH_ASSERT();
706 
707 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
708 		if (ia->ifa_addr == NULL)
709 			continue;
710 		af = ia->ifa_addr->sa_family;
711 		if (af != AF_INET && af != AF_INET6)
712 			continue;
713 		/*
714 		 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
715 		 *      jump over addresses without a proper route to work
716 		 *      around a problem with ppp not fully removing the
717 		 *      address used during IPCP.
718 		 */
719 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
720 		    !(ia->ifa_flags & IFA_ROUTE) &&
721 		    (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
722 			continue;
723 		if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
724 			continue;
725 		if ((flags & PFI_AFLAG_BROADCAST) &&
726 		    !(ifp->if_flags & IFF_BROADCAST))
727 			continue;
728 		if ((flags & PFI_AFLAG_PEER) &&
729 		    !(ifp->if_flags & IFF_POINTOPOINT))
730 			continue;
731 		if ((flags & (PFI_AFLAG_NETWORK | PFI_AFLAG_NOALIAS)) &&
732 		    af == AF_INET6 &&
733 		    IN6_IS_ADDR_LINKLOCAL(
734 		    &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
735 			continue;
736 		if (flags & PFI_AFLAG_NOALIAS) {
737 			if (af == AF_INET && got4)
738 				continue;
739 			if (af == AF_INET6 && got6)
740 				continue;
741 		}
742 		if (af == AF_INET)
743 			got4 = 1;
744 		else if (af == AF_INET6)
745 			got6 = 1;
746 		net2 = net;
747 		if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
748 			if (af == AF_INET)
749 				net2 = pfi_unmask(&((struct sockaddr_in *)
750 				    ia->ifa_netmask)->sin_addr);
751 			else if (af == AF_INET6)
752 				net2 = pfi_unmask(&((struct sockaddr_in6 *)
753 				    ia->ifa_netmask)->sin6_addr);
754 		}
755 		if (af == AF_INET && net2 > 32)
756 			net2 = 32;
757 		if (flags & PFI_AFLAG_BROADCAST)
758 			pfi_address_add(ia->ifa_broadaddr, af, net2);
759 		else if (flags & PFI_AFLAG_PEER)
760 			pfi_address_add(ia->ifa_dstaddr, af, net2);
761 		else
762 			pfi_address_add(ia->ifa_addr, af, net2);
763 	}
764 }
765 
766 static void
pfi_address_add(struct sockaddr * sa,int af,int net)767 pfi_address_add(struct sockaddr *sa, int af, int net)
768 {
769 	struct pfr_addr	*p;
770 	int		 i;
771 
772 	if (V_pfi_buffer_cnt >= V_pfi_buffer_max) {
773 		int		 new_max = V_pfi_buffer_max * 2;
774 
775 		if (new_max > PFI_BUFFER_MAX) {
776 			printf("%s: address buffer full (%d/%d)\n", __func__,
777 			    V_pfi_buffer_cnt, PFI_BUFFER_MAX);
778 			return;
779 		}
780 		p = malloc(new_max * sizeof(*V_pfi_buffer), PFI_MTYPE,
781 		    M_NOWAIT);
782 		if (p == NULL) {
783 			printf("%s: no memory to grow buffer (%d/%d)\n",
784 			    __func__, V_pfi_buffer_cnt, PFI_BUFFER_MAX);
785 			return;
786 		}
787 		memcpy(p, V_pfi_buffer, V_pfi_buffer_max * sizeof(*V_pfi_buffer));
788 		/* no need to zero buffer */
789 		free(V_pfi_buffer, PFI_MTYPE);
790 		V_pfi_buffer = p;
791 		V_pfi_buffer_max = new_max;
792 	}
793 	if (af == AF_INET && net > 32)
794 		net = 128;
795 	p = V_pfi_buffer + V_pfi_buffer_cnt++;
796 	memset(p, 0, sizeof(*p));
797 	p->pfra_af = af;
798 	p->pfra_net = net;
799 	if (af == AF_INET)
800 		p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
801 	else if (af == AF_INET6) {
802 		p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
803 		if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
804 			p->pfra_ip6addr.s6_addr16[1] = 0;
805 	}
806 	/* mask network address bits */
807 	if (net < 128)
808 		((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
809 	for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
810 		((caddr_t)p)[i] = 0;
811 }
812 
813 void
pfi_dynaddr_remove(struct pfi_dynaddr * dyn)814 pfi_dynaddr_remove(struct pfi_dynaddr *dyn)
815 {
816 
817 	KASSERT(dyn->pfid_kif != NULL, ("%s: null pfid_kif", __func__));
818 	KASSERT(dyn->pfid_kt != NULL, ("%s: null pfid_kt", __func__));
819 
820 	TAILQ_REMOVE(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
821 	pfi_kkif_unref(dyn->pfid_kif);
822 	pfr_detach_table(dyn->pfid_kt);
823 	free(dyn, PFI_MTYPE);
824 }
825 
826 void
pfi_dynaddr_copyout(struct pf_addr_wrap * aw)827 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
828 {
829 
830 	KASSERT(aw->type == PF_ADDR_DYNIFTL,
831 	    ("%s: type %u", __func__, aw->type));
832 
833 	if (aw->p.dyn == NULL || aw->p.dyn->pfid_kif == NULL)
834 		return;
835 	aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
836 }
837 
838 static int
pfi_kkif_compare(struct pfi_kkif * p,struct pfi_kkif * q)839 pfi_kkif_compare(struct pfi_kkif *p, struct pfi_kkif *q)
840 {
841 	return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
842 }
843 
844 void
pfi_update_status(const char * name,struct pf_status * pfs)845 pfi_update_status(const char *name, struct pf_status *pfs)
846 {
847 	struct pfi_kkif		*p;
848 	struct pfi_kif_cmp	 key;
849 	struct ifg_member	 p_member, *ifgm;
850 	CK_STAILQ_HEAD(, ifg_member) ifg_members;
851 	int			 i, j, k;
852 
853 	if (pfs) {
854 		memset(pfs->pcounters, 0, sizeof(pfs->pcounters));
855 		memset(pfs->bcounters, 0, sizeof(pfs->bcounters));
856 	}
857 
858 	strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
859 	p = RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kkif *)&key);
860 	if (p == NULL) {
861 		return;
862 	}
863 
864 	if (p->pfik_group != NULL) {
865 		memcpy(&ifg_members, &p->pfik_group->ifg_members,
866 		    sizeof(ifg_members));
867 	} else {
868 		/* build a temporary list for p only */
869 		memset(&p_member, 0, sizeof(p_member));
870 		p_member.ifgm_ifp = p->pfik_ifp;
871 		CK_STAILQ_INIT(&ifg_members);
872 		CK_STAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next);
873 	}
874 	CK_STAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) {
875 		if (ifgm->ifgm_ifp == NULL || ifgm->ifgm_ifp->if_pf_kif == NULL)
876 			continue;
877 		p = (struct pfi_kkif *)ifgm->ifgm_ifp->if_pf_kif;
878 
879 		/* just clear statistics */
880 		if (pfs == NULL) {
881 			pf_kkif_zero(p);
882 			continue;
883 		}
884 		for (i = 0; i < 2; i++)
885 			for (j = 0; j < 2; j++)
886 				for (k = 0; k < 2; k++) {
887 					pfs->pcounters[i][j][k] +=
888 					    pf_counter_u64_fetch(&p->pfik_packets[i][j][k]);
889 					pfs->bcounters[i][j] +=
890 					    pf_counter_u64_fetch(&p->pfik_bytes[i][j][k]);
891 				}
892 	}
893 }
894 
895 static void
pf_kkif_to_kif(struct pfi_kkif * kkif,struct pfi_kif * kif)896 pf_kkif_to_kif(struct pfi_kkif *kkif, struct pfi_kif *kif)
897 {
898 
899 	memset(kif, 0, sizeof(*kif));
900 	strlcpy(kif->pfik_name, kkif->pfik_name, sizeof(kif->pfik_name));
901 	for (int i = 0; i < 2; i++) {
902 		for (int j = 0; j < 2; j++) {
903 			for (int k = 0; k < 2; k++) {
904 				kif->pfik_packets[i][j][k] =
905 				    pf_counter_u64_fetch(&kkif->pfik_packets[i][j][k]);
906 				kif->pfik_bytes[i][j][k] =
907 				    pf_counter_u64_fetch(&kkif->pfik_bytes[i][j][k]);
908 			}
909 		}
910 	}
911 	kif->pfik_flags = kkif->pfik_flags;
912 	kif->pfik_tzero = kkif->pfik_tzero;
913 	kif->pfik_rulerefs = kkif->pfik_rulerefs;
914 	/*
915 	 * Userspace relies on this pointer to decide if this is a group or
916 	 * not. We don't want to share the actual pointer, because it's
917 	 * useless to userspace and leaks kernel memory layout information.
918 	 * So instead we provide 0xfeedcode as 'true' and NULL as 'false'.
919 	 */
920 	kif->pfik_group =
921 	    kkif->pfik_group ? (struct ifg_group *)0xfeedc0de : NULL;
922 }
923 
924 void
pfi_get_ifaces(const char * name,struct pfi_kif * buf,int * size)925 pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
926 {
927 	struct epoch_tracker et;
928 	struct pfi_kkif	*p, *nextp;
929 	int		 n = 0;
930 
931 	NET_EPOCH_ENTER(et);
932 	for (p = RB_MIN(pfi_ifhead, &V_pfi_ifs); p; p = nextp) {
933 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
934 		if (pfi_skip_if(name, p))
935 			continue;
936 		if (*size <= n++)
937 			break;
938 		if (!p->pfik_tzero)
939 			p->pfik_tzero = time_second;
940 		pf_kkif_to_kif(p, buf++);
941 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
942 	}
943 	*size = n;
944 	NET_EPOCH_EXIT(et);
945 }
946 
947 static int
pfi_skip_if(const char * filter,struct pfi_kkif * p)948 pfi_skip_if(const char *filter, struct pfi_kkif *p)
949 {
950 	struct ifg_list *i;
951 	int	n;
952 
953 	NET_EPOCH_ASSERT();
954 
955 	if (filter == NULL || !*filter)
956 		return (0);
957 	if (!strcmp(p->pfik_name, filter))
958 		return (0);	/* exact match */
959 	n = strlen(filter);
960 	if (n < 1 || n >= IFNAMSIZ)
961 		return (1);	/* sanity check */
962 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
963 		return (1);	/* group names may not end in a digit */
964 	if (p->pfik_ifp == NULL)
965 		return (1);
966 	CK_STAILQ_FOREACH(i, &p->pfik_ifp->if_groups, ifgl_next)
967 		if (!strncmp(i->ifgl_group->ifg_group, filter, IFNAMSIZ))
968 			return (0); /* iface is in group "filter" */
969 	return (1);
970 }
971 
972 int
pfi_set_flags(const char * name,int flags)973 pfi_set_flags(const char *name, int flags)
974 {
975 	struct epoch_tracker et;
976 	struct pfi_kkif	*p, *kif;
977 
978 	kif = pf_kkif_create(M_NOWAIT);
979 	if (kif == NULL)
980 		return (ENOMEM);
981 
982 	NET_EPOCH_ENTER(et);
983 
984 	kif = pfi_kkif_attach(kif, name);
985 
986 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
987 		if (pfi_skip_if(name, p))
988 			continue;
989 		p->pfik_flags |= flags;
990 	}
991 	NET_EPOCH_EXIT(et);
992 	return (0);
993 }
994 
995 int
pfi_clear_flags(const char * name,int flags)996 pfi_clear_flags(const char *name, int flags)
997 {
998 	struct epoch_tracker et;
999 	struct pfi_kkif *p, *tmp;
1000 
1001 	NET_EPOCH_ENTER(et);
1002 	RB_FOREACH_SAFE(p, pfi_ifhead, &V_pfi_ifs, tmp) {
1003 		if (pfi_skip_if(name, p))
1004 			continue;
1005 		p->pfik_flags &= ~flags;
1006 
1007 		if (p->pfik_ifp == NULL && p->pfik_group == NULL &&
1008 		    p->pfik_flags == 0 && p->pfik_rulerefs == 0) {
1009 			/* Delete this kif. */
1010 			RB_REMOVE(pfi_ifhead, &V_pfi_ifs, p);
1011 			pf_kkif_free(p);
1012 		}
1013 	}
1014 	NET_EPOCH_EXIT(et);
1015 	return (0);
1016 }
1017 
1018 /* from pf_print_state.c */
1019 static int
pfi_unmask(void * addr)1020 pfi_unmask(void *addr)
1021 {
1022 	struct pf_addr *m = addr;
1023 	int i = 31, j = 0, b = 0;
1024 	u_int32_t tmp;
1025 
1026 	while (j < 4 && m->addr32[j] == 0xffffffff) {
1027 		b += 32;
1028 		j++;
1029 	}
1030 	if (j < 4) {
1031 		tmp = ntohl(m->addr32[j]);
1032 		for (i = 31; tmp & (1 << i); --i)
1033 			b++;
1034 	}
1035 	return (b);
1036 }
1037 
1038 static void
pfi_attach_ifnet_event(void * arg __unused,struct ifnet * ifp)1039 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
1040 {
1041 	struct epoch_tracker et;
1042 	struct pfi_kkif *kif;
1043 
1044 	if (V_pf_vnet_active == 0) {
1045 		/* Avoid teardown race in the least expensive way. */
1046 		return;
1047 	}
1048 	kif = pf_kkif_create(M_NOWAIT);
1049 	NET_EPOCH_ENTER(et);
1050 	PF_RULES_WLOCK();
1051 	pfi_attach_ifnet(ifp, kif);
1052 #ifdef ALTQ
1053 	pf_altq_ifnet_event(ifp, 0);
1054 #endif
1055 	PF_RULES_WUNLOCK();
1056 	NET_EPOCH_EXIT(et);
1057 }
1058 
1059 static void
pfi_detach_ifnet_event(void * arg __unused,struct ifnet * ifp)1060 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
1061 {
1062 	struct epoch_tracker et;
1063 	struct pfi_kkif *kif = (struct pfi_kkif *)ifp->if_pf_kif;
1064 
1065 	if (pfsync_detach_ifnet_ptr)
1066 		pfsync_detach_ifnet_ptr(ifp);
1067 
1068 	if (kif == NULL)
1069 		return;
1070 
1071 	if (V_pf_vnet_active == 0) {
1072 		/* Avoid teardown race in the least expensive way. */
1073 		return;
1074 	}
1075 
1076 	NET_EPOCH_ENTER(et);
1077 	PF_RULES_WLOCK();
1078 	V_pfi_update++;
1079 	pfi_kkif_update(kif);
1080 
1081 	if (kif->pfik_ifp)
1082 		if_rele(kif->pfik_ifp);
1083 
1084 	kif->pfik_ifp = NULL;
1085 	ifp->if_pf_kif = NULL;
1086 #ifdef ALTQ
1087 	pf_altq_ifnet_event(ifp, 1);
1088 #endif
1089 	pfi_kkif_remove_if_unref(kif);
1090 
1091 	PF_RULES_WUNLOCK();
1092 	NET_EPOCH_EXIT(et);
1093 }
1094 
1095 static void
pfi_attach_group_event(void * arg __unused,struct ifg_group * ifg)1096 pfi_attach_group_event(void *arg __unused, struct ifg_group *ifg)
1097 {
1098 	struct epoch_tracker et;
1099 	struct pfi_kkif *kif;
1100 
1101 	if (V_pf_vnet_active == 0) {
1102 		/* Avoid teardown race in the least expensive way. */
1103 		return;
1104 	}
1105 	kif = pf_kkif_create(M_WAITOK);
1106 	NET_EPOCH_ENTER(et);
1107 	PF_RULES_WLOCK();
1108 	pfi_attach_ifgroup(ifg, kif);
1109 	PF_RULES_WUNLOCK();
1110 	NET_EPOCH_EXIT(et);
1111 }
1112 
1113 static void
pfi_change_group_event(void * arg __unused,char * gname)1114 pfi_change_group_event(void *arg __unused, char *gname)
1115 {
1116 	struct epoch_tracker et;
1117 	struct pfi_kkif *kif;
1118 
1119 	if (V_pf_vnet_active == 0) {
1120 		/* Avoid teardown race in the least expensive way. */
1121 		return;
1122 	}
1123 
1124 	kif = pf_kkif_create(M_WAITOK);
1125 	NET_EPOCH_ENTER(et);
1126 	PF_RULES_WLOCK();
1127 	V_pfi_update++;
1128 	kif = pfi_kkif_attach(kif, gname);
1129 	pfi_kkif_update(kif);
1130 	PF_RULES_WUNLOCK();
1131 	NET_EPOCH_EXIT(et);
1132 }
1133 
1134 static void
pfi_detach_group_event(void * arg __unused,struct ifg_group * ifg)1135 pfi_detach_group_event(void *arg __unused, struct ifg_group *ifg)
1136 {
1137 	struct pfi_kkif *kif = (struct pfi_kkif *)ifg->ifg_pf_kif;
1138 
1139 	if (kif == NULL)
1140 		return;
1141 
1142 	if (V_pf_vnet_active == 0) {
1143 		/* Avoid teardown race in the least expensive way. */
1144 		return;
1145 	}
1146 	PF_RULES_WLOCK();
1147 	V_pfi_update++;
1148 
1149 	kif->pfik_group = NULL;
1150 	ifg->ifg_pf_kif = NULL;
1151 
1152 	pfi_kkif_remove_if_unref(kif);
1153 	PF_RULES_WUNLOCK();
1154 }
1155 
1156 static void
pfi_ifaddr_event(void * arg __unused,struct ifnet * ifp)1157 pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1158 {
1159 
1160 	KASSERT(ifp, ("ifp == NULL"));
1161 
1162 	if (ifp->if_pf_kif == NULL)
1163 		return;
1164 
1165 	if (V_pf_vnet_active == 0) {
1166 		/* Avoid teardown race in the least expensive way. */
1167 		return;
1168 	}
1169 	PF_RULES_WLOCK();
1170 	if (ifp->if_pf_kif) {
1171 		struct epoch_tracker et;
1172 
1173 		V_pfi_update++;
1174 		NET_EPOCH_ENTER(et);
1175 		pfi_kkif_update(ifp->if_pf_kif);
1176 		NET_EPOCH_EXIT(et);
1177 	}
1178 	PF_RULES_WUNLOCK();
1179 }
1180