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