VirtualBox

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

Last change on this file since 63012 was 63012, checked in by vboxsync, 9 years ago

slirp: warnings

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