blob: 35547a52dc00fd680375da30324717aed7d71458 [file] [log] [blame]
aliguori63a01ef2008-10-31 19:10:00 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguori63a01ef2008-10-31 19:10:00 +000024#include <unistd.h>
25#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <zlib.h>
31
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
aliguori63a01ef2008-10-31 19:10:00 +000035#ifndef _WIN32
36#include <sys/times.h>
37#include <sys/wait.h>
38#include <termios.h>
39#include <sys/mman.h>
40#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000041#include <sys/resource.h>
aliguori63a01ef2008-10-31 19:10:00 +000042#include <sys/socket.h>
43#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000044#include <net/if.h>
45#ifdef __NetBSD__
46#include <net/if_tap.h>
47#endif
48#ifdef __linux__
49#include <linux/if_tun.h>
50#endif
51#include <arpa/inet.h>
aliguori63a01ef2008-10-31 19:10:00 +000052#include <dirent.h>
53#include <netdb.h>
54#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020055#ifdef CONFIG_BSD
aliguori63a01ef2008-10-31 19:10:00 +000056#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000057#if defined(__FreeBSD__) || defined(__DragonFly__)
aliguori63a01ef2008-10-31 19:10:00 +000058#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000059#else
60#include <util.h>
aliguori63a01ef2008-10-31 19:10:00 +000061#endif
62#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63#include <freebsd/stdlib.h>
64#else
65#ifdef __linux__
aliguori63a01ef2008-10-31 19:10:00 +000066#include <pty.h>
67#include <malloc.h>
68#include <linux/rtc.h>
69
70/* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72/* #include <linux/hpet.h> */
73#include "hpet.h"
74
75#include <linux/ppdev.h>
76#include <linux/parport.h>
77#endif
78#ifdef __sun__
79#include <sys/stat.h>
80#include <sys/ethernet.h>
81#include <sys/sockio.h>
82#include <netinet/arp.h>
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/ip_icmp.h> // must come after ip.h
87#include <netinet/udp.h>
88#include <netinet/tcp.h>
89#include <net/if.h>
90#include <syslog.h>
91#include <stropts.h>
92#endif
93#endif
94#endif
95
aliguori63a01ef2008-10-31 19:10:00 +000096#if defined(__OpenBSD__)
97#include <util.h>
98#endif
99
100#if defined(CONFIG_VDE)
101#include <libvdeplug.h>
102#endif
103
blueswir1511d2b12009-03-07 15:32:56 +0000104#include "qemu-common.h"
105#include "net.h"
106#include "monitor.h"
107#include "sysemu.h"
108#include "qemu-timer.h"
109#include "qemu-char.h"
110#include "audio/audio.h"
111#include "qemu_socket.h"
aliguoribb9ea792009-04-21 19:56:28 +0000112#include "qemu-log.h"
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100113#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000114
Jan Kiszkad918f232009-06-24 14:42:30 +0200115#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000116
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100117static QTAILQ_HEAD(, VLANState) vlans;
aliguori63a01ef2008-10-31 19:10:00 +0000118
119/***********************************************************/
120/* network device redirectors */
121
122#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
123static void hex_dump(FILE *f, const uint8_t *buf, int size)
124{
125 int len, i, j, c;
126
127 for(i=0;i<size;i+=16) {
128 len = size - i;
129 if (len > 16)
130 len = 16;
131 fprintf(f, "%08x ", i);
132 for(j=0;j<16;j++) {
133 if (j < len)
134 fprintf(f, " %02x", buf[i+j]);
135 else
136 fprintf(f, " ");
137 }
138 fprintf(f, " ");
139 for(j=0;j<len;j++) {
140 c = buf[i+j];
141 if (c < ' ' || c > '~')
142 c = '.';
143 fprintf(f, "%c", c);
144 }
145 fprintf(f, "\n");
146 }
147}
148#endif
149
150static int parse_macaddr(uint8_t *macaddr, const char *p)
151{
152 int i;
153 char *last_char;
154 long int offset;
155
156 errno = 0;
157 offset = strtol(p, &last_char, 0);
158 if (0 == errno && '\0' == *last_char &&
159 offset >= 0 && offset <= 0xFFFFFF) {
160 macaddr[3] = (offset & 0xFF0000) >> 16;
161 macaddr[4] = (offset & 0xFF00) >> 8;
162 macaddr[5] = offset & 0xFF;
163 return 0;
164 } else {
165 for(i = 0; i < 6; i++) {
166 macaddr[i] = strtol(p, (char **)&p, 16);
167 if (i == 5) {
168 if (*p != '\0')
169 return -1;
170 } else {
171 if (*p != ':' && *p != '-')
172 return -1;
173 p++;
174 }
175 }
176 return 0;
177 }
178
179 return -1;
180}
181
182static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
183{
184 const char *p, *p1;
185 int len;
186 p = *pp;
187 p1 = strchr(p, sep);
188 if (!p1)
189 return -1;
190 len = p1 - p;
191 p1++;
192 if (buf_size > 0) {
193 if (len > buf_size - 1)
194 len = buf_size - 1;
195 memcpy(buf, p, len);
196 buf[len] = '\0';
197 }
198 *pp = p1;
199 return 0;
200}
201
202int parse_host_src_port(struct sockaddr_in *haddr,
203 struct sockaddr_in *saddr,
204 const char *input_str)
205{
206 char *str = strdup(input_str);
207 char *host_str = str;
208 char *src_str;
209 const char *src_str2;
210 char *ptr;
211
212 /*
213 * Chop off any extra arguments at the end of the string which
214 * would start with a comma, then fill in the src port information
215 * if it was provided else use the "any address" and "any port".
216 */
217 if ((ptr = strchr(str,',')))
218 *ptr = '\0';
219
220 if ((src_str = strchr(input_str,'@'))) {
221 *src_str = '\0';
222 src_str++;
223 }
224
225 if (parse_host_port(haddr, host_str) < 0)
226 goto fail;
227
228 src_str2 = src_str;
229 if (!src_str || *src_str == '\0')
230 src_str2 = ":0";
231
232 if (parse_host_port(saddr, src_str2) < 0)
233 goto fail;
234
235 free(str);
236 return(0);
237
238fail:
239 free(str);
240 return -1;
241}
242
243int parse_host_port(struct sockaddr_in *saddr, const char *str)
244{
245 char buf[512];
246 struct hostent *he;
247 const char *p, *r;
248 int port;
249
250 p = str;
251 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
252 return -1;
253 saddr->sin_family = AF_INET;
254 if (buf[0] == '\0') {
255 saddr->sin_addr.s_addr = 0;
256 } else {
blueswir1cd390082008-11-16 13:53:32 +0000257 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000258 if (!inet_aton(buf, &saddr->sin_addr))
259 return -1;
260 } else {
261 if ((he = gethostbyname(buf)) == NULL)
262 return - 1;
263 saddr->sin_addr = *(struct in_addr *)he->h_addr;
264 }
265 }
266 port = strtol(p, (char **)&r, 0);
267 if (r == p)
268 return -1;
269 saddr->sin_port = htons(port);
270 return 0;
271}
272
aliguori7cb7434b2009-01-07 17:46:21 +0000273void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
274{
275 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000276 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
277 vc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000278 macaddr[0], macaddr[1], macaddr[2],
279 macaddr[3], macaddr[4], macaddr[5]);
280}
281
aliguori676cff22009-01-07 17:43:44 +0000282static char *assign_name(VLANClientState *vc1, const char *model)
283{
284 VLANState *vlan;
285 char buf[256];
286 int id = 0;
287
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100288 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori676cff22009-01-07 17:43:44 +0000289 VLANClientState *vc;
290
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100291 QTAILQ_FOREACH(vc, &vlan->clients, next) {
292 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000293 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100294 }
295 }
aliguori676cff22009-01-07 17:43:44 +0000296 }
297
298 snprintf(buf, sizeof(buf), "%s.%d", model, id);
299
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100300 return qemu_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000301}
302
aliguori63a01ef2008-10-31 19:10:00 +0000303VLANClientState *qemu_new_vlan_client(VLANState *vlan,
aliguoribf38c1a2009-01-07 17:42:25 +0000304 const char *model,
aliguori7a9f6e42009-01-07 17:48:51 +0000305 const char *name,
Mark McLoughlincda90462009-05-18 13:13:16 +0100306 NetCanReceive *can_receive,
307 NetReceive *receive,
308 NetReceiveIOV *receive_iov,
aliguorib946a152009-04-17 17:11:08 +0000309 NetCleanup *cleanup,
aliguori63a01ef2008-10-31 19:10:00 +0000310 void *opaque)
311{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100312 VLANClientState *vc;
313
aliguori63a01ef2008-10-31 19:10:00 +0000314 vc = qemu_mallocz(sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100315
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100316 vc->model = qemu_strdup(model);
aliguori7a9f6e42009-01-07 17:48:51 +0000317 if (name)
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100318 vc->name = qemu_strdup(name);
aliguori7a9f6e42009-01-07 17:48:51 +0000319 else
320 vc->name = assign_name(vc, model);
Mark McLoughlincda90462009-05-18 13:13:16 +0100321 vc->can_receive = can_receive;
322 vc->receive = receive;
323 vc->receive_iov = receive_iov;
aliguorib946a152009-04-17 17:11:08 +0000324 vc->cleanup = cleanup;
aliguori63a01ef2008-10-31 19:10:00 +0000325 vc->opaque = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000326
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100327 vc->vlan = vlan;
328 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
329
aliguori63a01ef2008-10-31 19:10:00 +0000330 return vc;
331}
332
333void qemu_del_vlan_client(VLANClientState *vc)
334{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100335 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000336
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100337 if (vc->cleanup) {
338 vc->cleanup(vc);
339 }
340
341 qemu_free(vc->name);
342 qemu_free(vc->model);
343 qemu_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000344}
345
aliguori8b13c4a2009-02-11 15:20:51 +0000346VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
347{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100348 VLANClientState *vc;
aliguori8b13c4a2009-02-11 15:20:51 +0000349
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100350 QTAILQ_FOREACH(vc, &vlan->clients, next) {
351 if (vc->opaque == opaque) {
352 return vc;
353 }
354 }
aliguori8b13c4a2009-02-11 15:20:51 +0000355
356 return NULL;
357}
358
Jan Kiszka1a609522009-06-24 14:42:31 +0200359static VLANClientState *
360qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
361 const char *client_str)
362{
363 VLANState *vlan;
364 VLANClientState *vc;
365
366 vlan = qemu_find_vlan(vlan_id, 0);
367 if (!vlan) {
368 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
369 return NULL;
370 }
371
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100372 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200373 if (!strcmp(vc->name, client_str)) {
374 break;
375 }
376 }
377 if (!vc) {
378 monitor_printf(mon, "can't find device %s on VLAN %d\n",
379 client_str, vlan_id);
380 }
381
382 return vc;
383}
384
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100385int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000386{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100387 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000388 VLANClientState *vc;
389
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100390 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100391 if (vc == sender) {
392 continue;
393 }
394
Mark McLoughlincda90462009-05-18 13:13:16 +0100395 /* no can_receive() handler, they can always receive */
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100396 if (!vc->can_receive || vc->can_receive(vc)) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100397 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000398 }
399 }
400 return 0;
401}
402
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100403static int
aliguori764a4d12009-04-21 19:56:41 +0000404qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
aliguori63a01ef2008-10-31 19:10:00 +0000405{
aliguori63a01ef2008-10-31 19:10:00 +0000406 VLANClientState *vc;
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100407 int ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000408
Mark McLoughline94667b2009-04-29 11:48:12 +0100409 sender->vlan->delivering = 1;
410
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100411 QTAILQ_FOREACH(vc, &sender->vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100412 ssize_t len;
413
414 if (vc == sender) {
415 continue;
aliguori764a4d12009-04-21 19:56:41 +0000416 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100417
418 if (vc->link_down) {
419 ret = size;
420 continue;
421 }
422
423 len = vc->receive(vc, buf, size);
424
425 ret = (ret >= 0) ? ret : len;
aliguori764a4d12009-04-21 19:56:41 +0000426 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100427
Mark McLoughline94667b2009-04-29 11:48:12 +0100428 sender->vlan->delivering = 0;
429
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100430 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000431}
432
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100433void qemu_purge_queued_packets(VLANClientState *vc)
434{
Jan Kiszka9da43182009-08-26 12:47:04 +0200435 VLANPacket *packet, *next;
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100436
Blue Swirl72cf2d42009-09-12 07:36:22 +0000437 QTAILQ_FOREACH_SAFE(packet, &vc->vlan->send_queue, entry, next) {
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100438 if (packet->sender == vc) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000439 QTAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100440 qemu_free(packet);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100441 }
442 }
443}
444
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100445void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100446{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000447 while (!QTAILQ_EMPTY(&vc->vlan->send_queue)) {
Jan Kiszka9da43182009-08-26 12:47:04 +0200448 VLANPacket *packet;
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100449 int ret;
450
Blue Swirl72cf2d42009-09-12 07:36:22 +0000451 packet = QTAILQ_FIRST(&vc->vlan->send_queue);
452 QTAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100453
454 ret = qemu_deliver_packet(packet->sender, packet->data, packet->size);
455 if (ret == 0 && packet->sent_cb != NULL) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000456 QTAILQ_INSERT_HEAD(&vc->vlan->send_queue, packet, entry);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100457 break;
458 }
459
460 if (packet->sent_cb)
Mark McLoughlin783527a2009-06-18 18:21:35 +0100461 packet->sent_cb(packet->sender, ret);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100462
Mark McLoughline94667b2009-04-29 11:48:12 +0100463 qemu_free(packet);
464 }
465}
466
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100467static void qemu_enqueue_packet(VLANClientState *sender,
468 const uint8_t *buf, int size,
469 NetPacketSent *sent_cb)
Mark McLoughline94667b2009-04-29 11:48:12 +0100470{
471 VLANPacket *packet;
472
473 packet = qemu_malloc(sizeof(VLANPacket) + size);
Mark McLoughline94667b2009-04-29 11:48:12 +0100474 packet->sender = sender;
475 packet->size = size;
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100476 packet->sent_cb = sent_cb;
Mark McLoughline94667b2009-04-29 11:48:12 +0100477 memcpy(packet->data, buf, size);
Jan Kiszka9da43182009-08-26 12:47:04 +0200478
Blue Swirl72cf2d42009-09-12 07:36:22 +0000479 QTAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
Mark McLoughline94667b2009-04-29 11:48:12 +0100480}
481
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100482ssize_t qemu_send_packet_async(VLANClientState *sender,
483 const uint8_t *buf, int size,
484 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000485{
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100486 int ret;
aliguori764a4d12009-04-21 19:56:41 +0000487
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100488 if (sender->link_down) {
489 return size;
aliguori63a01ef2008-10-31 19:10:00 +0000490 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100491
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100492#ifdef DEBUG_NET
493 printf("vlan %d send:\n", sender->vlan->id);
494 hex_dump(stdout, buf, size);
495#endif
496
497 if (sender->vlan->delivering) {
498 qemu_enqueue_packet(sender, buf, size, NULL);
499 return size;
500 }
501
502 ret = qemu_deliver_packet(sender, buf, size);
503 if (ret == 0 && sent_cb != NULL) {
504 qemu_enqueue_packet(sender, buf, size, sent_cb);
505 return 0;
506 }
507
508 qemu_flush_queued_packets(sender);
509
510 return ret;
511}
512
513void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
514{
515 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000516}
517
aliguorifbe78f42008-12-17 19:13:11 +0000518static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
519 int iovcnt)
520{
521 uint8_t buffer[4096];
522 size_t offset = 0;
523 int i;
524
525 for (i = 0; i < iovcnt; i++) {
526 size_t len;
527
528 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
529 memcpy(buffer + offset, iov[i].iov_base, len);
530 offset += len;
531 }
532
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100533 return vc->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000534}
535
aliguorie0e78772009-01-26 15:37:44 +0000536static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
537{
538 size_t offset = 0;
539 int i;
540
541 for (i = 0; i < iovcnt; i++)
542 offset += iov[i].iov_len;
543 return offset;
544}
545
Mark McLoughline94667b2009-04-29 11:48:12 +0100546static int qemu_deliver_packet_iov(VLANClientState *sender,
547 const struct iovec *iov, int iovcnt)
548{
549 VLANClientState *vc;
550 int ret = -1;
551
552 sender->vlan->delivering = 1;
553
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100554 QTAILQ_FOREACH(vc, &sender->vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100555 ssize_t len;
556
557 if (vc == sender) {
558 continue;
559 }
560
561 if (vc->link_down) {
562 ret = calc_iov_length(iov, iovcnt);
563 continue;
564 }
565
566 if (vc->receive_iov) {
567 len = vc->receive_iov(vc, iov, iovcnt);
568 } else {
569 len = vc_sendv_compat(vc, iov, iovcnt);
570 }
571
572 ret = (ret >= 0) ? ret : len;
573 }
574
575 sender->vlan->delivering = 0;
576
577 return ret;
578}
579
580static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100581 const struct iovec *iov, int iovcnt,
582 NetPacketSent *sent_cb)
Mark McLoughline94667b2009-04-29 11:48:12 +0100583{
584 VLANPacket *packet;
585 size_t max_len = 0;
586 int i;
587
588 max_len = calc_iov_length(iov, iovcnt);
589
590 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
Mark McLoughline94667b2009-04-29 11:48:12 +0100591 packet->sender = sender;
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100592 packet->sent_cb = sent_cb;
Mark McLoughline94667b2009-04-29 11:48:12 +0100593 packet->size = 0;
594
595 for (i = 0; i < iovcnt; i++) {
596 size_t len = iov[i].iov_len;
597
598 memcpy(packet->data + packet->size, iov[i].iov_base, len);
599 packet->size += len;
600 }
601
Blue Swirl72cf2d42009-09-12 07:36:22 +0000602 QTAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
Mark McLoughline94667b2009-04-29 11:48:12 +0100603
604 return packet->size;
605}
606
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100607ssize_t qemu_sendv_packet_async(VLANClientState *sender,
608 const struct iovec *iov, int iovcnt,
609 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000610{
Mark McLoughline94667b2009-04-29 11:48:12 +0100611 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000612
Mark McLoughline94667b2009-04-29 11:48:12 +0100613 if (sender->link_down) {
aliguorie0e78772009-01-26 15:37:44 +0000614 return calc_iov_length(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000615 }
616
Mark McLoughline94667b2009-04-29 11:48:12 +0100617 if (sender->vlan->delivering) {
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100618 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
Mark McLoughline94667b2009-04-29 11:48:12 +0100619 }
620
621 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100622 if (ret == 0 && sent_cb != NULL) {
623 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
624 return 0;
625 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100626
627 qemu_flush_queued_packets(sender);
628
629 return ret;
aliguorifbe78f42008-12-17 19:13:11 +0000630}
631
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100632ssize_t
633qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
634{
635 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
636}
637
aliguori63a01ef2008-10-31 19:10:00 +0000638#if defined(CONFIG_SLIRP)
639
640/* slirp network adapter */
641
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200642#define SLIRP_CFG_HOSTFWD 1
643#define SLIRP_CFG_LEGACY 2
Jan Kiszkaad196a92009-06-24 14:42:28 +0200644
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200645struct slirp_config_str {
646 struct slirp_config_str *next;
Jan Kiszkaad196a92009-06-24 14:42:28 +0200647 int flags;
648 char str[1024];
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200649 int legacy_format;
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200650};
651
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200652typedef struct SlirpState {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000653 QTAILQ_ENTRY(SlirpState) entry;
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200654 VLANClientState *vc;
655 Slirp *slirp;
Jan Kiszka28432462009-06-24 14:42:31 +0200656#ifndef _WIN32
657 char smb_dir[128];
658#endif
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200659} SlirpState;
660
Jan Kiszkaad196a92009-06-24 14:42:28 +0200661static struct slirp_config_str *slirp_configs;
662const char *legacy_tftp_prefix;
663const char *legacy_bootp_filename;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000664static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
665 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
aliguori63a01ef2008-10-31 19:10:00 +0000666
Markus Armbrusterfb125772009-10-06 12:16:58 +0100667static int slirp_hostfwd(SlirpState *s, const char *redir_str,
Markus Armbruster07527062009-10-06 12:16:57 +0100668 int legacy_format);
Markus Armbrusterfb125772009-10-06 12:16:58 +0100669static int slirp_guestfwd(SlirpState *s, const char *config_str,
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200670 int legacy_format);
Jan Kiszkaad196a92009-06-24 14:42:28 +0200671
Blue Swirlc5b76b32009-06-13 08:44:31 +0000672#ifndef _WIN32
Jan Kiszkaad196a92009-06-24 14:42:28 +0200673static const char *legacy_smb_export;
674
Markus Armbrusterfb125772009-10-06 12:16:58 +0100675static int slirp_smb(SlirpState *s, const char *exported_dir,
Markus Armbruster07527062009-10-06 12:16:57 +0100676 struct in_addr vserver_addr);
Jan Kiszka28432462009-06-24 14:42:31 +0200677static void slirp_smb_cleanup(SlirpState *s);
678#else
679static inline void slirp_smb_cleanup(SlirpState *s) { }
Blue Swirlc5b76b32009-06-13 08:44:31 +0000680#endif
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200681
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200682int slirp_can_output(void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000683{
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200684 SlirpState *s = opaque;
685
686 return qemu_can_send_packet(s->vc);
aliguori63a01ef2008-10-31 19:10:00 +0000687}
688
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200689void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
aliguori63a01ef2008-10-31 19:10:00 +0000690{
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200691 SlirpState *s = opaque;
692
aliguori63a01ef2008-10-31 19:10:00 +0000693#ifdef DEBUG_SLIRP
694 printf("slirp output:\n");
695 hex_dump(stdout, pkt, pkt_len);
696#endif
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200697 qemu_send_packet(s->vc, pkt, pkt_len);
aliguori63a01ef2008-10-31 19:10:00 +0000698}
699
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100700static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguori63a01ef2008-10-31 19:10:00 +0000701{
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200702 SlirpState *s = vc->opaque;
703
aliguori63a01ef2008-10-31 19:10:00 +0000704#ifdef DEBUG_SLIRP
705 printf("slirp input:\n");
706 hex_dump(stdout, buf, size);
707#endif
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200708 slirp_input(s->slirp, buf, size);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100709 return size;
aliguori63a01ef2008-10-31 19:10:00 +0000710}
711
aliguori8d6249a2009-04-21 20:49:11 +0000712static void net_slirp_cleanup(VLANClientState *vc)
713{
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200714 SlirpState *s = vc->opaque;
715
716 slirp_cleanup(s->slirp);
Jan Kiszka28432462009-06-24 14:42:31 +0200717 slirp_smb_cleanup(s);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000718 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200719 qemu_free(s);
aliguori8d6249a2009-04-21 20:49:11 +0000720}
721
Markus Armbrusterfb125772009-10-06 12:16:58 +0100722static int net_slirp_init(VLANState *vlan, const char *model,
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200723 const char *name, int restricted,
724 const char *vnetwork, const char *vhost,
725 const char *vhostname, const char *tftp_export,
726 const char *bootfile, const char *vdhcp_start,
727 const char *vnameserver, const char *smb_export,
728 const char *vsmbserver)
aliguori63a01ef2008-10-31 19:10:00 +0000729{
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200730 /* default settings according to historic slirp */
Anthony Liguori8389e7f2009-07-10 12:43:14 -0500731 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
732 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200733 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
734 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
735 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
736#ifndef _WIN32
737 struct in_addr smbsrv = { .s_addr = 0 };
738#endif
739 SlirpState *s;
740 char buf[20];
741 uint32_t addr;
742 int shift;
743 char *end;
Markus Armbruster3a179c62009-10-06 12:16:56 +0100744 struct slirp_config_str *config;
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200745
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200746 if (!tftp_export) {
747 tftp_export = legacy_tftp_prefix;
748 }
749 if (!bootfile) {
750 bootfile = legacy_bootp_filename;
751 }
752
753 if (vnetwork) {
754 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
755 if (!inet_aton(vnetwork, &net)) {
756 return -1;
757 }
758 addr = ntohl(net.s_addr);
759 if (!(addr & 0x80000000)) {
760 mask.s_addr = htonl(0xff000000); /* class A */
761 } else if ((addr & 0xfff00000) == 0xac100000) {
762 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
763 } else if ((addr & 0xc0000000) == 0x80000000) {
764 mask.s_addr = htonl(0xffff0000); /* class B */
765 } else if ((addr & 0xffff0000) == 0xc0a80000) {
766 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
767 } else if ((addr & 0xffff0000) == 0xc6120000) {
768 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
769 } else if ((addr & 0xe0000000) == 0xe0000000) {
770 mask.s_addr = htonl(0xffffff00); /* class C */
771 } else {
772 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
773 }
774 } else {
775 if (!inet_aton(buf, &net)) {
776 return -1;
777 }
778 shift = strtol(vnetwork, &end, 10);
779 if (*end != '\0') {
780 if (!inet_aton(vnetwork, &mask)) {
781 return -1;
782 }
783 } else if (shift < 4 || shift > 32) {
784 return -1;
785 } else {
786 mask.s_addr = htonl(0xffffffff << (32 - shift));
787 }
788 }
789 net.s_addr &= mask.s_addr;
790 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
791 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
792 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
793 }
794
795 if (vhost && !inet_aton(vhost, &host)) {
aliguori8d6249a2009-04-21 20:49:11 +0000796 return -1;
797 }
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200798 if ((host.s_addr & mask.s_addr) != net.s_addr) {
799 return -1;
aliguori63a01ef2008-10-31 19:10:00 +0000800 }
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200801
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200802 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
803 return -1;
804 }
805 if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
806 dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
807 return -1;
808 }
809
810 if (vnameserver && !inet_aton(vnameserver, &dns)) {
811 return -1;
812 }
813 if ((dns.s_addr & mask.s_addr) != net.s_addr ||
814 dns.s_addr == host.s_addr) {
815 return -1;
816 }
817
818#ifndef _WIN32
819 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
820 return -1;
821 }
822#endif
823
824 s = qemu_mallocz(sizeof(SlirpState));
825 s->slirp = slirp_init(restricted, net, mask, host, vhostname,
826 tftp_export, bootfile, dhcp, dns, s);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000827 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200828
Markus Armbruster3a179c62009-10-06 12:16:56 +0100829 for (config = slirp_configs; config; config = config->next) {
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200830 if (config->flags & SLIRP_CFG_HOSTFWD) {
Markus Armbrusterfb125772009-10-06 12:16:58 +0100831 if (slirp_hostfwd(s, config->str,
Markus Armbruster07527062009-10-06 12:16:57 +0100832 config->flags & SLIRP_CFG_LEGACY) < 0)
833 return -1;
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200834 } else {
Markus Armbrusterfb125772009-10-06 12:16:58 +0100835 if (slirp_guestfwd(s, config->str,
Markus Armbruster07527062009-10-06 12:16:57 +0100836 config->flags & SLIRP_CFG_LEGACY) < 0)
837 return -1;
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200838 }
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200839 }
840#ifndef _WIN32
841 if (!smb_export) {
842 smb_export = legacy_smb_export;
843 }
844 if (smb_export) {
Markus Armbrusterfb125772009-10-06 12:16:58 +0100845 if (slirp_smb(s, smb_export, smbsrv) < 0)
Markus Armbruster07527062009-10-06 12:16:57 +0100846 return -1;
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200847 }
848#endif
849
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200850 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
851 net_slirp_cleanup, s);
Jan Kiszkafc57bc52009-06-24 14:42:32 +0200852 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
853 "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n');
aliguori63a01ef2008-10-31 19:10:00 +0000854 return 0;
855}
856
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200857static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
858 const char *stack)
859{
860 VLANClientState *vc;
861
862 if (vlan) {
863 vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack);
864 if (!vc) {
865 return NULL;
866 }
867 if (strcmp(vc->model, "user")) {
868 monitor_printf(mon, "invalid device specified\n");
869 return NULL;
870 }
871 return vc->opaque;
872 } else {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000873 if (QTAILQ_EMPTY(&slirp_stacks)) {
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200874 monitor_printf(mon, "user mode network stack not in use\n");
875 return NULL;
876 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000877 return QTAILQ_FIRST(&slirp_stacks);
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200878 }
879}
880
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300881void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Alexander Grafc1261d82009-05-26 13:03:26 +0200882{
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200883 struct in_addr host_addr = { .s_addr = INADDR_ANY };
Alexander Grafc1261d82009-05-26 13:03:26 +0200884 int host_port;
885 char buf[256] = "";
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200886 const char *src_str, *p;
887 SlirpState *s;
Alexander Grafc1261d82009-05-26 13:03:26 +0200888 int is_udp = 0;
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200889 int err;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300890 const char *arg1 = qdict_get_str(qdict, "arg1");
891 const char *arg2 = qdict_get_try_str(qdict, "arg2");
892 const char *arg3 = qdict_get_try_str(qdict, "arg3");
Alexander Grafc1261d82009-05-26 13:03:26 +0200893
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200894 if (arg2) {
895 s = slirp_lookup(mon, arg1, arg2);
896 src_str = arg3;
897 } else {
898 s = slirp_lookup(mon, NULL, NULL);
899 src_str = arg1;
900 }
901 if (!s) {
Alexander Grafc1261d82009-05-26 13:03:26 +0200902 return;
Jan Kiszkaf3546de2009-06-24 14:42:28 +0200903 }
Alexander Grafc1261d82009-05-26 13:03:26 +0200904
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200905 if (!src_str || !src_str[0])
Alexander Grafc1261d82009-05-26 13:03:26 +0200906 goto fail_syntax;
907
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200908 p = src_str;
Alexander Grafc1261d82009-05-26 13:03:26 +0200909 get_str_sep(buf, sizeof(buf), &p, ':');
910
911 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
912 is_udp = 0;
913 } else if (!strcmp(buf, "udp")) {
914 is_udp = 1;
915 } else {
916 goto fail_syntax;
917 }
918
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200919 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
920 goto fail_syntax;
921 }
922 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
923 goto fail_syntax;
924 }
925
Alexander Grafc1261d82009-05-26 13:03:26 +0200926 host_port = atoi(p);
927
Blue Swirl72cf2d42009-09-12 07:36:22 +0000928 err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200929 host_addr, host_port);
Alexander Grafc1261d82009-05-26 13:03:26 +0200930
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200931 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
932 err ? "removed" : "not found");
Alexander Grafc1261d82009-05-26 13:03:26 +0200933 return;
934
935 fail_syntax:
936 monitor_printf(mon, "invalid format\n");
937}
938
Markus Armbrusterfb125772009-10-06 12:16:58 +0100939static int slirp_hostfwd(SlirpState *s, const char *redir_str,
Markus Armbruster07527062009-10-06 12:16:57 +0100940 int legacy_format)
aliguori63a01ef2008-10-31 19:10:00 +0000941{
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200942 struct in_addr host_addr = { .s_addr = INADDR_ANY };
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200943 struct in_addr guest_addr = { .s_addr = 0 };
aliguori63a01ef2008-10-31 19:10:00 +0000944 int host_port, guest_port;
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200945 const char *p;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200946 char buf[256];
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200947 int is_udp;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200948 char *end;
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200949
950 p = redir_str;
Jan Kiszkaf13b5722009-06-24 14:42:32 +0200951 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200952 goto fail_syntax;
953 }
954 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
955 is_udp = 0;
956 } else if (!strcmp(buf, "udp")) {
957 is_udp = 1;
958 } else {
959 goto fail_syntax;
960 }
961
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200962 if (!legacy_format) {
963 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
964 goto fail_syntax;
965 }
966 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
967 goto fail_syntax;
968 }
969 }
970
971 if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200972 goto fail_syntax;
973 }
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200974 host_port = strtol(buf, &end, 0);
975 if (*end != '\0' || host_port < 1 || host_port > 65535) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200976 goto fail_syntax;
977 }
978
979 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
980 goto fail_syntax;
981 }
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200982 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200983 goto fail_syntax;
984 }
985
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200986 guest_port = strtol(p, &end, 0);
987 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200988 goto fail_syntax;
989 }
990
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200991 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
992 guest_port) < 0) {
Markus Armbrusterfb125772009-10-06 12:16:58 +0100993 qemu_error("could not set up host forwarding rule '%s'\n",
994 redir_str);
Markus Armbruster07527062009-10-06 12:16:57 +0100995 return -1;
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200996 }
Markus Armbruster07527062009-10-06 12:16:57 +0100997 return 0;
Jan Kiszkab8e8af32009-05-08 12:34:18 +0200998
999 fail_syntax:
Markus Armbrusterfb125772009-10-06 12:16:58 +01001000 qemu_error("invalid host forwarding rule '%s'\n", redir_str);
Markus Armbruster07527062009-10-06 12:16:57 +01001001 return -1;
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001002}
1003
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001004void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict)
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001005{
Jan Kiszkaf13b5722009-06-24 14:42:32 +02001006 const char *redir_str;
1007 SlirpState *s;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001008 const char *arg1 = qdict_get_str(qdict, "arg1");
1009 const char *arg2 = qdict_get_try_str(qdict, "arg2");
1010 const char *arg3 = qdict_get_try_str(qdict, "arg3");
Jan Kiszkaf13b5722009-06-24 14:42:32 +02001011
1012 if (arg2) {
1013 s = slirp_lookup(mon, arg1, arg2);
1014 redir_str = arg3;
1015 } else {
1016 s = slirp_lookup(mon, NULL, NULL);
1017 redir_str = arg1;
1018 }
1019 if (s) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001020 slirp_hostfwd(s, redir_str, 0);
Alexander Grafc1261d82009-05-26 13:03:26 +02001021 }
1022
aliguori63a01ef2008-10-31 19:10:00 +00001023}
1024
Markus Armbruster07527062009-10-06 12:16:57 +01001025int net_slirp_redir(const char *redir_str)
Jan Kiszkaf3546de2009-06-24 14:42:28 +02001026{
1027 struct slirp_config_str *config;
1028
Blue Swirl72cf2d42009-09-12 07:36:22 +00001029 if (QTAILQ_EMPTY(&slirp_stacks)) {
Jan Kiszkaf3546de2009-06-24 14:42:28 +02001030 config = qemu_malloc(sizeof(*config));
1031 pstrcpy(config->str, sizeof(config->str), redir_str);
Jan Kiszka3c6a0582009-06-24 14:42:28 +02001032 config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
Jan Kiszkaf3546de2009-06-24 14:42:28 +02001033 config->next = slirp_configs;
1034 slirp_configs = config;
Markus Armbruster07527062009-10-06 12:16:57 +01001035 return 0;
Jan Kiszkaf3546de2009-06-24 14:42:28 +02001036 }
1037
Markus Armbrusterfb125772009-10-06 12:16:58 +01001038 return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1);
Jan Kiszkaf3546de2009-06-24 14:42:28 +02001039}
1040
aliguori63a01ef2008-10-31 19:10:00 +00001041#ifndef _WIN32
1042
aliguori63a01ef2008-10-31 19:10:00 +00001043/* automatic user mode samba server configuration */
Jan Kiszka28432462009-06-24 14:42:31 +02001044static void slirp_smb_cleanup(SlirpState *s)
aliguori63a01ef2008-10-31 19:10:00 +00001045{
Jan Kiszka28432462009-06-24 14:42:31 +02001046 char cmd[128];
Jan Kiszka09c18922009-06-24 14:42:31 +02001047
Jan Kiszka28432462009-06-24 14:42:31 +02001048 if (s->smb_dir[0] != '\0') {
1049 snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
1050 system(cmd);
1051 s->smb_dir[0] = '\0';
1052 }
aliguori63a01ef2008-10-31 19:10:00 +00001053}
1054
Markus Armbrusterfb125772009-10-06 12:16:58 +01001055static int slirp_smb(SlirpState* s, const char *exported_dir,
Markus Armbruster07527062009-10-06 12:16:57 +01001056 struct in_addr vserver_addr)
aliguori63a01ef2008-10-31 19:10:00 +00001057{
Jan Kiszka28432462009-06-24 14:42:31 +02001058 static int instance;
1059 char smb_conf[128];
1060 char smb_cmdline[128];
aliguori63a01ef2008-10-31 19:10:00 +00001061 FILE *f;
1062
Jan Kiszka28432462009-06-24 14:42:31 +02001063 snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
1064 (long)getpid(), instance++);
1065 if (mkdir(s->smb_dir, 0700) < 0) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001066 qemu_error("could not create samba server dir '%s'\n", s->smb_dir);
Markus Armbruster07527062009-10-06 12:16:57 +01001067 return -1;
aliguori63a01ef2008-10-31 19:10:00 +00001068 }
Jan Kiszka28432462009-06-24 14:42:31 +02001069 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
aliguori63a01ef2008-10-31 19:10:00 +00001070
1071 f = fopen(smb_conf, "w");
1072 if (!f) {
Jan Kiszka28432462009-06-24 14:42:31 +02001073 slirp_smb_cleanup(s);
Markus Armbrusterfb125772009-10-06 12:16:58 +01001074 qemu_error("could not create samba server configuration file '%s'\n",
1075 smb_conf);
Markus Armbruster07527062009-10-06 12:16:57 +01001076 return -1;
aliguori63a01ef2008-10-31 19:10:00 +00001077 }
1078 fprintf(f,
1079 "[global]\n"
1080 "private dir=%s\n"
1081 "smb ports=0\n"
1082 "socket address=127.0.0.1\n"
1083 "pid directory=%s\n"
1084 "lock directory=%s\n"
1085 "log file=%s/log.smbd\n"
1086 "smb passwd file=%s/smbpasswd\n"
1087 "security = share\n"
1088 "[qemu]\n"
1089 "path=%s\n"
1090 "read only=no\n"
1091 "guest ok=yes\n",
Jan Kiszka28432462009-06-24 14:42:31 +02001092 s->smb_dir,
1093 s->smb_dir,
1094 s->smb_dir,
1095 s->smb_dir,
1096 s->smb_dir,
aliguori63a01ef2008-10-31 19:10:00 +00001097 exported_dir
1098 );
1099 fclose(f);
aliguori63a01ef2008-10-31 19:10:00 +00001100
1101 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
1102 SMBD_COMMAND, smb_conf);
1103
Jan Kiszkabb53fc52009-07-22 17:03:52 +02001104 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) {
Jan Kiszka28432462009-06-24 14:42:31 +02001105 slirp_smb_cleanup(s);
Markus Armbrusterfb125772009-10-06 12:16:58 +01001106 qemu_error("conflicting/invalid smbserver address\n");
Markus Armbruster07527062009-10-06 12:16:57 +01001107 return -1;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001108 }
Markus Armbruster07527062009-10-06 12:16:57 +01001109 return 0;
aliguori63a01ef2008-10-31 19:10:00 +00001110}
1111
Jan Kiszkaad196a92009-06-24 14:42:28 +02001112/* automatic user mode samba server configuration (legacy interface) */
Markus Armbruster07527062009-10-06 12:16:57 +01001113int net_slirp_smb(const char *exported_dir)
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001114{
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001115 struct in_addr vserver_addr = { .s_addr = 0 };
1116
Jan Kiszkaad196a92009-06-24 14:42:28 +02001117 if (legacy_smb_export) {
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001118 fprintf(stderr, "-smb given twice\n");
Markus Armbruster07527062009-10-06 12:16:57 +01001119 return -1;
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001120 }
Jan Kiszkaad196a92009-06-24 14:42:28 +02001121 legacy_smb_export = exported_dir;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001122 if (!QTAILQ_EMPTY(&slirp_stacks)) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001123 return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
Markus Armbruster07527062009-10-06 12:16:57 +01001124 vserver_addr);
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001125 }
Markus Armbruster07527062009-10-06 12:16:57 +01001126 return 0;
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001127}
1128
aliguori63a01ef2008-10-31 19:10:00 +00001129#endif /* !defined(_WIN32) */
Jan Kiszkab8e8af32009-05-08 12:34:18 +02001130
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001131struct GuestFwd {
aliguori8ca92172009-02-16 15:34:18 +00001132 CharDriverState *hd;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001133 struct in_addr server;
aliguori8ca92172009-02-16 15:34:18 +00001134 int port;
Jan Kiszka9f8bd042009-06-24 14:42:31 +02001135 Slirp *slirp;
blueswir1511d2b12009-03-07 15:32:56 +00001136};
aliguori8ca92172009-02-16 15:34:18 +00001137
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001138static int guestfwd_can_read(void *opaque)
aliguori8ca92172009-02-16 15:34:18 +00001139{
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001140 struct GuestFwd *fwd = opaque;
Jan Kiszka9f8bd042009-06-24 14:42:31 +02001141 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
aliguori8ca92172009-02-16 15:34:18 +00001142}
1143
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001144static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
aliguori8ca92172009-02-16 15:34:18 +00001145{
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001146 struct GuestFwd *fwd = opaque;
Jan Kiszka9f8bd042009-06-24 14:42:31 +02001147 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
aliguori8ca92172009-02-16 15:34:18 +00001148}
1149
Markus Armbrusterfb125772009-10-06 12:16:58 +01001150static int slirp_guestfwd(SlirpState *s, const char *config_str,
Markus Armbruster07527062009-10-06 12:16:57 +01001151 int legacy_format)
Jan Kiszkaad196a92009-06-24 14:42:28 +02001152{
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001153 struct in_addr server = { .s_addr = 0 };
1154 struct GuestFwd *fwd;
1155 const char *p;
1156 char buf[128];
1157 char *end;
Jan Kiszkaad196a92009-06-24 14:42:28 +02001158 int port;
1159
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001160 p = config_str;
1161 if (legacy_format) {
1162 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1163 goto fail_syntax;
1164 }
1165 } else {
1166 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1167 goto fail_syntax;
1168 }
1169 if (strcmp(buf, "tcp") && buf[0] != '\0') {
1170 goto fail_syntax;
1171 }
1172 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1173 goto fail_syntax;
1174 }
1175 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
1176 goto fail_syntax;
1177 }
1178 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
1179 goto fail_syntax;
1180 }
1181 }
1182 port = strtol(buf, &end, 10);
1183 if (*end != '\0' || port < 1 || port > 65535) {
1184 goto fail_syntax;
1185 }
1186
1187 fwd = qemu_malloc(sizeof(struct GuestFwd));
1188 snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port);
1189 fwd->hd = qemu_chr_open(buf, p, NULL);
1190 if (!fwd->hd) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001191 qemu_error("could not open guest forwarding device '%s'\n", buf);
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001192 qemu_free(fwd);
Markus Armbruster07527062009-10-06 12:16:57 +01001193 return -1;
Jan Kiszkaad196a92009-06-24 14:42:28 +02001194 }
Jan Kiszkaad196a92009-06-24 14:42:28 +02001195
Jan Kiszkabb53fc52009-07-22 17:03:52 +02001196 if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001197 qemu_error("conflicting/invalid host:port in guest forwarding "
1198 "rule '%s'\n", config_str);
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001199 qemu_free(fwd);
Markus Armbruster07527062009-10-06 12:16:57 +01001200 return -1;
Jan Kiszkaad196a92009-06-24 14:42:28 +02001201 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +02001202 fwd->server = server;
1203 fwd->port = port;
1204 fwd->slirp = s->slirp;
1205
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001206 qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
1207 NULL, fwd);
Markus Armbruster07527062009-10-06 12:16:57 +01001208 return 0;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +02001209
1210 fail_syntax:
Markus Armbrusterfb125772009-10-06 12:16:58 +01001211 qemu_error("invalid guest forwarding rule '%s'\n", config_str);
Markus Armbruster07527062009-10-06 12:16:57 +01001212 return -1;
Jan Kiszkaad196a92009-06-24 14:42:28 +02001213}
1214
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001215void do_info_usernet(Monitor *mon)
1216{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +02001217 SlirpState *s;
Jan Kiszka9f8bd042009-06-24 14:42:31 +02001218
Blue Swirl72cf2d42009-09-12 07:36:22 +00001219 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Jan Kiszkab1c99fc2009-06-24 14:42:31 +02001220 monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
1221 slirp_connection_info(s->slirp, mon);
Jan Kiszka9f8bd042009-06-24 14:42:31 +02001222 }
Jan Kiszka6dbe5532009-06-24 14:42:29 +02001223}
1224
aliguori63a01ef2008-10-31 19:10:00 +00001225#endif /* CONFIG_SLIRP */
1226
1227#if !defined(_WIN32)
1228
1229typedef struct TAPState {
1230 VLANClientState *vc;
1231 int fd;
1232 char down_script[1024];
aliguori973cbd32009-01-13 19:15:55 +00001233 char down_script_arg[128];
Mark McLoughlin5b01e882009-05-18 12:05:44 +01001234 uint8_t buf[4096];
Mark McLoughlinb664e362009-06-18 18:21:31 +01001235 unsigned int read_poll : 1;
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001236 unsigned int write_poll : 1;
aliguori63a01ef2008-10-31 19:10:00 +00001237} TAPState;
1238
aliguorib946a152009-04-17 17:11:08 +00001239static int launch_script(const char *setup_script, const char *ifname, int fd);
1240
Mark McLoughlinb664e362009-06-18 18:21:31 +01001241static int tap_can_send(void *opaque);
1242static void tap_send(void *opaque);
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001243static void tap_writable(void *opaque);
Mark McLoughlinb664e362009-06-18 18:21:31 +01001244
1245static void tap_update_fd_handler(TAPState *s)
1246{
1247 qemu_set_fd_handler2(s->fd,
1248 s->read_poll ? tap_can_send : NULL,
1249 s->read_poll ? tap_send : NULL,
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001250 s->write_poll ? tap_writable : NULL,
Mark McLoughlinb664e362009-06-18 18:21:31 +01001251 s);
1252}
1253
1254static void tap_read_poll(TAPState *s, int enable)
1255{
1256 s->read_poll = !!enable;
1257 tap_update_fd_handler(s);
1258}
1259
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001260static void tap_write_poll(TAPState *s, int enable)
1261{
1262 s->write_poll = !!enable;
1263 tap_update_fd_handler(s);
1264}
1265
1266static void tap_writable(void *opaque)
1267{
1268 TAPState *s = opaque;
1269
1270 tap_write_poll(s, 0);
1271
1272 qemu_flush_queued_packets(s->vc);
1273}
1274
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001275static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
aliguorib535b7b2008-12-17 19:17:17 +00001276 int iovcnt)
1277{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001278 TAPState *s = vc->opaque;
aliguorib535b7b2008-12-17 19:17:17 +00001279 ssize_t len;
1280
1281 do {
1282 len = writev(s->fd, iov, iovcnt);
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001283 } while (len == -1 && errno == EINTR);
1284
1285 if (len == -1 && errno == EAGAIN) {
1286 tap_write_poll(s, 1);
1287 return 0;
1288 }
aliguorib535b7b2008-12-17 19:17:17 +00001289
1290 return len;
1291}
aliguorib535b7b2008-12-17 19:17:17 +00001292
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001293static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguori63a01ef2008-10-31 19:10:00 +00001294{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001295 TAPState *s = vc->opaque;
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001296 ssize_t len;
1297
1298 do {
1299 len = write(s->fd, buf, size);
1300 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
1301
1302 return len;
aliguori63a01ef2008-10-31 19:10:00 +00001303}
1304
Mark McLoughlin3471b752009-04-29 09:50:32 +01001305static int tap_can_send(void *opaque)
1306{
1307 TAPState *s = opaque;
1308
1309 return qemu_can_send_packet(s->vc);
1310}
1311
Mark McLoughlin5a6d8812009-04-29 09:43:37 +01001312#ifdef __sun__
1313static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1314{
1315 struct strbuf sbuf;
1316 int f = 0;
1317
1318 sbuf.maxlen = maxlen;
1319 sbuf.buf = (char *)buf;
1320
1321 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1322}
1323#else
1324static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1325{
1326 return read(tapfd, buf, maxlen);
1327}
1328#endif
1329
Mark McLoughlin783527a2009-06-18 18:21:35 +01001330static void tap_send_completed(VLANClientState *vc, ssize_t len)
Mark McLoughline19eb222009-04-29 13:30:24 +01001331{
1332 TAPState *s = vc->opaque;
Mark McLoughlinb664e362009-06-18 18:21:31 +01001333 tap_read_poll(s, 1);
Mark McLoughline19eb222009-04-29 13:30:24 +01001334}
1335
aliguori63a01ef2008-10-31 19:10:00 +00001336static void tap_send(void *opaque)
1337{
1338 TAPState *s = opaque;
aliguori63a01ef2008-10-31 19:10:00 +00001339 int size;
1340
Mark McLoughline19eb222009-04-29 13:30:24 +01001341 do {
1342 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1343 if (size <= 0) {
1344 break;
1345 }
1346
1347 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1348 if (size == 0) {
Mark McLoughlinb664e362009-06-18 18:21:31 +01001349 tap_read_poll(s, 0);
Mark McLoughline19eb222009-04-29 13:30:24 +01001350 }
1351 } while (size > 0);
aliguori63a01ef2008-10-31 19:10:00 +00001352}
1353
Mark McLoughlin0df0ff62009-06-18 18:21:34 +01001354#ifdef TUNSETSNDBUF
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001355/* sndbuf should be set to a value lower than the tx queue
1356 * capacity of any destination network interface.
1357 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
1358 * a good default, given a 1500 byte MTU.
1359 */
1360#define TAP_DEFAULT_SNDBUF 1024*1024
1361
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01001362static int tap_set_sndbuf(TAPState *s, QemuOpts *opts)
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001363{
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01001364 int sndbuf;
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001365
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01001366 sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001367 if (!sndbuf) {
1368 sndbuf = INT_MAX;
1369 }
1370
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01001371 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01001372 qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno));
Markus Armbruster07527062009-10-06 12:16:57 +01001373 return -1;
Mark McLoughlin0df0ff62009-06-18 18:21:34 +01001374 }
Markus Armbruster07527062009-10-06 12:16:57 +01001375 return 0;
Mark McLoughlin0df0ff62009-06-18 18:21:34 +01001376}
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001377#else
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01001378static int tap_set_sndbuf(TAPState *s, QemuOpts *opts)
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001379{
Markus Armbruster07527062009-10-06 12:16:57 +01001380 return 0;
Mark McLoughlinfc5b81d2009-06-30 10:02:57 +01001381}
1382#endif /* TUNSETSNDBUF */
Mark McLoughlin0df0ff62009-06-18 18:21:34 +01001383
aliguorib946a152009-04-17 17:11:08 +00001384static void tap_cleanup(VLANClientState *vc)
1385{
1386 TAPState *s = vc->opaque;
1387
Mark McLoughlinb9adce22009-06-18 18:21:30 +01001388 qemu_purge_queued_packets(vc);
1389
aliguorib946a152009-04-17 17:11:08 +00001390 if (s->down_script[0])
1391 launch_script(s->down_script, s->down_script_arg, s->fd);
1392
Mark McLoughlinb664e362009-06-18 18:21:31 +01001393 tap_read_poll(s, 0);
Mark McLoughlin1f7babf2009-06-18 18:21:32 +01001394 tap_write_poll(s, 0);
aliguorib946a152009-04-17 17:11:08 +00001395 close(s->fd);
1396 qemu_free(s);
1397}
1398
aliguori63a01ef2008-10-31 19:10:00 +00001399/* fd support */
1400
aliguori7a9f6e42009-01-07 17:48:51 +00001401static TAPState *net_tap_fd_init(VLANState *vlan,
1402 const char *model,
1403 const char *name,
1404 int fd)
aliguori63a01ef2008-10-31 19:10:00 +00001405{
1406 TAPState *s;
1407
1408 s = qemu_mallocz(sizeof(TAPState));
aliguori63a01ef2008-10-31 19:10:00 +00001409 s->fd = fd;
Mark McLoughlin463af532009-05-18 12:55:27 +01001410 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1411 tap_receive_iov, tap_cleanup, s);
Mark McLoughlinb664e362009-06-18 18:21:31 +01001412 tap_read_poll(s, 1);
aliguori7cb7434b2009-01-07 17:46:21 +00001413 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
aliguori63a01ef2008-10-31 19:10:00 +00001414 return s;
1415}
1416
Juan Quintela71e72a12009-07-27 16:12:56 +02001417#if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__)
aliguori63a01ef2008-10-31 19:10:00 +00001418static int tap_open(char *ifname, int ifname_size)
1419{
1420 int fd;
1421 char *dev;
1422 struct stat s;
1423
1424 TFR(fd = open("/dev/tap", O_RDWR));
1425 if (fd < 0) {
1426 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1427 return -1;
1428 }
1429
1430 fstat(fd, &s);
1431 dev = devname(s.st_rdev, S_IFCHR);
1432 pstrcpy(ifname, ifname_size, dev);
1433
1434 fcntl(fd, F_SETFL, O_NONBLOCK);
1435 return fd;
1436}
1437#elif defined(__sun__)
1438#define TUNNEWPPA (('T'<<16) | 0x0001)
1439/*
1440 * Allocate TAP device, returns opened fd.
1441 * Stores dev name in the first arg(must be large enough).
1442 */
blueswir13f4cb3d2009-04-13 16:31:01 +00001443static int tap_alloc(char *dev, size_t dev_size)
aliguori63a01ef2008-10-31 19:10:00 +00001444{
1445 int tap_fd, if_fd, ppa = -1;
1446 static int ip_fd = 0;
1447 char *ptr;
1448
1449 static int arp_fd = 0;
1450 int ip_muxid, arp_muxid;
1451 struct strioctl strioc_if, strioc_ppa;
1452 int link_type = I_PLINK;;
1453 struct lifreq ifr;
1454 char actual_name[32] = "";
1455
1456 memset(&ifr, 0x0, sizeof(ifr));
1457
1458 if( *dev ){
1459 ptr = dev;
blueswir147398b92008-11-22 20:04:24 +00001460 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
aliguori63a01ef2008-10-31 19:10:00 +00001461 ppa = atoi(ptr);
1462 }
1463
1464 /* Check if IP device was opened */
1465 if( ip_fd )
1466 close(ip_fd);
1467
1468 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1469 if (ip_fd < 0) {
1470 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1471 return -1;
1472 }
1473
1474 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1475 if (tap_fd < 0) {
1476 syslog(LOG_ERR, "Can't open /dev/tap");
1477 return -1;
1478 }
1479
1480 /* Assign a new PPA and get its unit number. */
1481 strioc_ppa.ic_cmd = TUNNEWPPA;
1482 strioc_ppa.ic_timout = 0;
1483 strioc_ppa.ic_len = sizeof(ppa);
1484 strioc_ppa.ic_dp = (char *)&ppa;
1485 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1486 syslog (LOG_ERR, "Can't assign new interface");
1487
1488 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1489 if (if_fd < 0) {
1490 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1491 return -1;
1492 }
1493 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1494 syslog(LOG_ERR, "Can't push IP module");
1495 return -1;
1496 }
1497
1498 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1499 syslog(LOG_ERR, "Can't get flags\n");
1500
1501 snprintf (actual_name, 32, "tap%d", ppa);
1502 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1503
1504 ifr.lifr_ppa = ppa;
1505 /* Assign ppa according to the unit number returned by tun device */
1506
1507 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1508 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1509 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1510 syslog (LOG_ERR, "Can't get flags\n");
1511 /* Push arp module to if_fd */
1512 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1513 syslog (LOG_ERR, "Can't push ARP module (2)");
1514
1515 /* Push arp module to ip_fd */
1516 if (ioctl (ip_fd, I_POP, NULL) < 0)
1517 syslog (LOG_ERR, "I_POP failed\n");
1518 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1519 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1520 /* Open arp_fd */
1521 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1522 if (arp_fd < 0)
1523 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1524
1525 /* Set ifname to arp */
1526 strioc_if.ic_cmd = SIOCSLIFNAME;
1527 strioc_if.ic_timout = 0;
1528 strioc_if.ic_len = sizeof(ifr);
1529 strioc_if.ic_dp = (char *)&ifr;
1530 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1531 syslog (LOG_ERR, "Can't set ifname to arp\n");
1532 }
1533
1534 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1535 syslog(LOG_ERR, "Can't link TAP device to IP");
1536 return -1;
1537 }
1538
1539 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1540 syslog (LOG_ERR, "Can't link TAP device to ARP");
1541
1542 close (if_fd);
1543
1544 memset(&ifr, 0x0, sizeof(ifr));
1545 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1546 ifr.lifr_ip_muxid = ip_muxid;
1547 ifr.lifr_arp_muxid = arp_muxid;
1548
1549 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1550 {
1551 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1552 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1553 syslog (LOG_ERR, "Can't set multiplexor id");
1554 }
1555
1556 snprintf(dev, dev_size, "tap%d", ppa);
1557 return tap_fd;
1558}
1559
1560static int tap_open(char *ifname, int ifname_size)
1561{
1562 char dev[10]="";
1563 int fd;
1564 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1565 fprintf(stderr, "Cannot allocate TAP device\n");
1566 return -1;
1567 }
1568 pstrcpy(ifname, ifname_size, dev);
1569 fcntl(fd, F_SETFL, O_NONBLOCK);
1570 return fd;
1571}
malcb29fe3e2008-11-18 01:42:22 +00001572#elif defined (_AIX)
1573static int tap_open(char *ifname, int ifname_size)
1574{
1575 fprintf (stderr, "no tap on AIX\n");
1576 return -1;
1577}
aliguori63a01ef2008-10-31 19:10:00 +00001578#else
1579static int tap_open(char *ifname, int ifname_size)
1580{
1581 struct ifreq ifr;
1582 int fd, ret;
1583
1584 TFR(fd = open("/dev/net/tun", O_RDWR));
1585 if (fd < 0) {
1586 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1587 return -1;
1588 }
1589 memset(&ifr, 0, sizeof(ifr));
1590 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1591 if (ifname[0] != '\0')
1592 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1593 else
1594 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1595 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1596 if (ret != 0) {
1597 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1598 close(fd);
1599 return -1;
1600 }
1601 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1602 fcntl(fd, F_SETFL, O_NONBLOCK);
1603 return fd;
1604}
1605#endif
1606
1607static int launch_script(const char *setup_script, const char *ifname, int fd)
1608{
Jan Kiszka7c3370d2009-05-08 12:34:17 +02001609 sigset_t oldmask, mask;
aliguori63a01ef2008-10-31 19:10:00 +00001610 int pid, status;
1611 char *args[3];
1612 char **parg;
1613
Jan Kiszka7c3370d2009-05-08 12:34:17 +02001614 sigemptyset(&mask);
1615 sigaddset(&mask, SIGCHLD);
1616 sigprocmask(SIG_BLOCK, &mask, &oldmask);
aliguori63a01ef2008-10-31 19:10:00 +00001617
Jan Kiszka7c3370d2009-05-08 12:34:17 +02001618 /* try to launch network script */
1619 pid = fork();
1620 if (pid == 0) {
1621 int open_max = sysconf(_SC_OPEN_MAX), i;
1622
1623 for (i = 0; i < open_max; i++) {
1624 if (i != STDIN_FILENO &&
1625 i != STDOUT_FILENO &&
1626 i != STDERR_FILENO &&
1627 i != fd) {
1628 close(i);
aliguori63a01ef2008-10-31 19:10:00 +00001629 }
1630 }
Jan Kiszka7c3370d2009-05-08 12:34:17 +02001631 parg = args;
1632 *parg++ = (char *)setup_script;
1633 *parg++ = (char *)ifname;
1634 *parg++ = NULL;
1635 execv(setup_script, args);
1636 _exit(1);
1637 } else if (pid > 0) {
1638 while (waitpid(pid, &status, 0) != pid) {
1639 /* loop */
1640 }
1641 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1642
1643 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1644 return 0;
1645 }
1646 }
1647 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1648 return -1;
aliguori63a01ef2008-10-31 19:10:00 +00001649}
1650
Mark McLoughlin4a77b252009-06-18 18:21:33 +01001651static TAPState *net_tap_init(VLANState *vlan, const char *model,
1652 const char *name, const char *ifname1,
1653 const char *setup_script, const char *down_script)
aliguori63a01ef2008-10-31 19:10:00 +00001654{
1655 TAPState *s;
1656 int fd;
1657 char ifname[128];
1658
1659 if (ifname1 != NULL)
1660 pstrcpy(ifname, sizeof(ifname), ifname1);
1661 else
1662 ifname[0] = '\0';
1663 TFR(fd = tap_open(ifname, sizeof(ifname)));
1664 if (fd < 0)
Mark McLoughlin4a77b252009-06-18 18:21:33 +01001665 return NULL;
aliguori63a01ef2008-10-31 19:10:00 +00001666
1667 if (!setup_script || !strcmp(setup_script, "no"))
1668 setup_script = "";
Mark McLoughlin4a77b252009-06-18 18:21:33 +01001669 if (setup_script[0] != '\0' &&
1670 launch_script(setup_script, ifname, fd)) {
1671 return NULL;
aliguori63a01ef2008-10-31 19:10:00 +00001672 }
aliguori7a9f6e42009-01-07 17:48:51 +00001673 s = net_tap_fd_init(vlan, model, name, fd);
aliguori63a01ef2008-10-31 19:10:00 +00001674 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
aliguori7cb7434b2009-01-07 17:46:21 +00001675 "ifname=%s,script=%s,downscript=%s",
1676 ifname, setup_script, down_script);
aliguori973cbd32009-01-13 19:15:55 +00001677 if (down_script && strcmp(down_script, "no")) {
aliguori63a01ef2008-10-31 19:10:00 +00001678 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
aliguori973cbd32009-01-13 19:15:55 +00001679 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1680 }
Mark McLoughlin4a77b252009-06-18 18:21:33 +01001681 return s;
aliguori63a01ef2008-10-31 19:10:00 +00001682}
1683
1684#endif /* !_WIN32 */
1685
1686#if defined(CONFIG_VDE)
1687typedef struct VDEState {
1688 VLANClientState *vc;
1689 VDECONN *vde;
1690} VDEState;
1691
1692static void vde_to_qemu(void *opaque)
1693{
1694 VDEState *s = opaque;
1695 uint8_t buf[4096];
1696 int size;
1697
Paul Brookcc63bb02009-05-03 22:40:54 +01001698 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
aliguori63a01ef2008-10-31 19:10:00 +00001699 if (size > 0) {
1700 qemu_send_packet(s->vc, buf, size);
1701 }
1702}
1703
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001704static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguori63a01ef2008-10-31 19:10:00 +00001705{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001706 VDEState *s = vc->opaque;
Anthony Liguori068daed2009-06-10 20:24:44 -05001707 ssize_t ret;
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001708
1709 do {
1710 ret = vde_send(s->vde, (const char *)buf, size, 0);
1711 } while (ret < 0 && errno == EINTR);
1712
1713 return ret;
aliguori63a01ef2008-10-31 19:10:00 +00001714}
1715
aliguorib946a152009-04-17 17:11:08 +00001716static void vde_cleanup(VLANClientState *vc)
1717{
1718 VDEState *s = vc->opaque;
1719 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1720 vde_close(s->vde);
1721 qemu_free(s);
1722}
1723
aliguori7a9f6e42009-01-07 17:48:51 +00001724static int net_vde_init(VLANState *vlan, const char *model,
1725 const char *name, const char *sock,
aliguoribf38c1a2009-01-07 17:42:25 +00001726 int port, const char *group, int mode)
aliguori63a01ef2008-10-31 19:10:00 +00001727{
1728 VDEState *s;
Mark McLoughlindd510582009-10-06 12:17:10 +01001729 char *init_group = (char *)group;
1730 char *init_sock = (char *)sock;
aliguori63a01ef2008-10-31 19:10:00 +00001731
1732 struct vde_open_args args = {
1733 .port = port,
1734 .group = init_group,
1735 .mode = mode,
1736 };
1737
1738 s = qemu_mallocz(sizeof(VDEState));
Paul Brookcc63bb02009-05-03 22:40:54 +01001739 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
aliguori63a01ef2008-10-31 19:10:00 +00001740 if (!s->vde){
1741 free(s);
1742 return -1;
1743 }
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001744 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
aliguorib946a152009-04-17 17:11:08 +00001745 NULL, vde_cleanup, s);
aliguori63a01ef2008-10-31 19:10:00 +00001746 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
aliguori7cb7434b2009-01-07 17:46:21 +00001747 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
aliguori63a01ef2008-10-31 19:10:00 +00001748 sock, vde_datafd(s->vde));
1749 return 0;
1750}
1751#endif
1752
1753/* network connection */
1754typedef struct NetSocketState {
1755 VLANClientState *vc;
1756 int fd;
1757 int state; /* 0 = getting length, 1 = getting data */
aliguoriabcd2ba2009-02-27 19:54:01 +00001758 unsigned int index;
1759 unsigned int packet_len;
aliguori63a01ef2008-10-31 19:10:00 +00001760 uint8_t buf[4096];
1761 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1762} NetSocketState;
1763
1764typedef struct NetSocketListenState {
1765 VLANState *vlan;
aliguoribf38c1a2009-01-07 17:42:25 +00001766 char *model;
aliguori7a9f6e42009-01-07 17:48:51 +00001767 char *name;
aliguori63a01ef2008-10-31 19:10:00 +00001768 int fd;
1769} NetSocketListenState;
1770
1771/* XXX: we consider we can send the whole packet without blocking */
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001772static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguori63a01ef2008-10-31 19:10:00 +00001773{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001774 NetSocketState *s = vc->opaque;
aliguori63a01ef2008-10-31 19:10:00 +00001775 uint32_t len;
1776 len = htonl(size);
1777
1778 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001779 return send_all(s->fd, buf, size);
aliguori63a01ef2008-10-31 19:10:00 +00001780}
1781
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001782static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguori63a01ef2008-10-31 19:10:00 +00001783{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01001784 NetSocketState *s = vc->opaque;
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001785
Stefan Weil70503262009-06-13 13:05:27 +02001786 return sendto(s->fd, (const void *)buf, size, 0,
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01001787 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
aliguori63a01ef2008-10-31 19:10:00 +00001788}
1789
1790static void net_socket_send(void *opaque)
1791{
1792 NetSocketState *s = opaque;
aliguoriabcd2ba2009-02-27 19:54:01 +00001793 int size, err;
1794 unsigned l;
aliguori63a01ef2008-10-31 19:10:00 +00001795 uint8_t buf1[4096];
1796 const uint8_t *buf;
1797
Blue Swirlc5b76b32009-06-13 08:44:31 +00001798 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
aliguori63a01ef2008-10-31 19:10:00 +00001799 if (size < 0) {
1800 err = socket_error();
1801 if (err != EWOULDBLOCK)
1802 goto eoc;
1803 } else if (size == 0) {
1804 /* end of connection */
1805 eoc:
1806 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1807 closesocket(s->fd);
1808 return;
1809 }
1810 buf = buf1;
1811 while (size > 0) {
1812 /* reassemble a packet from the network */
1813 switch(s->state) {
1814 case 0:
1815 l = 4 - s->index;
1816 if (l > size)
1817 l = size;
1818 memcpy(s->buf + s->index, buf, l);
1819 buf += l;
1820 size -= l;
1821 s->index += l;
1822 if (s->index == 4) {
1823 /* got length */
1824 s->packet_len = ntohl(*(uint32_t *)s->buf);
1825 s->index = 0;
1826 s->state = 1;
1827 }
1828 break;
1829 case 1:
1830 l = s->packet_len - s->index;
1831 if (l > size)
1832 l = size;
aliguoriabcd2ba2009-02-27 19:54:01 +00001833 if (s->index + l <= sizeof(s->buf)) {
1834 memcpy(s->buf + s->index, buf, l);
1835 } else {
1836 fprintf(stderr, "serious error: oversized packet received,"
1837 "connection terminated.\n");
1838 s->state = 0;
1839 goto eoc;
1840 }
1841
aliguori63a01ef2008-10-31 19:10:00 +00001842 s->index += l;
1843 buf += l;
1844 size -= l;
1845 if (s->index >= s->packet_len) {
1846 qemu_send_packet(s->vc, s->buf, s->packet_len);
1847 s->index = 0;
1848 s->state = 0;
1849 }
1850 break;
1851 }
1852 }
1853}
1854
1855static void net_socket_send_dgram(void *opaque)
1856{
1857 NetSocketState *s = opaque;
1858 int size;
1859
Blue Swirlc5b76b32009-06-13 08:44:31 +00001860 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
aliguori63a01ef2008-10-31 19:10:00 +00001861 if (size < 0)
1862 return;
1863 if (size == 0) {
1864 /* end of connection */
1865 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1866 return;
1867 }
1868 qemu_send_packet(s->vc, s->buf, size);
1869}
1870
1871static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1872{
1873 struct ip_mreq imr;
1874 int fd;
1875 int val, ret;
1876 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1877 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1878 inet_ntoa(mcastaddr->sin_addr),
1879 (int)ntohl(mcastaddr->sin_addr.s_addr));
1880 return -1;
1881
1882 }
1883 fd = socket(PF_INET, SOCK_DGRAM, 0);
1884 if (fd < 0) {
1885 perror("socket(PF_INET, SOCK_DGRAM)");
1886 return -1;
1887 }
1888
1889 val = 1;
1890 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1891 (const char *)&val, sizeof(val));
1892 if (ret < 0) {
1893 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1894 goto fail;
1895 }
1896
1897 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1898 if (ret < 0) {
1899 perror("bind");
1900 goto fail;
1901 }
1902
1903 /* Add host to multicast group */
1904 imr.imr_multiaddr = mcastaddr->sin_addr;
1905 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1906
1907 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1908 (const char *)&imr, sizeof(struct ip_mreq));
1909 if (ret < 0) {
1910 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1911 goto fail;
1912 }
1913
1914 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1915 val = 1;
1916 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1917 (const char *)&val, sizeof(val));
1918 if (ret < 0) {
1919 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1920 goto fail;
1921 }
1922
1923 socket_set_nonblock(fd);
1924 return fd;
1925fail:
1926 if (fd >= 0)
1927 closesocket(fd);
1928 return -1;
1929}
1930
aliguorib946a152009-04-17 17:11:08 +00001931static void net_socket_cleanup(VLANClientState *vc)
1932{
1933 NetSocketState *s = vc->opaque;
1934 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1935 close(s->fd);
1936 qemu_free(s);
1937}
1938
aliguori7a9f6e42009-01-07 17:48:51 +00001939static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1940 const char *model,
1941 const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00001942 int fd, int is_connected)
aliguori63a01ef2008-10-31 19:10:00 +00001943{
1944 struct sockaddr_in saddr;
1945 int newfd;
1946 socklen_t saddr_len;
1947 NetSocketState *s;
1948
1949 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1950 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1951 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1952 */
1953
1954 if (is_connected) {
1955 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1956 /* must be bound */
1957 if (saddr.sin_addr.s_addr==0) {
1958 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1959 fd);
1960 return NULL;
1961 }
1962 /* clone dgram socket */
1963 newfd = net_socket_mcast_create(&saddr);
1964 if (newfd < 0) {
1965 /* error already reported by net_socket_mcast_create() */
1966 close(fd);
1967 return NULL;
1968 }
1969 /* clone newfd to fd, close newfd */
1970 dup2(newfd, fd);
1971 close(newfd);
1972
1973 } else {
1974 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1975 fd, strerror(errno));
1976 return NULL;
1977 }
1978 }
1979
1980 s = qemu_mallocz(sizeof(NetSocketState));
aliguori63a01ef2008-10-31 19:10:00 +00001981 s->fd = fd;
1982
Mark McLoughlin463af532009-05-18 12:55:27 +01001983 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
aliguorib946a152009-04-17 17:11:08 +00001984 NULL, net_socket_cleanup, s);
aliguori63a01ef2008-10-31 19:10:00 +00001985 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1986
1987 /* mcast: save bound address as dst */
1988 if (is_connected) s->dgram_dst=saddr;
1989
1990 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1991 "socket: fd=%d (%s mcast=%s:%d)",
1992 fd, is_connected? "cloned" : "",
1993 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1994 return s;
1995}
1996
1997static void net_socket_connect(void *opaque)
1998{
1999 NetSocketState *s = opaque;
2000 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
2001}
2002
aliguori7a9f6e42009-01-07 17:48:51 +00002003static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
2004 const char *model,
2005 const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00002006 int fd, int is_connected)
aliguori63a01ef2008-10-31 19:10:00 +00002007{
2008 NetSocketState *s;
2009 s = qemu_mallocz(sizeof(NetSocketState));
aliguori63a01ef2008-10-31 19:10:00 +00002010 s->fd = fd;
Mark McLoughlin463af532009-05-18 12:55:27 +01002011 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
aliguorib946a152009-04-17 17:11:08 +00002012 NULL, net_socket_cleanup, s);
aliguori63a01ef2008-10-31 19:10:00 +00002013 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2014 "socket: fd=%d", fd);
2015 if (is_connected) {
2016 net_socket_connect(s);
2017 } else {
2018 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
2019 }
2020 return s;
2021}
2022
aliguori7a9f6e42009-01-07 17:48:51 +00002023static NetSocketState *net_socket_fd_init(VLANState *vlan,
2024 const char *model, const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00002025 int fd, int is_connected)
aliguori63a01ef2008-10-31 19:10:00 +00002026{
Michael S. Tsirkinacedcfb2009-09-30 18:56:44 +00002027 int so_type = -1, optlen=sizeof(so_type);
aliguori63a01ef2008-10-31 19:10:00 +00002028
2029 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
2030 (socklen_t *)&optlen)< 0) {
2031 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
2032 return NULL;
2033 }
2034 switch(so_type) {
2035 case SOCK_DGRAM:
aliguori7a9f6e42009-01-07 17:48:51 +00002036 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
aliguori63a01ef2008-10-31 19:10:00 +00002037 case SOCK_STREAM:
aliguori7a9f6e42009-01-07 17:48:51 +00002038 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
aliguori63a01ef2008-10-31 19:10:00 +00002039 default:
2040 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2041 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
aliguori7a9f6e42009-01-07 17:48:51 +00002042 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
aliguori63a01ef2008-10-31 19:10:00 +00002043 }
2044 return NULL;
2045}
2046
2047static void net_socket_accept(void *opaque)
2048{
2049 NetSocketListenState *s = opaque;
2050 NetSocketState *s1;
2051 struct sockaddr_in saddr;
2052 socklen_t len;
2053 int fd;
2054
2055 for(;;) {
2056 len = sizeof(saddr);
2057 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
2058 if (fd < 0 && errno != EINTR) {
2059 return;
2060 } else if (fd >= 0) {
2061 break;
2062 }
2063 }
aliguori7a9f6e42009-01-07 17:48:51 +00002064 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
aliguori63a01ef2008-10-31 19:10:00 +00002065 if (!s1) {
2066 closesocket(fd);
2067 } else {
2068 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
2069 "socket: connection from %s:%d",
2070 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2071 }
2072}
2073
aliguori7a9f6e42009-01-07 17:48:51 +00002074static int net_socket_listen_init(VLANState *vlan,
2075 const char *model,
2076 const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00002077 const char *host_str)
aliguori63a01ef2008-10-31 19:10:00 +00002078{
2079 NetSocketListenState *s;
2080 int fd, val, ret;
2081 struct sockaddr_in saddr;
2082
2083 if (parse_host_port(&saddr, host_str) < 0)
2084 return -1;
2085
2086 s = qemu_mallocz(sizeof(NetSocketListenState));
aliguori63a01ef2008-10-31 19:10:00 +00002087
2088 fd = socket(PF_INET, SOCK_STREAM, 0);
2089 if (fd < 0) {
2090 perror("socket");
2091 return -1;
2092 }
2093 socket_set_nonblock(fd);
2094
2095 /* allow fast reuse */
2096 val = 1;
2097 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2098
2099 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2100 if (ret < 0) {
2101 perror("bind");
2102 return -1;
2103 }
2104 ret = listen(fd, 0);
2105 if (ret < 0) {
2106 perror("listen");
2107 return -1;
2108 }
2109 s->vlan = vlan;
Mark McLoughlin02374aa2009-10-06 12:16:55 +01002110 s->model = qemu_strdup(model);
2111 s->name = name ? qemu_strdup(name) : NULL;
aliguori63a01ef2008-10-31 19:10:00 +00002112 s->fd = fd;
2113 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
2114 return 0;
2115}
2116
aliguori7a9f6e42009-01-07 17:48:51 +00002117static int net_socket_connect_init(VLANState *vlan,
2118 const char *model,
2119 const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00002120 const char *host_str)
aliguori63a01ef2008-10-31 19:10:00 +00002121{
2122 NetSocketState *s;
2123 int fd, connected, ret, err;
2124 struct sockaddr_in saddr;
2125
2126 if (parse_host_port(&saddr, host_str) < 0)
2127 return -1;
2128
2129 fd = socket(PF_INET, SOCK_STREAM, 0);
2130 if (fd < 0) {
2131 perror("socket");
2132 return -1;
2133 }
2134 socket_set_nonblock(fd);
2135
2136 connected = 0;
2137 for(;;) {
2138 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2139 if (ret < 0) {
2140 err = socket_error();
2141 if (err == EINTR || err == EWOULDBLOCK) {
2142 } else if (err == EINPROGRESS) {
2143 break;
2144#ifdef _WIN32
2145 } else if (err == WSAEALREADY) {
2146 break;
2147#endif
2148 } else {
2149 perror("connect");
2150 closesocket(fd);
2151 return -1;
2152 }
2153 } else {
2154 connected = 1;
2155 break;
2156 }
2157 }
aliguori7a9f6e42009-01-07 17:48:51 +00002158 s = net_socket_fd_init(vlan, model, name, fd, connected);
aliguori63a01ef2008-10-31 19:10:00 +00002159 if (!s)
2160 return -1;
2161 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2162 "socket: connect to %s:%d",
2163 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2164 return 0;
2165}
2166
aliguori7a9f6e42009-01-07 17:48:51 +00002167static int net_socket_mcast_init(VLANState *vlan,
2168 const char *model,
2169 const char *name,
aliguoribf38c1a2009-01-07 17:42:25 +00002170 const char *host_str)
aliguori63a01ef2008-10-31 19:10:00 +00002171{
2172 NetSocketState *s;
2173 int fd;
2174 struct sockaddr_in saddr;
2175
2176 if (parse_host_port(&saddr, host_str) < 0)
2177 return -1;
2178
2179
2180 fd = net_socket_mcast_create(&saddr);
2181 if (fd < 0)
2182 return -1;
2183
aliguori7a9f6e42009-01-07 17:48:51 +00002184 s = net_socket_fd_init(vlan, model, name, fd, 0);
aliguori63a01ef2008-10-31 19:10:00 +00002185 if (!s)
2186 return -1;
2187
2188 s->dgram_dst = saddr;
2189
2190 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2191 "socket: mcast=%s:%d",
2192 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2193 return 0;
2194
2195}
2196
aliguoribb9ea792009-04-21 19:56:28 +00002197typedef struct DumpState {
2198 VLANClientState *pcap_vc;
2199 int fd;
2200 int pcap_caplen;
2201} DumpState;
2202
2203#define PCAP_MAGIC 0xa1b2c3d4
2204
2205struct pcap_file_hdr {
2206 uint32_t magic;
2207 uint16_t version_major;
2208 uint16_t version_minor;
2209 int32_t thiszone;
2210 uint32_t sigfigs;
2211 uint32_t snaplen;
2212 uint32_t linktype;
2213};
2214
2215struct pcap_sf_pkthdr {
2216 struct {
2217 int32_t tv_sec;
2218 int32_t tv_usec;
2219 } ts;
2220 uint32_t caplen;
2221 uint32_t len;
2222};
2223
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01002224static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguoribb9ea792009-04-21 19:56:28 +00002225{
Mark McLoughline3f5ec22009-05-18 13:33:03 +01002226 DumpState *s = vc->opaque;
aliguoribb9ea792009-04-21 19:56:28 +00002227 struct pcap_sf_pkthdr hdr;
2228 int64_t ts;
2229 int caplen;
2230
2231 /* Early return in case of previous error. */
2232 if (s->fd < 0) {
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01002233 return size;
aliguoribb9ea792009-04-21 19:56:28 +00002234 }
2235
Juan Quintela6ee093c2009-09-10 03:04:26 +02002236 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
aliguoribb9ea792009-04-21 19:56:28 +00002237 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2238
Jan Kiszka37cb6fc2009-05-10 22:23:23 +02002239 hdr.ts.tv_sec = ts / 1000000;
aliguoribb9ea792009-04-21 19:56:28 +00002240 hdr.ts.tv_usec = ts % 1000000;
2241 hdr.caplen = caplen;
2242 hdr.len = size;
2243 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2244 write(s->fd, buf, caplen) != caplen) {
2245 qemu_log("-net dump write error - stop dump\n");
2246 close(s->fd);
2247 s->fd = -1;
2248 }
Mark McLoughlin4f1c9422009-05-18 13:40:55 +01002249
2250 return size;
aliguoribb9ea792009-04-21 19:56:28 +00002251}
2252
2253static void net_dump_cleanup(VLANClientState *vc)
2254{
2255 DumpState *s = vc->opaque;
2256
2257 close(s->fd);
2258 qemu_free(s);
2259}
2260
Markus Armbrusterfb125772009-10-06 12:16:58 +01002261static int net_dump_init(VLANState *vlan, const char *device,
aliguoribb9ea792009-04-21 19:56:28 +00002262 const char *name, const char *filename, int len)
2263{
2264 struct pcap_file_hdr hdr;
2265 DumpState *s;
2266
2267 s = qemu_malloc(sizeof(DumpState));
2268
Filip Navara024431b2009-06-17 19:48:08 +02002269 s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
aliguoribb9ea792009-04-21 19:56:28 +00002270 if (s->fd < 0) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01002271 qemu_error("-net dump: can't open %s\n", filename);
aliguoribb9ea792009-04-21 19:56:28 +00002272 return -1;
2273 }
2274
2275 s->pcap_caplen = len;
2276
2277 hdr.magic = PCAP_MAGIC;
2278 hdr.version_major = 2;
2279 hdr.version_minor = 4;
2280 hdr.thiszone = 0;
2281 hdr.sigfigs = 0;
2282 hdr.snaplen = s->pcap_caplen;
2283 hdr.linktype = 1;
2284
2285 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01002286 qemu_error("-net dump write error: %s\n", strerror(errno));
aliguoribb9ea792009-04-21 19:56:28 +00002287 close(s->fd);
2288 qemu_free(s);
2289 return -1;
2290 }
2291
Mark McLoughlin463af532009-05-18 12:55:27 +01002292 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
aliguoribb9ea792009-04-21 19:56:28 +00002293 net_dump_cleanup, s);
2294 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2295 "dump to %s (len=%d)", filename, len);
2296 return 0;
2297}
2298
aliguori63a01ef2008-10-31 19:10:00 +00002299/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +02002300VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +00002301{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002302 VLANState *vlan;
2303
2304 QTAILQ_FOREACH(vlan, &vlans, next) {
2305 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +00002306 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002307 }
aliguori63a01ef2008-10-31 19:10:00 +00002308 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002309
Jan Kiszka1a609522009-06-24 14:42:31 +02002310 if (!allocate) {
2311 return NULL;
2312 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002313
aliguori63a01ef2008-10-31 19:10:00 +00002314 vlan = qemu_mallocz(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +00002315 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002316 QTAILQ_INIT(&vlan->clients);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002317 QTAILQ_INIT(&vlan->send_queue);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01002318
2319 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
2320
aliguori63a01ef2008-10-31 19:10:00 +00002321 return vlan;
2322}
2323
aliguori76970792009-02-11 15:20:03 +00002324static int nic_get_free_idx(void)
2325{
2326 int index;
2327
2328 for (index = 0; index < MAX_NICS; index++)
2329 if (!nd_table[index].used)
2330 return index;
2331 return -1;
2332}
2333
Markus Armbruster07caea32009-09-25 03:53:51 +02002334int qemu_show_nic_models(const char *arg, const char *const *models)
2335{
2336 int i;
2337
2338 if (!arg || strcmp(arg, "?"))
2339 return 0;
2340
2341 fprintf(stderr, "qemu: Supported NIC models: ");
2342 for (i = 0 ; models[i]; i++)
2343 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2344 return 1;
2345}
2346
aliguorid07f22c2009-01-13 19:03:57 +00002347void qemu_check_nic_model(NICInfo *nd, const char *model)
2348{
2349 const char *models[2];
2350
2351 models[0] = model;
2352 models[1] = NULL;
2353
Markus Armbruster07caea32009-09-25 03:53:51 +02002354 if (qemu_show_nic_models(nd->model, models))
2355 exit(0);
2356 if (qemu_find_nic_model(nd, models, model) < 0)
2357 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +00002358}
2359
Markus Armbruster07caea32009-09-25 03:53:51 +02002360int qemu_find_nic_model(NICInfo *nd, const char * const *models,
2361 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +00002362{
Markus Armbruster07caea32009-09-25 03:53:51 +02002363 int i;
aliguorid07f22c2009-01-13 19:03:57 +00002364
2365 if (!nd->model)
Mark McLoughlin32a8e142009-10-06 12:16:51 +01002366 nd->model = qemu_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +00002367
Markus Armbruster07caea32009-09-25 03:53:51 +02002368 for (i = 0 ; models[i]; i++) {
2369 if (strcmp(nd->model, models[i]) == 0)
2370 return i;
aliguorid07f22c2009-01-13 19:03:57 +00002371 }
2372
Markus Armbruster07caea32009-09-25 03:53:51 +02002373 qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
2374 return -1;
aliguorid07f22c2009-01-13 19:03:57 +00002375}
2376
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +01002377static int net_handle_fd_param(Monitor *mon, const char *param)
2378{
2379 if (!qemu_isdigit(param[0])) {
2380 int fd;
2381
2382 fd = monitor_get_fd(mon, param);
2383 if (fd == -1) {
Markus Armbrusterfb125772009-10-06 12:16:58 +01002384 qemu_error("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +01002385 return -1;
2386 }
2387
2388 return fd;
2389 } else {
2390 return strtol(param, NULL, 0);
2391 }
2392}
2393
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002394static int net_init_nic(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002395{
2396 int idx;
2397 NICInfo *nd;
2398
2399 idx = nic_get_free_idx();
2400 if (idx == -1 || nb_nics >= MAX_NICS) {
2401 qemu_error("Too Many NICs\n");
2402 return -1;
2403 }
2404
2405 nd = &nd_table[idx];
2406
2407 memset(nd, 0, sizeof(*nd));
2408
2409 nd->vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2410
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002411 if (name) {
2412 nd->name = qemu_strdup(name);
2413 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002414 if (qemu_opt_get(opts, "model")) {
2415 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
2416 }
2417 if (qemu_opt_get(opts, "addr")) {
2418 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
2419 }
2420
2421 nd->macaddr[0] = 0x52;
2422 nd->macaddr[1] = 0x54;
2423 nd->macaddr[2] = 0x00;
2424 nd->macaddr[3] = 0x12;
2425 nd->macaddr[4] = 0x34;
2426 nd->macaddr[5] = 0x56 + idx;
2427
2428 if (qemu_opt_get(opts, "macaddr") &&
2429 parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
2430 qemu_error("invalid syntax for ethernet address\n");
2431 return -1;
2432 }
2433
2434 nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
2435 if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
2436 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
2437 qemu_error("invalid # of vectors: %d\n", nd->nvectors);
2438 return -1;
2439 }
2440
2441 nd->used = 1;
2442 nd->vlan->nb_guest_devs++;
2443 nb_nics++;
2444
2445 return idx;
2446}
2447
Juan Quintelaa3a766e2009-10-07 23:44:15 +02002448#if defined(CONFIG_SLIRP)
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002449static int net_init_slirp_configs(const char *name, const char *value, void *opaque)
2450{
2451 struct slirp_config_str *config;
2452
2453 if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) {
2454 return 0;
2455 }
2456
2457 config = qemu_mallocz(sizeof(*config));
2458
2459 pstrcpy(config->str, sizeof(config->str), value);
2460
2461 if (!strcmp(name, "hostfwd")) {
2462 config->flags = SLIRP_CFG_HOSTFWD;
2463 }
2464
2465 config->next = slirp_configs;
2466 slirp_configs = config;
2467
2468 return 0;
2469}
2470
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002471static int net_init_slirp(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002472{
2473 VLANState *vlan;
2474 struct slirp_config_str *config;
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002475 const char *vhost;
2476 const char *vhostname;
2477 const char *vdhcp_start;
2478 const char *vnamesrv;
2479 const char *tftp_export;
2480 const char *bootfile;
2481 const char *smb_export;
2482 const char *vsmbsrv;
2483 char *vnet = NULL;
2484 int restricted = 0;
2485 int ret;
2486
2487 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2488
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002489 vhost = qemu_opt_get(opts, "host");
2490 vhostname = qemu_opt_get(opts, "hostname");
2491 vdhcp_start = qemu_opt_get(opts, "dhcpstart");
2492 vnamesrv = qemu_opt_get(opts, "dns");
2493 tftp_export = qemu_opt_get(opts, "tftp");
2494 bootfile = qemu_opt_get(opts, "bootfile");
2495 smb_export = qemu_opt_get(opts, "smb");
2496 vsmbsrv = qemu_opt_get(opts, "smbserver");
2497
2498 if (qemu_opt_get(opts, "ip")) {
2499 const char *ip = qemu_opt_get(opts, "ip");
2500 int l = strlen(ip) + strlen("/24") + 1;
2501
2502 vnet = qemu_malloc(l);
2503
2504 /* emulate legacy ip= parameter */
2505 pstrcpy(vnet, l, ip);
2506 pstrcat(vnet, l, "/24");
2507 }
2508
2509 if (qemu_opt_get(opts, "net")) {
2510 if (vnet) {
2511 qemu_free(vnet);
2512 }
2513 vnet = qemu_strdup(qemu_opt_get(opts, "net"));
2514 }
2515
2516 if (qemu_opt_get(opts, "restrict") &&
2517 qemu_opt_get(opts, "restrict")[0] == 'y') {
2518 restricted = 1;
2519 }
2520
2521 qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0);
2522
2523 ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost,
2524 vhostname, tftp_export, bootfile, vdhcp_start,
2525 vnamesrv, smb_export, vsmbsrv);
2526
2527 while (slirp_configs) {
2528 config = slirp_configs;
2529 slirp_configs = config->next;
2530 qemu_free(config);
2531 }
2532
2533 if (ret != -1) {
2534 vlan->nb_host_devs++;
2535 }
2536
2537 qemu_free(vnet);
2538
2539 return ret;
2540}
Juan Quintelaa3a766e2009-10-07 23:44:15 +02002541#endif /* CONFIG_SLIRP */
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002542
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002543#ifdef _WIN32
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002544static int net_init_tap_win32(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002545{
2546 VLANState *vlan;
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002547 const char *ifname;
2548
2549 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2550
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002551 ifname = qemu_opt_get(opts, "ifname");
2552
2553 if (!ifname) {
2554 qemu_error("tap: no interface name\n");
2555 return -1;
2556 }
2557
2558 if (tap_win32_init(vlan, "tap", name, ifname) == -1) {
2559 return -1;
2560 }
2561
2562 vlan->nb_host_devs++;
2563
2564 return 0;
2565}
2566#elif !defined(_AIX)
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002567static int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002568{
2569 VLANState *vlan;
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002570 TAPState *s;
2571
2572 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2573
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002574 if (qemu_opt_get(opts, "fd")) {
2575 int fd;
2576
2577 if (qemu_opt_get(opts, "ifname") ||
2578 qemu_opt_get(opts, "script") ||
2579 qemu_opt_get(opts, "downscript")) {
2580 qemu_error("ifname=, script= and downscript= is invalid with fd=\n");
2581 return -1;
2582 }
2583
2584 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
2585 if (fd == -1) {
2586 return -1;
2587 }
2588
2589 fcntl(fd, F_SETFL, O_NONBLOCK);
2590
2591 s = net_tap_fd_init(vlan, "tap", name, fd);
2592 if (!s) {
2593 close(fd);
2594 }
2595 } else {
2596 const char *ifname, *script, *downscript;
2597
2598 ifname = qemu_opt_get(opts, "ifname");
2599 script = qemu_opt_get(opts, "script");
2600 downscript = qemu_opt_get(opts, "downscript");
2601
2602 if (!script) {
2603 script = DEFAULT_NETWORK_SCRIPT;
2604 }
2605 if (!downscript) {
2606 downscript = DEFAULT_NETWORK_DOWN_SCRIPT;
2607 }
2608
2609 s = net_tap_init(vlan, "tap", name, ifname, script, downscript);
2610 }
2611
2612 if (!s) {
2613 return -1;
2614 }
2615
2616 if (tap_set_sndbuf(s, opts) < 0) {
2617 return -1;
2618 }
2619
2620 vlan->nb_host_devs++;
2621
2622 return 0;
2623}
2624#endif
2625
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002626static int net_init_socket(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01002627{
2628 VLANState *vlan;
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01002629
2630 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2631
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01002632 if (qemu_opt_get(opts, "fd")) {
2633 int fd;
2634
2635 if (qemu_opt_get(opts, "listen") ||
2636 qemu_opt_get(opts, "connect") ||
2637 qemu_opt_get(opts, "mcast")) {
2638 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
2639 return -1;
2640 }
2641
2642 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
2643 if (fd == -1) {
2644 return -1;
2645 }
2646
2647 if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
2648 close(fd);
2649 return -1;
2650 }
2651 } else if (qemu_opt_get(opts, "listen")) {
2652 const char *listen;
2653
2654 if (qemu_opt_get(opts, "fd") ||
2655 qemu_opt_get(opts, "connect") ||
2656 qemu_opt_get(opts, "mcast")) {
2657 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
2658 return -1;
2659 }
2660
2661 listen = qemu_opt_get(opts, "listen");
2662
2663 if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
2664 return -1;
2665 }
2666 } else if (qemu_opt_get(opts, "connect")) {
2667 const char *connect;
2668
2669 if (qemu_opt_get(opts, "fd") ||
2670 qemu_opt_get(opts, "listen") ||
2671 qemu_opt_get(opts, "mcast")) {
2672 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
2673 return -1;
2674 }
2675
2676 connect = qemu_opt_get(opts, "connect");
2677
2678 if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
2679 return -1;
2680 }
2681 } else if (qemu_opt_get(opts, "mcast")) {
2682 const char *mcast;
2683
2684 if (qemu_opt_get(opts, "fd") ||
2685 qemu_opt_get(opts, "connect") ||
2686 qemu_opt_get(opts, "listen")) {
2687 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
2688 return -1;
2689 }
2690
2691 mcast = qemu_opt_get(opts, "mcast");
2692
2693 if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) {
2694 return -1;
2695 }
2696 } else {
2697 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
2698 return -1;
2699 }
2700
2701 vlan->nb_host_devs++;
2702
2703 return 0;
2704}
2705
Mark McLoughlindd510582009-10-06 12:17:10 +01002706#ifdef CONFIG_VDE
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002707static int net_init_vde(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlindd510582009-10-06 12:17:10 +01002708{
2709 VLANState *vlan;
Mark McLoughlindd510582009-10-06 12:17:10 +01002710 const char *sock;
2711 const char *group;
2712 int port, mode;
2713
2714 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2715
Mark McLoughlindd510582009-10-06 12:17:10 +01002716 sock = qemu_opt_get(opts, "sock");
2717 group = qemu_opt_get(opts, "group");
2718
2719 port = qemu_opt_get_number(opts, "port", 0);
2720 mode = qemu_opt_get_number(opts, "mode", 0700);
2721
2722 if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) {
2723 return -1;
2724 }
2725
2726 vlan->nb_host_devs++;
2727
2728 return 0;
2729}
2730#endif
2731
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002732static int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name)
Mark McLoughlined2955c2009-10-06 12:17:11 +01002733{
2734 VLANState *vlan;
2735 int len;
2736 const char *file;
Mark McLoughlined2955c2009-10-06 12:17:11 +01002737 char def_file[128];
2738
2739 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
2740
Mark McLoughlined2955c2009-10-06 12:17:11 +01002741 file = qemu_opt_get(opts, "file");
2742 if (!file) {
2743 snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id);
2744 file = def_file;
2745 }
2746
2747 len = qemu_opt_get_size(opts, "len", 65536);
2748
2749 return net_dump_init(vlan, "dump", name, file, len);
2750}
2751
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002752#define NET_COMMON_PARAMS_DESC \
2753 { \
2754 .name = "type", \
2755 .type = QEMU_OPT_STRING, \
2756 .help = "net client type (nic, tap etc.)", \
2757 }, { \
2758 .name = "vlan", \
2759 .type = QEMU_OPT_NUMBER, \
2760 .help = "vlan number", \
2761 }, { \
2762 .name = "name", \
2763 .type = QEMU_OPT_STRING, \
2764 .help = "identifier for monitor commands", \
2765 }
2766
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002767typedef int (*net_client_init_func)(QemuOpts *opts,
2768 Monitor *mon,
2769 const char *name);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002770
2771/* magic number, but compiler will warn if too small */
2772#define NET_MAX_DESC 20
2773
2774static struct {
2775 const char *type;
2776 net_client_init_func init;
2777 QemuOptDesc desc[NET_MAX_DESC];
2778} net_client_types[] = {
2779 {
2780 .type = "none",
2781 .desc = {
2782 NET_COMMON_PARAMS_DESC,
2783 { /* end of list */ }
2784 },
2785 }, {
2786 .type = "nic",
2787 .init = net_init_nic,
2788 .desc = {
2789 NET_COMMON_PARAMS_DESC,
2790 {
2791 .name = "macaddr",
2792 .type = QEMU_OPT_STRING,
2793 .help = "MAC address",
2794 }, {
2795 .name = "model",
2796 .type = QEMU_OPT_STRING,
2797 .help = "device model (e1000, rtl8139, virtio etc.)",
2798 }, {
2799 .name = "addr",
2800 .type = QEMU_OPT_STRING,
2801 .help = "PCI device address",
2802 }, {
2803 .name = "vectors",
2804 .type = QEMU_OPT_NUMBER,
2805 .help = "number of MSI-x vectors, 0 to disable MSI-X",
2806 },
2807 { /* end of list */ }
2808 },
Mark McLoughlinec302ff2009-10-06 12:17:07 +01002809#ifdef CONFIG_SLIRP
2810 }, {
2811 .type = "user",
2812 .init = net_init_slirp,
2813 .desc = {
2814 NET_COMMON_PARAMS_DESC,
2815 {
2816 .name = "hostname",
2817 .type = QEMU_OPT_STRING,
2818 .help = "client hostname reported by the builtin DHCP server",
2819 }, {
2820 .name = "restrict",
2821 .type = QEMU_OPT_STRING,
2822 .help = "isolate the guest from the host (y|yes|n|no)",
2823 }, {
2824 .name = "ip",
2825 .type = QEMU_OPT_STRING,
2826 .help = "legacy parameter, use net= instead",
2827 }, {
2828 .name = "net",
2829 .type = QEMU_OPT_STRING,
2830 .help = "IP address and optional netmask",
2831 }, {
2832 .name = "host",
2833 .type = QEMU_OPT_STRING,
2834 .help = "guest-visible address of the host",
2835 }, {
2836 .name = "tftp",
2837 .type = QEMU_OPT_STRING,
2838 .help = "root directory of the built-in TFTP server",
2839 }, {
2840 .name = "bootfile",
2841 .type = QEMU_OPT_STRING,
2842 .help = "BOOTP filename, for use with tftp=",
2843 }, {
2844 .name = "dhcpstart",
2845 .type = QEMU_OPT_STRING,
2846 .help = "the first of the 16 IPs the built-in DHCP server can assign",
2847 }, {
2848 .name = "dns",
2849 .type = QEMU_OPT_STRING,
2850 .help = "guest-visible address of the virtual nameserver",
2851 }, {
2852 .name = "smb",
2853 .type = QEMU_OPT_STRING,
2854 .help = "root directory of the built-in SMB server",
2855 }, {
2856 .name = "smbserver",
2857 .type = QEMU_OPT_STRING,
2858 .help = "IP address of the built-in SMB server",
2859 }, {
2860 .name = "hostfwd",
2861 .type = QEMU_OPT_STRING,
2862 .help = "guest port number to forward incoming TCP or UDP connections",
2863 }, {
2864 .name = "guestfwd",
2865 .type = QEMU_OPT_STRING,
2866 .help = "IP address and port to forward guest TCP connections",
2867 },
2868 { /* end of list */ }
2869 },
2870#endif
Mark McLoughlin8a1c5232009-10-06 12:17:08 +01002871#ifdef _WIN32
2872 }, {
2873 .type = "tap",
2874 .init = net_init_tap_win32,
2875 .desc = {
2876 NET_COMMON_PARAMS_DESC,
2877 {
2878 .name = "ifname",
2879 .type = QEMU_OPT_STRING,
2880 .help = "interface name",
2881 },
2882 { /* end of list */ }
2883 },
2884#elif !defined(_AIX)
2885 }, {
2886 .type = "tap",
2887 .init = net_init_tap,
2888 .desc = {
2889 NET_COMMON_PARAMS_DESC,
2890 {
2891 .name = "fd",
2892 .type = QEMU_OPT_STRING,
2893 .help = "file descriptor of an already opened tap",
2894 }, {
2895 .name = "ifname",
2896 .type = QEMU_OPT_STRING,
2897 .help = "interface name",
2898 }, {
2899 .name = "script",
2900 .type = QEMU_OPT_STRING,
2901 .help = "script to initialize the interface",
2902 }, {
2903 .name = "downscript",
2904 .type = QEMU_OPT_STRING,
2905 .help = "script to shut down the interface",
2906#ifdef TUNSETSNDBUF
2907 }, {
2908 .name = "sndbuf",
2909 .type = QEMU_OPT_SIZE,
2910 .help = "send buffer limit"
2911#endif
2912 },
2913 { /* end of list */ }
2914 },
2915#endif
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01002916 }, {
2917 .type = "socket",
2918 .init = net_init_socket,
2919 .desc = {
2920 NET_COMMON_PARAMS_DESC,
2921 {
2922 .name = "fd",
2923 .type = QEMU_OPT_STRING,
2924 .help = "file descriptor of an already opened socket",
2925 }, {
2926 .name = "listen",
2927 .type = QEMU_OPT_STRING,
2928 .help = "port number, and optional hostname, to listen on",
2929 }, {
2930 .name = "connect",
2931 .type = QEMU_OPT_STRING,
2932 .help = "port number, and optional hostname, to connect to",
2933 }, {
2934 .name = "mcast",
2935 .type = QEMU_OPT_STRING,
2936 .help = "UDP multicast address and port number",
2937 },
2938 { /* end of list */ }
2939 },
Mark McLoughlindd510582009-10-06 12:17:10 +01002940#ifdef CONFIG_VDE
2941 }, {
2942 .type = "vde",
2943 .init = net_init_vde,
2944 .desc = {
2945 NET_COMMON_PARAMS_DESC,
2946 {
2947 .name = "sock",
2948 .type = QEMU_OPT_STRING,
2949 .help = "socket path",
2950 }, {
2951 .name = "port",
2952 .type = QEMU_OPT_NUMBER,
2953 .help = "port number",
2954 }, {
2955 .name = "group",
2956 .type = QEMU_OPT_STRING,
2957 .help = "group owner of socket",
2958 }, {
2959 .name = "mode",
2960 .type = QEMU_OPT_NUMBER,
2961 .help = "permissions for socket",
2962 },
2963 { /* end of list */ }
2964 },
2965#endif
Mark McLoughlined2955c2009-10-06 12:17:11 +01002966 }, {
2967 .type = "dump",
2968 .init = net_init_dump,
2969 .desc = {
2970 NET_COMMON_PARAMS_DESC,
2971 {
2972 .name = "len",
2973 .type = QEMU_OPT_SIZE,
2974 .help = "per-packet size limit (64k default)",
2975 }, {
2976 .name = "file",
2977 .type = QEMU_OPT_STRING,
2978 .help = "dump file path (default is qemu-vlan0.pcap)",
2979 },
2980 { /* end of list */ }
2981 },
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002982 },
2983 { /* end of list */ }
2984};
2985
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01002986int net_client_init(Monitor *mon, QemuOpts *opts)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002987{
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002988 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01002989 const char *type;
2990 int i;
2991
2992 type = qemu_opt_get(opts, "type");
2993 if (!type) {
2994 qemu_error("No type specified for -net\n");
2995 return -1;
2996 }
2997
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01002998 name = qemu_opts_id(opts);
2999 if (!name) {
3000 name = qemu_opt_get(opts, "name");
3001 }
3002
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01003003 for (i = 0; net_client_types[i].type != NULL; i++) {
3004 if (!strcmp(net_client_types[i].type, type)) {
3005 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
3006 return -1;
3007 }
3008
3009 if (net_client_types[i].init) {
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01003010 return net_client_types[i].init(opts, mon, name);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01003011 } else {
3012 return 0;
3013 }
3014 }
3015 }
3016
3017 qemu_error("Invalid -net type '%s'\n", type);
3018 return -1;
3019}
3020
aliguori8b13c4a2009-02-11 15:20:51 +00003021void net_client_uninit(NICInfo *nd)
3022{
3023 nd->vlan->nb_guest_devs--;
3024 nb_nics--;
Glauber Costaa9796702009-09-17 16:53:39 -04003025
Mark McLoughlin9203f522009-10-06 12:16:53 +01003026 qemu_free(nd->model);
3027 qemu_free(nd->name);
3028 qemu_free(nd->devaddr);
Glauber Costaa9796702009-09-17 16:53:39 -04003029
Mark McLoughlind2cffe32009-10-06 12:16:54 +01003030 nd->used = 0;
aliguori8b13c4a2009-02-11 15:20:51 +00003031}
3032
aliguori6f338c32009-02-11 15:21:54 +00003033static int net_host_check_device(const char *device)
3034{
3035 int i;
aliguoribb9ea792009-04-21 19:56:28 +00003036 const char *valid_param_list[] = { "tap", "socket", "dump"
aliguori6f338c32009-02-11 15:21:54 +00003037#ifdef CONFIG_SLIRP
3038 ,"user"
3039#endif
3040#ifdef CONFIG_VDE
3041 ,"vde"
3042#endif
3043 };
3044 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
3045 if (!strncmp(valid_param_list[i], device,
3046 strlen(valid_param_list[i])))
3047 return 1;
3048 }
3049
3050 return 0;
3051}
3052
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003053void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00003054{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003055 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01003056 const char *opts_str = qdict_get_try_str(qdict, "opts");
3057 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003058
aliguori6f338c32009-02-11 15:21:54 +00003059 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00003060 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00003061 return;
3062 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01003063
3064 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
3065 if (!opts) {
3066 monitor_printf(mon, "parsing network options '%s' failed\n",
3067 opts_str ? opts_str : "");
3068 return;
3069 }
3070
3071 qemu_opt_set(opts, "type", device);
3072
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003073 if (net_client_init(mon, opts) < 0) {
aliguori5c8be672009-04-21 19:56:32 +00003074 monitor_printf(mon, "adding host network device %s failed\n", device);
3075 }
aliguori6f338c32009-02-11 15:21:54 +00003076}
3077
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003078void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00003079{
aliguori6f338c32009-02-11 15:21:54 +00003080 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003081 int vlan_id = qdict_get_int(qdict, "vlan_id");
3082 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00003083
Jan Kiszka1a609522009-06-24 14:42:31 +02003084 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +00003085 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +00003086 return;
3087 }
aliguorie8f1f9d2009-04-21 19:56:08 +00003088 if (!net_host_check_device(vc->model)) {
3089 monitor_printf(mon, "invalid host network device %s\n", device);
3090 return;
3091 }
aliguori6f338c32009-02-11 15:21:54 +00003092 qemu_del_vlan_client(vc);
3093}
3094
Glauber Costa406c8df2009-06-17 09:05:30 -04003095void net_set_boot_mask(int net_boot_mask)
3096{
3097 int i;
3098
3099 /* Only the first four NICs may be bootable */
3100 net_boot_mask = net_boot_mask & 0xF;
3101
3102 for (i = 0; i < nb_nics; i++) {
3103 if (net_boot_mask & (1 << i)) {
3104 nd_table[i].bootable = 1;
3105 net_boot_mask &= ~(1 << i);
3106 }
3107 }
3108
3109 if (net_boot_mask) {
3110 fprintf(stderr, "Cannot boot from non-existent NIC\n");
3111 exit(1);
3112 }
3113}
3114
aliguori376253e2009-03-05 23:01:23 +00003115void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00003116{
3117 VLANState *vlan;
aliguori63a01ef2008-10-31 19:10:00 +00003118
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003119 QTAILQ_FOREACH(vlan, &vlans, next) {
3120 VLANClientState *vc;
3121
aliguori376253e2009-03-05 23:01:23 +00003122 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003123
3124 QTAILQ_FOREACH(vc, &vlan->clients, next) {
aliguori376253e2009-03-05 23:01:23 +00003125 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003126 }
aliguori63a01ef2008-10-31 19:10:00 +00003127 }
3128}
3129
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003130void do_set_link(Monitor *mon, const QDict *qdict)
aliguori436e5e52009-01-08 19:44:06 +00003131{
3132 VLANState *vlan;
3133 VLANClientState *vc = NULL;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03003134 const char *name = qdict_get_str(qdict, "name");
3135 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
aliguori436e5e52009-01-08 19:44:06 +00003136
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003137 QTAILQ_FOREACH(vlan, &vlans, next) {
3138 QTAILQ_FOREACH(vc, &vlan->clients, next) {
3139 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00003140 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003141 }
3142 }
3143 }
edgar_igldd5de372009-01-09 00:48:28 +00003144done:
aliguori436e5e52009-01-08 19:44:06 +00003145
3146 if (!vc) {
Stefan Weil7dc3fa02009-08-08 23:33:26 +02003147 monitor_printf(mon, "could not find network device '%s'\n", name);
Luiz Capitulinoc3cf0d32009-08-03 13:56:59 -03003148 return;
aliguori436e5e52009-01-08 19:44:06 +00003149 }
3150
3151 if (strcmp(up_or_down, "up") == 0)
3152 vc->link_down = 0;
3153 else if (strcmp(up_or_down, "down") == 0)
3154 vc->link_down = 1;
3155 else
aliguori376253e2009-03-05 23:01:23 +00003156 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
3157 "valid\n", up_or_down);
aliguori436e5e52009-01-08 19:44:06 +00003158
aliguori34b25ca2009-01-08 19:45:03 +00003159 if (vc->link_status_changed)
3160 vc->link_status_changed(vc);
aliguori436e5e52009-01-08 19:44:06 +00003161}
3162
aliguori63a01ef2008-10-31 19:10:00 +00003163void net_cleanup(void)
3164{
3165 VLANState *vlan;
3166
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003167 QTAILQ_FOREACH(vlan, &vlans, next) {
3168 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00003169
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003170 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00003171 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00003172 }
3173 }
aliguori63a01ef2008-10-31 19:10:00 +00003174}
3175
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003176static void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00003177{
3178 VLANState *vlan;
3179
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003180 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori63a01ef2008-10-31 19:10:00 +00003181 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
3182 continue;
3183 if (vlan->nb_guest_devs == 0)
3184 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
3185 if (vlan->nb_host_devs == 0)
3186 fprintf(stderr,
3187 "Warning: vlan %d is not connected to host network\n",
3188 vlan->id);
3189 }
3190}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003191
3192static int net_init_client(QemuOpts *opts, void *dummy)
3193{
3194 return net_client_init(NULL, opts);
3195}
3196
3197int net_init_clients(void)
3198{
3199 if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
3200 /* if no clients, we use a default config */
3201 qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
3202#ifdef CONFIG_SLIRP
3203 qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
3204#endif
3205 }
3206
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01003207 QTAILQ_INIT(&vlans);
3208
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003209 if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
3210 return -1;
3211 }
3212
3213 net_check_clients();
3214
3215 return 0;
3216}
3217
3218int net_client_parse(const char *optarg)
3219{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02003220#if defined(CONFIG_SLIRP)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003221 /* handle legacy -net channel,port:chr */
3222 if (!strncmp(optarg, "channel,", strlen("channel,"))) {
3223 int ret;
3224
3225 optarg += strlen("channel,");
3226
3227 if (QTAILQ_EMPTY(&slirp_stacks)) {
3228 struct slirp_config_str *config;
3229
3230 config = qemu_malloc(sizeof(*config));
3231 pstrcpy(config->str, sizeof(config->str), optarg);
3232 config->flags = SLIRP_CFG_LEGACY;
3233 config->next = slirp_configs;
3234 slirp_configs = config;
3235 ret = 0;
3236 } else {
3237 ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1);
3238 }
3239
3240 return ret;
3241 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02003242#endif
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003243 if (!qemu_opts_parse(&qemu_net_opts, optarg, "type")) {
3244 return -1;
3245 }
3246
3247 return 0;
3248}