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