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