VirtualBox

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

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

slirp:ICMP send of icmp packet happens (wireshark for detection)

  • Property svn:eol-style set to native
File size: 19.1 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 insque(pData, &pData->icmp_socket, &udb);
88#else /* RT_OS_WINDOWS */
89 pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
90 if (pData->hmIcmpLibrary != NULL)
91 {
92 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
93 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
94 }
95 if (pData->pfIcmpParseReplies == NULL)
96 {
97 FreeLibrary(pData->hmIcmpLibrary);
98 pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
99 if (pData->hmIcmpLibrary == NULL)
100 {
101 LogRel(("NAT: Icmp.dll could not be loaded\n"));
102 return 1;
103 }
104 pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
105 GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
106 }
107 if (pData->pfIcmpParseReplies == NULL)
108 {
109 LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
110 FreeLibrary(pData->hmIcmpLibrary);
111 return 1;
112 }
113 pData->icmp_socket.sh = IcmpCreateFile();
114 pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
115 pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
116 pData->pvIcmpBuffer = malloc(pData->szIcmpBuffer);
117#endif /* RT_OS_WINDOWS */
118 LIST_INIT(&pData->icmp_msg_head);
119 return 0;
120}
121
122/*
123 * ip here is ip header + 64bytes readed from ICMP packet
124 */
125struct icmp_msg *
126icmp_find_original_mbuf(PNATState pData, struct ip *ip)
127{
128 struct mbuf *m0;
129 struct ip *ip0;
130 struct icmp *icp, *icp0;
131 struct icmp_msg *icm;
132 int found = 0;
133 struct socket *head_socket;
134 struct in_addr laddr, faddr;
135 u_int lport, fport;
136
137 switch (ip->ip_p)
138 {
139 case IPPROTO_ICMP:
140 icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
141 LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
142 {
143 m0 = icm->im_m;
144 ip0 = mtod(m0, struct ip *);
145 AssertRelease(ip0->ip_p == IPPROTO_ICMP);
146 icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
147 if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
148 || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
149 && icp->icmp_id == icp0->icmp_id
150 && icp->icmp_seq == icp->icmp_seq)
151 {
152 found = 1;
153 break;
154 }
155 }
156 case IPPROTO_UDP:
157 head_socket = &udb;
158 case IPPROTO_TCP:
159 head_socket = (head_socket != NULL ? head_socket : &tcb); /* head_socket could be initialized with udb*/
160 }
161 if (found == 1)
162 return icm;
163
164 return NULL;
165}
166
167static int
168icmp_attach(PNATState pData, struct mbuf *m)
169{
170 struct icmp_msg *icm;
171 struct ip *ip;
172 ip = mtod(m, struct ip *);
173 Assert(ip->ip_p == IPPROTO_ICMP);
174 icm = malloc(sizeof(struct icmp_msg));
175 icm->im_m = m;
176 LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
177 return (0);
178}
179#endif /* VBOX_WITH_SLIRP_ICMP */
180
181/*
182 * Process a received ICMP message.
183 */
184void
185icmp_input(PNATState pData, struct mbuf *m, int hlen)
186{
187 register struct icmp *icp;
188 register struct ip *ip=mtod(m, struct ip *);
189 int icmplen=ip->ip_len;
190 int status;
191 /* int code; */
192
193 DEBUG_CALL("icmp_input");
194 DEBUG_ARG("m = %lx", (long )m);
195 DEBUG_ARG("m_len = %d", m->m_len);
196
197 icmpstat.icps_received++;
198
199 /*
200 * Locate icmp structure in mbuf, and check
201 * that its not corrupted and of at least minimum length.
202 */
203 if (icmplen < ICMP_MINLEN)
204 {
205 /* min 8 bytes payload */
206 icmpstat.icps_tooshort++;
207freeit:
208 m_freem(pData, m);
209 goto end_error;
210 }
211
212 m->m_len -= hlen;
213 m->m_data += hlen;
214 icp = mtod(m, struct icmp *);
215 if (cksum(m, icmplen))
216 {
217 icmpstat.icps_checksum++;
218 goto freeit;
219 }
220 m->m_len += hlen;
221 m->m_data -= hlen;
222
223 /* icmpstat.icps_inhist[icp->icmp_type]++; */
224 /* code = icp->icmp_code; */
225
226 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
227 switch (icp->icmp_type)
228 {
229 case ICMP_ECHO:
230#ifndef VBOX_WITH_SLIRP_ICMP
231 icp->icmp_type = ICMP_ECHOREPLY;
232#endif /* !VBOX_WITH_SLIRP_ICMP */
233
234 ip->ip_len += hlen; /* since ip_input subtracts this */
235 if (ip->ip_dst.s_addr == alias_addr.s_addr)
236 {
237#ifdef VBOX_WITH_SLIRP_ICMP
238 icp->icmp_type = ICMP_ECHOREPLY;
239#endif /* VBOX_WITH_SLIRP_ICMP */
240 icmp_reflect(pData, m);
241 }
242 else
243 {
244 struct socket *so;
245 struct sockaddr_in addr;
246#ifndef VBOX_WITH_SLIRP_ICMP
247 if ((so = socreate()) == NULL)
248 goto freeit;
249 if (udp_attach(pData, so) == -1)
250 {
251 DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
252 errno,strerror(errno)));
253 sofree(pData, so);
254 m_free(pData, m);
255 goto end_error;
256 }
257 so->so_m = m;
258 so->so_faddr = ip->ip_dst;
259 so->so_fport = htons(7);
260 so->so_laddr = ip->ip_src;
261 so->so_lport = htons(9);
262 so->so_iptos = ip->ip_tos;
263 so->so_type = IPPROTO_ICMP;
264 so->so_state = SS_ISFCONNECTED;
265
266 addr.sin_family = AF_INET;
267 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
268 {
269 /* It's an alias */
270 switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
271 {
272 case CTL_DNS:
273 addr.sin_addr = dns_addr;
274 break;
275 case CTL_ALIAS:
276 default:
277 addr.sin_addr = loopback_addr;
278 break;
279 }
280 }
281 else
282 addr.sin_addr = so->so_faddr;
283 addr.sin_port = so->so_fport;
284 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
285 (struct sockaddr *)&addr, sizeof(addr)) == -1)
286 {
287 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
288 errno,strerror(errno)));
289 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
290 udp_detach(pData, so);
291 }
292#else /* VBOX_WITH_SLIRP_ICMP */
293# ifdef RT_OS_WINDOWS
294 IP_OPTION_INFORMATION ipopt;
295 int error;
296# endif
297 addr.sin_family = AF_INET;
298 if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
299 {
300 /* It's an alias */
301 switch(ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
302 {
303 case CTL_DNS:
304 addr.sin_addr = dns_addr;
305 break;
306 case CTL_ALIAS:
307 default:
308 addr.sin_addr = loopback_addr;
309 break;
310 }
311 }
312 else
313 addr.sin_addr.s_addr = ip->ip_dst.s_addr;
314 icmp_attach(pData, m);
315 /* Send the packet */
316# ifndef RT_OS_WINDOWS
317 status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL, (void *)&ip->ip_ttl, sizeof(ip->ip_ttl));
318 if (status < 0)
319 {
320 LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n", strerror(errno)));
321 }
322 if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
323 (struct sockaddr *)&addr, sizeof(addr)) == -1)
324 {
325 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
326 errno,strerror(errno)));
327 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
328 m_free(pData, m);
329 }
330# else /* RT_OS_WINDOWS */
331 memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
332 ipopt.Ttl = ip->ip_ttl;
333 status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
334 NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
335 icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
336 pData->pvIcmpBuffer, pData->szIcmpBuffer, 0);
337 if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
338 {
339 error = GetLastError();
340 LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
341 switch (error)
342 {
343 case ERROR_INVALID_PARAMETER:
344 LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
345 break;
346 case ERROR_NOT_SUPPORTED:
347 LogRel(("operation is unsupported)\n"));
348 break;
349 case ERROR_NOT_ENOUGH_MEMORY:
350 LogRel(("OOM!!!)\n"));
351 break;
352 case IP_BUF_TOO_SMALL:
353 LogRel(("Buffer too small)\n"));
354 break;
355 default:
356 LogRel(("Other error!!!)\n"));
357 break;
358 }
359 }
360# endif /* RT_OS_WINDOWS */
361#endif /* VBOX_WITH_SLIRP_ICMP */
362 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
363 break;
364 case ICMP_UNREACH:
365 /* XXX? report error? close socket? */
366 case ICMP_TIMXCEED:
367 case ICMP_PARAMPROB:
368 case ICMP_SOURCEQUENCH:
369 case ICMP_TSTAMP:
370 case ICMP_MASKREQ:
371 case ICMP_REDIRECT:
372 icmpstat.icps_notsupp++;
373 m_freem(pData, m);
374 break;
375
376 default:
377 icmpstat.icps_badtype++;
378 m_freem(pData, m);
379 } /* switch */
380
381end_error:
382 /* m is m_free()'d xor put in a socket xor or given to ip_send */
383 ;
384}
385
386
387/*
388 * Send an ICMP message in response to a situation
389 *
390 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
391 * MUST NOT change this header information.
392 * MUST NOT reply to a multicast/broadcast IP address.
393 * MUST NOT reply to a multicast/broadcast MAC address.
394 * MUST reply to only the first fragment.
395 */
396/*
397 * Send ICMP_UNREACH back to the source regarding msrc.
398 * mbuf *msrc is used as a template, but is NOT m_free()'d.
399 * It is reported as the bad ip packet. The header should
400 * be fully correct and in host byte order.
401 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
402 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
403 */
404
405#define ICMP_MAXDATALEN (IP_MSS-28)
406void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, char *message)
407{
408 unsigned hlen, shlen, s_ip_len;
409 register struct ip *ip;
410 register struct icmp *icp;
411 register struct mbuf *m;
412
413 DEBUG_CALL("icmp_error");
414 DEBUG_ARG("msrc = %lx", (long )msrc);
415 DEBUG_ARG("msrc_len = %d", msrc->m_len);
416
417 if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
418 goto end_error;
419
420 /* check msrc */
421 if (!msrc)
422 goto end_error;
423
424 ip = mtod(msrc, struct ip *);
425#if DEBUG
426 {
427 char bufa[20], bufb[20];
428 strcpy(bufa, inet_ntoa(ip->ip_src));
429 strcpy(bufb, inet_ntoa(ip->ip_dst));
430 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
431 }
432#endif
433 if (ip->ip_off & IP_OFFMASK)
434 goto end_error; /* Only reply to fragment 0 */
435
436 shlen=ip->ip_hl << 2;
437 s_ip_len=ip->ip_len;
438 if (ip->ip_p == IPPROTO_ICMP)
439 {
440 icp = (struct icmp *)((char *)ip + shlen);
441 /*
442 * Assume any unknown ICMP type is an error. This isn't
443 * specified by the RFC, but think about it..
444 */
445 if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
446 goto end_error;
447 }
448
449 /* make a copy */
450 if (!(m=m_get(pData)))
451 goto end_error; /* get mbuf */
452 {
453 int new_m_size;
454 new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
455 if (new_m_size>m->m_size)
456 m_inc(m, new_m_size);
457 }
458 memcpy(m->m_data, msrc->m_data, msrc->m_len);
459 m->m_len = msrc->m_len; /* copy msrc to m */
460
461 /* make the header of the reply packet */
462 ip = mtod(m, struct ip *);
463 hlen = sizeof(struct ip ); /* no options in reply */
464
465 /* fill in icmp */
466 m->m_data += hlen;
467 m->m_len -= hlen;
468
469 icp = mtod(m, struct icmp *);
470
471 if (minsize)
472 s_ip_len=shlen+ICMP_MINLEN; /* return header+8b only */
473 else if (s_ip_len>ICMP_MAXDATALEN) /* maximum size */
474 s_ip_len=ICMP_MAXDATALEN;
475
476 m->m_len=ICMP_MINLEN+s_ip_len; /* 8 bytes ICMP header */
477
478 /* min. size = 8+sizeof(struct ip)+8 */
479
480 icp->icmp_type = type;
481 icp->icmp_code = code;
482 icp->icmp_id = 0;
483 icp->icmp_seq = 0;
484
485 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
486 HTONS(icp->icmp_ip.ip_len);
487 HTONS(icp->icmp_ip.ip_id);
488 HTONS(icp->icmp_ip.ip_off);
489
490#if DEBUG
491 if (message)
492 {
493 /* DEBUG : append message to ICMP packet */
494 int message_len;
495 char *cpnt;
496 message_len=strlen(message);
497 if (message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
498 cpnt=(char *)m->m_data+m->m_len;
499 memcpy(cpnt, message, message_len);
500 m->m_len+=message_len;
501 }
502#endif
503
504 icp->icmp_cksum = 0;
505 icp->icmp_cksum = cksum(m, m->m_len);
506
507 m->m_data -= hlen;
508 m->m_len += hlen;
509
510 /* fill in ip */
511 ip->ip_hl = hlen >> 2;
512 ip->ip_len = m->m_len;
513
514 ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
515
516 ip->ip_ttl = MAXTTL;
517 ip->ip_p = IPPROTO_ICMP;
518 ip->ip_dst = ip->ip_src; /* ip adresses */
519 ip->ip_src = alias_addr;
520
521 (void ) ip_output(pData, (struct socket *)NULL, m);
522
523 icmpstat.icps_reflect++;
524
525end_error:
526 ;
527}
528#undef ICMP_MAXDATALEN
529
530/*
531 * Reflect the ip packet back to the source
532 */
533void
534icmp_reflect(PNATState pData, struct mbuf *m)
535{
536 register struct ip *ip = mtod(m, struct ip *);
537 int hlen = ip->ip_hl << 2;
538 int optlen = hlen - sizeof(struct ip );
539 register struct icmp *icp;
540
541 /*
542 * Send an icmp packet back to the ip level,
543 * after supplying a checksum.
544 */
545 m->m_data += hlen;
546 m->m_len -= hlen;
547 icp = mtod(m, struct icmp *);
548
549 icp->icmp_cksum = 0;
550 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
551
552 m->m_data -= hlen;
553 m->m_len += hlen;
554
555#ifndef VBOX_WITH_SLIRP_ICMP
556 /* fill in ip */
557 if (optlen > 0)
558 {
559 /*
560 * Strip out original options by copying rest of first
561 * mbuf's data back, and adjust the IP length.
562 */
563 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
564 (unsigned )(m->m_len - hlen));
565 hlen -= optlen;
566 ip->ip_hl = hlen >> 2;
567 ip->ip_len -= optlen;
568 m->m_len -= optlen;
569 }
570 ip->ip_ttl = MAXTTL;
571 {
572 /* swap */
573 struct in_addr icmp_dst;
574 icmp_dst = ip->ip_dst;
575 ip->ip_dst = ip->ip_src;
576 ip->ip_src = icmp_dst;
577 }
578#endif /* !VBOX_WITH_SLIRP_ICMP */
579
580 (void ) ip_output(pData, (struct socket *)NULL, m);
581
582 icmpstat.icps_reflect++;
583}
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