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