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