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 rbtree_destroy(struct rb_root *root)
44 struct rb_node *p, *n = root->rb_node;
45 struct iface_node *node;
47 /* Non-recursive destroy, like in ext3 */
58 node = rb_entry(n, struct iface_node, node);
61 else if (p->rb_left == n)
63 else if (p->rb_right == n)
72 iface_test(struct rb_root *root, const char **iface)
74 struct rb_node *n = root->rb_node;
77 const char *d = iface_data(n);
78 int res = strcmp(*iface, d);
93 iface_add(struct rb_root *root, const char **iface)
95 struct rb_node **n = &(root->rb_node), *p = NULL;
99 char *ifname = iface_data(*n);
100 int res = strcmp(*iface, ifname);
104 n = &((*n)->rb_left);
106 n = &((*n)->rb_right);
113 d = kzalloc(sizeof(*d), GFP_ATOMIC);
116 strcpy(d->iface, *iface);
118 rb_link_node(&d->node, p, n);
119 rb_insert_color(&d->node, root);
125 /* Type specific function prefix */
126 #define TYPE hash_netiface
129 hash_netiface_same_set(const struct ip_set *a, const struct ip_set *b);
131 #define hash_netiface4_same_set hash_netiface_same_set
132 #define hash_netiface6_same_set hash_netiface_same_set
134 #define STREQ(a, b) (strcmp(a, b) == 0)
136 /* The type variant functions: IPv4 */
138 struct hash_netiface4_elem_hashed {
146 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
148 /* Member elements without timeout */
149 struct hash_netiface4_elem {
158 /* Member elements with timeout support */
159 struct hash_netiface4_telem {
166 unsigned long timeout;
170 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
171 const struct hash_netiface4_elem *ip2,
174 return ip1->ip == ip2->ip &&
175 ip1->cidr == ip2->cidr &&
177 ip1->physdev == ip2->physdev &&
178 ip1->iface == ip2->iface;
182 hash_netiface4_data_isnull(const struct hash_netiface4_elem *elem)
184 return elem->cidr == 0;
188 hash_netiface4_data_copy(struct hash_netiface4_elem *dst,
189 const struct hash_netiface4_elem *src)
192 dst->cidr = src->cidr;
193 dst->physdev = src->physdev;
194 dst->iface = src->iface;
195 dst->nomatch = src->nomatch;
199 hash_netiface4_data_flags(struct hash_netiface4_elem *dst, u32 flags)
201 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
205 hash_netiface4_data_match(const struct hash_netiface4_elem *elem)
207 return !elem->nomatch;
211 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
213 elem->ip &= ip_set_netmask(cidr);
218 hash_netiface4_data_zero_out(struct hash_netiface4_elem *elem)
224 hash_netiface4_data_list(struct sk_buff *skb,
225 const struct hash_netiface4_elem *data)
227 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
230 flags |= IPSET_FLAG_NOMATCH;
231 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
232 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
233 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
235 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
236 goto nla_put_failure;
244 hash_netiface4_data_tlist(struct sk_buff *skb,
245 const struct hash_netiface4_elem *data)
247 const struct hash_netiface4_telem *tdata =
248 (const struct hash_netiface4_telem *)data;
249 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
252 flags |= IPSET_FLAG_NOMATCH;
253 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
254 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
255 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
257 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) ||
258 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
259 htonl(ip_set_timeout_get(tdata->timeout))))
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
274 #include <linux/netfilter/ipset/ip_set_ahash.h>
277 hash_netiface4_data_next(struct ip_set_hash *h,
278 const struct hash_netiface4_elem *d)
280 h->next.ip = ntohl(d->ip);
284 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
285 const struct xt_action_param *par,
286 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
288 struct ip_set_hash *h = set->data;
289 ipset_adtfn adtfn = set->variant->adt[adt];
290 struct hash_netiface4_elem data = {
291 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
297 if (adt == IPSET_TEST)
298 data.cidr = HOST_MASK;
300 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
301 data.ip &= ip_set_netmask(data.cidr);
303 #define IFACE(dir) (par->dir ? par->dir->name : NULL)
304 #define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
305 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
307 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
308 #ifdef CONFIG_BRIDGE_NETFILTER
309 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
313 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
319 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
323 ret = iface_test(&h->rbtree, &data.iface);
324 if (adt == IPSET_ADD) {
326 ret = iface_add(&h->rbtree, &data.iface);
333 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
337 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
338 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
340 struct ip_set_hash *h = set->data;
341 ipset_adtfn adtfn = set->variant->adt[adt];
342 struct hash_netiface4_elem data = { .cidr = HOST_MASK };
343 u32 ip = 0, ip_to, last;
344 u32 timeout = h->timeout;
345 char iface[IFNAMSIZ];
348 if (unlikely(!tb[IPSET_ATTR_IP] ||
349 !tb[IPSET_ATTR_IFACE] ||
350 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
351 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
352 return -IPSET_ERR_PROTOCOL;
354 if (tb[IPSET_ATTR_LINENO])
355 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
357 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
361 if (tb[IPSET_ATTR_CIDR]) {
362 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
363 if (!data.cidr || data.cidr > HOST_MASK)
364 return -IPSET_ERR_INVALID_CIDR;
367 if (tb[IPSET_ATTR_TIMEOUT]) {
368 if (!with_timeout(h->timeout))
369 return -IPSET_ERR_TIMEOUT;
370 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
373 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
375 ret = iface_test(&h->rbtree, &data.iface);
376 if (adt == IPSET_ADD) {
378 ret = iface_add(&h->rbtree, &data.iface);
385 if (tb[IPSET_ATTR_CADT_FLAGS]) {
386 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
387 if (cadt_flags & IPSET_FLAG_PHYSDEV)
389 if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH))
390 flags |= (cadt_flags << 16);
393 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
394 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
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);
405 if (ip + UINT_MAX == ip_to)
406 return -IPSET_ERR_HASH_RANGE;
408 ip_set_mask_from_to(ip, ip_to, data.cidr);
413 while (!after(ip, ip_to)) {
415 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
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)
430 const struct ip_set_hash *x = a->data;
431 const struct ip_set_hash *y = b->data;
433 /* Resizing changes htable_bits, so we ignore it */
434 return x->maxelem == y->maxelem &&
435 x->timeout == y->timeout;
438 /* The type variant functions: IPv6 */
440 struct hash_netiface6_elem_hashed {
441 union nf_inet_addr ip;
448 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
450 struct hash_netiface6_elem {
451 union nf_inet_addr ip;
459 struct hash_netiface6_telem {
460 union nf_inet_addr ip;
466 unsigned long timeout;
470 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
471 const struct hash_netiface6_elem *ip2,
474 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
475 ip1->cidr == ip2->cidr &&
477 ip1->physdev == ip2->physdev &&
478 ip1->iface == ip2->iface;
482 hash_netiface6_data_isnull(const struct hash_netiface6_elem *elem)
484 return elem->cidr == 0;
488 hash_netiface6_data_copy(struct hash_netiface6_elem *dst,
489 const struct hash_netiface6_elem *src)
491 memcpy(dst, src, sizeof(*dst));
495 hash_netiface6_data_flags(struct hash_netiface6_elem *dst, u32 flags)
497 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
501 hash_netiface6_data_match(const struct hash_netiface6_elem *elem)
503 return !elem->nomatch;
507 hash_netiface6_data_zero_out(struct hash_netiface6_elem *elem)
513 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
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];
522 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
524 ip6_netmask(&elem->ip, cidr);
529 hash_netiface6_data_list(struct sk_buff *skb,
530 const struct hash_netiface6_elem *data)
532 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
535 flags |= IPSET_FLAG_NOMATCH;
536 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
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, htonl(flags))))
541 goto nla_put_failure;
549 hash_netiface6_data_tlist(struct sk_buff *skb,
550 const struct hash_netiface6_elem *data)
552 const struct hash_netiface6_telem *e =
553 (const struct hash_netiface6_telem *)data;
554 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
557 flags |= IPSET_FLAG_NOMATCH;
558 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
559 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
560 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
562 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))) ||
563 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
564 htonl(ip_set_timeout_get(e->timeout))))
565 goto nla_put_failure;
576 #define HOST_MASK 128
577 #include <linux/netfilter/ipset/ip_set_ahash.h>
580 hash_netiface6_data_next(struct ip_set_hash *h,
581 const struct hash_netiface6_elem *d)
586 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
587 const struct xt_action_param *par,
588 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
590 struct ip_set_hash *h = set->data;
591 ipset_adtfn adtfn = set->variant->adt[adt];
592 struct hash_netiface6_elem data = {
593 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
599 if (adt == IPSET_TEST)
600 data.cidr = HOST_MASK;
602 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
603 ip6_netmask(&data.ip, data.cidr);
605 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
606 #ifdef CONFIG_BRIDGE_NETFILTER
607 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
611 data.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
617 data.iface = SRCDIR ? IFACE(in) : IFACE(out);
621 ret = iface_test(&h->rbtree, &data.iface);
622 if (adt == IPSET_ADD) {
624 ret = iface_add(&h->rbtree, &data.iface);
631 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
635 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
636 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
638 struct ip_set_hash *h = set->data;
639 ipset_adtfn adtfn = set->variant->adt[adt];
640 struct hash_netiface6_elem data = { .cidr = HOST_MASK };
641 u32 timeout = h->timeout;
642 char iface[IFNAMSIZ];
645 if (unlikely(!tb[IPSET_ATTR_IP] ||
646 !tb[IPSET_ATTR_IFACE] ||
647 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
648 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
649 return -IPSET_ERR_PROTOCOL;
650 if (unlikely(tb[IPSET_ATTR_IP_TO]))
651 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
653 if (tb[IPSET_ATTR_LINENO])
654 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
656 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
660 if (tb[IPSET_ATTR_CIDR])
661 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
662 if (!data.cidr || data.cidr > HOST_MASK)
663 return -IPSET_ERR_INVALID_CIDR;
664 ip6_netmask(&data.ip, data.cidr);
666 if (tb[IPSET_ATTR_TIMEOUT]) {
667 if (!with_timeout(h->timeout))
668 return -IPSET_ERR_TIMEOUT;
669 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
672 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
674 ret = iface_test(&h->rbtree, &data.iface);
675 if (adt == IPSET_ADD) {
677 ret = iface_add(&h->rbtree, &data.iface);
684 if (tb[IPSET_ATTR_CADT_FLAGS]) {
685 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
686 if (cadt_flags & IPSET_FLAG_PHYSDEV)
688 if (adt == IPSET_ADD && (cadt_flags & IPSET_FLAG_NOMATCH))
689 flags |= (cadt_flags << 16);
692 ret = adtfn(set, &data, timeout, flags);
694 return ip_set_eexist(ret, flags) ? 0 : ret;
697 /* Create hash:ip type of sets */
700 hash_netiface_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
702 struct ip_set_hash *h;
703 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
707 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
708 return -IPSET_ERR_INVALID_FAMILY;
710 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
711 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
712 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
713 return -IPSET_ERR_PROTOCOL;
715 if (tb[IPSET_ATTR_HASHSIZE]) {
716 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
717 if (hashsize < IPSET_MIMINAL_HASHSIZE)
718 hashsize = IPSET_MIMINAL_HASHSIZE;
721 if (tb[IPSET_ATTR_MAXELEM])
722 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
724 h = kzalloc(sizeof(*h)
725 + sizeof(struct ip_set_hash_nets)
726 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
730 h->maxelem = maxelem;
731 get_random_bytes(&h->initval, sizeof(h->initval));
732 h->timeout = IPSET_NO_TIMEOUT;
733 h->ahash_max = AHASH_MAX_SIZE;
735 hbits = htable_bits(hashsize);
736 hsize = htable_size(hbits);
741 h->table = ip_set_alloc(hsize);
746 h->table->htable_bits = hbits;
751 if (tb[IPSET_ATTR_TIMEOUT]) {
752 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
754 set->variant = set->family == NFPROTO_IPV4
755 ? &hash_netiface4_tvariant : &hash_netiface6_tvariant;
757 if (set->family == NFPROTO_IPV4)
758 hash_netiface4_gc_init(set);
760 hash_netiface6_gc_init(set);
762 set->variant = set->family == NFPROTO_IPV4
763 ? &hash_netiface4_variant : &hash_netiface6_variant;
766 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
767 set->name, jhash_size(h->table->htable_bits),
768 h->table->htable_bits, h->maxelem, set->data, h->table);
773 static struct ip_set_type hash_netiface_type __read_mostly = {
774 .name = "hash:net,iface",
775 .protocol = IPSET_PROTOCOL,
776 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE,
777 .dimension = IPSET_DIM_TWO,
778 .family = NFPROTO_UNSPEC,
780 .revision_max = 1, /* nomatch flag support added */
781 .create = hash_netiface_create,
783 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
784 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
785 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
786 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
787 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
788 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
791 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
792 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
793 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
794 .len = IPSET_MAXNAMELEN - 1 },
795 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
796 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
797 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
798 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
804 hash_netiface_init(void)
806 return ip_set_type_register(&hash_netiface_type);
810 hash_netiface_fini(void)
812 ip_set_type_unregister(&hash_netiface_type);
815 module_init(hash_netiface_init);
816 module_exit(hash_netiface_fini);