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