diff mbox series

[nf-next,v2,3/4] netfilter: nft_tunnel: support NFT_TUNNEL_IPV6_SRC/DST match

Message ID 1573890564-16500-4-git-send-email-wenxu@ucloud.cn
State Deferred
Delegated to: Pablo Neira
Headers show
Series netfilter: nft_tunnel: support tunnel match expr offload | expand

Commit Message

wenxu Nov. 16, 2019, 7:49 a.m. UTC
From: wenxu <wenxu@ucloud.cn>

Add new two NFT_TUNNEL_IPV6_SRC/DST match in nft_tunnel

Signed-off-by: wenxu <wenxu@ucloud.cn>
---
v2: add nft_tunnel_mode_match_ip6

 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nft_tunnel.c               | 36 ++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
diff mbox series

Patch

diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
index 1621d72..d067ee7 100644
--- a/include/uapi/linux/netfilter/nf_tables.h
+++ b/include/uapi/linux/netfilter/nf_tables.h
@@ -1779,6 +1779,8 @@  enum nft_tunnel_keys {
 	NFT_TUNNEL_ID,
 	NFT_TUNNEL_IPV4_SRC,
 	NFT_TUNNEL_IPV4_DST,
+	NFT_TUNNEL_IPV6_SRC,
+	NFT_TUNNEL_IPV6_DST,
 	__NFT_TUNNEL_MAX
 };
 #define NFT_TUNNEL_MAX	(__NFT_TUNNEL_MAX - 1)
diff --git a/net/netfilter/nft_tunnel.c b/net/netfilter/nft_tunnel.c
index 67f7718..943a704 100644
--- a/net/netfilter/nft_tunnel.c
+++ b/net/netfilter/nft_tunnel.c
@@ -41,6 +41,16 @@  static bool nft_tunnel_mode_match_ip(enum nft_tunnel_mode priv_mode,
 	return false;
 }
 
+static bool nft_tunnel_mode_match_ip6(enum nft_tunnel_mode priv_mode,
+				       struct ip_tunnel_info *tun_info)
+{
+	if (nft_tunnel_mode_match(priv_mode, tun_info->mode) &&
+	    ip_tunnel_info_af(tun_info) == AF_INET6)
+		return true;
+
+	return false;
+}
+
 static void nft_tunnel_get_eval(const struct nft_expr *expr,
 				struct nft_regs *regs,
 				const struct nft_pktinfo *pkt)
@@ -90,6 +100,28 @@  static void nft_tunnel_get_eval(const struct nft_expr *expr,
 		else
 			regs->verdict.code = NFT_BREAK;
 		break;
+	case NFT_TUNNEL_IPV6_SRC:
+		if (!tun_info) {
+			regs->verdict.code = NFT_BREAK;
+			return;
+		}
+		if (nft_tunnel_mode_match_ip6(priv->mode, tun_info))
+			memcpy(dest, &tun_info->key.u.ipv6.src,
+			       sizeof(struct in6_addr));
+		else
+			regs->verdict.code = NFT_BREAK;
+		break;
+	case NFT_TUNNEL_IPV6_DST:
+		if (!tun_info) {
+			regs->verdict.code = NFT_BREAK;
+			return;
+		}
+		if (nft_tunnel_mode_match_ip6(priv->mode, tun_info))
+			memcpy(dest, &tun_info->key.u.ipv6.dst,
+			       sizeof(struct in6_addr));
+		else
+			regs->verdict.code = NFT_BREAK;
+		break;
 	default:
 		WARN_ON(1);
 		regs->verdict.code = NFT_BREAK;
@@ -123,6 +155,10 @@  static int nft_tunnel_get_init(const struct nft_ctx *ctx,
 	case NFT_TUNNEL_IPV4_DST:
 		len = sizeof(u32);
 		break;
+	case NFT_TUNNEL_IPV6_SRC:
+	case NFT_TUNNEL_IPV6_DST:
+		len = sizeof(struct in6_addr);
+		break;
 	default:
 		return -EOPNOTSUPP;
 	}