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