xref: /dragonfly/sys/net/pf/pf_if.c (revision 02742ec6)
1 /*	$FreeBSD: src/sys/contrib/pf/net/pf_if.c,v 1.6 2004/09/14 15:20:24 mlaier Exp $ */
2 /*	$OpenBSD: pf_if.c,v 1.11 2004/03/15 11:38:23 cedric Exp $ */
3 /* add	$OpenBSD: pf_if.c,v 1.19 2004/08/11 12:06:44 henning Exp $ */
4 /*	$DragonFly: src/sys/net/pf/pf_if.c,v 1.1 2004/09/19 22:32:47 joerg Exp $ */
5 
6 /*
7  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
8  *
9  * Copyright (c) 2001 Daniel Hartmeier
10  * Copyright (c) 2003 Cedric Berger
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  *    - Redistributions of source code must retain the above copyright
18  *      notice, this list of conditions and the following disclaimer.
19  *    - Redistributions in binary form must reproduce the above
20  *      copyright notice, this list of conditions and the following
21  *      disclaimer in the documentation and/or other materials provided
22  *      with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/eventhandler.h>
46 #include <sys/filio.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/kernel.h>
50 #include <sys/time.h>
51 #include <vm/vm_zone.h>
52 
53 #include <net/if.h>
54 #include <net/if_types.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 
62 #include <net/pf/pfvar.h>
63 
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #endif /* INET6 */
67 
68 #define ACCEPT_FLAGS(oklist)			\
69 	do {					\
70 		if ((flags & ~(oklist)) &	\
71 		    PFI_FLAG_ALLMASK)		\
72 			return (EINVAL);	\
73 	} while (0)
74 
75 #define senderr(e)      do { rv = (e); goto _bad; } while (0)
76 
77 struct pfi_kif		**pfi_index2kif;
78 struct pfi_kif		 *pfi_self, *pfi_dummy;
79 int			  pfi_indexlim;
80 struct pfi_ifhead	  pfi_ifs;
81 struct pfi_statehead	  pfi_statehead;
82 int			  pfi_ifcnt;
83 vm_zone_t		  pfi_addr_pl;
84 long			  pfi_update = 1;
85 struct pfr_addr		 *pfi_buffer;
86 int			  pfi_buffer_cnt;
87 int			  pfi_buffer_max;
88 char			  pfi_reserved_anchor[PF_ANCHOR_NAME_SIZE] =
89 				PF_RESERVED_ANCHOR;
90 char			  pfi_interface_ruleset[PF_RULESET_NAME_SIZE] =
91 				PF_INTERFACE_RULESET;
92 
93 eventhandler_tag	 pfi_clone_cookie = NULL;
94 eventhandler_tag	 pfi_attach_cookie = NULL;
95 eventhandler_tag	 pfi_detach_cookie = NULL;
96 
97 void		 pfi_dynaddr_update(void *);
98 void		 pfi_kifaddr_update(void *);
99 void		 pfi_table_update(struct pfr_ktable *, struct pfi_kif *,
100 		    int, int);
101 void		 pfi_instance_add(struct ifnet *, int, int);
102 void		 pfi_address_add(struct sockaddr *, int, int);
103 int		 pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
104 struct pfi_kif	*pfi_if_create(const char *, struct pfi_kif *, int);
105 void		 pfi_copy_group(char *, const char *, int);
106 void		 pfi_dynamic_drivers(void);
107 void		 pfi_newgroup(const char *, int);
108 int		 pfi_skip_if(const char *, struct pfi_kif *, int);
109 int		 pfi_unmask(void *);
110 void		 pfi_dohooks(struct pfi_kif *);
111 void		 pfi_kifaddr_update_event(void *, struct ifnet *);
112 void		 pfi_attach_clone_event(void *, struct if_clone *);
113 void		 pfi_attach_ifnet_event(void *, struct ifnet *);
114 void		 pfi_detach_ifnet_event(void *, struct ifnet *);
115 
116 RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
117 RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
118 
119 #define PFI_DYNAMIC_BUSES	{ "pcmcia", "cardbus", "uhub" }
120 #define PFI_BUFFER_MAX		0x10000
121 MALLOC_DEFINE(PFI_MTYPE, "pf_if", "pf interface table");
122 
123 void
124 pfi_initialize(void)
125 {
126 	struct ifnet	*ifp;
127 
128 	if (pfi_self != NULL)	/* already initialized */
129 		return;
130 
131 	TAILQ_INIT(&pfi_statehead);
132 	pfi_buffer_max = 64;
133 	pfi_buffer = malloc(pfi_buffer_max * sizeof(*pfi_buffer),
134 	    PFI_MTYPE, M_WAITOK);
135 	pfi_self = pfi_if_create("self", NULL, PFI_IFLAG_GROUP);
136 	pfi_dynamic_drivers();
137 
138 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
139 		if (ifp->if_dunit != IF_DUNIT_NONE)
140 			pfi_attach_ifnet(ifp);
141 	}
142 	pfi_dummy = pfi_if_create("notyet", pfi_self,
143 	    PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
144 	pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event,
145 	    pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
146 	pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event,
147 	    pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
148 	pfi_clone_cookie = EVENTHANDLER_REGISTER(if_clone_event,
149 	    pfi_attach_clone_event, NULL, EVENTHANDLER_PRI_ANY);
150 }
151 
152 void
153 pfi_cleanup(void)
154 {
155 	struct pfi_kif *p, key;
156 	struct ifnet *ifp;
157 
158 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie);
159 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie);
160 	EVENTHANDLER_DEREGISTER(if_clone_event, pfi_clone_cookie);
161 
162 	/* release PFI_IFLAG_INSTANCE */
163 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
164 		strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
165 		p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
166 		if (p != NULL)
167 			pfi_detach_ifnet(ifp);
168 	}
169 
170 	/* XXX clear all other interface group */
171 	while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) {
172 		RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
173 
174 		free(p->pfik_ah_head, PFI_MTYPE);
175 		free(p, PFI_MTYPE);
176 	}
177 	free(pfi_index2kif, PFI_MTYPE);
178 	free(pfi_buffer, PFI_MTYPE);
179 	pfi_index2kif = NULL;
180 	pfi_buffer = NULL;
181 	pfi_self = NULL;
182 }
183 
184 /*
185  * Wrapper functions for FreeBSD eventhandler
186  */
187 void
188 pfi_kifaddr_update_event(void *arg, struct ifnet *ifp)
189 {
190 	struct pfi_kif *p = arg;
191 
192 	/*
193 	 * Check to see if it is 'our' interface as we do not have per
194 	 * interface hooks and thus get an update for every interface.
195 	 */
196 	if (p && p->pfik_ifp == ifp)
197 		pfi_kifaddr_update(p);
198 }
199 
200 void
201 pfi_attach_clone_event(void *arg __unused, struct if_clone *ifc)
202 {
203 	pfi_attach_clone(ifc);
204 }
205 
206 void
207 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
208 {
209 	if (ifp->if_dunit != IF_DUNIT_NONE)
210 		pfi_attach_ifnet(ifp);
211 }
212 
213 void
214 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
215 {
216 	pfi_detach_ifnet(ifp);
217 }
218 
219 void
220 pfi_attach_clone(struct if_clone *ifc)
221 {
222 	pfi_initialize();
223 	pfi_newgroup(ifc->ifc_name, PFI_IFLAG_CLONABLE);
224 }
225 
226 void
227 pfi_attach_ifnet(struct ifnet *ifp)
228 {
229 	struct pfi_kif	*p, *q, key;
230 	int		 s;
231 	int		 realname;
232 
233 	pfi_initialize();
234 	s = splsoftnet();
235 	pfi_update++;
236 	if (ifp->if_index >= pfi_indexlim) {
237 		/*
238 		 * grow pfi_index2kif,  similar to ifindex2ifnet code in if.c
239 		 */
240 		size_t m, n, oldlim;
241 		struct pfi_kif **mp, **np;
242 
243 		oldlim = pfi_indexlim;
244 		if (pfi_indexlim == 0)
245 			pfi_indexlim = 64;
246 		while (ifp->if_index >= pfi_indexlim)
247 			pfi_indexlim <<= 1;
248 
249 		m = oldlim * sizeof(struct pfi_kif *);
250 		mp = pfi_index2kif;
251 		n = pfi_indexlim * sizeof(struct pfi_kif *);
252 		np = malloc(n, PFI_MTYPE, M_NOWAIT);
253 		if (np == NULL)
254 			panic("pfi_attach_ifnet: "
255 			    "cannot allocate translation table");
256 		bzero(np, n);
257 		if (mp != NULL)
258 			bcopy(mp, np, m);
259 		pfi_index2kif = np;
260 		if (mp != NULL)
261 			free(mp, PFI_MTYPE);
262 	}
263 
264 	strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
265 	p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
266 	/* some additional trickery for placeholders */
267 	if ((p == NULL) || (p->pfik_parent == pfi_dummy)) {
268 		/* are we looking at a renamed instance or not? */
269 		pfi_copy_group(key.pfik_name, ifp->if_xname,
270 		    sizeof(key.pfik_name));
271 		realname = (strncmp(key.pfik_name, ifp->if_dname,
272 		    sizeof(key.pfik_name)) == 0);
273 		/* add group */
274 		/* we can change if_xname, hence use if_dname as group id */
275 		pfi_copy_group(key.pfik_name, ifp->if_dname,
276 		    sizeof(key.pfik_name));
277 		q = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
278 		if (q == NULL)
279 		    q = pfi_if_create(key.pfik_name, pfi_self,
280 		        PFI_IFLAG_GROUP|PFI_IFLAG_DYNAMIC);
281 		else if (q->pfik_parent == pfi_dummy) {
282 			q->pfik_parent = pfi_self;
283 			q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
284 		}
285 		if (q == NULL)
286 			panic("pfi_attach_ifnet: "
287 			    "cannot allocate '%s' group", key.pfik_name);
288 
289 		/* add/modify interface */
290 		if (p == NULL)
291 			p = pfi_if_create(ifp->if_xname, q,
292 			    realname?PFI_IFLAG_INSTANCE:PFI_IFLAG_PLACEHOLDER);
293 		else {
294 			/* remove from the dummy group */
295 			/* XXX: copy stats? We should not have any!!! */
296 			pfi_dummy->pfik_delcnt++;
297 			TAILQ_REMOVE(&pfi_dummy->pfik_grouphead, p,
298 			    pfik_instances);
299 			/* move to the right group */
300 			p->pfik_parent = q;
301 			q->pfik_addcnt++;
302 			TAILQ_INSERT_TAIL(&q->pfik_grouphead, p,
303 			    pfik_instances);
304 			if (realname) {
305 				p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER;
306 				p->pfik_flags |= PFI_IFLAG_INSTANCE;
307 			}
308 		}
309 		if (p == NULL)
310 			panic("pfi_attach_ifnet: "
311 			    "cannot allocate '%s' interface", ifp->if_xname);
312 	} else
313 		q = p->pfik_parent;
314 	p->pfik_ifp = ifp;
315 	p->pfik_flags |= PFI_IFLAG_ATTACHED;
316 	p->pfik_ah_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
317 	    pfi_kifaddr_update_event, p, EVENTHANDLER_PRI_ANY);
318 	pfi_index2kif[ifp->if_index] = p;
319 	pfi_dohooks(p);
320 	splx(s);
321 }
322 
323 void
324 pfi_detach_ifnet(struct ifnet *ifp)
325 {
326 	struct pfi_kif	*p, *q, key;
327 	int		 s;
328 
329 	strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
330 
331 	s = splsoftnet();
332 	pfi_update++;
333 	p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
334 	if (p == NULL) {
335 		printf("pfi_detach_ifnet: cannot find %s", ifp->if_xname);
336 		splx(s);
337 		return;
338 	}
339 	EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie);
340 	q = p->pfik_parent;
341 	p->pfik_ifp = NULL;
342 	p->pfik_flags &= ~PFI_IFLAG_ATTACHED;
343 	pfi_index2kif[ifp->if_index] = NULL;
344 	pfi_dohooks(p);
345 	pfi_maybe_destroy(p);
346 	splx(s);
347 }
348 
349 struct pfi_kif *
350 pfi_lookup_create(const char *name)
351 {
352 	struct pfi_kif	*p, *q, key;
353 	int		 s;
354 
355 	s = splsoftnet();
356 	p = pfi_lookup_if(name);
357 	if (p == NULL) {
358 		pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name));
359 		q = pfi_lookup_if(key.pfik_name);
360 		if ((q != NULL) && (q->pfik_parent != pfi_dummy))
361 			p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE);
362 		else {
363 			if (pfi_dummy == NULL)
364 				panic("no 'notyet' dummy group");
365 			p = pfi_if_create(name, pfi_dummy,
366 			    PFI_IFLAG_PLACEHOLDER);
367 		}
368 	}
369 	splx(s);
370 	return (p);
371 }
372 
373 struct pfi_kif *
374 pfi_attach_rule(const char *name)
375 {
376 	struct pfi_kif	*p;
377 
378 	p = pfi_lookup_create(name);
379 	if (p != NULL)
380 		p->pfik_rules++;
381 	return (p);
382 }
383 
384 void
385 pfi_detach_rule(struct pfi_kif *p)
386 {
387 	if (p == NULL)
388 		return;
389 	if (p->pfik_rules > 0)
390 		p->pfik_rules--;
391 	else
392 		printf("pfi_detach_rule: reference count at 0\n");
393 	pfi_maybe_destroy(p);
394 }
395 
396 void
397 pfi_attach_state(struct pfi_kif *p)
398 {
399 	if (!p->pfik_states++)
400 		TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states);
401 }
402 
403 void
404 pfi_detach_state(struct pfi_kif *p)
405 {
406 	if (p == NULL)
407 		return;
408 	if (p->pfik_states <= 0) {
409 		printf("pfi_detach_state: reference count <= 0\n");
410 		return;
411 	}
412 	if (!--p->pfik_states)
413 		TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states);
414 	pfi_maybe_destroy(p);
415 }
416 
417 int
418 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
419 {
420 	struct pfi_dynaddr	*dyn;
421 	char			 tblname[PF_TABLE_NAME_SIZE];
422 	struct pf_ruleset	*ruleset = NULL;
423 	int			 s, rv = 0;
424 
425 	if (aw->type != PF_ADDR_DYNIFTL)
426 		return (0);
427 	dyn = pool_get(&pfi_addr_pl, PR_NOWAIT);
428 	if (dyn == NULL)
429 		return (1);
430 	bzero(dyn, sizeof(*dyn));
431 
432 	s = splsoftnet();
433 	dyn->pfid_kif = pfi_attach_rule(aw->v.ifname);
434 	if (dyn->pfid_kif == NULL)
435 		senderr(1);
436 
437 	dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
438 	if (af == AF_INET && dyn->pfid_net == 32)
439 		dyn->pfid_net = 128;
440 	strlcpy(tblname, aw->v.ifname, sizeof(tblname));
441 	if (aw->iflags & PFI_AFLAG_NETWORK)
442 		strlcat(tblname, ":network", sizeof(tblname));
443 	if (aw->iflags & PFI_AFLAG_BROADCAST)
444 		strlcat(tblname, ":broadcast", sizeof(tblname));
445 	if (aw->iflags & PFI_AFLAG_PEER)
446 		strlcat(tblname, ":peer", sizeof(tblname));
447 	if (aw->iflags & PFI_AFLAG_NOALIAS)
448 		strlcat(tblname, ":0", sizeof(tblname));
449 	if (dyn->pfid_net != 128)
450 		snprintf(tblname + strlen(tblname),
451 		    sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
452 	ruleset = pf_find_or_create_ruleset(pfi_reserved_anchor,
453 	    pfi_interface_ruleset);
454 	if (ruleset == NULL)
455 		senderr(1);
456 
457 	dyn->pfid_kt = pfr_attach_table(ruleset, tblname);
458 	if (dyn->pfid_kt == NULL)
459 		senderr(1);
460 
461 	dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
462 	dyn->pfid_iflags = aw->iflags;
463 	dyn->pfid_af = af;
464 	dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1,
465 	    pfi_dynaddr_update, dyn);
466 	if (dyn->pfid_hook_cookie == NULL)
467 		senderr(1);
468 
469 	aw->p.dyn = dyn;
470 	pfi_dynaddr_update(aw->p.dyn);
471 	splx(s);
472 	return (0);
473 
474 _bad:
475 	if (dyn->pfid_kt != NULL)
476 		pfr_detach_table(dyn->pfid_kt);
477 	if (ruleset != NULL)
478 		pf_remove_if_empty_ruleset(ruleset);
479 	if (dyn->pfid_kif != NULL)
480 		pfi_detach_rule(dyn->pfid_kif);
481 	pool_put(&pfi_addr_pl, dyn);
482 	splx(s);
483 	return (rv);
484 }
485 
486 void
487 pfi_dynaddr_update(void *p)
488 {
489 	struct pfi_dynaddr	*dyn = (struct pfi_dynaddr *)p;
490 	struct pfi_kif		*kif = dyn->pfid_kif;
491 	struct pfr_ktable	*kt = dyn->pfid_kt;
492 
493 	if (dyn == NULL || kif == NULL || kt == NULL)
494 		panic("pfi_dynaddr_update");
495 	if (kt->pfrkt_larg != pfi_update) {
496 		/* this table needs to be brought up-to-date */
497 		pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
498 		kt->pfrkt_larg = pfi_update;
499 	}
500 	pfr_dynaddr_update(kt, dyn);
501 }
502 
503 void
504 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
505 {
506 	int			 e, size2 = 0;
507 	struct pfi_kif		*p;
508 	struct pfr_table	 t;
509 
510 	if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) {
511 		pfr_clr_addrs(&kt->pfrkt_t, NULL, 0);
512 		return;
513 	}
514 	pfi_buffer_cnt = 0;
515 	if ((kif->pfik_flags & PFI_IFLAG_INSTANCE))
516 		pfi_instance_add(kif->pfik_ifp, net, flags);
517 	else if (strcmp(kif->pfik_name, "self")) {
518 		TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances)
519 			pfi_instance_add(p->pfik_ifp, net, flags);
520 	} else {
521 		RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
522 			if (p->pfik_flags & PFI_IFLAG_INSTANCE)
523 				pfi_instance_add(p->pfik_ifp, net, flags);
524 	}
525 	t = kt->pfrkt_t;
526 	t.pfrt_flags = 0;
527 	if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2,
528 	    NULL, NULL, NULL, 0)))
529 		printf("pfi_table_update: cannot set %d new addresses "
530 		    "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
531 }
532 
533 void
534 pfi_instance_add(struct ifnet *ifp, int net, int flags)
535 {
536 	struct ifaddr	*ia;
537 	int		 got4 = 0, got6 = 0;
538 	int		 net2, af;
539 
540 	if (ifp == NULL)
541 		return;
542 	TAILQ_FOREACH(ia, &ifp->if_addrlist, ifa_list) {
543 		if (ia->ifa_addr == NULL)
544 			continue;
545 		af = ia->ifa_addr->sa_family;
546 		if (af != AF_INET && af != AF_INET6)
547 			continue;
548 		/*
549 		 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
550 		 *	jump over address without a proper route to work
551 		 *	around a problem with ppp not fully removing the
552 		 *	address used during IPCP.
553 		 */
554 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
555 		    !(ia->ifa_flags & IFA_ROUTE) &&
556 		    (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
557 			continue;
558 		if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
559 			continue;
560 		if ((flags & PFI_AFLAG_BROADCAST) &&
561 		    !(ifp->if_flags & IFF_BROADCAST))
562 			continue;
563 		if ((flags & PFI_AFLAG_PEER) &&
564 		    !(ifp->if_flags & IFF_POINTOPOINT))
565 			continue;
566 		if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
567 		    IN6_IS_ADDR_LINKLOCAL(
568 		    &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
569 			continue;
570 		if (flags & PFI_AFLAG_NOALIAS) {
571 			if (af == AF_INET && got4)
572 				continue;
573 			if (af == AF_INET6 && got6)
574 				continue;
575 		}
576 		if (af == AF_INET)
577 			got4 = 1;
578 		else
579 			got6 = 1;
580 		net2 = net;
581 		if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
582 			if (af == AF_INET) {
583 				net2 = pfi_unmask(&((struct sockaddr_in *)
584 				    ia->ifa_netmask)->sin_addr);
585 			} else {
586 				net2 = pfi_unmask(&((struct sockaddr_in6 *)
587 				    ia->ifa_netmask)->sin6_addr);
588 			}
589 		}
590 		if (af == AF_INET && net2 > 32)
591 			net2 = 32;
592 		if (flags & PFI_AFLAG_BROADCAST)
593 			pfi_address_add(ia->ifa_broadaddr, af, net2);
594 		else if (flags & PFI_AFLAG_PEER)
595 			pfi_address_add(ia->ifa_dstaddr, af, net2);
596 		else
597 			pfi_address_add(ia->ifa_addr, af, net2);
598 	}
599 }
600 
601 void
602 pfi_address_add(struct sockaddr *sa, int af, int net)
603 {
604 	struct pfr_addr	*p;
605 	int		 i;
606 
607 	if (pfi_buffer_cnt >= pfi_buffer_max) {
608 		int		 new_max = pfi_buffer_max * 2;
609 
610 		if (new_max > PFI_BUFFER_MAX) {
611 			printf("pfi_address_add: address buffer full (%d/%d)\n",
612 			    pfi_buffer_cnt, PFI_BUFFER_MAX);
613 			return;
614 		}
615 		p = malloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
616 		    M_NOWAIT);
617 		if (p == NULL) {
618 			printf("pfi_address_add: no memory to grow buffer "
619 			    "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
620 			return;
621 		}
622 		memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
623 		/* no need to zero buffer */
624 		free(pfi_buffer, PFI_MTYPE);
625 		pfi_buffer = p;
626 		pfi_buffer_max = new_max;
627 	}
628 	if (af == AF_INET && net > 32)
629 		net = 128;
630 	p = pfi_buffer + pfi_buffer_cnt++;
631 	bzero(p, sizeof(*p));
632 	p->pfra_af = af;
633 	p->pfra_net = net;
634 	if (af == AF_INET)
635 		p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
636 	if (af == AF_INET6) {
637 		p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
638 		if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr))
639 			p->pfra_ip6addr.s6_addr16[1] = 0;
640 	}
641 	/* mask network address bits */
642 	if (net < 128)
643 		((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
644 	for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
645 		((caddr_t)p)[i] = 0;
646 }
647 
648 void
649 pfi_dynaddr_remove(struct pf_addr_wrap *aw)
650 {
651 	int	s;
652 
653 	if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
654 	    aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
655 		return;
656 
657 	s = splsoftnet();
658 	hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head,
659 	    aw->p.dyn->pfid_hook_cookie);
660 	pfi_detach_rule(aw->p.dyn->pfid_kif);
661 	aw->p.dyn->pfid_kif = NULL;
662 	pfr_detach_table(aw->p.dyn->pfid_kt);
663 	aw->p.dyn->pfid_kt = NULL;
664 	pool_put(&pfi_addr_pl, aw->p.dyn);
665 	aw->p.dyn = NULL;
666 	splx(s);
667 }
668 
669 void
670 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
671 {
672 	if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
673 	    aw->p.dyn->pfid_kif == NULL)
674 		return;
675 	aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
676 }
677 
678 void
679 pfi_kifaddr_update(void *v)
680 {
681 	int		 s;
682 
683 	s = splsoftnet();
684 	pfi_update++;
685 	pfi_dohooks(v);
686 	splx(s);
687 }
688 
689 int
690 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
691 {
692 	return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
693 }
694 
695 struct pfi_kif *
696 pfi_if_create(const char *name, struct pfi_kif *q, int flags)
697 {
698 	struct pfi_kif *p;
699 
700 	p = malloc(sizeof(*p), PFI_MTYPE, M_NOWAIT);
701 	if (p == NULL)
702 		return (NULL);
703 	bzero(p, sizeof(*p));
704 	p->pfik_ah_head = malloc(sizeof(*p->pfik_ah_head), PFI_MTYPE,
705 	    M_NOWAIT);
706 	if (p->pfik_ah_head == NULL) {
707 		free(p, PFI_MTYPE);
708 		return (NULL);
709 	}
710 	bzero(p->pfik_ah_head, sizeof(*p->pfik_ah_head));
711 	TAILQ_INIT(p->pfik_ah_head);
712 	TAILQ_INIT(&p->pfik_grouphead);
713 	strlcpy(p->pfik_name, name, sizeof(p->pfik_name));
714 	RB_INIT(&p->pfik_lan_ext);
715 	RB_INIT(&p->pfik_ext_gwy);
716 	p->pfik_flags = flags;
717 	p->pfik_parent = q;
718 	p->pfik_tzero = time_second;
719 
720 	RB_INSERT(pfi_ifhead, &pfi_ifs, p);
721 	if (q != NULL) {
722 		q->pfik_addcnt++;
723 		TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances);
724 	}
725 	pfi_ifcnt++;
726 	return (p);
727 }
728 
729 int
730 pfi_maybe_destroy(struct pfi_kif *p)
731 {
732 	int		 i, j, k, s;
733 	struct pfi_kif	*q = p->pfik_parent;
734 
735 	if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) ||
736 	    p->pfik_rules > 0 || p->pfik_states > 0)
737 		if (!(p->pfik_flags & PFI_IFLAG_PLACEHOLDER))
738 			return (0);
739 
740 	s = splsoftnet();
741 	if (q != NULL) {
742 		for (i = 0; i < 2; i++)
743 			for (j = 0; j < 2; j++)
744 				for (k = 0; k < 2; k++) {
745 					q->pfik_bytes[i][j][k] +=
746 					    p->pfik_bytes[i][j][k];
747 					q->pfik_packets[i][j][k] +=
748 					    p->pfik_packets[i][j][k];
749 			/* clear stats in case we return to the dummy group */
750 					p->pfik_bytes[i][j][k] = 0;
751 					p->pfik_packets[i][j][k] = 0;
752 				}
753 		q->pfik_delcnt++;
754 		TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances);
755 	}
756 	if (p->pfik_rules > 0 || p->pfik_states > 0) {
757 		/* move back to the dummy group */
758 		p->pfik_parent = pfi_dummy;
759 		pfi_dummy->pfik_addcnt++;
760 		TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p,
761 		    pfik_instances);
762 		return (0);
763 	}
764 	pfi_ifcnt--;
765 	RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
766 	splx(s);
767 
768 	free(p->pfik_ah_head, PFI_MTYPE);
769 	free(p, PFI_MTYPE);
770 	return (1);
771 }
772 
773 void
774 pfi_copy_group(char *p, const char *q, int m)
775 {
776 	while (m > 1 && *q && !(*q >= '0' && *q <= '9')) {
777 		*p++ = *q++;
778 		m--;
779 	}
780 	if (m > 0)
781 		*p++ = '\0';
782 }
783 
784 void
785 pfi_dynamic_drivers(void)
786 {
787 	struct ifnet	*ifp;
788 
789 /*
790  * For FreeBSD basically every interface is "dynamic" as we can unload
791  * modules e.g.
792  */
793 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
794 		if (ifp->if_dunit == IF_DUNIT_NONE)
795 			continue;
796 		pfi_newgroup(ifp->if_dname, PFI_IFLAG_DYNAMIC);
797 	}
798 }
799 
800 void
801 pfi_newgroup(const char *name, int flags)
802 {
803 	struct pfi_kif	*p;
804 
805 	p = pfi_lookup_if(name);
806 	if (p == NULL)
807 		p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP);
808 	if (p == NULL) {
809 		printf("pfi_newgroup: cannot allocate '%s' group", name);
810 		return;
811 	}
812 	p->pfik_flags |= flags;
813 }
814 
815 void
816 pfi_fill_oldstatus(struct pf_status *pfs)
817 {
818 	struct pfi_kif	*p, key;
819 	int		 i, j, k, s;
820 
821 	strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
822 	s = splsoftnet();
823 	p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
824 	if (p == NULL) {
825 		splx(s);
826 		return;
827 	}
828 	bzero(pfs->pcounters, sizeof(pfs->pcounters));
829 	bzero(pfs->bcounters, sizeof(pfs->bcounters));
830 	for (i = 0; i < 2; i++)
831 		for (j = 0; j < 2; j++)
832 			for (k = 0; k < 2; k++) {
833 				pfs->pcounters[i][j][k] =
834 					p->pfik_packets[i][j][k];
835 				pfs->bcounters[i][j] +=
836 					p->pfik_bytes[i][j][k];
837 			}
838 	splx(s);
839 }
840 
841 int
842 pfi_clr_istats(const char *name, int *nzero, int flags)
843 {
844 	struct pfi_kif	*p;
845 	int		 n = 0, s;
846 	long		 tzero = time_second;
847 
848 	s = splsoftnet();
849 	ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
850 	RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
851 		if (pfi_skip_if(name, p, flags))
852 			continue;
853 		bzero(p->pfik_packets, sizeof(p->pfik_packets));
854 		bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
855 		p->pfik_tzero = tzero;
856 		n++;
857 	}
858 	splx(s);
859 	if (nzero != NULL)
860 		*nzero = n;
861 	return (0);
862 }
863 
864 int
865 pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags)
866 {
867 	struct pfi_kif	*p;
868 	int		 s, n = 0;
869 
870 	ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
871 	s = splsoftnet();
872 	RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
873 		if (pfi_skip_if(name, p, flags))
874 			continue;
875 		if (*size > n++) {
876 			if (!p->pfik_tzero)
877 				p->pfik_tzero = boottime.tv_sec;
878 			if (copyout(p, buf++, sizeof(*buf))) {
879 				splx(s);
880 				return (EFAULT);
881 			}
882 		}
883 	}
884 	splx(s);
885 	*size = n;
886 	return (0);
887 }
888 
889 struct pfi_kif *
890 pfi_lookup_if(const char *name)
891 {
892 	struct pfi_kif	*p, key;
893 
894 	strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
895 	p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
896 	return (p);
897 }
898 
899 int
900 pfi_skip_if(const char *filter, struct pfi_kif *p, int f)
901 {
902 	int	n;
903 
904 	if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP))
905 		return (1);
906 	if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE))
907 		return (1);
908 	if (filter == NULL || !*filter)
909 		return (0);
910 	if (!strcmp(p->pfik_name, filter))
911 		return (0);	/* exact match */
912 	n = strlen(filter);
913 	if (n < 1 || n >= IFNAMSIZ)
914 		return (1);	/* sanity check */
915 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
916 		return (1);	/* only do exact match in that case */
917 	if (strncmp(p->pfik_name, filter, n))
918 		return (1);	/* prefix doesn't match */
919 	return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
920 }
921 
922 /* from pf_print_state.c */
923 int
924 pfi_unmask(void *addr)
925 {
926 	struct pf_addr *m = addr;
927 	int i = 31, j = 0, b = 0;
928 	u_int32_t tmp;
929 
930 	while (j < 4 && m->addr32[j] == 0xffffffff) {
931 		b += 32;
932 		j++;
933 	}
934 	if (j < 4) {
935 		tmp = ntohl(m->addr32[j]);
936 		for (i = 31; tmp & (1 << i); --i)
937 			b++;
938 	}
939 	return (b);
940 }
941 
942 void
943 pfi_dohooks(struct pfi_kif *p)
944 {
945 	for (; p != NULL; p = p->pfik_parent)
946 		dohooks(p->pfik_ah_head, 0);
947 }
948 
949 int
950 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
951 {
952 	if (af == AF_INET) {
953 		switch (dyn->pfid_acnt4) {
954 		case 0:
955 			return (0);
956 		case 1:
957 			return (PF_MATCHA(0, &dyn->pfid_addr4,
958 			    &dyn->pfid_mask4, a, AF_INET));
959 		default:
960 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
961 		}
962 	} else {
963 		switch (dyn->pfid_acnt6) {
964 		case 0:
965 			return (0);
966 		case 1:
967 			return (PF_MATCHA(0, &dyn->pfid_addr6,
968 			    &dyn->pfid_mask6, a, AF_INET6));
969 		default:
970 			return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
971 		}
972 	}
973 }
974