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