VirtualBox

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

Last change on this file since 15590 was 15590, checked in by vboxsync, 16 years ago

slirp:icmp: logging too noisy

  • Property svn:eol-style set to native
File size: 28.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#if defined(VBOX_WITH_SLIRP_ICMP) && defined (RT_OS_WINDOWS)
16#include <iphlpapi.h>
17#include <icmpapi.h>
18#endif
19
20
21#ifdef VBOX_WITH_SLIRP_ICMP
22static void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
23static void sorecvfrom_icmp_win(PNATState, struct socket *);
24#endif
25static void sorecvfrom_icmp_unix(PNATState, struct socket *);
26
27void
28so_init()
29{
30}
31
32
33struct socket *
34solookup(struct socket *head, struct in_addr laddr,
35 u_int lport, struct in_addr faddr, u_int fport)
36{
37 struct socket *so;
38
39 for (so = head->so_next; so != head; so = so->so_next)
40 {
41 if ( so->so_lport == lport
42 && so->so_laddr.s_addr == laddr.s_addr
43 && so->so_faddr.s_addr == faddr.s_addr
44 && so->so_fport == fport)
45 return so;
46 }
47
48 return (struct socket *)NULL;
49}
50
51/*
52 * Create a new socket, initialise the fields
53 * It is the responsibility of the caller to
54 * insque() it into the correct linked-list
55 */
56struct socket *
57socreate()
58{
59 struct socket *so;
60
61 so = (struct socket *)malloc(sizeof(struct socket));
62 if(so)
63 {
64 memset(so, 0, sizeof(struct socket));
65 so->so_state = SS_NOFDREF;
66 so->s = -1;
67 }
68 return so;
69}
70
71/*
72 * remque and free a socket, clobber cache
73 */
74void
75sofree(PNATState pData, struct socket *so)
76{
77 if (so == tcp_last_so)
78 tcp_last_so = &tcb;
79 else if (so == udp_last_so)
80 udp_last_so = &udb;
81
82 /* check if mbuf haven't been already freed */
83 if (so->so_m != NULL)
84 m_free(pData, so->so_m);
85
86 if(so->so_next && so->so_prev)
87 remque(pData, so); /* crashes if so is not in a queue */
88
89 free(so);
90}
91
92/*
93 * Read from so's socket into sb_snd, updating all relevant sbuf fields
94 * NOTE: This will only be called if it is select()ed for reading, so
95 * a read() of 0 (or less) means it's disconnected
96 */
97int
98soread(PNATState pData, struct socket *so, int fCloseIfNothingRead)
99{
100 int n, nn, lss, total;
101 struct sbuf *sb = &so->so_snd;
102 size_t len = sb->sb_datalen - sb->sb_cc;
103 struct iovec iov[2];
104 int mss = so->so_tcpcb->t_maxseg;
105
106 DEBUG_CALL("soread");
107 DEBUG_ARG("so = %lx", (long )so);
108
109 /*
110 * No need to check if there's enough room to read.
111 * soread wouldn't have been called if there weren't
112 */
113
114 len = sb->sb_datalen - sb->sb_cc;
115
116 iov[0].iov_base = sb->sb_wptr;
117 iov[1].iov_base = 0;
118 iov[1].iov_len = 0;
119 if (sb->sb_wptr < sb->sb_rptr)
120 {
121 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
122 /* Should never succeed, but... */
123 if (iov[0].iov_len > len)
124 iov[0].iov_len = len;
125 if (iov[0].iov_len > mss)
126 iov[0].iov_len -= iov[0].iov_len%mss;
127 n = 1;
128 }
129 else
130 {
131 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
132 /* Should never succeed, but... */
133 if (iov[0].iov_len > len)
134 iov[0].iov_len = len;
135 len -= iov[0].iov_len;
136 if (len)
137 {
138 iov[1].iov_base = sb->sb_data;
139 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
140 if(iov[1].iov_len > len)
141 iov[1].iov_len = len;
142 total = iov[0].iov_len + iov[1].iov_len;
143 if (total > mss)
144 {
145 lss = total % mss;
146 if (iov[1].iov_len > lss)
147 {
148 iov[1].iov_len -= lss;
149 n = 2;
150 }
151 else
152 {
153 lss -= iov[1].iov_len;
154 iov[0].iov_len -= lss;
155 n = 1;
156 }
157 }
158 else
159 n = 2;
160 }
161 else
162 {
163 if (iov[0].iov_len > mss)
164 iov[0].iov_len -= iov[0].iov_len%mss;
165 n = 1;
166 }
167 }
168
169#ifdef HAVE_READV
170 nn = readv(so->s, (struct iovec *)iov, n);
171 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
172#else
173 nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
174#endif
175 if (nn <= 0)
176 {
177#if defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
178 /*
179 * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
180 * _could_ mean that the connection is closed. But we will receive an
181 * FD_CLOSE event later if the connection was _really_ closed. With
182 * www.youtube.com I see this very often. Closing the socket too early
183 * would be dangerous.
184 */
185 if (nn == 0 && !fCloseIfNothingRead)
186 return 0;
187#endif
188 if (nn < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
189 return 0;
190 else
191 {
192 /* nn == 0 means peer has performed an orderly shutdown */
193 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n",
194 nn, errno,strerror(errno)));
195 sofcantrcvmore(so);
196 tcp_sockclosed(pData, sototcpcb(so));
197 return -1;
198 }
199 }
200
201#ifndef HAVE_READV
202 /*
203 * If there was no error, try and read the second time round
204 * We read again if n = 2 (ie, there's another part of the buffer)
205 * and we read as much as we could in the first read
206 * We don't test for <= 0 this time, because there legitimately
207 * might not be any more data (since the socket is non-blocking),
208 * a close will be detected on next iteration.
209 * A return of -1 wont (shouldn't) happen, since it didn't happen above
210 */
211 if (n == 2 && nn == iov[0].iov_len)
212 {
213 int ret;
214 ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
215 if (ret > 0)
216 nn += ret;
217 }
218
219 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
220#endif
221
222 /* Update fields */
223 sb->sb_cc += nn;
224 sb->sb_wptr += nn;
225 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
226 sb->sb_wptr -= sb->sb_datalen;
227 return nn;
228}
229
230/*
231 * Get urgent data
232 *
233 * When the socket is created, we set it SO_OOBINLINE,
234 * so when OOB data arrives, we soread() it and everything
235 * in the send buffer is sent as urgent data
236 */
237void
238sorecvoob(PNATState pData, struct socket *so)
239{
240 struct tcpcb *tp = sototcpcb(so);
241
242 DEBUG_CALL("sorecvoob");
243 DEBUG_ARG("so = %lx", (long)so);
244
245 /*
246 * We take a guess at how much urgent data has arrived.
247 * In most situations, when urgent data arrives, the next
248 * read() should get all the urgent data. This guess will
249 * be wrong however if more data arrives just after the
250 * urgent data, or the read() doesn't return all the
251 * urgent data.
252 */
253 soread(pData, so, /*fCloseIfNothingRead=*/false);
254 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
255 tp->t_force = 1;
256 tcp_output(pData, tp);
257 tp->t_force = 0;
258}
259
260/*
261 * Send urgent data
262 * There's a lot duplicated code here, but...
263 */
264int
265sosendoob(struct socket *so)
266{
267 struct sbuf *sb = &so->so_rcv;
268 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
269
270 int n, len;
271
272 DEBUG_CALL("sosendoob");
273 DEBUG_ARG("so = %lx", (long)so);
274 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
275
276 if (so->so_urgc > sizeof(buff))
277 so->so_urgc = sizeof(buff); /* XXX */
278
279 if (sb->sb_rptr < sb->sb_wptr)
280 {
281 /* We can send it directly */
282 n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
283 so->so_urgc -= n;
284
285 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n",
286 n, so->so_urgc));
287 }
288 else
289 {
290 /*
291 * Since there's no sendv or sendtov like writev,
292 * we must copy all data to a linear buffer then
293 * send it all
294 */
295 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
296 if (len > so->so_urgc)
297 len = so->so_urgc;
298 memcpy(buff, sb->sb_rptr, len);
299 so->so_urgc -= len;
300 if (so->so_urgc)
301 {
302 n = sb->sb_wptr - sb->sb_data;
303 if (n > so->so_urgc)
304 n = so->so_urgc;
305 memcpy(buff + len, sb->sb_data, n);
306 so->so_urgc -= n;
307 len += n;
308 }
309 n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
310#ifdef DEBUG
311 if (n != len)
312 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
313#endif
314 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
315 n, so->so_urgc));
316 }
317
318 sb->sb_cc -= n;
319 sb->sb_rptr += n;
320 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
321 sb->sb_rptr -= sb->sb_datalen;
322
323 return n;
324}
325
326/*
327 * Write data from so_rcv to so's socket,
328 * updating all sbuf field as necessary
329 */
330int
331sowrite(PNATState pData, struct socket *so)
332{
333 int n,nn;
334 struct sbuf *sb = &so->so_rcv;
335 size_t len = sb->sb_cc;
336 struct iovec iov[2];
337
338 DEBUG_CALL("sowrite");
339 DEBUG_ARG("so = %lx", (long)so);
340
341 if (so->so_urgc)
342 {
343 sosendoob(so);
344 if (sb->sb_cc == 0)
345 return 0;
346 }
347
348 /*
349 * No need to check if there's something to write,
350 * sowrite wouldn't have been called otherwise
351 */
352
353 len = sb->sb_cc;
354
355 iov[0].iov_base = sb->sb_rptr;
356 iov[1].iov_base = 0;
357 iov[1].iov_len = 0;
358 if (sb->sb_rptr < sb->sb_wptr)
359 {
360 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
361 /* Should never succeed, but... */
362 if (iov[0].iov_len > len)
363 iov[0].iov_len = len;
364 n = 1;
365 }
366 else
367 {
368 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
369 if (iov[0].iov_len > len)
370 iov[0].iov_len = len;
371 len -= iov[0].iov_len;
372 if (len)
373 {
374 iov[1].iov_base = sb->sb_data;
375 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
376 if (iov[1].iov_len > len)
377 iov[1].iov_len = len;
378 n = 2;
379 }
380 else
381 n = 1;
382 }
383 /* Check if there's urgent data to send, and if so, send it */
384#ifdef HAVE_READV
385 nn = writev(so->s, (const struct iovec *)iov, n);
386 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
387#else
388 nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
389#endif
390 /* This should never happen, but people tell me it does *shrug* */
391 if (nn < 0 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
392 return 0;
393
394 if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
395 {
396 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
397 so->so_state, errno));
398 sofcantsendmore(so);
399 tcp_sockclosed(pData, sototcpcb(so));
400 return -1;
401 }
402
403#ifndef HAVE_READV
404 if (n == 2 && nn == iov[0].iov_len)
405 {
406 int ret;
407 ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
408 if (ret > 0)
409 nn += ret;
410 }
411 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
412#endif
413
414 /* Update sbuf */
415 sb->sb_cc -= nn;
416 sb->sb_rptr += nn;
417 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
418 sb->sb_rptr -= sb->sb_datalen;
419
420 /*
421 * If in DRAIN mode, and there's no more data, set
422 * it CANTSENDMORE
423 */
424 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
425 sofcantsendmore(so);
426
427 return nn;
428}
429
430/*
431 * recvfrom() a UDP socket
432 */
433void
434sorecvfrom(PNATState pData, struct socket *so)
435{
436 struct sockaddr_in addr;
437 socklen_t addrlen = sizeof(struct sockaddr_in);
438
439 DEBUG_CALL("sorecvfrom");
440 DEBUG_ARG("so = %lx", (long)so);
441
442 if (so->so_type == IPPROTO_ICMP)
443 {
444 /* This is a "ping" reply */
445#if !defined(VBOX_WITH_SLIRP_ICMP) || (defined(VBOX_WITH_SLIRP_ICMP) && !defined(RT_OS_WINDOWS))
446 sorecvfrom_icmp_unix(pData, so);
447#endif
448#if defined(VBOX_WITH_SLIRP_ICMP) && defined(RT_OS_WINDOWS)
449 sorecvfrom_icmp_win(pData, so);
450#endif
451 udp_detach(pData, so);
452 }
453 else
454 {
455 /* A "normal" UDP packet */
456 struct mbuf *m;
457 size_t len;
458 u_long n;
459
460 if (!(m = m_get(pData)))
461 return;
462 m->m_data += if_maxlinkhdr;
463
464 /*
465 * XXX Shouldn't FIONREAD packets destined for port 53,
466 * but I don't know the max packet size for DNS lookups
467 */
468 len = M_FREEROOM(m);
469 /* if (so->so_fport != htons(53)) */
470 {
471 ioctlsocket(so->s, FIONREAD, &n);
472
473 if (n > len)
474 {
475 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
476 m_inc(m, n);
477 len = M_FREEROOM(m);
478 }
479 }
480
481 m->m_len = recvfrom(so->s, m->m_data, len, 0,
482 (struct sockaddr *)&addr, &addrlen);
483 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
484 m->m_len, errno,strerror(errno)));
485 if(m->m_len < 0)
486 {
487 u_char code = ICMP_UNREACH_PORT;
488
489 if (errno == EHOSTUNREACH)
490 code = ICMP_UNREACH_HOST;
491 else if(errno == ENETUNREACH)
492 code = ICMP_UNREACH_NET;
493
494 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
495 icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
496 m_free(pData, m);
497 }
498 else
499 {
500 /*
501 * Hack: domain name lookup will be used the most for UDP,
502 * and since they'll only be used once there's no need
503 * for the 4 minute (or whatever) timeout... So we time them
504 * out much quicker (10 seconds for now...)
505 */
506 if (so->so_expire)
507 {
508 if (so->so_fport == htons(53))
509 so->so_expire = curtime + SO_EXPIREFAST;
510 else
511 so->so_expire = curtime + SO_EXPIRE;
512 }
513
514#if 0
515 if (m->m_len == len)
516 {
517 m_inc(m, MINCSIZE);
518 m->m_len = 0;
519 }
520#endif
521
522 /*
523 * If this packet was destined for CTL_ADDR,
524 * make it look like that's where it came from, done by udp_output
525 */
526 udp_output(pData, so, m, &addr);
527 } /* rx error */
528 } /* if ping packet */
529}
530
531/*
532 * sendto() a socket
533 */
534int
535sosendto(PNATState pData, struct socket *so, struct mbuf *m)
536{
537 int ret;
538 struct sockaddr_in addr;
539#if 0
540 struct sockaddr_in host_addr;
541#endif
542
543 DEBUG_CALL("sosendto");
544 DEBUG_ARG("so = %lx", (long)so);
545 DEBUG_ARG("m = %lx", (long)m);
546
547 addr.sin_family = AF_INET;
548 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
549 {
550 /* It's an alias */
551 uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
552 switch(last_byte)
553 {
554#if 0
555 /* handle this case at 'default:' */
556 case CTL_BROADCAST:
557 addr.sin_addr.s_addr = INADDR_BROADCAST;
558 /* Send the packet to host to fully emulate broadcast */
559 /** @todo r=klaus: on Linux host this causes the host to receive
560 * the packet twice for some reason. And I cannot find any place
561 * in the man pages which states that sending a broadcast does not
562 * reach the host itself. */
563 host_addr.sin_family = AF_INET;
564 host_addr.sin_port = so->so_fport;
565 host_addr.sin_addr = our_addr;
566 sendto(so->s, m->m_data, m->m_len, 0,
567 (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
568 break;
569#endif
570 case CTL_DNS:
571 if (!get_dns_addr(pData, &dns_addr))
572 addr.sin_addr = dns_addr;
573 else
574 addr.sin_addr = loopback_addr;
575 break;
576 case CTL_ALIAS:
577 default:
578 if (last_byte == ~pData->netmask)
579 addr.sin_addr.s_addr = INADDR_BROADCAST;
580 else
581 addr.sin_addr = loopback_addr;
582 break;
583 }
584 }
585 else
586 addr.sin_addr = so->so_faddr;
587 addr.sin_port = so->so_fport;
588
589 DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
590 ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
591
592 /* Don't care what port we get */
593 ret = sendto(so->s, m->m_data, m->m_len, 0,
594 (struct sockaddr *)&addr, sizeof (struct sockaddr));
595 if (ret < 0)
596 {
597 LogRel(("UDP: sendto fails (%s)\n", strerror(errno)));
598 return -1;
599 }
600
601 /*
602 * Kill the socket if there's no reply in 4 minutes,
603 * but only if it's an expirable socket
604 */
605 if (so->so_expire)
606 so->so_expire = curtime + SO_EXPIRE;
607 so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
608 return 0;
609}
610
611/*
612 * XXX This should really be tcp_listen
613 */
614struct socket *
615solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
616{
617 struct sockaddr_in addr;
618 struct socket *so;
619 socklen_t addrlen = sizeof(addr);
620 int s, opt = 1;
621
622 DEBUG_CALL("solisten");
623 DEBUG_ARG("port = %d", port);
624 DEBUG_ARG("laddr = %x", laddr);
625 DEBUG_ARG("lport = %d", lport);
626 DEBUG_ARG("flags = %x", flags);
627
628 if ((so = socreate()) == NULL)
629 {
630 /* free(so); Not sofree() ??? free(NULL) == NOP */
631 return NULL;
632 }
633
634 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
635 if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
636 {
637 free(so);
638 return NULL;
639 }
640 insque(pData, so,&tcb);
641
642 /*
643 * SS_FACCEPTONCE sockets must time out.
644 */
645 if (flags & SS_FACCEPTONCE)
646 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
647
648 so->so_state = (SS_FACCEPTCONN|flags);
649 so->so_lport = lport; /* Kept in network format */
650 so->so_laddr.s_addr = laddr; /* Ditto */
651
652 addr.sin_family = AF_INET;
653 addr.sin_addr.s_addr = INADDR_ANY;
654 addr.sin_port = port;
655
656 if ( ((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
657 || (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0)
658 || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
659 || (listen(s,1) < 0))
660 {
661#ifdef RT_OS_WINDOWS
662 int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
663 closesocket(s);
664 sofree(pData, so);
665 /* Restore the real errno */
666 WSASetLastError(tmperrno);
667#else
668 int tmperrno = errno; /* Don't clobber the real reason we failed */
669 close(s);
670 sofree(pData, so);
671 /* Restore the real errno */
672 errno = tmperrno;
673#endif
674 return NULL;
675 }
676 setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
677
678 getsockname(s,(struct sockaddr *)&addr,&addrlen);
679 so->so_fport = addr.sin_port;
680 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
681 so->so_faddr = alias_addr;
682 else
683 so->so_faddr = addr.sin_addr;
684
685 so->s = s;
686 return so;
687}
688
689/*
690 * Data is available in so_rcv
691 * Just write() the data to the socket
692 * XXX not yet...
693 */
694void
695sorwakeup(struct socket *so)
696{
697#if 0
698 sowrite(so);
699 FD_CLR(so->s,&writefds);
700#endif
701}
702
703/*
704 * Data has been freed in so_snd
705 * We have room for a read() if we want to
706 * For now, don't read, it'll be done in the main loop
707 */
708void
709sowwakeup(struct socket *so)
710{
711}
712
713/*
714 * Various session state calls
715 * XXX Should be #define's
716 * The socket state stuff needs work, these often get call 2 or 3
717 * times each when only 1 was needed
718 */
719void
720soisfconnecting(struct socket *so)
721{
722 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
723 SS_FCANTSENDMORE|SS_FWDRAIN);
724 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
725}
726
727void
728soisfconnected(struct socket *so)
729{
730 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
731 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
732}
733
734void
735sofcantrcvmore(struct socket *so)
736{
737 if ((so->so_state & SS_NOFDREF) == 0)
738 {
739 shutdown(so->s,0);
740 }
741 so->so_state &= ~(SS_ISFCONNECTING);
742 if (so->so_state & SS_FCANTSENDMORE)
743 so->so_state = SS_NOFDREF; /* Don't select it */
744 /* XXX close() here as well? */
745 else
746 so->so_state |= SS_FCANTRCVMORE;
747}
748
749void
750sofcantsendmore(struct socket *so)
751{
752 if ((so->so_state & SS_NOFDREF) == 0)
753 shutdown(so->s, 1); /* send FIN to fhost */
754
755 so->so_state &= ~(SS_ISFCONNECTING);
756 if (so->so_state & SS_FCANTRCVMORE)
757 so->so_state = SS_NOFDREF; /* as above */
758 else
759 so->so_state |= SS_FCANTSENDMORE;
760}
761
762void
763soisfdisconnected(struct socket *so)
764{
765#if 0
766 so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
767 close(so->s);
768 so->so_state = SS_ISFDISCONNECTED;
769 /*
770 * XXX Do nothing ... ?
771 */
772#endif
773}
774
775/*
776 * Set write drain mode
777 * Set CANTSENDMORE once all data has been write()n
778 */
779void
780sofwdrain(struct socket *so)
781{
782 if (so->so_rcv.sb_cc)
783 so->so_state |= SS_FWDRAIN;
784 else
785 sofcantsendmore(so);
786}
787
788#ifdef VBOX_WITH_SLIRP_ICMP
789static void
790send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
791{
792 struct ip *ip;
793 uint32_t dst,src;
794 char ip_copy[256];
795 struct icmp *icp;
796 int old_ip_len;
797 int hlen, original_hlen = 0;
798 struct mbuf *m;
799 struct icmp_msg *icm;
800 uint8_t proto;
801
802 ip = (struct ip *)buff;
803 hlen = (ip->ip_hl << 2);
804 icp = (struct icmp *)((char *)ip + hlen);
805
806 Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
807 if ( icp->icmp_type != ICMP_ECHOREPLY
808 && icp->icmp_type != ICMP_TIMXCEED
809 && icp->icmp_type != ICMP_UNREACH)
810 {
811 return;
812 }
813
814 if ( icp->icmp_type == ICMP_TIMXCEED
815 || icp->icmp_type == ICMP_UNREACH)
816 {
817 ip = &icp->icmp_ip;
818 DO_ALIAS(&ip->ip_dst);
819 }
820 else
821 {
822 DO_ALIAS(&ip->ip_src);
823 }
824
825 icm = icmp_find_original_mbuf(pData, ip);
826
827 if (icm == NULL)
828 {
829 Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
830 return;
831 }
832
833 m = icm->im_m;
834 Assert(m != NULL);
835
836 src = addr->sin_addr.s_addr;
837
838 ip = mtod(m, struct ip *);
839 proto = ip->ip_p;
840 /* Now ip is pointing on header we've sent from guest */
841 if ( icp->icmp_type == ICMP_TIMXCEED
842 || icp->icmp_type == ICMP_UNREACH)
843 {
844 old_ip_len = (ip->ip_hl << 2) + 64;
845 if (old_ip_len > sizeof(ip_copy))
846 old_ip_len = sizeof(ip_copy);
847 memcpy(ip_copy, ip, old_ip_len);
848 }
849
850 /* source address from original IP packet*/
851 dst = ip->ip_src.s_addr;
852
853 /* overide ther tail of old packet */
854 ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
855 original_hlen = ip->ip_hl << 2;
856 /* saves original ip header and options */
857 memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
858 m->m_len = len - hlen + original_hlen;
859 ip->ip_len = m->m_len;
860 ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
861
862 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
863 if ( icp->icmp_type == ICMP_TIMXCEED
864 || icp->icmp_type == ICMP_UNREACH)
865 {
866 /* according RFC 793 error messages required copy of initial IP header + 64 bit */
867 memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
868 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
869 }
870
871 ip->ip_src.s_addr = src;
872 ip->ip_dst.s_addr = dst;
873 icmp_reflect(pData, m);
874 LIST_REMOVE(icm, im_list);
875 /* Don't call m_free here*/
876
877 if ( icp->icmp_type == ICMP_TIMXCEED
878 || icp->icmp_type == ICMP_UNREACH)
879 {
880 icm->im_so->so_m = NULL;
881 switch (proto)
882 {
883 case IPPROTO_UDP:
884 /*XXX: so->so_m already freed so we shouldn't call sofree */
885 udp_detach(pData, icm->im_so);
886 break;
887 case IPPROTO_TCP:
888 /*close tcp should be here */
889 break;
890 default:
891 /* do nothing */
892 break;
893 }
894 }
895 free(icm);
896}
897
898# ifdef RT_OS_WINDOWS
899static void
900sorecvfrom_icmp_win(PNATState pData, struct socket *so)
901{
902 int len;
903 int i;
904 struct ip *ip;
905 struct mbuf *m;
906 struct icmp *icp;
907 struct icmp_msg *icm;
908 struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
909 uint32_t src;
910 ICMP_ECHO_REPLY *icr;
911 u_char code = ~0;
912 len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
913 if (len < 0)
914 {
915 LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
916 return;
917 }
918 if (len == 0)
919 return; /* no error */
920 icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
921 for (i = 0; i < len; ++i)
922 {
923 switch(icr[i].Status)
924 {
925 case IP_DEST_HOST_UNREACHABLE:
926 code = (code != ~0 ? code : ICMP_UNREACH_HOST);
927 case IP_DEST_NET_UNREACHABLE:
928 code = (code != ~0 ? code : ICMP_UNREACH_NET);
929 case IP_DEST_PROT_UNREACHABLE:
930 code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
931 /* UNREACH error inject here */
932 case IP_DEST_PORT_UNREACHABLE:
933 code = (code != ~0 ? code : ICMP_UNREACH_PORT);
934 icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
935 break;
936 case IP_SUCCESS: /* echo replied */
937 m = m_get(pData);
938 ip = mtod(m, struct ip *);
939 ip->ip_src.s_addr = icr[i].Address;
940 DO_ALIAS(&ip->ip_src);
941 ip->ip_p = IPPROTO_ICMP;
942 ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
943 ip->ip_hl = sizeof(struct ip) >> 2; /* requiered for icmp_reflect, no IP options */
944 ip->ip_ttl = icr[i].Options.Ttl;
945
946 icp = (struct icmp *)&ip[1]; /* no options */
947 icp->icmp_type = ICMP_ECHOREPLY;
948 icp->icmp_code = 0;
949 icp->icmp_id = so->so_icmp_id;
950 icp->icmp_seq = so->so_icmp_seq;
951 memcpy(icp->icmp_data, icr[i].Data, icr[i].DataSize);
952
953 ip->ip_len = sizeof(struct ip) + ICMP_MINLEN + icr[i].DataSize;
954 m->m_len = ip->ip_len;
955
956 icmp_reflect(pData, m);
957 break;
958 case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
959
960 ip_broken = icr[i].Data;
961 icm = icmp_find_original_mbuf(pData, ip_broken);
962 if (icm == NULL) {
963 Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
964 return;
965 }
966 m = icm->im_m;
967 ip = mtod(m, struct ip *);
968 ip->ip_ttl = icr[i].Options.Ttl;
969 src = ip->ip_src.s_addr;
970 ip->ip_dst.s_addr = src;
971 ip->ip_dst.s_addr = icr[i].Address;
972 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
973 ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
974 memcpy(icp->icmp_data, ip_broken, (ip_broken->ip_hl << 2) + 64);
975 icmp_reflect(pData, m);
976 break;
977 default:
978 Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
979 break;
980 }
981 }
982}
983# endif /* RT_OS_WINDOWS */
984#endif /* VBOX_WITH_SLIRP_ICMP */
985
986static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
987{
988 struct sockaddr_in addr;
989 socklen_t addrlen = sizeof(struct sockaddr_in);
990 char buff[1500];
991 int len;
992 len = recvfrom(so->s, buff, 1500, 0,
993 (struct sockaddr *)&addr, &addrlen);
994 /* XXX Check if reply is "correct"? */
995
996 if (len == -1 || len == 0)
997 {
998 u_char code = ICMP_UNREACH_PORT;
999
1000 if (errno == EHOSTUNREACH)
1001 code = ICMP_UNREACH_HOST;
1002 else if(errno == ENETUNREACH)
1003 code = ICMP_UNREACH_NET;
1004
1005 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
1006 errno,strerror(errno)));
1007 icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
1008 }
1009 else
1010 {
1011#ifdef VBOX_WITH_SLIRP_ICMP
1012 send_icmp_to_guest(pData, buff, len, so, &addr);
1013#else
1014 icmp_reflect(pData, so->so_m);
1015 so->so_m = 0; /* Don't m_free() it again! */
1016#endif
1017 }
1018}
1019
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