xref: /csrg-svn/sys/netinet/tcp_input.c (revision 30974)
123190Smckusick /*
229149Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323190Smckusick  * All rights reserved.  The Berkeley software License Agreement
423190Smckusick  * specifies the terms and conditions for redistribution.
523190Smckusick  *
6*30974Skarels  *	@(#)tcp_input.c	7.3 (Berkeley) 04/29/87
723190Smckusick  */
84601Swnj 
917062Sbloom #include "param.h"
1017062Sbloom #include "systm.h"
1117062Sbloom #include "mbuf.h"
1217062Sbloom #include "protosw.h"
1317062Sbloom #include "socket.h"
1417062Sbloom #include "socketvar.h"
1517062Sbloom #include "errno.h"
1610894Ssam 
1710894Ssam #include "../net/if.h"
1810894Ssam #include "../net/route.h"
1910894Ssam 
2017062Sbloom #include "in.h"
2117062Sbloom #include "in_pcb.h"
2217062Sbloom #include "in_systm.h"
2317062Sbloom #include "ip.h"
2417062Sbloom #include "ip_var.h"
2517062Sbloom #include "tcp.h"
2617062Sbloom #include "tcp_fsm.h"
2717062Sbloom #include "tcp_seq.h"
2817062Sbloom #include "tcp_timer.h"
2917062Sbloom #include "tcp_var.h"
3017062Sbloom #include "tcpip.h"
3117062Sbloom #include "tcp_debug.h"
324601Swnj 
335300Sroot int	tcpprintfs = 0;
344679Swnj int	tcpcksum = 1;
355267Sroot struct	tcpiphdr tcp_saveti;
365440Swnj extern	tcpnodelack;
374601Swnj 
385267Sroot struct	tcpcb *tcp_newtcpcb();
3924816Skarels 
405065Swnj /*
4124816Skarels  * Insert segment ti into reassembly queue of tcp with
4224816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
4324816Skarels  * a segment with FIN.  The macro form does the common case inline
4424816Skarels  * (segment is the next to be received on an established connection,
4524816Skarels  * and the queue is empty), avoiding linkage into and removal
4624816Skarels  * from the queue and repetition of various conversions.
4724816Skarels  */
4824816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
4924816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
5024816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
5124816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
5224816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
5324816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
5430525Skarels 		tcpstat.tcps_rcvpack++;\
5530525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
5624816Skarels 		sbappend(&(so)->so_rcv, (m)); \
5724816Skarels 		sorwakeup(so); \
5824816Skarels 	} else \
5924816Skarels 		(flags) = tcp_reass((tp), (ti)); \
6024816Skarels }
6124816Skarels 
6224816Skarels tcp_reass(tp, ti)
6324816Skarels 	register struct tcpcb *tp;
6424816Skarels 	register struct tcpiphdr *ti;
6524816Skarels {
6624816Skarels 	register struct tcpiphdr *q;
6724816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
6824816Skarels 	struct mbuf *m;
6924816Skarels 	int flags;
7024816Skarels 
7124816Skarels 	/*
7224816Skarels 	 * Call with ti==0 after become established to
7324816Skarels 	 * force pre-ESTABLISHED data up to user socket.
7424816Skarels 	 */
7524816Skarels 	if (ti == 0)
7624816Skarels 		goto present;
7724816Skarels 
7824816Skarels 	/*
7924816Skarels 	 * Find a segment which begins after this one does.
8024816Skarels 	 */
8124816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8224816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
8324816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8424816Skarels 			break;
8524816Skarels 
8624816Skarels 	/*
8724816Skarels 	 * If there is a preceding segment, it may provide some of
8824816Skarels 	 * our data already.  If so, drop the data from the incoming
8924816Skarels 	 * segment.  If it provides all of our data, drop us.
9024816Skarels 	 */
9124816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
9224816Skarels 		register int i;
9324816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
9424816Skarels 		/* conversion to int (in i) handles seq wraparound */
9524816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
9624816Skarels 		if (i > 0) {
9730525Skarels 			if (i >= ti->ti_len) {
9830525Skarels 				tcpstat.tcps_rcvduppack++;
9930525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
10024816Skarels 				goto drop;
10130525Skarels 			}
10224816Skarels 			m_adj(dtom(ti), i);
10324816Skarels 			ti->ti_len -= i;
10424816Skarels 			ti->ti_seq += i;
10524816Skarels 		}
10624816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
10724816Skarels 	}
10830525Skarels 	tcpstat.tcps_rcvoopack++;
10930525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
11024816Skarels 
11124816Skarels 	/*
11224816Skarels 	 * While we overlap succeeding segments trim them or,
11324816Skarels 	 * if they are completely covered, dequeue them.
11424816Skarels 	 */
11524816Skarels 	while (q != (struct tcpiphdr *)tp) {
11624816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
11724816Skarels 		if (i <= 0)
11824816Skarels 			break;
11924816Skarels 		if (i < q->ti_len) {
12024816Skarels 			q->ti_seq += i;
12124816Skarels 			q->ti_len -= i;
12224816Skarels 			m_adj(dtom(q), i);
12324816Skarels 			break;
12424816Skarels 		}
12524816Skarels 		q = (struct tcpiphdr *)q->ti_next;
12624816Skarels 		m = dtom(q->ti_prev);
12724816Skarels 		remque(q->ti_prev);
12824816Skarels 		m_freem(m);
12924816Skarels 	}
13024816Skarels 
13124816Skarels 	/*
13224816Skarels 	 * Stick new segment in its place.
13324816Skarels 	 */
13424816Skarels 	insque(ti, q->ti_prev);
13524816Skarels 
13624816Skarels present:
13724816Skarels 	/*
13824816Skarels 	 * Present data to user, advancing rcv_nxt through
13924816Skarels 	 * completed sequence space.
14024816Skarels 	 */
14124816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
14224816Skarels 		return (0);
14324816Skarels 	ti = tp->seg_next;
14424816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
14524816Skarels 		return (0);
14624816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
14724816Skarels 		return (0);
14824816Skarels 	do {
14924816Skarels 		tp->rcv_nxt += ti->ti_len;
15024816Skarels 		flags = ti->ti_flags & TH_FIN;
15124816Skarels 		remque(ti);
15224816Skarels 		m = dtom(ti);
15324816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
15424816Skarels 		if (so->so_state & SS_CANTRCVMORE)
15524816Skarels 			m_freem(m);
15624816Skarels 		else
15724816Skarels 			sbappend(&so->so_rcv, m);
15824816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
15924816Skarels 	sorwakeup(so);
16024816Skarels 	return (flags);
16124816Skarels drop:
16224816Skarels 	m_freem(dtom(ti));
16324816Skarels 	return (0);
16424816Skarels }
16524816Skarels 
16624816Skarels /*
1675065Swnj  * TCP input routine, follows pages 65-76 of the
1685065Swnj  * protocol specification dated September, 1981 very closely.
1695065Swnj  */
1704924Swnj tcp_input(m0)
1714924Swnj 	struct mbuf *m0;
1724601Swnj {
1734924Swnj 	register struct tcpiphdr *ti;
1744924Swnj 	struct inpcb *inp;
1754924Swnj 	register struct mbuf *m;
1765440Swnj 	struct mbuf *om = 0;
1774924Swnj 	int len, tlen, off;
1785391Swnj 	register struct tcpcb *tp = 0;
1794924Swnj 	register int tiflags;
1804803Swnj 	struct socket *so;
18126824Skarels 	int todrop, acked, needoutput = 0;
1825267Sroot 	short ostate;
1836028Sroot 	struct in_addr laddr;
18410769Ssam 	int dropsocket = 0;
18530525Skarels 	int iss = 0;
1864924Swnj 
18730525Skarels 	tcpstat.tcps_rcvtotal++;
1884924Swnj 	/*
1895244Sroot 	 * Get IP and TCP header together in first mbuf.
1905244Sroot 	 * Note: IP leaves IP header in first mbuf.
1914924Swnj 	 */
1924924Swnj 	m = m0;
1935020Sroot 	ti = mtod(m, struct tcpiphdr *);
1945244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
1955208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
1965307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
1975307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
19830525Skarels 			tcpstat.tcps_rcvshort++;
1995307Sroot 			return;
2005085Swnj 		}
2015085Swnj 		ti = mtod(m, struct tcpiphdr *);
2025085Swnj 	}
2034601Swnj 
2044601Swnj 	/*
2055244Sroot 	 * Checksum extended TCP header and data.
2064601Swnj 	 */
2074924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2084924Swnj 	len = sizeof (struct ip) + tlen;
2094679Swnj 	if (tcpcksum) {
2104924Swnj 		ti->ti_next = ti->ti_prev = 0;
2114924Swnj 		ti->ti_x1 = 0;
2125223Swnj 		ti->ti_len = (u_short)tlen;
2136161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
2145231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
21511830Ssam 			if (tcpprintfs)
21611830Ssam 				printf("tcp sum: src %x\n", ti->ti_src);
21730525Skarels 			tcpstat.tcps_rcvbadsum++;
2185085Swnj 			goto drop;
2194601Swnj 		}
2204601Swnj 	}
2214601Swnj 
2224601Swnj 	/*
2235244Sroot 	 * Check that TCP offset makes sense,
2245440Swnj 	 * pull out TCP options and adjust length.
2254601Swnj 	 */
2264924Swnj 	off = ti->ti_off << 2;
2275231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
22811830Ssam 		if (tcpprintfs)
22911830Ssam 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
23030525Skarels 		tcpstat.tcps_rcvbadoff++;
2315085Swnj 		goto drop;
2324924Swnj 	}
2336211Swnj 	tlen -= off;
2346211Swnj 	ti->ti_len = tlen;
2355440Swnj 	if (off > sizeof (struct tcphdr)) {
23624816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
23724816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
23830525Skarels 				tcpstat.tcps_rcvshort++;
23924816Skarels 				return;
24024816Skarels 			}
24124816Skarels 			ti = mtod(m, struct tcpiphdr *);
2425440Swnj 		}
2439642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2445440Swnj 		if (om == 0)
2455440Swnj 			goto drop;
2465440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2475440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2486161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2495440Swnj 		  m->m_len -= om->m_len;
2506161Ssam 		  bcopy(op+om->m_len, op,
2516161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2525440Swnj 		}
2535440Swnj 	}
2545065Swnj 	tiflags = ti->ti_flags;
2554924Swnj 
2566093Sroot 	/*
25725729Smckusick 	 * Drop TCP and IP headers; TCP options were dropped above.
2586093Sroot 	 */
25925729Smckusick 	m->m_off += sizeof(struct tcpiphdr);
26025729Smckusick 	m->m_len -= sizeof(struct tcpiphdr);
2616093Sroot 
2624924Swnj 	/*
2635244Sroot 	 * Convert TCP protocol specific fields to host format.
2645085Swnj 	 */
2655085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
2665085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
2675085Swnj 	ti->ti_win = ntohs(ti->ti_win);
2685085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
2695085Swnj 
2705085Swnj 	/*
2718271Sroot 	 * Locate pcb for segment.
2724924Swnj 	 */
27330525Skarels findpcb:
2745065Swnj 	inp = in_pcblookup
2756028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
2766028Sroot 		INPLOOKUP_WILDCARD);
2775065Swnj 
2785065Swnj 	/*
2795065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2805244Sroot 	 * all data in the incoming segment is discarded.
2815065Swnj 	 */
2825300Sroot 	if (inp == 0)
2835085Swnj 		goto dropwithreset;
2845065Swnj 	tp = intotcpcb(inp);
2855300Sroot 	if (tp == 0)
2865085Swnj 		goto dropwithreset;
2875109Swnj 	so = inp->inp_socket;
2885267Sroot 	if (so->so_options & SO_DEBUG) {
2895267Sroot 		ostate = tp->t_state;
2905267Sroot 		tcp_saveti = *ti;
2915267Sroot 	}
2927510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2937510Sroot 		so = sonewconn(so);
2947510Sroot 		if (so == 0)
2957510Sroot 			goto drop;
29610769Ssam 		/*
29710769Ssam 		 * This is ugly, but ....
29810769Ssam 		 *
29910769Ssam 		 * Mark socket as temporary until we're
30010769Ssam 		 * committed to keeping it.  The code at
30110769Ssam 		 * ``drop'' and ``dropwithreset'' check the
30210769Ssam 		 * flag dropsocket to see if the temporary
30310769Ssam 		 * socket created here should be discarded.
30410769Ssam 		 * We mark the socket as discardable until
30510769Ssam 		 * we're committed to it below in TCPS_LISTEN.
30610769Ssam 		 */
30710769Ssam 		dropsocket++;
3087510Sroot 		inp = (struct inpcb *)so->so_pcb;
3097510Sroot 		inp->inp_laddr = ti->ti_dst;
3107510Sroot 		inp->inp_lport = ti->ti_dport;
31124816Skarels 		inp->inp_options = ip_srcroute();
3127510Sroot 		tp = intotcpcb(inp);
3137510Sroot 		tp->t_state = TCPS_LISTEN;
3147510Sroot 	}
3154601Swnj 
3164601Swnj 	/*
3175162Swnj 	 * Segment received on connection.
3185162Swnj 	 * Reset idle time and keep-alive timer.
3195162Swnj 	 */
3205162Swnj 	tp->t_idle = 0;
3215162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
3225162Swnj 
3235162Swnj 	/*
32417272Skarels 	 * Process options if not in LISTEN state,
32517272Skarels 	 * else do it below (after getting remote address).
3265440Swnj 	 */
32717272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
32817272Skarels 		tcp_dooptions(tp, om, ti);
3295440Swnj 		om = 0;
3305440Swnj 	}
3315440Swnj 
3325440Swnj 	/*
3335085Swnj 	 * Calculate amount of space in receive window,
3345085Swnj 	 * and then do TCP input processing.
33524816Skarels 	 * Receive window is amount of space in rcv queue,
33624816Skarels 	 * but not less than advertised window.
3374601Swnj 	 */
33825939Skarels 	{ int win;
3394601Swnj 
34025939Skarels 	win = sbspace(&so->so_rcv);
34125939Skarels 	if (win < 0)
34225939Skarels 		win = 0;
34325939Skarels 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
34425939Skarels 	}
34525939Skarels 
3464601Swnj 	switch (tp->t_state) {
3474601Swnj 
3485065Swnj 	/*
3495065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3505065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3515065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
35225197Skarels 	 * Don't bother responding if the destination was a broadcast.
3535085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3545065Swnj 	 * tp->iss, and send a segment:
3555085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3565065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3575065Swnj 	 * Fill in remote peer address fields if not previously specified.
3585065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3595244Sroot 	 * segment in this state.
3605065Swnj 	 */
3618271Sroot 	case TCPS_LISTEN: {
36210145Ssam 		struct mbuf *am;
3638271Sroot 		register struct sockaddr_in *sin;
3648271Sroot 
3655065Swnj 		if (tiflags & TH_RST)
3665065Swnj 			goto drop;
3675300Sroot 		if (tiflags & TH_ACK)
3685085Swnj 			goto dropwithreset;
3695300Sroot 		if ((tiflags & TH_SYN) == 0)
3705065Swnj 			goto drop;
37125197Skarels 		if (in_broadcast(ti->ti_dst))
37225197Skarels 			goto drop;
37310145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
37410145Ssam 		if (am == NULL)
37510145Ssam 			goto drop;
37610145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3778599Sroot 		sin = mtod(am, struct sockaddr_in *);
3788271Sroot 		sin->sin_family = AF_INET;
3798271Sroot 		sin->sin_addr = ti->ti_src;
3808271Sroot 		sin->sin_port = ti->ti_sport;
3816028Sroot 		laddr = inp->inp_laddr;
38210145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3836028Sroot 			inp->inp_laddr = ti->ti_dst;
3848599Sroot 		if (in_pcbconnect(inp, am)) {
3856028Sroot 			inp->inp_laddr = laddr;
3868716Sroot 			(void) m_free(am);
3875244Sroot 			goto drop;
3886028Sroot 		}
3898716Sroot 		(void) m_free(am);
3905244Sroot 		tp->t_template = tcp_template(tp);
3915244Sroot 		if (tp->t_template == 0) {
39226386Skarels 			tp = tcp_drop(tp, ENOBUFS);
39317264Skarels 			dropsocket = 0;		/* socket is already gone */
3945244Sroot 			goto drop;
3955244Sroot 		}
39617272Skarels 		if (om) {
39717272Skarels 			tcp_dooptions(tp, om, ti);
39817272Skarels 			om = 0;
39917272Skarels 		}
40030525Skarels 		if (iss)
40130525Skarels 			tp->iss = iss;
40230525Skarels 		else
40330525Skarels 			tp->iss = tcp_iss;
40430525Skarels 		tcp_iss += TCP_ISSINCR/2;
4055065Swnj 		tp->irs = ti->ti_seq;
4065085Swnj 		tcp_sendseqinit(tp);
4075085Swnj 		tcp_rcvseqinit(tp);
40825939Skarels 		tp->t_flags |= TF_ACKNOW;
4095065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
4105244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
41110769Ssam 		dropsocket = 0;		/* committed to socket */
41230525Skarels 		tcpstat.tcps_accepts++;
4135085Swnj 		goto trimthenstep6;
4148271Sroot 		}
4154601Swnj 
4165065Swnj 	/*
4175065Swnj 	 * If the state is SYN_SENT:
4185065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
4195065Swnj 	 *	if seg contains a RST, then drop the connection.
4205065Swnj 	 *	if seg does not contain SYN, then drop it.
4215065Swnj 	 * Otherwise this is an acceptable SYN segment
4225065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4235065Swnj 	 *	if seg contains ack then advance tp->snd_una
4245065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4255065Swnj 	 *	arrange for segment to be acked (eventually)
4265065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4275065Swnj 	 */
4285065Swnj 	case TCPS_SYN_SENT:
4295065Swnj 		if ((tiflags & TH_ACK) &&
43024816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4315231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4325085Swnj 			goto dropwithreset;
4335065Swnj 		if (tiflags & TH_RST) {
43410394Ssam 			if (tiflags & TH_ACK)
43510394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4365065Swnj 			goto drop;
4374601Swnj 		}
4385065Swnj 		if ((tiflags & TH_SYN) == 0)
4395065Swnj 			goto drop;
440*30974Skarels 		if (tiflags & TH_ACK) {
441*30974Skarels 			tp->snd_una = ti->ti_ack;
442*30974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
443*30974Skarels 				tp->snd_nxt = tp->snd_una;
444*30974Skarels 		}
4455244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4465065Swnj 		tp->irs = ti->ti_seq;
4475085Swnj 		tcp_rcvseqinit(tp);
4485085Swnj 		tp->t_flags |= TF_ACKNOW;
449*30974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
45030525Skarels 			tcpstat.tcps_connects++;
4515244Sroot 			soisconnected(so);
4525065Swnj 			tp->t_state = TCPS_ESTABLISHED;
45317272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4545162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
4555162Swnj 		} else
4565085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4575085Swnj 
4585085Swnj trimthenstep6:
4595085Swnj 		/*
4605231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4615085Swnj 		 * If data, trim to stay within window,
4625085Swnj 		 * dropping FIN if necessary.
4635085Swnj 		 */
4645231Swnj 		ti->ti_seq++;
4655085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4665085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4675085Swnj 			m_adj(m, -todrop);
4685085Swnj 			ti->ti_len = tp->rcv_wnd;
46925939Skarels 			tiflags &= ~TH_FIN;
47030525Skarels 			tcpstat.tcps_rcvpackafterwin++;
47130525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
4725065Swnj 		}
4735263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
47425939Skarels 		tp->rcv_up = ti->ti_seq;
4755085Swnj 		goto step6;
4765065Swnj 	}
4774601Swnj 
4785065Swnj 	/*
47930525Skarels 	 * States other than LISTEN or SYN_SENT.
48030525Skarels 	 * First check that at least some bytes of segment are within
48130525Skarels 	 * receive window.  If segment begins before rcv_nxt,
48230525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
48316222Skarels 	 */
48430525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
48530525Skarels 	if (todrop > 0) {
48630525Skarels 		if (tiflags & TH_SYN) {
48730525Skarels 			tiflags &= ~TH_SYN;
48830525Skarels 			ti->ti_seq++;
48930525Skarels 			if (ti->ti_urp > 1)
49030525Skarels 				ti->ti_urp--;
49130525Skarels 			else
49230525Skarels 				tiflags &= ~TH_URG;
49330525Skarels 			todrop--;
49430525Skarels 		}
49530525Skarels 		if (todrop > ti->ti_len ||
49630525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
49730525Skarels 			tcpstat.tcps_rcvduppack++;
49830525Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
49930525Skarels 			goto dropafterack;
50030525Skarels 		}
50130525Skarels 		tcpstat.tcps_rcvpartduppack++;
50230525Skarels 		tcpstat.tcps_rcvpartdupbyte += todrop;
50330525Skarels 		m_adj(m, todrop);
50430525Skarels 		ti->ti_seq += todrop;
50530525Skarels 		ti->ti_len -= todrop;
50630525Skarels 		if (ti->ti_urp > todrop)
50730525Skarels 			ti->ti_urp -= todrop;
50830525Skarels 		else {
50930525Skarels 			tiflags &= ~TH_URG;
51030525Skarels 			ti->ti_urp = 0;
51130525Skarels 		}
51216222Skarels 	}
51316222Skarels 
5145065Swnj 	if (tp->rcv_wnd == 0) {
5155065Swnj 		/*
5165065Swnj 		 * If window is closed can only take segments at
5175231Swnj 		 * window edge, and have to drop data and PUSH from
5185065Swnj 		 * incoming segments.
51930525Skarels 		 *
52030525Skarels 		 * If new data is received on a connection after the
52130525Skarels 		 * user processes are gone, then RST the other end.
5225065Swnj 		 */
52330525Skarels 		if ((so->so_state & SS_NOFDREF) &&
52430525Skarels 		    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
52530525Skarels 			tp = tcp_close(tp);
52630525Skarels 			tcpstat.tcps_rcvafterclose++;
52730525Skarels 			goto dropwithreset;
52830525Skarels 		}
52930525Skarels 		if (tp->rcv_nxt != ti->ti_seq) {
53030525Skarels 			tcpstat.tcps_rcvpackafterwin++;
53130525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
5325065Swnj 			goto dropafterack;
53330525Skarels 		}
5345085Swnj 		if (ti->ti_len > 0) {
53530525Skarels 			if (ti->ti_len == 1)
53630525Skarels 				tcpstat.tcps_rcvwinprobe++;
53730525Skarels 			else {
53830525Skarels 				tcpstat.tcps_rcvpackafterwin++;
53930525Skarels 				tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
54030525Skarels 			}
5415690Swnj 			m_adj(m, ti->ti_len);
5425085Swnj 			ti->ti_len = 0;
54325939Skarels 			tiflags &= ~(TH_PUSH|TH_FIN);
5445065Swnj 		}
5455065Swnj 	} else {
5465065Swnj 		/*
5475065Swnj 		 * If segment ends after window, drop trailing data
5485085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
5495065Swnj 		 */
5505690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
5515690Swnj 		if (todrop > 0) {
55230525Skarels 			if (todrop >= ti->ti_len) {
55330525Skarels 				/*
55430525Skarels 				 * If a new connection request is received
55530525Skarels 				 * while in TIME_WAIT, drop the old connection
55630525Skarels 				 * and start over if the sequence numbers
55730525Skarels 				 * are above the previous ones.
55830525Skarels 				 */
55930525Skarels 				if (tiflags & TH_SYN &&
56030525Skarels 				    tp->t_state == TCPS_TIME_WAIT &&
56130525Skarels 				    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
56230525Skarels 					iss = tp->rcv_nxt + TCP_ISSINCR;
56330525Skarels 					(void) tcp_close(tp);
56430525Skarels 					goto findpcb;
56530525Skarels 				}
56630525Skarels 				if (todrop == 1)
56730525Skarels 					tcpstat.tcps_rcvwinprobe++;
56830525Skarels 				else {
56930525Skarels 					tcpstat.tcps_rcvpackafterwin++;
57030525Skarels 					tcpstat.tcps_rcvbyteafterwin += todrop;
57130525Skarels 				}
5725065Swnj 				goto dropafterack;
57330525Skarels 			}
57430525Skarels 			tcpstat.tcps_rcvpackafterwin++;
57530525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
5765065Swnj 			m_adj(m, -todrop);
5775065Swnj 			ti->ti_len -= todrop;
57825939Skarels 			tiflags &= ~(TH_PUSH|TH_FIN);
5795065Swnj 		}
5805065Swnj 	}
5814601Swnj 
5825065Swnj 	/*
5835065Swnj 	 * If the RST bit is set examine the state:
5845065Swnj 	 *    SYN_RECEIVED STATE:
5855065Swnj 	 *	If passive open, return to LISTEN state.
5865065Swnj 	 *	If active open, inform user that connection was refused.
5875065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
5885065Swnj 	 *	Inform user that connection was reset, and close tcb.
5895065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
5905065Swnj 	 *	Close the tcb.
5915065Swnj 	 */
5925065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
5935267Sroot 
5945065Swnj 	case TCPS_SYN_RECEIVED:
59510394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
5965065Swnj 		goto drop;
5974601Swnj 
5985065Swnj 	case TCPS_ESTABLISHED:
5995065Swnj 	case TCPS_FIN_WAIT_1:
6005065Swnj 	case TCPS_FIN_WAIT_2:
6015065Swnj 	case TCPS_CLOSE_WAIT:
60210394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6035065Swnj 		goto drop;
6045065Swnj 
6055065Swnj 	case TCPS_CLOSING:
6065065Swnj 	case TCPS_LAST_ACK:
6075065Swnj 	case TCPS_TIME_WAIT:
60810394Ssam 		tp = tcp_close(tp);
6095065Swnj 		goto drop;
6104601Swnj 	}
6114601Swnj 
6124601Swnj 	/*
6135065Swnj 	 * If a SYN is in the window, then this is an
6145065Swnj 	 * error and we send an RST and drop the connection.
6154601Swnj 	 */
6165065Swnj 	if (tiflags & TH_SYN) {
61710394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6185085Swnj 		goto dropwithreset;
6194601Swnj 	}
6204601Swnj 
6214601Swnj 	/*
6225065Swnj 	 * If the ACK bit is off we drop the segment and return.
6234601Swnj 	 */
6245085Swnj 	if ((tiflags & TH_ACK) == 0)
6255065Swnj 		goto drop;
6265065Swnj 
6275065Swnj 	/*
6285065Swnj 	 * Ack processing.
6295065Swnj 	 */
6304601Swnj 	switch (tp->t_state) {
6314601Swnj 
6325065Swnj 	/*
6335065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
6345065Swnj 	 * ESTABLISHED state and continue processing, othewise
6355065Swnj 	 * send an RST.
6365065Swnj 	 */
6375065Swnj 	case TCPS_SYN_RECEIVED:
6385085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
6395231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
6405085Swnj 			goto dropwithreset;
6415244Sroot 		tp->snd_una++;			/* SYN acked */
6425357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
6435357Sroot 			tp->snd_nxt = tp->snd_una;
6445244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
64530525Skarels 		tcpstat.tcps_connects++;
6465085Swnj 		soisconnected(so);
6475085Swnj 		tp->t_state = TCPS_ESTABLISHED;
64817272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
6495162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
6505244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
6515085Swnj 		/* fall into ... */
6524601Swnj 
6535065Swnj 	/*
6545065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
6555065Swnj 	 * ACKs.  If the ack is in the range
6565231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6575065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6585065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6595065Swnj 	 * more up to date window information we update our window information.
6605065Swnj 	 */
6615065Swnj 	case TCPS_ESTABLISHED:
6625065Swnj 	case TCPS_FIN_WAIT_1:
6635065Swnj 	case TCPS_FIN_WAIT_2:
6645065Swnj 	case TCPS_CLOSE_WAIT:
6655065Swnj 	case TCPS_CLOSING:
6665244Sroot 	case TCPS_LAST_ACK:
6675244Sroot 	case TCPS_TIME_WAIT:
6685085Swnj 
66930525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
67030525Skarels 			if (ti->ti_len == 0)
67130525Skarels 				tcpstat.tcps_rcvdupack++;
6725065Swnj 			break;
67330525Skarels 		}
67430525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
67530525Skarels 			tcpstat.tcps_rcvacktoomuch++;
6765065Swnj 			goto dropafterack;
67730525Skarels 		}
6785085Swnj 		acked = ti->ti_ack - tp->snd_una;
67930525Skarels 		tcpstat.tcps_rcvackpack++;
68030525Skarels 		tcpstat.tcps_rcvackbyte += acked;
6815951Swnj 
6825951Swnj 		/*
6835951Swnj 		 * If transmit timer is running and timed sequence
6845951Swnj 		 * number was acked, update smoothed round trip time.
6855951Swnj 		 */
6865951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
68730525Skarels 			tcpstat.tcps_rttupdated++;
6885951Swnj 			if (tp->t_srtt == 0)
6895951Swnj 				tp->t_srtt = tp->t_rtt;
6905951Swnj 			else
6915951Swnj 				tp->t_srtt =
6925951Swnj 				    tcp_alpha * tp->t_srtt +
6935951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
6945951Swnj 			tp->t_rtt = 0;
6955951Swnj 		}
6965951Swnj 
69726824Skarels 		/*
69826824Skarels 		 * If all outstanding data is acked, stop retransmit
69926824Skarels 		 * timer and remember to restart (more output or persist).
70026824Skarels 		 * If there is more data to be acked, restart retransmit
70126824Skarels 		 * timer.
70226824Skarels 		 */
70326824Skarels 		if (ti->ti_ack == tp->snd_max) {
7045244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
70526824Skarels 			needoutput = 1;
70626824Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0) {
7075244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
7085244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
7095300Sroot 			tp->t_rxtshift = 0;
7105085Swnj 		}
71117360Skarels 		/*
71217360Skarels 		 * When new data is acked, open the congestion window a bit.
71317360Skarels 		 */
71430525Skarels 		tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
7155307Sroot 		if (acked > so->so_snd.sb_cc) {
71615386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
71726386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
71830525Skarels #define	ourfinisacked	(acked > 0)
7195307Sroot 		} else {
7206161Ssam 			sbdrop(&so->so_snd, acked);
7215307Sroot 			tp->snd_wnd -= acked;
7225307Sroot 			acked = 0;
7235307Sroot 		}
7246434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
7255300Sroot 			sowwakeup(so);
7265231Swnj 		tp->snd_una = ti->ti_ack;
7275357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
7285357Sroot 			tp->snd_nxt = tp->snd_una;
7295162Swnj 
7304601Swnj 		switch (tp->t_state) {
7314601Swnj 
7325065Swnj 		/*
7335065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
7345065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
7355085Swnj 		 * then enter FIN_WAIT_2.
7365065Swnj 		 */
7375065Swnj 		case TCPS_FIN_WAIT_1:
7385896Swnj 			if (ourfinisacked) {
7395896Swnj 				/*
7405896Swnj 				 * If we can't receive any more
7415896Swnj 				 * data, then closing user can proceed.
74224816Skarels 				 * Starting the timer is contrary to the
74324816Skarels 				 * specification, but if we don't get a FIN
74424816Skarels 				 * we'll hang forever.
7455896Swnj 				 */
74624816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
7475896Swnj 					soisdisconnected(so);
74824816Skarels 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
74924816Skarels 				}
7505085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
7515896Swnj 			}
7524601Swnj 			break;
7534601Swnj 
7545065Swnj 	 	/*
7555065Swnj 		 * In CLOSING STATE in addition to the processing for
7565065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
7575065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
7585065Swnj 		 * the segment.
7595065Swnj 		 */
7605065Swnj 		case TCPS_CLOSING:
7615244Sroot 			if (ourfinisacked) {
7625065Swnj 				tp->t_state = TCPS_TIME_WAIT;
7635244Sroot 				tcp_canceltimers(tp);
7645244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
7655244Sroot 				soisdisconnected(so);
7665244Sroot 			}
7675244Sroot 			break;
7684601Swnj 
7695065Swnj 		/*
7705085Swnj 		 * The only thing that can arrive in  LAST_ACK state
7715085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
7725085Swnj 		 * acknowledged, delete the TCB, enter the closed state
7735085Swnj 		 * and return.
7745065Swnj 		 */
7755065Swnj 		case TCPS_LAST_ACK:
77610394Ssam 			if (ourfinisacked)
77710394Ssam 				tp = tcp_close(tp);
7785065Swnj 			goto drop;
7794601Swnj 
7805065Swnj 		/*
7815065Swnj 		 * In TIME_WAIT state the only thing that should arrive
7825065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
7835065Swnj 		 * it and restart the finack timer.
7845065Swnj 		 */
7855065Swnj 		case TCPS_TIME_WAIT:
7865162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
7875065Swnj 			goto dropafterack;
7884601Swnj 		}
7895085Swnj #undef ourfinisacked
7905085Swnj 	}
7914601Swnj 
7925065Swnj step6:
7935065Swnj 	/*
7945244Sroot 	 * Update window information.
79525939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
7965244Sroot 	 */
79725939Skarels 	if ((tiflags & TH_ACK) &&
79825939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
7995391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
80025939Skarels 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
80130525Skarels 		/* keep track of pure window updates */
80230525Skarels 		if (ti->ti_len == 0 &&
80330525Skarels 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) {
80430525Skarels 			tcpstat.tcps_rcvwinupd++;
80530525Skarels 			tcpstat.tcps_rcvdupack--;
80630525Skarels 		}
8075244Sroot 		tp->snd_wnd = ti->ti_win;
8085244Sroot 		tp->snd_wl1 = ti->ti_seq;
8095244Sroot 		tp->snd_wl2 = ti->ti_ack;
81025260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
81125260Skarels 			tp->max_sndwnd = tp->snd_wnd;
81226824Skarels 		needoutput = 1;
81326824Skarels 	}
8145244Sroot 
8155244Sroot 	/*
8165547Swnj 	 * Process segments with URG.
8175065Swnj 	 */
8187267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
8197267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8205547Swnj 		/*
82125939Skarels 		 * This is a kludge, but if we receive and accept
82213121Ssam 		 * random urgent pointers, we'll crash in
82313121Ssam 		 * soreceive.  It's hard to imagine someone
82413121Ssam 		 * actually wanting to send this much urgent data.
82512441Ssam 		 */
82625652Skarels 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
82712441Ssam 			ti->ti_urp = 0;			/* XXX */
82812441Ssam 			tiflags &= ~TH_URG;		/* XXX */
82925939Skarels 			goto dodata;			/* XXX */
83012441Ssam 		}
83112441Ssam 		/*
8325547Swnj 		 * If this segment advances the known urgent pointer,
8335547Swnj 		 * then mark the data stream.  This should not happen
8345547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
8355547Swnj 		 * a FIN has been received from the remote side.
8365547Swnj 		 * In these states we ignore the URG.
83727190Skarels 		 *
83827190Skarels 		 * According to RFC961 (Assigned Protocols),
83927190Skarels 		 * the urgent pointer points to the last octet
84027190Skarels 		 * of urgent data.  We continue, however,
84127190Skarels 		 * to consider it to indicate the first octet
84227190Skarels 		 * of data past the urgent section
84327190Skarels 		 * as the original spec states.
8445547Swnj 		 */
8455547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
8465547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
8475547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
8485547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
8495547Swnj 			if (so->so_oobmark == 0)
8505547Swnj 				so->so_state |= SS_RCVATMARK;
8518313Sroot 			sohasoutofband(so);
85224816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8535440Swnj 		}
8545547Swnj 		/*
8555547Swnj 		 * Remove out of band data so doesn't get presented to user.
8565547Swnj 		 * This can happen independent of advancing the URG pointer,
8575547Swnj 		 * but if two URG's are pending at once, some out-of-band
8585547Swnj 		 * data may creep in... ick.
8595547Swnj 		 */
86027190Skarels 		if (ti->ti_urp <= ti->ti_len &&
86127190Skarels 		    (so->so_options & SO_OOBINLINE) == 0)
8625547Swnj 			tcp_pulloutofband(so, ti);
86325939Skarels 	} else
86425939Skarels 		/*
86525939Skarels 		 * If no out of band data is expected,
86625939Skarels 		 * pull receive urgent pointer along
86725939Skarels 		 * with the receive window.
86825939Skarels 		 */
86925939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
87025939Skarels 			tp->rcv_up = tp->rcv_nxt;
87125939Skarels dodata:							/* XXX */
8724601Swnj 
8734601Swnj 	/*
8745065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
8755065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
8765065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
8775065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
8785065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
8795065Swnj 	 * connection then we just ignore the text.
8804601Swnj 	 */
88117946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
88217946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
88324816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
8845440Swnj 		if (tcpnodelack == 0)
8855440Swnj 			tp->t_flags |= TF_DELACK;
8865440Swnj 		else
8875440Swnj 			tp->t_flags |= TF_ACKNOW;
88825260Skarels 		/*
88925260Skarels 		 * Note the amount of data that peer has sent into
89025260Skarels 		 * our window, in order to estimate the sender's
89125260Skarels 		 * buffer size.
89225260Skarels 		 */
89325260Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv);
89425260Skarels 		if (len > tp->max_rcvd)
89525260Skarels 			tp->max_rcvd = len;
8965244Sroot 	} else {
8974924Swnj 		m_freem(m);
8985263Swnj 		tiflags &= ~TH_FIN;
8995244Sroot 	}
9004601Swnj 
9014601Swnj 	/*
9025263Swnj 	 * If FIN is received ACK the FIN and let the user know
9035263Swnj 	 * that the connection is closing.
9044601Swnj 	 */
9055263Swnj 	if (tiflags & TH_FIN) {
9065244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
9075244Sroot 			socantrcvmore(so);
9085244Sroot 			tp->t_flags |= TF_ACKNOW;
9095244Sroot 			tp->rcv_nxt++;
9105244Sroot 		}
9115065Swnj 		switch (tp->t_state) {
9124601Swnj 
9135065Swnj 	 	/*
9145065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
9155065Swnj 		 * enter the CLOSE_WAIT state.
9164884Swnj 		 */
9175065Swnj 		case TCPS_SYN_RECEIVED:
9185065Swnj 		case TCPS_ESTABLISHED:
9195065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
9205065Swnj 			break;
9214884Swnj 
9225065Swnj 	 	/*
9235085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
9245085Swnj 		 * enter the CLOSING state.
9254884Swnj 		 */
9265065Swnj 		case TCPS_FIN_WAIT_1:
9275085Swnj 			tp->t_state = TCPS_CLOSING;
9285065Swnj 			break;
9294601Swnj 
9305065Swnj 	 	/*
9315065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
9325065Swnj 		 * starting the time-wait timer, turning off the other
9335065Swnj 		 * standard timers.
9345065Swnj 		 */
9355065Swnj 		case TCPS_FIN_WAIT_2:
9365244Sroot 			tp->t_state = TCPS_TIME_WAIT;
9375074Swnj 			tcp_canceltimers(tp);
9385162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9395244Sroot 			soisdisconnected(so);
9405065Swnj 			break;
9415065Swnj 
9424884Swnj 		/*
9435065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
9444884Swnj 		 */
9455065Swnj 		case TCPS_TIME_WAIT:
9465162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9475065Swnj 			break;
9485085Swnj 		}
9494601Swnj 	}
9505267Sroot 	if (so->so_options & SO_DEBUG)
9515267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
9525085Swnj 
9535085Swnj 	/*
9545085Swnj 	 * Return any desired output.
9555085Swnj 	 */
95626824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
95725939Skarels 		(void) tcp_output(tp);
9585065Swnj 	return;
9595085Swnj 
9605065Swnj dropafterack:
9615085Swnj 	/*
9626211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
9636211Swnj 	 * sequence space, where the ACK reflects our state.
9645085Swnj 	 */
96526057Skarels 	if (tiflags & TH_RST)
9665085Swnj 		goto drop;
9676303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
9686303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
9695391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
9705231Swnj 	return;
9715085Swnj 
9725085Swnj dropwithreset:
97311731Ssam 	if (om) {
9746161Ssam 		(void) m_free(om);
97511731Ssam 		om = 0;
97611731Ssam 	}
9775085Swnj 	/*
9785244Sroot 	 * Generate a RST, dropping incoming segment.
9795085Swnj 	 * Make ACK acceptable to originator of segment.
98025197Skarels 	 * Don't bother to respond if destination was broadcast.
9815085Swnj 	 */
98225197Skarels 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
9835085Swnj 		goto drop;
9845085Swnj 	if (tiflags & TH_ACK)
9855391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
9865085Swnj 	else {
9875085Swnj 		if (tiflags & TH_SYN)
9885085Swnj 			ti->ti_len++;
9896211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
9906211Swnj 		    TH_RST|TH_ACK);
9915085Swnj 	}
99210769Ssam 	/* destroy temporarily created socket */
99310769Ssam 	if (dropsocket)
99410769Ssam 		(void) soabort(so);
9955231Swnj 	return;
9965085Swnj 
9975065Swnj drop:
99811730Ssam 	if (om)
99911730Ssam 		(void) m_free(om);
10005085Swnj 	/*
10015085Swnj 	 * Drop space held by incoming segment and return.
10025085Swnj 	 */
10036303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
10046303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
10055065Swnj 	m_freem(m);
100610769Ssam 	/* destroy temporarily created socket */
100710769Ssam 	if (dropsocket)
100810769Ssam 		(void) soabort(so);
10095267Sroot 	return;
10105065Swnj }
10115065Swnj 
101217272Skarels tcp_dooptions(tp, om, ti)
10135440Swnj 	struct tcpcb *tp;
10145440Swnj 	struct mbuf *om;
101517272Skarels 	struct tcpiphdr *ti;
10165419Swnj {
10175440Swnj 	register u_char *cp;
10185440Swnj 	int opt, optlen, cnt;
10195419Swnj 
10205440Swnj 	cp = mtod(om, u_char *);
10215440Swnj 	cnt = om->m_len;
10225440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
10235440Swnj 		opt = cp[0];
10245440Swnj 		if (opt == TCPOPT_EOL)
10255440Swnj 			break;
10265440Swnj 		if (opt == TCPOPT_NOP)
10275440Swnj 			optlen = 1;
102812169Ssam 		else {
10295440Swnj 			optlen = cp[1];
103012169Ssam 			if (optlen <= 0)
103112169Ssam 				break;
103212169Ssam 		}
10335440Swnj 		switch (opt) {
10345440Swnj 
10355440Swnj 		default:
10365440Swnj 			break;
10375440Swnj 
10385440Swnj 		case TCPOPT_MAXSEG:
10395440Swnj 			if (optlen != 4)
10405440Swnj 				continue;
104117272Skarels 			if (!(ti->ti_flags & TH_SYN))
104217272Skarels 				continue;
10435440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
10446161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
104517272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
10465440Swnj 			break;
10475419Swnj 		}
10485419Swnj 	}
10496161Ssam 	(void) m_free(om);
10505419Swnj }
10515419Swnj 
10525419Swnj /*
10535547Swnj  * Pull out of band byte out of a segment so
10545547Swnj  * it doesn't appear in the user's data queue.
10555547Swnj  * It is still reflected in the segment length for
10565547Swnj  * sequencing purposes.
10575547Swnj  */
10585547Swnj tcp_pulloutofband(so, ti)
10595547Swnj 	struct socket *so;
10605547Swnj 	struct tcpiphdr *ti;
10615547Swnj {
10625547Swnj 	register struct mbuf *m;
10636116Swnj 	int cnt = ti->ti_urp - 1;
10645547Swnj 
10655547Swnj 	m = dtom(ti);
10665547Swnj 	while (cnt >= 0) {
10675547Swnj 		if (m->m_len > cnt) {
10685547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
10695547Swnj 			struct tcpcb *tp = sototcpcb(so);
10705547Swnj 
10715547Swnj 			tp->t_iobc = *cp;
10725547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
10736161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
10745547Swnj 			m->m_len--;
10755547Swnj 			return;
10765547Swnj 		}
10775547Swnj 		cnt -= m->m_len;
10785547Swnj 		m = m->m_next;
10795547Swnj 		if (m == 0)
10805547Swnj 			break;
10815547Swnj 	}
10825547Swnj 	panic("tcp_pulloutofband");
10835547Swnj }
10845547Swnj 
10855547Swnj /*
108617272Skarels  *  Determine a reasonable value for maxseg size.
108717272Skarels  *  If the route is known, use one that can be handled
108817272Skarels  *  on the given interface without forcing IP to fragment.
108923975Skarels  *  If bigger than a page (CLBYTES), round down to nearest pagesize
109017272Skarels  *  to utilize pagesize mbufs.
109117272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
109223975Skarels  *  use a conservative size (512 or the default IP max size, but no more
109323975Skarels  *  than the mtu of the interface through which we route),
109417272Skarels  *  as we can't discover anything about intervening gateways or networks.
109517272Skarels  *
109617272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
109717272Skarels  */
109817272Skarels tcp_mss(tp)
109923975Skarels 	register struct tcpcb *tp;
110017272Skarels {
110117272Skarels 	struct route *ro;
110217272Skarels 	struct ifnet *ifp;
110317272Skarels 	int mss;
110417272Skarels 	struct inpcb *inp;
110517272Skarels 
110617272Skarels 	inp = tp->t_inpcb;
110717272Skarels 	ro = &inp->inp_route;
110817272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
110917272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
111017272Skarels 		/* No route yet, so try to acquire one */
111117272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
111217272Skarels 			ro->ro_dst.sa_family = AF_INET;
111317272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
111417272Skarels 				inp->inp_faddr;
111517272Skarels 			rtalloc(ro);
111617272Skarels 		}
111717272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
111817316Skarels 			return (TCP_MSS);
111917272Skarels 	}
112017272Skarels 
112117272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
112217272Skarels #if	(CLBYTES & (CLBYTES - 1)) == 0
112317272Skarels 	if (mss > CLBYTES)
112417272Skarels 		mss &= ~(CLBYTES-1);
112517272Skarels #else
112617272Skarels 	if (mss > CLBYTES)
112717272Skarels 		mss = mss / CLBYTES * CLBYTES;
112817272Skarels #endif
112923975Skarels 	if (in_localaddr(inp->inp_faddr))
113023975Skarels 		return (mss);
113117316Skarels 	return (MIN(mss, TCP_MSS));
113217272Skarels }
1133