VirtualBox

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

Last change on this file since 95573 was 95573, checked in by vboxsync, 3 years ago

Network/slirp: Advertising clause for Danny Gasparovsky was unintentional, should have always been 3-clause BSD. Replace 4-clause BSD license by 3-clause, see retroactive license change by UC Berkeley https://www.freebsd.org/copyright/license/

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 KB
Line 
1/* $Id: ip_icmp.c 95573 2022-07-08 18:16:35Z vboxsync $ */
2/** @file
3 * NAT - IP/ICMP handling.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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. Neither the name of the University nor the names of its contributors
33 * may be used to endorse or promote products derived from this software
34 * without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
49 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
50 */
51
52#include "slirp.h"
53#include "ip_icmp.h"
54
55#ifdef VBOX_RAWSOCK_DEBUG_HELPER
56int getrawsock(int type);
57#endif
58
59
60/* The message sent when emulating PING */
61/* Be nice and tell them it's just a psuedo-ping packet */
62#if 0 /* unused */
63static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
64#endif
65
66/* list of actions for icmp_error() on RX of an icmp message */
67static const int icmp_flush[19] =
68{
69/* ECHO REPLY (0) */ 0,
70 1,
71 1,
72/* DEST UNREACH (3) */ 1,
73/* SOURCE QUENCH (4)*/ 1,
74/* REDIRECT (5) */ 1,
75 1,
76 1,
77/* ECHO (8) */ 0,
78/* ROUTERADVERT (9) */ 1,
79/* ROUTERSOLICIT (10) */ 1,
80/* TIME EXCEEDED (11) */ 1,
81/* PARAMETER PROBLEM (12) */ 1,
82/* TIMESTAMP (13) */ 0,
83/* TIMESTAMP REPLY (14) */ 0,
84/* INFO (15) */ 0,
85/* INFO REPLY (16) */ 0,
86/* ADDR MASK (17) */ 0,
87/* ADDR MASK REPLY (18) */ 0
88};
89
90
91int
92icmp_init(PNATState pData, int iIcmpCacheLimit)
93{
94 pData->icmp_socket.so_type = IPPROTO_ICMP;
95 pData->icmp_socket.so_state = SS_ISFCONNECTED;
96
97#ifndef RT_OS_WINDOWS
98 TAILQ_INIT(&pData->icmp_msg_head);
99
100 if (iIcmpCacheLimit < 0)
101 {
102 LogRel(("NAT: iIcmpCacheLimit is invalid %d, will be alter to default value 100\n", iIcmpCacheLimit));
103 iIcmpCacheLimit = 100;
104 }
105 pData->iIcmpCacheLimit = iIcmpCacheLimit;
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# if defined(RT_OS_DARWIN) || !defined(VBOX_RAWSOCK_DEBUG_HELPER)
115 LogRel(("NAT: ICMP/ping not available (could not open ICMP socket, error %Rrc)\n", rc));
116 return 1;
117# else
118 /* try to get it from privileged helper */
119 LogRel(("NAT: ICMP/ping raw socket error %Rrc, asking helper...\n", rc));
120 pData->icmp_socket.s = getrawsock(AF_INET);
121 if (pData->icmp_socket.s == -1)
122 {
123 LogRel(("NAT: ICMP/ping not available\n"));
124 return 1;
125 }
126# endif /* !RT_OS_DARWIN && VBOX_RAWSOCK_DEBUG_HELPER */
127 }
128 fd_nonblock(pData->icmp_socket.s);
129 NSOCK_INC();
130
131#else /* RT_OS_WINDOWS */
132 RT_NOREF(iIcmpCacheLimit);
133
134 if (icmpwin_init(pData) != 0)
135 return 1;
136#endif /* RT_OS_WINDOWS */
137
138 return 0;
139}
140
141/**
142 * Cleans ICMP cache.
143 */
144void
145icmp_finit(PNATState pData)
146{
147#ifdef RT_OS_WINDOWS
148 icmpwin_finit(pData);
149#else
150 while (!TAILQ_EMPTY(&pData->icmp_msg_head))
151 {
152 struct icmp_msg *icm = TAILQ_FIRST(&pData->icmp_msg_head);
153 icmp_msg_delete(pData, icm);
154 }
155 closesocket(pData->icmp_socket.s);
156#endif
157}
158
159
160#if !defined(RT_OS_WINDOWS)
161static struct icmp_msg *
162icmp_msg_alloc(PNATState pData)
163{
164 struct icmp_msg *icm;
165
166#ifdef DEBUG
167 {
168 int iTally = 0;
169 TAILQ_FOREACH(icm, &pData->icmp_msg_head, im_queue)
170 ++iTally;
171 Assert(pData->cIcmpCacheSize == iTally);
172 }
173#endif
174
175 if (pData->cIcmpCacheSize >= pData->iIcmpCacheLimit)
176 {
177 int cTargetCacheSize = pData->iIcmpCacheLimit/2;
178
179 while (pData->cIcmpCacheSize > cTargetCacheSize)
180 {
181 icm = TAILQ_FIRST(&pData->icmp_msg_head);
182 icmp_msg_delete(pData, icm);
183 }
184 }
185
186 icm = RTMemAlloc(sizeof(struct icmp_msg));
187 if (RT_UNLIKELY(icm == NULL))
188 return NULL;
189
190 TAILQ_INSERT_TAIL(&pData->icmp_msg_head, icm, im_queue);
191 pData->cIcmpCacheSize++;
192
193 return icm;
194}
195
196
197static void
198icmp_attach(PNATState pData, struct mbuf *m)
199{
200 struct icmp_msg *icm;
201
202#ifdef DEBUG
203 {
204 /* only used for ping */
205 struct ip *ip = mtod(m, struct ip *);
206 Assert(ip->ip_p == IPPROTO_ICMP);
207 }
208#endif
209
210 icm = icmp_msg_alloc(pData);
211 if (RT_UNLIKELY(icm == NULL))
212 return;
213
214 icm->im_so = &pData->icmp_socket;
215 icm->im_m = m;
216}
217
218
219void
220icmp_msg_delete(PNATState pData, struct icmp_msg *icm)
221{
222 if (RT_UNLIKELY(icm == NULL))
223 return;
224
225#ifdef DEBUG
226 {
227 struct icmp_msg *existing;
228 int iTally = 0;
229
230 TAILQ_FOREACH(existing, &pData->icmp_msg_head, im_queue)
231 ++iTally;
232 Assert(pData->cIcmpCacheSize == iTally);
233
234 Assert(pData->cIcmpCacheSize > 0);
235 TAILQ_FOREACH(existing, &pData->icmp_msg_head, im_queue)
236 {
237 if (existing == icm)
238 break;
239 }
240 Assert(existing != NULL);
241 }
242#endif
243
244 TAILQ_REMOVE(&pData->icmp_msg_head, icm, im_queue);
245 pData->cIcmpCacheSize--;
246
247 icm->im_so->so_m = NULL;
248 if (icm->im_m != NULL)
249 m_freem(pData, icm->im_m);
250
251 RTMemFree(icm);
252}
253
254
255/*
256 * ip here is ip header + 64bytes readed from ICMP packet
257 */
258struct icmp_msg *
259icmp_find_original_mbuf(PNATState pData, struct ip *ip)
260{
261 struct mbuf *m0;
262 struct ip *ip0;
263 struct icmp *icp, *icp0;
264 struct icmp_msg *icm = NULL;
265 int found = 0;
266 struct udphdr *udp;
267 struct tcphdr *tcp;
268 struct socket *head_socket = NULL;
269 struct socket *last_socket = NULL;
270 struct socket *so = NULL;
271 struct in_addr faddr;
272 u_short lport, fport;
273
274 faddr.s_addr = ~0;
275
276 lport = ~0;
277 fport = ~0;
278
279
280 LogFlowFunc(("ENTER: ip->ip_p:%d\n", ip->ip_p));
281 switch (ip->ip_p)
282 {
283 case IPPROTO_ICMP:
284 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
285 TAILQ_FOREACH(icm, &pData->icmp_msg_head, im_queue)
286 {
287 m0 = icm->im_m;
288 ip0 = mtod(m0, struct ip *);
289 if (ip0->ip_p != IPPROTO_ICMP)
290 {
291 /* try next item */
292 continue;
293 }
294 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
295 /*
296 * IP could pointer to ICMP_REPLY datagram (1)
297 * or pointer IP header in ICMP payload in case of
298 * ICMP_TIMXCEED or ICMP_UNREACH (2)
299 *
300 * if (1) and then ICMP (type should be ICMP_ECHOREPLY) and we need check that
301 * IP.IP_SRC == IP0.IP_DST received datagramm comes from destination.
302 *
303 * if (2) then check that payload ICMP has got type ICMP_ECHO and
304 * IP.IP_DST == IP0.IP_DST destination of returned datagram is the same as
305 * one was sent.
306 */
307 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
308 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
309 && icp->icmp_id == icp0->icmp_id
310 && icp->icmp_seq == icp0->icmp_seq)
311 {
312 found = 1;
313 Log(("Have found %R[natsock]\n", icm->im_so));
314 break;
315 }
316 Log(("Have found nothing\n"));
317 }
318 break;
319
320 /*
321 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
322 * from which the IP package has been sent.
323 */
324 case IPPROTO_UDP:
325 head_socket = &udb;
326 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
327 faddr.s_addr = ip->ip_dst.s_addr;
328 fport = udp->uh_dport;
329 lport = udp->uh_sport;
330 last_socket = udp_last_so;
331 RT_FALL_THRU();
332
333 case IPPROTO_TCP:
334 if (head_socket == NULL)
335 {
336 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
337 head_socket = &tcb; /* head_socket could be initialized with udb*/
338 faddr.s_addr = ip->ip_dst.s_addr;
339 fport = tcp->th_dport;
340 lport = tcp->th_sport;
341 last_socket = tcp_last_so;
342 }
343 /* check last socket first */
344 if ( last_socket->so_faddr.s_addr == faddr.s_addr
345 && last_socket->so_fport == fport
346 && last_socket->so_hlport == lport)
347 {
348 found = 1;
349 so = last_socket;
350 break;
351 }
352 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
353 {
354 /* Should be replaced by hash here */
355 Log(("trying:%R[natsock] against %RTnaipv4:%d lport=%d hlport=%d\n",
356 so, faddr.s_addr, ntohs(fport), ntohs(lport), ntohs(so->so_hlport)));
357 if ( so->so_faddr.s_addr == faddr.s_addr
358 && so->so_fport == fport
359 && so->so_hlport == lport)
360 {
361 found = 1;
362 break;
363 }
364 }
365 break;
366
367 default:
368 Log(("NAT:ICMP: unsupported protocol(%d)\n", ip->ip_p));
369 }
370
371#ifdef DEBUG
372 if (found)
373 Assert((icm != NULL) ^ (so != NULL));
374#endif
375
376 if (found && icm == NULL)
377 {
378 /*
379 * XXX: Implies this is not a pong, found socket. This is, of
380 * course, wasteful since the caller will delete icmp_msg
381 * immediately after processing, so there's not much reason to
382 * clutter up the queue with it.
383 */
384 AssertReturn(so != NULL, NULL);
385
386 /*
387 * XXX: FIXME: If the very first send(2) fails, the socket is
388 * still in SS_NOFDREF and so we will not report this too.
389 */
390 if (so->so_state == SS_NOFDREF)
391 {
392 /* socket is shutting down we've already sent ICMP on it. */
393 Log(("NAT:ICMP: disconnected %R[natsock]\n", so));
394 LogFlowFunc(("LEAVE: icm:NULL\n"));
395 return NULL;
396 }
397
398 if (so->so_m == NULL)
399 {
400 Log(("NAT:ICMP: no saved mbuf for %R[natsock]\n", so));
401 LogFlowFunc(("LEAVE: icm:NULL\n"));
402 return NULL;
403 }
404
405 icm = icmp_msg_alloc(pData);
406 if (RT_UNLIKELY(icm == NULL))
407 {
408 LogFlowFunc(("LEAVE: icm:NULL\n"));
409 return NULL;
410 }
411
412 Log(("NAT:ICMP: for %R[natsock]\n", so));
413 icm->im_so = so;
414 icm->im_m = so->so_m;
415 }
416 LogFlowFunc(("LEAVE: icm:%p\n", icm));
417 return icm;
418}
419#endif /* !RT_OS_WINDOWS */
420
421
422/*
423 * Process a received ICMP message.
424 */
425void
426icmp_input(PNATState pData, struct mbuf *m, int hlen)
427{
428 register struct ip *ip = mtod(m, struct ip *);
429 int icmplen = ip->ip_len;
430 uint8_t icmp_type;
431 void *icp_buf = NULL;
432 uint32_t dst;
433
434 /* int code; */
435
436 LogFlowFunc(("ENTER: m = %p, m_len = %d\n", m, m ? m->m_len : 0));
437
438 icmpstat.icps_received++;
439
440 /*
441 * Locate icmp structure in mbuf, and check
442 * that its not corrupted and of at least minimum length.
443 */
444 if (icmplen < ICMP_MINLEN)
445 {
446 /* min 8 bytes payload */
447 icmpstat.icps_tooshort++;
448 goto end_error_free_m;
449 }
450
451 m->m_len -= hlen;
452 m->m_data += hlen;
453
454 if (cksum(m, icmplen))
455 {
456 icmpstat.icps_checksum++;
457 goto end_error_free_m;
458 }
459
460 /* are we guaranteed to have ICMP header in first mbuf? be safe. */
461 m_copydata(m, 0, sizeof(icmp_type), (caddr_t)&icmp_type);
462
463 m->m_len += hlen;
464 m->m_data -= hlen;
465
466 /* icmpstat.icps_inhist[icp->icmp_type]++; */
467 /* code = icp->icmp_code; */
468
469 LogFlow(("icmp_type = %d\n", icmp_type));
470 switch (icmp_type)
471 {
472 case ICMP_ECHO:
473 ip->ip_len += hlen; /* since ip_input subtracts this */
474 dst = ip->ip_dst.s_addr;
475 if ( CTL_CHECK(dst, CTL_ALIAS)
476 || CTL_CHECK(dst, CTL_DNS)
477 || CTL_CHECK(dst, CTL_TFTP))
478 {
479 /* Don't reply to ping requests for the hosts loopback interface if it is disabled. */
480 if ( CTL_CHECK(dst, CTL_ALIAS)
481 && !pData->fLocalhostReachable)
482 goto done;
483
484 uint8_t echo_reply = ICMP_ECHOREPLY;
485 m_copyback(pData, m, hlen + RT_OFFSETOF(struct icmp, icmp_type),
486 sizeof(echo_reply), (caddr_t)&echo_reply);
487 ip->ip_dst.s_addr = ip->ip_src.s_addr;
488 ip->ip_src.s_addr = dst;
489 icmp_reflect(pData, m);
490 m = NULL; /* m was consumed and freed */
491 goto done;
492 }
493
494#ifdef RT_OS_WINDOWS
495 {
496 icmpwin_ping(pData, m, hlen);
497 break; /* free mbuf */
498 }
499#else
500 {
501 struct icmp *icp;
502 struct sockaddr_in addr;
503
504 /* XXX: FIXME: this is bogus, see CTL_CHECKs above */
505 addr.sin_family = AF_INET;
506 if ((ip->ip_dst.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
507 {
508 /* It's an alias */
509 switch (RT_N2H_U32(ip->ip_dst.s_addr) & ~pData->netmask)
510 {
511 case CTL_DNS:
512 case CTL_ALIAS:
513 default:
514 addr.sin_addr = loopback_addr;
515 break;
516 }
517 }
518 else
519 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
520
521 if (m->m_next)
522 {
523 icp_buf = RTMemAlloc(icmplen);
524 if (!icp_buf)
525 {
526 Log(("NAT: not enought memory to allocate the buffer\n"));
527 goto end_error_free_m;
528 }
529 m_copydata(m, hlen, icmplen, icp_buf);
530 icp = (struct icmp *)icp_buf;
531 }
532 else
533 icp = (struct icmp *)(mtod(m, char *) + hlen);
534
535 if (pData->icmp_socket.s != -1)
536 {
537 static bool fIcmpSocketErrorReported;
538 int ttl;
539 int status;
540 ssize_t rc;
541
542 ttl = ip->ip_ttl;
543 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
544 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
545 (void *)&ttl, sizeof(ttl));
546 if (status < 0)
547 Log(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
548 strerror(errno)));
549 rc = sendto(pData->icmp_socket.s, icp, icmplen, 0,
550 (struct sockaddr *)&addr, sizeof(addr));
551 if (rc >= 0)
552 {
553 icmp_attach(pData, m);
554 m = NULL; /* m was stashed away for safekeeping */
555 goto done;
556 }
557
558
559 if (!fIcmpSocketErrorReported)
560 {
561 LogRel(("NAT: icmp_input udp sendto tx errno = %d (%s)\n",
562 errno, strerror(errno)));
563 fIcmpSocketErrorReported = true;
564 }
565 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
566 m = NULL; /* m was consumed and freed */
567 goto done;
568 }
569 }
570#endif /* !RT_OS_WINDOWS */
571 break;
572 case ICMP_UNREACH:
573 case ICMP_TIMXCEED:
574 /* @todo(vvl): both up cases comes from guest,
575 * indeed right solution would be find the socket
576 * corresponding to ICMP data and close it.
577 */
578 case ICMP_PARAMPROB:
579 case ICMP_SOURCEQUENCH:
580 case ICMP_TSTAMP:
581 case ICMP_MASKREQ:
582 case ICMP_REDIRECT:
583 icmpstat.icps_notsupp++;
584 break;
585
586 default:
587 icmpstat.icps_badtype++;
588 } /* switch */
589
590end_error_free_m:
591 if (m != NULL)
592 m_freem(pData, m);
593
594done:
595 if (icp_buf)
596 RTMemFree(icp_buf);
597}
598
599
600/**
601 * Send an ICMP message in response to a situation
602 *
603 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
604 * MUST NOT change this header information.
605 * MUST NOT reply to a multicast/broadcast IP address.
606 * MUST NOT reply to a multicast/broadcast MAC address.
607 * MUST reply to only the first fragment.
608 *
609 * Send ICMP_UNREACH back to the source regarding msrc.
610 * It is reported as the bad ip packet. The header should
611 * be fully correct and in host byte order.
612 * ICMP fragmentation is illegal.
613 *
614 * @note: implementation note: MSIZE is 256 bytes (minimal buffer).
615 * We always truncate original payload to 8 bytes required by the RFC,
616 * so the largest possible datagram is 14 (ethernet) + 20 (ip) +
617 * 8 (icmp) + 60 (max original ip with options) + 8 (original payload)
618 * = 110 bytes which fits into sinlge mbuf.
619 *
620 * @note This function will free msrc!
621 */
622
623void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
624{
625 unsigned ohlen, olen;
626 struct mbuf *m;
627 struct ip *oip, *ip;
628 struct icmp *icp;
629 void *payload;
630 RT_NOREF(minsize);
631
632 LogFlow(("icmp_error: msrc = %p, msrc_len = %d\n",
633 (void *)msrc, msrc ? msrc->m_len : 0));
634
635 if (RT_UNLIKELY(msrc == NULL))
636 goto end_error;
637
638 M_ASSERTPKTHDR(msrc);
639
640 if ( type != ICMP_UNREACH
641 && type != ICMP_TIMXCEED
642 && type != ICMP_SOURCEQUENCH)
643 goto end_error;
644
645 oip = mtod(msrc, struct ip *);
646 LogFunc(("msrc: %RTnaipv4 -> %RTnaipv4\n", oip->ip_src, oip->ip_dst));
647
648 if (oip->ip_src.s_addr == INADDR_ANY)
649 goto end_error;
650
651 if (oip->ip_off & IP_OFFMASK)
652 goto end_error; /* Only reply to fragment 0 */
653
654 ohlen = oip->ip_hl * 4;
655 AssertStmt(ohlen >= sizeof(struct ip), goto end_error);
656
657 olen = oip->ip_len;
658 AssertStmt(olen >= ohlen, goto end_error);
659
660 if (oip->ip_p == IPPROTO_ICMP)
661 {
662 struct icmp *oicp = (struct icmp *)((char *)oip + ohlen);
663 /*
664 * Assume any unknown ICMP type is an error. This isn't
665 * specified by the RFC, but think about it..
666 */
667 if (oicp->icmp_type > ICMP_MAXTYPE || icmp_flush[oicp->icmp_type])
668 goto end_error;
669 }
670
671 /* undo byte order conversions done in ip_input() */
672 HTONS(oip->ip_len);
673 HTONS(oip->ip_id);
674 HTONS(oip->ip_off);
675
676 m = m_gethdr(pData, M_NOWAIT, MT_HEADER);
677 if (RT_UNLIKELY(m == NULL))
678 goto end_error;
679
680 m->m_flags |= M_SKIP_FIREWALL;
681 m->m_data += if_maxlinkhdr;
682
683 ip = mtod(m, struct ip *);
684 m->m_pkthdr.header = (void *)ip;
685
686 /* fill in ip (ip_output0() does the boilerplate for us) */
687 ip->ip_tos = ((oip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
688 /* ip->ip_len will be set later */
689 ip->ip_off = 0;
690 ip->ip_ttl = MAXTTL;
691 ip->ip_p = IPPROTO_ICMP;
692 ip->ip_src = alias_addr;
693 ip->ip_dst = oip->ip_src;
694
695 /* fill in icmp */
696 icp = (struct icmp *)((char *)ip + sizeof(*ip));
697 icp->icmp_type = type;
698 icp->icmp_code = code;
699 icp->icmp_id = 0;
700 icp->icmp_seq = 0;
701
702 /* fill in icmp payload: original ip header plus 8 bytes of its payload */
703 if (olen > ohlen + 8)
704 olen = ohlen + 8;
705 payload = (void *)((char *)icp + ICMP_MINLEN);
706 memcpy(payload, oip, olen);
707
708 /*
709 * Original code appended this message after the payload. This
710 * might have been a good idea for real slirp, as it provided a
711 * communication channel with the remote host. But 90s are over.
712 */
713 NOREF(message);
714
715 /* hide ip header for icmp checksum calculation */
716 m->m_data += sizeof(struct ip);
717 m->m_len = ICMP_MINLEN + /* truncated */ olen;
718
719 icp->icmp_cksum = 0;
720 icp->icmp_cksum = cksum(m, m->m_len);
721
722 /* reveal ip header */
723 m->m_data -= sizeof(struct ip);
724 m->m_len += sizeof(struct ip);
725 ip->ip_len = m->m_len;
726
727 (void) ip_output0(pData, (struct socket *)NULL, m, 1);
728
729 icmpstat.icps_reflect++;
730
731 /* clear source datagramm in positive branch */
732 m_freem(pData, msrc);
733 LogFlowFuncLeave();
734 return;
735
736end_error:
737
738 /*
739 * clear source datagramm in case if some of requirement haven't been met.
740 */
741 if (msrc)
742 m_freem(pData, msrc);
743
744 {
745 static bool fIcmpErrorReported;
746 if (!fIcmpErrorReported)
747 {
748 LogRel(("NAT: Error occurred while sending ICMP error message\n"));
749 fIcmpErrorReported = true;
750 }
751 }
752 LogFlowFuncLeave();
753}
754
755/*
756 * Reflect the ip packet back to the source
757 * Note: m isn't duplicated by this method and more delivered to ip_output then.
758 */
759void
760icmp_reflect(PNATState pData, struct mbuf *m)
761{
762 register struct ip *ip = mtod(m, struct ip *);
763 int hlen = ip->ip_hl << 2;
764 register struct icmp *icp;
765 LogFlowFunc(("ENTER: m:%p\n", m));
766
767 /*
768 * Send an icmp packet back to the ip level,
769 * after supplying a checksum.
770 */
771 m->m_data += hlen;
772 m->m_len -= hlen;
773 icp = mtod(m, struct icmp *);
774
775 icp->icmp_cksum = 0;
776 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
777
778 m->m_data -= hlen;
779 m->m_len += hlen;
780
781 (void) ip_output(pData, (struct socket *)NULL, m);
782
783 icmpstat.icps_reflect++;
784 LogFlowFuncLeave();
785}
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