VirtualBox

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

Last change on this file since 40421 was 40421, checked in by vboxsync, 13 years ago

NAT: warnings [-Wunused-but-set-variable]

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