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