4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2022 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
30 #include "monitor/monitor.h"
31 #include "qapi/error.h"
32 #include "qemu/error-report.h"
33 #include "qemu/option.h"
34 #include "qemu/sockets.h"
36 #include "qemu/main-loop.h"
37 #include "qemu/cutils.h"
39 typedef struct NetDgramState
{
43 bool read_poll
; /* waiting to receive data? */
44 bool write_poll
; /* waiting to transmit data? */
45 /* contains destination iff connectionless */
46 struct sockaddr
*dest_addr
;
50 static void net_dgram_send(void *opaque
);
51 static void net_dgram_writable(void *opaque
);
53 static void net_dgram_update_fd_handler(NetDgramState
*s
)
55 qemu_set_fd_handler(s
->fd
,
56 s
->read_poll
? net_dgram_send
: NULL
,
57 s
->write_poll
? net_dgram_writable
: NULL
,
61 static void net_dgram_read_poll(NetDgramState
*s
, bool enable
)
63 s
->read_poll
= enable
;
64 net_dgram_update_fd_handler(s
);
67 static void net_dgram_write_poll(NetDgramState
*s
, bool enable
)
69 s
->write_poll
= enable
;
70 net_dgram_update_fd_handler(s
);
73 static void net_dgram_writable(void *opaque
)
75 NetDgramState
*s
= opaque
;
77 net_dgram_write_poll(s
, false);
79 qemu_flush_queued_packets(&s
->nc
);
82 static ssize_t
net_dgram_receive(NetClientState
*nc
,
83 const uint8_t *buf
, size_t size
)
85 NetDgramState
*s
= DO_UPCAST(NetDgramState
, nc
, nc
);
90 ret
= sendto(s
->fd
, buf
, size
, 0, s
->dest_addr
, s
->dest_len
);
92 ret
= send(s
->fd
, buf
, size
, 0);
94 } while (ret
== -1 && errno
== EINTR
);
96 if (ret
== -1 && errno
== EAGAIN
) {
97 net_dgram_write_poll(s
, true);
103 static void net_dgram_send_completed(NetClientState
*nc
, ssize_t len
)
105 NetDgramState
*s
= DO_UPCAST(NetDgramState
, nc
, nc
);
108 net_dgram_read_poll(s
, true);
112 static void net_dgram_rs_finalize(SocketReadState
*rs
)
114 NetDgramState
*s
= container_of(rs
, NetDgramState
, rs
);
116 if (qemu_send_packet_async(&s
->nc
, rs
->buf
,
118 net_dgram_send_completed
) == 0) {
119 net_dgram_read_poll(s
, false);
123 static void net_dgram_send(void *opaque
)
125 NetDgramState
*s
= opaque
;
128 size
= recv(s
->fd
, s
->rs
.buf
, sizeof(s
->rs
.buf
), 0);
133 /* end of connection */
134 net_dgram_read_poll(s
, false);
135 net_dgram_write_poll(s
, false);
138 if (qemu_send_packet_async(&s
->nc
, s
->rs
.buf
, size
,
139 net_dgram_send_completed
) == 0) {
140 net_dgram_read_poll(s
, false);
144 static int net_dgram_mcast_create(struct sockaddr_in
*mcastaddr
,
145 struct in_addr
*localaddr
,
157 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
158 error_setg(errp
, "specified mcastaddr %s (0x%08x) "
159 "does not contain a multicast address",
160 inet_ntoa(mcastaddr
->sin_addr
),
161 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
165 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
167 error_setg_errno(errp
, errno
, "can't create datagram socket");
172 * Allow multiple sockets to bind the same multicast ip and port by setting
173 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
174 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
175 * only on posix systems.
178 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
180 error_setg_errno(errp
, errno
, "can't set socket option SO_REUSEADDR");
184 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
186 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
187 inet_ntoa(mcastaddr
->sin_addr
));
191 /* Add host to multicast group */
192 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
194 imr
.imr_interface
= *localaddr
;
196 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
199 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
200 &imr
, sizeof(struct ip_mreq
));
202 error_setg_errno(errp
, errno
,
203 "can't add socket to multicast group %s",
204 inet_ntoa(imr
.imr_multiaddr
));
208 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
210 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
211 &loop
, sizeof(loop
));
213 error_setg_errno(errp
, errno
,
214 "can't force multicast message to loopback");
218 /* If a bind address is given, only send packets from that address */
219 if (localaddr
!= NULL
) {
220 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
221 localaddr
, sizeof(*localaddr
));
223 error_setg_errno(errp
, errno
,
224 "can't set the default network send interface");
229 qemu_socket_set_nonblock(fd
);
238 static void net_dgram_cleanup(NetClientState
*nc
)
240 NetDgramState
*s
= DO_UPCAST(NetDgramState
, nc
, nc
);
242 net_dgram_read_poll(s
, false);
243 net_dgram_write_poll(s
, false);
247 g_free(s
->dest_addr
);
252 static NetClientInfo net_dgram_socket_info
= {
253 .type
= NET_CLIENT_DRIVER_DGRAM
,
254 .size
= sizeof(NetDgramState
),
255 .receive
= net_dgram_receive
,
256 .cleanup
= net_dgram_cleanup
,
259 static NetDgramState
*net_dgram_fd_init(NetClientState
*peer
,
268 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
270 s
= DO_UPCAST(NetDgramState
, nc
, nc
);
273 net_socket_rs_init(&s
->rs
, net_dgram_rs_finalize
, false);
274 net_dgram_read_poll(s
, true);
279 static int net_dgram_mcast_init(NetClientState
*peer
,
282 SocketAddress
*remote
,
283 SocketAddress
*local
,
288 struct sockaddr_in
*saddr
;
290 if (remote
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
291 error_setg(errp
, "multicast only support inet type");
295 saddr
= g_new(struct sockaddr_in
, 1);
296 if (convert_host_port(saddr
, remote
->u
.inet
.host
, remote
->u
.inet
.port
,
303 fd
= net_dgram_mcast_create(saddr
, NULL
, errp
);
309 switch (local
->type
) {
310 case SOCKET_ADDRESS_TYPE_INET
: {
311 struct in_addr localaddr
;
313 if (inet_aton(local
->u
.inet
.host
, &localaddr
) == 0) {
315 error_setg(errp
, "localaddr '%s' is not a valid IPv4 address",
320 fd
= net_dgram_mcast_create(saddr
, &localaddr
, errp
);
327 case SOCKET_ADDRESS_TYPE_FD
: {
330 fd
= monitor_fd_param(monitor_cur(), local
->u
.fd
.str
, errp
);
335 ret
= qemu_socket_try_set_nonblock(fd
);
338 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
344 * fd passed: multicast: "learn" dest_addr address from bound
345 * address and save it. Because this may be "shared" socket from a
346 * "master" process, datagrams would be recv() by ONLY ONE process:
347 * we must "clone" this dgram socket --jjo
350 saddr
= g_new(struct sockaddr_in
, 1);
352 if (convert_host_port(saddr
, local
->u
.inet
.host
, local
->u
.inet
.port
,
360 if (saddr
->sin_addr
.s_addr
== 0) {
361 error_setg(errp
, "can't setup multicast destination address");
366 /* clone dgram socket */
367 newfd
= net_dgram_mcast_create(saddr
, NULL
, errp
);
373 /* clone newfd to fd, close newfd */
380 error_setg(errp
, "only support inet or fd type for local");
385 s
= net_dgram_fd_init(peer
, model
, name
, fd
, errp
);
391 g_assert(s
->dest_addr
== NULL
);
392 s
->dest_addr
= (struct sockaddr
*)saddr
;
393 s
->dest_len
= sizeof(*saddr
);
396 qemu_set_info_str(&s
->nc
, "mcast=%s:%d",
397 inet_ntoa(saddr
->sin_addr
),
398 ntohs(saddr
->sin_port
));
400 switch (local
->type
) {
401 case SOCKET_ADDRESS_TYPE_INET
:
402 qemu_set_info_str(&s
->nc
, "mcast=%s:%d",
403 inet_ntoa(saddr
->sin_addr
),
404 ntohs(saddr
->sin_port
));
406 case SOCKET_ADDRESS_TYPE_FD
:
407 qemu_set_info_str(&s
->nc
, "fd=%d (cloned mcast=%s:%d)",
408 fd
, inet_ntoa(saddr
->sin_addr
),
409 ntohs(saddr
->sin_port
));
412 g_assert_not_reached();
421 int net_init_dgram(const Netdev
*netdev
, const char *name
,
422 NetClientState
*peer
, Error
**errp
)
426 SocketAddress
*remote
, *local
;
427 struct sockaddr
*dest_addr
;
428 struct sockaddr_in laddr_in
, raddr_in
;
429 struct sockaddr_un laddr_un
, raddr_un
;
432 assert(netdev
->type
== NET_CLIENT_DRIVER_DGRAM
);
434 remote
= netdev
->u
.dgram
.remote
;
435 local
= netdev
->u
.dgram
.local
;
437 /* detect multicast address */
438 if (remote
&& remote
->type
== SOCKET_ADDRESS_TYPE_INET
) {
439 struct sockaddr_in mcastaddr
;
441 if (convert_host_port(&mcastaddr
, remote
->u
.inet
.host
,
442 remote
->u
.inet
.port
, errp
) < 0) {
446 if (IN_MULTICAST(ntohl(mcastaddr
.sin_addr
.s_addr
))) {
447 return net_dgram_mcast_init(peer
, "dram", name
, remote
, local
,
452 /* unicast address */
454 error_setg(errp
, "dgram requires local= parameter");
459 if (local
->type
== SOCKET_ADDRESS_TYPE_FD
) {
460 error_setg(errp
, "don't set remote with local.fd");
463 if (remote
->type
!= local
->type
) {
464 error_setg(errp
, "remote and local types must be the same");
468 if (local
->type
!= SOCKET_ADDRESS_TYPE_FD
) {
470 "type=inet or type=unix requires remote parameter");
475 switch (local
->type
) {
476 case SOCKET_ADDRESS_TYPE_INET
:
477 if (convert_host_port(&laddr_in
, local
->u
.inet
.host
, local
->u
.inet
.port
,
482 if (convert_host_port(&raddr_in
, remote
->u
.inet
.host
,
483 remote
->u
.inet
.port
, errp
) < 0) {
487 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
489 error_setg_errno(errp
, errno
, "can't create datagram socket");
493 ret
= socket_set_fast_reuse(fd
);
495 error_setg_errno(errp
, errno
,
496 "can't set socket option SO_REUSEADDR");
500 ret
= bind(fd
, (struct sockaddr
*)&laddr_in
, sizeof(laddr_in
));
502 error_setg_errno(errp
, errno
, "can't bind ip=%s to socket",
503 inet_ntoa(laddr_in
.sin_addr
));
507 qemu_socket_set_nonblock(fd
);
509 dest_len
= sizeof(raddr_in
);
510 dest_addr
= g_malloc(dest_len
);
511 memcpy(dest_addr
, &raddr_in
, dest_len
);
513 case SOCKET_ADDRESS_TYPE_UNIX
:
514 ret
= unlink(local
->u
.q_unix
.path
);
515 if (ret
< 0 && errno
!= ENOENT
) {
516 error_setg_errno(errp
, errno
, "failed to unlink socket %s",
517 local
->u
.q_unix
.path
);
521 laddr_un
.sun_family
= PF_UNIX
;
522 ret
= snprintf(laddr_un
.sun_path
, sizeof(laddr_un
.sun_path
), "%s",
523 local
->u
.q_unix
.path
);
524 if (ret
< 0 || ret
>= sizeof(laddr_un
.sun_path
)) {
525 error_setg(errp
, "UNIX socket path '%s' is too long",
526 local
->u
.q_unix
.path
);
527 error_append_hint(errp
, "Path must be less than %zu bytes\n",
528 sizeof(laddr_un
.sun_path
));
531 raddr_un
.sun_family
= PF_UNIX
;
532 ret
= snprintf(raddr_un
.sun_path
, sizeof(raddr_un
.sun_path
), "%s",
533 remote
->u
.q_unix
.path
);
534 if (ret
< 0 || ret
>= sizeof(raddr_un
.sun_path
)) {
535 error_setg(errp
, "UNIX socket path '%s' is too long",
536 remote
->u
.q_unix
.path
);
537 error_append_hint(errp
, "Path must be less than %zu bytes\n",
538 sizeof(raddr_un
.sun_path
));
541 fd
= qemu_socket(PF_UNIX
, SOCK_DGRAM
, 0);
543 error_setg_errno(errp
, errno
, "can't create datagram socket");
547 ret
= bind(fd
, (struct sockaddr
*)&laddr_un
, sizeof(laddr_un
));
549 error_setg_errno(errp
, errno
, "can't bind unix=%s to socket",
554 qemu_socket_set_nonblock(fd
);
556 dest_len
= sizeof(raddr_un
);
557 dest_addr
= g_malloc(dest_len
);
558 memcpy(dest_addr
, &raddr_un
, dest_len
);
560 case SOCKET_ADDRESS_TYPE_FD
:
561 fd
= monitor_fd_param(monitor_cur(), local
->u
.fd
.str
, errp
);
565 ret
= qemu_socket_try_set_nonblock(fd
);
567 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
575 error_setg(errp
, "only support inet or fd type for local");
579 s
= net_dgram_fd_init(peer
, "dgram", name
, fd
, errp
);
585 g_assert(s
->dest_addr
== NULL
);
586 s
->dest_addr
= dest_addr
;
587 s
->dest_len
= dest_len
;
590 switch (local
->type
) {
591 case SOCKET_ADDRESS_TYPE_INET
:
592 qemu_set_info_str(&s
->nc
, "udp=%s:%d/%s:%d",
593 inet_ntoa(laddr_in
.sin_addr
),
594 ntohs(laddr_in
.sin_port
),
595 inet_ntoa(raddr_in
.sin_addr
),
596 ntohs(raddr_in
.sin_port
));
598 case SOCKET_ADDRESS_TYPE_UNIX
:
599 qemu_set_info_str(&s
->nc
, "udp=%s:%s",
600 laddr_un
.sun_path
, raddr_un
.sun_path
);
602 case SOCKET_ADDRESS_TYPE_FD
: {
604 SocketAddressType sa_type
;
606 sa
= socket_local_address(fd
, errp
);
609 qapi_free_SocketAddress(sa
);
611 qemu_set_info_str(&s
->nc
, "fd=%d %s", fd
,
612 SocketAddressType_str(sa_type
));
614 qemu_set_info_str(&s
->nc
, "fd=%d", fd
);
619 g_assert_not_reached();