1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 | #include "ip_icmp.h"
|
---|
11 | #include "main.h"
|
---|
12 | #ifdef __sun__
|
---|
13 | #include <sys/filio.h>
|
---|
14 | #endif
|
---|
15 | #include <VBox/pdmdrv.h>
|
---|
16 | #if defined (RT_OS_WINDOWS)
|
---|
17 | #include <iphlpapi.h>
|
---|
18 | #include <icmpapi.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 |
|
---|
22 | static void send_icmp_to_guest(PNATState, char *, size_t, struct socket *, const struct sockaddr_in *);
|
---|
23 | #ifdef RT_OS_WINDOWS
|
---|
24 | static void sorecvfrom_icmp_win(PNATState, struct socket *);
|
---|
25 | #else /* RT_OS_WINDOWS */
|
---|
26 | static void sorecvfrom_icmp_unix(PNATState, struct socket *);
|
---|
27 | #endif /* !RT_OS_WINDOWS */
|
---|
28 |
|
---|
29 | void
|
---|
30 | so_init()
|
---|
31 | {
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | struct socket *
|
---|
36 | solookup(struct socket *head, struct in_addr laddr,
|
---|
37 | u_int lport, struct in_addr faddr, u_int fport)
|
---|
38 | {
|
---|
39 | struct socket *so;
|
---|
40 |
|
---|
41 | for (so = head->so_next; so != head; so = so->so_next)
|
---|
42 | {
|
---|
43 | if ( so->so_lport == lport
|
---|
44 | && so->so_laddr.s_addr == laddr.s_addr
|
---|
45 | && so->so_faddr.s_addr == faddr.s_addr
|
---|
46 | && so->so_fport == fport)
|
---|
47 | return so;
|
---|
48 | }
|
---|
49 |
|
---|
50 | return (struct socket *)NULL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Create a new socket, initialise the fields
|
---|
55 | * It is the responsibility of the caller to
|
---|
56 | * insque() it into the correct linked-list
|
---|
57 | */
|
---|
58 | struct socket *
|
---|
59 | socreate()
|
---|
60 | {
|
---|
61 | struct socket *so;
|
---|
62 |
|
---|
63 | so = (struct socket *)RTMemAllocZ(sizeof(struct socket));
|
---|
64 | if(so)
|
---|
65 | {
|
---|
66 | so->so_state = SS_NOFDREF;
|
---|
67 | so->s = -1;
|
---|
68 | #if !defined(RT_OS_WINDOWS)
|
---|
69 | so->so_poll_index = -1;
|
---|
70 | #endif
|
---|
71 | }
|
---|
72 | return so;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * remque and free a socket, clobber cache
|
---|
77 | * VBOX_WITH_SLIRP_MT: before sofree queue should be locked, because
|
---|
78 | * in sofree we don't know from which queue item beeing removed.
|
---|
79 | */
|
---|
80 | void
|
---|
81 | sofree(PNATState pData, struct socket *so)
|
---|
82 | {
|
---|
83 | struct socket *so_prev = NULL;
|
---|
84 | if (so == tcp_last_so)
|
---|
85 | tcp_last_so = &tcb;
|
---|
86 | else if (so == udp_last_so)
|
---|
87 | udp_last_so = &udb;
|
---|
88 |
|
---|
89 | /* check if mbuf haven't been already freed */
|
---|
90 | if (so->so_m != NULL)
|
---|
91 | m_free(pData, so->so_m);
|
---|
92 | #ifndef VBOX_WITH_SLIRP_MT
|
---|
93 | if(so->so_next && so->so_prev)
|
---|
94 | {
|
---|
95 | remque(pData, so); /* crashes if so is not in a queue */
|
---|
96 | NSOCK_DEC();
|
---|
97 | }
|
---|
98 |
|
---|
99 | RTMemFree(so);
|
---|
100 | #else
|
---|
101 | so->so_deleted = 1;
|
---|
102 | #endif
|
---|
103 | }
|
---|
104 |
|
---|
105 | #ifdef VBOX_WITH_SLIRP_MT
|
---|
106 | void
|
---|
107 | soread_queue(PNATState pData, struct socket *so, int *ret)
|
---|
108 | {
|
---|
109 | *ret = soread(pData, so);
|
---|
110 | }
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Read from so's socket into sb_snd, updating all relevant sbuf fields
|
---|
115 | * NOTE: This will only be called if it is select()ed for reading, so
|
---|
116 | * a read() of 0 (or less) means it's disconnected
|
---|
117 | */
|
---|
118 | int
|
---|
119 | soread(PNATState pData, struct socket *so)
|
---|
120 | {
|
---|
121 | int n, nn, lss, total;
|
---|
122 | struct sbuf *sb = &so->so_snd;
|
---|
123 | size_t len = sb->sb_datalen - sb->sb_cc;
|
---|
124 | struct iovec iov[2];
|
---|
125 | int mss = so->so_tcpcb->t_maxseg;
|
---|
126 |
|
---|
127 | STAM_PROFILE_START(&pData->StatIOread, a);
|
---|
128 | STAM_COUNTER_RESET(&pData->StatIORead_in_1);
|
---|
129 | STAM_COUNTER_RESET(&pData->StatIORead_in_2);
|
---|
130 |
|
---|
131 | QSOCKET_LOCK(tcb);
|
---|
132 | SOCKET_LOCK(so);
|
---|
133 | QSOCKET_UNLOCK(tcb);
|
---|
134 |
|
---|
135 | DEBUG_CALL("soread");
|
---|
136 | DEBUG_ARG("so = %lx", (long )so);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * No need to check if there's enough room to read.
|
---|
140 | * soread wouldn't have been called if there weren't
|
---|
141 | */
|
---|
142 |
|
---|
143 | len = sb->sb_datalen - sb->sb_cc;
|
---|
144 |
|
---|
145 | iov[0].iov_base = sb->sb_wptr;
|
---|
146 | iov[1].iov_base = 0;
|
---|
147 | iov[1].iov_len = 0;
|
---|
148 | if (sb->sb_wptr < sb->sb_rptr)
|
---|
149 | {
|
---|
150 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
|
---|
151 | /* Should never succeed, but... */
|
---|
152 | if (iov[0].iov_len > len)
|
---|
153 | iov[0].iov_len = len;
|
---|
154 | if (iov[0].iov_len > mss)
|
---|
155 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
156 | n = 1;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
|
---|
161 | /* Should never succeed, but... */
|
---|
162 | if (iov[0].iov_len > len)
|
---|
163 | iov[0].iov_len = len;
|
---|
164 | len -= iov[0].iov_len;
|
---|
165 | if (len)
|
---|
166 | {
|
---|
167 | iov[1].iov_base = sb->sb_data;
|
---|
168 | iov[1].iov_len = sb->sb_rptr - sb->sb_data;
|
---|
169 | if(iov[1].iov_len > len)
|
---|
170 | iov[1].iov_len = len;
|
---|
171 | total = iov[0].iov_len + iov[1].iov_len;
|
---|
172 | if (total > mss)
|
---|
173 | {
|
---|
174 | lss = total % mss;
|
---|
175 | if (iov[1].iov_len > lss)
|
---|
176 | {
|
---|
177 | iov[1].iov_len -= lss;
|
---|
178 | n = 2;
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | lss -= iov[1].iov_len;
|
---|
183 | iov[0].iov_len -= lss;
|
---|
184 | n = 1;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else
|
---|
188 | n = 2;
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | if (iov[0].iov_len > mss)
|
---|
193 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
194 | n = 1;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | #ifdef HAVE_READV
|
---|
199 | nn = readv(so->s, (struct iovec *)iov, n);
|
---|
200 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
201 | #else
|
---|
202 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len, 0);
|
---|
203 | #endif
|
---|
204 | if (nn <= 0)
|
---|
205 | {
|
---|
206 | #if defined(RT_OS_WINDOWS)
|
---|
207 | /*
|
---|
208 | * Special case for WSAEnumNetworkEvents: If we receive 0 bytes that
|
---|
209 | * _could_ mean that the connection is closed. But we will receive an
|
---|
210 | * FD_CLOSE event later if the connection was _really_ closed. With
|
---|
211 | * www.youtube.com I see this very often. Closing the socket too early
|
---|
212 | * would be dangerous.
|
---|
213 | */
|
---|
214 | int status, ignored;
|
---|
215 | unsigned long pending = 0;
|
---|
216 | status = WSAIoctl(so->s, FIONREAD, NULL, 0, &pending, sizeof(unsigned long), &ignored, NULL, NULL);
|
---|
217 | if (status < 0)
|
---|
218 | LogRel(("NAT:error in WSAIoctl: %d\n", WSAGetLastError()));
|
---|
219 | if (nn == 0 && (pending != 0))
|
---|
220 | {
|
---|
221 | SOCKET_UNLOCK(so);
|
---|
222 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
223 | return 0;
|
---|
224 | }
|
---|
225 | #endif
|
---|
226 | if (nn < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
|
---|
227 | {
|
---|
228 | SOCKET_UNLOCK(so);
|
---|
229 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
230 | return 0;
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | /* nn == 0 means peer has performed an orderly shutdown */
|
---|
235 | DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n",
|
---|
236 | nn, errno, strerror(errno)));
|
---|
237 | sofcantrcvmore(so);
|
---|
238 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
239 | SOCKET_UNLOCK(so);
|
---|
240 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
241 | return -1;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | STAM_STATS(
|
---|
245 | if (n == 1)
|
---|
246 | {
|
---|
247 | STAM_COUNTER_INC(&pData->StatIORead_in_1);
|
---|
248 | STAM_COUNTER_ADD(&pData->StatIORead_in_1_bytes, nn);
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | STAM_COUNTER_INC(&pData->StatIORead_in_2);
|
---|
253 | STAM_COUNTER_ADD(&pData->StatIORead_in_2_1st_bytes, nn);
|
---|
254 | }
|
---|
255 | );
|
---|
256 |
|
---|
257 | #ifndef HAVE_READV
|
---|
258 | /*
|
---|
259 | * If there was no error, try and read the second time round
|
---|
260 | * We read again if n = 2 (ie, there's another part of the buffer)
|
---|
261 | * and we read as much as we could in the first read
|
---|
262 | * We don't test for <= 0 this time, because there legitimately
|
---|
263 | * might not be any more data (since the socket is non-blocking),
|
---|
264 | * a close will be detected on next iteration.
|
---|
265 | * A return of -1 wont (shouldn't) happen, since it didn't happen above
|
---|
266 | */
|
---|
267 | if (n == 2 && nn == iov[0].iov_len)
|
---|
268 | {
|
---|
269 | int ret;
|
---|
270 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len, 0);
|
---|
271 | if (ret > 0)
|
---|
272 | nn += ret;
|
---|
273 | STAM_STATS(
|
---|
274 | if(ret > 0)
|
---|
275 | {
|
---|
276 | STAM_COUNTER_INC(&pData->StatIORead_in_2);
|
---|
277 | STAM_COUNTER_ADD(&pData->StatIORead_in_2_2nd_bytes, ret);
|
---|
278 | }
|
---|
279 | );
|
---|
280 | }
|
---|
281 |
|
---|
282 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | /* Update fields */
|
---|
286 | sb->sb_cc += nn;
|
---|
287 | sb->sb_wptr += nn;
|
---|
288 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
|
---|
289 | sb->sb_wptr -= sb->sb_datalen;
|
---|
290 | STAM_PROFILE_STOP(&pData->StatIOread, a);
|
---|
291 | SOCKET_UNLOCK(so);
|
---|
292 | return nn;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Get urgent data
|
---|
297 | *
|
---|
298 | * When the socket is created, we set it SO_OOBINLINE,
|
---|
299 | * so when OOB data arrives, we soread() it and everything
|
---|
300 | * in the send buffer is sent as urgent data
|
---|
301 | */
|
---|
302 | void
|
---|
303 | sorecvoob(PNATState pData, struct socket *so)
|
---|
304 | {
|
---|
305 | struct tcpcb *tp = sototcpcb(so);
|
---|
306 |
|
---|
307 | DEBUG_CALL("sorecvoob");
|
---|
308 | DEBUG_ARG("so = %lx", (long)so);
|
---|
309 |
|
---|
310 | /*
|
---|
311 | * We take a guess at how much urgent data has arrived.
|
---|
312 | * In most situations, when urgent data arrives, the next
|
---|
313 | * read() should get all the urgent data. This guess will
|
---|
314 | * be wrong however if more data arrives just after the
|
---|
315 | * urgent data, or the read() doesn't return all the
|
---|
316 | * urgent data.
|
---|
317 | */
|
---|
318 | soread(pData, so);
|
---|
319 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
|
---|
320 | tp->t_force = 1;
|
---|
321 | tcp_output(pData, tp);
|
---|
322 | tp->t_force = 0;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /*
|
---|
326 | * Send urgent data
|
---|
327 | * There's a lot duplicated code here, but...
|
---|
328 | */
|
---|
329 | int
|
---|
330 | sosendoob(struct socket *so)
|
---|
331 | {
|
---|
332 | struct sbuf *sb = &so->so_rcv;
|
---|
333 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
|
---|
334 |
|
---|
335 | int n, len;
|
---|
336 |
|
---|
337 | DEBUG_CALL("sosendoob");
|
---|
338 | DEBUG_ARG("so = %lx", (long)so);
|
---|
339 | DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
|
---|
340 |
|
---|
341 | if (so->so_urgc > sizeof(buff))
|
---|
342 | so->so_urgc = sizeof(buff); /* XXX */
|
---|
343 |
|
---|
344 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
345 | {
|
---|
346 | /* We can send it directly */
|
---|
347 | n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
348 | so->so_urgc -= n;
|
---|
349 |
|
---|
350 | DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
351 | n, so->so_urgc));
|
---|
352 | }
|
---|
353 | else
|
---|
354 | {
|
---|
355 | /*
|
---|
356 | * Since there's no sendv or sendtov like writev,
|
---|
357 | * we must copy all data to a linear buffer then
|
---|
358 | * send it all
|
---|
359 | */
|
---|
360 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
361 | if (len > so->so_urgc)
|
---|
362 | len = so->so_urgc;
|
---|
363 | memcpy(buff, sb->sb_rptr, len);
|
---|
364 | so->so_urgc -= len;
|
---|
365 | if (so->so_urgc)
|
---|
366 | {
|
---|
367 | n = sb->sb_wptr - sb->sb_data;
|
---|
368 | if (n > so->so_urgc)
|
---|
369 | n = so->so_urgc;
|
---|
370 | memcpy(buff + len, sb->sb_data, n);
|
---|
371 | so->so_urgc -= n;
|
---|
372 | len += n;
|
---|
373 | }
|
---|
374 | n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
375 | #ifdef DEBUG
|
---|
376 | if (n != len)
|
---|
377 | DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
|
---|
378 | #endif
|
---|
379 | DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n",
|
---|
380 | n, so->so_urgc));
|
---|
381 | }
|
---|
382 |
|
---|
383 | sb->sb_cc -= n;
|
---|
384 | sb->sb_rptr += n;
|
---|
385 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
386 | sb->sb_rptr -= sb->sb_datalen;
|
---|
387 |
|
---|
388 | return n;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * Write data from so_rcv to so's socket,
|
---|
393 | * updating all sbuf field as necessary
|
---|
394 | */
|
---|
395 | int
|
---|
396 | sowrite(PNATState pData, struct socket *so)
|
---|
397 | {
|
---|
398 | int n, nn;
|
---|
399 | struct sbuf *sb = &so->so_rcv;
|
---|
400 | size_t len = sb->sb_cc;
|
---|
401 | struct iovec iov[2];
|
---|
402 |
|
---|
403 | STAM_PROFILE_START(&pData->StatIOwrite, a);
|
---|
404 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_1);
|
---|
405 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_1_bytes);
|
---|
406 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2);
|
---|
407 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_1st_bytes);
|
---|
408 | STAM_COUNTER_RESET(&pData->StatIOWrite_in_2_2nd_bytes);
|
---|
409 | STAM_COUNTER_RESET(&pData->StatIOWrite_no_w);
|
---|
410 | STAM_COUNTER_RESET(&pData->StatIOWrite_rest);
|
---|
411 | STAM_COUNTER_RESET(&pData->StatIOWrite_rest_bytes);
|
---|
412 | DEBUG_CALL("sowrite");
|
---|
413 | DEBUG_ARG("so = %lx", (long)so);
|
---|
414 | QSOCKET_LOCK(tcb);
|
---|
415 | SOCKET_LOCK(so);
|
---|
416 | QSOCKET_UNLOCK(tcb);
|
---|
417 | if (so->so_urgc)
|
---|
418 | {
|
---|
419 | sosendoob(so);
|
---|
420 | if (sb->sb_cc == 0)
|
---|
421 | {
|
---|
422 | SOCKET_UNLOCK(so);
|
---|
423 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
424 | return 0;
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*
|
---|
429 | * No need to check if there's something to write,
|
---|
430 | * sowrite wouldn't have been called otherwise
|
---|
431 | */
|
---|
432 |
|
---|
433 | len = sb->sb_cc;
|
---|
434 |
|
---|
435 | iov[0].iov_base = sb->sb_rptr;
|
---|
436 | iov[1].iov_base = 0;
|
---|
437 | iov[1].iov_len = 0;
|
---|
438 | if (sb->sb_rptr < sb->sb_wptr)
|
---|
439 | {
|
---|
440 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
|
---|
441 | /* Should never succeed, but... */
|
---|
442 | if (iov[0].iov_len > len)
|
---|
443 | iov[0].iov_len = len;
|
---|
444 | n = 1;
|
---|
445 | }
|
---|
446 | else
|
---|
447 | {
|
---|
448 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
449 | if (iov[0].iov_len > len)
|
---|
450 | iov[0].iov_len = len;
|
---|
451 | len -= iov[0].iov_len;
|
---|
452 | if (len)
|
---|
453 | {
|
---|
454 | iov[1].iov_base = sb->sb_data;
|
---|
455 | iov[1].iov_len = sb->sb_wptr - sb->sb_data;
|
---|
456 | if (iov[1].iov_len > len)
|
---|
457 | iov[1].iov_len = len;
|
---|
458 | n = 2;
|
---|
459 | }
|
---|
460 | else
|
---|
461 | n = 1;
|
---|
462 | }
|
---|
463 | STAM_STATS({
|
---|
464 | if (n == 1)
|
---|
465 | {
|
---|
466 | STAM_COUNTER_INC(&pData->StatIOWrite_in_1);
|
---|
467 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_1_bytes, iov[0].iov_len);
|
---|
468 | }
|
---|
469 | else
|
---|
470 | {
|
---|
471 | STAM_COUNTER_INC(&pData->StatIOWrite_in_2);
|
---|
472 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_1st_bytes, iov[0].iov_len);
|
---|
473 | STAM_COUNTER_ADD(&pData->StatIOWrite_in_2_2nd_bytes, iov[1].iov_len);
|
---|
474 | }
|
---|
475 | });
|
---|
476 | /* Check if there's urgent data to send, and if so, send it */
|
---|
477 | #ifdef HAVE_READV
|
---|
478 | nn = writev(so->s, (const struct iovec *)iov, n);
|
---|
479 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
480 | #else
|
---|
481 | nn = send(so->s, iov[0].iov_base, iov[0].iov_len, 0);
|
---|
482 | #endif
|
---|
483 | /* This should never happen, but people tell me it does *shrug* */
|
---|
484 | if (nn < 0 && (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
|
---|
485 | {
|
---|
486 | SOCKET_UNLOCK(so);
|
---|
487 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
488 | return 0;
|
---|
489 | }
|
---|
490 |
|
---|
491 | if (nn < 0 || (nn == 0 && iov[0].iov_len > 0))
|
---|
492 | {
|
---|
493 | DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
|
---|
494 | so->so_state, errno));
|
---|
495 | sofcantsendmore(so);
|
---|
496 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
497 | SOCKET_UNLOCK(so);
|
---|
498 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
499 | return -1;
|
---|
500 | }
|
---|
501 |
|
---|
502 | #ifndef HAVE_READV
|
---|
503 | if (n == 2 && nn == iov[0].iov_len)
|
---|
504 | {
|
---|
505 | int ret;
|
---|
506 | ret = send(so->s, iov[1].iov_base, iov[1].iov_len, 0);
|
---|
507 | if (ret > 0)
|
---|
508 | nn += ret;
|
---|
509 | STAM_STATS({
|
---|
510 | if (ret > 0 && ret != iov[1].iov_len)
|
---|
511 | {
|
---|
512 | STAM_COUNTER_INC(&pData->StatIOWrite_rest);
|
---|
513 | STAM_COUNTER_ADD(&pData->StatIOWrite_rest_bytes, (ret - iov[1].iov_len));
|
---|
514 | }
|
---|
515 | });
|
---|
516 | }
|
---|
517 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
518 | #endif
|
---|
519 |
|
---|
520 | /* Update sbuf */
|
---|
521 | sb->sb_cc -= nn;
|
---|
522 | sb->sb_rptr += nn;
|
---|
523 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
524 | sb->sb_rptr -= sb->sb_datalen;
|
---|
525 |
|
---|
526 | /*
|
---|
527 | * If in DRAIN mode, and there's no more data, set
|
---|
528 | * it CANTSENDMORE
|
---|
529 | */
|
---|
530 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
|
---|
531 | sofcantsendmore(so);
|
---|
532 |
|
---|
533 | SOCKET_UNLOCK(so);
|
---|
534 | STAM_PROFILE_STOP(&pData->StatIOwrite, a);
|
---|
535 | return nn;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * recvfrom() a UDP socket
|
---|
540 | */
|
---|
541 | void
|
---|
542 | sorecvfrom(PNATState pData, struct socket *so)
|
---|
543 | {
|
---|
544 | struct sockaddr_in addr;
|
---|
545 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
546 |
|
---|
547 | DEBUG_CALL("sorecvfrom");
|
---|
548 | DEBUG_ARG("so = %lx", (long)so);
|
---|
549 |
|
---|
550 | if (so->so_type == IPPROTO_ICMP)
|
---|
551 | {
|
---|
552 | /* This is a "ping" reply */
|
---|
553 | #ifdef RT_OS_WINDOWS
|
---|
554 | sorecvfrom_icmp_win(pData, so);
|
---|
555 | #else /* RT_OS_WINDOWS */
|
---|
556 | sorecvfrom_icmp_unix(pData, so);
|
---|
557 | #endif /* !RT_OS_WINDOWS */
|
---|
558 | udp_detach(pData, so);
|
---|
559 | }
|
---|
560 | else
|
---|
561 | {
|
---|
562 | /* A "normal" UDP packet */
|
---|
563 | struct mbuf *m;
|
---|
564 | struct ethhdr *eh;
|
---|
565 | size_t len;
|
---|
566 | u_long n;
|
---|
567 |
|
---|
568 | QSOCKET_LOCK(udb);
|
---|
569 | SOCKET_LOCK(so);
|
---|
570 | QSOCKET_UNLOCK(udb);
|
---|
571 |
|
---|
572 | if (!(m = m_get(pData)))
|
---|
573 | {
|
---|
574 | SOCKET_UNLOCK(so);
|
---|
575 | return;
|
---|
576 | }
|
---|
577 | /* adjust both parameters to maks M_FREEROOM calculate correct */
|
---|
578 | m_adj(m, if_maxlinkhdr + sizeof(struct udphdr) + sizeof(struct ip));
|
---|
579 |
|
---|
580 | /*
|
---|
581 | * XXX Shouldn't FIONREAD packets destined for port 53,
|
---|
582 | * but I don't know the max packet size for DNS lookups
|
---|
583 | */
|
---|
584 | len = M_FREEROOM(m);
|
---|
585 | /* if (so->so_fport != htons(53)) */
|
---|
586 | {
|
---|
587 | ioctlsocket(so->s, FIONREAD, &n);
|
---|
588 |
|
---|
589 | if (n > len)
|
---|
590 | {
|
---|
591 | n = (m->m_data - m->m_dat) + m->m_len + n + 1;
|
---|
592 | m_inc(m, n);
|
---|
593 | len = M_FREEROOM(m);
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 | m->m_len = recvfrom(so->s, m->m_data, len, 0,
|
---|
598 | (struct sockaddr *)&addr, &addrlen);
|
---|
599 | Log2((" did recvfrom %d, errno = %d-%s\n",
|
---|
600 | m->m_len, errno, strerror(errno)));
|
---|
601 | if(m->m_len < 0)
|
---|
602 | {
|
---|
603 | u_char code = ICMP_UNREACH_PORT;
|
---|
604 |
|
---|
605 | if (errno == EHOSTUNREACH)
|
---|
606 | code = ICMP_UNREACH_HOST;
|
---|
607 | else if(errno == ENETUNREACH)
|
---|
608 | code = ICMP_UNREACH_NET;
|
---|
609 |
|
---|
610 | Log2((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
611 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
612 | so->so_m = NULL;
|
---|
613 | m_free(pData, m);
|
---|
614 | }
|
---|
615 | else
|
---|
616 | {
|
---|
617 | /*
|
---|
618 | * Hack: domain name lookup will be used the most for UDP,
|
---|
619 | * and since they'll only be used once there's no need
|
---|
620 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
621 | * out much quicker (10 seconds for now...)
|
---|
622 | */
|
---|
623 | if (so->so_expire)
|
---|
624 | {
|
---|
625 | if (so->so_fport != htons(53))
|
---|
626 | so->so_expire = curtime + SO_EXPIRE;
|
---|
627 | }
|
---|
628 | /*
|
---|
629 | * last argument should be changed if Slirp will inject IP attributes
|
---|
630 | * Note: Here we can't check if dnsproxy's sent initial request
|
---|
631 | */
|
---|
632 | if (so->so_fport == htons(53))
|
---|
633 | dnsproxy_answer(pData, so, m);
|
---|
634 |
|
---|
635 | #if 0
|
---|
636 | if (m->m_len == len)
|
---|
637 | {
|
---|
638 | m_inc(m, MINCSIZE);
|
---|
639 | m->m_len = 0;
|
---|
640 | }
|
---|
641 | #endif
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * If this packet was destined for CTL_ADDR,
|
---|
645 | * make it look like that's where it came from, done by udp_output
|
---|
646 | */
|
---|
647 | udp_output(pData, so, m, &addr);
|
---|
648 | SOCKET_UNLOCK(so);
|
---|
649 | } /* rx error */
|
---|
650 | } /* if ping packet */
|
---|
651 | }
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * sendto() a socket
|
---|
655 | */
|
---|
656 | int
|
---|
657 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
658 | {
|
---|
659 | int ret;
|
---|
660 | struct sockaddr_in addr;
|
---|
661 | #if 0
|
---|
662 | struct sockaddr_in host_addr;
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | DEBUG_CALL("sosendto");
|
---|
666 | DEBUG_ARG("so = %lx", (long)so);
|
---|
667 | DEBUG_ARG("m = %lx", (long)m);
|
---|
668 |
|
---|
669 | addr.sin_family = AF_INET;
|
---|
670 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
671 | {
|
---|
672 | /* It's an alias */
|
---|
673 | uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
674 | switch(last_byte)
|
---|
675 | {
|
---|
676 | #if 0
|
---|
677 | /* handle this case at 'default:' */
|
---|
678 | case CTL_BROADCAST:
|
---|
679 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
680 | /* Send the packet to host to fully emulate broadcast */
|
---|
681 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
682 | * the packet twice for some reason. And I cannot find any place
|
---|
683 | * in the man pages which states that sending a broadcast does not
|
---|
684 | * reach the host itself. */
|
---|
685 | host_addr.sin_family = AF_INET;
|
---|
686 | host_addr.sin_port = so->so_fport;
|
---|
687 | host_addr.sin_addr = our_addr;
|
---|
688 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
689 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
690 | break;
|
---|
691 | #endif
|
---|
692 | case CTL_DNS:
|
---|
693 | case CTL_ALIAS:
|
---|
694 | default:
|
---|
695 | if (last_byte == ~pData->netmask)
|
---|
696 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
697 | else
|
---|
698 | addr.sin_addr = loopback_addr;
|
---|
699 | break;
|
---|
700 | }
|
---|
701 | }
|
---|
702 | else
|
---|
703 | addr.sin_addr = so->so_faddr;
|
---|
704 | addr.sin_port = so->so_fport;
|
---|
705 |
|
---|
706 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
|
---|
707 | ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
708 |
|
---|
709 | /* Don't care what port we get */
|
---|
710 | ret = sendto(so->s, m->m_data, m->m_len, 0,
|
---|
711 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
712 | if (ret < 0)
|
---|
713 | {
|
---|
714 | LogRel(("UDP: sendto fails (%s)\n", strerror(errno)));
|
---|
715 | return -1;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * Kill the socket if there's no reply in 4 minutes,
|
---|
720 | * but only if it's an expirable socket
|
---|
721 | */
|
---|
722 | if (so->so_expire)
|
---|
723 | so->so_expire = curtime + SO_EXPIRE;
|
---|
724 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
725 | return 0;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /*
|
---|
729 | * XXX This should really be tcp_listen
|
---|
730 | */
|
---|
731 | struct socket *
|
---|
732 | solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
733 | {
|
---|
734 | struct sockaddr_in addr;
|
---|
735 | struct socket *so;
|
---|
736 | socklen_t addrlen = sizeof(addr);
|
---|
737 | int s, opt = 1;
|
---|
738 | int status;
|
---|
739 |
|
---|
740 | DEBUG_CALL("solisten");
|
---|
741 | DEBUG_ARG("port = %d", port);
|
---|
742 | DEBUG_ARG("laddr = %x", laddr);
|
---|
743 | DEBUG_ARG("lport = %d", lport);
|
---|
744 | DEBUG_ARG("flags = %x", flags);
|
---|
745 |
|
---|
746 | if ((so = socreate()) == NULL)
|
---|
747 | {
|
---|
748 | /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
|
---|
749 | return NULL;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
753 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
754 | {
|
---|
755 | RTMemFree(so);
|
---|
756 | return NULL;
|
---|
757 | }
|
---|
758 |
|
---|
759 | SOCKET_LOCK_CREATE(so);
|
---|
760 | SOCKET_LOCK(so);
|
---|
761 | QSOCKET_LOCK(tcb);
|
---|
762 | insque(pData, so,&tcb);
|
---|
763 | NSOCK_INC();
|
---|
764 | QSOCKET_UNLOCK(tcb);
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * SS_FACCEPTONCE sockets must time out.
|
---|
768 | */
|
---|
769 | if (flags & SS_FACCEPTONCE)
|
---|
770 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
771 |
|
---|
772 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
773 | so->so_lport = lport; /* Kept in network format */
|
---|
774 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
775 |
|
---|
776 | addr.sin_family = AF_INET;
|
---|
777 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
778 | addr.sin_port = port;
|
---|
779 |
|
---|
780 | if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
---|
781 | || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
|
---|
782 | || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
783 | || (listen(s, 1) < 0))
|
---|
784 | {
|
---|
785 | #ifdef RT_OS_WINDOWS
|
---|
786 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
787 | closesocket(s);
|
---|
788 | QSOCKET_LOCK(tcb);
|
---|
789 | sofree(pData, so);
|
---|
790 | QSOCKET_UNLOCK(tcb);
|
---|
791 | /* Restore the real errno */
|
---|
792 | WSASetLastError(tmperrno);
|
---|
793 | #else
|
---|
794 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
795 | close(s);
|
---|
796 | QSOCKET_LOCK(tcb);
|
---|
797 | sofree(pData, so);
|
---|
798 | QSOCKET_UNLOCK(tcb);
|
---|
799 | /* Restore the real errno */
|
---|
800 | errno = tmperrno;
|
---|
801 | #endif
|
---|
802 | return NULL;
|
---|
803 | }
|
---|
804 | fd_nonblock(s);
|
---|
805 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
|
---|
806 |
|
---|
807 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
808 | so->so_fport = addr.sin_port;
|
---|
809 | /* set socket buffers */
|
---|
810 | opt = pData->socket_rcv;
|
---|
811 | status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
|
---|
812 | if (status < 0)
|
---|
813 | {
|
---|
814 | LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
|
---|
815 | goto no_sockopt;
|
---|
816 | }
|
---|
817 | opt = pData->socket_snd;
|
---|
818 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
|
---|
819 | if (status < 0)
|
---|
820 | {
|
---|
821 | LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
|
---|
822 | goto no_sockopt;
|
---|
823 | }
|
---|
824 | no_sockopt:
|
---|
825 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
826 | so->so_faddr = alias_addr;
|
---|
827 | else
|
---|
828 | so->so_faddr = addr.sin_addr;
|
---|
829 |
|
---|
830 | so->s = s;
|
---|
831 | SOCKET_UNLOCK(so);
|
---|
832 | return so;
|
---|
833 | }
|
---|
834 |
|
---|
835 | /*
|
---|
836 | * Data is available in so_rcv
|
---|
837 | * Just write() the data to the socket
|
---|
838 | * XXX not yet...
|
---|
839 | */
|
---|
840 | void
|
---|
841 | sorwakeup(struct socket *so)
|
---|
842 | {
|
---|
843 | #if 0
|
---|
844 | sowrite(so);
|
---|
845 | FD_CLR(so->s,&writefds);
|
---|
846 | #endif
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*
|
---|
850 | * Data has been freed in so_snd
|
---|
851 | * We have room for a read() if we want to
|
---|
852 | * For now, don't read, it'll be done in the main loop
|
---|
853 | */
|
---|
854 | void
|
---|
855 | sowwakeup(struct socket *so)
|
---|
856 | {
|
---|
857 | }
|
---|
858 |
|
---|
859 | /*
|
---|
860 | * Various session state calls
|
---|
861 | * XXX Should be #define's
|
---|
862 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
863 | * times each when only 1 was needed
|
---|
864 | */
|
---|
865 | void
|
---|
866 | soisfconnecting(struct socket *so)
|
---|
867 | {
|
---|
868 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
869 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
870 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
871 | }
|
---|
872 |
|
---|
873 | void
|
---|
874 | soisfconnected(struct socket *so)
|
---|
875 | {
|
---|
876 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
877 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
878 | }
|
---|
879 |
|
---|
880 | void
|
---|
881 | sofcantrcvmore(struct socket *so)
|
---|
882 | {
|
---|
883 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
884 | {
|
---|
885 | shutdown(so->s, 0);
|
---|
886 | }
|
---|
887 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
888 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
889 | so->so_state = SS_NOFDREF; /* Don't select it */
|
---|
890 | /* XXX close() here as well? */
|
---|
891 | else
|
---|
892 | so->so_state |= SS_FCANTRCVMORE;
|
---|
893 | }
|
---|
894 |
|
---|
895 | void
|
---|
896 | sofcantsendmore(struct socket *so)
|
---|
897 | {
|
---|
898 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
899 | shutdown(so->s, 1); /* send FIN to fhost */
|
---|
900 |
|
---|
901 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
902 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
903 | so->so_state = SS_NOFDREF; /* as above */
|
---|
904 | else
|
---|
905 | so->so_state |= SS_FCANTSENDMORE;
|
---|
906 | }
|
---|
907 |
|
---|
908 | void
|
---|
909 | soisfdisconnected(struct socket *so)
|
---|
910 | {
|
---|
911 | #if 0
|
---|
912 | so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
|
---|
913 | close(so->s);
|
---|
914 | so->so_state = SS_ISFDISCONNECTED;
|
---|
915 | /*
|
---|
916 | * XXX Do nothing ... ?
|
---|
917 | */
|
---|
918 | #endif
|
---|
919 | }
|
---|
920 |
|
---|
921 | /*
|
---|
922 | * Set write drain mode
|
---|
923 | * Set CANTSENDMORE once all data has been write()n
|
---|
924 | */
|
---|
925 | void
|
---|
926 | sofwdrain(struct socket *so)
|
---|
927 | {
|
---|
928 | if (so->so_rcv.sb_cc)
|
---|
929 | so->so_state |= SS_FWDRAIN;
|
---|
930 | else
|
---|
931 | sofcantsendmore(so);
|
---|
932 | }
|
---|
933 |
|
---|
934 | static void
|
---|
935 | send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
|
---|
936 | {
|
---|
937 | struct ip *ip;
|
---|
938 | uint32_t dst, src;
|
---|
939 | char ip_copy[256];
|
---|
940 | struct icmp *icp;
|
---|
941 | int old_ip_len = 0;
|
---|
942 | int hlen, original_hlen = 0;
|
---|
943 | struct mbuf *m;
|
---|
944 | struct icmp_msg *icm;
|
---|
945 | uint8_t proto;
|
---|
946 | int type = 0;
|
---|
947 |
|
---|
948 | ip = (struct ip *)buff;
|
---|
949 | hlen = (ip->ip_hl << 2);
|
---|
950 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
951 |
|
---|
952 | Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
|
---|
953 | if ( icp->icmp_type != ICMP_ECHOREPLY
|
---|
954 | && icp->icmp_type != ICMP_TIMXCEED
|
---|
955 | && icp->icmp_type != ICMP_UNREACH)
|
---|
956 | {
|
---|
957 | return;
|
---|
958 | }
|
---|
959 |
|
---|
960 | type = icp->icmp_type;
|
---|
961 | if ( type == ICMP_TIMXCEED
|
---|
962 | || type == ICMP_UNREACH)
|
---|
963 | {
|
---|
964 | ip = &icp->icmp_ip;
|
---|
965 | }
|
---|
966 |
|
---|
967 | icm = icmp_find_original_mbuf(pData, ip);
|
---|
968 |
|
---|
969 | if (icm == NULL)
|
---|
970 | {
|
---|
971 | Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
|
---|
972 | return;
|
---|
973 | }
|
---|
974 |
|
---|
975 | m = icm->im_m;
|
---|
976 | Assert(m != NULL);
|
---|
977 |
|
---|
978 | src = addr->sin_addr.s_addr;
|
---|
979 |
|
---|
980 | ip = mtod(m, struct ip *);
|
---|
981 | proto = ip->ip_p;
|
---|
982 | /* Now ip is pointing on header we've sent from guest */
|
---|
983 | if ( icp->icmp_type == ICMP_TIMXCEED
|
---|
984 | || icp->icmp_type == ICMP_UNREACH)
|
---|
985 | {
|
---|
986 | old_ip_len = (ip->ip_hl << 2) + 64;
|
---|
987 | if (old_ip_len > sizeof(ip_copy))
|
---|
988 | old_ip_len = sizeof(ip_copy);
|
---|
989 | memcpy(ip_copy, ip, old_ip_len);
|
---|
990 | }
|
---|
991 |
|
---|
992 | /* source address from original IP packet*/
|
---|
993 | dst = ip->ip_src.s_addr;
|
---|
994 |
|
---|
995 | /* overide ther tail of old packet */
|
---|
996 | ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
|
---|
997 | original_hlen = ip->ip_hl << 2;
|
---|
998 | /* saves original ip header and options */
|
---|
999 | memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
|
---|
1000 | m->m_len = len - hlen + original_hlen;
|
---|
1001 | ip->ip_len = m->m_len;
|
---|
1002 | ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
|
---|
1003 |
|
---|
1004 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
1005 | type = icp->icmp_type;
|
---|
1006 | if ( type == ICMP_TIMXCEED
|
---|
1007 | || type == ICMP_UNREACH)
|
---|
1008 | {
|
---|
1009 | /* according RFC 793 error messages required copy of initial IP header + 64 bit */
|
---|
1010 | memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
|
---|
1011 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | ip->ip_src.s_addr = src;
|
---|
1015 | ip->ip_dst.s_addr = dst;
|
---|
1016 | icmp_reflect(pData, m);
|
---|
1017 | LIST_REMOVE(icm, im_list);
|
---|
1018 | /* Don't call m_free here*/
|
---|
1019 |
|
---|
1020 | if ( type == ICMP_TIMXCEED
|
---|
1021 | || type == ICMP_UNREACH)
|
---|
1022 | {
|
---|
1023 | icm->im_so->so_m = NULL;
|
---|
1024 | switch (proto)
|
---|
1025 | {
|
---|
1026 | case IPPROTO_UDP:
|
---|
1027 | /*XXX: so->so_m already freed so we shouldn't call sofree */
|
---|
1028 | udp_detach(pData, icm->im_so);
|
---|
1029 | break;
|
---|
1030 | case IPPROTO_TCP:
|
---|
1031 | /*close tcp should be here */
|
---|
1032 | break;
|
---|
1033 | default:
|
---|
1034 | /* do nothing */
|
---|
1035 | break;
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 | RTMemFree(icm);
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | #ifdef RT_OS_WINDOWS
|
---|
1042 | static void
|
---|
1043 | sorecvfrom_icmp_win(PNATState pData, struct socket *so)
|
---|
1044 | {
|
---|
1045 | int len;
|
---|
1046 | int i;
|
---|
1047 | struct ip *ip;
|
---|
1048 | struct mbuf *m;
|
---|
1049 | struct icmp *icp;
|
---|
1050 | struct icmp_msg *icm;
|
---|
1051 | struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
|
---|
1052 | uint32_t src;
|
---|
1053 | ICMP_ECHO_REPLY *icr;
|
---|
1054 | int hlen = 0;
|
---|
1055 | int data_len = 0;
|
---|
1056 | int nbytes = 0;
|
---|
1057 | u_char code = ~0;
|
---|
1058 |
|
---|
1059 | len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
|
---|
1060 | if (len < 0)
|
---|
1061 | {
|
---|
1062 | LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
|
---|
1063 | return;
|
---|
1064 | }
|
---|
1065 | if (len == 0)
|
---|
1066 | return; /* no error */
|
---|
1067 |
|
---|
1068 | icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
|
---|
1069 | for (i = 0; i < len; ++i)
|
---|
1070 | {
|
---|
1071 | switch(icr[i].Status)
|
---|
1072 | {
|
---|
1073 | case IP_DEST_HOST_UNREACHABLE:
|
---|
1074 | code = (code != ~0 ? code : ICMP_UNREACH_HOST);
|
---|
1075 | case IP_DEST_NET_UNREACHABLE:
|
---|
1076 | code = (code != ~0 ? code : ICMP_UNREACH_NET);
|
---|
1077 | case IP_DEST_PROT_UNREACHABLE:
|
---|
1078 | code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
|
---|
1079 | /* UNREACH error inject here */
|
---|
1080 | case IP_DEST_PORT_UNREACHABLE:
|
---|
1081 | code = (code != ~0 ? code : ICMP_UNREACH_PORT);
|
---|
1082 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
|
---|
1083 | so->so_m = NULL;
|
---|
1084 | break;
|
---|
1085 | case IP_SUCCESS: /* echo replied */
|
---|
1086 | m = m_get(pData);
|
---|
1087 | m->m_data += if_maxlinkhdr;
|
---|
1088 | ip = mtod(m, struct ip *);
|
---|
1089 | ip->ip_src.s_addr = icr[i].Address;
|
---|
1090 | ip->ip_p = IPPROTO_ICMP;
|
---|
1091 | ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
|
---|
1092 | data_len = sizeof(struct ip);
|
---|
1093 | ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
|
---|
1094 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1095 |
|
---|
1096 | icp = (struct icmp *)&ip[1]; /* no options */
|
---|
1097 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
1098 | icp->icmp_code = 0;
|
---|
1099 | icp->icmp_id = so->so_icmp_id;
|
---|
1100 | icp->icmp_seq = so->so_icmp_seq;
|
---|
1101 |
|
---|
1102 | data_len += ICMP_MINLEN;
|
---|
1103 |
|
---|
1104 | nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
|
---|
1105 | memcpy(icp->icmp_data, icr[i].Data, nbytes);
|
---|
1106 |
|
---|
1107 | data_len += icr[i].DataSize;
|
---|
1108 |
|
---|
1109 | ip->ip_len = data_len;
|
---|
1110 | m->m_len = ip->ip_len;
|
---|
1111 |
|
---|
1112 | icmp_reflect(pData, m);
|
---|
1113 | break;
|
---|
1114 | case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
|
---|
1115 |
|
---|
1116 | ip_broken = icr[i].Data;
|
---|
1117 | icm = icmp_find_original_mbuf(pData, ip_broken);
|
---|
1118 | if (icm == NULL) {
|
---|
1119 | Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
|
---|
1120 | return;
|
---|
1121 | }
|
---|
1122 | m = icm->im_m;
|
---|
1123 | ip = mtod(m, struct ip *);
|
---|
1124 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1125 | src = ip->ip_src.s_addr;
|
---|
1126 | ip->ip_dst.s_addr = src;
|
---|
1127 | ip->ip_dst.s_addr = icr[i].Address;
|
---|
1128 |
|
---|
1129 | hlen = (ip->ip_hl << 2);
|
---|
1130 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1131 | ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
|
---|
1132 | data_len = (ip_broken->ip_hl << 2) + 64;
|
---|
1133 |
|
---|
1134 | nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
|
---|
1135 | memcpy(icp->icmp_data, ip_broken, nbytes);
|
---|
1136 | icmp_reflect(pData, m);
|
---|
1137 | break;
|
---|
1138 | default:
|
---|
1139 | Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
|
---|
1140 | break;
|
---|
1141 | }
|
---|
1142 | }
|
---|
1143 | }
|
---|
1144 | #else /* RT_OS_WINDOWS */
|
---|
1145 | static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
|
---|
1146 | {
|
---|
1147 | struct sockaddr_in addr;
|
---|
1148 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
1149 | char buff[1500];
|
---|
1150 | int len;
|
---|
1151 | len = recvfrom(so->s, buff, 1500, 0,
|
---|
1152 | (struct sockaddr *)&addr, &addrlen);
|
---|
1153 | /* XXX Check if reply is "correct"? */
|
---|
1154 |
|
---|
1155 | if (len == -1 || len == 0)
|
---|
1156 | {
|
---|
1157 | u_char code = ICMP_UNREACH_PORT;
|
---|
1158 |
|
---|
1159 | if (errno == EHOSTUNREACH)
|
---|
1160 | code = ICMP_UNREACH_HOST;
|
---|
1161 | else if(errno == ENETUNREACH)
|
---|
1162 | code = ICMP_UNREACH_NET;
|
---|
1163 |
|
---|
1164 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
|
---|
1165 | errno, strerror(errno)));
|
---|
1166 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
1167 | so->so_m = NULL;
|
---|
1168 | }
|
---|
1169 | else
|
---|
1170 | {
|
---|
1171 | send_icmp_to_guest(pData, buff, len, so, &addr);
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 | #endif /* !RT_OS_WINDOWS */
|
---|