11 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
21 #include <linux/netfilter.h>
23 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
28 #define REVISION_MIN 0
31 #define REVISION_MAX 3
39 #define TYPE hash_ipportnet
42 hash_ipportnet_same_set(
const struct ip_set *
a,
const struct ip_set *
b);
44 #define hash_ipportnet4_same_set hash_ipportnet_same_set
45 #define hash_ipportnet6_same_set hash_ipportnet_same_set
53 #define IP_SET_HASH_WITH_NETS_PACKED
81 return ip1->
ip == ip2->
ip &&
91 return elem->
proto == 0;
98 memcpy(dst, src,
sizeof(*dst));
116 elem->
ip2 &= ip_set_netmask(cidr);
117 elem->
cidr = cidr - 1;
127 hash_ipportnet4_data_list(
struct sk_buff *
skb,
139 goto nla_put_failure;
147 hash_ipportnet4_data_tlist(
struct sk_buff *skb,
163 goto nla_put_failure;
170 #define IP_SET_HASH_WITH_PROTO
171 #define IP_SET_HASH_WITH_NETS
187 hash_ipportnet4_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
194 .
cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 :
HOST_MASK - 1
206 data.
ip2 &= ip_set_netmask(data.
cidr + 1);
208 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
219 u32 ip2_from, ip2_to, ip2_last,
ip2;
221 bool with_ports =
false;
233 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
244 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
247 data.
cidr = cidr - 1;
251 data.
port = nla_get_be16(tb[IPSET_ATTR_PORT]);
256 data.
proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
257 with_ports = ip_set_proto_with_ports(data.
proto);
270 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
276 flags |= (cadt_flags << 16);
284 data.
ip2 =
htonl(ip2_from & ip_set_hostmask(data.
cidr + 1));
285 ret = adtfn(
set, &data, timeout, flags);
286 return ip_set_eexist(ret, flags) ? 0 :
ret;
291 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
297 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
299 if (!cidr || cidr > 32)
306 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
312 if (tb[IPSET_ATTR_IP2_TO]) {
313 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
316 if (ip2_from > ip2_to)
317 swap(ip2_from, ip2_to);
326 for (; !before(ip_to, ip); ip++) {
330 for (; p <= port_to; p++) {
336 while (!
after(ip2, ip2_to)) {
340 data.
cidr = cidr - 1;
341 ret = adtfn(
set, &data, timeout, flags);
343 if (ret && !ip_set_eexist(ret, flags))
355 hash_ipportnet_same_set(
const struct ip_set *
a,
const struct ip_set *
b)
391 return ipv6_addr_cmp(&ip1->
ip.
in6, &ip2->
ip.
in6) == 0 &&
401 return elem->
proto == 0;
408 memcpy(dst, src,
sizeof(*dst));
432 ip->
ip6[0] &= ip_set_netmask6(prefix)[0];
433 ip->
ip6[1] &= ip_set_netmask6(prefix)[1];
434 ip->
ip6[2] &= ip_set_netmask6(prefix)[2];
435 ip->
ip6[3] &= ip_set_netmask6(prefix)[3];
441 ip6_netmask(&elem->
ip2, cidr);
442 elem->
cidr = cidr - 1;
446 hash_ipportnet6_data_list(
struct sk_buff *skb,
453 nla_put_net16(skb, IPSET_ATTR_PORT, data->
port) ||
454 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->
cidr + 1) ||
455 nla_put_u8(skb, IPSET_ATTR_PROTO, data->
proto) ||
458 goto nla_put_failure;
466 hash_ipportnet6_data_tlist(
struct sk_buff *skb,
475 nla_put_net16(skb, IPSET_ATTR_PORT, data->
port) ||
476 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->
cidr + 1) ||
477 nla_put_u8(skb, IPSET_ATTR_PROTO, data->
proto) ||
478 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
482 goto nla_put_failure;
493 #define HOST_MASK 128
504 hash_ipportnet6_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
511 .
cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 :
HOST_MASK - 1
523 ip6_netmask(&data.
ip2, data.
cidr + 1);
525 return adtfn(
set, &data, opt_timeout(opt, h), opt->
cmdflags);
529 hash_ipportnet6_uadt(
struct ip_set *
set,
struct nlattr *tb[],
537 bool with_ports =
false;
542 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
543 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
544 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
546 tb[IPSET_ATTR_IP_TO] ||
547 tb[IPSET_ATTR_CIDR]))
552 if (tb[IPSET_ATTR_LINENO])
553 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
563 if (tb[IPSET_ATTR_CIDR2]) {
564 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
567 data.
cidr = cidr - 1;
570 ip6_netmask(&data.
ip2, data.
cidr + 1);
572 if (tb[IPSET_ATTR_PORT])
573 data.
port = nla_get_be16(tb[IPSET_ATTR_PORT]);
577 if (tb[IPSET_ATTR_PROTO]) {
578 data.
proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
579 with_ports = ip_set_proto_with_ports(data.
proto);
589 if (tb[IPSET_ATTR_TIMEOUT]) {
592 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
598 flags |= (cadt_flags << 16);
601 if (adt ==
IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
602 ret = adtfn(
set, &data, timeout, flags);
603 return ip_set_eexist(ret, flags) ? 0 :
ret;
607 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
613 for (; port <= port_to; port++) {
615 ret = adtfn(
set, &data, timeout, flags);
617 if (ret && !ip_set_eexist(ret, flags))
628 hash_ipportnet_create(
struct ip_set *
set,
struct nlattr *tb[],
u32 flags)
640 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
644 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
650 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
652 h = kzalloc(
sizeof(*h)
662 hbits = htable_bits(hashsize);
663 hsize = htable_size(hbits);
673 h->
table->htable_bits = hbits;
677 if (tb[IPSET_ATTR_TIMEOUT]) {
678 h->
timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
681 ? &hash_ipportnet4_tvariant
682 : &hash_ipportnet6_tvariant;
685 hash_ipportnet4_gc_init(
set);
687 hash_ipportnet6_gc_init(
set);
690 ? &hash_ipportnet4_variant : &hash_ipportnet6_variant;
693 pr_debug(
"create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
701 .name =
"hash:ip,port,net",
709 .create = hash_ipportnet_create,
735 hash_ipportnet_init(
void)
741 hash_ipportnet_fini(
void)