VirtualBox

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

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

ICMP support disabled (doesn't work)

  • Property svn:eol-style set to native
File size: 11.2 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
40
41/* The message sent when emulating PING */
42/* Be nice and tell them it's just a psuedo-ping packet */
43static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
44
45/* list of actions for icmp_error() on RX of an icmp message */
46static const int icmp_flush[19] = {
47/* ECHO REPLY (0) */ 0,
48 1,
49 1,
50/* DEST UNREACH (3) */ 1,
51/* SOURCE QUENCH (4)*/ 1,
52/* REDIRECT (5) */ 1,
53 1,
54 1,
55/* ECHO (8) */ 0,
56/* ROUTERADVERT (9) */ 1,
57/* ROUTERSOLICIT (10) */ 1,
58/* TIME EXCEEDED (11) */ 1,
59/* PARAMETER PROBLEM (12) */ 1,
60/* TIMESTAMP (13) */ 0,
61/* TIMESTAMP REPLY (14) */ 0,
62/* INFO (15) */ 0,
63/* INFO REPLY (16) */ 0,
64/* ADDR MASK (17) */ 0,
65/* ADDR MASK REPLY (18) */ 0
66};
67
68#ifdef VBOX_WITH_SLIRP_ICMP
69static int
70icmp_attach(PNATState pData, struct socket *so) {
71 AssertRelease(so != NULL);
72 if (pData->icmp_socket == 0)
73 pData->icmp_socket = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
74 AssertRelease(pData->icmp_socket != -1);
75 so->s = pData->icmp_socket;
76 return (so->s);
77}
78#endif /* VBOX_WITH_SLIRP_ICMP */
79
80/*
81 * Process a received ICMP message.
82 */
83void
84icmp_input(PNATState pData, struct mbuf *m, int hlen)
85{
86 register struct icmp *icp;
87 register struct ip *ip=mtod(m, struct ip *);
88 int icmplen=ip->ip_len;
89 /* int code; */
90
91 DEBUG_CALL("icmp_input");
92 DEBUG_ARG("m = %lx", (long )m);
93 DEBUG_ARG("m_len = %d", m->m_len);
94
95 icmpstat.icps_received++;
96
97 /*
98 * Locate icmp structure in mbuf, and check
99 * that its not corrupted and of at least minimum length.
100 */
101 if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
102 icmpstat.icps_tooshort++;
103 freeit:
104 m_freem(pData, m);
105 goto end_error;
106 }
107
108 m->m_len -= hlen;
109 m->m_data += hlen;
110 icp = mtod(m, struct icmp *);
111 if (cksum(m, icmplen)) {
112 icmpstat.icps_checksum++;
113 goto freeit;
114 }
115 m->m_len += hlen;
116 m->m_data -= hlen;
117
118 /* icmpstat.icps_inhist[icp->icmp_type]++; */
119 /* code = icp->icmp_code; */
120
121 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
122 switch (icp->icmp_type) {
123 case ICMP_ECHO:
124 icp->icmp_type = ICMP_ECHOREPLY;
125 ip->ip_len += hlen; /* since ip_input subtracts this */
126 if (ip->ip_dst.s_addr == alias_addr.s_addr) {
127 icmp_reflect(pData, m);
128 } else {
129 struct socket *so;
130 struct sockaddr_in addr;
131 if ((so = socreate()) == NULL) goto freeit;
132#ifndef VBOX_WITH_SLIRP_ICMP
133 if(udp_attach(pData, so) == -1) {
134#else
135 if(icmp_attach(pData, so) == -1) {
136#endif
137 DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
138 errno,strerror(errno)));
139 sofree(pData, so);
140 m_free(pData, m);
141 goto end_error;
142 }
143 so->so_m = m;
144 so->so_faddr = ip->ip_dst;
145 so->so_fport = htons(7);
146 so->so_laddr = ip->ip_src;
147 so->so_lport = htons(9);
148 so->so_iptos = ip->ip_tos;
149 so->so_type = IPPROTO_ICMP;
150 so->so_state = SS_ISFCONNECTED;
151
152 /* Send the packet */
153 addr.sin_family = AF_INET;
154 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr) {
155 /* It's an alias */
156 switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask) {
157 case CTL_DNS:
158 addr.sin_addr = dns_addr;
159 break;
160 case CTL_ALIAS:
161 default:
162 addr.sin_addr = loopback_addr;
163 break;
164 }
165 } else {
166 addr.sin_addr = so->so_faddr;
167 }
168 addr.sin_port = so->so_fport;
169 if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
170 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
171 DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
172 errno,strerror(errno)));
173 icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
174 udp_detach(pData, so);
175 }
176 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
177 break;
178 case ICMP_UNREACH:
179 /* XXX? report error? close socket? */
180 case ICMP_TIMXCEED:
181 case ICMP_PARAMPROB:
182 case ICMP_SOURCEQUENCH:
183 case ICMP_TSTAMP:
184 case ICMP_MASKREQ:
185 case ICMP_REDIRECT:
186 icmpstat.icps_notsupp++;
187 m_freem(pData, m);
188 break;
189
190 default:
191 icmpstat.icps_badtype++;
192 m_freem(pData, m);
193 } /* swith */
194
195end_error:
196 /* m is m_free()'d xor put in a socket xor or given to ip_send */
197 return;
198}
199
200
201/*
202 * Send an ICMP message in response to a situation
203 *
204 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
205 * MUST NOT change this header information.
206 * MUST NOT reply to a multicast/broadcast IP address.
207 * MUST NOT reply to a multicast/broadcast MAC address.
208 * MUST reply to only the first fragment.
209 */
210/*
211 * Send ICMP_UNREACH back to the source regarding msrc.
212 * mbuf *msrc is used as a template, but is NOT m_free()'d.
213 * It is reported as the bad ip packet. The header should
214 * be fully correct and in host byte order.
215 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
216 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
217 */
218
219#define ICMP_MAXDATALEN (IP_MSS-28)
220void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, char *message)
221{
222 unsigned hlen, shlen, s_ip_len;
223 register struct ip *ip;
224 register struct icmp *icp;
225 register struct mbuf *m;
226
227 DEBUG_CALL("icmp_error");
228 DEBUG_ARG("msrc = %lx", (long )msrc);
229 DEBUG_ARG("msrc_len = %d", msrc->m_len);
230
231 if(type!=ICMP_UNREACH && type!=ICMP_TIMXCEED) goto end_error;
232
233 /* check msrc */
234 if(!msrc) goto end_error;
235 ip = mtod(msrc, struct ip *);
236#if DEBUG
237 { char bufa[20], bufb[20];
238 strcpy(bufa, inet_ntoa(ip->ip_src));
239 strcpy(bufb, inet_ntoa(ip->ip_dst));
240 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
241 }
242#endif
243 if(ip->ip_off & IP_OFFMASK) goto end_error; /* Only reply to fragment 0 */
244
245 shlen=ip->ip_hl << 2;
246 s_ip_len=ip->ip_len;
247 if(ip->ip_p == IPPROTO_ICMP) {
248 icp = (struct icmp *)((char *)ip + shlen);
249 /*
250 * Assume any unknown ICMP type is an error. This isn't
251 * specified by the RFC, but think about it..
252 */
253 if(icp->icmp_type>18 || icmp_flush[icp->icmp_type]) goto end_error;
254 }
255
256 /* make a copy */
257 if(!(m=m_get(pData))) goto end_error; /* get mbuf */
258 { int new_m_size;
259 new_m_size=sizeof(struct ip )+ICMP_MINLEN+msrc->m_len+ICMP_MAXDATALEN;
260 if(new_m_size>m->m_size) m_inc(m, new_m_size);
261 }
262 memcpy(m->m_data, msrc->m_data, msrc->m_len);
263 m->m_len = msrc->m_len; /* copy msrc to m */
264
265 /* make the header of the reply packet */
266 ip = mtod(m, struct ip *);
267 hlen= sizeof(struct ip ); /* no options in reply */
268
269 /* fill in icmp */
270 m->m_data += hlen;
271 m->m_len -= hlen;
272
273 icp = mtod(m, struct icmp *);
274
275 if(minsize) s_ip_len=shlen+ICMP_MINLEN; /* return header+8b only */
276 else if(s_ip_len>ICMP_MAXDATALEN) /* maximum size */
277 s_ip_len=ICMP_MAXDATALEN;
278
279 m->m_len=ICMP_MINLEN+s_ip_len; /* 8 bytes ICMP header */
280
281 /* min. size = 8+sizeof(struct ip)+8 */
282
283 icp->icmp_type = type;
284 icp->icmp_code = code;
285 icp->icmp_id = 0;
286 icp->icmp_seq = 0;
287
288 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
289 HTONS(icp->icmp_ip.ip_len);
290 HTONS(icp->icmp_ip.ip_id);
291 HTONS(icp->icmp_ip.ip_off);
292
293#if DEBUG
294 if(message) { /* DEBUG : append message to ICMP packet */
295 int message_len;
296 char *cpnt;
297 message_len=strlen(message);
298 if(message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
299 cpnt=(char *)m->m_data+m->m_len;
300 memcpy(cpnt, message, message_len);
301 m->m_len+=message_len;
302 }
303#endif
304
305 icp->icmp_cksum = 0;
306 icp->icmp_cksum = cksum(m, m->m_len);
307
308 m->m_data -= hlen;
309 m->m_len += hlen;
310
311 /* fill in ip */
312 ip->ip_hl = hlen >> 2;
313 ip->ip_len = m->m_len;
314
315 ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
316
317 ip->ip_ttl = MAXTTL;
318 ip->ip_p = IPPROTO_ICMP;
319 ip->ip_dst = ip->ip_src; /* ip adresses */
320 ip->ip_src = alias_addr;
321
322 (void ) ip_output(pData, (struct socket *)NULL, m);
323
324 icmpstat.icps_reflect++;
325
326end_error:
327 return;
328}
329#undef ICMP_MAXDATALEN
330
331/*
332 * Reflect the ip packet back to the source
333 */
334void
335icmp_reflect(PNATState pData, struct mbuf *m)
336{
337 register struct ip *ip = mtod(m, struct ip *);
338 int hlen = ip->ip_hl << 2;
339 int optlen = hlen - sizeof(struct ip );
340 register struct icmp *icp;
341
342 /*
343 * Send an icmp packet back to the ip level,
344 * after supplying a checksum.
345 */
346 m->m_data += hlen;
347 m->m_len -= hlen;
348 icp = mtod(m, struct icmp *);
349
350 icp->icmp_cksum = 0;
351 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
352
353 m->m_data -= hlen;
354 m->m_len += hlen;
355
356 /* fill in ip */
357 if (optlen > 0) {
358 /*
359 * Strip out original options by copying rest of first
360 * mbuf's data back, and adjust the IP length.
361 */
362 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
363 (unsigned )(m->m_len - hlen));
364 hlen -= optlen;
365 ip->ip_hl = hlen >> 2;
366 ip->ip_len -= optlen;
367 m->m_len -= optlen;
368 }
369
370 ip->ip_ttl = MAXTTL;
371 { /* swap */
372 struct in_addr icmp_dst;
373 icmp_dst = ip->ip_dst;
374 ip->ip_dst = ip->ip_src;
375 ip->ip_src = icmp_dst;
376 }
377
378 (void ) ip_output(pData, (struct socket *)NULL, m);
379
380 icmpstat.icps_reflect++;
381}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette