11 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <linux/rbtree.h>
21 #include <linux/netfilter.h>
23 #include <linux/netfilter/ipset/ip_set.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 #define REVISION_MIN 0
29 #define REVISION_MAX 2
43 #define iface_data(n) (rb_entry(n, struct iface_node, node)->iface)
46 rbtree_destroy(
struct rb_root *root)
97 iface_add(
struct rb_root *root,
const char **iface)
104 int res =
strcmp(*iface, ifname);
108 n = &((*n)->rb_left);
110 n = &((*n)->rb_right);
122 rb_link_node(&d->
node,
p, n);
130 #define TYPE hash_netiface
133 hash_netiface_same_set(
const struct ip_set *
a,
const struct ip_set *
b);
135 #define hash_netiface4_same_set hash_netiface_same_set
136 #define hash_netiface6_same_set hash_netiface_same_set
138 #define STREQ(a, b) (strcmp(a, b) == 0)
150 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
178 return ip1->
ip == ip2->
ip &&
188 return elem->
elem == 0;
195 memcpy(dst, src,
sizeof(*dst));
213 elem->
ip &= ip_set_netmask(cidr);
236 goto nla_put_failure;
244 hash_netiface4_data_tlist(
struct sk_buff *skb,
260 goto nla_put_failure;
268 #define IP_SET_HASH_WITH_NETS
269 #define IP_SET_HASH_WITH_RBTREE
270 #define IP_SET_HASH_WITH_MULTI
284 hash_netiface4_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
302 data.
ip &= ip_set_netmask(data.
cidr);
304 #define IFACE(dir) (par->dir ? par->dir->name : NULL)
305 #define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
306 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
309 #ifdef CONFIG_BRIDGE_NETFILTER
310 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
324 ret = iface_test(&h->rbtree, &data.
iface);
327 ret = iface_add(&h->rbtree, &data.
iface);
334 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
356 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
363 data.
cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
371 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
376 ret = iface_test(&h->rbtree, &data.
iface);
379 ret = iface_add(&h->rbtree, &data.
iface);
387 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
391 flags |= (cadt_flags << 16);
395 ret = adtfn(
set, &data, timeout, flags);
396 return ip_set_eexist(ret, flags) ? 0 :
ret;
399 if (tb[IPSET_ATTR_IP_TO]) {
400 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
413 while (!
after(ip, ip_to)) {
416 ret = adtfn(
set, &data, timeout, flags);
418 if (ret && !ip_set_eexist(ret, flags))
428 hash_netiface_same_set(
const struct ip_set *
a,
const struct ip_set *
b)
448 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
474 return ipv6_addr_cmp(&ip1->
ip.
in6, &ip2->
ip.
in6) == 0 &&
484 return elem->
elem == 0;
491 memcpy(dst, src,
sizeof(*dst));
515 ip->
ip6[0] &= ip_set_netmask6(prefix)[0];
516 ip->
ip6[1] &= ip_set_netmask6(prefix)[1];
517 ip->
ip6[2] &= ip_set_netmask6(prefix)[2];
518 ip->
ip6[3] &= ip_set_netmask6(prefix)[3];
524 ip6_netmask(&elem->
ip, cidr);
529 hash_netiface6_data_list(
struct sk_buff *skb,
532 u32 flags = data->
physdev ? IPSET_FLAG_PHYSDEV : 0;
537 nla_put_u8(skb, IPSET_ATTR_CIDR, data->
cidr) ||
540 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS,
htonl(flags))))
541 goto nla_put_failure;
549 hash_netiface6_data_tlist(
struct sk_buff *skb,
554 u32 flags = data->
physdev ? IPSET_FLAG_PHYSDEV : 0;
559 nla_put_u8(skb, IPSET_ATTR_CIDR, data->
cidr) ||
562 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS,
htonl(flags))) ||
563 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
565 goto nla_put_failure;
576 #define HOST_MASK 128
586 hash_netiface6_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
604 ip6_netmask(&data.
ip, data.
cidr);
606 if (opt->
cmdflags & IPSET_FLAG_PHYSDEV) {
607 #ifdef CONFIG_BRIDGE_NETFILTER
608 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
622 ret = iface_test(&h->rbtree, &data.
iface);
625 ret = iface_add(&h->rbtree, &data.
iface);
632 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
636 hash_netiface6_uadt(
struct ip_set *
set,
struct nlattr *tb[],
648 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
649 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
654 if (tb[IPSET_ATTR_LINENO])
655 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
661 if (tb[IPSET_ATTR_CIDR])
662 data.
cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
665 ip6_netmask(&data.
ip, data.
cidr);
667 if (tb[IPSET_ATTR_TIMEOUT]) {
670 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
675 ret = iface_test(&h->rbtree, &data.
iface);
678 ret = iface_add(&h->rbtree, &data.
iface);
685 if (tb[IPSET_ATTR_CADT_FLAGS]) {
686 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
687 if (cadt_flags & IPSET_FLAG_PHYSDEV)
690 flags |= (cadt_flags << 16);
693 ret = adtfn(
set, &data, timeout, flags);
695 return ip_set_eexist(ret, flags) ? 0 :
ret;
701 hash_netiface_create(
struct ip_set *
set,
struct nlattr *tb[],
u32 flags)
713 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
717 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
723 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
725 h = kzalloc(
sizeof(*h)
736 hbits = htable_bits(hashsize);
737 hsize = htable_size(hbits);
747 h->
table->htable_bits = hbits;
752 if (tb[IPSET_ATTR_TIMEOUT]) {
753 h->
timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
756 ? &hash_netiface4_tvariant : &hash_netiface6_tvariant;
759 hash_netiface4_gc_init(
set);
761 hash_netiface6_gc_init(
set);
764 ? &hash_netiface4_variant : &hash_netiface6_variant;
767 pr_debug(
"create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
775 .name =
"hash:net,iface",
783 .create = hash_netiface_create,
806 hash_netiface_init(
void)
812 hash_netiface_fini(
void)