xref: /csrg-svn/sys/netinet/tcp_input.c (revision 25196)
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*25196Skarels  *	@(#)tcp_input.c	6.14 (Berkeley) 10/14/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 	 */
329*25196Skarels 	tp->rcv_wnd = sbspace(&so->so_rcv);
3305231Swnj 	if (tp->rcv_wnd < 0)
3315231Swnj 		tp->rcv_wnd = 0;
332*25196Skarels 	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.
3405085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3415065Swnj 	 * tp->iss, and send a segment:
3425085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3435065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3445065Swnj 	 * Fill in remote peer address fields if not previously specified.
3455065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3465244Sroot 	 * segment in this state.
3475065Swnj 	 */
3488271Sroot 	case TCPS_LISTEN: {
34910145Ssam 		struct mbuf *am;
3508271Sroot 		register struct sockaddr_in *sin;
3518271Sroot 
3525065Swnj 		if (tiflags & TH_RST)
3535065Swnj 			goto drop;
3545300Sroot 		if (tiflags & TH_ACK)
3555085Swnj 			goto dropwithreset;
3565300Sroot 		if ((tiflags & TH_SYN) == 0)
3575065Swnj 			goto drop;
35810145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
35910145Ssam 		if (am == NULL)
36010145Ssam 			goto drop;
36110145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3628599Sroot 		sin = mtod(am, struct sockaddr_in *);
3638271Sroot 		sin->sin_family = AF_INET;
3648271Sroot 		sin->sin_addr = ti->ti_src;
3658271Sroot 		sin->sin_port = ti->ti_sport;
3666028Sroot 		laddr = inp->inp_laddr;
36710145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3686028Sroot 			inp->inp_laddr = ti->ti_dst;
3698599Sroot 		if (in_pcbconnect(inp, am)) {
3706028Sroot 			inp->inp_laddr = laddr;
3718716Sroot 			(void) m_free(am);
3725244Sroot 			goto drop;
3736028Sroot 		}
3748716Sroot 		(void) m_free(am);
3755244Sroot 		tp->t_template = tcp_template(tp);
3765244Sroot 		if (tp->t_template == 0) {
3775244Sroot 			in_pcbdisconnect(inp);
37817264Skarels 			dropsocket = 0;		/* socket is already gone */
3796320Swnj 			tp = 0;
3805244Sroot 			goto drop;
3815244Sroot 		}
38217272Skarels 		if (om) {
38317272Skarels 			tcp_dooptions(tp, om, ti);
38417272Skarels 			om = 0;
38517272Skarels 		}
3865085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
3875065Swnj 		tp->irs = ti->ti_seq;
3885085Swnj 		tcp_sendseqinit(tp);
3895085Swnj 		tcp_rcvseqinit(tp);
3905065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
3915244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
39210769Ssam 		dropsocket = 0;		/* committed to socket */
3935085Swnj 		goto trimthenstep6;
3948271Sroot 		}
3954601Swnj 
3965065Swnj 	/*
3975065Swnj 	 * If the state is SYN_SENT:
3985065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
3995065Swnj 	 *	if seg contains a RST, then drop the connection.
4005065Swnj 	 *	if seg does not contain SYN, then drop it.
4015065Swnj 	 * Otherwise this is an acceptable SYN segment
4025065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4035065Swnj 	 *	if seg contains ack then advance tp->snd_una
4045065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4055065Swnj 	 *	arrange for segment to be acked (eventually)
4065065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4075065Swnj 	 */
4085065Swnj 	case TCPS_SYN_SENT:
4095065Swnj 		if ((tiflags & TH_ACK) &&
41024816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4115231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4125085Swnj 			goto dropwithreset;
4135065Swnj 		if (tiflags & TH_RST) {
41410394Ssam 			if (tiflags & TH_ACK)
41510394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4165065Swnj 			goto drop;
4174601Swnj 		}
4185065Swnj 		if ((tiflags & TH_SYN) == 0)
4195065Swnj 			goto drop;
4205231Swnj 		tp->snd_una = ti->ti_ack;
4215357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4225357Sroot 			tp->snd_nxt = tp->snd_una;
4235244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4245065Swnj 		tp->irs = ti->ti_seq;
4255085Swnj 		tcp_rcvseqinit(tp);
4265085Swnj 		tp->t_flags |= TF_ACKNOW;
4275162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
4285244Sroot 			soisconnected(so);
4295065Swnj 			tp->t_state = TCPS_ESTABLISHED;
43017272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4315162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
4325162Swnj 		} else
4335085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4345085Swnj 		goto trimthenstep6;
4355085Swnj 
4365085Swnj trimthenstep6:
4375085Swnj 		/*
4385231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4395085Swnj 		 * If data, trim to stay within window,
4405085Swnj 		 * dropping FIN if necessary.
4415085Swnj 		 */
4425231Swnj 		ti->ti_seq++;
4435085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4445085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4455085Swnj 			m_adj(m, -todrop);
4465085Swnj 			ti->ti_len = tp->rcv_wnd;
4475085Swnj 			ti->ti_flags &= ~TH_FIN;
4485065Swnj 		}
4495263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
4505085Swnj 		goto step6;
4515065Swnj 	}
4524601Swnj 
4535065Swnj 	/*
45416222Skarels 	 * If data is received on a connection after the
45516222Skarels 	 * user processes are gone, then RST the other end.
45616222Skarels 	 */
45716222Skarels 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
45816222Skarels 	    ti->ti_len) {
45916222Skarels 		tp = tcp_close(tp);
46016222Skarels 		goto dropwithreset;
46116222Skarels 	}
46216222Skarels 
46316222Skarels 	/*
4645065Swnj 	 * States other than LISTEN or SYN_SENT.
4655065Swnj 	 * First check that at least some bytes of segment are within
4665065Swnj 	 * receive window.
4675065Swnj 	 */
4685065Swnj 	if (tp->rcv_wnd == 0) {
4695065Swnj 		/*
4705065Swnj 		 * If window is closed can only take segments at
4715231Swnj 		 * window edge, and have to drop data and PUSH from
4725065Swnj 		 * incoming segments.
4735065Swnj 		 */
4745300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
4755065Swnj 			goto dropafterack;
4765085Swnj 		if (ti->ti_len > 0) {
4775690Swnj 			m_adj(m, ti->ti_len);
4785085Swnj 			ti->ti_len = 0;
4795085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
4805065Swnj 		}
4815065Swnj 	} else {
4825065Swnj 		/*
4835231Swnj 		 * If segment begins before rcv_nxt, drop leading
4845065Swnj 		 * data (and SYN); if nothing left, just ack.
4855065Swnj 		 */
4865690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
4875690Swnj 		if (todrop > 0) {
4885085Swnj 			if (tiflags & TH_SYN) {
4895300Sroot 				tiflags &= ~TH_SYN;
4905690Swnj 				ti->ti_flags &= ~TH_SYN;
4915085Swnj 				ti->ti_seq++;
4925085Swnj 				if (ti->ti_urp > 1)
4935085Swnj 					ti->ti_urp--;
4945085Swnj 				else
4955085Swnj 					tiflags &= ~TH_URG;
4965085Swnj 				todrop--;
4975085Swnj 			}
4986211Swnj 			if (todrop > ti->ti_len ||
4996211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
5005065Swnj 				goto dropafterack;
5015065Swnj 			m_adj(m, todrop);
5025065Swnj 			ti->ti_seq += todrop;
5035065Swnj 			ti->ti_len -= todrop;
5045085Swnj 			if (ti->ti_urp > todrop)
5055085Swnj 				ti->ti_urp -= todrop;
5065085Swnj 			else {
5075085Swnj 				tiflags &= ~TH_URG;
5085690Swnj 				ti->ti_flags &= ~TH_URG;
5095690Swnj 				ti->ti_urp = 0;
5105085Swnj 			}
5115065Swnj 		}
5125065Swnj 		/*
5135065Swnj 		 * If segment ends after window, drop trailing data
5145085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
5155065Swnj 		 */
5165690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
5175690Swnj 		if (todrop > 0) {
5186211Swnj 			if (todrop >= ti->ti_len)
5195065Swnj 				goto dropafterack;
5205065Swnj 			m_adj(m, -todrop);
5215065Swnj 			ti->ti_len -= todrop;
5225085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
5235065Swnj 		}
5245065Swnj 	}
5254601Swnj 
5265065Swnj 	/*
5275065Swnj 	 * If the RST bit is set examine the state:
5285065Swnj 	 *    SYN_RECEIVED STATE:
5295065Swnj 	 *	If passive open, return to LISTEN state.
5305065Swnj 	 *	If active open, inform user that connection was refused.
5315065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
5325065Swnj 	 *	Inform user that connection was reset, and close tcb.
5335065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
5345065Swnj 	 *	Close the tcb.
5355065Swnj 	 */
5365065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
5375267Sroot 
5385065Swnj 	case TCPS_SYN_RECEIVED:
53910394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
5405065Swnj 		goto drop;
5414601Swnj 
5425065Swnj 	case TCPS_ESTABLISHED:
5435065Swnj 	case TCPS_FIN_WAIT_1:
5445065Swnj 	case TCPS_FIN_WAIT_2:
5455065Swnj 	case TCPS_CLOSE_WAIT:
54610394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5475065Swnj 		goto drop;
5485065Swnj 
5495065Swnj 	case TCPS_CLOSING:
5505065Swnj 	case TCPS_LAST_ACK:
5515065Swnj 	case TCPS_TIME_WAIT:
55210394Ssam 		tp = tcp_close(tp);
5535065Swnj 		goto drop;
5544601Swnj 	}
5554601Swnj 
5564601Swnj 	/*
5575065Swnj 	 * If a SYN is in the window, then this is an
5585065Swnj 	 * error and we send an RST and drop the connection.
5594601Swnj 	 */
5605065Swnj 	if (tiflags & TH_SYN) {
56110394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5625085Swnj 		goto dropwithreset;
5634601Swnj 	}
5644601Swnj 
5654601Swnj 	/*
5665065Swnj 	 * If the ACK bit is off we drop the segment and return.
5674601Swnj 	 */
5685085Swnj 	if ((tiflags & TH_ACK) == 0)
5695065Swnj 		goto drop;
5705065Swnj 
5715065Swnj 	/*
5725065Swnj 	 * Ack processing.
5735065Swnj 	 */
5744601Swnj 	switch (tp->t_state) {
5754601Swnj 
5765065Swnj 	/*
5775065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
5785065Swnj 	 * ESTABLISHED state and continue processing, othewise
5795065Swnj 	 * send an RST.
5805065Swnj 	 */
5815065Swnj 	case TCPS_SYN_RECEIVED:
5825085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
5835231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
5845085Swnj 			goto dropwithreset;
5855244Sroot 		tp->snd_una++;			/* SYN acked */
5865357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5875357Sroot 			tp->snd_nxt = tp->snd_una;
5885244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
5895085Swnj 		soisconnected(so);
5905085Swnj 		tp->t_state = TCPS_ESTABLISHED;
59117272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
5925162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
5935244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
5945085Swnj 		/* fall into ... */
5954601Swnj 
5965065Swnj 	/*
5975065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
5985065Swnj 	 * ACKs.  If the ack is in the range
5995231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
6005065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6015065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6025065Swnj 	 * more up to date window information we update our window information.
6035065Swnj 	 */
6045065Swnj 	case TCPS_ESTABLISHED:
6055065Swnj 	case TCPS_FIN_WAIT_1:
6065065Swnj 	case TCPS_FIN_WAIT_2:
6075065Swnj 	case TCPS_CLOSE_WAIT:
6085065Swnj 	case TCPS_CLOSING:
6095244Sroot 	case TCPS_LAST_ACK:
6105244Sroot 	case TCPS_TIME_WAIT:
6115085Swnj #define	ourfinisacked	(acked > 0)
6125085Swnj 
6135244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
6145065Swnj 			break;
6155300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
6165065Swnj 			goto dropafterack;
6175085Swnj 		acked = ti->ti_ack - tp->snd_una;
6185951Swnj 
6195951Swnj 		/*
6205951Swnj 		 * If transmit timer is running and timed sequence
6215951Swnj 		 * number was acked, update smoothed round trip time.
6225951Swnj 		 */
6235951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
6245951Swnj 			if (tp->t_srtt == 0)
6255951Swnj 				tp->t_srtt = tp->t_rtt;
6265951Swnj 			else
6275951Swnj 				tp->t_srtt =
6285951Swnj 				    tcp_alpha * tp->t_srtt +
6295951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
6305951Swnj 			tp->t_rtt = 0;
6315951Swnj 		}
6325951Swnj 
6335307Sroot 		if (ti->ti_ack == tp->snd_max)
6345244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
6355307Sroot 		else {
6365244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
6375244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
6385300Sroot 			tp->t_rxtshift = 0;
6395085Swnj 		}
64017360Skarels 		/*
64117360Skarels 		 * When new data is acked, open the congestion window a bit.
64217360Skarels 		 */
64317360Skarels 		if (acked > 0)
64417360Skarels 			tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
6455307Sroot 		if (acked > so->so_snd.sb_cc) {
64615386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
6475307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
6485307Sroot 		} else {
6496161Ssam 			sbdrop(&so->so_snd, acked);
6505307Sroot 			tp->snd_wnd -= acked;
6515307Sroot 			acked = 0;
6525307Sroot 		}
6536434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
6545300Sroot 			sowwakeup(so);
6555231Swnj 		tp->snd_una = ti->ti_ack;
6565357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
6575357Sroot 			tp->snd_nxt = tp->snd_una;
6585162Swnj 
6594601Swnj 		switch (tp->t_state) {
6604601Swnj 
6615065Swnj 		/*
6625065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
6635065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
6645085Swnj 		 * then enter FIN_WAIT_2.
6655065Swnj 		 */
6665065Swnj 		case TCPS_FIN_WAIT_1:
6675896Swnj 			if (ourfinisacked) {
6685896Swnj 				/*
6695896Swnj 				 * If we can't receive any more
6705896Swnj 				 * data, then closing user can proceed.
67124816Skarels 				 * Starting the timer is contrary to the
67224816Skarels 				 * specification, but if we don't get a FIN
67324816Skarels 				 * we'll hang forever.
6745896Swnj 				 */
67524816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
6765896Swnj 					soisdisconnected(so);
67724816Skarels 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
67824816Skarels 				}
6795085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
6805896Swnj 			}
6814601Swnj 			break;
6824601Swnj 
6835065Swnj 	 	/*
6845065Swnj 		 * In CLOSING STATE in addition to the processing for
6855065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
6865065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
6875065Swnj 		 * the segment.
6885065Swnj 		 */
6895065Swnj 		case TCPS_CLOSING:
6905244Sroot 			if (ourfinisacked) {
6915065Swnj 				tp->t_state = TCPS_TIME_WAIT;
6925244Sroot 				tcp_canceltimers(tp);
6935244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6945244Sroot 				soisdisconnected(so);
6955244Sroot 			}
6965244Sroot 			break;
6974601Swnj 
6985065Swnj 		/*
6995085Swnj 		 * The only thing that can arrive in  LAST_ACK state
7005085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
7015085Swnj 		 * acknowledged, delete the TCB, enter the closed state
7025085Swnj 		 * and return.
7035065Swnj 		 */
7045065Swnj 		case TCPS_LAST_ACK:
70510394Ssam 			if (ourfinisacked)
70610394Ssam 				tp = tcp_close(tp);
7075065Swnj 			goto drop;
7084601Swnj 
7095065Swnj 		/*
7105065Swnj 		 * In TIME_WAIT state the only thing that should arrive
7115065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
7125065Swnj 		 * it and restart the finack timer.
7135065Swnj 		 */
7145065Swnj 		case TCPS_TIME_WAIT:
7155162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
7165065Swnj 			goto dropafterack;
7174601Swnj 		}
7185085Swnj #undef ourfinisacked
7195085Swnj 	}
7204601Swnj 
7215065Swnj step6:
7225065Swnj 	/*
7235244Sroot 	 * Update window information.
7245244Sroot 	 */
7255300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
7265391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
7275300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
7285244Sroot 		tp->snd_wnd = ti->ti_win;
7295244Sroot 		tp->snd_wl1 = ti->ti_seq;
7305244Sroot 		tp->snd_wl2 = ti->ti_ack;
7315244Sroot 	}
7325244Sroot 
7335244Sroot 	/*
7345547Swnj 	 * Process segments with URG.
7355065Swnj 	 */
7367267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
7377267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
7385547Swnj 		/*
73913121Ssam 		 * This is a kludge, but if we receive accept
74013121Ssam 		 * random urgent pointers, we'll crash in
74113121Ssam 		 * soreceive.  It's hard to imagine someone
74213121Ssam 		 * actually wanting to send this much urgent data.
74312441Ssam 		 */
74417360Skarels 		if (ti->ti_urp + (unsigned) so->so_rcv.sb_cc > 32767) {
74512441Ssam 			ti->ti_urp = 0;			/* XXX */
74612441Ssam 			tiflags &= ~TH_URG;		/* XXX */
74712441Ssam 			ti->ti_flags &= ~TH_URG;	/* XXX */
74813121Ssam 			goto badurp;			/* XXX */
74912441Ssam 		}
75012441Ssam 		/*
7515547Swnj 		 * If this segment advances the known urgent pointer,
7525547Swnj 		 * then mark the data stream.  This should not happen
7535547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
7545547Swnj 		 * a FIN has been received from the remote side.
7555547Swnj 		 * In these states we ignore the URG.
7565547Swnj 		 */
7575547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
7585547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
7595547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
7605547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
7615547Swnj 			if (so->so_oobmark == 0)
7625547Swnj 				so->so_state |= SS_RCVATMARK;
7638313Sroot 			sohasoutofband(so);
76424816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
7655440Swnj 		}
7665547Swnj 		/*
7675547Swnj 		 * Remove out of band data so doesn't get presented to user.
7685547Swnj 		 * This can happen independent of advancing the URG pointer,
7695547Swnj 		 * but if two URG's are pending at once, some out-of-band
7705547Swnj 		 * data may creep in... ick.
7715547Swnj 		 */
7727510Sroot 		if (ti->ti_urp <= ti->ti_len)
7735547Swnj 			tcp_pulloutofband(so, ti);
7745419Swnj 	}
77513121Ssam badurp:							/* XXX */
7764601Swnj 
7774601Swnj 	/*
7785065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
7795065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
7805065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
7815065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
7825065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
7835065Swnj 	 * connection then we just ignore the text.
7844601Swnj 	 */
78517946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
78617946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
78724816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
7885440Swnj 		if (tcpnodelack == 0)
7895440Swnj 			tp->t_flags |= TF_DELACK;
7905440Swnj 		else
7915440Swnj 			tp->t_flags |= TF_ACKNOW;
7925244Sroot 	} else {
7934924Swnj 		m_freem(m);
7945263Swnj 		tiflags &= ~TH_FIN;
7955244Sroot 	}
7964601Swnj 
7974601Swnj 	/*
7985263Swnj 	 * If FIN is received ACK the FIN and let the user know
7995263Swnj 	 * that the connection is closing.
8004601Swnj 	 */
8015263Swnj 	if (tiflags & TH_FIN) {
8025244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8035244Sroot 			socantrcvmore(so);
8045244Sroot 			tp->t_flags |= TF_ACKNOW;
8055244Sroot 			tp->rcv_nxt++;
8065244Sroot 		}
8075065Swnj 		switch (tp->t_state) {
8084601Swnj 
8095065Swnj 	 	/*
8105065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
8115065Swnj 		 * enter the CLOSE_WAIT state.
8124884Swnj 		 */
8135065Swnj 		case TCPS_SYN_RECEIVED:
8145065Swnj 		case TCPS_ESTABLISHED:
8155065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
8165065Swnj 			break;
8174884Swnj 
8185065Swnj 	 	/*
8195085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
8205085Swnj 		 * enter the CLOSING state.
8214884Swnj 		 */
8225065Swnj 		case TCPS_FIN_WAIT_1:
8235085Swnj 			tp->t_state = TCPS_CLOSING;
8245065Swnj 			break;
8254601Swnj 
8265065Swnj 	 	/*
8275065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8285065Swnj 		 * starting the time-wait timer, turning off the other
8295065Swnj 		 * standard timers.
8305065Swnj 		 */
8315065Swnj 		case TCPS_FIN_WAIT_2:
8325244Sroot 			tp->t_state = TCPS_TIME_WAIT;
8335074Swnj 			tcp_canceltimers(tp);
8345162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8355244Sroot 			soisdisconnected(so);
8365065Swnj 			break;
8375065Swnj 
8384884Swnj 		/*
8395065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
8404884Swnj 		 */
8415065Swnj 		case TCPS_TIME_WAIT:
8425162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8435065Swnj 			break;
8445085Swnj 		}
8454601Swnj 	}
8465267Sroot 	if (so->so_options & SO_DEBUG)
8475267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
8485085Swnj 
8495085Swnj 	/*
8505085Swnj 	 * Return any desired output.
8515085Swnj 	 */
8526161Ssam 	(void) tcp_output(tp);
8535065Swnj 	return;
8545085Swnj 
8555065Swnj dropafterack:
8565085Swnj 	/*
8576211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
8586211Swnj 	 * sequence space, where the ACK reflects our state.
8595085Swnj 	 */
8606211Swnj 	if ((tiflags&TH_RST) ||
8616211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
8625085Swnj 		goto drop;
8636303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
8646303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
8655391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
8665231Swnj 	return;
8675085Swnj 
8685085Swnj dropwithreset:
86911731Ssam 	if (om) {
8706161Ssam 		(void) m_free(om);
87111731Ssam 		om = 0;
87211731Ssam 	}
8735085Swnj 	/*
8745244Sroot 	 * Generate a RST, dropping incoming segment.
8755085Swnj 	 * Make ACK acceptable to originator of segment.
8765085Swnj 	 */
8775085Swnj 	if (tiflags & TH_RST)
8785085Swnj 		goto drop;
8795085Swnj 	if (tiflags & TH_ACK)
8805391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
8815085Swnj 	else {
8825085Swnj 		if (tiflags & TH_SYN)
8835085Swnj 			ti->ti_len++;
8846211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
8856211Swnj 		    TH_RST|TH_ACK);
8865085Swnj 	}
88710769Ssam 	/* destroy temporarily created socket */
88810769Ssam 	if (dropsocket)
88910769Ssam 		(void) soabort(so);
8905231Swnj 	return;
8915085Swnj 
8925065Swnj drop:
89311730Ssam 	if (om)
89411730Ssam 		(void) m_free(om);
8955085Swnj 	/*
8965085Swnj 	 * Drop space held by incoming segment and return.
8975085Swnj 	 */
8986303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
8996303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
9005065Swnj 	m_freem(m);
90110769Ssam 	/* destroy temporarily created socket */
90210769Ssam 	if (dropsocket)
90310769Ssam 		(void) soabort(so);
9045267Sroot 	return;
9055065Swnj }
9065065Swnj 
90717272Skarels tcp_dooptions(tp, om, ti)
9085440Swnj 	struct tcpcb *tp;
9095440Swnj 	struct mbuf *om;
91017272Skarels 	struct tcpiphdr *ti;
9115419Swnj {
9125440Swnj 	register u_char *cp;
9135440Swnj 	int opt, optlen, cnt;
9145419Swnj 
9155440Swnj 	cp = mtod(om, u_char *);
9165440Swnj 	cnt = om->m_len;
9175440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
9185440Swnj 		opt = cp[0];
9195440Swnj 		if (opt == TCPOPT_EOL)
9205440Swnj 			break;
9215440Swnj 		if (opt == TCPOPT_NOP)
9225440Swnj 			optlen = 1;
92312169Ssam 		else {
9245440Swnj 			optlen = cp[1];
92512169Ssam 			if (optlen <= 0)
92612169Ssam 				break;
92712169Ssam 		}
9285440Swnj 		switch (opt) {
9295440Swnj 
9305440Swnj 		default:
9315440Swnj 			break;
9325440Swnj 
9335440Swnj 		case TCPOPT_MAXSEG:
9345440Swnj 			if (optlen != 4)
9355440Swnj 				continue;
93617272Skarels 			if (!(ti->ti_flags & TH_SYN))
93717272Skarels 				continue;
9385440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
9396161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
94017272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
9415440Swnj 			break;
9425419Swnj 		}
9435419Swnj 	}
9446161Ssam 	(void) m_free(om);
9455419Swnj }
9465419Swnj 
9475419Swnj /*
9485547Swnj  * Pull out of band byte out of a segment so
9495547Swnj  * it doesn't appear in the user's data queue.
9505547Swnj  * It is still reflected in the segment length for
9515547Swnj  * sequencing purposes.
9525547Swnj  */
9535547Swnj tcp_pulloutofband(so, ti)
9545547Swnj 	struct socket *so;
9555547Swnj 	struct tcpiphdr *ti;
9565547Swnj {
9575547Swnj 	register struct mbuf *m;
9586116Swnj 	int cnt = ti->ti_urp - 1;
9595547Swnj 
9605547Swnj 	m = dtom(ti);
9615547Swnj 	while (cnt >= 0) {
9625547Swnj 		if (m->m_len > cnt) {
9635547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
9645547Swnj 			struct tcpcb *tp = sototcpcb(so);
9655547Swnj 
9665547Swnj 			tp->t_iobc = *cp;
9675547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
9686161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
9695547Swnj 			m->m_len--;
9705547Swnj 			return;
9715547Swnj 		}
9725547Swnj 		cnt -= m->m_len;
9735547Swnj 		m = m->m_next;
9745547Swnj 		if (m == 0)
9755547Swnj 			break;
9765547Swnj 	}
9775547Swnj 	panic("tcp_pulloutofband");
9785547Swnj }
9795547Swnj 
9805547Swnj /*
98117272Skarels  *  Determine a reasonable value for maxseg size.
98217272Skarels  *  If the route is known, use one that can be handled
98317272Skarels  *  on the given interface without forcing IP to fragment.
98423975Skarels  *  If bigger than a page (CLBYTES), round down to nearest pagesize
98517272Skarels  *  to utilize pagesize mbufs.
98617272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
98723975Skarels  *  use a conservative size (512 or the default IP max size, but no more
98823975Skarels  *  than the mtu of the interface through which we route),
98917272Skarels  *  as we can't discover anything about intervening gateways or networks.
99017272Skarels  *
99117272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
99217272Skarels  */
99317272Skarels tcp_mss(tp)
99423975Skarels 	register struct tcpcb *tp;
99517272Skarels {
99617272Skarels 	struct route *ro;
99717272Skarels 	struct ifnet *ifp;
99817272Skarels 	int mss;
99917272Skarels 	struct inpcb *inp;
100017272Skarels 
100117272Skarels 	inp = tp->t_inpcb;
100217272Skarels 	ro = &inp->inp_route;
100317272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
100417272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
100517272Skarels 		/* No route yet, so try to acquire one */
100617272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
100717272Skarels 			ro->ro_dst.sa_family = AF_INET;
100817272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
100917272Skarels 				inp->inp_faddr;
101017272Skarels 			rtalloc(ro);
101117272Skarels 		}
101217272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
101317316Skarels 			return (TCP_MSS);
101417272Skarels 	}
101517272Skarels 
101617272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
101717272Skarels #if	(CLBYTES & (CLBYTES - 1)) == 0
101817272Skarels 	if (mss > CLBYTES)
101917272Skarels 		mss &= ~(CLBYTES-1);
102017272Skarels #else
102117272Skarels 	if (mss > CLBYTES)
102217272Skarels 		mss = mss / CLBYTES * CLBYTES;
102317272Skarels #endif
102423975Skarels 	if (in_localaddr(inp->inp_faddr))
102523975Skarels 		return (mss);
102617316Skarels 	return (MIN(mss, TCP_MSS));
102717272Skarels }
1028