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,
59 return ip1->ip == ip2->ip;
63 hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
69 hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
74 /* Zero valued IP addresses cannot be stored */
76 hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
82 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *data)
84 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip))
93 hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
95 const struct hash_ip4_telem *tdata =
96 (const struct hash_ip4_telem *)data;
98 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
99 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
100 htonl(ip_set_timeout_get(tdata->timeout))))
101 goto nla_put_failure;
109 #define IP_SET_HASH_WITH_NETMASK
112 #include <linux/netfilter/ipset/ip_set_ahash.h>
115 hash_ip4_data_next(struct ip_set_hash *h, const struct hash_ip4_elem *d)
117 h->next.ip = ntohl(d->ip);
121 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
122 const struct xt_action_param *par,
123 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
125 const struct ip_set_hash *h = set->data;
126 ipset_adtfn adtfn = set->variant->adt[adt];
129 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
130 ip &= ip_set_netmask(h->netmask);
134 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
138 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
139 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
141 const struct ip_set_hash *h = set->data;
142 ipset_adtfn adtfn = set->variant->adt[adt];
143 u32 ip, ip_to, hosts, timeout = h->timeout;
147 if (unlikely(!tb[IPSET_ATTR_IP] ||
148 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
149 return -IPSET_ERR_PROTOCOL;
151 if (tb[IPSET_ATTR_LINENO])
152 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
154 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
158 ip &= ip_set_hostmask(h->netmask);
160 if (tb[IPSET_ATTR_TIMEOUT]) {
161 if (!with_timeout(h->timeout))
162 return -IPSET_ERR_TIMEOUT;
163 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
166 if (adt == IPSET_TEST) {
169 return -IPSET_ERR_HASH_ELEM;
170 return adtfn(set, &nip, timeout, flags);
173 if (tb[IPSET_ATTR_IP_TO]) {
174 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
179 } else if (tb[IPSET_ATTR_CIDR]) {
180 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
183 return -IPSET_ERR_INVALID_CIDR;
184 ip_set_mask_from_to(ip, ip_to, cidr);
188 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
192 for (; !before(ip_to, ip); ip += hosts) {
195 return -IPSET_ERR_HASH_ELEM;
196 ret = adtfn(set, &nip, timeout, flags);
198 if (ret && !ip_set_eexist(ret, flags))
207 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b)
209 const struct ip_set_hash *x = a->data;
210 const struct ip_set_hash *y = b->data;
212 /* Resizing changes htable_bits, so we ignore it */
213 return x->maxelem == y->maxelem &&
214 x->timeout == y->timeout &&
215 x->netmask == y->netmask;
218 /* The type variant functions: IPv6 */
220 struct hash_ip6_elem {
221 union nf_inet_addr ip;
224 struct hash_ip6_telem {
225 union nf_inet_addr ip;
226 unsigned long timeout;
230 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
231 const struct hash_ip6_elem *ip2,
234 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0;
238 hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
240 return ipv6_addr_any(&elem->ip.in6);
244 hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
246 dst->ip.in6 = src->ip.in6;
250 hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
252 ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
256 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
258 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
259 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
260 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
261 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
265 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *data)
267 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6))
268 goto nla_put_failure;
276 hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
278 const struct hash_ip6_telem *e =
279 (const struct hash_ip6_telem *)data;
281 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
282 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
283 htonl(ip_set_timeout_get(e->timeout))))
284 goto nla_put_failure;
295 #define HOST_MASK 128
296 #include <linux/netfilter/ipset/ip_set_ahash.h>
299 hash_ip6_data_next(struct ip_set_hash *h, const struct hash_ip6_elem *d)
304 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
305 const struct xt_action_param *par,
306 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
308 const struct ip_set_hash *h = set->data;
309 ipset_adtfn adtfn = set->variant->adt[adt];
310 union nf_inet_addr ip;
312 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip.in6);
313 ip6_netmask(&ip, h->netmask);
314 if (ipv6_addr_any(&ip.in6))
317 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
320 static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX + 1] = {
321 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
322 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
323 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
327 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
328 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
330 const struct ip_set_hash *h = set->data;
331 ipset_adtfn adtfn = set->variant->adt[adt];
332 union nf_inet_addr ip;
333 u32 timeout = h->timeout;
336 if (unlikely(!tb[IPSET_ATTR_IP] ||
337 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
338 tb[IPSET_ATTR_IP_TO] ||
339 tb[IPSET_ATTR_CIDR]))
340 return -IPSET_ERR_PROTOCOL;
342 if (tb[IPSET_ATTR_LINENO])
343 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
345 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &ip);
349 ip6_netmask(&ip, h->netmask);
350 if (ipv6_addr_any(&ip.in6))
351 return -IPSET_ERR_HASH_ELEM;
353 if (tb[IPSET_ATTR_TIMEOUT]) {
354 if (!with_timeout(h->timeout))
355 return -IPSET_ERR_TIMEOUT;
356 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
359 ret = adtfn(set, &ip, timeout, flags);
361 return ip_set_eexist(ret, flags) ? 0 : ret;
364 /* Create hash:ip type of sets */
367 hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
369 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
372 struct ip_set_hash *h;
374 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
375 return -IPSET_ERR_INVALID_FAMILY;
376 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
377 pr_debug("Create set %s with family %s\n",
378 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
380 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
381 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
382 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
383 return -IPSET_ERR_PROTOCOL;
385 if (tb[IPSET_ATTR_HASHSIZE]) {
386 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
387 if (hashsize < IPSET_MIMINAL_HASHSIZE)
388 hashsize = IPSET_MIMINAL_HASHSIZE;
391 if (tb[IPSET_ATTR_MAXELEM])
392 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
394 if (tb[IPSET_ATTR_NETMASK]) {
395 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
397 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
398 (set->family == NFPROTO_IPV6 && netmask > 128) ||
400 return -IPSET_ERR_INVALID_NETMASK;
403 h = kzalloc(sizeof(*h), GFP_KERNEL);
407 h->maxelem = maxelem;
408 h->netmask = netmask;
409 get_random_bytes(&h->initval, sizeof(h->initval));
410 h->timeout = IPSET_NO_TIMEOUT;
412 hbits = htable_bits(hashsize);
413 hsize = htable_size(hbits);
418 h->table = ip_set_alloc(hsize);
423 h->table->htable_bits = hbits;
427 if (tb[IPSET_ATTR_TIMEOUT]) {
428 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
430 set->variant = set->family == NFPROTO_IPV4
431 ? &hash_ip4_tvariant : &hash_ip6_tvariant;
433 if (set->family == NFPROTO_IPV4)
434 hash_ip4_gc_init(set);
436 hash_ip6_gc_init(set);
438 set->variant = set->family == NFPROTO_IPV4
439 ? &hash_ip4_variant : &hash_ip6_variant;
442 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
443 set->name, jhash_size(h->table->htable_bits),
444 h->table->htable_bits, h->maxelem, set->data, h->table);
449 static struct ip_set_type hash_ip_type __read_mostly = {
451 .protocol = IPSET_PROTOCOL,
452 .features = IPSET_TYPE_IP,
453 .dimension = IPSET_DIM_ONE,
454 .family = NFPROTO_UNSPEC,
457 .create = hash_ip_create,
459 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
460 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
461 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
462 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
463 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
464 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
467 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
468 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
469 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
470 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
471 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
479 return ip_set_type_register(&hash_ip_type);
485 ip_set_type_unregister(&hash_ip_type);
488 module_init(hash_ip_init);
489 module_exit(hash_ip_fini);