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