at86rf230: mask irq's before deregister device
[deliverable/linux.git] / net / netfilter / ipset / ip_set_hash_netport.c
1 /* Copyright (C) 2003-2013 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:net,port 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>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_getport.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 #define IPSET_TYPE_REV_MIN 0
27 /* 1 SCTP and UDPLITE support added */
28 /* 2 Range as input support for IPv4 added */
29 /* 3 nomatch flag support added */
30 /* 4 Counters support added */
31 #define IPSET_TYPE_REV_MAX 5 /* Comments support added */
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
36 MODULE_ALIAS("ip_set_hash:net,port");
37
38 /* Type specific function prefix */
39 #define HTYPE hash_netport
40 #define IP_SET_HASH_WITH_PROTO
41 #define IP_SET_HASH_WITH_NETS
42
43 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
44 * However this way we have to store internally cidr - 1,
45 * dancing back and forth.
46 */
47 #define IP_SET_HASH_WITH_NETS_PACKED
48
49 /* IPv4 variant */
50
51 /* Member elements */
52 struct hash_netport4_elem {
53 __be32 ip;
54 __be16 port;
55 u8 proto;
56 u8 cidr:7;
57 u8 nomatch:1;
58 };
59
60 /* Common functions */
61
62 static inline bool
63 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
64 const struct hash_netport4_elem *ip2,
65 u32 *multi)
66 {
67 return ip1->ip == ip2->ip &&
68 ip1->port == ip2->port &&
69 ip1->proto == ip2->proto &&
70 ip1->cidr == ip2->cidr;
71 }
72
73 static inline int
74 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
75 {
76 return elem->nomatch ? -ENOTEMPTY : 1;
77 }
78
79 static inline void
80 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
81 {
82 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
83 }
84
85 static inline void
86 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
87 {
88 swap(*flags, elem->nomatch);
89 }
90
91 static inline void
92 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
93 {
94 elem->ip &= ip_set_netmask(cidr);
95 elem->cidr = cidr - 1;
96 }
97
98 static bool
99 hash_netport4_data_list(struct sk_buff *skb,
100 const struct hash_netport4_elem *data)
101 {
102 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
103
104 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
105 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
106 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
107 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
108 (flags &&
109 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
110 goto nla_put_failure;
111 return 0;
112
113 nla_put_failure:
114 return 1;
115 }
116
117 static inline void
118 hash_netport4_data_next(struct hash_netport4_elem *next,
119 const struct hash_netport4_elem *d)
120 {
121 next->ip = d->ip;
122 next->port = d->port;
123 }
124
125 #define MTYPE hash_netport4
126 #define PF 4
127 #define HOST_MASK 32
128 #include "ip_set_hash_gen.h"
129
130 static int
131 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
132 const struct xt_action_param *par,
133 enum ipset_adt adt, struct ip_set_adt_opt *opt)
134 {
135 const struct hash_netport *h = set->data;
136 ipset_adtfn adtfn = set->variant->adt[adt];
137 struct hash_netport4_elem e = {
138 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
139 };
140 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
141
142 if (adt == IPSET_TEST)
143 e.cidr = HOST_MASK - 1;
144
145 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
146 &e.port, &e.proto))
147 return -EINVAL;
148
149 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
150 e.ip &= ip_set_netmask(e.cidr + 1);
151
152 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
153 }
154
155 static int
156 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
157 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
158 {
159 const struct hash_netport *h = set->data;
160 ipset_adtfn adtfn = set->variant->adt[adt];
161 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
162 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
163 u32 port, port_to, p = 0, ip = 0, ip_to = 0, last;
164 bool with_ports = false;
165 u8 cidr;
166 int ret;
167
168 if (unlikely(!tb[IPSET_ATTR_IP] ||
169 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
170 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
171 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
172 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
175 return -IPSET_ERR_PROTOCOL;
176
177 if (tb[IPSET_ATTR_LINENO])
178 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
179
180 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
181 ip_set_get_extensions(set, tb, &ext);
182 if (ret)
183 return ret;
184
185 if (tb[IPSET_ATTR_CIDR]) {
186 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
187 if (!cidr || cidr > HOST_MASK)
188 return -IPSET_ERR_INVALID_CIDR;
189 e.cidr = cidr - 1;
190 }
191
192 if (tb[IPSET_ATTR_PORT])
193 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
194 else
195 return -IPSET_ERR_PROTOCOL;
196
197 if (tb[IPSET_ATTR_PROTO]) {
198 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
199 with_ports = ip_set_proto_with_ports(e.proto);
200
201 if (e.proto == 0)
202 return -IPSET_ERR_INVALID_PROTO;
203 } else
204 return -IPSET_ERR_MISSING_PROTO;
205
206 if (!(with_ports || e.proto == IPPROTO_ICMP))
207 e.port = 0;
208
209 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
210
211 if (tb[IPSET_ATTR_CADT_FLAGS]) {
212 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
213 if (cadt_flags & IPSET_FLAG_NOMATCH)
214 flags |= (IPSET_FLAG_NOMATCH << 16);
215 }
216
217 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
218 e.ip = htonl(ip & 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 port = port_to = ntohs(e.port);
225 if (tb[IPSET_ATTR_PORT_TO]) {
226 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
227 if (port_to < port)
228 swap(port, port_to);
229 }
230 if (tb[IPSET_ATTR_IP_TO]) {
231 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
232 if (ret)
233 return ret;
234 if (ip_to < ip)
235 swap(ip, ip_to);
236 if (ip + UINT_MAX == ip_to)
237 return -IPSET_ERR_HASH_RANGE;
238 } else
239 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
240
241 if (retried)
242 ip = ntohl(h->next.ip);
243 while (!after(ip, ip_to)) {
244 e.ip = htonl(ip);
245 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
246 e.cidr = cidr - 1;
247 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
248 : port;
249 for (; p <= port_to; p++) {
250 e.port = htons(p);
251 ret = adtfn(set, &e, &ext, &ext, flags);
252
253 if (ret && !ip_set_eexist(ret, flags))
254 return ret;
255 else
256 ret = 0;
257 }
258 ip = last + 1;
259 }
260 return ret;
261 }
262
263 /* IPv6 variant */
264
265 struct hash_netport6_elem {
266 union nf_inet_addr ip;
267 __be16 port;
268 u8 proto;
269 u8 cidr:7;
270 u8 nomatch:1;
271 };
272
273 /* Common functions */
274
275 static inline bool
276 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
277 const struct hash_netport6_elem *ip2,
278 u32 *multi)
279 {
280 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
281 ip1->port == ip2->port &&
282 ip1->proto == ip2->proto &&
283 ip1->cidr == ip2->cidr;
284 }
285
286 static inline int
287 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
288 {
289 return elem->nomatch ? -ENOTEMPTY : 1;
290 }
291
292 static inline void
293 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
294 {
295 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
296 }
297
298 static inline void
299 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
300 {
301 swap(*flags, elem->nomatch);
302 }
303
304 static inline void
305 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
306 {
307 ip6_netmask(&elem->ip, cidr);
308 elem->cidr = cidr - 1;
309 }
310
311 static bool
312 hash_netport6_data_list(struct sk_buff *skb,
313 const struct hash_netport6_elem *data)
314 {
315 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
316
317 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
318 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
319 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
320 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
321 (flags &&
322 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
323 goto nla_put_failure;
324 return 0;
325
326 nla_put_failure:
327 return 1;
328 }
329
330 static inline void
331 hash_netport6_data_next(struct hash_netport4_elem *next,
332 const struct hash_netport6_elem *d)
333 {
334 next->port = d->port;
335 }
336
337 #undef MTYPE
338 #undef PF
339 #undef HOST_MASK
340
341 #define MTYPE hash_netport6
342 #define PF 6
343 #define HOST_MASK 128
344 #define IP_SET_EMIT_CREATE
345 #include "ip_set_hash_gen.h"
346
347 static int
348 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
349 const struct xt_action_param *par,
350 enum ipset_adt adt, struct ip_set_adt_opt *opt)
351 {
352 const struct hash_netport *h = set->data;
353 ipset_adtfn adtfn = set->variant->adt[adt];
354 struct hash_netport6_elem e = {
355 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
356 };
357 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
358
359 if (adt == IPSET_TEST)
360 e.cidr = HOST_MASK - 1;
361
362 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
363 &e.port, &e.proto))
364 return -EINVAL;
365
366 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
367 ip6_netmask(&e.ip, e.cidr + 1);
368
369 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
370 }
371
372 static int
373 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
374 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
375 {
376 const struct hash_netport *h = set->data;
377 ipset_adtfn adtfn = set->variant->adt[adt];
378 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
379 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
380 u32 port, port_to;
381 bool with_ports = false;
382 u8 cidr;
383 int ret;
384
385 if (unlikely(!tb[IPSET_ATTR_IP] ||
386 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
387 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
388 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
389 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
390 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
391 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
392 return -IPSET_ERR_PROTOCOL;
393 if (unlikely(tb[IPSET_ATTR_IP_TO]))
394 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
395
396 if (tb[IPSET_ATTR_LINENO])
397 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
398
399 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
400 ip_set_get_extensions(set, tb, &ext);
401 if (ret)
402 return ret;
403
404 if (tb[IPSET_ATTR_CIDR]) {
405 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
406 if (!cidr || cidr > HOST_MASK)
407 return -IPSET_ERR_INVALID_CIDR;
408 e.cidr = cidr - 1;
409 }
410 ip6_netmask(&e.ip, e.cidr + 1);
411
412 if (tb[IPSET_ATTR_PORT])
413 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
414 else
415 return -IPSET_ERR_PROTOCOL;
416
417 if (tb[IPSET_ATTR_PROTO]) {
418 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
419 with_ports = ip_set_proto_with_ports(e.proto);
420
421 if (e.proto == 0)
422 return -IPSET_ERR_INVALID_PROTO;
423 } else
424 return -IPSET_ERR_MISSING_PROTO;
425
426 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
427 e.port = 0;
428
429 if (tb[IPSET_ATTR_CADT_FLAGS]) {
430 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
431 if (cadt_flags & IPSET_FLAG_NOMATCH)
432 flags |= (IPSET_FLAG_NOMATCH << 16);
433 }
434
435 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
436 ret = adtfn(set, &e, &ext, &ext, flags);
437 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
438 ip_set_eexist(ret, flags) ? 0 : ret;
439 }
440
441 port = ntohs(e.port);
442 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
443 if (port > port_to)
444 swap(port, port_to);
445
446 if (retried)
447 port = ntohs(h->next.port);
448 for (; port <= port_to; port++) {
449 e.port = htons(port);
450 ret = adtfn(set, &e, &ext, &ext, flags);
451
452 if (ret && !ip_set_eexist(ret, flags))
453 return ret;
454 else
455 ret = 0;
456 }
457 return ret;
458 }
459
460 static struct ip_set_type hash_netport_type __read_mostly = {
461 .name = "hash:net,port",
462 .protocol = IPSET_PROTOCOL,
463 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
464 .dimension = IPSET_DIM_TWO,
465 .family = NFPROTO_UNSPEC,
466 .revision_min = IPSET_TYPE_REV_MIN,
467 .revision_max = IPSET_TYPE_REV_MAX,
468 .create = hash_netport_create,
469 .create_policy = {
470 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
471 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
472 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
473 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
474 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
475 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
476 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
477 },
478 .adt_policy = {
479 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
480 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
481 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
482 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
483 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
484 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
485 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
486 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
487 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
488 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
489 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
490 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
491 },
492 .me = THIS_MODULE,
493 };
494
495 static int __init
496 hash_netport_init(void)
497 {
498 return ip_set_type_register(&hash_netport_type);
499 }
500
501 static void __exit
502 hash_netport_fini(void)
503 {
504 ip_set_type_unregister(&hash_netport_type);
505 }
506
507 module_init(hash_netport_init);
508 module_exit(hash_netport_fini);
This page took 0.085538 seconds and 5 git commands to generate.