]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/netfilter/ipset/ip_set_hash_net.c
Merge tag 'jfs-3.12' of git://github.com/kleikamp/linux-shaggy
[mirror_ubuntu-eoan-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 */
00d71b27
JK
27/* 2 nomatch flag support added */
28#define REVISION_MAX 3 /* Counters support added */
10111a6e 29
b3837029
JK
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
10111a6e 32IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_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
5d50e1d8 39/* IPv4 variants */
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 49struct hash_net4t_elem {
b3837029
JK
50 __be32 ip;
51 u16 padding0;
2a7cef2a 52 u8 nomatch;
b3837029
JK
53 u8 cidr;
54 unsigned long timeout;
55};
56
00d71b27
JK
57struct hash_net4c_elem {
58 __be32 ip;
59 u16 padding0;
60 u8 nomatch;
61 u8 cidr;
62 struct ip_set_counter counter;
63};
64
65struct hash_net4ct_elem {
66 __be32 ip;
67 u16 padding0;
68 u8 nomatch;
69 u8 cidr;
70 struct ip_set_counter counter;
71 unsigned long timeout;
72};
73
5d50e1d8
JK
74/* Common functions */
75
b3837029
JK
76static inline bool
77hash_net4_data_equal(const struct hash_net4_elem *ip1,
89dc79b7
JK
78 const struct hash_net4_elem *ip2,
79 u32 *multi)
b3837029 80{
2a7cef2a
JK
81 return ip1->ip == ip2->ip &&
82 ip1->cidr == ip2->cidr;
b3837029
JK
83}
84
5d50e1d8
JK
85static inline int
86hash_net4_do_data_match(const struct hash_net4_elem *elem)
b3837029 87{
5d50e1d8 88 return elem->nomatch ? -ENOTEMPTY : 1;
2a7cef2a
JK
89}
90
91static inline void
5d50e1d8 92hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags)
2a7cef2a 93{
5d50e1d8 94 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
6eb4c7e9
JK
95}
96
97static inline void
5d50e1d8 98hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags)
2a7cef2a 99{
5d50e1d8 100 swap(*flags, elem->nomatch);
b3837029
JK
101}
102
103static inline void
104hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
105{
106 elem->ip &= ip_set_netmask(cidr);
107 elem->cidr = cidr;
108}
109
b3837029
JK
110static bool
111hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
112{
2a7cef2a
JK
113 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
114
7cf7899d
DM
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
116 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
117 (flags &&
118 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
119 goto nla_put_failure;
b3837029
JK
120 return 0;
121
122nla_put_failure:
123 return 1;
124}
125
5d50e1d8
JK
126static inline void
127hash_net4_data_next(struct hash_net4_elem *next,
128 const struct hash_net4_elem *d)
b3837029 129{
5d50e1d8 130 next->ip = d->ip;
b3837029
JK
131}
132
5d50e1d8 133#define MTYPE hash_net4
b3837029
JK
134#define PF 4
135#define HOST_MASK 32
5d50e1d8 136#include "ip_set_hash_gen.h"
3d14b171 137
b3837029
JK
138static int
139hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 140 const struct xt_action_param *par,
5d50e1d8 141 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 142{
5d50e1d8 143 const struct hash_net *h = set->data;
b3837029 144 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 145 struct hash_net4_elem e = {
9b03a5ef
JK
146 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
147 };
5d50e1d8 148 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
b3837029 149
5d50e1d8 150 if (e.cidr == 0)
b3837029
JK
151 return -EINVAL;
152 if (adt == IPSET_TEST)
5d50e1d8 153 e.cidr = HOST_MASK;
b3837029 154
5d50e1d8
JK
155 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
156 e.ip &= ip_set_netmask(e.cidr);
b3837029 157
5d50e1d8 158 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
159}
160
161static int
162hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 163 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 164{
5d50e1d8 165 const struct hash_net *h = set->data;
b3837029 166 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8
JK
167 struct hash_net4_elem e = { .cidr = HOST_MASK };
168 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
d0d9e0a5 169 u32 ip = 0, ip_to, last;
b3837029
JK
170 int ret;
171
172 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a 173 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
00d71b27
JK
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
175 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
176 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
b3837029
JK
177 return -IPSET_ERR_PROTOCOL;
178
179 if (tb[IPSET_ATTR_LINENO])
180 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
181
5d50e1d8
JK
182 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
183 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
184 if (ret)
185 return ret;
186
d0d9e0a5 187 if (tb[IPSET_ATTR_CIDR]) {
5d50e1d8
JK
188 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
189 if (!e.cidr || e.cidr > HOST_MASK)
d0d9e0a5
JK
190 return -IPSET_ERR_INVALID_CIDR;
191 }
b3837029 192
43c56e59 193 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
194 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
195 if (cadt_flags & IPSET_FLAG_NOMATCH)
43c56e59 196 flags |= (IPSET_FLAG_NOMATCH << 16);
2a7cef2a
JK
197 }
198
d0d9e0a5 199 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
5d50e1d8
JK
200 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
201 ret = adtfn(set, &e, &ext, &ext, flags);
0f1799ba 202 return ip_set_enomatch(ret, flags, adt, set) ? -ret:
43c56e59 203 ip_set_eexist(ret, flags) ? 0 : ret;
d0d9e0a5 204 }
b3837029 205
d0d9e0a5
JK
206 ip_to = ip;
207 if (tb[IPSET_ATTR_IP_TO]) {
208 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
209 if (ret)
210 return ret;
211 if (ip_to < ip)
212 swap(ip, ip_to);
213 if (ip + UINT_MAX == ip_to)
214 return -IPSET_ERR_HASH_RANGE;
215 }
216 if (retried)
6e27c9b4 217 ip = ntohl(h->next.ip);
d0d9e0a5 218 while (!after(ip, ip_to)) {
5d50e1d8
JK
219 e.ip = htonl(ip);
220 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
221 ret = adtfn(set, &e, &ext, &ext, flags);
d0d9e0a5
JK
222 if (ret && !ip_set_eexist(ret, flags))
223 return ret;
224 else
225 ret = 0;
226 ip = last + 1;
227 }
228 return ret;
b3837029
JK
229}
230
5d50e1d8 231/* IPv6 variants */
b3837029
JK
232
233struct hash_net6_elem {
234 union nf_inet_addr ip;
235 u16 padding0;
2a7cef2a 236 u8 nomatch;
b3837029
JK
237 u8 cidr;
238};
239
5d50e1d8 240struct hash_net6t_elem {
b3837029
JK
241 union nf_inet_addr ip;
242 u16 padding0;
2a7cef2a 243 u8 nomatch;
b3837029
JK
244 u8 cidr;
245 unsigned long timeout;
246};
247
00d71b27
JK
248struct hash_net6c_elem {
249 union nf_inet_addr ip;
250 u16 padding0;
251 u8 nomatch;
252 u8 cidr;
253 struct ip_set_counter counter;
254};
255
256struct hash_net6ct_elem {
257 union nf_inet_addr ip;
258 u16 padding0;
259 u8 nomatch;
260 u8 cidr;
261 struct ip_set_counter counter;
262 unsigned long timeout;
263};
264
5d50e1d8
JK
265/* Common functions */
266
b3837029
JK
267static inline bool
268hash_net6_data_equal(const struct hash_net6_elem *ip1,
89dc79b7
JK
269 const struct hash_net6_elem *ip2,
270 u32 *multi)
b3837029 271{
29e3b160 272 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
b3837029
JK
273 ip1->cidr == ip2->cidr;
274}
275
5d50e1d8
JK
276static inline int
277hash_net6_do_data_match(const struct hash_net6_elem *elem)
2a7cef2a 278{
5d50e1d8 279 return elem->nomatch ? -ENOTEMPTY : 1;
6eb4c7e9
JK
280}
281
282static inline void
5d50e1d8 283hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
2a7cef2a 284{
5d50e1d8 285 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
b3837029
JK
286}
287
288static inline void
5d50e1d8 289hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
b3837029 290{
5d50e1d8 291 swap(*flags, elem->nomatch);
b3837029
JK
292}
293
b3837029
JK
294static inline void
295hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
296{
297 ip6_netmask(&elem->ip, cidr);
298 elem->cidr = cidr;
299}
300
301static bool
302hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
303{
2a7cef2a
JK
304 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
305
7cf7899d
DM
306 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
307 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
308 (flags &&
309 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
310 goto nla_put_failure;
b3837029
JK
311 return 0;
312
313nla_put_failure:
314 return 1;
315}
316
5d50e1d8
JK
317static inline void
318hash_net6_data_next(struct hash_net4_elem *next,
319 const struct hash_net6_elem *d)
b3837029 320{
b3837029
JK
321}
322
5d50e1d8 323#undef MTYPE
b3837029
JK
324#undef PF
325#undef HOST_MASK
326
5d50e1d8 327#define MTYPE hash_net6
b3837029
JK
328#define PF 6
329#define HOST_MASK 128
5d50e1d8
JK
330#define IP_SET_EMIT_CREATE
331#include "ip_set_hash_gen.h"
3d14b171 332
b3837029
JK
333static int
334hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 335 const struct xt_action_param *par,
5d50e1d8 336 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 337{
5d50e1d8 338 const struct hash_net *h = set->data;
b3837029 339 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 340 struct hash_net6_elem e = {
9b03a5ef
JK
341 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
342 };
5d50e1d8 343 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h);
b3837029 344
5d50e1d8 345 if (e.cidr == 0)
b3837029
JK
346 return -EINVAL;
347 if (adt == IPSET_TEST)
5d50e1d8 348 e.cidr = HOST_MASK;
b3837029 349
5d50e1d8
JK
350 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
351 ip6_netmask(&e.ip, e.cidr);
b3837029 352
5d50e1d8 353 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
354}
355
356static int
357hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 358 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 359{
5d50e1d8 360 const struct hash_net *h = set->data;
b3837029 361 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8
JK
362 struct hash_net6_elem e = { .cidr = HOST_MASK };
363 struct ip_set_ext ext = IP_SET_INIT_UEXT(h);
b3837029
JK
364 int ret;
365
366 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a 367 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
00d71b27
JK
368 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
369 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
370 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
b3837029 371 return -IPSET_ERR_PROTOCOL;
d0d9e0a5
JK
372 if (unlikely(tb[IPSET_ATTR_IP_TO]))
373 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
b3837029
JK
374
375 if (tb[IPSET_ATTR_LINENO])
376 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
377
5d50e1d8
JK
378 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
379 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
380 if (ret)
381 return ret;
382
383 if (tb[IPSET_ATTR_CIDR])
5d50e1d8 384 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
b3837029 385
5d50e1d8 386 if (!e.cidr || e.cidr > HOST_MASK)
b3837029
JK
387 return -IPSET_ERR_INVALID_CIDR;
388
5d50e1d8 389 ip6_netmask(&e.ip, e.cidr);
b3837029 390
43c56e59 391 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
392 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
393 if (cadt_flags & IPSET_FLAG_NOMATCH)
43c56e59 394 flags |= (IPSET_FLAG_NOMATCH << 16);
2a7cef2a
JK
395 }
396
5d50e1d8 397 ret = adtfn(set, &e, &ext, &ext, flags);
b3837029 398
0f1799ba 399 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
43c56e59 400 ip_set_eexist(ret, flags) ? 0 : ret;
b3837029
JK
401}
402
b3837029
JK
403static struct ip_set_type hash_net_type __read_mostly = {
404 .name = "hash:net",
405 .protocol = IPSET_PROTOCOL,
3e0304a5 406 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
b3837029 407 .dimension = IPSET_DIM_ONE,
c15f1c83 408 .family = NFPROTO_UNSPEC,
10111a6e
JK
409 .revision_min = REVISION_MIN,
410 .revision_max = REVISION_MAX,
b3837029
JK
411 .create = hash_net_create,
412 .create_policy = {
413 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
414 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
415 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
416 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
417 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
5d50e1d8 418 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
b3837029
JK
419 },
420 .adt_policy = {
421 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
d0d9e0a5 422 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
b3837029
JK
423 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
424 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
2a7cef2a 425 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
00d71b27
JK
426 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
427 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
b3837029
JK
428 },
429 .me = THIS_MODULE,
430};
431
432static int __init
433hash_net_init(void)
434{
435 return ip_set_type_register(&hash_net_type);
436}
437
438static void __exit
439hash_net_fini(void)
440{
441 ip_set_type_unregister(&hash_net_type);
442}
443
444module_init(hash_net_init);
445module_exit(hash_net_fini);