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