t_ping.c revision 1.14 1 1.14 christos /* $NetBSD: t_ping.c,v 1.14 2011/06/26 13:15:22 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.14 christos __RCSID("$NetBSD: t_ping.c,v 1.14 2011/06/26 13:15:22 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.14 christos #include <signal.h>
48 1.1 pooka
49 1.5 pooka #include <netinet/in.h>
50 1.5 pooka #include <netinet/ip_var.h>
51 1.5 pooka
52 1.1 pooka #include <rump/rump.h>
53 1.5 pooka #include <rump/rump_syscalls.h>
54 1.1 pooka
55 1.1 pooka #include "../../h_macros.h"
56 1.1 pooka #include "../config/netconfig.c"
57 1.1 pooka
58 1.1 pooka ATF_TC(simpleping);
59 1.1 pooka ATF_TC_HEAD(simpleping, tc)
60 1.1 pooka {
61 1.1 pooka
62 1.1 pooka atf_tc_set_md_var(tc, "descr", "check that kernel responds to ping");
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 }
111 1.2 pooka
112 1.3 pooka /* why the hell isn't this available in userspace??? */
113 1.3 pooka static uint16_t
114 1.3 pooka in_cksum(void *data, size_t len)
115 1.3 pooka {
116 1.3 pooka uint16_t *buf = data;
117 1.3 pooka unsigned sum;
118 1.3 pooka
119 1.3 pooka for (sum = 0; len > 1; len -= 2)
120 1.3 pooka sum += *buf++;
121 1.3 pooka if (len)
122 1.3 pooka sum += *(uint8_t *)buf;
123 1.3 pooka
124 1.3 pooka sum = (sum >> 16) + (sum & 0xffff);
125 1.3 pooka sum += (sum >> 16);
126 1.3 pooka
127 1.3 pooka return ~sum;
128 1.3 pooka }
129 1.3 pooka
130 1.3 pooka static int
131 1.10 christos doping(const char *target, int loops, u_int pktsize)
132 1.2 pooka {
133 1.12 martin union {
134 1.12 martin char buf[IP_MAXPACKET - sizeof(struct ip)];
135 1.12 martin struct icmp i; /* ensure proper alignment */
136 1.12 martin } sndbuf;
137 1.3 pooka char recvbuf[IP_MAXPACKET];
138 1.2 pooka struct sockaddr_in dst, pingee;
139 1.3 pooka struct icmp *icmp;
140 1.2 pooka socklen_t slen;
141 1.2 pooka ssize_t n;
142 1.9 christos int loop, succ;
143 1.2 pooka int x, xnon, s;
144 1.2 pooka
145 1.3 pooka RL(s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP));
146 1.3 pooka RL(x = rump_sys_fcntl(s, F_GETFL, 0));
147 1.3 pooka xnon = x | O_NONBLOCK;
148 1.3 pooka
149 1.3 pooka memset(&dst, 0, sizeof(dst));
150 1.3 pooka dst.sin_len = sizeof(dst);
151 1.3 pooka dst.sin_family = AF_INET;
152 1.3 pooka dst.sin_addr.s_addr = inet_addr(target);
153 1.3 pooka
154 1.12 martin icmp = (struct icmp *)&sndbuf;
155 1.3 pooka memset(icmp, 0, sizeof(*icmp));
156 1.3 pooka icmp->icmp_type = ICMP_ECHO;
157 1.3 pooka icmp->icmp_id = htons(37);
158 1.3 pooka
159 1.3 pooka if (pktsize < sizeof(*icmp))
160 1.3 pooka pktsize = sizeof(*icmp);
161 1.13 martin if (pktsize > sizeof(sndbuf.buf))
162 1.13 martin pktsize = sizeof(sndbuf.buf);
163 1.3 pooka
164 1.3 pooka RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
165 1.3 pooka &pktsize, sizeof(pktsize)));
166 1.3 pooka RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
167 1.3 pooka &pktsize, sizeof(pktsize)));
168 1.3 pooka
169 1.3 pooka slen = sizeof(pingee);
170 1.3 pooka succ = 0;
171 1.3 pooka for (loop = 0; loop < loops; loop++) {
172 1.3 pooka RL(rump_sys_fcntl(s, F_SETFL, x));
173 1.3 pooka icmp->icmp_seq = htons(loop);
174 1.3 pooka icmp->icmp_cksum = 0;
175 1.3 pooka icmp->icmp_cksum = in_cksum(icmp, pktsize);
176 1.3 pooka RL(rump_sys_sendto(s, icmp, pktsize, 0,
177 1.3 pooka (struct sockaddr *)&dst, sizeof(dst)));
178 1.3 pooka
179 1.3 pooka RL(rump_sys_fcntl(s, F_SETFL, xnon));
180 1.3 pooka while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
181 1.3 pooka (struct sockaddr *)&pingee, &slen)) > 0) {
182 1.3 pooka succ++;
183 1.3 pooka }
184 1.3 pooka if (n == -1 && errno == EAGAIN)
185 1.3 pooka continue;
186 1.3 pooka atf_tc_fail_errno("recv failed");
187 1.3 pooka }
188 1.3 pooka
189 1.3 pooka rump_sys_close(s);
190 1.3 pooka return succ;
191 1.3 pooka }
192 1.3 pooka
193 1.3 pooka #define LOOPS 10000
194 1.3 pooka
195 1.3 pooka ATF_TC_BODY(floodping, tc)
196 1.3 pooka {
197 1.3 pooka char ifname[IFNAMSIZ];
198 1.3 pooka pid_t cpid;
199 1.3 pooka int succ;
200 1.3 pooka
201 1.2 pooka cpid = fork();
202 1.2 pooka rump_init();
203 1.2 pooka netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
204 1.2 pooka
205 1.2 pooka switch (cpid) {
206 1.2 pooka case -1:
207 1.2 pooka atf_tc_fail_errno("fork failed");
208 1.2 pooka case 0:
209 1.2 pooka netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
210 1.2 pooka pause();
211 1.2 pooka break;
212 1.2 pooka default:
213 1.2 pooka break;
214 1.2 pooka }
215 1.2 pooka
216 1.2 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
217 1.2 pooka
218 1.3 pooka succ = doping("1.1.1.10", LOOPS, 56);
219 1.3 pooka printf("got %d/%d\n", succ, LOOPS);
220 1.3 pooka
221 1.3 pooka kill(cpid, SIGKILL);
222 1.3 pooka }
223 1.3 pooka
224 1.3 pooka ATF_TC(floodping2);
225 1.3 pooka ATF_TC_HEAD(floodping2, tc)
226 1.3 pooka {
227 1.3 pooka
228 1.3 pooka atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
229 1.3 pooka }
230 1.3 pooka
231 1.3 pooka ATF_TC_BODY(floodping2, tc)
232 1.3 pooka {
233 1.3 pooka char ifname[IFNAMSIZ];
234 1.3 pooka pid_t cpid;
235 1.3 pooka int succ;
236 1.3 pooka
237 1.3 pooka cpid = fork();
238 1.3 pooka rump_init();
239 1.3 pooka netcfg_rump_makeshmif("floodping2", ifname);
240 1.3 pooka
241 1.3 pooka switch (cpid) {
242 1.3 pooka case -1:
243 1.3 pooka atf_tc_fail_errno("fork failed");
244 1.3 pooka case 0:
245 1.3 pooka netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
246 1.3 pooka succ = doping("1.1.1.20", LOOPS, 56);
247 1.3 pooka break;
248 1.3 pooka default:
249 1.3 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
250 1.3 pooka succ = doping("1.1.1.10", LOOPS, 56);
251 1.3 pooka break;
252 1.3 pooka }
253 1.3 pooka
254 1.3 pooka printf("got %d/%d\n", succ, LOOPS);
255 1.3 pooka }
256 1.3 pooka
257 1.3 pooka ATF_TC(pingsize);
258 1.3 pooka ATF_TC_HEAD(pingsize, tc)
259 1.3 pooka {
260 1.3 pooka
261 1.3 pooka atf_tc_set_md_var(tc, "descr", "ping with packets min <= size <= max");
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.6 pooka atf_tc_set_md_var(tc, "timeout", "2");
311 1.5 pooka }
312 1.5 pooka
313 1.5 pooka ATF_TC_BODY(ping_of_death, tc)
314 1.5 pooka {
315 1.5 pooka char data[1500];
316 1.5 pooka struct sockaddr_in dst;
317 1.5 pooka struct ip *ip;
318 1.5 pooka struct icmp *icmp;
319 1.5 pooka char ifname[IFNAMSIZ];
320 1.5 pooka pid_t cpid;
321 1.5 pooka size_t tot, frag;
322 1.5 pooka int s, x, loop;
323 1.5 pooka
324 1.5 pooka cpid = fork();
325 1.5 pooka rump_init();
326 1.5 pooka netcfg_rump_makeshmif("jippikaiee", ifname);
327 1.5 pooka
328 1.5 pooka switch (cpid) {
329 1.5 pooka case -1:
330 1.5 pooka atf_tc_fail_errno("fork failed");
331 1.5 pooka case 0:
332 1.5 pooka /* wait until we receive a too long IP packet */
333 1.5 pooka for (loop = 0;; loop++) {
334 1.5 pooka uint64_t ipstat[IP_NSTATS];
335 1.5 pooka size_t arglen;
336 1.5 pooka int mib[4];
337 1.5 pooka
338 1.5 pooka if (loop == 1)
339 1.5 pooka netcfg_rump_if(ifname,
340 1.5 pooka "1.1.1.10", "255.255.255.0");
341 1.5 pooka
342 1.5 pooka mib[0] = CTL_NET;
343 1.5 pooka mib[1] = PF_INET;
344 1.5 pooka mib[2] = IPPROTO_IP;
345 1.5 pooka mib[3] = IPCTL_STATS;
346 1.5 pooka
347 1.5 pooka arglen = sizeof(ipstat);
348 1.5 pooka RL(rump_sys___sysctl(mib, 4, &ipstat, &arglen,
349 1.5 pooka NULL, 0));
350 1.5 pooka if (loop == 0 && ipstat[IP_STAT_TOOLONG] != 0)
351 1.5 pooka _exit(1);
352 1.5 pooka if (ipstat[IP_STAT_TOOLONG])
353 1.5 pooka break;
354 1.5 pooka usleep(10000);
355 1.5 pooka }
356 1.5 pooka
357 1.5 pooka _exit(0);
358 1.5 pooka break;
359 1.5 pooka default:
360 1.5 pooka break;
361 1.5 pooka }
362 1.5 pooka
363 1.5 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
364 1.5 pooka
365 1.5 pooka RL(s = rump_sys_socket(PF_INET, SOCK_RAW, 0));
366 1.5 pooka x = 1;
367 1.5 pooka RL(rump_sys_setsockopt(s, IPPROTO_IP, IP_HDRINCL, &x, sizeof(x)));
368 1.5 pooka
369 1.5 pooka memset(&dst, 0, sizeof(dst));
370 1.5 pooka dst.sin_len = sizeof(dst);
371 1.5 pooka dst.sin_family = AF_INET;
372 1.5 pooka dst.sin_addr.s_addr = inet_addr("1.1.1.10");
373 1.5 pooka
374 1.5 pooka /* construct packet */
375 1.5 pooka memset(data, 0, sizeof(data));
376 1.5 pooka ip = (struct ip *)data;
377 1.5 pooka ip->ip_v = 4;
378 1.5 pooka ip->ip_hl = sizeof(*ip) >> 2;
379 1.5 pooka ip->ip_p = IPPROTO_ICMP;
380 1.5 pooka ip->ip_ttl = IPDEFTTL;
381 1.5 pooka ip->ip_dst = dst.sin_addr;
382 1.5 pooka ip->ip_id = 1234;
383 1.5 pooka
384 1.5 pooka icmp = (struct icmp *)(ip + 1);
385 1.5 pooka icmp->icmp_type = ICMP_ECHO;
386 1.5 pooka icmp->icmp_cksum = in_cksum(icmp, sizeof(*icmp));
387 1.5 pooka
388 1.5 pooka for (;;) {
389 1.5 pooka int status;
390 1.5 pooka
391 1.5 pooka /* resolve arp before sending raw stuff */
392 1.5 pooka netcfg_rump_pingtest("1.1.1.10", 1);
393 1.5 pooka
394 1.5 pooka for (tot = 0;
395 1.5 pooka tot < 65538 - sizeof(*ip);
396 1.5 pooka tot += (frag - sizeof(*ip))) {
397 1.5 pooka frag = MIN(65538 - tot, sizeof(data));
398 1.5 pooka ip->ip_off = tot >> 3;
399 1.9 christos assert((size_t)ip->ip_off << 3 == tot);
400 1.5 pooka ip->ip_len = frag;
401 1.5 pooka
402 1.5 pooka if (frag == sizeof(data)) {
403 1.5 pooka ip->ip_off |= IP_MF;
404 1.5 pooka }
405 1.5 pooka
406 1.5 pooka RL(rump_sys_sendto(s, data, frag, 0,
407 1.5 pooka (struct sockaddr *)&dst, sizeof(dst)));
408 1.5 pooka }
409 1.5 pooka if (waitpid(-1, &status, WNOHANG) > 0) {
410 1.5 pooka if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
411 1.5 pooka break;
412 1.5 pooka atf_tc_fail("child did not exit clean");
413 1.5 pooka }
414 1.5 pooka
415 1.5 pooka usleep(10000);
416 1.5 pooka }
417 1.5 pooka }
418 1.5 pooka
419 1.1 pooka ATF_TP_ADD_TCS(tp)
420 1.1 pooka {
421 1.1 pooka
422 1.1 pooka ATF_TP_ADD_TC(tp, simpleping);
423 1.2 pooka ATF_TP_ADD_TC(tp, floodping);
424 1.3 pooka ATF_TP_ADD_TC(tp, floodping2);
425 1.3 pooka ATF_TP_ADD_TC(tp, pingsize);
426 1.5 pooka ATF_TP_ADD_TC(tp, ping_of_death);
427 1.1 pooka
428 1.1 pooka return atf_no_error();
429 1.1 pooka }
430