]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/netfilter/ipset/ip_set_hash_net.c
netfilter: ipset: Support comments in the list-type ipset.
[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 */
28#define IPSET_TYPE_REV_MAX 3 /* Counters 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;
b3837029
JK
95 return 0;
96
97nla_put_failure:
98 return 1;
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
JK
109#define PF 4
110#define HOST_MASK 32
5d50e1d8 111#include "ip_set_hash_gen.h"
3d14b171 112
b3837029
JK
113static int
114hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 115 const struct xt_action_param *par,
5d50e1d8 116 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 117{
5d50e1d8 118 const struct hash_net *h = set->data;
b3837029 119 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 120 struct hash_net4_elem e = {
a04d8b6b 121 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
9b03a5ef 122 };
ca134ce8 123 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
b3837029 124
5d50e1d8 125 if (e.cidr == 0)
b3837029
JK
126 return -EINVAL;
127 if (adt == IPSET_TEST)
5d50e1d8 128 e.cidr = HOST_MASK;
b3837029 129
5d50e1d8
JK
130 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
131 e.ip &= ip_set_netmask(e.cidr);
b3837029 132
5d50e1d8 133 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
134}
135
136static int
137hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 138 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 139{
5d50e1d8 140 const struct hash_net *h = set->data;
b3837029 141 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 142 struct hash_net4_elem e = { .cidr = HOST_MASK };
ca134ce8 143 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
20b2fab4 144 u32 ip = 0, ip_to = 0, last;
b3837029
JK
145 int ret;
146
147 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a 148 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
00d71b27
JK
149 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
150 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
151 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
b3837029
JK
152 return -IPSET_ERR_PROTOCOL;
153
154 if (tb[IPSET_ATTR_LINENO])
155 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
156
5d50e1d8
JK
157 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
158 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
159 if (ret)
160 return ret;
161
d0d9e0a5 162 if (tb[IPSET_ATTR_CIDR]) {
5d50e1d8
JK
163 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
164 if (!e.cidr || e.cidr > HOST_MASK)
d0d9e0a5
JK
165 return -IPSET_ERR_INVALID_CIDR;
166 }
b3837029 167
43c56e59 168 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
169 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
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);
0f1799ba 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);
d0d9e0a5 193 while (!after(ip, ip_to)) {
5d50e1d8
JK
194 e.ip = htonl(ip);
195 last = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
196 ret = adtfn(set, &e, &ext, &ext, flags);
d0d9e0a5
JK
197 if (ret && !ip_set_eexist(ret, flags))
198 return ret;
199 else
200 ret = 0;
201 ip = last + 1;
202 }
203 return ret;
b3837029
JK
204}
205
03c8b234 206/* IPv6 variant */
b3837029
JK
207
208struct hash_net6_elem {
209 union nf_inet_addr ip;
210 u16 padding0;
2a7cef2a 211 u8 nomatch;
b3837029
JK
212 u8 cidr;
213};
214
5d50e1d8
JK
215/* Common functions */
216
b3837029
JK
217static inline bool
218hash_net6_data_equal(const struct hash_net6_elem *ip1,
89dc79b7
JK
219 const struct hash_net6_elem *ip2,
220 u32 *multi)
b3837029 221{
29e3b160 222 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
b3837029
JK
223 ip1->cidr == ip2->cidr;
224}
225
5d50e1d8
JK
226static inline int
227hash_net6_do_data_match(const struct hash_net6_elem *elem)
2a7cef2a 228{
5d50e1d8 229 return elem->nomatch ? -ENOTEMPTY : 1;
6eb4c7e9
JK
230}
231
232static inline void
5d50e1d8 233hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
2a7cef2a 234{
5d50e1d8 235 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
b3837029
JK
236}
237
238static inline void
5d50e1d8 239hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
b3837029 240{
5d50e1d8 241 swap(*flags, elem->nomatch);
b3837029
JK
242}
243
b3837029
JK
244static inline void
245hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
246{
247 ip6_netmask(&elem->ip, cidr);
248 elem->cidr = cidr;
249}
250
251static bool
252hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
253{
2a7cef2a
JK
254 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
255
7cf7899d
DM
256 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
257 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
258 (flags &&
259 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
260 goto nla_put_failure;
b3837029
JK
261 return 0;
262
263nla_put_failure:
264 return 1;
265}
266
5d50e1d8
JK
267static inline void
268hash_net6_data_next(struct hash_net4_elem *next,
269 const struct hash_net6_elem *d)
b3837029 270{
b3837029
JK
271}
272
5d50e1d8 273#undef MTYPE
b3837029
JK
274#undef PF
275#undef HOST_MASK
276
5d50e1d8 277#define MTYPE hash_net6
b3837029
JK
278#define PF 6
279#define HOST_MASK 128
5d50e1d8
JK
280#define IP_SET_EMIT_CREATE
281#include "ip_set_hash_gen.h"
3d14b171 282
b3837029
JK
283static int
284hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 285 const struct xt_action_param *par,
5d50e1d8 286 enum ipset_adt adt, struct ip_set_adt_opt *opt)
b3837029 287{
5d50e1d8 288 const struct hash_net *h = set->data;
b3837029 289 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 290 struct hash_net6_elem e = {
a04d8b6b 291 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
9b03a5ef 292 };
ca134ce8 293 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
b3837029 294
5d50e1d8 295 if (e.cidr == 0)
b3837029
JK
296 return -EINVAL;
297 if (adt == IPSET_TEST)
5d50e1d8 298 e.cidr = HOST_MASK;
b3837029 299
5d50e1d8
JK
300 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
301 ip6_netmask(&e.ip, e.cidr);
b3837029 302
5d50e1d8 303 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
b3837029
JK
304}
305
306static int
307hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 308 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
b3837029 309{
b3837029 310 ipset_adtfn adtfn = set->variant->adt[adt];
5d50e1d8 311 struct hash_net6_elem e = { .cidr = HOST_MASK };
ca134ce8 312 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
b3837029
JK
313 int ret;
314
315 if (unlikely(!tb[IPSET_ATTR_IP] ||
2a7cef2a 316 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
00d71b27
JK
317 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
318 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
319 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
b3837029 320 return -IPSET_ERR_PROTOCOL;
d0d9e0a5
JK
321 if (unlikely(tb[IPSET_ATTR_IP_TO]))
322 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
b3837029
JK
323
324 if (tb[IPSET_ATTR_LINENO])
325 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
326
5d50e1d8
JK
327 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
328 ip_set_get_extensions(set, tb, &ext);
b3837029
JK
329 if (ret)
330 return ret;
331
332 if (tb[IPSET_ATTR_CIDR])
5d50e1d8 333 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
b3837029 334
5d50e1d8 335 if (!e.cidr || e.cidr > HOST_MASK)
b3837029
JK
336 return -IPSET_ERR_INVALID_CIDR;
337
5d50e1d8 338 ip6_netmask(&e.ip, e.cidr);
b3837029 339
43c56e59 340 if (tb[IPSET_ATTR_CADT_FLAGS]) {
2a7cef2a
JK
341 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
342 if (cadt_flags & IPSET_FLAG_NOMATCH)
43c56e59 343 flags |= (IPSET_FLAG_NOMATCH << 16);
2a7cef2a
JK
344 }
345
5d50e1d8 346 ret = adtfn(set, &e, &ext, &ext, flags);
b3837029 347
0f1799ba 348 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
43c56e59 349 ip_set_eexist(ret, flags) ? 0 : ret;
b3837029
JK
350}
351
b3837029
JK
352static struct ip_set_type hash_net_type __read_mostly = {
353 .name = "hash:net",
354 .protocol = IPSET_PROTOCOL,
3e0304a5 355 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
b3837029 356 .dimension = IPSET_DIM_ONE,
c15f1c83 357 .family = NFPROTO_UNSPEC,
35b8dcf8
JK
358 .revision_min = IPSET_TYPE_REV_MIN,
359 .revision_max = IPSET_TYPE_REV_MAX,
b3837029
JK
360 .create = hash_net_create,
361 .create_policy = {
362 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
363 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
364 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
365 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
366 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
5d50e1d8 367 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
b3837029
JK
368 },
369 .adt_policy = {
370 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
d0d9e0a5 371 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
b3837029
JK
372 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
373 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
2a7cef2a 374 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
00d71b27
JK
375 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
376 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
b3837029
JK
377 },
378 .me = THIS_MODULE,
379};
380
381static int __init
382hash_net_init(void)
383{
384 return ip_set_type_register(&hash_net_type);
385}
386
387static void __exit
388hash_net_fini(void)
389{
390 ip_set_type_unregister(&hash_net_type);
391}
392
393module_init(hash_net_init);
394module_exit(hash_net_fini);