]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/ipset/ip_set_hash_net.c
netfilter: ipset: The bitmap types with counter support
[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
10111a6e
JK
25#define REVISION_MIN 0
26/* 1 Range as input support for IPv4 added */
27#define REVISION_MAX 2 /* nomatch flag support added */
28
b3837029
JK
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
10111a6e 31IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_MAX);
b3837029
JK
32MODULE_ALIAS("ip_set_hash:net");
33
34/* Type specific function prefix */
5d50e1d8
JK
35#define HTYPE hash_net
36#define IP_SET_HASH_WITH_NETS
b3837029 37
5d50e1d8 38/* IPv4 variants */
b3837029 39
5d50e1d8 40/* Member elements */
b3837029
JK
41struct hash_net4_elem {
42 __be32 ip;
43 u16 padding0;
2a7cef2a 44 u8 nomatch;
b3837029
JK
45 u8 cidr;
46};
47
5d50e1d8 48struct hash_net4t_elem {
b3837029
JK
49 __be32 ip;
50 u16 padding0;
2a7cef2a 51 u8 nomatch;
b3837029
JK
52 u8 cidr;
53 unsigned long timeout;
54};
55
5d50e1d8
JK
56/* Common functions */
57
b3837029
JK
58static inline bool
59hash_net4_data_equal(const struct hash_net4_elem *ip1,
89dc79b7
JK
60 const struct hash_net4_elem *ip2,
61 u32 *multi)
b3837029 62{
2a7cef2a
JK
63 return ip1->ip == ip2->ip &&
64 ip1->cidr == ip2->cidr;
b3837029
JK
65}
66
5d50e1d8
JK
67static inline int
68hash_net4_do_data_match(const struct hash_net4_elem *elem)
b3837029 69{
5d50e1d8 70 return elem->nomatch ? -ENOTEMPTY : 1;
2a7cef2a
JK
71}
72
73static inline void
5d50e1d8 74hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags)
2a7cef2a 75{
5d50e1d8 76 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
6eb4c7e9
JK
77}
78
79static inline void
5d50e1d8 80hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags)
2a7cef2a 81{
5d50e1d8 82 swap(*flags, elem->nomatch);
b3837029
JK
83}
84
85static inline void
86hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
87{
88 elem->ip &= ip_set_netmask(cidr);
89 elem->cidr = cidr;
90}
91
b3837029
JK
92static bool
93hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
94{
2a7cef2a
JK
95 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
96
7cf7899d
DM
97 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
98 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
99 (flags &&
100 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
101 goto nla_put_failure;
b3837029
JK
102 return 0;
103
104nla_put_failure:
105 return 1;
106}
107
5d50e1d8
JK
108static inline void
109hash_net4_data_next(struct hash_net4_elem *next,
110 const struct hash_net4_elem *d)
b3837029 111{
5d50e1d8 112 next->ip = d->ip;
b3837029
JK
113}
114
5d50e1d8 115#define MTYPE hash_net4
b3837029
JK
116#define PF 4
117#define HOST_MASK 32
5d50e1d8 118#include "ip_set_hash_gen.h"
3d14b171 119
b3837029
JK
120static int
121hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 122 const struct xt_action_param *par,
5d50e1d8 123 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 124{
5d50e1d8 125 const struct hash_net *h = set->data;
b3837029 126 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 127 struct hash_net4_elem e = {
9b03a5ef
JK
128 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
129 };
5d50e1d8 130 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
b3837029 131
5d50e1d8 132 if (e.cidr == 0)
b3837029
JK
133 return -EINVAL;
134 if (adt == IPSET_TEST)
5d50e1d8 135 e.cidr = HOST_MASK;
b3837029 136
5d50e1d8
JK
137 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
138 e.ip &= ip_set_netmask(e.cidr);
b3837029 139
5d50e1d8 140 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
141}
142
143static int
144hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 145 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 146{
5d50e1d8 147 const struct hash_net *h = set->data;
b3837029 148 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8
JK
149 struct hash_net4_elem e = { .cidr = HOST_MASK };
150 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
d0d9e0a5 151 u32 ip = 0, ip_to, last;
b3837029
JK
152 int ret;
153
154 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a
JK
155 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
156 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
b3837029
JK
157 return -IPSET_ERR_PROTOCOL;
158
159 if (tb[IPSET_ATTR_LINENO])
160 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
161
5d50e1d8
JK
162 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
163 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
164 if (ret)
165 return ret;
166
d0d9e0a5 167 if (tb[IPSET_ATTR_CIDR]) {
5d50e1d8
JK
168 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
169 if (!e.cidr || e.cidr > HOST_MASK)
d0d9e0a5
JK
170 return -IPSET_ERR_INVALID_CIDR;
171 }
b3837029 172
43c56e59 173 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
174 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
175 if (cadt_flags & IPSET_FLAG_NOMATCH)
43c56e59 176 flags |= (IPSET_FLAG_NOMATCH << 16);
2a7cef2a
JK
177 }
178
d0d9e0a5 179 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
5d50e1d8
JK
180 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
181 ret = adtfn(set, &e, &ext, &ext, flags);
43c56e59
JK
182 return ip_set_enomatch(ret, flags, adt) ? 1 :
183 ip_set_eexist(ret, flags) ? 0 : ret;
d0d9e0a5 184 }
b3837029 185
d0d9e0a5
JK
186 ip_to = ip;
187 if (tb[IPSET_ATTR_IP_TO]) {
188 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
189 if (ret)
190 return ret;
191 if (ip_to < ip)
192 swap(ip, ip_to);
193 if (ip + UINT_MAX == ip_to)
194 return -IPSET_ERR_HASH_RANGE;
195 }
196 if (retried)
6e27c9b4 197 ip = ntohl(h->next.ip);
d0d9e0a5 198 while (!after(ip, ip_to)) {
5d50e1d8
JK
199 e.ip = htonl(ip);
200 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
201 ret = adtfn(set, &e, &ext, &ext, flags);
d0d9e0a5
JK
202 if (ret && !ip_set_eexist(ret, flags))
203 return ret;
204 else
205 ret = 0;
206 ip = last + 1;
207 }
208 return ret;
b3837029
JK
209}
210
5d50e1d8 211/* IPv6 variants */
b3837029
JK
212
213struct hash_net6_elem {
214 union nf_inet_addr ip;
215 u16 padding0;
2a7cef2a 216 u8 nomatch;
b3837029
JK
217 u8 cidr;
218};
219
5d50e1d8 220struct hash_net6t_elem {
b3837029
JK
221 union nf_inet_addr ip;
222 u16 padding0;
2a7cef2a 223 u8 nomatch;
b3837029
JK
224 u8 cidr;
225 unsigned long timeout;
226};
227
5d50e1d8
JK
228/* Common functions */
229
b3837029
JK
230static inline bool
231hash_net6_data_equal(const struct hash_net6_elem *ip1,
89dc79b7
JK
232 const struct hash_net6_elem *ip2,
233 u32 *multi)
b3837029 234{
29e3b160 235 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
b3837029
JK
236 ip1->cidr == ip2->cidr;
237}
238
5d50e1d8
JK
239static inline int
240hash_net6_do_data_match(const struct hash_net6_elem *elem)
2a7cef2a 241{
5d50e1d8 242 return elem->nomatch ? -ENOTEMPTY : 1;
6eb4c7e9
JK
243}
244
245static inline void
5d50e1d8 246hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
2a7cef2a 247{
5d50e1d8 248 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
b3837029
JK
249}
250
251static inline void
5d50e1d8 252hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
b3837029 253{
5d50e1d8 254 swap(*flags, elem->nomatch);
b3837029
JK
255}
256
b3837029
JK
257static inline void
258hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
259{
260 ip6_netmask(&elem->ip, cidr);
261 elem->cidr = cidr;
262}
263
264static bool
265hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
266{
2a7cef2a
JK
267 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
268
7cf7899d
DM
269 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
270 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
271 (flags &&
272 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
273 goto nla_put_failure;
b3837029
JK
274 return 0;
275
276nla_put_failure:
277 return 1;
278}
279
5d50e1d8
JK
280static inline void
281hash_net6_data_next(struct hash_net4_elem *next,
282 const struct hash_net6_elem *d)
b3837029 283{
b3837029
JK
284}
285
5d50e1d8 286#undef MTYPE
b3837029
JK
287#undef PF
288#undef HOST_MASK
289
5d50e1d8 290#define MTYPE hash_net6
b3837029
JK
291#define PF 6
292#define HOST_MASK 128
5d50e1d8
JK
293#define IP_SET_EMIT_CREATE
294#include "ip_set_hash_gen.h"
3d14b171 295
b3837029
JK
296static int
297hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 298 const struct xt_action_param *par,
5d50e1d8 299 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 300{
5d50e1d8 301 const struct hash_net *h = set->data;
b3837029 302 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 303 struct hash_net6_elem e = {
9b03a5ef
JK
304 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
305 };
5d50e1d8 306 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
b3837029 307
5d50e1d8 308 if (e.cidr == 0)
b3837029
JK
309 return -EINVAL;
310 if (adt == IPSET_TEST)
5d50e1d8 311 e.cidr = HOST_MASK;
b3837029 312
5d50e1d8
JK
313 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
314 ip6_netmask(&e.ip, e.cidr);
b3837029 315
5d50e1d8 316 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
317}
318
319static int
320hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 321 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 322{
5d50e1d8 323 const struct hash_net *h = set->data;
b3837029 324 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8
JK
325 struct hash_net6_elem e = { .cidr = HOST_MASK };
326 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
b3837029
JK
327 int ret;
328
329 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a
JK
330 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
331 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
b3837029 332 return -IPSET_ERR_PROTOCOL;
d0d9e0a5
JK
333 if (unlikely(tb[IPSET_ATTR_IP_TO]))
334 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
b3837029
JK
335
336 if (tb[IPSET_ATTR_LINENO])
337 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
338
5d50e1d8
JK
339 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
340 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
43c56e59
JK
360 return ip_set_enomatch(ret, flags, adt) ? 1 :
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,
10111a6e
JK
370 .revision_min = REVISION_MIN,
371 .revision_max = REVISION_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 },
b3837029
JK
387 },
388 .me = THIS_MODULE,
389};
390
391static int __init
392hash_net_init(void)
393{
394 return ip_set_type_register(&hash_net_type);
395}
396
397static void __exit
398hash_net_fini(void)
399{
400 ip_set_type_unregister(&hash_net_type);
401}
402
403module_init(hash_net_init);
404module_exit(hash_net_fini);