xref: /csrg-svn/sys/netinet/tcp_input.c (revision 33745)
123190Smckusick /*
2*33745Skarels  * 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
632787Sbostic  * provided that this notice is preserved and that due credit is given
732787Sbostic  * to the University of California at Berkeley. The name of the University
832787Sbostic  * may not be used to endorse or promote products derived from this
932787Sbostic  * software without specific prior written permission. This software
1032787Sbostic  * is provided ``as is'' without express or implied warranty.
1132787Sbostic  *
12*33745Skarels  *	@(#)tcp_input.c	7.17 (Berkeley) 03/16/88
1323190Smckusick  */
144601Swnj 
1517062Sbloom #include "param.h"
1617062Sbloom #include "systm.h"
1717062Sbloom #include "mbuf.h"
1817062Sbloom #include "protosw.h"
1917062Sbloom #include "socket.h"
2017062Sbloom #include "socketvar.h"
2117062Sbloom #include "errno.h"
2210894Ssam 
2310894Ssam #include "../net/if.h"
2410894Ssam #include "../net/route.h"
2510894Ssam 
2617062Sbloom #include "in.h"
2717062Sbloom #include "in_pcb.h"
2817062Sbloom #include "in_systm.h"
2917062Sbloom #include "ip.h"
3017062Sbloom #include "ip_var.h"
3117062Sbloom #include "tcp.h"
3217062Sbloom #include "tcp_fsm.h"
3317062Sbloom #include "tcp_seq.h"
3417062Sbloom #include "tcp_timer.h"
3517062Sbloom #include "tcp_var.h"
3617062Sbloom #include "tcpip.h"
3717062Sbloom #include "tcp_debug.h"
384601Swnj 
395300Sroot int	tcpprintfs = 0;
404679Swnj int	tcpcksum = 1;
4132098Skarels int	tcprexmtthresh = 3;
425267Sroot struct	tcpiphdr tcp_saveti;
435440Swnj extern	tcpnodelack;
444601Swnj 
455267Sroot struct	tcpcb *tcp_newtcpcb();
4624816Skarels 
475065Swnj /*
4824816Skarels  * Insert segment ti into reassembly queue of tcp with
4924816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
5024816Skarels  * a segment with FIN.  The macro form does the common case inline
5124816Skarels  * (segment is the next to be received on an established connection,
5224816Skarels  * and the queue is empty), avoiding linkage into and removal
5324816Skarels  * from the queue and repetition of various conversions.
5424816Skarels  */
5524816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
5624816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
5724816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
5824816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
5924816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
6024816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
6130525Skarels 		tcpstat.tcps_rcvpack++;\
6230525Skarels 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
6324816Skarels 		sbappend(&(so)->so_rcv, (m)); \
6424816Skarels 		sorwakeup(so); \
6524816Skarels 	} else \
6624816Skarels 		(flags) = tcp_reass((tp), (ti)); \
6724816Skarels }
6824816Skarels 
6924816Skarels tcp_reass(tp, ti)
7024816Skarels 	register struct tcpcb *tp;
7124816Skarels 	register struct tcpiphdr *ti;
7224816Skarels {
7324816Skarels 	register struct tcpiphdr *q;
7424816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
7524816Skarels 	struct mbuf *m;
7624816Skarels 	int flags;
7724816Skarels 
7824816Skarels 	/*
7924816Skarels 	 * Call with ti==0 after become established to
8024816Skarels 	 * force pre-ESTABLISHED data up to user socket.
8124816Skarels 	 */
8224816Skarels 	if (ti == 0)
8324816Skarels 		goto present;
8424816Skarels 
8524816Skarels 	/*
8624816Skarels 	 * Find a segment which begins after this one does.
8724816Skarels 	 */
8824816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8924816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
9024816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
9124816Skarels 			break;
9224816Skarels 
9324816Skarels 	/*
9424816Skarels 	 * If there is a preceding segment, it may provide some of
9524816Skarels 	 * our data already.  If so, drop the data from the incoming
9624816Skarels 	 * segment.  If it provides all of our data, drop us.
9724816Skarels 	 */
9824816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
9924816Skarels 		register int i;
10024816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
10124816Skarels 		/* conversion to int (in i) handles seq wraparound */
10224816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
10324816Skarels 		if (i > 0) {
10430525Skarels 			if (i >= ti->ti_len) {
10530525Skarels 				tcpstat.tcps_rcvduppack++;
10630525Skarels 				tcpstat.tcps_rcvdupbyte += ti->ti_len;
10724816Skarels 				goto drop;
10830525Skarels 			}
10924816Skarels 			m_adj(dtom(ti), i);
11024816Skarels 			ti->ti_len -= i;
11124816Skarels 			ti->ti_seq += i;
11224816Skarels 		}
11324816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
11424816Skarels 	}
11530525Skarels 	tcpstat.tcps_rcvoopack++;
11630525Skarels 	tcpstat.tcps_rcvoobyte += ti->ti_len;
11724816Skarels 
11824816Skarels 	/*
11924816Skarels 	 * While we overlap succeeding segments trim them or,
12024816Skarels 	 * if they are completely covered, dequeue them.
12124816Skarels 	 */
12224816Skarels 	while (q != (struct tcpiphdr *)tp) {
12324816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
12424816Skarels 		if (i <= 0)
12524816Skarels 			break;
12624816Skarels 		if (i < q->ti_len) {
12724816Skarels 			q->ti_seq += i;
12824816Skarels 			q->ti_len -= i;
12924816Skarels 			m_adj(dtom(q), i);
13024816Skarels 			break;
13124816Skarels 		}
13224816Skarels 		q = (struct tcpiphdr *)q->ti_next;
13324816Skarels 		m = dtom(q->ti_prev);
13424816Skarels 		remque(q->ti_prev);
13524816Skarels 		m_freem(m);
13624816Skarels 	}
13724816Skarels 
13824816Skarels 	/*
13924816Skarels 	 * Stick new segment in its place.
14024816Skarels 	 */
14124816Skarels 	insque(ti, q->ti_prev);
14224816Skarels 
14324816Skarels present:
14424816Skarels 	/*
14524816Skarels 	 * Present data to user, advancing rcv_nxt through
14624816Skarels 	 * completed sequence space.
14724816Skarels 	 */
14824816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
14924816Skarels 		return (0);
15024816Skarels 	ti = tp->seg_next;
15124816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
15224816Skarels 		return (0);
15324816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
15424816Skarels 		return (0);
15524816Skarels 	do {
15624816Skarels 		tp->rcv_nxt += ti->ti_len;
15724816Skarels 		flags = ti->ti_flags & TH_FIN;
15824816Skarels 		remque(ti);
15924816Skarels 		m = dtom(ti);
16024816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
16124816Skarels 		if (so->so_state & SS_CANTRCVMORE)
16224816Skarels 			m_freem(m);
16324816Skarels 		else
16424816Skarels 			sbappend(&so->so_rcv, m);
16524816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
16624816Skarels 	sorwakeup(so);
16724816Skarels 	return (flags);
16824816Skarels drop:
16924816Skarels 	m_freem(dtom(ti));
17024816Skarels 	return (0);
17124816Skarels }
17224816Skarels 
17324816Skarels /*
1745065Swnj  * TCP input routine, follows pages 65-76 of the
1755065Swnj  * protocol specification dated September, 1981 very closely.
1765065Swnj  */
1774924Swnj tcp_input(m0)
1784924Swnj 	struct mbuf *m0;
1794601Swnj {
1804924Swnj 	register struct tcpiphdr *ti;
1814924Swnj 	struct inpcb *inp;
1824924Swnj 	register struct mbuf *m;
1835440Swnj 	struct mbuf *om = 0;
1844924Swnj 	int len, tlen, off;
1855391Swnj 	register struct tcpcb *tp = 0;
1864924Swnj 	register int tiflags;
1874803Swnj 	struct socket *so;
18831721Skarels 	int todrop, acked, ourfinisacked, needoutput = 0;
1895267Sroot 	short ostate;
1906028Sroot 	struct in_addr laddr;
19110769Ssam 	int dropsocket = 0;
19230525Skarels 	int iss = 0;
1934924Swnj 
19430525Skarels 	tcpstat.tcps_rcvtotal++;
1954924Swnj 	/*
1965244Sroot 	 * Get IP and TCP header together in first mbuf.
1975244Sroot 	 * Note: IP leaves IP header in first mbuf.
1984924Swnj 	 */
1994924Swnj 	m = m0;
2005020Sroot 	ti = mtod(m, struct tcpiphdr *);
2015244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
2025208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
2035307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
2045307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
20530525Skarels 			tcpstat.tcps_rcvshort++;
2065307Sroot 			return;
2075085Swnj 		}
2085085Swnj 		ti = mtod(m, struct tcpiphdr *);
2095085Swnj 	}
2104601Swnj 
2114601Swnj 	/*
2125244Sroot 	 * Checksum extended TCP header and data.
2134601Swnj 	 */
2144924Swnj 	tlen = ((struct ip *)ti)->ip_len;
2154924Swnj 	len = sizeof (struct ip) + tlen;
2164679Swnj 	if (tcpcksum) {
2174924Swnj 		ti->ti_next = ti->ti_prev = 0;
2184924Swnj 		ti->ti_x1 = 0;
2195223Swnj 		ti->ti_len = (u_short)tlen;
2206161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
2215231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
22211830Ssam 			if (tcpprintfs)
22311830Ssam 				printf("tcp sum: src %x\n", ti->ti_src);
22430525Skarels 			tcpstat.tcps_rcvbadsum++;
2255085Swnj 			goto drop;
2264601Swnj 		}
2274601Swnj 	}
2284601Swnj 
2294601Swnj 	/*
2305244Sroot 	 * Check that TCP offset makes sense,
2315440Swnj 	 * pull out TCP options and adjust length.
2324601Swnj 	 */
2334924Swnj 	off = ti->ti_off << 2;
2345231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
23511830Ssam 		if (tcpprintfs)
23611830Ssam 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
23730525Skarels 		tcpstat.tcps_rcvbadoff++;
2385085Swnj 		goto drop;
2394924Swnj 	}
2406211Swnj 	tlen -= off;
2416211Swnj 	ti->ti_len = tlen;
2425440Swnj 	if (off > sizeof (struct tcphdr)) {
24324816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
24424816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
24530525Skarels 				tcpstat.tcps_rcvshort++;
24624816Skarels 				return;
24724816Skarels 			}
24824816Skarels 			ti = mtod(m, struct tcpiphdr *);
2495440Swnj 		}
2509642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2515440Swnj 		if (om == 0)
2525440Swnj 			goto drop;
2535440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2545440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2556161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2565440Swnj 		  m->m_len -= om->m_len;
2576161Ssam 		  bcopy(op+om->m_len, op,
2586161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2595440Swnj 		}
2605440Swnj 	}
2615065Swnj 	tiflags = ti->ti_flags;
2624924Swnj 
2636093Sroot 	/*
26425729Smckusick 	 * Drop TCP and IP headers; TCP options were dropped above.
2656093Sroot 	 */
26625729Smckusick 	m->m_off += sizeof(struct tcpiphdr);
26725729Smckusick 	m->m_len -= sizeof(struct tcpiphdr);
2686093Sroot 
2694924Swnj 	/*
2705244Sroot 	 * Convert TCP protocol specific fields to host format.
2715085Swnj 	 */
2725085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
2735085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
2745085Swnj 	ti->ti_win = ntohs(ti->ti_win);
2755085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
2765085Swnj 
2775085Swnj 	/*
2788271Sroot 	 * Locate pcb for segment.
2794924Swnj 	 */
28030525Skarels findpcb:
2815065Swnj 	inp = in_pcblookup
2826028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
2836028Sroot 		INPLOOKUP_WILDCARD);
2845065Swnj 
2855065Swnj 	/*
2865065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2875244Sroot 	 * all data in the incoming segment is discarded.
28832098Skarels 	 * If the TCB exists but is in CLOSED state, it is embryonic,
28932098Skarels 	 * but should either do a listen or a connect soon.
2905065Swnj 	 */
2915300Sroot 	if (inp == 0)
2925085Swnj 		goto dropwithreset;
2935065Swnj 	tp = intotcpcb(inp);
2945300Sroot 	if (tp == 0)
2955085Swnj 		goto dropwithreset;
29632098Skarels 	if (tp->t_state == TCPS_CLOSED)
29732098Skarels 		goto drop;
2985109Swnj 	so = inp->inp_socket;
2995267Sroot 	if (so->so_options & SO_DEBUG) {
3005267Sroot 		ostate = tp->t_state;
3015267Sroot 		tcp_saveti = *ti;
3025267Sroot 	}
3037510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
3047510Sroot 		so = sonewconn(so);
3057510Sroot 		if (so == 0)
3067510Sroot 			goto drop;
30710769Ssam 		/*
30810769Ssam 		 * This is ugly, but ....
30910769Ssam 		 *
31010769Ssam 		 * Mark socket as temporary until we're
31110769Ssam 		 * committed to keeping it.  The code at
31210769Ssam 		 * ``drop'' and ``dropwithreset'' check the
31310769Ssam 		 * flag dropsocket to see if the temporary
31410769Ssam 		 * socket created here should be discarded.
31510769Ssam 		 * We mark the socket as discardable until
31610769Ssam 		 * we're committed to it below in TCPS_LISTEN.
31710769Ssam 		 */
31810769Ssam 		dropsocket++;
3197510Sroot 		inp = (struct inpcb *)so->so_pcb;
3207510Sroot 		inp->inp_laddr = ti->ti_dst;
3217510Sroot 		inp->inp_lport = ti->ti_dport;
32224816Skarels 		inp->inp_options = ip_srcroute();
3237510Sroot 		tp = intotcpcb(inp);
3247510Sroot 		tp->t_state = TCPS_LISTEN;
3257510Sroot 	}
3264601Swnj 
3274601Swnj 	/*
3285162Swnj 	 * Segment received on connection.
3295162Swnj 	 * Reset idle time and keep-alive timer.
3305162Swnj 	 */
3315162Swnj 	tp->t_idle = 0;
332*33745Skarels 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
3335162Swnj 
3345162Swnj 	/*
33517272Skarels 	 * Process options if not in LISTEN state,
33617272Skarels 	 * else do it below (after getting remote address).
3375440Swnj 	 */
33817272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
33917272Skarels 		tcp_dooptions(tp, om, ti);
3405440Swnj 		om = 0;
3415440Swnj 	}
3425440Swnj 
3435440Swnj 	/*
3445085Swnj 	 * Calculate amount of space in receive window,
3455085Swnj 	 * and then do TCP input processing.
34624816Skarels 	 * Receive window is amount of space in rcv queue,
34724816Skarels 	 * but not less than advertised window.
3484601Swnj 	 */
34925939Skarels 	{ int win;
3504601Swnj 
35125939Skarels 	win = sbspace(&so->so_rcv);
35225939Skarels 	if (win < 0)
35325939Skarels 		win = 0;
35425939Skarels 	tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
35525939Skarels 	}
35625939Skarels 
3574601Swnj 	switch (tp->t_state) {
3584601Swnj 
3595065Swnj 	/*
3605065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3615065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3625065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
36325197Skarels 	 * Don't bother responding if the destination was a broadcast.
3645085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3655065Swnj 	 * tp->iss, and send a segment:
3665085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3675065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3685065Swnj 	 * Fill in remote peer address fields if not previously specified.
3695065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3705244Sroot 	 * segment in this state.
3715065Swnj 	 */
3728271Sroot 	case TCPS_LISTEN: {
37310145Ssam 		struct mbuf *am;
3748271Sroot 		register struct sockaddr_in *sin;
3758271Sroot 
3765065Swnj 		if (tiflags & TH_RST)
3775065Swnj 			goto drop;
3785300Sroot 		if (tiflags & TH_ACK)
3795085Swnj 			goto dropwithreset;
3805300Sroot 		if ((tiflags & TH_SYN) == 0)
3815065Swnj 			goto drop;
38225197Skarels 		if (in_broadcast(ti->ti_dst))
38325197Skarels 			goto drop;
38410145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
38510145Ssam 		if (am == NULL)
38610145Ssam 			goto drop;
38710145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3888599Sroot 		sin = mtod(am, struct sockaddr_in *);
3898271Sroot 		sin->sin_family = AF_INET;
3908271Sroot 		sin->sin_addr = ti->ti_src;
3918271Sroot 		sin->sin_port = ti->ti_sport;
3926028Sroot 		laddr = inp->inp_laddr;
39310145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3946028Sroot 			inp->inp_laddr = ti->ti_dst;
3958599Sroot 		if (in_pcbconnect(inp, am)) {
3966028Sroot 			inp->inp_laddr = laddr;
3978716Sroot 			(void) m_free(am);
3985244Sroot 			goto drop;
3996028Sroot 		}
4008716Sroot 		(void) m_free(am);
4015244Sroot 		tp->t_template = tcp_template(tp);
4025244Sroot 		if (tp->t_template == 0) {
40326386Skarels 			tp = tcp_drop(tp, ENOBUFS);
40417264Skarels 			dropsocket = 0;		/* socket is already gone */
4055244Sroot 			goto drop;
4065244Sroot 		}
40717272Skarels 		if (om) {
40817272Skarels 			tcp_dooptions(tp, om, ti);
40917272Skarels 			om = 0;
41017272Skarels 		}
41130525Skarels 		if (iss)
41230525Skarels 			tp->iss = iss;
41330525Skarels 		else
41430525Skarels 			tp->iss = tcp_iss;
41530525Skarels 		tcp_iss += TCP_ISSINCR/2;
4165065Swnj 		tp->irs = ti->ti_seq;
4175085Swnj 		tcp_sendseqinit(tp);
4185085Swnj 		tcp_rcvseqinit(tp);
41925939Skarels 		tp->t_flags |= TF_ACKNOW;
4205065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
421*33745Skarels 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
42210769Ssam 		dropsocket = 0;		/* committed to socket */
42330525Skarels 		tcpstat.tcps_accepts++;
4245085Swnj 		goto trimthenstep6;
4258271Sroot 		}
4264601Swnj 
4275065Swnj 	/*
4285065Swnj 	 * If the state is SYN_SENT:
4295065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
4305065Swnj 	 *	if seg contains a RST, then drop the connection.
4315065Swnj 	 *	if seg does not contain SYN, then drop it.
4325065Swnj 	 * Otherwise this is an acceptable SYN segment
4335065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4345065Swnj 	 *	if seg contains ack then advance tp->snd_una
4355065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4365065Swnj 	 *	arrange for segment to be acked (eventually)
4375065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4385065Swnj 	 */
4395065Swnj 	case TCPS_SYN_SENT:
4405065Swnj 		if ((tiflags & TH_ACK) &&
44124816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4425231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4435085Swnj 			goto dropwithreset;
4445065Swnj 		if (tiflags & TH_RST) {
44510394Ssam 			if (tiflags & TH_ACK)
44610394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4475065Swnj 			goto drop;
4484601Swnj 		}
4495065Swnj 		if ((tiflags & TH_SYN) == 0)
4505065Swnj 			goto drop;
45130974Skarels 		if (tiflags & TH_ACK) {
45230974Skarels 			tp->snd_una = ti->ti_ack;
45330974Skarels 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
45430974Skarels 				tp->snd_nxt = tp->snd_una;
45530974Skarels 		}
4565244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4575065Swnj 		tp->irs = ti->ti_seq;
4585085Swnj 		tcp_rcvseqinit(tp);
4595085Swnj 		tp->t_flags |= TF_ACKNOW;
46030974Skarels 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
46130525Skarels 			tcpstat.tcps_connects++;
4625244Sroot 			soisconnected(so);
4635065Swnj 			tp->t_state = TCPS_ESTABLISHED;
46417272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4655162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
46632098Skarels 			/*
46732098Skarels 			 * if we didn't have to retransmit the SYN,
46832098Skarels 			 * use its rtt as our initial srtt & rtt var.
46932098Skarels 			 */
47032098Skarels 			if (tp->t_rtt) {
47132098Skarels 				tp->t_srtt = tp->t_rtt << 3;
47232098Skarels 				tp->t_rttvar = tp->t_rtt << 1;
47332098Skarels 				TCPT_RANGESET(tp->t_rxtcur,
47432098Skarels 				    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
47532098Skarels 				    TCPTV_MIN, TCPTV_REXMTMAX);
47632098Skarels 				tp->t_rtt = 0;
47732098Skarels 			}
4785162Swnj 		} else
4795085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4805085Swnj 
4815085Swnj trimthenstep6:
4825085Swnj 		/*
4835231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4845085Swnj 		 * If data, trim to stay within window,
4855085Swnj 		 * dropping FIN if necessary.
4865085Swnj 		 */
4875231Swnj 		ti->ti_seq++;
4885085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4895085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4905085Swnj 			m_adj(m, -todrop);
4915085Swnj 			ti->ti_len = tp->rcv_wnd;
49225939Skarels 			tiflags &= ~TH_FIN;
49330525Skarels 			tcpstat.tcps_rcvpackafterwin++;
49430525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
4955065Swnj 		}
4965263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
49725939Skarels 		tp->rcv_up = ti->ti_seq;
4985085Swnj 		goto step6;
4995065Swnj 	}
5004601Swnj 
5015065Swnj 	/*
50230525Skarels 	 * States other than LISTEN or SYN_SENT.
50330525Skarels 	 * First check that at least some bytes of segment are within
50430525Skarels 	 * receive window.  If segment begins before rcv_nxt,
50530525Skarels 	 * drop leading data (and SYN); if nothing left, just ack.
50616222Skarels 	 */
50730525Skarels 	todrop = tp->rcv_nxt - ti->ti_seq;
50830525Skarels 	if (todrop > 0) {
50930525Skarels 		if (tiflags & TH_SYN) {
51030525Skarels 			tiflags &= ~TH_SYN;
51130525Skarels 			ti->ti_seq++;
51230525Skarels 			if (ti->ti_urp > 1)
51330525Skarels 				ti->ti_urp--;
51430525Skarels 			else
51530525Skarels 				tiflags &= ~TH_URG;
51630525Skarels 			todrop--;
51730525Skarels 		}
51830525Skarels 		if (todrop > ti->ti_len ||
51930525Skarels 		    todrop == ti->ti_len && (tiflags&TH_FIN) == 0) {
520*33745Skarels 			tcpstat.tcps_rcvduppack++;
521*33745Skarels 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
52231730Skarels 			/*
523*33745Skarels 			 * If segment is just one to the left of the window,
524*33745Skarels 			 * check two special cases:
525*33745Skarels 			 * 1. Don't toss RST in response to 4.2-style keepalive.
526*33745Skarels 			 * 2. If the only thing to drop is a FIN, we can drop
527*33745Skarels 			 *    it, but check the ACK or we will get into FIN
528*33745Skarels 			 *    wars if our FINs crossed (both CLOSING).
529*33745Skarels 			 * In either case, send ACK to resynchronize,
530*33745Skarels 			 * but keep on processing for RST or ACK.
53131730Skarels 			 */
532*33745Skarels 			if ((tiflags & TH_FIN && todrop == ti->ti_len + 1)
533*33745Skarels #ifdef TCP_COMPAT_42
534*33745Skarels 			  || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1)
53531730Skarels #endif
536*33745Skarels 			   ) {
537*33745Skarels 				todrop = ti->ti_len;
538*33745Skarels 				tiflags &= ~TH_FIN;
539*33745Skarels 				tp->t_flags |= TF_ACKNOW;
540*33745Skarels 			} else
541*33745Skarels 				goto dropafterack;
54232034Skarels 		} else {
54332034Skarels 			tcpstat.tcps_rcvpartduppack++;
54432034Skarels 			tcpstat.tcps_rcvpartdupbyte += todrop;
54530525Skarels 		}
54630525Skarels 		m_adj(m, todrop);
54730525Skarels 		ti->ti_seq += todrop;
54830525Skarels 		ti->ti_len -= todrop;
54930525Skarels 		if (ti->ti_urp > todrop)
55030525Skarels 			ti->ti_urp -= todrop;
55130525Skarels 		else {
55230525Skarels 			tiflags &= ~TH_URG;
55330525Skarels 			ti->ti_urp = 0;
55430525Skarels 		}
55516222Skarels 	}
55616222Skarels 
55732612Skarels 	/*
558*33745Skarels 	 * If new data are received on a connection after the
55932612Skarels 	 * user processes are gone, then RST the other end.
56032612Skarels 	 */
56132612Skarels 	if ((so->so_state & SS_NOFDREF) &&
56232612Skarels 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
56332612Skarels 		tp = tcp_close(tp);
56432612Skarels 		tcpstat.tcps_rcvafterclose++;
56532612Skarels 		goto dropwithreset;
56632612Skarels 	}
56732612Skarels 
56833445Skarels 	/*
56933445Skarels 	 * If segment ends after window, drop trailing data
57033445Skarels 	 * (and PUSH and FIN); if nothing left, just ACK.
57133445Skarels 	 */
57233445Skarels 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
57333445Skarels 	if (todrop > 0) {
57433445Skarels 		tcpstat.tcps_rcvpackafterwin++;
57533445Skarels 		if (todrop >= ti->ti_len) {
57630525Skarels 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
57733445Skarels 			/*
57833445Skarels 			 * If a new connection request is received
57933445Skarels 			 * while in TIME_WAIT, drop the old connection
58033445Skarels 			 * and start over if the sequence numbers
58133445Skarels 			 * are above the previous ones.
58233445Skarels 			 */
58333445Skarels 			if (tiflags & TH_SYN &&
58433445Skarels 			    tp->t_state == TCPS_TIME_WAIT &&
58533445Skarels 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
58633445Skarels 				iss = tp->rcv_nxt + TCP_ISSINCR;
58733445Skarels 				(void) tcp_close(tp);
58833445Skarels 				goto findpcb;
58933445Skarels 			}
59033445Skarels 			/*
59133445Skarels 			 * If window is closed can only take segments at
59233445Skarels 			 * window edge, and have to drop data and PUSH from
59333445Skarels 			 * incoming segments.  Continue processing, but
59433445Skarels 			 * remember to ack.  Otherwise, drop segment
59533445Skarels 			 * and ack.
59633445Skarels 			 */
59733445Skarels 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
59833445Skarels 				tp->t_flags |= TF_ACKNOW;
59930525Skarels 				tcpstat.tcps_rcvwinprobe++;
60033445Skarels 			} else
6015065Swnj 				goto dropafterack;
60233445Skarels 		} else
60330525Skarels 			tcpstat.tcps_rcvbyteafterwin += todrop;
60433445Skarels 		m_adj(m, -todrop);
60533445Skarels 		ti->ti_len -= todrop;
60633445Skarels 		tiflags &= ~(TH_PUSH|TH_FIN);
6075065Swnj 	}
6084601Swnj 
6095065Swnj 	/*
6105065Swnj 	 * If the RST bit is set examine the state:
6115065Swnj 	 *    SYN_RECEIVED STATE:
6125065Swnj 	 *	If passive open, return to LISTEN state.
6135065Swnj 	 *	If active open, inform user that connection was refused.
6145065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
6155065Swnj 	 *	Inform user that connection was reset, and close tcb.
6165065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
6175065Swnj 	 *	Close the tcb.
6185065Swnj 	 */
6195065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
6205267Sroot 
6215065Swnj 	case TCPS_SYN_RECEIVED:
622*33745Skarels 		so->so_error = ECONNREFUSED;
623*33745Skarels 		goto close;
6244601Swnj 
6255065Swnj 	case TCPS_ESTABLISHED:
6265065Swnj 	case TCPS_FIN_WAIT_1:
6275065Swnj 	case TCPS_FIN_WAIT_2:
6285065Swnj 	case TCPS_CLOSE_WAIT:
629*33745Skarels 		so->so_error = ECONNRESET;
630*33745Skarels 	close:
631*33745Skarels 		tp->t_state = TCPS_CLOSED;
632*33745Skarels 		tcpstat.tcps_drops++;
633*33745Skarels 		tp = tcp_close(tp);
6345065Swnj 		goto drop;
6355065Swnj 
6365065Swnj 	case TCPS_CLOSING:
6375065Swnj 	case TCPS_LAST_ACK:
6385065Swnj 	case TCPS_TIME_WAIT:
63910394Ssam 		tp = tcp_close(tp);
6405065Swnj 		goto drop;
6414601Swnj 	}
6424601Swnj 
6434601Swnj 	/*
6445065Swnj 	 * If a SYN is in the window, then this is an
6455065Swnj 	 * error and we send an RST and drop the connection.
6464601Swnj 	 */
6475065Swnj 	if (tiflags & TH_SYN) {
64810394Ssam 		tp = tcp_drop(tp, ECONNRESET);
6495085Swnj 		goto dropwithreset;
6504601Swnj 	}
6514601Swnj 
6524601Swnj 	/*
6535065Swnj 	 * If the ACK bit is off we drop the segment and return.
6544601Swnj 	 */
6555085Swnj 	if ((tiflags & TH_ACK) == 0)
6565065Swnj 		goto drop;
6575065Swnj 
6585065Swnj 	/*
6595065Swnj 	 * Ack processing.
6605065Swnj 	 */
6614601Swnj 	switch (tp->t_state) {
6624601Swnj 
6635065Swnj 	/*
6645065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
66531721Skarels 	 * ESTABLISHED state and continue processing, otherwise
6665065Swnj 	 * send an RST.
6675065Swnj 	 */
6685065Swnj 	case TCPS_SYN_RECEIVED:
6695085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
6705231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
6715085Swnj 			goto dropwithreset;
67230525Skarels 		tcpstat.tcps_connects++;
6735085Swnj 		soisconnected(so);
6745085Swnj 		tp->t_state = TCPS_ESTABLISHED;
67517272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
6765162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
6775244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
6785085Swnj 		/* fall into ... */
6794601Swnj 
6805065Swnj 	/*
6815065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
6825065Swnj 	 * ACKs.  If the ack is in the range
6835231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6845065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6855065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6865065Swnj 	 * more up to date window information we update our window information.
6875065Swnj 	 */
6885065Swnj 	case TCPS_ESTABLISHED:
6895065Swnj 	case TCPS_FIN_WAIT_1:
6905065Swnj 	case TCPS_FIN_WAIT_2:
6915065Swnj 	case TCPS_CLOSE_WAIT:
6925065Swnj 	case TCPS_CLOSING:
6935244Sroot 	case TCPS_LAST_ACK:
6945244Sroot 	case TCPS_TIME_WAIT:
6955085Swnj 
69630525Skarels 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
69732098Skarels 			if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) {
69830525Skarels 				tcpstat.tcps_rcvdupack++;
69932098Skarels 				/*
70032098Skarels 				 * If we have outstanding data (not a
70132098Skarels 				 * window probe), this is a completely
70232098Skarels 				 * duplicate ack (ie, window info didn't
70332098Skarels 				 * change), the ack is the biggest we've
70432098Skarels 				 * seen and we've seen exactly our rexmt
70532098Skarels 				 * threshhold of them, assume a packet
70632098Skarels 				 * has been dropped and retransmit it.
70732098Skarels 				 * Kludge snd_nxt & the congestion
70832098Skarels 				 * window so we send only this one
70932376Skarels 				 * packet.  If this packet fills the
71032376Skarels 				 * only hole in the receiver's seq.
71132376Skarels 				 * space, the next real ack will fully
71232376Skarels 				 * open our window.  This means we
71332376Skarels 				 * have to do the usual slow-start to
71432376Skarels 				 * not overwhelm an intermediate gateway
71532376Skarels 				 * with a burst of packets.  Leave
71632376Skarels 				 * here with the congestion window set
71732376Skarels 				 * to allow 2 packets on the next real
71832376Skarels 				 * ack and the exp-to-linear thresh
71932376Skarels 				 * set for half the current window
72032376Skarels 				 * size (since we know we're losing at
72132376Skarels 				 * the current window size).
72232098Skarels 				 */
72332098Skarels 				if (tp->t_timer[TCPT_REXMT] == 0 ||
72432098Skarels 				    ti->ti_ack != tp->snd_una)
72532098Skarels 					tp->t_dupacks = 0;
72632098Skarels 				else if (++tp->t_dupacks == tcprexmtthresh) {
72732098Skarels 					tcp_seq onxt = tp->snd_nxt;
72832376Skarels 					u_int win =
72932376Skarels 					    MIN(tp->snd_wnd, tp->snd_cwnd) / 2 /
73032376Skarels 						tp->t_maxseg;
73132098Skarels 
73232376Skarels 					if (win < 2)
73332376Skarels 						win = 2;
73432376Skarels 					tp->snd_ssthresh = win * tp->t_maxseg;
73532376Skarels 
73632098Skarels 					tp->t_timer[TCPT_REXMT] = 0;
73732098Skarels 					tp->t_rtt = 0;
73832098Skarels 					tp->snd_nxt = ti->ti_ack;
73932098Skarels 					tp->snd_cwnd = tp->t_maxseg;
74032098Skarels 					(void) tcp_output(tp);
74132098Skarels 
74232098Skarels 					if (SEQ_GT(onxt, tp->snd_nxt))
74332098Skarels 						tp->snd_nxt = onxt;
74432098Skarels 					goto drop;
74532098Skarels 				}
74632098Skarels 			} else
74732098Skarels 				tp->t_dupacks = 0;
7485065Swnj 			break;
74930525Skarels 		}
75032098Skarels 		tp->t_dupacks = 0;
75130525Skarels 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
75230525Skarels 			tcpstat.tcps_rcvacktoomuch++;
7535065Swnj 			goto dropafterack;
75430525Skarels 		}
7555085Swnj 		acked = ti->ti_ack - tp->snd_una;
75630525Skarels 		tcpstat.tcps_rcvackpack++;
75730525Skarels 		tcpstat.tcps_rcvackbyte += acked;
7585951Swnj 
7595951Swnj 		/*
7605951Swnj 		 * If transmit timer is running and timed sequence
7615951Swnj 		 * number was acked, update smoothed round trip time.
76232034Skarels 		 * Since we now have an rtt measurement, cancel the
76332034Skarels 		 * timer backoff (cf., Phil Karn's retransmit alg.).
76432034Skarels 		 * Recompute the initial retransmit timer.
7655951Swnj 		 */
7665951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
76730525Skarels 			tcpstat.tcps_rttupdated++;
76831726Skarels 			if (tp->t_srtt != 0) {
76931726Skarels 				register short delta;
77031726Skarels 
77131726Skarels 				/*
77231726Skarels 				 * srtt is stored as fixed point with 3 bits
77331726Skarels 				 * after the binary point (i.e., scaled by 8).
77431726Skarels 				 * The following magic is equivalent
77531726Skarels 				 * to the smoothing algorithm in rfc793
77631726Skarels 				 * with an alpha of .875
77731726Skarels 				 * (srtt = rtt/8 + srtt*7/8 in fixed point).
77832098Skarels 				 * Adjust t_rtt to origin 0.
77931726Skarels 				 */
780*33745Skarels 				delta = tp->t_rtt - 1 - (tp->t_srtt >> 3);
78131726Skarels 				if ((tp->t_srtt += delta) <= 0)
78231726Skarels 					tp->t_srtt = 1;
78331726Skarels 				/*
78432034Skarels 				 * We accumulate a smoothed rtt variance
78532034Skarels 				 * (actually, a smoothed mean difference),
78631726Skarels 				 * then set the retransmit timer to smoothed
78731726Skarels 				 * rtt + 2 times the smoothed variance.
78832098Skarels 				 * rttvar is stored as fixed point
78931726Skarels 				 * with 2 bits after the binary point
79031726Skarels 				 * (scaled by 4).  The following is equivalent
79131726Skarels 				 * to rfc793 smoothing with an alpha of .75
79231726Skarels 				 * (rttvar = rttvar*3/4 + |delta| / 4).
79331726Skarels 				 * This replaces rfc793's wired-in beta.
79431726Skarels 				 */
79531726Skarels 				if (delta < 0)
79631726Skarels 					delta = -delta;
79731726Skarels 				delta -= (tp->t_rttvar >> 2);
79831726Skarels 				if ((tp->t_rttvar += delta) <= 0)
79931726Skarels 					tp->t_rttvar = 1;
80031726Skarels 			} else {
80131726Skarels 				/*
80231726Skarels 				 * No rtt measurement yet - use the
80331726Skarels 				 * unsmoothed rtt.  Set the variance
80431726Skarels 				 * to half the rtt (so our first
80531726Skarels 				 * retransmit happens at 2*rtt)
80631726Skarels 				 */
80731726Skarels 				tp->t_srtt = tp->t_rtt << 3;
80831726Skarels 				tp->t_rttvar = tp->t_rtt << 1;
80931726Skarels 			}
8105951Swnj 			tp->t_rtt = 0;
81132034Skarels 			tp->t_rxtshift = 0;
81232034Skarels 			TCPT_RANGESET(tp->t_rxtcur,
81332034Skarels 			    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
81432034Skarels 			    TCPTV_MIN, TCPTV_REXMTMAX);
8155951Swnj 		}
8165951Swnj 
81726824Skarels 		/*
81826824Skarels 		 * If all outstanding data is acked, stop retransmit
81926824Skarels 		 * timer and remember to restart (more output or persist).
82026824Skarels 		 * If there is more data to be acked, restart retransmit
82132034Skarels 		 * timer, using current (possibly backed-off) value.
82226824Skarels 		 */
82326824Skarels 		if (ti->ti_ack == tp->snd_max) {
8245244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
82526824Skarels 			needoutput = 1;
82632034Skarels 		} else if (tp->t_timer[TCPT_PERSIST] == 0)
82732034Skarels 			tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
82817360Skarels 		/*
82932098Skarels 		 * When new data is acked, open the congestion window.
83032098Skarels 		 * If the window gives us less than ssthresh packets
83132098Skarels 		 * in flight, open exponentially (maxseg per packet).
83232098Skarels 		 * Otherwise open linearly (maxseg per window,
83332098Skarels 		 * or maxseg^2 / cwnd per packet).
83417360Skarels 		 */
83532098Skarels 		{
83632098Skarels 		u_int incr = tp->t_maxseg;
83732098Skarels 
83832098Skarels 		if (tp->snd_cwnd > tp->snd_ssthresh)
83932098Skarels 			incr = MAX(incr * incr / tp->snd_cwnd, 1);
84032098Skarels 
84133603Skarels 		tp->snd_cwnd = MIN(tp->snd_cwnd + incr, IP_MAXPACKET); /* XXX */
84232098Skarels 		}
8435307Sroot 		if (acked > so->so_snd.sb_cc) {
84415386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
84526386Skarels 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
84631721Skarels 			ourfinisacked = 1;
8475307Sroot 		} else {
8486161Ssam 			sbdrop(&so->so_snd, acked);
8495307Sroot 			tp->snd_wnd -= acked;
85031721Skarels 			ourfinisacked = 0;
8515307Sroot 		}
8526434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
8535300Sroot 			sowwakeup(so);
8545231Swnj 		tp->snd_una = ti->ti_ack;
8555357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
8565357Sroot 			tp->snd_nxt = tp->snd_una;
8575162Swnj 
8584601Swnj 		switch (tp->t_state) {
8594601Swnj 
8605065Swnj 		/*
8615065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
8625065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
8635085Swnj 		 * then enter FIN_WAIT_2.
8645065Swnj 		 */
8655065Swnj 		case TCPS_FIN_WAIT_1:
8665896Swnj 			if (ourfinisacked) {
8675896Swnj 				/*
8685896Swnj 				 * If we can't receive any more
8695896Swnj 				 * data, then closing user can proceed.
87024816Skarels 				 * Starting the timer is contrary to the
87124816Skarels 				 * specification, but if we don't get a FIN
87224816Skarels 				 * we'll hang forever.
8735896Swnj 				 */
87424816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
8755896Swnj 					soisdisconnected(so);
876*33745Skarels 					tp->t_timer[TCPT_2MSL] = tcp_maxidle;
87724816Skarels 				}
8785085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
8795896Swnj 			}
8804601Swnj 			break;
8814601Swnj 
8825065Swnj 	 	/*
8835065Swnj 		 * In CLOSING STATE in addition to the processing for
8845065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
8855065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
8865065Swnj 		 * the segment.
8875065Swnj 		 */
8885065Swnj 		case TCPS_CLOSING:
8895244Sroot 			if (ourfinisacked) {
8905065Swnj 				tp->t_state = TCPS_TIME_WAIT;
8915244Sroot 				tcp_canceltimers(tp);
8925244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8935244Sroot 				soisdisconnected(so);
8945244Sroot 			}
8955244Sroot 			break;
8964601Swnj 
8975065Swnj 		/*
89831743Skarels 		 * In LAST_ACK, we may still be waiting for data to drain
89931743Skarels 		 * and/or to be acked, as well as for the ack of our FIN.
90031743Skarels 		 * If our FIN is now acknowledged, delete the TCB,
90131743Skarels 		 * enter the closed state and return.
9025065Swnj 		 */
9035065Swnj 		case TCPS_LAST_ACK:
90431743Skarels 			if (ourfinisacked) {
90510394Ssam 				tp = tcp_close(tp);
90631743Skarels 				goto drop;
90731743Skarels 			}
90831743Skarels 			break;
9094601Swnj 
9105065Swnj 		/*
9115065Swnj 		 * In TIME_WAIT state the only thing that should arrive
9125065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
9135065Swnj 		 * it and restart the finack timer.
9145065Swnj 		 */
9155065Swnj 		case TCPS_TIME_WAIT:
9165162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
9175065Swnj 			goto dropafterack;
9184601Swnj 		}
9195085Swnj 	}
9204601Swnj 
9215065Swnj step6:
9225065Swnj 	/*
9235244Sroot 	 * Update window information.
92425939Skarels 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
9255244Sroot 	 */
92625939Skarels 	if ((tiflags & TH_ACK) &&
92725939Skarels 	    (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
9285391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
92925939Skarels 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) {
93030525Skarels 		/* keep track of pure window updates */
93130525Skarels 		if (ti->ti_len == 0 &&
93232098Skarels 		    tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)
93330525Skarels 			tcpstat.tcps_rcvwinupd++;
9345244Sroot 		tp->snd_wnd = ti->ti_win;
9355244Sroot 		tp->snd_wl1 = ti->ti_seq;
9365244Sroot 		tp->snd_wl2 = ti->ti_ack;
93725260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
93825260Skarels 			tp->max_sndwnd = tp->snd_wnd;
93926824Skarels 		needoutput = 1;
94026824Skarels 	}
9415244Sroot 
9425244Sroot 	/*
9435547Swnj 	 * Process segments with URG.
9445065Swnj 	 */
9457267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
9467267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
9475547Swnj 		/*
94825939Skarels 		 * This is a kludge, but if we receive and accept
94913121Ssam 		 * random urgent pointers, we'll crash in
95013121Ssam 		 * soreceive.  It's hard to imagine someone
95113121Ssam 		 * actually wanting to send this much urgent data.
95212441Ssam 		 */
95325652Skarels 		if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) {
95412441Ssam 			ti->ti_urp = 0;			/* XXX */
95512441Ssam 			tiflags &= ~TH_URG;		/* XXX */
95625939Skarels 			goto dodata;			/* XXX */
95712441Ssam 		}
95812441Ssam 		/*
9595547Swnj 		 * If this segment advances the known urgent pointer,
9605547Swnj 		 * then mark the data stream.  This should not happen
9615547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
9625547Swnj 		 * a FIN has been received from the remote side.
9635547Swnj 		 * In these states we ignore the URG.
96427190Skarels 		 *
96527190Skarels 		 * According to RFC961 (Assigned Protocols),
96627190Skarels 		 * the urgent pointer points to the last octet
96727190Skarels 		 * of urgent data.  We continue, however,
96827190Skarels 		 * to consider it to indicate the first octet
96927190Skarels 		 * of data past the urgent section
97027190Skarels 		 * as the original spec states.
9715547Swnj 		 */
9725547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
9735547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
9745547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
9755547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
9765547Swnj 			if (so->so_oobmark == 0)
9775547Swnj 				so->so_state |= SS_RCVATMARK;
9788313Sroot 			sohasoutofband(so);
97924816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
9805440Swnj 		}
9815547Swnj 		/*
9825547Swnj 		 * Remove out of band data so doesn't get presented to user.
9835547Swnj 		 * This can happen independent of advancing the URG pointer,
9845547Swnj 		 * but if two URG's are pending at once, some out-of-band
9855547Swnj 		 * data may creep in... ick.
9865547Swnj 		 */
98733603Skarels 		if (ti->ti_urp <= ti->ti_len &&
98833603Skarels 		    (so->so_options & SO_OOBINLINE) == 0)
9895547Swnj 			tcp_pulloutofband(so, ti);
99025939Skarels 	} else
99125939Skarels 		/*
99225939Skarels 		 * If no out of band data is expected,
99325939Skarels 		 * pull receive urgent pointer along
99425939Skarels 		 * with the receive window.
99525939Skarels 		 */
99625939Skarels 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
99725939Skarels 			tp->rcv_up = tp->rcv_nxt;
99825939Skarels dodata:							/* XXX */
9994601Swnj 
10004601Swnj 	/*
10015065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
10025065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
10035065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
10045065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
10055065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
10065065Swnj 	 * connection then we just ignore the text.
10074601Swnj 	 */
100817946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
100917946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
101024816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
10115440Swnj 		if (tcpnodelack == 0)
10125440Swnj 			tp->t_flags |= TF_DELACK;
10135440Swnj 		else
10145440Swnj 			tp->t_flags |= TF_ACKNOW;
101525260Skarels 		/*
101625260Skarels 		 * Note the amount of data that peer has sent into
101725260Skarels 		 * our window, in order to estimate the sender's
101825260Skarels 		 * buffer size.
101925260Skarels 		 */
102032098Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
102125260Skarels 		if (len > tp->max_rcvd)
102225260Skarels 			tp->max_rcvd = len;
10235244Sroot 	} else {
10244924Swnj 		m_freem(m);
10255263Swnj 		tiflags &= ~TH_FIN;
10265244Sroot 	}
10274601Swnj 
10284601Swnj 	/*
10295263Swnj 	 * If FIN is received ACK the FIN and let the user know
10305263Swnj 	 * that the connection is closing.
10314601Swnj 	 */
10325263Swnj 	if (tiflags & TH_FIN) {
10335244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
10345244Sroot 			socantrcvmore(so);
10355244Sroot 			tp->t_flags |= TF_ACKNOW;
10365244Sroot 			tp->rcv_nxt++;
10375244Sroot 		}
10385065Swnj 		switch (tp->t_state) {
10394601Swnj 
10405065Swnj 	 	/*
10415065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
10425065Swnj 		 * enter the CLOSE_WAIT state.
10434884Swnj 		 */
10445065Swnj 		case TCPS_SYN_RECEIVED:
10455065Swnj 		case TCPS_ESTABLISHED:
10465065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
10475065Swnj 			break;
10484884Swnj 
10495065Swnj 	 	/*
10505085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
10515085Swnj 		 * enter the CLOSING state.
10524884Swnj 		 */
10535065Swnj 		case TCPS_FIN_WAIT_1:
10545085Swnj 			tp->t_state = TCPS_CLOSING;
10555065Swnj 			break;
10564601Swnj 
10575065Swnj 	 	/*
10585065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
10595065Swnj 		 * starting the time-wait timer, turning off the other
10605065Swnj 		 * standard timers.
10615065Swnj 		 */
10625065Swnj 		case TCPS_FIN_WAIT_2:
10635244Sroot 			tp->t_state = TCPS_TIME_WAIT;
10645074Swnj 			tcp_canceltimers(tp);
10655162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10665244Sroot 			soisdisconnected(so);
10675065Swnj 			break;
10685065Swnj 
10694884Swnj 		/*
10705065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
10714884Swnj 		 */
10725065Swnj 		case TCPS_TIME_WAIT:
10735162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
10745065Swnj 			break;
10755085Swnj 		}
10764601Swnj 	}
10775267Sroot 	if (so->so_options & SO_DEBUG)
10785267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
10795085Swnj 
10805085Swnj 	/*
10815085Swnj 	 * Return any desired output.
10825085Swnj 	 */
108326824Skarels 	if (needoutput || (tp->t_flags & TF_ACKNOW))
108425939Skarels 		(void) tcp_output(tp);
10855065Swnj 	return;
10865085Swnj 
10875065Swnj dropafterack:
10885085Swnj 	/*
10896211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
10906211Swnj 	 * sequence space, where the ACK reflects our state.
10915085Swnj 	 */
109226057Skarels 	if (tiflags & TH_RST)
10935085Swnj 		goto drop;
109431749Skarels 	m_freem(m);
109531721Skarels 	tp->t_flags |= TF_ACKNOW;
109631721Skarels 	(void) tcp_output(tp);
10975231Swnj 	return;
10985085Swnj 
10995085Swnj dropwithreset:
110011731Ssam 	if (om) {
11016161Ssam 		(void) m_free(om);
110211731Ssam 		om = 0;
110311731Ssam 	}
11045085Swnj 	/*
11055244Sroot 	 * Generate a RST, dropping incoming segment.
11065085Swnj 	 * Make ACK acceptable to originator of segment.
110725197Skarels 	 * Don't bother to respond if destination was broadcast.
11085085Swnj 	 */
110925197Skarels 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
11105085Swnj 		goto drop;
11115085Swnj 	if (tiflags & TH_ACK)
11125391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
11135085Swnj 	else {
11145085Swnj 		if (tiflags & TH_SYN)
11155085Swnj 			ti->ti_len++;
11166211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
11176211Swnj 		    TH_RST|TH_ACK);
11185085Swnj 	}
111910769Ssam 	/* destroy temporarily created socket */
112010769Ssam 	if (dropsocket)
112110769Ssam 		(void) soabort(so);
11225231Swnj 	return;
11235085Swnj 
11245065Swnj drop:
112511730Ssam 	if (om)
112611730Ssam 		(void) m_free(om);
11275085Swnj 	/*
11285085Swnj 	 * Drop space held by incoming segment and return.
11295085Swnj 	 */
11306303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
11316303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
11325065Swnj 	m_freem(m);
113310769Ssam 	/* destroy temporarily created socket */
113410769Ssam 	if (dropsocket)
113510769Ssam 		(void) soabort(so);
11365267Sroot 	return;
11375065Swnj }
11385065Swnj 
113917272Skarels tcp_dooptions(tp, om, ti)
11405440Swnj 	struct tcpcb *tp;
11415440Swnj 	struct mbuf *om;
114217272Skarels 	struct tcpiphdr *ti;
11435419Swnj {
11445440Swnj 	register u_char *cp;
11455440Swnj 	int opt, optlen, cnt;
11465419Swnj 
11475440Swnj 	cp = mtod(om, u_char *);
11485440Swnj 	cnt = om->m_len;
11495440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
11505440Swnj 		opt = cp[0];
11515440Swnj 		if (opt == TCPOPT_EOL)
11525440Swnj 			break;
11535440Swnj 		if (opt == TCPOPT_NOP)
11545440Swnj 			optlen = 1;
115512169Ssam 		else {
11565440Swnj 			optlen = cp[1];
115712169Ssam 			if (optlen <= 0)
115812169Ssam 				break;
115912169Ssam 		}
11605440Swnj 		switch (opt) {
11615440Swnj 
11625440Swnj 		default:
11635440Swnj 			break;
11645440Swnj 
11655440Swnj 		case TCPOPT_MAXSEG:
11665440Swnj 			if (optlen != 4)
11675440Swnj 				continue;
116817272Skarels 			if (!(ti->ti_flags & TH_SYN))
116917272Skarels 				continue;
11705440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
11716161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
117217272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
11735440Swnj 			break;
11745419Swnj 		}
11755419Swnj 	}
11766161Ssam 	(void) m_free(om);
11775419Swnj }
11785419Swnj 
11795419Swnj /*
11805547Swnj  * Pull out of band byte out of a segment so
11815547Swnj  * it doesn't appear in the user's data queue.
11825547Swnj  * It is still reflected in the segment length for
11835547Swnj  * sequencing purposes.
11845547Swnj  */
11855547Swnj tcp_pulloutofband(so, ti)
11865547Swnj 	struct socket *so;
11875547Swnj 	struct tcpiphdr *ti;
11885547Swnj {
11895547Swnj 	register struct mbuf *m;
11906116Swnj 	int cnt = ti->ti_urp - 1;
11915547Swnj 
11925547Swnj 	m = dtom(ti);
11935547Swnj 	while (cnt >= 0) {
11945547Swnj 		if (m->m_len > cnt) {
11955547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
11965547Swnj 			struct tcpcb *tp = sototcpcb(so);
11975547Swnj 
11985547Swnj 			tp->t_iobc = *cp;
11995547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
12006161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
12015547Swnj 			m->m_len--;
12025547Swnj 			return;
12035547Swnj 		}
12045547Swnj 		cnt -= m->m_len;
12055547Swnj 		m = m->m_next;
12065547Swnj 		if (m == 0)
12075547Swnj 			break;
12085547Swnj 	}
12095547Swnj 	panic("tcp_pulloutofband");
12105547Swnj }
12115547Swnj 
12125547Swnj /*
121317272Skarels  *  Determine a reasonable value for maxseg size.
121417272Skarels  *  If the route is known, use one that can be handled
121517272Skarels  *  on the given interface without forcing IP to fragment.
121631726Skarels  *  If bigger than an mbuf cluster (MCLBYTES), round down to nearest size
121731726Skarels  *  to utilize large mbufs.
121817272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
121923975Skarels  *  use a conservative size (512 or the default IP max size, but no more
122023975Skarels  *  than the mtu of the interface through which we route),
122117272Skarels  *  as we can't discover anything about intervening gateways or networks.
122232034Skarels  *  We also initialize the congestion/slow start window to be a single
122332034Skarels  *  segment if the destination isn't local; this information should
122432034Skarels  *  probably all be saved with the routing entry at the transport level.
122517272Skarels  *
122617272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
122717272Skarels  */
122817272Skarels tcp_mss(tp)
122923975Skarels 	register struct tcpcb *tp;
123017272Skarels {
123117272Skarels 	struct route *ro;
123217272Skarels 	struct ifnet *ifp;
123317272Skarels 	int mss;
123417272Skarels 	struct inpcb *inp;
123517272Skarels 
123617272Skarels 	inp = tp->t_inpcb;
123717272Skarels 	ro = &inp->inp_route;
123817272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
123917272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
124017272Skarels 		/* No route yet, so try to acquire one */
124117272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
124217272Skarels 			ro->ro_dst.sa_family = AF_INET;
124317272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
124417272Skarels 				inp->inp_faddr;
124517272Skarels 			rtalloc(ro);
124617272Skarels 		}
124717272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
124817316Skarels 			return (TCP_MSS);
124917272Skarels 	}
125017272Skarels 
125117272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
125231726Skarels #if	(MCLBYTES & (MCLBYTES - 1)) == 0
125331726Skarels 	if (mss > MCLBYTES)
125431726Skarels 		mss &= ~(MCLBYTES-1);
125517272Skarels #else
125631726Skarels 	if (mss > MCLBYTES)
125731726Skarels 		mss = mss / MCLBYTES * MCLBYTES;
125817272Skarels #endif
125923975Skarels 	if (in_localaddr(inp->inp_faddr))
126023975Skarels 		return (mss);
126132098Skarels 
126232034Skarels 	mss = MIN(mss, TCP_MSS);
126332034Skarels 	tp->snd_cwnd = mss;
126432034Skarels 	return (mss);
126517272Skarels }
1266