Blame SOURCES/0025-netlink-do-not-send-messages-and-process-replies-in-.patch

4f6fcd
From fc5f2a6bb2911a951bf5a1364dc5732e521d735a Mon Sep 17 00:00:00 2001
4f6fcd
From: Michal Kubecek <mkubecek@suse.cz>
4f6fcd
Date: Mon, 9 Nov 2020 14:29:56 +0100
4f6fcd
Subject: [PATCH 25/26] netlink: do not send messages and process replies in
4f6fcd
 nl_parser()
4f6fcd
4f6fcd
When called with group_style = PARSER_GROUP_MSG, nl_parser() not only
4f6fcd
parses the command line and composes the messages but also sends them to
4f6fcd
kernel and processes the replies. This is inconsistent with other modes and
4f6fcd
also impractical as it takes the control over the process from caller where
4f6fcd
it belongs.
4f6fcd
4f6fcd
Modify nl_parser() to pass composed messages back to caller (which is only
4f6fcd
nl_sset() at the moment) and let it send requests and process replies. This
4f6fcd
will be needed for an upcoming backward compatibility patch which will need
4f6fcd
to inspect and possibly modify one of the composed messages.
4f6fcd
4f6fcd
Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
4f6fcd
(cherry picked from commit acd9730d1e794f85caf1192fe8788876e6f96305)
4f6fcd
---
4f6fcd
 netlink/cable_test.c |  2 +-
4f6fcd
 netlink/channels.c   |  2 +-
4f6fcd
 netlink/coalesce.c   |  2 +-
4f6fcd
 netlink/eee.c        |  2 +-
4f6fcd
 netlink/parser.c     | 43 ++++++++++++++++++++++++++++---------------
4f6fcd
 netlink/parser.h     |  3 ++-
4f6fcd
 netlink/pause.c      |  2 +-
4f6fcd
 netlink/rings.c      |  2 +-
4f6fcd
 netlink/settings.c   | 35 ++++++++++++++++++++++++++++++-----
4f6fcd
 9 files changed, 66 insertions(+), 27 deletions(-)
4f6fcd
4f6fcd
diff --git a/netlink/cable_test.c b/netlink/cable_test.c
4f6fcd
index d39b7d82efb0..e05b67c4e231 100644
4f6fcd
--- a/netlink/cable_test.c
4f6fcd
+++ b/netlink/cable_test.c
4f6fcd
@@ -574,7 +574,7 @@ int nl_cable_test_tdr(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST);
4f6fcd
+	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return ret;
4f6fcd
 
4f6fcd
diff --git a/netlink/channels.c b/netlink/channels.c
4f6fcd
index c6002ceeb121..894c74bcc11a 100644
4f6fcd
--- a/netlink/channels.c
4f6fcd
+++ b/netlink/channels.c
4f6fcd
@@ -126,7 +126,7 @@ int nl_schannels(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE);
4f6fcd
+	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return 1;
4f6fcd
 
4f6fcd
diff --git a/netlink/coalesce.c b/netlink/coalesce.c
4f6fcd
index 65f75cf9a8dd..0223f8e3484e 100644
4f6fcd
--- a/netlink/coalesce.c
4f6fcd
+++ b/netlink/coalesce.c
4f6fcd
@@ -254,7 +254,7 @@ int nl_scoalesce(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE);
4f6fcd
+	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return 1;
4f6fcd
 
4f6fcd
diff --git a/netlink/eee.c b/netlink/eee.c
4f6fcd
index d3135b2094a4..04d8f0bbe3fc 100644
4f6fcd
--- a/netlink/eee.c
4f6fcd
+++ b/netlink/eee.c
4f6fcd
@@ -174,7 +174,7 @@ int nl_seee(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE);
4f6fcd
+	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return 1;
4f6fcd
 
4f6fcd
diff --git a/netlink/parser.c b/netlink/parser.c
4f6fcd
index 3b25f5d5a88e..c2eae93efb69 100644
4f6fcd
--- a/netlink/parser.c
4f6fcd
+++ b/netlink/parser.c
4f6fcd
@@ -920,7 +920,7 @@ static void __parser_set(uint64_t *map, unsigned int idx)
4f6fcd
 }
4f6fcd
 
4f6fcd
 struct tmp_buff {
4f6fcd
-	struct nl_msg_buff	msgbuff;
4f6fcd
+	struct nl_msg_buff	*msgbuff;
4f6fcd
 	unsigned int		id;
4f6fcd
 	unsigned int		orig_len;
4f6fcd
 	struct tmp_buff		*next;
4f6fcd
@@ -951,7 +951,12 @@ static struct tmp_buff *tmp_buff_find_or_create(struct tmp_buff **phead,
4f6fcd
 	if (!new_buff)
4f6fcd
 		return NULL;
4f6fcd
 	new_buff->id = id;
4f6fcd
-	msgbuff_init(&new_buff->msgbuff);
4f6fcd
+	new_buff->msgbuff = malloc(sizeof(*new_buff->msgbuff));
4f6fcd
+	if (!new_buff->msgbuff) {
4f6fcd
+		free(new_buff);
4f6fcd
+		return NULL;
4f6fcd
+	}
4f6fcd
+	msgbuff_init(new_buff->msgbuff);
4f6fcd
 	new_buff->next = NULL;
4f6fcd
 	*pbuff = new_buff;
4f6fcd
 
4f6fcd
@@ -965,7 +970,10 @@ static void tmp_buff_destroy(struct tmp_buff *head)
4f6fcd
 
4f6fcd
 	while (buff) {
4f6fcd
 		next = buff->next;
4f6fcd
-		msgbuff_done(&buff->msgbuff);
4f6fcd
+		if (buff->msgbuff) {
4f6fcd
+			msgbuff_done(buff->msgbuff);
4f6fcd
+			free(buff->msgbuff);
4f6fcd
+		}
4f6fcd
 		free(buff);
4f6fcd
 		buff = next;
4f6fcd
 	}
4f6fcd
@@ -980,13 +988,22 @@ static void tmp_buff_destroy(struct tmp_buff *head)
4f6fcd
  *               param_parser::offset)
4f6fcd
  * @group_style: defines if identifiers in .group represent separate messages,
4f6fcd
  *               nested attributes or are not allowed
4f6fcd
+ * @msgbuffs:    (only used for @group_style = PARSER_GROUP_MSG) array to store
4f6fcd
+ *               pointers to composed messages; caller must make sure this
4f6fcd
+ *               array is sufficient, i.e. that it has at least as many entries
4f6fcd
+ *               as the number of different .group values in params array;
4f6fcd
+ *               entries are filled from the start, remaining entries are not
4f6fcd
+ *               modified; caller should zero initialize the array before
4f6fcd
+ *               calling nl_parser()
4f6fcd
  */
4f6fcd
 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
-	      void *dest, enum parser_group_style group_style)
4f6fcd
+	      void *dest, enum parser_group_style group_style,
4f6fcd
+	      struct nl_msg_buff **msgbuffs)
4f6fcd
 {
4f6fcd
 	struct nl_socket *nlsk = nlctx->ethnl_socket;
4f6fcd
 	const struct param_parser *parser;
4f6fcd
 	struct tmp_buff *buffs = NULL;
4f6fcd
+	unsigned int n_msgbuffs = 0;
4f6fcd
 	struct tmp_buff *buff;
4f6fcd
 	unsigned int n_params;
4f6fcd
 	uint64_t *params_seen;
4f6fcd
@@ -1004,7 +1021,7 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
 		buff = tmp_buff_find_or_create(&buffs, parser->group);
4f6fcd
 		if (!buff)
4f6fcd
 			goto out_free_buffs;
4f6fcd
-		msgbuff = &buff->msgbuff;
4f6fcd
+		msgbuff = buff->msgbuff;
4f6fcd
 		ret = msg_init(nlctx, msgbuff, parser->group,
4f6fcd
 			       NLM_F_REQUEST | NLM_F_ACK);
4f6fcd
 		if (ret < 0)
4f6fcd
@@ -1013,7 +1030,7 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
 		switch (group_style) {
4f6fcd
 		case PARSER_GROUP_NEST:
4f6fcd
 			ret = -EMSGSIZE;
4f6fcd
-			nest = ethnla_nest_start(&buff->msgbuff, parser->group);
4f6fcd
+			nest = ethnla_nest_start(buff->msgbuff, parser->group);
4f6fcd
 			if (!nest)
4f6fcd
 				goto out_free_buffs;
4f6fcd
 			break;
4f6fcd
@@ -1062,7 +1079,7 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
 		buff = NULL;
4f6fcd
 		if (parser->group)
4f6fcd
 			buff = tmp_buff_find(buffs, parser->group);
4f6fcd
-		msgbuff = buff ? &buff->msgbuff : &nlsk->msgbuff;
4f6fcd
+		msgbuff = buff ? buff->msgbuff : &nlsk->msgbuff;
4f6fcd
 
4f6fcd
 		param_dest = dest ? ((char *)dest + parser->dest_offset) : NULL;
4f6fcd
 		ret = parser->handler(nlctx, parser->type, parser->handler_data,
4f6fcd
@@ -1074,12 +1091,12 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
 	if (group_style == PARSER_GROUP_MSG) {
4f6fcd
 		ret = -EOPNOTSUPP;
4f6fcd
 		for (buff = buffs; buff; buff = buff->next)
4f6fcd
-			if (msgbuff_len(&buff->msgbuff) > buff->orig_len &&
4f6fcd
+			if (msgbuff_len(buff->msgbuff) > buff->orig_len &&
4f6fcd
 			    netlink_cmd_check(nlctx->ctx, buff->id, false))
4f6fcd
 				goto out_free;
4f6fcd
 	}
4f6fcd
 	for (buff = buffs; buff; buff = buff->next) {
4f6fcd
-		struct nl_msg_buff *msgbuff = &buff->msgbuff;
4f6fcd
+		struct nl_msg_buff *msgbuff = buff->msgbuff;
4f6fcd
 
4f6fcd
 		if (group_style == PARSER_GROUP_NONE ||
4f6fcd
 		    msgbuff_len(msgbuff) == buff->orig_len)
4f6fcd
@@ -1092,12 +1109,8 @@ int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
 				goto out_free;
4f6fcd
 			break;
4f6fcd
 		case PARSER_GROUP_MSG:
4f6fcd
-			ret = nlsock_sendmsg(nlsk, msgbuff);
4f6fcd
-			if (ret < 0)
4f6fcd
-				goto out_free;
4f6fcd
-			ret = nlsock_process_reply(nlsk, nomsg_reply_cb, NULL);
4f6fcd
-			if (ret < 0)
4f6fcd
-				goto out_free;
4f6fcd
+			msgbuffs[n_msgbuffs++] = msgbuff;
4f6fcd
+			buff->msgbuff = NULL;
4f6fcd
 			break;
4f6fcd
 		default:
4f6fcd
 			break;
4f6fcd
diff --git a/netlink/parser.h b/netlink/parser.h
4f6fcd
index fd55bc768d42..28f26ccc2a1c 100644
4f6fcd
--- a/netlink/parser.h
4f6fcd
+++ b/netlink/parser.h
4f6fcd
@@ -143,6 +143,7 @@ int nl_parse_char_bitset(struct nl_context *nlctx, uint16_t type,
4f6fcd
 
4f6fcd
 /* main entry point called to parse the command line */
4f6fcd
 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
4f6fcd
-	      void *dest, enum parser_group_style group_style);
4f6fcd
+	      void *dest, enum parser_group_style group_style,
4f6fcd
+	      struct nl_msg_buff **msgbuffs);
4f6fcd
 
4f6fcd
 #endif /* ETHTOOL_NETLINK_PARSER_H__ */
4f6fcd
diff --git a/netlink/pause.c b/netlink/pause.c
4f6fcd
index 7b6b3a1d2c10..048320b123d2 100644
4f6fcd
--- a/netlink/pause.c
4f6fcd
+++ b/netlink/pause.c
4f6fcd
@@ -208,7 +208,7 @@ int nl_spause(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE);
4f6fcd
+	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return 1;
4f6fcd
 
4f6fcd
diff --git a/netlink/rings.c b/netlink/rings.c
4f6fcd
index 4061520212d5..b8c458fce25f 100644
4f6fcd
--- a/netlink/rings.c
4f6fcd
+++ b/netlink/rings.c
4f6fcd
@@ -126,7 +126,7 @@ int nl_sring(struct cmd_context *ctx)
4f6fcd
 			       ctx->devname, 0))
4f6fcd
 		return -EMSGSIZE;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE);
4f6fcd
+	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE, NULL);
4f6fcd
 	if (ret < 0)
4f6fcd
 		return 1;
4f6fcd
 
4f6fcd
diff --git a/netlink/settings.c b/netlink/settings.c
4f6fcd
index 75db15e5069c..fac192e2fbb7 100644
4f6fcd
--- a/netlink/settings.c
4f6fcd
+++ b/netlink/settings.c
4f6fcd
@@ -1108,9 +1108,16 @@ static const struct param_parser sset_params[] = {
4f6fcd
 	{}
4f6fcd
 };
4f6fcd
 
4f6fcd
+/* Maximum number of request messages sent to kernel; must be equal to the
4f6fcd
+ * number of different .group values in sset_params[] array.
4f6fcd
+ */
4f6fcd
+#define SSET_MAX_MSGS 4
4f6fcd
+
4f6fcd
 int nl_sset(struct cmd_context *ctx)
4f6fcd
 {
4f6fcd
+	struct nl_msg_buff *msgbuffs[SSET_MAX_MSGS] = {};
4f6fcd
 	struct nl_context *nlctx = ctx->nlctx;
4f6fcd
+	unsigned int i;
4f6fcd
 	int ret;
4f6fcd
 
4f6fcd
 	nlctx->cmd = "-s";
4f6fcd
@@ -1118,11 +1125,29 @@ int nl_sset(struct cmd_context *ctx)
4f6fcd
 	nlctx->argc = ctx->argc;
4f6fcd
 	nlctx->devname = ctx->devname;
4f6fcd
 
4f6fcd
-	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG);
4f6fcd
-	if (ret < 0)
4f6fcd
-		return 1;
4f6fcd
+	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG, msgbuffs);
4f6fcd
+	if (ret < 0) {
4f6fcd
+		ret = 1;
4f6fcd
+		goto out_free;
4f6fcd
+	}
4f6fcd
+
4f6fcd
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
4f6fcd
+		struct nl_socket *nlsk = nlctx->ethnl_socket;
4f6fcd
 
4f6fcd
-	if (ret == 0)
4f6fcd
-		return 0;
4f6fcd
+		ret = nlsock_sendmsg(nlsk, msgbuffs[i]);
4f6fcd
+		if (ret < 0)
4f6fcd
+			goto out_free;
4f6fcd
+		ret = nlsock_process_reply(nlsk, nomsg_reply_cb, NULL);
4f6fcd
+		if (ret < 0)
4f6fcd
+			goto out_free;
4f6fcd
+	}
4f6fcd
+
4f6fcd
+out_free:
4f6fcd
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
4f6fcd
+		msgbuff_done(msgbuffs[i]);
4f6fcd
+		free(msgbuffs[i]);
4f6fcd
+	}
4f6fcd
+	if (ret >= 0)
4f6fcd
+		return ret;
4f6fcd
 	return nlctx->exit_code ?: 75;
4f6fcd
 }
4f6fcd
-- 
4f6fcd
2.26.2
4f6fcd