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