]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - net/netfilter/ipset/ip_set_hash_ipportnet.c
UBUNTU: Ubuntu-5.4.0-117.132
[mirror_ubuntu-focal-kernel.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 inline 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 inline int
75 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
76 {
77 return elem->nomatch ? -ENOTEMPTY : 1;
78 }
79
80 static inline 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 inline void
87 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
88 {
89 swap(*flags, elem->nomatch);
90 }
91
92 static inline 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 inline 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 const 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;
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 e.ip2 = htonl(ip2);
282 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr);
283 e.cidr = cidr - 1;
284 ret = adtfn(set, &e, &ext, &ext, flags);
285
286 if (ret && !ip_set_eexist(ret, flags))
287 return ret;
288
289 ret = 0;
290 } while (ip2++ < ip2_to);
291 ip2 = ip2_from;
292 }
293 p = port;
294 }
295 return ret;
296 }
297
298 /* IPv6 variant */
299
300 struct hash_ipportnet6_elem {
301 union nf_inet_addr ip;
302 union nf_inet_addr ip2;
303 __be16 port;
304 u8 cidr:7;
305 u8 nomatch:1;
306 u8 proto;
307 };
308
309 /* Common functions */
310
311 static inline bool
312 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
313 const struct hash_ipportnet6_elem *ip2,
314 u32 *multi)
315 {
316 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
317 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
318 ip1->cidr == ip2->cidr &&
319 ip1->port == ip2->port &&
320 ip1->proto == ip2->proto;
321 }
322
323 static inline int
324 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
325 {
326 return elem->nomatch ? -ENOTEMPTY : 1;
327 }
328
329 static inline void
330 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
331 {
332 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
333 }
334
335 static inline void
336 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
337 {
338 swap(*flags, elem->nomatch);
339 }
340
341 static inline void
342 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
343 {
344 ip6_netmask(&elem->ip2, cidr);
345 elem->cidr = cidr - 1;
346 }
347
348 static bool
349 hash_ipportnet6_data_list(struct sk_buff *skb,
350 const struct hash_ipportnet6_elem *data)
351 {
352 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
353
354 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
355 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
356 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
357 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
358 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
359 (flags &&
360 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
361 goto nla_put_failure;
362 return false;
363
364 nla_put_failure:
365 return true;
366 }
367
368 static inline void
369 hash_ipportnet6_data_next(struct hash_ipportnet6_elem *next,
370 const struct hash_ipportnet6_elem *d)
371 {
372 next->port = d->port;
373 }
374
375 #undef MTYPE
376 #undef HOST_MASK
377
378 #define MTYPE hash_ipportnet6
379 #define HOST_MASK 128
380 #define IP_SET_EMIT_CREATE
381 #include "ip_set_hash_gen.h"
382
383 static int
384 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
385 const struct xt_action_param *par,
386 enum ipset_adt adt, struct ip_set_adt_opt *opt)
387 {
388 const struct hash_ipportnet6 *h = set->data;
389 ipset_adtfn adtfn = set->variant->adt[adt];
390 struct hash_ipportnet6_elem e = {
391 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
392 };
393 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
394
395 if (adt == IPSET_TEST)
396 e.cidr = HOST_MASK - 1;
397
398 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
399 &e.port, &e.proto))
400 return -EINVAL;
401
402 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
403 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
404 ip6_netmask(&e.ip2, e.cidr + 1);
405
406 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
407 }
408
409 static int
410 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
411 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
412 {
413 const struct hash_ipportnet6 *h = set->data;
414 ipset_adtfn adtfn = set->variant->adt[adt];
415 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
416 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
417 u32 port, port_to;
418 bool with_ports = false;
419 u8 cidr;
420 int ret;
421
422 if (tb[IPSET_ATTR_LINENO])
423 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
424
425 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
426 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
427 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
428 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
429 return -IPSET_ERR_PROTOCOL;
430 if (unlikely(tb[IPSET_ATTR_IP_TO]))
431 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
432 if (unlikely(tb[IPSET_ATTR_CIDR])) {
433 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
434
435 if (cidr != HOST_MASK)
436 return -IPSET_ERR_INVALID_CIDR;
437 }
438
439 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
440 if (ret)
441 return ret;
442
443 ret = ip_set_get_extensions(set, tb, &ext);
444 if (ret)
445 return ret;
446
447 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
448 if (ret)
449 return ret;
450
451 if (tb[IPSET_ATTR_CIDR2]) {
452 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
453 if (!cidr || cidr > HOST_MASK)
454 return -IPSET_ERR_INVALID_CIDR;
455 e.cidr = cidr - 1;
456 }
457
458 ip6_netmask(&e.ip2, e.cidr + 1);
459
460 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
461
462 if (tb[IPSET_ATTR_PROTO]) {
463 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
464 with_ports = ip_set_proto_with_ports(e.proto);
465
466 if (e.proto == 0)
467 return -IPSET_ERR_INVALID_PROTO;
468 } else {
469 return -IPSET_ERR_MISSING_PROTO;
470 }
471
472 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
473 e.port = 0;
474
475 if (tb[IPSET_ATTR_CADT_FLAGS]) {
476 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
477
478 if (cadt_flags & IPSET_FLAG_NOMATCH)
479 flags |= (IPSET_FLAG_NOMATCH << 16);
480 }
481
482 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
483 ret = adtfn(set, &e, &ext, &ext, flags);
484 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
485 ip_set_eexist(ret, flags) ? 0 : ret;
486 }
487
488 port = ntohs(e.port);
489 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
490 if (port > port_to)
491 swap(port, port_to);
492
493 if (retried)
494 port = ntohs(h->next.port);
495 for (; port <= port_to; port++) {
496 e.port = htons(port);
497 ret = adtfn(set, &e, &ext, &ext, flags);
498
499 if (ret && !ip_set_eexist(ret, flags))
500 return ret;
501
502 ret = 0;
503 }
504 return ret;
505 }
506
507 static struct ip_set_type hash_ipportnet_type __read_mostly = {
508 .name = "hash:ip,port,net",
509 .protocol = IPSET_PROTOCOL,
510 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
511 IPSET_TYPE_NOMATCH,
512 .dimension = IPSET_DIM_THREE,
513 .family = NFPROTO_UNSPEC,
514 .revision_min = IPSET_TYPE_REV_MIN,
515 .revision_max = IPSET_TYPE_REV_MAX,
516 .create = hash_ipportnet_create,
517 .create_policy = {
518 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
519 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
520 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
521 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
522 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
523 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
524 },
525 .adt_policy = {
526 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
527 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
528 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
529 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
530 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
531 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
532 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
533 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
534 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
535 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
536 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
537 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
538 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
539 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
540 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
541 .len = IPSET_MAX_COMMENT_SIZE },
542 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
543 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
544 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
545 },
546 .me = THIS_MODULE,
547 };
548
549 static int __init
550 hash_ipportnet_init(void)
551 {
552 return ip_set_type_register(&hash_ipportnet_type);
553 }
554
555 static void __exit
556 hash_ipportnet_fini(void)
557 {
558 rcu_barrier();
559 ip_set_type_unregister(&hash_ipportnet_type);
560 }
561
562 module_init(hash_ipportnet_init);
563 module_exit(hash_ipportnet_fini);