VirtualBox

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

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

NAT: warnings.

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