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