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