Home | History | Annotate | Line # | Download | only in config
netconfig.c revision 1.7
      1  1.7  pooka /*	$NetBSD: netconfig.c,v 1.7 2011/02/28 21:21:14 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.7  pooka __RCSID("$NetBSD: netconfig.c,v 1.7 2011/02/28 21:21:14 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/ioctl.h>
     38  1.1  pooka 
     39  1.2  pooka #include <arpa/inet.h>
     40  1.2  pooka 
     41  1.1  pooka #include <net/route.h>
     42  1.4  pooka 
     43  1.2  pooka #include <netinet/in.h>
     44  1.4  pooka #include <netinet/in_systm.h>
     45  1.4  pooka #include <netinet/ip.h>
     46  1.4  pooka #include <netinet/ip_icmp.h>
     47  1.1  pooka 
     48  1.1  pooka #include <atf-c.h>
     49  1.7  pooka #include <err.h>
     50  1.1  pooka #include <errno.h>
     51  1.1  pooka #include <string.h>
     52  1.1  pooka 
     53  1.1  pooka #include <rump/rump.h>
     54  1.1  pooka #include <rump/rump_syscalls.h>
     55  1.1  pooka 
     56  1.1  pooka #include "../../h_macros.h"
     57  1.1  pooka 
     58  1.7  pooka int noatf;
     59  1.7  pooka 
     60  1.6  pooka static void __unused
     61  1.1  pooka netcfg_rump_makeshmif(const char *busname, char *ifname)
     62  1.1  pooka {
     63  1.1  pooka 	int rv, ifnum;
     64  1.1  pooka 
     65  1.1  pooka 	if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
     66  1.7  pooka 		if (noatf)
     67  1.7  pooka 			err(1, "makeshmif: rump_pub_shmif_create %d", rv);
     68  1.7  pooka 		else
     69  1.7  pooka 			atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
     70  1.1  pooka 	}
     71  1.1  pooka 	sprintf(ifname, "shmif%d", ifnum);
     72  1.1  pooka }
     73  1.1  pooka 
     74  1.6  pooka static void __unused
     75  1.6  pooka netcfg_rump_makevirtif(int ifnum, char *ifname)
     76  1.6  pooka {
     77  1.6  pooka 	int rv;
     78  1.6  pooka 
     79  1.6  pooka 	if ((rv = rump_pub_virtif_create(ifnum)) != 0) {
     80  1.7  pooka 		if (noatf)
     81  1.7  pooka 			err(1, "makeshmif: rump_pub_virtif_create %d", rv);
     82  1.7  pooka 		else
     83  1.7  pooka 			atf_tc_fail("makeshmif: rump_pub_virtif_create %d", rv);
     84  1.6  pooka 	}
     85  1.6  pooka 	sprintf(ifname, "virt%d", ifnum);
     86  1.6  pooka }
     87  1.6  pooka 
     88  1.6  pooka static void __unused
     89  1.3  pooka netcfg_rump_if(const char *ifname, const char *addr, const char *mask)
     90  1.1  pooka {
     91  1.1  pooka 	struct ifaliasreq ia;
     92  1.1  pooka 	struct sockaddr_in *sin;
     93  1.3  pooka 	in_addr_t inaddr, inmask;
     94  1.1  pooka 	int s, rv;
     95  1.1  pooka 
     96  1.1  pooka 	s = -1;
     97  1.1  pooka 	if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
     98  1.7  pooka 		if (noatf)
     99  1.7  pooka 			err(1, "if config socket");
    100  1.7  pooka 		else
    101  1.7  pooka 			atf_tc_fail_errno("if config socket");
    102  1.1  pooka 	}
    103  1.1  pooka 
    104  1.3  pooka 	inaddr = inet_addr(addr);
    105  1.3  pooka 	inmask = inet_addr(mask);
    106  1.3  pooka 
    107  1.1  pooka 	/* Address */
    108  1.1  pooka 	memset(&ia, 0, sizeof(ia));
    109  1.1  pooka 	strcpy(ia.ifra_name, ifname);
    110  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_addr;
    111  1.1  pooka 	sin->sin_family = AF_INET;
    112  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
    113  1.3  pooka 	sin->sin_addr.s_addr = inaddr;
    114  1.1  pooka 
    115  1.1  pooka 	/* Netmask */
    116  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_mask;
    117  1.1  pooka 	sin->sin_family = AF_INET;
    118  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
    119  1.3  pooka 	sin->sin_addr.s_addr = inmask;
    120  1.1  pooka 
    121  1.1  pooka 	/* Broadcast address */
    122  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
    123  1.1  pooka 	sin->sin_family = AF_INET;
    124  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
    125  1.3  pooka 	sin->sin_addr.s_addr = inaddr | ~inmask;
    126  1.1  pooka 
    127  1.1  pooka 	rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
    128  1.7  pooka 	if (rv == -1) {
    129  1.7  pooka 		if (noatf)
    130  1.7  pooka 			err(1, "SIOCAIFADDR");
    131  1.7  pooka 		else
    132  1.7  pooka 			atf_tc_fail_errno("SIOCAIFADDR");
    133  1.1  pooka 	}
    134  1.1  pooka 	rump_sys_close(s);
    135  1.1  pooka }
    136  1.1  pooka 
    137  1.1  pooka static void __unused
    138  1.1  pooka netcfg_rump_route(const char *dst, const char *mask, const char *gw)
    139  1.1  pooka {
    140  1.1  pooka 	size_t len;
    141  1.1  pooka 	struct {
    142  1.1  pooka 		struct rt_msghdr m_rtm;
    143  1.1  pooka 		uint8_t m_space[512];
    144  1.1  pooka 	} m_rtmsg;
    145  1.1  pooka #define rtm m_rtmsg.m_rtm
    146  1.1  pooka 	uint8_t *bp = m_rtmsg.m_space;
    147  1.1  pooka 	struct sockaddr_in sinstore;
    148  1.1  pooka 	int s, rv;
    149  1.1  pooka 
    150  1.1  pooka 	s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
    151  1.1  pooka 	if (s == -1) {
    152  1.7  pooka 		if (noatf)
    153  1.7  pooka 			err(1, "routing socket");
    154  1.7  pooka 		else
    155  1.7  pooka 			atf_tc_fail_errno("routing socket");
    156  1.1  pooka 	}
    157  1.1  pooka 
    158  1.1  pooka 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    159  1.1  pooka 	rtm.rtm_type = RTM_ADD;
    160  1.1  pooka 	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
    161  1.1  pooka 	rtm.rtm_version = RTM_VERSION;
    162  1.1  pooka 	rtm.rtm_seq = 2;
    163  1.1  pooka 	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
    164  1.1  pooka 
    165  1.1  pooka 	/* dst */
    166  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    167  1.1  pooka 	sinstore.sin_family = AF_INET;
    168  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    169  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(dst);
    170  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    171  1.1  pooka 	bp += sizeof(sinstore);
    172  1.1  pooka 
    173  1.1  pooka 	/* gw */
    174  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    175  1.1  pooka 	sinstore.sin_family = AF_INET;
    176  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    177  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(gw);
    178  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    179  1.1  pooka 	bp += sizeof(sinstore);
    180  1.1  pooka 
    181  1.1  pooka 	/* netmask */
    182  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    183  1.1  pooka 	sinstore.sin_family = AF_INET;
    184  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    185  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(mask);
    186  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    187  1.1  pooka 	bp += sizeof(sinstore);
    188  1.1  pooka 
    189  1.1  pooka 	len = bp - (uint8_t *)&m_rtmsg;
    190  1.1  pooka 	rtm.rtm_msglen = len;
    191  1.1  pooka 
    192  1.1  pooka 	rv = rump_sys_write(s, &m_rtmsg, len);
    193  1.1  pooka 	if (rv != (int)len) {
    194  1.7  pooka 		if (noatf)
    195  1.7  pooka 			err(1, "write routing message");
    196  1.7  pooka 		else
    197  1.7  pooka 			atf_tc_fail_errno("write routing message");
    198  1.1  pooka 	}
    199  1.1  pooka 	rump_sys_close(s);
    200  1.1  pooka }
    201  1.4  pooka 
    202  1.5  pooka static bool __unused
    203  1.4  pooka netcfg_rump_pingtest(const char *dst, int ms_timo)
    204  1.4  pooka {
    205  1.4  pooka 	struct timeval tv;
    206  1.4  pooka 	struct sockaddr_in sin;
    207  1.4  pooka 	struct icmp icmp;
    208  1.4  pooka 	socklen_t slen;
    209  1.4  pooka 	int s;
    210  1.6  pooka 	bool rv = false;
    211  1.4  pooka 
    212  1.4  pooka 	s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
    213  1.4  pooka 	if (s == -1)
    214  1.4  pooka 		return false;
    215  1.4  pooka 	tv.tv_sec = ms_timo / 1000;
    216  1.4  pooka 	tv.tv_usec = 1000 * (ms_timo % 1000);
    217  1.4  pooka 	if (rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
    218  1.4  pooka 	    &tv, sizeof(tv)) == -1)
    219  1.6  pooka 		goto out;
    220  1.4  pooka 
    221  1.4  pooka 	memset(&sin, 0, sizeof(sin));
    222  1.4  pooka 	sin.sin_len = sizeof(sin);
    223  1.4  pooka 	sin.sin_family = AF_INET;
    224  1.4  pooka 	sin.sin_addr.s_addr = inet_addr(dst);
    225  1.4  pooka 
    226  1.4  pooka 	memset(&icmp, 0, sizeof(icmp));
    227  1.4  pooka 	icmp.icmp_type = ICMP_ECHO;
    228  1.4  pooka 	icmp.icmp_id = htons(37);
    229  1.4  pooka 	icmp.icmp_cksum = htons(0xf7da); /* precalc */
    230  1.4  pooka 
    231  1.4  pooka 	slen = sizeof(sin);
    232  1.4  pooka 	if (rump_sys_sendto(s, &icmp, sizeof(icmp), 0,
    233  1.6  pooka 	    (struct sockaddr *)&sin, slen) == -1) {
    234  1.6  pooka 		goto out;
    235  1.6  pooka 	}
    236  1.4  pooka 
    237  1.4  pooka 	if (rump_sys_recvfrom(s, &icmp, sizeof(icmp), 0,
    238  1.4  pooka 	    (struct sockaddr *)&sin, &slen) == -1)
    239  1.6  pooka 		goto out;
    240  1.4  pooka 
    241  1.6  pooka 	rv = true;
    242  1.6  pooka  out:
    243  1.6  pooka 	rump_sys_close(s);
    244  1.6  pooka 	return rv;
    245  1.4  pooka }
    246