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