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