VirtualBox

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

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

NAT: don't create contiguous copy of all large ICMP datagrams, only do
one when necessary.

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