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