xref: /freebsd/sys/netpfil/pf/pf_if.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2001 Daniel Hartmeier
3  * Copyright (c) 2003 Cedric Berger
4  * Copyright (c) 2005 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 2005 Ryan McBride <mcbride@openbsd.org>
6  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  *    - Redistributions of source code must retain the above copyright
14  *      notice, this list of conditions and the following disclaimer.
15  *    - Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials provided
18  *      with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  *	$OpenBSD: pf_if.c,v 1.54 2008/06/14 16:55:28 mk Exp $
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mbuf.h>
46 #include <sys/rwlock.h>
47 #include <sys/socket.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/vnet.h>
52 #include <net/pfvar.h>
53 #include <net/route.h>
54 
55 VNET_DEFINE(struct pfi_kif *,	 pfi_all);
56 static VNET_DEFINE(long, pfi_update);
57 #define	V_pfi_update	VNET(pfi_update)
58 #define PFI_BUFFER_MAX	0x10000
59 
60 static VNET_DEFINE(struct pfr_addr *, pfi_buffer);
61 static VNET_DEFINE(int, pfi_buffer_cnt);
62 static VNET_DEFINE(int,	pfi_buffer_max);
63 #define	V_pfi_buffer		 VNET(pfi_buffer)
64 #define	V_pfi_buffer_cnt	 VNET(pfi_buffer_cnt)
65 #define	V_pfi_buffer_max	 VNET(pfi_buffer_max)
66 
67 eventhandler_tag	 pfi_attach_cookie;
68 eventhandler_tag	 pfi_detach_cookie;
69 eventhandler_tag	 pfi_attach_group_cookie;
70 eventhandler_tag	 pfi_change_group_cookie;
71 eventhandler_tag	 pfi_detach_group_cookie;
72 eventhandler_tag	 pfi_ifaddr_event_cookie;
73 
74 static void	 pfi_attach_ifnet(struct ifnet *);
75 static void	 pfi_attach_ifgroup(struct ifg_group *);
76 
77 static void	 pfi_kif_update(struct pfi_kif *);
78 static void	 pfi_dynaddr_update(struct pfi_dynaddr *dyn);
79 static void	 pfi_table_update(struct pfr_ktable *, struct pfi_kif *, int,
80 		    int);
81 static void	 pfi_instance_add(struct ifnet *, int, int);
82 static void	 pfi_address_add(struct sockaddr *, int, int);
83 static int	 pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
84 static int	 pfi_skip_if(const char *, struct pfi_kif *);
85 static int	 pfi_unmask(void *);
86 static void	 pfi_attach_ifnet_event(void * __unused, struct ifnet *);
87 static void	 pfi_detach_ifnet_event(void * __unused, struct ifnet *);
88 static void	 pfi_attach_group_event(void *, struct ifg_group *);
89 static void	 pfi_change_group_event(void *, char *);
90 static void	 pfi_detach_group_event(void *, struct ifg_group *);
91 static void	 pfi_ifaddr_event(void * __unused, struct ifnet *);
92 
93 RB_HEAD(pfi_ifhead, pfi_kif);
94 static RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
95 static RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
96 static VNET_DEFINE(struct pfi_ifhead, pfi_ifs);
97 #define	V_pfi_ifs	VNET(pfi_ifs)
98 
99 #define	PFI_BUFFER_MAX		0x10000
100 MALLOC_DEFINE(PFI_MTYPE, "pf_ifnet", "pf(4) interface database");
101 
102 LIST_HEAD(pfi_list, pfi_kif);
103 static VNET_DEFINE(struct pfi_list, pfi_unlinked_kifs);
104 #define	V_pfi_unlinked_kifs	VNET(pfi_unlinked_kifs)
105 static struct mtx pfi_unlnkdkifs_mtx;
106 
107 void
108 pfi_initialize(void)
109 {
110 	struct ifg_group *ifg;
111 	struct ifnet *ifp;
112 	struct pfi_kif *kif;
113 
114 	V_pfi_buffer_max = 64;
115 	V_pfi_buffer = malloc(V_pfi_buffer_max * sizeof(*V_pfi_buffer),
116 	    PFI_MTYPE, M_WAITOK);
117 
118 	mtx_init(&pfi_unlnkdkifs_mtx, "pf unlinked interfaces", NULL, MTX_DEF);
119 
120 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
121 	PF_RULES_WLOCK();
122 	V_pfi_all = pfi_kif_attach(kif, IFG_ALL);
123 	PF_RULES_WUNLOCK();
124 
125 	IFNET_RLOCK();
126 	TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
127 		pfi_attach_ifgroup(ifg);
128 	TAILQ_FOREACH(ifp, &V_ifnet, if_link)
129 		pfi_attach_ifnet(ifp);
130 	IFNET_RUNLOCK();
131 
132 	pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event,
133 	    pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
134 	pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event,
135 	    pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
136 	pfi_attach_group_cookie = EVENTHANDLER_REGISTER(group_attach_event,
137 	    pfi_attach_group_event, curvnet, EVENTHANDLER_PRI_ANY);
138 	pfi_change_group_cookie = EVENTHANDLER_REGISTER(group_change_event,
139 	    pfi_change_group_event, curvnet, EVENTHANDLER_PRI_ANY);
140 	pfi_detach_group_cookie = EVENTHANDLER_REGISTER(group_detach_event,
141 	    pfi_detach_group_event, curvnet, EVENTHANDLER_PRI_ANY);
142 	pfi_ifaddr_event_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
143 	    pfi_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
144 }
145 
146 void
147 pfi_cleanup(void)
148 {
149 	struct pfi_kif *p;
150 
151 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie);
152 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie);
153 	EVENTHANDLER_DEREGISTER(group_attach_event, pfi_attach_group_cookie);
154 	EVENTHANDLER_DEREGISTER(group_change_event, pfi_change_group_cookie);
155 	EVENTHANDLER_DEREGISTER(group_detach_event, pfi_detach_group_cookie);
156 	EVENTHANDLER_DEREGISTER(ifaddr_event, pfi_ifaddr_event_cookie);
157 
158 	V_pfi_all = NULL;
159 	while ((p = RB_MIN(pfi_ifhead, &V_pfi_ifs))) {
160 		RB_REMOVE(pfi_ifhead, &V_pfi_ifs, p);
161 		free(p, PFI_MTYPE);
162 	}
163 
164 	while ((p = LIST_FIRST(&V_pfi_unlinked_kifs))) {
165 		LIST_REMOVE(p, pfik_list);
166 		free(p, PFI_MTYPE);
167 	}
168 
169 	mtx_destroy(&pfi_unlnkdkifs_mtx);
170 
171 	free(V_pfi_buffer, PFI_MTYPE);
172 }
173 
174 struct pfi_kif *
175 pfi_kif_find(const char *kif_name)
176 {
177 	struct pfi_kif_cmp s;
178 
179 	PF_RULES_ASSERT();
180 
181 	bzero(&s, sizeof(s));
182 	strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
183 
184 	return (RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&s));
185 }
186 
187 struct pfi_kif *
188 pfi_kif_attach(struct pfi_kif *kif, const char *kif_name)
189 {
190 	struct pfi_kif *kif1;
191 
192 	PF_RULES_WASSERT();
193 	KASSERT(kif != NULL, ("%s: null kif", __func__));
194 
195 	kif1 = pfi_kif_find(kif_name);
196 	if (kif1 != NULL) {
197 		free(kif, PFI_MTYPE);
198 		return (kif1);
199 	}
200 
201 	bzero(kif, sizeof(*kif));
202 	strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
203 	/*
204 	 * It seems that the value of time_second is in unintialzied state
205 	 * when pf sets interface statistics clear time in boot phase if pf
206 	 * was statically linked to kernel. Instead of setting the bogus
207 	 * time value have pfi_get_ifaces handle this case. In
208 	 * pfi_get_ifaces it uses time_second if it sees the time is 0.
209 	 */
210 	kif->pfik_tzero = time_second > 1 ? time_second : 0;
211 	TAILQ_INIT(&kif->pfik_dynaddrs);
212 
213 	RB_INSERT(pfi_ifhead, &V_pfi_ifs, kif);
214 
215 	return (kif);
216 }
217 
218 void
219 pfi_kif_ref(struct pfi_kif *kif)
220 {
221 
222 	PF_RULES_WASSERT();
223 	kif->pfik_rulerefs++;
224 }
225 
226 void
227 pfi_kif_unref(struct pfi_kif *kif)
228 {
229 
230 	PF_RULES_WASSERT();
231 	KASSERT(kif->pfik_rulerefs > 0, ("%s: %p has zero refs", __func__, kif));
232 
233 	kif->pfik_rulerefs--;
234 
235 	if (kif->pfik_rulerefs > 0)
236 		return;
237 
238 	/* kif referencing an existing ifnet or group should exist. */
239 	if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == V_pfi_all)
240 		return;
241 
242 	RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif);
243 
244 	kif->pfik_flags |= PFI_IFLAG_REFS;
245 
246 	mtx_lock(&pfi_unlnkdkifs_mtx);
247 	LIST_INSERT_HEAD(&V_pfi_unlinked_kifs, kif, pfik_list);
248 	mtx_unlock(&pfi_unlnkdkifs_mtx);
249 }
250 
251 void
252 pfi_kif_purge(void)
253 {
254 	struct pfi_kif *kif, *kif1;
255 
256 	/*
257 	 * Do naive mark-and-sweep garbage collecting of old kifs.
258 	 * Reference flag is raised by pf_purge_expired_states().
259 	 */
260 	mtx_lock(&pfi_unlnkdkifs_mtx);
261 	LIST_FOREACH_SAFE(kif, &V_pfi_unlinked_kifs, pfik_list, kif1) {
262 		if (!(kif->pfik_flags & PFI_IFLAG_REFS)) {
263 			LIST_REMOVE(kif, pfik_list);
264 			free(kif, PFI_MTYPE);
265 		} else
266 			kif->pfik_flags &= ~PFI_IFLAG_REFS;
267 	}
268 	mtx_unlock(&pfi_unlnkdkifs_mtx);
269 }
270 
271 int
272 pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif)
273 {
274 	struct ifg_list	*p;
275 
276 	if (rule_kif == NULL || rule_kif == packet_kif)
277 		return (1);
278 
279 	if (rule_kif->pfik_group != NULL)
280 		/* XXXGL: locking? */
281 		TAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
282 			if (p->ifgl_group == rule_kif->pfik_group)
283 				return (1);
284 
285 	return (0);
286 }
287 
288 static void
289 pfi_attach_ifnet(struct ifnet *ifp)
290 {
291 	struct pfi_kif *kif;
292 
293 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
294 
295 	PF_RULES_WLOCK();
296 	V_pfi_update++;
297 	kif = pfi_kif_attach(kif, ifp->if_xname);
298 
299 	kif->pfik_ifp = ifp;
300 	ifp->if_pf_kif = kif;
301 
302 	pfi_kif_update(kif);
303 	PF_RULES_WUNLOCK();
304 }
305 
306 static void
307 pfi_attach_ifgroup(struct ifg_group *ifg)
308 {
309 	struct pfi_kif *kif;
310 
311 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
312 
313 	PF_RULES_WLOCK();
314 	V_pfi_update++;
315 	kif = pfi_kif_attach(kif, ifg->ifg_group);
316 
317 	kif->pfik_group = ifg;
318 	ifg->ifg_pf_kif = kif;
319 	PF_RULES_WUNLOCK();
320 }
321 
322 int
323 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
324 {
325 	switch (af) {
326 #ifdef INET
327 	case AF_INET:
328 		switch (dyn->pfid_acnt4) {
329 		case 0:
330 			return (0);
331 		case 1:
332 			return (PF_MATCHA(0, &dyn->pfid_addr4,
333 			    &dyn->pfid_mask4, a, AF_INET));
334 		default:
335 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
336 		}
337 		break;
338 #endif /* INET */
339 #ifdef INET6
340 	case AF_INET6:
341 		switch (dyn->pfid_acnt6) {
342 		case 0:
343 			return (0);
344 		case 1:
345 			return (PF_MATCHA(0, &dyn->pfid_addr6,
346 			    &dyn->pfid_mask6, a, AF_INET6));
347 		default:
348 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
349 		}
350 		break;
351 #endif /* INET6 */
352 	default:
353 		return (0);
354 	}
355 }
356 
357 int
358 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
359 {
360 	struct pfi_dynaddr	*dyn;
361 	char			 tblname[PF_TABLE_NAME_SIZE];
362 	struct pf_ruleset	*ruleset = NULL;
363 	struct pfi_kif		*kif;
364 	int			 rv = 0;
365 
366 	PF_RULES_WASSERT();
367 	KASSERT(aw->type == PF_ADDR_DYNIFTL, ("%s: type %u",
368 	    __func__, aw->type));
369 	KASSERT(aw->p.dyn == NULL, ("%s: dyn is %p", __func__, aw->p.dyn));
370 
371 	if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL)
372 		return (ENOMEM);
373 
374 	if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) {
375 		free(dyn, PFI_MTYPE);
376 		return (ENOMEM);
377 	}
378 
379 	if (!strcmp(aw->v.ifname, "self"))
380 		dyn->pfid_kif = pfi_kif_attach(kif, IFG_ALL);
381 	else
382 		dyn->pfid_kif = pfi_kif_attach(kif, aw->v.ifname);
383 	pfi_kif_ref(dyn->pfid_kif);
384 
385 	dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
386 	if (af == AF_INET && dyn->pfid_net == 32)
387 		dyn->pfid_net = 128;
388 	strlcpy(tblname, aw->v.ifname, sizeof(tblname));
389 	if (aw->iflags & PFI_AFLAG_NETWORK)
390 		strlcat(tblname, ":network", sizeof(tblname));
391 	if (aw->iflags & PFI_AFLAG_BROADCAST)
392 		strlcat(tblname, ":broadcast", sizeof(tblname));
393 	if (aw->iflags & PFI_AFLAG_PEER)
394 		strlcat(tblname, ":peer", sizeof(tblname));
395 	if (aw->iflags & PFI_AFLAG_NOALIAS)
396 		strlcat(tblname, ":0", sizeof(tblname));
397 	if (dyn->pfid_net != 128)
398 		snprintf(tblname + strlen(tblname),
399 		    sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
400 	if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
401 		rv = ENOMEM;
402 		goto _bad;
403 	}
404 
405 	if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
406 		rv = ENOMEM;
407 		goto _bad;
408 	}
409 
410 	dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
411 	dyn->pfid_iflags = aw->iflags;
412 	dyn->pfid_af = af;
413 
414 	TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
415 	aw->p.dyn = dyn;
416 	pfi_kif_update(dyn->pfid_kif);
417 
418 	return (0);
419 
420 _bad:
421 	if (dyn->pfid_kt != NULL)
422 		pfr_detach_table(dyn->pfid_kt);
423 	if (ruleset != NULL)
424 		pf_remove_if_empty_ruleset(ruleset);
425 	if (dyn->pfid_kif != NULL)
426 		pfi_kif_unref(dyn->pfid_kif);
427 	free(dyn, PFI_MTYPE);
428 
429 	return (rv);
430 }
431 
432 static void
433 pfi_kif_update(struct pfi_kif *kif)
434 {
435 	struct ifg_list		*ifgl;
436 	struct pfi_dynaddr	*p;
437 
438 	PF_RULES_WASSERT();
439 
440 	/* update all dynaddr */
441 	TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
442 		pfi_dynaddr_update(p);
443 
444 	/* again for all groups kif is member of */
445 	if (kif->pfik_ifp != NULL) {
446 		IF_ADDR_RLOCK(kif->pfik_ifp);
447 		TAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
448 			pfi_kif_update((struct pfi_kif *)
449 			    ifgl->ifgl_group->ifg_pf_kif);
450 		IF_ADDR_RUNLOCK(kif->pfik_ifp);
451 	}
452 }
453 
454 static void
455 pfi_dynaddr_update(struct pfi_dynaddr *dyn)
456 {
457 	struct pfi_kif		*kif;
458 	struct pfr_ktable	*kt;
459 
460 	PF_RULES_WASSERT();
461 	KASSERT(dyn && dyn->pfid_kif && dyn->pfid_kt,
462 	    ("%s: bad argument", __func__));
463 
464 	kif = dyn->pfid_kif;
465 	kt = dyn->pfid_kt;
466 
467 	if (kt->pfrkt_larg != V_pfi_update) {
468 		/* this table needs to be brought up-to-date */
469 		pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
470 		kt->pfrkt_larg = V_pfi_update;
471 	}
472 	pfr_dynaddr_update(kt, dyn);
473 }
474 
475 static void
476 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
477 {
478 	int			 e, size2 = 0;
479 	struct ifg_member	*ifgm;
480 
481 	V_pfi_buffer_cnt = 0;
482 
483 	if (kif->pfik_ifp != NULL)
484 		pfi_instance_add(kif->pfik_ifp, net, flags);
485 	else if (kif->pfik_group != NULL) {
486 		IFNET_RLOCK_NOSLEEP();
487 		TAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
488 			pfi_instance_add(ifgm->ifgm_ifp, net, flags);
489 		IFNET_RUNLOCK_NOSLEEP();
490 	}
491 
492 	if ((e = pfr_set_addrs(&kt->pfrkt_t, V_pfi_buffer, V_pfi_buffer_cnt, &size2,
493 	    NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
494 		printf("%s: cannot set %d new addresses into table %s: %d\n",
495 		    __func__, V_pfi_buffer_cnt, kt->pfrkt_name, e);
496 }
497 
498 static void
499 pfi_instance_add(struct ifnet *ifp, int net, int flags)
500 {
501 	struct ifaddr	*ia;
502 	int		 got4 = 0, got6 = 0;
503 	int		 net2, af;
504 
505 	IF_ADDR_RLOCK(ifp);
506 	TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_list) {
507 		if (ia->ifa_addr == NULL)
508 			continue;
509 		af = ia->ifa_addr->sa_family;
510 		if (af != AF_INET && af != AF_INET6)
511 			continue;
512 		/*
513 		 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
514 		 *      jump over addresses without a proper route to work
515 		 *      around a problem with ppp not fully removing the
516 		 *      address used during IPCP.
517 		 */
518 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
519 		    !(ia->ifa_flags & IFA_ROUTE) &&
520 		    (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
521 			continue;
522 		if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
523 			continue;
524 		if ((flags & PFI_AFLAG_BROADCAST) &&
525 		    !(ifp->if_flags & IFF_BROADCAST))
526 			continue;
527 		if ((flags & PFI_AFLAG_PEER) &&
528 		    !(ifp->if_flags & IFF_POINTOPOINT))
529 			continue;
530 		if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
531 		    IN6_IS_ADDR_LINKLOCAL(
532 		    &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
533 			continue;
534 		if (flags & PFI_AFLAG_NOALIAS) {
535 			if (af == AF_INET && got4)
536 				continue;
537 			if (af == AF_INET6 && got6)
538 				continue;
539 		}
540 		if (af == AF_INET)
541 			got4 = 1;
542 		else if (af == AF_INET6)
543 			got6 = 1;
544 		net2 = net;
545 		if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
546 			if (af == AF_INET)
547 				net2 = pfi_unmask(&((struct sockaddr_in *)
548 				    ia->ifa_netmask)->sin_addr);
549 			else if (af == AF_INET6)
550 				net2 = pfi_unmask(&((struct sockaddr_in6 *)
551 				    ia->ifa_netmask)->sin6_addr);
552 		}
553 		if (af == AF_INET && net2 > 32)
554 			net2 = 32;
555 		if (flags & PFI_AFLAG_BROADCAST)
556 			pfi_address_add(ia->ifa_broadaddr, af, net2);
557 		else if (flags & PFI_AFLAG_PEER)
558 			pfi_address_add(ia->ifa_dstaddr, af, net2);
559 		else
560 			pfi_address_add(ia->ifa_addr, af, net2);
561 	}
562 	IF_ADDR_RUNLOCK(ifp);
563 }
564 
565 static void
566 pfi_address_add(struct sockaddr *sa, int af, int net)
567 {
568 	struct pfr_addr	*p;
569 	int		 i;
570 
571 	if (V_pfi_buffer_cnt >= V_pfi_buffer_max) {
572 		int		 new_max = V_pfi_buffer_max * 2;
573 
574 		if (new_max > PFI_BUFFER_MAX) {
575 			printf("%s: address buffer full (%d/%d)\n", __func__,
576 			    V_pfi_buffer_cnt, PFI_BUFFER_MAX);
577 			return;
578 		}
579 		p = malloc(new_max * sizeof(*V_pfi_buffer), PFI_MTYPE,
580 		    M_NOWAIT);
581 		if (p == NULL) {
582 			printf("%s: no memory to grow buffer (%d/%d)\n",
583 			    __func__, V_pfi_buffer_cnt, PFI_BUFFER_MAX);
584 			return;
585 		}
586 		memcpy(V_pfi_buffer, p, V_pfi_buffer_cnt * sizeof(*V_pfi_buffer));
587 		/* no need to zero buffer */
588 		free(V_pfi_buffer, PFI_MTYPE);
589 		V_pfi_buffer = p;
590 		V_pfi_buffer_max = new_max;
591 	}
592 	if (af == AF_INET && net > 32)
593 		net = 128;
594 	p = V_pfi_buffer + V_pfi_buffer_cnt++;
595 	bzero(p, sizeof(*p));
596 	p->pfra_af = af;
597 	p->pfra_net = net;
598 	if (af == AF_INET)
599 		p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
600 	else if (af == AF_INET6) {
601 		p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
602 		if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
603 			p->pfra_ip6addr.s6_addr16[1] = 0;
604 	}
605 	/* mask network address bits */
606 	if (net < 128)
607 		((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
608 	for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
609 		((caddr_t)p)[i] = 0;
610 }
611 
612 void
613 pfi_dynaddr_remove(struct pfi_dynaddr *dyn)
614 {
615 
616 	KASSERT(dyn->pfid_kif != NULL, ("%s: null pfid_kif", __func__));
617 	KASSERT(dyn->pfid_kt != NULL, ("%s: null pfid_kt", __func__));
618 
619 	TAILQ_REMOVE(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
620 	pfi_kif_unref(dyn->pfid_kif);
621 	pfr_detach_table(dyn->pfid_kt);
622 	free(dyn, PFI_MTYPE);
623 }
624 
625 void
626 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
627 {
628 
629 	KASSERT(aw->type == PF_ADDR_DYNIFTL,
630 	    ("%s: type %u", __func__, aw->type));
631 
632 	if (aw->p.dyn == NULL || aw->p.dyn->pfid_kif == NULL)
633 		return;
634 	aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
635 }
636 
637 static int
638 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
639 {
640 	return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
641 }
642 
643 void
644 pfi_update_status(const char *name, struct pf_status *pfs)
645 {
646 	struct pfi_kif		*p;
647 	struct pfi_kif_cmp	 key;
648 	struct ifg_member	 p_member, *ifgm;
649 	TAILQ_HEAD(, ifg_member) ifg_members;
650 	int			 i, j, k;
651 
652 	strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
653 	p = RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&key);
654 	if (p == NULL)
655 		return;
656 
657 	if (p->pfik_group != NULL) {
658 		bcopy(&p->pfik_group->ifg_members, &ifg_members,
659 		    sizeof(ifg_members));
660 	} else {
661 		/* build a temporary list for p only */
662 		bzero(&p_member, sizeof(p_member));
663 		p_member.ifgm_ifp = p->pfik_ifp;
664 		TAILQ_INIT(&ifg_members);
665 		TAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next);
666 	}
667 	if (pfs) {
668 		bzero(pfs->pcounters, sizeof(pfs->pcounters));
669 		bzero(pfs->bcounters, sizeof(pfs->bcounters));
670 	}
671 	TAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) {
672 		if (ifgm->ifgm_ifp == NULL)
673 			continue;
674 		p = (struct pfi_kif *)ifgm->ifgm_ifp->if_pf_kif;
675 
676 		/* just clear statistics */
677 		if (pfs == NULL) {
678 			bzero(p->pfik_packets, sizeof(p->pfik_packets));
679 			bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
680 			p->pfik_tzero = time_second;
681 			continue;
682 		}
683 		for (i = 0; i < 2; i++)
684 			for (j = 0; j < 2; j++)
685 				for (k = 0; k < 2; k++) {
686 					pfs->pcounters[i][j][k] +=
687 						p->pfik_packets[i][j][k];
688 					pfs->bcounters[i][j] +=
689 						p->pfik_bytes[i][j][k];
690 				}
691 	}
692 }
693 
694 void
695 pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
696 {
697 	struct pfi_kif	*p, *nextp;
698 	int		 n = 0;
699 
700 	for (p = RB_MIN(pfi_ifhead, &V_pfi_ifs); p; p = nextp) {
701 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
702 		if (pfi_skip_if(name, p))
703 			continue;
704 		if (*size <= n++)
705 			break;
706 		if (!p->pfik_tzero)
707 			p->pfik_tzero = time_second;
708 		bcopy(p, buf++, sizeof(*buf));
709 		nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p);
710 	}
711 	*size = n;
712 }
713 
714 static int
715 pfi_skip_if(const char *filter, struct pfi_kif *p)
716 {
717 	int	n;
718 
719 	if (filter == NULL || !*filter)
720 		return (0);
721 	if (!strcmp(p->pfik_name, filter))
722 		return (0);	/* exact match */
723 	n = strlen(filter);
724 	if (n < 1 || n >= IFNAMSIZ)
725 		return (1);	/* sanity check */
726 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
727 		return (1);	/* only do exact match in that case */
728 	if (strncmp(p->pfik_name, filter, n))
729 		return (1);	/* prefix doesn't match */
730 	return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
731 }
732 
733 int
734 pfi_set_flags(const char *name, int flags)
735 {
736 	struct pfi_kif	*p;
737 
738 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
739 		if (pfi_skip_if(name, p))
740 			continue;
741 		p->pfik_flags |= flags;
742 	}
743 	return (0);
744 }
745 
746 int
747 pfi_clear_flags(const char *name, int flags)
748 {
749 	struct pfi_kif	*p;
750 
751 	RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) {
752 		if (pfi_skip_if(name, p))
753 			continue;
754 		p->pfik_flags &= ~flags;
755 	}
756 	return (0);
757 }
758 
759 /* from pf_print_state.c */
760 static int
761 pfi_unmask(void *addr)
762 {
763 	struct pf_addr *m = addr;
764 	int i = 31, j = 0, b = 0;
765 	u_int32_t tmp;
766 
767 	while (j < 4 && m->addr32[j] == 0xffffffff) {
768 		b += 32;
769 		j++;
770 	}
771 	if (j < 4) {
772 		tmp = ntohl(m->addr32[j]);
773 		for (i = 31; tmp & (1 << i); --i)
774 			b++;
775 	}
776 	return (b);
777 }
778 
779 static void
780 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
781 {
782 
783 	CURVNET_SET(ifp->if_vnet);
784 	pfi_attach_ifnet(ifp);
785 #ifdef ALTQ
786 	PF_RULES_WLOCK();
787 	pf_altq_ifnet_event(ifp, 0);
788 	PF_RULES_WUNLOCK();
789 #endif
790 	CURVNET_RESTORE();
791 }
792 
793 static void
794 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
795 {
796 	struct pfi_kif *kif = (struct pfi_kif *)ifp->if_pf_kif;
797 
798 	CURVNET_SET(ifp->if_vnet);
799 	PF_RULES_WLOCK();
800 	V_pfi_update++;
801 	pfi_kif_update(kif);
802 
803 	kif->pfik_ifp = NULL;
804 	ifp->if_pf_kif = NULL;
805 #ifdef ALTQ
806 	pf_altq_ifnet_event(ifp, 1);
807 #endif
808 	PF_RULES_WUNLOCK();
809 	CURVNET_RESTORE();
810 }
811 
812 static void
813 pfi_attach_group_event(void *arg , struct ifg_group *ifg)
814 {
815 
816 	CURVNET_SET((struct vnet *)arg);
817 	pfi_attach_ifgroup(ifg);
818 	CURVNET_RESTORE();
819 }
820 
821 static void
822 pfi_change_group_event(void *arg, char *gname)
823 {
824 	struct pfi_kif *kif;
825 
826 	kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK);
827 
828 	CURVNET_SET((struct vnet *)arg);
829 	PF_RULES_WLOCK();
830 	V_pfi_update++;
831 	kif = pfi_kif_attach(kif, gname);
832 	pfi_kif_update(kif);
833 	PF_RULES_WUNLOCK();
834 	CURVNET_RESTORE();
835 }
836 
837 static void
838 pfi_detach_group_event(void *arg, struct ifg_group *ifg)
839 {
840 	struct pfi_kif *kif = (struct pfi_kif *)ifg->ifg_pf_kif;
841 
842 	CURVNET_SET((struct vnet *)arg);
843 	PF_RULES_WLOCK();
844 	V_pfi_update++;
845 
846 	kif->pfik_group = NULL;
847 	ifg->ifg_pf_kif = NULL;
848 	PF_RULES_WUNLOCK();
849 	CURVNET_RESTORE();
850 }
851 
852 static void
853 pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp)
854 {
855 
856 	CURVNET_SET(ifp->if_vnet);
857 	PF_RULES_WLOCK();
858 	if (ifp && ifp->if_pf_kif) {
859 		V_pfi_update++;
860 		pfi_kif_update(ifp->if_pf_kif);
861 	}
862 	PF_RULES_WUNLOCK();
863 	CURVNET_RESTORE();
864 }
865