xref: /csrg-svn/sys/netinet/tcp_input.c (revision 24816)
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*24816Skarels  *	@(#)tcp_input.c	6.13 (Berkeley) 09/16/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();
39*24816Skarels 
405065Swnj /*
41*24816Skarels  * Insert segment ti into reassembly queue of tcp with
42*24816Skarels  * control block tp.  Return TH_FIN if reassembly now includes
43*24816Skarels  * a segment with FIN.  The macro form does the common case inline
44*24816Skarels  * (segment is the next to be received on an established connection,
45*24816Skarels  * and the queue is empty), avoiding linkage into and removal
46*24816Skarels  * from the queue and repetition of various conversions.
47*24816Skarels  */
48*24816Skarels #define	TCP_REASS(tp, ti, m, so, flags) { \
49*24816Skarels 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
50*24816Skarels 	    (tp)->seg_next == (struct tcpiphdr *)(tp) && \
51*24816Skarels 	    (tp)->t_state == TCPS_ESTABLISHED) { \
52*24816Skarels 		(tp)->rcv_nxt += (ti)->ti_len; \
53*24816Skarels 		flags = (ti)->ti_flags & TH_FIN; \
54*24816Skarels 		sbappend(&(so)->so_rcv, (m)); \
55*24816Skarels 		sorwakeup(so); \
56*24816Skarels 	} else \
57*24816Skarels 		(flags) = tcp_reass((tp), (ti)); \
58*24816Skarels }
59*24816Skarels 
60*24816Skarels tcp_reass(tp, ti)
61*24816Skarels 	register struct tcpcb *tp;
62*24816Skarels 	register struct tcpiphdr *ti;
63*24816Skarels {
64*24816Skarels 	register struct tcpiphdr *q;
65*24816Skarels 	struct socket *so = tp->t_inpcb->inp_socket;
66*24816Skarels 	struct mbuf *m;
67*24816Skarels 	int flags;
68*24816Skarels 
69*24816Skarels 	/*
70*24816Skarels 	 * Call with ti==0 after become established to
71*24816Skarels 	 * force pre-ESTABLISHED data up to user socket.
72*24816Skarels 	 */
73*24816Skarels 	if (ti == 0)
74*24816Skarels 		goto present;
75*24816Skarels 
76*24816Skarels 	/*
77*24816Skarels 	 * Find a segment which begins after this one does.
78*24816Skarels 	 */
79*24816Skarels 	for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
80*24816Skarels 	    q = (struct tcpiphdr *)q->ti_next)
81*24816Skarels 		if (SEQ_GT(q->ti_seq, ti->ti_seq))
82*24816Skarels 			break;
83*24816Skarels 
84*24816Skarels 	/*
85*24816Skarels 	 * If there is a preceding segment, it may provide some of
86*24816Skarels 	 * our data already.  If so, drop the data from the incoming
87*24816Skarels 	 * segment.  If it provides all of our data, drop us.
88*24816Skarels 	 */
89*24816Skarels 	if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
90*24816Skarels 		register int i;
91*24816Skarels 		q = (struct tcpiphdr *)q->ti_prev;
92*24816Skarels 		/* conversion to int (in i) handles seq wraparound */
93*24816Skarels 		i = q->ti_seq + q->ti_len - ti->ti_seq;
94*24816Skarels 		if (i > 0) {
95*24816Skarels 			if (i >= ti->ti_len)
96*24816Skarels 				goto drop;
97*24816Skarels 			m_adj(dtom(ti), i);
98*24816Skarels 			ti->ti_len -= i;
99*24816Skarels 			ti->ti_seq += i;
100*24816Skarels 		}
101*24816Skarels 		q = (struct tcpiphdr *)(q->ti_next);
102*24816Skarels 	}
103*24816Skarels 
104*24816Skarels 	/*
105*24816Skarels 	 * While we overlap succeeding segments trim them or,
106*24816Skarels 	 * if they are completely covered, dequeue them.
107*24816Skarels 	 */
108*24816Skarels 	while (q != (struct tcpiphdr *)tp) {
109*24816Skarels 		register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
110*24816Skarels 		if (i <= 0)
111*24816Skarels 			break;
112*24816Skarels 		if (i < q->ti_len) {
113*24816Skarels 			q->ti_seq += i;
114*24816Skarels 			q->ti_len -= i;
115*24816Skarels 			m_adj(dtom(q), i);
116*24816Skarels 			break;
117*24816Skarels 		}
118*24816Skarels 		q = (struct tcpiphdr *)q->ti_next;
119*24816Skarels 		m = dtom(q->ti_prev);
120*24816Skarels 		remque(q->ti_prev);
121*24816Skarels 		m_freem(m);
122*24816Skarels 	}
123*24816Skarels 
124*24816Skarels 	/*
125*24816Skarels 	 * Stick new segment in its place.
126*24816Skarels 	 */
127*24816Skarels 	insque(ti, q->ti_prev);
128*24816Skarels 
129*24816Skarels present:
130*24816Skarels 	/*
131*24816Skarels 	 * Present data to user, advancing rcv_nxt through
132*24816Skarels 	 * completed sequence space.
133*24816Skarels 	 */
134*24816Skarels 	if (TCPS_HAVERCVDSYN(tp->t_state) == 0)
135*24816Skarels 		return (0);
136*24816Skarels 	ti = tp->seg_next;
137*24816Skarels 	if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
138*24816Skarels 		return (0);
139*24816Skarels 	if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
140*24816Skarels 		return (0);
141*24816Skarels 	do {
142*24816Skarels 		tp->rcv_nxt += ti->ti_len;
143*24816Skarels 		flags = ti->ti_flags & TH_FIN;
144*24816Skarels 		remque(ti);
145*24816Skarels 		m = dtom(ti);
146*24816Skarels 		ti = (struct tcpiphdr *)ti->ti_next;
147*24816Skarels 		if (so->so_state & SS_CANTRCVMORE)
148*24816Skarels 			m_freem(m);
149*24816Skarels 		else
150*24816Skarels 			sbappend(&so->so_rcv, m);
151*24816Skarels 	} while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
152*24816Skarels 	sorwakeup(so);
153*24816Skarels 	return (flags);
154*24816Skarels drop:
155*24816Skarels 	m_freem(dtom(ti));
156*24816Skarels 	return (0);
157*24816Skarels }
158*24816Skarels 
159*24816Skarels /*
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)) {
227*24816Skarels 		if (m->m_len < sizeof(struct ip) + off) {
228*24816Skarels 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
229*24816Skarels 				tcpstat.tcps_hdrops++;
230*24816Skarels 				return;
231*24816Skarels 			}
232*24816Skarels 			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;
302*24816Skarels 		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.
326*24816Skarels 	 * Receive window is amount of space in rcv queue,
327*24816Skarels 	 * but not less than advertised window.
3284601Swnj 	 */
329*24816Skarels 	tp->rcv_wnd = MAX(sbspace(&so->so_rcv), tp->rcv_adv - tp->rcv_nxt);
3305231Swnj 	if (tp->rcv_wnd < 0)
3315231Swnj 		tp->rcv_wnd = 0;
3324601Swnj 
3334601Swnj 	switch (tp->t_state) {
3344601Swnj 
3355065Swnj 	/*
3365065Swnj 	 * If the state is LISTEN then ignore segment if it contains an RST.
3375065Swnj 	 * If the segment contains an ACK then it is bad and send a RST.
3385065Swnj 	 * If it does not contain a SYN then it is not interesting; drop it.
3395085Swnj 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
3405065Swnj 	 * tp->iss, and send a segment:
3415085Swnj 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3425065Swnj 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3435065Swnj 	 * Fill in remote peer address fields if not previously specified.
3445065Swnj 	 * Enter SYN_RECEIVED state, and process any other fields of this
3455244Sroot 	 * segment in this state.
3465065Swnj 	 */
3478271Sroot 	case TCPS_LISTEN: {
34810145Ssam 		struct mbuf *am;
3498271Sroot 		register struct sockaddr_in *sin;
3508271Sroot 
3515065Swnj 		if (tiflags & TH_RST)
3525065Swnj 			goto drop;
3535300Sroot 		if (tiflags & TH_ACK)
3545085Swnj 			goto dropwithreset;
3555300Sroot 		if ((tiflags & TH_SYN) == 0)
3565065Swnj 			goto drop;
35710145Ssam 		am = m_get(M_DONTWAIT, MT_SONAME);
35810145Ssam 		if (am == NULL)
35910145Ssam 			goto drop;
36010145Ssam 		am->m_len = sizeof (struct sockaddr_in);
3618599Sroot 		sin = mtod(am, struct sockaddr_in *);
3628271Sroot 		sin->sin_family = AF_INET;
3638271Sroot 		sin->sin_addr = ti->ti_src;
3648271Sroot 		sin->sin_port = ti->ti_sport;
3656028Sroot 		laddr = inp->inp_laddr;
36610145Ssam 		if (inp->inp_laddr.s_addr == INADDR_ANY)
3676028Sroot 			inp->inp_laddr = ti->ti_dst;
3688599Sroot 		if (in_pcbconnect(inp, am)) {
3696028Sroot 			inp->inp_laddr = laddr;
3708716Sroot 			(void) m_free(am);
3715244Sroot 			goto drop;
3726028Sroot 		}
3738716Sroot 		(void) m_free(am);
3745244Sroot 		tp->t_template = tcp_template(tp);
3755244Sroot 		if (tp->t_template == 0) {
3765244Sroot 			in_pcbdisconnect(inp);
37717264Skarels 			dropsocket = 0;		/* socket is already gone */
3786320Swnj 			tp = 0;
3795244Sroot 			goto drop;
3805244Sroot 		}
38117272Skarels 		if (om) {
38217272Skarels 			tcp_dooptions(tp, om, ti);
38317272Skarels 			om = 0;
38417272Skarels 		}
3855085Swnj 		tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
3865065Swnj 		tp->irs = ti->ti_seq;
3875085Swnj 		tcp_sendseqinit(tp);
3885085Swnj 		tcp_rcvseqinit(tp);
3895065Swnj 		tp->t_state = TCPS_SYN_RECEIVED;
3905244Sroot 		tp->t_timer[TCPT_KEEP] = TCPTV_KEEP;
39110769Ssam 		dropsocket = 0;		/* committed to socket */
3925085Swnj 		goto trimthenstep6;
3938271Sroot 		}
3944601Swnj 
3955065Swnj 	/*
3965065Swnj 	 * If the state is SYN_SENT:
3975065Swnj 	 *	if seg contains an ACK, but not for our SYN, drop the input.
3985065Swnj 	 *	if seg contains a RST, then drop the connection.
3995065Swnj 	 *	if seg does not contain SYN, then drop it.
4005065Swnj 	 * Otherwise this is an acceptable SYN segment
4015065Swnj 	 *	initialize tp->rcv_nxt and tp->irs
4025065Swnj 	 *	if seg contains ack then advance tp->snd_una
4035065Swnj 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
4045065Swnj 	 *	arrange for segment to be acked (eventually)
4055065Swnj 	 *	continue processing rest of data/controls, beginning with URG
4065065Swnj 	 */
4075065Swnj 	case TCPS_SYN_SENT:
4085065Swnj 		if ((tiflags & TH_ACK) &&
409*24816Skarels 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
4105231Swnj 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
4115085Swnj 			goto dropwithreset;
4125065Swnj 		if (tiflags & TH_RST) {
41310394Ssam 			if (tiflags & TH_ACK)
41410394Ssam 				tp = tcp_drop(tp, ECONNREFUSED);
4155065Swnj 			goto drop;
4164601Swnj 		}
4175065Swnj 		if ((tiflags & TH_SYN) == 0)
4185065Swnj 			goto drop;
4195231Swnj 		tp->snd_una = ti->ti_ack;
4205357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
4215357Sroot 			tp->snd_nxt = tp->snd_una;
4225244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
4235065Swnj 		tp->irs = ti->ti_seq;
4245085Swnj 		tcp_rcvseqinit(tp);
4255085Swnj 		tp->t_flags |= TF_ACKNOW;
4265162Swnj 		if (SEQ_GT(tp->snd_una, tp->iss)) {
4275244Sroot 			soisconnected(so);
4285065Swnj 			tp->t_state = TCPS_ESTABLISHED;
42917272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
4305162Swnj 			(void) tcp_reass(tp, (struct tcpiphdr *)0);
4315162Swnj 		} else
4325085Swnj 			tp->t_state = TCPS_SYN_RECEIVED;
4335085Swnj 		goto trimthenstep6;
4345085Swnj 
4355085Swnj trimthenstep6:
4365085Swnj 		/*
4375231Swnj 		 * Advance ti->ti_seq to correspond to first data byte.
4385085Swnj 		 * If data, trim to stay within window,
4395085Swnj 		 * dropping FIN if necessary.
4405085Swnj 		 */
4415231Swnj 		ti->ti_seq++;
4425085Swnj 		if (ti->ti_len > tp->rcv_wnd) {
4435085Swnj 			todrop = ti->ti_len - tp->rcv_wnd;
4445085Swnj 			m_adj(m, -todrop);
4455085Swnj 			ti->ti_len = tp->rcv_wnd;
4465085Swnj 			ti->ti_flags &= ~TH_FIN;
4475065Swnj 		}
4485263Swnj 		tp->snd_wl1 = ti->ti_seq - 1;
4495085Swnj 		goto step6;
4505065Swnj 	}
4514601Swnj 
4525065Swnj 	/*
45316222Skarels 	 * If data is received on a connection after the
45416222Skarels 	 * user processes are gone, then RST the other end.
45516222Skarels 	 */
45616222Skarels 	if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT &&
45716222Skarels 	    ti->ti_len) {
45816222Skarels 		tp = tcp_close(tp);
45916222Skarels 		goto dropwithreset;
46016222Skarels 	}
46116222Skarels 
46216222Skarels 	/*
4635065Swnj 	 * States other than LISTEN or SYN_SENT.
4645065Swnj 	 * First check that at least some bytes of segment are within
4655065Swnj 	 * receive window.
4665065Swnj 	 */
4675065Swnj 	if (tp->rcv_wnd == 0) {
4685065Swnj 		/*
4695065Swnj 		 * If window is closed can only take segments at
4705231Swnj 		 * window edge, and have to drop data and PUSH from
4715065Swnj 		 * incoming segments.
4725065Swnj 		 */
4735300Sroot 		if (tp->rcv_nxt != ti->ti_seq)
4745065Swnj 			goto dropafterack;
4755085Swnj 		if (ti->ti_len > 0) {
4765690Swnj 			m_adj(m, ti->ti_len);
4775085Swnj 			ti->ti_len = 0;
4785085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
4795065Swnj 		}
4805065Swnj 	} else {
4815065Swnj 		/*
4825231Swnj 		 * If segment begins before rcv_nxt, drop leading
4835065Swnj 		 * data (and SYN); if nothing left, just ack.
4845065Swnj 		 */
4855690Swnj 		todrop = tp->rcv_nxt - ti->ti_seq;
4865690Swnj 		if (todrop > 0) {
4875085Swnj 			if (tiflags & TH_SYN) {
4885300Sroot 				tiflags &= ~TH_SYN;
4895690Swnj 				ti->ti_flags &= ~TH_SYN;
4905085Swnj 				ti->ti_seq++;
4915085Swnj 				if (ti->ti_urp > 1)
4925085Swnj 					ti->ti_urp--;
4935085Swnj 				else
4945085Swnj 					tiflags &= ~TH_URG;
4955085Swnj 				todrop--;
4965085Swnj 			}
4976211Swnj 			if (todrop > ti->ti_len ||
4986211Swnj 			    todrop == ti->ti_len && (tiflags&TH_FIN) == 0)
4995065Swnj 				goto dropafterack;
5005065Swnj 			m_adj(m, todrop);
5015065Swnj 			ti->ti_seq += todrop;
5025065Swnj 			ti->ti_len -= todrop;
5035085Swnj 			if (ti->ti_urp > todrop)
5045085Swnj 				ti->ti_urp -= todrop;
5055085Swnj 			else {
5065085Swnj 				tiflags &= ~TH_URG;
5075690Swnj 				ti->ti_flags &= ~TH_URG;
5085690Swnj 				ti->ti_urp = 0;
5095085Swnj 			}
5105065Swnj 		}
5115065Swnj 		/*
5125065Swnj 		 * If segment ends after window, drop trailing data
5135085Swnj 		 * (and PUSH and FIN); if nothing left, just ACK.
5145065Swnj 		 */
5155690Swnj 		todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
5165690Swnj 		if (todrop > 0) {
5176211Swnj 			if (todrop >= ti->ti_len)
5185065Swnj 				goto dropafterack;
5195065Swnj 			m_adj(m, -todrop);
5205065Swnj 			ti->ti_len -= todrop;
5215085Swnj 			ti->ti_flags &= ~(TH_PUSH|TH_FIN);
5225065Swnj 		}
5235065Swnj 	}
5244601Swnj 
5255065Swnj 	/*
5265065Swnj 	 * If the RST bit is set examine the state:
5275065Swnj 	 *    SYN_RECEIVED STATE:
5285065Swnj 	 *	If passive open, return to LISTEN state.
5295065Swnj 	 *	If active open, inform user that connection was refused.
5305065Swnj 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
5315065Swnj 	 *	Inform user that connection was reset, and close tcb.
5325065Swnj 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
5335065Swnj 	 *	Close the tcb.
5345065Swnj 	 */
5355065Swnj 	if (tiflags&TH_RST) switch (tp->t_state) {
5365267Sroot 
5375065Swnj 	case TCPS_SYN_RECEIVED:
53810394Ssam 		tp = tcp_drop(tp, ECONNREFUSED);
5395065Swnj 		goto drop;
5404601Swnj 
5415065Swnj 	case TCPS_ESTABLISHED:
5425065Swnj 	case TCPS_FIN_WAIT_1:
5435065Swnj 	case TCPS_FIN_WAIT_2:
5445065Swnj 	case TCPS_CLOSE_WAIT:
54510394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5465065Swnj 		goto drop;
5475065Swnj 
5485065Swnj 	case TCPS_CLOSING:
5495065Swnj 	case TCPS_LAST_ACK:
5505065Swnj 	case TCPS_TIME_WAIT:
55110394Ssam 		tp = tcp_close(tp);
5525065Swnj 		goto drop;
5534601Swnj 	}
5544601Swnj 
5554601Swnj 	/*
5565065Swnj 	 * If a SYN is in the window, then this is an
5575065Swnj 	 * error and we send an RST and drop the connection.
5584601Swnj 	 */
5595065Swnj 	if (tiflags & TH_SYN) {
56010394Ssam 		tp = tcp_drop(tp, ECONNRESET);
5615085Swnj 		goto dropwithreset;
5624601Swnj 	}
5634601Swnj 
5644601Swnj 	/*
5655065Swnj 	 * If the ACK bit is off we drop the segment and return.
5664601Swnj 	 */
5675085Swnj 	if ((tiflags & TH_ACK) == 0)
5685065Swnj 		goto drop;
5695065Swnj 
5705065Swnj 	/*
5715065Swnj 	 * Ack processing.
5725065Swnj 	 */
5734601Swnj 	switch (tp->t_state) {
5744601Swnj 
5755065Swnj 	/*
5765065Swnj 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
5775065Swnj 	 * ESTABLISHED state and continue processing, othewise
5785065Swnj 	 * send an RST.
5795065Swnj 	 */
5805065Swnj 	case TCPS_SYN_RECEIVED:
5815085Swnj 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
5825231Swnj 		    SEQ_GT(ti->ti_ack, tp->snd_max))
5835085Swnj 			goto dropwithreset;
5845244Sroot 		tp->snd_una++;			/* SYN acked */
5855357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
5865357Sroot 			tp->snd_nxt = tp->snd_una;
5875244Sroot 		tp->t_timer[TCPT_REXMT] = 0;
5885085Swnj 		soisconnected(so);
5895085Swnj 		tp->t_state = TCPS_ESTABLISHED;
59017272Skarels 		tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
5915162Swnj 		(void) tcp_reass(tp, (struct tcpiphdr *)0);
5925244Sroot 		tp->snd_wl1 = ti->ti_seq - 1;
5935085Swnj 		/* fall into ... */
5944601Swnj 
5955065Swnj 	/*
5965065Swnj 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
5975065Swnj 	 * ACKs.  If the ack is in the range
5985231Swnj 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
5995065Swnj 	 * then advance tp->snd_una to ti->ti_ack and drop
6005065Swnj 	 * data from the retransmission queue.  If this ACK reflects
6015065Swnj 	 * more up to date window information we update our window information.
6025065Swnj 	 */
6035065Swnj 	case TCPS_ESTABLISHED:
6045065Swnj 	case TCPS_FIN_WAIT_1:
6055065Swnj 	case TCPS_FIN_WAIT_2:
6065065Swnj 	case TCPS_CLOSE_WAIT:
6075065Swnj 	case TCPS_CLOSING:
6085244Sroot 	case TCPS_LAST_ACK:
6095244Sroot 	case TCPS_TIME_WAIT:
6105085Swnj #define	ourfinisacked	(acked > 0)
6115085Swnj 
6125244Sroot 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una))
6135065Swnj 			break;
6145300Sroot 		if (SEQ_GT(ti->ti_ack, tp->snd_max))
6155065Swnj 			goto dropafterack;
6165085Swnj 		acked = ti->ti_ack - tp->snd_una;
6175951Swnj 
6185951Swnj 		/*
6195951Swnj 		 * If transmit timer is running and timed sequence
6205951Swnj 		 * number was acked, update smoothed round trip time.
6215951Swnj 		 */
6225951Swnj 		if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
6235951Swnj 			if (tp->t_srtt == 0)
6245951Swnj 				tp->t_srtt = tp->t_rtt;
6255951Swnj 			else
6265951Swnj 				tp->t_srtt =
6275951Swnj 				    tcp_alpha * tp->t_srtt +
6285951Swnj 				    (1 - tcp_alpha) * tp->t_rtt;
6295951Swnj 			tp->t_rtt = 0;
6305951Swnj 		}
6315951Swnj 
6325307Sroot 		if (ti->ti_ack == tp->snd_max)
6335244Sroot 			tp->t_timer[TCPT_REXMT] = 0;
6345307Sroot 		else {
6355244Sroot 			TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
6365244Sroot 			    tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
6375300Sroot 			tp->t_rxtshift = 0;
6385085Swnj 		}
63917360Skarels 		/*
64017360Skarels 		 * When new data is acked, open the congestion window a bit.
64117360Skarels 		 */
64217360Skarels 		if (acked > 0)
64317360Skarels 			tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535);
6445307Sroot 		if (acked > so->so_snd.sb_cc) {
64515386Ssam 			tp->snd_wnd -= so->so_snd.sb_cc;
6465307Sroot 			sbdrop(&so->so_snd, so->so_snd.sb_cc);
6475307Sroot 		} else {
6486161Ssam 			sbdrop(&so->so_snd, acked);
6495307Sroot 			tp->snd_wnd -= acked;
6505307Sroot 			acked = 0;
6515307Sroot 		}
6526434Swnj 		if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel)
6535300Sroot 			sowwakeup(so);
6545231Swnj 		tp->snd_una = ti->ti_ack;
6555357Sroot 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
6565357Sroot 			tp->snd_nxt = tp->snd_una;
6575162Swnj 
6584601Swnj 		switch (tp->t_state) {
6594601Swnj 
6605065Swnj 		/*
6615065Swnj 		 * In FIN_WAIT_1 STATE in addition to the processing
6625065Swnj 		 * for the ESTABLISHED state if our FIN is now acknowledged
6635085Swnj 		 * then enter FIN_WAIT_2.
6645065Swnj 		 */
6655065Swnj 		case TCPS_FIN_WAIT_1:
6665896Swnj 			if (ourfinisacked) {
6675896Swnj 				/*
6685896Swnj 				 * If we can't receive any more
6695896Swnj 				 * data, then closing user can proceed.
670*24816Skarels 				 * Starting the timer is contrary to the
671*24816Skarels 				 * specification, but if we don't get a FIN
672*24816Skarels 				 * we'll hang forever.
6735896Swnj 				 */
674*24816Skarels 				if (so->so_state & SS_CANTRCVMORE) {
6755896Swnj 					soisdisconnected(so);
676*24816Skarels 					tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE;
677*24816Skarels 				}
6785085Swnj 				tp->t_state = TCPS_FIN_WAIT_2;
6795896Swnj 			}
6804601Swnj 			break;
6814601Swnj 
6825065Swnj 	 	/*
6835065Swnj 		 * In CLOSING STATE in addition to the processing for
6845065Swnj 		 * the ESTABLISHED state if the ACK acknowledges our FIN
6855065Swnj 		 * then enter the TIME-WAIT state, otherwise ignore
6865065Swnj 		 * the segment.
6875065Swnj 		 */
6885065Swnj 		case TCPS_CLOSING:
6895244Sroot 			if (ourfinisacked) {
6905065Swnj 				tp->t_state = TCPS_TIME_WAIT;
6915244Sroot 				tcp_canceltimers(tp);
6925244Sroot 				tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
6935244Sroot 				soisdisconnected(so);
6945244Sroot 			}
6955244Sroot 			break;
6964601Swnj 
6975065Swnj 		/*
6985085Swnj 		 * The only thing that can arrive in  LAST_ACK state
6995085Swnj 		 * is an acknowledgment of our FIN.  If our FIN is now
7005085Swnj 		 * acknowledged, delete the TCB, enter the closed state
7015085Swnj 		 * and return.
7025065Swnj 		 */
7035065Swnj 		case TCPS_LAST_ACK:
70410394Ssam 			if (ourfinisacked)
70510394Ssam 				tp = tcp_close(tp);
7065065Swnj 			goto drop;
7074601Swnj 
7085065Swnj 		/*
7095065Swnj 		 * In TIME_WAIT state the only thing that should arrive
7105065Swnj 		 * is a retransmission of the remote FIN.  Acknowledge
7115065Swnj 		 * it and restart the finack timer.
7125065Swnj 		 */
7135065Swnj 		case TCPS_TIME_WAIT:
7145162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
7155065Swnj 			goto dropafterack;
7164601Swnj 		}
7175085Swnj #undef ourfinisacked
7185085Swnj 	}
7194601Swnj 
7205065Swnj step6:
7215065Swnj 	/*
7225244Sroot 	 * Update window information.
7235244Sroot 	 */
7245300Sroot 	if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
7255391Swnj 	    (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
7265300Sroot 	     tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
7275244Sroot 		tp->snd_wnd = ti->ti_win;
7285244Sroot 		tp->snd_wl1 = ti->ti_seq;
7295244Sroot 		tp->snd_wl2 = ti->ti_ack;
7305244Sroot 	}
7315244Sroot 
7325244Sroot 	/*
7335547Swnj 	 * Process segments with URG.
7345065Swnj 	 */
7357267Swnj 	if ((tiflags & TH_URG) && ti->ti_urp &&
7367267Swnj 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
7375547Swnj 		/*
73813121Ssam 		 * This is a kludge, but if we receive accept
73913121Ssam 		 * random urgent pointers, we'll crash in
74013121Ssam 		 * soreceive.  It's hard to imagine someone
74113121Ssam 		 * actually wanting to send this much urgent data.
74212441Ssam 		 */
74317360Skarels 		if (ti->ti_urp + (unsigned) so->so_rcv.sb_cc > 32767) {
74412441Ssam 			ti->ti_urp = 0;			/* XXX */
74512441Ssam 			tiflags &= ~TH_URG;		/* XXX */
74612441Ssam 			ti->ti_flags &= ~TH_URG;	/* XXX */
74713121Ssam 			goto badurp;			/* XXX */
74812441Ssam 		}
74912441Ssam 		/*
7505547Swnj 		 * If this segment advances the known urgent pointer,
7515547Swnj 		 * then mark the data stream.  This should not happen
7525547Swnj 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
7535547Swnj 		 * a FIN has been received from the remote side.
7545547Swnj 		 * In these states we ignore the URG.
7555547Swnj 		 */
7565547Swnj 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
7575547Swnj 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
7585547Swnj 			so->so_oobmark = so->so_rcv.sb_cc +
7595547Swnj 			    (tp->rcv_up - tp->rcv_nxt) - 1;
7605547Swnj 			if (so->so_oobmark == 0)
7615547Swnj 				so->so_state |= SS_RCVATMARK;
7628313Sroot 			sohasoutofband(so);
763*24816Skarels 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
7645440Swnj 		}
7655547Swnj 		/*
7665547Swnj 		 * Remove out of band data so doesn't get presented to user.
7675547Swnj 		 * This can happen independent of advancing the URG pointer,
7685547Swnj 		 * but if two URG's are pending at once, some out-of-band
7695547Swnj 		 * data may creep in... ick.
7705547Swnj 		 */
7717510Sroot 		if (ti->ti_urp <= ti->ti_len)
7725547Swnj 			tcp_pulloutofband(so, ti);
7735419Swnj 	}
77413121Ssam badurp:							/* XXX */
7754601Swnj 
7764601Swnj 	/*
7775065Swnj 	 * Process the segment text, merging it into the TCP sequencing queue,
7785065Swnj 	 * and arranging for acknowledgment of receipt if necessary.
7795065Swnj 	 * This process logically involves adjusting tp->rcv_wnd as data
7805065Swnj 	 * is presented to the user (this happens in tcp_usrreq.c,
7815065Swnj 	 * case PRU_RCVD).  If a FIN has already been received on this
7825065Swnj 	 * connection then we just ignore the text.
7834601Swnj 	 */
78417946Skarels 	if ((ti->ti_len || (tiflags&TH_FIN)) &&
78517946Skarels 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
786*24816Skarels 		TCP_REASS(tp, ti, m, so, tiflags);
7875440Swnj 		if (tcpnodelack == 0)
7885440Swnj 			tp->t_flags |= TF_DELACK;
7895440Swnj 		else
7905440Swnj 			tp->t_flags |= TF_ACKNOW;
7915244Sroot 	} else {
7924924Swnj 		m_freem(m);
7935263Swnj 		tiflags &= ~TH_FIN;
7945244Sroot 	}
7954601Swnj 
7964601Swnj 	/*
7975263Swnj 	 * If FIN is received ACK the FIN and let the user know
7985263Swnj 	 * that the connection is closing.
7994601Swnj 	 */
8005263Swnj 	if (tiflags & TH_FIN) {
8015244Sroot 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8025244Sroot 			socantrcvmore(so);
8035244Sroot 			tp->t_flags |= TF_ACKNOW;
8045244Sroot 			tp->rcv_nxt++;
8055244Sroot 		}
8065065Swnj 		switch (tp->t_state) {
8074601Swnj 
8085065Swnj 	 	/*
8095065Swnj 		 * In SYN_RECEIVED and ESTABLISHED STATES
8105065Swnj 		 * enter the CLOSE_WAIT state.
8114884Swnj 		 */
8125065Swnj 		case TCPS_SYN_RECEIVED:
8135065Swnj 		case TCPS_ESTABLISHED:
8145065Swnj 			tp->t_state = TCPS_CLOSE_WAIT;
8155065Swnj 			break;
8164884Swnj 
8175065Swnj 	 	/*
8185085Swnj 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
8195085Swnj 		 * enter the CLOSING state.
8204884Swnj 		 */
8215065Swnj 		case TCPS_FIN_WAIT_1:
8225085Swnj 			tp->t_state = TCPS_CLOSING;
8235065Swnj 			break;
8244601Swnj 
8255065Swnj 	 	/*
8265065Swnj 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
8275065Swnj 		 * starting the time-wait timer, turning off the other
8285065Swnj 		 * standard timers.
8295065Swnj 		 */
8305065Swnj 		case TCPS_FIN_WAIT_2:
8315244Sroot 			tp->t_state = TCPS_TIME_WAIT;
8325074Swnj 			tcp_canceltimers(tp);
8335162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8345244Sroot 			soisdisconnected(so);
8355065Swnj 			break;
8365065Swnj 
8374884Swnj 		/*
8385065Swnj 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
8394884Swnj 		 */
8405065Swnj 		case TCPS_TIME_WAIT:
8415162Swnj 			tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
8425065Swnj 			break;
8435085Swnj 		}
8444601Swnj 	}
8455267Sroot 	if (so->so_options & SO_DEBUG)
8465267Sroot 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
8475085Swnj 
8485085Swnj 	/*
8495085Swnj 	 * Return any desired output.
8505085Swnj 	 */
8516161Ssam 	(void) tcp_output(tp);
8525065Swnj 	return;
8535085Swnj 
8545065Swnj dropafterack:
8555085Swnj 	/*
8566211Swnj 	 * Generate an ACK dropping incoming segment if it occupies
8576211Swnj 	 * sequence space, where the ACK reflects our state.
8585085Swnj 	 */
8596211Swnj 	if ((tiflags&TH_RST) ||
8606211Swnj 	    tlen == 0 && (tiflags&(TH_SYN|TH_FIN)) == 0)
8615085Swnj 		goto drop;
8626303Sroot 	if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)
8636303Sroot 		tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0);
8645391Swnj 	tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK);
8655231Swnj 	return;
8665085Swnj 
8675085Swnj dropwithreset:
86811731Ssam 	if (om) {
8696161Ssam 		(void) m_free(om);
87011731Ssam 		om = 0;
87111731Ssam 	}
8725085Swnj 	/*
8735244Sroot 	 * Generate a RST, dropping incoming segment.
8745085Swnj 	 * Make ACK acceptable to originator of segment.
8755085Swnj 	 */
8765085Swnj 	if (tiflags & TH_RST)
8775085Swnj 		goto drop;
8785085Swnj 	if (tiflags & TH_ACK)
8795391Swnj 		tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST);
8805085Swnj 	else {
8815085Swnj 		if (tiflags & TH_SYN)
8825085Swnj 			ti->ti_len++;
8836211Swnj 		tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0,
8846211Swnj 		    TH_RST|TH_ACK);
8855085Swnj 	}
88610769Ssam 	/* destroy temporarily created socket */
88710769Ssam 	if (dropsocket)
88810769Ssam 		(void) soabort(so);
8895231Swnj 	return;
8905085Swnj 
8915065Swnj drop:
89211730Ssam 	if (om)
89311730Ssam 		(void) m_free(om);
8945085Swnj 	/*
8955085Swnj 	 * Drop space held by incoming segment and return.
8965085Swnj 	 */
8976303Sroot 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
8986303Sroot 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
8995065Swnj 	m_freem(m);
90010769Ssam 	/* destroy temporarily created socket */
90110769Ssam 	if (dropsocket)
90210769Ssam 		(void) soabort(so);
9035267Sroot 	return;
9045065Swnj }
9055065Swnj 
90617272Skarels tcp_dooptions(tp, om, ti)
9075440Swnj 	struct tcpcb *tp;
9085440Swnj 	struct mbuf *om;
90917272Skarels 	struct tcpiphdr *ti;
9105419Swnj {
9115440Swnj 	register u_char *cp;
9125440Swnj 	int opt, optlen, cnt;
9135419Swnj 
9145440Swnj 	cp = mtod(om, u_char *);
9155440Swnj 	cnt = om->m_len;
9165440Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
9175440Swnj 		opt = cp[0];
9185440Swnj 		if (opt == TCPOPT_EOL)
9195440Swnj 			break;
9205440Swnj 		if (opt == TCPOPT_NOP)
9215440Swnj 			optlen = 1;
92212169Ssam 		else {
9235440Swnj 			optlen = cp[1];
92412169Ssam 			if (optlen <= 0)
92512169Ssam 				break;
92612169Ssam 		}
9275440Swnj 		switch (opt) {
9285440Swnj 
9295440Swnj 		default:
9305440Swnj 			break;
9315440Swnj 
9325440Swnj 		case TCPOPT_MAXSEG:
9335440Swnj 			if (optlen != 4)
9345440Swnj 				continue;
93517272Skarels 			if (!(ti->ti_flags & TH_SYN))
93617272Skarels 				continue;
9375440Swnj 			tp->t_maxseg = *(u_short *)(cp + 2);
9386161Ssam 			tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
93917272Skarels 			tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp));
9405440Swnj 			break;
9415419Swnj 		}
9425419Swnj 	}
9436161Ssam 	(void) m_free(om);
9445419Swnj }
9455419Swnj 
9465419Swnj /*
9475547Swnj  * Pull out of band byte out of a segment so
9485547Swnj  * it doesn't appear in the user's data queue.
9495547Swnj  * It is still reflected in the segment length for
9505547Swnj  * sequencing purposes.
9515547Swnj  */
9525547Swnj tcp_pulloutofband(so, ti)
9535547Swnj 	struct socket *so;
9545547Swnj 	struct tcpiphdr *ti;
9555547Swnj {
9565547Swnj 	register struct mbuf *m;
9576116Swnj 	int cnt = ti->ti_urp - 1;
9585547Swnj 
9595547Swnj 	m = dtom(ti);
9605547Swnj 	while (cnt >= 0) {
9615547Swnj 		if (m->m_len > cnt) {
9625547Swnj 			char *cp = mtod(m, caddr_t) + cnt;
9635547Swnj 			struct tcpcb *tp = sototcpcb(so);
9645547Swnj 
9655547Swnj 			tp->t_iobc = *cp;
9665547Swnj 			tp->t_oobflags |= TCPOOB_HAVEDATA;
9676161Ssam 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
9685547Swnj 			m->m_len--;
9695547Swnj 			return;
9705547Swnj 		}
9715547Swnj 		cnt -= m->m_len;
9725547Swnj 		m = m->m_next;
9735547Swnj 		if (m == 0)
9745547Swnj 			break;
9755547Swnj 	}
9765547Swnj 	panic("tcp_pulloutofband");
9775547Swnj }
9785547Swnj 
9795547Swnj /*
98017272Skarels  *  Determine a reasonable value for maxseg size.
98117272Skarels  *  If the route is known, use one that can be handled
98217272Skarels  *  on the given interface without forcing IP to fragment.
98323975Skarels  *  If bigger than a page (CLBYTES), round down to nearest pagesize
98417272Skarels  *  to utilize pagesize mbufs.
98517272Skarels  *  If interface pointer is unavailable, or the destination isn't local,
98623975Skarels  *  use a conservative size (512 or the default IP max size, but no more
98723975Skarels  *  than the mtu of the interface through which we route),
98817272Skarels  *  as we can't discover anything about intervening gateways or networks.
98917272Skarels  *
99017272Skarels  *  This is ugly, and doesn't belong at this level, but has to happen somehow.
99117272Skarels  */
99217272Skarels tcp_mss(tp)
99323975Skarels 	register struct tcpcb *tp;
99417272Skarels {
99517272Skarels 	struct route *ro;
99617272Skarels 	struct ifnet *ifp;
99717272Skarels 	int mss;
99817272Skarels 	struct inpcb *inp;
99917272Skarels 
100017272Skarels 	inp = tp->t_inpcb;
100117272Skarels 	ro = &inp->inp_route;
100217272Skarels 	if ((ro->ro_rt == (struct rtentry *)0) ||
100317272Skarels 	    (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
100417272Skarels 		/* No route yet, so try to acquire one */
100517272Skarels 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
100617272Skarels 			ro->ro_dst.sa_family = AF_INET;
100717272Skarels 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
100817272Skarels 				inp->inp_faddr;
100917272Skarels 			rtalloc(ro);
101017272Skarels 		}
101117272Skarels 		if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0)
101217316Skarels 			return (TCP_MSS);
101317272Skarels 	}
101417272Skarels 
101517272Skarels 	mss = ifp->if_mtu - sizeof(struct tcpiphdr);
101617272Skarels #if	(CLBYTES & (CLBYTES - 1)) == 0
101717272Skarels 	if (mss > CLBYTES)
101817272Skarels 		mss &= ~(CLBYTES-1);
101917272Skarels #else
102017272Skarels 	if (mss > CLBYTES)
102117272Skarels 		mss = mss / CLBYTES * CLBYTES;
102217272Skarels #endif
102323975Skarels 	if (in_localaddr(inp->inp_faddr))
102423975Skarels 		return (mss);
102517316Skarels 	return (MIN(mss, TCP_MSS));
102617272Skarels }
1027