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     if (slirp_bind_outbound(so, AF_INET) != 0) {
94         // bind failed - close socket
95         closesocket(so->s);
96         so->s = -1;
97         return -1;
98     }
99 
100     so->so_m = m;
101     so->so_faddr = ip->ip_dst;
102     so->so_laddr = ip->ip_src;
103     so->so_iptos = ip->ip_tos;
104     so->so_type = IPPROTO_ICMP;
105     so->so_state = SS_ISFCONNECTED;
106     so->so_expire = curtime + SO_EXPIRE;
107 
108     addr.sin_family = AF_INET;
109     addr.sin_addr = so->so_faddr;
110 
111     insque(so, &so->slirp->icmp);
112 
113     if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
114                (struct sockaddr *)&addr, sizeof(addr)) == -1) {
115         DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s", errno,
116                    strerror(errno));
117         icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
118         icmp_detach(so);
119     }
120 
121     return 0;
122 }
123 
icmp_detach(struct socket * so)124 void icmp_detach(struct socket *so)
125 {
126     so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
127     closesocket(so->s);
128     sofree(so);
129 }
130 
131 /*
132  * Process a received ICMP message.
133  */
icmp_input(struct mbuf * m,int hlen)134 void icmp_input(struct mbuf *m, int hlen)
135 {
136     register struct icmp *icp;
137     register struct ip *ip = mtod(m, struct ip *);
138     int icmplen = ip->ip_len;
139     Slirp *slirp = m->slirp;
140 
141     DEBUG_CALL("icmp_input");
142     DEBUG_ARG("m = %p", m);
143     DEBUG_ARG("m_len = %d", m->m_len);
144 
145     /*
146      * Locate icmp structure in mbuf, and check
147      * that its not corrupted and of at least minimum length.
148      */
149     if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
150     freeit:
151         m_free(m);
152         goto end_error;
153     }
154 
155     m->m_len -= hlen;
156     m->m_data += hlen;
157     icp = mtod(m, struct icmp *);
158     if (cksum(m, icmplen)) {
159         goto freeit;
160     }
161     m->m_len += hlen;
162     m->m_data -= hlen;
163 
164     DEBUG_ARG("icmp_type = %d", icp->icmp_type);
165     switch (icp->icmp_type) {
166     case ICMP_ECHO:
167         ip->ip_len += hlen; /* since ip_input subtracts this */
168         if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
169             ip->ip_dst.s_addr == slirp->vnameserver_addr.s_addr) {
170             icmp_reflect(m);
171         } else if (slirp->restricted) {
172             goto freeit;
173         } else {
174             struct socket *so;
175             struct sockaddr_storage addr;
176             so = socreate(slirp);
177             if (icmp_send(so, m, hlen) == 0) {
178                 return;
179             }
180             if (udp_attach(so, AF_INET) == -1) {
181                 DEBUG_MISC("icmp_input udp_attach errno = %d-%s", errno,
182                            strerror(errno));
183                 sofree(so);
184                 m_free(m);
185                 goto end_error;
186             }
187             so->so_m = m;
188             so->so_ffamily = AF_INET;
189             so->so_faddr = ip->ip_dst;
190             so->so_fport = htons(7);
191             so->so_lfamily = AF_INET;
192             so->so_laddr = ip->ip_src;
193             so->so_lport = htons(9);
194             so->so_iptos = ip->ip_tos;
195             so->so_type = IPPROTO_ICMP;
196             so->so_state = SS_ISFCONNECTED;
197 
198             /* Send the packet */
199             addr = so->fhost.ss;
200             if (sotranslate_out(so, &addr) < 0) {
201                 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
202                                 strerror(errno));
203                 udp_detach(so);
204                 return;
205             }
206 
207             if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
208                        (struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {
209                 DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s", errno,
210                            strerror(errno));
211                 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
212                                 strerror(errno));
213                 udp_detach(so);
214             }
215         } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
216         break;
217     case ICMP_UNREACH:
218         /* XXX? report error? close socket? */
219     case ICMP_TIMXCEED:
220     case ICMP_PARAMPROB:
221     case ICMP_SOURCEQUENCH:
222     case ICMP_TSTAMP:
223     case ICMP_MASKREQ:
224     case ICMP_REDIRECT:
225         m_free(m);
226         break;
227 
228     default:
229         m_free(m);
230     } /* swith */
231 
232 end_error:
233     /* m is m_free()'d xor put in a socket xor or given to ip_send */
234     return;
235 }
236 
237 
238 /*
239  *	Send an ICMP message in response to a situation
240  *
241  *	RFC 1122: 3.2.2	MUST send at least the IP header and 8 bytes of header.
242  *MAY send more (we do). MUST NOT change this header information. MUST NOT reply
243  *to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast
244  *MAC address. MUST reply to only the first fragment.
245  */
246 /*
247  * Send ICMP_UNREACH back to the source regarding msrc.
248  * mbuf *msrc is used as a template, but is NOT m_free()'d.
249  * It is reported as the bad ip packet.  The header should
250  * be fully correct and in host byte order.
251  * ICMP fragmentation is illegal.  All machines must accept 576 bytes in one
252  * packet.  The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
253  */
254 
255 #define ICMP_MAXDATALEN (IP_MSS - 28)
icmp_send_error(struct mbuf * msrc,uint8_t type,uint8_t code,int minsize,const char * message)256 void icmp_send_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,
257                      const char *message)
258 {
259     unsigned hlen, shlen, s_ip_len;
260     register struct ip *ip;
261     register struct icmp *icp;
262     register struct mbuf *m;
263 
264     DEBUG_CALL("icmp_send_error");
265     DEBUG_ARG("msrc = %p", msrc);
266     DEBUG_ARG("msrc_len = %d", msrc->m_len);
267 
268     if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)
269         goto end_error;
270 
271     /* check msrc */
272     if (!msrc)
273         goto end_error;
274     ip = mtod(msrc, struct ip *);
275     if (slirp_debug & DBG_MISC) {
276         char bufa[20], bufb[20];
277         strcpy(bufa, inet_ntoa(ip->ip_src));
278         strcpy(bufb, inet_ntoa(ip->ip_dst));
279         DEBUG_MISC(" %.16s to %.16s", bufa, bufb);
280     }
281     if (ip->ip_off & IP_OFFMASK)
282         goto end_error; /* Only reply to fragment 0 */
283 
284     /* Do not reply to source-only IPs */
285     if ((ip->ip_src.s_addr & htonl(~(0xf << 28))) == 0) {
286         goto end_error;
287     }
288 
289     shlen = ip->ip_hl << 2;
290     s_ip_len = ip->ip_len;
291     if (ip->ip_p == IPPROTO_ICMP) {
292         icp = (struct icmp *)((char *)ip + shlen);
293         /*
294          *	Assume any unknown ICMP type is an error. This isn't
295          *	specified by the RFC, but think about it..
296          */
297         if (icp->icmp_type > 18 || icmp_flush[icp->icmp_type])
298             goto end_error;
299     }
300 
301     /* make a copy */
302     m = m_get(msrc->slirp);
303     if (!m) {
304         goto end_error;
305     }
306 
307     {
308         int new_m_size;
309         new_m_size =
310             sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
311         if (new_m_size > m->m_size)
312             m_inc(m, new_m_size);
313     }
314     memcpy(m->m_data, msrc->m_data, msrc->m_len);
315     m->m_len = msrc->m_len; /* copy msrc to m */
316 
317     /* make the header of the reply packet */
318     ip = mtod(m, struct ip *);
319     hlen = sizeof(struct ip); /* no options in reply */
320 
321     /* fill in icmp */
322     m->m_data += hlen;
323     m->m_len -= hlen;
324 
325     icp = mtod(m, struct icmp *);
326 
327     if (minsize)
328         s_ip_len = shlen + ICMP_MINLEN; /* return header+8b only */
329     else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
330         s_ip_len = ICMP_MAXDATALEN;
331 
332     m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
333 
334     /* min. size = 8+sizeof(struct ip)+8 */
335 
336     icp->icmp_type = type;
337     icp->icmp_code = code;
338     icp->icmp_id = 0;
339     icp->icmp_seq = 0;
340 
341     memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
342     HTONS(icp->icmp_ip.ip_len);
343     HTONS(icp->icmp_ip.ip_id);
344     HTONS(icp->icmp_ip.ip_off);
345 
346     if (message && WITH_ICMP_ERROR_MSG) { /* append message to ICMP packet */
347         int message_len;
348         char *cpnt;
349         message_len = strlen(message);
350         if (message_len > ICMP_MAXDATALEN)
351             message_len = ICMP_MAXDATALEN;
352         cpnt = (char *)m->m_data + m->m_len;
353         memcpy(cpnt, message, message_len);
354         m->m_len += message_len;
355     }
356 
357     icp->icmp_cksum = 0;
358     icp->icmp_cksum = cksum(m, m->m_len);
359 
360     m->m_data -= hlen;
361     m->m_len += hlen;
362 
363     /* fill in ip */
364     ip->ip_hl = hlen >> 2;
365     ip->ip_len = m->m_len;
366 
367     ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
368 
369     ip->ip_ttl = MAXTTL;
370     ip->ip_p = IPPROTO_ICMP;
371     ip->ip_dst = ip->ip_src; /* ip addresses */
372     ip->ip_src = m->slirp->vhost_addr;
373 
374     (void)ip_output((struct socket *)NULL, m);
375 
376 end_error:
377     return;
378 }
379 #undef ICMP_MAXDATALEN
380 
381 /*
382  * Reflect the ip packet back to the source
383  */
icmp_reflect(struct mbuf * m)384 void icmp_reflect(struct mbuf *m)
385 {
386     register struct ip *ip = mtod(m, struct ip *);
387     int hlen = ip->ip_hl << 2;
388     int optlen = hlen - sizeof(struct ip);
389     register struct icmp *icp;
390 
391     /*
392      * Send an icmp packet back to the ip level,
393      * after supplying a checksum.
394      */
395     m->m_data += hlen;
396     m->m_len -= hlen;
397     icp = mtod(m, struct icmp *);
398 
399     icp->icmp_type = ICMP_ECHOREPLY;
400     icp->icmp_cksum = 0;
401     icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
402 
403     m->m_data -= hlen;
404     m->m_len += hlen;
405 
406     /* fill in ip */
407     if (optlen > 0) {
408         /*
409          * Strip out original options by copying rest of first
410          * mbuf's data back, and adjust the IP length.
411          */
412         memmove((char *)(ip + 1), (char *)ip + hlen,
413                 (unsigned)(m->m_len - hlen));
414         hlen -= optlen;
415         ip->ip_hl = hlen >> 2;
416         ip->ip_len -= optlen;
417         m->m_len -= optlen;
418     }
419 
420     ip->ip_ttl = MAXTTL;
421     { /* swap */
422         struct in_addr icmp_dst;
423         icmp_dst = ip->ip_dst;
424         ip->ip_dst = ip->ip_src;
425         ip->ip_src = icmp_dst;
426     }
427 
428     (void)ip_output((struct socket *)NULL, m);
429 }
430 
icmp_receive(struct socket * so)431 void icmp_receive(struct socket *so)
432 {
433     struct mbuf *m = so->so_m;
434     struct ip *ip = mtod(m, struct ip *);
435     int hlen = ip->ip_hl << 2;
436     uint8_t error_code;
437     struct icmp *icp;
438     int id, len;
439 
440     m->m_data += hlen;
441     m->m_len -= hlen;
442     icp = mtod(m, struct icmp *);
443 
444     id = icp->icmp_id;
445     len = recv(so->s, icp, M_ROOM(m), 0);
446     /*
447      * The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
448      * between host OSes.  On Linux, only the ICMP header and payload is
449      * included.  On macOS/Darwin, the socket acts like a raw socket and
450      * includes the IP header as well.  On other BSDs, SOCK_DGRAM+IPPROTO_ICMP
451      * sockets aren't supported at all, so we treat them like raw sockets.  It
452      * isn't possible to detect this difference at runtime, so we must use an
453      * #ifdef to determine if we need to remove the IP header.
454      */
455 #ifdef CONFIG_BSD
456     if (len >= sizeof(struct ip)) {
457         struct ip *inner_ip = mtod(m, struct ip *);
458         int inner_hlen = inner_ip->ip_hl << 2;
459         if (inner_hlen > len) {
460             len = -1;
461             errno = -EINVAL;
462         } else {
463             len -= inner_hlen;
464             memmove(icp, (unsigned char *)icp + inner_hlen, len);
465         }
466     } else {
467         len = -1;
468         errno = -EINVAL;
469     }
470 #endif
471     icp->icmp_id = id;
472 
473     m->m_data -= hlen;
474     m->m_len += hlen;
475 
476     if (len == -1 || len == 0) {
477         if (errno == ENETUNREACH) {
478             error_code = ICMP_UNREACH_NET;
479         } else {
480             error_code = ICMP_UNREACH_HOST;
481         }
482         DEBUG_MISC(" udp icmp rx errno = %d-%s", errno, strerror(errno));
483         icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
484     } else {
485         icmp_reflect(so->so_m);
486         so->so_m = NULL; /* Don't m_free() it again! */
487     }
488     icmp_detach(so);
489 }
490