VirtualBox

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

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

icmp:slirp: correcting switch

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