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