netfilter connlabel extension
Subsystems (2)
Florian Westphal
maintainerPATCH v2 1/1 netfilter connlabel extension
The following three patches add the connlabel extension, plus ctnetlink support to set/clear labels from userspace.
Connlabels are similar to connmarks, except labels are bit-based; i.e. all labels may be attached to a flow at the same time.
Up to 128 labels are supported at this time.
This limit could be increased later, if the need arises.
To do this safely, one needs to check that the
conntrack extension array won't wrap when all conntrack extensions are in use at the same time (offsets are stored in 'u8' array).
Mapping of bit-identifier to label name is done in userspace.
The extension is enabled at run-time once "-m connlabel" netfilter rules are added. Existing conntracks will not be able to make use of the labels added later.
Change since V1:
- add ctnetlink bitmask attribute (CTA_LABELS_MASK)
to allow userspace to only manipulate a subset of the bitmask.
- otherwise, cosmetic changes only.
Changes since RFCv2:
- make it a variable-size extension and remove dynamic reallocation of the label array
- add ctnetlink support for receiving/setting labels
- limit to 128 instead of 1k labels due to limited extension space (128 is more than enough for now, so this is no problem).
The following changes since commit 61c5e88aecd6fbf2480f39394bb495964e6d9984:
skbuff: make __kmalloc_reserve static (2012-12-28 20:32:36 -0800)
are available in the git repository at:
git://git.breakpoint.cc/fw/nf-next.git nfct_ext_clabel_19
Florian Westphal (3):
netfilter: add connlabel conntrack extension
netfilter: ctnetlink: deliver labels to userspace
netfilter: ctnetlink: allow userspace to modify labels
include/net/netfilter/nf_conntrack_extend.h | 4 + include/net/netfilter/nf_conntrack_labels.h | 58 ++++++++++ include/net/netns/conntrack.h | 4 + include/uapi/linux/netfilter/nf_conntrack_common.h | 1 + include/uapi/linux/netfilter/nfnetlink_conntrack.h | 2 + include/uapi/linux/netfilter/xt_connlabel.h | 12 ++ net/netfilter/Kconfig | 18 +++ net/netfilter/Makefile | 2 + net/netfilter/nf_conntrack_core.c | 12 ++ net/netfilter/nf_conntrack_labels.c | 115 ++++++++++++++++++++ net/netfilter/nf_conntrack_netlink.c | 88 +++++++++++++++ net/netfilter/xt_connlabel.c | 99 +++++++++++++++++ 12 files changed, 415 insertions(+), 0 deletions(-) create mode 100644 include/net/netfilter/nf_conntrack_labels.h create mode 100644 include/uapi/linux/netfilter/xt_connlabel.h create mode 100644 net/netfilter/nf_conntrack_labels.c create mode 100644 net/netfilter/xt_connlabel.c
Florian Westphal
maintainerPATCH 1/3 netfilter: add connlabel conntrack extension
similar to connmarks, except labels are bit-based; i.e. all labels may be attached to a flow at the same time.
Up to 128 labels are supported. Supporting more labels is possible, but requires increasing the ct offset delta from u8 to u16 type due to increased extension sizes.
Mapping of bit-identifier to label name is done in userspace.
The extension is enabled at run-time once "-m connlabel" netfilter rules are added.
--- include/net/netfilter/nf_conntrack_extend.h | 4 + include/net/netfilter/nf_conntrack_labels.h | 55 +++++++++++++++ include/net/netns/conntrack.h | 4 + include/uapi/linux/netfilter/xt_connlabel.h | 12 +++ net/netfilter/Kconfig | 18 +++++ net/netfilter/Makefile | 2 + net/netfilter/nf_conntrack_core.c | 12 +++ net/netfilter/nf_conntrack_labels.c | 72 +++++++++++++++++++ net/netfilter/nf_conntrack_netlink.c | 3 + net/netfilter/xt_connlabel.c | 99 +++++++++++++++++++++++++++ 10 files changed, 281 insertions(+), 0 deletions(-) create mode 100644 include/net/netfilter/nf_conntrack_labels.h create mode 100644 include/uapi/linux/netfilter/xt_connlabel.h create mode 100644 net/netfilter/nf_conntrack_labels.c create mode 100644 net/netfilter/xt_connlabel.c
include/net/netfilter/nf_conntrack_extend.h +4 -0
@@ -23,6 +23,9 @@ enum nf_ct_ext_id {#ifdef CONFIG_NF_CONNTRACK_TIMEOUT NF_CT_EXT_TIMEOUT,#endif +#ifdef CONFIG_NF_CONNTRACK_LABELS +NF_CT_EXT_LABELS , +#endif NF_CT_EXT_NUM, };
@@ -33,6 +36,7 @@ enum nf_ct_ext_id {#define NF_CT_EXT_ZONE_TYPE struct nf_conntrack_zone#define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp#define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout +#define NF_CT_EXT_LABELS_TYPE struct nf_conn_labels/* Extensions: optional stuff which isn't permanently in struct. */ struct nf_ct_ext {
include/net/netfilter/nf_conntrack_labels.h new file +55 -0
55 lines not on this page · raw message
include/net/netns/conntrack.h +4 -0
@@ -83,6 +83,10 @@ struct netns_ct {int sysctl_auto_assign_helper;bool auto_assign_helper_warned;struct nf_ip_net nf_ct_proto; +#if defined(CONFIG_NF_CONNTRACK_LABELS ) +unsigned int labels_used; +u8 label_words; +#endif #ifdef CONFIG_NF_NAT_NEEDED struct hlist_head *nat_bysource;unsigned int nat_htable_size;
include/uapi/linux/netfilter/xt_connlabel.h new file +12 -0
@@ -0,0 +1,12 @@ +#include <linux/types.h> + +#define XT_CONNLABEL_MAXBIT 127 +enum xt_connlabel_mtopts { +XT_CONNLABEL_OP_INVERT =1 <<0 , +XT_CONNLABEL_OP_SET =1 <<1 , +}; + +struct xt_connlabel_mtinfo { +__u16 bit; +__u16 options; +};
net/netfilter/Kconfig +18 -0
@@ -124,6 +124,12 @@ config NF_CONNTRACK_TIMESTAMP If unsure, say `N'. +config NF_CONNTRACK_LABELS + bool + help + This option enables support for assigning user-defined flag bits + to connection tracking entries. It selected by the connlabel match. + config NF_CT_PROTO_DCCP tristate 'DCCP protocol connection tracking support (EXPERIMENTAL)' depends on EXPERIMENTAL
@@ -835,6 +841,18 @@ config NETFILTER_XT_MATCH_CONNBYTES If you want to compile it as a module, say M here and read <file:Documentation/kbuild/modules.txt>. If unsure, say `N'. +config NETFILTER_XT_MATCH_CONNLABEL + tristate '"connlabel" match support' + select NF_CONNTRACK_LABELS + depends on NETFILTER_ADVANCED + ---help--- + This match allows you to test and assign userspace-defined labels names + to a connection. The kernel only stores bit values - mapping + names to bits is done by userspace. + + Unlike connmark, more than 32 flag bits may be assigned to a + connection simultaneously. + config NETFILTER_XT_MATCH_CONNLIMIT tristate '"connlimit" match support"' depends on NF_CONNTRACK
net/netfilter/Makefile +2 -0
@@ -4,6 +4,7 @@ nf_conntrack-y := nf_conntrack_core.o nf_conntrack_standalone.o nf_conntrack_exp nf_conntrack-$(CONFIG_NF_CONNTRACK_TIMEOUT) += nf_conntrack_timeout.o nf_conntrack-$(CONFIG_NF_CONNTRACK_TIMESTAMP) += nf_conntrack_timestamp.o nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o +nf_conntrack-$(CONFIG_NF_CONNTRACK_LABELS) += nf_conntrack_labels.o obj-$(CONFIG_NETFILTER) = netfilter.o
@@ -101,6 +102,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_ADDRTYPE) += xt_addrtype.o obj-$(CONFIG_NETFILTER_XT_MATCH_CLUSTER) += xt_cluster.o obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o obj-$(CONFIG_NETFILTER_XT_MATCH_CONNBYTES) += xt_connbytes.o +obj-$(CONFIG_NETFILTER_XT_MATCH_CONNLABEL) += xt_connlabel.o obj-$(CONFIG_NETFILTER_XT_MATCH_CONNLIMIT) += xt_connlimit.o obj-$(CONFIG_NETFILTER_XT_MATCH_CONNTRACK) += xt_conntrack.o obj-$(CONFIG_NETFILTER_XT_MATCH_CPU) += xt_cpu.o
net/netfilter/nf_conntrack_core.c +12 -0
@@ -45,6 +45,7 @@#include <net/netfilter/nf_conntrack_zones.h> #include <net/netfilter/nf_conntrack_timestamp.h> #include <net/netfilter/nf_conntrack_timeout.h> +#include <net/netfilter/nf_conntrack_labels.h> #include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_core.h>
@@ -763,6 +764,7 @@ void nf_conntrack_free(struct nf_conn *ct) }EXPORT_SYMBOL_GPL (nf_conntrack_free); +/* Allocate a new conntrack: we return -ENOMEM if classification failed due to stress. Otherwise it really is unclassifiable. */ static struct nf_conntrack_tuple_hash *
@@ -809,6 +811,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,nf_ct_acct_ext_add (ct,GFP_ATOMIC );nf_ct_tstamp_ext_add (ct,GFP_ATOMIC ); +nf_ct_labels_ext_add (ct); ecache = tmpl ?nf_ct_ecache_find (tmpl) :NULL ; nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
@@ -1352,6 +1355,7 @@ static void nf_conntrack_cleanup_net(struct net *net) }nf_ct_free_hashtable (net->ct.hash, net->ct.htable_size); +nf_conntrack_labels_fini (net);nf_conntrack_helper_fini (net);nf_conntrack_timeout_fini (net);nf_conntrack_ecache_fini (net);
@@ -1580,7 +1584,15 @@ static int nf_conntrack_init_net(struct net *net) ret =nf_conntrack_helper_init (net);if (ret <0 ) goto err_helper; + + ret =nf_conntrack_labels_init (net); +if (ret <0 ) + goto err_labels; +return 0 ; + +err_labels: +nf_conntrack_helper_fini (net); err_helper:nf_conntrack_timeout_fini (net); err_timeout:
net/netfilter/nf_conntrack_labels.c new file +72 -0
72 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +3 -0
@@ -43,6 +43,7 @@#include <net/netfilter/nf_conntrack_acct.h> #include <net/netfilter/nf_conntrack_zones.h> #include <net/netfilter/nf_conntrack_timestamp.h> +#include <net/netfilter/nf_conntrack_labels.h> #ifdef CONFIG_NF_NAT_NEEDED #include <net/netfilter/nf_nat_core.h> #include <net/netfilter/nf_nat_l4proto.h>
@@ -1598,6 +1599,8 @@ ctnetlink_create_conntrack(struct net *net, u16 zone,nf_ct_acct_ext_add (ct,GFP_ATOMIC );nf_ct_tstamp_ext_add (ct,GFP_ATOMIC );nf_ct_ecache_ext_add (ct,0 ,0 ,GFP_ATOMIC ); +nf_ct_labels_ext_add (ct); +/* we must add conntrack extensions before confirmation. */ ct->status |=IPS_CONFIRMED ;
net/netfilter/xt_connlabel.c new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
1.7.8.6
Re: [PATCH 1/3] netfilter: add connlabel conntrack extension
Quoted 11 lines · Florian Westphal
similar to connmarks, except labels are bit-based; i.e. all labels may be attached to a flow at the same time.
Up to 128 labels are supported. Supporting more labels is possible, but requires increasing the ct offset delta from u8 to u16 type due to increased extension sizes.
Mapping of bit-identifier to label name is done in userspace.
The extension is enabled at run-time once "-m connlabel" netfilter rules are added.
Applied, thanks.
Florian Westphal
maintainerPATCH 2/3 netfilter: ctnetlink: deliver labels to userspace
In reply to Florian WestphalIntroduce CTA_LABELS attribute to send a bit-vector of currently active labels to userspace.
Future patch will permit userspace to also set/delete active labels.
--- include/uapi/linux/netfilter/nf_conntrack_common.h | 1 + include/uapi/linux/netfilter/nfnetlink_conntrack.h | 1 + net/netfilter/nf_conntrack_labels.c | 2 +- net/netfilter/nf_conntrack_netlink.c | 41 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletions(-)
include/uapi/linux/netfilter/nf_conntrack_common.h +1 -0
@@ -101,6 +101,7 @@ enum ip_conntrack_events {IPCT_MARK ,/* new mark has been set */ IPCT_NATSEQADJ ,/* NAT is doing sequence adjustment */ IPCT_SECMARK ,/* new security mark has been set */ + IPCT_LABEL, /* new connlabel has been set */ }; enum ip_conntrack_expect_events {
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1 -0
@@ -49,6 +49,7 @@ enum ctattr_type {CTA_SECCTX ,CTA_TIMESTAMP ,CTA_MARK_MASK , +CTA_LABELS , __CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)
net/netfilter/nf_conntrack_labels.c +1 -1
@@ -46,7 +46,7 @@ int nf_connlabel_set(struct nf_conn *ct, u16 bit)return 0 ;if (test_and_set_bit (bit, labels->bits)) -return 0 ; +nf_conntrack_event_cache (IPCT_LABEL , ct);return 0 ; }
net/netfilter/nf_conntrack_netlink.c +41 -0
71 lines not on this page · raw message
Signature · 1 line
1.7.8.6
Re: [PATCH 2/3] netfilter: ctnetlink: deliver labels to userspace
Quoted 4 lines · Florian Westphal
Introduce
CTA_LABELSattribute to send a bit-vector of currently active labels to userspace.Future patch will permit userspace to also set/delete active labels.
Applied, thanks.
Florian Westphal
maintainerPATCH 3/3 netfilter: ctnetlink: allow userspace to modify labels
In reply to Florian WestphalAdd the ability to set/clear labels assigned to a conntrack via ctnetlink.
To allow userspace to only alter specific bits, Pablo suggested to add a new CTA_LABELS_MASK attribute:
The new set of active labels is then determined via
active = (active & ~mask) ^ changeset
i.e., the mask selects those bits in the existing set that should be changed.
This follows the same method already used by MARK and CONNMARK targets.
Omitting CTA_LABELS_MASK is the same as setting all bits in CTA_LABELS_MASK to 1: The existing set is replaced by the one from userspace.
--- include/net/netfilter/nf_conntrack_labels.h | 3 + include/uapi/linux/netfilter/nfnetlink_conntrack.h | 1 + net/netfilter/nf_conntrack_labels.c | 43 +++++++++++++++++++ net/netfilter/nf_conntrack_netlink.c | 44 ++++++++++++++++++++ 4 files changed, 91 insertions(+), 0 deletions(-)
include/net/netfilter/nf_conntrack_labels.h +3 -0
@@ -46,6 +46,9 @@ static inline struct nf_conn_labels *nf_ct_labels_ext_add(struct nf_conn *ct)bool nf_connlabel_match (const struct nf_conn *ct,u16 bit);int nf_connlabel_set (struct nf_conn *ct,u16 bit); +int nf_connlabels_replace (struct nf_conn *ct, +const u32 *data,const u32 *mask,unsigned int words); +#ifdef CONFIG_NF_CONNTRACK_LABELS int nf_conntrack_labels_init (struct net *net);void nf_conntrack_labels_fini (struct net *net);
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1 -0
@@ -50,6 +50,7 @@ enum ctattr_type {CTA_TIMESTAMP ,CTA_MARK_MASK ,CTA_LABELS , +CTA_LABELS_MASK , __CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)
net/netfilter/nf_conntrack_labels.c +43 -0
49 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +44 -0
80 lines not on this page · raw message
Signature · 1 line
1.7.8.6
Pablo Neira Ayuso
maintainerRe: [PATCH 3/3] netfilter: ctnetlink: allow userspace to modify labels
Quoted 10 lines · Florian Westphal
Add the ability to set/clear labels assigned to a conntrack via ctnetlink.
To allow userspace to only alter specific bits, Pablo suggested to add a new
CTA_LABELS_MASKattribute:The new set of active labels is then determined via
active = (active & ~mask) ^ changeset
Quoted 7 lines
i.e., the mask selects those bits in the existing set that should be changed.
This follows the same method already used by MARK and CONNMARK targets.
Omitting
CTA_LABELS_MASKis the same as setting all bits inCTA_LABELS_MASKto 1: The existing set is replaced by the one from userspace.
And this also applied, thanks Florian.
You can push the user-space changes for this into some new -next branch of each tree. Then merge it to master by when 3.9-rc1 is released.