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