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