1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
|
---|
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_input.c 8.5 (Berkeley) 4/10/94
|
---|
34 | * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman 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 | #include <slirp.h>
|
---|
46 | #include "ip_icmp.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
|
---|
50 |
|
---|
51 | /* for modulo comparisons of timestamps */
|
---|
52 | #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
|
---|
53 | #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
|
---|
54 |
|
---|
55 | #ifndef VBOX_WITH_BSD_TCP_REASS
|
---|
56 | /*
|
---|
57 | * Insert segment ti into reassembly queue of tcp with
|
---|
58 | * control block tp. Return TH_FIN if reassembly now includes
|
---|
59 | * a segment with FIN. The macro form does the common case inline
|
---|
60 | * (segment is the next to be received on an established connection,
|
---|
61 | * and the queue is empty), avoiding linkage into and removal
|
---|
62 | * from the queue and repetition of various conversions.
|
---|
63 | * Set DELACK for segments received in order, but ack immediately
|
---|
64 | * when segments are out of order (so fast retransmit can work).
|
---|
65 | */
|
---|
66 | #ifdef TCP_ACK_HACK
|
---|
67 | #define TCP_REASS(pData, tp, ti, m, so, flags) {\
|
---|
68 | if ((ti)->ti_seq == (tp)->rcv_nxt && \
|
---|
69 | u32_to_ptr((pData), (tp)->seg_next, struct tcpcb *) == (tp) && \
|
---|
70 | (tp)->t_state == TCPS_ESTABLISHED) {\
|
---|
71 | if (ti->ti_flags & TH_PUSH) \
|
---|
72 | tp->t_flags |= TF_ACKNOW; \
|
---|
73 | else \
|
---|
74 | tp->t_flags |= TF_DELACK; \
|
---|
75 | (tp)->rcv_nxt += (ti)->ti_len; \
|
---|
76 | flags = (ti)->ti_flags & TH_FIN; \
|
---|
77 | tcpstat.tcps_rcvpack++;\
|
---|
78 | tcpstat.tcps_rcvbyte += (ti)->ti_len;\
|
---|
79 | if (so->so_emu) { \
|
---|
80 | if (tcp_emu((pData), (so),(m))) sbappend((pData), (so), (m)); \
|
---|
81 | } else \
|
---|
82 | sbappend((pData), (so), (m)); \
|
---|
83 | /* sorwakeup(so); */ \
|
---|
84 | } else {\
|
---|
85 | (flags) = tcp_reass((pData), (tp), (ti), (m)); \
|
---|
86 | tp->t_flags |= TF_ACKNOW; \
|
---|
87 | } \
|
---|
88 | }
|
---|
89 | #else
|
---|
90 | #define TCP_REASS(pData, tp, ti, m, so, flags) { \
|
---|
91 | if ((ti)->ti_seq == (tp)->rcv_nxt && \
|
---|
92 | u32_to_ptr((pData), (tp)->seg_next, struct tcpcb *) == (tp) && \
|
---|
93 | (tp)->t_state == TCPS_ESTABLISHED) { \
|
---|
94 | tp->t_flags |= TF_DELACK; \
|
---|
95 | (tp)->rcv_nxt += (ti)->ti_len; \
|
---|
96 | flags = (ti)->ti_flags & TH_FIN; \
|
---|
97 | tcpstat.tcps_rcvpack++;\
|
---|
98 | tcpstat.tcps_rcvbyte += (ti)->ti_len;\
|
---|
99 | if (so->so_emu) { \
|
---|
100 | if (tcp_emu((pData), (so),(m))) sbappend((pData), (so), (m)); \
|
---|
101 | } else \
|
---|
102 | sbappend((pData), (so), (m)); \
|
---|
103 | /* sorwakeup(so); */ \
|
---|
104 | } else { \
|
---|
105 | (flags) = tcp_reass((pData), (tp), (ti), (m)); \
|
---|
106 | tp->t_flags |= TF_ACKNOW; \
|
---|
107 | } \
|
---|
108 | }
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | int
|
---|
112 | tcp_reass(PNATState pData, register struct tcpcb *tp, register struct tcpiphdr *ti, struct mbuf *m)
|
---|
113 | {
|
---|
114 | register struct tcpiphdr *q;
|
---|
115 | struct socket *so = tp->t_socket;
|
---|
116 | int flags;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Call with ti==0 after become established to
|
---|
120 | * force pre-ESTABLISHED data up to user socket.
|
---|
121 | */
|
---|
122 | if (ti == 0)
|
---|
123 | goto present;
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Find a segment which begins after this one does.
|
---|
127 | */
|
---|
128 | for (q = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *); q != (struct tcpiphdr *)tp;
|
---|
129 | q = u32_to_ptr(pData, q->ti_next, struct tcpiphdr *))
|
---|
130 | if (SEQ_GT(q->ti_seq, ti->ti_seq))
|
---|
131 | break;
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * If there is a preceding segment, it may provide some of
|
---|
135 | * our data already. If so, drop the data from the incoming
|
---|
136 | * segment. If it provides all of our data, drop us.
|
---|
137 | */
|
---|
138 | if (u32_to_ptr(pData, q->ti_prev, struct tcpiphdr *) != (struct tcpiphdr *)tp) {
|
---|
139 | register int i;
|
---|
140 | q = u32_to_ptr(pData, q->ti_prev, struct tcpiphdr *);
|
---|
141 | /* conversion to int (in i) handles seq wraparound */
|
---|
142 | i = q->ti_seq + q->ti_len - ti->ti_seq;
|
---|
143 | if (i > 0) {
|
---|
144 | if (i >= ti->ti_len) {
|
---|
145 | tcpstat.tcps_rcvduppack++;
|
---|
146 | tcpstat.tcps_rcvdupbyte += ti->ti_len;
|
---|
147 | m_freem(pData, m);
|
---|
148 | /*
|
---|
149 | * Try to present any queued data
|
---|
150 | * at the left window edge to the user.
|
---|
151 | * This is needed after the 3-WHS
|
---|
152 | * completes.
|
---|
153 | */
|
---|
154 | goto present; /* ??? */
|
---|
155 | }
|
---|
156 | m_adj(m, i);
|
---|
157 | ti->ti_len -= i;
|
---|
158 | ti->ti_seq += i;
|
---|
159 | }
|
---|
160 | q = u32_to_ptr(pData, q->ti_next, struct tcpiphdr *);
|
---|
161 | }
|
---|
162 | tcpstat.tcps_rcvoopack++;
|
---|
163 | tcpstat.tcps_rcvoobyte += ti->ti_len;
|
---|
164 | REASS_MBUF_SET(ti, m); /* XXX */
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * While we overlap succeeding segments trim them or,
|
---|
168 | * if they are completely covered, dequeue them.
|
---|
169 | */
|
---|
170 | while (q != (struct tcpiphdr *)tp) {
|
---|
171 | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
|
---|
172 | if (i <= 0)
|
---|
173 | break;
|
---|
174 | if (i < q->ti_len) {
|
---|
175 | q->ti_seq += i;
|
---|
176 | q->ti_len -= i;
|
---|
177 | m_adj(REASS_MBUF_GET(q), i);
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | q = u32_to_ptr(pData, q->ti_next, struct tcpiphdr *);
|
---|
181 | m = REASS_MBUF_GET(u32_to_ptr(pData, q->ti_prev, struct tcpiphdr *));
|
---|
182 | remque_32(pData, u32_to_ptr(pData, q->ti_prev, struct tcpiphdr *));
|
---|
183 | m_freem(pData, m);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Stick new segment in its place.
|
---|
188 | */
|
---|
189 | insque_32(pData, ti, u32_to_ptr(pData, q->ti_prev, struct tcpiphdr *));
|
---|
190 |
|
---|
191 | present:
|
---|
192 | /*
|
---|
193 | * Present data to user, advancing rcv_nxt through
|
---|
194 | * completed sequence space.
|
---|
195 | */
|
---|
196 | if (!TCPS_HAVEESTABLISHED(tp->t_state))
|
---|
197 | return (0);
|
---|
198 | ti = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *);
|
---|
199 | if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
|
---|
200 | return (0);
|
---|
201 | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
|
---|
202 | return (0);
|
---|
203 | do {
|
---|
204 | tp->rcv_nxt += ti->ti_len;
|
---|
205 | flags = ti->ti_flags & TH_FIN;
|
---|
206 | remque_32(pData, ti);
|
---|
207 | m = REASS_MBUF_GET(ti); /* XXX */
|
---|
208 | ti = u32_to_ptr(pData, ti->ti_next, struct tcpiphdr *);
|
---|
209 | /* if (so->so_state & SS_FCANTRCVMORE) */
|
---|
210 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
211 | m_freem(pData, m);
|
---|
212 | else {
|
---|
213 | if (so->so_emu) {
|
---|
214 | if (tcp_emu(pData, so,m)) sbappend(pData, so, m);
|
---|
215 | } else
|
---|
216 | sbappend(pData, so, m);
|
---|
217 | }
|
---|
218 | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
|
---|
219 | /* sorwakeup(so); */
|
---|
220 | return (flags);
|
---|
221 | }
|
---|
222 |
|
---|
223 | #else /* VBOX_WITH_BSD_TCP_REASS */
|
---|
224 |
|
---|
225 | #ifndef TCP_ACK_HACK
|
---|
226 | #define DELAY_ACK(tp, ti) \
|
---|
227 | if (ti->ti_flags & TH_PUSH) \
|
---|
228 | tp->t_flags |= TF_ACKNOW; \
|
---|
229 | else \
|
---|
230 | tp->t_flags |= TF_DELACK;
|
---|
231 | #else /* !TCP_ACK_HACK */
|
---|
232 | #define DELAY_ACK(tp, ign) \
|
---|
233 | tp->t_flags |= TF_DELACK;
|
---|
234 | #endif /* TCP_ACK_HACK */
|
---|
235 |
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * deps: netinet/tcp_reass.c
|
---|
239 | * tcp_reass_maxqlen = 48 (deafault)
|
---|
240 | * tcp_reass_maxseg = nmbclusters/16 (nmbclusters = 1024 + maxusers * 64 from kern/kern_mbuf.c let's say 256)
|
---|
241 | */
|
---|
242 | int
|
---|
243 | tcp_reass(PNATState pData, struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
|
---|
244 | {
|
---|
245 | struct tseg_qent *q;
|
---|
246 | struct tseg_qent *p = NULL;
|
---|
247 | struct tseg_qent *nq;
|
---|
248 | struct tseg_qent *te = NULL;
|
---|
249 | struct socket *so = tp->t_socket;
|
---|
250 | int flags;
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * XXX: tcp_reass() is rather inefficient with its data structures
|
---|
254 | * and should be rewritten (see NetBSD for optimizations). While
|
---|
255 | * doing that it should move to its own file tcp_reass.c.
|
---|
256 | */
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Call with th==NULL after become established to
|
---|
260 | * force pre-ESTABLISHED data up to user socket.
|
---|
261 | */
|
---|
262 | if (th == NULL)
|
---|
263 | goto present;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Limit the number of segments in the reassembly queue to prevent
|
---|
267 | * holding on to too many segments (and thus running out of mbufs).
|
---|
268 | * Make sure to let the missing segment through which caused this
|
---|
269 | * queue. Always keep one global queue entry spare to be able to
|
---|
270 | * process the missing segment.
|
---|
271 | */
|
---|
272 | if (th->th_seq != tp->rcv_nxt &&
|
---|
273 | (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
|
---|
274 | tp->t_segqlen >= tcp_reass_maxqlen)) {
|
---|
275 | tcp_reass_overflows++;
|
---|
276 | tcpstat.tcps_rcvmemdrop++;
|
---|
277 | m_freem(pData, m);
|
---|
278 | *tlenp = 0;
|
---|
279 | return (0);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Allocate a new queue entry. If we can't, or hit the zone limit
|
---|
284 | * just drop the pkt.
|
---|
285 | */
|
---|
286 | te = malloc(sizeof(struct tseg_qent));
|
---|
287 | if (te == NULL) {
|
---|
288 | tcpstat.tcps_rcvmemdrop++;
|
---|
289 | m_freem(pData, m);
|
---|
290 | *tlenp = 0;
|
---|
291 | return (0);
|
---|
292 | }
|
---|
293 | tp->t_segqlen++;
|
---|
294 | tcp_reass_qsize++;
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Find a segment which begins after this one does.
|
---|
298 | */
|
---|
299 | LIST_FOREACH(q, &tp->t_segq, tqe_q) {
|
---|
300 | if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
|
---|
301 | break;
|
---|
302 | p = q;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * If there is a preceding segment, it may provide some of
|
---|
307 | * our data already. If so, drop the data from the incoming
|
---|
308 | * segment. If it provides all of our data, drop us.
|
---|
309 | */
|
---|
310 | if (p != NULL) {
|
---|
311 | int i;
|
---|
312 | /* conversion to int (in i) handles seq wraparound */
|
---|
313 | i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
|
---|
314 | if (i > 0) {
|
---|
315 | if (i >= *tlenp) {
|
---|
316 | tcpstat.tcps_rcvduppack++;
|
---|
317 | tcpstat.tcps_rcvdupbyte += *tlenp;
|
---|
318 | m_freem(pData, m);
|
---|
319 | free(te);
|
---|
320 | tp->t_segqlen--;
|
---|
321 | tcp_reass_qsize--;
|
---|
322 | /*
|
---|
323 | * Try to present any queued data
|
---|
324 | * at the left window edge to the user.
|
---|
325 | * This is needed after the 3-WHS
|
---|
326 | * completes.
|
---|
327 | */
|
---|
328 | goto present; /* ??? */
|
---|
329 | }
|
---|
330 | m_adj(m, i);
|
---|
331 | *tlenp -= i;
|
---|
332 | th->th_seq += i;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | tcpstat.tcps_rcvoopack++;
|
---|
336 | tcpstat.tcps_rcvoobyte += *tlenp;
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * While we overlap succeeding segments trim them or,
|
---|
340 | * if they are completely covered, dequeue them.
|
---|
341 | */
|
---|
342 | while (q) {
|
---|
343 | int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
|
---|
344 | if (i <= 0)
|
---|
345 | break;
|
---|
346 | if (i < q->tqe_len) {
|
---|
347 | q->tqe_th->th_seq += i;
|
---|
348 | q->tqe_len -= i;
|
---|
349 | m_adj(q->tqe_m, i);
|
---|
350 | break;
|
---|
351 | }
|
---|
352 |
|
---|
353 | nq = LIST_NEXT(q, tqe_q);
|
---|
354 | LIST_REMOVE(q, tqe_q);
|
---|
355 | m_freem(pData, q->tqe_m);
|
---|
356 | free(q);
|
---|
357 | tp->t_segqlen--;
|
---|
358 | tcp_reass_qsize--;
|
---|
359 | q = nq;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /* Insert the new segment queue entry into place. */
|
---|
363 | te->tqe_m = m;
|
---|
364 | te->tqe_th = th;
|
---|
365 | te->tqe_len = *tlenp;
|
---|
366 |
|
---|
367 | if (p == NULL) {
|
---|
368 | LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
|
---|
369 | } else {
|
---|
370 | LIST_INSERT_AFTER(p, te, tqe_q);
|
---|
371 | }
|
---|
372 |
|
---|
373 | present:
|
---|
374 | /*
|
---|
375 | * Present data to user, advancing rcv_nxt through
|
---|
376 | * completed sequence space.
|
---|
377 | */
|
---|
378 | if (!TCPS_HAVEESTABLISHED(tp->t_state))
|
---|
379 | return (0);
|
---|
380 | q = LIST_FIRST(&tp->t_segq);
|
---|
381 | if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
|
---|
382 | return (0);
|
---|
383 | do {
|
---|
384 | tp->rcv_nxt += q->tqe_len;
|
---|
385 | flags = q->tqe_th->th_flags & TH_FIN;
|
---|
386 | nq = LIST_NEXT(q, tqe_q);
|
---|
387 | LIST_REMOVE(q, tqe_q);
|
---|
388 | /* XXX: This place should be checked for the same code in
|
---|
389 | * original BSD code for Slirp and current BSD used SS_FCANTRCVMORE
|
---|
390 | */
|
---|
391 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
392 | m_freem(pData, q->tqe_m);
|
---|
393 | else
|
---|
394 | sbappend(pData, so, q->tqe_m);
|
---|
395 | free(q);
|
---|
396 | tp->t_segqlen--;
|
---|
397 | tcp_reass_qsize--;
|
---|
398 | q = nq;
|
---|
399 | } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
|
---|
400 | return (flags);
|
---|
401 | }
|
---|
402 | #endif /* VBOX_WITH_BSD_TCP_REASS */
|
---|
403 |
|
---|
404 | /*
|
---|
405 | * TCP input routine, follows pages 65-76 of the
|
---|
406 | * protocol specification dated September, 1981 very closely.
|
---|
407 | */
|
---|
408 | void
|
---|
409 | tcp_input(PNATState pData, register struct mbuf *m, int iphlen, struct socket *inso)
|
---|
410 | {
|
---|
411 | struct ip save_ip, *ip;
|
---|
412 | register struct tcpiphdr *ti;
|
---|
413 | caddr_t optp = NULL;
|
---|
414 | int optlen = 0;
|
---|
415 | int len, tlen, off;
|
---|
416 | register struct tcpcb *tp = 0;
|
---|
417 | register int tiflags;
|
---|
418 | struct socket *so = 0;
|
---|
419 | int todrop, acked, ourfinisacked, needoutput = 0;
|
---|
420 | /* int dropsocket = 0; */
|
---|
421 | int iss = 0;
|
---|
422 | u_long tiwin;
|
---|
423 | /* int ts_present = 0; */
|
---|
424 | int mbuf_freed = 0;
|
---|
425 |
|
---|
426 | DEBUG_CALL("tcp_input");
|
---|
427 | DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n",
|
---|
428 | (long )m, iphlen, (long )inso ));
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * If called with m == 0, then we're continuing the connect
|
---|
432 | */
|
---|
433 | if (m == NULL) {
|
---|
434 | so = inso;
|
---|
435 |
|
---|
436 | /* Re-set a few variables */
|
---|
437 | tp = sototcpcb(so);
|
---|
438 | m = so->so_m;
|
---|
439 | so->so_m = 0;
|
---|
440 | ti = so->so_ti;
|
---|
441 | tiwin = ti->ti_win;
|
---|
442 | tiflags = ti->ti_flags;
|
---|
443 |
|
---|
444 | goto cont_conn;
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | tcpstat.tcps_rcvtotal++;
|
---|
449 | /*
|
---|
450 | * Get IP and TCP header together in first mbuf.
|
---|
451 | * Note: IP leaves IP header in first mbuf.
|
---|
452 | */
|
---|
453 | ti = mtod(m, struct tcpiphdr *);
|
---|
454 | if (iphlen > sizeof(struct ip )) {
|
---|
455 | ip_stripoptions(m, (struct mbuf *)0);
|
---|
456 | iphlen=sizeof(struct ip );
|
---|
457 | }
|
---|
458 | /* XXX Check if too short */
|
---|
459 |
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Save a copy of the IP header in case we want restore it
|
---|
463 | * for sending an ICMP error message in response.
|
---|
464 | */
|
---|
465 | ip=mtod(m, struct ip *);
|
---|
466 | save_ip = *ip;
|
---|
467 | save_ip.ip_len+= iphlen;
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Checksum extended TCP header and data.
|
---|
471 | */
|
---|
472 | tlen = ((struct ip *)ti)->ip_len;
|
---|
473 | ti->ti_next = ti->ti_prev = 0;
|
---|
474 | ti->ti_x1 = 0;
|
---|
475 | ti->ti_len = htons((u_int16_t)tlen);
|
---|
476 | len = sizeof(struct ip ) + tlen;
|
---|
477 | /* keep checksum for ICMP reply
|
---|
478 | * ti->ti_sum = cksum(m, len);
|
---|
479 | * if (ti->ti_sum) { */
|
---|
480 | if(cksum(m, len)) {
|
---|
481 | tcpstat.tcps_rcvbadsum++;
|
---|
482 | goto drop;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /*
|
---|
486 | * Check that TCP offset makes sense,
|
---|
487 | * pull out TCP options and adjust length. XXX
|
---|
488 | */
|
---|
489 | off = ti->ti_off << 2;
|
---|
490 | if (off < sizeof (struct tcphdr) || off > tlen) {
|
---|
491 | tcpstat.tcps_rcvbadoff++;
|
---|
492 | goto drop;
|
---|
493 | }
|
---|
494 | tlen -= off;
|
---|
495 | ti->ti_len = tlen;
|
---|
496 | if (off > sizeof (struct tcphdr)) {
|
---|
497 | optlen = off - sizeof (struct tcphdr);
|
---|
498 | optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Do quick retrieval of timestamp options ("options
|
---|
502 | * prediction?"). If timestamp is the only option and it's
|
---|
503 | * formatted as recommended in RFC 1323 appendix A, we
|
---|
504 | * quickly get the values now and not bother calling
|
---|
505 | * tcp_dooptions(), etc.
|
---|
506 | */
|
---|
507 | /* if ((optlen == TCPOLEN_TSTAMP_APPA ||
|
---|
508 | * (optlen > TCPOLEN_TSTAMP_APPA &&
|
---|
509 | * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
|
---|
510 | * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
|
---|
511 | * (ti->ti_flags & TH_SYN) == 0) {
|
---|
512 | * ts_present = 1;
|
---|
513 | * ts_val = ntohl(*(u_int32_t *)(optp + 4));
|
---|
514 | * ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
|
---|
515 | * optp = NULL; / * we've parsed the options * /
|
---|
516 | * }
|
---|
517 | */
|
---|
518 | }
|
---|
519 | tiflags = ti->ti_flags;
|
---|
520 |
|
---|
521 | /*
|
---|
522 | * Convert TCP protocol specific fields to host format.
|
---|
523 | */
|
---|
524 | NTOHL(ti->ti_seq);
|
---|
525 | NTOHL(ti->ti_ack);
|
---|
526 | NTOHS(ti->ti_win);
|
---|
527 | NTOHS(ti->ti_urp);
|
---|
528 |
|
---|
529 | /*
|
---|
530 | * Drop TCP, IP headers and TCP options.
|
---|
531 | */
|
---|
532 | m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
|
---|
533 | m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
|
---|
534 |
|
---|
535 | /*
|
---|
536 | * Locate pcb for segment.
|
---|
537 | */
|
---|
538 | findso:
|
---|
539 | so = tcp_last_so;
|
---|
540 | if (so->so_fport != ti->ti_dport ||
|
---|
541 | so->so_lport != ti->ti_sport ||
|
---|
542 | so->so_laddr.s_addr != ti->ti_src.s_addr ||
|
---|
543 | so->so_faddr.s_addr != ti->ti_dst.s_addr) {
|
---|
544 | so = solookup(&tcb, ti->ti_src, ti->ti_sport,
|
---|
545 | ti->ti_dst, ti->ti_dport);
|
---|
546 | if (so)
|
---|
547 | tcp_last_so = so;
|
---|
548 | ++tcpstat.tcps_socachemiss;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * If the state is CLOSED (i.e., TCB does not exist) then
|
---|
553 | * all data in the incoming segment is discarded.
|
---|
554 | * If the TCB exists but is in CLOSED state, it is embryonic,
|
---|
555 | * but should either do a listen or a connect soon.
|
---|
556 | *
|
---|
557 | * state == CLOSED means we've done socreate() but haven't
|
---|
558 | * attached it to a protocol yet...
|
---|
559 | *
|
---|
560 | * XXX If a TCB does not exist, and the TH_SYN flag is
|
---|
561 | * the only flag set, then create a session, mark it
|
---|
562 | * as if it was LISTENING, and continue...
|
---|
563 | */
|
---|
564 | if (so == 0) {
|
---|
565 | if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
|
---|
566 | goto dropwithreset;
|
---|
567 |
|
---|
568 | if ((so = socreate()) == NULL)
|
---|
569 | goto dropwithreset;
|
---|
570 | if (tcp_attach(pData, so) < 0) {
|
---|
571 | free(so); /* Not sofree (if it failed, it's not insqued) */
|
---|
572 | goto dropwithreset;
|
---|
573 | }
|
---|
574 |
|
---|
575 | sbreserve(&so->so_snd, tcp_sndspace);
|
---|
576 | sbreserve(&so->so_rcv, tcp_rcvspace);
|
---|
577 |
|
---|
578 | /* tcp_last_so = so; */ /* XXX ? */
|
---|
579 | /* tp = sototcpcb(so); */
|
---|
580 |
|
---|
581 | so->so_laddr = ti->ti_src;
|
---|
582 | so->so_lport = ti->ti_sport;
|
---|
583 | so->so_faddr = ti->ti_dst;
|
---|
584 | so->so_fport = ti->ti_dport;
|
---|
585 |
|
---|
586 | if ((so->so_iptos = tcp_tos(so)) == 0)
|
---|
587 | so->so_iptos = ((struct ip *)ti)->ip_tos;
|
---|
588 |
|
---|
589 | tp = sototcpcb(so);
|
---|
590 | tp->t_state = TCPS_LISTEN;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * If this is a still-connecting socket, this probably
|
---|
595 | * a retransmit of the SYN. Whether it's a retransmit SYN
|
---|
596 | * or something else, we nuke it.
|
---|
597 | */
|
---|
598 | if (so->so_state & SS_ISFCONNECTING)
|
---|
599 | goto drop;
|
---|
600 |
|
---|
601 | tp = sototcpcb(so);
|
---|
602 |
|
---|
603 | /* XXX Should never fail */
|
---|
604 | if (tp == 0)
|
---|
605 | goto dropwithreset;
|
---|
606 | if (tp->t_state == TCPS_CLOSED)
|
---|
607 | goto drop;
|
---|
608 |
|
---|
609 | /* Unscale the window into a 32-bit value. */
|
---|
610 | /* if ((tiflags & TH_SYN) == 0)
|
---|
611 | * tiwin = ti->ti_win << tp->snd_scale;
|
---|
612 | * else
|
---|
613 | */
|
---|
614 | tiwin = ti->ti_win;
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * Segment received on connection.
|
---|
618 | * Reset idle time and keep-alive timer.
|
---|
619 | */
|
---|
620 | tp->t_idle = 0;
|
---|
621 | if (so_options)
|
---|
622 | tp->t_timer[TCPT_KEEP] = tcp_keepintvl;
|
---|
623 | else
|
---|
624 | tp->t_timer[TCPT_KEEP] = tcp_keepidle;
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Process options if not in LISTEN state,
|
---|
628 | * else do it below (after getting remote address).
|
---|
629 | */
|
---|
630 | if (optp && tp->t_state != TCPS_LISTEN)
|
---|
631 | tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
|
---|
632 | /* , */
|
---|
633 | /* &ts_present, &ts_val, &ts_ecr); */
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Header prediction: check for the two common cases
|
---|
637 | * of a uni-directional data xfer. If the packet has
|
---|
638 | * no control flags, is in-sequence, the window didn't
|
---|
639 | * change and we're not retransmitting, it's a
|
---|
640 | * candidate. If the length is zero and the ack moved
|
---|
641 | * forward, we're the sender side of the xfer. Just
|
---|
642 | * free the data acked & wake any higher level process
|
---|
643 | * that was blocked waiting for space. If the length
|
---|
644 | * is non-zero and the ack didn't move, we're the
|
---|
645 | * receiver side. If we're getting packets in-order
|
---|
646 | * (the reassembly queue is empty), add the data to
|
---|
647 | * the socket buffer and note that we need a delayed ack.
|
---|
648 | *
|
---|
649 | * XXX Some of these tests are not needed
|
---|
650 | * eg: the tiwin == tp->snd_wnd prevents many more
|
---|
651 | * predictions.. with no *real* advantage..
|
---|
652 | */
|
---|
653 | if (tp->t_state == TCPS_ESTABLISHED &&
|
---|
654 | (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
|
---|
655 | /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
|
---|
656 | ti->ti_seq == tp->rcv_nxt &&
|
---|
657 | tiwin && tiwin == tp->snd_wnd &&
|
---|
658 | tp->snd_nxt == tp->snd_max) {
|
---|
659 | /*
|
---|
660 | * If last ACK falls within this segment's sequence numbers,
|
---|
661 | * record the timestamp.
|
---|
662 | */
|
---|
663 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
|
---|
664 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
|
---|
665 | * tp->ts_recent_age = tcp_now;
|
---|
666 | * tp->ts_recent = ts_val;
|
---|
667 | * }
|
---|
668 | */
|
---|
669 | if (ti->ti_len == 0) {
|
---|
670 | if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
|
---|
671 | SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
|
---|
672 | tp->snd_cwnd >= tp->snd_wnd) {
|
---|
673 | /*
|
---|
674 | * this is a pure ack for outstanding data.
|
---|
675 | */
|
---|
676 | ++tcpstat.tcps_predack;
|
---|
677 | /* if (ts_present)
|
---|
678 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
|
---|
679 | * else
|
---|
680 | */ if (tp->t_rtt &&
|
---|
681 | SEQ_GT(ti->ti_ack, tp->t_rtseq))
|
---|
682 | tcp_xmit_timer(pData, tp, tp->t_rtt);
|
---|
683 | acked = ti->ti_ack - tp->snd_una;
|
---|
684 | tcpstat.tcps_rcvackpack++;
|
---|
685 | tcpstat.tcps_rcvackbyte += acked;
|
---|
686 | sbdrop(&so->so_snd, acked);
|
---|
687 | tp->snd_una = ti->ti_ack;
|
---|
688 | m_freem(pData, m);
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * If all outstanding data are acked, stop
|
---|
692 | * retransmit timer, otherwise restart timer
|
---|
693 | * using current (possibly backed-off) value.
|
---|
694 | * If process is waiting for space,
|
---|
695 | * wakeup/selwakeup/signal. If data
|
---|
696 | * are ready to send, let tcp_output
|
---|
697 | * decide between more output or persist.
|
---|
698 | */
|
---|
699 | if (tp->snd_una == tp->snd_max)
|
---|
700 | tp->t_timer[TCPT_REXMT] = 0;
|
---|
701 | else if (tp->t_timer[TCPT_PERSIST] == 0)
|
---|
702 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * There's room in so_snd, sowwakup will read()
|
---|
706 | * from the socket if we can
|
---|
707 | */
|
---|
708 | /* if (so->so_snd.sb_flags & SB_NOTIFY)
|
---|
709 | * sowwakeup(so);
|
---|
710 | */
|
---|
711 | /*
|
---|
712 | * This is called because sowwakeup might have
|
---|
713 | * put data into so_snd. Since we don't so sowwakeup,
|
---|
714 | * we don't need this.. XXX???
|
---|
715 | */
|
---|
716 | if (so->so_snd.sb_cc)
|
---|
717 | (void) tcp_output(pData, tp);
|
---|
718 |
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | } else if (ti->ti_ack == tp->snd_una &&
|
---|
722 | #ifndef VBOX_WITH_BSD_TCP_REASS
|
---|
723 | u32_to_ptr(pData, tp->seg_next, struct tcpcb *) == tp &&
|
---|
724 | #else /* VBOX_WITH_BSD_TCP_REASS */
|
---|
725 | LIST_FIRST(&tp->t_segq) &&
|
---|
726 | #endif /* VBOX_WITH_BSD_TCP_REASS */
|
---|
727 | ti->ti_len <= sbspace(&so->so_rcv)) {
|
---|
728 | /*
|
---|
729 | * this is a pure, in-sequence data packet
|
---|
730 | * with nothing on the reassembly queue and
|
---|
731 | * we have enough buffer space to take it.
|
---|
732 | */
|
---|
733 | ++tcpstat.tcps_preddat;
|
---|
734 | tp->rcv_nxt += ti->ti_len;
|
---|
735 | tcpstat.tcps_rcvpack++;
|
---|
736 | tcpstat.tcps_rcvbyte += ti->ti_len;
|
---|
737 | /*
|
---|
738 | * Add data to socket buffer.
|
---|
739 | */
|
---|
740 | if (so->so_emu) {
|
---|
741 | if (tcp_emu(pData, so,m)) sbappend(pData, so, m);
|
---|
742 | } else
|
---|
743 | sbappend(pData, so, m);
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * XXX This is called when data arrives. Later, check
|
---|
747 | * if we can actually write() to the socket
|
---|
748 | * XXX Need to check? It's be NON_BLOCKING
|
---|
749 | */
|
---|
750 | /* sorwakeup(so); */
|
---|
751 |
|
---|
752 | /*
|
---|
753 | * If this is a short packet, then ACK now - with Nagel
|
---|
754 | * congestion avoidance sender won't send more until
|
---|
755 | * he gets an ACK.
|
---|
756 | *
|
---|
757 | * It is better to not delay acks at all to maximize
|
---|
758 | * TCP throughput. See RFC 2581.
|
---|
759 | */
|
---|
760 | tp->t_flags |= TF_ACKNOW;
|
---|
761 | tcp_output(pData, tp);
|
---|
762 | return;
|
---|
763 | }
|
---|
764 | } /* header prediction */
|
---|
765 | /*
|
---|
766 | * Calculate amount of space in receive window,
|
---|
767 | * and then do TCP input processing.
|
---|
768 | * Receive window is amount of space in rcv queue,
|
---|
769 | * but not less than advertised window.
|
---|
770 | */
|
---|
771 | { int win;
|
---|
772 | win = sbspace(&so->so_rcv);
|
---|
773 | if (win < 0)
|
---|
774 | win = 0;
|
---|
775 | tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
|
---|
776 | }
|
---|
777 |
|
---|
778 | switch (tp->t_state) {
|
---|
779 |
|
---|
780 | /*
|
---|
781 | * If the state is LISTEN then ignore segment if it contains an RST.
|
---|
782 | * If the segment contains an ACK then it is bad and send a RST.
|
---|
783 | * If it does not contain a SYN then it is not interesting; drop it.
|
---|
784 | * Don't bother responding if the destination was a broadcast.
|
---|
785 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
|
---|
786 | * tp->iss, and send a segment:
|
---|
787 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
|
---|
788 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
|
---|
789 | * Fill in remote peer address fields if not previously specified.
|
---|
790 | * Enter SYN_RECEIVED state, and process any other fields of this
|
---|
791 | * segment in this state.
|
---|
792 | */
|
---|
793 | case TCPS_LISTEN: {
|
---|
794 |
|
---|
795 | if (tiflags & TH_RST)
|
---|
796 | goto drop;
|
---|
797 | if (tiflags & TH_ACK)
|
---|
798 | goto dropwithreset;
|
---|
799 | if ((tiflags & TH_SYN) == 0)
|
---|
800 | goto drop;
|
---|
801 |
|
---|
802 | /*
|
---|
803 | * This has way too many gotos...
|
---|
804 | * But a bit of spaghetti code never hurt anybody :)
|
---|
805 | */
|
---|
806 |
|
---|
807 | if (so->so_emu & EMU_NOCONNECT) {
|
---|
808 | so->so_emu &= ~EMU_NOCONNECT;
|
---|
809 | goto cont_input;
|
---|
810 | }
|
---|
811 |
|
---|
812 | if((tcp_fconnect(pData, so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
|
---|
813 | u_char code=ICMP_UNREACH_NET;
|
---|
814 | DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
|
---|
815 | errno,strerror(errno)));
|
---|
816 | if(errno == ECONNREFUSED) {
|
---|
817 | /* ACK the SYN, send RST to refuse the connection */
|
---|
818 | tcp_respond(pData, tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
|
---|
819 | TH_RST|TH_ACK);
|
---|
820 | } else {
|
---|
821 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
---|
822 | HTONL(ti->ti_seq); /* restore tcp header */
|
---|
823 | HTONL(ti->ti_ack);
|
---|
824 | HTONS(ti->ti_win);
|
---|
825 | HTONS(ti->ti_urp);
|
---|
826 | m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
|
---|
827 | m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
|
---|
828 | *ip=save_ip;
|
---|
829 | icmp_error(pData, m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
830 | }
|
---|
831 | tp = tcp_close(pData, tp);
|
---|
832 | m_free(pData, m);
|
---|
833 | } else {
|
---|
834 | /*
|
---|
835 | * Haven't connected yet, save the current mbuf
|
---|
836 | * and ti, and return
|
---|
837 | * XXX Some OS's don't tell us whether the connect()
|
---|
838 | * succeeded or not. So we must time it out.
|
---|
839 | */
|
---|
840 | so->so_m = m;
|
---|
841 | so->so_ti = ti;
|
---|
842 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
---|
843 | tp->t_state = TCPS_SYN_RECEIVED;
|
---|
844 | }
|
---|
845 | return;
|
---|
846 |
|
---|
847 | cont_conn:
|
---|
848 | /* m==NULL
|
---|
849 | * Check if the connect succeeded
|
---|
850 | */
|
---|
851 | if (so->so_state & SS_NOFDREF) {
|
---|
852 | tp = tcp_close(pData, tp);
|
---|
853 | goto dropwithreset;
|
---|
854 | }
|
---|
855 | cont_input:
|
---|
856 | tcp_template(tp);
|
---|
857 |
|
---|
858 | if (optp)
|
---|
859 | tcp_dooptions(pData, tp, (u_char *)optp, optlen, ti);
|
---|
860 | /* , */
|
---|
861 | /* &ts_present, &ts_val, &ts_ecr); */
|
---|
862 |
|
---|
863 | if (iss)
|
---|
864 | tp->iss = iss;
|
---|
865 | else
|
---|
866 | tp->iss = tcp_iss;
|
---|
867 | tcp_iss += TCP_ISSINCR/2;
|
---|
868 | tp->irs = ti->ti_seq;
|
---|
869 | tcp_sendseqinit(tp);
|
---|
870 | tcp_rcvseqinit(tp);
|
---|
871 | tp->t_flags |= TF_ACKNOW;
|
---|
872 | tp->t_state = TCPS_SYN_RECEIVED;
|
---|
873 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
---|
874 | tcpstat.tcps_accepts++;
|
---|
875 | goto trimthenstep6;
|
---|
876 | } /* case TCPS_LISTEN */
|
---|
877 |
|
---|
878 | /*
|
---|
879 | * If the state is SYN_SENT:
|
---|
880 | * if seg contains an ACK, but not for our SYN, drop the input.
|
---|
881 | * if seg contains a RST, then drop the connection.
|
---|
882 | * if seg does not contain SYN, then drop it.
|
---|
883 | * Otherwise this is an acceptable SYN segment
|
---|
884 | * initialize tp->rcv_nxt and tp->irs
|
---|
885 | * if seg contains ack then advance tp->snd_una
|
---|
886 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
|
---|
887 | * arrange for segment to be acked (eventually)
|
---|
888 | * continue processing rest of data/controls, beginning with URG
|
---|
889 | */
|
---|
890 | case TCPS_SYN_SENT:
|
---|
891 | if ((tiflags & TH_ACK) &&
|
---|
892 | (SEQ_LEQ(ti->ti_ack, tp->iss) ||
|
---|
893 | SEQ_GT(ti->ti_ack, tp->snd_max)))
|
---|
894 | goto dropwithreset;
|
---|
895 |
|
---|
896 | if (tiflags & TH_RST) {
|
---|
897 | if (tiflags & TH_ACK)
|
---|
898 | tp = tcp_drop(pData, tp,0); /* XXX Check t_softerror! */
|
---|
899 | goto drop;
|
---|
900 | }
|
---|
901 |
|
---|
902 | if ((tiflags & TH_SYN) == 0)
|
---|
903 | goto drop;
|
---|
904 | if (tiflags & TH_ACK) {
|
---|
905 | tp->snd_una = ti->ti_ack;
|
---|
906 | if (SEQ_LT(tp->snd_nxt, tp->snd_una))
|
---|
907 | tp->snd_nxt = tp->snd_una;
|
---|
908 | }
|
---|
909 |
|
---|
910 | tp->t_timer[TCPT_REXMT] = 0;
|
---|
911 | tp->irs = ti->ti_seq;
|
---|
912 | tcp_rcvseqinit(tp);
|
---|
913 | tp->t_flags |= TF_ACKNOW;
|
---|
914 | if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
|
---|
915 | tcpstat.tcps_connects++;
|
---|
916 | soisfconnected(so);
|
---|
917 | tp->t_state = TCPS_ESTABLISHED;
|
---|
918 |
|
---|
919 | /* Do window scaling on this connection? */
|
---|
920 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
|
---|
921 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
|
---|
922 | * tp->snd_scale = tp->requested_s_scale;
|
---|
923 | * tp->rcv_scale = tp->request_r_scale;
|
---|
924 | * }
|
---|
925 | */
|
---|
926 | #ifndef VBOX_WITH_BSD_TCP_REASS
|
---|
927 | (void) tcp_reass(pData, tp, (struct tcpiphdr *)0,
|
---|
928 | (struct mbuf *)0);
|
---|
929 | #else /* VBOX_WITH_BSD_TCP_REASS */
|
---|
930 | (void) tcp_reass(pData, tp, (struct tcphdr *)0, NULL, (struct mbuf *)0);
|
---|
931 | #endif /* VBOX_WITH_BSD_TCP_REASS */
|
---|
932 | /*
|
---|
933 | * if we didn't have to retransmit the SYN,
|
---|
934 | * use its rtt as our initial srtt & rtt var.
|
---|
935 | */
|
---|
936 | if (tp->t_rtt)
|
---|
937 | tcp_xmit_timer(pData, tp, tp->t_rtt);
|
---|
938 | } else
|
---|
939 | tp->t_state = TCPS_SYN_RECEIVED;
|
---|
940 |
|
---|
941 | trimthenstep6:
|
---|
942 | /*
|
---|
943 | * Advance ti->ti_seq to correspond to first data byte.
|
---|
944 | * If data, trim to stay within window,
|
---|
945 | * dropping FIN if necessary.
|
---|
946 | */
|
---|
947 | ti->ti_seq++;
|
---|
948 | if (ti->ti_len > tp->rcv_wnd) {
|
---|
949 | todrop = ti->ti_len - tp->rcv_wnd;
|
---|
950 | m_adj(m, -todrop);
|
---|
951 | ti->ti_len = tp->rcv_wnd;
|
---|
952 | tiflags &= ~TH_FIN;
|
---|
953 | tcpstat.tcps_rcvpackafterwin++;
|
---|
954 | tcpstat.tcps_rcvbyteafterwin += todrop;
|
---|
955 | }
|
---|
956 | tp->snd_wl1 = ti->ti_seq - 1;
|
---|
957 | tp->rcv_up = ti->ti_seq;
|
---|
958 | goto step6;
|
---|
959 | } /* switch tp->t_state */
|
---|
960 | /*
|
---|
961 | * States other than LISTEN or SYN_SENT.
|
---|
962 | * First check timestamp, if present.
|
---|
963 | * Then check that at least some bytes of segment are within
|
---|
964 | * receive window. If segment begins before rcv_nxt,
|
---|
965 | * drop leading data (and SYN); if nothing left, just ack.
|
---|
966 | *
|
---|
967 | * RFC 1323 PAWS: If we have a timestamp reply on this segment
|
---|
968 | * and it's less than ts_recent, drop it.
|
---|
969 | */
|
---|
970 | /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
|
---|
971 | * TSTMP_LT(ts_val, tp->ts_recent)) {
|
---|
972 | *
|
---|
973 | */ /* Check to see if ts_recent is over 24 days old. */
|
---|
974 | /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
|
---|
975 | */ /*
|
---|
976 | * * Invalidate ts_recent. If this segment updates
|
---|
977 | * * ts_recent, the age will be reset later and ts_recent
|
---|
978 | * * will get a valid value. If it does not, setting
|
---|
979 | * * ts_recent to zero will at least satisfy the
|
---|
980 | * * requirement that zero be placed in the timestamp
|
---|
981 | * * echo reply when ts_recent isn't valid. The
|
---|
982 | * * age isn't reset until we get a valid ts_recent
|
---|
983 | * * because we don't want out-of-order segments to be
|
---|
984 | * * dropped when ts_recent is old.
|
---|
985 | * */
|
---|
986 | /* tp->ts_recent = 0;
|
---|
987 | * } else {
|
---|
988 | * tcpstat.tcps_rcvduppack++;
|
---|
989 | * tcpstat.tcps_rcvdupbyte += ti->ti_len;
|
---|
990 | * tcpstat.tcps_pawsdrop++;
|
---|
991 | * goto dropafterack;
|
---|
992 | * }
|
---|
993 | * }
|
---|
994 | */
|
---|
995 |
|
---|
996 | todrop = tp->rcv_nxt - ti->ti_seq;
|
---|
997 | if (todrop > 0) {
|
---|
998 | if (tiflags & TH_SYN) {
|
---|
999 | tiflags &= ~TH_SYN;
|
---|
1000 | ti->ti_seq++;
|
---|
1001 | if (ti->ti_urp > 1)
|
---|
1002 | ti->ti_urp--;
|
---|
1003 | else
|
---|
1004 | tiflags &= ~TH_URG;
|
---|
1005 | todrop--;
|
---|
1006 | }
|
---|
1007 | /*
|
---|
1008 | * Following if statement from Stevens, vol. 2, p. 960.
|
---|
1009 | */
|
---|
1010 | if (todrop > ti->ti_len
|
---|
1011 | || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
|
---|
1012 | /*
|
---|
1013 | * Any valid FIN must be to the left of the window.
|
---|
1014 | * At this point the FIN must be a duplicate or out
|
---|
1015 | * of sequence; drop it.
|
---|
1016 | */
|
---|
1017 | tiflags &= ~TH_FIN;
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Send an ACK to resynchronize and drop any data.
|
---|
1021 | * But keep on processing for RST or ACK.
|
---|
1022 | */
|
---|
1023 | tp->t_flags |= TF_ACKNOW;
|
---|
1024 | todrop = ti->ti_len;
|
---|
1025 | tcpstat.tcps_rcvduppack++;
|
---|
1026 | tcpstat.tcps_rcvdupbyte += todrop;
|
---|
1027 | } else {
|
---|
1028 | tcpstat.tcps_rcvpartduppack++;
|
---|
1029 | tcpstat.tcps_rcvpartdupbyte += todrop;
|
---|
1030 | }
|
---|
1031 | m_adj(m, todrop);
|
---|
1032 | ti->ti_seq += todrop;
|
---|
1033 | ti->ti_len -= todrop;
|
---|
1034 | if (ti->ti_urp > todrop)
|
---|
1035 | ti->ti_urp -= todrop;
|
---|
1036 | else {
|
---|
1037 | tiflags &= ~TH_URG;
|
---|
1038 | ti->ti_urp = 0;
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 | /*
|
---|
1042 | * If new data are received on a connection after the
|
---|
1043 | * user processes are gone, then RST the other end.
|
---|
1044 | */
|
---|
1045 | if ((so->so_state & SS_NOFDREF) &&
|
---|
1046 | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
|
---|
1047 | tp = tcp_close(pData, tp);
|
---|
1048 | tcpstat.tcps_rcvafterclose++;
|
---|
1049 | goto dropwithreset;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * If segment ends after window, drop trailing data
|
---|
1054 | * (and PUSH and FIN); if nothing left, just ACK.
|
---|
1055 | */
|
---|
1056 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
|
---|
1057 | if (todrop > 0) {
|
---|
1058 | tcpstat.tcps_rcvpackafterwin++;
|
---|
1059 | if (todrop >= ti->ti_len) {
|
---|
1060 | tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
|
---|
1061 | /*
|
---|
1062 | * If a new connection request is received
|
---|
1063 | * while in TIME_WAIT, drop the old connection
|
---|
1064 | * and start over if the sequence numbers
|
---|
1065 | * are above the previous ones.
|
---|
1066 | */
|
---|
1067 | if (tiflags & TH_SYN &&
|
---|
1068 | tp->t_state == TCPS_TIME_WAIT &&
|
---|
1069 | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
|
---|
1070 | iss = tp->rcv_nxt + TCP_ISSINCR;
|
---|
1071 | tp = tcp_close(pData, tp);
|
---|
1072 | goto findso;
|
---|
1073 | }
|
---|
1074 | /*
|
---|
1075 | * If window is closed can only take segments at
|
---|
1076 | * window edge, and have to drop data and PUSH from
|
---|
1077 | * incoming segments. Continue processing, but
|
---|
1078 | * remember to ack. Otherwise, drop segment
|
---|
1079 | * and ack.
|
---|
1080 | */
|
---|
1081 | if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
|
---|
1082 | tp->t_flags |= TF_ACKNOW;
|
---|
1083 | tcpstat.tcps_rcvwinprobe++;
|
---|
1084 | } else
|
---|
1085 | goto dropafterack;
|
---|
1086 | } else
|
---|
1087 | tcpstat.tcps_rcvbyteafterwin += todrop;
|
---|
1088 | m_adj(m, -todrop);
|
---|
1089 | ti->ti_len -= todrop;
|
---|
1090 | tiflags &= ~(TH_PUSH|TH_FIN);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | /*
|
---|
1094 | * If last ACK falls within this segment's sequence numbers,
|
---|
1095 | * record its timestamp.
|
---|
1096 | */
|
---|
1097 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
|
---|
1098 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
|
---|
1099 | * ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
|
---|
1100 | * tp->ts_recent_age = tcp_now;
|
---|
1101 | * tp->ts_recent = ts_val;
|
---|
1102 | * }
|
---|
1103 | */
|
---|
1104 |
|
---|
1105 | /*
|
---|
1106 | * If the RST bit is set examine the state:
|
---|
1107 | * SYN_RECEIVED STATE:
|
---|
1108 | * If passive open, return to LISTEN state.
|
---|
1109 | * If active open, inform user that connection was refused.
|
---|
1110 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
|
---|
1111 | * Inform user that connection was reset, and close tcb.
|
---|
1112 | * CLOSING, LAST_ACK, TIME_WAIT STATES
|
---|
1113 | * Close the tcb.
|
---|
1114 | */
|
---|
1115 | if (tiflags&TH_RST) switch (tp->t_state) {
|
---|
1116 |
|
---|
1117 | case TCPS_SYN_RECEIVED:
|
---|
1118 | /* so->so_error = ECONNREFUSED; */
|
---|
1119 | goto close;
|
---|
1120 |
|
---|
1121 | case TCPS_ESTABLISHED:
|
---|
1122 | case TCPS_FIN_WAIT_1:
|
---|
1123 | case TCPS_FIN_WAIT_2:
|
---|
1124 | case TCPS_CLOSE_WAIT:
|
---|
1125 | /* so->so_error = ECONNRESET; */
|
---|
1126 | close:
|
---|
1127 | tp->t_state = TCPS_CLOSED;
|
---|
1128 | tcpstat.tcps_drops++;
|
---|
1129 | tp = tcp_close(pData, tp);
|
---|
1130 | goto drop;
|
---|
1131 |
|
---|
1132 | case TCPS_CLOSING:
|
---|
1133 | case TCPS_LAST_ACK:
|
---|
1134 | case TCPS_TIME_WAIT:
|
---|
1135 | tp = tcp_close(pData, tp);
|
---|
1136 | goto drop;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /*
|
---|
1140 | * If a SYN is in the window, then this is an
|
---|
1141 | * error and we send an RST and drop the connection.
|
---|
1142 | */
|
---|
1143 | if (tiflags & TH_SYN) {
|
---|
1144 | tp = tcp_drop(pData, tp,0);
|
---|
1145 | goto dropwithreset;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | /*
|
---|
1149 | * If the ACK bit is off we drop the segment and return.
|
---|
1150 | */
|
---|
1151 | if ((tiflags & TH_ACK) == 0) goto drop;
|
---|
1152 |
|
---|
1153 | /*
|
---|
1154 | * Ack processing.
|
---|
1155 | */
|
---|
1156 | switch (tp->t_state) {
|
---|
1157 | /*
|
---|
1158 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter
|
---|
1159 | * ESTABLISHED state and continue processing, otherwise
|
---|
1160 | * send an RST. una<=ack<=max
|
---|
1161 | */
|
---|
1162 | case TCPS_SYN_RECEIVED:
|
---|
1163 |
|
---|
1164 | if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
|
---|
1165 | SEQ_GT(ti->ti_ack, tp->snd_max))
|
---|
1166 | goto dropwithreset;
|
---|
1167 | tcpstat.tcps_connects++;
|
---|
1168 | tp->t_state = TCPS_ESTABLISHED;
|
---|
1169 | /*
|
---|
1170 | * The sent SYN is ack'ed with our sequence number +1
|
---|
1171 | * The first data byte already in the buffer will get
|
---|
1172 | * lost if no correction is made. This is only needed for
|
---|
1173 | * SS_CTL since the buffer is empty otherwise.
|
---|
1174 | * tp->snd_una++; or:
|
---|
1175 | */
|
---|
1176 | tp->snd_una=ti->ti_ack;
|
---|
1177 | soisfconnected(so);
|
---|
1178 |
|
---|
1179 | /* Do window scaling? */
|
---|
1180 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
|
---|
1181 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
|
---|
1182 | * tp->snd_scale = tp->requested_s_scale;
|
---|
1183 | * tp->rcv_scale = tp->request_r_scale;
|
---|
1184 | * }
|
---|
1185 | */
|
---|
1186 | #ifndef VBOX_WITH_BSD_TCP_REASS
|
---|
1187 | (void) tcp_reass(pData, tp, (struct tcpiphdr *)0, (struct mbuf *)0);
|
---|
1188 | #else /* VBOX_WITH_BSD_TCP_REASS */
|
---|
1189 | (void) tcp_reass(pData, tp, (struct tcphdr *)0, (int *)0, (struct mbuf *)0);
|
---|
1190 | #endif /*VBOX_WITH_BSD_TCP_REASS*/
|
---|
1191 | tp->snd_wl1 = ti->ti_seq - 1;
|
---|
1192 | /* Avoid ack processing; snd_una==ti_ack => dup ack */
|
---|
1193 | goto synrx_to_est;
|
---|
1194 | /* fall into ... */
|
---|
1195 |
|
---|
1196 | /*
|
---|
1197 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
|
---|
1198 | * ACKs. If the ack is in the range
|
---|
1199 | * tp->snd_una < ti->ti_ack <= tp->snd_max
|
---|
1200 | * then advance tp->snd_una to ti->ti_ack and drop
|
---|
1201 | * data from the retransmission queue. If this ACK reflects
|
---|
1202 | * more up to date window information we update our window information.
|
---|
1203 | */
|
---|
1204 | case TCPS_ESTABLISHED:
|
---|
1205 | case TCPS_FIN_WAIT_1:
|
---|
1206 | case TCPS_FIN_WAIT_2:
|
---|
1207 | case TCPS_CLOSE_WAIT:
|
---|
1208 | case TCPS_CLOSING:
|
---|
1209 | case TCPS_LAST_ACK:
|
---|
1210 | case TCPS_TIME_WAIT:
|
---|
1211 |
|
---|
1212 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
|
---|
1213 | if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
|
---|
1214 | tcpstat.tcps_rcvdupack++;
|
---|
1215 | DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n",
|
---|
1216 | (long )m, (long )so));
|
---|
1217 | /*
|
---|
1218 | * If we have outstanding data (other than
|
---|
1219 | * a window probe), this is a completely
|
---|
1220 | * duplicate ack (ie, window info didn't
|
---|
1221 | * change), the ack is the biggest we've
|
---|
1222 | * seen and we've seen exactly our rexmt
|
---|
1223 | * threshold of them, assume a packet
|
---|
1224 | * has been dropped and retransmit it.
|
---|
1225 | * Kludge snd_nxt & the congestion
|
---|
1226 | * window so we send only this one
|
---|
1227 | * packet.
|
---|
1228 | *
|
---|
1229 | * We know we're losing at the current
|
---|
1230 | * window size so do congestion avoidance
|
---|
1231 | * (set ssthresh to half the current window
|
---|
1232 | * and pull our congestion window back to
|
---|
1233 | * the new ssthresh).
|
---|
1234 | *
|
---|
1235 | * Dup acks mean that packets have left the
|
---|
1236 | * network (they're now cached at the receiver)
|
---|
1237 | * so bump cwnd by the amount in the receiver
|
---|
1238 | * to keep a constant cwnd packets in the
|
---|
1239 | * network.
|
---|
1240 | */
|
---|
1241 | if (tp->t_timer[TCPT_REXMT] == 0 ||
|
---|
1242 | ti->ti_ack != tp->snd_una)
|
---|
1243 | tp->t_dupacks = 0;
|
---|
1244 | else if (++tp->t_dupacks == tcprexmtthresh) {
|
---|
1245 | tcp_seq onxt = tp->snd_nxt;
|
---|
1246 | u_int win =
|
---|
1247 | min(tp->snd_wnd, tp->snd_cwnd) / 2 /
|
---|
1248 | tp->t_maxseg;
|
---|
1249 |
|
---|
1250 | if (win < 2)
|
---|
1251 | win = 2;
|
---|
1252 | tp->snd_ssthresh = win * tp->t_maxseg;
|
---|
1253 | tp->t_timer[TCPT_REXMT] = 0;
|
---|
1254 | tp->t_rtt = 0;
|
---|
1255 | tp->snd_nxt = ti->ti_ack;
|
---|
1256 | tp->snd_cwnd = tp->t_maxseg;
|
---|
1257 | (void) tcp_output(pData, tp);
|
---|
1258 | tp->snd_cwnd = tp->snd_ssthresh +
|
---|
1259 | tp->t_maxseg * tp->t_dupacks;
|
---|
1260 | if (SEQ_GT(onxt, tp->snd_nxt))
|
---|
1261 | tp->snd_nxt = onxt;
|
---|
1262 | goto drop;
|
---|
1263 | } else if (tp->t_dupacks > tcprexmtthresh) {
|
---|
1264 | tp->snd_cwnd += tp->t_maxseg;
|
---|
1265 | (void) tcp_output(pData, tp);
|
---|
1266 | goto drop;
|
---|
1267 | }
|
---|
1268 | } else
|
---|
1269 | tp->t_dupacks = 0;
|
---|
1270 | break;
|
---|
1271 | }
|
---|
1272 | synrx_to_est:
|
---|
1273 | /*
|
---|
1274 | * If the congestion window was inflated to account
|
---|
1275 | * for the other side's cached packets, retract it.
|
---|
1276 | */
|
---|
1277 | if (tp->t_dupacks > tcprexmtthresh &&
|
---|
1278 | tp->snd_cwnd > tp->snd_ssthresh)
|
---|
1279 | tp->snd_cwnd = tp->snd_ssthresh;
|
---|
1280 | tp->t_dupacks = 0;
|
---|
1281 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
|
---|
1282 | tcpstat.tcps_rcvacktoomuch++;
|
---|
1283 | goto dropafterack;
|
---|
1284 | }
|
---|
1285 | acked = ti->ti_ack - tp->snd_una;
|
---|
1286 | tcpstat.tcps_rcvackpack++;
|
---|
1287 | tcpstat.tcps_rcvackbyte += acked;
|
---|
1288 |
|
---|
1289 | /*
|
---|
1290 | * If we have a timestamp reply, update smoothed
|
---|
1291 | * round trip time. If no timestamp is present but
|
---|
1292 | * transmit timer is running and timed sequence
|
---|
1293 | * number was acked, update smoothed round trip time.
|
---|
1294 | * Since we now have an rtt measurement, cancel the
|
---|
1295 | * timer backoff (cf., Phil Karn's retransmit alg.).
|
---|
1296 | * Recompute the initial retransmit timer.
|
---|
1297 | */
|
---|
1298 | /* if (ts_present)
|
---|
1299 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
|
---|
1300 | * else
|
---|
1301 | */
|
---|
1302 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
|
---|
1303 | tcp_xmit_timer(pData, tp,tp->t_rtt);
|
---|
1304 |
|
---|
1305 | /*
|
---|
1306 | * If all outstanding data is acked, stop retransmit
|
---|
1307 | * timer and remember to restart (more output or persist).
|
---|
1308 | * If there is more data to be acked, restart retransmit
|
---|
1309 | * timer, using current (possibly backed-off) value.
|
---|
1310 | */
|
---|
1311 | if (ti->ti_ack == tp->snd_max) {
|
---|
1312 | tp->t_timer[TCPT_REXMT] = 0;
|
---|
1313 | needoutput = 1;
|
---|
1314 | } else if (tp->t_timer[TCPT_PERSIST] == 0)
|
---|
1315 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
|
---|
1316 | /*
|
---|
1317 | * When new data is acked, open the congestion window.
|
---|
1318 | * If the window gives us less than ssthresh packets
|
---|
1319 | * in flight, open exponentially (maxseg per packet).
|
---|
1320 | * Otherwise open linearly: maxseg per window
|
---|
1321 | * (maxseg^2 / cwnd per packet).
|
---|
1322 | */
|
---|
1323 | {
|
---|
1324 | register u_int cw = tp->snd_cwnd;
|
---|
1325 | register u_int incr = tp->t_maxseg;
|
---|
1326 |
|
---|
1327 | if (cw > tp->snd_ssthresh)
|
---|
1328 | incr = incr * incr / cw;
|
---|
1329 | tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
|
---|
1330 | }
|
---|
1331 | if (acked > so->so_snd.sb_cc) {
|
---|
1332 | tp->snd_wnd -= so->so_snd.sb_cc;
|
---|
1333 | sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
|
---|
1334 | ourfinisacked = 1;
|
---|
1335 | } else {
|
---|
1336 | sbdrop(&so->so_snd, acked);
|
---|
1337 | tp->snd_wnd -= acked;
|
---|
1338 | ourfinisacked = 0;
|
---|
1339 | }
|
---|
1340 | /*
|
---|
1341 | * XXX sowwakup is called when data is acked and there's room for
|
---|
1342 | * for more data... it should read() the socket
|
---|
1343 | */
|
---|
1344 | /* if (so->so_snd.sb_flags & SB_NOTIFY)
|
---|
1345 | * sowwakeup(so);
|
---|
1346 | */
|
---|
1347 | tp->snd_una = ti->ti_ack;
|
---|
1348 | if (SEQ_LT(tp->snd_nxt, tp->snd_una))
|
---|
1349 | tp->snd_nxt = tp->snd_una;
|
---|
1350 |
|
---|
1351 | switch (tp->t_state) {
|
---|
1352 |
|
---|
1353 | /*
|
---|
1354 | * In FIN_WAIT_1 STATE in addition to the processing
|
---|
1355 | * for the ESTABLISHED state if our FIN is now acknowledged
|
---|
1356 | * then enter FIN_WAIT_2.
|
---|
1357 | */
|
---|
1358 | case TCPS_FIN_WAIT_1:
|
---|
1359 | if (ourfinisacked) {
|
---|
1360 | /*
|
---|
1361 | * If we can't receive any more
|
---|
1362 | * data, then closing user can proceed.
|
---|
1363 | * Starting the timer is contrary to the
|
---|
1364 | * specification, but if we don't get a FIN
|
---|
1365 | * we'll hang forever.
|
---|
1366 | */
|
---|
1367 | if (so->so_state & SS_FCANTRCVMORE) {
|
---|
1368 | soisfdisconnected(so);
|
---|
1369 | tp->t_timer[TCPT_2MSL] = tcp_maxidle;
|
---|
1370 | }
|
---|
1371 | tp->t_state = TCPS_FIN_WAIT_2;
|
---|
1372 | }
|
---|
1373 | break;
|
---|
1374 |
|
---|
1375 | /*
|
---|
1376 | * In CLOSING STATE in addition to the processing for
|
---|
1377 | * the ESTABLISHED state if the ACK acknowledges our FIN
|
---|
1378 | * then enter the TIME-WAIT state, otherwise ignore
|
---|
1379 | * the segment.
|
---|
1380 | */
|
---|
1381 | case TCPS_CLOSING:
|
---|
1382 | if (ourfinisacked) {
|
---|
1383 | tp->t_state = TCPS_TIME_WAIT;
|
---|
1384 | tcp_canceltimers(tp);
|
---|
1385 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
---|
1386 | soisfdisconnected(so);
|
---|
1387 | }
|
---|
1388 | break;
|
---|
1389 |
|
---|
1390 | /*
|
---|
1391 | * In LAST_ACK, we may still be waiting for data to drain
|
---|
1392 | * and/or to be acked, as well as for the ack of our FIN.
|
---|
1393 | * If our FIN is now acknowledged, delete the TCB,
|
---|
1394 | * enter the closed state and return.
|
---|
1395 | */
|
---|
1396 | case TCPS_LAST_ACK:
|
---|
1397 | if (ourfinisacked) {
|
---|
1398 | tp = tcp_close(pData, tp);
|
---|
1399 | goto drop;
|
---|
1400 | }
|
---|
1401 | break;
|
---|
1402 |
|
---|
1403 | /*
|
---|
1404 | * In TIME_WAIT state the only thing that should arrive
|
---|
1405 | * is a retransmission of the remote FIN. Acknowledge
|
---|
1406 | * it and restart the finack timer.
|
---|
1407 | */
|
---|
1408 | case TCPS_TIME_WAIT:
|
---|
1409 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
---|
1410 | goto dropafterack;
|
---|
1411 | }
|
---|
1412 | } /* switch(tp->t_state) */
|
---|
1413 |
|
---|
1414 | step6:
|
---|
1415 | /*
|
---|
1416 | * Update window information.
|
---|
1417 | * Don't look at window if no ACK: TAC's send garbage on first SYN.
|
---|
1418 | */
|
---|
1419 | if ((tiflags & TH_ACK) &&
|
---|
1420 | (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
|
---|
1421 | (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
|
---|
1422 | (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
|
---|
1423 | /* keep track of pure window updates */
|
---|
1424 | if (ti->ti_len == 0 &&
|
---|
1425 | tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
|
---|
1426 | tcpstat.tcps_rcvwinupd++;
|
---|
1427 | tp->snd_wnd = tiwin;
|
---|
1428 | tp->snd_wl1 = ti->ti_seq;
|
---|
1429 | tp->snd_wl2 = ti->ti_ack;
|
---|
1430 | if (tp->snd_wnd > tp->max_sndwnd)
|
---|
1431 | tp->max_sndwnd = tp->snd_wnd;
|
---|
1432 | needoutput = 1;
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | /*
|
---|
1436 | * Process segments with URG.
|
---|
1437 | */
|
---|
1438 | if ((tiflags & TH_URG) && ti->ti_urp &&
|
---|
1439 | TCPS_HAVERCVDFIN(tp->t_state) == 0) {
|
---|
1440 | /*
|
---|
1441 | * This is a kludge, but if we receive and accept
|
---|
1442 | * random urgent pointers, we'll crash in
|
---|
1443 | * soreceive. It's hard to imagine someone
|
---|
1444 | * actually wanting to send this much urgent data.
|
---|
1445 | */
|
---|
1446 | if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
|
---|
1447 | ti->ti_urp = 0;
|
---|
1448 | tiflags &= ~TH_URG;
|
---|
1449 | goto dodata;
|
---|
1450 | }
|
---|
1451 | /*
|
---|
1452 | * If this segment advances the known urgent pointer,
|
---|
1453 | * then mark the data stream. This should not happen
|
---|
1454 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
|
---|
1455 | * a FIN has been received from the remote side.
|
---|
1456 | * In these states we ignore the URG.
|
---|
1457 | *
|
---|
1458 | * According to RFC961 (Assigned Protocols),
|
---|
1459 | * the urgent pointer points to the last octet
|
---|
1460 | * of urgent data. We continue, however,
|
---|
1461 | * to consider it to indicate the first octet
|
---|
1462 | * of data past the urgent section as the original
|
---|
1463 | * spec states (in one of two places).
|
---|
1464 | */
|
---|
1465 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
|
---|
1466 | tp->rcv_up = ti->ti_seq + ti->ti_urp;
|
---|
1467 | so->so_urgc = so->so_rcv.sb_cc +
|
---|
1468 | (tp->rcv_up - tp->rcv_nxt); /* -1; */
|
---|
1469 | tp->rcv_up = ti->ti_seq + ti->ti_urp;
|
---|
1470 |
|
---|
1471 | }
|
---|
1472 | } else
|
---|
1473 | /*
|
---|
1474 | * If no out of band data is expected,
|
---|
1475 | * pull receive urgent pointer along
|
---|
1476 | * with the receive window.
|
---|
1477 | */
|
---|
1478 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
|
---|
1479 | tp->rcv_up = tp->rcv_nxt;
|
---|
1480 | dodata:
|
---|
1481 |
|
---|
1482 | /*
|
---|
1483 | * Process the segment text, merging it into the TCP sequencing queue,
|
---|
1484 | * and arranging for acknowledgment of receipt if necessary.
|
---|
1485 | * This process logically involves adjusting tp->rcv_wnd as data
|
---|
1486 | * is presented to the user (this happens in tcp_usrreq.c,
|
---|
1487 | * case PRU_RCVD). If a FIN has already been received on this
|
---|
1488 | * connection then we just ignore the text.
|
---|
1489 | */
|
---|
1490 | if ((ti->ti_len || (tiflags&TH_FIN)) &&
|
---|
1491 | TCPS_HAVERCVDFIN(tp->t_state) == 0) {
|
---|
1492 | #ifndef VBOX_WITH_BSD_TCP_REASS
|
---|
1493 | TCP_REASS(pData, tp, ti, m, so, tiflags);
|
---|
1494 | #else /* VBOX_WITH_BSD_TCP_REASS */
|
---|
1495 | if (ti->ti_seq == tp->rcv_nxt
|
---|
1496 | && LIST_EMPTY(&tp->t_segq)
|
---|
1497 | && tp->t_state == TCPS_ESTABLISHED) {
|
---|
1498 | DELAY_ACK(tp, ti); /* little bit different from BSD declaration see netinet/tcp_input.c */
|
---|
1499 | tp->rcv_nxt += tlen;
|
---|
1500 | tiflags = ti->ti_t.th_flags & TH_FIN;
|
---|
1501 | tcpstat.tcps_rcvpack++;
|
---|
1502 | tcpstat.tcps_rcvbyte += tlen;
|
---|
1503 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
1504 | m_freem(pData, m);
|
---|
1505 | else
|
---|
1506 | sbappend(pData, so, m);
|
---|
1507 | }
|
---|
1508 | else {
|
---|
1509 | tiflags = tcp_reass(pData, tp, &ti->ti_t, &tlen, m);
|
---|
1510 | tiflags |= TF_ACKNOW;
|
---|
1511 | }
|
---|
1512 | #endif /* VBOX_WITH_BSD_TCP_REASS */
|
---|
1513 | /*
|
---|
1514 | * Note the amount of data that peer has sent into
|
---|
1515 | * our window, in order to estimate the sender's
|
---|
1516 | * buffer size.
|
---|
1517 | */
|
---|
1518 | len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt);
|
---|
1519 | } else {
|
---|
1520 | mbuf_freed = 1; /* The mbuf must be freed, but only when its content is not needed anymore. */
|
---|
1521 | tiflags &= ~TH_FIN;
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | /*
|
---|
1525 | * If FIN is received ACK the FIN and let the user know
|
---|
1526 | * that the connection is closing.
|
---|
1527 | */
|
---|
1528 | if (tiflags & TH_FIN) {
|
---|
1529 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
|
---|
1530 | /*
|
---|
1531 | * If we receive a FIN we can't send more data,
|
---|
1532 | * set it SS_FDRAIN
|
---|
1533 | * Shutdown the socket if there is no rx data in the
|
---|
1534 | * buffer.
|
---|
1535 | * soread() is called on completion of shutdown() and
|
---|
1536 | * will got to TCPS_LAST_ACK, and use tcp_output()
|
---|
1537 | * to send the FIN.
|
---|
1538 | */
|
---|
1539 | /* sofcantrcvmore(so); */
|
---|
1540 | sofwdrain(so);
|
---|
1541 |
|
---|
1542 | tp->t_flags |= TF_ACKNOW;
|
---|
1543 | tp->rcv_nxt++;
|
---|
1544 | }
|
---|
1545 | switch (tp->t_state) {
|
---|
1546 |
|
---|
1547 | /*
|
---|
1548 | * In SYN_RECEIVED and ESTABLISHED STATES
|
---|
1549 | * enter the CLOSE_WAIT state.
|
---|
1550 | */
|
---|
1551 | case TCPS_SYN_RECEIVED:
|
---|
1552 | case TCPS_ESTABLISHED:
|
---|
1553 | if(so->so_emu == EMU_CTL) /* no shutdown on socket */
|
---|
1554 | tp->t_state = TCPS_LAST_ACK;
|
---|
1555 | else
|
---|
1556 | tp->t_state = TCPS_CLOSE_WAIT;
|
---|
1557 | break;
|
---|
1558 |
|
---|
1559 | /*
|
---|
1560 | * If still in FIN_WAIT_1 STATE FIN has not been acked so
|
---|
1561 | * enter the CLOSING state.
|
---|
1562 | */
|
---|
1563 | case TCPS_FIN_WAIT_1:
|
---|
1564 | tp->t_state = TCPS_CLOSING;
|
---|
1565 | break;
|
---|
1566 |
|
---|
1567 | /*
|
---|
1568 | * In FIN_WAIT_2 state enter the TIME_WAIT state,
|
---|
1569 | * starting the time-wait timer, turning off the other
|
---|
1570 | * standard timers.
|
---|
1571 | */
|
---|
1572 | case TCPS_FIN_WAIT_2:
|
---|
1573 | tp->t_state = TCPS_TIME_WAIT;
|
---|
1574 | tcp_canceltimers(tp);
|
---|
1575 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
---|
1576 | soisfdisconnected(so);
|
---|
1577 | break;
|
---|
1578 |
|
---|
1579 | /*
|
---|
1580 | * In TIME_WAIT state restart the 2 MSL time_wait timer.
|
---|
1581 | */
|
---|
1582 | case TCPS_TIME_WAIT:
|
---|
1583 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
|
---|
1584 | break;
|
---|
1585 | }
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | /*
|
---|
1589 | * If this is a small packet, then ACK now - with Nagel
|
---|
1590 | * congestion avoidance sender won't send more until
|
---|
1591 | * he gets an ACK.
|
---|
1592 | *
|
---|
1593 | * See above.
|
---|
1594 | */
|
---|
1595 | /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
|
---|
1596 | */
|
---|
1597 | /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
|
---|
1598 | * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
|
---|
1599 | * ((so->so_iptos & IPTOS_LOWDELAY) &&
|
---|
1600 | * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
|
---|
1601 | */
|
---|
1602 | if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
|
---|
1603 | ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
|
---|
1604 | tp->t_flags |= TF_ACKNOW;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | if (mbuf_freed) {
|
---|
1608 | m_free(pData, m);
|
---|
1609 | }
|
---|
1610 | /*
|
---|
1611 | * Return any desired output.
|
---|
1612 | */
|
---|
1613 | if (needoutput || (tp->t_flags & TF_ACKNOW)) {
|
---|
1614 | (void) tcp_output(pData, tp);
|
---|
1615 | }
|
---|
1616 | return;
|
---|
1617 |
|
---|
1618 | dropafterack:
|
---|
1619 | /*
|
---|
1620 | * Generate an ACK dropping incoming segment if it occupies
|
---|
1621 | * sequence space, where the ACK reflects our state.
|
---|
1622 | */
|
---|
1623 | if (tiflags & TH_RST)
|
---|
1624 | goto drop;
|
---|
1625 | m_freem(pData, m);
|
---|
1626 | tp->t_flags |= TF_ACKNOW;
|
---|
1627 | (void) tcp_output(pData, tp);
|
---|
1628 | return;
|
---|
1629 |
|
---|
1630 | dropwithreset:
|
---|
1631 | /* reuses m if m!=NULL, m_free() unnecessary */
|
---|
1632 | if (tiflags & TH_ACK)
|
---|
1633 | tcp_respond(pData, tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
|
---|
1634 | else {
|
---|
1635 | if (tiflags & TH_SYN) ti->ti_len++;
|
---|
1636 | tcp_respond(pData, tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
|
---|
1637 | TH_RST|TH_ACK);
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | return;
|
---|
1641 |
|
---|
1642 | drop:
|
---|
1643 | /*
|
---|
1644 | * Drop space held by incoming segment and return.
|
---|
1645 | */
|
---|
1646 | m_free(pData, m);
|
---|
1647 |
|
---|
1648 | return;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | /* , ts_present, ts_val, ts_ecr) */
|
---|
1652 | /* int *ts_present;
|
---|
1653 | * u_int32_t *ts_val, *ts_ecr;
|
---|
1654 | */
|
---|
1655 | void
|
---|
1656 | tcp_dooptions(PNATState pData, struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
|
---|
1657 | {
|
---|
1658 | u_int16_t mss;
|
---|
1659 | int opt, optlen;
|
---|
1660 |
|
---|
1661 | DEBUG_CALL("tcp_dooptions");
|
---|
1662 | DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt));
|
---|
1663 |
|
---|
1664 | for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
---|
1665 | opt = cp[0];
|
---|
1666 | if (opt == TCPOPT_EOL)
|
---|
1667 | break;
|
---|
1668 | if (opt == TCPOPT_NOP)
|
---|
1669 | optlen = 1;
|
---|
1670 | else {
|
---|
1671 | optlen = cp[1];
|
---|
1672 | if (optlen <= 0)
|
---|
1673 | break;
|
---|
1674 | }
|
---|
1675 | switch (opt) {
|
---|
1676 |
|
---|
1677 | default:
|
---|
1678 | continue;
|
---|
1679 |
|
---|
1680 | case TCPOPT_MAXSEG:
|
---|
1681 | if (optlen != TCPOLEN_MAXSEG)
|
---|
1682 | continue;
|
---|
1683 | if (!(ti->ti_flags & TH_SYN))
|
---|
1684 | continue;
|
---|
1685 | memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
|
---|
1686 | NTOHS(mss);
|
---|
1687 | (void) tcp_mss(pData, tp, mss); /* sets t_maxseg */
|
---|
1688 | break;
|
---|
1689 |
|
---|
1690 | /* case TCPOPT_WINDOW:
|
---|
1691 | * if (optlen != TCPOLEN_WINDOW)
|
---|
1692 | * continue;
|
---|
1693 | * if (!(ti->ti_flags & TH_SYN))
|
---|
1694 | * continue;
|
---|
1695 | * tp->t_flags |= TF_RCVD_SCALE;
|
---|
1696 | * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
|
---|
1697 | * break;
|
---|
1698 | */
|
---|
1699 | /* case TCPOPT_TIMESTAMP:
|
---|
1700 | * if (optlen != TCPOLEN_TIMESTAMP)
|
---|
1701 | * continue;
|
---|
1702 | * *ts_present = 1;
|
---|
1703 | * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
|
---|
1704 | * NTOHL(*ts_val);
|
---|
1705 | * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
|
---|
1706 | * NTOHL(*ts_ecr);
|
---|
1707 | *
|
---|
1708 | */ /*
|
---|
1709 | * * A timestamp received in a SYN makes
|
---|
1710 | * * it ok to send timestamp requests and replies.
|
---|
1711 | * */
|
---|
1712 | /* if (ti->ti_flags & TH_SYN) {
|
---|
1713 | * tp->t_flags |= TF_RCVD_TSTMP;
|
---|
1714 | * tp->ts_recent = *ts_val;
|
---|
1715 | * tp->ts_recent_age = tcp_now;
|
---|
1716 | * }
|
---|
1717 | */ break;
|
---|
1718 | }
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 |
|
---|
1723 | /*
|
---|
1724 | * Pull out of band byte out of a segment so
|
---|
1725 | * it doesn't appear in the user's data queue.
|
---|
1726 | * It is still reflected in the segment length for
|
---|
1727 | * sequencing purposes.
|
---|
1728 | */
|
---|
1729 |
|
---|
1730 | #ifdef notdef
|
---|
1731 |
|
---|
1732 | void
|
---|
1733 | tcp_pulloutofband(so, ti, m)
|
---|
1734 | struct socket *so;
|
---|
1735 | struct tcpiphdr *ti;
|
---|
1736 | register struct mbuf *m;
|
---|
1737 | {
|
---|
1738 | int cnt = ti->ti_urp - 1;
|
---|
1739 |
|
---|
1740 | while (cnt >= 0) {
|
---|
1741 | if (m->m_len > cnt) {
|
---|
1742 | char *cp = mtod(m, caddr_t) + cnt;
|
---|
1743 | struct tcpcb *tp = sototcpcb(so);
|
---|
1744 |
|
---|
1745 | tp->t_iobc = *cp;
|
---|
1746 | tp->t_oobflags |= TCPOOB_HAVEDATA;
|
---|
1747 | memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
|
---|
1748 | m->m_len--;
|
---|
1749 | return;
|
---|
1750 | }
|
---|
1751 | cnt -= m->m_len;
|
---|
1752 | m = m->m_next; /* XXX WRONG! Fix it! */
|
---|
1753 | if (m == 0)
|
---|
1754 | break;
|
---|
1755 | }
|
---|
1756 | panic("tcp_pulloutofband");
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | #endif /* notdef */
|
---|
1760 |
|
---|
1761 | /*
|
---|
1762 | * Collect new round-trip time estimate
|
---|
1763 | * and update averages and current timeout.
|
---|
1764 | */
|
---|
1765 |
|
---|
1766 | void
|
---|
1767 | tcp_xmit_timer(PNATState pData, register struct tcpcb *tp, int rtt)
|
---|
1768 | {
|
---|
1769 | register short delta;
|
---|
1770 |
|
---|
1771 | DEBUG_CALL("tcp_xmit_timer");
|
---|
1772 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
1773 | DEBUG_ARG("rtt = %d", rtt);
|
---|
1774 |
|
---|
1775 | tcpstat.tcps_rttupdated++;
|
---|
1776 | if (tp->t_srtt != 0) {
|
---|
1777 | /*
|
---|
1778 | * srtt is stored as fixed point with 3 bits after the
|
---|
1779 | * binary point (i.e., scaled by 8). The following magic
|
---|
1780 | * is equivalent to the smoothing algorithm in rfc793 with
|
---|
1781 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
|
---|
1782 | * point). Adjust rtt to origin 0.
|
---|
1783 | */
|
---|
1784 | delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
|
---|
1785 | if ((tp->t_srtt += delta) <= 0)
|
---|
1786 | tp->t_srtt = 1;
|
---|
1787 | /*
|
---|
1788 | * We accumulate a smoothed rtt variance (actually, a
|
---|
1789 | * smoothed mean difference), then set the retransmit
|
---|
1790 | * timer to smoothed rtt + 4 times the smoothed variance.
|
---|
1791 | * rttvar is stored as fixed point with 2 bits after the
|
---|
1792 | * binary point (scaled by 4). The following is
|
---|
1793 | * equivalent to rfc793 smoothing with an alpha of .75
|
---|
1794 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
|
---|
1795 | * rfc793's wired-in beta.
|
---|
1796 | */
|
---|
1797 | if (delta < 0)
|
---|
1798 | delta = -delta;
|
---|
1799 | delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
|
---|
1800 | if ((tp->t_rttvar += delta) <= 0)
|
---|
1801 | tp->t_rttvar = 1;
|
---|
1802 | } else {
|
---|
1803 | /*
|
---|
1804 | * No rtt measurement yet - use the unsmoothed rtt.
|
---|
1805 | * Set the variance to half the rtt (so our first
|
---|
1806 | * retransmit happens at 3*rtt).
|
---|
1807 | */
|
---|
1808 | tp->t_srtt = rtt << TCP_RTT_SHIFT;
|
---|
1809 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
|
---|
1810 | }
|
---|
1811 | tp->t_rtt = 0;
|
---|
1812 | tp->t_rxtshift = 0;
|
---|
1813 |
|
---|
1814 | /*
|
---|
1815 | * the retransmit should happen at rtt + 4 * rttvar.
|
---|
1816 | * Because of the way we do the smoothing, srtt and rttvar
|
---|
1817 | * will each average +1/2 tick of bias. When we compute
|
---|
1818 | * the retransmit timer, we want 1/2 tick of rounding and
|
---|
1819 | * 1 extra tick because of +-1/2 tick uncertainty in the
|
---|
1820 | * firing of the timer. The bias will give us exactly the
|
---|
1821 | * 1.5 tick we need. But, because the bias is
|
---|
1822 | * statistical, we have to test that we don't drop below
|
---|
1823 | * the minimum feasible timer (which is 2 ticks).
|
---|
1824 | */
|
---|
1825 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
|
---|
1826 | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
|
---|
1827 |
|
---|
1828 | /*
|
---|
1829 | * We received an ack for a packet that wasn't retransmitted;
|
---|
1830 | * it is probably safe to discard any error indications we've
|
---|
1831 | * received recently. This isn't quite right, but close enough
|
---|
1832 | * for now (a route might have failed after we sent a segment,
|
---|
1833 | * and the return path might not be symmetrical).
|
---|
1834 | */
|
---|
1835 | tp->t_softerror = 0;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | /*
|
---|
1839 | * Determine a reasonable value for maxseg size.
|
---|
1840 | * If the route is known, check route for mtu.
|
---|
1841 | * If none, use an mss that can be handled on the outgoing
|
---|
1842 | * interface without forcing IP to fragment; if bigger than
|
---|
1843 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
|
---|
1844 | * to utilize large mbufs. If no route is found, route has no mtu,
|
---|
1845 | * or the destination isn't local, use a default, hopefully conservative
|
---|
1846 | * size (usually 512 or the default IP max size, but no more than the mtu
|
---|
1847 | * of the interface), as we can't discover anything about intervening
|
---|
1848 | * gateways or networks. We also initialize the congestion/slow start
|
---|
1849 | * window to be a single segment if the destination isn't local.
|
---|
1850 | * While looking at the routing entry, we also initialize other path-dependent
|
---|
1851 | * parameters from pre-set or cached values in the routing entry.
|
---|
1852 | */
|
---|
1853 |
|
---|
1854 | int
|
---|
1855 | tcp_mss(PNATState pData, register struct tcpcb *tp, u_int offer)
|
---|
1856 | {
|
---|
1857 | struct socket *so = tp->t_socket;
|
---|
1858 | int mss;
|
---|
1859 |
|
---|
1860 | DEBUG_CALL("tcp_mss");
|
---|
1861 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
1862 | DEBUG_ARG("offer = %d", offer);
|
---|
1863 |
|
---|
1864 | mss = min(if_mtu, if_mru) - sizeof(struct tcpiphdr);
|
---|
1865 | if (offer)
|
---|
1866 | mss = min(mss, offer);
|
---|
1867 | mss = max(mss, 32);
|
---|
1868 | if (mss < tp->t_maxseg || offer != 0)
|
---|
1869 | tp->t_maxseg = mss;
|
---|
1870 |
|
---|
1871 | tp->snd_cwnd = mss;
|
---|
1872 |
|
---|
1873 | sbreserve(&so->so_snd, tcp_sndspace+((tcp_sndspace%mss)?(mss-(tcp_sndspace%mss)):0));
|
---|
1874 | sbreserve(&so->so_rcv, tcp_rcvspace+((tcp_rcvspace%mss)?(mss-(tcp_rcvspace%mss)):0));
|
---|
1875 |
|
---|
1876 | DEBUG_MISC((dfd, " returning mss = %d\n", mss));
|
---|
1877 |
|
---|
1878 | return mss;
|
---|
1879 | }
|
---|