1 /* Copyright (C) 2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the hash:net,iface type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <linux/rbtree.h>
19 #include <net/netlink.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/ipset/pfxlen.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_timeout.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
29 MODULE_DESCRIPTION("hash:net,iface type of IP sets");
30 MODULE_ALIAS("ip_set_hash:net,iface");
32 /* Interface name rbtree */
39 #define iface_data(n) (rb_entry(n, struct iface_node, node)->iface)
42 ifname_compare(const char *_a, const char *_b)
44 const long *a = (const long *)_a;
45 const long *b = (const long *)_b;
47 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
50 if (IFNAMSIZ > sizeof(long)) {
54 if (IFNAMSIZ > 2 * sizeof(long)) {
58 if (IFNAMSIZ > 3 * sizeof(long)) {
66 rbtree_destroy(struct rb_root *root)
68 struct rb_node *p, *n = root->rb_node;
69 struct iface_node *node;
71 /* Non-recursive destroy, like in ext3 */
82 node = rb_entry(n, struct iface_node, node);
85 else if (p->rb_left == n)
87 else if (p->rb_right == n)
96 iface_test(struct rb_root *root, const char **iface)
98 struct rb_node *n = root->rb_node;
101 const char *d = iface_data(n);
102 long res = ifname_compare(*iface, d);
117 iface_add(struct rb_root *root, const char **iface)
119 struct rb_node **n = &(root->rb_node), *p = NULL;
120 struct iface_node *d;
123 char *ifname = iface_data(*n);
124 long res = ifname_compare(*iface, ifname);
128 n = &((*n)->rb_left);
130 n = &((*n)->rb_right);
137 d = kzalloc(sizeof(*d), GFP_ATOMIC);
140 strcpy(d->iface, *iface);
142 rb_link_node(&d->node, p, n);
143 rb_insert_color(&d->node, root);
149 /* Type specific function prefix */
150 #define TYPE hash_netiface
153 hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b);
155 #define hash_netiface4_same_set hash_netiface_same_set
156 #define hash_netiface6_same_set hash_netiface_same_set
158 #define STREQ(a, b) (strcmp(a, b) == 0)
160 /* The type variant functions: IPv4 */
162 struct hash_netiface4_elem_hashed {
169 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
171 /* Member elements without timeout */
172 struct hash_netiface4_elem {
180 /* Member elements with timeout support */
181 struct hash_netiface4_telem {
187 unsigned long timeout;
191 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
192 const struct hash_netiface4_elem *ip2,
195 return ip1->ip == ip2->ip &&
196 ip1->cidr == ip2->cidr &&
198 ip1->physdev == ip2->physdev &&
199 ip1->iface == ip2->iface;
203 hash_netiface4_data_isnull(const struct hash_netiface4_elem *elem)
205 return elem->cidr == 0;
209 hash_netiface4_data_copy(struct hash_netiface4_elem *dst,
210 const struct hash_netiface4_elem *src) {
212 dst->cidr = src->cidr;
213 dst->physdev = src->physdev;
214 dst->iface = src->iface;
218 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
220 elem->ip &= ip_set_netmask(cidr);
225 hash_netiface4_data_zero_out(struct hash_netiface4_elem *elem)
231 hash_netiface4_data_list(struct sk_buff *skb,
232 const struct hash_netiface4_elem *data)
234 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
236 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
237 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
238 NLA_PUT_STRING(skb, IPSET_ATTR_IFACE, data->iface);
240 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, flags);
248 hash_netiface4_data_tlist(struct sk_buff *skb,
249 const struct hash_netiface4_elem *data)
251 const struct hash_netiface4_telem *tdata =
252 (const struct hash_netiface4_telem *)data;
253 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
255 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
256 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
257 NLA_PUT_STRING(skb, IPSET_ATTR_IFACE, data->iface);
259 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, flags);
260 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
261 htonl(ip_set_timeout_get(tdata->timeout)));
269 #define IP_SET_HASH_WITH_NETS
270 #define IP_SET_HASH_WITH_RBTREE
271 #define IP_SET_HASH_WITH_MULTI
275 #include <linux/netfilter/ipset/ip_set_ahash.h>
278 hash_netiface4_data_next(struct ip_set_hash *h,
279 const struct hash_netiface4_elem *d)
281 h->next.ip = ntohl(d->ip);
285 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
286 const struct xt_action_param *par,
287 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
289 struct ip_set_hash *h = set->data;
290 ipset_adtfn adtfn = set->variant->adt[adt];
291 struct hash_netiface4_elem data = {
292 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
298 if (adt == IPSET_TEST)
299 data.cidr = HOST_MASK;
301 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
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)
308 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
309 #ifdef CONFIG_BRIDGE_NETFILTER
310 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
314 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
320 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
324 ret = iface_test(&h->rbtree, &data.iface);
325 if (adt == IPSET_ADD) {
327 ret = iface_add(&h->rbtree, &data.iface);
334 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
338 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
339 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
341 struct ip_set_hash *h = set->data;
342 ipset_adtfn adtfn = set->variant->adt[adt];
343 struct hash_netiface4_elem data = { .cidr = HOST_MASK };
344 u32 ip = 0, ip_to, last;
345 u32 timeout = h->timeout;
346 char iface[IFNAMSIZ] = {};
349 if (unlikely(!tb[IPSET_ATTR_IP] ||
350 !tb[IPSET_ATTR_IFACE] ||
351 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
352 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
353 return -IPSET_ERR_PROTOCOL;
355 if (tb[IPSET_ATTR_LINENO])
356 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
358 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
362 if (tb[IPSET_ATTR_CIDR]) {
363 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
365 return -IPSET_ERR_INVALID_CIDR;
368 if (tb[IPSET_ATTR_TIMEOUT]) {
369 if (!with_timeout(h->timeout))
370 return -IPSET_ERR_TIMEOUT;
371 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
374 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
376 ret = iface_test(&h->rbtree, &data.iface);
377 if (adt == IPSET_ADD) {
379 ret = iface_add(&h->rbtree, &data.iface);
386 if (tb[IPSET_ATTR_CADT_FLAGS]) {
387 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
388 if (cadt_flags & IPSET_FLAG_PHYSDEV)
392 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
393 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
394 ret = adtfn(set, &data, timeout, flags);
395 return ip_set_eexist(ret, flags) ? 0 : ret;
398 if (tb[IPSET_ATTR_IP_TO]) {
399 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
404 if (ip + UINT_MAX == ip_to)
405 return -IPSET_ERR_HASH_RANGE;
407 ip_set_mask_from_to(ip, ip_to, data.cidr);
412 while (!after(ip, ip_to)) {
414 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
415 ret = adtfn(set, &data, timeout, flags);
417 if (ret && !ip_set_eexist(ret, flags))
427 hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b)
429 const struct ip_set_hash *x = a->data;
430 const struct ip_set_hash *y = b->data;
432 /* Resizing changes htable_bits, so we ignore it */
433 return x->maxelem == y->maxelem &&
434 x->timeout == y->timeout;
437 /* The type variant functions: IPv6 */
439 struct hash_netiface6_elem_hashed {
440 union nf_inet_addr ip;
446 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
448 struct hash_netiface6_elem {
449 union nf_inet_addr ip;
456 struct hash_netiface6_telem {
457 union nf_inet_addr ip;
462 unsigned long timeout;
466 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
467 const struct hash_netiface6_elem *ip2,
470 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
471 ip1->cidr == ip2->cidr &&
473 ip1->physdev == ip2->physdev &&
474 ip1->iface == ip2->iface;
478 hash_netiface6_data_isnull(const struct hash_netiface6_elem *elem)
480 return elem->cidr == 0;
484 hash_netiface6_data_copy(struct hash_netiface6_elem *dst,
485 const struct hash_netiface6_elem *src)
487 memcpy(dst, src, sizeof(*dst));
491 hash_netiface6_data_zero_out(struct hash_netiface6_elem *elem)
496 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
498 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
499 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
500 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
501 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
505 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
507 ip6_netmask(&elem->ip, cidr);
512 hash_netiface6_data_list(struct sk_buff *skb,
513 const struct hash_netiface6_elem *data)
515 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
517 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
518 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
519 NLA_PUT_STRING(skb, IPSET_ATTR_IFACE, data->iface);
521 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, flags);
529 hash_netiface6_data_tlist(struct sk_buff *skb,
530 const struct hash_netiface6_elem *data)
532 const struct hash_netiface6_telem *e =
533 (const struct hash_netiface6_telem *)data;
534 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
536 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
537 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
538 NLA_PUT_STRING(skb, IPSET_ATTR_IFACE, data->iface);
540 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, flags);
541 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
542 htonl(ip_set_timeout_get(e->timeout)));
553 #define HOST_MASK 128
554 #include <linux/netfilter/ipset/ip_set_ahash.h>
557 hash_netiface6_data_next(struct ip_set_hash *h,
558 const struct hash_netiface6_elem *d)
563 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
564 const struct xt_action_param *par,
565 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
567 struct ip_set_hash *h = set->data;
568 ipset_adtfn adtfn = set->variant->adt[adt];
569 struct hash_netiface6_elem data = {
570 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
576 if (adt == IPSET_TEST)
577 data.cidr = HOST_MASK;
579 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
580 ip6_netmask(&data.ip, data.cidr);
582 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
583 #ifdef CONFIG_BRIDGE_NETFILTER
584 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
588 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
594 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
598 ret = iface_test(&h->rbtree, &data.iface);
599 if (adt == IPSET_ADD) {
601 ret = iface_add(&h->rbtree, &data.iface);
608 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
612 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
613 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
615 struct ip_set_hash *h = set->data;
616 ipset_adtfn adtfn = set->variant->adt[adt];
617 struct hash_netiface6_elem data = { .cidr = HOST_MASK };
618 u32 timeout = h->timeout;
619 char iface[IFNAMSIZ] = {};
622 if (unlikely(!tb[IPSET_ATTR_IP] ||
623 !tb[IPSET_ATTR_IFACE] ||
624 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
625 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
626 return -IPSET_ERR_PROTOCOL;
627 if (unlikely(tb[IPSET_ATTR_IP_TO]))
628 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
630 if (tb[IPSET_ATTR_LINENO])
631 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
633 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
637 if (tb[IPSET_ATTR_CIDR])
638 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
640 return -IPSET_ERR_INVALID_CIDR;
641 ip6_netmask(&data.ip, data.cidr);
643 if (tb[IPSET_ATTR_TIMEOUT]) {
644 if (!with_timeout(h->timeout))
645 return -IPSET_ERR_TIMEOUT;
646 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
649 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
651 ret = iface_test(&h->rbtree, &data.iface);
652 if (adt == IPSET_ADD) {
654 ret = iface_add(&h->rbtree, &data.iface);
661 if (tb[IPSET_ATTR_CADT_FLAGS]) {
662 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
663 if (cadt_flags & IPSET_FLAG_PHYSDEV)
667 ret = adtfn(set, &data, timeout, flags);
669 return ip_set_eexist(ret, flags) ? 0 : ret;
672 /* Create hash:ip type of sets */
675 hash_netiface_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
677 struct ip_set_hash *h;
678 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
681 if (!(set->family == AF_INET || set->family == AF_INET6))
682 return -IPSET_ERR_INVALID_FAMILY;
684 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
685 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
686 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
687 return -IPSET_ERR_PROTOCOL;
689 if (tb[IPSET_ATTR_HASHSIZE]) {
690 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
691 if (hashsize < IPSET_MIMINAL_HASHSIZE)
692 hashsize = IPSET_MIMINAL_HASHSIZE;
695 if (tb[IPSET_ATTR_MAXELEM])
696 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
698 h = kzalloc(sizeof(*h)
699 + sizeof(struct ip_set_hash_nets)
700 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
704 h->maxelem = maxelem;
705 get_random_bytes(&h->initval, sizeof(h->initval));
706 h->timeout = IPSET_NO_TIMEOUT;
707 h->ahash_max = AHASH_MAX_SIZE;
709 hbits = htable_bits(hashsize);
710 h->table = ip_set_alloc(
711 sizeof(struct htable)
712 + jhash_size(hbits) * sizeof(struct hbucket));
717 h->table->htable_bits = hbits;
722 if (tb[IPSET_ATTR_TIMEOUT]) {
723 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
725 set->variant = set->family == AF_INET
726 ? &hash_netiface4_tvariant : &hash_netiface6_tvariant;
728 if (set->family == AF_INET)
729 hash_netiface4_gc_init(set);
731 hash_netiface6_gc_init(set);
733 set->variant = set->family == AF_INET
734 ? &hash_netiface4_variant : &hash_netiface6_variant;
737 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
738 set->name, jhash_size(h->table->htable_bits),
739 h->table->htable_bits, h->maxelem, set->data, h->table);
744 static struct ip_set_type hash_netiface_type __read_mostly = {
745 .name = "hash:net,iface",
746 .protocol = IPSET_PROTOCOL,
747 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE,
748 .dimension = IPSET_DIM_TWO,
751 .create = hash_netiface_create,
753 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
754 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
755 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
756 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
757 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
758 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
761 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
762 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
763 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
764 .len = IPSET_MAXNAMELEN - 1 },
765 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
766 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
767 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
768 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
774 hash_netiface_init(void)
776 return ip_set_type_register(&hash_netiface_type);
780 hash_netiface_fini(void)
782 ip_set_type_unregister(&hash_netiface_type);
785 module_init(hash_netiface_init);
786 module_exit(hash_netiface_fini);