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