Skip to content

Commit a7b4f98

Browse files
Jozsef Kadlecsikkaber
authored andcommitted
netfilter: ipset: IP set core support
The patch adds the IP set core support to the kernel. The IP set core implements a netlink (nfnetlink) based protocol by which one can create, destroy, flush, rename, swap, list, save, restore sets, and add, delete, test elements from userspace. For simplicity (and backward compatibilty and for not to force ip(6)tables to be linked with a netlink library) reasons a small getsockopt-based protocol is also kept in order to communicate with the ip(6)tables match and target. The netlink protocol passes all u16, etc values in network order with NLA_F_NET_BYTEORDER flag. The protocol enforces the proper use of the NLA_F_NESTED and NLA_F_NET_BYTEORDER flags. For other kernel subsystems (netfilter match and target) the API contains the functions to add, delete and test elements in sets and the required calls to get/put refereces to the sets before those operations can be performed. The set types (which are implemented in independent modules) are stored in a simple RCU protected list. A set type may have variants: for example without timeout or with timeout support, for IPv4 or for IPv6. The sets (i.e. the pointers to the sets) are stored in an array. The sets are identified by their index in the array, which makes possible easy and fast swapping of sets. The array is protected indirectly by the nfnl mutex from nfnetlink. The content of the sets are protected by the rwlock of the set. There are functional differences between the add/del/test functions for the kernel and userspace: - kernel add/del/test: works on the current packet (i.e. one element) - kernel test: may trigger an "add" operation in order to fill out unspecified parts of the element from the packet (like MAC address) - userspace add/del: works on the netlink message and thus possibly on multiple elements from the IPSET_ATTR_ADT container attribute. - userspace add: may trigger resizing of a set Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent f703651 commit a7b4f98

File tree

10 files changed

+2626
-0
lines changed

10 files changed

+2626
-0
lines changed

include/linux/netfilter/ipset/ip_set.h

Lines changed: 452 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef _IP_SET_GETPORT_H
2+
#define _IP_SET_GETPORT_H
3+
4+
extern bool ip_set_get_ip4_port(const struct sk_buff *skb, bool src,
5+
__be16 *port, u8 *proto);
6+
extern bool ip_set_get_ip6_port(const struct sk_buff *skb, bool src,
7+
__be16 *port, u8 *proto);
8+
extern bool ip_set_get_ip_port(const struct sk_buff *skb, u8 pf, bool src,
9+
__be16 *port);
10+
11+
#endif /*_IP_SET_GETPORT_H*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _PFXLEN_H
2+
#define _PFXLEN_H
3+
4+
#include <asm/byteorder.h>
5+
#include <linux/netfilter.h>
6+
7+
/* Prefixlen maps, by Jan Engelhardt */
8+
extern const union nf_inet_addr ip_set_netmask_map[];
9+
extern const union nf_inet_addr ip_set_hostmask_map[];
10+
11+
static inline __be32
12+
ip_set_netmask(u8 pfxlen)
13+
{
14+
return ip_set_netmask_map[pfxlen].ip;
15+
}
16+
17+
static inline const __be32 *
18+
ip_set_netmask6(u8 pfxlen)
19+
{
20+
return &ip_set_netmask_map[pfxlen].ip6[0];
21+
}
22+
23+
static inline u32
24+
ip_set_hostmask(u8 pfxlen)
25+
{
26+
return (__force u32) ip_set_hostmask_map[pfxlen].ip;
27+
}
28+
29+
static inline const __be32 *
30+
ip_set_hostmask6(u8 pfxlen)
31+
{
32+
return &ip_set_hostmask_map[pfxlen].ip6[0];
33+
}
34+
35+
#endif /*_PFXLEN_H */

net/netfilter/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,4 +1052,6 @@ endif # NETFILTER_XTABLES
10521052

10531053
endmenu
10541054

1055+
source "net/netfilter/ipset/Kconfig"
1056+
10551057
source "net/netfilter/ipvs/Kconfig"

net/netfilter/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,8 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
105105
obj-$(CONFIG_NETFILTER_XT_MATCH_TIME) += xt_time.o
106106
obj-$(CONFIG_NETFILTER_XT_MATCH_U32) += xt_u32.o
107107

108+
# ipset
109+
obj-$(CONFIG_IP_SET) += ipset/
110+
108111
# IPVS
109112
obj-$(CONFIG_IP_VS) += ipvs/

net/netfilter/ipset/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
menuconfig IP_SET
2+
tristate "IP set support"
3+
depends on INET && NETFILTER
4+
help
5+
This option adds IP set support to the kernel.
6+
In order to define and use the sets, you need the userspace utility
7+
ipset(8). You can use the sets in netfilter via the "set" match
8+
and "SET" target.
9+
10+
To compile it as a module, choose M here. If unsure, say N.
11+
12+
if IP_SET
13+
14+
config IP_SET_MAX
15+
int "Maximum number of IP sets"
16+
default 256
17+
range 2 65534
18+
depends on IP_SET
19+
help
20+
You can define here default value of the maximum number
21+
of IP sets for the kernel.
22+
23+
The value can be overriden by the 'max_sets' module
24+
parameter of the 'ip_set' module.
25+
26+
endif # IP_SET

net/netfilter/ipset/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Makefile for the ipset modules
3+
#
4+
5+
ip_set-y := ip_set_core.o ip_set_getport.o pfxlen.o
6+
7+
# ipset core
8+
obj-$(CONFIG_IP_SET) += ip_set.o

0 commit comments

Comments
 (0)