target-i386: Create gen_lea_v_seg
[qemu/ar7.git] / slirp / tcp_input.c
blob2027a7511d4e3bb73fae13e3f81dcc3207332ac3
1 /*
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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94
30 * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
41 #include "qemu/osdep.h"
42 #include <slirp.h>
43 #include "ip_icmp.h"
45 #define TCPREXMTTHRESH 3
47 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
49 /* for modulo comparisons of timestamps */
50 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
51 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
54 * Insert segment ti into reassembly queue of tcp with
55 * control block tp. Return TH_FIN if reassembly now includes
56 * a segment with FIN. The macro form does the common case inline
57 * (segment is the next to be received on an established connection,
58 * and the queue is empty), avoiding linkage into and removal
59 * from the queue and repetition of various conversions.
60 * Set DELACK for segments received in order, but ack immediately
61 * when segments are out of order (so fast retransmit can work).
63 #ifdef TCP_ACK_HACK
64 #define TCP_REASS(tp, ti, m, so, flags) {\
65 if ((ti)->ti_seq == (tp)->rcv_nxt && \
66 tcpfrag_list_empty(tp) && \
67 (tp)->t_state == TCPS_ESTABLISHED) {\
68 if (ti->ti_flags & TH_PUSH) \
69 tp->t_flags |= TF_ACKNOW; \
70 else \
71 tp->t_flags |= TF_DELACK; \
72 (tp)->rcv_nxt += (ti)->ti_len; \
73 flags = (ti)->ti_flags & TH_FIN; \
74 if (so->so_emu) { \
75 if (tcp_emu((so),(m))) sbappend((so), (m)); \
76 } else \
77 sbappend((so), (m)); \
78 } else {\
79 (flags) = tcp_reass((tp), (ti), (m)); \
80 tp->t_flags |= TF_ACKNOW; \
81 } \
83 #else
84 #define TCP_REASS(tp, ti, m, so, flags) { \
85 if ((ti)->ti_seq == (tp)->rcv_nxt && \
86 tcpfrag_list_empty(tp) && \
87 (tp)->t_state == TCPS_ESTABLISHED) { \
88 tp->t_flags |= TF_DELACK; \
89 (tp)->rcv_nxt += (ti)->ti_len; \
90 flags = (ti)->ti_flags & TH_FIN; \
91 if (so->so_emu) { \
92 if (tcp_emu((so),(m))) sbappend(so, (m)); \
93 } else \
94 sbappend((so), (m)); \
95 } else { \
96 (flags) = tcp_reass((tp), (ti), (m)); \
97 tp->t_flags |= TF_ACKNOW; \
98 } \
100 #endif
101 static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt,
102 struct tcpiphdr *ti);
103 static void tcp_xmit_timer(register struct tcpcb *tp, int rtt);
105 static int
106 tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti,
107 struct mbuf *m)
109 register struct tcpiphdr *q;
110 struct socket *so = tp->t_socket;
111 int flags;
114 * Call with ti==NULL after become established to
115 * force pre-ESTABLISHED data up to user socket.
117 if (ti == NULL)
118 goto present;
121 * Find a segment which begins after this one does.
123 for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
124 q = tcpiphdr_next(q))
125 if (SEQ_GT(q->ti_seq, ti->ti_seq))
126 break;
129 * If there is a preceding segment, it may provide some of
130 * our data already. If so, drop the data from the incoming
131 * segment. If it provides all of our data, drop us.
133 if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
134 register int i;
135 q = tcpiphdr_prev(q);
136 /* conversion to int (in i) handles seq wraparound */
137 i = q->ti_seq + q->ti_len - ti->ti_seq;
138 if (i > 0) {
139 if (i >= ti->ti_len) {
140 m_free(m);
142 * Try to present any queued data
143 * at the left window edge to the user.
144 * This is needed after the 3-WHS
145 * completes.
147 goto present; /* ??? */
149 m_adj(m, i);
150 ti->ti_len -= i;
151 ti->ti_seq += i;
153 q = tcpiphdr_next(q);
155 ti->ti_mbuf = m;
158 * While we overlap succeeding segments trim them or,
159 * if they are completely covered, dequeue them.
161 while (!tcpfrag_list_end(q, tp)) {
162 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
163 if (i <= 0)
164 break;
165 if (i < q->ti_len) {
166 q->ti_seq += i;
167 q->ti_len -= i;
168 m_adj(q->ti_mbuf, i);
169 break;
171 q = tcpiphdr_next(q);
172 m = tcpiphdr_prev(q)->ti_mbuf;
173 remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
174 m_free(m);
178 * Stick new segment in its place.
180 insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
182 present:
184 * Present data to user, advancing rcv_nxt through
185 * completed sequence space.
187 if (!TCPS_HAVEESTABLISHED(tp->t_state))
188 return (0);
189 ti = tcpfrag_list_first(tp);
190 if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt)
191 return (0);
192 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
193 return (0);
194 do {
195 tp->rcv_nxt += ti->ti_len;
196 flags = ti->ti_flags & TH_FIN;
197 remque(tcpiphdr2qlink(ti));
198 m = ti->ti_mbuf;
199 ti = tcpiphdr_next(ti);
200 if (so->so_state & SS_FCANTSENDMORE)
201 m_free(m);
202 else {
203 if (so->so_emu) {
204 if (tcp_emu(so,m)) sbappend(so, m);
205 } else
206 sbappend(so, m);
208 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
209 return (flags);
213 * TCP input routine, follows pages 65-76 of the
214 * protocol specification dated September, 1981 very closely.
216 void
217 tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
219 struct ip save_ip, *ip;
220 register struct tcpiphdr *ti;
221 caddr_t optp = NULL;
222 int optlen = 0;
223 int len, tlen, off;
224 register struct tcpcb *tp = NULL;
225 register int tiflags;
226 struct socket *so = NULL;
227 int todrop, acked, ourfinisacked, needoutput = 0;
228 int iss = 0;
229 u_long tiwin;
230 int ret;
231 struct sockaddr_storage lhost, fhost;
232 struct sockaddr_in *lhost4, *fhost4;
233 struct ex_list *ex_ptr;
234 Slirp *slirp;
236 DEBUG_CALL("tcp_input");
237 DEBUG_ARGS((dfd, " m = %p iphlen = %2d inso = %p\n",
238 m, iphlen, inso));
241 * If called with m == 0, then we're continuing the connect
243 if (m == NULL) {
244 so = inso;
245 slirp = so->slirp;
247 /* Re-set a few variables */
248 tp = sototcpcb(so);
249 m = so->so_m;
250 so->so_m = NULL;
251 ti = so->so_ti;
252 tiwin = ti->ti_win;
253 tiflags = ti->ti_flags;
255 goto cont_conn;
257 slirp = m->slirp;
260 * Get IP and TCP header together in first mbuf.
261 * Note: IP leaves IP header in first mbuf.
263 ti = mtod(m, struct tcpiphdr *);
264 if (iphlen > sizeof(struct ip )) {
265 ip_stripoptions(m, (struct mbuf *)0);
266 iphlen=sizeof(struct ip );
268 /* XXX Check if too short */
272 * Save a copy of the IP header in case we want restore it
273 * for sending an ICMP error message in response.
275 ip=mtod(m, struct ip *);
276 save_ip = *ip;
277 save_ip.ip_len+= iphlen;
280 * Checksum extended TCP header and data.
282 tlen = ((struct ip *)ti)->ip_len;
283 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
284 memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
285 ti->ti_x1 = 0;
286 ti->ti_len = htons((uint16_t)tlen);
287 len = sizeof(struct ip ) + tlen;
288 if(cksum(m, len)) {
289 goto drop;
293 * Check that TCP offset makes sense,
294 * pull out TCP options and adjust length. XXX
296 off = ti->ti_off << 2;
297 if (off < sizeof (struct tcphdr) || off > tlen) {
298 goto drop;
300 tlen -= off;
301 ti->ti_len = tlen;
302 if (off > sizeof (struct tcphdr)) {
303 optlen = off - sizeof (struct tcphdr);
304 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
306 tiflags = ti->ti_flags;
309 * Convert TCP protocol specific fields to host format.
311 NTOHL(ti->ti_seq);
312 NTOHL(ti->ti_ack);
313 NTOHS(ti->ti_win);
314 NTOHS(ti->ti_urp);
317 * Drop TCP, IP headers and TCP options.
319 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
320 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
323 * Locate pcb for segment.
325 findso:
326 lhost.ss_family = AF_INET;
327 lhost4 = (struct sockaddr_in *) &lhost;
328 lhost4->sin_addr = ti->ti_src;
329 lhost4->sin_port = ti->ti_sport;
330 fhost.ss_family = AF_INET;
331 fhost4 = (struct sockaddr_in *) &fhost;
332 fhost4->sin_addr = ti->ti_dst;
333 fhost4->sin_port = ti->ti_dport;
335 so = solookup(&slirp->tcp_last_so, &slirp->tcb, &lhost, &fhost);
338 * If the state is CLOSED (i.e., TCB does not exist) then
339 * all data in the incoming segment is discarded.
340 * If the TCB exists but is in CLOSED state, it is embryonic,
341 * but should either do a listen or a connect soon.
343 * state == CLOSED means we've done socreate() but haven't
344 * attached it to a protocol yet...
346 * XXX If a TCB does not exist, and the TH_SYN flag is
347 * the only flag set, then create a session, mark it
348 * as if it was LISTENING, and continue...
350 if (so == NULL) {
351 if (slirp->restricted) {
352 /* Any hostfwds will have an existing socket, so we only get here
353 * for non-hostfwd connections. These should be dropped, unless it
354 * happens to be a guestfwd.
356 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
357 if (ex_ptr->ex_fport == ti->ti_dport &&
358 ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
359 break;
362 if (!ex_ptr) {
363 goto dropwithreset;
367 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
368 goto dropwithreset;
370 if ((so = socreate(slirp)) == NULL)
371 goto dropwithreset;
372 if (tcp_attach(so) < 0) {
373 free(so); /* Not sofree (if it failed, it's not insqued) */
374 goto dropwithreset;
377 sbreserve(&so->so_snd, TCP_SNDSPACE);
378 sbreserve(&so->so_rcv, TCP_RCVSPACE);
380 so->lhost.ss = lhost;
381 so->fhost.ss = fhost;
383 if ((so->so_iptos = tcp_tos(so)) == 0)
384 so->so_iptos = ((struct ip *)ti)->ip_tos;
386 tp = sototcpcb(so);
387 tp->t_state = TCPS_LISTEN;
391 * If this is a still-connecting socket, this probably
392 * a retransmit of the SYN. Whether it's a retransmit SYN
393 * or something else, we nuke it.
395 if (so->so_state & SS_ISFCONNECTING)
396 goto drop;
398 tp = sototcpcb(so);
400 /* XXX Should never fail */
401 if (tp == NULL)
402 goto dropwithreset;
403 if (tp->t_state == TCPS_CLOSED)
404 goto drop;
406 tiwin = ti->ti_win;
409 * Segment received on connection.
410 * Reset idle time and keep-alive timer.
412 tp->t_idle = 0;
413 if (SO_OPTIONS)
414 tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
415 else
416 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
419 * Process options if not in LISTEN state,
420 * else do it below (after getting remote address).
422 if (optp && tp->t_state != TCPS_LISTEN)
423 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
426 * Header prediction: check for the two common cases
427 * of a uni-directional data xfer. If the packet has
428 * no control flags, is in-sequence, the window didn't
429 * change and we're not retransmitting, it's a
430 * candidate. If the length is zero and the ack moved
431 * forward, we're the sender side of the xfer. Just
432 * free the data acked & wake any higher level process
433 * that was blocked waiting for space. If the length
434 * is non-zero and the ack didn't move, we're the
435 * receiver side. If we're getting packets in-order
436 * (the reassembly queue is empty), add the data to
437 * the socket buffer and note that we need a delayed ack.
439 * XXX Some of these tests are not needed
440 * eg: the tiwin == tp->snd_wnd prevents many more
441 * predictions.. with no *real* advantage..
443 if (tp->t_state == TCPS_ESTABLISHED &&
444 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
445 ti->ti_seq == tp->rcv_nxt &&
446 tiwin && tiwin == tp->snd_wnd &&
447 tp->snd_nxt == tp->snd_max) {
448 if (ti->ti_len == 0) {
449 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
450 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
451 tp->snd_cwnd >= tp->snd_wnd) {
453 * this is a pure ack for outstanding data.
455 if (tp->t_rtt &&
456 SEQ_GT(ti->ti_ack, tp->t_rtseq))
457 tcp_xmit_timer(tp, tp->t_rtt);
458 acked = ti->ti_ack - tp->snd_una;
459 sbdrop(&so->so_snd, acked);
460 tp->snd_una = ti->ti_ack;
461 m_free(m);
464 * If all outstanding data are acked, stop
465 * retransmit timer, otherwise restart timer
466 * using current (possibly backed-off) value.
467 * If process is waiting for space,
468 * wakeup/selwakeup/signal. If data
469 * are ready to send, let tcp_output
470 * decide between more output or persist.
472 if (tp->snd_una == tp->snd_max)
473 tp->t_timer[TCPT_REXMT] = 0;
474 else if (tp->t_timer[TCPT_PERSIST] == 0)
475 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
478 * This is called because sowwakeup might have
479 * put data into so_snd. Since we don't so sowwakeup,
480 * we don't need this.. XXX???
482 if (so->so_snd.sb_cc)
483 (void) tcp_output(tp);
485 return;
487 } else if (ti->ti_ack == tp->snd_una &&
488 tcpfrag_list_empty(tp) &&
489 ti->ti_len <= sbspace(&so->so_rcv)) {
491 * this is a pure, in-sequence data packet
492 * with nothing on the reassembly queue and
493 * we have enough buffer space to take it.
495 tp->rcv_nxt += ti->ti_len;
497 * Add data to socket buffer.
499 if (so->so_emu) {
500 if (tcp_emu(so,m)) sbappend(so, m);
501 } else
502 sbappend(so, m);
505 * If this is a short packet, then ACK now - with Nagel
506 * congestion avoidance sender won't send more until
507 * he gets an ACK.
509 * It is better to not delay acks at all to maximize
510 * TCP throughput. See RFC 2581.
512 tp->t_flags |= TF_ACKNOW;
513 tcp_output(tp);
514 return;
516 } /* header prediction */
518 * Calculate amount of space in receive window,
519 * and then do TCP input processing.
520 * Receive window is amount of space in rcv queue,
521 * but not less than advertised window.
523 { int win;
524 win = sbspace(&so->so_rcv);
525 if (win < 0)
526 win = 0;
527 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
530 switch (tp->t_state) {
533 * If the state is LISTEN then ignore segment if it contains an RST.
534 * If the segment contains an ACK then it is bad and send a RST.
535 * If it does not contain a SYN then it is not interesting; drop it.
536 * Don't bother responding if the destination was a broadcast.
537 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
538 * tp->iss, and send a segment:
539 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
540 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
541 * Fill in remote peer address fields if not previously specified.
542 * Enter SYN_RECEIVED state, and process any other fields of this
543 * segment in this state.
545 case TCPS_LISTEN: {
547 if (tiflags & TH_RST)
548 goto drop;
549 if (tiflags & TH_ACK)
550 goto dropwithreset;
551 if ((tiflags & TH_SYN) == 0)
552 goto drop;
555 * This has way too many gotos...
556 * But a bit of spaghetti code never hurt anybody :)
560 * If this is destined for the control address, then flag to
561 * tcp_ctl once connected, otherwise connect
563 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
564 slirp->vnetwork_addr.s_addr) {
565 if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
566 so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
567 /* May be an add exec */
568 for (ex_ptr = slirp->exec_list; ex_ptr;
569 ex_ptr = ex_ptr->ex_next) {
570 if(ex_ptr->ex_fport == so->so_fport &&
571 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
572 so->so_state |= SS_CTL;
573 break;
576 if (so->so_state & SS_CTL) {
577 goto cont_input;
580 /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
583 if (so->so_emu & EMU_NOCONNECT) {
584 so->so_emu &= ~EMU_NOCONNECT;
585 goto cont_input;
588 if ((tcp_fconnect(so, so->so_ffamily) == -1) &&
589 #if defined(_WIN32)
590 socket_error() != WSAEWOULDBLOCK
591 #else
592 (errno != EINPROGRESS) && (errno != EWOULDBLOCK)
593 #endif
595 u_char code=ICMP_UNREACH_NET;
596 DEBUG_MISC((dfd, " tcp fconnect errno = %d-%s\n",
597 errno,strerror(errno)));
598 if(errno == ECONNREFUSED) {
599 /* ACK the SYN, send RST to refuse the connection */
600 tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
601 TH_RST|TH_ACK);
602 } else {
603 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
604 HTONL(ti->ti_seq); /* restore tcp header */
605 HTONL(ti->ti_ack);
606 HTONS(ti->ti_win);
607 HTONS(ti->ti_urp);
608 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
609 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
610 *ip=save_ip;
611 icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno));
613 tcp_close(tp);
614 m_free(m);
615 } else {
617 * Haven't connected yet, save the current mbuf
618 * and ti, and return
619 * XXX Some OS's don't tell us whether the connect()
620 * succeeded or not. So we must time it out.
622 so->so_m = m;
623 so->so_ti = ti;
624 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
625 tp->t_state = TCPS_SYN_RECEIVED;
626 tcp_template(tp);
628 return;
630 cont_conn:
631 /* m==NULL
632 * Check if the connect succeeded
634 if (so->so_state & SS_NOFDREF) {
635 tp = tcp_close(tp);
636 goto dropwithreset;
638 cont_input:
639 tcp_template(tp);
641 if (optp)
642 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
644 if (iss)
645 tp->iss = iss;
646 else
647 tp->iss = slirp->tcp_iss;
648 slirp->tcp_iss += TCP_ISSINCR/2;
649 tp->irs = ti->ti_seq;
650 tcp_sendseqinit(tp);
651 tcp_rcvseqinit(tp);
652 tp->t_flags |= TF_ACKNOW;
653 tp->t_state = TCPS_SYN_RECEIVED;
654 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
655 goto trimthenstep6;
656 } /* case TCPS_LISTEN */
659 * If the state is SYN_SENT:
660 * if seg contains an ACK, but not for our SYN, drop the input.
661 * if seg contains a RST, then drop the connection.
662 * if seg does not contain SYN, then drop it.
663 * Otherwise this is an acceptable SYN segment
664 * initialize tp->rcv_nxt and tp->irs
665 * if seg contains ack then advance tp->snd_una
666 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
667 * arrange for segment to be acked (eventually)
668 * continue processing rest of data/controls, beginning with URG
670 case TCPS_SYN_SENT:
671 if ((tiflags & TH_ACK) &&
672 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
673 SEQ_GT(ti->ti_ack, tp->snd_max)))
674 goto dropwithreset;
676 if (tiflags & TH_RST) {
677 if (tiflags & TH_ACK) {
678 tcp_drop(tp, 0); /* XXX Check t_softerror! */
680 goto drop;
683 if ((tiflags & TH_SYN) == 0)
684 goto drop;
685 if (tiflags & TH_ACK) {
686 tp->snd_una = ti->ti_ack;
687 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
688 tp->snd_nxt = tp->snd_una;
691 tp->t_timer[TCPT_REXMT] = 0;
692 tp->irs = ti->ti_seq;
693 tcp_rcvseqinit(tp);
694 tp->t_flags |= TF_ACKNOW;
695 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
696 soisfconnected(so);
697 tp->t_state = TCPS_ESTABLISHED;
699 (void) tcp_reass(tp, (struct tcpiphdr *)0,
700 (struct mbuf *)0);
702 * if we didn't have to retransmit the SYN,
703 * use its rtt as our initial srtt & rtt var.
705 if (tp->t_rtt)
706 tcp_xmit_timer(tp, tp->t_rtt);
707 } else
708 tp->t_state = TCPS_SYN_RECEIVED;
710 trimthenstep6:
712 * Advance ti->ti_seq to correspond to first data byte.
713 * If data, trim to stay within window,
714 * dropping FIN if necessary.
716 ti->ti_seq++;
717 if (ti->ti_len > tp->rcv_wnd) {
718 todrop = ti->ti_len - tp->rcv_wnd;
719 m_adj(m, -todrop);
720 ti->ti_len = tp->rcv_wnd;
721 tiflags &= ~TH_FIN;
723 tp->snd_wl1 = ti->ti_seq - 1;
724 tp->rcv_up = ti->ti_seq;
725 goto step6;
726 } /* switch tp->t_state */
728 * States other than LISTEN or SYN_SENT.
729 * Check that at least some bytes of segment are within
730 * receive window. If segment begins before rcv_nxt,
731 * drop leading data (and SYN); if nothing left, just ack.
733 todrop = tp->rcv_nxt - ti->ti_seq;
734 if (todrop > 0) {
735 if (tiflags & TH_SYN) {
736 tiflags &= ~TH_SYN;
737 ti->ti_seq++;
738 if (ti->ti_urp > 1)
739 ti->ti_urp--;
740 else
741 tiflags &= ~TH_URG;
742 todrop--;
745 * Following if statement from Stevens, vol. 2, p. 960.
747 if (todrop > ti->ti_len
748 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
750 * Any valid FIN must be to the left of the window.
751 * At this point the FIN must be a duplicate or out
752 * of sequence; drop it.
754 tiflags &= ~TH_FIN;
757 * Send an ACK to resynchronize and drop any data.
758 * But keep on processing for RST or ACK.
760 tp->t_flags |= TF_ACKNOW;
761 todrop = ti->ti_len;
763 m_adj(m, todrop);
764 ti->ti_seq += todrop;
765 ti->ti_len -= todrop;
766 if (ti->ti_urp > todrop)
767 ti->ti_urp -= todrop;
768 else {
769 tiflags &= ~TH_URG;
770 ti->ti_urp = 0;
774 * If new data are received on a connection after the
775 * user processes are gone, then RST the other end.
777 if ((so->so_state & SS_NOFDREF) &&
778 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
779 tp = tcp_close(tp);
780 goto dropwithreset;
784 * If segment ends after window, drop trailing data
785 * (and PUSH and FIN); if nothing left, just ACK.
787 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
788 if (todrop > 0) {
789 if (todrop >= ti->ti_len) {
791 * If a new connection request is received
792 * while in TIME_WAIT, drop the old connection
793 * and start over if the sequence numbers
794 * are above the previous ones.
796 if (tiflags & TH_SYN &&
797 tp->t_state == TCPS_TIME_WAIT &&
798 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
799 iss = tp->rcv_nxt + TCP_ISSINCR;
800 tp = tcp_close(tp);
801 goto findso;
804 * If window is closed can only take segments at
805 * window edge, and have to drop data and PUSH from
806 * incoming segments. Continue processing, but
807 * remember to ack. Otherwise, drop segment
808 * and ack.
810 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
811 tp->t_flags |= TF_ACKNOW;
812 } else {
813 goto dropafterack;
816 m_adj(m, -todrop);
817 ti->ti_len -= todrop;
818 tiflags &= ~(TH_PUSH|TH_FIN);
822 * If the RST bit is set examine the state:
823 * SYN_RECEIVED STATE:
824 * If passive open, return to LISTEN state.
825 * If active open, inform user that connection was refused.
826 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
827 * Inform user that connection was reset, and close tcb.
828 * CLOSING, LAST_ACK, TIME_WAIT STATES
829 * Close the tcb.
831 if (tiflags&TH_RST) switch (tp->t_state) {
833 case TCPS_SYN_RECEIVED:
834 case TCPS_ESTABLISHED:
835 case TCPS_FIN_WAIT_1:
836 case TCPS_FIN_WAIT_2:
837 case TCPS_CLOSE_WAIT:
838 tp->t_state = TCPS_CLOSED;
839 tcp_close(tp);
840 goto drop;
842 case TCPS_CLOSING:
843 case TCPS_LAST_ACK:
844 case TCPS_TIME_WAIT:
845 tcp_close(tp);
846 goto drop;
850 * If a SYN is in the window, then this is an
851 * error and we send an RST and drop the connection.
853 if (tiflags & TH_SYN) {
854 tp = tcp_drop(tp,0);
855 goto dropwithreset;
859 * If the ACK bit is off we drop the segment and return.
861 if ((tiflags & TH_ACK) == 0) goto drop;
864 * Ack processing.
866 switch (tp->t_state) {
868 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
869 * ESTABLISHED state and continue processing, otherwise
870 * send an RST. una<=ack<=max
872 case TCPS_SYN_RECEIVED:
874 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
875 SEQ_GT(ti->ti_ack, tp->snd_max))
876 goto dropwithreset;
877 tp->t_state = TCPS_ESTABLISHED;
879 * The sent SYN is ack'ed with our sequence number +1
880 * The first data byte already in the buffer will get
881 * lost if no correction is made. This is only needed for
882 * SS_CTL since the buffer is empty otherwise.
883 * tp->snd_una++; or:
885 tp->snd_una=ti->ti_ack;
886 if (so->so_state & SS_CTL) {
887 /* So tcp_ctl reports the right state */
888 ret = tcp_ctl(so);
889 if (ret == 1) {
890 soisfconnected(so);
891 so->so_state &= ~SS_CTL; /* success XXX */
892 } else if (ret == 2) {
893 so->so_state &= SS_PERSISTENT_MASK;
894 so->so_state |= SS_NOFDREF; /* CTL_CMD */
895 } else {
896 needoutput = 1;
897 tp->t_state = TCPS_FIN_WAIT_1;
899 } else {
900 soisfconnected(so);
903 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
904 tp->snd_wl1 = ti->ti_seq - 1;
905 /* Avoid ack processing; snd_una==ti_ack => dup ack */
906 goto synrx_to_est;
907 /* fall into ... */
910 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
911 * ACKs. If the ack is in the range
912 * tp->snd_una < ti->ti_ack <= tp->snd_max
913 * then advance tp->snd_una to ti->ti_ack and drop
914 * data from the retransmission queue. If this ACK reflects
915 * more up to date window information we update our window information.
917 case TCPS_ESTABLISHED:
918 case TCPS_FIN_WAIT_1:
919 case TCPS_FIN_WAIT_2:
920 case TCPS_CLOSE_WAIT:
921 case TCPS_CLOSING:
922 case TCPS_LAST_ACK:
923 case TCPS_TIME_WAIT:
925 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
926 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
927 DEBUG_MISC((dfd, " dup ack m = %p so = %p\n",
928 m, so));
930 * If we have outstanding data (other than
931 * a window probe), this is a completely
932 * duplicate ack (ie, window info didn't
933 * change), the ack is the biggest we've
934 * seen and we've seen exactly our rexmt
935 * threshold of them, assume a packet
936 * has been dropped and retransmit it.
937 * Kludge snd_nxt & the congestion
938 * window so we send only this one
939 * packet.
941 * We know we're losing at the current
942 * window size so do congestion avoidance
943 * (set ssthresh to half the current window
944 * and pull our congestion window back to
945 * the new ssthresh).
947 * Dup acks mean that packets have left the
948 * network (they're now cached at the receiver)
949 * so bump cwnd by the amount in the receiver
950 * to keep a constant cwnd packets in the
951 * network.
953 if (tp->t_timer[TCPT_REXMT] == 0 ||
954 ti->ti_ack != tp->snd_una)
955 tp->t_dupacks = 0;
956 else if (++tp->t_dupacks == TCPREXMTTHRESH) {
957 tcp_seq onxt = tp->snd_nxt;
958 u_int win =
959 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
960 tp->t_maxseg;
962 if (win < 2)
963 win = 2;
964 tp->snd_ssthresh = win * tp->t_maxseg;
965 tp->t_timer[TCPT_REXMT] = 0;
966 tp->t_rtt = 0;
967 tp->snd_nxt = ti->ti_ack;
968 tp->snd_cwnd = tp->t_maxseg;
969 (void) tcp_output(tp);
970 tp->snd_cwnd = tp->snd_ssthresh +
971 tp->t_maxseg * tp->t_dupacks;
972 if (SEQ_GT(onxt, tp->snd_nxt))
973 tp->snd_nxt = onxt;
974 goto drop;
975 } else if (tp->t_dupacks > TCPREXMTTHRESH) {
976 tp->snd_cwnd += tp->t_maxseg;
977 (void) tcp_output(tp);
978 goto drop;
980 } else
981 tp->t_dupacks = 0;
982 break;
984 synrx_to_est:
986 * If the congestion window was inflated to account
987 * for the other side's cached packets, retract it.
989 if (tp->t_dupacks > TCPREXMTTHRESH &&
990 tp->snd_cwnd > tp->snd_ssthresh)
991 tp->snd_cwnd = tp->snd_ssthresh;
992 tp->t_dupacks = 0;
993 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
994 goto dropafterack;
996 acked = ti->ti_ack - tp->snd_una;
999 * If transmit timer is running and timed sequence
1000 * number was acked, update smoothed round trip time.
1001 * Since we now have an rtt measurement, cancel the
1002 * timer backoff (cf., Phil Karn's retransmit alg.).
1003 * Recompute the initial retransmit timer.
1005 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1006 tcp_xmit_timer(tp,tp->t_rtt);
1009 * If all outstanding data is acked, stop retransmit
1010 * timer and remember to restart (more output or persist).
1011 * If there is more data to be acked, restart retransmit
1012 * timer, using current (possibly backed-off) value.
1014 if (ti->ti_ack == tp->snd_max) {
1015 tp->t_timer[TCPT_REXMT] = 0;
1016 needoutput = 1;
1017 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1018 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1020 * When new data is acked, open the congestion window.
1021 * If the window gives us less than ssthresh packets
1022 * in flight, open exponentially (maxseg per packet).
1023 * Otherwise open linearly: maxseg per window
1024 * (maxseg^2 / cwnd per packet).
1027 register u_int cw = tp->snd_cwnd;
1028 register u_int incr = tp->t_maxseg;
1030 if (cw > tp->snd_ssthresh)
1031 incr = incr * incr / cw;
1032 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1034 if (acked > so->so_snd.sb_cc) {
1035 tp->snd_wnd -= so->so_snd.sb_cc;
1036 sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
1037 ourfinisacked = 1;
1038 } else {
1039 sbdrop(&so->so_snd, acked);
1040 tp->snd_wnd -= acked;
1041 ourfinisacked = 0;
1043 tp->snd_una = ti->ti_ack;
1044 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1045 tp->snd_nxt = tp->snd_una;
1047 switch (tp->t_state) {
1050 * In FIN_WAIT_1 STATE in addition to the processing
1051 * for the ESTABLISHED state if our FIN is now acknowledged
1052 * then enter FIN_WAIT_2.
1054 case TCPS_FIN_WAIT_1:
1055 if (ourfinisacked) {
1057 * If we can't receive any more
1058 * data, then closing user can proceed.
1059 * Starting the timer is contrary to the
1060 * specification, but if we don't get a FIN
1061 * we'll hang forever.
1063 if (so->so_state & SS_FCANTRCVMORE) {
1064 tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
1066 tp->t_state = TCPS_FIN_WAIT_2;
1068 break;
1071 * In CLOSING STATE in addition to the processing for
1072 * the ESTABLISHED state if the ACK acknowledges our FIN
1073 * then enter the TIME-WAIT state, otherwise ignore
1074 * the segment.
1076 case TCPS_CLOSING:
1077 if (ourfinisacked) {
1078 tp->t_state = TCPS_TIME_WAIT;
1079 tcp_canceltimers(tp);
1080 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1082 break;
1085 * In LAST_ACK, we may still be waiting for data to drain
1086 * and/or to be acked, as well as for the ack of our FIN.
1087 * If our FIN is now acknowledged, delete the TCB,
1088 * enter the closed state and return.
1090 case TCPS_LAST_ACK:
1091 if (ourfinisacked) {
1092 tcp_close(tp);
1093 goto drop;
1095 break;
1098 * In TIME_WAIT state the only thing that should arrive
1099 * is a retransmission of the remote FIN. Acknowledge
1100 * it and restart the finack timer.
1102 case TCPS_TIME_WAIT:
1103 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1104 goto dropafterack;
1106 } /* switch(tp->t_state) */
1108 step6:
1110 * Update window information.
1111 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1113 if ((tiflags & TH_ACK) &&
1114 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1115 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1116 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1117 tp->snd_wnd = tiwin;
1118 tp->snd_wl1 = ti->ti_seq;
1119 tp->snd_wl2 = ti->ti_ack;
1120 if (tp->snd_wnd > tp->max_sndwnd)
1121 tp->max_sndwnd = tp->snd_wnd;
1122 needoutput = 1;
1126 * Process segments with URG.
1128 if ((tiflags & TH_URG) && ti->ti_urp &&
1129 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1131 * This is a kludge, but if we receive and accept
1132 * random urgent pointers, we'll crash in
1133 * soreceive. It's hard to imagine someone
1134 * actually wanting to send this much urgent data.
1136 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1137 ti->ti_urp = 0;
1138 tiflags &= ~TH_URG;
1139 goto dodata;
1142 * If this segment advances the known urgent pointer,
1143 * then mark the data stream. This should not happen
1144 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1145 * a FIN has been received from the remote side.
1146 * In these states we ignore the URG.
1148 * According to RFC961 (Assigned Protocols),
1149 * the urgent pointer points to the last octet
1150 * of urgent data. We continue, however,
1151 * to consider it to indicate the first octet
1152 * of data past the urgent section as the original
1153 * spec states (in one of two places).
1155 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1156 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1157 so->so_urgc = so->so_rcv.sb_cc +
1158 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1159 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1162 } else
1164 * If no out of band data is expected,
1165 * pull receive urgent pointer along
1166 * with the receive window.
1168 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1169 tp->rcv_up = tp->rcv_nxt;
1170 dodata:
1173 * If this is a small packet, then ACK now - with Nagel
1174 * congestion avoidance sender won't send more until
1175 * he gets an ACK.
1177 if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1178 ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1179 tp->t_flags |= TF_ACKNOW;
1183 * Process the segment text, merging it into the TCP sequencing queue,
1184 * and arranging for acknowledgment of receipt if necessary.
1185 * This process logically involves adjusting tp->rcv_wnd as data
1186 * is presented to the user (this happens in tcp_usrreq.c,
1187 * case PRU_RCVD). If a FIN has already been received on this
1188 * connection then we just ignore the text.
1190 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1191 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1192 TCP_REASS(tp, ti, m, so, tiflags);
1193 } else {
1194 m_free(m);
1195 tiflags &= ~TH_FIN;
1199 * If FIN is received ACK the FIN and let the user know
1200 * that the connection is closing.
1202 if (tiflags & TH_FIN) {
1203 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1205 * If we receive a FIN we can't send more data,
1206 * set it SS_FDRAIN
1207 * Shutdown the socket if there is no rx data in the
1208 * buffer.
1209 * soread() is called on completion of shutdown() and
1210 * will got to TCPS_LAST_ACK, and use tcp_output()
1211 * to send the FIN.
1213 sofwdrain(so);
1215 tp->t_flags |= TF_ACKNOW;
1216 tp->rcv_nxt++;
1218 switch (tp->t_state) {
1221 * In SYN_RECEIVED and ESTABLISHED STATES
1222 * enter the CLOSE_WAIT state.
1224 case TCPS_SYN_RECEIVED:
1225 case TCPS_ESTABLISHED:
1226 if(so->so_emu == EMU_CTL) /* no shutdown on socket */
1227 tp->t_state = TCPS_LAST_ACK;
1228 else
1229 tp->t_state = TCPS_CLOSE_WAIT;
1230 break;
1233 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1234 * enter the CLOSING state.
1236 case TCPS_FIN_WAIT_1:
1237 tp->t_state = TCPS_CLOSING;
1238 break;
1241 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1242 * starting the time-wait timer, turning off the other
1243 * standard timers.
1245 case TCPS_FIN_WAIT_2:
1246 tp->t_state = TCPS_TIME_WAIT;
1247 tcp_canceltimers(tp);
1248 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1249 break;
1252 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1254 case TCPS_TIME_WAIT:
1255 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1256 break;
1261 * Return any desired output.
1263 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1264 (void) tcp_output(tp);
1266 return;
1268 dropafterack:
1270 * Generate an ACK dropping incoming segment if it occupies
1271 * sequence space, where the ACK reflects our state.
1273 if (tiflags & TH_RST)
1274 goto drop;
1275 m_free(m);
1276 tp->t_flags |= TF_ACKNOW;
1277 (void) tcp_output(tp);
1278 return;
1280 dropwithreset:
1281 /* reuses m if m!=NULL, m_free() unnecessary */
1282 if (tiflags & TH_ACK)
1283 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1284 else {
1285 if (tiflags & TH_SYN) ti->ti_len++;
1286 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1287 TH_RST|TH_ACK);
1290 return;
1292 drop:
1294 * Drop space held by incoming segment and return.
1296 m_free(m);
1299 static void
1300 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1302 uint16_t mss;
1303 int opt, optlen;
1305 DEBUG_CALL("tcp_dooptions");
1306 DEBUG_ARGS((dfd, " tp = %p cnt=%i\n", tp, cnt));
1308 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1309 opt = cp[0];
1310 if (opt == TCPOPT_EOL)
1311 break;
1312 if (opt == TCPOPT_NOP)
1313 optlen = 1;
1314 else {
1315 optlen = cp[1];
1316 if (optlen <= 0)
1317 break;
1319 switch (opt) {
1321 default:
1322 continue;
1324 case TCPOPT_MAXSEG:
1325 if (optlen != TCPOLEN_MAXSEG)
1326 continue;
1327 if (!(ti->ti_flags & TH_SYN))
1328 continue;
1329 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1330 NTOHS(mss);
1331 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1332 break;
1339 * Pull out of band byte out of a segment so
1340 * it doesn't appear in the user's data queue.
1341 * It is still reflected in the segment length for
1342 * sequencing purposes.
1345 #ifdef notdef
1347 void
1348 tcp_pulloutofband(so, ti, m)
1349 struct socket *so;
1350 struct tcpiphdr *ti;
1351 register struct mbuf *m;
1353 int cnt = ti->ti_urp - 1;
1355 while (cnt >= 0) {
1356 if (m->m_len > cnt) {
1357 char *cp = mtod(m, caddr_t) + cnt;
1358 struct tcpcb *tp = sototcpcb(so);
1360 tp->t_iobc = *cp;
1361 tp->t_oobflags |= TCPOOB_HAVEDATA;
1362 memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1363 m->m_len--;
1364 return;
1366 cnt -= m->m_len;
1367 m = m->m_next; /* XXX WRONG! Fix it! */
1368 if (m == 0)
1369 break;
1371 panic("tcp_pulloutofband");
1374 #endif /* notdef */
1377 * Collect new round-trip time estimate
1378 * and update averages and current timeout.
1381 static void
1382 tcp_xmit_timer(register struct tcpcb *tp, int rtt)
1384 register short delta;
1386 DEBUG_CALL("tcp_xmit_timer");
1387 DEBUG_ARG("tp = %p", tp);
1388 DEBUG_ARG("rtt = %d", rtt);
1390 if (tp->t_srtt != 0) {
1392 * srtt is stored as fixed point with 3 bits after the
1393 * binary point (i.e., scaled by 8). The following magic
1394 * is equivalent to the smoothing algorithm in rfc793 with
1395 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1396 * point). Adjust rtt to origin 0.
1398 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1399 if ((tp->t_srtt += delta) <= 0)
1400 tp->t_srtt = 1;
1402 * We accumulate a smoothed rtt variance (actually, a
1403 * smoothed mean difference), then set the retransmit
1404 * timer to smoothed rtt + 4 times the smoothed variance.
1405 * rttvar is stored as fixed point with 2 bits after the
1406 * binary point (scaled by 4). The following is
1407 * equivalent to rfc793 smoothing with an alpha of .75
1408 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1409 * rfc793's wired-in beta.
1411 if (delta < 0)
1412 delta = -delta;
1413 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1414 if ((tp->t_rttvar += delta) <= 0)
1415 tp->t_rttvar = 1;
1416 } else {
1418 * No rtt measurement yet - use the unsmoothed rtt.
1419 * Set the variance to half the rtt (so our first
1420 * retransmit happens at 3*rtt).
1422 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1423 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1425 tp->t_rtt = 0;
1426 tp->t_rxtshift = 0;
1429 * the retransmit should happen at rtt + 4 * rttvar.
1430 * Because of the way we do the smoothing, srtt and rttvar
1431 * will each average +1/2 tick of bias. When we compute
1432 * the retransmit timer, we want 1/2 tick of rounding and
1433 * 1 extra tick because of +-1/2 tick uncertainty in the
1434 * firing of the timer. The bias will give us exactly the
1435 * 1.5 tick we need. But, because the bias is
1436 * statistical, we have to test that we don't drop below
1437 * the minimum feasible timer (which is 2 ticks).
1439 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1440 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1443 * We received an ack for a packet that wasn't retransmitted;
1444 * it is probably safe to discard any error indications we've
1445 * received recently. This isn't quite right, but close enough
1446 * for now (a route might have failed after we sent a segment,
1447 * and the return path might not be symmetrical).
1449 tp->t_softerror = 0;
1453 * Determine a reasonable value for maxseg size.
1454 * If the route is known, check route for mtu.
1455 * If none, use an mss that can be handled on the outgoing
1456 * interface without forcing IP to fragment; if bigger than
1457 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1458 * to utilize large mbufs. If no route is found, route has no mtu,
1459 * or the destination isn't local, use a default, hopefully conservative
1460 * size (usually 512 or the default IP max size, but no more than the mtu
1461 * of the interface), as we can't discover anything about intervening
1462 * gateways or networks. We also initialize the congestion/slow start
1463 * window to be a single segment if the destination isn't local.
1464 * While looking at the routing entry, we also initialize other path-dependent
1465 * parameters from pre-set or cached values in the routing entry.
1469 tcp_mss(struct tcpcb *tp, u_int offer)
1471 struct socket *so = tp->t_socket;
1472 int mss;
1474 DEBUG_CALL("tcp_mss");
1475 DEBUG_ARG("tp = %p", tp);
1476 DEBUG_ARG("offer = %d", offer);
1478 mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr);
1479 if (offer)
1480 mss = min(mss, offer);
1481 mss = max(mss, 32);
1482 if (mss < tp->t_maxseg || offer != 0)
1483 tp->t_maxseg = mss;
1485 tp->snd_cwnd = mss;
1487 sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1488 (mss - (TCP_SNDSPACE % mss)) :
1489 0));
1490 sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1491 (mss - (TCP_RCVSPACE % mss)) :
1492 0));
1494 DEBUG_MISC((dfd, " returning mss = %d\n", mss));
1496 return mss;