Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 2744d92 | 2016-01-29 17:50:00 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Marc-André Lureau | 2addc8f | 2018-11-14 16:36:33 +0400 | [diff] [blame] | 26 | #include "qemu/log.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 27 | #include "net/slirp.h" |
| 28 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 29 | |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 30 | #if defined(CONFIG_SMBD_COMMAND) |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 31 | #include <pwd.h> |
Blue Swirl | 28b150b | 2010-01-27 17:47:33 +0000 | [diff] [blame] | 32 | #include <sys/wait.h> |
| 33 | #endif |
Bin Meng | 969e50b | 2021-03-17 14:26:29 +0800 | [diff] [blame] | 34 | #include "net/eth.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 35 | #include "net/net.h" |
Paolo Bonzini | a245fc1 | 2012-09-17 18:43:51 +0200 | [diff] [blame] | 36 | #include "clients.h" |
| 37 | #include "hub.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 38 | #include "monitor/monitor.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 39 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 40 | #include "qemu/sockets.h" |
Marc-André Lureau | 675b9b5 | 2019-02-12 17:25:23 +0100 | [diff] [blame] | 41 | #include <libslirp.h> |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 42 | #include "chardev/char-fe.h" |
Paolo Bonzini | f6c2e66 | 2016-07-12 09:57:12 +0200 | [diff] [blame] | 43 | #include "sysemu/sysemu.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 44 | #include "qemu/cutils.h" |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 45 | #include "qapi/error.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 46 | #include "qapi/qmp/qdict.h" |
Marc-André Lureau | e05ae1d | 2018-11-14 16:36:40 +0400 | [diff] [blame] | 47 | #include "util.h" |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 48 | #include "migration/register.h" |
Juan Quintela | b23db4c | 2023-10-20 11:07:23 +0200 | [diff] [blame] | 49 | #include "migration/vmstate.h" |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 50 | #include "migration/qemu-file-types.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 51 | |
| 52 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 53 | { |
| 54 | const char *p, *p1; |
| 55 | int len; |
| 56 | p = *pp; |
| 57 | p1 = strchr(p, sep); |
| 58 | if (!p1) |
| 59 | return -1; |
| 60 | len = p1 - p; |
| 61 | p1++; |
| 62 | if (buf_size > 0) { |
| 63 | if (len > buf_size - 1) |
| 64 | len = buf_size - 1; |
| 65 | memcpy(buf, p, len); |
| 66 | buf[len] = '\0'; |
| 67 | } |
| 68 | *pp = p1; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* slirp network adapter */ |
| 73 | |
| 74 | #define SLIRP_CFG_HOSTFWD 1 |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 75 | |
| 76 | struct slirp_config_str { |
| 77 | struct slirp_config_str *next; |
| 78 | int flags; |
| 79 | char str[1024]; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Marc-André Lureau | 8d45a3b | 2019-01-17 15:43:35 +0400 | [diff] [blame] | 82 | struct GuestFwd { |
| 83 | CharBackend hd; |
| 84 | struct in_addr server; |
| 85 | int port; |
| 86 | Slirp *slirp; |
| 87 | }; |
| 88 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 89 | typedef struct SlirpState { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 90 | NetClientState nc; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 91 | QTAILQ_ENTRY(SlirpState) entry; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 92 | Slirp *slirp; |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 93 | Notifier poll_notifier; |
Paolo Bonzini | f6c2e66 | 2016-07-12 09:57:12 +0200 | [diff] [blame] | 94 | Notifier exit_notifier; |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 95 | #if defined(CONFIG_SMBD_COMMAND) |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 96 | gchar *smb_dir; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 97 | #endif |
Marc-André Lureau | 8d45a3b | 2019-01-17 15:43:35 +0400 | [diff] [blame] | 98 | GSList *fwd; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 99 | } SlirpState; |
| 100 | |
| 101 | static struct slirp_config_str *slirp_configs; |
Paolo Bonzini | b58deb3 | 2018-12-06 11:58:10 +0100 | [diff] [blame] | 102 | static QTAILQ_HEAD(, SlirpState) slirp_stacks = |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 103 | QTAILQ_HEAD_INITIALIZER(slirp_stacks); |
| 104 | |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 105 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp); |
| 106 | static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 107 | |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 108 | #if defined(CONFIG_SMBD_COMMAND) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 109 | static int slirp_smb(SlirpState *s, const char *exported_dir, |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 110 | struct in_addr vserver_addr, Error **errp); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 111 | static void slirp_smb_cleanup(SlirpState *s); |
| 112 | #else |
| 113 | static inline void slirp_smb_cleanup(SlirpState *s) { } |
| 114 | #endif |
| 115 | |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 116 | static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len, |
| 117 | void *opaque) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 118 | { |
| 119 | SlirpState *s = opaque; |
Bin Meng | 969e50b | 2021-03-17 14:26:29 +0800 | [diff] [blame] | 120 | uint8_t min_pkt[ETH_ZLEN]; |
| 121 | size_t min_pktsz = sizeof(min_pkt); |
| 122 | |
Jason Wang | bc38e31 | 2021-04-23 11:18:03 +0800 | [diff] [blame] | 123 | if (net_peer_needs_padding(&s->nc)) { |
Bin Meng | 969e50b | 2021-03-17 14:26:29 +0800 | [diff] [blame] | 124 | if (eth_pad_short_frame(min_pkt, &min_pktsz, pkt, pkt_len)) { |
| 125 | pkt = min_pkt; |
| 126 | pkt_len = min_pktsz; |
| 127 | } |
| 128 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 129 | |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 130 | return qemu_send_packet(&s->nc, pkt, pkt_len); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 133 | static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 134 | { |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 135 | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 136 | |
| 137 | slirp_input(s->slirp, buf, size); |
| 138 | |
| 139 | return size; |
| 140 | } |
| 141 | |
Paolo Bonzini | f6c2e66 | 2016-07-12 09:57:12 +0200 | [diff] [blame] | 142 | static void slirp_smb_exit(Notifier *n, void *data) |
| 143 | { |
| 144 | SlirpState *s = container_of(n, SlirpState, exit_notifier); |
| 145 | slirp_smb_cleanup(s); |
| 146 | } |
| 147 | |
Marc-André Lureau | 8d45a3b | 2019-01-17 15:43:35 +0400 | [diff] [blame] | 148 | static void slirp_free_fwd(gpointer data) |
| 149 | { |
| 150 | struct GuestFwd *fwd = data; |
| 151 | |
| 152 | qemu_chr_fe_deinit(&fwd->hd, true); |
| 153 | g_free(data); |
| 154 | } |
| 155 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 156 | static void net_slirp_cleanup(NetClientState *nc) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 157 | { |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 158 | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 159 | |
Marc-André Lureau | 8d45a3b | 2019-01-17 15:43:35 +0400 | [diff] [blame] | 160 | g_slist_free_full(s->fwd, slirp_free_fwd); |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 161 | main_loop_poll_remove_notifier(&s->poll_notifier); |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 162 | unregister_savevm(NULL, "slirp", s->slirp); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 163 | slirp_cleanup(s->slirp); |
Marc-André Lureau | 67f3280 | 2016-08-18 17:44:05 +0400 | [diff] [blame] | 164 | if (s->exit_notifier.notify) { |
| 165 | qemu_remove_exit_notifier(&s->exit_notifier); |
| 166 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 167 | slirp_smb_cleanup(s); |
| 168 | QTAILQ_REMOVE(&slirp_stacks, s, entry); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 171 | static NetClientInfo net_slirp_info = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 172 | .type = NET_CLIENT_DRIVER_USER, |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 173 | .size = sizeof(SlirpState), |
| 174 | .receive = net_slirp_receive, |
| 175 | .cleanup = net_slirp_cleanup, |
| 176 | }; |
| 177 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 178 | static void net_slirp_guest_error(const char *msg, void *opaque) |
Marc-André Lureau | 2addc8f | 2018-11-14 16:36:33 +0400 | [diff] [blame] | 179 | { |
| 180 | qemu_log_mask(LOG_GUEST_ERROR, "%s", msg); |
| 181 | } |
| 182 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 183 | static int64_t net_slirp_clock_get_ns(void *opaque) |
Marc-André Lureau | e6dbff3 | 2018-11-22 02:06:28 +0400 | [diff] [blame] | 184 | { |
| 185 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 186 | } |
| 187 | |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 188 | typedef struct SlirpTimer SlirpTimer; |
Paolo Bonzini | ad2e5b8 | 2022-04-11 09:26:06 +0200 | [diff] [blame] | 189 | struct SlirpTimer { |
| 190 | QEMUTimer timer; |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 191 | #if SLIRP_CHECK_VERSION(4,7,0) |
| 192 | Slirp *slirp; |
| 193 | SlirpTimerId id; |
| 194 | void *cb_opaque; |
| 195 | #endif |
| 196 | }; |
| 197 | |
| 198 | #if SLIRP_CHECK_VERSION(4,7,0) |
| 199 | static void net_slirp_init_completed(Slirp *slirp, void *opaque) |
| 200 | { |
| 201 | SlirpState *s = opaque; |
| 202 | s->slirp = slirp; |
Paolo Bonzini | ad2e5b8 | 2022-04-11 09:26:06 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 205 | static void net_slirp_timer_cb(void *opaque) |
| 206 | { |
| 207 | SlirpTimer *t = opaque; |
| 208 | slirp_handle_timer(t->slirp, t->id, t->cb_opaque); |
| 209 | } |
| 210 | |
| 211 | static void *net_slirp_timer_new_opaque(SlirpTimerId id, |
| 212 | void *cb_opaque, void *opaque) |
| 213 | { |
| 214 | SlirpState *s = opaque; |
| 215 | SlirpTimer *t = g_new(SlirpTimer, 1); |
| 216 | t->slirp = s->slirp; |
| 217 | t->id = id; |
| 218 | t->cb_opaque = cb_opaque; |
| 219 | timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL, |
| 220 | SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL, |
| 221 | net_slirp_timer_cb, t); |
| 222 | return t; |
| 223 | } |
| 224 | #else |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 225 | static void *net_slirp_timer_new(SlirpTimerCb cb, |
| 226 | void *cb_opaque, void *opaque) |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 227 | { |
Paolo Bonzini | ad2e5b8 | 2022-04-11 09:26:06 +0200 | [diff] [blame] | 228 | SlirpTimer *t = g_new(SlirpTimer, 1); |
| 229 | timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL, |
| 230 | SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL, |
| 231 | cb, cb_opaque); |
| 232 | return t; |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 233 | } |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 234 | #endif |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 235 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 236 | static void net_slirp_timer_free(void *timer, void *opaque) |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 237 | { |
Paolo Bonzini | ad2e5b8 | 2022-04-11 09:26:06 +0200 | [diff] [blame] | 238 | SlirpTimer *t = timer; |
| 239 | timer_del(&t->timer); |
| 240 | g_free(t); |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 241 | } |
| 242 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 243 | static void net_slirp_timer_mod(void *timer, int64_t expire_timer, |
| 244 | void *opaque) |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 245 | { |
Paolo Bonzini | ad2e5b8 | 2022-04-11 09:26:06 +0200 | [diff] [blame] | 246 | SlirpTimer *t = timer; |
| 247 | timer_mod(&t->timer, expire_timer); |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 248 | } |
| 249 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 250 | static void net_slirp_register_poll_fd(int fd, void *opaque) |
Marc-André Lureau | 848c709 | 2019-01-17 15:43:41 +0400 | [diff] [blame] | 251 | { |
Marc-André Lureau | faa4ec1 | 2023-02-21 16:47:56 +0400 | [diff] [blame] | 252 | #ifdef WIN32 |
| 253 | AioContext *ctxt = qemu_get_aio_context(); |
| 254 | |
Marc-André Lureau | fd3c333 | 2023-02-21 16:47:58 +0400 | [diff] [blame] | 255 | if (WSAEventSelect(fd, event_notifier_get_handle(&ctxt->notifier), |
Marc-André Lureau | faa4ec1 | 2023-02-21 16:47:56 +0400 | [diff] [blame] | 256 | FD_READ | FD_ACCEPT | FD_CLOSE | |
Marc-André Lureau | fd3c333 | 2023-02-21 16:47:58 +0400 | [diff] [blame] | 257 | FD_CONNECT | FD_WRITE | FD_OOB) != 0) { |
| 258 | error_setg_win32(&error_warn, WSAGetLastError(), "failed to WSAEventSelect()"); |
| 259 | } |
Marc-André Lureau | faa4ec1 | 2023-02-21 16:47:56 +0400 | [diff] [blame] | 260 | #endif |
Marc-André Lureau | 848c709 | 2019-01-17 15:43:41 +0400 | [diff] [blame] | 261 | } |
| 262 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 263 | static void net_slirp_unregister_poll_fd(int fd, void *opaque) |
Marc-André Lureau | f6e5aa3 | 2019-01-17 15:43:42 +0400 | [diff] [blame] | 264 | { |
Marc-André Lureau | 21ac728 | 2023-02-21 16:47:57 +0400 | [diff] [blame] | 265 | #ifdef WIN32 |
Marc-André Lureau | fd3c333 | 2023-02-21 16:47:58 +0400 | [diff] [blame] | 266 | if (WSAEventSelect(fd, NULL, 0) != 0) { |
| 267 | error_setg_win32(&error_warn, WSAGetLastError(), "failed to WSAEventSelect()"); |
| 268 | } |
Marc-André Lureau | 21ac728 | 2023-02-21 16:47:57 +0400 | [diff] [blame] | 269 | #endif |
Marc-André Lureau | f6e5aa3 | 2019-01-17 15:43:42 +0400 | [diff] [blame] | 270 | } |
| 271 | |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 272 | static void net_slirp_notify(void *opaque) |
| 273 | { |
| 274 | qemu_notify_event(); |
| 275 | } |
| 276 | |
Marc-André Lureau | d846b92 | 2018-11-14 16:36:07 +0400 | [diff] [blame] | 277 | static const SlirpCb slirp_cb = { |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 278 | .send_packet = net_slirp_send_packet, |
Marc-André Lureau | 2addc8f | 2018-11-14 16:36:33 +0400 | [diff] [blame] | 279 | .guest_error = net_slirp_guest_error, |
Marc-André Lureau | e6dbff3 | 2018-11-22 02:06:28 +0400 | [diff] [blame] | 280 | .clock_get_ns = net_slirp_clock_get_ns, |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 281 | #if SLIRP_CHECK_VERSION(4,7,0) |
| 282 | .init_completed = net_slirp_init_completed, |
| 283 | .timer_new_opaque = net_slirp_timer_new_opaque, |
| 284 | #else |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 285 | .timer_new = net_slirp_timer_new, |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 286 | #endif |
Marc-André Lureau | 07abf6d | 2019-01-17 15:43:37 +0400 | [diff] [blame] | 287 | .timer_free = net_slirp_timer_free, |
| 288 | .timer_mod = net_slirp_timer_mod, |
Marc-André Lureau | 848c709 | 2019-01-17 15:43:41 +0400 | [diff] [blame] | 289 | .register_poll_fd = net_slirp_register_poll_fd, |
Marc-André Lureau | f6e5aa3 | 2019-01-17 15:43:42 +0400 | [diff] [blame] | 290 | .unregister_poll_fd = net_slirp_unregister_poll_fd, |
Marc-André Lureau | 3e0fad3 | 2019-01-17 15:43:58 +0400 | [diff] [blame] | 291 | .notify = net_slirp_notify, |
Marc-André Lureau | d846b92 | 2018-11-14 16:36:07 +0400 | [diff] [blame] | 292 | }; |
| 293 | |
Marc-André Lureau | deaeb3f | 2019-01-17 15:43:57 +0400 | [diff] [blame] | 294 | static int slirp_poll_to_gio(int events) |
| 295 | { |
| 296 | int ret = 0; |
| 297 | |
| 298 | if (events & SLIRP_POLL_IN) { |
| 299 | ret |= G_IO_IN; |
| 300 | } |
| 301 | if (events & SLIRP_POLL_OUT) { |
| 302 | ret |= G_IO_OUT; |
| 303 | } |
| 304 | if (events & SLIRP_POLL_PRI) { |
| 305 | ret |= G_IO_PRI; |
| 306 | } |
| 307 | if (events & SLIRP_POLL_ERR) { |
| 308 | ret |= G_IO_ERR; |
| 309 | } |
| 310 | if (events & SLIRP_POLL_HUP) { |
| 311 | ret |= G_IO_HUP; |
| 312 | } |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static int net_slirp_add_poll(int fd, int events, void *opaque) |
| 318 | { |
| 319 | GArray *pollfds = opaque; |
| 320 | GPollFD pfd = { |
| 321 | .fd = fd, |
| 322 | .events = slirp_poll_to_gio(events), |
| 323 | }; |
| 324 | int idx = pollfds->len; |
| 325 | g_array_append_val(pollfds, pfd); |
| 326 | return idx; |
| 327 | } |
| 328 | |
| 329 | static int slirp_gio_to_poll(int events) |
| 330 | { |
| 331 | int ret = 0; |
| 332 | |
| 333 | if (events & G_IO_IN) { |
| 334 | ret |= SLIRP_POLL_IN; |
| 335 | } |
| 336 | if (events & G_IO_OUT) { |
| 337 | ret |= SLIRP_POLL_OUT; |
| 338 | } |
| 339 | if (events & G_IO_PRI) { |
| 340 | ret |= SLIRP_POLL_PRI; |
| 341 | } |
| 342 | if (events & G_IO_ERR) { |
| 343 | ret |= SLIRP_POLL_ERR; |
| 344 | } |
| 345 | if (events & G_IO_HUP) { |
| 346 | ret |= SLIRP_POLL_HUP; |
| 347 | } |
| 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | static int net_slirp_get_revents(int idx, void *opaque) |
| 353 | { |
| 354 | GArray *pollfds = opaque; |
| 355 | |
| 356 | return slirp_gio_to_poll(g_array_index(pollfds, GPollFD, idx).revents); |
| 357 | } |
| 358 | |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 359 | static void net_slirp_poll_notify(Notifier *notifier, void *data) |
| 360 | { |
| 361 | MainLoopPoll *poll = data; |
| 362 | SlirpState *s = container_of(notifier, SlirpState, poll_notifier); |
| 363 | |
| 364 | switch (poll->state) { |
| 365 | case MAIN_LOOP_POLL_FILL: |
Marc-André Lureau | deaeb3f | 2019-01-17 15:43:57 +0400 | [diff] [blame] | 366 | slirp_pollfds_fill(s->slirp, &poll->timeout, |
| 367 | net_slirp_add_poll, poll->pollfds); |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 368 | break; |
| 369 | case MAIN_LOOP_POLL_OK: |
| 370 | case MAIN_LOOP_POLL_ERR: |
Marc-André Lureau | deaeb3f | 2019-01-17 15:43:57 +0400 | [diff] [blame] | 371 | slirp_pollfds_poll(s->slirp, poll->state == MAIN_LOOP_POLL_ERR, |
| 372 | net_slirp_get_revents, poll->pollfds); |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 373 | break; |
| 374 | default: |
| 375 | g_assert_not_reached(); |
| 376 | } |
| 377 | } |
| 378 | |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 379 | static ssize_t |
| 380 | net_slirp_stream_read(void *buf, size_t size, void *opaque) |
| 381 | { |
| 382 | QEMUFile *f = opaque; |
| 383 | |
| 384 | return qemu_get_buffer(f, buf, size); |
| 385 | } |
| 386 | |
| 387 | static ssize_t |
| 388 | net_slirp_stream_write(const void *buf, size_t size, void *opaque) |
| 389 | { |
| 390 | QEMUFile *f = opaque; |
| 391 | |
| 392 | qemu_put_buffer(f, buf, size); |
| 393 | if (qemu_file_get_error(f)) { |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | return size; |
| 398 | } |
| 399 | |
| 400 | static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id) |
| 401 | { |
| 402 | Slirp *slirp = opaque; |
| 403 | |
| 404 | return slirp_state_load(slirp, version_id, net_slirp_stream_read, f); |
| 405 | } |
| 406 | |
| 407 | static void net_slirp_state_save(QEMUFile *f, void *opaque) |
| 408 | { |
| 409 | Slirp *slirp = opaque; |
| 410 | |
| 411 | slirp_state_save(slirp, net_slirp_stream_write, f); |
| 412 | } |
| 413 | |
| 414 | static SaveVMHandlers savevm_slirp_state = { |
| 415 | .save_state = net_slirp_state_save, |
| 416 | .load_state = net_slirp_state_load, |
| 417 | }; |
| 418 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 419 | static int net_slirp_init(NetClientState *peer, const char *model, |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 420 | const char *name, int restricted, |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 421 | bool ipv4, const char *vnetwork, const char *vhost, |
| 422 | bool ipv6, const char *vprefix6, int vprefix6_len, |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 423 | const char *vhost6, |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 424 | const char *vhostname, const char *tftp_export, |
| 425 | const char *bootfile, const char *vdhcp_start, |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 426 | const char *vnameserver, const char *vnameserver6, |
| 427 | const char *smb_export, const char *vsmbserver, |
Benjamin Drung | f18d137 | 2018-02-27 17:06:01 +0100 | [diff] [blame] | 428 | const char **dnssearch, const char *vdomainname, |
Fam Zheng | 0fca92b | 2018-09-14 15:26:16 +0800 | [diff] [blame] | 429 | const char *tftp_server_name, |
Benjamin Drung | f18d137 | 2018-02-27 17:06:01 +0100 | [diff] [blame] | 430 | Error **errp) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 431 | { |
| 432 | /* default settings according to historic slirp */ |
| 433 | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ |
| 434 | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ |
| 435 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ |
| 436 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ |
| 437 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 438 | struct in6_addr ip6_prefix; |
| 439 | struct in6_addr ip6_host; |
| 440 | struct in6_addr ip6_dns; |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 441 | #if defined(CONFIG_SMBD_COMMAND) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 442 | struct in_addr smbsrv = { .s_addr = 0 }; |
| 443 | #endif |
Paolo Bonzini | bce63de | 2022-04-11 10:16:36 +0200 | [diff] [blame] | 444 | SlirpConfig cfg = { 0 }; |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 445 | NetClientState *nc; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 446 | SlirpState *s; |
| 447 | char buf[20]; |
| 448 | uint32_t addr; |
| 449 | int shift; |
| 450 | char *end; |
| 451 | struct slirp_config_str *config; |
| 452 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 453 | if (!ipv4 && (vnetwork || vhost || vnameserver)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 454 | error_setg(errp, "IPv4 disabled but netmask/host/dns provided"); |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 459 | error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided"); |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | if (!ipv4 && !ipv6) { |
| 464 | /* It doesn't make sense to disable both */ |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 465 | error_setg(errp, "IPv4 and IPv6 disabled"); |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 466 | return -1; |
| 467 | } |
| 468 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 469 | if (vnetwork) { |
| 470 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { |
| 471 | if (!inet_aton(vnetwork, &net)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 472 | error_setg(errp, "Failed to parse netmask"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | addr = ntohl(net.s_addr); |
| 476 | if (!(addr & 0x80000000)) { |
| 477 | mask.s_addr = htonl(0xff000000); /* class A */ |
| 478 | } else if ((addr & 0xfff00000) == 0xac100000) { |
| 479 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ |
| 480 | } else if ((addr & 0xc0000000) == 0x80000000) { |
| 481 | mask.s_addr = htonl(0xffff0000); /* class B */ |
| 482 | } else if ((addr & 0xffff0000) == 0xc0a80000) { |
| 483 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ |
| 484 | } else if ((addr & 0xffff0000) == 0xc6120000) { |
| 485 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ |
| 486 | } else if ((addr & 0xe0000000) == 0xe0000000) { |
| 487 | mask.s_addr = htonl(0xffffff00); /* class C */ |
| 488 | } else { |
| 489 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ |
| 490 | } |
| 491 | } else { |
| 492 | if (!inet_aton(buf, &net)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 493 | error_setg(errp, "Failed to parse netmask"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 494 | return -1; |
| 495 | } |
| 496 | shift = strtol(vnetwork, &end, 10); |
| 497 | if (*end != '\0') { |
| 498 | if (!inet_aton(vnetwork, &mask)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 499 | error_setg(errp, |
| 500 | "Failed to parse netmask (trailing chars)"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 501 | return -1; |
| 502 | } |
| 503 | } else if (shift < 4 || shift > 32) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 504 | error_setg(errp, |
| 505 | "Invalid netmask provided (must be in range 4-32)"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 506 | return -1; |
| 507 | } else { |
| 508 | mask.s_addr = htonl(0xffffffff << (32 - shift)); |
| 509 | } |
| 510 | } |
| 511 | net.s_addr &= mask.s_addr; |
| 512 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); |
| 513 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); |
| 514 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); |
| 515 | } |
| 516 | |
| 517 | if (vhost && !inet_aton(vhost, &host)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 518 | error_setg(errp, "Failed to parse host"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 519 | return -1; |
| 520 | } |
| 521 | if ((host.s_addr & mask.s_addr) != net.s_addr) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 522 | error_setg(errp, "Host doesn't belong to network"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 523 | return -1; |
| 524 | } |
| 525 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 526 | if (vnameserver && !inet_aton(vnameserver, &dns)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 527 | error_setg(errp, "Failed to parse DNS"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 528 | return -1; |
| 529 | } |
Samuel Thibault | 120b721 | 2019-09-29 20:08:20 +0200 | [diff] [blame] | 530 | if (restricted && (dns.s_addr & mask.s_addr) != net.s_addr) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 531 | error_setg(errp, "DNS doesn't belong to network"); |
| 532 | return -1; |
| 533 | } |
| 534 | if (dns.s_addr == host.s_addr) { |
| 535 | error_setg(errp, "DNS must be different from host"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 536 | return -1; |
| 537 | } |
| 538 | |
Bas van Sisseren | 68756ba | 2013-06-03 15:11:49 +0200 | [diff] [blame] | 539 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 540 | error_setg(errp, "Failed to parse DHCP start address"); |
Bas van Sisseren | 68756ba | 2013-06-03 15:11:49 +0200 | [diff] [blame] | 541 | return -1; |
| 542 | } |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 543 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr) { |
| 544 | error_setg(errp, "DHCP doesn't belong to network"); |
| 545 | return -1; |
| 546 | } |
| 547 | if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { |
Doug Evans | 0c373c0 | 2021-01-21 16:42:51 -0800 | [diff] [blame] | 548 | error_setg(errp, "DHCP must be different from host and DNS"); |
Bas van Sisseren | 68756ba | 2013-06-03 15:11:49 +0200 | [diff] [blame] | 549 | return -1; |
| 550 | } |
| 551 | |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 552 | #if defined(CONFIG_SMBD_COMMAND) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 553 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 554 | error_setg(errp, "Failed to parse SMB address"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 555 | return -1; |
| 556 | } |
| 557 | #endif |
| 558 | |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 559 | if (!vprefix6) { |
| 560 | vprefix6 = "fec0::"; |
| 561 | } |
| 562 | if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 563 | error_setg(errp, "Failed to parse IPv6 prefix"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 564 | return -1; |
| 565 | } |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 566 | |
| 567 | if (!vprefix6_len) { |
| 568 | vprefix6_len = 64; |
| 569 | } |
| 570 | if (vprefix6_len < 0 || vprefix6_len > 126) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 571 | error_setg(errp, |
Stefano Garzarella | 178a0a5 | 2019-05-15 11:08:05 +0200 | [diff] [blame] | 572 | "Invalid IPv6 prefix provided " |
| 573 | "(IPv6 prefix length must be between 0 and 126)"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | if (vhost6) { |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 578 | if (!inet_pton(AF_INET6, vhost6, &ip6_host)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 579 | error_setg(errp, "Failed to parse IPv6 host"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 583 | error_setg(errp, "IPv6 Host doesn't belong to network"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 584 | return -1; |
| 585 | } |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 586 | } else { |
| 587 | ip6_host = ip6_prefix; |
| 588 | ip6_host.s6_addr[15] |= 2; |
| 589 | } |
| 590 | |
| 591 | if (vnameserver6) { |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 592 | if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 593 | error_setg(errp, "Failed to parse IPv6 DNS"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 594 | return -1; |
| 595 | } |
Samuel Thibault | 120b721 | 2019-09-29 20:08:20 +0200 | [diff] [blame] | 596 | if (restricted && !in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 597 | error_setg(errp, "IPv6 DNS doesn't belong to network"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 598 | return -1; |
| 599 | } |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 600 | } else { |
| 601 | ip6_dns = ip6_prefix; |
| 602 | ip6_dns.s6_addr[15] |= 3; |
| 603 | } |
| 604 | |
Benjamin Drung | f18d137 | 2018-02-27 17:06:01 +0100 | [diff] [blame] | 605 | if (vdomainname && !*vdomainname) { |
| 606 | error_setg(errp, "'domainname' parameter cannot be empty"); |
| 607 | return -1; |
| 608 | } |
| 609 | |
Fam Zheng | 6e157a0 | 2018-09-14 15:26:15 +0800 | [diff] [blame] | 610 | if (vdomainname && strlen(vdomainname) > 255) { |
| 611 | error_setg(errp, "'domainname' parameter cannot exceed 255 bytes"); |
| 612 | return -1; |
| 613 | } |
| 614 | |
| 615 | if (vhostname && strlen(vhostname) > 255) { |
| 616 | error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes"); |
| 617 | return -1; |
| 618 | } |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 619 | |
Fam Zheng | 0fca92b | 2018-09-14 15:26:16 +0800 | [diff] [blame] | 620 | if (tftp_server_name && strlen(tftp_server_name) > 255) { |
| 621 | error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes"); |
| 622 | return -1; |
| 623 | } |
| 624 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 625 | nc = qemu_new_net_client(&net_slirp_info, peer, model, name); |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 626 | |
Laurent Vivier | 53b85d9 | 2022-10-21 11:09:10 +0200 | [diff] [blame] | 627 | qemu_set_info_str(nc, "net=%s,restrict=%s", inet_ntoa(net), |
| 628 | restricted ? "on" : "off"); |
Jason Wang | d89b4f8 | 2021-04-02 11:03:12 +0800 | [diff] [blame] | 629 | |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 630 | s = DO_UPCAST(SlirpState, nc, nc); |
| 631 | |
Paolo Bonzini | 6222e55 | 2022-04-11 09:39:16 +0200 | [diff] [blame] | 632 | cfg.version = SLIRP_CHECK_VERSION(4,7,0) ? 4 : 1; |
Paolo Bonzini | bce63de | 2022-04-11 10:16:36 +0200 | [diff] [blame] | 633 | cfg.restricted = restricted; |
| 634 | cfg.in_enabled = ipv4; |
| 635 | cfg.vnetwork = net; |
| 636 | cfg.vnetmask = mask; |
| 637 | cfg.vhost = host; |
| 638 | cfg.in6_enabled = ipv6; |
| 639 | cfg.vprefix_addr6 = ip6_prefix; |
| 640 | cfg.vprefix_len = vprefix6_len; |
| 641 | cfg.vhost6 = ip6_host; |
| 642 | cfg.vhostname = vhostname; |
| 643 | cfg.tftp_server_name = tftp_server_name; |
| 644 | cfg.tftp_path = tftp_export; |
| 645 | cfg.bootfile = bootfile; |
| 646 | cfg.vdhcp_start = dhcp; |
| 647 | cfg.vnameserver = dns; |
| 648 | cfg.vnameserver6 = ip6_dns; |
| 649 | cfg.vdnssearch = dnssearch; |
| 650 | cfg.vdomainname = vdomainname; |
| 651 | s->slirp = slirp_new(&cfg, &slirp_cb, s); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 652 | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); |
| 653 | |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 654 | /* |
| 655 | * Make sure the current bitstream version of slirp is 4, to avoid |
| 656 | * QEMU migration incompatibilities, if upstream slirp bumped the |
| 657 | * version. |
| 658 | * |
| 659 | * FIXME: use bitfields of features? teach libslirp to save with |
| 660 | * specific version? |
| 661 | */ |
| 662 | g_assert(slirp_state_version() == 4); |
Juan Quintela | b23db4c | 2023-10-20 11:07:23 +0200 | [diff] [blame] | 663 | register_savevm_live("slirp", VMSTATE_INSTANCE_ID_ANY, |
| 664 | slirp_state_version(), &savevm_slirp_state, s->slirp); |
Marc-André Lureau | d890344 | 2019-02-12 17:25:19 +0100 | [diff] [blame] | 665 | |
Marc-André Lureau | 1ab67b9 | 2019-01-17 15:43:55 +0400 | [diff] [blame] | 666 | s->poll_notifier.notify = net_slirp_poll_notify; |
| 667 | main_loop_poll_add_notifier(&s->poll_notifier); |
| 668 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 669 | for (config = slirp_configs; config; config = config->next) { |
| 670 | if (config->flags & SLIRP_CFG_HOSTFWD) { |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 671 | if (slirp_hostfwd(s, config->str, errp) < 0) { |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 672 | goto error; |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 673 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 674 | } else { |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 675 | if (slirp_guestfwd(s, config->str, errp) < 0) { |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 676 | goto error; |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 677 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 680 | #if defined(CONFIG_SMBD_COMMAND) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 681 | if (smb_export) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 682 | if (slirp_smb(s, smb_export, smbsrv, errp) < 0) { |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 683 | goto error; |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 684 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 685 | } |
| 686 | #endif |
| 687 | |
Paolo Bonzini | f6c2e66 | 2016-07-12 09:57:12 +0200 | [diff] [blame] | 688 | s->exit_notifier.notify = slirp_smb_exit; |
| 689 | qemu_add_exit_notifier(&s->exit_notifier); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 690 | return 0; |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 691 | |
| 692 | error: |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 693 | qemu_del_net_client(nc); |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 694 | return -1; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 697 | static SlirpState *slirp_lookup(Monitor *mon, const char *id) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 698 | { |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 699 | if (id) { |
| 700 | NetClientState *nc = qemu_find_netdev(id); |
| 701 | if (!nc) { |
| 702 | monitor_printf(mon, "unrecognized netdev id '%s'\n", id); |
| 703 | return NULL; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 704 | } |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 705 | if (strcmp(nc->model, "user")) { |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 706 | monitor_printf(mon, "invalid device specified\n"); |
| 707 | return NULL; |
| 708 | } |
Mark McLoughlin | ce20b5b | 2009-11-25 18:49:06 +0000 | [diff] [blame] | 709 | return DO_UPCAST(SlirpState, nc, nc); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 710 | } else { |
| 711 | if (QTAILQ_EMPTY(&slirp_stacks)) { |
| 712 | monitor_printf(mon, "user mode network stack not in use\n"); |
| 713 | return NULL; |
| 714 | } |
| 715 | return QTAILQ_FIRST(&slirp_stacks); |
| 716 | } |
| 717 | } |
| 718 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 719 | void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 720 | { |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 721 | struct sockaddr_in host_addr = { |
| 722 | .sin_family = AF_INET, |
| 723 | .sin_addr = { |
| 724 | .s_addr = INADDR_ANY, |
| 725 | }, |
| 726 | }; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 727 | int host_port; |
Markus Armbruster | e30e5eb | 2011-11-16 15:45:59 +0100 | [diff] [blame] | 728 | char buf[256]; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 729 | const char *src_str, *p; |
| 730 | SlirpState *s; |
| 731 | int is_udp = 0; |
| 732 | int err; |
| 733 | const char *arg1 = qdict_get_str(qdict, "arg1"); |
| 734 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 735 | |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 736 | if (arg2) { |
| 737 | s = slirp_lookup(mon, arg1); |
Thomas Huth | 9365306 | 2018-01-11 21:02:40 +0100 | [diff] [blame] | 738 | src_str = arg2; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 739 | } else { |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 740 | s = slirp_lookup(mon, NULL); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 741 | src_str = arg1; |
| 742 | } |
| 743 | if (!s) { |
| 744 | return; |
| 745 | } |
| 746 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 747 | p = src_str; |
Markus Armbruster | e30e5eb | 2011-11-16 15:45:59 +0100 | [diff] [blame] | 748 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 749 | goto fail_syntax; |
| 750 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 751 | |
| 752 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
| 753 | is_udp = 0; |
| 754 | } else if (!strcmp(buf, "udp")) { |
| 755 | is_udp = 1; |
| 756 | } else { |
| 757 | goto fail_syntax; |
| 758 | } |
| 759 | |
| 760 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 761 | goto fail_syntax; |
| 762 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 763 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr.sin_addr)) { |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 764 | goto fail_syntax; |
| 765 | } |
| 766 | |
Nia Alarie | 1fb3f7f | 2018-03-16 14:39:21 +0000 | [diff] [blame] | 767 | if (qemu_strtoi(p, NULL, 10, &host_port)) { |
| 768 | goto fail_syntax; |
| 769 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 770 | host_addr.sin_port = htons(host_port); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 771 | |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 772 | #if SLIRP_CHECK_VERSION(4, 5, 0) |
| 773 | err = slirp_remove_hostxfwd(s->slirp, (struct sockaddr *) &host_addr, |
| 774 | sizeof(host_addr), is_udp ? SLIRP_HOSTFWD_UDP : 0); |
| 775 | #else |
| 776 | err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr.sin_addr, host_port); |
| 777 | #endif |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 778 | |
| 779 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, |
Geoffrey Thomas | b15ba6c | 2011-12-17 04:23:59 -0500 | [diff] [blame] | 780 | err ? "not found" : "removed"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 781 | return; |
| 782 | |
| 783 | fail_syntax: |
| 784 | monitor_printf(mon, "invalid format\n"); |
| 785 | } |
| 786 | |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 787 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 788 | { |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 789 | struct sockaddr_in host_addr = { |
| 790 | .sin_family = AF_INET, |
| 791 | .sin_addr = { |
| 792 | .s_addr = INADDR_ANY, |
| 793 | }, |
| 794 | }; |
| 795 | struct sockaddr_in guest_addr = { |
| 796 | .sin_family = AF_INET, |
| 797 | .sin_addr = { |
| 798 | .s_addr = 0, |
| 799 | }, |
| 800 | }; |
| 801 | int err; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 802 | int host_port, guest_port; |
| 803 | const char *p; |
| 804 | char buf[256]; |
| 805 | int is_udp; |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 806 | const char *end; |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 807 | const char *fail_reason = "Unknown reason"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 808 | |
| 809 | p = redir_str; |
| 810 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 811 | fail_reason = "No : separators"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 812 | goto fail_syntax; |
| 813 | } |
| 814 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
| 815 | is_udp = 0; |
| 816 | } else if (!strcmp(buf, "udp")) { |
| 817 | is_udp = 1; |
| 818 | } else { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 819 | fail_reason = "Bad protocol name"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 820 | goto fail_syntax; |
| 821 | } |
| 822 | |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 823 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 824 | fail_reason = "Missing : separator"; |
| 825 | goto fail_syntax; |
| 826 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 827 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr.sin_addr)) { |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 828 | fail_reason = "Bad host address"; |
| 829 | goto fail_syntax; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 832 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 833 | fail_reason = "Bad host port separator"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 834 | goto fail_syntax; |
| 835 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 836 | err = qemu_strtoi(buf, &end, 0, &host_port); |
| 837 | if (err || host_port < 0 || host_port > 65535) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 838 | fail_reason = "Bad host port"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 839 | goto fail_syntax; |
| 840 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 841 | host_addr.sin_port = htons(host_port); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 842 | |
| 843 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 844 | fail_reason = "Missing guest address"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 845 | goto fail_syntax; |
| 846 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 847 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr.sin_addr)) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 848 | fail_reason = "Bad guest address"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 849 | goto fail_syntax; |
| 850 | } |
| 851 | |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 852 | err = qemu_strtoi(p, &end, 0, &guest_port); |
| 853 | if (err || guest_port < 1 || guest_port > 65535) { |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 854 | fail_reason = "Bad guest port"; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 855 | goto fail_syntax; |
| 856 | } |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 857 | guest_addr.sin_port = htons(guest_port); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 858 | |
Nicholas Ngai | 0355519 | 2021-09-25 14:48:20 -0700 | [diff] [blame] | 859 | #if SLIRP_CHECK_VERSION(4, 5, 0) |
| 860 | err = slirp_add_hostxfwd(s->slirp, |
| 861 | (struct sockaddr *) &host_addr, sizeof(host_addr), |
| 862 | (struct sockaddr *) &guest_addr, sizeof(guest_addr), |
| 863 | is_udp ? SLIRP_HOSTFWD_UDP : 0); |
| 864 | #else |
| 865 | err = slirp_add_hostfwd(s->slirp, is_udp, |
| 866 | host_addr.sin_addr, host_port, |
| 867 | guest_addr.sin_addr, guest_port); |
| 868 | #endif |
| 869 | |
| 870 | if (err < 0) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 871 | error_setg(errp, "Could not set up host forwarding rule '%s'", |
| 872 | redir_str); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 873 | return -1; |
| 874 | } |
| 875 | return 0; |
| 876 | |
| 877 | fail_syntax: |
Dr. David Alan Gilbert | 0e7e4fb | 2017-09-08 16:53:59 +0100 | [diff] [blame] | 878 | error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str, |
| 879 | fail_reason); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 880 | return -1; |
| 881 | } |
| 882 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 883 | void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 884 | { |
| 885 | const char *redir_str; |
| 886 | SlirpState *s; |
| 887 | const char *arg1 = qdict_get_str(qdict, "arg1"); |
| 888 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 889 | |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 890 | if (arg2) { |
| 891 | s = slirp_lookup(mon, arg1); |
Thomas Huth | 9365306 | 2018-01-11 21:02:40 +0100 | [diff] [blame] | 892 | redir_str = arg2; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 893 | } else { |
Thomas Huth | b4983c5 | 2019-12-05 11:41:09 +0100 | [diff] [blame] | 894 | s = slirp_lookup(mon, NULL); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 895 | redir_str = arg1; |
| 896 | } |
| 897 | if (s) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 898 | Error *err = NULL; |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 899 | if (slirp_hostfwd(s, redir_str, &err) < 0) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 900 | error_report_err(err); |
| 901 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | } |
| 905 | |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 906 | #if defined(CONFIG_SMBD_COMMAND) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 907 | |
| 908 | /* automatic user mode samba server configuration */ |
| 909 | static void slirp_smb_cleanup(SlirpState *s) |
| 910 | { |
Kirill A. Shutemov | 5a01e99 | 2010-01-20 00:56:16 +0100 | [diff] [blame] | 911 | int ret; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 912 | |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 913 | if (s->smb_dir) { |
| 914 | gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir); |
Kirill A. Shutemov | 5a01e99 | 2010-01-20 00:56:16 +0100 | [diff] [blame] | 915 | ret = system(cmd); |
Juan Quintela | 24ac07d | 2010-03-04 10:00:31 +0100 | [diff] [blame] | 916 | if (ret == -1 || !WIFEXITED(ret)) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 917 | error_report("'%s' failed.", cmd); |
Kirill A. Shutemov | 5a01e99 | 2010-01-20 00:56:16 +0100 | [diff] [blame] | 918 | } else if (WEXITSTATUS(ret)) { |
Markus Armbruster | 1ecda02 | 2010-02-18 17:25:24 +0100 | [diff] [blame] | 919 | error_report("'%s' failed. Error code: %d", |
| 920 | cmd, WEXITSTATUS(ret)); |
Kirill A. Shutemov | 5a01e99 | 2010-01-20 00:56:16 +0100 | [diff] [blame] | 921 | } |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 922 | g_free(cmd); |
| 923 | g_free(s->smb_dir); |
| 924 | s->smb_dir = NULL; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | static int slirp_smb(SlirpState* s, const char *exported_dir, |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 929 | struct in_addr vserver_addr, Error **errp) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 930 | { |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 931 | char *smb_conf; |
| 932 | char *smb_cmdline; |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 933 | struct passwd *passwd; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 934 | FILE *f; |
| 935 | |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 936 | passwd = getpwuid(geteuid()); |
| 937 | if (!passwd) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 938 | error_setg(errp, "Failed to retrieve user name"); |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 939 | return -1; |
| 940 | } |
| 941 | |
Dunrong Huang | 927d811 | 2012-07-06 14:04:43 +0800 | [diff] [blame] | 942 | if (access(CONFIG_SMBD_COMMAND, F_OK)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 943 | error_setg(errp, "Could not find '%s', please install it", |
| 944 | CONFIG_SMBD_COMMAND); |
Dunrong Huang | 927d811 | 2012-07-06 14:04:43 +0800 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | if (access(exported_dir, R_OK | X_OK)) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 949 | error_setg(errp, "Error accessing shared directory '%s': %s", |
| 950 | exported_dir, strerror(errno)); |
Dunrong Huang | 927d811 | 2012-07-06 14:04:43 +0800 | [diff] [blame] | 951 | return -1; |
| 952 | } |
| 953 | |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 954 | s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL); |
| 955 | if (!s->smb_dir) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 956 | error_setg(errp, "Could not create samba server dir"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 957 | return -1; |
| 958 | } |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 959 | smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 960 | |
| 961 | f = fopen(smb_conf, "w"); |
| 962 | if (!f) { |
| 963 | slirp_smb_cleanup(s); |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 964 | error_setg(errp, |
| 965 | "Could not create samba server configuration file '%s'", |
| 966 | smb_conf); |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 967 | g_free(smb_conf); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 968 | return -1; |
| 969 | } |
| 970 | fprintf(f, |
| 971 | "[global]\n" |
| 972 | "private dir=%s\n" |
Peter Wu | 7912d04 | 2014-11-03 11:52:10 +0100 | [diff] [blame] | 973 | "interfaces=127.0.0.1\n" |
| 974 | "bind interfaces only=yes\n" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 975 | "pid directory=%s\n" |
| 976 | "lock directory=%s\n" |
Nikolaus Rath | 276eda5 | 2012-04-25 09:57:19 -0400 | [diff] [blame] | 977 | "state directory=%s\n" |
Peter Wu | 7912d04 | 2014-11-03 11:52:10 +0100 | [diff] [blame] | 978 | "cache directory=%s\n" |
Michael Buesch | b87b8a8 | 2014-04-27 14:54:12 +0400 | [diff] [blame] | 979 | "ncalrpc dir=%s/ncalrpc\n" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 980 | "log file=%s/log.smbd\n" |
| 981 | "smb passwd file=%s/smbpasswd\n" |
Michael Buesch | c2804ee | 2013-11-01 12:23:49 +0100 | [diff] [blame] | 982 | "security = user\n" |
| 983 | "map to guest = Bad User\n" |
Peter Wu | 7912d04 | 2014-11-03 11:52:10 +0100 | [diff] [blame] | 984 | "load printers = no\n" |
| 985 | "printing = bsd\n" |
| 986 | "disable spoolss = yes\n" |
| 987 | "usershare max shares = 0\n" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 988 | "[qemu]\n" |
| 989 | "path=%s\n" |
| 990 | "read only=no\n" |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 991 | "guest ok=yes\n" |
| 992 | "force user=%s\n", |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 993 | s->smb_dir, |
| 994 | s->smb_dir, |
| 995 | s->smb_dir, |
| 996 | s->smb_dir, |
| 997 | s->smb_dir, |
Nikolaus Rath | 276eda5 | 2012-04-25 09:57:19 -0400 | [diff] [blame] | 998 | s->smb_dir, |
Michael Buesch | b87b8a8 | 2014-04-27 14:54:12 +0400 | [diff] [blame] | 999 | s->smb_dir, |
Peter Wu | 7912d04 | 2014-11-03 11:52:10 +0100 | [diff] [blame] | 1000 | s->smb_dir, |
Jan Kiszka | 1cb1c5d | 2012-07-05 19:35:57 +0200 | [diff] [blame] | 1001 | exported_dir, |
| 1002 | passwd->pw_name |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1003 | ); |
| 1004 | fclose(f); |
| 1005 | |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 1006 | smb_cmdline = g_strdup_printf("%s -l %s -s %s", |
Michael Tokarev | 44d8d2b | 2014-10-25 00:29:50 +0400 | [diff] [blame] | 1007 | CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf); |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 1008 | g_free(smb_conf); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1009 | |
Marc-André Lureau | 44b4ff2 | 2019-01-17 15:43:33 +0400 | [diff] [blame] | 1010 | if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 || |
| 1011 | slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) { |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1012 | slirp_smb_cleanup(s); |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 1013 | g_free(smb_cmdline); |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1014 | error_setg(errp, "Conflicting/invalid smbserver address"); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1015 | return -1; |
| 1016 | } |
Dr. David Alan Gilbert | f95cc8b | 2017-04-07 15:32:54 +0100 | [diff] [blame] | 1017 | g_free(smb_cmdline); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
Paolo Bonzini | 35acbb3 | 2021-10-13 13:43:36 +0200 | [diff] [blame] | 1021 | #endif /* defined(CONFIG_SMBD_COMMAND) */ |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1022 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1023 | static int guestfwd_can_read(void *opaque) |
| 1024 | { |
| 1025 | struct GuestFwd *fwd = opaque; |
| 1026 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); |
| 1027 | } |
| 1028 | |
| 1029 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) |
| 1030 | { |
| 1031 | struct GuestFwd *fwd = opaque; |
| 1032 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); |
| 1033 | } |
| 1034 | |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 1035 | static ssize_t guestfwd_write(const void *buf, size_t len, void *chr) |
Marc-André Lureau | 44b4ff2 | 2019-01-17 15:43:33 +0400 | [diff] [blame] | 1036 | { |
| 1037 | return qemu_chr_fe_write_all(chr, buf, len); |
| 1038 | } |
| 1039 | |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 1040 | static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1041 | { |
Samuel Thibault | ffe02f5 | 2016-04-01 00:46:35 +0200 | [diff] [blame] | 1042 | /* TODO: IPv6 */ |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1043 | struct in_addr server = { .s_addr = 0 }; |
| 1044 | struct GuestFwd *fwd; |
| 1045 | const char *p; |
| 1046 | char buf[128]; |
| 1047 | char *end; |
| 1048 | int port; |
| 1049 | |
| 1050 | p = config_str; |
Thomas Huth | d18572d | 2018-08-22 15:43:30 +0200 | [diff] [blame] | 1051 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1052 | goto fail_syntax; |
| 1053 | } |
| 1054 | if (strcmp(buf, "tcp") && buf[0] != '\0') { |
| 1055 | goto fail_syntax; |
| 1056 | } |
| 1057 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1058 | goto fail_syntax; |
| 1059 | } |
| 1060 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { |
| 1061 | goto fail_syntax; |
| 1062 | } |
| 1063 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { |
| 1064 | goto fail_syntax; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1065 | } |
| 1066 | port = strtol(buf, &end, 10); |
| 1067 | if (*end != '\0' || port < 1 || port > 65535) { |
| 1068 | goto fail_syntax; |
| 1069 | } |
| 1070 | |
Alexander Graf | a989999 | 2011-06-04 07:25:59 +0200 | [diff] [blame] | 1071 | snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1072 | |
Marc-André Lureau | 3624730 | 2019-01-17 15:43:34 +0400 | [diff] [blame] | 1073 | if (g_str_has_prefix(p, "cmd:")) { |
Marc-André Lureau | 44b4ff2 | 2019-01-17 15:43:33 +0400 | [diff] [blame] | 1074 | if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1075 | error_setg(errp, "Conflicting/invalid host:port in guest " |
| 1076 | "forwarding rule '%s'", config_str); |
Alexander Graf | b412eb6 | 2012-06-03 09:45:01 +0200 | [diff] [blame] | 1077 | return -1; |
| 1078 | } |
| 1079 | } else { |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 1080 | Error *err = NULL; |
Marc-André Lureau | 95e30b2 | 2018-08-22 19:19:42 +0200 | [diff] [blame] | 1081 | /* |
| 1082 | * FIXME: sure we want to support implicit |
| 1083 | * muxed monitors here? |
| 1084 | */ |
Paolo Bonzini | 4ad6f6c | 2019-02-13 14:18:13 +0100 | [diff] [blame] | 1085 | Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 1086 | |
| 1087 | if (!chr) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1088 | error_setg(errp, "Could not open guest forwarding device '%s'", |
| 1089 | buf); |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 1090 | return -1; |
| 1091 | } |
| 1092 | |
| 1093 | fwd = g_new(struct GuestFwd, 1); |
| 1094 | qemu_chr_fe_init(&fwd->hd, chr, &err); |
| 1095 | if (err) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1096 | error_propagate(errp, err); |
Marc-André Lureau | 8e207c3 | 2019-01-17 15:43:36 +0400 | [diff] [blame] | 1097 | object_unparent(OBJECT(chr)); |
Alexander Graf | b412eb6 | 2012-06-03 09:45:01 +0200 | [diff] [blame] | 1098 | g_free(fwd); |
| 1099 | return -1; |
| 1100 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1101 | |
Marc-André Lureau | 44b4ff2 | 2019-01-17 15:43:33 +0400 | [diff] [blame] | 1102 | if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd, |
| 1103 | &server, port) < 0) { |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1104 | error_setg(errp, "Conflicting/invalid host:port in guest " |
| 1105 | "forwarding rule '%s'", config_str); |
Marc-André Lureau | 8e207c3 | 2019-01-17 15:43:36 +0400 | [diff] [blame] | 1106 | qemu_chr_fe_deinit(&fwd->hd, true); |
Alexander Graf | b412eb6 | 2012-06-03 09:45:01 +0200 | [diff] [blame] | 1107 | g_free(fwd); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | fwd->server = server; |
| 1111 | fwd->port = port; |
| 1112 | fwd->slirp = s->slirp; |
| 1113 | |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 1114 | qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 1115 | NULL, NULL, fwd, NULL, true); |
Marc-André Lureau | 8d45a3b | 2019-01-17 15:43:35 +0400 | [diff] [blame] | 1116 | s->fwd = g_slist_append(s->fwd, fwd); |
Alexander Graf | b412eb6 | 2012-06-03 09:45:01 +0200 | [diff] [blame] | 1117 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1118 | return 0; |
| 1119 | |
| 1120 | fail_syntax: |
Hervé Poussineau | 5c843af | 2017-07-15 18:43:50 +0200 | [diff] [blame] | 1121 | error_setg(errp, "Invalid guest forwarding rule '%s'", config_str); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
| 1124 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1125 | void hmp_info_usernet(Monitor *mon, const QDict *qdict) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1126 | { |
| 1127 | SlirpState *s; |
| 1128 | |
| 1129 | QTAILQ_FOREACH(s, &slirp_stacks, entry) { |
Stefan Hajnoczi | 90d87a3 | 2012-07-24 16:35:06 +0100 | [diff] [blame] | 1130 | int id; |
Thomas Huth | 442da40 | 2018-04-30 20:02:24 +0200 | [diff] [blame] | 1131 | bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0; |
Marc-André Lureau | b7f43bf | 2018-11-10 17:45:43 +0400 | [diff] [blame] | 1132 | char *info = slirp_connection_info(s->slirp); |
| 1133 | monitor_printf(mon, "Hub %d (%s):\n%s", |
Thomas Huth | 442da40 | 2018-04-30 20:02:24 +0200 | [diff] [blame] | 1134 | got_hub_id ? id : -1, |
Marc-André Lureau | b7f43bf | 2018-11-10 17:45:43 +0400 | [diff] [blame] | 1135 | s->nc.name, info); |
| 1136 | g_free(info); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1140 | static void |
| 1141 | net_init_slirp_configs(const StringList *fwd, int flags) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1142 | { |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1143 | while (fwd) { |
| 1144 | struct slirp_config_str *config; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1145 | |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1146 | config = g_malloc0(sizeof(*config)); |
| 1147 | pstrcpy(config->str, sizeof(config->str), fwd->value->str); |
| 1148 | config->flags = flags; |
| 1149 | config->next = slirp_configs; |
| 1150 | slirp_configs = config; |
| 1151 | |
| 1152 | fwd = fwd->next; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1153 | } |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 1156 | static const char **slirp_dnssearch(const StringList *dnsname) |
| 1157 | { |
| 1158 | const StringList *c = dnsname; |
| 1159 | size_t i = 0, num_opts = 0; |
| 1160 | const char **ret; |
| 1161 | |
| 1162 | while (c) { |
| 1163 | num_opts++; |
| 1164 | c = c->next; |
| 1165 | } |
| 1166 | |
| 1167 | if (num_opts == 0) { |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | |
| 1171 | ret = g_malloc((num_opts + 1) * sizeof(*ret)); |
| 1172 | c = dnsname; |
| 1173 | while (c) { |
| 1174 | ret[i++] = c->value->str; |
| 1175 | c = c->next; |
| 1176 | } |
| 1177 | ret[i] = NULL; |
| 1178 | return ret; |
| 1179 | } |
| 1180 | |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 1181 | int net_init_slirp(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 1182 | NetClientState *peer, Error **errp) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1183 | { |
| 1184 | struct slirp_config_str *config; |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1185 | char *vnet; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1186 | int ret; |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1187 | const NetdevUserOptions *user; |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 1188 | const char **dnssearch; |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 1189 | bool ipv4 = true, ipv6 = true; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1190 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1191 | assert(netdev->type == NET_CLIENT_DRIVER_USER); |
| 1192 | user = &netdev->u.user; |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1193 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 1194 | if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) || |
| 1195 | (user->has_ipv4 && !user->ipv4)) { |
| 1196 | ipv4 = 0; |
| 1197 | } |
| 1198 | if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) || |
| 1199 | (user->has_ipv6 && !user->ipv6)) { |
| 1200 | ipv6 = 0; |
| 1201 | } |
| 1202 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1203 | vnet = user->net ? g_strdup(user->net) : |
| 1204 | user->ip ? g_strdup_printf("%s/24", user->ip) : |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1205 | NULL; |
Jan Kiszka | c54ed5b | 2011-07-20 12:20:14 +0200 | [diff] [blame] | 1206 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 1207 | dnssearch = slirp_dnssearch(user->dnssearch); |
| 1208 | |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1209 | /* all optional fields are initialized to "all bits zero" */ |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1210 | |
Laszlo Ersek | 094f15c | 2012-07-17 16:17:16 +0200 | [diff] [blame] | 1211 | net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD); |
| 1212 | net_init_slirp_configs(user->guestfwd, 0); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1213 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 1214 | ret = net_slirp_init(peer, "user", name, user->q_restrict, |
| 1215 | ipv4, vnet, user->host, |
| 1216 | ipv6, user->ipv6_prefix, user->ipv6_prefixlen, |
Samuel Thibault | d8eb386 | 2016-03-25 00:02:58 +0100 | [diff] [blame] | 1217 | user->ipv6_host, user->hostname, user->tftp, |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1218 | user->bootfile, user->dhcpstart, |
Samuel Thibault | d8eb386 | 2016-03-25 00:02:58 +0100 | [diff] [blame] | 1219 | user->dns, user->ipv6_dns, user->smb, |
Fam Zheng | 0fca92b | 2018-09-14 15:26:16 +0800 | [diff] [blame] | 1220 | user->smbserver, dnssearch, user->domainname, |
| 1221 | user->tftp_server_name, errp); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1222 | |
| 1223 | while (slirp_configs) { |
| 1224 | config = slirp_configs; |
| 1225 | slirp_configs = config->next; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1226 | g_free(config); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1229 | g_free(vnet); |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 1230 | g_free(dnssearch); |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1231 | |
| 1232 | return ret; |
| 1233 | } |