1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25
26#include "net/net.h"
27#include "clients.h"
28#include "monitor/monitor.h"
29#include "qapi/error.h"
30#include "qemu/error-report.h"
31#include "qemu/option.h"
32#include "qemu/sockets.h"
33#include "qemu/iov.h"
34#include "qemu/main-loop.h"
35
36typedef struct NetSocketState {
37 NetClientState nc;
38 int listen_fd;
39 int fd;
40 SocketReadState rs;
41 unsigned int send_index;
42 struct sockaddr_in dgram_dst;
43 IOHandler *send_fn;
44 bool read_poll;
45 bool write_poll;
46} NetSocketState;
47
48static void net_socket_accept(void *opaque);
49static void net_socket_writable(void *opaque);
50
51static void net_socket_update_fd_handler(NetSocketState *s)
52{
53 qemu_set_fd_handler(s->fd,
54 s->read_poll ? s->send_fn : NULL,
55 s->write_poll ? net_socket_writable : NULL,
56 s);
57}
58
59static void net_socket_read_poll(NetSocketState *s, bool enable)
60{
61 s->read_poll = enable;
62 net_socket_update_fd_handler(s);
63}
64
65static void net_socket_write_poll(NetSocketState *s, bool enable)
66{
67 s->write_poll = enable;
68 net_socket_update_fd_handler(s);
69}
70
71static void net_socket_writable(void *opaque)
72{
73 NetSocketState *s = opaque;
74
75 net_socket_write_poll(s, false);
76
77 qemu_flush_queued_packets(&s->nc);
78}
79
80static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
81{
82 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
83 uint32_t len = htonl(size);
84 struct iovec iov[] = {
85 {
86 .iov_base = &len,
87 .iov_len = sizeof(len),
88 }, {
89 .iov_base = (void *)buf,
90 .iov_len = size,
91 },
92 };
93 size_t remaining;
94 ssize_t ret;
95
96 remaining = iov_size(iov, 2) - s->send_index;
97 ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
98
99 if (ret == -1 && errno == EAGAIN) {
100 ret = 0;
101 }
102 if (ret == -1) {
103 s->send_index = 0;
104 return -errno;
105 }
106 if (ret < (ssize_t)remaining) {
107 s->send_index += ret;
108 net_socket_write_poll(s, true);
109 return 0;
110 }
111 s->send_index = 0;
112 return size;
113}
114
115static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
116{
117 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
118 ssize_t ret;
119
120 ret = RETRY_ON_EINTR(
121 s->dgram_dst.sin_family != AF_UNIX ?
122 sendto(s->fd, buf, size, 0,
123 (struct sockaddr *)&s->dgram_dst,
124 sizeof(s->dgram_dst)) :
125 send(s->fd, buf, size, 0)
126 );
127
128 if (ret == -1 && errno == EAGAIN) {
129 net_socket_write_poll(s, true);
130 return 0;
131 }
132 return ret;
133}
134
135static void net_socket_send_completed(NetClientState *nc, ssize_t len)
136{
137 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
138
139 if (!s->read_poll) {
140 net_socket_read_poll(s, true);
141 }
142}
143
144static void net_socket_rs_finalize(SocketReadState *rs)
145{
146 NetSocketState *s = container_of(rs, NetSocketState, rs);
147
148 if (qemu_send_packet_async(&s->nc, rs->buf,
149 rs->packet_len,
150 net_socket_send_completed) == 0) {
151 net_socket_read_poll(s, false);
152 }
153}
154
155static void net_socket_send(void *opaque)
156{
157 NetSocketState *s = opaque;
158 int size;
159 int ret;
160 uint8_t buf1[NET_BUFSIZE];
161 const uint8_t *buf;
162
163 size = recv(s->fd, buf1, sizeof(buf1), 0);
164 if (size < 0) {
165 if (errno != EWOULDBLOCK)
166 goto eoc;
167 } else if (size == 0) {
168
169 eoc:
170 net_socket_read_poll(s, false);
171 net_socket_write_poll(s, false);
172 if (s->listen_fd != -1) {
173 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
174 }
175 close(s->fd);
176
177 s->fd = -1;
178 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
179 s->nc.link_down = true;
180 qemu_set_info_str(&s->nc, "%s", "");
181
182 return;
183 }
184 buf = buf1;
185
186 ret = net_fill_rstate(&s->rs, buf, size);
187
188 if (ret == -1) {
189 goto eoc;
190 }
191}
192
193static void net_socket_send_dgram(void *opaque)
194{
195 NetSocketState *s = opaque;
196 int size;
197
198 size = recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0);
199 if (size < 0)
200 return;
201 if (size == 0) {
202
203 net_socket_read_poll(s, false);
204 net_socket_write_poll(s, false);
205 return;
206 }
207 if (qemu_send_packet_async(&s->nc, s->rs.buf, size,
208 net_socket_send_completed) == 0) {
209 net_socket_read_poll(s, false);
210 }
211}
212
213static int net_socket_mcast_create(struct sockaddr_in *mcastaddr,
214 struct in_addr *localaddr,
215 Error **errp)
216{
217 struct ip_mreq imr;
218 int fd;
219 int val, ret;
220#ifdef __OpenBSD__
221 unsigned char loop;
222#else
223 int loop;
224#endif
225
226 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
227 error_setg(errp, "specified mcastaddr %s (0x%08x) "
228 "does not contain a multicast address",
229 inet_ntoa(mcastaddr->sin_addr),
230 (int)ntohl(mcastaddr->sin_addr.s_addr));
231 return -1;
232 }
233
234 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
235 if (fd < 0) {
236 error_setg_errno(errp, errno, "can't create datagram socket");
237 return -1;
238 }
239
240
241
242
243
244
245 val = 1;
246 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
247 if (ret < 0) {
248 error_setg_errno(errp, errno,
249 "can't set socket option SO_REUSEADDR");
250 goto fail;
251 }
252
253 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
254 if (ret < 0) {
255 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
256 inet_ntoa(mcastaddr->sin_addr));
257 goto fail;
258 }
259
260
261 imr.imr_multiaddr = mcastaddr->sin_addr;
262 if (localaddr) {
263 imr.imr_interface = *localaddr;
264 } else {
265 imr.imr_interface.s_addr = htonl(INADDR_ANY);
266 }
267
268 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
269 &imr, sizeof(struct ip_mreq));
270 if (ret < 0) {
271 error_setg_errno(errp, errno,
272 "can't add socket to multicast group %s",
273 inet_ntoa(imr.imr_multiaddr));
274 goto fail;
275 }
276
277
278 loop = 1;
279 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
280 &loop, sizeof(loop));
281 if (ret < 0) {
282 error_setg_errno(errp, errno,
283 "can't force multicast message to loopback");
284 goto fail;
285 }
286
287
288 if (localaddr != NULL) {
289 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
290 localaddr, sizeof(*localaddr));
291 if (ret < 0) {
292 error_setg_errno(errp, errno,
293 "can't set the default network send interface");
294 goto fail;
295 }
296 }
297
298 qemu_socket_set_nonblock(fd);
299 return fd;
300fail:
301 if (fd >= 0)
302 close(fd);
303 return -1;
304}
305
306static void net_socket_cleanup(NetClientState *nc)
307{
308 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
309 if (s->fd != -1) {
310 net_socket_read_poll(s, false);
311 net_socket_write_poll(s, false);
312 close(s->fd);
313 s->fd = -1;
314 }
315 if (s->listen_fd != -1) {
316 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
317 close(s->listen_fd);
318 s->listen_fd = -1;
319 }
320}
321
322static NetClientInfo net_dgram_socket_info = {
323 .type = NET_CLIENT_DRIVER_SOCKET,
324 .size = sizeof(NetSocketState),
325 .receive = net_socket_receive_dgram,
326 .cleanup = net_socket_cleanup,
327};
328
329static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
330 const char *model,
331 const char *name,
332 int fd, int is_connected,
333 const char *mcast,
334 Error **errp)
335{
336 struct sockaddr_in saddr;
337 int newfd;
338 NetClientState *nc;
339 NetSocketState *s;
340 SocketAddress *sa;
341 SocketAddressType sa_type;
342
343 sa = socket_local_address(fd, errp);
344 if (!sa) {
345 return NULL;
346 }
347 sa_type = sa->type;
348 qapi_free_SocketAddress(sa);
349
350
351
352
353
354
355 if (is_connected && mcast != NULL) {
356 if (parse_host_port(&saddr, mcast, errp) < 0) {
357 goto err;
358 }
359
360 if (saddr.sin_addr.s_addr == 0) {
361 error_setg(errp, "can't setup multicast destination address");
362 goto err;
363 }
364
365 newfd = net_socket_mcast_create(&saddr, NULL, errp);
366 if (newfd < 0) {
367 goto err;
368 }
369
370 dup2(newfd, fd);
371 close(newfd);
372
373 }
374
375 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
376
377 s = DO_UPCAST(NetSocketState, nc, nc);
378
379 s->fd = fd;
380 s->listen_fd = -1;
381 s->send_fn = net_socket_send_dgram;
382 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
383 net_socket_read_poll(s, true);
384
385
386 if (is_connected && mcast != NULL) {
387 s->dgram_dst = saddr;
388 qemu_set_info_str(nc, "socket: fd=%d (cloned mcast=%s:%d)", fd,
389 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
390 } else {
391 if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
392 s->dgram_dst.sin_family = AF_UNIX;
393 }
394
395 qemu_set_info_str(nc, "socket: fd=%d %s", fd,
396 SocketAddressType_str(sa_type));
397 }
398
399 return s;
400
401err:
402 close(fd);
403 return NULL;
404}
405
406static void net_socket_connect(void *opaque)
407{
408 NetSocketState *s = opaque;
409 s->send_fn = net_socket_send;
410 net_socket_read_poll(s, true);
411}
412
413static NetClientInfo net_socket_info = {
414 .type = NET_CLIENT_DRIVER_SOCKET,
415 .size = sizeof(NetSocketState),
416 .receive = net_socket_receive,
417 .cleanup = net_socket_cleanup,
418};
419
420static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
421 const char *model,
422 const char *name,
423 int fd, int is_connected)
424{
425 NetClientState *nc;
426 NetSocketState *s;
427
428 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
429
430 qemu_set_info_str(nc, "socket: fd=%d", fd);
431
432 s = DO_UPCAST(NetSocketState, nc, nc);
433
434 s->fd = fd;
435 s->listen_fd = -1;
436 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
437
438
439 socket_set_nodelay(fd);
440
441 if (is_connected) {
442 net_socket_connect(s);
443 } else {
444 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
445 }
446 return s;
447}
448
449static NetSocketState *net_socket_fd_init(NetClientState *peer,
450 const char *model, const char *name,
451 int fd, int is_connected,
452 const char *mc, Error **errp)
453{
454 int so_type = -1, optlen=sizeof(so_type);
455
456 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
457 (socklen_t *)&optlen)< 0) {
458 error_setg(errp, "can't get socket option SO_TYPE");
459 close(fd);
460 return NULL;
461 }
462 switch(so_type) {
463 case SOCK_DGRAM:
464 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected,
465 mc, errp);
466 case SOCK_STREAM:
467 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
468 default:
469 error_setg(errp, "socket type=%d for fd=%d must be either"
470 " SOCK_DGRAM or SOCK_STREAM", so_type, fd);
471 close(fd);
472 }
473 return NULL;
474}
475
476static void net_socket_accept(void *opaque)
477{
478 NetSocketState *s = opaque;
479 struct sockaddr_in saddr;
480 socklen_t len;
481 int fd;
482
483 for(;;) {
484 len = sizeof(saddr);
485 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
486 if (fd < 0 && errno != EINTR) {
487 return;
488 } else if (fd >= 0) {
489 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
490 break;
491 }
492 }
493
494 s->fd = fd;
495 s->nc.link_down = false;
496 net_socket_connect(s);
497 qemu_set_info_str(&s->nc, "socket: connection from %s:%d",
498 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
499}
500
501static int net_socket_listen_init(NetClientState *peer,
502 const char *model,
503 const char *name,
504 const char *host_str,
505 Error **errp)
506{
507 NetClientState *nc;
508 NetSocketState *s;
509 struct sockaddr_in saddr;
510 int fd, ret;
511
512 if (parse_host_port(&saddr, host_str, errp) < 0) {
513 return -1;
514 }
515
516 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
517 if (fd < 0) {
518 error_setg_errno(errp, errno, "can't create stream socket");
519 return -1;
520 }
521 qemu_socket_set_nonblock(fd);
522
523 socket_set_fast_reuse(fd);
524
525 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
526 if (ret < 0) {
527 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
528 inet_ntoa(saddr.sin_addr));
529 close(fd);
530 return -1;
531 }
532 ret = listen(fd, 0);
533 if (ret < 0) {
534 error_setg_errno(errp, errno, "can't listen on socket");
535 close(fd);
536 return -1;
537 }
538
539 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
540 s = DO_UPCAST(NetSocketState, nc, nc);
541 s->fd = -1;
542 s->listen_fd = fd;
543 s->nc.link_down = true;
544 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
545
546 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
547 return 0;
548}
549
550static int net_socket_connect_init(NetClientState *peer,
551 const char *model,
552 const char *name,
553 const char *host_str,
554 Error **errp)
555{
556 NetSocketState *s;
557 int fd, connected, ret;
558 struct sockaddr_in saddr;
559
560 if (parse_host_port(&saddr, host_str, errp) < 0) {
561 return -1;
562 }
563
564 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
565 if (fd < 0) {
566 error_setg_errno(errp, errno, "can't create stream socket");
567 return -1;
568 }
569 qemu_socket_set_nonblock(fd);
570
571 connected = 0;
572 for(;;) {
573 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
574 if (ret < 0) {
575 if (errno == EINTR || errno == EWOULDBLOCK) {
576
577 } else if (errno == EINPROGRESS ||
578 errno == EALREADY) {
579 break;
580 } else {
581 error_setg_errno(errp, errno, "can't connect socket");
582 close(fd);
583 return -1;
584 }
585 } else {
586 connected = 1;
587 break;
588 }
589 }
590 s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp);
591 if (!s) {
592 return -1;
593 }
594
595 qemu_set_info_str(&s->nc, "socket: connect to %s:%d",
596 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
597 return 0;
598}
599
600static int net_socket_mcast_init(NetClientState *peer,
601 const char *model,
602 const char *name,
603 const char *host_str,
604 const char *localaddr_str,
605 Error **errp)
606{
607 NetSocketState *s;
608 int fd;
609 struct sockaddr_in saddr;
610 struct in_addr localaddr, *param_localaddr;
611
612 if (parse_host_port(&saddr, host_str, errp) < 0) {
613 return -1;
614 }
615
616 if (localaddr_str != NULL) {
617 if (inet_aton(localaddr_str, &localaddr) == 0) {
618 error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
619 localaddr_str);
620 return -1;
621 }
622 param_localaddr = &localaddr;
623 } else {
624 param_localaddr = NULL;
625 }
626
627 fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
628 if (fd < 0) {
629 return -1;
630 }
631
632 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
633 if (!s) {
634 return -1;
635 }
636
637 s->dgram_dst = saddr;
638
639 qemu_set_info_str(&s->nc, "socket: mcast=%s:%d",
640 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
641 return 0;
642
643}
644
645static int net_socket_udp_init(NetClientState *peer,
646 const char *model,
647 const char *name,
648 const char *rhost,
649 const char *lhost,
650 Error **errp)
651{
652 NetSocketState *s;
653 int fd, ret;
654 struct sockaddr_in laddr, raddr;
655
656 if (parse_host_port(&laddr, lhost, errp) < 0) {
657 return -1;
658 }
659
660 if (parse_host_port(&raddr, rhost, errp) < 0) {
661 return -1;
662 }
663
664 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
665 if (fd < 0) {
666 error_setg_errno(errp, errno, "can't create datagram socket");
667 return -1;
668 }
669
670 ret = socket_set_fast_reuse(fd);
671 if (ret < 0) {
672 error_setg_errno(errp, errno,
673 "can't set socket option SO_REUSEADDR");
674 close(fd);
675 return -1;
676 }
677 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
678 if (ret < 0) {
679 error_setg_errno(errp, errno, "can't bind ip=%s to socket",
680 inet_ntoa(laddr.sin_addr));
681 close(fd);
682 return -1;
683 }
684 qemu_socket_set_nonblock(fd);
685
686 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
687 if (!s) {
688 return -1;
689 }
690
691 s->dgram_dst = raddr;
692
693 qemu_set_info_str(&s->nc, "socket: udp=%s:%d", inet_ntoa(raddr.sin_addr),
694 ntohs(raddr.sin_port));
695 return 0;
696}
697
698int net_init_socket(const Netdev *netdev, const char *name,
699 NetClientState *peer, Error **errp)
700{
701 const NetdevSocketOptions *sock;
702
703 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
704 sock = &netdev->u.socket;
705
706 if (!!sock->fd + !!sock->listen + !!sock->connect + !!sock->mcast +
707 !!sock->udp != 1) {
708 error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
709 " is required");
710 return -1;
711 }
712
713 if (sock->localaddr && !sock->mcast && !sock->udp) {
714 error_setg(errp, "localaddr= is only valid with mcast= or udp=");
715 return -1;
716 }
717
718 if (sock->fd) {
719 int fd, ret;
720
721 fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
722 if (fd == -1) {
723 return -1;
724 }
725 ret = qemu_socket_try_set_nonblock(fd);
726 if (ret < 0) {
727 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
728 name, fd);
729 return -1;
730 }
731 if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast,
732 errp)) {
733 return -1;
734 }
735 return 0;
736 }
737
738 if (sock->listen) {
739 if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
740 < 0) {
741 return -1;
742 }
743 return 0;
744 }
745
746 if (sock->connect) {
747 if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
748 < 0) {
749 return -1;
750 }
751 return 0;
752 }
753
754 if (sock->mcast) {
755
756
757 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
758 sock->localaddr, errp) < 0) {
759 return -1;
760 }
761 return 0;
762 }
763
764 assert(sock->udp);
765 if (!sock->localaddr) {
766 error_setg(errp, "localaddr= is mandatory with udp=");
767 return -1;
768 }
769 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr,
770 errp) < 0) {
771 return -1;
772 }
773 return 0;
774}
775