2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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
38 * Changes and additions relating to SLiRP
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
52 #define min(x,y) ((x) < (y) ? (x) : (y))
53 #define max(x,y) ((x) > (y) ? (x) : (y))
55 int tcprexmtthresh
= 3;
56 struct socket
*tcp_last_so
= &tcb
;
58 tcp_seq tcp_iss
; /* tcp initial send seq # */
60 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
62 /* for modulo comparisons of timestamps */
63 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
64 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
67 * Insert segment ti into reassembly queue of tcp with
68 * control block tp. Return TH_FIN if reassembly now includes
69 * a segment with FIN. The macro form does the common case inline
70 * (segment is the next to be received on an established connection,
71 * and the queue is empty), avoiding linkage into and removal
72 * from the queue and repetition of various conversions.
73 * Set DELACK for segments received in order, but ack immediately
74 * when segments are out of order (so fast retransmit can work).
77 #define TCP_REASS(tp, ti, m, so, flags) {\
78 if ((ti)->ti_seq == (tp)->rcv_nxt && \
79 (tp)->seg_next == (tcpiphdrp_32)(tp) && \
80 (tp)->t_state == TCPS_ESTABLISHED) {\
81 if (ti->ti_flags & TH_PUSH) \
82 tp->t_flags |= TF_ACKNOW; \
84 tp->t_flags |= TF_DELACK; \
85 (tp)->rcv_nxt += (ti)->ti_len; \
86 flags = (ti)->ti_flags & TH_FIN; \
87 tcpstat.tcps_rcvpack++;\
88 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
90 if (tcp_emu((so),(m))) sbappend((so), (m)); \
92 sbappend((so), (m)); \
93 /* sorwakeup(so); */ \
95 (flags) = tcp_reass((tp), (ti), (m)); \
96 tp->t_flags |= TF_ACKNOW; \
100 #define TCP_REASS(tp, ti, m, so, flags) { \
101 if ((ti)->ti_seq == (tp)->rcv_nxt && \
102 (tp)->seg_next == (tcpiphdrp_32)(tp) && \
103 (tp)->t_state == TCPS_ESTABLISHED) { \
104 tp->t_flags |= TF_DELACK; \
105 (tp)->rcv_nxt += (ti)->ti_len; \
106 flags = (ti)->ti_flags & TH_FIN; \
107 tcpstat.tcps_rcvpack++;\
108 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
110 if (tcp_emu((so),(m))) sbappend(so, (m)); \
112 sbappend((so), (m)); \
113 /* sorwakeup(so); */ \
115 (flags) = tcp_reass((tp), (ti), (m)); \
116 tp->t_flags |= TF_ACKNOW; \
123 register struct tcpcb
*tp
;
124 register struct tcpiphdr
*ti
;
127 register struct tcpiphdr
*q
;
128 struct socket
*so
= tp
->t_socket
;
132 * Call with ti==0 after become established to
133 * force pre-ESTABLISHED data up to user socket.
139 * Find a segment which begins after this one does.
141 for (q
= (struct tcpiphdr
*)tp
->seg_next
; q
!= (struct tcpiphdr
*)tp
;
142 q
= (struct tcpiphdr
*)q
->ti_next
)
143 if (SEQ_GT(q
->ti_seq
, ti
->ti_seq
))
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 ((struct tcpiphdr
*)q
->ti_prev
!= (struct tcpiphdr
*)tp
) {
153 q
= (struct tcpiphdr
*)q
->ti_prev
;
154 /* conversion to int (in i) handles seq wraparound */
155 i
= q
->ti_seq
+ q
->ti_len
- ti
->ti_seq
;
157 if (i
>= ti
->ti_len
) {
158 tcpstat
.tcps_rcvduppack
++;
159 tcpstat
.tcps_rcvdupbyte
+= ti
->ti_len
;
162 * Try to present any queued data
163 * at the left window edge to the user.
164 * This is needed after the 3-WHS
167 goto present
; /* ??? */
173 q
= (struct tcpiphdr
*)(q
->ti_next
);
175 tcpstat
.tcps_rcvoopack
++;
176 tcpstat
.tcps_rcvoobyte
+= ti
->ti_len
;
177 REASS_MBUF(ti
) = (mbufp_32
) m
; /* XXX */
180 * While we overlap succeeding segments trim them or,
181 * if they are completely covered, dequeue them.
183 while (q
!= (struct tcpiphdr
*)tp
) {
184 register int i
= (ti
->ti_seq
+ ti
->ti_len
) - q
->ti_seq
;
190 m_adj((struct mbuf
*) REASS_MBUF(q
), i
);
193 q
= (struct tcpiphdr
*)q
->ti_next
;
194 m
= (struct mbuf
*) REASS_MBUF((struct tcpiphdr
*)q
->ti_prev
);
195 remque_32((void *)(q
->ti_prev
));
200 * Stick new segment in its place.
202 insque_32(ti
, (void *)(q
->ti_prev
));
206 * Present data to user, advancing rcv_nxt through
207 * completed sequence space.
209 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
211 ti
= (struct tcpiphdr
*) tp
->seg_next
;
212 if (ti
== (struct tcpiphdr
*)tp
|| ti
->ti_seq
!= tp
->rcv_nxt
)
214 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& ti
->ti_len
)
217 tp
->rcv_nxt
+= ti
->ti_len
;
218 flags
= ti
->ti_flags
& TH_FIN
;
220 m
= (struct mbuf
*) REASS_MBUF(ti
); /* XXX */
221 ti
= (struct tcpiphdr
*)ti
->ti_next
;
222 /* if (so->so_state & SS_FCANTRCVMORE) */
223 if (so
->so_state
& SS_FCANTSENDMORE
)
227 if (tcp_emu(so
,m
)) sbappend(so
, m
);
231 } while (ti
!= (struct tcpiphdr
*)tp
&& ti
->ti_seq
== tp
->rcv_nxt
);
237 * TCP input routine, follows pages 65-76 of the
238 * protocol specification dated September, 1981 very closely.
241 tcp_input(m
, iphlen
, inso
)
242 register struct mbuf
*m
;
246 struct ip save_ip
, *ip
;
247 register struct tcpiphdr
*ti
;
251 register struct tcpcb
*tp
= 0;
252 register int tiflags
;
253 struct socket
*so
= 0;
254 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
255 /* int dropsocket = 0; */
259 /* int ts_present = 0; */
261 DEBUG_CALL("tcp_input");
262 DEBUG_ARGS((dfd
," m = %8lx iphlen = %2d inso = %lx\n",
263 (long )m
, iphlen
, (long )inso
));
266 * If called with m == 0, then we're continuing the connect
271 /* Re-set a few variables */
277 tiflags
= ti
->ti_flags
;
283 tcpstat
.tcps_rcvtotal
++;
285 * Get IP and TCP header together in first mbuf.
286 * Note: IP leaves IP header in first mbuf.
288 ti
= mtod(m
, struct tcpiphdr
*);
289 if (iphlen
> sizeof(struct ip
)) {
290 ip_stripoptions(m
, (struct mbuf
*)0);
291 iphlen
=sizeof(struct ip
);
293 /* XXX Check if too short */
297 * Save a copy of the IP header in case we want restore it
298 * for sending an ICMP error message in response.
300 ip
=mtod(m
, struct ip
*);
304 * Checksum extended TCP header and data.
306 //tlen = ((struct ip *)ti)->ip_len;
307 // use save_ip instead of ti to be avoid gcc aliasing optimization problems
309 ti
->ti_next
= ti
->ti_prev
= 0;
311 ti
->ti_len
= htons((u_int16_t
)tlen
);
312 len
= sizeof(struct ip
) + tlen
;
313 /* keep checksum for ICMP reply
314 * ti->ti_sum = cksum(m, len);
315 * if (ti->ti_sum) { */
317 tcpstat
.tcps_rcvbadsum
++;
321 save_ip
.ip_len
+= iphlen
;
324 * Check that TCP offset makes sense,
325 * pull out TCP options and adjust length. XXX
327 off
= ti
->ti_off
<< 2;
328 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
329 tcpstat
.tcps_rcvbadoff
++;
334 if (off
> sizeof (struct tcphdr
)) {
335 optlen
= off
- sizeof (struct tcphdr
);
336 optp
= mtod(m
, caddr_t
) + sizeof (struct tcpiphdr
);
339 * Do quick retrieval of timestamp options ("options
340 * prediction?"). If timestamp is the only option and it's
341 * formatted as recommended in RFC 1323 appendix A, we
342 * quickly get the values now and not bother calling
343 * tcp_dooptions(), etc.
345 /* if ((optlen == TCPOLEN_TSTAMP_APPA ||
346 * (optlen > TCPOLEN_TSTAMP_APPA &&
347 * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
348 * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
349 * (ti->ti_flags & TH_SYN) == 0) {
351 * ts_val = ntohl(*(u_int32_t *)(optp + 4));
352 * ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
353 * optp = NULL; / * we've parsed the options * /
357 tiflags
= ti
->ti_flags
;
360 * Convert TCP protocol specific fields to host format.
368 * Drop TCP, IP headers and TCP options.
370 m
->m_data
+= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
371 m
->m_len
-= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
374 * Locate pcb for segment.
378 if (so
->so_fport
!= ti
->ti_dport
||
379 so
->so_lport
!= ti
->ti_sport
||
380 so
->so_laddr
.s_addr
!= ti
->ti_src
.s_addr
||
381 so
->so_faddr
.s_addr
!= ti
->ti_dst
.s_addr
) {
382 so
= solookup(&tcb
, ti
->ti_src
, ti
->ti_sport
,
383 ti
->ti_dst
, ti
->ti_dport
);
386 ++tcpstat
.tcps_socachemiss
;
390 * If the state is CLOSED (i.e., TCB does not exist) then
391 * all data in the incoming segment is discarded.
392 * If the TCB exists but is in CLOSED state, it is embryonic,
393 * but should either do a listen or a connect soon.
395 * state == CLOSED means we've done socreate() but haven't
396 * attached it to a protocol yet...
398 * XXX If a TCB does not exist, and the TH_SYN flag is
399 * the only flag set, then create a session, mark it
400 * as if it was LISTENING, and continue...
403 if ((tiflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) != TH_SYN
)
406 if ((so
= socreate()) == NULL
)
408 if (tcp_attach(so
) < 0) {
409 free(so
); /* Not sofree (if it failed, it's not insqued) */
413 sbreserve(&so
->so_snd
, tcp_sndspace
);
414 sbreserve(&so
->so_rcv
, tcp_rcvspace
);
416 /* tcp_last_so = so; */ /* XXX ? */
417 /* tp = sototcpcb(so); */
419 so
->so_laddr
= ti
->ti_src
;
420 so
->so_lport
= ti
->ti_sport
;
421 so
->so_faddr
= ti
->ti_dst
;
422 so
->so_fport
= ti
->ti_dport
;
424 if ((so
->so_iptos
= tcp_tos(so
)) == 0)
425 so
->so_iptos
= ((struct ip
*)ti
)->ip_tos
;
428 tp
->t_state
= TCPS_LISTEN
;
432 * If this is a still-connecting socket, this probably
433 * a retransmit of the SYN. Whether it's a retransmit SYN
434 * or something else, we nuke it.
436 if (so
->so_state
& SS_ISFCONNECTING
)
440 /* XXX Should never fail */
443 if (tp
->t_state
== TCPS_CLOSED
)
446 /* Unscale the window into a 32-bit value. */
447 /* if ((tiflags & TH_SYN) == 0)
448 * tiwin = ti->ti_win << tp->snd_scale;
454 * Segment received on connection.
455 * Reset idle time and keep-alive timer.
459 tp
->t_timer
[TCPT_KEEP
] = tcp_keepintvl
;
461 tp
->t_timer
[TCPT_KEEP
] = tcp_keepidle
;
464 * Process options if not in LISTEN state,
465 * else do it below (after getting remote address).
467 if (optp
&& tp
->t_state
!= TCPS_LISTEN
)
468 tcp_dooptions(tp
, (u_char
*)optp
, optlen
, ti
);
470 /* &ts_present, &ts_val, &ts_ecr); */
473 * Header prediction: check for the two common cases
474 * of a uni-directional data xfer. If the packet has
475 * no control flags, is in-sequence, the window didn't
476 * change and we're not retransmitting, it's a
477 * candidate. If the length is zero and the ack moved
478 * forward, we're the sender side of the xfer. Just
479 * free the data acked & wake any higher level process
480 * that was blocked waiting for space. If the length
481 * is non-zero and the ack didn't move, we're the
482 * receiver side. If we're getting packets in-order
483 * (the reassembly queue is empty), add the data to
484 * the socket buffer and note that we need a delayed ack.
486 * XXX Some of these tests are not needed
487 * eg: the tiwin == tp->snd_wnd prevents many more
488 * predictions.. with no *real* advantage..
490 if (tp
->t_state
== TCPS_ESTABLISHED
&&
491 (tiflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
492 /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
493 ti
->ti_seq
== tp
->rcv_nxt
&&
494 tiwin
&& tiwin
== tp
->snd_wnd
&&
495 tp
->snd_nxt
== tp
->snd_max
) {
497 * If last ACK falls within this segment's sequence numbers,
498 * record the timestamp.
500 /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
501 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
502 * tp->ts_recent_age = tcp_now;
503 * tp->ts_recent = ts_val;
506 if (ti
->ti_len
== 0) {
507 if (SEQ_GT(ti
->ti_ack
, tp
->snd_una
) &&
508 SEQ_LEQ(ti
->ti_ack
, tp
->snd_max
) &&
509 tp
->snd_cwnd
>= tp
->snd_wnd
) {
511 * this is a pure ack for outstanding data.
513 ++tcpstat
.tcps_predack
;
515 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
518 SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
519 tcp_xmit_timer(tp
, tp
->t_rtt
);
520 acked
= ti
->ti_ack
- tp
->snd_una
;
521 tcpstat
.tcps_rcvackpack
++;
522 tcpstat
.tcps_rcvackbyte
+= acked
;
523 sbdrop(&so
->so_snd
, acked
);
524 tp
->snd_una
= ti
->ti_ack
;
528 * If all outstanding data are acked, stop
529 * retransmit timer, otherwise restart timer
530 * using current (possibly backed-off) value.
531 * If process is waiting for space,
532 * wakeup/selwakeup/signal. If data
533 * are ready to send, let tcp_output
534 * decide between more output or persist.
536 if (tp
->snd_una
== tp
->snd_max
)
537 tp
->t_timer
[TCPT_REXMT
] = 0;
538 else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
539 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
542 * There's room in so_snd, sowwakup will read()
543 * from the socket if we can
545 /* if (so->so_snd.sb_flags & SB_NOTIFY)
549 * This is called because sowwakeup might have
550 * put data into so_snd. Since we don't so sowwakeup,
551 * we don't need this.. XXX???
553 if (so
->so_snd
.sb_cc
)
554 (void) tcp_output(tp
);
558 } else if (ti
->ti_ack
== tp
->snd_una
&&
559 tp
->seg_next
== (tcpiphdrp_32
)tp
&&
560 ti
->ti_len
<= sbspace(&so
->so_rcv
)) {
562 * this is a pure, in-sequence data packet
563 * with nothing on the reassembly queue and
564 * we have enough buffer space to take it.
566 ++tcpstat
.tcps_preddat
;
567 tp
->rcv_nxt
+= ti
->ti_len
;
568 tcpstat
.tcps_rcvpack
++;
569 tcpstat
.tcps_rcvbyte
+= ti
->ti_len
;
571 * Add data to socket buffer.
574 if (tcp_emu(so
,m
)) sbappend(so
, m
);
579 * XXX This is called when data arrives. Later, check
580 * if we can actually write() to the socket
581 * XXX Need to check? It's be NON_BLOCKING
586 * If this is a short packet, then ACK now - with Nagel
587 * congestion avoidance sender won't send more until
590 * Here are 3 interpretations of what should happen.
591 * The best (for me) is to delay-ack everything except
592 * if it's a one-byte packet containing an ESC
593 * (this means it's an arrow key (or similar) sent using
594 * Nagel, hence there will be no echo)
595 * The first of these is the original, the second is the
596 * middle ground between the other 2
598 /* if (((unsigned)ti->ti_len < tp->t_maxseg)) {
600 /* if (((unsigned)ti->ti_len < tp->t_maxseg &&
601 * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
602 * ((so->so_iptos & IPTOS_LOWDELAY) &&
603 * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
605 if ((unsigned)ti
->ti_len
== 1 &&
606 ((struct tcpiphdr_2
*)ti
)->first_char
== (char)27) {
607 tp
->t_flags
|= TF_ACKNOW
;
610 tp
->t_flags
|= TF_DELACK
;
614 } /* header prediction */
616 * Calculate amount of space in receive window,
617 * and then do TCP input processing.
618 * Receive window is amount of space in rcv queue,
619 * but not less than advertised window.
622 win
= sbspace(&so
->so_rcv
);
625 tp
->rcv_wnd
= max(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
628 switch (tp
->t_state
) {
631 * If the state is LISTEN then ignore segment if it contains an RST.
632 * If the segment contains an ACK then it is bad and send a RST.
633 * If it does not contain a SYN then it is not interesting; drop it.
634 * Don't bother responding if the destination was a broadcast.
635 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
636 * tp->iss, and send a segment:
637 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
638 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
639 * Fill in remote peer address fields if not previously specified.
640 * Enter SYN_RECEIVED state, and process any other fields of this
641 * segment in this state.
645 if (tiflags
& TH_RST
)
647 if (tiflags
& TH_ACK
)
649 if ((tiflags
& TH_SYN
) == 0)
653 * This has way too many gotos...
654 * But a bit of spaghetti code never hurt anybody :)
658 * If this is destined for the control address, then flag to
659 * tcp_ctl once connected, otherwise connect
661 if ((so
->so_faddr
.s_addr
&htonl(0xffffff00)) == special_addr
.s_addr
) {
662 int lastbyte
=ntohl(so
->so_faddr
.s_addr
) & 0xff;
663 if (lastbyte
!=CTL_ALIAS
&& lastbyte
!=CTL_DNS
) {
665 if(lastbyte
==CTL_CMD
|| lastbyte
==CTL_EXEC
) {
666 /* Command or exec adress */
667 so
->so_state
|= SS_CTL
;
669 /* May be an add exec */
670 struct ex_list
*ex_ptr
;
672 for(ex_ptr
= exec_list
; ex_ptr
; ex_ptr
= ex_ptr
->ex_next
) {
673 if(ex_ptr
->ex_fport
== so
->so_fport
&&
674 lastbyte
== ex_ptr
->ex_addr
) {
675 so
->so_state
|= SS_CTL
;
680 if(so
->so_state
& SS_CTL
) goto cont_input
;
683 /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
686 if (so
->so_emu
& EMU_NOCONNECT
) {
687 so
->so_emu
&= ~EMU_NOCONNECT
;
691 if(tcp_fconnect(so
) == -1 && errno
!= EINPROGRESS
) {
692 u_char code
=ICMP_UNREACH_NET
;
693 DEBUG_MISC((dfd
," tcp fconnect errno = %d-%s\n",
694 errno
,strerror(errno
)));
695 if(errno
== ECONNREFUSED
) {
696 /* ACK the SYN, send RST to refuse the connection */
697 tcp_respond(tp
, ti
, m
, ti
->ti_seq
+1, (tcp_seq
)0,
700 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
701 HTONL(ti
->ti_seq
); /* restore tcp header */
705 m
->m_data
-= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
706 m
->m_len
+= sizeof(struct tcpiphdr
)+off
-sizeof(struct tcphdr
);
708 icmp_error(m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
714 * Haven't connected yet, save the current mbuf
716 * XXX Some OS's don't tell us whether the connect()
717 * succeeded or not. So we must time it out.
721 tp
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
;
722 tp
->t_state
= TCPS_SYN_RECEIVED
;
728 * Check if the connect succeeded
730 if (so
->so_state
& SS_NOFDREF
) {
738 tcp_dooptions(tp
, (u_char
*)optp
, optlen
, ti
);
740 /* &ts_present, &ts_val, &ts_ecr); */
746 tcp_iss
+= TCP_ISSINCR
/2;
747 tp
->irs
= ti
->ti_seq
;
750 tp
->t_flags
|= TF_ACKNOW
;
751 tp
->t_state
= TCPS_SYN_RECEIVED
;
752 tp
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
;
753 tcpstat
.tcps_accepts
++;
755 } /* case TCPS_LISTEN */
758 * If the state is SYN_SENT:
759 * if seg contains an ACK, but not for our SYN, drop the input.
760 * if seg contains a RST, then drop the connection.
761 * if seg does not contain SYN, then drop it.
762 * Otherwise this is an acceptable SYN segment
763 * initialize tp->rcv_nxt and tp->irs
764 * if seg contains ack then advance tp->snd_una
765 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
766 * arrange for segment to be acked (eventually)
767 * continue processing rest of data/controls, beginning with URG
770 if ((tiflags
& TH_ACK
) &&
771 (SEQ_LEQ(ti
->ti_ack
, tp
->iss
) ||
772 SEQ_GT(ti
->ti_ack
, tp
->snd_max
)))
775 if (tiflags
& TH_RST
) {
776 if (tiflags
& TH_ACK
)
777 tp
= tcp_drop(tp
,0); /* XXX Check t_softerror! */
781 if ((tiflags
& TH_SYN
) == 0)
783 if (tiflags
& TH_ACK
) {
784 tp
->snd_una
= ti
->ti_ack
;
785 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
786 tp
->snd_nxt
= tp
->snd_una
;
789 tp
->t_timer
[TCPT_REXMT
] = 0;
790 tp
->irs
= ti
->ti_seq
;
792 tp
->t_flags
|= TF_ACKNOW
;
793 if (tiflags
& TH_ACK
&& SEQ_GT(tp
->snd_una
, tp
->iss
)) {
794 tcpstat
.tcps_connects
++;
796 tp
->t_state
= TCPS_ESTABLISHED
;
798 /* Do window scaling on this connection? */
799 /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
800 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
801 * tp->snd_scale = tp->requested_s_scale;
802 * tp->rcv_scale = tp->request_r_scale;
805 (void) tcp_reass(tp
, (struct tcpiphdr
*)0,
808 * if we didn't have to retransmit the SYN,
809 * use its rtt as our initial srtt & rtt var.
812 tcp_xmit_timer(tp
, tp
->t_rtt
);
814 tp
->t_state
= TCPS_SYN_RECEIVED
;
818 * Advance ti->ti_seq to correspond to first data byte.
819 * If data, trim to stay within window,
820 * dropping FIN if necessary.
823 if (ti
->ti_len
> tp
->rcv_wnd
) {
824 todrop
= ti
->ti_len
- tp
->rcv_wnd
;
826 ti
->ti_len
= tp
->rcv_wnd
;
828 tcpstat
.tcps_rcvpackafterwin
++;
829 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
831 tp
->snd_wl1
= ti
->ti_seq
- 1;
832 tp
->rcv_up
= ti
->ti_seq
;
834 } /* switch tp->t_state */
836 * States other than LISTEN or SYN_SENT.
837 * First check timestamp, if present.
838 * Then check that at least some bytes of segment are within
839 * receive window. If segment begins before rcv_nxt,
840 * drop leading data (and SYN); if nothing left, just ack.
842 * RFC 1323 PAWS: If we have a timestamp reply on this segment
843 * and it's less than ts_recent, drop it.
845 /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
846 * TSTMP_LT(ts_val, tp->ts_recent)) {
848 */ /* Check to see if ts_recent is over 24 days old. */
849 /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
851 * * Invalidate ts_recent. If this segment updates
852 * * ts_recent, the age will be reset later and ts_recent
853 * * will get a valid value. If it does not, setting
854 * * ts_recent to zero will at least satisfy the
855 * * requirement that zero be placed in the timestamp
856 * * echo reply when ts_recent isn't valid. The
857 * * age isn't reset until we get a valid ts_recent
858 * * because we don't want out-of-order segments to be
859 * * dropped when ts_recent is old.
861 /* tp->ts_recent = 0;
863 * tcpstat.tcps_rcvduppack++;
864 * tcpstat.tcps_rcvdupbyte += ti->ti_len;
865 * tcpstat.tcps_pawsdrop++;
871 todrop
= tp
->rcv_nxt
- ti
->ti_seq
;
873 if (tiflags
& TH_SYN
) {
883 * Following if statement from Stevens, vol. 2, p. 960.
885 if (todrop
> ti
->ti_len
886 || (todrop
== ti
->ti_len
&& (tiflags
& TH_FIN
) == 0)) {
888 * Any valid FIN must be to the left of the window.
889 * At this point the FIN must be a duplicate or out
890 * of sequence; drop it.
895 * Send an ACK to resynchronize and drop any data.
896 * But keep on processing for RST or ACK.
898 tp
->t_flags
|= TF_ACKNOW
;
900 tcpstat
.tcps_rcvduppack
++;
901 tcpstat
.tcps_rcvdupbyte
+= todrop
;
903 tcpstat
.tcps_rcvpartduppack
++;
904 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
907 ti
->ti_seq
+= todrop
;
908 ti
->ti_len
-= todrop
;
909 if (ti
->ti_urp
> todrop
)
910 ti
->ti_urp
-= todrop
;
917 * If new data are received on a connection after the
918 * user processes are gone, then RST the other end.
920 if ((so
->so_state
& SS_NOFDREF
) &&
921 tp
->t_state
> TCPS_CLOSE_WAIT
&& ti
->ti_len
) {
923 tcpstat
.tcps_rcvafterclose
++;
928 * If segment ends after window, drop trailing data
929 * (and PUSH and FIN); if nothing left, just ACK.
931 todrop
= (ti
->ti_seq
+ti
->ti_len
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
933 tcpstat
.tcps_rcvpackafterwin
++;
934 if (todrop
>= ti
->ti_len
) {
935 tcpstat
.tcps_rcvbyteafterwin
+= ti
->ti_len
;
937 * If a new connection request is received
938 * while in TIME_WAIT, drop the old connection
939 * and start over if the sequence numbers
940 * are above the previous ones.
942 if (tiflags
& TH_SYN
&&
943 tp
->t_state
== TCPS_TIME_WAIT
&&
944 SEQ_GT(ti
->ti_seq
, tp
->rcv_nxt
)) {
945 iss
= tp
->rcv_nxt
+ TCP_ISSINCR
;
950 * If window is closed can only take segments at
951 * window edge, and have to drop data and PUSH from
952 * incoming segments. Continue processing, but
953 * remember to ack. Otherwise, drop segment
956 if (tp
->rcv_wnd
== 0 && ti
->ti_seq
== tp
->rcv_nxt
) {
957 tp
->t_flags
|= TF_ACKNOW
;
958 tcpstat
.tcps_rcvwinprobe
++;
962 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
964 ti
->ti_len
-= todrop
;
965 tiflags
&= ~(TH_PUSH
|TH_FIN
);
969 * If last ACK falls within this segment's sequence numbers,
970 * record its timestamp.
972 /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
973 * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
974 * ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
975 * tp->ts_recent_age = tcp_now;
976 * tp->ts_recent = ts_val;
981 * If the RST bit is set examine the state:
982 * SYN_RECEIVED STATE:
983 * If passive open, return to LISTEN state.
984 * If active open, inform user that connection was refused.
985 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
986 * Inform user that connection was reset, and close tcb.
987 * CLOSING, LAST_ACK, TIME_WAIT STATES
990 if (tiflags
&TH_RST
) switch (tp
->t_state
) {
992 case TCPS_SYN_RECEIVED
:
993 /* so->so_error = ECONNREFUSED; */
996 case TCPS_ESTABLISHED
:
997 case TCPS_FIN_WAIT_1
:
998 case TCPS_FIN_WAIT_2
:
999 case TCPS_CLOSE_WAIT
:
1000 /* so->so_error = ECONNRESET; */
1002 tp
->t_state
= TCPS_CLOSED
;
1003 tcpstat
.tcps_drops
++;
1009 case TCPS_TIME_WAIT
:
1015 * If a SYN is in the window, then this is an
1016 * error and we send an RST and drop the connection.
1018 if (tiflags
& TH_SYN
) {
1019 tp
= tcp_drop(tp
,0);
1024 * If the ACK bit is off we drop the segment and return.
1026 if ((tiflags
& TH_ACK
) == 0) goto drop
;
1031 switch (tp
->t_state
) {
1033 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1034 * ESTABLISHED state and continue processing, otherwise
1035 * send an RST. una<=ack<=max
1037 case TCPS_SYN_RECEIVED
:
1039 if (SEQ_GT(tp
->snd_una
, ti
->ti_ack
) ||
1040 SEQ_GT(ti
->ti_ack
, tp
->snd_max
))
1042 tcpstat
.tcps_connects
++;
1043 tp
->t_state
= TCPS_ESTABLISHED
;
1045 * The sent SYN is ack'ed with our sequence number +1
1046 * The first data byte already in the buffer will get
1047 * lost if no correction is made. This is only needed for
1048 * SS_CTL since the buffer is empty otherwise.
1049 * tp->snd_una++; or:
1051 tp
->snd_una
=ti
->ti_ack
;
1052 if (so
->so_state
& SS_CTL
) {
1053 /* So tcp_ctl reports the right state */
1057 so
->so_state
&= ~SS_CTL
; /* success XXX */
1058 } else if (ret
== 2) {
1059 so
->so_state
= SS_NOFDREF
; /* CTL_CMD */
1062 tp
->t_state
= TCPS_FIN_WAIT_1
;
1068 /* Do window scaling? */
1069 /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1070 * (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1071 * tp->snd_scale = tp->requested_s_scale;
1072 * tp->rcv_scale = tp->request_r_scale;
1075 (void) tcp_reass(tp
, (struct tcpiphdr
*)0, (struct mbuf
*)0);
1076 tp
->snd_wl1
= ti
->ti_seq
- 1;
1077 /* Avoid ack processing; snd_una==ti_ack => dup ack */
1082 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1083 * ACKs. If the ack is in the range
1084 * tp->snd_una < ti->ti_ack <= tp->snd_max
1085 * then advance tp->snd_una to ti->ti_ack and drop
1086 * data from the retransmission queue. If this ACK reflects
1087 * more up to date window information we update our window information.
1089 case TCPS_ESTABLISHED
:
1090 case TCPS_FIN_WAIT_1
:
1091 case TCPS_FIN_WAIT_2
:
1092 case TCPS_CLOSE_WAIT
:
1095 case TCPS_TIME_WAIT
:
1097 if (SEQ_LEQ(ti
->ti_ack
, tp
->snd_una
)) {
1098 if (ti
->ti_len
== 0 && tiwin
== tp
->snd_wnd
) {
1099 tcpstat
.tcps_rcvdupack
++;
1100 DEBUG_MISC((dfd
," dup ack m = %lx so = %lx \n",
1101 (long )m
, (long )so
));
1103 * If we have outstanding data (other than
1104 * a window probe), this is a completely
1105 * duplicate ack (ie, window info didn't
1106 * change), the ack is the biggest we've
1107 * seen and we've seen exactly our rexmt
1108 * threshold of them, assume a packet
1109 * has been dropped and retransmit it.
1110 * Kludge snd_nxt & the congestion
1111 * window so we send only this one
1114 * We know we're losing at the current
1115 * window size so do congestion avoidance
1116 * (set ssthresh to half the current window
1117 * and pull our congestion window back to
1118 * the new ssthresh).
1120 * Dup acks mean that packets have left the
1121 * network (they're now cached at the receiver)
1122 * so bump cwnd by the amount in the receiver
1123 * to keep a constant cwnd packets in the
1126 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
1127 ti
->ti_ack
!= tp
->snd_una
)
1129 else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1130 tcp_seq onxt
= tp
->snd_nxt
;
1132 min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1137 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1138 tp
->t_timer
[TCPT_REXMT
] = 0;
1140 tp
->snd_nxt
= ti
->ti_ack
;
1141 tp
->snd_cwnd
= tp
->t_maxseg
;
1142 (void) tcp_output(tp
);
1143 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1144 tp
->t_maxseg
* tp
->t_dupacks
;
1145 if (SEQ_GT(onxt
, tp
->snd_nxt
))
1148 } else if (tp
->t_dupacks
> tcprexmtthresh
) {
1149 tp
->snd_cwnd
+= tp
->t_maxseg
;
1150 (void) tcp_output(tp
);
1159 * If the congestion window was inflated to account
1160 * for the other side's cached packets, retract it.
1162 if (tp
->t_dupacks
> tcprexmtthresh
&&
1163 tp
->snd_cwnd
> tp
->snd_ssthresh
)
1164 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1166 if (SEQ_GT(ti
->ti_ack
, tp
->snd_max
)) {
1167 tcpstat
.tcps_rcvacktoomuch
++;
1170 acked
= ti
->ti_ack
- tp
->snd_una
;
1171 tcpstat
.tcps_rcvackpack
++;
1172 tcpstat
.tcps_rcvackbyte
+= acked
;
1175 * If we have a timestamp reply, update smoothed
1176 * round trip time. If no timestamp is present but
1177 * transmit timer is running and timed sequence
1178 * number was acked, update smoothed round trip time.
1179 * Since we now have an rtt measurement, cancel the
1180 * timer backoff (cf., Phil Karn's retransmit alg.).
1181 * Recompute the initial retransmit timer.
1184 * tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1187 if (tp
->t_rtt
&& SEQ_GT(ti
->ti_ack
, tp
->t_rtseq
))
1188 tcp_xmit_timer(tp
,tp
->t_rtt
);
1191 * If all outstanding data is acked, stop retransmit
1192 * timer and remember to restart (more output or persist).
1193 * If there is more data to be acked, restart retransmit
1194 * timer, using current (possibly backed-off) value.
1196 if (ti
->ti_ack
== tp
->snd_max
) {
1197 tp
->t_timer
[TCPT_REXMT
] = 0;
1199 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
1200 tp
->t_timer
[TCPT_REXMT
] = tp
->t_rxtcur
;
1202 * When new data is acked, open the congestion window.
1203 * If the window gives us less than ssthresh packets
1204 * in flight, open exponentially (maxseg per packet).
1205 * Otherwise open linearly: maxseg per window
1206 * (maxseg^2 / cwnd per packet).
1209 register u_int cw
= tp
->snd_cwnd
;
1210 register u_int incr
= tp
->t_maxseg
;
1212 if (cw
> tp
->snd_ssthresh
)
1213 incr
= incr
* incr
/ cw
;
1214 tp
->snd_cwnd
= min(cw
+ incr
, TCP_MAXWIN
<<tp
->snd_scale
);
1216 if (acked
> so
->so_snd
.sb_cc
) {
1217 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
1218 sbdrop(&so
->so_snd
, (int )so
->so_snd
.sb_cc
);
1221 sbdrop(&so
->so_snd
, acked
);
1222 tp
->snd_wnd
-= acked
;
1226 * XXX sowwakup is called when data is acked and there's room for
1227 * for more data... it should read() the socket
1229 /* if (so->so_snd.sb_flags & SB_NOTIFY)
1232 tp
->snd_una
= ti
->ti_ack
;
1233 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
1234 tp
->snd_nxt
= tp
->snd_una
;
1236 switch (tp
->t_state
) {
1239 * In FIN_WAIT_1 STATE in addition to the processing
1240 * for the ESTABLISHED state if our FIN is now acknowledged
1241 * then enter FIN_WAIT_2.
1243 case TCPS_FIN_WAIT_1
:
1244 if (ourfinisacked
) {
1246 * If we can't receive any more
1247 * data, then closing user can proceed.
1248 * Starting the timer is contrary to the
1249 * specification, but if we don't get a FIN
1250 * we'll hang forever.
1252 if (so
->so_state
& SS_FCANTRCVMORE
) {
1253 soisfdisconnected(so
);
1254 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1256 tp
->t_state
= TCPS_FIN_WAIT_2
;
1261 * In CLOSING STATE in addition to the processing for
1262 * the ESTABLISHED state if the ACK acknowledges our FIN
1263 * then enter the TIME-WAIT state, otherwise ignore
1267 if (ourfinisacked
) {
1268 tp
->t_state
= TCPS_TIME_WAIT
;
1269 tcp_canceltimers(tp
);
1270 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1271 soisfdisconnected(so
);
1276 * In LAST_ACK, we may still be waiting for data to drain
1277 * and/or to be acked, as well as for the ack of our FIN.
1278 * If our FIN is now acknowledged, delete the TCB,
1279 * enter the closed state and return.
1282 if (ourfinisacked
) {
1289 * In TIME_WAIT state the only thing that should arrive
1290 * is a retransmission of the remote FIN. Acknowledge
1291 * it and restart the finack timer.
1293 case TCPS_TIME_WAIT
:
1294 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1297 } /* switch(tp->t_state) */
1301 * Update window information.
1302 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1304 if ((tiflags
& TH_ACK
) &&
1305 (SEQ_LT(tp
->snd_wl1
, ti
->ti_seq
) ||
1306 (tp
->snd_wl1
== ti
->ti_seq
&& (SEQ_LT(tp
->snd_wl2
, ti
->ti_ack
) ||
1307 (tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
))))) {
1308 /* keep track of pure window updates */
1309 if (ti
->ti_len
== 0 &&
1310 tp
->snd_wl2
== ti
->ti_ack
&& tiwin
> tp
->snd_wnd
)
1311 tcpstat
.tcps_rcvwinupd
++;
1312 tp
->snd_wnd
= tiwin
;
1313 tp
->snd_wl1
= ti
->ti_seq
;
1314 tp
->snd_wl2
= ti
->ti_ack
;
1315 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1316 tp
->max_sndwnd
= tp
->snd_wnd
;
1321 * Process segments with URG.
1323 if ((tiflags
& TH_URG
) && ti
->ti_urp
&&
1324 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1326 * This is a kludge, but if we receive and accept
1327 * random urgent pointers, we'll crash in
1328 * soreceive. It's hard to imagine someone
1329 * actually wanting to send this much urgent data.
1331 if (ti
->ti_urp
+ so
->so_rcv
.sb_cc
> so
->so_rcv
.sb_datalen
) {
1337 * If this segment advances the known urgent pointer,
1338 * then mark the data stream. This should not happen
1339 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1340 * a FIN has been received from the remote side.
1341 * In these states we ignore the URG.
1343 * According to RFC961 (Assigned Protocols),
1344 * the urgent pointer points to the last octet
1345 * of urgent data. We continue, however,
1346 * to consider it to indicate the first octet
1347 * of data past the urgent section as the original
1348 * spec states (in one of two places).
1350 if (SEQ_GT(ti
->ti_seq
+ti
->ti_urp
, tp
->rcv_up
)) {
1351 tp
->rcv_up
= ti
->ti_seq
+ ti
->ti_urp
;
1352 so
->so_urgc
= so
->so_rcv
.sb_cc
+
1353 (tp
->rcv_up
- tp
->rcv_nxt
); /* -1; */
1354 tp
->rcv_up
= ti
->ti_seq
+ ti
->ti_urp
;
1359 * If no out of band data is expected,
1360 * pull receive urgent pointer along
1361 * with the receive window.
1363 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
1364 tp
->rcv_up
= tp
->rcv_nxt
;
1368 * Process the segment text, merging it into the TCP sequencing queue,
1369 * and arranging for acknowledgment of receipt if necessary.
1370 * This process logically involves adjusting tp->rcv_wnd as data
1371 * is presented to the user (this happens in tcp_usrreq.c,
1372 * case PRU_RCVD). If a FIN has already been received on this
1373 * connection then we just ignore the text.
1375 if ((ti
->ti_len
|| (tiflags
&TH_FIN
)) &&
1376 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1377 TCP_REASS(tp
, ti
, m
, so
, tiflags
);
1379 * Note the amount of data that peer has sent into
1380 * our window, in order to estimate the sender's
1383 len
= so
->so_rcv
.sb_datalen
- (tp
->rcv_adv
- tp
->rcv_nxt
);
1390 * If FIN is received ACK the FIN and let the user know
1391 * that the connection is closing.
1393 if (tiflags
& TH_FIN
) {
1394 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
1396 * If we receive a FIN we can't send more data,
1398 * Shutdown the socket if there is no rx data in the
1400 * soread() is called on completion of shutdown() and
1401 * will got to TCPS_LAST_ACK, and use tcp_output()
1404 /* sofcantrcvmore(so); */
1407 tp
->t_flags
|= TF_ACKNOW
;
1410 switch (tp
->t_state
) {
1413 * In SYN_RECEIVED and ESTABLISHED STATES
1414 * enter the CLOSE_WAIT state.
1416 case TCPS_SYN_RECEIVED
:
1417 case TCPS_ESTABLISHED
:
1418 if(so
->so_emu
== EMU_CTL
) /* no shutdown on socket */
1419 tp
->t_state
= TCPS_LAST_ACK
;
1421 tp
->t_state
= TCPS_CLOSE_WAIT
;
1425 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1426 * enter the CLOSING state.
1428 case TCPS_FIN_WAIT_1
:
1429 tp
->t_state
= TCPS_CLOSING
;
1433 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1434 * starting the time-wait timer, turning off the other
1437 case TCPS_FIN_WAIT_2
:
1438 tp
->t_state
= TCPS_TIME_WAIT
;
1439 tcp_canceltimers(tp
);
1440 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1441 soisfdisconnected(so
);
1445 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1447 case TCPS_TIME_WAIT
:
1448 tp
->t_timer
[TCPT_2MSL
] = 2 * TCPTV_MSL
;
1454 * If this is a small packet, then ACK now - with Nagel
1455 * congestion avoidance sender won't send more until
1460 /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
1462 /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
1463 * (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
1464 * ((so->so_iptos & IPTOS_LOWDELAY) &&
1465 * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
1467 if (ti
->ti_len
&& (unsigned)ti
->ti_len
<= 5 &&
1468 ((struct tcpiphdr_2
*)ti
)->first_char
== (char)27) {
1469 tp
->t_flags
|= TF_ACKNOW
;
1473 * Return any desired output.
1475 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
1476 (void) tcp_output(tp
);
1482 * Generate an ACK dropping incoming segment if it occupies
1483 * sequence space, where the ACK reflects our state.
1485 if (tiflags
& TH_RST
)
1488 tp
->t_flags
|= TF_ACKNOW
;
1489 (void) tcp_output(tp
);
1493 /* reuses m if m!=NULL, m_free() unnecessary */
1494 if (tiflags
& TH_ACK
)
1495 tcp_respond(tp
, ti
, m
, (tcp_seq
)0, ti
->ti_ack
, TH_RST
);
1497 if (tiflags
& TH_SYN
) ti
->ti_len
++;
1498 tcp_respond(tp
, ti
, m
, ti
->ti_seq
+ti
->ti_len
, (tcp_seq
)0,
1506 * Drop space held by incoming segment and return.
1513 /* , ts_present, ts_val, ts_ecr) */
1515 * u_int32_t *ts_val, *ts_ecr;
1518 tcp_dooptions(tp
, cp
, cnt
, ti
)
1522 struct tcpiphdr
*ti
;
1527 DEBUG_CALL("tcp_dooptions");
1528 DEBUG_ARGS((dfd
," tp = %lx cnt=%i \n", (long )tp
, cnt
));
1530 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1532 if (opt
== TCPOPT_EOL
)
1534 if (opt
== TCPOPT_NOP
)
1547 if (optlen
!= TCPOLEN_MAXSEG
)
1549 if (!(ti
->ti_flags
& TH_SYN
))
1551 memcpy((char *) &mss
, (char *) cp
+ 2, sizeof(mss
));
1553 (void) tcp_mss(tp
, mss
); /* sets t_maxseg */
1556 /* case TCPOPT_WINDOW:
1557 * if (optlen != TCPOLEN_WINDOW)
1559 * if (!(ti->ti_flags & TH_SYN))
1561 * tp->t_flags |= TF_RCVD_SCALE;
1562 * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1565 /* case TCPOPT_TIMESTAMP:
1566 * if (optlen != TCPOLEN_TIMESTAMP)
1569 * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1571 * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1575 * * A timestamp received in a SYN makes
1576 * * it ok to send timestamp requests and replies.
1578 /* if (ti->ti_flags & TH_SYN) {
1579 * tp->t_flags |= TF_RCVD_TSTMP;
1580 * tp->ts_recent = *ts_val;
1581 * tp->ts_recent_age = tcp_now;
1590 * Pull out of band byte out of a segment so
1591 * it doesn't appear in the user's data queue.
1592 * It is still reflected in the segment length for
1593 * sequencing purposes.
1599 tcp_pulloutofband(so
, ti
, m
)
1601 struct tcpiphdr
*ti
;
1602 register struct mbuf
*m
;
1604 int cnt
= ti
->ti_urp
- 1;
1607 if (m
->m_len
> cnt
) {
1608 char *cp
= mtod(m
, caddr_t
) + cnt
;
1609 struct tcpcb
*tp
= sototcpcb(so
);
1612 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
1613 memcpy(sp
, cp
+1, (unsigned)(m
->m_len
- cnt
- 1));
1618 m
= m
->m_next
; /* XXX WRONG! Fix it! */
1622 panic("tcp_pulloutofband");
1628 * Collect new round-trip time estimate
1629 * and update averages and current timeout.
1633 tcp_xmit_timer(tp
, rtt
)
1634 register struct tcpcb
*tp
;
1637 register short delta
;
1639 DEBUG_CALL("tcp_xmit_timer");
1640 DEBUG_ARG("tp = %lx", (long)tp
);
1641 DEBUG_ARG("rtt = %d", rtt
);
1643 tcpstat
.tcps_rttupdated
++;
1644 if (tp
->t_srtt
!= 0) {
1646 * srtt is stored as fixed point with 3 bits after the
1647 * binary point (i.e., scaled by 8). The following magic
1648 * is equivalent to the smoothing algorithm in rfc793 with
1649 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1650 * point). Adjust rtt to origin 0.
1652 delta
= rtt
- 1 - (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1653 if ((tp
->t_srtt
+= delta
) <= 0)
1656 * We accumulate a smoothed rtt variance (actually, a
1657 * smoothed mean difference), then set the retransmit
1658 * timer to smoothed rtt + 4 times the smoothed variance.
1659 * rttvar is stored as fixed point with 2 bits after the
1660 * binary point (scaled by 4). The following is
1661 * equivalent to rfc793 smoothing with an alpha of .75
1662 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1663 * rfc793's wired-in beta.
1667 delta
-= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1668 if ((tp
->t_rttvar
+= delta
) <= 0)
1672 * No rtt measurement yet - use the unsmoothed rtt.
1673 * Set the variance to half the rtt (so our first
1674 * retransmit happens at 3*rtt).
1676 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
1677 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
1683 * the retransmit should happen at rtt + 4 * rttvar.
1684 * Because of the way we do the smoothing, srtt and rttvar
1685 * will each average +1/2 tick of bias. When we compute
1686 * the retransmit timer, we want 1/2 tick of rounding and
1687 * 1 extra tick because of +-1/2 tick uncertainty in the
1688 * firing of the timer. The bias will give us exactly the
1689 * 1.5 tick we need. But, because the bias is
1690 * statistical, we have to test that we don't drop below
1691 * the minimum feasible timer (which is 2 ticks).
1693 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
1694 (short)tp
->t_rttmin
, TCPTV_REXMTMAX
); /* XXX */
1697 * We received an ack for a packet that wasn't retransmitted;
1698 * it is probably safe to discard any error indications we've
1699 * received recently. This isn't quite right, but close enough
1700 * for now (a route might have failed after we sent a segment,
1701 * and the return path might not be symmetrical).
1703 tp
->t_softerror
= 0;
1707 * Determine a reasonable value for maxseg size.
1708 * If the route is known, check route for mtu.
1709 * If none, use an mss that can be handled on the outgoing
1710 * interface without forcing IP to fragment; if bigger than
1711 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1712 * to utilize large mbufs. If no route is found, route has no mtu,
1713 * or the destination isn't local, use a default, hopefully conservative
1714 * size (usually 512 or the default IP max size, but no more than the mtu
1715 * of the interface), as we can't discover anything about intervening
1716 * gateways or networks. We also initialize the congestion/slow start
1717 * window to be a single segment if the destination isn't local.
1718 * While looking at the routing entry, we also initialize other path-dependent
1719 * parameters from pre-set or cached values in the routing entry.
1724 register struct tcpcb
*tp
;
1727 struct socket
*so
= tp
->t_socket
;
1730 DEBUG_CALL("tcp_mss");
1731 DEBUG_ARG("tp = %lx", (long)tp
);
1732 DEBUG_ARG("offer = %d", offer
);
1734 mss
= min(if_mtu
, if_mru
) - sizeof(struct tcpiphdr
);
1736 mss
= min(mss
, offer
);
1738 if (mss
< tp
->t_maxseg
|| offer
!= 0)
1743 sbreserve(&so
->so_snd
, tcp_sndspace
+((tcp_sndspace
%mss
)?(mss
-(tcp_sndspace
%mss
)):0));
1744 sbreserve(&so
->so_rcv
, tcp_rcvspace
+((tcp_rcvspace
%mss
)?(mss
-(tcp_rcvspace
%mss
)):0));
1746 DEBUG_MISC((dfd
, " returning mss = %d\n", mss
));