1 /*	$NetBSD: t_forward.c,v 1.10 2017/01/13 21:30:42 christos 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.10 2017/01/13 21:30:42 christos 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 	struct sockaddr_in sin;
71 	char payload[1024];
72 	char ifname[IFNAMSIZ];
73 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
74 	int nv;
75 	int s;
76 
77 	netcfg_rump_makeshmif("bus1", ifname);
78 	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
79 	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
80 
81 	/* set global ttl to 1 */
82 	nv = 1;
83 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
84 		atf_tc_fail_errno("set ttl");
85 
86 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
87 	if (s == -1)
88 		atf_tc_fail_errno("create send socket");
89 
90 	memset(&sin, 0, sizeof(sin));
91 	sin.sin_len = sizeof(sin);
92 	sin.sin_family = AF_INET;
93 	sin.sin_port = htons(33434);
94 	sin.sin_addr.s_addr = inet_addr("9.9.9.9");
95 
96 	/* send udp datagram with ttl == 1 */
97 	if (rump_sys_sendto(s, payload, sizeof(payload), 0,
98 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
99 		atf_tc_fail_errno("sendto");
100 }
101 
102 static void
103 router(void)
104 {
105 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
106 		    ICMPCTL_RETURNDATABYTES };
107 	char ifname[IFNAMSIZ];
108 	int nv;
109 
110 	/* set returndatabytes to 200 */
111 	nv = 200;
112 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
113 		atf_tc_fail_errno("sysctl returndatabytes");
114 
115 	netcfg_rump_makeshmif("bus1", ifname);
116 	netcfg_rump_if(ifname, "1.0.0.2", "255.255.255.0");
117 
118 	/*
119 	 * Wait for parent to send us the data and for us to have
120 	 * a chance to process it.
121 	 */
122 	sleep(1);
123 	exit(0);
124 }
125 
126 ATF_TC(returndatabytes);
127 ATF_TC_HEAD(returndatabytes, tc)
128 {
129 
130 	atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
131 	    "packets can cause kernel panic (PR kern/43548)");
132 	atf_tc_set_md_var(tc, "timeout", "20"); /* just in case */
133 }
134 
135 ATF_TC_BODY(returndatabytes, tc)
136 {
137 	pid_t cpid;
138 	int status;
139 
140 	cpid = fork();
141 	rump_init();
142 
143 	switch (cpid) {
144 	case -1:
145 		atf_tc_fail_errno("fork failed");
146 	case 0:
147 		router();
148 		break;
149 	default:
150 		sendttl();
151 		if (wait(&status) == -1)
152 			atf_tc_fail_errno("wait");
153 		if (WIFEXITED(status)) {
154 			if (WEXITSTATUS(status))
155 				atf_tc_fail("child exited with status %d",
156 				    WEXITSTATUS(status));
157 		} else {
158 			atf_tc_fail("child died");
159 		}
160 	}
161 }
162 
163 ATF_TP_ADD_TCS(tp)
164 {
165 
166 	ATF_TP_ADD_TC(tp, returndatabytes);
167 
168 	return atf_no_error();
169 }
170