VirtualBox

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

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

slirp: style

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