xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 32789)
123193Smckusick /*
229152Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
3*32789Sbostic  * All rights reserved.
423193Smckusick  *
5*32789Sbostic  * Redistribution and use in source and binary forms are permitted
6*32789Sbostic  * provided that this notice is preserved and that due credit is given
7*32789Sbostic  * to the University of California at Berkeley. The name of the University
8*32789Sbostic  * may not be used to endorse or promote products derived from this
9*32789Sbostic  * software without specific prior written permission. This software
10*32789Sbostic  * is provided ``as is'' without express or implied warranty.
11*32789Sbostic  *
12*32789Sbostic  *	@(#)tcp_subr.c	7.13 (Berkeley) 12/07/87
1323193Smckusick  */
145068Swnj 
1517064Sbloom #include "param.h"
1617064Sbloom #include "systm.h"
1717064Sbloom #include "mbuf.h"
1817064Sbloom #include "socket.h"
1917064Sbloom #include "socketvar.h"
2017064Sbloom #include "protosw.h"
2117064Sbloom #include "errno.h"
2210896Ssam 
2310896Ssam #include "../net/route.h"
2410896Ssam #include "../net/if.h"
2510896Ssam 
2617064Sbloom #include "in.h"
2717064Sbloom #include "in_pcb.h"
2817064Sbloom #include "in_systm.h"
2917064Sbloom #include "ip.h"
3017064Sbloom #include "ip_var.h"
3117064Sbloom #include "ip_icmp.h"
3217064Sbloom #include "tcp.h"
3317064Sbloom #include "tcp_fsm.h"
3417064Sbloom #include "tcp_seq.h"
3517064Sbloom #include "tcp_timer.h"
3617064Sbloom #include "tcp_var.h"
3717064Sbloom #include "tcpip.h"
385068Swnj 
3931395Skarels int	tcp_ttl = TCP_TTL;
4031395Skarels 
415068Swnj /*
425068Swnj  * Tcp initialization
435068Swnj  */
445068Swnj tcp_init()
455068Swnj {
465068Swnj 
475068Swnj 	tcp_iss = 1;		/* wrong */
485068Swnj 	tcb.inp_next = tcb.inp_prev = &tcb;
495068Swnj }
505068Swnj 
515068Swnj /*
525068Swnj  * Create template to be used to send tcp packets on a connection.
535068Swnj  * Call after host entry created, allocates an mbuf and fills
545068Swnj  * in a skeletal tcp/ip header, minimizing the amount of work
555068Swnj  * necessary when the connection is used.
565068Swnj  */
575068Swnj struct tcpiphdr *
585068Swnj tcp_template(tp)
595068Swnj 	struct tcpcb *tp;
605068Swnj {
615068Swnj 	register struct inpcb *inp = tp->t_inpcb;
625068Swnj 	register struct mbuf *m;
635068Swnj 	register struct tcpiphdr *n;
645068Swnj 
6526815Skarels 	if ((n = tp->t_template) == 0) {
6632101Skarels 		m = m_get(M_DONTWAIT, MT_HEADER);
6726815Skarels 		if (m == NULL)
6826815Skarels 			return (0);
6926815Skarels 		m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
7026815Skarels 		m->m_len = sizeof (struct tcpiphdr);
7126815Skarels 		n = mtod(m, struct tcpiphdr *);
7226815Skarels 	}
735068Swnj 	n->ti_next = n->ti_prev = 0;
745068Swnj 	n->ti_x1 = 0;
755068Swnj 	n->ti_pr = IPPROTO_TCP;
765068Swnj 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
775068Swnj 	n->ti_src = inp->inp_laddr;
785068Swnj 	n->ti_dst = inp->inp_faddr;
795068Swnj 	n->ti_sport = inp->inp_lport;
805068Swnj 	n->ti_dport = inp->inp_fport;
815068Swnj 	n->ti_seq = 0;
825089Swnj 	n->ti_ack = 0;
835068Swnj 	n->ti_x2 = 0;
845068Swnj 	n->ti_off = 5;
855068Swnj 	n->ti_flags = 0;
865068Swnj 	n->ti_win = 0;
875068Swnj 	n->ti_sum = 0;
885068Swnj 	n->ti_urp = 0;
895068Swnj 	return (n);
905068Swnj }
915068Swnj 
925068Swnj /*
935164Swnj  * Send a single message to the TCP at address specified by
945164Swnj  * the given TCP/IP header.  If flags==0, then we make a copy
955164Swnj  * of the tcpiphdr at ti and send directly to the addressed host.
965164Swnj  * This is used to force keep alive messages out using the TCP
975164Swnj  * template for a connection tp->t_template.  If flags are given
985164Swnj  * then we send a message back to the TCP which originated the
995164Swnj  * segment ti, and discard the mbuf containing it and any other
1005164Swnj  * attached mbufs.
1015164Swnj  *
1025164Swnj  * In any case the ack and sequence number of the transmitted
1035164Swnj  * segment are as specified by the parameters.
1045068Swnj  */
1055392Swnj tcp_respond(tp, ti, ack, seq, flags)
1065392Swnj 	struct tcpcb *tp;
1075068Swnj 	register struct tcpiphdr *ti;
1085089Swnj 	tcp_seq ack, seq;
1095068Swnj 	int flags;
1105068Swnj {
11130762Skarels 	register struct mbuf *m;
1126212Swnj 	int win = 0, tlen;
1136353Ssam 	struct route *ro = 0;
1145068Swnj 
1156353Ssam 	if (tp) {
1165392Swnj 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
1176353Ssam 		ro = &tp->t_inpcb->inp_route;
1186353Ssam 	}
1195164Swnj 	if (flags == 0) {
1209644Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
12110144Ssam 		if (m == NULL)
1225164Swnj 			return;
12331727Skarels #ifdef TCP_COMPAT_42
12431727Skarels 		tlen = 1;
12531727Skarels #else
12631727Skarels 		tlen = 0;
12731727Skarels #endif
12830762Skarels 		m->m_len = sizeof (struct tcpiphdr) + tlen;
1295164Swnj 		*mtod(m, struct tcpiphdr *) = *ti;
1305164Swnj 		ti = mtod(m, struct tcpiphdr *);
1315164Swnj 		flags = TH_ACK;
1325164Swnj 	} else {
1335245Sroot 		m = dtom(ti);
1345164Swnj 		m_freem(m->m_next);
1355164Swnj 		m->m_next = 0;
1366117Swnj 		m->m_off = (int)ti - (int)m;
13730762Skarels 		tlen = 0;
1385164Swnj 		m->m_len = sizeof (struct tcpiphdr);
1395089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
1405164Swnj 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
1415164Swnj 		xchg(ti->ti_dport, ti->ti_sport, u_short);
1425068Swnj #undef xchg
1435164Swnj 	}
1445089Swnj 	ti->ti_next = ti->ti_prev = 0;
1455089Swnj 	ti->ti_x1 = 0;
1469185Ssam 	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
1478942Sroot 	ti->ti_seq = htonl(seq);
1488942Sroot 	ti->ti_ack = htonl(ack);
1495089Swnj 	ti->ti_x2 = 0;
1505089Swnj 	ti->ti_off = sizeof (struct tcphdr) >> 2;
1515068Swnj 	ti->ti_flags = flags;
1529185Ssam 	ti->ti_win = htons((u_short)win);
1535392Swnj 	ti->ti_urp = 0;
1546304Sroot 	ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
1556212Swnj 	((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
15631395Skarels 	((struct ip *)ti)->ip_ttl = tcp_ttl;
1576353Ssam 	(void) ip_output(m, (struct mbuf *)0, ro, 0);
1585068Swnj }
1595075Swnj 
1605089Swnj /*
1615089Swnj  * Create a new TCP control block, making an
1625089Swnj  * empty reassembly queue and hooking it to the argument
1635089Swnj  * protocol control block.
1645089Swnj  */
1655075Swnj struct tcpcb *
1665075Swnj tcp_newtcpcb(inp)
1675075Swnj 	struct inpcb *inp;
1685075Swnj {
1699644Ssam 	struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
1705075Swnj 	register struct tcpcb *tp;
1715075Swnj 
17210144Ssam 	if (m == NULL)
17310144Ssam 		return ((struct tcpcb *)0);
1745075Swnj 	tp = mtod(m, struct tcpcb *);
1755075Swnj 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
17617317Skarels 	tp->t_maxseg = TCP_MSS;
1776470Sroot 	tp->t_flags = 0;		/* sends options! */
1785075Swnj 	tp->t_inpcb = inp;
17931726Skarels 	/*
18031757Skarels 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
18131757Skarels 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
18231757Skarels 	 * reasonable initial retransmit time.
18331726Skarels 	 */
18431757Skarels 	tp->t_srtt = TCPTV_SRTTBASE;
18531757Skarels 	tp->t_rttvar = TCPTV_SRTTDFLT << 2;
18632374Skarels 	TCPT_RANGESET(tp->t_rxtcur,
18732374Skarels 	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
18832374Skarels 	    TCPTV_MIN, TCPTV_REXMTMAX);
18917359Skarels 	tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd);
19032101Skarels 	tp->snd_ssthresh = 65535;		/* XXX */
1915075Swnj 	inp->inp_ppcb = (caddr_t)tp;
1925075Swnj 	return (tp);
1935075Swnj }
1945075Swnj 
1955089Swnj /*
1965089Swnj  * Drop a TCP connection, reporting
1975089Swnj  * the specified error.  If connection is synchronized,
1985089Swnj  * then send a RST to peer.
1995089Swnj  */
20010395Ssam struct tcpcb *
2015075Swnj tcp_drop(tp, errno)
20210395Ssam 	register struct tcpcb *tp;
2035075Swnj 	int errno;
2045075Swnj {
2055075Swnj 	struct socket *so = tp->t_inpcb->inp_socket;
2065075Swnj 
2075286Sroot 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
2085075Swnj 		tp->t_state = TCPS_CLOSED;
2098776Sroot 		(void) tcp_output(tp);
21030524Skarels 		tcpstat.tcps_drops++;
21130524Skarels 	} else
21230524Skarels 		tcpstat.tcps_conndrops++;
2135075Swnj 	so->so_error = errno;
21410395Ssam 	return (tcp_close(tp));
2155075Swnj }
2165075Swnj 
2175089Swnj /*
2185089Swnj  * Close a TCP control block:
2195089Swnj  *	discard all space held by the tcp
2205089Swnj  *	discard internet protocol block
2215089Swnj  *	wake up any sleepers
2225089Swnj  */
22310395Ssam struct tcpcb *
2245075Swnj tcp_close(tp)
2255075Swnj 	register struct tcpcb *tp;
2265075Swnj {
2275075Swnj 	register struct tcpiphdr *t;
2285261Swnj 	struct inpcb *inp = tp->t_inpcb;
2295261Swnj 	struct socket *so = inp->inp_socket;
23012422Ssam 	register struct mbuf *m;
2315075Swnj 
2325075Swnj 	t = tp->seg_next;
23312422Ssam 	while (t != (struct tcpiphdr *)tp) {
23412422Ssam 		t = (struct tcpiphdr *)t->ti_next;
23512422Ssam 		m = dtom(t->ti_prev);
23612422Ssam 		remque(t->ti_prev);
23712422Ssam 		m_freem(m);
23812422Ssam 	}
2395089Swnj 	if (tp->t_template)
2405075Swnj 		(void) m_free(dtom(tp->t_template));
2415075Swnj 	(void) m_free(dtom(tp));
2425261Swnj 	inp->inp_ppcb = 0;
2436472Sroot 	soisdisconnected(so);
2445269Sroot 	in_pcbdetach(inp);
24530524Skarels 	tcpstat.tcps_closed++;
24610395Ssam 	return ((struct tcpcb *)0);
2475075Swnj }
2485075Swnj 
2495075Swnj tcp_drain()
2505075Swnj {
2515075Swnj 
2525075Swnj }
2535075Swnj 
25430233Skarels /*
25530233Skarels  * Notify a tcp user of an asynchronous error;
25630233Skarels  * just wake up so that he can collect error status.
25730233Skarels  */
25830233Skarels tcp_notify(inp)
25930233Skarels 	register struct inpcb *inp;
26030233Skarels {
26130233Skarels 
26230233Skarels 	wakeup((caddr_t) &inp->inp_socket->so_timeo);
26330233Skarels 	sorwakeup(inp->inp_socket);
26430233Skarels 	sowwakeup(inp->inp_socket);
26530233Skarels }
26624818Skarels tcp_ctlinput(cmd, sa)
2676584Ssam 	int cmd;
26824818Skarels 	struct sockaddr *sa;
2695075Swnj {
2706591Ssam 	extern u_char inetctlerrmap[];
27124818Skarels 	struct sockaddr_in *sin;
27221119Skarels 	int tcp_quench(), in_rtchange();
2736591Ssam 
27424818Skarels 	if ((unsigned)cmd > PRC_NCMDS)
2756591Ssam 		return;
27624818Skarels 	if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
27724818Skarels 		return;
27824818Skarels 	sin = (struct sockaddr_in *)sa;
27924818Skarels 	if (sin->sin_addr.s_addr == INADDR_ANY)
28024818Skarels 		return;
28124818Skarels 
2826591Ssam 	switch (cmd) {
2836591Ssam 
2846591Ssam 	case PRC_QUENCH:
28524818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench);
2866591Ssam 		break;
2876591Ssam 
28824818Skarels 	case PRC_ROUTEDEAD:
28921119Skarels 	case PRC_REDIRECT_NET:
29021119Skarels 	case PRC_REDIRECT_HOST:
29124818Skarels 	case PRC_REDIRECT_TOSNET:
29224818Skarels 	case PRC_REDIRECT_TOSHOST:
29324818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange);
29421119Skarels 		break;
29521119Skarels 
2966591Ssam 	default:
29721119Skarels 		if (inetctlerrmap[cmd] == 0)
29821119Skarels 			return;		/* XXX */
29924818Skarels 		in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd],
30030233Skarels 			tcp_notify);
3016591Ssam 	}
3025075Swnj }
30317359Skarels 
30417359Skarels /*
30517359Skarels  * When a source quench is received, close congestion window
30631442Skarels  * to one segment.  We will gradually open it again as we proceed.
30717359Skarels  */
30817359Skarels tcp_quench(inp)
30917359Skarels 	struct inpcb *inp;
31017359Skarels {
31117359Skarels 	struct tcpcb *tp = intotcpcb(inp);
31217359Skarels 
31324818Skarels 	if (tp)
31431442Skarels 		tp->snd_cwnd = tp->t_maxseg;
31517359Skarels }
316