VirtualBox

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

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

slirp:icmp:

  1. trying to socket list for traceroute searching shorter
  2. sockets should be closed, which received ICMP packets (disabled need to be fixed)
  • Property svn:eol-style set to native
File size: 22.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(AF_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;
149 struct socket *last_socket;
150 struct socket *so;
151 struct in_addr laddr, faddr;
152 u_int lport, fport;
153
154 laddr.s_addr = ~0;
155 faddr.s_addr = ~0;
156
157 lport = ~0;
158 fport = ~0;
159
160
161 LogRel(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
162 switch (ip->ip_p)
163 {
164 case IPPROTO_ICMP:
165 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
166 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
167 {
168 m0 = icm->im_m;
169 ip0 = mtod(m0, struct ip *);
170 AssertRelease(ip0->ip_p == IPPROTO_ICMP);
171 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
172 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
173 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
174 && icp->icmp_id == icp0->icmp_id
175 && icp->icmp_seq == icp->icmp_seq)
176 {
177 found = 1;
178 break;
179 }
180 LogRel(("Have found nothing\n"));
181 }
182 break;
183
184 /*
185 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
186 * from which the IP package has been sent.
187 */
188 case IPPROTO_UDP:
189 head_socket = &udb;
190 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
191 faddr.s_addr = ip->ip_dst.s_addr;
192 fport = udp->uh_dport;
193 laddr.s_addr = ip->ip_src.s_addr;
194 lport = udp->uh_sport;
195 last_socket = udp_last_so;
196 /* fall through */
197
198 case IPPROTO_TCP:
199 if (head_socket == NULL)
200 {
201 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
202 head_socket = &tcb; /* head_socket could be initialized with udb*/
203 faddr.s_addr = ip->ip_dst.s_addr;
204 fport = tcp->th_dport;
205 laddr.s_addr = ip->ip_src.s_addr;
206 lport = tcp->th_sport;
207 last_socket = tcp_last_so;
208 }
209 /* check last socket first */
210 if ( last_socket->so_faddr.s_addr == faddr.s_addr
211 && last_socket->so_fport == fport
212 && last_socket->so_hlport == lport)
213 {
214 found = 1;
215 goto sofound;
216 }
217 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
218 {
219 /* Should be reaplaced by hash here */
220 LogRel(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
221 if ( so->so_faddr.s_addr == faddr.s_addr
222 && so->so_fport == fport
223 && so->so_hlport == lport)
224 {
225 found = 1;
226 break;
227 }
228 }
229 break;
230
231 default:
232 LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
233 }
234 sofound:
235 if (found == 1 && icm == NULL)
236 {
237 icm = malloc(sizeof(struct icmp_msg));
238 icm->im_m = so->so_m;
239 icm->im_so = so;
240 found = 1;
241 LogRel(("hit:%R[natsock]\n", so));
242 /*XXX: this storage not very long,
243 * better add flag if it should removed from lis
244 */
245 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
246 return (icm);
247 }
248 if (found == 1)
249 return icm;
250
251 return NULL;
252}
253
254static int
255icmp_attach(PNATState pData, struct mbuf *m)
256{
257 struct icmp_msg *icm;
258 struct ip *ip;
259 ip = mtod(m, struct ip *);
260 Assert(ip->ip_p == IPPROTO_ICMP);
261 icm = malloc(sizeof(struct icmp_msg));
262 icm->im_m = m;
263 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
264 return 0;
265}
266#endif /* VBOX_WITH_SLIRP_ICMP */
267
268/*
269 * Process a received ICMP message.
270 */
271void
272icmp_input(PNATState pData, struct mbuf *m, int hlen)
273{
274 register struct icmp *icp;
275 register struct ip *ip = mtod(m, struct ip *);
276 int icmplen = ip->ip_len;
277 int status;
278 uint32_t dst;
279
280 /* int code; */
281
282 DEBUG_CALL("icmp_input");
283 DEBUG_ARG("m = %lx", (long )m);
284 DEBUG_ARG("m_len = %d", m->m_len);
285
286 icmpstat.icps_received++;
287
288 /*
289 * Locate icmp structure in mbuf, and check
290 * that its not corrupted and of at least minimum length.
291 */
292 if (icmplen < ICMP_MINLEN)
293 {
294 /* min 8 bytes payload */
295 icmpstat.icps_tooshort++;
296freeit:
297 m_freem(pData, m);
298 goto end_error;
299 }
300
301 m->m_len -= hlen;
302 m->m_data += hlen;
303 icp = mtod(m, struct icmp *);
304 if (cksum(m, icmplen))
305 {
306 icmpstat.icps_checksum++;
307 goto freeit;
308 }
309 m->m_len += hlen;
310 m->m_data -= hlen;
311
312 /* icmpstat.icps_inhist[icp->icmp_type]++; */
313 /* code = icp->icmp_code; */
314
315 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
316 switch (icp->icmp_type)
317 {
318 case ICMP_ECHO:
319#ifndef VBOX_WITH_SLIRP_ICMP
320 icp->icmp_type = ICMP_ECHOREPLY;
321#endif /* !VBOX_WITH_SLIRP_ICMP */
322
323 ip->ip_len += hlen; /* since ip_input subtracts this */
324 dst = ip->ip_dst.s_addr;
325 if (dst == alias_addr.s_addr)
326 {
327#ifdef VBOX_WITH_SLIRP_ICMP
328 icp->icmp_type = ICMP_ECHOREPLY;
329 ip->ip_dst.s_addr = ip->ip_src.s_addr;
330 ip->ip_src.s_addr = dst;
331#endif /* VBOX_WITH_SLIRP_ICMP */
332 icmp_reflect(pData, m);
333 }
334 else
335 {
336 struct socket *so;
337 struct sockaddr_in addr;
338#ifndef VBOX_WITH_SLIRP_ICMP
339 if ((so = socreate()) == NULL)
340 goto freeit;
341 if (udp_attach(pData, so) == -1)
342 {
343 DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
344 errno,strerror(errno)));
345 sofree(pData, so);
346 m_free(pData, m);
347 goto end_error;
348 }
349 so->so_m = m;
350 so->so_faddr = ip->ip_dst;
351 so->so_fport = htons(7);
352 so->so_laddr = ip->ip_src;
353 so->so_lport = htons(9);
354 so->so_iptos = ip->ip_tos;
355 so->so_type = IPPROTO_ICMP;
356 so->so_state = SS_ISFCONNECTED;
357
358 addr.sin_family = AF_INET;
359 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
360 {
361 /* It's an alias */
362 switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
363 {
364 case CTL_DNS:
365 addr.sin_addr = dns_addr;
366 break;
367 case CTL_ALIAS:
368 default:
369 addr.sin_addr = loopback_addr;
370 break;
371 }
372 }
373 else
374 addr.sin_addr = so->so_faddr;
375 addr.sin_port = so->so_fport;
376 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
377 (struct sockaddr *)&addr, sizeof(addr)) == -1)
378 {
379 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
380 errno,strerror(errno)));
381 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
382 udp_detach(pData, so);
383 }
384#else /* VBOX_WITH_SLIRP_ICMP */
385# ifdef RT_OS_WINDOWS
386 IP_OPTION_INFORMATION ipopt;
387 int error;
388# endif
389 addr.sin_family = AF_INET;
390 if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
391 {
392 /* It's an alias */
393 switch(ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
394 {
395 case CTL_DNS:
396 addr.sin_addr = dns_addr;
397 break;
398 case CTL_ALIAS:
399 default:
400 addr.sin_addr = loopback_addr;
401 break;
402 }
403 }
404 else
405 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
406 icmp_attach(pData, m);
407 /* Send the packet */
408# ifndef RT_OS_WINDOWS
409 LogRel(("NAT/ICMP: try to set TTL(%d)\n", ip->ip_ttl));
410 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL, (void *)&ip->ip_ttl, sizeof(ip->ip_ttl));
411 if (status < 0)
412 {
413 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n", strerror(errno)));
414 }
415 if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
416 (struct sockaddr *)&addr, sizeof(addr)) == -1)
417 {
418 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
419 errno,strerror(errno)));
420 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
421 m_free(pData, m);
422 }
423# else /* RT_OS_WINDOWS */
424 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
425 pData->icmp_socket.so_icmp_id = icp->icmp_id;
426 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
427 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
428 ipopt.Ttl = ip->ip_ttl;
429 status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
430 NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
431 icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
432 pData->pvIcmpBuffer, pData->szIcmpBuffer, 1);
433 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
434 {
435 error = GetLastError();
436 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
437 switch (error)
438 {
439 case ERROR_INVALID_PARAMETER:
440 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
441 break;
442 case ERROR_NOT_SUPPORTED:
443 LogRel(("operation is unsupported)\n"));
444 break;
445 case ERROR_NOT_ENOUGH_MEMORY:
446 LogRel(("OOM!!!)\n"));
447 break;
448 case IP_BUF_TOO_SMALL:
449 LogRel(("Buffer too small)\n"));
450 break;
451 default:
452 LogRel(("Other error!!!)\n"));
453 break;
454 }
455 }
456# endif /* RT_OS_WINDOWS */
457#endif /* VBOX_WITH_SLIRP_ICMP */
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 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
551 if (new_m_size>m->m_size)
552 m_inc(m, new_m_size);
553 }
554 memcpy(m->m_data, msrc->m_data, msrc->m_len);
555 m->m_len = msrc->m_len; /* copy msrc to m */
556
557 /* make the header of the reply packet */
558 ip = mtod(m, struct ip *);
559 hlen = sizeof(struct ip ); /* no options in reply */
560
561 /* fill in icmp */
562 m->m_data += hlen;
563 m->m_len -= hlen;
564
565 icp = mtod(m, struct icmp *);
566
567 if (minsize)
568 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
569 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
570 s_ip_len = ICMP_MAXDATALEN;
571
572 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
573
574 /* min. size = 8+sizeof(struct ip)+8 */
575
576 icp->icmp_type = type;
577 icp->icmp_code = code;
578 icp->icmp_id = 0;
579 icp->icmp_seq = 0;
580
581 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
582 HTONS(icp->icmp_ip.ip_len);
583 HTONS(icp->icmp_ip.ip_id);
584 HTONS(icp->icmp_ip.ip_off);
585
586#if DEBUG
587 if (message)
588 {
589 /* DEBUG : append message to ICMP packet */
590 int message_len;
591 char *cpnt;
592 message_len = strlen(message);
593 if (message_len > ICMP_MAXDATALEN)
594 message_len = ICMP_MAXDATALEN;
595 cpnt = (char *)m->m_data+m->m_len;
596 memcpy(cpnt, message, message_len);
597 m->m_len += message_len;
598 }
599#endif
600
601 icp->icmp_cksum = 0;
602 icp->icmp_cksum = cksum(m, m->m_len);
603
604 m->m_data -= hlen;
605 m->m_len += hlen;
606
607 /* fill in ip */
608 ip->ip_hl = hlen >> 2;
609 ip->ip_len = m->m_len;
610
611 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
612
613 ip->ip_ttl = MAXTTL;
614 ip->ip_p = IPPROTO_ICMP;
615 ip->ip_dst = ip->ip_src; /* ip adresses */
616 ip->ip_src = alias_addr;
617
618 (void ) ip_output(pData, (struct socket *)NULL, m);
619
620 icmpstat.icps_reflect++;
621
622end_error:
623 ;
624}
625#undef ICMP_MAXDATALEN
626
627/*
628 * Reflect the ip packet back to the source
629 */
630void
631icmp_reflect(PNATState pData, struct mbuf *m)
632{
633 register struct ip *ip = mtod(m, struct ip *);
634 int hlen = ip->ip_hl << 2;
635 int optlen = hlen - sizeof(struct ip );
636 register struct icmp *icp;
637
638 /*
639 * Send an icmp packet back to the ip level,
640 * after supplying a checksum.
641 */
642 m->m_data += hlen;
643 m->m_len -= hlen;
644 icp = mtod(m, struct icmp *);
645
646 icp->icmp_cksum = 0;
647 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
648
649 m->m_data -= hlen;
650 m->m_len += hlen;
651
652#ifndef VBOX_WITH_SLIRP_ICMP
653 /* fill in ip */
654 if (optlen > 0)
655 {
656 /*
657 * Strip out original options by copying rest of first
658 * mbuf's data back, and adjust the IP length.
659 */
660 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
661 (unsigned )(m->m_len - hlen));
662 hlen -= optlen;
663 ip->ip_hl = hlen >> 2;
664 ip->ip_len -= optlen;
665 m->m_len -= optlen;
666 }
667 ip->ip_ttl = MAXTTL;
668 {
669 /* swap */
670 struct in_addr icmp_dst;
671 icmp_dst = ip->ip_dst;
672 ip->ip_dst = ip->ip_src;
673 ip->ip_src = icmp_dst;
674 }
675#endif /* !VBOX_WITH_SLIRP_ICMP */
676
677 (void ) ip_output(pData, (struct socket *)NULL, m);
678
679 icmpstat.icps_reflect++;
680}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette