netfilter: nf_tables: add SECMARK support
Subsystems (5)
PATCH v3 1/2 netfilter: nf_tables: add SECMARK support
Add the ability to set the security context of packets within the nf_tables framework. Add a nft_object for holding security contexts in the kernel and manipulating packets on the wire.
Convert the security context strings at rule addition time to security identifiers. This is the same behavior like in xt_SECMARK and offers better performance than computing it per packet.
Set the maximum security context length to 256.
---
v3: switch context string from char[] to char *
rename function to nft_secmark_compute_secid()
v2: convert security context strings to ids on rule addition time
Based on nf-next
Tested with v4.18.8
include/net/netfilter/nf_tables_core.h | 4 + include/uapi/linux/netfilter/nf_tables.h | 18 +++- net/netfilter/nf_tables_core.c | 28 +++++- net/netfilter/nft_meta.c | 107 +++++++++++++++++++++++ 4 files changed, 152 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables_core.h +4 -0
@@ -16,6 +16,10 @@ extern struct nft_expr_type nft_meta_type;extern struct nft_expr_type nft_rt_type;extern struct nft_expr_type nft_exthdr_type; +#ifdef CONFIG_NETWORK_SECMARK +extern struct nft_object_type nft_secmark_obj_type; +#endif +int nf_tables_core_module_init (void );void nf_tables_core_module_exit (void );
include/uapi/linux/netfilter/nf_tables.h +17 -1
@@ -1176,6 +1176,21 @@ enum nft_quota_attributes { };#define NFTA_QUOTA_MAX (__NFTA_QUOTA_MAX - 1) +/** +* enum nft_secmark_attributes - nf_tables secmark object netlink attributes +* +* @NFTA_SECMARK_CTX: security context (NLA_STRING) +*/ +enum nft_secmark_attributes { +NFTA_SECMARK_UNSPEC , +NFTA_SECMARK_CTX , + __NFTA_SECMARK_MAX, +}; +#define NFTA_SECMARK_MAX (__NFTA_SECMARK_MAX - 1) + +/* Max security context length */ +#define NFT_SECMARK_CTX_MAXLEN 256 +/** * enum nft_reject_types - nf_tables reject expression reject types *
@@ -1432,7 +1447,8 @@ enum nft_ct_timeout_timeout_attributes {#define NFT_OBJECT_CONNLIMIT 5#define NFT_OBJECT_TUNNEL 6#define NFT_OBJECT_CT_TIMEOUT 7 -#define __NFT_OBJECT_MAX 8 +#define NFT_OBJECT_SECMARK 8 +#define __NFT_OBJECT_MAX 9#define NFT_OBJECT_MAX (__NFT_OBJECT_MAX - 1)/**
net/netfilter/nf_tables_core.c +24 -4
@@ -249,12 +249,24 @@ static struct nft_expr_type *nft_basic_types[] = { &nft_exthdr_type, }; +static struct nft_object_type *nft_basic_objects[] = { +#ifdef CONFIG_NETWORK_SECMARK + &nft_secmark_obj_type, +#endif +}; +int __init nf_tables_core_module_init (void ) { -int err, i; +int err, i, j =0 ; + +for (i =0 ; i <ARRAY_SIZE (nft_basic_objects); i++) { + err =nft_register_obj (nft_basic_objects[i]); +if (err) + goto err; + } -for (i =0 ; i <ARRAY_SIZE (nft_basic_types); i++) { - err =nft_register_expr (nft_basic_types[i]); +for (j =0 ; j <ARRAY_SIZE (nft_basic_types); j++) { + err =nft_register_expr (nft_basic_types[j]);if (err) goto err; }
@@ -262,8 +274,12 @@ int __init nf_tables_core_module_init(void)return 0 ; err: +while (j-- >0 ) +nft_unregister_expr (nft_basic_types[j]); +while (i-- >0 ) -nft_unregister_expr (nft_basic_types[i]); +nft_unregister_obj (nft_basic_objects[i]); +return err; }
@@ -274,4 +290,8 @@ void nf_tables_core_module_exit(void) i =ARRAY_SIZE (nft_basic_types);while (i-- >0 )nft_unregister_expr (nft_basic_types[i]); + + i =ARRAY_SIZE (nft_basic_objects); +while (i-- >0 ) +nft_unregister_obj (nft_basic_objects[i]); }
net/netfilter/nft_meta.c +107 -0
110 lines not on this page · raw message
Signature · 1 line
2.19.0
PATCH v3 2/2 netfilter: nf_tables: add requirements for connsecmark support
Add ability to set the connection tracking secmark value.
Add ability to set the meta secmark value.
---
v3: fix compile error when CONFIG_NF_CONNTRACK_MARK not defined
Based on nf-next
Tested with v4.18.8
net/netfilter/nft_ct.c | 17 ++++++++++++++++- net/netfilter/nft_meta.c | 8 ++++++++ 2 files changed, 24 insertions(+), 1 deletion(-)
net/netfilter/nft_ct.c +16 -1
@@ -279,7 +279,7 @@ static void nft_ct_set_eval(const struct nft_expr *expr, {const struct nft_ct *priv =nft_expr_priv (expr);struct sk_buff *skb = pkt->skb; -#ifdef CONFIG_NF_CONNTRACK_MARK +#if defined(CONFIG_NF_CONNTRACK_MARK ) || defined(CONFIG_NF_CONNTRACK_SECMARK )u32 value = regs->data[priv->sreg];#endif enum ip_conntrack_info ctinfo;
@@ -298,6 +298,14 @@ static void nft_ct_set_eval(const struct nft_expr *expr, }break ;#endif +#ifdef CONFIG_NF_CONNTRACK_SECMARK +case NFT_CT_SECMARK : +if (ct->secmark != value) { + ct->secmark = value; +nf_conntrack_event_cache (IPCT_SECMARK , ct); + } +break ; +#endif #ifdef CONFIG_NF_CONNTRACK_LABELS case NFT_CT_LABELS : nf_connlabels_replace(ct,
@@ -564,6 +572,13 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,return -EINVAL ; len =sizeof (u32);break ; +#endif +#ifdef CONFIG_NF_CONNTRACK_SECMARK +case NFT_CT_SECMARK : +if (tb[NFTA_CT_DIRECTION ]) +return -EINVAL ; + len =sizeof (u32); +break ;#endif default :return -EOPNOTSUPP ;
net/netfilter/nft_meta.c +8 -0
@@ -284,6 +284,11 @@ static void nft_meta_set_eval(const struct nft_expr *expr, skb->nf_trace = !!value8;break ; +#ifdef CONFIG_NETWORK_SECMARK +case NFT_META_SECMARK : + skb->secmark = value; +break ; +#endif default :WARN_ON (1 ); }
@@ -436,6 +441,9 @@ static int nft_meta_set_init(const struct nft_ctx *ctx,switch (priv->key) {case NFT_META_MARK :case NFT_META_PRIORITY : +#ifdef CONFIG_NETWORK_SECMARK +case NFT_META_SECMARK : +#endif len =sizeof (u32);break ;case NFT_META_NFTRACE :
Signature · 1 line
2.19.0
Pablo Neira Ayuso
maintainerRe: [PATCH v3 2/2] netfilter: nf_tables: add requirements for connsecmark support
Quoted 3 lines · Christian G?ttsche
Add ability to set the connection tracking secmark value.
Add ability to set the meta secmark value.
Also applied, thanks.
Quoted 7 lines · Christian G?ttsche
Add the ability to set the security context of packets within the
nf_tablesframework. Add anft_objectfor holding security contexts in the kernel and manipulating packets on the wire.Convert the security context strings at rule addition time to security identifiers. This is the same behavior like in
xt_SECMARKand offers better performance than computing it per packet.Set the maximum security context length to 256.
Applied, thanks Christian.