]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/ipset/ip_set_hash_ipport.c
netfilter: ipset: Add /0 network support to hash:net,iface type
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / ipset / ip_set_hash_ipport.c
CommitLineData
07896ed3
JK
1/* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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:ip,port 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>
07896ed3
JK
15#include <linux/random.h>
16#include <net/ip.h>
17#include <net/ipv6.h>
18#include <net/netlink.h>
19#include <net/tcp.h>
20
21#include <linux/netfilter.h>
22#include <linux/netfilter/ipset/pfxlen.h>
23#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_timeout.h>
25#include <linux/netfilter/ipset/ip_set_getport.h>
26#include <linux/netfilter/ipset/ip_set_hash.h>
27
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
30MODULE_DESCRIPTION("hash:ip,port type of IP sets");
31MODULE_ALIAS("ip_set_hash:ip,port");
32
33/* Type specific function prefix */
34#define TYPE hash_ipport
35
36static bool
37hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b);
38
39#define hash_ipport4_same_set hash_ipport_same_set
40#define hash_ipport6_same_set hash_ipport_same_set
41
42/* The type variant functions: IPv4 */
43
44/* Member elements without timeout */
45struct hash_ipport4_elem {
46 __be32 ip;
47 __be16 port;
48 u8 proto;
49 u8 padding;
50};
51
52/* Member elements with timeout support */
53struct hash_ipport4_telem {
54 __be32 ip;
55 __be16 port;
56 u8 proto;
57 u8 padding;
58 unsigned long timeout;
59};
60
61static inline bool
62hash_ipport4_data_equal(const struct hash_ipport4_elem *ip1,
89dc79b7
JK
63 const struct hash_ipport4_elem *ip2,
64 u32 *multi)
07896ed3
JK
65{
66 return ip1->ip == ip2->ip &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto;
69}
70
71static inline bool
72hash_ipport4_data_isnull(const struct hash_ipport4_elem *elem)
73{
74 return elem->proto == 0;
75}
76
77static inline void
78hash_ipport4_data_copy(struct hash_ipport4_elem *dst,
79 const struct hash_ipport4_elem *src)
80{
81 dst->ip = src->ip;
82 dst->port = src->port;
83 dst->proto = src->proto;
84}
85
86static inline void
87hash_ipport4_data_zero_out(struct hash_ipport4_elem *elem)
88{
89 elem->proto = 0;
90}
91
92static bool
93hash_ipport4_data_list(struct sk_buff *skb,
94 const struct hash_ipport4_elem *data)
95{
7cf7899d
DM
96 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
97 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
98 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
99 goto nla_put_failure;
07896ed3
JK
100 return 0;
101
102nla_put_failure:
103 return 1;
104}
105
106static bool
107hash_ipport4_data_tlist(struct sk_buff *skb,
108 const struct hash_ipport4_elem *data)
109{
110 const struct hash_ipport4_telem *tdata =
111 (const struct hash_ipport4_telem *)data;
112
7cf7899d
DM
113 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
114 nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
115 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
116 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
117 htonl(ip_set_timeout_get(tdata->timeout))))
118 goto nla_put_failure;
07896ed3
JK
119 return 0;
120
121nla_put_failure:
122 return 1;
123}
124
125#define PF 4
126#define HOST_MASK 32
127#include <linux/netfilter/ipset/ip_set_ahash.h>
128
3d14b171
JK
129static inline void
130hash_ipport4_data_next(struct ip_set_hash *h,
131 const struct hash_ipport4_elem *d)
132{
6e27c9b4
JK
133 h->next.ip = d->ip;
134 h->next.port = d->port;
3d14b171
JK
135}
136
07896ed3
JK
137static int
138hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 139 const struct xt_action_param *par,
ac8cc925 140 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
07896ed3
JK
141{
142 const struct ip_set_hash *h = set->data;
143 ipset_adtfn adtfn = set->variant->adt[adt];
144 struct hash_ipport4_elem data = { };
145
ac8cc925 146 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
07896ed3
JK
147 &data.port, &data.proto))
148 return -EINVAL;
149
ac8cc925 150 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
07896ed3 151
ac8cc925 152 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
07896ed3
JK
153}
154
155static int
156hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 157 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
07896ed3
JK
158{
159 const struct ip_set_hash *h = set->data;
160 ipset_adtfn adtfn = set->variant->adt[adt];
161 struct hash_ipport4_elem data = { };
648ae8e5 162 u32 ip, ip_to = 0, p = 0, port, port_to;
07896ed3 163 u32 timeout = h->timeout;
5e0c1eb7 164 bool with_ports = false;
07896ed3
JK
165 int ret;
166
167 if (unlikely(!tb[IPSET_ATTR_IP] ||
168 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
169 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
170 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
171 return -IPSET_ERR_PROTOCOL;
172
173 if (tb[IPSET_ATTR_LINENO])
174 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
175
176 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
177 if (ret)
178 return ret;
179
180 if (tb[IPSET_ATTR_PORT])
181 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
182 else
183 return -IPSET_ERR_PROTOCOL;
184
185 if (tb[IPSET_ATTR_PROTO]) {
186 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
5e0c1eb7 187 with_ports = ip_set_proto_with_ports(data.proto);
07896ed3
JK
188
189 if (data.proto == 0)
190 return -IPSET_ERR_INVALID_PROTO;
191 } else
192 return -IPSET_ERR_MISSING_PROTO;
193
5e0c1eb7 194 if (!(with_ports || data.proto == IPPROTO_ICMP))
07896ed3 195 data.port = 0;
07896ed3
JK
196
197 if (tb[IPSET_ATTR_TIMEOUT]) {
198 if (!with_timeout(h->timeout))
199 return -IPSET_ERR_TIMEOUT;
200 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
201 }
202
203 if (adt == IPSET_TEST ||
07896ed3
JK
204 !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
205 tb[IPSET_ATTR_PORT_TO])) {
5416219e 206 ret = adtfn(set, &data, timeout, flags);
07896ed3
JK
207 return ip_set_eexist(ret, flags) ? 0 : ret;
208 }
209
210 ip = ntohl(data.ip);
211 if (tb[IPSET_ATTR_IP_TO]) {
212 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
213 if (ret)
214 return ret;
215 if (ip > ip_to)
216 swap(ip, ip_to);
217 } else if (tb[IPSET_ATTR_CIDR]) {
218 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
219
b9fed748 220 if (!cidr || cidr > 32)
07896ed3 221 return -IPSET_ERR_INVALID_CIDR;
e6146e86 222 ip_set_mask_from_to(ip, ip_to, cidr);
07896ed3
JK
223 } else
224 ip_to = ip;
225
5e0c1eb7
JK
226 port_to = port = ntohs(data.port);
227 if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
07896ed3
JK
228 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
229 if (port > port_to)
230 swap(port, port_to);
5e0c1eb7 231 }
07896ed3 232
3d14b171 233 if (retried)
6e27c9b4 234 ip = ntohl(h->next.ip);
3d14b171 235 for (; !before(ip_to, ip); ip++) {
6e27c9b4
JK
236 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
237 : port;
3d14b171 238 for (; p <= port_to; p++) {
07896ed3
JK
239 data.ip = htonl(ip);
240 data.port = htons(p);
5416219e 241 ret = adtfn(set, &data, timeout, flags);
07896ed3
JK
242
243 if (ret && !ip_set_eexist(ret, flags))
244 return ret;
245 else
246 ret = 0;
247 }
3d14b171 248 }
07896ed3
JK
249 return ret;
250}
251
252static bool
253hash_ipport_same_set(const struct ip_set *a, const struct ip_set *b)
254{
255 const struct ip_set_hash *x = a->data;
256 const struct ip_set_hash *y = b->data;
257
258 /* Resizing changes htable_bits, so we ignore it */
259 return x->maxelem == y->maxelem &&
260 x->timeout == y->timeout;
261}
262
263/* The type variant functions: IPv6 */
264
265struct hash_ipport6_elem {
266 union nf_inet_addr ip;
267 __be16 port;
268 u8 proto;
269 u8 padding;
270};
271
272struct hash_ipport6_telem {
273 union nf_inet_addr ip;
274 __be16 port;
275 u8 proto;
276 u8 padding;
277 unsigned long timeout;
278};
279
280static inline bool
281hash_ipport6_data_equal(const struct hash_ipport6_elem *ip1,
89dc79b7
JK
282 const struct hash_ipport6_elem *ip2,
283 u32 *multi)
07896ed3
JK
284{
285 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
286 ip1->port == ip2->port &&
287 ip1->proto == ip2->proto;
288}
289
290static inline bool
291hash_ipport6_data_isnull(const struct hash_ipport6_elem *elem)
292{
293 return elem->proto == 0;
294}
295
296static inline void
297hash_ipport6_data_copy(struct hash_ipport6_elem *dst,
298 const struct hash_ipport6_elem *src)
299{
300 memcpy(dst, src, sizeof(*dst));
301}
302
303static inline void
304hash_ipport6_data_zero_out(struct hash_ipport6_elem *elem)
305{
306 elem->proto = 0;
307}
308
309static bool
310hash_ipport6_data_list(struct sk_buff *skb,
311 const struct hash_ipport6_elem *data)
312{
7cf7899d
DM
313 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
314 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
315 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
316 goto nla_put_failure;
07896ed3
JK
317 return 0;
318
319nla_put_failure:
320 return 1;
321}
322
323static bool
324hash_ipport6_data_tlist(struct sk_buff *skb,
325 const struct hash_ipport6_elem *data)
326{
327 const struct hash_ipport6_telem *e =
328 (const struct hash_ipport6_telem *)data;
329
7cf7899d
DM
330 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
331 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
332 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
333 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
334 htonl(ip_set_timeout_get(e->timeout))))
335 goto nla_put_failure;
07896ed3
JK
336 return 0;
337
338nla_put_failure:
339 return 1;
340}
341
342#undef PF
343#undef HOST_MASK
344
345#define PF 6
346#define HOST_MASK 128
347#include <linux/netfilter/ipset/ip_set_ahash.h>
348
3d14b171
JK
349static inline void
350hash_ipport6_data_next(struct ip_set_hash *h,
351 const struct hash_ipport6_elem *d)
352{
6e27c9b4 353 h->next.port = d->port;
3d14b171
JK
354}
355
07896ed3
JK
356static int
357hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
b66554cf 358 const struct xt_action_param *par,
ac8cc925 359 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
07896ed3
JK
360{
361 const struct ip_set_hash *h = set->data;
362 ipset_adtfn adtfn = set->variant->adt[adt];
363 struct hash_ipport6_elem data = { };
364
ac8cc925 365 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
07896ed3
JK
366 &data.port, &data.proto))
367 return -EINVAL;
368
ac8cc925 369 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
07896ed3 370
ac8cc925 371 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
07896ed3
JK
372}
373
374static int
375hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
3d14b171 376 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
07896ed3
JK
377{
378 const struct ip_set_hash *h = set->data;
379 ipset_adtfn adtfn = set->variant->adt[adt];
380 struct hash_ipport6_elem data = { };
381 u32 port, port_to;
382 u32 timeout = h->timeout;
5e0c1eb7 383 bool with_ports = false;
07896ed3
JK
384 int ret;
385
386 if (unlikely(!tb[IPSET_ATTR_IP] ||
387 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
388 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
389 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
390 tb[IPSET_ATTR_IP_TO] ||
391 tb[IPSET_ATTR_CIDR]))
392 return -IPSET_ERR_PROTOCOL;
393
394 if (tb[IPSET_ATTR_LINENO])
395 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
396
397 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
398 if (ret)
399 return ret;
400
401 if (tb[IPSET_ATTR_PORT])
402 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
403 else
404 return -IPSET_ERR_PROTOCOL;
405
406 if (tb[IPSET_ATTR_PROTO]) {
407 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
5e0c1eb7 408 with_ports = ip_set_proto_with_ports(data.proto);
07896ed3
JK
409
410 if (data.proto == 0)
411 return -IPSET_ERR_INVALID_PROTO;
412 } else
413 return -IPSET_ERR_MISSING_PROTO;
414
5e0c1eb7 415 if (!(with_ports || data.proto == IPPROTO_ICMPV6))
07896ed3 416 data.port = 0;
07896ed3
JK
417
418 if (tb[IPSET_ATTR_TIMEOUT]) {
419 if (!with_timeout(h->timeout))
420 return -IPSET_ERR_TIMEOUT;
421 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
422 }
423
5e0c1eb7 424 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
5416219e 425 ret = adtfn(set, &data, timeout, flags);
07896ed3
JK
426 return ip_set_eexist(ret, flags) ? 0 : ret;
427 }
428
429 port = ntohs(data.port);
430 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
431 if (port > port_to)
432 swap(port, port_to);
433
3d14b171 434 if (retried)
6e27c9b4 435 port = ntohs(h->next.port);
07896ed3
JK
436 for (; port <= port_to; port++) {
437 data.port = htons(port);
5416219e 438 ret = adtfn(set, &data, timeout, flags);
07896ed3
JK
439
440 if (ret && !ip_set_eexist(ret, flags))
441 return ret;
442 else
443 ret = 0;
444 }
445 return ret;
446}
447
448/* Create hash:ip type of sets */
449
450static int
451hash_ipport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
452{
453 struct ip_set_hash *h;
454 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
455 u8 hbits;
26a5d3cc 456 size_t hsize;
07896ed3 457
c15f1c83 458 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
07896ed3
JK
459 return -IPSET_ERR_INVALID_FAMILY;
460
461 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
462 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
463 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
464 return -IPSET_ERR_PROTOCOL;
465
466 if (tb[IPSET_ATTR_HASHSIZE]) {
467 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
468 if (hashsize < IPSET_MIMINAL_HASHSIZE)
469 hashsize = IPSET_MIMINAL_HASHSIZE;
470 }
471
472 if (tb[IPSET_ATTR_MAXELEM])
473 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
474
475 h = kzalloc(sizeof(*h), GFP_KERNEL);
476 if (!h)
477 return -ENOMEM;
478
479 h->maxelem = maxelem;
480 get_random_bytes(&h->initval, sizeof(h->initval));
481 h->timeout = IPSET_NO_TIMEOUT;
482
483 hbits = htable_bits(hashsize);
26a5d3cc
JK
484 hsize = htable_size(hbits);
485 if (hsize == 0) {
486 kfree(h);
487 return -ENOMEM;
488 }
489 h->table = ip_set_alloc(hsize);
07896ed3
JK
490 if (!h->table) {
491 kfree(h);
492 return -ENOMEM;
493 }
494 h->table->htable_bits = hbits;
495
496 set->data = h;
497
498 if (tb[IPSET_ATTR_TIMEOUT]) {
499 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
500
c15f1c83 501 set->variant = set->family == NFPROTO_IPV4
07896ed3
JK
502 ? &hash_ipport4_tvariant : &hash_ipport6_tvariant;
503
c15f1c83 504 if (set->family == NFPROTO_IPV4)
07896ed3
JK
505 hash_ipport4_gc_init(set);
506 else
507 hash_ipport6_gc_init(set);
508 } else {
c15f1c83 509 set->variant = set->family == NFPROTO_IPV4
07896ed3
JK
510 ? &hash_ipport4_variant : &hash_ipport6_variant;
511 }
512
513 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
514 set->name, jhash_size(h->table->htable_bits),
515 h->table->htable_bits, h->maxelem, set->data, h->table);
516
517 return 0;
518}
519
520static struct ip_set_type hash_ipport_type __read_mostly = {
521 .name = "hash:ip,port",
522 .protocol = IPSET_PROTOCOL,
523 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
524 .dimension = IPSET_DIM_TWO,
c15f1c83 525 .family = NFPROTO_UNSPEC,
f1e00b39
JK
526 .revision_min = 0,
527 .revision_max = 1, /* SCTP and UDPLITE support added */
07896ed3
JK
528 .create = hash_ipport_create,
529 .create_policy = {
530 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
531 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
532 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
533 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
534 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
535 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
536 },
537 .adt_policy = {
538 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
539 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
540 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
541 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
542 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
543 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
544 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
545 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
546 },
547 .me = THIS_MODULE,
548};
549
550static int __init
551hash_ipport_init(void)
552{
553 return ip_set_type_register(&hash_ipport_type);
554}
555
556static void __exit
557hash_ipport_fini(void)
558{
559 ip_set_type_unregister(&hash_ipport_type);
560}
561
562module_init(hash_ipport_init);
563module_exit(hash_ipport_fini);