netfilter: ipset: Fix coding styles reported by checkpatch.pl
[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 int
135 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
136 const struct xt_action_param *par,
137 enum ipset_adt adt, struct ip_set_adt_opt *opt)
138 {
139 const struct hash_netnet *h = set->data;
140 ipset_adtfn adtfn = set->variant->adt[adt];
141 struct hash_netnet4_elem e = { };
142 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
143
144 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
145 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
146 if (adt == IPSET_TEST)
147 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
148
149 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
150 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
151 e.ip[0] &= ip_set_netmask(e.cidr[0]);
152 e.ip[1] &= ip_set_netmask(e.cidr[1]);
153
154 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
155 }
156
157 static int
158 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
159 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
160 {
161 const struct hash_netnet *h = set->data;
162 ipset_adtfn adtfn = set->variant->adt[adt];
163 struct hash_netnet4_elem e = { .cidr = { HOST_MASK, HOST_MASK, }, };
164 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
165 u32 ip = 0, ip_to = 0, last;
166 u32 ip2 = 0, ip2_from = 0, ip2_to = 0, last2;
167 int ret;
168
169 if (tb[IPSET_ATTR_LINENO])
170 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
171
172 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
174 return -IPSET_ERR_PROTOCOL;
175
176 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
177 if (ret)
178 return ret;
179
180 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
181 if (ret)
182 return ret;
183
184 ret = ip_set_get_extensions(set, tb, &ext);
185 if (ret)
186 return ret;
187
188 if (tb[IPSET_ATTR_CIDR]) {
189 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
190 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
191 return -IPSET_ERR_INVALID_CIDR;
192 }
193
194 if (tb[IPSET_ATTR_CIDR2]) {
195 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
196 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
197 return -IPSET_ERR_INVALID_CIDR;
198 }
199
200 if (tb[IPSET_ATTR_CADT_FLAGS]) {
201 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
202
203 if (cadt_flags & IPSET_FLAG_NOMATCH)
204 flags |= (IPSET_FLAG_NOMATCH << 16);
205 }
206
207 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
208 tb[IPSET_ATTR_IP2_TO])) {
209 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
210 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
211 ret = adtfn(set, &e, &ext, &ext, flags);
212 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
213 ip_set_eexist(ret, flags) ? 0 : ret;
214 }
215
216 ip_to = ip;
217 if (tb[IPSET_ATTR_IP_TO]) {
218 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
219 if (ret)
220 return ret;
221 if (ip_to < ip)
222 swap(ip, ip_to);
223 if (unlikely(ip + UINT_MAX == ip_to))
224 return -IPSET_ERR_HASH_RANGE;
225 } else {
226 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
227 }
228
229 ip2_to = ip2_from;
230 if (tb[IPSET_ATTR_IP2_TO]) {
231 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
232 if (ret)
233 return ret;
234 if (ip2_to < ip2_from)
235 swap(ip2_from, ip2_to);
236 if (unlikely(ip2_from + UINT_MAX == ip2_to))
237 return -IPSET_ERR_HASH_RANGE;
238 } else {
239 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
240 }
241
242 if (retried)
243 ip = ntohl(h->next.ip[0]);
244
245 while (!after(ip, ip_to)) {
246 e.ip[0] = htonl(ip);
247 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
248 ip2 = (retried &&
249 ip == ntohl(h->next.ip[0])) ? ntohl(h->next.ip[1])
250 : ip2_from;
251 while (!after(ip2, ip2_to)) {
252 e.ip[1] = htonl(ip2);
253 last2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
254 ret = adtfn(set, &e, &ext, &ext, flags);
255 if (ret && !ip_set_eexist(ret, flags))
256 return ret;
257
258 ret = 0;
259 ip2 = last2 + 1;
260 }
261 ip = last + 1;
262 }
263 return ret;
264 }
265
266 /* IPv6 variants */
267
268 struct hash_netnet6_elem {
269 union nf_inet_addr ip[2];
270 u8 nomatch;
271 u8 padding;
272 union {
273 u8 cidr[2];
274 u16 ccmp;
275 };
276 };
277
278 /* Common functions */
279
280 static inline bool
281 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
282 const struct hash_netnet6_elem *ip2,
283 u32 *multi)
284 {
285 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
286 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
287 ip1->ccmp == ip2->ccmp;
288 }
289
290 static inline int
291 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
292 {
293 return elem->nomatch ? -ENOTEMPTY : 1;
294 }
295
296 static inline void
297 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
298 {
299 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
300 }
301
302 static inline void
303 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
304 {
305 swap(*flags, elem->nomatch);
306 }
307
308 static inline void
309 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
310 struct hash_netnet6_elem *orig)
311 {
312 elem->ip[1] = orig->ip[1];
313 }
314
315 static inline void
316 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
317 {
318 if (inner) {
319 ip6_netmask(&elem->ip[1], cidr);
320 elem->cidr[1] = cidr;
321 } else {
322 ip6_netmask(&elem->ip[0], cidr);
323 elem->cidr[0] = cidr;
324 }
325 }
326
327 static bool
328 hash_netnet6_data_list(struct sk_buff *skb,
329 const struct hash_netnet6_elem *data)
330 {
331 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
332
333 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
334 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
335 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
336 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
337 (flags &&
338 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
339 goto nla_put_failure;
340 return false;
341
342 nla_put_failure:
343 return true;
344 }
345
346 static inline void
347 hash_netnet6_data_next(struct hash_netnet4_elem *next,
348 const struct hash_netnet6_elem *d)
349 {
350 }
351
352 #undef MTYPE
353 #undef HOST_MASK
354
355 #define MTYPE hash_netnet6
356 #define HOST_MASK 128
357 #define IP_SET_EMIT_CREATE
358 #include "ip_set_hash_gen.h"
359
360 static int
361 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
362 const struct xt_action_param *par,
363 enum ipset_adt adt, struct ip_set_adt_opt *opt)
364 {
365 const struct hash_netnet *h = set->data;
366 ipset_adtfn adtfn = set->variant->adt[adt];
367 struct hash_netnet6_elem e = { };
368 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
369
370 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
371 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
372 if (adt == IPSET_TEST)
373 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
374
375 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
376 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
377 ip6_netmask(&e.ip[0], e.cidr[0]);
378 ip6_netmask(&e.ip[1], e.cidr[1]);
379
380 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
381 }
382
383 static int
384 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
385 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
386 {
387 ipset_adtfn adtfn = set->variant->adt[adt];
388 struct hash_netnet6_elem e = { .cidr = { HOST_MASK, HOST_MASK, }, };
389 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
390 int ret;
391
392 if (tb[IPSET_ATTR_LINENO])
393 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
394
395 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
396 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
397 return -IPSET_ERR_PROTOCOL;
398 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
399 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
400
401 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
402 if (ret)
403 return ret;
404
405 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
406 if (ret)
407 return ret;
408
409 ret = ip_set_get_extensions(set, tb, &ext);
410 if (ret)
411 return ret;
412
413 if (tb[IPSET_ATTR_CIDR]) {
414 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
415 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
416 return -IPSET_ERR_INVALID_CIDR;
417 }
418
419 if (tb[IPSET_ATTR_CIDR2]) {
420 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
421 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
422 return -IPSET_ERR_INVALID_CIDR;
423 }
424
425 ip6_netmask(&e.ip[0], e.cidr[0]);
426 ip6_netmask(&e.ip[1], e.cidr[1]);
427
428 if (tb[IPSET_ATTR_CADT_FLAGS]) {
429 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
430
431 if (cadt_flags & IPSET_FLAG_NOMATCH)
432 flags |= (IPSET_FLAG_NOMATCH << 16);
433 }
434
435 ret = adtfn(set, &e, &ext, &ext, flags);
436
437 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
438 ip_set_eexist(ret, flags) ? 0 : ret;
439 }
440
441 static struct ip_set_type hash_netnet_type __read_mostly = {
442 .name = "hash:net,net",
443 .protocol = IPSET_PROTOCOL,
444 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
445 .dimension = IPSET_DIM_TWO,
446 .family = NFPROTO_UNSPEC,
447 .revision_min = IPSET_TYPE_REV_MIN,
448 .revision_max = IPSET_TYPE_REV_MAX,
449 .create = hash_netnet_create,
450 .create_policy = {
451 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
452 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
453 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
454 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
455 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
456 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
457 },
458 .adt_policy = {
459 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
460 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
461 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
462 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
463 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
464 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
465 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
466 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
467 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
468 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
469 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
470 .len = IPSET_MAX_COMMENT_SIZE },
471 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
472 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
473 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
474 },
475 .me = THIS_MODULE,
476 };
477
478 static int __init
479 hash_netnet_init(void)
480 {
481 return ip_set_type_register(&hash_netnet_type);
482 }
483
484 static void __exit
485 hash_netnet_fini(void)
486 {
487 rcu_barrier();
488 ip_set_type_unregister(&hash_netnet_type);
489 }
490
491 module_init(hash_netnet_init);
492 module_exit(hash_netnet_fini);
This page took 0.040378 seconds and 5 git commands to generate.