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