Remove building with NOCRYPTO option
[minix3.git] / tests / net / if_loop / t_pr.c
bloba22953f933b210f798d2c2374c6ed5824df5a7ed
1 /* $NetBSD: t_pr.c,v 1.7 2012/03/18 09:46:50 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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 */
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
39 #include <netinet/in.h>
40 #include <net/route.h>
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
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>
52 #include "../config/netconfig.c"
53 #include "../../h_macros.h"
56 * Prepare rump, configure interface and route to cause fragmentation
58 static void
59 setup(void)
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;
71 rump_init();
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");
78 if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
79 atf_tc_fail_errno("routing socket");
82 * set MTU for interface so that route MTU doesn't
83 * get overridden by it.
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");
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);
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");
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);
113 * Turn on checksums on loopback interfaces
115 static int
116 enable_locsums(void)
118 struct sysctlnode q, ans[256];
119 int mib[5], enable;
120 size_t alen;
121 unsigned i;
123 mib[0] = CTL_NET;
124 mib[1] = PF_INET;
125 mib[2] = IPPROTO_IP;
126 mib[3] = CTL_QUERY;
127 alen = sizeof(ans);
129 memset(&q, 0, sizeof(q));
130 q.sysctl_flags = SYSCTL_VERSION;
132 if (rump_sys___sysctl(mib, 4, ans, &alen, &q, sizeof(q)) == -1)
133 return -1;
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;
143 mib[3] = ans[i].sysctl_num;
145 enable = 1;
146 if (rump_sys___sysctl(mib, 4, NULL, NULL, &enable,
147 sizeof(enable)) == -1)
148 return errno;
150 return 0;
153 ATF_TC(loopmtu);
154 ATF_TC_HEAD(loopmtu, tc)
157 atf_tc_set_md_var(tc, "descr",
158 "test lo0 fragmentation (PR kern/43664)");
161 ATF_TC_BODY(loopmtu, tc)
163 struct sockaddr_in sin;
164 char data[2000];
165 int s;
167 setup();
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");
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");
181 if (rump_sys_sendto(s, data, sizeof(data), 0,
182 (struct sockaddr *)&sin, sizeof(sin)) == -1)
183 atf_tc_fail_errno("sendto failed");
186 ATF_TC(loopmtu_csum);
187 ATF_TC_HEAD(loopmtu_csum, tc)
190 atf_tc_set_md_var(tc, "descr",
191 "test lo0 fragmentation with checksums (PR kern/43664)");
194 ATF_TC_BODY(loopmtu_csum, tc)
196 struct sockaddr_in sin;
197 char data[2000];
198 int s;
200 setup();
202 ATF_CHECK(enable_locsums() == 0);
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");
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");
216 if (rump_sys_sendto(s, data, sizeof(data), 0,
217 (struct sockaddr *)&sin, sizeof(sin)) == -1)
218 atf_tc_fail_errno("sendto failed");
221 ATF_TP_ADD_TCS(tp)
224 ATF_TP_ADD_TC(tp, loopmtu);
225 ATF_TP_ADD_TC(tp, loopmtu_csum);
227 return atf_no_error();