VirtualBox

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

Last change on this file since 33955 was 33955, checked in by vboxsync, 14 years ago

NAT: log.

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