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