1 /* $NetBSD: tcp_subr.c,v 1.272 2018/01/19 07:53:01 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * 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. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1997, 1998, 2000, 2001, 2008 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 38 * Facility, NASA Ames Research Center. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 91 */ 92 93 #include <sys/cdefs.h> 94 __KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.272 2018/01/19 07:53:01 ozaki-r Exp $"); 95 96 #ifdef _KERNEL_OPT 97 #include "opt_inet.h" 98 #include "opt_ipsec.h" 99 #include "opt_inet_csum.h" 100 #include "opt_mbuftrace.h" 101 #endif 102 103 #include <sys/param.h> 104 #include <sys/atomic.h> 105 #include <sys/proc.h> 106 #include <sys/systm.h> 107 #include <sys/mbuf.h> 108 #include <sys/once.h> 109 #include <sys/socket.h> 110 #include <sys/socketvar.h> 111 #include <sys/protosw.h> 112 #include <sys/errno.h> 113 #include <sys/kernel.h> 114 #include <sys/pool.h> 115 #include <sys/md5.h> 116 #include <sys/cprng.h> 117 118 #include <net/route.h> 119 #include <net/if.h> 120 121 #include <netinet/in.h> 122 #include <netinet/in_systm.h> 123 #include <netinet/ip.h> 124 #include <netinet/in_pcb.h> 125 #include <netinet/ip_var.h> 126 #include <netinet/ip_icmp.h> 127 128 #ifdef INET6 129 #ifndef INET 130 #include <netinet/in.h> 131 #endif 132 #include <netinet/ip6.h> 133 #include <netinet6/in6_pcb.h> 134 #include <netinet6/ip6_var.h> 135 #include <netinet6/in6_var.h> 136 #include <netinet6/ip6protosw.h> 137 #include <netinet/icmp6.h> 138 #include <netinet6/nd6.h> 139 #endif 140 141 #include <netinet/tcp.h> 142 #include <netinet/tcp_fsm.h> 143 #include <netinet/tcp_seq.h> 144 #include <netinet/tcp_timer.h> 145 #include <netinet/tcp_var.h> 146 #include <netinet/tcp_vtw.h> 147 #include <netinet/tcp_private.h> 148 #include <netinet/tcp_congctl.h> 149 #include <netinet/tcpip.h> 150 151 #ifdef IPSEC 152 #include <netipsec/ipsec.h> 153 #include <netipsec/xform.h> 154 #ifdef INET6 155 #include <netipsec/ipsec6.h> 156 #endif 157 #include <netipsec/key.h> 158 #endif /* IPSEC*/ 159 160 161 struct inpcbtable tcbtable; /* head of queue of active tcpcb's */ 162 u_int32_t tcp_now; /* slow ticks, for RFC 1323 timestamps */ 163 164 percpu_t *tcpstat_percpu; 165 166 /* patchable/settable parameters for tcp */ 167 int tcp_mssdflt = TCP_MSS; 168 int tcp_minmss = TCP_MINMSS; 169 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 170 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */ 171 int tcp_do_rfc1948 = 0; /* ISS by cryptographic hash */ 172 int tcp_do_sack = 1; /* selective acknowledgement */ 173 int tcp_do_win_scale = 1; /* RFC1323 window scaling */ 174 int tcp_do_timestamps = 1; /* RFC1323 timestamps */ 175 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */ 176 int tcp_do_ecn = 0; /* Explicit Congestion Notification */ 177 #ifndef TCP_INIT_WIN 178 #define TCP_INIT_WIN 4 /* initial slow start window */ 179 #endif 180 #ifndef TCP_INIT_WIN_LOCAL 181 #define TCP_INIT_WIN_LOCAL 4 /* initial slow start window for local nets */ 182 #endif 183 /* 184 * Up to 5 we scale linearly, to reach 3 * 1460; then (iw) * 1460. 185 * This is to simulate current behavior for iw == 4 186 */ 187 int tcp_init_win_max[] = { 188 1 * 1460, 189 1 * 1460, 190 2 * 1460, 191 2 * 1460, 192 3 * 1460, 193 5 * 1460, 194 6 * 1460, 195 7 * 1460, 196 8 * 1460, 197 9 * 1460, 198 10 * 1460 199 }; 200 int tcp_init_win = TCP_INIT_WIN; 201 int tcp_init_win_local = TCP_INIT_WIN_LOCAL; 202 int tcp_mss_ifmtu = 0; 203 int tcp_rst_ppslim = 100; /* 100pps */ 204 int tcp_ackdrop_ppslim = 100; /* 100pps */ 205 int tcp_do_loopback_cksum = 0; 206 int tcp_do_abc = 1; /* RFC3465 Appropriate byte counting. */ 207 int tcp_abc_aggressive = 1; /* 1: L=2*SMSS 0: L=1*SMSS */ 208 int tcp_sack_tp_maxholes = 32; 209 int tcp_sack_globalmaxholes = 1024; 210 int tcp_sack_globalholes = 0; 211 int tcp_ecn_maxretries = 1; 212 int tcp_msl_enable = 1; /* enable TIME_WAIT truncation */ 213 int tcp_msl_loop = PR_SLOWHZ; /* MSL for loopback */ 214 int tcp_msl_local = 5 * PR_SLOWHZ; /* MSL for 'local' */ 215 int tcp_msl_remote = TCPTV_MSL; /* MSL otherwise */ 216 int tcp_msl_remote_threshold = TCPTV_SRTTDFLT; /* RTT threshold */ 217 int tcp_rttlocal = 0; /* Use RTT to decide who's 'local' */ 218 219 int tcp4_vtw_enable = 0; /* 1 to enable */ 220 int tcp6_vtw_enable = 0; /* 1 to enable */ 221 int tcp_vtw_was_enabled = 0; 222 int tcp_vtw_entries = 1 << 4; /* 16 vestigial TIME_WAIT entries */ 223 224 /* tcb hash */ 225 #ifndef TCBHASHSIZE 226 #define TCBHASHSIZE 128 227 #endif 228 int tcbhashsize = TCBHASHSIZE; 229 230 /* syn hash parameters */ 231 #define TCP_SYN_HASH_SIZE 293 232 #define TCP_SYN_BUCKET_SIZE 35 233 int tcp_syn_cache_size = TCP_SYN_HASH_SIZE; 234 int tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE; 235 int tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE; 236 struct syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE]; 237 238 int tcp_freeq(struct tcpcb *); 239 static int tcp_iss_secret_init(void); 240 241 #ifdef INET 242 static void tcp_mtudisc_callback(struct in_addr); 243 #endif 244 245 #ifdef INET6 246 void tcp6_mtudisc(struct in6pcb *, int); 247 #endif 248 249 static struct pool tcpcb_pool; 250 251 static int tcp_drainwanted; 252 253 #ifdef TCP_CSUM_COUNTERS 254 #include <sys/device.h> 255 256 #if defined(INET) 257 struct evcnt tcp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 258 NULL, "tcp", "hwcsum bad"); 259 struct evcnt tcp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 260 NULL, "tcp", "hwcsum ok"); 261 struct evcnt tcp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 262 NULL, "tcp", "hwcsum data"); 263 struct evcnt tcp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 264 NULL, "tcp", "swcsum"); 265 266 EVCNT_ATTACH_STATIC(tcp_hwcsum_bad); 267 EVCNT_ATTACH_STATIC(tcp_hwcsum_ok); 268 EVCNT_ATTACH_STATIC(tcp_hwcsum_data); 269 EVCNT_ATTACH_STATIC(tcp_swcsum); 270 #endif /* defined(INET) */ 271 272 #if defined(INET6) 273 struct evcnt tcp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 274 NULL, "tcp6", "hwcsum bad"); 275 struct evcnt tcp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 276 NULL, "tcp6", "hwcsum ok"); 277 struct evcnt tcp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 278 NULL, "tcp6", "hwcsum data"); 279 struct evcnt tcp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 280 NULL, "tcp6", "swcsum"); 281 282 EVCNT_ATTACH_STATIC(tcp6_hwcsum_bad); 283 EVCNT_ATTACH_STATIC(tcp6_hwcsum_ok); 284 EVCNT_ATTACH_STATIC(tcp6_hwcsum_data); 285 EVCNT_ATTACH_STATIC(tcp6_swcsum); 286 #endif /* defined(INET6) */ 287 #endif /* TCP_CSUM_COUNTERS */ 288 289 290 #ifdef TCP_OUTPUT_COUNTERS 291 #include <sys/device.h> 292 293 struct evcnt tcp_output_bigheader = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 294 NULL, "tcp", "output big header"); 295 struct evcnt tcp_output_predict_hit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 296 NULL, "tcp", "output predict hit"); 297 struct evcnt tcp_output_predict_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 298 NULL, "tcp", "output predict miss"); 299 struct evcnt tcp_output_copysmall = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 300 NULL, "tcp", "output copy small"); 301 struct evcnt tcp_output_copybig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 302 NULL, "tcp", "output copy big"); 303 struct evcnt tcp_output_refbig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 304 NULL, "tcp", "output reference big"); 305 306 EVCNT_ATTACH_STATIC(tcp_output_bigheader); 307 EVCNT_ATTACH_STATIC(tcp_output_predict_hit); 308 EVCNT_ATTACH_STATIC(tcp_output_predict_miss); 309 EVCNT_ATTACH_STATIC(tcp_output_copysmall); 310 EVCNT_ATTACH_STATIC(tcp_output_copybig); 311 EVCNT_ATTACH_STATIC(tcp_output_refbig); 312 313 #endif /* TCP_OUTPUT_COUNTERS */ 314 315 #ifdef TCP_REASS_COUNTERS 316 #include <sys/device.h> 317 318 struct evcnt tcp_reass_ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 319 NULL, "tcp_reass", "calls"); 320 struct evcnt tcp_reass_empty = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 321 &tcp_reass_, "tcp_reass", "insert into empty queue"); 322 struct evcnt tcp_reass_iteration[8] = { 323 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", ">7 iterations"), 324 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "1 iteration"), 325 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "2 iterations"), 326 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "3 iterations"), 327 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "4 iterations"), 328 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "5 iterations"), 329 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "6 iterations"), 330 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "7 iterations"), 331 }; 332 struct evcnt tcp_reass_prependfirst = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 333 &tcp_reass_, "tcp_reass", "prepend to first"); 334 struct evcnt tcp_reass_prepend = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 335 &tcp_reass_, "tcp_reass", "prepend"); 336 struct evcnt tcp_reass_insert = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 337 &tcp_reass_, "tcp_reass", "insert"); 338 struct evcnt tcp_reass_inserttail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 339 &tcp_reass_, "tcp_reass", "insert at tail"); 340 struct evcnt tcp_reass_append = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 341 &tcp_reass_, "tcp_reass", "append"); 342 struct evcnt tcp_reass_appendtail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 343 &tcp_reass_, "tcp_reass", "append to tail fragment"); 344 struct evcnt tcp_reass_overlaptail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 345 &tcp_reass_, "tcp_reass", "overlap at end"); 346 struct evcnt tcp_reass_overlapfront = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 347 &tcp_reass_, "tcp_reass", "overlap at start"); 348 struct evcnt tcp_reass_segdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 349 &tcp_reass_, "tcp_reass", "duplicate segment"); 350 struct evcnt tcp_reass_fragdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 351 &tcp_reass_, "tcp_reass", "duplicate fragment"); 352 353 EVCNT_ATTACH_STATIC(tcp_reass_); 354 EVCNT_ATTACH_STATIC(tcp_reass_empty); 355 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 0); 356 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 1); 357 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 2); 358 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 3); 359 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 4); 360 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 5); 361 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 6); 362 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 7); 363 EVCNT_ATTACH_STATIC(tcp_reass_prependfirst); 364 EVCNT_ATTACH_STATIC(tcp_reass_prepend); 365 EVCNT_ATTACH_STATIC(tcp_reass_insert); 366 EVCNT_ATTACH_STATIC(tcp_reass_inserttail); 367 EVCNT_ATTACH_STATIC(tcp_reass_append); 368 EVCNT_ATTACH_STATIC(tcp_reass_appendtail); 369 EVCNT_ATTACH_STATIC(tcp_reass_overlaptail); 370 EVCNT_ATTACH_STATIC(tcp_reass_overlapfront); 371 EVCNT_ATTACH_STATIC(tcp_reass_segdup); 372 EVCNT_ATTACH_STATIC(tcp_reass_fragdup); 373 374 #endif /* TCP_REASS_COUNTERS */ 375 376 #ifdef MBUFTRACE 377 struct mowner tcp_mowner = MOWNER_INIT("tcp", ""); 378 struct mowner tcp_rx_mowner = MOWNER_INIT("tcp", "rx"); 379 struct mowner tcp_tx_mowner = MOWNER_INIT("tcp", "tx"); 380 struct mowner tcp_sock_mowner = MOWNER_INIT("tcp", "sock"); 381 struct mowner tcp_sock_rx_mowner = MOWNER_INIT("tcp", "sock rx"); 382 struct mowner tcp_sock_tx_mowner = MOWNER_INIT("tcp", "sock tx"); 383 #endif 384 385 static int 386 do_tcpinit(void) 387 { 388 389 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize); 390 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", 391 NULL, IPL_SOFTNET); 392 393 tcp_usrreq_init(); 394 395 /* Initialize timer state. */ 396 tcp_timer_init(); 397 398 /* Initialize the compressed state engine. */ 399 syn_cache_init(); 400 401 /* Initialize the congestion control algorithms. */ 402 tcp_congctl_init(); 403 404 /* Initialize the TCPCB template. */ 405 tcp_tcpcb_template(); 406 407 /* Initialize reassembly queue */ 408 tcpipqent_init(); 409 410 /* SACK */ 411 tcp_sack_init(); 412 413 MOWNER_ATTACH(&tcp_tx_mowner); 414 MOWNER_ATTACH(&tcp_rx_mowner); 415 MOWNER_ATTACH(&tcp_reass_mowner); 416 MOWNER_ATTACH(&tcp_sock_mowner); 417 MOWNER_ATTACH(&tcp_sock_tx_mowner); 418 MOWNER_ATTACH(&tcp_sock_rx_mowner); 419 MOWNER_ATTACH(&tcp_mowner); 420 421 tcpstat_percpu = percpu_alloc(sizeof(uint64_t) * TCP_NSTATS); 422 423 vtw_earlyinit(); 424 425 tcp_slowtimo_init(); 426 427 return 0; 428 } 429 430 void 431 tcp_init_common(unsigned basehlen) 432 { 433 static ONCE_DECL(dotcpinit); 434 unsigned hlen = basehlen + sizeof(struct tcphdr); 435 unsigned oldhlen; 436 437 if (max_linkhdr + hlen > MHLEN) 438 panic("tcp_init"); 439 while ((oldhlen = max_protohdr) < hlen) 440 atomic_cas_uint(&max_protohdr, oldhlen, hlen); 441 442 RUN_ONCE(&dotcpinit, do_tcpinit); 443 } 444 445 /* 446 * Tcp initialization 447 */ 448 void 449 tcp_init(void) 450 { 451 452 icmp_mtudisc_callback_register(tcp_mtudisc_callback); 453 454 tcp_init_common(sizeof(struct ip)); 455 } 456 457 /* 458 * Create template to be used to send tcp packets on a connection. 459 * Call after host entry created, allocates an mbuf and fills 460 * in a skeletal tcp/ip header, minimizing the amount of work 461 * necessary when the connection is used. 462 */ 463 struct mbuf * 464 tcp_template(struct tcpcb *tp) 465 { 466 struct inpcb *inp = tp->t_inpcb; 467 #ifdef INET6 468 struct in6pcb *in6p = tp->t_in6pcb; 469 #endif 470 struct tcphdr *n; 471 struct mbuf *m; 472 int hlen; 473 474 switch (tp->t_family) { 475 case AF_INET: 476 hlen = sizeof(struct ip); 477 if (inp) 478 break; 479 #ifdef INET6 480 if (in6p) { 481 /* mapped addr case */ 482 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 483 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) 484 break; 485 } 486 #endif 487 return NULL; /*EINVAL*/ 488 #ifdef INET6 489 case AF_INET6: 490 hlen = sizeof(struct ip6_hdr); 491 if (in6p) { 492 /* more sainty check? */ 493 break; 494 } 495 return NULL; /*EINVAL*/ 496 #endif 497 default: 498 hlen = 0; /*pacify gcc*/ 499 return NULL; /*EAFNOSUPPORT*/ 500 } 501 #ifdef DIAGNOSTIC 502 if (hlen + sizeof(struct tcphdr) > MCLBYTES) 503 panic("mclbytes too small for t_template"); 504 #endif 505 m = tp->t_template; 506 if (m && m->m_len == hlen + sizeof(struct tcphdr)) 507 ; 508 else { 509 if (m) 510 m_freem(m); 511 m = tp->t_template = NULL; 512 MGETHDR(m, M_DONTWAIT, MT_HEADER); 513 if (m && hlen + sizeof(struct tcphdr) > MHLEN) { 514 MCLGET(m, M_DONTWAIT); 515 if ((m->m_flags & M_EXT) == 0) { 516 m_free(m); 517 m = NULL; 518 } 519 } 520 if (m == NULL) 521 return NULL; 522 MCLAIM(m, &tcp_mowner); 523 m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr); 524 } 525 526 memset(mtod(m, void *), 0, m->m_len); 527 528 n = (struct tcphdr *)(mtod(m, char *) + hlen); 529 530 switch (tp->t_family) { 531 case AF_INET: 532 { 533 struct ipovly *ipov; 534 mtod(m, struct ip *)->ip_v = 4; 535 mtod(m, struct ip *)->ip_hl = hlen >> 2; 536 ipov = mtod(m, struct ipovly *); 537 ipov->ih_pr = IPPROTO_TCP; 538 ipov->ih_len = htons(sizeof(struct tcphdr)); 539 if (inp) { 540 ipov->ih_src = inp->inp_laddr; 541 ipov->ih_dst = inp->inp_faddr; 542 } 543 #ifdef INET6 544 else if (in6p) { 545 /* mapped addr case */ 546 bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src, 547 sizeof(ipov->ih_src)); 548 bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst, 549 sizeof(ipov->ih_dst)); 550 } 551 #endif 552 /* 553 * Compute the pseudo-header portion of the checksum 554 * now. We incrementally add in the TCP option and 555 * payload lengths later, and then compute the TCP 556 * checksum right before the packet is sent off onto 557 * the wire. 558 */ 559 n->th_sum = in_cksum_phdr(ipov->ih_src.s_addr, 560 ipov->ih_dst.s_addr, 561 htons(sizeof(struct tcphdr) + IPPROTO_TCP)); 562 break; 563 } 564 #ifdef INET6 565 case AF_INET6: 566 { 567 struct ip6_hdr *ip6; 568 mtod(m, struct ip *)->ip_v = 6; 569 ip6 = mtod(m, struct ip6_hdr *); 570 ip6->ip6_nxt = IPPROTO_TCP; 571 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 572 ip6->ip6_src = in6p->in6p_laddr; 573 ip6->ip6_dst = in6p->in6p_faddr; 574 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 575 if (ip6_auto_flowlabel) { 576 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK; 577 ip6->ip6_flow |= 578 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 579 } 580 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 581 ip6->ip6_vfc |= IPV6_VERSION; 582 583 /* 584 * Compute the pseudo-header portion of the checksum 585 * now. We incrementally add in the TCP option and 586 * payload lengths later, and then compute the TCP 587 * checksum right before the packet is sent off onto 588 * the wire. 589 */ 590 n->th_sum = in6_cksum_phdr(&in6p->in6p_laddr, 591 &in6p->in6p_faddr, htonl(sizeof(struct tcphdr)), 592 htonl(IPPROTO_TCP)); 593 break; 594 } 595 #endif 596 } 597 if (inp) { 598 n->th_sport = inp->inp_lport; 599 n->th_dport = inp->inp_fport; 600 } 601 #ifdef INET6 602 else if (in6p) { 603 n->th_sport = in6p->in6p_lport; 604 n->th_dport = in6p->in6p_fport; 605 } 606 #endif 607 n->th_seq = 0; 608 n->th_ack = 0; 609 n->th_x2 = 0; 610 n->th_off = 5; 611 n->th_flags = 0; 612 n->th_win = 0; 613 n->th_urp = 0; 614 return (m); 615 } 616 617 /* 618 * Send a single message to the TCP at address specified by 619 * the given TCP/IP header. If m == 0, then we make a copy 620 * of the tcpiphdr at ti and send directly to the addressed host. 621 * This is used to force keep alive messages out using the TCP 622 * template for a connection tp->t_template. If flags are given 623 * then we send a message back to the TCP which originated the 624 * segment ti, and discard the mbuf containing it and any other 625 * attached mbufs. 626 * 627 * In any case the ack and sequence number of the transmitted 628 * segment are as specified by the parameters. 629 */ 630 int 631 tcp_respond(struct tcpcb *tp, struct mbuf *mtemplate, struct mbuf *m, 632 struct tcphdr *th0, tcp_seq ack, tcp_seq seq, int flags) 633 { 634 struct route *ro; 635 int error, tlen, win = 0; 636 int hlen; 637 struct ip *ip; 638 #ifdef INET6 639 struct ip6_hdr *ip6; 640 #endif 641 int family; /* family on packet, not inpcb/in6pcb! */ 642 struct tcphdr *th; 643 644 if (tp != NULL && (flags & TH_RST) == 0) { 645 #ifdef DIAGNOSTIC 646 if (tp->t_inpcb && tp->t_in6pcb) 647 panic("tcp_respond: both t_inpcb and t_in6pcb are set"); 648 #endif 649 #ifdef INET 650 if (tp->t_inpcb) 651 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 652 #endif 653 #ifdef INET6 654 if (tp->t_in6pcb) 655 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv); 656 #endif 657 } 658 659 th = NULL; /* Quell uninitialized warning */ 660 ip = NULL; 661 #ifdef INET6 662 ip6 = NULL; 663 #endif 664 if (m == 0) { 665 if (!mtemplate) 666 return EINVAL; 667 668 /* get family information from template */ 669 switch (mtod(mtemplate, struct ip *)->ip_v) { 670 case 4: 671 family = AF_INET; 672 hlen = sizeof(struct ip); 673 break; 674 #ifdef INET6 675 case 6: 676 family = AF_INET6; 677 hlen = sizeof(struct ip6_hdr); 678 break; 679 #endif 680 default: 681 return EAFNOSUPPORT; 682 } 683 684 MGETHDR(m, M_DONTWAIT, MT_HEADER); 685 if (m) { 686 MCLAIM(m, &tcp_tx_mowner); 687 MCLGET(m, M_DONTWAIT); 688 if ((m->m_flags & M_EXT) == 0) { 689 m_free(m); 690 m = NULL; 691 } 692 } 693 if (m == NULL) 694 return (ENOBUFS); 695 696 tlen = 0; 697 698 m->m_data += max_linkhdr; 699 bcopy(mtod(mtemplate, void *), mtod(m, void *), 700 mtemplate->m_len); 701 switch (family) { 702 case AF_INET: 703 ip = mtod(m, struct ip *); 704 th = (struct tcphdr *)(ip + 1); 705 break; 706 #ifdef INET6 707 case AF_INET6: 708 ip6 = mtod(m, struct ip6_hdr *); 709 th = (struct tcphdr *)(ip6 + 1); 710 break; 711 #endif 712 #if 0 713 default: 714 /* noone will visit here */ 715 m_freem(m); 716 return EAFNOSUPPORT; 717 #endif 718 } 719 flags = TH_ACK; 720 } else { 721 722 if ((m->m_flags & M_PKTHDR) == 0) { 723 #if 0 724 printf("non PKTHDR to tcp_respond\n"); 725 #endif 726 m_freem(m); 727 return EINVAL; 728 } 729 #ifdef DIAGNOSTIC 730 if (!th0) 731 panic("th0 == NULL in tcp_respond"); 732 #endif 733 734 /* get family information from m */ 735 switch (mtod(m, struct ip *)->ip_v) { 736 case 4: 737 family = AF_INET; 738 hlen = sizeof(struct ip); 739 ip = mtod(m, struct ip *); 740 break; 741 #ifdef INET6 742 case 6: 743 family = AF_INET6; 744 hlen = sizeof(struct ip6_hdr); 745 ip6 = mtod(m, struct ip6_hdr *); 746 break; 747 #endif 748 default: 749 m_freem(m); 750 return EAFNOSUPPORT; 751 } 752 /* clear h/w csum flags inherited from rx packet */ 753 m->m_pkthdr.csum_flags = 0; 754 755 if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2)) 756 tlen = sizeof(*th0); 757 else 758 tlen = th0->th_off << 2; 759 760 if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 && 761 mtod(m, char *) + hlen == (char *)th0) { 762 m->m_len = hlen + tlen; 763 m_freem(m->m_next); 764 m->m_next = NULL; 765 } else { 766 struct mbuf *n; 767 768 #ifdef DIAGNOSTIC 769 if (max_linkhdr + hlen + tlen > MCLBYTES) { 770 m_freem(m); 771 return EMSGSIZE; 772 } 773 #endif 774 MGETHDR(n, M_DONTWAIT, MT_HEADER); 775 if (n && max_linkhdr + hlen + tlen > MHLEN) { 776 MCLGET(n, M_DONTWAIT); 777 if ((n->m_flags & M_EXT) == 0) { 778 m_freem(n); 779 n = NULL; 780 } 781 } 782 if (!n) { 783 m_freem(m); 784 return ENOBUFS; 785 } 786 787 MCLAIM(n, &tcp_tx_mowner); 788 n->m_data += max_linkhdr; 789 n->m_len = hlen + tlen; 790 m_copyback(n, 0, hlen, mtod(m, void *)); 791 m_copyback(n, hlen, tlen, (void *)th0); 792 793 m_freem(m); 794 m = n; 795 n = NULL; 796 } 797 798 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 799 switch (family) { 800 case AF_INET: 801 ip = mtod(m, struct ip *); 802 th = (struct tcphdr *)(ip + 1); 803 ip->ip_p = IPPROTO_TCP; 804 xchg(ip->ip_dst, ip->ip_src, struct in_addr); 805 ip->ip_p = IPPROTO_TCP; 806 break; 807 #ifdef INET6 808 case AF_INET6: 809 ip6 = mtod(m, struct ip6_hdr *); 810 th = (struct tcphdr *)(ip6 + 1); 811 ip6->ip6_nxt = IPPROTO_TCP; 812 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 813 ip6->ip6_nxt = IPPROTO_TCP; 814 break; 815 #endif 816 #if 0 817 default: 818 /* noone will visit here */ 819 m_freem(m); 820 return EAFNOSUPPORT; 821 #endif 822 } 823 xchg(th->th_dport, th->th_sport, u_int16_t); 824 #undef xchg 825 tlen = 0; /*be friendly with the following code*/ 826 } 827 th->th_seq = htonl(seq); 828 th->th_ack = htonl(ack); 829 th->th_x2 = 0; 830 if ((flags & TH_SYN) == 0) { 831 if (tp) 832 win >>= tp->rcv_scale; 833 if (win > TCP_MAXWIN) 834 win = TCP_MAXWIN; 835 th->th_win = htons((u_int16_t)win); 836 th->th_off = sizeof (struct tcphdr) >> 2; 837 tlen += sizeof(*th); 838 } else 839 tlen += th->th_off << 2; 840 m->m_len = hlen + tlen; 841 m->m_pkthdr.len = hlen + tlen; 842 m_reset_rcvif(m); 843 th->th_flags = flags; 844 th->th_urp = 0; 845 846 switch (family) { 847 #ifdef INET 848 case AF_INET: 849 { 850 struct ipovly *ipov = (struct ipovly *)ip; 851 memset(ipov->ih_x1, 0, sizeof ipov->ih_x1); 852 ipov->ih_len = htons((u_int16_t)tlen); 853 854 th->th_sum = 0; 855 th->th_sum = in_cksum(m, hlen + tlen); 856 ip->ip_len = htons(hlen + tlen); 857 ip->ip_ttl = ip_defttl; 858 break; 859 } 860 #endif 861 #ifdef INET6 862 case AF_INET6: 863 { 864 th->th_sum = 0; 865 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 866 tlen); 867 ip6->ip6_plen = htons(tlen); 868 if (tp && tp->t_in6pcb) 869 ip6->ip6_hlim = in6_selecthlim_rt(tp->t_in6pcb); 870 else 871 ip6->ip6_hlim = ip6_defhlim; 872 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK; 873 if (ip6_auto_flowlabel) { 874 ip6->ip6_flow |= 875 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 876 } 877 break; 878 } 879 #endif 880 } 881 882 if (tp != NULL && tp->t_inpcb != NULL) { 883 ro = &tp->t_inpcb->inp_route; 884 #ifdef DIAGNOSTIC 885 if (family != AF_INET) 886 panic("tcp_respond: address family mismatch"); 887 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) { 888 panic("tcp_respond: ip_dst %x != inp_faddr %x", 889 ntohl(ip->ip_dst.s_addr), 890 ntohl(tp->t_inpcb->inp_faddr.s_addr)); 891 } 892 #endif 893 } 894 #ifdef INET6 895 else if (tp != NULL && tp->t_in6pcb != NULL) { 896 ro = (struct route *)&tp->t_in6pcb->in6p_route; 897 #ifdef DIAGNOSTIC 898 if (family == AF_INET) { 899 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr)) 900 panic("tcp_respond: not mapped addr"); 901 if (memcmp(&ip->ip_dst, 902 &tp->t_in6pcb->in6p_faddr.s6_addr32[3], 903 sizeof(ip->ip_dst)) != 0) { 904 panic("tcp_respond: ip_dst != in6p_faddr"); 905 } 906 } else if (family == AF_INET6) { 907 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 908 &tp->t_in6pcb->in6p_faddr)) 909 panic("tcp_respond: ip6_dst != in6p_faddr"); 910 } else 911 panic("tcp_respond: address family mismatch"); 912 #endif 913 } 914 #endif 915 else 916 ro = NULL; 917 918 switch (family) { 919 #ifdef INET 920 case AF_INET: 921 error = ip_output(m, NULL, ro, 922 (tp && tp->t_mtudisc ? IP_MTUDISC : 0), NULL, 923 tp ? tp->t_inpcb : NULL); 924 break; 925 #endif 926 #ifdef INET6 927 case AF_INET6: 928 error = ip6_output(m, NULL, ro, 0, NULL, 929 tp ? tp->t_in6pcb : NULL, NULL); 930 break; 931 #endif 932 default: 933 error = EAFNOSUPPORT; 934 break; 935 } 936 937 return (error); 938 } 939 940 /* 941 * Template TCPCB. Rather than zeroing a new TCPCB and initializing 942 * a bunch of members individually, we maintain this template for the 943 * static and mostly-static components of the TCPCB, and copy it into 944 * the new TCPCB instead. 945 */ 946 static struct tcpcb tcpcb_template = { 947 .t_srtt = TCPTV_SRTTBASE, 948 .t_rttmin = TCPTV_MIN, 949 950 .snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT, 951 .snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT, 952 .snd_numholes = 0, 953 .snd_cubic_wmax = 0, 954 .snd_cubic_wmax_last = 0, 955 .snd_cubic_ctime = 0, 956 957 .t_partialacks = -1, 958 .t_bytes_acked = 0, 959 .t_sndrexmitpack = 0, 960 .t_rcvoopack = 0, 961 .t_sndzerowin = 0, 962 }; 963 964 /* 965 * Updates the TCPCB template whenever a parameter that would affect 966 * the template is changed. 967 */ 968 void 969 tcp_tcpcb_template(void) 970 { 971 struct tcpcb *tp = &tcpcb_template; 972 int flags; 973 974 tp->t_peermss = tcp_mssdflt; 975 tp->t_ourmss = tcp_mssdflt; 976 tp->t_segsz = tcp_mssdflt; 977 978 flags = 0; 979 if (tcp_do_rfc1323 && tcp_do_win_scale) 980 flags |= TF_REQ_SCALE; 981 if (tcp_do_rfc1323 && tcp_do_timestamps) 982 flags |= TF_REQ_TSTMP; 983 tp->t_flags = flags; 984 985 /* 986 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 987 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 988 * reasonable initial retransmit time. 989 */ 990 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1); 991 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 992 TCPTV_MIN, TCPTV_REXMTMAX); 993 994 /* Keep Alive */ 995 tp->t_keepinit = tcp_keepinit; 996 tp->t_keepidle = tcp_keepidle; 997 tp->t_keepintvl = tcp_keepintvl; 998 tp->t_keepcnt = tcp_keepcnt; 999 tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl; 1000 1001 /* MSL */ 1002 tp->t_msl = TCPTV_MSL; 1003 } 1004 1005 /* 1006 * Create a new TCP control block, making an 1007 * empty reassembly queue and hooking it to the argument 1008 * protocol control block. 1009 */ 1010 /* family selects inpcb, or in6pcb */ 1011 struct tcpcb * 1012 tcp_newtcpcb(int family, void *aux) 1013 { 1014 struct tcpcb *tp; 1015 int i; 1016 1017 /* XXX Consider using a pool_cache for speed. */ 1018 tp = pool_get(&tcpcb_pool, PR_NOWAIT); /* splsoftnet via tcp_usrreq */ 1019 if (tp == NULL) 1020 return (NULL); 1021 memcpy(tp, &tcpcb_template, sizeof(*tp)); 1022 TAILQ_INIT(&tp->segq); 1023 TAILQ_INIT(&tp->timeq); 1024 tp->t_family = family; /* may be overridden later on */ 1025 TAILQ_INIT(&tp->snd_holes); 1026 LIST_INIT(&tp->t_sc); /* XXX can template this */ 1027 1028 /* Don't sweat this loop; hopefully the compiler will unroll it. */ 1029 for (i = 0; i < TCPT_NTIMERS; i++) { 1030 callout_init(&tp->t_timer[i], CALLOUT_MPSAFE); 1031 TCP_TIMER_INIT(tp, i); 1032 } 1033 callout_init(&tp->t_delack_ch, CALLOUT_MPSAFE); 1034 1035 switch (family) { 1036 case AF_INET: 1037 { 1038 struct inpcb *inp = (struct inpcb *)aux; 1039 1040 inp->inp_ip.ip_ttl = ip_defttl; 1041 inp->inp_ppcb = (void *)tp; 1042 1043 tp->t_inpcb = inp; 1044 tp->t_mtudisc = ip_mtudisc; 1045 break; 1046 } 1047 #ifdef INET6 1048 case AF_INET6: 1049 { 1050 struct in6pcb *in6p = (struct in6pcb *)aux; 1051 1052 in6p->in6p_ip6.ip6_hlim = in6_selecthlim_rt(in6p); 1053 in6p->in6p_ppcb = (void *)tp; 1054 1055 tp->t_in6pcb = in6p; 1056 /* for IPv6, always try to run path MTU discovery */ 1057 tp->t_mtudisc = 1; 1058 break; 1059 } 1060 #endif /* INET6 */ 1061 default: 1062 for (i = 0; i < TCPT_NTIMERS; i++) 1063 callout_destroy(&tp->t_timer[i]); 1064 callout_destroy(&tp->t_delack_ch); 1065 pool_put(&tcpcb_pool, tp); /* splsoftnet via tcp_usrreq */ 1066 return (NULL); 1067 } 1068 1069 /* 1070 * Initialize our timebase. When we send timestamps, we take 1071 * the delta from tcp_now -- this means each connection always 1072 * gets a timebase of 1, which makes it, among other things, 1073 * more difficult to determine how long a system has been up, 1074 * and thus how many TCP sequence increments have occurred. 1075 * 1076 * We start with 1, because 0 doesn't work with linux, which 1077 * considers timestamp 0 in a SYN packet as a bug and disables 1078 * timestamps. 1079 */ 1080 tp->ts_timebase = tcp_now - 1; 1081 1082 tcp_congctl_select(tp, tcp_congctl_global_name); 1083 1084 return (tp); 1085 } 1086 1087 /* 1088 * Drop a TCP connection, reporting 1089 * the specified error. If connection is synchronized, 1090 * then send a RST to peer. 1091 */ 1092 struct tcpcb * 1093 tcp_drop(struct tcpcb *tp, int errno) 1094 { 1095 struct socket *so = NULL; 1096 1097 #ifdef DIAGNOSTIC 1098 if (tp->t_inpcb && tp->t_in6pcb) 1099 panic("tcp_drop: both t_inpcb and t_in6pcb are set"); 1100 #endif 1101 #ifdef INET 1102 if (tp->t_inpcb) 1103 so = tp->t_inpcb->inp_socket; 1104 #endif 1105 #ifdef INET6 1106 if (tp->t_in6pcb) 1107 so = tp->t_in6pcb->in6p_socket; 1108 #endif 1109 if (!so) 1110 return NULL; 1111 1112 if (TCPS_HAVERCVDSYN(tp->t_state)) { 1113 tp->t_state = TCPS_CLOSED; 1114 (void) tcp_output(tp); 1115 TCP_STATINC(TCP_STAT_DROPS); 1116 } else 1117 TCP_STATINC(TCP_STAT_CONNDROPS); 1118 if (errno == ETIMEDOUT && tp->t_softerror) 1119 errno = tp->t_softerror; 1120 so->so_error = errno; 1121 return (tcp_close(tp)); 1122 } 1123 1124 /* 1125 * Close a TCP control block: 1126 * discard all space held by the tcp 1127 * discard internet protocol block 1128 * wake up any sleepers 1129 */ 1130 struct tcpcb * 1131 tcp_close(struct tcpcb *tp) 1132 { 1133 struct inpcb *inp; 1134 #ifdef INET6 1135 struct in6pcb *in6p; 1136 #endif 1137 struct socket *so; 1138 #ifdef RTV_RTT 1139 struct rtentry *rt = NULL; 1140 #endif 1141 struct route *ro; 1142 int j; 1143 1144 inp = tp->t_inpcb; 1145 #ifdef INET6 1146 in6p = tp->t_in6pcb; 1147 #endif 1148 so = NULL; 1149 ro = NULL; 1150 if (inp) { 1151 so = inp->inp_socket; 1152 ro = &inp->inp_route; 1153 } 1154 #ifdef INET6 1155 else if (in6p) { 1156 so = in6p->in6p_socket; 1157 ro = (struct route *)&in6p->in6p_route; 1158 } 1159 #endif 1160 1161 #ifdef RTV_RTT 1162 /* 1163 * If we sent enough data to get some meaningful characteristics, 1164 * save them in the routing entry. 'Enough' is arbitrarily 1165 * defined as the sendpipesize (default 4K) * 16. This would 1166 * give us 16 rtt samples assuming we only get one sample per 1167 * window (the usual case on a long haul net). 16 samples is 1168 * enough for the srtt filter to converge to within 5% of the correct 1169 * value; fewer samples and we could save a very bogus rtt. 1170 * 1171 * Don't update the default route's characteristics and don't 1172 * update anything that the user "locked". 1173 */ 1174 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 1175 ro && (rt = rtcache_validate(ro)) != NULL && 1176 !in_nullhost(satocsin(rt_getkey(rt))->sin_addr)) { 1177 u_long i = 0; 1178 1179 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 1180 i = tp->t_srtt * 1181 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 1182 if (rt->rt_rmx.rmx_rtt && i) 1183 /* 1184 * filter this update to half the old & half 1185 * the new values, converting scale. 1186 * See route.h and tcp_var.h for a 1187 * description of the scaling constants. 1188 */ 1189 rt->rt_rmx.rmx_rtt = 1190 (rt->rt_rmx.rmx_rtt + i) / 2; 1191 else 1192 rt->rt_rmx.rmx_rtt = i; 1193 } 1194 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 1195 i = tp->t_rttvar * 1196 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2)); 1197 if (rt->rt_rmx.rmx_rttvar && i) 1198 rt->rt_rmx.rmx_rttvar = 1199 (rt->rt_rmx.rmx_rttvar + i) / 2; 1200 else 1201 rt->rt_rmx.rmx_rttvar = i; 1202 } 1203 /* 1204 * update the pipelimit (ssthresh) if it has been updated 1205 * already or if a pipesize was specified & the threshhold 1206 * got below half the pipesize. I.e., wait for bad news 1207 * before we start updating, then update on both good 1208 * and bad news. 1209 */ 1210 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 1211 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) || 1212 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 1213 /* 1214 * convert the limit from user data bytes to 1215 * packets then to packet data bytes. 1216 */ 1217 i = (i + tp->t_segsz / 2) / tp->t_segsz; 1218 if (i < 2) 1219 i = 2; 1220 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr)); 1221 if (rt->rt_rmx.rmx_ssthresh) 1222 rt->rt_rmx.rmx_ssthresh = 1223 (rt->rt_rmx.rmx_ssthresh + i) / 2; 1224 else 1225 rt->rt_rmx.rmx_ssthresh = i; 1226 } 1227 } 1228 rtcache_unref(rt, ro); 1229 #endif /* RTV_RTT */ 1230 /* free the reassembly queue, if any */ 1231 TCP_REASS_LOCK(tp); 1232 (void) tcp_freeq(tp); 1233 TCP_REASS_UNLOCK(tp); 1234 1235 /* free the SACK holes list. */ 1236 tcp_free_sackholes(tp); 1237 tcp_congctl_release(tp); 1238 syn_cache_cleanup(tp); 1239 1240 if (tp->t_template) { 1241 m_free(tp->t_template); 1242 tp->t_template = NULL; 1243 } 1244 1245 /* 1246 * Detaching the pcb will unlock the socket/tcpcb, and stopping 1247 * the timers can also drop the lock. We need to prevent access 1248 * to the tcpcb as it's half torn down. Flag the pcb as dead 1249 * (prevents access by timers) and only then detach it. 1250 */ 1251 tp->t_flags |= TF_DEAD; 1252 if (inp) { 1253 inp->inp_ppcb = 0; 1254 soisdisconnected(so); 1255 in_pcbdetach(inp); 1256 } 1257 #ifdef INET6 1258 else if (in6p) { 1259 in6p->in6p_ppcb = 0; 1260 soisdisconnected(so); 1261 in6_pcbdetach(in6p); 1262 } 1263 #endif 1264 /* 1265 * pcb is no longer visble elsewhere, so we can safely release 1266 * the lock in callout_halt() if needed. 1267 */ 1268 TCP_STATINC(TCP_STAT_CLOSED); 1269 for (j = 0; j < TCPT_NTIMERS; j++) { 1270 callout_halt(&tp->t_timer[j], softnet_lock); 1271 callout_destroy(&tp->t_timer[j]); 1272 } 1273 callout_halt(&tp->t_delack_ch, softnet_lock); 1274 callout_destroy(&tp->t_delack_ch); 1275 pool_put(&tcpcb_pool, tp); 1276 1277 return NULL; 1278 } 1279 1280 int 1281 tcp_freeq(struct tcpcb *tp) 1282 { 1283 struct ipqent *qe; 1284 int rv = 0; 1285 #ifdef TCPREASS_DEBUG 1286 int i = 0; 1287 #endif 1288 1289 TCP_REASS_LOCK_CHECK(tp); 1290 1291 while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) { 1292 #ifdef TCPREASS_DEBUG 1293 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n", 1294 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len, 1295 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST)); 1296 #endif 1297 TAILQ_REMOVE(&tp->segq, qe, ipqe_q); 1298 TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq); 1299 m_freem(qe->ipqe_m); 1300 tcpipqent_free(qe); 1301 rv = 1; 1302 } 1303 tp->t_segqlen = 0; 1304 KASSERT(TAILQ_EMPTY(&tp->timeq)); 1305 return (rv); 1306 } 1307 1308 void 1309 tcp_fasttimo(void) 1310 { 1311 if (tcp_drainwanted) { 1312 tcp_drain(); 1313 tcp_drainwanted = 0; 1314 } 1315 } 1316 1317 void 1318 tcp_drainstub(void) 1319 { 1320 tcp_drainwanted = 1; 1321 } 1322 1323 /* 1324 * Protocol drain routine. Called when memory is in short supply. 1325 * Called from pr_fasttimo thus a callout context. 1326 */ 1327 void 1328 tcp_drain(void) 1329 { 1330 struct inpcb_hdr *inph; 1331 struct tcpcb *tp; 1332 1333 mutex_enter(softnet_lock); 1334 KERNEL_LOCK(1, NULL); 1335 1336 /* 1337 * Free the sequence queue of all TCP connections. 1338 */ 1339 TAILQ_FOREACH(inph, &tcbtable.inpt_queue, inph_queue) { 1340 switch (inph->inph_af) { 1341 case AF_INET: 1342 tp = intotcpcb((struct inpcb *)inph); 1343 break; 1344 #ifdef INET6 1345 case AF_INET6: 1346 tp = in6totcpcb((struct in6pcb *)inph); 1347 break; 1348 #endif 1349 default: 1350 tp = NULL; 1351 break; 1352 } 1353 if (tp != NULL) { 1354 /* 1355 * We may be called from a device's interrupt 1356 * context. If the tcpcb is already busy, 1357 * just bail out now. 1358 */ 1359 if (tcp_reass_lock_try(tp) == 0) 1360 continue; 1361 if (tcp_freeq(tp)) 1362 TCP_STATINC(TCP_STAT_CONNSDRAINED); 1363 TCP_REASS_UNLOCK(tp); 1364 } 1365 } 1366 1367 KERNEL_UNLOCK_ONE(NULL); 1368 mutex_exit(softnet_lock); 1369 } 1370 1371 /* 1372 * Notify a tcp user of an asynchronous error; 1373 * store error as soft error, but wake up user 1374 * (for now, won't do anything until can select for soft error). 1375 */ 1376 void 1377 tcp_notify(struct inpcb *inp, int error) 1378 { 1379 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 1380 struct socket *so = inp->inp_socket; 1381 1382 /* 1383 * Ignore some errors if we are hooked up. 1384 * If connection hasn't completed, has retransmitted several times, 1385 * and receives a second error, give up now. This is better 1386 * than waiting a long time to establish a connection that 1387 * can never complete. 1388 */ 1389 if (tp->t_state == TCPS_ESTABLISHED && 1390 (error == EHOSTUNREACH || error == ENETUNREACH || 1391 error == EHOSTDOWN)) { 1392 return; 1393 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1394 tp->t_rxtshift > 3 && tp->t_softerror) 1395 so->so_error = error; 1396 else 1397 tp->t_softerror = error; 1398 cv_broadcast(&so->so_cv); 1399 sorwakeup(so); 1400 sowwakeup(so); 1401 } 1402 1403 #ifdef INET6 1404 void 1405 tcp6_notify(struct in6pcb *in6p, int error) 1406 { 1407 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb; 1408 struct socket *so = in6p->in6p_socket; 1409 1410 /* 1411 * Ignore some errors if we are hooked up. 1412 * If connection hasn't completed, has retransmitted several times, 1413 * and receives a second error, give up now. This is better 1414 * than waiting a long time to establish a connection that 1415 * can never complete. 1416 */ 1417 if (tp->t_state == TCPS_ESTABLISHED && 1418 (error == EHOSTUNREACH || error == ENETUNREACH || 1419 error == EHOSTDOWN)) { 1420 return; 1421 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1422 tp->t_rxtshift > 3 && tp->t_softerror) 1423 so->so_error = error; 1424 else 1425 tp->t_softerror = error; 1426 cv_broadcast(&so->so_cv); 1427 sorwakeup(so); 1428 sowwakeup(so); 1429 } 1430 #endif 1431 1432 #ifdef INET6 1433 void * 1434 tcp6_ctlinput(int cmd, const struct sockaddr *sa, void *d) 1435 { 1436 struct tcphdr th; 1437 void (*notify)(struct in6pcb *, int) = tcp6_notify; 1438 int nmatch; 1439 struct ip6_hdr *ip6; 1440 const struct sockaddr_in6 *sa6_src = NULL; 1441 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; 1442 struct mbuf *m; 1443 int off; 1444 1445 if (sa->sa_family != AF_INET6 || 1446 sa->sa_len != sizeof(struct sockaddr_in6)) 1447 return NULL; 1448 if ((unsigned)cmd >= PRC_NCMDS) 1449 return NULL; 1450 else if (cmd == PRC_QUENCH) { 1451 /* 1452 * Don't honor ICMP Source Quench messages meant for 1453 * TCP connections. 1454 */ 1455 return NULL; 1456 } else if (PRC_IS_REDIRECT(cmd)) 1457 notify = in6_rtchange, d = NULL; 1458 else if (cmd == PRC_MSGSIZE) 1459 ; /* special code is present, see below */ 1460 else if (cmd == PRC_HOSTDEAD) 1461 d = NULL; 1462 else if (inet6ctlerrmap[cmd] == 0) 1463 return NULL; 1464 1465 /* if the parameter is from icmp6, decode it. */ 1466 if (d != NULL) { 1467 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 1468 m = ip6cp->ip6c_m; 1469 ip6 = ip6cp->ip6c_ip6; 1470 off = ip6cp->ip6c_off; 1471 sa6_src = ip6cp->ip6c_src; 1472 } else { 1473 m = NULL; 1474 ip6 = NULL; 1475 sa6_src = &sa6_any; 1476 off = 0; 1477 } 1478 1479 if (ip6) { 1480 /* 1481 * XXX: We assume that when ip6 is non NULL, 1482 * M and OFF are valid. 1483 */ 1484 1485 /* check if we can safely examine src and dst ports */ 1486 if (m->m_pkthdr.len < off + sizeof(th)) { 1487 if (cmd == PRC_MSGSIZE) 1488 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0); 1489 return NULL; 1490 } 1491 1492 memset(&th, 0, sizeof(th)); 1493 m_copydata(m, off, sizeof(th), (void *)&th); 1494 1495 if (cmd == PRC_MSGSIZE) { 1496 int valid = 0; 1497 1498 /* 1499 * Check to see if we have a valid TCP connection 1500 * corresponding to the address in the ICMPv6 message 1501 * payload. 1502 */ 1503 if (in6_pcblookup_connect(&tcbtable, &sa6->sin6_addr, 1504 th.th_dport, 1505 (const struct in6_addr *)&sa6_src->sin6_addr, 1506 th.th_sport, 0, 0)) 1507 valid++; 1508 1509 /* 1510 * Depending on the value of "valid" and routing table 1511 * size (mtudisc_{hi,lo}wat), we will: 1512 * - recalcurate the new MTU and create the 1513 * corresponding routing entry, or 1514 * - ignore the MTU change notification. 1515 */ 1516 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 1517 1518 /* 1519 * no need to call in6_pcbnotify, it should have been 1520 * called via callback if necessary 1521 */ 1522 return NULL; 1523 } 1524 1525 nmatch = in6_pcbnotify(&tcbtable, sa, th.th_dport, 1526 (const struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify); 1527 if (nmatch == 0 && syn_cache_count && 1528 (inet6ctlerrmap[cmd] == EHOSTUNREACH || 1529 inet6ctlerrmap[cmd] == ENETUNREACH || 1530 inet6ctlerrmap[cmd] == EHOSTDOWN)) 1531 syn_cache_unreach((const struct sockaddr *)sa6_src, 1532 sa, &th); 1533 } else { 1534 (void) in6_pcbnotify(&tcbtable, sa, 0, 1535 (const struct sockaddr *)sa6_src, 0, cmd, NULL, notify); 1536 } 1537 1538 return NULL; 1539 } 1540 #endif 1541 1542 #ifdef INET 1543 /* assumes that ip header and tcp header are contiguous on mbuf */ 1544 void * 1545 tcp_ctlinput(int cmd, const struct sockaddr *sa, void *v) 1546 { 1547 struct ip *ip = v; 1548 struct tcphdr *th; 1549 struct icmp *icp; 1550 extern const int inetctlerrmap[]; 1551 void (*notify)(struct inpcb *, int) = tcp_notify; 1552 int errno; 1553 int nmatch; 1554 struct tcpcb *tp; 1555 u_int mtu; 1556 tcp_seq seq; 1557 struct inpcb *inp; 1558 #ifdef INET6 1559 struct in6pcb *in6p; 1560 struct in6_addr src6, dst6; 1561 #endif 1562 1563 if (sa->sa_family != AF_INET || 1564 sa->sa_len != sizeof(struct sockaddr_in)) 1565 return NULL; 1566 if ((unsigned)cmd >= PRC_NCMDS) 1567 return NULL; 1568 errno = inetctlerrmap[cmd]; 1569 if (cmd == PRC_QUENCH) 1570 /* 1571 * Don't honor ICMP Source Quench messages meant for 1572 * TCP connections. 1573 */ 1574 return NULL; 1575 else if (PRC_IS_REDIRECT(cmd)) 1576 notify = in_rtchange, ip = 0; 1577 else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) { 1578 /* 1579 * Check to see if we have a valid TCP connection 1580 * corresponding to the address in the ICMP message 1581 * payload. 1582 * 1583 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN. 1584 */ 1585 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2)); 1586 #ifdef INET6 1587 in6_in_2_v4mapin6(&ip->ip_src, &src6); 1588 in6_in_2_v4mapin6(&ip->ip_dst, &dst6); 1589 #endif 1590 if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst, 1591 th->th_dport, ip->ip_src, th->th_sport, 0)) != NULL) 1592 #ifdef INET6 1593 in6p = NULL; 1594 #else 1595 ; 1596 #endif 1597 #ifdef INET6 1598 else if ((in6p = in6_pcblookup_connect(&tcbtable, &dst6, 1599 th->th_dport, &src6, th->th_sport, 0, 0)) != NULL) 1600 ; 1601 #endif 1602 else 1603 return NULL; 1604 1605 /* 1606 * Now that we've validated that we are actually communicating 1607 * with the host indicated in the ICMP message, locate the 1608 * ICMP header, recalculate the new MTU, and create the 1609 * corresponding routing entry. 1610 */ 1611 icp = (struct icmp *)((char *)ip - 1612 offsetof(struct icmp, icmp_ip)); 1613 if (inp) { 1614 if ((tp = intotcpcb(inp)) == NULL) 1615 return NULL; 1616 } 1617 #ifdef INET6 1618 else if (in6p) { 1619 if ((tp = in6totcpcb(in6p)) == NULL) 1620 return NULL; 1621 } 1622 #endif 1623 else 1624 return NULL; 1625 seq = ntohl(th->th_seq); 1626 if (SEQ_LT(seq, tp->snd_una) || SEQ_GT(seq, tp->snd_max)) 1627 return NULL; 1628 /* 1629 * If the ICMP message advertises a Next-Hop MTU 1630 * equal or larger than the maximum packet size we have 1631 * ever sent, drop the message. 1632 */ 1633 mtu = (u_int)ntohs(icp->icmp_nextmtu); 1634 if (mtu >= tp->t_pmtud_mtu_sent) 1635 return NULL; 1636 if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) { 1637 /* 1638 * Calculate new MTU, and create corresponding 1639 * route (traditional PMTUD). 1640 */ 1641 tp->t_flags &= ~TF_PMTUD_PEND; 1642 icmp_mtudisc(icp, ip->ip_dst); 1643 } else { 1644 /* 1645 * Record the information got in the ICMP 1646 * message; act on it later. 1647 * If we had already recorded an ICMP message, 1648 * replace the old one only if the new message 1649 * refers to an older TCP segment 1650 */ 1651 if (tp->t_flags & TF_PMTUD_PEND) { 1652 if (SEQ_LT(tp->t_pmtud_th_seq, seq)) 1653 return NULL; 1654 } else 1655 tp->t_flags |= TF_PMTUD_PEND; 1656 tp->t_pmtud_th_seq = seq; 1657 tp->t_pmtud_nextmtu = icp->icmp_nextmtu; 1658 tp->t_pmtud_ip_len = icp->icmp_ip.ip_len; 1659 tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl; 1660 } 1661 return NULL; 1662 } else if (cmd == PRC_HOSTDEAD) 1663 ip = 0; 1664 else if (errno == 0) 1665 return NULL; 1666 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) { 1667 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2)); 1668 nmatch = in_pcbnotify(&tcbtable, satocsin(sa)->sin_addr, 1669 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 1670 if (nmatch == 0 && syn_cache_count && 1671 (inetctlerrmap[cmd] == EHOSTUNREACH || 1672 inetctlerrmap[cmd] == ENETUNREACH || 1673 inetctlerrmap[cmd] == EHOSTDOWN)) { 1674 struct sockaddr_in sin; 1675 memset(&sin, 0, sizeof(sin)); 1676 sin.sin_len = sizeof(sin); 1677 sin.sin_family = AF_INET; 1678 sin.sin_port = th->th_sport; 1679 sin.sin_addr = ip->ip_src; 1680 syn_cache_unreach((struct sockaddr *)&sin, sa, th); 1681 } 1682 1683 /* XXX mapped address case */ 1684 } else 1685 in_pcbnotifyall(&tcbtable, satocsin(sa)->sin_addr, errno, 1686 notify); 1687 return NULL; 1688 } 1689 1690 /* 1691 * When a source quench is received, we are being notified of congestion. 1692 * Close the congestion window down to the Loss Window (one segment). 1693 * We will gradually open it again as we proceed. 1694 */ 1695 void 1696 tcp_quench(struct inpcb *inp, int errno) 1697 { 1698 struct tcpcb *tp = intotcpcb(inp); 1699 1700 if (tp) { 1701 tp->snd_cwnd = tp->t_segsz; 1702 tp->t_bytes_acked = 0; 1703 } 1704 } 1705 #endif 1706 1707 #ifdef INET6 1708 void 1709 tcp6_quench(struct in6pcb *in6p, int errno) 1710 { 1711 struct tcpcb *tp = in6totcpcb(in6p); 1712 1713 if (tp) { 1714 tp->snd_cwnd = tp->t_segsz; 1715 tp->t_bytes_acked = 0; 1716 } 1717 } 1718 #endif 1719 1720 #ifdef INET 1721 /* 1722 * Path MTU Discovery handlers. 1723 */ 1724 void 1725 tcp_mtudisc_callback(struct in_addr faddr) 1726 { 1727 #ifdef INET6 1728 struct in6_addr in6; 1729 #endif 1730 1731 in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc); 1732 #ifdef INET6 1733 in6_in_2_v4mapin6(&faddr, &in6); 1734 tcp6_mtudisc_callback(&in6); 1735 #endif 1736 } 1737 1738 /* 1739 * On receipt of path MTU corrections, flush old route and replace it 1740 * with the new one. Retransmit all unacknowledged packets, to ensure 1741 * that all packets will be received. 1742 */ 1743 void 1744 tcp_mtudisc(struct inpcb *inp, int errno) 1745 { 1746 struct tcpcb *tp = intotcpcb(inp); 1747 struct rtentry *rt; 1748 1749 if (tp == NULL) 1750 return; 1751 1752 rt = in_pcbrtentry(inp); 1753 if (rt != NULL) { 1754 /* 1755 * If this was not a host route, remove and realloc. 1756 */ 1757 if ((rt->rt_flags & RTF_HOST) == 0) { 1758 in_pcbrtentry_unref(rt, inp); 1759 in_rtchange(inp, errno); 1760 if ((rt = in_pcbrtentry(inp)) == NULL) 1761 return; 1762 } 1763 1764 /* 1765 * Slow start out of the error condition. We 1766 * use the MTU because we know it's smaller 1767 * than the previously transmitted segment. 1768 * 1769 * Note: This is more conservative than the 1770 * suggestion in draft-floyd-incr-init-win-03. 1771 */ 1772 if (rt->rt_rmx.rmx_mtu != 0) 1773 tp->snd_cwnd = 1774 TCP_INITIAL_WINDOW(tcp_init_win, 1775 rt->rt_rmx.rmx_mtu); 1776 in_pcbrtentry_unref(rt, inp); 1777 } 1778 1779 /* 1780 * Resend unacknowledged packets. 1781 */ 1782 tp->snd_nxt = tp->sack_newdata = tp->snd_una; 1783 tcp_output(tp); 1784 } 1785 #endif /* INET */ 1786 1787 #ifdef INET6 1788 /* 1789 * Path MTU Discovery handlers. 1790 */ 1791 void 1792 tcp6_mtudisc_callback(struct in6_addr *faddr) 1793 { 1794 struct sockaddr_in6 sin6; 1795 1796 memset(&sin6, 0, sizeof(sin6)); 1797 sin6.sin6_family = AF_INET6; 1798 sin6.sin6_len = sizeof(struct sockaddr_in6); 1799 sin6.sin6_addr = *faddr; 1800 (void) in6_pcbnotify(&tcbtable, (struct sockaddr *)&sin6, 0, 1801 (const struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc); 1802 } 1803 1804 void 1805 tcp6_mtudisc(struct in6pcb *in6p, int errno) 1806 { 1807 struct tcpcb *tp = in6totcpcb(in6p); 1808 struct rtentry *rt; 1809 1810 if (tp == NULL) 1811 return; 1812 1813 rt = in6_pcbrtentry(in6p); 1814 if (rt != NULL) { 1815 /* 1816 * If this was not a host route, remove and realloc. 1817 */ 1818 if ((rt->rt_flags & RTF_HOST) == 0) { 1819 in6_pcbrtentry_unref(rt, in6p); 1820 in6_rtchange(in6p, errno); 1821 rt = in6_pcbrtentry(in6p); 1822 if (rt == NULL) 1823 return; 1824 } 1825 1826 /* 1827 * Slow start out of the error condition. We 1828 * use the MTU because we know it's smaller 1829 * than the previously transmitted segment. 1830 * 1831 * Note: This is more conservative than the 1832 * suggestion in draft-floyd-incr-init-win-03. 1833 */ 1834 if (rt->rt_rmx.rmx_mtu != 0) { 1835 tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, 1836 rt->rt_rmx.rmx_mtu); 1837 } 1838 in6_pcbrtentry_unref(rt, in6p); 1839 } 1840 1841 /* 1842 * Resend unacknowledged packets. 1843 */ 1844 tp->snd_nxt = tp->sack_newdata = tp->snd_una; 1845 tcp_output(tp); 1846 } 1847 #endif /* INET6 */ 1848 1849 /* 1850 * Compute the MSS to advertise to the peer. Called only during 1851 * the 3-way handshake. If we are the server (peer initiated 1852 * connection), we are called with a pointer to the interface 1853 * on which the SYN packet arrived. If we are the client (we 1854 * initiated connection), we are called with a pointer to the 1855 * interface out which this connection should go. 1856 * 1857 * NOTE: Do not subtract IP option/extension header size nor IPsec 1858 * header size from MSS advertisement. MSS option must hold the maximum 1859 * segment size we can accept, so it must always be: 1860 * max(if mtu) - ip header - tcp header 1861 */ 1862 u_long 1863 tcp_mss_to_advertise(const struct ifnet *ifp, int af) 1864 { 1865 extern u_long in_maxmtu; 1866 u_long mss = 0; 1867 u_long hdrsiz; 1868 1869 /* 1870 * In order to avoid defeating path MTU discovery on the peer, 1871 * we advertise the max MTU of all attached networks as our MSS, 1872 * per RFC 1191, section 3.1. 1873 * 1874 * We provide the option to advertise just the MTU of 1875 * the interface on which we hope this connection will 1876 * be receiving. If we are responding to a SYN, we 1877 * will have a pretty good idea about this, but when 1878 * initiating a connection there is a bit more doubt. 1879 * 1880 * We also need to ensure that loopback has a large enough 1881 * MSS, as the loopback MTU is never included in in_maxmtu. 1882 */ 1883 1884 if (ifp != NULL) 1885 switch (af) { 1886 case AF_INET: 1887 mss = ifp->if_mtu; 1888 break; 1889 #ifdef INET6 1890 case AF_INET6: 1891 mss = IN6_LINKMTU(ifp); 1892 break; 1893 #endif 1894 } 1895 1896 if (tcp_mss_ifmtu == 0) 1897 switch (af) { 1898 case AF_INET: 1899 mss = max(in_maxmtu, mss); 1900 break; 1901 #ifdef INET6 1902 case AF_INET6: 1903 mss = max(in6_maxmtu, mss); 1904 break; 1905 #endif 1906 } 1907 1908 switch (af) { 1909 case AF_INET: 1910 hdrsiz = sizeof(struct ip); 1911 break; 1912 #ifdef INET6 1913 case AF_INET6: 1914 hdrsiz = sizeof(struct ip6_hdr); 1915 break; 1916 #endif 1917 default: 1918 hdrsiz = 0; 1919 break; 1920 } 1921 hdrsiz += sizeof(struct tcphdr); 1922 if (mss > hdrsiz) 1923 mss -= hdrsiz; 1924 1925 mss = max(tcp_mssdflt, mss); 1926 return (mss); 1927 } 1928 1929 /* 1930 * Set connection variables based on the peer's advertised MSS. 1931 * We are passed the TCPCB for the actual connection. If we 1932 * are the server, we are called by the compressed state engine 1933 * when the 3-way handshake is complete. If we are the client, 1934 * we are called when we receive the SYN,ACK from the server. 1935 * 1936 * NOTE: Our advertised MSS value must be initialized in the TCPCB 1937 * before this routine is called! 1938 */ 1939 void 1940 tcp_mss_from_peer(struct tcpcb *tp, int offer) 1941 { 1942 struct socket *so; 1943 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1944 struct rtentry *rt; 1945 #endif 1946 u_long bufsize; 1947 int mss; 1948 1949 #ifdef DIAGNOSTIC 1950 if (tp->t_inpcb && tp->t_in6pcb) 1951 panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set"); 1952 #endif 1953 so = NULL; 1954 rt = NULL; 1955 #ifdef INET 1956 if (tp->t_inpcb) { 1957 so = tp->t_inpcb->inp_socket; 1958 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1959 rt = in_pcbrtentry(tp->t_inpcb); 1960 #endif 1961 } 1962 #endif 1963 #ifdef INET6 1964 if (tp->t_in6pcb) { 1965 so = tp->t_in6pcb->in6p_socket; 1966 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1967 rt = in6_pcbrtentry(tp->t_in6pcb); 1968 #endif 1969 } 1970 #endif 1971 1972 /* 1973 * As per RFC1122, use the default MSS value, unless they 1974 * sent us an offer. Do not accept offers less than 256 bytes. 1975 */ 1976 mss = tcp_mssdflt; 1977 if (offer) 1978 mss = offer; 1979 mss = max(mss, 256); /* sanity */ 1980 tp->t_peermss = mss; 1981 mss -= tcp_optlen(tp); 1982 #ifdef INET 1983 if (tp->t_inpcb) 1984 mss -= ip_optlen(tp->t_inpcb); 1985 #endif 1986 #ifdef INET6 1987 if (tp->t_in6pcb) 1988 mss -= ip6_optlen(tp->t_in6pcb); 1989 #endif 1990 1991 /* 1992 * If there's a pipesize, change the socket buffer to that size. 1993 * Make the socket buffer an integral number of MSS units. If 1994 * the MSS is larger than the socket buffer, artificially decrease 1995 * the MSS. 1996 */ 1997 #ifdef RTV_SPIPE 1998 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 1999 bufsize = rt->rt_rmx.rmx_sendpipe; 2000 else 2001 #endif 2002 { 2003 KASSERT(so != NULL); 2004 bufsize = so->so_snd.sb_hiwat; 2005 } 2006 if (bufsize < mss) 2007 mss = bufsize; 2008 else { 2009 bufsize = roundup(bufsize, mss); 2010 if (bufsize > sb_max) 2011 bufsize = sb_max; 2012 (void) sbreserve(&so->so_snd, bufsize, so); 2013 } 2014 tp->t_segsz = mss; 2015 2016 #ifdef RTV_SSTHRESH 2017 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 2018 /* 2019 * There's some sort of gateway or interface buffer 2020 * limit on the path. Use this to set the slow 2021 * start threshold, but set the threshold to no less 2022 * than 2 * MSS. 2023 */ 2024 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 2025 } 2026 #endif 2027 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 2028 #ifdef INET 2029 if (tp->t_inpcb) 2030 in_pcbrtentry_unref(rt, tp->t_inpcb); 2031 #endif 2032 #ifdef INET6 2033 if (tp->t_in6pcb) 2034 in6_pcbrtentry_unref(rt, tp->t_in6pcb); 2035 #endif 2036 #endif 2037 } 2038 2039 /* 2040 * Processing necessary when a TCP connection is established. 2041 */ 2042 void 2043 tcp_established(struct tcpcb *tp) 2044 { 2045 struct socket *so; 2046 #ifdef RTV_RPIPE 2047 struct rtentry *rt; 2048 #endif 2049 u_long bufsize; 2050 2051 #ifdef DIAGNOSTIC 2052 if (tp->t_inpcb && tp->t_in6pcb) 2053 panic("tcp_established: both t_inpcb and t_in6pcb are set"); 2054 #endif 2055 so = NULL; 2056 rt = NULL; 2057 #ifdef INET 2058 /* This is a while() to reduce the dreadful stairstepping below */ 2059 while (tp->t_inpcb) { 2060 so = tp->t_inpcb->inp_socket; 2061 #if defined(RTV_RPIPE) 2062 rt = in_pcbrtentry(tp->t_inpcb); 2063 #endif 2064 if (__predict_true(tcp_msl_enable)) { 2065 if (tp->t_inpcb->inp_laddr.s_addr == INADDR_LOOPBACK) { 2066 tp->t_msl = tcp_msl_loop ? tcp_msl_loop : (TCPTV_MSL >> 2); 2067 break; 2068 } 2069 2070 if (__predict_false(tcp_rttlocal)) { 2071 /* This may be adjusted by tcp_input */ 2072 tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1); 2073 break; 2074 } 2075 if (in_localaddr(tp->t_inpcb->inp_faddr)) { 2076 tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1); 2077 break; 2078 } 2079 } 2080 tp->t_msl = tcp_msl_remote ? tcp_msl_remote : TCPTV_MSL; 2081 break; 2082 } 2083 #endif 2084 #ifdef INET6 2085 /* The !tp->t_inpcb lets the compiler know it can't be v4 *and* v6 */ 2086 while (!tp->t_inpcb && tp->t_in6pcb) { 2087 so = tp->t_in6pcb->in6p_socket; 2088 #if defined(RTV_RPIPE) 2089 rt = in6_pcbrtentry(tp->t_in6pcb); 2090 #endif 2091 if (__predict_true(tcp_msl_enable)) { 2092 extern const struct in6_addr in6addr_loopback; 2093 2094 if (IN6_ARE_ADDR_EQUAL(&tp->t_in6pcb->in6p_laddr, 2095 &in6addr_loopback)) { 2096 tp->t_msl = tcp_msl_loop ? tcp_msl_loop : (TCPTV_MSL >> 2); 2097 break; 2098 } 2099 2100 if (__predict_false(tcp_rttlocal)) { 2101 /* This may be adjusted by tcp_input */ 2102 tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1); 2103 break; 2104 } 2105 if (in6_localaddr(&tp->t_in6pcb->in6p_faddr)) { 2106 tp->t_msl = tcp_msl_local ? tcp_msl_local : (TCPTV_MSL >> 1); 2107 break; 2108 } 2109 } 2110 tp->t_msl = tcp_msl_remote ? tcp_msl_remote : TCPTV_MSL; 2111 break; 2112 } 2113 #endif 2114 2115 tp->t_state = TCPS_ESTABLISHED; 2116 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle); 2117 2118 #ifdef RTV_RPIPE 2119 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 2120 bufsize = rt->rt_rmx.rmx_recvpipe; 2121 else 2122 #endif 2123 { 2124 KASSERT(so != NULL); 2125 bufsize = so->so_rcv.sb_hiwat; 2126 } 2127 if (bufsize > tp->t_ourmss) { 2128 bufsize = roundup(bufsize, tp->t_ourmss); 2129 if (bufsize > sb_max) 2130 bufsize = sb_max; 2131 (void) sbreserve(&so->so_rcv, bufsize, so); 2132 } 2133 #ifdef RTV_RPIPE 2134 #ifdef INET 2135 if (tp->t_inpcb) 2136 in_pcbrtentry_unref(rt, tp->t_inpcb); 2137 #endif 2138 #ifdef INET6 2139 if (tp->t_in6pcb) 2140 in6_pcbrtentry_unref(rt, tp->t_in6pcb); 2141 #endif 2142 #endif 2143 } 2144 2145 /* 2146 * Check if there's an initial rtt or rttvar. Convert from the 2147 * route-table units to scaled multiples of the slow timeout timer. 2148 * Called only during the 3-way handshake. 2149 */ 2150 void 2151 tcp_rmx_rtt(struct tcpcb *tp) 2152 { 2153 #ifdef RTV_RTT 2154 struct rtentry *rt = NULL; 2155 int rtt; 2156 2157 #ifdef DIAGNOSTIC 2158 if (tp->t_inpcb && tp->t_in6pcb) 2159 panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set"); 2160 #endif 2161 #ifdef INET 2162 if (tp->t_inpcb) 2163 rt = in_pcbrtentry(tp->t_inpcb); 2164 #endif 2165 #ifdef INET6 2166 if (tp->t_in6pcb) 2167 rt = in6_pcbrtentry(tp->t_in6pcb); 2168 #endif 2169 if (rt == NULL) 2170 return; 2171 2172 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2173 /* 2174 * XXX The lock bit for MTU indicates that the value 2175 * is also a minimum value; this is subject to time. 2176 */ 2177 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2178 TCPT_RANGESET(tp->t_rttmin, 2179 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 2180 TCPTV_MIN, TCPTV_REXMTMAX); 2181 tp->t_srtt = rtt / 2182 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 2183 if (rt->rt_rmx.rmx_rttvar) { 2184 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2185 ((RTM_RTTUNIT / PR_SLOWHZ) >> 2186 (TCP_RTTVAR_SHIFT + 2)); 2187 } else { 2188 /* Default variation is +- 1 rtt */ 2189 tp->t_rttvar = 2190 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 2191 } 2192 TCPT_RANGESET(tp->t_rxtcur, 2193 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 2194 tp->t_rttmin, TCPTV_REXMTMAX); 2195 } 2196 #ifdef INET 2197 if (tp->t_inpcb) 2198 in_pcbrtentry_unref(rt, tp->t_inpcb); 2199 #endif 2200 #ifdef INET6 2201 if (tp->t_in6pcb) 2202 in6_pcbrtentry_unref(rt, tp->t_in6pcb); 2203 #endif 2204 #endif 2205 } 2206 2207 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 2208 2209 /* 2210 * Get a new sequence value given a tcp control block 2211 */ 2212 tcp_seq 2213 tcp_new_iss(struct tcpcb *tp, tcp_seq addin) 2214 { 2215 2216 #ifdef INET 2217 if (tp->t_inpcb != NULL) { 2218 return (tcp_new_iss1(&tp->t_inpcb->inp_laddr, 2219 &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport, 2220 tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr), 2221 addin)); 2222 } 2223 #endif 2224 #ifdef INET6 2225 if (tp->t_in6pcb != NULL) { 2226 return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr, 2227 &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport, 2228 tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr), 2229 addin)); 2230 } 2231 #endif 2232 /* Not possible. */ 2233 panic("tcp_new_iss"); 2234 } 2235 2236 static u_int8_t tcp_iss_secret[16]; /* 128 bits; should be plenty */ 2237 2238 /* 2239 * Initialize RFC 1948 ISS Secret 2240 */ 2241 static int 2242 tcp_iss_secret_init(void) 2243 { 2244 cprng_strong(kern_cprng, 2245 tcp_iss_secret, sizeof(tcp_iss_secret), 0); 2246 2247 return 0; 2248 } 2249 2250 /* 2251 * This routine actually generates a new TCP initial sequence number. 2252 */ 2253 tcp_seq 2254 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport, 2255 size_t addrsz, tcp_seq addin) 2256 { 2257 tcp_seq tcp_iss; 2258 2259 if (tcp_do_rfc1948) { 2260 MD5_CTX ctx; 2261 u_int8_t hash[16]; /* XXX MD5 knowledge */ 2262 static ONCE_DECL(tcp_iss_secret_control); 2263 2264 /* 2265 * If we haven't been here before, initialize our cryptographic 2266 * hash secret. 2267 */ 2268 RUN_ONCE(&tcp_iss_secret_control, tcp_iss_secret_init); 2269 2270 /* 2271 * Compute the base value of the ISS. It is a hash 2272 * of (saddr, sport, daddr, dport, secret). 2273 */ 2274 MD5Init(&ctx); 2275 2276 MD5Update(&ctx, (u_char *) laddr, addrsz); 2277 MD5Update(&ctx, (u_char *) &lport, sizeof(lport)); 2278 2279 MD5Update(&ctx, (u_char *) faddr, addrsz); 2280 MD5Update(&ctx, (u_char *) &fport, sizeof(fport)); 2281 2282 MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret)); 2283 2284 MD5Final(hash, &ctx); 2285 2286 memcpy(&tcp_iss, hash, sizeof(tcp_iss)); 2287 2288 /* 2289 * Now increment our "timer", and add it in to 2290 * the computed value. 2291 * 2292 * XXX Use `addin'? 2293 * XXX TCP_ISSINCR too large to use? 2294 */ 2295 tcp_iss_seq += TCP_ISSINCR; 2296 #ifdef TCPISS_DEBUG 2297 printf("ISS hash 0x%08x, ", tcp_iss); 2298 #endif 2299 tcp_iss += tcp_iss_seq + addin; 2300 #ifdef TCPISS_DEBUG 2301 printf("new ISS 0x%08x\n", tcp_iss); 2302 #endif 2303 } else { 2304 /* 2305 * Randomize. 2306 */ 2307 tcp_iss = cprng_fast32(); 2308 2309 /* 2310 * If we were asked to add some amount to a known value, 2311 * we will take a random value obtained above, mask off 2312 * the upper bits, and add in the known value. We also 2313 * add in a constant to ensure that we are at least a 2314 * certain distance from the original value. 2315 * 2316 * This is used when an old connection is in timed wait 2317 * and we have a new one coming in, for instance. 2318 */ 2319 if (addin != 0) { 2320 #ifdef TCPISS_DEBUG 2321 printf("Random %08x, ", tcp_iss); 2322 #endif 2323 tcp_iss &= TCP_ISS_RANDOM_MASK; 2324 tcp_iss += addin + TCP_ISSINCR; 2325 #ifdef TCPISS_DEBUG 2326 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 2327 #endif 2328 } else { 2329 tcp_iss &= TCP_ISS_RANDOM_MASK; 2330 tcp_iss += tcp_iss_seq; 2331 tcp_iss_seq += TCP_ISSINCR; 2332 #ifdef TCPISS_DEBUG 2333 printf("ISS %08x\n", tcp_iss); 2334 #endif 2335 } 2336 } 2337 2338 return (tcp_iss); 2339 } 2340 2341 #if defined(IPSEC) 2342 /* compute ESP/AH header size for TCP, including outer IP header. */ 2343 size_t 2344 ipsec4_hdrsiz_tcp(struct tcpcb *tp) 2345 { 2346 struct inpcb *inp; 2347 size_t hdrsiz; 2348 2349 /* XXX mapped addr case (tp->t_in6pcb) */ 2350 if (!tp || !tp->t_template || !(inp = tp->t_inpcb)) 2351 return 0; 2352 switch (tp->t_family) { 2353 case AF_INET: 2354 /* XXX: should use currect direction. */ 2355 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp); 2356 break; 2357 default: 2358 hdrsiz = 0; 2359 break; 2360 } 2361 2362 return hdrsiz; 2363 } 2364 2365 #ifdef INET6 2366 size_t 2367 ipsec6_hdrsiz_tcp(struct tcpcb *tp) 2368 { 2369 struct in6pcb *in6p; 2370 size_t hdrsiz; 2371 2372 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb)) 2373 return 0; 2374 switch (tp->t_family) { 2375 case AF_INET6: 2376 /* XXX: should use currect direction. */ 2377 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p); 2378 break; 2379 case AF_INET: 2380 /* mapped address case - tricky */ 2381 default: 2382 hdrsiz = 0; 2383 break; 2384 } 2385 2386 return hdrsiz; 2387 } 2388 #endif 2389 #endif /*IPSEC*/ 2390 2391 /* 2392 * Determine the length of the TCP options for this connection. 2393 * 2394 * XXX: What do we do for SACK, when we add that? Just reserve 2395 * all of the space? Otherwise we can't exactly be incrementing 2396 * cwnd by an amount that varies depending on the amount we last 2397 * had to SACK! 2398 */ 2399 2400 u_int 2401 tcp_optlen(struct tcpcb *tp) 2402 { 2403 u_int optlen; 2404 2405 optlen = 0; 2406 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 2407 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 2408 optlen += TCPOLEN_TSTAMP_APPA; 2409 2410 #ifdef TCP_SIGNATURE 2411 if (tp->t_flags & TF_SIGNATURE) 2412 optlen += TCPOLEN_SIGLEN; 2413 #endif /* TCP_SIGNATURE */ 2414 2415 return optlen; 2416 } 2417 2418 u_int 2419 tcp_hdrsz(struct tcpcb *tp) 2420 { 2421 u_int hlen; 2422 2423 switch (tp->t_family) { 2424 #ifdef INET6 2425 case AF_INET6: 2426 hlen = sizeof(struct ip6_hdr); 2427 break; 2428 #endif 2429 case AF_INET: 2430 hlen = sizeof(struct ip); 2431 break; 2432 default: 2433 hlen = 0; 2434 break; 2435 } 2436 hlen += sizeof(struct tcphdr); 2437 2438 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2439 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 2440 hlen += TCPOLEN_TSTAMP_APPA; 2441 #ifdef TCP_SIGNATURE 2442 if (tp->t_flags & TF_SIGNATURE) 2443 hlen += TCPOLEN_SIGLEN; 2444 #endif 2445 return hlen; 2446 } 2447 2448 void 2449 tcp_statinc(u_int stat) 2450 { 2451 2452 KASSERT(stat < TCP_NSTATS); 2453 TCP_STATINC(stat); 2454 } 2455 2456 void 2457 tcp_statadd(u_int stat, uint64_t val) 2458 { 2459 2460 KASSERT(stat < TCP_NSTATS); 2461 TCP_STATADD(stat, val); 2462 } 2463