VirtualBox

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

Last change on this file since 30401 was 30401, checked in by vboxsync, 15 years ago

NAT: assign m_so before icmp_attach.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.6 KB
Line 
1/* $Id: ip_icmp.c 30401 2010-06-23 16:41:07Z vboxsync $ */
2/** @file
3 * NAT - IP/ICMP handling.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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#endif
62
63#ifdef RT_OS_WINDOWS
64# define ICMP_SEND_ECHO(event, routine, addr, data, datasize, ipopt) \
65 IcmpSendEcho2(pData->icmp_socket.sh, (event), NULL, NULL, (addr), (data), (datasize), \
66 (ipopt), pData->pvIcmpBuffer, pData->szIcmpBuffer, 1)
67#endif /* RT_OS_WINDOWS */
68
69/* The message sent when emulating PING */
70/* Be nice and tell them it's just a psuedo-ping packet */
71static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
72
73/* list of actions for icmp_error() on RX of an icmp message */
74static const int icmp_flush[19] =
75{
76/* ECHO REPLY (0) */ 0,
77 1,
78 1,
79/* DEST UNREACH (3) */ 1,
80/* SOURCE QUENCH (4)*/ 1,
81/* REDIRECT (5) */ 1,
82 1,
83 1,
84/* ECHO (8) */ 0,
85/* ROUTERADVERT (9) */ 1,
86/* ROUTERSOLICIT (10) */ 1,
87/* TIME EXCEEDED (11) */ 1,
88/* PARAMETER PROBLEM (12) */ 1,
89/* TIMESTAMP (13) */ 0,
90/* TIMESTAMP REPLY (14) */ 0,
91/* INFO (15) */ 0,
92/* INFO REPLY (16) */ 0,
93/* ADDR MASK (17) */ 0,
94/* ADDR MASK REPLY (18) */ 0
95};
96
97int
98icmp_init(PNATState pData)
99{
100 pData->icmp_socket.so_type = IPPROTO_ICMP;
101 pData->icmp_socket.so_state = SS_ISFCONNECTED;
102#ifndef RT_OS_WINDOWS
103# ifndef RT_OS_DARWIN
104 pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
105# else /* !RT_OS_DARWIN */
106 pData->icmp_socket.s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
107# endif /* RT_OS_DARWIN */
108 if (pData->icmp_socket.s == -1)
109 {
110 int rc = RTErrConvertFromErrno(errno);
111 LogRel(("NAT: ICMP/ping not available (could not open ICMP socket, error %Rrc)\n", rc));
112 return 1;
113 }
114 fd_nonblock(pData->icmp_socket.s);
115 NSOCK_INC();
116#else /* RT_OS_WINDOWS */
117 pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
118 if (pData->hmIcmpLibrary != NULL)
119 {
120 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
121 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
122 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
123 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
124 pData->pfGetAdaptersAddresses = (ULONG (WINAPI *)(ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG))
125 GetProcAddress(pData->hmIcmpLibrary, "GetAdaptersAddresses");
126 if (pData->pfGetAdaptersAddresses == NULL)
127 {
128 LogRel(("NAT: Can't find GetAdapterAddresses in Iphlpapi.dll"));
129 }
130 }
131
132 if (pData->pfIcmpParseReplies == NULL)
133 {
134 if(pData->pfGetAdaptersAddresses == NULL)
135 FreeLibrary(pData->hmIcmpLibrary);
136 pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
137 if (pData->hmIcmpLibrary == NULL)
138 {
139 LogRel(("NAT: Icmp.dll could not be loaded\n"));
140 return 1;
141 }
142 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
143 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
144 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
145 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
146 }
147 if (pData->pfIcmpParseReplies == NULL)
148 {
149 LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
150 FreeLibrary(pData->hmIcmpLibrary);
151 return 1;
152 }
153 if (pData->pfIcmpCloseHandle == NULL)
154 {
155 LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
156 FreeLibrary(pData->hmIcmpLibrary);
157 return 1;
158 }
159 pData->icmp_socket.sh = IcmpCreateFile();
160 pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
161 pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
162 pData->pvIcmpBuffer = RTMemAlloc(pData->szIcmpBuffer);
163#endif /* RT_OS_WINDOWS */
164 LIST_INIT(&pData->icmp_msg_head);
165 return 0;
166}
167
168/*
169 * ip here is ip header + 64bytes readed from ICMP packet
170 */
171struct icmp_msg *
172icmp_find_original_mbuf(PNATState pData, struct ip *ip)
173{
174 struct mbuf *m0;
175 struct ip *ip0;
176 struct icmp *icp, *icp0;
177 struct icmp_msg *icm = NULL;
178 int found = 0;
179 struct udphdr *udp;
180 struct tcphdr *tcp;
181 struct socket *head_socket = NULL;
182 struct socket *last_socket = NULL;
183 struct socket *so = NULL;
184 struct in_addr laddr, faddr;
185 u_short lport, fport;
186
187 laddr.s_addr = ~0;
188 faddr.s_addr = ~0;
189
190 lport = ~0;
191 fport = ~0;
192
193
194 Log(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
195 switch (ip->ip_p)
196 {
197 case IPPROTO_ICMP:
198 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
199 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
200 {
201 m0 = icm->im_m;
202 ip0 = mtod(m0, struct ip *);
203 if (ip0->ip_p != IPPROTO_ICMP)
204 {
205 /* try next item */
206 continue;
207 }
208 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
209 /*
210 * IP could pointer to ICMP_REPLY datagram (1)
211 * or pointer IP header in ICMP payload in case of
212 * ICMP_TIMXCEED or ICMP_UNREACH (2)
213 *
214 * if (1) and then ICMP (type should be ICMP_ECHOREPLY) and we need check that
215 * IP.IP_SRC == IP0.IP_DST received datagramm comes from destination.
216 *
217 * if (2) then check that payload ICMP has got type ICMP_ECHO and
218 * IP.IP_DST == IP0.IP_DST destination of returned datagram is the same as
219 * one was sent.
220 */
221 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
222 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
223 && icp->icmp_id == icp0->icmp_id
224 && icp->icmp_seq == icp0->icmp_seq)
225 {
226 found = 1;
227 Log(("Have found %R[natsock]\n", icm->im_so));
228 break;
229 }
230 Log(("Have found nothing\n"));
231 }
232 break;
233
234 /*
235 * for TCP and UDP logic little bit reverted, we try to find the HOST socket
236 * from which the IP package has been sent.
237 */
238 case IPPROTO_UDP:
239 head_socket = &udb;
240 udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
241 faddr.s_addr = ip->ip_dst.s_addr;
242 fport = udp->uh_dport;
243 laddr.s_addr = ip->ip_src.s_addr;
244 lport = udp->uh_sport;
245 last_socket = udp_last_so;
246 /* fall through */
247
248 case IPPROTO_TCP:
249 if (head_socket == NULL)
250 {
251 tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
252 head_socket = &tcb; /* head_socket could be initialized with udb*/
253 faddr.s_addr = ip->ip_dst.s_addr;
254 fport = tcp->th_dport;
255 laddr.s_addr = ip->ip_src.s_addr;
256 lport = tcp->th_sport;
257 last_socket = tcp_last_so;
258 }
259 /* check last socket first */
260 if ( last_socket->so_faddr.s_addr == faddr.s_addr
261 && last_socket->so_fport == fport
262 && last_socket->so_hlport == lport)
263 {
264 found = 1;
265 so = last_socket;
266 goto sofound;
267 }
268 for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
269 {
270 /* Should be reaplaced by hash here */
271 Log(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
272 if ( so->so_faddr.s_addr == faddr.s_addr
273 && so->so_fport == fport
274 && so->so_hlport == lport)
275 {
276 found = 1;
277 break;
278 }
279 }
280 break;
281
282 default:
283 LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
284 }
285 sofound:
286 if (found == 1 && icm == NULL)
287 {
288 if (so->so_state == SS_NOFDREF)
289 {
290 /* socket is shutdowning we've already sent ICMP on it.*/
291 LogRel(("NAT: Received icmp on shutdowning socket (probably corresponding ICMP socket has been already sent)\n"));
292 return NULL;
293 }
294 icm = RTMemAlloc(sizeof(struct icmp_msg));
295 icm->im_m = so->so_m;
296 icm->im_so = so;
297 found = 1;
298 Log(("hit:%R[natsock]\n", so));
299 /*XXX: this storage not very long,
300 * better add flag if it should removed from lis
301 */
302 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
303 return (icm);
304 }
305 if (found == 1)
306 return icm;
307
308 return NULL;
309}
310
311static int
312icmp_attach(PNATState pData, struct mbuf *m)
313{
314 struct icmp_msg *icm;
315 struct ip *ip;
316 ip = mtod(m, struct ip *);
317 Assert(ip->ip_p == IPPROTO_ICMP);
318 icm = RTMemAlloc(sizeof(struct icmp_msg));
319 icm->im_m = m;
320 icm->im_so = m->m_so;
321 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
322 return 0;
323}
324
325/*
326 * Process a received ICMP message.
327 */
328void
329icmp_input(PNATState pData, struct mbuf *m, int hlen)
330{
331 register struct icmp *icp;
332 int fIcpOnMbuf = 1; /* if icp pointer to m->m_data */
333 register struct ip *ip = mtod(m, struct ip *);
334 int icmplen = ip->ip_len;
335 int status;
336 uint32_t dst;
337#if !defined(RT_OS_WINDOWS)
338 int ttl;
339#endif
340
341 /* int code; */
342
343 DEBUG_CALL("icmp_input");
344 DEBUG_ARG("m = %lx", (long )m);
345 DEBUG_ARG("m_len = %d", m ? m->m_len : 0);
346
347 icmpstat.icps_received++;
348
349 /*
350 * Locate icmp structure in mbuf, and check
351 * that its not corrupted and of at least minimum length.
352 */
353 if (icmplen < ICMP_MINLEN)
354 {
355 /* min 8 bytes payload */
356 icmpstat.icps_tooshort++;
357 goto end_error;
358 }
359
360 m->m_len -= hlen;
361 m->m_data += hlen;
362
363 if (cksum(m, icmplen))
364 {
365 icmpstat.icps_checksum++;
366 goto end_error;
367 }
368
369 if (m->m_next != NULL)
370 {
371 char *buf = RTMemAlloc(icmplen);
372 if (!buf)
373 {
374 LogRel(("NAT: not enought memory to allocate the buffer\n"));
375 goto end_error;
376 }
377 m_copydata(m, 0, icmplen, buf);
378 icp = (struct icmp *)buf;
379 fIcpOnMbuf = 0;
380 }
381 else
382 icp = mtod(m, struct icmp *);
383
384
385 m->m_len += hlen;
386 m->m_data -= hlen;
387
388 /* icmpstat.icps_inhist[icp->icmp_type]++; */
389 /* code = icp->icmp_code; */
390
391 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
392 switch (icp->icmp_type)
393 {
394 case ICMP_ECHO:
395 ip->ip_len += hlen; /* since ip_input subtracts this */
396 dst = ip->ip_dst.s_addr;
397 if (dst == alias_addr.s_addr)
398 {
399 icp->icmp_type = ICMP_ECHOREPLY;
400 ip->ip_dst.s_addr = ip->ip_src.s_addr;
401 ip->ip_src.s_addr = dst;
402 icmp_reflect(pData, m);
403 goto done;
404 }
405 else
406 {
407 struct sockaddr_in addr;
408#ifdef RT_OS_WINDOWS
409 IP_OPTION_INFORMATION ipopt;
410 int error;
411#endif
412 addr.sin_family = AF_INET;
413 if ((ip->ip_dst.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
414 {
415 /* It's an alias */
416 switch (RT_N2H_U32(ip->ip_dst.s_addr) & ~pData->netmask)
417 {
418 case CTL_DNS:
419 case CTL_ALIAS:
420 default:
421 addr.sin_addr = loopback_addr;
422 break;
423 }
424 }
425 else
426 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
427#ifndef RT_OS_WINDOWS
428 if (pData->icmp_socket.s != -1)
429 {
430 ssize_t rc;
431 ttl = ip->ip_ttl;
432 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
433 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
434 (void *)&ttl, sizeof(ttl));
435 if (status < 0)
436 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
437 strerror(errno)));
438 rc = sendto(pData->icmp_socket.s, icp, icmplen, 0,
439 (struct sockaddr *)&addr, sizeof(addr));
440 if (rc >= 0)
441 {
442 m->m_so = &pData->icmp_socket;
443 icmp_attach(pData, m);
444 /* don't let m_freem at the end free atached buffer */
445 goto done;
446 }
447
448 LogRel((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
449 errno, strerror(errno)));
450 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
451 }
452#else /* RT_OS_WINDOWS */
453 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
454 pData->icmp_socket.so_icmp_id = icp->icmp_id;
455 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
456 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
457 ipopt.Ttl = ip->ip_ttl;
458 status = ICMP_SEND_ECHO(pData->phEvents[VBOX_ICMP_EVENT_INDEX], notify_slirp, addr.sin_addr.s_addr,
459 icp->icmp_data, icmplen - ICMP_MINLEN, &ipopt);
460 error = GetLastError();
461 if ( status != 0
462 || error == ERROR_IO_PENDING)
463 {
464 m->m_so = &pData->icmp_socket;
465 icmp_attach(pData, m);
466 /* don't let m_freem at the end free atached buffer */
467 goto done;
468 }
469 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
470 switch (error)
471 {
472 case ERROR_INVALID_PARAMETER:
473 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
474 break;
475 case ERROR_NOT_SUPPORTED:
476 LogRel(("operation is unsupported)\n"));
477 break;
478 case ERROR_NOT_ENOUGH_MEMORY:
479 LogRel(("OOM!!!)\n"));
480 break;
481 case IP_BUF_TOO_SMALL:
482 LogRel(("Buffer too small)\n"));
483 break;
484 default:
485 LogRel(("Other error!!!)\n"));
486 break;
487 }
488#endif /* RT_OS_WINDOWS */
489 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
490 break;
491 case ICMP_UNREACH:
492 case ICMP_TIMXCEED:
493 /* @todo(vvl): both up cases comes from guest,
494 * indeed right solution would be find the socket
495 * corresponding to ICMP data and close it.
496 */
497 case ICMP_PARAMPROB:
498 case ICMP_SOURCEQUENCH:
499 case ICMP_TSTAMP:
500 case ICMP_MASKREQ:
501 case ICMP_REDIRECT:
502 icmpstat.icps_notsupp++;
503 break;
504
505 default:
506 icmpstat.icps_badtype++;
507 } /* switch */
508
509end_error:
510 m_freem(pData, m);
511done:
512 if ( !fIcpOnMbuf
513 && !icp)
514 RTMemFree(icp);
515}
516
517
518/**
519 * Send an ICMP message in response to a situation
520 *
521 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
522 * MUST NOT change this header information.
523 * MUST NOT reply to a multicast/broadcast IP address.
524 * MUST NOT reply to a multicast/broadcast MAC address.
525 * MUST reply to only the first fragment.
526 *
527 * Send ICMP_UNREACH back to the source regarding msrc.
528 * It is reported as the bad ip packet. The header should
529 * be fully correct and in host byte order.
530 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
531 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
532 *
533 * @note This function will NOT free msrc!
534 */
535
536#define ICMP_MAXDATALEN (IP_MSS-28)
537void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
538{
539 unsigned hlen, shlen, s_ip_len;
540 register struct ip *ip;
541 register struct icmp *icp;
542 register struct mbuf *m;
543 int new_m_size = 0;
544 int size = 0;
545
546 DEBUG_CALL("icmp_error");
547 DEBUG_ARG("msrc = %lx", (long )msrc);
548 DEBUG_ARG("msrc_len = %d", msrc ? msrc->m_len : 0);
549 if (msrc != NULL)
550 M_ASSERTPKTHDR(msrc);
551
552 if ( type != ICMP_UNREACH
553 && type != ICMP_TIMXCEED
554 && type != ICMP_SOURCEQUENCH)
555 goto end_error;
556
557 /* check msrc */
558 if (!msrc)
559 goto end_error;
560
561 ip = mtod(msrc, struct ip *);
562#if DEBUG
563 {
564 char bufa[20], bufb[20];
565 strcpy(bufa, inet_ntoa(ip->ip_src));
566 strcpy(bufb, inet_ntoa(ip->ip_dst));
567 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
568 }
569#endif
570 if ( ip->ip_off & IP_OFFMASK
571 && type != ICMP_SOURCEQUENCH)
572 goto end_error; /* Only reply to fragment 0 */
573
574 shlen = ip->ip_hl << 2;
575 s_ip_len = ip->ip_len;
576 if (ip->ip_p == IPPROTO_ICMP)
577 {
578 icp = (struct icmp *)((char *)ip + shlen);
579 /*
580 * Assume any unknown ICMP type is an error. This isn't
581 * specified by the RFC, but think about it..
582 */
583 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
584 goto end_error;
585 }
586
587 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
588 if (new_m_size < MSIZE)
589 {
590 size = MCLBYTES;
591 }
592 else if (new_m_size < MCLBYTES)
593 {
594 size = MCLBYTES;
595 }
596 else if(new_m_size < MJUM9BYTES)
597 {
598 size = MJUM9BYTES;
599 }
600 else if (new_m_size < MJUM16BYTES)
601 {
602 size = MJUM16BYTES;
603 }
604 else
605 {
606 AssertMsgFailed(("Unsupported size"));
607 }
608 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
609 if (!m)
610 goto end_error;
611
612 m->m_data += if_maxlinkhdr;
613 m->m_pkthdr.header = mtod(m, void *);
614
615 memcpy(m->m_data, msrc->m_data, msrc->m_len);
616 m->m_len = msrc->m_len; /* copy msrc to m */
617
618 /* make the header of the reply packet */
619 ip = mtod(m, struct ip *);
620 hlen = sizeof(struct ip ); /* no options in reply */
621
622 /* fill in icmp */
623 m->m_data += hlen;
624 m->m_len -= hlen;
625
626 icp = mtod(m, struct icmp *);
627
628 if (minsize)
629 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
630 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
631 s_ip_len = ICMP_MAXDATALEN;
632
633 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
634
635 /* min. size = 8+sizeof(struct ip)+8 */
636
637 icp->icmp_type = type;
638 icp->icmp_code = code;
639 icp->icmp_id = 0;
640 icp->icmp_seq = 0;
641
642 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
643
644 HTONS(icp->icmp_ip.ip_len);
645 HTONS(icp->icmp_ip.ip_id);
646 HTONS(icp->icmp_ip.ip_off);
647
648#if DEBUG
649 if (message)
650 {
651 /* DEBUG : append message to ICMP packet */
652 int message_len;
653 char *cpnt;
654 message_len = strlen(message);
655 if (message_len > ICMP_MAXDATALEN)
656 message_len = ICMP_MAXDATALEN;
657 cpnt = (char *)m->m_data+m->m_len;
658 m_append(pData, m, message_len, message);
659 }
660#endif
661
662 icp->icmp_cksum = 0;
663 icp->icmp_cksum = cksum(m, m->m_len);
664
665 /* fill in ip */
666 ip->ip_hl = hlen >> 2;
667 ip->ip_len = m->m_len;
668
669 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
670
671 ip->ip_ttl = MAXTTL;
672 ip->ip_p = IPPROTO_ICMP;
673 ip->ip_dst = ip->ip_src; /* ip adresses */
674 ip->ip_src = alias_addr;
675
676 (void ) ip_output0(pData, (struct socket *)NULL, m, 1);
677
678 icmpstat.icps_reflect++;
679
680 return;
681
682end_error_free_m:
683 m_freem(pData, m);
684
685end_error:
686 LogRel(("NAT: error occurred while sending ICMP error message\n"));
687}
688#undef ICMP_MAXDATALEN
689
690/*
691 * Reflect the ip packet back to the source
692 * Note: m isn't duplicated by this method and more delivered to ip_output then.
693 */
694void
695icmp_reflect(PNATState pData, struct mbuf *m)
696{
697 register struct ip *ip = mtod(m, struct ip *);
698 int hlen = ip->ip_hl << 2;
699 int optlen = hlen - sizeof(struct ip );
700 register struct icmp *icp;
701
702 /*
703 * Send an icmp packet back to the ip level,
704 * after supplying a checksum.
705 */
706 m->m_data += hlen;
707 m->m_len -= hlen;
708 icp = mtod(m, struct icmp *);
709
710 icp->icmp_cksum = 0;
711 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
712
713 m->m_data -= hlen;
714 m->m_len += hlen;
715
716 (void ) ip_output(pData, (struct socket *)NULL, m);
717
718 icmpstat.icps_reflect++;
719}
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