1 /* Copyright (C) 2011-2013 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_hash.h>
26 #define IPSET_TYPE_REV_MIN 0
27 /* 1 nomatch flag support added */
28 /* 2 /0 support added */
29 /* 3 Counters support added */
30 #define IPSET_TYPE_REV_MAX 4 /* Comments support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:net,iface");
37 /* Interface name rbtree */
44 #define iface_data(n) (rb_entry(n, struct iface_node, node)->iface)
47 rbtree_destroy(struct rb_root *root)
49 struct rb_node *p, *n = root->rb_node;
50 struct iface_node *node;
52 /* Non-recursive destroy, like in ext3 */
63 node = rb_entry(n, struct iface_node, node);
66 else if (p->rb_left == n)
68 else if (p->rb_right == n)
77 iface_test(struct rb_root *root, const char **iface)
79 struct rb_node *n = root->rb_node;
82 const char *d = iface_data(n);
83 int res = strcmp(*iface, d);
98 iface_add(struct rb_root *root, const char **iface)
100 struct rb_node **n = &(root->rb_node), *p = NULL;
101 struct iface_node *d;
104 char *ifname = iface_data(*n);
105 int res = strcmp(*iface, ifname);
109 n = &((*n)->rb_left);
111 n = &((*n)->rb_right);
118 d = kzalloc(sizeof(*d), GFP_ATOMIC);
121 strcpy(d->iface, *iface);
123 rb_link_node(&d->node, p, n);
124 rb_insert_color(&d->node, root);
130 /* Type specific function prefix */
131 #define HTYPE hash_netiface
132 #define IP_SET_HASH_WITH_NETS
133 #define IP_SET_HASH_WITH_RBTREE
134 #define IP_SET_HASH_WITH_MULTI
136 #define STREQ(a, b) (strcmp(a, b) == 0)
140 struct hash_netiface4_elem_hashed {
148 /* Member elements */
149 struct hash_netiface4_elem {
158 /* Common functions */
161 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
162 const struct hash_netiface4_elem *ip2,
165 return ip1->ip == ip2->ip &&
166 ip1->cidr == ip2->cidr &&
168 ip1->physdev == ip2->physdev &&
169 ip1->iface == ip2->iface;
173 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
175 return elem->nomatch ? -ENOTEMPTY : 1;
179 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
181 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
185 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
187 swap(*flags, elem->nomatch);
191 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
193 elem->ip &= ip_set_netmask(cidr);
198 hash_netiface4_data_list(struct sk_buff *skb,
199 const struct hash_netiface4_elem *data)
201 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
204 flags |= IPSET_FLAG_NOMATCH;
205 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
206 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
207 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
209 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
210 goto nla_put_failure;
218 hash_netiface4_data_next(struct hash_netiface4_elem *next,
219 const struct hash_netiface4_elem *d)
224 #define MTYPE hash_netiface4
227 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
228 #include "ip_set_hash_gen.h"
231 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
232 const struct xt_action_param *par,
233 enum ipset_adt adt, struct ip_set_adt_opt *opt)
235 struct hash_netiface *h = set->data;
236 ipset_adtfn adtfn = set->variant->adt[adt];
237 struct hash_netiface4_elem e = {
238 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
241 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
246 if (adt == IPSET_TEST)
249 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
250 e.ip &= ip_set_netmask(e.cidr);
252 #define IFACE(dir) (par->dir ? par->dir->name : NULL)
253 #define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
254 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
256 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
257 #ifdef CONFIG_BRIDGE_NETFILTER
258 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
262 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
268 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
272 ret = iface_test(&h->rbtree, &e.iface);
273 if (adt == IPSET_ADD) {
275 ret = iface_add(&h->rbtree, &e.iface);
282 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
286 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
287 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
289 struct hash_netiface *h = set->data;
290 ipset_adtfn adtfn = set->variant->adt[adt];
291 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
292 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
293 u32 ip = 0, ip_to = 0, last;
294 char iface[IFNAMSIZ];
297 if (unlikely(!tb[IPSET_ATTR_IP] ||
298 !tb[IPSET_ATTR_IFACE] ||
299 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
300 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
301 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
302 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
303 return -IPSET_ERR_PROTOCOL;
305 if (tb[IPSET_ATTR_LINENO])
306 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
308 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
309 ip_set_get_extensions(set, tb, &ext);
313 if (tb[IPSET_ATTR_CIDR]) {
314 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
315 if (e.cidr > HOST_MASK)
316 return -IPSET_ERR_INVALID_CIDR;
319 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
321 ret = iface_test(&h->rbtree, &e.iface);
322 if (adt == IPSET_ADD) {
324 ret = iface_add(&h->rbtree, &e.iface);
331 if (tb[IPSET_ATTR_CADT_FLAGS]) {
332 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
333 if (cadt_flags & IPSET_FLAG_PHYSDEV)
335 if (cadt_flags & IPSET_FLAG_NOMATCH)
336 flags |= (IPSET_FLAG_NOMATCH << 16);
338 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
339 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
340 ret = adtfn(set, &e, &ext, &ext, flags);
341 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
342 ip_set_eexist(ret, flags) ? 0 : ret;
345 if (tb[IPSET_ATTR_IP_TO]) {
346 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
351 if (ip + UINT_MAX == ip_to)
352 return -IPSET_ERR_HASH_RANGE;
354 ip_set_mask_from_to(ip, ip_to, e.cidr);
357 ip = ntohl(h->next.ip);
358 while (!after(ip, ip_to)) {
360 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
361 ret = adtfn(set, &e, &ext, &ext, flags);
363 if (ret && !ip_set_eexist(ret, flags))
374 struct hash_netiface6_elem_hashed {
375 union nf_inet_addr ip;
382 struct hash_netiface6_elem {
383 union nf_inet_addr ip;
391 /* Common functions */
394 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
395 const struct hash_netiface6_elem *ip2,
398 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
399 ip1->cidr == ip2->cidr &&
401 ip1->physdev == ip2->physdev &&
402 ip1->iface == ip2->iface;
406 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
408 return elem->nomatch ? -ENOTEMPTY : 1;
412 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
414 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
418 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
420 swap(*flags, elem->nomatch);
424 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
426 ip6_netmask(&elem->ip, cidr);
431 hash_netiface6_data_list(struct sk_buff *skb,
432 const struct hash_netiface6_elem *data)
434 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
437 flags |= IPSET_FLAG_NOMATCH;
438 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
439 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
440 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
442 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
443 goto nla_put_failure;
451 hash_netiface6_data_next(struct hash_netiface4_elem *next,
452 const struct hash_netiface6_elem *d)
461 #define MTYPE hash_netiface6
463 #define HOST_MASK 128
464 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
465 #define IP_SET_EMIT_CREATE
466 #include "ip_set_hash_gen.h"
469 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
470 const struct xt_action_param *par,
471 enum ipset_adt adt, struct ip_set_adt_opt *opt)
473 struct hash_netiface *h = set->data;
474 ipset_adtfn adtfn = set->variant->adt[adt];
475 struct hash_netiface6_elem e = {
476 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
479 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
484 if (adt == IPSET_TEST)
487 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
488 ip6_netmask(&e.ip, e.cidr);
490 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
491 #ifdef CONFIG_BRIDGE_NETFILTER
492 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
496 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
502 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
506 ret = iface_test(&h->rbtree, &e.iface);
507 if (adt == IPSET_ADD) {
509 ret = iface_add(&h->rbtree, &e.iface);
516 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
520 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
521 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
523 struct hash_netiface *h = set->data;
524 ipset_adtfn adtfn = set->variant->adt[adt];
525 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
526 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
527 char iface[IFNAMSIZ];
530 if (unlikely(!tb[IPSET_ATTR_IP] ||
531 !tb[IPSET_ATTR_IFACE] ||
532 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
533 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
534 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
535 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
536 return -IPSET_ERR_PROTOCOL;
537 if (unlikely(tb[IPSET_ATTR_IP_TO]))
538 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
540 if (tb[IPSET_ATTR_LINENO])
541 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
543 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
544 ip_set_get_extensions(set, tb, &ext);
548 if (tb[IPSET_ATTR_CIDR])
549 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
550 if (e.cidr > HOST_MASK)
551 return -IPSET_ERR_INVALID_CIDR;
552 ip6_netmask(&e.ip, e.cidr);
554 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
556 ret = iface_test(&h->rbtree, &e.iface);
557 if (adt == IPSET_ADD) {
559 ret = iface_add(&h->rbtree, &e.iface);
566 if (tb[IPSET_ATTR_CADT_FLAGS]) {
567 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
568 if (cadt_flags & IPSET_FLAG_PHYSDEV)
570 if (cadt_flags & IPSET_FLAG_NOMATCH)
571 flags |= (IPSET_FLAG_NOMATCH << 16);
574 ret = adtfn(set, &e, &ext, &ext, flags);
576 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
577 ip_set_eexist(ret, flags) ? 0 : ret;
580 static struct ip_set_type hash_netiface_type __read_mostly = {
581 .name = "hash:net,iface",
582 .protocol = IPSET_PROTOCOL,
583 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
585 .dimension = IPSET_DIM_TWO,
586 .family = NFPROTO_UNSPEC,
587 .revision_min = IPSET_TYPE_REV_MIN,
588 .revision_max = IPSET_TYPE_REV_MAX,
589 .create = hash_netiface_create,
591 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
592 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
593 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
594 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
595 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
596 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
597 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
600 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
601 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
602 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
603 .len = IFNAMSIZ - 1 },
604 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
605 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
606 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
607 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
608 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
609 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
610 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
616 hash_netiface_init(void)
618 return ip_set_type_register(&hash_netiface_type);
622 hash_netiface_fini(void)
624 ip_set_type_unregister(&hash_netiface_type);
627 module_init(hash_netiface_init);
628 module_exit(hash_netiface_fini);