xref: /freebsd/sys/netpfil/pf/pf_if.c (revision 235130433aa8ef492d1c12270a3687fc5e99d5bd)
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_kif *,	 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 *);
80 static void	 pfi_attach_ifgroup(struct ifg_group *);
81 
82 static void	 pfi_kif_update(struct pfi_kif *);
83 static void	 pfi_dynaddr_update(struct pfi_dynaddr *dyn);
84 static void	 pfi_table_update(struct pfr_ktable *, struct pfi_kif *, 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_if_compare(struct pfi_kif *, struct pfi_kif *);
89 static int	 pfi_skip_if(const char *, struct pfi_kif *);
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_kif);
99 static RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
100 static RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_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_kif);
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 ifg_group *ifg;
118 	struct ifnet *ifp;
119 	struct pfi_kif *kif;
120 
121 	V_pfi_buffer_max = 64;
122 	V_pfi_buffer = malloc(V_pfi_buffer_max * sizeof(*V_pfi_buffer),
123 	    PFI_MTYPE, M_WAITOK);
124 
125 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
126 	PF_RULES_WLOCK();
127 	V_pfi_all = pfi_kif_attach(kif, IFG_ALL);
128 	PF_RULES_WUNLOCK();
129 
130 	IFNET_RLOCK();
131 	CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
132 		pfi_attach_ifgroup(ifg);
133 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
134 		pfi_attach_ifnet(ifp);
135 	IFNET_RUNLOCK();
136 }
137 
138 void
139 pfi_initialize(void)
140 {
141 
142 	pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event,
143 	    pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
144 	pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event,
145 	    pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
146 	pfi_attach_group_cookie = EVENTHANDLER_REGISTER(group_attach_event,
147 	    pfi_attach_group_event, NULL, EVENTHANDLER_PRI_ANY);
148 	pfi_change_group_cookie = EVENTHANDLER_REGISTER(group_change_event,
149 	    pfi_change_group_event, NULL, EVENTHANDLER_PRI_ANY);
150 	pfi_detach_group_cookie = EVENTHANDLER_REGISTER(group_detach_event,
151 	    pfi_detach_group_event, NULL, EVENTHANDLER_PRI_ANY);
152 	pfi_ifaddr_event_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
153 	    pfi_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
154 }
155 
156 void
157 pfi_cleanup_vnet(void)
158 {
159 	struct pfi_kif *kif;
160 
161 	PF_RULES_WASSERT();
162 
163 	V_pfi_all = NULL;
164 	while ((kif = RB_MIN(pfi_ifhead, &V_pfi_ifs))) {
165 		RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif);
166 		if (kif->pfik_group)
167 			kif->pfik_group->ifg_pf_kif = NULL;
168 		if (kif->pfik_ifp)
169 			kif->pfik_ifp->if_pf_kif = NULL;
170 		free(kif, PFI_MTYPE);
171 	}
172 
173 	mtx_lock(&pfi_unlnkdkifs_mtx);
174 	while ((kif = LIST_FIRST(&V_pfi_unlinked_kifs))) {
175 		LIST_REMOVE(kif, pfik_list);
176 		free(kif, PFI_MTYPE);
177 	}
178 	mtx_unlock(&pfi_unlnkdkifs_mtx);
179 
180 	free(V_pfi_buffer, PFI_MTYPE);
181 }
182 
183 void
184 pfi_cleanup(void)
185 {
186 
187 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie);
188 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie);
189 	EVENTHANDLER_DEREGISTER(group_attach_event, pfi_attach_group_cookie);
190 	EVENTHANDLER_DEREGISTER(group_change_event, pfi_change_group_cookie);
191 	EVENTHANDLER_DEREGISTER(group_detach_event, pfi_detach_group_cookie);
192 	EVENTHANDLER_DEREGISTER(ifaddr_event, pfi_ifaddr_event_cookie);
193 }
194 
195 struct pfi_kif *
196 pfi_kif_find(const char *kif_name)
197 {
198 	struct pfi_kif_cmp s;
199 
200 	PF_RULES_ASSERT();
201 
202 	bzero(&s, sizeof(s));
203 	strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
204 
205 	return (RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&s));
206 }
207 
208 struct pfi_kif *
209 pfi_kif_attach(struct pfi_kif *kif, const char *kif_name)
210 {
211 	struct pfi_kif *kif1;
212 
213 	PF_RULES_WASSERT();
214 	KASSERT(kif != NULL, ("%s: null kif", __func__));
215 
216 	kif1 = pfi_kif_find(kif_name);
217 	if (kif1 != NULL) {
218 		free(kif, PFI_MTYPE);
219 		return (kif1);
220 	}
221 
222 	bzero(kif, sizeof(*kif));
223 	strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
224 	/*
225 	 * It seems that the value of time_second is in unintialzied state
226 	 * when pf sets interface statistics clear time in boot phase if pf
227 	 * was statically linked to kernel. Instead of setting the bogus
228 	 * time value have pfi_get_ifaces handle this case. In
229 	 * pfi_get_ifaces it uses time_second if it sees the time is 0.
230 	 */
231 	kif->pfik_tzero = time_second > 1 ? time_second : 0;
232 	TAILQ_INIT(&kif->pfik_dynaddrs);
233 
234 	RB_INSERT(pfi_ifhead, &V_pfi_ifs, kif);
235 
236 	return (kif);
237 }
238 
239 void
240 pfi_kif_ref(struct pfi_kif *kif)
241 {
242 
243 	PF_RULES_WASSERT();
244 	kif->pfik_rulerefs++;
245 }
246 
247 void
248 pfi_kif_unref(struct pfi_kif *kif)
249 {
250 
251 	PF_RULES_WASSERT();
252 	KASSERT(kif->pfik_rulerefs > 0, ("%s: %p has zero refs", __func__, kif));
253 
254 	kif->pfik_rulerefs--;
255 
256 	if (kif->pfik_rulerefs > 0)
257 		return;
258 
259 	/* kif referencing an existing ifnet or group should exist. */
260 	if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == V_pfi_all)
261 		return;
262 
263 	RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif);
264 
265 	kif->pfik_flags |= PFI_IFLAG_REFS;
266 
267 	mtx_lock(&pfi_unlnkdkifs_mtx);
268 	LIST_INSERT_HEAD(&V_pfi_unlinked_kifs, kif, pfik_list);
269 	mtx_unlock(&pfi_unlnkdkifs_mtx);
270 }
271 
272 void
273 pfi_kif_purge(void)
274 {
275 	struct pfi_kif *kif, *kif1;
276 
277 	/*
278 	 * Do naive mark-and-sweep garbage collecting of old kifs.
279 	 * Reference flag is raised by pf_purge_expired_states().
280 	 */
281 	mtx_lock(&pfi_unlnkdkifs_mtx);
282 	LIST_FOREACH_SAFE(kif, &V_pfi_unlinked_kifs, pfik_list, kif1) {
283 		if (!(kif->pfik_flags & PFI_IFLAG_REFS)) {
284 			LIST_REMOVE(kif, pfik_list);
285 			free(kif, PFI_MTYPE);
286 		} else
287 			kif->pfik_flags &= ~PFI_IFLAG_REFS;
288 	}
289 	mtx_unlock(&pfi_unlnkdkifs_mtx);
290 }
291 
292 int
293 pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif)
294 {
295 	struct ifg_list	*p;
296 
297 	if (rule_kif == NULL || rule_kif == packet_kif)
298 		return (1);
299 
300 	if (rule_kif->pfik_group != NULL) {
301 		IF_ADDR_RLOCK(packet_kif->pfik_ifp);
302 		CK_STAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
303 			if (p->ifgl_group == rule_kif->pfik_group) {
304 				IF_ADDR_RUNLOCK(packet_kif->pfik_ifp);
305 				return (1);
306 			}
307 		IF_ADDR_RUNLOCK(packet_kif->pfik_ifp);
308 	}
309 
310 
311 	return (0);
312 }
313 
314 static void
315 pfi_attach_ifnet(struct ifnet *ifp)
316 {
317 	struct pfi_kif *kif;
318 
319 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
320 
321 	PF_RULES_WLOCK();
322 	V_pfi_update++;
323 	kif = pfi_kif_attach(kif, ifp->if_xname);
324 
325 	kif->pfik_ifp = ifp;
326 	ifp->if_pf_kif = kif;
327 
328 	pfi_kif_update(kif);
329 	PF_RULES_WUNLOCK();
330 }
331 
332 static void
333 pfi_attach_ifgroup(struct ifg_group *ifg)
334 {
335 	struct pfi_kif *kif;
336 
337 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
338 
339 	PF_RULES_WLOCK();
340 	V_pfi_update++;
341 	kif = pfi_kif_attach(kif, ifg->ifg_group);
342 
343 	kif->pfik_group = ifg;
344 	ifg->ifg_pf_kif = kif;
345 	PF_RULES_WUNLOCK();
346 }
347 
348 int
349 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
350 {
351 	switch (af) {
352 #ifdef INET
353 	case AF_INET:
354 		switch (dyn->pfid_acnt4) {
355 		case 0:
356 			return (0);
357 		case 1:
358 			return (PF_MATCHA(0, &dyn->pfid_addr4,
359 			    &dyn->pfid_mask4, a, AF_INET));
360 		default:
361 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
362 		}
363 		break;
364 #endif /* INET */
365 #ifdef INET6
366 	case AF_INET6:
367 		switch (dyn->pfid_acnt6) {
368 		case 0:
369 			return (0);
370 		case 1:
371 			return (PF_MATCHA(0, &dyn->pfid_addr6,
372 			    &dyn->pfid_mask6, a, AF_INET6));
373 		default:
374 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
375 		}
376 		break;
377 #endif /* INET6 */
378 	default:
379 		return (0);
380 	}
381 }
382 
383 int
384 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
385 {
386 	struct pfi_dynaddr	*dyn;
387 	char			 tblname[PF_TABLE_NAME_SIZE];
388 	struct pf_ruleset	*ruleset = NULL;
389 	struct pfi_kif		*kif;
390 	int			 rv = 0;
391 
392 	PF_RULES_WASSERT();
393 	KASSERT(aw->type == PF_ADDR_DYNIFTL, ("%s: type %u",
394 	    __func__, aw->type));
395 	KASSERT(aw->p.dyn == NULL, ("%s: dyn is %p", __func__, aw->p.dyn));
396 
397 	if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL)
398 		return (ENOMEM);
399 
400 	if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) {
401 		free(dyn, PFI_MTYPE);
402 		return (ENOMEM);
403 	}
404 
405 	if (!strcmp(aw->v.ifname, "self"))
406 		dyn->pfid_kif = pfi_kif_attach(kif, IFG_ALL);
407 	else
408 		dyn->pfid_kif = pfi_kif_attach(kif, aw->v.ifname);
409 	pfi_kif_ref(dyn->pfid_kif);
410 
411 	dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
412 	if (af == AF_INET && dyn->pfid_net == 32)
413 		dyn->pfid_net = 128;
414 	strlcpy(tblname, aw->v.ifname, sizeof(tblname));
415 	if (aw->iflags & PFI_AFLAG_NETWORK)
416 		strlcat(tblname, ":network", sizeof(tblname));
417 	if (aw->iflags & PFI_AFLAG_BROADCAST)
418 		strlcat(tblname, ":broadcast", sizeof(tblname));
419 	if (aw->iflags & PFI_AFLAG_PEER)
420 		strlcat(tblname, ":peer", sizeof(tblname));
421 	if (aw->iflags & PFI_AFLAG_NOALIAS)
422 		strlcat(tblname, ":0", sizeof(tblname));
423 	if (dyn->pfid_net != 128)
424 		snprintf(tblname + strlen(tblname),
425 		    sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
426 	if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
427 		rv = ENOMEM;
428 		goto _bad;
429 	}
430 
431 	if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
432 		rv = ENOMEM;
433 		goto _bad;
434 	}
435 
436 	dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
437 	dyn->pfid_iflags = aw->iflags;
438 	dyn->pfid_af = af;
439 
440 	TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
441 	aw->p.dyn = dyn;
442 	pfi_kif_update(dyn->pfid_kif);
443 
444 	return (0);
445 
446 _bad:
447 	if (dyn->pfid_kt != NULL)
448 		pfr_detach_table(dyn->pfid_kt);
449 	if (ruleset != NULL)
450 		pf_remove_if_empty_ruleset(ruleset);
451 	if (dyn->pfid_kif != NULL)
452 		pfi_kif_unref(dyn->pfid_kif);
453 	free(dyn, PFI_MTYPE);
454 
455 	return (rv);
456 }
457 
458 static void
459 pfi_kif_update(struct pfi_kif *kif)
460 {
461 	struct ifg_list		*ifgl;
462 	struct pfi_dynaddr	*p;
463 
464 	PF_RULES_WASSERT();
465 
466 	/* update all dynaddr */
467 	TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
468 		pfi_dynaddr_update(p);
469 
470 	/* again for all groups kif is member of */
471 	if (kif->pfik_ifp != NULL) {
472 		IF_ADDR_RLOCK(kif->pfik_ifp);
473 		CK_STAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
474 			pfi_kif_update((struct pfi_kif *)
475 			    ifgl->ifgl_group->ifg_pf_kif);
476 		IF_ADDR_RUNLOCK(kif->pfik_ifp);
477 	}
478 }
479 
480 static void
481 pfi_dynaddr_update(struct pfi_dynaddr *dyn)
482 {
483 	struct pfi_kif		*kif;
484 	struct pfr_ktable	*kt;
485 
486 	PF_RULES_WASSERT();
487 	KASSERT(dyn && dyn->pfid_kif && dyn->pfid_kt,
488 	    ("%s: bad argument", __func__));
489 
490 	kif = dyn->pfid_kif;
491 	kt = dyn->pfid_kt;
492 
493 	if (kt->pfrkt_larg != V_pfi_update) {
494 		/* this table needs to be brought up-to-date */
495 		pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
496 		kt->pfrkt_larg = V_pfi_update;
497 	}
498 	pfr_dynaddr_update(kt, dyn);
499 }
500 
501 static void
502 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
503 {
504 	int			 e, size2 = 0;
505 	struct ifg_member	*ifgm;
506 
507 	V_pfi_buffer_cnt = 0;
508 
509 	if (kif->pfik_ifp != NULL)
510 		pfi_instance_add(kif->pfik_ifp, net, flags);
511 	else if (kif->pfik_group != NULL) {
512 		IFNET_RLOCK_NOSLEEP();
513 		CK_STAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
514 			pfi_instance_add(ifgm->ifgm_ifp, net, flags);
515 		IFNET_RUNLOCK_NOSLEEP();
516 	}
517 
518 	if ((e = pfr_set_addrs(&kt->pfrkt_t, V_pfi_buffer, V_pfi_buffer_cnt, &size2,
519 	    NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
520 		printf("%s: cannot set %d new addresses into table %s: %d\n",
521 		    __func__, V_pfi_buffer_cnt, kt->pfrkt_name, e);
522 }
523 
524 static void
525 pfi_instance_add(struct ifnet *ifp, int net, int flags)
526 {
527 	struct ifaddr	*ia;
528 	int		 got4 = 0, got6 = 0;
529 	int		 net2, af;
530 
531 	IF_ADDR_RLOCK(ifp);
532 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
533 		if (ia->ifa_addr == NULL)
534 			continue;
535 		af = ia->ifa_addr->sa_family;
536 		if (af != AF_INET && af != AF_INET6)
537 			continue;
538 		/*
539 		 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
540 		 *      jump over addresses without a proper route to work
541 		 *      around a problem with ppp not fully removing the
542 		 *      address used during IPCP.
543 		 */
544 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
545 		    !(ia->ifa_flags & IFA_ROUTE) &&
546 		    (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
547 			continue;
548 		if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
549 			continue;
550 		if ((flags & PFI_AFLAG_BROADCAST) &&
551 		    !(ifp->if_flags & IFF_BROADCAST))
552 			continue;
553 		if ((flags & PFI_AFLAG_PEER) &&
554 		    !(ifp->if_flags & IFF_POINTOPOINT))
555 			continue;
556 		if ((flags & (PFI_AFLAG_NETWORK | PFI_AFLAG_NOALIAS)) &&
557 		    af == AF_INET6 &&
558 		    IN6_IS_ADDR_LINKLOCAL(
559 		    &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
560 			continue;
561 		if (flags & PFI_AFLAG_NOALIAS) {
562 			if (af == AF_INET && got4)
563 				continue;
564 			if (af == AF_INET6 && got6)
565 				continue;
566 		}
567 		if (af == AF_INET)
568 			got4 = 1;
569 		else if (af == AF_INET6)
570 			got6 = 1;
571 		net2 = net;
572 		if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
573 			if (af == AF_INET)
574 				net2 = pfi_unmask(&((struct sockaddr_in *)
575 				    ia->ifa_netmask)->sin_addr);
576 			else if (af == AF_INET6)
577 				net2 = pfi_unmask(&((struct sockaddr_in6 *)
578 				    ia->ifa_netmask)->sin6_addr);
579 		}
580 		if (af == AF_INET && net2 > 32)
581 			net2 = 32;
582 		if (flags & PFI_AFLAG_BROADCAST)
583 			pfi_address_add(ia->ifa_broadaddr, af, net2);
584 		else if (flags & PFI_AFLAG_PEER)
585 			pfi_address_add(ia->ifa_dstaddr, af, net2);
586 		else
587 			pfi_address_add(ia->ifa_addr, af, net2);
588 	}
589 	IF_ADDR_RUNLOCK(ifp);
590 }
591 
592 static void
593 pfi_address_add(struct sockaddr *sa, int af, int net)
594 {
595 	struct pfr_addr	*p;
596 	int		 i;
597 
598 	if (V_pfi_buffer_cnt >= V_pfi_buffer_max) {
599 		int		 new_max = V_pfi_buffer_max * 2;
600 
601 		if (new_max > PFI_BUFFER_MAX) {
602 			printf("%s: address buffer full (%d/%d)\n", __func__,
603 			    V_pfi_buffer_cnt, PFI_BUFFER_MAX);
604 			return;
605 		}
606 		p = malloc(new_max * sizeof(*V_pfi_buffer), PFI_MTYPE,
607 		    M_NOWAIT);
608 		if (p == NULL) {
609 			printf("%s: no memory to grow buffer (%d/%d)\n",
610 			    __func__, V_pfi_buffer_cnt, PFI_BUFFER_MAX);
611 			return;
612 		}
613 		memcpy(p, V_pfi_buffer, V_pfi_buffer_max * sizeof(*V_pfi_buffer));
614 		/* no need to zero buffer */
615 		free(V_pfi_buffer, PFI_MTYPE);
616 		V_pfi_buffer = p;
617 		V_pfi_buffer_max = new_max;
618 	}
619 	if (af == AF_INET && net > 32)
620 		net = 128;
621 	p = V_pfi_buffer + V_pfi_buffer_cnt++;
622 	bzero(p, sizeof(*p));
623 	p->pfra_af = af;
624 	p->pfra_net = net;
625 	if (af == AF_INET)
626 		p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
627 	else if (af == AF_INET6) {
628 		p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
629 		if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
630 			p->pfra_ip6addr.s6_addr16[1] = 0;
631 	}
632 	/* mask network address bits */
633 	if (net < 128)
634 		((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
635 	for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
636 		((caddr_t)p)[i] = 0;
637 }
638 
639 void
640 pfi_dynaddr_remove(struct pfi_dynaddr *dyn)
641 {
642 
643 	KASSERT(dyn->pfid_kif != NULL, ("%s: null pfid_kif", __func__));
644 	KASSERT(dyn->pfid_kt != NULL, ("%s: null pfid_kt", __func__));
645 
646 	TAILQ_REMOVE(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
647 	pfi_kif_unref(dyn->pfid_kif);
648 	pfr_detach_table(dyn->pfid_kt);
649 	free(dyn, PFI_MTYPE);
650 }
651 
652 void
653 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
654 {
655 
656 	KASSERT(aw->type == PF_ADDR_DYNIFTL,
657 	    ("%s: type %u", __func__, aw->type));
658 
659 	if (aw->p.dyn == NULL || aw->p.dyn->pfid_kif == NULL)
660 		return;
661 	aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
662 }
663 
664 static int
665 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
666 {
667 	return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
668 }
669 
670 void
671 pfi_update_status(const char *name, struct pf_status *pfs)
672 {
673 	struct pfi_kif		*p;
674 	struct pfi_kif_cmp	 key;
675 	struct ifg_member	 p_member, *ifgm;
676 	CK_STAILQ_HEAD(, ifg_member) ifg_members;
677 	int			 i, j, k;
678 
679 	strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
680 	p = RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&key);
681 	if (p == NULL)
682 		return;
683 
684 	if (p->pfik_group != NULL) {
685 		bcopy(&p->pfik_group->ifg_members, &ifg_members,
686 		    sizeof(ifg_members));
687 	} else {
688 		/* build a temporary list for p only */
689 		bzero(&p_member, sizeof(p_member));
690 		p_member.ifgm_ifp = p->pfik_ifp;
691 		CK_STAILQ_INIT(&ifg_members);
692 		CK_STAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next);
693 	}
694 	if (pfs) {
695 		bzero(pfs->pcounters, sizeof(pfs->pcounters));
696 		bzero(pfs->bcounters, sizeof(pfs->bcounters));
697 	}
698 	CK_STAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) {
699 		if (ifgm->ifgm_ifp == NULL || ifgm->ifgm_ifp->if_pf_kif == NULL)
700 			continue;
701 		p = (struct pfi_kif *)ifgm->ifgm_ifp->if_pf_kif;
702 
703 		/* just clear statistics */
704 		if (pfs == NULL) {
705 			bzero(p->pfik_packets, sizeof(p->pfik_packets));
706 			bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
707 			p->pfik_tzero = time_second;
708 			continue;
709 		}
710 		for (i = 0; i < 2; i++)
711 			for (j = 0; j < 2; j++)
712 				for (k = 0; k < 2; k++) {
713 					pfs->pcounters[i][j][k] +=
714 						p->pfik_packets[i][j][k];
715 					pfs->bcounters[i][j] +=
716 						p->pfik_bytes[i][j][k];
717 				}
718 	}
719 }
720 
721 void
722 pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
723 {
724 	struct pfi_kif	*p, *nextp;
725 	int		 n = 0;
726 
727 	for (p = RB_MIN(pfi_ifhead, &V_pfi_ifs); p; p = nextp) {
728 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
729 		if (pfi_skip_if(name, p))
730 			continue;
731 		if (*size <= n++)
732 			break;
733 		if (!p->pfik_tzero)
734 			p->pfik_tzero = time_second;
735 		bcopy(p, buf++, sizeof(*buf));
736 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
737 	}
738 	*size = n;
739 }
740 
741 static int
742 pfi_skip_if(const char *filter, struct pfi_kif *p)
743 {
744 	struct ifg_list *i;
745 	int	n;
746 
747 	if (filter == NULL || !*filter)
748 		return (0);
749 	if (!strcmp(p->pfik_name, filter))
750 		return (0);	/* exact match */
751 	n = strlen(filter);
752 	if (n < 1 || n >= IFNAMSIZ)
753 		return (1);	/* sanity check */
754 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
755 		return (1);	/* group names may not end in a digit */
756 	if (p->pfik_ifp != NULL) {
757 		IF_ADDR_RLOCK(p->pfik_ifp);
758 		CK_STAILQ_FOREACH(i, &p->pfik_ifp->if_groups, ifgl_next) {
759 			if (!strncmp(i->ifgl_group->ifg_group, filter,
760 			      IFNAMSIZ)) {
761 				IF_ADDR_RUNLOCK(p->pfik_ifp);
762 				return (0); /* iface is in group "filter" */
763 			}
764 		}
765 		IF_ADDR_RUNLOCK(p->pfik_ifp);
766 	}
767 	return (1);
768 }
769 
770 int
771 pfi_set_flags(const char *name, int flags)
772 {
773 	struct pfi_kif	*p;
774 
775 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
776 		if (pfi_skip_if(name, p))
777 			continue;
778 		p->pfik_flags |= flags;
779 	}
780 	return (0);
781 }
782 
783 int
784 pfi_clear_flags(const char *name, int flags)
785 {
786 	struct pfi_kif	*p;
787 
788 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
789 		if (pfi_skip_if(name, p))
790 			continue;
791 		p->pfik_flags &= ~flags;
792 	}
793 	return (0);
794 }
795 
796 /* from pf_print_state.c */
797 static int
798 pfi_unmask(void *addr)
799 {
800 	struct pf_addr *m = addr;
801 	int i = 31, j = 0, b = 0;
802 	u_int32_t tmp;
803 
804 	while (j < 4 && m->addr32[j] == 0xffffffff) {
805 		b += 32;
806 		j++;
807 	}
808 	if (j < 4) {
809 		tmp = ntohl(m->addr32[j]);
810 		for (i = 31; tmp & (1 << i); --i)
811 			b++;
812 	}
813 	return (b);
814 }
815 
816 static void
817 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
818 {
819 
820 	if (V_pf_vnet_active == 0) {
821 		/* Avoid teardown race in the least expensive way. */
822 		return;
823 	}
824 	pfi_attach_ifnet(ifp);
825 #ifdef ALTQ
826 	PF_RULES_WLOCK();
827 	pf_altq_ifnet_event(ifp, 0);
828 	PF_RULES_WUNLOCK();
829 #endif
830 }
831 
832 static void
833 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
834 {
835 	struct pfi_kif *kif = (struct pfi_kif *)ifp->if_pf_kif;
836 
837 	if (kif == NULL)
838 		return;
839 
840 	if (V_pf_vnet_active == 0) {
841 		/* Avoid teardown race in the least expensive way. */
842 		return;
843 	}
844 	PF_RULES_WLOCK();
845 	V_pfi_update++;
846 	pfi_kif_update(kif);
847 
848 	kif->pfik_ifp = NULL;
849 	ifp->if_pf_kif = NULL;
850 #ifdef ALTQ
851 	pf_altq_ifnet_event(ifp, 1);
852 #endif
853 	PF_RULES_WUNLOCK();
854 }
855 
856 static void
857 pfi_attach_group_event(void *arg __unused, struct ifg_group *ifg)
858 {
859 
860 	if (V_pf_vnet_active == 0) {
861 		/* Avoid teardown race in the least expensive way. */
862 		return;
863 	}
864 	pfi_attach_ifgroup(ifg);
865 }
866 
867 static void
868 pfi_change_group_event(void *arg __unused, char *gname)
869 {
870 	struct pfi_kif *kif;
871 
872 	if (V_pf_vnet_active == 0) {
873 		/* Avoid teardown race in the least expensive way. */
874 		return;
875 	}
876 
877 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
878 	PF_RULES_WLOCK();
879 	V_pfi_update++;
880 	kif = pfi_kif_attach(kif, gname);
881 	pfi_kif_update(kif);
882 	PF_RULES_WUNLOCK();
883 }
884 
885 static void
886 pfi_detach_group_event(void *arg __unused, struct ifg_group *ifg)
887 {
888 	struct pfi_kif *kif = (struct pfi_kif *)ifg->ifg_pf_kif;
889 
890 	if (kif == NULL)
891 		return;
892 
893 	if (V_pf_vnet_active == 0) {
894 		/* Avoid teardown race in the least expensive way. */
895 		return;
896 	}
897 	PF_RULES_WLOCK();
898 	V_pfi_update++;
899 
900 	kif->pfik_group = NULL;
901 	ifg->ifg_pf_kif = NULL;
902 	PF_RULES_WUNLOCK();
903 }
904 
905 static void
906 pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp)
907 {
908 
909 	KASSERT(ifp, ("ifp == NULL"));
910 
911 	if (ifp->if_pf_kif == NULL)
912 		return;
913 
914 	if (V_pf_vnet_active == 0) {
915 		/* Avoid teardown race in the least expensive way. */
916 		return;
917 	}
918 	PF_RULES_WLOCK();
919 	if (ifp->if_pf_kif) {
920 		V_pfi_update++;
921 		pfi_kif_update(ifp->if_pf_kif);
922 	}
923 	PF_RULES_WUNLOCK();
924 }
925