1 /* 2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved. 36 * 37 * License terms: all terms for the DragonFly license above plus the following: 38 * 39 * 4. All advertising materials mentioning features or use of this software 40 * must display the following acknowledgement: 41 * 42 * This product includes software developed by Jeffrey M. Hsu 43 * for the DragonFly Project. 44 * 45 * This requirement may be waived with permission from Jeffrey Hsu. 46 * This requirement will sunset and may be removed on July 8 2005, 47 * after which the standard DragonFly license (as shown above) will 48 * apply. 49 */ 50 51 /* 52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 53 * The Regents of the University of California. All rights reserved. 54 * 55 * Redistribution and use in source and binary forms, with or without 56 * modification, are permitted provided that the following conditions 57 * are met: 58 * 1. Redistributions of source code must retain the above copyright 59 * notice, this list of conditions and the following disclaimer. 60 * 2. Redistributions in binary form must reproduce the above copyright 61 * notice, this list of conditions and the following disclaimer in the 62 * documentation and/or other materials provided with the distribution. 63 * 3. All advertising materials mentioning features or use of this software 64 * must display the following acknowledgement: 65 * This product includes software developed by the University of 66 * California, Berkeley and its contributors. 67 * 4. Neither the name of the University nor the names of its contributors 68 * may be used to endorse or promote products derived from this software 69 * without specific prior written permission. 70 * 71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 81 * SUCH DAMAGE. 82 * 83 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 84 * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.31 2003/01/24 05:11:34 sam Exp $ 85 * $DragonFly: src/sys/netinet/tcp_subr.c,v 1.36 2004/07/08 22:07:35 hsu Exp $ 86 */ 87 88 #include "opt_compat.h" 89 #include "opt_inet6.h" 90 #include "opt_ipsec.h" 91 #include "opt_tcpdebug.h" 92 93 #include <sys/param.h> 94 #include <sys/systm.h> 95 #include <sys/callout.h> 96 #include <sys/kernel.h> 97 #include <sys/sysctl.h> 98 #include <sys/malloc.h> 99 #include <sys/mpipe.h> 100 #include <sys/mbuf.h> 101 #ifdef INET6 102 #include <sys/domain.h> 103 #endif 104 #include <sys/proc.h> 105 #include <sys/socket.h> 106 #include <sys/socketvar.h> 107 #include <sys/protosw.h> 108 #include <sys/random.h> 109 #include <sys/in_cksum.h> 110 111 #include <vm/vm_zone.h> 112 113 #include <net/route.h> 114 #include <net/if.h> 115 #include <net/netisr.h> 116 117 #define _IP_VHL 118 #include <netinet/in.h> 119 #include <netinet/in_systm.h> 120 #include <netinet/ip.h> 121 #include <netinet/ip6.h> 122 #include <netinet/in_pcb.h> 123 #include <netinet6/in6_pcb.h> 124 #include <netinet/in_var.h> 125 #include <netinet/ip_var.h> 126 #include <netinet6/ip6_var.h> 127 #include <netinet/tcp.h> 128 #include <netinet/tcp_fsm.h> 129 #include <netinet/tcp_seq.h> 130 #include <netinet/tcp_timer.h> 131 #include <netinet/tcp_var.h> 132 #include <netinet6/tcp6_var.h> 133 #include <netinet/tcpip.h> 134 #ifdef TCPDEBUG 135 #include <netinet/tcp_debug.h> 136 #endif 137 #include <netinet6/ip6protosw.h> 138 139 #ifdef IPSEC 140 #include <netinet6/ipsec.h> 141 #ifdef INET6 142 #include <netinet6/ipsec6.h> 143 #endif 144 #endif 145 146 #ifdef FAST_IPSEC 147 #include <netipsec/ipsec.h> 148 #ifdef INET6 149 #include <netipsec/ipsec6.h> 150 #endif 151 #define IPSEC 152 #endif 153 154 #include <sys/md5.h> 155 156 #include <sys/msgport2.h> 157 158 #include <machine/smp.h> 159 160 int tcp_mssdflt = TCP_MSS; 161 SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 162 &tcp_mssdflt, 0, "Default TCP Maximum Segment Size"); 163 164 #ifdef INET6 165 int tcp_v6mssdflt = TCP6_MSS; 166 SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, CTLFLAG_RW, 167 &tcp_v6mssdflt, 0, "Default TCP Maximum Segment Size for IPv6"); 168 #endif 169 170 #if 0 171 static int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 172 SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 173 &tcp_rttdflt, 0, "Default maximum TCP Round Trip Time"); 174 #endif 175 176 int tcp_do_rfc1323 = 1; 177 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 178 &tcp_do_rfc1323, 0, "Enable rfc1323 (high performance TCP) extensions"); 179 180 int tcp_do_rfc1644 = 0; 181 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW, 182 &tcp_do_rfc1644, 0, "Enable rfc1644 (TTCP) extensions"); 183 184 static int tcp_tcbhashsize = 0; 185 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD, 186 &tcp_tcbhashsize, 0, "Size of TCP control block hashtable"); 187 188 static int do_tcpdrain = 1; 189 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 190 "Enable tcp_drain routine for extra help when low on mbufs"); 191 192 /* XXX JH */ 193 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 194 &tcbinfo[0].ipi_count, 0, "Number of active PCBs"); 195 196 static int icmp_may_rst = 1; 197 SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0, 198 "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 199 200 static int tcp_isn_reseed_interval = 0; 201 SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW, 202 &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret"); 203 204 /* 205 * TCP bandwidth limiting sysctls. Note that the default lower bound of 206 * 1024 exists only for debugging. A good production default would be 207 * something like 6100. 208 */ 209 static int tcp_inflight_enable = 0; 210 SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_enable, CTLFLAG_RW, 211 &tcp_inflight_enable, 0, "Enable automatic TCP inflight data limiting"); 212 213 static int tcp_inflight_debug = 0; 214 SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_debug, CTLFLAG_RW, 215 &tcp_inflight_debug, 0, "Debug TCP inflight calculations"); 216 217 static int tcp_inflight_min = 6144; 218 SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_min, CTLFLAG_RW, 219 &tcp_inflight_min, 0, "Lower bound for TCP inflight window"); 220 221 static int tcp_inflight_max = TCP_MAXWIN << TCP_MAX_WINSHIFT; 222 SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_max, CTLFLAG_RW, 223 &tcp_inflight_max, 0, "Upper bound for TCP inflight window"); 224 225 static int tcp_inflight_stab = 20; 226 SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_stab, CTLFLAG_RW, 227 &tcp_inflight_stab, 0, "Slop in maximal packets / 10 (20 = 2 packets)"); 228 229 static MALLOC_DEFINE(M_TCPTEMP, "tcptemp", "TCP Templates for Keepalives"); 230 static struct malloc_pipe tcptemp_mpipe; 231 232 static void tcp_cleartaocache (void); 233 static void tcp_notify (struct inpcb *, int); 234 235 struct tcp_stats tcpstats_ary[MAXCPU]; 236 #ifdef SMP 237 static int 238 sysctl_tcpstats(SYSCTL_HANDLER_ARGS) 239 { 240 int cpu, error = 0; 241 242 for (cpu = 0; cpu < ncpus; ++cpu) { 243 if ((error = SYSCTL_OUT(req, (void *)&tcpstats_ary[cpu], 244 sizeof(struct tcp_stats)))) 245 break; 246 if ((error = SYSCTL_IN(req, (void *)&tcpstats_ary[cpu], 247 sizeof(struct tcp_stats)))) 248 break; 249 } 250 251 return (error); 252 } 253 SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW), 254 0, 0, sysctl_tcpstats, "S,tcp_stats", "TCP statistics"); 255 #else 256 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 257 &tcpstat, tcp_stats, "TCP statistics"); 258 #endif 259 260 /* 261 * Target size of TCP PCB hash tables. Must be a power of two. 262 * 263 * Note that this can be overridden by the kernel environment 264 * variable net.inet.tcp.tcbhashsize 265 */ 266 #ifndef TCBHASHSIZE 267 #define TCBHASHSIZE 512 268 #endif 269 270 /* 271 * This is the actual shape of what we allocate using the zone 272 * allocator. Doing it this way allows us to protect both structures 273 * using the same generation count, and also eliminates the overhead 274 * of allocating tcpcbs separately. By hiding the structure here, 275 * we avoid changing most of the rest of the code (although it needs 276 * to be changed, eventually, for greater efficiency). 277 */ 278 #define ALIGNMENT 32 279 #define ALIGNM1 (ALIGNMENT - 1) 280 struct inp_tp { 281 union { 282 struct inpcb inp; 283 char align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1]; 284 } inp_tp_u; 285 struct tcpcb tcb; 286 struct callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl; 287 struct callout inp_tp_delack; 288 }; 289 #undef ALIGNMENT 290 #undef ALIGNM1 291 292 /* 293 * Tcp initialization 294 */ 295 void 296 tcp_init() 297 { 298 struct inpcbporthead *porthashbase; 299 u_long porthashmask; 300 struct vm_zone *ipi_zone; 301 int hashsize = TCBHASHSIZE; 302 int cpu; 303 304 /* 305 * note: tcptemp is used for keepalives, and it is ok for an 306 * allocation to fail so do not specify MPF_INT. 307 */ 308 mpipe_init(&tcptemp_mpipe, M_TCPTEMP, sizeof(struct tcptemp), 309 25, -1, 0, NULL); 310 311 tcp_ccgen = 1; 312 tcp_cleartaocache(); 313 314 tcp_delacktime = TCPTV_DELACK; 315 tcp_keepinit = TCPTV_KEEP_INIT; 316 tcp_keepidle = TCPTV_KEEP_IDLE; 317 tcp_keepintvl = TCPTV_KEEPINTVL; 318 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 319 tcp_msl = TCPTV_MSL; 320 tcp_rexmit_min = TCPTV_MIN; 321 tcp_rexmit_slop = TCPTV_CPU_VAR; 322 323 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize); 324 if (!powerof2(hashsize)) { 325 printf("WARNING: TCB hash size not a power of 2\n"); 326 hashsize = 512; /* safe default */ 327 } 328 tcp_tcbhashsize = hashsize; 329 porthashbase = hashinit(hashsize, M_PCB, &porthashmask); 330 ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets, 331 ZONE_INTERRUPT, 0); 332 333 for (cpu = 0; cpu < ncpus2; cpu++) { 334 in_pcbinfo_init(&tcbinfo[cpu]); 335 tcbinfo[cpu].hashbase = hashinit(hashsize, M_PCB, 336 &tcbinfo[cpu].hashmask); 337 tcbinfo[cpu].porthashbase = porthashbase; 338 tcbinfo[cpu].porthashmask = porthashmask; 339 tcbinfo[cpu].wildcardhashbase = hashinit(hashsize, M_PCB, 340 &tcbinfo[cpu].wildcardhashmask); 341 tcbinfo[cpu].ipi_zone = ipi_zone; 342 } 343 344 tcp_reass_maxseg = nmbclusters / 16; 345 TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", &tcp_reass_maxseg); 346 347 #ifdef INET6 348 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) 349 #else 350 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr)) 351 #endif 352 if (max_protohdr < TCP_MINPROTOHDR) 353 max_protohdr = TCP_MINPROTOHDR; 354 if (max_linkhdr + TCP_MINPROTOHDR > MHLEN) 355 panic("tcp_init"); 356 #undef TCP_MINPROTOHDR 357 358 /* 359 * Initialize TCP statistics. 360 * 361 * It is layed out as an array which is has one element for UP, 362 * and SMP_MAXCPU elements for SMP. This allows us to retain 363 * the access mechanism from userland for both UP and SMP. 364 */ 365 #ifdef SMP 366 for (cpu = 0; cpu < ncpus; ++cpu) { 367 bzero(&tcpstats_ary[cpu], sizeof(struct tcp_stats)); 368 } 369 #else 370 bzero(&tcpstat, sizeof(struct tcp_stats)); 371 #endif 372 373 syncache_init(); 374 tcp_thread_init(); 375 } 376 377 /* 378 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. 379 * tcp_template used to store this data in mbufs, but we now recopy it out 380 * of the tcpcb each time to conserve mbufs. 381 */ 382 void 383 tcp_fillheaders(struct tcpcb *tp, void *ip_ptr, void *tcp_ptr) 384 { 385 struct inpcb *inp = tp->t_inpcb; 386 struct tcphdr *tcp_hdr = (struct tcphdr *)tcp_ptr; 387 388 #ifdef INET6 389 if (inp->inp_vflag & INP_IPV6) { 390 struct ip6_hdr *ip6; 391 392 ip6 = (struct ip6_hdr *)ip_ptr; 393 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 394 (inp->in6p_flowinfo & IPV6_FLOWINFO_MASK); 395 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 396 (IPV6_VERSION & IPV6_VERSION_MASK); 397 ip6->ip6_nxt = IPPROTO_TCP; 398 ip6->ip6_plen = sizeof(struct tcphdr); 399 ip6->ip6_src = inp->in6p_laddr; 400 ip6->ip6_dst = inp->in6p_faddr; 401 tcp_hdr->th_sum = 0; 402 } else 403 #endif 404 { 405 struct ip *ip = (struct ip *) ip_ptr; 406 407 ip->ip_vhl = IP_VHL_BORING; 408 ip->ip_tos = 0; 409 ip->ip_len = 0; 410 ip->ip_id = 0; 411 ip->ip_off = 0; 412 ip->ip_ttl = 0; 413 ip->ip_sum = 0; 414 ip->ip_p = IPPROTO_TCP; 415 ip->ip_src = inp->inp_laddr; 416 ip->ip_dst = inp->inp_faddr; 417 tcp_hdr->th_sum = in_pseudo(ip->ip_src.s_addr, 418 ip->ip_dst.s_addr, 419 htons(sizeof(struct tcphdr) + IPPROTO_TCP)); 420 } 421 422 tcp_hdr->th_sport = inp->inp_lport; 423 tcp_hdr->th_dport = inp->inp_fport; 424 tcp_hdr->th_seq = 0; 425 tcp_hdr->th_ack = 0; 426 tcp_hdr->th_x2 = 0; 427 tcp_hdr->th_off = 5; 428 tcp_hdr->th_flags = 0; 429 tcp_hdr->th_win = 0; 430 tcp_hdr->th_urp = 0; 431 } 432 433 /* 434 * Create template to be used to send tcp packets on a connection. 435 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only 436 * use for this function is in keepalives, which use tcp_respond. 437 */ 438 struct tcptemp * 439 tcp_maketemplate(struct tcpcb *tp) 440 { 441 struct tcptemp *tmp; 442 443 if ((tmp = mpipe_alloc_nowait(&tcptemp_mpipe)) == NULL) 444 return (NULL); 445 tcp_fillheaders(tp, (void *)&tmp->tt_ipgen, (void *)&tmp->tt_t); 446 return (tmp); 447 } 448 449 void 450 tcp_freetemplate(struct tcptemp *tmp) 451 { 452 mpipe_free(&tcptemp_mpipe, tmp); 453 } 454 455 /* 456 * Send a single message to the TCP at address specified by 457 * the given TCP/IP header. If m == NULL, then we make a copy 458 * of the tcpiphdr at ti and send directly to the addressed host. 459 * This is used to force keep alive messages out using the TCP 460 * template for a connection. If flags are given then we send 461 * a message back to the TCP which originated the * segment ti, 462 * and discard the mbuf containing it and any other attached mbufs. 463 * 464 * In any case the ack and sequence number of the transmitted 465 * segment are as specified by the parameters. 466 * 467 * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 468 */ 469 void 470 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, 471 tcp_seq ack, tcp_seq seq, int flags) 472 { 473 int tlen; 474 int win = 0; 475 struct route *ro = NULL; 476 struct route sro; 477 struct ip *ip = ipgen; 478 struct tcphdr *nth; 479 int ipflags = 0; 480 struct route_in6 *ro6 = NULL; 481 struct route_in6 sro6; 482 struct ip6_hdr *ip6 = ipgen; 483 #ifdef INET6 484 boolean_t isipv6 = (IP_VHL_V(ip->ip_vhl) == 6); 485 #else 486 const boolean_t isipv6 = FALSE; 487 #endif 488 489 if (tp != NULL) { 490 if (!(flags & TH_RST)) { 491 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 492 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 493 win = (long)TCP_MAXWIN << tp->rcv_scale; 494 } 495 if (isipv6) 496 ro6 = &tp->t_inpcb->in6p_route; 497 else 498 ro = &tp->t_inpcb->inp_route; 499 } else { 500 if (isipv6) { 501 ro6 = &sro6; 502 bzero(ro6, sizeof *ro6); 503 } else { 504 ro = &sro; 505 bzero(ro, sizeof *ro); 506 } 507 } 508 if (m == NULL) { 509 m = m_gethdr(MB_DONTWAIT, MT_HEADER); 510 if (m == NULL) 511 return; 512 tlen = 0; 513 m->m_data += max_linkhdr; 514 if (isipv6) { 515 bcopy(ip6, mtod(m, caddr_t), sizeof(struct ip6_hdr)); 516 ip6 = mtod(m, struct ip6_hdr *); 517 nth = (struct tcphdr *)(ip6 + 1); 518 } else { 519 bcopy(ip, mtod(m, caddr_t), sizeof(struct ip)); 520 ip = mtod(m, struct ip *); 521 nth = (struct tcphdr *)(ip + 1); 522 } 523 bcopy(th, nth, sizeof(struct tcphdr)); 524 flags = TH_ACK; 525 } else { 526 m_freem(m->m_next); 527 m->m_next = NULL; 528 m->m_data = (caddr_t)ipgen; 529 /* m_len is set later */ 530 tlen = 0; 531 #define xchg(a, b, type) { type t; t = a; a = b; b = t; } 532 if (isipv6) { 533 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 534 nth = (struct tcphdr *)(ip6 + 1); 535 } else { 536 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long); 537 nth = (struct tcphdr *)(ip + 1); 538 } 539 if (th != nth) { 540 /* 541 * this is usually a case when an extension header 542 * exists between the IPv6 header and the 543 * TCP header. 544 */ 545 nth->th_sport = th->th_sport; 546 nth->th_dport = th->th_dport; 547 } 548 xchg(nth->th_dport, nth->th_sport, n_short); 549 #undef xchg 550 } 551 if (isipv6) { 552 ip6->ip6_flow = 0; 553 ip6->ip6_vfc = IPV6_VERSION; 554 ip6->ip6_nxt = IPPROTO_TCP; 555 ip6->ip6_plen = htons((u_short)(sizeof(struct tcphdr) + tlen)); 556 tlen += sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 557 } else { 558 tlen += sizeof(struct tcpiphdr); 559 ip->ip_len = tlen; 560 ip->ip_ttl = ip_defttl; 561 } 562 m->m_len = tlen; 563 m->m_pkthdr.len = tlen; 564 m->m_pkthdr.rcvif = (struct ifnet *) NULL; 565 nth->th_seq = htonl(seq); 566 nth->th_ack = htonl(ack); 567 nth->th_x2 = 0; 568 nth->th_off = sizeof(struct tcphdr) >> 2; 569 nth->th_flags = flags; 570 if (tp != NULL) 571 nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 572 else 573 nth->th_win = htons((u_short)win); 574 nth->th_urp = 0; 575 if (isipv6) { 576 nth->th_sum = 0; 577 nth->th_sum = in6_cksum(m, IPPROTO_TCP, 578 sizeof(struct ip6_hdr), 579 tlen - sizeof(struct ip6_hdr)); 580 ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL, 581 (ro6 && ro6->ro_rt) ? 582 ro6->ro_rt->rt_ifp : NULL); 583 } else { 584 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 585 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 586 m->m_pkthdr.csum_flags = CSUM_TCP; 587 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 588 } 589 #ifdef TCPDEBUG 590 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 591 tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); 592 #endif 593 if (isipv6) { 594 (void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL, 595 tp ? tp->t_inpcb : NULL); 596 if ((ro6 == &sro6) && (ro6->ro_rt != NULL)) { 597 RTFREE(ro6->ro_rt); 598 ro6->ro_rt = NULL; 599 } 600 } else { 601 (void)ip_output(m, NULL, ro, ipflags, NULL, 602 tp ? tp->t_inpcb : NULL); 603 if ((ro == &sro) && (ro->ro_rt != NULL)) { 604 RTFREE(ro->ro_rt); 605 ro->ro_rt = NULL; 606 } 607 } 608 } 609 610 /* 611 * Create a new TCP control block, making an 612 * empty reassembly queue and hooking it to the argument 613 * protocol control block. The `inp' parameter must have 614 * come from the zone allocator set up in tcp_init(). 615 */ 616 struct tcpcb * 617 tcp_newtcpcb(struct inpcb *inp) 618 { 619 struct inp_tp *it; 620 struct tcpcb *tp; 621 #ifdef INET6 622 boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) != 0); 623 #else 624 const boolean_t isipv6 = FALSE; 625 #endif 626 627 it = (struct inp_tp *)inp; 628 tp = &it->tcb; 629 bzero(tp, sizeof(struct tcpcb)); 630 LIST_INIT(&tp->t_segq); 631 tp->t_maxseg = tp->t_maxopd = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 632 633 /* Set up our timeouts. */ 634 callout_init(tp->tt_rexmt = &it->inp_tp_rexmt); 635 callout_init(tp->tt_persist = &it->inp_tp_persist); 636 callout_init(tp->tt_keep = &it->inp_tp_keep); 637 callout_init(tp->tt_2msl = &it->inp_tp_2msl); 638 callout_init(tp->tt_delack = &it->inp_tp_delack); 639 640 if (tcp_do_rfc1323) 641 tp->t_flags = (TF_REQ_SCALE | TF_REQ_TSTMP); 642 if (tcp_do_rfc1644) 643 tp->t_flags |= TF_REQ_CC; 644 tp->t_inpcb = inp; /* XXX */ 645 /* 646 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 647 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 648 * reasonable initial retransmit time. 649 */ 650 tp->t_srtt = TCPTV_SRTTBASE; 651 tp->t_rttvar = 652 ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 653 tp->t_rttmin = tcp_rexmit_min; 654 tp->t_rxtcur = TCPTV_RTOBASE; 655 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 656 tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 657 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 658 tp->t_rcvtime = ticks; 659 tp->t_bw_rtttime = ticks; 660 /* 661 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 662 * because the socket may be bound to an IPv6 wildcard address, 663 * which may match an IPv4-mapped IPv6 address. 664 */ 665 inp->inp_ip_ttl = ip_defttl; 666 inp->inp_ppcb = (caddr_t)tp; 667 return (tp); /* XXX */ 668 } 669 670 /* 671 * Drop a TCP connection, reporting the specified error. 672 * If connection is synchronized, then send a RST to peer. 673 */ 674 struct tcpcb * 675 tcp_drop(struct tcpcb *tp, int errno) 676 { 677 struct socket *so = tp->t_inpcb->inp_socket; 678 679 if (TCPS_HAVERCVDSYN(tp->t_state)) { 680 tp->t_state = TCPS_CLOSED; 681 (void) tcp_output(tp); 682 tcpstat.tcps_drops++; 683 } else 684 tcpstat.tcps_conndrops++; 685 if (errno == ETIMEDOUT && tp->t_softerror) 686 errno = tp->t_softerror; 687 so->so_error = errno; 688 return (tcp_close(tp)); 689 } 690 691 #ifdef SMP 692 struct netmsg_remwildcard { 693 struct lwkt_msg nm_lmsg; 694 struct inpcb *nm_inp; 695 struct inpcbinfo *nm_pcbinfo; 696 }; 697 698 static int 699 in_pcbremwildcardhash_handler(struct lwkt_msg *msg0) 700 { 701 struct netmsg_remwildcard *msg = (struct netmsg_remwildcard *)msg0; 702 703 in_pcbremwildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo); 704 lwkt_replymsg(&msg->nm_lmsg, 0); 705 return (EASYNC); 706 } 707 #endif 708 709 /* 710 * Close a TCP control block: 711 * discard all space held by the tcp 712 * discard internet protocol block 713 * wake up any sleepers 714 */ 715 struct tcpcb * 716 tcp_close(struct tcpcb *tp) 717 { 718 struct tseg_qent *q; 719 struct inpcb *inp = tp->t_inpcb; 720 struct socket *so = inp->inp_socket; 721 struct rtentry *rt; 722 boolean_t dosavessthresh; 723 #ifdef SMP 724 int cpu; 725 #endif 726 #ifdef INET6 727 boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) != 0); 728 #else 729 const boolean_t isipv6 = FALSE; 730 #endif 731 732 /* 733 * Make sure that all of our timers are stopped before we 734 * delete the PCB. 735 */ 736 callout_stop(tp->tt_rexmt); 737 callout_stop(tp->tt_persist); 738 callout_stop(tp->tt_keep); 739 callout_stop(tp->tt_2msl); 740 callout_stop(tp->tt_delack); 741 742 /* 743 * If we got enough samples through the srtt filter, 744 * save the rtt and rttvar in the routing entry. 745 * 'Enough' is arbitrarily defined as the 16 samples. 746 * 16 samples is enough for the srtt filter to converge 747 * to within 5% of the correct value; fewer samples and 748 * we could save a very bogus rtt. 749 * 750 * Don't update the default route's characteristics and don't 751 * update anything that the user "locked". 752 */ 753 if (tp->t_rttupdated >= 16) { 754 u_long i = 0; 755 756 if (isipv6) { 757 struct sockaddr_in6 *sin6; 758 759 if ((rt = inp->in6p_route.ro_rt) == NULL) 760 goto no_valid_rt; 761 sin6 = (struct sockaddr_in6 *)rt_key(rt); 762 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 763 goto no_valid_rt; 764 } else 765 if ((rt = inp->inp_route.ro_rt) == NULL || 766 ((struct sockaddr_in *)rt_key(rt))-> 767 sin_addr.s_addr == INADDR_ANY) 768 goto no_valid_rt; 769 770 if (!(rt->rt_rmx.rmx_locks & RTV_RTT)) { 771 i = tp->t_srtt * (RTM_RTTUNIT / (hz * TCP_RTT_SCALE)); 772 if (rt->rt_rmx.rmx_rtt && i) 773 /* 774 * filter this update to half the old & half 775 * the new values, converting scale. 776 * See route.h and tcp_var.h for a 777 * description of the scaling constants. 778 */ 779 rt->rt_rmx.rmx_rtt = 780 (rt->rt_rmx.rmx_rtt + i) / 2; 781 else 782 rt->rt_rmx.rmx_rtt = i; 783 tcpstat.tcps_cachedrtt++; 784 } 785 if (!(rt->rt_rmx.rmx_locks & RTV_RTTVAR)) { 786 i = tp->t_rttvar * 787 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE)); 788 if (rt->rt_rmx.rmx_rttvar && i) 789 rt->rt_rmx.rmx_rttvar = 790 (rt->rt_rmx.rmx_rttvar + i) / 2; 791 else 792 rt->rt_rmx.rmx_rttvar = i; 793 tcpstat.tcps_cachedrttvar++; 794 } 795 /* 796 * The old comment here said: 797 * update the pipelimit (ssthresh) if it has been updated 798 * already or if a pipesize was specified & the threshhold 799 * got below half the pipesize. I.e., wait for bad news 800 * before we start updating, then update on both good 801 * and bad news. 802 * 803 * But we want to save the ssthresh even if no pipesize is 804 * specified explicitly in the route, because such 805 * connections still have an implicit pipesize specified 806 * by the global tcp_sendspace. In the absence of a reliable 807 * way to calculate the pipesize, it will have to do. 808 */ 809 i = tp->snd_ssthresh; 810 if (rt->rt_rmx.rmx_sendpipe != 0) 811 dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe/2); 812 else 813 dosavessthresh = (i < so->so_snd.sb_hiwat/2); 814 if (dosavessthresh || 815 (!(rt->rt_rmx.rmx_locks & RTV_SSTHRESH) && (i != 0) && 816 (rt->rt_rmx.rmx_ssthresh != 0))) { 817 /* 818 * convert the limit from user data bytes to 819 * packets then to packet data bytes. 820 */ 821 i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 822 if (i < 2) 823 i = 2; 824 i *= tp->t_maxseg + 825 (isipv6 ? 826 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : 827 sizeof(struct tcpiphdr)); 828 if (rt->rt_rmx.rmx_ssthresh) 829 rt->rt_rmx.rmx_ssthresh = 830 (rt->rt_rmx.rmx_ssthresh + i) / 2; 831 else 832 rt->rt_rmx.rmx_ssthresh = i; 833 tcpstat.tcps_cachedssthresh++; 834 } 835 } 836 837 no_valid_rt: 838 /* free the reassembly queue, if any */ 839 while((q = LIST_FIRST(&tp->t_segq)) != NULL) { 840 LIST_REMOVE(q, tqe_q); 841 m_freem(q->tqe_m); 842 FREE(q, M_TSEGQ); 843 tcp_reass_qsize--; 844 } 845 inp->inp_ppcb = NULL; 846 soisdisconnected(so); 847 848 #ifdef SMP 849 if (inp->inp_flags & INP_WILDCARD_MP) { 850 for (cpu = 0; cpu < ncpus2; cpu ++) { 851 struct netmsg_remwildcard *msg; 852 853 msg = malloc(sizeof(struct netmsg_remwildcard), 854 M_LWKTMSG, M_INTWAIT); 855 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0, 856 lwkt_cmd_func(in_pcbremwildcardhash_handler), 857 lwkt_cmd_op_none); 858 msg->nm_inp = inp; 859 msg->nm_pcbinfo = &tcbinfo[cpu]; 860 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg); 861 } 862 } 863 #endif 864 865 #ifdef INET6 866 if (INP_CHECK_SOCKAF(so, AF_INET6)) 867 in6_pcbdetach(inp); 868 else 869 #endif 870 in_pcbdetach(inp); 871 tcpstat.tcps_closed++; 872 return (NULL); 873 } 874 875 static __inline void 876 tcp_drain_oncpu(struct inpcbhead *head) 877 { 878 struct inpcb *inpb; 879 struct tcpcb *tcpb; 880 struct tseg_qent *te; 881 882 LIST_FOREACH(inpb, head, inp_list) { 883 if (inpb->inp_flags & INP_PLACEMARKER) 884 continue; 885 if ((tcpb = intotcpcb(inpb))) { 886 while ((te = LIST_FIRST(&tcpb->t_segq)) != NULL) { 887 LIST_REMOVE(te, tqe_q); 888 m_freem(te->tqe_m); 889 FREE(te, M_TSEGQ); 890 tcp_reass_qsize--; 891 } 892 } 893 } 894 } 895 896 #ifdef SMP 897 struct netmsg_tcp_drain { 898 struct lwkt_msg nm_lmsg; 899 struct inpcbhead *nm_head; 900 }; 901 902 static int 903 tcp_drain_handler(lwkt_msg_t lmsg) 904 { 905 struct netmsg_tcp_drain *nm = (void *)lmsg; 906 907 tcp_drain_oncpu(nm->nm_head); 908 lwkt_replymsg(lmsg, 0); 909 return(EASYNC); 910 } 911 #endif 912 913 void 914 tcp_drain() 915 { 916 #ifdef SMP 917 int cpu; 918 #endif 919 920 if (!do_tcpdrain) 921 return; 922 923 /* 924 * Walk the tcpbs, if existing, and flush the reassembly queue, 925 * if there is one... 926 * XXX: The "Net/3" implementation doesn't imply that the TCP 927 * reassembly queue should be flushed, but in a situation 928 * where we're really low on mbufs, this is potentially 929 * useful. 930 */ 931 #ifdef SMP 932 for (cpu = 0; cpu < ncpus2; cpu++) { 933 struct netmsg_tcp_drain *msg; 934 935 if (cpu == mycpu->gd_cpuid) { 936 tcp_drain_oncpu(&tcbinfo[cpu].pcblisthead); 937 } else { 938 msg = malloc(sizeof(struct netmsg_tcp_drain), 939 M_LWKTMSG, M_NOWAIT); 940 if (msg == NULL) 941 continue; 942 lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0, 943 lwkt_cmd_func(tcp_drain_handler), 944 lwkt_cmd_op_none); 945 msg->nm_head = &tcbinfo[cpu].pcblisthead; 946 lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg); 947 } 948 } 949 #else 950 tcp_drain_oncpu(&tcbinfo[0].pcblisthead); 951 #endif 952 } 953 954 /* 955 * Notify a tcp user of an asynchronous error; 956 * store error as soft error, but wake up user 957 * (for now, won't do anything until can select for soft error). 958 * 959 * Do not wake up user since there currently is no mechanism for 960 * reporting soft errors (yet - a kqueue filter may be added). 961 */ 962 static void 963 tcp_notify(struct inpcb *inp, int error) 964 { 965 struct tcpcb *tp = intotcpcb(inp); 966 967 /* 968 * Ignore some errors if we are hooked up. 969 * If connection hasn't completed, has retransmitted several times, 970 * and receives a second error, give up now. This is better 971 * than waiting a long time to establish a connection that 972 * can never complete. 973 */ 974 if (tp->t_state == TCPS_ESTABLISHED && 975 (error == EHOSTUNREACH || error == ENETUNREACH || 976 error == EHOSTDOWN)) { 977 return; 978 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 979 tp->t_softerror) 980 tcp_drop(tp, error); 981 else 982 tp->t_softerror = error; 983 #if 0 984 wakeup((caddr_t) &so->so_timeo); 985 sorwakeup(so); 986 sowwakeup(so); 987 #endif 988 } 989 990 static int 991 tcp_pcblist(SYSCTL_HANDLER_ARGS) 992 { 993 int error, i, n; 994 struct inpcb *marker; 995 struct inpcb *inp; 996 inp_gen_t gencnt; 997 struct xinpgen xig; 998 globaldata_t gd; 999 int origcpu, ccpu; 1000 1001 error = 0; 1002 n = 0; 1003 1004 /* 1005 * The process of preparing the TCB list is too time-consuming and 1006 * resource-intensive to repeat twice on every request. 1007 */ 1008 if (req->oldptr == NULL) { 1009 for (ccpu = 0; ccpu < ncpus; ++ccpu) { 1010 gd = globaldata_find(ccpu); 1011 n += tcbinfo[gd->gd_cpuid].ipi_count; 1012 } 1013 req->oldidx = 2 * ncpus * (sizeof xig) + 1014 (n + n/8) * sizeof(struct xtcpcb); 1015 return (0); 1016 } 1017 1018 if (req->newptr != NULL) 1019 return (EPERM); 1020 1021 marker = malloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO); 1022 marker->inp_flags |= INP_PLACEMARKER; 1023 1024 /* 1025 * OK, now we're committed to doing something. Run the inpcb list 1026 * for each cpu in the system and construct the output. Use a 1027 * list placemarker to deal with list changes occuring during 1028 * copyout blockages (but otherwise depend on being on the correct 1029 * cpu to avoid races). 1030 */ 1031 origcpu = mycpu->gd_cpuid; 1032 for (ccpu = 1; ccpu <= ncpus && error == 0; ++ccpu) { 1033 globaldata_t rgd; 1034 caddr_t inp_ppcb; 1035 struct xtcpcb xt; 1036 int cpu_id; 1037 1038 cpu_id = (origcpu + ccpu) % ncpus; 1039 if ((smp_active_mask & (1 << cpu_id)) == 0) 1040 continue; 1041 rgd = globaldata_find(cpu_id); 1042 lwkt_setcpu_self(rgd); 1043 1044 /* indicate change of CPU */ 1045 cpu_mb1(); 1046 1047 gencnt = tcbinfo[cpu_id].ipi_gencnt; 1048 n = tcbinfo[cpu_id].ipi_count; 1049 1050 xig.xig_len = sizeof xig; 1051 xig.xig_count = n; 1052 xig.xig_gen = gencnt; 1053 xig.xig_sogen = so_gencnt; 1054 xig.xig_cpu = cpu_id; 1055 error = SYSCTL_OUT(req, &xig, sizeof xig); 1056 if (error != 0) 1057 break; 1058 1059 LIST_INSERT_HEAD(&tcbinfo[cpu_id].pcblisthead, marker, inp_list); 1060 i = 0; 1061 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) { 1062 /* 1063 * process a snapshot of pcbs, ignoring placemarkers 1064 * and using our own to allow SYSCTL_OUT to block. 1065 */ 1066 LIST_REMOVE(marker, inp_list); 1067 LIST_INSERT_AFTER(inp, marker, inp_list); 1068 1069 if (inp->inp_flags & INP_PLACEMARKER) 1070 continue; 1071 if (inp->inp_gencnt > gencnt) 1072 continue; 1073 if (prison_xinpcb(req->td, inp)) 1074 continue; 1075 1076 xt.xt_len = sizeof xt; 1077 bcopy(inp, &xt.xt_inp, sizeof *inp); 1078 inp_ppcb = inp->inp_ppcb; 1079 if (inp_ppcb != NULL) 1080 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 1081 else 1082 bzero(&xt.xt_tp, sizeof xt.xt_tp); 1083 if (inp->inp_socket) 1084 sotoxsocket(inp->inp_socket, &xt.xt_socket); 1085 if ((error = SYSCTL_OUT(req, &xt, sizeof xt)) != 0) 1086 break; 1087 ++i; 1088 } 1089 LIST_REMOVE(marker, inp_list); 1090 if (error == 0 && i < n) { 1091 bzero(&xt, sizeof(xt)); 1092 xt.xt_len = sizeof(xt); 1093 while (i < n) { 1094 error = SYSCTL_OUT(req, &xt, sizeof (xt)); 1095 if (error) 1096 break; 1097 ++i; 1098 } 1099 } 1100 if (error == 0) { 1101 /* 1102 * Give the user an updated idea of our state. 1103 * If the generation differs from what we told 1104 * her before, she knows that something happened 1105 * while we were processing this request, and it 1106 * might be necessary to retry. 1107 */ 1108 xig.xig_gen = tcbinfo[cpu_id].ipi_gencnt; 1109 xig.xig_sogen = so_gencnt; 1110 xig.xig_count = tcbinfo[cpu_id].ipi_count; 1111 error = SYSCTL_OUT(req, &xig, sizeof xig); 1112 } 1113 } 1114 1115 /* 1116 * Make sure we are on the same cpu we were on originally, since 1117 * higher level callers expect this. Also don't pollute caches with 1118 * migrated userland data by (eventually) returning to userland 1119 * on a different cpu. 1120 */ 1121 lwkt_setcpu_self(globaldata_find(origcpu)); 1122 free(marker, M_TEMP); 1123 return (error); 1124 } 1125 1126 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 1127 tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 1128 1129 static int 1130 tcp_getcred(SYSCTL_HANDLER_ARGS) 1131 { 1132 struct sockaddr_in addrs[2]; 1133 struct inpcb *inp; 1134 int cpu; 1135 int error, s; 1136 1137 error = suser(req->td); 1138 if (error != 0) 1139 return (error); 1140 error = SYSCTL_IN(req, addrs, sizeof addrs); 1141 if (error != 0) 1142 return (error); 1143 s = splnet(); 1144 1145 cpu = tcp_addrcpu(addrs[1].sin_addr.s_addr, addrs[1].sin_port, 1146 addrs[0].sin_addr.s_addr, addrs[0].sin_port); 1147 inp = in_pcblookup_hash(&tcbinfo[cpu], addrs[1].sin_addr, 1148 addrs[1].sin_port, addrs[0].sin_addr, addrs[0].sin_port, 0, NULL); 1149 if (inp == NULL || inp->inp_socket == NULL) { 1150 error = ENOENT; 1151 goto out; 1152 } 1153 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 1154 out: 1155 splx(s); 1156 return (error); 1157 } 1158 1159 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, (CTLTYPE_OPAQUE | CTLFLAG_RW), 1160 0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection"); 1161 1162 #ifdef INET6 1163 static int 1164 tcp6_getcred(SYSCTL_HANDLER_ARGS) 1165 { 1166 struct sockaddr_in6 addrs[2]; 1167 struct inpcb *inp; 1168 int error, s; 1169 boolean_t mapped = FALSE; 1170 1171 error = suser(req->td); 1172 if (error != 0) 1173 return (error); 1174 error = SYSCTL_IN(req, addrs, sizeof addrs); 1175 if (error != 0) 1176 return (error); 1177 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 1178 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 1179 mapped = TRUE; 1180 else 1181 return (EINVAL); 1182 } 1183 s = splnet(); 1184 if (mapped) { 1185 inp = in_pcblookup_hash(&tcbinfo[0], 1186 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 1187 addrs[1].sin6_port, 1188 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 1189 addrs[0].sin6_port, 1190 0, NULL); 1191 } else { 1192 inp = in6_pcblookup_hash(&tcbinfo[0], 1193 &addrs[1].sin6_addr, addrs[1].sin6_port, 1194 &addrs[0].sin6_addr, addrs[0].sin6_port, 1195 0, NULL); 1196 } 1197 if (inp == NULL || inp->inp_socket == NULL) { 1198 error = ENOENT; 1199 goto out; 1200 } 1201 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 1202 out: 1203 splx(s); 1204 return (error); 1205 } 1206 1207 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, (CTLTYPE_OPAQUE | CTLFLAG_RW), 1208 0, 0, 1209 tcp6_getcred, "S,ucred", "Get the ucred of a TCP6 connection"); 1210 #endif 1211 1212 void 1213 tcp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 1214 { 1215 struct ip *ip = vip; 1216 struct tcphdr *th; 1217 struct in_addr faddr; 1218 struct inpcb *inp; 1219 struct tcpcb *tp; 1220 void (*notify)(struct inpcb *, int) = tcp_notify; 1221 tcp_seq icmp_seq; 1222 int cpu; 1223 int s; 1224 1225 faddr = ((struct sockaddr_in *)sa)->sin_addr; 1226 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 1227 return; 1228 1229 if (cmd == PRC_QUENCH) 1230 notify = tcp_quench; 1231 else if (icmp_may_rst && 1232 (cmd == PRC_UNREACH_ADMIN_PROHIB || cmd == PRC_UNREACH_PORT || 1233 cmd == PRC_TIMXCEED_INTRANS) && 1234 ip != NULL) 1235 notify = tcp_drop_syn_sent; 1236 else if (cmd == PRC_MSGSIZE) 1237 notify = tcp_mtudisc; 1238 else if (PRC_IS_REDIRECT(cmd)) { 1239 ip = NULL; 1240 notify = in_rtchange; 1241 } else if (cmd == PRC_HOSTDEAD) 1242 ip = NULL; 1243 else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) 1244 return; 1245 if (ip != NULL) { 1246 s = splnet(); 1247 th = (struct tcphdr *)((caddr_t)ip + 1248 (IP_VHL_HL(ip->ip_vhl) << 2)); 1249 cpu = tcp_addrcpu(faddr.s_addr, th->th_dport, 1250 ip->ip_src.s_addr, th->th_sport); 1251 inp = in_pcblookup_hash(&tcbinfo[cpu], faddr, th->th_dport, 1252 ip->ip_src, th->th_sport, 0, NULL); 1253 if ((inp != NULL) && (inp->inp_socket != NULL)) { 1254 icmp_seq = htonl(th->th_seq); 1255 tp = intotcpcb(inp); 1256 if (SEQ_GEQ(icmp_seq, tp->snd_una) && 1257 SEQ_LT(icmp_seq, tp->snd_max)) 1258 (*notify)(inp, inetctlerrmap[cmd]); 1259 } else { 1260 struct in_conninfo inc; 1261 1262 inc.inc_fport = th->th_dport; 1263 inc.inc_lport = th->th_sport; 1264 inc.inc_faddr = faddr; 1265 inc.inc_laddr = ip->ip_src; 1266 #ifdef INET6 1267 inc.inc_isipv6 = 0; 1268 #endif 1269 syncache_unreach(&inc, th); 1270 } 1271 splx(s); 1272 } else { 1273 for (cpu = 0; cpu < ncpus2; cpu++) { 1274 in_pcbnotifyall(&tcbinfo[cpu].pcblisthead, faddr, 1275 inetctlerrmap[cmd], notify); 1276 } 1277 } 1278 } 1279 1280 #ifdef INET6 1281 void 1282 tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 1283 { 1284 struct tcphdr th; 1285 void (*notify) (struct inpcb *, int) = tcp_notify; 1286 struct ip6_hdr *ip6; 1287 struct mbuf *m; 1288 struct ip6ctlparam *ip6cp = NULL; 1289 const struct sockaddr_in6 *sa6_src = NULL; 1290 int off; 1291 struct tcp_portonly { 1292 u_int16_t th_sport; 1293 u_int16_t th_dport; 1294 } *thp; 1295 1296 if (sa->sa_family != AF_INET6 || 1297 sa->sa_len != sizeof(struct sockaddr_in6)) 1298 return; 1299 1300 if (cmd == PRC_QUENCH) 1301 notify = tcp_quench; 1302 else if (cmd == PRC_MSGSIZE) 1303 notify = tcp_mtudisc; 1304 else if (!PRC_IS_REDIRECT(cmd) && 1305 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 1306 return; 1307 1308 /* if the parameter is from icmp6, decode it. */ 1309 if (d != NULL) { 1310 ip6cp = (struct ip6ctlparam *)d; 1311 m = ip6cp->ip6c_m; 1312 ip6 = ip6cp->ip6c_ip6; 1313 off = ip6cp->ip6c_off; 1314 sa6_src = ip6cp->ip6c_src; 1315 } else { 1316 m = NULL; 1317 ip6 = NULL; 1318 off = 0; /* fool gcc */ 1319 sa6_src = &sa6_any; 1320 } 1321 1322 if (ip6 != NULL) { 1323 struct in_conninfo inc; 1324 /* 1325 * XXX: We assume that when IPV6 is non NULL, 1326 * M and OFF are valid. 1327 */ 1328 1329 /* check if we can safely examine src and dst ports */ 1330 if (m->m_pkthdr.len < off + sizeof *thp) 1331 return; 1332 1333 bzero(&th, sizeof th); 1334 m_copydata(m, off, sizeof *thp, (caddr_t)&th); 1335 1336 in6_pcbnotify(&tcbinfo[0].pcblisthead, sa, th.th_dport, 1337 (struct sockaddr *)ip6cp->ip6c_src, 1338 th.th_sport, cmd, notify); 1339 1340 inc.inc_fport = th.th_dport; 1341 inc.inc_lport = th.th_sport; 1342 inc.inc6_faddr = ((struct sockaddr_in6 *)sa)->sin6_addr; 1343 inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr; 1344 inc.inc_isipv6 = 1; 1345 syncache_unreach(&inc, &th); 1346 } else 1347 in6_pcbnotify(&tcbinfo[0].pcblisthead, sa, 0, 1348 (const struct sockaddr *)sa6_src, 0, cmd, notify); 1349 } 1350 #endif 1351 1352 /* 1353 * Following is where TCP initial sequence number generation occurs. 1354 * 1355 * There are two places where we must use initial sequence numbers: 1356 * 1. In SYN-ACK packets. 1357 * 2. In SYN packets. 1358 * 1359 * All ISNs for SYN-ACK packets are generated by the syncache. See 1360 * tcp_syncache.c for details. 1361 * 1362 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling 1363 * depends on this property. In addition, these ISNs should be 1364 * unguessable so as to prevent connection hijacking. To satisfy 1365 * the requirements of this situation, the algorithm outlined in 1366 * RFC 1948 is used to generate sequence numbers. 1367 * 1368 * Implementation details: 1369 * 1370 * Time is based off the system timer, and is corrected so that it 1371 * increases by one megabyte per second. This allows for proper 1372 * recycling on high speed LANs while still leaving over an hour 1373 * before rollover. 1374 * 1375 * net.inet.tcp.isn_reseed_interval controls the number of seconds 1376 * between seeding of isn_secret. This is normally set to zero, 1377 * as reseeding should not be necessary. 1378 * 1379 */ 1380 1381 #define ISN_BYTES_PER_SECOND 1048576 1382 1383 u_char isn_secret[32]; 1384 int isn_last_reseed; 1385 MD5_CTX isn_ctx; 1386 1387 tcp_seq 1388 tcp_new_isn(struct tcpcb *tp) 1389 { 1390 u_int32_t md5_buffer[4]; 1391 tcp_seq new_isn; 1392 1393 /* Seed if this is the first use, reseed if requested. */ 1394 if ((isn_last_reseed == 0) || ((tcp_isn_reseed_interval > 0) && 1395 (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval*hz) 1396 < (u_int)ticks))) { 1397 read_random_unlimited(&isn_secret, sizeof isn_secret); 1398 isn_last_reseed = ticks; 1399 } 1400 1401 /* Compute the md5 hash and return the ISN. */ 1402 MD5Init(&isn_ctx); 1403 MD5Update(&isn_ctx, (u_char *)&tp->t_inpcb->inp_fport, sizeof(u_short)); 1404 MD5Update(&isn_ctx, (u_char *)&tp->t_inpcb->inp_lport, sizeof(u_short)); 1405 #ifdef INET6 1406 if (tp->t_inpcb->inp_vflag & INP_IPV6) { 1407 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr, 1408 sizeof(struct in6_addr)); 1409 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr, 1410 sizeof(struct in6_addr)); 1411 } else 1412 #endif 1413 { 1414 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr, 1415 sizeof(struct in_addr)); 1416 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr, 1417 sizeof(struct in_addr)); 1418 } 1419 MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret)); 1420 MD5Final((u_char *) &md5_buffer, &isn_ctx); 1421 new_isn = (tcp_seq) md5_buffer[0]; 1422 new_isn += ticks * (ISN_BYTES_PER_SECOND / hz); 1423 return (new_isn); 1424 } 1425 1426 /* 1427 * When a source quench is received, close congestion window 1428 * to one segment. We will gradually open it again as we proceed. 1429 */ 1430 void 1431 tcp_quench(struct inpcb *inp, int errno) 1432 { 1433 struct tcpcb *tp = intotcpcb(inp); 1434 1435 if (tp != NULL) 1436 tp->snd_cwnd = tp->t_maxseg; 1437 } 1438 1439 /* 1440 * When a specific ICMP unreachable message is received and the 1441 * connection state is SYN-SENT, drop the connection. This behavior 1442 * is controlled by the icmp_may_rst sysctl. 1443 */ 1444 void 1445 tcp_drop_syn_sent(struct inpcb *inp, int errno) 1446 { 1447 struct tcpcb *tp = intotcpcb(inp); 1448 1449 if ((tp != NULL) && (tp->t_state == TCPS_SYN_SENT)) 1450 tcp_drop(tp, errno); 1451 } 1452 1453 /* 1454 * When `need fragmentation' ICMP is received, update our idea of the MSS 1455 * based on the new value in the route. Also nudge TCP to send something, 1456 * since we know the packet we just sent was dropped. 1457 * This duplicates some code in the tcp_mss() function in tcp_input.c. 1458 */ 1459 void 1460 tcp_mtudisc(struct inpcb *inp, int errno) 1461 { 1462 struct tcpcb *tp = intotcpcb(inp); 1463 struct rtentry *rt; 1464 struct rmxp_tao *taop; 1465 struct socket *so = inp->inp_socket; 1466 int offered; 1467 int mss; 1468 #ifdef INET6 1469 boolean_t isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0); 1470 #else 1471 const boolean_t isipv6 = FALSE; 1472 #endif 1473 1474 if (tp != NULL) { 1475 if (isipv6) 1476 rt = tcp_rtlookup6(&inp->inp_inc); 1477 else 1478 rt = tcp_rtlookup(&inp->inp_inc); 1479 if (rt == NULL || rt->rt_rmx.rmx_mtu == 0) { 1480 tp->t_maxopd = tp->t_maxseg = 1481 isipv6 ? tcp_v6mssdflt : tcp_mssdflt; 1482 return; 1483 } 1484 taop = rmx_taop(rt->rt_rmx); 1485 offered = taop->tao_mssopt; 1486 mss = rt->rt_rmx.rmx_mtu - 1487 (isipv6 ? 1488 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : 1489 sizeof(struct tcpiphdr)); 1490 1491 if (offered != 0) 1492 mss = min(mss, offered); 1493 /* 1494 * XXX - The above conditional probably violates the TCP 1495 * spec. The problem is that, since we don't know the 1496 * other end's MSS, we are supposed to use a conservative 1497 * default. But, if we do that, then MTU discovery will 1498 * never actually take place, because the conservative 1499 * default is much less than the MTUs typically seen 1500 * on the Internet today. For the moment, we'll sweep 1501 * this under the carpet. 1502 * 1503 * The conservative default might not actually be a problem 1504 * if the only case this occurs is when sending an initial 1505 * SYN with options and data to a host we've never talked 1506 * to before. Then, they will reply with an MSS value which 1507 * will get recorded and the new parameters should get 1508 * recomputed. For Further Study. 1509 */ 1510 if (tp->t_maxopd <= mss) 1511 return; 1512 tp->t_maxopd = mss; 1513 1514 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP && 1515 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 1516 mss -= TCPOLEN_TSTAMP_APPA; 1517 if ((tp->t_flags & (TF_REQ_CC | TF_NOOPT)) == TF_REQ_CC && 1518 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC) 1519 mss -= TCPOLEN_CC_APPA; 1520 #if (MCLBYTES & (MCLBYTES - 1)) == 0 1521 if (mss > MCLBYTES) 1522 mss &= ~(MCLBYTES - 1); 1523 #else 1524 if (mss > MCLBYTES) 1525 mss = mss / MCLBYTES * MCLBYTES; 1526 #endif 1527 if (so->so_snd.sb_hiwat < mss) 1528 mss = so->so_snd.sb_hiwat; 1529 1530 tp->t_maxseg = mss; 1531 1532 tcpstat.tcps_mturesent++; 1533 tp->t_rtttime = 0; 1534 tp->snd_nxt = tp->snd_una; 1535 tcp_output(tp); 1536 } 1537 } 1538 1539 /* 1540 * Look-up the routing entry to the peer of this inpcb. If no route 1541 * is found and it cannot be allocated the return NULL. This routine 1542 * is called by TCP routines that access the rmx structure and by tcp_mss 1543 * to get the interface MTU. 1544 */ 1545 struct rtentry * 1546 tcp_rtlookup(struct in_conninfo *inc) 1547 { 1548 struct route *ro; 1549 struct rtentry *rt; 1550 1551 ro = &inc->inc_route; 1552 rt = ro->ro_rt; 1553 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 1554 /* No route yet, so try to acquire one */ 1555 if (inc->inc_faddr.s_addr != INADDR_ANY) { 1556 /* 1557 * unused portions of the structure MUST be zero'd 1558 * out because rtalloc() treats it as opaque data 1559 */ 1560 bzero(&ro->ro_dst, sizeof(struct sockaddr_in)); 1561 ro->ro_dst.sa_family = AF_INET; 1562 ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 1563 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 1564 inc->inc_faddr; 1565 rtalloc(ro); 1566 rt = ro->ro_rt; 1567 } 1568 } 1569 return (rt); 1570 } 1571 1572 #ifdef INET6 1573 struct rtentry * 1574 tcp_rtlookup6(struct in_conninfo *inc) 1575 { 1576 struct route_in6 *ro6; 1577 struct rtentry *rt; 1578 1579 ro6 = &inc->inc6_route; 1580 rt = ro6->ro_rt; 1581 if (rt == NULL || !(rt->rt_flags & RTF_UP)) { 1582 /* No route yet, so try to acquire one */ 1583 if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) { 1584 /* 1585 * unused portions of the structure MUST be zero'd 1586 * out because rtalloc() treats it as opaque data 1587 */ 1588 bzero(&ro6->ro_dst, sizeof(struct sockaddr_in6)); 1589 ro6->ro_dst.sin6_family = AF_INET6; 1590 ro6->ro_dst.sin6_len = sizeof(struct sockaddr_in6); 1591 ro6->ro_dst.sin6_addr = inc->inc6_faddr; 1592 rtalloc((struct route *)ro6); 1593 rt = ro6->ro_rt; 1594 } 1595 } 1596 return (rt); 1597 } 1598 #endif 1599 1600 #ifdef IPSEC 1601 /* compute ESP/AH header size for TCP, including outer IP header. */ 1602 size_t 1603 ipsec_hdrsiz_tcp(struct tcpcb *tp) 1604 { 1605 struct inpcb *inp; 1606 struct mbuf *m; 1607 size_t hdrsiz; 1608 struct ip *ip; 1609 struct tcphdr *th; 1610 1611 if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL)) 1612 return (0); 1613 MGETHDR(m, MB_DONTWAIT, MT_DATA); 1614 if (!m) 1615 return (0); 1616 1617 #ifdef INET6 1618 if (inp->inp_vflag & INP_IPV6) { 1619 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1620 1621 th = (struct tcphdr *)(ip6 + 1); 1622 m->m_pkthdr.len = m->m_len = 1623 sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1624 tcp_fillheaders(tp, ip6, th); 1625 hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1626 } else 1627 #endif 1628 { 1629 ip = mtod(m, struct ip *); 1630 th = (struct tcphdr *)(ip + 1); 1631 m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); 1632 tcp_fillheaders(tp, ip, th); 1633 hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1634 } 1635 1636 m_free(m); 1637 return (hdrsiz); 1638 } 1639 #endif 1640 1641 /* 1642 * Return a pointer to the cached information about the remote host. 1643 * The cached information is stored in the protocol specific part of 1644 * the route metrics. 1645 */ 1646 struct rmxp_tao * 1647 tcp_gettaocache(struct in_conninfo *inc) 1648 { 1649 struct rtentry *rt; 1650 1651 #ifdef INET6 1652 if (inc->inc_isipv6) 1653 rt = tcp_rtlookup6(inc); 1654 else 1655 #endif 1656 rt = tcp_rtlookup(inc); 1657 1658 /* Make sure this is a host route and is up. */ 1659 if (rt == NULL || 1660 (rt->rt_flags & (RTF_UP | RTF_HOST)) != (RTF_UP | RTF_HOST)) 1661 return (NULL); 1662 1663 return (rmx_taop(rt->rt_rmx)); 1664 } 1665 1666 /* 1667 * Clear all the TAO cache entries, called from tcp_init. 1668 * 1669 * XXX 1670 * This routine is just an empty one, because we assume that the routing 1671 * routing tables are initialized at the same time when TCP, so there is 1672 * nothing in the cache left over. 1673 */ 1674 static void 1675 tcp_cleartaocache() 1676 { 1677 } 1678 1679 /* 1680 * TCP BANDWIDTH DELAY PRODUCT WINDOW LIMITING 1681 * 1682 * This code attempts to calculate the bandwidth-delay product as a 1683 * means of determining the optimal window size to maximize bandwidth, 1684 * minimize RTT, and avoid the over-allocation of buffers on interfaces and 1685 * routers. This code also does a fairly good job keeping RTTs in check 1686 * across slow links like modems. We implement an algorithm which is very 1687 * similar (but not meant to be) TCP/Vegas. The code operates on the 1688 * transmitter side of a TCP connection and so only effects the transmit 1689 * side of the connection. 1690 * 1691 * BACKGROUND: TCP makes no provision for the management of buffer space 1692 * at the end points or at the intermediate routers and switches. A TCP 1693 * stream, whether using NewReno or not, will eventually buffer as 1694 * many packets as it is able and the only reason this typically works is 1695 * due to the fairly small default buffers made available for a connection 1696 * (typicaly 16K or 32K). As machines use larger windows and/or window 1697 * scaling it is now fairly easy for even a single TCP connection to blow-out 1698 * all available buffer space not only on the local interface, but on 1699 * intermediate routers and switches as well. NewReno makes a misguided 1700 * attempt to 'solve' this problem by waiting for an actual failure to occur, 1701 * then backing off, then steadily increasing the window again until another 1702 * failure occurs, ad-infinitum. This results in terrible oscillation that 1703 * is only made worse as network loads increase and the idea of intentionally 1704 * blowing out network buffers is, frankly, a terrible way to manage network 1705 * resources. 1706 * 1707 * It is far better to limit the transmit window prior to the failure 1708 * condition being achieved. There are two general ways to do this: First 1709 * you can 'scan' through different transmit window sizes and locate the 1710 * point where the RTT stops increasing, indicating that you have filled the 1711 * pipe, then scan backwards until you note that RTT stops decreasing, then 1712 * repeat ad-infinitum. This method works in principle but has severe 1713 * implementation issues due to RTT variances, timer granularity, and 1714 * instability in the algorithm which can lead to many false positives and 1715 * create oscillations as well as interact badly with other TCP streams 1716 * implementing the same algorithm. 1717 * 1718 * The second method is to limit the window to the bandwidth delay product 1719 * of the link. This is the method we implement. RTT variances and our 1720 * own manipulation of the congestion window, bwnd, can potentially 1721 * destabilize the algorithm. For this reason we have to stabilize the 1722 * elements used to calculate the window. We do this by using the minimum 1723 * observed RTT, the long term average of the observed bandwidth, and 1724 * by adding two segments worth of slop. It isn't perfect but it is able 1725 * to react to changing conditions and gives us a very stable basis on 1726 * which to extend the algorithm. 1727 */ 1728 void 1729 tcp_xmit_bandwidth_limit(struct tcpcb *tp, tcp_seq ack_seq) 1730 { 1731 u_long bw; 1732 u_long bwnd; 1733 int save_ticks; 1734 1735 /* 1736 * If inflight_enable is disabled in the middle of a tcp connection, 1737 * make sure snd_bwnd is effectively disabled. 1738 */ 1739 if (!tcp_inflight_enable) { 1740 tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 1741 tp->snd_bandwidth = 0; 1742 return; 1743 } 1744 1745 /* 1746 * Figure out the bandwidth. Due to the tick granularity this 1747 * is a very rough number and it MUST be averaged over a fairly 1748 * long period of time. XXX we need to take into account a link 1749 * that is not using all available bandwidth, but for now our 1750 * slop will ramp us up if this case occurs and the bandwidth later 1751 * increases. 1752 * 1753 * Note: if ticks rollover 'bw' may wind up negative. We must 1754 * effectively reset t_bw_rtttime for this case. 1755 */ 1756 save_ticks = ticks; 1757 if ((u_int)(save_ticks - tp->t_bw_rtttime) < 1) 1758 return; 1759 1760 bw = (int64_t)(ack_seq - tp->t_bw_rtseq) * hz / 1761 (save_ticks - tp->t_bw_rtttime); 1762 tp->t_bw_rtttime = save_ticks; 1763 tp->t_bw_rtseq = ack_seq; 1764 if (tp->t_bw_rtttime == 0 || (int)bw < 0) 1765 return; 1766 bw = ((int64_t)tp->snd_bandwidth * 15 + bw) >> 4; 1767 1768 tp->snd_bandwidth = bw; 1769 1770 /* 1771 * Calculate the semi-static bandwidth delay product, plus two maximal 1772 * segments. The additional slop puts us squarely in the sweet 1773 * spot and also handles the bandwidth run-up case. Without the 1774 * slop we could be locking ourselves into a lower bandwidth. 1775 * 1776 * Situations Handled: 1777 * (1) Prevents over-queueing of packets on LANs, especially on 1778 * high speed LANs, allowing larger TCP buffers to be 1779 * specified, and also does a good job preventing 1780 * over-queueing of packets over choke points like modems 1781 * (at least for the transmit side). 1782 * 1783 * (2) Is able to handle changing network loads (bandwidth 1784 * drops so bwnd drops, bandwidth increases so bwnd 1785 * increases). 1786 * 1787 * (3) Theoretically should stabilize in the face of multiple 1788 * connections implementing the same algorithm (this may need 1789 * a little work). 1790 * 1791 * (4) Stability value (defaults to 20 = 2 maximal packets) can 1792 * be adjusted with a sysctl but typically only needs to be on 1793 * very slow connections. A value no smaller then 5 should 1794 * be used, but only reduce this default if you have no other 1795 * choice. 1796 */ 1797 1798 #define USERTT ((tp->t_srtt + tp->t_rttbest) / 2) 1799 bwnd = (int64_t)bw * USERTT / (hz << TCP_RTT_SHIFT) + 1800 tcp_inflight_stab * (int)tp->t_maxseg / 10; 1801 #undef USERTT 1802 1803 if (tcp_inflight_debug > 0) { 1804 static int ltime; 1805 if ((u_int)(ticks - ltime) >= hz / tcp_inflight_debug) { 1806 ltime = ticks; 1807 printf("%p bw %ld rttbest %d srtt %d bwnd %ld\n", 1808 tp, bw, tp->t_rttbest, tp->t_srtt, bwnd); 1809 } 1810 } 1811 if ((long)bwnd < tcp_inflight_min) 1812 bwnd = tcp_inflight_min; 1813 if (bwnd > tcp_inflight_max) 1814 bwnd = tcp_inflight_max; 1815 if ((long)bwnd < tp->t_maxseg * 2) 1816 bwnd = tp->t_maxseg * 2; 1817 tp->snd_bwnd = bwnd; 1818 } 1819