VirtualBox

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

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

DrvNat,slirp: simplify statistics and deregister them.

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