VirtualBox

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

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

NAT: comment to ICMP find routine.

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