Home | History | Annotate | Line # | Download | only in icmp
t_forward.c revision 1.9
      1  1.9  martin /*	$NetBSD: t_forward.c,v 1.9 2015/02/26 13:03:21 martin Exp $	*/
      2  1.1   pooka 
      3  1.1   pooka /*-
      4  1.1   pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1   pooka  * All rights reserved.
      6  1.1   pooka  *
      7  1.1   pooka  * Redistribution and use in source and binary forms, with or without
      8  1.1   pooka  * modification, are permitted provided that the following conditions
      9  1.1   pooka  * are met:
     10  1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     11  1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     12  1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   pooka  *    documentation and/or other materials provided with the distribution.
     15  1.1   pooka  *
     16  1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1   pooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1   pooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1   pooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   pooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1   pooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1   pooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1   pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1   pooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1   pooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1   pooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1   pooka  */
     29  1.1   pooka 
     30  1.1   pooka #include <sys/cdefs.h>
     31  1.1   pooka #ifndef lint
     32  1.9  martin __RCSID("$NetBSD: t_forward.c,v 1.9 2015/02/26 13:03:21 martin Exp $");
     33  1.1   pooka #endif /* not lint */
     34  1.1   pooka 
     35  1.1   pooka #include <sys/types.h>
     36  1.1   pooka #include <sys/socket.h>
     37  1.1   pooka #include <sys/time.h>
     38  1.1   pooka #include <sys/sysctl.h>
     39  1.1   pooka #include <sys/wait.h>
     40  1.1   pooka 
     41  1.1   pooka #include <arpa/inet.h>
     42  1.1   pooka 
     43  1.1   pooka #include <netinet/in.h>
     44  1.1   pooka #include <netinet/in_systm.h>
     45  1.1   pooka #include <netinet/ip.h>
     46  1.1   pooka #include <netinet/ip_icmp.h>
     47  1.1   pooka #include <netinet/icmp_var.h>
     48  1.1   pooka #include <net/route.h>
     49  1.1   pooka 
     50  1.1   pooka #include <rump/rump.h>
     51  1.1   pooka #include <rump/rump_syscalls.h>
     52  1.1   pooka 
     53  1.1   pooka #include <atf-c.h>
     54  1.1   pooka #include <errno.h>
     55  1.1   pooka #include <stdio.h>
     56  1.1   pooka #include <stdlib.h>
     57  1.1   pooka #include <string.h>
     58  1.1   pooka #include <unistd.h>
     59  1.1   pooka 
     60  1.1   pooka #include "../../h_macros.h"
     61  1.3   pooka #include "../config/netconfig.c"
     62  1.1   pooka 
     63  1.1   pooka /*
     64  1.1   pooka  * Since our maxttl is in our private namespace, we don't need raw packet
     65  1.1   pooka  * construction like traceroute(8) -- we can just use the global maxttl.
     66  1.1   pooka  */
     67  1.1   pooka static void
     68  1.1   pooka sendttl(void)
     69  1.1   pooka {
     70  1.1   pooka 	struct sockaddr_in sin;
     71  1.1   pooka 	char payload[1024];
     72  1.3   pooka 	char ifname[IFNAMSIZ];
     73  1.1   pooka 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
     74  1.1   pooka 	int nv;
     75  1.1   pooka 	int s;
     76  1.1   pooka 
     77  1.3   pooka 	netcfg_rump_makeshmif("bus1", ifname);
     78  1.4   pooka 	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
     79  1.3   pooka 	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
     80  1.1   pooka 
     81  1.1   pooka 	/* set global ttl to 1 */
     82  1.1   pooka 	nv = 1;
     83  1.1   pooka 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
     84  1.1   pooka 		atf_tc_fail_errno("set ttl");
     85  1.1   pooka 
     86  1.1   pooka 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
     87  1.1   pooka 	if (s == -1)
     88  1.1   pooka 		atf_tc_fail_errno("create send socket");
     89  1.1   pooka 
     90  1.1   pooka 	memset(&sin, 0, sizeof(sin));
     91  1.1   pooka 	sin.sin_len = sizeof(sin);
     92  1.1   pooka 	sin.sin_family = AF_INET;
     93  1.1   pooka 	sin.sin_port = htons(33434);
     94  1.1   pooka 	sin.sin_addr.s_addr = inet_addr("9.9.9.9");
     95  1.1   pooka 
     96  1.1   pooka 	/* send udp datagram with ttl == 1 */
     97  1.1   pooka 	if (rump_sys_sendto(s, payload, sizeof(payload), 0,
     98  1.1   pooka 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
     99  1.1   pooka 		atf_tc_fail_errno("sendto");
    100  1.1   pooka }
    101  1.1   pooka 
    102  1.1   pooka static void
    103  1.1   pooka router(void)
    104  1.1   pooka {
    105  1.1   pooka 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
    106  1.1   pooka 		    ICMPCTL_RETURNDATABYTES };
    107  1.5   pooka 	char ifname[IFNAMSIZ];
    108  1.1   pooka 	int nv;
    109  1.1   pooka 
    110  1.1   pooka 	/* set returndatabytes to 200 */
    111  1.1   pooka 	nv = 200;
    112  1.1   pooka 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
    113  1.1   pooka 		atf_tc_fail_errno("sysctl returndatabytes");
    114  1.1   pooka 
    115  1.5   pooka 	netcfg_rump_makeshmif("bus1", ifname);
    116  1.5   pooka 	netcfg_rump_if(ifname, "1.0.0.2", "255.255.255.0");
    117  1.1   pooka 
    118  1.1   pooka 	/*
    119  1.1   pooka 	 * Wait for parent to send us the data and for us to have
    120  1.1   pooka 	 * a chance to process it.
    121  1.1   pooka 	 */
    122  1.1   pooka 	sleep(1);
    123  1.1   pooka 	exit(0);
    124  1.1   pooka }
    125  1.1   pooka 
    126  1.1   pooka ATF_TC(returndatabytes);
    127  1.1   pooka ATF_TC_HEAD(returndatabytes, tc)
    128  1.1   pooka {
    129  1.1   pooka 
    130  1.1   pooka 	atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
    131  1.8  jruoho 	    "packets can cause kernel panic (PR kern/43548)");
    132  1.9  martin 	atf_tc_set_md_var(tc, "timeout", "20"); /* just in case */
    133  1.1   pooka }
    134  1.1   pooka 
    135  1.1   pooka ATF_TC_BODY(returndatabytes, tc)
    136  1.1   pooka {
    137  1.1   pooka 	pid_t cpid;
    138  1.1   pooka 	int status;
    139  1.1   pooka 
    140  1.1   pooka 	cpid = fork();
    141  1.1   pooka 	rump_init();
    142  1.1   pooka 
    143  1.1   pooka 	switch (cpid) {
    144  1.1   pooka 	case -1:
    145  1.1   pooka 		atf_tc_fail_errno("fork failed");
    146  1.1   pooka 	case 0:
    147  1.1   pooka 		router();
    148  1.1   pooka 		break;
    149  1.1   pooka 	default:
    150  1.1   pooka 		sendttl();
    151  1.1   pooka 		if (wait(&status) == -1)
    152  1.1   pooka 			atf_tc_fail_errno("wait");
    153  1.1   pooka 		if (WIFEXITED(status)) {
    154  1.1   pooka 			if (WEXITSTATUS(status))
    155  1.1   pooka 				atf_tc_fail("child exited with status %d",
    156  1.1   pooka 				    WEXITSTATUS(status));
    157  1.1   pooka 		} else {
    158  1.1   pooka 			atf_tc_fail("child died");
    159  1.1   pooka 		}
    160  1.1   pooka 	}
    161  1.1   pooka }
    162  1.1   pooka 
    163  1.1   pooka ATF_TP_ADD_TCS(tp)
    164  1.1   pooka {
    165  1.1   pooka 
    166  1.1   pooka 	ATF_TP_ADD_TC(tp, returndatabytes);
    167  1.1   pooka 
    168  1.1   pooka 	return atf_no_error();
    169  1.1   pooka }
    170