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