Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ip_icmp.c
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1/*2* Copyright (c) 1982, 1986, 1988, 19933* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. Neither the name of the University nor the names of its contributors14* may be used to endorse or promote products derived from this software15* without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* @(#)ip_icmp.c 8.2 (Berkeley) 1/4/9430* ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp31*/3233#include "slirp.h"34#include "ip_icmp.h"3536#ifndef _WIN3237#include <sys/param.h>38#endif3940#ifndef WITH_ICMP_ERROR_MSG41#define WITH_ICMP_ERROR_MSG 042#endif4344/* The message sent when emulating PING */45/* Be nice and tell them it's just a pseudo-ping packet */46static const char icmp_ping_msg[] =47"This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST "48"packets.\n";4950/* list of actions for icmp_send_error() on RX of an icmp message */51static const int icmp_flush[19] = {52/* ECHO REPLY (0) */ 0,531,541,55/* DEST UNREACH (3) */ 1,56/* SOURCE QUENCH (4)*/ 1,57/* REDIRECT (5) */ 1,581,591,60/* ECHO (8) */ 0,61/* ROUTERADVERT (9) */ 1,62/* ROUTERSOLICIT (10) */ 1,63/* TIME EXCEEDED (11) */ 1,64/* PARAMETER PROBLEM (12) */ 1,65/* TIMESTAMP (13) */ 0,66/* TIMESTAMP REPLY (14) */ 0,67/* INFO (15) */ 0,68/* INFO REPLY (16) */ 0,69/* ADDR MASK (17) */ 0,70/* ADDR MASK REPLY (18) */ 071};7273void icmp_init(Slirp *slirp)74{75slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;76slirp->icmp_last_so = &slirp->icmp;77}7879void icmp_cleanup(Slirp *slirp)80{81struct socket *so, *so_next;8283for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so_next) {84so_next = so->so_next;85icmp_detach(so);86}87}8889/* Send ICMP packet to the Internet, and save it to so_m */90static int icmp_send(struct socket *so, struct mbuf *m, int hlen)91{92Slirp *slirp = m->slirp;9394struct sockaddr_in addr;9596/*97* The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent98* between host OSes. On Linux, only the ICMP header and payload is99* included. On macOS/Darwin, the socket acts like a raw socket and100* includes the IP header as well. On other BSDs, SOCK_DGRAM+IPPROTO_ICMP101* sockets aren't supported at all, so we treat them like raw sockets. It102* isn't possible to detect this difference at runtime, so we must use an103* #ifdef to determine if we need to remove the IP header.104*/105#if defined(BSD) && !defined(__GNU__)106so->so_type = IPPROTO_IP;107#else108so->so_type = IPPROTO_ICMP;109#endif110111so->s = slirp_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);112if (so->s == -1) {113if (errno == EAFNOSUPPORT114|| errno == EPROTONOSUPPORT115|| errno == EACCES) {116/* Kernel doesn't support or allow ping sockets. */117so->so_type = IPPROTO_IP;118so->s = slirp_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);119}120}121if (so->s == -1) {122return -1;123}124so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque);125126if (slirp_bind_outbound(so, AF_INET) != 0) {127// bind failed - close socket128closesocket(so->s);129so->s = -1;130return -1;131}132133M_DUP_DEBUG(slirp, m, 0, 0);134struct ip *ip = mtod(m, struct ip *);135136so->so_m = m;137so->so_faddr = ip->ip_dst;138so->so_laddr = ip->ip_src;139so->so_iptos = ip->ip_tos;140so->so_state = SS_ISFCONNECTED;141so->so_expire = curtime + SO_EXPIRE;142143addr.sin_family = AF_INET;144addr.sin_addr = so->so_faddr;145146slirp_insque(so, &so->slirp->icmp);147148if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,149(struct sockaddr *)&addr, sizeof(addr)) == -1) {150DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s", errno,151strerror(errno));152icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));153icmp_detach(so);154}155156return 0;157}158159void icmp_detach(struct socket *so)160{161so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);162closesocket(so->s);163sofree(so);164}165166/*167* Process a received ICMP message.168*/169void icmp_input(struct mbuf *m, int hlen)170{171Slirp *slirp = m->slirp;172M_DUP_DEBUG(slirp, m, 0, 0);173174register struct icmp *icp;175register struct ip *ip = mtod(m, struct ip *);176int icmplen = ip->ip_len;177178DEBUG_CALL("icmp_input");179DEBUG_ARG("m = %p", m);180DEBUG_ARG("m_len = %d", m->m_len);181182/*183* Locate icmp structure in mbuf, and check184* that its not corrupted and of at least minimum length.185*/186if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */187freeit:188m_free(m);189goto end_error;190}191192m->m_len -= hlen;193m->m_data += hlen;194icp = mtod(m, struct icmp *);195if (cksum(m, icmplen)) {196goto freeit;197}198m->m_len += hlen;199m->m_data -= hlen;200201DEBUG_ARG("icmp_type = %d", icp->icmp_type);202switch (icp->icmp_type) {203case ICMP_ECHO:204ip->ip_len += hlen; /* since ip_input subtracts this */205if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||206ip->ip_dst.s_addr == slirp->vnameserver_addr.s_addr) {207icmp_reflect(m);208} else if (slirp->restricted) {209goto freeit;210} else {211struct socket *so;212struct sockaddr_storage addr;213int ttl;214215so = socreate(slirp, IPPROTO_ICMP);216if (icmp_send(so, m, hlen) == 0) {217/* We could send this as ICMP, good! */218return;219}220221/* We could not send this as ICMP, try to send it on UDP echo222* service (7), wishfully hoping that it is open there. */223224if (udp_attach(so, AF_INET) == -1) {225DEBUG_MISC("icmp_input udp_attach errno = %d-%s", errno,226strerror(errno));227sofree(so);228m_free(m);229goto end_error;230}231so->so_m = m;232so->so_ffamily = AF_INET;233so->so_faddr = ip->ip_dst;234so->so_fport = htons(7);235so->so_lfamily = AF_INET;236so->so_laddr = ip->ip_src;237so->so_lport = htons(9);238so->so_iptos = ip->ip_tos;239so->so_state = SS_ISFCONNECTED;240241/* Send the packet */242addr = so->fhost.ss;243if (sotranslate_out(so, &addr) < 0) {244icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,245strerror(errno));246udp_detach(so);247return;248}249250/*251* Check for TTL252*/253ttl = ip->ip_ttl-1;254if (ttl <= 0) {255DEBUG_MISC("udp ttl exceeded");256icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0,257NULL);258udp_detach(so);259break;260}261setsockopt(so->s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));262263if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,264(struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {265DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s", errno,266strerror(errno));267icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,268strerror(errno));269udp_detach(so);270}271} /* if ip->ip_dst.s_addr == alias_addr.s_addr */272break;273case ICMP_UNREACH:274/* XXX? report error? close socket? */275case ICMP_TIMXCEED:276case ICMP_PARAMPROB:277case ICMP_SOURCEQUENCH:278case ICMP_TSTAMP:279case ICMP_MASKREQ:280case ICMP_REDIRECT:281m_free(m);282break;283284default:285m_free(m);286} /* switch */287288end_error:289/* m is m_free()'d xor put in a socket xor or given to ip_send */290return;291}292293294/*295* Send an ICMP message in response to a situation296*297* RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.298*MAY send more (we do). MUST NOT change this header information. MUST NOT reply299*to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast300*MAC address. MUST reply to only the first fragment.301*/302/*303* Send ICMP_UNREACH back to the source regarding msrc.304* mbuf *msrc is used as a template, but is NOT m_free()'d.305* It is reported as the bad ip packet. The header should306* be fully correct and in host byte order.307* ICMP fragmentation is illegal. All machines must accept 576 bytes in one308* packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548309*/310311#define ICMP_MAXDATALEN (IP_MSS - 28)312void icmp_forward_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,313const char *message, struct in_addr *src)314{315unsigned hlen, shlen, s_ip_len;316register struct ip *ip;317register struct icmp *icp;318register struct mbuf *m;319320DEBUG_CALL("icmp_send_error");321DEBUG_ARG("msrc = %p", msrc);322DEBUG_ARG("msrc_len = %d", msrc->m_len);323324if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)325goto end_error;326327/* check msrc */328if (!msrc)329goto end_error;330ip = mtod(msrc, struct ip *);331if (slirp_debug & DBG_MISC) {332char addr_src[INET_ADDRSTRLEN];333char addr_dst[INET_ADDRSTRLEN];334335inet_ntop(AF_INET, &ip->ip_src, addr_src, sizeof(addr_src));336inet_ntop(AF_INET, &ip->ip_dst, addr_dst, sizeof(addr_dst));337DEBUG_MISC(" %.16s to %.16s", addr_src, addr_dst);338}339if (ip->ip_off & IP_OFFMASK)340goto end_error; /* Only reply to fragment 0 */341342/* Do not reply to source-only IPs */343if ((ip->ip_src.s_addr & htonl(~(0xf << 28))) == 0) {344goto end_error;345}346347shlen = ip->ip_hl << 2;348s_ip_len = ip->ip_len;349if (ip->ip_p == IPPROTO_ICMP) {350icp = (struct icmp *)((char *)ip + shlen);351/*352* Assume any unknown ICMP type is an error. This isn't353* specified by the RFC, but think about it..354*/355if (icp->icmp_type > 18 || icmp_flush[icp->icmp_type])356goto end_error;357}358359/* make a copy */360m = m_get(msrc->slirp);361if (!m) {362goto end_error;363}364365{366int new_m_size;367new_m_size =368sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;369if (new_m_size > m->m_size)370m_inc(m, new_m_size);371}372memcpy(m->m_data, msrc->m_data, msrc->m_len);373m->m_len = msrc->m_len; /* copy msrc to m */374375/* make the header of the reply packet */376ip = mtod(m, struct ip *);377hlen = sizeof(struct ip); /* no options in reply */378379/* fill in icmp */380m->m_data += hlen;381m->m_len -= hlen;382383icp = mtod(m, struct icmp *);384385if (minsize)386s_ip_len = shlen + ICMP_MINLEN; /* return header+8b only */387else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */388s_ip_len = ICMP_MAXDATALEN;389390m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */391392/* min. size = 8+sizeof(struct ip)+8 */393394icp->icmp_type = type;395icp->icmp_code = code;396icp->icmp_id = 0;397icp->icmp_seq = 0;398399memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */400HTONS(icp->icmp_ip.ip_len);401HTONS(icp->icmp_ip.ip_id);402HTONS(icp->icmp_ip.ip_off);403404if (message && WITH_ICMP_ERROR_MSG) { /* append message to ICMP packet */405int message_len;406char *cpnt;407message_len = strlen(message);408if (message_len > ICMP_MAXDATALEN)409message_len = ICMP_MAXDATALEN;410cpnt = (char *)m->m_data + m->m_len;411memcpy(cpnt, message, message_len);412m->m_len += message_len;413}414415icp->icmp_cksum = 0;416icp->icmp_cksum = cksum(m, m->m_len);417418m->m_data -= hlen;419m->m_len += hlen;420421/* fill in ip */422ip->ip_hl = hlen >> 2;423ip->ip_len = m->m_len;424425ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */426427ip->ip_ttl = MAXTTL;428ip->ip_p = IPPROTO_ICMP;429ip->ip_dst = ip->ip_src; /* ip addresses */430ip->ip_src = *src;431432ip_output((struct socket *)NULL, m);433434end_error:435return;436}437#undef ICMP_MAXDATALEN438439void icmp_send_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,440const char *message)441{442icmp_forward_error(msrc, type, code, minsize, message, &msrc->slirp->vhost_addr);443}444445/*446* Reflect the ip packet back to the source447*/448void icmp_reflect(struct mbuf *m)449{450register struct ip *ip = mtod(m, struct ip *);451int hlen = ip->ip_hl << 2;452int optlen = hlen - sizeof(struct ip);453register struct icmp *icp;454455/*456* Send an icmp packet back to the ip level,457* after supplying a checksum.458*/459m->m_data += hlen;460m->m_len -= hlen;461icp = mtod(m, struct icmp *);462463icp->icmp_type = ICMP_ECHOREPLY;464icp->icmp_cksum = 0;465icp->icmp_cksum = cksum(m, ip->ip_len - hlen);466467m->m_data -= hlen;468m->m_len += hlen;469470/* fill in ip */471if (optlen > 0) {472/*473* Strip out original options by copying rest of first474* mbuf's data back, and adjust the IP length.475*/476memmove((char *)(ip + 1), (char *)ip + hlen,477(unsigned)(m->m_len - hlen));478hlen -= optlen;479ip->ip_hl = hlen >> 2;480ip->ip_len -= optlen;481m->m_len -= optlen;482}483484ip->ip_ttl = MAXTTL;485{ /* swap */486struct in_addr icmp_dst;487icmp_dst = ip->ip_dst;488ip->ip_dst = ip->ip_src;489ip->ip_src = icmp_dst;490}491492ip_output((struct socket *)NULL, m);493}494495void icmp_receive(struct socket *so)496{497struct mbuf *m = so->so_m;498struct ip *ip = mtod(m, struct ip *);499int hlen = ip->ip_hl << 2;500uint8_t error_code;501struct icmp *icp;502int id, len;503504m->m_data += hlen;505m->m_len -= hlen;506icp = mtod(m, struct icmp *);507508id = icp->icmp_id;509len = recv(so->s, icp, M_ROOM(m), 0);510511if (so->so_type == IPPROTO_IP) {512if (len >= sizeof(struct ip)) {513struct ip *inner_ip = mtod(m, struct ip *);514int inner_hlen = inner_ip->ip_hl << 2;515if (inner_hlen > len) {516len = -1;517errno = -EINVAL;518} else {519len -= inner_hlen;520memmove(icp, (unsigned char *)icp + inner_hlen, len);521}522} else {523len = -1;524errno = -EINVAL;525}526}527528icp->icmp_id = id;529530m->m_data -= hlen;531m->m_len += hlen;532533if (len == -1 || len == 0) {534if (errno == ENETUNREACH) {535error_code = ICMP_UNREACH_NET;536} else {537error_code = ICMP_UNREACH_HOST;538}539DEBUG_MISC(" udp icmp rx errno = %d-%s", errno, strerror(errno));540icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));541} else {542icmp_reflect(so->so_m);543so->so_m = NULL; /* Don't m_free() it again! */544}545icmp_detach(so);546}547548549