xref: /csrg-svn/sys/netinet/tcp_input.c (revision 34854)
123190Smckusick /*
233745Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332787Sbostic  * All rights reserved.
423190Smckusick  *
532787Sbostic  * Redistribution and use in source and binary forms are permitted
6*34854Sbostic  * provided that the above copyright notice and this paragraph are
7*34854Sbostic  * duplicated in all such forms and that any documentation,
8*34854Sbostic  * advertising materials, and other materials related to such
9*34854Sbostic  * distribution and use acknowledge that the software was developed
10*34854Sbostic  * by the University of California, Berkeley.  The name of the
11*34854Sbostic  * University may not be used to endorse or promote products derived
12*34854Sbostic  * from this software without specific prior written permission.
13*34854Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34854Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34854Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632787Sbostic  *
17*34854Sbostic  *	@(#)tcp_input.c	7.19 (Berkeley) 06/29/88
1823190Smckusick  */
194601Swnj 
2017062Sbloom #include "param.h"
2117062Sbloom #include "systm.h"
2217062Sbloom #include "mbuf.h"
2317062Sbloom #include "protosw.h"
2417062Sbloom #include "socket.h"
2517062Sbloom #include "socketvar.h"
2617062Sbloom #include "errno.h"
2710894Ssam 
2810894Ssam #include "../net/if.h"
2910894Ssam #include "../net/route.h"
3010894Ssam 
3117062Sbloom #include "in.h"
3217062Sbloom #include "in_pcb.h"
3317062Sbloom #include "in_systm.h"
3417062Sbloom #include "ip.h"
3517062Sbloom #include "ip_var.h"
3617062Sbloom #include "tcp.h"
3717062Sbloom #include "tcp_fsm.h"
3817062Sbloom #include "tcp_seq.h"
3917062Sbloom #include "tcp_timer.h"
4017062Sbloom #include "tcp_var.h"
4117062Sbloom #include "tcpip.h"
4217062Sbloom #include "tcp_debug.h"
434601Swnj 
445300Sroot int	tcpprintfs = 0;
454679Swnj int	tcpcksum = 1;
4632098Skarels int	tcprexmtthresh = 3;
475267Sroot struct	tcpiphdr tcp_saveti;
484601Swnj 
495267Sroot struct	tcpcb *tcp_newtcpcb();
5024816Skarels 
515065Swnj /*
5224816Skarels  * Insert segment ti into reassembly queue of tcp with
5324816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
5424816Skarels  * a segment with FIN.  The macro form does the common case inline
5524816Skarels  * (segment is the next to be received on an established connection,
5624816Skarels  * and the queue is empty), avoiding linkage into and removal
5724816Skarels  * from the queue and repetition of various conversions.
5834278Skarels  * Set DELACK for segments received in order, but ack immediately
5934278Skarels  * when segments are out of order (so fast retransmit can work).
6024816Skarels  */
6124816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
6224816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
6324816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
6424816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
6534278Skarels 		tp->t_flags |= TF_DELACK; \
6624816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
6724816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
6830525Skarels 		tcpstat.tcps_rcvpack++;\
6930525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
7024816Skarels 		sbappend(&(so)->so_rcv, (m)); \
7124816Skarels 		sorwakeup(so); \
7234278Skarels 	} else { \
7324816Skarels 		(flags) = tcp_reass((tp), (ti)); \
7434278Skarels 		tp->t_flags |= TF_ACKNOW; \
7534278Skarels 	} \
7624816Skarels }
7724816Skarels 
7824816Skarels tcp_reass(tp, ti)
7924816Skarels 	register struct tcpcb *tp;
8024816Skarels 	register struct tcpiphdr *ti;
8124816Skarels {
8224816Skarels 	register struct tcpiphdr *q;
8324816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
8424816Skarels 	struct mbuf *m;
8524816Skarels 	int flags;
8624816Skarels 
8724816Skarels 	/*
8824816Skarels 	 * Call with ti==0 after become established to
8924816Skarels 	 * force pre-ESTABLISHED data up to user socket.
9024816Skarels 	 */
9124816Skarels 	if (ti == 0)
9224816Skarels 		goto present;
9324816Skarels 
9424816Skarels 	/*
9524816Skarels 	 * Find a segment which begins after this one does.
9624816Skarels 	 */
9724816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
9824816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
9924816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
10024816Skarels 			break;
10124816Skarels 
10224816Skarels 	/*
10324816Skarels 	 * If there is a preceding segment, it may provide some of
10424816Skarels 	 * our data already.  If so, drop the data from the incoming
10524816Skarels 	 * segment.  If it provides all of our data, drop us.
10624816Skarels 	 */
10724816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
10824816Skarels 		register int i;
10924816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
11024816Skarels 		/* conversion to int (in i) handles seq wraparound */
11124816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
11224816Skarels 		if (i > 0) {
11330525Skarels 			if (i >= ti->ti_len) {
11430525Skarels 				tcpstat.tcps_rcvduppack++;
11530525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
11624816Skarels 				goto drop;
11730525Skarels 			}
11824816Skarels 			m_adj(dtom(ti), i);
11924816Skarels 			ti->ti_len -= i;
12024816Skarels 			ti->ti_seq += i;
12124816Skarels 		}
12224816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
12324816Skarels 	}
12430525Skarels 	tcpstat.tcps_rcvoopack++;
12530525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
12624816Skarels 
12724816Skarels 	/*
12824816Skarels 	 * While we overlap succeeding segments trim them or,
12924816Skarels 	 * if they are completely covered, dequeue them.
13024816Skarels 	 */
13124816Skarels 	while (q != (struct tcpiphdr *)tp) {
13224816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
13324816Skarels 		if (i <= 0)
13424816Skarels 			break;
13524816Skarels 		if (i < q->ti_len) {
13624816Skarels 			q->ti_seq += i;
13724816Skarels 			q->ti_len -= i;
13824816Skarels 			m_adj(dtom(q), i);
13924816Skarels 			break;
14024816Skarels 		}
14124816Skarels 		q = (struct tcpiphdr *)q->ti_next;
14224816Skarels 		m = dtom(q->ti_prev);
14324816Skarels 		remque(q->ti_prev);
14424816Skarels 		m_freem(m);
14524816Skarels 	}
14624816Skarels 
14724816Skarels 	/*
14824816Skarels 	 * Stick new segment in its place.
14924816Skarels 	 */
15024816Skarels 	insque(ti, q->ti_prev);
15124816Skarels 
15224816Skarels present:
15324816Skarels 	/*
15424816Skarels 	 * Present data to user, advancing rcv_nxt through
15524816Skarels 	 * completed sequence space.
15624816Skarels 	 */
15724816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
15824816Skarels 		return (0);
15924816Skarels 	ti = tp->seg_next;
16024816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
16124816Skarels 		return (0);
16224816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
16324816Skarels 		return (0);
16424816Skarels 	do {
16524816Skarels 		tp->rcv_nxt += ti->ti_len;
16624816Skarels 		flags = ti->ti_flags & TH_FIN;
16724816Skarels 		remque(ti);
16824816Skarels 		m = dtom(ti);
16924816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
17024816Skarels 		if (so->so_state & SS_CANTRCVMORE)
17124816Skarels 			m_freem(m);
17224816Skarels 		else
17324816Skarels 			sbappend(&so->so_rcv, m);
17424816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
17524816Skarels 	sorwakeup(so);
17624816Skarels 	return (flags);
17724816Skarels drop:
17824816Skarels 	m_freem(dtom(ti));
17924816Skarels 	return (0);
18024816Skarels }
18124816Skarels 
18224816Skarels /*
1835065Swnj  * TCP input routine, follows pages 65-76 of the
1845065Swnj  * protocol specification dated September, 1981 very closely.
1855065Swnj  */
1864924Swnj tcp_input(m0)
1874924Swnj 	struct mbuf *m0;
1884601Swnj {
1894924Swnj 	register struct tcpiphdr *ti;
1904924Swnj 	struct inpcb *inp;
1914924Swnj 	register struct mbuf *m;
1925440Swnj 	struct mbuf *om = 0;
1934924Swnj 	int len, tlen, off;
1945391Swnj 	register struct tcpcb *tp = 0;
1954924Swnj 	register int tiflags;
1964803Swnj 	struct socket *so;
19731721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
1985267Sroot 	short ostate;
1996028Sroot 	struct in_addr laddr;
20010769Ssam 	int dropsocket = 0;
20130525Skarels 	int iss = 0;
2024924Swnj 
20330525Skarels 	tcpstat.tcps_rcvtotal++;
2044924Swnj 	/*
2055244Sroot 	 * Get IP and TCP header together in first mbuf.
2065244Sroot 	 * Note: IP leaves IP header in first mbuf.
2074924Swnj 	 */
2084924Swnj 	m = m0;
2095020Sroot 	ti = mtod(m, struct tcpiphdr *);
2105244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
2115208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
2125307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
2135307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
21430525Skarels 			tcpstat.tcps_rcvshort++;
2155307Sroot 			return;
2165085Swnj 		}
2175085Swnj 		ti = mtod(m, struct tcpiphdr *);
2185085Swnj 	}
2194601Swnj 
2204601Swnj 	/*
2215244Sroot 	 * Checksum extended TCP header and data.
2224601Swnj 	 */
2234924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2244924Swnj 	len = sizeof (struct ip) + tlen;
2254679Swnj 	if (tcpcksum) {
2264924Swnj 		ti->ti_next = ti->ti_prev = 0;
2274924Swnj 		ti->ti_x1 = 0;
2285223Swnj 		ti->ti_len = (u_short)tlen;
2296161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
2305231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
23111830Ssam 			if (tcpprintfs)
23211830Ssam 				printf("tcp sum: src %x\n", ti->ti_src);
23330525Skarels 			tcpstat.tcps_rcvbadsum++;
2345085Swnj 			goto drop;
2354601Swnj 		}
2364601Swnj 	}
2374601Swnj 
2384601Swnj 	/*
2395244Sroot 	 * Check that TCP offset makes sense,
2405440Swnj 	 * pull out TCP options and adjust length.
2414601Swnj 	 */
2424924Swnj 	off = ti->ti_off << 2;
2435231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
24411830Ssam 		if (tcpprintfs)
24511830Ssam 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
24630525Skarels 		tcpstat.tcps_rcvbadoff++;
2475085Swnj 		goto drop;
2484924Swnj 	}
2496211Swnj 	tlen -= off;
2506211Swnj 	ti->ti_len = tlen;
2515440Swnj 	if (off > sizeof (struct tcphdr)) {
25224816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
25324816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
25430525Skarels 				tcpstat.tcps_rcvshort++;
25524816Skarels 				return;
25624816Skarels 			}
25724816Skarels 			ti = mtod(m, struct tcpiphdr *);
2585440Swnj 		}
2599642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2605440Swnj 		if (om == 0)
2615440Swnj 			goto drop;
2625440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2635440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2646161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2655440Swnj 		  m->m_len -= om->m_len;
2666161Ssam 		  bcopy(op+om->m_len, op,
2676161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2685440Swnj 		}
2695440Swnj 	}
2705065Swnj 	tiflags = ti->ti_flags;
2714924Swnj 
2726093Sroot 	/*
27325729Smckusick 	 * Drop TCP and IP headers; TCP options were dropped above.
2746093Sroot 	 */
27525729Smckusick 	m->m_off += sizeof(struct tcpiphdr);
27625729Smckusick 	m->m_len -= sizeof(struct tcpiphdr);
2776093Sroot 
2784924Swnj 	/*
2795244Sroot 	 * Convert TCP protocol specific fields to host format.
2805085Swnj 	 */
2815085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
2825085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
2835085Swnj 	ti->ti_win = ntohs(ti->ti_win);
2845085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
2855085Swnj 
2865085Swnj 	/*
2878271Sroot 	 * Locate pcb for segment.
2884924Swnj 	 */
28930525Skarels findpcb:
2905065Swnj 	inp = in_pcblookup
2916028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
2926028Sroot 		INPLOOKUP_WILDCARD);
2935065Swnj 
2945065Swnj 	/*
2955065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2965244Sroot 	 * all data in the incoming segment is discarded.
29732098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
29832098Skarels 	 * but should either do a listen or a connect soon.
2995065Swnj 	 */
3005300Sroot 	if (inp == 0)
3015085Swnj 		goto dropwithreset;
3025065Swnj 	tp = intotcpcb(inp);
3035300Sroot 	if (tp == 0)
3045085Swnj 		goto dropwithreset;
30532098Skarels 	if (tp->t_state == TCPS_CLOSED)
30632098Skarels 		goto drop;
3075109Swnj 	so = inp->inp_socket;
3085267Sroot 	if (so->so_options & SO_DEBUG) {
3095267Sroot 		ostate = tp->t_state;
3105267Sroot 		tcp_saveti = *ti;
3115267Sroot 	}
3127510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
3137510Sroot 		so = sonewconn(so);
3147510Sroot 		if (so == 0)
3157510Sroot 			goto drop;
31610769Ssam 		/*
31710769Ssam 		 * This is ugly, but ....
31810769Ssam 		 *
31910769Ssam 		 * Mark socket as temporary until we're
32010769Ssam 		 * committed to keeping it.  The code at
32110769Ssam 		 * ``drop'' and ``dropwithreset'' check the
32210769Ssam 		 * flag dropsocket to see if the temporary
32310769Ssam 		 * socket created here should be discarded.
32410769Ssam 		 * We mark the socket as discardable until
32510769Ssam 		 * we're committed to it below in TCPS_LISTEN.
32610769Ssam 		 */
32710769Ssam 		dropsocket++;
3287510Sroot 		inp = (struct inpcb *)so->so_pcb;
3297510Sroot 		inp->inp_laddr = ti->ti_dst;
3307510Sroot 		inp->inp_lport = ti->ti_dport;
33124816Skarels 		inp->inp_options = ip_srcroute();
3327510Sroot 		tp = intotcpcb(inp);
3337510Sroot 		tp->t_state = TCPS_LISTEN;
3347510Sroot 	}
3354601Swnj 
3364601Swnj 	/*
3375162Swnj 	 * Segment received on connection.
3385162Swnj 	 * Reset idle time and keep-alive timer.
3395162Swnj 	 */
3405162Swnj 	tp->t_idle = 0;
34133745Skarels 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3425162Swnj 
3435162Swnj 	/*
34417272Skarels 	 * Process options if not in LISTEN state,
34517272Skarels 	 * else do it below (after getting remote address).
3465440Swnj 	 */
34717272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
34817272Skarels 		tcp_dooptions(tp, om, ti);
3495440Swnj 		om = 0;
3505440Swnj 	}
3515440Swnj 
3525440Swnj 	/*
3535085Swnj 	 * Calculate amount of space in receive window,
3545085Swnj 	 * and then do TCP input processing.
35524816Skarels 	 * Receive window is amount of space in rcv queue,
35624816Skarels 	 * but not less than advertised window.
3574601Swnj 	 */
35825939Skarels 	{ int win;
3594601Swnj 
36025939Skarels 	win = sbspace(&so->so_rcv);
36125939Skarels 	if (win < 0)
36225939Skarels 		win = 0;
36325939Skarels 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
36425939Skarels 	}
36525939Skarels 
3664601Swnj 	switch (tp->t_state) {
3674601Swnj 
3685065Swnj 	/*
3695065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3705065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3715065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
37225197Skarels 	 * Don't bother responding if the destination was a broadcast.
3735085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3745065Swnj 	 * tp->iss, and send a segment:
3755085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3765065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3775065Swnj 	 * Fill in remote peer address fields if not previously specified.
3785065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3795244Sroot 	 * segment in this state.
3805065Swnj 	 */
3818271Sroot 	case TCPS_LISTEN: {
38210145Ssam 		struct mbuf *am;
3838271Sroot 		register struct sockaddr_in *sin;
3848271Sroot 
3855065Swnj 		if (tiflags & TH_RST)
3865065Swnj 			goto drop;
3875300Sroot 		if (tiflags & TH_ACK)
3885085Swnj 			goto dropwithreset;
3895300Sroot 		if ((tiflags & TH_SYN) == 0)
3905065Swnj 			goto drop;
39125197Skarels 		if (in_broadcast(ti->ti_dst))
39225197Skarels 			goto drop;
39310145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
39410145Ssam 		if (am == NULL)
39510145Ssam 			goto drop;
39610145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3978599Sroot 		sin = mtod(am, struct sockaddr_in *);
3988271Sroot 		sin->sin_family = AF_INET;
3998271Sroot 		sin->sin_addr = ti->ti_src;
4008271Sroot 		sin->sin_port = ti->ti_sport;
4016028Sroot 		laddr = inp->inp_laddr;
40210145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
4036028Sroot 			inp->inp_laddr = ti->ti_dst;
4048599Sroot 		if (in_pcbconnect(inp, am)) {
4056028Sroot 			inp->inp_laddr = laddr;
4068716Sroot 			(void) m_free(am);
4075244Sroot 			goto drop;
4086028Sroot 		}
4098716Sroot 		(void) m_free(am);
4105244Sroot 		tp->t_template = tcp_template(tp);
4115244Sroot 		if (tp->t_template == 0) {
41226386Skarels 			tp = tcp_drop(tp, ENOBUFS);
41317264Skarels 			dropsocket = 0;		/* socket is already gone */
4145244Sroot 			goto drop;
4155244Sroot 		}
41617272Skarels 		if (om) {
41717272Skarels 			tcp_dooptions(tp, om, ti);
41817272Skarels 			om = 0;
41917272Skarels 		}
42030525Skarels 		if (iss)
42130525Skarels 			tp->iss = iss;
42230525Skarels 		else
42330525Skarels 			tp->iss = tcp_iss;
42430525Skarels 		tcp_iss += TCP_ISSINCR/2;
4255065Swnj 		tp->irs = ti->ti_seq;
4265085Swnj 		tcp_sendseqinit(tp);
4275085Swnj 		tcp_rcvseqinit(tp);
42825939Skarels 		tp->t_flags |= TF_ACKNOW;
4295065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
43033745Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
43110769Ssam 		dropsocket = 0;		/* committed to socket */
43230525Skarels 		tcpstat.tcps_accepts++;
4335085Swnj 		goto trimthenstep6;
4348271Sroot 		}
4354601Swnj 
4365065Swnj 	/*
4375065Swnj 	 * If the state is SYN_SENT:
4385065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
4395065Swnj 	 *	if seg contains a RST, then drop the connection.
4405065Swnj 	 *	if seg does not contain SYN, then drop it.
4415065Swnj 	 * Otherwise this is an acceptable SYN segment
4425065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4435065Swnj 	 *	if seg contains ack then advance tp->snd_una
4445065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4455065Swnj 	 *	arrange for segment to be acked (eventually)
4465065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4475065Swnj 	 */
4485065Swnj 	case TCPS_SYN_SENT:
4495065Swnj 		if ((tiflags & TH_ACK) &&
45024816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4515231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4525085Swnj 			goto dropwithreset;
4535065Swnj 		if (tiflags & TH_RST) {
45410394Ssam 			if (tiflags & TH_ACK)
45510394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4565065Swnj 			goto drop;
4574601Swnj 		}
4585065Swnj 		if ((tiflags & TH_SYN) == 0)
4595065Swnj 			goto drop;
46030974Skarels 		if (tiflags & TH_ACK) {
46130974Skarels 			tp->snd_una = ti->ti_ack;
46230974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
46330974Skarels 				tp->snd_nxt = tp->snd_una;
46430974Skarels 		}
4655244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4665065Swnj 		tp->irs = ti->ti_seq;
4675085Swnj 		tcp_rcvseqinit(tp);
4685085Swnj 		tp->t_flags |= TF_ACKNOW;
46930974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
47030525Skarels 			tcpstat.tcps_connects++;
4715244Sroot 			soisconnected(so);
4725065Swnj 			tp->t_state = TCPS_ESTABLISHED;
47317272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4745162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
47532098Skarels 			/*
47632098Skarels 			 * if we didn't have to retransmit the SYN,
47732098Skarels 			 * use its rtt as our initial srtt & rtt var.
47832098Skarels 			 */
47932098Skarels 			if (tp->t_rtt) {
48032098Skarels 				tp->t_srtt = tp->t_rtt << 3;
48132098Skarels 				tp->t_rttvar = tp->t_rtt << 1;
48232098Skarels 				TCPT_RANGESET(tp->t_rxtcur,
48332098Skarels 				    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
48432098Skarels 				    TCPTV_MIN, TCPTV_REXMTMAX);
48532098Skarels 				tp->t_rtt = 0;
48632098Skarels 			}
4875162Swnj 		} else
4885085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4895085Swnj 
4905085Swnj trimthenstep6:
4915085Swnj 		/*
4925231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4935085Swnj 		 * If data, trim to stay within window,
4945085Swnj 		 * dropping FIN if necessary.
4955085Swnj 		 */
4965231Swnj 		ti->ti_seq++;
4975085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4985085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4995085Swnj 			m_adj(m, -todrop);
5005085Swnj 			ti->ti_len = tp->rcv_wnd;
50125939Skarels 			tiflags &= ~TH_FIN;
50230525Skarels 			tcpstat.tcps_rcvpackafterwin++;
50330525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
5045065Swnj 		}
5055263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
50625939Skarels 		tp->rcv_up = ti->ti_seq;
5075085Swnj 		goto step6;
5085065Swnj 	}
5094601Swnj 
5105065Swnj 	/*
51130525Skarels 	 * States other than LISTEN or SYN_SENT.
51230525Skarels 	 * First check that at least some bytes of segment are within
51330525Skarels 	 * receive window.  If segment begins before rcv_nxt,
51430525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
51516222Skarels 	 */
51630525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
51730525Skarels 	if (todrop > 0) {
51830525Skarels 		if (tiflags & TH_SYN) {
51930525Skarels 			tiflags &= ~TH_SYN;
52030525Skarels 			ti->ti_seq++;
52130525Skarels 			if (ti->ti_urp > 1)
52230525Skarels 				ti->ti_urp--;
52330525Skarels 			else
52430525Skarels 				tiflags &= ~TH_URG;
52530525Skarels 			todrop--;
52630525Skarels 		}
52730525Skarels 		if (todrop > ti->ti_len ||
52830525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
52933745Skarels 			tcpstat.tcps_rcvduppack++;
53033745Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
53131730Skarels 			/*
53233745Skarels 			 * If segment is just one to the left of the window,
53333745Skarels 			 * check two special cases:
53433745Skarels 			 * 1. Don't toss RST in response to 4.2-style keepalive.
53533745Skarels 			 * 2. If the only thing to drop is a FIN, we can drop
53633745Skarels 			 *    it, but check the ACK or we will get into FIN
53733745Skarels 			 *    wars if our FINs crossed (both CLOSING).
53833745Skarels 			 * In either case, send ACK to resynchronize,
53933745Skarels 			 * but keep on processing for RST or ACK.
54031730Skarels 			 */
54133745Skarels 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
54233745Skarels #ifdef TCP_COMPAT_42
54333745Skarels 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
54431730Skarels #endif
54533745Skarels 			   ) {
54633745Skarels 				todrop = ti->ti_len;
54733745Skarels 				tiflags &= ~TH_FIN;
54833745Skarels 				tp->t_flags |= TF_ACKNOW;
54933745Skarels 			} else
55033745Skarels 				goto dropafterack;
55132034Skarels 		} else {
55232034Skarels 			tcpstat.tcps_rcvpartduppack++;
55332034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
55430525Skarels 		}
55530525Skarels 		m_adj(m, todrop);
55630525Skarels 		ti->ti_seq += todrop;
55730525Skarels 		ti->ti_len -= todrop;
55830525Skarels 		if (ti->ti_urp > todrop)
55930525Skarels 			ti->ti_urp -= todrop;
56030525Skarels 		else {
56130525Skarels 			tiflags &= ~TH_URG;
56230525Skarels 			ti->ti_urp = 0;
56330525Skarels 		}
56416222Skarels 	}
56516222Skarels 
56632612Skarels 	/*
56733745Skarels 	 * If new data are received on a connection after the
56832612Skarels 	 * user processes are gone, then RST the other end.
56932612Skarels 	 */
57032612Skarels 	if ((so->so_state & SS_NOFDREF) &&
57132612Skarels 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
57232612Skarels 		tp = tcp_close(tp);
57332612Skarels 		tcpstat.tcps_rcvafterclose++;
57432612Skarels 		goto dropwithreset;
57532612Skarels 	}
57632612Skarels 
57733445Skarels 	/*
57833445Skarels 	 * If segment ends after window, drop trailing data
57933445Skarels 	 * (and PUSH and FIN); if nothing left, just ACK.
58033445Skarels 	 */
58133445Skarels 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
58233445Skarels 	if (todrop > 0) {
58333445Skarels 		tcpstat.tcps_rcvpackafterwin++;
58433445Skarels 		if (todrop >= ti->ti_len) {
58530525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
58633445Skarels 			/*
58733445Skarels 			 * If a new connection request is received
58833445Skarels 			 * while in TIME_WAIT, drop the old connection
58933445Skarels 			 * and start over if the sequence numbers
59033445Skarels 			 * are above the previous ones.
59133445Skarels 			 */
59233445Skarels 			if (tiflags & TH_SYN &&
59333445Skarels 			    tp->t_state == TCPS_TIME_WAIT &&
59433445Skarels 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
59533445Skarels 				iss = tp->rcv_nxt + TCP_ISSINCR;
59633445Skarels 				(void) tcp_close(tp);
59733445Skarels 				goto findpcb;
59833445Skarels 			}
59933445Skarels 			/*
60033445Skarels 			 * If window is closed can only take segments at
60133445Skarels 			 * window edge, and have to drop data and PUSH from
60233445Skarels 			 * incoming segments.  Continue processing, but
60333445Skarels 			 * remember to ack.  Otherwise, drop segment
60433445Skarels 			 * and ack.
60533445Skarels 			 */
60633445Skarels 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
60733445Skarels 				tp->t_flags |= TF_ACKNOW;
60830525Skarels 				tcpstat.tcps_rcvwinprobe++;
60933445Skarels 			} else
6105065Swnj 				goto dropafterack;
61133445Skarels 		} else
61230525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
61333445Skarels 		m_adj(m, -todrop);
61433445Skarels 		ti->ti_len -= todrop;
61533445Skarels 		tiflags &= ~(TH_PUSH|TH_FIN);
6165065Swnj 	}
6174601Swnj 
6185065Swnj 	/*
6195065Swnj 	 * If the RST bit is set examine the state:
6205065Swnj 	 *    SYN_RECEIVED STATE:
6215065Swnj 	 *	If passive open, return to LISTEN state.
6225065Swnj 	 *	If active open, inform user that connection was refused.
6235065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
6245065Swnj 	 *	Inform user that connection was reset, and close tcb.
6255065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
6265065Swnj 	 *	Close the tcb.
6275065Swnj 	 */
6285065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
6295267Sroot 
6305065Swnj 	case TCPS_SYN_RECEIVED:
63133745Skarels 		so->so_error = ECONNREFUSED;
63233745Skarels 		goto close;
6334601Swnj 
6345065Swnj 	case TCPS_ESTABLISHED:
6355065Swnj 	case TCPS_FIN_WAIT_1:
6365065Swnj 	case TCPS_FIN_WAIT_2:
6375065Swnj 	case TCPS_CLOSE_WAIT:
63833745Skarels 		so->so_error = ECONNRESET;
63933745Skarels 	close:
64033745Skarels 		tp->t_state = TCPS_CLOSED;
64133745Skarels 		tcpstat.tcps_drops++;
64233745Skarels 		tp = tcp_close(tp);
6435065Swnj 		goto drop;
6445065Swnj 
6455065Swnj 	case TCPS_CLOSING:
6465065Swnj 	case TCPS_LAST_ACK:
6475065Swnj 	case TCPS_TIME_WAIT:
64810394Ssam 		tp = tcp_close(tp);
6495065Swnj 		goto drop;
6504601Swnj 	}
6514601Swnj 
6524601Swnj 	/*
6535065Swnj 	 * If a SYN is in the window, then this is an
6545065Swnj 	 * error and we send an RST and drop the connection.
6554601Swnj 	 */
6565065Swnj 	if (tiflags & TH_SYN) {
65710394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6585085Swnj 		goto dropwithreset;
6594601Swnj 	}
6604601Swnj 
6614601Swnj 	/*
6625065Swnj 	 * If the ACK bit is off we drop the segment and return.
6634601Swnj 	 */
6645085Swnj 	if ((tiflags & TH_ACK) == 0)
6655065Swnj 		goto drop;
6665065Swnj 
6675065Swnj 	/*
6685065Swnj 	 * Ack processing.
6695065Swnj 	 */
6704601Swnj 	switch (tp->t_state) {
6714601Swnj 
6725065Swnj 	/*
6735065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
67431721Skarels 	 * ESTABLISHED state and continue processing, otherwise
6755065Swnj 	 * send an RST.
6765065Swnj 	 */
6775065Swnj 	case TCPS_SYN_RECEIVED:
6785085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
6795231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
6805085Swnj 			goto dropwithreset;
68130525Skarels 		tcpstat.tcps_connects++;
6825085Swnj 		soisconnected(so);
6835085Swnj 		tp->t_state = TCPS_ESTABLISHED;
68417272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
6855162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
6865244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
6875085Swnj 		/* fall into ... */
6884601Swnj 
6895065Swnj 	/*
6905065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
6915065Swnj 	 * ACKs.  If the ack is in the range
6925231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6935065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6945065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6955065Swnj 	 * more up to date window information we update our window information.
6965065Swnj 	 */
6975065Swnj 	case TCPS_ESTABLISHED:
6985065Swnj 	case TCPS_FIN_WAIT_1:
6995065Swnj 	case TCPS_FIN_WAIT_2:
7005065Swnj 	case TCPS_CLOSE_WAIT:
7015065Swnj 	case TCPS_CLOSING:
7025244Sroot 	case TCPS_LAST_ACK:
7035244Sroot 	case TCPS_TIME_WAIT:
7045085Swnj 
70530525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
70632098Skarels 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
70730525Skarels 				tcpstat.tcps_rcvdupack++;
70832098Skarels 				/*
70932098Skarels 				 * If we have outstanding data (not a
71032098Skarels 				 * window probe), this is a completely
71132098Skarels 				 * duplicate ack (ie, window info didn't
71232098Skarels 				 * change), the ack is the biggest we've
71332098Skarels 				 * seen and we've seen exactly our rexmt
71432098Skarels 				 * threshhold of them, assume a packet
71532098Skarels 				 * has been dropped and retransmit it.
71632098Skarels 				 * Kludge snd_nxt & the congestion
71732098Skarels 				 * window so we send only this one
71832376Skarels 				 * packet.  If this packet fills the
71932376Skarels 				 * only hole in the receiver's seq.
72032376Skarels 				 * space, the next real ack will fully
72132376Skarels 				 * open our window.  This means we
72232376Skarels 				 * have to do the usual slow-start to
72332376Skarels 				 * not overwhelm an intermediate gateway
72432376Skarels 				 * with a burst of packets.  Leave
72532376Skarels 				 * here with the congestion window set
72632376Skarels 				 * to allow 2 packets on the next real
72732376Skarels 				 * ack and the exp-to-linear thresh
72832376Skarels 				 * set for half the current window
72932376Skarels 				 * size (since we know we're losing at
73032376Skarels 				 * the current window size).
73132098Skarels 				 */
73232098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
73332098Skarels 				    ti->ti_ack != tp->snd_una)
73432098Skarels 					tp->t_dupacks = 0;
73532098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
73632098Skarels 					tcp_seq onxt = tp->snd_nxt;
73732376Skarels 					u_int win =
73832376Skarels 					    MIN(tp->snd_wnd, tp->snd_cwnd) / 2 /
73932376Skarels 						tp->t_maxseg;
74032098Skarels 
74132376Skarels 					if (win < 2)
74232376Skarels 						win = 2;
74332376Skarels 					tp->snd_ssthresh = win * tp->t_maxseg;
74432376Skarels 
74532098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
74632098Skarels 					tp->t_rtt = 0;
74732098Skarels 					tp->snd_nxt = ti->ti_ack;
74832098Skarels 					tp->snd_cwnd = tp->t_maxseg;
74932098Skarels 					(void) tcp_output(tp);
75032098Skarels 
75132098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
75232098Skarels 						tp->snd_nxt = onxt;
75332098Skarels 					goto drop;
75432098Skarels 				}
75532098Skarels 			} else
75632098Skarels 				tp->t_dupacks = 0;
7575065Swnj 			break;
75830525Skarels 		}
75932098Skarels 		tp->t_dupacks = 0;
76030525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
76130525Skarels 			tcpstat.tcps_rcvacktoomuch++;
7625065Swnj 			goto dropafterack;
76330525Skarels 		}
7645085Swnj 		acked = ti->ti_ack - tp->snd_una;
76530525Skarels 		tcpstat.tcps_rcvackpack++;
76630525Skarels 		tcpstat.tcps_rcvackbyte += acked;
7675951Swnj 
7685951Swnj 		/*
7695951Swnj 		 * If transmit timer is running and timed sequence
7705951Swnj 		 * number was acked, update smoothed round trip time.
77132034Skarels 		 * Since we now have an rtt measurement, cancel the
77232034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
77332034Skarels 		 * Recompute the initial retransmit timer.
7745951Swnj 		 */
7755951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
77630525Skarels 			tcpstat.tcps_rttupdated++;
77731726Skarels 			if (tp->t_srtt != 0) {
77831726Skarels 				register short delta;
77931726Skarels 
78031726Skarels 				/*
78131726Skarels 				 * srtt is stored as fixed point with 3 bits
78231726Skarels 				 * after the binary point (i.e., scaled by 8).
78331726Skarels 				 * The following magic is equivalent
78431726Skarels 				 * to the smoothing algorithm in rfc793
78531726Skarels 				 * with an alpha of .875
78631726Skarels 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
78732098Skarels 				 * Adjust t_rtt to origin 0.
78831726Skarels 				 */
78933745Skarels 				delta = tp->t_rtt - 1 - (tp->t_srtt >> 3);
79031726Skarels 				if ((tp->t_srtt += delta) <= 0)
79131726Skarels 					tp->t_srtt = 1;
79231726Skarels 				/*
79332034Skarels 				 * We accumulate a smoothed rtt variance
79432034Skarels 				 * (actually, a smoothed mean difference),
79531726Skarels 				 * then set the retransmit timer to smoothed
79631726Skarels 				 * rtt + 2 times the smoothed variance.
79732098Skarels 				 * rttvar is stored as fixed point
79831726Skarels 				 * with 2 bits after the binary point
79931726Skarels 				 * (scaled by 4).  The following is equivalent
80031726Skarels 				 * to rfc793 smoothing with an alpha of .75
80131726Skarels 				 * (rttvar = rttvar*3/4 + |delta| / 4).
80231726Skarels 				 * This replaces rfc793's wired-in beta.
80331726Skarels 				 */
80431726Skarels 				if (delta < 0)
80531726Skarels 					delta = -delta;
80631726Skarels 				delta -= (tp->t_rttvar >> 2);
80731726Skarels 				if ((tp->t_rttvar += delta) <= 0)
80831726Skarels 					tp->t_rttvar = 1;
80931726Skarels 			} else {
81031726Skarels 				/*
81131726Skarels 				 * No rtt measurement yet - use the
81231726Skarels 				 * unsmoothed rtt.  Set the variance
81331726Skarels 				 * to half the rtt (so our first
81431726Skarels 				 * retransmit happens at 2*rtt)
81531726Skarels 				 */
81631726Skarels 				tp->t_srtt = tp->t_rtt << 3;
81731726Skarels 				tp->t_rttvar = tp->t_rtt << 1;
81831726Skarels 			}
8195951Swnj 			tp->t_rtt = 0;
82032034Skarels 			tp->t_rxtshift = 0;
82132034Skarels 			TCPT_RANGESET(tp->t_rxtcur,
82232034Skarels 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
82332034Skarels 			    TCPTV_MIN, TCPTV_REXMTMAX);
8245951Swnj 		}
8255951Swnj 
82626824Skarels 		/*
82726824Skarels 		 * If all outstanding data is acked, stop retransmit
82826824Skarels 		 * timer and remember to restart (more output or persist).
82926824Skarels 		 * If there is more data to be acked, restart retransmit
83032034Skarels 		 * timer, using current (possibly backed-off) value.
83126824Skarels 		 */
83226824Skarels 		if (ti->ti_ack == tp->snd_max) {
8335244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
83426824Skarels 			needoutput = 1;
83532034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
83632034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
83717360Skarels 		/*
83832098Skarels 		 * When new data is acked, open the congestion window.
83932098Skarels 		 * If the window gives us less than ssthresh packets
84032098Skarels 		 * in flight, open exponentially (maxseg per packet).
84132098Skarels 		 * Otherwise open linearly (maxseg per window,
84232098Skarels 		 * or maxseg^2 / cwnd per packet).
84317360Skarels 		 */
84432098Skarels 		{
84532098Skarels 		u_int incr = tp->t_maxseg;
84632098Skarels 
84732098Skarels 		if (tp->snd_cwnd > tp->snd_ssthresh)
84832098Skarels 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
84932098Skarels 
85033603Skarels 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, IP_MAXPACKET); /* XXX */
85132098Skarels 		}
8525307Sroot 		if (acked > so->so_snd.sb_cc) {
85315386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
85426386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
85531721Skarels 			ourfinisacked = 1;
8565307Sroot 		} else {
8576161Ssam 			sbdrop(&so->so_snd, acked);
8585307Sroot 			tp->snd_wnd -= acked;
85931721Skarels 			ourfinisacked = 0;
8605307Sroot 		}
8616434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
8625300Sroot 			sowwakeup(so);
8635231Swnj 		tp->snd_una = ti->ti_ack;
8645357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
8655357Sroot 			tp->snd_nxt = tp->snd_una;
8665162Swnj 
8674601Swnj 		switch (tp->t_state) {
8684601Swnj 
8695065Swnj 		/*
8705065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
8715065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
8725085Swnj 		 * then enter FIN_WAIT_2.
8735065Swnj 		 */
8745065Swnj 		case TCPS_FIN_WAIT_1:
8755896Swnj 			if (ourfinisacked) {
8765896Swnj 				/*
8775896Swnj 				 * If we can't receive any more
8785896Swnj 				 * data, then closing user can proceed.
87924816Skarels 				 * Starting the timer is contrary to the
88024816Skarels 				 * specification, but if we don't get a FIN
88124816Skarels 				 * we'll hang forever.
8825896Swnj 				 */
88324816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
8845896Swnj 					soisdisconnected(so);
88533745Skarels 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
88624816Skarels 				}
8875085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
8885896Swnj 			}
8894601Swnj 			break;
8904601Swnj 
8915065Swnj 	 	/*
8925065Swnj 		 * In CLOSING STATE in addition to the processing for
8935065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
8945065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
8955065Swnj 		 * the segment.
8965065Swnj 		 */
8975065Swnj 		case TCPS_CLOSING:
8985244Sroot 			if (ourfinisacked) {
8995065Swnj 				tp->t_state = TCPS_TIME_WAIT;
9005244Sroot 				tcp_canceltimers(tp);
9015244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9025244Sroot 				soisdisconnected(so);
9035244Sroot 			}
9045244Sroot 			break;
9054601Swnj 
9065065Swnj 		/*
90731743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
90831743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
90931743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
91031743Skarels 		 * enter the closed state and return.
9115065Swnj 		 */
9125065Swnj 		case TCPS_LAST_ACK:
91331743Skarels 			if (ourfinisacked) {
91410394Ssam 				tp = tcp_close(tp);
91531743Skarels 				goto drop;
91631743Skarels 			}
91731743Skarels 			break;
9184601Swnj 
9195065Swnj 		/*
9205065Swnj 		 * In TIME_WAIT state the only thing that should arrive
9215065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
9225065Swnj 		 * it and restart the finack timer.
9235065Swnj 		 */
9245065Swnj 		case TCPS_TIME_WAIT:
9255162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9265065Swnj 			goto dropafterack;
9274601Swnj 		}
9285085Swnj 	}
9294601Swnj 
9305065Swnj step6:
9315065Swnj 	/*
9325244Sroot 	 * Update window information.
93325939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
9345244Sroot 	 */
93525939Skarels 	if ((tiflags & TH_ACK) &&
93625939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
9375391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
93825939Skarels 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
93930525Skarels 		/* keep track of pure window updates */
94030525Skarels 		if (ti->ti_len == 0 &&
94132098Skarels 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
94230525Skarels 			tcpstat.tcps_rcvwinupd++;
9435244Sroot 		tp->snd_wnd = ti->ti_win;
9445244Sroot 		tp->snd_wl1 = ti->ti_seq;
9455244Sroot 		tp->snd_wl2 = ti->ti_ack;
94625260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
94725260Skarels 			tp->max_sndwnd = tp->snd_wnd;
94826824Skarels 		needoutput = 1;
94926824Skarels 	}
9505244Sroot 
9515244Sroot 	/*
9525547Swnj 	 * Process segments with URG.
9535065Swnj 	 */
9547267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
9557267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
9565547Swnj 		/*
95725939Skarels 		 * This is a kludge, but if we receive and accept
95813121Ssam 		 * random urgent pointers, we'll crash in
95913121Ssam 		 * soreceive.  It's hard to imagine someone
96013121Ssam 		 * actually wanting to send this much urgent data.
96112441Ssam 		 */
96225652Skarels 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
96312441Ssam 			ti->ti_urp = 0;			/* XXX */
96412441Ssam 			tiflags &= ~TH_URG;		/* XXX */
96525939Skarels 			goto dodata;			/* XXX */
96612441Ssam 		}
96712441Ssam 		/*
9685547Swnj 		 * If this segment advances the known urgent pointer,
9695547Swnj 		 * then mark the data stream.  This should not happen
9705547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
9715547Swnj 		 * a FIN has been received from the remote side.
9725547Swnj 		 * In these states we ignore the URG.
97327190Skarels 		 *
97427190Skarels 		 * According to RFC961 (Assigned Protocols),
97527190Skarels 		 * the urgent pointer points to the last octet
97627190Skarels 		 * of urgent data.  We continue, however,
97727190Skarels 		 * to consider it to indicate the first octet
97827190Skarels 		 * of data past the urgent section
97927190Skarels 		 * as the original spec states.
9805547Swnj 		 */
9815547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
9825547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
9835547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
9845547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
9855547Swnj 			if (so->so_oobmark == 0)
9865547Swnj 				so->so_state |= SS_RCVATMARK;
9878313Sroot 			sohasoutofband(so);
98824816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
9895440Swnj 		}
9905547Swnj 		/*
9915547Swnj 		 * Remove out of band data so doesn't get presented to user.
9925547Swnj 		 * This can happen independent of advancing the URG pointer,
9935547Swnj 		 * but if two URG's are pending at once, some out-of-band
9945547Swnj 		 * data may creep in... ick.
9955547Swnj 		 */
99634278Skarels 		if (ti->ti_urp <= ti->ti_len &&
99734278Skarels 		    (so->so_options & SO_OOBINLINE) == 0)
9985547Swnj 			tcp_pulloutofband(so, ti);
99925939Skarels 	} else
100025939Skarels 		/*
100125939Skarels 		 * If no out of band data is expected,
100225939Skarels 		 * pull receive urgent pointer along
100325939Skarels 		 * with the receive window.
100425939Skarels 		 */
100525939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
100625939Skarels 			tp->rcv_up = tp->rcv_nxt;
100725939Skarels dodata:							/* XXX */
10084601Swnj 
10094601Swnj 	/*
10105065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
10115065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
10125065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
10135065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
10145065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
10155065Swnj 	 * connection then we just ignore the text.
10164601Swnj 	 */
101717946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
101817946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
101924816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
102025260Skarels 		/*
102125260Skarels 		 * Note the amount of data that peer has sent into
102225260Skarels 		 * our window, in order to estimate the sender's
102325260Skarels 		 * buffer size.
102425260Skarels 		 */
102532098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
102625260Skarels 		if (len > tp->max_rcvd)
102725260Skarels 			tp->max_rcvd = len;
10285244Sroot 	} else {
10294924Swnj 		m_freem(m);
10305263Swnj 		tiflags &= ~TH_FIN;
10315244Sroot 	}
10324601Swnj 
10334601Swnj 	/*
10345263Swnj 	 * If FIN is received ACK the FIN and let the user know
10355263Swnj 	 * that the connection is closing.
10364601Swnj 	 */
10375263Swnj 	if (tiflags & TH_FIN) {
10385244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10395244Sroot 			socantrcvmore(so);
10405244Sroot 			tp->t_flags |= TF_ACKNOW;
10415244Sroot 			tp->rcv_nxt++;
10425244Sroot 		}
10435065Swnj 		switch (tp->t_state) {
10444601Swnj 
10455065Swnj 	 	/*
10465065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
10475065Swnj 		 * enter the CLOSE_WAIT state.
10484884Swnj 		 */
10495065Swnj 		case TCPS_SYN_RECEIVED:
10505065Swnj 		case TCPS_ESTABLISHED:
10515065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
10525065Swnj 			break;
10534884Swnj 
10545065Swnj 	 	/*
10555085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
10565085Swnj 		 * enter the CLOSING state.
10574884Swnj 		 */
10585065Swnj 		case TCPS_FIN_WAIT_1:
10595085Swnj 			tp->t_state = TCPS_CLOSING;
10605065Swnj 			break;
10614601Swnj 
10625065Swnj 	 	/*
10635065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10645065Swnj 		 * starting the time-wait timer, turning off the other
10655065Swnj 		 * standard timers.
10665065Swnj 		 */
10675065Swnj 		case TCPS_FIN_WAIT_2:
10685244Sroot 			tp->t_state = TCPS_TIME_WAIT;
10695074Swnj 			tcp_canceltimers(tp);
10705162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10715244Sroot 			soisdisconnected(so);
10725065Swnj 			break;
10735065Swnj 
10744884Swnj 		/*
10755065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
10764884Swnj 		 */
10775065Swnj 		case TCPS_TIME_WAIT:
10785162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10795065Swnj 			break;
10805085Swnj 		}
10814601Swnj 	}
10825267Sroot 	if (so->so_options & SO_DEBUG)
10835267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
10845085Swnj 
10855085Swnj 	/*
10865085Swnj 	 * Return any desired output.
10875085Swnj 	 */
108826824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
108925939Skarels 		(void) tcp_output(tp);
10905065Swnj 	return;
10915085Swnj 
10925065Swnj dropafterack:
10935085Swnj 	/*
10946211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
10956211Swnj 	 * sequence space, where the ACK reflects our state.
10965085Swnj 	 */
109726057Skarels 	if (tiflags & TH_RST)
10985085Swnj 		goto drop;
109931749Skarels 	m_freem(m);
110031721Skarels 	tp->t_flags |= TF_ACKNOW;
110131721Skarels 	(void) tcp_output(tp);
11025231Swnj 	return;
11035085Swnj 
11045085Swnj dropwithreset:
110511731Ssam 	if (om) {
11066161Ssam 		(void) m_free(om);
110711731Ssam 		om = 0;
110811731Ssam 	}
11095085Swnj 	/*
11105244Sroot 	 * Generate a RST, dropping incoming segment.
11115085Swnj 	 * Make ACK acceptable to originator of segment.
111225197Skarels 	 * Don't bother to respond if destination was broadcast.
11135085Swnj 	 */
111425197Skarels 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
11155085Swnj 		goto drop;
11165085Swnj 	if (tiflags & TH_ACK)
11175391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
11185085Swnj 	else {
11195085Swnj 		if (tiflags & TH_SYN)
11205085Swnj 			ti->ti_len++;
11216211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
11226211Swnj 		    TH_RST|TH_ACK);
11235085Swnj 	}
112410769Ssam 	/* destroy temporarily created socket */
112510769Ssam 	if (dropsocket)
112610769Ssam 		(void) soabort(so);
11275231Swnj 	return;
11285085Swnj 
11295065Swnj drop:
113011730Ssam 	if (om)
113111730Ssam 		(void) m_free(om);
11325085Swnj 	/*
11335085Swnj 	 * Drop space held by incoming segment and return.
11345085Swnj 	 */
11356303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
11366303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
11375065Swnj 	m_freem(m);
113810769Ssam 	/* destroy temporarily created socket */
113910769Ssam 	if (dropsocket)
114010769Ssam 		(void) soabort(so);
11415267Sroot 	return;
11425065Swnj }
11435065Swnj 
114417272Skarels tcp_dooptions(tp, om, ti)
11455440Swnj 	struct tcpcb *tp;
11465440Swnj 	struct mbuf *om;
114717272Skarels 	struct tcpiphdr *ti;
11485419Swnj {
11495440Swnj 	register u_char *cp;
11505440Swnj 	int opt, optlen, cnt;
11515419Swnj 
11525440Swnj 	cp = mtod(om, u_char *);
11535440Swnj 	cnt = om->m_len;
11545440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
11555440Swnj 		opt = cp[0];
11565440Swnj 		if (opt == TCPOPT_EOL)
11575440Swnj 			break;
11585440Swnj 		if (opt == TCPOPT_NOP)
11595440Swnj 			optlen = 1;
116012169Ssam 		else {
11615440Swnj 			optlen = cp[1];
116212169Ssam 			if (optlen <= 0)
116312169Ssam 				break;
116412169Ssam 		}
11655440Swnj 		switch (opt) {
11665440Swnj 
11675440Swnj 		default:
11685440Swnj 			break;
11695440Swnj 
11705440Swnj 		case TCPOPT_MAXSEG:
11715440Swnj 			if (optlen != 4)
11725440Swnj 				continue;
117317272Skarels 			if (!(ti->ti_flags & TH_SYN))
117417272Skarels 				continue;
11755440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
11766161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
117717272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
11785440Swnj 			break;
11795419Swnj 		}
11805419Swnj 	}
11816161Ssam 	(void) m_free(om);
11825419Swnj }
11835419Swnj 
11845419Swnj /*
11855547Swnj  * Pull out of band byte out of a segment so
11865547Swnj  * it doesn't appear in the user's data queue.
11875547Swnj  * It is still reflected in the segment length for
11885547Swnj  * sequencing purposes.
11895547Swnj  */
11905547Swnj tcp_pulloutofband(so, ti)
11915547Swnj 	struct socket *so;
11925547Swnj 	struct tcpiphdr *ti;
11935547Swnj {
11945547Swnj 	register struct mbuf *m;
11956116Swnj 	int cnt = ti->ti_urp - 1;
11965547Swnj 
11975547Swnj 	m = dtom(ti);
11985547Swnj 	while (cnt >= 0) {
11995547Swnj 		if (m->m_len > cnt) {
12005547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
12015547Swnj 			struct tcpcb *tp = sototcpcb(so);
12025547Swnj 
12035547Swnj 			tp->t_iobc = *cp;
12045547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
12056161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
12065547Swnj 			m->m_len--;
12075547Swnj 			return;
12085547Swnj 		}
12095547Swnj 		cnt -= m->m_len;
12105547Swnj 		m = m->m_next;
12115547Swnj 		if (m == 0)
12125547Swnj 			break;
12135547Swnj 	}
12145547Swnj 	panic("tcp_pulloutofband");
12155547Swnj }
12165547Swnj 
12175547Swnj /*
121817272Skarels  *  Determine a reasonable value for maxseg size.
121917272Skarels  *  If the route is known, use one that can be handled
122017272Skarels  *  on the given interface without forcing IP to fragment.
122131726Skarels  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
122231726Skarels  *  to utilize large mbufs.
122317272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
122423975Skarels  *  use a conservative size (512 or the default IP max size, but no more
122523975Skarels  *  than the mtu of the interface through which we route),
122617272Skarels  *  as we can't discover anything about intervening gateways or networks.
122732034Skarels  *  We also initialize the congestion/slow start window to be a single
122832034Skarels  *  segment if the destination isn't local; this information should
122932034Skarels  *  probably all be saved with the routing entry at the transport level.
123017272Skarels  *
123117272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
123217272Skarels  */
123317272Skarels tcp_mss(tp)
123423975Skarels 	register struct tcpcb *tp;
123517272Skarels {
123617272Skarels 	struct route *ro;
123717272Skarels 	struct ifnet *ifp;
123817272Skarels 	int mss;
123917272Skarels 	struct inpcb *inp;
124017272Skarels 
124117272Skarels 	inp = tp->t_inpcb;
124217272Skarels 	ro = &inp->inp_route;
124317272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
124417272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
124517272Skarels 		/* No route yet, so try to acquire one */
124617272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
124717272Skarels 			ro->ro_dst.sa_family = AF_INET;
124817272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
124917272Skarels 				inp->inp_faddr;
125017272Skarels 			rtalloc(ro);
125117272Skarels 		}
125217272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
125317316Skarels 			return (TCP_MSS);
125417272Skarels 	}
125517272Skarels 
125617272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
125731726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
125831726Skarels 	if (mss > MCLBYTES)
125931726Skarels 		mss &= ~(MCLBYTES-1);
126017272Skarels #else
126131726Skarels 	if (mss > MCLBYTES)
126231726Skarels 		mss = mss / MCLBYTES * MCLBYTES;
126317272Skarels #endif
126423975Skarels 	if (in_localaddr(inp->inp_faddr))
126523975Skarels 		return (mss);
126632098Skarels 
126732034Skarels 	mss = MIN(mss, TCP_MSS);
126832034Skarels 	tp->snd_cwnd = mss;
126932034Skarels 	return (mss);
127017272Skarels }
1271