blob: 37ac5cf2fb260cffbf85b5f4f80b72d0fbcc9053 [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
4 * Please read the file COPYRIGHT for the
bellardf0cbd3e2004-04-22 00:10:48 +00005 * terms and conditions of the copyright.
6 */
7
aliguorie1c5a2b2009-01-08 19:18:21 +00008#include "qemu-common.h"
bellardf0cbd3e2004-04-22 00:10:48 +00009#include <slirp.h>
10#include "ip_icmp.h"
bellardec530c82006-04-25 22:36:06 +000011#ifdef __sun__
12#include <sys/filio.h>
13#endif
bellardf0cbd3e2004-04-22 00:10:48 +000014
blueswir19634d902007-10-26 19:01:16 +000015static void sofcantrcvmore(struct socket *so);
16static void sofcantsendmore(struct socket *so);
17
bellardf0cbd3e2004-04-22 00:10:48 +000018struct socket *
blueswir1511d2b12009-03-07 15:32:56 +000019solookup(struct socket *head, struct in_addr laddr, u_int lport,
20 struct in_addr faddr, u_int fport)
bellardf0cbd3e2004-04-22 00:10:48 +000021{
22 struct socket *so;
ths5fafdf22007-09-16 21:08:06 +000023
bellardf0cbd3e2004-04-22 00:10:48 +000024 for (so = head->so_next; so != head; so = so->so_next) {
ths5fafdf22007-09-16 21:08:06 +000025 if (so->so_lport == lport &&
bellardf0cbd3e2004-04-22 00:10:48 +000026 so->so_laddr.s_addr == laddr.s_addr &&
27 so->so_faddr.s_addr == faddr.s_addr &&
28 so->so_fport == fport)
29 break;
30 }
ths5fafdf22007-09-16 21:08:06 +000031
bellardf0cbd3e2004-04-22 00:10:48 +000032 if (so == head)
33 return (struct socket *)NULL;
34 return so;
ths5fafdf22007-09-16 21:08:06 +000035
bellardf0cbd3e2004-04-22 00:10:48 +000036}
37
38/*
39 * Create a new socket, initialise the fields
40 * It is the responsibility of the caller to
41 * insque() it into the correct linked-list
42 */
43struct socket *
Jan Kiszka460fec62009-06-24 14:42:31 +020044socreate(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000045{
46 struct socket *so;
ths5fafdf22007-09-16 21:08:06 +000047
bellardf0cbd3e2004-04-22 00:10:48 +000048 so = (struct socket *)malloc(sizeof(struct socket));
49 if(so) {
50 memset(so, 0, sizeof(struct socket));
51 so->so_state = SS_NOFDREF;
52 so->s = -1;
Jan Kiszka460fec62009-06-24 14:42:31 +020053 so->slirp = slirp;
Jan Kiszka7bd43ec2013-02-22 20:47:10 +010054 so->pollfds_idx = -1;
bellardf0cbd3e2004-04-22 00:10:48 +000055 }
56 return(so);
57}
58
59/*
60 * remque and free a socket, clobber cache
61 */
62void
blueswir1511d2b12009-03-07 15:32:56 +000063sofree(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +000064{
Jan Kiszka460fec62009-06-24 14:42:31 +020065 Slirp *slirp = so->slirp;
66
bellardf0cbd3e2004-04-22 00:10:48 +000067 if (so->so_emu==EMU_RSH && so->extra) {
68 sofree(so->extra);
69 so->extra=NULL;
70 }
Jan Kiszka460fec62009-06-24 14:42:31 +020071 if (so == slirp->tcp_last_so) {
72 slirp->tcp_last_so = &slirp->tcb;
73 } else if (so == slirp->udp_last_so) {
74 slirp->udp_last_so = &slirp->udb;
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020075 } else if (so == slirp->icmp_last_so) {
76 slirp->icmp_last_so = &slirp->icmp;
Jan Kiszka460fec62009-06-24 14:42:31 +020077 }
bellardf0cbd3e2004-04-22 00:10:48 +000078 m_free(so->so_m);
ths5fafdf22007-09-16 21:08:06 +000079
80 if(so->so_next && so->so_prev)
bellardf0cbd3e2004-04-22 00:10:48 +000081 remque(so); /* crashes if so is not in a queue */
82
83 free(so);
84}
85
aliguorie1c5a2b2009-01-08 19:18:21 +000086size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
bellardf0cbd3e2004-04-22 00:10:48 +000087{
aliguorie1c5a2b2009-01-08 19:18:21 +000088 int n, lss, total;
bellardf0cbd3e2004-04-22 00:10:48 +000089 struct sbuf *sb = &so->so_snd;
90 int len = sb->sb_datalen - sb->sb_cc;
bellardf0cbd3e2004-04-22 00:10:48 +000091 int mss = so->so_tcpcb->t_maxseg;
ths5fafdf22007-09-16 21:08:06 +000092
aliguorie1c5a2b2009-01-08 19:18:21 +000093 DEBUG_CALL("sopreprbuf");
bellardf0cbd3e2004-04-22 00:10:48 +000094 DEBUG_ARG("so = %lx", (long )so);
ths5fafdf22007-09-16 21:08:06 +000095
aliguorie1c5a2b2009-01-08 19:18:21 +000096 if (len <= 0)
97 return 0;
98
bellardf0cbd3e2004-04-22 00:10:48 +000099 iov[0].iov_base = sb->sb_wptr;
blueswir166029f62008-10-01 19:39:40 +0000100 iov[1].iov_base = NULL;
101 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000102 if (sb->sb_wptr < sb->sb_rptr) {
103 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
104 /* Should never succeed, but... */
105 if (iov[0].iov_len > len)
106 iov[0].iov_len = len;
107 if (iov[0].iov_len > mss)
108 iov[0].iov_len -= iov[0].iov_len%mss;
109 n = 1;
110 } else {
111 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
112 /* Should never succeed, but... */
113 if (iov[0].iov_len > len) iov[0].iov_len = len;
114 len -= iov[0].iov_len;
115 if (len) {
116 iov[1].iov_base = sb->sb_data;
117 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
118 if(iov[1].iov_len > len)
119 iov[1].iov_len = len;
120 total = iov[0].iov_len + iov[1].iov_len;
121 if (total > mss) {
122 lss = total%mss;
123 if (iov[1].iov_len > lss) {
124 iov[1].iov_len -= lss;
125 n = 2;
126 } else {
127 lss -= iov[1].iov_len;
128 iov[0].iov_len -= lss;
129 n = 1;
130 }
131 } else
132 n = 2;
133 } else {
134 if (iov[0].iov_len > mss)
135 iov[0].iov_len -= iov[0].iov_len%mss;
136 n = 1;
137 }
138 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000139 if (np)
140 *np = n;
141
142 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
143}
144
145/*
146 * Read from so's socket into sb_snd, updating all relevant sbuf fields
147 * NOTE: This will only be called if it is select()ed for reading, so
148 * a read() of 0 (or less) means it's disconnected
149 */
150int
blueswir1511d2b12009-03-07 15:32:56 +0000151soread(struct socket *so)
aliguorie1c5a2b2009-01-08 19:18:21 +0000152{
153 int n, nn;
154 struct sbuf *sb = &so->so_snd;
155 struct iovec iov[2];
156
157 DEBUG_CALL("soread");
158 DEBUG_ARG("so = %lx", (long )so);
159
160 /*
161 * No need to check if there's enough room to read.
162 * soread wouldn't have been called if there weren't
163 */
164 sopreprbuf(so, iov, &n);
ths5fafdf22007-09-16 21:08:06 +0000165
bellardf0cbd3e2004-04-22 00:10:48 +0000166#ifdef HAVE_READV
167 nn = readv(so->s, (struct iovec *)iov, n);
168 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
169#else
Blue Swirl00aa0042011-07-23 20:04:29 +0000170 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
ths5fafdf22007-09-16 21:08:06 +0000171#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000172 if (nn <= 0) {
173 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
174 return 0;
175 else {
176 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
177 sofcantrcvmore(so);
178 tcp_sockclosed(sototcpcb(so));
179 return -1;
180 }
181 }
ths5fafdf22007-09-16 21:08:06 +0000182
bellardf0cbd3e2004-04-22 00:10:48 +0000183#ifndef HAVE_READV
184 /*
185 * If there was no error, try and read the second time round
186 * We read again if n = 2 (ie, there's another part of the buffer)
187 * and we read as much as we could in the first read
188 * We don't test for <= 0 this time, because there legitimately
189 * might not be any more data (since the socket is non-blocking),
190 * a close will be detected on next iteration.
191 * A return of -1 wont (shouldn't) happen, since it didn't happen above
192 */
bellard17444c92004-11-21 20:02:08 +0000193 if (n == 2 && nn == iov[0].iov_len) {
194 int ret;
Blue Swirl00aa0042011-07-23 20:04:29 +0000195 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
bellard17444c92004-11-21 20:02:08 +0000196 if (ret > 0)
197 nn += ret;
198 }
ths5fafdf22007-09-16 21:08:06 +0000199
bellardf0cbd3e2004-04-22 00:10:48 +0000200 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
201#endif
ths5fafdf22007-09-16 21:08:06 +0000202
bellardf0cbd3e2004-04-22 00:10:48 +0000203 /* Update fields */
204 sb->sb_cc += nn;
205 sb->sb_wptr += nn;
206 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
207 sb->sb_wptr -= sb->sb_datalen;
208 return nn;
209}
ths5fafdf22007-09-16 21:08:06 +0000210
aliguorie1c5a2b2009-01-08 19:18:21 +0000211int soreadbuf(struct socket *so, const char *buf, int size)
212{
213 int n, nn, copy = size;
214 struct sbuf *sb = &so->so_snd;
215 struct iovec iov[2];
216
217 DEBUG_CALL("soreadbuf");
218 DEBUG_ARG("so = %lx", (long )so);
219
220 /*
221 * No need to check if there's enough room to read.
222 * soread wouldn't have been called if there weren't
223 */
224 if (sopreprbuf(so, iov, &n) < size)
225 goto err;
226
227 nn = MIN(iov[0].iov_len, copy);
228 memcpy(iov[0].iov_base, buf, nn);
229
230 copy -= nn;
231 buf += nn;
232
233 if (copy == 0)
234 goto done;
235
236 memcpy(iov[1].iov_base, buf, copy);
237
238done:
239 /* Update fields */
240 sb->sb_cc += size;
241 sb->sb_wptr += size;
242 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
243 sb->sb_wptr -= sb->sb_datalen;
244 return size;
245err:
246
247 sofcantrcvmore(so);
248 tcp_sockclosed(sototcpcb(so));
249 fprintf(stderr, "soreadbuf buffer to small");
250 return -1;
251}
252
bellardf0cbd3e2004-04-22 00:10:48 +0000253/*
254 * Get urgent data
ths5fafdf22007-09-16 21:08:06 +0000255 *
bellardf0cbd3e2004-04-22 00:10:48 +0000256 * When the socket is created, we set it SO_OOBINLINE,
257 * so when OOB data arrives, we soread() it and everything
258 * in the send buffer is sent as urgent data
259 */
260void
blueswir1511d2b12009-03-07 15:32:56 +0000261sorecvoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000262{
263 struct tcpcb *tp = sototcpcb(so);
264
265 DEBUG_CALL("sorecvoob");
266 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000267
bellardf0cbd3e2004-04-22 00:10:48 +0000268 /*
269 * We take a guess at how much urgent data has arrived.
270 * In most situations, when urgent data arrives, the next
271 * read() should get all the urgent data. This guess will
272 * be wrong however if more data arrives just after the
ths5fafdf22007-09-16 21:08:06 +0000273 * urgent data, or the read() doesn't return all the
bellardf0cbd3e2004-04-22 00:10:48 +0000274 * urgent data.
275 */
276 soread(so);
277 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
278 tp->t_force = 1;
279 tcp_output(tp);
280 tp->t_force = 0;
281}
282
283/*
284 * Send urgent data
285 * There's a lot duplicated code here, but...
286 */
287int
blueswir1511d2b12009-03-07 15:32:56 +0000288sosendoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000289{
290 struct sbuf *sb = &so->so_rcv;
291 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
ths5fafdf22007-09-16 21:08:06 +0000292
bellardf0cbd3e2004-04-22 00:10:48 +0000293 int n, len;
ths5fafdf22007-09-16 21:08:06 +0000294
bellardf0cbd3e2004-04-22 00:10:48 +0000295 DEBUG_CALL("sosendoob");
296 DEBUG_ARG("so = %lx", (long)so);
297 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
ths5fafdf22007-09-16 21:08:06 +0000298
bellardf0cbd3e2004-04-22 00:10:48 +0000299 if (so->so_urgc > 2048)
300 so->so_urgc = 2048; /* XXXX */
ths5fafdf22007-09-16 21:08:06 +0000301
bellardf0cbd3e2004-04-22 00:10:48 +0000302 if (sb->sb_rptr < sb->sb_wptr) {
303 /* We can send it directly */
aliguorie1c5a2b2009-01-08 19:18:21 +0000304 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000305 so->so_urgc -= n;
ths3b46e622007-09-17 08:09:54 +0000306
bellardf0cbd3e2004-04-22 00:10:48 +0000307 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
308 } else {
ths5fafdf22007-09-16 21:08:06 +0000309 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000310 * Since there's no sendv or sendtov like writev,
311 * we must copy all data to a linear buffer then
312 * send it all
313 */
314 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
315 if (len > so->so_urgc) len = so->so_urgc;
316 memcpy(buff, sb->sb_rptr, len);
317 so->so_urgc -= len;
318 if (so->so_urgc) {
319 n = sb->sb_wptr - sb->sb_data;
320 if (n > so->so_urgc) n = so->so_urgc;
321 memcpy((buff + len), sb->sb_data, n);
322 so->so_urgc -= n;
323 len += n;
324 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000325 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000326#ifdef DEBUG
327 if (n != len)
328 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
ths3b46e622007-09-17 08:09:54 +0000329#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000330 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
331 }
ths5fafdf22007-09-16 21:08:06 +0000332
bellardf0cbd3e2004-04-22 00:10:48 +0000333 sb->sb_cc -= n;
334 sb->sb_rptr += n;
335 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
336 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000337
bellardf0cbd3e2004-04-22 00:10:48 +0000338 return n;
339}
340
341/*
ths5fafdf22007-09-16 21:08:06 +0000342 * Write data from so_rcv to so's socket,
bellardf0cbd3e2004-04-22 00:10:48 +0000343 * updating all sbuf field as necessary
344 */
345int
blueswir1511d2b12009-03-07 15:32:56 +0000346sowrite(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000347{
348 int n,nn;
349 struct sbuf *sb = &so->so_rcv;
350 int len = sb->sb_cc;
351 struct iovec iov[2];
ths5fafdf22007-09-16 21:08:06 +0000352
bellardf0cbd3e2004-04-22 00:10:48 +0000353 DEBUG_CALL("sowrite");
354 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000355
bellardf0cbd3e2004-04-22 00:10:48 +0000356 if (so->so_urgc) {
357 sosendoob(so);
358 if (sb->sb_cc == 0)
359 return 0;
360 }
361
362 /*
363 * No need to check if there's something to write,
364 * sowrite wouldn't have been called otherwise
365 */
ths5fafdf22007-09-16 21:08:06 +0000366
bellardf0cbd3e2004-04-22 00:10:48 +0000367 iov[0].iov_base = sb->sb_rptr;
blueswir166029f62008-10-01 19:39:40 +0000368 iov[1].iov_base = NULL;
369 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000370 if (sb->sb_rptr < sb->sb_wptr) {
371 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
372 /* Should never succeed, but... */
373 if (iov[0].iov_len > len) iov[0].iov_len = len;
374 n = 1;
375 } else {
376 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
377 if (iov[0].iov_len > len) iov[0].iov_len = len;
378 len -= iov[0].iov_len;
379 if (len) {
380 iov[1].iov_base = sb->sb_data;
381 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
382 if (iov[1].iov_len > len) iov[1].iov_len = len;
383 n = 2;
384 } else
385 n = 1;
386 }
387 /* Check if there's urgent data to send, and if so, send it */
388
389#ifdef HAVE_READV
390 nn = writev(so->s, (const struct iovec *)iov, n);
ths5fafdf22007-09-16 21:08:06 +0000391
bellardf0cbd3e2004-04-22 00:10:48 +0000392 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
393#else
aliguorie1c5a2b2009-01-08 19:18:21 +0000394 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000395#endif
396 /* This should never happen, but people tell me it does *shrug* */
397 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
398 return 0;
ths5fafdf22007-09-16 21:08:06 +0000399
bellardf0cbd3e2004-04-22 00:10:48 +0000400 if (nn <= 0) {
401 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
402 so->so_state, errno));
403 sofcantsendmore(so);
404 tcp_sockclosed(sototcpcb(so));
405 return -1;
406 }
ths5fafdf22007-09-16 21:08:06 +0000407
bellardf0cbd3e2004-04-22 00:10:48 +0000408#ifndef HAVE_READV
bellard3bc21752004-11-24 20:39:26 +0000409 if (n == 2 && nn == iov[0].iov_len) {
410 int ret;
aliguorie1c5a2b2009-01-08 19:18:21 +0000411 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
bellard3bc21752004-11-24 20:39:26 +0000412 if (ret > 0)
413 nn += ret;
414 }
bellardf0cbd3e2004-04-22 00:10:48 +0000415 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
416#endif
ths5fafdf22007-09-16 21:08:06 +0000417
bellardf0cbd3e2004-04-22 00:10:48 +0000418 /* Update sbuf */
419 sb->sb_cc -= nn;
420 sb->sb_rptr += nn;
421 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
422 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000423
bellardf0cbd3e2004-04-22 00:10:48 +0000424 /*
425 * If in DRAIN mode, and there's no more data, set
426 * it CANTSENDMORE
427 */
428 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
429 sofcantsendmore(so);
ths5fafdf22007-09-16 21:08:06 +0000430
bellardf0cbd3e2004-04-22 00:10:48 +0000431 return nn;
432}
433
434/*
435 * recvfrom() a UDP socket
436 */
437void
blueswir1511d2b12009-03-07 15:32:56 +0000438sorecvfrom(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000439{
440 struct sockaddr_in addr;
balrog242acf32008-05-10 01:49:53 +0000441 socklen_t addrlen = sizeof(struct sockaddr_in);
ths5fafdf22007-09-16 21:08:06 +0000442
bellardf0cbd3e2004-04-22 00:10:48 +0000443 DEBUG_CALL("sorecvfrom");
444 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000445
bellardf0cbd3e2004-04-22 00:10:48 +0000446 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
447 char buff[256];
448 int len;
ths3b46e622007-09-17 08:09:54 +0000449
ths5fafdf22007-09-16 21:08:06 +0000450 len = recvfrom(so->s, buff, 256, 0,
bellardf0cbd3e2004-04-22 00:10:48 +0000451 (struct sockaddr *)&addr, &addrlen);
452 /* XXX Check if reply is "correct"? */
ths3b46e622007-09-17 08:09:54 +0000453
bellardf0cbd3e2004-04-22 00:10:48 +0000454 if(len == -1 || len == 0) {
455 u_char code=ICMP_UNREACH_PORT;
456
457 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
458 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
ths3b46e622007-09-17 08:09:54 +0000459
bellardf0cbd3e2004-04-22 00:10:48 +0000460 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
461 errno,strerror(errno)));
462 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
463 } else {
464 icmp_reflect(so->so_m);
blueswir1511d2b12009-03-07 15:32:56 +0000465 so->so_m = NULL; /* Don't m_free() it again! */
bellardf0cbd3e2004-04-22 00:10:48 +0000466 }
467 /* No need for this socket anymore, udp_detach it */
468 udp_detach(so);
469 } else { /* A "normal" UDP packet */
470 struct mbuf *m;
Blue Swirlc5b76b32009-06-13 08:44:31 +0000471 int len;
472#ifdef _WIN32
473 unsigned long n;
474#else
475 int n;
476#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000477
Jan Kiszka460fec62009-06-24 14:42:31 +0200478 m = m_get(so->slirp);
479 if (!m) {
480 return;
481 }
blueswir19634d902007-10-26 19:01:16 +0000482 m->m_data += IF_MAXLINKHDR;
ths3b46e622007-09-17 08:09:54 +0000483
ths5fafdf22007-09-16 21:08:06 +0000484 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000485 * XXX Shouldn't FIONREAD packets destined for port 53,
486 * but I don't know the max packet size for DNS lookups
487 */
488 len = M_FREEROOM(m);
489 /* if (so->so_fport != htons(53)) { */
bellard379ff532004-07-12 22:33:07 +0000490 ioctlsocket(so->s, FIONREAD, &n);
ths3b46e622007-09-17 08:09:54 +0000491
bellardf0cbd3e2004-04-22 00:10:48 +0000492 if (n > len) {
493 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
494 m_inc(m, n);
495 len = M_FREEROOM(m);
496 }
497 /* } */
ths3b46e622007-09-17 08:09:54 +0000498
bellardf0cbd3e2004-04-22 00:10:48 +0000499 m->m_len = recvfrom(so->s, m->m_data, len, 0,
500 (struct sockaddr *)&addr, &addrlen);
ths5fafdf22007-09-16 21:08:06 +0000501 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
bellardf0cbd3e2004-04-22 00:10:48 +0000502 m->m_len, errno,strerror(errno)));
503 if(m->m_len<0) {
504 u_char code=ICMP_UNREACH_PORT;
505
506 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
507 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
ths3b46e622007-09-17 08:09:54 +0000508
bellardf0cbd3e2004-04-22 00:10:48 +0000509 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
510 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
511 m_free(m);
512 } else {
513 /*
514 * Hack: domain name lookup will be used the most for UDP,
515 * and since they'll only be used once there's no need
516 * for the 4 minute (or whatever) timeout... So we time them
517 * out much quicker (10 seconds for now...)
518 */
519 if (so->so_expire) {
520 if (so->so_fport == htons(53))
521 so->so_expire = curtime + SO_EXPIREFAST;
522 else
523 so->so_expire = curtime + SO_EXPIRE;
524 }
525
ths5fafdf22007-09-16 21:08:06 +0000526 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000527 * If this packet was destined for CTL_ADDR,
528 * make it look like that's where it came from, done by udp_output
529 */
530 udp_output(so, m, &addr);
531 } /* rx error */
532 } /* if ping packet */
533}
534
535/*
536 * sendto() a socket
537 */
538int
blueswir1511d2b12009-03-07 15:32:56 +0000539sosendto(struct socket *so, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000540{
Jan Kiszka460fec62009-06-24 14:42:31 +0200541 Slirp *slirp = so->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000542 int ret;
543 struct sockaddr_in addr;
544
545 DEBUG_CALL("sosendto");
546 DEBUG_ARG("so = %lx", (long)so);
547 DEBUG_ARG("m = %lx", (long)m);
ths5fafdf22007-09-16 21:08:06 +0000548
bellardf0cbd3e2004-04-22 00:10:48 +0000549 addr.sin_family = AF_INET;
Jan Kiszka460fec62009-06-24 14:42:31 +0200550 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
551 slirp->vnetwork_addr.s_addr) {
bellardf0cbd3e2004-04-22 00:10:48 +0000552 /* It's an alias */
Jan Kiszka460fec62009-06-24 14:42:31 +0200553 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700554 if (get_dns_addr(&addr.sin_addr) < 0)
555 addr.sin_addr = loopback_addr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200556 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000557 addr.sin_addr = loopback_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000558 }
559 } else
560 addr.sin_addr = so->so_faddr;
561 addr.sin_port = so->so_fport;
562
563 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
ths5fafdf22007-09-16 21:08:06 +0000564
bellardf0cbd3e2004-04-22 00:10:48 +0000565 /* Don't care what port we get */
566 ret = sendto(so->s, m->m_data, m->m_len, 0,
567 (struct sockaddr *)&addr, sizeof (struct sockaddr));
568 if (ret < 0)
569 return -1;
ths5fafdf22007-09-16 21:08:06 +0000570
bellardf0cbd3e2004-04-22 00:10:48 +0000571 /*
572 * Kill the socket if there's no reply in 4 minutes,
573 * but only if it's an expirable socket
574 */
575 if (so->so_expire)
576 so->so_expire = curtime + SO_EXPIRE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200577 so->so_state &= SS_PERSISTENT_MASK;
578 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
bellardf0cbd3e2004-04-22 00:10:48 +0000579 return 0;
580}
581
582/*
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200583 * Listen for incoming TCP connections
bellardf0cbd3e2004-04-22 00:10:48 +0000584 */
585struct socket *
Stefan Weilb6dce922010-07-22 22:15:23 +0200586tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
Jan Kiszka460fec62009-06-24 14:42:31 +0200587 u_int lport, int flags)
bellardf0cbd3e2004-04-22 00:10:48 +0000588{
589 struct sockaddr_in addr;
590 struct socket *so;
balrog242acf32008-05-10 01:49:53 +0000591 int s, opt = 1;
592 socklen_t addrlen = sizeof(addr);
Juha Riihimäkiab07b982010-04-13 09:16:55 +0300593 memset(&addr, 0, addrlen);
bellardf0cbd3e2004-04-22 00:10:48 +0000594
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200595 DEBUG_CALL("tcp_listen");
Jan Kiszka9f349492009-06-24 14:42:29 +0200596 DEBUG_ARG("haddr = %x", haddr);
597 DEBUG_ARG("hport = %d", hport);
bellardf0cbd3e2004-04-22 00:10:48 +0000598 DEBUG_ARG("laddr = %x", laddr);
599 DEBUG_ARG("lport = %d", lport);
600 DEBUG_ARG("flags = %x", flags);
ths5fafdf22007-09-16 21:08:06 +0000601
Jan Kiszka460fec62009-06-24 14:42:31 +0200602 so = socreate(slirp);
603 if (!so) {
bellardf0cbd3e2004-04-22 00:10:48 +0000604 return NULL;
605 }
ths5fafdf22007-09-16 21:08:06 +0000606
bellardf0cbd3e2004-04-22 00:10:48 +0000607 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
608 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
609 free(so);
610 return NULL;
611 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200612 insque(so, &slirp->tcb);
ths5fafdf22007-09-16 21:08:06 +0000613
614 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000615 * SS_FACCEPTONCE sockets must time out.
616 */
617 if (flags & SS_FACCEPTONCE)
618 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
ths5fafdf22007-09-16 21:08:06 +0000619
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200620 so->so_state &= SS_PERSISTENT_MASK;
621 so->so_state |= (SS_FACCEPTCONN | flags);
bellardf0cbd3e2004-04-22 00:10:48 +0000622 so->so_lport = lport; /* Kept in network format */
623 so->so_laddr.s_addr = laddr; /* Ditto */
ths5fafdf22007-09-16 21:08:06 +0000624
bellardf0cbd3e2004-04-22 00:10:48 +0000625 addr.sin_family = AF_INET;
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200626 addr.sin_addr.s_addr = haddr;
627 addr.sin_port = hport;
ths5fafdf22007-09-16 21:08:06 +0000628
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100629 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
Sebastian Ottlikaad12392013-10-02 12:23:15 +0200630 (socket_set_fast_reuse(s) < 0) ||
bellardf0cbd3e2004-04-22 00:10:48 +0000631 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
632 (listen(s,1) < 0)) {
633 int tmperrno = errno; /* Don't clobber the real reason we failed */
ths3b46e622007-09-17 08:09:54 +0000634
bellardf0cbd3e2004-04-22 00:10:48 +0000635 close(s);
636 sofree(so);
637 /* Restore the real errno */
bellard02d2c542004-10-07 23:27:35 +0000638#ifdef _WIN32
639 WSASetLastError(tmperrno);
640#else
bellardf0cbd3e2004-04-22 00:10:48 +0000641 errno = tmperrno;
bellard02d2c542004-10-07 23:27:35 +0000642#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000643 return NULL;
644 }
Stefan Weil9957fc72013-03-08 19:58:32 +0100645 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
ths5fafdf22007-09-16 21:08:06 +0000646
bellardf0cbd3e2004-04-22 00:10:48 +0000647 getsockname(s,(struct sockaddr *)&addr,&addrlen);
648 so->so_fport = addr.sin_port;
649 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
Jan Kiszka460fec62009-06-24 14:42:31 +0200650 so->so_faddr = slirp->vhost_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000651 else
652 so->so_faddr = addr.sin_addr;
653
654 so->s = s;
655 return so;
656}
657
bellardf0cbd3e2004-04-22 00:10:48 +0000658/*
659 * Various session state calls
660 * XXX Should be #define's
661 * The socket state stuff needs work, these often get call 2 or 3
662 * times each when only 1 was needed
663 */
664void
blueswir1511d2b12009-03-07 15:32:56 +0000665soisfconnecting(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000666{
667 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
668 SS_FCANTSENDMORE|SS_FWDRAIN);
669 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
670}
671
672void
blueswir1511d2b12009-03-07 15:32:56 +0000673soisfconnected(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000674{
675 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
676 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
677}
678
blueswir19634d902007-10-26 19:01:16 +0000679static void
680sofcantrcvmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000681{
682 if ((so->so_state & SS_NOFDREF) == 0) {
683 shutdown(so->s,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000684 }
685 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200686 if (so->so_state & SS_FCANTSENDMORE) {
687 so->so_state &= SS_PERSISTENT_MASK;
688 so->so_state |= SS_NOFDREF; /* Don't select it */
689 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000690 so->so_state |= SS_FCANTRCVMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200691 }
bellardf0cbd3e2004-04-22 00:10:48 +0000692}
693
blueswir19634d902007-10-26 19:01:16 +0000694static void
695sofcantsendmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000696{
697 if ((so->so_state & SS_NOFDREF) == 0) {
bellard02d2c542004-10-07 23:27:35 +0000698 shutdown(so->s,1); /* send FIN to fhost */
bellardf0cbd3e2004-04-22 00:10:48 +0000699 }
700 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200701 if (so->so_state & SS_FCANTRCVMORE) {
702 so->so_state &= SS_PERSISTENT_MASK;
703 so->so_state |= SS_NOFDREF; /* as above */
704 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000705 so->so_state |= SS_FCANTSENDMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200706 }
bellardf0cbd3e2004-04-22 00:10:48 +0000707}
708
bellardf0cbd3e2004-04-22 00:10:48 +0000709/*
710 * Set write drain mode
711 * Set CANTSENDMORE once all data has been write()n
712 */
713void
blueswir1511d2b12009-03-07 15:32:56 +0000714sofwdrain(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000715{
716 if (so->so_rcv.sb_cc)
717 so->so_state |= SS_FWDRAIN;
718 else
719 sofcantsendmore(so);
720}