xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 69644)
123193Smckusick /*
2*69644Skarels  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
363218Sbostic  *	The Regents of the University of California.  All rights reserved.
423193Smckusick  *
544488Sbostic  * %sccs.include.redist.c%
632789Sbostic  *
7*69644Skarels  *	@(#)tcp_subr.c	8.2 (Berkeley) 05/24/95
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 */
3744378Skarels int 	tcp_mssdflt = TCP_MSS;
3844378Skarels int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
3957433Sandrew int	tcp_do_rfc1323 = 1;
4031395Skarels 
4144378Skarels extern	struct inpcb *tcp_last_inpcb;
4244378Skarels 
435068Swnj /*
445068Swnj  * Tcp initialization
455068Swnj  */
4661335Sbostic void
tcp_init()475068Swnj tcp_init()
485068Swnj {
495068Swnj 
50*69644Skarels 	tcp_iss = random();	/* wrong, but better than a constant */
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 *
tcp_template(tp)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  */
11161335Sbostic void
tcp_respond(tp,ti,m,ack,seq,flags)11240691Skarels tcp_respond(tp, ti, m, ack, seq, flags)
1135392Swnj 	struct tcpcb *tp;
1145068Swnj 	register struct tcpiphdr *ti;
11540691Skarels 	register struct mbuf *m;
1165089Swnj 	tcp_seq ack, seq;
1175068Swnj 	int flags;
1185068Swnj {
11944966Skarels 	register int tlen;
12044966Skarels 	int win = 0;
1216353Ssam 	struct route *ro = 0;
1225068Swnj 
1236353Ssam 	if (tp) {
1245392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1256353Ssam 		ro = &tp->t_inpcb->inp_route;
1266353Ssam 	}
12740691Skarels 	if (m == 0) {
12840691Skarels 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
12910144Ssam 		if (m == NULL)
1305164Swnj 			return;
13131727Skarels #ifdef TCP_COMPAT_42
13231727Skarels 		tlen = 1;
13331727Skarels #else
13431727Skarels 		tlen = 0;
13531727Skarels #endif
13640691Skarels 		m->m_data += max_linkhdr;
1375164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1385164Swnj 		ti = mtod(m, struct tcpiphdr *);
1395164Swnj 		flags = TH_ACK;
1405164Swnj 	} else {
1415164Swnj 		m_freem(m->m_next);
1425164Swnj 		m->m_next = 0;
14340691Skarels 		m->m_data = (caddr_t)ti;
14444378Skarels 		m->m_len = sizeof (struct tcpiphdr);
14530762Skarels 		tlen = 0;
1465089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1475164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1485164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1495068Swnj #undef xchg
1505164Swnj 	}
15144966Skarels 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
15244966Skarels 	tlen += sizeof (struct tcpiphdr);
15344966Skarels 	m->m_len = tlen;
15444966Skarels 	m->m_pkthdr.len = tlen;
15544966Skarels 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
1565089Swnj 	ti->ti_next = ti->ti_prev = 0;
1575089Swnj 	ti->ti_x1 = 0;
1588942Sroot 	ti->ti_seq = htonl(seq);
1598942Sroot 	ti->ti_ack = htonl(ack);
1605089Swnj 	ti->ti_x2 = 0;
1615089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1625068Swnj 	ti->ti_flags = flags;
16357433Sandrew 	if (tp)
16457433Sandrew 		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
16557433Sandrew 	else
16657433Sandrew 		ti->ti_win = htons((u_short)win);
1675392Swnj 	ti->ti_urp = 0;
16857433Sandrew 	ti->ti_sum = 0;
16944966Skarels 	ti->ti_sum = in_cksum(m, tlen);
17044966Skarels 	((struct ip *)ti)->ip_len = tlen;
17159132Smckusick 	((struct ip *)ti)->ip_ttl = ip_defttl;
17261335Sbostic 	(void) ip_output(m, NULL, ro, 0, NULL);
1735068Swnj }
1745075Swnj 
1755089Swnj /*
1765089Swnj  * Create a new TCP control block, making an
1775089Swnj  * empty reassembly queue and hooking it to the argument
1785089Swnj  * protocol control block.
1795089Swnj  */
1805075Swnj struct tcpcb *
tcp_newtcpcb(inp)1815075Swnj tcp_newtcpcb(inp)
1825075Swnj 	struct inpcb *inp;
1835075Swnj {
1845075Swnj 	register struct tcpcb *tp;
1855075Swnj 
18657433Sandrew 	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
18757433Sandrew 	if (tp == NULL)
18810144Ssam 		return ((struct tcpcb *)0);
18957433Sandrew 	bzero((char *) tp, sizeof(struct tcpcb));
1905075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
19144378Skarels 	tp->t_maxseg = tcp_mssdflt;
19244378Skarels 
19357433Sandrew 	tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
1945075Swnj 	tp->t_inpcb = inp;
19531726Skarels 	/*
19631757Skarels 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
19731757Skarels 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
19831757Skarels 	 * reasonable initial retransmit time.
19931726Skarels 	 */
20031757Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
20144378Skarels 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
20244378Skarels 	tp->t_rttmin = TCPTV_MIN;
20332374Skarels 	TCPT_RANGESET(tp->t_rxtcur,
20432374Skarels 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
20532374Skarels 	    TCPTV_MIN, TCPTV_REXMTMAX);
20657433Sandrew 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
20757433Sandrew 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
20859132Smckusick 	inp->inp_ip.ip_ttl = ip_defttl;
2095075Swnj 	inp->inp_ppcb = (caddr_t)tp;
2105075Swnj 	return (tp);
2115075Swnj }
2125075Swnj 
2135089Swnj /*
2145089Swnj  * Drop a TCP connection, reporting
2155089Swnj  * the specified error.  If connection is synchronized,
2165089Swnj  * then send a RST to peer.
2175089Swnj  */
21810395Ssam struct tcpcb *
tcp_drop(tp,errno)2195075Swnj tcp_drop(tp, errno)
22010395Ssam 	register struct tcpcb *tp;
2215075Swnj 	int errno;
2225075Swnj {
2235075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
2245075Swnj 
2255286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
2265075Swnj 		tp->t_state = TCPS_CLOSED;
2278776Sroot 		(void) tcp_output(tp);
22830524Skarels 		tcpstat.tcps_drops++;
22930524Skarels 	} else
23030524Skarels 		tcpstat.tcps_conndrops++;
23144378Skarels 	if (errno == ETIMEDOUT && tp->t_softerror)
23244378Skarels 		errno = tp->t_softerror;
2335075Swnj 	so->so_error = errno;
23410395Ssam 	return (tcp_close(tp));
2355075Swnj }
2365075Swnj 
2375089Swnj /*
2385089Swnj  * Close a TCP control block:
2395089Swnj  *	discard all space held by the tcp
2405089Swnj  *	discard internet protocol block
2415089Swnj  *	wake up any sleepers
2425089Swnj  */
24310395Ssam struct tcpcb *
tcp_close(tp)2445075Swnj tcp_close(tp)
2455075Swnj 	register struct tcpcb *tp;
2465075Swnj {
2475075Swnj 	register struct tcpiphdr *t;
2485261Swnj 	struct inpcb *inp = tp->t_inpcb;
2495261Swnj 	struct socket *so = inp->inp_socket;
25012422Ssam 	register struct mbuf *m;
25144378Skarels #ifdef RTV_RTT
25244378Skarels 	register struct rtentry *rt;
2535075Swnj 
25444378Skarels 	/*
25544378Skarels 	 * If we sent enough data to get some meaningful characteristics,
25644378Skarels 	 * save them in the routing entry.  'Enough' is arbitrarily
25745676Skarels 	 * defined as the sendpipesize (default 4K) * 16.  This would
25844378Skarels 	 * give us 16 rtt samples assuming we only get one sample per
25944378Skarels 	 * window (the usual case on a long haul net).  16 samples is
26044378Skarels 	 * enough for the srtt filter to converge to within 5% of the correct
26144378Skarels 	 * value; fewer samples and we could save a very bogus rtt.
26244378Skarels 	 *
26344378Skarels 	 * Don't update the default route's characteristics and don't
26444378Skarels 	 * update anything that the user "locked".
26544378Skarels 	 */
26645676Skarels 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
26744378Skarels 	    (rt = inp->inp_route.ro_rt) &&
26845676Skarels 	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
26944378Skarels 		register u_long i;
27044378Skarels 
27144378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
27244378Skarels 			i = tp->t_srtt *
27344378Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
27444378Skarels 			if (rt->rt_rmx.rmx_rtt && i)
27544378Skarels 				/*
27644378Skarels 				 * filter this update to half the old & half
27744378Skarels 				 * the new values, converting scale.
27844378Skarels 				 * See route.h and tcp_var.h for a
27944378Skarels 				 * description of the scaling constants.
28044378Skarels 				 */
28144378Skarels 				rt->rt_rmx.rmx_rtt =
28244378Skarels 				    (rt->rt_rmx.rmx_rtt + i) / 2;
28344378Skarels 			else
28444378Skarels 				rt->rt_rmx.rmx_rtt = i;
28544378Skarels 		}
28644378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
28744378Skarels 			i = tp->t_rttvar *
28844378Skarels 			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
28944378Skarels 			if (rt->rt_rmx.rmx_rttvar && i)
29044378Skarels 				rt->rt_rmx.rmx_rttvar =
29144378Skarels 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
29244378Skarels 			else
29344378Skarels 				rt->rt_rmx.rmx_rttvar = i;
29444378Skarels 		}
29544378Skarels 		/*
29644378Skarels 		 * update the pipelimit (ssthresh) if it has been updated
29744378Skarels 		 * already or if a pipesize was specified & the threshhold
29844378Skarels 		 * got below half the pipesize.  I.e., wait for bad news
29944378Skarels 		 * before we start updating, then update on both good
30044378Skarels 		 * and bad news.
30144378Skarels 		 */
30244378Skarels 		if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
30344378Skarels 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh ||
30444378Skarels 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
30544378Skarels 			/*
30644378Skarels 			 * convert the limit from user data bytes to
30744378Skarels 			 * packets then to packet data bytes.
30844378Skarels 			 */
30944378Skarels 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
31044378Skarels 			if (i < 2)
31144378Skarels 				i = 2;
31244378Skarels 			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
31344378Skarels 			if (rt->rt_rmx.rmx_ssthresh)
31444378Skarels 				rt->rt_rmx.rmx_ssthresh =
31544378Skarels 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
31644378Skarels 			else
31744378Skarels 				rt->rt_rmx.rmx_ssthresh = i;
31844378Skarels 		}
31944378Skarels 	}
32060347Storek #endif /* RTV_RTT */
32144378Skarels 	/* free the reassembly queue, if any */
3225075Swnj 	t = tp->seg_next;
32312422Ssam 	while (t != (struct tcpiphdr *)tp) {
32412422Ssam 		t = (struct tcpiphdr *)t->ti_next;
32544378Skarels 		m = REASS_MBUF((struct tcpiphdr *)t->ti_prev);
32612422Ssam 		remque(t->ti_prev);
32712422Ssam 		m_freem(m);
32812422Ssam 	}
3295089Swnj 	if (tp->t_template)
3305075Swnj 		(void) m_free(dtom(tp->t_template));
33157433Sandrew 	free(tp, M_PCB);
3325261Swnj 	inp->inp_ppcb = 0;
3336472Sroot 	soisdisconnected(so);
33444378Skarels 	/* clobber input pcb cache if we're closing the cached connection */
33544378Skarels 	if (inp == tcp_last_inpcb)
33644378Skarels 		tcp_last_inpcb = &tcb;
3375269Sroot 	in_pcbdetach(inp);
33830524Skarels 	tcpstat.tcps_closed++;
33910395Ssam 	return ((struct tcpcb *)0);
3405075Swnj }
3415075Swnj 
34261335Sbostic void
tcp_drain()3435075Swnj tcp_drain()
3445075Swnj {
3455075Swnj 
3465075Swnj }
3475075Swnj 
34830233Skarels /*
34930233Skarels  * Notify a tcp user of an asynchronous error;
35044378Skarels  * store error as soft error, but wake up user
35144378Skarels  * (for now, won't do anything until can select for soft error).
35230233Skarels  */
35361335Sbostic void
tcp_notify(inp,error)35444378Skarels tcp_notify(inp, error)
35554810Skarels 	struct inpcb *inp;
35644378Skarels 	int error;
35730233Skarels {
35854810Skarels 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
35954810Skarels 	register struct socket *so = inp->inp_socket;
36030233Skarels 
36154810Skarels 	/*
36257433Sandrew 	 * Ignore some errors if we are hooked up.
36354810Skarels 	 * If connection hasn't completed, has retransmitted several times,
36454810Skarels 	 * and receives a second error, give up now.  This is better
36554810Skarels 	 * than waiting a long time to establish a connection that
36654810Skarels 	 * can never complete.
36754810Skarels 	 */
36857433Sandrew 	if (tp->t_state == TCPS_ESTABLISHED &&
36957433Sandrew 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
37057433Sandrew 	      error == EHOSTDOWN)) {
37157433Sandrew 		return;
37257433Sandrew 	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
37354810Skarels 	    tp->t_softerror)
37454810Skarels 		so->so_error = error;
37557433Sandrew 	else
37654810Skarels 		tp->t_softerror = error;
37754810Skarels 	wakeup((caddr_t) &so->so_timeo);
37854810Skarels 	sorwakeup(so);
37954810Skarels 	sowwakeup(so);
38030233Skarels }
38140691Skarels 
38261335Sbostic void
tcp_ctlinput(cmd,sa,ip)38340691Skarels tcp_ctlinput(cmd, sa, ip)
3846584Ssam 	int cmd;
38524818Skarels 	struct sockaddr *sa;
38640691Skarels 	register struct ip *ip;
3875075Swnj {
38840691Skarels 	register struct tcphdr *th;
38940691Skarels 	extern struct in_addr zeroin_addr;
3906591Ssam 	extern u_char inetctlerrmap[];
39161335Sbostic 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
3926591Ssam 
39340691Skarels 	if (cmd == PRC_QUENCH)
39440691Skarels 		notify = tcp_quench;
39557433Sandrew 	else if (!PRC_IS_REDIRECT(cmd) &&
39657433Sandrew 		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
3976591Ssam 		return;
39840691Skarels 	if (ip) {
39940691Skarels 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
40040691Skarels 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
40140691Skarels 			cmd, notify);
40240691Skarels 	} else
40340691Skarels 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
4045075Swnj }
40517359Skarels 
40617359Skarels /*
40717359Skarels  * When a source quench is received, close congestion window
40831442Skarels  * to one segment.  We will gradually open it again as we proceed.
40917359Skarels  */
41061335Sbostic void
tcp_quench(inp,errno)41161335Sbostic tcp_quench(inp, errno)
41217359Skarels 	struct inpcb *inp;
41361335Sbostic 	int errno;
41417359Skarels {
41517359Skarels 	struct tcpcb *tp = intotcpcb(inp);
41617359Skarels 
41724818Skarels 	if (tp)
41831442Skarels 		tp->snd_cwnd = tp->t_maxseg;
41917359Skarels }
420