VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/socket.c@ 26837

Last change on this file since 26837 was 26598, checked in by vboxsync, 15 years ago

NAT: read no more than available space in the mbuf.

  • Property svn:eol-style set to native
File size: 37.2 KB
Line 
1/*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
8#define WANT_SYS_IOCTL_H
9#include <slirp.h>
10#include "ip_icmp.h"
11#include "main.h"
12#ifdef __sun__
13#include <sys/filio.h>
14#endif
15#include <VBox/pdmdrv.h>
16#if defined (RT_OS_WINDOWS)
17#include <iphlpapi.h>
18#include <icmpapi.h>
19#endif
20
21
22static void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
23#ifdef RT_OS_WINDOWS
24static void sorecvfrom_icmp_win(PNATState, struct socket *);
25#else /* RT_OS_WINDOWS */
26static void sorecvfrom_icmp_unix(PNATState, struct socket *);
27#endif /* !RT_OS_WINDOWS */
28
29void
30so_init()
31{
32}
33
34
35struct socket *
36solookup(struct socket *head, struct in_addr laddr,
37 u_int lport, struct in_addr faddr, u_int fport)
38{
39 struct socket *so;
40
41 for (so = head->so_next; so != head; so = so->so_next)
42 {
43 if ( so->so_lport == lport
44 && so->so_laddr.s_addr == laddr.s_addr
45 && so->so_faddr.s_addr == faddr.s_addr
46 && so->so_fport == fport)
47 return so;
48 }
49
50 return (struct socket *)NULL;
51}
52
53/*
54 * Create a new socket, initialise the fields
55 * It is the responsibility of the caller to
56 * insque() it into the correct linked-list
57 */
58struct socket *
59socreate()
60{
61 struct socket *so;
62
63 so = (struct socket *)RTMemAllocZ(sizeof(struct socket));
64 if (so)
65 {
66 so->so_state = SS_NOFDREF;
67 so->s = -1;
68#if !defined(RT_OS_WINDOWS)
69 so->so_poll_index = -1;
70#endif
71 }
72 return so;
73}
74
75/*
76 * remque and free a socket, clobber cache
77 * VBOX_WITH_SLIRP_MT: before sofree queue should be locked, because
78 * in sofree we don't know from which queue item beeing removed.
79 */
80void
81sofree(PNATState pData, struct socket *so)
82{
83 struct socket *so_prev = NULL;
84 if (so == tcp_last_so)
85 tcp_last_so = &tcb;
86 else if (so == udp_last_so)
87 udp_last_so = &udb;
88
89 /* check if mbuf haven't been already freed */
90 if (so->so_m != NULL)
91 m_free(pData, so->so_m);
92#ifndef VBOX_WITH_SLIRP_MT
93 if (so->so_next && so->so_prev)
94 {
95 remque(pData, so); /* crashes if so is not in a queue */
96 NSOCK_DEC();
97 }
98
99 RTMemFree(so);
100#else
101 so->so_deleted = 1;
102#endif
103}
104
105#ifdef VBOX_WITH_SLIRP_MT
106void
107soread_queue(PNATState pData, struct socket *so, int *ret)
108{
109 *ret = soread(pData, so);
110}
111#endif
112
113/*
114 * Read from so's socket into sb_snd, updating all relevant sbuf fields
115 * NOTE: This will only be called if it is select()ed for reading, so
116 * a read() of 0 (or less) means it's disconnected
117 */
118int
119soread(PNATState pData, struct socket *so)
120{
121 int n, nn, lss, total;
122 struct sbuf *sb = &so->so_snd;
123 size_t len = sb->sb_datalen - sb->sb_cc;
124 struct iovec iov[2];
125 int mss = so->so_tcpcb->t_maxseg;
126
127 STAM_PROFILE_START(&pData->StatIOread, a);
128 STAM_COUNTER_RESET(&pData->StatIORead_in_1);
129 STAM_COUNTER_RESET(&pData->StatIORead_in_2);
130
131 QSOCKET_LOCK(tcb);
132 SOCKET_LOCK(so);
133 QSOCKET_UNLOCK(tcb);
134
135 DEBUG_CALL("soread");
136 DEBUG_ARG("so = %lx", (long )so);
137
138 /*
139 * No need to check if there's enough room to read.
140 * soread wouldn't have been called if there weren't
141 */
142
143 len = sb->sb_datalen - sb->sb_cc;
144
145 iov[0].iov_base = sb->sb_wptr;
146 iov[1].iov_base = 0;
147 iov[1].iov_len = 0;
148 if (sb->sb_wptr < sb->sb_rptr)
149 {
150 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
151 /* Should never succeed, but... */
152 if (iov[0].iov_len > len)
153 iov[0].iov_len = len;
154 if (iov[0].iov_len > mss)
155 iov[0].iov_len -= iov[0].iov_len%mss;
156 n = 1;
157 }
158 else
159 {
160 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
161 /* Should never succeed, but... */
162 if (iov[0].iov_len > len)
163 iov[0].iov_len = len;
164 len -= iov[0].iov_len;
165 if (len)
166 {
167 iov[1].iov_base = sb->sb_data;
168 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
169 if (iov[1].iov_len > len)
170 iov[1].iov_len = len;
171 total = iov[0].iov_len + iov[1].iov_len;
172 if (total > mss)
173 {
174 lss = total % mss;
175 if (iov[1].iov_len > lss)
176 {
177 iov[1].iov_len -= lss;
178 n = 2;
179 }
180 else
181 {
182 lss -= iov[1].iov_len;
183 iov[0].iov_len -= lss;
184 n = 1;
185 }
186 }
187 else
188 n = 2;
189 }
190 else
191 {
192 if (iov[0].iov_len > mss)
193 iov[0].iov_len -= iov[0].iov_len%mss;
194 n = 1;
195 }
196 }
197
198#ifdef HAVE_READV
199 nn = readv(so->s, (struct iovec *)iov, n);
200 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
201#else
202 nn = recv(so->s, iov[0].iov_base, iov[0].iov_len, 0);
203#endif
204 if (nn <= 0)
205 {
206#if defined(RT_OS_WINDOWS)
207 /*
208 * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
209 * _could_ mean that the connection is closed. But we will receive an
210 * FD_CLOSE event later if the connection was _really_ closed. With
211 * www.youtube.com I see this very often. Closing the socket too early
212 * would be dangerous.
213 */
214 int status, ignored;
215 unsigned long pending = 0;
216 status = WSAIoctl(so->s, FIONREAD, NULL, 0, &pending, sizeof(unsigned long), &ignored, NULL, NULL);
217 if (status < 0)
218 LogRel(("NAT:error in WSAIoctl: %d\n", WSAGetLastError()));
219 if (nn == 0 && (pending != 0))
220 {
221 SOCKET_UNLOCK(so);
222 STAM_PROFILE_STOP(&pData->StatIOread, a);
223 return 0;
224 }
225#endif
226 if ( nn < 0
227 && ( errno == EINTR
228 || errno == EAGAIN
229 || errno == EWOULDBLOCK))
230 {
231 SOCKET_UNLOCK(so);
232 STAM_PROFILE_STOP(&pData->StatIOread, a);
233 return 0;
234 }
235 else
236 {
237 /* nn == 0 means peer has performed an orderly shutdown */
238 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n",
239 nn, errno, strerror(errno)));
240 sofcantrcvmore(so);
241 tcp_sockclosed(pData, sototcpcb(so));
242 SOCKET_UNLOCK(so);
243 STAM_PROFILE_STOP(&pData->StatIOread, a);
244 return -1;
245 }
246 }
247 STAM_STATS(
248 if (n == 1)
249 {
250 STAM_COUNTER_INC(&pData->StatIORead_in_1);
251 STAM_COUNTER_ADD(&pData->StatIORead_in_1_bytes, nn);
252 }
253 else
254 {
255 STAM_COUNTER_INC(&pData->StatIORead_in_2);
256 STAM_COUNTER_ADD(&pData->StatIORead_in_2_1st_bytes, nn);
257 }
258 );
259
260#ifndef HAVE_READV
261 /*
262 * If there was no error, try and read the second time round
263 * We read again if n = 2 (ie, there's another part of the buffer)
264 * and we read as much as we could in the first read
265 * We don't test for <= 0 this time, because there legitimately
266 * might not be any more data (since the socket is non-blocking),
267 * a close will be detected on next iteration.
268 * A return of -1 wont (shouldn't) happen, since it didn't happen above
269 */
270 if (n == 2 && nn == iov[0].iov_len)
271 {
272 int ret;
273 ret = recv(so->s, iov[1].iov_base, iov[1].iov_len, 0);
274 if (ret > 0)
275 nn += ret;
276 STAM_STATS(
277 if (ret > 0)
278 {
279 STAM_COUNTER_INC(&pData->StatIORead_in_2);
280 STAM_COUNTER_ADD(&pData->StatIORead_in_2_2nd_bytes, ret);
281 }
282 );
283 }
284
285 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
286#endif
287
288 /* Update fields */
289 sb->sb_cc += nn;
290 sb->sb_wptr += nn;
291 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
292 sb->sb_wptr -= sb->sb_datalen;
293 STAM_PROFILE_STOP(&pData->StatIOread, a);
294 SOCKET_UNLOCK(so);
295 return nn;
296}
297
298/*
299 * Get urgent data
300 *
301 * When the socket is created, we set it SO_OOBINLINE,
302 * so when OOB data arrives, we soread() it and everything
303 * in the send buffer is sent as urgent data
304 */
305void
306sorecvoob(PNATState pData, struct socket *so)
307{
308 struct tcpcb *tp = sototcpcb(so);
309
310 DEBUG_CALL("sorecvoob");
311 DEBUG_ARG("so = %lx", (long)so);
312
313 /*
314 * We take a guess at how much urgent data has arrived.
315 * In most situations, when urgent data arrives, the next
316 * read() should get all the urgent data. This guess will
317 * be wrong however if more data arrives just after the
318 * urgent data, or the read() doesn't return all the
319 * urgent data.
320 */
321 soread(pData, so);
322 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
323 tp->t_force = 1;
324 tcp_output(pData, tp);
325 tp->t_force = 0;
326}
327
328/*
329 * Send urgent data
330 * There's a lot duplicated code here, but...
331 */
332int
333sosendoob(struct socket *so)
334{
335 struct sbuf *sb = &so->so_rcv;
336 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
337
338 int n, len;
339
340 DEBUG_CALL("sosendoob");
341 DEBUG_ARG("so = %lx", (long)so);
342 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
343
344 if (so->so_urgc > sizeof(buff))
345 so->so_urgc = sizeof(buff); /* XXX */
346
347 if (sb->sb_rptr < sb->sb_wptr)
348 {
349 /* We can send it directly */
350 n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
351 so->so_urgc -= n;
352
353 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n",
354 n, so->so_urgc));
355 }
356 else
357 {
358 /*
359 * Since there's no sendv or sendtov like writev,
360 * we must copy all data to a linear buffer then
361 * send it all
362 */
363 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
364 if (len > so->so_urgc)
365 len = so->so_urgc;
366 memcpy(buff, sb->sb_rptr, len);
367 so->so_urgc -= len;
368 if (so->so_urgc)
369 {
370 n = sb->sb_wptr - sb->sb_data;
371 if (n > so->so_urgc)
372 n = so->so_urgc;
373 memcpy(buff + len, sb->sb_data, n);
374 so->so_urgc -= n;
375 len += n;
376 }
377 n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
378#ifdef DEBUG
379 if (n != len)
380 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
381#endif
382 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
383 n, so->so_urgc));
384 }
385
386 sb->sb_cc -= n;
387 sb->sb_rptr += n;
388 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
389 sb->sb_rptr -= sb->sb_datalen;
390
391 return n;
392}
393
394/*
395 * Write data from so_rcv to so's socket,
396 * updating all sbuf field as necessary
397 */
398int
399sowrite(PNATState pData, struct socket *so)
400{
401 int n, nn;
402 struct sbuf *sb = &so->so_rcv;
403 size_t len = sb->sb_cc;
404 struct iovec iov[2];
405
406 STAM_PROFILE_START(&pData->StatIOwrite, a);
407 STAM_COUNTER_RESET(&pData->StatIOWrite_in_1);
408 STAM_COUNTER_RESET(&pData->StatIOWrite_in_1_bytes);
409 STAM_COUNTER_RESET(&pData->StatIOWrite_in_2);
410 STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_1st_bytes);
411 STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_2nd_bytes);
412 STAM_COUNTER_RESET(&pData->StatIOWrite_no_w);
413 STAM_COUNTER_RESET(&pData->StatIOWrite_rest);
414 STAM_COUNTER_RESET(&pData->StatIOWrite_rest_bytes);
415 DEBUG_CALL("sowrite");
416 DEBUG_ARG("so = %lx", (long)so);
417 QSOCKET_LOCK(tcb);
418 SOCKET_LOCK(so);
419 QSOCKET_UNLOCK(tcb);
420 if (so->so_urgc)
421 {
422 sosendoob(so);
423 if (sb->sb_cc == 0)
424 {
425 SOCKET_UNLOCK(so);
426 STAM_PROFILE_STOP(&pData->StatIOwrite, a);
427 return 0;
428 }
429 }
430
431 /*
432 * No need to check if there's something to write,
433 * sowrite wouldn't have been called otherwise
434 */
435
436 len = sb->sb_cc;
437
438 iov[0].iov_base = sb->sb_rptr;
439 iov[1].iov_base = 0;
440 iov[1].iov_len = 0;
441 if (sb->sb_rptr < sb->sb_wptr)
442 {
443 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
444 /* Should never succeed, but... */
445 if (iov[0].iov_len > len)
446 iov[0].iov_len = len;
447 n = 1;
448 }
449 else
450 {
451 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
452 if (iov[0].iov_len > len)
453 iov[0].iov_len = len;
454 len -= iov[0].iov_len;
455 if (len)
456 {
457 iov[1].iov_base = sb->sb_data;
458 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
459 if (iov[1].iov_len > len)
460 iov[1].iov_len = len;
461 n = 2;
462 }
463 else
464 n = 1;
465 }
466 STAM_STATS({
467 if (n == 1)
468 {
469 STAM_COUNTER_INC(&pData->StatIOWrite_in_1);
470 STAM_COUNTER_ADD(&pData->StatIOWrite_in_1_bytes, iov[0].iov_len);
471 }
472 else
473 {
474 STAM_COUNTER_INC(&pData->StatIOWrite_in_2);
475 STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_1st_bytes, iov[0].iov_len);
476 STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_2nd_bytes, iov[1].iov_len);
477 }
478 });
479 /* Check if there's urgent data to send, and if so, send it */
480#ifdef HAVE_READV
481 nn = writev(so->s, (const struct iovec *)iov, n);
482 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
483#else
484 nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
485#endif
486 /* This should never happen, but people tell me it does *shrug* */
487 if ( nn < 0
488 && ( errno == EAGAIN
489 || errno == EINTR
490 || errno == EWOULDBLOCK))
491 {
492 SOCKET_UNLOCK(so);
493 STAM_PROFILE_STOP(&pData->StatIOwrite, a);
494 return 0;
495 }
496
497 if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
498 {
499 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
500 so->so_state, errno));
501 sofcantsendmore(so);
502 tcp_sockclosed(pData, sototcpcb(so));
503 SOCKET_UNLOCK(so);
504 STAM_PROFILE_STOP(&pData->StatIOwrite, a);
505 return -1;
506 }
507
508#ifndef HAVE_READV
509 if (n == 2 && nn == iov[0].iov_len)
510 {
511 int ret;
512 ret = send(so->s, iov[1].iov_base, iov[1].iov_len, 0);
513 if (ret > 0)
514 nn += ret;
515 STAM_STATS({
516 if (ret > 0 && ret != iov[1].iov_len)
517 {
518 STAM_COUNTER_INC(&pData->StatIOWrite_rest);
519 STAM_COUNTER_ADD(&pData->StatIOWrite_rest_bytes, (ret - iov[1].iov_len));
520 }
521 });
522 }
523 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
524#endif
525
526 /* Update sbuf */
527 sb->sb_cc -= nn;
528 sb->sb_rptr += nn;
529 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
530 sb->sb_rptr -= sb->sb_datalen;
531
532 /*
533 * If in DRAIN mode, and there's no more data, set
534 * it CANTSENDMORE
535 */
536 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
537 sofcantsendmore(so);
538
539 SOCKET_UNLOCK(so);
540 STAM_PROFILE_STOP(&pData->StatIOwrite, a);
541 return nn;
542}
543
544/*
545 * recvfrom() a UDP socket
546 */
547void
548sorecvfrom(PNATState pData, struct socket *so)
549{
550 ssize_t ret = 0;
551 struct sockaddr_in addr;
552 socklen_t addrlen = sizeof(struct sockaddr_in);
553
554 DEBUG_CALL("sorecvfrom");
555 DEBUG_ARG("so = %lx", (long)so);
556
557 if (so->so_type == IPPROTO_ICMP)
558 {
559 /* This is a "ping" reply */
560#ifdef RT_OS_WINDOWS
561 sorecvfrom_icmp_win(pData, so);
562#else /* RT_OS_WINDOWS */
563 sorecvfrom_icmp_unix(pData, so);
564#endif /* !RT_OS_WINDOWS */
565 udp_detach(pData, so);
566 }
567 else
568 {
569 /* A "normal" UDP packet */
570 struct mbuf *m;
571 struct ethhdr *eh;
572 ssize_t len;
573 u_long n = 0;
574#ifdef VBOX_WITH_SLIRP_BSD_MBUF
575 int size;
576#endif
577 int rc = 0;
578 static int signalled = 0;
579
580 QSOCKET_LOCK(udb);
581 SOCKET_LOCK(so);
582 QSOCKET_UNLOCK(udb);
583
584#ifndef VBOX_WITH_SLIRP_BSD_MBUF
585 if (!(m = m_get(pData)))
586 {
587 SOCKET_UNLOCK(so);
588 return;
589 }
590 /* adjust both parameters to maks M_FREEROOM calculate correct */
591 m->m_data += if_maxlinkhdr + sizeof(struct udphdr) + sizeof(struct ip);
592
593 /*
594 * XXX Shouldn't FIONREAD packets destined for port 53,
595 * but I don't know the max packet size for DNS lookups
596 */
597 len = M_FREEROOM(m);
598 /* if (so->so_fport != RT_H2N_U16_C(53)) */
599 rc = ioctlsocket(so->s, FIONREAD, &n);
600 if ( rc == -1
601 && ( errno == EAGAIN
602 || errno == EWOULDBLOCK
603 || errno == EINPROGRESS
604 || errno == ENOTCONN))
605 {
606 m_free(pData, m);
607 return;
608 }
609
610 Log2(("NAT: %R[natsock] ioctlsocket before read "
611 "(rc:%d errno:%d, n:%d)\n", so, rc, errno, n));
612
613 if (rc == -1 && signalled == 0)
614 {
615 LogRel(("NAT: can't fetch amount of bytes on socket %R[natsock], so message will be truncated.\n", so));
616 signalled = 1;
617 m_free(pData, m);
618 return;
619 }
620
621 if (rc != -1 && n > len)
622 {
623 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
624 m_inc(m, n);
625 len = M_FREEROOM(m);
626 }
627 ret = recvfrom(so->s, m->m_data, len, 0,
628 (struct sockaddr *)&addr, &addrlen);
629 Log2(("NAT: %R[natsock] ioctlsocket after read "
630 "(rc:%d errno:%d, n:%d) ret:%d, len:%d\n", so,
631 rc, errno, n, ret, len));
632#else
633 /*How many data has been received ?*/
634 /*
635 * 1. calculate how much we can read
636 * 2. read as much as possible
637 * 3. attach buffer to allocated header mbuf
638 */
639 rc = ioctlsocket(so->s, FIONREAD, &n);
640 if (rc == -1 && signalled == 0)
641 {
642 LogRel(("NAT: can't fetch amount of bytes on socket %R[natsock], so message will be truncated.\n", so));
643 signalled = 1;
644 }
645
646 len = sizeof(struct udpiphdr) + ETH_HLEN;
647 if (n > (if_mtu - len))
648 {
649 n = if_mtu - len; /* can't read than we can put in the mbuf*/
650 }
651 len += n;
652
653 size = MCLBYTES;
654 if (len < MSIZE)
655 size = MCLBYTES;
656 else if (len < MCLBYTES)
657 size = MCLBYTES;
658 else if (len < MJUM9BYTES)
659 size = MJUM9BYTES;
660 else if (len < MJUM16BYTES)
661 size = MJUM16BYTES;
662 else
663 AssertMsgFailed(("Unsupported size"));
664
665 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
666 m->m_data += ETH_HLEN;
667 m->m_pkthdr.header = mtod(m, void *);
668 m->m_data += sizeof(struct udpiphdr);
669 ret = recvfrom(so->s, mtod(m, char *), n, 0,
670 (struct sockaddr *)&addr, &addrlen);
671 /* @todo (vvl) check which flags and type should be passed */
672#endif
673 m->m_len = ret;
674 if (ret < 0)
675 {
676 u_char code = ICMP_UNREACH_PORT;
677
678 if (errno == EHOSTUNREACH)
679 code = ICMP_UNREACH_HOST;
680 else if (errno == ENETUNREACH)
681 code = ICMP_UNREACH_NET;
682
683 m_free(pData, m);
684 if ( errno == EAGAIN
685 || errno == EWOULDBLOCK
686 || errno == EINPROGRESS
687 || errno == ENOTCONN)
688 {
689 return;
690 }
691
692 Log2((" rx error, tx icmp ICMP_UNREACH:%i\n", code));
693 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
694 so->so_m = NULL;
695 }
696 else
697 {
698 /*
699 * Hack: domain name lookup will be used the most for UDP,
700 * and since they'll only be used once there's no need
701 * for the 4 minute (or whatever) timeout... So we time them
702 * out much quicker (10 seconds for now...)
703 */
704 if (so->so_expire)
705 {
706 if (so->so_fport != RT_H2N_U16_C(53))
707 so->so_expire = curtime + SO_EXPIRE;
708 }
709 /*
710 * last argument should be changed if Slirp will inject IP attributes
711 * Note: Here we can't check if dnsproxy's sent initial request
712 */
713#ifndef VBOX_WITH_SLIRP_BSD_MBUF
714 if (so->so_fport == RT_H2N_U16_C(53))
715 dnsproxy_answer(pData, so, m);
716#endif
717
718#if 0
719 if (m->m_len == len)
720 {
721 m_inc(m, MINCSIZE);
722 m->m_len = 0;
723 }
724#endif
725
726 /*
727 * If this packet was destined for CTL_ADDR,
728 * make it look like that's where it came from, done by udp_output
729 */
730 udp_output(pData, so, m, &addr);
731 SOCKET_UNLOCK(so);
732 } /* rx error */
733 } /* if ping packet */
734}
735
736/*
737 * sendto() a socket
738 */
739int
740sosendto(PNATState pData, struct socket *so, struct mbuf *m)
741{
742 int ret;
743 struct sockaddr_in *paddr;
744 struct sockaddr addr;
745#if 0
746 struct sockaddr_in host_addr;
747#endif
748#ifdef VBOX_WITH_SLIRP_BSD_MBUF
749 caddr_t buf;
750 int mlen;
751#endif
752
753 DEBUG_CALL("sosendto");
754 DEBUG_ARG("so = %lx", (long)so);
755 DEBUG_ARG("m = %lx", (long)m);
756
757 memset(&addr, 0, sizeof(struct sockaddr));
758#ifdef RT_OS_DARWIN
759 addr.sa_len = sizeof(struct sockaddr_in);
760#endif
761 paddr = (struct sockaddr_in *)&addr;
762 paddr->sin_family = AF_INET;
763 if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
764 {
765 /* It's an alias */
766 uint32_t last_byte = RT_N2H_U32(so->so_faddr.s_addr) & ~pData->netmask;
767 switch(last_byte)
768 {
769#if 0
770 /* handle this case at 'default:' */
771 case CTL_BROADCAST:
772 addr.sin_addr.s_addr = INADDR_BROADCAST;
773 /* Send the packet to host to fully emulate broadcast */
774 /** @todo r=klaus: on Linux host this causes the host to receive
775 * the packet twice for some reason. And I cannot find any place
776 * in the man pages which states that sending a broadcast does not
777 * reach the host itself. */
778 host_addr.sin_family = AF_INET;
779 host_addr.sin_port = so->so_fport;
780 host_addr.sin_addr = our_addr;
781 sendto(so->s, m->m_data, m->m_len, 0,
782 (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
783 break;
784#endif
785 case CTL_DNS:
786 case CTL_ALIAS:
787 default:
788 if (last_byte == ~pData->netmask)
789 paddr->sin_addr.s_addr = INADDR_BROADCAST;
790 else
791 paddr->sin_addr = loopback_addr;
792 break;
793 }
794 }
795 else
796 paddr->sin_addr = so->so_faddr;
797 paddr->sin_port = so->so_fport;
798
799 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
800 RT_N2H_U16(paddr->sin_port), inet_ntoa(paddr->sin_addr)));
801
802 /* Don't care what port we get */
803#ifndef VBOX_WITH_SLIRP_BSD_MBUF
804 ret = sendto(so->s, m->m_data, m->m_len, 0, &addr, sizeof (struct sockaddr_in));
805#else
806 mlen = m_length(m, NULL);
807 buf = RTMemAlloc(mlen);
808 if (buf == NULL)
809 {
810 return -1;
811 }
812 m_copydata(m, 0, mlen, buf);
813 ret = sendto(so->s, buf, mlen, 0,
814 (struct sockaddr *)&addr, sizeof (struct sockaddr));
815#endif
816 if (ret < 0)
817 {
818 Log2(("UDP: sendto fails (%s)\n", strerror(errno)));
819 return -1;
820 }
821
822 /*
823 * Kill the socket if there's no reply in 4 minutes,
824 * but only if it's an expirable socket
825 */
826 if (so->so_expire)
827 so->so_expire = curtime + SO_EXPIRE;
828 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
829 return 0;
830}
831
832/*
833 * XXX This should really be tcp_listen
834 */
835struct socket *
836solisten(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
837{
838 struct sockaddr_in addr;
839 struct socket *so;
840 socklen_t addrlen = sizeof(addr);
841 int s, opt = 1;
842 int status;
843
844 DEBUG_CALL("solisten");
845 DEBUG_ARG("port = %d", port);
846 DEBUG_ARG("laddr = %x", laddr);
847 DEBUG_ARG("lport = %d", lport);
848 DEBUG_ARG("flags = %x", flags);
849
850 if ((so = socreate()) == NULL)
851 {
852 /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
853 return NULL;
854 }
855
856 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
857 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
858 {
859 RTMemFree(so);
860 return NULL;
861 }
862
863 SOCKET_LOCK_CREATE(so);
864 SOCKET_LOCK(so);
865 QSOCKET_LOCK(tcb);
866 insque(pData, so,&tcb);
867 NSOCK_INC();
868 QSOCKET_UNLOCK(tcb);
869
870 /*
871 * SS_FACCEPTONCE sockets must time out.
872 */
873 if (flags & SS_FACCEPTONCE)
874 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
875
876 so->so_state = (SS_FACCEPTCONN|flags);
877 so->so_lport = lport; /* Kept in network format */
878 so->so_laddr.s_addr = laddr; /* Ditto */
879
880 memset(&addr, 0, sizeof(addr));
881#ifdef RT_OS_DARWIN
882 addr.sin_len = sizeof(addr);
883#endif
884 addr.sin_family = AF_INET;
885 addr.sin_addr.s_addr = bind_addr;
886 addr.sin_port = port;
887
888 if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
889 || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
890 || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
891 || (listen(s, 1) < 0))
892 {
893#ifdef RT_OS_WINDOWS
894 int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
895 closesocket(s);
896 QSOCKET_LOCK(tcb);
897 sofree(pData, so);
898 QSOCKET_UNLOCK(tcb);
899 /* Restore the real errno */
900 WSASetLastError(tmperrno);
901#else
902 int tmperrno = errno; /* Don't clobber the real reason we failed */
903 close(s);
904 QSOCKET_LOCK(tcb);
905 sofree(pData, so);
906 QSOCKET_UNLOCK(tcb);
907 /* Restore the real errno */
908 errno = tmperrno;
909#endif
910 return NULL;
911 }
912 fd_nonblock(s);
913 setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
914
915 getsockname(s,(struct sockaddr *)&addr,&addrlen);
916 so->so_fport = addr.sin_port;
917 /* set socket buffers */
918 opt = pData->socket_rcv;
919 status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
920 if (status < 0)
921 {
922 LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
923 goto no_sockopt;
924 }
925 opt = pData->socket_snd;
926 status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
927 if (status < 0)
928 {
929 LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
930 goto no_sockopt;
931 }
932no_sockopt:
933 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
934 so->so_faddr = alias_addr;
935 else
936 so->so_faddr = addr.sin_addr;
937
938 so->s = s;
939 SOCKET_UNLOCK(so);
940 return so;
941}
942
943/*
944 * Data is available in so_rcv
945 * Just write() the data to the socket
946 * XXX not yet...
947 */
948void
949sorwakeup(struct socket *so)
950{
951#if 0
952 sowrite(so);
953 FD_CLR(so->s,&writefds);
954#endif
955}
956
957/*
958 * Data has been freed in so_snd
959 * We have room for a read() if we want to
960 * For now, don't read, it'll be done in the main loop
961 */
962void
963sowwakeup(struct socket *so)
964{
965}
966
967/*
968 * Various session state calls
969 * XXX Should be #define's
970 * The socket state stuff needs work, these often get call 2 or 3
971 * times each when only 1 was needed
972 */
973void
974soisfconnecting(struct socket *so)
975{
976 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
977 SS_FCANTSENDMORE|SS_FWDRAIN);
978 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
979}
980
981void
982soisfconnected(struct socket *so)
983{
984 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
985 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
986}
987
988void
989sofcantrcvmore(struct socket *so)
990{
991 if ((so->so_state & SS_NOFDREF) == 0)
992 {
993 shutdown(so->s, 0);
994 }
995 so->so_state &= ~(SS_ISFCONNECTING);
996 if (so->so_state & SS_FCANTSENDMORE)
997 so->so_state = SS_NOFDREF; /* Don't select it */
998 /* XXX close() here as well? */
999 else
1000 so->so_state |= SS_FCANTRCVMORE;
1001}
1002
1003void
1004sofcantsendmore(struct socket *so)
1005{
1006 if ((so->so_state & SS_NOFDREF) == 0)
1007 shutdown(so->s, 1); /* send FIN to fhost */
1008
1009 so->so_state &= ~(SS_ISFCONNECTING);
1010 if (so->so_state & SS_FCANTRCVMORE)
1011 so->so_state = SS_NOFDREF; /* as above */
1012 else
1013 so->so_state |= SS_FCANTSENDMORE;
1014}
1015
1016void
1017soisfdisconnected(struct socket *so)
1018{
1019#if 0
1020 so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
1021 close(so->s);
1022 so->so_state = SS_ISFDISCONNECTED;
1023 /*
1024 * XXX Do nothing ... ?
1025 */
1026#endif
1027}
1028
1029/*
1030 * Set write drain mode
1031 * Set CANTSENDMORE once all data has been write()n
1032 */
1033void
1034sofwdrain(struct socket *so)
1035{
1036 if (so->so_rcv.sb_cc)
1037 so->so_state |= SS_FWDRAIN;
1038 else
1039 sofcantsendmore(so);
1040}
1041
1042static void
1043send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
1044{
1045 struct ip *ip;
1046 uint32_t dst, src;
1047 char ip_copy[256];
1048 struct icmp *icp;
1049 int old_ip_len = 0;
1050 int hlen, original_hlen = 0;
1051 struct mbuf *m;
1052 struct icmp_msg *icm;
1053 uint8_t proto;
1054 int type = 0;
1055
1056 ip = (struct ip *)buff;
1057 hlen = (ip->ip_hl << 2);
1058 icp = (struct icmp *)((char *)ip + hlen);
1059
1060 Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
1061 if ( icp->icmp_type != ICMP_ECHOREPLY
1062 && icp->icmp_type != ICMP_TIMXCEED
1063 && icp->icmp_type != ICMP_UNREACH)
1064 {
1065 return;
1066 }
1067
1068 type = icp->icmp_type;
1069 if ( type == ICMP_TIMXCEED
1070 || type == ICMP_UNREACH)
1071 {
1072 ip = &icp->icmp_ip;
1073 }
1074
1075 icm = icmp_find_original_mbuf(pData, ip);
1076
1077 if (icm == NULL)
1078 {
1079 Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
1080 return;
1081 }
1082
1083 m = icm->im_m;
1084 Assert(m != NULL);
1085
1086 src = addr->sin_addr.s_addr;
1087
1088 ip = mtod(m, struct ip *);
1089 proto = ip->ip_p;
1090 /* Now ip is pointing on header we've sent from guest */
1091 if ( icp->icmp_type == ICMP_TIMXCEED
1092 || icp->icmp_type == ICMP_UNREACH)
1093 {
1094 old_ip_len = (ip->ip_hl << 2) + 64;
1095 if (old_ip_len > sizeof(ip_copy))
1096 old_ip_len = sizeof(ip_copy);
1097 memcpy(ip_copy, ip, old_ip_len);
1098 }
1099
1100 /* source address from original IP packet*/
1101 dst = ip->ip_src.s_addr;
1102
1103 /* overide ther tail of old packet */
1104 ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
1105 original_hlen = ip->ip_hl << 2;
1106 /* saves original ip header and options */
1107 memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
1108#ifndef VBOX_WITH_SLIRP_BSD_MBUF
1109 m->m_len = len - hlen + original_hlen;
1110 ip->ip_len = m->m_len;
1111#else
1112 ip->ip_len = m_length(m, NULL);
1113#endif
1114 ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
1115
1116 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
1117 type = icp->icmp_type;
1118 if ( type == ICMP_TIMXCEED
1119 || type == ICMP_UNREACH)
1120 {
1121 /* according RFC 793 error messages required copy of initial IP header + 64 bit */
1122 memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
1123 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
1124 }
1125
1126 ip->ip_src.s_addr = src;
1127 ip->ip_dst.s_addr = dst;
1128 icmp_reflect(pData, m);
1129 LIST_REMOVE(icm, im_list);
1130 /* Don't call m_free here*/
1131
1132 if ( type == ICMP_TIMXCEED
1133 || type == ICMP_UNREACH)
1134 {
1135 icm->im_so->so_m = NULL;
1136 switch (proto)
1137 {
1138 case IPPROTO_UDP:
1139 /*XXX: so->so_m already freed so we shouldn't call sofree */
1140 udp_detach(pData, icm->im_so);
1141 break;
1142 case IPPROTO_TCP:
1143 /*close tcp should be here */
1144 break;
1145 default:
1146 /* do nothing */
1147 break;
1148 }
1149 }
1150 RTMemFree(icm);
1151}
1152
1153#ifdef RT_OS_WINDOWS
1154static void
1155sorecvfrom_icmp_win(PNATState pData, struct socket *so)
1156{
1157 int len;
1158 int i;
1159 struct ip *ip;
1160 struct mbuf *m;
1161 struct icmp *icp;
1162 struct icmp_msg *icm;
1163 struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
1164 uint32_t src;
1165 ICMP_ECHO_REPLY *icr;
1166 int hlen = 0;
1167 int data_len = 0;
1168 int nbytes = 0;
1169 u_char code = ~0;
1170
1171 len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
1172 if (len < 0)
1173 {
1174 LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
1175 return;
1176 }
1177 if (len == 0)
1178 return; /* no error */
1179
1180 icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
1181 for (i = 0; i < len; ++i)
1182 {
1183 switch(icr[i].Status)
1184 {
1185 case IP_DEST_HOST_UNREACHABLE:
1186 code = (code != ~0 ? code : ICMP_UNREACH_HOST);
1187 case IP_DEST_NET_UNREACHABLE:
1188 code = (code != ~0 ? code : ICMP_UNREACH_NET);
1189 case IP_DEST_PROT_UNREACHABLE:
1190 code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
1191 /* UNREACH error inject here */
1192 case IP_DEST_PORT_UNREACHABLE:
1193 code = (code != ~0 ? code : ICMP_UNREACH_PORT);
1194 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
1195 so->so_m = NULL;
1196 break;
1197 case IP_SUCCESS: /* echo replied */
1198#ifndef VBOX_WITH_SLIRP_BSD_MBUF
1199 m = m_get(pData);
1200#else
1201 m = m_gethdr(pData, M_NOWAIT, MT_HEADER);
1202#endif
1203 m->m_data += if_maxlinkhdr;
1204 ip = mtod(m, struct ip *);
1205 ip->ip_src.s_addr = icr[i].Address;
1206 ip->ip_p = IPPROTO_ICMP;
1207 ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
1208 data_len = sizeof(struct ip);
1209 ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
1210 ip->ip_ttl = icr[i].Options.Ttl;
1211
1212 icp = (struct icmp *)&ip[1]; /* no options */
1213 icp->icmp_type = ICMP_ECHOREPLY;
1214 icp->icmp_code = 0;
1215 icp->icmp_id = so->so_icmp_id;
1216 icp->icmp_seq = so->so_icmp_seq;
1217
1218 data_len += ICMP_MINLEN;
1219
1220#ifndef VBOX_WITH_SLIRP_BSD_MBUF
1221 nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
1222 memcpy(icp->icmp_data, icr[i].Data, nbytes);
1223#else
1224 AssertMsgFailed(("ICMP"));
1225#endif
1226
1227 data_len += icr[i].DataSize;
1228
1229 ip->ip_len = data_len;
1230 m->m_len = ip->ip_len;
1231
1232 icmp_reflect(pData, m);
1233 break;
1234 case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
1235
1236 ip_broken = icr[i].Data;
1237 icm = icmp_find_original_mbuf(pData, ip_broken);
1238 if (icm == NULL) {
1239 Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
1240 return;
1241 }
1242 m = icm->im_m;
1243 ip = mtod(m, struct ip *);
1244 ip->ip_ttl = icr[i].Options.Ttl;
1245 src = ip->ip_src.s_addr;
1246 ip->ip_dst.s_addr = src;
1247 ip->ip_dst.s_addr = icr[i].Address;
1248
1249 hlen = (ip->ip_hl << 2);
1250 icp = (struct icmp *)((char *)ip + hlen);
1251 ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
1252 data_len = (ip_broken->ip_hl << 2) + 64;
1253
1254#ifndef VBOX_WITH_SLIRP_BSD_MBUF
1255 nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
1256 memcpy(icp->icmp_data, ip_broken, nbytes);
1257#else
1258 AssertMsgFailed(("ICMP"));
1259#endif
1260 icmp_reflect(pData, m);
1261 break;
1262 default:
1263 Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
1264 break;
1265 }
1266 }
1267}
1268#else /* RT_OS_WINDOWS */
1269static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
1270{
1271 struct sockaddr_in addr;
1272 socklen_t addrlen = sizeof(struct sockaddr_in);
1273 char *buff;
1274 int len = 0;
1275 int rc = 0;
1276 static int signalled = 0;
1277
1278 rc = ioctlsocket(so->s, FIONREAD, &len);
1279 if ( rc == -1
1280 && ( errno == EAGAIN
1281 || errno == EWOULDBLOCK
1282 || errno == EINPROGRESS
1283 || errno == ENOTCONN))
1284 {
1285 return;
1286 }
1287 if (rc == -1 && signalled == 0)
1288 {
1289 signalled = 1;
1290 LogRel(("NAT: fetching number of bits has been failed for ICMP socket (%d: %s)\n",
1291 errno, strerror(errno)));
1292 return;
1293 }
1294 len = (len != 0 && rc != -1 ? len : 1500);
1295 buff = RTMemAlloc(len);
1296 len = recvfrom(so->s, buff, len, 0,
1297 (struct sockaddr *)&addr, &addrlen);
1298 /* XXX Check if reply is "correct"? */
1299
1300 if (len == -1 || len == 0)
1301 {
1302 u_char code;
1303 if ( len == -1
1304 && (errno == EAGAIN
1305 || errno == EWOULDBLOCK
1306 || errno == EINPROGRESS
1307 || errno == ENOTCONN))
1308 {
1309 return;
1310 }
1311 code = ICMP_UNREACH_PORT;
1312
1313 if (errno == EHOSTUNREACH)
1314 code = ICMP_UNREACH_HOST;
1315 else if (errno == ENETUNREACH)
1316 code = ICMP_UNREACH_NET;
1317
1318 LogRel((" udp icmp rx errno = %d-%s\n",
1319 errno, strerror(errno)));
1320 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
1321 so->so_m = NULL;
1322 }
1323 else
1324 {
1325 send_icmp_to_guest(pData, buff, len, so, &addr);
1326 }
1327 RTMemFree(buff);
1328}
1329#endif /* !RT_OS_WINDOWS */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette