tcp_input.c revision 1.2 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.2 1993/05/18 18:20:15 cgd Exp $
35 */
36
37 #include "param.h"
38 #include "systm.h"
39 #include "malloc.h"
40 #include "select.h"
41 #include "mbuf.h"
42 #include "protosw.h"
43 #include "socket.h"
44 #include "socketvar.h"
45 #include "errno.h"
46
47 #include "../net/if.h"
48 #include "../net/route.h"
49
50 #include "in.h"
51 #include "in_systm.h"
52 #include "ip.h"
53 #include "in_pcb.h"
54 #include "ip_var.h"
55 #include "tcp.h"
56 #include "tcp_fsm.h"
57 #include "tcp_seq.h"
58 #include "tcp_timer.h"
59 #include "tcp_var.h"
60 #include "tcpip.h"
61 #include "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 struct tcpcb *tcp_newtcpcb();
71
72 /*
73 * Insert segment ti into reassembly queue of tcp with
74 * control block tp. Return TH_FIN if reassembly now includes
75 * a segment with FIN. The macro form does the common case inline
76 * (segment is the next to be received on an established connection,
77 * and the queue is empty), avoiding linkage into and removal
78 * from the queue and repetition of various conversions.
79 * Set DELACK for segments received in order, but ack immediately
80 * when segments are out of order (so fast retransmit can work).
81 */
82 #define TCP_REASS(tp, ti, m, so, flags) { \
83 if ((ti)->ti_seq == (tp)->rcv_nxt && \
84 (tp)->seg_next == (struct tcpiphdr *)(tp) && \
85 (tp)->t_state == TCPS_ESTABLISHED) { \
86 tp->t_flags |= TF_DELACK; \
87 (tp)->rcv_nxt += (ti)->ti_len; \
88 flags = (ti)->ti_flags & TH_FIN; \
89 tcpstat.tcps_rcvpack++;\
90 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
91 sbappend(&(so)->so_rcv, (m)); \
92 sorwakeup(so); \
93 } else { \
94 (flags) = tcp_reass((tp), (ti), (m)); \
95 tp->t_flags |= TF_ACKNOW; \
96 } \
97 }
98
99 tcp_reass(tp, ti, m)
100 register struct tcpcb *tp;
101 register struct tcpiphdr *ti;
102 struct mbuf *m;
103 {
104 register struct tcpiphdr *q;
105 struct socket *so = tp->t_inpcb->inp_socket;
106 int flags;
107
108 /*
109 * Call with ti==0 after become established to
110 * force pre-ESTABLISHED data up to user socket.
111 */
112 if (ti == 0)
113 goto present;
114
115 /*
116 * Find a segment which begins after this one does.
117 */
118 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
119 q = (struct tcpiphdr *)q->ti_next)
120 if (SEQ_GT(q->ti_seq, ti->ti_seq))
121 break;
122
123 /*
124 * If there is a preceding segment, it may provide some of
125 * our data already. If so, drop the data from the incoming
126 * segment. If it provides all of our data, drop us.
127 */
128 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
129 register int i;
130 q = (struct tcpiphdr *)q->ti_prev;
131 /* conversion to int (in i) handles seq wraparound */
132 i = q->ti_seq + q->ti_len - ti->ti_seq;
133 if (i > 0) {
134 if (i >= ti->ti_len) {
135 tcpstat.tcps_rcvduppack++;
136 tcpstat.tcps_rcvdupbyte += ti->ti_len;
137 m_freem(m);
138 return (0);
139 }
140 m_adj(m, i);
141 ti->ti_len -= i;
142 ti->ti_seq += i;
143 }
144 q = (struct tcpiphdr *)(q->ti_next);
145 }
146 tcpstat.tcps_rcvoopack++;
147 tcpstat.tcps_rcvoobyte += ti->ti_len;
148 REASS_MBUF(ti) = m; /* XXX */
149
150 /*
151 * While we overlap succeeding segments trim them or,
152 * if they are completely covered, dequeue them.
153 */
154 while (q != (struct tcpiphdr *)tp) {
155 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
156 if (i <= 0)
157 break;
158 if (i < q->ti_len) {
159 q->ti_seq += i;
160 q->ti_len -= i;
161 m_adj(REASS_MBUF(q), i);
162 break;
163 }
164 q = (struct tcpiphdr *)q->ti_next;
165 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
166 remque(q->ti_prev);
167 m_freem(m);
168 }
169
170 /*
171 * Stick new segment in its place.
172 */
173 insque(ti, q->ti_prev);
174
175 present:
176 /*
177 * Present data to user, advancing rcv_nxt through
178 * completed sequence space.
179 */
180 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
181 return (0);
182 ti = tp->seg_next;
183 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
184 return (0);
185 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
186 return (0);
187 do {
188 tp->rcv_nxt += ti->ti_len;
189 flags = ti->ti_flags & TH_FIN;
190 remque(ti);
191 m = REASS_MBUF(ti);
192 ti = (struct tcpiphdr *)ti->ti_next;
193 if (so->so_state & SS_CANTRCVMORE)
194 m_freem(m);
195 else
196 sbappend(&so->so_rcv, m);
197 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
198 sorwakeup(so);
199 return (flags);
200 }
201
202 /*
203 * TCP input routine, follows pages 65-76 of the
204 * protocol specification dated September, 1981 very closely.
205 */
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 tcpstat.tcps_rcvduppack++;
631 tcpstat.tcps_rcvdupbyte += ti->ti_len;
632 /*
633 * If segment is just one to the left of the window,
634 * check two special cases:
635 * 1. Don't toss RST in response to 4.2-style keepalive.
636 * 2. If the only thing to drop is a FIN, we can drop
637 * it, but check the ACK or we will get into FIN
638 * wars if our FINs crossed (both CLOSING).
639 * In either case, send ACK to resynchronize,
640 * but keep on processing for RST or ACK.
641 */
642 if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
643 #ifdef TCP_COMPAT_42
644 || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
645 #endif
646 ) {
647 todrop = ti->ti_len;
648 tiflags &= ~TH_FIN;
649 tp->t_flags |= TF_ACKNOW;
650 } else
651 goto dropafterack;
652 } else {
653 tcpstat.tcps_rcvpartduppack++;
654 tcpstat.tcps_rcvpartdupbyte += todrop;
655 }
656 m_adj(m, todrop);
657 ti->ti_seq += todrop;
658 ti->ti_len -= todrop;
659 if (ti->ti_urp > todrop)
660 ti->ti_urp -= todrop;
661 else {
662 tiflags &= ~TH_URG;
663 ti->ti_urp = 0;
664 }
665 }
666
667 /*
668 * If new data are received on a connection after the
669 * user processes are gone, then RST the other end.
670 */
671 if ((so->so_state & SS_NOFDREF) &&
672 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
673 tp = tcp_close(tp);
674 tcpstat.tcps_rcvafterclose++;
675 goto dropwithreset;
676 }
677
678 /*
679 * If segment ends after window, drop trailing data
680 * (and PUSH and FIN); if nothing left, just ACK.
681 */
682 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
683 if (todrop > 0) {
684 tcpstat.tcps_rcvpackafterwin++;
685 if (todrop >= ti->ti_len) {
686 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
687 /*
688 * If a new connection request is received
689 * while in TIME_WAIT, drop the old connection
690 * and start over if the sequence numbers
691 * are above the previous ones.
692 */
693 if (tiflags & TH_SYN &&
694 tp->t_state == TCPS_TIME_WAIT &&
695 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
696 iss = tp->rcv_nxt + TCP_ISSINCR;
697 tp = tcp_close(tp);
698 goto findpcb;
699 }
700 /*
701 * If window is closed can only take segments at
702 * window edge, and have to drop data and PUSH from
703 * incoming segments. Continue processing, but
704 * remember to ack. Otherwise, drop segment
705 * and ack.
706 */
707 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
708 tp->t_flags |= TF_ACKNOW;
709 tcpstat.tcps_rcvwinprobe++;
710 } else
711 goto dropafterack;
712 } else
713 tcpstat.tcps_rcvbyteafterwin += todrop;
714 m_adj(m, -todrop);
715 ti->ti_len -= todrop;
716 tiflags &= ~(TH_PUSH|TH_FIN);
717 }
718
719 /*
720 * If the RST bit is set examine the state:
721 * SYN_RECEIVED STATE:
722 * If passive open, return to LISTEN state.
723 * If active open, inform user that connection was refused.
724 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
725 * Inform user that connection was reset, and close tcb.
726 * CLOSING, LAST_ACK, TIME_WAIT STATES
727 * Close the tcb.
728 */
729 if (tiflags&TH_RST) switch (tp->t_state) {
730
731 case TCPS_SYN_RECEIVED:
732 so->so_error = ECONNREFUSED;
733 goto close;
734
735 case TCPS_ESTABLISHED:
736 case TCPS_FIN_WAIT_1:
737 case TCPS_FIN_WAIT_2:
738 case TCPS_CLOSE_WAIT:
739 so->so_error = ECONNRESET;
740 close:
741 tp->t_state = TCPS_CLOSED;
742 tcpstat.tcps_drops++;
743 tp = tcp_close(tp);
744 goto drop;
745
746 case TCPS_CLOSING:
747 case TCPS_LAST_ACK:
748 case TCPS_TIME_WAIT:
749 tp = tcp_close(tp);
750 goto drop;
751 }
752
753 /*
754 * If a SYN is in the window, then this is an
755 * error and we send an RST and drop the connection.
756 */
757 if (tiflags & TH_SYN) {
758 tp = tcp_drop(tp, ECONNRESET);
759 goto dropwithreset;
760 }
761
762 /*
763 * If the ACK bit is off we drop the segment and return.
764 */
765 if ((tiflags & TH_ACK) == 0)
766 goto drop;
767
768 /*
769 * Ack processing.
770 */
771 switch (tp->t_state) {
772
773 /*
774 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
775 * ESTABLISHED state and continue processing, otherwise
776 * send an RST.
777 */
778 case TCPS_SYN_RECEIVED:
779 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
780 SEQ_GT(ti->ti_ack, tp->snd_max))
781 goto dropwithreset;
782 tcpstat.tcps_connects++;
783 soisconnected(so);
784 tp->t_state = TCPS_ESTABLISHED;
785 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
786 tp->snd_wl1 = ti->ti_seq - 1;
787 /* fall into ... */
788
789 /*
790 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
791 * ACKs. If the ack is in the range
792 * tp->snd_una < ti->ti_ack <= tp->snd_max
793 * then advance tp->snd_una to ti->ti_ack and drop
794 * data from the retransmission queue. If this ACK reflects
795 * more up to date window information we update our window information.
796 */
797 case TCPS_ESTABLISHED:
798 case TCPS_FIN_WAIT_1:
799 case TCPS_FIN_WAIT_2:
800 case TCPS_CLOSE_WAIT:
801 case TCPS_CLOSING:
802 case TCPS_LAST_ACK:
803 case TCPS_TIME_WAIT:
804
805 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
806 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
807 tcpstat.tcps_rcvdupack++;
808 /*
809 * If we have outstanding data (other than
810 * a window probe), this is a completely
811 * duplicate ack (ie, window info didn't
812 * change), the ack is the biggest we've
813 * seen and we've seen exactly our rexmt
814 * threshhold of them, assume a packet
815 * has been dropped and retransmit it.
816 * Kludge snd_nxt & the congestion
817 * window so we send only this one
818 * packet.
819 *
820 * We know we're losing at the current
821 * window size so do congestion avoidance
822 * (set ssthresh to half the current window
823 * and pull our congestion window back to
824 * the new ssthresh).
825 *
826 * Dup acks mean that packets have left the
827 * network (they're now cached at the receiver)
828 * so bump cwnd by the amount in the receiver
829 * to keep a constant cwnd packets in the
830 * network.
831 */
832 if (tp->t_timer[TCPT_REXMT] == 0 ||
833 ti->ti_ack != tp->snd_una)
834 tp->t_dupacks = 0;
835 else if (++tp->t_dupacks == tcprexmtthresh) {
836 tcp_seq onxt = tp->snd_nxt;
837 u_int win =
838 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
839 tp->t_maxseg;
840
841 if (win < 2)
842 win = 2;
843 tp->snd_ssthresh = win * tp->t_maxseg;
844 tp->t_timer[TCPT_REXMT] = 0;
845 tp->t_rtt = 0;
846 tp->snd_nxt = ti->ti_ack;
847 tp->snd_cwnd = tp->t_maxseg;
848 (void) tcp_output(tp);
849 tp->snd_cwnd = tp->snd_ssthresh +
850 tp->t_maxseg * tp->t_dupacks;
851 if (SEQ_GT(onxt, tp->snd_nxt))
852 tp->snd_nxt = onxt;
853 goto drop;
854 } else if (tp->t_dupacks > tcprexmtthresh) {
855 tp->snd_cwnd += tp->t_maxseg;
856 (void) tcp_output(tp);
857 goto drop;
858 }
859 } else
860 tp->t_dupacks = 0;
861 break;
862 }
863 /*
864 * If the congestion window was inflated to account
865 * for the other side's cached packets, retract it.
866 */
867 if (tp->t_dupacks > tcprexmtthresh &&
868 tp->snd_cwnd > tp->snd_ssthresh)
869 tp->snd_cwnd = tp->snd_ssthresh;
870 tp->t_dupacks = 0;
871 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
872 tcpstat.tcps_rcvacktoomuch++;
873 goto dropafterack;
874 }
875 acked = ti->ti_ack - tp->snd_una;
876 tcpstat.tcps_rcvackpack++;
877 tcpstat.tcps_rcvackbyte += acked;
878
879 /*
880 * If transmit timer is running and timed sequence
881 * number was acked, update smoothed round trip time.
882 * Since we now have an rtt measurement, cancel the
883 * timer backoff (cf., Phil Karn's retransmit alg.).
884 * Recompute the initial retransmit timer.
885 */
886 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
887 tcp_xmit_timer(tp);
888
889 /*
890 * If all outstanding data is acked, stop retransmit
891 * timer and remember to restart (more output or persist).
892 * If there is more data to be acked, restart retransmit
893 * timer, using current (possibly backed-off) value.
894 */
895 if (ti->ti_ack == tp->snd_max) {
896 tp->t_timer[TCPT_REXMT] = 0;
897 needoutput = 1;
898 } else if (tp->t_timer[TCPT_PERSIST] == 0)
899 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
900 /*
901 * When new data is acked, open the congestion window.
902 * If the window gives us less than ssthresh packets
903 * in flight, open exponentially (maxseg per packet).
904 * Otherwise open linearly: maxseg per window
905 * (maxseg^2 / cwnd per packet), plus a constant
906 * fraction of a packet (maxseg/8) to help larger windows
907 * open quickly enough.
908 */
909 {
910 register u_int cw = tp->snd_cwnd;
911 register u_int incr = tp->t_maxseg;
912
913 if (cw > tp->snd_ssthresh)
914 incr = incr * incr / cw + incr / 8;
915 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
916 }
917 if (acked > so->so_snd.sb_cc) {
918 tp->snd_wnd -= so->so_snd.sb_cc;
919 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
920 ourfinisacked = 1;
921 } else {
922 sbdrop(&so->so_snd, acked);
923 tp->snd_wnd -= acked;
924 ourfinisacked = 0;
925 }
926 if (so->so_snd.sb_flags & SB_NOTIFY)
927 sowwakeup(so);
928 tp->snd_una = ti->ti_ack;
929 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
930 tp->snd_nxt = tp->snd_una;
931
932 switch (tp->t_state) {
933
934 /*
935 * In FIN_WAIT_1 STATE in addition to the processing
936 * for the ESTABLISHED state if our FIN is now acknowledged
937 * then enter FIN_WAIT_2.
938 */
939 case TCPS_FIN_WAIT_1:
940 if (ourfinisacked) {
941 /*
942 * If we can't receive any more
943 * data, then closing user can proceed.
944 * Starting the timer is contrary to the
945 * specification, but if we don't get a FIN
946 * we'll hang forever.
947 */
948 if (so->so_state & SS_CANTRCVMORE) {
949 soisdisconnected(so);
950 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
951 }
952 tp->t_state = TCPS_FIN_WAIT_2;
953 }
954 break;
955
956 /*
957 * In CLOSING STATE in addition to the processing for
958 * the ESTABLISHED state if the ACK acknowledges our FIN
959 * then enter the TIME-WAIT state, otherwise ignore
960 * the segment.
961 */
962 case TCPS_CLOSING:
963 if (ourfinisacked) {
964 tp->t_state = TCPS_TIME_WAIT;
965 tcp_canceltimers(tp);
966 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
967 soisdisconnected(so);
968 }
969 break;
970
971 /*
972 * In LAST_ACK, we may still be waiting for data to drain
973 * and/or to be acked, as well as for the ack of our FIN.
974 * If our FIN is now acknowledged, delete the TCB,
975 * enter the closed state and return.
976 */
977 case TCPS_LAST_ACK:
978 if (ourfinisacked) {
979 tp = tcp_close(tp);
980 goto drop;
981 }
982 break;
983
984 /*
985 * In TIME_WAIT state the only thing that should arrive
986 * is a retransmission of the remote FIN. Acknowledge
987 * it and restart the finack timer.
988 */
989 case TCPS_TIME_WAIT:
990 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
991 goto dropafterack;
992 }
993 }
994
995 step6:
996 /*
997 * Update window information.
998 * Don't look at window if no ACK: TAC's send garbage on first SYN.
999 */
1000 if ((tiflags & TH_ACK) &&
1001 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
1002 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1003 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
1004 /* keep track of pure window updates */
1005 if (ti->ti_len == 0 &&
1006 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
1007 tcpstat.tcps_rcvwinupd++;
1008 tp->snd_wnd = ti->ti_win;
1009 tp->snd_wl1 = ti->ti_seq;
1010 tp->snd_wl2 = ti->ti_ack;
1011 if (tp->snd_wnd > tp->max_sndwnd)
1012 tp->max_sndwnd = tp->snd_wnd;
1013 needoutput = 1;
1014 }
1015
1016 /*
1017 * Process segments with URG.
1018 */
1019 if ((tiflags & TH_URG) && ti->ti_urp &&
1020 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1021 /*
1022 * This is a kludge, but if we receive and accept
1023 * random urgent pointers, we'll crash in
1024 * soreceive. It's hard to imagine someone
1025 * actually wanting to send this much urgent data.
1026 */
1027 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
1028 ti->ti_urp = 0; /* XXX */
1029 tiflags &= ~TH_URG; /* XXX */
1030 goto dodata; /* XXX */
1031 }
1032 /*
1033 * If this segment advances the known urgent pointer,
1034 * then mark the data stream. This should not happen
1035 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1036 * a FIN has been received from the remote side.
1037 * In these states we ignore the URG.
1038 *
1039 * According to RFC961 (Assigned Protocols),
1040 * the urgent pointer points to the last octet
1041 * of urgent data. We continue, however,
1042 * to consider it to indicate the first octet
1043 * of data past the urgent section as the original
1044 * spec states (in one of two places).
1045 */
1046 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1047 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1048 so->so_oobmark = so->so_rcv.sb_cc +
1049 (tp->rcv_up - tp->rcv_nxt) - 1;
1050 if (so->so_oobmark == 0)
1051 so->so_state |= SS_RCVATMARK;
1052 sohasoutofband(so);
1053 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1054 }
1055 /*
1056 * Remove out of band data so doesn't get presented to user.
1057 * This can happen independent of advancing the URG pointer,
1058 * but if two URG's are pending at once, some out-of-band
1059 * data may creep in... ick.
1060 */
1061 if (ti->ti_urp <= ti->ti_len
1062 #ifdef SO_OOBINLINE
1063 && (so->so_options & SO_OOBINLINE) == 0
1064 #endif
1065 )
1066 tcp_pulloutofband(so, ti, m);
1067 } else
1068 /*
1069 * If no out of band data is expected,
1070 * pull receive urgent pointer along
1071 * with the receive window.
1072 */
1073 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1074 tp->rcv_up = tp->rcv_nxt;
1075 dodata: /* XXX */
1076
1077 /*
1078 * Process the segment text, merging it into the TCP sequencing queue,
1079 * and arranging for acknowledgment of receipt if necessary.
1080 * This process logically involves adjusting tp->rcv_wnd as data
1081 * is presented to the user (this happens in tcp_usrreq.c,
1082 * case PRU_RCVD). If a FIN has already been received on this
1083 * connection then we just ignore the text.
1084 */
1085 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1086 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1087 TCP_REASS(tp, ti, m, so, tiflags);
1088 /*
1089 * Note the amount of data that peer has sent into
1090 * our window, in order to estimate the sender's
1091 * buffer size.
1092 */
1093 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1094 } else {
1095 m_freem(m);
1096 tiflags &= ~TH_FIN;
1097 }
1098
1099 /*
1100 * If FIN is received ACK the FIN and let the user know
1101 * that the connection is closing.
1102 */
1103 if (tiflags & TH_FIN) {
1104 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1105 socantrcvmore(so);
1106 tp->t_flags |= TF_ACKNOW;
1107 tp->rcv_nxt++;
1108 }
1109 switch (tp->t_state) {
1110
1111 /*
1112 * In SYN_RECEIVED and ESTABLISHED STATES
1113 * enter the CLOSE_WAIT state.
1114 */
1115 case TCPS_SYN_RECEIVED:
1116 case TCPS_ESTABLISHED:
1117 tp->t_state = TCPS_CLOSE_WAIT;
1118 break;
1119
1120 /*
1121 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1122 * enter the CLOSING state.
1123 */
1124 case TCPS_FIN_WAIT_1:
1125 tp->t_state = TCPS_CLOSING;
1126 break;
1127
1128 /*
1129 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1130 * starting the time-wait timer, turning off the other
1131 * standard timers.
1132 */
1133 case TCPS_FIN_WAIT_2:
1134 tp->t_state = TCPS_TIME_WAIT;
1135 tcp_canceltimers(tp);
1136 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1137 soisdisconnected(so);
1138 break;
1139
1140 /*
1141 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1142 */
1143 case TCPS_TIME_WAIT:
1144 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1145 break;
1146 }
1147 }
1148 if (so->so_options & SO_DEBUG)
1149 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1150
1151 /*
1152 * Return any desired output.
1153 */
1154 if (needoutput || (tp->t_flags & TF_ACKNOW))
1155 (void) tcp_output(tp);
1156 return;
1157
1158 dropafterack:
1159 /*
1160 * Generate an ACK dropping incoming segment if it occupies
1161 * sequence space, where the ACK reflects our state.
1162 */
1163 if (tiflags & TH_RST)
1164 goto drop;
1165 m_freem(m);
1166 tp->t_flags |= TF_ACKNOW;
1167 (void) tcp_output(tp);
1168 return;
1169
1170 dropwithreset:
1171 if (om) {
1172 (void) m_free(om);
1173 om = 0;
1174 }
1175 /*
1176 * Generate a RST, dropping incoming segment.
1177 * Make ACK acceptable to originator of segment.
1178 * Don't bother to respond if destination was broadcast.
1179 */
1180 if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
1181 goto drop;
1182 if (tiflags & TH_ACK)
1183 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1184 else {
1185 if (tiflags & TH_SYN)
1186 ti->ti_len++;
1187 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1188 TH_RST|TH_ACK);
1189 }
1190 /* destroy temporarily created socket */
1191 if (dropsocket)
1192 (void) soabort(so);
1193 return;
1194
1195 drop:
1196 if (om)
1197 (void) m_free(om);
1198 /*
1199 * Drop space held by incoming segment and return.
1200 */
1201 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1202 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1203 m_freem(m);
1204 /* destroy temporarily created socket */
1205 if (dropsocket)
1206 (void) soabort(so);
1207 return;
1208 }
1209
1210 tcp_dooptions(tp, om, ti)
1211 struct tcpcb *tp;
1212 struct mbuf *om;
1213 struct tcpiphdr *ti;
1214 {
1215 register u_char *cp;
1216 u_short mss;
1217 int opt, optlen, cnt;
1218
1219 cp = mtod(om, u_char *);
1220 cnt = om->m_len;
1221 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1222 opt = cp[0];
1223 if (opt == TCPOPT_EOL)
1224 break;
1225 if (opt == TCPOPT_NOP)
1226 optlen = 1;
1227 else {
1228 optlen = cp[1];
1229 if (optlen <= 0)
1230 break;
1231 }
1232 switch (opt) {
1233
1234 default:
1235 continue;
1236
1237 case TCPOPT_MAXSEG:
1238 if (optlen != 4)
1239 continue;
1240 if (!(ti->ti_flags & TH_SYN))
1241 continue;
1242 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1243 NTOHS(mss);
1244 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1245 break;
1246 }
1247 }
1248 (void) m_free(om);
1249 }
1250
1251 /*
1252 * Pull out of band byte out of a segment so
1253 * it doesn't appear in the user's data queue.
1254 * It is still reflected in the segment length for
1255 * sequencing purposes.
1256 */
1257 tcp_pulloutofband(so, ti, m)
1258 struct socket *so;
1259 struct tcpiphdr *ti;
1260 register struct mbuf *m;
1261 {
1262 int cnt = ti->ti_urp - 1;
1263
1264 while (cnt >= 0) {
1265 if (m->m_len > cnt) {
1266 char *cp = mtod(m, caddr_t) + cnt;
1267 struct tcpcb *tp = sototcpcb(so);
1268
1269 tp->t_iobc = *cp;
1270 tp->t_oobflags |= TCPOOB_HAVEDATA;
1271 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1272 m->m_len--;
1273 return;
1274 }
1275 cnt -= m->m_len;
1276 m = m->m_next;
1277 if (m == 0)
1278 break;
1279 }
1280 panic("tcp_pulloutofband");
1281 }
1282
1283 /*
1284 * Collect new round-trip time estimate
1285 * and update averages and current timeout.
1286 */
1287 tcp_xmit_timer(tp)
1288 register struct tcpcb *tp;
1289 {
1290 register short delta;
1291
1292 tcpstat.tcps_rttupdated++;
1293 if (tp->t_srtt != 0) {
1294 /*
1295 * srtt is stored as fixed point with 3 bits after the
1296 * binary point (i.e., scaled by 8). The following magic
1297 * is equivalent to the smoothing algorithm in rfc793 with
1298 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1299 * point). Adjust t_rtt to origin 0.
1300 */
1301 delta = tp->t_rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1302 if ((tp->t_srtt += delta) <= 0)
1303 tp->t_srtt = 1;
1304 /*
1305 * We accumulate a smoothed rtt variance (actually, a
1306 * smoothed mean difference), then set the retransmit
1307 * timer to smoothed rtt + 4 times the smoothed variance.
1308 * rttvar is stored as fixed point with 2 bits after the
1309 * binary point (scaled by 4). The following is
1310 * equivalent to rfc793 smoothing with an alpha of .75
1311 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1312 * rfc793's wired-in beta.
1313 */
1314 if (delta < 0)
1315 delta = -delta;
1316 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1317 if ((tp->t_rttvar += delta) <= 0)
1318 tp->t_rttvar = 1;
1319 } else {
1320 /*
1321 * No rtt measurement yet - use the unsmoothed rtt.
1322 * Set the variance to half the rtt (so our first
1323 * retransmit happens at 2*rtt)
1324 */
1325 tp->t_srtt = tp->t_rtt << TCP_RTT_SHIFT;
1326 tp->t_rttvar = tp->t_rtt << (TCP_RTTVAR_SHIFT - 1);
1327 }
1328 tp->t_rtt = 0;
1329 tp->t_rxtshift = 0;
1330
1331 /*
1332 * the retransmit should happen at rtt + 4 * rttvar.
1333 * Because of the way we do the smoothing, srtt and rttvar
1334 * will each average +1/2 tick of bias. When we compute
1335 * the retransmit timer, we want 1/2 tick of rounding and
1336 * 1 extra tick because of +-1/2 tick uncertainty in the
1337 * firing of the timer. The bias will give us exactly the
1338 * 1.5 tick we need. But, because the bias is
1339 * statistical, we have to test that we don't drop below
1340 * the minimum feasible timer (which is 2 ticks).
1341 */
1342 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1343 tp->t_rttmin, TCPTV_REXMTMAX);
1344
1345 /*
1346 * We received an ack for a packet that wasn't retransmitted;
1347 * it is probably safe to discard any error indications we've
1348 * received recently. This isn't quite right, but close enough
1349 * for now (a route might have failed after we sent a segment,
1350 * and the return path might not be symmetrical).
1351 */
1352 tp->t_softerror = 0;
1353 }
1354
1355 /*
1356 * Determine a reasonable value for maxseg size.
1357 * If the route is known, check route for mtu.
1358 * If none, use an mss that can be handled on the outgoing
1359 * interface without forcing IP to fragment; if bigger than
1360 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1361 * to utilize large mbufs. If no route is found, route has no mtu,
1362 * or the destination isn't local, use a default, hopefully conservative
1363 * size (usually 512 or the default IP max size, but no more than the mtu
1364 * of the interface), as we can't discover anything about intervening
1365 * gateways or networks. We also initialize the congestion/slow start
1366 * window to be a single segment if the destination isn't local.
1367 * While looking at the routing entry, we also initialize other path-dependent
1368 * parameters from pre-set or cached values in the routing entry.
1369 */
1370
1371 tcp_mss(tp, offer)
1372 register struct tcpcb *tp;
1373 u_short offer;
1374 {
1375 struct route *ro;
1376 register struct rtentry *rt;
1377 struct ifnet *ifp;
1378 register int rtt, mss;
1379 u_long bufsize;
1380 struct inpcb *inp;
1381 struct socket *so;
1382 extern int tcp_mssdflt, tcp_rttdflt;
1383
1384 inp = tp->t_inpcb;
1385 ro = &inp->inp_route;
1386
1387 if ((rt = ro->ro_rt) == (struct rtentry *)0) {
1388 /* No route yet, so try to acquire one */
1389 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1390 ro->ro_dst.sa_family = AF_INET;
1391 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1392 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1393 inp->inp_faddr;
1394 rtalloc(ro);
1395 }
1396 if ((rt = ro->ro_rt) == (struct rtentry *)0)
1397 return (tcp_mssdflt);
1398 }
1399 ifp = rt->rt_ifp;
1400 so = inp->inp_socket;
1401
1402 #ifdef RTV_MTU /* if route characteristics exist ... */
1403 /*
1404 * While we're here, check if there's an initial rtt
1405 * or rttvar. Convert from the route-table units
1406 * to scaled multiples of the slow timeout timer.
1407 */
1408 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
1409 if (rt->rt_rmx.rmx_locks & RTV_MTU)
1410 tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
1411 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
1412 if (rt->rt_rmx.rmx_rttvar)
1413 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
1414 (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
1415 else
1416 /* default variation is +- 1 rtt */
1417 tp->t_rttvar =
1418 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
1419 TCPT_RANGESET(tp->t_rxtcur,
1420 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
1421 tp->t_rttmin, TCPTV_REXMTMAX);
1422 }
1423 /*
1424 * if there's an mtu associated with the route, use it
1425 */
1426 if (rt->rt_rmx.rmx_mtu)
1427 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
1428 else
1429 #endif /* RTV_MTU */
1430 {
1431 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1432 #if (MCLBYTES & (MCLBYTES - 1)) == 0
1433 if (mss > MCLBYTES)
1434 mss &= ~(MCLBYTES-1);
1435 #else
1436 if (mss > MCLBYTES)
1437 mss = mss / MCLBYTES * MCLBYTES;
1438 #endif
1439 if (!in_localaddr(inp->inp_faddr))
1440 mss = min(mss, tcp_mssdflt);
1441 }
1442 /*
1443 * The current mss, t_maxseg, is initialized to the default value.
1444 * If we compute a smaller value, reduce the current mss.
1445 * If we compute a larger value, return it for use in sending
1446 * a max seg size option, but don't store it for use
1447 * unless we received an offer at least that large from peer.
1448 * However, do not accept offers under 32 bytes.
1449 */
1450 if (offer)
1451 mss = min(mss, offer);
1452 mss = max(mss, 32); /* sanity */
1453 if (mss < tp->t_maxseg || offer != 0) {
1454 /*
1455 * If there's a pipesize, change the socket buffer
1456 * to that size. Make the socket buffers an integral
1457 * number of mss units; if the mss is larger than
1458 * the socket buffer, decrease the mss.
1459 */
1460 #ifdef RTV_SPIPE
1461 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
1462 #endif
1463 bufsize = so->so_snd.sb_hiwat;
1464 if (bufsize < mss)
1465 mss = bufsize;
1466 else {
1467 bufsize = min(bufsize, SB_MAX) / mss * mss;
1468 (void) sbreserve(&so->so_snd, bufsize);
1469 }
1470 tp->t_maxseg = mss;
1471
1472 #ifdef RTV_RPIPE
1473 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
1474 #endif
1475 bufsize = so->so_rcv.sb_hiwat;
1476 if (bufsize > mss) {
1477 bufsize = min(bufsize, SB_MAX) / mss * mss;
1478 (void) sbreserve(&so->so_rcv, bufsize);
1479 }
1480 }
1481 tp->snd_cwnd = mss;
1482
1483 #ifdef RTV_SSTHRESH
1484 if (rt->rt_rmx.rmx_ssthresh) {
1485 /*
1486 * There's some sort of gateway or interface
1487 * buffer limit on the path. Use this to set
1488 * the slow start threshhold, but set the
1489 * threshold to no less than 2*mss.
1490 */
1491 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
1492 }
1493 #endif /* RTV_MTU */
1494 return (mss);
1495 }
1496