ip_tunnel: specify tunnel type via template
Subsystems (8)
Pablo Neira Ayuso
maintainercover ip_tunnel: specify tunnel type via template
Hi,
The following patchset adds a new field to the tunnel metadata template. This new field allows us to restrict the configuration to a given tunnel driver in order to catch incorrect configuration that may result in packets going to the wrong tunnel driver.
Changes with regards to initial RFC [1] are:
1) Explicit tunnel type initialization to TUNNEL_TYPE_UNSPEC in existing clients for this code, as requested by Daniel.
2) Add TUNNEL_TYPE_* definition through enum tunnel_type in uapi/linux/if_tunnel.h, so we don't need to redefine this in every client of this infrastructure.
3) Add TUNNEL_TYPE_IPIP, TUNNEL_TYPE_IPIP6 and TUNNEL_TYPE_IP6IP6, which were missing in the original RFC.
Let me know if you any more comments, thanks.
[1] https://marc.info/?l=netfilter-devel&m=153861145204094&w=2
Pablo Neira Ayuso (3):
ip_tunnel: add type field to struct ip_tunnel_info
net: act_tunnel_key: support for tunnel type
netfilter: nft_tunnel: support for tunnel type
drivers/net/geneve.c | 3 ++- drivers/net/vxlan.c | 13 +++++++------ include/net/dst_metadata.h | 1 + include/net/ip_tunnels.h | 8 ++++++++ include/uapi/linux/if_tunnel.h | 13 ++++++++++++- include/uapi/linux/netfilter/nf_tables.h | 1 + include/uapi/linux/tc_act/tc_tunnel_key.h | 1 + net/core/filter.c | 1 + net/ipv4/ip_gre.c | 2 ++ net/ipv4/ip_tunnel.c | 3 ++- net/ipv6/ip6_gre.c | 2 ++ net/ipv6/ip6_tunnel.c | 6 ++++-- net/netfilter/nft_tunnel.c | 9 ++++++++- net/openvswitch/flow_netlink.c | 1 + net/sched/act_tunnel_key.c | 9 +++++++++ 15 files changed, 61 insertions(+), 12 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/3 ip_tunnel: add type field to struct ip_tunnel_info
This new field allows you to restrict the metadata template for a given tunnel driver. This is convenient in scenarios that combine different tunneling drivers, eg. vxlan and erspan. This helps you deal with possible incorrect configurations. Default value is TUNNEL_TYPE_UNSPEC, to retain the existing behaviour. This also implicitly exposes what drivers are currently supported in the TUNNEL_INFO_TX mode.
--- drivers/net/geneve.c | 3 ++- drivers/net/vxlan.c | 13 +++++++------ include/net/dst_metadata.h | 1 + include/net/ip_tunnels.h | 8 ++++++++ include/uapi/linux/if_tunnel.h | 13 ++++++++++++- net/core/filter.c | 1 + net/ipv4/ip_gre.c | 2 ++ net/ipv4/ip_tunnel.c | 3 ++- net/ipv6/ip6_gre.c | 2 ++ net/ipv6/ip6_tunnel.c | 6 ++++-- net/openvswitch/flow_netlink.c | 1 + 11 files changed, 42 insertions(+), 11 deletions(-)
drivers/net/geneve.c +2 -1
@@ -920,7 +920,8 @@ static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)if (geneve->collect_md) { info =skb_tunnel_info (skb); -if (unlikely (!info || !(info->mode &IP_TUNNEL_INFO_TX ))) { +if (unlikely (!info || !(info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (info,TUNNEL_TYPE_GENEVE ))) { err = -EINVAL ;netdev_dbg (dev,"no tunnel metadata\n" ); goto tx_error;
drivers/net/vxlan.c +7 -6
@@ -2296,14 +2296,15 @@ static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)skb_reset_mac_header (skb);if (vxlan->cfg.flags &VXLAN_F_COLLECT_METADATA ) { -if (info && info->mode &IP_TUNNEL_INFO_BRIDGE && - info->mode &IP_TUNNEL_INFO_TX ) { +if (unlikely (!info || !(info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (info,TUNNEL_TYPE_VXLAN ))) { +kfree_skb (skb); +return NETDEV_TX_OK ; + } +if (info->mode &IP_TUNNEL_INFO_BRIDGE ) { vni =tunnel_id_to_key32 (info->key.tun_id); }else { -if (info && info->mode &IP_TUNNEL_INFO_TX ) -vxlan_xmit_one (skb, dev, vni,NULL , false); -else -kfree_skb (skb); +vxlan_xmit_one (skb, dev, vni,NULL , false);return NETDEV_TX_OK ; } }
include/net/dst_metadata.h +1 -0
@@ -100,6 +100,7 @@ static inline struct metadata_dst *tun_rx_dst(int md_size)if (!tun_dst)return NULL ; + tun_dst->u.tun_info.type =TUNNEL_TYPE_UNSPEC ; tun_dst->u.tun_info.options_len =0 ; tun_dst->u.tun_info.mode =0 ;return tun_dst;
include/net/ip_tunnels.h +8 -0
@@ -67,6 +67,7 @@ struct ip_tunnel_key { options_len) * BITS_PER_BYTE) - 1, 0)struct ip_tunnel_info { +enum tunnel_type type;struct ip_tunnel_key key;#ifdef CONFIG_DST_CACHE struct dst_cache dst_cache;
@@ -75,6 +76,13 @@ struct ip_tunnel_info {u8 mode; }; +static inline bool ip_tunnel_type (const struct ip_tunnel_info *tun_info, +enum tunnel_type type) +{ +return tun_info->type ==TUNNEL_TYPE_UNSPEC || + tun_info->type == type; +} +/* 6rd prefix/relay information */ #ifdef CONFIG_IPV6_SIT_6RD struct ip_tunnel_6rd_parm {
include/uapi/linux/if_tunnel.h +12 -1
@@ -158,6 +158,17 @@ enum {IFLA_VTI_FWMARK , __IFLA_VTI_MAX, }; -#define IFLA_VTI_MAX (__IFLA_VTI_MAX - 1) + +enum tunnel_type { +TUNNEL_TYPE_UNSPEC =0 , +TUNNEL_TYPE_GRE , +TUNNEL_TYPE_VXLAN , +TUNNEL_TYPE_GENEVE , +TUNNEL_TYPE_ERSPAN , +TUNNEL_TYPE_IPIP , +TUNNEL_TYPE_IPIP6 , +TUNNEL_TYPE_IP6IP6 , +}; +#endif /* _UAPI_IF_TUNNEL_H_ */
net/core/filter.c +1 -0
@@ -3654,6 +3654,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, info = &md->u.tun_info;memset (info,0 ,sizeof (*info)); info->mode =IP_TUNNEL_INFO_TX ; + info->type =TUNNEL_TYPE_UNSPEC ; info->key.tun_flags =TUNNEL_KEY |TUNNEL_CSUM |TUNNEL_NOCACHE ;if (flags &BPF_F_DONT_FRAGMENT )
net/ipv4/ip_gre.c +2 -0
@@ -534,6 +534,7 @@ static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev, tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_GRE ) ||ip_tunnel_info_af (tun_info) !=AF_INET )) goto err_free_skb;
@@ -585,6 +586,7 @@ static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_ERSPAN ) ||ip_tunnel_info_af (tun_info) !=AF_INET )) goto err_free_skb;
net/ipv4/ip_tunnel.c +2 -1
@@ -562,7 +562,8 @@ void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, u8 proto) tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || -ip_tunnel_info_af (tun_info) !=AF_INET )) +ip_tunnel_info_af (tun_info) !=AF_INET ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_IPIP )) goto tx_error; key = &tun_info->key;memset (&(IPCB (skb)->opt),0 ,sizeof (IPCB (skb)->opt));
net/ipv6/ip6_gre.c +2 -0
@@ -732,6 +732,7 @@ static netdev_tx_t __gre6_xmit(struct sk_buff *skb, tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_GRE ) ||ip_tunnel_info_af (tun_info) !=AF_INET6 ))return -EINVAL ;
@@ -960,6 +961,7 @@ static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_ERSPAN ) ||ip_tunnel_info_af (tun_info) !=AF_INET6 ))return -EINVAL ;
net/ipv6/ip6_tunnel.c +4 -2
@@ -1259,7 +1259,8 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || -ip_tunnel_info_af (tun_info) !=AF_INET6 )) +ip_tunnel_info_af (tun_info) !=AF_INET6 ) || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_IPIP6 ))return -1 ; key = &tun_info->key;memset (&fl6,0 ,sizeof (fl6));
@@ -1335,7 +1336,8 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) tun_info =skb_tunnel_info (skb);if (unlikely (!tun_info || !(tun_info->mode &IP_TUNNEL_INFO_TX ) || -ip_tunnel_info_af (tun_info) !=AF_INET6 )) +ip_tunnel_info_af (tun_info) !=AF_INET6 || + !ip_tunnel_type (tun_info,TUNNEL_TYPE_IP6IP6 )))return -1 ; key = &tun_info->key;memset (&fl6,0 ,sizeof (fl6));
net/openvswitch/flow_netlink.c +1 -0
@@ -2602,6 +2602,7 @@ static int validate_and_copy_set_tun(const struct nlattr *attr, ovs_tun->tun_dst = tun_dst; tun_info = &tun_dst->u.tun_info; + tun_info->type =TUNNEL_TYPE_UNSPEC ; tun_info->mode =IP_TUNNEL_INFO_TX ;if (key.tun_proto ==AF_INET6 ) tun_info->mode |=IP_TUNNEL_INFO_IPV6 ;
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 2/3 net: act_tunnel_key: support for tunnel type
In reply to Pablo Neira AyusoThis patch allows you to set an explicit tunnel driver type in the metadata template. In case of misconfiguration, ie. if the packets ends up in the wrong tunnel device, the packet is dropped.
--- include/uapi/linux/tc_act/tc_tunnel_key.h | 1 + net/sched/act_tunnel_key.c | 9 +++++++++ 2 files changed, 10 insertions(+)
include/uapi/linux/tc_act/tc_tunnel_key.h +1 -0
@@ -41,6 +41,7 @@ enum {*/ TCA_TUNNEL_KEY_ENC_TOS ,/* u8 */ TCA_TUNNEL_KEY_ENC_TTL ,/* u8 */ +TCA_TUNNEL_KEY_ENC_TYPE ,/* u32 */ __TCA_TUNNEL_KEY_MAX, };
net/sched/act_tunnel_key.c +9 -0
@@ -195,6 +195,7 @@ static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = { [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED }, [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 }, [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 }, + [TCA_TUNNEL_KEY_ENC_TYPE] = { .type = NLA_U32 }, }; static int tunnel_key_init(struct net *net, struct nlattr *nla,
@@ -215,6 +216,7 @@ static int tunnel_key_init(struct net *net, struct nlattr *nla,__be16 flags;u8 tos, ttl;int ret =0 ; +u32 type;int err;if (!nla) {
@@ -278,6 +280,10 @@ static int tunnel_key_init(struct net *net, struct nlattr *nla,if (tb[TCA_TUNNEL_KEY_ENC_TTL ]) ttl =nla_get_u8 (tb[TCA_TUNNEL_KEY_ENC_TTL ]); + type =TUNNEL_TYPE_UNSPEC ; +if (tb[TCA_TUNNEL_KEY_ENC_TYPE ]) + type =nla_get_u32 (tb[TCA_TUNNEL_KEY_ENC_TYPE ]); +if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC ] && tb[TCA_TUNNEL_KEY_ENC_IPV4_DST ]) {__be32 saddr;
@@ -320,6 +326,7 @@ static int tunnel_key_init(struct net *net, struct nlattr *nla, goto release_tun_meta; } + metadata->u.tun_info.type = type; metadata->u.tun_info.mode |=IP_TUNNEL_INFO_TX ;break ;default :
@@ -522,6 +529,8 @@ static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,if (key->ttl &&nla_put_u8 (skb,TCA_TUNNEL_KEY_ENC_TTL , key->ttl)) goto nla_put_failure; +if (nla_put_u32 (skb,TCA_TUNNEL_KEY_ENC_TYPE , info->type)) + goto nla_put_failure; }tcf_tm_dump (&tm, &t->tcf_tm);
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/3 netfilter: nft_tunnel: support for tunnel type
In reply to Pablo Neira AyusoThis patch allows you to set an explicit tunnel driver type in the metadata template. In case of misconfiguration, ie. if the packets ends up in the wrong tunnel device, the packet is dropped.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nft_tunnel.c | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -1703,6 +1703,7 @@ enum nft_tunnel_key_attributes {NFTA_TUNNEL_KEY_SPORT ,NFTA_TUNNEL_KEY_DPORT ,NFTA_TUNNEL_KEY_OPTS , +NFTA_TUNNEL_KEY_TYPE , __NFTA_TUNNEL_KEY_MAX };#define NFTA_TUNNEL_KEY_MAX (__NFTA_TUNNEL_KEY_MAX - 1)
net/netfilter/nft_tunnel.c +8 -1
@@ -305,6 +305,7 @@ static const struct nla_policy nft_tunnel_key_policy[NFTA_TUNNEL_KEY_MAX + 1] = [NFTA_TUNNEL_KEY_TOS] = { .type = NLA_U8, }, [NFTA_TUNNEL_KEY_TTL] = { .type = NLA_U8, }, [NFTA_TUNNEL_KEY_OPTS] = { .type = NLA_NESTED, }, + [NFTA_TUNNEL_KEY_TYPE] = { .type = NLA_U32, }, }; static int nft_tunnel_obj_init(const struct nft_ctx *ctx,
@@ -312,6 +313,7 @@ static int nft_tunnel_obj_init(const struct nft_ctx *ctx, struct nft_object *obj) {struct nft_tunnel_obj *priv =nft_obj_data (obj); +u32 type =TUNNEL_TYPE_UNSPEC ;struct ip_tunnel_info info;struct metadata_dst *md;int err;
@@ -319,7 +321,11 @@ static int nft_tunnel_obj_init(const struct nft_ctx *ctx,if (!tb[NFTA_TUNNEL_KEY_ID ])return -EINVAL ; +if (tb[NFTA_TUNNEL_KEY_TYPE ]) + type =ntohl (nla_get_be32 (tb[NFTA_TUNNEL_KEY_TYPE ])); +memset (&info,0 ,sizeof (info)); + info.type = type; info.mode =IP_TUNNEL_INFO_TX ; info.key.tun_id =key32_to_tunnel_id (nla_get_be32 (tb[NFTA_TUNNEL_KEY_ID ])); info.key.tun_flags =TUNNEL_KEY |TUNNEL_CSUM |TUNNEL_NOCACHE ;
@@ -494,7 +500,8 @@ static int nft_tunnel_obj_dump(struct sk_buff *skb,struct nft_tunnel_obj *priv =nft_obj_data (obj);struct ip_tunnel_info *info = &priv->md->u.tun_info; - if (nla_put_be32(skb, NFTA_TUNNEL_KEY_ID, + if (nla_put_be32(skb, NFTA_TUNNEL_KEY_TYPE, htonl(info->type)) || + nla_put_be32(skb, NFTA_TUNNEL_KEY_ID, tunnel_id_to_key32(info->key.tun_id)) || nft_tunnel_ip_dump(skb, info) < 0 || nft_tunnel_ports_dump(skb, info) < 0 ||
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Wed, 10 Oct 2018 00:24:36 +0200
Quoted 14 lines
The following patchset adds a new field to the tunnel metadata template. This new field allows us to restrict the configuration to a given tunnel driver in order to catch incorrect configuration that may result in packets going to the wrong tunnel driver.
Changes with regards to initial RFC [1] are:
1) Explicit tunnel type initialization to
TUNNEL_TYPE_UNSPECin existing clients for this code, as requested by Daniel.2) Add TUNNEL_TYPE_* definition through enum
tunnel_typeinuapi/linux/if_tunnel.h, so we don't need to redefine this in every client of this infrastructure.
Quoted 6 lines
3) Add
TUNNEL_TYPE_IPIP,TUNNEL_TYPE_IPIP6andTUNNEL_TYPE_IP6IP6, which were missing in the original RFC.Let me know if you any more comments, thanks.
[1] https://marc.info/?l=netfilter-devel&m=153861145204094&w=2
People don't need to update a core common UAPI header to add a new ethernet driver.
They shouldn't have to do so to add a new tunneling driver either.
But that requirement is created by this patch set.
Pablo Neira Ayuso
maintainerQuoted 22 lines · David Miller
From: Pablo Neira Ayuso <[email protected]>
Date: Wed, 10 Oct 2018 00:24:36 +0200Quoted 19 lines
The following patchset adds a new field to the tunnel metadata template. This new field allows us to restrict the configuration to a given tunnel driver in order to catch incorrect configuration that may result in packets going to the wrong tunnel driver.
Changes with regards to initial RFC [1] are:
1) Explicit tunnel type initialization to
TUNNEL_TYPE_UNSPECin existing clients for this code, as requested by Daniel.2) Add TUNNEL_TYPE_* definition through enum
tunnel_typeinuapi/linux/if_tunnel.h, so we don't need to redefine this in every client of this infrastructure.3) Add
TUNNEL_TYPE_IPIP,TUNNEL_TYPE_IPIP6andTUNNEL_TYPE_IP6IP6, which were missing in the original RFC.Let me know if you any more comments, thanks.
Quoted 8 lines
Quoted 1 line
[1] https://marc.info/?l=netfilter-devel&m=153861145204094&w=2
People don't need to update a core common UAPI header to add a new ethernet driver.
They shouldn't have to do so to add a new tunneling driver either.
But that requirement is created by this patch set.
No, you can keep using TUNNEL_TYPE_UNSPEC in such scenario.
It is entirely optional and backward compatible.