xref: /netbsd-src/tests/net/icmp/t_forward.c (revision 179b12252ecaf3553d9c2b7458ce62b6a2203d0c)
1 /*	$NetBSD: t_forward.c,v 1.5 2010/07/26 14:10:31 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_forward.c,v 1.5 2010/07/26 14:10:31 pooka Exp $");
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/time.h>
38 #include <sys/sysctl.h>
39 #include <sys/wait.h>
40 
41 #include <arpa/inet.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_icmp.h>
47 #include <netinet/icmp_var.h>
48 #include <net/route.h>
49 
50 #include <rump/rump.h>
51 #include <rump/rump_syscalls.h>
52 
53 #include <atf-c.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "../../h_macros.h"
61 #include "../config/netconfig.c"
62 
63 /*
64  * Since our maxttl is in our private namespace, we don't need raw packet
65  * construction like traceroute(8) -- we can just use the global maxttl.
66  */
67 static void
68 sendttl(void)
69 {
70 	extern int rumpns_ip_defttl;
71 	struct sockaddr_in sin;
72 	char payload[1024];
73 	char ifname[IFNAMSIZ];
74 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
75 	int nv;
76 	int s;
77 
78 	netcfg_rump_makeshmif("bus1", ifname);
79 	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
80 	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
81 
82 	/* set global ttl to 1 */
83 	nv = 1;
84 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
85 		atf_tc_fail_errno("set ttl");
86 
87 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
88 	if (s == -1)
89 		atf_tc_fail_errno("create send socket");
90 
91 	memset(&sin, 0, sizeof(sin));
92 	sin.sin_len = sizeof(sin);
93 	sin.sin_family = AF_INET;
94 	sin.sin_port = htons(33434);
95 	sin.sin_addr.s_addr = inet_addr("9.9.9.9");
96 
97 	/* send udp datagram with ttl == 1 */
98 	if (rump_sys_sendto(s, payload, sizeof(payload), 0,
99 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
100 		atf_tc_fail_errno("sendto");
101 }
102 
103 static void
104 router(void)
105 {
106 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
107 		    ICMPCTL_RETURNDATABYTES };
108 	char ifname[IFNAMSIZ];
109 	int nv;
110 
111 	/* set returndatabytes to 200 */
112 	nv = 200;
113 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
114 		atf_tc_fail_errno("sysctl returndatabytes");
115 
116 	netcfg_rump_makeshmif("bus1", ifname);
117 	netcfg_rump_if(ifname, "1.0.0.2", "255.255.255.0");
118 
119 	/*
120 	 * Wait for parent to send us the data and for us to have
121 	 * a chance to process it.
122 	 */
123 	sleep(1);
124 	exit(0);
125 }
126 
127 ATF_TC(returndatabytes);
128 ATF_TC_HEAD(returndatabytes, tc)
129 {
130 
131 	atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
132 	    "packets can cause kernel panic");
133 	atf_tc_set_md_var(tc, "use.fs", "true");
134 	atf_tc_set_md_var(tc, "timeout", "4"); /* just in case */
135 	/* PR kern/43548 */
136 }
137 
138 ATF_TC_BODY(returndatabytes, tc)
139 {
140 	pid_t cpid;
141 	int status;
142 
143 	cpid = fork();
144 	rump_init();
145 
146 	switch (cpid) {
147 	case -1:
148 		atf_tc_fail_errno("fork failed");
149 	case 0:
150 		router();
151 		break;
152 	default:
153 		sendttl();
154 		if (wait(&status) == -1)
155 			atf_tc_fail_errno("wait");
156 		if (WIFEXITED(status)) {
157 			if (WEXITSTATUS(status))
158 				atf_tc_fail("child exited with status %d",
159 				    WEXITSTATUS(status));
160 		} else {
161 			atf_tc_fail("child died");
162 		}
163 	}
164 }
165 
166 ATF_TP_ADD_TCS(tp)
167 {
168 
169 	ATF_TP_ADD_TC(tp, returndatabytes);
170 
171 	return atf_no_error();
172 }
173