blob: 77b0c9819785342087b63b8d55de92ce76872ddb [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;
bellardf0cbd3e2004-04-22 00:10:48 +000054 }
55 return(so);
56}
57
58/*
59 * remque and free a socket, clobber cache
60 */
61void
blueswir1511d2b12009-03-07 15:32:56 +000062sofree(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +000063{
Jan Kiszka460fec62009-06-24 14:42:31 +020064 Slirp *slirp = so->slirp;
65
bellardf0cbd3e2004-04-22 00:10:48 +000066 if (so->so_emu==EMU_RSH && so->extra) {
67 sofree(so->extra);
68 so->extra=NULL;
69 }
Jan Kiszka460fec62009-06-24 14:42:31 +020070 if (so == slirp->tcp_last_so) {
71 slirp->tcp_last_so = &slirp->tcb;
72 } else if (so == slirp->udp_last_so) {
73 slirp->udp_last_so = &slirp->udb;
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020074 } else if (so == slirp->icmp_last_so) {
75 slirp->icmp_last_so = &slirp->icmp;
Jan Kiszka460fec62009-06-24 14:42:31 +020076 }
bellardf0cbd3e2004-04-22 00:10:48 +000077 m_free(so->so_m);
ths5fafdf22007-09-16 21:08:06 +000078
79 if(so->so_next && so->so_prev)
bellardf0cbd3e2004-04-22 00:10:48 +000080 remque(so); /* crashes if so is not in a queue */
81
82 free(so);
83}
84
aliguorie1c5a2b2009-01-08 19:18:21 +000085size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
bellardf0cbd3e2004-04-22 00:10:48 +000086{
aliguorie1c5a2b2009-01-08 19:18:21 +000087 int n, lss, total;
bellardf0cbd3e2004-04-22 00:10:48 +000088 struct sbuf *sb = &so->so_snd;
89 int len = sb->sb_datalen - sb->sb_cc;
bellardf0cbd3e2004-04-22 00:10:48 +000090 int mss = so->so_tcpcb->t_maxseg;
ths5fafdf22007-09-16 21:08:06 +000091
aliguorie1c5a2b2009-01-08 19:18:21 +000092 DEBUG_CALL("sopreprbuf");
bellardf0cbd3e2004-04-22 00:10:48 +000093 DEBUG_ARG("so = %lx", (long )so);
ths5fafdf22007-09-16 21:08:06 +000094
aliguorie1c5a2b2009-01-08 19:18:21 +000095 if (len <= 0)
96 return 0;
97
bellardf0cbd3e2004-04-22 00:10:48 +000098 iov[0].iov_base = sb->sb_wptr;
blueswir166029f62008-10-01 19:39:40 +000099 iov[1].iov_base = NULL;
100 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000101 if (sb->sb_wptr < sb->sb_rptr) {
102 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
103 /* Should never succeed, but... */
104 if (iov[0].iov_len > len)
105 iov[0].iov_len = len;
106 if (iov[0].iov_len > mss)
107 iov[0].iov_len -= iov[0].iov_len%mss;
108 n = 1;
109 } else {
110 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
111 /* Should never succeed, but... */
112 if (iov[0].iov_len > len) iov[0].iov_len = len;
113 len -= iov[0].iov_len;
114 if (len) {
115 iov[1].iov_base = sb->sb_data;
116 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
117 if(iov[1].iov_len > len)
118 iov[1].iov_len = len;
119 total = iov[0].iov_len + iov[1].iov_len;
120 if (total > mss) {
121 lss = total%mss;
122 if (iov[1].iov_len > lss) {
123 iov[1].iov_len -= lss;
124 n = 2;
125 } else {
126 lss -= iov[1].iov_len;
127 iov[0].iov_len -= lss;
128 n = 1;
129 }
130 } else
131 n = 2;
132 } else {
133 if (iov[0].iov_len > mss)
134 iov[0].iov_len -= iov[0].iov_len%mss;
135 n = 1;
136 }
137 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000138 if (np)
139 *np = n;
140
141 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
142}
143
144/*
145 * Read from so's socket into sb_snd, updating all relevant sbuf fields
146 * NOTE: This will only be called if it is select()ed for reading, so
147 * a read() of 0 (or less) means it's disconnected
148 */
149int
blueswir1511d2b12009-03-07 15:32:56 +0000150soread(struct socket *so)
aliguorie1c5a2b2009-01-08 19:18:21 +0000151{
152 int n, nn;
153 struct sbuf *sb = &so->so_snd;
154 struct iovec iov[2];
155
156 DEBUG_CALL("soread");
157 DEBUG_ARG("so = %lx", (long )so);
158
159 /*
160 * No need to check if there's enough room to read.
161 * soread wouldn't have been called if there weren't
162 */
163 sopreprbuf(so, iov, &n);
ths5fafdf22007-09-16 21:08:06 +0000164
bellardf0cbd3e2004-04-22 00:10:48 +0000165#ifdef HAVE_READV
166 nn = readv(so->s, (struct iovec *)iov, n);
167 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
168#else
Blue Swirl00aa0042011-07-23 20:04:29 +0000169 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
ths5fafdf22007-09-16 21:08:06 +0000170#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000171 if (nn <= 0) {
172 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
173 return 0;
174 else {
175 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
176 sofcantrcvmore(so);
177 tcp_sockclosed(sototcpcb(so));
178 return -1;
179 }
180 }
ths5fafdf22007-09-16 21:08:06 +0000181
bellardf0cbd3e2004-04-22 00:10:48 +0000182#ifndef HAVE_READV
183 /*
184 * If there was no error, try and read the second time round
185 * We read again if n = 2 (ie, there's another part of the buffer)
186 * and we read as much as we could in the first read
187 * We don't test for <= 0 this time, because there legitimately
188 * might not be any more data (since the socket is non-blocking),
189 * a close will be detected on next iteration.
190 * A return of -1 wont (shouldn't) happen, since it didn't happen above
191 */
bellard17444c92004-11-21 20:02:08 +0000192 if (n == 2 && nn == iov[0].iov_len) {
193 int ret;
Blue Swirl00aa0042011-07-23 20:04:29 +0000194 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
bellard17444c92004-11-21 20:02:08 +0000195 if (ret > 0)
196 nn += ret;
197 }
ths5fafdf22007-09-16 21:08:06 +0000198
bellardf0cbd3e2004-04-22 00:10:48 +0000199 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
200#endif
ths5fafdf22007-09-16 21:08:06 +0000201
bellardf0cbd3e2004-04-22 00:10:48 +0000202 /* Update fields */
203 sb->sb_cc += nn;
204 sb->sb_wptr += nn;
205 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
206 sb->sb_wptr -= sb->sb_datalen;
207 return nn;
208}
ths5fafdf22007-09-16 21:08:06 +0000209
aliguorie1c5a2b2009-01-08 19:18:21 +0000210int soreadbuf(struct socket *so, const char *buf, int size)
211{
212 int n, nn, copy = size;
213 struct sbuf *sb = &so->so_snd;
214 struct iovec iov[2];
215
216 DEBUG_CALL("soreadbuf");
217 DEBUG_ARG("so = %lx", (long )so);
218
219 /*
220 * No need to check if there's enough room to read.
221 * soread wouldn't have been called if there weren't
222 */
223 if (sopreprbuf(so, iov, &n) < size)
224 goto err;
225
226 nn = MIN(iov[0].iov_len, copy);
227 memcpy(iov[0].iov_base, buf, nn);
228
229 copy -= nn;
230 buf += nn;
231
232 if (copy == 0)
233 goto done;
234
235 memcpy(iov[1].iov_base, buf, copy);
236
237done:
238 /* Update fields */
239 sb->sb_cc += size;
240 sb->sb_wptr += size;
241 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
242 sb->sb_wptr -= sb->sb_datalen;
243 return size;
244err:
245
246 sofcantrcvmore(so);
247 tcp_sockclosed(sototcpcb(so));
248 fprintf(stderr, "soreadbuf buffer to small");
249 return -1;
250}
251
bellardf0cbd3e2004-04-22 00:10:48 +0000252/*
253 * Get urgent data
ths5fafdf22007-09-16 21:08:06 +0000254 *
bellardf0cbd3e2004-04-22 00:10:48 +0000255 * When the socket is created, we set it SO_OOBINLINE,
256 * so when OOB data arrives, we soread() it and everything
257 * in the send buffer is sent as urgent data
258 */
259void
blueswir1511d2b12009-03-07 15:32:56 +0000260sorecvoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000261{
262 struct tcpcb *tp = sototcpcb(so);
263
264 DEBUG_CALL("sorecvoob");
265 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000266
bellardf0cbd3e2004-04-22 00:10:48 +0000267 /*
268 * We take a guess at how much urgent data has arrived.
269 * In most situations, when urgent data arrives, the next
270 * read() should get all the urgent data. This guess will
271 * be wrong however if more data arrives just after the
ths5fafdf22007-09-16 21:08:06 +0000272 * urgent data, or the read() doesn't return all the
bellardf0cbd3e2004-04-22 00:10:48 +0000273 * urgent data.
274 */
275 soread(so);
276 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
277 tp->t_force = 1;
278 tcp_output(tp);
279 tp->t_force = 0;
280}
281
282/*
283 * Send urgent data
284 * There's a lot duplicated code here, but...
285 */
286int
blueswir1511d2b12009-03-07 15:32:56 +0000287sosendoob(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000288{
289 struct sbuf *sb = &so->so_rcv;
290 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
ths5fafdf22007-09-16 21:08:06 +0000291
bellardf0cbd3e2004-04-22 00:10:48 +0000292 int n, len;
ths5fafdf22007-09-16 21:08:06 +0000293
bellardf0cbd3e2004-04-22 00:10:48 +0000294 DEBUG_CALL("sosendoob");
295 DEBUG_ARG("so = %lx", (long)so);
296 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
ths5fafdf22007-09-16 21:08:06 +0000297
bellardf0cbd3e2004-04-22 00:10:48 +0000298 if (so->so_urgc > 2048)
299 so->so_urgc = 2048; /* XXXX */
ths5fafdf22007-09-16 21:08:06 +0000300
bellardf0cbd3e2004-04-22 00:10:48 +0000301 if (sb->sb_rptr < sb->sb_wptr) {
302 /* We can send it directly */
aliguorie1c5a2b2009-01-08 19:18:21 +0000303 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000304 so->so_urgc -= n;
ths3b46e622007-09-17 08:09:54 +0000305
bellardf0cbd3e2004-04-22 00:10:48 +0000306 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
307 } else {
ths5fafdf22007-09-16 21:08:06 +0000308 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000309 * Since there's no sendv or sendtov like writev,
310 * we must copy all data to a linear buffer then
311 * send it all
312 */
313 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
314 if (len > so->so_urgc) len = so->so_urgc;
315 memcpy(buff, sb->sb_rptr, len);
316 so->so_urgc -= len;
317 if (so->so_urgc) {
318 n = sb->sb_wptr - sb->sb_data;
319 if (n > so->so_urgc) n = so->so_urgc;
320 memcpy((buff + len), sb->sb_data, n);
321 so->so_urgc -= n;
322 len += n;
323 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000324 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
bellardf0cbd3e2004-04-22 00:10:48 +0000325#ifdef DEBUG
326 if (n != len)
327 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
ths3b46e622007-09-17 08:09:54 +0000328#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000329 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
330 }
ths5fafdf22007-09-16 21:08:06 +0000331
bellardf0cbd3e2004-04-22 00:10:48 +0000332 sb->sb_cc -= n;
333 sb->sb_rptr += n;
334 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
335 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000336
bellardf0cbd3e2004-04-22 00:10:48 +0000337 return n;
338}
339
340/*
ths5fafdf22007-09-16 21:08:06 +0000341 * Write data from so_rcv to so's socket,
bellardf0cbd3e2004-04-22 00:10:48 +0000342 * updating all sbuf field as necessary
343 */
344int
blueswir1511d2b12009-03-07 15:32:56 +0000345sowrite(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000346{
347 int n,nn;
348 struct sbuf *sb = &so->so_rcv;
349 int len = sb->sb_cc;
350 struct iovec iov[2];
ths5fafdf22007-09-16 21:08:06 +0000351
bellardf0cbd3e2004-04-22 00:10:48 +0000352 DEBUG_CALL("sowrite");
353 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000354
bellardf0cbd3e2004-04-22 00:10:48 +0000355 if (so->so_urgc) {
356 sosendoob(so);
357 if (sb->sb_cc == 0)
358 return 0;
359 }
360
361 /*
362 * No need to check if there's something to write,
363 * sowrite wouldn't have been called otherwise
364 */
ths5fafdf22007-09-16 21:08:06 +0000365
bellardf0cbd3e2004-04-22 00:10:48 +0000366 iov[0].iov_base = sb->sb_rptr;
blueswir166029f62008-10-01 19:39:40 +0000367 iov[1].iov_base = NULL;
368 iov[1].iov_len = 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000369 if (sb->sb_rptr < sb->sb_wptr) {
370 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
371 /* Should never succeed, but... */
372 if (iov[0].iov_len > len) iov[0].iov_len = len;
373 n = 1;
374 } else {
375 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
376 if (iov[0].iov_len > len) iov[0].iov_len = len;
377 len -= iov[0].iov_len;
378 if (len) {
379 iov[1].iov_base = sb->sb_data;
380 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
381 if (iov[1].iov_len > len) iov[1].iov_len = len;
382 n = 2;
383 } else
384 n = 1;
385 }
386 /* Check if there's urgent data to send, and if so, send it */
387
388#ifdef HAVE_READV
389 nn = writev(so->s, (const struct iovec *)iov, n);
ths5fafdf22007-09-16 21:08:06 +0000390
bellardf0cbd3e2004-04-22 00:10:48 +0000391 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
392#else
aliguorie1c5a2b2009-01-08 19:18:21 +0000393 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000394#endif
395 /* This should never happen, but people tell me it does *shrug* */
396 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
397 return 0;
ths5fafdf22007-09-16 21:08:06 +0000398
bellardf0cbd3e2004-04-22 00:10:48 +0000399 if (nn <= 0) {
400 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
401 so->so_state, errno));
402 sofcantsendmore(so);
403 tcp_sockclosed(sototcpcb(so));
404 return -1;
405 }
ths5fafdf22007-09-16 21:08:06 +0000406
bellardf0cbd3e2004-04-22 00:10:48 +0000407#ifndef HAVE_READV
bellard3bc21752004-11-24 20:39:26 +0000408 if (n == 2 && nn == iov[0].iov_len) {
409 int ret;
aliguorie1c5a2b2009-01-08 19:18:21 +0000410 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
bellard3bc21752004-11-24 20:39:26 +0000411 if (ret > 0)
412 nn += ret;
413 }
bellardf0cbd3e2004-04-22 00:10:48 +0000414 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
415#endif
ths5fafdf22007-09-16 21:08:06 +0000416
bellardf0cbd3e2004-04-22 00:10:48 +0000417 /* Update sbuf */
418 sb->sb_cc -= nn;
419 sb->sb_rptr += nn;
420 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
421 sb->sb_rptr -= sb->sb_datalen;
ths5fafdf22007-09-16 21:08:06 +0000422
bellardf0cbd3e2004-04-22 00:10:48 +0000423 /*
424 * If in DRAIN mode, and there's no more data, set
425 * it CANTSENDMORE
426 */
427 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
428 sofcantsendmore(so);
ths5fafdf22007-09-16 21:08:06 +0000429
bellardf0cbd3e2004-04-22 00:10:48 +0000430 return nn;
431}
432
433/*
434 * recvfrom() a UDP socket
435 */
436void
blueswir1511d2b12009-03-07 15:32:56 +0000437sorecvfrom(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000438{
439 struct sockaddr_in addr;
balrog242acf32008-05-10 01:49:53 +0000440 socklen_t addrlen = sizeof(struct sockaddr_in);
ths5fafdf22007-09-16 21:08:06 +0000441
bellardf0cbd3e2004-04-22 00:10:48 +0000442 DEBUG_CALL("sorecvfrom");
443 DEBUG_ARG("so = %lx", (long)so);
ths5fafdf22007-09-16 21:08:06 +0000444
bellardf0cbd3e2004-04-22 00:10:48 +0000445 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
446 char buff[256];
447 int len;
ths3b46e622007-09-17 08:09:54 +0000448
ths5fafdf22007-09-16 21:08:06 +0000449 len = recvfrom(so->s, buff, 256, 0,
bellardf0cbd3e2004-04-22 00:10:48 +0000450 (struct sockaddr *)&addr, &addrlen);
451 /* XXX Check if reply is "correct"? */
ths3b46e622007-09-17 08:09:54 +0000452
bellardf0cbd3e2004-04-22 00:10:48 +0000453 if(len == -1 || len == 0) {
454 u_char code=ICMP_UNREACH_PORT;
455
456 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
457 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
ths3b46e622007-09-17 08:09:54 +0000458
bellardf0cbd3e2004-04-22 00:10:48 +0000459 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
460 errno,strerror(errno)));
461 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
462 } else {
463 icmp_reflect(so->so_m);
blueswir1511d2b12009-03-07 15:32:56 +0000464 so->so_m = NULL; /* Don't m_free() it again! */
bellardf0cbd3e2004-04-22 00:10:48 +0000465 }
466 /* No need for this socket anymore, udp_detach it */
467 udp_detach(so);
468 } else { /* A "normal" UDP packet */
469 struct mbuf *m;
Blue Swirlc5b76b32009-06-13 08:44:31 +0000470 int len;
471#ifdef _WIN32
472 unsigned long n;
473#else
474 int n;
475#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000476
Jan Kiszka460fec62009-06-24 14:42:31 +0200477 m = m_get(so->slirp);
478 if (!m) {
479 return;
480 }
blueswir19634d902007-10-26 19:01:16 +0000481 m->m_data += IF_MAXLINKHDR;
ths3b46e622007-09-17 08:09:54 +0000482
ths5fafdf22007-09-16 21:08:06 +0000483 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000484 * XXX Shouldn't FIONREAD packets destined for port 53,
485 * but I don't know the max packet size for DNS lookups
486 */
487 len = M_FREEROOM(m);
488 /* if (so->so_fport != htons(53)) { */
bellard379ff532004-07-12 22:33:07 +0000489 ioctlsocket(so->s, FIONREAD, &n);
ths3b46e622007-09-17 08:09:54 +0000490
bellardf0cbd3e2004-04-22 00:10:48 +0000491 if (n > len) {
492 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
493 m_inc(m, n);
494 len = M_FREEROOM(m);
495 }
496 /* } */
ths3b46e622007-09-17 08:09:54 +0000497
bellardf0cbd3e2004-04-22 00:10:48 +0000498 m->m_len = recvfrom(so->s, m->m_data, len, 0,
499 (struct sockaddr *)&addr, &addrlen);
ths5fafdf22007-09-16 21:08:06 +0000500 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
bellardf0cbd3e2004-04-22 00:10:48 +0000501 m->m_len, errno,strerror(errno)));
502 if(m->m_len<0) {
503 u_char code=ICMP_UNREACH_PORT;
504
505 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
506 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
ths3b46e622007-09-17 08:09:54 +0000507
bellardf0cbd3e2004-04-22 00:10:48 +0000508 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
509 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
510 m_free(m);
511 } else {
512 /*
513 * Hack: domain name lookup will be used the most for UDP,
514 * and since they'll only be used once there's no need
515 * for the 4 minute (or whatever) timeout... So we time them
516 * out much quicker (10 seconds for now...)
517 */
518 if (so->so_expire) {
519 if (so->so_fport == htons(53))
520 so->so_expire = curtime + SO_EXPIREFAST;
521 else
522 so->so_expire = curtime + SO_EXPIRE;
523 }
524
ths5fafdf22007-09-16 21:08:06 +0000525 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000526 * If this packet was destined for CTL_ADDR,
527 * make it look like that's where it came from, done by udp_output
528 */
529 udp_output(so, m, &addr);
530 } /* rx error */
531 } /* if ping packet */
532}
533
534/*
535 * sendto() a socket
536 */
537int
blueswir1511d2b12009-03-07 15:32:56 +0000538sosendto(struct socket *so, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000539{
Jan Kiszka460fec62009-06-24 14:42:31 +0200540 Slirp *slirp = so->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000541 int ret;
542 struct sockaddr_in addr;
543
544 DEBUG_CALL("sosendto");
545 DEBUG_ARG("so = %lx", (long)so);
546 DEBUG_ARG("m = %lx", (long)m);
ths5fafdf22007-09-16 21:08:06 +0000547
bellardf0cbd3e2004-04-22 00:10:48 +0000548 addr.sin_family = AF_INET;
Jan Kiszka460fec62009-06-24 14:42:31 +0200549 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
550 slirp->vnetwork_addr.s_addr) {
bellardf0cbd3e2004-04-22 00:10:48 +0000551 /* It's an alias */
Jan Kiszka460fec62009-06-24 14:42:31 +0200552 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700553 if (get_dns_addr(&addr.sin_addr) < 0)
554 addr.sin_addr = loopback_addr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200555 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000556 addr.sin_addr = loopback_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000557 }
558 } else
559 addr.sin_addr = so->so_faddr;
560 addr.sin_port = so->so_fport;
561
562 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 +0000563
bellardf0cbd3e2004-04-22 00:10:48 +0000564 /* Don't care what port we get */
565 ret = sendto(so->s, m->m_data, m->m_len, 0,
566 (struct sockaddr *)&addr, sizeof (struct sockaddr));
567 if (ret < 0)
568 return -1;
ths5fafdf22007-09-16 21:08:06 +0000569
bellardf0cbd3e2004-04-22 00:10:48 +0000570 /*
571 * Kill the socket if there's no reply in 4 minutes,
572 * but only if it's an expirable socket
573 */
574 if (so->so_expire)
575 so->so_expire = curtime + SO_EXPIRE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200576 so->so_state &= SS_PERSISTENT_MASK;
577 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
bellardf0cbd3e2004-04-22 00:10:48 +0000578 return 0;
579}
580
581/*
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200582 * Listen for incoming TCP connections
bellardf0cbd3e2004-04-22 00:10:48 +0000583 */
584struct socket *
Stefan Weilb6dce922010-07-22 22:15:23 +0200585tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
Jan Kiszka460fec62009-06-24 14:42:31 +0200586 u_int lport, int flags)
bellardf0cbd3e2004-04-22 00:10:48 +0000587{
588 struct sockaddr_in addr;
589 struct socket *so;
balrog242acf32008-05-10 01:49:53 +0000590 int s, opt = 1;
591 socklen_t addrlen = sizeof(addr);
Juha Riihimäkiab07b982010-04-13 09:16:55 +0300592 memset(&addr, 0, addrlen);
bellardf0cbd3e2004-04-22 00:10:48 +0000593
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200594 DEBUG_CALL("tcp_listen");
Jan Kiszka9f349492009-06-24 14:42:29 +0200595 DEBUG_ARG("haddr = %x", haddr);
596 DEBUG_ARG("hport = %d", hport);
bellardf0cbd3e2004-04-22 00:10:48 +0000597 DEBUG_ARG("laddr = %x", laddr);
598 DEBUG_ARG("lport = %d", lport);
599 DEBUG_ARG("flags = %x", flags);
ths5fafdf22007-09-16 21:08:06 +0000600
Jan Kiszka460fec62009-06-24 14:42:31 +0200601 so = socreate(slirp);
602 if (!so) {
bellardf0cbd3e2004-04-22 00:10:48 +0000603 return NULL;
604 }
ths5fafdf22007-09-16 21:08:06 +0000605
bellardf0cbd3e2004-04-22 00:10:48 +0000606 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
607 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
608 free(so);
609 return NULL;
610 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200611 insque(so, &slirp->tcb);
ths5fafdf22007-09-16 21:08:06 +0000612
613 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000614 * SS_FACCEPTONCE sockets must time out.
615 */
616 if (flags & SS_FACCEPTONCE)
617 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
ths5fafdf22007-09-16 21:08:06 +0000618
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200619 so->so_state &= SS_PERSISTENT_MASK;
620 so->so_state |= (SS_FACCEPTCONN | flags);
bellardf0cbd3e2004-04-22 00:10:48 +0000621 so->so_lport = lport; /* Kept in network format */
622 so->so_laddr.s_addr = laddr; /* Ditto */
ths5fafdf22007-09-16 21:08:06 +0000623
bellardf0cbd3e2004-04-22 00:10:48 +0000624 addr.sin_family = AF_INET;
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200625 addr.sin_addr.s_addr = haddr;
626 addr.sin_port = hport;
ths5fafdf22007-09-16 21:08:06 +0000627
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100628 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
pbrookb55669b2006-03-11 20:48:36 +0000629 (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
bellardf0cbd3e2004-04-22 00:10:48 +0000630 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
631 (listen(s,1) < 0)) {
632 int tmperrno = errno; /* Don't clobber the real reason we failed */
ths3b46e622007-09-17 08:09:54 +0000633
bellardf0cbd3e2004-04-22 00:10:48 +0000634 close(s);
635 sofree(so);
636 /* Restore the real errno */
bellard02d2c542004-10-07 23:27:35 +0000637#ifdef _WIN32
638 WSASetLastError(tmperrno);
639#else
bellardf0cbd3e2004-04-22 00:10:48 +0000640 errno = tmperrno;
bellard02d2c542004-10-07 23:27:35 +0000641#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000642 return NULL;
643 }
bellardf0cbd3e2004-04-22 00:10:48 +0000644 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
ths5fafdf22007-09-16 21:08:06 +0000645
bellardf0cbd3e2004-04-22 00:10:48 +0000646 getsockname(s,(struct sockaddr *)&addr,&addrlen);
647 so->so_fport = addr.sin_port;
648 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
Jan Kiszka460fec62009-06-24 14:42:31 +0200649 so->so_faddr = slirp->vhost_addr;
bellardf0cbd3e2004-04-22 00:10:48 +0000650 else
651 so->so_faddr = addr.sin_addr;
652
653 so->s = s;
654 return so;
655}
656
bellardf0cbd3e2004-04-22 00:10:48 +0000657/*
658 * Various session state calls
659 * XXX Should be #define's
660 * The socket state stuff needs work, these often get call 2 or 3
661 * times each when only 1 was needed
662 */
663void
blueswir1511d2b12009-03-07 15:32:56 +0000664soisfconnecting(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000665{
666 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
667 SS_FCANTSENDMORE|SS_FWDRAIN);
668 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
669}
670
671void
blueswir1511d2b12009-03-07 15:32:56 +0000672soisfconnected(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000673{
674 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
675 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
676}
677
blueswir19634d902007-10-26 19:01:16 +0000678static void
679sofcantrcvmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000680{
681 if ((so->so_state & SS_NOFDREF) == 0) {
682 shutdown(so->s,0);
bellard02d2c542004-10-07 23:27:35 +0000683 if(global_writefds) {
684 FD_CLR(so->s,global_writefds);
685 }
bellardf0cbd3e2004-04-22 00:10:48 +0000686 }
687 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200688 if (so->so_state & SS_FCANTSENDMORE) {
689 so->so_state &= SS_PERSISTENT_MASK;
690 so->so_state |= SS_NOFDREF; /* Don't select it */
691 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000692 so->so_state |= SS_FCANTRCVMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200693 }
bellardf0cbd3e2004-04-22 00:10:48 +0000694}
695
blueswir19634d902007-10-26 19:01:16 +0000696static void
697sofcantsendmore(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000698{
699 if ((so->so_state & SS_NOFDREF) == 0) {
bellard02d2c542004-10-07 23:27:35 +0000700 shutdown(so->s,1); /* send FIN to fhost */
701 if (global_readfds) {
702 FD_CLR(so->s,global_readfds);
703 }
704 if (global_xfds) {
705 FD_CLR(so->s,global_xfds);
706 }
bellardf0cbd3e2004-04-22 00:10:48 +0000707 }
708 so->so_state &= ~(SS_ISFCONNECTING);
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200709 if (so->so_state & SS_FCANTRCVMORE) {
710 so->so_state &= SS_PERSISTENT_MASK;
711 so->so_state |= SS_NOFDREF; /* as above */
712 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000713 so->so_state |= SS_FCANTSENDMORE;
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200714 }
bellardf0cbd3e2004-04-22 00:10:48 +0000715}
716
bellardf0cbd3e2004-04-22 00:10:48 +0000717/*
718 * Set write drain mode
719 * Set CANTSENDMORE once all data has been write()n
720 */
721void
blueswir1511d2b12009-03-07 15:32:56 +0000722sofwdrain(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000723{
724 if (so->so_rcv.sb_cc)
725 so->so_state |= SS_FWDRAIN;
726 else
727 sofcantsendmore(so);
728}