netlink: do not send messages and process replies in nl_parser()

When called with group_style = PARSER_GROUP_MSG, nl_parser() not only
parses the command line and composes the messages but also sends them to
kernel and processes the replies. This is inconsistent with other modes and
also impractical as it takes the control over the process from caller where
it belongs.

Modify nl_parser() to pass composed messages back to caller (which is only
nl_sset() at the moment) and let it send requests and process replies. This
will be needed for an upcoming backward compatibility patch which will need
to inspect and possibly modify one of the composed messages.

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
diff --git a/netlink/cable_test.c b/netlink/cable_test.c
index 8a71453..17139f7 100644
--- a/netlink/cable_test.c
+++ b/netlink/cable_test.c
@@ -574,7 +574,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST);
+	ret = nl_parser(nlctx, tdr_params, NULL, PARSER_GROUP_NEST, NULL);
 	if (ret < 0)
 		return ret;
 
diff --git a/netlink/channels.c b/netlink/channels.c
index c6002ce..894c74b 100644
--- a/netlink/channels.c
+++ b/netlink/channels.c
@@ -126,7 +126,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, schannels_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
diff --git a/netlink/coalesce.c b/netlink/coalesce.c
index 07a92d0..75922a9 100644
--- a/netlink/coalesce.c
+++ b/netlink/coalesce.c
@@ -254,7 +254,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, scoalesce_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
diff --git a/netlink/eee.c b/netlink/eee.c
index d3135b2..04d8f0b 100644
--- a/netlink/eee.c
+++ b/netlink/eee.c
@@ -174,7 +174,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, seee_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
diff --git a/netlink/parser.c b/netlink/parser.c
index 3b25f5d..c2eae93 100644
--- a/netlink/parser.c
+++ b/netlink/parser.c
@@ -920,7 +920,7 @@
 }
 
 struct tmp_buff {
-	struct nl_msg_buff	msgbuff;
+	struct nl_msg_buff	*msgbuff;
 	unsigned int		id;
 	unsigned int		orig_len;
 	struct tmp_buff		*next;
@@ -951,7 +951,12 @@
 	if (!new_buff)
 		return NULL;
 	new_buff->id = id;
-	msgbuff_init(&new_buff->msgbuff);
+	new_buff->msgbuff = malloc(sizeof(*new_buff->msgbuff));
+	if (!new_buff->msgbuff) {
+		free(new_buff);
+		return NULL;
+	}
+	msgbuff_init(new_buff->msgbuff);
 	new_buff->next = NULL;
 	*pbuff = new_buff;
 
@@ -965,7 +970,10 @@
 
 	while (buff) {
 		next = buff->next;
-		msgbuff_done(&buff->msgbuff);
+		if (buff->msgbuff) {
+			msgbuff_done(buff->msgbuff);
+			free(buff->msgbuff);
+		}
 		free(buff);
 		buff = next;
 	}
@@ -980,13 +988,22 @@
  *               param_parser::offset)
  * @group_style: defines if identifiers in .group represent separate messages,
  *               nested attributes or are not allowed
+ * @msgbuffs:    (only used for @group_style = PARSER_GROUP_MSG) array to store
+ *               pointers to composed messages; caller must make sure this
+ *               array is sufficient, i.e. that it has at least as many entries
+ *               as the number of different .group values in params array;
+ *               entries are filled from the start, remaining entries are not
+ *               modified; caller should zero initialize the array before
+ *               calling nl_parser()
  */
 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
-	      void *dest, enum parser_group_style group_style)
+	      void *dest, enum parser_group_style group_style,
+	      struct nl_msg_buff **msgbuffs)
 {
 	struct nl_socket *nlsk = nlctx->ethnl_socket;
 	const struct param_parser *parser;
 	struct tmp_buff *buffs = NULL;
+	unsigned int n_msgbuffs = 0;
 	struct tmp_buff *buff;
 	unsigned int n_params;
 	uint64_t *params_seen;
@@ -1004,7 +1021,7 @@
 		buff = tmp_buff_find_or_create(&buffs, parser->group);
 		if (!buff)
 			goto out_free_buffs;
-		msgbuff = &buff->msgbuff;
+		msgbuff = buff->msgbuff;
 		ret = msg_init(nlctx, msgbuff, parser->group,
 			       NLM_F_REQUEST | NLM_F_ACK);
 		if (ret < 0)
@@ -1013,7 +1030,7 @@
 		switch (group_style) {
 		case PARSER_GROUP_NEST:
 			ret = -EMSGSIZE;
-			nest = ethnla_nest_start(&buff->msgbuff, parser->group);
+			nest = ethnla_nest_start(buff->msgbuff, parser->group);
 			if (!nest)
 				goto out_free_buffs;
 			break;
@@ -1062,7 +1079,7 @@
 		buff = NULL;
 		if (parser->group)
 			buff = tmp_buff_find(buffs, parser->group);
-		msgbuff = buff ? &buff->msgbuff : &nlsk->msgbuff;
+		msgbuff = buff ? buff->msgbuff : &nlsk->msgbuff;
 
 		param_dest = dest ? ((char *)dest + parser->dest_offset) : NULL;
 		ret = parser->handler(nlctx, parser->type, parser->handler_data,
@@ -1074,12 +1091,12 @@
 	if (group_style == PARSER_GROUP_MSG) {
 		ret = -EOPNOTSUPP;
 		for (buff = buffs; buff; buff = buff->next)
-			if (msgbuff_len(&buff->msgbuff) > buff->orig_len &&
+			if (msgbuff_len(buff->msgbuff) > buff->orig_len &&
 			    netlink_cmd_check(nlctx->ctx, buff->id, false))
 				goto out_free;
 	}
 	for (buff = buffs; buff; buff = buff->next) {
-		struct nl_msg_buff *msgbuff = &buff->msgbuff;
+		struct nl_msg_buff *msgbuff = buff->msgbuff;
 
 		if (group_style == PARSER_GROUP_NONE ||
 		    msgbuff_len(msgbuff) == buff->orig_len)
@@ -1092,12 +1109,8 @@
 				goto out_free;
 			break;
 		case PARSER_GROUP_MSG:
-			ret = nlsock_sendmsg(nlsk, msgbuff);
-			if (ret < 0)
-				goto out_free;
-			ret = nlsock_process_reply(nlsk, nomsg_reply_cb, NULL);
-			if (ret < 0)
-				goto out_free;
+			msgbuffs[n_msgbuffs++] = msgbuff;
+			buff->msgbuff = NULL;
 			break;
 		default:
 			break;
diff --git a/netlink/parser.h b/netlink/parser.h
index fd55bc7..28f26cc 100644
--- a/netlink/parser.h
+++ b/netlink/parser.h
@@ -143,6 +143,7 @@
 
 /* main entry point called to parse the command line */
 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
-	      void *dest, enum parser_group_style group_style);
+	      void *dest, enum parser_group_style group_style,
+	      struct nl_msg_buff **msgbuffs);
 
 #endif /* ETHTOOL_NETLINK_PARSER_H__ */
diff --git a/netlink/pause.c b/netlink/pause.c
index 9bc9a30..867d0da 100644
--- a/netlink/pause.c
+++ b/netlink/pause.c
@@ -293,7 +293,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, spause_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
diff --git a/netlink/rings.c b/netlink/rings.c
index 4061520..b8c458f 100644
--- a/netlink/rings.c
+++ b/netlink/rings.c
@@ -126,7 +126,7 @@
 			       ctx->devname, 0))
 		return -EMSGSIZE;
 
-	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE);
+	ret = nl_parser(nlctx, sring_params, NULL, PARSER_GROUP_NONE, NULL);
 	if (ret < 0)
 		return 1;
 
diff --git a/netlink/settings.c b/netlink/settings.c
index 41a2e5a..dc9280c 100644
--- a/netlink/settings.c
+++ b/netlink/settings.c
@@ -1110,9 +1110,16 @@
 	{}
 };
 
+/* Maximum number of request messages sent to kernel; must be equal to the
+ * number of different .group values in sset_params[] array.
+ */
+#define SSET_MAX_MSGS 4
+
 int nl_sset(struct cmd_context *ctx)
 {
+	struct nl_msg_buff *msgbuffs[SSET_MAX_MSGS] = {};
 	struct nl_context *nlctx = ctx->nlctx;
+	unsigned int i;
 	int ret;
 
 	nlctx->cmd = "-s";
@@ -1120,11 +1127,29 @@
 	nlctx->argc = ctx->argc;
 	nlctx->devname = ctx->devname;
 
-	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG);
-	if (ret < 0)
-		return 1;
+	ret = nl_parser(nlctx, sset_params, NULL, PARSER_GROUP_MSG, msgbuffs);
+	if (ret < 0) {
+		ret = 1;
+		goto out_free;
+	}
 
-	if (ret == 0)
-		return 0;
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
+		struct nl_socket *nlsk = nlctx->ethnl_socket;
+
+		ret = nlsock_sendmsg(nlsk, msgbuffs[i]);
+		if (ret < 0)
+			goto out_free;
+		ret = nlsock_process_reply(nlsk, nomsg_reply_cb, NULL);
+		if (ret < 0)
+			goto out_free;
+	}
+
+out_free:
+	for (i = 0; i < SSET_MAX_MSGS && msgbuffs[i]; i++) {
+		msgbuff_done(msgbuffs[i]);
+		free(msgbuffs[i]);
+	}
+	if (ret >= 0)
+		return ret;
 	return nlctx->exit_code ?: 75;
 }