11 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
20 #include <linux/netfilter.h>
22 #include <linux/netfilter/ipset/ip_set.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
27 #define REVISION_MIN 0
30 #define REVISION_MAX 3
38 #define TYPE hash_netport
41 hash_netport_same_set(
const struct ip_set *
a,
const struct ip_set *
b);
43 #define hash_netport4_same_set hash_netport_same_set
44 #define hash_netport6_same_set hash_netport_same_set
52 #define IP_SET_HASH_WITH_NETS_PACKED
78 return ip1->
ip == ip2->
ip &&
87 return elem->
proto == 0;
116 elem->
ip &= ip_set_netmask(cidr);
117 elem->
cidr = cidr - 1;
138 goto nla_put_failure;
146 hash_netport4_data_tlist(
struct sk_buff *skb,
161 goto nla_put_failure;
168 #define IP_SET_HASH_WITH_PROTO
169 #define IP_SET_HASH_WITH_NETS
184 hash_netport4_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
191 .
cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 :
HOST_MASK - 1
202 data.
ip &= ip_set_netmask(data.
cidr + 1);
204 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
216 bool with_ports =
false;
228 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
235 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
238 data.
cidr = cidr - 1;
242 data.
port = nla_get_be16(tb[IPSET_ATTR_PORT]);
247 data.
proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
248 with_ports = ip_set_proto_with_ports(data.
proto);
261 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
269 flags |= (cadt_flags << 16);
274 ret = adtfn(
set, &data, timeout, flags);
275 return ip_set_eexist(ret, flags) ? 0 :
ret;
280 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
284 if (tb[IPSET_ATTR_IP_TO]) {
285 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
301 data.
cidr = cidr - 1;
304 for (; p <= port_to; p++) {
306 ret = adtfn(
set, &data, timeout, flags);
308 if (ret && !ip_set_eexist(ret, flags))
319 hash_netport_same_set(
const struct ip_set *
a,
const struct ip_set *
b)
353 return ipv6_addr_cmp(&ip1->
ip.
in6, &ip2->
ip.
in6) == 0 &&
362 return elem->
proto == 0;
369 memcpy(dst, src,
sizeof(*dst));
393 ip->
ip6[0] &= ip_set_netmask6(prefix)[0];
394 ip->
ip6[1] &= ip_set_netmask6(prefix)[1];
395 ip->
ip6[2] &= ip_set_netmask6(prefix)[2];
396 ip->
ip6[3] &= ip_set_netmask6(prefix)[3];
402 ip6_netmask(&elem->
ip, cidr);
403 elem->
cidr = cidr - 1;
407 hash_netport6_data_list(
struct sk_buff *skb,
413 nla_put_net16(skb, IPSET_ATTR_PORT, data->
port) ||
414 nla_put_u8(skb, IPSET_ATTR_CIDR, data->
cidr + 1) ||
415 nla_put_u8(skb, IPSET_ATTR_PROTO, data->
proto) ||
418 goto nla_put_failure;
426 hash_netport6_data_tlist(
struct sk_buff *skb,
434 nla_put_net16(skb, IPSET_ATTR_PORT, data->
port) ||
435 nla_put_u8(skb, IPSET_ATTR_CIDR, data->
cidr + 1) ||
436 nla_put_u8(skb, IPSET_ATTR_PROTO, data->
proto) ||
437 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
441 goto nla_put_failure;
452 #define HOST_MASK 128
463 hash_netport6_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
470 .
cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 :
HOST_MASK - 1,
481 ip6_netmask(&data.
ip, data.
cidr + 1);
483 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
487 hash_netport6_uadt(
struct ip_set *
set,
struct nlattr *tb[],
495 bool with_ports =
false;
500 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
501 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
502 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
508 if (tb[IPSET_ATTR_LINENO])
509 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
515 if (tb[IPSET_ATTR_CIDR]) {
516 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
519 data.
cidr = cidr - 1;
521 ip6_netmask(&data.
ip, data.
cidr + 1);
523 if (tb[IPSET_ATTR_PORT])
524 data.
port = nla_get_be16(tb[IPSET_ATTR_PORT]);
528 if (tb[IPSET_ATTR_PROTO]) {
529 data.
proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
530 with_ports = ip_set_proto_with_ports(data.
proto);
540 if (tb[IPSET_ATTR_TIMEOUT]) {
543 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
549 flags |= (cadt_flags << 16);
552 if (adt ==
IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
553 ret = adtfn(
set, &data, timeout, flags);
554 return ip_set_eexist(ret, flags) ? 0 :
ret;
558 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
564 for (; port <= port_to; port++) {
566 ret = adtfn(
set, &data, timeout, flags);
568 if (ret && !ip_set_eexist(ret, flags))
579 hash_netport_create(
struct ip_set *
set,
struct nlattr *tb[],
u32 flags)
591 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
595 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
601 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
603 h = kzalloc(
sizeof(*h)
613 hbits = htable_bits(hashsize);
614 hsize = htable_size(hbits);
624 h->
table->htable_bits = hbits;
628 if (tb[IPSET_ATTR_TIMEOUT]) {
629 h->
timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
632 ? &hash_netport4_tvariant : &hash_netport6_tvariant;
635 hash_netport4_gc_init(
set);
637 hash_netport6_gc_init(
set);
640 ? &hash_netport4_variant : &hash_netport6_variant;
643 pr_debug(
"create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
651 .name =
"hash:net,port",
658 .create = hash_netport_create,
682 hash_netport_init(
void)
688 hash_netport_fini(
void)