netconfig.c revision 1.3 1 1.3 pooka /* $NetBSD: netconfig.c,v 1.3 2010/07/26 14:07:04 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * Redistribution and use in source and binary forms, with or without
8 1.1 pooka * modification, are permitted provided that the following conditions
9 1.1 pooka * are met:
10 1.1 pooka * 1. Redistributions of source code must retain the above copyright
11 1.1 pooka * notice, this list of conditions and the following disclaimer.
12 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer in the
14 1.1 pooka * documentation and/or other materials provided with the distribution.
15 1.1 pooka *
16 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.1 pooka * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 pooka * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1 pooka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 pooka * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.1 pooka * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1 pooka * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1 pooka * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 pooka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1 pooka * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.1 pooka #include <sys/cdefs.h>
31 1.1 pooka #ifndef lint
32 1.3 pooka __RCSID("$NetBSD: netconfig.c,v 1.3 2010/07/26 14:07:04 pooka Exp $");
33 1.1 pooka #endif /* not lint */
34 1.1 pooka
35 1.1 pooka #include <sys/types.h>
36 1.1 pooka #include <sys/socket.h>
37 1.1 pooka #include <sys/ioctl.h>
38 1.1 pooka
39 1.2 pooka #include <arpa/inet.h>
40 1.2 pooka
41 1.1 pooka #include <net/route.h>
42 1.2 pooka #include <netinet/in.h>
43 1.1 pooka
44 1.1 pooka #include <atf-c.h>
45 1.1 pooka #include <errno.h>
46 1.1 pooka #include <string.h>
47 1.1 pooka
48 1.1 pooka #include <rump/rump.h>
49 1.1 pooka #include <rump/rump_syscalls.h>
50 1.1 pooka
51 1.1 pooka #include "../../h_macros.h"
52 1.1 pooka
53 1.1 pooka static void
54 1.1 pooka netcfg_rump_makeshmif(const char *busname, char *ifname)
55 1.1 pooka {
56 1.1 pooka int rv, ifnum;
57 1.1 pooka
58 1.1 pooka if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
59 1.1 pooka atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
60 1.1 pooka }
61 1.1 pooka sprintf(ifname, "shmif%d", ifnum);
62 1.1 pooka }
63 1.1 pooka
64 1.1 pooka static void
65 1.3 pooka netcfg_rump_if(const char *ifname, const char *addr, const char *mask)
66 1.1 pooka {
67 1.1 pooka struct ifaliasreq ia;
68 1.1 pooka struct sockaddr_in *sin;
69 1.3 pooka in_addr_t inaddr, inmask;
70 1.1 pooka int s, rv;
71 1.1 pooka
72 1.1 pooka s = -1;
73 1.1 pooka if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
74 1.1 pooka atf_tc_fail_errno("if config socket");
75 1.1 pooka }
76 1.1 pooka
77 1.3 pooka inaddr = inet_addr(addr);
78 1.3 pooka inmask = inet_addr(mask);
79 1.3 pooka
80 1.1 pooka /* Address */
81 1.1 pooka memset(&ia, 0, sizeof(ia));
82 1.1 pooka strcpy(ia.ifra_name, ifname);
83 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_addr;
84 1.1 pooka sin->sin_family = AF_INET;
85 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
86 1.3 pooka sin->sin_addr.s_addr = inaddr;
87 1.1 pooka
88 1.1 pooka /* Netmask */
89 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_mask;
90 1.1 pooka sin->sin_family = AF_INET;
91 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
92 1.3 pooka sin->sin_addr.s_addr = inmask;
93 1.1 pooka
94 1.1 pooka /* Broadcast address */
95 1.1 pooka sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
96 1.1 pooka sin->sin_family = AF_INET;
97 1.1 pooka sin->sin_len = sizeof(struct sockaddr_in);
98 1.3 pooka sin->sin_addr.s_addr = inaddr | ~inmask;
99 1.1 pooka
100 1.1 pooka rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
101 1.1 pooka if (rv) {
102 1.1 pooka atf_tc_fail_errno("SIOCAIFADDR");
103 1.1 pooka }
104 1.1 pooka rump_sys_close(s);
105 1.1 pooka }
106 1.1 pooka
107 1.1 pooka static void __unused
108 1.1 pooka netcfg_rump_route(const char *dst, const char *mask, const char *gw)
109 1.1 pooka {
110 1.1 pooka size_t len;
111 1.1 pooka struct {
112 1.1 pooka struct rt_msghdr m_rtm;
113 1.1 pooka uint8_t m_space[512];
114 1.1 pooka } m_rtmsg;
115 1.1 pooka #define rtm m_rtmsg.m_rtm
116 1.1 pooka uint8_t *bp = m_rtmsg.m_space;
117 1.1 pooka struct sockaddr_in sinstore;
118 1.1 pooka int s, rv;
119 1.1 pooka
120 1.1 pooka s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
121 1.1 pooka if (s == -1) {
122 1.1 pooka atf_tc_fail_errno("routing socket");
123 1.1 pooka }
124 1.1 pooka
125 1.1 pooka memset(&m_rtmsg, 0, sizeof(m_rtmsg));
126 1.1 pooka rtm.rtm_type = RTM_ADD;
127 1.1 pooka rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
128 1.1 pooka rtm.rtm_version = RTM_VERSION;
129 1.1 pooka rtm.rtm_seq = 2;
130 1.1 pooka rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
131 1.1 pooka
132 1.1 pooka /* dst */
133 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
134 1.1 pooka sinstore.sin_family = AF_INET;
135 1.1 pooka sinstore.sin_len = sizeof(sinstore);
136 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(dst);
137 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
138 1.1 pooka bp += sizeof(sinstore);
139 1.1 pooka
140 1.1 pooka /* gw */
141 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
142 1.1 pooka sinstore.sin_family = AF_INET;
143 1.1 pooka sinstore.sin_len = sizeof(sinstore);
144 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(gw);
145 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
146 1.1 pooka bp += sizeof(sinstore);
147 1.1 pooka
148 1.1 pooka /* netmask */
149 1.1 pooka memset(&sinstore, 0, sizeof(sinstore));
150 1.1 pooka sinstore.sin_family = AF_INET;
151 1.1 pooka sinstore.sin_len = sizeof(sinstore);
152 1.1 pooka sinstore.sin_addr.s_addr = inet_addr(mask);
153 1.1 pooka memcpy(bp, &sinstore, sizeof(sinstore));
154 1.1 pooka bp += sizeof(sinstore);
155 1.1 pooka
156 1.1 pooka len = bp - (uint8_t *)&m_rtmsg;
157 1.1 pooka rtm.rtm_msglen = len;
158 1.1 pooka
159 1.1 pooka rv = rump_sys_write(s, &m_rtmsg, len);
160 1.1 pooka if (rv != (int)len) {
161 1.1 pooka atf_tc_fail_errno("write routing message");
162 1.1 pooka }
163 1.1 pooka rump_sys_close(s);
164 1.1 pooka }
165