ipv6_flowlabel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Test IPV6_FLOWINFO cmsg on send and recv */
  3. #define _GNU_SOURCE
  4. #include <arpa/inet.h>
  5. #include <asm/byteorder.h>
  6. #include <error.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <linux/icmpv6.h>
  11. #include <linux/in6.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/socket.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. /* uapi/glibc weirdness may leave this undefined */
  23. #ifndef IPV6_FLOWINFO
  24. #define IPV6_FLOWINFO 11
  25. #endif
  26. #ifndef IPV6_FLOWLABEL_MGR
  27. #define IPV6_FLOWLABEL_MGR 32
  28. #endif
  29. #ifndef IPV6_FLOWINFO_SEND
  30. #define IPV6_FLOWINFO_SEND 33
  31. #endif
  32. #define FLOWLABEL_WILDCARD ((uint32_t) -1)
  33. static const char cfg_data[] = "a";
  34. static uint32_t cfg_label = 1;
  35. static bool use_ping;
  36. static bool use_flowinfo_send;
  37. static struct icmp6hdr icmp6 = {
  38. .icmp6_type = ICMPV6_ECHO_REQUEST
  39. };
  40. static struct sockaddr_in6 addr = {
  41. .sin6_family = AF_INET6,
  42. .sin6_addr = IN6ADDR_LOOPBACK_INIT,
  43. };
  44. static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
  45. {
  46. char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
  47. struct msghdr msg = {0};
  48. struct iovec iov = {
  49. .iov_base = (char *)cfg_data,
  50. .iov_len = sizeof(cfg_data)
  51. };
  52. int ret;
  53. if (use_ping) {
  54. iov.iov_base = &icmp6;
  55. iov.iov_len = sizeof(icmp6);
  56. }
  57. msg.msg_iov = &iov;
  58. msg.msg_iovlen = 1;
  59. if (use_flowinfo_send) {
  60. msg.msg_name = &addr;
  61. msg.msg_namelen = sizeof(addr);
  62. } else if (with_flowlabel) {
  63. struct cmsghdr *cm;
  64. cm = (void *)control;
  65. cm->cmsg_len = CMSG_LEN(sizeof(flowlabel));
  66. cm->cmsg_level = SOL_IPV6;
  67. cm->cmsg_type = IPV6_FLOWINFO;
  68. *(uint32_t *)CMSG_DATA(cm) = htonl(flowlabel);
  69. msg.msg_control = control;
  70. msg.msg_controllen = sizeof(control);
  71. }
  72. ret = sendmsg(fd, &msg, 0);
  73. if (ret == -1)
  74. error(1, errno, "send");
  75. if (with_flowlabel)
  76. fprintf(stderr, "sent with label %u\n", flowlabel);
  77. else
  78. fprintf(stderr, "sent without label\n");
  79. }
  80. static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
  81. {
  82. char control[CMSG_SPACE(sizeof(expect))];
  83. char data[sizeof(cfg_data)];
  84. struct msghdr msg = {0};
  85. struct iovec iov = {0};
  86. struct cmsghdr *cm;
  87. uint32_t flowlabel;
  88. int ret;
  89. iov.iov_base = data;
  90. iov.iov_len = sizeof(data);
  91. msg.msg_iov = &iov;
  92. msg.msg_iovlen = 1;
  93. memset(control, 0, sizeof(control));
  94. msg.msg_control = control;
  95. msg.msg_controllen = sizeof(control);
  96. ret = recvmsg(fd, &msg, 0);
  97. if (ret == -1)
  98. error(1, errno, "recv");
  99. if (use_ping)
  100. goto parse_cmsg;
  101. if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
  102. error(1, 0, "recv: truncated");
  103. if (ret != sizeof(cfg_data))
  104. error(1, 0, "recv: length mismatch");
  105. if (memcmp(data, cfg_data, sizeof(data)))
  106. error(1, 0, "recv: data mismatch");
  107. parse_cmsg:
  108. cm = CMSG_FIRSTHDR(&msg);
  109. if (with_flowlabel) {
  110. if (!cm)
  111. error(1, 0, "recv: missing cmsg");
  112. if (CMSG_NXTHDR(&msg, cm))
  113. error(1, 0, "recv: too many cmsg");
  114. if (cm->cmsg_level != SOL_IPV6 ||
  115. cm->cmsg_type != IPV6_FLOWINFO)
  116. error(1, 0, "recv: unexpected cmsg level or type");
  117. flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
  118. fprintf(stderr, "recv with label %u\n", flowlabel);
  119. if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) {
  120. fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
  121. flowlabel, expect);
  122. error(1, 0, "recv: flowlabel is wrong");
  123. }
  124. } else {
  125. fprintf(stderr, "recv without label\n");
  126. }
  127. }
  128. static bool get_autoflowlabel_enabled(void)
  129. {
  130. int fd, ret;
  131. char val;
  132. fd = open("/proc/sys/net/ipv6/auto_flowlabels", O_RDONLY);
  133. if (fd == -1)
  134. error(1, errno, "open sysctl");
  135. ret = read(fd, &val, 1);
  136. if (ret == -1)
  137. error(1, errno, "read sysctl");
  138. if (ret == 0)
  139. error(1, 0, "read sysctl: 0");
  140. if (close(fd))
  141. error(1, errno, "close sysctl");
  142. return val == '1';
  143. }
  144. static void flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
  145. {
  146. struct in6_flowlabel_req req = {
  147. .flr_action = IPV6_FL_A_GET,
  148. .flr_label = htonl(label),
  149. .flr_flags = flags,
  150. .flr_share = share,
  151. };
  152. /* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
  153. req.flr_dst.s6_addr[0] = 0xfd;
  154. req.flr_dst.s6_addr[15] = 0x1;
  155. if (setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req)))
  156. error(1, errno, "setsockopt flowlabel get");
  157. }
  158. static void parse_opts(int argc, char **argv)
  159. {
  160. int c;
  161. while ((c = getopt(argc, argv, "l:ps")) != -1) {
  162. switch (c) {
  163. case 'l':
  164. cfg_label = strtoul(optarg, NULL, 0);
  165. break;
  166. case 'p':
  167. use_ping = true;
  168. break;
  169. case 's':
  170. use_flowinfo_send = true;
  171. break;
  172. default:
  173. error(1, 0, "%s: parse error", argv[0]);
  174. }
  175. }
  176. }
  177. int main(int argc, char **argv)
  178. {
  179. const int one = 1;
  180. int fdt, fdr;
  181. int prot = 0;
  182. addr.sin6_port = htons(8000);
  183. parse_opts(argc, argv);
  184. if (use_ping) {
  185. fprintf(stderr, "attempting to use ping sockets\n");
  186. prot = IPPROTO_ICMPV6;
  187. }
  188. fdt = socket(PF_INET6, SOCK_DGRAM, prot);
  189. if (fdt == -1)
  190. error(1, errno, "socket t");
  191. fdr = use_ping ? fdt : socket(PF_INET6, SOCK_DGRAM, 0);
  192. if (fdr == -1)
  193. error(1, errno, "socket r");
  194. if (connect(fdt, (void *)&addr, sizeof(addr)))
  195. error(1, errno, "connect");
  196. if (!use_ping && bind(fdr, (void *)&addr, sizeof(addr)))
  197. error(1, errno, "bind");
  198. flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
  199. if (setsockopt(fdr, SOL_IPV6, IPV6_FLOWINFO, &one, sizeof(one)))
  200. error(1, errno, "setsockopt flowinfo");
  201. if (get_autoflowlabel_enabled()) {
  202. fprintf(stderr, "send no label: recv auto flowlabel\n");
  203. do_send(fdt, false, 0);
  204. do_recv(fdr, true, FLOWLABEL_WILDCARD);
  205. } else {
  206. fprintf(stderr, "send no label: recv no label (auto off)\n");
  207. do_send(fdt, false, 0);
  208. do_recv(fdr, false, 0);
  209. }
  210. if (use_flowinfo_send) {
  211. fprintf(stderr, "using IPV6_FLOWINFO_SEND to send label\n");
  212. addr.sin6_flowinfo = htonl(cfg_label);
  213. if (setsockopt(fdt, SOL_IPV6, IPV6_FLOWINFO_SEND, &one,
  214. sizeof(one)) == -1)
  215. error(1, errno, "setsockopt flowinfo_send");
  216. }
  217. fprintf(stderr, "send label\n");
  218. do_send(fdt, true, cfg_label);
  219. do_recv(fdr, true, cfg_label);
  220. if (close(fdr))
  221. error(1, errno, "close r");
  222. if (!use_ping && close(fdt))
  223. error(1, errno, "close t");
  224. return 0;
  225. }