Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / net / netfilter / ipset / ip_set_hash_netnet.c
CommitLineData
ea53ac5b
OS
1/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9/* Kernel module implementing an IP set type: the hash:net type */
10
11#include <linux/jhash.h>
12#include <linux/module.h>
13#include <linux/ip.h>
14#include <linux/skbuff.h>
15#include <linux/errno.h>
16#include <linux/random.h>
17#include <net/ip.h>
18#include <net/ipv6.h>
19#include <net/netlink.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_hash.h>
25
26#define IPSET_TYPE_REV_MIN 0
af331419
AD
27/* 1 Forceadd support added */
28#define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
ea53ac5b
OS
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
32IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33MODULE_ALIAS("ip_set_hash:net,net");
34
35/* Type specific function prefix */
36#define HTYPE hash_netnet
37#define IP_SET_HASH_WITH_NETS
38#define IPSET_NET_COUNT 2
39
40/* IPv4 variants */
41
42/* Member elements */
43struct hash_netnet4_elem {
44 union {
45 __be32 ip[2];
46 __be64 ipcmp;
47 };
48 u8 nomatch;
cac37639 49 u8 padding;
ea53ac5b
OS
50 union {
51 u8 cidr[2];
52 u16 ccmp;
53 };
54};
55
56/* Common functions */
57
58static inline bool
59hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
ca0f6a5c
JK
60 const struct hash_netnet4_elem *ip2,
61 u32 *multi)
ea53ac5b
OS
62{
63 return ip1->ipcmp == ip2->ipcmp &&
b49faea7 64 ip1->ccmp == ip2->ccmp;
ea53ac5b
OS
65}
66
67static inline int
68hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
69{
70 return elem->nomatch ? -ENOTEMPTY : 1;
71}
72
73static inline void
74hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
75{
76 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
77}
78
79static inline void
80hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
81{
82 swap(*flags, elem->nomatch);
83}
84
85static inline void
86hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
ca0f6a5c 87 struct hash_netnet4_elem *orig)
ea53ac5b
OS
88{
89 elem->ip[1] = orig->ip[1];
90}
91
92static inline void
93hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
94{
95 if (inner) {
96 elem->ip[1] &= ip_set_netmask(cidr);
97 elem->cidr[1] = cidr;
98 } else {
99 elem->ip[0] &= ip_set_netmask(cidr);
100 elem->cidr[0] = cidr;
101 }
102}
103
104static bool
105hash_netnet4_data_list(struct sk_buff *skb,
ca0f6a5c 106 const struct hash_netnet4_elem *data)
ea53ac5b
OS
107{
108 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
109
110 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
111 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
112 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
113 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
114 (flags &&
115 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
116 goto nla_put_failure;
9562cf28 117 return false;
ea53ac5b
OS
118
119nla_put_failure:
9562cf28 120 return true;
ea53ac5b
OS
121}
122
123static inline void
124hash_netnet4_data_next(struct hash_netnet4_elem *next,
ca0f6a5c 125 const struct hash_netnet4_elem *d)
ea53ac5b
OS
126{
127 next->ipcmp = d->ipcmp;
128}
129
130#define MTYPE hash_netnet4
ea53ac5b
OS
131#define HOST_MASK 32
132#include "ip_set_hash_gen.h"
133
96be5f28
ER
134static void
135hash_netnet4_init(struct hash_netnet4_elem *e)
136{
137 e->cidr[0] = HOST_MASK;
138 e->cidr[1] = HOST_MASK;
139}
140
ea53ac5b
OS
141static int
142hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
ca0f6a5c
JK
143 const struct xt_action_param *par,
144 enum ipset_adt adt, struct ip_set_adt_opt *opt)
ea53ac5b
OS
145{
146 const struct hash_netnet *h = set->data;
147 ipset_adtfn adtfn = set->variant->adt[adt];
1a869205 148 struct hash_netnet4_elem e = { };
ea53ac5b
OS
149 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
150
f690cbae
JK
151 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
152 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
ea53ac5b
OS
153 if (adt == IPSET_TEST)
154 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
155
156 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
157 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
158 e.ip[0] &= ip_set_netmask(e.cidr[0]);
159 e.ip[1] &= ip_set_netmask(e.cidr[1]);
160
161 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
162}
163
164static int
165hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
ca0f6a5c 166 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
ea53ac5b
OS
167{
168 const struct hash_netnet *h = set->data;
169 ipset_adtfn adtfn = set->variant->adt[adt];
96be5f28 170 struct hash_netnet4_elem e = { };
ea53ac5b
OS
171 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
172 u32 ip = 0, ip_to = 0, last;
173 u32 ip2 = 0, ip2_from = 0, ip2_to = 0, last2;
ea53ac5b
OS
174 int ret;
175
a212e08e
SP
176 if (tb[IPSET_ATTR_LINENO])
177 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
178
96be5f28 179 hash_netnet4_init(&e);
ea53ac5b 180 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
7dd37bc8 181 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
ea53ac5b
OS
182 return -IPSET_ERR_PROTOCOL;
183
8e55d2e5
SP
184 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
185 if (ret)
186 return ret;
187
188 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
189 if (ret)
190 return ret;
191
192 ret = ip_set_get_extensions(set, tb, &ext);
ea53ac5b
OS
193 if (ret)
194 return ret;
195
196 if (tb[IPSET_ATTR_CIDR]) {
aff22758
SP
197 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
198 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
ea53ac5b 199 return -IPSET_ERR_INVALID_CIDR;
ea53ac5b
OS
200 }
201
202 if (tb[IPSET_ATTR_CIDR2]) {
aff22758
SP
203 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
204 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
ea53ac5b 205 return -IPSET_ERR_INVALID_CIDR;
ea53ac5b
OS
206 }
207
208 if (tb[IPSET_ATTR_CADT_FLAGS]) {
209 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
ca0f6a5c 210
ea53ac5b
OS
211 if (cadt_flags & IPSET_FLAG_NOMATCH)
212 flags |= (IPSET_FLAG_NOMATCH << 16);
213 }
214
6e41ee68 215 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
ea53ac5b
OS
216 tb[IPSET_ATTR_IP2_TO])) {
217 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
218 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
219 ret = adtfn(set, &e, &ext, &ext, flags);
220 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
221 ip_set_eexist(ret, flags) ? 0 : ret;
222 }
223
224 ip_to = ip;
225 if (tb[IPSET_ATTR_IP_TO]) {
226 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
227 if (ret)
228 return ret;
229 if (ip_to < ip)
230 swap(ip, ip_to);
6e41ee68 231 if (unlikely(ip + UINT_MAX == ip_to))
ea53ac5b 232 return -IPSET_ERR_HASH_RANGE;
ca0f6a5c 233 } else {
6e41ee68 234 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
ca0f6a5c 235 }
ea53ac5b
OS
236
237 ip2_to = ip2_from;
238 if (tb[IPSET_ATTR_IP2_TO]) {
239 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
240 if (ret)
241 return ret;
242 if (ip2_to < ip2_from)
243 swap(ip2_from, ip2_to);
6e41ee68 244 if (unlikely(ip2_from + UINT_MAX == ip2_to))
ea53ac5b 245 return -IPSET_ERR_HASH_RANGE;
ca0f6a5c 246 } else {
6e41ee68 247 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
ca0f6a5c 248 }
ea53ac5b
OS
249
250 if (retried)
251 ip = ntohl(h->next.ip[0]);
252
253 while (!after(ip, ip_to)) {
254 e.ip[0] = htonl(ip);
aff22758 255 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
ea53ac5b
OS
256 ip2 = (retried &&
257 ip == ntohl(h->next.ip[0])) ? ntohl(h->next.ip[1])
258 : ip2_from;
259 while (!after(ip2, ip2_to)) {
260 e.ip[1] = htonl(ip2);
aff22758 261 last2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
ea53ac5b
OS
262 ret = adtfn(set, &e, &ext, &ext, flags);
263 if (ret && !ip_set_eexist(ret, flags))
264 return ret;
ca0f6a5c
JK
265
266 ret = 0;
ea53ac5b
OS
267 ip2 = last2 + 1;
268 }
269 ip = last + 1;
270 }
271 return ret;
272}
273
274/* IPv6 variants */
275
276struct hash_netnet6_elem {
277 union nf_inet_addr ip[2];
278 u8 nomatch;
cac37639 279 u8 padding;
ea53ac5b
OS
280 union {
281 u8 cidr[2];
282 u16 ccmp;
283 };
284};
285
286/* Common functions */
287
288static inline bool
289hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
ca0f6a5c
JK
290 const struct hash_netnet6_elem *ip2,
291 u32 *multi)
ea53ac5b
OS
292{
293 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
294 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
295 ip1->ccmp == ip2->ccmp;
296}
297
298static inline int
299hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
300{
301 return elem->nomatch ? -ENOTEMPTY : 1;
302}
303
304static inline void
305hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
306{
307 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
308}
309
310static inline void
311hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
312{
313 swap(*flags, elem->nomatch);
314}
315
316static inline void
317hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
ca0f6a5c 318 struct hash_netnet6_elem *orig)
ea53ac5b
OS
319{
320 elem->ip[1] = orig->ip[1];
321}
322
323static inline void
324hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
325{
326 if (inner) {
327 ip6_netmask(&elem->ip[1], cidr);
328 elem->cidr[1] = cidr;
329 } else {
330 ip6_netmask(&elem->ip[0], cidr);
331 elem->cidr[0] = cidr;
332 }
333}
334
335static bool
336hash_netnet6_data_list(struct sk_buff *skb,
ca0f6a5c 337 const struct hash_netnet6_elem *data)
ea53ac5b
OS
338{
339 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
340
341 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
342 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
343 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
344 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
345 (flags &&
346 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
347 goto nla_put_failure;
9562cf28 348 return false;
ea53ac5b
OS
349
350nla_put_failure:
9562cf28 351 return true;
ea53ac5b
OS
352}
353
354static inline void
355hash_netnet6_data_next(struct hash_netnet4_elem *next,
ca0f6a5c 356 const struct hash_netnet6_elem *d)
ea53ac5b
OS
357{
358}
359
360#undef MTYPE
ea53ac5b
OS
361#undef HOST_MASK
362
363#define MTYPE hash_netnet6
ea53ac5b
OS
364#define HOST_MASK 128
365#define IP_SET_EMIT_CREATE
366#include "ip_set_hash_gen.h"
367
96be5f28
ER
368static void
369hash_netnet6_init(struct hash_netnet6_elem *e)
370{
371 e->cidr[0] = HOST_MASK;
372 e->cidr[1] = HOST_MASK;
373}
374
ea53ac5b
OS
375static int
376hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
ca0f6a5c
JK
377 const struct xt_action_param *par,
378 enum ipset_adt adt, struct ip_set_adt_opt *opt)
ea53ac5b
OS
379{
380 const struct hash_netnet *h = set->data;
381 ipset_adtfn adtfn = set->variant->adt[adt];
1a869205 382 struct hash_netnet6_elem e = { };
ea53ac5b
OS
383 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
384
f690cbae
JK
385 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
386 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
ea53ac5b 387 if (adt == IPSET_TEST)
ca0f6a5c 388 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
ea53ac5b
OS
389
390 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
391 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
392 ip6_netmask(&e.ip[0], e.cidr[0]);
393 ip6_netmask(&e.ip[1], e.cidr[1]);
394
395 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
396}
397
398static int
399hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
ca0f6a5c 400 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
ea53ac5b
OS
401{
402 ipset_adtfn adtfn = set->variant->adt[adt];
96be5f28 403 struct hash_netnet6_elem e = { };
ea53ac5b
OS
404 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
405 int ret;
406
a212e08e
SP
407 if (tb[IPSET_ATTR_LINENO])
408 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
409
96be5f28 410 hash_netnet6_init(&e);
ea53ac5b 411 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
7dd37bc8 412 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
ea53ac5b
OS
413 return -IPSET_ERR_PROTOCOL;
414 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
415 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
416
8e55d2e5
SP
417 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
418 if (ret)
419 return ret;
420
421 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
422 if (ret)
423 return ret;
424
425 ret = ip_set_get_extensions(set, tb, &ext);
ea53ac5b
OS
426 if (ret)
427 return ret;
428
aff22758 429 if (tb[IPSET_ATTR_CIDR]) {
ea53ac5b 430 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
aff22758
SP
431 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
432 return -IPSET_ERR_INVALID_CIDR;
433 }
ea53ac5b 434
aff22758 435 if (tb[IPSET_ATTR_CIDR2]) {
ea53ac5b 436 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
aff22758
SP
437 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
438 return -IPSET_ERR_INVALID_CIDR;
439 }
ea53ac5b
OS
440
441 ip6_netmask(&e.ip[0], e.cidr[0]);
442 ip6_netmask(&e.ip[1], e.cidr[1]);
443
444 if (tb[IPSET_ATTR_CADT_FLAGS]) {
445 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
ca0f6a5c 446
ea53ac5b
OS
447 if (cadt_flags & IPSET_FLAG_NOMATCH)
448 flags |= (IPSET_FLAG_NOMATCH << 16);
449 }
450
451 ret = adtfn(set, &e, &ext, &ext, flags);
452
453 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
454 ip_set_eexist(ret, flags) ? 0 : ret;
455}
456
457static struct ip_set_type hash_netnet_type __read_mostly = {
458 .name = "hash:net,net",
459 .protocol = IPSET_PROTOCOL,
460 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
461 .dimension = IPSET_DIM_TWO,
462 .family = NFPROTO_UNSPEC,
463 .revision_min = IPSET_TYPE_REV_MIN,
464 .revision_max = IPSET_TYPE_REV_MAX,
465 .create = hash_netnet_create,
466 .create_policy = {
467 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
468 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
469 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
470 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
471 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
472 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
473 },
474 .adt_policy = {
475 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
476 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
477 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
478 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
479 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
480 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
481 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
482 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
483 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
484 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
03726186
SP
485 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
486 .len = IPSET_MAX_COMMENT_SIZE },
af331419
AD
487 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
488 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
489 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
ea53ac5b
OS
490 },
491 .me = THIS_MODULE,
492};
493
494static int __init
495hash_netnet_init(void)
496{
497 return ip_set_type_register(&hash_netnet_type);
498}
499
500static void __exit
501hash_netnet_fini(void)
502{
18f84d41 503 rcu_barrier();
ea53ac5b
OS
504 ip_set_type_unregister(&hash_netnet_type);
505}
506
507module_init(hash_netnet_init);
508module_exit(hash_netnet_fini);