VirtualBox

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

Last change on this file since 30363 was 30363, checked in by vboxsync, 14 years ago

NAT: more comments + cosmetics

  • 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 30363 2010-06-22 11:21:22Z 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 register struct ip *ip = mtod(m, struct ip *);
333 int icmplen = ip->ip_len;
334 int status;
335 uint32_t dst;
336#if !defined(RT_OS_WINDOWS)
337 int ttl;
338#endif
339
340 /* int code; */
341
342 DEBUG_CALL("icmp_input");
343 DEBUG_ARG("m = %lx", (long )m);
344 DEBUG_ARG("m_len = %d", m ? m->m_len : 0);
345
346 icmpstat.icps_received++;
347
348 /*
349 * Locate icmp structure in mbuf, and check
350 * that its not corrupted and of at least minimum length.
351 */
352 if (icmplen < ICMP_MINLEN)
353 {
354 /* min 8 bytes payload */
355 icmpstat.icps_tooshort++;
356freeit:
357 m_freem(pData, m);
358 goto end_error;
359 }
360
361 m->m_len -= hlen;
362 m->m_data += hlen;
363 if (m->m_next != NULL)
364 {
365 char *buf = RTMemAlloc(icmplen);
366 m_copydata(m, 0, icmplen, buf);
367 icp = (struct icmp *)buf;
368 }
369 else
370 {
371 icp = mtod(m, struct icmp *);
372 }
373 if (cksum(m, icmplen))
374 {
375 icmpstat.icps_checksum++;
376 goto freeit;
377 }
378 m->m_len += hlen;
379 m->m_data -= hlen;
380
381 /* icmpstat.icps_inhist[icp->icmp_type]++; */
382 /* code = icp->icmp_code; */
383
384 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
385 switch (icp->icmp_type)
386 {
387 case ICMP_ECHO:
388 ip->ip_len += hlen; /* since ip_input subtracts this */
389 dst = ip->ip_dst.s_addr;
390 if (dst == alias_addr.s_addr)
391 {
392 icp->icmp_type = ICMP_ECHOREPLY;
393 ip->ip_dst.s_addr = ip->ip_src.s_addr;
394 ip->ip_src.s_addr = dst;
395 icmp_reflect(pData, m);
396 }
397 else
398 {
399 struct sockaddr_in addr;
400#ifdef RT_OS_WINDOWS
401 IP_OPTION_INFORMATION ipopt;
402 int error;
403#endif
404 addr.sin_family = AF_INET;
405 if ((ip->ip_dst.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
406 {
407 /* It's an alias */
408 switch (RT_N2H_U32(ip->ip_dst.s_addr) & ~pData->netmask)
409 {
410 case CTL_DNS:
411 case CTL_ALIAS:
412 default:
413 addr.sin_addr = loopback_addr;
414 break;
415 }
416 }
417 else
418 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
419#ifndef RT_OS_WINDOWS
420 if (pData->icmp_socket.s != -1)
421 {
422 ssize_t rc;
423 m->m_so = &pData->icmp_socket;
424 icmp_attach(pData, m);
425 ttl = ip->ip_ttl;
426 Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
427 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
428 (void *)&ttl, sizeof(ttl));
429 if (status < 0)
430 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
431 strerror(errno)));
432 rc = sendto(pData->icmp_socket.s, icp, icmplen, 0,
433 (struct sockaddr *)&addr, sizeof(addr));
434 if (rc < 0)
435 {
436 LogRel((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
437 errno, strerror(errno)));
438 icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
439 }
440 }
441 else
442 {
443 /*
444 * We're freeing the ICMP message, which unable sent or process.
445 * That behavior described in rfc 793, we shouldn't notify sender about
446 * fail of processing it's ICMP packets
447 */
448 m_free(pData, m);
449 return;
450 }
451#else /* RT_OS_WINDOWS */
452 icmp_attach(pData, m);
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 m->m_so = &pData->icmp_socket;
457 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
458 ipopt.Ttl = ip->ip_ttl;
459 status = ICMP_SEND_ECHO(pData->phEvents[VBOX_ICMP_EVENT_INDEX], notify_slirp, addr.sin_addr.s_addr,
460 icp->icmp_data, icmplen - ICMP_MINLEN, &ipopt);
461 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
462 {
463 error = GetLastError();
464 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
465 switch (error)
466 {
467 case ERROR_INVALID_PARAMETER:
468 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
469 break;
470 case ERROR_NOT_SUPPORTED:
471 LogRel(("operation is unsupported)\n"));
472 break;
473 case ERROR_NOT_ENOUGH_MEMORY:
474 LogRel(("OOM!!!)\n"));
475 break;
476 case IP_BUF_TOO_SMALL:
477 LogRel(("Buffer too small)\n"));
478 break;
479 default:
480 LogRel(("Other error!!!)\n"));
481 break;
482 }
483 }
484#endif /* RT_OS_WINDOWS */
485 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
486 break;
487 case ICMP_UNREACH:
488 /* XXX? report error? close socket? */
489 case ICMP_TIMXCEED:
490 case ICMP_PARAMPROB:
491 case ICMP_SOURCEQUENCH:
492 case ICMP_TSTAMP:
493 case ICMP_MASKREQ:
494 case ICMP_REDIRECT:
495 icmpstat.icps_notsupp++;
496 m_freem(pData, m);
497 break;
498
499 default:
500 icmpstat.icps_badtype++;
501 m_freem(pData, m);
502 } /* switch */
503 if (m->m_next != NULL && icp != NULL)
504 RTMemFree(icp);
505
506end_error:
507 /* m is m_free()'d xor put in a socket xor or given to ip_send */
508 ;
509}
510
511
512/**
513 * Send an ICMP message in response to a situation
514 *
515 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
516 * MUST NOT change this header information.
517 * MUST NOT reply to a multicast/broadcast IP address.
518 * MUST NOT reply to a multicast/broadcast MAC address.
519 * MUST reply to only the first fragment.
520 */
521/*
522 * Send ICMP_UNREACH back to the source regarding msrc.
523 * mbuf *msrc is used as a template, but is NOT m_free()'d.
524 * It is reported as the bad ip packet. The header should
525 * be fully correct and in host byte order.
526 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
527 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
528 *
529 * @note This function will free msrc!
530 */
531
532#define ICMP_MAXDATALEN (IP_MSS-28)
533void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
534{
535 unsigned hlen, shlen, s_ip_len;
536 register struct ip *ip;
537 register struct icmp *icp;
538 register struct mbuf *m;
539 int new_m_size = 0;
540 int size = 0;
541
542 DEBUG_CALL("icmp_error");
543 DEBUG_ARG("msrc = %lx", (long )msrc);
544 DEBUG_ARG("msrc_len = %d", msrc ? msrc->m_len : 0);
545 if (msrc != NULL)
546 M_ASSERTPKTHDR(msrc);
547
548 if ( type != ICMP_UNREACH
549 && type != ICMP_TIMXCEED
550 && type != ICMP_SOURCEQUENCH)
551 goto end_error;
552
553 /* check msrc */
554 if (!msrc)
555 goto end_error;
556
557 ip = mtod(msrc, struct ip *);
558#if DEBUG
559 {
560 char bufa[20], bufb[20];
561 strcpy(bufa, inet_ntoa(ip->ip_src));
562 strcpy(bufb, inet_ntoa(ip->ip_dst));
563 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
564 }
565#endif
566 if ( ip->ip_off & IP_OFFMASK
567 && type != ICMP_SOURCEQUENCH)
568 goto end_error; /* Only reply to fragment 0 */
569
570 shlen = ip->ip_hl << 2;
571 s_ip_len = ip->ip_len;
572 if (ip->ip_p == IPPROTO_ICMP)
573 {
574 icp = (struct icmp *)((char *)ip + shlen);
575 /*
576 * Assume any unknown ICMP type is an error. This isn't
577 * specified by the RFC, but think about it..
578 */
579 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
580 goto end_error;
581 }
582
583 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
584 if (new_m_size < MSIZE)
585 {
586 size = MCLBYTES;
587 }
588 else if (new_m_size < MCLBYTES)
589 {
590 size = MCLBYTES;
591 }
592 else if(new_m_size < MJUM9BYTES)
593 {
594 size = MJUM9BYTES;
595 }
596 else if (new_m_size < MJUM16BYTES)
597 {
598 size = MJUM16BYTES;
599 }
600 else
601 {
602 AssertMsgFailed(("Unsupported size"));
603 }
604 m = m_getjcl(pData, M_NOWAIT, MT_HEADER, M_PKTHDR, size);
605 if (!m)
606 goto end_error;
607
608 m->m_data += if_maxlinkhdr;
609 m->m_pkthdr.header = mtod(m, void *);
610
611 memcpy(m->m_data, msrc->m_data, msrc->m_len);
612 m->m_len = msrc->m_len; /* copy msrc to m */
613
614 /* make the header of the reply packet */
615 ip = mtod(m, struct ip *);
616 hlen = sizeof(struct ip ); /* no options in reply */
617
618 /* fill in icmp */
619 m->m_data += hlen;
620 m->m_len -= hlen;
621
622 icp = mtod(m, struct icmp *);
623
624 if (minsize)
625 s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
626 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
627 s_ip_len = ICMP_MAXDATALEN;
628
629 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
630
631 /* min. size = 8+sizeof(struct ip)+8 */
632
633 icp->icmp_type = type;
634 icp->icmp_code = code;
635 icp->icmp_id = 0;
636 icp->icmp_seq = 0;
637
638 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
639
640 HTONS(icp->icmp_ip.ip_len);
641 HTONS(icp->icmp_ip.ip_id);
642 HTONS(icp->icmp_ip.ip_off);
643
644#if DEBUG
645 if (message)
646 {
647 /* DEBUG : append message to ICMP packet */
648 int message_len;
649 char *cpnt;
650 message_len = strlen(message);
651 if (message_len > ICMP_MAXDATALEN)
652 message_len = ICMP_MAXDATALEN;
653 cpnt = (char *)m->m_data+m->m_len;
654 m_append(pData, m, message_len, message);
655 }
656#endif
657
658 icp->icmp_cksum = 0;
659 icp->icmp_cksum = cksum(m, m->m_len);
660
661 /* fill in ip */
662 ip->ip_hl = hlen >> 2;
663 ip->ip_len = m->m_len;
664
665 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
666
667 ip->ip_ttl = MAXTTL;
668 ip->ip_p = IPPROTO_ICMP;
669 ip->ip_dst = ip->ip_src; /* ip adresses */
670 ip->ip_src = alias_addr;
671
672 (void ) ip_output0(pData, (struct socket *)NULL, m, 1);
673
674 icmpstat.icps_reflect++;
675
676 return;
677
678end_error_free_m:
679 m_freem(pData, m);
680
681end_error:
682 LogRel(("NAT: error occurred while sending ICMP error message\n"));
683}
684#undef ICMP_MAXDATALEN
685
686/*
687 * Reflect the ip packet back to the source
688 */
689void
690icmp_reflect(PNATState pData, struct mbuf *m)
691{
692 register struct ip *ip = mtod(m, struct ip *);
693 int hlen = ip->ip_hl << 2;
694 int optlen = hlen - sizeof(struct ip );
695 register struct icmp *icp;
696
697 /*
698 * Send an icmp packet back to the ip level,
699 * after supplying a checksum.
700 */
701 m->m_data += hlen;
702 m->m_len -= hlen;
703 icp = mtod(m, struct icmp *);
704
705 icp->icmp_cksum = 0;
706 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
707
708 m->m_data -= hlen;
709 m->m_len += hlen;
710
711 (void ) ip_output(pData, (struct socket *)NULL, m);
712
713 icmpstat.icps_reflect++;
714}
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