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.8 2006/12/22 23:44:57 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);
255 panic("pfi_attach_ifnet: "
256 "cannot allocate translation table");
262 kfree(mp, PFI_MTYPE);
265 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
266 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
267 /* some additional trickery for placeholders */
268 if ((p == NULL) || (p->pfik_parent == pfi_dummy)) {
269 /* are we looking at a renamed instance or not? */
270 pfi_copy_group(key.pfik_name, ifp->if_xname,
271 sizeof(key.pfik_name));
272 realname = (strncmp(key.pfik_name, ifp->if_dname,
273 sizeof(key.pfik_name)) == 0);
275 /* we can change if_xname, hence use if_dname as group id */
276 pfi_copy_group(key.pfik_name, ifp->if_dname,
277 sizeof(key.pfik_name));
278 q = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
280 q = pfi_if_create(key.pfik_name, pfi_self,
281 PFI_IFLAG_GROUP|PFI_IFLAG_DYNAMIC);
282 else if (q->pfik_parent == pfi_dummy) {
283 q->pfik_parent = pfi_self;
284 q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC);
287 panic("pfi_attach_ifnet: "
288 "cannot allocate '%s' group", key.pfik_name);
290 /* add/modify interface */
292 p = pfi_if_create(ifp->if_xname, q,
293 realname?PFI_IFLAG_INSTANCE:PFI_IFLAG_PLACEHOLDER);
295 /* remove from the dummy group */
296 /* XXX: copy stats? We should not have any!!! */
297 pfi_dummy->pfik_delcnt++;
298 TAILQ_REMOVE(&pfi_dummy->pfik_grouphead, p,
300 /* move to the right group */
303 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p,
306 p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER;
307 p->pfik_flags |= PFI_IFLAG_INSTANCE;
311 panic("pfi_attach_ifnet: "
312 "cannot allocate '%s' interface", ifp->if_xname);
316 p->pfik_flags |= PFI_IFLAG_ATTACHED;
317 p->pfik_ah_cookie = EVENTHANDLER_REGISTER(ifaddr_event,
318 pfi_kifaddr_update_event, p, EVENTHANDLER_PRI_ANY);
319 pfi_index2kif[ifp->if_index] = p;
325 pfi_detach_ifnet(struct ifnet *ifp)
327 struct pfi_kif *p, *q, key;
329 strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name));
333 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
335 kprintf("pfi_detach_ifnet: cannot find %s", ifp->if_xname);
339 EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie);
342 p->pfik_flags &= ~PFI_IFLAG_ATTACHED;
343 pfi_index2kif[ifp->if_index] = NULL;
345 pfi_maybe_destroy(p);
350 pfi_lookup_create(const char *name)
352 struct pfi_kif *p, *q, key;
355 p = pfi_lookup_if(name);
357 pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name));
358 q = pfi_lookup_if(key.pfik_name);
359 if ((q != NULL) && (q->pfik_parent != pfi_dummy))
360 p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE);
362 if (pfi_dummy == NULL)
363 panic("no 'notyet' dummy group");
364 p = pfi_if_create(name, pfi_dummy,
365 PFI_IFLAG_PLACEHOLDER);
373 pfi_attach_rule(const char *name)
377 p = pfi_lookup_create(name);
384 pfi_detach_rule(struct pfi_kif *p)
388 if (p->pfik_rules > 0)
391 kprintf("pfi_detach_rule: reference count at 0\n");
392 pfi_maybe_destroy(p);
396 pfi_attach_state(struct pfi_kif *p)
398 if (!p->pfik_states++)
399 TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states);
403 pfi_detach_state(struct pfi_kif *p)
407 if (p->pfik_states <= 0) {
408 kprintf("pfi_detach_state: reference count <= 0\n");
411 if (!--p->pfik_states)
412 TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states);
413 pfi_maybe_destroy(p);
417 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
419 struct pfi_dynaddr *dyn;
420 char tblname[PF_TABLE_NAME_SIZE];
421 struct pf_ruleset *ruleset = NULL;
424 if (aw->type != PF_ADDR_DYNIFTL)
426 dyn = pool_get(&pfi_addr_pl, PR_NOWAIT);
429 bzero(dyn, sizeof(*dyn));
432 dyn->pfid_kif = pfi_attach_rule(aw->v.ifname);
433 if (dyn->pfid_kif == NULL)
436 dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
437 if (af == AF_INET && dyn->pfid_net == 32)
439 strlcpy(tblname, aw->v.ifname, sizeof(tblname));
440 if (aw->iflags & PFI_AFLAG_NETWORK)
441 strlcat(tblname, ":network", sizeof(tblname));
442 if (aw->iflags & PFI_AFLAG_BROADCAST)
443 strlcat(tblname, ":broadcast", sizeof(tblname));
444 if (aw->iflags & PFI_AFLAG_PEER)
445 strlcat(tblname, ":peer", sizeof(tblname));
446 if (aw->iflags & PFI_AFLAG_NOALIAS)
447 strlcat(tblname, ":0", sizeof(tblname));
448 if (dyn->pfid_net != 128)
449 ksnprintf(tblname + strlen(tblname),
450 sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
451 ruleset = pf_find_or_create_ruleset(pfi_reserved_anchor,
452 pfi_interface_ruleset);
456 dyn->pfid_kt = pfr_attach_table(ruleset, tblname);
457 if (dyn->pfid_kt == NULL)
460 dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
461 dyn->pfid_iflags = aw->iflags;
463 dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1,
464 pfi_dynaddr_update, dyn);
465 if (dyn->pfid_hook_cookie == NULL)
469 pfi_dynaddr_update(aw->p.dyn);
474 if (dyn->pfid_kt != NULL)
475 pfr_detach_table(dyn->pfid_kt);
477 pf_remove_if_empty_ruleset(ruleset);
478 if (dyn->pfid_kif != NULL)
479 pfi_detach_rule(dyn->pfid_kif);
480 pool_put(&pfi_addr_pl, dyn);
486 pfi_dynaddr_update(void *p)
488 struct pfi_dynaddr *dyn = (struct pfi_dynaddr *)p;
489 struct pfi_kif *kif = dyn->pfid_kif;
490 struct pfr_ktable *kt = dyn->pfid_kt;
492 if (dyn == NULL || kif == NULL || kt == NULL)
493 panic("pfi_dynaddr_update");
494 if (kt->pfrkt_larg != pfi_update) {
495 /* this table needs to be brought up-to-date */
496 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
497 kt->pfrkt_larg = pfi_update;
499 pfr_dynaddr_update(kt, dyn);
503 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
509 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) {
510 pfr_clr_addrs(&kt->pfrkt_t, NULL, 0);
514 if ((kif->pfik_flags & PFI_IFLAG_INSTANCE))
515 pfi_instance_add(kif->pfik_ifp, net, flags);
516 else if (strcmp(kif->pfik_name, "self")) {
517 TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances)
518 pfi_instance_add(p->pfik_ifp, net, flags);
520 RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
521 if (p->pfik_flags & PFI_IFLAG_INSTANCE)
522 pfi_instance_add(p->pfik_ifp, net, flags);
526 if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2,
527 NULL, NULL, NULL, 0)))
528 kprintf("pfi_table_update: cannot set %d new addresses "
529 "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
533 pfi_instance_add(struct ifnet *ifp, int net, int flags)
536 int got4 = 0, got6 = 0;
541 TAILQ_FOREACH(ia, &ifp->if_addrlist, ifa_list) {
542 if (ia->ifa_addr == NULL)
544 af = ia->ifa_addr->sa_family;
545 if (af != AF_INET && af != AF_INET6)
548 * XXX: For point-to-point interfaces, (ifname:0) and IPv4,
549 * jump over address without a proper route to work
550 * around a problem with ppp not fully removing the
551 * address used during IPCP.
553 if ((ifp->if_flags & IFF_POINTOPOINT) &&
554 !(ia->ifa_flags & IFA_ROUTE) &&
555 (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET))
557 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
559 if ((flags & PFI_AFLAG_BROADCAST) &&
560 !(ifp->if_flags & IFF_BROADCAST))
562 if ((flags & PFI_AFLAG_PEER) &&
563 !(ifp->if_flags & IFF_POINTOPOINT))
565 if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
566 IN6_IS_ADDR_LINKLOCAL(
567 &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr))
569 if (flags & PFI_AFLAG_NOALIAS) {
570 if (af == AF_INET && got4)
572 if (af == AF_INET6 && got6)
580 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
582 net2 = pfi_unmask(&((struct sockaddr_in *)
583 ia->ifa_netmask)->sin_addr);
585 net2 = pfi_unmask(&((struct sockaddr_in6 *)
586 ia->ifa_netmask)->sin6_addr);
589 if (af == AF_INET && net2 > 32)
591 if (flags & PFI_AFLAG_BROADCAST)
592 pfi_address_add(ia->ifa_broadaddr, af, net2);
593 else if (flags & PFI_AFLAG_PEER)
594 pfi_address_add(ia->ifa_dstaddr, af, net2);
596 pfi_address_add(ia->ifa_addr, af, net2);
601 pfi_address_add(struct sockaddr *sa, int af, int net)
606 if (pfi_buffer_cnt >= pfi_buffer_max) {
607 int new_max = pfi_buffer_max * 2;
609 if (new_max > PFI_BUFFER_MAX) {
610 kprintf("pfi_address_add: address buffer full (%d/%d)\n",
611 pfi_buffer_cnt, PFI_BUFFER_MAX);
614 p = kmalloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
617 kprintf("pfi_address_add: no memory to grow buffer "
618 "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
621 memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof(*pfi_buffer));
622 /* no need to zero buffer */
623 kfree(pfi_buffer, PFI_MTYPE);
625 pfi_buffer_max = new_max;
627 if (af == AF_INET && net > 32)
629 p = pfi_buffer + pfi_buffer_cnt++;
630 bzero(p, sizeof(*p));
634 p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
635 if (af == AF_INET6) {
636 p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
637 if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr))
638 p->pfra_ip6addr.s6_addr16[1] = 0;
640 /* mask network address bits */
642 ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
643 for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
648 pfi_dynaddr_remove(struct pf_addr_wrap *aw)
650 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
651 aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
655 hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head,
656 aw->p.dyn->pfid_hook_cookie);
657 pfi_detach_rule(aw->p.dyn->pfid_kif);
658 aw->p.dyn->pfid_kif = NULL;
659 pfr_detach_table(aw->p.dyn->pfid_kt);
660 aw->p.dyn->pfid_kt = NULL;
661 pool_put(&pfi_addr_pl, aw->p.dyn);
667 pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
669 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
670 aw->p.dyn->pfid_kif == NULL)
672 aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
676 pfi_kifaddr_update(void *v)
685 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
687 return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
691 pfi_if_create(const char *name, struct pfi_kif *q, int flags)
695 p = kmalloc(sizeof(*p), PFI_MTYPE, M_NOWAIT);
698 bzero(p, sizeof(*p));
699 p->pfik_ah_head = kmalloc(sizeof(*p->pfik_ah_head), PFI_MTYPE,
701 if (p->pfik_ah_head == NULL) {
705 bzero(p->pfik_ah_head, sizeof(*p->pfik_ah_head));
706 TAILQ_INIT(p->pfik_ah_head);
707 TAILQ_INIT(&p->pfik_grouphead);
708 strlcpy(p->pfik_name, name, sizeof(p->pfik_name));
709 RB_INIT(&p->pfik_lan_ext);
710 RB_INIT(&p->pfik_ext_gwy);
711 p->pfik_flags = flags;
713 p->pfik_tzero = time_second;
715 RB_INSERT(pfi_ifhead, &pfi_ifs, p);
718 TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances);
725 pfi_maybe_destroy(struct pfi_kif *p)
728 struct pfi_kif *q = p->pfik_parent;
730 if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) ||
731 p->pfik_rules > 0 || p->pfik_states > 0)
732 if (!(p->pfik_flags & PFI_IFLAG_PLACEHOLDER))
737 for (i = 0; i < 2; i++)
738 for (j = 0; j < 2; j++)
739 for (k = 0; k < 2; k++) {
740 q->pfik_bytes[i][j][k] +=
741 p->pfik_bytes[i][j][k];
742 q->pfik_packets[i][j][k] +=
743 p->pfik_packets[i][j][k];
744 /* clear stats in case we return to the dummy group */
745 p->pfik_bytes[i][j][k] = 0;
746 p->pfik_packets[i][j][k] = 0;
749 TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances);
751 if (p->pfik_rules > 0 || p->pfik_states > 0) {
752 /* move back to the dummy group */
753 p->pfik_parent = pfi_dummy;
754 pfi_dummy->pfik_addcnt++;
755 TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p,
761 RB_REMOVE(pfi_ifhead, &pfi_ifs, p);
764 kfree(p->pfik_ah_head, PFI_MTYPE);
770 pfi_copy_group(char *p, const char *q, int m)
772 while (m > 1 && *q && !(*q >= '0' && *q <= '9')) {
781 pfi_dynamic_drivers(void)
786 * For FreeBSD basically every interface is "dynamic" as we can unload
789 TAILQ_FOREACH(ifp, &ifnet, if_link) {
790 if (ifp->if_dunit == IF_DUNIT_NONE)
792 pfi_newgroup(ifp->if_dname, PFI_IFLAG_DYNAMIC);
797 pfi_newgroup(const char *name, int flags)
801 p = pfi_lookup_if(name);
803 p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP);
805 kprintf("pfi_newgroup: cannot allocate '%s' group", name);
808 p->pfik_flags |= flags;
812 pfi_fill_oldstatus(struct pf_status *pfs)
814 struct pfi_kif *p, key;
817 strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name));
819 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
824 bzero(pfs->pcounters, sizeof(pfs->pcounters));
825 bzero(pfs->bcounters, sizeof(pfs->bcounters));
826 for (i = 0; i < 2; i++)
827 for (j = 0; j < 2; j++)
828 for (k = 0; k < 2; k++) {
829 pfs->pcounters[i][j][k] =
830 p->pfik_packets[i][j][k];
831 pfs->bcounters[i][j] +=
832 p->pfik_bytes[i][j][k];
838 pfi_clr_istats(const char *name, int *nzero, int flags)
842 long tzero = time_second;
845 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
846 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
847 if (pfi_skip_if(name, p, flags))
849 bzero(p->pfik_packets, sizeof(p->pfik_packets));
850 bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
851 p->pfik_tzero = tzero;
861 pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags)
866 ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE);
868 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
869 if (pfi_skip_if(name, p, flags))
873 p->pfik_tzero = boottime.tv_sec;
874 if (copyout(p, buf++, sizeof(*buf))) {
886 pfi_lookup_if(const char *name)
888 struct pfi_kif *p, key;
890 strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
891 p = RB_FIND(pfi_ifhead, &pfi_ifs, &key);
896 pfi_skip_if(const char *filter, struct pfi_kif *p, int f)
900 if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP))
902 if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE))
904 if (filter == NULL || !*filter)
906 if (!strcmp(p->pfik_name, filter))
907 return (0); /* exact match */
909 if (n < 1 || n >= IFNAMSIZ)
910 return (1); /* sanity check */
911 if (filter[n-1] >= '0' && filter[n-1] <= '9')
912 return (1); /* only do exact match in that case */
913 if (strncmp(p->pfik_name, filter, n))
914 return (1); /* prefix doesn't match */
915 return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9');
918 /* from pf_print_state.c */
920 pfi_unmask(void *addr)
922 struct pf_addr *m = addr;
923 int i = 31, j = 0, b = 0;
926 while (j < 4 && m->addr32[j] == 0xffffffff) {
931 tmp = ntohl(m->addr32[j]);
932 for (i = 31; tmp & (1 << i); --i)
939 pfi_dohooks(struct pfi_kif *p)
941 for (; p != NULL; p = p->pfik_parent)
942 dohooks(p->pfik_ah_head, 0);
946 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
949 switch (dyn->pfid_acnt4) {
953 return (PF_MATCHA(0, &dyn->pfid_addr4,
954 &dyn->pfid_mask4, a, AF_INET));
956 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
959 switch (dyn->pfid_acnt6) {
963 return (PF_MATCHA(0, &dyn->pfid_addr6,
964 &dyn->pfid_mask6, a, AF_INET6));
966 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));