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