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