1 // SPDX-License-Identifier: GPL-2.0
2 /* Test IPV6_FLOWINFO cmsg on send and recv */
7 #include <asm/byteorder.h>
12 #include <linux/in6.h>
18 #include <sys/socket.h>
21 #include <sys/types.h>
24 /* uapi/glibc weirdness may leave this undefined */
26 #define IPV6_FLOWINFO 11
29 #ifndef IPV6_FLOWLABEL_MGR
30 #define IPV6_FLOWLABEL_MGR 32
33 #define FLOWLABEL_WILDCARD ((uint32_t) -1)
35 static const char cfg_data[] = "a";
36 static uint32_t cfg_label = 1;
38 static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
40 char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
41 struct msghdr msg = {0};
42 struct iovec iov = {0};
45 iov.iov_base = (char *)cfg_data;
46 iov.iov_len = sizeof(cfg_data);
55 cm->cmsg_len = CMSG_LEN(sizeof(flowlabel));
56 cm->cmsg_level = SOL_IPV6;
57 cm->cmsg_type = IPV6_FLOWINFO;
58 *(uint32_t *)CMSG_DATA(cm) = htonl(flowlabel);
60 msg.msg_control = control;
61 msg.msg_controllen = sizeof(control);
64 ret = sendmsg(fd, &msg, 0);
66 error(1, errno, "send");
69 fprintf(stderr, "sent with label %u\n", flowlabel);
71 fprintf(stderr, "sent without label\n");
74 static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
76 char control[CMSG_SPACE(sizeof(expect))];
77 char data[sizeof(cfg_data)];
78 struct msghdr msg = {0};
79 struct iovec iov = {0};
85 iov.iov_len = sizeof(data);
90 memset(control, 0, sizeof(control));
91 msg.msg_control = control;
92 msg.msg_controllen = sizeof(control);
94 ret = recvmsg(fd, &msg, 0);
96 error(1, errno, "recv");
97 if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
98 error(1, 0, "recv: truncated");
99 if (ret != sizeof(cfg_data))
100 error(1, 0, "recv: length mismatch");
101 if (memcmp(data, cfg_data, sizeof(data)))
102 error(1, 0, "recv: data mismatch");
104 cm = CMSG_FIRSTHDR(&msg);
105 if (with_flowlabel) {
107 error(1, 0, "recv: missing cmsg");
108 if (CMSG_NXTHDR(&msg, cm))
109 error(1, 0, "recv: too many cmsg");
110 if (cm->cmsg_level != SOL_IPV6 ||
111 cm->cmsg_type != IPV6_FLOWINFO)
112 error(1, 0, "recv: unexpected cmsg level or type");
114 flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
115 fprintf(stderr, "recv with label %u\n", flowlabel);
117 if (expect != FLOWLABEL_WILDCARD && expect != flowlabel)
118 fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
122 fprintf(stderr, "recv without label\n");
126 static bool get_autoflowlabel_enabled(void)
131 fd = open("/proc/sys/net/ipv6/auto_flowlabels", O_RDONLY);
133 error(1, errno, "open sysctl");
135 ret = read(fd, &val, 1);
137 error(1, errno, "read sysctl");
139 error(1, 0, "read sysctl: 0");
142 error(1, errno, "close sysctl");
147 static void flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
149 struct in6_flowlabel_req req = {
150 .flr_action = IPV6_FL_A_GET,
151 .flr_label = htonl(label),
156 /* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
157 req.flr_dst.s6_addr[0] = 0xfd;
158 req.flr_dst.s6_addr[15] = 0x1;
160 if (setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req)))
161 error(1, errno, "setsockopt flowlabel get");
164 static void parse_opts(int argc, char **argv)
168 while ((c = getopt(argc, argv, "l:")) != -1) {
171 cfg_label = strtoul(optarg, NULL, 0);
174 error(1, 0, "%s: parse error", argv[0]);
179 int main(int argc, char **argv)
181 struct sockaddr_in6 addr = {
182 .sin6_family = AF_INET6,
183 .sin6_port = htons(8000),
184 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
189 parse_opts(argc, argv);
191 fdt = socket(PF_INET6, SOCK_DGRAM, 0);
193 error(1, errno, "socket t");
195 fdr = socket(PF_INET6, SOCK_DGRAM, 0);
197 error(1, errno, "socket r");
199 if (connect(fdt, (void *)&addr, sizeof(addr)))
200 error(1, errno, "connect");
201 if (bind(fdr, (void *)&addr, sizeof(addr)))
202 error(1, errno, "bind");
204 flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
206 if (setsockopt(fdr, SOL_IPV6, IPV6_FLOWINFO, &one, sizeof(one)))
207 error(1, errno, "setsockopt flowinfo");
209 if (get_autoflowlabel_enabled()) {
210 fprintf(stderr, "send no label: recv auto flowlabel\n");
211 do_send(fdt, false, 0);
212 do_recv(fdr, true, FLOWLABEL_WILDCARD);
214 fprintf(stderr, "send no label: recv no label (auto off)\n");
215 do_send(fdt, false, 0);
216 do_recv(fdr, false, 0);
219 fprintf(stderr, "send label\n");
220 do_send(fdt, true, cfg_label);
221 do_recv(fdr, true, cfg_label);
224 error(1, errno, "close r");
226 error(1, errno, "close t");