cmsg_sender.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <errno.h>
  3. #include <error.h>
  4. #include <netdb.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <linux/errqueue.h>
  12. #include <linux/icmp.h>
  13. #include <linux/icmpv6.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/types.h>
  16. #include <linux/udp.h>
  17. #include <sys/socket.h>
  18. #include "../kselftest.h"
  19. enum {
  20. ERN_SUCCESS = 0,
  21. /* Well defined errors, callers may depend on these */
  22. ERN_SEND = 1,
  23. /* Informational, can reorder */
  24. ERN_HELP,
  25. ERN_SEND_SHORT,
  26. ERN_SOCK_CREATE,
  27. ERN_RESOLVE,
  28. ERN_CMSG_WR,
  29. ERN_SOCKOPT,
  30. ERN_GETTIME,
  31. ERN_RECVERR,
  32. ERN_CMSG_RD,
  33. ERN_CMSG_RCV,
  34. };
  35. struct option_cmsg_u32 {
  36. bool ena;
  37. unsigned int val;
  38. };
  39. struct options {
  40. bool silent_send;
  41. const char *host;
  42. const char *service;
  43. unsigned int size;
  44. struct {
  45. unsigned int mark;
  46. unsigned int dontfrag;
  47. unsigned int tclass;
  48. unsigned int hlimit;
  49. } sockopt;
  50. struct {
  51. unsigned int family;
  52. unsigned int type;
  53. unsigned int proto;
  54. } sock;
  55. struct option_cmsg_u32 mark;
  56. struct {
  57. bool ena;
  58. unsigned int delay;
  59. } txtime;
  60. struct {
  61. bool ena;
  62. } ts;
  63. struct {
  64. struct option_cmsg_u32 dontfrag;
  65. struct option_cmsg_u32 tclass;
  66. struct option_cmsg_u32 hlimit;
  67. struct option_cmsg_u32 exthdr;
  68. } v6;
  69. } opt = {
  70. .size = 13,
  71. .sock = {
  72. .family = AF_UNSPEC,
  73. .type = SOCK_DGRAM,
  74. .proto = IPPROTO_UDP,
  75. },
  76. };
  77. static struct timespec time_start_real;
  78. static struct timespec time_start_mono;
  79. static void __attribute__((noreturn)) cs_usage(const char *bin)
  80. {
  81. printf("Usage: %s [opts] <dst host> <dst port / service>\n", bin);
  82. printf("Options:\n"
  83. "\t\t-s Silent send() failures\n"
  84. "\t\t-S send() size\n"
  85. "\t\t-4/-6 Force IPv4 / IPv6 only\n"
  86. "\t\t-p prot Socket protocol\n"
  87. "\t\t (u = UDP (default); i = ICMP; r = RAW)\n"
  88. "\n"
  89. "\t\t-m val Set SO_MARK with given value\n"
  90. "\t\t-M val Set SO_MARK via setsockopt\n"
  91. "\t\t-d val Set SO_TXTIME with given delay (usec)\n"
  92. "\t\t-t Enable time stamp reporting\n"
  93. "\t\t-f val Set don't fragment via cmsg\n"
  94. "\t\t-F val Set don't fragment via setsockopt\n"
  95. "\t\t-c val Set TCLASS via cmsg\n"
  96. "\t\t-C val Set TCLASS via setsockopt\n"
  97. "\t\t-l val Set HOPLIMIT via cmsg\n"
  98. "\t\t-L val Set HOPLIMIT via setsockopt\n"
  99. "\t\t-H type Add an IPv6 header option\n"
  100. "\t\t (h = HOP; d = DST; r = RTDST)"
  101. "");
  102. exit(ERN_HELP);
  103. }
  104. static void cs_parse_args(int argc, char *argv[])
  105. {
  106. int o;
  107. while ((o = getopt(argc, argv, "46sS:p:m:M:d:tf:F:c:C:l:L:H:")) != -1) {
  108. switch (o) {
  109. case 's':
  110. opt.silent_send = true;
  111. break;
  112. case 'S':
  113. opt.size = atoi(optarg);
  114. break;
  115. case '4':
  116. opt.sock.family = AF_INET;
  117. break;
  118. case '6':
  119. opt.sock.family = AF_INET6;
  120. break;
  121. case 'p':
  122. if (*optarg == 'u' || *optarg == 'U') {
  123. opt.sock.proto = IPPROTO_UDP;
  124. } else if (*optarg == 'i' || *optarg == 'I') {
  125. opt.sock.proto = IPPROTO_ICMP;
  126. } else if (*optarg == 'r') {
  127. opt.sock.type = SOCK_RAW;
  128. } else {
  129. printf("Error: unknown protocol: %s\n", optarg);
  130. cs_usage(argv[0]);
  131. }
  132. break;
  133. case 'm':
  134. opt.mark.ena = true;
  135. opt.mark.val = atoi(optarg);
  136. break;
  137. case 'M':
  138. opt.sockopt.mark = atoi(optarg);
  139. break;
  140. case 'd':
  141. opt.txtime.ena = true;
  142. opt.txtime.delay = atoi(optarg);
  143. break;
  144. case 't':
  145. opt.ts.ena = true;
  146. break;
  147. case 'f':
  148. opt.v6.dontfrag.ena = true;
  149. opt.v6.dontfrag.val = atoi(optarg);
  150. break;
  151. case 'F':
  152. opt.sockopt.dontfrag = atoi(optarg);
  153. break;
  154. case 'c':
  155. opt.v6.tclass.ena = true;
  156. opt.v6.tclass.val = atoi(optarg);
  157. break;
  158. case 'C':
  159. opt.sockopt.tclass = atoi(optarg);
  160. break;
  161. case 'l':
  162. opt.v6.hlimit.ena = true;
  163. opt.v6.hlimit.val = atoi(optarg);
  164. break;
  165. case 'L':
  166. opt.sockopt.hlimit = atoi(optarg);
  167. break;
  168. case 'H':
  169. opt.v6.exthdr.ena = true;
  170. switch (optarg[0]) {
  171. case 'h':
  172. opt.v6.exthdr.val = IPV6_HOPOPTS;
  173. break;
  174. case 'd':
  175. opt.v6.exthdr.val = IPV6_DSTOPTS;
  176. break;
  177. case 'r':
  178. opt.v6.exthdr.val = IPV6_RTHDRDSTOPTS;
  179. break;
  180. default:
  181. printf("Error: hdr type: %s\n", optarg);
  182. break;
  183. }
  184. break;
  185. }
  186. }
  187. if (optind != argc - 2)
  188. cs_usage(argv[0]);
  189. opt.host = argv[optind];
  190. opt.service = argv[optind + 1];
  191. }
  192. static void memrnd(void *s, size_t n)
  193. {
  194. int *dword = s;
  195. char *byte;
  196. for (; n >= 4; n -= 4)
  197. *dword++ = rand();
  198. byte = (void *)dword;
  199. while (n--)
  200. *byte++ = rand();
  201. }
  202. static void
  203. ca_write_cmsg_u32(char *cbuf, size_t cbuf_sz, size_t *cmsg_len,
  204. int level, int optname, struct option_cmsg_u32 *uopt)
  205. {
  206. struct cmsghdr *cmsg;
  207. if (!uopt->ena)
  208. return;
  209. cmsg = (struct cmsghdr *)(cbuf + *cmsg_len);
  210. *cmsg_len += CMSG_SPACE(sizeof(__u32));
  211. if (cbuf_sz < *cmsg_len)
  212. error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
  213. cmsg->cmsg_level = level;
  214. cmsg->cmsg_type = optname;
  215. cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
  216. *(__u32 *)CMSG_DATA(cmsg) = uopt->val;
  217. }
  218. static void
  219. cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
  220. {
  221. struct cmsghdr *cmsg;
  222. size_t cmsg_len;
  223. msg->msg_control = cbuf;
  224. cmsg_len = 0;
  225. ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len,
  226. SOL_SOCKET, SO_MARK, &opt.mark);
  227. ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len,
  228. SOL_IPV6, IPV6_DONTFRAG, &opt.v6.dontfrag);
  229. ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len,
  230. SOL_IPV6, IPV6_TCLASS, &opt.v6.tclass);
  231. ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len,
  232. SOL_IPV6, IPV6_HOPLIMIT, &opt.v6.hlimit);
  233. if (opt.txtime.ena) {
  234. struct sock_txtime so_txtime = {
  235. .clockid = CLOCK_MONOTONIC,
  236. };
  237. __u64 txtime;
  238. if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
  239. &so_txtime, sizeof(so_txtime)))
  240. error(ERN_SOCKOPT, errno, "setsockopt TXTIME");
  241. txtime = time_start_mono.tv_sec * (1000ULL * 1000 * 1000) +
  242. time_start_mono.tv_nsec +
  243. opt.txtime.delay * 1000;
  244. cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
  245. cmsg_len += CMSG_SPACE(sizeof(txtime));
  246. if (cbuf_sz < cmsg_len)
  247. error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
  248. cmsg->cmsg_level = SOL_SOCKET;
  249. cmsg->cmsg_type = SCM_TXTIME;
  250. cmsg->cmsg_len = CMSG_LEN(sizeof(txtime));
  251. memcpy(CMSG_DATA(cmsg), &txtime, sizeof(txtime));
  252. }
  253. if (opt.ts.ena) {
  254. __u32 val = SOF_TIMESTAMPING_SOFTWARE |
  255. SOF_TIMESTAMPING_OPT_TSONLY;
  256. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
  257. &val, sizeof(val)))
  258. error(ERN_SOCKOPT, errno, "setsockopt TIMESTAMPING");
  259. cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
  260. cmsg_len += CMSG_SPACE(sizeof(__u32));
  261. if (cbuf_sz < cmsg_len)
  262. error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
  263. cmsg->cmsg_level = SOL_SOCKET;
  264. cmsg->cmsg_type = SO_TIMESTAMPING;
  265. cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
  266. *(__u32 *)CMSG_DATA(cmsg) = SOF_TIMESTAMPING_TX_SCHED |
  267. SOF_TIMESTAMPING_TX_SOFTWARE;
  268. }
  269. if (opt.v6.exthdr.ena) {
  270. cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
  271. cmsg_len += CMSG_SPACE(8);
  272. if (cbuf_sz < cmsg_len)
  273. error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
  274. cmsg->cmsg_level = SOL_IPV6;
  275. cmsg->cmsg_type = opt.v6.exthdr.val;
  276. cmsg->cmsg_len = CMSG_LEN(8);
  277. *(__u64 *)CMSG_DATA(cmsg) = 0;
  278. }
  279. if (cmsg_len)
  280. msg->msg_controllen = cmsg_len;
  281. else
  282. msg->msg_control = NULL;
  283. }
  284. static const char *cs_ts_info2str(unsigned int info)
  285. {
  286. static const char *names[] = {
  287. [SCM_TSTAMP_SND] = "SND",
  288. [SCM_TSTAMP_SCHED] = "SCHED",
  289. [SCM_TSTAMP_ACK] = "ACK",
  290. };
  291. if (info < ARRAY_SIZE(names))
  292. return names[info];
  293. return "unknown";
  294. }
  295. static void
  296. cs_read_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
  297. {
  298. struct sock_extended_err *see;
  299. struct scm_timestamping *ts;
  300. struct cmsghdr *cmsg;
  301. int i, err;
  302. if (!opt.ts.ena)
  303. return;
  304. msg->msg_control = cbuf;
  305. msg->msg_controllen = cbuf_sz;
  306. while (true) {
  307. ts = NULL;
  308. see = NULL;
  309. memset(cbuf, 0, cbuf_sz);
  310. err = recvmsg(fd, msg, MSG_ERRQUEUE);
  311. if (err < 0) {
  312. if (errno == EAGAIN)
  313. break;
  314. error(ERN_RECVERR, errno, "recvmsg ERRQ");
  315. }
  316. for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
  317. cmsg = CMSG_NXTHDR(msg, cmsg)) {
  318. if (cmsg->cmsg_level == SOL_SOCKET &&
  319. cmsg->cmsg_type == SO_TIMESTAMPING_OLD) {
  320. if (cmsg->cmsg_len < sizeof(*ts))
  321. error(ERN_CMSG_RD, EINVAL, "TS cmsg");
  322. ts = (void *)CMSG_DATA(cmsg);
  323. }
  324. if ((cmsg->cmsg_level == SOL_IP &&
  325. cmsg->cmsg_type == IP_RECVERR) ||
  326. (cmsg->cmsg_level == SOL_IPV6 &&
  327. cmsg->cmsg_type == IPV6_RECVERR)) {
  328. if (cmsg->cmsg_len < sizeof(*see))
  329. error(ERN_CMSG_RD, EINVAL, "sock_err cmsg");
  330. see = (void *)CMSG_DATA(cmsg);
  331. }
  332. }
  333. if (!ts)
  334. error(ERN_CMSG_RCV, ENOENT, "TS cmsg not found");
  335. if (!see)
  336. error(ERN_CMSG_RCV, ENOENT, "sock_err cmsg not found");
  337. for (i = 0; i < 3; i++) {
  338. unsigned long long rel_time;
  339. if (!ts->ts[i].tv_sec && !ts->ts[i].tv_nsec)
  340. continue;
  341. rel_time = (ts->ts[i].tv_sec - time_start_real.tv_sec) *
  342. (1000ULL * 1000) +
  343. (ts->ts[i].tv_nsec - time_start_real.tv_nsec) /
  344. 1000;
  345. printf(" %5s ts%d %lluus\n",
  346. cs_ts_info2str(see->ee_info),
  347. i, rel_time);
  348. }
  349. }
  350. }
  351. static void ca_set_sockopts(int fd)
  352. {
  353. if (opt.sockopt.mark &&
  354. setsockopt(fd, SOL_SOCKET, SO_MARK,
  355. &opt.sockopt.mark, sizeof(opt.sockopt.mark)))
  356. error(ERN_SOCKOPT, errno, "setsockopt SO_MARK");
  357. if (opt.sockopt.dontfrag &&
  358. setsockopt(fd, SOL_IPV6, IPV6_DONTFRAG,
  359. &opt.sockopt.dontfrag, sizeof(opt.sockopt.dontfrag)))
  360. error(ERN_SOCKOPT, errno, "setsockopt IPV6_DONTFRAG");
  361. if (opt.sockopt.tclass &&
  362. setsockopt(fd, SOL_IPV6, IPV6_TCLASS,
  363. &opt.sockopt.tclass, sizeof(opt.sockopt.tclass)))
  364. error(ERN_SOCKOPT, errno, "setsockopt IPV6_TCLASS");
  365. if (opt.sockopt.hlimit &&
  366. setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS,
  367. &opt.sockopt.hlimit, sizeof(opt.sockopt.hlimit)))
  368. error(ERN_SOCKOPT, errno, "setsockopt IPV6_HOPLIMIT");
  369. }
  370. int main(int argc, char *argv[])
  371. {
  372. struct addrinfo hints, *ai;
  373. struct iovec iov[1];
  374. unsigned char *buf;
  375. struct msghdr msg;
  376. char cbuf[1024];
  377. int err;
  378. int fd;
  379. cs_parse_args(argc, argv);
  380. buf = malloc(opt.size);
  381. memrnd(buf, opt.size);
  382. memset(&hints, 0, sizeof(hints));
  383. hints.ai_family = opt.sock.family;
  384. ai = NULL;
  385. err = getaddrinfo(opt.host, opt.service, &hints, &ai);
  386. if (err) {
  387. fprintf(stderr, "Can't resolve address [%s]:%s\n",
  388. opt.host, opt.service);
  389. return ERN_SOCK_CREATE;
  390. }
  391. if (ai->ai_family == AF_INET6 && opt.sock.proto == IPPROTO_ICMP)
  392. opt.sock.proto = IPPROTO_ICMPV6;
  393. fd = socket(ai->ai_family, opt.sock.type, opt.sock.proto);
  394. if (fd < 0) {
  395. fprintf(stderr, "Can't open socket: %s\n", strerror(errno));
  396. freeaddrinfo(ai);
  397. return ERN_RESOLVE;
  398. }
  399. if (opt.sock.proto == IPPROTO_ICMP) {
  400. buf[0] = ICMP_ECHO;
  401. buf[1] = 0;
  402. } else if (opt.sock.proto == IPPROTO_ICMPV6) {
  403. buf[0] = ICMPV6_ECHO_REQUEST;
  404. buf[1] = 0;
  405. } else if (opt.sock.type == SOCK_RAW) {
  406. struct udphdr hdr = { 1, 2, htons(opt.size), 0 };
  407. struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
  408. memcpy(buf, &hdr, sizeof(hdr));
  409. sin6->sin6_port = htons(opt.sock.proto);
  410. }
  411. ca_set_sockopts(fd);
  412. if (clock_gettime(CLOCK_REALTIME, &time_start_real))
  413. error(ERN_GETTIME, errno, "gettime REALTIME");
  414. if (clock_gettime(CLOCK_MONOTONIC, &time_start_mono))
  415. error(ERN_GETTIME, errno, "gettime MONOTONIC");
  416. iov[0].iov_base = buf;
  417. iov[0].iov_len = opt.size;
  418. memset(&msg, 0, sizeof(msg));
  419. msg.msg_name = ai->ai_addr;
  420. msg.msg_namelen = ai->ai_addrlen;
  421. msg.msg_iov = iov;
  422. msg.msg_iovlen = 1;
  423. cs_write_cmsg(fd, &msg, cbuf, sizeof(cbuf));
  424. err = sendmsg(fd, &msg, 0);
  425. if (err < 0) {
  426. if (!opt.silent_send)
  427. fprintf(stderr, "send failed: %s\n", strerror(errno));
  428. err = ERN_SEND;
  429. goto err_out;
  430. } else if (err != (int)opt.size) {
  431. fprintf(stderr, "short send\n");
  432. err = ERN_SEND_SHORT;
  433. goto err_out;
  434. } else {
  435. err = ERN_SUCCESS;
  436. }
  437. /* Make sure all timestamps have time to loop back */
  438. usleep(opt.txtime.delay);
  439. cs_read_cmsg(fd, &msg, cbuf, sizeof(cbuf));
  440. err_out:
  441. close(fd);
  442. freeaddrinfo(ai);
  443. return err;
  444. }