blob: 1a0ea1be4ef23a3be5d336ef8e0a84f7278c7425 [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardf0cbd3e2004-04-22 00:10:48 +00004 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
Peter Maydell7df74822016-01-29 17:49:59 +00008#include "qemu/osdep.h"
bellardf0cbd3e2004-04-22 00:10:48 +00009#include <slirp.h>
Jan Kiszka6dbe5532009-06-24 14:42:29 +020010#include <libslirp.h>
11
Paolo Bonzini83c90892012-12-17 18:19:49 +010012#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010013#include "qemu/error-report.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010014#include "qemu/main-loop.h"
bellardf0cbd3e2004-04-22 00:10:48 +000015
Jan Kiszka9f349492009-06-24 14:42:29 +020016#ifdef DEBUG
17int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
18#endif
19
bellardf0cbd3e2004-04-22 00:10:48 +000020inline void
blueswir1511d2b12009-03-07 15:32:56 +000021insque(void *a, void *b)
bellardf0cbd3e2004-04-22 00:10:48 +000022{
23 register struct quehead *element = (struct quehead *) a;
24 register struct quehead *head = (struct quehead *) b;
25 element->qh_link = head->qh_link;
26 head->qh_link = (struct quehead *)element;
27 element->qh_rlink = (struct quehead *)head;
28 ((struct quehead *)(element->qh_link))->qh_rlink
29 = (struct quehead *)element;
30}
31
32inline void
blueswir1511d2b12009-03-07 15:32:56 +000033remque(void *a)
bellardf0cbd3e2004-04-22 00:10:48 +000034{
35 register struct quehead *element = (struct quehead *) a;
36 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
37 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
38 element->qh_rlink = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +000039}
40
Jan Kiszkaa13a4122009-06-24 14:42:28 +020041int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
42 struct in_addr addr, int port)
bellardf0cbd3e2004-04-22 00:10:48 +000043{
44 struct ex_list *tmp_ptr;
ths5fafdf22007-09-16 21:08:06 +000045
bellardf0cbd3e2004-04-22 00:10:48 +000046 /* First, check if the port is "bound" */
47 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +020048 if (port == tmp_ptr->ex_fport &&
49 addr.s_addr == tmp_ptr->ex_addr.s_addr)
50 return -1;
bellardf0cbd3e2004-04-22 00:10:48 +000051 }
ths5fafdf22007-09-16 21:08:06 +000052
bellardf0cbd3e2004-04-22 00:10:48 +000053 tmp_ptr = *ex_ptr;
zhanghailiang2fd5d862014-08-19 16:30:17 +080054 *ex_ptr = g_new(struct ex_list, 1);
bellardf0cbd3e2004-04-22 00:10:48 +000055 (*ex_ptr)->ex_fport = port;
56 (*ex_ptr)->ex_addr = addr;
57 (*ex_ptr)->ex_pty = do_pty;
zhanghailiang2fd5d862014-08-19 16:30:17 +080058 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
bellardf0cbd3e2004-04-22 00:10:48 +000059 (*ex_ptr)->ex_next = tmp_ptr;
60 return 0;
61}
62
bellardf0cbd3e2004-04-22 00:10:48 +000063
bellarda3d4af02004-09-05 23:10:26 +000064#ifdef _WIN32
65
66int
blueswir19634d902007-10-26 19:01:16 +000067fork_exec(struct socket *so, const char *ex, int do_pty)
bellarda3d4af02004-09-05 23:10:26 +000068{
69 /* not implemented */
70 return 0;
71}
72
73#else
74
bellardf0cbd3e2004-04-22 00:10:48 +000075/*
76 * XXX This is ugly
77 * We create and bind a socket, then fork off to another
78 * process, which connects to this socket, after which we
79 * exec the wanted program. If something (strange) happens,
80 * the accept() call could block us forever.
ths5fafdf22007-09-16 21:08:06 +000081 *
bellardf0cbd3e2004-04-22 00:10:48 +000082 * do_pty = 0 Fork/exec inetd style
83 * do_pty = 1 Fork/exec using slirp.telnetd
84 * do_ptr = 2 Fork/exec using pty
85 */
86int
blueswir19634d902007-10-26 19:01:16 +000087fork_exec(struct socket *so, const char *ex, int do_pty)
bellardf0cbd3e2004-04-22 00:10:48 +000088{
89 int s;
90 struct sockaddr_in addr;
balrog242acf32008-05-10 01:49:53 +000091 socklen_t addrlen = sizeof(addr);
bellardf0cbd3e2004-04-22 00:10:48 +000092 int opt;
blueswir17ccfb2e2008-09-14 06:45:34 +000093 const char *argv[256];
bellardf0cbd3e2004-04-22 00:10:48 +000094 /* don't want to clobber the original */
95 char *bptr;
blueswir19634d902007-10-26 19:01:16 +000096 const char *curarg;
bellard7b91a172004-12-12 11:45:10 +000097 int c, i, ret;
Paolo Bonzini4d54ec72011-03-09 18:21:10 +010098 pid_t pid;
ths5fafdf22007-09-16 21:08:06 +000099
bellardf0cbd3e2004-04-22 00:10:48 +0000100 DEBUG_CALL("fork_exec");
Stefan Weilecc804c2015-08-29 09:12:35 +0200101 DEBUG_ARG("so = %p", so);
102 DEBUG_ARG("ex = %p", ex);
103 DEBUG_ARG("do_pty = %x", do_pty);
ths5fafdf22007-09-16 21:08:06 +0000104
bellardf0cbd3e2004-04-22 00:10:48 +0000105 if (do_pty == 2) {
balrog3f9b2b12007-11-13 01:56:12 +0000106 return 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000107 } else {
108 addr.sin_family = AF_INET;
109 addr.sin_port = 0;
110 addr.sin_addr.s_addr = INADDR_ANY;
ths3b46e622007-09-17 08:09:54 +0000111
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100112 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
bellardf0cbd3e2004-04-22 00:10:48 +0000113 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
114 listen(s, 1) < 0) {
Cole Robinson02d16082014-03-21 19:42:20 -0400115 error_report("Error: inet socket: %s", strerror(errno));
bellard379ff532004-07-12 22:33:07 +0000116 closesocket(s);
ths3b46e622007-09-17 08:09:54 +0000117
bellardf0cbd3e2004-04-22 00:10:48 +0000118 return 0;
119 }
120 }
ths5fafdf22007-09-16 21:08:06 +0000121
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100122 pid = fork();
123 switch(pid) {
bellardf0cbd3e2004-04-22 00:10:48 +0000124 case -1:
Cole Robinson02d16082014-03-21 19:42:20 -0400125 error_report("Error: fork failed: %s", strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000126 close(s);
bellardf0cbd3e2004-04-22 00:10:48 +0000127 return 0;
ths3b46e622007-09-17 08:09:54 +0000128
bellardf0cbd3e2004-04-22 00:10:48 +0000129 case 0:
Jan Kiszka565465f2011-07-20 12:20:17 +0200130 setsid();
131
bellardf0cbd3e2004-04-22 00:10:48 +0000132 /* Set the DISPLAY */
Stefan Weil9f1134d2012-01-05 14:18:45 +0100133 getsockname(s, (struct sockaddr *)&addr, &addrlen);
134 close(s);
135 /*
136 * Connect to the socket
137 * XXX If any of these fail, we're in trouble!
138 */
139 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
140 addr.sin_addr = loopback_addr;
141 do {
142 ret = connect(s, (struct sockaddr *)&addr, addrlen);
143 } while (ret < 0 && errno == EINTR);
ths3b46e622007-09-17 08:09:54 +0000144
bellardf0cbd3e2004-04-22 00:10:48 +0000145 dup2(s, 0);
146 dup2(s, 1);
147 dup2(s, 2);
blueswir19634d902007-10-26 19:01:16 +0000148 for (s = getdtablesize() - 1; s >= 3; s--)
bellardf0cbd3e2004-04-22 00:10:48 +0000149 close(s);
ths3b46e622007-09-17 08:09:54 +0000150
bellardf0cbd3e2004-04-22 00:10:48 +0000151 i = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500152 bptr = g_strdup(ex); /* No need to free() this */
bellardf0cbd3e2004-04-22 00:10:48 +0000153 if (do_pty == 1) {
154 /* Setup "slirp.telnetd -x" */
155 argv[i++] = "slirp.telnetd";
156 argv[i++] = "-x";
157 argv[i++] = bptr;
158 } else
159 do {
160 /* Change the string into argv[] */
161 curarg = bptr;
162 while (*bptr != ' ' && *bptr != (char)0)
163 bptr++;
164 c = *bptr;
165 *bptr++ = (char)0;
zhanghailiang2fd5d862014-08-19 16:30:17 +0800166 argv[i++] = g_strdup(curarg);
bellardf0cbd3e2004-04-22 00:10:48 +0000167 } while (c);
ths3b46e622007-09-17 08:09:54 +0000168
blueswir1511d2b12009-03-07 15:32:56 +0000169 argv[i] = NULL;
blueswir17ccfb2e2008-09-14 06:45:34 +0000170 execvp(argv[0], (char **)argv);
ths3b46e622007-09-17 08:09:54 +0000171
bellardf0cbd3e2004-04-22 00:10:48 +0000172 /* Ooops, failed, let's tell the user why */
Kirill A. Shutemovf0d98b02009-12-25 18:19:18 +0000173 fprintf(stderr, "Error: execvp of %s failed: %s\n",
174 argv[0], strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000175 close(0); close(1); close(2); /* XXX */
176 exit(1);
ths3b46e622007-09-17 08:09:54 +0000177
bellardf0cbd3e2004-04-22 00:10:48 +0000178 default:
Paolo Bonzini4d54ec72011-03-09 18:21:10 +0100179 qemu_add_child_watch(pid);
Stefan Weil9f1134d2012-01-05 14:18:45 +0100180 /*
181 * XXX this could block us...
182 * XXX Should set a timer here, and if accept() doesn't
183 * return after X seconds, declare it a failure
184 * The only reason this will block forever is if socket()
185 * of connect() fail in the child process
186 */
187 do {
188 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
189 } while (so->s < 0 && errno == EINTR);
190 closesocket(s);
Sebastian Ottlikaad12392013-10-02 12:23:15 +0200191 socket_set_fast_reuse(so->s);
Stefan Weil9f1134d2012-01-05 14:18:45 +0100192 opt = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +0100193 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100194 qemu_set_nonblock(so->s);
ths3b46e622007-09-17 08:09:54 +0000195
bellardf0cbd3e2004-04-22 00:10:48 +0000196 /* Append the telnet options now */
blueswir1511d2b12009-03-07 15:32:56 +0000197 if (so->so_m != NULL && do_pty == 1) {
bellardf0cbd3e2004-04-22 00:10:48 +0000198 sbappend(so, so->so_m);
blueswir1511d2b12009-03-07 15:32:56 +0000199 so->so_m = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000200 }
ths3b46e622007-09-17 08:09:54 +0000201
bellardf0cbd3e2004-04-22 00:10:48 +0000202 return 1;
203 }
204}
205#endif
206
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200207void slirp_connection_info(Slirp *slirp, Monitor *mon)
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200208{
209 const char * const tcpstates[] = {
210 [TCPS_CLOSED] = "CLOSED",
211 [TCPS_LISTEN] = "LISTEN",
212 [TCPS_SYN_SENT] = "SYN_SENT",
213 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
214 [TCPS_ESTABLISHED] = "ESTABLISHED",
215 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
216 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
217 [TCPS_CLOSING] = "CLOSING",
218 [TCPS_LAST_ACK] = "LAST_ACK",
219 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
220 [TCPS_TIME_WAIT] = "TIME_WAIT",
221 };
222 struct in_addr dst_addr;
223 struct sockaddr_in src;
224 socklen_t src_len;
225 uint16_t dst_port;
226 struct socket *so;
227 const char *state;
228 char buf[20];
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200229
230 monitor_printf(mon, " Protocol[State] FD Source Address Port "
231 "Dest. Address Port RecvQ SendQ\n");
232
Jan Kiszka460fec62009-06-24 14:42:31 +0200233 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200234 if (so->so_state & SS_HOSTFWD) {
235 state = "HOST_FORWARD";
236 } else if (so->so_tcpcb) {
237 state = tcpstates[so->so_tcpcb->t_state];
238 } else {
239 state = "NONE";
240 }
241 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
242 src_len = sizeof(src);
243 getsockname(so->s, (struct sockaddr *)&src, &src_len);
244 dst_addr = so->so_laddr;
245 dst_port = so->so_lport;
246 } else {
247 src.sin_addr = so->so_laddr;
248 src.sin_port = so->so_lport;
249 dst_addr = so->so_faddr;
250 dst_port = so->so_fport;
251 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200252 snprintf(buf, sizeof(buf), " TCP[%s]", state);
253 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200254 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
255 ntohs(src.sin_port));
256 monitor_printf(mon, "%15s %5d %5d %5d\n",
257 inet_ntoa(dst_addr), ntohs(dst_port),
258 so->so_rcv.sb_cc, so->so_snd.sb_cc);
259 }
260
Jan Kiszka460fec62009-06-24 14:42:31 +0200261 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200262 if (so->so_state & SS_HOSTFWD) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200263 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200264 src_len = sizeof(src);
265 getsockname(so->s, (struct sockaddr *)&src, &src_len);
266 dst_addr = so->so_laddr;
267 dst_port = so->so_lport;
268 } else {
Alon Levyf293d8b2012-02-24 13:33:49 +0200269 snprintf(buf, sizeof(buf), " UDP[%d sec]",
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200270 (so->so_expire - curtime) / 1000);
271 src.sin_addr = so->so_laddr;
272 src.sin_port = so->so_lport;
273 dst_addr = so->so_faddr;
274 dst_port = so->so_fport;
275 }
Alon Levyf293d8b2012-02-24 13:33:49 +0200276 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200277 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
278 ntohs(src.sin_port));
279 monitor_printf(mon, "%15s %5d %5d %5d\n",
280 inet_ntoa(dst_addr), ntohs(dst_port),
281 so->so_rcv.sb_cc, so->so_snd.sb_cc);
282 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200283
284 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
Alon Levyf293d8b2012-02-24 13:33:49 +0200285 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200286 (so->so_expire - curtime) / 1000);
287 src.sin_addr = so->so_laddr;
288 dst_addr = so->so_faddr;
Alon Levyf293d8b2012-02-24 13:33:49 +0200289 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200290 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
291 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
292 so->so_rcv.sb_cc, so->so_snd.sb_cc);
293 }
Jan Kiszka6dbe5532009-06-24 14:42:29 +0200294}