blob: 1f51edf0efe316b6eac763907d356bc9c99dc8ab [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
Jan Kiszka1b4a91f2012-02-29 19:14:23 +010071void icmp_cleanup(Slirp *slirp)
72{
73 while (slirp->icmp.so_next != &slirp->icmp) {
74 icmp_detach(slirp->icmp.so_next);
75 }
76}
77
Jan Kiszka68a34002011-07-20 12:20:18 +020078static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
79{
80 struct ip *ip = mtod(m, struct ip *);
81 struct sockaddr_in addr;
82
83 so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
84 if (so->s == -1) {
85 return -1;
86 }
87
88 so->so_m = m;
89 so->so_faddr = ip->ip_dst;
90 so->so_laddr = ip->ip_src;
91 so->so_iptos = ip->ip_tos;
92 so->so_type = IPPROTO_ICMP;
93 so->so_state = SS_ISFCONNECTED;
94 so->so_expire = curtime + SO_EXPIRE;
95
96 addr.sin_family = AF_INET;
97 addr.sin_addr = so->so_faddr;
98
99 insque(so, &so->slirp->icmp);
100
101 if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
102 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
103 DEBUG_MISC((dfd, "icmp_input icmp sendto tx errno = %d-%s\n", errno,
104 strerror(errno)));
105 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
106 icmp_detach(so);
107 }
108
109 return 0;
110}
111
112void icmp_detach(struct socket *so)
113{
114 closesocket(so->s);
115 sofree(so);
116}
117
bellard29aca442004-04-22 00:10:48 +0000118/*
119 * Process a received ICMP message.
120 */
blueswir15b0221f2009-03-07 15:32:56 +0000121void icmp_input(struct mbuf *m, int hlen)
bellard29aca442004-04-22 00:10:48 +0000122{
123 register struct icmp *icp;
124 register struct ip *ip = mtod(m, struct ip *);
125 int icmplen = ip->ip_len;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200126 Slirp *slirp = m->slirp;
bellard29aca442004-04-22 00:10:48 +0000127
128 DEBUG_CALL("icmp_input");
Stefan Weilf75c02c2015-08-29 09:12:35 +0200129 DEBUG_ARG("m = %p", m);
bellard29aca442004-04-22 00:10:48 +0000130 DEBUG_ARG("m_len = %d", m->m_len);
131
bellard29aca442004-04-22 00:10:48 +0000132 /*
133 * Locate icmp structure in mbuf, and check
134 * that its not corrupted and of at least minimum length.
135 */
136 if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
bellard29aca442004-04-22 00:10:48 +0000137 freeit:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200138 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000139 goto end_error;
140 }
141
142 m->m_len -= hlen;
143 m->m_data += hlen;
144 icp = mtod(m, struct icmp *);
145 if (cksum(m, icmplen)) {
bellard29aca442004-04-22 00:10:48 +0000146 goto freeit;
147 }
148 m->m_len += hlen;
149 m->m_data -= hlen;
150
bellard29aca442004-04-22 00:10:48 +0000151 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
152 switch (icp->icmp_type) {
153 case ICMP_ECHO:
bellard29aca442004-04-22 00:10:48 +0000154 ip->ip_len += hlen; /* since ip_input subtracts this */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200155 if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
bellard29aca442004-04-22 00:10:48 +0000156 icmp_reflect(m);
Jan Kiszka6c5f2202011-07-20 12:20:13 +0200157 } else if (slirp->restricted) {
158 goto freeit;
bellard29aca442004-04-22 00:10:48 +0000159 } else {
160 struct socket *so;
161 struct sockaddr_in addr;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200162 if ((so = socreate(slirp)) == NULL)
bellard29aca442004-04-22 00:10:48 +0000163 goto freeit;
Jan Kiszka68a34002011-07-20 12:20:18 +0200164 if (icmp_send(so, m, hlen) == 0) {
165 return;
166 }
bellard29aca442004-04-22 00:10:48 +0000167 if (udp_attach(so) == -1) {
168 DEBUG_MISC((dfd, "icmp_input udp_attach errno = %d-%s\n", errno,
169 strerror(errno)));
170 sofree(so);
171 m_free(m);
172 goto end_error;
173 }
174 so->so_m = m;
175 so->so_faddr = ip->ip_dst;
176 so->so_fport = htons(7);
177 so->so_laddr = ip->ip_src;
178 so->so_lport = htons(9);
179 so->so_iptos = ip->ip_tos;
180 so->so_type = IPPROTO_ICMP;
181 so->so_state = SS_ISFCONNECTED;
182
183 /* Send the packet */
184 addr.sin_family = AF_INET;
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200185 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
186 slirp->vnetwork_addr.s_addr) {
bellard29aca442004-04-22 00:10:48 +0000187 /* It's an alias */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200188 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
Ed Swierkce220b62009-08-20 19:00:31 -0700189 if (get_dns_addr(&addr.sin_addr) < 0)
190 addr.sin_addr = loopback_addr;
Jan Kiszka12531b42009-06-24 14:42:28 +0200191 } else {
bellard29aca442004-04-22 00:10:48 +0000192 addr.sin_addr = loopback_addr;
bellard29aca442004-04-22 00:10:48 +0000193 }
194 } else {
195 addr.sin_addr = so->so_faddr;
196 }
197 addr.sin_port = so->so_fport;
198 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
199 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
200 DEBUG_MISC((dfd, "icmp_input udp sendto tx errno = %d-%s\n",
201 errno, strerror(errno)));
202 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
203 strerror(errno));
204 udp_detach(so);
205 }
bellard299cfa92006-05-03 19:58:17 +0000206 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
bellard29aca442004-04-22 00:10:48 +0000207 break;
208 case ICMP_UNREACH:
209 /* XXX? report error? close socket? */
210 case ICMP_TIMXCEED:
211 case ICMP_PARAMPROB:
212 case ICMP_SOURCEQUENCH:
213 case ICMP_TSTAMP:
214 case ICMP_MASKREQ:
215 case ICMP_REDIRECT:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200216 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000217 break;
218
219 default:
Jan Kiszkab0f74a42011-07-20 12:20:16 +0200220 m_free(m);
bellard29aca442004-04-22 00:10:48 +0000221 } /* swith */
222
223end_error:
224 /* m is m_free()'d xor put in a socket xor or given to ip_send */
225 return;
226}
227
228
229/*
230 * Send an ICMP message in response to a situation
231 *
232 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
233 *MAY send more (we do). MUST NOT change this header information. MUST NOT reply
234 *to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast
235 *MAC address. MUST reply to only the first fragment.
236 */
237/*
238 * Send ICMP_UNREACH back to the source regarding msrc.
239 * mbuf *msrc is used as a template, but is NOT m_free()'d.
240 * It is reported as the bad ip packet. The header should
241 * be fully correct and in host byte order.
242 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
243 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
244 */
245
246#define ICMP_MAXDATALEN (IP_MSS - 28)
blueswir18078f0a2008-09-14 06:45:34 +0000247void icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
248 const char *message)
bellard29aca442004-04-22 00:10:48 +0000249{
250 unsigned hlen, shlen, s_ip_len;
251 register struct ip *ip;
252 register struct icmp *icp;
253 register struct mbuf *m;
254
255 DEBUG_CALL("icmp_error");
Stefan Weilf75c02c2015-08-29 09:12:35 +0200256 DEBUG_ARG("msrc = %p", msrc);
bellard29aca442004-04-22 00:10:48 +0000257 DEBUG_ARG("msrc_len = %d", msrc->m_len);
258
259 if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)
260 goto end_error;
261
262 /* check msrc */
263 if (!msrc)
264 goto end_error;
265 ip = mtod(msrc, struct ip *);
blueswir1602eb942008-09-06 17:47:39 +0000266#ifdef DEBUG
bellard29aca442004-04-22 00:10:48 +0000267 {
268 char bufa[20], bufb[20];
269 strcpy(bufa, inet_ntoa(ip->ip_src));
270 strcpy(bufb, inet_ntoa(ip->ip_dst));
271 DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
272 }
273#endif
274 if (ip->ip_off & IP_OFFMASK)
275 goto end_error; /* Only reply to fragment 0 */
276
Jan Kiszka634caa32012-02-08 10:05:45 +0100277 /* Do not reply to source-only IPs */
278 if ((ip->ip_src.s_addr & htonl(~(0xf << 28))) == 0) {
279 goto end_error;
280 }
281
bellard29aca442004-04-22 00:10:48 +0000282 shlen = ip->ip_hl << 2;
283 s_ip_len = ip->ip_len;
284 if (ip->ip_p == IPPROTO_ICMP) {
285 icp = (struct icmp *)((char *)ip + shlen);
286 /*
287 * Assume any unknown ICMP type is an error. This isn't
288 * specified by the RFC, but think about it..
289 */
290 if (icp->icmp_type > 18 || icmp_flush[icp->icmp_type])
291 goto end_error;
292 }
293
294 /* make a copy */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200295 m = m_get(msrc->slirp);
296 if (!m) {
297 goto end_error;
298 }
299
bellard29aca442004-04-22 00:10:48 +0000300 {
301 int new_m_size;
302 new_m_size =
303 sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
304 if (new_m_size > m->m_size)
305 m_inc(m, new_m_size);
306 }
307 memcpy(m->m_data, msrc->m_data, msrc->m_len);
308 m->m_len = msrc->m_len; /* copy msrc to m */
309
310 /* make the header of the reply packet */
311 ip = mtod(m, struct ip *);
312 hlen = sizeof(struct ip); /* no options in reply */
313
314 /* fill in icmp */
315 m->m_data += hlen;
316 m->m_len -= hlen;
317
318 icp = mtod(m, struct icmp *);
319
320 if (minsize)
321 s_ip_len = shlen + ICMP_MINLEN; /* return header+8b only */
322 else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
323 s_ip_len = ICMP_MAXDATALEN;
324
325 m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
326
327 /* min. size = 8+sizeof(struct ip)+8 */
328
329 icp->icmp_type = type;
330 icp->icmp_code = code;
331 icp->icmp_id = 0;
332 icp->icmp_seq = 0;
333
334 memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
335 HTONS(icp->icmp_ip.ip_len);
336 HTONS(icp->icmp_ip.ip_id);
337 HTONS(icp->icmp_ip.ip_off);
338
blueswir1602eb942008-09-06 17:47:39 +0000339#ifdef DEBUG
bellard29aca442004-04-22 00:10:48 +0000340 if (message) { /* DEBUG : append message to ICMP packet */
341 int message_len;
342 char *cpnt;
343 message_len = strlen(message);
344 if (message_len > ICMP_MAXDATALEN)
345 message_len = ICMP_MAXDATALEN;
346 cpnt = (char *)m->m_data + m->m_len;
347 memcpy(cpnt, message, message_len);
348 m->m_len += message_len;
349 }
350#endif
351
352 icp->icmp_cksum = 0;
353 icp->icmp_cksum = cksum(m, m->m_len);
354
355 m->m_data -= hlen;
356 m->m_len += hlen;
357
358 /* fill in ip */
359 ip->ip_hl = hlen >> 2;
360 ip->ip_len = m->m_len;
361
362 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
363
364 ip->ip_ttl = MAXTTL;
365 ip->ip_p = IPPROTO_ICMP;
Stefan Weil2c69e1d2012-11-02 08:29:53 +0100366 ip->ip_dst = ip->ip_src; /* ip addresses */
Jan Kiszkab3bbd4e2009-06-24 14:42:31 +0200367 ip->ip_src = m->slirp->vhost_addr;
bellard29aca442004-04-22 00:10:48 +0000368
369 (void)ip_output((struct socket *)NULL, m);
370
bellard29aca442004-04-22 00:10:48 +0000371end_error:
372 return;
373}
374#undef ICMP_MAXDATALEN
375
376/*
377 * Reflect the ip packet back to the source
378 */
blueswir15b0221f2009-03-07 15:32:56 +0000379void icmp_reflect(struct mbuf *m)
bellard29aca442004-04-22 00:10:48 +0000380{
381 register struct ip *ip = mtod(m, struct ip *);
382 int hlen = ip->ip_hl << 2;
383 int optlen = hlen - sizeof(struct ip);
384 register struct icmp *icp;
385
386 /*
387 * Send an icmp packet back to the ip level,
388 * after supplying a checksum.
389 */
390 m->m_data += hlen;
391 m->m_len -= hlen;
392 icp = mtod(m, struct icmp *);
393
Jan Kiszka68a34002011-07-20 12:20:18 +0200394 icp->icmp_type = ICMP_ECHOREPLY;
bellard29aca442004-04-22 00:10:48 +0000395 icp->icmp_cksum = 0;
396 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
397
398 m->m_data -= hlen;
399 m->m_len += hlen;
400
401 /* fill in ip */
402 if (optlen > 0) {
403 /*
404 * Strip out original options by copying rest of first
405 * mbuf's data back, and adjust the IP length.
406 */
407 memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
408 (unsigned)(m->m_len - hlen));
409 hlen -= optlen;
410 ip->ip_hl = hlen >> 2;
411 ip->ip_len -= optlen;
412 m->m_len -= optlen;
413 }
414
415 ip->ip_ttl = MAXTTL;
416 { /* swap */
417 struct in_addr icmp_dst;
418 icmp_dst = ip->ip_dst;
419 ip->ip_dst = ip->ip_src;
420 ip->ip_src = icmp_dst;
421 }
422
423 (void)ip_output((struct socket *)NULL, m);
bellard29aca442004-04-22 00:10:48 +0000424}
Jan Kiszka68a34002011-07-20 12:20:18 +0200425
426void icmp_receive(struct socket *so)
427{
428 struct mbuf *m = so->so_m;
429 struct ip *ip = mtod(m, struct ip *);
430 int hlen = ip->ip_hl << 2;
431 u_char error_code;
432 struct icmp *icp;
433 int id, len;
434
435 m->m_data += hlen;
436 m->m_len -= hlen;
437 icp = mtod(m, struct icmp *);
438
439 id = icp->icmp_id;
Blue Swirlccd09ef2011-07-23 20:04:29 +0000440 len = qemu_recv(so->s, icp, m->m_len, 0);
Jan Kiszka68a34002011-07-20 12:20:18 +0200441 icp->icmp_id = id;
442
443 m->m_data -= hlen;
444 m->m_len += hlen;
445
446 if (len == -1 || len == 0) {
447 if (errno == ENETUNREACH) {
448 error_code = ICMP_UNREACH_NET;
449 } else {
450 error_code = ICMP_UNREACH_HOST;
451 }
452 DEBUG_MISC(
453 (dfd, " udp icmp rx errno = %d-%s\n", errno, strerror(errno)));
454 icmp_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
455 } else {
456 icmp_reflect(so->so_m);
457 so->so_m = NULL; /* Don't m_free() it again! */
458 }
459 icmp_detach(so);
460}