VirtualBox

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

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

NAT: cosmetics; refined comments

  • 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 struct ethhdr *eh;
571 ssize_t len;
572 u_long n = 0;
573#ifdef VBOX_WITH_SLIRP_BSD_MBUF
574 int size;
575#endif
576 int rc = 0;
577 static int signalled = 0;
578
579 QSOCKET_LOCK(udb);
580 SOCKET_LOCK(so);
581 QSOCKET_UNLOCK(udb);
582
583#ifndef VBOX_WITH_SLIRP_BSD_MBUF
584 if (!(m = m_get(pData)))
585 {
586 SOCKET_UNLOCK(so);
587 return;
588 }
589 /* adjust both parameters to maks M_FREEROOM calculate correct */
590 m->m_data += if_maxlinkhdr + sizeof(struct udphdr) + sizeof(struct ip);
591
592 /*
593 * XXX Shouldn't FIONREAD packets destined for port 53,
594 * but I don't know the max packet size for DNS lookups
595 */
596 len = M_FREEROOM(m);
597 /* if (so->so_fport != RT_H2N_U16_C(53)) */
598 rc = ioctlsocket(so->s, FIONREAD, &n);
599 if ( rc == -1
600 && ( errno == EAGAIN
601 || errno == EWOULDBLOCK
602 || errno == EINPROGRESS
603 || errno == ENOTCONN))
604 {
605 m_free(pData, m);
606 return;
607 }
608
609 Log2(("NAT: %R[natsock] ioctlsocket before read "
610 "(rc:%d errno:%d, n:%d)\n", so, rc, errno, n));
611
612 if (rc == -1 && signalled == 0)
613 {
614 LogRel(("NAT: can't fetch amount of bytes on socket %R[natsock], so message will be truncated.\n", so));
615 signalled = 1;
616 m_free(pData, m);
617 return;
618 }
619
620 if (rc != -1 && n > len)
621 {
622 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
623 m_inc(m, n);
624 len = M_FREEROOM(m);
625 }
626 ret = recvfrom(so->s, m->m_data, len, 0,
627 (struct sockaddr *)&addr, &addrlen);
628 Log2(("NAT: %R[natsock] ioctlsocket after read "
629 "(rc:%d errno:%d, n:%d) ret:%d, len:%d\n", so,
630 rc, errno, n, ret, len));
631#else
632 /*How many data has been received ?*/
633 /*
634 * 1. calculate how much we can read
635 * 2. read as much as possible
636 * 3. attach buffer to allocated header mbuf
637 */
638 rc = ioctlsocket(so->s, FIONREAD, &n);
639 if (rc == -1 && signalled == 0)
640 {
641 LogRel(("NAT: can't fetch amount of bytes on socket %R[natsock], so message will be truncated.\n", so));
642 signalled = 1;
643 }
644
645 len = sizeof(struct udpiphdr) + ETH_HLEN;
646 if (n > (if_mtu - len))
647 {
648 n = if_mtu - len; /* can't read than we can put in the mbuf*/
649 }
650 len += n;
651
652 size = MCLBYTES;
653 if (len < MSIZE)
654 size = MCLBYTES;
655 else if (len < MCLBYTES)
656 size = MCLBYTES;
657 else if (len < MJUM9BYTES)
658 size = MJUM9BYTES;
659 else if (len < MJUM16BYTES)
660 size = MJUM16BYTES;
661 else
662 AssertMsgFailed(("Unsupported size"));
663
664 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
665 m->m_data += ETH_HLEN;
666 m->m_pkthdr.header = mtod(m, void *);
667 m->m_data += sizeof(struct udpiphdr);
668 ret = recvfrom(so->s, mtod(m, char *), n, 0,
669 (struct sockaddr *)&addr, &addrlen);
670 /* @todo (vvl) check which flags and type should be passed */
671#endif
672 m->m_len = ret;
673 if (ret < 0)
674 {
675 u_char code = ICMP_UNREACH_PORT;
676
677 if (errno == EHOSTUNREACH)
678 code = ICMP_UNREACH_HOST;
679 else if (errno == ENETUNREACH)
680 code = ICMP_UNREACH_NET;
681
682 m_free(pData, m);
683 if ( errno == EAGAIN
684 || errno == EWOULDBLOCK
685 || errno == EINPROGRESS
686 || errno == ENOTCONN)
687 {
688 return;
689 }
690
691 Log2((" rx error, tx icmp ICMP_UNREACH:%i\n", code));
692 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
693 so->so_m = NULL;
694 }
695 else
696 {
697 /*
698 * Hack: domain name lookup will be used the most for UDP,
699 * and since they'll only be used once there's no need
700 * for the 4 minute (or whatever) timeout... So we time them
701 * out much quicker (10 seconds for now...)
702 */
703 if (so->so_expire)
704 {
705 if (so->so_fport != RT_H2N_U16_C(53))
706 so->so_expire = curtime + SO_EXPIRE;
707 }
708 /*
709 * last argument should be changed if Slirp will inject IP attributes
710 * Note: Here we can't check if dnsproxy's sent initial request
711 */
712#ifndef VBOX_WITH_SLIRP_BSD_MBUF
713 if (so->so_fport == RT_H2N_U16_C(53))
714 dnsproxy_answer(pData, so, m);
715#endif
716
717#if 0
718 if (m->m_len == len)
719 {
720 m_inc(m, MINCSIZE);
721 m->m_len = 0;
722 }
723#endif
724
725 /*
726 * If this packet was destined for CTL_ADDR,
727 * make it look like that's where it came from, done by udp_output
728 */
729 udp_output(pData, so, m, &addr);
730 SOCKET_UNLOCK(so);
731 } /* rx error */
732 } /* if ping packet */
733}
734
735/*
736 * sendto() a socket
737 */
738int
739sosendto(PNATState pData, struct socket *so, struct mbuf *m)
740{
741 int ret;
742 struct sockaddr_in *paddr;
743 struct sockaddr addr;
744#if 0
745 struct sockaddr_in host_addr;
746#endif
747#ifdef VBOX_WITH_SLIRP_BSD_MBUF
748 caddr_t buf;
749 int mlen;
750#endif
751
752 DEBUG_CALL("sosendto");
753 DEBUG_ARG("so = %lx", (long)so);
754 DEBUG_ARG("m = %lx", (long)m);
755
756 memset(&addr, 0, sizeof(struct sockaddr));
757#ifdef RT_OS_DARWIN
758 addr.sa_len = sizeof(struct sockaddr_in);
759#endif
760 paddr = (struct sockaddr_in *)&addr;
761 paddr->sin_family = AF_INET;
762 if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
763 {
764 /* It's an alias */
765 uint32_t last_byte = RT_N2H_U32(so->so_faddr.s_addr) & ~pData->netmask;
766 switch(last_byte)
767 {
768#if 0
769 /* handle this case at 'default:' */
770 case CTL_BROADCAST:
771 addr.sin_addr.s_addr = INADDR_BROADCAST;
772 /* Send the packet to host to fully emulate broadcast */
773 /** @todo r=klaus: on Linux host this causes the host to receive
774 * the packet twice for some reason. And I cannot find any place
775 * in the man pages which states that sending a broadcast does not
776 * reach the host itself. */
777 host_addr.sin_family = AF_INET;
778 host_addr.sin_port = so->so_fport;
779 host_addr.sin_addr = our_addr;
780 sendto(so->s, m->m_data, m->m_len, 0,
781 (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
782 break;
783#endif
784 case CTL_DNS:
785 case CTL_ALIAS:
786 default:
787 if (last_byte == ~pData->netmask)
788 paddr->sin_addr.s_addr = INADDR_BROADCAST;
789 else
790 paddr->sin_addr = loopback_addr;
791 break;
792 }
793 }
794 else
795 paddr->sin_addr = so->so_faddr;
796 paddr->sin_port = so->so_fport;
797
798 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
799 RT_N2H_U16(paddr->sin_port), inet_ntoa(paddr->sin_addr)));
800
801 /* Don't care what port we get */
802#ifndef VBOX_WITH_SLIRP_BSD_MBUF
803 ret = sendto(so->s, m->m_data, m->m_len, 0, &addr, sizeof (struct sockaddr_in));
804#else
805 mlen = m_length(m, NULL);
806 buf = RTMemAlloc(mlen);
807 if (buf == NULL)
808 {
809 return -1;
810 }
811 m_copydata(m, 0, mlen, buf);
812 ret = sendto(so->s, buf, mlen, 0,
813 (struct sockaddr *)&addr, sizeof (struct sockaddr));
814#endif
815 if (ret < 0)
816 {
817 Log2(("UDP: sendto fails (%s)\n", strerror(errno)));
818 return -1;
819 }
820
821 /*
822 * Kill the socket if there's no reply in 4 minutes,
823 * but only if it's an expirable socket
824 */
825 if (so->so_expire)
826 so->so_expire = curtime + SO_EXPIRE;
827 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
828 return 0;
829}
830
831/*
832 * XXX This should really be tcp_listen
833 */
834struct socket *
835solisten(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
836{
837 struct sockaddr_in addr;
838 struct socket *so;
839 socklen_t addrlen = sizeof(addr);
840 int s, opt = 1;
841 int status;
842
843 DEBUG_CALL("solisten");
844 DEBUG_ARG("port = %d", port);
845 DEBUG_ARG("laddr = %x", laddr);
846 DEBUG_ARG("lport = %d", lport);
847 DEBUG_ARG("flags = %x", flags);
848
849 if ((so = socreate()) == NULL)
850 {
851 /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
852 return NULL;
853 }
854
855 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
856 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
857 {
858 RTMemFree(so);
859 return NULL;
860 }
861
862 SOCKET_LOCK_CREATE(so);
863 SOCKET_LOCK(so);
864 QSOCKET_LOCK(tcb);
865 insque(pData, so,&tcb);
866 NSOCK_INC();
867 QSOCKET_UNLOCK(tcb);
868
869 /*
870 * SS_FACCEPTONCE sockets must time out.
871 */
872 if (flags & SS_FACCEPTONCE)
873 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
874
875 so->so_state = (SS_FACCEPTCONN|flags);
876 so->so_lport = lport; /* Kept in network format */
877 so->so_laddr.s_addr = laddr; /* Ditto */
878
879 memset(&addr, 0, sizeof(addr));
880#ifdef RT_OS_DARWIN
881 addr.sin_len = sizeof(addr);
882#endif
883 addr.sin_family = AF_INET;
884 addr.sin_addr.s_addr = bind_addr;
885 addr.sin_port = port;
886
887 if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
888 || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
889 || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
890 || (listen(s, 1) < 0))
891 {
892#ifdef RT_OS_WINDOWS
893 int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
894 closesocket(s);
895 QSOCKET_LOCK(tcb);
896 sofree(pData, so);
897 QSOCKET_UNLOCK(tcb);
898 /* Restore the real errno */
899 WSASetLastError(tmperrno);
900#else
901 int tmperrno = errno; /* Don't clobber the real reason we failed */
902 close(s);
903 QSOCKET_LOCK(tcb);
904 sofree(pData, so);
905 QSOCKET_UNLOCK(tcb);
906 /* Restore the real errno */
907 errno = tmperrno;
908#endif
909 return NULL;
910 }
911 fd_nonblock(s);
912 setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
913
914 getsockname(s,(struct sockaddr *)&addr,&addrlen);
915 so->so_fport = addr.sin_port;
916 /* set socket buffers */
917 opt = pData->socket_rcv;
918 status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
919 if (status < 0)
920 {
921 LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
922 goto no_sockopt;
923 }
924 opt = pData->socket_snd;
925 status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
926 if (status < 0)
927 {
928 LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
929 goto no_sockopt;
930 }
931no_sockopt:
932 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
933 so->so_faddr = alias_addr;
934 else
935 so->so_faddr = addr.sin_addr;
936
937 so->s = s;
938 SOCKET_UNLOCK(so);
939 return so;
940}
941
942/*
943 * Data is available in so_rcv
944 * Just write() the data to the socket
945 * XXX not yet...
946 */
947void
948sorwakeup(struct socket *so)
949{
950#if 0
951 sowrite(so);
952 FD_CLR(so->s,&writefds);
953#endif
954}
955
956/*
957 * Data has been freed in so_snd
958 * We have room for a read() if we want to
959 * For now, don't read, it'll be done in the main loop
960 */
961void
962sowwakeup(struct socket *so)
963{
964}
965
966/*
967 * Various session state calls
968 * XXX Should be #define's
969 * The socket state stuff needs work, these often get call 2 or 3
970 * times each when only 1 was needed
971 */
972void
973soisfconnecting(struct socket *so)
974{
975 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
976 SS_FCANTSENDMORE|SS_FWDRAIN);
977 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
978}
979
980void
981soisfconnected(struct socket *so)
982{
983 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
984 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
985}
986
987void
988sofcantrcvmore(struct socket *so)
989{
990 if ((so->so_state & SS_NOFDREF) == 0)
991 {
992 shutdown(so->s, 0);
993 }
994 so->so_state &= ~(SS_ISFCONNECTING);
995 if (so->so_state & SS_FCANTSENDMORE)
996 so->so_state = SS_NOFDREF; /* Don't select it */
997 /* XXX close() here as well? */
998 else
999 so->so_state |= SS_FCANTRCVMORE;
1000}
1001
1002void
1003sofcantsendmore(struct socket *so)
1004{
1005 if ((so->so_state & SS_NOFDREF) == 0)
1006 shutdown(so->s, 1); /* send FIN to fhost */
1007
1008 so->so_state &= ~(SS_ISFCONNECTING);
1009 if (so->so_state & SS_FCANTRCVMORE)
1010 so->so_state = SS_NOFDREF; /* as above */
1011 else
1012 so->so_state |= SS_FCANTSENDMORE;
1013}
1014
1015void
1016soisfdisconnected(struct socket *so)
1017{
1018#if 0
1019 so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
1020 close(so->s);
1021 so->so_state = SS_ISFDISCONNECTED;
1022 /*
1023 * XXX Do nothing ... ?
1024 */
1025#endif
1026}
1027
1028/*
1029 * Set write drain mode
1030 * Set CANTSENDMORE once all data has been write()n
1031 */
1032void
1033sofwdrain(struct socket *so)
1034{
1035 if (so->so_rcv.sb_cc)
1036 so->so_state |= SS_FWDRAIN;
1037 else
1038 sofcantsendmore(so);
1039}
1040
1041static void
1042send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
1043{
1044 struct ip *ip;
1045 uint32_t dst, src;
1046 char ip_copy[256];
1047 struct icmp *icp;
1048 int old_ip_len = 0;
1049 int hlen, original_hlen = 0;
1050 struct mbuf *m;
1051 struct icmp_msg *icm;
1052 uint8_t proto;
1053 int type = 0;
1054 int m_room;
1055
1056 ip = (struct ip *)buff;
1057 /* Fix ip->ip_len to contain the total packet length including the header
1058 * in _host_ byte order for all OSes. On Darwin, that value already is in
1059 * host byte order. Solaris and Darwin report only the payload. */
1060#ifndef RT_OS_DARWIN
1061 ip->ip_len = RT_N2H_U16(ip->ip_len);
1062#endif
1063 hlen = (ip->ip_hl << 2);
1064#if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
1065 ip->ip_len += hlen;
1066#endif
1067 if (ip->ip_len < hlen + ICMP_MINLEN)
1068 {
1069 Log(("send_icmp_to_guest: ICMP header is too small to understand which type/subtype of the datagram\n"));
1070 return;
1071 }
1072 icp = (struct icmp *)((char *)ip + hlen);
1073
1074 Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
1075 if ( icp->icmp_type != ICMP_ECHOREPLY
1076 && icp->icmp_type != ICMP_TIMXCEED
1077 && icp->icmp_type != ICMP_UNREACH)
1078 {
1079 return;
1080 }
1081
1082 /*
1083 * ICMP_ECHOREPLY, ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
1084 * ICMP_ECHOREPLY assuming data 0
1085 * icmp_{type(8), code(8), cksum(16),identifier(16),seqnum(16)}
1086 */
1087 if (ip->ip_len < hlen + 8)
1088 {
1089 Log(("send_icmp_to_guest: NAT accept ICMP_{ECHOREPLY, TIMXCEED, UNREACH} the minimum size is 64 (see rfc792)\n"));
1090 return;
1091 }
1092
1093 type = icp->icmp_type;
1094 if ( type == ICMP_TIMXCEED
1095 || type == ICMP_UNREACH)
1096 {
1097 /*
1098 * ICMP_TIMXCEED, ICMP_UNREACH minimal header size is
1099 * icmp_{type(8), code(8), cksum(16),unused(32)} + IP header + 64 bit of original datagram
1100 */
1101 if (ip->ip_len < hlen + 2*8 + sizeof(struct ip))
1102 {
1103 Log(("send_icmp_to_guest: NAT accept ICMP_{TIMXCEED, UNREACH} the minimum size of ipheader + 64 bit of data (see rfc792)\n"));
1104 return;
1105 }
1106 ip = &icp->icmp_ip;
1107 }
1108
1109 icm = icmp_find_original_mbuf(pData, ip);
1110 if (icm == NULL)
1111 {
1112 Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
1113 return;
1114 }
1115
1116 m = icm->im_m;
1117 Assert(m != NULL);
1118
1119 src = addr->sin_addr.s_addr;
1120 if (type == ICMP_ECHOREPLY)
1121 {
1122 struct ip *ip0 = mtod(m, struct ip *);
1123 struct icmp *icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
1124 if (icp0->icmp_type != ICMP_ECHO)
1125 {
1126 Log(("NAT: we haven't found echo for this reply\n"));
1127 return;
1128 }
1129 /*
1130 * while combining buffer to send (see ip_icmp.c) we control ICMP header only,
1131 * IP header combined by OS network stack, our local copy of IP header contians values
1132 * in host byte order so no byte order conversion is required. IP headers fields are converting
1133 * in ip_output0 routine only.
1134 */
1135 if ( (ip->ip_len - hlen)
1136 != (ip0->ip_len - (ip0->ip_hl << 2)))
1137 {
1138 Log(("NAT: ECHO(%d) lenght doesn't match ECHOREPLY(%d)\n",
1139 (ip->ip_len - hlen), (ip0->ip_len - (ip0->ip_hl << 2))));
1140 return;
1141 }
1142 }
1143
1144 /* ip points on origianal ip header */
1145 ip = mtod(m, struct ip *);
1146 proto = ip->ip_p;
1147 /* Now ip is pointing on header we've sent from guest */
1148 if ( icp->icmp_type == ICMP_TIMXCEED
1149 || icp->icmp_type == ICMP_UNREACH)
1150 {
1151 old_ip_len = (ip->ip_hl << 2) + 64;
1152 if (old_ip_len > sizeof(ip_copy))
1153 old_ip_len = sizeof(ip_copy);
1154 memcpy(ip_copy, ip, old_ip_len);
1155 }
1156
1157 /* source address from original IP packet*/
1158 dst = ip->ip_src.s_addr;
1159
1160 /* overide ther tail of old packet */
1161 ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
1162 original_hlen = ip->ip_hl << 2;
1163 /* saves original ip header and options */
1164#ifdef VBOX_WITH_SLIRP_BSD_MBUF
1165 m_copyback(pData, m, original_hlen, len - hlen, buff + hlen);
1166 ip->ip_len = m_length(m, NULL);
1167#else
1168 /* m_room space in the saved m buffer */
1169 m_room = M_ROOM(m);
1170 if (m_room < len - hlen + original_hlen)
1171 {
1172 /* we need involve ether header length into new buffer buffer calculation */
1173 m_inc(m, if_maxlinkhdr + len - hlen + original_hlen);
1174 if (m->m_size < if_maxlinkhdr + len - hlen + original_hlen)
1175 {
1176 Log(("send_icmp_to_guest: extending buffer was failed (packet is dropped)\n"));
1177 return;
1178 }
1179 }
1180 memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
1181 m->m_len = len - hlen + original_hlen;
1182 ip->ip_len = m->m_len;
1183#endif
1184 ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
1185
1186 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
1187 type = icp->icmp_type;
1188 if ( type == ICMP_TIMXCEED
1189 || type == ICMP_UNREACH)
1190 {
1191 /* according RFC 793 error messages required copy of initial IP header + 64 bit */
1192 memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
1193 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
1194 }
1195
1196 ip->ip_src.s_addr = src;
1197 ip->ip_dst.s_addr = dst;
1198 icmp_reflect(pData, m);
1199 LIST_REMOVE(icm, im_list);
1200 /* Don't call m_free here*/
1201
1202 if ( type == ICMP_TIMXCEED
1203 || type == ICMP_UNREACH)
1204 {
1205 icm->im_so->so_m = NULL;
1206 switch (proto)
1207 {
1208 case IPPROTO_UDP:
1209 /*XXX: so->so_m already freed so we shouldn't call sofree */
1210 udp_detach(pData, icm->im_so);
1211 break;
1212 case IPPROTO_TCP:
1213 /*close tcp should be here */
1214 break;
1215 default:
1216 /* do nothing */
1217 break;
1218 }
1219 }
1220 RTMemFree(icm);
1221}
1222
1223#ifdef RT_OS_WINDOWS
1224static void
1225sorecvfrom_icmp_win(PNATState pData, struct socket *so)
1226{
1227 int len;
1228 int i;
1229 struct ip *ip;
1230 struct mbuf *m;
1231 struct icmp *icp;
1232 struct icmp_msg *icm;
1233 struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
1234 uint32_t src;
1235 ICMP_ECHO_REPLY *icr;
1236 int hlen = 0;
1237 int data_len = 0;
1238 int nbytes = 0;
1239 u_char code = ~0;
1240
1241 len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
1242 if (len < 0)
1243 {
1244 LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
1245 return;
1246 }
1247 if (len == 0)
1248 return; /* no error */
1249
1250 icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
1251 for (i = 0; i < len; ++i)
1252 {
1253 switch(icr[i].Status)
1254 {
1255 case IP_DEST_HOST_UNREACHABLE:
1256 code = (code != ~0 ? code : ICMP_UNREACH_HOST);
1257 case IP_DEST_NET_UNREACHABLE:
1258 code = (code != ~0 ? code : ICMP_UNREACH_NET);
1259 case IP_DEST_PROT_UNREACHABLE:
1260 code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
1261 /* UNREACH error inject here */
1262 case IP_DEST_PORT_UNREACHABLE:
1263 code = (code != ~0 ? code : ICMP_UNREACH_PORT);
1264 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
1265 so->so_m = NULL;
1266 break;
1267 case IP_SUCCESS: /* echo replied */
1268# ifndef VBOX_WITH_SLIRP_BSD_MBUF
1269 m = m_get(pData);
1270# else
1271 m = m_gethdr(pData, M_NOWAIT, MT_HEADER);
1272# endif
1273 m->m_data += if_maxlinkhdr;
1274 ip = mtod(m, struct ip *);
1275 ip->ip_src.s_addr = icr[i].Address;
1276 ip->ip_p = IPPROTO_ICMP;
1277 ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
1278 data_len = sizeof(struct ip);
1279 ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
1280 ip->ip_ttl = icr[i].Options.Ttl;
1281
1282 icp = (struct icmp *)&ip[1]; /* no options */
1283 icp->icmp_type = ICMP_ECHOREPLY;
1284 icp->icmp_code = 0;
1285 icp->icmp_id = so->so_icmp_id;
1286 icp->icmp_seq = so->so_icmp_seq;
1287
1288 data_len += ICMP_MINLEN;
1289
1290# ifndef VBOX_WITH_SLIRP_BSD_MBUF
1291 nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
1292 memcpy(icp->icmp_data, icr[i].Data, nbytes);
1293# else
1294 AssertMsgFailed(("ICMP"));
1295# endif
1296
1297 data_len += icr[i].DataSize;
1298
1299 ip->ip_len = data_len;
1300 m->m_len = ip->ip_len;
1301
1302 icmp_reflect(pData, m);
1303 break;
1304 case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
1305
1306 ip_broken = icr[i].Data;
1307 icm = icmp_find_original_mbuf(pData, ip_broken);
1308 if (icm == NULL) {
1309 Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
1310 return;
1311 }
1312 m = icm->im_m;
1313 ip = mtod(m, struct ip *);
1314 ip->ip_ttl = icr[i].Options.Ttl;
1315 src = ip->ip_src.s_addr;
1316 ip->ip_dst.s_addr = src;
1317 ip->ip_dst.s_addr = icr[i].Address;
1318
1319 hlen = (ip->ip_hl << 2);
1320 icp = (struct icmp *)((char *)ip + hlen);
1321 ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
1322 data_len = (ip_broken->ip_hl << 2) + 64;
1323
1324#ifndef VBOX_WITH_SLIRP_BSD_MBUF
1325 nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
1326 memcpy(icp->icmp_data, ip_broken, nbytes);
1327#else
1328 AssertMsgFailed(("ICMP"));
1329#endif
1330 icmp_reflect(pData, m);
1331 break;
1332 default:
1333 Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
1334 break;
1335 }
1336 }
1337}
1338#else /* !RT_OS_WINDOWS */
1339static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
1340{
1341 struct sockaddr_in addr;
1342 socklen_t addrlen = sizeof(struct sockaddr_in);
1343 struct ip ip;
1344 char *buff;
1345 int len = 0;
1346
1347 /* 1- step: read the ip header */
1348 len = recvfrom(so->s, &ip, sizeof(struct ip), MSG_PEEK,
1349 (struct sockaddr *)&addr, &addrlen);
1350 if ( len < 0
1351 && ( errno == EAGAIN
1352 || errno == EWOULDBLOCK
1353 || errno == EINPROGRESS
1354 || errno == ENOTCONN))
1355 {
1356 Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm (would block)\n"));
1357 return;
1358 }
1359
1360 if ( len < sizeof(struct ip)
1361 || len < 0
1362 || len == 0)
1363 {
1364 u_char code;
1365 code = ICMP_UNREACH_PORT;
1366
1367 if (errno == EHOSTUNREACH)
1368 code = ICMP_UNREACH_HOST;
1369 else if (errno == ENETUNREACH)
1370 code = ICMP_UNREACH_NET;
1371
1372 LogRel((" udp icmp rx errno = %d-%s\n",
1373 errno, strerror(errno)));
1374 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
1375 so->so_m = NULL;
1376 Log(("sorecvfrom_icmp_unix: 1 - step can't read IP datagramm \n"));
1377 return;
1378 }
1379 /* basic check of IP header */
1380 if ( ip.ip_v != IPVERSION
1381# ifndef RT_OS_DARWIN
1382 || ip.ip_p != IPPROTO_ICMP
1383# endif
1384 )
1385 {
1386 Log(("sorecvfrom_icmp_unix: 1 - step IP isn't IPv4 \n"));
1387 return;
1388 }
1389# ifndef RT_OS_DARWIN
1390 /* Darwin reports the IP length already in host byte order. */
1391 ip.ip_len = RT_N2H_U16(ip.ip_len);
1392# endif
1393# if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
1394 /* Solaris and Darwin report the payload only */
1395 ip.ip_len += (ip.ip_hl << 2);
1396# endif
1397 /* Note: ip->ip_len in host byte order (all OS) */
1398 len = ip.ip_len;
1399 buff = RTMemAlloc(len);
1400 if (buff == NULL)
1401 {
1402 Log(("sorecvfrom_icmp_unix: 1 - step can't allocate enought room for datagram\n"));
1403 return;
1404 }
1405 /* 2 - step: we're reading rest of the datagramm to the buffer */
1406 addrlen = sizeof(struct sockaddr_in);
1407 memset(&addr, 0, addrlen);
1408 len = recvfrom(so->s, buff, len, 0,
1409 (struct sockaddr *)&addr, &addrlen);
1410 if ( len < 0
1411 && ( errno == EAGAIN
1412 || errno == EWOULDBLOCK
1413 || errno == EINPROGRESS
1414 || errno == ENOTCONN))
1415 {
1416 Log(("sorecvfrom_icmp_unix: 2 - step can't read IP body (would block expected:%d)\n",
1417 ip.ip_len));
1418 RTMemFree(buff);
1419 return;
1420 }
1421 if ( len < 0
1422 || len == 0)
1423 {
1424 Log(("sorecvfrom_icmp_unix: 2 - step read of the rest of datagramm is fallen (errno:%d, len:%d expected: %d)\n",
1425 errno, len, (ip.ip_len - sizeof(struct ip))));
1426 RTMemFree(buff);
1427 return;
1428 }
1429 /* len is modified in 2nd read, when the rest of the datagramm was read */
1430 send_icmp_to_guest(pData, buff, len, so, &addr);
1431 RTMemFree(buff);
1432}
1433#endif /* !RT_OS_WINDOWS */
Note: See TracBrowser for help on using the repository browser.

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