tcp_timewait.c revision 41187
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
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.2 (Berkeley) 5/24/95
34 *	$Id: tcp_subr.c,v 1.47 1998/09/06 08:17:35 phk Exp $
35 */
36
37#include "opt_compat.h"
38#include "opt_tcpdebug.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/protosw.h>
49
50#include <vm/vm_zone.h>
51
52#include <net/route.h>
53#include <net/if.h>
54
55#define _IP_VHL
56#include <netinet/in.h>
57#include <netinet/in_systm.h>
58#include <netinet/ip.h>
59#include <netinet/in_pcb.h>
60#include <netinet/in_var.h>
61#include <netinet/ip_var.h>
62#include <netinet/tcp.h>
63#include <netinet/tcp_fsm.h>
64#include <netinet/tcp_seq.h>
65#include <netinet/tcp_timer.h>
66#include <netinet/tcp_var.h>
67#include <netinet/tcpip.h>
68#ifdef TCPDEBUG
69#include <netinet/tcp_debug.h>
70#endif
71
72int 	tcp_mssdflt = TCP_MSS;
73SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt,
74	CTLFLAG_RW, &tcp_mssdflt , 0, "");
75
76static int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
77SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt,
78	CTLFLAG_RW, &tcp_rttdflt , 0, "");
79
80static int	tcp_do_rfc1323 = 1;
81SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323,
82	CTLFLAG_RW, &tcp_do_rfc1323 , 0, "");
83
84static int	tcp_do_rfc1644 = 0;
85SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644,
86	CTLFLAG_RW, &tcp_do_rfc1644 , 0, "");
87
88SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, &tcbinfo.ipi_count,
89	   0, "Number of active PCBs");
90
91static void	tcp_cleartaocache __P((void));
92static void	tcp_notify __P((struct inpcb *, int));
93
94/*
95 * Target size of TCP PCB hash tables. Must be a power of two.
96 */
97#ifndef TCBHASHSIZE
98#define TCBHASHSIZE	512
99#endif
100
101/*
102 * This is the actual shape of what we allocate using the zone
103 * allocator.  Doing it this way allows us to protect both structures
104 * using the same generation count, and also eliminates the overhead
105 * of allocating tcpcbs separately.  By hiding the structure here,
106 * we avoid changing most of the rest of the code (although it needs
107 * to be changed, eventually, for greater efficiency).
108 */
109#define	ALIGNMENT	32
110#define	ALIGNM1		(ALIGNMENT - 1)
111struct	inp_tp {
112	union {
113		struct	inpcb inp;
114		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
115	} inp_tp_u;
116	struct	tcpcb tcb;
117};
118#undef ALIGNMENT
119#undef ALIGNM1
120
121/*
122 * Tcp initialization
123 */
124void
125tcp_init()
126{
127
128	tcp_iss = random();	/* wrong, but better than a constant */
129	tcp_ccgen = 1;
130	tcp_cleartaocache();
131	LIST_INIT(&tcb);
132	tcbinfo.listhead = &tcb;
133	tcbinfo.hashbase = hashinit(TCBHASHSIZE, M_PCB, &tcbinfo.hashmask);
134	tcbinfo.porthashbase = hashinit(TCBHASHSIZE, M_PCB,
135					&tcbinfo.porthashmask);
136	tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
137				 ZONE_INTERRUPT, 0);
138	if (max_protohdr < sizeof(struct tcpiphdr))
139		max_protohdr = sizeof(struct tcpiphdr);
140	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
141		panic("tcp_init");
142}
143
144/*
145 * Create template to be used to send tcp packets on a connection.
146 * Call after host entry created, allocates an mbuf and fills
147 * in a skeletal tcp/ip header, minimizing the amount of work
148 * necessary when the connection is used.
149 */
150struct tcpiphdr *
151tcp_template(tp)
152	struct tcpcb *tp;
153{
154	register struct inpcb *inp = tp->t_inpcb;
155	register struct mbuf *m;
156	register struct tcpiphdr *n;
157
158	if ((n = tp->t_template) == 0) {
159		m = m_get(M_DONTWAIT, MT_HEADER);
160		if (m == NULL)
161			return (0);
162		m->m_len = sizeof (struct tcpiphdr);
163		n = mtod(m, struct tcpiphdr *);
164	}
165	bzero(n->ti_x1, sizeof(n->ti_x1));
166	n->ti_pr = IPPROTO_TCP;
167	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
168	n->ti_src = inp->inp_laddr;
169	n->ti_dst = inp->inp_faddr;
170	n->ti_sport = inp->inp_lport;
171	n->ti_dport = inp->inp_fport;
172	n->ti_seq = 0;
173	n->ti_ack = 0;
174	n->ti_x2 = 0;
175	n->ti_off = 5;
176	n->ti_flags = 0;
177	n->ti_win = 0;
178	n->ti_sum = 0;
179	n->ti_urp = 0;
180	return (n);
181}
182
183/*
184 * Send a single message to the TCP at address specified by
185 * the given TCP/IP header.  If m == 0, then we make a copy
186 * of the tcpiphdr at ti and send directly to the addressed host.
187 * This is used to force keep alive messages out using the TCP
188 * template for a connection tp->t_template.  If flags are given
189 * then we send a message back to the TCP which originated the
190 * segment ti, and discard the mbuf containing it and any other
191 * attached mbufs.
192 *
193 * In any case the ack and sequence number of the transmitted
194 * segment are as specified by the parameters.
195 *
196 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
197 */
198void
199tcp_respond(tp, ti, m, ack, seq, flags)
200	struct tcpcb *tp;
201	register struct tcpiphdr *ti;
202	register struct mbuf *m;
203	tcp_seq ack, seq;
204	int flags;
205{
206	register int tlen;
207	int win = 0;
208	struct route *ro = 0;
209	struct route sro;
210
211	if (tp) {
212		if (!(flags & TH_RST))
213			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
214		ro = &tp->t_inpcb->inp_route;
215	} else {
216		ro = &sro;
217		bzero(ro, sizeof *ro);
218	}
219	if (m == 0) {
220		m = m_gethdr(M_DONTWAIT, MT_HEADER);
221		if (m == NULL)
222			return;
223#ifdef TCP_COMPAT_42
224		tlen = 1;
225#else
226		tlen = 0;
227#endif
228		m->m_data += max_linkhdr;
229		*mtod(m, struct tcpiphdr *) = *ti;
230		ti = mtod(m, struct tcpiphdr *);
231		flags = TH_ACK;
232	} else {
233		m_freem(m->m_next);
234		m->m_next = 0;
235		m->m_data = (caddr_t)ti;
236		m->m_len = sizeof (struct tcpiphdr);
237		tlen = 0;
238#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
239		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, n_long);
240		xchg(ti->ti_dport, ti->ti_sport, n_short);
241#undef xchg
242	}
243	ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
244	tlen += sizeof (struct tcpiphdr);
245	m->m_len = tlen;
246	m->m_pkthdr.len = tlen;
247	m->m_pkthdr.rcvif = (struct ifnet *) 0;
248	bzero(ti->ti_x1, sizeof(ti->ti_x1));
249	ti->ti_seq = htonl(seq);
250	ti->ti_ack = htonl(ack);
251	ti->ti_x2 = 0;
252	ti->ti_off = sizeof (struct tcphdr) >> 2;
253	ti->ti_flags = flags;
254	if (tp)
255		ti->ti_win = htons((u_short) (win >> tp->rcv_scale));
256	else
257		ti->ti_win = htons((u_short)win);
258	ti->ti_urp = 0;
259	ti->ti_sum = 0;
260	ti->ti_sum = in_cksum(m, tlen);
261	((struct ip *)ti)->ip_len = tlen;
262	((struct ip *)ti)->ip_ttl = ip_defttl;
263#ifdef TCPDEBUG
264	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
265		tcp_trace(TA_OUTPUT, 0, tp, ti, 0);
266#endif
267	(void) ip_output(m, NULL, ro, 0, NULL);
268	if (ro == &sro && ro->ro_rt) {
269		RTFREE(ro->ro_rt);
270	}
271}
272
273/*
274 * Create a new TCP control block, making an
275 * empty reassembly queue and hooking it to the argument
276 * protocol control block.  The `inp' parameter must have
277 * come from the zone allocator set up in tcp_init().
278 */
279struct tcpcb *
280tcp_newtcpcb(inp)
281	struct inpcb *inp;
282{
283	struct inp_tp *it;
284	register struct tcpcb *tp;
285
286	it = (struct inp_tp *)inp;
287	tp = &it->tcb;
288	bzero((char *) tp, sizeof(struct tcpcb));
289	tp->t_segq = NULL;
290	tp->t_maxseg = tp->t_maxopd = tcp_mssdflt;
291
292	if (tcp_do_rfc1323)
293		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
294	if (tcp_do_rfc1644)
295		tp->t_flags |= TF_REQ_CC;
296	tp->t_inpcb = inp;	/* XXX */
297	/*
298	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
299	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
300	 * reasonable initial retransmit time.
301	 */
302	tp->t_srtt = TCPTV_SRTTBASE;
303	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
304	tp->t_rttmin = TCPTV_MIN;
305	tp->t_rxtcur = TCPTV_RTOBASE;
306	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
307	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
308	inp->inp_ip_ttl = ip_defttl;
309	inp->inp_ppcb = (caddr_t)tp;
310	return (tp);		/* XXX */
311}
312
313/*
314 * Drop a TCP connection, reporting
315 * the specified error.  If connection is synchronized,
316 * then send a RST to peer.
317 */
318struct tcpcb *
319tcp_drop(tp, errno)
320	register struct tcpcb *tp;
321	int errno;
322{
323	struct socket *so = tp->t_inpcb->inp_socket;
324
325	if (TCPS_HAVERCVDSYN(tp->t_state)) {
326		tp->t_state = TCPS_CLOSED;
327		(void) tcp_output(tp);
328		tcpstat.tcps_drops++;
329	} else
330		tcpstat.tcps_conndrops++;
331	if (errno == ETIMEDOUT && tp->t_softerror)
332		errno = tp->t_softerror;
333	so->so_error = errno;
334	return (tcp_close(tp));
335}
336
337/*
338 * Close a TCP control block:
339 *	discard all space held by the tcp
340 *	discard internet protocol block
341 *	wake up any sleepers
342 */
343struct tcpcb *
344tcp_close(tp)
345	register struct tcpcb *tp;
346{
347	register struct mbuf *q;
348	register struct mbuf *nq;
349	struct inpcb *inp = tp->t_inpcb;
350	struct socket *so = inp->inp_socket;
351	register struct mbuf *m;
352	register struct rtentry *rt;
353	int dosavessthresh;
354
355	/*
356	 * If we got enough samples through the srtt filter,
357	 * save the rtt and rttvar in the routing entry.
358	 * 'Enough' is arbitrarily defined as the 16 samples.
359	 * 16 samples is enough for the srtt filter to converge
360	 * to within 5% of the correct value; fewer samples and
361	 * we could save a very bogus rtt.
362	 *
363	 * Don't update the default route's characteristics and don't
364	 * update anything that the user "locked".
365	 */
366	if (tp->t_rttupdated >= 16 &&
367	    (rt = inp->inp_route.ro_rt) &&
368	    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) {
369		register u_long i = 0;
370
371		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
372			i = tp->t_srtt *
373			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
374			if (rt->rt_rmx.rmx_rtt && i)
375				/*
376				 * filter this update to half the old & half
377				 * the new values, converting scale.
378				 * See route.h and tcp_var.h for a
379				 * description of the scaling constants.
380				 */
381				rt->rt_rmx.rmx_rtt =
382				    (rt->rt_rmx.rmx_rtt + i) / 2;
383			else
384				rt->rt_rmx.rmx_rtt = i;
385			tcpstat.tcps_cachedrtt++;
386		}
387		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
388			i = tp->t_rttvar *
389			    (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
390			if (rt->rt_rmx.rmx_rttvar && i)
391				rt->rt_rmx.rmx_rttvar =
392				    (rt->rt_rmx.rmx_rttvar + i) / 2;
393			else
394				rt->rt_rmx.rmx_rttvar = i;
395			tcpstat.tcps_cachedrttvar++;
396		}
397		/*
398		 * The old comment here said:
399		 * update the pipelimit (ssthresh) if it has been updated
400		 * already or if a pipesize was specified & the threshhold
401		 * got below half the pipesize.  I.e., wait for bad news
402		 * before we start updating, then update on both good
403		 * and bad news.
404		 *
405		 * But we want to save the ssthresh even if no pipesize is
406		 * specified explicitly in the route, because such
407		 * connections still have an implicit pipesize specified
408		 * by the global tcp_sendspace.  In the absence of a reliable
409		 * way to calculate the pipesize, it will have to do.
410		 */
411		i = tp->snd_ssthresh;
412		if (rt->rt_rmx.rmx_sendpipe != 0)
413			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
414		else
415			dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
416		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
417		     i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
418		    || dosavessthresh) {
419			/*
420			 * convert the limit from user data bytes to
421			 * packets then to packet data bytes.
422			 */
423			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
424			if (i < 2)
425				i = 2;
426			i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr));
427			if (rt->rt_rmx.rmx_ssthresh)
428				rt->rt_rmx.rmx_ssthresh =
429				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
430			else
431				rt->rt_rmx.rmx_ssthresh = i;
432			tcpstat.tcps_cachedssthresh++;
433		}
434	}
435	/* free the reassembly queue, if any */
436	for (q = tp->t_segq; q; q = nq) {
437		nq = q->m_nextpkt;
438		tp->t_segq = nq;
439		m_freem(q);
440	}
441	if (tp->t_template)
442		(void) m_free(dtom(tp->t_template));
443	inp->inp_ppcb = NULL;
444	soisdisconnected(so);
445	in_pcbdetach(inp);
446	tcpstat.tcps_closed++;
447	return ((struct tcpcb *)0);
448}
449
450void
451tcp_drain()
452{
453
454}
455
456/*
457 * Notify a tcp user of an asynchronous error;
458 * store error as soft error, but wake up user
459 * (for now, won't do anything until can select for soft error).
460 */
461static void
462tcp_notify(inp, error)
463	struct inpcb *inp;
464	int error;
465{
466	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
467	register struct socket *so = inp->inp_socket;
468
469	/*
470	 * Ignore some errors if we are hooked up.
471	 * If connection hasn't completed, has retransmitted several times,
472	 * and receives a second error, give up now.  This is better
473	 * than waiting a long time to establish a connection that
474	 * can never complete.
475	 */
476	if (tp->t_state == TCPS_ESTABLISHED &&
477	     (error == EHOSTUNREACH || error == ENETUNREACH ||
478	      error == EHOSTDOWN)) {
479		return;
480	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
481	    tp->t_softerror)
482		so->so_error = error;
483	else
484		tp->t_softerror = error;
485	wakeup((caddr_t) &so->so_timeo);
486	sorwakeup(so);
487	sowwakeup(so);
488}
489
490static int
491tcp_pcblist SYSCTL_HANDLER_ARGS
492{
493	int error, i, n, s;
494	struct inpcb *inp, **inp_list;
495	inp_gen_t gencnt;
496	struct xinpgen xig;
497
498	/*
499	 * The process of preparing the TCB list is too time-consuming and
500	 * resource-intensive to repeat twice on every request.
501	 */
502	if (req->oldptr == 0) {
503		n = tcbinfo.ipi_count;
504		req->oldidx = 2 * (sizeof xig)
505			+ (n + n/8) * sizeof(struct xtcpcb);
506		return 0;
507	}
508
509	if (req->newptr != 0)
510		return EPERM;
511
512	/*
513	 * OK, now we're committed to doing something.
514	 */
515	s = splnet();
516	gencnt = tcbinfo.ipi_gencnt;
517	n = tcbinfo.ipi_count;
518	splx(s);
519
520	xig.xig_len = sizeof xig;
521	xig.xig_count = n;
522	xig.xig_gen = gencnt;
523	xig.xig_sogen = so_gencnt;
524	error = SYSCTL_OUT(req, &xig, sizeof xig);
525	if (error)
526		return error;
527
528	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
529	if (inp_list == 0)
530		return ENOMEM;
531
532	s = splnet();
533	for (inp = tcbinfo.listhead->lh_first, i = 0; inp && i < n;
534	     inp = inp->inp_list.le_next) {
535		if (inp->inp_gencnt <= gencnt)
536			inp_list[i++] = inp;
537	}
538	splx(s);
539	n = i;
540
541	error = 0;
542	for (i = 0; i < n; i++) {
543		inp = inp_list[i];
544		if (inp->inp_gencnt <= gencnt) {
545			struct xtcpcb xt;
546			xt.xt_len = sizeof xt;
547			/* XXX should avoid extra copy */
548			bcopy(inp, &xt.xt_inp, sizeof *inp);
549			bcopy(inp->inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
550			if (inp->inp_socket)
551				sotoxsocket(inp->inp_socket, &xt.xt_socket);
552			error = SYSCTL_OUT(req, &xt, sizeof xt);
553		}
554	}
555	if (!error) {
556		/*
557		 * Give the user an updated idea of our state.
558		 * If the generation differs from what we told
559		 * her before, she knows that something happened
560		 * while we were processing this request, and it
561		 * might be necessary to retry.
562		 */
563		s = splnet();
564		xig.xig_gen = tcbinfo.ipi_gencnt;
565		xig.xig_sogen = so_gencnt;
566		xig.xig_count = tcbinfo.ipi_count;
567		splx(s);
568		error = SYSCTL_OUT(req, &xig, sizeof xig);
569	}
570	free(inp_list, M_TEMP);
571	return error;
572}
573
574SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
575	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
576
577void
578tcp_ctlinput(cmd, sa, vip)
579	int cmd;
580	struct sockaddr *sa;
581	void *vip;
582{
583	register struct ip *ip = vip;
584	register struct tcphdr *th;
585	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
586
587	if (cmd == PRC_QUENCH)
588		notify = tcp_quench;
589	else if (cmd == PRC_MSGSIZE)
590		notify = tcp_mtudisc;
591	else if (!PRC_IS_REDIRECT(cmd) &&
592		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
593		return;
594	if (ip) {
595		th = (struct tcphdr *)((caddr_t)ip
596				       + (IP_VHL_HL(ip->ip_vhl) << 2));
597		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
598			cmd, notify);
599	} else
600		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
601}
602
603/*
604 * When a source quench is received, close congestion window
605 * to one segment.  We will gradually open it again as we proceed.
606 */
607void
608tcp_quench(inp, errno)
609	struct inpcb *inp;
610	int errno;
611{
612	struct tcpcb *tp = intotcpcb(inp);
613
614	if (tp)
615		tp->snd_cwnd = tp->t_maxseg;
616}
617
618/*
619 * When `need fragmentation' ICMP is received, update our idea of the MSS
620 * based on the new value in the route.  Also nudge TCP to send something,
621 * since we know the packet we just sent was dropped.
622 * This duplicates some code in the tcp_mss() function in tcp_input.c.
623 */
624void
625tcp_mtudisc(inp, errno)
626	struct inpcb *inp;
627	int errno;
628{
629	struct tcpcb *tp = intotcpcb(inp);
630	struct rtentry *rt;
631	struct rmxp_tao *taop;
632	struct socket *so = inp->inp_socket;
633	int offered;
634	int mss;
635
636	if (tp) {
637		rt = tcp_rtlookup(inp);
638		if (!rt || !rt->rt_rmx.rmx_mtu) {
639			tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
640			return;
641		}
642		taop = rmx_taop(rt->rt_rmx);
643		offered = taop->tao_mssopt;
644		mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
645		if (offered)
646			mss = min(mss, offered);
647		/*
648		 * XXX - The above conditional probably violates the TCP
649		 * spec.  The problem is that, since we don't know the
650		 * other end's MSS, we are supposed to use a conservative
651		 * default.  But, if we do that, then MTU discovery will
652		 * never actually take place, because the conservative
653		 * default is much less than the MTUs typically seen
654		 * on the Internet today.  For the moment, we'll sweep
655		 * this under the carpet.
656		 *
657		 * The conservative default might not actually be a problem
658		 * if the only case this occurs is when sending an initial
659		 * SYN with options and data to a host we've never talked
660		 * to before.  Then, they will reply with an MSS value which
661		 * will get recorded and the new parameters should get
662		 * recomputed.  For Further Study.
663		 */
664		if (tp->t_maxopd <= mss)
665			return;
666		tp->t_maxopd = mss;
667
668		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
669		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
670			mss -= TCPOLEN_TSTAMP_APPA;
671		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
672		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
673			mss -= TCPOLEN_CC_APPA;
674#if	(MCLBYTES & (MCLBYTES - 1)) == 0
675		if (mss > MCLBYTES)
676			mss &= ~(MCLBYTES-1);
677#else
678		if (mss > MCLBYTES)
679			mss = mss / MCLBYTES * MCLBYTES;
680#endif
681		if (so->so_snd.sb_hiwat < mss)
682			mss = so->so_snd.sb_hiwat;
683
684		tp->t_maxseg = mss;
685
686		tcpstat.tcps_mturesent++;
687		tp->t_rtt = 0;
688		tp->snd_nxt = tp->snd_una;
689		tcp_output(tp);
690	}
691}
692
693/*
694 * Look-up the routing entry to the peer of this inpcb.  If no route
695 * is found and it cannot be allocated the return NULL.  This routine
696 * is called by TCP routines that access the rmx structure and by tcp_mss
697 * to get the interface MTU.
698 */
699struct rtentry *
700tcp_rtlookup(inp)
701	struct inpcb *inp;
702{
703	struct route *ro;
704	struct rtentry *rt;
705
706	ro = &inp->inp_route;
707	rt = ro->ro_rt;
708	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
709		/* No route yet, so try to acquire one */
710		if (inp->inp_faddr.s_addr != INADDR_ANY) {
711			ro->ro_dst.sa_family = AF_INET;
712			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
713			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
714				inp->inp_faddr;
715			rtalloc(ro);
716			rt = ro->ro_rt;
717		}
718	}
719	return rt;
720}
721
722/*
723 * Return a pointer to the cached information about the remote host.
724 * The cached information is stored in the protocol specific part of
725 * the route metrics.
726 */
727struct rmxp_tao *
728tcp_gettaocache(inp)
729	struct inpcb *inp;
730{
731	struct rtentry *rt = tcp_rtlookup(inp);
732
733	/* Make sure this is a host route and is up. */
734	if (rt == NULL ||
735	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
736		return NULL;
737
738	return rmx_taop(rt->rt_rmx);
739}
740
741/*
742 * Clear all the TAO cache entries, called from tcp_init.
743 *
744 * XXX
745 * This routine is just an empty one, because we assume that the routing
746 * routing tables are initialized at the same time when TCP, so there is
747 * nothing in the cache left over.
748 */
749static void
750tcp_cleartaocache()
751{
752}
753