xref: /netbsd-src/sys/netinet/tcp_subr.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: tcp_subr.c,v 1.38 1998/01/05 10:32:09 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
36  */
37 
38 #include "rnd.h"
39 
40 #include <sys/param.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/protosw.h>
48 #include <sys/errno.h>
49 #include <sys/kernel.h>
50 #if NRND > 0
51 #include <sys/rnd.h>
52 #endif
53 
54 #include <net/route.h>
55 #include <net/if.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_icmp.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_seq.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/tcpip.h>
69 
70 /* patchable/settable parameters for tcp */
71 int 	tcp_mssdflt = TCP_MSS;
72 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
73 int	tcp_do_rfc1323 = 1;
74 int	tcp_init_win = 1;
75 
76 #ifndef TCBHASHSIZE
77 #define	TCBHASHSIZE	128
78 #endif
79 int	tcbhashsize = TCBHASHSIZE;
80 
81 int	tcp_freeq __P((struct tcpcb *));
82 
83 /*
84  * Tcp initialization
85  */
86 void
87 tcp_init()
88 {
89 
90 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
91 	LIST_INIT(&tcp_delacks);
92 	if (max_protohdr < sizeof(struct tcpiphdr))
93 		max_protohdr = sizeof(struct tcpiphdr);
94 	if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN)
95 		panic("tcp_init");
96 }
97 
98 /*
99  * Create template to be used to send tcp packets on a connection.
100  * Call after host entry created, allocates an mbuf and fills
101  * in a skeletal tcp/ip header, minimizing the amount of work
102  * necessary when the connection is used.
103  */
104 struct tcpiphdr *
105 tcp_template(tp)
106 	struct tcpcb *tp;
107 {
108 	register struct inpcb *inp = tp->t_inpcb;
109 	register struct tcpiphdr *n;
110 
111 	if ((n = tp->t_template) == 0) {
112 		MALLOC(n, struct tcpiphdr *, sizeof (struct tcpiphdr),
113 		    M_MBUF, M_NOWAIT);
114 		if (n == NULL)
115 			return (0);
116 	}
117 	bzero(n->ti_x1, sizeof n->ti_x1);
118 	n->ti_pr = IPPROTO_TCP;
119 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
120 	n->ti_src = inp->inp_laddr;
121 	n->ti_dst = inp->inp_faddr;
122 	n->ti_sport = inp->inp_lport;
123 	n->ti_dport = inp->inp_fport;
124 	n->ti_seq = 0;
125 	n->ti_ack = 0;
126 	n->ti_x2 = 0;
127 	n->ti_off = 5;
128 	n->ti_flags = 0;
129 	n->ti_win = 0;
130 	n->ti_sum = 0;
131 	n->ti_urp = 0;
132 	return (n);
133 }
134 
135 /*
136  * Send a single message to the TCP at address specified by
137  * the given TCP/IP header.  If m == 0, then we make a copy
138  * of the tcpiphdr at ti and send directly to the addressed host.
139  * This is used to force keep alive messages out using the TCP
140  * template for a connection tp->t_template.  If flags are given
141  * then we send a message back to the TCP which originated the
142  * segment ti, and discard the mbuf containing it and any other
143  * attached mbufs.
144  *
145  * In any case the ack and sequence number of the transmitted
146  * segment are as specified by the parameters.
147  */
148 int
149 tcp_respond(tp, ti, m, ack, seq, flags)
150 	struct tcpcb *tp;
151 	register struct tcpiphdr *ti;
152 	register struct mbuf *m;
153 	tcp_seq ack, seq;
154 	int flags;
155 {
156 	register int tlen;
157 	int win = 0;
158 	struct route *ro = 0;
159 
160 	if (tp) {
161 		win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
162 		ro = &tp->t_inpcb->inp_route;
163 	}
164 	if (m == 0) {
165 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
166 		if (m == NULL)
167 			return (ENOBUFS);
168 #ifdef TCP_COMPAT_42
169 		tlen = 1;
170 #else
171 		tlen = 0;
172 #endif
173 		m->m_data += max_linkhdr;
174 		*mtod(m, struct tcpiphdr *) = *ti;
175 		ti = mtod(m, struct tcpiphdr *);
176 		flags = TH_ACK;
177 	} else {
178 		m_freem(m->m_next);
179 		m->m_next = 0;
180 		m->m_data = (caddr_t)ti;
181 		m->m_len = sizeof (struct tcpiphdr);
182 		tlen = 0;
183 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
184 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
185 		xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
186 #undef xchg
187 	}
188 	bzero(ti->ti_x1, sizeof ti->ti_x1);
189 	ti->ti_seq = htonl(seq);
190 	ti->ti_ack = htonl(ack);
191 	ti->ti_x2 = 0;
192 	if ((flags & TH_SYN) == 0) {
193 		if (tp)
194 			ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
195 		else
196 			ti->ti_win = htons((u_int16_t)win);
197 		ti->ti_off = sizeof (struct tcphdr) >> 2;
198 		tlen += sizeof (struct tcphdr);
199 	} else
200 		tlen += ti->ti_off << 2;
201 	ti->ti_len = htons((u_int16_t)tlen);
202 	tlen += sizeof (struct ip);
203 	m->m_len = tlen;
204 	m->m_pkthdr.len = tlen;
205 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
206 	ti->ti_flags = flags;
207 	ti->ti_urp = 0;
208 	ti->ti_sum = 0;
209 	ti->ti_sum = in_cksum(m, tlen);
210 	((struct ip *)ti)->ip_len = tlen;
211 	((struct ip *)ti)->ip_ttl = ip_defttl;
212 	return ip_output(m, NULL, ro, 0, NULL);
213 }
214 
215 /*
216  * Create a new TCP control block, making an
217  * empty reassembly queue and hooking it to the argument
218  * protocol control block.
219  */
220 struct tcpcb *
221 tcp_newtcpcb(inp)
222 	struct inpcb *inp;
223 {
224 	register struct tcpcb *tp;
225 
226 	tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT);
227 	if (tp == NULL)
228 		return ((struct tcpcb *)0);
229 	bzero((caddr_t)tp, sizeof(struct tcpcb));
230 	LIST_INIT(&tp->segq);
231 	tp->t_peermss = tcp_mssdflt;
232 	tp->t_ourmss = tcp_mssdflt;
233 	tp->t_segsz = tcp_mssdflt;
234 
235 	tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
236 	tp->t_inpcb = inp;
237 	/*
238 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
239 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
240 	 * reasonable initial retransmit time.
241 	 */
242 	tp->t_srtt = TCPTV_SRTTBASE;
243 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
244 	tp->t_rttmin = TCPTV_MIN;
245 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
246 	    TCPTV_MIN, TCPTV_REXMTMAX);
247 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
248 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
249 	inp->inp_ip.ip_ttl = ip_defttl;
250 	inp->inp_ppcb = (caddr_t)tp;
251 	return (tp);
252 }
253 
254 /*
255  * Drop a TCP connection, reporting
256  * the specified error.  If connection is synchronized,
257  * then send a RST to peer.
258  */
259 struct tcpcb *
260 tcp_drop(tp, errno)
261 	register struct tcpcb *tp;
262 	int errno;
263 {
264 	struct socket *so = tp->t_inpcb->inp_socket;
265 
266 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
267 		tp->t_state = TCPS_CLOSED;
268 		(void) tcp_output(tp);
269 		tcpstat.tcps_drops++;
270 	} else
271 		tcpstat.tcps_conndrops++;
272 	if (errno == ETIMEDOUT && tp->t_softerror)
273 		errno = tp->t_softerror;
274 	so->so_error = errno;
275 	return (tcp_close(tp));
276 }
277 
278 /*
279  * Close a TCP control block:
280  *	discard all space held by the tcp
281  *	discard internet protocol block
282  *	wake up any sleepers
283  */
284 struct tcpcb *
285 tcp_close(tp)
286 	register struct tcpcb *tp;
287 {
288 	struct inpcb *inp = tp->t_inpcb;
289 	struct socket *so = inp->inp_socket;
290 #ifdef RTV_RTT
291 	register struct rtentry *rt;
292 
293 	/*
294 	 * If we sent enough data to get some meaningful characteristics,
295 	 * save them in the routing entry.  'Enough' is arbitrarily
296 	 * defined as the sendpipesize (default 4K) * 16.  This would
297 	 * give us 16 rtt samples assuming we only get one sample per
298 	 * window (the usual case on a long haul net).  16 samples is
299 	 * enough for the srtt filter to converge to within 5% of the correct
300 	 * value; fewer samples and we could save a very bogus rtt.
301 	 *
302 	 * Don't update the default route's characteristics and don't
303 	 * update anything that the user "locked".
304 	 */
305 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
306 	    (rt = inp->inp_route.ro_rt) &&
307 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
308 		register u_long i = 0;
309 
310 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
311 			i = tp->t_srtt *
312 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
313 			if (rt->rt_rmx.rmx_rtt && i)
314 				/*
315 				 * filter this update to half the old & half
316 				 * the new values, converting scale.
317 				 * See route.h and tcp_var.h for a
318 				 * description of the scaling constants.
319 				 */
320 				rt->rt_rmx.rmx_rtt =
321 				    (rt->rt_rmx.rmx_rtt + i) / 2;
322 			else
323 				rt->rt_rmx.rmx_rtt = i;
324 		}
325 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
326 			i = tp->t_rttvar *
327 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
328 			if (rt->rt_rmx.rmx_rttvar && i)
329 				rt->rt_rmx.rmx_rttvar =
330 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
331 			else
332 				rt->rt_rmx.rmx_rttvar = i;
333 		}
334 		/*
335 		 * update the pipelimit (ssthresh) if it has been updated
336 		 * already or if a pipesize was specified & the threshhold
337 		 * got below half the pipesize.  I.e., wait for bad news
338 		 * before we start updating, then update on both good
339 		 * and bad news.
340 		 */
341 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
342 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
343 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
344 			/*
345 			 * convert the limit from user data bytes to
346 			 * packets then to packet data bytes.
347 			 */
348 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
349 			if (i < 2)
350 				i = 2;
351 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
352 			if (rt->rt_rmx.rmx_ssthresh)
353 				rt->rt_rmx.rmx_ssthresh =
354 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
355 			else
356 				rt->rt_rmx.rmx_ssthresh = i;
357 		}
358 	}
359 #endif /* RTV_RTT */
360 	/* free the reassembly queue, if any */
361 	(void) tcp_freeq(tp);
362 
363 	if (tp->t_template)
364 		FREE(tp->t_template, M_MBUF);
365 	free(tp, M_PCB);
366 	inp->inp_ppcb = 0;
367 	soisdisconnected(so);
368 	in_pcbdetach(inp);
369 	tcpstat.tcps_closed++;
370 	return ((struct tcpcb *)0);
371 }
372 
373 int
374 tcp_freeq(tp)
375 	struct tcpcb *tp;
376 {
377 	register struct ipqent *qe;
378 	int rv = 0;
379 
380 	while ((qe = tp->segq.lh_first) != NULL) {
381 		LIST_REMOVE(qe, ipqe_q);
382 		m_freem(qe->ipqe_m);
383 		FREE(qe, M_IPQ);
384 		rv = 1;
385 	}
386 	return (rv);
387 }
388 
389 /*
390  * Protocol drain routine.  Called when memory is in short supply.
391  */
392 void
393 tcp_drain()
394 {
395 	register struct inpcb *inp;
396 	register struct tcpcb *tp;
397 
398 	/*
399 	 * Free the sequence queue of all TCP connections.
400 	 */
401 	inp = tcbtable.inpt_queue.cqh_first;
402 	if (inp)						/* XXX */
403 	for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
404 	    inp = inp->inp_queue.cqe_next) {
405 		if ((tp = intotcpcb(inp)) != NULL) {
406 			if (tcp_freeq(tp))
407 				tcpstat.tcps_connsdrained++;
408 		}
409 	}
410 }
411 
412 /*
413  * Notify a tcp user of an asynchronous error;
414  * store error as soft error, but wake up user
415  * (for now, won't do anything until can select for soft error).
416  */
417 void
418 tcp_notify(inp, error)
419 	struct inpcb *inp;
420 	int error;
421 {
422 	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
423 	register struct socket *so = inp->inp_socket;
424 
425 	/*
426 	 * Ignore some errors if we are hooked up.
427 	 * If connection hasn't completed, has retransmitted several times,
428 	 * and receives a second error, give up now.  This is better
429 	 * than waiting a long time to establish a connection that
430 	 * can never complete.
431 	 */
432 	if (tp->t_state == TCPS_ESTABLISHED &&
433 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
434 	      error == EHOSTDOWN)) {
435 		return;
436 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
437 	    tp->t_rxtshift > 3 && tp->t_softerror)
438 		so->so_error = error;
439 	else
440 		tp->t_softerror = error;
441 	wakeup((caddr_t) &so->so_timeo);
442 	sorwakeup(so);
443 	sowwakeup(so);
444 }
445 
446 void *
447 tcp_ctlinput(cmd, sa, v)
448 	int cmd;
449 	struct sockaddr *sa;
450 	register void *v;
451 {
452 	register struct ip *ip = v;
453 	register struct tcphdr *th;
454 	extern int inetctlerrmap[];
455 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
456 	int errno;
457 	int nmatch;
458 
459 	if ((unsigned)cmd >= PRC_NCMDS)
460 		return NULL;
461 	errno = inetctlerrmap[cmd];
462 	if (cmd == PRC_QUENCH)
463 		notify = tcp_quench;
464 	else if (PRC_IS_REDIRECT(cmd))
465 		notify = in_rtchange, ip = 0;
466 	else if (cmd == PRC_MSGSIZE && ip_mtudisc)
467 		notify = tcp_mtudisc, ip = 0;
468 	else if (cmd == PRC_HOSTDEAD)
469 		ip = 0;
470 	else if (errno == 0)
471 		return NULL;
472 	if (ip) {
473 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
474 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
475 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
476 		if (nmatch == 0 && syn_cache_count &&
477 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
478 		    inetctlerrmap[cmd] == ENETUNREACH ||
479 		    inetctlerrmap[cmd] == EHOSTDOWN))
480 			syn_cache_unreach(ip, th);
481 	} else
482 		(void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
483 		    notify);
484 	return NULL;
485 }
486 
487 /*
488  * When a source quench is received, close congestion window
489  * to one segment.  We will gradually open it again as we proceed.
490  */
491 void
492 tcp_quench(inp, errno)
493 	struct inpcb *inp;
494 	int errno;
495 {
496 	struct tcpcb *tp = intotcpcb(inp);
497 
498 	if (tp)
499 		tp->snd_cwnd = tp->t_segsz;
500 }
501 
502 /*
503  * On receipt of path MTU corrections, flush old route and replace it
504  * with the new one.  Retransmit all unacknowledged packets, to ensure
505  * that all packets will be received.
506  */
507 void
508 tcp_mtudisc(inp, errno)
509 	struct inpcb *inp;
510 	int errno;
511 {
512 	struct tcpcb *tp = intotcpcb(inp);
513 	struct rtentry *rt = in_pcbrtentry(inp);
514 
515 	if (tp != 0) {
516 		if (rt != 0) {
517 			/*
518 			 * If this was not a host route, remove and realloc.
519 			 */
520 			if ((rt->rt_flags & RTF_HOST) == 0) {
521 				in_rtchange(inp, errno);
522 				if ((rt = in_pcbrtentry(inp)) == 0)
523 					return;
524 			}
525 
526 			/*
527 			 * Slow start out of the error condition.  We
528 			 * use the MTU because we know it's smaller
529 			 * than the previously transmitted segment.
530 			 */
531 			if (rt->rt_rmx.rmx_mtu != 0)
532 				tp->snd_cwnd =
533 				    TCP_INITIAL_WINDOW(rt->rt_rmx.rmx_mtu);
534 		}
535 
536 		/*
537 		 * Resend unacknowledged packets.
538 		 */
539 		tp->snd_nxt = tp->snd_una;
540 		tcp_output(tp);
541 	}
542 }
543 
544 
545 /*
546  * Compute the MSS to advertise to the peer.  Called only during
547  * the 3-way handshake.  If we are the server (peer initiated
548  * connection), we are called with the TCPCB for the listen
549  * socket.  If we are the client (we initiated connection), we
550  * are called witht he TCPCB for the actual connection.
551  */
552 int
553 tcp_mss_to_advertise(tp)
554 	const struct tcpcb *tp;
555 {
556 	extern u_long in_maxmtu;
557 	struct inpcb *inp;
558 	struct socket *so;
559 	int mss;
560 
561 	inp = tp->t_inpcb;
562 	so = inp->inp_socket;
563 
564 	/*
565 	 * In order to avoid defeating path MTU discovery on the peer,
566 	 * we advertise the max MTU of all attached networks as our MSS,
567 	 * per RFC 1191, section 3.1.
568 	 *
569 	 * XXX Should we allow room for the timestamp option if
570 	 * XXX rfc1323 is enabled?
571 	 */
572 	mss = in_maxmtu - sizeof(struct tcpiphdr);
573 
574 	return (mss);
575 }
576 
577 /*
578  * Set connection variables based on the peer's advertised MSS.
579  * We are passed the TCPCB for the actual connection.  If we
580  * are the server, we are called by the compressed state engine
581  * when the 3-way handshake is complete.  If we are the client,
582  * we are called when we recieve the SYN,ACK from the server.
583  *
584  * NOTE: Our advertised MSS value must be initialized in the TCPCB
585  * before this routine is called!
586  */
587 void
588 tcp_mss_from_peer(tp, offer)
589 	struct tcpcb *tp;
590 	int offer;
591 {
592 	struct inpcb *inp = tp->t_inpcb;
593 	struct socket *so = inp->inp_socket;
594 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
595 	struct rtentry *rt = in_pcbrtentry(inp);
596 #endif
597 	u_long bufsize;
598 	int mss;
599 
600 	/*
601 	 * Assume our MSS is the MSS of the peer, unless they sent us
602 	 * an offer.  Do not accept offers less than 32 bytes.
603 	 */
604 	mss = tp->t_ourmss;
605 	if (offer)
606 		mss = offer;
607 	mss = max(mss, 32);		/* sanity */
608 
609 	/*
610 	 * If there's a pipesize, change the socket buffer to that size.
611 	 * Make the socket buffer an integral number of MSS units.  If
612 	 * the MSS is larger than the socket buffer, artificially decrease
613 	 * the MSS.
614 	 */
615 #ifdef RTV_SPIPE
616 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
617 		bufsize = rt->rt_rmx.rmx_sendpipe;
618 	else
619 #endif
620 		bufsize = so->so_snd.sb_hiwat;
621 	if (bufsize < mss)
622 		mss = bufsize;
623 	else {
624 		bufsize = roundup(bufsize, mss);
625 		if (bufsize > sb_max)
626 			bufsize = sb_max;
627 		(void) sbreserve(&so->so_snd, bufsize);
628 	}
629 	tp->t_peermss = mss;
630 	tp->t_segsz = mss;
631 
632 	/* Initialize the initial congestion window. */
633 	tp->snd_cwnd = TCP_INITIAL_WINDOW(mss);
634 
635 #ifdef RTV_SSTHRESH
636 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
637 		/*
638 		 * There's some sort of gateway or interface buffer
639 		 * limit on the path.  Use this to set the slow
640 		 * start threshold, but set the threshold to no less
641 		 * than 2 * MSS.
642 		 */
643 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
644 	}
645 #endif
646 }
647 
648 /*
649  * Processing necessary when a TCP connection is established.
650  */
651 void
652 tcp_established(tp)
653 	struct tcpcb *tp;
654 {
655 	struct inpcb *inp = tp->t_inpcb;
656 	struct socket *so = inp->inp_socket;
657 #ifdef RTV_RPIPE
658 	struct rtentry *rt = in_pcbrtentry(inp);
659 #endif
660 	u_long bufsize;
661 
662 	tp->t_state = TCPS_ESTABLISHED;
663 	tp->t_timer[TCPT_KEEP] = tcp_keepidle;
664 
665 #ifdef RTV_RPIPE
666 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
667 		bufsize = rt->rt_rmx.rmx_recvpipe;
668 	else
669 #endif
670 		bufsize = so->so_rcv.sb_hiwat;
671 	if (bufsize > tp->t_ourmss) {
672 		bufsize = roundup(bufsize, tp->t_ourmss);
673 		if (bufsize > sb_max)
674 			bufsize = sb_max;
675 		(void) sbreserve(&so->so_rcv, bufsize);
676 	}
677 }
678 
679 /*
680  * Check if there's an initial rtt or rttvar.  Convert from the
681  * route-table units to scaled multiples of the slow timeout timer.
682  * Called only during the 3-way handshake.
683  */
684 void
685 tcp_rmx_rtt(tp)
686 	struct tcpcb *tp;
687 {
688 #ifdef RTV_RTT
689 	struct rtentry *rt;
690 	int rtt;
691 
692 	if ((rt = in_pcbrtentry(tp->t_inpcb)) == NULL)
693 		return;
694 
695 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
696 		/*
697 		 * XXX The lock bit for MTU indicates that the value
698 		 * is also a minimum value; this is subject to time.
699 		 */
700 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
701 			tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
702 		tp->t_srtt = rtt /
703 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
704 		if (rt->rt_rmx.rmx_rttvar) {
705 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
706 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
707 				(TCP_RTTVAR_SHIFT + 2));
708 		} else {
709 			/* Default variation is +- 1 rtt */
710 			tp->t_rttvar =
711 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
712 		}
713 		TCPT_RANGESET(tp->t_rxtcur,
714 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
715 		    tp->t_rttmin, TCPTV_REXMTMAX);
716 	}
717 #endif
718 }
719 
720 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
721 
722 /*
723  * Get a new sequence value given a tcp control block
724  */
725 tcp_seq
726 tcp_new_iss(tp, len, addin)
727 	void            *tp;
728 	u_long           len;
729 	tcp_seq		 addin;
730 {
731 	tcp_seq          tcp_iss;
732 
733 	/*
734 	 * add randomness about this connection, but do not estimate
735 	 * entropy from the timing, since the physical device driver would
736 	 * have done that for us.
737 	 */
738 #if NRND > 0
739 	if (tp != NULL)
740 		rnd_add_data(NULL, tp, len, 0);
741 #endif
742 
743 	/*
744 	 * randomize.
745 	 */
746 #if NRND > 0
747 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
748 #else
749 	tcp_iss = random();
750 #endif
751 
752 	/*
753 	 * If we were asked to add some amount to a known value,
754 	 * we will take a random value obtained above, mask off the upper
755 	 * bits, and add in the known value.  We also add in a constant to
756 	 * ensure that we are at least a certain distance from the original
757 	 * value.
758 	 *
759 	 * This is used when an old connection is in timed wait
760 	 * and we have a new one coming in, for instance.
761 	 */
762 	if (addin != 0) {
763 #ifdef TCPISS_DEBUG
764 		printf("Random %08x, ", tcp_iss);
765 #endif
766 		tcp_iss &= TCP_ISS_RANDOM_MASK;
767 		tcp_iss = tcp_iss + addin + TCP_ISSINCR;
768 		tcp_iss_seq += TCP_ISSINCR;
769 		tcp_iss += tcp_iss_seq;
770 #ifdef TCPISS_DEBUG
771 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
772 #endif
773 	} else {
774 		tcp_iss &= TCP_ISS_RANDOM_MASK;
775 		tcp_iss_seq += TCP_ISSINCR;
776 		tcp_iss += tcp_iss_seq;
777 #ifdef TCPISS_DEBUG
778 		printf("ISS %08x\n", tcp_iss);
779 #endif
780 	}
781 
782 #ifdef TCP_COMPAT_42
783 	/*
784 	 * limit it to the positive range for really old TCP implementations
785 	 */
786 	if ((int)tcp_iss < 0)
787 		tcp_iss &= 0x7fffffff;		/* XXX */
788 #endif
789 
790 	return tcp_iss;
791 }
792