1 /////////////////////////////////////////////////////////////////////////
2 // $Id: ip_icmp.cc 13932 2020-09-02 08:35:44Z vruppert $
3 /////////////////////////////////////////////////////////////////////////
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
33  * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
34  */
35 
36 #include "slirp.h"
37 #include "ip_icmp.h"
38 
39 #if BX_NETWORKING && BX_NETMOD_SLIRP
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[] = "This is a pseudo-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 */
46 static 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 
icmp_init(Slirp * slirp)68 void icmp_init(Slirp *slirp)
69 {
70     slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;
71     slirp->icmp_last_so = &slirp->icmp;
72 }
73 
icmp_cleanup(Slirp * slirp)74 void icmp_cleanup(Slirp *slirp)
75 {
76     while (slirp->icmp.so_next != &slirp->icmp) {
77         icmp_detach(slirp->icmp.so_next);
78     }
79 }
80 
icmp_send(struct socket * so,struct mbuf * m,int hlen)81 static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
82 {
83     struct ip *ip = mtod(m, struct ip *);
84     struct sockaddr_in addr;
85 
86     so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
87     if (so->s == -1) {
88         return -1;
89     }
90 
91     so->so_m = m;
92     so->so_faddr = ip->ip_dst;
93     so->so_laddr = ip->ip_src;
94     so->so_iptos = ip->ip_tos;
95     so->so_type = IPPROTO_ICMP;
96     so->so_state = SS_ISFCONNECTED;
97     so->so_expire = curtime + SO_EXPIRE;
98 
99     addr.sin_family = AF_INET;
100     addr.sin_addr = so->so_faddr;
101 
102     insque(so, &so->slirp->icmp);
103 
104     if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
105                (struct sockaddr *)&addr, sizeof(addr)) == -1) {
106         DEBUG_MISC((dfd, "icmp_input icmp sendto tx errno = %d-%s\n",
107                     errno, strerror(errno)));
108         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
109         icmp_detach(so);
110     }
111 
112     return 0;
113 }
114 
icmp_detach(struct socket * so)115 void icmp_detach(struct socket *so)
116 {
117     closesocket(so->s);
118     sofree(so);
119 }
120 
121 /*
122  * Process a received ICMP message.
123  */
124 void
icmp_input(struct mbuf * m,int hlen)125 icmp_input(struct mbuf *m, int hlen)
126 {
127   struct icmp *icp;
128   struct ip *ip=mtod(m, struct ip *);
129   int icmplen=ip->ip_len;
130   Slirp *slirp = m->slirp;
131 
132   DEBUG_CALL("icmp_input");
133   DEBUG_ARG("m = %lx", (long )m);
134   DEBUG_ARG("m_len = %d", m->m_len);
135 
136   /*
137    * Locate icmp structure in mbuf, and check
138    * that its not corrupted and of at least minimum length.
139    */
140   if (icmplen < ICMP_MINLEN) {          /* min 8 bytes payload */
141   freeit:
142     m_free(m);
143     goto end_error;
144   }
145 
146   m->m_len -= hlen;
147   m->m_data += hlen;
148   icp = mtod(m, struct icmp *);
149   if (cksum(m, icmplen)) {
150     goto freeit;
151   }
152   m->m_len += hlen;
153   m->m_data -= hlen;
154 
155   DEBUG_ARG("icmp_type = %d", icp->icmp_type);
156   switch (icp->icmp_type) {
157   case ICMP_ECHO:
158     ip->ip_len += hlen;	             /* since ip_input subtracts this */
159     if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
160       icmp_reflect(m);
161     } else if (slirp->restricted) {
162         goto freeit;
163     } else {
164       struct socket *so;
165       struct sockaddr_in addr;
166       if ((so = socreate(slirp)) == NULL) goto freeit;
167       if (icmp_send(so, m, hlen) == 0) {
168         return;
169       }
170       if(udp_attach(so) == -1) {
171 	DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
172 		    errno,strerror(errno)));
173 	sofree(so);
174 	m_free(m);
175 	goto end_error;
176       }
177       so->so_m = m;
178       so->so_faddr = ip->ip_dst;
179       so->so_fport = htons(7);
180       so->so_laddr = ip->ip_src;
181       so->so_lport = htons(9);
182       so->so_iptos = ip->ip_tos;
183       so->so_type = IPPROTO_ICMP;
184       so->so_state = SS_ISFCONNECTED;
185 
186       /* Send the packet */
187       addr.sin_family = AF_INET;
188       if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
189           slirp->vnetwork_addr.s_addr) {
190 	/* It's an alias */
191 	if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
192 	  if (get_dns_addr(&addr.sin_addr) < 0)
193 	    addr.sin_addr = loopback_addr;
194 	} else {
195 	  addr.sin_addr = loopback_addr;
196 	}
197       } else {
198 	addr.sin_addr = so->so_faddr;
199       }
200       addr.sin_port = so->so_fport;
201       if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
202 		(struct sockaddr *)&addr, sizeof(addr)) == -1) {
203 	DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
204 		    errno,strerror(errno)));
205 	icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,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. MAY send more (we do).
235  *			MUST NOT change this header information.
236  *			MUST NOT reply to a multicast/broadcast IP address.
237  *			MUST NOT reply to a multicast/broadcast MAC address.
238  *			MUST reply to only the first fragment.
239  */
240 /*
241  * Send ICMP_UNREACH back to the source regarding msrc.
242  * mbuf *msrc is used as a template, but is NOT m_free()'d.
243  * It is reported as the bad ip packet.  The header should
244  * be fully correct and in host byte order.
245  * ICMP fragmentation is illegal.  All machines must accept 576 bytes in one
246  * packet.  The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
247  */
248 
249 #define ICMP_MAXDATALEN (IP_MSS-28)
250 void
icmp_error(struct mbuf * msrc,u_char type,u_char code,int minsize,const char * message)251 icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
252            const char *message)
253 {
254   unsigned hlen, shlen, s_ip_len;
255   struct ip *ip;
256   struct icmp *icp;
257   struct mbuf *m;
258 
259   DEBUG_CALL("icmp_error");
260   DEBUG_ARG("msrc = %lx", (long )msrc);
261   DEBUG_ARG("msrc_len = %d", msrc->m_len);
262 
263   if(type!=ICMP_UNREACH && type!=ICMP_TIMXCEED) goto end_error;
264 
265   /* check msrc */
266   if(!msrc) goto end_error;
267   ip = mtod(msrc, struct ip *);
268 #ifdef DEBUG
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((dfd, " %.16s to %.16s\n", bufa, bufb));
273   }
274 #endif
275   if(ip->ip_off & IP_OFFMASK) 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]) goto end_error;
291   }
292 
293   /* make a copy */
294   m = m_get(msrc->slirp);
295   if (!m) {
296       goto end_error;
297   }
298 
299   { int new_m_size;
300     new_m_size=sizeof(struct ip )+ICMP_MINLEN+msrc->m_len+ICMP_MAXDATALEN;
301     if(new_m_size>m->m_size) m_inc(m, new_m_size);
302   }
303   memcpy(m->m_data, msrc->m_data, msrc->m_len);
304   m->m_len = msrc->m_len;                        /* copy msrc to m */
305 
306   /* make the header of the reply packet */
307   ip  = mtod(m, struct ip *);
308   hlen= sizeof(struct ip );     /* no options in reply */
309 
310   /* fill in icmp */
311   m->m_data += hlen;
312   m->m_len -= hlen;
313 
314   icp = mtod(m, struct icmp *);
315 
316   if(minsize) s_ip_len=shlen+ICMP_MINLEN;   /* return header+8b only */
317   else if(s_ip_len>ICMP_MAXDATALEN)         /* maximum size */
318     s_ip_len=ICMP_MAXDATALEN;
319 
320   m->m_len=ICMP_MINLEN+s_ip_len;        /* 8 bytes ICMP header */
321 
322   /* min. size = 8+sizeof(struct ip)+8 */
323 
324   icp->icmp_type = type;
325   icp->icmp_code = code;
326   icp->icmp_id = 0;
327   icp->icmp_seq = 0;
328 
329   memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len);   /* report the ip packet */
330   HTONS(icp->icmp_ip.ip_len);
331   HTONS(icp->icmp_ip.ip_id);
332   HTONS(icp->icmp_ip.ip_off);
333 
334 #ifdef DEBUG
335   if(message) {           /* DEBUG : append message to ICMP packet */
336     int message_len;
337     char *cpnt;
338     message_len=strlen(message);
339     if(message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
340     cpnt=(char *)m->m_data+m->m_len;
341     memcpy(cpnt, message, message_len);
342     m->m_len+=message_len;
343   }
344 #endif
345 
346   icp->icmp_cksum = 0;
347   icp->icmp_cksum = cksum(m, m->m_len);
348 
349   m->m_data -= hlen;
350   m->m_len += hlen;
351 
352   /* fill in ip */
353   ip->ip_hl = hlen >> 2;
354   ip->ip_len = m->m_len;
355 
356   ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0);  /* high priority for errors */
357 
358   ip->ip_ttl = MAXTTL;
359   ip->ip_p = IPPROTO_ICMP;
360   ip->ip_dst = ip->ip_src;    /* ip addresses */
361   ip->ip_src = m->slirp->vhost_addr;
362 
363   (void ) ip_output((struct socket *)NULL, m);
364 
365 end_error:
366   return;
367 }
368 #undef ICMP_MAXDATALEN
369 
370 /*
371  * Reflect the ip packet back to the source
372  */
373 void
icmp_reflect(struct mbuf * m)374 icmp_reflect(struct mbuf *m)
375 {
376   struct ip *ip = mtod(m, struct ip *);
377   int hlen = ip->ip_hl << 2;
378   int optlen = hlen - sizeof(struct ip );
379   struct icmp *icp;
380 
381   /*
382    * Send an icmp packet back to the ip level,
383    * after supplying a checksum.
384    */
385   m->m_data += hlen;
386   m->m_len -= hlen;
387   icp = mtod(m, struct icmp *);
388 
389   icp->icmp_type = ICMP_ECHOREPLY;
390   icp->icmp_cksum = 0;
391   icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
392 
393   m->m_data -= hlen;
394   m->m_len += hlen;
395 
396   /* fill in ip */
397   if (optlen > 0) {
398     /*
399      * Strip out original options by copying rest of first
400      * mbuf's data back, and adjust the IP length.
401      */
402     memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
403 	    (unsigned )(m->m_len - hlen));
404     hlen -= optlen;
405     ip->ip_hl = hlen >> 2;
406     ip->ip_len -= optlen;
407     m->m_len -= optlen;
408   }
409 
410   ip->ip_ttl = MAXTTL;
411   { /* swap */
412     struct in_addr icmp_dst;
413     icmp_dst = ip->ip_dst;
414     ip->ip_dst = ip->ip_src;
415     ip->ip_src = icmp_dst;
416   }
417 
418   (void ) ip_output((struct socket *)NULL, m);
419 }
420 
icmp_receive(struct socket * so)421 void icmp_receive(struct socket *so)
422 {
423     struct mbuf *m = so->so_m;
424     struct ip *ip = mtod(m, struct ip *);
425     int hlen = ip->ip_hl << 2;
426     u_char error_code;
427     struct icmp *icp;
428     int id, len;
429 
430     m->m_data += hlen;
431     m->m_len -= hlen;
432     icp = mtod(m, struct icmp *);
433 
434     id = icp->icmp_id;
435     len = qemu_recv(so->s, icp, m->m_len, 0);
436     icp->icmp_id = id;
437 
438     m->m_data -= hlen;
439     m->m_len += hlen;
440 
441     if (len == -1 || len == 0) {
442         if (errno == ENETUNREACH) {
443             error_code = ICMP_UNREACH_NET;
444         } else {
445             error_code = ICMP_UNREACH_HOST;
446         }
447         DEBUG_MISC((dfd, " udp icmp rx errno = %d-%s\n", errno,
448                     strerror(errno)));
449         icmp_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
450     } else {
451         icmp_reflect(so->so_m);
452         so->so_m = NULL; /* Don't m_free() it again! */
453     }
454     icmp_detach(so);
455 }
456 
457 #endif
458