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