Merge remote-tracking branch 'regulator/topic/max8973' into regulator-next
[deliverable/linux.git] / net / netfilter / ipset / ip_set_hash_ip.c
CommitLineData
6c027889
JK
1/* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 *
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 */
7
8/* Kernel module implementing an IP set type: the hash:ip type */
9
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>
6c027889
JK
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>
20
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>
26
10111a6e
JK
27#define REVISION_MIN 0
28#define REVISION_MAX 0
29
6c027889
JK
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
10111a6e 32IP_SET_MODULE_DESC("hash:ip", REVISION_MIN, REVISION_MAX);
6c027889
JK
33MODULE_ALIAS("ip_set_hash:ip");
34
35/* Type specific function prefix */
36#define TYPE hash_ip
37
38static bool
39hash_ip_same_set(const struct ip_set *a, const struct ip_set *b);
40
41#define hash_ip4_same_set hash_ip_same_set
42#define hash_ip6_same_set hash_ip_same_set
43
44/* The type variant functions: IPv4 */
45
46/* Member elements without timeout */
47struct hash_ip4_elem {
48 __be32 ip;
49};
50
51/* Member elements with timeout support */
52struct hash_ip4_telem {
53 __be32 ip;
54 unsigned long timeout;
55};
56
57static inline bool
58hash_ip4_data_equal(const struct hash_ip4_elem *ip1,
89dc79b7
JK
59 const struct hash_ip4_elem *ip2,
60 u32 *multi)
6c027889
JK
61{
62 return ip1->ip == ip2->ip;
63}
64
65static inline bool
66hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
67{
68 return elem->ip == 0;
69}
70
71static inline void
72hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
73{
74 dst->ip = src->ip;
75}
76
77/* Zero valued IP addresses cannot be stored */
78static inline void
79hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
80{
81 elem->ip = 0;
82}
83
84static inline bool
85hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *data)
86{
7cf7899d
DM
87 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip))
88 goto nla_put_failure;
6c027889
JK
89 return 0;
90
91nla_put_failure:
92 return 1;
93}
94
95static bool
96hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
97{
98 const struct hash_ip4_telem *tdata =
99 (const struct hash_ip4_telem *)data;
100
7cf7899d
DM
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;
6c027889
JK
105
106 return 0;
107
108nla_put_failure:
109 return 1;
110}
111
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>
116
3d14b171
JK
117static inline void
118hash_ip4_data_next(struct ip_set_hash *h, const struct hash_ip4_elem *d)
119{
6e27c9b4 120 h->next.ip = d->ip;
3d14b171
JK
121}
122
6c027889
JK
123static int
124hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 125 const struct xt_action_param *par,
ac8cc925 126 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
6c027889
JK
127{
128 const struct ip_set_hash *h = set->data;
129 ipset_adtfn adtfn = set->variant->adt[adt];
130 __be32 ip;
131
ac8cc925 132 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
6c027889
JK
133 ip &= ip_set_netmask(h->netmask);
134 if (ip == 0)
135 return -EINVAL;
136
ac8cc925 137 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
6c027889
JK
138}
139
140static int
141hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 142 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
6c027889
JK
143{
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;
149
150 if (unlikely(!tb[IPSET_ATTR_IP] ||
151 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
152 return -IPSET_ERR_PROTOCOL;
153
154 if (tb[IPSET_ATTR_LINENO])
155 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
156
157 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
158 if (ret)
159 return ret;
160
161 ip &= ip_set_hostmask(h->netmask);
162
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]);
167 }
168
169 if (adt == IPSET_TEST) {
170 nip = htonl(ip);
171 if (nip == 0)
172 return -IPSET_ERR_HASH_ELEM;
5416219e 173 return adtfn(set, &nip, timeout, flags);
6c027889
JK
174 }
175
4fe198e6 176 ip_to = ip;
6c027889
JK
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]);
185
b9fed748 186 if (!cidr || cidr > 32)
6c027889 187 return -IPSET_ERR_INVALID_CIDR;
e6146e86 188 ip_set_mask_from_to(ip, ip_to, cidr);
4fe198e6 189 }
6c027889
JK
190
191 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
192
3d14b171 193 if (retried)
6e27c9b4 194 ip = ntohl(h->next.ip);
6c027889
JK
195 for (; !before(ip_to, ip); ip += hosts) {
196 nip = htonl(ip);
197 if (nip == 0)
198 return -IPSET_ERR_HASH_ELEM;
5416219e 199 ret = adtfn(set, &nip, timeout, flags);
6c027889
JK
200
201 if (ret && !ip_set_eexist(ret, flags))
202 return ret;
203 else
204 ret = 0;
205 }
206 return ret;
207}
208
209static bool
210hash_ip_same_set(const struct ip_set *a, const struct ip_set *b)
211{
212 const struct ip_set_hash *x = a->data;
213 const struct ip_set_hash *y = b->data;
214
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;
219}
220
221/* The type variant functions: IPv6 */
222
223struct hash_ip6_elem {
224 union nf_inet_addr ip;
225};
226
227struct hash_ip6_telem {
228 union nf_inet_addr ip;
229 unsigned long timeout;
230};
231
232static inline bool
233hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
89dc79b7
JK
234 const struct hash_ip6_elem *ip2,
235 u32 *multi)
6c027889
JK
236{
237 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0;
238}
239
240static inline bool
241hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
242{
243 return ipv6_addr_any(&elem->ip.in6);
244}
245
246static inline void
247hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
248{
4e3fd7a0 249 dst->ip.in6 = src->ip.in6;
6c027889
JK
250}
251
252static inline void
253hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
254{
255 ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
256}
257
258static inline void
259ip6_netmask(union nf_inet_addr *ip, u8 prefix)
260{
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];
265}
266
267static bool
268hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *data)
269{
7cf7899d
DM
270 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6))
271 goto nla_put_failure;
6c027889
JK
272 return 0;
273
274nla_put_failure:
275 return 1;
276}
277
278static bool
279hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
280{
281 const struct hash_ip6_telem *e =
282 (const struct hash_ip6_telem *)data;
283
7cf7899d
DM
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;
6c027889
JK
288 return 0;
289
290nla_put_failure:
291 return 1;
292}
293
294#undef PF
295#undef HOST_MASK
296
297#define PF 6
298#define HOST_MASK 128
299#include <linux/netfilter/ipset/ip_set_ahash.h>
300
3d14b171
JK
301static inline void
302hash_ip6_data_next(struct ip_set_hash *h, const struct hash_ip6_elem *d)
303{
304}
305
6c027889
JK
306static int
307hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 308 const struct xt_action_param *par,
ac8cc925 309 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
6c027889
JK
310{
311 const struct ip_set_hash *h = set->data;
312 ipset_adtfn adtfn = set->variant->adt[adt];
313 union nf_inet_addr ip;
314
ac8cc925 315 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip.in6);
6c027889
JK
316 ip6_netmask(&ip, h->netmask);
317 if (ipv6_addr_any(&ip.in6))
318 return -EINVAL;
319
ac8cc925 320 return adtfn(set, &ip, opt_timeout(opt, h), opt->cmdflags);
6c027889
JK
321}
322
323static 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 },
327};
328
329static int
330hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 331 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
6c027889
JK
332{
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;
338
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;
344
345 if (tb[IPSET_ATTR_LINENO])
346 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
347
348 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &ip);
349 if (ret)
350 return ret;
351
352 ip6_netmask(&ip, h->netmask);
353 if (ipv6_addr_any(&ip.in6))
354 return -IPSET_ERR_HASH_ELEM;
355
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]);
360 }
361
5416219e 362 ret = adtfn(set, &ip, timeout, flags);
6c027889
JK
363
364 return ip_set_eexist(ret, flags) ? 0 : ret;
365}
366
367/* Create hash:ip type of sets */
368
369static int
370hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
371{
372 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
373 u8 netmask, hbits;
26a5d3cc 374 size_t hsize;
6c027889
JK
375 struct ip_set_hash *h;
376
c15f1c83 377 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
6c027889 378 return -IPSET_ERR_INVALID_FAMILY;
c15f1c83 379 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
6c027889 380 pr_debug("Create set %s with family %s\n",
c15f1c83 381 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
6c027889
JK
382
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;
387
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;
392 }
393
394 if (tb[IPSET_ATTR_MAXELEM])
395 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
396
397 if (tb[IPSET_ATTR_NETMASK]) {
398 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
399
c15f1c83
JE
400 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
401 (set->family == NFPROTO_IPV6 && netmask > 128) ||
6c027889
JK
402 netmask == 0)
403 return -IPSET_ERR_INVALID_NETMASK;
404 }
405
406 h = kzalloc(sizeof(*h), GFP_KERNEL);
407 if (!h)
408 return -ENOMEM;
409
410 h->maxelem = maxelem;
411 h->netmask = netmask;
412 get_random_bytes(&h->initval, sizeof(h->initval));
413 h->timeout = IPSET_NO_TIMEOUT;
414
415 hbits = htable_bits(hashsize);
26a5d3cc
JK
416 hsize = htable_size(hbits);
417 if (hsize == 0) {
418 kfree(h);
419 return -ENOMEM;
420 }
421 h->table = ip_set_alloc(hsize);
6c027889
JK
422 if (!h->table) {
423 kfree(h);
424 return -ENOMEM;
425 }
426 h->table->htable_bits = hbits;
427
428 set->data = h;
429
430 if (tb[IPSET_ATTR_TIMEOUT]) {
431 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
432
c15f1c83 433 set->variant = set->family == NFPROTO_IPV4
6c027889
JK
434 ? &hash_ip4_tvariant : &hash_ip6_tvariant;
435
c15f1c83 436 if (set->family == NFPROTO_IPV4)
6c027889
JK
437 hash_ip4_gc_init(set);
438 else
439 hash_ip6_gc_init(set);
440 } else {
c15f1c83 441 set->variant = set->family == NFPROTO_IPV4
6c027889
JK
442 ? &hash_ip4_variant : &hash_ip6_variant;
443 }
444
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);
448
449 return 0;
450}
451
452static 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,
c15f1c83 457 .family = NFPROTO_UNSPEC,
10111a6e
JK
458 .revision_min = REVISION_MIN,
459 .revision_max = REVISION_MAX,
6c027889
JK
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 },
468 },
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 },
475 },
476 .me = THIS_MODULE,
477};
478
479static int __init
480hash_ip_init(void)
481{
482 return ip_set_type_register(&hash_ip_type);
483}
484
485static void __exit
486hash_ip_fini(void)
487{
488 ip_set_type_unregister(&hash_ip_type);
489}
490
491module_init(hash_ip_init);
492module_exit(hash_ip_fini);
This page took 0.137331 seconds and 5 git commands to generate.