blob: 4b2c3be49bcea7f860ee4af27498aec3c5cf6ad3 [file] [log] [blame]
bellard29aca442004-04-22 00:10:48 +00001/*
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.
aliguori3bac3912009-01-26 19:37:41 +000013 * 3. Neither the name of the University nor the names of its contributors
bellard29aca442004-04-22 00:10:48 +000014 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
30 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
31 */
32
33#include "slirp.h"
34#include "ip_icmp.h"
35
bellard29aca442004-04-22 00:10:48 +000036/* The message sent when emulating PING */
blueswir1412c9f42007-10-26 19:34:46 +000037/* Be nice and tell them it's just a pseudo-ping packet */
blueswir1f7c64082008-10-01 19:06:48 +000038static const char icmp_ping_msg[] =
39 "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST "
40 "packets.\n";
bellard29aca442004-04-22 00:10:48 +000041
42/* list of actions for icmp_error() on RX of an icmp message */
blueswir1527e33c2007-10-26 19:01:16 +000043static const int icmp_flush[19] = {
bellard29aca442004-04-22 00:10:48 +000044 /* ECHO REPLY (0) */ 0,
45 1,
46 1,
47 /* DEST UNREACH (3) */ 1,
48 /* SOURCE QUENCH (4)*/ 1,
49 /* REDIRECT (5) */ 1,
50 1,
51 1,
52 /* ECHO (8) */ 0,
53 /* ROUTERADVERT (9) */ 1,
54 /* ROUTERSOLICIT (10) */ 1,
55 /* TIME EXCEEDED (11) */ 1,
56 /* PARAMETER PROBLEM (12) */ 1,
57 /* TIMESTAMP (13) */ 0,
58 /* TIMESTAMP REPLY (14) */ 0,
59 /* INFO (15) */ 0,
60 /* INFO REPLY (16) */ 0,
61 /* ADDR MASK (17) */ 0,
62 /* ADDR MASK REPLY (18) */ 0
63};
64
Jan Kiszka68a34002011-07-20 12:20:18 +020065void icmp_init(Slirp *slirp)
66{
67 slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;
68 slirp->icmp_last_so = &slirp->icmp;
69}
70
71static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
72{
73 struct ip *ip = mtod(m, struct ip *);
74 struct sockaddr_in addr;
75
76 so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
77 if (so->s == -1) {
78 return -1;
79 }
80
81 so->so_m = m;
82 so->so_faddr = ip->ip_dst;
83 so->so_laddr = ip->ip_src;
84 so->so_iptos = ip->ip_tos;
85 so->so_type = IPPROTO_ICMP;
86 so->so_state = SS_ISFCONNECTED;
87 so->so_expire = curtime + SO_EXPIRE;
88
89 addr.sin_family = AF_INET;
90 addr.sin_addr = so->so_faddr;
91
92 insque(so, &so->slirp->icmp);
93
94 if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
95 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
96 DEBUG_MISC((dfd, "icmp_input icmp sendto tx errno = %d-%s\n", errno,
97 strerror(errno)));
98 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
99 icmp_detach(so);
100 }
101
102 return 0;
103}
104
105void icmp_detach(struct socket *so)
106{
107 closesocket(so->s);
108 sofree(so);
109}
110
bellard29aca442004-04-22 00:10:48 +0000111/*
112 * Process a received ICMP message.
113 */
blueswir15b0221f2009-03-07 15:32:56 +0000114void icmp_input(struct mbuf *m, int hlen)
bellard29aca442004-04-22 00:10:48 +0000115{
116 register struct icmp *icp;
117 register struct ip *ip = mtod(m, struct ip *);
118 int icmplen = ip->ip_len;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200119 Slirp *slirp = m->slirp;
bellard29aca442004-04-22 00:10:48 +0000120
121 DEBUG_CALL("icmp_input");
122 DEBUG_ARG("m = %lx", (long)m);
123 DEBUG_ARG("m_len = %d", m->m_len);
124
bellard29aca442004-04-22 00:10:48 +0000125 /*
126 * Locate icmp structure in mbuf, and check
127 * that its not corrupted and of at least minimum length.
128 */
129 if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
bellard29aca442004-04-22 00:10:48 +0000130 freeit:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200131 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000132 goto end_error;
133 }
134
135 m->m_len -= hlen;
136 m->m_data += hlen;
137 icp = mtod(m, struct icmp *);
138 if (cksum(m, icmplen)) {
bellard29aca442004-04-22 00:10:48 +0000139 goto freeit;
140 }
141 m->m_len += hlen;
142 m->m_data -= hlen;
143
bellard29aca442004-04-22 00:10:48 +0000144 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
145 switch (icp->icmp_type) {
146 case ICMP_ECHO:
bellard29aca442004-04-22 00:10:48 +0000147 ip->ip_len += hlen; /* since ip_input subtracts this */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200148 if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
bellard29aca442004-04-22 00:10:48 +0000149 icmp_reflect(m);
Jan Kiszka6c5f2202011-07-20 12:20:13 +0200150 } else if (slirp->restricted) {
151 goto freeit;
bellard29aca442004-04-22 00:10:48 +0000152 } else {
153 struct socket *so;
154 struct sockaddr_in addr;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200155 if ((so = socreate(slirp)) == NULL)
bellard29aca442004-04-22 00:10:48 +0000156 goto freeit;
Jan Kiszka68a34002011-07-20 12:20:18 +0200157 if (icmp_send(so, m, hlen) == 0) {
158 return;
159 }
bellard29aca442004-04-22 00:10:48 +0000160 if (udp_attach(so) == -1) {
161 DEBUG_MISC((dfd, "icmp_input udp_attach errno = %d-%s\n", errno,
162 strerror(errno)));
163 sofree(so);
164 m_free(m);
165 goto end_error;
166 }
167 so->so_m = m;
168 so->so_faddr = ip->ip_dst;
169 so->so_fport = htons(7);
170 so->so_laddr = ip->ip_src;
171 so->so_lport = htons(9);
172 so->so_iptos = ip->ip_tos;
173 so->so_type = IPPROTO_ICMP;
174 so->so_state = SS_ISFCONNECTED;
175
176 /* Send the packet */
177 addr.sin_family = AF_INET;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200178 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
179 slirp->vnetwork_addr.s_addr) {
bellard29aca442004-04-22 00:10:48 +0000180 /* It's an alias */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200181 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
Ed Swierkce220b62009-08-20 19:00:31 -0700182 if (get_dns_addr(&addr.sin_addr) < 0)
183 addr.sin_addr = loopback_addr;
Jan Kiszka12531b42009-06-24 14:42:28 +0200184 } else {
bellard29aca442004-04-22 00:10:48 +0000185 addr.sin_addr = loopback_addr;
bellard29aca442004-04-22 00:10:48 +0000186 }
187 } else {
188 addr.sin_addr = so->so_faddr;
189 }
190 addr.sin_port = so->so_fport;
191 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
192 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
193 DEBUG_MISC((dfd, "icmp_input udp sendto tx errno = %d-%s\n",
194 errno, strerror(errno)));
195 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
196 strerror(errno));
197 udp_detach(so);
198 }
bellard299cfa92006-05-03 19:58:17 +0000199 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
bellard29aca442004-04-22 00:10:48 +0000200 break;
201 case ICMP_UNREACH:
202 /* XXX? report error? close socket? */
203 case ICMP_TIMXCEED:
204 case ICMP_PARAMPROB:
205 case ICMP_SOURCEQUENCH:
206 case ICMP_TSTAMP:
207 case ICMP_MASKREQ:
208 case ICMP_REDIRECT:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200209 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000210 break;
211
212 default:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200213 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000214 } /* swith */
215
216end_error:
217 /* m is m_free()'d xor put in a socket xor or given to ip_send */
218 return;
219}
220
221
222/*
223 * Send an ICMP message in response to a situation
224 *
225 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
226 *MAY send more (we do). MUST NOT change this header information. MUST NOT reply
227 *to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast
228 *MAC address. MUST reply to only the first fragment.
229 */
230/*
231 * Send ICMP_UNREACH back to the source regarding msrc.
232 * mbuf *msrc is used as a template, but is NOT m_free()'d.
233 * It is reported as the bad ip packet. The header should
234 * be fully correct and in host byte order.
235 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
236 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
237 */
238
239#define ICMP_MAXDATALEN (IP_MSS - 28)
blueswir18078f0a2008-09-14 06:45:34 +0000240void icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
241 const char *message)
bellard29aca442004-04-22 00:10:48 +0000242{
243 unsigned hlen, shlen, s_ip_len;
244 register struct ip *ip;
245 register struct icmp *icp;
246 register struct mbuf *m;
247
248 DEBUG_CALL("icmp_error");
249 DEBUG_ARG("msrc = %lx", (long)msrc);
250 DEBUG_ARG("msrc_len = %d", msrc->m_len);
251
252 if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)
253 goto end_error;
254
255 /* check msrc */
256 if (!msrc)
257 goto end_error;
258 ip = mtod(msrc, struct ip *);
blueswir1602eb942008-09-06 17:47:39 +0000259#ifdef DEBUG
bellard29aca442004-04-22 00:10:48 +0000260 {
261 char bufa[20], bufb[20];
262 strcpy(bufa, inet_ntoa(ip->ip_src));
263 strcpy(bufb, inet_ntoa(ip->ip_dst));
264 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
265 }
266#endif
267 if (ip->ip_off & IP_OFFMASK)
268 goto end_error; /* Only reply to fragment 0 */
269
270 shlen = ip->ip_hl << 2;
271 s_ip_len = ip->ip_len;
272 if (ip->ip_p == IPPROTO_ICMP) {
273 icp = (struct icmp *)((char *)ip + shlen);
274 /*
275 * Assume any unknown ICMP type is an error. This isn't
276 * specified by the RFC, but think about it..
277 */
278 if (icp->icmp_type > 18 || icmp_flush[icp->icmp_type])
279 goto end_error;
280 }
281
282 /* make a copy */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200283 m = m_get(msrc->slirp);
284 if (!m) {
285 goto end_error;
286 }
287
bellard29aca442004-04-22 00:10:48 +0000288 {
289 int new_m_size;
290 new_m_size =
291 sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
292 if (new_m_size > m->m_size)
293 m_inc(m, new_m_size);
294 }
295 memcpy(m->m_data, msrc->m_data, msrc->m_len);
296 m->m_len = msrc->m_len; /* copy msrc to m */
297
298 /* make the header of the reply packet */
299 ip = mtod(m, struct ip *);
300 hlen = sizeof(struct ip); /* no options in reply */
301
302 /* fill in icmp */
303 m->m_data += hlen;
304 m->m_len -= hlen;
305
306 icp = mtod(m, struct icmp *);
307
308 if (minsize)
309 s_ip_len = shlen + ICMP_MINLEN; /* return header+8b only */
310 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
311 s_ip_len = ICMP_MAXDATALEN;
312
313 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
314
315 /* min. size = 8+sizeof(struct ip)+8 */
316
317 icp->icmp_type = type;
318 icp->icmp_code = code;
319 icp->icmp_id = 0;
320 icp->icmp_seq = 0;
321
322 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
323 HTONS(icp->icmp_ip.ip_len);
324 HTONS(icp->icmp_ip.ip_id);
325 HTONS(icp->icmp_ip.ip_off);
326
blueswir1602eb942008-09-06 17:47:39 +0000327#ifdef DEBUG
bellard29aca442004-04-22 00:10:48 +0000328 if (message) { /* DEBUG : append message to ICMP packet */
329 int message_len;
330 char *cpnt;
331 message_len = strlen(message);
332 if (message_len > ICMP_MAXDATALEN)
333 message_len = ICMP_MAXDATALEN;
334 cpnt = (char *)m->m_data + m->m_len;
335 memcpy(cpnt, message, message_len);
336 m->m_len += message_len;
337 }
338#endif
339
340 icp->icmp_cksum = 0;
341 icp->icmp_cksum = cksum(m, m->m_len);
342
343 m->m_data -= hlen;
344 m->m_len += hlen;
345
346 /* fill in ip */
347 ip->ip_hl = hlen >> 2;
348 ip->ip_len = m->m_len;
349
350 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
351
352 ip->ip_ttl = MAXTTL;
353 ip->ip_p = IPPROTO_ICMP;
354 ip->ip_dst = ip->ip_src; /* ip adresses */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200355 ip->ip_src = m->slirp->vhost_addr;
bellard29aca442004-04-22 00:10:48 +0000356
357 (void)ip_output((struct socket *)NULL, m);
358
bellard29aca442004-04-22 00:10:48 +0000359end_error:
360 return;
361}
362#undef ICMP_MAXDATALEN
363
364/*
365 * Reflect the ip packet back to the source
366 */
blueswir15b0221f2009-03-07 15:32:56 +0000367void icmp_reflect(struct mbuf *m)
bellard29aca442004-04-22 00:10:48 +0000368{
369 register struct ip *ip = mtod(m, struct ip *);
370 int hlen = ip->ip_hl << 2;
371 int optlen = hlen - sizeof(struct ip);
372 register struct icmp *icp;
373
374 /*
375 * Send an icmp packet back to the ip level,
376 * after supplying a checksum.
377 */
378 m->m_data += hlen;
379 m->m_len -= hlen;
380 icp = mtod(m, struct icmp *);
381
Jan Kiszka68a34002011-07-20 12:20:18 +0200382 icp->icmp_type = ICMP_ECHOREPLY;
bellard29aca442004-04-22 00:10:48 +0000383 icp->icmp_cksum = 0;
384 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
385
386 m->m_data -= hlen;
387 m->m_len += hlen;
388
389 /* fill in ip */
390 if (optlen > 0) {
391 /*
392 * Strip out original options by copying rest of first
393 * mbuf's data back, and adjust the IP length.
394 */
395 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
396 (unsigned)(m->m_len - hlen));
397 hlen -= optlen;
398 ip->ip_hl = hlen >> 2;
399 ip->ip_len -= optlen;
400 m->m_len -= optlen;
401 }
402
403 ip->ip_ttl = MAXTTL;
404 { /* swap */
405 struct in_addr icmp_dst;
406 icmp_dst = ip->ip_dst;
407 ip->ip_dst = ip->ip_src;
408 ip->ip_src = icmp_dst;
409 }
410
411 (void)ip_output((struct socket *)NULL, m);
bellard29aca442004-04-22 00:10:48 +0000412}
Jan Kiszka68a34002011-07-20 12:20:18 +0200413
414void icmp_receive(struct socket *so)
415{
416 struct mbuf *m = so->so_m;
417 struct ip *ip = mtod(m, struct ip *);
418 int hlen = ip->ip_hl << 2;
419 u_char error_code;
420 struct icmp *icp;
421 int id, len;
422
423 m->m_data += hlen;
424 m->m_len -= hlen;
425 icp = mtod(m, struct icmp *);
426
427 id = icp->icmp_id;
Blue Swirlccd09ef2011-07-23 20:04:29 +0000428 len = qemu_recv(so->s, icp, m->m_len, 0);
Jan Kiszka68a34002011-07-20 12:20:18 +0200429 icp->icmp_id = id;
430
431 m->m_data -= hlen;
432 m->m_len += hlen;
433
434 if (len == -1 || len == 0) {
435 if (errno == ENETUNREACH) {
436 error_code = ICMP_UNREACH_NET;
437 } else {
438 error_code = ICMP_UNREACH_HOST;
439 }
440 DEBUG_MISC(
441 (dfd, " udp icmp rx errno = %d-%s\n", errno, strerror(errno)));
442 icmp_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
443 } else {
444 icmp_reflect(so->so_m);
445 so->so_m = NULL; /* Don't m_free() it again! */
446 }
447 icmp_detach(so);
448}