xref: /csrg-svn/sys/netinet/tcp_input.c (revision 25260)
123190Smckusick /*
223190Smckusick  * Copyright (c) 1982 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*25260Skarels  *	@(#)tcp_input.c	6.16 (Berkeley) 10/23/85
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; \
5424816Skarels 		sbappend(&(so)->so_rcv, (m)); \
5524816Skarels 		sorwakeup(so); \
5624816Skarels 	} else \
5724816Skarels 		(flags) = tcp_reass((tp), (ti)); \
5824816Skarels }
5924816Skarels 
6024816Skarels tcp_reass(tp, ti)
6124816Skarels 	register struct tcpcb *tp;
6224816Skarels 	register struct tcpiphdr *ti;
6324816Skarels {
6424816Skarels 	register struct tcpiphdr *q;
6524816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
6624816Skarels 	struct mbuf *m;
6724816Skarels 	int flags;
6824816Skarels 
6924816Skarels 	/*
7024816Skarels 	 * Call with ti==0 after become established to
7124816Skarels 	 * force pre-ESTABLISHED data up to user socket.
7224816Skarels 	 */
7324816Skarels 	if (ti == 0)
7424816Skarels 		goto present;
7524816Skarels 
7624816Skarels 	/*
7724816Skarels 	 * Find a segment which begins after this one does.
7824816Skarels 	 */
7924816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
8024816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
8124816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
8224816Skarels 			break;
8324816Skarels 
8424816Skarels 	/*
8524816Skarels 	 * If there is a preceding segment, it may provide some of
8624816Skarels 	 * our data already.  If so, drop the data from the incoming
8724816Skarels 	 * segment.  If it provides all of our data, drop us.
8824816Skarels 	 */
8924816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
9024816Skarels 		register int i;
9124816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
9224816Skarels 		/* conversion to int (in i) handles seq wraparound */
9324816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
9424816Skarels 		if (i > 0) {
9524816Skarels 			if (i >= ti->ti_len)
9624816Skarels 				goto drop;
9724816Skarels 			m_adj(dtom(ti), i);
9824816Skarels 			ti->ti_len -= i;
9924816Skarels 			ti->ti_seq += i;
10024816Skarels 		}
10124816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
10224816Skarels 	}
10324816Skarels 
10424816Skarels 	/*
10524816Skarels 	 * While we overlap succeeding segments trim them or,
10624816Skarels 	 * if they are completely covered, dequeue them.
10724816Skarels 	 */
10824816Skarels 	while (q != (struct tcpiphdr *)tp) {
10924816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
11024816Skarels 		if (i <= 0)
11124816Skarels 			break;
11224816Skarels 		if (i < q->ti_len) {
11324816Skarels 			q->ti_seq += i;
11424816Skarels 			q->ti_len -= i;
11524816Skarels 			m_adj(dtom(q), i);
11624816Skarels 			break;
11724816Skarels 		}
11824816Skarels 		q = (struct tcpiphdr *)q->ti_next;
11924816Skarels 		m = dtom(q->ti_prev);
12024816Skarels 		remque(q->ti_prev);
12124816Skarels 		m_freem(m);
12224816Skarels 	}
12324816Skarels 
12424816Skarels 	/*
12524816Skarels 	 * Stick new segment in its place.
12624816Skarels 	 */
12724816Skarels 	insque(ti, q->ti_prev);
12824816Skarels 
12924816Skarels present:
13024816Skarels 	/*
13124816Skarels 	 * Present data to user, advancing rcv_nxt through
13224816Skarels 	 * completed sequence space.
13324816Skarels 	 */
13424816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
13524816Skarels 		return (0);
13624816Skarels 	ti = tp->seg_next;
13724816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
13824816Skarels 		return (0);
13924816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
14024816Skarels 		return (0);
14124816Skarels 	do {
14224816Skarels 		tp->rcv_nxt += ti->ti_len;
14324816Skarels 		flags = ti->ti_flags & TH_FIN;
14424816Skarels 		remque(ti);
14524816Skarels 		m = dtom(ti);
14624816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
14724816Skarels 		if (so->so_state & SS_CANTRCVMORE)
14824816Skarels 			m_freem(m);
14924816Skarels 		else
15024816Skarels 			sbappend(&so->so_rcv, m);
15124816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
15224816Skarels 	sorwakeup(so);
15324816Skarels 	return (flags);
15424816Skarels drop:
15524816Skarels 	m_freem(dtom(ti));
15624816Skarels 	return (0);
15724816Skarels }
15824816Skarels 
15924816Skarels /*
1605065Swnj  * TCP input routine, follows pages 65-76 of the
1615065Swnj  * protocol specification dated September, 1981 very closely.
1625065Swnj  */
1634924Swnj tcp_input(m0)
1644924Swnj 	struct mbuf *m0;
1654601Swnj {
1664924Swnj 	register struct tcpiphdr *ti;
1674924Swnj 	struct inpcb *inp;
1684924Swnj 	register struct mbuf *m;
1695440Swnj 	struct mbuf *om = 0;
1704924Swnj 	int len, tlen, off;
1715391Swnj 	register struct tcpcb *tp = 0;
1724924Swnj 	register int tiflags;
1734803Swnj 	struct socket *so;
1745109Swnj 	int todrop, acked;
1755267Sroot 	short ostate;
1766028Sroot 	struct in_addr laddr;
17710769Ssam 	int dropsocket = 0;
1784924Swnj 
1794924Swnj 	/*
1805244Sroot 	 * Get IP and TCP header together in first mbuf.
1815244Sroot 	 * Note: IP leaves IP header in first mbuf.
1824924Swnj 	 */
1834924Swnj 	m = m0;
1845020Sroot 	ti = mtod(m, struct tcpiphdr *);
1855244Sroot 	if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2))
1865208Swnj 		ip_stripoptions((struct ip *)ti, (struct mbuf *)0);
1875307Sroot 	if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) {
1885307Sroot 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
1895085Swnj 			tcpstat.tcps_hdrops++;
1905307Sroot 			return;
1915085Swnj 		}
1925085Swnj 		ti = mtod(m, struct tcpiphdr *);
1935085Swnj 	}
1944601Swnj 
1954601Swnj 	/*
1965244Sroot 	 * Checksum extended TCP header and data.
1974601Swnj 	 */
1984924Swnj 	tlen = ((struct ip *)ti)->ip_len;
1994924Swnj 	len = sizeof (struct ip) + tlen;
2004679Swnj 	if (tcpcksum) {
2014924Swnj 		ti->ti_next = ti->ti_prev = 0;
2024924Swnj 		ti->ti_x1 = 0;
2035223Swnj 		ti->ti_len = (u_short)tlen;
2046161Ssam 		ti->ti_len = htons((u_short)ti->ti_len);
2055231Swnj 		if (ti->ti_sum = in_cksum(m, len)) {
20611830Ssam 			if (tcpprintfs)
20711830Ssam 				printf("tcp sum: src %x\n", ti->ti_src);
2084924Swnj 			tcpstat.tcps_badsum++;
2095085Swnj 			goto drop;
2104601Swnj 		}
2114601Swnj 	}
2124601Swnj 
2134601Swnj 	/*
2145244Sroot 	 * Check that TCP offset makes sense,
2155440Swnj 	 * pull out TCP options and adjust length.
2164601Swnj 	 */
2174924Swnj 	off = ti->ti_off << 2;
2185231Swnj 	if (off < sizeof (struct tcphdr) || off > tlen) {
21911830Ssam 		if (tcpprintfs)
22011830Ssam 			printf("tcp off: src %x off %d\n", ti->ti_src, off);
2214924Swnj 		tcpstat.tcps_badoff++;
2225085Swnj 		goto drop;
2234924Swnj 	}
2246211Swnj 	tlen -= off;
2256211Swnj 	ti->ti_len = tlen;
2265440Swnj 	if (off > sizeof (struct tcphdr)) {
22724816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
22824816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
22924816Skarels 				tcpstat.tcps_hdrops++;
23024816Skarels 				return;
23124816Skarels 			}
23224816Skarels 			ti = mtod(m, struct tcpiphdr *);
2335440Swnj 		}
2349642Ssam 		om = m_get(M_DONTWAIT, MT_DATA);
2355440Swnj 		if (om == 0)
2365440Swnj 			goto drop;
2375440Swnj 		om->m_len = off - sizeof (struct tcphdr);
2385440Swnj 		{ caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
2396161Ssam 		  bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
2405440Swnj 		  m->m_len -= om->m_len;
2416161Ssam 		  bcopy(op+om->m_len, op,
2426161Ssam 		   (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
2435440Swnj 		}
2445440Swnj 	}
2455065Swnj 	tiflags = ti->ti_flags;
2464924Swnj 
2476093Sroot 	/*
2486211Swnj 	 * Drop TCP and IP headers.
2496093Sroot 	 */
2506093Sroot 	off += sizeof (struct ip);
2516093Sroot 	m->m_off += off;
2526093Sroot 	m->m_len -= off;
2536093Sroot 
2544924Swnj 	/*
2555244Sroot 	 * Convert TCP protocol specific fields to host format.
2565085Swnj 	 */
2575085Swnj 	ti->ti_seq = ntohl(ti->ti_seq);
2585085Swnj 	ti->ti_ack = ntohl(ti->ti_ack);
2595085Swnj 	ti->ti_win = ntohs(ti->ti_win);
2605085Swnj 	ti->ti_urp = ntohs(ti->ti_urp);
2615085Swnj 
2625085Swnj 	/*
2638271Sroot 	 * Locate pcb for segment.
2644924Swnj 	 */
2655065Swnj 	inp = in_pcblookup
2666028Sroot 		(&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
2676028Sroot 		INPLOOKUP_WILDCARD);
2685065Swnj 
2695065Swnj 	/*
2705065Swnj 	 * If the state is CLOSED (i.e., TCB does not exist) then
2715244Sroot 	 * all data in the incoming segment is discarded.
2725065Swnj 	 */
2735300Sroot 	if (inp == 0)
2745085Swnj 		goto dropwithreset;
2755065Swnj 	tp = intotcpcb(inp);
2765300Sroot 	if (tp == 0)
2775085Swnj 		goto dropwithreset;
2785109Swnj 	so = inp->inp_socket;
2795267Sroot 	if (so->so_options & SO_DEBUG) {
2805267Sroot 		ostate = tp->t_state;
2815267Sroot 		tcp_saveti = *ti;
2825267Sroot 	}
2837510Sroot 	if (so->so_options & SO_ACCEPTCONN) {
2847510Sroot 		so = sonewconn(so);
2857510Sroot 		if (so == 0)
2867510Sroot 			goto drop;
28710769Ssam 		/*
28810769Ssam 		 * This is ugly, but ....
28910769Ssam 		 *
29010769Ssam 		 * Mark socket as temporary until we're
29110769Ssam 		 * committed to keeping it.  The code at
29210769Ssam 		 * ``drop'' and ``dropwithreset'' check the
29310769Ssam 		 * flag dropsocket to see if the temporary
29410769Ssam 		 * socket created here should be discarded.
29510769Ssam 		 * We mark the socket as discardable until
29610769Ssam 		 * we're committed to it below in TCPS_LISTEN.
29710769Ssam 		 */
29810769Ssam 		dropsocket++;
2997510Sroot 		inp = (struct inpcb *)so->so_pcb;
3007510Sroot 		inp->inp_laddr = ti->ti_dst;
3017510Sroot 		inp->inp_lport = ti->ti_dport;
30224816Skarels 		inp->inp_options = ip_srcroute();
3037510Sroot 		tp = intotcpcb(inp);
3047510Sroot 		tp->t_state = TCPS_LISTEN;
3057510Sroot 	}
3064601Swnj 
3074601Swnj 	/*
3085162Swnj 	 * Segment received on connection.
3095162Swnj 	 * Reset idle time and keep-alive timer.
3105162Swnj 	 */
3115162Swnj 	tp->t_idle = 0;
3125162Swnj 	tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
3135162Swnj 
3145162Swnj 	/*
31517272Skarels 	 * Process options if not in LISTEN state,
31617272Skarels 	 * else do it below (after getting remote address).
3175440Swnj 	 */
31817272Skarels 	if (om && tp->t_state != TCPS_LISTEN) {
31917272Skarels 		tcp_dooptions(tp, om, ti);
3205440Swnj 		om = 0;
3215440Swnj 	}
3225440Swnj 
3235440Swnj 	/*
3245085Swnj 	 * Calculate amount of space in receive window,
3255085Swnj 	 * and then do TCP input processing.
32624816Skarels 	 * Receive window is amount of space in rcv queue,
32724816Skarels 	 * but not less than advertised window.
3284601Swnj 	 */
32925196Skarels 	tp->rcv_wnd = sbspace(&so->so_rcv);
3305231Swnj 	if (tp->rcv_wnd < 0)
3315231Swnj 		tp->rcv_wnd = 0;
33225196Skarels 	tp->rcv_wnd = MAX(tp->rcv_wnd, (short)(tp->rcv_adv - tp->rcv_nxt));
3334601Swnj 
3344601Swnj 	switch (tp->t_state) {
3354601Swnj 
3365065Swnj 	/*
3375065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3385065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3395065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
34025197Skarels 	 * Don't bother responding if the destination was a broadcast.
3415085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3425065Swnj 	 * tp->iss, and send a segment:
3435085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3445065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3455065Swnj 	 * Fill in remote peer address fields if not previously specified.
3465065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3475244Sroot 	 * segment in this state.
3485065Swnj 	 */
3498271Sroot 	case TCPS_LISTEN: {
35010145Ssam 		struct mbuf *am;
3518271Sroot 		register struct sockaddr_in *sin;
3528271Sroot 
3535065Swnj 		if (tiflags & TH_RST)
3545065Swnj 			goto drop;
3555300Sroot 		if (tiflags & TH_ACK)
3565085Swnj 			goto dropwithreset;
3575300Sroot 		if ((tiflags & TH_SYN) == 0)
3585065Swnj 			goto drop;
35925197Skarels 		if (in_broadcast(ti->ti_dst))
36025197Skarels 			goto drop;
36110145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
36210145Ssam 		if (am == NULL)
36310145Ssam 			goto drop;
36410145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3658599Sroot 		sin = mtod(am, struct sockaddr_in *);
3668271Sroot 		sin->sin_family = AF_INET;
3678271Sroot 		sin->sin_addr = ti->ti_src;
3688271Sroot 		sin->sin_port = ti->ti_sport;
3696028Sroot 		laddr = inp->inp_laddr;
37010145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3716028Sroot 			inp->inp_laddr = ti->ti_dst;
3728599Sroot 		if (in_pcbconnect(inp, am)) {
3736028Sroot 			inp->inp_laddr = laddr;
3748716Sroot 			(void) m_free(am);
3755244Sroot 			goto drop;
3766028Sroot 		}
3778716Sroot 		(void) m_free(am);
3785244Sroot 		tp->t_template = tcp_template(tp);
3795244Sroot 		if (tp->t_template == 0) {
3805244Sroot 			in_pcbdisconnect(inp);
38117264Skarels 			dropsocket = 0;		/* socket is already gone */
3826320Swnj 			tp = 0;
3835244Sroot 			goto drop;
3845244Sroot 		}
38517272Skarels 		if (om) {
38617272Skarels 			tcp_dooptions(tp, om, ti);
38717272Skarels 			om = 0;
38817272Skarels 		}
3895085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
3905065Swnj 		tp->irs = ti->ti_seq;
3915085Swnj 		tcp_sendseqinit(tp);
3925085Swnj 		tcp_rcvseqinit(tp);
3935065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
3945244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
39510769Ssam 		dropsocket = 0;		/* committed to socket */
3965085Swnj 		goto trimthenstep6;
3978271Sroot 		}
3984601Swnj 
3995065Swnj 	/*
4005065Swnj 	 * If the state is SYN_SENT:
4015065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
4025065Swnj 	 *	if seg contains a RST, then drop the connection.
4035065Swnj 	 *	if seg does not contain SYN, then drop it.
4045065Swnj 	 * Otherwise this is an acceptable SYN segment
4055065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4065065Swnj 	 *	if seg contains ack then advance tp->snd_una
4075065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4085065Swnj 	 *	arrange for segment to be acked (eventually)
4095065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4105065Swnj 	 */
4115065Swnj 	case TCPS_SYN_SENT:
4125065Swnj 		if ((tiflags & TH_ACK) &&
41324816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4145231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4155085Swnj 			goto dropwithreset;
4165065Swnj 		if (tiflags & TH_RST) {
41710394Ssam 			if (tiflags & TH_ACK)
41810394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4195065Swnj 			goto drop;
4204601Swnj 		}
4215065Swnj 		if ((tiflags & TH_SYN) == 0)
4225065Swnj 			goto drop;
4235231Swnj 		tp->snd_una = ti->ti_ack;
4245357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4255357Sroot 			tp->snd_nxt = tp->snd_una;
4265244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4275065Swnj 		tp->irs = ti->ti_seq;
4285085Swnj 		tcp_rcvseqinit(tp);
4295085Swnj 		tp->t_flags |= TF_ACKNOW;
4305162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
4315244Sroot 			soisconnected(so);
4325065Swnj 			tp->t_state = TCPS_ESTABLISHED;
43317272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4345162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
4355162Swnj 		} else
4365085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4375085Swnj 		goto trimthenstep6;
4385085Swnj 
4395085Swnj trimthenstep6:
4405085Swnj 		/*
4415231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4425085Swnj 		 * If data, trim to stay within window,
4435085Swnj 		 * dropping FIN if necessary.
4445085Swnj 		 */
4455231Swnj 		ti->ti_seq++;
4465085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4475085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4485085Swnj 			m_adj(m, -todrop);
4495085Swnj 			ti->ti_len = tp->rcv_wnd;
4505085Swnj 			ti->ti_flags &= ~TH_FIN;
4515065Swnj 		}
4525263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
4535085Swnj 		goto step6;
4545065Swnj 	}
4554601Swnj 
4565065Swnj 	/*
45716222Skarels 	 * If data is received on a connection after the
45816222Skarels 	 * user processes are gone, then RST the other end.
45916222Skarels 	 */
46016222Skarels 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
46116222Skarels 	    ti->ti_len) {
46216222Skarels 		tp = tcp_close(tp);
46316222Skarels 		goto dropwithreset;
46416222Skarels 	}
46516222Skarels 
46616222Skarels 	/*
4675065Swnj 	 * States other than LISTEN or SYN_SENT.
4685065Swnj 	 * First check that at least some bytes of segment are within
4695065Swnj 	 * receive window.
4705065Swnj 	 */
4715065Swnj 	if (tp->rcv_wnd == 0) {
4725065Swnj 		/*
4735065Swnj 		 * If window is closed can only take segments at
4745231Swnj 		 * window edge, and have to drop data and PUSH from
4755065Swnj 		 * incoming segments.
4765065Swnj 		 */
4775300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
4785065Swnj 			goto dropafterack;
4795085Swnj 		if (ti->ti_len > 0) {
4805690Swnj 			m_adj(m, ti->ti_len);
4815085Swnj 			ti->ti_len = 0;
4825085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
4835065Swnj 		}
4845065Swnj 	} else {
4855065Swnj 		/*
4865231Swnj 		 * If segment begins before rcv_nxt, drop leading
4875065Swnj 		 * data (and SYN); if nothing left, just ack.
4885065Swnj 		 */
4895690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
4905690Swnj 		if (todrop > 0) {
4915085Swnj 			if (tiflags & TH_SYN) {
4925300Sroot 				tiflags &= ~TH_SYN;
4935690Swnj 				ti->ti_flags &= ~TH_SYN;
4945085Swnj 				ti->ti_seq++;
4955085Swnj 				if (ti->ti_urp > 1)
4965085Swnj 					ti->ti_urp--;
4975085Swnj 				else
4985085Swnj 					tiflags &= ~TH_URG;
4995085Swnj 				todrop--;
5005085Swnj 			}
5016211Swnj 			if (todrop > ti->ti_len ||
5026211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
5035065Swnj 				goto dropafterack;
5045065Swnj 			m_adj(m, todrop);
5055065Swnj 			ti->ti_seq += todrop;
5065065Swnj 			ti->ti_len -= todrop;
5075085Swnj 			if (ti->ti_urp > todrop)
5085085Swnj 				ti->ti_urp -= todrop;
5095085Swnj 			else {
5105085Swnj 				tiflags &= ~TH_URG;
5115690Swnj 				ti->ti_flags &= ~TH_URG;
5125690Swnj 				ti->ti_urp = 0;
5135085Swnj 			}
5145065Swnj 		}
5155065Swnj 		/*
5165065Swnj 		 * If segment ends after window, drop trailing data
5175085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
5185065Swnj 		 */
5195690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
5205690Swnj 		if (todrop > 0) {
5216211Swnj 			if (todrop >= ti->ti_len)
5225065Swnj 				goto dropafterack;
5235065Swnj 			m_adj(m, -todrop);
5245065Swnj 			ti->ti_len -= todrop;
5255085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
5265065Swnj 		}
5275065Swnj 	}
5284601Swnj 
5295065Swnj 	/*
5305065Swnj 	 * If the RST bit is set examine the state:
5315065Swnj 	 *    SYN_RECEIVED STATE:
5325065Swnj 	 *	If passive open, return to LISTEN state.
5335065Swnj 	 *	If active open, inform user that connection was refused.
5345065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
5355065Swnj 	 *	Inform user that connection was reset, and close tcb.
5365065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
5375065Swnj 	 *	Close the tcb.
5385065Swnj 	 */
5395065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
5405267Sroot 
5415065Swnj 	case TCPS_SYN_RECEIVED:
54210394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
5435065Swnj 		goto drop;
5444601Swnj 
5455065Swnj 	case TCPS_ESTABLISHED:
5465065Swnj 	case TCPS_FIN_WAIT_1:
5475065Swnj 	case TCPS_FIN_WAIT_2:
5485065Swnj 	case TCPS_CLOSE_WAIT:
54910394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5505065Swnj 		goto drop;
5515065Swnj 
5525065Swnj 	case TCPS_CLOSING:
5535065Swnj 	case TCPS_LAST_ACK:
5545065Swnj 	case TCPS_TIME_WAIT:
55510394Ssam 		tp = tcp_close(tp);
5565065Swnj 		goto drop;
5574601Swnj 	}
5584601Swnj 
5594601Swnj 	/*
5605065Swnj 	 * If a SYN is in the window, then this is an
5615065Swnj 	 * error and we send an RST and drop the connection.
5624601Swnj 	 */
5635065Swnj 	if (tiflags & TH_SYN) {
56410394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5655085Swnj 		goto dropwithreset;
5664601Swnj 	}
5674601Swnj 
5684601Swnj 	/*
5695065Swnj 	 * If the ACK bit is off we drop the segment and return.
5704601Swnj 	 */
5715085Swnj 	if ((tiflags & TH_ACK) == 0)
5725065Swnj 		goto drop;
5735065Swnj 
5745065Swnj 	/*
5755065Swnj 	 * Ack processing.
5765065Swnj 	 */
5774601Swnj 	switch (tp->t_state) {
5784601Swnj 
5795065Swnj 	/*
5805065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
5815065Swnj 	 * ESTABLISHED state and continue processing, othewise
5825065Swnj 	 * send an RST.
5835065Swnj 	 */
5845065Swnj 	case TCPS_SYN_RECEIVED:
5855085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
5865231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
5875085Swnj 			goto dropwithreset;
5885244Sroot 		tp->snd_una++;			/* SYN acked */
5895357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5905357Sroot 			tp->snd_nxt = tp->snd_una;
5915244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
5925085Swnj 		soisconnected(so);
5935085Swnj 		tp->t_state = TCPS_ESTABLISHED;
59417272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
5955162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
5965244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
5975085Swnj 		/* fall into ... */
5984601Swnj 
5995065Swnj 	/*
6005065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
6015065Swnj 	 * ACKs.  If the ack is in the range
6025231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6035065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6045065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6055065Swnj 	 * more up to date window information we update our window information.
6065065Swnj 	 */
6075065Swnj 	case TCPS_ESTABLISHED:
6085065Swnj 	case TCPS_FIN_WAIT_1:
6095065Swnj 	case TCPS_FIN_WAIT_2:
6105065Swnj 	case TCPS_CLOSE_WAIT:
6115065Swnj 	case TCPS_CLOSING:
6125244Sroot 	case TCPS_LAST_ACK:
6135244Sroot 	case TCPS_TIME_WAIT:
6145085Swnj #define	ourfinisacked	(acked > 0)
6155085Swnj 
6165244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
6175065Swnj 			break;
6185300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
6195065Swnj 			goto dropafterack;
6205085Swnj 		acked = ti->ti_ack - tp->snd_una;
6215951Swnj 
6225951Swnj 		/*
6235951Swnj 		 * If transmit timer is running and timed sequence
6245951Swnj 		 * number was acked, update smoothed round trip time.
6255951Swnj 		 */
6265951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
6275951Swnj 			if (tp->t_srtt == 0)
6285951Swnj 				tp->t_srtt = tp->t_rtt;
6295951Swnj 			else
6305951Swnj 				tp->t_srtt =
6315951Swnj 				    tcp_alpha * tp->t_srtt +
6325951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
6335951Swnj 			tp->t_rtt = 0;
6345951Swnj 		}
6355951Swnj 
6365307Sroot 		if (ti->ti_ack == tp->snd_max)
6375244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
6385307Sroot 		else {
6395244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
6405244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
6415300Sroot 			tp->t_rxtshift = 0;
6425085Swnj 		}
64317360Skarels 		/*
64417360Skarels 		 * When new data is acked, open the congestion window a bit.
64517360Skarels 		 */
64617360Skarels 		if (acked > 0)
64717360Skarels 			tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
6485307Sroot 		if (acked > so->so_snd.sb_cc) {
64915386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
6505307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
6515307Sroot 		} else {
6526161Ssam 			sbdrop(&so->so_snd, acked);
6535307Sroot 			tp->snd_wnd -= acked;
6545307Sroot 			acked = 0;
6555307Sroot 		}
6566434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
6575300Sroot 			sowwakeup(so);
6585231Swnj 		tp->snd_una = ti->ti_ack;
6595357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
6605357Sroot 			tp->snd_nxt = tp->snd_una;
6615162Swnj 
6624601Swnj 		switch (tp->t_state) {
6634601Swnj 
6645065Swnj 		/*
6655065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
6665065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
6675085Swnj 		 * then enter FIN_WAIT_2.
6685065Swnj 		 */
6695065Swnj 		case TCPS_FIN_WAIT_1:
6705896Swnj 			if (ourfinisacked) {
6715896Swnj 				/*
6725896Swnj 				 * If we can't receive any more
6735896Swnj 				 * data, then closing user can proceed.
67424816Skarels 				 * Starting the timer is contrary to the
67524816Skarels 				 * specification, but if we don't get a FIN
67624816Skarels 				 * we'll hang forever.
6775896Swnj 				 */
67824816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
6795896Swnj 					soisdisconnected(so);
68024816Skarels 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
68124816Skarels 				}
6825085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
6835896Swnj 			}
6844601Swnj 			break;
6854601Swnj 
6865065Swnj 	 	/*
6875065Swnj 		 * In CLOSING STATE in addition to the processing for
6885065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
6895065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
6905065Swnj 		 * the segment.
6915065Swnj 		 */
6925065Swnj 		case TCPS_CLOSING:
6935244Sroot 			if (ourfinisacked) {
6945065Swnj 				tp->t_state = TCPS_TIME_WAIT;
6955244Sroot 				tcp_canceltimers(tp);
6965244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6975244Sroot 				soisdisconnected(so);
6985244Sroot 			}
6995244Sroot 			break;
7004601Swnj 
7015065Swnj 		/*
7025085Swnj 		 * The only thing that can arrive in  LAST_ACK state
7035085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
7045085Swnj 		 * acknowledged, delete the TCB, enter the closed state
7055085Swnj 		 * and return.
7065065Swnj 		 */
7075065Swnj 		case TCPS_LAST_ACK:
70810394Ssam 			if (ourfinisacked)
70910394Ssam 				tp = tcp_close(tp);
7105065Swnj 			goto drop;
7114601Swnj 
7125065Swnj 		/*
7135065Swnj 		 * In TIME_WAIT state the only thing that should arrive
7145065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
7155065Swnj 		 * it and restart the finack timer.
7165065Swnj 		 */
7175065Swnj 		case TCPS_TIME_WAIT:
7185162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
7195065Swnj 			goto dropafterack;
7204601Swnj 		}
7215085Swnj #undef ourfinisacked
7225085Swnj 	}
7234601Swnj 
7245065Swnj step6:
7255065Swnj 	/*
7265244Sroot 	 * Update window information.
7275244Sroot 	 */
7285300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
7295391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
7305300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
7315244Sroot 		tp->snd_wnd = ti->ti_win;
7325244Sroot 		tp->snd_wl1 = ti->ti_seq;
7335244Sroot 		tp->snd_wl2 = ti->ti_ack;
734*25260Skarels 		if (tp->snd_wnd > tp->max_sndwnd)
735*25260Skarels 			tp->max_sndwnd = tp->snd_wnd;
7365244Sroot 	}
7375244Sroot 
7385244Sroot 	/*
7395547Swnj 	 * Process segments with URG.
7405065Swnj 	 */
7417267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
7427267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
7435547Swnj 		/*
74413121Ssam 		 * This is a kludge, but if we receive accept
74513121Ssam 		 * random urgent pointers, we'll crash in
74613121Ssam 		 * soreceive.  It's hard to imagine someone
74713121Ssam 		 * actually wanting to send this much urgent data.
74812441Ssam 		 */
74917360Skarels 		if (ti->ti_urp + (unsigned) so->so_rcv.sb_cc > 32767) {
75012441Ssam 			ti->ti_urp = 0;			/* XXX */
75112441Ssam 			tiflags &= ~TH_URG;		/* XXX */
75212441Ssam 			ti->ti_flags &= ~TH_URG;	/* XXX */
75313121Ssam 			goto badurp;			/* XXX */
75412441Ssam 		}
75512441Ssam 		/*
7565547Swnj 		 * If this segment advances the known urgent pointer,
7575547Swnj 		 * then mark the data stream.  This should not happen
7585547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
7595547Swnj 		 * a FIN has been received from the remote side.
7605547Swnj 		 * In these states we ignore the URG.
7615547Swnj 		 */
7625547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
7635547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
7645547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
7655547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
7665547Swnj 			if (so->so_oobmark == 0)
7675547Swnj 				so->so_state |= SS_RCVATMARK;
7688313Sroot 			sohasoutofband(so);
76924816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
7705440Swnj 		}
7715547Swnj 		/*
7725547Swnj 		 * Remove out of band data so doesn't get presented to user.
7735547Swnj 		 * This can happen independent of advancing the URG pointer,
7745547Swnj 		 * but if two URG's are pending at once, some out-of-band
7755547Swnj 		 * data may creep in... ick.
7765547Swnj 		 */
7777510Sroot 		if (ti->ti_urp <= ti->ti_len)
7785547Swnj 			tcp_pulloutofband(so, ti);
7795419Swnj 	}
78013121Ssam badurp:							/* XXX */
7814601Swnj 
7824601Swnj 	/*
7835065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
7845065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
7855065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
7865065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
7875065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
7885065Swnj 	 * connection then we just ignore the text.
7894601Swnj 	 */
79017946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
79117946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
79224816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
7935440Swnj 		if (tcpnodelack == 0)
7945440Swnj 			tp->t_flags |= TF_DELACK;
7955440Swnj 		else
7965440Swnj 			tp->t_flags |= TF_ACKNOW;
797*25260Skarels 		/*
798*25260Skarels 		 * Note the amount of data that peer has sent into
799*25260Skarels 		 * our window, in order to estimate the sender's
800*25260Skarels 		 * buffer size.
801*25260Skarels 		 */
802*25260Skarels 		len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv);
803*25260Skarels 		if (len > tp->max_rcvd)
804*25260Skarels 			tp->max_rcvd = len;
8055244Sroot 	} else {
8064924Swnj 		m_freem(m);
8075263Swnj 		tiflags &= ~TH_FIN;
8085244Sroot 	}
8094601Swnj 
8104601Swnj 	/*
8115263Swnj 	 * If FIN is received ACK the FIN and let the user know
8125263Swnj 	 * that the connection is closing.
8134601Swnj 	 */
8145263Swnj 	if (tiflags & TH_FIN) {
8155244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8165244Sroot 			socantrcvmore(so);
8175244Sroot 			tp->t_flags |= TF_ACKNOW;
8185244Sroot 			tp->rcv_nxt++;
8195244Sroot 		}
8205065Swnj 		switch (tp->t_state) {
8214601Swnj 
8225065Swnj 	 	/*
8235065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
8245065Swnj 		 * enter the CLOSE_WAIT state.
8254884Swnj 		 */
8265065Swnj 		case TCPS_SYN_RECEIVED:
8275065Swnj 		case TCPS_ESTABLISHED:
8285065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
8295065Swnj 			break;
8304884Swnj 
8315065Swnj 	 	/*
8325085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
8335085Swnj 		 * enter the CLOSING state.
8344884Swnj 		 */
8355065Swnj 		case TCPS_FIN_WAIT_1:
8365085Swnj 			tp->t_state = TCPS_CLOSING;
8375065Swnj 			break;
8384601Swnj 
8395065Swnj 	 	/*
8405065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8415065Swnj 		 * starting the time-wait timer, turning off the other
8425065Swnj 		 * standard timers.
8435065Swnj 		 */
8445065Swnj 		case TCPS_FIN_WAIT_2:
8455244Sroot 			tp->t_state = TCPS_TIME_WAIT;
8465074Swnj 			tcp_canceltimers(tp);
8475162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8485244Sroot 			soisdisconnected(so);
8495065Swnj 			break;
8505065Swnj 
8514884Swnj 		/*
8525065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
8534884Swnj 		 */
8545065Swnj 		case TCPS_TIME_WAIT:
8555162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8565065Swnj 			break;
8575085Swnj 		}
8584601Swnj 	}
8595267Sroot 	if (so->so_options & SO_DEBUG)
8605267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
8615085Swnj 
8625085Swnj 	/*
8635085Swnj 	 * Return any desired output.
8645085Swnj 	 */
8656161Ssam 	(void) tcp_output(tp);
8665065Swnj 	return;
8675085Swnj 
8685065Swnj dropafterack:
8695085Swnj 	/*
8706211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
8716211Swnj 	 * sequence space, where the ACK reflects our state.
8725085Swnj 	 */
8736211Swnj 	if ((tiflags&TH_RST) ||
8746211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
8755085Swnj 		goto drop;
8766303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
8776303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
8785391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
8795231Swnj 	return;
8805085Swnj 
8815085Swnj dropwithreset:
88211731Ssam 	if (om) {
8836161Ssam 		(void) m_free(om);
88411731Ssam 		om = 0;
88511731Ssam 	}
8865085Swnj 	/*
8875244Sroot 	 * Generate a RST, dropping incoming segment.
8885085Swnj 	 * Make ACK acceptable to originator of segment.
88925197Skarels 	 * Don't bother to respond if destination was broadcast.
8905085Swnj 	 */
89125197Skarels 	if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst))
8925085Swnj 		goto drop;
8935085Swnj 	if (tiflags & TH_ACK)
8945391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
8955085Swnj 	else {
8965085Swnj 		if (tiflags & TH_SYN)
8975085Swnj 			ti->ti_len++;
8986211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
8996211Swnj 		    TH_RST|TH_ACK);
9005085Swnj 	}
90110769Ssam 	/* destroy temporarily created socket */
90210769Ssam 	if (dropsocket)
90310769Ssam 		(void) soabort(so);
9045231Swnj 	return;
9055085Swnj 
9065065Swnj drop:
90711730Ssam 	if (om)
90811730Ssam 		(void) m_free(om);
9095085Swnj 	/*
9105085Swnj 	 * Drop space held by incoming segment and return.
9115085Swnj 	 */
9126303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
9136303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
9145065Swnj 	m_freem(m);
91510769Ssam 	/* destroy temporarily created socket */
91610769Ssam 	if (dropsocket)
91710769Ssam 		(void) soabort(so);
9185267Sroot 	return;
9195065Swnj }
9205065Swnj 
92117272Skarels tcp_dooptions(tp, om, ti)
9225440Swnj 	struct tcpcb *tp;
9235440Swnj 	struct mbuf *om;
92417272Skarels 	struct tcpiphdr *ti;
9255419Swnj {
9265440Swnj 	register u_char *cp;
9275440Swnj 	int opt, optlen, cnt;
9285419Swnj 
9295440Swnj 	cp = mtod(om, u_char *);
9305440Swnj 	cnt = om->m_len;
9315440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
9325440Swnj 		opt = cp[0];
9335440Swnj 		if (opt == TCPOPT_EOL)
9345440Swnj 			break;
9355440Swnj 		if (opt == TCPOPT_NOP)
9365440Swnj 			optlen = 1;
93712169Ssam 		else {
9385440Swnj 			optlen = cp[1];
93912169Ssam 			if (optlen <= 0)
94012169Ssam 				break;
94112169Ssam 		}
9425440Swnj 		switch (opt) {
9435440Swnj 
9445440Swnj 		default:
9455440Swnj 			break;
9465440Swnj 
9475440Swnj 		case TCPOPT_MAXSEG:
9485440Swnj 			if (optlen != 4)
9495440Swnj 				continue;
95017272Skarels 			if (!(ti->ti_flags & TH_SYN))
95117272Skarels 				continue;
9525440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
9536161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
95417272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
9555440Swnj 			break;
9565419Swnj 		}
9575419Swnj 	}
9586161Ssam 	(void) m_free(om);
9595419Swnj }
9605419Swnj 
9615419Swnj /*
9625547Swnj  * Pull out of band byte out of a segment so
9635547Swnj  * it doesn't appear in the user's data queue.
9645547Swnj  * It is still reflected in the segment length for
9655547Swnj  * sequencing purposes.
9665547Swnj  */
9675547Swnj tcp_pulloutofband(so, ti)
9685547Swnj 	struct socket *so;
9695547Swnj 	struct tcpiphdr *ti;
9705547Swnj {
9715547Swnj 	register struct mbuf *m;
9726116Swnj 	int cnt = ti->ti_urp - 1;
9735547Swnj 
9745547Swnj 	m = dtom(ti);
9755547Swnj 	while (cnt >= 0) {
9765547Swnj 		if (m->m_len > cnt) {
9775547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
9785547Swnj 			struct tcpcb *tp = sototcpcb(so);
9795547Swnj 
9805547Swnj 			tp->t_iobc = *cp;
9815547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
9826161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
9835547Swnj 			m->m_len--;
9845547Swnj 			return;
9855547Swnj 		}
9865547Swnj 		cnt -= m->m_len;
9875547Swnj 		m = m->m_next;
9885547Swnj 		if (m == 0)
9895547Swnj 			break;
9905547Swnj 	}
9915547Swnj 	panic("tcp_pulloutofband");
9925547Swnj }
9935547Swnj 
9945547Swnj /*
99517272Skarels  *  Determine a reasonable value for maxseg size.
99617272Skarels  *  If the route is known, use one that can be handled
99717272Skarels  *  on the given interface without forcing IP to fragment.
99823975Skarels  *  If bigger than a page (CLBYTES), round down to nearest pagesize
99917272Skarels  *  to utilize pagesize mbufs.
100017272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
100123975Skarels  *  use a conservative size (512 or the default IP max size, but no more
100223975Skarels  *  than the mtu of the interface through which we route),
100317272Skarels  *  as we can't discover anything about intervening gateways or networks.
100417272Skarels  *
100517272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
100617272Skarels  */
100717272Skarels tcp_mss(tp)
100823975Skarels 	register struct tcpcb *tp;
100917272Skarels {
101017272Skarels 	struct route *ro;
101117272Skarels 	struct ifnet *ifp;
101217272Skarels 	int mss;
101317272Skarels 	struct inpcb *inp;
101417272Skarels 
101517272Skarels 	inp = tp->t_inpcb;
101617272Skarels 	ro = &inp->inp_route;
101717272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
101817272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
101917272Skarels 		/* No route yet, so try to acquire one */
102017272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
102117272Skarels 			ro->ro_dst.sa_family = AF_INET;
102217272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
102317272Skarels 				inp->inp_faddr;
102417272Skarels 			rtalloc(ro);
102517272Skarels 		}
102617272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
102717316Skarels 			return (TCP_MSS);
102817272Skarels 	}
102917272Skarels 
103017272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
103117272Skarels #if	(CLBYTES & (CLBYTES - 1)) == 0
103217272Skarels 	if (mss > CLBYTES)
103317272Skarels 		mss &= ~(CLBYTES-1);
103417272Skarels #else
103517272Skarels 	if (mss > CLBYTES)
103617272Skarels 		mss = mss / CLBYTES * CLBYTES;
103717272Skarels #endif
103823975Skarels 	if (in_localaddr(inp->inp_faddr))
103923975Skarels 		return (mss);
104017316Skarels 	return (MIN(mss, TCP_MSS));
104117272Skarels }
1042