VirtualBox

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

Last change on this file since 5291 was 5291, checked in by vboxsync, 17 years ago

Fox memory leak when suppressing NAT ping to outside IP addresses.

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