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->m_data += 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 *paddr;
|
---|
661 | struct sockaddr addr;
|
---|
662 | #if 0
|
---|
663 | struct sockaddr_in host_addr;
|
---|
664 | #endif
|
---|
665 |
|
---|
666 | DEBUG_CALL("sosendto");
|
---|
667 | DEBUG_ARG("so = %lx", (long)so);
|
---|
668 | DEBUG_ARG("m = %lx", (long)m);
|
---|
669 |
|
---|
670 | memset(&addr, 0, sizeof(struct sockaddr));
|
---|
671 | #ifdef RT_OS_DARWIN
|
---|
672 | addr.sa_len = sizeof(struct sockaddr_in);
|
---|
673 | #endif
|
---|
674 | paddr = (struct sockaddr_in *)&addr;
|
---|
675 | paddr->sin_family = AF_INET;
|
---|
676 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
677 | {
|
---|
678 | /* It's an alias */
|
---|
679 | uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
680 | switch(last_byte)
|
---|
681 | {
|
---|
682 | #if 0
|
---|
683 | /* handle this case at 'default:' */
|
---|
684 | case CTL_BROADCAST:
|
---|
685 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
686 | /* Send the packet to host to fully emulate broadcast */
|
---|
687 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
688 | * the packet twice for some reason. And I cannot find any place
|
---|
689 | * in the man pages which states that sending a broadcast does not
|
---|
690 | * reach the host itself. */
|
---|
691 | host_addr.sin_family = AF_INET;
|
---|
692 | host_addr.sin_port = so->so_fport;
|
---|
693 | host_addr.sin_addr = our_addr;
|
---|
694 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
695 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
696 | break;
|
---|
697 | #endif
|
---|
698 | case CTL_DNS:
|
---|
699 | case CTL_ALIAS:
|
---|
700 | default:
|
---|
701 | if (last_byte == ~pData->netmask)
|
---|
702 | paddr->sin_addr.s_addr = INADDR_BROADCAST;
|
---|
703 | else
|
---|
704 | paddr->sin_addr = loopback_addr;
|
---|
705 | break;
|
---|
706 | }
|
---|
707 | }
|
---|
708 | else
|
---|
709 | paddr->sin_addr = so->so_faddr;
|
---|
710 | paddr->sin_port = so->so_fport;
|
---|
711 |
|
---|
712 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n",
|
---|
713 | ntohs(paddr->sin_port), inet_ntoa(paddr->sin_addr)));
|
---|
714 |
|
---|
715 | /* Don't care what port we get */
|
---|
716 | ret = sendto(so->s, m->m_data, m->m_len, 0, &addr, sizeof (struct sockaddr_in));
|
---|
717 | if (ret < 0)
|
---|
718 | {
|
---|
719 | LogRel(("UDP: sendto fails (%s)\n", strerror(errno)));
|
---|
720 | return -1;
|
---|
721 | }
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Kill the socket if there's no reply in 4 minutes,
|
---|
725 | * but only if it's an expirable socket
|
---|
726 | */
|
---|
727 | if (so->so_expire)
|
---|
728 | so->so_expire = curtime + SO_EXPIRE;
|
---|
729 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
730 | return 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * XXX This should really be tcp_listen
|
---|
735 | */
|
---|
736 | struct socket *
|
---|
737 | solisten(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
738 | {
|
---|
739 | struct sockaddr_in addr;
|
---|
740 | struct socket *so;
|
---|
741 | socklen_t addrlen = sizeof(addr);
|
---|
742 | int s, opt = 1;
|
---|
743 | int status;
|
---|
744 |
|
---|
745 | DEBUG_CALL("solisten");
|
---|
746 | DEBUG_ARG("port = %d", port);
|
---|
747 | DEBUG_ARG("laddr = %x", laddr);
|
---|
748 | DEBUG_ARG("lport = %d", lport);
|
---|
749 | DEBUG_ARG("flags = %x", flags);
|
---|
750 |
|
---|
751 | if ((so = socreate()) == NULL)
|
---|
752 | {
|
---|
753 | /* RTMemFree(so); Not sofree() ??? free(NULL) == NOP */
|
---|
754 | return NULL;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
758 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
759 | {
|
---|
760 | RTMemFree(so);
|
---|
761 | return NULL;
|
---|
762 | }
|
---|
763 |
|
---|
764 | SOCKET_LOCK_CREATE(so);
|
---|
765 | SOCKET_LOCK(so);
|
---|
766 | QSOCKET_LOCK(tcb);
|
---|
767 | insque(pData, so,&tcb);
|
---|
768 | NSOCK_INC();
|
---|
769 | QSOCKET_UNLOCK(tcb);
|
---|
770 |
|
---|
771 | /*
|
---|
772 | * SS_FACCEPTONCE sockets must time out.
|
---|
773 | */
|
---|
774 | if (flags & SS_FACCEPTONCE)
|
---|
775 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
776 |
|
---|
777 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
778 | so->so_lport = lport; /* Kept in network format */
|
---|
779 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
780 |
|
---|
781 | memset(&addr, 0, sizeof(addr));
|
---|
782 | #ifdef RT_OS_DARWIN
|
---|
783 | addr.sin_len = sizeof(addr);
|
---|
784 | #endif
|
---|
785 | addr.sin_family = AF_INET;
|
---|
786 | addr.sin_addr.s_addr = bind_addr;
|
---|
787 | addr.sin_port = port;
|
---|
788 |
|
---|
789 | if ( ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
---|
790 | || (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int)) < 0)
|
---|
791 | || (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
792 | || (listen(s, 1) < 0))
|
---|
793 | {
|
---|
794 | #ifdef RT_OS_WINDOWS
|
---|
795 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
796 | closesocket(s);
|
---|
797 | QSOCKET_LOCK(tcb);
|
---|
798 | sofree(pData, so);
|
---|
799 | QSOCKET_UNLOCK(tcb);
|
---|
800 | /* Restore the real errno */
|
---|
801 | WSASetLastError(tmperrno);
|
---|
802 | #else
|
---|
803 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
804 | close(s);
|
---|
805 | QSOCKET_LOCK(tcb);
|
---|
806 | sofree(pData, so);
|
---|
807 | QSOCKET_UNLOCK(tcb);
|
---|
808 | /* Restore the real errno */
|
---|
809 | errno = tmperrno;
|
---|
810 | #endif
|
---|
811 | return NULL;
|
---|
812 | }
|
---|
813 | fd_nonblock(s);
|
---|
814 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
|
---|
815 |
|
---|
816 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
817 | so->so_fport = addr.sin_port;
|
---|
818 | /* set socket buffers */
|
---|
819 | opt = pData->socket_rcv;
|
---|
820 | status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
|
---|
821 | if (status < 0)
|
---|
822 | {
|
---|
823 | LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
|
---|
824 | goto no_sockopt;
|
---|
825 | }
|
---|
826 | opt = pData->socket_snd;
|
---|
827 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
|
---|
828 | if (status < 0)
|
---|
829 | {
|
---|
830 | LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
|
---|
831 | goto no_sockopt;
|
---|
832 | }
|
---|
833 | no_sockopt:
|
---|
834 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
835 | so->so_faddr = alias_addr;
|
---|
836 | else
|
---|
837 | so->so_faddr = addr.sin_addr;
|
---|
838 |
|
---|
839 | so->s = s;
|
---|
840 | SOCKET_UNLOCK(so);
|
---|
841 | return so;
|
---|
842 | }
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * Data is available in so_rcv
|
---|
846 | * Just write() the data to the socket
|
---|
847 | * XXX not yet...
|
---|
848 | */
|
---|
849 | void
|
---|
850 | sorwakeup(struct socket *so)
|
---|
851 | {
|
---|
852 | #if 0
|
---|
853 | sowrite(so);
|
---|
854 | FD_CLR(so->s,&writefds);
|
---|
855 | #endif
|
---|
856 | }
|
---|
857 |
|
---|
858 | /*
|
---|
859 | * Data has been freed in so_snd
|
---|
860 | * We have room for a read() if we want to
|
---|
861 | * For now, don't read, it'll be done in the main loop
|
---|
862 | */
|
---|
863 | void
|
---|
864 | sowwakeup(struct socket *so)
|
---|
865 | {
|
---|
866 | }
|
---|
867 |
|
---|
868 | /*
|
---|
869 | * Various session state calls
|
---|
870 | * XXX Should be #define's
|
---|
871 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
872 | * times each when only 1 was needed
|
---|
873 | */
|
---|
874 | void
|
---|
875 | soisfconnecting(struct socket *so)
|
---|
876 | {
|
---|
877 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
878 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
879 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
880 | }
|
---|
881 |
|
---|
882 | void
|
---|
883 | soisfconnected(struct socket *so)
|
---|
884 | {
|
---|
885 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
886 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
887 | }
|
---|
888 |
|
---|
889 | void
|
---|
890 | sofcantrcvmore(struct socket *so)
|
---|
891 | {
|
---|
892 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
893 | {
|
---|
894 | shutdown(so->s, 0);
|
---|
895 | }
|
---|
896 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
897 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
898 | so->so_state = SS_NOFDREF; /* Don't select it */
|
---|
899 | /* XXX close() here as well? */
|
---|
900 | else
|
---|
901 | so->so_state |= SS_FCANTRCVMORE;
|
---|
902 | }
|
---|
903 |
|
---|
904 | void
|
---|
905 | sofcantsendmore(struct socket *so)
|
---|
906 | {
|
---|
907 | if ((so->so_state & SS_NOFDREF) == 0)
|
---|
908 | shutdown(so->s, 1); /* send FIN to fhost */
|
---|
909 |
|
---|
910 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
911 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
912 | so->so_state = SS_NOFDREF; /* as above */
|
---|
913 | else
|
---|
914 | so->so_state |= SS_FCANTSENDMORE;
|
---|
915 | }
|
---|
916 |
|
---|
917 | void
|
---|
918 | soisfdisconnected(struct socket *so)
|
---|
919 | {
|
---|
920 | #if 0
|
---|
921 | so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED);
|
---|
922 | close(so->s);
|
---|
923 | so->so_state = SS_ISFDISCONNECTED;
|
---|
924 | /*
|
---|
925 | * XXX Do nothing ... ?
|
---|
926 | */
|
---|
927 | #endif
|
---|
928 | }
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * Set write drain mode
|
---|
932 | * Set CANTSENDMORE once all data has been write()n
|
---|
933 | */
|
---|
934 | void
|
---|
935 | sofwdrain(struct socket *so)
|
---|
936 | {
|
---|
937 | if (so->so_rcv.sb_cc)
|
---|
938 | so->so_state |= SS_FWDRAIN;
|
---|
939 | else
|
---|
940 | sofcantsendmore(so);
|
---|
941 | }
|
---|
942 |
|
---|
943 | static void
|
---|
944 | send_icmp_to_guest(PNATState pData, char *buff, size_t len, struct socket *so, const struct sockaddr_in *addr)
|
---|
945 | {
|
---|
946 | struct ip *ip;
|
---|
947 | uint32_t dst, src;
|
---|
948 | char ip_copy[256];
|
---|
949 | struct icmp *icp;
|
---|
950 | int old_ip_len = 0;
|
---|
951 | int hlen, original_hlen = 0;
|
---|
952 | struct mbuf *m;
|
---|
953 | struct icmp_msg *icm;
|
---|
954 | uint8_t proto;
|
---|
955 | int type = 0;
|
---|
956 |
|
---|
957 | ip = (struct ip *)buff;
|
---|
958 | hlen = (ip->ip_hl << 2);
|
---|
959 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
960 |
|
---|
961 | Log(("ICMP:received msg(t:%d, c:%d)\n", icp->icmp_type, icp->icmp_code));
|
---|
962 | if ( icp->icmp_type != ICMP_ECHOREPLY
|
---|
963 | && icp->icmp_type != ICMP_TIMXCEED
|
---|
964 | && icp->icmp_type != ICMP_UNREACH)
|
---|
965 | {
|
---|
966 | return;
|
---|
967 | }
|
---|
968 |
|
---|
969 | type = icp->icmp_type;
|
---|
970 | if ( type == ICMP_TIMXCEED
|
---|
971 | || type == ICMP_UNREACH)
|
---|
972 | {
|
---|
973 | ip = &icp->icmp_ip;
|
---|
974 | }
|
---|
975 |
|
---|
976 | icm = icmp_find_original_mbuf(pData, ip);
|
---|
977 |
|
---|
978 | if (icm == NULL)
|
---|
979 | {
|
---|
980 | Log(("NAT: Can't find the corresponding packet for the received ICMP\n"));
|
---|
981 | return;
|
---|
982 | }
|
---|
983 |
|
---|
984 | m = icm->im_m;
|
---|
985 | Assert(m != NULL);
|
---|
986 |
|
---|
987 | src = addr->sin_addr.s_addr;
|
---|
988 |
|
---|
989 | ip = mtod(m, struct ip *);
|
---|
990 | proto = ip->ip_p;
|
---|
991 | /* Now ip is pointing on header we've sent from guest */
|
---|
992 | if ( icp->icmp_type == ICMP_TIMXCEED
|
---|
993 | || icp->icmp_type == ICMP_UNREACH)
|
---|
994 | {
|
---|
995 | old_ip_len = (ip->ip_hl << 2) + 64;
|
---|
996 | if (old_ip_len > sizeof(ip_copy))
|
---|
997 | old_ip_len = sizeof(ip_copy);
|
---|
998 | memcpy(ip_copy, ip, old_ip_len);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | /* source address from original IP packet*/
|
---|
1002 | dst = ip->ip_src.s_addr;
|
---|
1003 |
|
---|
1004 | /* overide ther tail of old packet */
|
---|
1005 | ip = mtod(m, struct ip *); /* ip is from mbuf we've overrided */
|
---|
1006 | original_hlen = ip->ip_hl << 2;
|
---|
1007 | /* saves original ip header and options */
|
---|
1008 | memcpy(m->m_data + original_hlen, buff + hlen, len - hlen);
|
---|
1009 | m->m_len = len - hlen + original_hlen;
|
---|
1010 | ip->ip_len = m->m_len;
|
---|
1011 | ip->ip_p = IPPROTO_ICMP; /* the original package could be whatever, but we're response via ICMP*/
|
---|
1012 |
|
---|
1013 | icp = (struct icmp *)((char *)ip + (ip->ip_hl << 2));
|
---|
1014 | type = icp->icmp_type;
|
---|
1015 | if ( type == ICMP_TIMXCEED
|
---|
1016 | || type == ICMP_UNREACH)
|
---|
1017 | {
|
---|
1018 | /* according RFC 793 error messages required copy of initial IP header + 64 bit */
|
---|
1019 | memcpy(&icp->icmp_ip, ip_copy, old_ip_len);
|
---|
1020 | ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | ip->ip_src.s_addr = src;
|
---|
1024 | ip->ip_dst.s_addr = dst;
|
---|
1025 | icmp_reflect(pData, m);
|
---|
1026 | LIST_REMOVE(icm, im_list);
|
---|
1027 | /* Don't call m_free here*/
|
---|
1028 |
|
---|
1029 | if ( type == ICMP_TIMXCEED
|
---|
1030 | || type == ICMP_UNREACH)
|
---|
1031 | {
|
---|
1032 | icm->im_so->so_m = NULL;
|
---|
1033 | switch (proto)
|
---|
1034 | {
|
---|
1035 | case IPPROTO_UDP:
|
---|
1036 | /*XXX: so->so_m already freed so we shouldn't call sofree */
|
---|
1037 | udp_detach(pData, icm->im_so);
|
---|
1038 | break;
|
---|
1039 | case IPPROTO_TCP:
|
---|
1040 | /*close tcp should be here */
|
---|
1041 | break;
|
---|
1042 | default:
|
---|
1043 | /* do nothing */
|
---|
1044 | break;
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | RTMemFree(icm);
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | #ifdef RT_OS_WINDOWS
|
---|
1051 | static void
|
---|
1052 | sorecvfrom_icmp_win(PNATState pData, struct socket *so)
|
---|
1053 | {
|
---|
1054 | int len;
|
---|
1055 | int i;
|
---|
1056 | struct ip *ip;
|
---|
1057 | struct mbuf *m;
|
---|
1058 | struct icmp *icp;
|
---|
1059 | struct icmp_msg *icm;
|
---|
1060 | struct ip *ip_broken; /* ICMP returns header + 64 bit of packet */
|
---|
1061 | uint32_t src;
|
---|
1062 | ICMP_ECHO_REPLY *icr;
|
---|
1063 | int hlen = 0;
|
---|
1064 | int data_len = 0;
|
---|
1065 | int nbytes = 0;
|
---|
1066 | u_char code = ~0;
|
---|
1067 |
|
---|
1068 | len = pData->pfIcmpParseReplies(pData->pvIcmpBuffer, pData->szIcmpBuffer);
|
---|
1069 | if (len < 0)
|
---|
1070 | {
|
---|
1071 | LogRel(("NAT: Error (%d) occurred on ICMP receiving\n", GetLastError()));
|
---|
1072 | return;
|
---|
1073 | }
|
---|
1074 | if (len == 0)
|
---|
1075 | return; /* no error */
|
---|
1076 |
|
---|
1077 | icr = (ICMP_ECHO_REPLY *)pData->pvIcmpBuffer;
|
---|
1078 | for (i = 0; i < len; ++i)
|
---|
1079 | {
|
---|
1080 | switch(icr[i].Status)
|
---|
1081 | {
|
---|
1082 | case IP_DEST_HOST_UNREACHABLE:
|
---|
1083 | code = (code != ~0 ? code : ICMP_UNREACH_HOST);
|
---|
1084 | case IP_DEST_NET_UNREACHABLE:
|
---|
1085 | code = (code != ~0 ? code : ICMP_UNREACH_NET);
|
---|
1086 | case IP_DEST_PROT_UNREACHABLE:
|
---|
1087 | code = (code != ~0 ? code : ICMP_UNREACH_PROTOCOL);
|
---|
1088 | /* UNREACH error inject here */
|
---|
1089 | case IP_DEST_PORT_UNREACHABLE:
|
---|
1090 | code = (code != ~0 ? code : ICMP_UNREACH_PORT);
|
---|
1091 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, "Error occurred!!!");
|
---|
1092 | so->so_m = NULL;
|
---|
1093 | break;
|
---|
1094 | case IP_SUCCESS: /* echo replied */
|
---|
1095 | m = m_get(pData);
|
---|
1096 | m->m_data += if_maxlinkhdr;
|
---|
1097 | ip = mtod(m, struct ip *);
|
---|
1098 | ip->ip_src.s_addr = icr[i].Address;
|
---|
1099 | ip->ip_p = IPPROTO_ICMP;
|
---|
1100 | ip->ip_dst.s_addr = so->so_laddr.s_addr; /*XXX: still the hack*/
|
---|
1101 | data_len = sizeof(struct ip);
|
---|
1102 | ip->ip_hl = data_len >> 2; /* requiered for icmp_reflect, no IP options */
|
---|
1103 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1104 |
|
---|
1105 | icp = (struct icmp *)&ip[1]; /* no options */
|
---|
1106 | icp->icmp_type = ICMP_ECHOREPLY;
|
---|
1107 | icp->icmp_code = 0;
|
---|
1108 | icp->icmp_id = so->so_icmp_id;
|
---|
1109 | icp->icmp_seq = so->so_icmp_seq;
|
---|
1110 |
|
---|
1111 | data_len += ICMP_MINLEN;
|
---|
1112 |
|
---|
1113 | nbytes = (data_len + icr[i].DataSize > m->m_size? m->m_size - data_len: icr[i].DataSize);
|
---|
1114 | memcpy(icp->icmp_data, icr[i].Data, nbytes);
|
---|
1115 |
|
---|
1116 | data_len += icr[i].DataSize;
|
---|
1117 |
|
---|
1118 | ip->ip_len = data_len;
|
---|
1119 | m->m_len = ip->ip_len;
|
---|
1120 |
|
---|
1121 | icmp_reflect(pData, m);
|
---|
1122 | break;
|
---|
1123 | case IP_TTL_EXPIRED_TRANSIT: /* TTL expired */
|
---|
1124 |
|
---|
1125 | ip_broken = icr[i].Data;
|
---|
1126 | icm = icmp_find_original_mbuf(pData, ip_broken);
|
---|
1127 | if (icm == NULL) {
|
---|
1128 | Log(("ICMP: can't find original package (first double word %x)\n", *(uint32_t *)ip_broken));
|
---|
1129 | return;
|
---|
1130 | }
|
---|
1131 | m = icm->im_m;
|
---|
1132 | ip = mtod(m, struct ip *);
|
---|
1133 | ip->ip_ttl = icr[i].Options.Ttl;
|
---|
1134 | src = ip->ip_src.s_addr;
|
---|
1135 | ip->ip_dst.s_addr = src;
|
---|
1136 | ip->ip_dst.s_addr = icr[i].Address;
|
---|
1137 |
|
---|
1138 | hlen = (ip->ip_hl << 2);
|
---|
1139 | icp = (struct icmp *)((char *)ip + hlen);
|
---|
1140 | ip_broken->ip_src.s_addr = src; /*it packet sent from host not from guest*/
|
---|
1141 | data_len = (ip_broken->ip_hl << 2) + 64;
|
---|
1142 |
|
---|
1143 | nbytes =(hlen + ICMP_MINLEN + data_len > m->m_size? m->m_size - (hlen + ICMP_MINLEN): data_len);
|
---|
1144 | memcpy(icp->icmp_data, ip_broken, nbytes);
|
---|
1145 | icmp_reflect(pData, m);
|
---|
1146 | break;
|
---|
1147 | default:
|
---|
1148 | Log(("ICMP(default): message with Status: %x was received from %x\n", icr[i].Status, icr[i].Address));
|
---|
1149 | break;
|
---|
1150 | }
|
---|
1151 | }
|
---|
1152 | }
|
---|
1153 | #else /* RT_OS_WINDOWS */
|
---|
1154 | static void sorecvfrom_icmp_unix(PNATState pData, struct socket *so)
|
---|
1155 | {
|
---|
1156 | struct sockaddr_in addr;
|
---|
1157 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
1158 | char buff[1500];
|
---|
1159 | int len;
|
---|
1160 | len = recvfrom(so->s, buff, 1500, 0,
|
---|
1161 | (struct sockaddr *)&addr, &addrlen);
|
---|
1162 | /* XXX Check if reply is "correct"? */
|
---|
1163 |
|
---|
1164 | if (len == -1 || len == 0)
|
---|
1165 | {
|
---|
1166 | u_char code = ICMP_UNREACH_PORT;
|
---|
1167 |
|
---|
1168 | if (errno == EHOSTUNREACH)
|
---|
1169 | code = ICMP_UNREACH_HOST;
|
---|
1170 | else if(errno == ENETUNREACH)
|
---|
1171 | code = ICMP_UNREACH_NET;
|
---|
1172 |
|
---|
1173 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
|
---|
1174 | errno, strerror(errno)));
|
---|
1175 | icmp_error(pData, so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
|
---|
1176 | so->so_m = NULL;
|
---|
1177 | }
|
---|
1178 | else
|
---|
1179 | {
|
---|
1180 | send_icmp_to_guest(pData, buff, len, so, &addr);
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | #endif /* !RT_OS_WINDOWS */
|
---|