diff options
Diffstat (limited to 'sys/contrib/pf/net/pf_if.c')
-rw-r--r-- | sys/contrib/pf/net/pf_if.c | 995 |
1 files changed, 367 insertions, 628 deletions
diff --git a/sys/contrib/pf/net/pf_if.c b/sys/contrib/pf/net/pf_if.c index 8e35070..005e1b6 100644 --- a/sys/contrib/pf/net/pf_if.c +++ b/sys/contrib/pf/net/pf_if.c @@ -1,7 +1,8 @@ -/* $FreeBSD$ */ -/* $OpenBSD: pf_if.c,v 1.23 2004/12/22 17:17:55 dhartmei Exp $ */ +/* $OpenBSD: pf_if.c,v 1.46 2006/12/13 09:01:59 itojun Exp $ */ /* + * Copyright 2005 Henning Brauer <henning@openbsd.org> + * Copyright 2005 Ryan McBride <mcbride@openbsd.org> * Copyright (c) 2001 Daniel Hartmeier * Copyright (c) 2003 Cedric Berger * All rights reserved. @@ -34,6 +35,9 @@ #if defined(__FreeBSD__) #include "opt_inet.h" #include "opt_inet6.h" + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); #endif #include <sys/param.h> @@ -66,74 +70,58 @@ #include <netinet/ip6.h> #endif /* INET6 */ -#define ACCEPT_FLAGS(oklist) \ - do { \ - if ((flags & ~(oklist)) & \ - PFI_FLAG_ALLMASK) \ - return (EINVAL); \ - } while (0) - -#define senderr(e) do { rv = (e); goto _bad; } while (0) - -struct pfi_kif **pfi_index2kif; -struct pfi_kif *pfi_self, *pfi_dummy; -int pfi_indexlim; -struct pfi_ifhead pfi_ifs; +struct pfi_kif *pfi_all = NULL; struct pfi_statehead pfi_statehead; -int pfi_ifcnt; #ifdef __FreeBSD__ uma_zone_t pfi_addr_pl; #else struct pool pfi_addr_pl; #endif +struct pfi_ifhead pfi_ifs; long pfi_update = 1; struct pfr_addr *pfi_buffer; int pfi_buffer_cnt; int pfi_buffer_max; #ifdef __FreeBSD__ -eventhandler_tag pfi_clone_cookie = NULL; -eventhandler_tag pfi_attach_cookie = NULL; -eventhandler_tag pfi_detach_cookie = NULL; +eventhandler_tag pfi_attach_cookie = NULL; +eventhandler_tag pfi_detach_cookie = NULL; +eventhandler_tag pfi_attach_group_cookie = NULL; +eventhandler_tag pfi_change_group_cookie = NULL; +eventhandler_tag pfi_detach_group_cookie = NULL; +eventhandler_tag pfi_ifaddr_event_cookie = NULL; #endif -void pfi_dynaddr_update(void *); -void pfi_kifaddr_update(void *); +void pfi_kif_update(struct pfi_kif *); +void pfi_dynaddr_update(struct pfi_dynaddr *dyn); void pfi_table_update(struct pfr_ktable *, struct pfi_kif *, int, int); +void pfi_kifaddr_update(void *); void pfi_instance_add(struct ifnet *, int, int); void pfi_address_add(struct sockaddr *, int, int); int pfi_if_compare(struct pfi_kif *, struct pfi_kif *); -struct pfi_kif *pfi_if_create(const char *, struct pfi_kif *, int); -void pfi_copy_group(char *, const char *, int); -void pfi_newgroup(const char *, int); -int pfi_skip_if(const char *, struct pfi_kif *, int); +int pfi_skip_if(const char *, struct pfi_kif *); int pfi_unmask(void *); -void pfi_dohooks(struct pfi_kif *); #ifdef __FreeBSD__ -void pfi_kifaddr_update_event(void *, struct ifnet *); -void pfi_attach_clone_event(void * __unused, struct if_clone *); void pfi_attach_ifnet_event(void * __unused, struct ifnet *); void pfi_detach_ifnet_event(void * __unused, struct ifnet *); +void pfi_attach_group_event(void * __unused, struct ifg_group *); +void pfi_change_group_event(void * __unused, char *); +void pfi_detach_group_event(void * __unused, struct ifg_group *); +void pfi_ifaddr_event(void * __unused, struct ifnet *); + +extern struct ifgrouphead ifg_head; #endif RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); #define PFI_BUFFER_MAX 0x10000 -#ifdef __FreeBSD__ -MALLOC_DEFINE(PFI_MTYPE, "pf_if", "pf interface table"); -#else #define PFI_MTYPE M_IFADDR -#endif void pfi_initialize(void) { -#ifdef __FreeBSD__ - struct ifnet *ifp; -#endif - - if (pfi_self != NULL) /* already initialized */ + if (pfi_all != NULL) /* already initialized */ return; TAILQ_INIT(&pfi_statehead); @@ -144,362 +132,268 @@ pfi_initialize(void) pfi_buffer_max = 64; pfi_buffer = malloc(pfi_buffer_max * sizeof(*pfi_buffer), PFI_MTYPE, M_WAITOK); - pfi_self = pfi_if_create("self", NULL, PFI_IFLAG_GROUP); + + if ((pfi_all = pfi_kif_get(IFG_ALL)) == NULL) + panic("pfi_kif_get for pfi_all failed"); + #ifdef __FreeBSD__ - /* XXX_IMPORT */ - PF_LOCK(); + struct ifg_group *ifg; + struct ifnet *ifp; + IFNET_RLOCK(); - TAILQ_FOREACH(ifp, &ifnet, if_link) { - IFNET_RUNLOCK(); + TAILQ_FOREACH(ifg, &ifg_head, ifg_next) + pfi_attach_ifgroup(ifg); + TAILQ_FOREACH(ifp, &ifnet, if_link) pfi_attach_ifnet(ifp); - IFNET_RLOCK(); - } IFNET_RUNLOCK(); - PF_UNLOCK(); - pfi_dummy = pfi_if_create("notyet", pfi_self, - PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC); + pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event, pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event, pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); - pfi_clone_cookie = EVENTHANDLER_REGISTER(if_clone_event, - pfi_attach_clone_event, NULL, EVENTHANDLER_PRI_ANY); + pfi_attach_group_cookie = EVENTHANDLER_REGISTER(group_attach_event, + pfi_attach_group_event, NULL, EVENTHANDLER_PRI_ANY); + pfi_change_group_cookie = EVENTHANDLER_REGISTER(group_change_event, + pfi_change_group_event, NULL, EVENTHANDLER_PRI_ANY); + pfi_detach_group_cookie = EVENTHANDLER_REGISTER(group_detach_event, + pfi_detach_group_event, NULL, EVENTHANDLER_PRI_ANY); + pfi_ifaddr_event_cookie = EVENTHANDLER_REGISTER(ifaddr_event, + pfi_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY); #endif } -#ifdef __FreeBSD__ -void -pfi_cleanup(void) +struct pfi_kif * +pfi_kif_get(const char *kif_name) { - struct pfi_kif *p, key; - struct ifnet *ifp; - - PF_ASSERT(MA_OWNED); + struct pfi_kif *kif; + struct pfi_kif_cmp s; - PF_UNLOCK(); - EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie); - EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie); - EVENTHANDLER_DEREGISTER(if_clone_event, pfi_clone_cookie); - PF_LOCK(); + bzero(&s, sizeof(s)); + strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name)); + if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&s)) != NULL) + return (kif); - IFNET_RLOCK(); - /* release PFI_IFLAG_INSTANCE */ - TAILQ_FOREACH(ifp, &ifnet, if_link) { - strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name)); - p = RB_FIND(pfi_ifhead, &pfi_ifs, &key); - if (p != NULL) { - IFNET_RUNLOCK(); - pfi_detach_ifnet(ifp); - IFNET_RLOCK(); - } - } - IFNET_RUNLOCK(); + /* create new one */ +#ifdef __FreeBSD__ + if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) +#else + if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_DONTWAIT)) == NULL) +#endif + return (NULL); - /* XXX clear all other interface group */ - while ((p = RB_MIN(pfi_ifhead, &pfi_ifs))) { - RB_REMOVE(pfi_ifhead, &pfi_ifs, p); + bzero(kif, sizeof(*kif)); + strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name)); + kif->pfik_tzero = time_second; + TAILQ_INIT(&kif->pfik_dynaddrs); - free(p->pfik_ah_head, PFI_MTYPE); - free(p, PFI_MTYPE); - } - free(pfi_index2kif, PFI_MTYPE); - free(pfi_buffer, PFI_MTYPE); - pfi_index2kif = NULL; - pfi_buffer = NULL; - pfi_self = NULL; + RB_INSERT(pfi_ifhead, &pfi_ifs, kif); + return (kif); } -/* - * Wrapper functions for FreeBSD eventhandler - */ void -pfi_kifaddr_update_event(void *arg, struct ifnet *ifp) +pfi_kif_ref(struct pfi_kif *kif, enum pfi_kif_refs what) { - struct pfi_kif *p = arg; - - PF_LOCK(); - /* - * Check to see if it is 'our' interface as we do not have per - * interface hooks and thus get an update for every interface. - */ - if (p && p->pfik_ifp == ifp) - pfi_kifaddr_update(p); - PF_UNLOCK(); + switch (what) { + case PFI_KIF_REF_RULE: + kif->pfik_rules++; + break; + case PFI_KIF_REF_STATE: + if (!kif->pfik_states++) + TAILQ_INSERT_TAIL(&pfi_statehead, kif, pfik_w_states); + break; + default: + panic("pfi_kif_ref with unknown type"); + } } void -pfi_attach_clone_event(void *arg __unused, struct if_clone *ifc) +pfi_kif_unref(struct pfi_kif *kif, enum pfi_kif_refs what) { - PF_LOCK(); - pfi_attach_clone(ifc); - PF_UNLOCK(); -} + if (kif == NULL) + return; -void -pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp) -{ - PF_LOCK(); - pfi_attach_ifnet(ifp); - PF_UNLOCK(); -} + switch (what) { + case PFI_KIF_REF_NONE: + break; + case PFI_KIF_REF_RULE: + if (kif->pfik_rules <= 0) { + printf("pfi_kif_unref: rules refcount <= 0\n"); + return; + } + kif->pfik_rules--; + break; + case PFI_KIF_REF_STATE: + if (kif->pfik_states <= 0) { + printf("pfi_kif_unref: state refcount <= 0\n"); + return; + } + if (!--kif->pfik_states) + TAILQ_REMOVE(&pfi_statehead, kif, pfik_w_states); + break; + default: + panic("pfi_kif_unref with unknown type"); + } -void -pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp) -{ - PF_LOCK(); - pfi_detach_ifnet(ifp); - PF_UNLOCK(); + if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == pfi_all) + return; + + if (kif->pfik_rules || kif->pfik_states) + return; + + RB_REMOVE(pfi_ifhead, &pfi_ifs, kif); + free(kif, PFI_MTYPE); } -#endif /* __FreeBSD__ */ -void -pfi_attach_clone(struct if_clone *ifc) +int +pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif) { - pfi_initialize(); - pfi_newgroup(ifc->ifc_name, PFI_IFLAG_CLONABLE); + struct ifg_list *p; + + if (rule_kif == NULL || rule_kif == packet_kif) + return (1); + + if (rule_kif->pfik_group != NULL) + TAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next) + if (p->ifgl_group == rule_kif->pfik_group) + return (1); + + return (0); } void pfi_attach_ifnet(struct ifnet *ifp) { - struct pfi_kif *p, *q, key; - int s; -#ifdef __FreeBSD__ - int realname; -#endif + struct pfi_kif *kif; + int s; pfi_initialize(); s = splsoftnet(); pfi_update++; - if (ifp->if_index >= pfi_indexlim) { - /* - * grow pfi_index2kif, similar to ifindex2ifnet code in if.c - */ - size_t m, n, oldlim; - struct pfi_kif **mp, **np; - - oldlim = pfi_indexlim; - if (pfi_indexlim == 0) - pfi_indexlim = 64; - while (ifp->if_index >= pfi_indexlim) - pfi_indexlim <<= 1; - - m = oldlim * sizeof(struct pfi_kif *); - mp = pfi_index2kif; - n = pfi_indexlim * sizeof(struct pfi_kif *); -#ifdef __FreeBSD__ - np = malloc(n, PFI_MTYPE, M_NOWAIT); -#else - np = malloc(n, PFI_MTYPE, M_DONTWAIT); -#endif - if (np == NULL) - panic("pfi_attach_ifnet: " - "cannot allocate translation table"); - bzero(np, n); - if (mp != NULL) - bcopy(mp, np, m); - pfi_index2kif = np; - if (mp != NULL) - free(mp, PFI_MTYPE); - } + if ((kif = pfi_kif_get(ifp->if_xname)) == NULL) + panic("pfi_kif_get failed"); - strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name)); - p = RB_FIND(pfi_ifhead, &pfi_ifs, &key); -#ifdef __FreeBSD__ - /* some additional trickery for placeholders */ - if ((p == NULL) || (p->pfik_parent == pfi_dummy)) { - /* are we looking at a renamed instance or not? */ - pfi_copy_group(key.pfik_name, ifp->if_xname, - sizeof(key.pfik_name)); - realname = (strncmp(key.pfik_name, ifp->if_dname, - sizeof(key.pfik_name)) == 0); - /* add group */ - /* we can change if_xname, hence use if_dname as group id */ - pfi_copy_group(key.pfik_name, ifp->if_dname, - sizeof(key.pfik_name)); - q = RB_FIND(pfi_ifhead, &pfi_ifs, &key); - if (q == NULL) - q = pfi_if_create(key.pfik_name, pfi_self, - PFI_IFLAG_GROUP|PFI_IFLAG_DYNAMIC); - else if (q->pfik_parent == pfi_dummy) { - q->pfik_parent = pfi_self; - q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC); - } - if (q == NULL) - panic("pfi_attach_ifnet: " - "cannot allocate '%s' group", key.pfik_name); - - /* add/modify interface */ - if (p == NULL) - p = pfi_if_create(ifp->if_xname, q, PFI_IFLAG_INSTANCE | - (realname?0:PFI_IFLAG_PLACEHOLDER)); - else { - /* remove from the dummy group */ - /* XXX: copy stats? We should not have any!!! */ - pfi_dummy->pfik_delcnt++; - TAILQ_REMOVE(&pfi_dummy->pfik_grouphead, p, - pfik_instances); - /* move to the right group */ - p->pfik_parent = q; - q->pfik_addcnt++; - TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, - pfik_instances); - if (realname) - p->pfik_flags &= ~PFI_IFLAG_PLACEHOLDER; - p->pfik_flags |= PFI_IFLAG_INSTANCE; - } - if (p == NULL) - panic("pfi_attach_ifnet: " - "cannot allocate '%s' interface", ifp->if_xname); -#else - if (p == NULL) { - /* add group */ - pfi_copy_group(key.pfik_name, ifp->if_xname, - sizeof(key.pfik_name)); - q = RB_FIND(pfi_ifhead, &pfi_ifs, &key); - if (q == NULL) - q = pfi_if_create(key.pfik_name, pfi_self, PFI_IFLAG_GROUP); - else if (q->pfik_parent == pfi_dummy) { - q->pfik_parent = pfi_self; - q->pfik_flags = (PFI_IFLAG_GROUP | PFI_IFLAG_DYNAMIC); - } - if (q == NULL) - panic("pfi_attach_ifnet: " - "cannot allocate '%s' group", key.pfik_name); - - /* add interface */ - p = pfi_if_create(ifp->if_xname, q, PFI_IFLAG_INSTANCE); - if (p == NULL) - panic("pfi_attach_ifnet: " - "cannot allocate '%s' interface", ifp->if_xname); -#endif - } else - q = p->pfik_parent; - p->pfik_ifp = ifp; - p->pfik_flags |= PFI_IFLAG_ATTACHED; -#ifdef __FreeBSD__ - PF_UNLOCK(); - p->pfik_ah_cookie = EVENTHANDLER_REGISTER(ifaddr_event, - pfi_kifaddr_update_event, p, EVENTHANDLER_PRI_ANY); - PF_LOCK(); -#else - p->pfik_ah_cookie = - hook_establish(ifp->if_addrhooks, 1, pfi_kifaddr_update, p); + kif->pfik_ifp = ifp; + ifp->if_pf_kif = (caddr_t)kif; + +#ifndef __FreeBSD__ + if ((kif->pfik_ah_cookie = hook_establish(ifp->if_addrhooks, 1, + pfi_kifaddr_update, kif)) == NULL) + panic("pfi_attach_ifnet: cannot allocate '%s' address hook", + ifp->if_xname); #endif - pfi_index2kif[ifp->if_index] = p; - pfi_dohooks(p); + + pfi_kif_update(kif); + splx(s); } void pfi_detach_ifnet(struct ifnet *ifp) { - struct pfi_kif *p, *q, key; - int s; + int s; + struct pfi_kif *kif; - strlcpy(key.pfik_name, ifp->if_xname, sizeof(key.pfik_name)); + if ((kif = (struct pfi_kif *)ifp->if_pf_kif) == NULL) + return; s = splsoftnet(); pfi_update++; - p = RB_FIND(pfi_ifhead, &pfi_ifs, &key); - if (p == NULL) { - printf("pfi_detach_ifnet: cannot find %s", ifp->if_xname); - splx(s); - return; - } -#ifdef __FreeBSD__ - PF_UNLOCK(); - EVENTHANDLER_DEREGISTER(ifaddr_event, p->pfik_ah_cookie); - PF_LOCK(); -#else - hook_disestablish(p->pfik_ifp->if_addrhooks, p->pfik_ah_cookie); +#ifndef __FreeBSD__ + hook_disestablish(ifp->if_addrhooks, kif->pfik_ah_cookie); #endif - q = p->pfik_parent; - p->pfik_ifp = NULL; - p->pfik_flags &= ~PFI_IFLAG_ATTACHED; - pfi_index2kif[ifp->if_index] = NULL; - pfi_dohooks(p); - pfi_maybe_destroy(p); + pfi_kif_update(kif); + + kif->pfik_ifp = NULL; + ifp->if_pf_kif = NULL; + pfi_kif_unref(kif, PFI_KIF_REF_NONE); splx(s); } -struct pfi_kif * -pfi_lookup_create(const char *name) +void +pfi_attach_ifgroup(struct ifg_group *ifg) { - struct pfi_kif *p, *q, key; + struct pfi_kif *kif; int s; + pfi_initialize(); s = splsoftnet(); - p = pfi_lookup_if(name); - if (p == NULL) { - pfi_copy_group(key.pfik_name, name, sizeof(key.pfik_name)); - q = pfi_lookup_if(key.pfik_name); -#ifdef __FreeBSD__ - /* XXX_IMPORT */ - if ((q != NULL) && (q->pfik_parent != pfi_dummy)) - p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE); - else { - if (pfi_dummy == NULL) - panic("no 'notyet' dummy group"); - p = pfi_if_create(name, pfi_dummy, - PFI_IFLAG_PLACEHOLDER); - } -#else - if (q == NULL) { - pfi_newgroup(key.pfik_name, PFI_IFLAG_DYNAMIC); - q = pfi_lookup_if(key.pfik_name); - } - p = pfi_lookup_if(name); - if (p == NULL && q != NULL) - p = pfi_if_create(name, q, PFI_IFLAG_INSTANCE); -#endif - } - splx(s); - return (p); -} + pfi_update++; + if ((kif = pfi_kif_get(ifg->ifg_group)) == NULL) + panic("pfi_kif_get failed"); -struct pfi_kif * -pfi_attach_rule(const char *name) -{ - struct pfi_kif *p; + kif->pfik_group = ifg; + ifg->ifg_pf_kif = (caddr_t)kif; - p = pfi_lookup_create(name); - if (p != NULL) - p->pfik_rules++; - return (p); + splx(s); } void -pfi_detach_rule(struct pfi_kif *p) +pfi_detach_ifgroup(struct ifg_group *ifg) { - if (p == NULL) + int s; + struct pfi_kif *kif; + + if ((kif = (struct pfi_kif *)ifg->ifg_pf_kif) == NULL) return; - if (p->pfik_rules > 0) - p->pfik_rules--; - else - printf("pfi_detach_rule: reference count at 0\n"); - pfi_maybe_destroy(p); + + s = splsoftnet(); + pfi_update++; + + kif->pfik_group = NULL; + ifg->ifg_pf_kif = NULL; + pfi_kif_unref(kif, PFI_KIF_REF_NONE); + splx(s); } void -pfi_attach_state(struct pfi_kif *p) +pfi_group_change(const char *group) { - if (!p->pfik_states++) - TAILQ_INSERT_TAIL(&pfi_statehead, p, pfik_w_states); + struct pfi_kif *kif; + int s; + + s = splsoftnet(); + pfi_update++; + if ((kif = pfi_kif_get(group)) == NULL) + panic("pfi_kif_get failed"); + + pfi_kif_update(kif); + + splx(s); } -void -pfi_detach_state(struct pfi_kif *p) +int +pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af) { - if (p == NULL) - return; - if (p->pfik_states <= 0) { - printf("pfi_detach_state: reference count <= 0\n"); - return; + switch (af) { +#ifdef INET + case AF_INET: + switch (dyn->pfid_acnt4) { + case 0: + return (0); + case 1: + return (PF_MATCHA(0, &dyn->pfid_addr4, + &dyn->pfid_mask4, a, AF_INET)); + default: + return (pfr_match_addr(dyn->pfid_kt, a, AF_INET)); + } + break; +#endif /* INET */ +#ifdef INET6 + case AF_INET6: + switch (dyn->pfid_acnt6) { + case 0: + return (0); + case 1: + return (PF_MATCHA(0, &dyn->pfid_addr6, + &dyn->pfid_mask6, a, AF_INET6)); + default: + return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6)); + } + break; +#endif /* INET6 */ + default: + return (0); } - if (!--p->pfik_states) - TAILQ_REMOVE(&pfi_statehead, p, pfik_w_states); - pfi_maybe_destroy(p); } int @@ -512,15 +406,20 @@ pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af) if (aw->type != PF_ADDR_DYNIFTL) return (0); - dyn = pool_get(&pfi_addr_pl, PR_NOWAIT); - if (dyn == NULL) + if ((dyn = pool_get(&pfi_addr_pl, PR_NOWAIT)) == NULL) return (1); bzero(dyn, sizeof(*dyn)); s = splsoftnet(); - dyn->pfid_kif = pfi_attach_rule(aw->v.ifname); - if (dyn->pfid_kif == NULL) - senderr(1); + if (!strcmp(aw->v.ifname, "self")) + dyn->pfid_kif = pfi_kif_get(IFG_ALL); + else + dyn->pfid_kif = pfi_kif_get(aw->v.ifname); + if (dyn->pfid_kif == NULL) { + rv = 1; + goto _bad; + } + pfi_kif_ref(dyn->pfid_kif, PFI_KIF_REF_RULE); dyn->pfid_net = pfi_unmask(&aw->v.a.mask); if (af == AF_INET && dyn->pfid_net == 32) @@ -537,24 +436,23 @@ pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af) if (dyn->pfid_net != 128) snprintf(tblname + strlen(tblname), sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net); - ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR); - if (ruleset == NULL) - senderr(1); + if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) { + rv = 1; + goto _bad; + } - dyn->pfid_kt = pfr_attach_table(ruleset, tblname); - if (dyn->pfid_kt == NULL) - senderr(1); + if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) { + rv = 1; + goto _bad; + } dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE; dyn->pfid_iflags = aw->iflags; dyn->pfid_af = af; - dyn->pfid_hook_cookie = hook_establish(dyn->pfid_kif->pfik_ah_head, 1, - pfi_dynaddr_update, dyn); - if (dyn->pfid_hook_cookie == NULL) - senderr(1); + TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry); aw->p.dyn = dyn; - pfi_dynaddr_update(aw->p.dyn); + pfi_kif_update(dyn->pfid_kif); splx(s); return (0); @@ -564,16 +462,32 @@ _bad: if (ruleset != NULL) pf_remove_if_empty_ruleset(ruleset); if (dyn->pfid_kif != NULL) - pfi_detach_rule(dyn->pfid_kif); + pfi_kif_unref(dyn->pfid_kif, PFI_KIF_REF_RULE); pool_put(&pfi_addr_pl, dyn); splx(s); return (rv); } void -pfi_dynaddr_update(void *p) +pfi_kif_update(struct pfi_kif *kif) +{ + struct ifg_list *ifgl; + struct pfi_dynaddr *p; + + /* update all dynaddr */ + TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry) + pfi_dynaddr_update(p); + + /* again for all groups kif is member of */ + if (kif->pfik_ifp != NULL) + TAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next) + pfi_kif_update((struct pfi_kif *) + ifgl->ifgl_group->ifg_pf_kif); +} + +void +pfi_dynaddr_update(struct pfi_dynaddr *dyn) { - struct pfi_dynaddr *dyn = (struct pfi_dynaddr *)p; struct pfi_kif *kif; struct pfr_ktable *kt; @@ -582,6 +496,7 @@ pfi_dynaddr_update(void *p) kif = dyn->pfid_kif; kt = dyn->pfid_kt; + if (kt->pfrkt_larg != pfi_update) { /* this table needs to be brought up-to-date */ pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags); @@ -594,28 +509,18 @@ void pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags) { int e, size2 = 0; - struct pfi_kif *p; - struct pfr_table t; + struct ifg_member *ifgm; - if ((kif->pfik_flags & PFI_IFLAG_INSTANCE) && kif->pfik_ifp == NULL) { - pfr_clr_addrs(&kt->pfrkt_t, NULL, 0); - return; - } pfi_buffer_cnt = 0; - if ((kif->pfik_flags & PFI_IFLAG_INSTANCE)) + + if (kif->pfik_ifp != NULL) pfi_instance_add(kif->pfik_ifp, net, flags); - else if (strcmp(kif->pfik_name, "self")) { - TAILQ_FOREACH(p, &kif->pfik_grouphead, pfik_instances) - pfi_instance_add(p->pfik_ifp, net, flags); - } else { - RB_FOREACH(p, pfi_ifhead, &pfi_ifs) - if (p->pfik_flags & PFI_IFLAG_INSTANCE) - pfi_instance_add(p->pfik_ifp, net, flags); - } - t = kt->pfrkt_t; - t.pfrt_flags = 0; - if ((e = pfr_set_addrs(&t, pfi_buffer, pfi_buffer_cnt, &size2, - NULL, NULL, NULL, 0))) + else if (kif->pfik_group != NULL) + TAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next) + pfi_instance_add(ifgm->ifgm_ifp, net, flags); + + if ((e = pfr_set_addrs(&kt->pfrkt_t, pfi_buffer, pfi_buffer_cnt, &size2, + NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK))) printf("pfi_table_update: cannot set %d new addresses " "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e); } @@ -635,18 +540,6 @@ pfi_instance_add(struct ifnet *ifp, int net, int flags) af = ia->ifa_addr->sa_family; if (af != AF_INET && af != AF_INET6) continue; -#ifdef __FreeBSD__ - /* - * XXX: For point-to-point interfaces, (ifname:0) and IPv4, - * jump over addresses without a proper route to work - * around a problem with ppp not fully removing the - * address used during IPCP. - */ - if ((ifp->if_flags & IFF_POINTOPOINT) && - !(ia->ifa_flags & IFA_ROUTE) && - (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET)) - continue; -#endif if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6) continue; if ((flags & PFI_AFLAG_BROADCAST) && @@ -671,13 +564,12 @@ pfi_instance_add(struct ifnet *ifp, int net, int flags) got6 = 1; net2 = net; if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) { - if (af == AF_INET) { + if (af == AF_INET) net2 = pfi_unmask(&((struct sockaddr_in *) ia->ifa_netmask)->sin_addr); - } else if (af == AF_INET6) { + else if (af == AF_INET6) net2 = pfi_unmask(&((struct sockaddr_in6 *) ia->ifa_netmask)->sin6_addr); - } } if (af == AF_INET && net2 > 32) net2 = 32; @@ -704,11 +596,10 @@ pfi_address_add(struct sockaddr *sa, int af, int net) pfi_buffer_cnt, PFI_BUFFER_MAX); return; } -#ifdef __FreeBSD__ p = malloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE, +#ifdef __FreeBSD__ M_NOWAIT); #else - p = malloc(new_max * sizeof(*pfi_buffer), PFI_MTYPE, M_DONTWAIT); #endif if (p == NULL) { @@ -730,9 +621,9 @@ pfi_address_add(struct sockaddr *sa, int af, int net) p->pfra_net = net; if (af == AF_INET) p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr; - if (af == AF_INET6) { + else if (af == AF_INET6) { p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr; - if (IN6_IS_ADDR_LINKLOCAL(&p->pfra_ip6addr)) + if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr)) p->pfra_ip6addr.s6_addr16[1] = 0; } /* mask network address bits */ @@ -752,9 +643,8 @@ pfi_dynaddr_remove(struct pf_addr_wrap *aw) return; s = splsoftnet(); - hook_disestablish(aw->p.dyn->pfid_kif->pfik_ah_head, - aw->p.dyn->pfid_hook_cookie); - pfi_detach_rule(aw->p.dyn->pfid_kif); + TAILQ_REMOVE(&aw->p.dyn->pfid_kif->pfik_dynaddrs, aw->p.dyn, entry); + pfi_kif_unref(aw->p.dyn->pfid_kif, PFI_KIF_REF_RULE); aw->p.dyn->pfid_kif = NULL; pfr_detach_table(aw->p.dyn->pfid_kt); aw->p.dyn->pfid_kt = NULL; @@ -775,11 +665,12 @@ pfi_dynaddr_copyout(struct pf_addr_wrap *aw) void pfi_kifaddr_update(void *v) { - int s; + int s; + struct pfi_kif *kif = (struct pfi_kif *)v; s = splsoftnet(); pfi_update++; - pfi_dohooks(v); + pfi_kif_update(kif); splx(s); } @@ -789,149 +680,16 @@ pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q) return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ)); } -struct pfi_kif * -pfi_if_create(const char *name, struct pfi_kif *q, int flags) -{ - struct pfi_kif *p; - -#ifdef __FreeBSD__ - p = malloc(sizeof(*p), PFI_MTYPE, M_NOWAIT); -#else - p = malloc(sizeof(*p), PFI_MTYPE, M_DONTWAIT); -#endif - if (p == NULL) - return (NULL); - bzero(p, sizeof(*p)); -#ifdef __FreeBSD__ - p->pfik_ah_head = malloc(sizeof(*p->pfik_ah_head), PFI_MTYPE, - M_NOWAIT); -#else - p->pfik_ah_head = malloc(sizeof(*p->pfik_ah_head), PFI_MTYPE, - M_DONTWAIT); -#endif - if (p->pfik_ah_head == NULL) { - free(p, PFI_MTYPE); - return (NULL); - } - bzero(p->pfik_ah_head, sizeof(*p->pfik_ah_head)); - TAILQ_INIT(p->pfik_ah_head); - TAILQ_INIT(&p->pfik_grouphead); - strlcpy(p->pfik_name, name, sizeof(p->pfik_name)); - RB_INIT(&p->pfik_lan_ext); - RB_INIT(&p->pfik_ext_gwy); - p->pfik_flags = flags; - p->pfik_parent = q; -#ifdef __FreeBSD__ - /* - * It seems that the value of time_second is in unintialzied state when - * pf sets interface statistics clear time in boot phase if pf was - * statically linked to kernel. Instead of setting the bogus time value - * have pfi_get_ifaces handle this case. In pfi_get_ifaces it uses - * boottime.tv_sec if it sees the time is 0. - */ - p->pfik_tzero = time_second > 1 ? time_second : 0; -#else - p->pfik_tzero = time_second; -#endif - - RB_INSERT(pfi_ifhead, &pfi_ifs, p); - if (q != NULL) { - q->pfik_addcnt++; - TAILQ_INSERT_TAIL(&q->pfik_grouphead, p, pfik_instances); - } - pfi_ifcnt++; - return (p); -} - -int -pfi_maybe_destroy(struct pfi_kif *p) -{ - int i, j, k, s; - struct pfi_kif *q = p->pfik_parent; - -#ifdef __FreeBSD__ - if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) || - ((p->pfik_rules > 0 || p->pfik_states > 0) && - (p->pfik_flags & PFI_IFLAG_PLACEHOLDER) == 0)) -#else - if ((p->pfik_flags & (PFI_IFLAG_ATTACHED | PFI_IFLAG_GROUP)) || - p->pfik_rules > 0 || p->pfik_states > 0) -#endif - return (0); - - s = splsoftnet(); - if (q != NULL) { - for (i = 0; i < 2; i++) - for (j = 0; j < 2; j++) - for (k = 0; k < 2; k++) { - q->pfik_bytes[i][j][k] += - p->pfik_bytes[i][j][k]; - q->pfik_packets[i][j][k] += - p->pfik_packets[i][j][k]; -#ifdef __FreeBSD__ - /* clear stats in case we return to the dummy group */ - p->pfik_bytes[i][j][k] = 0; - p->pfik_packets[i][j][k] = 0; -#endif - } - q->pfik_delcnt++; - TAILQ_REMOVE(&q->pfik_grouphead, p, pfik_instances); - } -#ifdef __FreeBSD__ - if (p->pfik_rules > 0 || p->pfik_states > 0) { - /* move back to the dummy group */ - p->pfik_parent = pfi_dummy; - p->pfik_flags &= ~PFI_IFLAG_INSTANCE; - pfi_dummy->pfik_addcnt++; - TAILQ_INSERT_TAIL(&pfi_dummy->pfik_grouphead, p, - pfik_instances); - return (0); - } -#endif - pfi_ifcnt--; - RB_REMOVE(pfi_ifhead, &pfi_ifs, p); - splx(s); - - free(p->pfik_ah_head, PFI_MTYPE); - free(p, PFI_MTYPE); - return (1); -} - -void -pfi_copy_group(char *p, const char *q, int m) -{ - while (m > 1 && *q && !(*q >= '0' && *q <= '9')) { - *p++ = *q++; - m--; - } - if (m > 0) - *p++ = '\0'; -} - -void -pfi_newgroup(const char *name, int flags) -{ - struct pfi_kif *p; - - p = pfi_lookup_if(name); - if (p == NULL) - p = pfi_if_create(name, pfi_self, PFI_IFLAG_GROUP); - if (p == NULL) { - printf("pfi_newgroup: cannot allocate '%s' group", name); - return; - } - p->pfik_flags |= flags; -} - void pfi_fill_oldstatus(struct pf_status *pfs) { - struct pfi_kif *p, key; - int i, j, k, s; + struct pfi_kif *p; + struct pfi_kif_cmp key; + int i, j, k, s; strlcpy(key.pfik_name, pfs->ifname, sizeof(key.pfik_name)); s = splsoftnet(); - p = RB_FIND(pfi_ifhead, &pfi_ifs, &key); + p = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&key); if (p == NULL) { splx(s); return; @@ -950,92 +708,46 @@ pfi_fill_oldstatus(struct pf_status *pfs) } int -pfi_clr_istats(const char *name, int *nzero, int flags) +pfi_clr_istats(const char *name) { struct pfi_kif *p; - int n = 0, s; - long tzero = time_second; + int s; - ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE); s = splsoftnet(); RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { - if (pfi_skip_if(name, p, flags)) + if (pfi_skip_if(name, p)) continue; bzero(p->pfik_packets, sizeof(p->pfik_packets)); bzero(p->pfik_bytes, sizeof(p->pfik_bytes)); - p->pfik_tzero = tzero; - n++; + p->pfik_tzero = time_second; } splx(s); - if (nzero != NULL) - *nzero = n; - return (0); -} -int -pfi_set_flags(const char *name, int flags) -{ - struct pfi_kif *p; - int s; - - if (flags & ~PFI_IFLAG_SETABLE_MASK) - return (EINVAL); - - s = splsoftnet(); - RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { - if (pfi_skip_if(name, p, PFI_FLAG_GROUP|PFI_FLAG_INSTANCE)) - continue; - p->pfik_flags |= flags; - } - splx(s); return (0); } int -pfi_clear_flags(const char *name, int flags) +pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size) { - struct pfi_kif *p; - int s; - - if (flags & ~PFI_IFLAG_SETABLE_MASK) - return (EINVAL); - - s = splsoftnet(); - RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { - if (pfi_skip_if(name, p, PFI_FLAG_GROUP|PFI_FLAG_INSTANCE)) - continue; - p->pfik_flags &= ~flags; - } - splx(s); - return (0); -} - -int -pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags) -{ - struct pfi_kif *p; + struct pfi_kif *p, *nextp; int s, n = 0; -#ifdef __FreeBSD__ - int ec; -#endif - ACCEPT_FLAGS(PFI_FLAG_GROUP|PFI_FLAG_INSTANCE); s = splsoftnet(); - RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { - if (pfi_skip_if(name, p, flags)) + for (p = RB_MIN(pfi_ifhead, &pfi_ifs); p; p = nextp) { + nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p); + if (pfi_skip_if(name, p)) continue; if (*size > n++) { if (!p->pfik_tzero) p->pfik_tzero = time_second; -#ifdef __FreeBSD__ - PF_COPYOUT(p, buf++, sizeof(*buf), ec); - if (ec) { -#else + pfi_kif_ref(p, PFI_KIF_REF_RULE); if (copyout(p, buf++, sizeof(*buf))) { -#endif + pfi_kif_unref(p, PFI_KIF_REF_RULE); splx(s); return (EFAULT); } + nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p); + pfi_kif_unref(p, PFI_KIF_REF_RULE); } } splx(s); @@ -1043,25 +755,11 @@ pfi_get_ifaces(const char *name, struct pfi_if *buf, int *size, int flags) return (0); } -struct pfi_kif * -pfi_lookup_if(const char *name) -{ - struct pfi_kif *p, key; - - strlcpy(key.pfik_name, name, sizeof(key.pfik_name)); - p = RB_FIND(pfi_ifhead, &pfi_ifs, &key); - return (p); -} - int -pfi_skip_if(const char *filter, struct pfi_kif *p, int f) +pfi_skip_if(const char *filter, struct pfi_kif *p) { int n; - if ((p->pfik_flags & PFI_IFLAG_GROUP) && !(f & PFI_FLAG_GROUP)) - return (1); - if ((p->pfik_flags & PFI_IFLAG_INSTANCE) && !(f & PFI_FLAG_INSTANCE)) - return (1); if (filter == NULL || !*filter) return (0); if (!strcmp(p->pfik_name, filter)) @@ -1076,6 +774,38 @@ pfi_skip_if(const char *filter, struct pfi_kif *p, int f) return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9'); } +int +pfi_set_flags(const char *name, int flags) +{ + struct pfi_kif *p; + int s; + + s = splsoftnet(); + RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { + if (pfi_skip_if(name, p)) + continue; + p->pfik_flags |= flags; + } + splx(s); + return (0); +} + +int +pfi_clear_flags(const char *name, int flags) +{ + struct pfi_kif *p; + int s; + + s = splsoftnet(); + RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { + if (pfi_skip_if(name, p)) + continue; + p->pfik_flags &= ~flags; + } + splx(s); + return (0); +} + /* from pf_print_state.c */ int pfi_unmask(void *addr) @@ -1096,44 +826,53 @@ pfi_unmask(void *addr) return (b); } +#ifdef __FreeBSD__ void -pfi_dohooks(struct pfi_kif *p) +pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp) { - for (; p != NULL; p = p->pfik_parent) - dohooks(p->pfik_ah_head, 0); + PF_LOCK(); + pfi_attach_ifnet(ifp); + PF_UNLOCK(); } -int -pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af) +void +pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp) { - switch (af) { -#ifdef INET - case AF_INET: - switch (dyn->pfid_acnt4) { - case 0: - return (0); - case 1: - return (PF_MATCHA(0, &dyn->pfid_addr4, - &dyn->pfid_mask4, a, AF_INET)); - default: - return (pfr_match_addr(dyn->pfid_kt, a, AF_INET)); - } - break; -#endif /* INET */ -#ifdef INET6 - case AF_INET6: - switch (dyn->pfid_acnt6) { - case 0: - return (0); - case 1: - return (PF_MATCHA(0, &dyn->pfid_addr6, - &dyn->pfid_mask6, a, AF_INET6)); - default: - return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6)); - } - break; -#endif /* INET6 */ - default: - return (0); - } + PF_LOCK(); + pfi_detach_ifnet(ifp); + PF_UNLOCK(); } + +void +pfi_attach_group_event(void *arg __unused, struct ifg_group *ifg) +{ + PF_LOCK(); + pfi_attach_ifgroup(ifg); + PF_UNLOCK(); +} + +void +pfi_change_group_event(void *arg __unused, char *gname) +{ + PF_LOCK(); + pfi_group_change(gname); + PF_UNLOCK(); +} + +void +pfi_detach_group_event(void *arg __unused, struct ifg_group *ifg) +{ + PF_LOCK(); + pfi_detach_ifgroup(ifg); + PF_UNLOCK(); +} + +void +pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp) +{ + PF_LOCK(); + if (ifp && ifp->if_pf_kif) + pfi_kifaddr_update(ifp->if_pf_kif); + PF_UNLOCK(); +} +#endif /* __FreeBSD__ */ |