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