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