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 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));
125 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
127 const struct hash_net4_telem *tdata =
128 (const struct hash_net4_telem *)data;
129 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
131 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
132 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
133 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
134 htonl(ip_set_timeout_get(tdata->timeout)));
136 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
144 #define IP_SET_HASH_WITH_NETS
148 #include <linux/netfilter/ipset/ip_set_ahash.h>
151 hash_net4_data_next(struct ip_set_hash *h,
152 const struct hash_net4_elem *d)
154 h->next.ip = ntohl(d->ip);
158 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
159 const struct xt_action_param *par,
160 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
162 const struct ip_set_hash *h = set->data;
163 ipset_adtfn adtfn = set->variant->adt[adt];
164 struct hash_net4_elem data = {
165 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
170 if (adt == IPSET_TEST)
171 data.cidr = HOST_MASK;
173 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
174 data.ip &= ip_set_netmask(data.cidr);
176 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
180 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
181 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
183 const struct ip_set_hash *h = set->data;
184 ipset_adtfn adtfn = set->variant->adt[adt];
185 struct hash_net4_elem data = { .cidr = HOST_MASK };
186 u32 timeout = h->timeout;
187 u32 ip = 0, ip_to, last;
190 if (unlikely(!tb[IPSET_ATTR_IP] ||
191 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
192 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
193 return -IPSET_ERR_PROTOCOL;
195 if (tb[IPSET_ATTR_LINENO])
196 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
198 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
202 if (tb[IPSET_ATTR_CIDR]) {
203 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
204 if (!data.cidr || data.cidr > HOST_MASK)
205 return -IPSET_ERR_INVALID_CIDR;
208 if (tb[IPSET_ATTR_TIMEOUT]) {
209 if (!with_timeout(h->timeout))
210 return -IPSET_ERR_TIMEOUT;
211 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
214 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
215 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
216 if (cadt_flags & IPSET_FLAG_NOMATCH)
217 flags |= (cadt_flags << 16);
220 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
221 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
222 ret = adtfn(set, &data, timeout, flags);
223 return ip_set_eexist(ret, flags) ? 0 : ret;
227 if (tb[IPSET_ATTR_IP_TO]) {
228 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
233 if (ip + UINT_MAX == ip_to)
234 return -IPSET_ERR_HASH_RANGE;
238 while (!after(ip, ip_to)) {
240 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
241 ret = adtfn(set, &data, timeout, flags);
242 if (ret && !ip_set_eexist(ret, flags))
252 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
254 const struct ip_set_hash *x = a->data;
255 const struct ip_set_hash *y = b->data;
257 /* Resizing changes htable_bits, so we ignore it */
258 return x->maxelem == y->maxelem &&
259 x->timeout == y->timeout;
262 /* The type variant functions: IPv6 */
264 struct hash_net6_elem {
265 union nf_inet_addr ip;
271 struct hash_net6_telem {
272 union nf_inet_addr ip;
276 unsigned long timeout;
280 hash_net6_data_equal(const struct hash_net6_elem *ip1,
281 const struct hash_net6_elem *ip2,
284 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
285 ip1->cidr == ip2->cidr;
289 hash_net6_data_isnull(const struct hash_net6_elem *elem)
291 return elem->cidr == 0;
295 hash_net6_data_copy(struct hash_net6_elem *dst,
296 const struct hash_net6_elem *src)
298 dst->ip.in6 = src->ip.in6;
299 dst->cidr = src->cidr;
300 dst->nomatch = src->nomatch;
304 hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
306 dst->nomatch = flags & IPSET_FLAG_NOMATCH;
310 hash_net6_data_match(const struct hash_net6_elem *elem)
312 return !elem->nomatch;
316 hash_net6_data_zero_out(struct hash_net6_elem *elem)
322 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
324 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
325 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
326 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
327 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
331 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
333 ip6_netmask(&elem->ip, cidr);
338 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
340 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
342 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
343 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
345 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
353 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
355 const struct hash_net6_telem *e =
356 (const struct hash_net6_telem *)data;
357 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
359 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
360 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
361 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
362 htonl(ip_set_timeout_get(e->timeout)));
364 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
375 #define HOST_MASK 128
376 #include <linux/netfilter/ipset/ip_set_ahash.h>
379 hash_net6_data_next(struct ip_set_hash *h,
380 const struct hash_net6_elem *d)
385 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
386 const struct xt_action_param *par,
387 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
389 const struct ip_set_hash *h = set->data;
390 ipset_adtfn adtfn = set->variant->adt[adt];
391 struct hash_net6_elem data = {
392 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
397 if (adt == IPSET_TEST)
398 data.cidr = HOST_MASK;
400 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
401 ip6_netmask(&data.ip, data.cidr);
403 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
407 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
408 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
410 const struct ip_set_hash *h = set->data;
411 ipset_adtfn adtfn = set->variant->adt[adt];
412 struct hash_net6_elem data = { .cidr = HOST_MASK };
413 u32 timeout = h->timeout;
416 if (unlikely(!tb[IPSET_ATTR_IP] ||
417 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
418 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
419 return -IPSET_ERR_PROTOCOL;
420 if (unlikely(tb[IPSET_ATTR_IP_TO]))
421 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
423 if (tb[IPSET_ATTR_LINENO])
424 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
426 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
430 if (tb[IPSET_ATTR_CIDR])
431 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
433 if (!data.cidr || data.cidr > HOST_MASK)
434 return -IPSET_ERR_INVALID_CIDR;
436 ip6_netmask(&data.ip, data.cidr);
438 if (tb[IPSET_ATTR_TIMEOUT]) {
439 if (!with_timeout(h->timeout))
440 return -IPSET_ERR_TIMEOUT;
441 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
444 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
445 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
446 if (cadt_flags & IPSET_FLAG_NOMATCH)
447 flags |= (cadt_flags << 16);
450 ret = adtfn(set, &data, timeout, flags);
452 return ip_set_eexist(ret, flags) ? 0 : ret;
455 /* Create hash:ip type of sets */
458 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
460 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
461 struct ip_set_hash *h;
465 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
466 return -IPSET_ERR_INVALID_FAMILY;
468 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
470 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
471 return -IPSET_ERR_PROTOCOL;
473 if (tb[IPSET_ATTR_HASHSIZE]) {
474 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
475 if (hashsize < IPSET_MIMINAL_HASHSIZE)
476 hashsize = IPSET_MIMINAL_HASHSIZE;
479 if (tb[IPSET_ATTR_MAXELEM])
480 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
482 h = kzalloc(sizeof(*h)
483 + sizeof(struct ip_set_hash_nets)
484 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
488 h->maxelem = maxelem;
489 get_random_bytes(&h->initval, sizeof(h->initval));
490 h->timeout = IPSET_NO_TIMEOUT;
492 hbits = htable_bits(hashsize);
493 hsize = htable_size(hbits);
498 h->table = ip_set_alloc(hsize);
503 h->table->htable_bits = hbits;
507 if (tb[IPSET_ATTR_TIMEOUT]) {
508 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
510 set->variant = set->family == NFPROTO_IPV4
511 ? &hash_net4_tvariant : &hash_net6_tvariant;
513 if (set->family == NFPROTO_IPV4)
514 hash_net4_gc_init(set);
516 hash_net6_gc_init(set);
518 set->variant = set->family == NFPROTO_IPV4
519 ? &hash_net4_variant : &hash_net6_variant;
522 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
523 set->name, jhash_size(h->table->htable_bits),
524 h->table->htable_bits, h->maxelem, set->data, h->table);
529 static struct ip_set_type hash_net_type __read_mostly = {
531 .protocol = IPSET_PROTOCOL,
532 .features = IPSET_TYPE_IP,
533 .dimension = IPSET_DIM_ONE,
534 .family = NFPROTO_UNSPEC,
536 /* = 1 Range as input support for IPv4 added */
537 .revision_max = 2, /* nomatch flag support added */
538 .create = hash_net_create,
540 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
541 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
542 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
543 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
544 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
547 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
548 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
549 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
550 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
551 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
559 return ip_set_type_register(&hash_net_type);
565 ip_set_type_unregister(&hash_net_type);
568 module_init(hash_net_init);
569 module_exit(hash_net_fini);