1 | /* $Id: tcp_subr.c 30045 2010-06-04 20:38:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - TCP support.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
22 | * The Regents of the University of California. All rights reserved.
|
---|
23 | *
|
---|
24 | * Redistribution and use in source and binary forms, with or without
|
---|
25 | * modification, are permitted provided that the following conditions
|
---|
26 | * are met:
|
---|
27 | * 1. Redistributions of source code must retain the above copyright
|
---|
28 | * notice, this list of conditions and the following disclaimer.
|
---|
29 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
30 | * notice, this list of conditions and the following disclaimer in the
|
---|
31 | * documentation and/or other materials provided with the distribution.
|
---|
32 | * 3. All advertising materials mentioning features or use of this software
|
---|
33 | * must display the following acknowledgement:
|
---|
34 | * This product includes software developed by the University of
|
---|
35 | * California, Berkeley and its contributors.
|
---|
36 | * 4. Neither the name of the University nor the names of its contributors
|
---|
37 | * may be used to endorse or promote products derived from this software
|
---|
38 | * without specific prior written permission.
|
---|
39 | *
|
---|
40 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
50 | * SUCH DAMAGE.
|
---|
51 | *
|
---|
52 | * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
|
---|
53 | * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Changes and additions relating to SLiRP
|
---|
58 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
59 | *
|
---|
60 | * Please read the file COPYRIGHT for the
|
---|
61 | * terms and conditions of the copyright.
|
---|
62 | */
|
---|
63 |
|
---|
64 | #define WANT_SYS_IOCTL_H
|
---|
65 | #include <slirp.h>
|
---|
66 |
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Tcp initialization
|
---|
70 | */
|
---|
71 | void
|
---|
72 | tcp_init(PNATState pData)
|
---|
73 | {
|
---|
74 | tcp_iss = 1; /* wrong */
|
---|
75 | tcb.so_next = tcb.so_prev = &tcb;
|
---|
76 | tcp_last_so = &tcb;
|
---|
77 | tcp_reass_maxqlen = 48;
|
---|
78 | tcp_reass_maxseg = 256;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Create template to be used to send tcp packets on a connection.
|
---|
83 | * Call after host entry created, fills
|
---|
84 | * in a skeletal tcp/ip header, minimizing the amount of work
|
---|
85 | * necessary when the connection is used.
|
---|
86 | */
|
---|
87 | /* struct tcpiphdr * */
|
---|
88 | void
|
---|
89 | tcp_template(struct tcpcb *tp)
|
---|
90 | {
|
---|
91 | struct socket *so = tp->t_socket;
|
---|
92 | register struct tcpiphdr *n = &tp->t_template;
|
---|
93 |
|
---|
94 | memset(n->ti_x1, 0, 9);
|
---|
95 | n->ti_pr = IPPROTO_TCP;
|
---|
96 | n->ti_len = RT_H2N_U16(sizeof (struct tcpiphdr) - sizeof (struct ip));
|
---|
97 | n->ti_src = so->so_faddr;
|
---|
98 | n->ti_dst = so->so_laddr;
|
---|
99 | n->ti_sport = so->so_fport;
|
---|
100 | n->ti_dport = so->so_lport;
|
---|
101 |
|
---|
102 | n->ti_seq = 0;
|
---|
103 | n->ti_ack = 0;
|
---|
104 | n->ti_x2 = 0;
|
---|
105 | n->ti_off = 5;
|
---|
106 | n->ti_flags = 0;
|
---|
107 | n->ti_win = 0;
|
---|
108 | n->ti_sum = 0;
|
---|
109 | n->ti_urp = 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Send a single message to the TCP at address specified by
|
---|
114 | * the given TCP/IP header. If m == 0, then we make a copy
|
---|
115 | * of the tcpiphdr at ti and send directly to the addressed host.
|
---|
116 | * This is used to force keep alive messages out using the TCP
|
---|
117 | * template for a connection tp->t_template. If flags are given
|
---|
118 | * then we send a message back to the TCP which originated the
|
---|
119 | * segment ti, and discard the mbuf containing it and any other
|
---|
120 | * attached mbufs.
|
---|
121 | *
|
---|
122 | * In any case the ack and sequence number of the transmitted
|
---|
123 | * segment are as specified by the parameters.
|
---|
124 | */
|
---|
125 | void
|
---|
126 | tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
|
---|
127 | {
|
---|
128 | register int tlen;
|
---|
129 | int win = 0;
|
---|
130 |
|
---|
131 | DEBUG_CALL("tcp_respond");
|
---|
132 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
133 | DEBUG_ARG("ti = %lx", (long)ti);
|
---|
134 | DEBUG_ARG("m = %lx", (long)m);
|
---|
135 | DEBUG_ARG("ack = %u", ack);
|
---|
136 | DEBUG_ARG("seq = %u", seq);
|
---|
137 | DEBUG_ARG("flags = %x", flags);
|
---|
138 |
|
---|
139 | if (tp)
|
---|
140 | win = sbspace(&tp->t_socket->so_rcv);
|
---|
141 | if (m == 0)
|
---|
142 | {
|
---|
143 | if ((m = m_gethdr(pData, M_DONTWAIT, MT_HEADER)) == NULL)
|
---|
144 | return;
|
---|
145 | #ifdef TCP_COMPAT_42
|
---|
146 | tlen = 1;
|
---|
147 | #else
|
---|
148 | tlen = 0;
|
---|
149 | #endif
|
---|
150 | m->m_data += if_maxlinkhdr;
|
---|
151 | m->m_pkthdr.header = mtod(m, void *);
|
---|
152 | *mtod(m, struct tcpiphdr *) = *ti;
|
---|
153 | ti = mtod(m, struct tcpiphdr *);
|
---|
154 | flags = TH_ACK;
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * ti points into m so the next line is just making
|
---|
160 | * the mbuf point to ti
|
---|
161 | */
|
---|
162 | m->m_data = (caddr_t)ti;
|
---|
163 |
|
---|
164 | m->m_len = sizeof (struct tcpiphdr);
|
---|
165 | tlen = 0;
|
---|
166 | #define xchg(a,b,type) { type t; t = a; a = b; b = t; }
|
---|
167 | xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
|
---|
168 | xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
|
---|
169 | #undef xchg
|
---|
170 | }
|
---|
171 | ti->ti_len = RT_H2N_U16((u_short)(sizeof (struct tcphdr) + tlen));
|
---|
172 | tlen += sizeof (struct tcpiphdr);
|
---|
173 | m->m_len = tlen;
|
---|
174 |
|
---|
175 | memset(ti->ti_x1, 0, 9);
|
---|
176 | ti->ti_seq = RT_H2N_U32(seq);
|
---|
177 | ti->ti_ack = RT_H2N_U32(ack);
|
---|
178 | ti->ti_x2 = 0;
|
---|
179 | ti->ti_off = sizeof (struct tcphdr) >> 2;
|
---|
180 | ti->ti_flags = flags;
|
---|
181 | if (tp)
|
---|
182 | ti->ti_win = RT_H2N_U16((u_int16_t) (win >> tp->rcv_scale));
|
---|
183 | else
|
---|
184 | ti->ti_win = RT_H2N_U16((u_int16_t)win);
|
---|
185 | ti->ti_urp = 0;
|
---|
186 | ti->ti_sum = 0;
|
---|
187 | ti->ti_sum = cksum(m, tlen);
|
---|
188 | ((struct ip *)ti)->ip_len = tlen;
|
---|
189 |
|
---|
190 | if(flags & TH_RST)
|
---|
191 | ((struct ip *)ti)->ip_ttl = MAXTTL;
|
---|
192 | else
|
---|
193 | ((struct ip *)ti)->ip_ttl = ip_defttl;
|
---|
194 |
|
---|
195 | (void) ip_output(pData, (struct socket *)0, m);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * Create a new TCP control block, making an
|
---|
200 | * empty reassembly queue and hooking it to the argument
|
---|
201 | * protocol control block.
|
---|
202 | */
|
---|
203 | struct tcpcb *
|
---|
204 | tcp_newtcpcb(PNATState pData, struct socket *so)
|
---|
205 | {
|
---|
206 | register struct tcpcb *tp;
|
---|
207 |
|
---|
208 | tp = (struct tcpcb *)RTMemAllocZ(sizeof(*tp));
|
---|
209 | if (tp == NULL)
|
---|
210 | return ((struct tcpcb *)0);
|
---|
211 |
|
---|
212 | tp->t_maxseg = tcp_mssdflt;
|
---|
213 |
|
---|
214 | tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
|
---|
215 | tp->t_socket = so;
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
|
---|
219 | * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
|
---|
220 | * reasonable initial retransmit time.
|
---|
221 | */
|
---|
222 | tp->t_srtt = TCPTV_SRTTBASE;
|
---|
223 | tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
|
---|
224 | tp->t_rttmin = TCPTV_MIN;
|
---|
225 |
|
---|
226 | TCPT_RANGESET(tp->t_rxtcur,
|
---|
227 | ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
|
---|
228 | TCPTV_MIN, TCPTV_REXMTMAX);
|
---|
229 |
|
---|
230 | tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
---|
231 | tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
---|
232 | tp->t_state = TCPS_CLOSED;
|
---|
233 |
|
---|
234 | so->so_tcpcb = tp;
|
---|
235 |
|
---|
236 | return (tp);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Drop a TCP connection, reporting
|
---|
241 | * the specified error. If connection is synchronized,
|
---|
242 | * then send a RST to peer.
|
---|
243 | */
|
---|
244 | struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
|
---|
245 | {
|
---|
246 | /* tcp_drop(tp, errno)
|
---|
247 | register struct tcpcb *tp;
|
---|
248 | int errno;
|
---|
249 | {
|
---|
250 | */
|
---|
251 | DEBUG_CALL("tcp_drop");
|
---|
252 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
253 | DEBUG_ARG("errno = %d", errno);
|
---|
254 |
|
---|
255 | if (TCPS_HAVERCVDSYN(tp->t_state))
|
---|
256 | {
|
---|
257 | tp->t_state = TCPS_CLOSED;
|
---|
258 | (void) tcp_output(pData, tp);
|
---|
259 | tcpstat.tcps_drops++;
|
---|
260 | }
|
---|
261 | else
|
---|
262 | tcpstat.tcps_conndrops++;
|
---|
263 | #if 0
|
---|
264 | if (errno == ETIMEDOUT && tp->t_softerror)
|
---|
265 | errno = tp->t_softerror;
|
---|
266 |
|
---|
267 | so->so_error = errno;
|
---|
268 | #endif
|
---|
269 | return (tcp_close(pData, tp));
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * Close a TCP control block:
|
---|
274 | * discard all space held by the tcp
|
---|
275 | * discard internet protocol block
|
---|
276 | * wake up any sleepers
|
---|
277 | */
|
---|
278 | struct tcpcb *
|
---|
279 | tcp_close(PNATState pData, register struct tcpcb *tp)
|
---|
280 | {
|
---|
281 | struct socket *so = tp->t_socket;
|
---|
282 | struct socket *so_next, *so_prev;
|
---|
283 |
|
---|
284 | struct tseg_qent *te = NULL;
|
---|
285 | DEBUG_CALL("tcp_close");
|
---|
286 | DEBUG_ARG("tp = %lx", (long )tp);
|
---|
287 | so_next = so_prev = NULL;
|
---|
288 | /*XXX: freeing the reassembly queue */
|
---|
289 | while (!LIST_EMPTY(&tp->t_segq))
|
---|
290 | {
|
---|
291 | te = LIST_FIRST(&tp->t_segq);
|
---|
292 | LIST_REMOVE(te, tqe_q);
|
---|
293 | m_freem(pData, te->tqe_m);
|
---|
294 | RTMemFree(te);
|
---|
295 | tcp_reass_qsize--;
|
---|
296 | }
|
---|
297 | RTMemFree(tp);
|
---|
298 | so->so_tcpcb = 0;
|
---|
299 | soisfdisconnected(so);
|
---|
300 | /* clobber input socket cache if we're closing the cached connection */
|
---|
301 | if (so == tcp_last_so)
|
---|
302 | tcp_last_so = &tcb;
|
---|
303 | closesocket(so->s);
|
---|
304 | /* Avoid double free if the socket is listening and therefore doesn't have
|
---|
305 | * any sbufs reserved. */
|
---|
306 | if (!(so->so_state & SS_FACCEPTCONN))
|
---|
307 | {
|
---|
308 | #ifndef VBOX_WITH_SLIRP_BSD_SBUF
|
---|
309 | sbfree(&so->so_rcv);
|
---|
310 | sbfree(&so->so_snd);
|
---|
311 | #else
|
---|
312 | sbuf_delete(&so->so_rcv);
|
---|
313 | sbuf_delete(&so->so_snd);
|
---|
314 | #endif
|
---|
315 | }
|
---|
316 | sofree(pData, so);
|
---|
317 | SOCKET_UNLOCK(so);
|
---|
318 | tcpstat.tcps_closed++;
|
---|
319 | return ((struct tcpcb *)0);
|
---|
320 | }
|
---|
321 |
|
---|
322 | void
|
---|
323 | tcp_drain()
|
---|
324 | {
|
---|
325 | /* XXX */
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*
|
---|
329 | * When a source quench is received, close congestion window
|
---|
330 | * to one segment. We will gradually open it again as we proceed.
|
---|
331 | */
|
---|
332 |
|
---|
333 | #if 0
|
---|
334 |
|
---|
335 | void
|
---|
336 | tcp_quench(i, int errno)
|
---|
337 | {
|
---|
338 | struct tcpcb *tp = intotcpcb(inp);
|
---|
339 |
|
---|
340 | if (tp)
|
---|
341 | tp->snd_cwnd = tp->t_maxseg;
|
---|
342 | }
|
---|
343 |
|
---|
344 | #endif
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * TCP protocol interface to socket abstraction.
|
---|
348 | */
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * User issued close, and wish to trail through shutdown states:
|
---|
352 | * if never received SYN, just forget it. If got a SYN from peer,
|
---|
353 | * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
|
---|
354 | * If already got a FIN from peer, then almost done; go to LAST_ACK
|
---|
355 | * state. In all other cases, have already sent FIN to peer (e.g.
|
---|
356 | * after PRU_SHUTDOWN), and just have to play tedious game waiting
|
---|
357 | * for peer to send FIN or not respond to keep-alives, etc.
|
---|
358 | * We can let the user exit from the close as soon as the FIN is acked.
|
---|
359 | */
|
---|
360 | void
|
---|
361 | tcp_sockclosed(PNATState pData, struct tcpcb *tp)
|
---|
362 | {
|
---|
363 | DEBUG_CALL("tcp_sockclosed");
|
---|
364 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
365 |
|
---|
366 | switch (tp->t_state)
|
---|
367 | {
|
---|
368 | case TCPS_CLOSED:
|
---|
369 | case TCPS_LISTEN:
|
---|
370 | case TCPS_SYN_SENT:
|
---|
371 | tp->t_state = TCPS_CLOSED;
|
---|
372 | tp = tcp_close(pData, tp);
|
---|
373 | break;
|
---|
374 |
|
---|
375 | case TCPS_SYN_RECEIVED:
|
---|
376 | case TCPS_ESTABLISHED:
|
---|
377 | tp->t_state = TCPS_FIN_WAIT_1;
|
---|
378 | break;
|
---|
379 |
|
---|
380 | case TCPS_CLOSE_WAIT:
|
---|
381 | tp->t_state = TCPS_LAST_ACK;
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | /* soisfdisconnecting(tp->t_socket); */
|
---|
385 | if ( tp
|
---|
386 | && tp->t_state >= TCPS_FIN_WAIT_2)
|
---|
387 | soisfdisconnected(tp->t_socket);
|
---|
388 | /*
|
---|
389 | * (vasily) there're situations when the FIN or FIN,ACK are lost (Windows host)
|
---|
390 | * and retransmitting keeps VBox busy on sending closing sequences *very* frequent,
|
---|
391 | * easting a lot of CPU. To avoid this we don't sent on sockets marked as closed
|
---|
392 | * (see slirp.c for details about setting so_close member).
|
---|
393 | */
|
---|
394 | if ( tp
|
---|
395 | && tp->t_socket
|
---|
396 | && !tp->t_socket->so_close)
|
---|
397 | tcp_output(pData, tp);
|
---|
398 | }
|
---|
399 |
|
---|
400 | /*
|
---|
401 | * Connect to a host on the Internet
|
---|
402 | * Called by tcp_input
|
---|
403 | * Only do a connect, the tcp fields will be set in tcp_input
|
---|
404 | * return 0 if there's a result of the connect,
|
---|
405 | * else return -1 means we're still connecting
|
---|
406 | * The return value is almost always -1 since the socket is
|
---|
407 | * nonblocking. Connect returns after the SYN is sent, and does
|
---|
408 | * not wait for ACK+SYN.
|
---|
409 | */
|
---|
410 | int tcp_fconnect(PNATState pData, struct socket *so)
|
---|
411 | {
|
---|
412 | int ret = 0;
|
---|
413 |
|
---|
414 | DEBUG_CALL("tcp_fconnect");
|
---|
415 | DEBUG_ARG("so = %lx", (long )so);
|
---|
416 |
|
---|
417 | if ((ret = so->s = socket(AF_INET, SOCK_STREAM, 0)) >= 0)
|
---|
418 | {
|
---|
419 | int opt, s = so->s;
|
---|
420 | struct sockaddr_in addr;
|
---|
421 |
|
---|
422 | fd_nonblock(s);
|
---|
423 | opt = 1;
|
---|
424 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
---|
425 | opt = 1;
|
---|
426 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&opt, sizeof(opt));
|
---|
427 |
|
---|
428 | addr.sin_family = AF_INET;
|
---|
429 | if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
|
---|
430 | {
|
---|
431 | /* It's an alias */
|
---|
432 | switch(RT_N2H_U32(so->so_faddr.s_addr) & ~pData->netmask)
|
---|
433 | {
|
---|
434 | case CTL_DNS:
|
---|
435 | case CTL_ALIAS:
|
---|
436 | default:
|
---|
437 | addr.sin_addr = loopback_addr;
|
---|
438 | break;
|
---|
439 | }
|
---|
440 | }
|
---|
441 | else
|
---|
442 | addr.sin_addr = so->so_faddr;
|
---|
443 | addr.sin_port = so->so_fport;
|
---|
444 |
|
---|
445 | DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
|
---|
446 | "addr.sin_addr.s_addr=%.16s\n",
|
---|
447 | RT_N2H_U16(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
448 | /* We don't care what port we get */
|
---|
449 | ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * If it's not in progress, it failed, so we just return 0,
|
---|
453 | * without clearing SS_NOFDREF
|
---|
454 | */
|
---|
455 | soisfconnecting(so);
|
---|
456 | }
|
---|
457 |
|
---|
458 | return(ret);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Accept the socket and connect to the local-host
|
---|
463 | *
|
---|
464 | * We have a problem. The correct thing to do would be
|
---|
465 | * to first connect to the local-host, and only if the
|
---|
466 | * connection is accepted, then do an accept() here.
|
---|
467 | * But, a) we need to know who's trying to connect
|
---|
468 | * to the socket to be able to SYN the local-host, and
|
---|
469 | * b) we are already connected to the foreign host by
|
---|
470 | * the time it gets to accept(), so... We simply accept
|
---|
471 | * here and SYN the local-host.
|
---|
472 | */
|
---|
473 | void
|
---|
474 | tcp_connect(PNATState pData, struct socket *inso)
|
---|
475 | {
|
---|
476 | struct socket *so;
|
---|
477 | struct sockaddr_in addr;
|
---|
478 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
479 | struct tcpcb *tp;
|
---|
480 | int s, opt;
|
---|
481 | int status;
|
---|
482 | socklen_t optlen;
|
---|
483 | static int cVerbose = 1;
|
---|
484 |
|
---|
485 | DEBUG_CALL("tcp_connect");
|
---|
486 | DEBUG_ARG("inso = %lx", (long)inso);
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * If it's an SS_ACCEPTONCE socket, no need to socreate()
|
---|
490 | * another socket, just use the accept() socket.
|
---|
491 | */
|
---|
492 | if (inso->so_state & SS_FACCEPTONCE)
|
---|
493 | {
|
---|
494 | /* FACCEPTONCE already have a tcpcb */
|
---|
495 | so = inso;
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | if ((so = socreate()) == NULL)
|
---|
500 | {
|
---|
501 | /* If it failed, get rid of the pending connection */
|
---|
502 | closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
|
---|
503 | return;
|
---|
504 | }
|
---|
505 | if (tcp_attach(pData, so) < 0)
|
---|
506 | {
|
---|
507 | RTMemFree(so); /* NOT sofree */
|
---|
508 | return;
|
---|
509 | }
|
---|
510 | so->so_laddr = inso->so_laddr;
|
---|
511 | so->so_lport = inso->so_lport;
|
---|
512 | so->so_la = inso->so_la;
|
---|
513 | }
|
---|
514 |
|
---|
515 | (void) tcp_mss(pData, sototcpcb(so), 0);
|
---|
516 |
|
---|
517 | fd_nonblock(inso->s);
|
---|
518 | if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0)
|
---|
519 | {
|
---|
520 | tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
|
---|
521 | return;
|
---|
522 | }
|
---|
523 | fd_nonblock(s);
|
---|
524 | opt = 1;
|
---|
525 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
|
---|
526 | opt = 1;
|
---|
527 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int));
|
---|
528 | #if 0
|
---|
529 | opt = 1;
|
---|
530 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY,(char *)&opt, sizeof(int));
|
---|
531 | #endif
|
---|
532 |
|
---|
533 | optlen = sizeof(int);
|
---|
534 | status = getsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, &optlen);
|
---|
535 | if (status < 0)
|
---|
536 | {
|
---|
537 | LogRel(("NAT: Error(%d) while getting RCV capacity\n", errno));
|
---|
538 | goto no_sockopt;
|
---|
539 | }
|
---|
540 | if (cVerbose > 0)
|
---|
541 | LogRel(("NAT: old socket rcv size: %dKB\n", opt / 1024));
|
---|
542 | /* @todo (r-vvl) make it configurable (via extra data) */
|
---|
543 | opt = pData->socket_rcv;
|
---|
544 | status = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&opt, sizeof(int));
|
---|
545 | if (status < 0)
|
---|
546 | {
|
---|
547 | LogRel(("NAT: Error(%d) while setting RCV capacity to (%d)\n", errno, opt));
|
---|
548 | goto no_sockopt;
|
---|
549 | }
|
---|
550 | optlen = sizeof(int);
|
---|
551 | status = getsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, &optlen);
|
---|
552 | if (status < 0)
|
---|
553 | {
|
---|
554 | LogRel(("NAT: Error(%d) while getting SND capacity\n", errno));
|
---|
555 | goto no_sockopt;
|
---|
556 | }
|
---|
557 | if (cVerbose > 0)
|
---|
558 | LogRel(("NAT: old socket snd size: %dKB\n", opt / 1024));
|
---|
559 | opt = pData->socket_rcv;
|
---|
560 | status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&opt, sizeof(int));
|
---|
561 | if (status < 0)
|
---|
562 | {
|
---|
563 | LogRel(("NAT: Error(%d) while setting SND capacity to (%d)\n", errno, opt));
|
---|
564 | goto no_sockopt;
|
---|
565 | }
|
---|
566 | if (cVerbose > 0)
|
---|
567 | cVerbose--;
|
---|
568 |
|
---|
569 | no_sockopt:
|
---|
570 | so->so_fport = addr.sin_port;
|
---|
571 | so->so_faddr = addr.sin_addr;
|
---|
572 | /* Translate connections from localhost to the real hostname */
|
---|
573 | if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
|
---|
574 | so->so_faddr = alias_addr;
|
---|
575 |
|
---|
576 | /* Close the accept() socket, set right state */
|
---|
577 | if (inso->so_state & SS_FACCEPTONCE)
|
---|
578 | {
|
---|
579 | closesocket(so->s); /* If we only accept once, close the accept() socket */
|
---|
580 | so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
|
---|
581 | /* if it's not FACCEPTONCE, it's already NOFDREF */
|
---|
582 | }
|
---|
583 | so->s = s;
|
---|
584 |
|
---|
585 | tp = sototcpcb(so);
|
---|
586 |
|
---|
587 | tcp_template(tp);
|
---|
588 |
|
---|
589 | /* Compute window scaling to request. */
|
---|
590 | /* while (tp->request_r_scale < TCP_MAX_WINSHIFT
|
---|
591 | * && (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
|
---|
592 | * tp->request_r_scale++;
|
---|
593 | */
|
---|
594 |
|
---|
595 | /* soisconnecting(so); */ /* NOFDREF used instead */
|
---|
596 | tcpstat.tcps_connattempt++;
|
---|
597 |
|
---|
598 | tp->t_state = TCPS_SYN_SENT;
|
---|
599 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
---|
600 | tp->iss = tcp_iss;
|
---|
601 | tcp_iss += TCP_ISSINCR/2;
|
---|
602 | tcp_sendseqinit(tp);
|
---|
603 | tcp_output(pData, tp);
|
---|
604 | }
|
---|
605 |
|
---|
606 | /*
|
---|
607 | * Attach a TCPCB to a socket.
|
---|
608 | */
|
---|
609 | int
|
---|
610 | tcp_attach(PNATState pData, struct socket *so)
|
---|
611 | {
|
---|
612 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
613 | return -1;
|
---|
614 |
|
---|
615 | SOCKET_LOCK_CREATE(so);
|
---|
616 | QSOCKET_LOCK(tcb);
|
---|
617 | insque(pData, so, &tcb);
|
---|
618 | NSOCK_INC();
|
---|
619 | QSOCKET_UNLOCK(tcb);
|
---|
620 | return 0;
|
---|
621 | }
|
---|