xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 6212)
1*6212Swnj /*	tcp_subr.c	4.18	82/03/15	*/
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"
95089Swnj #include "../net/in.h"
105089Swnj #include "../net/in_pcb.h"
115089Swnj #include "../net/in_systm.h"
125068Swnj #include "../net/if.h"
135068Swnj #include "../net/ip.h"
145068Swnj #include "../net/ip_var.h"
155068Swnj #include "../net/tcp.h"
165068Swnj #include "../net/tcp_fsm.h"
175089Swnj #include "../net/tcp_seq.h"
185089Swnj #include "../net/tcp_timer.h"
195068Swnj #include "../net/tcp_var.h"
205089Swnj #include "../net/tcpip.h"
215111Swnj #include "../errno.h"
225068Swnj 
235068Swnj /*
245068Swnj  * Tcp initialization
255068Swnj  */
265068Swnj tcp_init()
275068Swnj {
285068Swnj 
295089Swnj COUNT(TCP_INIT);
305068Swnj 	tcp_iss = 1;		/* wrong */
315068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
325164Swnj 	tcp_alpha = TCP_ALPHA;
335164Swnj 	tcp_beta = TCP_BETA;
345068Swnj }
355068Swnj 
365068Swnj /*
375068Swnj  * Create template to be used to send tcp packets on a connection.
385068Swnj  * Call after host entry created, allocates an mbuf and fills
395068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
405068Swnj  * necessary when the connection is used.
415068Swnj  */
425068Swnj struct tcpiphdr *
435068Swnj tcp_template(tp)
445068Swnj 	struct tcpcb *tp;
455068Swnj {
465068Swnj 	register struct inpcb *inp = tp->t_inpcb;
475068Swnj 	register struct mbuf *m;
485068Swnj 	register struct tcpiphdr *n;
495068Swnj 
505068Swnj COUNT(TCP_TEMPLATE);
515852Sroot 	m = m_get(M_WAIT);
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;
96*6212Swnj 	int win = 0, tlen;
975068Swnj 
985089Swnj COUNT(TCP_RESPOND);
995392Swnj 	if (tp)
1005392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1015164Swnj 	if (flags == 0) {
1025585Sroot 		m = m_get(M_DONTWAIT);
1035164Swnj 		if (m == 0)
1045164Swnj 			return;
1055164Swnj 		m->m_off = MMINOFF;
106*6212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1075164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1085164Swnj 		ti = mtod(m, struct tcpiphdr *);
1095164Swnj 		flags = TH_ACK;
110*6212Swnj 		tlen = 1;
1115164Swnj 	} else {
1125245Sroot 		m = dtom(ti);
1135164Swnj 		m_freem(m->m_next);
1145164Swnj 		m->m_next = 0;
1156117Swnj 		m->m_off = (int)ti - (int)m;
1165164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1175089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1185164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1195164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1205068Swnj #undef xchg
121*6212Swnj 		tlen = 0;
1225164Swnj 	}
1235089Swnj 	ti->ti_next = ti->ti_prev = 0;
1245089Swnj 	ti->ti_x1 = 0;
125*6212Swnj 	ti->ti_len = sizeof (struct tcphdr) + tlen;
1265245Sroot 	ti->ti_seq = seq;
1275245Sroot 	ti->ti_ack = ack;
1285245Sroot #if vax
1296163Ssam 	ti->ti_len = htons((u_short)ti->ti_len);
1305245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1315245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1325245Sroot #endif
1335089Swnj 	ti->ti_x2 = 0;
1345089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1355068Swnj 	ti->ti_flags = flags;
1365392Swnj 	ti->ti_win = win;
1375392Swnj #if vax
1385392Swnj 	ti->ti_win = htons(ti->ti_win);
1395392Swnj #endif
1405392Swnj 	ti->ti_urp = 0;
1415089Swnj 	ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr));
142*6212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1435089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
144*6212Swnj 	(void) ip_output(m, (struct mbuf *)0, 0);
1455068Swnj }
1465075Swnj 
1475089Swnj /*
1485089Swnj  * Create a new TCP control block, making an
1495089Swnj  * empty reassembly queue and hooking it to the argument
1505089Swnj  * protocol control block.
1515089Swnj  */
1525075Swnj struct tcpcb *
1535075Swnj tcp_newtcpcb(inp)
1545075Swnj 	struct inpcb *inp;
1555075Swnj {
1565852Sroot 	struct mbuf *m = m_getclr(M_DONTWAIT);
1575075Swnj 	register struct tcpcb *tp;
1585075Swnj COUNT(TCP_NEWTCPCB);
1595075Swnj 
1605075Swnj 	if (m == 0)
1615075Swnj 		return (0);
1625075Swnj 	tp = mtod(m, struct tcpcb *);
1635075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
1645089Swnj 	tp->t_maxseg = 1024;
1655442Swnj 	tp->t_flags = TF_NOOPT;		/* until all TCP's take options */
1665075Swnj 	tp->t_inpcb = inp;
1675075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1685075Swnj 	return (tp);
1695075Swnj }
1705075Swnj 
1715089Swnj /*
1725089Swnj  * Drop a TCP connection, reporting
1735089Swnj  * the specified error.  If connection is synchronized,
1745089Swnj  * then send a RST to peer.
1755089Swnj  */
1765075Swnj tcp_drop(tp, errno)
1775075Swnj 	struct tcpcb *tp;
1785075Swnj 	int errno;
1795075Swnj {
1805075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1815075Swnj 
1825075Swnj COUNT(TCP_DROP);
1835286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1845075Swnj 		tp->t_state = TCPS_CLOSED;
1855075Swnj 		tcp_output(tp);
1865075Swnj 	}
1875075Swnj 	so->so_error = errno;
1885075Swnj 	tcp_close(tp);
1895075Swnj }
1905075Swnj 
1915089Swnj /*
1925089Swnj  * Close a TCP control block:
1935089Swnj  *	discard all space held by the tcp
1945089Swnj  *	discard internet protocol block
1955089Swnj  *	wake up any sleepers
1965089Swnj  */
1975075Swnj tcp_close(tp)
1985075Swnj 	register struct tcpcb *tp;
1995075Swnj {
2005075Swnj 	register struct tcpiphdr *t;
2015261Swnj 	struct inpcb *inp = tp->t_inpcb;
2025261Swnj 	struct socket *so = inp->inp_socket;
2035075Swnj 
2045075Swnj COUNT(TCP_CLOSE);
2055075Swnj 	t = tp->seg_next;
2065075Swnj 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
2075075Swnj 		m_freem(dtom(t));
2085089Swnj 	if (tp->t_template)
2095075Swnj 		(void) m_free(dtom(tp->t_template));
2105089Swnj 	if (tp->t_tcpopt)
2115089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2125089Swnj 	if (tp->t_ipopt)
2135089Swnj 		(void) m_free(dtom(tp->t_ipopt));
2145075Swnj 	(void) m_free(dtom(tp));
2155261Swnj 	inp->inp_ppcb = 0;
2165269Sroot 	in_pcbdetach(inp);
2175245Sroot 	soisdisconnected(so);
2185075Swnj }
2195075Swnj 
2205075Swnj tcp_drain()
2215075Swnj {
2225075Swnj 
2235075Swnj COUNT(TCP_DRAIN);
2245075Swnj }
2255075Swnj 
2265075Swnj tcp_ctlinput(m)
2275075Swnj 	struct mbuf *m;
2285075Swnj {
2295075Swnj 
2305075Swnj COUNT(TCP_CTLINPUT);
2315075Swnj 	m_freem(m);
2325075Swnj }
233