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