Skip to content

Commit f5f66d2

Browse files
Gang Yanintel-lab-lkp
Gang Yan
authored andcommitted
selftests: mptcp: refactor NLMSG handling with 'proto'
This patch introduces the '__u32 proto' variable to the 'send_query' and 'recv_nlmsg' functions for further extending function. In the 'send_query' function, the inclusion of this variable makes the structure clearer and more readable. In the 'recv_nlmsg' function, the '__u32 proto' variable ensures that the 'diag_info' field remains unmodified when processing IPPROTO_TCP data, thereby preventing unintended transformation into 'mptcp_info' format. While at it, increment iovlen directly when an item is added to simplify this portion of the code and improve its readaility. Co-developed-by: Geliang Tang <[email protected]> Signed-off-by: Geliang Tang <[email protected]> Signed-off-by: Gang Yan <[email protected]>
1 parent a7109e1 commit f5f66d2

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

tools/testing/selftests/net/mptcp/mptcp_diag.c

+20-18
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void die_usage(int r)
6262
exit(r);
6363
}
6464

65-
static void send_query(int fd, struct inet_diag_req_v2 *r)
65+
static void send_query(int fd, struct inet_diag_req_v2 *r, __u32 proto)
6666
{
6767
struct sockaddr_nl nladdr = {
6868
.nl_family = AF_NETLINK
@@ -80,21 +80,22 @@ static void send_query(int fd, struct inet_diag_req_v2 *r)
8080
};
8181
struct rtattr rta_proto;
8282
struct iovec iov[6];
83-
int iovlen = 1;
84-
__u32 proto;
83+
int iovlen = 0;
8584

86-
proto = IPPROTO_MPTCP;
87-
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
88-
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
89-
90-
iov[0] = (struct iovec) {
85+
iov[iovlen++] = (struct iovec) {
9186
.iov_base = &req,
9287
.iov_len = sizeof(req)
9388
};
94-
iov[iovlen] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
95-
iov[iovlen + 1] = (struct iovec){ &proto, sizeof(proto)};
96-
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
97-
iovlen += 2;
89+
90+
if (proto == IPPROTO_MPTCP) {
91+
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
92+
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
93+
94+
iov[iovlen++] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
95+
iov[iovlen++] = (struct iovec){ &proto, sizeof(proto)};
96+
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
97+
}
98+
9899
struct msghdr msg = {
99100
.msg_name = &nladdr,
100101
.msg_namelen = sizeof(nladdr),
@@ -158,7 +159,7 @@ static void print_info_msg(struct mptcp_info *info)
158159
printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
159160
}
160161

161-
static void parse_nlmsg(struct nlmsghdr *nlh)
162+
static void parse_nlmsg(struct nlmsghdr *nlh, __u32 proto)
162163
{
163164
struct inet_diag_msg *r = NLMSG_DATA(nlh);
164165
struct rtattr *tb[INET_DIAG_MAX + 1];
@@ -167,7 +168,7 @@ static void parse_nlmsg(struct nlmsghdr *nlh)
167168
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)),
168169
NLA_F_NESTED);
169170

170-
if (tb[INET_DIAG_INFO]) {
171+
if (proto == IPPROTO_MPTCP && tb[INET_DIAG_INFO]) {
171172
int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
172173
struct mptcp_info *info;
173174

@@ -183,7 +184,7 @@ static void parse_nlmsg(struct nlmsghdr *nlh)
183184
}
184185
}
185186

186-
static void recv_nlmsg(int fd)
187+
static void recv_nlmsg(int fd, __u32 proto)
187188
{
188189
char rcv_buff[8192];
189190
struct nlmsghdr *nlh = (struct nlmsghdr *)rcv_buff;
@@ -216,7 +217,7 @@ static void recv_nlmsg(int fd)
216217
-(err->error), strerror(-(err->error)));
217218
break;
218219
}
219-
parse_nlmsg(nlh);
220+
parse_nlmsg(nlh, proto);
220221
nlh = NLMSG_NEXT(nlh, len);
221222
}
222223
}
@@ -230,14 +231,15 @@ static void get_mptcpinfo(__u32 token)
230231
.idiag_ext = 1 << (INET_DIAG_INFO - 1),
231232
.id.idiag_cookie[0] = token,
232233
};
234+
__u32 proto = IPPROTO_MPTCP;
233235
int fd;
234236

235237
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
236238
if (fd < 0)
237239
die_perror("Netlink socket");
238240

239-
send_query(fd, &r);
240-
recv_nlmsg(fd);
241+
send_query(fd, &r, proto);
242+
recv_nlmsg(fd, proto);
241243

242244
close(fd);
243245
}

0 commit comments

Comments
 (0)