t_forward.c revision 1.3 1 1.3 pooka /* $NetBSD: t_forward.c,v 1.3 2010/07/25 21:39:21 pooka 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.3 pooka __RCSID("$NetBSD: t_forward.c,v 1.3 2010/07/25 21:39:21 pooka Exp $");
33 1.1 pooka #endif /* not lint */
34 1.1 pooka
35 1.1 pooka #include <sys/types.h>
36 1.1 pooka #include <sys/socket.h>
37 1.1 pooka #include <sys/time.h>
38 1.1 pooka #include <sys/sysctl.h>
39 1.1 pooka #include <sys/wait.h>
40 1.1 pooka
41 1.1 pooka #include <arpa/inet.h>
42 1.1 pooka
43 1.1 pooka #include <netinet/in.h>
44 1.1 pooka #include <netinet/in_systm.h>
45 1.1 pooka #include <netinet/ip.h>
46 1.1 pooka #include <netinet/ip_icmp.h>
47 1.1 pooka #include <netinet/icmp_var.h>
48 1.1 pooka #include <net/route.h>
49 1.1 pooka
50 1.1 pooka #include <rump/rump.h>
51 1.1 pooka #include <rump/rump_syscalls.h>
52 1.1 pooka
53 1.1 pooka #include <atf-c.h>
54 1.1 pooka #include <errno.h>
55 1.1 pooka #include <stdio.h>
56 1.1 pooka #include <stdlib.h>
57 1.1 pooka #include <string.h>
58 1.1 pooka #include <unistd.h>
59 1.1 pooka
60 1.1 pooka #include "../../h_macros.h"
61 1.3 pooka #include "../config/netconfig.c"
62 1.1 pooka
63 1.1 pooka static void
64 1.1 pooka configure_interface(const char *busname, const char *addr, const char *mask,
65 1.1 pooka const char *bcast)
66 1.1 pooka {
67 1.1 pooka char ifname[32];
68 1.1 pooka struct ifaliasreq ia;
69 1.1 pooka struct sockaddr_in *sin;
70 1.1 pooka int s, rv, ifnum;
71 1.1 pooka
72 1.1 pooka if ((s = rump_pub_shmif_create(busname, &ifnum)) != 0) {
73 1.2 pooka atf_tc_fail("rump_pub_shmif_create(%d)", s);
74 1.1 pooka }
75 1.1 pooka
76 1.1 pooka if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
77 1.1 pooka atf_tc_fail_errno("if config socket");
78 1.1 pooka }
79 1.1 pooka sprintf(ifname, "shmif%d", ifnum);
80 1.1 pooka
81 1.1 pooka /* Address */
82 1.1 pooka memset(&ia, 0, sizeof(ia));
83 1.1 pooka strcpy(ia.ifra_name, ifname);
84 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_addr;
85 1.1 pooka sin->sin_family = AF_INET;
86 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
87 1.1 pooka sin->sin_addr.s_addr = inet_addr(addr);
88 1.1 pooka
89 1.1 pooka /* Netmask */
90 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_mask;
91 1.1 pooka sin->sin_family = AF_INET;
92 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
93 1.1 pooka sin->sin_addr.s_addr = inet_addr(mask);
94 1.1 pooka
95 1.1 pooka /* Broadcast address */
96 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
97 1.1 pooka sin->sin_family = AF_INET;
98 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
99 1.1 pooka sin->sin_addr.s_addr = inet_addr(bcast);
100 1.1 pooka
101 1.1 pooka rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
102 1.1 pooka if (rv) {
103 1.1 pooka atf_tc_fail_errno("SIOCAIFADDR");
104 1.1 pooka }
105 1.1 pooka rump_sys_close(s);
106 1.1 pooka }
107 1.1 pooka
108 1.1 pooka static void
109 1.1 pooka configure_routing(const char *dst, const char *mask, const char *gw)
110 1.1 pooka {
111 1.1 pooka size_t len;
112 1.1 pooka struct {
113 1.1 pooka struct rt_msghdr m_rtm;
114 1.1 pooka uint8_t m_space[512];
115 1.1 pooka } m_rtmsg;
116 1.1 pooka #define rtm m_rtmsg.m_rtm
117 1.1 pooka uint8_t *bp = m_rtmsg.m_space;
118 1.1 pooka struct sockaddr_in sinstore;
119 1.1 pooka int s, rv;
120 1.1 pooka
121 1.1 pooka s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
122 1.1 pooka if (s == -1) {
123 1.1 pooka atf_tc_fail_errno("routing socket");
124 1.1 pooka }
125 1.1 pooka
126 1.1 pooka memset(&m_rtmsg, 0, sizeof(m_rtmsg));
127 1.1 pooka rtm.rtm_type = RTM_ADD;
128 1.1 pooka rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
129 1.1 pooka rtm.rtm_version = RTM_VERSION;
130 1.1 pooka rtm.rtm_seq = 2;
131 1.1 pooka rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
132 1.1 pooka
133 1.1 pooka /* dst */
134 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
135 1.1 pooka sinstore.sin_family = AF_INET;
136 1.1 pooka sinstore.sin_len = sizeof(sinstore);
137 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(dst);
138 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
139 1.1 pooka bp += sizeof(sinstore);
140 1.1 pooka
141 1.1 pooka /* gw */
142 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
143 1.1 pooka sinstore.sin_family = AF_INET;
144 1.1 pooka sinstore.sin_len = sizeof(sinstore);
145 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(gw);
146 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
147 1.1 pooka bp += sizeof(sinstore);
148 1.1 pooka
149 1.1 pooka /* netmask */
150 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
151 1.1 pooka sinstore.sin_family = AF_INET;
152 1.1 pooka sinstore.sin_len = sizeof(sinstore);
153 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(mask);
154 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
155 1.1 pooka bp += sizeof(sinstore);
156 1.1 pooka
157 1.1 pooka len = bp - (uint8_t *)&m_rtmsg;
158 1.1 pooka rtm.rtm_msglen = len;
159 1.1 pooka
160 1.1 pooka rv = rump_sys_write(s, &m_rtmsg, len);
161 1.1 pooka if (rv != (int)len) {
162 1.1 pooka atf_tc_fail_errno("write routing message");
163 1.1 pooka }
164 1.1 pooka rump_sys_close(s);
165 1.1 pooka }
166 1.1 pooka
167 1.1 pooka /*
168 1.1 pooka * Since our maxttl is in our private namespace, we don't need raw packet
169 1.1 pooka * construction like traceroute(8) -- we can just use the global maxttl.
170 1.1 pooka */
171 1.1 pooka static void
172 1.1 pooka sendttl(void)
173 1.1 pooka {
174 1.1 pooka extern int rumpns_ip_defttl;
175 1.1 pooka struct sockaddr_in sin;
176 1.1 pooka char payload[1024];
177 1.3 pooka char ifname[IFNAMSIZ];
178 1.1 pooka int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
179 1.1 pooka int nv;
180 1.1 pooka int s;
181 1.1 pooka
182 1.3 pooka netcfg_rump_makeshmif("bus1", ifname);
183 1.3 pooka netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0", "1.0.0.255");
184 1.3 pooka netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
185 1.1 pooka
186 1.1 pooka /* set global ttl to 1 */
187 1.1 pooka nv = 1;
188 1.1 pooka if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
189 1.1 pooka atf_tc_fail_errno("set ttl");
190 1.1 pooka
191 1.1 pooka s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
192 1.1 pooka if (s == -1)
193 1.1 pooka atf_tc_fail_errno("create send socket");
194 1.1 pooka
195 1.1 pooka memset(&sin, 0, sizeof(sin));
196 1.1 pooka sin.sin_len = sizeof(sin);
197 1.1 pooka sin.sin_family = AF_INET;
198 1.1 pooka sin.sin_port = htons(33434);
199 1.1 pooka sin.sin_addr.s_addr = inet_addr("9.9.9.9");
200 1.1 pooka
201 1.1 pooka /* send udp datagram with ttl == 1 */
202 1.1 pooka if (rump_sys_sendto(s, payload, sizeof(payload), 0,
203 1.1 pooka (struct sockaddr *)&sin, sizeof(sin)) == -1)
204 1.1 pooka atf_tc_fail_errno("sendto");
205 1.1 pooka }
206 1.1 pooka
207 1.1 pooka static void
208 1.1 pooka router(void)
209 1.1 pooka {
210 1.1 pooka int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
211 1.1 pooka ICMPCTL_RETURNDATABYTES };
212 1.1 pooka int nv;
213 1.1 pooka
214 1.1 pooka /* set returndatabytes to 200 */
215 1.1 pooka nv = 200;
216 1.1 pooka if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
217 1.1 pooka atf_tc_fail_errno("sysctl returndatabytes");
218 1.1 pooka
219 1.1 pooka configure_interface("bus1", "1.0.0.2", "255.255.255.0", "1.0.0.255");
220 1.1 pooka
221 1.1 pooka /*
222 1.1 pooka * Wait for parent to send us the data and for us to have
223 1.1 pooka * a chance to process it.
224 1.1 pooka */
225 1.1 pooka sleep(1);
226 1.1 pooka exit(0);
227 1.1 pooka }
228 1.1 pooka
229 1.1 pooka ATF_TC(returndatabytes);
230 1.1 pooka ATF_TC_HEAD(returndatabytes, tc)
231 1.1 pooka {
232 1.1 pooka
233 1.1 pooka atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
234 1.1 pooka "packets can cause kernel panic");
235 1.1 pooka atf_tc_set_md_var(tc, "use.fs", "true");
236 1.1 pooka atf_tc_set_md_var(tc, "timeout", "4"); /* just in case */
237 1.1 pooka /* PR kern/43548 */
238 1.1 pooka }
239 1.1 pooka
240 1.1 pooka ATF_TC_BODY(returndatabytes, tc)
241 1.1 pooka {
242 1.1 pooka pid_t cpid;
243 1.1 pooka int status;
244 1.1 pooka
245 1.1 pooka cpid = fork();
246 1.1 pooka rump_init();
247 1.1 pooka
248 1.1 pooka switch (cpid) {
249 1.1 pooka case -1:
250 1.1 pooka atf_tc_fail_errno("fork failed");
251 1.1 pooka case 0:
252 1.1 pooka router();
253 1.1 pooka break;
254 1.1 pooka default:
255 1.1 pooka sendttl();
256 1.1 pooka if (wait(&status) == -1)
257 1.1 pooka atf_tc_fail_errno("wait");
258 1.1 pooka if (WIFEXITED(status)) {
259 1.1 pooka if (WEXITSTATUS(status))
260 1.1 pooka atf_tc_fail("child exited with status %d",
261 1.1 pooka WEXITSTATUS(status));
262 1.1 pooka } else {
263 1.1 pooka atf_tc_fail("child died");
264 1.1 pooka }
265 1.1 pooka }
266 1.1 pooka }
267 1.1 pooka
268 1.1 pooka ATF_TP_ADD_TCS(tp)
269 1.1 pooka {
270 1.1 pooka
271 1.1 pooka ATF_TP_ADD_TC(tp, returndatabytes);
272 1.1 pooka
273 1.1 pooka return atf_no_error();
274 1.1 pooka }
275