Home | History | Annotate | Line # | Download | only in icmp
t_forward.c revision 1.5
      1  1.5  pooka /*	$NetBSD: t_forward.c,v 1.5 2010/07/26 14:10:31 pooka 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.5  pooka __RCSID("$NetBSD: t_forward.c,v 1.5 2010/07/26 14:10:31 pooka 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 	extern int rumpns_ip_defttl;
     71  1.1  pooka 	struct sockaddr_in sin;
     72  1.1  pooka 	char payload[1024];
     73  1.3  pooka 	char ifname[IFNAMSIZ];
     74  1.1  pooka 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
     75  1.1  pooka 	int nv;
     76  1.1  pooka 	int s;
     77  1.1  pooka 
     78  1.3  pooka 	netcfg_rump_makeshmif("bus1", ifname);
     79  1.4  pooka 	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
     80  1.3  pooka 	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
     81  1.1  pooka 
     82  1.1  pooka 	/* set global ttl to 1 */
     83  1.1  pooka 	nv = 1;
     84  1.1  pooka 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
     85  1.1  pooka 		atf_tc_fail_errno("set ttl");
     86  1.1  pooka 
     87  1.1  pooka 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
     88  1.1  pooka 	if (s == -1)
     89  1.1  pooka 		atf_tc_fail_errno("create send socket");
     90  1.1  pooka 
     91  1.1  pooka 	memset(&sin, 0, sizeof(sin));
     92  1.1  pooka 	sin.sin_len = sizeof(sin);
     93  1.1  pooka 	sin.sin_family = AF_INET;
     94  1.1  pooka 	sin.sin_port = htons(33434);
     95  1.1  pooka 	sin.sin_addr.s_addr = inet_addr("9.9.9.9");
     96  1.1  pooka 
     97  1.1  pooka 	/* send udp datagram with ttl == 1 */
     98  1.1  pooka 	if (rump_sys_sendto(s, payload, sizeof(payload), 0,
     99  1.1  pooka 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
    100  1.1  pooka 		atf_tc_fail_errno("sendto");
    101  1.1  pooka }
    102  1.1  pooka 
    103  1.1  pooka static void
    104  1.1  pooka router(void)
    105  1.1  pooka {
    106  1.1  pooka 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
    107  1.1  pooka 		    ICMPCTL_RETURNDATABYTES };
    108  1.5  pooka 	char ifname[IFNAMSIZ];
    109  1.1  pooka 	int nv;
    110  1.1  pooka 
    111  1.1  pooka 	/* set returndatabytes to 200 */
    112  1.1  pooka 	nv = 200;
    113  1.1  pooka 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
    114  1.1  pooka 		atf_tc_fail_errno("sysctl returndatabytes");
    115  1.1  pooka 
    116  1.5  pooka 	netcfg_rump_makeshmif("bus1", ifname);
    117  1.5  pooka 	netcfg_rump_if(ifname, "1.0.0.2", "255.255.255.0");
    118  1.1  pooka 
    119  1.1  pooka 	/*
    120  1.1  pooka 	 * Wait for parent to send us the data and for us to have
    121  1.1  pooka 	 * a chance to process it.
    122  1.1  pooka 	 */
    123  1.1  pooka 	sleep(1);
    124  1.1  pooka 	exit(0);
    125  1.1  pooka }
    126  1.1  pooka 
    127  1.1  pooka ATF_TC(returndatabytes);
    128  1.1  pooka ATF_TC_HEAD(returndatabytes, tc)
    129  1.1  pooka {
    130  1.1  pooka 
    131  1.1  pooka 	atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
    132  1.1  pooka 	    "packets can cause kernel panic");
    133  1.1  pooka 	atf_tc_set_md_var(tc, "use.fs", "true");
    134  1.1  pooka 	atf_tc_set_md_var(tc, "timeout", "4"); /* just in case */
    135  1.1  pooka 	/* PR kern/43548 */
    136  1.1  pooka }
    137  1.1  pooka 
    138  1.1  pooka ATF_TC_BODY(returndatabytes, tc)
    139  1.1  pooka {
    140  1.1  pooka 	pid_t cpid;
    141  1.1  pooka 	int status;
    142  1.1  pooka 
    143  1.1  pooka 	cpid = fork();
    144  1.1  pooka 	rump_init();
    145  1.1  pooka 
    146  1.1  pooka 	switch (cpid) {
    147  1.1  pooka 	case -1:
    148  1.1  pooka 		atf_tc_fail_errno("fork failed");
    149  1.1  pooka 	case 0:
    150  1.1  pooka 		router();
    151  1.1  pooka 		break;
    152  1.1  pooka 	default:
    153  1.1  pooka 		sendttl();
    154  1.1  pooka 		if (wait(&status) == -1)
    155  1.1  pooka 			atf_tc_fail_errno("wait");
    156  1.1  pooka 		if (WIFEXITED(status)) {
    157  1.1  pooka 			if (WEXITSTATUS(status))
    158  1.1  pooka 				atf_tc_fail("child exited with status %d",
    159  1.1  pooka 				    WEXITSTATUS(status));
    160  1.1  pooka 		} else {
    161  1.1  pooka 			atf_tc_fail("child died");
    162  1.1  pooka 		}
    163  1.1  pooka 	}
    164  1.1  pooka }
    165  1.1  pooka 
    166  1.1  pooka ATF_TP_ADD_TCS(tp)
    167  1.1  pooka {
    168  1.1  pooka 
    169  1.1  pooka 	ATF_TP_ADD_TC(tp, returndatabytes);
    170  1.1  pooka 
    171  1.1  pooka 	return atf_no_error();
    172  1.1  pooka }
    173