Skip to contentSkip to conversation

netlink: Add l_netlink_iter

PATCH In review Denis Kenzior 1 participant 7 messages Aug 23, 2024 – Aug 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 netlink: Add l_netlink_iter

Move netlink attribute iterator out of genl.c and into netlink.c. Both genl and netlink use the same attribute structure, and is applicable to both. This makes implementing netlink and rtnl message parsers much easier.

In order to keep source compatibility, structure the netlink iterator in the same way as the genl iterator. In the future, the use of genl iterators will be removed.

---
 ell/ell.sym   |  5 ++--
 ell/genl.c    | 76 +++------------------------------------------------
 ell/genl.h    | 18 +++++++++---
 ell/netlink.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/netlink.h | 15 ++++++++++
 5 files changed, 111 insertions(+), 78 deletions(-)
ell/ell.sym +3 -2
index c5d1c2a9edea..1499081cb25e 100644
@@ -344,8 +344,6 @@ global:
 	l_genl_msg_enter_nested;
 	l_genl_msg_leave_nested;
 	l_genl_attr_init;
-	l_genl_attr_next;
-	l_genl_attr_recurse;
 	l_genl_family_info_can_send;
 	l_genl_family_info_can_dump;
 	l_genl_family_info_has_group;
@@ -445,6 +443,9 @@ global:
 	l_netlink_message_add_header;
 	l_netlink_message_enter_nested;
 	l_netlink_message_leave_nested;
+	l_netlink_attr_init;
+	l_netlink_attr_next;
+	l_netlink_attr_recurse;
 	/* path */
 	l_basename;
 	l_path_find;
ell/genl.c +4 -72

84 lines not on this page · raw message

ell/genl.h +14 -4
index 3f38fd9d5710..ed5f17c4fd2e 100644
@@ -91,10 +91,20 @@ bool l_genl_msg_enter_nested(struct l_genl_msg *msg, uint16_t type);
 bool l_genl_msg_leave_nested(struct l_genl_msg *msg);
 
 bool l_genl_attr_init(struct l_genl_attr *attr, struct l_genl_msg *msg);
-bool l_genl_attr_next(struct l_genl_attr *attr, uint16_t *type,
-					uint16_t *len, const void **data);
-bool l_genl_attr_recurse(const struct l_genl_attr *attr,
-				struct l_genl_attr *nested);
+
+static inline bool l_genl_attr_next(struct l_genl_attr *attr, uint16_t *type,
+					uint16_t *len, const void **data)
+{
+	return l_netlink_attr_next((struct l_netlink_attr *) attr,
+					type, len, data) == 0;
+}
+
+static inline bool l_genl_attr_recurse(const struct l_genl_attr *attr,
+				struct l_genl_attr *nested)
+{
+	return l_netlink_attr_recurse((struct l_netlink_attr *) attr,
+					(struct l_netlink_attr *) nested) == 0;
+}
 
 bool l_genl_family_info_has_group(const struct l_genl_family_info *info,
 					const char *group);
ell/netlink.c +75 -0

78 lines not on this page · raw message

ell/netlink.h +15 -0
index 08aff74fbac2..f09d42043675 100644
@@ -119,6 +119,21 @@ static inline int l_netlink_message_append_mac(struct l_netlink_message *message
 	return l_netlink_message_append(message, type, mac, 6);
 }
 
+struct l_netlink_attr {
+	const struct nlattr *data;
+	uint32_t len;
+	const struct nlattr *next_data;
+	uint32_t next_len;
+};
+
+int l_netlink_attr_init(struct l_netlink_attr *attr, size_t header_len,
+					const void *data, uint32_t len);
+int l_netlink_attr_next(struct l_netlink_attr *attr,
+					uint16_t *type, uint16_t *len,
+					const void **data);
+int l_netlink_attr_recurse(const struct l_netlink_attr *iter,
+					struct l_netlink_attr *nested);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/6 unit: Use the new l_netlink_attr API

---
 unit/test-netlink.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
unit/test-netlink.c +16 -13
index da23ee5c9b0e..113d735adf40 100644
@@ -27,31 +27,34 @@ static void getlink_callback(int error, uint16_t type, const void *data,
 						uint32_t len, void *user_data)
 {
 	const struct ifinfomsg *ifi = data;
-	struct rtattr *rta;
-	int bytes;
+	struct l_netlink_attr attr;
 	char ifname[IF_NAMESIZE];
 	uint32_t index, flags;
+	uint16_t rta_type;
+	uint16_t rta_len;
+	const void *rta_data;
 
 	if (error)
 		goto done;
 
-	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
+	if (l_netlink_attr_init(&attr, sizeof(struct ifinfomsg), data, len) < 0)
+		goto done;
 
 	memset(ifname, 0, sizeof(ifname));
 
-	index = ifi->ifi_index;
-	flags = ifi->ifi_flags;
+	while (!l_netlink_attr_next(&attr, &rta_type, &rta_len, &rta_data)) {
+		if (rta_type != IFLA_IFNAME)
+			continue;
+
+		if (rta_len <= IF_NAMESIZE)
+			strcpy(ifname, rta_data);
 
-	for (rta = IFLA_RTA(ifi); RTA_OK(rta, bytes);
-					rta = RTA_NEXT(rta, bytes)) {
-		switch (rta->rta_type) {
-		case IFLA_IFNAME:
-			if (RTA_PAYLOAD(rta) <= IF_NAMESIZE)
-				strcpy(ifname, RTA_DATA(rta));
-			break;
-		}
+		break;
 	}
 
+	index = ifi->ifi_index;
+	flags = ifi->ifi_flags;
+
 	l_info("index=%d flags=0x%08x name=%s", index, flags, ifname);
 
 done:
Signature · 1 line
2.45.2
#2 lore raw

PATCH 3/6 netlink: Add l_netlink_message_append_string

In reply to Denis Kenzior
---
 ell/netlink.h | 9 +++++++++
 1 file changed, 9 insertions(+)
ell/netlink.h +9 -0
index f09d42043675..b51748195ca6 100644
@@ -10,6 +10,7 @@
 
 #include <stdbool.h>
 #include <stdint.h>
+#include <string.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -119,6 +120,14 @@ static inline int l_netlink_message_append_mac(struct l_netlink_message *message
 	return l_netlink_message_append(message, type, mac, 6);
 }
 
+static inline int l_netlink_message_append_string(
+					struct l_netlink_message *message,
+					uint16_t type,
+					const char *str)
+{
+	return l_netlink_message_append(message, type, str, strlen(str) + 1);
+}
+
 struct l_netlink_attr {
 	const struct nlattr *data;
 	uint32_t len;
Signature · 1 line
2.45.2
#3 lore raw

PATCH 4/6 netlink: Add l_netlink_request_sent

In reply to Denis Kenzior

This is similar to l_genl_family_request_sent()

---
 ell/ell.sym   |  1 +
 ell/netlink.c | 18 ++++++++++++++++++
 ell/netlink.h |  1 +
 3 files changed, 20 insertions(+)
ell/ell.sym +1 -0
index 1499081cb25e..cbeb61ff0f46 100644
@@ -431,6 +431,7 @@ global:
 	l_netlink_destroy;
 	l_netlink_send;
 	l_netlink_cancel;
+	l_netlink_request_sent;
 	l_netlink_register;
 	l_netlink_unregister;
 	l_netlink_set_debug;
ell/netlink.c +18 -0
index 0be3009c736f..7e6ef0f760e7 100644
@@ -467,6 +467,24 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
 	return true;
 }
 
+LIB_EXPORT bool l_netlink_request_sent(struct l_netlink *netlink,
+							unsigned int id)
+{
+	struct command *command;
+	struct nlmsghdr *hdr;
+
+	if (unlikely(!netlink || !id))
+		return false;
+
+	command = l_hashmap_lookup(netlink->command_lookup, L_UINT_TO_PTR(id));
+	if (!command)
+		return false;
+
+	hdr = command->message->hdr;
+	return l_hashmap_lookup(netlink->command_pending,
+					L_UINT_TO_PTR(hdr->nlmsg_seq));
+}
+
 static bool add_membership(struct l_netlink *netlink, uint32_t group)
 {
 	int sk, value = group;
ell/netlink.h +1 -0
index b51748195ca6..368f38619155 100644
@@ -37,6 +37,7 @@ unsigned int l_netlink_send(struct l_netlink *netlink,
 				void *user_data,
 				l_netlink_destroy_func_t destroy);
 bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id);
+bool l_netlink_request_sent(struct l_netlink *netlink, unsigned int id);
 
 unsigned int l_netlink_register(struct l_netlink *netlink,
 			uint32_t group, l_netlink_notify_func_t function,
Signature · 1 line
2.45.2
#4 lore raw

PATCH 5/6 util: Handle NULL pointers in l_safe_ato* functions

In reply to Denis Kenzior
---
 ell/util.c | 6 ++++++
 1 file changed, 6 insertions(+)
ell/util.c +6 -0
index 45ec6c8110bc..f474098c082b 100644
@@ -760,6 +760,9 @@ static int safe_atou(const char *s, int base, unsigned int *out_u)
 
 LIB_EXPORT int l_safe_atou32(const char *s, uint32_t *out_u)
 {
+	if (unlikely(!s))
+		return -EINVAL;
+
 	if (!l_ascii_isdigit(s[0]))
 		return -EINVAL;
 
@@ -808,6 +811,9 @@ LIB_EXPORT int l_safe_atox16(const char *s, uint16_t *out_x)
 
 LIB_EXPORT int l_safe_atox32(const char *s, uint32_t *out_x)
 {
+	if (unlikely(!s))
+		return -EINVAL;
+
 	if (!l_ascii_isxdigit(s[0]))
 		return -EINVAL;
 
Signature · 1 line
2.45.2
#5 lore raw

PATCH 6/6 rtnl: Add l_rtnl_link_set_mtu

In reply to Denis Kenzior

This helper is used to set a given MTU on a network interface (link).

---
 ell/ell.sym |  1 +
 ell/rtnl.c  | 18 ++++++++++++++++++
 ell/rtnl.h  |  4 ++++
 3 files changed, 23 insertions(+)
ell/ell.sym +1 -0
index cbeb61ff0f46..00123136a527 100644
@@ -701,6 +701,7 @@ global:
 	l_rtnl_set_linkmode_and_operstate;
 	l_rtnl_set_mac;
 	l_rtnl_set_powered;
+	l_rtnl_link_set_mtu;
 	l_rtnl_ifaddr4_extract;
 	l_rtnl_ifaddr4_dump;
 	l_rtnl_ifaddr4_add;
ell/rtnl.c +18 -0
index da5d219feb48..fbad795949be 100644
@@ -787,6 +787,24 @@ LIB_EXPORT uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex,
 	return l_netlink_send(rtnl, nlm, cb, user_data, destroy);
 }
 
+LIB_EXPORT uint32_t l_rtnl_link_set_mtu(struct l_netlink *rtnl, int ifindex,
+				uint32_t mtu,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy)
+{
+	struct l_netlink_message *nlm = l_netlink_message_new(RTM_SETLINK, 0);
+	struct ifinfomsg ifi;
+
+	memset(&ifi, 0, sizeof(ifi));
+	ifi.ifi_family = AF_UNSPEC;
+	ifi.ifi_index = ifindex;
+
+	l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
+	l_netlink_message_append_u32(nlm, IFLA_MTU, mtu);
+
+	return l_netlink_send(rtnl, nlm, cb, user_data, destroy);
+}
+
 LIB_EXPORT void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
 				char **label, char **ip, char **broadcast)
 {
ell/rtnl.h +4 -0
index fa5d99eb4b53..d44cda24d6fd 100644
@@ -103,6 +103,10 @@ uint32_t l_rtnl_set_powered(struct l_netlink *rtnl, int ifindex, bool powered,
 				l_netlink_command_func_t cb, void *user_data,
 				l_netlink_destroy_func_t destroy);
 
+uint32_t l_rtnl_link_set_mtu(struct l_netlink *rtnl, int ifindex, uint32_t mtu,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy);
+
 void l_rtnl_ifaddr4_extract(const struct ifaddrmsg *ifa, int bytes,
 				char **label, char **ip, char **broadcast);
 uint32_t l_rtnl_ifaddr4_dump(struct l_netlink *rtnl, l_netlink_command_func_t cb,
Signature · 1 line
2.45.2
#6 lore raw
In reply to Denis Kenzior
Quoted 8 lines · Denis Kenzior

Move netlink attribute iterator out of genl.c and into netlink.c. Both genl and netlink use the same attribute structure, and is applicable to both. This makes implementing netlink and rtnl message parsers much easier.

In order to keep source compatibility, structure the netlink iterator in the same way as the genl iterator. In the future, the use of genl iterators will be removed.

Quoted 8 lines
---
  ell/ell.sym   |  5 ++--
  ell/genl.c    | 76 +++------------------------------------------------
  ell/genl.h    | 18 +++++++++---
  ell/netlink.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/netlink.h | 15 ++++++++++
  5 files changed, 111 insertions(+), 78 deletions(-)

All applied