root / net / slirp.c @ f5c5e381
History | View | Annotate | Download (21 kB)
1 | 68ac40d2 | Mark McLoughlin | /*
|
---|---|---|---|
2 | 68ac40d2 | Mark McLoughlin | * QEMU System Emulator
|
3 | 68ac40d2 | Mark McLoughlin | *
|
4 | 68ac40d2 | Mark McLoughlin | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 68ac40d2 | Mark McLoughlin | *
|
6 | 68ac40d2 | Mark McLoughlin | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 68ac40d2 | Mark McLoughlin | * of this software and associated documentation files (the "Software"), to deal
|
8 | 68ac40d2 | Mark McLoughlin | * in the Software without restriction, including without limitation the rights
|
9 | 68ac40d2 | Mark McLoughlin | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 68ac40d2 | Mark McLoughlin | * copies of the Software, and to permit persons to whom the Software is
|
11 | 68ac40d2 | Mark McLoughlin | * furnished to do so, subject to the following conditions:
|
12 | 68ac40d2 | Mark McLoughlin | *
|
13 | 68ac40d2 | Mark McLoughlin | * The above copyright notice and this permission notice shall be included in
|
14 | 68ac40d2 | Mark McLoughlin | * all copies or substantial portions of the Software.
|
15 | 68ac40d2 | Mark McLoughlin | *
|
16 | 68ac40d2 | Mark McLoughlin | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 68ac40d2 | Mark McLoughlin | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 68ac40d2 | Mark McLoughlin | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 68ac40d2 | Mark McLoughlin | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 68ac40d2 | Mark McLoughlin | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 68ac40d2 | Mark McLoughlin | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 68ac40d2 | Mark McLoughlin | * THE SOFTWARE.
|
23 | 68ac40d2 | Mark McLoughlin | */
|
24 | 68ac40d2 | Mark McLoughlin | #include "net/slirp.h" |
25 | 68ac40d2 | Mark McLoughlin | |
26 | 68ac40d2 | Mark McLoughlin | #include "config-host.h" |
27 | 68ac40d2 | Mark McLoughlin | |
28 | 68ac40d2 | Mark McLoughlin | #include "net.h" |
29 | 68ac40d2 | Mark McLoughlin | #include "monitor.h" |
30 | 68ac40d2 | Mark McLoughlin | #include "sysemu.h" |
31 | 68ac40d2 | Mark McLoughlin | #include "qemu_socket.h" |
32 | 68ac40d2 | Mark McLoughlin | #include "slirp/libslirp.h" |
33 | 68ac40d2 | Mark McLoughlin | |
34 | 68ac40d2 | Mark McLoughlin | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
35 | 68ac40d2 | Mark McLoughlin | { |
36 | 68ac40d2 | Mark McLoughlin | const char *p, *p1; |
37 | 68ac40d2 | Mark McLoughlin | int len;
|
38 | 68ac40d2 | Mark McLoughlin | p = *pp; |
39 | 68ac40d2 | Mark McLoughlin | p1 = strchr(p, sep); |
40 | 68ac40d2 | Mark McLoughlin | if (!p1)
|
41 | 68ac40d2 | Mark McLoughlin | return -1; |
42 | 68ac40d2 | Mark McLoughlin | len = p1 - p; |
43 | 68ac40d2 | Mark McLoughlin | p1++; |
44 | 68ac40d2 | Mark McLoughlin | if (buf_size > 0) { |
45 | 68ac40d2 | Mark McLoughlin | if (len > buf_size - 1) |
46 | 68ac40d2 | Mark McLoughlin | len = buf_size - 1;
|
47 | 68ac40d2 | Mark McLoughlin | memcpy(buf, p, len); |
48 | 68ac40d2 | Mark McLoughlin | buf[len] = '\0';
|
49 | 68ac40d2 | Mark McLoughlin | } |
50 | 68ac40d2 | Mark McLoughlin | *pp = p1; |
51 | 68ac40d2 | Mark McLoughlin | return 0; |
52 | 68ac40d2 | Mark McLoughlin | } |
53 | 68ac40d2 | Mark McLoughlin | |
54 | 68ac40d2 | Mark McLoughlin | /* slirp network adapter */
|
55 | 68ac40d2 | Mark McLoughlin | |
56 | 68ac40d2 | Mark McLoughlin | #define SLIRP_CFG_HOSTFWD 1 |
57 | 68ac40d2 | Mark McLoughlin | #define SLIRP_CFG_LEGACY 2 |
58 | 68ac40d2 | Mark McLoughlin | |
59 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str {
|
60 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *next;
|
61 | 68ac40d2 | Mark McLoughlin | int flags;
|
62 | 68ac40d2 | Mark McLoughlin | char str[1024]; |
63 | 68ac40d2 | Mark McLoughlin | int legacy_format;
|
64 | 68ac40d2 | Mark McLoughlin | }; |
65 | 68ac40d2 | Mark McLoughlin | |
66 | 68ac40d2 | Mark McLoughlin | typedef struct SlirpState { |
67 | ce20b5be | Mark McLoughlin | VLANClientState nc; |
68 | 68ac40d2 | Mark McLoughlin | QTAILQ_ENTRY(SlirpState) entry; |
69 | 68ac40d2 | Mark McLoughlin | Slirp *slirp; |
70 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
71 | 68ac40d2 | Mark McLoughlin | char smb_dir[128]; |
72 | 68ac40d2 | Mark McLoughlin | #endif
|
73 | 68ac40d2 | Mark McLoughlin | } SlirpState; |
74 | 68ac40d2 | Mark McLoughlin | |
75 | 68ac40d2 | Mark McLoughlin | static struct slirp_config_str *slirp_configs; |
76 | 68ac40d2 | Mark McLoughlin | const char *legacy_tftp_prefix; |
77 | 68ac40d2 | Mark McLoughlin | const char *legacy_bootp_filename; |
78 | 68ac40d2 | Mark McLoughlin | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
|
79 | 68ac40d2 | Mark McLoughlin | QTAILQ_HEAD_INITIALIZER(slirp_stacks); |
80 | 68ac40d2 | Mark McLoughlin | |
81 | 68ac40d2 | Mark McLoughlin | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
82 | 68ac40d2 | Mark McLoughlin | int legacy_format);
|
83 | 68ac40d2 | Mark McLoughlin | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
84 | 68ac40d2 | Mark McLoughlin | int legacy_format);
|
85 | 68ac40d2 | Mark McLoughlin | |
86 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
87 | 68ac40d2 | Mark McLoughlin | static const char *legacy_smb_export; |
88 | 68ac40d2 | Mark McLoughlin | |
89 | 68ac40d2 | Mark McLoughlin | static int slirp_smb(SlirpState *s, const char *exported_dir, |
90 | 68ac40d2 | Mark McLoughlin | struct in_addr vserver_addr);
|
91 | 68ac40d2 | Mark McLoughlin | static void slirp_smb_cleanup(SlirpState *s); |
92 | 68ac40d2 | Mark McLoughlin | #else
|
93 | 68ac40d2 | Mark McLoughlin | static inline void slirp_smb_cleanup(SlirpState *s) { } |
94 | 68ac40d2 | Mark McLoughlin | #endif
|
95 | 68ac40d2 | Mark McLoughlin | |
96 | 68ac40d2 | Mark McLoughlin | int slirp_can_output(void *opaque) |
97 | 68ac40d2 | Mark McLoughlin | { |
98 | 68ac40d2 | Mark McLoughlin | SlirpState *s = opaque; |
99 | 68ac40d2 | Mark McLoughlin | |
100 | ce20b5be | Mark McLoughlin | return qemu_can_send_packet(&s->nc);
|
101 | 68ac40d2 | Mark McLoughlin | } |
102 | 68ac40d2 | Mark McLoughlin | |
103 | 68ac40d2 | Mark McLoughlin | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) |
104 | 68ac40d2 | Mark McLoughlin | { |
105 | 68ac40d2 | Mark McLoughlin | SlirpState *s = opaque; |
106 | 68ac40d2 | Mark McLoughlin | |
107 | ce20b5be | Mark McLoughlin | qemu_send_packet(&s->nc, pkt, pkt_len); |
108 | 68ac40d2 | Mark McLoughlin | } |
109 | 68ac40d2 | Mark McLoughlin | |
110 | ce20b5be | Mark McLoughlin | static ssize_t net_slirp_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
111 | 68ac40d2 | Mark McLoughlin | { |
112 | ce20b5be | Mark McLoughlin | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); |
113 | 68ac40d2 | Mark McLoughlin | |
114 | 68ac40d2 | Mark McLoughlin | slirp_input(s->slirp, buf, size); |
115 | 68ac40d2 | Mark McLoughlin | |
116 | 68ac40d2 | Mark McLoughlin | return size;
|
117 | 68ac40d2 | Mark McLoughlin | } |
118 | 68ac40d2 | Mark McLoughlin | |
119 | ce20b5be | Mark McLoughlin | static void net_slirp_cleanup(VLANClientState *nc) |
120 | 68ac40d2 | Mark McLoughlin | { |
121 | ce20b5be | Mark McLoughlin | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); |
122 | 68ac40d2 | Mark McLoughlin | |
123 | 68ac40d2 | Mark McLoughlin | slirp_cleanup(s->slirp); |
124 | 68ac40d2 | Mark McLoughlin | slirp_smb_cleanup(s); |
125 | 68ac40d2 | Mark McLoughlin | QTAILQ_REMOVE(&slirp_stacks, s, entry); |
126 | 68ac40d2 | Mark McLoughlin | } |
127 | 68ac40d2 | Mark McLoughlin | |
128 | ce20b5be | Mark McLoughlin | static NetClientInfo net_slirp_info = {
|
129 | ce20b5be | Mark McLoughlin | .type = NET_CLIENT_TYPE_SLIRP, |
130 | ce20b5be | Mark McLoughlin | .size = sizeof(SlirpState),
|
131 | ce20b5be | Mark McLoughlin | .receive = net_slirp_receive, |
132 | ce20b5be | Mark McLoughlin | .cleanup = net_slirp_cleanup, |
133 | ce20b5be | Mark McLoughlin | }; |
134 | ce20b5be | Mark McLoughlin | |
135 | 68ac40d2 | Mark McLoughlin | static int net_slirp_init(VLANState *vlan, const char *model, |
136 | 68ac40d2 | Mark McLoughlin | const char *name, int restricted, |
137 | 68ac40d2 | Mark McLoughlin | const char *vnetwork, const char *vhost, |
138 | 68ac40d2 | Mark McLoughlin | const char *vhostname, const char *tftp_export, |
139 | 68ac40d2 | Mark McLoughlin | const char *bootfile, const char *vdhcp_start, |
140 | 68ac40d2 | Mark McLoughlin | const char *vnameserver, const char *smb_export, |
141 | 68ac40d2 | Mark McLoughlin | const char *vsmbserver) |
142 | 68ac40d2 | Mark McLoughlin | { |
143 | 68ac40d2 | Mark McLoughlin | /* default settings according to historic slirp */
|
144 | 68ac40d2 | Mark McLoughlin | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ |
145 | 68ac40d2 | Mark McLoughlin | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ |
146 | 68ac40d2 | Mark McLoughlin | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ |
147 | 68ac40d2 | Mark McLoughlin | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ |
148 | 68ac40d2 | Mark McLoughlin | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ |
149 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
150 | 68ac40d2 | Mark McLoughlin | struct in_addr smbsrv = { .s_addr = 0 }; |
151 | 68ac40d2 | Mark McLoughlin | #endif
|
152 | ce20b5be | Mark McLoughlin | VLANClientState *nc; |
153 | 68ac40d2 | Mark McLoughlin | SlirpState *s; |
154 | 68ac40d2 | Mark McLoughlin | char buf[20]; |
155 | 68ac40d2 | Mark McLoughlin | uint32_t addr; |
156 | 68ac40d2 | Mark McLoughlin | int shift;
|
157 | 68ac40d2 | Mark McLoughlin | char *end;
|
158 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *config;
|
159 | 68ac40d2 | Mark McLoughlin | |
160 | 68ac40d2 | Mark McLoughlin | if (!tftp_export) {
|
161 | 68ac40d2 | Mark McLoughlin | tftp_export = legacy_tftp_prefix; |
162 | 68ac40d2 | Mark McLoughlin | } |
163 | 68ac40d2 | Mark McLoughlin | if (!bootfile) {
|
164 | 68ac40d2 | Mark McLoughlin | bootfile = legacy_bootp_filename; |
165 | 68ac40d2 | Mark McLoughlin | } |
166 | 68ac40d2 | Mark McLoughlin | |
167 | 68ac40d2 | Mark McLoughlin | if (vnetwork) {
|
168 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { |
169 | 68ac40d2 | Mark McLoughlin | if (!inet_aton(vnetwork, &net)) {
|
170 | 68ac40d2 | Mark McLoughlin | return -1; |
171 | 68ac40d2 | Mark McLoughlin | } |
172 | 68ac40d2 | Mark McLoughlin | addr = ntohl(net.s_addr); |
173 | 68ac40d2 | Mark McLoughlin | if (!(addr & 0x80000000)) { |
174 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xff000000); /* class A */ |
175 | 68ac40d2 | Mark McLoughlin | } else if ((addr & 0xfff00000) == 0xac100000) { |
176 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ |
177 | 68ac40d2 | Mark McLoughlin | } else if ((addr & 0xc0000000) == 0x80000000) { |
178 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xffff0000); /* class B */ |
179 | 68ac40d2 | Mark McLoughlin | } else if ((addr & 0xffff0000) == 0xc0a80000) { |
180 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ |
181 | 68ac40d2 | Mark McLoughlin | } else if ((addr & 0xffff0000) == 0xc6120000) { |
182 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ |
183 | 68ac40d2 | Mark McLoughlin | } else if ((addr & 0xe0000000) == 0xe0000000) { |
184 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xffffff00); /* class C */ |
185 | 68ac40d2 | Mark McLoughlin | } else {
|
186 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ |
187 | 68ac40d2 | Mark McLoughlin | } |
188 | 68ac40d2 | Mark McLoughlin | } else {
|
189 | 68ac40d2 | Mark McLoughlin | if (!inet_aton(buf, &net)) {
|
190 | 68ac40d2 | Mark McLoughlin | return -1; |
191 | 68ac40d2 | Mark McLoughlin | } |
192 | 68ac40d2 | Mark McLoughlin | shift = strtol(vnetwork, &end, 10);
|
193 | 68ac40d2 | Mark McLoughlin | if (*end != '\0') { |
194 | 68ac40d2 | Mark McLoughlin | if (!inet_aton(vnetwork, &mask)) {
|
195 | 68ac40d2 | Mark McLoughlin | return -1; |
196 | 68ac40d2 | Mark McLoughlin | } |
197 | 68ac40d2 | Mark McLoughlin | } else if (shift < 4 || shift > 32) { |
198 | 68ac40d2 | Mark McLoughlin | return -1; |
199 | 68ac40d2 | Mark McLoughlin | } else {
|
200 | 68ac40d2 | Mark McLoughlin | mask.s_addr = htonl(0xffffffff << (32 - shift)); |
201 | 68ac40d2 | Mark McLoughlin | } |
202 | 68ac40d2 | Mark McLoughlin | } |
203 | 68ac40d2 | Mark McLoughlin | net.s_addr &= mask.s_addr; |
204 | 68ac40d2 | Mark McLoughlin | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
|
205 | 68ac40d2 | Mark McLoughlin | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
|
206 | 68ac40d2 | Mark McLoughlin | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
|
207 | 68ac40d2 | Mark McLoughlin | } |
208 | 68ac40d2 | Mark McLoughlin | |
209 | 68ac40d2 | Mark McLoughlin | if (vhost && !inet_aton(vhost, &host)) {
|
210 | 68ac40d2 | Mark McLoughlin | return -1; |
211 | 68ac40d2 | Mark McLoughlin | } |
212 | 68ac40d2 | Mark McLoughlin | if ((host.s_addr & mask.s_addr) != net.s_addr) {
|
213 | 68ac40d2 | Mark McLoughlin | return -1; |
214 | 68ac40d2 | Mark McLoughlin | } |
215 | 68ac40d2 | Mark McLoughlin | |
216 | 68ac40d2 | Mark McLoughlin | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
|
217 | 68ac40d2 | Mark McLoughlin | return -1; |
218 | 68ac40d2 | Mark McLoughlin | } |
219 | 68ac40d2 | Mark McLoughlin | if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
|
220 | 68ac40d2 | Mark McLoughlin | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { |
221 | 68ac40d2 | Mark McLoughlin | return -1; |
222 | 68ac40d2 | Mark McLoughlin | } |
223 | 68ac40d2 | Mark McLoughlin | |
224 | 68ac40d2 | Mark McLoughlin | if (vnameserver && !inet_aton(vnameserver, &dns)) {
|
225 | 68ac40d2 | Mark McLoughlin | return -1; |
226 | 68ac40d2 | Mark McLoughlin | } |
227 | 68ac40d2 | Mark McLoughlin | if ((dns.s_addr & mask.s_addr) != net.s_addr ||
|
228 | 68ac40d2 | Mark McLoughlin | dns.s_addr == host.s_addr) { |
229 | 68ac40d2 | Mark McLoughlin | return -1; |
230 | 68ac40d2 | Mark McLoughlin | } |
231 | 68ac40d2 | Mark McLoughlin | |
232 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
233 | 68ac40d2 | Mark McLoughlin | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
|
234 | 68ac40d2 | Mark McLoughlin | return -1; |
235 | 68ac40d2 | Mark McLoughlin | } |
236 | 68ac40d2 | Mark McLoughlin | #endif
|
237 | 68ac40d2 | Mark McLoughlin | |
238 | ce20b5be | Mark McLoughlin | nc = qemu_new_net_client(&net_slirp_info, vlan, NULL, model, name);
|
239 | ce20b5be | Mark McLoughlin | |
240 | ce20b5be | Mark McLoughlin | snprintf(nc->info_str, sizeof(nc->info_str),
|
241 | ce20b5be | Mark McLoughlin | "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n'); |
242 | ce20b5be | Mark McLoughlin | |
243 | ce20b5be | Mark McLoughlin | s = DO_UPCAST(SlirpState, nc, nc); |
244 | ce20b5be | Mark McLoughlin | |
245 | 68ac40d2 | Mark McLoughlin | s->slirp = slirp_init(restricted, net, mask, host, vhostname, |
246 | 68ac40d2 | Mark McLoughlin | tftp_export, bootfile, dhcp, dns, s); |
247 | 68ac40d2 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); |
248 | 68ac40d2 | Mark McLoughlin | |
249 | 68ac40d2 | Mark McLoughlin | for (config = slirp_configs; config; config = config->next) {
|
250 | 68ac40d2 | Mark McLoughlin | if (config->flags & SLIRP_CFG_HOSTFWD) {
|
251 | 68ac40d2 | Mark McLoughlin | if (slirp_hostfwd(s, config->str,
|
252 | 68ac40d2 | Mark McLoughlin | config->flags & SLIRP_CFG_LEGACY) < 0)
|
253 | ce20b5be | Mark McLoughlin | goto error;
|
254 | 68ac40d2 | Mark McLoughlin | } else {
|
255 | 68ac40d2 | Mark McLoughlin | if (slirp_guestfwd(s, config->str,
|
256 | 68ac40d2 | Mark McLoughlin | config->flags & SLIRP_CFG_LEGACY) < 0)
|
257 | ce20b5be | Mark McLoughlin | goto error;
|
258 | 68ac40d2 | Mark McLoughlin | } |
259 | 68ac40d2 | Mark McLoughlin | } |
260 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
261 | 68ac40d2 | Mark McLoughlin | if (!smb_export) {
|
262 | 68ac40d2 | Mark McLoughlin | smb_export = legacy_smb_export; |
263 | 68ac40d2 | Mark McLoughlin | } |
264 | 68ac40d2 | Mark McLoughlin | if (smb_export) {
|
265 | 68ac40d2 | Mark McLoughlin | if (slirp_smb(s, smb_export, smbsrv) < 0) |
266 | ce20b5be | Mark McLoughlin | goto error;
|
267 | 68ac40d2 | Mark McLoughlin | } |
268 | 68ac40d2 | Mark McLoughlin | #endif
|
269 | 68ac40d2 | Mark McLoughlin | |
270 | 68ac40d2 | Mark McLoughlin | return 0; |
271 | ce20b5be | Mark McLoughlin | |
272 | ce20b5be | Mark McLoughlin | error:
|
273 | ce20b5be | Mark McLoughlin | qemu_del_vlan_client(nc); |
274 | ce20b5be | Mark McLoughlin | return -1; |
275 | 68ac40d2 | Mark McLoughlin | } |
276 | 68ac40d2 | Mark McLoughlin | |
277 | 68ac40d2 | Mark McLoughlin | static SlirpState *slirp_lookup(Monitor *mon, const char *vlan, |
278 | 68ac40d2 | Mark McLoughlin | const char *stack) |
279 | 68ac40d2 | Mark McLoughlin | { |
280 | 68ac40d2 | Mark McLoughlin | |
281 | 68ac40d2 | Mark McLoughlin | if (vlan) {
|
282 | ce20b5be | Mark McLoughlin | VLANClientState *nc; |
283 | ce20b5be | Mark McLoughlin | nc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack); |
284 | ce20b5be | Mark McLoughlin | if (!nc) {
|
285 | 68ac40d2 | Mark McLoughlin | return NULL; |
286 | 68ac40d2 | Mark McLoughlin | } |
287 | ce20b5be | Mark McLoughlin | if (strcmp(nc->model, "user")) { |
288 | 68ac40d2 | Mark McLoughlin | monitor_printf(mon, "invalid device specified\n");
|
289 | 68ac40d2 | Mark McLoughlin | return NULL; |
290 | 68ac40d2 | Mark McLoughlin | } |
291 | ce20b5be | Mark McLoughlin | return DO_UPCAST(SlirpState, nc, nc);
|
292 | 68ac40d2 | Mark McLoughlin | } else {
|
293 | 68ac40d2 | Mark McLoughlin | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
294 | 68ac40d2 | Mark McLoughlin | monitor_printf(mon, "user mode network stack not in use\n");
|
295 | 68ac40d2 | Mark McLoughlin | return NULL; |
296 | 68ac40d2 | Mark McLoughlin | } |
297 | 68ac40d2 | Mark McLoughlin | return QTAILQ_FIRST(&slirp_stacks);
|
298 | 68ac40d2 | Mark McLoughlin | } |
299 | 68ac40d2 | Mark McLoughlin | } |
300 | 68ac40d2 | Mark McLoughlin | |
301 | 68ac40d2 | Mark McLoughlin | void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) |
302 | 68ac40d2 | Mark McLoughlin | { |
303 | 68ac40d2 | Mark McLoughlin | struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
304 | 68ac40d2 | Mark McLoughlin | int host_port;
|
305 | 68ac40d2 | Mark McLoughlin | char buf[256] = ""; |
306 | 68ac40d2 | Mark McLoughlin | const char *src_str, *p; |
307 | 68ac40d2 | Mark McLoughlin | SlirpState *s; |
308 | 68ac40d2 | Mark McLoughlin | int is_udp = 0; |
309 | 68ac40d2 | Mark McLoughlin | int err;
|
310 | 68ac40d2 | Mark McLoughlin | const char *arg1 = qdict_get_str(qdict, "arg1"); |
311 | 68ac40d2 | Mark McLoughlin | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
312 | 68ac40d2 | Mark McLoughlin | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
313 | 68ac40d2 | Mark McLoughlin | |
314 | 68ac40d2 | Mark McLoughlin | if (arg2) {
|
315 | 68ac40d2 | Mark McLoughlin | s = slirp_lookup(mon, arg1, arg2); |
316 | 68ac40d2 | Mark McLoughlin | src_str = arg3; |
317 | 68ac40d2 | Mark McLoughlin | } else {
|
318 | 68ac40d2 | Mark McLoughlin | s = slirp_lookup(mon, NULL, NULL); |
319 | 68ac40d2 | Mark McLoughlin | src_str = arg1; |
320 | 68ac40d2 | Mark McLoughlin | } |
321 | 68ac40d2 | Mark McLoughlin | if (!s) {
|
322 | 68ac40d2 | Mark McLoughlin | return;
|
323 | 68ac40d2 | Mark McLoughlin | } |
324 | 68ac40d2 | Mark McLoughlin | |
325 | 68ac40d2 | Mark McLoughlin | if (!src_str || !src_str[0]) |
326 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
327 | 68ac40d2 | Mark McLoughlin | |
328 | 68ac40d2 | Mark McLoughlin | p = src_str; |
329 | 68ac40d2 | Mark McLoughlin | get_str_sep(buf, sizeof(buf), &p, ':'); |
330 | 68ac40d2 | Mark McLoughlin | |
331 | 68ac40d2 | Mark McLoughlin | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
332 | 68ac40d2 | Mark McLoughlin | is_udp = 0;
|
333 | 68ac40d2 | Mark McLoughlin | } else if (!strcmp(buf, "udp")) { |
334 | 68ac40d2 | Mark McLoughlin | is_udp = 1;
|
335 | 68ac40d2 | Mark McLoughlin | } else {
|
336 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
337 | 68ac40d2 | Mark McLoughlin | } |
338 | 68ac40d2 | Mark McLoughlin | |
339 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
340 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
341 | 68ac40d2 | Mark McLoughlin | } |
342 | 68ac40d2 | Mark McLoughlin | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
343 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
344 | 68ac40d2 | Mark McLoughlin | } |
345 | 68ac40d2 | Mark McLoughlin | |
346 | 68ac40d2 | Mark McLoughlin | host_port = atoi(p); |
347 | 68ac40d2 | Mark McLoughlin | |
348 | 68ac40d2 | Mark McLoughlin | err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, |
349 | 68ac40d2 | Mark McLoughlin | host_addr, host_port); |
350 | 68ac40d2 | Mark McLoughlin | |
351 | 68ac40d2 | Mark McLoughlin | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
|
352 | 68ac40d2 | Mark McLoughlin | err ? "removed" : "not found"); |
353 | 68ac40d2 | Mark McLoughlin | return;
|
354 | 68ac40d2 | Mark McLoughlin | |
355 | 68ac40d2 | Mark McLoughlin | fail_syntax:
|
356 | 68ac40d2 | Mark McLoughlin | monitor_printf(mon, "invalid format\n");
|
357 | 68ac40d2 | Mark McLoughlin | } |
358 | 68ac40d2 | Mark McLoughlin | |
359 | 68ac40d2 | Mark McLoughlin | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
360 | 68ac40d2 | Mark McLoughlin | int legacy_format)
|
361 | 68ac40d2 | Mark McLoughlin | { |
362 | 68ac40d2 | Mark McLoughlin | struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
363 | 68ac40d2 | Mark McLoughlin | struct in_addr guest_addr = { .s_addr = 0 }; |
364 | 68ac40d2 | Mark McLoughlin | int host_port, guest_port;
|
365 | 68ac40d2 | Mark McLoughlin | const char *p; |
366 | 68ac40d2 | Mark McLoughlin | char buf[256]; |
367 | 68ac40d2 | Mark McLoughlin | int is_udp;
|
368 | 68ac40d2 | Mark McLoughlin | char *end;
|
369 | 68ac40d2 | Mark McLoughlin | |
370 | 68ac40d2 | Mark McLoughlin | p = redir_str; |
371 | 68ac40d2 | Mark McLoughlin | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
372 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
373 | 68ac40d2 | Mark McLoughlin | } |
374 | 68ac40d2 | Mark McLoughlin | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
375 | 68ac40d2 | Mark McLoughlin | is_udp = 0;
|
376 | 68ac40d2 | Mark McLoughlin | } else if (!strcmp(buf, "udp")) { |
377 | 68ac40d2 | Mark McLoughlin | is_udp = 1;
|
378 | 68ac40d2 | Mark McLoughlin | } else {
|
379 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
380 | 68ac40d2 | Mark McLoughlin | } |
381 | 68ac40d2 | Mark McLoughlin | |
382 | 68ac40d2 | Mark McLoughlin | if (!legacy_format) {
|
383 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
384 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
385 | 68ac40d2 | Mark McLoughlin | } |
386 | 68ac40d2 | Mark McLoughlin | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
387 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
388 | 68ac40d2 | Mark McLoughlin | } |
389 | 68ac40d2 | Mark McLoughlin | } |
390 | 68ac40d2 | Mark McLoughlin | |
391 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { |
392 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
393 | 68ac40d2 | Mark McLoughlin | } |
394 | 68ac40d2 | Mark McLoughlin | host_port = strtol(buf, &end, 0);
|
395 | 68ac40d2 | Mark McLoughlin | if (*end != '\0' || host_port < 1 || host_port > 65535) { |
396 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
397 | 68ac40d2 | Mark McLoughlin | } |
398 | 68ac40d2 | Mark McLoughlin | |
399 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
400 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
401 | 68ac40d2 | Mark McLoughlin | } |
402 | 68ac40d2 | Mark McLoughlin | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { |
403 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
404 | 68ac40d2 | Mark McLoughlin | } |
405 | 68ac40d2 | Mark McLoughlin | |
406 | 68ac40d2 | Mark McLoughlin | guest_port = strtol(p, &end, 0);
|
407 | 68ac40d2 | Mark McLoughlin | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { |
408 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
409 | 68ac40d2 | Mark McLoughlin | } |
410 | 68ac40d2 | Mark McLoughlin | |
411 | 68ac40d2 | Mark McLoughlin | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
|
412 | 68ac40d2 | Mark McLoughlin | guest_port) < 0) {
|
413 | 68ac40d2 | Mark McLoughlin | qemu_error("could not set up host forwarding rule '%s'\n",
|
414 | 68ac40d2 | Mark McLoughlin | redir_str); |
415 | 68ac40d2 | Mark McLoughlin | return -1; |
416 | 68ac40d2 | Mark McLoughlin | } |
417 | 68ac40d2 | Mark McLoughlin | return 0; |
418 | 68ac40d2 | Mark McLoughlin | |
419 | 68ac40d2 | Mark McLoughlin | fail_syntax:
|
420 | 68ac40d2 | Mark McLoughlin | qemu_error("invalid host forwarding rule '%s'\n", redir_str);
|
421 | 68ac40d2 | Mark McLoughlin | return -1; |
422 | 68ac40d2 | Mark McLoughlin | } |
423 | 68ac40d2 | Mark McLoughlin | |
424 | 68ac40d2 | Mark McLoughlin | void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) |
425 | 68ac40d2 | Mark McLoughlin | { |
426 | 68ac40d2 | Mark McLoughlin | const char *redir_str; |
427 | 68ac40d2 | Mark McLoughlin | SlirpState *s; |
428 | 68ac40d2 | Mark McLoughlin | const char *arg1 = qdict_get_str(qdict, "arg1"); |
429 | 68ac40d2 | Mark McLoughlin | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
430 | 68ac40d2 | Mark McLoughlin | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
431 | 68ac40d2 | Mark McLoughlin | |
432 | 68ac40d2 | Mark McLoughlin | if (arg2) {
|
433 | 68ac40d2 | Mark McLoughlin | s = slirp_lookup(mon, arg1, arg2); |
434 | 68ac40d2 | Mark McLoughlin | redir_str = arg3; |
435 | 68ac40d2 | Mark McLoughlin | } else {
|
436 | 68ac40d2 | Mark McLoughlin | s = slirp_lookup(mon, NULL, NULL); |
437 | 68ac40d2 | Mark McLoughlin | redir_str = arg1; |
438 | 68ac40d2 | Mark McLoughlin | } |
439 | 68ac40d2 | Mark McLoughlin | if (s) {
|
440 | 68ac40d2 | Mark McLoughlin | slirp_hostfwd(s, redir_str, 0);
|
441 | 68ac40d2 | Mark McLoughlin | } |
442 | 68ac40d2 | Mark McLoughlin | |
443 | 68ac40d2 | Mark McLoughlin | } |
444 | 68ac40d2 | Mark McLoughlin | |
445 | 68ac40d2 | Mark McLoughlin | int net_slirp_redir(const char *redir_str) |
446 | 68ac40d2 | Mark McLoughlin | { |
447 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *config;
|
448 | 68ac40d2 | Mark McLoughlin | |
449 | 68ac40d2 | Mark McLoughlin | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
450 | 68ac40d2 | Mark McLoughlin | config = qemu_malloc(sizeof(*config));
|
451 | 68ac40d2 | Mark McLoughlin | pstrcpy(config->str, sizeof(config->str), redir_str);
|
452 | 68ac40d2 | Mark McLoughlin | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; |
453 | 68ac40d2 | Mark McLoughlin | config->next = slirp_configs; |
454 | 68ac40d2 | Mark McLoughlin | slirp_configs = config; |
455 | 68ac40d2 | Mark McLoughlin | return 0; |
456 | 68ac40d2 | Mark McLoughlin | } |
457 | 68ac40d2 | Mark McLoughlin | |
458 | 68ac40d2 | Mark McLoughlin | return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1); |
459 | 68ac40d2 | Mark McLoughlin | } |
460 | 68ac40d2 | Mark McLoughlin | |
461 | 68ac40d2 | Mark McLoughlin | #ifndef _WIN32
|
462 | 68ac40d2 | Mark McLoughlin | |
463 | 68ac40d2 | Mark McLoughlin | /* automatic user mode samba server configuration */
|
464 | 68ac40d2 | Mark McLoughlin | static void slirp_smb_cleanup(SlirpState *s) |
465 | 68ac40d2 | Mark McLoughlin | { |
466 | 68ac40d2 | Mark McLoughlin | char cmd[128]; |
467 | 68ac40d2 | Mark McLoughlin | |
468 | 68ac40d2 | Mark McLoughlin | if (s->smb_dir[0] != '\0') { |
469 | 68ac40d2 | Mark McLoughlin | snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir); |
470 | 68ac40d2 | Mark McLoughlin | system(cmd); |
471 | 68ac40d2 | Mark McLoughlin | s->smb_dir[0] = '\0'; |
472 | 68ac40d2 | Mark McLoughlin | } |
473 | 68ac40d2 | Mark McLoughlin | } |
474 | 68ac40d2 | Mark McLoughlin | |
475 | 68ac40d2 | Mark McLoughlin | static int slirp_smb(SlirpState* s, const char *exported_dir, |
476 | 68ac40d2 | Mark McLoughlin | struct in_addr vserver_addr)
|
477 | 68ac40d2 | Mark McLoughlin | { |
478 | 68ac40d2 | Mark McLoughlin | static int instance; |
479 | 68ac40d2 | Mark McLoughlin | char smb_conf[128]; |
480 | 68ac40d2 | Mark McLoughlin | char smb_cmdline[128]; |
481 | 68ac40d2 | Mark McLoughlin | FILE *f; |
482 | 68ac40d2 | Mark McLoughlin | |
483 | 68ac40d2 | Mark McLoughlin | snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d", |
484 | 68ac40d2 | Mark McLoughlin | (long)getpid(), instance++);
|
485 | 68ac40d2 | Mark McLoughlin | if (mkdir(s->smb_dir, 0700) < 0) { |
486 | 68ac40d2 | Mark McLoughlin | qemu_error("could not create samba server dir '%s'\n", s->smb_dir);
|
487 | 68ac40d2 | Mark McLoughlin | return -1; |
488 | 68ac40d2 | Mark McLoughlin | } |
489 | 68ac40d2 | Mark McLoughlin | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf"); |
490 | 68ac40d2 | Mark McLoughlin | |
491 | 68ac40d2 | Mark McLoughlin | f = fopen(smb_conf, "w");
|
492 | 68ac40d2 | Mark McLoughlin | if (!f) {
|
493 | 68ac40d2 | Mark McLoughlin | slirp_smb_cleanup(s); |
494 | 68ac40d2 | Mark McLoughlin | qemu_error("could not create samba server configuration file '%s'\n",
|
495 | 68ac40d2 | Mark McLoughlin | smb_conf); |
496 | 68ac40d2 | Mark McLoughlin | return -1; |
497 | 68ac40d2 | Mark McLoughlin | } |
498 | 68ac40d2 | Mark McLoughlin | fprintf(f, |
499 | 68ac40d2 | Mark McLoughlin | "[global]\n"
|
500 | 68ac40d2 | Mark McLoughlin | "private dir=%s\n"
|
501 | 68ac40d2 | Mark McLoughlin | "smb ports=0\n"
|
502 | 68ac40d2 | Mark McLoughlin | "socket address=127.0.0.1\n"
|
503 | 68ac40d2 | Mark McLoughlin | "pid directory=%s\n"
|
504 | 68ac40d2 | Mark McLoughlin | "lock directory=%s\n"
|
505 | 68ac40d2 | Mark McLoughlin | "log file=%s/log.smbd\n"
|
506 | 68ac40d2 | Mark McLoughlin | "smb passwd file=%s/smbpasswd\n"
|
507 | 68ac40d2 | Mark McLoughlin | "security = share\n"
|
508 | 68ac40d2 | Mark McLoughlin | "[qemu]\n"
|
509 | 68ac40d2 | Mark McLoughlin | "path=%s\n"
|
510 | 68ac40d2 | Mark McLoughlin | "read only=no\n"
|
511 | 68ac40d2 | Mark McLoughlin | "guest ok=yes\n",
|
512 | 68ac40d2 | Mark McLoughlin | s->smb_dir, |
513 | 68ac40d2 | Mark McLoughlin | s->smb_dir, |
514 | 68ac40d2 | Mark McLoughlin | s->smb_dir, |
515 | 68ac40d2 | Mark McLoughlin | s->smb_dir, |
516 | 68ac40d2 | Mark McLoughlin | s->smb_dir, |
517 | 68ac40d2 | Mark McLoughlin | exported_dir |
518 | 68ac40d2 | Mark McLoughlin | ); |
519 | 68ac40d2 | Mark McLoughlin | fclose(f); |
520 | 68ac40d2 | Mark McLoughlin | |
521 | 68ac40d2 | Mark McLoughlin | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
522 | 68ac40d2 | Mark McLoughlin | SMBD_COMMAND, smb_conf); |
523 | 68ac40d2 | Mark McLoughlin | |
524 | 68ac40d2 | Mark McLoughlin | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) { |
525 | 68ac40d2 | Mark McLoughlin | slirp_smb_cleanup(s); |
526 | 68ac40d2 | Mark McLoughlin | qemu_error("conflicting/invalid smbserver address\n");
|
527 | 68ac40d2 | Mark McLoughlin | return -1; |
528 | 68ac40d2 | Mark McLoughlin | } |
529 | 68ac40d2 | Mark McLoughlin | return 0; |
530 | 68ac40d2 | Mark McLoughlin | } |
531 | 68ac40d2 | Mark McLoughlin | |
532 | 68ac40d2 | Mark McLoughlin | /* automatic user mode samba server configuration (legacy interface) */
|
533 | 68ac40d2 | Mark McLoughlin | int net_slirp_smb(const char *exported_dir) |
534 | 68ac40d2 | Mark McLoughlin | { |
535 | 68ac40d2 | Mark McLoughlin | struct in_addr vserver_addr = { .s_addr = 0 }; |
536 | 68ac40d2 | Mark McLoughlin | |
537 | 68ac40d2 | Mark McLoughlin | if (legacy_smb_export) {
|
538 | 68ac40d2 | Mark McLoughlin | fprintf(stderr, "-smb given twice\n");
|
539 | 68ac40d2 | Mark McLoughlin | return -1; |
540 | 68ac40d2 | Mark McLoughlin | } |
541 | 68ac40d2 | Mark McLoughlin | legacy_smb_export = exported_dir; |
542 | 68ac40d2 | Mark McLoughlin | if (!QTAILQ_EMPTY(&slirp_stacks)) {
|
543 | 68ac40d2 | Mark McLoughlin | return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
|
544 | 68ac40d2 | Mark McLoughlin | vserver_addr); |
545 | 68ac40d2 | Mark McLoughlin | } |
546 | 68ac40d2 | Mark McLoughlin | return 0; |
547 | 68ac40d2 | Mark McLoughlin | } |
548 | 68ac40d2 | Mark McLoughlin | |
549 | 68ac40d2 | Mark McLoughlin | #endif /* !defined(_WIN32) */ |
550 | 68ac40d2 | Mark McLoughlin | |
551 | 68ac40d2 | Mark McLoughlin | struct GuestFwd {
|
552 | 68ac40d2 | Mark McLoughlin | CharDriverState *hd; |
553 | 68ac40d2 | Mark McLoughlin | struct in_addr server;
|
554 | 68ac40d2 | Mark McLoughlin | int port;
|
555 | 68ac40d2 | Mark McLoughlin | Slirp *slirp; |
556 | 68ac40d2 | Mark McLoughlin | }; |
557 | 68ac40d2 | Mark McLoughlin | |
558 | 68ac40d2 | Mark McLoughlin | static int guestfwd_can_read(void *opaque) |
559 | 68ac40d2 | Mark McLoughlin | { |
560 | 68ac40d2 | Mark McLoughlin | struct GuestFwd *fwd = opaque;
|
561 | 68ac40d2 | Mark McLoughlin | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
|
562 | 68ac40d2 | Mark McLoughlin | } |
563 | 68ac40d2 | Mark McLoughlin | |
564 | 68ac40d2 | Mark McLoughlin | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) |
565 | 68ac40d2 | Mark McLoughlin | { |
566 | 68ac40d2 | Mark McLoughlin | struct GuestFwd *fwd = opaque;
|
567 | 68ac40d2 | Mark McLoughlin | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); |
568 | 68ac40d2 | Mark McLoughlin | } |
569 | 68ac40d2 | Mark McLoughlin | |
570 | 68ac40d2 | Mark McLoughlin | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
571 | 68ac40d2 | Mark McLoughlin | int legacy_format)
|
572 | 68ac40d2 | Mark McLoughlin | { |
573 | 68ac40d2 | Mark McLoughlin | struct in_addr server = { .s_addr = 0 }; |
574 | 68ac40d2 | Mark McLoughlin | struct GuestFwd *fwd;
|
575 | 68ac40d2 | Mark McLoughlin | const char *p; |
576 | 68ac40d2 | Mark McLoughlin | char buf[128]; |
577 | 68ac40d2 | Mark McLoughlin | char *end;
|
578 | 68ac40d2 | Mark McLoughlin | int port;
|
579 | 68ac40d2 | Mark McLoughlin | |
580 | 68ac40d2 | Mark McLoughlin | p = config_str; |
581 | 68ac40d2 | Mark McLoughlin | if (legacy_format) {
|
582 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
583 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
584 | 68ac40d2 | Mark McLoughlin | } |
585 | 68ac40d2 | Mark McLoughlin | } else {
|
586 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
587 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
588 | 68ac40d2 | Mark McLoughlin | } |
589 | 68ac40d2 | Mark McLoughlin | if (strcmp(buf, "tcp") && buf[0] != '\0') { |
590 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
591 | 68ac40d2 | Mark McLoughlin | } |
592 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
593 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
594 | 68ac40d2 | Mark McLoughlin | } |
595 | 68ac40d2 | Mark McLoughlin | if (buf[0] != '\0' && !inet_aton(buf, &server)) { |
596 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
597 | 68ac40d2 | Mark McLoughlin | } |
598 | 68ac40d2 | Mark McLoughlin | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { |
599 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
600 | 68ac40d2 | Mark McLoughlin | } |
601 | 68ac40d2 | Mark McLoughlin | } |
602 | 68ac40d2 | Mark McLoughlin | port = strtol(buf, &end, 10);
|
603 | 68ac40d2 | Mark McLoughlin | if (*end != '\0' || port < 1 || port > 65535) { |
604 | 68ac40d2 | Mark McLoughlin | goto fail_syntax;
|
605 | 68ac40d2 | Mark McLoughlin | } |
606 | 68ac40d2 | Mark McLoughlin | |
607 | 68ac40d2 | Mark McLoughlin | fwd = qemu_malloc(sizeof(struct GuestFwd)); |
608 | 68ac40d2 | Mark McLoughlin | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); |
609 | 68ac40d2 | Mark McLoughlin | fwd->hd = qemu_chr_open(buf, p, NULL);
|
610 | 68ac40d2 | Mark McLoughlin | if (!fwd->hd) {
|
611 | 68ac40d2 | Mark McLoughlin | qemu_error("could not open guest forwarding device '%s'\n", buf);
|
612 | 68ac40d2 | Mark McLoughlin | qemu_free(fwd); |
613 | 68ac40d2 | Mark McLoughlin | return -1; |
614 | 68ac40d2 | Mark McLoughlin | } |
615 | 68ac40d2 | Mark McLoughlin | |
616 | 68ac40d2 | Mark McLoughlin | if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) { |
617 | 68ac40d2 | Mark McLoughlin | qemu_error("conflicting/invalid host:port in guest forwarding "
|
618 | 68ac40d2 | Mark McLoughlin | "rule '%s'\n", config_str);
|
619 | 68ac40d2 | Mark McLoughlin | qemu_free(fwd); |
620 | 68ac40d2 | Mark McLoughlin | return -1; |
621 | 68ac40d2 | Mark McLoughlin | } |
622 | 68ac40d2 | Mark McLoughlin | fwd->server = server; |
623 | 68ac40d2 | Mark McLoughlin | fwd->port = port; |
624 | 68ac40d2 | Mark McLoughlin | fwd->slirp = s->slirp; |
625 | 68ac40d2 | Mark McLoughlin | |
626 | 68ac40d2 | Mark McLoughlin | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, |
627 | 68ac40d2 | Mark McLoughlin | NULL, fwd);
|
628 | 68ac40d2 | Mark McLoughlin | return 0; |
629 | 68ac40d2 | Mark McLoughlin | |
630 | 68ac40d2 | Mark McLoughlin | fail_syntax:
|
631 | 68ac40d2 | Mark McLoughlin | qemu_error("invalid guest forwarding rule '%s'\n", config_str);
|
632 | 68ac40d2 | Mark McLoughlin | return -1; |
633 | 68ac40d2 | Mark McLoughlin | } |
634 | 68ac40d2 | Mark McLoughlin | |
635 | 68ac40d2 | Mark McLoughlin | void do_info_usernet(Monitor *mon)
|
636 | 68ac40d2 | Mark McLoughlin | { |
637 | 68ac40d2 | Mark McLoughlin | SlirpState *s; |
638 | 68ac40d2 | Mark McLoughlin | |
639 | 68ac40d2 | Mark McLoughlin | QTAILQ_FOREACH(s, &slirp_stacks, entry) { |
640 | ce20b5be | Mark McLoughlin | monitor_printf(mon, "VLAN %d (%s):\n",
|
641 | ce20b5be | Mark McLoughlin | s->nc.vlan ? s->nc.vlan->id : -1,
|
642 | ce20b5be | Mark McLoughlin | s->nc.name); |
643 | 68ac40d2 | Mark McLoughlin | slirp_connection_info(s->slirp, mon); |
644 | 68ac40d2 | Mark McLoughlin | } |
645 | 68ac40d2 | Mark McLoughlin | } |
646 | 68ac40d2 | Mark McLoughlin | |
647 | 68ac40d2 | Mark McLoughlin | static int net_init_slirp_configs(const char *name, const char *value, void *opaque) |
648 | 68ac40d2 | Mark McLoughlin | { |
649 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *config;
|
650 | 68ac40d2 | Mark McLoughlin | |
651 | 68ac40d2 | Mark McLoughlin | if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) { |
652 | 68ac40d2 | Mark McLoughlin | return 0; |
653 | 68ac40d2 | Mark McLoughlin | } |
654 | 68ac40d2 | Mark McLoughlin | |
655 | 68ac40d2 | Mark McLoughlin | config = qemu_mallocz(sizeof(*config));
|
656 | 68ac40d2 | Mark McLoughlin | |
657 | 68ac40d2 | Mark McLoughlin | pstrcpy(config->str, sizeof(config->str), value);
|
658 | 68ac40d2 | Mark McLoughlin | |
659 | 68ac40d2 | Mark McLoughlin | if (!strcmp(name, "hostfwd")) { |
660 | 68ac40d2 | Mark McLoughlin | config->flags = SLIRP_CFG_HOSTFWD; |
661 | 68ac40d2 | Mark McLoughlin | } |
662 | 68ac40d2 | Mark McLoughlin | |
663 | 68ac40d2 | Mark McLoughlin | config->next = slirp_configs; |
664 | 68ac40d2 | Mark McLoughlin | slirp_configs = config; |
665 | 68ac40d2 | Mark McLoughlin | |
666 | 68ac40d2 | Mark McLoughlin | return 0; |
667 | 68ac40d2 | Mark McLoughlin | } |
668 | 68ac40d2 | Mark McLoughlin | |
669 | 68ac40d2 | Mark McLoughlin | int net_init_slirp(QemuOpts *opts,
|
670 | 68ac40d2 | Mark McLoughlin | Monitor *mon, |
671 | 68ac40d2 | Mark McLoughlin | const char *name, |
672 | 68ac40d2 | Mark McLoughlin | VLANState *vlan) |
673 | 68ac40d2 | Mark McLoughlin | { |
674 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *config;
|
675 | 68ac40d2 | Mark McLoughlin | const char *vhost; |
676 | 68ac40d2 | Mark McLoughlin | const char *vhostname; |
677 | 68ac40d2 | Mark McLoughlin | const char *vdhcp_start; |
678 | 68ac40d2 | Mark McLoughlin | const char *vnamesrv; |
679 | 68ac40d2 | Mark McLoughlin | const char *tftp_export; |
680 | 68ac40d2 | Mark McLoughlin | const char *bootfile; |
681 | 68ac40d2 | Mark McLoughlin | const char *smb_export; |
682 | 68ac40d2 | Mark McLoughlin | const char *vsmbsrv; |
683 | 68ac40d2 | Mark McLoughlin | char *vnet = NULL; |
684 | 68ac40d2 | Mark McLoughlin | int restricted = 0; |
685 | 68ac40d2 | Mark McLoughlin | int ret;
|
686 | 68ac40d2 | Mark McLoughlin | |
687 | 68ac40d2 | Mark McLoughlin | vhost = qemu_opt_get(opts, "host");
|
688 | 68ac40d2 | Mark McLoughlin | vhostname = qemu_opt_get(opts, "hostname");
|
689 | 68ac40d2 | Mark McLoughlin | vdhcp_start = qemu_opt_get(opts, "dhcpstart");
|
690 | 68ac40d2 | Mark McLoughlin | vnamesrv = qemu_opt_get(opts, "dns");
|
691 | 68ac40d2 | Mark McLoughlin | tftp_export = qemu_opt_get(opts, "tftp");
|
692 | 68ac40d2 | Mark McLoughlin | bootfile = qemu_opt_get(opts, "bootfile");
|
693 | 68ac40d2 | Mark McLoughlin | smb_export = qemu_opt_get(opts, "smb");
|
694 | 68ac40d2 | Mark McLoughlin | vsmbsrv = qemu_opt_get(opts, "smbserver");
|
695 | 68ac40d2 | Mark McLoughlin | |
696 | 68ac40d2 | Mark McLoughlin | if (qemu_opt_get(opts, "ip")) { |
697 | 68ac40d2 | Mark McLoughlin | const char *ip = qemu_opt_get(opts, "ip"); |
698 | 68ac40d2 | Mark McLoughlin | int l = strlen(ip) + strlen("/24") + 1; |
699 | 68ac40d2 | Mark McLoughlin | |
700 | 68ac40d2 | Mark McLoughlin | vnet = qemu_malloc(l); |
701 | 68ac40d2 | Mark McLoughlin | |
702 | 68ac40d2 | Mark McLoughlin | /* emulate legacy ip= parameter */
|
703 | 68ac40d2 | Mark McLoughlin | pstrcpy(vnet, l, ip); |
704 | 68ac40d2 | Mark McLoughlin | pstrcat(vnet, l, "/24");
|
705 | 68ac40d2 | Mark McLoughlin | } |
706 | 68ac40d2 | Mark McLoughlin | |
707 | 68ac40d2 | Mark McLoughlin | if (qemu_opt_get(opts, "net")) { |
708 | 68ac40d2 | Mark McLoughlin | if (vnet) {
|
709 | 68ac40d2 | Mark McLoughlin | qemu_free(vnet); |
710 | 68ac40d2 | Mark McLoughlin | } |
711 | 68ac40d2 | Mark McLoughlin | vnet = qemu_strdup(qemu_opt_get(opts, "net"));
|
712 | 68ac40d2 | Mark McLoughlin | } |
713 | 68ac40d2 | Mark McLoughlin | |
714 | 68ac40d2 | Mark McLoughlin | if (qemu_opt_get(opts, "restrict") && |
715 | 68ac40d2 | Mark McLoughlin | qemu_opt_get(opts, "restrict")[0] == 'y') { |
716 | 68ac40d2 | Mark McLoughlin | restricted = 1;
|
717 | 68ac40d2 | Mark McLoughlin | } |
718 | 68ac40d2 | Mark McLoughlin | |
719 | 68ac40d2 | Mark McLoughlin | qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0); |
720 | 68ac40d2 | Mark McLoughlin | |
721 | 68ac40d2 | Mark McLoughlin | ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost,
|
722 | 68ac40d2 | Mark McLoughlin | vhostname, tftp_export, bootfile, vdhcp_start, |
723 | 68ac40d2 | Mark McLoughlin | vnamesrv, smb_export, vsmbsrv); |
724 | 68ac40d2 | Mark McLoughlin | |
725 | 68ac40d2 | Mark McLoughlin | while (slirp_configs) {
|
726 | 68ac40d2 | Mark McLoughlin | config = slirp_configs; |
727 | 68ac40d2 | Mark McLoughlin | slirp_configs = config->next; |
728 | 68ac40d2 | Mark McLoughlin | qemu_free(config); |
729 | 68ac40d2 | Mark McLoughlin | } |
730 | 68ac40d2 | Mark McLoughlin | |
731 | 68ac40d2 | Mark McLoughlin | if (ret != -1 && vlan) { |
732 | 68ac40d2 | Mark McLoughlin | vlan->nb_host_devs++; |
733 | 68ac40d2 | Mark McLoughlin | } |
734 | 68ac40d2 | Mark McLoughlin | |
735 | 68ac40d2 | Mark McLoughlin | qemu_free(vnet); |
736 | 68ac40d2 | Mark McLoughlin | |
737 | 68ac40d2 | Mark McLoughlin | return ret;
|
738 | 68ac40d2 | Mark McLoughlin | } |
739 | 68ac40d2 | Mark McLoughlin | |
740 | 68ac40d2 | Mark McLoughlin | int net_slirp_parse_legacy(QemuOptsList *opts_list, const char *optarg, int *ret) |
741 | 68ac40d2 | Mark McLoughlin | { |
742 | 68ac40d2 | Mark McLoughlin | if (strcmp(opts_list->name, "net") != 0 || |
743 | 68ac40d2 | Mark McLoughlin | strncmp(optarg, "channel,", strlen("channel,")) != 0) { |
744 | 68ac40d2 | Mark McLoughlin | return 0; |
745 | 68ac40d2 | Mark McLoughlin | } |
746 | 68ac40d2 | Mark McLoughlin | |
747 | 68ac40d2 | Mark McLoughlin | /* handle legacy -net channel,port:chr */
|
748 | 68ac40d2 | Mark McLoughlin | optarg += strlen("channel,");
|
749 | 68ac40d2 | Mark McLoughlin | |
750 | 68ac40d2 | Mark McLoughlin | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
751 | 68ac40d2 | Mark McLoughlin | struct slirp_config_str *config;
|
752 | 68ac40d2 | Mark McLoughlin | |
753 | 68ac40d2 | Mark McLoughlin | config = qemu_malloc(sizeof(*config));
|
754 | 68ac40d2 | Mark McLoughlin | pstrcpy(config->str, sizeof(config->str), optarg);
|
755 | 68ac40d2 | Mark McLoughlin | config->flags = SLIRP_CFG_LEGACY; |
756 | 68ac40d2 | Mark McLoughlin | config->next = slirp_configs; |
757 | 68ac40d2 | Mark McLoughlin | slirp_configs = config; |
758 | 68ac40d2 | Mark McLoughlin | *ret = 0;
|
759 | 68ac40d2 | Mark McLoughlin | } else {
|
760 | 68ac40d2 | Mark McLoughlin | *ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1);
|
761 | 68ac40d2 | Mark McLoughlin | } |
762 | 68ac40d2 | Mark McLoughlin | |
763 | 68ac40d2 | Mark McLoughlin | return 1; |
764 | 68ac40d2 | Mark McLoughlin | } |