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