1*11be35a1SLionel Sambuc /* $NetBSD: netconfig.c,v 1.8 2013/07/03 19:13:33 pooka Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc */
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc #include <sys/cdefs.h>
31*11be35a1SLionel Sambuc #ifndef lint
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: netconfig.c,v 1.8 2013/07/03 19:13:33 pooka Exp $");
33*11be35a1SLionel Sambuc #endif /* not lint */
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <sys/types.h>
36*11be35a1SLionel Sambuc #include <sys/socket.h>
37*11be35a1SLionel Sambuc #include <sys/ioctl.h>
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc #include <arpa/inet.h>
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc #include <net/route.h>
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc #include <netinet/in.h>
44*11be35a1SLionel Sambuc #include <netinet/in_systm.h>
45*11be35a1SLionel Sambuc #include <netinet/ip.h>
46*11be35a1SLionel Sambuc #include <netinet/ip_icmp.h>
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc #include <atf-c.h>
49*11be35a1SLionel Sambuc #include <err.h>
50*11be35a1SLionel Sambuc #include <errno.h>
51*11be35a1SLionel Sambuc #include <string.h>
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc #include <rump/rump.h>
54*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc #include "../../h_macros.h"
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc int noatf;
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc static void __unused
netcfg_rump_makeshmif(const char * busname,char * ifname)61*11be35a1SLionel Sambuc netcfg_rump_makeshmif(const char *busname, char *ifname)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc int rv, ifnum;
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
66*11be35a1SLionel Sambuc if (noatf)
67*11be35a1SLionel Sambuc err(1, "makeshmif: rump_pub_shmif_create %d", rv);
68*11be35a1SLionel Sambuc else
69*11be35a1SLionel Sambuc atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc sprintf(ifname, "shmif%d", ifnum);
72*11be35a1SLionel Sambuc }
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc static void __unused
netcfg_rump_if(const char * ifname,const char * addr,const char * mask)75*11be35a1SLionel Sambuc netcfg_rump_if(const char *ifname, const char *addr, const char *mask)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc struct ifaliasreq ia;
78*11be35a1SLionel Sambuc struct sockaddr_in *sin;
79*11be35a1SLionel Sambuc in_addr_t inaddr, inmask;
80*11be35a1SLionel Sambuc int s, rv;
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc s = -1;
83*11be35a1SLionel Sambuc if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
84*11be35a1SLionel Sambuc if (noatf)
85*11be35a1SLionel Sambuc err(1, "if config socket");
86*11be35a1SLionel Sambuc else
87*11be35a1SLionel Sambuc atf_tc_fail_errno("if config socket");
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc inaddr = inet_addr(addr);
91*11be35a1SLionel Sambuc inmask = inet_addr(mask);
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc /* Address */
94*11be35a1SLionel Sambuc memset(&ia, 0, sizeof(ia));
95*11be35a1SLionel Sambuc strcpy(ia.ifra_name, ifname);
96*11be35a1SLionel Sambuc sin = (struct sockaddr_in *)&ia.ifra_addr;
97*11be35a1SLionel Sambuc sin->sin_family = AF_INET;
98*11be35a1SLionel Sambuc sin->sin_len = sizeof(struct sockaddr_in);
99*11be35a1SLionel Sambuc sin->sin_addr.s_addr = inaddr;
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc /* Netmask */
102*11be35a1SLionel Sambuc sin = (struct sockaddr_in *)&ia.ifra_mask;
103*11be35a1SLionel Sambuc sin->sin_family = AF_INET;
104*11be35a1SLionel Sambuc sin->sin_len = sizeof(struct sockaddr_in);
105*11be35a1SLionel Sambuc sin->sin_addr.s_addr = inmask;
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc /* Broadcast address */
108*11be35a1SLionel Sambuc sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
109*11be35a1SLionel Sambuc sin->sin_family = AF_INET;
110*11be35a1SLionel Sambuc sin->sin_len = sizeof(struct sockaddr_in);
111*11be35a1SLionel Sambuc sin->sin_addr.s_addr = inaddr | ~inmask;
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
114*11be35a1SLionel Sambuc if (rv == -1) {
115*11be35a1SLionel Sambuc if (noatf)
116*11be35a1SLionel Sambuc err(1, "SIOCAIFADDR");
117*11be35a1SLionel Sambuc else
118*11be35a1SLionel Sambuc atf_tc_fail_errno("SIOCAIFADDR");
119*11be35a1SLionel Sambuc }
120*11be35a1SLionel Sambuc rump_sys_close(s);
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc static void __unused
netcfg_rump_route(const char * dst,const char * mask,const char * gw)124*11be35a1SLionel Sambuc netcfg_rump_route(const char *dst, const char *mask, const char *gw)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc size_t len;
127*11be35a1SLionel Sambuc struct {
128*11be35a1SLionel Sambuc struct rt_msghdr m_rtm;
129*11be35a1SLionel Sambuc uint8_t m_space[512];
130*11be35a1SLionel Sambuc } m_rtmsg;
131*11be35a1SLionel Sambuc #define rtm m_rtmsg.m_rtm
132*11be35a1SLionel Sambuc uint8_t *bp = m_rtmsg.m_space;
133*11be35a1SLionel Sambuc struct sockaddr_in sinstore;
134*11be35a1SLionel Sambuc int s, rv;
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
137*11be35a1SLionel Sambuc if (s == -1) {
138*11be35a1SLionel Sambuc if (noatf)
139*11be35a1SLionel Sambuc err(1, "routing socket");
140*11be35a1SLionel Sambuc else
141*11be35a1SLionel Sambuc atf_tc_fail_errno("routing socket");
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc memset(&m_rtmsg, 0, sizeof(m_rtmsg));
145*11be35a1SLionel Sambuc rtm.rtm_type = RTM_ADD;
146*11be35a1SLionel Sambuc rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
147*11be35a1SLionel Sambuc rtm.rtm_version = RTM_VERSION;
148*11be35a1SLionel Sambuc rtm.rtm_seq = 2;
149*11be35a1SLionel Sambuc rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc /* dst */
152*11be35a1SLionel Sambuc memset(&sinstore, 0, sizeof(sinstore));
153*11be35a1SLionel Sambuc sinstore.sin_family = AF_INET;
154*11be35a1SLionel Sambuc sinstore.sin_len = sizeof(sinstore);
155*11be35a1SLionel Sambuc sinstore.sin_addr.s_addr = inet_addr(dst);
156*11be35a1SLionel Sambuc memcpy(bp, &sinstore, sizeof(sinstore));
157*11be35a1SLionel Sambuc bp += sizeof(sinstore);
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc /* gw */
160*11be35a1SLionel Sambuc memset(&sinstore, 0, sizeof(sinstore));
161*11be35a1SLionel Sambuc sinstore.sin_family = AF_INET;
162*11be35a1SLionel Sambuc sinstore.sin_len = sizeof(sinstore);
163*11be35a1SLionel Sambuc sinstore.sin_addr.s_addr = inet_addr(gw);
164*11be35a1SLionel Sambuc memcpy(bp, &sinstore, sizeof(sinstore));
165*11be35a1SLionel Sambuc bp += sizeof(sinstore);
166*11be35a1SLionel Sambuc
167*11be35a1SLionel Sambuc /* netmask */
168*11be35a1SLionel Sambuc memset(&sinstore, 0, sizeof(sinstore));
169*11be35a1SLionel Sambuc sinstore.sin_family = AF_INET;
170*11be35a1SLionel Sambuc sinstore.sin_len = sizeof(sinstore);
171*11be35a1SLionel Sambuc sinstore.sin_addr.s_addr = inet_addr(mask);
172*11be35a1SLionel Sambuc memcpy(bp, &sinstore, sizeof(sinstore));
173*11be35a1SLionel Sambuc bp += sizeof(sinstore);
174*11be35a1SLionel Sambuc
175*11be35a1SLionel Sambuc len = bp - (uint8_t *)&m_rtmsg;
176*11be35a1SLionel Sambuc rtm.rtm_msglen = len;
177*11be35a1SLionel Sambuc
178*11be35a1SLionel Sambuc rv = rump_sys_write(s, &m_rtmsg, len);
179*11be35a1SLionel Sambuc if (rv != (int)len) {
180*11be35a1SLionel Sambuc if (noatf)
181*11be35a1SLionel Sambuc err(1, "write routing message");
182*11be35a1SLionel Sambuc else
183*11be35a1SLionel Sambuc atf_tc_fail_errno("write routing message");
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc rump_sys_close(s);
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc static bool __unused
netcfg_rump_pingtest(const char * dst,int ms_timo)189*11be35a1SLionel Sambuc netcfg_rump_pingtest(const char *dst, int ms_timo)
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc struct timeval tv;
192*11be35a1SLionel Sambuc struct sockaddr_in sin;
193*11be35a1SLionel Sambuc struct icmp icmp;
194*11be35a1SLionel Sambuc socklen_t slen;
195*11be35a1SLionel Sambuc int s;
196*11be35a1SLionel Sambuc bool rv = false;
197*11be35a1SLionel Sambuc
198*11be35a1SLionel Sambuc s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
199*11be35a1SLionel Sambuc if (s == -1)
200*11be35a1SLionel Sambuc return false;
201*11be35a1SLionel Sambuc tv.tv_sec = ms_timo / 1000;
202*11be35a1SLionel Sambuc tv.tv_usec = 1000 * (ms_timo % 1000);
203*11be35a1SLionel Sambuc if (rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
204*11be35a1SLionel Sambuc &tv, sizeof(tv)) == -1)
205*11be35a1SLionel Sambuc goto out;
206*11be35a1SLionel Sambuc
207*11be35a1SLionel Sambuc memset(&sin, 0, sizeof(sin));
208*11be35a1SLionel Sambuc sin.sin_len = sizeof(sin);
209*11be35a1SLionel Sambuc sin.sin_family = AF_INET;
210*11be35a1SLionel Sambuc sin.sin_addr.s_addr = inet_addr(dst);
211*11be35a1SLionel Sambuc
212*11be35a1SLionel Sambuc memset(&icmp, 0, sizeof(icmp));
213*11be35a1SLionel Sambuc icmp.icmp_type = ICMP_ECHO;
214*11be35a1SLionel Sambuc icmp.icmp_id = htons(37);
215*11be35a1SLionel Sambuc icmp.icmp_cksum = htons(0xf7da); /* precalc */
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc slen = sizeof(sin);
218*11be35a1SLionel Sambuc if (rump_sys_sendto(s, &icmp, sizeof(icmp), 0,
219*11be35a1SLionel Sambuc (struct sockaddr *)&sin, slen) == -1) {
220*11be35a1SLionel Sambuc goto out;
221*11be35a1SLionel Sambuc }
222*11be35a1SLionel Sambuc
223*11be35a1SLionel Sambuc if (rump_sys_recvfrom(s, &icmp, sizeof(icmp), 0,
224*11be35a1SLionel Sambuc (struct sockaddr *)&sin, &slen) == -1)
225*11be35a1SLionel Sambuc goto out;
226*11be35a1SLionel Sambuc
227*11be35a1SLionel Sambuc rv = true;
228*11be35a1SLionel Sambuc out:
229*11be35a1SLionel Sambuc rump_sys_close(s);
230*11be35a1SLionel Sambuc return rv;
231*11be35a1SLionel Sambuc }
232