xref: /csrg-svn/sys/netinet/tcp_input.c (revision 25729)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tcp_input.c	6.18 (Berkeley) 01/07/86
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "mbuf.h"
12 #include "protosw.h"
13 #include "socket.h"
14 #include "socketvar.h"
15 #include "errno.h"
16 
17 #include "../net/if.h"
18 #include "../net/route.h"
19 
20 #include "in.h"
21 #include "in_pcb.h"
22 #include "in_systm.h"
23 #include "ip.h"
24 #include "ip_var.h"
25 #include "tcp.h"
26 #include "tcp_fsm.h"
27 #include "tcp_seq.h"
28 #include "tcp_timer.h"
29 #include "tcp_var.h"
30 #include "tcpip.h"
31 #include "tcp_debug.h"
32 
33 int	tcpprintfs = 0;
34 int	tcpcksum = 1;
35 struct	tcpiphdr tcp_saveti;
36 extern	tcpnodelack;
37 
38 struct	tcpcb *tcp_newtcpcb();
39 
40 /*
41  * Insert segment ti into reassembly queue of tcp with
42  * control block tp.  Return TH_FIN if reassembly now includes
43  * a segment with FIN.  The macro form does the common case inline
44  * (segment is the next to be received on an established connection,
45  * and the queue is empty), avoiding linkage into and removal
46  * from the queue and repetition of various conversions.
47  */
48 #define	TCP_REASS(tp, ti, m, so, flags) { \
49 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
50 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
51 	    (tp)->t_state == TCPS_ESTABLISHED) { \
52 		(tp)->rcv_nxt += (ti)->ti_len; \
53 		flags = (ti)->ti_flags & TH_FIN; \
54 		sbappend(&(so)->so_rcv, (m)); \
55 		sorwakeup(so); \
56 	} else \
57 		(flags) = tcp_reass((tp), (ti)); \
58 }
59 
60 tcp_reass(tp, ti)
61 	register struct tcpcb *tp;
62 	register struct tcpiphdr *ti;
63 {
64 	register struct tcpiphdr *q;
65 	struct socket *so = tp->t_inpcb->inp_socket;
66 	struct mbuf *m;
67 	int flags;
68 
69 	/*
70 	 * Call with ti==0 after become established to
71 	 * force pre-ESTABLISHED data up to user socket.
72 	 */
73 	if (ti == 0)
74 		goto present;
75 
76 	/*
77 	 * Find a segment which begins after this one does.
78 	 */
79 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
80 	    q = (struct tcpiphdr *)q->ti_next)
81 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
82 			break;
83 
84 	/*
85 	 * If there is a preceding segment, it may provide some of
86 	 * our data already.  If so, drop the data from the incoming
87 	 * segment.  If it provides all of our data, drop us.
88 	 */
89 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
90 		register int i;
91 		q = (struct tcpiphdr *)q->ti_prev;
92 		/* conversion to int (in i) handles seq wraparound */
93 		i = q->ti_seq + q->ti_len - ti->ti_seq;
94 		if (i > 0) {
95 			if (i >= ti->ti_len)
96 				goto drop;
97 			m_adj(dtom(ti), i);
98 			ti->ti_len -= i;
99 			ti->ti_seq += i;
100 		}
101 		q = (struct tcpiphdr *)(q->ti_next);
102 	}
103 
104 	/*
105 	 * While we overlap succeeding segments trim them or,
106 	 * if they are completely covered, dequeue them.
107 	 */
108 	while (q != (struct tcpiphdr *)tp) {
109 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
110 		if (i <= 0)
111 			break;
112 		if (i < q->ti_len) {
113 			q->ti_seq += i;
114 			q->ti_len -= i;
115 			m_adj(dtom(q), i);
116 			break;
117 		}
118 		q = (struct tcpiphdr *)q->ti_next;
119 		m = dtom(q->ti_prev);
120 		remque(q->ti_prev);
121 		m_freem(m);
122 	}
123 
124 	/*
125 	 * Stick new segment in its place.
126 	 */
127 	insque(ti, q->ti_prev);
128 
129 present:
130 	/*
131 	 * Present data to user, advancing rcv_nxt through
132 	 * completed sequence space.
133 	 */
134 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
135 		return (0);
136 	ti = tp->seg_next;
137 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
138 		return (0);
139 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
140 		return (0);
141 	do {
142 		tp->rcv_nxt += ti->ti_len;
143 		flags = ti->ti_flags & TH_FIN;
144 		remque(ti);
145 		m = dtom(ti);
146 		ti = (struct tcpiphdr *)ti->ti_next;
147 		if (so->so_state & SS_CANTRCVMORE)
148 			m_freem(m);
149 		else
150 			sbappend(&so->so_rcv, m);
151 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
152 	sorwakeup(so);
153 	return (flags);
154 drop:
155 	m_freem(dtom(ti));
156 	return (0);
157 }
158 
159 /*
160  * TCP input routine, follows pages 65-76 of the
161  * protocol specification dated September, 1981 very closely.
162  */
163 tcp_input(m0)
164 	struct mbuf *m0;
165 {
166 	register struct tcpiphdr *ti;
167 	struct inpcb *inp;
168 	register struct mbuf *m;
169 	struct mbuf *om = 0;
170 	int len, tlen, off;
171 	register struct tcpcb *tp = 0;
172 	register int tiflags;
173 	struct socket *so;
174 	int todrop, acked;
175 	short ostate;
176 	struct in_addr laddr;
177 	int dropsocket = 0;
178 
179 	/*
180 	 * Get IP and TCP header together in first mbuf.
181 	 * Note: IP leaves IP header in first mbuf.
182 	 */
183 	m = m0;
184 	ti = mtod(m, struct tcpiphdr *);
185 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
186 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
187 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
188 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
189 			tcpstat.tcps_hdrops++;
190 			return;
191 		}
192 		ti = mtod(m, struct tcpiphdr *);
193 	}
194 
195 	/*
196 	 * Checksum extended TCP header and data.
197 	 */
198 	tlen = ((struct ip *)ti)->ip_len;
199 	len = sizeof (struct ip) + tlen;
200 	if (tcpcksum) {
201 		ti->ti_next = ti->ti_prev = 0;
202 		ti->ti_x1 = 0;
203 		ti->ti_len = (u_short)tlen;
204 		ti->ti_len = htons((u_short)ti->ti_len);
205 		if (ti->ti_sum = in_cksum(m, len)) {
206 			if (tcpprintfs)
207 				printf("tcp sum: src %x\n", ti->ti_src);
208 			tcpstat.tcps_badsum++;
209 			goto drop;
210 		}
211 	}
212 
213 	/*
214 	 * Check that TCP offset makes sense,
215 	 * pull out TCP options and adjust length.
216 	 */
217 	off = ti->ti_off << 2;
218 	if (off < sizeof (struct tcphdr) || off > tlen) {
219 		if (tcpprintfs)
220 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
221 		tcpstat.tcps_badoff++;
222 		goto drop;
223 	}
224 	tlen -= off;
225 	ti->ti_len = tlen;
226 	if (off > sizeof (struct tcphdr)) {
227 		if (m->m_len < sizeof(struct ip) + off) {
228 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
229 				tcpstat.tcps_hdrops++;
230 				return;
231 			}
232 			ti = mtod(m, struct tcpiphdr *);
233 		}
234 		om = m_get(M_DONTWAIT, MT_DATA);
235 		if (om == 0)
236 			goto drop;
237 		om->m_len = off - sizeof (struct tcphdr);
238 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
239 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
240 		  m->m_len -= om->m_len;
241 		  bcopy(op+om->m_len, op,
242 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
243 		}
244 	}
245 	tiflags = ti->ti_flags;
246 
247 	/*
248 	 * Drop TCP and IP headers; TCP options were dropped above.
249 	 */
250 	m->m_off += sizeof(struct tcpiphdr);
251 	m->m_len -= sizeof(struct tcpiphdr);
252 
253 	/*
254 	 * Convert TCP protocol specific fields to host format.
255 	 */
256 	ti->ti_seq = ntohl(ti->ti_seq);
257 	ti->ti_ack = ntohl(ti->ti_ack);
258 	ti->ti_win = ntohs(ti->ti_win);
259 	ti->ti_urp = ntohs(ti->ti_urp);
260 
261 	/*
262 	 * Locate pcb for segment.
263 	 */
264 	inp = in_pcblookup
265 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
266 		INPLOOKUP_WILDCARD);
267 
268 	/*
269 	 * If the state is CLOSED (i.e., TCB does not exist) then
270 	 * all data in the incoming segment is discarded.
271 	 */
272 	if (inp == 0)
273 		goto dropwithreset;
274 	tp = intotcpcb(inp);
275 	if (tp == 0)
276 		goto dropwithreset;
277 	so = inp->inp_socket;
278 	if (so->so_options & SO_DEBUG) {
279 		ostate = tp->t_state;
280 		tcp_saveti = *ti;
281 	}
282 	if (so->so_options & SO_ACCEPTCONN) {
283 		so = sonewconn(so);
284 		if (so == 0)
285 			goto drop;
286 		/*
287 		 * This is ugly, but ....
288 		 *
289 		 * Mark socket as temporary until we're
290 		 * committed to keeping it.  The code at
291 		 * ``drop'' and ``dropwithreset'' check the
292 		 * flag dropsocket to see if the temporary
293 		 * socket created here should be discarded.
294 		 * We mark the socket as discardable until
295 		 * we're committed to it below in TCPS_LISTEN.
296 		 */
297 		dropsocket++;
298 		inp = (struct inpcb *)so->so_pcb;
299 		inp->inp_laddr = ti->ti_dst;
300 		inp->inp_lport = ti->ti_dport;
301 		inp->inp_options = ip_srcroute();
302 		tp = intotcpcb(inp);
303 		tp->t_state = TCPS_LISTEN;
304 	}
305 
306 	/*
307 	 * Segment received on connection.
308 	 * Reset idle time and keep-alive timer.
309 	 */
310 	tp->t_idle = 0;
311 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
312 
313 	/*
314 	 * Process options if not in LISTEN state,
315 	 * else do it below (after getting remote address).
316 	 */
317 	if (om && tp->t_state != TCPS_LISTEN) {
318 		tcp_dooptions(tp, om, ti);
319 		om = 0;
320 	}
321 
322 	/*
323 	 * Calculate amount of space in receive window,
324 	 * and then do TCP input processing.
325 	 * Receive window is amount of space in rcv queue,
326 	 * but not less than advertised window.
327 	 */
328 	tp->rcv_wnd = sbspace(&so->so_rcv);
329 	if (tp->rcv_wnd < 0)
330 		tp->rcv_wnd = 0;
331 	tp->rcv_wnd = MAX(tp->rcv_wnd, (short)(tp->rcv_adv - tp->rcv_nxt));
332 
333 	switch (tp->t_state) {
334 
335 	/*
336 	 * If the state is LISTEN then ignore segment if it contains an RST.
337 	 * If the segment contains an ACK then it is bad and send a RST.
338 	 * If it does not contain a SYN then it is not interesting; drop it.
339 	 * Don't bother responding if the destination was a broadcast.
340 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
341 	 * tp->iss, and send a segment:
342 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
343 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
344 	 * Fill in remote peer address fields if not previously specified.
345 	 * Enter SYN_RECEIVED state, and process any other fields of this
346 	 * segment in this state.
347 	 */
348 	case TCPS_LISTEN: {
349 		struct mbuf *am;
350 		register struct sockaddr_in *sin;
351 
352 		if (tiflags & TH_RST)
353 			goto drop;
354 		if (tiflags & TH_ACK)
355 			goto dropwithreset;
356 		if ((tiflags & TH_SYN) == 0)
357 			goto drop;
358 		if (in_broadcast(ti->ti_dst))
359 			goto drop;
360 		am = m_get(M_DONTWAIT, MT_SONAME);
361 		if (am == NULL)
362 			goto drop;
363 		am->m_len = sizeof (struct sockaddr_in);
364 		sin = mtod(am, struct sockaddr_in *);
365 		sin->sin_family = AF_INET;
366 		sin->sin_addr = ti->ti_src;
367 		sin->sin_port = ti->ti_sport;
368 		laddr = inp->inp_laddr;
369 		if (inp->inp_laddr.s_addr == INADDR_ANY)
370 			inp->inp_laddr = ti->ti_dst;
371 		if (in_pcbconnect(inp, am)) {
372 			inp->inp_laddr = laddr;
373 			(void) m_free(am);
374 			goto drop;
375 		}
376 		(void) m_free(am);
377 		tp->t_template = tcp_template(tp);
378 		if (tp->t_template == 0) {
379 			in_pcbdisconnect(inp);
380 			dropsocket = 0;		/* socket is already gone */
381 			tp = 0;
382 			goto drop;
383 		}
384 		if (om) {
385 			tcp_dooptions(tp, om, ti);
386 			om = 0;
387 		}
388 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
389 		tp->irs = ti->ti_seq;
390 		tcp_sendseqinit(tp);
391 		tcp_rcvseqinit(tp);
392 		tp->t_state = TCPS_SYN_RECEIVED;
393 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
394 		dropsocket = 0;		/* committed to socket */
395 		goto trimthenstep6;
396 		}
397 
398 	/*
399 	 * If the state is SYN_SENT:
400 	 *	if seg contains an ACK, but not for our SYN, drop the input.
401 	 *	if seg contains a RST, then drop the connection.
402 	 *	if seg does not contain SYN, then drop it.
403 	 * Otherwise this is an acceptable SYN segment
404 	 *	initialize tp->rcv_nxt and tp->irs
405 	 *	if seg contains ack then advance tp->snd_una
406 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
407 	 *	arrange for segment to be acked (eventually)
408 	 *	continue processing rest of data/controls, beginning with URG
409 	 */
410 	case TCPS_SYN_SENT:
411 		if ((tiflags & TH_ACK) &&
412 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
413 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
414 			goto dropwithreset;
415 		if (tiflags & TH_RST) {
416 			if (tiflags & TH_ACK)
417 				tp = tcp_drop(tp, ECONNREFUSED);
418 			goto drop;
419 		}
420 		if ((tiflags & TH_SYN) == 0)
421 			goto drop;
422 		tp->snd_una = ti->ti_ack;
423 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
424 			tp->snd_nxt = tp->snd_una;
425 		tp->t_timer[TCPT_REXMT] = 0;
426 		tp->irs = ti->ti_seq;
427 		tcp_rcvseqinit(tp);
428 		tp->t_flags |= TF_ACKNOW;
429 		if (SEQ_GT(tp->snd_una, tp->iss)) {
430 			soisconnected(so);
431 			tp->t_state = TCPS_ESTABLISHED;
432 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
433 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
434 		} else
435 			tp->t_state = TCPS_SYN_RECEIVED;
436 		goto trimthenstep6;
437 
438 trimthenstep6:
439 		/*
440 		 * Advance ti->ti_seq to correspond to first data byte.
441 		 * If data, trim to stay within window,
442 		 * dropping FIN if necessary.
443 		 */
444 		ti->ti_seq++;
445 		if (ti->ti_len > tp->rcv_wnd) {
446 			todrop = ti->ti_len - tp->rcv_wnd;
447 			m_adj(m, -todrop);
448 			ti->ti_len = tp->rcv_wnd;
449 			ti->ti_flags &= ~TH_FIN;
450 		}
451 		tp->snd_wl1 = ti->ti_seq - 1;
452 		goto step6;
453 	}
454 
455 	/*
456 	 * If data is received on a connection after the
457 	 * user processes are gone, then RST the other end.
458 	 */
459 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
460 	    ti->ti_len) {
461 		tp = tcp_close(tp);
462 		goto dropwithreset;
463 	}
464 
465 	/*
466 	 * States other than LISTEN or SYN_SENT.
467 	 * First check that at least some bytes of segment are within
468 	 * receive window.
469 	 */
470 	if (tp->rcv_wnd == 0) {
471 		/*
472 		 * If window is closed can only take segments at
473 		 * window edge, and have to drop data and PUSH from
474 		 * incoming segments.
475 		 */
476 		if (tp->rcv_nxt != ti->ti_seq)
477 			goto dropafterack;
478 		if (ti->ti_len > 0) {
479 			m_adj(m, ti->ti_len);
480 			ti->ti_len = 0;
481 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
482 		}
483 	} else {
484 		/*
485 		 * If segment begins before rcv_nxt, drop leading
486 		 * data (and SYN); if nothing left, just ack.
487 		 */
488 		todrop = tp->rcv_nxt - ti->ti_seq;
489 		if (todrop > 0) {
490 			if (tiflags & TH_SYN) {
491 				tiflags &= ~TH_SYN;
492 				ti->ti_flags &= ~TH_SYN;
493 				ti->ti_seq++;
494 				if (ti->ti_urp > 1)
495 					ti->ti_urp--;
496 				else
497 					tiflags &= ~TH_URG;
498 				todrop--;
499 			}
500 			if (todrop > ti->ti_len ||
501 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
502 				goto dropafterack;
503 			m_adj(m, todrop);
504 			ti->ti_seq += todrop;
505 			ti->ti_len -= todrop;
506 			if (ti->ti_urp > todrop)
507 				ti->ti_urp -= todrop;
508 			else {
509 				tiflags &= ~TH_URG;
510 				ti->ti_flags &= ~TH_URG;
511 				ti->ti_urp = 0;
512 			}
513 		}
514 		/*
515 		 * If segment ends after window, drop trailing data
516 		 * (and PUSH and FIN); if nothing left, just ACK.
517 		 */
518 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
519 		if (todrop > 0) {
520 			if (todrop >= ti->ti_len)
521 				goto dropafterack;
522 			m_adj(m, -todrop);
523 			ti->ti_len -= todrop;
524 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
525 		}
526 	}
527 
528 	/*
529 	 * If the RST bit is set examine the state:
530 	 *    SYN_RECEIVED STATE:
531 	 *	If passive open, return to LISTEN state.
532 	 *	If active open, inform user that connection was refused.
533 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
534 	 *	Inform user that connection was reset, and close tcb.
535 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
536 	 *	Close the tcb.
537 	 */
538 	if (tiflags&TH_RST) switch (tp->t_state) {
539 
540 	case TCPS_SYN_RECEIVED:
541 		tp = tcp_drop(tp, ECONNREFUSED);
542 		goto drop;
543 
544 	case TCPS_ESTABLISHED:
545 	case TCPS_FIN_WAIT_1:
546 	case TCPS_FIN_WAIT_2:
547 	case TCPS_CLOSE_WAIT:
548 		tp = tcp_drop(tp, ECONNRESET);
549 		goto drop;
550 
551 	case TCPS_CLOSING:
552 	case TCPS_LAST_ACK:
553 	case TCPS_TIME_WAIT:
554 		tp = tcp_close(tp);
555 		goto drop;
556 	}
557 
558 	/*
559 	 * If a SYN is in the window, then this is an
560 	 * error and we send an RST and drop the connection.
561 	 */
562 	if (tiflags & TH_SYN) {
563 		tp = tcp_drop(tp, ECONNRESET);
564 		goto dropwithreset;
565 	}
566 
567 	/*
568 	 * If the ACK bit is off we drop the segment and return.
569 	 */
570 	if ((tiflags & TH_ACK) == 0)
571 		goto drop;
572 
573 	/*
574 	 * Ack processing.
575 	 */
576 	switch (tp->t_state) {
577 
578 	/*
579 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
580 	 * ESTABLISHED state and continue processing, othewise
581 	 * send an RST.
582 	 */
583 	case TCPS_SYN_RECEIVED:
584 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
585 		    SEQ_GT(ti->ti_ack, tp->snd_max))
586 			goto dropwithreset;
587 		tp->snd_una++;			/* SYN acked */
588 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
589 			tp->snd_nxt = tp->snd_una;
590 		tp->t_timer[TCPT_REXMT] = 0;
591 		soisconnected(so);
592 		tp->t_state = TCPS_ESTABLISHED;
593 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
594 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
595 		tp->snd_wl1 = ti->ti_seq - 1;
596 		/* fall into ... */
597 
598 	/*
599 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
600 	 * ACKs.  If the ack is in the range
601 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
602 	 * then advance tp->snd_una to ti->ti_ack and drop
603 	 * data from the retransmission queue.  If this ACK reflects
604 	 * more up to date window information we update our window information.
605 	 */
606 	case TCPS_ESTABLISHED:
607 	case TCPS_FIN_WAIT_1:
608 	case TCPS_FIN_WAIT_2:
609 	case TCPS_CLOSE_WAIT:
610 	case TCPS_CLOSING:
611 	case TCPS_LAST_ACK:
612 	case TCPS_TIME_WAIT:
613 #define	ourfinisacked	(acked > 0)
614 
615 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
616 			break;
617 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
618 			goto dropafterack;
619 		acked = ti->ti_ack - tp->snd_una;
620 
621 		/*
622 		 * If transmit timer is running and timed sequence
623 		 * number was acked, update smoothed round trip time.
624 		 */
625 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
626 			if (tp->t_srtt == 0)
627 				tp->t_srtt = tp->t_rtt;
628 			else
629 				tp->t_srtt =
630 				    tcp_alpha * tp->t_srtt +
631 				    (1 - tcp_alpha) * tp->t_rtt;
632 			tp->t_rtt = 0;
633 		}
634 
635 		if (ti->ti_ack == tp->snd_max)
636 			tp->t_timer[TCPT_REXMT] = 0;
637 		else {
638 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
639 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
640 			tp->t_rxtshift = 0;
641 		}
642 		/*
643 		 * When new data is acked, open the congestion window a bit.
644 		 */
645 		if (acked > 0)
646 			tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
647 		if (acked > so->so_snd.sb_cc) {
648 			tp->snd_wnd -= so->so_snd.sb_cc;
649 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
650 		} else {
651 			sbdrop(&so->so_snd, acked);
652 			tp->snd_wnd -= acked;
653 			acked = 0;
654 		}
655 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
656 			sowwakeup(so);
657 		tp->snd_una = ti->ti_ack;
658 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
659 			tp->snd_nxt = tp->snd_una;
660 
661 		switch (tp->t_state) {
662 
663 		/*
664 		 * In FIN_WAIT_1 STATE in addition to the processing
665 		 * for the ESTABLISHED state if our FIN is now acknowledged
666 		 * then enter FIN_WAIT_2.
667 		 */
668 		case TCPS_FIN_WAIT_1:
669 			if (ourfinisacked) {
670 				/*
671 				 * If we can't receive any more
672 				 * data, then closing user can proceed.
673 				 * Starting the timer is contrary to the
674 				 * specification, but if we don't get a FIN
675 				 * we'll hang forever.
676 				 */
677 				if (so->so_state & SS_CANTRCVMORE) {
678 					soisdisconnected(so);
679 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
680 				}
681 				tp->t_state = TCPS_FIN_WAIT_2;
682 			}
683 			break;
684 
685 	 	/*
686 		 * In CLOSING STATE in addition to the processing for
687 		 * the ESTABLISHED state if the ACK acknowledges our FIN
688 		 * then enter the TIME-WAIT state, otherwise ignore
689 		 * the segment.
690 		 */
691 		case TCPS_CLOSING:
692 			if (ourfinisacked) {
693 				tp->t_state = TCPS_TIME_WAIT;
694 				tcp_canceltimers(tp);
695 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
696 				soisdisconnected(so);
697 			}
698 			break;
699 
700 		/*
701 		 * The only thing that can arrive in  LAST_ACK state
702 		 * is an acknowledgment of our FIN.  If our FIN is now
703 		 * acknowledged, delete the TCB, enter the closed state
704 		 * and return.
705 		 */
706 		case TCPS_LAST_ACK:
707 			if (ourfinisacked)
708 				tp = tcp_close(tp);
709 			goto drop;
710 
711 		/*
712 		 * In TIME_WAIT state the only thing that should arrive
713 		 * is a retransmission of the remote FIN.  Acknowledge
714 		 * it and restart the finack timer.
715 		 */
716 		case TCPS_TIME_WAIT:
717 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
718 			goto dropafterack;
719 		}
720 #undef ourfinisacked
721 	}
722 
723 step6:
724 	/*
725 	 * Update window information.
726 	 */
727 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
728 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
729 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
730 		tp->snd_wnd = ti->ti_win;
731 		tp->snd_wl1 = ti->ti_seq;
732 		tp->snd_wl2 = ti->ti_ack;
733 		if (tp->snd_wnd > tp->max_sndwnd)
734 			tp->max_sndwnd = tp->snd_wnd;
735 	}
736 
737 	/*
738 	 * Process segments with URG.
739 	 */
740 	if ((tiflags & TH_URG) && ti->ti_urp &&
741 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
742 		/*
743 		 * This is a kludge, but if we receive accept
744 		 * random urgent pointers, we'll crash in
745 		 * soreceive.  It's hard to imagine someone
746 		 * actually wanting to send this much urgent data.
747 		 */
748 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
749 			ti->ti_urp = 0;			/* XXX */
750 			tiflags &= ~TH_URG;		/* XXX */
751 			ti->ti_flags &= ~TH_URG;	/* XXX */
752 			goto badurp;			/* XXX */
753 		}
754 		/*
755 		 * If this segment advances the known urgent pointer,
756 		 * then mark the data stream.  This should not happen
757 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
758 		 * a FIN has been received from the remote side.
759 		 * In these states we ignore the URG.
760 		 */
761 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
762 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
763 			so->so_oobmark = so->so_rcv.sb_cc +
764 			    (tp->rcv_up - tp->rcv_nxt) - 1;
765 			if (so->so_oobmark == 0)
766 				so->so_state |= SS_RCVATMARK;
767 			sohasoutofband(so);
768 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
769 		}
770 		/*
771 		 * Remove out of band data so doesn't get presented to user.
772 		 * This can happen independent of advancing the URG pointer,
773 		 * but if two URG's are pending at once, some out-of-band
774 		 * data may creep in... ick.
775 		 */
776 		if (ti->ti_urp <= ti->ti_len)
777 			tcp_pulloutofband(so, ti);
778 	}
779 badurp:							/* XXX */
780 
781 	/*
782 	 * Process the segment text, merging it into the TCP sequencing queue,
783 	 * and arranging for acknowledgment of receipt if necessary.
784 	 * This process logically involves adjusting tp->rcv_wnd as data
785 	 * is presented to the user (this happens in tcp_usrreq.c,
786 	 * case PRU_RCVD).  If a FIN has already been received on this
787 	 * connection then we just ignore the text.
788 	 */
789 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
790 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
791 		TCP_REASS(tp, ti, m, so, tiflags);
792 		if (tcpnodelack == 0)
793 			tp->t_flags |= TF_DELACK;
794 		else
795 			tp->t_flags |= TF_ACKNOW;
796 		/*
797 		 * Note the amount of data that peer has sent into
798 		 * our window, in order to estimate the sender's
799 		 * buffer size.
800 		 */
801 		len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv);
802 		if (len > tp->max_rcvd)
803 			tp->max_rcvd = len;
804 	} else {
805 		m_freem(m);
806 		tiflags &= ~TH_FIN;
807 	}
808 
809 	/*
810 	 * If FIN is received ACK the FIN and let the user know
811 	 * that the connection is closing.
812 	 */
813 	if (tiflags & TH_FIN) {
814 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
815 			socantrcvmore(so);
816 			tp->t_flags |= TF_ACKNOW;
817 			tp->rcv_nxt++;
818 		}
819 		switch (tp->t_state) {
820 
821 	 	/*
822 		 * In SYN_RECEIVED and ESTABLISHED STATES
823 		 * enter the CLOSE_WAIT state.
824 		 */
825 		case TCPS_SYN_RECEIVED:
826 		case TCPS_ESTABLISHED:
827 			tp->t_state = TCPS_CLOSE_WAIT;
828 			break;
829 
830 	 	/*
831 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
832 		 * enter the CLOSING state.
833 		 */
834 		case TCPS_FIN_WAIT_1:
835 			tp->t_state = TCPS_CLOSING;
836 			break;
837 
838 	 	/*
839 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
840 		 * starting the time-wait timer, turning off the other
841 		 * standard timers.
842 		 */
843 		case TCPS_FIN_WAIT_2:
844 			tp->t_state = TCPS_TIME_WAIT;
845 			tcp_canceltimers(tp);
846 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
847 			soisdisconnected(so);
848 			break;
849 
850 		/*
851 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
852 		 */
853 		case TCPS_TIME_WAIT:
854 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
855 			break;
856 		}
857 	}
858 	if (so->so_options & SO_DEBUG)
859 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
860 
861 	/*
862 	 * Return any desired output.
863 	 */
864 	(void) tcp_output(tp);
865 	return;
866 
867 dropafterack:
868 	/*
869 	 * Generate an ACK dropping incoming segment if it occupies
870 	 * sequence space, where the ACK reflects our state.
871 	 */
872 	if ((tiflags&TH_RST) ||
873 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
874 		goto drop;
875 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
876 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
877 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
878 	return;
879 
880 dropwithreset:
881 	if (om) {
882 		(void) m_free(om);
883 		om = 0;
884 	}
885 	/*
886 	 * Generate a RST, dropping incoming segment.
887 	 * Make ACK acceptable to originator of segment.
888 	 * Don't bother to respond if destination was broadcast.
889 	 */
890 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
891 		goto drop;
892 	if (tiflags & TH_ACK)
893 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
894 	else {
895 		if (tiflags & TH_SYN)
896 			ti->ti_len++;
897 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
898 		    TH_RST|TH_ACK);
899 	}
900 	/* destroy temporarily created socket */
901 	if (dropsocket)
902 		(void) soabort(so);
903 	return;
904 
905 drop:
906 	if (om)
907 		(void) m_free(om);
908 	/*
909 	 * Drop space held by incoming segment and return.
910 	 */
911 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
912 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
913 	m_freem(m);
914 	/* destroy temporarily created socket */
915 	if (dropsocket)
916 		(void) soabort(so);
917 	return;
918 }
919 
920 tcp_dooptions(tp, om, ti)
921 	struct tcpcb *tp;
922 	struct mbuf *om;
923 	struct tcpiphdr *ti;
924 {
925 	register u_char *cp;
926 	int opt, optlen, cnt;
927 
928 	cp = mtod(om, u_char *);
929 	cnt = om->m_len;
930 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
931 		opt = cp[0];
932 		if (opt == TCPOPT_EOL)
933 			break;
934 		if (opt == TCPOPT_NOP)
935 			optlen = 1;
936 		else {
937 			optlen = cp[1];
938 			if (optlen <= 0)
939 				break;
940 		}
941 		switch (opt) {
942 
943 		default:
944 			break;
945 
946 		case TCPOPT_MAXSEG:
947 			if (optlen != 4)
948 				continue;
949 			if (!(ti->ti_flags & TH_SYN))
950 				continue;
951 			tp->t_maxseg = *(u_short *)(cp + 2);
952 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
953 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
954 			break;
955 		}
956 	}
957 	(void) m_free(om);
958 }
959 
960 /*
961  * Pull out of band byte out of a segment so
962  * it doesn't appear in the user's data queue.
963  * It is still reflected in the segment length for
964  * sequencing purposes.
965  */
966 tcp_pulloutofband(so, ti)
967 	struct socket *so;
968 	struct tcpiphdr *ti;
969 {
970 	register struct mbuf *m;
971 	int cnt = ti->ti_urp - 1;
972 
973 	m = dtom(ti);
974 	while (cnt >= 0) {
975 		if (m->m_len > cnt) {
976 			char *cp = mtod(m, caddr_t) + cnt;
977 			struct tcpcb *tp = sototcpcb(so);
978 
979 			tp->t_iobc = *cp;
980 			tp->t_oobflags |= TCPOOB_HAVEDATA;
981 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
982 			m->m_len--;
983 			return;
984 		}
985 		cnt -= m->m_len;
986 		m = m->m_next;
987 		if (m == 0)
988 			break;
989 	}
990 	panic("tcp_pulloutofband");
991 }
992 
993 /*
994  *  Determine a reasonable value for maxseg size.
995  *  If the route is known, use one that can be handled
996  *  on the given interface without forcing IP to fragment.
997  *  If bigger than a page (CLBYTES), round down to nearest pagesize
998  *  to utilize pagesize mbufs.
999  *  If interface pointer is unavailable, or the destination isn't local,
1000  *  use a conservative size (512 or the default IP max size, but no more
1001  *  than the mtu of the interface through which we route),
1002  *  as we can't discover anything about intervening gateways or networks.
1003  *
1004  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
1005  */
1006 tcp_mss(tp)
1007 	register struct tcpcb *tp;
1008 {
1009 	struct route *ro;
1010 	struct ifnet *ifp;
1011 	int mss;
1012 	struct inpcb *inp;
1013 
1014 	inp = tp->t_inpcb;
1015 	ro = &inp->inp_route;
1016 	if ((ro->ro_rt == (struct rtentry *)0) ||
1017 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
1018 		/* No route yet, so try to acquire one */
1019 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1020 			ro->ro_dst.sa_family = AF_INET;
1021 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1022 				inp->inp_faddr;
1023 			rtalloc(ro);
1024 		}
1025 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
1026 			return (TCP_MSS);
1027 	}
1028 
1029 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
1030 #if	(CLBYTES & (CLBYTES - 1)) == 0
1031 	if (mss > CLBYTES)
1032 		mss &= ~(CLBYTES-1);
1033 #else
1034 	if (mss > CLBYTES)
1035 		mss = mss / CLBYTES * CLBYTES;
1036 #endif
1037 	if (in_localaddr(inp->inp_faddr))
1038 		return (mss);
1039 	return (MIN(mss, TCP_MSS));
1040 }
1041