VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/tcp_input.c@ 14470

最後變更 在這個檔案從14470是 14470,由 vboxsync 提交於 16 年 前

slirp: remove tabs

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette