xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 9644)
1*9644Ssam /*	tcp_subr.c	4.36	82/12/14	*/
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"
98403Swnj #include "../netinet/in.h"
106353Ssam #include "../net/route.h"
118403Swnj #include "../netinet/in_pcb.h"
128403Swnj #include "../netinet/in_systm.h"
135068Swnj #include "../net/if.h"
148403Swnj #include "../netinet/ip.h"
158403Swnj #include "../netinet/ip_var.h"
168403Swnj #include "../netinet/ip_icmp.h"
178403Swnj #include "../netinet/tcp.h"
188403Swnj #include "../netinet/tcp_fsm.h"
198403Swnj #include "../netinet/tcp_seq.h"
208403Swnj #include "../netinet/tcp_timer.h"
218403Swnj #include "../netinet/tcp_var.h"
228403Swnj #include "../netinet/tcpip.h"
237301Ssam #include <errno.h>
245068Swnj 
255068Swnj /*
265068Swnj  * Tcp initialization
275068Swnj  */
285068Swnj tcp_init()
295068Swnj {
305068Swnj 
315068Swnj 	tcp_iss = 1;		/* wrong */
325068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
335164Swnj 	tcp_alpha = TCP_ALPHA;
345164Swnj 	tcp_beta = TCP_BETA;
355068Swnj }
365068Swnj 
375068Swnj /*
385068Swnj  * Create template to be used to send tcp packets on a connection.
395068Swnj  * Call after host entry created, allocates an mbuf and fills
405068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
415068Swnj  * necessary when the connection is used.
425068Swnj  */
435068Swnj struct tcpiphdr *
445068Swnj tcp_template(tp)
455068Swnj 	struct tcpcb *tp;
465068Swnj {
475068Swnj 	register struct inpcb *inp = tp->t_inpcb;
485068Swnj 	register struct mbuf *m;
495068Swnj 	register struct tcpiphdr *n;
505068Swnj 
51*9644Ssam 	m = m_get(M_WAIT, MT_HEADER);
525068Swnj 	if (m == 0)
535068Swnj 		return (0);
545068Swnj 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
555068Swnj 	m->m_len = sizeof (struct tcpiphdr);
565068Swnj 	n = mtod(m, struct tcpiphdr *);
575068Swnj 	n->ti_next = n->ti_prev = 0;
585068Swnj 	n->ti_x1 = 0;
595068Swnj 	n->ti_pr = IPPROTO_TCP;
605068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
615068Swnj 	n->ti_src = inp->inp_laddr;
625068Swnj 	n->ti_dst = inp->inp_faddr;
635068Swnj 	n->ti_sport = inp->inp_lport;
645068Swnj 	n->ti_dport = inp->inp_fport;
655068Swnj 	n->ti_seq = 0;
665089Swnj 	n->ti_ack = 0;
675068Swnj 	n->ti_x2 = 0;
685068Swnj 	n->ti_off = 5;
695068Swnj 	n->ti_flags = 0;
705068Swnj 	n->ti_win = 0;
715068Swnj 	n->ti_sum = 0;
725068Swnj 	n->ti_urp = 0;
735068Swnj 	return (n);
745068Swnj }
755068Swnj 
765068Swnj /*
775164Swnj  * Send a single message to the TCP at address specified by
785164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
795164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
805164Swnj  * This is used to force keep alive messages out using the TCP
815164Swnj  * template for a connection tp->t_template.  If flags are given
825164Swnj  * then we send a message back to the TCP which originated the
835164Swnj  * segment ti, and discard the mbuf containing it and any other
845164Swnj  * attached mbufs.
855164Swnj  *
865164Swnj  * In any case the ack and sequence number of the transmitted
875164Swnj  * segment are as specified by the parameters.
885068Swnj  */
895392Swnj tcp_respond(tp, ti, ack, seq, flags)
905392Swnj 	struct tcpcb *tp;
915068Swnj 	register struct tcpiphdr *ti;
925089Swnj 	tcp_seq ack, seq;
935068Swnj 	int flags;
945068Swnj {
955164Swnj 	struct mbuf *m;
966212Swnj 	int win = 0, tlen;
976353Ssam 	struct route *ro = 0;
985068Swnj 
996353Ssam 	if (tp) {
1005392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1016353Ssam 		ro = &tp->t_inpcb->inp_route;
1026353Ssam 	}
1035164Swnj 	if (flags == 0) {
104*9644Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
1055164Swnj 		if (m == 0)
1065164Swnj 			return;
1076212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1085164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1095164Swnj 		ti = mtod(m, struct tcpiphdr *);
1105164Swnj 		flags = TH_ACK;
1116212Swnj 		tlen = 1;
1125164Swnj 	} else {
1135245Sroot 		m = dtom(ti);
1145164Swnj 		m_freem(m->m_next);
1155164Swnj 		m->m_next = 0;
1166117Swnj 		m->m_off = (int)ti - (int)m;
1175164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1185089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1195164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1205164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1215068Swnj #undef xchg
1226212Swnj 		tlen = 0;
1235164Swnj 	}
1245089Swnj 	ti->ti_next = ti->ti_prev = 0;
1255089Swnj 	ti->ti_x1 = 0;
1269185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1278942Sroot 	ti->ti_seq = htonl(seq);
1288942Sroot 	ti->ti_ack = htonl(ack);
1295089Swnj 	ti->ti_x2 = 0;
1305089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1315068Swnj 	ti->ti_flags = flags;
1329185Ssam 	ti->ti_win = htons((u_short)win);
1335392Swnj 	ti->ti_urp = 0;
1346304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1356212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1365089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1376353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1385068Swnj }
1395075Swnj 
1405089Swnj /*
1415089Swnj  * Create a new TCP control block, making an
1425089Swnj  * empty reassembly queue and hooking it to the argument
1435089Swnj  * protocol control block.
1445089Swnj  */
1455075Swnj struct tcpcb *
1465075Swnj tcp_newtcpcb(inp)
1475075Swnj 	struct inpcb *inp;
1485075Swnj {
149*9644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1505075Swnj 	register struct tcpcb *tp;
1515075Swnj 
1525075Swnj 	if (m == 0)
1535075Swnj 		return (0);
1545075Swnj 	tp = mtod(m, struct tcpcb *);
1555075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
1566601Ssam 	tp->t_maxseg = 576;		/* satisfy the rest of the world */
1576470Sroot 	tp->t_flags = 0;		/* sends options! */
1585075Swnj 	tp->t_inpcb = inp;
1595075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1605075Swnj 	return (tp);
1615075Swnj }
1625075Swnj 
1635089Swnj /*
1645089Swnj  * Drop a TCP connection, reporting
1655089Swnj  * the specified error.  If connection is synchronized,
1665089Swnj  * then send a RST to peer.
1675089Swnj  */
1685075Swnj tcp_drop(tp, errno)
1695075Swnj 	struct tcpcb *tp;
1705075Swnj 	int errno;
1715075Swnj {
1725075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1735075Swnj 
1745286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1755075Swnj 		tp->t_state = TCPS_CLOSED;
1768776Sroot 		(void) tcp_output(tp);
1775075Swnj 	}
1785075Swnj 	so->so_error = errno;
1795075Swnj 	tcp_close(tp);
1805075Swnj }
1815075Swnj 
1826584Ssam tcp_abort(inp)
1836584Ssam 	struct inpcb *inp;
1846584Ssam {
1858640Sroot 
1868640Sroot 	tcp_close((struct tcpcb *)inp->inp_ppcb);
1876584Ssam }
1886584Ssam 
1895089Swnj /*
1905089Swnj  * Close a TCP control block:
1915089Swnj  *	discard all space held by the tcp
1925089Swnj  *	discard internet protocol block
1935089Swnj  *	wake up any sleepers
1945089Swnj  */
1955075Swnj tcp_close(tp)
1965075Swnj 	register struct tcpcb *tp;
1975075Swnj {
1985075Swnj 	register struct tcpiphdr *t;
1995261Swnj 	struct inpcb *inp = tp->t_inpcb;
2005261Swnj 	struct socket *so = inp->inp_socket;
2015075Swnj 
2025075Swnj 	t = tp->seg_next;
2035075Swnj 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
2045075Swnj 		m_freem(dtom(t));
2055089Swnj 	if (tp->t_template)
2065075Swnj 		(void) m_free(dtom(tp->t_template));
2075089Swnj 	if (tp->t_tcpopt)
2085089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2095089Swnj 	if (tp->t_ipopt)
2105089Swnj 		(void) m_free(dtom(tp->t_ipopt));
2115075Swnj 	(void) m_free(dtom(tp));
2125261Swnj 	inp->inp_ppcb = 0;
2136472Sroot 	soisdisconnected(so);
2145269Sroot 	in_pcbdetach(inp);
2155075Swnj }
2165075Swnj 
2175075Swnj tcp_drain()
2185075Swnj {
2195075Swnj 
2205075Swnj }
2215075Swnj 
2226584Ssam tcp_ctlinput(cmd, arg)
2236584Ssam 	int cmd;
2246584Ssam 	caddr_t arg;
2255075Swnj {
2266591Ssam 	struct in_addr *sin;
2276591Ssam 	extern u_char inetctlerrmap[];
2286591Ssam 
2296591Ssam 	if (cmd < 0 || cmd > PRC_NCMDS)
2306591Ssam 		return;
2316591Ssam 	switch (cmd) {
2326591Ssam 
2336591Ssam 	case PRC_ROUTEDEAD:
2346591Ssam 		break;
2356591Ssam 
2366591Ssam 	case PRC_QUENCH:
2376591Ssam 		break;
2386591Ssam 
2396591Ssam 	/* these are handled by ip */
2406591Ssam 	case PRC_IFDOWN:
2416591Ssam 	case PRC_HOSTDEAD:
2426591Ssam 	case PRC_HOSTUNREACH:
2436591Ssam 		break;
2446591Ssam 
2456591Ssam 	default:
2466591Ssam 		sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
2478640Sroot 		in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort);
2486591Ssam 	}
2495075Swnj }
250