VirtualBox

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

Last change on this file since 33978 was 33978, checked in by vboxsync, 14 years ago

NAT: log.

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