0001
0002
0003
0004 #define _GNU_SOURCE
0005
0006 #include <arpa/inet.h>
0007 #include <asm/byteorder.h>
0008 #include <error.h>
0009 #include <errno.h>
0010 #include <fcntl.h>
0011 #include <limits.h>
0012 #include <linux/icmpv6.h>
0013 #include <linux/in6.h>
0014 #include <stdbool.h>
0015 #include <stdio.h>
0016 #include <stdint.h>
0017 #include <stdlib.h>
0018 #include <string.h>
0019 #include <sys/socket.h>
0020 #include <sys/stat.h>
0021 #include <sys/time.h>
0022 #include <sys/types.h>
0023 #include <unistd.h>
0024
0025
0026 #ifndef IPV6_FLOWINFO
0027 #define IPV6_FLOWINFO 11
0028 #endif
0029
0030 #ifndef IPV6_FLOWLABEL_MGR
0031 #define IPV6_FLOWLABEL_MGR 32
0032 #endif
0033 #ifndef IPV6_FLOWINFO_SEND
0034 #define IPV6_FLOWINFO_SEND 33
0035 #endif
0036
0037 #define FLOWLABEL_WILDCARD ((uint32_t) -1)
0038
0039 static const char cfg_data[] = "a";
0040 static uint32_t cfg_label = 1;
0041 static bool use_ping;
0042 static bool use_flowinfo_send;
0043
0044 static struct icmp6hdr icmp6 = {
0045 .icmp6_type = ICMPV6_ECHO_REQUEST
0046 };
0047
0048 static struct sockaddr_in6 addr = {
0049 .sin6_family = AF_INET6,
0050 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
0051 };
0052
0053 static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
0054 {
0055 char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
0056 struct msghdr msg = {0};
0057 struct iovec iov = {
0058 .iov_base = (char *)cfg_data,
0059 .iov_len = sizeof(cfg_data)
0060 };
0061 int ret;
0062
0063 if (use_ping) {
0064 iov.iov_base = &icmp6;
0065 iov.iov_len = sizeof(icmp6);
0066 }
0067
0068 msg.msg_iov = &iov;
0069 msg.msg_iovlen = 1;
0070
0071 if (use_flowinfo_send) {
0072 msg.msg_name = &addr;
0073 msg.msg_namelen = sizeof(addr);
0074 } else if (with_flowlabel) {
0075 struct cmsghdr *cm;
0076
0077 cm = (void *)control;
0078 cm->cmsg_len = CMSG_LEN(sizeof(flowlabel));
0079 cm->cmsg_level = SOL_IPV6;
0080 cm->cmsg_type = IPV6_FLOWINFO;
0081 *(uint32_t *)CMSG_DATA(cm) = htonl(flowlabel);
0082
0083 msg.msg_control = control;
0084 msg.msg_controllen = sizeof(control);
0085 }
0086
0087 ret = sendmsg(fd, &msg, 0);
0088 if (ret == -1)
0089 error(1, errno, "send");
0090
0091 if (with_flowlabel)
0092 fprintf(stderr, "sent with label %u\n", flowlabel);
0093 else
0094 fprintf(stderr, "sent without label\n");
0095 }
0096
0097 static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
0098 {
0099 char control[CMSG_SPACE(sizeof(expect))];
0100 char data[sizeof(cfg_data)];
0101 struct msghdr msg = {0};
0102 struct iovec iov = {0};
0103 struct cmsghdr *cm;
0104 uint32_t flowlabel;
0105 int ret;
0106
0107 iov.iov_base = data;
0108 iov.iov_len = sizeof(data);
0109
0110 msg.msg_iov = &iov;
0111 msg.msg_iovlen = 1;
0112
0113 memset(control, 0, sizeof(control));
0114 msg.msg_control = control;
0115 msg.msg_controllen = sizeof(control);
0116
0117 ret = recvmsg(fd, &msg, 0);
0118 if (ret == -1)
0119 error(1, errno, "recv");
0120 if (use_ping)
0121 goto parse_cmsg;
0122 if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
0123 error(1, 0, "recv: truncated");
0124 if (ret != sizeof(cfg_data))
0125 error(1, 0, "recv: length mismatch");
0126 if (memcmp(data, cfg_data, sizeof(data)))
0127 error(1, 0, "recv: data mismatch");
0128
0129 parse_cmsg:
0130 cm = CMSG_FIRSTHDR(&msg);
0131 if (with_flowlabel) {
0132 if (!cm)
0133 error(1, 0, "recv: missing cmsg");
0134 if (CMSG_NXTHDR(&msg, cm))
0135 error(1, 0, "recv: too many cmsg");
0136 if (cm->cmsg_level != SOL_IPV6 ||
0137 cm->cmsg_type != IPV6_FLOWINFO)
0138 error(1, 0, "recv: unexpected cmsg level or type");
0139
0140 flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
0141 fprintf(stderr, "recv with label %u\n", flowlabel);
0142
0143 if (expect != FLOWLABEL_WILDCARD && expect != flowlabel) {
0144 fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
0145 flowlabel, expect);
0146 error(1, 0, "recv: flowlabel is wrong");
0147 }
0148
0149 } else {
0150 fprintf(stderr, "recv without label\n");
0151 }
0152 }
0153
0154 static bool get_autoflowlabel_enabled(void)
0155 {
0156 int fd, ret;
0157 char val;
0158
0159 fd = open("/proc/sys/net/ipv6/auto_flowlabels", O_RDONLY);
0160 if (fd == -1)
0161 error(1, errno, "open sysctl");
0162
0163 ret = read(fd, &val, 1);
0164 if (ret == -1)
0165 error(1, errno, "read sysctl");
0166 if (ret == 0)
0167 error(1, 0, "read sysctl: 0");
0168
0169 if (close(fd))
0170 error(1, errno, "close sysctl");
0171
0172 return val == '1';
0173 }
0174
0175 static void flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
0176 {
0177 struct in6_flowlabel_req req = {
0178 .flr_action = IPV6_FL_A_GET,
0179 .flr_label = htonl(label),
0180 .flr_flags = flags,
0181 .flr_share = share,
0182 };
0183
0184
0185 req.flr_dst.s6_addr[0] = 0xfd;
0186 req.flr_dst.s6_addr[15] = 0x1;
0187
0188 if (setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req)))
0189 error(1, errno, "setsockopt flowlabel get");
0190 }
0191
0192 static void parse_opts(int argc, char **argv)
0193 {
0194 int c;
0195
0196 while ((c = getopt(argc, argv, "l:ps")) != -1) {
0197 switch (c) {
0198 case 'l':
0199 cfg_label = strtoul(optarg, NULL, 0);
0200 break;
0201 case 'p':
0202 use_ping = true;
0203 break;
0204 case 's':
0205 use_flowinfo_send = true;
0206 break;
0207 default:
0208 error(1, 0, "%s: parse error", argv[0]);
0209 }
0210 }
0211 }
0212
0213 int main(int argc, char **argv)
0214 {
0215 const int one = 1;
0216 int fdt, fdr;
0217 int prot = 0;
0218
0219 addr.sin6_port = htons(8000);
0220
0221 parse_opts(argc, argv);
0222
0223 if (use_ping) {
0224 fprintf(stderr, "attempting to use ping sockets\n");
0225 prot = IPPROTO_ICMPV6;
0226 }
0227
0228 fdt = socket(PF_INET6, SOCK_DGRAM, prot);
0229 if (fdt == -1)
0230 error(1, errno, "socket t");
0231
0232 fdr = use_ping ? fdt : socket(PF_INET6, SOCK_DGRAM, 0);
0233 if (fdr == -1)
0234 error(1, errno, "socket r");
0235
0236 if (connect(fdt, (void *)&addr, sizeof(addr)))
0237 error(1, errno, "connect");
0238 if (!use_ping && bind(fdr, (void *)&addr, sizeof(addr)))
0239 error(1, errno, "bind");
0240
0241 flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);
0242
0243 if (setsockopt(fdr, SOL_IPV6, IPV6_FLOWINFO, &one, sizeof(one)))
0244 error(1, errno, "setsockopt flowinfo");
0245
0246 if (get_autoflowlabel_enabled()) {
0247 fprintf(stderr, "send no label: recv auto flowlabel\n");
0248 do_send(fdt, false, 0);
0249 do_recv(fdr, true, FLOWLABEL_WILDCARD);
0250 } else {
0251 fprintf(stderr, "send no label: recv no label (auto off)\n");
0252 do_send(fdt, false, 0);
0253 do_recv(fdr, false, 0);
0254 }
0255
0256 if (use_flowinfo_send) {
0257 fprintf(stderr, "using IPV6_FLOWINFO_SEND to send label\n");
0258 addr.sin6_flowinfo = htonl(cfg_label);
0259 if (setsockopt(fdt, SOL_IPV6, IPV6_FLOWINFO_SEND, &one,
0260 sizeof(one)) == -1)
0261 error(1, errno, "setsockopt flowinfo_send");
0262 }
0263
0264 fprintf(stderr, "send label\n");
0265 do_send(fdt, true, cfg_label);
0266 do_recv(fdr, true, cfg_label);
0267
0268 if (close(fdr))
0269 error(1, errno, "close r");
0270 if (!use_ping && close(fdt))
0271 error(1, errno, "close t");
0272
0273 return 0;
0274 }