nf_tables inner tunnel header match support
Subsystems (2)
Pablo Neira Ayuso
maintainerv2 cover nf_tables inner tunnel header match support
Hi,
This is version 2 for this patchset.
The inner expression provides a packet parser for the tunneled packet which uses a userspace description of the expected inner headers. Then, the inner expression (only payload and meta supported at this stage) is used to match on the inner header protocol fields, using the new link, network and transport offsets as well as inner metadata.
This patchset adds support for VxLAN, Geneve, GRE and IPIP. More tunnel protocol can be supported via userspace updates only.
Changes in this v2:
Patch #1 interpret GRE flags to handle variable GRE header size. Patch #2 no changes in IPIP support.
Patch #3 add nft_inner_parse_tunhdr() helper function to prepare
for caching the inner offset in percpu area.
Patch #4 add NFT_PKTINFO_INNER_FULL flag and percpu area to cache
the inner link, network and transport offsets. So the inner offsets are calculated one for the inner header type specified by userspace.
Patch #5 no changes in meta inner support.
Patch #6 add geneve support, this is required because it has optional
TLV area which needs to be considered to accordingly calculate the inner link layer offset.
Thanks.
Pablo Neira Ayuso (6):
netfilter: nft_payload: access GRE payload via inner offset
netfilter: nft_payload: access ipip payload for inner offset
netfilter: nft_inner: support for inner tunnel header matching
netfilter: nft_inner: add percpu inner context
netfilter: nft_meta: add inner match support
netfilter: nft_inner: add geneve support
include/net/netfilter/nf_tables.h | 6 + include/net/netfilter/nf_tables_core.h | 25 ++ include/net/netfilter/nft_meta.h | 6 + include/uapi/linux/netfilter/nf_tables.h | 27 ++ net/netfilter/Makefile | 3 +- net/netfilter/nf_tables_api.c | 37 +++ net/netfilter/nf_tables_core.c | 1 + net/netfilter/nft_inner.c | 366 +++++++++++++++++++++++ net/netfilter/nft_meta.c | 62 ++++ net/netfilter/nft_payload.c | 114 ++++++- 10 files changed, 645 insertions(+), 2 deletions(-) create mode 100644 net/netfilter/nft_inner.c
--
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 1/6 netfilter: nft_payload: access GRE payload via inner offset
Parse GRE packets to properly set up inner offset, this allow for matching on inner headers.
---
v2: interpret GRE flags to handle variable GRE header size.
net/netfilter/nft_payload.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
net/netfilter/nft_payload.c +22 -0
@@ -19,6 +19,8 @@/* For layer 4 checksum field offset. */ #include <linux/tcp.h> #include <linux/udp.h> +#include <net/gre.h> +#include <net/pptp.h> #include <linux/icmpv6.h> #include <linux/ip.h> #include <linux/ipv6.h>
@@ -100,6 +102,26 @@ static int __nft_payload_inner_offset(struct nft_pktinfo *pkt) pkt->inneroff = thoff +__tcp_hdrlen (th); }break ; +case IPPROTO_GRE : { +struct gre_base_hdr *gre, _gre; +u32 offset =0 ; + + gre =skb_header_pointer (pkt->skb, thoff,sizeof (_gre), &_gre); +if (!gre) +return -1 ; + +if (gre->flags &GRE_CSUM ) { + offset += sizeof_field(struct gre_full_hdr, csum) + + sizeof_field(struct gre_full_hdr, reserved1); + } +if (gre->flags &GRE_KEY ) + offset += sizeof_field(struct gre_full_hdr, key); +if (gre->flags &GRE_SEQ ) + offset += sizeof_field(struct pptp_gre_header, seq); + + pkt->inneroff = thoff +sizeof (struct gre_base_hdr ) + offset; + } +break ;default :return -1 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 2/6 netfilter: nft_payload: access ipip payload for inner offset
In reply to Pablo Neira Ayusoipip is an special case, transport and inner header offset are set to the same offset to use the upcoming inner expression for matching on inner tunnel headers.
---
v2: no changes
net/netfilter/nft_payload.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nft_payload.c +3 -0
@@ -122,6 +122,9 @@ static int __nft_payload_inner_offset(struct nft_pktinfo *pkt) pkt->inneroff = thoff +sizeof (struct gre_base_hdr ) + offset; }break ; +case IPPROTO_IPIP : + pkt->inneroff = thoff; +break ;default :return -1 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 3/6 netfilter: nft_inner: support for inner tunnel header matching
In reply to Pablo Neira AyusoThis new expression allows you to match on the inner headers that are encapsulated by any of the existing tunneling protocols.
This expression parses the inner packet to set the link, network and transport offsets, so the existing expressions (with a few updates) can be reused to match on the inner headers.
The inner 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)
The following fields are used to describe the tunnel protocol:
- flags, which describe how to parse the inner headers:
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 of these flags. While GRE only sets on NFT_PAYLOAD_CTX_INNER_NH and NFT_PAYLOAD_CTX_INNER_TH. Then, ESP over UDP only sets on NFT_PAYLOAD_CTX_INNER_TH.
The tunnel description is composed of the following attributes:
- header size: in case the tunnel comes with its own header, eg. VxLAN.
- 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.
- expression: currently only payload supported. Follow up patch adds also inner meta support which is required by autogenerated dependencies. The exthdr expression should be supported too at some point. There is a new inner_ops operation that needs to be set on to allow to use an existing expression from the inner expression.
This patch adds a new NFT_PAYLOAD_TUN_HEADER base which allows to match on the tunnel header fields, eg. vxlan vni.
The payload expression is embedded into nft_inner private area and this private data area is passed to the payload inner eval function via direct call.
--- include/net/netfilter/nf_tables.h | 5 + include/net/netfilter/nf_tables_core.h | 24 ++ include/uapi/linux/netfilter/nf_tables.h | 26 ++ net/netfilter/Makefile | 3 +- net/netfilter/nf_tables_api.c | 37 +++ net/netfilter/nf_tables_core.c | 1 + net/netfilter/nft_inner.c | 322 +++++++++++++++++++++++ net/netfilter/nft_payload.c | 89 ++++++- 8 files changed, 505 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 +24 -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,27 @@ 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; +__be16 llproto; +u8 l4proto; +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 +26 -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,31 @@ 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_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 +2 -1
@@ -80,7 +80,8 @@ nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \ nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.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_counter.o nft_inner.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
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 +322 -0
322 lines not on this page · raw message
net/netfilter/nft_payload.c +88 -1
107 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 4/6 netfilter: nft_inner: add percpu inner context
In reply to Pablo Neira AyusoAdd NFT_PKTINFO_INNER_FULL flag to annotate that inner offsets are available. Store nft_inner_tun_ctx object in percpu area to cache existing inner offsets for this skbuff.
--- include/net/netfilter/nf_tables.h | 1 + include/net/netfilter/nf_tables_core.h | 1 + net/netfilter/nft_inner.c | 27 ++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-)
include/net/netfilter/nf_tables.h +1 -0
@@ -24,6 +24,7 @@ struct module;enum {NFT_PKTINFO_L4PROTO = (1 <<0 ),NFT_PKTINFO_INNER = (1 <<1 ), +NFT_PKTINFO_INNER_FULL = (1 <<2 ), };struct nft_pktinfo {
include/net/netfilter/nf_tables_core.h +1 -0
@@ -148,6 +148,7 @@ enum { };struct nft_inner_tun_ctx { +u8 type;u8 inner_tunoff;u8 inner_lloff;u8 inner_nhoff;
net/netfilter/nft_inner.c +23 -4
@@ -21,6 +21,8 @@#include <linux/ip.h> #include <linux/ipv6.h> +static DEFINE_PER_CPU(struct nft_inner_tun_ctx, nft_pcpu_tun_ctx); +struct __nft_expr {const struct nft_expr_ops *ops;union {
@@ -169,7 +171,7 @@ static int nft_inner_parse_tunhdr(const struct nft_inner *priv, }static int nft_inner_parse (const struct nft_inner *priv, -const struct nft_pktinfo *pkt, +struct nft_pktinfo *pkt,struct nft_inner_tun_ctx *ctx) {u32 off = pkt->inneroff;
@@ -186,24 +188,41 @@ static int nft_inner_parse(const struct nft_inner *priv, ctx->flags |=NFT_PAYLOAD_CTX_INNER_TH ; } + ctx->type = priv->type; + pkt->flags |=NFT_PKTINFO_INNER_FULL ; +return 0 ; } +static bool nft_inner_parse_needed (const struct nft_inner *priv, +const struct nft_pktinfo *pkt, +const struct nft_inner_tun_ctx *tun_ctx) +{ +if (!(pkt->flags &NFT_PKTINFO_INNER_FULL )) +return true; + +if (priv->type != tun_ctx->type) +return true; + +return false; +} +static void nft_inner_eval (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt) { +struct nft_inner_tun_ctx *tun_ctx =this_cpu_ptr (&nft_pcpu_tun_ctx);const struct nft_inner *priv =nft_expr_priv (expr); -struct nft_inner_tun_ctx tun_ctx = {};if (nft_payload_inner_offset (pkt) <0 ) goto err; -if (nft_inner_parse (priv, pkt, &tun_ctx) <0 ) +if (nft_inner_parse_needed (priv, pkt, tun_ctx) && +nft_inner_parse (priv, (struct nft_pktinfo *)pkt, tun_ctx) <0 ) goto err;switch (priv->expr_type) {case NFT_INNER_EXPR_PAYLOAD : -nft_payload_inner_eval ((struct nft_expr *)&priv->expr, regs, pkt, &tun_ctx); +nft_payload_inner_eval ((struct nft_expr *)&priv->expr, regs, pkt, tun_ctx);break ;default :WARN_ON_ONCE (1 );
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 5/6 netfilter: nft_meta: add inner match support
In reply to Pablo Neira AyusoAdd support for inner meta matching on:
- NFT_META_PROTOCOL: to match on the ethertype, this can be used regardless tunnel protocol provides no link layer header, in that case nft_inner sets on the ethertype based on the IP header version field.
- NFT_META_L4PROTO: to match on the layer 4 protocol.
These meta expression are usually autogenerated as dependencies by userspace nftables.
--- include/net/netfilter/nft_meta.h | 6 ++++ net/netfilter/nft_inner.c | 8 +++++ net/netfilter/nft_meta.c | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+)
include/net/netfilter/nft_meta.h +6 -0
@@ -46,4 +46,10 @@ int nft_meta_set_validate(const struct nft_ctx *ctx,bool nft_meta_get_reduce (struct nft_regs_track *track,const struct nft_expr *expr); + +struct nft_inner_tun_ctx ; +void nft_meta_inner_eval (const struct nft_expr *expr, +struct nft_regs *regs,const struct nft_pktinfo *pkt, +struct nft_inner_tun_ctx *tun_ctx); +#endif
net/netfilter/nft_inner.c +8 -0
@@ -12,6 +12,7 @@#include <linux/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_core.h> #include <net/netfilter/nf_tables.h> +#include <net/netfilter/nft_meta.h> #include <net/netfilter/nf_tables_offload.h> #include <linux/tcp.h> #include <linux/udp.h>
@@ -27,11 +28,13 @@ struct __nft_expr {const struct nft_expr_ops *ops;union {struct nft_payload payload; +struct nft_meta meta; } __attribute__((aligned (__alignof__(u64 )))); };enum {NFT_INNER_EXPR_PAYLOAD , +NFT_INNER_EXPR_META , };struct nft_inner {
@@ -224,6 +227,9 @@ static void nft_inner_eval(const struct nft_expr *expr, struct nft_regs *regs,case NFT_INNER_EXPR_PAYLOAD :nft_payload_inner_eval ((struct nft_expr *)&priv->expr, regs, pkt, tun_ctx);break ; +case NFT_INNER_EXPR_META : +nft_meta_inner_eval ((struct nft_expr *)&priv->expr, regs, pkt, tun_ctx); +break ;default :WARN_ON_ONCE (1 ); goto err;
@@ -293,6 +299,8 @@ static int nft_inner_init(const struct nft_ctx *ctx,if (!strcmp (expr_info.ops->type->name,"payload" )) priv->expr_type =NFT_INNER_EXPR_PAYLOAD ; +else if (!strcmp (expr_info.ops->type->name,"meta" )) + priv->expr_type =NFT_INNER_EXPR_META ;else return -EINVAL ;
net/netfilter/nft_meta.c +62 -0
71 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 6/6 netfilter: nft_inner: add geneve support
In reply to Pablo Neira AyusoGeneve tunnel header may contain options, parse geneve header and update offset to point to the link layer header according to the opt_len field.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nft_inner.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -783,6 +783,7 @@ enum nft_payload_csum_flags {enum nft_inner_type {NFT_INNER_UNSPEC =0 ,NFT_INNER_VXLAN , +NFT_INNER_GENEVE , };enum nft_inner_flags {
net/netfilter/nft_inner.c +17 -0
@@ -17,6 +17,7 @@#include <linux/tcp.h> #include <linux/udp.h> #include <net/gre.h> +#include <net/geneve.h> #include <net/ip.h> #include <linux/icmpv6.h> #include <linux/ip.h>
@@ -170,6 +171,22 @@ static int nft_inner_parse_tunhdr(const struct nft_inner *priv, ctx->flags |=NFT_PAYLOAD_CTX_INNER_TUN ; *off += priv->hdrsize; +switch (priv->type) { +case NFT_INNER_GENEVE : { +struct genevehdr *gnvh, _gnvh; + + gnvh =skb_header_pointer (pkt->skb, pkt->inneroff, +sizeof (_gnvh), &_gnvh); +if (!gnvh) +return -1 ; + + *off += gnvh->opt_len *4 ; + } +break ; +default : +break ; + } +return 0 ; }
Signature · 1 line
2.30.2