VirtualBox

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

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

NAT: ICMP logging

  • Property svn:eol-style set to native
File size: 21.9 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 }
113 if (pData->pfIcmpParseReplies == NULL)
114 {
115 FreeLibrary(pData->hmIcmpLibrary);
116 pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
117 if (pData->hmIcmpLibrary == NULL)
118 {
119 LogRel(("NAT: Icmp.dll could not be loaded\n"));
120 return 1;
121 }
122 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
123 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
124 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
125 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
126 }
127 if (pData->pfIcmpParseReplies == NULL)
128 {
129 LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
130 FreeLibrary(pData->hmIcmpLibrary);
131 return 1;
132 }
133 if (pData->pfIcmpCloseHandle == NULL)
134 {
135 LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
136 FreeLibrary(pData->hmIcmpLibrary);
137 return 1;
138 }
139 pData->icmp_socket.sh = IcmpCreateFile();
140# ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
141 pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
142# endif /* VBOX_WITH_SIMPLIFIED_SLIRP_SYNC */
143 pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
144 pData->pvIcmpBuffer = RTMemAlloc(pData->szIcmpBuffer);
145#endif /* RT_OS_WINDOWS */
146 LIST_INIT(&pData->icmp_msg_head);
147 return 0;
148}
149
150/*
151 * ip here is ip header + 64bytes readed from ICMP packet
152 */
153struct icmp_msg *
154icmp_find_original_mbuf(PNATState pData, struct ip *ip)
155{
156 struct mbuf *m0;
157 struct ip *ip0;
158 struct icmp *icp, *icp0;
159 struct icmp_msg *icm = NULL;
160 int found = 0;
161 struct udphdr *udp;
162 struct tcphdr *tcp;
163 struct socket *head_socket = NULL;
164 struct socket *last_socket = NULL;
165 struct socket *so = NULL;
166 struct in_addr laddr, faddr;
167 u_short lport, fport;
168
169 laddr.s_addr = ~0;
170 faddr.s_addr = ~0;
171
172 lport = ~0;
173 fport = ~0;
174
175
176 Log(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
177 switch (ip->ip_p)
178 {
179 case IPPROTO_ICMP:
180 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
181 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
182 {
183 m0 = icm->im_m;
184 ip0 = mtod(m0, struct ip *);
185 AssertRelease(ip0->ip_p == IPPROTO_ICMP);
186 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
187 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
188 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
189 && icp->icmp_id == icp0->icmp_id
190 && icp->icmp_seq == icp0->icmp_seq)
191 {
192 found = 1;
193 Log(("Have found %R[natsock]\n", icm->im_so));
194 break;
195 }
196 Log(("Have found nothing\n"));
197 }
198 break;
199
200 /*
201 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
202 * from which the IP package has been sent.
203 */
204 case IPPROTO_UDP:
205 head_socket = &udb;
206 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
207 faddr.s_addr = ip->ip_dst.s_addr;
208 fport = udp->uh_dport;
209 laddr.s_addr = ip->ip_src.s_addr;
210 lport = udp->uh_sport;
211 last_socket = udp_last_so;
212 /* fall through */
213
214 case IPPROTO_TCP:
215 if (head_socket == NULL)
216 {
217 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
218 head_socket = &tcb; /* head_socket could be initialized with udb*/
219 faddr.s_addr = ip->ip_dst.s_addr;
220 fport = tcp->th_dport;
221 laddr.s_addr = ip->ip_src.s_addr;
222 lport = tcp->th_sport;
223 last_socket = tcp_last_so;
224 }
225 /* check last socket first */
226 if ( last_socket->so_faddr.s_addr == faddr.s_addr
227 && last_socket->so_fport == fport
228 && last_socket->so_hlport == lport)
229 {
230 found = 1;
231 so = last_socket;
232 goto sofound;
233 }
234 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
235 {
236 /* Should be reaplaced by hash here */
237 Log(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
238 if ( so->so_faddr.s_addr == faddr.s_addr
239 && so->so_fport == fport
240 && so->so_hlport == lport)
241 {
242 found = 1;
243 break;
244 }
245 }
246 break;
247
248 default:
249 LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
250 }
251 sofound:
252 if (found == 1 && icm == NULL)
253 {
254 if (so->so_state == SS_NOFDREF)
255 {
256 /* socket is shutdowning we've already sent ICMP on it.*/
257 LogRel(("NAT: Received icmp on shutdowning socket (probably corresponding ICMP socket has been already sent)\n"));
258 return NULL;
259 }
260 icm = RTMemAlloc(sizeof(struct icmp_msg));
261 icm->im_m = so->so_m;
262 icm->im_so = so;
263 found = 1;
264 Log(("hit:%R[natsock]\n", so));
265 /*XXX: this storage not very long,
266 * better add flag if it should removed from lis
267 */
268 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
269 return (icm);
270 }
271 if (found == 1)
272 return icm;
273
274 return NULL;
275}
276
277static int
278icmp_attach(PNATState pData, struct mbuf *m)
279{
280 struct icmp_msg *icm;
281 struct ip *ip;
282 ip = mtod(m, struct ip *);
283 Assert(ip->ip_p == IPPROTO_ICMP);
284 icm = RTMemAlloc(sizeof(struct icmp_msg));
285 icm->im_m = m;
286 icm->im_so = m->m_so;
287 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
288 return 0;
289}
290
291/*
292 * Process a received ICMP message.
293 */
294void
295icmp_input(PNATState pData, struct mbuf *m, int hlen)
296{
297 register struct icmp *icp;
298 register struct ip *ip = mtod(m, struct ip *);
299 int icmplen = ip->ip_len;
300 int status;
301 uint32_t dst;
302#if !defined(RT_OS_WINDOWS)
303 int ttl;
304#endif
305
306 /* int code; */
307
308 DEBUG_CALL("icmp_input");
309 DEBUG_ARG("m = %lx", (long )m);
310 DEBUG_ARG("m_len = %d", m->m_len);
311
312 icmpstat.icps_received++;
313
314 /*
315 * Locate icmp structure in mbuf, and check
316 * that its not corrupted and of at least minimum length.
317 */
318 if (icmplen < ICMP_MINLEN)
319 {
320 /* min 8 bytes payload */
321 icmpstat.icps_tooshort++;
322freeit:
323 m_freem(pData, m);
324 goto end_error;
325 }
326
327 m->m_len -= hlen;
328 m->m_data += hlen;
329 icp = mtod(m, struct icmp *);
330 if (cksum(m, icmplen))
331 {
332 icmpstat.icps_checksum++;
333 goto freeit;
334 }
335 m->m_len += hlen;
336 m->m_data -= hlen;
337
338 /* icmpstat.icps_inhist[icp->icmp_type]++; */
339 /* code = icp->icmp_code; */
340
341 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
342 switch (icp->icmp_type)
343 {
344 case ICMP_ECHO:
345 ip->ip_len += hlen; /* since ip_input subtracts this */
346 dst = ip->ip_dst.s_addr;
347 if (dst == alias_addr.s_addr)
348 {
349 icp->icmp_type = ICMP_ECHOREPLY;
350 ip->ip_dst.s_addr = ip->ip_src.s_addr;
351 ip->ip_src.s_addr = dst;
352 icmp_reflect(pData, m);
353 }
354 else
355 {
356 struct sockaddr_in addr;
357#ifdef RT_OS_WINDOWS
358 IP_OPTION_INFORMATION ipopt;
359 int error;
360#endif
361 addr.sin_family = AF_INET;
362 if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
363 {
364 /* It's an alias */
365 switch (ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
366 {
367 case CTL_DNS:
368#ifndef VBOX_WITH_MULTI_DNS
369 addr.sin_addr = dns_addr;
370 break;
371#endif
372 case CTL_ALIAS:
373 default:
374 addr.sin_addr = loopback_addr;
375 break;
376 }
377 }
378 else
379 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
380#ifndef RT_OS_WINDOWS
381 if (pData->icmp_socket.s != -1)
382 {
383 m->m_so = &pData->icmp_socket;
384 icmp_attach(pData, m);
385 ttl = ip->ip_ttl;
386 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
387 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
388 (void *)&ttl, sizeof(ttl));
389 if (status < 0)
390 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
391 strerror(errno)));
392 if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
393 (struct sockaddr *)&addr, sizeof(addr)) == -1)
394 {
395 Log((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
396 errno, strerror(errno)));
397 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0, strerror(errno));
398 }
399 }
400 else
401 {
402 /*
403 * We're freeing the ICMP message, which unable sent or process.
404 * That behavior described in rfc 793, we shouldn't notify sender about
405 * fail of processing it's ICMP packets
406 */
407 m_free(pData, m);
408 return;
409 }
410#else /* RT_OS_WINDOWS */
411 icmp_attach(pData, m);
412 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
413 pData->icmp_socket.so_icmp_id = icp->icmp_id;
414 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
415 m->m_so = &pData->icmp_socket;
416 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
417 ipopt.Ttl = ip->ip_ttl;
418 status = ICMP_SEND_ECHO(pData->phEvents[VBOX_ICMP_EVENT_INDEX], notify_slirp, addr.sin_addr.s_addr,
419 icp->icmp_data, icmplen - ICMP_MINLEN, &ipopt);
420 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
421 {
422 error = GetLastError();
423 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
424 switch (error)
425 {
426 case ERROR_INVALID_PARAMETER:
427 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
428 break;
429 case ERROR_NOT_SUPPORTED:
430 LogRel(("operation is unsupported)\n"));
431 break;
432 case ERROR_NOT_ENOUGH_MEMORY:
433 LogRel(("OOM!!!)\n"));
434 break;
435 case IP_BUF_TOO_SMALL:
436 LogRel(("Buffer too small)\n"));
437 break;
438 default:
439 LogRel(("Other error!!!)\n"));
440 break;
441 }
442 }
443#endif /* RT_OS_WINDOWS */
444 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
445 break;
446 case ICMP_UNREACH:
447 /* XXX? report error? close socket? */
448 case ICMP_TIMXCEED:
449 case ICMP_PARAMPROB:
450 case ICMP_SOURCEQUENCH:
451 case ICMP_TSTAMP:
452 case ICMP_MASKREQ:
453 case ICMP_REDIRECT:
454 icmpstat.icps_notsupp++;
455 m_freem(pData, m);
456 break;
457
458 default:
459 icmpstat.icps_badtype++;
460 m_freem(pData, m);
461 } /* switch */
462
463end_error:
464 /* m is m_free()'d xor put in a socket xor or given to ip_send */
465 ;
466}
467
468
469/*
470 * Send an ICMP message in response to a situation
471 *
472 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
473 * MUST NOT change this header information.
474 * MUST NOT reply to a multicast/broadcast IP address.
475 * MUST NOT reply to a multicast/broadcast MAC address.
476 * MUST reply to only the first fragment.
477 */
478/*
479 * Send ICMP_UNREACH back to the source regarding msrc.
480 * mbuf *msrc is used as a template, but is NOT m_free()'d.
481 * It is reported as the bad ip packet. The header should
482 * be fully correct and in host byte order.
483 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
484 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
485 */
486
487#define ICMP_MAXDATALEN (IP_MSS-28)
488void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
489{
490 unsigned hlen, shlen, s_ip_len;
491 register struct ip *ip;
492 register struct icmp *icp;
493 register struct mbuf *m;
494
495 DEBUG_CALL("icmp_error");
496 DEBUG_ARG("msrc = %lx", (long )msrc);
497 DEBUG_ARG("msrc_len = %d", msrc->m_len);
498
499 if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
500 goto end_error;
501
502 /* check msrc */
503 if (!msrc)
504 goto end_error;
505
506 ip = mtod(msrc, struct ip *);
507#if DEBUG
508 {
509 char bufa[20], bufb[20];
510 strcpy(bufa, inet_ntoa(ip->ip_src));
511 strcpy(bufb, inet_ntoa(ip->ip_dst));
512 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
513 }
514#endif
515 if (ip->ip_off & IP_OFFMASK)
516 goto end_error; /* Only reply to fragment 0 */
517
518 shlen = ip->ip_hl << 2;
519 s_ip_len = ip->ip_len;
520 if (ip->ip_p == IPPROTO_ICMP)
521 {
522 icp = (struct icmp *)((char *)ip + shlen);
523 /*
524 * Assume any unknown ICMP type is an error. This isn't
525 * specified by the RFC, but think about it..
526 */
527 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
528 goto end_error;
529 }
530
531 /* make a copy */
532 if (!(m = m_get(pData)))
533 goto end_error; /* get mbuf */
534 {
535 int new_m_size;
536 m->m_data += if_maxlinkhdr;
537 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
538 if (new_m_size>m->m_size)
539 m_inc(m, new_m_size);
540 }
541 memcpy(m->m_data, msrc->m_data, msrc->m_len);
542 m->m_len = msrc->m_len; /* copy msrc to m */
543
544 /* make the header of the reply packet */
545 ip = mtod(m, struct ip *);
546 hlen = sizeof(struct ip ); /* no options in reply */
547
548 /* fill in icmp */
549 m->m_data += hlen;
550 m->m_len -= hlen;
551
552 icp = mtod(m, struct icmp *);
553
554 if (minsize)
555 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
556 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
557 s_ip_len = ICMP_MAXDATALEN;
558
559 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
560
561 /* min. size = 8+sizeof(struct ip)+8 */
562
563 icp->icmp_type = type;
564 icp->icmp_code = code;
565 icp->icmp_id = 0;
566 icp->icmp_seq = 0;
567
568 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
569 HTONS(icp->icmp_ip.ip_len);
570 HTONS(icp->icmp_ip.ip_id);
571 HTONS(icp->icmp_ip.ip_off);
572
573#if DEBUG
574 if (message)
575 {
576 /* DEBUG : append message to ICMP packet */
577 int message_len;
578 char *cpnt;
579 message_len = strlen(message);
580 if (message_len > ICMP_MAXDATALEN)
581 message_len = ICMP_MAXDATALEN;
582 cpnt = (char *)m->m_data+m->m_len;
583 memcpy(cpnt, message, message_len);
584 m->m_len += message_len;
585 }
586#endif
587
588 icp->icmp_cksum = 0;
589 icp->icmp_cksum = cksum(m, m->m_len);
590
591 m->m_data -= hlen;
592 m->m_len += hlen;
593
594 /* fill in ip */
595 ip->ip_hl = hlen >> 2;
596 ip->ip_len = m->m_len;
597
598 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
599
600 ip->ip_ttl = MAXTTL;
601 ip->ip_p = IPPROTO_ICMP;
602 ip->ip_dst = ip->ip_src; /* ip adresses */
603 ip->ip_src = alias_addr;
604
605 (void ) ip_output(pData, (struct socket *)NULL, m);
606
607 icmpstat.icps_reflect++;
608
609 return;
610
611end_error:
612 LogRel(("NAT: error occured while sending ICMP error message \n"));
613}
614#undef ICMP_MAXDATALEN
615
616/*
617 * Reflect the ip packet back to the source
618 */
619void
620icmp_reflect(PNATState pData, struct mbuf *m)
621{
622 register struct ip *ip = mtod(m, struct ip *);
623 int hlen = ip->ip_hl << 2;
624 int optlen = hlen - sizeof(struct ip );
625 register struct icmp *icp;
626
627 /*
628 * Send an icmp packet back to the ip level,
629 * after supplying a checksum.
630 */
631 m->m_data += hlen;
632 m->m_len -= hlen;
633 icp = mtod(m, struct icmp *);
634
635 icp->icmp_cksum = 0;
636 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
637
638 m->m_data -= hlen;
639 m->m_len += hlen;
640
641 (void ) ip_output(pData, (struct socket *)NULL, m);
642
643 icmpstat.icps_reflect++;
644}
645#if defined(RT_OS_WINDOWS) && !defined(VBOX_WITH_SIMPLIFIED_SLIRP_SYNC)
646static void WINAPI
647notify_slirp(void *ctx)
648{
649 /* pData name is important see slirp_state.h */
650 PNATState pData = (PNATState)ctx;
651 fIcmp = 1;
652}
653#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