udpgso.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <stddef.h>
  4. #include <arpa/inet.h>
  5. #include <error.h>
  6. #include <errno.h>
  7. #include <net/if.h>
  8. #include <linux/in.h>
  9. #include <linux/netlink.h>
  10. #include <linux/rtnetlink.h>
  11. #include <netinet/if_ether.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/ip6.h>
  14. #include <netinet/udp.h>
  15. #include <stdbool.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #ifndef ETH_MAX_MTU
  27. #define ETH_MAX_MTU 0xFFFFU
  28. #endif
  29. #ifndef UDP_SEGMENT
  30. #define UDP_SEGMENT 103
  31. #endif
  32. #ifndef UDP_MAX_SEGMENTS
  33. #define UDP_MAX_SEGMENTS (1 << 6UL)
  34. #endif
  35. #define CONST_MTU_TEST 1500
  36. #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
  37. #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
  38. #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
  39. #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
  40. #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
  41. #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
  42. static bool cfg_do_ipv4;
  43. static bool cfg_do_ipv6;
  44. static bool cfg_do_connected;
  45. static bool cfg_do_connectionless;
  46. static bool cfg_do_msgmore;
  47. static bool cfg_do_setsockopt;
  48. static int cfg_specific_test_id = -1;
  49. static const char cfg_ifname[] = "lo";
  50. static unsigned short cfg_port = 9000;
  51. static char buf[ETH_MAX_MTU];
  52. struct testcase {
  53. int tlen; /* send() buffer size, may exceed mss */
  54. bool tfail; /* send() call is expected to fail */
  55. int gso_len; /* mss after applying gso */
  56. int r_num_mss; /* recv(): number of calls of full mss */
  57. int r_len_last; /* recv(): size of last non-mss dgram, if any */
  58. };
  59. const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
  60. const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
  61. struct testcase testcases_v4[] = {
  62. {
  63. /* no GSO: send a single byte */
  64. .tlen = 1,
  65. .r_len_last = 1,
  66. },
  67. {
  68. /* no GSO: send a single MSS */
  69. .tlen = CONST_MSS_V4,
  70. .r_num_mss = 1,
  71. },
  72. {
  73. /* no GSO: send a single MSS + 1B: fail */
  74. .tlen = CONST_MSS_V4 + 1,
  75. .tfail = true,
  76. },
  77. {
  78. /* send a single MSS: will fail with GSO, because the segment
  79. * logic in udp4_ufo_fragment demands a gso skb to be > MTU
  80. */
  81. .tlen = CONST_MSS_V4,
  82. .gso_len = CONST_MSS_V4,
  83. .tfail = true,
  84. .r_num_mss = 1,
  85. },
  86. {
  87. /* send a single MSS + 1B */
  88. .tlen = CONST_MSS_V4 + 1,
  89. .gso_len = CONST_MSS_V4,
  90. .r_num_mss = 1,
  91. .r_len_last = 1,
  92. },
  93. {
  94. /* send exactly 2 MSS */
  95. .tlen = CONST_MSS_V4 * 2,
  96. .gso_len = CONST_MSS_V4,
  97. .r_num_mss = 2,
  98. },
  99. {
  100. /* send 2 MSS + 1B */
  101. .tlen = (CONST_MSS_V4 * 2) + 1,
  102. .gso_len = CONST_MSS_V4,
  103. .r_num_mss = 2,
  104. .r_len_last = 1,
  105. },
  106. {
  107. /* send MAX segs */
  108. .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
  109. .gso_len = CONST_MSS_V4,
  110. .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
  111. },
  112. {
  113. /* send MAX bytes */
  114. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
  115. .gso_len = CONST_MSS_V4,
  116. .r_num_mss = CONST_MAX_SEGS_V4,
  117. .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
  118. (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
  119. },
  120. {
  121. /* send MAX + 1: fail */
  122. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
  123. .gso_len = CONST_MSS_V4,
  124. .tfail = true,
  125. },
  126. {
  127. /* send a single 1B MSS: will fail, see single MSS above */
  128. .tlen = 1,
  129. .gso_len = 1,
  130. .tfail = true,
  131. .r_num_mss = 1,
  132. },
  133. {
  134. /* send 2 1B segments */
  135. .tlen = 2,
  136. .gso_len = 1,
  137. .r_num_mss = 2,
  138. },
  139. {
  140. /* send 2B + 2B + 1B segments */
  141. .tlen = 5,
  142. .gso_len = 2,
  143. .r_num_mss = 2,
  144. .r_len_last = 1,
  145. },
  146. {
  147. /* send max number of min sized segments */
  148. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
  149. .gso_len = 1,
  150. .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
  151. },
  152. {
  153. /* send max number + 1 of min sized segments: fail */
  154. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4 + 1,
  155. .gso_len = 1,
  156. .tfail = true,
  157. },
  158. {
  159. /* EOL */
  160. }
  161. };
  162. #ifndef IP6_MAX_MTU
  163. #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
  164. #endif
  165. struct testcase testcases_v6[] = {
  166. {
  167. /* no GSO: send a single byte */
  168. .tlen = 1,
  169. .r_len_last = 1,
  170. },
  171. {
  172. /* no GSO: send a single MSS */
  173. .tlen = CONST_MSS_V6,
  174. .r_num_mss = 1,
  175. },
  176. {
  177. /* no GSO: send a single MSS + 1B: fail */
  178. .tlen = CONST_MSS_V6 + 1,
  179. .tfail = true,
  180. },
  181. {
  182. /* send a single MSS: will fail with GSO, because the segment
  183. * logic in udp4_ufo_fragment demands a gso skb to be > MTU
  184. */
  185. .tlen = CONST_MSS_V6,
  186. .gso_len = CONST_MSS_V6,
  187. .tfail = true,
  188. .r_num_mss = 1,
  189. },
  190. {
  191. /* send a single MSS + 1B */
  192. .tlen = CONST_MSS_V6 + 1,
  193. .gso_len = CONST_MSS_V6,
  194. .r_num_mss = 1,
  195. .r_len_last = 1,
  196. },
  197. {
  198. /* send exactly 2 MSS */
  199. .tlen = CONST_MSS_V6 * 2,
  200. .gso_len = CONST_MSS_V6,
  201. .r_num_mss = 2,
  202. },
  203. {
  204. /* send 2 MSS + 1B */
  205. .tlen = (CONST_MSS_V6 * 2) + 1,
  206. .gso_len = CONST_MSS_V6,
  207. .r_num_mss = 2,
  208. .r_len_last = 1,
  209. },
  210. {
  211. /* send MAX segs */
  212. .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
  213. .gso_len = CONST_MSS_V6,
  214. .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
  215. },
  216. {
  217. /* send MAX bytes */
  218. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
  219. .gso_len = CONST_MSS_V6,
  220. .r_num_mss = CONST_MAX_SEGS_V6,
  221. .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
  222. (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
  223. },
  224. {
  225. /* send MAX + 1: fail */
  226. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
  227. .gso_len = CONST_MSS_V6,
  228. .tfail = true,
  229. },
  230. {
  231. /* send a single 1B MSS: will fail, see single MSS above */
  232. .tlen = 1,
  233. .gso_len = 1,
  234. .tfail = true,
  235. .r_num_mss = 1,
  236. },
  237. {
  238. /* send 2 1B segments */
  239. .tlen = 2,
  240. .gso_len = 1,
  241. .r_num_mss = 2,
  242. },
  243. {
  244. /* send 2B + 2B + 1B segments */
  245. .tlen = 5,
  246. .gso_len = 2,
  247. .r_num_mss = 2,
  248. .r_len_last = 1,
  249. },
  250. {
  251. /* send max number of min sized segments */
  252. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
  253. .gso_len = 1,
  254. .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
  255. },
  256. {
  257. /* send max number + 1 of min sized segments: fail */
  258. .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6 + 1,
  259. .gso_len = 1,
  260. .tfail = true,
  261. },
  262. {
  263. /* EOL */
  264. }
  265. };
  266. static unsigned int get_device_mtu(int fd, const char *ifname)
  267. {
  268. struct ifreq ifr;
  269. memset(&ifr, 0, sizeof(ifr));
  270. strcpy(ifr.ifr_name, ifname);
  271. if (ioctl(fd, SIOCGIFMTU, &ifr))
  272. error(1, errno, "ioctl get mtu");
  273. return ifr.ifr_mtu;
  274. }
  275. static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
  276. {
  277. struct ifreq ifr;
  278. memset(&ifr, 0, sizeof(ifr));
  279. ifr.ifr_mtu = mtu;
  280. strcpy(ifr.ifr_name, ifname);
  281. if (ioctl(fd, SIOCSIFMTU, &ifr))
  282. error(1, errno, "ioctl set mtu");
  283. }
  284. static void set_device_mtu(int fd, int mtu)
  285. {
  286. int val;
  287. val = get_device_mtu(fd, cfg_ifname);
  288. fprintf(stderr, "device mtu (orig): %u\n", val);
  289. __set_device_mtu(fd, cfg_ifname, mtu);
  290. val = get_device_mtu(fd, cfg_ifname);
  291. if (val != mtu)
  292. error(1, 0, "unable to set device mtu to %u\n", val);
  293. fprintf(stderr, "device mtu (test): %u\n", val);
  294. }
  295. static void set_pmtu_discover(int fd, bool is_ipv4)
  296. {
  297. int level, name, val;
  298. if (is_ipv4) {
  299. level = SOL_IP;
  300. name = IP_MTU_DISCOVER;
  301. val = IP_PMTUDISC_DO;
  302. } else {
  303. level = SOL_IPV6;
  304. name = IPV6_MTU_DISCOVER;
  305. val = IPV6_PMTUDISC_DO;
  306. }
  307. if (setsockopt(fd, level, name, &val, sizeof(val)))
  308. error(1, errno, "setsockopt path mtu");
  309. }
  310. static unsigned int get_path_mtu(int fd, bool is_ipv4)
  311. {
  312. socklen_t vallen;
  313. unsigned int mtu;
  314. int ret;
  315. vallen = sizeof(mtu);
  316. if (is_ipv4)
  317. ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
  318. else
  319. ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
  320. if (ret)
  321. error(1, errno, "getsockopt mtu");
  322. fprintf(stderr, "path mtu (read): %u\n", mtu);
  323. return mtu;
  324. }
  325. /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
  326. static void set_route_mtu(int mtu, bool is_ipv4)
  327. {
  328. struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
  329. struct nlmsghdr *nh;
  330. struct rtattr *rta;
  331. struct rtmsg *rt;
  332. char data[NLMSG_ALIGN(sizeof(*nh)) +
  333. NLMSG_ALIGN(sizeof(*rt)) +
  334. NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
  335. NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
  336. NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
  337. int fd, ret, alen, off = 0;
  338. alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
  339. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  340. if (fd == -1)
  341. error(1, errno, "socket netlink");
  342. memset(data, 0, sizeof(data));
  343. nh = (void *)data;
  344. nh->nlmsg_type = RTM_NEWROUTE;
  345. nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
  346. off += NLMSG_ALIGN(sizeof(*nh));
  347. rt = (void *)(data + off);
  348. rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
  349. rt->rtm_table = RT_TABLE_MAIN;
  350. rt->rtm_dst_len = alen << 3;
  351. rt->rtm_protocol = RTPROT_BOOT;
  352. rt->rtm_scope = RT_SCOPE_UNIVERSE;
  353. rt->rtm_type = RTN_UNICAST;
  354. off += NLMSG_ALIGN(sizeof(*rt));
  355. rta = (void *)(data + off);
  356. rta->rta_type = RTA_DST;
  357. rta->rta_len = RTA_LENGTH(alen);
  358. if (is_ipv4)
  359. memcpy(RTA_DATA(rta), &addr4, alen);
  360. else
  361. memcpy(RTA_DATA(rta), &addr6, alen);
  362. off += NLMSG_ALIGN(rta->rta_len);
  363. rta = (void *)(data + off);
  364. rta->rta_type = RTA_OIF;
  365. rta->rta_len = RTA_LENGTH(sizeof(int));
  366. *((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
  367. off += NLMSG_ALIGN(rta->rta_len);
  368. /* MTU is a subtype in a metrics type */
  369. rta = (void *)(data + off);
  370. rta->rta_type = RTA_METRICS;
  371. rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
  372. off += NLMSG_ALIGN(rta->rta_len);
  373. /* now fill MTU subtype. Note that it fits within above rta_len */
  374. rta = (void *)(((char *) rta) + RTA_LENGTH(0));
  375. rta->rta_type = RTAX_MTU;
  376. rta->rta_len = RTA_LENGTH(sizeof(int));
  377. *((int *)(RTA_DATA(rta))) = mtu;
  378. nh->nlmsg_len = off;
  379. ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
  380. if (ret != off)
  381. error(1, errno, "send netlink: %uB != %uB\n", ret, off);
  382. if (close(fd))
  383. error(1, errno, "close netlink");
  384. fprintf(stderr, "route mtu (test): %u\n", mtu);
  385. }
  386. static bool __send_one(int fd, struct msghdr *msg, int flags)
  387. {
  388. int ret;
  389. ret = sendmsg(fd, msg, flags);
  390. if (ret == -1 &&
  391. (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
  392. return false;
  393. if (ret == -1)
  394. error(1, errno, "sendmsg");
  395. if (ret != msg->msg_iov->iov_len)
  396. error(1, 0, "sendto: %d != %lu", ret, msg->msg_iov->iov_len);
  397. if (msg->msg_flags)
  398. error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
  399. return true;
  400. }
  401. static bool send_one(int fd, int len, int gso_len,
  402. struct sockaddr *addr, socklen_t alen)
  403. {
  404. char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
  405. struct msghdr msg = {0};
  406. struct iovec iov = {0};
  407. struct cmsghdr *cm;
  408. iov.iov_base = buf;
  409. iov.iov_len = len;
  410. msg.msg_iov = &iov;
  411. msg.msg_iovlen = 1;
  412. msg.msg_name = addr;
  413. msg.msg_namelen = alen;
  414. if (gso_len && !cfg_do_setsockopt) {
  415. msg.msg_control = control;
  416. msg.msg_controllen = sizeof(control);
  417. cm = CMSG_FIRSTHDR(&msg);
  418. cm->cmsg_level = SOL_UDP;
  419. cm->cmsg_type = UDP_SEGMENT;
  420. cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
  421. *((uint16_t *) CMSG_DATA(cm)) = gso_len;
  422. }
  423. /* If MSG_MORE, send 1 byte followed by remainder */
  424. if (cfg_do_msgmore && len > 1) {
  425. iov.iov_len = 1;
  426. if (!__send_one(fd, &msg, MSG_MORE))
  427. error(1, 0, "send 1B failed");
  428. iov.iov_base++;
  429. iov.iov_len = len - 1;
  430. }
  431. return __send_one(fd, &msg, 0);
  432. }
  433. static int recv_one(int fd, int flags)
  434. {
  435. int ret;
  436. ret = recv(fd, buf, sizeof(buf), flags);
  437. if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
  438. return 0;
  439. if (ret == -1)
  440. error(1, errno, "recv");
  441. return ret;
  442. }
  443. static void run_one(struct testcase *test, int fdt, int fdr,
  444. struct sockaddr *addr, socklen_t alen)
  445. {
  446. int i, ret, val, mss;
  447. bool sent;
  448. fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
  449. addr->sa_family == AF_INET ? 4 : 6,
  450. test->tlen, test->gso_len,
  451. test->tfail ? "(fail)" : "");
  452. val = test->gso_len;
  453. if (cfg_do_setsockopt) {
  454. if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
  455. error(1, errno, "setsockopt udp segment");
  456. }
  457. sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
  458. if (sent && test->tfail)
  459. error(1, 0, "send succeeded while expecting failure");
  460. if (!sent && !test->tfail)
  461. error(1, 0, "send failed while expecting success");
  462. if (!sent)
  463. return;
  464. if (test->gso_len)
  465. mss = test->gso_len;
  466. else
  467. mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
  468. /* Recv all full MSS datagrams */
  469. for (i = 0; i < test->r_num_mss; i++) {
  470. ret = recv_one(fdr, 0);
  471. if (ret != mss)
  472. error(1, 0, "recv.%d: %d != %d", i, ret, mss);
  473. }
  474. /* Recv the non-full last datagram, if tlen was not a multiple of mss */
  475. if (test->r_len_last) {
  476. ret = recv_one(fdr, 0);
  477. if (ret != test->r_len_last)
  478. error(1, 0, "recv.%d: %d != %d (last)",
  479. i, ret, test->r_len_last);
  480. }
  481. /* Verify received all data */
  482. ret = recv_one(fdr, MSG_DONTWAIT);
  483. if (ret)
  484. error(1, 0, "recv: unexpected datagram");
  485. }
  486. static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
  487. {
  488. struct testcase *tests, *test;
  489. tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
  490. for (test = tests; test->tlen; test++) {
  491. /* if a specific test is given, then skip all others */
  492. if (cfg_specific_test_id == -1 ||
  493. cfg_specific_test_id == test - tests)
  494. run_one(test, fdt, fdr, addr, alen);
  495. }
  496. }
  497. static void run_test(struct sockaddr *addr, socklen_t alen)
  498. {
  499. struct timeval tv = { .tv_usec = 100 * 1000 };
  500. int fdr, fdt, val;
  501. fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
  502. if (fdr == -1)
  503. error(1, errno, "socket r");
  504. if (bind(fdr, addr, alen))
  505. error(1, errno, "bind");
  506. /* Have tests fail quickly instead of hang */
  507. if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
  508. error(1, errno, "setsockopt rcv timeout");
  509. fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
  510. if (fdt == -1)
  511. error(1, errno, "socket t");
  512. /* Do not fragment these datagrams: only succeed if GSO works */
  513. set_pmtu_discover(fdt, addr->sa_family == AF_INET);
  514. if (cfg_do_connectionless) {
  515. set_device_mtu(fdt, CONST_MTU_TEST);
  516. run_all(fdt, fdr, addr, alen);
  517. }
  518. if (cfg_do_connected) {
  519. set_device_mtu(fdt, CONST_MTU_TEST + 100);
  520. set_route_mtu(CONST_MTU_TEST, addr->sa_family == AF_INET);
  521. if (connect(fdt, addr, alen))
  522. error(1, errno, "connect");
  523. val = get_path_mtu(fdt, addr->sa_family == AF_INET);
  524. if (val != CONST_MTU_TEST)
  525. error(1, 0, "bad path mtu %u\n", val);
  526. run_all(fdt, fdr, addr, 0 /* use connected addr */);
  527. }
  528. if (close(fdt))
  529. error(1, errno, "close t");
  530. if (close(fdr))
  531. error(1, errno, "close r");
  532. }
  533. static void run_test_v4(void)
  534. {
  535. struct sockaddr_in addr = {0};
  536. addr.sin_family = AF_INET;
  537. addr.sin_port = htons(cfg_port);
  538. addr.sin_addr = addr4;
  539. run_test((void *)&addr, sizeof(addr));
  540. }
  541. static void run_test_v6(void)
  542. {
  543. struct sockaddr_in6 addr = {0};
  544. addr.sin6_family = AF_INET6;
  545. addr.sin6_port = htons(cfg_port);
  546. addr.sin6_addr = addr6;
  547. run_test((void *)&addr, sizeof(addr));
  548. }
  549. static void parse_opts(int argc, char **argv)
  550. {
  551. int c;
  552. while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
  553. switch (c) {
  554. case '4':
  555. cfg_do_ipv4 = true;
  556. break;
  557. case '6':
  558. cfg_do_ipv6 = true;
  559. break;
  560. case 'c':
  561. cfg_do_connected = true;
  562. break;
  563. case 'C':
  564. cfg_do_connectionless = true;
  565. break;
  566. case 'm':
  567. cfg_do_msgmore = true;
  568. break;
  569. case 's':
  570. cfg_do_setsockopt = true;
  571. break;
  572. case 't':
  573. cfg_specific_test_id = strtoul(optarg, NULL, 0);
  574. break;
  575. default:
  576. error(1, 0, "%s: parse error", argv[0]);
  577. }
  578. }
  579. }
  580. int main(int argc, char **argv)
  581. {
  582. parse_opts(argc, argv);
  583. if (cfg_do_ipv4)
  584. run_test_v4();
  585. if (cfg_do_ipv6)
  586. run_test_v6();
  587. fprintf(stderr, "OK\n");
  588. return 0;
  589. }