summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/ipv6_flowlabel.c
blob: a7c41375374f2d0847c9ad2b0fe3d65e226a5072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// SPDX-License-Identifier: GPL-2.0
/* Test IPV6_FLOWINFO cmsg on send and recv */

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <asm/byteorder.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/in6.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/* uapi/glibc weirdness may leave this undefined */
#ifndef IPV6_FLOWINFO
#define IPV6_FLOWINFO 11
#endif

#ifndef IPV6_FLOWLABEL_MGR
#define IPV6_FLOWLABEL_MGR 32
#endif

#define FLOWLABEL_WILDCARD	((uint32_t) -1)

static const char cfg_data[]	= "a";
static uint32_t cfg_label	= 1;

static void do_send(int fd, bool with_flowlabel, uint32_t flowlabel)
{
	char control[CMSG_SPACE(sizeof(flowlabel))] = {0};
	struct msghdr msg = {0};
	struct iovec iov = {0};
	int ret;

	iov.iov_base = (char *)cfg_data;
	iov.iov_len = sizeof(cfg_data);

	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	if (with_flowlabel) {
		struct cmsghdr *cm;

		cm = (void *)control;
		cm->cmsg_len = CMSG_LEN(sizeof(flowlabel));
		cm->cmsg_level = SOL_IPV6;
		cm->cmsg_type = IPV6_FLOWINFO;
		*(uint32_t *)CMSG_DATA(cm) = htonl(flowlabel);

		msg.msg_control = control;
		msg.msg_controllen = sizeof(control);
	}

	ret = sendmsg(fd, &msg, 0);
	if (ret == -1)
		error(1, errno, "send");

	if (with_flowlabel)
		fprintf(stderr, "sent with label %u\n", flowlabel);
	else
		fprintf(stderr, "sent without label\n");
}

static void do_recv(int fd, bool with_flowlabel, uint32_t expect)
{
	char control[CMSG_SPACE(sizeof(expect))];
	char data[sizeof(cfg_data)];
	struct msghdr msg = {0};
	struct iovec iov = {0};
	struct cmsghdr *cm;
	uint32_t flowlabel;
	int ret;

	iov.iov_base = data;
	iov.iov_len = sizeof(data);

	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	memset(control, 0, sizeof(control));
	msg.msg_control = control;
	msg.msg_controllen = sizeof(control);

	ret = recvmsg(fd, &msg, 0);
	if (ret == -1)
		error(1, errno, "recv");
	if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
		error(1, 0, "recv: truncated");
	if (ret != sizeof(cfg_data))
		error(1, 0, "recv: length mismatch");
	if (memcmp(data, cfg_data, sizeof(data)))
		error(1, 0, "recv: data mismatch");

	cm = CMSG_FIRSTHDR(&msg);
	if (with_flowlabel) {
		if (!cm)
			error(1, 0, "recv: missing cmsg");
		if (CMSG_NXTHDR(&msg, cm))
			error(1, 0, "recv: too many cmsg");
		if (cm->cmsg_level != SOL_IPV6 ||
		    cm->cmsg_type != IPV6_FLOWINFO)
			error(1, 0, "recv: unexpected cmsg level or type");

		flowlabel = ntohl(*(uint32_t *)CMSG_DATA(cm));
		fprintf(stderr, "recv with label %u\n", flowlabel);

		if (expect != FLOWLABEL_WILDCARD && expect != flowlabel)
			fprintf(stderr, "recv: incorrect flowlabel %u != %u\n",
					flowlabel, expect);

	} else {
		fprintf(stderr, "recv without label\n");
	}
}

static bool get_autoflowlabel_enabled(void)
{
	int fd, ret;
	char val;

	fd = open("/proc/sys/net/ipv6/auto_flowlabels", O_RDONLY);
	if (fd == -1)
		error(1, errno, "open sysctl");

	ret = read(fd, &val, 1);
	if (ret == -1)
		error(1, errno, "read sysctl");
	if (ret == 0)
		error(1, 0, "read sysctl: 0");

	if (close(fd))
		error(1, errno, "close sysctl");

	return val == '1';
}

static void flowlabel_get(int fd, uint32_t label, uint8_t share, uint16_t flags)
{
	struct in6_flowlabel_req req = {
		.flr_action = IPV6_FL_A_GET,
		.flr_label = htonl(label),
		.flr_flags = flags,
		.flr_share = share,
	};

	/* do not pass IPV6_ADDR_ANY or IPV6_ADDR_MAPPED */
	req.flr_dst.s6_addr[0] = 0xfd;
	req.flr_dst.s6_addr[15] = 0x1;

	if (setsockopt(fd, SOL_IPV6, IPV6_FLOWLABEL_MGR, &req, sizeof(req)))
		error(1, errno, "setsockopt flowlabel get");
}

static void parse_opts(int argc, char **argv)
{
	int c;

	while ((c = getopt(argc, argv, "l:")) != -1) {
		switch (c) {
		case 'l':
			cfg_label = strtoul(optarg, NULL, 0);
			break;
		default:
			error(1, 0, "%s: parse error", argv[0]);
		}
	}
}

int main(int argc, char **argv)
{
	struct sockaddr_in6 addr = {
		.sin6_family = AF_INET6,
		.sin6_port = htons(8000),
		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
	};
	const int one = 1;
	int fdt, fdr;

	parse_opts(argc, argv);

	fdt = socket(PF_INET6, SOCK_DGRAM, 0);
	if (fdt == -1)
		error(1, errno, "socket t");

	fdr = socket(PF_INET6, SOCK_DGRAM, 0);
	if (fdr == -1)
		error(1, errno, "socket r");

	if (connect(fdt, (void *)&addr, sizeof(addr)))
		error(1, errno, "connect");
	if (bind(fdr, (void *)&addr, sizeof(addr)))
		error(1, errno, "bind");

	flowlabel_get(fdt, cfg_label, IPV6_FL_S_EXCL, IPV6_FL_F_CREATE);

	if (setsockopt(fdr, SOL_IPV6, IPV6_FLOWINFO, &one, sizeof(one)))
		error(1, errno, "setsockopt flowinfo");

	if (get_autoflowlabel_enabled()) {
		fprintf(stderr, "send no label: recv auto flowlabel\n");
		do_send(fdt, false, 0);
		do_recv(fdr, true, FLOWLABEL_WILDCARD);
	} else {
		fprintf(stderr, "send no label: recv no label (auto off)\n");
		do_send(fdt, false, 0);
		do_recv(fdr, false, 0);
	}

	fprintf(stderr, "send label\n");
	do_send(fdt, true, cfg_label);
	do_recv(fdr, true, cfg_label);

	if (close(fdr))
		error(1, errno, "close r");
	if (close(fdt))
		error(1, errno, "close t");

	return 0;
}