Skip to content

Commit 72205fc

Browse files
Jozsef Kadlecsikkaber
authored andcommitted
netfilter: ipset: bitmap:ip set type support
The module implements the bitmap:ip set type in two flavours, without and with timeout support. In this kind of set one can store IPv4 addresses (or network addresses) from a given range. In order not to waste memory, the timeout version does not rely on the kernel timer for every element to be timed out but on garbage collection. All set types use this mechanism. Signed-off-by: Jozsef Kadlecsik <[email protected]> Signed-off-by: Patrick McHardy <[email protected]>
1 parent a7b4f98 commit 72205fc

File tree

5 files changed

+758
-0
lines changed

5 files changed

+758
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __IP_SET_BITMAP_H
2+
#define __IP_SET_BITMAP_H
3+
4+
/* Bitmap type specific error codes */
5+
enum {
6+
/* The element is out of the range of the set */
7+
IPSET_ERR_BITMAP_RANGE = IPSET_ERR_TYPE_SPECIFIC,
8+
/* The range exceeds the size limit of the set type */
9+
IPSET_ERR_BITMAP_RANGE_SIZE,
10+
};
11+
12+
#ifdef __KERNEL__
13+
#define IPSET_BITMAP_MAX_RANGE 0x0000FFFF
14+
15+
/* Common functions */
16+
17+
static inline u32
18+
range_to_mask(u32 from, u32 to, u8 *bits)
19+
{
20+
u32 mask = 0xFFFFFFFE;
21+
22+
*bits = 32;
23+
while (--(*bits) > 0 && mask && (to & mask) != from)
24+
mask <<= 1;
25+
26+
return mask;
27+
}
28+
29+
#endif /* __KERNEL__ */
30+
31+
#endif /* __IP_SET_BITMAP_H */
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#ifndef _IP_SET_TIMEOUT_H
2+
#define _IP_SET_TIMEOUT_H
3+
4+
/* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#ifdef __KERNEL__
12+
13+
/* How often should the gc be run by default */
14+
#define IPSET_GC_TIME (3 * 60)
15+
16+
/* Timeout period depending on the timeout value of the given set */
17+
#define IPSET_GC_PERIOD(timeout) \
18+
((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
19+
20+
/* Set is defined without timeout support: timeout value may be 0 */
21+
#define IPSET_NO_TIMEOUT UINT_MAX
22+
23+
#define with_timeout(timeout) ((timeout) != IPSET_NO_TIMEOUT)
24+
25+
static inline unsigned int
26+
ip_set_timeout_uget(struct nlattr *tb)
27+
{
28+
unsigned int timeout = ip_set_get_h32(tb);
29+
30+
/* Userspace supplied TIMEOUT parameter: adjust crazy size */
31+
return timeout == IPSET_NO_TIMEOUT ? IPSET_NO_TIMEOUT - 1 : timeout;
32+
}
33+
34+
#ifdef IP_SET_BITMAP_TIMEOUT
35+
36+
/* Bitmap specific timeout constants and macros for the entries */
37+
38+
/* Bitmap entry is unset */
39+
#define IPSET_ELEM_UNSET 0
40+
/* Bitmap entry is set with no timeout value */
41+
#define IPSET_ELEM_PERMANENT (UINT_MAX/2)
42+
43+
static inline bool
44+
ip_set_timeout_test(unsigned long timeout)
45+
{
46+
return timeout != IPSET_ELEM_UNSET &&
47+
(timeout == IPSET_ELEM_PERMANENT ||
48+
time_after(timeout, jiffies));
49+
}
50+
51+
static inline bool
52+
ip_set_timeout_expired(unsigned long timeout)
53+
{
54+
return timeout != IPSET_ELEM_UNSET &&
55+
timeout != IPSET_ELEM_PERMANENT &&
56+
time_before(timeout, jiffies);
57+
}
58+
59+
static inline unsigned long
60+
ip_set_timeout_set(u32 timeout)
61+
{
62+
unsigned long t;
63+
64+
if (!timeout)
65+
return IPSET_ELEM_PERMANENT;
66+
67+
t = timeout * HZ + jiffies;
68+
if (t == IPSET_ELEM_UNSET || t == IPSET_ELEM_PERMANENT)
69+
/* Bingo! */
70+
t++;
71+
72+
return t;
73+
}
74+
75+
static inline u32
76+
ip_set_timeout_get(unsigned long timeout)
77+
{
78+
return timeout == IPSET_ELEM_PERMANENT ? 0 : (timeout - jiffies)/HZ;
79+
}
80+
81+
#else
82+
83+
/* Hash specific timeout constants and macros for the entries */
84+
85+
/* Hash entry is set with no timeout value */
86+
#define IPSET_ELEM_PERMANENT 0
87+
88+
static inline bool
89+
ip_set_timeout_test(unsigned long timeout)
90+
{
91+
return timeout == IPSET_ELEM_PERMANENT ||
92+
time_after(timeout, jiffies);
93+
}
94+
95+
static inline bool
96+
ip_set_timeout_expired(unsigned long timeout)
97+
{
98+
return timeout != IPSET_ELEM_PERMANENT &&
99+
time_before(timeout, jiffies);
100+
}
101+
102+
static inline unsigned long
103+
ip_set_timeout_set(u32 timeout)
104+
{
105+
unsigned long t;
106+
107+
if (!timeout)
108+
return IPSET_ELEM_PERMANENT;
109+
110+
t = timeout * HZ + jiffies;
111+
if (t == IPSET_ELEM_PERMANENT)
112+
/* Bingo! :-) */
113+
t++;
114+
115+
return t;
116+
}
117+
118+
static inline u32
119+
ip_set_timeout_get(unsigned long timeout)
120+
{
121+
return timeout == IPSET_ELEM_PERMANENT ? 0 : (timeout - jiffies)/HZ;
122+
}
123+
#endif /* ! IP_SET_BITMAP_TIMEOUT */
124+
125+
#endif /* __KERNEL__ */
126+
127+
#endif /* _IP_SET_TIMEOUT_H */

net/netfilter/ipset/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ config IP_SET_MAX
2323
The value can be overriden by the 'max_sets' module
2424
parameter of the 'ip_set' module.
2525

26+
config IP_SET_BITMAP_IP
27+
tristate "bitmap:ip set support"
28+
depends on IP_SET
29+
help
30+
This option adds the bitmap:ip set type support, by which one
31+
can store IPv4 addresses (or network addresse) from a range.
32+
33+
To compile it as a module, choose M here. If unsure, say N.
34+
2635
endif # IP_SET

net/netfilter/ipset/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ ip_set-y := ip_set_core.o ip_set_getport.o pfxlen.o
66

77
# ipset core
88
obj-$(CONFIG_IP_SET) += ip_set.o
9+
10+
# bitmap types
11+
obj-$(CONFIG_IP_SET_BITMAP_IP) += ip_set_bitmap_ip.o

0 commit comments

Comments
 (0)