summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/getsockopt_iter.c
blob: fe5a5268bc34ec6d57213543a467527fbaad466f (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// SPDX-License-Identifier: GPL-2.0
/*
 * Quick test for getsockopt{_iter} tests.
 *
 * Each fixture targets one converted protocol and pins down the
 * returned-length / errno semantics across buffer-size variations,
 * an unknown optname and a bogus level.
 *
 * - netlink: NETLINK_PKTINFO covers the flag-style int path; the
 *   NETLINK_LIST_MEMBERSHIPS cases cover the size-discovery path
 *   that always reports the required buffer length back via optlen,
 *   even when the user buffer is too small to receive any group bits.
 * - vsock:   SO_VM_SOCKETS_BUFFER_SIZE covers the u64 path.
 * - raw:     ICMP_FILTER covers a fixed-size struct payload that clamps
 *            the length down on a short buffer instead of failing.
 *
 * Author: Breno Leitao <leitao@debian.org>
 */

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/time_types.h>
#include <linux/vm_sockets.h>
#include <linux/icmp.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "kselftest_harness.h"

#ifndef AF_VSOCK
#define AF_VSOCK 40
#endif
#ifndef SOL_RAW
#define SOL_RAW 255
#endif
#ifndef ICMP_FILTER
#define ICMP_FILTER 1
#endif

/* ---------- netlink ---------- */

FIXTURE(netlink)
{
	int fd;
};

FIXTURE_SETUP(netlink)
{
	int group = RTNLGRP_LINK;

	self->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
	if (self->fd < 0)
		SKIP(return, "AF_NETLINK socket: %s", strerror(errno));

	/* Joining a multicast group grows nlk->ngroups so the
	 * NETLINK_LIST_MEMBERSHIPS path has a non-zero size to report.
	 */
	if (setsockopt(self->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
		       &group, sizeof(group)) < 0)
		SKIP(return, "NETLINK_ADD_MEMBERSHIP: %s", strerror(errno));
}

FIXTURE_TEARDOWN(netlink)
{
	if (self->fd >= 0)
		close(self->fd);
}

TEST_F(netlink, pktinfo_exact)
{
	socklen_t optlen;
	int val = -1;

	optlen = sizeof(val);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
				&val, &optlen));
	ASSERT_EQ(sizeof(int), optlen);
	ASSERT_TRUE(val == 0 || val == 1);
}

TEST_F(netlink, pktinfo_oversize_clamped)
{
	char buf[16] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
				buf, &optlen));
	ASSERT_EQ(sizeof(int), optlen);
}

TEST_F(netlink, pktinfo_undersize)
{
	char buf[2] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(-1, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
				 buf, &optlen));
	ASSERT_EQ(EINVAL, errno);
	ASSERT_EQ(sizeof(buf), optlen);
}

TEST_F(netlink, list_memberships_size_discovery)
{
	socklen_t optlen = 0;
	char dummy;

	ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK,
				NETLINK_LIST_MEMBERSHIPS,
				&dummy, &optlen));
	ASSERT_GT(optlen, 0);
	ASSERT_EQ(0, optlen % sizeof(__u32));
}

TEST_F(netlink, list_memberships_full_read)
{
	__u32 buf[64] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK,
				NETLINK_LIST_MEMBERSHIPS,
				buf, &optlen));
	ASSERT_GT(optlen, 0);
	ASSERT_LE(optlen, sizeof(buf));
	ASSERT_EQ(0, optlen % sizeof(__u32));
}

TEST_F(netlink, bad_level)
{
	socklen_t optlen;
	int val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, SOL_SOCKET + 1, NETLINK_PKTINFO,
				 &val, &optlen));
	ASSERT_EQ(ENOPROTOOPT, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

TEST_F(netlink, bad_optname)
{
	socklen_t optlen;
	int val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, SOL_NETLINK, 0x7fff,
				 &val, &optlen));
	ASSERT_EQ(ENOPROTOOPT, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

/* ---------- vsock ---------- */

FIXTURE(vsock)
{
	int fd;
};

FIXTURE_SETUP(vsock)
{
	self->fd = socket(AF_VSOCK, SOCK_STREAM, 0);
	if (self->fd < 0)
		SKIP(return, "AF_VSOCK socket: %s", strerror(errno));
}

FIXTURE_TEARDOWN(vsock)
{
	if (self->fd >= 0)
		close(self->fd);
}

TEST_F(vsock, buffer_size_exact)
{
	socklen_t optlen;
	uint64_t val = 0;

	optlen = sizeof(val);

	ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
				SO_VM_SOCKETS_BUFFER_SIZE,
				&val, &optlen));
	ASSERT_EQ(sizeof(uint64_t), optlen);
	ASSERT_GT(val, 0);
}

TEST_F(vsock, buffer_size_oversize_clamped)
{
	char buf[16] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
				SO_VM_SOCKETS_BUFFER_SIZE,
				buf, &optlen));
	ASSERT_EQ(sizeof(uint64_t), optlen);
}

TEST_F(vsock, buffer_size_undersize)
{
	char buf[4] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(-1, getsockopt(self->fd, AF_VSOCK,
				 SO_VM_SOCKETS_BUFFER_SIZE,
				 buf, &optlen));
	ASSERT_EQ(EINVAL, errno);
	ASSERT_EQ(sizeof(buf), optlen);
}

TEST_F(vsock, bad_level)
{
	socklen_t optlen;
	uint64_t val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, SOL_SOCKET + 1,
				 SO_VM_SOCKETS_BUFFER_SIZE,
				 &val, &optlen));
	ASSERT_EQ(ENOPROTOOPT, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

TEST_F(vsock, bad_optname)
{
	socklen_t optlen;
	uint64_t val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, AF_VSOCK, 0x7fff,
				 &val, &optlen));
	ASSERT_EQ(ENOPROTOOPT, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

/* SO_VM_SOCKETS_CONNECT_TIMEOUT_{NEW,OLD} return a sock_timeval-shaped
 * payload, which is wider than u64 on 64-bit. They exercise the path
 * where the protocol's reported lv (16 bytes) is larger than the
 * common 8-byte u64 case covered above.
 */
TEST_F(vsock, connect_timeout_new_exact)
{
	struct __kernel_sock_timeval tv = {};
	socklen_t optlen;

	optlen = sizeof(tv);

	ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
				SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW,
				&tv, &optlen));
	ASSERT_EQ(sizeof(tv), optlen);
}

TEST_F(vsock, connect_timeout_new_oversize_clamped)
{
	char buf[sizeof(struct __kernel_sock_timeval) * 2] = {};
	socklen_t optlen;

	optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
				SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW,
				buf, &optlen));
	ASSERT_EQ(sizeof(struct __kernel_sock_timeval), optlen);
}

TEST_F(vsock, connect_timeout_new_undersize)
{
	socklen_t optlen;
	uint64_t val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, AF_VSOCK,
				 SO_VM_SOCKETS_CONNECT_TIMEOUT_NEW,
				 &val, &optlen));
	ASSERT_EQ(EINVAL, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

TEST_F(vsock, connect_timeout_old_exact)
{
	struct __kernel_old_timeval tv = {};
	socklen_t optlen;

	optlen = sizeof(tv);

	ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
				SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD,
				&tv, &optlen));
	ASSERT_EQ(sizeof(tv), optlen);
}

/* ---------- raw (ipv4) ---------- */

FIXTURE(raw)
{
	int fd;
};

FIXTURE_SETUP(raw)
{
	struct icmp_filter filt = { .data = 0xdeadbeef };

	self->fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
	if (self->fd < 0)
		SKIP(return, "SOCK_RAW/ICMP socket: %s", strerror(errno));

	if (setsockopt(self->fd, SOL_RAW, ICMP_FILTER, &filt, sizeof(filt)) < 0)
		SKIP(return, "set ICMP_FILTER: %s", strerror(errno));
}

FIXTURE_TEARDOWN(raw)
{
	if (self->fd >= 0)
		close(self->fd);
}

TEST_F(raw, icmpfilter_exact)
{
	struct icmp_filter filt = {};
	socklen_t optlen = sizeof(filt);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_RAW, ICMP_FILTER,
				&filt, &optlen));
	ASSERT_EQ(sizeof(filt), optlen);
	ASSERT_EQ(0xdeadbeef, filt.data);
}

TEST_F(raw, icmpfilter_oversize_clamped)
{
	char buf[16] = {};
	socklen_t optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_RAW, ICMP_FILTER,
				buf, &optlen));
	ASSERT_EQ(sizeof(struct icmp_filter), optlen);
}

/* Unlike the int/u64 options above, ICMP_FILTER clamps the length down
 * to the user buffer instead of returning EINVAL: a short buffer
 * succeeds and reports the truncated length back via optlen.
 */
TEST_F(raw, icmpfilter_undersize_clamped)
{
	char buf[2] = {};
	socklen_t optlen = sizeof(buf);

	ASSERT_EQ(0, getsockopt(self->fd, SOL_RAW, ICMP_FILTER,
				buf, &optlen));
	ASSERT_EQ(sizeof(buf), optlen);
}

TEST_F(raw, icmpfilter_wrong_proto)
{
	struct icmp_filter filt;
	socklen_t optlen = sizeof(filt);
	int fd;

	fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
	if (fd < 0)
		SKIP(return, "SOCK_RAW/UDP socket: %s", strerror(errno));

	ASSERT_EQ(-1, getsockopt(fd, SOL_RAW, ICMP_FILTER, &filt, &optlen));
	ASSERT_EQ(EOPNOTSUPP, errno);
	close(fd);
}

TEST_F(raw, bad_optname)
{
	socklen_t optlen;
	int val;

	optlen = sizeof(val);

	ASSERT_EQ(-1, getsockopt(self->fd, SOL_RAW, 0x7fff, &val, &optlen));
	ASSERT_EQ(ENOPROTOOPT, errno);
	ASSERT_EQ(sizeof(val), optlen);
}

TEST_HARNESS_MAIN