xref: /freebsd/tests/sys/netinet/raw.c (revision f6989880841b89d54ebcd5d12195c40a23627646)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2026 Gleb Smirnoff <glebius@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netinet/ip.h>
33 #include <net/if.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 
37 #include <atf-c.h>
38 
39 #define	PROT1		253 /* RFC3692 */
40 #define	PROT2		254 /* RFC3692 */
41 #define	ADDR1		{ htonl(0xc0000202) }	/* RFC5737 */
42 #define	ADDR2		{ htonl(0xc0000203) }	/* RFC5737 */
43 #define	WILD		{ htonl(INADDR_ANY) }
44 #define	LOOP(x)		{ htonl(INADDR_LOOPBACK + (x)) }
45 #define	MULT(x)		{ htonl(INADDR_UNSPEC_GROUP + (x)) }
46 
47 static int
48 rawsender(bool mcast)
49 {
50 	int s;
51 
52 	ATF_REQUIRE((s = socket(PF_INET, SOCK_RAW, 0)) != -1);
53 	ATF_REQUIRE(setsockopt(s, IPPROTO_IP, IP_HDRINCL, &(int){1},
54 	    sizeof(int)) == 0);
55 	/*
56 	 * Make sending socket connected.  The socket API requires connected
57 	 * status to use send(2), even with IP_HDRINCL.
58 	 */
59 	ATF_REQUIRE(connect(s,
60 	    (struct sockaddr *)&(struct sockaddr_in){
61 		.sin_family = AF_INET,
62 		.sin_len = sizeof(struct sockaddr_in),
63 		.sin_addr = { htonl(INADDR_ANY) },
64 	    }, sizeof(struct sockaddr_in)) == 0);
65 
66 	if (mcast)
67 		ATF_REQUIRE(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
68 		    &(struct ip_mreqn){
69 			.imr_ifindex = if_nametoindex("lo0"),
70 		    }, sizeof(struct ip_mreqn)) == 0);
71 
72 	return (s);
73 }
74 
75 /*
76  * The 'input' test exercises logic of rip_input().  The best documentation
77  * for raw socket input behavior is collected in Stevens's UNIX Network
78  * Programming, Section 28.4.  We create several sockets, with different
79  * remote and local bindings, as well as a socket with multicast membership
80  * and then we send different packets and see which sockets received their
81  * copy.
82  * The table tests[] describes our expectations.
83  */
84 ATF_TC_WITHOUT_HEAD(input);
85 ATF_TC_BODY(input, tc)
86 {
87 	static const struct rcvr {
88 		struct in_addr laddr, faddr, maddr;
89 		uint8_t	proto;
90 	} rcvrs[] = {
91 		{ WILD,	   WILD,    WILD,    0 },
92 		{ WILD,    WILD,    WILD,    PROT1 },
93 		{ LOOP(0), WILD,    WILD,    0 },
94 		{ LOOP(0), WILD,    WILD,    PROT1 },
95 		{ LOOP(1), WILD,    WILD,    0 },
96 		{ LOOP(1), WILD,    WILD,    PROT1 },
97 		{ LOOP(0), LOOP(2), WILD,    0 },
98 		{ LOOP(0), LOOP(2), WILD,    PROT1 },
99 		{ LOOP(0), LOOP(3), WILD,    0 },
100 		{ LOOP(0), LOOP(3), WILD,    PROT1 },
101 		{ LOOP(1), LOOP(3), WILD,    0 },
102 		{ LOOP(1), LOOP(3), WILD,    PROT1 },
103 		{ WILD,	   WILD,    MULT(1), 0 },
104 	};
105 	static const struct test {
106 		struct in_addr src, dst;
107 		uint8_t proto;
108 		bool results[nitems(rcvrs)];
109 	} tests[] = {
110 #define	x true
111 #define	o false
112 		{ LOOP(2), LOOP(0), PROT1,
113 		  { x, x, x, x, o, o, x, x, o, o, o, o, x } },
114 		{ LOOP(2), LOOP(0), PROT2,
115 		  { x, o, x, o, o, o, x, o, o, o, o, o, x } },
116 		{ LOOP(3), LOOP(0), PROT1,
117 		  { x, x, x, x, o, o, o, o, x, x, o, o, x } },
118 		{ LOOP(3), LOOP(0), PROT2,
119 		  { x, o, x, o, o, o, o, o, x, o, o, o, x } },
120 		{ LOOP(2), LOOP(1), PROT1,
121 		  { x, x, o, o, x, x, o, o, o, o, o, o, x } },
122 		{ LOOP(2), LOOP(1), PROT2,
123 		  { x, o, o, o, x, o, o, o, o, o, o, o, x } },
124 		{ LOOP(3), LOOP(1), PROT1,
125 		  { x, x, o, o, x, x, o, o, o, o, x, x, x } },
126 		{ LOOP(3), LOOP(1), PROT2,
127 		  { x, o, o, o, x, o, o, o, o, o, x, o, x } },
128 		{ LOOP(3), MULT(1), PROT1,
129 		  { x, x, o, o, o, o, o, o, o, o, o, o, x } },
130 		{ LOOP(3), MULT(2), PROT1,
131 		  { x, x, o, o, o, o, o, o, o, o, o, o, o } },
132 #undef x
133 #undef o
134 	};
135 	struct pkt {
136 		struct ip ip;
137 		char payload[100];
138 	} __packed pkt = {
139 		.ip.ip_v = IPVERSION,
140 		.ip.ip_hl = sizeof(struct ip) >> 2,
141 		.ip.ip_len = htons(sizeof(struct pkt)),
142 		.ip.ip_ttl = 16,
143 	};
144 	struct sockaddr_in sin = {
145 		.sin_family = AF_INET,
146 		.sin_len = sizeof(sin),
147 	};
148 	struct ip_mreqn mreqn = {
149 		.imr_ifindex = if_nametoindex("lo0"),
150 	};
151 	int r[nitems(rcvrs)];
152 	int s;
153 
154 	/*
155 	 * This XXX to be removed when kyua provides generic framework for
156 	 * constructing test jail environments.
157 	 */
158 	system("/sbin/ifconfig lo0 127.0.0.1/32");
159 	system("/sbin/ifconfig lo0 127.0.0.2/32 alias");
160 
161 	for (u_int i = 0; i < nitems(rcvrs); i++) {
162 		/*
163 		 * To avoid a race between send(2) and packet queueing in
164 		 * netisr(9) and our recv(2), set the very first receiver
165 		 * socket to blocking mode.  Note in the above table that first
166 		 * receiver is supposed to receive something in every test.
167 		 */
168 		ATF_REQUIRE((r[i] = socket(PF_INET, SOCK_RAW |
169 		    (i != 0 ? SOCK_NONBLOCK : 0),
170 		    rcvrs[i].proto)) != -1);
171 		if (rcvrs[i].laddr.s_addr != htonl(INADDR_ANY)) {
172 			sin.sin_addr = rcvrs[i].laddr;
173 			ATF_REQUIRE(bind(r[i], (struct sockaddr *)&sin,
174 			    sizeof(sin)) == 0);
175 		}
176 		if (rcvrs[i].faddr.s_addr != htonl(INADDR_ANY)) {
177 			sin.sin_addr = rcvrs[i].faddr;
178 			ATF_REQUIRE(connect(r[i], (struct sockaddr *)&sin,
179 			    sizeof(sin)) == 0);
180 		}
181 		if (rcvrs[i].maddr.s_addr != htonl(INADDR_ANY)) {
182 			mreqn.imr_multiaddr = rcvrs[i].maddr;
183 			ATF_REQUIRE(setsockopt(r[i], IPPROTO_IP,
184 			    IP_ADD_MEMBERSHIP, &mreqn, sizeof(mreqn)) == 0);
185 		}
186 	}
187 
188 	/*
189 	 * Force multicast interface for the sending socket to be able to
190 	 * send to MULT(x) destinations.
191 	 */
192 	s = rawsender(true);
193 
194 	for (u_int i = 0; i < nitems(tests); i++) {
195 		arc4random_buf(&pkt.payload, sizeof(pkt.payload));
196 		pkt.ip.ip_src = tests[i].src;
197 		pkt.ip.ip_dst = tests[i].dst;
198 		pkt.ip.ip_p = tests[i].proto;
199 		ATF_REQUIRE(send(s, &pkt, sizeof(pkt), 0) == sizeof(pkt));
200 		for (u_int j = 0; j < nitems(rcvrs); j++) {
201 			char buf[sizeof(pkt)];
202 			char p[4][INET_ADDRSTRLEN];
203 			ssize_t ss;
204 
205 			ss = recv(r[j], buf, sizeof(buf), 0);
206 
207 			ATF_REQUIRE_MSG((tests[i].results[j] == true &&
208 			    ss == sizeof(buf) && memcmp(buf + sizeof(struct ip),
209 			    pkt.payload, sizeof(pkt.payload)) == 0) ||
210 			    (tests[i].results[j] == false &&
211 			    ss == -1 && errno == EAGAIN),
212 			    "test #%u %s->%s %u unexpected receive of %zd "
213 			     "bytes errno %d on socket #%u %s->%s %u", i,
214 			    inet_ntop(AF_INET, &tests[i].src, p[0],
215 				INET_ADDRSTRLEN),
216 			    inet_ntop(AF_INET, &tests[i].dst, p[1],
217 				INET_ADDRSTRLEN),
218 			    tests[i].proto, ss, errno, j,
219 			    inet_ntop(AF_INET, &rcvrs[j].faddr, p[2],
220 				INET_ADDRSTRLEN),
221 			    inet_ntop(AF_INET, &rcvrs[j].laddr, p[3],
222 				INET_ADDRSTRLEN),
223 			    rcvrs[j].proto);
224 		}
225 	}
226 }
227 
228 /*
229  * Test input on the same socket that changes its connection status.  We send
230  * packets with different sources in each iteration and check results.
231  * Check that connect(INADDR_ANY) is effectively a disconnect and turns socket
232  * back to receive-all mode.
233  */
234 ATF_TC_WITHOUT_HEAD(reconnect);
235 ATF_TC_BODY(reconnect, tc)
236 {
237 	static const struct in_addr srcs[] = { ADDR1, ADDR2 };
238 	static const struct test {
239 		struct in_addr faddr;
240 		bool results[nitems(srcs)];
241 	} tests[] = {
242 		{ ADDR1,	{ true, false } },
243 		{ ADDR2,	{ false, true } },
244 		{ {INADDR_ANY},	{ true, true } },
245 	};
246 	struct pkt {
247 		struct ip ip;
248 		char payload[100];
249 	} __packed pkt = {
250 		.ip.ip_v = IPVERSION,
251 		.ip.ip_hl = sizeof(struct ip) >> 2,
252 		.ip.ip_len = htons(sizeof(struct pkt)),
253 		.ip.ip_ttl = 16,
254 		.ip.ip_p = PROT1,
255 		.ip.ip_dst = LOOP(0),
256 	};
257 	int r, s;
258 
259 	/* XXX */
260 	system("/sbin/ifconfig lo0 127.0.0.1/32");
261 
262 	ATF_REQUIRE((r = socket(PF_INET, SOCK_RAW | SOCK_NONBLOCK, 0)) != -1);
263 	s = rawsender(false);
264 
265 	for (u_int i = 0; i < nitems(tests); i++) {
266 		ATF_REQUIRE(connect(r,
267 		    (struct sockaddr *)&(struct sockaddr_in){
268 			.sin_family = AF_INET,
269 			.sin_len = sizeof(struct sockaddr_in),
270 			.sin_addr = tests[i].faddr,
271 		    }, sizeof(struct sockaddr_in)) == 0);
272 
273 		for (u_int j = 0; j < nitems(srcs); j++) {
274 			char buf[sizeof(pkt)];
275 			char p[2][INET_ADDRSTRLEN];
276 			ssize_t ss;
277 
278 			arc4random_buf(&pkt.payload, sizeof(pkt.payload));
279 			pkt.ip.ip_src = srcs[j];
280 			ATF_REQUIRE(send(s, &pkt, sizeof(pkt), 0) ==
281 			    sizeof(pkt));
282 
283 			/*
284 			 * The sender is a blocking socket, so we first receive
285 			 * from the sender and when this read returns we are
286 			 * guaranteed that the test socket also received the
287 			 * datagram.
288 			 */
289 			ss = recv(s, buf, sizeof(buf), 0);
290 			ATF_REQUIRE(ss == sizeof(buf) &&
291 			    memcmp(buf + sizeof(struct ip),
292 			    pkt.payload, sizeof(pkt.payload)) == 0);
293 
294 			ss = recv(r, buf, sizeof(buf), 0);
295 
296 			ATF_REQUIRE_MSG((tests[i].results[j] == true &&
297 			    ss == sizeof(buf) && memcmp(buf + sizeof(struct ip),
298 			    pkt.payload, sizeof(pkt.payload)) == 0) ||
299 			    (tests[i].results[j] == false && ss == -1 &&
300 			    errno == EAGAIN),
301 			    "test #%u src %s connect address %s unexpected "
302 			    "receive of %zd bytes errno %d", i,
303 			    inet_ntop(AF_INET, &srcs[j], p[0],
304 				INET_ADDRSTRLEN),
305 			    inet_ntop(AF_INET, &tests[i].faddr, p[1],
306 				INET_ADDRSTRLEN),
307 			    ss, errno);
308 		}
309 	}
310 }
311 
312 ATF_TP_ADD_TCS(tp)
313 {
314 	ATF_TP_ADD_TC(tp, input);
315 	ATF_TP_ADD_TC(tp, reconnect);
316 
317 	return (atf_no_error());
318 }
319