From 8157559f1a074cfddfabe7aa00182a53186fc062 Mon Sep 17 00:00:00 2001
From: Thomas Devoogdt <thomas.devoogdt@barco.com>
Date: Mon, 12 Aug 2024 11:08:23 +0200
Subject: [PATCH] rtnl: check if the kernel provides if_team

"net: introduce ethernet teaming device" was introduced in Linux v3.3 [1],
this patch adds support for older kernels

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3d249d4ca7d0ed6629a135ea1ea21c72286c0d80

Signed-off-by: Thomas Devoogdt <thomas.devoogdt@barco.com>
---
 incdefs.sh |  5 +++++
 missing.h  | 39 +++++++++++++++++++++++++++++++++++++++
 rtnl.c     |  2 ++
 3 files changed, 46 insertions(+)

diff --git a/incdefs.sh b/incdefs.sh
index 6ab59dc7..060cc0bf 100755
--- a/incdefs.sh
+++ b/incdefs.sh
@@ -154,6 +154,7 @@ kernel_flags()
 	prefix=""
 	tstamp=/usr/include/linux/net_tstamp.h
 	ptp_clock=/usr/include/linux/ptp_clock.h
+	if_team=/usr/include/linux/if_team.h
 
 	if [ "x$KBUILD_OUTPUT" != "x" ]; then
 		# With KBUILD_OUTPUT set, we are building against
@@ -185,6 +186,10 @@ kernel_flags()
 	if grep -q adjust_phase ${prefix}${ptp_clock}; then
 		printf " -DHAVE_PTP_CAPS_ADJUST_PHASE"
 	fi
+
+	if grep -q -s TEAM_GENL_NAME ${prefix}${if_team}; then
+		printf " -DHAVE_IF_TEAM"
+	fi
 }
 
 flags="$(user_flags)$(kernel_flags)"
diff --git a/missing.h b/missing.h
index fde1a3db..c6be8fdb 100644
--- a/missing.h
+++ b/missing.h
@@ -78,6 +78,45 @@ struct so_timestamping {
 };
 #endif
 
+#ifndef HAVE_IF_TEAM
+#define TEAM_GENL_NAME		"team"
+#define TEAM_GENL_VERSION	1
+
+enum {
+	TEAM_ATTR_UNSPEC,
+	TEAM_ATTR_TEAM_IFINDEX,
+	TEAM_ATTR_LIST_OPTION,
+	TEAM_ATTR_LIST_PORT,
+
+	__TEAM_ATTR_MAX,
+	TEAM_ATTR_MAX = (__TEAM_ATTR_MAX - 1)
+};
+
+enum {
+	TEAM_ATTR_OPTION_UNSPEC,
+	TEAM_ATTR_OPTION_NAME,
+	TEAM_ATTR_OPTION_CHANGED,
+	TEAM_ATTR_OPTION_TYPE,
+	TEAM_ATTR_OPTION_DATA,
+	TEAM_ATTR_OPTION_REMOVED,
+	TEAM_ATTR_OPTION_PORT_IFINDEX,
+	TEAM_ATTR_OPTION_ARRAY_INDEX,
+
+	__TEAM_ATTR_OPTION_MAX,
+	TEAM_ATTR_OPTION_MAX = (__TEAM_ATTR_OPTION_MAX - 1)
+};
+
+enum {
+	TEAM_CMD_NOOP,
+	TEAM_CMD_OPTIONS_SET,
+	TEAM_CMD_OPTIONS_GET,
+	TEAM_CMD_PORT_LIST_GET,
+
+	__TEAM_CMD_MAX,
+	TEAM_CMD_MAX = (__TEAM_CMD_MAX - 1)
+};
+#endif
+
 #ifndef HWTSTAMP_FLAG_BONDED_PHC_INDEX
 enum {
 	HWTSTAMP_FLAG_BONDED_PHC_INDEX = (1<<0),
diff --git a/rtnl.c b/rtnl.c
index fa023886..1037c449 100644
--- a/rtnl.c
+++ b/rtnl.c
@@ -24,7 +24,9 @@
 #endif
 #include <linux/rtnetlink.h>
 #include <linux/genetlink.h>
+#ifdef HAVE_IF_TEAM
 #include <linux/if_team.h>
+#endif
 #include <net/if.h>
 #include <stdio.h>
 #include <stdlib.h>
