GNU Linux-libre 5.10.217-gnu1
[releases.git] / net / netfilter / ipset / ip_set_hash_ipportnet.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:ip,port,net 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 #include <net/tcp.h>
16
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_getport.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
22
23 #define IPSET_TYPE_REV_MIN      0
24 /*                              1    SCTP and UDPLITE support added */
25 /*                              2    Range as input support for IPv4 added */
26 /*                              3    nomatch flag support added */
27 /*                              4    Counters support added */
28 /*                              5    Comments support added */
29 /*                              6    Forceadd support added */
30 #define IPSET_TYPE_REV_MAX      7 /* skbinfo support added */
31
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:ip,port,net");
36
37 /* Type specific function prefix */
38 #define HTYPE           hash_ipportnet
39
40 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
41  * However this way we have to store internally cidr - 1,
42  * dancing back and forth.
43  */
44 #define IP_SET_HASH_WITH_NETS_PACKED
45 #define IP_SET_HASH_WITH_PROTO
46 #define IP_SET_HASH_WITH_NETS
47
48 /* IPv4 variant */
49
50 /* Member elements */
51 struct hash_ipportnet4_elem {
52         __be32 ip;
53         __be32 ip2;
54         __be16 port;
55         u8 cidr:7;
56         u8 nomatch:1;
57         u8 proto;
58 };
59
60 /* Common functions */
61
62 static bool
63 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
64                            const struct hash_ipportnet4_elem *ip2,
65                            u32 *multi)
66 {
67         return ip1->ip == ip2->ip &&
68                ip1->ip2 == ip2->ip2 &&
69                ip1->cidr == ip2->cidr &&
70                ip1->port == ip2->port &&
71                ip1->proto == ip2->proto;
72 }
73
74 static int
75 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
76 {
77         return elem->nomatch ? -ENOTEMPTY : 1;
78 }
79
80 static void
81 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags)
82 {
83         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
84 }
85
86 static void
87 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
88 {
89         swap(*flags, elem->nomatch);
90 }
91
92 static void
93 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
94 {
95         elem->ip2 &= ip_set_netmask(cidr);
96         elem->cidr = cidr - 1;
97 }
98
99 static bool
100 hash_ipportnet4_data_list(struct sk_buff *skb,
101                           const struct hash_ipportnet4_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_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
107             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
108             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
109             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
110             (flags &&
111              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
112                 goto nla_put_failure;
113         return false;
114
115 nla_put_failure:
116         return true;
117 }
118
119 static void
120 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next,
121                           const struct hash_ipportnet4_elem *d)
122 {
123         next->ip = d->ip;
124         next->port = d->port;
125         next->ip2 = d->ip2;
126 }
127
128 #define MTYPE           hash_ipportnet4
129 #define HOST_MASK       32
130 #include "ip_set_hash_gen.h"
131
132 static int
133 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
134                      const struct xt_action_param *par,
135                      enum ipset_adt adt, struct ip_set_adt_opt *opt)
136 {
137         const struct hash_ipportnet4 *h = set->data;
138         ipset_adtfn adtfn = set->variant->adt[adt];
139         struct hash_ipportnet4_elem e = {
140                 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
141         };
142         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
143
144         if (adt == IPSET_TEST)
145                 e.cidr = HOST_MASK - 1;
146
147         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
148                                  &e.port, &e.proto))
149                 return -EINVAL;
150
151         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
152         ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
153         e.ip2 &= ip_set_netmask(e.cidr + 1);
154
155         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
156 }
157
158 static int
159 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
160                      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
161 {
162         struct hash_ipportnet4 *h = set->data;
163         ipset_adtfn adtfn = set->variant->adt[adt];
164         struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
165         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
166         u32 ip = 0, ip_to = 0, p = 0, port, port_to;
167         u32 ip2_from = 0, ip2_to = 0, ip2, i = 0;
168         bool with_ports = false;
169         u8 cidr;
170         int ret;
171
172         if (tb[IPSET_ATTR_LINENO])
173                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
174
175         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
176                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
177                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
178                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
179                 return -IPSET_ERR_PROTOCOL;
180
181         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
182         if (ret)
183                 return ret;
184
185         ret = ip_set_get_extensions(set, tb, &ext);
186         if (ret)
187                 return ret;
188
189         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
190         if (ret)
191                 return ret;
192
193         if (tb[IPSET_ATTR_CIDR2]) {
194                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
195                 if (!cidr || cidr > HOST_MASK)
196                         return -IPSET_ERR_INVALID_CIDR;
197                 e.cidr = cidr - 1;
198         }
199
200         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
201
202         if (tb[IPSET_ATTR_PROTO]) {
203                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
204                 with_ports = ip_set_proto_with_ports(e.proto);
205
206                 if (e.proto == 0)
207                         return -IPSET_ERR_INVALID_PROTO;
208         } else {
209                 return -IPSET_ERR_MISSING_PROTO;
210         }
211
212         if (!(with_ports || e.proto == IPPROTO_ICMP))
213                 e.port = 0;
214
215         if (tb[IPSET_ATTR_CADT_FLAGS]) {
216                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
217
218                 if (cadt_flags & IPSET_FLAG_NOMATCH)
219                         flags |= (IPSET_FLAG_NOMATCH << 16);
220         }
221
222         with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
223         if (adt == IPSET_TEST ||
224             !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
225               tb[IPSET_ATTR_IP2_TO])) {
226                 e.ip = htonl(ip);
227                 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1));
228                 ret = adtfn(set, &e, &ext, &ext, flags);
229                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
230                        ip_set_eexist(ret, flags) ? 0 : ret;
231         }
232
233         ip_to = ip;
234         if (tb[IPSET_ATTR_IP_TO]) {
235                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
236                 if (ret)
237                         return ret;
238                 if (ip > ip_to)
239                         swap(ip, ip_to);
240         } else if (tb[IPSET_ATTR_CIDR]) {
241                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
242
243                 if (!cidr || cidr > HOST_MASK)
244                         return -IPSET_ERR_INVALID_CIDR;
245                 ip_set_mask_from_to(ip, ip_to, cidr);
246         }
247
248         port_to = port = ntohs(e.port);
249         if (tb[IPSET_ATTR_PORT_TO]) {
250                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
251                 if (port > port_to)
252                         swap(port, port_to);
253         }
254
255         ip2_to = ip2_from;
256         if (tb[IPSET_ATTR_IP2_TO]) {
257                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
258                 if (ret)
259                         return ret;
260                 if (ip2_from > ip2_to)
261                         swap(ip2_from, ip2_to);
262                 if (ip2_from + UINT_MAX == ip2_to)
263                         return -IPSET_ERR_HASH_RANGE;
264         } else {
265                 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1);
266         }
267
268         if (retried) {
269                 ip = ntohl(h->next.ip);
270                 p = ntohs(h->next.port);
271                 ip2 = ntohl(h->next.ip2);
272         } else {
273                 p = port;
274                 ip2 = ip2_from;
275         }
276         for (; ip <= ip_to; ip++) {
277                 e.ip = htonl(ip);
278                 for (; p <= port_to; p++) {
279                         e.port = htons(p);
280                         do {
281                                 i++;
282                                 e.ip2 = htonl(ip2);
283                                 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr);
284                                 e.cidr = cidr - 1;
285                                 if (i > IPSET_MAX_RANGE) {
286                                         hash_ipportnet4_data_next(&h->next,
287                                                                   &e);
288                                         return -ERANGE;
289                                 }
290                                 ret = adtfn(set, &e, &ext, &ext, flags);
291
292                                 if (ret && !ip_set_eexist(ret, flags))
293                                         return ret;
294
295                                 ret = 0;
296                         } while (ip2++ < ip2_to);
297                         ip2 = ip2_from;
298                 }
299                 p = port;
300         }
301         return ret;
302 }
303
304 /* IPv6 variant */
305
306 struct hash_ipportnet6_elem {
307         union nf_inet_addr ip;
308         union nf_inet_addr ip2;
309         __be16 port;
310         u8 cidr:7;
311         u8 nomatch:1;
312         u8 proto;
313 };
314
315 /* Common functions */
316
317 static bool
318 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
319                            const struct hash_ipportnet6_elem *ip2,
320                            u32 *multi)
321 {
322         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
323                ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
324                ip1->cidr == ip2->cidr &&
325                ip1->port == ip2->port &&
326                ip1->proto == ip2->proto;
327 }
328
329 static int
330 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
331 {
332         return elem->nomatch ? -ENOTEMPTY : 1;
333 }
334
335 static void
336 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
337 {
338         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
339 }
340
341 static void
342 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
343 {
344         swap(*flags, elem->nomatch);
345 }
346
347 static void
348 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
349 {
350         ip6_netmask(&elem->ip2, cidr);
351         elem->cidr = cidr - 1;
352 }
353
354 static bool
355 hash_ipportnet6_data_list(struct sk_buff *skb,
356                           const struct hash_ipportnet6_elem *data)
357 {
358         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
359
360         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
361             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
362             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
363             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
364             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
365             (flags &&
366              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
367                 goto nla_put_failure;
368         return false;
369
370 nla_put_failure:
371         return true;
372 }
373
374 static void
375 hash_ipportnet6_data_next(struct hash_ipportnet6_elem *next,
376                           const struct hash_ipportnet6_elem *d)
377 {
378         next->port = d->port;
379 }
380
381 #undef MTYPE
382 #undef HOST_MASK
383
384 #define MTYPE           hash_ipportnet6
385 #define HOST_MASK       128
386 #define IP_SET_EMIT_CREATE
387 #include "ip_set_hash_gen.h"
388
389 static int
390 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
391                      const struct xt_action_param *par,
392                      enum ipset_adt adt, struct ip_set_adt_opt *opt)
393 {
394         const struct hash_ipportnet6 *h = set->data;
395         ipset_adtfn adtfn = set->variant->adt[adt];
396         struct hash_ipportnet6_elem e = {
397                 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
398         };
399         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
400
401         if (adt == IPSET_TEST)
402                 e.cidr = HOST_MASK - 1;
403
404         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
405                                  &e.port, &e.proto))
406                 return -EINVAL;
407
408         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
409         ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
410         ip6_netmask(&e.ip2, e.cidr + 1);
411
412         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
413 }
414
415 static int
416 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
417                      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
418 {
419         const struct hash_ipportnet6 *h = set->data;
420         ipset_adtfn adtfn = set->variant->adt[adt];
421         struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
422         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
423         u32 port, port_to;
424         bool with_ports = false;
425         u8 cidr;
426         int ret;
427
428         if (tb[IPSET_ATTR_LINENO])
429                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
430
431         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
432                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
433                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
434                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
435                 return -IPSET_ERR_PROTOCOL;
436         if (unlikely(tb[IPSET_ATTR_IP_TO]))
437                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
438         if (unlikely(tb[IPSET_ATTR_CIDR])) {
439                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
440
441                 if (cidr != HOST_MASK)
442                         return -IPSET_ERR_INVALID_CIDR;
443         }
444
445         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
446         if (ret)
447                 return ret;
448
449         ret = ip_set_get_extensions(set, tb, &ext);
450         if (ret)
451                 return ret;
452
453         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
454         if (ret)
455                 return ret;
456
457         if (tb[IPSET_ATTR_CIDR2]) {
458                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
459                 if (!cidr || cidr > HOST_MASK)
460                         return -IPSET_ERR_INVALID_CIDR;
461                 e.cidr = cidr - 1;
462         }
463
464         ip6_netmask(&e.ip2, e.cidr + 1);
465
466         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
467
468         if (tb[IPSET_ATTR_PROTO]) {
469                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
470                 with_ports = ip_set_proto_with_ports(e.proto);
471
472                 if (e.proto == 0)
473                         return -IPSET_ERR_INVALID_PROTO;
474         } else {
475                 return -IPSET_ERR_MISSING_PROTO;
476         }
477
478         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
479                 e.port = 0;
480
481         if (tb[IPSET_ATTR_CADT_FLAGS]) {
482                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
483
484                 if (cadt_flags & IPSET_FLAG_NOMATCH)
485                         flags |= (IPSET_FLAG_NOMATCH << 16);
486         }
487
488         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
489                 ret = adtfn(set, &e, &ext, &ext, flags);
490                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
491                        ip_set_eexist(ret, flags) ? 0 : ret;
492         }
493
494         port = ntohs(e.port);
495         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
496         if (port > port_to)
497                 swap(port, port_to);
498
499         if (retried)
500                 port = ntohs(h->next.port);
501         for (; port <= port_to; port++) {
502                 e.port = htons(port);
503                 ret = adtfn(set, &e, &ext, &ext, flags);
504
505                 if (ret && !ip_set_eexist(ret, flags))
506                         return ret;
507
508                 ret = 0;
509         }
510         return ret;
511 }
512
513 static struct ip_set_type hash_ipportnet_type __read_mostly = {
514         .name           = "hash:ip,port,net",
515         .protocol       = IPSET_PROTOCOL,
516         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
517                           IPSET_TYPE_NOMATCH,
518         .dimension      = IPSET_DIM_THREE,
519         .family         = NFPROTO_UNSPEC,
520         .revision_min   = IPSET_TYPE_REV_MIN,
521         .revision_max   = IPSET_TYPE_REV_MAX,
522         .create         = hash_ipportnet_create,
523         .create_policy  = {
524                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
525                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
526                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
527                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
528                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
529                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
530         },
531         .adt_policy     = {
532                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
533                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
534                 [IPSET_ATTR_IP2]        = { .type = NLA_NESTED },
535                 [IPSET_ATTR_IP2_TO]     = { .type = NLA_NESTED },
536                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
537                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
538                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
539                 [IPSET_ATTR_CIDR2]      = { .type = NLA_U8 },
540                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
541                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
542                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
543                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
544                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
545                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
546                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
547                                             .len  = IPSET_MAX_COMMENT_SIZE },
548                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
549                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
550                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
551         },
552         .me             = THIS_MODULE,
553 };
554
555 static int __init
556 hash_ipportnet_init(void)
557 {
558         return ip_set_type_register(&hash_ipportnet_type);
559 }
560
561 static void __exit
562 hash_ipportnet_fini(void)
563 {
564         rcu_barrier();
565         ip_set_type_unregister(&hash_ipportnet_type);
566 }
567
568 module_init(hash_ipportnet_init);
569 module_exit(hash_ipportnet_fini);