tcp_input.c revision 1.8 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.8 1994/04/25 19:16:53 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 if ((ti)->ti_flags & TH_PUSH) \
85 tp->t_flags |= TF_ACKNOW; \
86 else \
87 tp->t_flags |= TF_DELACK; \
88 (tp)->rcv_nxt += (ti)->ti_len; \
89 flags = (ti)->ti_flags & TH_FIN; \
90 tcpstat.tcps_rcvpack++;\
91 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
92 sbappend(&(so)->so_rcv, (m)); \
93 sorwakeup(so); \
94 } else { \
95 (flags) = tcp_reass((tp), (ti), (m)); \
96 tp->t_flags |= TF_ACKNOW; \
97 } \
98 }
99
100 int
101 tcp_reass(tp, ti, m)
102 register struct tcpcb *tp;
103 register struct tcpiphdr *ti;
104 struct mbuf *m;
105 {
106 register struct tcpiphdr *q;
107 struct socket *so = tp->t_inpcb->inp_socket;
108 int flags;
109
110 /*
111 * Call with ti==0 after become established to
112 * force pre-ESTABLISHED data up to user socket.
113 */
114 if (ti == 0)
115 goto present;
116
117 /*
118 * Find a segment which begins after this one does.
119 */
120 for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
121 q = (struct tcpiphdr *)q->ti_next)
122 if (SEQ_GT(q->ti_seq, ti->ti_seq))
123 break;
124
125 /*
126 * If there is a preceding segment, it may provide some of
127 * our data already. If so, drop the data from the incoming
128 * segment. If it provides all of our data, drop us.
129 */
130 if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
131 register int i;
132 q = (struct tcpiphdr *)q->ti_prev;
133 /* conversion to int (in i) handles seq wraparound */
134 i = q->ti_seq + q->ti_len - ti->ti_seq;
135 if (i > 0) {
136 if (i >= ti->ti_len) {
137 tcpstat.tcps_rcvduppack++;
138 tcpstat.tcps_rcvdupbyte += ti->ti_len;
139 m_freem(m);
140 return (0);
141 }
142 m_adj(m, i);
143 ti->ti_len -= i;
144 ti->ti_seq += i;
145 }
146 q = (struct tcpiphdr *)(q->ti_next);
147 }
148 tcpstat.tcps_rcvoopack++;
149 tcpstat.tcps_rcvoobyte += ti->ti_len;
150 REASS_MBUF(ti) = m; /* XXX */
151
152 /*
153 * While we overlap succeeding segments trim them or,
154 * if they are completely covered, dequeue them.
155 */
156 while (q != (struct tcpiphdr *)tp) {
157 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
158 if (i <= 0)
159 break;
160 if (i < q->ti_len) {
161 q->ti_seq += i;
162 q->ti_len -= i;
163 m_adj(REASS_MBUF(q), i);
164 break;
165 }
166 q = (struct tcpiphdr *)q->ti_next;
167 m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
168 remque(q->ti_prev);
169 m_freem(m);
170 }
171
172 /*
173 * Stick new segment in its place.
174 */
175 insque(ti, q->ti_prev);
176
177 present:
178 /*
179 * Present data to user, advancing rcv_nxt through
180 * completed sequence space.
181 */
182 if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
183 return (0);
184 ti = tp->seg_next;
185 if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
186 return (0);
187 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
188 return (0);
189 do {
190 tp->rcv_nxt += ti->ti_len;
191 flags = ti->ti_flags & TH_FIN;
192 remque(ti);
193 m = REASS_MBUF(ti);
194 ti = (struct tcpiphdr *)ti->ti_next;
195 if (so->so_state & SS_CANTRCVMORE)
196 m_freem(m);
197 else
198 sbappend(&so->so_rcv, m);
199 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
200 sorwakeup(so);
201 return (flags);
202 }
203
204 /*
205 * TCP input routine, follows pages 65-76 of the
206 * protocol specification dated September, 1981 very closely.
207 */
208 void
209 tcp_input(m, iphlen)
210 register struct mbuf *m;
211 int iphlen;
212 {
213 register struct tcpiphdr *ti;
214 register struct inpcb *inp;
215 struct mbuf *om = 0;
216 int len, tlen, off;
217 register struct tcpcb *tp = 0;
218 register int tiflags;
219 struct socket *so;
220 int todrop, acked, ourfinisacked, needoutput = 0;
221 short ostate;
222 struct in_addr laddr;
223 int dropsocket = 0;
224 int iss = 0;
225
226 tcpstat.tcps_rcvtotal++;
227 /*
228 * Get IP and TCP header together in first mbuf.
229 * Note: IP leaves IP header in first mbuf.
230 */
231 ti = mtod(m, struct tcpiphdr *);
232 if (iphlen > sizeof (struct ip))
233 ip_stripoptions(m, (struct mbuf *)0);
234 if (m->m_len < sizeof (struct tcpiphdr)) {
235 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
236 tcpstat.tcps_rcvshort++;
237 return;
238 }
239 ti = mtod(m, struct tcpiphdr *);
240 }
241
242 /*
243 * Checksum extended TCP header and data.
244 */
245 tlen = ((struct ip *)ti)->ip_len;
246 len = sizeof (struct ip) + tlen;
247 ti->ti_next = ti->ti_prev = 0;
248 ti->ti_x1 = 0;
249 ti->ti_len = (u_short)tlen;
250 HTONS(ti->ti_len);
251 if (ti->ti_sum = in_cksum(m, len)) {
252 tcpstat.tcps_rcvbadsum++;
253 goto drop;
254 }
255
256 /*
257 * Check that TCP offset makes sense,
258 * pull out TCP options and adjust length. XXX
259 */
260 off = ti->ti_off << 2;
261 if (off < sizeof (struct tcphdr) || off > tlen) {
262 tcpstat.tcps_rcvbadoff++;
263 goto drop;
264 }
265 tlen -= off;
266 ti->ti_len = tlen;
267 if (off > sizeof (struct tcphdr)) {
268 if (m->m_len < sizeof(struct ip) + off) {
269 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
270 tcpstat.tcps_rcvshort++;
271 return;
272 }
273 ti = mtod(m, struct tcpiphdr *);
274 }
275 om = m_get(M_DONTWAIT, MT_DATA);
276 if (om == 0)
277 goto drop;
278 om->m_len = off - sizeof (struct tcphdr);
279 { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
280 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
281 m->m_len -= om->m_len;
282 m->m_pkthdr.len -= om->m_len;
283 bcopy(op+om->m_len, op,
284 (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
285 }
286 }
287 tiflags = ti->ti_flags;
288
289 /*
290 * Convert TCP protocol specific fields to host format.
291 */
292 NTOHL(ti->ti_seq);
293 NTOHL(ti->ti_ack);
294 NTOHS(ti->ti_win);
295 NTOHS(ti->ti_urp);
296
297 /*
298 * Locate pcb for segment.
299 */
300 findpcb:
301 inp = tcp_last_inpcb;
302 if (inp->inp_lport != ti->ti_dport ||
303 inp->inp_fport != ti->ti_sport ||
304 inp->inp_faddr.s_addr != ti->ti_src.s_addr ||
305 inp->inp_laddr.s_addr != ti->ti_dst.s_addr) {
306 inp = in_pcblookup(&tcb, ti->ti_src, ti->ti_sport,
307 ti->ti_dst, ti->ti_dport, INPLOOKUP_WILDCARD);
308 if (inp)
309 tcp_last_inpcb = inp;
310 ++tcppcbcachemiss;
311 }
312
313 /*
314 * If the state is CLOSED (i.e., TCB does not exist) then
315 * all data in the incoming segment is discarded.
316 * If the TCB exists but is in CLOSED state, it is embryonic,
317 * but should either do a listen or a connect soon.
318 */
319 if (inp == 0)
320 goto dropwithreset;
321 tp = intotcpcb(inp);
322 if (tp == 0)
323 goto dropwithreset;
324 if (tp->t_state == TCPS_CLOSED)
325 goto drop;
326 so = inp->inp_socket;
327 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
328 if (so->so_options & SO_DEBUG) {
329 ostate = tp->t_state;
330 tcp_saveti = *ti;
331 }
332 if (so->so_options & SO_ACCEPTCONN) {
333 so = sonewconn(so, 0);
334 if (so == 0)
335 goto drop;
336 /*
337 * This is ugly, but ....
338 *
339 * Mark socket as temporary until we're
340 * committed to keeping it. The code at
341 * ``drop'' and ``dropwithreset'' check the
342 * flag dropsocket to see if the temporary
343 * socket created here should be discarded.
344 * We mark the socket as discardable until
345 * we're committed to it below in TCPS_LISTEN.
346 */
347 dropsocket++;
348 inp = (struct inpcb *)so->so_pcb;
349 inp->inp_laddr = ti->ti_dst;
350 inp->inp_lport = ti->ti_dport;
351 #if BSD>=43
352 inp->inp_options = ip_srcroute();
353 #endif
354 tp = intotcpcb(inp);
355 tp->t_state = TCPS_LISTEN;
356 }
357 }
358
359 /*
360 * Segment received on connection.
361 * Reset idle time and keep-alive timer.
362 */
363 tp->t_idle = 0;
364 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
365
366 /*
367 * Process options if not in LISTEN state,
368 * else do it below (after getting remote address).
369 */
370 if (om && tp->t_state != TCPS_LISTEN) {
371 tcp_dooptions(tp, om, ti);
372 om = 0;
373 }
374 /*
375 * Header prediction: check for the two common cases
376 * of a uni-directional data xfer. If the packet has
377 * no control flags, is in-sequence, the window didn't
378 * change and we're not retransmitting, it's a
379 * candidate. If the length is zero and the ack moved
380 * forward, we're the sender side of the xfer. Just
381 * free the data acked & wake any higher level process
382 * that was blocked waiting for space. If the length
383 * is non-zero and the ack didn't move, we're the
384 * receiver side. If we're getting packets in-order
385 * (the reassembly queue is empty), add the data to
386 * the socket buffer and note that we need a delayed ack.
387 */
388 if (tp->t_state == TCPS_ESTABLISHED &&
389 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
390 ti->ti_seq == tp->rcv_nxt &&
391 ti->ti_win && ti->ti_win == tp->snd_wnd &&
392 tp->snd_nxt == tp->snd_max) {
393 if (ti->ti_len == 0) {
394 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
395 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
396 tp->snd_cwnd >= tp->snd_wnd) {
397 /*
398 * this is a pure ack for outstanding data.
399 */
400 ++tcppredack;
401 if (tp->t_rtt && SEQ_GT(ti->ti_ack,tp->t_rtseq))
402 tcp_xmit_timer(tp);
403 acked = ti->ti_ack - tp->snd_una;
404 tcpstat.tcps_rcvackpack++;
405 tcpstat.tcps_rcvackbyte += acked;
406 sbdrop(&so->so_snd, acked);
407 tp->snd_una = ti->ti_ack;
408 m_freem(m);
409
410 /*
411 * If all outstanding data are acked, stop
412 * retransmit timer, otherwise restart timer
413 * using current (possibly backed-off) value.
414 * If process is waiting for space,
415 * wakeup/selwakeup/signal. If data
416 * are ready to send, let tcp_output
417 * decide between more output or persist.
418 */
419 if (tp->snd_una == tp->snd_max)
420 tp->t_timer[TCPT_REXMT] = 0;
421 else if (tp->t_timer[TCPT_PERSIST] == 0)
422 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
423
424 if (so->so_snd.sb_flags & SB_NOTIFY)
425 sowwakeup(so);
426 if (so->so_snd.sb_cc)
427 (void) tcp_output(tp);
428 return;
429 }
430 } else if (ti->ti_ack == tp->snd_una &&
431 tp->seg_next == (struct tcpiphdr *)tp &&
432 ti->ti_len <= sbspace(&so->so_rcv)) {
433 /*
434 * this is a pure, in-sequence data packet
435 * with nothing on the reassembly queue and
436 * we have enough buffer space to take it.
437 */
438 ++tcppreddat;
439 tp->rcv_nxt += ti->ti_len;
440 tcpstat.tcps_rcvpack++;
441 tcpstat.tcps_rcvbyte += ti->ti_len;
442 /*
443 * Drop TCP and IP headers then add data
444 * to socket buffer
445 */
446 m->m_data += sizeof(struct tcpiphdr);
447 m->m_len -= sizeof(struct tcpiphdr);
448 sbappend(&so->so_rcv, m);
449 sorwakeup(so);
450 if (ti->ti_flags & TH_PUSH)
451 tp->t_flags |= TF_ACKNOW;
452 else
453 tp->t_flags |= TF_DELACK;
454 return;
455 }
456 }
457
458 /*
459 * Drop TCP and IP headers; TCP options were dropped above.
460 */
461 m->m_data += sizeof(struct tcpiphdr);
462 m->m_len -= sizeof(struct tcpiphdr);
463
464 /*
465 * Calculate amount of space in receive window,
466 * and then do TCP input processing.
467 * Receive window is amount of space in rcv queue,
468 * but not less than advertised window.
469 */
470 { int win;
471
472 win = sbspace(&so->so_rcv);
473 if (win < 0)
474 win = 0;
475 tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
476 }
477
478 switch (tp->t_state) {
479
480 /*
481 * If the state is LISTEN then ignore segment if it contains an RST.
482 * If the segment contains an ACK then it is bad and send a RST.
483 * If it does not contain a SYN then it is not interesting; drop it.
484 * Don't bother responding if the destination was a broadcast.
485 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
486 * tp->iss, and send a segment:
487 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
488 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
489 * Fill in remote peer address fields if not previously specified.
490 * Enter SYN_RECEIVED state, and process any other fields of this
491 * segment in this state.
492 */
493 case TCPS_LISTEN: {
494 struct mbuf *am;
495 register struct sockaddr_in *sin;
496
497 if (tiflags & TH_RST)
498 goto drop;
499 if (tiflags & TH_ACK)
500 goto dropwithreset;
501 if ((tiflags & TH_SYN) == 0)
502 goto drop;
503 if (m->m_flags & M_BCAST)
504 goto drop;
505 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
506 if (am == NULL)
507 goto drop;
508 am->m_len = sizeof (struct sockaddr_in);
509 sin = mtod(am, struct sockaddr_in *);
510 sin->sin_family = AF_INET;
511 sin->sin_len = sizeof(*sin);
512 sin->sin_addr = ti->ti_src;
513 sin->sin_port = ti->ti_sport;
514 laddr = inp->inp_laddr;
515 if (inp->inp_laddr.s_addr == INADDR_ANY)
516 inp->inp_laddr = ti->ti_dst;
517 if (in_pcbconnect(inp, am)) {
518 inp->inp_laddr = laddr;
519 (void) m_free(am);
520 goto drop;
521 }
522 (void) m_free(am);
523 tp->t_template = tcp_template(tp);
524 if (tp->t_template == 0) {
525 tp = tcp_drop(tp, ENOBUFS);
526 dropsocket = 0; /* socket is already gone */
527 goto drop;
528 }
529 if (om) {
530 tcp_dooptions(tp, om, ti);
531 om = 0;
532 }
533 if (iss)
534 tp->iss = iss;
535 else
536 tp->iss = tcp_iss;
537 tcp_iss += TCP_ISSINCR/2;
538 tp->irs = ti->ti_seq;
539 tcp_sendseqinit(tp);
540 tcp_rcvseqinit(tp);
541 tp->t_flags |= TF_ACKNOW;
542 tp->t_state = TCPS_SYN_RECEIVED;
543 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
544 dropsocket = 0; /* committed to socket */
545 tcpstat.tcps_accepts++;
546 goto trimthenstep6;
547 }
548
549 /*
550 * If the state is SYN_SENT:
551 * if seg contains an ACK, but not for our SYN, drop the input.
552 * if seg contains a RST, then drop the connection.
553 * if seg does not contain SYN, then drop it.
554 * Otherwise this is an acceptable SYN segment
555 * initialize tp->rcv_nxt and tp->irs
556 * if seg contains ack then advance tp->snd_una
557 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
558 * arrange for segment to be acked (eventually)
559 * continue processing rest of data/controls, beginning with URG
560 */
561 case TCPS_SYN_SENT:
562 if ((tiflags & TH_ACK) &&
563 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
564 SEQ_GT(ti->ti_ack, tp->snd_max)))
565 goto dropwithreset;
566 if (tiflags & TH_RST) {
567 if (tiflags & TH_ACK)
568 tp = tcp_drop(tp, ECONNREFUSED);
569 goto drop;
570 }
571 if ((tiflags & TH_SYN) == 0)
572 goto drop;
573 if (tiflags & TH_ACK) {
574 tp->snd_una = ti->ti_ack;
575 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
576 tp->snd_nxt = tp->snd_una;
577 }
578 tp->t_timer[TCPT_REXMT] = 0;
579 tp->irs = ti->ti_seq;
580 tcp_rcvseqinit(tp);
581 tp->t_flags |= TF_ACKNOW;
582 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
583 tcpstat.tcps_connects++;
584 soisconnected(so);
585 tp->t_state = TCPS_ESTABLISHED;
586 (void) tcp_reass(tp, (struct tcpiphdr *)0,
587 (struct mbuf *)0);
588 /*
589 * if we didn't have to retransmit the SYN,
590 * use its rtt as our initial srtt & rtt var.
591 */
592 if (tp->t_rtt)
593 tcp_xmit_timer(tp);
594 } else
595 tp->t_state = TCPS_SYN_RECEIVED;
596
597 trimthenstep6:
598 /*
599 * Advance ti->ti_seq to correspond to first data byte.
600 * If data, trim to stay within window,
601 * dropping FIN if necessary.
602 */
603 ti->ti_seq++;
604 if (ti->ti_len > tp->rcv_wnd) {
605 todrop = ti->ti_len - tp->rcv_wnd;
606 m_adj(m, -todrop);
607 ti->ti_len = tp->rcv_wnd;
608 tiflags &= ~TH_FIN;
609 tcpstat.tcps_rcvpackafterwin++;
610 tcpstat.tcps_rcvbyteafterwin += todrop;
611 }
612 tp->snd_wl1 = ti->ti_seq - 1;
613 tp->rcv_up = ti->ti_seq;
614 goto step6;
615 }
616
617 /*
618 * States other than LISTEN or SYN_SENT.
619 * First check that at least some bytes of segment are within
620 * receive window. If segment begins before rcv_nxt,
621 * drop leading data (and SYN); if nothing left, just ack.
622 */
623 todrop = tp->rcv_nxt - ti->ti_seq;
624 if (todrop > 0) {
625 if (tiflags & TH_SYN) {
626 tiflags &= ~TH_SYN;
627 ti->ti_seq++;
628 if (ti->ti_urp > 1)
629 ti->ti_urp--;
630 else
631 tiflags &= ~TH_URG;
632 todrop--;
633 }
634 if (todrop > ti->ti_len ||
635 todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
636 /*
637 * Any valid FIN must be to the left of the
638 * window. At this point, FIN must be a
639 * duplicate or out-of-sequence, so drop it.
640 */
641 tiflags &= ~TH_FIN;
642 /*
643 * Send ACK to resynchronize, and drop any data,
644 * but keep on processing for RST or ACK.
645 */
646 tp->t_flags |= TF_ACKNOW;
647 tcpstat.tcps_rcvdupbyte += todrop = ti->ti_len;
648 tcpstat.tcps_rcvduppack++;
649 } else {
650 tcpstat.tcps_rcvpartduppack++;
651 tcpstat.tcps_rcvpartdupbyte += todrop;
652 }
653 m_adj(m, todrop);
654 ti->ti_seq += todrop;
655 ti->ti_len -= todrop;
656 if (ti->ti_urp > todrop)
657 ti->ti_urp -= todrop;
658 else {
659 tiflags &= ~TH_URG;
660 ti->ti_urp = 0;
661 }
662 }
663
664 /*
665 * If new data are received on a connection after the
666 * user processes are gone, then RST the other end.
667 */
668 if ((so->so_state & SS_NOFDREF) &&
669 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
670 tp = tcp_close(tp);
671 tcpstat.tcps_rcvafterclose++;
672 goto dropwithreset;
673 }
674
675 /*
676 * If segment ends after window, drop trailing data
677 * (and PUSH and FIN); if nothing left, just ACK.
678 */
679 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
680 if (todrop > 0) {
681 tcpstat.tcps_rcvpackafterwin++;
682 if (todrop >= ti->ti_len) {
683 tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
684 /*
685 * If a new connection request is received
686 * while in TIME_WAIT, drop the old connection
687 * and start over if the sequence numbers
688 * are above the previous ones.
689 */
690 if (tiflags & TH_SYN &&
691 tp->t_state == TCPS_TIME_WAIT &&
692 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
693 iss = tp->rcv_nxt + TCP_ISSINCR;
694 tp = tcp_close(tp);
695 goto findpcb;
696 }
697 /*
698 * If window is closed can only take segments at
699 * window edge, and have to drop data and PUSH from
700 * incoming segments. Continue processing, but
701 * remember to ack. Otherwise, drop segment
702 * and ack.
703 */
704 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
705 tp->t_flags |= TF_ACKNOW;
706 tcpstat.tcps_rcvwinprobe++;
707 } else
708 goto dropafterack;
709 } else
710 tcpstat.tcps_rcvbyteafterwin += todrop;
711 m_adj(m, -todrop);
712 ti->ti_len -= todrop;
713 tiflags &= ~(TH_PUSH|TH_FIN);
714 }
715
716 /*
717 * If the RST bit is set examine the state:
718 * SYN_RECEIVED STATE:
719 * If passive open, return to LISTEN state.
720 * If active open, inform user that connection was refused.
721 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
722 * Inform user that connection was reset, and close tcb.
723 * CLOSING, LAST_ACK, TIME_WAIT STATES
724 * Close the tcb.
725 */
726 if (tiflags&TH_RST) switch (tp->t_state) {
727
728 case TCPS_SYN_RECEIVED:
729 so->so_error = ECONNREFUSED;
730 goto close;
731
732 case TCPS_ESTABLISHED:
733 case TCPS_FIN_WAIT_1:
734 case TCPS_FIN_WAIT_2:
735 case TCPS_CLOSE_WAIT:
736 so->so_error = ECONNRESET;
737 close:
738 tp->t_state = TCPS_CLOSED;
739 tcpstat.tcps_drops++;
740 tp = tcp_close(tp);
741 goto drop;
742
743 case TCPS_CLOSING:
744 case TCPS_LAST_ACK:
745 case TCPS_TIME_WAIT:
746 tp = tcp_close(tp);
747 goto drop;
748 }
749
750 /*
751 * If a SYN is in the window, then this is an
752 * error and we send an RST and drop the connection.
753 */
754 if (tiflags & TH_SYN) {
755 tp = tcp_drop(tp, ECONNRESET);
756 goto dropwithreset;
757 }
758
759 /*
760 * If the ACK bit is off we drop the segment and return.
761 */
762 if ((tiflags & TH_ACK) == 0)
763 goto drop;
764
765 /*
766 * Ack processing.
767 */
768 switch (tp->t_state) {
769
770 /*
771 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
772 * ESTABLISHED state and continue processing, otherwise
773 * send an RST.
774 */
775 case TCPS_SYN_RECEIVED:
776 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
777 SEQ_GT(ti->ti_ack, tp->snd_max))
778 goto dropwithreset;
779 tcpstat.tcps_connects++;
780 soisconnected(so);
781 tp->t_state = TCPS_ESTABLISHED;
782 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
783 tp->snd_wl1 = ti->ti_seq - 1;
784 /* fall into ... */
785
786 /*
787 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
788 * ACKs. If the ack is in the range
789 * tp->snd_una < ti->ti_ack <= tp->snd_max
790 * then advance tp->snd_una to ti->ti_ack and drop
791 * data from the retransmission queue. If this ACK reflects
792 * more up to date window information we update our window information.
793 */
794 case TCPS_ESTABLISHED:
795 case TCPS_FIN_WAIT_1:
796 case TCPS_FIN_WAIT_2:
797 case TCPS_CLOSE_WAIT:
798 case TCPS_CLOSING:
799 case TCPS_LAST_ACK:
800 case TCPS_TIME_WAIT:
801
802 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
803 if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
804 tcpstat.tcps_rcvdupack++;
805 /*
806 * If we have outstanding data (other than
807 * a window probe), this is a completely
808 * duplicate ack (ie, window info didn't
809 * change), the ack is the biggest we've
810 * seen and we've seen exactly our rexmt
811 * threshhold of them, assume a packet
812 * has been dropped and retransmit it.
813 * Kludge snd_nxt & the congestion
814 * window so we send only this one
815 * packet.
816 *
817 * We know we're losing at the current
818 * window size so do congestion avoidance
819 * (set ssthresh to half the current window
820 * and pull our congestion window back to
821 * the new ssthresh).
822 *
823 * Dup acks mean that packets have left the
824 * network (they're now cached at the receiver)
825 * so bump cwnd by the amount in the receiver
826 * to keep a constant cwnd packets in the
827 * network.
828 */
829 if (tp->t_timer[TCPT_REXMT] == 0 ||
830 ti->ti_ack != tp->snd_una)
831 tp->t_dupacks = 0;
832 else if (++tp->t_dupacks == tcprexmtthresh) {
833 tcp_seq onxt = tp->snd_nxt;
834 u_int win =
835 min(tp->snd_wnd, tp->snd_cwnd) / 2 /
836 tp->t_maxseg;
837
838 if (win < 2)
839 win = 2;
840 tp->snd_ssthresh = win * tp->t_maxseg;
841 tp->t_timer[TCPT_REXMT] = 0;
842 tp->t_rtt = 0;
843 tp->snd_nxt = ti->ti_ack;
844 tp->snd_cwnd = tp->t_maxseg;
845 (void) tcp_output(tp);
846 tp->snd_cwnd = tp->snd_ssthresh +
847 tp->t_maxseg * tp->t_dupacks;
848 if (SEQ_GT(onxt, tp->snd_nxt))
849 tp->snd_nxt = onxt;
850 goto drop;
851 } else if (tp->t_dupacks > tcprexmtthresh) {
852 tp->snd_cwnd += tp->t_maxseg;
853 (void) tcp_output(tp);
854 goto drop;
855 }
856 } else
857 tp->t_dupacks = 0;
858 break;
859 }
860 /*
861 * If the congestion window was inflated to account
862 * for the other side's cached packets, retract it.
863 */
864 if (tp->t_dupacks > tcprexmtthresh &&
865 tp->snd_cwnd > tp->snd_ssthresh)
866 tp->snd_cwnd = tp->snd_ssthresh;
867 tp->t_dupacks = 0;
868 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
869 tcpstat.tcps_rcvacktoomuch++;
870 goto dropafterack;
871 }
872 acked = ti->ti_ack - tp->snd_una;
873 tcpstat.tcps_rcvackpack++;
874 tcpstat.tcps_rcvackbyte += acked;
875
876 /*
877 * If transmit timer is running and timed sequence
878 * number was acked, update smoothed round trip time.
879 * Since we now have an rtt measurement, cancel the
880 * timer backoff (cf., Phil Karn's retransmit alg.).
881 * Recompute the initial retransmit timer.
882 */
883 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
884 tcp_xmit_timer(tp);
885
886 /*
887 * If all outstanding data is acked, stop retransmit
888 * timer and remember to restart (more output or persist).
889 * If there is more data to be acked, restart retransmit
890 * timer, using current (possibly backed-off) value.
891 */
892 if (ti->ti_ack == tp->snd_max) {
893 tp->t_timer[TCPT_REXMT] = 0;
894 needoutput = 1;
895 } else if (tp->t_timer[TCPT_PERSIST] == 0)
896 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
897 /*
898 * When new data is acked, open the congestion window.
899 * If the window gives us less than ssthresh packets
900 * in flight, open exponentially (maxseg per packet).
901 * Otherwise open linearly: maxseg per window
902 * (maxseg^2 / cwnd per packet), plus a constant
903 * fraction of a packet (maxseg/8) to help larger windows
904 * open quickly enough.
905 */
906 {
907 register u_int cw = tp->snd_cwnd;
908 register u_int incr = tp->t_maxseg;
909
910 if (cw > tp->snd_ssthresh)
911 incr = incr * incr / cw + incr / 8;
912 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN);
913 }
914 if (acked > so->so_snd.sb_cc) {
915 tp->snd_wnd -= so->so_snd.sb_cc;
916 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
917 ourfinisacked = 1;
918 } else {
919 sbdrop(&so->so_snd, acked);
920 tp->snd_wnd -= acked;
921 ourfinisacked = 0;
922 }
923 if (so->so_snd.sb_flags & SB_NOTIFY)
924 sowwakeup(so);
925 tp->snd_una = ti->ti_ack;
926 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
927 tp->snd_nxt = tp->snd_una;
928
929 switch (tp->t_state) {
930
931 /*
932 * In FIN_WAIT_1 STATE in addition to the processing
933 * for the ESTABLISHED state if our FIN is now acknowledged
934 * then enter FIN_WAIT_2.
935 */
936 case TCPS_FIN_WAIT_1:
937 if (ourfinisacked) {
938 /*
939 * If we can't receive any more
940 * data, then closing user can proceed.
941 * Starting the timer is contrary to the
942 * specification, but if we don't get a FIN
943 * we'll hang forever.
944 */
945 if (so->so_state & SS_CANTRCVMORE) {
946 soisdisconnected(so);
947 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
948 }
949 tp->t_state = TCPS_FIN_WAIT_2;
950 }
951 break;
952
953 /*
954 * In CLOSING STATE in addition to the processing for
955 * the ESTABLISHED state if the ACK acknowledges our FIN
956 * then enter the TIME-WAIT state, otherwise ignore
957 * the segment.
958 */
959 case TCPS_CLOSING:
960 if (ourfinisacked) {
961 tp->t_state = TCPS_TIME_WAIT;
962 tcp_canceltimers(tp);
963 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
964 soisdisconnected(so);
965 }
966 break;
967
968 /*
969 * In LAST_ACK, we may still be waiting for data to drain
970 * and/or to be acked, as well as for the ack of our FIN.
971 * If our FIN is now acknowledged, delete the TCB,
972 * enter the closed state and return.
973 */
974 case TCPS_LAST_ACK:
975 if (ourfinisacked) {
976 tp = tcp_close(tp);
977 goto drop;
978 }
979 break;
980
981 /*
982 * In TIME_WAIT state the only thing that should arrive
983 * is a retransmission of the remote FIN. Acknowledge
984 * it and restart the finack timer.
985 */
986 case TCPS_TIME_WAIT:
987 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
988 goto dropafterack;
989 }
990 }
991
992 step6:
993 /*
994 * Update window information.
995 * Don't look at window if no ACK: TAC's send garbage on first SYN.
996 */
997 if ((tiflags & TH_ACK) &&
998 (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
999 (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1000 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
1001 /* keep track of pure window updates */
1002 if (ti->ti_len == 0 &&
1003 tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
1004 tcpstat.tcps_rcvwinupd++;
1005 tp->snd_wnd = ti->ti_win;
1006 tp->snd_wl1 = ti->ti_seq;
1007 tp->snd_wl2 = ti->ti_ack;
1008 if (tp->snd_wnd > tp->max_sndwnd)
1009 tp->max_sndwnd = tp->snd_wnd;
1010 needoutput = 1;
1011 }
1012
1013 /*
1014 * Process segments with URG.
1015 */
1016 if ((tiflags & TH_URG) && ti->ti_urp &&
1017 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1018 /*
1019 * This is a kludge, but if we receive and accept
1020 * random urgent pointers, we'll crash in
1021 * soreceive. It's hard to imagine someone
1022 * actually wanting to send this much urgent data.
1023 */
1024 if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
1025 ti->ti_urp = 0; /* XXX */
1026 tiflags &= ~TH_URG; /* XXX */
1027 goto dodata; /* XXX */
1028 }
1029 /*
1030 * If this segment advances the known urgent pointer,
1031 * then mark the data stream. This should not happen
1032 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1033 * a FIN has been received from the remote side.
1034 * In these states we ignore the URG.
1035 *
1036 * According to RFC961 (Assigned Protocols),
1037 * the urgent pointer points to the last octet
1038 * of urgent data. We continue, however,
1039 * to consider it to indicate the first octet
1040 * of data past the urgent section as the original
1041 * spec states (in one of two places).
1042 */
1043 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1044 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1045 so->so_oobmark = so->so_rcv.sb_cc +
1046 (tp->rcv_up - tp->rcv_nxt) - 1;
1047 if (so->so_oobmark == 0)
1048 so->so_state |= SS_RCVATMARK;
1049 sohasoutofband(so);
1050 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1051 }
1052 /*
1053 * Remove out of band data so doesn't get presented to user.
1054 * This can happen independent of advancing the URG pointer,
1055 * but if two URG's are pending at once, some out-of-band
1056 * data may creep in... ick.
1057 */
1058 if (ti->ti_urp <= ti->ti_len
1059 #ifdef SO_OOBINLINE
1060 && (so->so_options & SO_OOBINLINE) == 0
1061 #endif
1062 )
1063 tcp_pulloutofband(so, ti, m);
1064 } else
1065 /*
1066 * If no out of band data is expected,
1067 * pull receive urgent pointer along
1068 * with the receive window.
1069 */
1070 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1071 tp->rcv_up = tp->rcv_nxt;
1072 dodata: /* XXX */
1073
1074 /*
1075 * Process the segment text, merging it into the TCP sequencing queue,
1076 * and arranging for acknowledgment of receipt if necessary.
1077 * This process logically involves adjusting tp->rcv_wnd as data
1078 * is presented to the user (this happens in tcp_usrreq.c,
1079 * case PRU_RCVD). If a FIN has already been received on this
1080 * connection then we just ignore the text.
1081 */
1082 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1083 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1084 TCP_REASS(tp, ti, m, so, tiflags);
1085 /*
1086 * Note the amount of data that peer has sent into
1087 * our window, in order to estimate the sender's
1088 * buffer size.
1089 */
1090 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1091 } else {
1092 m_freem(m);
1093 tiflags &= ~TH_FIN;
1094 }
1095
1096 /*
1097 * If FIN is received ACK the FIN and let the user know
1098 * that the connection is closing.
1099 */
1100 if (tiflags & TH_FIN) {
1101 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1102 socantrcvmore(so);
1103 tp->t_flags |= TF_ACKNOW;
1104 tp->rcv_nxt++;
1105 }
1106 switch (tp->t_state) {
1107
1108 /*
1109 * In SYN_RECEIVED and ESTABLISHED STATES
1110 * enter the CLOSE_WAIT state.
1111 */
1112 case TCPS_SYN_RECEIVED:
1113 case TCPS_ESTABLISHED:
1114 tp->t_state = TCPS_CLOSE_WAIT;
1115 break;
1116
1117 /*
1118 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1119 * enter the CLOSING state.
1120 */
1121 case TCPS_FIN_WAIT_1:
1122 tp->t_state = TCPS_CLOSING;
1123 break;
1124
1125 /*
1126 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1127 * starting the time-wait timer, turning off the other
1128 * standard timers.
1129 */
1130 case TCPS_FIN_WAIT_2:
1131 tp->t_state = TCPS_TIME_WAIT;
1132 tcp_canceltimers(tp);
1133 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1134 soisdisconnected(so);
1135 break;
1136
1137 /*
1138 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1139 */
1140 case TCPS_TIME_WAIT:
1141 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1142 break;
1143 }
1144 }
1145 if (so->so_options & SO_DEBUG)
1146 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1147
1148 /*
1149 * Return any desired output.
1150 */
1151 if (needoutput || (tp->t_flags & TF_ACKNOW))
1152 (void) tcp_output(tp);
1153 return;
1154
1155 dropafterack:
1156 /*
1157 * Generate an ACK dropping incoming segment if it occupies
1158 * sequence space, where the ACK reflects our state.
1159 */
1160 if (tiflags & TH_RST)
1161 goto drop;
1162 m_freem(m);
1163 tp->t_flags |= TF_ACKNOW;
1164 (void) tcp_output(tp);
1165 return;
1166
1167 dropwithreset:
1168 if (om) {
1169 (void) m_free(om);
1170 om = 0;
1171 }
1172 /*
1173 * Generate a RST, dropping incoming segment.
1174 * Make ACK acceptable to originator of segment.
1175 * Don't bother to respond if destination was broadcast.
1176 */
1177 if ((tiflags & TH_RST) || m->m_flags & M_BCAST)
1178 goto drop;
1179 if (tiflags & TH_ACK)
1180 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1181 else {
1182 if (tiflags & TH_SYN)
1183 ti->ti_len++;
1184 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1185 TH_RST|TH_ACK);
1186 }
1187 /* destroy temporarily created socket */
1188 if (dropsocket)
1189 (void) soabort(so);
1190 return;
1191
1192 drop:
1193 if (om)
1194 (void) m_free(om);
1195 /*
1196 * Drop space held by incoming segment and return.
1197 */
1198 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1199 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1200 m_freem(m);
1201 /* destroy temporarily created socket */
1202 if (dropsocket)
1203 (void) soabort(so);
1204 return;
1205 }
1206
1207 void
1208 tcp_dooptions(tp, om, ti)
1209 struct tcpcb *tp;
1210 struct mbuf *om;
1211 struct tcpiphdr *ti;
1212 {
1213 register u_char *cp;
1214 u_short mss;
1215 int opt, optlen, cnt;
1216
1217 cp = mtod(om, u_char *);
1218 cnt = om->m_len;
1219 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1220 opt = cp[0];
1221 if (opt == TCPOPT_EOL)
1222 break;
1223 if (opt == TCPOPT_NOP)
1224 optlen = 1;
1225 else {
1226 optlen = cp[1];
1227 if (optlen <= 0)
1228 break;
1229 }
1230 switch (opt) {
1231
1232 default:
1233 continue;
1234
1235 case TCPOPT_MAXSEG:
1236 if (optlen != 4)
1237 continue;
1238 if (!(ti->ti_flags & TH_SYN))
1239 continue;
1240 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1241 NTOHS(mss);
1242 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1243 break;
1244 }
1245 }
1246 (void) m_free(om);
1247 }
1248
1249 /*
1250 * Pull out of band byte out of a segment so
1251 * it doesn't appear in the user's data queue.
1252 * It is still reflected in the segment length for
1253 * sequencing purposes.
1254 */
1255 void
1256 tcp_pulloutofband(so, ti, m)
1257 struct socket *so;
1258 struct tcpiphdr *ti;
1259 register struct mbuf *m;
1260 {
1261 int cnt = ti->ti_urp - 1;
1262
1263 while (cnt >= 0) {
1264 if (m->m_len > cnt) {
1265 char *cp = mtod(m, caddr_t) + cnt;
1266 struct tcpcb *tp = sototcpcb(so);
1267
1268 tp->t_iobc = *cp;
1269 tp->t_oobflags |= TCPOOB_HAVEDATA;
1270 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1271 m->m_len--;
1272 return;
1273 }
1274 cnt -= m->m_len;
1275 m = m->m_next;
1276 if (m == 0)
1277 break;
1278 }
1279 panic("tcp_pulloutofband");
1280 }
1281
1282 /*
1283 * Collect new round-trip time estimate
1284 * and update averages and current timeout.
1285 */
1286 void
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 int
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