nf_tables inner match support
Subsystems (2)
Pablo Neira Ayuso
maintainercover nf_tables inner match support
Hi,
This is an early RFC to show progress on the inner match support, which allows to match on inner header fields that are usually encapsulated by tunnel protocols.
The inner expression provides a parser for the tunneled packet which uses a userspace description of the expected inner headers, then the inner expression (only payload supported at this stage) is used to match on the inner header protocol fields, using the new link, network and transport offsets.
Only VxLAN is supported in this patchset, I have work-in-progress with other type of tunnel protocols, such as IPIP and GRE.
Currently nft generated bytecode to match on ethernet link layer and IPv4/IPv6 header fields. More work in userspace is required to generalize protocol tracking and implicit dependencies by reusing the existing codebase. Matching on VxLAN containing a VLAN frame is also broken, for the same reason. Maybe VxLAN containing VLAN is not top priority to support at this stage, but the goal is to allow for such flexibility by allowing users to fetch this new feature via userspace tool update.
As an example, the bytecode that nft generates using this new expression looks like this:
# nft --debug=netlink add rule x y udp dport 7777 vxlan ip saddr 1.2.3.4 ip x y [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000011 ] [ payload load 2b @ transport header + 2 => reg 1 ] [ cmp eq reg 1 0x0000611e ] [ inner hdrsize 8 flags f type 1 nfproto 2 [ payload load 4b @ network header + 12 => reg 1 ] ] [ cmp eq reg 1 0x04030201 ]
Comments welcome.
Pablo Neira Ayuso (1):
netfilter: nft_inner: support for inner header matching
include/net/netfilter/nf_tables.h | 5 + include/net/netfilter/nf_tables_core.h | 22 ++ include/uapi/linux/netfilter/nf_tables.h | 27 ++ net/netfilter/Makefile | 2 +- net/netfilter/nf_tables_api.c | 37 +++ net/netfilter/nf_tables_core.c | 1 + net/netfilter/nft_inner.c | 303 +++++++++++++++++++++++ net/netfilter/nft_payload.c | 113 ++++++++- 8 files changed, 508 insertions(+), 2 deletions(-) create mode 100644 net/netfilter/nft_inner.c
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerRFC PATCH 1/1 netfilter: nft_inner: support for inner header matching
This new expression allows you to match on the inner headers that is encapsulated by any of the tunneling protocols.
Basically, the idea is to update the link, network and transport offsets that are provided by nft_pktinfo, so the existing expressions (with little updates) can be reused to match on the inner headers.
The expression supports for different tunnel combinations such as:
- ethernet frame over IPv4/IPv6 packet, eg. VxLAN.
- IPv4/IPv6 packet over IPv4/IPv6 packet, eg. IPIP.
- IPv4/IPv6 packet over IPv4/IPv6 + transport header, eg. GRE.
- transport header (ESP or SCTP) over transport header (usually UDP)
Currently it only supports for the payload expression, but it should be possible to extend it to support for the meta expression too, hence, matching on inner meta protocol and l4proto reports the inner header, as well as extension headers.
The new inner expression parses the inner header based on the template description provided by userspace.
- flags: to describe how to parse the inner packet:
NFT_PAYLOAD_CTX_INNER_TUN, the tunnel provides its own header. NFT_PAYLOAD_CTX_INNER_ETHER, the ethernet frame is available as inner header. NFT_PAYLOAD_CTX_INNER_NH, the network header is available as inner header. NFT_PAYLOAD_CTX_INNER_TH, the transport header is available as inner header.
For example, VxLAN sets on all these flags. While GRE only sets on NFT_PAYLOAD_CTX_INNER_NH and NFT_PAYLOAD_CTX_INNER_TH, because the GRE tunnel header comes as an outer transport header offset to match on it.
- header size: in case the tunnel comes with its own header. Only used for VxLAN and Geneve since they come over UDP.
- type: this provides a hint to userspace on how to delinearize the rule. This is useful for VxLAN and Geneve since they run over UDP, since transport does not provide a hint. This is also useful in case hardware offload is ever supported. The type is not currently interpreted by the kernel.
- expected inner layer 3 protocol (via INNER_NFPROTO), either IPv4 or IPv6. NFPROTO_INET is used to allow for both.
- expression: currently only payload supported. The meta and exthdr expressions should be supported too. There is a new expression operation that needs to be registers via inner_ops which uses the inner header offsets.
There is a new NFT_PAYLOAD_TUN_HEADER base which allows to match on the tunnel header fields, eg. vxlan vni.
The inner expression is embedded into nft_inner private area and it is passed to the payload inner eval call.
--- include/net/netfilter/nf_tables.h | 5 + include/net/netfilter/nf_tables_core.h | 22 ++ include/uapi/linux/netfilter/nf_tables.h | 27 ++ net/netfilter/Makefile | 2 +- net/netfilter/nf_tables_api.c | 37 +++ net/netfilter/nf_tables_core.c | 1 + net/netfilter/nft_inner.c | 303 +++++++++++++++++++++++ net/netfilter/nft_payload.c | 113 ++++++++- 8 files changed, 508 insertions(+), 2 deletions(-) create mode 100644 net/netfilter/nft_inner.c
include/net/netfilter/nf_tables.h +5 -0
@@ -375,6 +375,10 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)return (void *)expr->data; } +struct nft_expr_info ; + +int nft_expr_inner_parse (const struct nft_ctx *ctx,const struct nlattr *nla, +struct nft_expr_info *info);int nft_expr_clone (struct nft_expr *dst,struct nft_expr *src);void nft_expr_destroy (const struct nft_ctx *ctx,struct nft_expr *expr); int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
@@ -864,6 +868,7 @@ struct nft_expr_type { const struct nlattr * const tb[]);void (*release_ops)(const struct nft_expr_ops *ops);const struct nft_expr_ops *ops; +const struct nft_expr_ops *inner_ops;struct list_head list;const char *name;struct module *owner;
include/net/netfilter/nf_tables_core.h +22 -0
@@ -18,6 +18,7 @@ extern struct nft_expr_type nft_meta_type;extern struct nft_expr_type nft_rt_type;extern struct nft_expr_type nft_exthdr_type;extern struct nft_expr_type nft_last_type; +extern struct nft_expr_type nft_inner_type;#ifdef CONFIG_NETWORK_SECMARK extern struct nft_object_type nft_secmark_obj_type;
@@ -138,4 +139,25 @@ void nft_rt_get_eval(const struct nft_expr *expr, struct nft_regs *regs, const struct nft_pktinfo *pkt);void nft_counter_eval (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt); + +enum { +NFT_PAYLOAD_CTX_INNER_TUN = (1 <<0 ), +NFT_PAYLOAD_CTX_INNER_LL = (1 <<1 ), +NFT_PAYLOAD_CTX_INNER_NH = (1 <<2 ), +NFT_PAYLOAD_CTX_INNER_TH = (1 <<3 ), +}; + +struct nft_inner_tun_ctx { +u8 inner_tunoff; +u8 inner_lloff; +u8 inner_nhoff; +u8 inner_thoff; +u8 flags; +}; + +int nft_payload_inner_offset (const struct nft_pktinfo *pkt); +void nft_payload_inner_eval (const struct nft_expr *expr,struct nft_regs *regs, +const struct nft_pktinfo *pkt, +struct nft_inner_tun_ctx *ctx); +#endif /* _NET_NF_TABLES_CORE_H */
include/uapi/linux/netfilter/nf_tables.h +27 -0
@@ -760,6 +760,7 @@ enum nft_payload_bases {NFT_PAYLOAD_NETWORK_HEADER ,NFT_PAYLOAD_TRANSPORT_HEADER ,NFT_PAYLOAD_INNER_HEADER , + NFT_PAYLOAD_TUN_HEADER, };/**
@@ -779,6 +780,32 @@ enum nft_payload_csum_flags { NFT_PAYLOAD_L4CSUM_PSEUDOHDR = (1 << 0), }; +enum nft_inner_type { +NFT_INNER_UNSPEC =0 , +NFT_INNER_VXLAN , +}; + +enum nft_inner_flags { +NFT_INNER_HDRSIZE = (1 <<0 ), +NFT_INNER_LL = (1 <<1 ), +NFT_INNER_NH = (1 <<2 ), +NFT_INNER_TH = (1 <<3 ), +}; +#define NFT_INNER_MASK (NFT_INNER_HDRSIZE | NFT_INNER_LL | \ + NFT_INNER_NH | NFT_INNER_TH) + +enum nft_inner_attributes { +NFTA_INNER_UNSPEC , +NFTA_INNER_NUM , +NFTA_INNER_TYPE , +NFTA_INNER_FLAGS , +NFTA_INNER_HDRSIZE , +NFTA_INNER_NFPROTO , +NFTA_INNER_EXPR , + __NFTA_INNER_MAX +}; +#define NFTA_INNER_MAX (__NFTA_INNER_MAX - 1) +/** * enum nft_payload_attributes - nf_tables payload expression netlink attributes *
net/netfilter/Makefile +1 -1
@@ -82,7 +82,7 @@ nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o nft_last.o \ nft_counter.o nft_chain_route.o nf_tables_offload.o \ nft_set_hash.o nft_set_bitmap.o nft_set_rbtree.o \ - nft_set_pipapo.o + nft_set_pipapo.o nft_inner.o ifdef CONFIG_X86_64 ifndef CONFIG_UML
net/netfilter/nf_tables_api.c +37 -0
@@ -2857,6 +2857,43 @@ static int nf_tables_expr_parse(const struct nft_ctx *ctx,return err; } +int nft_expr_inner_parse (const struct nft_ctx *ctx,const struct nlattr *nla, +struct nft_expr_info *info) +{ +struct nlattr *tb[NFTA_EXPR_MAX +1 ]; +const struct nft_expr_type *type; +int err; + + err =nla_parse_nested_deprecated (tb,NFTA_EXPR_MAX , nla, + nft_expr_policy,NULL ); +if (err <0 ) +return err; + +if (!tb[NFTA_EXPR_DATA ]) +return -EINVAL ; + + type =__nft_expr_type_get (ctx->family, tb[NFTA_EXPR_NAME ]); +if (IS_ERR (type)) +return PTR_ERR (type); + +if (!type->inner_ops) +return -EOPNOTSUPP ; + + err =nla_parse_nested_deprecated (info->tb, type->maxattr, + tb[NFTA_EXPR_DATA ], + type->policy,NULL ); +if (err <0 ) + goto err_nla_parse; + + info->attr = nla; + info->ops = type->inner_ops; + +return 0 ; + +err_nla_parse: +return err; +} +static int nf_tables_newexpr (const struct nft_ctx *ctx,const struct nft_expr_info *expr_info,struct nft_expr *expr)
net/netfilter/nf_tables_core.c +1 -0
@@ -340,6 +340,7 @@ static struct nft_expr_type *nft_basic_types[] = { &nft_exthdr_type, &nft_last_type, &nft_counter_type, + &nft_inner_type, }; static struct nft_object_type *nft_basic_objects[] = {
net/netfilter/nft_inner.c new file +303 -0
303 lines not on this page · raw message
net/netfilter/nft_payload.c +112 -1
131 lines not on this page · raw message
Signature · 1 line
2.30.2