Skip to contentSkip to conversation

netlink: Remove pointless checks

PATCH In review Denis Kenzior 2 participants 9 messages Jul 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 netlink: Remove pointless checks

command_queue, command_pending and command_lookup structures are always initialized at construction time and are not destroyed until l_netlink destruction. There's no need to check whether they ar valid.

---
 ell/netlink.c | 8 --------
 1 file changed, 8 deletions(-)
ell/netlink.c +0 -8
index 024740b62fc3..26c31679e4c7 100644
@@ -410,10 +410,6 @@ LIB_EXPORT unsigned int l_netlink_send(struct l_netlink *netlink,
 	if (unlikely(!netlink))
 		return 0;
 
-	if (!netlink->command_queue || !netlink->command_pending ||
-						!netlink->command_lookup)
-		return 0;
-
 	if (flags & 0xff)
 		return 0;
 
@@ -471,10 +467,6 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
 	if (unlikely(!netlink || !id))
 		return false;
 
-	if (!netlink->command_queue || !netlink->command_pending ||
-						!netlink->command_lookup)
-		return false;
-
 	command = l_hashmap_remove(netlink->command_lookup, L_UINT_TO_PTR(id));
 	if (!command)
 		return false;
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/7 netlink: Disallow certain flags in message constructor

Certain flags are reserved for use by l_netlink when sending messages. Other flags don't make sense for messages that are to be sent to the kernel. Disallow their use.

---
 ell/netlink.c        | 3 +++
 unit/test-genl-msg.c | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
ell/netlink.c +3 -0
index 26c31679e4c7..58dfd9779875 100644
@@ -751,6 +751,9 @@ LIB_EXPORT struct l_netlink_message *l_netlink_message_new_sized(uint16_t type,
 {
 	struct l_netlink_message *message;
 
+	if (flags & 0xff)
+		return NULL;
+
 	message = l_new(struct l_netlink_message, 1);
 
 	message->size = initial_len + NLMSG_HDRLEN;
unit/test-genl-msg.c +4 -2
index 3d996f4e242a..d10a87f30e3b 100644
@@ -134,7 +134,7 @@ static void build_set_station_netlink(const void *data)
 	struct l_netlink_message *m;
 	struct genlmsghdr genlhdr = { .cmd = 18, .version = 0, .reserved = 0 };
 
-	m = l_netlink_message_new(0x17, NLM_F_REQUEST | NLM_F_ACK);
+	m = l_netlink_message_new(0x17, 0);
 	assert(m);
 
 	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
@@ -153,6 +153,7 @@ static void build_set_station_netlink(const void *data)
 				sizeof(set_station_request), do_debug, "[MSG] ");
 	}
 
+	m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
 	assert(m->hdr->nlmsg_len == sizeof(set_station_request));
 	assert(!memcmp(m->data, set_station_request,
 					sizeof(set_station_request)));
@@ -292,7 +293,7 @@ static void build_set_rekey_offload_netlink(const void *data)
 	struct l_netlink_message *m;
 	struct genlmsghdr genlhdr = { .cmd = 79, .version = 0, .reserved = 0 };
 
-	m = l_netlink_message_new(0x1b, NLM_F_REQUEST | NLM_F_ACK);
+	m = l_netlink_message_new(0x1b, 0);
 	assert(m);
 
 	assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr)));
@@ -315,6 +316,7 @@ static void build_set_rekey_offload_netlink(const void *data)
 				do_debug, "[MSG] ");
 	}
 
+	m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
 	assert(m->hdr->nlmsg_len == sizeof(set_rekey_offload_request));
 	assert(!memcmp(m->data, set_rekey_offload_request,
 					sizeof(set_rekey_offload_request)));
Signature · 1 line
2.45.2
#2 lore raw

PATCH 3/7 rtnl: Add a builder for RTM_{NEW|DEL}ROUTE messages

In reply to Denis Kenzior

This builder is based on the new l_netlink_message APIs and is much simpler to understand. Use 'rtm' for struct rtmsg since that is the common convention.

---
 ell/rtnl-private.h |  4 +++
 ell/rtnl.c         | 76 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
ell/rtnl-private.h +4 -0
index 8af2ca6d52aa..ade43b3f053a 100644
@@ -28,3 +28,7 @@ struct l_rtnl_route {
 	uint32_t priority;
 	uint8_t preference;
 };
+
+struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags,
+						int ifindex,
+						const struct l_rtnl_route *rt);
ell/rtnl.c +76 -0

91 lines not on this page · raw message

Signature · 1 line
2.45.2
#3 lore raw

PATCH 4/7 rtnl: Add a builder for RTM_{NEW|DEL}ADDR messages

In reply to Denis Kenzior

This builder is based on the new l_netlink_message APIs and is much simpler to understand. Use 'ifa' for struct ifaddrmsg since that is the common convention.

---
 ell/rtnl-private.h | 20 +++++++++++++
 ell/rtnl.c         | 73 +++++++++++++++++++++++++++++++++++-----------
 2 files changed, 76 insertions(+), 17 deletions(-)
ell/rtnl-private.h +20 -0
index ade43b3f053a..fe9cbf35b232 100644
@@ -5,6 +5,23 @@
  * SPDX-License-Identifier: LGPL-2.1-or-later
  */
 
+struct l_rtnl_address {
+	uint8_t family;
+	uint8_t prefix_len;
+	uint8_t scope;
+	union {
+		struct in6_addr in6_addr;
+		struct in_addr in_addr;
+	};
+	struct in_addr broadcast;
+	char label[IFNAMSIZ];
+	uint32_t preferred_lifetime;
+	uint32_t valid_lifetime;
+	uint64_t preferred_expiry_time;
+	uint64_t valid_expiry_time;
+	uint32_t flags;
+};
+
 struct l_rtnl_route {
 	uint8_t family;
 	uint8_t scope;
@@ -32,3 +49,6 @@ struct l_rtnl_route {
 struct l_netlink_message *rtnl_message_from_route(uint16_t type, uint16_t flags,
 						int ifindex,
 						const struct l_rtnl_route *rt);
+struct l_netlink_message *rtnl_message_from_address(uint16_t type,
+					uint16_t flags, int ifindex,
+					const struct l_rtnl_address *addr);
ell/rtnl.c +56 -17

82 lines not on this page · raw message

Signature · 1 line
2.45.2
#4 lore raw

PATCH 5/7 unit: Add tests for rtnl_message_from_{route|address}

In reply to Denis Kenzior
---
 unit/test-rtnl.c | 278 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 277 insertions(+), 1 deletion(-)
unit/test-rtnl.c +277 -1
index cd944af64de9..25e1859205b4 100644
@@ -17,12 +17,282 @@
 #include <signal.h>
 #include <sys/wait.h>
 #include <asm/types.h>
+#include <arpa/inet.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
+#include <linux/neighbour.h>
+#include <linux/icmpv6.h>
+#include <linux/if.h>
 #include <sys/socket.h>
+#include <assert.h>
+#include <limits.h>
 
 #include <ell/ell.h>
-#include "ell/dbus-private.h"
+#include "ell/netlink-private.h"
+#include "ell/rtnl-private.h"
+#include "ell/useful.h"
+
+static size_t rta_add_u8(void *rta_buf, unsigned short type, uint8_t value)
+{
+	struct rtattr *rta = rta_buf;
+
+	rta->rta_len = RTA_LENGTH(sizeof(uint8_t));
+	rta->rta_type = type;
+	*((uint8_t *) RTA_DATA(rta)) = value;
+
+	return RTA_SPACE(sizeof(uint8_t));
+}
+
+static size_t rta_add_u32(void *rta_buf, unsigned short type, uint32_t value)
+{
+	struct rtattr *rta = rta_buf;
+
+	rta->rta_len = RTA_LENGTH(sizeof(uint32_t));
+	rta->rta_type = type;
+	*((uint32_t *) RTA_DATA(rta)) = value;
+
+	return RTA_SPACE(sizeof(uint32_t));
+}
+
+static size_t rta_add_data(void *rta_buf, unsigned short type, const void *data,
+								size_t data_len)
+{
+	struct rtattr *rta = rta_buf;
+
+	rta->rta_len = RTA_LENGTH(data_len);
+	rta->rta_type = type;
+	memcpy(RTA_DATA(rta), data, data_len);
+
+	return RTA_SPACE(data_len);
+}
+
+static size_t rta_add_address(void *rta_buf, unsigned short type,
+				uint8_t family,
+				const struct in6_addr *v6,
+				const struct in_addr *v4)
+{
+	struct rtattr *rta = rta_buf;
+
+	rta->rta_type = type;
+
+	switch (family) {
+	case AF_INET6:
+		rta->rta_len = RTA_LENGTH(sizeof(struct in6_addr));
+		memcpy(RTA_DATA(rta), v6, sizeof(struct in6_addr));
+		return RTA_SPACE(sizeof(struct in6_addr));
+	case AF_INET:
+		rta->rta_len = RTA_LENGTH(sizeof(struct in_addr));
+		memcpy(RTA_DATA(rta), v4, sizeof(struct in_addr));
+		return RTA_SPACE(sizeof(struct in_addr));
+	}
+
+	return 0;
+}
+
+static int address_is_null(int family, const struct in_addr *v4,
+						const struct in6_addr *v6)
+{
+	switch (family) {
+	case AF_INET:
+		return v4->s_addr == 0;
+	case AF_INET6:
+		return IN6_IS_ADDR_UNSPECIFIED(v6);
+	}
+
+	return -EAFNOSUPPORT;
+}
+
+static bool messages_equal(const struct l_netlink_message *nlm,
+				void *other_message, size_t len)
+{
+
+	if (len != nlm->hdr->nlmsg_len - NLMSG_HDRLEN)
+		return false;
+
+	return !memcmp(nlm->data + NLMSG_HDRLEN, other_message, len);
+}
+
+static const struct l_rtnl_route route = {
+	.family = AF_INET6,
+	.scope = RT_SCOPE_UNIVERSE,
+	.protocol = RTPROT_UNSPEC,
+	.gw = { .in6_addr = IN6ADDR_LOOPBACK_INIT },
+	.dst = { .in6_addr = IN6ADDR_LOOPBACK_INIT },
+	.dst_prefix_len = 32,
+	.prefsrc = { .in6_addr = IN6ADDR_LOOPBACK_INIT },
+	.lifetime = INT_MAX,
+	.expiry_time = 0, /* math performed, will cause messages to differ */
+	.mtu = 8096,
+	.priority = 300,
+	.preference = ICMPV6_ROUTER_PREF_LOW,
+};
+
+static struct rtmsg *build_rtmsg(const struct l_rtnl_route *rt, int ifindex,
+					size_t *out_len)
+{
+	struct rtmsg *rtmmsg;
+	size_t bufsize;
+	void *rta_buf;
+	uint64_t now = l_time_now();
+
+	bufsize = NLMSG_ALIGN(sizeof(struct rtmsg)) +
+			RTA_SPACE(sizeof(uint32_t)) +        /* RTA_OIF */
+			RTA_SPACE(sizeof(uint32_t)) +        /* RTA_PRIORITY */
+			RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_GATEWAY */
+			RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_DST */
+			RTA_SPACE(sizeof(struct in6_addr)) + /* RTA_PREFSRC */
+			256 +                                /* RTA_METRICS */
+			RTA_SPACE(sizeof(uint8_t)) +         /* RTA_PREF */
+			RTA_SPACE(sizeof(uint32_t));         /* RTA_EXPIRES */
+
+	rtmmsg = l_malloc(bufsize);
+	memset(rtmmsg, 0, bufsize);
+
+	rtmmsg->rtm_family = rt->family;
+	rtmmsg->rtm_table = RT_TABLE_MAIN;
+	rtmmsg->rtm_protocol = rt->protocol;
+	rtmmsg->rtm_type = RTN_UNICAST;
+	rtmmsg->rtm_scope = rt->scope;
+
+	rta_buf = (void *) rtmmsg + NLMSG_ALIGN(sizeof(struct rtmsg));
+	rta_buf += rta_add_u32(rta_buf, RTA_OIF, ifindex);
+
+	if (rt->priority)
+		rta_buf += rta_add_u32(rta_buf, RTA_PRIORITY,
+						rt->priority + ifindex);
+
+	if (!address_is_null(rt->family, &rt->gw.in_addr, &rt->gw.in6_addr))
+		rta_buf += rta_add_address(rta_buf, RTA_GATEWAY, rt->family,
+					&rt->gw.in6_addr, &rt->gw.in_addr);
+
+	if (rt->dst_prefix_len) {
+		rtmmsg->rtm_dst_len = rt->dst_prefix_len;
+		rta_buf += rta_add_address(rta_buf, RTA_DST, rt->family,
+					&rt->dst.in6_addr, &rt->dst.in_addr);
+	}
+
+	if (!address_is_null(rt->family, &rt->prefsrc.in_addr,
+						&rt->prefsrc.in6_addr))
+		rta_buf += rta_add_address(rta_buf, RTA_PREFSRC, rt->family,
+						&rt->prefsrc.in6_addr,
+						&rt->prefsrc.in_addr);
+
+	if (rt->mtu) {
+		uint8_t buf[256];
+		size_t written = rta_add_u32(buf, RTAX_MTU, rt->mtu);
+
+		rta_buf += rta_add_data(rta_buf, RTA_METRICS | NLA_F_NESTED,
+							buf, written);
+	}
+
+	if (rt->preference)
+		rta_buf += rta_add_u8(rta_buf, RTA_PREF, rt->preference);
+
+	if (rt->expiry_time > now)
+		rta_buf += rta_add_u32(rta_buf, RTA_EXPIRES,
+					l_time_to_secs(rt->expiry_time - now));
+
+	*out_len = rta_buf - (void *) rtmmsg;
+	return rtmmsg;
+}
+
+static void test_route(const void *data)
+{
+	static const int ifindex = 3;
+	size_t rtmsg_len;
+	_auto_(l_free) void *rtmsg = build_rtmsg(&route, ifindex, &rtmsg_len);
+	struct l_netlink_message *nlm =
+		rtnl_message_from_route(RTM_NEWROUTE,
+					NLM_F_CREATE | NLM_F_REPLACE,
+					ifindex, &route);
+
+	assert(messages_equal(nlm, rtmsg, rtmsg_len));
+	l_netlink_message_unref(nlm);
+}
+
+static const struct l_rtnl_address address = {
+	.family = AF_INET6,
+	.prefix_len = 31,
+	.scope = RT_SCOPE_UNIVERSE,
+	.in6_addr = IN6ADDR_LOOPBACK_INIT,
+	.label = { 'f', 'o', 'o', 'b', 'a', 'r', '\0' },
+	.flags = IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE,
+};
+
+static struct ifaddrmsg *build_ifaddrmsg(const struct l_rtnl_address *addr,
+						int ifindex, size_t *out_len)
+{
+	struct ifaddrmsg *ifamsg;
+	void *buf;
+	size_t bufsize;
+	uint64_t now = l_time_now();
+
+	bufsize = NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
+					RTA_SPACE(sizeof(struct in6_addr)) +
+					RTA_SPACE(sizeof(struct in_addr)) +
+					RTA_SPACE(sizeof(uint32_t)) +
+					RTA_SPACE(IFNAMSIZ) +
+					RTA_SPACE(sizeof(struct ifa_cacheinfo));
+
+	ifamsg = l_malloc(bufsize);
+	memset(ifamsg, 0, bufsize);
+
+	ifamsg->ifa_index = ifindex;
+	ifamsg->ifa_family = addr->family;
+	ifamsg->ifa_scope = addr->scope;
+	ifamsg->ifa_prefixlen = addr->prefix_len;
+	/* Kernel ignores legacy flags in IFA_FLAGS, so set them here */
+	ifamsg->ifa_flags = addr->flags & 0xff;
+
+	buf = (void *) ifamsg + NLMSG_ALIGN(sizeof(struct ifaddrmsg));
+
+	if (addr->family == AF_INET) {
+		buf += rta_add_data(buf, IFA_LOCAL, &addr->in_addr,
+						sizeof(struct in_addr));
+		buf += rta_add_data(buf, IFA_BROADCAST, &addr->broadcast,
+						sizeof(struct in_addr));
+	} else
+		buf += rta_add_data(buf, IFA_LOCAL, &addr->in6_addr,
+						sizeof(struct in6_addr));
+
+	if (addr->flags & 0xffffff00)
+		buf += rta_add_u32(buf, IFA_FLAGS, addr->flags & 0xffffff00);
+
+	if (addr->label[0])
+		buf += rta_add_data(buf, IFA_LABEL,
+					addr->label, strlen(addr->label) + 1);
+
+	if (addr->preferred_expiry_time > now ||
+			addr->valid_expiry_time > now) {
+		struct ifa_cacheinfo cinfo;
+
+		memset(&cinfo, 0, sizeof(cinfo));
+		cinfo.ifa_prefered = addr->preferred_expiry_time > now ?
+			l_time_to_secs(addr->preferred_expiry_time - now) : 0;
+		cinfo.ifa_valid =  addr->valid_expiry_time > now ?
+			l_time_to_secs(addr->valid_expiry_time - now) : 0;
+
+		buf += rta_add_data(buf, IFA_CACHEINFO, &cinfo, sizeof(cinfo));
+	}
+
+	*out_len = buf - (void *) ifamsg;
+	return ifamsg;
+}
+
+static void test_address(const void *data)
+{
+	static const int ifindex = 3;
+	size_t ifamsg_len;
+	_auto_(l_free) void *ifamsg =
+				build_ifaddrmsg(&address, ifindex, &ifamsg_len);
+	struct l_netlink_message *nlm =
+		rtnl_message_from_address(RTM_NEWADDR,
+					NLM_F_CREATE | NLM_F_REPLACE,
+					ifindex, &address);
+
+	assert(messages_equal(nlm, ifamsg, ifamsg_len));
+	l_netlink_message_unref(nlm);
+}
 
 static void signal_handler(uint32_t signo, void *user_data)
 {
@@ -234,6 +504,12 @@ int main(int argc, char *argv[])
 	if (!l_main_init())
 		return -1;
 
+	/* Run the the tests not requiring the main event loop first */
+	l_test_init(&argc, &argv);
+	l_test_add("route", test_route, NULL);
+	l_test_add("address", test_address, NULL);
+	l_test_run();
+
 	test_add("Dump IPv4 routing table", test_route4_dump, NULL);
 	test_add("Dump IPv6 routing table", test_route6_dump, NULL);
 	test_add("Dump IPv4 addresses", test_ifaddr4_dump, NULL);
Signature · 1 line
2.45.2
#5 lore raw

PATCH 6/7 netlink: Add sealed flag to l_netlink_message

In reply to Denis Kenzior

If the sealed flag is set, the message can no longer be modified. This is useful when netlink message ownership is taken over by l_netlink_send to prevent the message from being modified once it has been queued.

---
 ell/netlink-private.h | 1 +
 ell/netlink.c         | 3 +++
 2 files changed, 4 insertions(+)
ell/netlink-private.h +1 -0
index b114f62f6bf3..f7119f6218e9 100644
@@ -40,6 +40,7 @@ struct l_netlink_message {
 	};
 	uint32_t nest_offset[4];
 	uint8_t nest_level;
+	bool sealed : 1;
 };
 
 bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg,
ell/netlink.c +3 -0
index 58dfd9779875..b6d5fb1fd36d 100644
@@ -685,6 +685,9 @@ static int message_grow(struct l_netlink_message *message, uint32_t needed)
 {
 	uint32_t grow_to;
 
+	if (message->sealed)
+		return -EPERM;
+
 	if (message->size - message->hdr->nlmsg_len >= needed)
 		return 0;
 
Signature · 1 line
2.45.2
#6 lore raw

PATCH 7/7 netlink/rtnl: Change l_netlink_send API

In reply to Denis Kenzior

Change l_netlink_send API to operate on newly introduced l_netlink_message objects instead of arbitrary void * buffers. This cuts down the number of arguments to l_netlink_send considerably, making it much easier to use.

The change in public API does affect the test-netlink unit test. Fix it up to use the new API as well.

---
 ell/netlink.c       |  80 ++++----
 ell/netlink.h       |   8 +-
 ell/rtnl.c          | 453 +++++++++++---------------------------------
 unit/test-netlink.c |  11 +-
 4 files changed, 151 insertions(+), 401 deletions(-)
ell/netlink.c +33 -47
index b6d5fb1fd36d..121a322fccf5 100644
@@ -24,11 +24,10 @@
 
 struct command {
 	unsigned int id;
-	uint32_t seq;
-	uint32_t len;
 	l_netlink_command_func_t handler;
 	l_netlink_destroy_func_t destroy;
 	void *user_data;
+	struct l_netlink_message *message;
 };
 
 struct notify {
@@ -61,6 +60,7 @@ static void destroy_command(void *data)
 	if (command->destroy)
 		command->destroy(command->user_data);
 
+	l_netlink_message_unref(command->message);
 	l_free(command);
 }
 
@@ -85,8 +85,8 @@ static bool can_write_data(struct l_io *io, void *user_data)
 {
 	struct l_netlink *netlink = user_data;
 	struct command *command;
+	struct nlmsghdr *hdr;
 	struct sockaddr_nl addr;
-	const void *data;
 	ssize_t written;
 	int sk;
 
@@ -94,28 +94,27 @@ static bool can_write_data(struct l_io *io, void *user_data)
 	if (!command)
 		return false;
 
+	hdr = command->message->hdr;
 	sk = l_io_get_fd(io);
 
 	memset(&addr, 0, sizeof(addr));
 	addr.nl_family = AF_NETLINK;
 	addr.nl_pid = 0;
 
-	data = ((void *) command) + NLMSG_ALIGN(sizeof(struct command));
-
-	written = sendto(sk, data, command->len, 0,
-				(struct sockaddr *) &addr, sizeof(addr));
-	if (written < 0 || (uint32_t) written != command->len) {
+	written = sendto(sk, hdr, hdr->nlmsg_len, 0,
+			(struct sockaddr *) &addr, sizeof(addr));
+	if (written < 0 || (uint32_t) written != hdr->nlmsg_len) {
 		l_hashmap_remove(netlink->command_lookup,
 					L_UINT_TO_PTR(command->id));
 		destroy_command(command);
 		return true;
 	}
 
-	l_util_hexdump(false, data, command->len,
+	l_util_hexdump(false, hdr, hdr->nlmsg_len,
 				netlink->debug_handler, netlink->debug_data);
 
 	l_hashmap_insert(netlink->command_pending,
-				L_UINT_TO_PTR(command->seq), command);
+				L_UINT_TO_PTR(hdr->nlmsg_seq), command);
 
 	return l_queue_length(netlink->command_queue) > 0;
 }
@@ -399,70 +398,55 @@ LIB_EXPORT void l_netlink_destroy(struct l_netlink *netlink)
 }
 
 LIB_EXPORT unsigned int l_netlink_send(struct l_netlink *netlink,
-			uint16_t type, uint16_t flags, const void *data,
-			uint32_t len, l_netlink_command_func_t function,
-			void *user_data, l_netlink_destroy_func_t destroy)
+					struct l_netlink_message *message,
+					l_netlink_command_func_t function,
+					void *user_data,
+					l_netlink_destroy_func_t destroy)
 {
 	struct command *command;
+	uint16_t extra_flags = NLM_F_REQUEST;
 	struct nlmsghdr *nlmsg;
-	size_t size;
 
 	if (unlikely(!netlink))
 		return 0;
 
-	if (flags & 0xff)
+	if (unlikely(message->nest_level))
 		return 0;
 
 	if (function)
-		flags |= NLM_F_ACK;
+		extra_flags |= NLM_F_ACK;
 
-	size = NLMSG_ALIGN(sizeof(struct command)) +
-					NLMSG_HDRLEN + NLMSG_ALIGN(len);
+	command = l_new(struct command, 1);
 
-	command = l_malloc(size);
+	if (!l_hashmap_insert(netlink->command_lookup,
+				L_UINT_TO_PTR(netlink->next_command_id),
+				command)) {
+		l_free(command);
+		return 0;
+	}
 
-	memset(command, 0, size);
 	command->handler = function;
 	command->destroy = destroy;
 	command->user_data = user_data;
+	command->id = netlink->next_command_id++;
+	command->message = message;
+	message->sealed = true;
 
-	command->id = netlink->next_command_id;
-
-	if (!l_hashmap_insert(netlink->command_lookup,
-					L_UINT_TO_PTR(command->id), command))
-		goto free_command;
-
-	command->seq = netlink->next_seq++;
-	command->len = NLMSG_HDRLEN + NLMSG_ALIGN(len);
-
-	nlmsg = ((void *) command) + NLMSG_ALIGN(sizeof(struct command));
-
-	nlmsg->nlmsg_len = command->len;
-	nlmsg->nlmsg_type = type;
-	nlmsg->nlmsg_flags = NLM_F_REQUEST | flags;
-	nlmsg->nlmsg_seq = command->seq;
+	nlmsg = message->hdr;
+	nlmsg->nlmsg_flags |= extra_flags;
+	nlmsg->nlmsg_seq = netlink->next_seq++;
 	nlmsg->nlmsg_pid = netlink->pid;
 
-	if (data && len > 0)
-		memcpy(((void *) nlmsg) + NLMSG_HDRLEN, data, len);
-
 	l_queue_push_tail(netlink->command_queue, command);
-
 	l_io_set_write_handler(netlink->io, can_write_data, netlink, NULL);
 
-	netlink->next_command_id++;
-
 	return command->id;
-
-free_command:
-	l_free(command);
-
-	return 0;
 }
 
 LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
 {
 	struct command *command;
+	struct nlmsghdr *hdr;
 
 	if (unlikely(!netlink || !id))
 		return false;
@@ -471,9 +455,11 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
 	if (!command)
 		return false;
 
+	hdr = command->message->hdr;
+
 	if (!l_queue_remove(netlink->command_queue, command)) {
 		l_hashmap_remove(netlink->command_pending,
-					L_UINT_TO_PTR(command->seq));
+					L_UINT_TO_PTR(hdr->nlmsg_seq));
 	}
 
 	destroy_command(command);
ell/netlink.h +5 -3
index e7962b9932dc..08aff74fbac2 100644
@@ -25,14 +25,16 @@ typedef void (*l_netlink_notify_func_t) (uint16_t type, const void *data,
 typedef void (*l_netlink_destroy_func_t) (void *user_data);
 
 struct l_netlink;
+struct l_netlink_message;
 
 struct l_netlink *l_netlink_new(int protocol);
 void l_netlink_destroy(struct l_netlink *netlink);
 
 unsigned int l_netlink_send(struct l_netlink *netlink,
-			uint16_t type, uint16_t flags, const void *data,
-			uint32_t len, l_netlink_command_func_t function,
-			void *user_data, l_netlink_destroy_func_t destroy);
+				struct l_netlink_message *message,
+				l_netlink_command_func_t function,
+				void *user_data,
+				l_netlink_destroy_func_t destroy);
 bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id);
 
 unsigned int l_netlink_register(struct l_netlink *netlink,
ell/rtnl.c +106 -347

571 lines not on this page · raw message

unit/test-netlink.c +7 -4
index 1bcf1e4b274c..da23ee5c9b0e 100644
@@ -66,7 +66,10 @@ static void link_notification(uint16_t type, void const * data,
 int main(int argc, char *argv[])
 {
 	struct l_netlink *netlink;
-	struct ifinfomsg msg;
+	struct ifinfomsg ifi;
+	struct l_netlink_message *nlm =
+			l_netlink_message_new_sized(RTM_GETLINK,
+							NLM_F_DUMP, sizeof(ifi));
 	unsigned int link_id;
 
 	if (!l_main_init())
@@ -78,10 +81,10 @@ int main(int argc, char *argv[])
 
 	l_netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL);
 
-	memset(&msg, 0, sizeof(msg));
+	memset(&ifi, 0, sizeof(ifi));
+	l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
 
-	l_netlink_send(netlink, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg),
-						getlink_callback, NULL, NULL);
+	l_netlink_send(netlink, nlm, getlink_callback, NULL, NULL);
 
 	link_id = l_netlink_register(netlink, RTNLGRP_LINK,
 					link_notification, NULL, NULL);
Signature · 1 line
2.45.2
#7 lore raw
In reply to Denis Kenzior

Hi Denis,

Quoted 23 lines · Denis Kenzior

command_queue, command_pending and command_lookup structures are always initialized at construction time and are not destroyed until l_netlink destruction. There's no need to check whether they ar valid.

---
  ell/netlink.c | 8 --------
  1 file changed, 8 deletions(-)
ell/netlink.c +0 -4
index 024740b62fc3..26c31679e4c7 100644
@@ -410,10 +410,6 @@ LIB_EXPORT unsigned int l_netlink_send(struct l_netlink *netlink,
  	if (unlikely(!netlink))
  		return 0;
 
-	if (!netlink->command_queue || !netlink->command_pending ||
-						!netlink->command_lookup)
-		return 0;
-
  	if (flags & 0xff)
  		return 0;
 

ell/netlink.c LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id) · line 472

Quoted 10 lines
ell/netlink.c +0 -4
@@ -471,10 +467,6 @@ LIB_EXPORT bool l_netlink_cancel(struct l_netlink *netlink, unsigned int id)
  	if (unlikely(!netlink || !id))
  		return false;
 
-	if (!netlink->command_queue || !netlink->command_pending ||
-						!netlink->command_lookup)
-		return false;
-
  	command = l_hashmap_remove(netlink->command_lookup, L_UINT_TO_PTR(id));
  	if (!command)
  		return false;

These all look good to me visually, assuming they pass autotests (once the update to IWD/netdev is done)

Thanks,

James

#8 lore raw

Hi James,

Quoted 3 lines

These all look good to me visually, assuming they pass autotests (once the update to IWD/netdev is done)

autotests seem to pass locally (with some usual test-runner flakiness), so I'll push this out and send the iwd patchset in a bit. I do need to merge the iwd monitor patch first though. Have you looked at that one?

Regards,
-Denis