diff mbox series

[net-next,v3,05/13] flow_dissector: UDP encap infrastructure

Message ID 20240821212212.1795357-6-tom@herbertland.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series flow_dissector: Dissect UDP encapsulation protocols | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 58 this patch: 58
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: pabeni@redhat.com ast@fiberby.net dcaratti@redhat.com
netdev/build_clang success Errors and warnings before: 116 this patch: 116
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4962 this patch: 4962
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 174 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 47 this patch: 47
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-08-22--03-00 (tests: 711)

Commit Message

Tom Herbert Aug. 21, 2024, 9:22 p.m. UTC
Add infrastructure for parsing into UDP encapsulations

Add function __skb_flow_dissect_udp that is called for IPPROTO_UDP.
The flag FLOW_DISSECTOR_F_PARSE_UDP_ENCAPS enables parsing of UDP
encapsulations. If the flag is set when parsing a UDP packet then
a socket lookup is performed. The offset of the base network header,
either an IPv4 or IPv6 header, is tracked and passed to
__skb_flow_dissect_udp so that it can perform the socket lookup

If a socket is found and it's for a UDP encapsulation (encap_type is
set in the UDP socket) then a switch is performed on the encap_type
value (cases are UDP_ENCAP_* values)

An encapsulated packet in UDP can either be indicated by an
EtherType or IP protocol. The processing for dissecting a UDP encap
protocol returns a flow dissector return code. If
FLOW_DISSECT_RET_PROTO_AGAIN or FLOW_DISSECT_RET_IPPROTO_AGAIN is
returned then the corresponding  encapsulated protocol is dissected.
The nhoff is set to point to the header to process.  In the case
FLOW_DISSECT_RET_PROTO_AGAIN the EtherType protocol is returned and
the IP protocol is set to zero. In the case of
FLOW_DISSECT_RET_IPPROTO_AGAIN, the IP protocol is returned and
the EtherType protocol is returned unchanged

Signed-off-by: Tom Herbert <tom@herbertland.com>
---
 include/net/flow_dissector.h |   1 +
 net/core/flow_dissector.c    | 137 +++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)

Comments

kernel test robot Aug. 22, 2024, 8:25 a.m. UTC | #1
Hi Tom,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Tom-Herbert/ipv6-Add-udp6_lib_lookup-to-IPv6-stubs/20240822-052515
base:   net-next/main
patch link:    https://lore.kernel.org/r/20240821212212.1795357-6-tom%40herbertland.com
patch subject: [PATCH net-next v3 05/13] flow_dissector: UDP encap infrastructure
config: x86_64-buildonly-randconfig-001-20240822 (https://download.01.org/0day-ci/archive/20240822/202408221600.x6NZMg4C-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240822/202408221600.x6NZMg4C-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202408221600.x6NZMg4C-lkp@intel.com/

All warnings (new ones prefixed by >>):

   net/core/flow_dissector.c: In function '__skb_flow_dissect_udp':
>> net/core/flow_dissector.c:821:22: warning: unused variable 'sk' [-Wunused-variable]
     821 |         struct sock *sk;
         |                      ^~
>> net/core/flow_dissector.c:819:30: warning: unused variable 'udph' [-Wunused-variable]
     819 |         const struct udphdr *udph;
         |                              ^~~~


vim +/sk +821 net/core/flow_dissector.c

   809	
   810	static enum flow_dissect_ret
   811	__skb_flow_dissect_udp(const struct sk_buff *skb, const struct net *net,
   812			       struct flow_dissector *flow_dissector,
   813			       void *target_container, const void *data,
   814			       int *p_nhoff, int hlen, __be16 *p_proto,
   815			       u8 *p_ip_proto, int base_nhoff, unsigned int flags,
   816			       unsigned int num_hdrs)
   817	{
   818		enum flow_dissect_ret ret;
 > 819		const struct udphdr *udph;
   820		struct udphdr _udph;
 > 821		struct sock *sk;
   822		__u8 encap_type;
   823		int nhoff;
   824	
   825		if (!(flags & FLOW_DISSECTOR_F_PARSE_UDP_ENCAPS))
   826			return FLOW_DISSECT_RET_OUT_GOOD;
   827	
   828		/* Check that the netns for the skb device is the same as the caller's,
   829		 * and only dissect UDP if we haven't yet encountered any encapsulation.
   830		 * The goal is to ensure that the socket lookup is being done in the
   831		 * right netns. Encapsulations may push packets into different name
   832		 * spaces, so this scheme is restricting UDP dissection to cases where
   833		 * they are in the same name spaces or at least the original name space.
   834		 * This should capture the majority of use cases for UDP encaps, and
   835		 * if we do encounter a UDP encapsulation within a different namespace
   836		 * then the only effect is we don't attempt UDP dissection
   837		 */
   838		if (dev_net(skb->dev) != net || num_hdrs > 0)
   839			return FLOW_DISSECT_RET_OUT_GOOD;
   840	
   841		switch (*p_proto) {
   842	#ifdef CONFIG_INET
   843		case htons(ETH_P_IP): {
   844			const struct iphdr *iph;
   845			struct iphdr _iph;
   846	
   847			iph = __skb_header_pointer(skb, base_nhoff, sizeof(_iph), data,
   848						   hlen, &_iph);
   849			if (!iph)
   850				return FLOW_DISSECT_RET_OUT_BAD;
   851	
   852			udph = __skb_header_pointer(skb, *p_nhoff, sizeof(_udph), data,
   853						    hlen, &_udph);
   854			if (!udph)
   855				return FLOW_DISSECT_RET_OUT_BAD;
   856	
   857			rcu_read_lock();
   858			/* Look up the UDPv4 socket and get the encap_type */
   859			sk = __udp4_lib_lookup(net, iph->saddr, udph->source,
   860					       iph->daddr, udph->dest,
   861					       inet_iif(skb), inet_sdif(skb),
   862					       net->ipv4.udp_table, NULL);
   863			if (!sk || !udp_sk(sk)->encap_type) {
   864				rcu_read_unlock();
   865				return FLOW_DISSECT_RET_OUT_GOOD;
   866			}
   867	
   868			encap_type = udp_sk(sk)->encap_type;
   869			rcu_read_unlock();
   870	
   871			break;
   872		}
   873	#if IS_ENABLED(CONFIG_IPV6)
   874		case htons(ETH_P_IPV6): {
   875			const struct ipv6hdr *iph;
   876			struct ipv6hdr _iph;
   877	
   878			if (!likely(ipv6_stub))
   879				return FLOW_DISSECT_RET_OUT_GOOD;
   880	
   881			iph = __skb_header_pointer(skb, base_nhoff, sizeof(_iph), data,
   882						   hlen, &_iph);
   883			if (!iph)
   884				return FLOW_DISSECT_RET_OUT_BAD;
   885	
   886			udph = __skb_header_pointer(skb, *p_nhoff, sizeof(_udph), data,
   887						    hlen, &_udph);
   888			if (!udph)
   889				return FLOW_DISSECT_RET_OUT_BAD;
   890	
   891			rcu_read_lock();
   892			/* Look up the UDPv6 socket and get the encap_type */
   893			sk = ipv6_stub->udp6_lib_lookup(net,
   894					&iph->saddr, udph->source,
   895					&iph->daddr, udph->dest,
   896					inet_iif(skb), inet_sdif(skb),
   897					net->ipv4.udp_table, NULL);
   898	
   899			if (!sk || !udp_sk(sk)->encap_type) {
   900				rcu_read_unlock();
   901				return FLOW_DISSECT_RET_OUT_GOOD;
   902			}
   903	
   904			encap_type = udp_sk(sk)->encap_type;
   905			rcu_read_unlock();
   906	
   907			break;
   908		}
   909	#endif /* CONFIG_IPV6 */
   910	#endif /* CONFIG_INET */
   911		default:
   912			return FLOW_DISSECT_RET_OUT_GOOD;
   913		}
   914	
   915		nhoff = *p_nhoff + sizeof(_udph);
   916		ret = FLOW_DISSECT_RET_OUT_GOOD;
   917	
   918		switch (encap_type) {
   919		default:
   920			break;
   921		}
   922	
   923		switch (ret) {
   924		case FLOW_DISSECT_RET_PROTO_AGAIN:
   925			*p_ip_proto = 0;
   926			fallthrough;
   927		case FLOW_DISSECT_RET_IPPROTO_AGAIN:
   928			*p_nhoff = nhoff;
   929			break;
   930		default:
   931			break;
   932		}
   933	
   934		return ret;
   935	}
   936
diff mbox series

Patch

diff --git a/include/net/flow_dissector.h b/include/net/flow_dissector.h
index ced79dc8e856..8a868a88a6f1 100644
--- a/include/net/flow_dissector.h
+++ b/include/net/flow_dissector.h
@@ -384,6 +384,7 @@  enum flow_dissector_key_id {
 #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(1)
 #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(2)
 #define FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP	BIT(3)
+#define FLOW_DISSECTOR_F_PARSE_UDP_ENCAPS	BIT(4)
 
 struct flow_dissector_key {
 	enum flow_dissector_key_id key_id;
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index 5170676a224c..a5b1b1badc67 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -13,6 +13,7 @@ 
 #include <net/gre.h>
 #include <net/pptp.h>
 #include <net/tipc.h>
+#include <net/udp.h>
 #include <linux/igmp.h>
 #include <linux/icmp.h>
 #include <linux/sctp.h>
@@ -806,6 +807,133 @@  __skb_flow_dissect_batadv(const struct sk_buff *skb,
 	return FLOW_DISSECT_RET_PROTO_AGAIN;
 }
 
+static enum flow_dissect_ret
+__skb_flow_dissect_udp(const struct sk_buff *skb, const struct net *net,
+		       struct flow_dissector *flow_dissector,
+		       void *target_container, const void *data,
+		       int *p_nhoff, int hlen, __be16 *p_proto,
+		       u8 *p_ip_proto, int base_nhoff, unsigned int flags,
+		       unsigned int num_hdrs)
+{
+	enum flow_dissect_ret ret;
+	const struct udphdr *udph;
+	struct udphdr _udph;
+	struct sock *sk;
+	__u8 encap_type;
+	int nhoff;
+
+	if (!(flags & FLOW_DISSECTOR_F_PARSE_UDP_ENCAPS))
+		return FLOW_DISSECT_RET_OUT_GOOD;
+
+	/* Check that the netns for the skb device is the same as the caller's,
+	 * and only dissect UDP if we haven't yet encountered any encapsulation.
+	 * The goal is to ensure that the socket lookup is being done in the
+	 * right netns. Encapsulations may push packets into different name
+	 * spaces, so this scheme is restricting UDP dissection to cases where
+	 * they are in the same name spaces or at least the original name space.
+	 * This should capture the majority of use cases for UDP encaps, and
+	 * if we do encounter a UDP encapsulation within a different namespace
+	 * then the only effect is we don't attempt UDP dissection
+	 */
+	if (dev_net(skb->dev) != net || num_hdrs > 0)
+		return FLOW_DISSECT_RET_OUT_GOOD;
+
+	switch (*p_proto) {
+#ifdef CONFIG_INET
+	case htons(ETH_P_IP): {
+		const struct iphdr *iph;
+		struct iphdr _iph;
+
+		iph = __skb_header_pointer(skb, base_nhoff, sizeof(_iph), data,
+					   hlen, &_iph);
+		if (!iph)
+			return FLOW_DISSECT_RET_OUT_BAD;
+
+		udph = __skb_header_pointer(skb, *p_nhoff, sizeof(_udph), data,
+					    hlen, &_udph);
+		if (!udph)
+			return FLOW_DISSECT_RET_OUT_BAD;
+
+		rcu_read_lock();
+		/* Look up the UDPv4 socket and get the encap_type */
+		sk = __udp4_lib_lookup(net, iph->saddr, udph->source,
+				       iph->daddr, udph->dest,
+				       inet_iif(skb), inet_sdif(skb),
+				       net->ipv4.udp_table, NULL);
+		if (!sk || !udp_sk(sk)->encap_type) {
+			rcu_read_unlock();
+			return FLOW_DISSECT_RET_OUT_GOOD;
+		}
+
+		encap_type = udp_sk(sk)->encap_type;
+		rcu_read_unlock();
+
+		break;
+	}
+#if IS_ENABLED(CONFIG_IPV6)
+	case htons(ETH_P_IPV6): {
+		const struct ipv6hdr *iph;
+		struct ipv6hdr _iph;
+
+		if (!likely(ipv6_stub))
+			return FLOW_DISSECT_RET_OUT_GOOD;
+
+		iph = __skb_header_pointer(skb, base_nhoff, sizeof(_iph), data,
+					   hlen, &_iph);
+		if (!iph)
+			return FLOW_DISSECT_RET_OUT_BAD;
+
+		udph = __skb_header_pointer(skb, *p_nhoff, sizeof(_udph), data,
+					    hlen, &_udph);
+		if (!udph)
+			return FLOW_DISSECT_RET_OUT_BAD;
+
+		rcu_read_lock();
+		/* Look up the UDPv6 socket and get the encap_type */
+		sk = ipv6_stub->udp6_lib_lookup(net,
+				&iph->saddr, udph->source,
+				&iph->daddr, udph->dest,
+				inet_iif(skb), inet_sdif(skb),
+				net->ipv4.udp_table, NULL);
+
+		if (!sk || !udp_sk(sk)->encap_type) {
+			rcu_read_unlock();
+			return FLOW_DISSECT_RET_OUT_GOOD;
+		}
+
+		encap_type = udp_sk(sk)->encap_type;
+		rcu_read_unlock();
+
+		break;
+	}
+#endif /* CONFIG_IPV6 */
+#endif /* CONFIG_INET */
+	default:
+		return FLOW_DISSECT_RET_OUT_GOOD;
+	}
+
+	nhoff = *p_nhoff + sizeof(_udph);
+	ret = FLOW_DISSECT_RET_OUT_GOOD;
+
+	switch (encap_type) {
+	default:
+		break;
+	}
+
+	switch (ret) {
+	case FLOW_DISSECT_RET_PROTO_AGAIN:
+		*p_ip_proto = 0;
+		fallthrough;
+	case FLOW_DISSECT_RET_IPPROTO_AGAIN:
+		*p_nhoff = nhoff;
+		break;
+	default:
+		break;
+	}
+
+	return ret;
+}
+
 static void
 __skb_flow_dissect_tcp(const struct sk_buff *skb,
 		       struct flow_dissector *flow_dissector,
@@ -1046,6 +1174,7 @@  bool __skb_flow_dissect(const struct net *net,
 	int mpls_lse = 0;
 	int num_hdrs = 0;
 	u8 ip_proto = 0;
+	int base_nhoff;
 	bool ret;
 
 	if (!data) {
@@ -1168,6 +1297,7 @@  bool __skb_flow_dissect(const struct net *net,
 
 proto_again:
 	fdret = FLOW_DISSECT_RET_CONTINUE;
+	base_nhoff = nhoff;
 
 	switch (proto) {
 	case htons(ETH_P_IP): {
@@ -1649,6 +1779,13 @@  bool __skb_flow_dissect(const struct net *net,
 				       data, nhoff, hlen);
 		break;
 
+	case IPPROTO_UDP:
+		fdret = __skb_flow_dissect_udp(skb, net, flow_dissector,
+					       target_container, data, &nhoff,
+					       hlen, &proto, &ip_proto,
+					       base_nhoff, flags, num_hdrs);
+		break;
+
 	case IPPROTO_ICMP:
 	case IPPROTO_ICMPV6:
 		__skb_flow_dissect_icmp(skb, flow_dissector, target_container,