Home | History | Annotate | Line # | Download | only in config
netconfig.c revision 1.2
      1 /*	$NetBSD: netconfig.c,v 1.2 2010/07/25 22:28:48 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: netconfig.c,v 1.2 2010/07/25 22:28:48 pooka Exp $");
     33 #endif /* not lint */
     34 
     35 #include <sys/types.h>
     36 #include <sys/socket.h>
     37 #include <sys/ioctl.h>
     38 
     39 #include <arpa/inet.h>
     40 
     41 #include <net/route.h>
     42 #include <netinet/in.h>
     43 
     44 #include <atf-c.h>
     45 #include <errno.h>
     46 #include <string.h>
     47 
     48 #include <rump/rump.h>
     49 #include <rump/rump_syscalls.h>
     50 
     51 #include "../../h_macros.h"
     52 
     53 static void
     54 netcfg_rump_makeshmif(const char *busname, char *ifname)
     55 {
     56 	int rv, ifnum;
     57 
     58 	if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
     59 		atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
     60 	}
     61 	sprintf(ifname, "shmif%d", ifnum);
     62 }
     63 
     64 static void
     65 netcfg_rump_if(const char *ifname,
     66 	const char *addr, const char *mask, const char *bcast)
     67 {
     68 	struct ifaliasreq ia;
     69 	struct sockaddr_in *sin;
     70 	int s, rv;
     71 
     72 	s = -1;
     73 	if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
     74 		atf_tc_fail_errno("if config socket");
     75 	}
     76 
     77 	/* Address */
     78 	memset(&ia, 0, sizeof(ia));
     79 	strcpy(ia.ifra_name, ifname);
     80 	sin = (struct sockaddr_in *)&ia.ifra_addr;
     81 	sin->sin_family = AF_INET;
     82 	sin->sin_len = sizeof(struct sockaddr_in);
     83 	sin->sin_addr.s_addr = inet_addr(addr);
     84 
     85 	/* Netmask */
     86 	sin = (struct sockaddr_in *)&ia.ifra_mask;
     87 	sin->sin_family = AF_INET;
     88 	sin->sin_len = sizeof(struct sockaddr_in);
     89 	sin->sin_addr.s_addr = inet_addr(mask);
     90 
     91 	/* Broadcast address */
     92 	sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
     93 	sin->sin_family = AF_INET;
     94 	sin->sin_len = sizeof(struct sockaddr_in);
     95 	sin->sin_addr.s_addr = inet_addr(bcast);
     96 
     97 	rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
     98 	if (rv) {
     99 		atf_tc_fail_errno("SIOCAIFADDR");
    100 	}
    101 	rump_sys_close(s);
    102 }
    103 
    104 static void __unused
    105 netcfg_rump_route(const char *dst, const char *mask, const char *gw)
    106 {
    107 	size_t len;
    108 	struct {
    109 		struct rt_msghdr m_rtm;
    110 		uint8_t m_space[512];
    111 	} m_rtmsg;
    112 #define rtm m_rtmsg.m_rtm
    113 	uint8_t *bp = m_rtmsg.m_space;
    114 	struct sockaddr_in sinstore;
    115 	int s, rv;
    116 
    117 	s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
    118 	if (s == -1) {
    119 		atf_tc_fail_errno("routing socket");
    120 	}
    121 
    122 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    123 	rtm.rtm_type = RTM_ADD;
    124 	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
    125 	rtm.rtm_version = RTM_VERSION;
    126 	rtm.rtm_seq = 2;
    127 	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
    128 
    129 	/* dst */
    130 	memset(&sinstore, 0, sizeof(sinstore));
    131 	sinstore.sin_family = AF_INET;
    132 	sinstore.sin_len = sizeof(sinstore);
    133 	sinstore.sin_addr.s_addr = inet_addr(dst);
    134 	memcpy(bp, &sinstore, sizeof(sinstore));
    135 	bp += sizeof(sinstore);
    136 
    137 	/* gw */
    138 	memset(&sinstore, 0, sizeof(sinstore));
    139 	sinstore.sin_family = AF_INET;
    140 	sinstore.sin_len = sizeof(sinstore);
    141 	sinstore.sin_addr.s_addr = inet_addr(gw);
    142 	memcpy(bp, &sinstore, sizeof(sinstore));
    143 	bp += sizeof(sinstore);
    144 
    145 	/* netmask */
    146 	memset(&sinstore, 0, sizeof(sinstore));
    147 	sinstore.sin_family = AF_INET;
    148 	sinstore.sin_len = sizeof(sinstore);
    149 	sinstore.sin_addr.s_addr = inet_addr(mask);
    150 	memcpy(bp, &sinstore, sizeof(sinstore));
    151 	bp += sizeof(sinstore);
    152 
    153 	len = bp - (uint8_t *)&m_rtmsg;
    154 	rtm.rtm_msglen = len;
    155 
    156 	rv = rump_sys_write(s, &m_rtmsg, len);
    157 	if (rv != (int)len) {
    158 		atf_tc_fail_errno("write routing message");
    159 	}
    160 	rump_sys_close(s);
    161 }
    162