xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 30233)
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*30233Skarels  *	@(#)tcp_subr.c	7.2 (Berkeley) 11/30/86
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;
1216212Swnj 		tlen = 1;
1225164Swnj 	} else {
1235245Sroot 		m = dtom(ti);
1245164Swnj 		m_freem(m->m_next);
1255164Swnj 		m->m_next = 0;
1266117Swnj 		m->m_off = (int)ti - (int)m;
1275164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1285089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1295164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1305164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1315068Swnj #undef xchg
1326212Swnj 		tlen = 0;
1335164Swnj 	}
1345089Swnj 	ti->ti_next = ti->ti_prev = 0;
1355089Swnj 	ti->ti_x1 = 0;
1369185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1378942Sroot 	ti->ti_seq = htonl(seq);
1388942Sroot 	ti->ti_ack = htonl(ack);
1395089Swnj 	ti->ti_x2 = 0;
1405089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1415068Swnj 	ti->ti_flags = flags;
1429185Ssam 	ti->ti_win = htons((u_short)win);
1435392Swnj 	ti->ti_urp = 0;
1446304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1456212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1465089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1476353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1485068Swnj }
1495075Swnj 
1505089Swnj /*
1515089Swnj  * Create a new TCP control block, making an
1525089Swnj  * empty reassembly queue and hooking it to the argument
1535089Swnj  * protocol control block.
1545089Swnj  */
1555075Swnj struct tcpcb *
1565075Swnj tcp_newtcpcb(inp)
1575075Swnj 	struct inpcb *inp;
1585075Swnj {
1599644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1605075Swnj 	register struct tcpcb *tp;
1615075Swnj 
16210144Ssam 	if (m == NULL)
16310144Ssam 		return ((struct tcpcb *)0);
1645075Swnj 	tp = mtod(m, struct tcpcb *);
1655075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
16617317Skarels 	tp->t_maxseg = TCP_MSS;
1676470Sroot 	tp->t_flags = 0;		/* sends options! */
1685075Swnj 	tp->t_inpcb = inp;
16917317Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
17017359Skarels 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
1715075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1725075Swnj 	return (tp);
1735075Swnj }
1745075Swnj 
1755089Swnj /*
1765089Swnj  * Drop a TCP connection, reporting
1775089Swnj  * the specified error.  If connection is synchronized,
1785089Swnj  * then send a RST to peer.
1795089Swnj  */
18010395Ssam struct tcpcb *
1815075Swnj tcp_drop(tp, errno)
18210395Ssam 	register struct tcpcb *tp;
1835075Swnj 	int errno;
1845075Swnj {
1855075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1865075Swnj 
1875286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1885075Swnj 		tp->t_state = TCPS_CLOSED;
1898776Sroot 		(void) tcp_output(tp);
1905075Swnj 	}
1915075Swnj 	so->so_error = errno;
19210395Ssam 	return (tcp_close(tp));
1935075Swnj }
1945075Swnj 
1955089Swnj /*
1965089Swnj  * Close a TCP control block:
1975089Swnj  *	discard all space held by the tcp
1985089Swnj  *	discard internet protocol block
1995089Swnj  *	wake up any sleepers
2005089Swnj  */
20110395Ssam struct tcpcb *
2025075Swnj tcp_close(tp)
2035075Swnj 	register struct tcpcb *tp;
2045075Swnj {
2055075Swnj 	register struct tcpiphdr *t;
2065261Swnj 	struct inpcb *inp = tp->t_inpcb;
2075261Swnj 	struct socket *so = inp->inp_socket;
20812422Ssam 	register struct mbuf *m;
2095075Swnj 
2105075Swnj 	t = tp->seg_next;
21112422Ssam 	while (t != (struct tcpiphdr *)tp) {
21212422Ssam 		t = (struct tcpiphdr *)t->ti_next;
21312422Ssam 		m = dtom(t->ti_prev);
21412422Ssam 		remque(t->ti_prev);
21512422Ssam 		m_freem(m);
21612422Ssam 	}
2175089Swnj 	if (tp->t_template)
2185075Swnj 		(void) m_free(dtom(tp->t_template));
2195089Swnj 	if (tp->t_tcpopt)
2205089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2215075Swnj 	(void) m_free(dtom(tp));
2225261Swnj 	inp->inp_ppcb = 0;
2236472Sroot 	soisdisconnected(so);
2245269Sroot 	in_pcbdetach(inp);
22510395Ssam 	return ((struct tcpcb *)0);
2265075Swnj }
2275075Swnj 
2285075Swnj tcp_drain()
2295075Swnj {
2305075Swnj 
2315075Swnj }
2325075Swnj 
233*30233Skarels /*
234*30233Skarels  * Notify a tcp user of an asynchronous error;
235*30233Skarels  * just wake up so that he can collect error status.
236*30233Skarels  */
237*30233Skarels tcp_notify(inp)
238*30233Skarels 	register struct inpcb *inp;
239*30233Skarels {
240*30233Skarels 
241*30233Skarels 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
242*30233Skarels 	sorwakeup(inp->inp_socket);
243*30233Skarels 	sowwakeup(inp->inp_socket);
244*30233Skarels }
24524818Skarels tcp_ctlinput(cmd, sa)
2466584Ssam 	int cmd;
24724818Skarels 	struct sockaddr *sa;
2485075Swnj {
2496591Ssam 	extern u_char inetctlerrmap[];
25024818Skarels 	struct sockaddr_in *sin;
25121119Skarels 	int tcp_quench(), in_rtchange();
2526591Ssam 
25324818Skarels 	if ((unsigned)cmd > PRC_NCMDS)
2546591Ssam 		return;
25524818Skarels 	if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
25624818Skarels 		return;
25724818Skarels 	sin = (struct sockaddr_in *)sa;
25824818Skarels 	if (sin->sin_addr.s_addr == INADDR_ANY)
25924818Skarels 		return;
26024818Skarels 
2616591Ssam 	switch (cmd) {
2626591Ssam 
2636591Ssam 	case PRC_QUENCH:
26424818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench);
2656591Ssam 		break;
2666591Ssam 
26724818Skarels 	case PRC_ROUTEDEAD:
26821119Skarels 	case PRC_REDIRECT_NET:
26921119Skarels 	case PRC_REDIRECT_HOST:
27024818Skarels 	case PRC_REDIRECT_TOSNET:
27124818Skarels 	case PRC_REDIRECT_TOSHOST:
27224818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange);
27321119Skarels 		break;
27421119Skarels 
2756591Ssam 	default:
27621119Skarels 		if (inetctlerrmap[cmd] == 0)
27721119Skarels 			return;		/* XXX */
27824818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd],
279*30233Skarels 			tcp_notify);
2806591Ssam 	}
2815075Swnj }
28217359Skarels 
28317359Skarels /*
28417359Skarels  * When a source quench is received, close congestion window
28517359Skarels  * to 80% of the outstanding data (but not less than one segment).
28617359Skarels  */
28717359Skarels tcp_quench(inp)
28817359Skarels 	struct inpcb *inp;
28917359Skarels {
29017359Skarels 	struct tcpcb *tp = intotcpcb(inp);
29117359Skarels 
29224818Skarels 	if (tp)
29324818Skarels 	    tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10,
29424818Skarels 		tp->t_maxseg);
29517359Skarels }
296