Merge remote-tracking branches 'regulator/topic/load', 'regulator/topic/max77802...
[deliverable/linux.git] / net / netfilter / ipset / ip_set_hash_netnet.c
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
27 /* 1 Forceadd support added */
28 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
32 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_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 */
43 struct hash_netnet4_elem {
44 union {
45 __be32 ip[2];
46 __be64 ipcmp;
47 };
48 u8 nomatch;
49 u8 padding;
50 union {
51 u8 cidr[2];
52 u16 ccmp;
53 };
54 };
55
56 /* Common functions */
57
58 static inline bool
59 hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
60 const struct hash_netnet4_elem *ip2,
61 u32 *multi)
62 {
63 return ip1->ipcmp == ip2->ipcmp &&
64 ip1->ccmp == ip2->ccmp;
65 }
66
67 static inline int
68 hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
69 {
70 return elem->nomatch ? -ENOTEMPTY : 1;
71 }
72
73 static inline void
74 hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
75 {
76 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
77 }
78
79 static inline void
80 hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
81 {
82 swap(*flags, elem->nomatch);
83 }
84
85 static inline void
86 hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
87 struct hash_netnet4_elem *orig)
88 {
89 elem->ip[1] = orig->ip[1];
90 }
91
92 static inline void
93 hash_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
104 static bool
105 hash_netnet4_data_list(struct sk_buff *skb,
106 const struct hash_netnet4_elem *data)
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;
117 return false;
118
119 nla_put_failure:
120 return true;
121 }
122
123 static inline void
124 hash_netnet4_data_next(struct hash_netnet4_elem *next,
125 const struct hash_netnet4_elem *d)
126 {
127 next->ipcmp = d->ipcmp;
128 }
129
130 #define MTYPE hash_netnet4
131 #define HOST_MASK 32
132 #include "ip_set_hash_gen.h"
133
134 static void
135 hash_netnet4_init(struct hash_netnet4_elem *e)
136 {
137 e->cidr[0] = HOST_MASK;
138 e->cidr[1] = HOST_MASK;
139 }
140
141 static int
142 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
143 const struct xt_action_param *par,
144 enum ipset_adt adt, struct ip_set_adt_opt *opt)
145 {
146 const struct hash_netnet *h = set->data;
147 ipset_adtfn adtfn = set->variant->adt[adt];
148 struct hash_netnet4_elem e = { };
149 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
150
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);
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
164 static int
165 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
166 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
167 {
168 const struct hash_netnet *h = set->data;
169 ipset_adtfn adtfn = set->variant->adt[adt];
170 struct hash_netnet4_elem e = { };
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;
174 int ret;
175
176 if (tb[IPSET_ATTR_LINENO])
177 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
178
179 hash_netnet4_init(&e);
180 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
181 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
182 return -IPSET_ERR_PROTOCOL;
183
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);
193 if (ret)
194 return ret;
195
196 if (tb[IPSET_ATTR_CIDR]) {
197 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
198 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
199 return -IPSET_ERR_INVALID_CIDR;
200 }
201
202 if (tb[IPSET_ATTR_CIDR2]) {
203 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
204 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
205 return -IPSET_ERR_INVALID_CIDR;
206 }
207
208 if (tb[IPSET_ATTR_CADT_FLAGS]) {
209 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
210
211 if (cadt_flags & IPSET_FLAG_NOMATCH)
212 flags |= (IPSET_FLAG_NOMATCH << 16);
213 }
214
215 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
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);
231 if (unlikely(ip + UINT_MAX == ip_to))
232 return -IPSET_ERR_HASH_RANGE;
233 } else {
234 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
235 }
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);
244 if (unlikely(ip2_from + UINT_MAX == ip2_to))
245 return -IPSET_ERR_HASH_RANGE;
246 } else {
247 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
248 }
249
250 if (retried)
251 ip = ntohl(h->next.ip[0]);
252
253 while (!after(ip, ip_to)) {
254 e.ip[0] = htonl(ip);
255 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
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);
261 last2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
262 ret = adtfn(set, &e, &ext, &ext, flags);
263 if (ret && !ip_set_eexist(ret, flags))
264 return ret;
265
266 ret = 0;
267 ip2 = last2 + 1;
268 }
269 ip = last + 1;
270 }
271 return ret;
272 }
273
274 /* IPv6 variants */
275
276 struct hash_netnet6_elem {
277 union nf_inet_addr ip[2];
278 u8 nomatch;
279 u8 padding;
280 union {
281 u8 cidr[2];
282 u16 ccmp;
283 };
284 };
285
286 /* Common functions */
287
288 static inline bool
289 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
290 const struct hash_netnet6_elem *ip2,
291 u32 *multi)
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
298 static inline int
299 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
300 {
301 return elem->nomatch ? -ENOTEMPTY : 1;
302 }
303
304 static inline void
305 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
306 {
307 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
308 }
309
310 static inline void
311 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
312 {
313 swap(*flags, elem->nomatch);
314 }
315
316 static inline void
317 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
318 struct hash_netnet6_elem *orig)
319 {
320 elem->ip[1] = orig->ip[1];
321 }
322
323 static inline void
324 hash_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
335 static bool
336 hash_netnet6_data_list(struct sk_buff *skb,
337 const struct hash_netnet6_elem *data)
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;
348 return false;
349
350 nla_put_failure:
351 return true;
352 }
353
354 static inline void
355 hash_netnet6_data_next(struct hash_netnet4_elem *next,
356 const struct hash_netnet6_elem *d)
357 {
358 }
359
360 #undef MTYPE
361 #undef HOST_MASK
362
363 #define MTYPE hash_netnet6
364 #define HOST_MASK 128
365 #define IP_SET_EMIT_CREATE
366 #include "ip_set_hash_gen.h"
367
368 static void
369 hash_netnet6_init(struct hash_netnet6_elem *e)
370 {
371 e->cidr[0] = HOST_MASK;
372 e->cidr[1] = HOST_MASK;
373 }
374
375 static int
376 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
377 const struct xt_action_param *par,
378 enum ipset_adt adt, struct ip_set_adt_opt *opt)
379 {
380 const struct hash_netnet *h = set->data;
381 ipset_adtfn adtfn = set->variant->adt[adt];
382 struct hash_netnet6_elem e = { };
383 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
384
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);
387 if (adt == IPSET_TEST)
388 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
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
398 static int
399 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
400 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
401 {
402 ipset_adtfn adtfn = set->variant->adt[adt];
403 struct hash_netnet6_elem e = { };
404 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
405 int ret;
406
407 if (tb[IPSET_ATTR_LINENO])
408 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
409
410 hash_netnet6_init(&e);
411 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
412 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
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
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);
426 if (ret)
427 return ret;
428
429 if (tb[IPSET_ATTR_CIDR]) {
430 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
431 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
432 return -IPSET_ERR_INVALID_CIDR;
433 }
434
435 if (tb[IPSET_ATTR_CIDR2]) {
436 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
437 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
438 return -IPSET_ERR_INVALID_CIDR;
439 }
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]);
446
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
457 static 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 },
485 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
486 .len = IPSET_MAX_COMMENT_SIZE },
487 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
488 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
489 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
490 },
491 .me = THIS_MODULE,
492 };
493
494 static int __init
495 hash_netnet_init(void)
496 {
497 return ip_set_type_register(&hash_netnet_type);
498 }
499
500 static void __exit
501 hash_netnet_fini(void)
502 {
503 rcu_barrier();
504 ip_set_type_unregister(&hash_netnet_type);
505 }
506
507 module_init(hash_netnet_init);
508 module_exit(hash_netnet_fini);
This page took 0.041636 seconds and 6 git commands to generate.