xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 24818)
123193Smckusick /*
223193Smckusick  * Copyright (c) 1982 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*24818Skarels  *	@(#)tcp_subr.c	6.7 (Berkeley) 09/16/85
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 
599644Ssam 	m = m_get(M_WAIT, MT_HEADER);
6010144Ssam 	if (m == NULL)
615068Swnj 		return (0);
625068Swnj 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
635068Swnj 	m->m_len = sizeof (struct tcpiphdr);
645068Swnj 	n = mtod(m, struct tcpiphdr *);
655068Swnj 	n->ti_next = n->ti_prev = 0;
665068Swnj 	n->ti_x1 = 0;
675068Swnj 	n->ti_pr = IPPROTO_TCP;
685068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
695068Swnj 	n->ti_src = inp->inp_laddr;
705068Swnj 	n->ti_dst = inp->inp_faddr;
715068Swnj 	n->ti_sport = inp->inp_lport;
725068Swnj 	n->ti_dport = inp->inp_fport;
735068Swnj 	n->ti_seq = 0;
745089Swnj 	n->ti_ack = 0;
755068Swnj 	n->ti_x2 = 0;
765068Swnj 	n->ti_off = 5;
775068Swnj 	n->ti_flags = 0;
785068Swnj 	n->ti_win = 0;
795068Swnj 	n->ti_sum = 0;
805068Swnj 	n->ti_urp = 0;
815068Swnj 	return (n);
825068Swnj }
835068Swnj 
845068Swnj /*
855164Swnj  * Send a single message to the TCP at address specified by
865164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
875164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
885164Swnj  * This is used to force keep alive messages out using the TCP
895164Swnj  * template for a connection tp->t_template.  If flags are given
905164Swnj  * then we send a message back to the TCP which originated the
915164Swnj  * segment ti, and discard the mbuf containing it and any other
925164Swnj  * attached mbufs.
935164Swnj  *
945164Swnj  * In any case the ack and sequence number of the transmitted
955164Swnj  * segment are as specified by the parameters.
965068Swnj  */
975392Swnj tcp_respond(tp, ti, ack, seq, flags)
985392Swnj 	struct tcpcb *tp;
995068Swnj 	register struct tcpiphdr *ti;
1005089Swnj 	tcp_seq ack, seq;
1015068Swnj 	int flags;
1025068Swnj {
1035164Swnj 	struct mbuf *m;
1046212Swnj 	int win = 0, tlen;
1056353Ssam 	struct route *ro = 0;
1065068Swnj 
1076353Ssam 	if (tp) {
1085392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1096353Ssam 		ro = &tp->t_inpcb->inp_route;
1106353Ssam 	}
1115164Swnj 	if (flags == 0) {
1129644Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
11310144Ssam 		if (m == NULL)
1145164Swnj 			return;
1156212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1165164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1175164Swnj 		ti = mtod(m, struct tcpiphdr *);
1185164Swnj 		flags = TH_ACK;
1196212Swnj 		tlen = 1;
1205164Swnj 	} else {
1215245Sroot 		m = dtom(ti);
1225164Swnj 		m_freem(m->m_next);
1235164Swnj 		m->m_next = 0;
1246117Swnj 		m->m_off = (int)ti - (int)m;
1255164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1265089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1275164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1285164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1295068Swnj #undef xchg
1306212Swnj 		tlen = 0;
1315164Swnj 	}
1325089Swnj 	ti->ti_next = ti->ti_prev = 0;
1335089Swnj 	ti->ti_x1 = 0;
1349185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1358942Sroot 	ti->ti_seq = htonl(seq);
1368942Sroot 	ti->ti_ack = htonl(ack);
1375089Swnj 	ti->ti_x2 = 0;
1385089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1395068Swnj 	ti->ti_flags = flags;
1409185Ssam 	ti->ti_win = htons((u_short)win);
1415392Swnj 	ti->ti_urp = 0;
1426304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1436212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1445089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1456353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1465068Swnj }
1475075Swnj 
1485089Swnj /*
1495089Swnj  * Create a new TCP control block, making an
1505089Swnj  * empty reassembly queue and hooking it to the argument
1515089Swnj  * protocol control block.
1525089Swnj  */
1535075Swnj struct tcpcb *
1545075Swnj tcp_newtcpcb(inp)
1555075Swnj 	struct inpcb *inp;
1565075Swnj {
1579644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1585075Swnj 	register struct tcpcb *tp;
1595075Swnj 
16010144Ssam 	if (m == NULL)
16110144Ssam 		return ((struct tcpcb *)0);
1625075Swnj 	tp = mtod(m, struct tcpcb *);
1635075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
16417317Skarels 	tp->t_maxseg = TCP_MSS;
1656470Sroot 	tp->t_flags = 0;		/* sends options! */
1665075Swnj 	tp->t_inpcb = inp;
16717317Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
16817359Skarels 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
1695075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1705075Swnj 	return (tp);
1715075Swnj }
1725075Swnj 
1735089Swnj /*
1745089Swnj  * Drop a TCP connection, reporting
1755089Swnj  * the specified error.  If connection is synchronized,
1765089Swnj  * then send a RST to peer.
1775089Swnj  */
17810395Ssam struct tcpcb *
1795075Swnj tcp_drop(tp, errno)
18010395Ssam 	register struct tcpcb *tp;
1815075Swnj 	int errno;
1825075Swnj {
1835075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1845075Swnj 
1855286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1865075Swnj 		tp->t_state = TCPS_CLOSED;
1878776Sroot 		(void) tcp_output(tp);
1885075Swnj 	}
1895075Swnj 	so->so_error = errno;
19010395Ssam 	return (tcp_close(tp));
1915075Swnj }
1925075Swnj 
1935089Swnj /*
1945089Swnj  * Close a TCP control block:
1955089Swnj  *	discard all space held by the tcp
1965089Swnj  *	discard internet protocol block
1975089Swnj  *	wake up any sleepers
1985089Swnj  */
19910395Ssam struct tcpcb *
2005075Swnj tcp_close(tp)
2015075Swnj 	register struct tcpcb *tp;
2025075Swnj {
2035075Swnj 	register struct tcpiphdr *t;
2045261Swnj 	struct inpcb *inp = tp->t_inpcb;
2055261Swnj 	struct socket *so = inp->inp_socket;
20612422Ssam 	register struct mbuf *m;
2075075Swnj 
2085075Swnj 	t = tp->seg_next;
20912422Ssam 	while (t != (struct tcpiphdr *)tp) {
21012422Ssam 		t = (struct tcpiphdr *)t->ti_next;
21112422Ssam 		m = dtom(t->ti_prev);
21212422Ssam 		remque(t->ti_prev);
21312422Ssam 		m_freem(m);
21412422Ssam 	}
2155089Swnj 	if (tp->t_template)
2165075Swnj 		(void) m_free(dtom(tp->t_template));
2175089Swnj 	if (tp->t_tcpopt)
2185089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2195075Swnj 	(void) m_free(dtom(tp));
2205261Swnj 	inp->inp_ppcb = 0;
2216472Sroot 	soisdisconnected(so);
2225269Sroot 	in_pcbdetach(inp);
22310395Ssam 	return ((struct tcpcb *)0);
2245075Swnj }
2255075Swnj 
2265075Swnj tcp_drain()
2275075Swnj {
2285075Swnj 
2295075Swnj }
2305075Swnj 
231*24818Skarels tcp_ctlinput(cmd, sa)
2326584Ssam 	int cmd;
233*24818Skarels 	struct sockaddr *sa;
2345075Swnj {
2356591Ssam 	extern u_char inetctlerrmap[];
236*24818Skarels 	struct sockaddr_in *sin;
23721119Skarels 	int tcp_quench(), in_rtchange();
2386591Ssam 
239*24818Skarels 	if ((unsigned)cmd > PRC_NCMDS)
2406591Ssam 		return;
241*24818Skarels 	if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
242*24818Skarels 		return;
243*24818Skarels 	sin = (struct sockaddr_in *)sa;
244*24818Skarels 	if (sin->sin_addr.s_addr == INADDR_ANY)
245*24818Skarels 		return;
246*24818Skarels 
2476591Ssam 	switch (cmd) {
2486591Ssam 
2496591Ssam 	case PRC_QUENCH:
250*24818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench);
2516591Ssam 		break;
2526591Ssam 
253*24818Skarels 	case PRC_ROUTEDEAD:
25421119Skarels 	case PRC_REDIRECT_NET:
25521119Skarels 	case PRC_REDIRECT_HOST:
256*24818Skarels 	case PRC_REDIRECT_TOSNET:
257*24818Skarels 	case PRC_REDIRECT_TOSHOST:
258*24818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange);
25921119Skarels 		break;
26021119Skarels 
2616591Ssam 	default:
26221119Skarels 		if (inetctlerrmap[cmd] == 0)
26321119Skarels 			return;		/* XXX */
264*24818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd],
265*24818Skarels 			(int (*)())0);
2666591Ssam 	}
2675075Swnj }
26817359Skarels 
26917359Skarels /*
27017359Skarels  * When a source quench is received, close congestion window
27117359Skarels  * to 80% of the outstanding data (but not less than one segment).
27217359Skarels  */
27317359Skarels tcp_quench(inp)
27417359Skarels 	struct inpcb *inp;
27517359Skarels {
27617359Skarels 	struct tcpcb *tp = intotcpcb(inp);
27717359Skarels 
278*24818Skarels 	if (tp)
279*24818Skarels 	    tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10,
280*24818Skarels 		tp->t_maxseg);
28117359Skarels }
282