Home | History | Annotate | Line # | Download | only in icmp
t_forward.c revision 1.4
      1 /*	$NetBSD: t_forward.c,v 1.4 2010/07/26 14:07:04 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      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.
     15  *
     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.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #ifndef lint
     32 __RCSID("$NetBSD: t_forward.c,v 1.4 2010/07/26 14:07:04 pooka Exp $");
     33 #endif /* not lint */
     34 
     35 #include <sys/types.h>
     36 #include <sys/socket.h>
     37 #include <sys/time.h>
     38 #include <sys/sysctl.h>
     39 #include <sys/wait.h>
     40 
     41 #include <arpa/inet.h>
     42 
     43 #include <netinet/in.h>
     44 #include <netinet/in_systm.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/ip_icmp.h>
     47 #include <netinet/icmp_var.h>
     48 #include <net/route.h>
     49 
     50 #include <rump/rump.h>
     51 #include <rump/rump_syscalls.h>
     52 
     53 #include <atf-c.h>
     54 #include <errno.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #include "../../h_macros.h"
     61 #include "../config/netconfig.c"
     62 
     63 static void
     64 configure_interface(const char *busname, const char *addr, const char *mask,
     65 	const char *bcast)
     66 {
     67 	char ifname[32];
     68 	struct ifaliasreq ia;
     69 	struct sockaddr_in *sin;
     70 	int s, rv, ifnum;
     71 
     72 	if ((s = rump_pub_shmif_create(busname, &ifnum)) != 0) {
     73 		atf_tc_fail("rump_pub_shmif_create(%d)", s);
     74 	}
     75 
     76 	if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
     77 		atf_tc_fail_errno("if config socket");
     78 	}
     79 	sprintf(ifname, "shmif%d", ifnum);
     80 
     81 	/* Address */
     82 	memset(&ia, 0, sizeof(ia));
     83 	strcpy(ia.ifra_name, ifname);
     84 	sin = (struct sockaddr_in *)&ia.ifra_addr;
     85 	sin->sin_family = AF_INET;
     86 	sin->sin_len = sizeof(struct sockaddr_in);
     87 	sin->sin_addr.s_addr = inet_addr(addr);
     88 
     89 	/* Netmask */
     90 	sin = (struct sockaddr_in *)&ia.ifra_mask;
     91 	sin->sin_family = AF_INET;
     92 	sin->sin_len = sizeof(struct sockaddr_in);
     93 	sin->sin_addr.s_addr = inet_addr(mask);
     94 
     95 	/* Broadcast address */
     96 	sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
     97 	sin->sin_family = AF_INET;
     98 	sin->sin_len = sizeof(struct sockaddr_in);
     99 	sin->sin_addr.s_addr = inet_addr(bcast);
    100 
    101 	rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
    102 	if (rv) {
    103 		atf_tc_fail_errno("SIOCAIFADDR");
    104 	}
    105 	rump_sys_close(s);
    106 }
    107 
    108 static void
    109 configure_routing(const char *dst, const char *mask, const char *gw)
    110 {
    111 	size_t len;
    112 	struct {
    113 		struct rt_msghdr m_rtm;
    114 		uint8_t m_space[512];
    115 	} m_rtmsg;
    116 #define rtm m_rtmsg.m_rtm
    117 	uint8_t *bp = m_rtmsg.m_space;
    118 	struct sockaddr_in sinstore;
    119 	int s, rv;
    120 
    121 	s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
    122 	if (s == -1) {
    123 		atf_tc_fail_errno("routing socket");
    124 	}
    125 
    126 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    127 	rtm.rtm_type = RTM_ADD;
    128 	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
    129 	rtm.rtm_version = RTM_VERSION;
    130 	rtm.rtm_seq = 2;
    131 	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
    132 
    133 	/* dst */
    134 	memset(&sinstore, 0, sizeof(sinstore));
    135 	sinstore.sin_family = AF_INET;
    136 	sinstore.sin_len = sizeof(sinstore);
    137 	sinstore.sin_addr.s_addr = inet_addr(dst);
    138 	memcpy(bp, &sinstore, sizeof(sinstore));
    139 	bp += sizeof(sinstore);
    140 
    141 	/* gw */
    142 	memset(&sinstore, 0, sizeof(sinstore));
    143 	sinstore.sin_family = AF_INET;
    144 	sinstore.sin_len = sizeof(sinstore);
    145 	sinstore.sin_addr.s_addr = inet_addr(gw);
    146 	memcpy(bp, &sinstore, sizeof(sinstore));
    147 	bp += sizeof(sinstore);
    148 
    149 	/* netmask */
    150 	memset(&sinstore, 0, sizeof(sinstore));
    151 	sinstore.sin_family = AF_INET;
    152 	sinstore.sin_len = sizeof(sinstore);
    153 	sinstore.sin_addr.s_addr = inet_addr(mask);
    154 	memcpy(bp, &sinstore, sizeof(sinstore));
    155 	bp += sizeof(sinstore);
    156 
    157 	len = bp - (uint8_t *)&m_rtmsg;
    158 	rtm.rtm_msglen = len;
    159 
    160 	rv = rump_sys_write(s, &m_rtmsg, len);
    161 	if (rv != (int)len) {
    162 		atf_tc_fail_errno("write routing message");
    163 	}
    164 	rump_sys_close(s);
    165 }
    166 
    167 /*
    168  * Since our maxttl is in our private namespace, we don't need raw packet
    169  * construction like traceroute(8) -- we can just use the global maxttl.
    170  */
    171 static void
    172 sendttl(void)
    173 {
    174 	extern int rumpns_ip_defttl;
    175 	struct sockaddr_in sin;
    176 	char payload[1024];
    177 	char ifname[IFNAMSIZ];
    178 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_IP, IPCTL_DEFTTL };
    179 	int nv;
    180 	int s;
    181 
    182 	netcfg_rump_makeshmif("bus1", ifname);
    183 	netcfg_rump_if(ifname, "1.0.0.1", "255.255.255.0");
    184 	netcfg_rump_route("0.0.0.0", "0.0.0.0", "1.0.0.2"); /* default router */
    185 
    186 	/* set global ttl to 1 */
    187 	nv = 1;
    188 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
    189 		atf_tc_fail_errno("set ttl");
    190 
    191 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
    192 	if (s == -1)
    193 		atf_tc_fail_errno("create send socket");
    194 
    195 	memset(&sin, 0, sizeof(sin));
    196 	sin.sin_len = sizeof(sin);
    197 	sin.sin_family = AF_INET;
    198 	sin.sin_port = htons(33434);
    199 	sin.sin_addr.s_addr = inet_addr("9.9.9.9");
    200 
    201 	/* send udp datagram with ttl == 1 */
    202 	if (rump_sys_sendto(s, payload, sizeof(payload), 0,
    203 	    (struct sockaddr *)&sin, sizeof(sin)) == -1)
    204 		atf_tc_fail_errno("sendto");
    205 }
    206 
    207 static void
    208 router(void)
    209 {
    210 	int mib[4] = { CTL_NET, PF_INET, IPPROTO_ICMP,
    211 		    ICMPCTL_RETURNDATABYTES };
    212 	int nv;
    213 
    214 	/* set returndatabytes to 200 */
    215 	nv = 200;
    216 	if (rump_sys___sysctl(mib, 4, NULL, NULL, &nv, sizeof(nv)) == -1)
    217 		atf_tc_fail_errno("sysctl returndatabytes");
    218 
    219 	configure_interface("bus1", "1.0.0.2", "255.255.255.0", "1.0.0.255");
    220 
    221 	/*
    222 	 * Wait for parent to send us the data and for us to have
    223 	 * a chance to process it.
    224 	 */
    225 	sleep(1);
    226 	exit(0);
    227 }
    228 
    229 ATF_TC(returndatabytes);
    230 ATF_TC_HEAD(returndatabytes, tc)
    231 {
    232 
    233 	atf_tc_set_md_var(tc, "descr", "icmp.returndatabytes with certain "
    234 	    "packets can cause kernel panic");
    235 	atf_tc_set_md_var(tc, "use.fs", "true");
    236 	atf_tc_set_md_var(tc, "timeout", "4"); /* just in case */
    237 	/* PR kern/43548 */
    238 }
    239 
    240 ATF_TC_BODY(returndatabytes, tc)
    241 {
    242 	pid_t cpid;
    243 	int status;
    244 
    245 	cpid = fork();
    246 	rump_init();
    247 
    248 	switch (cpid) {
    249 	case -1:
    250 		atf_tc_fail_errno("fork failed");
    251 	case 0:
    252 		router();
    253 		break;
    254 	default:
    255 		sendttl();
    256 		if (wait(&status) == -1)
    257 			atf_tc_fail_errno("wait");
    258 		if (WIFEXITED(status)) {
    259 			if (WEXITSTATUS(status))
    260 				atf_tc_fail("child exited with status %d",
    261 				    WEXITSTATUS(status));
    262 		} else {
    263 			atf_tc_fail("child died");
    264 		}
    265 	}
    266 }
    267 
    268 ATF_TP_ADD_TCS(tp)
    269 {
    270 
    271 	ATF_TP_ADD_TC(tp, returndatabytes);
    272 
    273 	return atf_no_error();
    274 }
    275