revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / tcp_input.c
blob5586cfda5aeaf9e727841443c9ea9fedbf7d7068
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2006 Pavel Fedin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * From: @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
35 * $Id$
38 #ifndef TUBA_INCLUDE
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47 #include <sys/queue.h>
49 #include <net/route.h>
50 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_fsm.h>
59 #include <netinet/tcp_seq.h>
60 #include <netinet/tcp_timer.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/tcpip.h>
63 #ifdef TCPDEBUG
64 #include <netinet/tcp_debug.h>
65 struct tcpiphdr tcp_saveti;
66 #endif
68 int tcprexmtthresh = 3;
69 #if defined(__AROS__)
70 int tcp_iss;
71 int tcp_ccgen;
72 #else
73 tcp_seq tcp_iss;
74 tcp_cc tcp_ccgen;
75 #endif
76 struct tcpstat tcpstat;
77 u_long tcp_now;
78 struct inpcbhead tcb;
79 struct inpcbinfo tcbinfo;
81 #endif /* TUBA_INCLUDE */
83 #include <kern/uipc_socket2_protos.h>
84 //#include <netinet/tcp_subr_protos.h>
87 * Insert segment ti into reassembly queue of tcp with
88 * control block tp. Return TH_FIN if reassembly now includes
89 * a segment with FIN. The macro form does the common case inline
90 * (segment is the next to be received on an established connection,
91 * and the queue is empty), avoiding linkage into and removal
92 * from the queue and repetition of various conversions.
93 * Set DELACK for segments received in order, but ack immediately
94 * when segments are out of order (so fast retransmit can work).
96 #define TCP_REASS(tp, ti, m, so, flags) { \
97 if ((ti)->ti_seq == (tp)->rcv_nxt && \
98 (tp)->t_segq == NULL && \
99 (tp)->t_state == TCPS_ESTABLISHED) { \
100 if (ti->ti_flags & TH_PUSH) \
101 tp->t_flags |= TF_ACKNOW; \
102 else \
103 tp->t_flags |= TF_DELACK; \
104 (tp)->rcv_nxt += (ti)->ti_len; \
105 flags = (ti)->ti_flags & TH_FIN; \
106 tcpstat.tcps_rcvpack++;\
107 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
108 sbappend(&(so)->so_rcv, (m)); \
109 sorwakeup(so); \
110 } else { \
111 (flags) = tcp_reass((tp), (ti), (m)); \
112 tp->t_flags |= TF_ACKNOW; \
117 tcp_reass(tp, ti, m)
118 struct tcpcb *tp;
119 struct tcpiphdr *ti;
120 struct mbuf *m;
122 struct mbuf *q;
123 struct mbuf *p;
124 struct mbuf *nq;
125 struct socket *so = tp->t_inpcb->inp_socket;
126 int flags;
128 #define GETTCP(m) ((struct tcpiphdr *)m->m_pkthdr.header)
131 * Call with ti==0 after become established to
132 * force pre-ESTABLISHED data up to user socket.
134 if (ti == 0)
135 goto present;
137 m->m_pkthdr.header = (caddr_t)ti;
140 * Find a segment which begins after this one does.
142 for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt)
143 if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq))
144 break;
147 * If there is a preceding segment, it may provide some of
148 * our data already. If so, drop the data from the incoming
149 * segment. If it provides all of our data, drop us.
151 if (p != NULL) {
152 register int i;
153 /* conversion to int (in i) handles seq wraparound */
154 i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq;
155 if (i > 0) {
156 if (i >= ti->ti_len) {
157 tcpstat.tcps_rcvduppack++;
158 tcpstat.tcps_rcvdupbyte += ti->ti_len;
159 m_freem(m);
161 * Try to present any queued data
162 * at the left window edge to the user.
163 * This is needed after the 3-WHS
164 * completes.
166 goto present; /* ??? */
168 m_adj(m, i);
169 ti->ti_len -= i;
170 ti->ti_seq += i;
173 tcpstat.tcps_rcvoopack++;
174 tcpstat.tcps_rcvoobyte += ti->ti_len;
177 * While we overlap succeeding segments trim them or,
178 * if they are completely covered, dequeue them.
180 while (q) {
181 register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq;
182 if (i <= 0)
183 break;
184 if (i < GETTCP(q)->ti_len) {
185 GETTCP(q)->ti_seq += i;
186 GETTCP(q)->ti_len -= i;
187 m_adj(q, i);
188 break;
191 nq = q->m_nextpkt;
192 if (p)
193 p->m_nextpkt = nq;
194 else
195 tp->t_segq = nq;
196 m_freem(q);
197 q = nq;
200 if (p == NULL) {
201 m->m_nextpkt = tp->t_segq;
202 tp->t_segq = m;
203 } else {
204 m->m_nextpkt = p->m_nextpkt;
205 p->m_nextpkt = m;
208 present:
210 * Present data to user, advancing rcv_nxt through
211 * completed sequence space.
213 if (!TCPS_HAVEESTABLISHED(tp->t_state))
214 return (0);
215 q = tp->t_segq;
216 if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt)
217 return (0);
218 do {
219 tp->rcv_nxt += GETTCP(q)->ti_len;
220 flags = GETTCP(q)->ti_flags & TH_FIN;
221 nq = q->m_nextpkt;
222 tp->t_segq = nq;
223 q->m_nextpkt = NULL;
224 if (so->so_state & SS_CANTRCVMORE)
225 m_freem(q);
226 else
227 sbappend(&so->so_rcv, q);
228 q = nq;
229 } while (q && GETTCP(q)->ti_seq == tp->rcv_nxt);
230 sorwakeup(so);
231 return (flags);
233 #undef GETTCP
237 * TCP input routine, follows pages 65-76 of the
238 * protocol specification dated September, 1981 very closely.
240 void tcp_input(void *arg, ...)
242 register struct mbuf *m = arg;
243 int iphlen;
244 register struct tcpiphdr *ti;
245 register struct inpcb *inp;
246 caddr_t optp = NULL;
247 int optlen = 0;
248 int len, tlen, off;
249 register struct tcpcb *tp = 0;
250 register int tiflags;
251 struct socket *so = 0;
252 int todrop, acked, ourfinisacked, needoutput = 0;
253 struct in_addr laddr;
254 int dropsocket = 0;
255 int iss = 0;
256 u_long tiwin;
257 struct tcpopt to; /* options in this segment */
258 struct rmxp_tao *taop; /* pointer to our TAO cache entry */
259 struct rmxp_tao tao_noncached; /* in case there's no cached entry */
260 #ifdef TCPDEBUG
261 short ostate = 0;
262 #endif
263 va_list va;
265 va_start(va, arg);
266 iphlen = va_arg(va, int);
267 va_end(va);
270 bzero((char *)&to, sizeof(to));
272 tcpstat.tcps_rcvtotal++;
274 * Get IP and TCP header together in first mbuf.
275 * Note: IP leaves IP header in first mbuf.
277 ti = mtod(m, struct tcpiphdr *);
278 if (iphlen > sizeof (struct ip))
279 ip_stripoptions(m, (struct mbuf *)0);
280 if (m->m_len < sizeof (struct tcpiphdr)) {
281 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
282 tcpstat.tcps_rcvshort++;
283 return;
285 ti = mtod(m, struct tcpiphdr *);
289 * Checksum extended TCP header and data.
291 tlen = ((struct ip *)ti)->ip_len;
292 len = sizeof (struct ip) + tlen;
293 bzero(ti->ti_x1, sizeof(ti->ti_x1));
294 ti->ti_len = (u_short)tlen;
295 HTONS(ti->ti_len);
296 ti->ti_sum = in_cksum(m, len);
297 if (ti->ti_sum) {
298 tcpstat.tcps_rcvbadsum++;
299 goto drop;
303 * Check that TCP offset makes sense,
304 * pull out TCP options and adjust length. XXX
306 off = ti->ti_off << 2;
307 if (off < sizeof (struct tcphdr) || off > tlen) {
308 tcpstat.tcps_rcvbadoff++;
309 goto drop;
311 tlen -= off;
312 ti->ti_len = tlen;
313 if (off > sizeof (struct tcphdr)) {
314 if (m->m_len < sizeof(struct ip) + off) {
315 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
316 tcpstat.tcps_rcvshort++;
317 return;
319 ti = mtod(m, struct tcpiphdr *);
321 optlen = off - sizeof (struct tcphdr);
322 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
324 * Do quick retrieval of timestamp options ("options
325 * prediction?"). If timestamp is the only option and it's
326 * formatted as recommended in RFC 1323 appendix A, we
327 * quickly get the values now and not bother calling
328 * tcp_dooptions(), etc.
330 if ((optlen == TCPOLEN_TSTAMP_APPA ||
331 (optlen > TCPOLEN_TSTAMP_APPA &&
332 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
333 *(u_long *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
334 (ti->ti_flags & TH_SYN) == 0) {
335 to.to_flag |= TOF_TS;
336 to.to_tsval = ntohl(*(u_long *)(optp + 4));
337 to.to_tsecr = ntohl(*(u_long *)(optp + 8));
338 optp = NULL; /* we've parsed the options */
341 tiflags = ti->ti_flags;
344 * Convert TCP protocol specific fields to host format.
346 NTOHL(ti->ti_seq);
347 NTOHL(ti->ti_ack);
348 NTOHS(ti->ti_win);
349 NTOHS(ti->ti_urp);
352 * Drop TCP, IP headers and TCP options.
354 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
355 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
358 * Locate pcb for segment.
360 findpcb:
362 * First look for an exact match.
364 inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport,
365 ti->ti_dst, ti->ti_dport);
367 * ...and if that fails, do a wildcard search.
369 if (inp == NULL) {
370 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
371 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
375 * If the state is CLOSED (i.e., TCB does not exist) then
376 * all data in the incoming segment is discarded.
377 * If the TCB exists but is in CLOSED state, it is embryonic,
378 * but should either do a listen or a connect soon.
380 if (inp == NULL)
381 goto dropwithreset;
382 tp = intotcpcb(inp);
383 if (tp == 0)
384 goto dropwithreset;
385 if (tp->t_state == TCPS_CLOSED)
386 goto drop;
388 /* Unscale the window into a 32-bit value. */
389 if ((tiflags & TH_SYN) == 0)
390 tiwin = ti->ti_win << tp->snd_scale;
391 else
392 tiwin = ti->ti_win;
394 so = inp->inp_socket;
395 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
396 #ifdef TCPDEBUG
397 if (so->so_options & SO_DEBUG) {
398 ostate = tp->t_state;
399 tcp_saveti = *ti;
401 #endif
402 if (so->so_options & SO_ACCEPTCONN) {
403 register struct tcpcb *tp0 = tp;
405 * call to access control.. (AmiTCP/IP extra)
407 if (controlaccess(ti->ti_src, ti->ti_dport) == 0)
408 goto dropwithreset;
409 so = sonewconn(so, 0);
410 if (so == 0)
411 goto drop;
413 * This is ugly, but ....
415 * Mark socket as temporary until we're
416 * committed to keeping it. The code at
417 * ``drop'' and ``dropwithreset'' check the
418 * flag dropsocket to see if the temporary
419 * socket created here should be discarded.
420 * We mark the socket as discardable until
421 * we're committed to it below in TCPS_LISTEN.
423 dropsocket++;
424 inp = (struct inpcb *)so->so_pcb;
425 inp->inp_laddr = ti->ti_dst;
426 inp->inp_lport = ti->ti_dport;
427 in_pcbrehash(inp);
428 #if BSD>=43
429 inp->inp_options = ip_srcroute();
430 #endif
431 tp = intotcpcb(inp);
432 tp->t_state = TCPS_LISTEN;
433 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
435 /* Compute proper scaling value from buffer space */
436 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
437 TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
438 tp->request_r_scale++;
443 * Segment received on connection.
444 * Reset idle time and keep-alive timer.
446 tp->t_idle = 0;
447 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
450 * Process options if not in LISTEN state,
451 * else do it below (after getting remote address).
453 if (optp && tp->t_state != TCPS_LISTEN)
454 tcp_dooptions(tp, optp, optlen, ti,
455 &to);
458 * Header prediction: check for the two common cases
459 * of a uni-directional data xfer. If the packet has
460 * no control flags, is in-sequence, the window didn't
461 * change and we're not retransmitting, it's a
462 * candidate. If the length is zero and the ack moved
463 * forward, we're the sender side of the xfer. Just
464 * free the data acked & wake any higher level process
465 * that was blocked waiting for space. If the length
466 * is non-zero and the ack didn't move, we're the
467 * receiver side. If we're getting packets in-order
468 * (the reassembly queue is empty), add the data to
469 * the socket buffer and note that we need a delayed ack.
470 * Make sure that the hidden state-flags are also off.
471 * Since we check for TCPS_ESTABLISHED above, it can only
472 * be TH_NEEDSYN.
474 if (tp->t_state == TCPS_ESTABLISHED &&
475 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
476 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
477 ((to.to_flag & TOF_TS) == 0 ||
478 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
480 * Using the CC option is compulsory if once started:
481 * the segment is OK if no T/TCP was negotiated or
482 * if the segment has a CC option equal to CCrecv
484 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
485 (to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv) &&
486 ti->ti_seq == tp->rcv_nxt &&
487 tiwin && tiwin == tp->snd_wnd &&
488 tp->snd_nxt == tp->snd_max) {
491 * If last ACK falls within this segment's sequence numbers,
492 * record the timestamp.
493 * NOTE that the test is modified according to the latest
494 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
496 if ((to.to_flag & TOF_TS) != 0 &&
497 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
498 tp->ts_recent_age = tcp_now;
499 tp->ts_recent = to.to_tsval;
502 if (ti->ti_len == 0) {
503 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
504 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
505 tp->snd_cwnd >= tp->snd_wnd) {
507 * this is a pure ack for outstanding data.
509 ++tcpstat.tcps_predack;
510 if ((to.to_flag & TOF_TS) != 0)
511 tcp_xmit_timer(tp,
512 tcp_now - to.to_tsecr + 1);
513 else if (tp->t_rtt &&
514 SEQ_GT(ti->ti_ack, tp->t_rtseq))
515 tcp_xmit_timer(tp, tp->t_rtt);
516 acked = ti->ti_ack - tp->snd_una;
517 tcpstat.tcps_rcvackpack++;
518 tcpstat.tcps_rcvackbyte += acked;
519 sbdrop(&so->so_snd, acked);
520 tp->snd_una = ti->ti_ack;
521 m_freem(m);
524 * If all outstanding data are acked, stop
525 * retransmit timer, otherwise restart timer
526 * using current (possibly backed-off) value.
527 * If process is waiting for space,
528 * wakeup/selwakeup/signal. If data
529 * are ready to send, let tcp_output
530 * decide between more output or persist.
532 if (tp->snd_una == tp->snd_max)
533 tp->t_timer[TCPT_REXMT] = 0;
534 else if (tp->t_timer[TCPT_PERSIST] == 0)
535 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
537 if (so->so_snd.sb_flags & SB_NOTIFY)
538 sowwakeup(so);
539 if (so->so_snd.sb_cc)
540 (void) tcp_output(tp);
541 return;
543 } else if (ti->ti_ack == tp->snd_una &&
544 tp->t_segq == NULL &&
545 ti->ti_len <= sbspace(&so->so_rcv)) {
547 * this is a pure, in-sequence data packet
548 * with nothing on the reassembly queue and
549 * we have enough buffer space to take it.
551 ++tcpstat.tcps_preddat;
552 tp->rcv_nxt += ti->ti_len;
553 tcpstat.tcps_rcvpack++;
554 tcpstat.tcps_rcvbyte += ti->ti_len;
556 * Add data to socket buffer.
558 sbappend(&so->so_rcv, m);
559 sorwakeup(so);
560 #ifdef TCP_ACK_HACK
562 * If this is a short packet, then ACK now - with Nagel
563 * congestion avoidance sender won't send more until
564 * he gets an ACK.
566 if (tiflags & TH_PUSH) {
567 tp->t_flags |= TF_ACKNOW;
568 tcp_output(tp);
569 } else {
570 tp->t_flags |= TF_DELACK;
572 #else
573 tp->t_flags |= TF_DELACK;
574 #endif
575 return;
580 * Calculate amount of space in receive window,
581 * and then do TCP input processing.
582 * Receive window is amount of space in rcv queue,
583 * but not less than advertised window.
585 { int win;
587 win = sbspace(&so->so_rcv);
588 if (win < 0)
589 win = 0;
590 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
593 switch (tp->t_state) {
596 * If the state is LISTEN then ignore segment if it contains an RST.
597 * If the segment contains an ACK then it is bad and send a RST.
598 * If it does not contain a SYN then it is not interesting; drop it.
599 * Don't bother responding if the destination was a broadcast.
600 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
601 * tp->iss, and send a segment:
602 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
603 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
604 * Fill in remote peer address fields if not previously specified.
605 * Enter SYN_RECEIVED state, and process any other fields of this
606 * segment in this state.
608 case TCPS_LISTEN: {
609 struct mbuf *am;
610 register struct sockaddr_in *sin;
612 if (tiflags & TH_RST)
613 goto drop;
614 if (tiflags & TH_ACK)
615 goto dropwithreset;
616 if ((tiflags & TH_SYN) == 0)
617 goto drop;
619 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
620 * in_broadcast() should never return true on a received
621 * packet with M_BCAST not set.
623 if (m->m_flags & (M_BCAST|M_MCAST) ||
624 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
625 goto drop;
626 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
627 if (am == NULL)
628 goto drop;
629 am->m_len = sizeof (struct sockaddr_in);
630 sin = mtod(am, struct sockaddr_in *);
631 sin->sin_family = AF_INET;
632 sin->sin_len = sizeof(*sin);
633 sin->sin_addr = ti->ti_src;
634 sin->sin_port = ti->ti_sport;
635 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
636 laddr = inp->inp_laddr;
637 if (inp->inp_laddr.s_addr == INADDR_ANY)
638 inp->inp_laddr = ti->ti_dst;
639 if (in_pcbconnect(inp, am)) {
640 inp->inp_laddr = laddr;
641 (void) m_free(am);
642 goto drop;
644 (void) m_free(am);
645 tp->t_template = tcp_template(tp);
646 if (tp->t_template == 0) {
647 tp = tcp_drop(tp, ENOBUFS);
648 dropsocket = 0; /* socket is already gone */
649 goto drop;
651 if ((taop = tcp_gettaocache(inp)) == NULL) {
652 taop = &tao_noncached;
653 bzero(taop, sizeof(*taop));
655 if (optp)
656 tcp_dooptions(tp, optp, optlen, ti,
657 &to);
658 if (iss)
659 tp->iss = iss;
660 else
661 tp->iss = tcp_iss;
662 tcp_iss += TCP_ISSINCR/2;
663 tp->irs = ti->ti_seq;
664 tcp_sendseqinit(tp);
665 tcp_rcvseqinit(tp);
667 * Initialization of the tcpcb for transaction;
668 * set SND.WND = SEG.WND,
669 * initialize CCsend and CCrecv.
671 tp->snd_wnd = tiwin; /* initial send-window */
672 tp->cc_send = CC_INC(tcp_ccgen);
673 tp->cc_recv = to.to_cc;
675 * Perform TAO test on incoming CC (SEG.CC) option, if any.
676 * - compare SEG.CC against cached CC from the same host,
677 * if any.
678 * - if SEG.CC > chached value, SYN must be new and is accepted
679 * immediately: save new CC in the cache, mark the socket
680 * connected, enter ESTABLISHED state, turn on flag to
681 * send a SYN in the next segment.
682 * A virtual advertised window is set in rcv_adv to
683 * initialize SWS prevention. Then enter normal segment
684 * processing: drop SYN, process data and FIN.
685 * - otherwise do a normal 3-way handshake.
687 if ((to.to_flag & TOF_CC) != 0) {
688 if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
689 taop->tao_cc = to.to_cc;
690 tp->t_state = TCPS_ESTABLISHED;
693 * If there is a FIN, or if there is data and the
694 * connection is local, then delay SYN,ACK(SYN) in
695 * the hope of piggy-backing it on a response
696 * segment. Otherwise must send ACK now in case
697 * the other side is slow starting.
699 if ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
700 in_localaddr(inp->inp_faddr)))
701 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
702 else
703 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
704 tp->rcv_adv += tp->rcv_wnd;
705 tcpstat.tcps_connects++;
706 soisconnected(so);
707 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
708 dropsocket = 0; /* committed to socket */
709 tcpstat.tcps_accepts++;
710 goto trimthenstep6;
712 /* else do standard 3-way handshake */
713 } else {
715 * No CC option, but maybe CC.NEW:
716 * invalidate cached value.
718 taop->tao_cc = 0;
721 * TAO test failed or there was no CC option,
722 * do a standard 3-way handshake.
724 tp->t_flags |= TF_ACKNOW;
725 tp->t_state = TCPS_SYN_RECEIVED;
726 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
727 dropsocket = 0; /* committed to socket */
728 tcpstat.tcps_accepts++;
729 goto trimthenstep6;
733 * If the state is SYN_SENT:
734 * if seg contains an ACK, but not for our SYN, drop the input.
735 * if seg contains a RST, then drop the connection.
736 * if seg does not contain SYN, then drop it.
737 * Otherwise this is an acceptable SYN segment
738 * initialize tp->rcv_nxt and tp->irs
739 * if seg contains ack then advance tp->snd_una
740 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
741 * arrange for segment to be acked (eventually)
742 * continue processing rest of data/controls, beginning with URG
744 case TCPS_SYN_SENT:
745 if ((taop = tcp_gettaocache(inp)) == NULL) {
746 taop = &tao_noncached;
747 bzero(taop, sizeof(*taop));
750 if ((tiflags & TH_ACK) &&
751 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
752 SEQ_GT(ti->ti_ack, tp->snd_max))) {
754 * If we have a cached CCsent for the remote host,
755 * hence we haven't just crashed and restarted,
756 * do not send a RST. This may be a retransmission
757 * from the other side after our earlier ACK was lost.
758 * Our new SYN, when it arrives, will serve as the
759 * needed ACK.
761 if (taop->tao_ccsent != 0)
762 goto drop;
763 else
764 goto dropwithreset;
766 if (tiflags & TH_RST) {
767 if (tiflags & TH_ACK)
768 tp = tcp_drop(tp, ECONNREFUSED);
769 goto drop;
771 if ((tiflags & TH_SYN) == 0)
772 goto drop;
773 tp->snd_wnd = ti->ti_win; /* initial send window */
774 tp->cc_recv = to.to_cc; /* foreign CC */
776 tp->irs = ti->ti_seq;
777 tcp_rcvseqinit(tp);
778 if (tiflags & TH_ACK && SEQ_GT(ti->ti_ack, tp->iss)) {
779 tcpstat.tcps_connects++;
780 soisconnected(so);
781 /* Do window scaling on this connection? */
782 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
783 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
784 tp->snd_scale = tp->requested_s_scale;
785 tp->rcv_scale = tp->request_r_scale;
788 * Our SYN was acked. If segment contains CC.ECHO
789 * option, check it to make sure this segment really
790 * matches our SYN. If not, just drop it as old
791 * duplicate, but send an RST if we're still playing
792 * by the old rules.
794 if ((to.to_flag & TOF_CCECHO) &&
795 tp->cc_send != to.to_ccecho) {
796 if (taop->tao_ccsent != 0)
797 goto drop;
798 else
799 goto dropwithreset;
801 /* Segment is acceptable, update cache if undefined. */
802 if (taop->tao_ccsent == 0)
803 taop->tao_ccsent = to.to_ccecho;
805 tp->rcv_adv += tp->rcv_wnd;
806 tp->snd_una++; /* SYN is acked */
808 * If there's data, delay ACK; if there's also a FIN
809 * ACKNOW will be turned on later.
811 if (ti->ti_len != 0)
812 tp->t_flags |= TF_DELACK;
813 else
814 tp->t_flags |= TF_ACKNOW;
816 * Received <SYN,ACK> in SYN_SENT[*] state.
817 * Transitions:
818 * SYN_SENT --> ESTABLISHED
819 * SYN_SENT* --> FIN_WAIT_1
821 if (tp->t_flags & TF_NEEDFIN) {
822 tp->t_state = TCPS_FIN_WAIT_1;
823 tp->t_flags &= ~TF_NEEDFIN;
824 tiflags &= ~TH_SYN;
825 } else
826 tp->t_state = TCPS_ESTABLISHED;
828 } else {
830 * Received initial SYN in SYN-SENT[*] state => simul-
831 * taneous open. If segment contains CC option and there is
832 * a cached CC, apply TAO test; if it succeeds, connection is
833 * half-synchronized. Otherwise, do 3-way handshake:
834 * SYN-SENT -> SYN-RECEIVED
835 * SYN-SENT* -> SYN-RECEIVED*
836 * If there was no CC option, clear cached CC value.
838 tp->t_flags |= TF_ACKNOW;
839 tp->t_timer[TCPT_REXMT] = 0;
840 if (to.to_flag & TOF_CC) {
841 if (taop->tao_cc != 0 &&
842 CC_GT(to.to_cc, taop->tao_cc)) {
844 * update cache and make transition:
845 * SYN-SENT -> ESTABLISHED*
846 * SYN-SENT* -> FIN-WAIT-1*
848 taop->tao_cc = to.to_cc;
849 if (tp->t_flags & TF_NEEDFIN) {
850 tp->t_state = TCPS_FIN_WAIT_1;
851 tp->t_flags &= ~TF_NEEDFIN;
852 } else
853 tp->t_state = TCPS_ESTABLISHED;
854 tp->t_flags |= TF_NEEDSYN;
855 } else
856 tp->t_state = TCPS_SYN_RECEIVED;
857 } else {
858 /* CC.NEW or no option => invalidate cache */
859 taop->tao_cc = 0;
860 tp->t_state = TCPS_SYN_RECEIVED;
864 trimthenstep6:
866 * Advance ti->ti_seq to correspond to first data byte.
867 * If data, trim to stay within window,
868 * dropping FIN if necessary.
870 ti->ti_seq++;
871 if (ti->ti_len > tp->rcv_wnd) {
872 todrop = ti->ti_len - tp->rcv_wnd;
873 m_adj(m, -todrop);
874 ti->ti_len = tp->rcv_wnd;
875 tiflags &= ~TH_FIN;
876 tcpstat.tcps_rcvpackafterwin++;
877 tcpstat.tcps_rcvbyteafterwin += todrop;
879 tp->snd_wl1 = ti->ti_seq - 1;
880 tp->rcv_up = ti->ti_seq;
882 * Client side of transaction: already sent SYN and data.
883 * If the remote host used T/TCP to validate the SYN,
884 * our data will be ACK'd; if so, enter normal data segment
885 * processing in the middle of step 5, ack processing.
886 * Otherwise, goto step 6.
888 if (tiflags & TH_ACK)
889 goto process_ACK;
890 goto step6;
892 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
893 * if segment contains a SYN and CC [not CC.NEW] option:
894 * if state == TIME_WAIT and connection duration > MSL,
895 * drop packet and send RST;
897 * if SEG.CC > CCrecv then is new SYN, and can implicitly
898 * ack the FIN (and data) in retransmission queue.
899 * Complete close and delete TCPCB. Then reprocess
900 * segment, hoping to find new TCPCB in LISTEN state;
902 * else must be old SYN; drop it.
903 * else do normal processing.
905 case TCPS_LAST_ACK:
906 case TCPS_CLOSING:
907 case TCPS_TIME_WAIT:
908 if ((tiflags & TH_SYN) &&
909 (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
910 if (tp->t_state == TCPS_TIME_WAIT &&
911 tp->t_duration > TCPTV_MSL)
912 goto dropwithreset;
913 if (CC_GT(to.to_cc, tp->cc_recv)) {
914 tp = tcp_close(tp);
915 goto findpcb;
917 else
918 goto drop;
920 break; /* continue normal processing */
924 * States other than LISTEN or SYN_SENT.
925 * First check timestamp, if present.
926 * Then check the connection count, if present.
927 * Then check that at least some bytes of segment are within
928 * receive window. If segment begins before rcv_nxt,
929 * drop leading data (and SYN); if nothing left, just ack.
931 * RFC 1323 PAWS: If we have a timestamp reply on this segment
932 * and it's less than ts_recent, drop it.
934 if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 &&
935 tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) {
937 /* Check to see if ts_recent is over 24 days old. */
938 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
940 * Invalidate ts_recent. If this segment updates
941 * ts_recent, the age will be reset later and ts_recent
942 * will get a valid value. If it does not, setting
943 * ts_recent to zero will at least satisfy the
944 * requirement that zero be placed in the timestamp
945 * echo reply when ts_recent isn't valid. The
946 * age isn't reset until we get a valid ts_recent
947 * because we don't want out-of-order segments to be
948 * dropped when ts_recent is old.
950 tp->ts_recent = 0;
951 } else {
952 tcpstat.tcps_rcvduppack++;
953 tcpstat.tcps_rcvdupbyte += ti->ti_len;
954 tcpstat.tcps_pawsdrop++;
955 goto dropafterack;
960 * T/TCP mechanism
961 * If T/TCP was negotiated and the segment doesn't have CC,
962 * or if it's CC is wrong then drop the segment.
963 * RST segments do not have to comply with this.
965 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
966 ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) &&
967 (tiflags & TH_RST) == 0)
968 goto dropafterack;
970 todrop = tp->rcv_nxt - ti->ti_seq;
971 if (todrop > 0) {
972 if (tiflags & TH_SYN) {
973 tiflags &= ~TH_SYN;
974 ti->ti_seq++;
975 if (ti->ti_urp > 1)
976 ti->ti_urp--;
977 else
978 tiflags &= ~TH_URG;
979 todrop--;
982 * Following if statement from Stevens, vol. 2, p. 960.
984 if (todrop > ti->ti_len
985 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
987 * Any valid FIN must be to the left of the window.
988 * At this point the FIN must be a duplicate or out
989 * of sequence; drop it.
991 tiflags &= ~TH_FIN;
994 * Send an ACK to resynchronize and drop any data.
995 * But keep on processing for RST or ACK.
997 tp->t_flags |= TF_ACKNOW;
998 todrop = ti->ti_len;
999 tcpstat.tcps_rcvduppack++;
1000 tcpstat.tcps_rcvdupbyte += todrop;
1001 } else {
1002 tcpstat.tcps_rcvpartduppack++;
1003 tcpstat.tcps_rcvpartdupbyte += todrop;
1005 m_adj(m, todrop);
1006 ti->ti_seq += todrop;
1007 ti->ti_len -= todrop;
1008 if (ti->ti_urp > todrop)
1009 ti->ti_urp -= todrop;
1010 else {
1011 tiflags &= ~TH_URG;
1012 ti->ti_urp = 0;
1017 * If new data are received on a connection after the
1018 * user processes are gone, then RST the other end.
1020 if ((so->so_state & SS_NOFDREF) &&
1021 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1022 tp = tcp_close(tp);
1023 tcpstat.tcps_rcvafterclose++;
1024 goto dropwithreset;
1028 * If segment ends after window, drop trailing data
1029 * (and PUSH and FIN); if nothing left, just ACK.
1031 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1032 if (todrop > 0) {
1033 tcpstat.tcps_rcvpackafterwin++;
1034 if (todrop >= ti->ti_len) {
1035 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1037 * If a new connection request is received
1038 * while in TIME_WAIT, drop the old connection
1039 * and start over if the sequence numbers
1040 * are above the previous ones.
1042 if (tiflags & TH_SYN &&
1043 tp->t_state == TCPS_TIME_WAIT &&
1044 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1045 iss = tp->rcv_nxt + TCP_ISSINCR;
1046 tp = tcp_close(tp);
1047 goto findpcb;
1050 * If window is closed can only take segments at
1051 * window edge, and have to drop data and PUSH from
1052 * incoming segments. Continue processing, but
1053 * remember to ack. Otherwise, drop segment
1054 * and ack.
1056 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1057 tp->t_flags |= TF_ACKNOW;
1058 tcpstat.tcps_rcvwinprobe++;
1059 } else
1060 goto dropafterack;
1061 } else
1062 tcpstat.tcps_rcvbyteafterwin += todrop;
1063 m_adj(m, -todrop);
1064 ti->ti_len -= todrop;
1065 tiflags &= ~(TH_PUSH|TH_FIN);
1069 * If last ACK falls within this segment's sequence numbers,
1070 * record its timestamp.
1071 * NOTE that the test is modified according to the latest
1072 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1074 if ((to.to_flag & TOF_TS) != 0 &&
1075 SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1076 tp->ts_recent_age = tcp_now;
1077 tp->ts_recent = to.to_tsval;
1081 * If the RST bit is set examine the state:
1082 * SYN_RECEIVED STATE:
1083 * If passive open, return to LISTEN state.
1084 * If active open, inform user that connection was refused.
1085 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1086 * Inform user that connection was reset, and close tcb.
1087 * CLOSING, LAST_ACK, TIME_WAIT STATES
1088 * Close the tcb.
1090 if (tiflags&TH_RST) switch (tp->t_state) {
1092 case TCPS_SYN_RECEIVED:
1093 so->so_error = ECONNREFUSED;
1094 goto close;
1096 case TCPS_ESTABLISHED:
1097 case TCPS_FIN_WAIT_1:
1098 case TCPS_FIN_WAIT_2:
1099 case TCPS_CLOSE_WAIT:
1100 so->so_error = ECONNRESET;
1101 close:
1102 tp->t_state = TCPS_CLOSED;
1103 tcpstat.tcps_drops++;
1104 tp = tcp_close(tp);
1105 goto drop;
1107 case TCPS_CLOSING:
1108 case TCPS_LAST_ACK:
1109 case TCPS_TIME_WAIT:
1110 tp = tcp_close(tp);
1111 goto drop;
1115 * If a SYN is in the window, then this is an
1116 * error and we send an RST and drop the connection.
1118 if (tiflags & TH_SYN) {
1119 tp = tcp_drop(tp, ECONNRESET);
1120 goto dropwithreset;
1124 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1125 * flag is on (half-synchronized state), then queue data for
1126 * later processing; else drop segment and return.
1128 if ((tiflags & TH_ACK) == 0) {
1129 if (tp->t_state == TCPS_SYN_RECEIVED ||
1130 (tp->t_flags & TF_NEEDSYN))
1131 goto step6;
1132 else
1133 goto drop;
1137 * Ack processing.
1139 switch (tp->t_state) {
1142 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1143 * ESTABLISHED state and continue processing, otherwise
1144 * send an RST.
1146 case TCPS_SYN_RECEIVED:
1147 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1148 SEQ_GT(ti->ti_ack, tp->snd_max))
1149 goto dropwithreset;
1151 tcpstat.tcps_connects++;
1152 soisconnected(so);
1153 /* Do window scaling? */
1154 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1155 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1156 tp->snd_scale = tp->requested_s_scale;
1157 tp->rcv_scale = tp->request_r_scale;
1160 * Upon successful completion of 3-way handshake,
1161 * update cache.CC if it was undefined, pass any queued
1162 * data to the user, and advance state appropriately.
1164 if ((taop = tcp_gettaocache(inp)) != NULL &&
1165 taop->tao_cc == 0)
1166 taop->tao_cc = tp->cc_recv;
1169 * Make transitions:
1170 * SYN-RECEIVED -> ESTABLISHED
1171 * SYN-RECEIVED* -> FIN-WAIT-1
1173 if (tp->t_flags & TF_NEEDFIN) {
1174 tp->t_state = TCPS_FIN_WAIT_1;
1175 tp->t_flags &= ~TF_NEEDFIN;
1176 } else
1177 tp->t_state = TCPS_ESTABLISHED;
1179 * If segment contains data or ACK, will call tcp_reass()
1180 * later; if not, do so now to pass queued data to user.
1182 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1183 (void) tcp_reass(tp, (struct tcpiphdr *)0,
1184 (struct mbuf *)0);
1185 tp->snd_wl1 = ti->ti_seq - 1;
1186 /* fall into ... */
1189 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1190 * ACKs. If the ack is in the range
1191 * tp->snd_una < ti->ti_ack <= tp->snd_max
1192 * then advance tp->snd_una to ti->ti_ack and drop
1193 * data from the retransmission queue. If this ACK reflects
1194 * more up to date window information we update our window information.
1196 case TCPS_ESTABLISHED:
1197 case TCPS_FIN_WAIT_1:
1198 case TCPS_FIN_WAIT_2:
1199 case TCPS_CLOSE_WAIT:
1200 case TCPS_CLOSING:
1201 case TCPS_LAST_ACK:
1202 case TCPS_TIME_WAIT:
1204 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1205 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1206 tcpstat.tcps_rcvdupack++;
1208 * If we have outstanding data (other than
1209 * a window probe), this is a completely
1210 * duplicate ack (ie, window info didn't
1211 * change), the ack is the biggest we've
1212 * seen and we've seen exactly our rexmt
1213 * threshhold of them, assume a packet
1214 * has been dropped and retransmit it.
1215 * Kludge snd_nxt & the congestion
1216 * window so we send only this one
1217 * packet.
1219 * We know we're losing at the current
1220 * window size so do congestion avoidance
1221 * (set ssthresh to half the current window
1222 * and pull our congestion window back to
1223 * the new ssthresh).
1225 * Dup acks mean that packets have left the
1226 * network (they're now cached at the receiver)
1227 * so bump cwnd by the amount in the receiver
1228 * to keep a constant cwnd packets in the
1229 * network.
1231 if (tp->t_timer[TCPT_REXMT] == 0 ||
1232 ti->ti_ack != tp->snd_una)
1233 tp->t_dupacks = 0;
1234 else if (++tp->t_dupacks == tcprexmtthresh) {
1235 tcp_seq onxt = tp->snd_nxt;
1236 u_int win =
1237 MIN(tp->snd_wnd, tp->snd_cwnd) / 2 /
1238 tp->t_maxseg;
1240 if (win < 2)
1241 win = 2;
1242 tp->snd_ssthresh = win * tp->t_maxseg;
1243 tp->t_timer[TCPT_REXMT] = 0;
1244 tp->t_rtt = 0;
1245 tp->snd_nxt = ti->ti_ack;
1246 tp->snd_cwnd = tp->t_maxseg;
1247 (void) tcp_output(tp);
1248 tp->snd_cwnd = tp->snd_ssthresh +
1249 tp->t_maxseg * tp->t_dupacks;
1250 if (SEQ_GT(onxt, tp->snd_nxt))
1251 tp->snd_nxt = onxt;
1252 goto drop;
1253 } else if (tp->t_dupacks > tcprexmtthresh) {
1254 tp->snd_cwnd += tp->t_maxseg;
1255 (void) tcp_output(tp);
1256 goto drop;
1258 } else
1259 tp->t_dupacks = 0;
1260 break;
1263 * If the congestion window was inflated to account
1264 * for the other side's cached packets, retract it.
1266 if (tp->t_dupacks > tcprexmtthresh &&
1267 tp->snd_cwnd > tp->snd_ssthresh)
1268 tp->snd_cwnd = tp->snd_ssthresh;
1269 tp->t_dupacks = 0;
1270 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1271 tcpstat.tcps_rcvacktoomuch++;
1272 goto dropafterack;
1275 * If we reach this point, ACK is not a duplicate,
1276 * i.e., it ACKs something we sent.
1278 if (tp->t_flags & TF_NEEDSYN) {
1280 * T/TCP: Connection was half-synchronized, and our
1281 * SYN has been ACK'd (so connection is now fully
1282 * synchronized). Go to non-starred state and
1283 * increment snd_una for ACK of SYN.
1285 tp->t_flags &= ~TF_NEEDSYN;
1286 tp->snd_una++;
1289 process_ACK:
1290 acked = ti->ti_ack - tp->snd_una;
1291 tcpstat.tcps_rcvackpack++;
1292 tcpstat.tcps_rcvackbyte += acked;
1295 * If we have a timestamp reply, update smoothed
1296 * round trip time. If no timestamp is present but
1297 * transmit timer is running and timed sequence
1298 * number was acked, update smoothed round trip time.
1299 * Since we now have an rtt measurement, cancel the
1300 * timer backoff (cf., Phil Karn's retransmit alg.).
1301 * Recompute the initial retransmit timer.
1303 if (to.to_flag & TOF_TS)
1304 tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1305 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1306 tcp_xmit_timer(tp,tp->t_rtt);
1309 * If all outstanding data is acked, stop retransmit
1310 * timer and remember to restart (more output or persist).
1311 * If there is more data to be acked, restart retransmit
1312 * timer, using current (possibly backed-off) value.
1314 if (ti->ti_ack == tp->snd_max) {
1315 tp->t_timer[TCPT_REXMT] = 0;
1316 needoutput = 1;
1317 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1318 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1321 * If no data (only SYN) was ACK'd,
1322 * skip rest of ACK processing.
1324 if (acked == 0)
1325 goto step6;
1328 * When new data is acked, open the congestion window.
1329 * If the window gives us less than ssthresh packets
1330 * in flight, open exponentially (maxseg per packet).
1331 * Otherwise open linearly: maxseg per window
1332 * (maxseg^2 / cwnd per packet).
1335 register u_int cw = tp->snd_cwnd;
1336 register u_int incr = tp->t_maxseg;
1338 if (cw > tp->snd_ssthresh)
1339 incr = incr * incr / cw;
1340 tp->snd_cwnd = MIN(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1342 if (acked > so->so_snd.sb_cc) {
1343 tp->snd_wnd -= so->so_snd.sb_cc;
1344 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1345 ourfinisacked = 1;
1346 } else {
1347 sbdrop(&so->so_snd, acked);
1348 tp->snd_wnd -= acked;
1349 ourfinisacked = 0;
1351 if (so->so_snd.sb_flags & SB_NOTIFY)
1352 sowwakeup(so);
1353 tp->snd_una = ti->ti_ack;
1354 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1355 tp->snd_nxt = tp->snd_una;
1357 switch (tp->t_state) {
1360 * In FIN_WAIT_1 STATE in addition to the processing
1361 * for the ESTABLISHED state if our FIN is now acknowledged
1362 * then enter FIN_WAIT_2.
1364 case TCPS_FIN_WAIT_1:
1365 if (ourfinisacked) {
1367 * If we can't receive any more
1368 * data, then closing user can proceed.
1369 * Starting the timer is contrary to the
1370 * specification, but if we don't get a FIN
1371 * we'll hang forever.
1373 if (so->so_state & SS_CANTRCVMORE) {
1374 soisdisconnected(so);
1375 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1377 tp->t_state = TCPS_FIN_WAIT_2;
1379 break;
1382 * In CLOSING STATE in addition to the processing for
1383 * the ESTABLISHED state if the ACK acknowledges our FIN
1384 * then enter the TIME-WAIT state, otherwise ignore
1385 * the segment.
1387 case TCPS_CLOSING:
1388 if (ourfinisacked) {
1389 tp->t_state = TCPS_TIME_WAIT;
1390 tcp_canceltimers(tp);
1391 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1392 if (tp->cc_recv != 0 &&
1393 tp->t_duration < TCPTV_MSL)
1394 tp->t_timer[TCPT_2MSL] =
1395 tp->t_rxtcur * TCPTV_TWTRUNC;
1396 else
1397 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1398 soisdisconnected(so);
1400 break;
1403 * In LAST_ACK, we may still be waiting for data to drain
1404 * and/or to be acked, as well as for the ack of our FIN.
1405 * If our FIN is now acknowledged, delete the TCB,
1406 * enter the closed state and return.
1408 case TCPS_LAST_ACK:
1409 if (ourfinisacked) {
1410 tp = tcp_close(tp);
1411 goto drop;
1413 break;
1416 * In TIME_WAIT state the only thing that should arrive
1417 * is a retransmission of the remote FIN. Acknowledge
1418 * it and restart the finack timer.
1420 case TCPS_TIME_WAIT:
1421 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1422 goto dropafterack;
1426 step6:
1428 * Update window information.
1429 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1431 if ((tiflags & TH_ACK) &&
1432 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1433 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1434 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1435 /* keep track of pure window updates */
1436 if (ti->ti_len == 0 &&
1437 tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1438 tcpstat.tcps_rcvwinupd++;
1439 tp->snd_wnd = tiwin;
1440 tp->snd_wl1 = ti->ti_seq;
1441 tp->snd_wl2 = ti->ti_ack;
1442 if (tp->snd_wnd > tp->max_sndwnd)
1443 tp->max_sndwnd = tp->snd_wnd;
1444 needoutput = 1;
1448 * Process segments with URG.
1450 if ((tiflags & TH_URG) && ti->ti_urp &&
1451 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1453 * This is a kludge, but if we receive and accept
1454 * random urgent pointers, we'll crash in
1455 * soreceive. It's hard to imagine someone
1456 * actually wanting to send this much urgent data.
1458 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1459 ti->ti_urp = 0; /* XXX */
1460 tiflags &= ~TH_URG; /* XXX */
1461 goto dodata; /* XXX */
1464 * If this segment advances the known urgent pointer,
1465 * then mark the data stream. This should not happen
1466 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1467 * a FIN has been received from the remote side.
1468 * In these states we ignore the URG.
1470 * According to RFC961 (Assigned Protocols),
1471 * the urgent pointer points to the last octet
1472 * of urgent data. We continue, however,
1473 * to consider it to indicate the first octet
1474 * of data past the urgent section as the original
1475 * spec states (in one of two places).
1477 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1478 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1479 so->so_oobmark = so->so_rcv.sb_cc +
1480 (tp->rcv_up - tp->rcv_nxt) - 1;
1481 if (so->so_oobmark == 0)
1482 so->so_state |= SS_RCVATMARK;
1483 sohasoutofband(so);
1484 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1487 * Remove out of band data so doesn't get presented to user.
1488 * This can happen independent of advancing the URG pointer,
1489 * but if two URG's are pending at once, some out-of-band
1490 * data may creep in... ick.
1492 if (ti->ti_urp <= (u_long)ti->ti_len
1493 #ifdef SO_OOBINLINE
1494 && (so->so_options & SO_OOBINLINE) == 0
1495 #endif
1497 tcp_pulloutofband(so, ti, m);
1498 } else
1500 * If no out of band data is expected,
1501 * pull receive urgent pointer along
1502 * with the receive window.
1504 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1505 tp->rcv_up = tp->rcv_nxt;
1506 dodata: /* XXX */
1509 * Process the segment text, merging it into the TCP sequencing queue,
1510 * and arranging for acknowledgment of receipt if necessary.
1511 * This process logically involves adjusting tp->rcv_wnd as data
1512 * is presented to the user (this happens in tcp_usrreq.c,
1513 * case PRU_RCVD). If a FIN has already been received on this
1514 * connection then we just ignore the text.
1516 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1517 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1518 TCP_REASS(tp, ti, m, so, tiflags);
1520 * Note the amount of data that peer has sent into
1521 * our window, in order to estimate the sender's
1522 * buffer size.
1524 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1525 } else {
1526 m_freem(m);
1527 tiflags &= ~TH_FIN;
1531 * If FIN is received ACK the FIN and let the user know
1532 * that the connection is closing.
1534 if (tiflags & TH_FIN) {
1535 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1536 socantrcvmore(so);
1538 * If connection is half-synchronized
1539 * (ie SEND_SYN flag on) then delay ACK,
1540 * so it may be piggybacked when SYN is sent.
1541 * Otherwise, since we received a FIN then no
1542 * more input can be expected, send ACK now.
1544 if (tp->t_flags & TF_NEEDSYN)
1545 tp->t_flags |= TF_DELACK;
1546 else
1547 tp->t_flags |= TF_ACKNOW;
1548 tp->rcv_nxt++;
1550 switch (tp->t_state) {
1553 * In SYN_RECEIVED and ESTABLISHED STATES
1554 * enter the CLOSE_WAIT state.
1556 case TCPS_SYN_RECEIVED:
1557 case TCPS_ESTABLISHED:
1558 tp->t_state = TCPS_CLOSE_WAIT;
1559 break;
1562 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1563 * enter the CLOSING state.
1565 case TCPS_FIN_WAIT_1:
1566 tp->t_state = TCPS_CLOSING;
1567 break;
1570 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1571 * starting the time-wait timer, turning off the other
1572 * standard timers.
1574 case TCPS_FIN_WAIT_2:
1575 tp->t_state = TCPS_TIME_WAIT;
1576 tcp_canceltimers(tp);
1577 /* Shorten TIME_WAIT [RFC-1644, p.28] */
1578 if (tp->cc_recv != 0 &&
1579 tp->t_duration < TCPTV_MSL) {
1580 tp->t_timer[TCPT_2MSL] =
1581 tp->t_rxtcur * TCPTV_TWTRUNC;
1582 /* For transaction client, force ACK now. */
1583 tp->t_flags |= TF_ACKNOW;
1585 else
1586 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1587 soisdisconnected(so);
1588 break;
1591 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1593 case TCPS_TIME_WAIT:
1594 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1595 break;
1598 #ifdef TCPDEBUG
1599 if (so->so_options & SO_DEBUG)
1600 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1601 #endif
1604 * Return any desired output.
1606 if (needoutput || (tp->t_flags & TF_ACKNOW))
1607 (void) tcp_output(tp);
1608 return;
1610 dropafterack:
1612 * Generate an ACK dropping incoming segment if it occupies
1613 * sequence space, where the ACK reflects our state.
1615 if (tiflags & TH_RST)
1616 goto drop;
1617 #ifdef TCPDEBUG
1618 if (so->so_options & SO_DEBUG)
1619 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1620 #endif
1621 m_freem(m);
1622 tp->t_flags |= TF_ACKNOW;
1623 (void) tcp_output(tp);
1624 return;
1626 dropwithreset:
1628 * Generate a RST, dropping incoming segment.
1629 * Make ACK acceptable to originator of segment.
1630 * Don't bother to respond if destination was broadcast/multicast.
1632 if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1633 IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1634 goto drop;
1635 #ifdef TCPDEBUG
1636 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1637 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1638 #endif
1639 if (tiflags & TH_ACK)
1640 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1641 else {
1642 if (tiflags & TH_SYN)
1643 ti->ti_len++;
1644 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1645 TH_RST|TH_ACK);
1647 /* destroy temporarily created socket */
1648 if (dropsocket)
1649 (void) soabort(so);
1650 return;
1652 drop:
1654 * Drop space held by incoming segment and return.
1656 #ifdef TCPDEBUG
1657 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1658 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1659 #endif
1660 m_freem(m);
1661 /* destroy temporarily created socket */
1662 if (dropsocket)
1663 (void) soabort(so);
1664 return;
1665 #ifndef TUBA_INCLUDE
1668 void
1669 tcp_dooptions(tp, cp, cnt, ti, to)
1670 struct tcpcb *tp;
1671 u_char *cp;
1672 int cnt;
1673 struct tcpiphdr *ti;
1674 struct tcpopt *to;
1676 u_short mss = 0;
1677 int opt, optlen;
1679 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1680 opt = cp[0];
1681 if (opt == TCPOPT_EOL)
1682 break;
1683 if (opt == TCPOPT_NOP)
1684 optlen = 1;
1685 else {
1686 optlen = cp[1];
1687 if (optlen <= 0)
1688 break;
1690 switch (opt) {
1692 default:
1693 continue;
1695 case TCPOPT_MAXSEG:
1696 if (optlen != TCPOLEN_MAXSEG)
1697 continue;
1698 if (!(ti->ti_flags & TH_SYN))
1699 continue;
1700 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1701 NTOHS(mss);
1702 break;
1704 case TCPOPT_WINDOW:
1705 if (optlen != TCPOLEN_WINDOW)
1706 continue;
1707 if (!(ti->ti_flags & TH_SYN))
1708 continue;
1709 tp->t_flags |= TF_RCVD_SCALE;
1710 tp->requested_s_scale = MIN(cp[2], TCP_MAX_WINSHIFT);
1711 break;
1713 case TCPOPT_TIMESTAMP:
1714 if (optlen != TCPOLEN_TIMESTAMP)
1715 continue;
1716 to->to_flag |= TOF_TS;
1717 bcopy((char *)cp + 2,
1718 (char *)&to->to_tsval, sizeof(to->to_tsval));
1719 NTOHL(to->to_tsval);
1720 bcopy((char *)cp + 6,
1721 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1722 NTOHL(to->to_tsecr);
1725 * A timestamp received in a SYN makes
1726 * it ok to send timestamp requests and replies.
1728 if (ti->ti_flags & TH_SYN) {
1729 tp->t_flags |= TF_RCVD_TSTMP;
1730 tp->ts_recent = to->to_tsval;
1731 tp->ts_recent_age = tcp_now;
1733 break;
1734 case TCPOPT_CC:
1735 if (optlen != TCPOLEN_CC)
1736 continue;
1737 to->to_flag |= TOF_CC;
1738 bcopy((char *)cp + 2,
1739 (char *)&to->to_cc, sizeof(to->to_cc));
1740 NTOHL(to->to_cc);
1742 * A CC or CC.new option received in a SYN makes
1743 * it ok to send CC in subsequent segments.
1745 if (ti->ti_flags & TH_SYN)
1746 tp->t_flags |= TF_RCVD_CC;
1747 break;
1748 case TCPOPT_CCNEW:
1749 if (optlen != TCPOLEN_CC)
1750 continue;
1751 if (!(ti->ti_flags & TH_SYN))
1752 continue;
1753 to->to_flag |= TOF_CCNEW;
1754 bcopy((char *)cp + 2,
1755 (char *)&to->to_cc, sizeof(to->to_cc));
1756 NTOHL(to->to_cc);
1758 * A CC or CC.new option received in a SYN makes
1759 * it ok to send CC in subsequent segments.
1761 tp->t_flags |= TF_RCVD_CC;
1762 break;
1763 case TCPOPT_CCECHO:
1764 if (optlen != TCPOLEN_CC)
1765 continue;
1766 if (!(ti->ti_flags & TH_SYN))
1767 continue;
1768 to->to_flag |= TOF_CCECHO;
1769 bcopy((char *)cp + 2,
1770 (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1771 NTOHL(to->to_ccecho);
1772 break;
1775 if (ti->ti_flags & TH_SYN)
1776 tcp_mss(tp, mss); /* sets t_maxseg */
1780 * Pull out of band byte out of a segment so
1781 * it doesn't appear in the user's data queue.
1782 * It is still reflected in the segment length for
1783 * sequencing purposes.
1785 void
1786 tcp_pulloutofband(so, ti, m)
1787 struct socket *so;
1788 struct tcpiphdr *ti;
1789 register struct mbuf *m;
1791 int cnt = ti->ti_urp - 1;
1793 while (cnt >= 0) {
1794 if (m->m_len > cnt) {
1795 char *cp = mtod(m, caddr_t) + cnt;
1796 struct tcpcb *tp = sototcpcb(so);
1798 tp->t_iobc = *cp;
1799 tp->t_oobflags |= TCPOOB_HAVEDATA;
1800 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1801 m->m_len--;
1802 return;
1804 cnt -= m->m_len;
1805 m = m->m_next;
1806 if (m == 0)
1807 break;
1809 panic("tcp_pulloutofband");
1813 * Collect new round-trip time estimate
1814 * and update averages and current timeout.
1816 void
1817 tcp_xmit_timer(tp, rtt)
1818 register struct tcpcb *tp;
1819 short rtt;
1821 register short delta;
1823 tcpstat.tcps_rttupdated++;
1824 if (tp->t_srtt != 0) {
1826 * srtt is stored as fixed point with 3 bits after the
1827 * binary point (i.e., scaled by 8). The following magic
1828 * is equivalent to the smoothing algorithm in rfc793 with
1829 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1830 * point). Adjust rtt to origin 0.
1832 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1833 if ((tp->t_srtt += delta) <= 0)
1834 tp->t_srtt = 1;
1836 * We accumulate a smoothed rtt variance (actually, a
1837 * smoothed mean difference), then set the retransmit
1838 * timer to smoothed rtt + 4 times the smoothed variance.
1839 * rttvar is stored as fixed point with 2 bits after the
1840 * binary point (scaled by 4). The following is
1841 * equivalent to rfc793 smoothing with an alpha of .75
1842 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1843 * rfc793's wired-in beta.
1845 if (delta < 0)
1846 delta = -delta;
1847 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1848 if ((tp->t_rttvar += delta) <= 0)
1849 tp->t_rttvar = 1;
1850 } else {
1852 * No rtt measurement yet - use the unsmoothed rtt.
1853 * Set the variance to half the rtt (so our first
1854 * retransmit happens at 3*rtt).
1856 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1857 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1859 tp->t_rtt = 0;
1860 tp->t_rxtshift = 0;
1863 * the retransmit should happen at rtt + 4 * rttvar.
1864 * Because of the way we do the smoothing, srtt and rttvar
1865 * will each average +1/2 tick of bias. When we compute
1866 * the retransmit timer, we want 1/2 tick of rounding and
1867 * 1 extra tick because of +-1/2 tick uncertainty in the
1868 * firing of the timer. The bias will give us exactly the
1869 * 1.5 tick we need. But, because the bias is
1870 * statistical, we have to test that we don't drop below
1871 * the minimum feasible timer (which is 2 ticks).
1873 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1874 tp->t_rttmin, TCPTV_REXMTMAX);
1877 * We received an ack for a packet that wasn't retransmitted;
1878 * it is probably safe to discard any error indications we've
1879 * received recently. This isn't quite right, but close enough
1880 * for now (a route might have failed after we sent a segment,
1881 * and the return path might not be symmetrical).
1883 tp->t_softerror = 0;
1887 * Determine a reasonable value for maxseg size.
1888 * If the route is known, check route for mtu.
1889 * If none, use an mss that can be handled on the outgoing
1890 * interface without forcing IP to fragment; if bigger than
1891 * an mbuf cluster (mbconf.mclbytes), round down to nearest multiple of mbconf.mclbytes
1892 * to utilize large mbufs. If no route is found, route has no mtu,
1893 * or the destination isn't local, use a default, hopefully conservative
1894 * size (usually 512 or the default IP max size, but no more than the mtu
1895 * of the interface), as we can't discover anything about intervening
1896 * gateways or networks. We also initialize the congestion/slow start
1897 * window to be a single segment if the destination isn't local.
1898 * While looking at the routing entry, we also initialize other path-dependent
1899 * parameters from pre-set or cached values in the routing entry.
1901 * Also take into account the space needed for options that we
1902 * send regularly. Make maxseg shorter by that amount to assure
1903 * that we can send maxseg amount of data even when the options
1904 * are present. Store the upper limit of the length of options plus
1905 * data in maxopd.
1907 * NOTE that this routine is only called when we process an incoming
1908 * segment, for outgoing segments only tcp_mssopt is called.
1910 * In case of T/TCP, we call this routine during implicit connection
1911 * setup as well (offer = -1), to initialize maxseg from the cached
1912 * MSS of our peer.
1914 void
1915 tcp_mss(tp, offer)
1916 struct tcpcb *tp;
1917 int offer;
1919 register struct rtentry *rt;
1920 struct ifnet *ifp;
1921 register int rtt, mss;
1922 u_long bufsize;
1923 struct inpcb *inp;
1924 struct socket *so;
1925 struct rmxp_tao *taop;
1926 int origoffer = offer;
1928 inp = tp->t_inpcb;
1929 if ((rt = tcp_rtlookup(inp)) == NULL) {
1930 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
1931 return;
1933 ifp = rt->rt_ifp;
1934 so = inp->inp_socket;
1936 taop = rmx_taop(rt->rt_rmx);
1938 * Offer == -1 means that we didn't receive SYN yet,
1939 * use cached value in that case;
1941 if (offer == -1)
1942 offer = taop->tao_mssopt;
1944 * Offer == 0 means that there was no MSS on the SYN segment,
1945 * in this case we use tcp_mssdflt.
1947 if (offer == 0)
1948 offer = tcp_mssdflt;
1949 else
1951 * Sanity check: make sure that maxopd will be large
1952 * enough to allow some data on segments even is the
1953 * all the option space is used (40bytes). Otherwise
1954 * funny things may happen in tcp_output.
1956 offer = MAX(offer, 64);
1957 taop->tao_mssopt = offer;
1959 #ifdef RTV_MTU /* if route characteristics exist ... */
1961 * While we're here, check if there's an initial rtt
1962 * or rttvar. Convert from the route-table units
1963 * to scaled multiples of the slow timeout timer.
1965 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1967 * XXX the lock bit for RTT indicates that the value
1968 * is also a minimum value; this is subject to time.
1970 if (rt->rt_rmx.rmx_locks & RTV_RTT)
1971 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1972 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1973 if (rt->rt_rmx.rmx_rttvar)
1974 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1975 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1976 else
1977 /* default variation is +- 1 rtt */
1978 tp->t_rttvar =
1979 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1980 TCPT_RANGESET(tp->t_rxtcur,
1981 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1982 tp->t_rttmin, TCPTV_REXMTMAX);
1985 * if there's an mtu associated with the route, use it
1987 if (rt->rt_rmx.rmx_mtu)
1988 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1989 else
1990 #endif /* RTV_MTU */
1992 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1993 if (!in_localaddr(inp->inp_faddr))
1994 mss = MIN(mss, tcp_mssdflt);
1996 mss = MIN(mss, offer);
1998 * maxopd stores the maximum length of data AND options
1999 * in a segment; maxseg is the amount of data in a normal
2000 * segment. We need to store this value (maxopd) apart
2001 * from maxseg, because now every segment carries options
2002 * and thus we normally have somewhat less data in segments.
2004 tp->t_maxopd = mss;
2007 * In case of T/TCP, origoffer==-1 indicates, that no segments
2008 * were received yet. In this case we just guess, otherwise
2009 * we do the same as before T/TCP.
2011 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2012 (origoffer == -1 ||
2013 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2014 mss -= TCPOLEN_TSTAMP_APPA;
2015 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2016 (origoffer == -1 ||
2017 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2018 mss -= TCPOLEN_CC_APPA;
2020 if ((mbconf.mclbytes & (mbconf.mclbytes - 1)) == 0) {
2021 if (mss > mbconf.mclbytes)
2022 mss &= ~(mbconf.mclbytes-1);
2023 } else {
2024 if (mss > mbconf.mclbytes)
2026 * DO NOT REMOVE THIS! A side effect is rounding.
2027 * Read the function description for more info
2029 mss = mss / mbconf.mclbytes * mbconf.mclbytes;
2032 * If there's a pipesize, change the socket buffer
2033 * to that size. Make the socket buffers an integral
2034 * number of mss units; if the mss is larger than
2035 * the socket buffer, decrease the mss.
2037 #ifdef RTV_SPIPE
2038 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2039 #endif
2040 bufsize = so->so_snd.sb_hiwat;
2041 if (bufsize < mss)
2042 mss = bufsize;
2043 else {
2044 bufsize = roundup(bufsize, mss);
2045 if (bufsize > sb_max)
2046 bufsize = sb_max;
2047 (void)sbreserve(&so->so_snd, bufsize);
2049 tp->t_maxseg = mss;
2051 #ifdef RTV_RPIPE
2052 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2053 #endif
2054 bufsize = so->so_rcv.sb_hiwat;
2055 if (bufsize > mss) {
2056 bufsize = roundup(bufsize, mss);
2057 if (bufsize > sb_max)
2058 bufsize = sb_max;
2059 (void)sbreserve(&so->so_rcv, bufsize);
2062 * Don't force slow-start on local network.
2064 if (!in_localaddr(inp->inp_faddr))
2065 tp->snd_cwnd = mss;
2067 #ifdef RTV_SSTHRESH
2068 if (rt->rt_rmx.rmx_ssthresh) {
2070 * There's some sort of gateway or interface
2071 * buffer limit on the path. Use this to set
2072 * the slow start threshhold, but set the
2073 * threshold to no less than 2*mss.
2075 tp->snd_ssthresh = MAX(2 * mss, rt->rt_rmx.rmx_ssthresh);
2077 #endif
2081 * Determine the MSS option to send on an outgoing SYN.
2084 tcp_mssopt(tp)
2085 struct tcpcb *tp;
2087 struct rtentry *rt;
2089 rt = tcp_rtlookup(tp->t_inpcb);
2090 if (rt == NULL)
2091 return tcp_mssdflt;
2094 * if there's an mtu associated with the route, use it
2096 if (rt->rt_rmx.rmx_mtu)
2097 return rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2099 return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2101 #endif /* TUBA_INCLUDE */