tcp_timewait.c revision 6283
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)tcp_subr.c	8.1 (Berkeley) 6/10/93
34 * $Id: tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/proc.h>
39#include <sys/systm.h>
40#include <sys/malloc.h>
41#include <sys/mbuf.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/protosw.h>
45#include <sys/errno.h>
46
47#include <net/route.h>
48#include <net/if.h>
49
50#include <netinet/in.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>
53#include <netinet/in_pcb.h>
54#include <netinet/ip_var.h>
55#include <netinet/ip_icmp.h>
56#include <netinet/tcp.h>
57#define	TCPOUTFLAGS
58#include <netinet/tcp_fsm.h>
59#include <netinet/tcp_seq.h>
60#include <netinet/tcp_timer.h>
61#include <netinet/tcp_var.h>
62#include <netinet/tcpip.h>
63#ifdef TCPDEBUG
64#include <netinet/tcp_debug.h>
65#endif
66
67/* patchable/settable parameters for tcp */
68int 	tcp_mssdflt = TCP_MSS;
69int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
70int	tcp_do_rfc1323 = 1;
71#ifdef TTCP
72int	tcp_do_rfc1644 = 1;
73static	void tcp_cleartaocache(void);
74#endif
75
76extern	struct inpcb *tcp_last_inpcb;
77
78/*
79 * Tcp initialization
80 */
81void
82tcp_init()
83{
84
85	tcp_iss = 1;		/* wrong */
86#ifdef TTCP
87	tcp_ccgen = 1;
88	tcp_cleartaocache();
89#endif
90	tcb.inp_next = tcb.inp_prev = &tcb;
91	if (max_protohdr < sizeof(struct tcpiphdr))
92		max_protohdr = sizeof(struct tcpiphdr);
93	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
94		panic("tcp_init");
95}
96
97/*
98 * Create template to be used to send tcp packets on a connection.
99 * Call after host entry created, allocates an mbuf and fills
100 * in a skeletal tcp/ip header, minimizing the amount of work
101 * necessary when the connection is used.
102 */
103struct tcpiphdr *
104tcp_template(tp)
105	struct tcpcb *tp;
106{
107	register struct inpcb *inp = tp->t_inpcb;
108	register struct mbuf *m;
109	register struct tcpiphdr *n;
110
111	if ((n = tp->t_template) == 0) {
112		m = m_get(M_DONTWAIT, MT_HEADER);
113		if (m == NULL)
114			return (0);
115		m->m_len = sizeof (struct tcpiphdr);
116		n = mtod(m, struct tcpiphdr *);
117	}
118	n->ti_next = n->ti_prev = 0;
119	n->ti_x1 = 0;
120	n->ti_pr = IPPROTO_TCP;
121	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
122	n->ti_src = inp->inp_laddr;
123	n->ti_dst = inp->inp_faddr;
124	n->ti_sport = inp->inp_lport;
125	n->ti_dport = inp->inp_fport;
126	n->ti_seq = 0;
127	n->ti_ack = 0;
128	n->ti_x2 = 0;
129	n->ti_off = 5;
130	n->ti_flags = 0;
131	n->ti_win = 0;
132	n->ti_sum = 0;
133	n->ti_urp = 0;
134	return (n);
135}
136
137/*
138 * Send a single message to the TCP at address specified by
139 * the given TCP/IP header.  If m == 0, then we make a copy
140 * of the tcpiphdr at ti and send directly to the addressed host.
141 * This is used to force keep alive messages out using the TCP
142 * template for a connection tp->t_template.  If flags are given
143 * then we send a message back to the TCP which originated the
144 * segment ti, and discard the mbuf containing it and any other
145 * attached mbufs.
146 *
147 * In any case the ack and sequence number of the transmitted
148 * segment are as specified by the parameters.
149 */
150void
151tcp_respond(tp, ti, m, ack, seq, flags)
152	struct tcpcb *tp;
153	register struct tcpiphdr *ti;
154	register struct mbuf *m;
155	tcp_seq ack, seq;
156	int flags;
157{
158	register int tlen;
159	int win = 0;
160	struct route *ro = 0;
161
162	if (tp) {
163		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
164		ro = &tp->t_inpcb->inp_route;
165	}
166	if (m == 0) {
167		m = m_gethdr(M_DONTWAIT, MT_HEADER);
168		if (m == NULL)
169			return;
170#ifdef TCP_COMPAT_42
171		tlen = 1;
172#else
173		tlen = 0;
174#endif
175		m->m_data += max_linkhdr;
176		*mtod(m, struct tcpiphdr *) = *ti;
177		ti = mtod(m, struct tcpiphdr *);
178		flags = TH_ACK;
179	} else {
180		m_freem(m->m_next);
181		m->m_next = 0;
182		m->m_data = (caddr_t)ti;
183		m->m_len = sizeof (struct tcpiphdr);
184		tlen = 0;
185#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
186		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
187		xchg(ti->ti_dport, ti->ti_sport, u_short);
188#undef xchg
189	}
190	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
191	tlen += sizeof (struct tcpiphdr);
192	m->m_len = tlen;
193	m->m_pkthdr.len = tlen;
194	m->m_pkthdr.rcvif = (struct ifnet *) 0;
195	ti->ti_next = ti->ti_prev = 0;
196	ti->ti_x1 = 0;
197	ti->ti_seq = htonl(seq);
198	ti->ti_ack = htonl(ack);
199	ti->ti_x2 = 0;
200	ti->ti_off = sizeof (struct tcphdr) >> 2;
201	ti->ti_flags = flags;
202	if (tp)
203		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
204	else
205		ti->ti_win = htons((u_short)win);
206	ti->ti_urp = 0;
207	ti->ti_sum = 0;
208	ti->ti_sum = in_cksum(m, tlen);
209	((struct ip *)ti)->ip_len = tlen;
210	((struct ip *)ti)->ip_ttl = ip_defttl;
211#ifdef TCPDEBUG
212	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
213		tcp_trace(TA_OUTPUT, 0, tp, ti, 0);
214#endif
215	(void) ip_output(m, NULL, ro, 0, NULL);
216}
217
218/*
219 * Create a new TCP control block, making an
220 * empty reassembly queue and hooking it to the argument
221 * protocol control block.
222 */
223struct tcpcb *
224tcp_newtcpcb(inp)
225	struct inpcb *inp;
226{
227	register struct tcpcb *tp;
228
229	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
230	if (tp == NULL)
231		return ((struct tcpcb *)0);
232	bzero((char *) tp, sizeof(struct tcpcb));
233	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
234	tp->t_maxseg = tp->t_maxopd = tcp_mssdflt;
235
236	if (tcp_do_rfc1323)
237		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
238#ifdef TTCP
239	if (tcp_do_rfc1644)
240		tp->t_flags |= TF_REQ_CC;
241#endif
242	tp->t_inpcb = inp;
243	/*
244	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
245	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
246	 * reasonable initial retransmit time.
247	 */
248	tp->t_srtt = TCPTV_SRTTBASE;
249	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
250	tp->t_rttmin = TCPTV_MIN;
251	TCPT_RANGESET(tp->t_rxtcur,
252	    ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
253	    TCPTV_MIN, TCPTV_REXMTMAX);
254	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
255	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
256	inp->inp_ip.ip_ttl = ip_defttl;
257	inp->inp_ppcb = (caddr_t)tp;
258	return (tp);
259}
260
261/*
262 * Drop a TCP connection, reporting
263 * the specified error.  If connection is synchronized,
264 * then send a RST to peer.
265 */
266struct tcpcb *
267tcp_drop(tp, errno)
268	register struct tcpcb *tp;
269	int errno;
270{
271	struct socket *so = tp->t_inpcb->inp_socket;
272
273	if (TCPS_HAVERCVDSYN(tp->t_state)) {
274		tp->t_state = TCPS_CLOSED;
275		(void) tcp_output(tp);
276		tcpstat.tcps_drops++;
277	} else
278		tcpstat.tcps_conndrops++;
279	if (errno == ETIMEDOUT && tp->t_softerror)
280		errno = tp->t_softerror;
281	so->so_error = errno;
282	return (tcp_close(tp));
283}
284
285/*
286 * Close a TCP control block:
287 *	discard all space held by the tcp
288 *	discard internet protocol block
289 *	wake up any sleepers
290 */
291struct tcpcb *
292tcp_close(tp)
293	register struct tcpcb *tp;
294{
295	register struct tcpiphdr *t;
296	struct inpcb *inp = tp->t_inpcb;
297	struct socket *so = inp->inp_socket;
298	register struct mbuf *m;
299#ifdef RTV_RTT
300	register struct rtentry *rt;
301
302	/*
303	 * If we sent enough data to get some meaningful characteristics,
304	 * save them in the routing entry.  'Enough' is arbitrarily
305	 * defined as the sendpipesize (default 4K) * 16.  This would
306	 * give us 16 rtt samples assuming we only get one sample per
307	 * window (the usual case on a long haul net).  16 samples is
308	 * enough for the srtt filter to converge to within 5% of the correct
309	 * value; fewer samples and we could save a very bogus rtt.
310	 *
311	 * Don't update the default route's characteristics and don't
312	 * update anything that the user "locked".
313	 */
314	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
315	    (rt = inp->inp_route.ro_rt) &&
316	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
317		register u_long i = 0;
318
319		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
320			i = tp->t_srtt *
321			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
322			if (rt->rt_rmx.rmx_rtt && i)
323				/*
324				 * filter this update to half the old & half
325				 * the new values, converting scale.
326				 * See route.h and tcp_var.h for a
327				 * description of the scaling constants.
328				 */
329				rt->rt_rmx.rmx_rtt =
330				    (rt->rt_rmx.rmx_rtt + i) / 2;
331			else
332				rt->rt_rmx.rmx_rtt = i;
333		}
334		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
335			i = tp->t_rttvar *
336			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
337			if (rt->rt_rmx.rmx_rttvar && i)
338				rt->rt_rmx.rmx_rttvar =
339				    (rt->rt_rmx.rmx_rttvar + i) / 2;
340			else
341				rt->rt_rmx.rmx_rttvar = i;
342		}
343		/*
344		 * update the pipelimit (ssthresh) if it has been updated
345		 * already or if a pipesize was specified & the threshhold
346		 * got below half the pipesize.  I.e., wait for bad news
347		 * before we start updating, then update on both good
348		 * and bad news.
349		 */
350		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
351		    ((i = tp->snd_ssthresh) != 0) && rt->rt_rmx.rmx_ssthresh) ||
352		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
353			/*
354			 * convert the limit from user data bytes to
355			 * packets then to packet data bytes.
356			 */
357			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
358			if (i < 2)
359				i = 2;
360			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
361			if (rt->rt_rmx.rmx_ssthresh)
362				rt->rt_rmx.rmx_ssthresh =
363				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
364			else
365				rt->rt_rmx.rmx_ssthresh = i;
366		}
367	}
368#endif /* RTV_RTT */
369	/* free the reassembly queue, if any */
370	t = tp->seg_next;
371	while (t != (struct tcpiphdr *)tp) {
372		t = (struct tcpiphdr *)t->ti_next;
373		m = REASS_MBUF((struct tcpiphdr *)t->ti_prev);
374		remque(t->ti_prev);
375		m_freem(m);
376	}
377	if (tp->t_template)
378		(void) m_free(dtom(tp->t_template));
379	free(tp, M_PCB);
380	inp->inp_ppcb = 0;
381	soisdisconnected(so);
382	/* clobber input pcb cache if we're closing the cached connection */
383	if (inp == tcp_last_inpcb)
384		tcp_last_inpcb = &tcb;
385	in_pcbdetach(inp);
386	tcpstat.tcps_closed++;
387	return ((struct tcpcb *)0);
388}
389
390void
391tcp_drain()
392{
393
394}
395
396/*
397 * Notify a tcp user of an asynchronous error;
398 * store error as soft error, but wake up user
399 * (for now, won't do anything until can select for soft error).
400 */
401void
402tcp_notify(inp, error)
403	struct inpcb *inp;
404	int error;
405{
406	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
407	register struct socket *so = inp->inp_socket;
408
409	/*
410	 * Ignore some errors if we are hooked up.
411	 * If connection hasn't completed, has retransmitted several times,
412	 * and receives a second error, give up now.  This is better
413	 * than waiting a long time to establish a connection that
414	 * can never complete.
415	 */
416	if (tp->t_state == TCPS_ESTABLISHED &&
417	     (error == EHOSTUNREACH || error == ENETUNREACH ||
418	      error == EHOSTDOWN)) {
419		return;
420	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
421	    tp->t_softerror)
422		so->so_error = error;
423	else
424		tp->t_softerror = error;
425	wakeup((caddr_t) &so->so_timeo);
426	sorwakeup(so);
427	sowwakeup(so);
428}
429
430void
431tcp_ctlinput(cmd, sa, ip)
432	int cmd;
433	struct sockaddr *sa;
434	register struct ip *ip;
435{
436	register struct tcphdr *th;
437	extern struct in_addr zeroin_addr;
438	extern u_char inetctlerrmap[];
439	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
440
441	if (cmd == PRC_QUENCH)
442		notify = tcp_quench;
443	else if (!PRC_IS_REDIRECT(cmd) &&
444		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
445		return;
446	if (ip) {
447		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
448		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
449			cmd, notify);
450	} else
451		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
452}
453
454/*
455 * When a source quench is received, close congestion window
456 * to one segment.  We will gradually open it again as we proceed.
457 */
458void
459tcp_quench(inp, errno)
460	struct inpcb *inp;
461	int errno;
462{
463	struct tcpcb *tp = intotcpcb(inp);
464
465	if (tp)
466		tp->snd_cwnd = tp->t_maxseg;
467}
468
469/*
470 * Look-up the routing entry to the peer of this inpcb.  If no route
471 * is found and it cannot be allocated the return NULL.  This routine
472 * is called by TCP routines that access the rmx structure and by tcp_mss
473 * to get the interface MTU.
474 */
475struct rtentry *
476tcp_rtlookup(inp)
477	struct inpcb *inp;
478{
479	struct route *ro;
480	struct rtentry *rt;
481
482	ro = &inp->inp_route;
483	rt = ro->ro_rt;
484	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
485		/* No route yet, so try to acquire one */
486		if (inp->inp_faddr.s_addr != INADDR_ANY) {
487			ro->ro_dst.sa_family = AF_INET;
488			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
489			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
490				inp->inp_faddr;
491			rtalloc(ro);
492			rt = ro->ro_rt;
493		}
494	}
495	return rt;
496}
497
498#ifdef TTCP
499/*
500 * Return a pointer to the cached information about the remote host.
501 * The cached information is stored in the protocol specific part of
502 * the route metrics.
503 */
504struct rmxp_tao *
505tcp_gettaocache(inp)
506	struct inpcb *inp;
507{
508	struct rtentry *rt = tcp_rtlookup(inp);
509
510	/* Make sure this is a host route and is up. */
511	if (rt == NULL ||
512	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
513		return NULL;
514
515	return rmx_taop(rt->rt_rmx);
516}
517
518/*
519 * Clear all the TAO cache entries, called from tcp_init.
520 *
521 * XXX
522 * This routine is just an empty one, because we assume that the routing
523 * routing tables are initialized at the same time when TCP, so there is
524 * nothing in the cache left over.
525 */
526static void
527tcp_cleartaocache(void)
528{ }
529#endif /* TTCP */
530