Statistics
| Branch: | Revision:

root / slirp / ip_icmp.c @ 14015304

History | View | Annotate | Download (12 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1982, 1986, 1988, 1993
3 f0cbd3ec bellard
 *        The Regents of the University of California.  All rights reserved.
4 f0cbd3ec bellard
 *
5 f0cbd3ec bellard
 * Redistribution and use in source and binary forms, with or without
6 f0cbd3ec bellard
 * modification, are permitted provided that the following conditions
7 f0cbd3ec bellard
 * are met:
8 f0cbd3ec bellard
 * 1. Redistributions of source code must retain the above copyright
9 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer.
10 f0cbd3ec bellard
 * 2. Redistributions in binary form must reproduce the above copyright
11 f0cbd3ec bellard
 *    notice, this list of conditions and the following disclaimer in the
12 f0cbd3ec bellard
 *    documentation and/or other materials provided with the distribution.
13 2f5f8996 aliguori
 * 3. Neither the name of the University nor the names of its contributors
14 f0cbd3ec bellard
 *    may be used to endorse or promote products derived from this software
15 f0cbd3ec bellard
 *    without specific prior written permission.
16 f0cbd3ec bellard
 *
17 f0cbd3ec bellard
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 f0cbd3ec bellard
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 f0cbd3ec bellard
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 f0cbd3ec bellard
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 f0cbd3ec bellard
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 f0cbd3ec bellard
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 f0cbd3ec bellard
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 f0cbd3ec bellard
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f0cbd3ec bellard
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 f0cbd3ec bellard
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 f0cbd3ec bellard
 * SUCH DAMAGE.
28 f0cbd3ec bellard
 *
29 f0cbd3ec bellard
 *        @(#)ip_icmp.c        8.2 (Berkeley) 1/4/94
30 f0cbd3ec bellard
 * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
31 f0cbd3ec bellard
 */
32 f0cbd3ec bellard
33 f0cbd3ec bellard
#include "slirp.h"
34 f0cbd3ec bellard
#include "ip_icmp.h"
35 f0cbd3ec bellard
36 f0cbd3ec bellard
/* The message sent when emulating PING */
37 7878ff6b blueswir1
/* Be nice and tell them it's just a pseudo-ping packet */
38 7ca699c8 blueswir1
static const char icmp_ping_msg[] = "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
39 f0cbd3ec bellard
40 f0cbd3ec bellard
/* list of actions for icmp_error() on RX of an icmp message */
41 9634d903 blueswir1
static const int icmp_flush[19] = {
42 f0cbd3ec bellard
/*  ECHO REPLY (0)  */   0,
43 f0cbd3ec bellard
                         1,
44 f0cbd3ec bellard
                         1,
45 f0cbd3ec bellard
/* DEST UNREACH (3) */   1,
46 f0cbd3ec bellard
/* SOURCE QUENCH (4)*/   1,
47 f0cbd3ec bellard
/* REDIRECT (5) */       1,
48 f0cbd3ec bellard
                         1,
49 f0cbd3ec bellard
                         1,
50 f0cbd3ec bellard
/* ECHO (8) */           0,
51 f0cbd3ec bellard
/* ROUTERADVERT (9) */   1,
52 f0cbd3ec bellard
/* ROUTERSOLICIT (10) */ 1,
53 f0cbd3ec bellard
/* TIME EXCEEDED (11) */ 1,
54 f0cbd3ec bellard
/* PARAMETER PROBLEM (12) */ 1,
55 f0cbd3ec bellard
/* TIMESTAMP (13) */     0,
56 f0cbd3ec bellard
/* TIMESTAMP REPLY (14) */ 0,
57 f0cbd3ec bellard
/* INFO (15) */          0,
58 f0cbd3ec bellard
/* INFO REPLY (16) */    0,
59 f0cbd3ec bellard
/* ADDR MASK (17) */     0,
60 5fafdf24 ths
/* ADDR MASK REPLY (18) */ 0
61 f0cbd3ec bellard
};
62 f0cbd3ec bellard
63 e6d43cfb Jan Kiszka
void icmp_init(Slirp *slirp)
64 e6d43cfb Jan Kiszka
{
65 e6d43cfb Jan Kiszka
    slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;
66 e6d43cfb Jan Kiszka
    slirp->icmp_last_so = &slirp->icmp;
67 e6d43cfb Jan Kiszka
}
68 e6d43cfb Jan Kiszka
69 e6d43cfb Jan Kiszka
static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
70 e6d43cfb Jan Kiszka
{
71 e6d43cfb Jan Kiszka
    struct ip *ip = mtod(m, struct ip *);
72 e6d43cfb Jan Kiszka
    struct sockaddr_in addr;
73 e6d43cfb Jan Kiszka
74 e6d43cfb Jan Kiszka
    so->s = qemu_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
75 e6d43cfb Jan Kiszka
    if (so->s == -1) {
76 e6d43cfb Jan Kiszka
        return -1;
77 e6d43cfb Jan Kiszka
    }
78 e6d43cfb Jan Kiszka
79 e6d43cfb Jan Kiszka
    so->so_m = m;
80 e6d43cfb Jan Kiszka
    so->so_faddr = ip->ip_dst;
81 e6d43cfb Jan Kiszka
    so->so_laddr = ip->ip_src;
82 e6d43cfb Jan Kiszka
    so->so_iptos = ip->ip_tos;
83 e6d43cfb Jan Kiszka
    so->so_type = IPPROTO_ICMP;
84 e6d43cfb Jan Kiszka
    so->so_state = SS_ISFCONNECTED;
85 e6d43cfb Jan Kiszka
    so->so_expire = curtime + SO_EXPIRE;
86 e6d43cfb Jan Kiszka
87 e6d43cfb Jan Kiszka
    addr.sin_family = AF_INET;
88 e6d43cfb Jan Kiszka
    addr.sin_addr = so->so_faddr;
89 e6d43cfb Jan Kiszka
90 e6d43cfb Jan Kiszka
    insque(so, &so->slirp->icmp);
91 e6d43cfb Jan Kiszka
92 e6d43cfb Jan Kiszka
    if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
93 e6d43cfb Jan Kiszka
               (struct sockaddr *)&addr, sizeof(addr)) == -1) {
94 e6d43cfb Jan Kiszka
        DEBUG_MISC((dfd, "icmp_input icmp sendto tx errno = %d-%s\n",
95 e6d43cfb Jan Kiszka
                    errno, strerror(errno)));
96 e6d43cfb Jan Kiszka
        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
97 e6d43cfb Jan Kiszka
        icmp_detach(so);
98 e6d43cfb Jan Kiszka
    }
99 e6d43cfb Jan Kiszka
100 e6d43cfb Jan Kiszka
    return 0;
101 e6d43cfb Jan Kiszka
}
102 e6d43cfb Jan Kiszka
103 e6d43cfb Jan Kiszka
void icmp_detach(struct socket *so)
104 e6d43cfb Jan Kiszka
{
105 e6d43cfb Jan Kiszka
    closesocket(so->s);
106 e6d43cfb Jan Kiszka
    sofree(so);
107 e6d43cfb Jan Kiszka
}
108 e6d43cfb Jan Kiszka
109 f0cbd3ec bellard
/*
110 f0cbd3ec bellard
 * Process a received ICMP message.
111 f0cbd3ec bellard
 */
112 f0cbd3ec bellard
void
113 511d2b14 blueswir1
icmp_input(struct mbuf *m, int hlen)
114 f0cbd3ec bellard
{
115 f0cbd3ec bellard
  register struct icmp *icp;
116 f0cbd3ec bellard
  register struct ip *ip=mtod(m, struct ip *);
117 f0cbd3ec bellard
  int icmplen=ip->ip_len;
118 460fec67 Jan Kiszka
  Slirp *slirp = m->slirp;
119 5fafdf24 ths
120 f0cbd3ec bellard
  DEBUG_CALL("icmp_input");
121 f0cbd3ec bellard
  DEBUG_ARG("m = %lx", (long )m);
122 f0cbd3ec bellard
  DEBUG_ARG("m_len = %d", m->m_len);
123 f0cbd3ec bellard
124 f0cbd3ec bellard
  /*
125 f0cbd3ec bellard
   * Locate icmp structure in mbuf, and check
126 f0cbd3ec bellard
   * that its not corrupted and of at least minimum length.
127 f0cbd3ec bellard
   */
128 f0cbd3ec bellard
  if (icmplen < ICMP_MINLEN) {          /* min 8 bytes payload */
129 f0cbd3ec bellard
  freeit:
130 3acccfc6 Jan Kiszka
    m_free(m);
131 f0cbd3ec bellard
    goto end_error;
132 f0cbd3ec bellard
  }
133 f0cbd3ec bellard
134 f0cbd3ec bellard
  m->m_len -= hlen;
135 f0cbd3ec bellard
  m->m_data += hlen;
136 f0cbd3ec bellard
  icp = mtod(m, struct icmp *);
137 f0cbd3ec bellard
  if (cksum(m, icmplen)) {
138 f0cbd3ec bellard
    goto freeit;
139 f0cbd3ec bellard
  }
140 f0cbd3ec bellard
  m->m_len += hlen;
141 f0cbd3ec bellard
  m->m_data -= hlen;
142 3b46e624 ths
143 f0cbd3ec bellard
  DEBUG_ARG("icmp_type = %d", icp->icmp_type);
144 f0cbd3ec bellard
  switch (icp->icmp_type) {
145 f0cbd3ec bellard
  case ICMP_ECHO:
146 f0cbd3ec bellard
    ip->ip_len += hlen;                     /* since ip_input subtracts this */
147 460fec67 Jan Kiszka
    if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
148 f0cbd3ec bellard
      icmp_reflect(m);
149 12b513d8 Jan Kiszka
    } else if (slirp->restricted) {
150 12b513d8 Jan Kiszka
        goto freeit;
151 f0cbd3ec bellard
    } else {
152 f0cbd3ec bellard
      struct socket *so;
153 f0cbd3ec bellard
      struct sockaddr_in addr;
154 460fec67 Jan Kiszka
      if ((so = socreate(slirp)) == NULL) goto freeit;
155 e6d43cfb Jan Kiszka
      if (icmp_send(so, m, hlen) == 0) {
156 e6d43cfb Jan Kiszka
        return;
157 e6d43cfb Jan Kiszka
      }
158 f0cbd3ec bellard
      if(udp_attach(so) == -1) {
159 5fafdf24 ths
        DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
160 f0cbd3ec bellard
                    errno,strerror(errno)));
161 f0cbd3ec bellard
        sofree(so);
162 f0cbd3ec bellard
        m_free(m);
163 f0cbd3ec bellard
        goto end_error;
164 f0cbd3ec bellard
      }
165 f0cbd3ec bellard
      so->so_m = m;
166 f0cbd3ec bellard
      so->so_faddr = ip->ip_dst;
167 f0cbd3ec bellard
      so->so_fport = htons(7);
168 f0cbd3ec bellard
      so->so_laddr = ip->ip_src;
169 f0cbd3ec bellard
      so->so_lport = htons(9);
170 f0cbd3ec bellard
      so->so_iptos = ip->ip_tos;
171 f0cbd3ec bellard
      so->so_type = IPPROTO_ICMP;
172 f0cbd3ec bellard
      so->so_state = SS_ISFCONNECTED;
173 3b46e624 ths
174 f0cbd3ec bellard
      /* Send the packet */
175 f0cbd3ec bellard
      addr.sin_family = AF_INET;
176 460fec67 Jan Kiszka
      if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
177 460fec67 Jan Kiszka
          slirp->vnetwork_addr.s_addr) {
178 f0cbd3ec bellard
        /* It's an alias */
179 460fec67 Jan Kiszka
        if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
180 df7a86ed Ed Swierk
          if (get_dns_addr(&addr.sin_addr) < 0)
181 df7a86ed Ed Swierk
            addr.sin_addr = loopback_addr;
182 a13a4126 Jan Kiszka
        } else {
183 f0cbd3ec bellard
          addr.sin_addr = loopback_addr;
184 f0cbd3ec bellard
        }
185 f0cbd3ec bellard
      } else {
186 f0cbd3ec bellard
        addr.sin_addr = so->so_faddr;
187 f0cbd3ec bellard
      }
188 f0cbd3ec bellard
      addr.sin_port = so->so_fport;
189 f0cbd3ec bellard
      if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
190 f0cbd3ec bellard
                (struct sockaddr *)&addr, sizeof(addr)) == -1) {
191 f0cbd3ec bellard
        DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
192 f0cbd3ec bellard
                    errno,strerror(errno)));
193 5fafdf24 ths
        icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
194 f0cbd3ec bellard
        udp_detach(so);
195 f0cbd3ec bellard
      }
196 8dbca8dd bellard
    } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
197 f0cbd3ec bellard
    break;
198 f0cbd3ec bellard
  case ICMP_UNREACH:
199 f0cbd3ec bellard
    /* XXX? report error? close socket? */
200 f0cbd3ec bellard
  case ICMP_TIMXCEED:
201 f0cbd3ec bellard
  case ICMP_PARAMPROB:
202 f0cbd3ec bellard
  case ICMP_SOURCEQUENCH:
203 f0cbd3ec bellard
  case ICMP_TSTAMP:
204 f0cbd3ec bellard
  case ICMP_MASKREQ:
205 f0cbd3ec bellard
  case ICMP_REDIRECT:
206 3acccfc6 Jan Kiszka
    m_free(m);
207 f0cbd3ec bellard
    break;
208 3b46e624 ths
209 f0cbd3ec bellard
  default:
210 3acccfc6 Jan Kiszka
    m_free(m);
211 f0cbd3ec bellard
  } /* swith */
212 f0cbd3ec bellard
213 f0cbd3ec bellard
end_error:
214 f0cbd3ec bellard
  /* m is m_free()'d xor put in a socket xor or given to ip_send */
215 f0cbd3ec bellard
  return;
216 f0cbd3ec bellard
}
217 f0cbd3ec bellard
218 f0cbd3ec bellard
219 f0cbd3ec bellard
/*
220 f0cbd3ec bellard
 *        Send an ICMP message in response to a situation
221 f0cbd3ec bellard
 *
222 f0cbd3ec bellard
 *        RFC 1122: 3.2.2        MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
223 f0cbd3ec bellard
 *                        MUST NOT change this header information.
224 f0cbd3ec bellard
 *                        MUST NOT reply to a multicast/broadcast IP address.
225 f0cbd3ec bellard
 *                        MUST NOT reply to a multicast/broadcast MAC address.
226 f0cbd3ec bellard
 *                        MUST reply to only the first fragment.
227 f0cbd3ec bellard
 */
228 f0cbd3ec bellard
/*
229 f0cbd3ec bellard
 * Send ICMP_UNREACH back to the source regarding msrc.
230 f0cbd3ec bellard
 * mbuf *msrc is used as a template, but is NOT m_free()'d.
231 f0cbd3ec bellard
 * It is reported as the bad ip packet.  The header should
232 f0cbd3ec bellard
 * be fully correct and in host byte order.
233 5fafdf24 ths
 * ICMP fragmentation is illegal.  All machines must accept 576 bytes in one
234 f0cbd3ec bellard
 * packet.  The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
235 f0cbd3ec bellard
 */
236 f0cbd3ec bellard
237 f0cbd3ec bellard
#define ICMP_MAXDATALEN (IP_MSS-28)
238 f0cbd3ec bellard
void
239 7ccfb2eb blueswir1
icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
240 7ccfb2eb blueswir1
           const char *message)
241 f0cbd3ec bellard
{
242 f0cbd3ec bellard
  unsigned hlen, shlen, s_ip_len;
243 f0cbd3ec bellard
  register struct ip *ip;
244 f0cbd3ec bellard
  register struct icmp *icp;
245 f0cbd3ec bellard
  register struct mbuf *m;
246 f0cbd3ec bellard
247 f0cbd3ec bellard
  DEBUG_CALL("icmp_error");
248 f0cbd3ec bellard
  DEBUG_ARG("msrc = %lx", (long )msrc);
249 f0cbd3ec bellard
  DEBUG_ARG("msrc_len = %d", msrc->m_len);
250 f0cbd3ec bellard
251 f0cbd3ec bellard
  if(type!=ICMP_UNREACH && type!=ICMP_TIMXCEED) goto end_error;
252 f0cbd3ec bellard
253 f0cbd3ec bellard
  /* check msrc */
254 f0cbd3ec bellard
  if(!msrc) goto end_error;
255 f0cbd3ec bellard
  ip = mtod(msrc, struct ip *);
256 eb38c52c blueswir1
#ifdef DEBUG
257 f0cbd3ec bellard
  { char bufa[20], bufb[20];
258 f0cbd3ec bellard
    strcpy(bufa, inet_ntoa(ip->ip_src));
259 f0cbd3ec bellard
    strcpy(bufb, inet_ntoa(ip->ip_dst));
260 f0cbd3ec bellard
    DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
261 f0cbd3ec bellard
  }
262 f0cbd3ec bellard
#endif
263 f0cbd3ec bellard
  if(ip->ip_off & IP_OFFMASK) goto end_error;    /* Only reply to fragment 0 */
264 f0cbd3ec bellard
265 f0cbd3ec bellard
  shlen=ip->ip_hl << 2;
266 f0cbd3ec bellard
  s_ip_len=ip->ip_len;
267 f0cbd3ec bellard
  if(ip->ip_p == IPPROTO_ICMP) {
268 f0cbd3ec bellard
    icp = (struct icmp *)((char *)ip + shlen);
269 f0cbd3ec bellard
    /*
270 f0cbd3ec bellard
     *        Assume any unknown ICMP type is an error. This isn't
271 f0cbd3ec bellard
     *        specified by the RFC, but think about it..
272 f0cbd3ec bellard
     */
273 f0cbd3ec bellard
    if(icp->icmp_type>18 || icmp_flush[icp->icmp_type]) goto end_error;
274 f0cbd3ec bellard
  }
275 f0cbd3ec bellard
276 f0cbd3ec bellard
  /* make a copy */
277 460fec67 Jan Kiszka
  m = m_get(msrc->slirp);
278 460fec67 Jan Kiszka
  if (!m) {
279 460fec67 Jan Kiszka
      goto end_error;
280 460fec67 Jan Kiszka
  }
281 460fec67 Jan Kiszka
282 f0cbd3ec bellard
  { int new_m_size;
283 f0cbd3ec bellard
    new_m_size=sizeof(struct ip )+ICMP_MINLEN+msrc->m_len+ICMP_MAXDATALEN;
284 f0cbd3ec bellard
    if(new_m_size>m->m_size) m_inc(m, new_m_size);
285 f0cbd3ec bellard
  }
286 f0cbd3ec bellard
  memcpy(m->m_data, msrc->m_data, msrc->m_len);
287 f0cbd3ec bellard
  m->m_len = msrc->m_len;                        /* copy msrc to m */
288 f0cbd3ec bellard
289 f0cbd3ec bellard
  /* make the header of the reply packet */
290 f0cbd3ec bellard
  ip  = mtod(m, struct ip *);
291 f0cbd3ec bellard
  hlen= sizeof(struct ip );     /* no options in reply */
292 3b46e624 ths
293 f0cbd3ec bellard
  /* fill in icmp */
294 3b46e624 ths
  m->m_data += hlen;
295 f0cbd3ec bellard
  m->m_len -= hlen;
296 f0cbd3ec bellard
297 f0cbd3ec bellard
  icp = mtod(m, struct icmp *);
298 f0cbd3ec bellard
299 f0cbd3ec bellard
  if(minsize) s_ip_len=shlen+ICMP_MINLEN;   /* return header+8b only */
300 f0cbd3ec bellard
  else if(s_ip_len>ICMP_MAXDATALEN)         /* maximum size */
301 f0cbd3ec bellard
    s_ip_len=ICMP_MAXDATALEN;
302 f0cbd3ec bellard
303 3b46e624 ths
  m->m_len=ICMP_MINLEN+s_ip_len;        /* 8 bytes ICMP header */
304 f0cbd3ec bellard
305 f0cbd3ec bellard
  /* min. size = 8+sizeof(struct ip)+8 */
306 f0cbd3ec bellard
307 f0cbd3ec bellard
  icp->icmp_type = type;
308 f0cbd3ec bellard
  icp->icmp_code = code;
309 f0cbd3ec bellard
  icp->icmp_id = 0;
310 f0cbd3ec bellard
  icp->icmp_seq = 0;
311 f0cbd3ec bellard
312 f0cbd3ec bellard
  memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len);   /* report the ip packet */
313 f0cbd3ec bellard
  HTONS(icp->icmp_ip.ip_len);
314 f0cbd3ec bellard
  HTONS(icp->icmp_ip.ip_id);
315 f0cbd3ec bellard
  HTONS(icp->icmp_ip.ip_off);
316 f0cbd3ec bellard
317 eb38c52c blueswir1
#ifdef DEBUG
318 f0cbd3ec bellard
  if(message) {           /* DEBUG : append message to ICMP packet */
319 f0cbd3ec bellard
    int message_len;
320 f0cbd3ec bellard
    char *cpnt;
321 f0cbd3ec bellard
    message_len=strlen(message);
322 f0cbd3ec bellard
    if(message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
323 f0cbd3ec bellard
    cpnt=(char *)m->m_data+m->m_len;
324 f0cbd3ec bellard
    memcpy(cpnt, message, message_len);
325 f0cbd3ec bellard
    m->m_len+=message_len;
326 f0cbd3ec bellard
  }
327 f0cbd3ec bellard
#endif
328 f0cbd3ec bellard
329 f0cbd3ec bellard
  icp->icmp_cksum = 0;
330 f0cbd3ec bellard
  icp->icmp_cksum = cksum(m, m->m_len);
331 f0cbd3ec bellard
332 f0cbd3ec bellard
  m->m_data -= hlen;
333 f0cbd3ec bellard
  m->m_len += hlen;
334 f0cbd3ec bellard
335 f0cbd3ec bellard
  /* fill in ip */
336 f0cbd3ec bellard
  ip->ip_hl = hlen >> 2;
337 f0cbd3ec bellard
  ip->ip_len = m->m_len;
338 3b46e624 ths
339 f0cbd3ec bellard
  ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0);  /* high priority for errors */
340 f0cbd3ec bellard
341 f0cbd3ec bellard
  ip->ip_ttl = MAXTTL;
342 f0cbd3ec bellard
  ip->ip_p = IPPROTO_ICMP;
343 f0cbd3ec bellard
  ip->ip_dst = ip->ip_src;    /* ip adresses */
344 460fec67 Jan Kiszka
  ip->ip_src = m->slirp->vhost_addr;
345 f0cbd3ec bellard
346 f0cbd3ec bellard
  (void ) ip_output((struct socket *)NULL, m);
347 3b46e624 ths
348 f0cbd3ec bellard
end_error:
349 f0cbd3ec bellard
  return;
350 f0cbd3ec bellard
}
351 f0cbd3ec bellard
#undef ICMP_MAXDATALEN
352 f0cbd3ec bellard
353 f0cbd3ec bellard
/*
354 f0cbd3ec bellard
 * Reflect the ip packet back to the source
355 f0cbd3ec bellard
 */
356 f0cbd3ec bellard
void
357 511d2b14 blueswir1
icmp_reflect(struct mbuf *m)
358 f0cbd3ec bellard
{
359 f0cbd3ec bellard
  register struct ip *ip = mtod(m, struct ip *);
360 f0cbd3ec bellard
  int hlen = ip->ip_hl << 2;
361 f0cbd3ec bellard
  int optlen = hlen - sizeof(struct ip );
362 f0cbd3ec bellard
  register struct icmp *icp;
363 f0cbd3ec bellard
364 f0cbd3ec bellard
  /*
365 f0cbd3ec bellard
   * Send an icmp packet back to the ip level,
366 f0cbd3ec bellard
   * after supplying a checksum.
367 f0cbd3ec bellard
   */
368 f0cbd3ec bellard
  m->m_data += hlen;
369 f0cbd3ec bellard
  m->m_len -= hlen;
370 f0cbd3ec bellard
  icp = mtod(m, struct icmp *);
371 f0cbd3ec bellard
372 e6d43cfb Jan Kiszka
  icp->icmp_type = ICMP_ECHOREPLY;
373 f0cbd3ec bellard
  icp->icmp_cksum = 0;
374 f0cbd3ec bellard
  icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
375 f0cbd3ec bellard
376 f0cbd3ec bellard
  m->m_data -= hlen;
377 f0cbd3ec bellard
  m->m_len += hlen;
378 f0cbd3ec bellard
379 f0cbd3ec bellard
  /* fill in ip */
380 f0cbd3ec bellard
  if (optlen > 0) {
381 f0cbd3ec bellard
    /*
382 f0cbd3ec bellard
     * Strip out original options by copying rest of first
383 f0cbd3ec bellard
     * mbuf's data back, and adjust the IP length.
384 f0cbd3ec bellard
     */
385 f0cbd3ec bellard
    memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
386 f0cbd3ec bellard
            (unsigned )(m->m_len - hlen));
387 f0cbd3ec bellard
    hlen -= optlen;
388 f0cbd3ec bellard
    ip->ip_hl = hlen >> 2;
389 f0cbd3ec bellard
    ip->ip_len -= optlen;
390 f0cbd3ec bellard
    m->m_len -= optlen;
391 f0cbd3ec bellard
  }
392 f0cbd3ec bellard
393 f0cbd3ec bellard
  ip->ip_ttl = MAXTTL;
394 f0cbd3ec bellard
  { /* swap */
395 f0cbd3ec bellard
    struct in_addr icmp_dst;
396 f0cbd3ec bellard
    icmp_dst = ip->ip_dst;
397 f0cbd3ec bellard
    ip->ip_dst = ip->ip_src;
398 f0cbd3ec bellard
    ip->ip_src = icmp_dst;
399 f0cbd3ec bellard
  }
400 f0cbd3ec bellard
401 f0cbd3ec bellard
  (void ) ip_output((struct socket *)NULL, m);
402 f0cbd3ec bellard
}
403 e6d43cfb Jan Kiszka
404 e6d43cfb Jan Kiszka
void icmp_receive(struct socket *so)
405 e6d43cfb Jan Kiszka
{
406 e6d43cfb Jan Kiszka
    struct mbuf *m = so->so_m;
407 e6d43cfb Jan Kiszka
    struct ip *ip = mtod(m, struct ip *);
408 e6d43cfb Jan Kiszka
    int hlen = ip->ip_hl << 2;
409 e6d43cfb Jan Kiszka
    u_char error_code;
410 e6d43cfb Jan Kiszka
    struct icmp *icp;
411 e6d43cfb Jan Kiszka
    int id, len;
412 e6d43cfb Jan Kiszka
413 e6d43cfb Jan Kiszka
    m->m_data += hlen;
414 e6d43cfb Jan Kiszka
    m->m_len -= hlen;
415 e6d43cfb Jan Kiszka
    icp = mtod(m, struct icmp *);
416 e6d43cfb Jan Kiszka
417 e6d43cfb Jan Kiszka
    id = icp->icmp_id;
418 00aa0040 Blue Swirl
    len = qemu_recv(so->s, icp, m->m_len, 0);
419 e6d43cfb Jan Kiszka
    icp->icmp_id = id;
420 e6d43cfb Jan Kiszka
421 e6d43cfb Jan Kiszka
    m->m_data -= hlen;
422 e6d43cfb Jan Kiszka
    m->m_len += hlen;
423 e6d43cfb Jan Kiszka
424 e6d43cfb Jan Kiszka
    if (len == -1 || len == 0) {
425 e6d43cfb Jan Kiszka
        if (errno == ENETUNREACH) {
426 e6d43cfb Jan Kiszka
            error_code = ICMP_UNREACH_NET;
427 e6d43cfb Jan Kiszka
        } else {
428 e6d43cfb Jan Kiszka
            error_code = ICMP_UNREACH_HOST;
429 e6d43cfb Jan Kiszka
        }
430 e6d43cfb Jan Kiszka
        DEBUG_MISC((dfd, " udp icmp rx errno = %d-%s\n", errno,
431 e6d43cfb Jan Kiszka
                    strerror(errno)));
432 e6d43cfb Jan Kiszka
        icmp_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
433 e6d43cfb Jan Kiszka
    } else {
434 e6d43cfb Jan Kiszka
        icmp_reflect(so->so_m);
435 e6d43cfb Jan Kiszka
        so->so_m = NULL; /* Don't m_free() it again! */
436 e6d43cfb Jan Kiszka
    }
437 e6d43cfb Jan Kiszka
    icmp_detach(so);
438 e6d43cfb Jan Kiszka
}