Skip to content

Commit 54ba681

Browse files
Dwyane-Yanintel-lab-lkp
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. Co-developed-by: Geliang Tang <[email protected]> Signed-off-by: Geliang Tang <[email protected]> Signed-off-by: Gang Yan <[email protected]>
1 parent e7f625b commit 54ba681

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
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
@@ -81,19 +81,21 @@ static void send_query(int fd, struct inet_diag_req_v2 *r)
8181
struct rtattr rta_proto;
8282
struct iovec iov[6];
8383
int iovlen = 1;
84-
__u32 proto;
85-
86-
proto = IPPROTO_MPTCP;
87-
rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
88-
rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
8984

9085
iov[0] = (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++] = (struct iovec){ &proto, sizeof(proto)};
96-
req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
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+
9799
struct msghdr msg = {
98100
.msg_name = &nladdr,
99101
.msg_namelen = sizeof(nladdr),
@@ -157,7 +159,7 @@ static void print_info_msg(struct mptcp_info *info)
157159
printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
158160
}
159161

160-
static void parse_nlmsg(struct nlmsghdr *nlh)
162+
static void parse_nlmsg(struct nlmsghdr *nlh, __u32 proto)
161163
{
162164
struct inet_diag_msg *r = NLMSG_DATA(nlh);
163165
struct rtattr *tb[INET_DIAG_MAX + 1];
@@ -166,7 +168,7 @@ static void parse_nlmsg(struct nlmsghdr *nlh)
166168
nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)),
167169
NLA_F_NESTED);
168170

169-
if (tb[INET_DIAG_INFO]) {
171+
if (proto == IPPROTO_MPTCP && tb[INET_DIAG_INFO]) {
170172
int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
171173
struct mptcp_info *info;
172174

@@ -182,7 +184,7 @@ static void parse_nlmsg(struct nlmsghdr *nlh)
182184
}
183185
}
184186

185-
static void recv_nlmsg(int fd)
187+
static void recv_nlmsg(int fd, __u32 proto)
186188
{
187189
char rcv_buff[8192];
188190
struct nlmsghdr *nlh = (struct nlmsghdr *)rcv_buff;
@@ -215,7 +217,7 @@ static void recv_nlmsg(int fd)
215217
-(err->error), strerror(-(err->error)));
216218
break;
217219
}
218-
parse_nlmsg(nlh);
220+
parse_nlmsg(nlh, proto);
219221
nlh = NLMSG_NEXT(nlh, len);
220222
}
221223
}
@@ -228,15 +230,16 @@ static void get_mptcpinfo(__u32 token)
228230
.sdiag_protocol = IPPROTO_TCP,
229231
.id.idiag_cookie[0] = token,
230232
};
233+
__u32 proto = IPPROTO_MPTCP;
231234
int fd;
232235

233236
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
234237
if (fd < 0)
235238
die_perror("Netlink socket");
236239

237240
r.idiag_ext |= (1 << (INET_DIAG_INFO - 1));
238-
send_query(fd, &r);
239-
recv_nlmsg(fd);
241+
send_query(fd, &r, proto);
242+
recv_nlmsg(fd, proto);
240243

241244
close(fd);
242245
}

0 commit comments

Comments
 (0)