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