mptcp_diag.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2025, Kylin Software */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10. #include <netinet/in.h>
  11. #include <linux/compiler.h>
  12. #include <linux/inet_diag.h>
  13. #include <linux/netlink.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/sock_diag.h>
  16. #include <linux/tcp.h>
  17. #ifndef IPPROTO_MPTCP
  18. #define IPPROTO_MPTCP 262
  19. #endif
  20. #define parse_rtattr_nested(tb, max, rta) \
  21. (parse_rtattr_flags((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta), \
  22. NLA_F_NESTED))
  23. struct params {
  24. __u32 target_token;
  25. char subflow_addrs[1024];
  26. };
  27. struct mptcp_info {
  28. __u8 mptcpi_subflows;
  29. __u8 mptcpi_add_addr_signal;
  30. __u8 mptcpi_add_addr_accepted;
  31. __u8 mptcpi_subflows_max;
  32. __u8 mptcpi_add_addr_signal_max;
  33. __u8 mptcpi_add_addr_accepted_max;
  34. __u32 mptcpi_flags;
  35. __u32 mptcpi_token;
  36. __u64 mptcpi_write_seq;
  37. __u64 mptcpi_snd_una;
  38. __u64 mptcpi_rcv_nxt;
  39. __u8 mptcpi_local_addr_used;
  40. __u8 mptcpi_local_addr_max;
  41. __u8 mptcpi_csum_enabled;
  42. __u32 mptcpi_retransmits;
  43. __u64 mptcpi_bytes_retrans;
  44. __u64 mptcpi_bytes_sent;
  45. __u64 mptcpi_bytes_received;
  46. __u64 mptcpi_bytes_acked;
  47. __u8 mptcpi_subflows_total;
  48. __u8 reserved[3];
  49. __u32 mptcpi_last_data_sent;
  50. __u32 mptcpi_last_data_recv;
  51. __u32 mptcpi_last_ack_recv;
  52. };
  53. enum {
  54. MPTCP_SUBFLOW_ATTR_UNSPEC,
  55. MPTCP_SUBFLOW_ATTR_TOKEN_REM,
  56. MPTCP_SUBFLOW_ATTR_TOKEN_LOC,
  57. MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ,
  58. MPTCP_SUBFLOW_ATTR_MAP_SEQ,
  59. MPTCP_SUBFLOW_ATTR_MAP_SFSEQ,
  60. MPTCP_SUBFLOW_ATTR_SSN_OFFSET,
  61. MPTCP_SUBFLOW_ATTR_MAP_DATALEN,
  62. MPTCP_SUBFLOW_ATTR_FLAGS,
  63. MPTCP_SUBFLOW_ATTR_ID_REM,
  64. MPTCP_SUBFLOW_ATTR_ID_LOC,
  65. MPTCP_SUBFLOW_ATTR_PAD,
  66. __MPTCP_SUBFLOW_ATTR_MAX
  67. };
  68. #define MPTCP_SUBFLOW_ATTR_MAX (__MPTCP_SUBFLOW_ATTR_MAX - 1)
  69. #define MPTCP_SUBFLOW_FLAG_MCAP_REM _BITUL(0)
  70. #define MPTCP_SUBFLOW_FLAG_MCAP_LOC _BITUL(1)
  71. #define MPTCP_SUBFLOW_FLAG_JOIN_REM _BITUL(2)
  72. #define MPTCP_SUBFLOW_FLAG_JOIN_LOC _BITUL(3)
  73. #define MPTCP_SUBFLOW_FLAG_BKUP_REM _BITUL(4)
  74. #define MPTCP_SUBFLOW_FLAG_BKUP_LOC _BITUL(5)
  75. #define MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED _BITUL(6)
  76. #define MPTCP_SUBFLOW_FLAG_CONNECTED _BITUL(7)
  77. #define MPTCP_SUBFLOW_FLAG_MAPVALID _BITUL(8)
  78. #define rta_getattr(type, value) (*(type *)RTA_DATA(value))
  79. static void __noreturn die_perror(const char *msg)
  80. {
  81. perror(msg);
  82. exit(1);
  83. }
  84. static void die_usage(int r)
  85. {
  86. fprintf(stderr, "Usage:\n"
  87. "mptcp_diag -t <token>\n"
  88. "mptcp_diag -s \"<saddr>:<sport> <daddr>:<dport>\"\n");
  89. exit(r);
  90. }
  91. static void send_query(int fd, struct inet_diag_req_v2 *r, __u32 proto)
  92. {
  93. struct sockaddr_nl nladdr = {
  94. .nl_family = AF_NETLINK
  95. };
  96. struct {
  97. struct nlmsghdr nlh;
  98. struct inet_diag_req_v2 r;
  99. } req = {
  100. .nlh = {
  101. .nlmsg_len = sizeof(req),
  102. .nlmsg_type = SOCK_DIAG_BY_FAMILY,
  103. .nlmsg_flags = NLM_F_REQUEST
  104. },
  105. .r = *r
  106. };
  107. struct rtattr rta_proto;
  108. struct iovec iov[6];
  109. int iovlen = 0;
  110. iov[iovlen++] = (struct iovec) {
  111. .iov_base = &req,
  112. .iov_len = sizeof(req)
  113. };
  114. if (proto == IPPROTO_MPTCP) {
  115. rta_proto.rta_type = INET_DIAG_REQ_PROTOCOL;
  116. rta_proto.rta_len = RTA_LENGTH(sizeof(proto));
  117. iov[iovlen++] = (struct iovec){ &rta_proto, sizeof(rta_proto)};
  118. iov[iovlen++] = (struct iovec){ &proto, sizeof(proto)};
  119. req.nlh.nlmsg_len += RTA_LENGTH(sizeof(proto));
  120. }
  121. struct msghdr msg = {
  122. .msg_name = &nladdr,
  123. .msg_namelen = sizeof(nladdr),
  124. .msg_iov = iov,
  125. .msg_iovlen = iovlen
  126. };
  127. for (;;) {
  128. if (sendmsg(fd, &msg, 0) < 0) {
  129. if (errno == EINTR)
  130. continue;
  131. die_perror("sendmsg");
  132. }
  133. break;
  134. }
  135. }
  136. static void parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta,
  137. int len, unsigned short flags)
  138. {
  139. unsigned short type;
  140. memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
  141. while (RTA_OK(rta, len)) {
  142. type = rta->rta_type & ~flags;
  143. if (type <= max && !tb[type])
  144. tb[type] = rta;
  145. rta = RTA_NEXT(rta, len);
  146. }
  147. }
  148. static void print_info_msg(struct mptcp_info *info)
  149. {
  150. printf("Token & Flags\n");
  151. printf("token: %x\n", info->mptcpi_token);
  152. printf("flags: %x\n", info->mptcpi_flags);
  153. printf("csum_enabled: %u\n", info->mptcpi_csum_enabled);
  154. printf("\nBasic Info\n");
  155. printf("subflows: %u\n", info->mptcpi_subflows);
  156. printf("subflows_max: %u\n", info->mptcpi_subflows_max);
  157. printf("subflows_total: %u\n", info->mptcpi_subflows_total);
  158. printf("local_addr_used: %u\n", info->mptcpi_local_addr_used);
  159. printf("local_addr_max: %u\n", info->mptcpi_local_addr_max);
  160. printf("add_addr_signal: %u\n", info->mptcpi_add_addr_signal);
  161. printf("add_addr_accepted: %u\n", info->mptcpi_add_addr_accepted);
  162. printf("add_addr_signal_max: %u\n", info->mptcpi_add_addr_signal_max);
  163. printf("add_addr_accepted_max: %u\n", info->mptcpi_add_addr_accepted_max);
  164. printf("\nTransmission Info\n");
  165. printf("write_seq: %llu\n", info->mptcpi_write_seq);
  166. printf("snd_una: %llu\n", info->mptcpi_snd_una);
  167. printf("rcv_nxt: %llu\n", info->mptcpi_rcv_nxt);
  168. printf("last_data_sent: %u\n", info->mptcpi_last_data_sent);
  169. printf("last_data_recv: %u\n", info->mptcpi_last_data_recv);
  170. printf("last_ack_recv: %u\n", info->mptcpi_last_ack_recv);
  171. printf("retransmits: %u\n", info->mptcpi_retransmits);
  172. printf("retransmit bytes: %llu\n", info->mptcpi_bytes_retrans);
  173. printf("bytes_sent: %llu\n", info->mptcpi_bytes_sent);
  174. printf("bytes_received: %llu\n", info->mptcpi_bytes_received);
  175. printf("bytes_acked: %llu\n", info->mptcpi_bytes_acked);
  176. }
  177. /*
  178. * 'print_subflow_info' is from 'mptcp_subflow_info'
  179. * which is a function in 'misc/ss.c' of iproute2.
  180. */
  181. static void print_subflow_info(struct rtattr *tb[])
  182. {
  183. u_int32_t flags = 0;
  184. printf("It's a mptcp subflow, the subflow info:\n");
  185. if (tb[MPTCP_SUBFLOW_ATTR_FLAGS]) {
  186. char caps[32 + 1] = { 0 }, *cap = &caps[0];
  187. flags = rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_FLAGS]);
  188. if (flags & MPTCP_SUBFLOW_FLAG_MCAP_REM)
  189. *cap++ = 'M';
  190. if (flags & MPTCP_SUBFLOW_FLAG_MCAP_LOC)
  191. *cap++ = 'm';
  192. if (flags & MPTCP_SUBFLOW_FLAG_JOIN_REM)
  193. *cap++ = 'J';
  194. if (flags & MPTCP_SUBFLOW_FLAG_JOIN_LOC)
  195. *cap++ = 'j';
  196. if (flags & MPTCP_SUBFLOW_FLAG_BKUP_REM)
  197. *cap++ = 'B';
  198. if (flags & MPTCP_SUBFLOW_FLAG_BKUP_LOC)
  199. *cap++ = 'b';
  200. if (flags & MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED)
  201. *cap++ = 'e';
  202. if (flags & MPTCP_SUBFLOW_FLAG_CONNECTED)
  203. *cap++ = 'c';
  204. if (flags & MPTCP_SUBFLOW_FLAG_MAPVALID)
  205. *cap++ = 'v';
  206. if (flags)
  207. printf(" flags:%s", caps);
  208. }
  209. if (tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM] &&
  210. tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC] &&
  211. tb[MPTCP_SUBFLOW_ATTR_ID_REM] &&
  212. tb[MPTCP_SUBFLOW_ATTR_ID_LOC])
  213. printf(" token:%04x(id:%u)/%04x(id:%u)",
  214. rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_TOKEN_REM]),
  215. rta_getattr(__u8, tb[MPTCP_SUBFLOW_ATTR_ID_REM]),
  216. rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_TOKEN_LOC]),
  217. rta_getattr(__u8, tb[MPTCP_SUBFLOW_ATTR_ID_LOC]));
  218. if (tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ])
  219. printf(" seq:%llu",
  220. rta_getattr(__u64, tb[MPTCP_SUBFLOW_ATTR_MAP_SEQ]));
  221. if (tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ])
  222. printf(" sfseq:%u",
  223. rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_MAP_SFSEQ]));
  224. if (tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET])
  225. printf(" ssnoff:%u",
  226. rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_SSN_OFFSET]));
  227. if (tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN])
  228. printf(" maplen:%u",
  229. rta_getattr(__u32, tb[MPTCP_SUBFLOW_ATTR_MAP_DATALEN]));
  230. printf("\n");
  231. }
  232. static void parse_nlmsg(struct nlmsghdr *nlh, __u32 proto)
  233. {
  234. struct inet_diag_msg *r = NLMSG_DATA(nlh);
  235. struct rtattr *tb[INET_DIAG_MAX + 1];
  236. parse_rtattr_flags(tb, INET_DIAG_MAX, (struct rtattr *)(r + 1),
  237. nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)),
  238. NLA_F_NESTED);
  239. if (proto == IPPROTO_MPTCP && tb[INET_DIAG_INFO]) {
  240. int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
  241. struct mptcp_info *info;
  242. /* workaround fort older kernels with less fields */
  243. if (len < sizeof(*info)) {
  244. info = alloca(sizeof(*info));
  245. memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
  246. memset((char *)info + len, 0, sizeof(*info) - len);
  247. } else {
  248. info = RTA_DATA(tb[INET_DIAG_INFO]);
  249. }
  250. print_info_msg(info);
  251. }
  252. if (proto == IPPROTO_TCP && tb[INET_DIAG_ULP_INFO]) {
  253. struct rtattr *ulpinfo[INET_ULP_INFO_MAX + 1] = { 0 };
  254. parse_rtattr_nested(ulpinfo, INET_ULP_INFO_MAX,
  255. tb[INET_DIAG_ULP_INFO]);
  256. if (ulpinfo[INET_ULP_INFO_MPTCP]) {
  257. struct rtattr *sfinfo[MPTCP_SUBFLOW_ATTR_MAX + 1] = { 0 };
  258. parse_rtattr_nested(sfinfo, MPTCP_SUBFLOW_ATTR_MAX,
  259. ulpinfo[INET_ULP_INFO_MPTCP]);
  260. print_subflow_info(sfinfo);
  261. } else {
  262. printf("It's a normal TCP!\n");
  263. }
  264. }
  265. }
  266. static void recv_nlmsg(int fd, __u32 proto)
  267. {
  268. char rcv_buff[8192];
  269. struct nlmsghdr *nlh = (struct nlmsghdr *)rcv_buff;
  270. struct sockaddr_nl rcv_nladdr = {
  271. .nl_family = AF_NETLINK
  272. };
  273. struct iovec rcv_iov = {
  274. .iov_base = rcv_buff,
  275. .iov_len = sizeof(rcv_buff)
  276. };
  277. struct msghdr rcv_msg = {
  278. .msg_name = &rcv_nladdr,
  279. .msg_namelen = sizeof(rcv_nladdr),
  280. .msg_iov = &rcv_iov,
  281. .msg_iovlen = 1
  282. };
  283. int len;
  284. len = recvmsg(fd, &rcv_msg, 0);
  285. while (NLMSG_OK(nlh, len)) {
  286. if (nlh->nlmsg_type == NLMSG_DONE) {
  287. printf("NLMSG_DONE\n");
  288. break;
  289. } else if (nlh->nlmsg_type == NLMSG_ERROR) {
  290. struct nlmsgerr *err;
  291. err = (struct nlmsgerr *)NLMSG_DATA(nlh);
  292. printf("Error %d:%s\n",
  293. -(err->error), strerror(-(err->error)));
  294. break;
  295. }
  296. parse_nlmsg(nlh, proto);
  297. nlh = NLMSG_NEXT(nlh, len);
  298. }
  299. }
  300. static void get_mptcpinfo(__u32 token)
  301. {
  302. struct inet_diag_req_v2 r = {
  303. .sdiag_family = AF_INET,
  304. /* Real proto is set via INET_DIAG_REQ_PROTOCOL */
  305. .sdiag_protocol = IPPROTO_TCP,
  306. .idiag_ext = 1 << (INET_DIAG_INFO - 1),
  307. .id.idiag_cookie[0] = token,
  308. };
  309. __u32 proto = IPPROTO_MPTCP;
  310. int fd;
  311. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
  312. if (fd < 0)
  313. die_perror("Netlink socket");
  314. send_query(fd, &r, proto);
  315. recv_nlmsg(fd, proto);
  316. close(fd);
  317. }
  318. static void get_subflow_info(char *subflow_addrs)
  319. {
  320. struct inet_diag_req_v2 r = {
  321. .sdiag_family = AF_INET,
  322. .sdiag_protocol = IPPROTO_TCP,
  323. .idiag_ext = 1 << (INET_DIAG_INFO - 1),
  324. .id.idiag_cookie[0] = INET_DIAG_NOCOOKIE,
  325. .id.idiag_cookie[1] = INET_DIAG_NOCOOKIE,
  326. };
  327. char saddr[64], daddr[64];
  328. int sport, dport;
  329. int ret;
  330. int fd;
  331. ret = sscanf(subflow_addrs, "%[^:]:%d %[^:]:%d", saddr, &sport, daddr, &dport);
  332. if (ret != 4)
  333. die_perror("IP PORT Pairs has style problems!");
  334. printf("%s:%d -> %s:%d\n", saddr, sport, daddr, dport);
  335. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
  336. if (fd < 0)
  337. die_perror("Netlink socket");
  338. r.id.idiag_sport = htons(sport);
  339. r.id.idiag_dport = htons(dport);
  340. inet_pton(AF_INET, saddr, &r.id.idiag_src);
  341. inet_pton(AF_INET, daddr, &r.id.idiag_dst);
  342. send_query(fd, &r, IPPROTO_TCP);
  343. recv_nlmsg(fd, IPPROTO_TCP);
  344. }
  345. static void parse_opts(int argc, char **argv, struct params *p)
  346. {
  347. int c;
  348. if (argc < 2)
  349. die_usage(1);
  350. while ((c = getopt(argc, argv, "ht:s:")) != -1) {
  351. switch (c) {
  352. case 'h':
  353. die_usage(0);
  354. break;
  355. case 't':
  356. sscanf(optarg, "%x", &p->target_token);
  357. break;
  358. case 's':
  359. strncpy(p->subflow_addrs, optarg,
  360. sizeof(p->subflow_addrs) - 1);
  361. break;
  362. default:
  363. die_usage(1);
  364. break;
  365. }
  366. }
  367. }
  368. int main(int argc, char *argv[])
  369. {
  370. struct params p = { 0 };
  371. parse_opts(argc, argv, &p);
  372. if (p.target_token)
  373. get_mptcpinfo(p.target_token);
  374. if (p.subflow_addrs[0] != '\0')
  375. get_subflow_info(p.subflow_addrs);
  376. return 0;
  377. }