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