1 /* $NetBSD: tcp_subr.c,v 1.122 2001/11/13 00:32:41 lukem 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.122 2001/11/13 00:32:41 lukem 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 0, NULL, NULL, M_PCB); 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 /* 1109 * Notify a tcp user of an asynchronous error; 1110 * store error as soft error, but wake up user 1111 * (for now, won't do anything until can select for soft error). 1112 */ 1113 void 1114 tcp_notify(inp, error) 1115 struct inpcb *inp; 1116 int error; 1117 { 1118 struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 1119 struct socket *so = inp->inp_socket; 1120 1121 /* 1122 * Ignore some errors if we are hooked up. 1123 * If connection hasn't completed, has retransmitted several times, 1124 * and receives a second error, give up now. This is better 1125 * than waiting a long time to establish a connection that 1126 * can never complete. 1127 */ 1128 if (tp->t_state == TCPS_ESTABLISHED && 1129 (error == EHOSTUNREACH || error == ENETUNREACH || 1130 error == EHOSTDOWN)) { 1131 return; 1132 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1133 tp->t_rxtshift > 3 && tp->t_softerror) 1134 so->so_error = error; 1135 else 1136 tp->t_softerror = error; 1137 wakeup((caddr_t) &so->so_timeo); 1138 sorwakeup(so); 1139 sowwakeup(so); 1140 } 1141 1142 #ifdef INET6 1143 void 1144 tcp6_notify(in6p, error) 1145 struct in6pcb *in6p; 1146 int error; 1147 { 1148 struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb; 1149 struct socket *so = in6p->in6p_socket; 1150 1151 /* 1152 * Ignore some errors if we are hooked up. 1153 * If connection hasn't completed, has retransmitted several times, 1154 * and receives a second error, give up now. This is better 1155 * than waiting a long time to establish a connection that 1156 * can never complete. 1157 */ 1158 if (tp->t_state == TCPS_ESTABLISHED && 1159 (error == EHOSTUNREACH || error == ENETUNREACH || 1160 error == EHOSTDOWN)) { 1161 return; 1162 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1163 tp->t_rxtshift > 3 && tp->t_softerror) 1164 so->so_error = error; 1165 else 1166 tp->t_softerror = error; 1167 wakeup((caddr_t) &so->so_timeo); 1168 sorwakeup(so); 1169 sowwakeup(so); 1170 } 1171 #endif 1172 1173 #ifdef INET6 1174 void 1175 tcp6_ctlinput(cmd, sa, d) 1176 int cmd; 1177 struct sockaddr *sa; 1178 void *d; 1179 { 1180 struct tcphdr th; 1181 void (*notify) __P((struct in6pcb *, int)) = tcp6_notify; 1182 int nmatch; 1183 struct ip6_hdr *ip6; 1184 const struct sockaddr_in6 *sa6_src = NULL; 1185 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 1186 struct mbuf *m; 1187 int off; 1188 1189 if (sa->sa_family != AF_INET6 || 1190 sa->sa_len != sizeof(struct sockaddr_in6)) 1191 return; 1192 if ((unsigned)cmd >= PRC_NCMDS) 1193 return; 1194 else if (cmd == PRC_QUENCH) { 1195 /* XXX there's no PRC_QUENCH in IPv6 */ 1196 notify = tcp6_quench; 1197 } else if (PRC_IS_REDIRECT(cmd)) 1198 notify = in6_rtchange, d = NULL; 1199 else if (cmd == PRC_MSGSIZE) 1200 ; /* special code is present, see below */ 1201 else if (cmd == PRC_HOSTDEAD) 1202 d = NULL; 1203 else if (inet6ctlerrmap[cmd] == 0) 1204 return; 1205 1206 /* if the parameter is from icmp6, decode it. */ 1207 if (d != NULL) { 1208 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 1209 m = ip6cp->ip6c_m; 1210 ip6 = ip6cp->ip6c_ip6; 1211 off = ip6cp->ip6c_off; 1212 sa6_src = ip6cp->ip6c_src; 1213 } else { 1214 m = NULL; 1215 ip6 = NULL; 1216 sa6_src = &sa6_any; 1217 } 1218 1219 if (ip6) { 1220 /* 1221 * XXX: We assume that when ip6 is non NULL, 1222 * M and OFF are valid. 1223 */ 1224 1225 /* check if we can safely examine src and dst ports */ 1226 if (m->m_pkthdr.len < off + sizeof(th)) { 1227 if (cmd == PRC_MSGSIZE) 1228 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0); 1229 return; 1230 } 1231 1232 bzero(&th, sizeof(th)); 1233 m_copydata(m, off, sizeof(th), (caddr_t)&th); 1234 1235 if (cmd == PRC_MSGSIZE) { 1236 int valid = 0; 1237 1238 /* 1239 * Check to see if we have a valid TCP connection 1240 * corresponding to the address in the ICMPv6 message 1241 * payload. 1242 */ 1243 if (in6_pcblookup_connect(&tcb6, &sa6->sin6_addr, 1244 th.th_dport, (struct in6_addr *)&sa6_src->sin6_addr, 1245 th.th_sport, 0)) 1246 valid++; 1247 1248 /* 1249 * Depending on the value of "valid" and routing table 1250 * size (mtudisc_{hi,lo}wat), we will: 1251 * - recalcurate the new MTU and create the 1252 * corresponding routing entry, or 1253 * - ignore the MTU change notification. 1254 */ 1255 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 1256 1257 /* 1258 * no need to call in6_pcbnotify, it should have been 1259 * called via callback if necessary 1260 */ 1261 return; 1262 } 1263 1264 nmatch = in6_pcbnotify(&tcb6, sa, th.th_dport, 1265 (struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify); 1266 if (nmatch == 0 && syn_cache_count && 1267 (inet6ctlerrmap[cmd] == EHOSTUNREACH || 1268 inet6ctlerrmap[cmd] == ENETUNREACH || 1269 inet6ctlerrmap[cmd] == EHOSTDOWN)) 1270 syn_cache_unreach((struct sockaddr *)sa6_src, 1271 sa, &th); 1272 } else { 1273 (void) in6_pcbnotify(&tcb6, sa, 0, (struct sockaddr *)sa6_src, 1274 0, cmd, NULL, notify); 1275 } 1276 } 1277 #endif 1278 1279 #ifdef INET 1280 /* assumes that ip header and tcp header are contiguous on mbuf */ 1281 void * 1282 tcp_ctlinput(cmd, sa, v) 1283 int cmd; 1284 struct sockaddr *sa; 1285 void *v; 1286 { 1287 struct ip *ip = v; 1288 struct tcphdr *th; 1289 struct icmp *icp; 1290 extern const int inetctlerrmap[]; 1291 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 1292 int errno; 1293 int nmatch; 1294 1295 if (sa->sa_family != AF_INET || 1296 sa->sa_len != sizeof(struct sockaddr_in)) 1297 return NULL; 1298 if ((unsigned)cmd >= PRC_NCMDS) 1299 return NULL; 1300 errno = inetctlerrmap[cmd]; 1301 if (cmd == PRC_QUENCH) 1302 notify = tcp_quench; 1303 else if (PRC_IS_REDIRECT(cmd)) 1304 notify = in_rtchange, ip = 0; 1305 else if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) { 1306 /* 1307 * Check to see if we have a valid TCP connection 1308 * corresponding to the address in the ICMP message 1309 * payload. 1310 * 1311 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN. 1312 */ 1313 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 1314 if (in_pcblookup_connect(&tcbtable, 1315 ip->ip_dst, th->th_dport, 1316 ip->ip_src, th->th_sport) == NULL) 1317 return NULL; 1318 1319 /* 1320 * Now that we've validated that we are actually communicating 1321 * with the host indicated in the ICMP message, locate the 1322 * ICMP header, recalculate the new MTU, and create the 1323 * corresponding routing entry. 1324 */ 1325 icp = (struct icmp *)((caddr_t)ip - 1326 offsetof(struct icmp, icmp_ip)); 1327 icmp_mtudisc(icp, ip->ip_dst); 1328 1329 return NULL; 1330 } else if (cmd == PRC_HOSTDEAD) 1331 ip = 0; 1332 else if (errno == 0) 1333 return NULL; 1334 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) { 1335 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 1336 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr, 1337 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 1338 if (nmatch == 0 && syn_cache_count && 1339 (inetctlerrmap[cmd] == EHOSTUNREACH || 1340 inetctlerrmap[cmd] == ENETUNREACH || 1341 inetctlerrmap[cmd] == EHOSTDOWN)) { 1342 struct sockaddr_in sin; 1343 bzero(&sin, sizeof(sin)); 1344 sin.sin_len = sizeof(sin); 1345 sin.sin_family = AF_INET; 1346 sin.sin_port = th->th_sport; 1347 sin.sin_addr = ip->ip_src; 1348 syn_cache_unreach((struct sockaddr *)&sin, sa, th); 1349 } 1350 1351 /* XXX mapped address case */ 1352 } else 1353 in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno, 1354 notify); 1355 return NULL; 1356 } 1357 1358 /* 1359 * When a source quence is received, we are being notifed of congestion. 1360 * Close the congestion window down to the Loss Window (one segment). 1361 * We will gradually open it again as we proceed. 1362 */ 1363 void 1364 tcp_quench(inp, errno) 1365 struct inpcb *inp; 1366 int errno; 1367 { 1368 struct tcpcb *tp = intotcpcb(inp); 1369 1370 if (tp) 1371 tp->snd_cwnd = tp->t_segsz; 1372 } 1373 #endif 1374 1375 #ifdef INET6 1376 void 1377 tcp6_quench(in6p, errno) 1378 struct in6pcb *in6p; 1379 int errno; 1380 { 1381 struct tcpcb *tp = in6totcpcb(in6p); 1382 1383 if (tp) 1384 tp->snd_cwnd = tp->t_segsz; 1385 } 1386 #endif 1387 1388 #ifdef INET 1389 /* 1390 * Path MTU Discovery handlers. 1391 */ 1392 void 1393 tcp_mtudisc_callback(faddr) 1394 struct in_addr faddr; 1395 { 1396 1397 in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc); 1398 } 1399 1400 /* 1401 * On receipt of path MTU corrections, flush old route and replace it 1402 * with the new one. Retransmit all unacknowledged packets, to ensure 1403 * that all packets will be received. 1404 */ 1405 void 1406 tcp_mtudisc(inp, errno) 1407 struct inpcb *inp; 1408 int errno; 1409 { 1410 struct tcpcb *tp = intotcpcb(inp); 1411 struct rtentry *rt = in_pcbrtentry(inp); 1412 1413 if (tp != 0) { 1414 if (rt != 0) { 1415 /* 1416 * If this was not a host route, remove and realloc. 1417 */ 1418 if ((rt->rt_flags & RTF_HOST) == 0) { 1419 in_rtchange(inp, errno); 1420 if ((rt = in_pcbrtentry(inp)) == 0) 1421 return; 1422 } 1423 1424 /* 1425 * Slow start out of the error condition. We 1426 * use the MTU because we know it's smaller 1427 * than the previously transmitted segment. 1428 * 1429 * Note: This is more conservative than the 1430 * suggestion in draft-floyd-incr-init-win-03. 1431 */ 1432 if (rt->rt_rmx.rmx_mtu != 0) 1433 tp->snd_cwnd = 1434 TCP_INITIAL_WINDOW(tcp_init_win, 1435 rt->rt_rmx.rmx_mtu); 1436 } 1437 1438 /* 1439 * Resend unacknowledged packets. 1440 */ 1441 tp->snd_nxt = tp->snd_una; 1442 tcp_output(tp); 1443 } 1444 } 1445 #endif 1446 1447 #ifdef INET6 1448 /* 1449 * Path MTU Discovery handlers. 1450 */ 1451 void 1452 tcp6_mtudisc_callback(faddr) 1453 struct in6_addr *faddr; 1454 { 1455 struct sockaddr_in6 sin6; 1456 1457 bzero(&sin6, sizeof(sin6)); 1458 sin6.sin6_family = AF_INET6; 1459 sin6.sin6_len = sizeof(struct sockaddr_in6); 1460 sin6.sin6_addr = *faddr; 1461 (void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sin6, 0, 1462 (struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc); 1463 } 1464 1465 void 1466 tcp6_mtudisc(in6p, errno) 1467 struct in6pcb *in6p; 1468 int errno; 1469 { 1470 struct tcpcb *tp = in6totcpcb(in6p); 1471 struct rtentry *rt = in6_pcbrtentry(in6p); 1472 1473 if (tp != 0) { 1474 if (rt != 0) { 1475 /* 1476 * If this was not a host route, remove and realloc. 1477 */ 1478 if ((rt->rt_flags & RTF_HOST) == 0) { 1479 in6_rtchange(in6p, errno); 1480 if ((rt = in6_pcbrtentry(in6p)) == 0) 1481 return; 1482 } 1483 1484 /* 1485 * Slow start out of the error condition. We 1486 * use the MTU because we know it's smaller 1487 * than the previously transmitted segment. 1488 * 1489 * Note: This is more conservative than the 1490 * suggestion in draft-floyd-incr-init-win-03. 1491 */ 1492 if (rt->rt_rmx.rmx_mtu != 0) 1493 tp->snd_cwnd = 1494 TCP_INITIAL_WINDOW(tcp_init_win, 1495 rt->rt_rmx.rmx_mtu); 1496 } 1497 1498 /* 1499 * Resend unacknowledged packets. 1500 */ 1501 tp->snd_nxt = tp->snd_una; 1502 tcp_output(tp); 1503 } 1504 } 1505 #endif /* INET6 */ 1506 1507 /* 1508 * Compute the MSS to advertise to the peer. Called only during 1509 * the 3-way handshake. If we are the server (peer initiated 1510 * connection), we are called with a pointer to the interface 1511 * on which the SYN packet arrived. If we are the client (we 1512 * initiated connection), we are called with a pointer to the 1513 * interface out which this connection should go. 1514 * 1515 * NOTE: Do not subtract IP option/extension header size nor IPsec 1516 * header size from MSS advertisement. MSS option must hold the maximum 1517 * segment size we can accept, so it must always be: 1518 * max(if mtu) - ip header - tcp header 1519 */ 1520 u_long 1521 tcp_mss_to_advertise(ifp, af) 1522 const struct ifnet *ifp; 1523 int af; 1524 { 1525 extern u_long in_maxmtu; 1526 u_long mss = 0; 1527 u_long hdrsiz; 1528 1529 /* 1530 * In order to avoid defeating path MTU discovery on the peer, 1531 * we advertise the max MTU of all attached networks as our MSS, 1532 * per RFC 1191, section 3.1. 1533 * 1534 * We provide the option to advertise just the MTU of 1535 * the interface on which we hope this connection will 1536 * be receiving. If we are responding to a SYN, we 1537 * will have a pretty good idea about this, but when 1538 * initiating a connection there is a bit more doubt. 1539 * 1540 * We also need to ensure that loopback has a large enough 1541 * MSS, as the loopback MTU is never included in in_maxmtu. 1542 */ 1543 1544 if (ifp != NULL) 1545 mss = ifp->if_mtu; 1546 1547 if (tcp_mss_ifmtu == 0) 1548 switch (af) { 1549 case AF_INET: 1550 mss = max(in_maxmtu, mss); 1551 break; 1552 #ifdef INET6 1553 case AF_INET6: 1554 mss = max(in6_maxmtu, mss); 1555 break; 1556 #endif 1557 } 1558 1559 switch (af) { 1560 case AF_INET: 1561 hdrsiz = sizeof(struct ip); 1562 break; 1563 #ifdef INET6 1564 case AF_INET6: 1565 hdrsiz = sizeof(struct ip6_hdr); 1566 break; 1567 #endif 1568 default: 1569 hdrsiz = 0; 1570 break; 1571 } 1572 hdrsiz += sizeof(struct tcphdr); 1573 if (mss > hdrsiz) 1574 mss -= hdrsiz; 1575 1576 mss = max(tcp_mssdflt, mss); 1577 return (mss); 1578 } 1579 1580 /* 1581 * Set connection variables based on the peer's advertised MSS. 1582 * We are passed the TCPCB for the actual connection. If we 1583 * are the server, we are called by the compressed state engine 1584 * when the 3-way handshake is complete. If we are the client, 1585 * we are called when we receive the SYN,ACK from the server. 1586 * 1587 * NOTE: Our advertised MSS value must be initialized in the TCPCB 1588 * before this routine is called! 1589 */ 1590 void 1591 tcp_mss_from_peer(tp, offer) 1592 struct tcpcb *tp; 1593 int offer; 1594 { 1595 struct socket *so; 1596 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1597 struct rtentry *rt; 1598 #endif 1599 u_long bufsize; 1600 int mss; 1601 1602 #ifdef DIAGNOSTIC 1603 if (tp->t_inpcb && tp->t_in6pcb) 1604 panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set"); 1605 #endif 1606 so = NULL; 1607 rt = NULL; 1608 #ifdef INET 1609 if (tp->t_inpcb) { 1610 so = tp->t_inpcb->inp_socket; 1611 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1612 rt = in_pcbrtentry(tp->t_inpcb); 1613 #endif 1614 } 1615 #endif 1616 #ifdef INET6 1617 if (tp->t_in6pcb) { 1618 so = tp->t_in6pcb->in6p_socket; 1619 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1620 rt = in6_pcbrtentry(tp->t_in6pcb); 1621 #endif 1622 } 1623 #endif 1624 1625 /* 1626 * As per RFC1122, use the default MSS value, unless they 1627 * sent us an offer. Do not accept offers less than 32 bytes. 1628 */ 1629 mss = tcp_mssdflt; 1630 if (offer) 1631 mss = offer; 1632 mss = max(mss, 32); /* sanity */ 1633 tp->t_peermss = mss; 1634 mss -= tcp_optlen(tp); 1635 #ifdef INET 1636 if (tp->t_inpcb) 1637 mss -= ip_optlen(tp->t_inpcb); 1638 #endif 1639 #ifdef INET6 1640 if (tp->t_in6pcb) 1641 mss -= ip6_optlen(tp->t_in6pcb); 1642 #endif 1643 1644 /* 1645 * If there's a pipesize, change the socket buffer to that size. 1646 * Make the socket buffer an integral number of MSS units. If 1647 * the MSS is larger than the socket buffer, artificially decrease 1648 * the MSS. 1649 */ 1650 #ifdef RTV_SPIPE 1651 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 1652 bufsize = rt->rt_rmx.rmx_sendpipe; 1653 else 1654 #endif 1655 bufsize = so->so_snd.sb_hiwat; 1656 if (bufsize < mss) 1657 mss = bufsize; 1658 else { 1659 bufsize = roundup(bufsize, mss); 1660 if (bufsize > sb_max) 1661 bufsize = sb_max; 1662 (void) sbreserve(&so->so_snd, bufsize); 1663 } 1664 tp->t_segsz = mss; 1665 1666 #ifdef RTV_SSTHRESH 1667 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 1668 /* 1669 * There's some sort of gateway or interface buffer 1670 * limit on the path. Use this to set the slow 1671 * start threshold, but set the threshold to no less 1672 * than 2 * MSS. 1673 */ 1674 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 1675 } 1676 #endif 1677 } 1678 1679 /* 1680 * Processing necessary when a TCP connection is established. 1681 */ 1682 void 1683 tcp_established(tp) 1684 struct tcpcb *tp; 1685 { 1686 struct socket *so; 1687 #ifdef RTV_RPIPE 1688 struct rtentry *rt; 1689 #endif 1690 u_long bufsize; 1691 1692 #ifdef DIAGNOSTIC 1693 if (tp->t_inpcb && tp->t_in6pcb) 1694 panic("tcp_established: both t_inpcb and t_in6pcb are set"); 1695 #endif 1696 so = NULL; 1697 rt = NULL; 1698 #ifdef INET 1699 if (tp->t_inpcb) { 1700 so = tp->t_inpcb->inp_socket; 1701 #if defined(RTV_RPIPE) 1702 rt = in_pcbrtentry(tp->t_inpcb); 1703 #endif 1704 } 1705 #endif 1706 #ifdef INET6 1707 if (tp->t_in6pcb) { 1708 so = tp->t_in6pcb->in6p_socket; 1709 #if defined(RTV_RPIPE) 1710 rt = in6_pcbrtentry(tp->t_in6pcb); 1711 #endif 1712 } 1713 #endif 1714 1715 tp->t_state = TCPS_ESTABLISHED; 1716 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 1717 1718 #ifdef RTV_RPIPE 1719 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 1720 bufsize = rt->rt_rmx.rmx_recvpipe; 1721 else 1722 #endif 1723 bufsize = so->so_rcv.sb_hiwat; 1724 if (bufsize > tp->t_ourmss) { 1725 bufsize = roundup(bufsize, tp->t_ourmss); 1726 if (bufsize > sb_max) 1727 bufsize = sb_max; 1728 (void) sbreserve(&so->so_rcv, bufsize); 1729 } 1730 } 1731 1732 /* 1733 * Check if there's an initial rtt or rttvar. Convert from the 1734 * route-table units to scaled multiples of the slow timeout timer. 1735 * Called only during the 3-way handshake. 1736 */ 1737 void 1738 tcp_rmx_rtt(tp) 1739 struct tcpcb *tp; 1740 { 1741 #ifdef RTV_RTT 1742 struct rtentry *rt = NULL; 1743 int rtt; 1744 1745 #ifdef DIAGNOSTIC 1746 if (tp->t_inpcb && tp->t_in6pcb) 1747 panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set"); 1748 #endif 1749 #ifdef INET 1750 if (tp->t_inpcb) 1751 rt = in_pcbrtentry(tp->t_inpcb); 1752 #endif 1753 #ifdef INET6 1754 if (tp->t_in6pcb) 1755 rt = in6_pcbrtentry(tp->t_in6pcb); 1756 #endif 1757 if (rt == NULL) 1758 return; 1759 1760 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1761 /* 1762 * XXX The lock bit for MTU indicates that the value 1763 * is also a minimum value; this is subject to time. 1764 */ 1765 if (rt->rt_rmx.rmx_locks & RTV_RTT) 1766 TCPT_RANGESET(tp->t_rttmin, 1767 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 1768 TCPTV_MIN, TCPTV_REXMTMAX); 1769 tp->t_srtt = rtt / 1770 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 1771 if (rt->rt_rmx.rmx_rttvar) { 1772 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 1773 ((RTM_RTTUNIT / PR_SLOWHZ) >> 1774 (TCP_RTTVAR_SHIFT + 2)); 1775 } else { 1776 /* Default variation is +- 1 rtt */ 1777 tp->t_rttvar = 1778 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 1779 } 1780 TCPT_RANGESET(tp->t_rxtcur, 1781 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 1782 tp->t_rttmin, TCPTV_REXMTMAX); 1783 } 1784 #endif 1785 } 1786 1787 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 1788 #if NRND > 0 1789 u_int8_t tcp_iss_secret[16]; /* 128 bits; should be plenty */ 1790 #endif 1791 1792 /* 1793 * Get a new sequence value given a tcp control block 1794 */ 1795 tcp_seq 1796 tcp_new_iss(struct tcpcb *tp, tcp_seq addin) 1797 { 1798 1799 #ifdef INET 1800 if (tp->t_inpcb != NULL) { 1801 return (tcp_new_iss1(&tp->t_inpcb->inp_laddr, 1802 &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport, 1803 tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr), 1804 addin)); 1805 } 1806 #endif 1807 #ifdef INET6 1808 if (tp->t_in6pcb != NULL) { 1809 return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr, 1810 &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport, 1811 tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr), 1812 addin)); 1813 } 1814 #endif 1815 /* Not possible. */ 1816 panic("tcp_new_iss"); 1817 } 1818 1819 /* 1820 * This routine actually generates a new TCP initial sequence number. 1821 */ 1822 tcp_seq 1823 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport, 1824 size_t addrsz, tcp_seq addin) 1825 { 1826 tcp_seq tcp_iss; 1827 1828 #if NRND > 0 1829 static int beenhere; 1830 1831 /* 1832 * If we haven't been here before, initialize our cryptographic 1833 * hash secret. 1834 */ 1835 if (beenhere == 0) { 1836 rnd_extract_data(tcp_iss_secret, sizeof(tcp_iss_secret), 1837 RND_EXTRACT_ANY); 1838 beenhere = 1; 1839 } 1840 1841 if (tcp_do_rfc1948) { 1842 MD5_CTX ctx; 1843 u_int8_t hash[16]; /* XXX MD5 knowledge */ 1844 1845 /* 1846 * Compute the base value of the ISS. It is a hash 1847 * of (saddr, sport, daddr, dport, secret). 1848 */ 1849 MD5Init(&ctx); 1850 1851 MD5Update(&ctx, (u_char *) laddr, addrsz); 1852 MD5Update(&ctx, (u_char *) &lport, sizeof(lport)); 1853 1854 MD5Update(&ctx, (u_char *) faddr, addrsz); 1855 MD5Update(&ctx, (u_char *) &fport, sizeof(fport)); 1856 1857 MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret)); 1858 1859 MD5Final(hash, &ctx); 1860 1861 memcpy(&tcp_iss, hash, sizeof(tcp_iss)); 1862 1863 /* 1864 * Now increment our "timer", and add it in to 1865 * the computed value. 1866 * 1867 * XXX Use `addin'? 1868 * XXX TCP_ISSINCR too large to use? 1869 */ 1870 tcp_iss_seq += TCP_ISSINCR; 1871 #ifdef TCPISS_DEBUG 1872 printf("ISS hash 0x%08x, ", tcp_iss); 1873 #endif 1874 tcp_iss += tcp_iss_seq + addin; 1875 #ifdef TCPISS_DEBUG 1876 printf("new ISS 0x%08x\n", tcp_iss); 1877 #endif 1878 } else 1879 #endif /* NRND > 0 */ 1880 { 1881 /* 1882 * Randomize. 1883 */ 1884 #if NRND > 0 1885 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY); 1886 #else 1887 tcp_iss = random(); 1888 #endif 1889 1890 /* 1891 * If we were asked to add some amount to a known value, 1892 * we will take a random value obtained above, mask off 1893 * the upper bits, and add in the known value. We also 1894 * add in a constant to ensure that we are at least a 1895 * certain distance from the original value. 1896 * 1897 * This is used when an old connection is in timed wait 1898 * and we have a new one coming in, for instance. 1899 */ 1900 if (addin != 0) { 1901 #ifdef TCPISS_DEBUG 1902 printf("Random %08x, ", tcp_iss); 1903 #endif 1904 tcp_iss &= TCP_ISS_RANDOM_MASK; 1905 tcp_iss += addin + TCP_ISSINCR; 1906 #ifdef TCPISS_DEBUG 1907 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 1908 #endif 1909 } else { 1910 tcp_iss &= TCP_ISS_RANDOM_MASK; 1911 tcp_iss += tcp_iss_seq; 1912 tcp_iss_seq += TCP_ISSINCR; 1913 #ifdef TCPISS_DEBUG 1914 printf("ISS %08x\n", tcp_iss); 1915 #endif 1916 } 1917 } 1918 1919 if (tcp_compat_42) { 1920 /* 1921 * Limit it to the positive range for really old TCP 1922 * implementations. 1923 */ 1924 if (tcp_iss >= 0x80000000) 1925 tcp_iss &= 0x7fffffff; /* XXX */ 1926 } 1927 1928 return (tcp_iss); 1929 } 1930 1931 #ifdef IPSEC 1932 /* compute ESP/AH header size for TCP, including outer IP header. */ 1933 size_t 1934 ipsec4_hdrsiz_tcp(tp) 1935 struct tcpcb *tp; 1936 { 1937 struct inpcb *inp; 1938 size_t hdrsiz; 1939 1940 /* XXX mapped addr case (tp->t_in6pcb) */ 1941 if (!tp || !tp->t_template || !(inp = tp->t_inpcb)) 1942 return 0; 1943 switch (tp->t_family) { 1944 case AF_INET: 1945 /* XXX: should use currect direction. */ 1946 hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp); 1947 break; 1948 default: 1949 hdrsiz = 0; 1950 break; 1951 } 1952 1953 return hdrsiz; 1954 } 1955 1956 #ifdef INET6 1957 size_t 1958 ipsec6_hdrsiz_tcp(tp) 1959 struct tcpcb *tp; 1960 { 1961 struct in6pcb *in6p; 1962 size_t hdrsiz; 1963 1964 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb)) 1965 return 0; 1966 switch (tp->t_family) { 1967 case AF_INET6: 1968 /* XXX: should use currect direction. */ 1969 hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p); 1970 break; 1971 case AF_INET: 1972 /* mapped address case - tricky */ 1973 default: 1974 hdrsiz = 0; 1975 break; 1976 } 1977 1978 return hdrsiz; 1979 } 1980 #endif 1981 #endif /*IPSEC*/ 1982 1983 /* 1984 * Determine the length of the TCP options for this connection. 1985 * 1986 * XXX: What do we do for SACK, when we add that? Just reserve 1987 * all of the space? Otherwise we can't exactly be incrementing 1988 * cwnd by an amount that varies depending on the amount we last 1989 * had to SACK! 1990 */ 1991 1992 u_int 1993 tcp_optlen(tp) 1994 struct tcpcb *tp; 1995 { 1996 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 1997 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 1998 return TCPOLEN_TSTAMP_APPA; 1999 else 2000 return 0; 2001 } 2002