1 /* Copyright (C) 2003-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 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>
18 #include <net/netlink.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28 MODULE_DESCRIPTION("hash:net type of IP sets");
29 MODULE_ALIAS("ip_set_hash:net");
31 /* Type specific function prefix */
35 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
37 #define hash_net4_same_set hash_net_same_set
38 #define hash_net6_same_set hash_net_same_set
40 /* The type variant functions: IPv4 */
42 /* Member elements without timeout */
43 struct hash_net4_elem {
50 /* Member elements with timeout support */
51 struct hash_net4_telem {
56 unsigned long timeout;
60 hash_net4_data_equal(const struct hash_net4_elem *ip1,
61 const struct hash_net4_elem *ip2,
64 return ip1->ip == ip2->ip &&
65 ip1->cidr == ip2->cidr;
69 hash_net4_data_isnull(const struct hash_net4_elem *elem)
71 return elem->cidr == 0;
75 hash_net4_data_copy(struct hash_net4_elem *dst,
76 const struct hash_net4_elem *src)
79 dst->cidr = src->cidr;
80 dst->nomatch = src->nomatch;
84 hash_net4_data_flags(struct hash_net4_elem *dst, u32 flags)
86 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
90 hash_net4_data_match(const struct hash_net4_elem *elem)
92 return !elem->nomatch;
96 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
98 elem->ip &= ip_set_netmask(cidr);
102 /* Zero CIDR values cannot be stored */
104 hash_net4_data_zero_out(struct hash_net4_elem *elem)
110 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
112 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
114 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
115 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
117 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
118 goto nla_put_failure;
126 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
128 const struct hash_net4_telem *tdata =
129 (const struct hash_net4_telem *)data;
130 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
132 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
133 nla_put_u8(skb, IPSET_ATTR_CIDR, tdata->cidr) ||
134 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
135 htonl(ip_set_timeout_get(tdata->timeout))) ||
137 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
138 goto nla_put_failure;
145 #define IP_SET_HASH_WITH_NETS
149 #include <linux/netfilter/ipset/ip_set_ahash.h>
152 hash_net4_data_next(struct ip_set_hash *h,
153 const struct hash_net4_elem *d)
155 h->next.ip = ntohl(d->ip);
159 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
160 const struct xt_action_param *par,
161 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
163 const struct ip_set_hash *h = set->data;
164 ipset_adtfn adtfn = set->variant->adt[adt];
165 struct hash_net4_elem data = {
166 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
171 if (adt == IPSET_TEST)
172 data.cidr = HOST_MASK;
174 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
175 data.ip &= ip_set_netmask(data.cidr);
177 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
181 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
182 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
184 const struct ip_set_hash *h = set->data;
185 ipset_adtfn adtfn = set->variant->adt[adt];
186 struct hash_net4_elem data = { .cidr = HOST_MASK };
187 u32 timeout = h->timeout;
188 u32 ip = 0, ip_to, last;
191 if (unlikely(!tb[IPSET_ATTR_IP] ||
192 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
193 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
194 return -IPSET_ERR_PROTOCOL;
196 if (tb[IPSET_ATTR_LINENO])
197 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
199 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
203 if (tb[IPSET_ATTR_CIDR]) {
204 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
205 if (!data.cidr || data.cidr > HOST_MASK)
206 return -IPSET_ERR_INVALID_CIDR;
209 if (tb[IPSET_ATTR_TIMEOUT]) {
210 if (!with_timeout(h->timeout))
211 return -IPSET_ERR_TIMEOUT;
212 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
215 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
216 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
217 if (cadt_flags & IPSET_FLAG_NOMATCH)
218 flags |= (cadt_flags << 16);
221 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
222 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
223 ret = adtfn(set, &data, timeout, flags);
224 return ip_set_eexist(ret, flags) ? 0 : ret;
228 if (tb[IPSET_ATTR_IP_TO]) {
229 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
234 if (ip + UINT_MAX == ip_to)
235 return -IPSET_ERR_HASH_RANGE;
239 while (!after(ip, ip_to)) {
241 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
242 ret = adtfn(set, &data, timeout, flags);
243 if (ret && !ip_set_eexist(ret, flags))
253 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
255 const struct ip_set_hash *x = a->data;
256 const struct ip_set_hash *y = b->data;
258 /* Resizing changes htable_bits, so we ignore it */
259 return x->maxelem == y->maxelem &&
260 x->timeout == y->timeout;
263 /* The type variant functions: IPv6 */
265 struct hash_net6_elem {
266 union nf_inet_addr ip;
272 struct hash_net6_telem {
273 union nf_inet_addr ip;
277 unsigned long timeout;
281 hash_net6_data_equal(const struct hash_net6_elem *ip1,
282 const struct hash_net6_elem *ip2,
285 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
286 ip1->cidr == ip2->cidr;
290 hash_net6_data_isnull(const struct hash_net6_elem *elem)
292 return elem->cidr == 0;
296 hash_net6_data_copy(struct hash_net6_elem *dst,
297 const struct hash_net6_elem *src)
299 dst->ip.in6 = src->ip.in6;
300 dst->cidr = src->cidr;
301 dst->nomatch = src->nomatch;
305 hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
307 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
311 hash_net6_data_match(const struct hash_net6_elem *elem)
313 return !elem->nomatch;
317 hash_net6_data_zero_out(struct hash_net6_elem *elem)
323 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
325 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
326 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
327 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
328 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
332 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
334 ip6_netmask(&elem->ip, cidr);
339 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
341 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
343 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
344 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
346 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
347 goto nla_put_failure;
355 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
357 const struct hash_net6_telem *e =
358 (const struct hash_net6_telem *)data;
359 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
361 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
362 nla_put_u8(skb, IPSET_ATTR_CIDR, e->cidr) ||
363 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
364 htonl(ip_set_timeout_get(e->timeout))) ||
366 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
367 goto nla_put_failure;
378 #define HOST_MASK 128
379 #include <linux/netfilter/ipset/ip_set_ahash.h>
382 hash_net6_data_next(struct ip_set_hash *h,
383 const struct hash_net6_elem *d)
388 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
389 const struct xt_action_param *par,
390 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
392 const struct ip_set_hash *h = set->data;
393 ipset_adtfn adtfn = set->variant->adt[adt];
394 struct hash_net6_elem data = {
395 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
400 if (adt == IPSET_TEST)
401 data.cidr = HOST_MASK;
403 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
404 ip6_netmask(&data.ip, data.cidr);
406 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
410 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
411 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
413 const struct ip_set_hash *h = set->data;
414 ipset_adtfn adtfn = set->variant->adt[adt];
415 struct hash_net6_elem data = { .cidr = HOST_MASK };
416 u32 timeout = h->timeout;
419 if (unlikely(!tb[IPSET_ATTR_IP] ||
420 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
421 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
422 return -IPSET_ERR_PROTOCOL;
423 if (unlikely(tb[IPSET_ATTR_IP_TO]))
424 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
426 if (tb[IPSET_ATTR_LINENO])
427 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
429 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
433 if (tb[IPSET_ATTR_CIDR])
434 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
436 if (!data.cidr || data.cidr > HOST_MASK)
437 return -IPSET_ERR_INVALID_CIDR;
439 ip6_netmask(&data.ip, data.cidr);
441 if (tb[IPSET_ATTR_TIMEOUT]) {
442 if (!with_timeout(h->timeout))
443 return -IPSET_ERR_TIMEOUT;
444 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
447 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
448 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
449 if (cadt_flags & IPSET_FLAG_NOMATCH)
450 flags |= (cadt_flags << 16);
453 ret = adtfn(set, &data, timeout, flags);
455 return ip_set_eexist(ret, flags) ? 0 : ret;
458 /* Create hash:ip type of sets */
461 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
463 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
464 struct ip_set_hash *h;
468 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
469 return -IPSET_ERR_INVALID_FAMILY;
471 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
472 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
473 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
474 return -IPSET_ERR_PROTOCOL;
476 if (tb[IPSET_ATTR_HASHSIZE]) {
477 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
478 if (hashsize < IPSET_MIMINAL_HASHSIZE)
479 hashsize = IPSET_MIMINAL_HASHSIZE;
482 if (tb[IPSET_ATTR_MAXELEM])
483 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
485 h = kzalloc(sizeof(*h)
486 + sizeof(struct ip_set_hash_nets)
487 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
491 h->maxelem = maxelem;
492 get_random_bytes(&h->initval, sizeof(h->initval));
493 h->timeout = IPSET_NO_TIMEOUT;
495 hbits = htable_bits(hashsize);
496 hsize = htable_size(hbits);
501 h->table = ip_set_alloc(hsize);
506 h->table->htable_bits = hbits;
510 if (tb[IPSET_ATTR_TIMEOUT]) {
511 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
513 set->variant = set->family == NFPROTO_IPV4
514 ? &hash_net4_tvariant : &hash_net6_tvariant;
516 if (set->family == NFPROTO_IPV4)
517 hash_net4_gc_init(set);
519 hash_net6_gc_init(set);
521 set->variant = set->family == NFPROTO_IPV4
522 ? &hash_net4_variant : &hash_net6_variant;
525 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
526 set->name, jhash_size(h->table->htable_bits),
527 h->table->htable_bits, h->maxelem, set->data, h->table);
532 static struct ip_set_type hash_net_type __read_mostly = {
534 .protocol = IPSET_PROTOCOL,
535 .features = IPSET_TYPE_IP,
536 .dimension = IPSET_DIM_ONE,
537 .family = NFPROTO_UNSPEC,
539 /* = 1 Range as input support for IPv4 added */
540 .revision_max = 2, /* nomatch flag support added */
541 .create = hash_net_create,
543 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
544 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
545 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
546 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
547 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
550 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
551 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
552 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
553 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
554 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
562 return ip_set_type_register(&hash_net_type);
568 ip_set_type_unregister(&hash_net_type);
571 module_init(hash_net_init);
572 module_exit(hash_net_fini);