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