xref: /csrg-svn/sys/netinet/tcp_input.c (revision 8696)
1 /*	tcp_input.c	1.80	82/10/20	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/protosw.h"
7 #include "../h/socket.h"
8 #include "../h/socketvar.h"
9 #include "../netinet/in.h"
10 #include "../net/route.h"
11 #include "../netinet/in_pcb.h"
12 #include "../netinet/in_systm.h"
13 #include "../net/if.h"
14 #include "../netinet/ip.h"
15 #include "../netinet/ip_var.h"
16 #include "../netinet/tcp.h"
17 #include "../netinet/tcp_fsm.h"
18 #include "../netinet/tcp_seq.h"
19 #include "../netinet/tcp_timer.h"
20 #include "../netinet/tcp_var.h"
21 #include "../netinet/tcpip.h"
22 #include "../netinet/tcp_debug.h"
23 #include <errno.h>
24 
25 int	tcpprintfs = 0;
26 int	tcpcksum = 1;
27 struct	tcpiphdr tcp_saveti;
28 extern	tcpnodelack;
29 
30 struct	tcpcb *tcp_newtcpcb();
31 /*
32  * TCP input routine, follows pages 65-76 of the
33  * protocol specification dated September, 1981 very closely.
34  */
35 tcp_input(m0)
36 	struct mbuf *m0;
37 {
38 	register struct tcpiphdr *ti;
39 	struct inpcb *inp;
40 	register struct mbuf *m;
41 	struct mbuf *om = 0;
42 	int len, tlen, off;
43 	register struct tcpcb *tp = 0;
44 	register int tiflags;
45 	struct socket *so;
46 	int todrop, acked;
47 	short ostate;
48 	struct in_addr laddr;
49 
50 	/*
51 	 * Get IP and TCP header together in first mbuf.
52 	 * Note: IP leaves IP header in first mbuf.
53 	 */
54 	m = m0;
55 	ti = mtod(m, struct tcpiphdr *);
56 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
57 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
58 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
59 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
60 			tcpstat.tcps_hdrops++;
61 			return;
62 		}
63 		ti = mtod(m, struct tcpiphdr *);
64 	}
65 
66 	/*
67 	 * Checksum extended TCP header and data.
68 	 */
69 	tlen = ((struct ip *)ti)->ip_len;
70 	len = sizeof (struct ip) + tlen;
71 	if (tcpcksum) {
72 		ti->ti_next = ti->ti_prev = 0;
73 		ti->ti_x1 = 0;
74 		ti->ti_len = (u_short)tlen;
75 #if vax || pdp11 || ns16032
76 		ti->ti_len = htons((u_short)ti->ti_len);
77 #endif
78 		if (ti->ti_sum = in_cksum(m, len)) {
79 			tcpstat.tcps_badsum++;
80 			if (tcpprintfs)
81 				printf("tcp cksum %x\n", ti->ti_sum);
82 			goto drop;
83 		}
84 	}
85 
86 	/*
87 	 * Check that TCP offset makes sense,
88 	 * pull out TCP options and adjust length.
89 	 */
90 	off = ti->ti_off << 2;
91 	if (off < sizeof (struct tcphdr) || off > tlen) {
92 		tcpstat.tcps_badoff++;
93 		goto drop;
94 	}
95 	tlen -= off;
96 	ti->ti_len = tlen;
97 	if (off > sizeof (struct tcphdr)) {
98 		if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
99 			tcpstat.tcps_hdrops++;
100 			goto drop;
101 		}
102 		ti = mtod(m, struct tcpiphdr *);
103 		om = m_get(M_DONTWAIT);
104 		if (om == 0)
105 			goto drop;
106 		om->m_len = off - sizeof (struct tcphdr);
107 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
108 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
109 		  m->m_len -= om->m_len;
110 		  bcopy(op+om->m_len, op,
111 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
112 		}
113 	}
114 	tiflags = ti->ti_flags;
115 
116 	/*
117 	 * Drop TCP and IP headers.
118 	 */
119 	off += sizeof (struct ip);
120 	m->m_off += off;
121 	m->m_len -= off;
122 
123 #if vax || pdp11 || ns16032
124 	/*
125 	 * Convert TCP protocol specific fields to host format.
126 	 */
127 	ti->ti_seq = ntohl(ti->ti_seq);
128 	ti->ti_ack = ntohl(ti->ti_ack);
129 	ti->ti_win = ntohs(ti->ti_win);
130 	ti->ti_urp = ntohs(ti->ti_urp);
131 #endif
132 
133 	/*
134 	 * Locate pcb for segment.
135 	 */
136 	inp = in_pcblookup
137 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
138 		INPLOOKUP_WILDCARD);
139 
140 	/*
141 	 * If the state is CLOSED (i.e., TCB does not exist) then
142 	 * all data in the incoming segment is discarded.
143 	 */
144 	if (inp == 0)
145 		goto dropwithreset;
146 	tp = intotcpcb(inp);
147 	if (tp == 0)
148 		goto dropwithreset;
149 	so = inp->inp_socket;
150 	if (so->so_options & SO_DEBUG) {
151 		ostate = tp->t_state;
152 		tcp_saveti = *ti;
153 	}
154 	if (so->so_options & SO_ACCEPTCONN) {
155 		so = sonewconn(so);
156 		if (so == 0)
157 			goto drop;
158 		inp = (struct inpcb *)so->so_pcb;
159 		inp->inp_laddr = ti->ti_dst;
160 		inp->inp_lport = ti->ti_dport;
161 		tp = intotcpcb(inp);
162 		tp->t_state = TCPS_LISTEN;
163 	}
164 
165 	/*
166 	 * Segment received on connection.
167 	 * Reset idle time and keep-alive timer.
168 	 */
169 	tp->t_idle = 0;
170 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
171 
172 	/*
173 	 * Process options.
174 	 */
175 	if (om) {
176 		tcp_dooptions(tp, om);
177 		om = 0;
178 	}
179 
180 	/*
181 	 * Calculate amount of space in receive window,
182 	 * and then do TCP input processing.
183 	 */
184 	tp->rcv_wnd = sbspace(&so->so_rcv);
185 	if (tp->rcv_wnd < 0)
186 		tp->rcv_wnd = 0;
187 
188 	switch (tp->t_state) {
189 
190 	/*
191 	 * If the state is LISTEN then ignore segment if it contains an RST.
192 	 * If the segment contains an ACK then it is bad and send a RST.
193 	 * If it does not contain a SYN then it is not interesting; drop it.
194 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
195 	 * tp->iss, and send a segment:
196 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
197 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
198 	 * Fill in remote peer address fields if not previously specified.
199 	 * Enter SYN_RECEIVED state, and process any other fields of this
200 	 * segment in this state.
201 	 */
202 	case TCPS_LISTEN: {
203 		struct mbuf *am = m_get(M_DONTWAIT);
204 		register struct sockaddr_in *sin;
205 
206 		if (am == 0)
207 			goto drop;
208 		am->m_len = sizeof (struct sockaddr_in);
209 		if (tiflags & TH_RST)
210 			goto drop;
211 		if (tiflags & TH_ACK)
212 			goto dropwithreset;
213 		if ((tiflags & TH_SYN) == 0)
214 			goto drop;
215 		sin = mtod(am, struct sockaddr_in *);
216 		sin->sin_family = AF_INET;
217 		sin->sin_addr = ti->ti_src;
218 		sin->sin_port = ti->ti_sport;
219 		laddr = inp->inp_laddr;
220 		if (inp->inp_laddr.s_addr == 0)
221 			inp->inp_laddr = ti->ti_dst;
222 		if (in_pcbconnect(inp, am)) {
223 			inp->inp_laddr = laddr;
224 			m_free(am);
225 			goto drop;
226 		}
227 		m_free(am);
228 		tp->t_template = tcp_template(tp);
229 		if (tp->t_template == 0) {
230 			in_pcbdisconnect(inp);
231 			inp->inp_laddr = laddr;
232 			tp = 0;
233 			goto drop;
234 		}
235 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
236 		tp->irs = ti->ti_seq;
237 		tcp_sendseqinit(tp);
238 		tcp_rcvseqinit(tp);
239 		tp->t_state = TCPS_SYN_RECEIVED;
240 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
241 		goto trimthenstep6;
242 		}
243 
244 	/*
245 	 * If the state is SYN_SENT:
246 	 *	if seg contains an ACK, but not for our SYN, drop the input.
247 	 *	if seg contains a RST, then drop the connection.
248 	 *	if seg does not contain SYN, then drop it.
249 	 * Otherwise this is an acceptable SYN segment
250 	 *	initialize tp->rcv_nxt and tp->irs
251 	 *	if seg contains ack then advance tp->snd_una
252 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
253 	 *	arrange for segment to be acked (eventually)
254 	 *	continue processing rest of data/controls, beginning with URG
255 	 */
256 	case TCPS_SYN_SENT:
257 		if ((tiflags & TH_ACK) &&
258 /* this should be SEQ_LT; is SEQ_LEQ for BBN vax TCP only */
259 		    (SEQ_LT(ti->ti_ack, tp->iss) ||
260 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
261 			goto dropwithreset;
262 		if (tiflags & TH_RST) {
263 			if (tiflags & TH_ACK) {
264 				tcp_drop(tp, ECONNREFUSED);
265 				tp = 0;
266 			}
267 			goto drop;
268 		}
269 		if ((tiflags & TH_SYN) == 0)
270 			goto drop;
271 		tp->snd_una = ti->ti_ack;
272 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
273 			tp->snd_nxt = tp->snd_una;
274 		tp->t_timer[TCPT_REXMT] = 0;
275 		tp->irs = ti->ti_seq;
276 		tcp_rcvseqinit(tp);
277 		tp->t_flags |= TF_ACKNOW;
278 		if (SEQ_GT(tp->snd_una, tp->iss)) {
279 			soisconnected(so);
280 			tp->t_state = TCPS_ESTABLISHED;
281 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
282 		} else
283 			tp->t_state = TCPS_SYN_RECEIVED;
284 		goto trimthenstep6;
285 
286 trimthenstep6:
287 		/*
288 		 * Advance ti->ti_seq to correspond to first data byte.
289 		 * If data, trim to stay within window,
290 		 * dropping FIN if necessary.
291 		 */
292 		ti->ti_seq++;
293 		if (ti->ti_len > tp->rcv_wnd) {
294 			todrop = ti->ti_len - tp->rcv_wnd;
295 			m_adj(m, -todrop);
296 			ti->ti_len = tp->rcv_wnd;
297 			ti->ti_flags &= ~TH_FIN;
298 		}
299 		tp->snd_wl1 = ti->ti_seq - 1;
300 		goto step6;
301 	}
302 
303 	/*
304 	 * States other than LISTEN or SYN_SENT.
305 	 * First check that at least some bytes of segment are within
306 	 * receive window.
307 	 */
308 	if (tp->rcv_wnd == 0) {
309 		/*
310 		 * If window is closed can only take segments at
311 		 * window edge, and have to drop data and PUSH from
312 		 * incoming segments.
313 		 */
314 		if (tp->rcv_nxt != ti->ti_seq)
315 			goto dropafterack;
316 		if (ti->ti_len > 0) {
317 			m_adj(m, ti->ti_len);
318 			ti->ti_len = 0;
319 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
320 		}
321 	} else {
322 		/*
323 		 * If segment begins before rcv_nxt, drop leading
324 		 * data (and SYN); if nothing left, just ack.
325 		 */
326 		todrop = tp->rcv_nxt - ti->ti_seq;
327 		if (todrop > 0) {
328 			if (tiflags & TH_SYN) {
329 				tiflags &= ~TH_SYN;
330 				ti->ti_flags &= ~TH_SYN;
331 				ti->ti_seq++;
332 				if (ti->ti_urp > 1)
333 					ti->ti_urp--;
334 				else
335 					tiflags &= ~TH_URG;
336 				todrop--;
337 			}
338 			if (todrop > ti->ti_len ||
339 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
340 				goto dropafterack;
341 			m_adj(m, todrop);
342 			ti->ti_seq += todrop;
343 			ti->ti_len -= todrop;
344 			if (ti->ti_urp > todrop)
345 				ti->ti_urp -= todrop;
346 			else {
347 				tiflags &= ~TH_URG;
348 				ti->ti_flags &= ~TH_URG;
349 				ti->ti_urp = 0;
350 			}
351 		}
352 		/*
353 		 * If segment ends after window, drop trailing data
354 		 * (and PUSH and FIN); if nothing left, just ACK.
355 		 */
356 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
357 		if (todrop > 0) {
358 			if (todrop >= ti->ti_len)
359 				goto dropafterack;
360 			m_adj(m, -todrop);
361 			ti->ti_len -= todrop;
362 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
363 		}
364 	}
365 
366 	/*
367 	 * If a segment is received on a connection after the
368 	 * user processes are gone, then RST the other end.
369 	 */
370 	if (so->so_state & SS_NOFDREF) {
371 		tcp_close(tp);
372 		tp = 0;
373 		goto dropwithreset;
374 	}
375 
376 	/*
377 	 * If the RST bit is set examine the state:
378 	 *    SYN_RECEIVED STATE:
379 	 *	If passive open, return to LISTEN state.
380 	 *	If active open, inform user that connection was refused.
381 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
382 	 *	Inform user that connection was reset, and close tcb.
383 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
384 	 *	Close the tcb.
385 	 */
386 	if (tiflags&TH_RST) switch (tp->t_state) {
387 
388 	case TCPS_SYN_RECEIVED:
389 		tcp_drop(tp, ECONNREFUSED);
390 		tp = 0;
391 		goto drop;
392 
393 	case TCPS_ESTABLISHED:
394 	case TCPS_FIN_WAIT_1:
395 	case TCPS_FIN_WAIT_2:
396 	case TCPS_CLOSE_WAIT:
397 		tcp_drop(tp, ECONNRESET);
398 		tp = 0;
399 		goto drop;
400 
401 	case TCPS_CLOSING:
402 	case TCPS_LAST_ACK:
403 	case TCPS_TIME_WAIT:
404 		tcp_close(tp);
405 		tp = 0;
406 		goto drop;
407 	}
408 
409 	/*
410 	 * If a SYN is in the window, then this is an
411 	 * error and we send an RST and drop the connection.
412 	 */
413 	if (tiflags & TH_SYN) {
414 		tcp_drop(tp, ECONNRESET);
415 		tp = 0;
416 		goto dropwithreset;
417 	}
418 
419 	/*
420 	 * If the ACK bit is off we drop the segment and return.
421 	 */
422 	if ((tiflags & TH_ACK) == 0)
423 		goto drop;
424 
425 	/*
426 	 * Ack processing.
427 	 */
428 	switch (tp->t_state) {
429 
430 	/*
431 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
432 	 * ESTABLISHED state and continue processing, othewise
433 	 * send an RST.
434 	 */
435 	case TCPS_SYN_RECEIVED:
436 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
437 		    SEQ_GT(ti->ti_ack, tp->snd_max))
438 			goto dropwithreset;
439 		tp->snd_una++;			/* SYN acked */
440 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
441 			tp->snd_nxt = tp->snd_una;
442 		tp->t_timer[TCPT_REXMT] = 0;
443 		soisconnected(so);
444 		tp->t_state = TCPS_ESTABLISHED;
445 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
446 		tp->snd_wl1 = ti->ti_seq - 1;
447 		/* fall into ... */
448 
449 	/*
450 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
451 	 * ACKs.  If the ack is in the range
452 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
453 	 * then advance tp->snd_una to ti->ti_ack and drop
454 	 * data from the retransmission queue.  If this ACK reflects
455 	 * more up to date window information we update our window information.
456 	 */
457 	case TCPS_ESTABLISHED:
458 	case TCPS_FIN_WAIT_1:
459 	case TCPS_FIN_WAIT_2:
460 	case TCPS_CLOSE_WAIT:
461 	case TCPS_CLOSING:
462 	case TCPS_LAST_ACK:
463 	case TCPS_TIME_WAIT:
464 #define	ourfinisacked	(acked > 0)
465 
466 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
467 			break;
468 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
469 			goto dropafterack;
470 		acked = ti->ti_ack - tp->snd_una;
471 
472 		/*
473 		 * If transmit timer is running and timed sequence
474 		 * number was acked, update smoothed round trip time.
475 		 */
476 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
477 			if (tp->t_srtt == 0)
478 				tp->t_srtt = tp->t_rtt;
479 			else
480 				tp->t_srtt =
481 				    tcp_alpha * tp->t_srtt +
482 				    (1 - tcp_alpha) * tp->t_rtt;
483 /* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
484 			tp->t_rtt = 0;
485 		}
486 
487 		if (ti->ti_ack == tp->snd_max)
488 			tp->t_timer[TCPT_REXMT] = 0;
489 		else {
490 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
491 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
492 			tp->t_rtt = 1;
493 			tp->t_rxtshift = 0;
494 		}
495 		if (acked > so->so_snd.sb_cc) {
496 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
497 			tp->snd_wnd -= so->so_snd.sb_cc;
498 		} else {
499 			sbdrop(&so->so_snd, acked);
500 			tp->snd_wnd -= acked;
501 			acked = 0;
502 		}
503 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
504 			sowwakeup(so);
505 		tp->snd_una = ti->ti_ack;
506 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
507 			tp->snd_nxt = tp->snd_una;
508 
509 		switch (tp->t_state) {
510 
511 		/*
512 		 * In FIN_WAIT_1 STATE in addition to the processing
513 		 * for the ESTABLISHED state if our FIN is now acknowledged
514 		 * then enter FIN_WAIT_2.
515 		 */
516 		case TCPS_FIN_WAIT_1:
517 			if (ourfinisacked) {
518 				/*
519 				 * If we can't receive any more
520 				 * data, then closing user can proceed.
521 				 */
522 				if (so->so_state & SS_CANTRCVMORE)
523 					soisdisconnected(so);
524 				tp->t_state = TCPS_FIN_WAIT_2;
525 			}
526 			break;
527 
528 	 	/*
529 		 * In CLOSING STATE in addition to the processing for
530 		 * the ESTABLISHED state if the ACK acknowledges our FIN
531 		 * then enter the TIME-WAIT state, otherwise ignore
532 		 * the segment.
533 		 */
534 		case TCPS_CLOSING:
535 			if (ourfinisacked) {
536 				tp->t_state = TCPS_TIME_WAIT;
537 				tcp_canceltimers(tp);
538 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
539 				soisdisconnected(so);
540 			}
541 			break;
542 
543 		/*
544 		 * The only thing that can arrive in  LAST_ACK state
545 		 * is an acknowledgment of our FIN.  If our FIN is now
546 		 * acknowledged, delete the TCB, enter the closed state
547 		 * and return.
548 		 */
549 		case TCPS_LAST_ACK:
550 			if (ourfinisacked) {
551 				tcp_close(tp);
552 				tp = 0;
553 			}
554 			goto drop;
555 
556 		/*
557 		 * In TIME_WAIT state the only thing that should arrive
558 		 * is a retransmission of the remote FIN.  Acknowledge
559 		 * it and restart the finack timer.
560 		 */
561 		case TCPS_TIME_WAIT:
562 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
563 			goto dropafterack;
564 		}
565 #undef ourfinisacked
566 	}
567 
568 step6:
569 	/*
570 	 * Update window information.
571 	 */
572 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
573 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
574 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
575 		tp->snd_wnd = ti->ti_win;
576 		tp->snd_wl1 = ti->ti_seq;
577 		tp->snd_wl2 = ti->ti_ack;
578 		if (tp->snd_wnd != 0)
579 			tp->t_timer[TCPT_PERSIST] = 0;
580 	}
581 
582 	/*
583 	 * Process segments with URG.
584 	 */
585 	if ((tiflags & TH_URG) && ti->ti_urp &&
586 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
587 		/*
588 		 * If this segment advances the known urgent pointer,
589 		 * then mark the data stream.  This should not happen
590 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
591 		 * a FIN has been received from the remote side.
592 		 * In these states we ignore the URG.
593 		 */
594 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
595 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
596 			so->so_oobmark = so->so_rcv.sb_cc +
597 			    (tp->rcv_up - tp->rcv_nxt) - 1;
598 			if (so->so_oobmark == 0)
599 				so->so_state |= SS_RCVATMARK;
600 			sohasoutofband(so);
601 			tp->t_oobflags &= ~TCPOOB_HAVEDATA;
602 		}
603 		/*
604 		 * Remove out of band data so doesn't get presented to user.
605 		 * This can happen independent of advancing the URG pointer,
606 		 * but if two URG's are pending at once, some out-of-band
607 		 * data may creep in... ick.
608 		 */
609 		if (ti->ti_urp <= ti->ti_len)
610 			tcp_pulloutofband(so, ti);
611 	}
612 
613 	/*
614 	 * Process the segment text, merging it into the TCP sequencing queue,
615 	 * and arranging for acknowledgment of receipt if necessary.
616 	 * This process logically involves adjusting tp->rcv_wnd as data
617 	 * is presented to the user (this happens in tcp_usrreq.c,
618 	 * case PRU_RCVD).  If a FIN has already been received on this
619 	 * connection then we just ignore the text.
620 	 */
621 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
622 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
623 		tiflags = tcp_reass(tp, ti);
624 		if (tcpnodelack == 0)
625 			tp->t_flags |= TF_DELACK;
626 		else
627 			tp->t_flags |= TF_ACKNOW;
628 	} else {
629 		m_freem(m);
630 		tiflags &= ~TH_FIN;
631 	}
632 
633 	/*
634 	 * If FIN is received ACK the FIN and let the user know
635 	 * that the connection is closing.
636 	 */
637 	if (tiflags & TH_FIN) {
638 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
639 			socantrcvmore(so);
640 			tp->t_flags |= TF_ACKNOW;
641 			tp->rcv_nxt++;
642 		}
643 		switch (tp->t_state) {
644 
645 	 	/*
646 		 * In SYN_RECEIVED and ESTABLISHED STATES
647 		 * enter the CLOSE_WAIT state.
648 		 */
649 		case TCPS_SYN_RECEIVED:
650 		case TCPS_ESTABLISHED:
651 			tp->t_state = TCPS_CLOSE_WAIT;
652 			break;
653 
654 	 	/*
655 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
656 		 * enter the CLOSING state.
657 		 */
658 		case TCPS_FIN_WAIT_1:
659 			tp->t_state = TCPS_CLOSING;
660 			break;
661 
662 	 	/*
663 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
664 		 * starting the time-wait timer, turning off the other
665 		 * standard timers.
666 		 */
667 		case TCPS_FIN_WAIT_2:
668 			tp->t_state = TCPS_TIME_WAIT;
669 			tcp_canceltimers(tp);
670 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
671 			soisdisconnected(so);
672 			break;
673 
674 		/*
675 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
676 		 */
677 		case TCPS_TIME_WAIT:
678 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
679 			break;
680 		}
681 	}
682 	if (so->so_options & SO_DEBUG)
683 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
684 
685 	/*
686 	 * Return any desired output.
687 	 */
688 	(void) tcp_output(tp);
689 	return;
690 
691 dropafterack:
692 	/*
693 	 * Generate an ACK dropping incoming segment if it occupies
694 	 * sequence space, where the ACK reflects our state.
695 	 */
696 	if ((tiflags&TH_RST) ||
697 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
698 		goto drop;
699 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
700 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
701 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
702 	return;
703 
704 dropwithreset:
705 	if (om)
706 		(void) m_free(om);
707 	/*
708 	 * Generate a RST, dropping incoming segment.
709 	 * Make ACK acceptable to originator of segment.
710 	 */
711 	if (tiflags & TH_RST)
712 		goto drop;
713 	if (tiflags & TH_ACK)
714 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
715 	else {
716 		if (tiflags & TH_SYN)
717 			ti->ti_len++;
718 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
719 		    TH_RST|TH_ACK);
720 	}
721 	return;
722 
723 drop:
724 	/*
725 	 * Drop space held by incoming segment and return.
726 	 */
727 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
728 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
729 	m_freem(m);
730 	return;
731 }
732 
733 tcp_dooptions(tp, om)
734 	struct tcpcb *tp;
735 	struct mbuf *om;
736 {
737 	register u_char *cp;
738 	int opt, optlen, cnt;
739 
740 	cp = mtod(om, u_char *);
741 	cnt = om->m_len;
742 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
743 		opt = cp[0];
744 		if (opt == TCPOPT_EOL)
745 			break;
746 		if (opt == TCPOPT_NOP)
747 			optlen = 1;
748 		else
749 			optlen = cp[1];
750 		switch (opt) {
751 
752 		default:
753 			break;
754 
755 		case TCPOPT_MAXSEG:
756 			if (optlen != 4)
757 				continue;
758 			tp->t_maxseg = *(u_short *)(cp + 2);
759 #if vax || pdp11 || ns16032
760 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
761 #endif
762 			break;
763 		}
764 	}
765 	(void) m_free(om);
766 }
767 
768 /*
769  * Pull out of band byte out of a segment so
770  * it doesn't appear in the user's data queue.
771  * It is still reflected in the segment length for
772  * sequencing purposes.
773  */
774 tcp_pulloutofband(so, ti)
775 	struct socket *so;
776 	struct tcpiphdr *ti;
777 {
778 	register struct mbuf *m;
779 	int cnt = ti->ti_urp - 1;
780 
781 	m = dtom(ti);
782 	while (cnt >= 0) {
783 		if (m->m_len > cnt) {
784 			char *cp = mtod(m, caddr_t) + cnt;
785 			struct tcpcb *tp = sototcpcb(so);
786 
787 			tp->t_iobc = *cp;
788 			tp->t_oobflags |= TCPOOB_HAVEDATA;
789 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
790 			m->m_len--;
791 			return;
792 		}
793 		cnt -= m->m_len;
794 		m = m->m_next;
795 		if (m == 0)
796 			break;
797 	}
798 	panic("tcp_pulloutofband");
799 }
800 
801 /*
802  * Insert segment ti into reassembly queue of tcp with
803  * control block tp.  Return TH_FIN if reassembly now includes
804  * a segment with FIN.
805  */
806 tcp_reass(tp, ti)
807 	register struct tcpcb *tp;
808 	register struct tcpiphdr *ti;
809 {
810 	register struct tcpiphdr *q;
811 	struct socket *so = tp->t_inpcb->inp_socket;
812 	struct mbuf *m;
813 	int flags;
814 
815 	/*
816 	 * Call with ti==0 after become established to
817 	 * force pre-ESTABLISHED data up to user socket.
818 	 */
819 	if (ti == 0)
820 		goto present;
821 
822 	/*
823 	 * Find a segment which begins after this one does.
824 	 */
825 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
826 	    q = (struct tcpiphdr *)q->ti_next)
827 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
828 			break;
829 
830 	/*
831 	 * If there is a preceding segment, it may provide some of
832 	 * our data already.  If so, drop the data from the incoming
833 	 * segment.  If it provides all of our data, drop us.
834 	 */
835 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
836 		register int i;
837 		q = (struct tcpiphdr *)q->ti_prev;
838 		/* conversion to int (in i) handles seq wraparound */
839 		i = q->ti_seq + q->ti_len - ti->ti_seq;
840 		if (i > 0) {
841 			if (i >= ti->ti_len)
842 				goto drop;
843 			m_adj(dtom(ti), i);
844 			ti->ti_len -= i;
845 			ti->ti_seq += i;
846 		}
847 		q = (struct tcpiphdr *)(q->ti_next);
848 	}
849 
850 	/*
851 	 * While we overlap succeeding segments trim them or,
852 	 * if they are completely covered, dequeue them.
853 	 */
854 	while (q != (struct tcpiphdr *)tp) {
855 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
856 		if (i <= 0)
857 			break;
858 		if (i < q->ti_len) {
859 			q->ti_seq += i;
860 			q->ti_len -= i;
861 			m_adj(dtom(q), i);
862 			break;
863 		}
864 		q = (struct tcpiphdr *)q->ti_next;
865 		m = dtom(q->ti_prev);
866 		remque(q->ti_prev);
867 		m_freem(m);
868 	}
869 
870 	/*
871 	 * Stick new segment in its place.
872 	 */
873 	insque(ti, q->ti_prev);
874 
875 present:
876 	/*
877 	 * Present data to user, advancing rcv_nxt through
878 	 * completed sequence space.
879 	 */
880 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
881 		return (0);
882 	ti = tp->seg_next;
883 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
884 		return (0);
885 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
886 		return (0);
887 	do {
888 		tp->rcv_nxt += ti->ti_len;
889 		flags = ti->ti_flags & TH_FIN;
890 		remque(ti);
891 		m = dtom(ti);
892 		ti = (struct tcpiphdr *)ti->ti_next;
893 		if (so->so_state & SS_CANTRCVMORE)
894 			m_freem(m);
895 		else {
896 SBCHECK(&so->so_rcv, "tcp_input before");
897 			sbappend(&so->so_rcv, m);
898 SBCHECK(&so->so_rcv, "tcp_input after");
899 		}
900 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
901 	sorwakeup(so);
902 	return (flags);
903 drop:
904 	m_freem(dtom(ti));
905 	return (0);
906 }
907