GNU Linux-libre 5.10.217-gnu1
[releases.git] / net / netfilter / ipset / ip_set_hash_ipport.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 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    Counters support added */
26 /*                              3    Comments support added */
27 /*                              4    Forceadd support added */
28 #define IPSET_TYPE_REV_MAX      5 /* skbinfo support added */
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32 IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_hash:ip,port");
34
35 /* Type specific function prefix */
36 #define HTYPE           hash_ipport
37
38 /* IPv4 variant */
39
40 /* Member elements */
41 struct hash_ipport4_elem {
42         __be32 ip;
43         __be16 port;
44         u8 proto;
45         u8 padding;
46 };
47
48 /* Common functions */
49
50 static bool
51 hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
52                         const struct hash_ipport4_elem *ip2,
53                         u32 *multi)
54 {
55         return ip1->ip == ip2->ip &&
56                ip1->port == ip2->port &&
57                ip1->proto == ip2->proto;
58 }
59
60 static bool
61 hash_ipport4_data_list(struct sk_buff *skb,
62                        const struct hash_ipport4_elem *data)
63 {
64         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
65             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
66             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
67                 goto nla_put_failure;
68         return false;
69
70 nla_put_failure:
71         return true;
72 }
73
74 static void
75 hash_ipport4_data_next(struct hash_ipport4_elem *next,
76                        const struct hash_ipport4_elem *d)
77 {
78         next->ip = d->ip;
79         next->port = d->port;
80 }
81
82 #define MTYPE           hash_ipport4
83 #define HOST_MASK       32
84 #include "ip_set_hash_gen.h"
85
86 static int
87 hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
88                   const struct xt_action_param *par,
89                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
90 {
91         ipset_adtfn adtfn = set->variant->adt[adt];
92         struct hash_ipport4_elem e = { .ip = 0 };
93         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
94
95         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
96                                  &e.port, &e.proto))
97                 return -EINVAL;
98
99         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
100         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
101 }
102
103 static int
104 hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
105                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
106 {
107         struct hash_ipport4 *h = set->data;
108         ipset_adtfn adtfn = set->variant->adt[adt];
109         struct hash_ipport4_elem e = { .ip = 0 };
110         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
111         u32 ip, ip_to = 0, p = 0, port, port_to, i = 0;
112         bool with_ports = false;
113         int ret;
114
115         if (tb[IPSET_ATTR_LINENO])
116                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
117
118         if (unlikely(!tb[IPSET_ATTR_IP] ||
119                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
120                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
121                 return -IPSET_ERR_PROTOCOL;
122
123         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
124         if (ret)
125                 return ret;
126
127         ret = ip_set_get_extensions(set, tb, &ext);
128         if (ret)
129                 return ret;
130
131         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
132
133         if (tb[IPSET_ATTR_PROTO]) {
134                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
135                 with_ports = ip_set_proto_with_ports(e.proto);
136
137                 if (e.proto == 0)
138                         return -IPSET_ERR_INVALID_PROTO;
139         } else {
140                 return -IPSET_ERR_MISSING_PROTO;
141         }
142
143         if (!(with_ports || e.proto == IPPROTO_ICMP))
144                 e.port = 0;
145
146         if (adt == IPSET_TEST ||
147             !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
148               tb[IPSET_ATTR_PORT_TO])) {
149                 ret = adtfn(set, &e, &ext, &ext, flags);
150                 return ip_set_eexist(ret, flags) ? 0 : ret;
151         }
152
153         ip_to = ip = ntohl(e.ip);
154         if (tb[IPSET_ATTR_IP_TO]) {
155                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
156                 if (ret)
157                         return ret;
158                 if (ip > ip_to)
159                         swap(ip, ip_to);
160         } else if (tb[IPSET_ATTR_CIDR]) {
161                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
162
163                 if (!cidr || cidr > HOST_MASK)
164                         return -IPSET_ERR_INVALID_CIDR;
165                 ip_set_mask_from_to(ip, ip_to, cidr);
166         }
167
168         port_to = port = ntohs(e.port);
169         if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
170                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
171                 if (port > port_to)
172                         swap(port, port_to);
173         }
174
175         if (retried)
176                 ip = ntohl(h->next.ip);
177         for (; ip <= ip_to; ip++) {
178                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
179                                                        : port;
180                 for (; p <= port_to; p++, i++) {
181                         e.ip = htonl(ip);
182                         e.port = htons(p);
183                         if (i > IPSET_MAX_RANGE) {
184                                 hash_ipport4_data_next(&h->next, &e);
185                                 return -ERANGE;
186                         }
187                         ret = adtfn(set, &e, &ext, &ext, flags);
188
189                         if (ret && !ip_set_eexist(ret, flags))
190                                 return ret;
191
192                         ret = 0;
193                 }
194         }
195         return ret;
196 }
197
198 /* IPv6 variant */
199
200 struct hash_ipport6_elem {
201         union nf_inet_addr ip;
202         __be16 port;
203         u8 proto;
204         u8 padding;
205 };
206
207 /* Common functions */
208
209 static bool
210 hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
211                         const struct hash_ipport6_elem *ip2,
212                         u32 *multi)
213 {
214         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
215                ip1->port == ip2->port &&
216                ip1->proto == ip2->proto;
217 }
218
219 static bool
220 hash_ipport6_data_list(struct sk_buff *skb,
221                        const struct hash_ipport6_elem *data)
222 {
223         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
224             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
225             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
226                 goto nla_put_failure;
227         return false;
228
229 nla_put_failure:
230         return true;
231 }
232
233 static void
234 hash_ipport6_data_next(struct hash_ipport6_elem *next,
235                        const struct hash_ipport6_elem *d)
236 {
237         next->port = d->port;
238 }
239
240 #undef MTYPE
241 #undef HOST_MASK
242
243 #define MTYPE           hash_ipport6
244 #define HOST_MASK       128
245 #define IP_SET_EMIT_CREATE
246 #include "ip_set_hash_gen.h"
247
248 static int
249 hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
250                   const struct xt_action_param *par,
251                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
252 {
253         ipset_adtfn adtfn = set->variant->adt[adt];
254         struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
255         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
256
257         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
258                                  &e.port, &e.proto))
259                 return -EINVAL;
260
261         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
262         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
263 }
264
265 static int
266 hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
267                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
268 {
269         const struct hash_ipport6 *h = set->data;
270         ipset_adtfn adtfn = set->variant->adt[adt];
271         struct hash_ipport6_elem e = { .ip = { .all = { 0 } } };
272         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
273         u32 port, port_to;
274         bool with_ports = false;
275         int ret;
276
277         if (tb[IPSET_ATTR_LINENO])
278                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
279
280         if (unlikely(!tb[IPSET_ATTR_IP] ||
281                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
282                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO)))
283                 return -IPSET_ERR_PROTOCOL;
284         if (unlikely(tb[IPSET_ATTR_IP_TO]))
285                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
286         if (unlikely(tb[IPSET_ATTR_CIDR])) {
287                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
288
289                 if (cidr != HOST_MASK)
290                         return -IPSET_ERR_INVALID_CIDR;
291         }
292
293         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
294         if (ret)
295                 return ret;
296
297         ret = ip_set_get_extensions(set, tb, &ext);
298         if (ret)
299                 return ret;
300
301         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
302
303         if (tb[IPSET_ATTR_PROTO]) {
304                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
305                 with_ports = ip_set_proto_with_ports(e.proto);
306
307                 if (e.proto == 0)
308                         return -IPSET_ERR_INVALID_PROTO;
309         } else {
310                 return -IPSET_ERR_MISSING_PROTO;
311         }
312
313         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
314                 e.port = 0;
315
316         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
317                 ret = adtfn(set, &e, &ext, &ext, flags);
318                 return ip_set_eexist(ret, flags) ? 0 : ret;
319         }
320
321         port = ntohs(e.port);
322         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
323         if (port > port_to)
324                 swap(port, port_to);
325
326         if (retried)
327                 port = ntohs(h->next.port);
328         for (; port <= port_to; port++) {
329                 e.port = htons(port);
330                 ret = adtfn(set, &e, &ext, &ext, flags);
331
332                 if (ret && !ip_set_eexist(ret, flags))
333                         return ret;
334
335                 ret = 0;
336         }
337         return ret;
338 }
339
340 static struct ip_set_type hash_ipport_type __read_mostly = {
341         .name           = "hash:ip,port",
342         .protocol       = IPSET_PROTOCOL,
343         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT,
344         .dimension      = IPSET_DIM_TWO,
345         .family         = NFPROTO_UNSPEC,
346         .revision_min   = IPSET_TYPE_REV_MIN,
347         .revision_max   = IPSET_TYPE_REV_MAX,
348         .create         = hash_ipport_create,
349         .create_policy  = {
350                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
351                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
352                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
353                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
354                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
355                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
356                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
357         },
358         .adt_policy     = {
359                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
360                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
361                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
362                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
363                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
364                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
365                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
366                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
367                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
368                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
369                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
370                                             .len  = IPSET_MAX_COMMENT_SIZE },
371                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
372                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
373                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
374         },
375         .me             = THIS_MODULE,
376 };
377
378 static int __init
379 hash_ipport_init(void)
380 {
381         return ip_set_type_register(&hash_ipport_type);
382 }
383
384 static void __exit
385 hash_ipport_fini(void)
386 {
387         rcu_barrier();
388         ip_set_type_unregister(&hash_ipport_type);
389 }
390
391 module_init(hash_ipport_init);
392 module_exit(hash_ipport_fini);