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