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