t_ping.c revision 1.13 1 1.13 martin /* $NetBSD: t_ping.c,v 1.13 2011/01/05 14:43:40 martin 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.13 martin __RCSID("$NetBSD: t_ping.c,v 1.13 2011/01/05 14:43:40 martin 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, "timeout", "2");
63 1.1 pooka }
64 1.1 pooka
65 1.1 pooka ATF_TC_BODY(simpleping, tc)
66 1.1 pooka {
67 1.1 pooka char ifname[IFNAMSIZ];
68 1.1 pooka pid_t cpid;
69 1.1 pooka bool win, win2;
70 1.1 pooka
71 1.1 pooka cpid = fork();
72 1.1 pooka rump_init();
73 1.1 pooka netcfg_rump_makeshmif("but-can-i-buy-your-ether-bus", ifname);
74 1.1 pooka
75 1.1 pooka switch (cpid) {
76 1.1 pooka case -1:
77 1.1 pooka atf_tc_fail_errno("fork failed");
78 1.1 pooka case 0:
79 1.1 pooka netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
80 1.1 pooka pause();
81 1.1 pooka break;
82 1.1 pooka default:
83 1.1 pooka break;
84 1.1 pooka }
85 1.1 pooka
86 1.7 pooka usleep(500000);
87 1.7 pooka
88 1.1 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
89 1.1 pooka
90 1.1 pooka /*
91 1.1 pooka * The beauty of shmif is that we don't have races here.
92 1.1 pooka */
93 1.1 pooka win = netcfg_rump_pingtest("1.1.1.10", 500);
94 1.1 pooka win2 = netcfg_rump_pingtest("1.1.1.30", 500);
95 1.1 pooka
96 1.1 pooka kill(cpid, SIGKILL);
97 1.1 pooka
98 1.1 pooka if (!win)
99 1.1 pooka atf_tc_fail("ping failed");
100 1.1 pooka if (win2)
101 1.1 pooka atf_tc_fail("non-existent host responded");
102 1.1 pooka }
103 1.1 pooka
104 1.2 pooka ATF_TC(floodping);
105 1.2 pooka ATF_TC_HEAD(floodping, tc)
106 1.2 pooka {
107 1.2 pooka
108 1.2 pooka atf_tc_set_md_var(tc, "descr", "see how kernel responds to floodping");
109 1.2 pooka }
110 1.2 pooka
111 1.3 pooka /* why the hell isn't this available in userspace??? */
112 1.3 pooka static uint16_t
113 1.3 pooka in_cksum(void *data, size_t len)
114 1.3 pooka {
115 1.3 pooka uint16_t *buf = data;
116 1.3 pooka unsigned sum;
117 1.3 pooka
118 1.3 pooka for (sum = 0; len > 1; len -= 2)
119 1.3 pooka sum += *buf++;
120 1.3 pooka if (len)
121 1.3 pooka sum += *(uint8_t *)buf;
122 1.3 pooka
123 1.3 pooka sum = (sum >> 16) + (sum & 0xffff);
124 1.3 pooka sum += (sum >> 16);
125 1.3 pooka
126 1.3 pooka return ~sum;
127 1.3 pooka }
128 1.3 pooka
129 1.3 pooka static int
130 1.10 christos doping(const char *target, int loops, u_int pktsize)
131 1.2 pooka {
132 1.12 martin union {
133 1.12 martin char buf[IP_MAXPACKET - sizeof(struct ip)];
134 1.12 martin struct icmp i; /* ensure proper alignment */
135 1.12 martin } sndbuf;
136 1.3 pooka char recvbuf[IP_MAXPACKET];
137 1.2 pooka struct sockaddr_in dst, pingee;
138 1.3 pooka struct icmp *icmp;
139 1.2 pooka socklen_t slen;
140 1.2 pooka ssize_t n;
141 1.9 christos int loop, succ;
142 1.2 pooka int x, xnon, s;
143 1.2 pooka
144 1.3 pooka RL(s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP));
145 1.3 pooka RL(x = rump_sys_fcntl(s, F_GETFL, 0));
146 1.3 pooka xnon = x | O_NONBLOCK;
147 1.3 pooka
148 1.3 pooka memset(&dst, 0, sizeof(dst));
149 1.3 pooka dst.sin_len = sizeof(dst);
150 1.3 pooka dst.sin_family = AF_INET;
151 1.3 pooka dst.sin_addr.s_addr = inet_addr(target);
152 1.3 pooka
153 1.12 martin icmp = (struct icmp *)&sndbuf;
154 1.3 pooka memset(icmp, 0, sizeof(*icmp));
155 1.3 pooka icmp->icmp_type = ICMP_ECHO;
156 1.3 pooka icmp->icmp_id = htons(37);
157 1.3 pooka
158 1.3 pooka if (pktsize < sizeof(*icmp))
159 1.3 pooka pktsize = sizeof(*icmp);
160 1.13 martin if (pktsize > sizeof(sndbuf.buf))
161 1.13 martin pktsize = sizeof(sndbuf.buf);
162 1.3 pooka
163 1.3 pooka RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
164 1.3 pooka &pktsize, sizeof(pktsize)));
165 1.3 pooka RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
166 1.3 pooka &pktsize, sizeof(pktsize)));
167 1.3 pooka
168 1.3 pooka slen = sizeof(pingee);
169 1.3 pooka succ = 0;
170 1.3 pooka for (loop = 0; loop < loops; loop++) {
171 1.3 pooka RL(rump_sys_fcntl(s, F_SETFL, x));
172 1.3 pooka icmp->icmp_seq = htons(loop);
173 1.3 pooka icmp->icmp_cksum = 0;
174 1.3 pooka icmp->icmp_cksum = in_cksum(icmp, pktsize);
175 1.3 pooka RL(rump_sys_sendto(s, icmp, pktsize, 0,
176 1.3 pooka (struct sockaddr *)&dst, sizeof(dst)));
177 1.3 pooka
178 1.3 pooka RL(rump_sys_fcntl(s, F_SETFL, xnon));
179 1.3 pooka while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
180 1.3 pooka (struct sockaddr *)&pingee, &slen)) > 0) {
181 1.3 pooka succ++;
182 1.3 pooka }
183 1.3 pooka if (n == -1 && errno == EAGAIN)
184 1.3 pooka continue;
185 1.3 pooka atf_tc_fail_errno("recv failed");
186 1.3 pooka }
187 1.3 pooka
188 1.3 pooka rump_sys_close(s);
189 1.3 pooka return succ;
190 1.3 pooka }
191 1.3 pooka
192 1.3 pooka #define LOOPS 10000
193 1.3 pooka
194 1.3 pooka ATF_TC_BODY(floodping, tc)
195 1.3 pooka {
196 1.3 pooka char ifname[IFNAMSIZ];
197 1.3 pooka pid_t cpid;
198 1.3 pooka int succ;
199 1.3 pooka
200 1.2 pooka cpid = fork();
201 1.2 pooka rump_init();
202 1.2 pooka netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
203 1.2 pooka
204 1.2 pooka switch (cpid) {
205 1.2 pooka case -1:
206 1.2 pooka atf_tc_fail_errno("fork failed");
207 1.2 pooka case 0:
208 1.2 pooka netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
209 1.2 pooka pause();
210 1.2 pooka break;
211 1.2 pooka default:
212 1.2 pooka break;
213 1.2 pooka }
214 1.2 pooka
215 1.2 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
216 1.2 pooka
217 1.3 pooka succ = doping("1.1.1.10", LOOPS, 56);
218 1.3 pooka printf("got %d/%d\n", succ, LOOPS);
219 1.3 pooka
220 1.3 pooka kill(cpid, SIGKILL);
221 1.3 pooka }
222 1.3 pooka
223 1.3 pooka ATF_TC(floodping2);
224 1.3 pooka ATF_TC_HEAD(floodping2, tc)
225 1.3 pooka {
226 1.3 pooka
227 1.3 pooka atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
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 }
262 1.3 pooka
263 1.3 pooka ATF_TC_BODY(pingsize, tc)
264 1.3 pooka {
265 1.3 pooka char ifname[IFNAMSIZ];
266 1.3 pooka pid_t cpid;
267 1.3 pooka int succ, i;
268 1.3 pooka
269 1.3 pooka cpid = fork();
270 1.3 pooka rump_init();
271 1.3 pooka netcfg_rump_makeshmif("jippikaiee", ifname);
272 1.2 pooka
273 1.3 pooka switch (cpid) {
274 1.3 pooka case -1:
275 1.3 pooka atf_tc_fail_errno("fork failed");
276 1.3 pooka case 0:
277 1.3 pooka netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
278 1.3 pooka pause();
279 1.3 pooka break;
280 1.3 pooka default:
281 1.3 pooka break;
282 1.3 pooka }
283 1.2 pooka
284 1.3 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
285 1.2 pooka
286 1.2 pooka succ = 0;
287 1.2 pooka
288 1.3 pooka /* small sizes */
289 1.4 pooka for (i = 0 ; i < IP_MAXPACKET - 60000; i++)
290 1.3 pooka succ += doping("1.1.1.10", 1, i);
291 1.3 pooka
292 1.3 pooka /* medium sizes */
293 1.4 pooka for (i = IP_MAXPACKET - 60000; i < IP_MAXPACKET - 100; i += 1000)
294 1.3 pooka succ += doping("1.1.1.10", 1, i);
295 1.3 pooka
296 1.3 pooka /* big sizes */
297 1.4 pooka for (i = IP_MAXPACKET - 100; i < IP_MAXPACKET; i += 10)
298 1.3 pooka succ += doping("1.1.1.10", 1, i);
299 1.2 pooka
300 1.3 pooka printf("got %d/%d\n", succ, IP_MAXPACKET);
301 1.2 pooka kill(cpid, SIGKILL);
302 1.2 pooka }
303 1.2 pooka
304 1.5 pooka ATF_TC(ping_of_death);
305 1.5 pooka ATF_TC_HEAD(ping_of_death, tc)
306 1.5 pooka {
307 1.5 pooka
308 1.5 pooka atf_tc_set_md_var(tc, "descr", "send a \"ping of death\"");
309 1.6 pooka atf_tc_set_md_var(tc, "timeout", "2");
310 1.5 pooka }
311 1.5 pooka
312 1.5 pooka ATF_TC_BODY(ping_of_death, tc)
313 1.5 pooka {
314 1.5 pooka char data[1500];
315 1.5 pooka struct sockaddr_in dst;
316 1.5 pooka struct ip *ip;
317 1.5 pooka struct icmp *icmp;
318 1.5 pooka char ifname[IFNAMSIZ];
319 1.5 pooka pid_t cpid;
320 1.5 pooka size_t tot, frag;
321 1.5 pooka int s, x, loop;
322 1.5 pooka
323 1.5 pooka cpid = fork();
324 1.5 pooka rump_init();
325 1.5 pooka netcfg_rump_makeshmif("jippikaiee", ifname);
326 1.5 pooka
327 1.5 pooka switch (cpid) {
328 1.5 pooka case -1:
329 1.5 pooka atf_tc_fail_errno("fork failed");
330 1.5 pooka case 0:
331 1.5 pooka /* wait until we receive a too long IP packet */
332 1.5 pooka for (loop = 0;; loop++) {
333 1.5 pooka uint64_t ipstat[IP_NSTATS];
334 1.5 pooka size_t arglen;
335 1.5 pooka int mib[4];
336 1.5 pooka
337 1.5 pooka if (loop == 1)
338 1.5 pooka netcfg_rump_if(ifname,
339 1.5 pooka "1.1.1.10", "255.255.255.0");
340 1.5 pooka
341 1.5 pooka mib[0] = CTL_NET;
342 1.5 pooka mib[1] = PF_INET;
343 1.5 pooka mib[2] = IPPROTO_IP;
344 1.5 pooka mib[3] = IPCTL_STATS;
345 1.5 pooka
346 1.5 pooka arglen = sizeof(ipstat);
347 1.5 pooka RL(rump_sys___sysctl(mib, 4, &ipstat, &arglen,
348 1.5 pooka NULL, 0));
349 1.5 pooka if (loop == 0 && ipstat[IP_STAT_TOOLONG] != 0)
350 1.5 pooka _exit(1);
351 1.5 pooka if (ipstat[IP_STAT_TOOLONG])
352 1.5 pooka break;
353 1.5 pooka usleep(10000);
354 1.5 pooka }
355 1.5 pooka
356 1.5 pooka _exit(0);
357 1.5 pooka break;
358 1.5 pooka default:
359 1.5 pooka break;
360 1.5 pooka }
361 1.5 pooka
362 1.5 pooka netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
363 1.5 pooka
364 1.5 pooka RL(s = rump_sys_socket(PF_INET, SOCK_RAW, 0));
365 1.5 pooka x = 1;
366 1.5 pooka RL(rump_sys_setsockopt(s, IPPROTO_IP, IP_HDRINCL, &x, sizeof(x)));
367 1.5 pooka
368 1.5 pooka memset(&dst, 0, sizeof(dst));
369 1.5 pooka dst.sin_len = sizeof(dst);
370 1.5 pooka dst.sin_family = AF_INET;
371 1.5 pooka dst.sin_addr.s_addr = inet_addr("1.1.1.10");
372 1.5 pooka
373 1.5 pooka /* construct packet */
374 1.5 pooka memset(data, 0, sizeof(data));
375 1.5 pooka ip = (struct ip *)data;
376 1.5 pooka ip->ip_v = 4;
377 1.5 pooka ip->ip_hl = sizeof(*ip) >> 2;
378 1.5 pooka ip->ip_p = IPPROTO_ICMP;
379 1.5 pooka ip->ip_ttl = IPDEFTTL;
380 1.5 pooka ip->ip_dst = dst.sin_addr;
381 1.5 pooka ip->ip_id = 1234;
382 1.5 pooka
383 1.5 pooka icmp = (struct icmp *)(ip + 1);
384 1.5 pooka icmp->icmp_type = ICMP_ECHO;
385 1.5 pooka icmp->icmp_cksum = in_cksum(icmp, sizeof(*icmp));
386 1.5 pooka
387 1.5 pooka for (;;) {
388 1.5 pooka int status;
389 1.5 pooka
390 1.5 pooka /* resolve arp before sending raw stuff */
391 1.5 pooka netcfg_rump_pingtest("1.1.1.10", 1);
392 1.5 pooka
393 1.5 pooka for (tot = 0;
394 1.5 pooka tot < 65538 - sizeof(*ip);
395 1.5 pooka tot += (frag - sizeof(*ip))) {
396 1.5 pooka frag = MIN(65538 - tot, sizeof(data));
397 1.5 pooka ip->ip_off = tot >> 3;
398 1.9 christos assert((size_t)ip->ip_off << 3 == tot);
399 1.5 pooka ip->ip_len = frag;
400 1.5 pooka
401 1.5 pooka if (frag == sizeof(data)) {
402 1.5 pooka ip->ip_off |= IP_MF;
403 1.5 pooka }
404 1.5 pooka
405 1.5 pooka RL(rump_sys_sendto(s, data, frag, 0,
406 1.5 pooka (struct sockaddr *)&dst, sizeof(dst)));
407 1.5 pooka }
408 1.5 pooka if (waitpid(-1, &status, WNOHANG) > 0) {
409 1.5 pooka if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
410 1.5 pooka break;
411 1.5 pooka atf_tc_fail("child did not exit clean");
412 1.5 pooka }
413 1.5 pooka
414 1.5 pooka usleep(10000);
415 1.5 pooka }
416 1.5 pooka }
417 1.5 pooka
418 1.1 pooka ATF_TP_ADD_TCS(tp)
419 1.1 pooka {
420 1.1 pooka
421 1.1 pooka ATF_TP_ADD_TC(tp, simpleping);
422 1.2 pooka ATF_TP_ADD_TC(tp, floodping);
423 1.3 pooka ATF_TP_ADD_TC(tp, floodping2);
424 1.3 pooka ATF_TP_ADD_TC(tp, pingsize);
425 1.5 pooka ATF_TP_ADD_TC(tp, ping_of_death);
426 1.1 pooka
427 1.1 pooka return atf_no_error();
428 1.1 pooka }
429