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