xref: /minix3/tests/net/if_loop/t_pr.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_pr.c,v 1.7 2012/03/18 09:46:50 jruoho Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <sys/cdefs.h>
31*11be35a1SLionel Sambuc #ifndef lint
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_pr.c,v 1.7 2012/03/18 09:46:50 jruoho Exp $");
33*11be35a1SLionel Sambuc #endif /* not lint */
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <sys/types.h>
36*11be35a1SLionel Sambuc #include <sys/socket.h>
37*11be35a1SLionel Sambuc #include <sys/sysctl.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include <netinet/in.h>
40*11be35a1SLionel Sambuc #include <net/route.h>
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc #include <rump/rump.h>
43*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc #include <atf-c.h>
46*11be35a1SLionel Sambuc #include <errno.h>
47*11be35a1SLionel Sambuc #include <stdio.h>
48*11be35a1SLionel Sambuc #include <stdlib.h>
49*11be35a1SLionel Sambuc #include <string.h>
50*11be35a1SLionel Sambuc #include <unistd.h>
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc #include "../config/netconfig.c"
53*11be35a1SLionel Sambuc #include "../../h_macros.h"
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc /*
56*11be35a1SLionel Sambuc  * Prepare rump, configure interface and route to cause fragmentation
57*11be35a1SLionel Sambuc  */
58*11be35a1SLionel Sambuc static void
setup(void)59*11be35a1SLionel Sambuc setup(void)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc 	char ifname[IFNAMSIZ];
62*11be35a1SLionel Sambuc 	struct {
63*11be35a1SLionel Sambuc 		struct rt_msghdr m_rtm;
64*11be35a1SLionel Sambuc 		struct sockaddr_in m_sin;
65*11be35a1SLionel Sambuc 	} m_rtmsg;
66*11be35a1SLionel Sambuc #define rtm m_rtmsg.m_rtm
67*11be35a1SLionel Sambuc #define rsin m_rtmsg.m_sin
68*11be35a1SLionel Sambuc 	struct ifreq ifr;
69*11be35a1SLionel Sambuc 	int s;
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc 	rump_init();
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 	/* first, config lo0 & route */
74*11be35a1SLionel Sambuc 	strcpy(ifname, "lo0");
75*11be35a1SLionel Sambuc 	netcfg_rump_if(ifname, "127.0.0.1", "255.0.0.0");
76*11be35a1SLionel Sambuc 	netcfg_rump_route("127.0.0.1", "255.0.0.0", "127.0.0.1");
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc 	if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
79*11be35a1SLionel Sambuc 		atf_tc_fail_errno("routing socket");
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc 	/*
82*11be35a1SLionel Sambuc 	 * set MTU for interface so that route MTU doesn't
83*11be35a1SLionel Sambuc 	 * get overridden by it.
84*11be35a1SLionel Sambuc 	 */
85*11be35a1SLionel Sambuc 	memset(&ifr, 0, sizeof(ifr));
86*11be35a1SLionel Sambuc 	strcpy(ifr.ifr_name, "lo0");
87*11be35a1SLionel Sambuc 	ifr.ifr_mtu = 1300;
88*11be35a1SLionel Sambuc 	if (rump_sys_ioctl(s, SIOCSIFMTU, &ifr) == -1)
89*11be35a1SLionel Sambuc 		atf_tc_fail_errno("set mtu");
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 	/* change route MTU to 100 */
92*11be35a1SLionel Sambuc 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
93*11be35a1SLionel Sambuc 	rtm.rtm_type = RTM_CHANGE;
94*11be35a1SLionel Sambuc 	rtm.rtm_flags = RTF_STATIC;
95*11be35a1SLionel Sambuc 	rtm.rtm_version = RTM_VERSION;
96*11be35a1SLionel Sambuc 	rtm.rtm_seq = 3;
97*11be35a1SLionel Sambuc 	rtm.rtm_inits = RTV_MTU;
98*11be35a1SLionel Sambuc 	rtm.rtm_addrs = RTA_DST;
99*11be35a1SLionel Sambuc 	rtm.rtm_rmx.rmx_mtu = 100;
100*11be35a1SLionel Sambuc 	rtm.rtm_msglen = sizeof(m_rtmsg);
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc 	memset(&rsin, 0, sizeof(rsin));
103*11be35a1SLionel Sambuc 	rsin.sin_family = AF_INET;
104*11be35a1SLionel Sambuc 	rsin.sin_len = sizeof(rsin);
105*11be35a1SLionel Sambuc 	rsin.sin_addr.s_addr = inet_addr("127.0.0.1");
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 	if (rump_sys_write(s, &m_rtmsg, sizeof(m_rtmsg)) != sizeof(m_rtmsg))
108*11be35a1SLionel Sambuc 		atf_tc_fail_errno("set route mtu");
109*11be35a1SLionel Sambuc 	rump_sys_close(s);
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc /*
113*11be35a1SLionel Sambuc  * Turn on checksums on loopback interfaces
114*11be35a1SLionel Sambuc  */
115*11be35a1SLionel Sambuc static int
enable_locsums(void)116*11be35a1SLionel Sambuc enable_locsums(void)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc 	struct sysctlnode q, ans[256];
119*11be35a1SLionel Sambuc 	int mib[5], enable;
120*11be35a1SLionel Sambuc 	size_t alen;
121*11be35a1SLionel Sambuc 	unsigned i;
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 	mib[0] = CTL_NET;
124*11be35a1SLionel Sambuc 	mib[1] = PF_INET;
125*11be35a1SLionel Sambuc 	mib[2] = IPPROTO_IP;
126*11be35a1SLionel Sambuc 	mib[3] = CTL_QUERY;
127*11be35a1SLionel Sambuc 	alen = sizeof(ans);
128*11be35a1SLionel Sambuc 
129*11be35a1SLionel Sambuc 	memset(&q, 0, sizeof(q));
130*11be35a1SLionel Sambuc 	q.sysctl_flags = SYSCTL_VERSION;
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc 	if (rump_sys___sysctl(mib, 4, ans, &alen, &q, sizeof(q)) == -1)
133*11be35a1SLionel Sambuc 		return -1;
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(ans); i++)
136*11be35a1SLionel Sambuc 		if (strcmp("do_loopback_cksum", ans[i].sysctl_name) == 0)
137*11be35a1SLionel Sambuc 			break;
138*11be35a1SLionel Sambuc 	if (i == __arraycount(ans)) {
139*11be35a1SLionel Sambuc 		errno = ENOENT;
140*11be35a1SLionel Sambuc 		return -1;
141*11be35a1SLionel Sambuc 	}
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc 	mib[3] = ans[i].sysctl_num;
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc 	enable = 1;
146*11be35a1SLionel Sambuc 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &enable,
147*11be35a1SLionel Sambuc 	    sizeof(enable)) == -1)
148*11be35a1SLionel Sambuc 		return errno;
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 	return 0;
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc ATF_TC(loopmtu);
ATF_TC_HEAD(loopmtu,tc)154*11be35a1SLionel Sambuc ATF_TC_HEAD(loopmtu, tc)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
158*11be35a1SLionel Sambuc 	    "test lo0 fragmentation (PR kern/43664)");
159*11be35a1SLionel Sambuc }
160*11be35a1SLionel Sambuc 
ATF_TC_BODY(loopmtu,tc)161*11be35a1SLionel Sambuc ATF_TC_BODY(loopmtu, tc)
162*11be35a1SLionel Sambuc {
163*11be35a1SLionel Sambuc 	struct sockaddr_in sin;
164*11be35a1SLionel Sambuc 	char data[2000];
165*11be35a1SLionel Sambuc 	int s;
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc 	setup();
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 	/* open raw socket */
170*11be35a1SLionel Sambuc 	s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
171*11be35a1SLionel Sambuc 	if (s == -1)
172*11be35a1SLionel Sambuc 		atf_tc_fail_errno("raw socket");
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	/* then, send data */
175*11be35a1SLionel Sambuc 	memset(&sin, 0, sizeof(sin));
176*11be35a1SLionel Sambuc 	sin.sin_family = AF_INET;
177*11be35a1SLionel Sambuc 	sin.sin_len = sizeof(sin);
178*11be35a1SLionel Sambuc 	sin.sin_port = htons(12345);
179*11be35a1SLionel Sambuc 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc 	if (rump_sys_sendto(s, data, sizeof(data), 0,
182*11be35a1SLionel Sambuc 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
183*11be35a1SLionel Sambuc 		atf_tc_fail_errno("sendto failed");
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc ATF_TC(loopmtu_csum);
ATF_TC_HEAD(loopmtu_csum,tc)187*11be35a1SLionel Sambuc ATF_TC_HEAD(loopmtu_csum, tc)
188*11be35a1SLionel Sambuc {
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
191*11be35a1SLionel Sambuc 	    "test lo0 fragmentation with checksums (PR kern/43664)");
192*11be35a1SLionel Sambuc }
193*11be35a1SLionel Sambuc 
ATF_TC_BODY(loopmtu_csum,tc)194*11be35a1SLionel Sambuc ATF_TC_BODY(loopmtu_csum, tc)
195*11be35a1SLionel Sambuc {
196*11be35a1SLionel Sambuc 	struct sockaddr_in sin;
197*11be35a1SLionel Sambuc 	char data[2000];
198*11be35a1SLionel Sambuc 	int s;
199*11be35a1SLionel Sambuc 
200*11be35a1SLionel Sambuc 	setup();
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc 	ATF_CHECK(enable_locsums() == 0);
203*11be35a1SLionel Sambuc 
204*11be35a1SLionel Sambuc 	/* open raw socket */
205*11be35a1SLionel Sambuc 	s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
206*11be35a1SLionel Sambuc 	if (s == -1)
207*11be35a1SLionel Sambuc 		atf_tc_fail_errno("raw socket");
208*11be35a1SLionel Sambuc 
209*11be35a1SLionel Sambuc 	/* then, send data */
210*11be35a1SLionel Sambuc 	memset(&sin, 0, sizeof(sin));
211*11be35a1SLionel Sambuc 	sin.sin_family = AF_INET;
212*11be35a1SLionel Sambuc 	sin.sin_len = sizeof(sin);
213*11be35a1SLionel Sambuc 	sin.sin_port = htons(12345);
214*11be35a1SLionel Sambuc 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 	if (rump_sys_sendto(s, data, sizeof(data), 0,
217*11be35a1SLionel Sambuc 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
218*11be35a1SLionel Sambuc 		atf_tc_fail_errno("sendto failed");
219*11be35a1SLionel Sambuc }
220*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)221*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
222*11be35a1SLionel Sambuc {
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, loopmtu);
225*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, loopmtu_csum);
226*11be35a1SLionel Sambuc 
227*11be35a1SLionel Sambuc 	return atf_no_error();
228*11be35a1SLionel Sambuc }
229*11be35a1SLionel Sambuc 
230