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