VirtualBox

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

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

NAT: logs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.5 KB
Line 
1/* $Id: ip_icmp.c 37936 2011-07-14 03:54:41Z 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\n"));
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 LogFlowFunc(("ENTER: ip->ip_p:%d\n", 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 %RTnaipv4:%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 Log(("NAT:ICMP: unsupported protocol(%d)\n", 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 Log(("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 LogFlowFunc(("LEAVE: icm:%p\n", icm));
298 return (icm);
299 }
300 if (found == 1)
301 {
302 LogFlowFunc(("LEAVE: icm:%p\n", icm));
303 return icm;
304 }
305
306 LogFlowFunc(("LEAVE: NULL\n"));
307 return NULL;
308}
309
310static int
311icmp_attach(PNATState pData, struct mbuf *m)
312{
313 struct icmp_msg *icm;
314 struct ip *ip;
315 ip = mtod(m, struct ip *);
316 Assert(ip->ip_p == IPPROTO_ICMP);
317 icm = RTMemAlloc(sizeof(struct icmp_msg));
318 icm->im_m = m;
319 icm->im_so = m->m_so;
320 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
321 return 0;
322}
323
324/*
325 * Process a received ICMP message.
326 */
327void
328icmp_input(PNATState pData, struct mbuf *m, int hlen)
329{
330 register struct icmp *icp;
331 void *icp_buf = NULL;
332 register struct ip *ip = mtod(m, struct ip *);
333 int icmplen = ip->ip_len;
334 int status;
335 uint32_t dst;
336#if !defined(RT_OS_WINDOWS)
337 int ttl;
338#endif
339
340 /* int code; */
341
342 LogFlowFunc(("ENTER: m = %lx, m_len = %d\n", (long)m, m ? m->m_len : 0));
343
344 icmpstat.icps_received++;
345
346 /*
347 * Locate icmp structure in mbuf, and check
348 * that its not corrupted and of at least minimum length.
349 */
350 if (icmplen < ICMP_MINLEN)
351 {
352 /* min 8 bytes payload */
353 icmpstat.icps_tooshort++;
354 goto end_error_free_m;
355 }
356
357 m->m_len -= hlen;
358 m->m_data += hlen;
359
360 if (cksum(m, icmplen))
361 {
362 icmpstat.icps_checksum++;
363 goto end_error_free_m;
364 }
365
366 if (m->m_next)
367 {
368 icp_buf = RTMemAlloc(icmplen);
369 if (!icp_buf)
370 {
371 Log(("NAT: not enought memory to allocate the buffer\n"));
372 goto end_error_free_m;
373 }
374 m_copydata(m, 0, icmplen, icp_buf);
375 icp = (struct icmp *)icp_buf;
376 }
377 else
378 icp = mtod(m, struct icmp *);
379
380 m->m_len += hlen;
381 m->m_data -= hlen;
382
383 /* icmpstat.icps_inhist[icp->icmp_type]++; */
384 /* code = icp->icmp_code; */
385
386 LogFlow(("icmp_type = %d\n", icp->icmp_type));
387 switch (icp->icmp_type)
388 {
389 case ICMP_ECHO:
390 ip->ip_len += hlen; /* since ip_input subtracts this */
391 dst = ip->ip_dst.s_addr;
392 if (dst == alias_addr.s_addr)
393 {
394 icp->icmp_type = ICMP_ECHOREPLY;
395 ip->ip_dst.s_addr = ip->ip_src.s_addr;
396 ip->ip_src.s_addr = dst;
397 icmp_reflect(pData, m);
398 goto done;
399 }
400 else
401 {
402 struct sockaddr_in addr;
403#ifdef RT_OS_WINDOWS
404 IP_OPTION_INFORMATION ipopt;
405 int error;
406#endif
407 addr.sin_family = AF_INET;
408 if ((ip->ip_dst.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
409 {
410 /* It's an alias */
411 switch (RT_N2H_U32(ip->ip_dst.s_addr) & ~pData->netmask)
412 {
413 case CTL_DNS:
414 case CTL_ALIAS:
415 default:
416 addr.sin_addr = loopback_addr;
417 break;
418 }
419 }
420 else
421 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
422#ifndef RT_OS_WINDOWS
423 if (pData->icmp_socket.s != -1)
424 {
425 ssize_t rc;
426 static bool fIcmpSocketErrorReported;
427 ttl = ip->ip_ttl;
428 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
429 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
430 (void *)&ttl, sizeof(ttl));
431 if (status < 0)
432 Log(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
433 strerror(errno)));
434 rc = sendto(pData->icmp_socket.s, icp, icmplen, 0,
435 (struct sockaddr *)&addr, sizeof(addr));
436 if (rc >= 0)
437 {
438 m->m_so = &pData->icmp_socket;
439 icmp_attach(pData, m);
440 /* don't let m_freem at the end free atached buffer */
441 goto done;
442 }
443
444
445 if (!fIcmpSocketErrorReported)
446 {
447 LogRel(("icmp_input udp sendto tx errno = %d (%s)\n",
448 errno, strerror(errno)));
449 fIcmpSocketErrorReported = true;
450 }
451 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
452 }
453#else /* RT_OS_WINDOWS */
454 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
455 pData->icmp_socket.so_icmp_id = icp->icmp_id;
456 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
457 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
458 ipopt.Ttl = ip->ip_ttl;
459 status = IcmpSendEcho2(pData->icmp_socket.sh /*=handle*/,
460 pData->phEvents[VBOX_ICMP_EVENT_INDEX] /*=Event*/,
461 NULL /*=ApcRoutine*/,
462 NULL /*=ApcContext*/,
463 addr.sin_addr.s_addr /*=DestinationAddress*/,
464 icp->icmp_data /*=RequestData*/,
465 icmplen - ICMP_MINLEN /*=RequestSize*/,
466 &ipopt /*=RequestOptions*/,
467 pData->pvIcmpBuffer /*=ReplyBuffer*/,
468 pData->szIcmpBuffer /*=ReplySize*/,
469 1 /*=Timeout in ms*/);
470 error = GetLastError();
471 if ( status != 0
472 || error == ERROR_IO_PENDING)
473 {
474 /* no error! */
475 m->m_so = &pData->icmp_socket;
476 icmp_attach(pData, m);
477 /* don't let m_freem at the end free atached buffer */
478 goto done;
479 }
480 Log(("NAT: Error (%d) occurred while sending ICMP (", error));
481 switch (error)
482 {
483 case ERROR_INVALID_PARAMETER:
484 Log(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
485 break;
486 case ERROR_NOT_SUPPORTED:
487 Log(("operation is unsupported)\n"));
488 break;
489 case ERROR_NOT_ENOUGH_MEMORY:
490 Log(("OOM!!!)\n"));
491 break;
492 case IP_BUF_TOO_SMALL:
493 Log(("Buffer too small)\n"));
494 break;
495 default:
496 Log(("Other error!!!)\n"));
497 break;
498 }
499#endif /* RT_OS_WINDOWS */
500 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
501 break;
502 case ICMP_UNREACH:
503 case ICMP_TIMXCEED:
504 /* @todo(vvl): both up cases comes from guest,
505 * indeed right solution would be find the socket
506 * corresponding to ICMP data and close it.
507 */
508 case ICMP_PARAMPROB:
509 case ICMP_SOURCEQUENCH:
510 case ICMP_TSTAMP:
511 case ICMP_MASKREQ:
512 case ICMP_REDIRECT:
513 icmpstat.icps_notsupp++;
514 break;
515
516 default:
517 icmpstat.icps_badtype++;
518 } /* switch */
519
520end_error_free_m:
521 m_freem(pData, m);
522
523done:
524 if (icp_buf)
525 RTMemFree(icp_buf);
526}
527
528
529/**
530 * Send an ICMP message in response to a situation
531 *
532 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
533 * MUST NOT change this header information.
534 * MUST NOT reply to a multicast/broadcast IP address.
535 * MUST NOT reply to a multicast/broadcast MAC address.
536 * MUST reply to only the first fragment.
537 *
538 * Send ICMP_UNREACH back to the source regarding msrc.
539 * It is reported as the bad ip packet. The header should
540 * be fully correct and in host byte order.
541 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
542 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
543 *
544 * @note This function will free msrc!
545 */
546
547#define ICMP_MAXDATALEN (IP_MSS-28)
548void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
549{
550 unsigned hlen, shlen, s_ip_len;
551 register struct ip *ip;
552 register struct icmp *icp;
553 register struct mbuf *m;
554 int new_m_size = 0;
555 int size = 0;
556
557 LogFlow(("icmp_error: msrc = %lx, msrc_len = %d\n", (long)msrc, msrc ? msrc->m_len : 0));
558 if (msrc != NULL)
559 M_ASSERTPKTHDR(msrc);
560
561 if ( type != ICMP_UNREACH
562 && type != ICMP_TIMXCEED
563 && type != ICMP_SOURCEQUENCH)
564 goto end_error;
565
566 /* check msrc */
567 if (!msrc)
568 goto end_error;
569
570 ip = mtod(msrc, struct ip *);
571#if DEBUG
572 {
573 char bufa[20], bufb[20];
574 strcpy(bufa, inet_ntoa(ip->ip_src));
575 strcpy(bufb, inet_ntoa(ip->ip_dst));
576 Log2((" %.16s to %.16s\n", bufa, bufb));
577 }
578#endif
579 if ( ip->ip_off & IP_OFFMASK
580 && type != ICMP_SOURCEQUENCH)
581 goto end_error; /* Only reply to fragment 0 */
582
583 shlen = ip->ip_hl << 2;
584 s_ip_len = ip->ip_len;
585 if (ip->ip_p == IPPROTO_ICMP)
586 {
587 icp = (struct icmp *)((char *)ip + shlen);
588 /*
589 * Assume any unknown ICMP type is an error. This isn't
590 * specified by the RFC, but think about it..
591 */
592 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
593 goto end_error;
594 }
595
596 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
597 if (new_m_size < MSIZE)
598 size = MCLBYTES;
599 else if (new_m_size < MCLBYTES)
600 size = MCLBYTES;
601 else if(new_m_size < MJUM9BYTES)
602 size = MJUM9BYTES;
603 else if (new_m_size < MJUM16BYTES)
604 size = MJUM16BYTES;
605 else
606 AssertMsgFailed(("Unsupported size"));
607 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
608 if (!m)
609 goto end_error;
610
611 m->m_data += if_maxlinkhdr;
612 m->m_pkthdr.header = mtod(m, void *);
613
614 memcpy(m->m_data, msrc->m_data, msrc->m_len);
615 m->m_len = msrc->m_len; /* copy msrc to m */
616
617 /* make the header of the reply packet */
618 ip = mtod(m, struct ip *);
619 hlen = sizeof(struct ip); /* no options in reply */
620
621 /* fill in icmp */
622 m->m_data += hlen;
623 m->m_len -= hlen;
624
625 icp = mtod(m, struct icmp *);
626
627 if (minsize)
628 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
629 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
630 s_ip_len = ICMP_MAXDATALEN;
631
632 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
633
634 /* min. size = 8+sizeof(struct ip)+8 */
635
636 icp->icmp_type = type;
637 icp->icmp_code = code;
638 icp->icmp_id = 0;
639 icp->icmp_seq = 0;
640
641 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
642
643 HTONS(icp->icmp_ip.ip_len);
644 HTONS(icp->icmp_ip.ip_id);
645 HTONS(icp->icmp_ip.ip_off);
646
647#if DEBUG
648 if (message)
649 {
650 /* DEBUG : append message to ICMP packet */
651 int message_len;
652 char *cpnt;
653 message_len = strlen(message);
654 if (message_len > ICMP_MAXDATALEN)
655 message_len = ICMP_MAXDATALEN;
656 cpnt = (char *)m->m_data+m->m_len;
657 m_append(pData, m, message_len, message);
658 }
659#endif
660
661 icp->icmp_cksum = 0;
662 icp->icmp_cksum = cksum(m, m->m_len);
663
664 /* fill in ip */
665 ip->ip_hl = hlen >> 2;
666 ip->ip_len = m->m_len;
667
668 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
669
670 ip->ip_ttl = MAXTTL;
671 ip->ip_p = IPPROTO_ICMP;
672 ip->ip_dst = ip->ip_src; /* ip adresses */
673 ip->ip_src = alias_addr;
674
675 /* returns pointer back. */
676 m->m_data -= hlen;
677 m->m_len += hlen;
678 (void) ip_output0(pData, (struct socket *)NULL, m, 1);
679
680 icmpstat.icps_reflect++;
681
682 /* clear source datagramm in positive branch */
683 m_freem(pData, msrc);
684 LogFlowFuncLeave();
685 return;
686
687end_error_free_m:
688 m_freem(pData, m);
689
690end_error:
691
692 /*
693 * clear source datagramm in case if some of requirement haven't been met.
694 */
695 if (!msrc)
696 m_freem(pData, msrc);
697
698 {
699 static bool fIcmpErrorReported;
700 if (!fIcmpErrorReported)
701 {
702 LogRel(("NAT: error occurred while sending ICMP error message\n"));
703 fIcmpErrorReported = true;
704 }
705 }
706 LogFlowFuncLeave();
707}
708#undef ICMP_MAXDATALEN
709
710/*
711 * Reflect the ip packet back to the source
712 * Note: m isn't duplicated by this method and more delivered to ip_output then.
713 */
714void
715icmp_reflect(PNATState pData, struct mbuf *m)
716{
717 register struct ip *ip = mtod(m, struct ip *);
718 int hlen = ip->ip_hl << 2;
719 int optlen = hlen - sizeof(struct ip);
720 register struct icmp *icp;
721 LogFlowFunc(("ENTER: m:%p\n", m));
722
723 /*
724 * Send an icmp packet back to the ip level,
725 * after supplying a checksum.
726 */
727 m->m_data += hlen;
728 m->m_len -= hlen;
729 icp = mtod(m, struct icmp *);
730
731 icp->icmp_cksum = 0;
732 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
733
734 m->m_data -= hlen;
735 m->m_len += hlen;
736
737 (void) ip_output(pData, (struct socket *)NULL, m);
738
739 icmpstat.icps_reflect++;
740 LogFlowFuncLeave();
741}
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