tcp_timewait.c revision 6475
1139804Simp/*
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1990, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
987218Srwatson *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)tcp_subr.c	8.1 (Berkeley) 6/10/93
341541Srgrimes * $Id: tcp_subr.c,v 1.6 1995/02/09 23:13:25 wollman Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#include <sys/param.h>
381541Srgrimes#include <sys/proc.h>
391541Srgrimes#include <sys/systm.h>
401541Srgrimes#include <sys/malloc.h>
411541Srgrimes#include <sys/mbuf.h>
42116182Sobrien#include <sys/socket.h>
43116182Sobrien#include <sys/socketvar.h>
44116182Sobrien#include <sys/protosw.h>
4531778Seivind#include <sys/errno.h>
46101001Srwatson
4731778Seivind#include <net/route.h>
481541Srgrimes#include <net/if.h>
4976166Smarkm
501541Srgrimes#include <netinet/in.h>
51132548Srwatson#include <netinet/in_systm.h>
5241059Speter#include <netinet/ip.h>
5370317Sjake#include <netinet/in_pcb.h>
54101173Srwatson#include <netinet/ip_var.h>
5591140Stanimura#include <netinet/ip_icmp.h>
5676166Smarkm#include <netinet/tcp.h>
5791140Stanimura#define	TCPOUTFLAGS
581541Srgrimes#include <netinet/tcp_fsm.h>
5976166Smarkm#include <netinet/tcp_seq.h>
6087218Srwatson#include <netinet/tcp_timer.h>
6131891Ssef#include <netinet/tcp_var.h>
6265495Struckman#include <netinet/tcpip.h>
6392976Srwatson#ifdef TCPDEBUG
6492976Srwatson#include <netinet/tcp_debug.h>
6561287Srwatson#endif
661541Srgrimes
6730354Sphk/* patchable/settable parameters for tcp */
6830354Sphkint 	tcp_mssdflt = TCP_MSS;
6989414Sarrint 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
7089414Sarrint	tcp_do_rfc1323 = 1;
7187138Srwatsonint	tcp_do_rfc1644 = 1;
7287138Srwatsonstatic	void tcp_cleartaocache(void);
7312221Sbde
7411332Sswallaceextern	struct inpcb *tcp_last_inpcb;
751541Srgrimes
761541Srgrimes/*
7712221Sbde * Tcp initialization
7858717Sdillon */
7982749Sdillonvoid
8082749Sdillontcp_init()
811541Srgrimes{
821549Srgrimes
8393580Sjhb	tcp_iss = 1;		/* wrong */
841541Srgrimes	tcp_ccgen = 1;
8583366Sjulian	tcp_cleartaocache();
861541Srgrimes	tcb.inp_next = tcb.inp_prev = &tcb;
8783366Sjulian	if (max_protohdr < sizeof(struct tcpiphdr))
88130344Sphk		max_protohdr = sizeof(struct tcpiphdr);
8974728Sjhb	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
9083366Sjulian		panic("tcp_init");
9174728Sjhb}
921541Srgrimes
931541Srgrimes/*
941541Srgrimes * Create template to be used to send tcp packets on a connection.
951541Srgrimes * Call after host entry created, allocates an mbuf and fills
9612221Sbde * in a skeletal tcp/ip header, minimizing the amount of work
9711332Sswallace * necessary when the connection is used.
9811332Sswallace */
9911332Sswallacestruct tcpiphdr *
10012221Sbdetcp_template(tp)
10182749Sdillon	struct tcpcb *tp;
10282749Sdillon{
10382749Sdillon	register struct inpcb *inp = tp->t_inpcb;
1041541Srgrimes	register struct mbuf *m;
1051549Srgrimes	register struct tcpiphdr *n;
10693580Sjhb
1071541Srgrimes	if ((n = tp->t_template) == 0) {
10883366Sjulian		m = m_get(M_DONTWAIT, MT_HEADER);
1091541Srgrimes		if (m == NULL)
11074728Sjhb			return (0);
11183366Sjulian		m->m_len = sizeof (struct tcpiphdr);
11274728Sjhb		n = mtod(m, struct tcpiphdr *);
1131541Srgrimes	}
1141541Srgrimes	n->ti_next = n->ti_prev = 0;
1151541Srgrimes	n->ti_x1 = 0;
11687466Srwatson	n->ti_pr = IPPROTO_TCP;
11787218Srwatson	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
11858717Sdillon	n->ti_src = inp->inp_laddr;
11912221Sbde	n->ti_dst = inp->inp_faddr;
12011332Sswallace	n->ti_sport = inp->inp_lport;
12111332Sswallace	n->ti_dport = inp->inp_fport;
12211332Sswallace	n->ti_seq = 0;
12312221Sbde	n->ti_ack = 0;
12482749Sdillon	n->ti_x2 = 0;
12582749Sdillon	n->ti_off = 5;
12682749Sdillon	n->ti_flags = 0;
1271549Srgrimes	n->ti_win = 0;
12893580Sjhb	n->ti_sum = 0;
1291541Srgrimes	n->ti_urp = 0;
13083366Sjulian	return (n);
1311541Srgrimes}
13291140Stanimura
13383366Sjulian/*
13491140Stanimura * Send a single message to the TCP at address specified by
1351541Srgrimes * the given TCP/IP header.  If m == 0, then we make a copy
1361541Srgrimes * of the tcpiphdr at ti and send directly to the addressed host.
1371541Srgrimes * This is used to force keep alive messages out using the TCP
13828401Speter * template for a connection tp->t_template.  If flags are given
13912221Sbde * then we send a message back to the TCP which originated the
14028401Speter * segment ti, and discard the mbuf containing it and any other
14128401Speter * attached mbufs.
14228401Speter *
14328401Speter * In any case the ack and sequence number of the transmitted
14482749Sdillon * segment are as specified by the parameters.
14582749Sdillon */
14682749Sdillonvoid
14728401Spetertcp_respond(tp, ti, m, ack, seq, flags)
14893580Sjhb	struct tcpcb *tp;
14928401Speter	register struct tcpiphdr *ti;
150114031Sjhb	register struct mbuf *m;
15192985Sjhb	tcp_seq ack, seq;
15241726Struckman	int flags;
15391140Stanimura{
154114031Sjhb	register int tlen;
15591140Stanimura	int win = 0;
156114031Sjhb	struct route *ro = 0;
157114031Sjhb
158114031Sjhb	if (tp) {
159114031Sjhb		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
160114031Sjhb		ro = &tp->t_inpcb->inp_route;
161114031Sjhb	}
162114031Sjhb	if (m == 0) {
163114031Sjhb		m = m_gethdr(M_DONTWAIT, MT_HEADER);
164114031Sjhb		if (m == NULL)
16575893Sjhb			return;
166114031Sjhb#ifdef TCP_COMPAT_42
167114031Sjhb		tlen = 1;
168114031Sjhb#else
16928401Speter		tlen = 0;
17028401Speter#endif
17128401Speter		m->m_data += max_linkhdr;
17228401Speter		*mtod(m, struct tcpiphdr *) = *ti;
17328401Speter		ti = mtod(m, struct tcpiphdr *);
17428401Speter		flags = TH_ACK;
17528401Speter	} else {
17628401Speter		m_freem(m->m_next);
17728401Speter		m->m_next = 0;
17828401Speter		m->m_data = (caddr_t)ti;
17982749Sdillon		m->m_len = sizeof (struct tcpiphdr);
18082749Sdillon		tlen = 0;
18182749Sdillon#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
18228401Speter		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
18393580Sjhb		xchg(ti->ti_dport, ti->ti_sport, u_short);
18428401Speter#undef xchg
185114031Sjhb	}
18687218Srwatson	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
18741726Struckman	tlen += sizeof (struct tcpiphdr);
18891140Stanimura	m->m_len = tlen;
189114031Sjhb	m->m_pkthdr.len = tlen;
19091140Stanimura	m->m_pkthdr.rcvif = (struct ifnet *) 0;
191114031Sjhb	ti->ti_next = ti->ti_prev = 0;
192114031Sjhb	ti->ti_x1 = 0;
193114031Sjhb	ti->ti_seq = htonl(seq);
194114031Sjhb	ti->ti_ack = htonl(ack);
195114031Sjhb	ti->ti_x2 = 0;
196114031Sjhb	ti->ti_off = sizeof (struct tcphdr) >> 2;
197114031Sjhb	ti->ti_flags = flags;
198114031Sjhb	if (tp)
199114031Sjhb		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
20075893Sjhb	else
201114031Sjhb		ti->ti_win = htons((u_short)win);
202114031Sjhb	ti->ti_urp = 0;
203114031Sjhb	ti->ti_sum = 0;
20428401Speter	ti->ti_sum = in_cksum(m, tlen);
20528401Speter	((struct ip *)ti)->ip_len = tlen;
20628401Speter	((struct ip *)ti)->ip_ttl = ip_defttl;
20711332Sswallace#ifdef TCPDEBUG
20811332Sswallace	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
20911332Sswallace		tcp_trace(TA_OUTPUT, 0, tp, ti, 0);
21012221Sbde#endif
21182749Sdillon	(void) ip_output(m, NULL, ro, 0, NULL);
21282749Sdillon}
21382749Sdillon
2141541Srgrimes/*
2151549Srgrimes * Create a new TCP control block, making an
21693580Sjhb * empty reassembly queue and hooking it to the argument
2171541Srgrimes * protocol control block.
2181541Srgrimes */
21992987Sjhbstruct tcpcb *
220130344Sphktcp_newtcpcb(inp)
22192987Sjhb	struct inpcb *inp;
2221541Srgrimes{
2231541Srgrimes	register struct tcpcb *tp;
2241541Srgrimes
2251541Srgrimes	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
22612221Sbde	if (tp == NULL)
22711332Sswallace		return ((struct tcpcb *)0);
22811332Sswallace	bzero((char *) tp, sizeof(struct tcpcb));
22911332Sswallace	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
23012221Sbde	tp->t_maxseg = tp->t_maxopd = tcp_mssdflt;
23187218Srwatson
23287218Srwatson	if (tcp_do_rfc1323)
23387218Srwatson		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
2341541Srgrimes	if (tcp_do_rfc1644)
2351549Srgrimes		tp->t_flags |= TF_REQ_CC;
23693580Sjhb	tp->t_inpcb = inp;
2371541Srgrimes	/*
23892987Sjhb	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
23992987Sjhb	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
2401541Srgrimes	 * reasonable initial retransmit time.
2411541Srgrimes	 */
2421541Srgrimes	tp->t_srtt = TCPTV_SRTTBASE;
24312221Sbde	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
24411332Sswallace	tp->t_rttmin = TCPTV_MIN;
24511332Sswallace	TCPT_RANGESET(tp->t_rxtcur,
24611332Sswallace	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
24712221Sbde	    TCPTV_MIN, TCPTV_REXMTMAX);
24882749Sdillon	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
24982749Sdillon	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
25082749Sdillon	inp->inp_ip.ip_ttl = ip_defttl;
2511541Srgrimes	inp->inp_ppcb = (caddr_t)tp;
2521549Srgrimes	return (tp);
25393580Sjhb}
2541541Srgrimes
2551541Srgrimes/*
25692987Sjhb * Drop a TCP connection, reporting
257130344Sphk * the specified error.  If connection is synchronized,
25892987Sjhb * then send a RST to peer.
2591541Srgrimes */
2601541Srgrimesstruct tcpcb *
2611541Srgrimestcp_drop(tp, errno)
2621541Srgrimes	register struct tcpcb *tp;
2631541Srgrimes	int errno;
2641541Srgrimes{
2651541Srgrimes	struct socket *so = tp->t_inpcb->inp_socket;
2661541Srgrimes
2671541Srgrimes	if (TCPS_HAVERCVDSYN(tp->t_state)) {
26812221Sbde		tp->t_state = TCPS_CLOSED;
26911332Sswallace		(void) tcp_output(tp);
27011332Sswallace		tcpstat.tcps_drops++;
27111332Sswallace	} else
27212221Sbde		tcpstat.tcps_conndrops++;
27382749Sdillon	if (errno == ETIMEDOUT && tp->t_softerror)
27482749Sdillon		errno = tp->t_softerror;
27582749Sdillon	so->so_error = errno;
2761541Srgrimes	return (tcp_close(tp));
2771549Srgrimes}
27893580Sjhb
2791541Srgrimes/*
2801541Srgrimes * Close a TCP control block:
28192987Sjhb *	discard all space held by the tcp
2821541Srgrimes *	discard internet protocol block
2831541Srgrimes *	wake up any sleepers
2841541Srgrimes */
28512221Sbdestruct tcpcb *
2861541Srgrimestcp_close(tp)
2871541Srgrimes	register struct tcpcb *tp;
2881541Srgrimes{
2891541Srgrimes	register struct tcpiphdr *t;
29012221Sbde	struct inpcb *inp = tp->t_inpcb;
29182749Sdillon	struct socket *so = inp->inp_socket;
29282749Sdillon	register struct mbuf *m;
29382749Sdillon#ifdef RTV_RTT
2941549Srgrimes	register struct rtentry *rt;
29593580Sjhb
2961541Srgrimes	/*
29782749Sdillon	 * If we sent enough data to get some meaningful characteristics,
29877183Srwatson	 * save them in the routing entry.  'Enough' is arbitrarily
29987218Srwatson	 * defined as the sendpipesize (default 4K) * 16.  This would
3001541Srgrimes	 * give us 16 rtt samples assuming we only get one sample per
30192987Sjhb	 * window (the usual case on a long haul net).  16 samples is
3021541Srgrimes	 * enough for the srtt filter to converge to within 5% of the correct
30383366Sjulian	 * value; fewer samples and we could save a very bogus rtt.
30492987Sjhb	 *
3051541Srgrimes	 * Don't update the default route's characteristics and don't
30692987Sjhb	 * update anything that the user "locked".
30792987Sjhb	 */
30877183Srwatson	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
30999009Salfred	    (rt = inp->inp_route.ro_rt) &&
31093264Sdillon	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
31193557Sjhb		register u_long i = 0;
31293264Sdillon
3131541Srgrimes		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
3141541Srgrimes			i = tp->t_srtt *
31512221Sbde			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
31612207Sbde			if (rt->rt_rmx.rmx_rtt && i)
31711332Sswallace				/*
31811332Sswallace				 * filter this update to half the old & half
31912221Sbde				 * the new values, converting scale.
32082749Sdillon				 * See route.h and tcp_var.h for a
32182749Sdillon				 * description of the scaling constants.
32282749Sdillon				 */
3231541Srgrimes				rt->rt_rmx.rmx_rtt =
3241549Srgrimes				    (rt->rt_rmx.rmx_rtt + i) / 2;
32593580Sjhb			else
3261541Srgrimes				rt->rt_rmx.rmx_rtt = i;
32791140Stanimura		}
32882749Sdillon		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
32983366Sjulian			i = tp->t_rttvar *
33091140Stanimura			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
33191140Stanimura			if (rt->rt_rmx.rmx_rttvar && i)
3321541Srgrimes				rt->rt_rmx.rmx_rttvar =
33391140Stanimura				    (rt->rt_rmx.rmx_rttvar + i) / 2;
33491140Stanimura			else
33591140Stanimura				rt->rt_rmx.rmx_rttvar = i;
336111119Simp		}
337111119Simp		/*
33891140Stanimura		 * update the pipelimit (ssthresh) if it has been updated
33994859Sjhb		 * already or if a pipesize was specified & the threshhold
34091140Stanimura		 * got below half the pipesize.  I.e., wait for bad news
34191140Stanimura		 * before we start updating, then update on both good
34291140Stanimura		 * and bad news.
34391140Stanimura		 */
34482749Sdillon		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
34591140Stanimura		    ((i = tp->snd_ssthresh) != 0) && rt->rt_rmx.rmx_ssthresh) ||
34691140Stanimura		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
34783366Sjulian			/*
34894859Sjhb			 * convert the limit from user data bytes to
34994859Sjhb			 * packets then to packet data bytes.
3501541Srgrimes			 */
35191140Stanimura			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
35294859Sjhb			if (i < 2)
35391140Stanimura				i = 2;
35495973Stanimura			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
35595973Stanimura			if (rt->rt_rmx.rmx_ssthresh)
35695973Stanimura				rt->rt_rmx.rmx_ssthresh =
35795973Stanimura				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
35891140Stanimura			else
35994859Sjhb				rt->rt_rmx.rmx_ssthresh = i;
3601541Srgrimes		}
3611541Srgrimes	}
3621541Srgrimes#endif /* RTV_RTT */
3631541Srgrimes	/* free the reassembly queue, if any */
3641541Srgrimes	t = tp->seg_next;
3651541Srgrimes	while (t != (struct tcpiphdr *)tp) {
3661541Srgrimes		t = (struct tcpiphdr *)t->ti_next;
3671541Srgrimes		m = REASS_MBUF((struct tcpiphdr *)t->ti_prev);
3681541Srgrimes		remque(t->ti_prev);
3691541Srgrimes		m_freem(m);
3701541Srgrimes	}
3711541Srgrimes	if (tp->t_template)
3721541Srgrimes		(void) m_free(dtom(tp->t_template));
3731541Srgrimes	free(tp, M_PCB);
3741541Srgrimes	inp->inp_ppcb = 0;
37512221Sbde	soisdisconnected(so);
3761541Srgrimes	/* clobber input pcb cache if we're closing the cached connection */
37787218Srwatson	if (inp == tcp_last_inpcb)
37887218Srwatson		tcp_last_inpcb = &tcb;
3791541Srgrimes	in_pcbdetach(inp);
38012221Sbde	tcpstat.tcps_closed++;
38182749Sdillon	return ((struct tcpcb *)0);
38282749Sdillon}
38382749Sdillon
3841541Srgrimesvoid
3851549Srgrimestcp_drain()
38693580Sjhb{
3871541Srgrimes
38883366Sjulian}
38987218Srwatson
39087218Srwatson/*
39175448Srwatson * Notify a tcp user of an asynchronous error;
39291140Stanimura * store error as soft error, but wake up user
3931541Srgrimes * (for now, won't do anything until can select for soft error).
39420677Sbde */
39520677Sbdevoid
39691140Stanimuratcp_notify(inp, error)
39791140Stanimura	struct inpcb *inp;
39891140Stanimura	int error;
399111119Simp{
40091140Stanimura	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
40194859Sjhb	register struct socket *so = inp->inp_socket;
4021541Srgrimes
40391140Stanimura	/*
40482749Sdillon	 * Ignore some errors if we are hooked up.
40594859Sjhb	 * If connection hasn't completed, has retransmitted several times,
40675893Sjhb	 * and receives a second error, give up now.  This is better
40791140Stanimura	 * than waiting a long time to establish a connection that
40891140Stanimura	 * can never complete.
40991371Stanimura	 */
41094859Sjhb	if (tp->t_state == TCPS_ESTABLISHED &&
41191140Stanimura	     (error == EHOSTUNREACH || error == ENETUNREACH ||
412132568Srwatson	      error == EHOSTDOWN)) {
41375893Sjhb		return;
41494859Sjhb	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
41575893Sjhb	    tp->t_softerror)
41675893Sjhb		so->so_error = error;
41775893Sjhb	else
41875893Sjhb		tp->t_softerror = error;
41982749Sdillon	wakeup((caddr_t) &so->so_timeo);
42094859Sjhb	sorwakeup(so);
42175893Sjhb	sowwakeup(so);
42275893Sjhb}
42375893Sjhb
42482749Sdillonvoid
42594859Sjhbtcp_ctlinput(cmd, sa, ip)
42675893Sjhb	int cmd;
42791140Stanimura	struct sockaddr *sa;
42891140Stanimura	register struct ip *ip;
4291541Srgrimes{
43075893Sjhb	register struct tcphdr *th;
43182749Sdillon	extern struct in_addr zeroin_addr;
43294859Sjhb	extern u_char inetctlerrmap[];
43375893Sjhb	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
43487218Srwatson
4351541Srgrimes	if (cmd == PRC_QUENCH)
436117214Scognet		notify = tcp_quench;
437117214Scognet	else if (!PRC_IS_REDIRECT(cmd) &&
438117214Scognet		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
439117214Scognet		return;
440117214Scognet	if (ip) {
441117214Scognet		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
442117214Scognet		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
443117214Scognet			cmd, notify);
44491140Stanimura	} else
445117214Scognet		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
446117214Scognet}
44794859Sjhb
44875893Sjhb/*
449117214Scognet * When a source quench is received, close congestion window
450117214Scognet * to one segment.  We will gradually open it again as we proceed.
45191140Stanimura */
452117214Scognetvoid
45391140Stanimuratcp_quench(inp, errno)
45491140Stanimura	struct inpcb *inp;
45591140Stanimura	int errno;
45691140Stanimura{
45782749Sdillon	struct tcpcb *tp = intotcpcb(inp);
45891140Stanimura
45994859Sjhb	if (tp)
46094859Sjhb		tp->snd_cwnd = tp->t_maxseg;
46194859Sjhb}
46295973Stanimura
46391140Stanimura/*
46482749Sdillon * Look-up the routing entry to the peer of this inpcb.  If no route
4651541Srgrimes * is found and it cannot be allocated the return NULL.  This routine
4661541Srgrimes * is called by TCP routines that access the rmx structure and by tcp_mss
46724448Speter * to get the interface MTU.
46824448Speter */
46972093Sasmodaistruct rtentry *
47024448Spetertcp_rtlookup(inp)
47124448Speter	struct inpcb *inp;
47224448Speter{
47324448Speter	struct route *ro;
47424448Speter	struct rtentry *rt;
47524448Speter
47624448Speter	ro = &inp->inp_route;
47724448Speter	rt = ro->ro_rt;
47824448Speter	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
47912221Sbde		/* No route yet, so try to acquire one */
4801541Srgrimes		if (inp->inp_faddr.s_addr != INADDR_ANY) {
4811541Srgrimes			ro->ro_dst.sa_family = AF_INET;
4821541Srgrimes			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
48312221Sbde			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
48482749Sdillon				inp->inp_faddr;
48582749Sdillon			rtalloc(ro);
48682749Sdillon			rt = ro->ro_rt;
4871541Srgrimes		}
4881549Srgrimes	}
48993580Sjhb	return rt;
4901541Srgrimes}
49183366Sjulian
49277183Srwatson/*
49377183Srwatson * Return a pointer to the cached information about the remote host.
49498417Salfred * The cached information is stored in the protocol specific part of
49587218Srwatson * the route metrics.
4961541Srgrimes */
49777183Srwatsonstruct rmxp_tao *
49894619Sjhbtcp_gettaocache(inp)
49998417Salfred	struct inpcb *inp;
50094619Sjhb{
50187219Srwatson	struct rtentry *rt = tcp_rtlookup(inp);
50287466Srwatson
50324448Speter	/* Make sure this is a host route and is up. */
50424448Speter	if (rt == NULL ||
50524448Speter	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
50687218Srwatson		return NULL;
50724448Speter
50872093Sasmodai	return rmx_taop(rt->rt_rmx);
50924448Speter}
51024448Speter
51124448Speter/*
51224448Speter * Clear all the TAO cache entries, called from tcp_init.
51324448Speter *
51424448Speter * XXX
51524448Speter * This routine is just an empty one, because we assume that the routing
51687218Srwatson * routing tables are initialized at the same time when TCP, so there is
51724448Speter * nothing in the cache left over.
51824448Speter */
51924448Speterstatic void
52077183Srwatsontcp_cleartaocache(void)
52117994Sache{ }
52277183Srwatson