xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 10896)
1*10896Ssam /*	tcp_subr.c	4.40	83/02/10	*/
25068Swnj 
35068Swnj #include "../h/param.h"
45068Swnj #include "../h/systm.h"
55068Swnj #include "../h/mbuf.h"
65068Swnj #include "../h/socket.h"
75068Swnj #include "../h/socketvar.h"
85068Swnj #include "../h/protosw.h"
9*10896Ssam #include "../h/errno.h"
10*10896Ssam 
11*10896Ssam #include "../net/route.h"
12*10896Ssam #include "../net/if.h"
13*10896Ssam 
148403Swnj #include "../netinet/in.h"
158403Swnj #include "../netinet/in_pcb.h"
168403Swnj #include "../netinet/in_systm.h"
178403Swnj #include "../netinet/ip.h"
188403Swnj #include "../netinet/ip_var.h"
198403Swnj #include "../netinet/ip_icmp.h"
208403Swnj #include "../netinet/tcp.h"
218403Swnj #include "../netinet/tcp_fsm.h"
228403Swnj #include "../netinet/tcp_seq.h"
238403Swnj #include "../netinet/tcp_timer.h"
248403Swnj #include "../netinet/tcp_var.h"
258403Swnj #include "../netinet/tcpip.h"
265068Swnj 
275068Swnj /*
285068Swnj  * Tcp initialization
295068Swnj  */
305068Swnj tcp_init()
315068Swnj {
325068Swnj 
335068Swnj 	tcp_iss = 1;		/* wrong */
345068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
355164Swnj 	tcp_alpha = TCP_ALPHA;
365164Swnj 	tcp_beta = TCP_BETA;
375068Swnj }
385068Swnj 
395068Swnj /*
405068Swnj  * Create template to be used to send tcp packets on a connection.
415068Swnj  * Call after host entry created, allocates an mbuf and fills
425068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
435068Swnj  * necessary when the connection is used.
445068Swnj  */
455068Swnj struct tcpiphdr *
465068Swnj tcp_template(tp)
475068Swnj 	struct tcpcb *tp;
485068Swnj {
495068Swnj 	register struct inpcb *inp = tp->t_inpcb;
505068Swnj 	register struct mbuf *m;
515068Swnj 	register struct tcpiphdr *n;
525068Swnj 
539644Ssam 	m = m_get(M_WAIT, MT_HEADER);
5410144Ssam 	if (m == NULL)
555068Swnj 		return (0);
565068Swnj 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
575068Swnj 	m->m_len = sizeof (struct tcpiphdr);
585068Swnj 	n = mtod(m, struct tcpiphdr *);
595068Swnj 	n->ti_next = n->ti_prev = 0;
605068Swnj 	n->ti_x1 = 0;
615068Swnj 	n->ti_pr = IPPROTO_TCP;
625068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
635068Swnj 	n->ti_src = inp->inp_laddr;
645068Swnj 	n->ti_dst = inp->inp_faddr;
655068Swnj 	n->ti_sport = inp->inp_lport;
665068Swnj 	n->ti_dport = inp->inp_fport;
675068Swnj 	n->ti_seq = 0;
685089Swnj 	n->ti_ack = 0;
695068Swnj 	n->ti_x2 = 0;
705068Swnj 	n->ti_off = 5;
715068Swnj 	n->ti_flags = 0;
725068Swnj 	n->ti_win = 0;
735068Swnj 	n->ti_sum = 0;
745068Swnj 	n->ti_urp = 0;
755068Swnj 	return (n);
765068Swnj }
775068Swnj 
785068Swnj /*
795164Swnj  * Send a single message to the TCP at address specified by
805164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
815164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
825164Swnj  * This is used to force keep alive messages out using the TCP
835164Swnj  * template for a connection tp->t_template.  If flags are given
845164Swnj  * then we send a message back to the TCP which originated the
855164Swnj  * segment ti, and discard the mbuf containing it and any other
865164Swnj  * attached mbufs.
875164Swnj  *
885164Swnj  * In any case the ack and sequence number of the transmitted
895164Swnj  * segment are as specified by the parameters.
905068Swnj  */
915392Swnj tcp_respond(tp, ti, ack, seq, flags)
925392Swnj 	struct tcpcb *tp;
935068Swnj 	register struct tcpiphdr *ti;
945089Swnj 	tcp_seq ack, seq;
955068Swnj 	int flags;
965068Swnj {
975164Swnj 	struct mbuf *m;
986212Swnj 	int win = 0, tlen;
996353Ssam 	struct route *ro = 0;
1005068Swnj 
1016353Ssam 	if (tp) {
1025392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1036353Ssam 		ro = &tp->t_inpcb->inp_route;
1046353Ssam 	}
1055164Swnj 	if (flags == 0) {
1069644Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
10710144Ssam 		if (m == NULL)
1085164Swnj 			return;
1096212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1105164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1115164Swnj 		ti = mtod(m, struct tcpiphdr *);
1125164Swnj 		flags = TH_ACK;
1136212Swnj 		tlen = 1;
1145164Swnj 	} else {
1155245Sroot 		m = dtom(ti);
1165164Swnj 		m_freem(m->m_next);
1175164Swnj 		m->m_next = 0;
1186117Swnj 		m->m_off = (int)ti - (int)m;
1195164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1205089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1215164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1225164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1235068Swnj #undef xchg
1246212Swnj 		tlen = 0;
1255164Swnj 	}
1265089Swnj 	ti->ti_next = ti->ti_prev = 0;
1275089Swnj 	ti->ti_x1 = 0;
1289185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1298942Sroot 	ti->ti_seq = htonl(seq);
1308942Sroot 	ti->ti_ack = htonl(ack);
1315089Swnj 	ti->ti_x2 = 0;
1325089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1335068Swnj 	ti->ti_flags = flags;
1349185Ssam 	ti->ti_win = htons((u_short)win);
1355392Swnj 	ti->ti_urp = 0;
1366304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1376212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1385089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1396353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1405068Swnj }
1415075Swnj 
1425089Swnj /*
1435089Swnj  * Create a new TCP control block, making an
1445089Swnj  * empty reassembly queue and hooking it to the argument
1455089Swnj  * protocol control block.
1465089Swnj  */
1475075Swnj struct tcpcb *
1485075Swnj tcp_newtcpcb(inp)
1495075Swnj 	struct inpcb *inp;
1505075Swnj {
1519644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1525075Swnj 	register struct tcpcb *tp;
1535075Swnj 
15410144Ssam 	if (m == NULL)
15510144Ssam 		return ((struct tcpcb *)0);
1565075Swnj 	tp = mtod(m, struct tcpcb *);
1575075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
1589741Ssam 	/*
1599741Ssam 	 * If the default maximum IP packet size is 576 bytes
1609741Ssam 	 * and a standard IP header is 20 bytes, with a TCP
1619741Ssam 	 * header of 20 bytes plus the options necessary to
1629741Ssam 	 * upgrade it to something higher, then initialize the
1639741Ssam 	 * maximum segment size to 576 - (20 + 20 + 8 + slop).
1649741Ssam 	 */
1659741Ssam 	tp->t_maxseg = 512;		/* satisfy the rest of the world */
1666470Sroot 	tp->t_flags = 0;		/* sends options! */
1675075Swnj 	tp->t_inpcb = inp;
1685075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1695075Swnj 	return (tp);
1705075Swnj }
1715075Swnj 
1725089Swnj /*
1735089Swnj  * Drop a TCP connection, reporting
1745089Swnj  * the specified error.  If connection is synchronized,
1755089Swnj  * then send a RST to peer.
1765089Swnj  */
17710395Ssam struct tcpcb *
1785075Swnj tcp_drop(tp, errno)
17910395Ssam 	register struct tcpcb *tp;
1805075Swnj 	int errno;
1815075Swnj {
1825075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1835075Swnj 
1845286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1855075Swnj 		tp->t_state = TCPS_CLOSED;
1868776Sroot 		(void) tcp_output(tp);
1875075Swnj 	}
1885075Swnj 	so->so_error = errno;
18910395Ssam 	return (tcp_close(tp));
1905075Swnj }
1915075Swnj 
1926584Ssam tcp_abort(inp)
1936584Ssam 	struct inpcb *inp;
1946584Ssam {
1958640Sroot 
19610395Ssam 	(void) tcp_close((struct tcpcb *)inp->inp_ppcb);
1976584Ssam }
1986584Ssam 
1995089Swnj /*
2005089Swnj  * Close a TCP control block:
2015089Swnj  *	discard all space held by the tcp
2025089Swnj  *	discard internet protocol block
2035089Swnj  *	wake up any sleepers
2045089Swnj  */
20510395Ssam struct tcpcb *
2065075Swnj tcp_close(tp)
2075075Swnj 	register struct tcpcb *tp;
2085075Swnj {
2095075Swnj 	register struct tcpiphdr *t;
2105261Swnj 	struct inpcb *inp = tp->t_inpcb;
2115261Swnj 	struct socket *so = inp->inp_socket;
2125075Swnj 
2135075Swnj 	t = tp->seg_next;
2145075Swnj 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
2155075Swnj 		m_freem(dtom(t));
2165089Swnj 	if (tp->t_template)
2175075Swnj 		(void) m_free(dtom(tp->t_template));
2185089Swnj 	if (tp->t_tcpopt)
2195089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2205089Swnj 	if (tp->t_ipopt)
2215089Swnj 		(void) m_free(dtom(tp->t_ipopt));
2225075Swnj 	(void) m_free(dtom(tp));
2235261Swnj 	inp->inp_ppcb = 0;
2246472Sroot 	soisdisconnected(so);
2255269Sroot 	in_pcbdetach(inp);
22610395Ssam 	return ((struct tcpcb *)0);
2275075Swnj }
2285075Swnj 
2295075Swnj tcp_drain()
2305075Swnj {
2315075Swnj 
2325075Swnj }
2335075Swnj 
2346584Ssam tcp_ctlinput(cmd, arg)
2356584Ssam 	int cmd;
2366584Ssam 	caddr_t arg;
2375075Swnj {
2386591Ssam 	struct in_addr *sin;
2396591Ssam 	extern u_char inetctlerrmap[];
2406591Ssam 
2416591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
2426591Ssam 		return;
2436591Ssam 	switch (cmd) {
2446591Ssam 
2456591Ssam 	case PRC_ROUTEDEAD:
2466591Ssam 		break;
2476591Ssam 
2486591Ssam 	case PRC_QUENCH:
2496591Ssam 		break;
2506591Ssam 
2516591Ssam 	/* these are handled by ip */
2526591Ssam 	case PRC_IFDOWN:
2536591Ssam 	case PRC_HOSTDEAD:
2546591Ssam 	case PRC_HOSTUNREACH:
2556591Ssam 		break;
2566591Ssam 
2576591Ssam 	default:
2586591Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
2598640Sroot 		in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort);
2606591Ssam 	}
2615075Swnj }
262