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 /* 4 Comments support added */
31 /* 5 Forceadd support added */
32 #define IPSET_TYPE_REV_MAX 6 /* skbinfo support added */
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
36 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
37 MODULE_ALIAS("ip_set_hash:net,iface");
39 /* Interface name rbtree */
46 #define iface_data(n) (rb_entry(n, struct iface_node, node)->iface)
49 rbtree_destroy(struct rb_root *root)
51 struct iface_node *node, *next;
53 rbtree_postorder_for_each_entry_safe(node, next, root, node)
60 iface_test(struct rb_root *root, const char **iface)
62 struct rb_node *n = root->rb_node;
65 const char *d = iface_data(n);
66 int res = strcmp(*iface, d);
81 iface_add(struct rb_root *root, const char **iface)
83 struct rb_node **n = &(root->rb_node), *p = NULL;
87 char *ifname = iface_data(*n);
88 int res = strcmp(*iface, ifname);
94 n = &((*n)->rb_right);
101 d = kzalloc(sizeof(*d), GFP_ATOMIC);
104 strcpy(d->iface, *iface);
106 rb_link_node(&d->node, p, n);
107 rb_insert_color(&d->node, root);
113 /* Type specific function prefix */
114 #define HTYPE hash_netiface
115 #define IP_SET_HASH_WITH_NETS
116 #define IP_SET_HASH_WITH_RBTREE
117 #define IP_SET_HASH_WITH_MULTI
119 #define STREQ(a, b) (strcmp(a, b) == 0)
123 struct hash_netiface4_elem_hashed {
131 /* Member elements */
132 struct hash_netiface4_elem {
141 /* Common functions */
144 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
145 const struct hash_netiface4_elem *ip2,
148 return ip1->ip == ip2->ip &&
149 ip1->cidr == ip2->cidr &&
151 ip1->physdev == ip2->physdev &&
152 ip1->iface == ip2->iface;
156 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
158 return elem->nomatch ? -ENOTEMPTY : 1;
162 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
164 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
168 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
170 swap(*flags, elem->nomatch);
174 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
176 elem->ip &= ip_set_netmask(cidr);
181 hash_netiface4_data_list(struct sk_buff *skb,
182 const struct hash_netiface4_elem *data)
184 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
187 flags |= IPSET_FLAG_NOMATCH;
188 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
189 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
190 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
192 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
193 goto nla_put_failure;
201 hash_netiface4_data_next(struct hash_netiface4_elem *next,
202 const struct hash_netiface4_elem *d)
207 #define MTYPE hash_netiface4
210 #define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
211 #include "ip_set_hash_gen.h"
214 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
215 const struct xt_action_param *par,
216 enum ipset_adt adt, struct ip_set_adt_opt *opt)
218 struct hash_netiface *h = set->data;
219 ipset_adtfn adtfn = set->variant->adt[adt];
220 struct hash_netiface4_elem e = {
221 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
224 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
229 if (adt == IPSET_TEST)
232 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
233 e.ip &= ip_set_netmask(e.cidr);
235 #define IFACE(dir) (par->dir ? par->dir->name : NULL)
236 #define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
237 #define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
239 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
240 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
241 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
245 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
251 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
255 ret = iface_test(&h->rbtree, &e.iface);
256 if (adt == IPSET_ADD) {
258 ret = iface_add(&h->rbtree, &e.iface);
265 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
269 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
270 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
272 struct hash_netiface *h = set->data;
273 ipset_adtfn adtfn = set->variant->adt[adt];
274 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
275 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
276 u32 ip = 0, ip_to = 0, last;
277 char iface[IFNAMSIZ];
280 if (unlikely(!tb[IPSET_ATTR_IP] ||
281 !tb[IPSET_ATTR_IFACE] ||
282 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
283 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
284 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
285 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
286 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
287 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
288 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
289 return -IPSET_ERR_PROTOCOL;
291 if (tb[IPSET_ATTR_LINENO])
292 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
294 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
295 ip_set_get_extensions(set, tb, &ext);
299 if (tb[IPSET_ATTR_CIDR]) {
300 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
301 if (e.cidr > HOST_MASK)
302 return -IPSET_ERR_INVALID_CIDR;
305 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
307 ret = iface_test(&h->rbtree, &e.iface);
308 if (adt == IPSET_ADD) {
310 ret = iface_add(&h->rbtree, &e.iface);
317 if (tb[IPSET_ATTR_CADT_FLAGS]) {
318 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
319 if (cadt_flags & IPSET_FLAG_PHYSDEV)
321 if (cadt_flags & IPSET_FLAG_NOMATCH)
322 flags |= (IPSET_FLAG_NOMATCH << 16);
324 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
325 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
326 ret = adtfn(set, &e, &ext, &ext, flags);
327 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
328 ip_set_eexist(ret, flags) ? 0 : ret;
331 if (tb[IPSET_ATTR_IP_TO]) {
332 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
337 if (ip + UINT_MAX == ip_to)
338 return -IPSET_ERR_HASH_RANGE;
340 ip_set_mask_from_to(ip, ip_to, e.cidr);
343 ip = ntohl(h->next.ip);
344 while (!after(ip, ip_to)) {
346 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
347 ret = adtfn(set, &e, &ext, &ext, flags);
349 if (ret && !ip_set_eexist(ret, flags))
360 struct hash_netiface6_elem_hashed {
361 union nf_inet_addr ip;
368 struct hash_netiface6_elem {
369 union nf_inet_addr ip;
377 /* Common functions */
380 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
381 const struct hash_netiface6_elem *ip2,
384 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
385 ip1->cidr == ip2->cidr &&
387 ip1->physdev == ip2->physdev &&
388 ip1->iface == ip2->iface;
392 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
394 return elem->nomatch ? -ENOTEMPTY : 1;
398 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
400 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
404 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
406 swap(*flags, elem->nomatch);
410 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
412 ip6_netmask(&elem->ip, cidr);
417 hash_netiface6_data_list(struct sk_buff *skb,
418 const struct hash_netiface6_elem *data)
420 u32 flags = data->physdev ? IPSET_FLAG_PHYSDEV : 0;
423 flags |= IPSET_FLAG_NOMATCH;
424 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
425 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
426 nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
428 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
429 goto nla_put_failure;
437 hash_netiface6_data_next(struct hash_netiface4_elem *next,
438 const struct hash_netiface6_elem *d)
447 #define MTYPE hash_netiface6
449 #define HOST_MASK 128
450 #define HKEY_DATALEN sizeof(struct hash_netiface6_elem_hashed)
451 #define IP_SET_EMIT_CREATE
452 #include "ip_set_hash_gen.h"
455 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
456 const struct xt_action_param *par,
457 enum ipset_adt adt, struct ip_set_adt_opt *opt)
459 struct hash_netiface *h = set->data;
460 ipset_adtfn adtfn = set->variant->adt[adt];
461 struct hash_netiface6_elem e = {
462 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
465 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
470 if (adt == IPSET_TEST)
473 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
474 ip6_netmask(&e.ip, e.cidr);
476 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
477 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
478 const struct nf_bridge_info *nf_bridge = skb->nf_bridge;
482 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
488 e.iface = SRCDIR ? IFACE(in) : IFACE(out);
492 ret = iface_test(&h->rbtree, &e.iface);
493 if (adt == IPSET_ADD) {
495 ret = iface_add(&h->rbtree, &e.iface);
502 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
506 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
507 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
509 struct hash_netiface *h = set->data;
510 ipset_adtfn adtfn = set->variant->adt[adt];
511 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
512 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
513 char iface[IFNAMSIZ];
516 if (unlikely(!tb[IPSET_ATTR_IP] ||
517 !tb[IPSET_ATTR_IFACE] ||
518 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
519 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
520 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
521 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
522 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
523 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
524 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
525 return -IPSET_ERR_PROTOCOL;
526 if (unlikely(tb[IPSET_ATTR_IP_TO]))
527 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
529 if (tb[IPSET_ATTR_LINENO])
530 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
532 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
533 ip_set_get_extensions(set, tb, &ext);
537 if (tb[IPSET_ATTR_CIDR])
538 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
539 if (e.cidr > HOST_MASK)
540 return -IPSET_ERR_INVALID_CIDR;
541 ip6_netmask(&e.ip, e.cidr);
543 strcpy(iface, nla_data(tb[IPSET_ATTR_IFACE]));
545 ret = iface_test(&h->rbtree, &e.iface);
546 if (adt == IPSET_ADD) {
548 ret = iface_add(&h->rbtree, &e.iface);
555 if (tb[IPSET_ATTR_CADT_FLAGS]) {
556 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
557 if (cadt_flags & IPSET_FLAG_PHYSDEV)
559 if (cadt_flags & IPSET_FLAG_NOMATCH)
560 flags |= (IPSET_FLAG_NOMATCH << 16);
563 ret = adtfn(set, &e, &ext, &ext, flags);
565 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
566 ip_set_eexist(ret, flags) ? 0 : ret;
569 static struct ip_set_type hash_netiface_type __read_mostly = {
570 .name = "hash:net,iface",
571 .protocol = IPSET_PROTOCOL,
572 .features = IPSET_TYPE_IP | IPSET_TYPE_IFACE |
574 .dimension = IPSET_DIM_TWO,
575 .family = NFPROTO_UNSPEC,
576 .revision_min = IPSET_TYPE_REV_MIN,
577 .revision_max = IPSET_TYPE_REV_MAX,
578 .create = hash_netiface_create,
580 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
581 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
582 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
583 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
584 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
585 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
586 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
589 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
590 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
591 [IPSET_ATTR_IFACE] = { .type = NLA_NUL_STRING,
592 .len = IFNAMSIZ - 1 },
593 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
594 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
595 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
596 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
597 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
598 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
599 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
600 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
601 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
602 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
608 hash_netiface_init(void)
610 return ip_set_type_register(&hash_netiface_type);
614 hash_netiface_fini(void)
616 ip_set_type_unregister(&hash_netiface_type);
619 module_init(hash_netiface_init);
620 module_exit(hash_netiface_fini);