netconfig.c revision 1.1 1 /* $NetBSD: netconfig.c,v 1.1 2010/07/25 21:39:20 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.1 2010/07/25 21:39:20 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 <net/route.h>
40
41 #include <atf-c.h>
42 #include <errno.h>
43 #include <string.h>
44
45 #include <rump/rump.h>
46 #include <rump/rump_syscalls.h>
47
48 #include "../../h_macros.h"
49
50 static void
51 netcfg_rump_makeshmif(const char *busname, char *ifname)
52 {
53 int rv, ifnum;
54
55 if ((rv = rump_pub_shmif_create(busname, &ifnum)) != 0) {
56 atf_tc_fail("makeshmif: rump_pub_shmif_create %d", rv);
57 }
58 sprintf(ifname, "shmif%d", ifnum);
59 }
60
61 static void
62 netcfg_rump_if(const char *ifname,
63 const char *addr, const char *mask, const char *bcast)
64 {
65 struct ifaliasreq ia;
66 struct sockaddr_in *sin;
67 int s, rv;
68
69 s = -1;
70 if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
71 atf_tc_fail_errno("if config socket");
72 }
73
74 /* Address */
75 memset(&ia, 0, sizeof(ia));
76 strcpy(ia.ifra_name, ifname);
77 sin = (struct sockaddr_in *)&ia.ifra_addr;
78 sin->sin_family = AF_INET;
79 sin->sin_len = sizeof(struct sockaddr_in);
80 sin->sin_addr.s_addr = inet_addr(addr);
81
82 /* Netmask */
83 sin = (struct sockaddr_in *)&ia.ifra_mask;
84 sin->sin_family = AF_INET;
85 sin->sin_len = sizeof(struct sockaddr_in);
86 sin->sin_addr.s_addr = inet_addr(mask);
87
88 /* Broadcast address */
89 sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
90 sin->sin_family = AF_INET;
91 sin->sin_len = sizeof(struct sockaddr_in);
92 sin->sin_addr.s_addr = inet_addr(bcast);
93
94 rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
95 if (rv) {
96 atf_tc_fail_errno("SIOCAIFADDR");
97 }
98 rump_sys_close(s);
99 }
100
101 static void __unused
102 netcfg_rump_route(const char *dst, const char *mask, const char *gw)
103 {
104 size_t len;
105 struct {
106 struct rt_msghdr m_rtm;
107 uint8_t m_space[512];
108 } m_rtmsg;
109 #define rtm m_rtmsg.m_rtm
110 uint8_t *bp = m_rtmsg.m_space;
111 struct sockaddr_in sinstore;
112 int s, rv;
113
114 s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0);
115 if (s == -1) {
116 atf_tc_fail_errno("routing socket");
117 }
118
119 memset(&m_rtmsg, 0, sizeof(m_rtmsg));
120 rtm.rtm_type = RTM_ADD;
121 rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
122 rtm.rtm_version = RTM_VERSION;
123 rtm.rtm_seq = 2;
124 rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
125
126 /* dst */
127 memset(&sinstore, 0, sizeof(sinstore));
128 sinstore.sin_family = AF_INET;
129 sinstore.sin_len = sizeof(sinstore);
130 sinstore.sin_addr.s_addr = inet_addr(dst);
131 memcpy(bp, &sinstore, sizeof(sinstore));
132 bp += sizeof(sinstore);
133
134 /* gw */
135 memset(&sinstore, 0, sizeof(sinstore));
136 sinstore.sin_family = AF_INET;
137 sinstore.sin_len = sizeof(sinstore);
138 sinstore.sin_addr.s_addr = inet_addr(gw);
139 memcpy(bp, &sinstore, sizeof(sinstore));
140 bp += sizeof(sinstore);
141
142 /* netmask */
143 memset(&sinstore, 0, sizeof(sinstore));
144 sinstore.sin_family = AF_INET;
145 sinstore.sin_len = sizeof(sinstore);
146 sinstore.sin_addr.s_addr = inet_addr(mask);
147 memcpy(bp, &sinstore, sizeof(sinstore));
148 bp += sizeof(sinstore);
149
150 len = bp - (uint8_t *)&m_rtmsg;
151 rtm.rtm_msglen = len;
152
153 rv = rump_sys_write(s, &m_rtmsg, len);
154 if (rv != (int)len) {
155 atf_tc_fail_errno("write routing message");
156 }
157 rump_sys_close(s);
158 }
159