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