xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 40691)
123193Smckusick /*
2*40691Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332789Sbostic  * All rights reserved.
423193Smckusick  *
532789Sbostic  * Redistribution and use in source and binary forms are permitted
634855Sbostic  * provided that the above copyright notice and this paragraph are
734855Sbostic  * duplicated in all such forms and that any documentation,
834855Sbostic  * advertising materials, and other materials related to such
934855Sbostic  * distribution and use acknowledge that the software was developed
1034855Sbostic  * by the University of California, Berkeley.  The name of the
1134855Sbostic  * University may not be used to endorse or promote products derived
1234855Sbostic  * from this software without specific prior written permission.
1334855Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434855Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534855Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632789Sbostic  *
17*40691Skarels  *	@(#)tcp_subr.c	7.16 (Berkeley) 04/03/90
1823193Smckusick  */
195068Swnj 
2017064Sbloom #include "param.h"
2117064Sbloom #include "systm.h"
22*40691Skarels #include "malloc.h"
2317064Sbloom #include "mbuf.h"
2417064Sbloom #include "socket.h"
2517064Sbloom #include "socketvar.h"
2617064Sbloom #include "protosw.h"
2717064Sbloom #include "errno.h"
2810896Ssam 
2910896Ssam #include "../net/route.h"
3010896Ssam #include "../net/if.h"
3110896Ssam 
3217064Sbloom #include "in.h"
3317064Sbloom #include "in_systm.h"
3417064Sbloom #include "ip.h"
35*40691Skarels #include "in_pcb.h"
3617064Sbloom #include "ip_var.h"
3717064Sbloom #include "ip_icmp.h"
3817064Sbloom #include "tcp.h"
3917064Sbloom #include "tcp_fsm.h"
4017064Sbloom #include "tcp_seq.h"
4117064Sbloom #include "tcp_timer.h"
4217064Sbloom #include "tcp_var.h"
4317064Sbloom #include "tcpip.h"
445068Swnj 
4531395Skarels int	tcp_ttl = TCP_TTL;
4631395Skarels 
475068Swnj /*
485068Swnj  * Tcp initialization
495068Swnj  */
505068Swnj tcp_init()
515068Swnj {
525068Swnj 
535068Swnj 	tcp_iss = 1;		/* wrong */
545068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
55*40691Skarels 	if (max_protohdr < sizeof(struct tcpiphdr))
56*40691Skarels 		max_protohdr = sizeof(struct tcpiphdr);
57*40691Skarels 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
58*40691Skarels 		panic("tcp_init");
595068Swnj }
605068Swnj 
615068Swnj /*
625068Swnj  * Create template to be used to send tcp packets on a connection.
635068Swnj  * Call after host entry created, allocates an mbuf and fills
645068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
655068Swnj  * necessary when the connection is used.
665068Swnj  */
675068Swnj struct tcpiphdr *
685068Swnj tcp_template(tp)
695068Swnj 	struct tcpcb *tp;
705068Swnj {
715068Swnj 	register struct inpcb *inp = tp->t_inpcb;
725068Swnj 	register struct mbuf *m;
735068Swnj 	register struct tcpiphdr *n;
745068Swnj 
7526815Skarels 	if ((n = tp->t_template) == 0) {
7632101Skarels 		m = m_get(M_DONTWAIT, MT_HEADER);
7726815Skarels 		if (m == NULL)
7826815Skarels 			return (0);
7926815Skarels 		m->m_len = sizeof (struct tcpiphdr);
8026815Skarels 		n = mtod(m, struct tcpiphdr *);
8126815Skarels 	}
825068Swnj 	n->ti_next = n->ti_prev = 0;
835068Swnj 	n->ti_x1 = 0;
845068Swnj 	n->ti_pr = IPPROTO_TCP;
855068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
865068Swnj 	n->ti_src = inp->inp_laddr;
875068Swnj 	n->ti_dst = inp->inp_faddr;
885068Swnj 	n->ti_sport = inp->inp_lport;
895068Swnj 	n->ti_dport = inp->inp_fport;
905068Swnj 	n->ti_seq = 0;
915089Swnj 	n->ti_ack = 0;
925068Swnj 	n->ti_x2 = 0;
935068Swnj 	n->ti_off = 5;
945068Swnj 	n->ti_flags = 0;
955068Swnj 	n->ti_win = 0;
965068Swnj 	n->ti_sum = 0;
975068Swnj 	n->ti_urp = 0;
985068Swnj 	return (n);
995068Swnj }
1005068Swnj 
1015068Swnj /*
1025164Swnj  * Send a single message to the TCP at address specified by
1035164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
1045164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
1055164Swnj  * This is used to force keep alive messages out using the TCP
1065164Swnj  * template for a connection tp->t_template.  If flags are given
1075164Swnj  * then we send a message back to the TCP which originated the
1085164Swnj  * segment ti, and discard the mbuf containing it and any other
1095164Swnj  * attached mbufs.
1105164Swnj  *
1115164Swnj  * In any case the ack and sequence number of the transmitted
1125164Swnj  * segment are as specified by the parameters.
1135068Swnj  */
114*40691Skarels tcp_respond(tp, ti, m, ack, seq, flags)
1155392Swnj 	struct tcpcb *tp;
1165068Swnj 	register struct tcpiphdr *ti;
117*40691Skarels 	register struct mbuf *m;
1185089Swnj 	tcp_seq ack, seq;
1195068Swnj 	int flags;
1205068Swnj {
1216212Swnj 	int win = 0, tlen;
1226353Ssam 	struct route *ro = 0;
1235068Swnj 
1246353Ssam 	if (tp) {
1255392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1266353Ssam 		ro = &tp->t_inpcb->inp_route;
1276353Ssam 	}
128*40691Skarels 	if (m == 0) {
129*40691Skarels 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
13010144Ssam 		if (m == NULL)
1315164Swnj 			return;
13231727Skarels #ifdef TCP_COMPAT_42
13331727Skarels 		tlen = 1;
13431727Skarels #else
13531727Skarels 		tlen = 0;
13631727Skarels #endif
13730762Skarels 		m->m_len = sizeof (struct tcpiphdr) + tlen;
138*40691Skarels 		m->m_data += max_linkhdr;
1395164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1405164Swnj 		ti = mtod(m, struct tcpiphdr *);
1415164Swnj 		flags = TH_ACK;
1425164Swnj 	} else {
1435164Swnj 		m_freem(m->m_next);
1445164Swnj 		m->m_next = 0;
145*40691Skarels 		m->m_data = (caddr_t)ti;
14630762Skarels 		tlen = 0;
1475164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1485089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1495164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1505164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1515068Swnj #undef xchg
1525164Swnj 	}
1535089Swnj 	ti->ti_next = ti->ti_prev = 0;
1545089Swnj 	ti->ti_x1 = 0;
1559185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1568942Sroot 	ti->ti_seq = htonl(seq);
1578942Sroot 	ti->ti_ack = htonl(ack);
1585089Swnj 	ti->ti_x2 = 0;
1595089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1605068Swnj 	ti->ti_flags = flags;
1619185Ssam 	ti->ti_win = htons((u_short)win);
1625392Swnj 	ti->ti_urp = 0;
1636304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1646212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
16531395Skarels 	((struct ip *)ti)->ip_ttl = tcp_ttl;
1666353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1675068Swnj }
1685075Swnj 
1695089Swnj /*
1705089Swnj  * Create a new TCP control block, making an
1715089Swnj  * empty reassembly queue and hooking it to the argument
1725089Swnj  * protocol control block.
1735089Swnj  */
1745075Swnj struct tcpcb *
1755075Swnj tcp_newtcpcb(inp)
1765075Swnj 	struct inpcb *inp;
1775075Swnj {
1789644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1795075Swnj 	register struct tcpcb *tp;
1805075Swnj 
18110144Ssam 	if (m == NULL)
18210144Ssam 		return ((struct tcpcb *)0);
1835075Swnj 	tp = mtod(m, struct tcpcb *);
1845075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
18517317Skarels 	tp->t_maxseg = TCP_MSS;
1866470Sroot 	tp->t_flags = 0;		/* sends options! */
1875075Swnj 	tp->t_inpcb = inp;
18831726Skarels 	/*
18931757Skarels 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
19031757Skarels 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
19131757Skarels 	 * reasonable initial retransmit time.
19231726Skarels 	 */
19331757Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
19431757Skarels 	tp->t_rttvar = TCPTV_SRTTDFLT << 2;
19532374Skarels 	TCPT_RANGESET(tp->t_rxtcur,
19632374Skarels 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
19732374Skarels 	    TCPTV_MIN, TCPTV_REXMTMAX);
19817359Skarels 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
19932101Skarels 	tp->snd_ssthresh = 65535;		/* XXX */
2005075Swnj 	inp->inp_ppcb = (caddr_t)tp;
2015075Swnj 	return (tp);
2025075Swnj }
2035075Swnj 
2045089Swnj /*
2055089Swnj  * Drop a TCP connection, reporting
2065089Swnj  * the specified error.  If connection is synchronized,
2075089Swnj  * then send a RST to peer.
2085089Swnj  */
20910395Ssam struct tcpcb *
2105075Swnj tcp_drop(tp, errno)
21110395Ssam 	register struct tcpcb *tp;
2125075Swnj 	int errno;
2135075Swnj {
2145075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
2155075Swnj 
2165286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
2175075Swnj 		tp->t_state = TCPS_CLOSED;
2188776Sroot 		(void) tcp_output(tp);
21930524Skarels 		tcpstat.tcps_drops++;
22030524Skarels 	} else
22130524Skarels 		tcpstat.tcps_conndrops++;
2225075Swnj 	so->so_error = errno;
22310395Ssam 	return (tcp_close(tp));
2245075Swnj }
2255075Swnj 
2265089Swnj /*
2275089Swnj  * Close a TCP control block:
2285089Swnj  *	discard all space held by the tcp
2295089Swnj  *	discard internet protocol block
2305089Swnj  *	wake up any sleepers
2315089Swnj  */
23210395Ssam struct tcpcb *
2335075Swnj tcp_close(tp)
2345075Swnj 	register struct tcpcb *tp;
2355075Swnj {
2365075Swnj 	register struct tcpiphdr *t;
2375261Swnj 	struct inpcb *inp = tp->t_inpcb;
2385261Swnj 	struct socket *so = inp->inp_socket;
23912422Ssam 	register struct mbuf *m;
2405075Swnj 
2415075Swnj 	t = tp->seg_next;
24212422Ssam 	while (t != (struct tcpiphdr *)tp) {
24312422Ssam 		t = (struct tcpiphdr *)t->ti_next;
24412422Ssam 		m = dtom(t->ti_prev);
24512422Ssam 		remque(t->ti_prev);
24612422Ssam 		m_freem(m);
24712422Ssam 	}
2485089Swnj 	if (tp->t_template)
2495075Swnj 		(void) m_free(dtom(tp->t_template));
2505075Swnj 	(void) m_free(dtom(tp));
2515261Swnj 	inp->inp_ppcb = 0;
2526472Sroot 	soisdisconnected(so);
2535269Sroot 	in_pcbdetach(inp);
25430524Skarels 	tcpstat.tcps_closed++;
25510395Ssam 	return ((struct tcpcb *)0);
2565075Swnj }
2575075Swnj 
2585075Swnj tcp_drain()
2595075Swnj {
2605075Swnj 
2615075Swnj }
2625075Swnj 
26330233Skarels /*
26430233Skarels  * Notify a tcp user of an asynchronous error;
26530233Skarels  * just wake up so that he can collect error status.
26630233Skarels  */
26730233Skarels tcp_notify(inp)
26830233Skarels 	register struct inpcb *inp;
26930233Skarels {
27030233Skarels 
27130233Skarels 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
27230233Skarels 	sorwakeup(inp->inp_socket);
27330233Skarels 	sowwakeup(inp->inp_socket);
27430233Skarels }
275*40691Skarels 
276*40691Skarels tcp_ctlinput(cmd, sa, ip)
2776584Ssam 	int cmd;
27824818Skarels 	struct sockaddr *sa;
279*40691Skarels 	register struct ip *ip;
2805075Swnj {
281*40691Skarels 	register struct tcphdr *th;
282*40691Skarels 	extern struct in_addr zeroin_addr;
2836591Ssam 	extern u_char inetctlerrmap[];
284*40691Skarels 	int (*notify)() = tcp_notify, tcp_quench();
2856591Ssam 
286*40691Skarels 	if (cmd == PRC_QUENCH)
287*40691Skarels 		notify = tcp_quench;
288*40691Skarels 	else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
2896591Ssam 		return;
290*40691Skarels 	if (ip) {
291*40691Skarels 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
292*40691Skarels 		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
293*40691Skarels 			cmd, notify);
294*40691Skarels 	} else
295*40691Skarels 		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
2965075Swnj }
29717359Skarels 
29817359Skarels /*
29917359Skarels  * When a source quench is received, close congestion window
30031442Skarels  * to one segment.  We will gradually open it again as we proceed.
30117359Skarels  */
30217359Skarels tcp_quench(inp)
30317359Skarels 	struct inpcb *inp;
30417359Skarels {
30517359Skarels 	struct tcpcb *tp = intotcpcb(inp);
30617359Skarels 
30724818Skarels 	if (tp)
30831442Skarels 		tp->snd_cwnd = tp->t_maxseg;
30917359Skarels }
310