tcp_input.c revision 1.7 1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * from: @(#)tcp_input.c 7.25 (Berkeley) 6/30/90
34 * $Id: tcp_input.c,v 1.7 1994/04/12 18:07:46 mycroft Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/select.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/errno.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/tcp.h>
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/tcp_seq.h>
58 #include <netinet/tcp_timer.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/tcpip.h>
61 #include <netinet/tcp_debug.h>
62
63 int tcprexmtthresh = 3;
64 int tcppredack; /* XXX debugging: times hdr predict ok for acks */
65 int tcppreddat; /* XXX # times header prediction ok for data packets */
66 int tcppcbcachemiss;
67 struct tcpiphdr tcp_saveti;
68 struct inpcb *tcp_last_inpcb = &tcb;
69
70 /*
71 * Insert segment ti into reassembly queue of tcp with
72 * control block tp. Return TH_FIN if reassembly now includes
73 * a segment with FIN. The macro form does the common case inline
74 * (segment is the next to be received on an established connection,
75 * and the queue is empty), avoiding linkage into and removal
76 * from the queue and repetition of various conversions.
77 * Set DELACK for segments received in order, but ack immediately
78 * when segments are out of order (so fast retransmit can work).
79 */
80 #define TCP_REASS(tp, ti, m, so, flags) { \
81 if ((ti)->ti_seq == (tp)->rcv_nxt && \
82 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
83 (tp)->t_state == TCPS_ESTABLISHED) { \
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;\
89 sbappend(&(so)->so_rcv, (m)); \
90 sorwakeup(so); \
91 } else { \
92 (flags) = tcp_reass((tp), (ti), (m)); \
93 tp->t_flags |= TF_ACKNOW; \
94 } \
95 }
96
97 int
98 tcp_reass(tp, ti, m)
99 register struct tcpcb *tp;
100 register struct tcpiphdr *ti;
101 struct mbuf *m;
102 {
103 register struct tcpiphdr *q;
104 struct socket *so = tp->t_inpcb->inp_socket;
105 int flags;
106
107 /*
108 * Call with ti==0 after become established to
109 * force pre-ESTABLISHED data up to user socket.
110 */
111 if (ti == 0)
112 goto present;
113
114 /*
115 * Find a segment which begins after this one does.
116 */
117 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
118 q = (struct tcpiphdr *)q->ti_next)
119 if (SEQ_GT(q->ti_seq, ti->ti_seq))
120 break;
121
122 /*
123 * If there is a preceding segment, it may provide some of
124 * our data already. If so, drop the data from the incoming
125 * segment. If it provides all of our data, drop us.
126 */
127 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
128 register int i;
129 q = (struct tcpiphdr *)q->ti_prev;
130 /* conversion to int (in i) handles seq wraparound */
131 i = q->ti_seq + q->ti_len - ti->ti_seq;
132 if (i > 0) {
133 if (i >= ti->ti_len) {
134 tcpstat.tcps_rcvduppack++;
135 tcpstat.tcps_rcvdupbyte += ti->ti_len;
136 m_freem(m);
137 return (0);
138 }
139 m_adj(m, i);
140 ti->ti_len -= i;
141 ti->ti_seq += i;
142 }
143 q = (struct tcpiphdr *)(q->ti_next);
144 }
145 tcpstat.tcps_rcvoopack++;
146 tcpstat.tcps_rcvoobyte += ti->ti_len;
147 REASS_MBUF(ti) = m; /* XXX */
148
149 /*
150 * While we overlap succeeding segments trim them or,
151 * if they are completely covered, dequeue them.
152 */
153 while (q != (struct tcpiphdr *)tp) {
154 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
155 if (i <= 0)
156 break;
157 if (i < q->ti_len) {
158 q->ti_seq += i;
159 q->ti_len -= i;
160 m_adj(REASS_MBUF(q), i);
161 break;
162 }
163 q = (struct tcpiphdr *)q->ti_next;
164 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
165 remque(q->ti_prev);
166 m_freem(m);
167 }
168
169 /*
170 * Stick new segment in its place.
171 */
172 insque(ti, q->ti_prev);
173
174 present:
175 /*
176 * Present data to user, advancing rcv_nxt through
177 * completed sequence space.
178 */
179 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
180 return (0);
181 ti = tp->seg_next;
182 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
183 return (0);
184 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
185 return (0);
186 do {
187 tp->rcv_nxt += ti->ti_len;
188 flags = ti->ti_flags & TH_FIN;
189 remque(ti);
190 m = REASS_MBUF(ti);
191 ti = (struct tcpiphdr *)ti->ti_next;
192 if (so->so_state & SS_CANTRCVMORE)
193 m_freem(m);
194 else
195 sbappend(&so->so_rcv, m);
196 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
197 sorwakeup(so);
198 return (flags);
199 }
200
201 /*
202 * TCP input routine, follows pages 65-76 of the
203 * protocol specification dated September, 1981 very closely.
204 */
205 void
206 tcp_input(m, iphlen)
207 register struct mbuf *m;
208 int iphlen;
209 {
210 register struct tcpiphdr *ti;
211 register struct inpcb *inp;
212 struct mbuf *om = 0;
213 int len, tlen, off;
214 register struct tcpcb *tp = 0;
215 register int tiflags;
216 struct socket *so;
217 int todrop, acked, ourfinisacked, needoutput = 0;
218 short ostate;
219 struct in_addr laddr;
220 int dropsocket = 0;
221 int iss = 0;
222
223 tcpstat.tcps_rcvtotal++;
224 /*
225 * Get IP and TCP header together in first mbuf.
226 * Note: IP leaves IP header in first mbuf.
227 */
228 ti = mtod(m, struct tcpiphdr *);
229 if (iphlen > sizeof (struct ip))
230 ip_stripoptions(m, (struct mbuf *)0);
231 if (m->m_len < sizeof (struct tcpiphdr)) {
232 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
233 tcpstat.tcps_rcvshort++;
234 return;
235 }
236 ti = mtod(m, struct tcpiphdr *);
237 }
238
239 /*
240 * Checksum extended TCP header and data.
241 */
242 tlen = ((struct ip *)ti)->ip_len;
243 len = sizeof (struct ip) + tlen;
244 ti->ti_next = ti->ti_prev = 0;
245 ti->ti_x1 = 0;
246 ti->ti_len = (u_short)tlen;
247 HTONS(ti->ti_len);
248 if (ti->ti_sum = in_cksum(m, len)) {
249 tcpstat.tcps_rcvbadsum++;
250 goto drop;
251 }
252
253 /*
254 * Check that TCP offset makes sense,
255 * pull out TCP options and adjust length. XXX
256 */
257 off = ti->ti_off << 2;
258 if (off < sizeof (struct tcphdr) || off > tlen) {
259 tcpstat.tcps_rcvbadoff++;
260 goto drop;
261 }
262 tlen -= off;
263 ti->ti_len = tlen;
264 if (off > sizeof (struct tcphdr)) {
265 if (m->m_len < sizeof(struct ip) + off) {
266 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
267 tcpstat.tcps_rcvshort++;
268 return;
269 }
270 ti = mtod(m, struct tcpiphdr *);
271 }
272 om = m_get(M_DONTWAIT, MT_DATA);
273 if (om == 0)
274 goto drop;
275 om->m_len = off - sizeof (struct tcphdr);
276 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
277 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
278 m->m_len -= om->m_len;
279 m->m_pkthdr.len -= om->m_len;
280 bcopy(op+om->m_len, op,
281 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
282 }
283 }
284 tiflags = ti->ti_flags;
285
286 /*
287 * Convert TCP protocol specific fields to host format.
288 */
289 NTOHL(ti->ti_seq);
290 NTOHL(ti->ti_ack);
291 NTOHS(ti->ti_win);
292 NTOHS(ti->ti_urp);
293
294 /*
295 * Locate pcb for segment.
296 */
297 findpcb:
298 inp = tcp_last_inpcb;
299 if (inp->inp_lport != ti->ti_dport ||
300 inp->inp_fport != ti->ti_sport ||
301 inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
302 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
303 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
304 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
305 if (inp)
306 tcp_last_inpcb = inp;
307 ++tcppcbcachemiss;
308 }
309
310 /*
311 * If the state is CLOSED (i.e., TCB does not exist) then
312 * all data in the incoming segment is discarded.
313 * If the TCB exists but is in CLOSED state, it is embryonic,
314 * but should either do a listen or a connect soon.
315 */
316 if (inp == 0)
317 goto dropwithreset;
318 tp = intotcpcb(inp);
319 if (tp == 0)
320 goto dropwithreset;
321 if (tp->t_state == TCPS_CLOSED)
322 goto drop;
323 so = inp->inp_socket;
324 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
325 if (so->so_options & SO_DEBUG) {
326 ostate = tp->t_state;
327 tcp_saveti = *ti;
328 }
329 if (so->so_options & SO_ACCEPTCONN) {
330 so = sonewconn(so, 0);
331 if (so == 0)
332 goto drop;
333 /*
334 * This is ugly, but ....
335 *
336 * Mark socket as temporary until we're
337 * committed to keeping it. The code at
338 * ``drop'' and ``dropwithreset'' check the
339 * flag dropsocket to see if the temporary
340 * socket created here should be discarded.
341 * We mark the socket as discardable until
342 * we're committed to it below in TCPS_LISTEN.
343 */
344 dropsocket++;
345 inp = (struct inpcb *)so->so_pcb;
346 inp->inp_laddr = ti->ti_dst;
347 inp->inp_lport = ti->ti_dport;
348 #if BSD>=43
349 inp->inp_options = ip_srcroute();
350 #endif
351 tp = intotcpcb(inp);
352 tp->t_state = TCPS_LISTEN;
353 }
354 }
355
356 /*
357 * Segment received on connection.
358 * Reset idle time and keep-alive timer.
359 */
360 tp->t_idle = 0;
361 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
362
363 /*
364 * Process options if not in LISTEN state,
365 * else do it below (after getting remote address).
366 */
367 if (om && tp->t_state != TCPS_LISTEN) {
368 tcp_dooptions(tp, om, ti);
369 om = 0;
370 }
371 /*
372 * Header prediction: check for the two common cases
373 * of a uni-directional data xfer. If the packet has
374 * no control flags, is in-sequence, the window didn't
375 * change and we're not retransmitting, it's a
376 * candidate. If the length is zero and the ack moved
377 * forward, we're the sender side of the xfer. Just
378 * free the data acked & wake any higher level process
379 * that was blocked waiting for space. If the length
380 * is non-zero and the ack didn't move, we're the
381 * receiver side. If we're getting packets in-order
382 * (the reassembly queue is empty), add the data to
383 * the socket buffer and note that we need a delayed ack.
384 */
385 if (tp->t_state == TCPS_ESTABLISHED &&
386 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
387 ti->ti_seq == tp->rcv_nxt &&
388 ti->ti_win && ti->ti_win == tp->snd_wnd &&
389 tp->snd_nxt == tp->snd_max) {
390 if (ti->ti_len == 0) {
391 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
392 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
393 tp->snd_cwnd >= tp->snd_wnd) {
394 /*
395 * this is a pure ack for outstanding data.
396 */
397 ++tcppredack;
398 if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq))
399 tcp_xmit_timer(tp);
400 acked = ti->ti_ack - tp->snd_una;
401 tcpstat.tcps_rcvackpack++;
402 tcpstat.tcps_rcvackbyte += acked;
403 sbdrop(&so->so_snd, acked);
404 tp->snd_una = ti->ti_ack;
405 m_freem(m);
406
407 /*
408 * If all outstanding data are acked, stop
409 * retransmit timer, otherwise restart timer
410 * using current (possibly backed-off) value.
411 * If process is waiting for space,
412 * wakeup/selwakeup/signal. If data
413 * are ready to send, let tcp_output
414 * decide between more output or persist.
415 */
416 if (tp->snd_una == tp->snd_max)
417 tp->t_timer[TCPT_REXMT] = 0;
418 else if (tp->t_timer[TCPT_PERSIST] == 0)
419 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
420
421 if (so->so_snd.sb_flags & SB_NOTIFY)
422 sowwakeup(so);
423 if (so->so_snd.sb_cc)
424 (void) tcp_output(tp);
425 return;
426 }
427 } else if (ti->ti_ack == tp->snd_una &&
428 tp->seg_next == (struct tcpiphdr *)tp &&
429 ti->ti_len <= sbspace(&so->so_rcv)) {
430 /*
431 * this is a pure, in-sequence data packet
432 * with nothing on the reassembly queue and
433 * we have enough buffer space to take it.
434 */
435 ++tcppreddat;
436 tp->rcv_nxt += ti->ti_len;
437 tcpstat.tcps_rcvpack++;
438 tcpstat.tcps_rcvbyte += ti->ti_len;
439 /*
440 * Drop TCP and IP headers then add data
441 * to socket buffer
442 */
443 m->m_data += sizeof(struct tcpiphdr);
444 m->m_len -= sizeof(struct tcpiphdr);
445 sbappend(&so->so_rcv, m);
446 sorwakeup(so);
447 tp->t_flags |= TF_DELACK;
448 return;
449 }
450 }
451
452 /*
453 * Drop TCP and IP headers; TCP options were dropped above.
454 */
455 m->m_data += sizeof(struct tcpiphdr);
456 m->m_len -= sizeof(struct tcpiphdr);
457
458 /*
459 * Calculate amount of space in receive window,
460 * and then do TCP input processing.
461 * Receive window is amount of space in rcv queue,
462 * but not less than advertised window.
463 */
464 { int win;
465
466 win = sbspace(&so->so_rcv);
467 if (win < 0)
468 win = 0;
469 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
470 }
471
472 switch (tp->t_state) {
473
474 /*
475 * If the state is LISTEN then ignore segment if it contains an RST.
476 * If the segment contains an ACK then it is bad and send a RST.
477 * If it does not contain a SYN then it is not interesting; drop it.
478 * Don't bother responding if the destination was a broadcast.
479 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
480 * tp->iss, and send a segment:
481 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
482 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
483 * Fill in remote peer address fields if not previously specified.
484 * Enter SYN_RECEIVED state, and process any other fields of this
485 * segment in this state.
486 */
487 case TCPS_LISTEN: {
488 struct mbuf *am;
489 register struct sockaddr_in *sin;
490
491 if (tiflags & TH_RST)
492 goto drop;
493 if (tiflags & TH_ACK)
494 goto dropwithreset;
495 if ((tiflags & TH_SYN) == 0)
496 goto drop;
497 if (m->m_flags & M_BCAST)
498 goto drop;
499 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
500 if (am == NULL)
501 goto drop;
502 am->m_len = sizeof (struct sockaddr_in);
503 sin = mtod(am, struct sockaddr_in *);
504 sin->sin_family = AF_INET;
505 sin->sin_len = sizeof(*sin);
506 sin->sin_addr = ti->ti_src;
507 sin->sin_port = ti->ti_sport;
508 laddr = inp->inp_laddr;
509 if (inp->inp_laddr.s_addr == INADDR_ANY)
510 inp->inp_laddr = ti->ti_dst;
511 if (in_pcbconnect(inp, am)) {
512 inp->inp_laddr = laddr;
513 (void) m_free(am);
514 goto drop;
515 }
516 (void) m_free(am);
517 tp->t_template = tcp_template(tp);
518 if (tp->t_template == 0) {
519 tp = tcp_drop(tp, ENOBUFS);
520 dropsocket = 0; /* socket is already gone */
521 goto drop;
522 }
523 if (om) {
524 tcp_dooptions(tp, om, ti);
525 om = 0;
526 }
527 if (iss)
528 tp->iss = iss;
529 else
530 tp->iss = tcp_iss;
531 tcp_iss += TCP_ISSINCR/2;
532 tp->irs = ti->ti_seq;
533 tcp_sendseqinit(tp);
534 tcp_rcvseqinit(tp);
535 tp->t_flags |= TF_ACKNOW;
536 tp->t_state = TCPS_SYN_RECEIVED;
537 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
538 dropsocket = 0; /* committed to socket */
539 tcpstat.tcps_accepts++;
540 goto trimthenstep6;
541 }
542
543 /*
544 * If the state is SYN_SENT:
545 * if seg contains an ACK, but not for our SYN, drop the input.
546 * if seg contains a RST, then drop the connection.
547 * if seg does not contain SYN, then drop it.
548 * Otherwise this is an acceptable SYN segment
549 * initialize tp->rcv_nxt and tp->irs
550 * if seg contains ack then advance tp->snd_una
551 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
552 * arrange for segment to be acked (eventually)
553 * continue processing rest of data/controls, beginning with URG
554 */
555 case TCPS_SYN_SENT:
556 if ((tiflags & TH_ACK) &&
557 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
558 SEQ_GT(ti->ti_ack, tp->snd_max)))
559 goto dropwithreset;
560 if (tiflags & TH_RST) {
561 if (tiflags & TH_ACK)
562 tp = tcp_drop(tp, ECONNREFUSED);
563 goto drop;
564 }
565 if ((tiflags & TH_SYN) == 0)
566 goto drop;
567 if (tiflags & TH_ACK) {
568 tp->snd_una = ti->ti_ack;
569 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
570 tp->snd_nxt = tp->snd_una;
571 }
572 tp->t_timer[TCPT_REXMT] = 0;
573 tp->irs = ti->ti_seq;
574 tcp_rcvseqinit(tp);
575 tp->t_flags |= TF_ACKNOW;
576 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
577 tcpstat.tcps_connects++;
578 soisconnected(so);
579 tp->t_state = TCPS_ESTABLISHED;
580 (void) tcp_reass(tp, (struct tcpiphdr *)0,
581 (struct mbuf *)0);
582 /*
583 * if we didn't have to retransmit the SYN,
584 * use its rtt as our initial srtt & rtt var.
585 */
586 if (tp->t_rtt)
587 tcp_xmit_timer(tp);
588 } else
589 tp->t_state = TCPS_SYN_RECEIVED;
590
591 trimthenstep6:
592 /*
593 * Advance ti->ti_seq to correspond to first data byte.
594 * If data, trim to stay within window,
595 * dropping FIN if necessary.
596 */
597 ti->ti_seq++;
598 if (ti->ti_len > tp->rcv_wnd) {
599 todrop = ti->ti_len - tp->rcv_wnd;
600 m_adj(m, -todrop);
601 ti->ti_len = tp->rcv_wnd;
602 tiflags &= ~TH_FIN;
603 tcpstat.tcps_rcvpackafterwin++;
604 tcpstat.tcps_rcvbyteafterwin += todrop;
605 }
606 tp->snd_wl1 = ti->ti_seq - 1;
607 tp->rcv_up = ti->ti_seq;
608 goto step6;
609 }
610
611 /*
612 * States other than LISTEN or SYN_SENT.
613 * First check that at least some bytes of segment are within
614 * receive window. If segment begins before rcv_nxt,
615 * drop leading data (and SYN); if nothing left, just ack.
616 */
617 todrop = tp->rcv_nxt - ti->ti_seq;
618 if (todrop > 0) {
619 if (tiflags & TH_SYN) {
620 tiflags &= ~TH_SYN;
621 ti->ti_seq++;
622 if (ti->ti_urp > 1)
623 ti->ti_urp--;
624 else
625 tiflags &= ~TH_URG;
626 todrop--;
627 }
628 if (todrop > ti->ti_len ||
629 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
630 /*
631 * Any valid FIN must be to the left of the
632 * window. At this point, FIN must be a
633 * duplicate or out-of-sequence, so drop it.
634 */
635 tiflags &= ~TH_FIN;
636 /*
637 * Send ACK to resynchronize, and drop any data,
638 * but keep on processing for RST or ACK.
639 */
640 tp->t_flags |= TF_ACKNOW;
641 tcpstat.tcps_rcvdupbyte += todrop = ti->ti_len;
642 tcpstat.tcps_rcvduppack++;
643 } else {
644 tcpstat.tcps_rcvpartduppack++;
645 tcpstat.tcps_rcvpartdupbyte += todrop;
646 }
647 m_adj(m, todrop);
648 ti->ti_seq += todrop;
649 ti->ti_len -= todrop;
650 if (ti->ti_urp > todrop)
651 ti->ti_urp -= todrop;
652 else {
653 tiflags &= ~TH_URG;
654 ti->ti_urp = 0;
655 }
656 }
657
658 /*
659 * If new data are received on a connection after the
660 * user processes are gone, then RST the other end.
661 */
662 if ((so->so_state & SS_NOFDREF) &&
663 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
664 tp = tcp_close(tp);
665 tcpstat.tcps_rcvafterclose++;
666 goto dropwithreset;
667 }
668
669 /*
670 * If segment ends after window, drop trailing data
671 * (and PUSH and FIN); if nothing left, just ACK.
672 */
673 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
674 if (todrop > 0) {
675 tcpstat.tcps_rcvpackafterwin++;
676 if (todrop >= ti->ti_len) {
677 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
678 /*
679 * If a new connection request is received
680 * while in TIME_WAIT, drop the old connection
681 * and start over if the sequence numbers
682 * are above the previous ones.
683 */
684 if (tiflags & TH_SYN &&
685 tp->t_state == TCPS_TIME_WAIT &&
686 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
687 iss = tp->rcv_nxt + TCP_ISSINCR;
688 tp = tcp_close(tp);
689 goto findpcb;
690 }
691 /*
692 * If window is closed can only take segments at
693 * window edge, and have to drop data and PUSH from
694 * incoming segments. Continue processing, but
695 * remember to ack. Otherwise, drop segment
696 * and ack.
697 */
698 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
699 tp->t_flags |= TF_ACKNOW;
700 tcpstat.tcps_rcvwinprobe++;
701 } else
702 goto dropafterack;
703 } else
704 tcpstat.tcps_rcvbyteafterwin += todrop;
705 m_adj(m, -todrop);
706 ti->ti_len -= todrop;
707 tiflags &= ~(TH_PUSH|TH_FIN);
708 }
709
710 /*
711 * If the RST bit is set examine the state:
712 * SYN_RECEIVED STATE:
713 * If passive open, return to LISTEN state.
714 * If active open, inform user that connection was refused.
715 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
716 * Inform user that connection was reset, and close tcb.
717 * CLOSING, LAST_ACK, TIME_WAIT STATES
718 * Close the tcb.
719 */
720 if (tiflags&TH_RST) switch (tp->t_state) {
721
722 case TCPS_SYN_RECEIVED:
723 so->so_error = ECONNREFUSED;
724 goto close;
725
726 case TCPS_ESTABLISHED:
727 case TCPS_FIN_WAIT_1:
728 case TCPS_FIN_WAIT_2:
729 case TCPS_CLOSE_WAIT:
730 so->so_error = ECONNRESET;
731 close:
732 tp->t_state = TCPS_CLOSED;
733 tcpstat.tcps_drops++;
734 tp = tcp_close(tp);
735 goto drop;
736
737 case TCPS_CLOSING:
738 case TCPS_LAST_ACK:
739 case TCPS_TIME_WAIT:
740 tp = tcp_close(tp);
741 goto drop;
742 }
743
744 /*
745 * If a SYN is in the window, then this is an
746 * error and we send an RST and drop the connection.
747 */
748 if (tiflags & TH_SYN) {
749 tp = tcp_drop(tp, ECONNRESET);
750 goto dropwithreset;
751 }
752
753 /*
754 * If the ACK bit is off we drop the segment and return.
755 */
756 if ((tiflags & TH_ACK) == 0)
757 goto drop;
758
759 /*
760 * Ack processing.
761 */
762 switch (tp->t_state) {
763
764 /*
765 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
766 * ESTABLISHED state and continue processing, otherwise
767 * send an RST.
768 */
769 case TCPS_SYN_RECEIVED:
770 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
771 SEQ_GT(ti->ti_ack, tp->snd_max))
772 goto dropwithreset;
773 tcpstat.tcps_connects++;
774 soisconnected(so);
775 tp->t_state = TCPS_ESTABLISHED;
776 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
777 tp->snd_wl1 = ti->ti_seq - 1;
778 /* fall into ... */
779
780 /*
781 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
782 * ACKs. If the ack is in the range
783 * tp->snd_una < ti->ti_ack <= tp->snd_max
784 * then advance tp->snd_una to ti->ti_ack and drop
785 * data from the retransmission queue. If this ACK reflects
786 * more up to date window information we update our window information.
787 */
788 case TCPS_ESTABLISHED:
789 case TCPS_FIN_WAIT_1:
790 case TCPS_FIN_WAIT_2:
791 case TCPS_CLOSE_WAIT:
792 case TCPS_CLOSING:
793 case TCPS_LAST_ACK:
794 case TCPS_TIME_WAIT:
795
796 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
797 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
798 tcpstat.tcps_rcvdupack++;
799 /*
800 * If we have outstanding data (other than
801 * a window probe), this is a completely
802 * duplicate ack (ie, window info didn't
803 * change), the ack is the biggest we've
804 * seen and we've seen exactly our rexmt
805 * threshhold of them, assume a packet
806 * has been dropped and retransmit it.
807 * Kludge snd_nxt & the congestion
808 * window so we send only this one
809 * packet.
810 *
811 * We know we're losing at the current
812 * window size so do congestion avoidance
813 * (set ssthresh to half the current window
814 * and pull our congestion window back to
815 * the new ssthresh).
816 *
817 * Dup acks mean that packets have left the
818 * network (they're now cached at the receiver)
819 * so bump cwnd by the amount in the receiver
820 * to keep a constant cwnd packets in the
821 * network.
822 */
823 if (tp->t_timer[TCPT_REXMT] == 0 ||
824 ti->ti_ack != tp->snd_una)
825 tp->t_dupacks = 0;
826 else if (++tp->t_dupacks == tcprexmtthresh) {
827 tcp_seq onxt = tp->snd_nxt;
828 u_int win =
829 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
830 tp->t_maxseg;
831
832 if (win < 2)
833 win = 2;
834 tp->snd_ssthresh = win * tp->t_maxseg;
835 tp->t_timer[TCPT_REXMT] = 0;
836 tp->t_rtt = 0;
837 tp->snd_nxt = ti->ti_ack;
838 tp->snd_cwnd = tp->t_maxseg;
839 (void) tcp_output(tp);
840 tp->snd_cwnd = tp->snd_ssthresh +
841 tp->t_maxseg * tp->t_dupacks;
842 if (SEQ_GT(onxt, tp->snd_nxt))
843 tp->snd_nxt = onxt;
844 goto drop;
845 } else if (tp->t_dupacks > tcprexmtthresh) {
846 tp->snd_cwnd += tp->t_maxseg;
847 (void) tcp_output(tp);
848 goto drop;
849 }
850 } else
851 tp->t_dupacks = 0;
852 break;
853 }
854 /*
855 * If the congestion window was inflated to account
856 * for the other side's cached packets, retract it.
857 */
858 if (tp->t_dupacks > tcprexmtthresh &&
859 tp->snd_cwnd > tp->snd_ssthresh)
860 tp->snd_cwnd = tp->snd_ssthresh;
861 tp->t_dupacks = 0;
862 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
863 tcpstat.tcps_rcvacktoomuch++;
864 goto dropafterack;
865 }
866 acked = ti->ti_ack - tp->snd_una;
867 tcpstat.tcps_rcvackpack++;
868 tcpstat.tcps_rcvackbyte += acked;
869
870 /*
871 * If transmit timer is running and timed sequence
872 * number was acked, update smoothed round trip time.
873 * Since we now have an rtt measurement, cancel the
874 * timer backoff (cf., Phil Karn's retransmit alg.).
875 * Recompute the initial retransmit timer.
876 */
877 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
878 tcp_xmit_timer(tp);
879
880 /*
881 * If all outstanding data is acked, stop retransmit
882 * timer and remember to restart (more output or persist).
883 * If there is more data to be acked, restart retransmit
884 * timer, using current (possibly backed-off) value.
885 */
886 if (ti->ti_ack == tp->snd_max) {
887 tp->t_timer[TCPT_REXMT] = 0;
888 needoutput = 1;
889 } else if (tp->t_timer[TCPT_PERSIST] == 0)
890 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
891 /*
892 * When new data is acked, open the congestion window.
893 * If the window gives us less than ssthresh packets
894 * in flight, open exponentially (maxseg per packet).
895 * Otherwise open linearly: maxseg per window
896 * (maxseg^2 / cwnd per packet), plus a constant
897 * fraction of a packet (maxseg/8) to help larger windows
898 * open quickly enough.
899 */
900 {
901 register u_int cw = tp->snd_cwnd;
902 register u_int incr = tp->t_maxseg;
903
904 if (cw > tp->snd_ssthresh)
905 incr = incr * incr / cw + incr / 8;
906 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
907 }
908 if (acked > so->so_snd.sb_cc) {
909 tp->snd_wnd -= so->so_snd.sb_cc;
910 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
911 ourfinisacked = 1;
912 } else {
913 sbdrop(&so->so_snd, acked);
914 tp->snd_wnd -= acked;
915 ourfinisacked = 0;
916 }
917 if (so->so_snd.sb_flags & SB_NOTIFY)
918 sowwakeup(so);
919 tp->snd_una = ti->ti_ack;
920 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
921 tp->snd_nxt = tp->snd_una;
922
923 switch (tp->t_state) {
924
925 /*
926 * In FIN_WAIT_1 STATE in addition to the processing
927 * for the ESTABLISHED state if our FIN is now acknowledged
928 * then enter FIN_WAIT_2.
929 */
930 case TCPS_FIN_WAIT_1:
931 if (ourfinisacked) {
932 /*
933 * If we can't receive any more
934 * data, then closing user can proceed.
935 * Starting the timer is contrary to the
936 * specification, but if we don't get a FIN
937 * we'll hang forever.
938 */
939 if (so->so_state & SS_CANTRCVMORE) {
940 soisdisconnected(so);
941 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
942 }
943 tp->t_state = TCPS_FIN_WAIT_2;
944 }
945 break;
946
947 /*
948 * In CLOSING STATE in addition to the processing for
949 * the ESTABLISHED state if the ACK acknowledges our FIN
950 * then enter the TIME-WAIT state, otherwise ignore
951 * the segment.
952 */
953 case TCPS_CLOSING:
954 if (ourfinisacked) {
955 tp->t_state = TCPS_TIME_WAIT;
956 tcp_canceltimers(tp);
957 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
958 soisdisconnected(so);
959 }
960 break;
961
962 /*
963 * In LAST_ACK, we may still be waiting for data to drain
964 * and/or to be acked, as well as for the ack of our FIN.
965 * If our FIN is now acknowledged, delete the TCB,
966 * enter the closed state and return.
967 */
968 case TCPS_LAST_ACK:
969 if (ourfinisacked) {
970 tp = tcp_close(tp);
971 goto drop;
972 }
973 break;
974
975 /*
976 * In TIME_WAIT state the only thing that should arrive
977 * is a retransmission of the remote FIN. Acknowledge
978 * it and restart the finack timer.
979 */
980 case TCPS_TIME_WAIT:
981 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
982 goto dropafterack;
983 }
984 }
985
986 step6:
987 /*
988 * Update window information.
989 * Don't look at window if no ACK: TAC's send garbage on first SYN.
990 */
991 if ((tiflags & TH_ACK) &&
992 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
993 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
994 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
995 /* keep track of pure window updates */
996 if (ti->ti_len == 0 &&
997 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
998 tcpstat.tcps_rcvwinupd++;
999 tp->snd_wnd = ti->ti_win;
1000 tp->snd_wl1 = ti->ti_seq;
1001 tp->snd_wl2 = ti->ti_ack;
1002 if (tp->snd_wnd > tp->max_sndwnd)
1003 tp->max_sndwnd = tp->snd_wnd;
1004 needoutput = 1;
1005 }
1006
1007 /*
1008 * Process segments with URG.
1009 */
1010 if ((tiflags & TH_URG) && ti->ti_urp &&
1011 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1012 /*
1013 * This is a kludge, but if we receive and accept
1014 * random urgent pointers, we'll crash in
1015 * soreceive. It's hard to imagine someone
1016 * actually wanting to send this much urgent data.
1017 */
1018 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
1019 ti->ti_urp = 0; /* XXX */
1020 tiflags &= ~TH_URG; /* XXX */
1021 goto dodata; /* XXX */
1022 }
1023 /*
1024 * If this segment advances the known urgent pointer,
1025 * then mark the data stream. This should not happen
1026 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1027 * a FIN has been received from the remote side.
1028 * In these states we ignore the URG.
1029 *
1030 * According to RFC961 (Assigned Protocols),
1031 * the urgent pointer points to the last octet
1032 * of urgent data. We continue, however,
1033 * to consider it to indicate the first octet
1034 * of data past the urgent section as the original
1035 * spec states (in one of two places).
1036 */
1037 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1038 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1039 so->so_oobmark = so->so_rcv.sb_cc +
1040 (tp->rcv_up - tp->rcv_nxt) - 1;
1041 if (so->so_oobmark == 0)
1042 so->so_state |= SS_RCVATMARK;
1043 sohasoutofband(so);
1044 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1045 }
1046 /*
1047 * Remove out of band data so doesn't get presented to user.
1048 * This can happen independent of advancing the URG pointer,
1049 * but if two URG's are pending at once, some out-of-band
1050 * data may creep in... ick.
1051 */
1052 if (ti->ti_urp <= ti->ti_len
1053 #ifdef SO_OOBINLINE
1054 && (so->so_options & SO_OOBINLINE) == 0
1055 #endif
1056 )
1057 tcp_pulloutofband(so, ti, m);
1058 } else
1059 /*
1060 * If no out of band data is expected,
1061 * pull receive urgent pointer along
1062 * with the receive window.
1063 */
1064 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1065 tp->rcv_up = tp->rcv_nxt;
1066 dodata: /* XXX */
1067
1068 /*
1069 * Process the segment text, merging it into the TCP sequencing queue,
1070 * and arranging for acknowledgment of receipt if necessary.
1071 * This process logically involves adjusting tp->rcv_wnd as data
1072 * is presented to the user (this happens in tcp_usrreq.c,
1073 * case PRU_RCVD). If a FIN has already been received on this
1074 * connection then we just ignore the text.
1075 */
1076 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1077 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1078 TCP_REASS(tp, ti, m, so, tiflags);
1079 /*
1080 * Note the amount of data that peer has sent into
1081 * our window, in order to estimate the sender's
1082 * buffer size.
1083 */
1084 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1085 } else {
1086 m_freem(m);
1087 tiflags &= ~TH_FIN;
1088 }
1089
1090 /*
1091 * If FIN is received ACK the FIN and let the user know
1092 * that the connection is closing.
1093 */
1094 if (tiflags & TH_FIN) {
1095 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1096 socantrcvmore(so);
1097 tp->t_flags |= TF_ACKNOW;
1098 tp->rcv_nxt++;
1099 }
1100 switch (tp->t_state) {
1101
1102 /*
1103 * In SYN_RECEIVED and ESTABLISHED STATES
1104 * enter the CLOSE_WAIT state.
1105 */
1106 case TCPS_SYN_RECEIVED:
1107 case TCPS_ESTABLISHED:
1108 tp->t_state = TCPS_CLOSE_WAIT;
1109 break;
1110
1111 /*
1112 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1113 * enter the CLOSING state.
1114 */
1115 case TCPS_FIN_WAIT_1:
1116 tp->t_state = TCPS_CLOSING;
1117 break;
1118
1119 /*
1120 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1121 * starting the time-wait timer, turning off the other
1122 * standard timers.
1123 */
1124 case TCPS_FIN_WAIT_2:
1125 tp->t_state = TCPS_TIME_WAIT;
1126 tcp_canceltimers(tp);
1127 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1128 soisdisconnected(so);
1129 break;
1130
1131 /*
1132 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1133 */
1134 case TCPS_TIME_WAIT:
1135 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1136 break;
1137 }
1138 }
1139 if (so->so_options & SO_DEBUG)
1140 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1141
1142 /*
1143 * Return any desired output.
1144 */
1145 if (needoutput || (tp->t_flags & TF_ACKNOW))
1146 (void) tcp_output(tp);
1147 return;
1148
1149 dropafterack:
1150 /*
1151 * Generate an ACK dropping incoming segment if it occupies
1152 * sequence space, where the ACK reflects our state.
1153 */
1154 if (tiflags & TH_RST)
1155 goto drop;
1156 m_freem(m);
1157 tp->t_flags |= TF_ACKNOW;
1158 (void) tcp_output(tp);
1159 return;
1160
1161 dropwithreset:
1162 if (om) {
1163 (void) m_free(om);
1164 om = 0;
1165 }
1166 /*
1167 * Generate a RST, dropping incoming segment.
1168 * Make ACK acceptable to originator of segment.
1169 * Don't bother to respond if destination was broadcast.
1170 */
1171 if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
1172 goto drop;
1173 if (tiflags & TH_ACK)
1174 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1175 else {
1176 if (tiflags & TH_SYN)
1177 ti->ti_len++;
1178 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1179 TH_RST|TH_ACK);
1180 }
1181 /* destroy temporarily created socket */
1182 if (dropsocket)
1183 (void) soabort(so);
1184 return;
1185
1186 drop:
1187 if (om)
1188 (void) m_free(om);
1189 /*
1190 * Drop space held by incoming segment and return.
1191 */
1192 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1193 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1194 m_freem(m);
1195 /* destroy temporarily created socket */
1196 if (dropsocket)
1197 (void) soabort(so);
1198 return;
1199 }
1200
1201 void
1202 tcp_dooptions(tp, om, ti)
1203 struct tcpcb *tp;
1204 struct mbuf *om;
1205 struct tcpiphdr *ti;
1206 {
1207 register u_char *cp;
1208 u_short mss;
1209 int opt, optlen, cnt;
1210
1211 cp = mtod(om, u_char *);
1212 cnt = om->m_len;
1213 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1214 opt = cp[0];
1215 if (opt == TCPOPT_EOL)
1216 break;
1217 if (opt == TCPOPT_NOP)
1218 optlen = 1;
1219 else {
1220 optlen = cp[1];
1221 if (optlen <= 0)
1222 break;
1223 }
1224 switch (opt) {
1225
1226 default:
1227 continue;
1228
1229 case TCPOPT_MAXSEG:
1230 if (optlen != 4)
1231 continue;
1232 if (!(ti->ti_flags & TH_SYN))
1233 continue;
1234 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1235 NTOHS(mss);
1236 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1237 break;
1238 }
1239 }
1240 (void) m_free(om);
1241 }
1242
1243 /*
1244 * Pull out of band byte out of a segment so
1245 * it doesn't appear in the user's data queue.
1246 * It is still reflected in the segment length for
1247 * sequencing purposes.
1248 */
1249 void
1250 tcp_pulloutofband(so, ti, m)
1251 struct socket *so;
1252 struct tcpiphdr *ti;
1253 register struct mbuf *m;
1254 {
1255 int cnt = ti->ti_urp - 1;
1256
1257 while (cnt >= 0) {
1258 if (m->m_len > cnt) {
1259 char *cp = mtod(m, caddr_t) + cnt;
1260 struct tcpcb *tp = sototcpcb(so);
1261
1262 tp->t_iobc = *cp;
1263 tp->t_oobflags |= TCPOOB_HAVEDATA;
1264 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1265 m->m_len--;
1266 return;
1267 }
1268 cnt -= m->m_len;
1269 m = m->m_next;
1270 if (m == 0)
1271 break;
1272 }
1273 panic("tcp_pulloutofband");
1274 }
1275
1276 /*
1277 * Collect new round-trip time estimate
1278 * and update averages and current timeout.
1279 */
1280 void
1281 tcp_xmit_timer(tp)
1282 register struct tcpcb *tp;
1283 {
1284 register short delta;
1285
1286 tcpstat.tcps_rttupdated++;
1287 if (tp->t_srtt != 0) {
1288 /*
1289 * srtt is stored as fixed point with 3 bits after the
1290 * binary point (i.e., scaled by 8). The following magic
1291 * is equivalent to the smoothing algorithm in rfc793 with
1292 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1293 * point). Adjust t_rtt to origin 0.
1294 */
1295 delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1296 if ((tp->t_srtt += delta) <= 0)
1297 tp->t_srtt = 1;
1298 /*
1299 * We accumulate a smoothed rtt variance (actually, a
1300 * smoothed mean difference), then set the retransmit
1301 * timer to smoothed rtt + 4 times the smoothed variance.
1302 * rttvar is stored as fixed point with 2 bits after the
1303 * binary point (scaled by 4). The following is
1304 * equivalent to rfc793 smoothing with an alpha of .75
1305 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1306 * rfc793's wired-in beta.
1307 */
1308 if (delta < 0)
1309 delta = -delta;
1310 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1311 if ((tp->t_rttvar += delta) <= 0)
1312 tp->t_rttvar = 1;
1313 } else {
1314 /*
1315 * No rtt measurement yet - use the unsmoothed rtt.
1316 * Set the variance to half the rtt (so our first
1317 * retransmit happens at 2*rtt)
1318 */
1319 tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT;
1320 tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1);
1321 }
1322 tp->t_rtt = 0;
1323 tp->t_rxtshift = 0;
1324
1325 /*
1326 * the retransmit should happen at rtt + 4 * rttvar.
1327 * Because of the way we do the smoothing, srtt and rttvar
1328 * will each average +1/2 tick of bias. When we compute
1329 * the retransmit timer, we want 1/2 tick of rounding and
1330 * 1 extra tick because of +-1/2 tick uncertainty in the
1331 * firing of the timer. The bias will give us exactly the
1332 * 1.5 tick we need. But, because the bias is
1333 * statistical, we have to test that we don't drop below
1334 * the minimum feasible timer (which is 2 ticks).
1335 */
1336 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1337 tp->t_rttmin, TCPTV_REXMTMAX);
1338
1339 /*
1340 * We received an ack for a packet that wasn't retransmitted;
1341 * it is probably safe to discard any error indications we've
1342 * received recently. This isn't quite right, but close enough
1343 * for now (a route might have failed after we sent a segment,
1344 * and the return path might not be symmetrical).
1345 */
1346 tp->t_softerror = 0;
1347 }
1348
1349 /*
1350 * Determine a reasonable value for maxseg size.
1351 * If the route is known, check route for mtu.
1352 * If none, use an mss that can be handled on the outgoing
1353 * interface without forcing IP to fragment; if bigger than
1354 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1355 * to utilize large mbufs. If no route is found, route has no mtu,
1356 * or the destination isn't local, use a default, hopefully conservative
1357 * size (usually 512 or the default IP max size, but no more than the mtu
1358 * of the interface), as we can't discover anything about intervening
1359 * gateways or networks. We also initialize the congestion/slow start
1360 * window to be a single segment if the destination isn't local.
1361 * While looking at the routing entry, we also initialize other path-dependent
1362 * parameters from pre-set or cached values in the routing entry.
1363 */
1364 int
1365 tcp_mss(tp, offer)
1366 register struct tcpcb *tp;
1367 u_short offer;
1368 {
1369 struct route *ro;
1370 register struct rtentry *rt;
1371 struct ifnet *ifp;
1372 register int rtt, mss;
1373 u_long bufsize;
1374 struct inpcb *inp;
1375 struct socket *so;
1376 extern int tcp_mssdflt, tcp_rttdflt;
1377
1378 inp = tp->t_inpcb;
1379 ro = &inp->inp_route;
1380
1381 if ((rt = ro->ro_rt) == (struct rtentry *)0) {
1382 /* No route yet, so try to acquire one */
1383 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1384 ro->ro_dst.sa_family = AF_INET;
1385 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1386 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1387 inp->inp_faddr;
1388 rtalloc(ro);
1389 }
1390 if ((rt = ro->ro_rt) == (struct rtentry *)0)
1391 return (tcp_mssdflt);
1392 }
1393 ifp = rt->rt_ifp;
1394 so = inp->inp_socket;
1395
1396 #ifdef RTV_MTU /* if route characteristics exist ... */
1397 /*
1398 * While we're here, check if there's an initial rtt
1399 * or rttvar. Convert from the route-table units
1400 * to scaled multiples of the slow timeout timer.
1401 */
1402 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1403 if (rt->rt_rmx.rmx_locks & RTV_MTU)
1404 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1405 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1406 if (rt->rt_rmx.rmx_rttvar)
1407 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1408 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1409 else
1410 /* default variation is +- 1 rtt */
1411 tp->t_rttvar =
1412 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1413 TCPT_RANGESET(tp->t_rxtcur,
1414 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1415 tp->t_rttmin, TCPTV_REXMTMAX);
1416 }
1417 /*
1418 * if there's an mtu associated with the route, use it
1419 */
1420 if (rt->rt_rmx.rmx_mtu)
1421 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1422 else
1423 #endif /* RTV_MTU */
1424 {
1425 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1426 #if (MCLBYTES & (MCLBYTES - 1)) == 0
1427 if (mss > MCLBYTES)
1428 mss &= ~(MCLBYTES-1);
1429 #else
1430 if (mss > MCLBYTES)
1431 mss = mss / MCLBYTES * MCLBYTES;
1432 #endif
1433 if (!in_localaddr(inp->inp_faddr))
1434 mss = min(mss, tcp_mssdflt);
1435 }
1436 /*
1437 * The current mss, t_maxseg, is initialized to the default value.
1438 * If we compute a smaller value, reduce the current mss.
1439 * If we compute a larger value, return it for use in sending
1440 * a max seg size option, but don't store it for use
1441 * unless we received an offer at least that large from peer.
1442 * However, do not accept offers under 32 bytes.
1443 */
1444 if (offer)
1445 mss = min(mss, offer);
1446 mss = max(mss, 32); /* sanity */
1447 if (mss < tp->t_maxseg || offer != 0) {
1448 /*
1449 * If there's a pipesize, change the socket buffer
1450 * to that size. Make the socket buffers an integral
1451 * number of mss units; if the mss is larger than
1452 * the socket buffer, decrease the mss.
1453 */
1454 #ifdef RTV_SPIPE
1455 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
1456 #endif
1457 bufsize = so->so_snd.sb_hiwat;
1458 if (bufsize < mss)
1459 mss = bufsize;
1460 else {
1461 bufsize = min(bufsize, SB_MAX) / mss * mss;
1462 (void) sbreserve(&so->so_snd, bufsize);
1463 }
1464 tp->t_maxseg = mss;
1465
1466 #ifdef RTV_RPIPE
1467 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
1468 #endif
1469 bufsize = so->so_rcv.sb_hiwat;
1470 if (bufsize > mss) {
1471 bufsize = min(bufsize, SB_MAX) / mss * mss;
1472 (void) sbreserve(&so->so_rcv, bufsize);
1473 }
1474 }
1475 tp->snd_cwnd = mss;
1476
1477 #ifdef RTV_SSTHRESH
1478 if (rt->rt_rmx.rmx_ssthresh) {
1479 /*
1480 * There's some sort of gateway or interface
1481 * buffer limit on the path. Use this to set
1482 * the slow start threshhold, but set the
1483 * threshold to no less than 2*mss.
1484 */
1485 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1486 }
1487 #endif /* RTV_MTU */
1488 return (mss);
1489 }
1490