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