diff mbox series

[4/4] batctl: event: Get ifname from netlink message

Message ID 20210510130734.41080-4-sven@narfation.org
State Accepted, archived
Delegated to: Simon Wunderlich
Headers show
Series [1/4] batctl: originators: Get outgoing ifname from netlink | expand

Commit Message

Sven Eckelmann May 10, 2021, 1:07 p.m. UTC
There is no need to gather the interface name for an interface via a
different syscall(s) when the kernel directly provides this information in
the netlink message for this event.

batman-adv provides this functionality with Linux 5.14/batman-adv 2021.2.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 event.c | 52 ++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 36 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/event.c b/event.c
index 955fe11..274f99f 100644
--- a/event.c
+++ b/event.c
@@ -174,10 +174,15 @@  static void event_parse_set_mesh(struct nlattr **attrs)
 				    ARRAY_SIZE(mesh_mandatory)))
 		return;
 
-	mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
-	meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
-	if (!meshif_name)
-		return;
+	if (attrs[BATADV_ATTR_MESH_IFNAME]) {
+		meshif_name = nla_get_string(attrs[BATADV_ATTR_MESH_IFNAME]);
+	} else {
+		/* compatibility for Linux < 5.14/batman-adv < 2021.2 */
+		mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
+		meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
+		if (!meshif_name)
+			return;
+	}
 
 	printf("%s: set mesh:\n", meshif_name);
 
@@ -305,15 +310,25 @@  static void event_parse_set_hardif(struct nlattr **attrs)
 				    ARRAY_SIZE(hardif_mandatory)))
 		return;
 
-	mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
-	meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
-	if (!meshif_name)
-		return;
+	if (attrs[BATADV_ATTR_MESH_IFNAME]) {
+		meshif_name = nla_get_string(attrs[BATADV_ATTR_MESH_IFNAME]);
+	} else {
+		/* compatibility for Linux < 5.14/batman-adv < 2021.2 */
+		mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
+		meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
+		if (!meshif_name)
+			return;
+	}
 
-	hardif_ifindex = nla_get_u32(attrs[BATADV_ATTR_HARD_IFINDEX]);
-	hardif_name = if_indextoname(hardif_ifindex, hardif_buf);
-	if (!hardif_name)
-		return;
+	if (attrs[BATADV_ATTR_MESH_IFNAME]) {
+		hardif_name = nla_get_string(attrs[BATADV_ATTR_HARD_IFNAME]);
+	} else {
+		/* compatibility for Linux < 5.14/batman-adv < 2021.2 */
+		hardif_ifindex = nla_get_u32(attrs[BATADV_ATTR_HARD_IFINDEX]);
+		hardif_name = if_indextoname(hardif_ifindex, hardif_buf);
+		if (!hardif_name)
+			return;
+	}
 
 	printf("%s (%s): set hardif:\n", meshif_name, hardif_name);
 
@@ -350,10 +365,15 @@  static void event_parse_set_vlan(struct nlattr **attrs)
 				    ARRAY_SIZE(vlan_mandatory)))
 		return;
 
-	mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
-	meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
-	if (!meshif_name)
-		return;
+	if (attrs[BATADV_ATTR_MESH_IFNAME]) {
+		meshif_name = nla_get_string(attrs[BATADV_ATTR_MESH_IFNAME]);
+	} else {
+		/* compatibility for Linux < 5.14/batman-adv < 2021.2 */
+		mesh_ifindex = nla_get_u32(attrs[BATADV_ATTR_MESH_IFINDEX]);
+		meshif_name = if_indextoname(mesh_ifindex, meshif_buf);
+		if (!meshif_name)
+			return;
+	}
 
 	vid = nla_get_u16(attrs[BATADV_ATTR_VLANID]);