xref: /freebsd/sys/netpfil/pf/pf_if.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
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 static VNET_DEFINE(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 static VNET_DEFINE(struct pfr_addr *, pfi_buffer);
66 static VNET_DEFINE(int, pfi_buffer_cnt);
67 static VNET_DEFINE(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 static VNET_DEFINE(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 static VNET_DEFINE(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 		/* XXXGL: locking? */
302 		CK_STAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
303 			if (p->ifgl_group == rule_kif->pfik_group)
304 				return (1);
305 
306 	return (0);
307 }
308 
309 static void
310 pfi_attach_ifnet(struct ifnet *ifp)
311 {
312 	struct pfi_kif *kif;
313 
314 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
315 
316 	PF_RULES_WLOCK();
317 	V_pfi_update++;
318 	kif = pfi_kif_attach(kif, ifp->if_xname);
319 
320 	kif->pfik_ifp = ifp;
321 	ifp->if_pf_kif = kif;
322 
323 	pfi_kif_update(kif);
324 	PF_RULES_WUNLOCK();
325 }
326 
327 static void
328 pfi_attach_ifgroup(struct ifg_group *ifg)
329 {
330 	struct pfi_kif *kif;
331 
332 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
333 
334 	PF_RULES_WLOCK();
335 	V_pfi_update++;
336 	kif = pfi_kif_attach(kif, ifg->ifg_group);
337 
338 	kif->pfik_group = ifg;
339 	ifg->ifg_pf_kif = kif;
340 	PF_RULES_WUNLOCK();
341 }
342 
343 int
344 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
345 {
346 	switch (af) {
347 #ifdef INET
348 	case AF_INET:
349 		switch (dyn->pfid_acnt4) {
350 		case 0:
351 			return (0);
352 		case 1:
353 			return (PF_MATCHA(0, &dyn->pfid_addr4,
354 			    &dyn->pfid_mask4, a, AF_INET));
355 		default:
356 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
357 		}
358 		break;
359 #endif /* INET */
360 #ifdef INET6
361 	case AF_INET6:
362 		switch (dyn->pfid_acnt6) {
363 		case 0:
364 			return (0);
365 		case 1:
366 			return (PF_MATCHA(0, &dyn->pfid_addr6,
367 			    &dyn->pfid_mask6, a, AF_INET6));
368 		default:
369 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
370 		}
371 		break;
372 #endif /* INET6 */
373 	default:
374 		return (0);
375 	}
376 }
377 
378 int
379 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
380 {
381 	struct pfi_dynaddr	*dyn;
382 	char			 tblname[PF_TABLE_NAME_SIZE];
383 	struct pf_ruleset	*ruleset = NULL;
384 	struct pfi_kif		*kif;
385 	int			 rv = 0;
386 
387 	PF_RULES_WASSERT();
388 	KASSERT(aw->type == PF_ADDR_DYNIFTL, ("%s: type %u",
389 	    __func__, aw->type));
390 	KASSERT(aw->p.dyn == NULL, ("%s: dyn is %p", __func__, aw->p.dyn));
391 
392 	if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL)
393 		return (ENOMEM);
394 
395 	if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) {
396 		free(dyn, PFI_MTYPE);
397 		return (ENOMEM);
398 	}
399 
400 	if (!strcmp(aw->v.ifname, "self"))
401 		dyn->pfid_kif = pfi_kif_attach(kif, IFG_ALL);
402 	else
403 		dyn->pfid_kif = pfi_kif_attach(kif, aw->v.ifname);
404 	pfi_kif_ref(dyn->pfid_kif);
405 
406 	dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
407 	if (af == AF_INET && dyn->pfid_net == 32)
408 		dyn->pfid_net = 128;
409 	strlcpy(tblname, aw->v.ifname, sizeof(tblname));
410 	if (aw->iflags & PFI_AFLAG_NETWORK)
411 		strlcat(tblname, ":network", sizeof(tblname));
412 	if (aw->iflags & PFI_AFLAG_BROADCAST)
413 		strlcat(tblname, ":broadcast", sizeof(tblname));
414 	if (aw->iflags & PFI_AFLAG_PEER)
415 		strlcat(tblname, ":peer", sizeof(tblname));
416 	if (aw->iflags & PFI_AFLAG_NOALIAS)
417 		strlcat(tblname, ":0", sizeof(tblname));
418 	if (dyn->pfid_net != 128)
419 		snprintf(tblname + strlen(tblname),
420 		    sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
421 	if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
422 		rv = ENOMEM;
423 		goto _bad;
424 	}
425 
426 	if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
427 		rv = ENOMEM;
428 		goto _bad;
429 	}
430 
431 	dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
432 	dyn->pfid_iflags = aw->iflags;
433 	dyn->pfid_af = af;
434 
435 	TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
436 	aw->p.dyn = dyn;
437 	pfi_kif_update(dyn->pfid_kif);
438 
439 	return (0);
440 
441 _bad:
442 	if (dyn->pfid_kt != NULL)
443 		pfr_detach_table(dyn->pfid_kt);
444 	if (ruleset != NULL)
445 		pf_remove_if_empty_ruleset(ruleset);
446 	if (dyn->pfid_kif != NULL)
447 		pfi_kif_unref(dyn->pfid_kif);
448 	free(dyn, PFI_MTYPE);
449 
450 	return (rv);
451 }
452 
453 static void
454 pfi_kif_update(struct pfi_kif *kif)
455 {
456 	struct ifg_list		*ifgl;
457 	struct pfi_dynaddr	*p;
458 
459 	PF_RULES_WASSERT();
460 
461 	/* update all dynaddr */
462 	TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
463 		pfi_dynaddr_update(p);
464 
465 	/* again for all groups kif is member of */
466 	if (kif->pfik_ifp != NULL) {
467 		IF_ADDR_RLOCK(kif->pfik_ifp);
468 		CK_STAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
469 			pfi_kif_update((struct pfi_kif *)
470 			    ifgl->ifgl_group->ifg_pf_kif);
471 		IF_ADDR_RUNLOCK(kif->pfik_ifp);
472 	}
473 }
474 
475 static void
476 pfi_dynaddr_update(struct pfi_dynaddr *dyn)
477 {
478 	struct pfi_kif		*kif;
479 	struct pfr_ktable	*kt;
480 
481 	PF_RULES_WASSERT();
482 	KASSERT(dyn && dyn->pfid_kif && dyn->pfid_kt,
483 	    ("%s: bad argument", __func__));
484 
485 	kif = dyn->pfid_kif;
486 	kt = dyn->pfid_kt;
487 
488 	if (kt->pfrkt_larg != V_pfi_update) {
489 		/* this table needs to be brought up-to-date */
490 		pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
491 		kt->pfrkt_larg = V_pfi_update;
492 	}
493 	pfr_dynaddr_update(kt, dyn);
494 }
495 
496 static void
497 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
498 {
499 	int			 e, size2 = 0;
500 	struct ifg_member	*ifgm;
501 
502 	V_pfi_buffer_cnt = 0;
503 
504 	if (kif->pfik_ifp != NULL)
505 		pfi_instance_add(kif->pfik_ifp, net, flags);
506 	else if (kif->pfik_group != NULL) {
507 		IFNET_RLOCK_NOSLEEP();
508 		CK_STAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
509 			pfi_instance_add(ifgm->ifgm_ifp, net, flags);
510 		IFNET_RUNLOCK_NOSLEEP();
511 	}
512 
513 	if ((e = pfr_set_addrs(&kt->pfrkt_t, V_pfi_buffer, V_pfi_buffer_cnt, &size2,
514 	    NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
515 		printf("%s: cannot set %d new addresses into table %s: %d\n",
516 		    __func__, V_pfi_buffer_cnt, kt->pfrkt_name, e);
517 }
518 
519 static void
520 pfi_instance_add(struct ifnet *ifp, int net, int flags)
521 {
522 	struct ifaddr	*ia;
523 	int		 got4 = 0, got6 = 0;
524 	int		 net2, af;
525 
526 	IF_ADDR_RLOCK(ifp);
527 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
528 		if (ia->ifa_addr == NULL)
529 			continue;
530 		af = ia->ifa_addr->sa_family;
531 		if (af != AF_INET && af != AF_INET6)
532 			continue;
533 		/*
534 		 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
535 		 *      jump over addresses without a proper route to work
536 		 *      around a problem with ppp not fully removing the
537 		 *      address used during IPCP.
538 		 */
539 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
540 		    !(ia->ifa_flags & IFA_ROUTE) &&
541 		    (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
542 			continue;
543 		if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
544 			continue;
545 		if ((flags & PFI_AFLAG_BROADCAST) &&
546 		    !(ifp->if_flags & IFF_BROADCAST))
547 			continue;
548 		if ((flags & PFI_AFLAG_PEER) &&
549 		    !(ifp->if_flags & IFF_POINTOPOINT))
550 			continue;
551 		if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
552 		    IN6_IS_ADDR_LINKLOCAL(
553 		    &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
554 			continue;
555 		if (flags & PFI_AFLAG_NOALIAS) {
556 			if (af == AF_INET && got4)
557 				continue;
558 			if (af == AF_INET6 && got6)
559 				continue;
560 		}
561 		if (af == AF_INET)
562 			got4 = 1;
563 		else if (af == AF_INET6)
564 			got6 = 1;
565 		net2 = net;
566 		if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
567 			if (af == AF_INET)
568 				net2 = pfi_unmask(&((struct sockaddr_in *)
569 				    ia->ifa_netmask)->sin_addr);
570 			else if (af == AF_INET6)
571 				net2 = pfi_unmask(&((struct sockaddr_in6 *)
572 				    ia->ifa_netmask)->sin6_addr);
573 		}
574 		if (af == AF_INET && net2 > 32)
575 			net2 = 32;
576 		if (flags & PFI_AFLAG_BROADCAST)
577 			pfi_address_add(ia->ifa_broadaddr, af, net2);
578 		else if (flags & PFI_AFLAG_PEER)
579 			pfi_address_add(ia->ifa_dstaddr, af, net2);
580 		else
581 			pfi_address_add(ia->ifa_addr, af, net2);
582 	}
583 	IF_ADDR_RUNLOCK(ifp);
584 }
585 
586 static void
587 pfi_address_add(struct sockaddr *sa, int af, int net)
588 {
589 	struct pfr_addr	*p;
590 	int		 i;
591 
592 	if (V_pfi_buffer_cnt >= V_pfi_buffer_max) {
593 		int		 new_max = V_pfi_buffer_max * 2;
594 
595 		if (new_max > PFI_BUFFER_MAX) {
596 			printf("%s: address buffer full (%d/%d)\n", __func__,
597 			    V_pfi_buffer_cnt, PFI_BUFFER_MAX);
598 			return;
599 		}
600 		p = malloc(new_max * sizeof(*V_pfi_buffer), PFI_MTYPE,
601 		    M_NOWAIT);
602 		if (p == NULL) {
603 			printf("%s: no memory to grow buffer (%d/%d)\n",
604 			    __func__, V_pfi_buffer_cnt, PFI_BUFFER_MAX);
605 			return;
606 		}
607 		memcpy(p, V_pfi_buffer, V_pfi_buffer_max * sizeof(*V_pfi_buffer));
608 		/* no need to zero buffer */
609 		free(V_pfi_buffer, PFI_MTYPE);
610 		V_pfi_buffer = p;
611 		V_pfi_buffer_max = new_max;
612 	}
613 	if (af == AF_INET && net > 32)
614 		net = 128;
615 	p = V_pfi_buffer + V_pfi_buffer_cnt++;
616 	bzero(p, sizeof(*p));
617 	p->pfra_af = af;
618 	p->pfra_net = net;
619 	if (af == AF_INET)
620 		p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
621 	else if (af == AF_INET6) {
622 		p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
623 		if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
624 			p->pfra_ip6addr.s6_addr16[1] = 0;
625 	}
626 	/* mask network address bits */
627 	if (net < 128)
628 		((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
629 	for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
630 		((caddr_t)p)[i] = 0;
631 }
632 
633 void
634 pfi_dynaddr_remove(struct pfi_dynaddr *dyn)
635 {
636 
637 	KASSERT(dyn->pfid_kif != NULL, ("%s: null pfid_kif", __func__));
638 	KASSERT(dyn->pfid_kt != NULL, ("%s: null pfid_kt", __func__));
639 
640 	TAILQ_REMOVE(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
641 	pfi_kif_unref(dyn->pfid_kif);
642 	pfr_detach_table(dyn->pfid_kt);
643 	free(dyn, PFI_MTYPE);
644 }
645 
646 void
647 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
648 {
649 
650 	KASSERT(aw->type == PF_ADDR_DYNIFTL,
651 	    ("%s: type %u", __func__, aw->type));
652 
653 	if (aw->p.dyn == NULL || aw->p.dyn->pfid_kif == NULL)
654 		return;
655 	aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
656 }
657 
658 static int
659 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
660 {
661 	return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
662 }
663 
664 void
665 pfi_update_status(const char *name, struct pf_status *pfs)
666 {
667 	struct pfi_kif		*p;
668 	struct pfi_kif_cmp	 key;
669 	struct ifg_member	 p_member, *ifgm;
670 	CK_STAILQ_HEAD(, ifg_member) ifg_members;
671 	int			 i, j, k;
672 
673 	strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
674 	p = RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&key);
675 	if (p == NULL)
676 		return;
677 
678 	if (p->pfik_group != NULL) {
679 		bcopy(&p->pfik_group->ifg_members, &ifg_members,
680 		    sizeof(ifg_members));
681 	} else {
682 		/* build a temporary list for p only */
683 		bzero(&p_member, sizeof(p_member));
684 		p_member.ifgm_ifp = p->pfik_ifp;
685 		CK_STAILQ_INIT(&ifg_members);
686 		CK_STAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next);
687 	}
688 	if (pfs) {
689 		bzero(pfs->pcounters, sizeof(pfs->pcounters));
690 		bzero(pfs->bcounters, sizeof(pfs->bcounters));
691 	}
692 	CK_STAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) {
693 		if (ifgm->ifgm_ifp == NULL || ifgm->ifgm_ifp->if_pf_kif == NULL)
694 			continue;
695 		p = (struct pfi_kif *)ifgm->ifgm_ifp->if_pf_kif;
696 
697 		/* just clear statistics */
698 		if (pfs == NULL) {
699 			bzero(p->pfik_packets, sizeof(p->pfik_packets));
700 			bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
701 			p->pfik_tzero = time_second;
702 			continue;
703 		}
704 		for (i = 0; i < 2; i++)
705 			for (j = 0; j < 2; j++)
706 				for (k = 0; k < 2; k++) {
707 					pfs->pcounters[i][j][k] +=
708 						p->pfik_packets[i][j][k];
709 					pfs->bcounters[i][j] +=
710 						p->pfik_bytes[i][j][k];
711 				}
712 	}
713 }
714 
715 void
716 pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
717 {
718 	struct pfi_kif	*p, *nextp;
719 	int		 n = 0;
720 
721 	for (p = RB_MIN(pfi_ifhead, &V_pfi_ifs); p; p = nextp) {
722 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
723 		if (pfi_skip_if(name, p))
724 			continue;
725 		if (*size <= n++)
726 			break;
727 		if (!p->pfik_tzero)
728 			p->pfik_tzero = time_second;
729 		bcopy(p, buf++, sizeof(*buf));
730 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
731 	}
732 	*size = n;
733 }
734 
735 static int
736 pfi_skip_if(const char *filter, struct pfi_kif *p)
737 {
738 	int	n;
739 
740 	if (filter == NULL || !*filter)
741 		return (0);
742 	if (!strcmp(p->pfik_name, filter))
743 		return (0);	/* exact match */
744 	n = strlen(filter);
745 	if (n < 1 || n >= IFNAMSIZ)
746 		return (1);	/* sanity check */
747 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
748 		return (1);	/* only do exact match in that case */
749 	if (strncmp(p->pfik_name, filter, n))
750 		return (1);	/* prefix doesn't match */
751 	return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
752 }
753 
754 int
755 pfi_set_flags(const char *name, int flags)
756 {
757 	struct pfi_kif	*p;
758 
759 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
760 		if (pfi_skip_if(name, p))
761 			continue;
762 		p->pfik_flags |= flags;
763 	}
764 	return (0);
765 }
766 
767 int
768 pfi_clear_flags(const char *name, int flags)
769 {
770 	struct pfi_kif	*p;
771 
772 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
773 		if (pfi_skip_if(name, p))
774 			continue;
775 		p->pfik_flags &= ~flags;
776 	}
777 	return (0);
778 }
779 
780 /* from pf_print_state.c */
781 static int
782 pfi_unmask(void *addr)
783 {
784 	struct pf_addr *m = addr;
785 	int i = 31, j = 0, b = 0;
786 	u_int32_t tmp;
787 
788 	while (j < 4 && m->addr32[j] == 0xffffffff) {
789 		b += 32;
790 		j++;
791 	}
792 	if (j < 4) {
793 		tmp = ntohl(m->addr32[j]);
794 		for (i = 31; tmp & (1 << i); --i)
795 			b++;
796 	}
797 	return (b);
798 }
799 
800 static void
801 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
802 {
803 
804 	if (V_pf_vnet_active == 0) {
805 		/* Avoid teardown race in the least expensive way. */
806 		return;
807 	}
808 	pfi_attach_ifnet(ifp);
809 #ifdef ALTQ
810 	PF_RULES_WLOCK();
811 	pf_altq_ifnet_event(ifp, 0);
812 	PF_RULES_WUNLOCK();
813 #endif
814 }
815 
816 static void
817 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
818 {
819 	struct pfi_kif *kif = (struct pfi_kif *)ifp->if_pf_kif;
820 
821 	if (kif == NULL)
822 		return;
823 
824 	if (V_pf_vnet_active == 0) {
825 		/* Avoid teardown race in the least expensive way. */
826 		return;
827 	}
828 	PF_RULES_WLOCK();
829 	V_pfi_update++;
830 	pfi_kif_update(kif);
831 
832 	kif->pfik_ifp = NULL;
833 	ifp->if_pf_kif = NULL;
834 #ifdef ALTQ
835 	pf_altq_ifnet_event(ifp, 1);
836 #endif
837 	PF_RULES_WUNLOCK();
838 }
839 
840 static void
841 pfi_attach_group_event(void *arg __unused, struct ifg_group *ifg)
842 {
843 
844 	if (V_pf_vnet_active == 0) {
845 		/* Avoid teardown race in the least expensive way. */
846 		return;
847 	}
848 	pfi_attach_ifgroup(ifg);
849 }
850 
851 static void
852 pfi_change_group_event(void *arg __unused, char *gname)
853 {
854 	struct pfi_kif *kif;
855 
856 	if (V_pf_vnet_active == 0) {
857 		/* Avoid teardown race in the least expensive way. */
858 		return;
859 	}
860 
861 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
862 	PF_RULES_WLOCK();
863 	V_pfi_update++;
864 	kif = pfi_kif_attach(kif, gname);
865 	pfi_kif_update(kif);
866 	PF_RULES_WUNLOCK();
867 }
868 
869 static void
870 pfi_detach_group_event(void *arg __unused, struct ifg_group *ifg)
871 {
872 	struct pfi_kif *kif = (struct pfi_kif *)ifg->ifg_pf_kif;
873 
874 	if (kif == NULL)
875 		return;
876 
877 	if (V_pf_vnet_active == 0) {
878 		/* Avoid teardown race in the least expensive way. */
879 		return;
880 	}
881 	PF_RULES_WLOCK();
882 	V_pfi_update++;
883 
884 	kif->pfik_group = NULL;
885 	ifg->ifg_pf_kif = NULL;
886 	PF_RULES_WUNLOCK();
887 }
888 
889 static void
890 pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp)
891 {
892 	if (ifp->if_pf_kif == NULL)
893 		return;
894 
895 	if (V_pf_vnet_active == 0) {
896 		/* Avoid teardown race in the least expensive way. */
897 		return;
898 	}
899 	PF_RULES_WLOCK();
900 	if (ifp && ifp->if_pf_kif) {
901 		V_pfi_update++;
902 		pfi_kif_update(ifp->if_pf_kif);
903 	}
904 	PF_RULES_WUNLOCK();
905 }
906