1 /* $NetBSD: t_ping.c,v 1.15 2012/09/04 22:31:58 alnsn Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_ping.c,v 1.15 2012/09/04 22:31:58 alnsn Exp $");
35 #include <sys/types.h>
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
49 #include <netinet/in.h>
50 #include <netinet/ip_var.h>
52 #include <rump/rump.h>
53 #include <rump/rump_syscalls.h>
55 #include "../../h_macros.h"
56 #include "../config/netconfig.c"
59 ATF_TC_HEAD(simpleping, tc)
62 atf_tc_set_md_var(tc, "descr", "check that kernel responds to ping");
63 atf_tc_set_md_var(tc, "timeout", "2");
66 ATF_TC_BODY(simpleping, tc)
68 char ifname[IFNAMSIZ];
78 netcfg_rump_makeshmif("but-can-i-buy-your-ether-bus", ifname);
82 atf_tc_fail_errno("fork failed");
84 netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
86 ATF_CHECK(write(channel[1], "U", 1) == 1);
95 ATF_CHECK(read(channel[0], &token, 1) == 1 && token == 'U');
98 netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
101 * The beauty of shmif is that we don't have races here.
103 win = netcfg_rump_pingtest("1.1.1.10", 500);
104 win2 = netcfg_rump_pingtest("1.1.1.30", 500);
109 atf_tc_fail("ping failed");
111 atf_tc_fail("non-existent host responded");
115 ATF_TC_HEAD(floodping, tc)
118 atf_tc_set_md_var(tc, "descr", "see how kernel responds to floodping");
121 /* why the hell isn't this available in userspace??? */
123 in_cksum(void *data, size_t len)
125 uint16_t *buf = data;
128 for (sum = 0; len > 1; len -= 2)
131 sum += *(uint8_t *)buf;
133 sum = (sum >> 16) + (sum & 0xffff);
140 doping(const char *target, int loops, u_int pktsize)
143 char buf[IP_MAXPACKET - sizeof(struct ip)];
144 struct icmp i; /* ensure proper alignment */
146 char recvbuf[IP_MAXPACKET];
147 struct sockaddr_in dst, pingee;
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;
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);
163 icmp = (struct icmp *)&sndbuf;
164 memset(icmp, 0, sizeof(*icmp));
165 icmp->icmp_type = ICMP_ECHO;
166 icmp->icmp_id = htons(37);
168 if (pktsize < sizeof(*icmp))
169 pktsize = sizeof(*icmp);
170 if (pktsize > sizeof(sndbuf.buf))
171 pktsize = sizeof(sndbuf.buf);
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)));
178 slen = sizeof(pingee);
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 RL(rump_sys_sendto(s, icmp, pktsize, 0,
186 (struct sockaddr *)&dst, sizeof(dst)));
188 RL(rump_sys_fcntl(s, F_SETFL, xnon));
189 while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
190 (struct sockaddr *)&pingee, &slen)) > 0) {
193 if (n == -1 && errno == EAGAIN)
195 atf_tc_fail_errno("recv failed");
204 ATF_TC_BODY(floodping, tc)
206 char ifname[IFNAMSIZ];
212 netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
216 atf_tc_fail_errno("fork failed");
218 netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
225 netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
227 succ = doping("1.1.1.10", LOOPS, 56);
228 printf("got %d/%d\n", succ, LOOPS);
234 ATF_TC_HEAD(floodping2, tc)
237 atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
240 ATF_TC_BODY(floodping2, tc)
242 char ifname[IFNAMSIZ];
248 netcfg_rump_makeshmif("floodping2", ifname);
252 atf_tc_fail_errno("fork failed");
254 netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
255 succ = doping("1.1.1.20", LOOPS, 56);
258 netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
259 succ = doping("1.1.1.10", LOOPS, 56);
263 printf("got %d/%d\n", succ, LOOPS);
267 ATF_TC_HEAD(pingsize, tc)
270 atf_tc_set_md_var(tc, "descr", "ping with packets min <= size <= max");
273 ATF_TC_BODY(pingsize, tc)
275 char ifname[IFNAMSIZ];
281 netcfg_rump_makeshmif("jippikaiee", ifname);
285 atf_tc_fail_errno("fork failed");
287 netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
294 netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
299 for (i = 0 ; i < IP_MAXPACKET - 60000; i++)
300 succ += doping("1.1.1.10", 1, i);
303 for (i = IP_MAXPACKET - 60000; i < IP_MAXPACKET - 100; i += 1000)
304 succ += doping("1.1.1.10", 1, i);
307 for (i = IP_MAXPACKET - 100; i < IP_MAXPACKET; i += 10)
308 succ += doping("1.1.1.10", 1, i);
310 printf("got %d/%d\n", succ, IP_MAXPACKET);
314 ATF_TC(ping_of_death);
315 ATF_TC_HEAD(ping_of_death, tc)
318 atf_tc_set_md_var(tc, "descr", "send a \"ping of death\"");
319 atf_tc_set_md_var(tc, "timeout", "2");
322 ATF_TC_BODY(ping_of_death, tc)
325 struct sockaddr_in dst;
328 char ifname[IFNAMSIZ];
335 netcfg_rump_makeshmif("jippikaiee", ifname);
339 atf_tc_fail_errno("fork failed");
341 /* wait until we receive a too long IP packet */
342 for (loop = 0;; loop++) {
343 uint64_t ipstat[IP_NSTATS];
348 netcfg_rump_if(ifname,
349 "1.1.1.10", "255.255.255.0");
354 mib[3] = IPCTL_STATS;
356 arglen = sizeof(ipstat);
357 RL(rump_sys___sysctl(mib, 4, &ipstat, &arglen,
359 if (loop == 0 && ipstat[IP_STAT_TOOLONG] != 0)
361 if (ipstat[IP_STAT_TOOLONG])
372 netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
374 RL(s = rump_sys_socket(PF_INET, SOCK_RAW, 0));
376 RL(rump_sys_setsockopt(s, IPPROTO_IP, IP_HDRINCL, &x, sizeof(x)));
378 memset(&dst, 0, sizeof(dst));
379 dst.sin_len = sizeof(dst);
380 dst.sin_family = AF_INET;
381 dst.sin_addr.s_addr = inet_addr("1.1.1.10");
383 /* construct packet */
384 memset(data, 0, sizeof(data));
385 ip = (struct ip *)data;
387 ip->ip_hl = sizeof(*ip) >> 2;
388 ip->ip_p = IPPROTO_ICMP;
389 ip->ip_ttl = IPDEFTTL;
390 ip->ip_dst = dst.sin_addr;
393 icmp = (struct icmp *)(ip + 1);
394 icmp->icmp_type = ICMP_ECHO;
395 icmp->icmp_cksum = in_cksum(icmp, sizeof(*icmp));
400 /* resolve arp before sending raw stuff */
401 netcfg_rump_pingtest("1.1.1.10", 1);
404 tot < 65538 - sizeof(*ip);
405 tot += (frag - sizeof(*ip))) {
406 frag = MIN(65538 - tot, sizeof(data));
407 ip->ip_off = tot >> 3;
408 assert((size_t)ip->ip_off << 3 == tot);
411 if (frag == sizeof(data)) {
415 RL(rump_sys_sendto(s, data, frag, 0,
416 (struct sockaddr *)&dst, sizeof(dst)));
418 if (waitpid(-1, &status, WNOHANG) > 0) {
419 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
421 atf_tc_fail("child did not exit clean");
431 ATF_TP_ADD_TC(tp, simpleping);
432 ATF_TP_ADD_TC(tp, floodping);
433 ATF_TP_ADD_TC(tp, floodping2);
434 ATF_TP_ADD_TC(tp, pingsize);
435 ATF_TP_ADD_TC(tp, ping_of_death);
437 return atf_no_error();