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 | int
|
---|
74 | icmp_init(PNATState pData)
|
---|
75 | {
|
---|
76 | pData->icmp_socket.so_type = IPPROTO_ICMP;
|
---|
77 | pData->icmp_socket.so_state = SS_ISFCONNECTED;
|
---|
78 | #ifndef RT_OS_WINDOWS
|
---|
79 | # ifndef RT_OS_DARWIN
|
---|
80 | pData->icmp_socket.s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
|
---|
81 | # else /* !RT_OS_DARWIN */
|
---|
82 | pData->icmp_socket.s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
|
---|
83 | # endif /* RT_OS_DARWIN */
|
---|
84 | if (pData->icmp_socket.s == -1)
|
---|
85 | {
|
---|
86 | int rc = RTErrConvertFromErrno(errno);
|
---|
87 | LogRel(("NAT: ICMP/ping not available (could open ICMP socket, error %Rrc)\n", rc));
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 | #else /* RT_OS_WINDOWS */
|
---|
91 | pData->hmIcmpLibrary = LoadLibrary("Iphlpapi.dll");
|
---|
92 | if (pData->hmIcmpLibrary != NULL)
|
---|
93 | {
|
---|
94 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
95 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
96 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
97 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
98 | }
|
---|
99 | if (pData->pfIcmpParseReplies == NULL)
|
---|
100 | {
|
---|
101 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
102 | pData->hmIcmpLibrary = LoadLibrary("Icmp.dll");
|
---|
103 | if (pData->hmIcmpLibrary == NULL)
|
---|
104 | {
|
---|
105 | LogRel(("NAT: Icmp.dll could not be loaded\n"));
|
---|
106 | return 1;
|
---|
107 | }
|
---|
108 | pData->pfIcmpParseReplies = (long (WINAPI *)(void *, long))
|
---|
109 | GetProcAddress(pData->hmIcmpLibrary, "IcmpParseReplies");
|
---|
110 | pData->pfIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
|
---|
111 | GetProcAddress(pData->hmIcmpLibrary, "IcmpCloseHandle");
|
---|
112 | }
|
---|
113 | if (pData->pfIcmpParseReplies == NULL)
|
---|
114 | {
|
---|
115 | LogRel(("NAT: Can't find IcmpParseReplies symbol\n"));
|
---|
116 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 | if (pData->pfIcmpCloseHandle == NULL)
|
---|
120 | {
|
---|
121 | LogRel(("NAT: Can't find IcmpCloseHandle symbol\n"));
|
---|
122 | FreeLibrary(pData->hmIcmpLibrary);
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 | pData->icmp_socket.sh = IcmpCreateFile();
|
---|
126 | pData->phEvents[VBOX_ICMP_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
127 | pData->szIcmpBuffer = sizeof(ICMP_ECHO_REPLY) * 10;
|
---|
128 | pData->pvIcmpBuffer = malloc(pData->szIcmpBuffer);
|
---|
129 | #endif /* RT_OS_WINDOWS */
|
---|
130 | LIST_INIT(&pData->icmp_msg_head);
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * ip here is ip header + 64bytes readed from ICMP packet
|
---|
136 | */
|
---|
137 | struct icmp_msg *
|
---|
138 | icmp_find_original_mbuf(PNATState pData, struct ip *ip)
|
---|
139 | {
|
---|
140 | struct mbuf *m0;
|
---|
141 | struct ip *ip0;
|
---|
142 | struct icmp *icp, *icp0;
|
---|
143 | struct icmp_msg *icm = NULL;
|
---|
144 | int found = 0;
|
---|
145 | struct udphdr *udp;
|
---|
146 | struct tcphdr *tcp;
|
---|
147 | struct socket *head_socket = NULL;
|
---|
148 | struct socket *last_socket = NULL;
|
---|
149 | struct socket *so = NULL;
|
---|
150 | struct in_addr laddr, faddr;
|
---|
151 | u_short lport, fport;
|
---|
152 |
|
---|
153 | laddr.s_addr = ~0;
|
---|
154 | faddr.s_addr = ~0;
|
---|
155 |
|
---|
156 | lport = ~0;
|
---|
157 | fport = ~0;
|
---|
158 |
|
---|
159 |
|
---|
160 | Log(("%s: processing (proto:%d)\n", __FUNCTION__, ip->ip_p));
|
---|
161 | switch (ip->ip_p)
|
---|
162 | {
|
---|
163 | case IPPROTO_ICMP:
|
---|
164 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
165 | LIST_FOREACH(icm, &pData->icmp_msg_head, im_list)
|
---|
166 | {
|
---|
167 | m0 = icm->im_m;
|
---|
168 | ip0 = mtod(m0, struct ip *);
|
---|
169 | AssertRelease(ip0->ip_p == IPPROTO_ICMP);
|
---|
170 | icp0 = (struct icmp *)((char *)ip0 + (ip0->ip_hl << 2));
|
---|
171 | if ( ( (icp->icmp_type != ICMP_ECHO && ip->ip_src.s_addr == ip0->ip_dst.s_addr)
|
---|
172 | || (icp->icmp_type == ICMP_ECHO && ip->ip_dst.s_addr == ip0->ip_dst.s_addr))
|
---|
173 | && icp->icmp_id == icp0->icmp_id
|
---|
174 | && icp->icmp_seq == icp0->icmp_seq)
|
---|
175 | {
|
---|
176 | found = 1;
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | Log(("Have found nothing\n"));
|
---|
180 | }
|
---|
181 | break;
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * for TCP and UDP logic little bit reverted, we try to find the HOST socket
|
---|
185 | * from which the IP package has been sent.
|
---|
186 | */
|
---|
187 | case IPPROTO_UDP:
|
---|
188 | head_socket = &udb;
|
---|
189 | udp = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
190 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
191 | fport = udp->uh_dport;
|
---|
192 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
193 | lport = udp->uh_sport;
|
---|
194 | last_socket = udp_last_so;
|
---|
195 | /* fall through */
|
---|
196 |
|
---|
197 | case IPPROTO_TCP:
|
---|
198 | if (head_socket == NULL)
|
---|
199 | {
|
---|
200 | tcp = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
|
---|
201 | head_socket = &tcb; /* head_socket could be initialized with udb*/
|
---|
202 | faddr.s_addr = ip->ip_dst.s_addr;
|
---|
203 | fport = tcp->th_dport;
|
---|
204 | laddr.s_addr = ip->ip_src.s_addr;
|
---|
205 | lport = tcp->th_sport;
|
---|
206 | last_socket = tcp_last_so;
|
---|
207 | }
|
---|
208 | /* check last socket first */
|
---|
209 | if ( last_socket->so_faddr.s_addr == faddr.s_addr
|
---|
210 | && last_socket->so_fport == fport
|
---|
211 | && last_socket->so_hlport == lport)
|
---|
212 | {
|
---|
213 | found = 1;
|
---|
214 | so = last_socket;
|
---|
215 | goto sofound;
|
---|
216 | }
|
---|
217 | for (so = head_socket->so_prev; so != head_socket; so = so->so_prev)
|
---|
218 | {
|
---|
219 | /* Should be reaplaced by hash here */
|
---|
220 | Log(("trying:%R[natsock] against %R[IP4]:%d lport=%d hlport=%d\n", so, &faddr, fport, lport, so->so_hlport));
|
---|
221 | if ( so->so_faddr.s_addr == faddr.s_addr
|
---|
222 | && so->so_fport == fport
|
---|
223 | && so->so_hlport == lport)
|
---|
224 | {
|
---|
225 | found = 1;
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | break;
|
---|
230 |
|
---|
231 | default:
|
---|
232 | LogRel(("%s:ICMP: unsupported protocol(%d)\n", __FUNCTION__, ip->ip_p));
|
---|
233 | }
|
---|
234 | sofound:
|
---|
235 | if (found == 1 && icm == NULL)
|
---|
236 | {
|
---|
237 | icm = malloc(sizeof(struct icmp_msg));
|
---|
238 | icm->im_m = so->so_m;
|
---|
239 | icm->im_so = so;
|
---|
240 | found = 1;
|
---|
241 | Log(("hit:%R[natsock]\n", so));
|
---|
242 | /*XXX: this storage not very long,
|
---|
243 | * better add flag if it should removed from lis
|
---|
244 | */
|
---|
245 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
246 | return (icm);
|
---|
247 | }
|
---|
248 | if (found == 1)
|
---|
249 | return icm;
|
---|
250 |
|
---|
251 | return NULL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static int
|
---|
255 | icmp_attach(PNATState pData, struct mbuf *m)
|
---|
256 | {
|
---|
257 | struct icmp_msg *icm;
|
---|
258 | struct ip *ip;
|
---|
259 | ip = mtod(m, struct ip *);
|
---|
260 | Assert(ip->ip_p == IPPROTO_ICMP);
|
---|
261 | icm = malloc(sizeof(struct icmp_msg));
|
---|
262 | icm->im_m = m;
|
---|
263 | LIST_INSERT_HEAD(&pData->icmp_msg_head, icm, im_list);
|
---|
264 | return 0;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*
|
---|
268 | * Process a received ICMP message.
|
---|
269 | */
|
---|
270 | void
|
---|
271 | icmp_input(PNATState pData, struct mbuf *m, int hlen)
|
---|
272 | {
|
---|
273 | register struct icmp *icp;
|
---|
274 | register struct ip *ip = mtod(m, struct ip *);
|
---|
275 | int icmplen = ip->ip_len;
|
---|
276 | int status;
|
---|
277 | uint32_t dst;
|
---|
278 | #if !defined(RT_OS_WINDOWS)
|
---|
279 | int ttl;
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | /* int code; */
|
---|
283 |
|
---|
284 | DEBUG_CALL("icmp_input");
|
---|
285 | DEBUG_ARG("m = %lx", (long )m);
|
---|
286 | DEBUG_ARG("m_len = %d", m->m_len);
|
---|
287 |
|
---|
288 | icmpstat.icps_received++;
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Locate icmp structure in mbuf, and check
|
---|
292 | * that its not corrupted and of at least minimum length.
|
---|
293 | */
|
---|
294 | if (icmplen < ICMP_MINLEN)
|
---|
295 | {
|
---|
296 | /* min 8 bytes payload */
|
---|
297 | icmpstat.icps_tooshort++;
|
---|
298 | freeit:
|
---|
299 | m_freem(pData, m);
|
---|
300 | goto end_error;
|
---|
301 | }
|
---|
302 |
|
---|
303 | m->m_len -= hlen;
|
---|
304 | m->m_data += hlen;
|
---|
305 | icp = mtod(m, struct icmp *);
|
---|
306 | if (cksum(m, icmplen))
|
---|
307 | {
|
---|
308 | icmpstat.icps_checksum++;
|
---|
309 | goto freeit;
|
---|
310 | }
|
---|
311 | m->m_len += hlen;
|
---|
312 | m->m_data -= hlen;
|
---|
313 |
|
---|
314 | /* icmpstat.icps_inhist[icp->icmp_type]++; */
|
---|
315 | /* code = icp->icmp_code; */
|
---|
316 |
|
---|
317 | DEBUG_ARG("icmp_type = %d", icp->icmp_type);
|
---|
318 | switch (icp->icmp_type)
|
---|
319 | {
|
---|
320 | case ICMP_ECHO:
|
---|
321 | ip->ip_len += hlen; /* since ip_input subtracts this */
|
---|
322 | dst = ip->ip_dst.s_addr;
|
---|
323 | if (dst == alias_addr.s_addr)
|
---|
324 | {
|
---|
325 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
326 | ip->ip_dst.s_addr = ip->ip_src.s_addr;
|
---|
327 | ip->ip_src.s_addr = dst;
|
---|
328 | icmp_reflect(pData, m);
|
---|
329 | }
|
---|
330 | else
|
---|
331 | {
|
---|
332 | struct sockaddr_in addr;
|
---|
333 | #ifdef RT_OS_WINDOWS
|
---|
334 | IP_OPTION_INFORMATION ipopt;
|
---|
335 | int error;
|
---|
336 | #endif
|
---|
337 | addr.sin_family = AF_INET;
|
---|
338 | if ((ip->ip_dst.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
339 | {
|
---|
340 | /* It's an alias */
|
---|
341 | switch (ntohl(ip->ip_dst.s_addr) & ~pData->netmask)
|
---|
342 | {
|
---|
343 | case CTL_DNS:
|
---|
344 | addr.sin_addr = dns_addr;
|
---|
345 | break;
|
---|
346 | case CTL_ALIAS:
|
---|
347 | default:
|
---|
348 | addr.sin_addr = loopback_addr;
|
---|
349 | break;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | else
|
---|
353 | addr.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
354 | #ifndef RT_OS_WINDOWS
|
---|
355 | if (pData->icmp_socket.s != -1)
|
---|
356 | {
|
---|
357 | icmp_attach(pData, m);
|
---|
358 | ttl = ip->ip_ttl;
|
---|
359 | Log(("NAT/ICMP: try to set TTL(%d)\n", ttl));
|
---|
360 | status = setsockopt(pData->icmp_socket.s, IPPROTO_IP, IP_TTL,
|
---|
361 | (void *)&ttl, sizeof(ttl));
|
---|
362 | if (status < 0)
|
---|
363 | LogRel(("NAT: Error (%s) occurred while setting TTL attribute of IP packet\n",
|
---|
364 | strerror(errno)));
|
---|
365 | if (sendto(pData->icmp_socket.s, icp, icmplen, 0,
|
---|
366 | (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
---|
367 | {
|
---|
368 | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
---|
369 | errno, strerror(errno)));
|
---|
370 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
371 | m_free(pData, m);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * We're freeing the ICMP message, which unable sent or process.
|
---|
378 | * That behavior described in rfc 793, we shouldn't notify sender about
|
---|
379 | * fail of processing it's ICMP packets
|
---|
380 | */
|
---|
381 | m_free(pData, m);
|
---|
382 | return;
|
---|
383 | }
|
---|
384 | #else /* RT_OS_WINDOWS */
|
---|
385 | icmp_attach(pData, m);
|
---|
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, 1);
|
---|
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 | } /* if ip->ip_dst.s_addr == alias_addr.s_addr */
|
---|
420 | break;
|
---|
421 | case ICMP_UNREACH:
|
---|
422 | /* XXX? report error? close socket? */
|
---|
423 | case ICMP_TIMXCEED:
|
---|
424 | case ICMP_PARAMPROB:
|
---|
425 | case ICMP_SOURCEQUENCH:
|
---|
426 | case ICMP_TSTAMP:
|
---|
427 | case ICMP_MASKREQ:
|
---|
428 | case ICMP_REDIRECT:
|
---|
429 | icmpstat.icps_notsupp++;
|
---|
430 | m_freem(pData, m);
|
---|
431 | break;
|
---|
432 |
|
---|
433 | default:
|
---|
434 | icmpstat.icps_badtype++;
|
---|
435 | m_freem(pData, m);
|
---|
436 | } /* switch */
|
---|
437 |
|
---|
438 | end_error:
|
---|
439 | /* m is m_free()'d xor put in a socket xor or given to ip_send */
|
---|
440 | ;
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * Send an ICMP message in response to a situation
|
---|
446 | *
|
---|
447 | * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
|
---|
448 | * MUST NOT change this header information.
|
---|
449 | * MUST NOT reply to a multicast/broadcast IP address.
|
---|
450 | * MUST NOT reply to a multicast/broadcast MAC address.
|
---|
451 | * MUST reply to only the first fragment.
|
---|
452 | */
|
---|
453 | /*
|
---|
454 | * Send ICMP_UNREACH back to the source regarding msrc.
|
---|
455 | * mbuf *msrc is used as a template, but is NOT m_free()'d.
|
---|
456 | * It is reported as the bad ip packet. The header should
|
---|
457 | * be fully correct and in host byte order.
|
---|
458 | * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
|
---|
459 | * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
|
---|
460 | */
|
---|
461 |
|
---|
462 | #define ICMP_MAXDATALEN (IP_MSS-28)
|
---|
463 | void icmp_error(PNATState pData, struct mbuf *msrc, u_char type, u_char code, int minsize, const char *message)
|
---|
464 | {
|
---|
465 | unsigned hlen, shlen, s_ip_len;
|
---|
466 | register struct ip *ip;
|
---|
467 | register struct icmp *icp;
|
---|
468 | register struct mbuf *m;
|
---|
469 |
|
---|
470 | DEBUG_CALL("icmp_error");
|
---|
471 | DEBUG_ARG("msrc = %lx", (long )msrc);
|
---|
472 | DEBUG_ARG("msrc_len = %d", msrc->m_len);
|
---|
473 |
|
---|
474 | if (type!=ICMP_UNREACH && type!=ICMP_TIMXCEED)
|
---|
475 | goto end_error;
|
---|
476 |
|
---|
477 | /* check msrc */
|
---|
478 | if (!msrc)
|
---|
479 | goto end_error;
|
---|
480 |
|
---|
481 | ip = mtod(msrc, struct ip *);
|
---|
482 | #if DEBUG
|
---|
483 | {
|
---|
484 | char bufa[20], bufb[20];
|
---|
485 | strcpy(bufa, inet_ntoa(ip->ip_src));
|
---|
486 | strcpy(bufb, inet_ntoa(ip->ip_dst));
|
---|
487 | DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
|
---|
488 | }
|
---|
489 | #endif
|
---|
490 | if (ip->ip_off & IP_OFFMASK)
|
---|
491 | goto end_error; /* Only reply to fragment 0 */
|
---|
492 |
|
---|
493 | shlen = ip->ip_hl << 2;
|
---|
494 | s_ip_len = ip->ip_len;
|
---|
495 | if (ip->ip_p == IPPROTO_ICMP)
|
---|
496 | {
|
---|
497 | icp = (struct icmp *)((char *)ip + shlen);
|
---|
498 | /*
|
---|
499 | * Assume any unknown ICMP type is an error. This isn't
|
---|
500 | * specified by the RFC, but think about it..
|
---|
501 | */
|
---|
502 | if (icp->icmp_type>18 || icmp_flush[icp->icmp_type])
|
---|
503 | goto end_error;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /* make a copy */
|
---|
507 | if (!(m = m_get(pData)))
|
---|
508 | goto end_error; /* get mbuf */
|
---|
509 | {
|
---|
510 | int new_m_size;
|
---|
511 | new_m_size = sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
|
---|
512 | if (new_m_size>m->m_size)
|
---|
513 | m_inc(m, new_m_size);
|
---|
514 | }
|
---|
515 | memcpy(m->m_data, msrc->m_data, msrc->m_len);
|
---|
516 | m->m_len = msrc->m_len; /* copy msrc to m */
|
---|
517 |
|
---|
518 | /* make the header of the reply packet */
|
---|
519 | ip = mtod(m, struct ip *);
|
---|
520 | hlen = sizeof(struct ip ); /* no options in reply */
|
---|
521 |
|
---|
522 | /* fill in icmp */
|
---|
523 | m->m_data += hlen;
|
---|
524 | m->m_len -= hlen;
|
---|
525 |
|
---|
526 | icp = mtod(m, struct icmp *);
|
---|
527 |
|
---|
528 | if (minsize)
|
---|
529 | s_ip_len = shlen+ICMP_MINLEN; /* return header+8b only */
|
---|
530 | else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
|
---|
531 | s_ip_len = ICMP_MAXDATALEN;
|
---|
532 |
|
---|
533 | m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
|
---|
534 |
|
---|
535 | /* min. size = 8+sizeof(struct ip)+8 */
|
---|
536 |
|
---|
537 | icp->icmp_type = type;
|
---|
538 | icp->icmp_code = code;
|
---|
539 | icp->icmp_id = 0;
|
---|
540 | icp->icmp_seq = 0;
|
---|
541 |
|
---|
542 | memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
|
---|
543 | HTONS(icp->icmp_ip.ip_len);
|
---|
544 | HTONS(icp->icmp_ip.ip_id);
|
---|
545 | HTONS(icp->icmp_ip.ip_off);
|
---|
546 |
|
---|
547 | #if DEBUG
|
---|
548 | if (message)
|
---|
549 | {
|
---|
550 | /* DEBUG : append message to ICMP packet */
|
---|
551 | int message_len;
|
---|
552 | char *cpnt;
|
---|
553 | message_len = strlen(message);
|
---|
554 | if (message_len > ICMP_MAXDATALEN)
|
---|
555 | 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 | (void ) ip_output(pData, (struct socket *)NULL, m);
|
---|
614 |
|
---|
615 | icmpstat.icps_reflect++;
|
---|
616 | }
|
---|