VirtualBox

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

Last change on this file since 15223 was 15222, checked in by vboxsync, 16 years ago

slirp: fix burns

  • Property svn:eol-style set to native
File size: 19.9 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
34 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
35 */
36
37#include "slirp.h"
38#include "ip_icmp.h"
39#ifdef RT_OS_WINDOWS
40#include <Icmpapi.h>
41#include <Iphlpapi.h>
42#endif
43
44
45/* The message sent when emulating PING */
46/* Be nice and tell them it's just a psuedo-ping packet */
47static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
48
49/* list of actions for icmp_error() on RX of an icmp message */
50static const int icmp_flush[19] =
51{
52/* ECHO REPLY (0) */ 0,
53 1,
54 1,
55/* DEST UNREACH (3) */ 1,
56/* SOURCE QUENCH (4)*/ 1,
57/* REDIRECT (5) */ 1,
58 1,
59 1,
60/* ECHO (8) */ 0,
61/* ROUTERADVERT (9) */ 1,
62/* ROUTERSOLICIT (10) */ 1,
63/* TIME EXCEEDED (11) */ 1,
64/* PARAMETER PROBLEM (12) */ 1,
65/* TIMESTAMP (13) */ 0,
66/* TIMESTAMP REPLY (14) */ 0,
67/* INFO (15) */ 0,
68/* INFO REPLY (16) */ 0,
69/* ADDR MASK (17) */ 0,
70/* ADDR MASK REPLY (18) */ 0
71};
72
73#ifdef VBOX_WITH_SLIRP_ICMP
74int
75icmp_init(PNATState pData)
76{
77 pData->icmp_socket.so_type = IPPROTO_ICMP;
78 pData->icmp_socket.so_state = SS_ISFCONNECTED;
79#ifndef RT_OS_WINDOWS
80 pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
81 if (pData->icmp_socket.s == -1)
82 {
83 int rc = RTErrConvertFromErrno(errno);
84 LogRel(("NAT: ICMP/ping not available (could open ICMP socket, error %Rrc)\n", rc));
85 return 1;
86 }
87#else /* RT_OS_WINDOWS */
88 pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
89 if (pData->hmIcmpLibrary != NULL)
90 {
91 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
92 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
93 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
94 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
95 }
96 if (pData->pfIcmpParseReplies == NULL)
97 {
98 FreeLibrary(pData->hmIcmpLibrary);
99 pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
100 if (pData->hmIcmpLibrary == NULL)
101 {
102 LogRel(("NAT: Icmp.dll could not be loaded\n"));
103 return 1;
104 }
105 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
106 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
107 pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
108 GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
109 }
110 if (pData->pfIcmpParseReplies == NULL)
111 {
112 LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
113 FreeLibrary(pData->hmIcmpLibrary);
114 return 1;
115 }
116 if (pData->pfIcmpCloseHandle == NULL)
117 {
118 LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
119 FreeLibrary(pData->hmIcmpLibrary);
120 return 1;
121 }
122 pData->icmp_socket.sh = IcmpCreateFile();
123 pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
124 pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
125 pData->pvIcmpBuffer = malloc(pData->szIcmpBuffer);
126#endif /* RT_OS_WINDOWS */
127 LIST_INIT(&pData->icmp_msg_head);
128 return 0;
129}
130
131/*
132 * ip here is ip header + 64bytes readed from ICMP packet
133 */
134struct icmp_msg *
135icmp_find_original_mbuf(PNATState pData, struct ip *ip)
136{
137 struct mbuf *m0;
138 struct ip *ip0;
139 struct icmp *icp, *icp0;
140 struct icmp_msg *icm = NULL;
141 int found = 0;
142 struct socket *head_socket;
143 struct in_addr laddr, faddr;
144 u_int lport, fport;
145
146 switch (ip->ip_p)
147 {
148 case IPPROTO_ICMP:
149 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
150 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
151 {
152 m0 = icm->im_m;
153 ip0 = mtod(m0, struct ip *);
154 AssertRelease(ip0->ip_p == IPPROTO_ICMP);
155 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
156 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
157 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
158 && icp->icmp_id == icp0->icmp_id
159 && icp->icmp_seq == icp->icmp_seq)
160 {
161 found = 1;
162 break;
163 }
164 }
165 case IPPROTO_UDP:
166 head_socket = &udb;
167 case IPPROTO_TCP:
168 head_socket = (head_socket != NULL ? head_socket : &tcb); /* head_socket could be initialized with udb*/
169 }
170 if (found == 1)
171 return icm;
172
173 return NULL;
174}
175
176static int
177icmp_attach(PNATState pData, struct mbuf *m)
178{
179 struct icmp_msg *icm;
180 struct ip *ip;
181 ip = mtod(m, struct ip *);
182 Assert(ip->ip_p == IPPROTO_ICMP);
183 icm = malloc(sizeof(struct icmp_msg));
184 icm->im_m = m;
185 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
186 return (0);
187}
188#endif /* VBOX_WITH_SLIRP_ICMP */
189
190/*
191 * Process a received ICMP message.
192 */
193void
194icmp_input(PNATState pData, struct mbuf *m, int hlen)
195{
196 register struct icmp *icp;
197 register struct ip *ip=mtod(m, struct ip *);
198 int icmplen=ip->ip_len;
199 int status;
200 uint32_t dst;
201
202 /* int code; */
203
204 DEBUG_CALL("icmp_input");
205 DEBUG_ARG("m = %lx", (long )m);
206 DEBUG_ARG("m_len = %d", m->m_len);
207
208 icmpstat.icps_received++;
209
210 /*
211 * Locate icmp structure in mbuf, and check
212 * that its not corrupted and of at least minimum length.
213 */
214 if (icmplen < ICMP_MINLEN)
215 {
216 /* min 8 bytes payload */
217 icmpstat.icps_tooshort++;
218freeit:
219 m_freem(pData, m);
220 goto end_error;
221 }
222
223 m->m_len -= hlen;
224 m->m_data += hlen;
225 icp = mtod(m, struct icmp *);
226 if (cksum(m, icmplen))
227 {
228 icmpstat.icps_checksum++;
229 goto freeit;
230 }
231 m->m_len += hlen;
232 m->m_data -= hlen;
233
234 /* icmpstat.icps_inhist[icp->icmp_type]++; */
235 /* code = icp->icmp_code; */
236
237 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
238 switch (icp->icmp_type)
239 {
240 case ICMP_ECHO:
241#ifndef VBOX_WITH_SLIRP_ICMP
242 icp->icmp_type = ICMP_ECHOREPLY;
243#endif /* !VBOX_WITH_SLIRP_ICMP */
244
245 ip->ip_len += hlen; /* since ip_input subtracts this */
246 dst = ip->ip_dst.s_addr;
247 if (dst == alias_addr.s_addr)
248 {
249#ifdef VBOX_WITH_SLIRP_ICMP
250 icp->icmp_type = ICMP_ECHOREPLY;
251 ip->ip_dst.s_addr = ip->ip_src.s_addr;
252 ip->ip_src.s_addr = dst;
253#endif /* VBOX_WITH_SLIRP_ICMP */
254 icmp_reflect(pData, m);
255 }
256 else
257 {
258 struct socket *so;
259 struct sockaddr_in addr;
260#ifndef VBOX_WITH_SLIRP_ICMP
261 if ((so = socreate()) == NULL)
262 goto freeit;
263 if (udp_attach(pData, so) == -1)
264 {
265 DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
266 errno,strerror(errno)));
267 sofree(pData, so);
268 m_free(pData, m);
269 goto end_error;
270 }
271 so->so_m = m;
272 so->so_faddr = ip->ip_dst;
273 so->so_fport = htons(7);
274 so->so_laddr = ip->ip_src;
275 so->so_lport = htons(9);
276 so->so_iptos = ip->ip_tos;
277 so->so_type = IPPROTO_ICMP;
278 so->so_state = SS_ISFCONNECTED;
279
280 addr.sin_family = AF_INET;
281 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
282 {
283 /* It's an alias */
284 switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
285 {
286 case CTL_DNS:
287 addr.sin_addr = dns_addr;
288 break;
289 case CTL_ALIAS:
290 default:
291 addr.sin_addr = loopback_addr;
292 break;
293 }
294 }
295 else
296 addr.sin_addr = so->so_faddr;
297 addr.sin_port = so->so_fport;
298 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
299 (struct sockaddr *)&addr, sizeof(addr)) == -1)
300 {
301 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
302 errno,strerror(errno)));
303 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
304 udp_detach(pData, so);
305 }
306#else /* VBOX_WITH_SLIRP_ICMP */
307# ifdef RT_OS_WINDOWS
308 IP_OPTION_INFORMATION ipopt;
309 int error;
310# endif
311 addr.sin_family = AF_INET;
312 if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
313 {
314 /* It's an alias */
315 switch(ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
316 {
317 case CTL_DNS:
318 addr.sin_addr = dns_addr;
319 break;
320 case CTL_ALIAS:
321 default:
322 addr.sin_addr = loopback_addr;
323 break;
324 }
325 }
326 else
327 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
328 icmp_attach(pData, m);
329 /* Send the packet */
330# ifndef RT_OS_WINDOWS
331 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL, (void *)&ip->ip_ttl, sizeof(ip->ip_ttl));
332 if (status < 0)
333 {
334 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n", strerror(errno)));
335 }
336 if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
337 (struct sockaddr *)&addr, sizeof(addr)) == -1)
338 {
339 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
340 errno,strerror(errno)));
341 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
342 m_free(pData, m);
343 }
344# else /* RT_OS_WINDOWS */
345 pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
346 pData->icmp_socket.so_icmp_id = icp->icmp_id;
347 pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
348 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
349 ipopt.Ttl = ip->ip_ttl;
350 status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
351 NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
352 icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
353 pData->pvIcmpBuffer, pData->szIcmpBuffer, 0);
354 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
355 {
356 error = GetLastError();
357 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
358 switch (error)
359 {
360 case ERROR_INVALID_PARAMETER:
361 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
362 break;
363 case ERROR_NOT_SUPPORTED:
364 LogRel(("operation is unsupported)\n"));
365 break;
366 case ERROR_NOT_ENOUGH_MEMORY:
367 LogRel(("OOM!!!)\n"));
368 break;
369 case IP_BUF_TOO_SMALL:
370 LogRel(("Buffer too small)\n"));
371 break;
372 default:
373 LogRel(("Other error!!!)\n"));
374 break;
375 }
376 }
377# endif /* RT_OS_WINDOWS */
378#endif /* VBOX_WITH_SLIRP_ICMP */
379 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
380 break;
381 case ICMP_UNREACH:
382 /* XXX? report error? close socket? */
383 case ICMP_TIMXCEED:
384 case ICMP_PARAMPROB:
385 case ICMP_SOURCEQUENCH:
386 case ICMP_TSTAMP:
387 case ICMP_MASKREQ:
388 case ICMP_REDIRECT:
389 icmpstat.icps_notsupp++;
390 m_freem(pData, m);
391 break;
392
393 default:
394 icmpstat.icps_badtype++;
395 m_freem(pData, m);
396 } /* switch */
397
398end_error:
399 /* m is m_free()'d xor put in a socket xor or given to ip_send */
400 ;
401}
402
403
404/*
405 * Send an ICMP message in response to a situation
406 *
407 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
408 * MUST NOT change this header information.
409 * MUST NOT reply to a multicast/broadcast IP address.
410 * MUST NOT reply to a multicast/broadcast MAC address.
411 * MUST reply to only the first fragment.
412 */
413/*
414 * Send ICMP_UNREACH back to the source regarding msrc.
415 * mbuf *msrc is used as a template, but is NOT m_free()'d.
416 * It is reported as the bad ip packet. The header should
417 * be fully correct and in host byte order.
418 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
419 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
420 */
421
422#define ICMP_MAXDATALEN (IP_MSS-28)
423void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, char *message)
424{
425 unsigned hlen, shlen, s_ip_len;
426 register struct ip *ip;
427 register struct icmp *icp;
428 register struct mbuf *m;
429
430 DEBUG_CALL("icmp_error");
431 DEBUG_ARG("msrc = %lx", (long )msrc);
432 DEBUG_ARG("msrc_len = %d", msrc->m_len);
433
434 if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
435 goto end_error;
436
437 /* check msrc */
438 if (!msrc)
439 goto end_error;
440
441 ip = mtod(msrc, struct ip *);
442#if DEBUG
443 {
444 char bufa[20], bufb[20];
445 strcpy(bufa, inet_ntoa(ip->ip_src));
446 strcpy(bufb, inet_ntoa(ip->ip_dst));
447 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
448 }
449#endif
450 if (ip->ip_off & IP_OFFMASK)
451 goto end_error; /* Only reply to fragment 0 */
452
453 shlen=ip->ip_hl << 2;
454 s_ip_len=ip->ip_len;
455 if (ip->ip_p == IPPROTO_ICMP)
456 {
457 icp = (struct icmp *)((char *)ip + shlen);
458 /*
459 * Assume any unknown ICMP type is an error. This isn't
460 * specified by the RFC, but think about it..
461 */
462 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
463 goto end_error;
464 }
465
466 /* make a copy */
467 if (!(m=m_get(pData)))
468 goto end_error; /* get mbuf */
469 {
470 int new_m_size;
471 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
472 if (new_m_size>m->m_size)
473 m_inc(m, new_m_size);
474 }
475 memcpy(m->m_data, msrc->m_data, msrc->m_len);
476 m->m_len = msrc->m_len; /* copy msrc to m */
477
478 /* make the header of the reply packet */
479 ip = mtod(m, struct ip *);
480 hlen = sizeof(struct ip ); /* no options in reply */
481
482 /* fill in icmp */
483 m->m_data += hlen;
484 m->m_len -= hlen;
485
486 icp = mtod(m, struct icmp *);
487
488 if (minsize)
489 s_ip_len=shlen+ICMP_MINLEN; /* return header+8b only */
490 else if (s_ip_len>ICMP_MAXDATALEN) /* maximum size */
491 s_ip_len=ICMP_MAXDATALEN;
492
493 m->m_len=ICMP_MINLEN+s_ip_len; /* 8 bytes ICMP header */
494
495 /* min. size = 8+sizeof(struct ip)+8 */
496
497 icp->icmp_type = type;
498 icp->icmp_code = code;
499 icp->icmp_id = 0;
500 icp->icmp_seq = 0;
501
502 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
503 HTONS(icp->icmp_ip.ip_len);
504 HTONS(icp->icmp_ip.ip_id);
505 HTONS(icp->icmp_ip.ip_off);
506
507#if DEBUG
508 if (message)
509 {
510 /* DEBUG : append message to ICMP packet */
511 int message_len;
512 char *cpnt;
513 message_len=strlen(message);
514 if (message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
515 cpnt=(char *)m->m_data+m->m_len;
516 memcpy(cpnt, message, message_len);
517 m->m_len+=message_len;
518 }
519#endif
520
521 icp->icmp_cksum = 0;
522 icp->icmp_cksum = cksum(m, m->m_len);
523
524 m->m_data -= hlen;
525 m->m_len += hlen;
526
527 /* fill in ip */
528 ip->ip_hl = hlen >> 2;
529 ip->ip_len = m->m_len;
530
531 ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
532
533 ip->ip_ttl = MAXTTL;
534 ip->ip_p = IPPROTO_ICMP;
535 ip->ip_dst = ip->ip_src; /* ip adresses */
536 ip->ip_src = alias_addr;
537
538 (void ) ip_output(pData, (struct socket *)NULL, m);
539
540 icmpstat.icps_reflect++;
541
542end_error:
543 ;
544}
545#undef ICMP_MAXDATALEN
546
547/*
548 * Reflect the ip packet back to the source
549 */
550void
551icmp_reflect(PNATState pData, struct mbuf *m)
552{
553 register struct ip *ip = mtod(m, struct ip *);
554 int hlen = ip->ip_hl << 2;
555 int optlen = hlen - sizeof(struct ip );
556 register struct icmp *icp;
557
558 /*
559 * Send an icmp packet back to the ip level,
560 * after supplying a checksum.
561 */
562 m->m_data += hlen;
563 m->m_len -= hlen;
564 icp = mtod(m, struct icmp *);
565
566 icp->icmp_cksum = 0;
567 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
568
569 m->m_data -= hlen;
570 m->m_len += hlen;
571
572#ifndef VBOX_WITH_SLIRP_ICMP
573 /* fill in ip */
574 if (optlen > 0)
575 {
576 /*
577 * Strip out original options by copying rest of first
578 * mbuf's data back, and adjust the IP length.
579 */
580 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
581 (unsigned )(m->m_len - hlen));
582 hlen -= optlen;
583 ip->ip_hl = hlen >> 2;
584 ip->ip_len -= optlen;
585 m->m_len -= optlen;
586 }
587 ip->ip_ttl = MAXTTL;
588 {
589 /* swap */
590 struct in_addr icmp_dst;
591 icmp_dst = ip->ip_dst;
592 ip->ip_dst = ip->ip_src;
593 ip->ip_src = icmp_dst;
594 }
595#endif /* !VBOX_WITH_SLIRP_ICMP */
596
597 (void ) ip_output(pData, (struct socket *)NULL, m);
598
599 icmpstat.icps_reflect++;
600}
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