xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 30524)
123193Smckusick /*
229152Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323193Smckusick  * All rights reserved.  The Berkeley software License Agreement
423193Smckusick  * specifies the terms and conditions for redistribution.
523193Smckusick  *
6*30524Skarels  *	@(#)tcp_subr.c	7.3 (Berkeley) 02/19/87
723193Smckusick  */
85068Swnj 
917064Sbloom #include "param.h"
1017064Sbloom #include "systm.h"
1117064Sbloom #include "mbuf.h"
1217064Sbloom #include "socket.h"
1317064Sbloom #include "socketvar.h"
1417064Sbloom #include "protosw.h"
1517064Sbloom #include "errno.h"
1610896Ssam 
1710896Ssam #include "../net/route.h"
1810896Ssam #include "../net/if.h"
1910896Ssam 
2017064Sbloom #include "in.h"
2117064Sbloom #include "in_pcb.h"
2217064Sbloom #include "in_systm.h"
2317064Sbloom #include "ip.h"
2417064Sbloom #include "ip_var.h"
2517064Sbloom #include "ip_icmp.h"
2617064Sbloom #include "tcp.h"
2717064Sbloom #include "tcp_fsm.h"
2817064Sbloom #include "tcp_seq.h"
2917064Sbloom #include "tcp_timer.h"
3017064Sbloom #include "tcp_var.h"
3117064Sbloom #include "tcpip.h"
325068Swnj 
335068Swnj /*
345068Swnj  * Tcp initialization
355068Swnj  */
365068Swnj tcp_init()
375068Swnj {
385068Swnj 
395068Swnj 	tcp_iss = 1;		/* wrong */
405068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
415164Swnj 	tcp_alpha = TCP_ALPHA;
425164Swnj 	tcp_beta = TCP_BETA;
435068Swnj }
445068Swnj 
455068Swnj /*
465068Swnj  * Create template to be used to send tcp packets on a connection.
475068Swnj  * Call after host entry created, allocates an mbuf and fills
485068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
495068Swnj  * necessary when the connection is used.
505068Swnj  */
515068Swnj struct tcpiphdr *
525068Swnj tcp_template(tp)
535068Swnj 	struct tcpcb *tp;
545068Swnj {
555068Swnj 	register struct inpcb *inp = tp->t_inpcb;
565068Swnj 	register struct mbuf *m;
575068Swnj 	register struct tcpiphdr *n;
585068Swnj 
5926815Skarels 	if ((n = tp->t_template) == 0) {
6026815Skarels 		m = m_get(M_WAIT, MT_HEADER);
6126815Skarels 		if (m == NULL)
6226815Skarels 			return (0);
6326815Skarels 		m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
6426815Skarels 		m->m_len = sizeof (struct tcpiphdr);
6526815Skarels 		n = mtod(m, struct tcpiphdr *);
6626815Skarels 	}
675068Swnj 	n->ti_next = n->ti_prev = 0;
685068Swnj 	n->ti_x1 = 0;
695068Swnj 	n->ti_pr = IPPROTO_TCP;
705068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
715068Swnj 	n->ti_src = inp->inp_laddr;
725068Swnj 	n->ti_dst = inp->inp_faddr;
735068Swnj 	n->ti_sport = inp->inp_lport;
745068Swnj 	n->ti_dport = inp->inp_fport;
755068Swnj 	n->ti_seq = 0;
765089Swnj 	n->ti_ack = 0;
775068Swnj 	n->ti_x2 = 0;
785068Swnj 	n->ti_off = 5;
795068Swnj 	n->ti_flags = 0;
805068Swnj 	n->ti_win = 0;
815068Swnj 	n->ti_sum = 0;
825068Swnj 	n->ti_urp = 0;
835068Swnj 	return (n);
845068Swnj }
855068Swnj 
865068Swnj /*
875164Swnj  * Send a single message to the TCP at address specified by
885164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
895164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
905164Swnj  * This is used to force keep alive messages out using the TCP
915164Swnj  * template for a connection tp->t_template.  If flags are given
925164Swnj  * then we send a message back to the TCP which originated the
935164Swnj  * segment ti, and discard the mbuf containing it and any other
945164Swnj  * attached mbufs.
955164Swnj  *
965164Swnj  * In any case the ack and sequence number of the transmitted
975164Swnj  * segment are as specified by the parameters.
985068Swnj  */
995392Swnj tcp_respond(tp, ti, ack, seq, flags)
1005392Swnj 	struct tcpcb *tp;
1015068Swnj 	register struct tcpiphdr *ti;
1025089Swnj 	tcp_seq ack, seq;
1035068Swnj 	int flags;
1045068Swnj {
1055164Swnj 	struct mbuf *m;
1066212Swnj 	int win = 0, tlen;
1076353Ssam 	struct route *ro = 0;
1085068Swnj 
1096353Ssam 	if (tp) {
1105392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1116353Ssam 		ro = &tp->t_inpcb->inp_route;
1126353Ssam 	}
1135164Swnj 	if (flags == 0) {
1149644Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
11510144Ssam 		if (m == NULL)
1165164Swnj 			return;
1176212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1185164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1195164Swnj 		ti = mtod(m, struct tcpiphdr *);
1205164Swnj 		flags = TH_ACK;
1215164Swnj 	} else {
1225245Sroot 		m = dtom(ti);
1235164Swnj 		m_freem(m->m_next);
1245164Swnj 		m->m_next = 0;
1256117Swnj 		m->m_off = (int)ti - (int)m;
1265164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1275089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1285164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1295164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1305068Swnj #undef xchg
1315164Swnj 	}
132*30524Skarels 	tlen = 0;
1335089Swnj 	ti->ti_next = ti->ti_prev = 0;
1345089Swnj 	ti->ti_x1 = 0;
1359185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1368942Sroot 	ti->ti_seq = htonl(seq);
1378942Sroot 	ti->ti_ack = htonl(ack);
1385089Swnj 	ti->ti_x2 = 0;
1395089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1405068Swnj 	ti->ti_flags = flags;
1419185Ssam 	ti->ti_win = htons((u_short)win);
1425392Swnj 	ti->ti_urp = 0;
1436304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1446212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1455089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1466353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1475068Swnj }
1485075Swnj 
1495089Swnj /*
1505089Swnj  * Create a new TCP control block, making an
1515089Swnj  * empty reassembly queue and hooking it to the argument
1525089Swnj  * protocol control block.
1535089Swnj  */
1545075Swnj struct tcpcb *
1555075Swnj tcp_newtcpcb(inp)
1565075Swnj 	struct inpcb *inp;
1575075Swnj {
1589644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1595075Swnj 	register struct tcpcb *tp;
1605075Swnj 
16110144Ssam 	if (m == NULL)
16210144Ssam 		return ((struct tcpcb *)0);
1635075Swnj 	tp = mtod(m, struct tcpcb *);
1645075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
16517317Skarels 	tp->t_maxseg = TCP_MSS;
1666470Sroot 	tp->t_flags = 0;		/* sends options! */
1675075Swnj 	tp->t_inpcb = inp;
16817317Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
16917359Skarels 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
1705075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1715075Swnj 	return (tp);
1725075Swnj }
1735075Swnj 
1745089Swnj /*
1755089Swnj  * Drop a TCP connection, reporting
1765089Swnj  * the specified error.  If connection is synchronized,
1775089Swnj  * then send a RST to peer.
1785089Swnj  */
17910395Ssam struct tcpcb *
1805075Swnj tcp_drop(tp, errno)
18110395Ssam 	register struct tcpcb *tp;
1825075Swnj 	int errno;
1835075Swnj {
1845075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1855075Swnj 
1865286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1875075Swnj 		tp->t_state = TCPS_CLOSED;
1888776Sroot 		(void) tcp_output(tp);
189*30524Skarels 		tcpstat.tcps_drops++;
190*30524Skarels 	} else
191*30524Skarels 		tcpstat.tcps_conndrops++;
1925075Swnj 	so->so_error = errno;
19310395Ssam 	return (tcp_close(tp));
1945075Swnj }
1955075Swnj 
1965089Swnj /*
1975089Swnj  * Close a TCP control block:
1985089Swnj  *	discard all space held by the tcp
1995089Swnj  *	discard internet protocol block
2005089Swnj  *	wake up any sleepers
2015089Swnj  */
20210395Ssam struct tcpcb *
2035075Swnj tcp_close(tp)
2045075Swnj 	register struct tcpcb *tp;
2055075Swnj {
2065075Swnj 	register struct tcpiphdr *t;
2075261Swnj 	struct inpcb *inp = tp->t_inpcb;
2085261Swnj 	struct socket *so = inp->inp_socket;
20912422Ssam 	register struct mbuf *m;
2105075Swnj 
2115075Swnj 	t = tp->seg_next;
21212422Ssam 	while (t != (struct tcpiphdr *)tp) {
21312422Ssam 		t = (struct tcpiphdr *)t->ti_next;
21412422Ssam 		m = dtom(t->ti_prev);
21512422Ssam 		remque(t->ti_prev);
21612422Ssam 		m_freem(m);
21712422Ssam 	}
2185089Swnj 	if (tp->t_template)
2195075Swnj 		(void) m_free(dtom(tp->t_template));
2205089Swnj 	if (tp->t_tcpopt)
2215089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2225075Swnj 	(void) m_free(dtom(tp));
2235261Swnj 	inp->inp_ppcb = 0;
2246472Sroot 	soisdisconnected(so);
2255269Sroot 	in_pcbdetach(inp);
226*30524Skarels 	tcpstat.tcps_closed++;
22710395Ssam 	return ((struct tcpcb *)0);
2285075Swnj }
2295075Swnj 
2305075Swnj tcp_drain()
2315075Swnj {
2325075Swnj 
2335075Swnj }
2345075Swnj 
23530233Skarels /*
23630233Skarels  * Notify a tcp user of an asynchronous error;
23730233Skarels  * just wake up so that he can collect error status.
23830233Skarels  */
23930233Skarels tcp_notify(inp)
24030233Skarels 	register struct inpcb *inp;
24130233Skarels {
24230233Skarels 
24330233Skarels 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
24430233Skarels 	sorwakeup(inp->inp_socket);
24530233Skarels 	sowwakeup(inp->inp_socket);
24630233Skarels }
24724818Skarels tcp_ctlinput(cmd, sa)
2486584Ssam 	int cmd;
24924818Skarels 	struct sockaddr *sa;
2505075Swnj {
2516591Ssam 	extern u_char inetctlerrmap[];
25224818Skarels 	struct sockaddr_in *sin;
25321119Skarels 	int tcp_quench(), in_rtchange();
2546591Ssam 
25524818Skarels 	if ((unsigned)cmd > PRC_NCMDS)
2566591Ssam 		return;
25724818Skarels 	if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
25824818Skarels 		return;
25924818Skarels 	sin = (struct sockaddr_in *)sa;
26024818Skarels 	if (sin->sin_addr.s_addr == INADDR_ANY)
26124818Skarels 		return;
26224818Skarels 
2636591Ssam 	switch (cmd) {
2646591Ssam 
2656591Ssam 	case PRC_QUENCH:
26624818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench);
2676591Ssam 		break;
2686591Ssam 
26924818Skarels 	case PRC_ROUTEDEAD:
27021119Skarels 	case PRC_REDIRECT_NET:
27121119Skarels 	case PRC_REDIRECT_HOST:
27224818Skarels 	case PRC_REDIRECT_TOSNET:
27324818Skarels 	case PRC_REDIRECT_TOSHOST:
27424818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange);
27521119Skarels 		break;
27621119Skarels 
2776591Ssam 	default:
27821119Skarels 		if (inetctlerrmap[cmd] == 0)
27921119Skarels 			return;		/* XXX */
28024818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd],
28130233Skarels 			tcp_notify);
2826591Ssam 	}
2835075Swnj }
28417359Skarels 
28517359Skarels /*
28617359Skarels  * When a source quench is received, close congestion window
28717359Skarels  * to 80% of the outstanding data (but not less than one segment).
28817359Skarels  */
28917359Skarels tcp_quench(inp)
29017359Skarels 	struct inpcb *inp;
29117359Skarels {
29217359Skarels 	struct tcpcb *tp = intotcpcb(inp);
29317359Skarels 
29424818Skarels 	if (tp)
29524818Skarels 	    tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10,
29624818Skarels 		tp->t_maxseg);
29717359Skarels }
298