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