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:ip 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>
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_timeout.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
29 MODULE_DESCRIPTION("hash:ip type of IP sets");
30 MODULE_ALIAS("ip_set_hash:ip");
32 /* Type specific function prefix */
36 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b);
38 #define hash_ip4_same_set hash_ip_same_set
39 #define hash_ip6_same_set hash_ip_same_set
41 /* The type variant functions: IPv4 */
43 /* Member elements without timeout */
44 struct hash_ip4_elem {
48 /* Member elements with timeout support */
49 struct hash_ip4_telem {
51 unsigned long timeout;
55 hash_ip4_data_equal(const struct hash_ip4_elem *ip1,
56 const struct hash_ip4_elem *ip2)
58 return ip1->ip == ip2->ip;
62 hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
68 hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
73 /* Zero valued IP addresses cannot be stored */
75 hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
81 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *data)
83 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
91 hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
93 const struct hash_ip4_telem *tdata =
94 (const struct hash_ip4_telem *)data;
96 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
97 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
98 htonl(ip_set_timeout_get(tdata->timeout)));
106 #define IP_SET_HASH_WITH_NETMASK
109 #include <linux/netfilter/ipset/ip_set_ahash.h>
112 hash_ip4_data_next(struct ip_set_hash *h, const struct hash_ip4_elem *d)
114 h->next.ip = ntohl(d->ip);
118 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
119 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
121 const struct ip_set_hash *h = set->data;
122 ipset_adtfn adtfn = set->variant->adt[adt];
125 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
126 ip &= ip_set_netmask(h->netmask);
130 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
134 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
135 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
137 const struct ip_set_hash *h = set->data;
138 ipset_adtfn adtfn = set->variant->adt[adt];
139 u32 ip, ip_to, hosts, timeout = h->timeout;
143 if (unlikely(!tb[IPSET_ATTR_IP] ||
144 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
145 return -IPSET_ERR_PROTOCOL;
147 if (tb[IPSET_ATTR_LINENO])
148 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
150 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
154 ip &= ip_set_hostmask(h->netmask);
156 if (tb[IPSET_ATTR_TIMEOUT]) {
157 if (!with_timeout(h->timeout))
158 return -IPSET_ERR_TIMEOUT;
159 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
162 if (adt == IPSET_TEST) {
165 return -IPSET_ERR_HASH_ELEM;
166 return adtfn(set, &nip, timeout, flags);
169 if (tb[IPSET_ATTR_IP_TO]) {
170 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
175 } else if (tb[IPSET_ATTR_CIDR]) {
176 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
179 return -IPSET_ERR_INVALID_CIDR;
180 ip &= ip_set_hostmask(cidr);
181 ip_to = ip | ~ip_set_hostmask(cidr);
185 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
189 for (; !before(ip_to, ip); ip += hosts) {
192 return -IPSET_ERR_HASH_ELEM;
193 ret = adtfn(set, &nip, timeout, flags);
195 if (ret && !ip_set_eexist(ret, flags))
204 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b)
206 const struct ip_set_hash *x = a->data;
207 const struct ip_set_hash *y = b->data;
209 /* Resizing changes htable_bits, so we ignore it */
210 return x->maxelem == y->maxelem &&
211 x->timeout == y->timeout &&
212 x->netmask == y->netmask;
215 /* The type variant functions: IPv6 */
217 struct hash_ip6_elem {
218 union nf_inet_addr ip;
221 struct hash_ip6_telem {
222 union nf_inet_addr ip;
223 unsigned long timeout;
227 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
228 const struct hash_ip6_elem *ip2)
230 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0;
234 hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
236 return ipv6_addr_any(&elem->ip.in6);
240 hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
242 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
246 hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
248 ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
252 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
254 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
255 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
256 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
257 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
261 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *data)
263 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
271 hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
273 const struct hash_ip6_telem *e =
274 (const struct hash_ip6_telem *)data;
276 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
277 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
278 htonl(ip_set_timeout_get(e->timeout)));
289 #define HOST_MASK 128
290 #include <linux/netfilter/ipset/ip_set_ahash.h>
293 hash_ip6_data_next(struct ip_set_hash *h, const struct hash_ip6_elem *d)
298 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
299 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
301 const struct ip_set_hash *h = set->data;
302 ipset_adtfn adtfn = set->variant->adt[adt];
303 union nf_inet_addr ip;
305 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip.in6);
306 ip6_netmask(&ip, h->netmask);
307 if (ipv6_addr_any(&ip.in6))
310 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
313 static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX + 1] = {
314 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
315 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
316 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
320 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
321 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
323 const struct ip_set_hash *h = set->data;
324 ipset_adtfn adtfn = set->variant->adt[adt];
325 union nf_inet_addr ip;
326 u32 timeout = h->timeout;
329 if (unlikely(!tb[IPSET_ATTR_IP] ||
330 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
331 tb[IPSET_ATTR_IP_TO] ||
332 tb[IPSET_ATTR_CIDR]))
333 return -IPSET_ERR_PROTOCOL;
335 if (tb[IPSET_ATTR_LINENO])
336 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
338 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &ip);
342 ip6_netmask(&ip, h->netmask);
343 if (ipv6_addr_any(&ip.in6))
344 return -IPSET_ERR_HASH_ELEM;
346 if (tb[IPSET_ATTR_TIMEOUT]) {
347 if (!with_timeout(h->timeout))
348 return -IPSET_ERR_TIMEOUT;
349 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
352 ret = adtfn(set, &ip, timeout, flags);
354 return ip_set_eexist(ret, flags) ? 0 : ret;
357 /* Create hash:ip type of sets */
360 hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
362 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
364 struct ip_set_hash *h;
366 if (!(set->family == AF_INET || set->family == AF_INET6))
367 return -IPSET_ERR_INVALID_FAMILY;
368 netmask = set->family == AF_INET ? 32 : 128;
369 pr_debug("Create set %s with family %s\n",
370 set->name, set->family == AF_INET ? "inet" : "inet6");
372 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
373 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
374 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
375 return -IPSET_ERR_PROTOCOL;
377 if (tb[IPSET_ATTR_HASHSIZE]) {
378 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
379 if (hashsize < IPSET_MIMINAL_HASHSIZE)
380 hashsize = IPSET_MIMINAL_HASHSIZE;
383 if (tb[IPSET_ATTR_MAXELEM])
384 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
386 if (tb[IPSET_ATTR_NETMASK]) {
387 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
389 if ((set->family == AF_INET && netmask > 32) ||
390 (set->family == AF_INET6 && netmask > 128) ||
392 return -IPSET_ERR_INVALID_NETMASK;
395 h = kzalloc(sizeof(*h), GFP_KERNEL);
399 h->maxelem = maxelem;
400 h->netmask = netmask;
401 get_random_bytes(&h->initval, sizeof(h->initval));
402 h->timeout = IPSET_NO_TIMEOUT;
404 hbits = htable_bits(hashsize);
405 h->table = ip_set_alloc(
406 sizeof(struct htable)
407 + jhash_size(hbits) * sizeof(struct hbucket));
412 h->table->htable_bits = hbits;
416 if (tb[IPSET_ATTR_TIMEOUT]) {
417 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
419 set->variant = set->family == AF_INET
420 ? &hash_ip4_tvariant : &hash_ip6_tvariant;
422 if (set->family == AF_INET)
423 hash_ip4_gc_init(set);
425 hash_ip6_gc_init(set);
427 set->variant = set->family == AF_INET
428 ? &hash_ip4_variant : &hash_ip6_variant;
431 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
432 set->name, jhash_size(h->table->htable_bits),
433 h->table->htable_bits, h->maxelem, set->data, h->table);
438 static struct ip_set_type hash_ip_type __read_mostly = {
440 .protocol = IPSET_PROTOCOL,
441 .features = IPSET_TYPE_IP,
442 .dimension = IPSET_DIM_ONE,
445 .create = hash_ip_create,
447 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
448 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
449 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
450 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
451 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
452 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
455 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
456 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
457 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
458 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
459 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
467 return ip_set_type_register(&hash_ip_type);
473 ip_set_type_unregister(&hash_ip_type);
476 module_init(hash_ip_init);
477 module_exit(hash_ip_fini);