1 /* $NetBSD: tcp_subr.c,v 1.229 2008/04/26 08:13:35 yamt 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 * 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.229 2008/04/26 08:13:35 yamt 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_private.h> 155 #include <netinet/tcp_congctl.h> 156 #include <netinet/tcpip.h> 157 158 #ifdef IPSEC 159 #include <netinet6/ipsec.h> 160 #include <netkey/key.h> 161 #endif /*IPSEC*/ 162 163 #ifdef FAST_IPSEC 164 #include <netipsec/ipsec.h> 165 #include <netipsec/xform.h> 166 #ifdef INET6 167 #include <netipsec/ipsec6.h> 168 #endif 169 #include <netipsec/key.h> 170 #endif /* FAST_IPSEC*/ 171 172 173 struct inpcbtable tcbtable; /* head of queue of active tcpcb's */ 174 u_int32_t tcp_now; /* for RFC 1323 timestamps */ 175 176 percpu_t *tcpstat_percpu; 177 178 /* patchable/settable parameters for tcp */ 179 int tcp_mssdflt = TCP_MSS; 180 int tcp_minmss = TCP_MINMSS; 181 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 182 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */ 183 #if NRND > 0 184 int tcp_do_rfc1948 = 0; /* ISS by cryptographic hash */ 185 #endif 186 int tcp_do_sack = 1; /* selective acknowledgement */ 187 int tcp_do_win_scale = 1; /* RFC1323 window scaling */ 188 int tcp_do_timestamps = 1; /* RFC1323 timestamps */ 189 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */ 190 int tcp_do_ecn = 0; /* Explicit Congestion Notification */ 191 #ifndef TCP_INIT_WIN 192 #define TCP_INIT_WIN 0 /* initial slow start window */ 193 #endif 194 #ifndef TCP_INIT_WIN_LOCAL 195 #define TCP_INIT_WIN_LOCAL 4 /* initial slow start window for local nets */ 196 #endif 197 int tcp_init_win = TCP_INIT_WIN; 198 int tcp_init_win_local = TCP_INIT_WIN_LOCAL; 199 int tcp_mss_ifmtu = 0; 200 #ifdef TCP_COMPAT_42 201 int tcp_compat_42 = 1; 202 #else 203 int tcp_compat_42 = 0; 204 #endif 205 int tcp_rst_ppslim = 100; /* 100pps */ 206 int tcp_ackdrop_ppslim = 100; /* 100pps */ 207 int tcp_do_loopback_cksum = 0; 208 int tcp_do_abc = 1; /* RFC3465 Appropriate byte counting. */ 209 int tcp_abc_aggressive = 1; /* 1: L=2*SMSS 0: L=1*SMSS */ 210 int tcp_sack_tp_maxholes = 32; 211 int tcp_sack_globalmaxholes = 1024; 212 int tcp_sack_globalholes = 0; 213 int tcp_ecn_maxretries = 1; 214 215 /* tcb hash */ 216 #ifndef TCBHASHSIZE 217 #define TCBHASHSIZE 128 218 #endif 219 int tcbhashsize = TCBHASHSIZE; 220 221 /* syn hash parameters */ 222 #define TCP_SYN_HASH_SIZE 293 223 #define TCP_SYN_BUCKET_SIZE 35 224 int tcp_syn_cache_size = TCP_SYN_HASH_SIZE; 225 int tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE; 226 int tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE; 227 struct syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE]; 228 229 int tcp_freeq(struct tcpcb *); 230 231 #ifdef INET 232 void tcp_mtudisc_callback(struct in_addr); 233 #endif 234 #ifdef INET6 235 void tcp6_mtudisc_callback(struct in6_addr *); 236 #endif 237 238 #ifdef INET6 239 void tcp6_mtudisc(struct in6pcb *, int); 240 #endif 241 242 POOL_INIT(tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", NULL, 243 IPL_SOFTNET); 244 245 #ifdef TCP_CSUM_COUNTERS 246 #include <sys/device.h> 247 248 #if defined(INET) 249 struct evcnt tcp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 250 NULL, "tcp", "hwcsum bad"); 251 struct evcnt tcp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 252 NULL, "tcp", "hwcsum ok"); 253 struct evcnt tcp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 254 NULL, "tcp", "hwcsum data"); 255 struct evcnt tcp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 256 NULL, "tcp", "swcsum"); 257 258 EVCNT_ATTACH_STATIC(tcp_hwcsum_bad); 259 EVCNT_ATTACH_STATIC(tcp_hwcsum_ok); 260 EVCNT_ATTACH_STATIC(tcp_hwcsum_data); 261 EVCNT_ATTACH_STATIC(tcp_swcsum); 262 #endif /* defined(INET) */ 263 264 #if defined(INET6) 265 struct evcnt tcp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 266 NULL, "tcp6", "hwcsum bad"); 267 struct evcnt tcp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 268 NULL, "tcp6", "hwcsum ok"); 269 struct evcnt tcp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 270 NULL, "tcp6", "hwcsum data"); 271 struct evcnt tcp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 272 NULL, "tcp6", "swcsum"); 273 274 EVCNT_ATTACH_STATIC(tcp6_hwcsum_bad); 275 EVCNT_ATTACH_STATIC(tcp6_hwcsum_ok); 276 EVCNT_ATTACH_STATIC(tcp6_hwcsum_data); 277 EVCNT_ATTACH_STATIC(tcp6_swcsum); 278 #endif /* defined(INET6) */ 279 #endif /* TCP_CSUM_COUNTERS */ 280 281 282 #ifdef TCP_OUTPUT_COUNTERS 283 #include <sys/device.h> 284 285 struct evcnt tcp_output_bigheader = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 286 NULL, "tcp", "output big header"); 287 struct evcnt tcp_output_predict_hit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 288 NULL, "tcp", "output predict hit"); 289 struct evcnt tcp_output_predict_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 290 NULL, "tcp", "output predict miss"); 291 struct evcnt tcp_output_copysmall = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 292 NULL, "tcp", "output copy small"); 293 struct evcnt tcp_output_copybig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 294 NULL, "tcp", "output copy big"); 295 struct evcnt tcp_output_refbig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 296 NULL, "tcp", "output reference big"); 297 298 EVCNT_ATTACH_STATIC(tcp_output_bigheader); 299 EVCNT_ATTACH_STATIC(tcp_output_predict_hit); 300 EVCNT_ATTACH_STATIC(tcp_output_predict_miss); 301 EVCNT_ATTACH_STATIC(tcp_output_copysmall); 302 EVCNT_ATTACH_STATIC(tcp_output_copybig); 303 EVCNT_ATTACH_STATIC(tcp_output_refbig); 304 305 #endif /* TCP_OUTPUT_COUNTERS */ 306 307 #ifdef TCP_REASS_COUNTERS 308 #include <sys/device.h> 309 310 struct evcnt tcp_reass_ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 311 NULL, "tcp_reass", "calls"); 312 struct evcnt tcp_reass_empty = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 313 &tcp_reass_, "tcp_reass", "insert into empty queue"); 314 struct evcnt tcp_reass_iteration[8] = { 315 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", ">7 iterations"), 316 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "1 iteration"), 317 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "2 iterations"), 318 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "3 iterations"), 319 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "4 iterations"), 320 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "5 iterations"), 321 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "6 iterations"), 322 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "7 iterations"), 323 }; 324 struct evcnt tcp_reass_prependfirst = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 325 &tcp_reass_, "tcp_reass", "prepend to first"); 326 struct evcnt tcp_reass_prepend = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 327 &tcp_reass_, "tcp_reass", "prepend"); 328 struct evcnt tcp_reass_insert = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 329 &tcp_reass_, "tcp_reass", "insert"); 330 struct evcnt tcp_reass_inserttail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 331 &tcp_reass_, "tcp_reass", "insert at tail"); 332 struct evcnt tcp_reass_append = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 333 &tcp_reass_, "tcp_reass", "append"); 334 struct evcnt tcp_reass_appendtail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 335 &tcp_reass_, "tcp_reass", "append to tail fragment"); 336 struct evcnt tcp_reass_overlaptail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 337 &tcp_reass_, "tcp_reass", "overlap at end"); 338 struct evcnt tcp_reass_overlapfront = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 339 &tcp_reass_, "tcp_reass", "overlap at start"); 340 struct evcnt tcp_reass_segdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 341 &tcp_reass_, "tcp_reass", "duplicate segment"); 342 struct evcnt tcp_reass_fragdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 343 &tcp_reass_, "tcp_reass", "duplicate fragment"); 344 345 EVCNT_ATTACH_STATIC(tcp_reass_); 346 EVCNT_ATTACH_STATIC(tcp_reass_empty); 347 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 0); 348 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 1); 349 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 2); 350 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 3); 351 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 4); 352 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 5); 353 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 6); 354 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 7); 355 EVCNT_ATTACH_STATIC(tcp_reass_prependfirst); 356 EVCNT_ATTACH_STATIC(tcp_reass_prepend); 357 EVCNT_ATTACH_STATIC(tcp_reass_insert); 358 EVCNT_ATTACH_STATIC(tcp_reass_inserttail); 359 EVCNT_ATTACH_STATIC(tcp_reass_append); 360 EVCNT_ATTACH_STATIC(tcp_reass_appendtail); 361 EVCNT_ATTACH_STATIC(tcp_reass_overlaptail); 362 EVCNT_ATTACH_STATIC(tcp_reass_overlapfront); 363 EVCNT_ATTACH_STATIC(tcp_reass_segdup); 364 EVCNT_ATTACH_STATIC(tcp_reass_fragdup); 365 366 #endif /* TCP_REASS_COUNTERS */ 367 368 #ifdef MBUFTRACE 369 struct mowner tcp_mowner = MOWNER_INIT("tcp", ""); 370 struct mowner tcp_rx_mowner = MOWNER_INIT("tcp", "rx"); 371 struct mowner tcp_tx_mowner = MOWNER_INIT("tcp", "tx"); 372 struct mowner tcp_sock_mowner = MOWNER_INIT("tcp", "sock"); 373 struct mowner tcp_sock_rx_mowner = MOWNER_INIT("tcp", "sock rx"); 374 struct mowner tcp_sock_tx_mowner = MOWNER_INIT("tcp", "sock tx"); 375 #endif 376 377 /* 378 * Tcp initialization 379 */ 380 void 381 tcp_init(void) 382 { 383 int hlen; 384 385 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize); 386 387 hlen = sizeof(struct ip) + sizeof(struct tcphdr); 388 #ifdef INET6 389 if (sizeof(struct ip) < sizeof(struct ip6_hdr)) 390 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 391 #endif 392 if (max_protohdr < hlen) 393 max_protohdr = hlen; 394 if (max_linkhdr + hlen > MHLEN) 395 panic("tcp_init"); 396 397 #ifdef INET 398 icmp_mtudisc_callback_register(tcp_mtudisc_callback); 399 #endif 400 #ifdef INET6 401 icmp6_mtudisc_callback_register(tcp6_mtudisc_callback); 402 #endif 403 404 /* Initialize timer state. */ 405 tcp_timer_init(); 406 407 /* Initialize the compressed state engine. */ 408 syn_cache_init(); 409 410 /* Initialize the congestion control algorithms. */ 411 tcp_congctl_init(); 412 413 /* Initialize the TCPCB template. */ 414 tcp_tcpcb_template(); 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 bzero(mtod(m, void *), 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 bzero(ipov->ih_x1, 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 (bcmp(&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 1223 tcp_congctl_release(tp); 1224 1225 tcp_canceltimers(tp); 1226 TCP_CLEAR_DELACK(tp); 1227 syn_cache_cleanup(tp); 1228 1229 if (tp->t_template) { 1230 m_free(tp->t_template); 1231 tp->t_template = NULL; 1232 } 1233 tp->t_flags |= TF_DEAD; 1234 for (j = 0; j < TCPT_NTIMERS; j++) { 1235 callout_halt(&tp->t_timer[j], softnet_lock); 1236 callout_destroy(&tp->t_timer[j]); 1237 } 1238 callout_halt(&tp->t_delack_ch, softnet_lock); 1239 callout_destroy(&tp->t_delack_ch); 1240 pool_put(&tcpcb_pool, tp); 1241 1242 if (inp) { 1243 inp->inp_ppcb = 0; 1244 soisdisconnected(so); 1245 in_pcbdetach(inp); 1246 } 1247 #ifdef INET6 1248 else if (in6p) { 1249 in6p->in6p_ppcb = 0; 1250 soisdisconnected(so); 1251 in6_pcbdetach(in6p); 1252 } 1253 #endif 1254 TCP_STATINC(TCP_STAT_CLOSED); 1255 return ((struct tcpcb *)0); 1256 } 1257 1258 int 1259 tcp_freeq(struct tcpcb *tp) 1260 { 1261 struct ipqent *qe; 1262 int rv = 0; 1263 #ifdef TCPREASS_DEBUG 1264 int i = 0; 1265 #endif 1266 1267 TCP_REASS_LOCK_CHECK(tp); 1268 1269 while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) { 1270 #ifdef TCPREASS_DEBUG 1271 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n", 1272 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len, 1273 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST)); 1274 #endif 1275 TAILQ_REMOVE(&tp->segq, qe, ipqe_q); 1276 TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq); 1277 m_freem(qe->ipqe_m); 1278 tcpipqent_free(qe); 1279 rv = 1; 1280 } 1281 tp->t_segqlen = 0; 1282 KASSERT(TAILQ_EMPTY(&tp->timeq)); 1283 return (rv); 1284 } 1285 1286 /* 1287 * Protocol drain routine. Called when memory is in short supply. 1288 */ 1289 void 1290 tcp_drain(void) 1291 { 1292 struct inpcb_hdr *inph; 1293 struct tcpcb *tp; 1294 1295 mutex_enter(softnet_lock); 1296 KERNEL_LOCK(1, NULL); 1297 1298 /* 1299 * Free the sequence queue of all TCP connections. 1300 */ 1301 CIRCLEQ_FOREACH(inph, &tcbtable.inpt_queue, inph_queue) { 1302 switch (inph->inph_af) { 1303 case AF_INET: 1304 tp = intotcpcb((struct inpcb *)inph); 1305 break; 1306 #ifdef INET6 1307 case AF_INET6: 1308 tp = in6totcpcb((struct in6pcb *)inph); 1309 break; 1310 #endif 1311 default: 1312 tp = NULL; 1313 break; 1314 } 1315 if (tp != NULL) { 1316 /* 1317 * We may be called from a device's interrupt 1318 * context. If the tcpcb is already busy, 1319 * just bail out now. 1320 */ 1321 if (tcp_reass_lock_try(tp) == 0) 1322 continue; 1323 if (tcp_freeq(tp)) 1324 TCP_STATINC(TCP_STAT_CONNSDRAINED); 1325 TCP_REASS_UNLOCK(tp); 1326 } 1327 } 1328 1329 KERNEL_UNLOCK_ONE(NULL); 1330 mutex_exit(softnet_lock); 1331 } 1332 1333 /* 1334 * Notify a tcp user of an asynchronous error; 1335 * store error as soft error, but wake up user 1336 * (for now, won't do anything until can select for soft error). 1337 */ 1338 void 1339 tcp_notify(struct inpcb *inp, int error) 1340 { 1341 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 1342 struct socket *so = inp->inp_socket; 1343 1344 /* 1345 * Ignore some errors if we are hooked up. 1346 * If connection hasn't completed, has retransmitted several times, 1347 * and receives a second error, give up now. This is better 1348 * than waiting a long time to establish a connection that 1349 * can never complete. 1350 */ 1351 if (tp->t_state == TCPS_ESTABLISHED && 1352 (error == EHOSTUNREACH || error == ENETUNREACH || 1353 error == EHOSTDOWN)) { 1354 return; 1355 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1356 tp->t_rxtshift > 3 && tp->t_softerror) 1357 so->so_error = error; 1358 else 1359 tp->t_softerror = error; 1360 cv_broadcast(&so->so_cv); 1361 sorwakeup(so); 1362 sowwakeup(so); 1363 } 1364 1365 #ifdef INET6 1366 void 1367 tcp6_notify(struct in6pcb *in6p, int error) 1368 { 1369 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb; 1370 struct socket *so = in6p->in6p_socket; 1371 1372 /* 1373 * Ignore some errors if we are hooked up. 1374 * If connection hasn't completed, has retransmitted several times, 1375 * and receives a second error, give up now. This is better 1376 * than waiting a long time to establish a connection that 1377 * can never complete. 1378 */ 1379 if (tp->t_state == TCPS_ESTABLISHED && 1380 (error == EHOSTUNREACH || error == ENETUNREACH || 1381 error == EHOSTDOWN)) { 1382 return; 1383 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1384 tp->t_rxtshift > 3 && tp->t_softerror) 1385 so->so_error = error; 1386 else 1387 tp->t_softerror = error; 1388 cv_broadcast(&so->so_cv); 1389 sorwakeup(so); 1390 sowwakeup(so); 1391 } 1392 #endif 1393 1394 #ifdef INET6 1395 void * 1396 tcp6_ctlinput(int cmd, const struct sockaddr *sa, void *d) 1397 { 1398 struct tcphdr th; 1399 void (*notify)(struct in6pcb *, int) = tcp6_notify; 1400 int nmatch; 1401 struct ip6_hdr *ip6; 1402 const struct sockaddr_in6 *sa6_src = NULL; 1403 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; 1404 struct mbuf *m; 1405 int off; 1406 1407 if (sa->sa_family != AF_INET6 || 1408 sa->sa_len != sizeof(struct sockaddr_in6)) 1409 return NULL; 1410 if ((unsigned)cmd >= PRC_NCMDS) 1411 return NULL; 1412 else if (cmd == PRC_QUENCH) { 1413 /* 1414 * Don't honor ICMP Source Quench messages meant for 1415 * TCP connections. 1416 */ 1417 return NULL; 1418 } else if (PRC_IS_REDIRECT(cmd)) 1419 notify = in6_rtchange, d = NULL; 1420 else if (cmd == PRC_MSGSIZE) 1421 ; /* special code is present, see below */ 1422 else if (cmd == PRC_HOSTDEAD) 1423 d = NULL; 1424 else if (inet6ctlerrmap[cmd] == 0) 1425 return NULL; 1426 1427 /* if the parameter is from icmp6, decode it. */ 1428 if (d != NULL) { 1429 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 1430 m = ip6cp->ip6c_m; 1431 ip6 = ip6cp->ip6c_ip6; 1432 off = ip6cp->ip6c_off; 1433 sa6_src = ip6cp->ip6c_src; 1434 } else { 1435 m = NULL; 1436 ip6 = NULL; 1437 sa6_src = &sa6_any; 1438 off = 0; 1439 } 1440 1441 if (ip6) { 1442 /* 1443 * XXX: We assume that when ip6 is non NULL, 1444 * M and OFF are valid. 1445 */ 1446 1447 /* check if we can safely examine src and dst ports */ 1448 if (m->m_pkthdr.len < off + sizeof(th)) { 1449 if (cmd == PRC_MSGSIZE) 1450 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0); 1451 return NULL; 1452 } 1453 1454 bzero(&th, sizeof(th)); 1455 m_copydata(m, off, sizeof(th), (void *)&th); 1456 1457 if (cmd == PRC_MSGSIZE) { 1458 int valid = 0; 1459 1460 /* 1461 * Check to see if we have a valid TCP connection 1462 * corresponding to the address in the ICMPv6 message 1463 * payload. 1464 */ 1465 if (in6_pcblookup_connect(&tcbtable, &sa6->sin6_addr, 1466 th.th_dport, 1467 (const struct in6_addr *)&sa6_src->sin6_addr, 1468 th.th_sport, 0)) 1469 valid++; 1470 1471 /* 1472 * Depending on the value of "valid" and routing table 1473 * size (mtudisc_{hi,lo}wat), we will: 1474 * - recalcurate the new MTU and create the 1475 * corresponding routing entry, or 1476 * - ignore the MTU change notification. 1477 */ 1478 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 1479 1480 /* 1481 * no need to call in6_pcbnotify, it should have been 1482 * called via callback if necessary 1483 */ 1484 return NULL; 1485 } 1486 1487 nmatch = in6_pcbnotify(&tcbtable, sa, th.th_dport, 1488 (const struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify); 1489 if (nmatch == 0 && syn_cache_count && 1490 (inet6ctlerrmap[cmd] == EHOSTUNREACH || 1491 inet6ctlerrmap[cmd] == ENETUNREACH || 1492 inet6ctlerrmap[cmd] == EHOSTDOWN)) 1493 syn_cache_unreach((const struct sockaddr *)sa6_src, 1494 sa, &th); 1495 } else { 1496 (void) in6_pcbnotify(&tcbtable, sa, 0, 1497 (const struct sockaddr *)sa6_src, 0, cmd, NULL, notify); 1498 } 1499 1500 return NULL; 1501 } 1502 #endif 1503 1504 #ifdef INET 1505 /* assumes that ip header and tcp header are contiguous on mbuf */ 1506 void * 1507 tcp_ctlinput(int cmd, const struct sockaddr *sa, void *v) 1508 { 1509 struct ip *ip = v; 1510 struct tcphdr *th; 1511 struct icmp *icp; 1512 extern const int inetctlerrmap[]; 1513 void (*notify)(struct inpcb *, int) = tcp_notify; 1514 int errno; 1515 int nmatch; 1516 struct tcpcb *tp; 1517 u_int mtu; 1518 tcp_seq seq; 1519 struct inpcb *inp; 1520 #ifdef INET6 1521 struct in6pcb *in6p; 1522 struct in6_addr src6, dst6; 1523 #endif 1524 1525 if (sa->sa_family != AF_INET || 1526 sa->sa_len != sizeof(struct sockaddr_in)) 1527 return NULL; 1528 if ((unsigned)cmd >= PRC_NCMDS) 1529 return NULL; 1530 errno = inetctlerrmap[cmd]; 1531 if (cmd == PRC_QUENCH) 1532 /* 1533 * Don't honor ICMP Source Quench messages meant for 1534 * TCP connections. 1535 */ 1536 return NULL; 1537 else if (PRC_IS_REDIRECT(cmd)) 1538 notify = in_rtchange, ip = 0; 1539 else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) { 1540 /* 1541 * Check to see if we have a valid TCP connection 1542 * corresponding to the address in the ICMP message 1543 * payload. 1544 * 1545 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN. 1546 */ 1547 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2)); 1548 #ifdef INET6 1549 memset(&src6, 0, sizeof(src6)); 1550 memset(&dst6, 0, sizeof(dst6)); 1551 src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff; 1552 memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr)); 1553 memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr)); 1554 #endif 1555 if ((inp = in_pcblookup_connect(&tcbtable, ip->ip_dst, 1556 th->th_dport, ip->ip_src, th->th_sport)) != NULL) 1557 #ifdef INET6 1558 in6p = NULL; 1559 #else 1560 ; 1561 #endif 1562 #ifdef INET6 1563 else if ((in6p = in6_pcblookup_connect(&tcbtable, &dst6, 1564 th->th_dport, &src6, th->th_sport, 0)) != NULL) 1565 ; 1566 #endif 1567 else 1568 return NULL; 1569 1570 /* 1571 * Now that we've validated that we are actually communicating 1572 * with the host indicated in the ICMP message, locate the 1573 * ICMP header, recalculate the new MTU, and create the 1574 * corresponding routing entry. 1575 */ 1576 icp = (struct icmp *)((char *)ip - 1577 offsetof(struct icmp, icmp_ip)); 1578 if (inp) { 1579 if ((tp = intotcpcb(inp)) == NULL) 1580 return NULL; 1581 } 1582 #ifdef INET6 1583 else if (in6p) { 1584 if ((tp = in6totcpcb(in6p)) == NULL) 1585 return NULL; 1586 } 1587 #endif 1588 else 1589 return NULL; 1590 seq = ntohl(th->th_seq); 1591 if (SEQ_LT(seq, tp->snd_una) || SEQ_GT(seq, tp->snd_max)) 1592 return NULL; 1593 /* 1594 * If the ICMP message advertises a Next-Hop MTU 1595 * equal or larger than the maximum packet size we have 1596 * ever sent, drop the message. 1597 */ 1598 mtu = (u_int)ntohs(icp->icmp_nextmtu); 1599 if (mtu >= tp->t_pmtud_mtu_sent) 1600 return NULL; 1601 if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) { 1602 /* 1603 * Calculate new MTU, and create corresponding 1604 * route (traditional PMTUD). 1605 */ 1606 tp->t_flags &= ~TF_PMTUD_PEND; 1607 icmp_mtudisc(icp, ip->ip_dst); 1608 } else { 1609 /* 1610 * Record the information got in the ICMP 1611 * message; act on it later. 1612 * If we had already recorded an ICMP message, 1613 * replace the old one only if the new message 1614 * refers to an older TCP segment 1615 */ 1616 if (tp->t_flags & TF_PMTUD_PEND) { 1617 if (SEQ_LT(tp->t_pmtud_th_seq, seq)) 1618 return NULL; 1619 } else 1620 tp->t_flags |= TF_PMTUD_PEND; 1621 tp->t_pmtud_th_seq = seq; 1622 tp->t_pmtud_nextmtu = icp->icmp_nextmtu; 1623 tp->t_pmtud_ip_len = icp->icmp_ip.ip_len; 1624 tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl; 1625 } 1626 return NULL; 1627 } else if (cmd == PRC_HOSTDEAD) 1628 ip = 0; 1629 else if (errno == 0) 1630 return NULL; 1631 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) { 1632 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2)); 1633 nmatch = in_pcbnotify(&tcbtable, satocsin(sa)->sin_addr, 1634 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 1635 if (nmatch == 0 && syn_cache_count && 1636 (inetctlerrmap[cmd] == EHOSTUNREACH || 1637 inetctlerrmap[cmd] == ENETUNREACH || 1638 inetctlerrmap[cmd] == EHOSTDOWN)) { 1639 struct sockaddr_in sin; 1640 bzero(&sin, sizeof(sin)); 1641 sin.sin_len = sizeof(sin); 1642 sin.sin_family = AF_INET; 1643 sin.sin_port = th->th_sport; 1644 sin.sin_addr = ip->ip_src; 1645 syn_cache_unreach((struct sockaddr *)&sin, sa, th); 1646 } 1647 1648 /* XXX mapped address case */ 1649 } else 1650 in_pcbnotifyall(&tcbtable, satocsin(sa)->sin_addr, errno, 1651 notify); 1652 return NULL; 1653 } 1654 1655 /* 1656 * When a source quench is received, we are being notified of congestion. 1657 * Close the congestion window down to the Loss Window (one segment). 1658 * We will gradually open it again as we proceed. 1659 */ 1660 void 1661 tcp_quench(struct inpcb *inp, int errno) 1662 { 1663 struct tcpcb *tp = intotcpcb(inp); 1664 1665 if (tp) { 1666 tp->snd_cwnd = tp->t_segsz; 1667 tp->t_bytes_acked = 0; 1668 } 1669 } 1670 #endif 1671 1672 #ifdef INET6 1673 void 1674 tcp6_quench(struct in6pcb *in6p, int errno) 1675 { 1676 struct tcpcb *tp = in6totcpcb(in6p); 1677 1678 if (tp) { 1679 tp->snd_cwnd = tp->t_segsz; 1680 tp->t_bytes_acked = 0; 1681 } 1682 } 1683 #endif 1684 1685 #ifdef INET 1686 /* 1687 * Path MTU Discovery handlers. 1688 */ 1689 void 1690 tcp_mtudisc_callback(struct in_addr faddr) 1691 { 1692 #ifdef INET6 1693 struct in6_addr in6; 1694 #endif 1695 1696 in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc); 1697 #ifdef INET6 1698 memset(&in6, 0, sizeof(in6)); 1699 in6.s6_addr16[5] = 0xffff; 1700 memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr)); 1701 tcp6_mtudisc_callback(&in6); 1702 #endif 1703 } 1704 1705 /* 1706 * On receipt of path MTU corrections, flush old route and replace it 1707 * with the new one. Retransmit all unacknowledged packets, to ensure 1708 * that all packets will be received. 1709 */ 1710 void 1711 tcp_mtudisc(struct inpcb *inp, int errno) 1712 { 1713 struct tcpcb *tp = intotcpcb(inp); 1714 struct rtentry *rt = in_pcbrtentry(inp); 1715 1716 if (tp != 0) { 1717 if (rt != 0) { 1718 /* 1719 * If this was not a host route, remove and realloc. 1720 */ 1721 if ((rt->rt_flags & RTF_HOST) == 0) { 1722 in_rtchange(inp, errno); 1723 if ((rt = in_pcbrtentry(inp)) == 0) 1724 return; 1725 } 1726 1727 /* 1728 * Slow start out of the error condition. We 1729 * use the MTU because we know it's smaller 1730 * than the previously transmitted segment. 1731 * 1732 * Note: This is more conservative than the 1733 * suggestion in draft-floyd-incr-init-win-03. 1734 */ 1735 if (rt->rt_rmx.rmx_mtu != 0) 1736 tp->snd_cwnd = 1737 TCP_INITIAL_WINDOW(tcp_init_win, 1738 rt->rt_rmx.rmx_mtu); 1739 } 1740 1741 /* 1742 * Resend unacknowledged packets. 1743 */ 1744 tp->snd_nxt = tp->sack_newdata = tp->snd_una; 1745 tcp_output(tp); 1746 } 1747 } 1748 #endif 1749 1750 #ifdef INET6 1751 /* 1752 * Path MTU Discovery handlers. 1753 */ 1754 void 1755 tcp6_mtudisc_callback(struct in6_addr *faddr) 1756 { 1757 struct sockaddr_in6 sin6; 1758 1759 bzero(&sin6, sizeof(sin6)); 1760 sin6.sin6_family = AF_INET6; 1761 sin6.sin6_len = sizeof(struct sockaddr_in6); 1762 sin6.sin6_addr = *faddr; 1763 (void) in6_pcbnotify(&tcbtable, (struct sockaddr *)&sin6, 0, 1764 (const struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc); 1765 } 1766 1767 void 1768 tcp6_mtudisc(struct in6pcb *in6p, int errno) 1769 { 1770 struct tcpcb *tp = in6totcpcb(in6p); 1771 struct rtentry *rt = in6_pcbrtentry(in6p); 1772 1773 if (tp != 0) { 1774 if (rt != 0) { 1775 /* 1776 * If this was not a host route, remove and realloc. 1777 */ 1778 if ((rt->rt_flags & RTF_HOST) == 0) { 1779 in6_rtchange(in6p, errno); 1780 if ((rt = in6_pcbrtentry(in6p)) == 0) 1781 return; 1782 } 1783 1784 /* 1785 * Slow start out of the error condition. We 1786 * use the MTU because we know it's smaller 1787 * than the previously transmitted segment. 1788 * 1789 * Note: This is more conservative than the 1790 * suggestion in draft-floyd-incr-init-win-03. 1791 */ 1792 if (rt->rt_rmx.rmx_mtu != 0) 1793 tp->snd_cwnd = 1794 TCP_INITIAL_WINDOW(tcp_init_win, 1795 rt->rt_rmx.rmx_mtu); 1796 } 1797 1798 /* 1799 * Resend unacknowledged packets. 1800 */ 1801 tp->snd_nxt = tp->sack_newdata = tp->snd_una; 1802 tcp_output(tp); 1803 } 1804 } 1805 #endif /* INET6 */ 1806 1807 /* 1808 * Compute the MSS to advertise to the peer. Called only during 1809 * the 3-way handshake. If we are the server (peer initiated 1810 * connection), we are called with a pointer to the interface 1811 * on which the SYN packet arrived. If we are the client (we 1812 * initiated connection), we are called with a pointer to the 1813 * interface out which this connection should go. 1814 * 1815 * NOTE: Do not subtract IP option/extension header size nor IPsec 1816 * header size from MSS advertisement. MSS option must hold the maximum 1817 * segment size we can accept, so it must always be: 1818 * max(if mtu) - ip header - tcp header 1819 */ 1820 u_long 1821 tcp_mss_to_advertise(const struct ifnet *ifp, int af) 1822 { 1823 extern u_long in_maxmtu; 1824 u_long mss = 0; 1825 u_long hdrsiz; 1826 1827 /* 1828 * In order to avoid defeating path MTU discovery on the peer, 1829 * we advertise the max MTU of all attached networks as our MSS, 1830 * per RFC 1191, section 3.1. 1831 * 1832 * We provide the option to advertise just the MTU of 1833 * the interface on which we hope this connection will 1834 * be receiving. If we are responding to a SYN, we 1835 * will have a pretty good idea about this, but when 1836 * initiating a connection there is a bit more doubt. 1837 * 1838 * We also need to ensure that loopback has a large enough 1839 * MSS, as the loopback MTU is never included in in_maxmtu. 1840 */ 1841 1842 if (ifp != NULL) 1843 switch (af) { 1844 case AF_INET: 1845 mss = ifp->if_mtu; 1846 break; 1847 #ifdef INET6 1848 case AF_INET6: 1849 mss = IN6_LINKMTU(ifp); 1850 break; 1851 #endif 1852 } 1853 1854 if (tcp_mss_ifmtu == 0) 1855 switch (af) { 1856 case AF_INET: 1857 mss = max(in_maxmtu, mss); 1858 break; 1859 #ifdef INET6 1860 case AF_INET6: 1861 mss = max(in6_maxmtu, mss); 1862 break; 1863 #endif 1864 } 1865 1866 switch (af) { 1867 case AF_INET: 1868 hdrsiz = sizeof(struct ip); 1869 break; 1870 #ifdef INET6 1871 case AF_INET6: 1872 hdrsiz = sizeof(struct ip6_hdr); 1873 break; 1874 #endif 1875 default: 1876 hdrsiz = 0; 1877 break; 1878 } 1879 hdrsiz += sizeof(struct tcphdr); 1880 if (mss > hdrsiz) 1881 mss -= hdrsiz; 1882 1883 mss = max(tcp_mssdflt, mss); 1884 return (mss); 1885 } 1886 1887 /* 1888 * Set connection variables based on the peer's advertised MSS. 1889 * We are passed the TCPCB for the actual connection. If we 1890 * are the server, we are called by the compressed state engine 1891 * when the 3-way handshake is complete. If we are the client, 1892 * we are called when we receive the SYN,ACK from the server. 1893 * 1894 * NOTE: Our advertised MSS value must be initialized in the TCPCB 1895 * before this routine is called! 1896 */ 1897 void 1898 tcp_mss_from_peer(struct tcpcb *tp, int offer) 1899 { 1900 struct socket *so; 1901 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1902 struct rtentry *rt; 1903 #endif 1904 u_long bufsize; 1905 int mss; 1906 1907 #ifdef DIAGNOSTIC 1908 if (tp->t_inpcb && tp->t_in6pcb) 1909 panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set"); 1910 #endif 1911 so = NULL; 1912 rt = NULL; 1913 #ifdef INET 1914 if (tp->t_inpcb) { 1915 so = tp->t_inpcb->inp_socket; 1916 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1917 rt = in_pcbrtentry(tp->t_inpcb); 1918 #endif 1919 } 1920 #endif 1921 #ifdef INET6 1922 if (tp->t_in6pcb) { 1923 so = tp->t_in6pcb->in6p_socket; 1924 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1925 rt = in6_pcbrtentry(tp->t_in6pcb); 1926 #endif 1927 } 1928 #endif 1929 1930 /* 1931 * As per RFC1122, use the default MSS value, unless they 1932 * sent us an offer. Do not accept offers less than 256 bytes. 1933 */ 1934 mss = tcp_mssdflt; 1935 if (offer) 1936 mss = offer; 1937 mss = max(mss, 256); /* sanity */ 1938 tp->t_peermss = mss; 1939 mss -= tcp_optlen(tp); 1940 #ifdef INET 1941 if (tp->t_inpcb) 1942 mss -= ip_optlen(tp->t_inpcb); 1943 #endif 1944 #ifdef INET6 1945 if (tp->t_in6pcb) 1946 mss -= ip6_optlen(tp->t_in6pcb); 1947 #endif 1948 1949 /* 1950 * If there's a pipesize, change the socket buffer to that size. 1951 * Make the socket buffer an integral number of MSS units. If 1952 * the MSS is larger than the socket buffer, artificially decrease 1953 * the MSS. 1954 */ 1955 #ifdef RTV_SPIPE 1956 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 1957 bufsize = rt->rt_rmx.rmx_sendpipe; 1958 else 1959 #endif 1960 { 1961 KASSERT(so != NULL); 1962 bufsize = so->so_snd.sb_hiwat; 1963 } 1964 if (bufsize < mss) 1965 mss = bufsize; 1966 else { 1967 bufsize = roundup(bufsize, mss); 1968 if (bufsize > sb_max) 1969 bufsize = sb_max; 1970 (void) sbreserve(&so->so_snd, bufsize, so); 1971 } 1972 tp->t_segsz = mss; 1973 1974 #ifdef RTV_SSTHRESH 1975 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 1976 /* 1977 * There's some sort of gateway or interface buffer 1978 * limit on the path. Use this to set the slow 1979 * start threshold, but set the threshold to no less 1980 * than 2 * MSS. 1981 */ 1982 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 1983 } 1984 #endif 1985 } 1986 1987 /* 1988 * Processing necessary when a TCP connection is established. 1989 */ 1990 void 1991 tcp_established(struct tcpcb *tp) 1992 { 1993 struct socket *so; 1994 #ifdef RTV_RPIPE 1995 struct rtentry *rt; 1996 #endif 1997 u_long bufsize; 1998 1999 #ifdef DIAGNOSTIC 2000 if (tp->t_inpcb && tp->t_in6pcb) 2001 panic("tcp_established: both t_inpcb and t_in6pcb are set"); 2002 #endif 2003 so = NULL; 2004 rt = NULL; 2005 #ifdef INET 2006 if (tp->t_inpcb) { 2007 so = tp->t_inpcb->inp_socket; 2008 #if defined(RTV_RPIPE) 2009 rt = in_pcbrtentry(tp->t_inpcb); 2010 #endif 2011 } 2012 #endif 2013 #ifdef INET6 2014 if (tp->t_in6pcb) { 2015 so = tp->t_in6pcb->in6p_socket; 2016 #if defined(RTV_RPIPE) 2017 rt = in6_pcbrtentry(tp->t_in6pcb); 2018 #endif 2019 } 2020 #endif 2021 2022 tp->t_state = TCPS_ESTABLISHED; 2023 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle); 2024 2025 #ifdef RTV_RPIPE 2026 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 2027 bufsize = rt->rt_rmx.rmx_recvpipe; 2028 else 2029 #endif 2030 { 2031 KASSERT(so != NULL); 2032 bufsize = so->so_rcv.sb_hiwat; 2033 } 2034 if (bufsize > tp->t_ourmss) { 2035 bufsize = roundup(bufsize, tp->t_ourmss); 2036 if (bufsize > sb_max) 2037 bufsize = sb_max; 2038 (void) sbreserve(&so->so_rcv, bufsize, so); 2039 } 2040 } 2041 2042 /* 2043 * Check if there's an initial rtt or rttvar. Convert from the 2044 * route-table units to scaled multiples of the slow timeout timer. 2045 * Called only during the 3-way handshake. 2046 */ 2047 void 2048 tcp_rmx_rtt(struct tcpcb *tp) 2049 { 2050 #ifdef RTV_RTT 2051 struct rtentry *rt = NULL; 2052 int rtt; 2053 2054 #ifdef DIAGNOSTIC 2055 if (tp->t_inpcb && tp->t_in6pcb) 2056 panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set"); 2057 #endif 2058 #ifdef INET 2059 if (tp->t_inpcb) 2060 rt = in_pcbrtentry(tp->t_inpcb); 2061 #endif 2062 #ifdef INET6 2063 if (tp->t_in6pcb) 2064 rt = in6_pcbrtentry(tp->t_in6pcb); 2065 #endif 2066 if (rt == NULL) 2067 return; 2068 2069 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 2070 /* 2071 * XXX The lock bit for MTU indicates that the value 2072 * is also a minimum value; this is subject to time. 2073 */ 2074 if (rt->rt_rmx.rmx_locks & RTV_RTT) 2075 TCPT_RANGESET(tp->t_rttmin, 2076 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 2077 TCPTV_MIN, TCPTV_REXMTMAX); 2078 tp->t_srtt = rtt / 2079 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 2080 if (rt->rt_rmx.rmx_rttvar) { 2081 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 2082 ((RTM_RTTUNIT / PR_SLOWHZ) >> 2083 (TCP_RTTVAR_SHIFT + 2)); 2084 } else { 2085 /* Default variation is +- 1 rtt */ 2086 tp->t_rttvar = 2087 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 2088 } 2089 TCPT_RANGESET(tp->t_rxtcur, 2090 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 2091 tp->t_rttmin, TCPTV_REXMTMAX); 2092 } 2093 #endif 2094 } 2095 2096 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 2097 #if NRND > 0 2098 u_int8_t tcp_iss_secret[16]; /* 128 bits; should be plenty */ 2099 #endif 2100 2101 /* 2102 * Get a new sequence value given a tcp control block 2103 */ 2104 tcp_seq 2105 tcp_new_iss(struct tcpcb *tp, tcp_seq addin) 2106 { 2107 2108 #ifdef INET 2109 if (tp->t_inpcb != NULL) { 2110 return (tcp_new_iss1(&tp->t_inpcb->inp_laddr, 2111 &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport, 2112 tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr), 2113 addin)); 2114 } 2115 #endif 2116 #ifdef INET6 2117 if (tp->t_in6pcb != NULL) { 2118 return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr, 2119 &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport, 2120 tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr), 2121 addin)); 2122 } 2123 #endif 2124 /* Not possible. */ 2125 panic("tcp_new_iss"); 2126 } 2127 2128 /* 2129 * This routine actually generates a new TCP initial sequence number. 2130 */ 2131 tcp_seq 2132 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport, 2133 size_t addrsz, tcp_seq addin) 2134 { 2135 tcp_seq tcp_iss; 2136 2137 #if NRND > 0 2138 static bool tcp_iss_gotten_secret; 2139 2140 /* 2141 * If we haven't been here before, initialize our cryptographic 2142 * hash secret. 2143 */ 2144 if (tcp_iss_gotten_secret == false) { 2145 rnd_extract_data(tcp_iss_secret, sizeof(tcp_iss_secret), 2146 RND_EXTRACT_ANY); 2147 tcp_iss_gotten_secret = true; 2148 } 2149 2150 if (tcp_do_rfc1948) { 2151 MD5_CTX ctx; 2152 u_int8_t hash[16]; /* XXX MD5 knowledge */ 2153 2154 /* 2155 * Compute the base value of the ISS. It is a hash 2156 * of (saddr, sport, daddr, dport, secret). 2157 */ 2158 MD5Init(&ctx); 2159 2160 MD5Update(&ctx, (u_char *) laddr, addrsz); 2161 MD5Update(&ctx, (u_char *) &lport, sizeof(lport)); 2162 2163 MD5Update(&ctx, (u_char *) faddr, addrsz); 2164 MD5Update(&ctx, (u_char *) &fport, sizeof(fport)); 2165 2166 MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret)); 2167 2168 MD5Final(hash, &ctx); 2169 2170 memcpy(&tcp_iss, hash, sizeof(tcp_iss)); 2171 2172 /* 2173 * Now increment our "timer", and add it in to 2174 * the computed value. 2175 * 2176 * XXX Use `addin'? 2177 * XXX TCP_ISSINCR too large to use? 2178 */ 2179 tcp_iss_seq += TCP_ISSINCR; 2180 #ifdef TCPISS_DEBUG 2181 printf("ISS hash 0x%08x, ", tcp_iss); 2182 #endif 2183 tcp_iss += tcp_iss_seq + addin; 2184 #ifdef TCPISS_DEBUG 2185 printf("new ISS 0x%08x\n", tcp_iss); 2186 #endif 2187 } else 2188 #endif /* NRND > 0 */ 2189 { 2190 /* 2191 * Randomize. 2192 */ 2193 #if NRND > 0 2194 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY); 2195 #else 2196 tcp_iss = arc4random(); 2197 #endif 2198 2199 /* 2200 * If we were asked to add some amount to a known value, 2201 * we will take a random value obtained above, mask off 2202 * the upper bits, and add in the known value. We also 2203 * add in a constant to ensure that we are at least a 2204 * certain distance from the original value. 2205 * 2206 * This is used when an old connection is in timed wait 2207 * and we have a new one coming in, for instance. 2208 */ 2209 if (addin != 0) { 2210 #ifdef TCPISS_DEBUG 2211 printf("Random %08x, ", tcp_iss); 2212 #endif 2213 tcp_iss &= TCP_ISS_RANDOM_MASK; 2214 tcp_iss += addin + TCP_ISSINCR; 2215 #ifdef TCPISS_DEBUG 2216 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 2217 #endif 2218 } else { 2219 tcp_iss &= TCP_ISS_RANDOM_MASK; 2220 tcp_iss += tcp_iss_seq; 2221 tcp_iss_seq += TCP_ISSINCR; 2222 #ifdef TCPISS_DEBUG 2223 printf("ISS %08x\n", tcp_iss); 2224 #endif 2225 } 2226 } 2227 2228 if (tcp_compat_42) { 2229 /* 2230 * Limit it to the positive range for really old TCP 2231 * implementations. 2232 * Just AND off the top bit instead of checking if 2233 * is set first - saves a branch 50% of the time. 2234 */ 2235 tcp_iss &= 0x7fffffff; /* XXX */ 2236 } 2237 2238 return (tcp_iss); 2239 } 2240 2241 #if defined(IPSEC) || defined(FAST_IPSEC) 2242 /* compute ESP/AH header size for TCP, including outer IP header. */ 2243 size_t 2244 ipsec4_hdrsiz_tcp(struct tcpcb *tp) 2245 { 2246 struct inpcb *inp; 2247 size_t hdrsiz; 2248 2249 /* XXX mapped addr case (tp->t_in6pcb) */ 2250 if (!tp || !tp->t_template || !(inp = tp->t_inpcb)) 2251 return 0; 2252 switch (tp->t_family) { 2253 case AF_INET: 2254 /* XXX: should use currect direction. */ 2255 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp); 2256 break; 2257 default: 2258 hdrsiz = 0; 2259 break; 2260 } 2261 2262 return hdrsiz; 2263 } 2264 2265 #ifdef INET6 2266 size_t 2267 ipsec6_hdrsiz_tcp(struct tcpcb *tp) 2268 { 2269 struct in6pcb *in6p; 2270 size_t hdrsiz; 2271 2272 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb)) 2273 return 0; 2274 switch (tp->t_family) { 2275 case AF_INET6: 2276 /* XXX: should use currect direction. */ 2277 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p); 2278 break; 2279 case AF_INET: 2280 /* mapped address case - tricky */ 2281 default: 2282 hdrsiz = 0; 2283 break; 2284 } 2285 2286 return hdrsiz; 2287 } 2288 #endif 2289 #endif /*IPSEC*/ 2290 2291 /* 2292 * Determine the length of the TCP options for this connection. 2293 * 2294 * XXX: What do we do for SACK, when we add that? Just reserve 2295 * all of the space? Otherwise we can't exactly be incrementing 2296 * cwnd by an amount that varies depending on the amount we last 2297 * had to SACK! 2298 */ 2299 2300 u_int 2301 tcp_optlen(struct tcpcb *tp) 2302 { 2303 u_int optlen; 2304 2305 optlen = 0; 2306 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 2307 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 2308 optlen += TCPOLEN_TSTAMP_APPA; 2309 2310 #ifdef TCP_SIGNATURE 2311 if (tp->t_flags & TF_SIGNATURE) 2312 optlen += TCPOLEN_SIGNATURE + 2; 2313 #endif /* TCP_SIGNATURE */ 2314 2315 return optlen; 2316 } 2317 2318 u_int 2319 tcp_hdrsz(struct tcpcb *tp) 2320 { 2321 u_int hlen; 2322 2323 switch (tp->t_family) { 2324 #ifdef INET6 2325 case AF_INET6: 2326 hlen = sizeof(struct ip6_hdr); 2327 break; 2328 #endif 2329 case AF_INET: 2330 hlen = sizeof(struct ip); 2331 break; 2332 default: 2333 hlen = 0; 2334 break; 2335 } 2336 hlen += sizeof(struct tcphdr); 2337 2338 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2339 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 2340 hlen += TCPOLEN_TSTAMP_APPA; 2341 #ifdef TCP_SIGNATURE 2342 if (tp->t_flags & TF_SIGNATURE) 2343 hlen += TCPOLEN_SIGLEN; 2344 #endif 2345 return hlen; 2346 } 2347 2348 void 2349 tcp_statinc(u_int stat) 2350 { 2351 2352 KASSERT(stat < TCP_NSTATS); 2353 TCP_STATINC(stat); 2354 } 2355 2356 void 2357 tcp_statadd(u_int stat, uint64_t val) 2358 { 2359 2360 KASSERT(stat < TCP_NSTATS); 2361 TCP_STATADD(stat, val); 2362 } 2363