VirtualBox

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

Last change on this file since 53776 was 53776, checked in by vboxsync, 10 years ago

NAT: Make it possible to use privileged helper to obtain raw ICMP
socket when debugging unprivileged. Conditional on
VBOX_RAWSOCK_DEBUG_HELPER that is added to the makefile but is
commented out.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1/* $Id: ip_icmp.c 53776 2015-01-12 16:38:11Z vboxsync $ */
2/** @file
3 * NAT - IP/ICMP handling.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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
59#ifdef VBOX_RAWSOCK_DEBUG_HELPER
60int getrawsock(int type);
61#endif
62
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
100#ifndef RT_OS_WINDOWS
101 if (iIcmpCacheLimit < 0)
102 {
103 LogRel(("NAT: iIcmpCacheLimit is invalid %d, will be alter to default value 100\n", iIcmpCacheLimit));
104 iIcmpCacheLimit = 100;
105 }
106 pData->iIcmpCacheLimit = iIcmpCacheLimit;
107# ifndef RT_OS_DARWIN
108 pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
109# else /* !RT_OS_DARWIN */
110 pData->icmp_socket.s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
111# endif /* RT_OS_DARWIN */
112 if (pData->icmp_socket.s == -1)
113 {
114 int rc = RTErrConvertFromErrno(errno);
115# if defined(RT_OS_DARWIN) || !defined(VBOX_RAWSOCK_DEBUG_HELPER)
116 LogRel(("NAT: ICMP/ping not available (could not open ICMP socket, error %Rrc)\n", rc));
117 return 1;
118# else
119 /* try to get it from privileged helper */
120 LogRel(("NAT: ICMP/ping raw socket error %Rrc, asking helper...\n", rc));
121 pData->icmp_socket.s = getrawsock(AF_INET);
122 if (pData->icmp_socket.s == -1)
123 {
124 LogRel(("NAT: ICMP/ping not available\n"));
125 return 1;
126 }
127# endif /* !RT_OS_DARWIN && VBOX_RAWSOCK_DEBUG_HELPER */
128 }
129 fd_nonblock(pData->icmp_socket.s);
130 NSOCK_INC();
131
132 LIST_INIT(&pData->icmp_msg_head);
133
134#else /* RT_OS_WINDOWS */
135 if (icmpwin_init(pData) != 0)
136 return 1;
137#endif /* RT_OS_WINDOWS */
138
139 return 0;
140}
141
142/**
143 * Cleans ICMP cache.
144 */
145void
146icmp_finit(PNATState pData)
147{
148#ifdef RT_OS_WINDOWS
149 icmpwin_finit(pData);
150#else
151 icmp_cache_clean(pData, -1);
152 closesocket(pData->icmp_socket.s);
153#endif
154}
155
156#if !defined(RT_OS_WINDOWS)
157/*
158 * ip here is ip header + 64bytes readed from ICMP packet
159 */
160struct icmp_msg *
161icmp_find_original_mbuf(PNATState pData, struct ip *ip)
162{
163 struct mbuf *m0;
164 struct ip *ip0;
165 struct icmp *icp, *icp0;
166 struct icmp_msg *icm = NULL;
167 int found = 0;
168 struct udphdr *udp;
169 struct tcphdr *tcp;
170 struct socket *head_socket = NULL;
171 struct socket *last_socket = NULL;
172 struct socket *so = NULL;
173 struct in_addr faddr;
174 u_short lport, fport;
175
176 faddr.s_addr = ~0;
177
178 lport = ~0;
179 fport = ~0;
180
181
182 LogFlowFunc(("ENTER: ip->ip_p:%d\n", ip->ip_p));
183 switch (ip->ip_p)
184 {
185 case IPPROTO_ICMP:
186 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
187 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
188 {
189 m0 = icm->im_m;
190 ip0 = mtod(m0, struct ip *);
191 if (ip0->ip_p != IPPROTO_ICMP)
192 {
193 /* try next item */
194 continue;
195 }
196 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
197 /*
198 * IP could pointer to ICMP_REPLY datagram (1)
199 * or pointer IP header in ICMP payload in case of
200 * ICMP_TIMXCEED or ICMP_UNREACH (2)
201 *
202 * if (1) and then ICMP (type should be ICMP_ECHOREPLY) and we need check that
203 * IP.IP_SRC == IP0.IP_DST received datagramm comes from destination.
204 *
205 * if (2) then check that payload ICMP has got type ICMP_ECHO and
206 * IP.IP_DST == IP0.IP_DST destination of returned datagram is the same as
207 * one was sent.
208 */
209 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
210 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
211 && icp->icmp_id == icp0->icmp_id
212 && icp->icmp_seq == icp0->icmp_seq)
213 {
214 found = 1;
215 Log(("Have found %R[natsock]\n", icm->im_so));
216 break;
217 }
218 Log(("Have found nothing\n"));
219 }
220 break;
221
222 /*
223 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
224 * from which the IP package has been sent.
225 */
226 case IPPROTO_UDP:
227 head_socket = &udb;
228 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
229 faddr.s_addr = ip->ip_dst.s_addr;
230 fport = udp->uh_dport;
231 lport = udp->uh_sport;
232 last_socket = udp_last_so;
233 /* fall through */
234
235 case IPPROTO_TCP:
236 if (head_socket == NULL)
237 {
238 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
239 head_socket = &tcb; /* head_socket could be initialized with udb*/
240 faddr.s_addr = ip->ip_dst.s_addr;
241 fport = tcp->th_dport;
242 lport = tcp->th_sport;
243 last_socket = tcp_last_so;
244 }
245 /* check last socket first */
246 if ( last_socket->so_faddr.s_addr == faddr.s_addr
247 && last_socket->so_fport == fport
248 && last_socket->so_hlport == lport)
249 {
250 found = 1;
251 so = last_socket;
252 goto sofound;
253 }
254 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
255 {
256 /* Should be reaplaced by hash here */
257 Log(("trying:%R[natsock] against %RTnaipv4:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
258 if ( so->so_faddr.s_addr == faddr.s_addr
259 && so->so_fport == fport
260 && so->so_hlport == lport)
261 {
262 found = 1;
263 break;
264 }
265 }
266 break;
267
268 default:
269 Log(("NAT:ICMP: unsupported protocol(%d)\n", ip->ip_p));
270 }
271 sofound:
272 if (found == 1 && icm == NULL)
273 {
274 if (so->so_state == SS_NOFDREF)
275 {
276 /* socket is shutdowning we've already sent ICMP on it.*/
277 Log(("NAT: Received icmp on shutdowning socket (probably corresponding ICMP socket has been already sent)\n"));
278 return NULL;
279 }
280 icm = RTMemAlloc(sizeof(struct icmp_msg));
281 icm->im_m = so->so_m;
282 icm->im_so = so;
283 found = 1;
284 Log(("hit:%R[natsock]\n", so));
285 /*XXX: this storage not very long,
286 * better add flag if it should removed from lis
287 */
288 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
289 pData->cIcmpCacheSize++;
290 if (pData->cIcmpCacheSize > pData->iIcmpCacheLimit)
291 icmp_cache_clean(pData, pData->iIcmpCacheLimit/2);
292 LogFlowFunc(("LEAVE: icm:%p\n", icm));
293 return (icm);
294 }
295 if (found == 1)
296 {
297 LogFlowFunc(("LEAVE: icm:%p\n", icm));
298 return icm;
299 }
300
301 LogFlowFunc(("LEAVE: NULL\n"));
302 return NULL;
303}
304
305/**
306 * iEntries how many entries to leave, if iEntries < 0, clean all
307 */
308static void icmp_cache_clean(PNATState pData, int iEntries)
309{
310 int iIcmpCount = 0;
311 struct icmp_msg *icm = NULL;
312 LogFlowFunc(("iEntries:%d\n", iEntries));
313 if (iEntries > pData->cIcmpCacheSize)
314 {
315 LogFlowFuncLeave();
316 return;
317 }
318 while(!LIST_EMPTY(&pData->icmp_msg_head))
319 {
320 icm = LIST_FIRST(&pData->icmp_msg_head);
321 if ( iEntries > 0
322 && iIcmpCount < iEntries)
323 {
324 iIcmpCount++;
325 continue;
326 }
327
328 LIST_REMOVE(icm, im_list);
329 if (icm->im_m)
330 {
331 pData->cIcmpCacheSize--;
332 m_freem(pData, icm->im_m);
333 }
334 RTMemFree(icm);
335 }
336 LogFlowFuncLeave();
337}
338
339static int
340icmp_attach(PNATState pData, struct mbuf *m)
341{
342 struct icmp_msg *icm;
343 struct ip *ip;
344 ip = mtod(m, struct ip *);
345 Assert(ip->ip_p == IPPROTO_ICMP);
346 icm = RTMemAlloc(sizeof(struct icmp_msg));
347 icm->im_m = m;
348 icm->im_so = m->m_so;
349 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
350 pData->cIcmpCacheSize++;
351 if (pData->cIcmpCacheSize > pData->iIcmpCacheLimit)
352 icmp_cache_clean(pData, pData->iIcmpCacheLimit/2);
353 return 0;
354}
355#endif /* !RT_OS_WINDOWS */
356
357/*
358 * Process a received ICMP message.
359 */
360void
361icmp_input(PNATState pData, struct mbuf *m, int hlen)
362{
363 register struct ip *ip = mtod(m, struct ip *);
364 int icmplen = ip->ip_len;
365 uint8_t icmp_type;
366 void *icp_buf = NULL;
367 uint32_t dst;
368
369 /* int code; */
370
371 LogFlowFunc(("ENTER: m = %lx, m_len = %d\n", (long)m, m ? m->m_len : 0));
372
373 icmpstat.icps_received++;
374
375 /*
376 * Locate icmp structure in mbuf, and check
377 * that its not corrupted and of at least minimum length.
378 */
379 if (icmplen < ICMP_MINLEN)
380 {
381 /* min 8 bytes payload */
382 icmpstat.icps_tooshort++;
383 goto end_error_free_m;
384 }
385
386 m->m_len -= hlen;
387 m->m_data += hlen;
388
389 if (cksum(m, icmplen))
390 {
391 icmpstat.icps_checksum++;
392 goto end_error_free_m;
393 }
394
395 /* are we guaranteed to have ICMP header in first mbuf? be safe. */
396 m_copydata(m, 0, sizeof(icmp_type), (caddr_t)&icmp_type);
397
398 m->m_len += hlen;
399 m->m_data -= hlen;
400
401 /* icmpstat.icps_inhist[icp->icmp_type]++; */
402 /* code = icp->icmp_code; */
403
404 LogFlow(("icmp_type = %d\n", icmp_type));
405 switch (icmp_type)
406 {
407 case ICMP_ECHO:
408 ip->ip_len += hlen; /* since ip_input subtracts this */
409 dst = ip->ip_dst.s_addr;
410 if ( CTL_CHECK(dst, CTL_ALIAS)
411 || CTL_CHECK(dst, CTL_DNS)
412 || CTL_CHECK(dst, CTL_TFTP))
413 {
414 uint8_t echo_reply = ICMP_ECHOREPLY;
415 m_copyback(pData, m, hlen + RT_OFFSETOF(struct icmp, icmp_type),
416 sizeof(echo_reply), (caddr_t)&echo_reply);
417 ip->ip_dst.s_addr = ip->ip_src.s_addr;
418 ip->ip_src.s_addr = dst;
419 icmp_reflect(pData, m);
420 goto done;
421 }
422
423#ifdef RT_OS_WINDOWS
424 {
425 icmpwin_ping(pData, m, hlen);
426 break; /* free mbuf */
427 }
428#else
429 {
430 struct icmp *icp;
431 struct sockaddr_in addr;
432
433 /* XXX: FIXME: this is bogus, see CTL_CHECKs above */
434 addr.sin_family = AF_INET;
435 if ((ip->ip_dst.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
436 {
437 /* It's an alias */
438 switch (RT_N2H_U32(ip->ip_dst.s_addr) & ~pData->netmask)
439 {
440 case CTL_DNS:
441 case CTL_ALIAS:
442 default:
443 addr.sin_addr = loopback_addr;
444 break;
445 }
446 }
447 else
448 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
449
450 if (m->m_next)
451 {
452 icp_buf = RTMemAlloc(icmplen);
453 if (!icp_buf)
454 {
455 Log(("NAT: not enought memory to allocate the buffer\n"));
456 goto end_error_free_m;
457 }
458 m_copydata(m, hlen, icmplen, icp_buf);
459 icp = (struct icmp *)icp_buf;
460 }
461 else
462 icp = (struct icmp *)(mtod(m, char *) + hlen);
463
464 if (pData->icmp_socket.s != -1)
465 {
466 static bool fIcmpSocketErrorReported;
467 int ttl;
468 int status;
469 ssize_t rc;
470
471 ttl = ip->ip_ttl;
472 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
473 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
474 (void *)&ttl, sizeof(ttl));
475 if (status < 0)
476 Log(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
477 strerror(errno)));
478 rc = sendto(pData->icmp_socket.s, icp, icmplen, 0,
479 (struct sockaddr *)&addr, sizeof(addr));
480 if (rc >= 0)
481 {
482 m->m_so = &pData->icmp_socket;
483 icmp_attach(pData, m);
484 /* don't let m_freem at the end free atached buffer */
485 goto done;
486 }
487
488
489 if (!fIcmpSocketErrorReported)
490 {
491 LogRel(("icmp_input udp sendto tx errno = %d (%s)\n",
492 errno, strerror(errno)));
493 fIcmpSocketErrorReported = true;
494 }
495 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
496 }
497 }
498#endif /* !RT_OS_WINDOWS */
499 break;
500 case ICMP_UNREACH:
501 case ICMP_TIMXCEED:
502 /* @todo(vvl): both up cases comes from guest,
503 * indeed right solution would be find the socket
504 * corresponding to ICMP data and close it.
505 */
506 case ICMP_PARAMPROB:
507 case ICMP_SOURCEQUENCH:
508 case ICMP_TSTAMP:
509 case ICMP_MASKREQ:
510 case ICMP_REDIRECT:
511 icmpstat.icps_notsupp++;
512 break;
513
514 default:
515 icmpstat.icps_badtype++;
516 } /* switch */
517
518end_error_free_m:
519 m_freem(pData, m);
520
521done:
522 if (icp_buf)
523 RTMemFree(icp_buf);
524}
525
526
527/**
528 * Send an ICMP message in response to a situation
529 *
530 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
531 * MUST NOT change this header information.
532 * MUST NOT reply to a multicast/broadcast IP address.
533 * MUST NOT reply to a multicast/broadcast MAC address.
534 * MUST reply to only the first fragment.
535 *
536 * Send ICMP_UNREACH back to the source regarding msrc.
537 * It is reported as the bad ip packet. The header should
538 * be fully correct and in host byte order.
539 * ICMP fragmentation is illegal.
540 *
541 * @note: implementation note: MSIZE is 256 bytes (minimal buffer).
542 * We always truncate original payload to 8 bytes required by the RFC,
543 * so the largest possible datagram is 14 (ethernet) + 20 (ip) +
544 * 8 (icmp) + 60 (max original ip with options) + 8 (original payload)
545 * = 110 bytes which fits into sinlge mbuf.
546 *
547 * @note This function will free msrc!
548 */
549
550void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
551{
552 unsigned ohlen, olen;
553 struct mbuf *m;
554 struct ip *oip, *ip;
555 struct icmp *icp;
556 void *payload;
557
558 LogFlow(("icmp_error: msrc = %p, msrc_len = %d\n",
559 (void *)msrc, msrc ? msrc->m_len : 0));
560
561 if (RT_UNLIKELY(msrc == NULL))
562 goto end_error;
563
564 M_ASSERTPKTHDR(msrc);
565
566 if ( type != ICMP_UNREACH
567 && type != ICMP_TIMXCEED
568 && type != ICMP_SOURCEQUENCH)
569 goto end_error;
570
571 oip = mtod(msrc, struct ip *);
572 LogFunc(("msrc: %RTnaipv4 -> %RTnaipv4\n", oip->ip_src, oip->ip_dst));
573
574 if (oip->ip_src.s_addr == INADDR_ANY)
575 goto end_error;
576
577 if (oip->ip_off & IP_OFFMASK)
578 goto end_error; /* Only reply to fragment 0 */
579
580 ohlen = oip->ip_hl * 4;
581 AssertStmt(ohlen >= sizeof(struct ip), goto end_error);
582
583 olen = oip->ip_len;
584 AssertStmt(olen >= ohlen, goto end_error);
585
586 if (oip->ip_p == IPPROTO_ICMP)
587 {
588 struct icmp *oicp = (struct icmp *)((char *)oip + ohlen);
589 /*
590 * Assume any unknown ICMP type is an error. This isn't
591 * specified by the RFC, but think about it..
592 */
593 if (oicp->icmp_type > ICMP_MAXTYPE || icmp_flush[oicp->icmp_type])
594 goto end_error;
595 }
596
597 /* undo byte order conversions done in ip_input() */
598 HTONS(oip->ip_len);
599 HTONS(oip->ip_id);
600 HTONS(oip->ip_off);
601
602 m = m_gethdr(pData, M_NOWAIT, MT_HEADER);
603 if (RT_UNLIKELY(m == NULL))
604 goto end_error;
605
606 m->m_flags |= M_SKIP_FIREWALL;
607 m->m_data += if_maxlinkhdr;
608
609 ip = mtod(m, struct ip *);
610 m->m_pkthdr.header = (void *)ip;
611
612 /* fill in ip (ip_output0() does the boilerplate for us) */
613 ip->ip_tos = ((oip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
614 /* ip->ip_len will be set later */
615 ip->ip_off = 0;
616 ip->ip_ttl = MAXTTL;
617 ip->ip_p = IPPROTO_ICMP;
618 ip->ip_src = alias_addr;
619 ip->ip_dst = oip->ip_src;
620
621 /* fill in icmp */
622 icp = (struct icmp *)((char *)ip + sizeof(*ip));
623 icp->icmp_type = type;
624 icp->icmp_code = code;
625 icp->icmp_id = 0;
626 icp->icmp_seq = 0;
627
628 /* fill in icmp payload: original ip header plus 8 bytes of its payload */
629 if (olen > ohlen + 8)
630 olen = ohlen + 8;
631 payload = (void *)((char *)icp + ICMP_MINLEN);
632 memcpy(payload, oip, olen);
633
634 /*
635 * Original code appended this message after the payload. This
636 * might have been a good idea for real slirp, as it provided a
637 * communication channel with the remote host. But 90s are over.
638 */
639 NOREF(message);
640
641 /* hide ip header for icmp checksum calculation */
642 m->m_data += sizeof(struct ip);
643 m->m_len = ICMP_MINLEN + /* truncated */ olen;
644
645 icp->icmp_cksum = 0;
646 icp->icmp_cksum = cksum(m, m->m_len);
647
648 /* reveal ip header */
649 m->m_data -= sizeof(struct ip);
650 m->m_len += sizeof(struct ip);
651 ip->ip_len = m->m_len;
652
653 (void) ip_output0(pData, (struct socket *)NULL, m, 1);
654
655 icmpstat.icps_reflect++;
656
657 /* clear source datagramm in positive branch */
658 m_freem(pData, msrc);
659 LogFlowFuncLeave();
660 return;
661
662end_error:
663
664 /*
665 * clear source datagramm in case if some of requirement haven't been met.
666 */
667 if (msrc)
668 m_freem(pData, msrc);
669
670 {
671 static bool fIcmpErrorReported;
672 if (!fIcmpErrorReported)
673 {
674 LogRel(("NAT: error occurred while sending ICMP error message\n"));
675 fIcmpErrorReported = true;
676 }
677 }
678 LogFlowFuncLeave();
679}
680
681/*
682 * Reflect the ip packet back to the source
683 * Note: m isn't duplicated by this method and more delivered to ip_output then.
684 */
685void
686icmp_reflect(PNATState pData, struct mbuf *m)
687{
688 register struct ip *ip = mtod(m, struct ip *);
689 int hlen = ip->ip_hl << 2;
690 register struct icmp *icp;
691 LogFlowFunc(("ENTER: m:%p\n", m));
692
693 /*
694 * Send an icmp packet back to the ip level,
695 * after supplying a checksum.
696 */
697 m->m_data += hlen;
698 m->m_len -= hlen;
699 icp = mtod(m, struct icmp *);
700
701 icp->icmp_cksum = 0;
702 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
703
704 m->m_data -= hlen;
705 m->m_len += hlen;
706
707 (void) ip_output(pData, (struct socket *)NULL, m);
708
709 icmpstat.icps_reflect++;
710 LogFlowFuncLeave();
711}
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