1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1988, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
|
---|
34 | * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include "slirp.h"
|
---|
38 | #include "ip_icmp.h"
|
---|
39 | #ifdef RT_OS_WINDOWS
|
---|
40 | #include <Icmpapi.h>
|
---|
41 | #include <Iphlpapi.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /* The message sent when emulating PING */
|
---|
46 | /* Be nice and tell them it's just a psuedo-ping packet */
|
---|
47 | static const char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
|
---|
48 |
|
---|
49 | /* list of actions for icmp_error() on RX of an icmp message */
|
---|
50 | static const int icmp_flush[19] =
|
---|
51 | {
|
---|
52 | /* ECHO REPLY (0) */ 0,
|
---|
53 | 1,
|
---|
54 | 1,
|
---|
55 | /* DEST UNREACH (3) */ 1,
|
---|
56 | /* SOURCE QUENCH (4)*/ 1,
|
---|
57 | /* REDIRECT (5) */ 1,
|
---|
58 | 1,
|
---|
59 | 1,
|
---|
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) */ 0
|
---|
71 | };
|
---|
72 |
|
---|
73 | #ifdef VBOX_WITH_SLIRP_ICMP
|
---|
74 | int
|
---|
75 | icmp_init(PNATState pData)
|
---|
76 | {
|
---|
77 | pData->icmp_socket.so_type = IPPROTO_ICMP;
|
---|
78 | pData->icmp_socket.so_state = SS_ISFCONNECTED;
|
---|
79 | #ifndef RT_OS_WINDOWS
|
---|
80 | pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
|
---|
81 | if (pData->icmp_socket.s == -1)
|
---|
82 | {
|
---|
83 | int rc = RTErrConvertFromErrno(errno);
|
---|
84 | LogRel(("NAT: ICMP/ping not available (could open ICMP socket, error %Rrc)\n", rc));
|
---|
85 | return 1;
|
---|
86 | }
|
---|
87 | #else /* RT_OS_WINDOWS */
|
---|
88 | pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
|
---|
89 | if (pData->hmIcmpLibrary != NULL)
|
---|
90 | {
|
---|
91 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
92 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
93 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
94 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
95 | }
|
---|
96 | if (pData->pfIcmpParseReplies == NULL)
|
---|
97 | {
|
---|
98 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
99 | pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
|
---|
100 | if (pData->hmIcmpLibrary == NULL)
|
---|
101 | {
|
---|
102 | LogRel(("NAT: Icmp.dll could not be loaded\n"));
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
106 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
107 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
108 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
109 | }
|
---|
110 | if (pData->pfIcmpParseReplies == NULL)
|
---|
111 | {
|
---|
112 | LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
|
---|
113 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 | if (pData->pfIcmpCloseHandle == NULL)
|
---|
117 | {
|
---|
118 | LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
|
---|
119 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 | pData->icmp_socket.sh = IcmpCreateFile();
|
---|
123 | pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
124 | pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
|
---|
125 | pData->pvIcmpBuffer = malloc(pData->szIcmpBuffer);
|
---|
126 | #endif /* RT_OS_WINDOWS */
|
---|
127 | LIST_INIT(&pData->icmp_msg_head);
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * ip here is ip header + 64bytes readed from ICMP packet
|
---|
133 | */
|
---|
134 | struct icmp_msg *
|
---|
135 | icmp_find_original_mbuf(PNATState pData, struct ip *ip)
|
---|
136 | {
|
---|
137 | struct mbuf *m0;
|
---|
138 | struct ip *ip0;
|
---|
139 | struct icmp *icp, *icp0;
|
---|
140 | struct icmp_msg *icm = NULL;
|
---|
141 | int found = 0;
|
---|
142 | struct udphdr *udp;
|
---|
143 | struct tcphdr *tcp;
|
---|
144 | struct socket *head_socket, *so;
|
---|
145 | struct in_addr laddr, faddr;
|
---|
146 | u_int lport, fport;
|
---|
147 |
|
---|
148 | laddr.s_addr = ~0;
|
---|
149 | faddr.s_addr = ~0;
|
---|
150 |
|
---|
151 | lport = ~0;
|
---|
152 | fport = ~0;
|
---|
153 |
|
---|
154 |
|
---|
155 | switch (ip->ip_p)
|
---|
156 | {
|
---|
157 | case IPPROTO_ICMP:
|
---|
158 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
159 | LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
|
---|
160 | {
|
---|
161 | m0 = icm->im_m;
|
---|
162 | ip0 = mtod(m0, struct ip *);
|
---|
163 | AssertRelease(ip0->ip_p == IPPROTO_ICMP);
|
---|
164 | icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
165 | if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
|
---|
166 | || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
|
---|
167 | && icp->icmp_id == icp0->icmp_id
|
---|
168 | && icp->icmp_seq == icp->icmp_seq)
|
---|
169 | {
|
---|
170 | found = 1;
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | /*
|
---|
175 | * for TCP and UDP logic little bit reverted, we try to find the HOST socket
|
---|
176 | * from which the IP package has been sent.
|
---|
177 | */
|
---|
178 | case IPPROTO_UDP:
|
---|
179 | head_socket = &udb;
|
---|
180 | udp = (struct udphdr *)((char *)ip + (ip->ip_hl >> 2));
|
---|
181 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
182 | fport = udp->uh_dport;
|
---|
183 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
184 | lport = udp->uh_sport;
|
---|
185 | case IPPROTO_TCP:
|
---|
186 | if (head_socket == NULL)
|
---|
187 | {
|
---|
188 | tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl >> 2));
|
---|
189 | head_socket = &tcb; /* head_socket could be initialized with udb*/
|
---|
190 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
191 | fport = tcp->th_dport;
|
---|
192 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
193 | lport = tcp->th_sport;
|
---|
194 | }
|
---|
195 | for (so = head_socket; so != head_socket; so = so->so_next)
|
---|
196 | {
|
---|
197 | /* Should be reaplaced by hash here */
|
---|
198 | if (so->so_faddr.s_addr == faddr.s_addr
|
---|
199 | && so->so_fport == fport
|
---|
200 | && so->so_hladdr.s_addr == laddr.s_addr
|
---|
201 | && so->so_hlport == lport) {
|
---|
202 | icm = malloc(sizeof(struct icmp_msg));
|
---|
203 | icm->im_m = so->so_m;
|
---|
204 | found = 1;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | break;
|
---|
208 | default:
|
---|
209 | LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
|
---|
210 | }
|
---|
211 | if (found == 1)
|
---|
212 | return icm;
|
---|
213 |
|
---|
214 | return NULL;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static int
|
---|
218 | icmp_attach(PNATState pData, struct mbuf *m)
|
---|
219 | {
|
---|
220 | struct icmp_msg *icm;
|
---|
221 | struct ip *ip;
|
---|
222 | ip = mtod(m, struct ip *);
|
---|
223 | Assert(ip->ip_p == IPPROTO_ICMP);
|
---|
224 | icm = malloc(sizeof(struct icmp_msg));
|
---|
225 | icm->im_m = m;
|
---|
226 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
227 | return (0);
|
---|
228 | }
|
---|
229 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Process a received ICMP message.
|
---|
233 | */
|
---|
234 | void
|
---|
235 | icmp_input(PNATState pData, struct mbuf *m, int hlen)
|
---|
236 | {
|
---|
237 | register struct icmp *icp;
|
---|
238 | register struct ip *ip=mtod(m, struct ip *);
|
---|
239 | int icmplen=ip->ip_len;
|
---|
240 | int status;
|
---|
241 | uint32_t dst;
|
---|
242 |
|
---|
243 | /* int code; */
|
---|
244 |
|
---|
245 | DEBUG_CALL("icmp_input");
|
---|
246 | DEBUG_ARG("m = %lx", (long )m);
|
---|
247 | DEBUG_ARG("m_len = %d", m->m_len);
|
---|
248 |
|
---|
249 | icmpstat.icps_received++;
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Locate icmp structure in mbuf, and check
|
---|
253 | * that its not corrupted and of at least minimum length.
|
---|
254 | */
|
---|
255 | if (icmplen < ICMP_MINLEN)
|
---|
256 | {
|
---|
257 | /* min 8 bytes payload */
|
---|
258 | icmpstat.icps_tooshort++;
|
---|
259 | freeit:
|
---|
260 | m_freem(pData, m);
|
---|
261 | goto end_error;
|
---|
262 | }
|
---|
263 |
|
---|
264 | m->m_len -= hlen;
|
---|
265 | m->m_data += hlen;
|
---|
266 | icp = mtod(m, struct icmp *);
|
---|
267 | if (cksum(m, icmplen))
|
---|
268 | {
|
---|
269 | icmpstat.icps_checksum++;
|
---|
270 | goto freeit;
|
---|
271 | }
|
---|
272 | m->m_len += hlen;
|
---|
273 | m->m_data -= hlen;
|
---|
274 |
|
---|
275 | /* icmpstat.icps_inhist[icp->icmp_type]++; */
|
---|
276 | /* code = icp->icmp_code; */
|
---|
277 |
|
---|
278 | DEBUG_ARG("icmp_type = %d", icp->icmp_type);
|
---|
279 | switch (icp->icmp_type)
|
---|
280 | {
|
---|
281 | case ICMP_ECHO:
|
---|
282 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
283 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
284 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
285 |
|
---|
286 | ip->ip_len += hlen; /* since ip_input subtracts this */
|
---|
287 | dst = ip->ip_dst.s_addr;
|
---|
288 | if (dst == alias_addr.s_addr)
|
---|
289 | {
|
---|
290 | #ifdef VBOX_WITH_SLIRP_ICMP
|
---|
291 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
292 | ip->ip_dst.s_addr = ip->ip_src.s_addr;
|
---|
293 | ip->ip_src.s_addr = dst;
|
---|
294 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
295 | icmp_reflect(pData, m);
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | struct socket *so;
|
---|
300 | struct sockaddr_in addr;
|
---|
301 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
302 | if ((so = socreate()) == NULL)
|
---|
303 | goto freeit;
|
---|
304 | if (udp_attach(pData, so) == -1)
|
---|
305 | {
|
---|
306 | DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
|
---|
307 | errno,strerror(errno)));
|
---|
308 | sofree(pData, so);
|
---|
309 | m_free(pData, m);
|
---|
310 | goto end_error;
|
---|
311 | }
|
---|
312 | so->so_m = m;
|
---|
313 | so->so_faddr = ip->ip_dst;
|
---|
314 | so->so_fport = htons(7);
|
---|
315 | so->so_laddr = ip->ip_src;
|
---|
316 | so->so_lport = htons(9);
|
---|
317 | so->so_iptos = ip->ip_tos;
|
---|
318 | so->so_type = IPPROTO_ICMP;
|
---|
319 | so->so_state = SS_ISFCONNECTED;
|
---|
320 |
|
---|
321 | addr.sin_family = AF_INET;
|
---|
322 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
323 | {
|
---|
324 | /* It's an alias */
|
---|
325 | switch (ntohl(so->so_faddr.s_addr) & ~pData->netmask)
|
---|
326 | {
|
---|
327 | case CTL_DNS:
|
---|
328 | addr.sin_addr = dns_addr;
|
---|
329 | break;
|
---|
330 | case CTL_ALIAS:
|
---|
331 | default:
|
---|
332 | addr.sin_addr = loopback_addr;
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | else
|
---|
337 | addr.sin_addr = so->so_faddr;
|
---|
338 | addr.sin_port = so->so_fport;
|
---|
339 | if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
|
---|
340 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
341 | {
|
---|
342 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
343 | errno,strerror(errno)));
|
---|
344 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
345 | udp_detach(pData, so);
|
---|
346 | }
|
---|
347 | #else /* VBOX_WITH_SLIRP_ICMP */
|
---|
348 | # ifdef RT_OS_WINDOWS
|
---|
349 | IP_OPTION_INFORMATION ipopt;
|
---|
350 | int error;
|
---|
351 | # endif
|
---|
352 | addr.sin_family = AF_INET;
|
---|
353 | if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
354 | {
|
---|
355 | /* It's an alias */
|
---|
356 | switch(ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
|
---|
357 | {
|
---|
358 | case CTL_DNS:
|
---|
359 | addr.sin_addr = dns_addr;
|
---|
360 | break;
|
---|
361 | case CTL_ALIAS:
|
---|
362 | default:
|
---|
363 | addr.sin_addr = loopback_addr;
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | else
|
---|
368 | addr.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
369 | icmp_attach(pData, m);
|
---|
370 | /* Send the packet */
|
---|
371 | # ifndef RT_OS_WINDOWS
|
---|
372 | status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL, (void *)&ip->ip_ttl, sizeof(ip->ip_ttl));
|
---|
373 | if (status < 0)
|
---|
374 | {
|
---|
375 | LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n", strerror(errno)));
|
---|
376 | }
|
---|
377 | if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
|
---|
378 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
379 | {
|
---|
380 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
381 | errno,strerror(errno)));
|
---|
382 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
383 | m_free(pData, m);
|
---|
384 | }
|
---|
385 | # else /* RT_OS_WINDOWS */
|
---|
386 | pData->icmp_socket.so_laddr.s_addr = ip->ip_src.s_addr; /* XXX: hack*/
|
---|
387 | pData->icmp_socket.so_icmp_id = icp->icmp_id;
|
---|
388 | pData->icmp_socket.so_icmp_seq = icp->icmp_seq;
|
---|
389 | memset(&ipopt, 0, sizeof(IP_OPTION_INFORMATION));
|
---|
390 | ipopt.Ttl = ip->ip_ttl;
|
---|
391 | status = IcmpSendEcho2(pData->icmp_socket.sh, pData->phEvents[VBOX_ICMP_EVENT_INDEX],
|
---|
392 | NULL, NULL, addr.sin_addr.s_addr, icp->icmp_data,
|
---|
393 | icmplen - offsetof(struct icmp, icmp_data) , &ipopt,
|
---|
394 | pData->pvIcmpBuffer, pData->szIcmpBuffer, 0);
|
---|
395 | if (status == 0 && (error = GetLastError()) != ERROR_IO_PENDING)
|
---|
396 | {
|
---|
397 | error = GetLastError();
|
---|
398 | LogRel(("NAT: Error (%d) occurred while sending ICMP (", error));
|
---|
399 | switch (error)
|
---|
400 | {
|
---|
401 | case ERROR_INVALID_PARAMETER:
|
---|
402 | LogRel(("icmp_socket:%lx is invalid)\n", pData->icmp_socket.s));
|
---|
403 | break;
|
---|
404 | case ERROR_NOT_SUPPORTED:
|
---|
405 | LogRel(("operation is unsupported)\n"));
|
---|
406 | break;
|
---|
407 | case ERROR_NOT_ENOUGH_MEMORY:
|
---|
408 | LogRel(("OOM!!!)\n"));
|
---|
409 | break;
|
---|
410 | case IP_BUF_TOO_SMALL:
|
---|
411 | LogRel(("Buffer too small)\n"));
|
---|
412 | break;
|
---|
413 | default:
|
---|
414 | LogRel(("Other error!!!)\n"));
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | # endif /* RT_OS_WINDOWS */
|
---|
419 | #endif /* VBOX_WITH_SLIRP_ICMP */
|
---|
420 | } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
|
---|
421 | break;
|
---|
422 | case ICMP_UNREACH:
|
---|
423 | /* XXX? report error? close socket? */
|
---|
424 | case ICMP_TIMXCEED:
|
---|
425 | case ICMP_PARAMPROB:
|
---|
426 | case ICMP_SOURCEQUENCH:
|
---|
427 | case ICMP_TSTAMP:
|
---|
428 | case ICMP_MASKREQ:
|
---|
429 | case ICMP_REDIRECT:
|
---|
430 | icmpstat.icps_notsupp++;
|
---|
431 | m_freem(pData, m);
|
---|
432 | break;
|
---|
433 |
|
---|
434 | default:
|
---|
435 | icmpstat.icps_badtype++;
|
---|
436 | m_freem(pData, m);
|
---|
437 | } /* switch */
|
---|
438 |
|
---|
439 | end_error:
|
---|
440 | /* m is m_free()'d xor put in a socket xor or given to ip_send */
|
---|
441 | ;
|
---|
442 | }
|
---|
443 |
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Send an ICMP message in response to a situation
|
---|
447 | *
|
---|
448 | * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
|
---|
449 | * MUST NOT change this header information.
|
---|
450 | * MUST NOT reply to a multicast/broadcast IP address.
|
---|
451 | * MUST NOT reply to a multicast/broadcast MAC address.
|
---|
452 | * MUST reply to only the first fragment.
|
---|
453 | */
|
---|
454 | /*
|
---|
455 | * Send ICMP_UNREACH back to the source regarding msrc.
|
---|
456 | * mbuf *msrc is used as a template, but is NOT m_free()'d.
|
---|
457 | * It is reported as the bad ip packet. The header should
|
---|
458 | * be fully correct and in host byte order.
|
---|
459 | * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
|
---|
460 | * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
|
---|
461 | */
|
---|
462 |
|
---|
463 | #define ICMP_MAXDATALEN (IP_MSS-28)
|
---|
464 | void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, char *message)
|
---|
465 | {
|
---|
466 | unsigned hlen, shlen, s_ip_len;
|
---|
467 | register struct ip *ip;
|
---|
468 | register struct icmp *icp;
|
---|
469 | register struct mbuf *m;
|
---|
470 |
|
---|
471 | DEBUG_CALL("icmp_error");
|
---|
472 | DEBUG_ARG("msrc = %lx", (long )msrc);
|
---|
473 | DEBUG_ARG("msrc_len = %d", msrc->m_len);
|
---|
474 |
|
---|
475 | if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
|
---|
476 | goto end_error;
|
---|
477 |
|
---|
478 | /* check msrc */
|
---|
479 | if (!msrc)
|
---|
480 | goto end_error;
|
---|
481 |
|
---|
482 | ip = mtod(msrc, struct ip *);
|
---|
483 | #if DEBUG
|
---|
484 | {
|
---|
485 | char bufa[20], bufb[20];
|
---|
486 | strcpy(bufa, inet_ntoa(ip->ip_src));
|
---|
487 | strcpy(bufb, inet_ntoa(ip->ip_dst));
|
---|
488 | DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
|
---|
489 | }
|
---|
490 | #endif
|
---|
491 | if (ip->ip_off & IP_OFFMASK)
|
---|
492 | goto end_error; /* Only reply to fragment 0 */
|
---|
493 |
|
---|
494 | shlen=ip->ip_hl << 2;
|
---|
495 | s_ip_len=ip->ip_len;
|
---|
496 | if (ip->ip_p == IPPROTO_ICMP)
|
---|
497 | {
|
---|
498 | icp = (struct icmp *)((char *)ip + shlen);
|
---|
499 | /*
|
---|
500 | * Assume any unknown ICMP type is an error. This isn't
|
---|
501 | * specified by the RFC, but think about it..
|
---|
502 | */
|
---|
503 | if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
|
---|
504 | goto end_error;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* make a copy */
|
---|
508 | if (!(m=m_get(pData)))
|
---|
509 | goto end_error; /* get mbuf */
|
---|
510 | {
|
---|
511 | int new_m_size;
|
---|
512 | new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
|
---|
513 | if (new_m_size>m->m_size)
|
---|
514 | m_inc(m, new_m_size);
|
---|
515 | }
|
---|
516 | memcpy(m->m_data, msrc->m_data, msrc->m_len);
|
---|
517 | m->m_len = msrc->m_len; /* copy msrc to m */
|
---|
518 |
|
---|
519 | /* make the header of the reply packet */
|
---|
520 | ip = mtod(m, struct ip *);
|
---|
521 | hlen = sizeof(struct ip ); /* no options in reply */
|
---|
522 |
|
---|
523 | /* fill in icmp */
|
---|
524 | m->m_data += hlen;
|
---|
525 | m->m_len -= hlen;
|
---|
526 |
|
---|
527 | icp = mtod(m, struct icmp *);
|
---|
528 |
|
---|
529 | if (minsize)
|
---|
530 | s_ip_len=shlen+ICMP_MINLEN; /* return header+8b only */
|
---|
531 | else if (s_ip_len>ICMP_MAXDATALEN) /* maximum size */
|
---|
532 | s_ip_len=ICMP_MAXDATALEN;
|
---|
533 |
|
---|
534 | m->m_len=ICMP_MINLEN+s_ip_len; /* 8 bytes ICMP header */
|
---|
535 |
|
---|
536 | /* min. size = 8+sizeof(struct ip)+8 */
|
---|
537 |
|
---|
538 | icp->icmp_type = type;
|
---|
539 | icp->icmp_code = code;
|
---|
540 | icp->icmp_id = 0;
|
---|
541 | icp->icmp_seq = 0;
|
---|
542 |
|
---|
543 | memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
|
---|
544 | HTONS(icp->icmp_ip.ip_len);
|
---|
545 | HTONS(icp->icmp_ip.ip_id);
|
---|
546 | HTONS(icp->icmp_ip.ip_off);
|
---|
547 |
|
---|
548 | #if DEBUG
|
---|
549 | if (message)
|
---|
550 | {
|
---|
551 | /* DEBUG : append message to ICMP packet */
|
---|
552 | int message_len;
|
---|
553 | char *cpnt;
|
---|
554 | message_len=strlen(message);
|
---|
555 | if (message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
|
---|
556 | cpnt=(char *)m->m_data+m->m_len;
|
---|
557 | memcpy(cpnt, message, message_len);
|
---|
558 | m->m_len+=message_len;
|
---|
559 | }
|
---|
560 | #endif
|
---|
561 |
|
---|
562 | icp->icmp_cksum = 0;
|
---|
563 | icp->icmp_cksum = cksum(m, m->m_len);
|
---|
564 |
|
---|
565 | m->m_data -= hlen;
|
---|
566 | m->m_len += hlen;
|
---|
567 |
|
---|
568 | /* fill in ip */
|
---|
569 | ip->ip_hl = hlen >> 2;
|
---|
570 | ip->ip_len = m->m_len;
|
---|
571 |
|
---|
572 | ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
573 |
|
---|
574 | ip->ip_ttl = MAXTTL;
|
---|
575 | ip->ip_p = IPPROTO_ICMP;
|
---|
576 | ip->ip_dst = ip->ip_src; /* ip adresses */
|
---|
577 | ip->ip_src = alias_addr;
|
---|
578 |
|
---|
579 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
580 |
|
---|
581 | icmpstat.icps_reflect++;
|
---|
582 |
|
---|
583 | end_error:
|
---|
584 | ;
|
---|
585 | }
|
---|
586 | #undef ICMP_MAXDATALEN
|
---|
587 |
|
---|
588 | /*
|
---|
589 | * Reflect the ip packet back to the source
|
---|
590 | */
|
---|
591 | void
|
---|
592 | icmp_reflect(PNATState pData, struct mbuf *m)
|
---|
593 | {
|
---|
594 | register struct ip *ip = mtod(m, struct ip *);
|
---|
595 | int hlen = ip->ip_hl << 2;
|
---|
596 | int optlen = hlen - sizeof(struct ip );
|
---|
597 | register struct icmp *icp;
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * Send an icmp packet back to the ip level,
|
---|
601 | * after supplying a checksum.
|
---|
602 | */
|
---|
603 | m->m_data += hlen;
|
---|
604 | m->m_len -= hlen;
|
---|
605 | icp = mtod(m, struct icmp *);
|
---|
606 |
|
---|
607 | icp->icmp_cksum = 0;
|
---|
608 | icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
|
---|
609 |
|
---|
610 | m->m_data -= hlen;
|
---|
611 | m->m_len += hlen;
|
---|
612 |
|
---|
613 | #ifndef VBOX_WITH_SLIRP_ICMP
|
---|
614 | /* fill in ip */
|
---|
615 | if (optlen > 0)
|
---|
616 | {
|
---|
617 | /*
|
---|
618 | * Strip out original options by copying rest of first
|
---|
619 | * mbuf's data back, and adjust the IP length.
|
---|
620 | */
|
---|
621 | memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
|
---|
622 | (unsigned )(m->m_len - hlen));
|
---|
623 | hlen -= optlen;
|
---|
624 | ip->ip_hl = hlen >> 2;
|
---|
625 | ip->ip_len -= optlen;
|
---|
626 | m->m_len -= optlen;
|
---|
627 | }
|
---|
628 | ip->ip_ttl = MAXTTL;
|
---|
629 | {
|
---|
630 | /* swap */
|
---|
631 | struct in_addr icmp_dst;
|
---|
632 | icmp_dst = ip->ip_dst;
|
---|
633 | ip->ip_dst = ip->ip_src;
|
---|
634 | ip->ip_src = icmp_dst;
|
---|
635 | }
|
---|
636 | #endif /* !VBOX_WITH_SLIRP_ICMP */
|
---|
637 |
|
---|
638 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
639 |
|
---|
640 | icmpstat.icps_reflect++;
|
---|
641 | }
|
---|