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 && ip1->cidr == ip2->cidr;
68 hash_net4_data_isnull(const struct hash_net4_elem *elem)
70 return elem->cidr == 0;
74 hash_net4_data_copy(struct hash_net4_elem *dst,
75 const struct hash_net4_elem *src)
78 dst->cidr = src->cidr;
82 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
84 elem->ip &= ip_set_netmask(cidr);
88 /* Zero CIDR values cannot be stored */
90 hash_net4_data_zero_out(struct hash_net4_elem *elem)
96 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
98 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
99 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
107 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
109 const struct hash_net4_telem *tdata =
110 (const struct hash_net4_telem *)data;
112 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
113 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
114 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
115 htonl(ip_set_timeout_get(tdata->timeout)));
123 #define IP_SET_HASH_WITH_NETS
127 #include <linux/netfilter/ipset/ip_set_ahash.h>
130 hash_net4_data_next(struct ip_set_hash *h,
131 const struct hash_net4_elem *d)
133 h->next.ip = ntohl(d->ip);
137 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
138 const struct xt_action_param *par,
139 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
141 const struct ip_set_hash *h = set->data;
142 ipset_adtfn adtfn = set->variant->adt[adt];
143 struct hash_net4_elem data = {
144 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
149 if (adt == IPSET_TEST)
150 data.cidr = HOST_MASK;
152 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
153 data.ip &= ip_set_netmask(data.cidr);
155 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
159 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
160 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
162 const struct ip_set_hash *h = set->data;
163 ipset_adtfn adtfn = set->variant->adt[adt];
164 struct hash_net4_elem data = { .cidr = HOST_MASK };
165 u32 timeout = h->timeout;
166 u32 ip = 0, ip_to, last;
169 if (unlikely(!tb[IPSET_ATTR_IP] ||
170 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
171 return -IPSET_ERR_PROTOCOL;
173 if (tb[IPSET_ATTR_LINENO])
174 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
176 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
180 if (tb[IPSET_ATTR_CIDR]) {
181 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
183 return -IPSET_ERR_INVALID_CIDR;
186 if (tb[IPSET_ATTR_TIMEOUT]) {
187 if (!with_timeout(h->timeout))
188 return -IPSET_ERR_TIMEOUT;
189 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
192 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
193 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
194 ret = adtfn(set, &data, timeout, flags);
195 return ip_set_eexist(ret, flags) ? 0 : ret;
199 if (tb[IPSET_ATTR_IP_TO]) {
200 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
205 if (ip + UINT_MAX == ip_to)
206 return -IPSET_ERR_HASH_RANGE;
210 while (!after(ip, ip_to)) {
212 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
213 ret = adtfn(set, &data, timeout, flags);
214 if (ret && !ip_set_eexist(ret, flags))
224 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
226 const struct ip_set_hash *x = a->data;
227 const struct ip_set_hash *y = b->data;
229 /* Resizing changes htable_bits, so we ignore it */
230 return x->maxelem == y->maxelem &&
231 x->timeout == y->timeout;
234 /* The type variant functions: IPv6 */
236 struct hash_net6_elem {
237 union nf_inet_addr ip;
243 struct hash_net6_telem {
244 union nf_inet_addr ip;
248 unsigned long timeout;
252 hash_net6_data_equal(const struct hash_net6_elem *ip1,
253 const struct hash_net6_elem *ip2,
256 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
257 ip1->cidr == ip2->cidr;
261 hash_net6_data_isnull(const struct hash_net6_elem *elem)
263 return elem->cidr == 0;
267 hash_net6_data_copy(struct hash_net6_elem *dst,
268 const struct hash_net6_elem *src)
270 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
271 dst->cidr = src->cidr;
275 hash_net6_data_zero_out(struct hash_net6_elem *elem)
281 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
283 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
284 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
285 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
286 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
290 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
292 ip6_netmask(&elem->ip, cidr);
297 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
299 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
300 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
308 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
310 const struct hash_net6_telem *e =
311 (const struct hash_net6_telem *)data;
313 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
314 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
315 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
316 htonl(ip_set_timeout_get(e->timeout)));
327 #define HOST_MASK 128
328 #include <linux/netfilter/ipset/ip_set_ahash.h>
331 hash_net6_data_next(struct ip_set_hash *h,
332 const struct hash_net6_elem *d)
337 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
338 const struct xt_action_param *par,
339 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
341 const struct ip_set_hash *h = set->data;
342 ipset_adtfn adtfn = set->variant->adt[adt];
343 struct hash_net6_elem data = {
344 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
349 if (adt == IPSET_TEST)
350 data.cidr = HOST_MASK;
352 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
353 ip6_netmask(&data.ip, data.cidr);
355 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
359 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
360 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
362 const struct ip_set_hash *h = set->data;
363 ipset_adtfn adtfn = set->variant->adt[adt];
364 struct hash_net6_elem data = { .cidr = HOST_MASK };
365 u32 timeout = h->timeout;
368 if (unlikely(!tb[IPSET_ATTR_IP] ||
369 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
370 return -IPSET_ERR_PROTOCOL;
371 if (unlikely(tb[IPSET_ATTR_IP_TO]))
372 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
374 if (tb[IPSET_ATTR_LINENO])
375 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
377 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
381 if (tb[IPSET_ATTR_CIDR])
382 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
385 return -IPSET_ERR_INVALID_CIDR;
387 ip6_netmask(&data.ip, data.cidr);
389 if (tb[IPSET_ATTR_TIMEOUT]) {
390 if (!with_timeout(h->timeout))
391 return -IPSET_ERR_TIMEOUT;
392 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
395 ret = adtfn(set, &data, timeout, flags);
397 return ip_set_eexist(ret, flags) ? 0 : ret;
400 /* Create hash:ip type of sets */
403 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
405 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
406 struct ip_set_hash *h;
409 if (!(set->family == AF_INET || set->family == AF_INET6))
410 return -IPSET_ERR_INVALID_FAMILY;
412 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
413 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
414 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
415 return -IPSET_ERR_PROTOCOL;
417 if (tb[IPSET_ATTR_HASHSIZE]) {
418 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
419 if (hashsize < IPSET_MIMINAL_HASHSIZE)
420 hashsize = IPSET_MIMINAL_HASHSIZE;
423 if (tb[IPSET_ATTR_MAXELEM])
424 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
426 h = kzalloc(sizeof(*h)
427 + sizeof(struct ip_set_hash_nets)
428 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
432 h->maxelem = maxelem;
433 get_random_bytes(&h->initval, sizeof(h->initval));
434 h->timeout = IPSET_NO_TIMEOUT;
436 hbits = htable_bits(hashsize);
437 h->table = ip_set_alloc(
438 sizeof(struct htable)
439 + jhash_size(hbits) * sizeof(struct hbucket));
444 h->table->htable_bits = hbits;
448 if (tb[IPSET_ATTR_TIMEOUT]) {
449 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
451 set->variant = set->family == AF_INET
452 ? &hash_net4_tvariant : &hash_net6_tvariant;
454 if (set->family == AF_INET)
455 hash_net4_gc_init(set);
457 hash_net6_gc_init(set);
459 set->variant = set->family == AF_INET
460 ? &hash_net4_variant : &hash_net6_variant;
463 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
464 set->name, jhash_size(h->table->htable_bits),
465 h->table->htable_bits, h->maxelem, set->data, h->table);
470 static struct ip_set_type hash_net_type __read_mostly = {
472 .protocol = IPSET_PROTOCOL,
473 .features = IPSET_TYPE_IP,
474 .dimension = IPSET_DIM_ONE,
477 .revision_max = 1, /* Range as input support for IPv4 added */
478 .create = hash_net_create,
480 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
481 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
482 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
483 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
484 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
487 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
488 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
489 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
490 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
498 return ip_set_type_register(&hash_net_type);
504 ip_set_type_unregister(&hash_net_type);
507 module_init(hash_net_init);
508 module_exit(hash_net_fini);