summaryrefslogtreecommitdiff
path: root/tests/sys/netinet/raw.c
blob: d3feb315d5c4bd96340bab62fb94e333e7ff815f (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
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2026 Gleb Smirnoff <glebius@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <net/if.h>
#include <errno.h>
#include <stdlib.h>

#include <atf-c.h>

#define	PROT1		253 /* RFC3692 */
#define	PROT2		254 /* RFC3692 */
#define	ADDR1		{ htonl(0xc0000202) }	/* RFC5737 */
#define	ADDR2		{ htonl(0xc0000203) }	/* RFC5737 */
#define	WILD		{ htonl(INADDR_ANY) }
#define	LOOP(x)		{ htonl(INADDR_LOOPBACK + (x)) }
#define	MULT(x)		{ htonl(INADDR_UNSPEC_GROUP + (x)) }

static int
rawsender(bool mcast)
{
	int s;

	ATF_REQUIRE((s = socket(PF_INET, SOCK_RAW, 0)) != -1);
	ATF_REQUIRE(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &(int){1},
	    sizeof(int)) == 0);
	/*
	 * Make sending socket connected.  The socket API requires connected
	 * status to use send(2), even with IP_HDRINCL.
	 */
	ATF_REQUIRE(connect(s,
	    (struct sockaddr *)&(struct sockaddr_in){
		.sin_family = AF_INET,
		.sin_len = sizeof(struct sockaddr_in),
		.sin_addr = { htonl(INADDR_ANY) },
	    }, sizeof(struct sockaddr_in)) == 0);

	if (mcast)
		ATF_REQUIRE(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
		    &(struct ip_mreqn){
			.imr_ifindex = if_nametoindex("lo0"),
		    }, sizeof(struct ip_mreqn)) == 0);

	return (s);
}

/*
 * The 'input' test exercises logic of rip_input().  The best documentation
 * for raw socket input behavior is collected in Stevens's UNIX Network
 * Programming, Section 28.4.  We create several sockets, with different
 * remote and local bindings, as well as a socket with multicast membership
 * and then we send different packets and see which sockets received their
 * copy.
 * The table tests[] describes our expectations.
 */
ATF_TC_WITHOUT_HEAD(input);
ATF_TC_BODY(input, tc)
{
	static const struct rcvr {
		struct in_addr laddr, faddr, maddr;
		uint8_t	proto;
	} rcvrs[] = {
		{ WILD,	   WILD,    WILD,    0 },
		{ WILD,    WILD,    WILD,    PROT1 },
		{ LOOP(0), WILD,    WILD,    0 },
		{ LOOP(0), WILD,    WILD,    PROT1 },
		{ LOOP(1), WILD,    WILD,    0 },
		{ LOOP(1), WILD,    WILD,    PROT1 },
		{ LOOP(0), LOOP(2), WILD,    0 },
		{ LOOP(0), LOOP(2), WILD,    PROT1 },
		{ LOOP(0), LOOP(3), WILD,    0 },
		{ LOOP(0), LOOP(3), WILD,    PROT1 },
		{ LOOP(1), LOOP(3), WILD,    0 },
		{ LOOP(1), LOOP(3), WILD,    PROT1 },
		{ WILD,	   WILD,    MULT(1), 0 },
	};
	static const struct test {
		struct in_addr src, dst;
		uint8_t proto;
		bool results[nitems(rcvrs)];
	} tests[] = {
#define	x true
#define	o false
		{ LOOP(2), LOOP(0), PROT1,
		  { x, x, x, x, o, o, x, x, o, o, o, o, x } },
		{ LOOP(2), LOOP(0), PROT2,
		  { x, o, x, o, o, o, x, o, o, o, o, o, x } },
		{ LOOP(3), LOOP(0), PROT1,
		  { x, x, x, x, o, o, o, o, x, x, o, o, x } },
		{ LOOP(3), LOOP(0), PROT2,
		  { x, o, x, o, o, o, o, o, x, o, o, o, x } },
		{ LOOP(2), LOOP(1), PROT1,
		  { x, x, o, o, x, x, o, o, o, o, o, o, x } },
		{ LOOP(2), LOOP(1), PROT2,
		  { x, o, o, o, x, o, o, o, o, o, o, o, x } },
		{ LOOP(3), LOOP(1), PROT1,
		  { x, x, o, o, x, x, o, o, o, o, x, x, x } },
		{ LOOP(3), LOOP(1), PROT2,
		  { x, o, o, o, x, o, o, o, o, o, x, o, x } },
		{ LOOP(3), MULT(1), PROT1,
		  { x, x, o, o, o, o, o, o, o, o, o, o, x } },
		{ LOOP(3), MULT(2), PROT1,
		  { x, x, o, o, o, o, o, o, o, o, o, o, o } },
#undef x
#undef o
	};
	struct pkt {
		struct ip ip;
		char payload[100];
	} __packed pkt = {
		.ip.ip_v = IPVERSION,
		.ip.ip_hl = sizeof(struct ip) >> 2,
		.ip.ip_len = htons(sizeof(struct pkt)),
		.ip.ip_ttl = 16,
	};
	struct sockaddr_in sin = {
		.sin_family = AF_INET,
		.sin_len = sizeof(sin),
	};
	struct ip_mreqn mreqn = {
		.imr_ifindex = if_nametoindex("lo0"),
	};
	int r[nitems(rcvrs)];
	int s;

	/*
	 * This XXX to be removed when kyua provides generic framework for
	 * constructing test jail environments.
	 */
	system("/sbin/ifconfig lo0 127.0.0.1/32");
	system("/sbin/ifconfig lo0 127.0.0.2/32 alias");

	for (u_int i = 0; i < nitems(rcvrs); i++) {
		/*
		 * To avoid a race between send(2) and packet queueing in
		 * netisr(9) and our recv(2), set the very first receiver
		 * socket to blocking mode.  Note in the above table that first
		 * receiver is supposed to receive something in every test.
		 */
		ATF_REQUIRE((r[i] = socket(PF_INET, SOCK_RAW |
		    (i != 0 ? SOCK_NONBLOCK : 0),
		    rcvrs[i].proto)) != -1);
		if (rcvrs[i].laddr.s_addr != htonl(INADDR_ANY)) {
			sin.sin_addr = rcvrs[i].laddr;
			ATF_REQUIRE(bind(r[i], (struct sockaddr *)&sin,
			    sizeof(sin)) == 0);
		}
		if (rcvrs[i].faddr.s_addr != htonl(INADDR_ANY)) {
			sin.sin_addr = rcvrs[i].faddr;
			ATF_REQUIRE(connect(r[i], (struct sockaddr *)&sin,
			    sizeof(sin)) == 0);
		}
		if (rcvrs[i].maddr.s_addr != htonl(INADDR_ANY)) {
			mreqn.imr_multiaddr = rcvrs[i].maddr;
			ATF_REQUIRE(setsockopt(r[i], IPPROTO_IP,
			    IP_ADD_MEMBERSHIP, &mreqn, sizeof(mreqn)) == 0);
		}
	}

	/*
	 * Force multicast interface for the sending socket to be able to
	 * send to MULT(x) destinations.
	 */
	s = rawsender(true);

	for (u_int i = 0; i < nitems(tests); i++) {
		arc4random_buf(&pkt.payload, sizeof(pkt.payload));
		pkt.ip.ip_src = tests[i].src;
		pkt.ip.ip_dst = tests[i].dst;
		pkt.ip.ip_p = tests[i].proto;
		ATF_REQUIRE(send(s, &pkt, sizeof(pkt), 0) == sizeof(pkt));
		for (u_int j = 0; j < nitems(rcvrs); j++) {
			char buf[sizeof(pkt)];
			char p[4][INET_ADDRSTRLEN];
			ssize_t ss;

			ss = recv(r[j], buf, sizeof(buf), 0);

			ATF_REQUIRE_MSG((tests[i].results[j] == true &&
			    ss == sizeof(buf) && memcmp(buf + sizeof(struct ip),
			    pkt.payload, sizeof(pkt.payload)) == 0) ||
			    (tests[i].results[j] == false &&
			    ss == -1 && errno == EAGAIN),
			    "test #%u %s->%s %u unexpected receive of %zd "
			     "bytes errno %d on socket #%u %s->%s %u", i,
			    inet_ntop(AF_INET, &tests[i].src, p[0],
				INET_ADDRSTRLEN),
			    inet_ntop(AF_INET, &tests[i].dst, p[1],
				INET_ADDRSTRLEN),
			    tests[i].proto, ss, errno, j,
			    inet_ntop(AF_INET, &rcvrs[j].faddr, p[2],
				INET_ADDRSTRLEN),
			    inet_ntop(AF_INET, &rcvrs[j].laddr, p[3],
				INET_ADDRSTRLEN),
			    rcvrs[j].proto);
		}
	}
}

/*
 * Test input on the same socket that changes its connection status.  We send
 * packets with different sources in each iteration and check results.
 * Check that connect(INADDR_ANY) is effectively a disconnect and turns socket
 * back to receive-all mode.
 */
ATF_TC_WITHOUT_HEAD(reconnect);
ATF_TC_BODY(reconnect, tc)
{
	static const struct in_addr srcs[] = { ADDR1, ADDR2 };
	static const struct test {
		struct in_addr faddr;
		bool results[nitems(srcs)];
	} tests[] = {
		{ ADDR1,	{ true, false } },
		{ ADDR2,	{ false, true } },
		{ {INADDR_ANY},	{ true, true } },
	};
	struct pkt {
		struct ip ip;
		char payload[100];
	} __packed pkt = {
		.ip.ip_v = IPVERSION,
		.ip.ip_hl = sizeof(struct ip) >> 2,
		.ip.ip_len = htons(sizeof(struct pkt)),
		.ip.ip_ttl = 16,
		.ip.ip_p = PROT1,
		.ip.ip_dst = LOOP(0),
	};
	int r, s;

	/* XXX */
	system("/sbin/ifconfig lo0 127.0.0.1/32");

	ATF_REQUIRE((r = socket(PF_INET, SOCK_RAW | SOCK_NONBLOCK, 0)) != -1);
	s = rawsender(false);

	for (u_int i = 0; i < nitems(tests); i++) {
		ATF_REQUIRE(connect(r,
		    (struct sockaddr *)&(struct sockaddr_in){
			.sin_family = AF_INET,
			.sin_len = sizeof(struct sockaddr_in),
			.sin_addr = tests[i].faddr,
		    }, sizeof(struct sockaddr_in)) == 0);

		for (u_int j = 0; j < nitems(srcs); j++) {
			char buf[sizeof(pkt)];
			char p[2][INET_ADDRSTRLEN];
			ssize_t ss;

			arc4random_buf(&pkt.payload, sizeof(pkt.payload));
			pkt.ip.ip_src = srcs[j];
			ATF_REQUIRE(send(s, &pkt, sizeof(pkt), 0) ==
			    sizeof(pkt));

			/*
			 * The sender is a blocking socket, so we first receive
			 * from the sender and when this read returns we are
			 * guaranteed that the test socket also received the
			 * datagram.
			 */
			ss = recv(s, buf, sizeof(buf), 0);
			ATF_REQUIRE(ss == sizeof(buf) &&
			    memcmp(buf + sizeof(struct ip),
			    pkt.payload, sizeof(pkt.payload)) == 0);

			ss = recv(r, buf, sizeof(buf), 0);

			ATF_REQUIRE_MSG((tests[i].results[j] == true &&
			    ss == sizeof(buf) && memcmp(buf + sizeof(struct ip),
			    pkt.payload, sizeof(pkt.payload)) == 0) ||
			    (tests[i].results[j] == false && ss == -1 &&
			    errno == EAGAIN),
			    "test #%u src %s connect address %s unexpected "
			    "receive of %zd bytes errno %d", i,
			    inet_ntop(AF_INET, &srcs[j], p[0],
				INET_ADDRSTRLEN),
			    inet_ntop(AF_INET, &tests[i].faddr, p[1],
				INET_ADDRSTRLEN),
			    ss, errno);
		}
	}
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, input);
	ATF_TP_ADD_TC(tp, reconnect);

	return (atf_no_error());
}