VirtualBox

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

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

Devices: Updated (C) year.

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