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