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 #define REVISION_MIN 0
28 #define REVISION_MAX 0
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("hash:ip", REVISION_MIN, REVISION_MAX);
33 MODULE_ALIAS("ip_set_hash:ip");
35 /* Type specific function prefix */
39 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b);
41 #define hash_ip4_same_set hash_ip_same_set
42 #define hash_ip6_same_set hash_ip_same_set
44 /* The type variant functions: IPv4 */
46 /* Member elements without timeout */
47 struct hash_ip4_elem {
51 /* Member elements with timeout support */
52 struct hash_ip4_telem {
54 unsigned long timeout;
58 hash_ip4_data_equal(const struct hash_ip4_elem *ip1,
59 const struct hash_ip4_elem *ip2,
62 return ip1->ip == ip2->ip;
66 hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
72 hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
77 /* Zero valued IP addresses cannot be stored */
79 hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
85 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *data)
87 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip))
96 hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
98 const struct hash_ip4_telem *tdata =
99 (const struct hash_ip4_telem *)data;
101 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
102 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
103 htonl(ip_set_timeout_get(tdata->timeout))))
104 goto nla_put_failure;
112 #define IP_SET_HASH_WITH_NETMASK
115 #include <linux/netfilter/ipset/ip_set_ahash.h>
118 hash_ip4_data_next(struct ip_set_hash *h, const struct hash_ip4_elem *d)
124 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
125 const struct xt_action_param *par,
126 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
128 const struct ip_set_hash *h = set->data;
129 ipset_adtfn adtfn = set->variant->adt[adt];
132 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
133 ip &= ip_set_netmask(h->netmask);
137 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
141 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
142 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
144 const struct ip_set_hash *h = set->data;
145 ipset_adtfn adtfn = set->variant->adt[adt];
146 u32 ip, ip_to, hosts, timeout = h->timeout;
150 if (unlikely(!tb[IPSET_ATTR_IP] ||
151 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
152 return -IPSET_ERR_PROTOCOL;
154 if (tb[IPSET_ATTR_LINENO])
155 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
157 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
161 ip &= ip_set_hostmask(h->netmask);
163 if (tb[IPSET_ATTR_TIMEOUT]) {
164 if (!with_timeout(h->timeout))
165 return -IPSET_ERR_TIMEOUT;
166 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
169 if (adt == IPSET_TEST) {
172 return -IPSET_ERR_HASH_ELEM;
173 return adtfn(set, &nip, timeout, flags);
177 if (tb[IPSET_ATTR_IP_TO]) {
178 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
183 } else if (tb[IPSET_ATTR_CIDR]) {
184 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
186 if (!cidr || cidr > 32)
187 return -IPSET_ERR_INVALID_CIDR;
188 ip_set_mask_from_to(ip, ip_to, cidr);
191 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
194 ip = ntohl(h->next.ip);
195 for (; !before(ip_to, ip); ip += hosts) {
198 return -IPSET_ERR_HASH_ELEM;
199 ret = adtfn(set, &nip, timeout, flags);
201 if (ret && !ip_set_eexist(ret, flags))
210 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b)
212 const struct ip_set_hash *x = a->data;
213 const struct ip_set_hash *y = b->data;
215 /* Resizing changes htable_bits, so we ignore it */
216 return x->maxelem == y->maxelem &&
217 x->timeout == y->timeout &&
218 x->netmask == y->netmask;
221 /* The type variant functions: IPv6 */
223 struct hash_ip6_elem {
224 union nf_inet_addr ip;
227 struct hash_ip6_telem {
228 union nf_inet_addr ip;
229 unsigned long timeout;
233 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
234 const struct hash_ip6_elem *ip2,
237 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
241 hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
243 return ipv6_addr_any(&elem->ip.in6);
247 hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
249 dst->ip.in6 = src->ip.in6;
253 hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
255 ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
259 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
261 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
262 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
263 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
264 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
268 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *data)
270 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6))
271 goto nla_put_failure;
279 hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
281 const struct hash_ip6_telem *e =
282 (const struct hash_ip6_telem *)data;
284 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
285 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
286 htonl(ip_set_timeout_get(e->timeout))))
287 goto nla_put_failure;
298 #define HOST_MASK 128
299 #include <linux/netfilter/ipset/ip_set_ahash.h>
302 hash_ip6_data_next(struct ip_set_hash *h, const struct hash_ip6_elem *d)
307 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
308 const struct xt_action_param *par,
309 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
311 const struct ip_set_hash *h = set->data;
312 ipset_adtfn adtfn = set->variant->adt[adt];
313 union nf_inet_addr ip;
315 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip.in6);
316 ip6_netmask(&ip, h->netmask);
317 if (ipv6_addr_any(&ip.in6))
320 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
323 static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX + 1] = {
324 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
325 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
326 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
330 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
331 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
333 const struct ip_set_hash *h = set->data;
334 ipset_adtfn adtfn = set->variant->adt[adt];
335 union nf_inet_addr ip;
336 u32 timeout = h->timeout;
339 if (unlikely(!tb[IPSET_ATTR_IP] ||
340 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
341 tb[IPSET_ATTR_IP_TO] ||
342 tb[IPSET_ATTR_CIDR]))
343 return -IPSET_ERR_PROTOCOL;
345 if (tb[IPSET_ATTR_LINENO])
346 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
348 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &ip);
352 ip6_netmask(&ip, h->netmask);
353 if (ipv6_addr_any(&ip.in6))
354 return -IPSET_ERR_HASH_ELEM;
356 if (tb[IPSET_ATTR_TIMEOUT]) {
357 if (!with_timeout(h->timeout))
358 return -IPSET_ERR_TIMEOUT;
359 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
362 ret = adtfn(set, &ip, timeout, flags);
364 return ip_set_eexist(ret, flags) ? 0 : ret;
367 /* Create hash:ip type of sets */
370 hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
372 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
375 struct ip_set_hash *h;
377 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
378 return -IPSET_ERR_INVALID_FAMILY;
379 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
380 pr_debug("Create set %s with family %s\n",
381 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
383 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
384 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
385 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
386 return -IPSET_ERR_PROTOCOL;
388 if (tb[IPSET_ATTR_HASHSIZE]) {
389 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
390 if (hashsize < IPSET_MIMINAL_HASHSIZE)
391 hashsize = IPSET_MIMINAL_HASHSIZE;
394 if (tb[IPSET_ATTR_MAXELEM])
395 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
397 if (tb[IPSET_ATTR_NETMASK]) {
398 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
400 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
401 (set->family == NFPROTO_IPV6 && netmask > 128) ||
403 return -IPSET_ERR_INVALID_NETMASK;
406 h = kzalloc(sizeof(*h), GFP_KERNEL);
410 h->maxelem = maxelem;
411 h->netmask = netmask;
412 get_random_bytes(&h->initval, sizeof(h->initval));
413 h->timeout = IPSET_NO_TIMEOUT;
415 hbits = htable_bits(hashsize);
416 hsize = htable_size(hbits);
421 h->table = ip_set_alloc(hsize);
426 h->table->htable_bits = hbits;
430 if (tb[IPSET_ATTR_TIMEOUT]) {
431 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
433 set->variant = set->family == NFPROTO_IPV4
434 ? &hash_ip4_tvariant : &hash_ip6_tvariant;
436 if (set->family == NFPROTO_IPV4)
437 hash_ip4_gc_init(set);
439 hash_ip6_gc_init(set);
441 set->variant = set->family == NFPROTO_IPV4
442 ? &hash_ip4_variant : &hash_ip6_variant;
445 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
446 set->name, jhash_size(h->table->htable_bits),
447 h->table->htable_bits, h->maxelem, set->data, h->table);
452 static struct ip_set_type hash_ip_type __read_mostly = {
454 .protocol = IPSET_PROTOCOL,
455 .features = IPSET_TYPE_IP,
456 .dimension = IPSET_DIM_ONE,
457 .family = NFPROTO_UNSPEC,
458 .revision_min = REVISION_MIN,
459 .revision_max = REVISION_MAX,
460 .create = hash_ip_create,
462 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
463 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
464 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
465 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
466 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
467 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
470 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
471 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
472 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
473 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
474 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
482 return ip_set_type_register(&hash_ip_type);
488 ip_set_type_unregister(&hash_ip_type);
491 module_init(hash_ip_init);
492 module_exit(hash_ip_fini);