1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_forward.c,v 1.9 2015/02/26 13:03:21 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <sys/cdefs.h>
3111be35a1SLionel Sambuc #ifndef lint
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_forward.c,v 1.9 2015/02/26 13:03:21 martin Exp $");
3311be35a1SLionel Sambuc #endif /* not lint */
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include <sys/types.h>
3611be35a1SLionel Sambuc #include <sys/socket.h>
3711be35a1SLionel Sambuc #include <sys/time.h>
3811be35a1SLionel Sambuc #include <sys/sysctl.h>
3911be35a1SLionel Sambuc #include <sys/wait.h>
4011be35a1SLionel Sambuc
4111be35a1SLionel Sambuc #include <arpa/inet.h>
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc #include <netinet/in.h>
4411be35a1SLionel Sambuc #include <netinet/in_systm.h>
4511be35a1SLionel Sambuc #include <netinet/ip.h>
4611be35a1SLionel Sambuc #include <netinet/ip_icmp.h>
4711be35a1SLionel Sambuc #include <netinet/icmp_var.h>
4811be35a1SLionel Sambuc #include <net/route.h>
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc #include <rump/rump.h>
5111be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc #include <atf-c.h>
5411be35a1SLionel Sambuc #include <errno.h>
5511be35a1SLionel Sambuc #include <stdio.h>
5611be35a1SLionel Sambuc #include <stdlib.h>
5711be35a1SLionel Sambuc #include <string.h>
5811be35a1SLionel Sambuc #include <unistd.h>
5911be35a1SLionel Sambuc
6011be35a1SLionel Sambuc #include "../../h_macros.h"
6111be35a1SLionel Sambuc #include "../config/netconfig.c"
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc /*
6411be35a1SLionel Sambuc * Since our maxttl is in our private namespace, we don't need raw packet
6511be35a1SLionel Sambuc * construction like traceroute(8) -- we can just use the global maxttl.
6611be35a1SLionel Sambuc */
6711be35a1SLionel Sambuc static void
sendttl(void)6811be35a1SLionel Sambuc sendttl(void)
6911be35a1SLionel Sambuc {
7011be35a1SLionel Sambuc struct sockaddr_in sin;
7111be35a1SLionel Sambuc char payload[1024];
7211be35a1SLionel Sambuc char ifname[IFNAMSIZ];
7311be35a1SLionel Sambuc int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
7411be35a1SLionel Sambuc int nv;
7511be35a1SLionel Sambuc int s;
7611be35a1SLionel Sambuc
7711be35a1SLionel Sambuc netcfg_rump_makeshmif("bus1", ifname);
7811be35a1SLionel Sambuc netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
7911be35a1SLionel Sambuc netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
8011be35a1SLionel Sambuc
8111be35a1SLionel Sambuc /* set global ttl to 1 */
8211be35a1SLionel Sambuc nv = 1;
8311be35a1SLionel Sambuc if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
8411be35a1SLionel Sambuc atf_tc_fail_errno("set ttl");
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
8711be35a1SLionel Sambuc if (s == -1)
8811be35a1SLionel Sambuc atf_tc_fail_errno("create send socket");
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc memset(&sin, 0, sizeof(sin));
9111be35a1SLionel Sambuc sin.sin_len = sizeof(sin);
9211be35a1SLionel Sambuc sin.sin_family = AF_INET;
9311be35a1SLionel Sambuc sin.sin_port = htons(33434);
9411be35a1SLionel Sambuc sin.sin_addr.s_addr = inet_addr("9.9.9.9");
9511be35a1SLionel Sambuc
9611be35a1SLionel Sambuc /* send udp datagram with ttl == 1 */
9711be35a1SLionel Sambuc if (rump_sys_sendto(s, payload, sizeof(payload), 0,
9811be35a1SLionel Sambuc (struct sockaddr *)&sin, sizeof(sin)) == -1)
9911be35a1SLionel Sambuc atf_tc_fail_errno("sendto");
10011be35a1SLionel Sambuc }
10111be35a1SLionel Sambuc
10211be35a1SLionel Sambuc static void
router(void)10311be35a1SLionel Sambuc router(void)
10411be35a1SLionel Sambuc {
10511be35a1SLionel Sambuc int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
10611be35a1SLionel Sambuc ICMPCTL_RETURNDATABYTES };
10711be35a1SLionel Sambuc char ifname[IFNAMSIZ];
10811be35a1SLionel Sambuc int nv;
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc /* set returndatabytes to 200 */
11111be35a1SLionel Sambuc nv = 200;
11211be35a1SLionel Sambuc if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
11311be35a1SLionel Sambuc atf_tc_fail_errno("sysctl returndatabytes");
11411be35a1SLionel Sambuc
11511be35a1SLionel Sambuc netcfg_rump_makeshmif("bus1", ifname);
11611be35a1SLionel Sambuc netcfg_rump_if(ifname, "1.0.0.2", "255.255.255.0");
11711be35a1SLionel Sambuc
11811be35a1SLionel Sambuc /*
11911be35a1SLionel Sambuc * Wait for parent to send us the data and for us to have
12011be35a1SLionel Sambuc * a chance to process it.
12111be35a1SLionel Sambuc */
12211be35a1SLionel Sambuc sleep(1);
12311be35a1SLionel Sambuc exit(0);
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc ATF_TC(returndatabytes);
ATF_TC_HEAD(returndatabytes,tc)12711be35a1SLionel Sambuc ATF_TC_HEAD(returndatabytes, tc)
12811be35a1SLionel Sambuc {
12911be35a1SLionel Sambuc
13011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
13111be35a1SLionel Sambuc "packets can cause kernel panic (PR kern/43548)");
132*0a6a1f1dSLionel Sambuc atf_tc_set_md_var(tc, "timeout", "20"); /* just in case */
13311be35a1SLionel Sambuc }
13411be35a1SLionel Sambuc
ATF_TC_BODY(returndatabytes,tc)13511be35a1SLionel Sambuc ATF_TC_BODY(returndatabytes, tc)
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc pid_t cpid;
13811be35a1SLionel Sambuc int status;
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc cpid = fork();
14111be35a1SLionel Sambuc rump_init();
14211be35a1SLionel Sambuc
14311be35a1SLionel Sambuc switch (cpid) {
14411be35a1SLionel Sambuc case -1:
14511be35a1SLionel Sambuc atf_tc_fail_errno("fork failed");
14611be35a1SLionel Sambuc case 0:
14711be35a1SLionel Sambuc router();
14811be35a1SLionel Sambuc break;
14911be35a1SLionel Sambuc default:
15011be35a1SLionel Sambuc sendttl();
15111be35a1SLionel Sambuc if (wait(&status) == -1)
15211be35a1SLionel Sambuc atf_tc_fail_errno("wait");
15311be35a1SLionel Sambuc if (WIFEXITED(status)) {
15411be35a1SLionel Sambuc if (WEXITSTATUS(status))
15511be35a1SLionel Sambuc atf_tc_fail("child exited with status %d",
15611be35a1SLionel Sambuc WEXITSTATUS(status));
15711be35a1SLionel Sambuc } else {
15811be35a1SLionel Sambuc atf_tc_fail("child died");
15911be35a1SLionel Sambuc }
16011be35a1SLionel Sambuc }
16111be35a1SLionel Sambuc }
16211be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)16311be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
16411be35a1SLionel Sambuc {
16511be35a1SLionel Sambuc
16611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, returndatabytes);
16711be35a1SLionel Sambuc
16811be35a1SLionel Sambuc return atf_no_error();
16911be35a1SLionel Sambuc }
170