xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 6472)
1*6472Sroot /*	tcp_subr.c	4.23	82/04/04	*/
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"
106353Ssam #include "../net/route.h"
115089Swnj #include "../net/in_pcb.h"
125089Swnj #include "../net/in_systm.h"
135068Swnj #include "../net/if.h"
145068Swnj #include "../net/ip.h"
155068Swnj #include "../net/ip_var.h"
165068Swnj #include "../net/tcp.h"
175068Swnj #include "../net/tcp_fsm.h"
185089Swnj #include "../net/tcp_seq.h"
195089Swnj #include "../net/tcp_timer.h"
205068Swnj #include "../net/tcp_var.h"
215089Swnj #include "../net/tcpip.h"
225111Swnj #include "../errno.h"
235068Swnj 
245068Swnj /*
255068Swnj  * Tcp initialization
265068Swnj  */
275068Swnj tcp_init()
285068Swnj {
295068Swnj 
305089Swnj COUNT(TCP_INIT);
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 
515068Swnj COUNT(TCP_TEMPLATE);
525852Sroot 	m = m_get(M_WAIT);
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  */
905392Swnj tcp_respond(tp, ti, ack, seq, flags)
915392Swnj 	struct tcpcb *tp;
925068Swnj 	register struct tcpiphdr *ti;
935089Swnj 	tcp_seq ack, seq;
945068Swnj 	int flags;
955068Swnj {
965164Swnj 	struct mbuf *m;
976212Swnj 	int win = 0, tlen;
986353Ssam 	struct route *ro = 0;
995068Swnj 
1005089Swnj COUNT(TCP_RESPOND);
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) {
1065585Sroot 		m = m_get(M_DONTWAIT);
1075164Swnj 		if (m == 0)
1085164Swnj 			return;
1095164Swnj 		m->m_off = MMINOFF;
1106212Swnj 		m->m_len = sizeof (struct tcpiphdr) + 1;
1115164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1125164Swnj 		ti = mtod(m, struct tcpiphdr *);
1135164Swnj 		flags = TH_ACK;
1146212Swnj 		tlen = 1;
1155164Swnj 	} else {
1165245Sroot 		m = dtom(ti);
1175164Swnj 		m_freem(m->m_next);
1185164Swnj 		m->m_next = 0;
1196117Swnj 		m->m_off = (int)ti - (int)m;
1205164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1215089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1225164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1235164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1245068Swnj #undef xchg
1256212Swnj 		tlen = 0;
1265164Swnj 	}
1275089Swnj 	ti->ti_next = ti->ti_prev = 0;
1285089Swnj 	ti->ti_x1 = 0;
1296212Swnj 	ti->ti_len = sizeof (struct tcphdr) + tlen;
1305245Sroot 	ti->ti_seq = seq;
1315245Sroot 	ti->ti_ack = ack;
1325245Sroot #if vax
1336163Ssam 	ti->ti_len = htons((u_short)ti->ti_len);
1345245Sroot 	ti->ti_seq = htonl(ti->ti_seq);
1355245Sroot 	ti->ti_ack = htonl(ti->ti_ack);
1365245Sroot #endif
1375089Swnj 	ti->ti_x2 = 0;
1385089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1395068Swnj 	ti->ti_flags = flags;
1405392Swnj 	ti->ti_win = win;
1415392Swnj #if vax
1425392Swnj 	ti->ti_win = htons(ti->ti_win);
1435392Swnj #endif
1445392Swnj 	ti->ti_urp = 0;
1456304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1466212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
1475089Swnj 	((struct ip *)ti)->ip_ttl = TCP_TTL;
1486353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1495068Swnj }
1505075Swnj 
1515089Swnj /*
1525089Swnj  * Create a new TCP control block, making an
1535089Swnj  * empty reassembly queue and hooking it to the argument
1545089Swnj  * protocol control block.
1555089Swnj  */
1565075Swnj struct tcpcb *
1575075Swnj tcp_newtcpcb(inp)
1585075Swnj 	struct inpcb *inp;
1595075Swnj {
1605852Sroot 	struct mbuf *m = m_getclr(M_DONTWAIT);
1615075Swnj 	register struct tcpcb *tp;
1625075Swnj COUNT(TCP_NEWTCPCB);
1635075Swnj 
1645075Swnj 	if (m == 0)
1655075Swnj 		return (0);
1665075Swnj 	tp = mtod(m, struct tcpcb *);
1675075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
1685089Swnj 	tp->t_maxseg = 1024;
1696470Sroot 	tp->t_flags = 0;		/* sends options! */
1705075Swnj 	tp->t_inpcb = inp;
1715075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1725075Swnj 	return (tp);
1735075Swnj }
1745075Swnj 
1755089Swnj /*
1765089Swnj  * Drop a TCP connection, reporting
1775089Swnj  * the specified error.  If connection is synchronized,
1785089Swnj  * then send a RST to peer.
1795089Swnj  */
1805075Swnj tcp_drop(tp, errno)
1815075Swnj 	struct tcpcb *tp;
1825075Swnj 	int errno;
1835075Swnj {
1845075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
1855075Swnj 
1865075Swnj COUNT(TCP_DROP);
1875286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
1885075Swnj 		tp->t_state = TCPS_CLOSED;
1895075Swnj 		tcp_output(tp);
1905075Swnj 	}
1915075Swnj 	so->so_error = errno;
1925075Swnj 	tcp_close(tp);
1935075Swnj }
1945075Swnj 
1955089Swnj /*
1965089Swnj  * Close a TCP control block:
1975089Swnj  *	discard all space held by the tcp
1985089Swnj  *	discard internet protocol block
1995089Swnj  *	wake up any sleepers
2005089Swnj  */
2015075Swnj tcp_close(tp)
2025075Swnj 	register struct tcpcb *tp;
2035075Swnj {
2045075Swnj 	register struct tcpiphdr *t;
2055261Swnj 	struct inpcb *inp = tp->t_inpcb;
2065261Swnj 	struct socket *so = inp->inp_socket;
2075075Swnj 
2085075Swnj COUNT(TCP_CLOSE);
2095075Swnj 	t = tp->seg_next;
2105075Swnj 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
2115075Swnj 		m_freem(dtom(t));
2125089Swnj 	if (tp->t_template)
2135075Swnj 		(void) m_free(dtom(tp->t_template));
2145089Swnj 	if (tp->t_tcpopt)
2155089Swnj 		(void) m_free(dtom(tp->t_tcpopt));
2165089Swnj 	if (tp->t_ipopt)
2175089Swnj 		(void) m_free(dtom(tp->t_ipopt));
2185075Swnj 	(void) m_free(dtom(tp));
2195261Swnj 	inp->inp_ppcb = 0;
220*6472Sroot 	soisdisconnected(so);
2215269Sroot 	in_pcbdetach(inp);
2225075Swnj }
2235075Swnj 
2245075Swnj tcp_drain()
2255075Swnj {
2265075Swnj 
2275075Swnj COUNT(TCP_DRAIN);
2285075Swnj }
2295075Swnj 
2305075Swnj tcp_ctlinput(m)
2315075Swnj 	struct mbuf *m;
2325075Swnj {
2335075Swnj 
2345075Swnj COUNT(TCP_CTLINPUT);
2355075Swnj 	m_freem(m);
2365075Swnj }
237