VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/ip_icmp.c@ 17389

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

NAT: Multi DNS takes DNS servers IP addresses only from UP interfaces

  • Property svn:eol-style set to native
File size: 22.4 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
34 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
35 */
36
37#include "slirp.h"
38#include "ip_icmp.h"
39#ifdef RT_OS_WINDOWS
40#include <Icmpapi.h>
41#include <Iphlpapi.h>
42#endif
43
44#ifdef RT_OS_WINDOWS
45# ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
46# define ICMP_SEND_ECHO(event, routine, addr, data, datasize, ipopt) \
47 IcmpSendEcho2(pData->icmp_socket.sh, (event), NULL, NULL, (addr), (data), (datasize), \
48 (ipopt), pData->pvIcmpBuffer, pData->szIcmpBuffer, 1)
49# else /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
50# define ICMP_SEND_ECHO(event, routine, addr, data, datasize, ipopt) \
51 IcmpSendEcho2(pData->icmp_socket.sh, NULL, (FARPROC)(routine), (void *)pData, (addr), (data), (datasize), \
52 (ipopt), pData->pvIcmpBuffer, pData->szIcmpBuffer, 1)
53static void WINAPI notify_slirp(void *);
54# endif /* !VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
55#endif /* RT_OS_WINDOWS */
56
57/* The message sent when emulating PING */
58/* Be nice and tell them it's just a psuedo-ping packet */
59static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
60
61/* list of actions for icmp_error() on RX of an icmp message */
62static const int icmp_flush[19] =
63{
64/* ECHO REPLY (0) */ 0,
65 1,
66 1,
67/* DEST UNREACH (3) */ 1,
68/* SOURCE QUENCH (4)*/ 1,
69/* REDIRECT (5) */ 1,
70 1,
71 1,
72/* ECHO (8) */ 0,
73/* ROUTERADVERT (9) */ 1,
74/* ROUTERSOLICIT (10) */ 1,
75/* TIME EXCEEDED (11) */ 1,
76/* PARAMETER PROBLEM (12) */ 1,
77/* TIMESTAMP (13) */ 0,
78/* TIMESTAMP REPLY (14) */ 0,
79/* INFO (15) */ 0,
80/* INFO REPLY (16) */ 0,
81/* ADDR MASK (17) */ 0,
82/* ADDR MASK REPLY (18) */ 0
83};
84
85int
86icmp_init(PNATState pData)
87{
88 pData->icmp_socket.so_type = IPPROTO_ICMP;
89 pData->icmp_socket.so_state = SS_ISFCONNECTED;
90#ifndef RT_OS_WINDOWS
91# ifndef RT_OS_DARWIN
92 pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
93# else /* !RT_OS_DARWIN */
94 pData->icmp_socket.s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
95# endif /* RT_OS_DARWIN */
96 if (pData->icmp_socket.s == -1)
97 {
98 int rc = RTErrConvertFromErrno(errno);
99 LogRel(("NAT: ICMP/ping not available (could open ICMP socket, error %Rrc)\n", rc));
100 return 1;
101 }
102 fd_nonblock(pData->icmp_socket.s);
103 NSOCK_INC();
104#else /* RT_OS_WINDOWS */
105 pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
106 if (pData->hmIcmpLibrary != NULL)
107 {
108 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
109 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
110 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
111 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
112# ifdef VBOX_WITH_MULTI_DNS
113 pData->pfGetAdaptersAddresses = (ULONG (WINAPI *)(HANDLE))
114 GetProcAddress(pData->hmIcmpLibrary, "GetAdaptersAddresses");
115 if (pData->pfGetAdaptersAddresses == NULL)
116 {
117 LogRel(("NAT: Can't find GetAdapterAddresses in Iphlpapi.dll"));
118 }
119# endif
120 }
121
122 if (pData->pfIcmpParseReplies == NULL)
123 {
124# ifdef VBOX_WITH_MULTI_DNS
125 if(pData->pfGetAdaptersAddresses == NULL)
126 FreeLibrary(pData->hmIcmpLibrary);
127# else
128 FreeLibrary(pData->hmIcmpLibrary);
129# endif
130 pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
131 if (pData->hmIcmpLibrary == NULL)
132 {
133 LogRel(("NAT: Icmp.dll could not be loaded\n"));
134 return 1;
135 }
136 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
137 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
138 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
139 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
140 }
141 if (pData->pfIcmpParseReplies == NULL)
142 {
143 LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
144 FreeLibrary(pData->hmIcmpLibrary);
145 return 1;
146 }
147 if (pData->pfIcmpCloseHandle == NULL)
148 {
149 LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
150 FreeLibrary(pData->hmIcmpLibrary);
151 return 1;
152 }
153 pData->icmp_socket.sh = IcmpCreateFile();
154# ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
155 pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
156# endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
157 pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
158 pData->pvIcmpBuffer = RTMemAlloc(pData->szIcmpBuffer);
159#endif /* RT_OS_WINDOWS */
160 LIST_INIT(&pData->icmp_msg_head);
161 return 0;
162}
163
164/*
165 * ip here is ip header + 64bytes readed from ICMP packet
166 */
167struct icmp_msg *
168icmp_find_original_mbuf(PNATState pData, struct ip *ip)
169{
170 struct mbuf *m0;
171 struct ip *ip0;
172 struct icmp *icp, *icp0;
173 struct icmp_msg *icm = NULL;
174 int found = 0;
175 struct udphdr *udp;
176 struct tcphdr *tcp;
177 struct socket *head_socket = NULL;
178 struct socket *last_socket = NULL;
179 struct socket *so = NULL;
180 struct in_addr laddr, faddr;
181 u_short lport, fport;
182
183 laddr.s_addr = ~0;
184 faddr.s_addr = ~0;
185
186 lport = ~0;
187 fport = ~0;
188
189
190 Log(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
191 switch (ip->ip_p)
192 {
193 case IPPROTO_ICMP:
194 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
195 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
196 {
197 m0 = icm->im_m;
198 ip0 = mtod(m0, struct ip *);
199 AssertRelease(ip0->ip_p == IPPROTO_ICMP);
200 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
201 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
202 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
203 && icp->icmp_id == icp0->icmp_id
204 && icp->icmp_seq == icp0->icmp_seq)
205 {
206 found = 1;
207 Log(("Have found %R[natsock]\n", icm->im_so));
208 break;
209 }
210 Log(("Have found nothing\n"));
211 }
212 break;
213
214 /*
215 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
216 * from which the IP package has been sent.
217 */
218 case IPPROTO_UDP:
219 head_socket = &udb;
220 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
221 faddr.s_addr = ip->ip_dst.s_addr;
222 fport = udp->uh_dport;
223 laddr.s_addr = ip->ip_src.s_addr;
224 lport = udp->uh_sport;
225 last_socket = udp_last_so;
226 /* fall through */
227
228 case IPPROTO_TCP:
229 if (head_socket == NULL)
230 {
231 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
232 head_socket = &tcb; /* head_socket could be initialized with udb*/
233 faddr.s_addr = ip->ip_dst.s_addr;
234 fport = tcp->th_dport;
235 laddr.s_addr = ip->ip_src.s_addr;
236 lport = tcp->th_sport;
237 last_socket = tcp_last_so;
238 }
239 /* check last socket first */
240 if ( last_socket->so_faddr.s_addr == faddr.s_addr
241 && last_socket->so_fport == fport
242 && last_socket->so_hlport == lport)
243 {
244 found = 1;
245 so = last_socket;
246 goto sofound;
247 }
248 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
249 {
250 /* Should be reaplaced by hash here */
251 Log(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
252 if ( so->so_faddr.s_addr == faddr.s_addr
253 && so->so_fport == fport
254 && so->so_hlport == lport)
255 {
256 found = 1;
257 break;
258 }
259 }
260 break;
261
262 default:
263 LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
264 }
265 sofound:
266 if (found == 1 && icm == NULL)
267 {
268 if (so->so_state == SS_NOFDREF)
269 {
270 /* socket is shutdowning we've already sent ICMP on it.*/
271 LogRel(("NAT: Received icmp on shutdowning socket (probably corresponding ICMP socket has been already sent)\n"));
272 return NULL;
273 }
274 icm = RTMemAlloc(sizeof(struct icmp_msg));
275 icm->im_m = so->so_m;
276 icm->im_so = so;
277 found = 1;
278 Log(("hit:%R[natsock]\n", so));
279 /*XXX: this storage not very long,
280 * better add flag if it should removed from lis
281 */
282 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
283 return (icm);
284 }
285 if (found == 1)
286 return icm;
287
288 return NULL;
289}
290
291static int
292icmp_attach(PNATState pData, struct mbuf *m)
293{
294 struct icmp_msg *icm;
295 struct ip *ip;
296 ip = mtod(m, struct ip *);
297 Assert(ip->ip_p == IPPROTO_ICMP);
298 icm = RTMemAlloc(sizeof(struct icmp_msg));
299 icm->im_m = m;
300 icm->im_so = m->m_so;
301 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
302 return 0;
303}
304
305/*
306 * Process a received ICMP message.
307 */
308void
309icmp_input(PNATState pData, struct mbuf *m, int hlen)
310{
311 register struct icmp *icp;
312 register struct ip *ip = mtod(m, struct ip *);
313 int icmplen = ip->ip_len;
314 int status;
315 uint32_t dst;
316#if !defined(RT_OS_WINDOWS)
317 int ttl;
318#endif
319
320 /* int code; */
321
322 DEBUG_CALL("icmp_input");
323 DEBUG_ARG("m = %lx", (long )m);
324 DEBUG_ARG("m_len = %d", m->m_len);
325
326 icmpstat.icps_received++;
327
328 /*
329 * Locate icmp structure in mbuf, and check
330 * that its not corrupted and of at least minimum length.
331 */
332 if (icmplen < ICMP_MINLEN)
333 {
334 /* min 8 bytes payload */
335 icmpstat.icps_tooshort++;
336freeit:
337 m_freem(pData, m);
338 goto end_error;
339 }
340
341 m->m_len -= hlen;
342 m->m_data += hlen;
343 icp = mtod(m, struct icmp *);
344 if (cksum(m, icmplen))
345 {
346 icmpstat.icps_checksum++;
347 goto freeit;
348 }
349 m->m_len += hlen;
350 m->m_data -= hlen;
351
352 /* icmpstat.icps_inhist[icp->icmp_type]++; */
353 /* code = icp->icmp_code; */
354
355 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
356 switch (icp->icmp_type)
357 {
358 case ICMP_ECHO:
359 ip->ip_len += hlen; /* since ip_input subtracts this */
360 dst = ip->ip_dst.s_addr;
361 if (dst == alias_addr.s_addr)
362 {
363 icp->icmp_type = ICMP_ECHOREPLY;
364 ip->ip_dst.s_addr = ip->ip_src.s_addr;
365 ip->ip_src.s_addr = dst;
366 icmp_reflect(pData, m);
367 }
368 else
369 {
370 struct sockaddr_in addr;
371#ifdef RT_OS_WINDOWS
372 IP_OPTION_INFORMATION ipopt;
373 int error;
374#endif
375 addr.sin_family = AF_INET;
376 if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
377 {
378 /* It's an alias */
379 switch (ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
380 {
381 case CTL_DNS:
382#ifndef VBOX_WITH_MULTI_DNS
383 addr.sin_addr = dns_addr;
384 break;
385#endif
386 case CTL_ALIAS:
387 default:
388 addr.sin_addr = loopback_addr;
389 break;
390 }
391 }
392 else
393 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
394#ifndef RT_OS_WINDOWS
395 if (pData->icmp_socket.s != -1)
396 {
397 m->m_so = &pData->icmp_socket;
398 icmp_attach(pData, m);
399 ttl = ip->ip_ttl;
400 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
401 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
402 (void *)&ttl, sizeof(ttl));
403 if (status < 0)
404 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
405 strerror(errno)));
406 if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
407 (struct sockaddr *)&addr, sizeof(addr)) == -1)
408 {
409 Log((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
410 errno, strerror(errno)));
411 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
412 }
413 }
414 else
415 {
416 /*
417 * We're freeing the ICMP message, which unable sent or process.
418 * That behavior described in rfc 793, we shouldn't notify sender about
419 * fail of processing it's ICMP packets
420 */
421 m_free(pData, m);
422 return;
423 }
424#else /* RT_OS_WINDOWS */
425 icmp_attach(pData, m);
426 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
427 pData->icmp_socket.so_icmp_id = icp->icmp_id;
428 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
429 m->m_so = &pData->icmp_socket;
430 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
431 ipopt.Ttl = ip->ip_ttl;
432 status = ICMP_SEND_ECHO(pData->phEvents[VBOX_ICMP_EVENT_INDEX], notify_slirp, addr.sin_addr.s_addr,
433 icp->icmp_data, icmplen - ICMP_MINLEN, &ipopt);
434 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
435 {
436 error = GetLastError();
437 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
438 switch (error)
439 {
440 case ERROR_INVALID_PARAMETER:
441 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
442 break;
443 case ERROR_NOT_SUPPORTED:
444 LogRel(("operation is unsupported)\n"));
445 break;
446 case ERROR_NOT_ENOUGH_MEMORY:
447 LogRel(("OOM!!!)\n"));
448 break;
449 case IP_BUF_TOO_SMALL:
450 LogRel(("Buffer too small)\n"));
451 break;
452 default:
453 LogRel(("Other error!!!)\n"));
454 break;
455 }
456 }
457#endif /* RT_OS_WINDOWS */
458 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
459 break;
460 case ICMP_UNREACH:
461 /* XXX? report error? close socket? */
462 case ICMP_TIMXCEED:
463 case ICMP_PARAMPROB:
464 case ICMP_SOURCEQUENCH:
465 case ICMP_TSTAMP:
466 case ICMP_MASKREQ:
467 case ICMP_REDIRECT:
468 icmpstat.icps_notsupp++;
469 m_freem(pData, m);
470 break;
471
472 default:
473 icmpstat.icps_badtype++;
474 m_freem(pData, m);
475 } /* switch */
476
477end_error:
478 /* m is m_free()'d xor put in a socket xor or given to ip_send */
479 ;
480}
481
482
483/*
484 * Send an ICMP message in response to a situation
485 *
486 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
487 * MUST NOT change this header information.
488 * MUST NOT reply to a multicast/broadcast IP address.
489 * MUST NOT reply to a multicast/broadcast MAC address.
490 * MUST reply to only the first fragment.
491 */
492/*
493 * Send ICMP_UNREACH back to the source regarding msrc.
494 * mbuf *msrc is used as a template, but is NOT m_free()'d.
495 * It is reported as the bad ip packet. The header should
496 * be fully correct and in host byte order.
497 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
498 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
499 */
500
501#define ICMP_MAXDATALEN (IP_MSS-28)
502void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
503{
504 unsigned hlen, shlen, s_ip_len;
505 register struct ip *ip;
506 register struct icmp *icp;
507 register struct mbuf *m;
508
509 DEBUG_CALL("icmp_error");
510 DEBUG_ARG("msrc = %lx", (long )msrc);
511 DEBUG_ARG("msrc_len = %d", msrc->m_len);
512
513 if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
514 goto end_error;
515
516 /* check msrc */
517 if (!msrc)
518 goto end_error;
519
520 ip = mtod(msrc, struct ip *);
521#if DEBUG
522 {
523 char bufa[20], bufb[20];
524 strcpy(bufa, inet_ntoa(ip->ip_src));
525 strcpy(bufb, inet_ntoa(ip->ip_dst));
526 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
527 }
528#endif
529 if (ip->ip_off & IP_OFFMASK)
530 goto end_error; /* Only reply to fragment 0 */
531
532 shlen = ip->ip_hl << 2;
533 s_ip_len = ip->ip_len;
534 if (ip->ip_p == IPPROTO_ICMP)
535 {
536 icp = (struct icmp *)((char *)ip + shlen);
537 /*
538 * Assume any unknown ICMP type is an error. This isn't
539 * specified by the RFC, but think about it..
540 */
541 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
542 goto end_error;
543 }
544
545 /* make a copy */
546 if (!(m = m_get(pData)))
547 goto end_error; /* get mbuf */
548 {
549 int new_m_size;
550 m->m_data += if_maxlinkhdr;
551 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
552 if (new_m_size>m->m_size)
553 m_inc(m, new_m_size);
554 }
555 memcpy(m->m_data, msrc->m_data, msrc->m_len);
556 m->m_len = msrc->m_len; /* copy msrc to m */
557
558 /* make the header of the reply packet */
559 ip = mtod(m, struct ip *);
560 hlen = sizeof(struct ip ); /* no options in reply */
561
562 /* fill in icmp */
563 m->m_data += hlen;
564 m->m_len -= hlen;
565
566 icp = mtod(m, struct icmp *);
567
568 if (minsize)
569 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
570 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
571 s_ip_len = ICMP_MAXDATALEN;
572
573 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
574
575 /* min. size = 8+sizeof(struct ip)+8 */
576
577 icp->icmp_type = type;
578 icp->icmp_code = code;
579 icp->icmp_id = 0;
580 icp->icmp_seq = 0;
581
582 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
583 HTONS(icp->icmp_ip.ip_len);
584 HTONS(icp->icmp_ip.ip_id);
585 HTONS(icp->icmp_ip.ip_off);
586
587#if DEBUG
588 if (message)
589 {
590 /* DEBUG : append message to ICMP packet */
591 int message_len;
592 char *cpnt;
593 message_len = strlen(message);
594 if (message_len > ICMP_MAXDATALEN)
595 message_len = ICMP_MAXDATALEN;
596 cpnt = (char *)m->m_data+m->m_len;
597 memcpy(cpnt, message, message_len);
598 m->m_len += message_len;
599 }
600#endif
601
602 icp->icmp_cksum = 0;
603 icp->icmp_cksum = cksum(m, m->m_len);
604
605 m->m_data -= hlen;
606 m->m_len += hlen;
607
608 /* fill in ip */
609 ip->ip_hl = hlen >> 2;
610 ip->ip_len = m->m_len;
611
612 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
613
614 ip->ip_ttl = MAXTTL;
615 ip->ip_p = IPPROTO_ICMP;
616 ip->ip_dst = ip->ip_src; /* ip adresses */
617 ip->ip_src = alias_addr;
618
619 (void ) ip_output(pData, (struct socket *)NULL, m);
620
621 icmpstat.icps_reflect++;
622
623 return;
624
625end_error:
626 LogRel(("NAT: error occured while sending ICMP error message \n"));
627}
628#undef ICMP_MAXDATALEN
629
630/*
631 * Reflect the ip packet back to the source
632 */
633void
634icmp_reflect(PNATState pData, struct mbuf *m)
635{
636 register struct ip *ip = mtod(m, struct ip *);
637 int hlen = ip->ip_hl << 2;
638 int optlen = hlen - sizeof(struct ip );
639 register struct icmp *icp;
640
641 /*
642 * Send an icmp packet back to the ip level,
643 * after supplying a checksum.
644 */
645 m->m_data += hlen;
646 m->m_len -= hlen;
647 icp = mtod(m, struct icmp *);
648
649 icp->icmp_cksum = 0;
650 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
651
652 m->m_data -= hlen;
653 m->m_len += hlen;
654
655 (void ) ip_output(pData, (struct socket *)NULL, m);
656
657 icmpstat.icps_reflect++;
658}
659#if defined(RT_OS_WINDOWS) && !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
660static void WINAPI
661notify_slirp(void *ctx)
662{
663 /* pData name is important see slirp_state.h */
664 PNATState pData = (PNATState)ctx;
665 fIcmp = 1;
666}
667#endif
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