VirtualBox

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

Last change on this file since 1076 was 1076, checked in by vboxsync, 18 years ago

Removed tons of ifdef VBOX conditionals to make slirp readable again

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