xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 5245)
1*5245Sroot /* tcp_subr.c 4.7 81/12/12 */
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);
51*5245Sroot printf("tcp_template %x\n", tp);
525068Swnj 	m = m_get(1);
535068Swnj 	if (m == 0)
545068Swnj 		return (0);
555068Swnj 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
565068Swnj 	m->m_len = sizeof (struct tcpiphdr);
575068Swnj 	n = mtod(m, struct tcpiphdr *);
585068Swnj 	n->ti_next = n->ti_prev = 0;
595068Swnj 	n->ti_x1 = 0;
605068Swnj 	n->ti_pr = IPPROTO_TCP;
615068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
625068Swnj 	n->ti_src = inp->inp_laddr;
635068Swnj 	n->ti_dst = inp->inp_faddr;
645068Swnj 	n->ti_sport = inp->inp_lport;
655068Swnj 	n->ti_dport = inp->inp_fport;
665068Swnj 	n->ti_seq = 0;
675089Swnj 	n->ti_ack = 0;
685068Swnj 	n->ti_x2 = 0;
695068Swnj 	n->ti_off = 5;
705068Swnj 	n->ti_flags = 0;
715068Swnj 	n->ti_win = 0;
725068Swnj 	n->ti_sum = 0;
735068Swnj 	n->ti_urp = 0;
745068Swnj 	return (n);
755068Swnj }
765068Swnj 
775068Swnj /*
785164Swnj  * Send a single message to the TCP at address specified by
795164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
805164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
815164Swnj  * This is used to force keep alive messages out using the TCP
825164Swnj  * template for a connection tp->t_template.  If flags are given
835164Swnj  * then we send a message back to the TCP which originated the
845164Swnj  * segment ti, and discard the mbuf containing it and any other
855164Swnj  * attached mbufs.
865164Swnj  *
875164Swnj  * In any case the ack and sequence number of the transmitted
885164Swnj  * segment are as specified by the parameters.
895068Swnj  */
905089Swnj tcp_respond(ti, ack, seq, flags)
915068Swnj 	register struct tcpiphdr *ti;
925089Swnj 	tcp_seq ack, seq;
935068Swnj 	int flags;
945068Swnj {
955164Swnj 	struct mbuf *m;
965068Swnj 
975089Swnj COUNT(TCP_RESPOND);
98*5245Sroot printf("tcp_respond ack %x seq %x flags %x\n", ack, seq, flags);
995164Swnj 	if (flags == 0) {
1005164Swnj 		m = m_get(0);
1015164Swnj 		if (m == 0)
1025164Swnj 			return;
1035164Swnj 		m->m_off = MMINOFF;
1045164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1055164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1065164Swnj 		ti = mtod(m, struct tcpiphdr *);
1075164Swnj 		flags = TH_ACK;
1085164Swnj 	} else {
109*5245Sroot 		m = dtom(ti);
1105164Swnj 		m_freem(m->m_next);
1115164Swnj 		m->m_next = 0;
1125164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1135089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1145164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1155164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1165068Swnj #undef xchg
1175164Swnj 	}
1185089Swnj 	ti->ti_next = ti->ti_prev = 0;
1195089Swnj 	ti->ti_x1 = 0;
120*5245Sroot 	ti->ti_len = sizeof (struct tcphdr);
121*5245Sroot 	ti->ti_seq = seq;
122*5245Sroot 	ti->ti_ack = ack;
123*5245Sroot #if vax
124*5245Sroot 	ti->ti_len = htons(ti->ti_len);
125*5245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
126*5245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
127*5245Sroot #endif
1285089Swnj 	ti->ti_x2 = 0;
1295089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1305068Swnj 	ti->ti_flags = flags;
1315089Swnj 	ti->ti_win = ti->ti_urp = 0;
132*5245Sroot printf("before cksum m->m_len %d\n", m->m_len);
1335089Swnj 	ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr));
1345068Swnj 	((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr);
1355089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
136*5245Sroot printf("to ip_output ip_len %d, m %x\n", ((struct ip *)ti)->ip_len, m);
1375111Swnj 	(void) ip_output(m, (struct mbuf *)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 {
1495075Swnj 	struct mbuf *m = m_getclr(0);
1505075Swnj 	register struct tcpcb *tp;
1515075Swnj COUNT(TCP_NEWTCPCB);
1525075Swnj 
153*5245Sroot printf("tcp_newtcpcb %x\n", inp);
1545075Swnj 	if (m == 0)
1555075Swnj 		return (0);
1565075Swnj 	tp = mtod(m, struct tcpcb *);
1575075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
1585089Swnj 	tp->t_maxseg = 1024;
1595075Swnj 	tp->t_inpcb = inp;
1605075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1615075Swnj 	return (tp);
1625075Swnj }
1635075Swnj 
1645089Swnj /*
1655089Swnj  * Drop a TCP connection, reporting
1665089Swnj  * the specified error.  If connection is synchronized,
1675089Swnj  * then send a RST to peer.
1685089Swnj  */
1695075Swnj tcp_drop(tp, errno)
1705075Swnj 	struct tcpcb *tp;
1715075Swnj 	int errno;
1725075Swnj {
1735075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1745075Swnj 
1755075Swnj COUNT(TCP_DROP);
176*5245Sroot printf("tcp_drop %x %d\n", tp, errno);
1775075Swnj 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
1785089Swnj 	    TCPS_OURFINNOTACKED(tp->t_state)) {
1795075Swnj 		tp->t_state = TCPS_CLOSED;
1805075Swnj 		tcp_output(tp);
1815075Swnj 	}
1825075Swnj 	so->so_error = errno;
1835075Swnj 	tcp_close(tp);
1845164Swnj 	in_pcbdetach(tp->t_inpcb);
1855075Swnj }
1865075Swnj 
1875089Swnj /*
1885089Swnj  * Close a TCP control block:
1895089Swnj  *	discard all space held by the tcp
1905089Swnj  *	discard internet protocol block
1915089Swnj  *	wake up any sleepers
1925089Swnj  */
1935075Swnj tcp_close(tp)
1945075Swnj 	register struct tcpcb *tp;
1955075Swnj {
1965075Swnj 	register struct tcpiphdr *t;
1975089Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1985075Swnj 
1995075Swnj COUNT(TCP_CLOSE);
200*5245Sroot printf("tcp_close %x\n", tp);
2015075Swnj 	t = tp->seg_next;
2025075Swnj 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
2035075Swnj 		m_freem(dtom(t));
2045089Swnj 	if (tp->t_template)
2055075Swnj 		(void) m_free(dtom(tp->t_template));
2065089Swnj 	if (tp->t_tcpopt)
2075089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2085089Swnj 	if (tp->t_ipopt)
2095089Swnj 		(void) m_free(dtom(tp->t_ipopt));
2105075Swnj 	(void) m_free(dtom(tp));
211*5245Sroot 	soisdisconnected(so);
2125164Swnj 	in_pcbdisconnect(tp->t_inpcb);
2135075Swnj }
2145075Swnj 
2155075Swnj tcp_drain()
2165075Swnj {
2175075Swnj 
2185075Swnj COUNT(TCP_DRAIN);
2195075Swnj }
2205075Swnj 
2215075Swnj tcp_ctlinput(m)
2225075Swnj 	struct mbuf *m;
2235075Swnj {
2245075Swnj 
2255075Swnj COUNT(TCP_CTLINPUT);
2265075Swnj 	m_freem(m);
2275075Swnj }
228