iommu/AMD: Per-thread IOMMU Interrupt Handling
[linux-2.6/btrfs-unstable.git] / net / netfilter / ipset / ip_set_hash_ip.c
blobb7d4cb475ae69ab088e33eef7589d06da81851e0
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.
6 */
8 /* Kernel module implementing an IP set type: the hash:ip type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.h>
19 #include <net/tcp.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 */
36 #define TYPE hash_ip
38 static bool
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 {
48 __be32 ip;
51 /* Member elements with timeout support */
52 struct hash_ip4_telem {
53 __be32 ip;
54 unsigned long timeout;
57 static inline bool
58 hash_ip4_data_equal(const struct hash_ip4_elem *ip1,
59 const struct hash_ip4_elem *ip2,
60 u32 *multi)
62 return ip1->ip == ip2->ip;
65 static inline bool
66 hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
68 return elem->ip == 0;
71 static inline void
72 hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
74 dst->ip = src->ip;
77 /* Zero valued IP addresses cannot be stored */
78 static inline void
79 hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
81 elem->ip = 0;
84 static inline bool
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))
88 goto nla_put_failure;
89 return 0;
91 nla_put_failure:
92 return 1;
95 static bool
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;
106 return 0;
108 nla_put_failure:
109 return 1;
112 #define IP_SET_HASH_WITH_NETMASK
113 #define PF 4
114 #define HOST_MASK 32
115 #include <linux/netfilter/ipset/ip_set_ahash.h>
117 static inline void
118 hash_ip4_data_next(struct ip_set_hash *h, const struct hash_ip4_elem *d)
120 h->next.ip = d->ip;
123 static int
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];
130 __be32 ip;
132 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
133 ip &= ip_set_netmask(h->netmask);
134 if (ip == 0)
135 return -EINVAL;
137 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
140 static int
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;
147 __be32 nip;
148 int ret = 0;
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);
158 if (ret)
159 return ret;
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) {
170 nip = htonl(ip);
171 if (nip == 0)
172 return -IPSET_ERR_HASH_ELEM;
173 return adtfn(set, &nip, timeout, flags);
176 ip_to = ip;
177 if (tb[IPSET_ATTR_IP_TO]) {
178 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
179 if (ret)
180 return ret;
181 if (ip > ip_to)
182 swap(ip, 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);
193 if (retried)
194 ip = ntohl(h->next.ip);
195 for (; !before(ip_to, ip); ip += hosts) {
196 nip = htonl(ip);
197 if (nip == 0)
198 return -IPSET_ERR_HASH_ELEM;
199 ret = adtfn(set, &nip, timeout, flags);
201 if (ret && !ip_set_eexist(ret, flags))
202 return ret;
203 else
204 ret = 0;
206 return ret;
209 static bool
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;
232 static inline bool
233 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
234 const struct hash_ip6_elem *ip2,
235 u32 *multi)
237 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
240 static inline bool
241 hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
243 return ipv6_addr_any(&elem->ip.in6);
246 static inline void
247 hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
249 dst->ip.in6 = src->ip.in6;
252 static inline void
253 hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
255 ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
258 static inline void
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];
267 static bool
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;
272 return 0;
274 nla_put_failure:
275 return 1;
278 static bool
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;
288 return 0;
290 nla_put_failure:
291 return 1;
294 #undef PF
295 #undef HOST_MASK
297 #define PF 6
298 #define HOST_MASK 128
299 #include <linux/netfilter/ipset/ip_set_ahash.h>
301 static inline void
302 hash_ip6_data_next(struct ip_set_hash *h, const struct hash_ip6_elem *d)
306 static int
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))
318 return -EINVAL;
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 },
329 static int
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;
337 int ret;
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);
349 if (ret)
350 return ret;
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 */
369 static int
370 hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
372 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
373 u8 netmask, hbits;
374 size_t hsize;
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) ||
402 netmask == 0)
403 return -IPSET_ERR_INVALID_NETMASK;
406 h = kzalloc(sizeof(*h), GFP_KERNEL);
407 if (!h)
408 return -ENOMEM;
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);
417 if (hsize == 0) {
418 kfree(h);
419 return -ENOMEM;
421 h->table = ip_set_alloc(hsize);
422 if (!h->table) {
423 kfree(h);
424 return -ENOMEM;
426 h->table->htable_bits = hbits;
428 set->data = h;
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);
438 else
439 hash_ip6_gc_init(set);
440 } else {
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);
449 return 0;
452 static struct ip_set_type hash_ip_type __read_mostly = {
453 .name = "hash:ip",
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,
461 .create_policy = {
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 },
469 .adt_policy = {
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 },
476 .me = THIS_MODULE,
479 static int __init
480 hash_ip_init(void)
482 return ip_set_type_register(&hash_ip_type);
485 static void __exit
486 hash_ip_fini(void)
488 ip_set_type_unregister(&hash_ip_type);
491 module_init(hash_ip_init);
492 module_exit(hash_ip_fini);