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