VirtualBox

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

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

NAT: ioctl->ioctlsocket

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