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