xref: /netbsd-src/tests/net/icmp/t_ping.c (revision dd9a37587628c7b66115640171fbbeb53747344a)
1 /*	$NetBSD: t_ping.c,v 1.24 2019/06/11 08:34:01 gson Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: t_ping.c,v 1.24 2019/06/11 08:34:01 gson Exp $");
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
38 #include <sys/wait.h>
39 
40 #include <atf-c.h>
41 #include <assert.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <signal.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/ip_var.h>
51 
52 #include <rump/rump.h>
53 #include <rump/rump_syscalls.h>
54 
55 #include "h_macros.h"
56 #include "../config/netconfig.c"
57 
58 ATF_TC(simpleping);
ATF_TC_HEAD(simpleping,tc)59 ATF_TC_HEAD(simpleping, tc)
60 {
61 
62 	atf_tc_set_md_var(tc, "descr", "check that kernel responds to ping");
63 	atf_tc_set_md_var(tc, "timeout", "20");
64 }
65 
ATF_TC_BODY(simpleping,tc)66 ATF_TC_BODY(simpleping, tc)
67 {
68 	char ifname[IFNAMSIZ];
69 	pid_t cpid;
70 	bool win, win2;
71 	char token;
72 	int channel[2];
73 
74 	RL(pipe(channel));
75 
76 	cpid = fork();
77 	rump_init();
78 	netcfg_rump_makeshmif("but-can-i-buy-your-ether-bus", ifname);
79 
80 	switch (cpid) {
81 	case -1:
82 		atf_tc_fail_errno("fork failed");
83 	case 0:
84 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
85 		close(channel[0]);
86 		ATF_CHECK(write(channel[1], "U", 1) == 1);
87 		close(channel[1]);
88 		pause();
89 		break;
90 	default:
91 		break;
92 	}
93 
94 	close(channel[1]);
95 	ATF_CHECK(read(channel[0], &token, 1) == 1 && token == 'U');
96 	close(channel[0]);
97 
98 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
99 
100 	/*
101 	 * The beauty of shmif is that we don't have races here.
102 	 */
103 	win = netcfg_rump_pingtest("1.1.1.10", 500);
104 	win2 = netcfg_rump_pingtest("1.1.1.30", 500);
105 
106 	kill(cpid, SIGKILL);
107 
108 	if (!win)
109 		atf_tc_fail("ping failed");
110 	if (win2)
111 		atf_tc_fail("non-existent host responded");
112 }
113 
114 ATF_TC(floodping);
ATF_TC_HEAD(floodping,tc)115 ATF_TC_HEAD(floodping, tc)
116 {
117 
118 	atf_tc_set_md_var(tc, "descr", "see how kernel responds to floodping");
119 }
120 
121 /* why the hell isn't this available in userspace??? */
122 static uint16_t
in_cksum(void * data,size_t len)123 in_cksum(void *data, size_t len)
124 {
125 	uint16_t *buf = data;
126 	unsigned sum;
127 
128 	for (sum = 0; len > 1; len -= 2)
129 		sum += *buf++;
130 	if (len)
131 		sum += *(uint8_t *)buf;
132 
133 	sum = (sum >> 16) + (sum & 0xffff);
134 	sum += (sum >> 16);
135 
136 	return ~sum;
137 }
138 
139 static int
doping(const char * target,int loops,u_int pktsize)140 doping(const char *target, int loops, u_int pktsize)
141 {
142 	union {
143 		char buf[IP_MAXPACKET - sizeof(struct ip)];
144 		struct icmp i;	/* ensure proper alignment */
145 	} sndbuf;
146 	char recvbuf[IP_MAXPACKET];
147 	struct sockaddr_in dst, pingee;
148 	struct icmp *icmp;
149 	socklen_t slen;
150 	ssize_t n;
151 	int loop, succ;
152 	int x, xnon, s;
153 
154 	RL(s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP));
155 	RL(x = rump_sys_fcntl(s, F_GETFL, 0));
156 	xnon = x | O_NONBLOCK;
157 
158 	memset(&dst, 0, sizeof(dst));
159 	dst.sin_len = sizeof(dst);
160 	dst.sin_family = AF_INET;
161 	dst.sin_addr.s_addr = inet_addr(target);
162 
163 	icmp = (struct icmp *)&sndbuf;
164 	memset(icmp, 0, sizeof(*icmp));
165 	icmp->icmp_type = ICMP_ECHO;
166 	icmp->icmp_id = htons(37);
167 
168 	if (pktsize < sizeof(*icmp))
169 		pktsize = sizeof(*icmp);
170 	if (pktsize > sizeof(sndbuf.buf))
171 		pktsize = sizeof(sndbuf.buf);
172 
173 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
174 	    &pktsize, sizeof(pktsize)));
175 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
176 	    &pktsize, sizeof(pktsize)));
177 
178 	slen = sizeof(pingee);
179 	succ = 0;
180 	for (loop = 0; loop < loops; loop++) {
181 		RL(rump_sys_fcntl(s, F_SETFL, x));
182 		icmp->icmp_seq = htons(loop);
183 		icmp->icmp_cksum = 0;
184 		icmp->icmp_cksum = in_cksum(icmp, pktsize);
185 
186 		n = rump_sys_sendto(s, icmp, pktsize, 0,
187 		    (struct sockaddr *)&dst, sizeof(dst));
188 		if (n == -1) {
189 			if (errno == ENOBUFS)
190 				continue;
191 			atf_tc_fail_errno("sendto failed");
192 		}
193 
194 		RL(rump_sys_fcntl(s, F_SETFL, xnon));
195 		while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
196 		    (struct sockaddr *)&pingee, &slen)) > 0) {
197 			succ++;
198 		}
199 		if (n == -1 && (errno == EAGAIN || errno == ENOBUFS))
200 			continue;
201 		atf_tc_fail_errno("recv failed (n == %zd)", n);
202 	}
203 
204 	rump_sys_close(s);
205 	return succ;
206 }
207 
208 #define LOOPS 10000
209 
ATF_TC_BODY(floodping,tc)210 ATF_TC_BODY(floodping, tc)
211 {
212 	char ifname[IFNAMSIZ];
213 	pid_t cpid;
214 	int succ;
215 
216 	cpid = fork();
217 	rump_init();
218 	netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
219 
220 	switch (cpid) {
221 	case -1:
222 		atf_tc_fail_errno("fork failed");
223 	case 0:
224 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
225 		pause();
226 		break;
227 	default:
228 		break;
229 	}
230 
231 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
232 
233 	succ = doping("1.1.1.10", LOOPS, 56);
234 	printf("got %d/%d\n", succ, LOOPS);
235 
236 	kill(cpid, SIGKILL);
237 }
238 
239 ATF_TC(floodping2);
ATF_TC_HEAD(floodping2,tc)240 ATF_TC_HEAD(floodping2, tc)
241 {
242 
243 	atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
244 }
245 
ATF_TC_BODY(floodping2,tc)246 ATF_TC_BODY(floodping2, tc)
247 {
248 	char ifname[IFNAMSIZ];
249 	pid_t cpid;
250 	int succ;
251 
252 	cpid = fork();
253 	rump_init();
254 	netcfg_rump_makeshmif("floodping2", ifname);
255 
256 	switch (cpid) {
257 	case -1:
258 		atf_tc_fail_errno("fork failed");
259 	case 0:
260 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
261 		succ = doping("1.1.1.20", LOOPS, 56);
262 		break;
263 	default:
264 		netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
265 		succ = doping("1.1.1.10", LOOPS, 56);
266 		break;
267 	}
268 
269 	printf("got %d/%d\n", succ, LOOPS);
270 }
271 
272 ATF_TC(pingsize);
ATF_TC_HEAD(pingsize,tc)273 ATF_TC_HEAD(pingsize, tc)
274 {
275 
276 	atf_tc_set_md_var(tc, "descr", "ping with packets min <= size <= max");
277 }
278 
ATF_TC_BODY(pingsize,tc)279 ATF_TC_BODY(pingsize, tc)
280 {
281 	char ifname[IFNAMSIZ];
282 	pid_t cpid;
283 	int sent, succ, i;
284 
285 	cpid = fork();
286 	rump_init();
287 	netcfg_rump_makeshmif("jippikaiee", ifname);
288 
289 	switch (cpid) {
290 	case -1:
291 		atf_tc_fail_errno("fork failed");
292 	case 0:
293 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
294 		pause();
295 		break;
296 	default:
297 		break;
298 	}
299 
300 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
301 
302 	succ = sent = 0;
303 
304 	/* small sizes */
305 	for (i = 0 ; i < IP_MAXPACKET - 60000; i++) {
306 		sent++;
307 		succ += doping("1.1.1.10", 1, i);
308 	}
309 
310 	/* medium sizes */
311 	for (i = IP_MAXPACKET - 60000; i < IP_MAXPACKET - 100; i += 1000) {
312 		sent++;
313 		succ += doping("1.1.1.10", 1, i);
314 	}
315 
316 	/* big sizes */
317 	for (i = IP_MAXPACKET - 100; i < IP_MAXPACKET; i += 10) {
318 		sent++;
319 		succ += doping("1.1.1.10", 1, i);
320 	}
321 
322 	printf("got %d/%d\n", succ, sent);
323 	kill(cpid, SIGKILL);
324 }
325 
326 ATF_TC(ping_of_death);
ATF_TC_HEAD(ping_of_death,tc)327 ATF_TC_HEAD(ping_of_death, tc)
328 {
329 
330 	atf_tc_set_md_var(tc, "descr", "send a \"ping of death\"");
331 	atf_tc_set_md_var(tc, "timeout", "20");
332 }
333 
ATF_TC_BODY(ping_of_death,tc)334 ATF_TC_BODY(ping_of_death, tc)
335 {
336 	char data[1500];
337 	struct sockaddr_in dst;
338 	struct ip *ip;
339 	struct icmp *icmp;
340 	char ifname[IFNAMSIZ];
341 	pid_t cpid;
342 	size_t tot, frag;
343 	int s, x, loop;
344 	ssize_t error;
345 
346 	cpid = fork();
347 	rump_init();
348 	netcfg_rump_makeshmif("jippikaiee", ifname);
349 
350 	switch (cpid) {
351 	case -1:
352 		atf_tc_fail_errno("fork failed");
353 	case 0:
354 		/* wait until we receive a too long IP packet */
355 		for (loop = 0;; loop++) {
356 			uint64_t ipstat[IP_NSTATS];
357 			size_t arglen;
358 			int mib[4];
359 
360 			if (loop == 1)
361 				netcfg_rump_if(ifname,
362 				    "1.1.1.10", "255.255.255.0");
363 
364 			mib[0] = CTL_NET;
365 			mib[1] = PF_INET;
366 			mib[2] = IPPROTO_IP;
367 			mib[3] = IPCTL_STATS;
368 
369 			arglen = sizeof(ipstat);
370 			RL(rump_sys___sysctl(mib, 4, &ipstat, &arglen,
371 			    NULL, 0));
372 			if (loop == 0 && ipstat[IP_STAT_TOOLONG] != 0)
373 				_exit(1);
374 			if (ipstat[IP_STAT_TOOLONG])
375 				break;
376 			usleep(10000);
377 		}
378 
379 		_exit(0);
380 		break;
381 	default:
382 		break;
383 	}
384 
385 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
386 
387 	RL(s = rump_sys_socket(PF_INET, SOCK_RAW, 0));
388 	x = 1;
389 	RL(rump_sys_setsockopt(s, IPPROTO_IP, IP_HDRINCL, &x, sizeof(x)));
390 
391 	memset(&dst, 0, sizeof(dst));
392 	dst.sin_len = sizeof(dst);
393 	dst.sin_family = AF_INET;
394 	dst.sin_addr.s_addr = inet_addr("1.1.1.10");
395 
396 	/* construct packet */
397 	memset(data, 0, sizeof(data));
398 	ip = (struct ip *)data;
399 	ip->ip_v = 4;
400 	ip->ip_hl = sizeof(*ip) >> 2;
401 	ip->ip_p = IPPROTO_ICMP;
402 	ip->ip_ttl = IPDEFTTL;
403 	ip->ip_dst = dst.sin_addr;
404 	ip->ip_id = 1234;
405 
406 	icmp = (struct icmp *)(ip + 1);
407 	icmp->icmp_type = ICMP_ECHO;
408 	icmp->icmp_cksum = in_cksum(icmp, sizeof(*icmp));
409 
410 	for (;;) {
411 		int status;
412 
413 		/* resolve arp before sending raw stuff */
414 		netcfg_rump_pingtest("1.1.1.10", 1);
415 
416 		for (tot = 0;
417 		    tot < 65538 - sizeof(*ip);
418 		    tot += (frag - sizeof(*ip))) {
419 			frag = MIN(65538 - tot, sizeof(data));
420 			ip->ip_off = tot >> 3;
421 			assert((size_t)ip->ip_off << 3 == tot);
422 			ip->ip_len = frag;
423 
424 			if (frag == sizeof(data)) {
425 				ip->ip_off |= IP_MF;
426 			}
427 
428 			error = rump_sys_sendto(s, data, frag, 0,
429 			    (struct sockaddr *)&dst, sizeof(dst));
430 			if (error == -1) {
431 				if (errno == ENOBUFS)
432 					continue;
433 				atf_tc_fail_errno("sendto failed");
434 			}
435 			if ((size_t)error != frag)
436 				atf_tc_fail("sendto did not write all data");
437 		}
438 		if (waitpid(-1, &status, WNOHANG) > 0) {
439 			if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
440 				break;
441 			atf_tc_fail("child did not exit clean");
442 		}
443 
444 		usleep(10000);
445 	}
446 }
447 
ATF_TP_ADD_TCS(tp)448 ATF_TP_ADD_TCS(tp)
449 {
450 
451 	ATF_TP_ADD_TC(tp, simpleping);
452 	ATF_TP_ADD_TC(tp, floodping);
453 	ATF_TP_ADD_TC(tp, floodping2);
454 	ATF_TP_ADD_TC(tp, pingsize);
455 	ATF_TP_ADD_TC(tp, ping_of_death);
456 
457 	return atf_no_error();
458 }
459