xref: /minix3/tests/net/icmp/t_ping.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_ping.c,v 1.16 2015/02/26 13:06:10 martin Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc  *
1611be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc  */
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc #include <sys/cdefs.h>
3111be35a1SLionel Sambuc #ifndef lint
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_ping.c,v 1.16 2015/02/26 13:06:10 martin Exp $");
3311be35a1SLionel Sambuc #endif /* not lint */
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include <sys/types.h>
3611be35a1SLionel Sambuc #include <sys/resource.h>
3711be35a1SLionel Sambuc #include <sys/sysctl.h>
3811be35a1SLionel Sambuc #include <sys/wait.h>
3911be35a1SLionel Sambuc 
4011be35a1SLionel Sambuc #include <atf-c.h>
4111be35a1SLionel Sambuc #include <assert.h>
4211be35a1SLionel Sambuc #include <fcntl.h>
4311be35a1SLionel Sambuc #include <stdio.h>
4411be35a1SLionel Sambuc #include <stdlib.h>
4511be35a1SLionel Sambuc #include <string.h>
4611be35a1SLionel Sambuc #include <unistd.h>
4711be35a1SLionel Sambuc #include <signal.h>
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc #include <netinet/in.h>
5011be35a1SLionel Sambuc #include <netinet/ip_var.h>
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc #include <rump/rump.h>
5311be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc #include "../../h_macros.h"
5611be35a1SLionel Sambuc #include "../config/netconfig.c"
5711be35a1SLionel Sambuc 
5811be35a1SLionel Sambuc ATF_TC(simpleping);
ATF_TC_HEAD(simpleping,tc)5911be35a1SLionel Sambuc ATF_TC_HEAD(simpleping, tc)
6011be35a1SLionel Sambuc {
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "check that kernel responds to ping");
63*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "timeout", "20");
6411be35a1SLionel Sambuc }
6511be35a1SLionel Sambuc 
ATF_TC_BODY(simpleping,tc)6611be35a1SLionel Sambuc ATF_TC_BODY(simpleping, tc)
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
6911be35a1SLionel Sambuc 	pid_t cpid;
7011be35a1SLionel Sambuc 	bool win, win2;
7111be35a1SLionel Sambuc 	char token;
7211be35a1SLionel Sambuc 	int channel[2];
7311be35a1SLionel Sambuc 
7411be35a1SLionel Sambuc 	RL(pipe(channel));
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc 	cpid = fork();
7711be35a1SLionel Sambuc 	rump_init();
7811be35a1SLionel Sambuc 	netcfg_rump_makeshmif("but-can-i-buy-your-ether-bus", ifname);
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc 	switch (cpid) {
8111be35a1SLionel Sambuc 	case -1:
8211be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
8311be35a1SLionel Sambuc 	case 0:
8411be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
8511be35a1SLionel Sambuc 		close(channel[0]);
8611be35a1SLionel Sambuc 		ATF_CHECK(write(channel[1], "U", 1) == 1);
8711be35a1SLionel Sambuc 		close(channel[1]);
8811be35a1SLionel Sambuc 		pause();
8911be35a1SLionel Sambuc 		break;
9011be35a1SLionel Sambuc 	default:
9111be35a1SLionel Sambuc 		break;
9211be35a1SLionel Sambuc 	}
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc 	close(channel[1]);
9511be35a1SLionel Sambuc 	ATF_CHECK(read(channel[0], &token, 1) == 1 && token == 'U');
9611be35a1SLionel Sambuc 	close(channel[0]);
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc 	/*
10111be35a1SLionel Sambuc 	 * The beauty of shmif is that we don't have races here.
10211be35a1SLionel Sambuc 	 */
10311be35a1SLionel Sambuc 	win = netcfg_rump_pingtest("1.1.1.10", 500);
10411be35a1SLionel Sambuc 	win2 = netcfg_rump_pingtest("1.1.1.30", 500);
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc 	kill(cpid, SIGKILL);
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 	if (!win)
10911be35a1SLionel Sambuc 		atf_tc_fail("ping failed");
11011be35a1SLionel Sambuc 	if (win2)
11111be35a1SLionel Sambuc 		atf_tc_fail("non-existent host responded");
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc ATF_TC(floodping);
ATF_TC_HEAD(floodping,tc)11511be35a1SLionel Sambuc ATF_TC_HEAD(floodping, tc)
11611be35a1SLionel Sambuc {
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "see how kernel responds to floodping");
11911be35a1SLionel Sambuc }
12011be35a1SLionel Sambuc 
12111be35a1SLionel Sambuc /* why the hell isn't this available in userspace??? */
12211be35a1SLionel Sambuc static uint16_t
in_cksum(void * data,size_t len)12311be35a1SLionel Sambuc in_cksum(void *data, size_t len)
12411be35a1SLionel Sambuc {
12511be35a1SLionel Sambuc 	uint16_t *buf = data;
12611be35a1SLionel Sambuc 	unsigned sum;
12711be35a1SLionel Sambuc 
12811be35a1SLionel Sambuc 	for (sum = 0; len > 1; len -= 2)
12911be35a1SLionel Sambuc 		sum += *buf++;
13011be35a1SLionel Sambuc 	if (len)
13111be35a1SLionel Sambuc 		sum += *(uint8_t *)buf;
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc 	sum = (sum >> 16) + (sum & 0xffff);
13411be35a1SLionel Sambuc 	sum += (sum >> 16);
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc 	return ~sum;
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc 
13911be35a1SLionel Sambuc static int
doping(const char * target,int loops,u_int pktsize)14011be35a1SLionel Sambuc doping(const char *target, int loops, u_int pktsize)
14111be35a1SLionel Sambuc {
14211be35a1SLionel Sambuc 	union {
14311be35a1SLionel Sambuc 		char buf[IP_MAXPACKET - sizeof(struct ip)];
14411be35a1SLionel Sambuc 		struct icmp i;	/* ensure proper alignment */
14511be35a1SLionel Sambuc 	} sndbuf;
14611be35a1SLionel Sambuc 	char recvbuf[IP_MAXPACKET];
14711be35a1SLionel Sambuc 	struct sockaddr_in dst, pingee;
14811be35a1SLionel Sambuc 	struct icmp *icmp;
14911be35a1SLionel Sambuc 	socklen_t slen;
15011be35a1SLionel Sambuc 	ssize_t n;
15111be35a1SLionel Sambuc 	int loop, succ;
15211be35a1SLionel Sambuc 	int x, xnon, s;
15311be35a1SLionel Sambuc 
15411be35a1SLionel Sambuc 	RL(s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP));
15511be35a1SLionel Sambuc 	RL(x = rump_sys_fcntl(s, F_GETFL, 0));
15611be35a1SLionel Sambuc 	xnon = x | O_NONBLOCK;
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc 	memset(&dst, 0, sizeof(dst));
15911be35a1SLionel Sambuc 	dst.sin_len = sizeof(dst);
16011be35a1SLionel Sambuc 	dst.sin_family = AF_INET;
16111be35a1SLionel Sambuc 	dst.sin_addr.s_addr = inet_addr(target);
16211be35a1SLionel Sambuc 
16311be35a1SLionel Sambuc 	icmp = (struct icmp *)&sndbuf;
16411be35a1SLionel Sambuc 	memset(icmp, 0, sizeof(*icmp));
16511be35a1SLionel Sambuc 	icmp->icmp_type = ICMP_ECHO;
16611be35a1SLionel Sambuc 	icmp->icmp_id = htons(37);
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc 	if (pktsize < sizeof(*icmp))
16911be35a1SLionel Sambuc 		pktsize = sizeof(*icmp);
17011be35a1SLionel Sambuc 	if (pktsize > sizeof(sndbuf.buf))
17111be35a1SLionel Sambuc 		pktsize = sizeof(sndbuf.buf);
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
17411be35a1SLionel Sambuc 	    &pktsize, sizeof(pktsize)));
17511be35a1SLionel Sambuc 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
17611be35a1SLionel Sambuc 	    &pktsize, sizeof(pktsize)));
17711be35a1SLionel Sambuc 
17811be35a1SLionel Sambuc 	slen = sizeof(pingee);
17911be35a1SLionel Sambuc 	succ = 0;
18011be35a1SLionel Sambuc 	for (loop = 0; loop < loops; loop++) {
18111be35a1SLionel Sambuc 		RL(rump_sys_fcntl(s, F_SETFL, x));
18211be35a1SLionel Sambuc 		icmp->icmp_seq = htons(loop);
18311be35a1SLionel Sambuc 		icmp->icmp_cksum = 0;
18411be35a1SLionel Sambuc 		icmp->icmp_cksum = in_cksum(icmp, pktsize);
18511be35a1SLionel Sambuc 		RL(rump_sys_sendto(s, icmp, pktsize, 0,
18611be35a1SLionel Sambuc 		    (struct sockaddr *)&dst, sizeof(dst)));
18711be35a1SLionel Sambuc 
18811be35a1SLionel Sambuc 		RL(rump_sys_fcntl(s, F_SETFL, xnon));
18911be35a1SLionel Sambuc 		while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
19011be35a1SLionel Sambuc 		    (struct sockaddr *)&pingee, &slen)) > 0) {
19111be35a1SLionel Sambuc 			succ++;
19211be35a1SLionel Sambuc 		}
19311be35a1SLionel Sambuc 		if (n == -1 && errno == EAGAIN)
19411be35a1SLionel Sambuc 			continue;
19511be35a1SLionel Sambuc 		atf_tc_fail_errno("recv failed");
19611be35a1SLionel Sambuc 	}
19711be35a1SLionel Sambuc 
19811be35a1SLionel Sambuc 	rump_sys_close(s);
19911be35a1SLionel Sambuc 	return succ;
20011be35a1SLionel Sambuc }
20111be35a1SLionel Sambuc 
20211be35a1SLionel Sambuc #define LOOPS 10000
20311be35a1SLionel Sambuc 
ATF_TC_BODY(floodping,tc)20411be35a1SLionel Sambuc ATF_TC_BODY(floodping, tc)
20511be35a1SLionel Sambuc {
20611be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
20711be35a1SLionel Sambuc 	pid_t cpid;
20811be35a1SLionel Sambuc 	int succ;
20911be35a1SLionel Sambuc 
21011be35a1SLionel Sambuc 	cpid = fork();
21111be35a1SLionel Sambuc 	rump_init();
21211be35a1SLionel Sambuc 	netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
21311be35a1SLionel Sambuc 
21411be35a1SLionel Sambuc 	switch (cpid) {
21511be35a1SLionel Sambuc 	case -1:
21611be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
21711be35a1SLionel Sambuc 	case 0:
21811be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
21911be35a1SLionel Sambuc 		pause();
22011be35a1SLionel Sambuc 		break;
22111be35a1SLionel Sambuc 	default:
22211be35a1SLionel Sambuc 		break;
22311be35a1SLionel Sambuc 	}
22411be35a1SLionel Sambuc 
22511be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc 	succ = doping("1.1.1.10", LOOPS, 56);
22811be35a1SLionel Sambuc 	printf("got %d/%d\n", succ, LOOPS);
22911be35a1SLionel Sambuc 
23011be35a1SLionel Sambuc 	kill(cpid, SIGKILL);
23111be35a1SLionel Sambuc }
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc ATF_TC(floodping2);
ATF_TC_HEAD(floodping2,tc)23411be35a1SLionel Sambuc ATF_TC_HEAD(floodping2, tc)
23511be35a1SLionel Sambuc {
23611be35a1SLionel Sambuc 
23711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
23811be35a1SLionel Sambuc }
23911be35a1SLionel Sambuc 
ATF_TC_BODY(floodping2,tc)24011be35a1SLionel Sambuc ATF_TC_BODY(floodping2, tc)
24111be35a1SLionel Sambuc {
24211be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
24311be35a1SLionel Sambuc 	pid_t cpid;
24411be35a1SLionel Sambuc 	int succ;
24511be35a1SLionel Sambuc 
24611be35a1SLionel Sambuc 	cpid = fork();
24711be35a1SLionel Sambuc 	rump_init();
24811be35a1SLionel Sambuc 	netcfg_rump_makeshmif("floodping2", ifname);
24911be35a1SLionel Sambuc 
25011be35a1SLionel Sambuc 	switch (cpid) {
25111be35a1SLionel Sambuc 	case -1:
25211be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
25311be35a1SLionel Sambuc 	case 0:
25411be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
25511be35a1SLionel Sambuc 		succ = doping("1.1.1.20", LOOPS, 56);
25611be35a1SLionel Sambuc 		break;
25711be35a1SLionel Sambuc 	default:
25811be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
25911be35a1SLionel Sambuc 		succ = doping("1.1.1.10", LOOPS, 56);
26011be35a1SLionel Sambuc 		break;
26111be35a1SLionel Sambuc 	}
26211be35a1SLionel Sambuc 
26311be35a1SLionel Sambuc 	printf("got %d/%d\n", succ, LOOPS);
26411be35a1SLionel Sambuc }
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc ATF_TC(pingsize);
ATF_TC_HEAD(pingsize,tc)26711be35a1SLionel Sambuc ATF_TC_HEAD(pingsize, tc)
26811be35a1SLionel Sambuc {
26911be35a1SLionel Sambuc 
27011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "ping with packets min <= size <= max");
27111be35a1SLionel Sambuc }
27211be35a1SLionel Sambuc 
ATF_TC_BODY(pingsize,tc)27311be35a1SLionel Sambuc ATF_TC_BODY(pingsize, tc)
27411be35a1SLionel Sambuc {
27511be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
27611be35a1SLionel Sambuc 	pid_t cpid;
27711be35a1SLionel Sambuc 	int succ, i;
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc 	cpid = fork();
28011be35a1SLionel Sambuc 	rump_init();
28111be35a1SLionel Sambuc 	netcfg_rump_makeshmif("jippikaiee", ifname);
28211be35a1SLionel Sambuc 
28311be35a1SLionel Sambuc 	switch (cpid) {
28411be35a1SLionel Sambuc 	case -1:
28511be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
28611be35a1SLionel Sambuc 	case 0:
28711be35a1SLionel Sambuc 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
28811be35a1SLionel Sambuc 		pause();
28911be35a1SLionel Sambuc 		break;
29011be35a1SLionel Sambuc 	default:
29111be35a1SLionel Sambuc 		break;
29211be35a1SLionel Sambuc 	}
29311be35a1SLionel Sambuc 
29411be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
29511be35a1SLionel Sambuc 
29611be35a1SLionel Sambuc 	succ = 0;
29711be35a1SLionel Sambuc 
29811be35a1SLionel Sambuc 	/* small sizes */
29911be35a1SLionel Sambuc 	for (i = 0 ; i < IP_MAXPACKET - 60000; i++)
30011be35a1SLionel Sambuc 		succ += doping("1.1.1.10", 1, i);
30111be35a1SLionel Sambuc 
30211be35a1SLionel Sambuc 	/* medium sizes */
30311be35a1SLionel Sambuc 	for (i = IP_MAXPACKET - 60000; i < IP_MAXPACKET - 100; i += 1000)
30411be35a1SLionel Sambuc 		succ += doping("1.1.1.10", 1, i);
30511be35a1SLionel Sambuc 
30611be35a1SLionel Sambuc 	/* big sizes */
30711be35a1SLionel Sambuc 	for (i = IP_MAXPACKET - 100; i < IP_MAXPACKET; i += 10)
30811be35a1SLionel Sambuc 		succ += doping("1.1.1.10", 1, i);
30911be35a1SLionel Sambuc 
31011be35a1SLionel Sambuc 	printf("got %d/%d\n", succ, IP_MAXPACKET);
31111be35a1SLionel Sambuc 	kill(cpid, SIGKILL);
31211be35a1SLionel Sambuc }
31311be35a1SLionel Sambuc 
31411be35a1SLionel Sambuc ATF_TC(ping_of_death);
ATF_TC_HEAD(ping_of_death,tc)31511be35a1SLionel Sambuc ATF_TC_HEAD(ping_of_death, tc)
31611be35a1SLionel Sambuc {
31711be35a1SLionel Sambuc 
31811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "send a \"ping of death\"");
319*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "timeout", "20");
32011be35a1SLionel Sambuc }
32111be35a1SLionel Sambuc 
ATF_TC_BODY(ping_of_death,tc)32211be35a1SLionel Sambuc ATF_TC_BODY(ping_of_death, tc)
32311be35a1SLionel Sambuc {
32411be35a1SLionel Sambuc 	char data[1500];
32511be35a1SLionel Sambuc 	struct sockaddr_in dst;
32611be35a1SLionel Sambuc 	struct ip *ip;
32711be35a1SLionel Sambuc 	struct icmp *icmp;
32811be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
32911be35a1SLionel Sambuc 	pid_t cpid;
33011be35a1SLionel Sambuc 	size_t tot, frag;
33111be35a1SLionel Sambuc 	int s, x, loop;
33211be35a1SLionel Sambuc 
33311be35a1SLionel Sambuc 	cpid = fork();
33411be35a1SLionel Sambuc 	rump_init();
33511be35a1SLionel Sambuc 	netcfg_rump_makeshmif("jippikaiee", ifname);
33611be35a1SLionel Sambuc 
33711be35a1SLionel Sambuc 	switch (cpid) {
33811be35a1SLionel Sambuc 	case -1:
33911be35a1SLionel Sambuc 		atf_tc_fail_errno("fork failed");
34011be35a1SLionel Sambuc 	case 0:
34111be35a1SLionel Sambuc 		/* wait until we receive a too long IP packet */
34211be35a1SLionel Sambuc 		for (loop = 0;; loop++) {
34311be35a1SLionel Sambuc 			uint64_t ipstat[IP_NSTATS];
34411be35a1SLionel Sambuc 			size_t arglen;
34511be35a1SLionel Sambuc 			int mib[4];
34611be35a1SLionel Sambuc 
34711be35a1SLionel Sambuc 			if (loop == 1)
34811be35a1SLionel Sambuc 				netcfg_rump_if(ifname,
34911be35a1SLionel Sambuc 				    "1.1.1.10", "255.255.255.0");
35011be35a1SLionel Sambuc 
35111be35a1SLionel Sambuc 			mib[0] = CTL_NET;
35211be35a1SLionel Sambuc 			mib[1] = PF_INET;
35311be35a1SLionel Sambuc 			mib[2] = IPPROTO_IP;
35411be35a1SLionel Sambuc 			mib[3] = IPCTL_STATS;
35511be35a1SLionel Sambuc 
35611be35a1SLionel Sambuc 			arglen = sizeof(ipstat);
35711be35a1SLionel Sambuc 			RL(rump_sys___sysctl(mib, 4, &ipstat, &arglen,
35811be35a1SLionel Sambuc 			    NULL, 0));
35911be35a1SLionel Sambuc 			if (loop == 0 && ipstat[IP_STAT_TOOLONG] != 0)
36011be35a1SLionel Sambuc 				_exit(1);
36111be35a1SLionel Sambuc 			if (ipstat[IP_STAT_TOOLONG])
36211be35a1SLionel Sambuc 				break;
36311be35a1SLionel Sambuc 			usleep(10000);
36411be35a1SLionel Sambuc 		}
36511be35a1SLionel Sambuc 
36611be35a1SLionel Sambuc 		_exit(0);
36711be35a1SLionel Sambuc 		break;
36811be35a1SLionel Sambuc 	default:
36911be35a1SLionel Sambuc 		break;
37011be35a1SLionel Sambuc 	}
37111be35a1SLionel Sambuc 
37211be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
37311be35a1SLionel Sambuc 
37411be35a1SLionel Sambuc 	RL(s = rump_sys_socket(PF_INET, SOCK_RAW, 0));
37511be35a1SLionel Sambuc 	x = 1;
37611be35a1SLionel Sambuc 	RL(rump_sys_setsockopt(s, IPPROTO_IP, IP_HDRINCL, &x, sizeof(x)));
37711be35a1SLionel Sambuc 
37811be35a1SLionel Sambuc 	memset(&dst, 0, sizeof(dst));
37911be35a1SLionel Sambuc 	dst.sin_len = sizeof(dst);
38011be35a1SLionel Sambuc 	dst.sin_family = AF_INET;
38111be35a1SLionel Sambuc 	dst.sin_addr.s_addr = inet_addr("1.1.1.10");
38211be35a1SLionel Sambuc 
38311be35a1SLionel Sambuc 	/* construct packet */
38411be35a1SLionel Sambuc 	memset(data, 0, sizeof(data));
38511be35a1SLionel Sambuc 	ip = (struct ip *)data;
38611be35a1SLionel Sambuc 	ip->ip_v = 4;
38711be35a1SLionel Sambuc 	ip->ip_hl = sizeof(*ip) >> 2;
38811be35a1SLionel Sambuc 	ip->ip_p = IPPROTO_ICMP;
38911be35a1SLionel Sambuc 	ip->ip_ttl = IPDEFTTL;
39011be35a1SLionel Sambuc 	ip->ip_dst = dst.sin_addr;
39111be35a1SLionel Sambuc 	ip->ip_id = 1234;
39211be35a1SLionel Sambuc 
39311be35a1SLionel Sambuc 	icmp = (struct icmp *)(ip + 1);
39411be35a1SLionel Sambuc 	icmp->icmp_type = ICMP_ECHO;
39511be35a1SLionel Sambuc 	icmp->icmp_cksum = in_cksum(icmp, sizeof(*icmp));
39611be35a1SLionel Sambuc 
39711be35a1SLionel Sambuc 	for (;;) {
39811be35a1SLionel Sambuc 		int status;
39911be35a1SLionel Sambuc 
40011be35a1SLionel Sambuc 		/* resolve arp before sending raw stuff */
40111be35a1SLionel Sambuc 		netcfg_rump_pingtest("1.1.1.10", 1);
40211be35a1SLionel Sambuc 
40311be35a1SLionel Sambuc 		for (tot = 0;
40411be35a1SLionel Sambuc 		    tot < 65538 - sizeof(*ip);
40511be35a1SLionel Sambuc 		    tot += (frag - sizeof(*ip))) {
40611be35a1SLionel Sambuc 			frag = MIN(65538 - tot, sizeof(data));
40711be35a1SLionel Sambuc 			ip->ip_off = tot >> 3;
40811be35a1SLionel Sambuc 			assert((size_t)ip->ip_off << 3 == tot);
40911be35a1SLionel Sambuc 			ip->ip_len = frag;
41011be35a1SLionel Sambuc 
41111be35a1SLionel Sambuc 			if (frag == sizeof(data)) {
41211be35a1SLionel Sambuc 				ip->ip_off |= IP_MF;
41311be35a1SLionel Sambuc 			}
41411be35a1SLionel Sambuc 
41511be35a1SLionel Sambuc 			RL(rump_sys_sendto(s, data, frag, 0,
41611be35a1SLionel Sambuc 			    (struct sockaddr *)&dst, sizeof(dst)));
41711be35a1SLionel Sambuc 		}
41811be35a1SLionel Sambuc 		if (waitpid(-1, &status, WNOHANG) > 0) {
41911be35a1SLionel Sambuc 			if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
42011be35a1SLionel Sambuc 				break;
42111be35a1SLionel Sambuc 			atf_tc_fail("child did not exit clean");
42211be35a1SLionel Sambuc 		}
42311be35a1SLionel Sambuc 
42411be35a1SLionel Sambuc 		usleep(10000);
42511be35a1SLionel Sambuc 	}
42611be35a1SLionel Sambuc }
42711be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)42811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
42911be35a1SLionel Sambuc {
43011be35a1SLionel Sambuc 
43111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, simpleping);
43211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, floodping);
43311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, floodping2);
43411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, pingsize);
43511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, ping_of_death);
43611be35a1SLionel Sambuc 
43711be35a1SLionel Sambuc 	return atf_no_error();
43811be35a1SLionel Sambuc }
439