VirtualBox

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

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

introducing ICMP support in Windows (not working)

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