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