]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/netfilter/ipset/ip_set_hash_net.c
netfilter: ipset: Preprocessor directices cleanup
[thirdparty/kernel/stable.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;
b3837029
JK
98 return 0;
99
100nla_put_failure:
101 return 1;
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
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);
0f1799ba 182 return ip_set_enomatch(ret, flags, adt, set) ? -ret:
43c56e59 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
03c8b234 211/* IPv6 variant */
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
JK
220/* Common functions */
221
b3837029
JK
222static inline bool
223hash_net6_data_equal(const struct hash_net6_elem *ip1,
89dc79b7
JK
224 const struct hash_net6_elem *ip2,
225 u32 *multi)
b3837029 226{
29e3b160 227 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
b3837029
JK
228 ip1->cidr == ip2->cidr;
229}
230
5d50e1d8
JK
231static inline int
232hash_net6_do_data_match(const struct hash_net6_elem *elem)
2a7cef2a 233{
5d50e1d8 234 return elem->nomatch ? -ENOTEMPTY : 1;
6eb4c7e9
JK
235}
236
237static inline void
5d50e1d8 238hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
2a7cef2a 239{
5d50e1d8 240 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
b3837029
JK
241}
242
243static inline void
5d50e1d8 244hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
b3837029 245{
5d50e1d8 246 swap(*flags, elem->nomatch);
b3837029
JK
247}
248
b3837029
JK
249static inline void
250hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
251{
252 ip6_netmask(&elem->ip, cidr);
253 elem->cidr = cidr;
254}
255
256static bool
257hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
258{
2a7cef2a
JK
259 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
260
7cf7899d
DM
261 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
262 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
263 (flags &&
264 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
265 goto nla_put_failure;
b3837029
JK
266 return 0;
267
268nla_put_failure:
269 return 1;
270}
271
5d50e1d8
JK
272static inline void
273hash_net6_data_next(struct hash_net4_elem *next,
274 const struct hash_net6_elem *d)
b3837029 275{
b3837029
JK
276}
277
5d50e1d8 278#undef MTYPE
b3837029
JK
279#undef HOST_MASK
280
5d50e1d8 281#define MTYPE hash_net6
b3837029 282#define HOST_MASK 128
5d50e1d8
JK
283#define IP_SET_EMIT_CREATE
284#include "ip_set_hash_gen.h"
3d14b171 285
b3837029
JK
286static int
287hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 288 const struct xt_action_param *par,
5d50e1d8 289 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 290{
5d50e1d8 291 const struct hash_net *h = set->data;
b3837029 292 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 293 struct hash_net6_elem e = {
a04d8b6b 294 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
9b03a5ef 295 };
ca134ce8 296 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
b3837029 297
5d50e1d8 298 if (e.cidr == 0)
b3837029
JK
299 return -EINVAL;
300 if (adt == IPSET_TEST)
5d50e1d8 301 e.cidr = HOST_MASK;
b3837029 302
5d50e1d8
JK
303 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
304 ip6_netmask(&e.ip, e.cidr);
b3837029 305
5d50e1d8 306 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
307}
308
309static int
310hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 311 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 312{
b3837029 313 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 314 struct hash_net6_elem e = { .cidr = HOST_MASK };
ca134ce8 315 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
b3837029
JK
316 int ret;
317
318 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a 319 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
00d71b27
JK
320 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
321 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
af331419
AD
322 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
323 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
324 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
325 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
b3837029 326 return -IPSET_ERR_PROTOCOL;
d0d9e0a5
JK
327 if (unlikely(tb[IPSET_ATTR_IP_TO]))
328 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
b3837029
JK
329
330 if (tb[IPSET_ATTR_LINENO])
331 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
332
5d50e1d8
JK
333 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
334 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
335 if (ret)
336 return ret;
337
338 if (tb[IPSET_ATTR_CIDR])
5d50e1d8 339 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
b3837029 340
5d50e1d8 341 if (!e.cidr || e.cidr > HOST_MASK)
b3837029
JK
342 return -IPSET_ERR_INVALID_CIDR;
343
5d50e1d8 344 ip6_netmask(&e.ip, e.cidr);
b3837029 345
43c56e59 346 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
347 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
348 if (cadt_flags & IPSET_FLAG_NOMATCH)
43c56e59 349 flags |= (IPSET_FLAG_NOMATCH << 16);
2a7cef2a
JK
350 }
351
5d50e1d8 352 ret = adtfn(set, &e, &ext, &ext, flags);
b3837029 353
0f1799ba 354 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
43c56e59 355 ip_set_eexist(ret, flags) ? 0 : ret;
b3837029
JK
356}
357
b3837029
JK
358static struct ip_set_type hash_net_type __read_mostly = {
359 .name = "hash:net",
360 .protocol = IPSET_PROTOCOL,
3e0304a5 361 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
b3837029 362 .dimension = IPSET_DIM_ONE,
c15f1c83 363 .family = NFPROTO_UNSPEC,
35b8dcf8
JK
364 .revision_min = IPSET_TYPE_REV_MIN,
365 .revision_max = IPSET_TYPE_REV_MAX,
b3837029
JK
366 .create = hash_net_create,
367 .create_policy = {
368 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
369 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
370 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
371 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
372 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
5d50e1d8 373 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
b3837029
JK
374 },
375 .adt_policy = {
376 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
d0d9e0a5 377 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
b3837029
JK
378 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
379 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
2a7cef2a 380 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
00d71b27
JK
381 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
382 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
fda75c6d 383 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
af331419
AD
384 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
385 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
386 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
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);