1*63d1fd59SEnji Cooper /* $NetBSD: t_pr.c,v 1.8 2017/01/13 21:30:42 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1757718be8SEnji Cooper * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1857718be8SEnji Cooper * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1957718be8SEnji Cooper * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2057718be8SEnji Cooper * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2157718be8SEnji Cooper * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2257718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2357718be8SEnji Cooper * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2457718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2557718be8SEnji Cooper * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2657718be8SEnji Cooper * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2757718be8SEnji Cooper * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2857718be8SEnji Cooper */
2957718be8SEnji Cooper
3057718be8SEnji Cooper #include <sys/cdefs.h>
3157718be8SEnji Cooper #ifndef lint
32*63d1fd59SEnji Cooper __RCSID("$NetBSD: t_pr.c,v 1.8 2017/01/13 21:30:42 christos Exp $");
3357718be8SEnji Cooper #endif /* not lint */
3457718be8SEnji Cooper
3557718be8SEnji Cooper #include <sys/types.h>
3657718be8SEnji Cooper #include <sys/socket.h>
3757718be8SEnji Cooper #include <sys/sysctl.h>
3857718be8SEnji Cooper
3957718be8SEnji Cooper #include <netinet/in.h>
4057718be8SEnji Cooper #include <net/route.h>
4157718be8SEnji Cooper
4257718be8SEnji Cooper #include <rump/rump.h>
4357718be8SEnji Cooper #include <rump/rump_syscalls.h>
4457718be8SEnji Cooper
4557718be8SEnji Cooper #include <atf-c.h>
4657718be8SEnji Cooper #include <errno.h>
4757718be8SEnji Cooper #include <stdio.h>
4857718be8SEnji Cooper #include <stdlib.h>
4957718be8SEnji Cooper #include <string.h>
5057718be8SEnji Cooper #include <unistd.h>
5157718be8SEnji Cooper
5257718be8SEnji Cooper #include "../config/netconfig.c"
53*63d1fd59SEnji Cooper #include "h_macros.h"
5457718be8SEnji Cooper
5557718be8SEnji Cooper /*
5657718be8SEnji Cooper * Prepare rump, configure interface and route to cause fragmentation
5757718be8SEnji Cooper */
5857718be8SEnji Cooper static void
setup(void)5957718be8SEnji Cooper setup(void)
6057718be8SEnji Cooper {
6157718be8SEnji Cooper char ifname[IFNAMSIZ];
6257718be8SEnji Cooper struct {
6357718be8SEnji Cooper struct rt_msghdr m_rtm;
6457718be8SEnji Cooper struct sockaddr_in m_sin;
6557718be8SEnji Cooper } m_rtmsg;
6657718be8SEnji Cooper #define rtm m_rtmsg.m_rtm
6757718be8SEnji Cooper #define rsin m_rtmsg.m_sin
6857718be8SEnji Cooper struct ifreq ifr;
6957718be8SEnji Cooper int s;
7057718be8SEnji Cooper
7157718be8SEnji Cooper rump_init();
7257718be8SEnji Cooper
7357718be8SEnji Cooper /* first, config lo0 & route */
7457718be8SEnji Cooper strcpy(ifname, "lo0");
7557718be8SEnji Cooper netcfg_rump_if(ifname, "127.0.0.1", "255.0.0.0");
7657718be8SEnji Cooper netcfg_rump_route("127.0.0.1", "255.0.0.0", "127.0.0.1");
7757718be8SEnji Cooper
7857718be8SEnji Cooper if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
7957718be8SEnji Cooper atf_tc_fail_errno("routing socket");
8057718be8SEnji Cooper
8157718be8SEnji Cooper /*
8257718be8SEnji Cooper * set MTU for interface so that route MTU doesn't
8357718be8SEnji Cooper * get overridden by it.
8457718be8SEnji Cooper */
8557718be8SEnji Cooper memset(&ifr, 0, sizeof(ifr));
8657718be8SEnji Cooper strcpy(ifr.ifr_name, "lo0");
8757718be8SEnji Cooper ifr.ifr_mtu = 1300;
8857718be8SEnji Cooper if (rump_sys_ioctl(s, SIOCSIFMTU, &ifr) == -1)
8957718be8SEnji Cooper atf_tc_fail_errno("set mtu");
9057718be8SEnji Cooper
9157718be8SEnji Cooper /* change route MTU to 100 */
9257718be8SEnji Cooper memset(&m_rtmsg, 0, sizeof(m_rtmsg));
9357718be8SEnji Cooper rtm.rtm_type = RTM_CHANGE;
9457718be8SEnji Cooper rtm.rtm_flags = RTF_STATIC;
9557718be8SEnji Cooper rtm.rtm_version = RTM_VERSION;
9657718be8SEnji Cooper rtm.rtm_seq = 3;
9757718be8SEnji Cooper rtm.rtm_inits = RTV_MTU;
9857718be8SEnji Cooper rtm.rtm_addrs = RTA_DST;
9957718be8SEnji Cooper rtm.rtm_rmx.rmx_mtu = 100;
10057718be8SEnji Cooper rtm.rtm_msglen = sizeof(m_rtmsg);
10157718be8SEnji Cooper
10257718be8SEnji Cooper memset(&rsin, 0, sizeof(rsin));
10357718be8SEnji Cooper rsin.sin_family = AF_INET;
10457718be8SEnji Cooper rsin.sin_len = sizeof(rsin);
10557718be8SEnji Cooper rsin.sin_addr.s_addr = inet_addr("127.0.0.1");
10657718be8SEnji Cooper
10757718be8SEnji Cooper if (rump_sys_write(s, &m_rtmsg, sizeof(m_rtmsg)) != sizeof(m_rtmsg))
10857718be8SEnji Cooper atf_tc_fail_errno("set route mtu");
10957718be8SEnji Cooper rump_sys_close(s);
11057718be8SEnji Cooper }
11157718be8SEnji Cooper
11257718be8SEnji Cooper /*
11357718be8SEnji Cooper * Turn on checksums on loopback interfaces
11457718be8SEnji Cooper */
11557718be8SEnji Cooper static int
enable_locsums(void)11657718be8SEnji Cooper enable_locsums(void)
11757718be8SEnji Cooper {
11857718be8SEnji Cooper struct sysctlnode q, ans[256];
11957718be8SEnji Cooper int mib[5], enable;
12057718be8SEnji Cooper size_t alen;
12157718be8SEnji Cooper unsigned i;
12257718be8SEnji Cooper
12357718be8SEnji Cooper mib[0] = CTL_NET;
12457718be8SEnji Cooper mib[1] = PF_INET;
12557718be8SEnji Cooper mib[2] = IPPROTO_IP;
12657718be8SEnji Cooper mib[3] = CTL_QUERY;
12757718be8SEnji Cooper alen = sizeof(ans);
12857718be8SEnji Cooper
12957718be8SEnji Cooper memset(&q, 0, sizeof(q));
13057718be8SEnji Cooper q.sysctl_flags = SYSCTL_VERSION;
13157718be8SEnji Cooper
13257718be8SEnji Cooper if (rump_sys___sysctl(mib, 4, ans, &alen, &q, sizeof(q)) == -1)
13357718be8SEnji Cooper return -1;
13457718be8SEnji Cooper
13557718be8SEnji Cooper for (i = 0; i < __arraycount(ans); i++)
13657718be8SEnji Cooper if (strcmp("do_loopback_cksum", ans[i].sysctl_name) == 0)
13757718be8SEnji Cooper break;
13857718be8SEnji Cooper if (i == __arraycount(ans)) {
13957718be8SEnji Cooper errno = ENOENT;
14057718be8SEnji Cooper return -1;
14157718be8SEnji Cooper }
14257718be8SEnji Cooper
14357718be8SEnji Cooper mib[3] = ans[i].sysctl_num;
14457718be8SEnji Cooper
14557718be8SEnji Cooper enable = 1;
14657718be8SEnji Cooper if (rump_sys___sysctl(mib, 4, NULL, NULL, &enable,
14757718be8SEnji Cooper sizeof(enable)) == -1)
14857718be8SEnji Cooper return errno;
14957718be8SEnji Cooper
15057718be8SEnji Cooper return 0;
15157718be8SEnji Cooper }
15257718be8SEnji Cooper
15357718be8SEnji Cooper ATF_TC(loopmtu);
ATF_TC_HEAD(loopmtu,tc)15457718be8SEnji Cooper ATF_TC_HEAD(loopmtu, tc)
15557718be8SEnji Cooper {
15657718be8SEnji Cooper
15757718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
15857718be8SEnji Cooper "test lo0 fragmentation (PR kern/43664)");
15957718be8SEnji Cooper }
16057718be8SEnji Cooper
ATF_TC_BODY(loopmtu,tc)16157718be8SEnji Cooper ATF_TC_BODY(loopmtu, tc)
16257718be8SEnji Cooper {
16357718be8SEnji Cooper struct sockaddr_in sin;
16457718be8SEnji Cooper char data[2000];
16557718be8SEnji Cooper int s;
16657718be8SEnji Cooper
16757718be8SEnji Cooper setup();
16857718be8SEnji Cooper
16957718be8SEnji Cooper /* open raw socket */
17057718be8SEnji Cooper s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
17157718be8SEnji Cooper if (s == -1)
17257718be8SEnji Cooper atf_tc_fail_errno("raw socket");
17357718be8SEnji Cooper
17457718be8SEnji Cooper /* then, send data */
17557718be8SEnji Cooper memset(&sin, 0, sizeof(sin));
17657718be8SEnji Cooper sin.sin_family = AF_INET;
17757718be8SEnji Cooper sin.sin_len = sizeof(sin);
17857718be8SEnji Cooper sin.sin_port = htons(12345);
17957718be8SEnji Cooper sin.sin_addr.s_addr = inet_addr("127.0.0.1");
18057718be8SEnji Cooper
18157718be8SEnji Cooper if (rump_sys_sendto(s, data, sizeof(data), 0,
18257718be8SEnji Cooper (struct sockaddr *)&sin, sizeof(sin)) == -1)
18357718be8SEnji Cooper atf_tc_fail_errno("sendto failed");
18457718be8SEnji Cooper }
18557718be8SEnji Cooper
18657718be8SEnji Cooper ATF_TC(loopmtu_csum);
ATF_TC_HEAD(loopmtu_csum,tc)18757718be8SEnji Cooper ATF_TC_HEAD(loopmtu_csum, tc)
18857718be8SEnji Cooper {
18957718be8SEnji Cooper
19057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
19157718be8SEnji Cooper "test lo0 fragmentation with checksums (PR kern/43664)");
19257718be8SEnji Cooper }
19357718be8SEnji Cooper
ATF_TC_BODY(loopmtu_csum,tc)19457718be8SEnji Cooper ATF_TC_BODY(loopmtu_csum, tc)
19557718be8SEnji Cooper {
19657718be8SEnji Cooper struct sockaddr_in sin;
19757718be8SEnji Cooper char data[2000];
19857718be8SEnji Cooper int s;
19957718be8SEnji Cooper
20057718be8SEnji Cooper setup();
20157718be8SEnji Cooper
20257718be8SEnji Cooper ATF_CHECK(enable_locsums() == 0);
20357718be8SEnji Cooper
20457718be8SEnji Cooper /* open raw socket */
20557718be8SEnji Cooper s = rump_sys_socket(PF_INET, SOCK_RAW, 0);
20657718be8SEnji Cooper if (s == -1)
20757718be8SEnji Cooper atf_tc_fail_errno("raw socket");
20857718be8SEnji Cooper
20957718be8SEnji Cooper /* then, send data */
21057718be8SEnji Cooper memset(&sin, 0, sizeof(sin));
21157718be8SEnji Cooper sin.sin_family = AF_INET;
21257718be8SEnji Cooper sin.sin_len = sizeof(sin);
21357718be8SEnji Cooper sin.sin_port = htons(12345);
21457718be8SEnji Cooper sin.sin_addr.s_addr = inet_addr("127.0.0.1");
21557718be8SEnji Cooper
21657718be8SEnji Cooper if (rump_sys_sendto(s, data, sizeof(data), 0,
21757718be8SEnji Cooper (struct sockaddr *)&sin, sizeof(sin)) == -1)
21857718be8SEnji Cooper atf_tc_fail_errno("sendto failed");
21957718be8SEnji Cooper }
22057718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)22157718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
22257718be8SEnji Cooper {
22357718be8SEnji Cooper
22457718be8SEnji Cooper ATF_TP_ADD_TC(tp, loopmtu);
22557718be8SEnji Cooper ATF_TP_ADD_TC(tp, loopmtu_csum);
22657718be8SEnji Cooper
22757718be8SEnji Cooper return atf_no_error();
22857718be8SEnji Cooper }
22957718be8SEnji Cooper
230