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