1 /* $NetBSD: netconfig.c,v 1.8 2013/07/03 19:13:33 pooka Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: netconfig.c,v 1.8 2013/07/03 19:13:33 pooka Exp $");
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
39 #include <arpa/inet.h>
41 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_icmp.h>
53 #include <rump/rump.h>
54 #include <rump/rump_syscalls.h>
56 #include "../../h_macros.h"
61 netcfg_rump_makeshmif(const char *busname
, char *ifname
)
65 if ((rv
= rump_pub_shmif_create(busname
, &ifnum
)) != 0) {
67 err(1, "makeshmif: rump_pub_shmif_create %d", rv
);
69 atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv
);
71 sprintf(ifname
, "shmif%d", ifnum
);
75 netcfg_rump_if(const char *ifname
, const char *addr
, const char *mask
)
78 struct sockaddr_in
*sin
;
79 in_addr_t inaddr
, inmask
;
83 if ((s
= rump_sys_socket(PF_INET
, SOCK_DGRAM
, 0)) < 0) {
85 err(1, "if config socket");
87 atf_tc_fail_errno("if config socket");
90 inaddr
= inet_addr(addr
);
91 inmask
= inet_addr(mask
);
94 memset(&ia
, 0, sizeof(ia
));
95 strcpy(ia
.ifra_name
, ifname
);
96 sin
= (struct sockaddr_in
*)&ia
.ifra_addr
;
97 sin
->sin_family
= AF_INET
;
98 sin
->sin_len
= sizeof(struct sockaddr_in
);
99 sin
->sin_addr
.s_addr
= inaddr
;
102 sin
= (struct sockaddr_in
*)&ia
.ifra_mask
;
103 sin
->sin_family
= AF_INET
;
104 sin
->sin_len
= sizeof(struct sockaddr_in
);
105 sin
->sin_addr
.s_addr
= inmask
;
107 /* Broadcast address */
108 sin
= (struct sockaddr_in
*)&ia
.ifra_broadaddr
;
109 sin
->sin_family
= AF_INET
;
110 sin
->sin_len
= sizeof(struct sockaddr_in
);
111 sin
->sin_addr
.s_addr
= inaddr
| ~inmask
;
113 rv
= rump_sys_ioctl(s
, SIOCAIFADDR
, &ia
);
116 err(1, "SIOCAIFADDR");
118 atf_tc_fail_errno("SIOCAIFADDR");
124 netcfg_rump_route(const char *dst
, const char *mask
, const char *gw
)
128 struct rt_msghdr m_rtm
;
129 uint8_t m_space
[512];
131 #define rtm m_rtmsg.m_rtm
132 uint8_t *bp
= m_rtmsg
.m_space
;
133 struct sockaddr_in sinstore
;
136 s
= rump_sys_socket(PF_ROUTE
, SOCK_RAW
, 0);
139 err(1, "routing socket");
141 atf_tc_fail_errno("routing socket");
144 memset(&m_rtmsg
, 0, sizeof(m_rtmsg
));
145 rtm
.rtm_type
= RTM_ADD
;
146 rtm
.rtm_flags
= RTF_UP
| RTF_GATEWAY
| RTF_STATIC
;
147 rtm
.rtm_version
= RTM_VERSION
;
149 rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
| RTA_NETMASK
;
152 memset(&sinstore
, 0, sizeof(sinstore
));
153 sinstore
.sin_family
= AF_INET
;
154 sinstore
.sin_len
= sizeof(sinstore
);
155 sinstore
.sin_addr
.s_addr
= inet_addr(dst
);
156 memcpy(bp
, &sinstore
, sizeof(sinstore
));
157 bp
+= sizeof(sinstore
);
160 memset(&sinstore
, 0, sizeof(sinstore
));
161 sinstore
.sin_family
= AF_INET
;
162 sinstore
.sin_len
= sizeof(sinstore
);
163 sinstore
.sin_addr
.s_addr
= inet_addr(gw
);
164 memcpy(bp
, &sinstore
, sizeof(sinstore
));
165 bp
+= sizeof(sinstore
);
168 memset(&sinstore
, 0, sizeof(sinstore
));
169 sinstore
.sin_family
= AF_INET
;
170 sinstore
.sin_len
= sizeof(sinstore
);
171 sinstore
.sin_addr
.s_addr
= inet_addr(mask
);
172 memcpy(bp
, &sinstore
, sizeof(sinstore
));
173 bp
+= sizeof(sinstore
);
175 len
= bp
- (uint8_t *)&m_rtmsg
;
176 rtm
.rtm_msglen
= len
;
178 rv
= rump_sys_write(s
, &m_rtmsg
, len
);
179 if (rv
!= (int)len
) {
181 err(1, "write routing message");
183 atf_tc_fail_errno("write routing message");
189 netcfg_rump_pingtest(const char *dst
, int ms_timo
)
192 struct sockaddr_in sin
;
198 s
= rump_sys_socket(PF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
201 tv
.tv_sec
= ms_timo
/ 1000;
202 tv
.tv_usec
= 1000 * (ms_timo
% 1000);
203 if (rump_sys_setsockopt(s
, SOL_SOCKET
, SO_RCVTIMEO
,
204 &tv
, sizeof(tv
)) == -1)
207 memset(&sin
, 0, sizeof(sin
));
208 sin
.sin_len
= sizeof(sin
);
209 sin
.sin_family
= AF_INET
;
210 sin
.sin_addr
.s_addr
= inet_addr(dst
);
212 memset(&icmp
, 0, sizeof(icmp
));
213 icmp
.icmp_type
= ICMP_ECHO
;
214 icmp
.icmp_id
= htons(37);
215 icmp
.icmp_cksum
= htons(0xf7da); /* precalc */
218 if (rump_sys_sendto(s
, &icmp
, sizeof(icmp
), 0,
219 (struct sockaddr
*)&sin
, slen
) == -1) {
223 if (rump_sys_recvfrom(s
, &icmp
, sizeof(icmp
), 0,
224 (struct sockaddr
*)&sin
, &slen
) == -1)