xref: /freebsd/contrib/netbsd-tests/net/if_loop/t_pr.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*	$NetBSD: t_pr.c,v 1.7 2012/03/18 09:46:50 jruoho 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_pr.c,v 1.7 2012/03/18 09:46:50 jruoho Exp $");
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 
39 #include <netinet/in.h>
40 #include <net/route.h>
41 
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
44 
45 #include <atf-c.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "../config/netconfig.c"
53 #include "../../h_macros.h"
54 
55 /*
56  * Prepare rump, configure interface and route to cause fragmentation
57  */
58 static void
59 setup(void)
60 {
61 	char ifname[IFNAMSIZ];
62 	struct {
63 		struct rt_msghdr m_rtm;
64 		struct sockaddr_in m_sin;
65 	} m_rtmsg;
66 #define rtm m_rtmsg.m_rtm
67 #define rsin m_rtmsg.m_sin
68 	struct ifreq ifr;
69 	int s;
70 
71 	rump_init();
72 
73 	/* first, config lo0 & route */
74 	strcpy(ifname, "lo0");
75 	netcfg_rump_if(ifname, "127.0.0.1", "255.0.0.0");
76 	netcfg_rump_route("127.0.0.1", "255.0.0.0", "127.0.0.1");
77 
78 	if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
79 		atf_tc_fail_errno("routing socket");
80 
81 	/*
82 	 * set MTU for interface so that route MTU doesn't
83 	 * get overridden by it.
84 	 */
85 	memset(&ifr, 0, sizeof(ifr));
86 	strcpy(ifr.ifr_name, "lo0");
87 	ifr.ifr_mtu = 1300;
88 	if (rump_sys_ioctl(s, SIOCSIFMTU, &ifr) == -1)
89 		atf_tc_fail_errno("set mtu");
90 
91 	/* change route MTU to 100 */
92 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
93 	rtm.rtm_type = RTM_CHANGE;
94 	rtm.rtm_flags = RTF_STATIC;
95 	rtm.rtm_version = RTM_VERSION;
96 	rtm.rtm_seq = 3;
97 	rtm.rtm_inits = RTV_MTU;
98 	rtm.rtm_addrs = RTA_DST;
99 	rtm.rtm_rmx.rmx_mtu = 100;
100 	rtm.rtm_msglen = sizeof(m_rtmsg);
101 
102 	memset(&rsin, 0, sizeof(rsin));
103 	rsin.sin_family = AF_INET;
104 	rsin.sin_len = sizeof(rsin);
105 	rsin.sin_addr.s_addr = inet_addr("127.0.0.1");
106 
107 	if (rump_sys_write(s, &m_rtmsg, sizeof(m_rtmsg)) != sizeof(m_rtmsg))
108 		atf_tc_fail_errno("set route mtu");
109 	rump_sys_close(s);
110 }
111 
112 /*
113  * Turn on checksums on loopback interfaces
114  */
115 static int
116 enable_locsums(void)
117 {
118 	struct sysctlnode q, ans[256];
119 	int mib[5], enable;
120 	size_t alen;
121 	unsigned i;
122 
123 	mib[0] = CTL_NET;
124 	mib[1] = PF_INET;
125 	mib[2] = IPPROTO_IP;
126 	mib[3] = CTL_QUERY;
127 	alen = sizeof(ans);
128 
129 	memset(&q, 0, sizeof(q));
130 	q.sysctl_flags = SYSCTL_VERSION;
131 
132 	if (rump_sys___sysctl(mib, 4, ans, &alen, &q, sizeof(q)) == -1)
133 		return -1;
134 
135 	for (i = 0; i < __arraycount(ans); i++)
136 		if (strcmp("do_loopback_cksum", ans[i].sysctl_name) == 0)
137 			break;
138 	if (i == __arraycount(ans)) {
139 		errno = ENOENT;
140 		return -1;
141 	}
142 
143 	mib[3] = ans[i].sysctl_num;
144 
145 	enable = 1;
146 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &enable,
147 	    sizeof(enable)) == -1)
148 		return errno;
149 
150 	return 0;
151 }
152 
153 ATF_TC(loopmtu);
154 ATF_TC_HEAD(loopmtu, tc)
155 {
156 
157 	atf_tc_set_md_var(tc, "descr",
158 	    "test lo0 fragmentation (PR kern/43664)");
159 }
160 
161 ATF_TC_BODY(loopmtu, tc)
162 {
163 	struct sockaddr_in sin;
164 	char data[2000];
165 	int s;
166 
167 	setup();
168 
169 	/* open raw socket */
170 	s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
171 	if (s == -1)
172 		atf_tc_fail_errno("raw socket");
173 
174 	/* then, send data */
175 	memset(&sin, 0, sizeof(sin));
176 	sin.sin_family = AF_INET;
177 	sin.sin_len = sizeof(sin);
178 	sin.sin_port = htons(12345);
179 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
180 
181 	if (rump_sys_sendto(s, data, sizeof(data), 0,
182 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
183 		atf_tc_fail_errno("sendto failed");
184 }
185 
186 ATF_TC(loopmtu_csum);
187 ATF_TC_HEAD(loopmtu_csum, tc)
188 {
189 
190 	atf_tc_set_md_var(tc, "descr",
191 	    "test lo0 fragmentation with checksums (PR kern/43664)");
192 }
193 
194 ATF_TC_BODY(loopmtu_csum, tc)
195 {
196 	struct sockaddr_in sin;
197 	char data[2000];
198 	int s;
199 
200 	setup();
201 
202 	ATF_CHECK(enable_locsums() == 0);
203 
204 	/* open raw socket */
205 	s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
206 	if (s == -1)
207 		atf_tc_fail_errno("raw socket");
208 
209 	/* then, send data */
210 	memset(&sin, 0, sizeof(sin));
211 	sin.sin_family = AF_INET;
212 	sin.sin_len = sizeof(sin);
213 	sin.sin_port = htons(12345);
214 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
215 
216 	if (rump_sys_sendto(s, data, sizeof(data), 0,
217 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
218 		atf_tc_fail_errno("sendto failed");
219 }
220 
221 ATF_TP_ADD_TCS(tp)
222 {
223 
224 	ATF_TP_ADD_TC(tp, loopmtu);
225 	ATF_TP_ADD_TC(tp, loopmtu_csum);
226 
227 	return atf_no_error();
228 }
229 
230