Skip to content

Commit ea53ac5

Browse files
OliproJozsef Kadlecsik
authored andcommitted
netfilter: ipset: Add hash:net,net module to kernel.
This adds a new set that provides the ability to configure pairs of subnets. A small amount of additional handling code has been added to the generic hash header file - this code is conditionally activated by a preprocessor definition. Signed-off-by: Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa> Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1 parent d9628bb commit ea53ac5

4 files changed

Lines changed: 541 additions & 9 deletions

File tree

‎net/netfilter/ipset/Kconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ config IP_SET_HASH_NET
9999

100100
To compile it as a module, choose M here. If unsure, say N.
101101

102+
config IP_SET_HASH_NETNET
103+
tristate "hash:net,net set support"
104+
depends on IP_SET
105+
help
106+
This option adds the hash:net,net set type support, by which
107+
one can store IPv4/IPv6 network address/prefix pairs in a set.
108+
109+
To compile it as a module, choose M here. If unsure, say N.
110+
102111
config IP_SET_HASH_NETPORT
103112
tristate "hash:net,port set support"
104113
depends on IP_SET

‎net/netfilter/ipset/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ obj-$(CONFIG_IP_SET_HASH_IPPORTNET) += ip_set_hash_ipportnet.o
2020
obj-$(CONFIG_IP_SET_HASH_NET) += ip_set_hash_net.o
2121
obj-$(CONFIG_IP_SET_HASH_NETPORT) += ip_set_hash_netport.o
2222
obj-$(CONFIG_IP_SET_HASH_NETIFACE) += ip_set_hash_netiface.o
23+
obj-$(CONFIG_IP_SET_HASH_NETNET) += ip_set_hash_netnet.o
2324

2425
# list types
2526
obj-$(CONFIG_IP_SET_LIST_SET) += ip_set_list_set.o

‎net/netfilter/ipset/ip_set_hash_gen.h‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
142142
}
143143

144144
#ifdef IP_SET_HASH_WITH_NETS
145+
#if IPSET_NET_COUNT > 1
146+
#define __CIDR(cidr, i) (cidr[i])
147+
#else
148+
#define __CIDR(cidr, i) (cidr)
149+
#endif
145150
#ifdef IP_SET_HASH_WITH_NETS_PACKED
146151
/* When cidr is packed with nomatch, cidr - 1 is stored in the entry */
147-
#define CIDR(cidr) (cidr + 1)
152+
#define CIDR(cidr, i) (__CIDR(cidr, i) + 1)
148153
#else
149-
#define CIDR(cidr) (cidr)
154+
#define CIDR(cidr, i) (__CIDR(cidr, i))
150155
#endif
151156

152157
#define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
@@ -210,6 +215,7 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
210215
#define mtype_do_data_match(d) 1
211216
#endif
212217
#define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
218+
#define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
213219
#define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
214220
#define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
215221
#define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
@@ -461,6 +467,9 @@ mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
461467
struct mtype_elem *data;
462468
u32 i;
463469
int j;
470+
#ifdef IP_SET_HASH_WITH_NETS
471+
u8 k;
472+
#endif
464473

465474
rcu_read_lock_bh();
466475
t = rcu_dereference_bh(h->table);
@@ -471,8 +480,9 @@ mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
471480
if (ip_set_timeout_expired(ext_timeout(data, set))) {
472481
pr_debug("expired %u/%u\n", i, j);
473482
#ifdef IP_SET_HASH_WITH_NETS
474-
mtype_del_cidr(h, CIDR(data->cidr),
475-
nets_length, 0);
483+
for (k = 0; k < IPSET_NET_COUNT; k++)
484+
mtype_del_cidr(h, CIDR(data->cidr, k),
485+
nets_length, k);
476486
#endif
477487
ip_set_ext_destroy(set, data);
478488
if (j != n->pos - 1)
@@ -658,8 +668,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
658668
/* Fill out reused slot */
659669
data = ahash_data(n, j, set->dsize);
660670
#ifdef IP_SET_HASH_WITH_NETS
661-
mtype_del_cidr(h, CIDR(data->cidr), NLEN(set->family), 0);
662-
mtype_add_cidr(h, CIDR(d->cidr), NLEN(set->family), 0);
671+
for (i = 0; i < IPSET_NET_COUNT; i++) {
672+
mtype_del_cidr(h, CIDR(data->cidr, i),
673+
NLEN(set->family), i);
674+
mtype_add_cidr(h, CIDR(d->cidr, i),
675+
NLEN(set->family), i);
676+
}
663677
#endif
664678
ip_set_ext_destroy(set, data);
665679
} else {
@@ -673,7 +687,9 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
673687
}
674688
data = ahash_data(n, n->pos++, set->dsize);
675689
#ifdef IP_SET_HASH_WITH_NETS
676-
mtype_add_cidr(h, CIDR(d->cidr), NLEN(set->family), 0);
690+
for (i = 0; i < IPSET_NET_COUNT; i++)
691+
mtype_add_cidr(h, CIDR(d->cidr, i), NLEN(set->family),
692+
i);
677693
#endif
678694
h->elements++;
679695
}
@@ -704,6 +720,9 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
704720
struct mtype_elem *data;
705721
struct hbucket *n;
706722
int i, ret = -IPSET_ERR_EXIST;
723+
#ifdef IP_SET_HASH_WITH_NETS
724+
u8 j;
725+
#endif
707726
u32 key, multi = 0;
708727

709728
rcu_read_lock_bh();
@@ -725,7 +744,9 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
725744
n->pos--;
726745
h->elements--;
727746
#ifdef IP_SET_HASH_WITH_NETS
728-
mtype_del_cidr(h, CIDR(d->cidr), NLEN(set->family), 0);
747+
for (j = 0; j < IPSET_NET_COUNT; j++)
748+
mtype_del_cidr(h, CIDR(d->cidr, j), NLEN(set->family),
749+
j);
729750
#endif
730751
ip_set_ext_destroy(set, data);
731752
if (n->pos + AHASH_INIT_SIZE < n->size) {
@@ -772,13 +793,26 @@ mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
772793
struct htable *t = rcu_dereference_bh(h->table);
773794
struct hbucket *n;
774795
struct mtype_elem *data;
796+
#if IPSET_NET_COUNT == 2
797+
struct mtype_elem orig = *d;
798+
int i, j = 0, k;
799+
#else
775800
int i, j = 0;
801+
#endif
776802
u32 key, multi = 0;
777803
u8 nets_length = NLEN(set->family);
778804

779805
pr_debug("test by nets\n");
780806
for (; j < nets_length && h->nets[j].nets[0] && !multi; j++) {
807+
#if IPSET_NET_COUNT == 2
808+
mtype_data_reset_elem(d, &orig);
809+
mtype_data_netmask(d, h->nets[j].cidr[0], false);
810+
for (k = 0; k < nets_length && h->nets[k].nets[1] && !multi;
811+
k++) {
812+
mtype_data_netmask(d, h->nets[k].cidr[1], true);
813+
#else
781814
mtype_data_netmask(d, h->nets[j].cidr[0]);
815+
#endif
782816
key = HKEY(d, h->initval, t->htable_bits);
783817
n = hbucket(t, key);
784818
for (i = 0; i < n->pos; i++) {
@@ -798,6 +832,9 @@ mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
798832
return mtype_data_match(data, ext,
799833
mext, set, flags);
800834
}
835+
#if IPSET_NET_COUNT == 2
836+
}
837+
#endif
801838
}
802839
return 0;
803840
}
@@ -821,7 +858,10 @@ mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
821858
#ifdef IP_SET_HASH_WITH_NETS
822859
/* If we test an IP address and not a network address,
823860
* try all possible network sizes */
824-
if (CIDR(d->cidr) == SET_HOST_MASK(set->family)) {
861+
for (i = 0; i < IPSET_NET_COUNT; i++)
862+
if (CIDR(d->cidr, i) != SET_HOST_MASK(set->family))
863+
break;
864+
if (i == IPSET_NET_COUNT) {
825865
ret = mtype_test_cidrs(set, d, ext, mext, flags);
826866
goto out;
827867
}

0 commit comments

Comments
 (0)