1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3 * Copyright (c) 1982, 1986, 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
31 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
32 */
33
34 #include "slirp.h"
35 #include "ip_icmp.h"
36
37 #ifndef WITH_ICMP_ERROR_MSG
38 #define WITH_ICMP_ERROR_MSG 0
39 #endif
40
41 /* The message sent when emulating PING */
42 /* Be nice and tell them it's just a pseudo-ping packet */
43 static const char icmp_ping_msg[] =
44 "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST "
45 "packets.\n";
46
47 /* list of actions for icmp_send_error() on RX of an icmp message */
48 static const int icmp_flush[19] = {
49 /* ECHO REPLY (0) */ 0,
50 1,
51 1,
52 /* DEST UNREACH (3) */ 1,
53 /* SOURCE QUENCH (4)*/ 1,
54 /* REDIRECT (5) */ 1,
55 1,
56 1,
57 /* ECHO (8) */ 0,
58 /* ROUTERADVERT (9) */ 1,
59 /* ROUTERSOLICIT (10) */ 1,
60 /* TIME EXCEEDED (11) */ 1,
61 /* PARAMETER PROBLEM (12) */ 1,
62 /* TIMESTAMP (13) */ 0,
63 /* TIMESTAMP REPLY (14) */ 0,
64 /* INFO (15) */ 0,
65 /* INFO REPLY (16) */ 0,
66 /* ADDR MASK (17) */ 0,
67 /* ADDR MASK REPLY (18) */ 0
68 };
69
icmp_init(Slirp * slirp)70 void icmp_init(Slirp *slirp)
71 {
72 slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;
73 slirp->icmp_last_so = &slirp->icmp;
74 }
75
icmp_cleanup(Slirp * slirp)76 void icmp_cleanup(Slirp *slirp)
77 {
78 while (slirp->icmp.so_next != &slirp->icmp) {
79 icmp_detach(slirp->icmp.so_next);
80 }
81 }
82
icmp_send(struct socket * so,struct mbuf * m,int hlen)83 static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
84 {
85 struct ip *ip = mtod(m, struct ip *);
86 struct sockaddr_in addr;
87
88 so->s = slirp_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
89 if (so->s == -1) {
90 return -1;
91 }
92
93 so->so_m = m;
94 so->so_faddr = ip->ip_dst;
95 so->so_laddr = ip->ip_src;
96 so->so_iptos = ip->ip_tos;
97 so->so_type = IPPROTO_ICMP;
98 so->so_state = SS_ISFCONNECTED;
99 so->so_expire = curtime + SO_EXPIRE;
100
101 addr.sin_family = AF_INET;
102 addr.sin_addr = so->so_faddr;
103
104 insque(so, &so->slirp->icmp);
105
106 if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
107 (struct sockaddr *)&addr, sizeof(addr)) == -1) {
108 DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s", errno,
109 strerror(errno));
110 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
111 icmp_detach(so);
112 }
113
114 return 0;
115 }
116
icmp_detach(struct socket * so)117 void icmp_detach(struct socket *so)
118 {
119 so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
120 closesocket(so->s);
121 sofree(so);
122 }
123
124 /*
125 * Process a received ICMP message.
126 */
icmp_input(struct mbuf * m,int hlen)127 void icmp_input(struct mbuf *m, int hlen)
128 {
129 register struct icmp *icp;
130 register struct ip *ip = mtod(m, struct ip *);
131 int icmplen = ip->ip_len;
132 Slirp *slirp = m->slirp;
133
134 DEBUG_CALL("icmp_input");
135 DEBUG_ARG("m = %p", m);
136 DEBUG_ARG("m_len = %d", m->m_len);
137
138 /*
139 * Locate icmp structure in mbuf, and check
140 * that its not corrupted and of at least minimum length.
141 */
142 if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
143 freeit:
144 m_free(m);
145 goto end_error;
146 }
147
148 m->m_len -= hlen;
149 m->m_data += hlen;
150 icp = mtod(m, struct icmp *);
151 if (cksum(m, icmplen)) {
152 goto freeit;
153 }
154 m->m_len += hlen;
155 m->m_data -= hlen;
156
157 DEBUG_ARG("icmp_type = %d", icp->icmp_type);
158 switch (icp->icmp_type) {
159 case ICMP_ECHO:
160 ip->ip_len += hlen; /* since ip_input subtracts this */
161 if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
162 ip->ip_dst.s_addr == slirp->vnameserver_addr.s_addr) {
163 icmp_reflect(m);
164 } else if (slirp->restricted) {
165 goto freeit;
166 } else {
167 struct socket *so;
168 struct sockaddr_storage addr;
169 so = socreate(slirp);
170 if (icmp_send(so, m, hlen) == 0) {
171 return;
172 }
173 if (udp_attach(so, AF_INET) == -1) {
174 DEBUG_MISC("icmp_input udp_attach errno = %d-%s", errno,
175 strerror(errno));
176 sofree(so);
177 m_free(m);
178 goto end_error;
179 }
180 so->so_m = m;
181 so->so_ffamily = AF_INET;
182 so->so_faddr = ip->ip_dst;
183 so->so_fport = htons(7);
184 so->so_lfamily = AF_INET;
185 so->so_laddr = ip->ip_src;
186 so->so_lport = htons(9);
187 so->so_iptos = ip->ip_tos;
188 so->so_type = IPPROTO_ICMP;
189 so->so_state = SS_ISFCONNECTED;
190
191 /* Send the packet */
192 addr = so->fhost.ss;
193 if (sotranslate_out(so, &addr) < 0) {
194 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
195 strerror(errno));
196 udp_detach(so);
197 return;
198 }
199
200 if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
201 (struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {
202 DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s", errno,
203 strerror(errno));
204 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
205 strerror(errno));
206 udp_detach(so);
207 }
208 } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
209 break;
210 case ICMP_UNREACH:
211 /* XXX? report error? close socket? */
212 case ICMP_TIMXCEED:
213 case ICMP_PARAMPROB:
214 case ICMP_SOURCEQUENCH:
215 case ICMP_TSTAMP:
216 case ICMP_MASKREQ:
217 case ICMP_REDIRECT:
218 m_free(m);
219 break;
220
221 default:
222 m_free(m);
223 } /* swith */
224
225 end_error:
226 /* m is m_free()'d xor put in a socket xor or given to ip_send */
227 return;
228 }
229
230
231 /*
232 * Send an ICMP message in response to a situation
233 *
234 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
235 *MAY send more (we do). MUST NOT change this header information. MUST NOT reply
236 *to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast
237 *MAC address. MUST reply to only the first fragment.
238 */
239 /*
240 * Send ICMP_UNREACH back to the source regarding msrc.
241 * mbuf *msrc is used as a template, but is NOT m_free()'d.
242 * It is reported as the bad ip packet. The header should
243 * be fully correct and in host byte order.
244 * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
245 * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
246 */
247
248 #define ICMP_MAXDATALEN (IP_MSS - 28)
icmp_send_error(struct mbuf * msrc,uint8_t type,uint8_t code,int minsize,const char * message)249 void icmp_send_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,
250 const char *message)
251 {
252 unsigned hlen, shlen, s_ip_len;
253 register struct ip *ip;
254 register struct icmp *icp;
255 register struct mbuf *m;
256
257 DEBUG_CALL("icmp_send_error");
258 DEBUG_ARG("msrc = %p", msrc);
259 DEBUG_ARG("msrc_len = %d", msrc->m_len);
260
261 if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)
262 goto end_error;
263
264 /* check msrc */
265 if (!msrc)
266 goto end_error;
267 ip = mtod(msrc, struct ip *);
268 if (slirp_debug & DBG_MISC) {
269 char bufa[20], bufb[20];
270 strcpy(bufa, inet_ntoa(ip->ip_src));
271 strcpy(bufb, inet_ntoa(ip->ip_dst));
272 DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
273 }
274 if (ip->ip_off & IP_OFFMASK)
275 goto end_error; /* Only reply to fragment 0 */
276
277 /* Do not reply to source-only IPs */
278 if ((ip->ip_src.s_addr & htonl(~(0xf << 28))) == 0) {
279 goto end_error;
280 }
281
282 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 */
295 m = m_get(msrc->slirp);
296 if (!m) {
297 goto end_error;
298 }
299
300 {
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
339 if (message && WITH_ICMP_ERROR_MSG) { /* append message to ICMP packet */
340 int message_len;
341 char *cpnt;
342 message_len = strlen(message);
343 if (message_len > ICMP_MAXDATALEN)
344 message_len = ICMP_MAXDATALEN;
345 cpnt = (char *)m->m_data + m->m_len;
346 memcpy(cpnt, message, message_len);
347 m->m_len += message_len;
348 }
349
350 icp->icmp_cksum = 0;
351 icp->icmp_cksum = cksum(m, m->m_len);
352
353 m->m_data -= hlen;
354 m->m_len += hlen;
355
356 /* fill in ip */
357 ip->ip_hl = hlen >> 2;
358 ip->ip_len = m->m_len;
359
360 ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
361
362 ip->ip_ttl = MAXTTL;
363 ip->ip_p = IPPROTO_ICMP;
364 ip->ip_dst = ip->ip_src; /* ip addresses */
365 ip->ip_src = m->slirp->vhost_addr;
366
367 (void)ip_output((struct socket *)NULL, m);
368
369 end_error:
370 return;
371 }
372 #undef ICMP_MAXDATALEN
373
374 /*
375 * Reflect the ip packet back to the source
376 */
icmp_reflect(struct mbuf * m)377 void icmp_reflect(struct mbuf *m)
378 {
379 register struct ip *ip = mtod(m, struct ip *);
380 int hlen = ip->ip_hl << 2;
381 int optlen = hlen - sizeof(struct ip);
382 register struct icmp *icp;
383
384 /*
385 * Send an icmp packet back to the ip level,
386 * after supplying a checksum.
387 */
388 m->m_data += hlen;
389 m->m_len -= hlen;
390 icp = mtod(m, struct icmp *);
391
392 icp->icmp_type = ICMP_ECHOREPLY;
393 icp->icmp_cksum = 0;
394 icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
395
396 m->m_data -= hlen;
397 m->m_len += hlen;
398
399 /* fill in ip */
400 if (optlen > 0) {
401 /*
402 * Strip out original options by copying rest of first
403 * mbuf's data back, and adjust the IP length.
404 */
405 memmove((char *)(ip + 1), (char *)ip + hlen,
406 (unsigned)(m->m_len - hlen));
407 hlen -= optlen;
408 ip->ip_hl = hlen >> 2;
409 ip->ip_len -= optlen;
410 m->m_len -= optlen;
411 }
412
413 ip->ip_ttl = MAXTTL;
414 { /* swap */
415 struct in_addr icmp_dst;
416 icmp_dst = ip->ip_dst;
417 ip->ip_dst = ip->ip_src;
418 ip->ip_src = icmp_dst;
419 }
420
421 (void)ip_output((struct socket *)NULL, m);
422 }
423
icmp_receive(struct socket * so)424 void icmp_receive(struct socket *so)
425 {
426 struct mbuf *m = so->so_m;
427 struct ip *ip = mtod(m, struct ip *);
428 int hlen = ip->ip_hl << 2;
429 uint8_t error_code;
430 struct icmp *icp;
431 int id, len;
432
433 m->m_data += hlen;
434 m->m_len -= hlen;
435 icp = mtod(m, struct icmp *);
436
437 id = icp->icmp_id;
438 len = recv(so->s, icp, M_ROOM(m), 0);
439 /*
440 * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
441 * between host OSes. On Linux, only the ICMP header and payload is
442 * included. On macOS/Darwin, the socket acts like a raw socket and
443 * includes the IP header as well. On other BSDs, SOCK_DGRAM+IPPROTO_ICMP
444 * sockets aren't supported at all, so we treat them like raw sockets. It
445 * isn't possible to detect this difference at runtime, so we must use an
446 * #ifdef to determine if we need to remove the IP header.
447 */
448 #ifdef CONFIG_BSD
449 if (len >= sizeof(struct ip)) {
450 struct ip *inner_ip = mtod(m, struct ip *);
451 int inner_hlen = inner_ip->ip_hl << 2;
452 if (inner_hlen > len) {
453 len = -1;
454 errno = -EINVAL;
455 } else {
456 len -= inner_hlen;
457 memmove(icp, (unsigned char *)icp + inner_hlen, len);
458 }
459 } else {
460 len = -1;
461 errno = -EINVAL;
462 }
463 #endif
464 icp->icmp_id = id;
465
466 m->m_data -= hlen;
467 m->m_len += hlen;
468
469 if (len == -1 || len == 0) {
470 if (errno == ENETUNREACH) {
471 error_code = ICMP_UNREACH_NET;
472 } else {
473 error_code = ICMP_UNREACH_HOST;
474 }
475 DEBUG_MISC(" udp icmp rx errno = %d-%s", errno, strerror(errno));
476 icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
477 } else {
478 icmp_reflect(so->so_m);
479 so->so_m = NULL; /* Don't m_free() it again! */
480 }
481 icmp_detach(so);
482 }
483