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.9 2008/01/05 14:02:38 swildner Exp $ */
7 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
9 * Copyright (c) 2001 Daniel Hartmeier
10 * Copyright (c) 2003 Cedric Berger
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.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/thread2.h>
52 #include <vm/vm_zone.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
64 #include <net/pf/pfvar.h>
67 #include <netinet/ip6.h>
70 #define ACCEPT_FLAGS(oklist) \
72 if ((flags & ~(oklist)) & \
77 #define senderr(e) do { rv = (e); goto _bad; } while (0)
79 struct pfi_kif **pfi_index2kif;
80 struct pfi_kif *pfi_self, *pfi_dummy;
82 struct pfi_ifhead pfi_ifs;
83 struct pfi_statehead pfi_statehead;
85 vm_zone_t pfi_addr_pl;
87 struct pfr_addr *pfi_buffer;
90 char pfi_reserved_anchor[PF_ANCHOR_NAME_SIZE] =
92 char pfi_interface_ruleset[PF_RULESET_NAME_SIZE] =
95 eventhandler_tag pfi_clone_cookie = NULL;
96 eventhandler_tag pfi_attach_cookie = NULL;
97 eventhandler_tag pfi_detach_cookie = NULL;
99 void pfi_dynaddr_update(void *);
100 void pfi_kifaddr_update(void *);
101 void pfi_table_update(struct pfr_ktable *, struct pfi_kif *,
103 void pfi_instance_add(struct ifnet *, int, int);
104 void pfi_address_add(struct sockaddr *, int, int);
105 int pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
106 struct pfi_kif *pfi_if_create(const char *, struct pfi_kif *, int);
107 void pfi_copy_group(char *, const char *, int);
108 void pfi_dynamic_drivers(void);
109 void pfi_newgroup(const char *, int);
110 int pfi_skip_if(const char *, struct pfi_kif *, int);
111 int pfi_unmask(void *);
112 void pfi_dohooks(struct pfi_kif *);
113 void pfi_kifaddr_update_event(void *, struct ifnet *);
114 void pfi_attach_clone_event(void *, struct if_clone *);
115 void pfi_attach_ifnet_event(void *, struct ifnet *);
116 void pfi_detach_ifnet_event(void *, struct ifnet *);
118 RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
119 RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
121 #define PFI_DYNAMIC_BUSES { "pcmcia", "cardbus", "uhub" }
122 #define PFI_BUFFER_MAX 0x10000
123 MALLOC_DEFINE(PFI_MTYPE, "pf_if", "pf interface table");
130 if (pfi_self != NULL) /* already initialized */
133 TAILQ_INIT(&pfi_statehead);
135 pfi_buffer = kmalloc(pfi_buffer_max * sizeof(*pfi_buffer),
136 PFI_MTYPE, M_WAITOK);
137 pfi_self = pfi_if_create("self", NULL, PFI_IFLAG_GROUP);
138 pfi_dynamic_drivers();
140 TAILQ_FOREACH(ifp, &ifnet, if_link) {
141 if (ifp->if_dunit != IF_DUNIT_NONE)
142 pfi_attach_ifnet(ifp);
144 pfi_dummy = pfi_if_create("notyet", pfi_self,
145 PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
146 pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_attach_event,
147 pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
148 pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_detach_event,
149 pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
150 pfi_clone_cookie = EVENTHANDLER_REGISTER(if_clone_event,
151 pfi_attach_clone_event, NULL, EVENTHANDLER_PRI_ANY);
157 struct pfi_kif *p, key;
160 EVENTHANDLER_DEREGISTER(ifnet_attach_event, pfi_attach_cookie);
161 EVENTHANDLER_DEREGISTER(ifnet_detach_event, pfi_detach_cookie);
162 EVENTHANDLER_DEREGISTER(if_clone_event, pfi_clone_cookie);
164 /* release PFI_IFLAG_INSTANCE */
165 TAILQ_FOREACH(ifp, &ifnet, if_link) {
166 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
167 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
169 pfi_detach_ifnet(ifp);
172 /* XXX clear all other interface group */
173 while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) {
174 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
176 kfree(p->pfik_ah_head, PFI_MTYPE);
179 kfree(pfi_index2kif, PFI_MTYPE);
180 kfree(pfi_buffer, PFI_MTYPE);
181 pfi_index2kif = NULL;
187 * Wrapper functions for FreeBSD eventhandler
190 pfi_kifaddr_update_event(void *arg, struct ifnet *ifp)
192 struct pfi_kif *p = arg;
195 * Check to see if it is 'our' interface as we do not have per
196 * interface hooks and thus get an update for every interface.
198 if (p && p->pfik_ifp == ifp)
199 pfi_kifaddr_update(p);
203 pfi_attach_clone_event(void *arg __unused, struct if_clone *ifc)
205 pfi_attach_clone(ifc);
209 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp)
211 if (ifp->if_dunit != IF_DUNIT_NONE)
212 pfi_attach_ifnet(ifp);
216 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp)
218 pfi_detach_ifnet(ifp);
222 pfi_attach_clone(struct if_clone *ifc)
225 pfi_newgroup(ifc->ifc_name, PFI_IFLAG_CLONABLE);
229 pfi_attach_ifnet(struct ifnet *ifp)
231 struct pfi_kif *p, *q, key;
237 if (ifp->if_index >= pfi_indexlim) {
239 * grow pfi_index2kif, similar to ifindex2ifnet code in if.c
242 struct pfi_kif **mp, **np;
244 oldlim = pfi_indexlim;
245 if (pfi_indexlim == 0)
247 while (ifp->if_index >= pfi_indexlim)
250 m = oldlim * sizeof(struct pfi_kif *);
252 n = pfi_indexlim * sizeof(struct pfi_kif *);
253 np = kmalloc(n, PFI_MTYPE, M_NOWAIT | M_ZERO);
255 panic("pfi_attach_ifnet: "
256 "cannot allocate translation table");
261 kfree(mp, PFI_MTYPE);
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);
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);
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);
286 panic("pfi_attach_ifnet: "
287 "cannot allocate '%s' group", key.pfik_name);
289 /* add/modify interface */
291 p = pfi_if_create(ifp->if_xname, q,
292 realname?PFI_IFLAG_INSTANCE:PFI_IFLAG_PLACEHOLDER);
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,
299 /* move to the right group */
302 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p,
305 p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER;
306 p->pfik_flags |= PFI_IFLAG_INSTANCE;
310 panic("pfi_attach_ifnet: "
311 "cannot allocate '%s' interface", ifp->if_xname);
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;
324 pfi_detach_ifnet(struct ifnet *ifp)
326 struct pfi_kif *p, *q, key;
328 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
332 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
334 kprintf("pfi_detach_ifnet: cannot find %s", ifp->if_xname);
338 EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie);
341 p->pfik_flags &= ~PFI_IFLAG_ATTACHED;
342 pfi_index2kif[ifp->if_index] = NULL;
344 pfi_maybe_destroy(p);
349 pfi_lookup_create(const char *name)
351 struct pfi_kif *p, *q, key;
354 p = pfi_lookup_if(name);
356 pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name));
357 q = pfi_lookup_if(key.pfik_name);
358 if ((q != NULL) && (q->pfik_parent != pfi_dummy))
359 p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE);
361 if (pfi_dummy == NULL)
362 panic("no 'notyet' dummy group");
363 p = pfi_if_create(name, pfi_dummy,
364 PFI_IFLAG_PLACEHOLDER);
372 pfi_attach_rule(const char *name)
376 p = pfi_lookup_create(name);
383 pfi_detach_rule(struct pfi_kif *p)
387 if (p->pfik_rules > 0)
390 kprintf("pfi_detach_rule: reference count at 0\n");
391 pfi_maybe_destroy(p);
395 pfi_attach_state(struct pfi_kif *p)
397 if (!p->pfik_states++)
398 TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states);
402 pfi_detach_state(struct pfi_kif *p)
406 if (p->pfik_states <= 0) {
407 kprintf("pfi_detach_state: reference count <= 0\n");
410 if (!--p->pfik_states)
411 TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states);
412 pfi_maybe_destroy(p);
416 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
418 struct pfi_dynaddr *dyn;
419 char tblname[PF_TABLE_NAME_SIZE];
420 struct pf_ruleset *ruleset = NULL;
423 if (aw->type != PF_ADDR_DYNIFTL)
425 dyn = pool_get(&pfi_addr_pl, PR_NOWAIT);
428 bzero(dyn, sizeof(*dyn));
431 dyn->pfid_kif = pfi_attach_rule(aw->v.ifname);
432 if (dyn->pfid_kif == NULL)
435 dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
436 if (af == AF_INET && dyn->pfid_net == 32)
438 strlcpy(tblname, aw->v.ifname, sizeof(tblname));
439 if (aw->iflags & PFI_AFLAG_NETWORK)
440 strlcat(tblname, ":network", sizeof(tblname));
441 if (aw->iflags & PFI_AFLAG_BROADCAST)
442 strlcat(tblname, ":broadcast", sizeof(tblname));
443 if (aw->iflags & PFI_AFLAG_PEER)
444 strlcat(tblname, ":peer", sizeof(tblname));
445 if (aw->iflags & PFI_AFLAG_NOALIAS)
446 strlcat(tblname, ":0", sizeof(tblname));
447 if (dyn->pfid_net != 128)
448 ksnprintf(tblname + strlen(tblname),
449 sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
450 ruleset = pf_find_or_create_ruleset(pfi_reserved_anchor,
451 pfi_interface_ruleset);
455 dyn->pfid_kt = pfr_attach_table(ruleset, tblname);
456 if (dyn->pfid_kt == NULL)
459 dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
460 dyn->pfid_iflags = aw->iflags;
462 dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1,
463 pfi_dynaddr_update, dyn);
464 if (dyn->pfid_hook_cookie == NULL)
468 pfi_dynaddr_update(aw->p.dyn);
473 if (dyn->pfid_kt != NULL)
474 pfr_detach_table(dyn->pfid_kt);
476 pf_remove_if_empty_ruleset(ruleset);
477 if (dyn->pfid_kif != NULL)
478 pfi_detach_rule(dyn->pfid_kif);
479 pool_put(&pfi_addr_pl, dyn);
485 pfi_dynaddr_update(void *p)
487 struct pfi_dynaddr *dyn = (struct pfi_dynaddr *)p;
488 struct pfi_kif *kif = dyn->pfid_kif;
489 struct pfr_ktable *kt = dyn->pfid_kt;
491 if (dyn == NULL || kif == NULL || kt == NULL)
492 panic("pfi_dynaddr_update");
493 if (kt->pfrkt_larg != pfi_update) {
494 /* this table needs to be brought up-to-date */
495 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
496 kt->pfrkt_larg = pfi_update;
498 pfr_dynaddr_update(kt, dyn);
502 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
508 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) {
509 pfr_clr_addrs(&kt->pfrkt_t, NULL, 0);
513 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE))
514 pfi_instance_add(kif->pfik_ifp, net, flags);
515 else if (strcmp(kif->pfik_name, "self")) {
516 TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances)
517 pfi_instance_add(p->pfik_ifp, net, flags);
519 RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
520 if (p->pfik_flags & PFI_IFLAG_INSTANCE)
521 pfi_instance_add(p->pfik_ifp, net, flags);
525 if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2,
526 NULL, NULL, NULL, 0)))
527 kprintf("pfi_table_update: cannot set %d new addresses "
528 "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
532 pfi_instance_add(struct ifnet *ifp, int net, int flags)
535 int got4 = 0, got6 = 0;
540 TAILQ_FOREACH(ia, &ifp->if_addrlist, ifa_list) {
541 if (ia->ifa_addr == NULL)
543 af = ia->ifa_addr->sa_family;
544 if (af != AF_INET && af != AF_INET6)
547 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
548 * jump over address without a proper route to work
549 * around a problem with ppp not fully removing the
550 * address used during IPCP.
552 if ((ifp->if_flags & IFF_POINTOPOINT) &&
553 !(ia->ifa_flags & IFA_ROUTE) &&
554 (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
556 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
558 if ((flags & PFI_AFLAG_BROADCAST) &&
559 !(ifp->if_flags & IFF_BROADCAST))
561 if ((flags & PFI_AFLAG_PEER) &&
562 !(ifp->if_flags & IFF_POINTOPOINT))
564 if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
565 IN6_IS_ADDR_LINKLOCAL(
566 &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
568 if (flags & PFI_AFLAG_NOALIAS) {
569 if (af == AF_INET && got4)
571 if (af == AF_INET6 && got6)
579 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
581 net2 = pfi_unmask(&((struct sockaddr_in *)
582 ia->ifa_netmask)->sin_addr);
584 net2 = pfi_unmask(&((struct sockaddr_in6 *)
585 ia->ifa_netmask)->sin6_addr);
588 if (af == AF_INET && net2 > 32)
590 if (flags & PFI_AFLAG_BROADCAST)
591 pfi_address_add(ia->ifa_broadaddr, af, net2);
592 else if (flags & PFI_AFLAG_PEER)
593 pfi_address_add(ia->ifa_dstaddr, af, net2);
595 pfi_address_add(ia->ifa_addr, af, net2);
600 pfi_address_add(struct sockaddr *sa, int af, int net)
605 if (pfi_buffer_cnt >= pfi_buffer_max) {
606 int new_max = pfi_buffer_max * 2;
608 if (new_max > PFI_BUFFER_MAX) {
609 kprintf("pfi_address_add: address buffer full (%d/%d)\n",
610 pfi_buffer_cnt, PFI_BUFFER_MAX);
613 p = kmalloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
616 kprintf("pfi_address_add: no memory to grow buffer "
617 "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
620 memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
621 /* no need to zero buffer */
622 kfree(pfi_buffer, PFI_MTYPE);
624 pfi_buffer_max = new_max;
626 if (af == AF_INET && net > 32)
628 p = pfi_buffer + pfi_buffer_cnt++;
629 bzero(p, sizeof(*p));
633 p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
634 if (af == AF_INET6) {
635 p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
636 if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr))
637 p->pfra_ip6addr.s6_addr16[1] = 0;
639 /* mask network address bits */
641 ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
642 for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
647 pfi_dynaddr_remove(struct pf_addr_wrap *aw)
649 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
650 aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
654 hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head,
655 aw->p.dyn->pfid_hook_cookie);
656 pfi_detach_rule(aw->p.dyn->pfid_kif);
657 aw->p.dyn->pfid_kif = NULL;
658 pfr_detach_table(aw->p.dyn->pfid_kt);
659 aw->p.dyn->pfid_kt = NULL;
660 pool_put(&pfi_addr_pl, aw->p.dyn);
666 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
668 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
669 aw->p.dyn->pfid_kif == NULL)
671 aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
675 pfi_kifaddr_update(void *v)
684 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
686 return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
690 pfi_if_create(const char *name, struct pfi_kif *q, int flags)
694 p = kmalloc(sizeof(*p), PFI_MTYPE, M_NOWAIT | M_ZERO);
697 p->pfik_ah_head = kmalloc(sizeof(*p->pfik_ah_head), PFI_MTYPE,
699 if (p->pfik_ah_head == NULL) {
703 TAILQ_INIT(p->pfik_ah_head);
704 TAILQ_INIT(&p->pfik_grouphead);
705 strlcpy(p->pfik_name, name, sizeof(p->pfik_name));
706 RB_INIT(&p->pfik_lan_ext);
707 RB_INIT(&p->pfik_ext_gwy);
708 p->pfik_flags = flags;
710 p->pfik_tzero = time_second;
712 RB_INSERT(pfi_ifhead, &pfi_ifs, p);
715 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances);
722 pfi_maybe_destroy(struct pfi_kif *p)
725 struct pfi_kif *q = p->pfik_parent;
727 if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) ||
728 p->pfik_rules > 0 || p->pfik_states > 0)
729 if (!(p->pfik_flags & PFI_IFLAG_PLACEHOLDER))
734 for (i = 0; i < 2; i++)
735 for (j = 0; j < 2; j++)
736 for (k = 0; k < 2; k++) {
737 q->pfik_bytes[i][j][k] +=
738 p->pfik_bytes[i][j][k];
739 q->pfik_packets[i][j][k] +=
740 p->pfik_packets[i][j][k];
741 /* clear stats in case we return to the dummy group */
742 p->pfik_bytes[i][j][k] = 0;
743 p->pfik_packets[i][j][k] = 0;
746 TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances);
748 if (p->pfik_rules > 0 || p->pfik_states > 0) {
749 /* move back to the dummy group */
750 p->pfik_parent = pfi_dummy;
751 pfi_dummy->pfik_addcnt++;
752 TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p,
758 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
761 kfree(p->pfik_ah_head, PFI_MTYPE);
767 pfi_copy_group(char *p, const char *q, int m)
769 while (m > 1 && *q && !(*q >= '0' && *q <= '9')) {
778 pfi_dynamic_drivers(void)
783 * For FreeBSD basically every interface is "dynamic" as we can unload
786 TAILQ_FOREACH(ifp, &ifnet, if_link) {
787 if (ifp->if_dunit == IF_DUNIT_NONE)
789 pfi_newgroup(ifp->if_dname, PFI_IFLAG_DYNAMIC);
794 pfi_newgroup(const char *name, int flags)
798 p = pfi_lookup_if(name);
800 p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP);
802 kprintf("pfi_newgroup: cannot allocate '%s' group", name);
805 p->pfik_flags |= flags;
809 pfi_fill_oldstatus(struct pf_status *pfs)
811 struct pfi_kif *p, key;
814 strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
816 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
821 bzero(pfs->pcounters, sizeof(pfs->pcounters));
822 bzero(pfs->bcounters, sizeof(pfs->bcounters));
823 for (i = 0; i < 2; i++)
824 for (j = 0; j < 2; j++)
825 for (k = 0; k < 2; k++) {
826 pfs->pcounters[i][j][k] =
827 p->pfik_packets[i][j][k];
828 pfs->bcounters[i][j] +=
829 p->pfik_bytes[i][j][k];
835 pfi_clr_istats(const char *name, int *nzero, int flags)
839 long tzero = time_second;
842 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
843 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
844 if (pfi_skip_if(name, p, flags))
846 bzero(p->pfik_packets, sizeof(p->pfik_packets));
847 bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
848 p->pfik_tzero = tzero;
858 pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags)
863 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
865 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
866 if (pfi_skip_if(name, p, flags))
870 p->pfik_tzero = boottime.tv_sec;
871 if (copyout(p, buf++, sizeof(*buf))) {
883 pfi_lookup_if(const char *name)
885 struct pfi_kif *p, key;
887 strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
888 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
893 pfi_skip_if(const char *filter, struct pfi_kif *p, int f)
897 if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP))
899 if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE))
901 if (filter == NULL || !*filter)
903 if (!strcmp(p->pfik_name, filter))
904 return (0); /* exact match */
906 if (n < 1 || n >= IFNAMSIZ)
907 return (1); /* sanity check */
908 if (filter[n-1] >= '0' && filter[n-1] <= '9')
909 return (1); /* only do exact match in that case */
910 if (strncmp(p->pfik_name, filter, n))
911 return (1); /* prefix doesn't match */
912 return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
915 /* from pf_print_state.c */
917 pfi_unmask(void *addr)
919 struct pf_addr *m = addr;
920 int i = 31, j = 0, b = 0;
923 while (j < 4 && m->addr32[j] == 0xffffffff) {
928 tmp = ntohl(m->addr32[j]);
929 for (i = 31; tmp & (1 << i); --i)
936 pfi_dohooks(struct pfi_kif *p)
938 for (; p != NULL; p = p->pfik_parent)
939 dohooks(p->pfik_ah_head, 0);
943 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
946 switch (dyn->pfid_acnt4) {
950 return (PF_MATCHA(0, &dyn->pfid_addr4,
951 &dyn->pfid_mask4, a, AF_INET));
953 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
956 switch (dyn->pfid_acnt6) {
960 return (PF_MATCHA(0, &dyn->pfid_addr6,
961 &dyn->pfid_mask6, a, AF_INET6));
963 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));