VirtualBox

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

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

*: scm cleans up whitespace and adds a new line at the end of ApplianceimplPrivate.h.

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