xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 57433)
123193Smckusick /*
244378Skarels  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
332789Sbostic  * All rights reserved.
423193Smckusick  *
544488Sbostic  * %sccs.include.redist.c%
632789Sbostic  *
7*57433Sandrew  *	@(#)tcp_subr.c	7.25 (Berkeley) 01/08/93
823193Smckusick  */
95068Swnj 
1056564Ssklower #include <sys/param.h>
1156564Ssklower #include <sys/proc.h>
1256564Ssklower #include <sys/systm.h>
1356564Ssklower #include <sys/malloc.h>
1456564Ssklower #include <sys/mbuf.h>
1556564Ssklower #include <sys/socket.h>
1656564Ssklower #include <sys/socketvar.h>
1756564Ssklower #include <sys/protosw.h>
1856564Ssklower #include <sys/errno.h>
1910896Ssam 
2056531Sbostic #include <net/route.h>
2156531Sbostic #include <net/if.h>
2210896Ssam 
2356531Sbostic #include <netinet/in.h>
2456531Sbostic #include <netinet/in_systm.h>
2556531Sbostic #include <netinet/ip.h>
2656531Sbostic #include <netinet/in_pcb.h>
2756531Sbostic #include <netinet/ip_var.h>
2856531Sbostic #include <netinet/ip_icmp.h>
2956531Sbostic #include <netinet/tcp.h>
3056531Sbostic #include <netinet/tcp_fsm.h>
3156531Sbostic #include <netinet/tcp_seq.h>
3256531Sbostic #include <netinet/tcp_timer.h>
3356531Sbostic #include <netinet/tcp_var.h>
3456531Sbostic #include <netinet/tcpip.h>
355068Swnj 
3644378Skarels /* patchable/settable parameters for tcp */
3731395Skarels int	tcp_ttl = TCP_TTL;
3844378Skarels int 	tcp_mssdflt = TCP_MSS;
3944378Skarels int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
40*57433Sandrew int	tcp_do_rfc1323 = 1;
4131395Skarels 
4244378Skarels extern	struct inpcb *tcp_last_inpcb;
4344378Skarels 
445068Swnj /*
455068Swnj  * Tcp initialization
465068Swnj  */
475068Swnj tcp_init()
485068Swnj {
495068Swnj 
505068Swnj 	tcp_iss = 1;		/* wrong */
515068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
5240691Skarels 	if (max_protohdr < sizeof(struct tcpiphdr))
5340691Skarels 		max_protohdr = sizeof(struct tcpiphdr);
5440691Skarels 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
5540691Skarels 		panic("tcp_init");
565068Swnj }
575068Swnj 
585068Swnj /*
595068Swnj  * Create template to be used to send tcp packets on a connection.
605068Swnj  * Call after host entry created, allocates an mbuf and fills
615068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
625068Swnj  * necessary when the connection is used.
635068Swnj  */
645068Swnj struct tcpiphdr *
655068Swnj tcp_template(tp)
665068Swnj 	struct tcpcb *tp;
675068Swnj {
685068Swnj 	register struct inpcb *inp = tp->t_inpcb;
695068Swnj 	register struct mbuf *m;
705068Swnj 	register struct tcpiphdr *n;
715068Swnj 
7226815Skarels 	if ((n = tp->t_template) == 0) {
7332101Skarels 		m = m_get(M_DONTWAIT, MT_HEADER);
7426815Skarels 		if (m == NULL)
7526815Skarels 			return (0);
7626815Skarels 		m->m_len = sizeof (struct tcpiphdr);
7726815Skarels 		n = mtod(m, struct tcpiphdr *);
7826815Skarels 	}
795068Swnj 	n->ti_next = n->ti_prev = 0;
805068Swnj 	n->ti_x1 = 0;
815068Swnj 	n->ti_pr = IPPROTO_TCP;
825068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
835068Swnj 	n->ti_src = inp->inp_laddr;
845068Swnj 	n->ti_dst = inp->inp_faddr;
855068Swnj 	n->ti_sport = inp->inp_lport;
865068Swnj 	n->ti_dport = inp->inp_fport;
875068Swnj 	n->ti_seq = 0;
885089Swnj 	n->ti_ack = 0;
895068Swnj 	n->ti_x2 = 0;
905068Swnj 	n->ti_off = 5;
915068Swnj 	n->ti_flags = 0;
925068Swnj 	n->ti_win = 0;
935068Swnj 	n->ti_sum = 0;
945068Swnj 	n->ti_urp = 0;
955068Swnj 	return (n);
965068Swnj }
975068Swnj 
985068Swnj /*
995164Swnj  * Send a single message to the TCP at address specified by
10044378Skarels  * the given TCP/IP header.  If m == 0, then we make a copy
1015164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
1025164Swnj  * This is used to force keep alive messages out using the TCP
1035164Swnj  * template for a connection tp->t_template.  If flags are given
1045164Swnj  * then we send a message back to the TCP which originated the
1055164Swnj  * segment ti, and discard the mbuf containing it and any other
1065164Swnj  * attached mbufs.
1075164Swnj  *
1085164Swnj  * In any case the ack and sequence number of the transmitted
1095164Swnj  * segment are as specified by the parameters.
1105068Swnj  */
11140691Skarels tcp_respond(tp, ti, m, ack, seq, flags)
1125392Swnj 	struct tcpcb *tp;
1135068Swnj 	register struct tcpiphdr *ti;
11440691Skarels 	register struct mbuf *m;
1155089Swnj 	tcp_seq ack, seq;
1165068Swnj 	int flags;
1175068Swnj {
11844966Skarels 	register int tlen;
11944966Skarels 	int win = 0;
1206353Ssam 	struct route *ro = 0;
1215068Swnj 
1226353Ssam 	if (tp) {
1235392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1246353Ssam 		ro = &tp->t_inpcb->inp_route;
1256353Ssam 	}
12640691Skarels 	if (m == 0) {
12740691Skarels 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
12810144Ssam 		if (m == NULL)
1295164Swnj 			return;
13031727Skarels #ifdef TCP_COMPAT_42
13131727Skarels 		tlen = 1;
13231727Skarels #else
13331727Skarels 		tlen = 0;
13431727Skarels #endif
13540691Skarels 		m->m_data += max_linkhdr;
1365164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1375164Swnj 		ti = mtod(m, struct tcpiphdr *);
1385164Swnj 		flags = TH_ACK;
1395164Swnj 	} else {
1405164Swnj 		m_freem(m->m_next);
1415164Swnj 		m->m_next = 0;
14240691Skarels 		m->m_data = (caddr_t)ti;
14344378Skarels 		m->m_len = sizeof (struct tcpiphdr);
14430762Skarels 		tlen = 0;
1455089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1465164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1475164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1485068Swnj #undef xchg
1495164Swnj 	}
15044966Skarels 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
15144966Skarels 	tlen += sizeof (struct tcpiphdr);
15244966Skarels 	m->m_len = tlen;
15344966Skarels 	m->m_pkthdr.len = tlen;
15444966Skarels 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
1555089Swnj 	ti->ti_next = ti->ti_prev = 0;
1565089Swnj 	ti->ti_x1 = 0;
1578942Sroot 	ti->ti_seq = htonl(seq);
1588942Sroot 	ti->ti_ack = htonl(ack);
1595089Swnj 	ti->ti_x2 = 0;
1605089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1615068Swnj 	ti->ti_flags = flags;
162*57433Sandrew 	if (tp)
163*57433Sandrew 		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
164*57433Sandrew 	else
165*57433Sandrew 		ti->ti_win = htons((u_short)win);
1665392Swnj 	ti->ti_urp = 0;
167*57433Sandrew 	ti->ti_sum = 0;
16844966Skarels 	ti->ti_sum = in_cksum(m, tlen);
16944966Skarels 	((struct ip *)ti)->ip_len = tlen;
17031395Skarels 	((struct ip *)ti)->ip_ttl = tcp_ttl;
1716353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1725068Swnj }
1735075Swnj 
1745089Swnj /*
1755089Swnj  * Create a new TCP control block, making an
1765089Swnj  * empty reassembly queue and hooking it to the argument
1775089Swnj  * protocol control block.
1785089Swnj  */
1795075Swnj struct tcpcb *
1805075Swnj tcp_newtcpcb(inp)
1815075Swnj 	struct inpcb *inp;
1825075Swnj {
1835075Swnj 	register struct tcpcb *tp;
1845075Swnj 
185*57433Sandrew 	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
186*57433Sandrew 	if (tp == NULL)
18710144Ssam 		return ((struct tcpcb *)0);
188*57433Sandrew 	bzero((char *) tp, sizeof(struct tcpcb));
1895075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
19044378Skarels 	tp->t_maxseg = tcp_mssdflt;
19144378Skarels 
192*57433Sandrew 	tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
1935075Swnj 	tp->t_inpcb = inp;
19431726Skarels 	/*
19531757Skarels 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
19631757Skarels 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
19731757Skarels 	 * reasonable initial retransmit time.
19831726Skarels 	 */
19931757Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
20044378Skarels 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
20144378Skarels 	tp->t_rttmin = TCPTV_MIN;
20232374Skarels 	TCPT_RANGESET(tp->t_rxtcur,
20332374Skarels 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
20432374Skarels 	    TCPTV_MIN, TCPTV_REXMTMAX);
205*57433Sandrew 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
206*57433Sandrew 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
20744378Skarels 	inp->inp_ip.ip_ttl = tcp_ttl;
2085075Swnj 	inp->inp_ppcb = (caddr_t)tp;
2095075Swnj 	return (tp);
2105075Swnj }
2115075Swnj 
2125089Swnj /*
2135089Swnj  * Drop a TCP connection, reporting
2145089Swnj  * the specified error.  If connection is synchronized,
2155089Swnj  * then send a RST to peer.
2165089Swnj  */
21710395Ssam struct tcpcb *
2185075Swnj tcp_drop(tp, errno)
21910395Ssam 	register struct tcpcb *tp;
2205075Swnj 	int errno;
2215075Swnj {
2225075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
2235075Swnj 
2245286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
2255075Swnj 		tp->t_state = TCPS_CLOSED;
2268776Sroot 		(void) tcp_output(tp);
22730524Skarels 		tcpstat.tcps_drops++;
22830524Skarels 	} else
22930524Skarels 		tcpstat.tcps_conndrops++;
23044378Skarels 	if (errno == ETIMEDOUT && tp->t_softerror)
23144378Skarels 		errno = tp->t_softerror;
2325075Swnj 	so->so_error = errno;
23310395Ssam 	return (tcp_close(tp));
2345075Swnj }
2355075Swnj 
2365089Swnj /*
2375089Swnj  * Close a TCP control block:
2385089Swnj  *	discard all space held by the tcp
2395089Swnj  *	discard internet protocol block
2405089Swnj  *	wake up any sleepers
2415089Swnj  */
24210395Ssam struct tcpcb *
2435075Swnj tcp_close(tp)
2445075Swnj 	register struct tcpcb *tp;
2455075Swnj {
2465075Swnj 	register struct tcpiphdr *t;
2475261Swnj 	struct inpcb *inp = tp->t_inpcb;
2485261Swnj 	struct socket *so = inp->inp_socket;
24912422Ssam 	register struct mbuf *m;
25044378Skarels #ifdef RTV_RTT
25144378Skarels 	register struct rtentry *rt;
2525075Swnj 
25344378Skarels 	/*
25444378Skarels 	 * If we sent enough data to get some meaningful characteristics,
25544378Skarels 	 * save them in the routing entry.  'Enough' is arbitrarily
25645676Skarels 	 * defined as the sendpipesize (default 4K) * 16.  This would
25744378Skarels 	 * give us 16 rtt samples assuming we only get one sample per
25844378Skarels 	 * window (the usual case on a long haul net).  16 samples is
25944378Skarels 	 * enough for the srtt filter to converge to within 5% of the correct
26044378Skarels 	 * value; fewer samples and we could save a very bogus rtt.
26144378Skarels 	 *
26244378Skarels 	 * Don't update the default route's characteristics and don't
26344378Skarels 	 * update anything that the user "locked".
26444378Skarels 	 */
26545676Skarels 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
26644378Skarels 	    (rt = inp->inp_route.ro_rt) &&
26745676Skarels 	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
26844378Skarels 		register u_long i;
26944378Skarels 
27044378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
27144378Skarels 			i = tp->t_srtt *
27244378Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
27344378Skarels 			if (rt->rt_rmx.rmx_rtt && i)
27444378Skarels 				/*
27544378Skarels 				 * filter this update to half the old & half
27644378Skarels 				 * the new values, converting scale.
27744378Skarels 				 * See route.h and tcp_var.h for a
27844378Skarels 				 * description of the scaling constants.
27944378Skarels 				 */
28044378Skarels 				rt->rt_rmx.rmx_rtt =
28144378Skarels 				    (rt->rt_rmx.rmx_rtt + i) / 2;
28244378Skarels 			else
28344378Skarels 				rt->rt_rmx.rmx_rtt = i;
28444378Skarels 		}
28544378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
28644378Skarels 			i = tp->t_rttvar *
28744378Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
28844378Skarels 			if (rt->rt_rmx.rmx_rttvar && i)
28944378Skarels 				rt->rt_rmx.rmx_rttvar =
29044378Skarels 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
29144378Skarels 			else
29244378Skarels 				rt->rt_rmx.rmx_rttvar = i;
29344378Skarels 		}
29444378Skarels 		/*
29544378Skarels 		 * update the pipelimit (ssthresh) if it has been updated
29644378Skarels 		 * already or if a pipesize was specified & the threshhold
29744378Skarels 		 * got below half the pipesize.  I.e., wait for bad news
29844378Skarels 		 * before we start updating, then update on both good
29944378Skarels 		 * and bad news.
30044378Skarels 		 */
30144378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
30244378Skarels 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh ||
30344378Skarels 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
30444378Skarels 			/*
30544378Skarels 			 * convert the limit from user data bytes to
30644378Skarels 			 * packets then to packet data bytes.
30744378Skarels 			 */
30844378Skarels 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
30944378Skarels 			if (i < 2)
31044378Skarels 				i = 2;
31144378Skarels 			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
31244378Skarels 			if (rt->rt_rmx.rmx_ssthresh)
31344378Skarels 				rt->rt_rmx.rmx_ssthresh =
31444378Skarels 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
31544378Skarels 			else
31644378Skarels 				rt->rt_rmx.rmx_ssthresh = i;
31744378Skarels 		}
31844378Skarels 	}
31944378Skarels #endif RTV_RTT
32044378Skarels 	/* free the reassembly queue, if any */
3215075Swnj 	t = tp->seg_next;
32212422Ssam 	while (t != (struct tcpiphdr *)tp) {
32312422Ssam 		t = (struct tcpiphdr *)t->ti_next;
32444378Skarels 		m = REASS_MBUF((struct tcpiphdr *)t->ti_prev);
32512422Ssam 		remque(t->ti_prev);
32612422Ssam 		m_freem(m);
32712422Ssam 	}
3285089Swnj 	if (tp->t_template)
3295075Swnj 		(void) m_free(dtom(tp->t_template));
330*57433Sandrew 	free(tp, M_PCB);
3315261Swnj 	inp->inp_ppcb = 0;
3326472Sroot 	soisdisconnected(so);
33344378Skarels 	/* clobber input pcb cache if we're closing the cached connection */
33444378Skarels 	if (inp == tcp_last_inpcb)
33544378Skarels 		tcp_last_inpcb = &tcb;
3365269Sroot 	in_pcbdetach(inp);
33730524Skarels 	tcpstat.tcps_closed++;
33810395Ssam 	return ((struct tcpcb *)0);
3395075Swnj }
3405075Swnj 
3415075Swnj tcp_drain()
3425075Swnj {
3435075Swnj 
3445075Swnj }
3455075Swnj 
34630233Skarels /*
34730233Skarels  * Notify a tcp user of an asynchronous error;
34844378Skarels  * store error as soft error, but wake up user
34944378Skarels  * (for now, won't do anything until can select for soft error).
35030233Skarels  */
35144378Skarels tcp_notify(inp, error)
35254810Skarels 	struct inpcb *inp;
35344378Skarels 	int error;
35430233Skarels {
35554810Skarels 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
35654810Skarels 	register struct socket *so = inp->inp_socket;
35730233Skarels 
35854810Skarels 	/*
359*57433Sandrew 	 * Ignore some errors if we are hooked up.
36054810Skarels 	 * If connection hasn't completed, has retransmitted several times,
36154810Skarels 	 * and receives a second error, give up now.  This is better
36254810Skarels 	 * than waiting a long time to establish a connection that
36354810Skarels 	 * can never complete.
36454810Skarels 	 */
365*57433Sandrew 	if (tp->t_state == TCPS_ESTABLISHED &&
366*57433Sandrew 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
367*57433Sandrew 	      error == EHOSTDOWN)) {
368*57433Sandrew 		return;
369*57433Sandrew 	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
37054810Skarels 	    tp->t_softerror)
37154810Skarels 		so->so_error = error;
372*57433Sandrew 	else
37354810Skarels 		tp->t_softerror = error;
37454810Skarels 	wakeup((caddr_t) &so->so_timeo);
37554810Skarels 	sorwakeup(so);
37654810Skarels 	sowwakeup(so);
37730233Skarels }
37840691Skarels 
37940691Skarels tcp_ctlinput(cmd, sa, ip)
3806584Ssam 	int cmd;
38124818Skarels 	struct sockaddr *sa;
38240691Skarels 	register struct ip *ip;
3835075Swnj {
38440691Skarels 	register struct tcphdr *th;
38540691Skarels 	extern struct in_addr zeroin_addr;
3866591Ssam 	extern u_char inetctlerrmap[];
38740691Skarels 	int (*notify)() = tcp_notify, tcp_quench();
3886591Ssam 
38940691Skarels 	if (cmd == PRC_QUENCH)
39040691Skarels 		notify = tcp_quench;
391*57433Sandrew 	else if (!PRC_IS_REDIRECT(cmd) &&
392*57433Sandrew 		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
3936591Ssam 		return;
39440691Skarels 	if (ip) {
39540691Skarels 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
39640691Skarels 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
39740691Skarels 			cmd, notify);
39840691Skarels 	} else
39940691Skarels 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
4005075Swnj }
40117359Skarels 
40217359Skarels /*
40317359Skarels  * When a source quench is received, close congestion window
40431442Skarels  * to one segment.  We will gradually open it again as we proceed.
40517359Skarels  */
40617359Skarels tcp_quench(inp)
40717359Skarels 	struct inpcb *inp;
40817359Skarels {
40917359Skarels 	struct tcpcb *tp = intotcpcb(inp);
41017359Skarels 
41124818Skarels 	if (tp)
41231442Skarels 		tp->snd_cwnd = tp->t_maxseg;
41317359Skarels }
414