public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 09/17] netlink: Fill in netlink header fields from nl_req()
Date: Thu,  3 Aug 2023 17:19:48 +1000	[thread overview]
Message-ID: <20230803071956.3198452-10-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230803071956.3198452-1-david@gibson.dropbear.id.au>

Currently netlink functions need to fill in a full netlink header, as well
as a payload then call nl_req() to submit that to the kernel.  It makes
things a bit terser if we just give the relevant header fields as
parameters to nl_req() and have it complete the header.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 netlink.c | 126 ++++++++++++++++++------------------------------------
 1 file changed, 42 insertions(+), 84 deletions(-)

diff --git a/netlink.c b/netlink.c
index 3aeaeb5..5f008ce 100644
--- a/netlink.c
+++ b/netlink.c
@@ -97,25 +97,29 @@ fail:
 }
 
 /**
- * nl_req() - Send netlink request and read response
+ * nl_req() - Prepare and send netlink request, read response
  * @s:		Netlink socket
  * @buf:	Buffer for response (at least NLBUFSIZ long)
- * @req:	Request with netlink header
+ * @req:	Request (will fill netlink header)
+ * @type:	Request type
+ * @flags:	Extra request flags (NLM_F_REQUEST and NLM_F_ACK assumed)
  * @len:	Request length
  *
  * Return: received length on success, terminates on error
  */
-static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
+static ssize_t nl_req(int s, char *buf, void *req,
+		      uint16_t type, uint16_t flags, ssize_t len)
 {
 	char flush[NLBUFSIZ];
+	struct nlmsghdr *nh;
 	int done = 0;
 	ssize_t n;
 
 	while (!done && (n = recv(s, flush, sizeof(flush), MSG_DONTWAIT)) > 0) {
-		struct nlmsghdr *nh = (struct nlmsghdr *)flush;
 		size_t nm = n;
 
-		for ( ; NLMSG_OK(nh, nm); nh = NLMSG_NEXT(nh, nm)) {
+		for (nh = (struct nlmsghdr *)flush;
+		     NLMSG_OK(nh, nm); nh = NLMSG_NEXT(nh, nm)) {
 			if (nh->nlmsg_type == NLMSG_DONE ||
 			    nh->nlmsg_type == NLMSG_ERROR) {
 				done = 1;
@@ -124,6 +128,13 @@ static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
 		}
 	}
 
+	nh = (struct nlmsghdr *)req;
+	nh->nlmsg_type = type;
+	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
+	nh->nlmsg_len = len;
+	nh->nlmsg_seq = nl_seq++;
+	nh->nlmsg_pid = 0;
+
 	n = send(s, req, len, 0);
 	if (n < 0)
 		die("netlink: Failed to send(): %s", strerror(errno));
@@ -148,11 +159,6 @@ static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
 unsigned int nl_get_ext_if(int s, sa_family_t af)
 {
 	struct { struct nlmsghdr nlh; struct rtmsg rtm; } req = {
-		.nlh.nlmsg_type	 = RTM_GETROUTE,
-		.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_len	 = NLMSG_LENGTH(sizeof(struct rtmsg)),
-		.nlh.nlmsg_seq	 = nl_seq++,
-
 		.rtm.rtm_table	 = RT_TABLE_MAIN,
 		.rtm.rtm_scope	 = RT_SCOPE_UNIVERSE,
 		.rtm.rtm_type	 = RTN_UNICAST,
@@ -164,7 +170,7 @@ unsigned int nl_get_ext_if(int s, sa_family_t af)
 	ssize_t n;
 	size_t na;
 
-	n = nl_req(s, buf, &req, sizeof(req));
+	n = nl_req(s, buf, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	nh = (struct nlmsghdr *)buf;
 
@@ -205,11 +211,6 @@ void nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		struct rtattr rta;
 		unsigned int ifi;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETROUTE,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -223,7 +224,7 @@ void nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	n = nl_req(s, buf, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -278,11 +279,6 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 			} r4;
 		} set;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_NEWROUTE,
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK |
-				    NLM_F_CREATE | NLM_F_EXCL,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -294,12 +290,12 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		.ifi		  = ifi,
 	};
 	char buf[NLBUFSIZ];
+	ssize_t len;
 
 	if (af == AF_INET6) {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.r6.d));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.r6)
-			+ sizeof(req.set.r6);
+		len = offsetof(struct req_t, set.r6) + sizeof(req.set.r6);
 
 		req.set.r6.rta_dst.rta_type = RTA_DST;
 		req.set.r6.rta_dst.rta_len = rta_len;
@@ -310,8 +306,7 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 	} else {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.r4.d));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.r4)
-			+ sizeof(req.set.r4);
+		len = offsetof(struct req_t, set.r4) + sizeof(req.set.r4);
 
 		req.set.r4.rta_dst.rta_type = RTA_DST;
 		req.set.r4.rta_dst.rta_len = rta_len;
@@ -321,7 +316,7 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		req.set.r4.rta_gw.rta_len = rta_len;
 	}
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
 }
 
 /**
@@ -341,11 +336,6 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		struct rtattr rta;
 		unsigned int ifi;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETROUTE,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -361,7 +351,8 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 	char buf[NLBUFSIZ];
 	unsigned i;
 
-	nlmsgs_size = nl_req(s_src, buf, &req, req.nlh.nlmsg_len);
+	nlmsgs_size = nl_req(s_src, buf, &req,
+			     RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf, n = nlmsgs_size;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -373,10 +364,6 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		if (nh->nlmsg_type != RTM_NEWROUTE)
 			continue;
 
-		nh->nlmsg_pid = 0;
-		nh->nlmsg_flags &= ~NLM_F_DUMP_FILTERED;
-		nh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK |
-			NLM_F_CREATE;
 		dup_routes++;
 
 		for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
@@ -397,13 +384,15 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		for (nh = (struct nlmsghdr *)buf, n = nlmsgs_size;
 		     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
 		     nh = NLMSG_NEXT(nh, n)) {
+			uint16_t flags = nh->nlmsg_flags;
 			char resp[NLBUFSIZ];
 
 			if (nh->nlmsg_type != RTM_NEWROUTE)
 				continue;
 
-			nh->nlmsg_seq = nl_seq++;
-			nl_req(s_dst, resp, nh, nh->nlmsg_len);
+			nl_req(s_dst, resp, nh, RTM_NEWROUTE,
+			       (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
+			       nh->nlmsg_len);
 		}
 	}
 }
@@ -424,11 +413,6 @@ void nl_addr_get(int s, unsigned int ifi, sa_family_t af,
 		struct nlmsghdr nlh;
 		struct ifaddrmsg ifa;
 	} req = {
-		.nlh.nlmsg_type    = RTM_GETADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
-		.nlh.nlmsg_len     = sizeof(req),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi,
 	};
@@ -436,7 +420,7 @@ void nl_addr_get(int s, unsigned int ifi, sa_family_t af,
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	n = nl_req(s, buf, &req, RTM_GETADDR, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -500,18 +484,13 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 			} a6;
 		} set;
 	} req = {
-		.nlh.nlmsg_type    = RTM_NEWADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_ACK |
-				     NLM_F_CREATE | NLM_F_EXCL,
-		.nlh.nlmsg_len     = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi,
 		.ifa.ifa_prefixlen = prefix_len,
 		.ifa.ifa_scope	   = RT_SCOPE_UNIVERSE,
 	};
 	char buf[NLBUFSIZ];
+	ssize_t len;
 
 	if (af == AF_INET6) {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.a6.l));
@@ -519,8 +498,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 		/* By default, strictly speaking, it's duplicated */
 		req.ifa.ifa_flags = IFA_F_NODAD;
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.a6)
-			+ sizeof(req.set.a6);
+		len = offsetof(struct req_t, set.a6) + sizeof(req.set.a6);
 
 		memcpy(&req.set.a6.l, addr, sizeof(req.set.a6.l));
 		req.set.a6.rta_l.rta_len = rta_len;
@@ -531,8 +509,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 	} else {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.a4.l));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.a4)
-			+ sizeof(req.set.a4);
+		len = offsetof(struct req_t, set.a4) + sizeof(req.set.a4);
 
 		memcpy(&req.set.a4.l, addr, sizeof(req.set.a4.l));
 		req.set.a4.rta_l.rta_len = rta_len;
@@ -541,7 +518,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 		req.set.a4.rta_a.rta_type = IFA_ADDRESS;
 	}
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL, len);
 }
 
 /**
@@ -559,11 +536,6 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 		struct nlmsghdr nlh;
 		struct ifaddrmsg ifa;
 	} req = {
-		.nlh.nlmsg_type    = RTM_GETADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_len     = sizeof(req),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi_src,
 		.ifa.ifa_prefixlen = 0,
@@ -572,7 +544,7 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 	struct nlmsghdr *nh;
 	ssize_t n;
 
-	n = nl_req(s_src, buf, &req, sizeof(req));
+	n = nl_req(s_src, buf, &req, RTM_GETADDR, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -585,11 +557,6 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 		if (nh->nlmsg_type != RTM_NEWADDR)
 			continue;
 
-		nh->nlmsg_seq = nl_seq++;
-		nh->nlmsg_pid = 0;
-		nh->nlmsg_flags &= ~NLM_F_DUMP_FILTERED;
-		nh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
-
 		ifa = (struct ifaddrmsg *)NLMSG_DATA(nh);
 
 		if (ifa->ifa_scope == RT_SCOPE_LINK ||
@@ -604,7 +571,9 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 				rta->rta_type = IFA_UNSPEC;
 		}
 
-		nl_req(s_dst, resp, nh, nh->nlmsg_len);
+		nl_req(s_dst, resp, nh, RTM_NEWADDR,
+		       (nh->nlmsg_flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
+		       nh->nlmsg_len);
 	}
 }
 
@@ -620,10 +589,6 @@ void nl_link_get_mac(int s, unsigned int ifi, void *mac)
 		struct nlmsghdr nlh;
 		struct ifinfomsg ifm;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETLINK,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 	};
@@ -631,7 +596,7 @@ void nl_link_get_mac(int s, unsigned int ifi, void *mac)
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, sizeof(req));
+	n = nl_req(s, buf, &req, RTM_GETLINK, 0, sizeof(req));
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
 	     nh = NLMSG_NEXT(nh, n)) {
@@ -669,10 +634,6 @@ void nl_link_set_mac(int s, unsigned int ifi, void *mac)
 		struct rtattr rta;
 		unsigned char mac[ETH_ALEN];
 	} req = {
-		.nlh.nlmsg_type	  = RTM_NEWLINK,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 		.rta.rta_type	  = IFLA_ADDRESS,
@@ -682,7 +643,7 @@ void nl_link_set_mac(int s, unsigned int ifi, void *mac)
 
 	memcpy(req.mac, mac, ETH_ALEN);
 
-	nl_req(s, buf, &req, sizeof(req));
+	nl_req(s, buf, &req, RTM_NEWLINK, 0, sizeof(req));
 }
 
 /**
@@ -699,10 +660,6 @@ void nl_link_up(int s, unsigned int ifi, int mtu)
 		struct rtattr rta;
 		unsigned int mtu;
 	} req = {
-		.nlh.nlmsg_type   = RTM_NEWLINK,
-		.nlh.nlmsg_len    = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 		.ifm.ifi_flags	  = IFF_UP,
@@ -711,11 +668,12 @@ void nl_link_up(int s, unsigned int ifi, int mtu)
 		.rta.rta_len	  = RTA_LENGTH(sizeof(unsigned int)),
 		.mtu		  = mtu,
 	};
+	ssize_t len = sizeof(req);
 	char buf[NLBUFSIZ];
 
 	if (!mtu)
 		/* Shorten request to drop MTU attribute */
-		req.nlh.nlmsg_len = offsetof(struct req_t, rta);
+		len = offsetof(struct req_t, rta);
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWLINK, 0, len);
 }
-- 
@@ -97,25 +97,29 @@ fail:
 }
 
 /**
- * nl_req() - Send netlink request and read response
+ * nl_req() - Prepare and send netlink request, read response
  * @s:		Netlink socket
  * @buf:	Buffer for response (at least NLBUFSIZ long)
- * @req:	Request with netlink header
+ * @req:	Request (will fill netlink header)
+ * @type:	Request type
+ * @flags:	Extra request flags (NLM_F_REQUEST and NLM_F_ACK assumed)
  * @len:	Request length
  *
  * Return: received length on success, terminates on error
  */
-static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
+static ssize_t nl_req(int s, char *buf, void *req,
+		      uint16_t type, uint16_t flags, ssize_t len)
 {
 	char flush[NLBUFSIZ];
+	struct nlmsghdr *nh;
 	int done = 0;
 	ssize_t n;
 
 	while (!done && (n = recv(s, flush, sizeof(flush), MSG_DONTWAIT)) > 0) {
-		struct nlmsghdr *nh = (struct nlmsghdr *)flush;
 		size_t nm = n;
 
-		for ( ; NLMSG_OK(nh, nm); nh = NLMSG_NEXT(nh, nm)) {
+		for (nh = (struct nlmsghdr *)flush;
+		     NLMSG_OK(nh, nm); nh = NLMSG_NEXT(nh, nm)) {
 			if (nh->nlmsg_type == NLMSG_DONE ||
 			    nh->nlmsg_type == NLMSG_ERROR) {
 				done = 1;
@@ -124,6 +128,13 @@ static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
 		}
 	}
 
+	nh = (struct nlmsghdr *)req;
+	nh->nlmsg_type = type;
+	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
+	nh->nlmsg_len = len;
+	nh->nlmsg_seq = nl_seq++;
+	nh->nlmsg_pid = 0;
+
 	n = send(s, req, len, 0);
 	if (n < 0)
 		die("netlink: Failed to send(): %s", strerror(errno));
@@ -148,11 +159,6 @@ static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len)
 unsigned int nl_get_ext_if(int s, sa_family_t af)
 {
 	struct { struct nlmsghdr nlh; struct rtmsg rtm; } req = {
-		.nlh.nlmsg_type	 = RTM_GETROUTE,
-		.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_len	 = NLMSG_LENGTH(sizeof(struct rtmsg)),
-		.nlh.nlmsg_seq	 = nl_seq++,
-
 		.rtm.rtm_table	 = RT_TABLE_MAIN,
 		.rtm.rtm_scope	 = RT_SCOPE_UNIVERSE,
 		.rtm.rtm_type	 = RTN_UNICAST,
@@ -164,7 +170,7 @@ unsigned int nl_get_ext_if(int s, sa_family_t af)
 	ssize_t n;
 	size_t na;
 
-	n = nl_req(s, buf, &req, sizeof(req));
+	n = nl_req(s, buf, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	nh = (struct nlmsghdr *)buf;
 
@@ -205,11 +211,6 @@ void nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		struct rtattr rta;
 		unsigned int ifi;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETROUTE,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -223,7 +224,7 @@ void nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	n = nl_req(s, buf, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -278,11 +279,6 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 			} r4;
 		} set;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_NEWROUTE,
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK |
-				    NLM_F_CREATE | NLM_F_EXCL,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -294,12 +290,12 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		.ifi		  = ifi,
 	};
 	char buf[NLBUFSIZ];
+	ssize_t len;
 
 	if (af == AF_INET6) {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.r6.d));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.r6)
-			+ sizeof(req.set.r6);
+		len = offsetof(struct req_t, set.r6) + sizeof(req.set.r6);
 
 		req.set.r6.rta_dst.rta_type = RTA_DST;
 		req.set.r6.rta_dst.rta_len = rta_len;
@@ -310,8 +306,7 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 	} else {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.r4.d));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.r4)
-			+ sizeof(req.set.r4);
+		len = offsetof(struct req_t, set.r4) + sizeof(req.set.r4);
 
 		req.set.r4.rta_dst.rta_type = RTA_DST;
 		req.set.r4.rta_dst.rta_len = rta_len;
@@ -321,7 +316,7 @@ void nl_route_set_def(int s, unsigned int ifi, sa_family_t af, void *gw)
 		req.set.r4.rta_gw.rta_len = rta_len;
 	}
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
 }
 
 /**
@@ -341,11 +336,6 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		struct rtattr rta;
 		unsigned int ifi;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETROUTE,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_seq	  = nl_seq++,
-
 		.rtm.rtm_family	  = af,
 		.rtm.rtm_table	  = RT_TABLE_MAIN,
 		.rtm.rtm_scope	  = RT_SCOPE_UNIVERSE,
@@ -361,7 +351,8 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 	char buf[NLBUFSIZ];
 	unsigned i;
 
-	nlmsgs_size = nl_req(s_src, buf, &req, req.nlh.nlmsg_len);
+	nlmsgs_size = nl_req(s_src, buf, &req,
+			     RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf, n = nlmsgs_size;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -373,10 +364,6 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		if (nh->nlmsg_type != RTM_NEWROUTE)
 			continue;
 
-		nh->nlmsg_pid = 0;
-		nh->nlmsg_flags &= ~NLM_F_DUMP_FILTERED;
-		nh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK |
-			NLM_F_CREATE;
 		dup_routes++;
 
 		for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
@@ -397,13 +384,15 @@ void nl_route_dup(int s_src, unsigned int ifi_src,
 		for (nh = (struct nlmsghdr *)buf, n = nlmsgs_size;
 		     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
 		     nh = NLMSG_NEXT(nh, n)) {
+			uint16_t flags = nh->nlmsg_flags;
 			char resp[NLBUFSIZ];
 
 			if (nh->nlmsg_type != RTM_NEWROUTE)
 				continue;
 
-			nh->nlmsg_seq = nl_seq++;
-			nl_req(s_dst, resp, nh, nh->nlmsg_len);
+			nl_req(s_dst, resp, nh, RTM_NEWROUTE,
+			       (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
+			       nh->nlmsg_len);
 		}
 	}
 }
@@ -424,11 +413,6 @@ void nl_addr_get(int s, unsigned int ifi, sa_family_t af,
 		struct nlmsghdr nlh;
 		struct ifaddrmsg ifa;
 	} req = {
-		.nlh.nlmsg_type    = RTM_GETADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
-		.nlh.nlmsg_len     = sizeof(req),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi,
 	};
@@ -436,7 +420,7 @@ void nl_addr_get(int s, unsigned int ifi, sa_family_t af,
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	n = nl_req(s, buf, &req, RTM_GETADDR, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -500,18 +484,13 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 			} a6;
 		} set;
 	} req = {
-		.nlh.nlmsg_type    = RTM_NEWADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_ACK |
-				     NLM_F_CREATE | NLM_F_EXCL,
-		.nlh.nlmsg_len     = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi,
 		.ifa.ifa_prefixlen = prefix_len,
 		.ifa.ifa_scope	   = RT_SCOPE_UNIVERSE,
 	};
 	char buf[NLBUFSIZ];
+	ssize_t len;
 
 	if (af == AF_INET6) {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.a6.l));
@@ -519,8 +498,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 		/* By default, strictly speaking, it's duplicated */
 		req.ifa.ifa_flags = IFA_F_NODAD;
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.a6)
-			+ sizeof(req.set.a6);
+		len = offsetof(struct req_t, set.a6) + sizeof(req.set.a6);
 
 		memcpy(&req.set.a6.l, addr, sizeof(req.set.a6.l));
 		req.set.a6.rta_l.rta_len = rta_len;
@@ -531,8 +509,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 	} else {
 		size_t rta_len = RTA_LENGTH(sizeof(req.set.a4.l));
 
-		req.nlh.nlmsg_len = offsetof(struct req_t, set.a4)
-			+ sizeof(req.set.a4);
+		len = offsetof(struct req_t, set.a4) + sizeof(req.set.a4);
 
 		memcpy(&req.set.a4.l, addr, sizeof(req.set.a4.l));
 		req.set.a4.rta_l.rta_len = rta_len;
@@ -541,7 +518,7 @@ void nl_addr_set(int s, unsigned int ifi, sa_family_t af,
 		req.set.a4.rta_a.rta_type = IFA_ADDRESS;
 	}
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWADDR, NLM_F_CREATE | NLM_F_EXCL, len);
 }
 
 /**
@@ -559,11 +536,6 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 		struct nlmsghdr nlh;
 		struct ifaddrmsg ifa;
 	} req = {
-		.nlh.nlmsg_type    = RTM_GETADDR,
-		.nlh.nlmsg_flags   = NLM_F_REQUEST | NLM_F_DUMP,
-		.nlh.nlmsg_len     = sizeof(req),
-		.nlh.nlmsg_seq     = nl_seq++,
-
 		.ifa.ifa_family    = af,
 		.ifa.ifa_index     = ifi_src,
 		.ifa.ifa_prefixlen = 0,
@@ -572,7 +544,7 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 	struct nlmsghdr *nh;
 	ssize_t n;
 
-	n = nl_req(s_src, buf, &req, sizeof(req));
+	n = nl_req(s_src, buf, &req, RTM_GETADDR, NLM_F_DUMP, sizeof(req));
 
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
@@ -585,11 +557,6 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 		if (nh->nlmsg_type != RTM_NEWADDR)
 			continue;
 
-		nh->nlmsg_seq = nl_seq++;
-		nh->nlmsg_pid = 0;
-		nh->nlmsg_flags &= ~NLM_F_DUMP_FILTERED;
-		nh->nlmsg_flags |= NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
-
 		ifa = (struct ifaddrmsg *)NLMSG_DATA(nh);
 
 		if (ifa->ifa_scope == RT_SCOPE_LINK ||
@@ -604,7 +571,9 @@ void nl_addr_dup(int s_src, unsigned int ifi_src,
 				rta->rta_type = IFA_UNSPEC;
 		}
 
-		nl_req(s_dst, resp, nh, nh->nlmsg_len);
+		nl_req(s_dst, resp, nh, RTM_NEWADDR,
+		       (nh->nlmsg_flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
+		       nh->nlmsg_len);
 	}
 }
 
@@ -620,10 +589,6 @@ void nl_link_get_mac(int s, unsigned int ifi, void *mac)
 		struct nlmsghdr nlh;
 		struct ifinfomsg ifm;
 	} req = {
-		.nlh.nlmsg_type	  = RTM_GETLINK,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 	};
@@ -631,7 +596,7 @@ void nl_link_get_mac(int s, unsigned int ifi, void *mac)
 	char buf[NLBUFSIZ];
 	ssize_t n;
 
-	n = nl_req(s, buf, &req, sizeof(req));
+	n = nl_req(s, buf, &req, RTM_GETLINK, 0, sizeof(req));
 	for (nh = (struct nlmsghdr *)buf;
 	     NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE;
 	     nh = NLMSG_NEXT(nh, n)) {
@@ -669,10 +634,6 @@ void nl_link_set_mac(int s, unsigned int ifi, void *mac)
 		struct rtattr rta;
 		unsigned char mac[ETH_ALEN];
 	} req = {
-		.nlh.nlmsg_type	  = RTM_NEWLINK,
-		.nlh.nlmsg_len	  = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 		.rta.rta_type	  = IFLA_ADDRESS,
@@ -682,7 +643,7 @@ void nl_link_set_mac(int s, unsigned int ifi, void *mac)
 
 	memcpy(req.mac, mac, ETH_ALEN);
 
-	nl_req(s, buf, &req, sizeof(req));
+	nl_req(s, buf, &req, RTM_NEWLINK, 0, sizeof(req));
 }
 
 /**
@@ -699,10 +660,6 @@ void nl_link_up(int s, unsigned int ifi, int mtu)
 		struct rtattr rta;
 		unsigned int mtu;
 	} req = {
-		.nlh.nlmsg_type   = RTM_NEWLINK,
-		.nlh.nlmsg_len    = sizeof(req),
-		.nlh.nlmsg_flags  = NLM_F_REQUEST | NLM_F_ACK,
-		.nlh.nlmsg_seq	  = nl_seq++,
 		.ifm.ifi_family	  = AF_UNSPEC,
 		.ifm.ifi_index	  = ifi,
 		.ifm.ifi_flags	  = IFF_UP,
@@ -711,11 +668,12 @@ void nl_link_up(int s, unsigned int ifi, int mtu)
 		.rta.rta_len	  = RTA_LENGTH(sizeof(unsigned int)),
 		.mtu		  = mtu,
 	};
+	ssize_t len = sizeof(req);
 	char buf[NLBUFSIZ];
 
 	if (!mtu)
 		/* Shorten request to drop MTU attribute */
-		req.nlh.nlmsg_len = offsetof(struct req_t, rta);
+		len = offsetof(struct req_t, rta);
 
-	nl_req(s, buf, &req, req.nlh.nlmsg_len);
+	nl_req(s, buf, &req, RTM_NEWLINK, 0, len);
 }
-- 
2.41.0


  parent reply	other threads:[~2023-08-03  7:20 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-03  7:19 [PATCH v2 00/17] netlink fixes and cleanups David Gibson
2023-08-03  7:19 ` [PATCH v2 01/17] netlink: Split up functionality of nl_link() David Gibson
2023-08-03  7:19 ` [PATCH v2 02/17] netlink: Split nl_addr() into separate operation functions David Gibson
2023-08-03  7:19 ` [PATCH v2 03/17] netlink: Split nl_route() " David Gibson
2023-08-03  7:19 ` [PATCH v2 04/17] netlink: Use struct in_addr for IPv4 addresses, not bare uint32_t David Gibson
2023-08-03  7:19 ` [PATCH v2 05/17] netlink: Explicitly pass netlink sockets to operations David Gibson
2023-08-03  7:19 ` [PATCH v2 06/17] netlink: Make nl_*_dup() use a separate datagram for each request David Gibson
2023-08-03  7:19 ` [PATCH v2 07/17] netlink: Start sequence number from 1 instead of 0 David Gibson
2023-08-03  7:19 ` [PATCH v2 08/17] netlink: Treat send() or recv() errors as fatal David Gibson
2023-08-03  7:19 ` David Gibson [this message]
2023-08-03  7:19 ` [PATCH v2 10/17] netlink: Add nl_do() helper for simple operations with error checking David Gibson
2023-08-03  7:19 ` [PATCH v2 11/17] netlink: Clearer reasoning about the netlink response buffer size David Gibson
2023-08-03  7:19 ` [PATCH v2 12/17] netlink: Split nl_req() to allow processing multiple response datagrams David Gibson
2023-08-03  7:19 ` [PATCH v2 13/17] netlink: Add nl_foreach_oftype to filter response message types David Gibson
2023-08-03  7:19 ` [PATCH v2 14/17] netlink: Propagate errors for "set" operations David Gibson
2023-08-03  7:19 ` [PATCH v2 15/17] netlink: Always process all responses to a netlink request David Gibson
2023-08-03  7:19 ` [PATCH v2 16/17] netlink: Propagate errors for "dump" operations David Gibson
2023-08-03  7:19 ` [PATCH v2 17/17] netlink: Propagate errors for "dup" operations David Gibson
2023-08-04  7:04 ` [PATCH v2 00/17] netlink fixes and cleanups Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230803071956.3198452-10-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).