1 /* $NetBSD: tcp_subr.c,v 1.81 1999/09/23 04:02:27 enami 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 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 "opt_inet.h" 105 #include "opt_ipsec.h" 106 #include "opt_tcp_compat_42.h" 107 #include "rnd.h" 108 109 #include <sys/param.h> 110 #include <sys/proc.h> 111 #include <sys/systm.h> 112 #include <sys/malloc.h> 113 #include <sys/mbuf.h> 114 #include <sys/socket.h> 115 #include <sys/socketvar.h> 116 #include <sys/protosw.h> 117 #include <sys/errno.h> 118 #include <sys/kernel.h> 119 #include <sys/pool.h> 120 #if NRND > 0 121 #include <sys/rnd.h> 122 #endif 123 124 #include <net/route.h> 125 #include <net/if.h> 126 127 #include <netinet/in.h> 128 #include <netinet/in_systm.h> 129 #include <netinet/ip.h> 130 #include <netinet/in_pcb.h> 131 #include <netinet/ip_var.h> 132 #include <netinet/ip_icmp.h> 133 134 #ifdef INET6 135 #ifndef INET 136 #include <netinet/in.h> 137 #endif 138 #include <netinet/ip6.h> 139 #include <netinet6/in6_pcb.h> 140 #include <netinet6/ip6_var.h> 141 #include <netinet6/in6_var.h> 142 #endif 143 144 #include <netinet/tcp.h> 145 #include <netinet/tcp_fsm.h> 146 #include <netinet/tcp_seq.h> 147 #include <netinet/tcp_timer.h> 148 #include <netinet/tcp_var.h> 149 #include <netinet/tcpip.h> 150 151 #ifdef IPSEC 152 #include <netinet6/ipsec.h> 153 #endif /*IPSEC*/ 154 155 #ifdef INET6 156 struct in6pcb tcb6; 157 #endif 158 159 /* patchable/settable parameters for tcp */ 160 int tcp_mssdflt = TCP_MSS; 161 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 162 int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */ 163 int tcp_do_sack = 1; /* selective acknowledgement */ 164 int tcp_do_win_scale = 1; /* RFC1323 window scaling */ 165 int tcp_do_timestamps = 1; /* RFC1323 timestamps */ 166 int tcp_do_newreno = 0; /* Use the New Reno algorithms */ 167 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */ 168 int tcp_init_win = 1; 169 int tcp_mss_ifmtu = 0; 170 #ifdef TCP_COMPAT_42 171 int tcp_compat_42 = 1; 172 #else 173 int tcp_compat_42 = 0; 174 #endif 175 176 #ifndef TCBHASHSIZE 177 #define TCBHASHSIZE 128 178 #endif 179 int tcbhashsize = TCBHASHSIZE; 180 181 int tcp_freeq __P((struct tcpcb *)); 182 183 struct pool tcpcb_pool; 184 185 /* 186 * Tcp initialization 187 */ 188 void 189 tcp_init() 190 { 191 int hlen; 192 193 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", 194 0, NULL, NULL, M_PCB); 195 in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize); 196 #ifdef INET6 197 tcb6.in6p_next = tcb6.in6p_prev = &tcb6; 198 #endif 199 LIST_INIT(&tcp_delacks); 200 201 hlen = sizeof(struct ip) + sizeof(struct tcphdr); 202 #ifdef INET6 203 if (sizeof(struct ip) < sizeof(struct ip6_hdr)) 204 hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 205 #endif 206 if (max_protohdr < hlen) 207 max_protohdr = hlen; 208 if (max_linkhdr + hlen > MHLEN) 209 panic("tcp_init"); 210 211 /* Initialize the compressed state engine. */ 212 syn_cache_init(); 213 } 214 215 /* 216 * Create template to be used to send tcp packets on a connection. 217 * Call after host entry created, allocates an mbuf and fills 218 * in a skeletal tcp/ip header, minimizing the amount of work 219 * necessary when the connection is used. 220 */ 221 struct mbuf * 222 tcp_template(tp) 223 struct tcpcb *tp; 224 { 225 register struct inpcb *inp = tp->t_inpcb; 226 #ifdef INET6 227 register struct in6pcb *in6p = tp->t_in6pcb; 228 #endif 229 register struct tcphdr *n; 230 register struct mbuf *m; 231 int hlen; 232 233 switch (tp->t_family) { 234 case AF_INET: 235 hlen = sizeof(struct ip); 236 if (inp) 237 break; 238 #ifdef INET6 239 if (in6p) { 240 /* mapped addr case */ 241 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) 242 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) 243 break; 244 } 245 #endif 246 return NULL; /*EINVAL*/ 247 #ifdef INET6 248 case AF_INET6: 249 hlen = sizeof(struct ip6_hdr); 250 if (in6p) { 251 /* more sainty check? */ 252 break; 253 } 254 return NULL; /*EINVAL*/ 255 #endif 256 default: 257 hlen = 0; /*pacify gcc*/ 258 return NULL; /*EAFNOSUPPORT*/ 259 } 260 if ((m = tp->t_template) == 0) { 261 MGETHDR(m, M_DONTWAIT, MT_HEADER); 262 if (m) { 263 MCLGET(m, M_DONTWAIT); 264 if ((m->m_flags & M_EXT) == 0) { 265 m_free(m); 266 m = NULL; 267 } 268 } 269 if (m == NULL) 270 return NULL; 271 m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr); 272 } 273 bzero(mtod(m, caddr_t), m->m_len); 274 switch (tp->t_family) { 275 case AF_INET: 276 { 277 struct ipovly *ipov; 278 mtod(m, struct ip *)->ip_v = 4; 279 ipov = mtod(m, struct ipovly *); 280 ipov->ih_pr = IPPROTO_TCP; 281 ipov->ih_len = htons(sizeof(struct tcphdr)); 282 if (inp) { 283 ipov->ih_src = inp->inp_laddr; 284 ipov->ih_dst = inp->inp_faddr; 285 } 286 #ifdef INET6 287 else if (in6p) { 288 /* mapped addr case */ 289 bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src, 290 sizeof(ipov->ih_src)); 291 bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst, 292 sizeof(ipov->ih_dst)); 293 } 294 #endif 295 break; 296 } 297 #ifdef INET6 298 case AF_INET6: 299 { 300 struct ip6_hdr *ip6; 301 mtod(m, struct ip *)->ip_v = 6; 302 ip6 = mtod(m, struct ip6_hdr *); 303 ip6->ip6_nxt = IPPROTO_TCP; 304 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 305 ip6->ip6_src = in6p->in6p_laddr; 306 ip6->ip6_dst = in6p->in6p_faddr; 307 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 308 if (ip6_auto_flowlabel) { 309 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK; 310 ip6->ip6_flow |= 311 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 312 } 313 ip6->ip6_vfc = IPV6_VERSION; 314 break; 315 } 316 #endif 317 } 318 n = (struct tcphdr *)(mtod(m, caddr_t) + hlen); 319 if (inp) { 320 n->th_sport = inp->inp_lport; 321 n->th_dport = inp->inp_fport; 322 } 323 #ifdef INET6 324 else if (in6p) { 325 n->th_sport = in6p->in6p_lport; 326 n->th_dport = in6p->in6p_fport; 327 } 328 #endif 329 n->th_seq = 0; 330 n->th_ack = 0; 331 n->th_x2 = 0; 332 n->th_off = 5; 333 n->th_flags = 0; 334 n->th_win = 0; 335 n->th_sum = 0; 336 n->th_urp = 0; 337 return (m); 338 } 339 340 /* 341 * Send a single message to the TCP at address specified by 342 * the given TCP/IP header. If m == 0, then we make a copy 343 * of the tcpiphdr at ti and send directly to the addressed host. 344 * This is used to force keep alive messages out using the TCP 345 * template for a connection tp->t_template. If flags are given 346 * then we send a message back to the TCP which originated the 347 * segment ti, and discard the mbuf containing it and any other 348 * attached mbufs. 349 * 350 * In any case the ack and sequence number of the transmitted 351 * segment are as specified by the parameters. 352 */ 353 int 354 tcp_respond(tp, template, m, th0, ack, seq, flags) 355 struct tcpcb *tp; 356 struct mbuf *template; 357 register struct mbuf *m; 358 struct tcphdr *th0; 359 tcp_seq ack, seq; 360 int flags; 361 { 362 #ifndef INET6 363 struct route iproute; 364 #else 365 struct route_in6 iproute; /* sizeof(route_in6) > sizeof(route) */ 366 #endif 367 struct route *ro; 368 struct rtentry *rt; 369 int error, tlen, win = 0; 370 int hlen; 371 struct ip *ip; 372 #ifdef INET6 373 struct ip6_hdr *ip6; 374 #endif 375 int family; /* family on packet, not inpcb/in6pcb! */ 376 struct tcphdr *th; 377 378 if (tp != NULL && (flags & TH_RST) == 0) { 379 if (tp->t_inpcb) 380 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 381 #ifdef INET6 382 else if (tp->t_in6pcb) 383 win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv); 384 #endif 385 } 386 387 ip = NULL; 388 #ifdef INET6 389 ip6 = NULL; 390 #endif 391 if (m == 0) { 392 if (!template) 393 return EINVAL; 394 395 /* get family information from template */ 396 switch (mtod(template, struct ip *)->ip_v) { 397 case 4: 398 family = AF_INET; 399 hlen = sizeof(struct ip); 400 break; 401 #ifdef INET6 402 case 6: 403 family = AF_INET6; 404 hlen = sizeof(struct ip6_hdr); 405 break; 406 #endif 407 default: 408 return EAFNOSUPPORT; 409 } 410 411 MGETHDR(m, M_DONTWAIT, MT_HEADER); 412 if (m) { 413 MCLGET(m, M_DONTWAIT); 414 if ((m->m_flags & M_EXT) == 0) { 415 m_free(m); 416 m = NULL; 417 } 418 } 419 if (m == NULL) 420 return (ENOBUFS); 421 422 if (tcp_compat_42) 423 tlen = 1; 424 else 425 tlen = 0; 426 427 m->m_data += max_linkhdr; 428 bcopy(mtod(template, caddr_t), mtod(m, caddr_t), 429 template->m_len); 430 switch (family) { 431 case AF_INET: 432 ip = mtod(m, struct ip *); 433 th = (struct tcphdr *)(ip + 1); 434 break; 435 #ifdef INET6 436 case AF_INET6: 437 ip6 = mtod(m, struct ip6_hdr *); 438 th = (struct tcphdr *)(ip6 + 1); 439 break; 440 #endif 441 default: /*pacify gcc*/ 442 ip = NULL; 443 #ifdef INET6 444 ip6 = NULL; 445 #endif 446 th = NULL; 447 break; 448 } 449 flags = TH_ACK; 450 } else { 451 /* get family information from m */ 452 switch (mtod(m, struct ip *)->ip_v) { 453 case 4: 454 family = AF_INET; 455 hlen = sizeof(struct ip); 456 break; 457 #ifdef INET6 458 case 6: 459 family = AF_INET6; 460 hlen = sizeof(struct ip6_hdr); 461 break; 462 #endif 463 default: 464 if (m) 465 m_freem(m); 466 return EAFNOSUPPORT; 467 } 468 469 /* template pointer almost has no meaning */ 470 m_freem(m->m_next); 471 m->m_next = 0; 472 m->m_len = hlen + sizeof(struct tcphdr); 473 if ((m->m_flags & M_PKTHDR) == 0) { 474 printf("non PKTHDR to tcp_respond\n"); 475 m_freem(m); 476 return EINVAL; 477 } 478 479 tlen = 0; 480 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 481 switch (family) { 482 case AF_INET: 483 ip = mtod(m, struct ip *); 484 th = (struct tcphdr *)(ip + 1); 485 xchg(ip->ip_dst, ip->ip_src, struct in_addr); 486 ip->ip_p = IPPROTO_TCP; 487 break; 488 #ifdef INET6 489 case AF_INET6: 490 ip6 = mtod(m, struct ip6_hdr *); 491 th = (struct tcphdr *)(ip6 + 1); 492 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 493 ip6->ip6_nxt = IPPROTO_TCP; 494 break; 495 #endif 496 } 497 *th = *th0; 498 xchg(th->th_dport, th->th_sport, u_int16_t); 499 #undef xchg 500 } 501 th->th_seq = htonl(seq); 502 th->th_ack = htonl(ack); 503 th->th_x2 = 0; 504 if ((flags & TH_SYN) == 0) { 505 if (tp) 506 th->th_win = htons((u_int16_t) (win >> tp->rcv_scale)); 507 else 508 th->th_win = htons((u_int16_t)win); 509 th->th_off = sizeof (struct tcphdr) >> 2; 510 tlen += sizeof (struct tcphdr); 511 } else 512 tlen += th->th_off << 2; 513 m->m_len = hlen + tlen; 514 m->m_pkthdr.len = hlen + tlen; 515 m->m_pkthdr.rcvif = (struct ifnet *) 0; 516 th->th_flags = flags; 517 th->th_urp = 0; 518 519 switch (family) { 520 case AF_INET: 521 { 522 struct ipovly *ipov = (struct ipovly *)ip; 523 bzero(ipov->ih_x1, sizeof ipov->ih_x1); 524 ipov->ih_len = htons((u_int16_t)tlen); 525 526 th->th_sum = 0; 527 th->th_sum = in_cksum(m, hlen + tlen); 528 ip->ip_len = hlen + tlen; /*will be flipped on output*/ 529 ip->ip_ttl = ip_defttl; 530 break; 531 } 532 #ifdef INET6 533 case AF_INET6: 534 { 535 th->th_sum = 0; 536 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 537 tlen); 538 ip6->ip6_plen = ntohs(tlen); 539 ip6->ip6_hlim = ip6_defhlim; 540 ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK; 541 if (ip6_auto_flowlabel) { 542 ip6->ip6_flow |= 543 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 544 } 545 break; 546 } 547 #endif 548 } 549 550 #ifdef IPSEC 551 m->m_pkthdr.rcvif = NULL; 552 #endif /*IPSEC*/ 553 554 /* 555 * If we're doing Path MTU discovery, we need to set DF unless 556 * the route's MTU is locked. If we lack a route, we need to 557 * look it up now. 558 * 559 * ip_output() could do this for us, but it's convenient to just 560 * do it here unconditionally. 561 */ 562 if (tp != NULL && tp->t_inpcb != NULL) { 563 ro = &tp->t_inpcb->inp_route; 564 #ifdef IPSEC 565 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket; 566 #endif 567 #ifdef DIAGNOSTIC 568 if (family != AF_INET) 569 panic("tcp_respond: address family mismatch"); 570 if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) { 571 panic("tcp_respond: ip_dst %x != inp_faddr %x", 572 ntohl(ip->ip_dst.s_addr), 573 ntohl(tp->t_inpcb->inp_faddr.s_addr)); 574 } 575 #endif 576 } 577 #ifdef INET6 578 else if (tp != NULL && tp->t_in6pcb != NULL) { 579 ro = (struct route *)&tp->t_in6pcb->in6p_route; 580 #ifdef IPSEC 581 m->m_pkthdr.rcvif = (struct ifnet *)tp->t_in6pcb->in6p_socket; 582 #endif 583 #ifdef DIAGNOSTIC 584 if (family == AF_INET) { 585 if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr)) 586 panic("tcp_respond: not mapped addr"); 587 if (bcmp(&ip->ip_dst, 588 &tp->t_in6pcb->in6p_faddr.s6_addr32[3], 589 sizeof(ip->ip_dst)) != 0) { 590 panic("tcp_respond: ip_dst != in6p_faddr"); 591 } 592 } else if (family == AF_INET6) { 593 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr)) 594 panic("tcp_respond: ip6_dst != in6p_faddr"); 595 } else 596 panic("tcp_respond: address family mismatch"); 597 #endif 598 } 599 #endif 600 else { 601 ro = (struct route *)&iproute; 602 bzero(ro, sizeof(iproute)); 603 } 604 if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) { 605 if (ro->ro_rt != NULL) { 606 RTFREE(ro->ro_rt); 607 ro->ro_rt = NULL; 608 } 609 switch (family) { 610 case AF_INET: 611 { 612 struct sockaddr_in *dst; 613 dst = satosin(&ro->ro_dst); 614 dst->sin_family = AF_INET; 615 dst->sin_len = sizeof(*dst); 616 dst->sin_addr = ip->ip_dst; 617 break; 618 } 619 #ifdef INET6 620 case AF_INET6: 621 { 622 struct sockaddr_in6 *dst; 623 dst = satosin6(&ro->ro_dst); 624 bzero(dst, sizeof(*dst)); 625 dst->sin6_family = AF_INET6; 626 dst->sin6_len = sizeof(*dst); 627 dst->sin6_addr = ip6->ip6_dst; 628 break; 629 } 630 #endif 631 } 632 rtalloc(ro); 633 if ((rt = ro->ro_rt) == NULL) { 634 m_freem(m); 635 switch (family) { 636 case AF_INET: 637 ipstat.ips_noroute++; 638 break; 639 #ifdef INET6 640 case AF_INET6: 641 ip6stat.ip6s_noroute++; 642 break; 643 #endif 644 } 645 return (EHOSTUNREACH); 646 } 647 } 648 switch (family) { 649 case AF_INET: 650 if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 651 ip->ip_off |= IP_DF; 652 653 error = ip_output(m, NULL, ro, 0, NULL); 654 break; 655 #ifdef INET6 656 case AF_INET6: 657 error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL); 658 break; 659 #endif 660 default: 661 error = EAFNOSUPPORT; 662 break; 663 } 664 665 if (ro == (struct route *)&iproute) { 666 RTFREE(ro->ro_rt); 667 ro->ro_rt = NULL; 668 } 669 670 return (error); 671 } 672 673 /* 674 * Create a new TCP control block, making an 675 * empty reassembly queue and hooking it to the argument 676 * protocol control block. 677 */ 678 struct tcpcb * 679 tcp_newtcpcb(family, aux) 680 int family; /* selects inpcb, or in6pcb */ 681 void *aux; 682 { 683 register struct tcpcb *tp; 684 685 switch (family) { 686 case PF_INET: 687 break; 688 #ifdef INET6 689 case PF_INET6: 690 break; 691 #endif 692 default: 693 return NULL; 694 } 695 696 tp = pool_get(&tcpcb_pool, PR_NOWAIT); 697 if (tp == NULL) 698 return (NULL); 699 bzero((caddr_t)tp, sizeof(struct tcpcb)); 700 LIST_INIT(&tp->segq); 701 LIST_INIT(&tp->timeq); 702 tp->t_family = family; /* may be overridden later on */ 703 tp->t_peermss = tcp_mssdflt; 704 tp->t_ourmss = tcp_mssdflt; 705 tp->t_segsz = tcp_mssdflt; 706 LIST_INIT(&tp->t_sc); 707 708 tp->t_flags = 0; 709 if (tcp_do_rfc1323 && tcp_do_win_scale) 710 tp->t_flags |= TF_REQ_SCALE; 711 if (tcp_do_rfc1323 && tcp_do_timestamps) 712 tp->t_flags |= TF_REQ_TSTMP; 713 if (tcp_do_sack == 2) 714 tp->t_flags |= TF_WILL_SACK; 715 else if (tcp_do_sack == 1) 716 tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK; 717 tp->t_flags |= TF_CANT_TXSACK; 718 switch (family) { 719 case PF_INET: 720 tp->t_inpcb = (struct inpcb *)aux; 721 break; 722 #ifdef INET6 723 case PF_INET6: 724 tp->t_in6pcb = (struct in6pcb *)aux; 725 break; 726 #endif 727 } 728 /* 729 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 730 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 731 * reasonable initial retransmit time. 732 */ 733 tp->t_srtt = TCPTV_SRTTBASE; 734 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1); 735 tp->t_rttmin = TCPTV_MIN; 736 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 737 TCPTV_MIN, TCPTV_REXMTMAX); 738 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 739 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 740 if (family == AF_INET) { 741 struct inpcb *inp = (struct inpcb *)aux; 742 inp->inp_ip.ip_ttl = ip_defttl; 743 inp->inp_ppcb = (caddr_t)tp; 744 } 745 #ifdef INET6 746 else if (family == AF_INET6) { 747 struct in6pcb *in6p = (struct in6pcb *)aux; 748 in6p->in6p_ip6.ip6_hlim = ip6_defhlim; 749 in6p->in6p_ppcb = (caddr_t)tp; 750 } 751 #endif 752 return (tp); 753 } 754 755 /* 756 * Drop a TCP connection, reporting 757 * the specified error. If connection is synchronized, 758 * then send a RST to peer. 759 */ 760 struct tcpcb * 761 tcp_drop(tp, errno) 762 register struct tcpcb *tp; 763 int errno; 764 { 765 struct socket *so; 766 767 if (tp->t_inpcb) 768 so = tp->t_inpcb->inp_socket; 769 #ifdef INET6 770 else if (tp->t_in6pcb) 771 so = tp->t_in6pcb->in6p_socket; 772 #endif 773 else 774 return NULL; 775 776 if (TCPS_HAVERCVDSYN(tp->t_state)) { 777 tp->t_state = TCPS_CLOSED; 778 (void) tcp_output(tp); 779 tcpstat.tcps_drops++; 780 } else 781 tcpstat.tcps_conndrops++; 782 if (errno == ETIMEDOUT && tp->t_softerror) 783 errno = tp->t_softerror; 784 so->so_error = errno; 785 return (tcp_close(tp)); 786 } 787 788 /* 789 * Close a TCP control block: 790 * discard all space held by the tcp 791 * discard internet protocol block 792 * wake up any sleepers 793 */ 794 struct tcpcb * 795 tcp_close(tp) 796 register struct tcpcb *tp; 797 { 798 struct inpcb *inp; 799 #ifdef INET6 800 struct in6pcb *in6p; 801 #endif 802 struct socket *so; 803 #ifdef RTV_RTT 804 register struct rtentry *rt; 805 #endif 806 struct route *ro; 807 808 inp = tp->t_inpcb; 809 #ifdef INET6 810 in6p = tp->t_in6pcb; 811 #endif 812 so = NULL; 813 ro = NULL; 814 if (inp) { 815 so = inp->inp_socket; 816 ro = &inp->inp_route; 817 } 818 #ifdef INET6 819 else if (in6p) { 820 so = in6p->in6p_socket; 821 ro = (struct route *)&in6p->in6p_route; 822 } 823 #endif 824 825 #ifdef RTV_RTT 826 /* 827 * If we sent enough data to get some meaningful characteristics, 828 * save them in the routing entry. 'Enough' is arbitrarily 829 * defined as the sendpipesize (default 4K) * 16. This would 830 * give us 16 rtt samples assuming we only get one sample per 831 * window (the usual case on a long haul net). 16 samples is 832 * enough for the srtt filter to converge to within 5% of the correct 833 * value; fewer samples and we could save a very bogus rtt. 834 * 835 * Don't update the default route's characteristics and don't 836 * update anything that the user "locked". 837 */ 838 if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 839 ro && (rt = ro->ro_rt) && 840 !in_nullhost(satosin(rt_key(rt))->sin_addr)) { 841 register u_long i = 0; 842 843 if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 844 i = tp->t_srtt * 845 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 846 if (rt->rt_rmx.rmx_rtt && i) 847 /* 848 * filter this update to half the old & half 849 * the new values, converting scale. 850 * See route.h and tcp_var.h for a 851 * description of the scaling constants. 852 */ 853 rt->rt_rmx.rmx_rtt = 854 (rt->rt_rmx.rmx_rtt + i) / 2; 855 else 856 rt->rt_rmx.rmx_rtt = i; 857 } 858 if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 859 i = tp->t_rttvar * 860 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2)); 861 if (rt->rt_rmx.rmx_rttvar && i) 862 rt->rt_rmx.rmx_rttvar = 863 (rt->rt_rmx.rmx_rttvar + i) / 2; 864 else 865 rt->rt_rmx.rmx_rttvar = i; 866 } 867 /* 868 * update the pipelimit (ssthresh) if it has been updated 869 * already or if a pipesize was specified & the threshhold 870 * got below half the pipesize. I.e., wait for bad news 871 * before we start updating, then update on both good 872 * and bad news. 873 */ 874 if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 875 (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) || 876 i < (rt->rt_rmx.rmx_sendpipe / 2)) { 877 /* 878 * convert the limit from user data bytes to 879 * packets then to packet data bytes. 880 */ 881 i = (i + tp->t_segsz / 2) / tp->t_segsz; 882 if (i < 2) 883 i = 2; 884 i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr)); 885 if (rt->rt_rmx.rmx_ssthresh) 886 rt->rt_rmx.rmx_ssthresh = 887 (rt->rt_rmx.rmx_ssthresh + i) / 2; 888 else 889 rt->rt_rmx.rmx_ssthresh = i; 890 } 891 } 892 #endif /* RTV_RTT */ 893 /* free the reassembly queue, if any */ 894 TCP_REASS_LOCK(tp); 895 (void) tcp_freeq(tp); 896 TCP_REASS_UNLOCK(tp); 897 898 TCP_CLEAR_DELACK(tp); 899 syn_cache_cleanup(tp); 900 901 if (tp->t_template) { 902 m_free(tp->t_template); 903 tp->t_template = NULL; 904 } 905 pool_put(&tcpcb_pool, tp); 906 if (inp) { 907 inp->inp_ppcb = 0; 908 soisdisconnected(so); 909 in_pcbdetach(inp); 910 } 911 #ifdef INET6 912 else if (in6p) { 913 in6p->in6p_ppcb = 0; 914 soisdisconnected(so); 915 in6_pcbdetach(in6p); 916 } 917 #endif 918 tcpstat.tcps_closed++; 919 return ((struct tcpcb *)0); 920 } 921 922 int 923 tcp_freeq(tp) 924 struct tcpcb *tp; 925 { 926 register struct ipqent *qe; 927 int rv = 0; 928 #ifdef TCPREASS_DEBUG 929 int i = 0; 930 #endif 931 932 TCP_REASS_LOCK_CHECK(tp); 933 934 while ((qe = tp->segq.lh_first) != NULL) { 935 #ifdef TCPREASS_DEBUG 936 printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n", 937 tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len, 938 qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST)); 939 #endif 940 LIST_REMOVE(qe, ipqe_q); 941 LIST_REMOVE(qe, ipqe_timeq); 942 m_freem(qe->ipqe_m); 943 pool_put(&ipqent_pool, qe); 944 rv = 1; 945 } 946 return (rv); 947 } 948 949 /* 950 * Protocol drain routine. Called when memory is in short supply. 951 */ 952 void 953 tcp_drain() 954 { 955 register struct inpcb *inp; 956 register struct tcpcb *tp; 957 958 /* 959 * Free the sequence queue of all TCP connections. 960 */ 961 inp = tcbtable.inpt_queue.cqh_first; 962 if (inp) /* XXX */ 963 for (; inp != (struct inpcb *)&tcbtable.inpt_queue; 964 inp = inp->inp_queue.cqe_next) { 965 if ((tp = intotcpcb(inp)) != NULL) { 966 /* 967 * We may be called from a device's interrupt 968 * context. If the tcpcb is already busy, 969 * just bail out now. 970 */ 971 if (tcp_reass_lock_try(tp) == 0) 972 continue; 973 if (tcp_freeq(tp)) 974 tcpstat.tcps_connsdrained++; 975 TCP_REASS_UNLOCK(tp); 976 } 977 } 978 } 979 980 /* 981 * Notify a tcp user of an asynchronous error; 982 * store error as soft error, but wake up user 983 * (for now, won't do anything until can select for soft error). 984 */ 985 void 986 tcp_notify(inp, error) 987 struct inpcb *inp; 988 int error; 989 { 990 register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 991 register struct socket *so = inp->inp_socket; 992 993 /* 994 * Ignore some errors if we are hooked up. 995 * If connection hasn't completed, has retransmitted several times, 996 * and receives a second error, give up now. This is better 997 * than waiting a long time to establish a connection that 998 * can never complete. 999 */ 1000 if (tp->t_state == TCPS_ESTABLISHED && 1001 (error == EHOSTUNREACH || error == ENETUNREACH || 1002 error == EHOSTDOWN)) { 1003 return; 1004 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1005 tp->t_rxtshift > 3 && tp->t_softerror) 1006 so->so_error = error; 1007 else 1008 tp->t_softerror = error; 1009 wakeup((caddr_t) &so->so_timeo); 1010 sorwakeup(so); 1011 sowwakeup(so); 1012 } 1013 1014 #if defined(INET6) && !defined(TCP6) 1015 void 1016 tcp6_notify(in6p, error) 1017 struct in6pcb *in6p; 1018 int error; 1019 { 1020 register struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb; 1021 register struct socket *so = in6p->in6p_socket; 1022 1023 /* 1024 * Ignore some errors if we are hooked up. 1025 * If connection hasn't completed, has retransmitted several times, 1026 * and receives a second error, give up now. This is better 1027 * than waiting a long time to establish a connection that 1028 * can never complete. 1029 */ 1030 if (tp->t_state == TCPS_ESTABLISHED && 1031 (error == EHOSTUNREACH || error == ENETUNREACH || 1032 error == EHOSTDOWN)) { 1033 return; 1034 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 1035 tp->t_rxtshift > 3 && tp->t_softerror) 1036 so->so_error = error; 1037 else 1038 tp->t_softerror = error; 1039 wakeup((caddr_t) &so->so_timeo); 1040 sorwakeup(so); 1041 sowwakeup(so); 1042 } 1043 #endif 1044 1045 #if defined(INET6) && !defined(TCP6) 1046 void 1047 tcp6_ctlinput(cmd, sa, ip6, m, off) 1048 int cmd; 1049 struct sockaddr *sa; 1050 register struct ip6_hdr *ip6; 1051 struct mbuf *m; 1052 int off; 1053 { 1054 register struct tcphdr *thp; 1055 struct tcphdr th; 1056 void (*notify) __P((struct in6pcb *, int)) = tcp6_notify; 1057 int nmatch; 1058 extern struct in6_addr zeroin6_addr; /* netinet6/in6_pcb.c */ 1059 struct sockaddr_in6 sa6; 1060 1061 if (sa->sa_family != AF_INET6 || 1062 sa->sa_len != sizeof(struct sockaddr_in6)) 1063 return; 1064 if (cmd == PRC_QUENCH) 1065 notify = tcp6_quench; 1066 else if (cmd == PRC_MSGSIZE) 1067 notify = tcp6_mtudisc; 1068 else if (!PRC_IS_REDIRECT(cmd) && 1069 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 1070 return; 1071 1072 /* translate addresses into internal form */ 1073 sa6 = *(struct sockaddr_in6 *)sa; 1074 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr)) 1075 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 1076 1077 if (ip6) { 1078 /* 1079 * XXX: We assume that when ip6 is non NULL, 1080 * M and OFF are valid. 1081 */ 1082 struct in6_addr s; 1083 1084 /* translate addresses into internal form */ 1085 memcpy(&s, &ip6->ip6_src, sizeof(s)); 1086 if (IN6_IS_ADDR_LINKLOCAL(&s)) 1087 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 1088 1089 if (m->m_len < off + sizeof(th)) { 1090 /* 1091 * this should be rare case, 1092 * so we compromise on this copy... 1093 */ 1094 m_copydata(m, off, sizeof(th), (caddr_t)&th); 1095 thp = &th; 1096 } else 1097 thp = (struct tcphdr *)(mtod(m, caddr_t) + off); 1098 nmatch = in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 1099 thp->th_dport, &s, thp->th_sport, cmd, notify); 1100 if (nmatch == 0 && syn_cache_count && 1101 (inet6ctlerrmap[cmd] == EHOSTUNREACH || 1102 inet6ctlerrmap[cmd] == ENETUNREACH || 1103 inet6ctlerrmap[cmd] == EHOSTDOWN)) { 1104 struct sockaddr_in6 sin6; 1105 bzero(&sin6, sizeof(sin6)); 1106 sin6.sin6_len = sizeof(sin6); 1107 sin6.sin6_family = AF_INET6; 1108 sin6.sin6_port = thp->th_sport; 1109 sin6.sin6_addr = s; 1110 syn_cache_unreach((struct sockaddr *)&sin6, sa, thp); 1111 } 1112 } else { 1113 (void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 0, 1114 &zeroin6_addr, 0, cmd, notify); 1115 } 1116 } 1117 #endif 1118 1119 /* assumes that ip header and tcp header are contiguous on mbuf */ 1120 void * 1121 tcp_ctlinput(cmd, sa, v) 1122 int cmd; 1123 struct sockaddr *sa; 1124 register void *v; 1125 { 1126 register struct ip *ip = v; 1127 register struct tcphdr *th; 1128 extern int inetctlerrmap[]; 1129 void (*notify) __P((struct inpcb *, int)) = tcp_notify; 1130 int errno; 1131 int nmatch; 1132 1133 if (sa->sa_family != AF_INET || 1134 sa->sa_len != sizeof(struct sockaddr_in)) 1135 return NULL; 1136 if ((unsigned)cmd >= PRC_NCMDS) 1137 return NULL; 1138 errno = inetctlerrmap[cmd]; 1139 if (cmd == PRC_QUENCH) 1140 notify = tcp_quench; 1141 else if (PRC_IS_REDIRECT(cmd)) 1142 notify = in_rtchange, ip = 0; 1143 else if (cmd == PRC_MSGSIZE && ip_mtudisc) 1144 notify = tcp_mtudisc, ip = 0; 1145 else if (cmd == PRC_HOSTDEAD) 1146 ip = 0; 1147 else if (errno == 0) 1148 return NULL; 1149 if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) { 1150 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 1151 nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr, 1152 th->th_dport, ip->ip_src, th->th_sport, errno, notify); 1153 if (nmatch == 0 && syn_cache_count && 1154 (inetctlerrmap[cmd] == EHOSTUNREACH || 1155 inetctlerrmap[cmd] == ENETUNREACH || 1156 inetctlerrmap[cmd] == EHOSTDOWN)) { 1157 struct sockaddr_in sin; 1158 bzero(&sin, sizeof(sin)); 1159 sin.sin_len = sizeof(sin); 1160 sin.sin_family = AF_INET; 1161 sin.sin_port = th->th_sport; 1162 sin.sin_addr = ip->ip_src; 1163 syn_cache_unreach((struct sockaddr *)&sin, sa, th); 1164 } 1165 1166 /* XXX mapped address case */ 1167 } 1168 else { 1169 (void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno, 1170 notify); 1171 } 1172 return NULL; 1173 } 1174 1175 /* 1176 * When a source quence is received, we are being notifed of congestion. 1177 * Close the congestion window down to the Loss Window (one segment). 1178 * We will gradually open it again as we proceed. 1179 */ 1180 void 1181 tcp_quench(inp, errno) 1182 struct inpcb *inp; 1183 int errno; 1184 { 1185 struct tcpcb *tp = intotcpcb(inp); 1186 1187 if (tp) 1188 tp->snd_cwnd = tp->t_segsz; 1189 } 1190 1191 #if defined(INET6) && !defined(TCP6) 1192 void 1193 tcp6_quench(in6p, errno) 1194 struct in6pcb *in6p; 1195 int errno; 1196 { 1197 struct tcpcb *tp = in6totcpcb(in6p); 1198 1199 if (tp) 1200 tp->snd_cwnd = tp->t_segsz; 1201 } 1202 #endif 1203 1204 /* 1205 * On receipt of path MTU corrections, flush old route and replace it 1206 * with the new one. Retransmit all unacknowledged packets, to ensure 1207 * that all packets will be received. 1208 */ 1209 void 1210 tcp_mtudisc(inp, errno) 1211 struct inpcb *inp; 1212 int errno; 1213 { 1214 struct tcpcb *tp = intotcpcb(inp); 1215 struct rtentry *rt = in_pcbrtentry(inp); 1216 1217 if (tp != 0) { 1218 if (rt != 0) { 1219 /* 1220 * If this was not a host route, remove and realloc. 1221 */ 1222 if ((rt->rt_flags & RTF_HOST) == 0) { 1223 in_rtchange(inp, errno); 1224 if ((rt = in_pcbrtentry(inp)) == 0) 1225 return; 1226 } 1227 1228 /* 1229 * Slow start out of the error condition. We 1230 * use the MTU because we know it's smaller 1231 * than the previously transmitted segment. 1232 * 1233 * Note: This is more conservative than the 1234 * suggestion in draft-floyd-incr-init-win-03. 1235 */ 1236 if (rt->rt_rmx.rmx_mtu != 0) 1237 tp->snd_cwnd = 1238 TCP_INITIAL_WINDOW(tcp_init_win, 1239 rt->rt_rmx.rmx_mtu); 1240 } 1241 1242 /* 1243 * Resend unacknowledged packets. 1244 */ 1245 tp->snd_nxt = tp->snd_una; 1246 tcp_output(tp); 1247 } 1248 } 1249 1250 #if defined(INET6) && !defined(TCP6) 1251 void 1252 tcp6_mtudisc(in6p, errno) 1253 struct in6pcb *in6p; 1254 int errno; 1255 { 1256 struct tcpcb *tp = in6totcpcb(in6p); 1257 struct rtentry *rt = in6_pcbrtentry(in6p); 1258 1259 if (tp != 0) { 1260 if (rt != 0) { 1261 /* 1262 * If this was not a host route, remove and realloc. 1263 */ 1264 if ((rt->rt_flags & RTF_HOST) == 0) { 1265 in6_rtchange(in6p, errno); 1266 if ((rt = in6_pcbrtentry(in6p)) == 0) 1267 return; 1268 } 1269 1270 /* 1271 * Slow start out of the error condition. We 1272 * use the MTU because we know it's smaller 1273 * than the previously transmitted segment. 1274 * 1275 * Note: This is more conservative than the 1276 * suggestion in draft-floyd-incr-init-win-03. 1277 */ 1278 if (rt->rt_rmx.rmx_mtu != 0) 1279 tp->snd_cwnd = 1280 TCP_INITIAL_WINDOW(tcp_init_win, 1281 rt->rt_rmx.rmx_mtu); 1282 } 1283 1284 /* 1285 * Resend unacknowledged packets. 1286 */ 1287 tp->snd_nxt = tp->snd_una; 1288 tcp_output(tp); 1289 } 1290 } 1291 #endif 1292 1293 /* 1294 * Compute the MSS to advertise to the peer. Called only during 1295 * the 3-way handshake. If we are the server (peer initiated 1296 * connection), we are called with a pointer to the interface 1297 * on which the SYN packet arrived. If we are the client (we 1298 * initiated connection), we are called with a pointer to the 1299 * interface out which this connection should go. 1300 * 1301 * NOTE: Do not subtract IP option/extension header size nor IPsec 1302 * header size from MSS advertisement. MSS option must hold the maximum 1303 * segment size we can accept, so it must always be: 1304 * max(if mtu) - ip header - tcp header 1305 */ 1306 u_long 1307 tcp_mss_to_advertise(ifp, af) 1308 const struct ifnet *ifp; 1309 int af; 1310 { 1311 extern u_long in_maxmtu; 1312 u_long mss = 0; 1313 u_long hdrsiz; 1314 1315 /* 1316 * In order to avoid defeating path MTU discovery on the peer, 1317 * we advertise the max MTU of all attached networks as our MSS, 1318 * per RFC 1191, section 3.1. 1319 * 1320 * We provide the option to advertise just the MTU of 1321 * the interface on which we hope this connection will 1322 * be receiving. If we are responding to a SYN, we 1323 * will have a pretty good idea about this, but when 1324 * initiating a connection there is a bit more doubt. 1325 * 1326 * We also need to ensure that loopback has a large enough 1327 * MSS, as the loopback MTU is never included in in_maxmtu. 1328 */ 1329 1330 if (ifp != NULL) 1331 mss = ifp->if_mtu; 1332 1333 if (tcp_mss_ifmtu == 0) 1334 mss = max(in_maxmtu, mss); 1335 1336 switch (af) { 1337 case AF_INET: 1338 hdrsiz = sizeof(struct ip); 1339 break; 1340 #ifdef INET6 1341 case AF_INET6: 1342 hdrsiz = sizeof(struct ip6_hdr); 1343 break; 1344 #endif 1345 default: 1346 hdrsiz = 0; 1347 break; 1348 } 1349 hdrsiz += sizeof(struct tcphdr); 1350 if (mss > hdrsiz) 1351 mss -= hdrsiz; 1352 1353 mss = max(tcp_mssdflt, mss); 1354 return (mss); 1355 } 1356 1357 /* 1358 * Set connection variables based on the peer's advertised MSS. 1359 * We are passed the TCPCB for the actual connection. If we 1360 * are the server, we are called by the compressed state engine 1361 * when the 3-way handshake is complete. If we are the client, 1362 * we are called when we recieve the SYN,ACK from the server. 1363 * 1364 * NOTE: Our advertised MSS value must be initialized in the TCPCB 1365 * before this routine is called! 1366 */ 1367 void 1368 tcp_mss_from_peer(tp, offer) 1369 struct tcpcb *tp; 1370 int offer; 1371 { 1372 struct socket *so; 1373 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1374 struct rtentry *rt; 1375 #endif 1376 u_long bufsize; 1377 int mss; 1378 1379 so = NULL; 1380 rt = NULL; 1381 if (tp->t_inpcb) { 1382 so = tp->t_inpcb->inp_socket; 1383 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1384 rt = in_pcbrtentry(tp->t_inpcb); 1385 #endif 1386 } 1387 #ifdef INET6 1388 else if (tp->t_in6pcb) { 1389 so = tp->t_in6pcb->in6p_socket; 1390 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH) 1391 #ifdef TCP6 1392 rt = NULL; 1393 #else 1394 rt = in6_pcbrtentry(tp->t_in6pcb); 1395 #endif 1396 #endif 1397 } 1398 #endif 1399 1400 /* 1401 * As per RFC1122, use the default MSS value, unless they 1402 * sent us an offer. Do not accept offers less than 32 bytes. 1403 */ 1404 mss = tcp_mssdflt; 1405 if (offer) 1406 mss = offer; 1407 mss = max(mss, 32); /* sanity */ 1408 tp->t_peermss = mss; 1409 mss -= tcp_optlen(tp); 1410 if (tp->t_inpcb) 1411 mss -= ip_optlen(tp->t_inpcb); 1412 #ifdef INET6 1413 else if (tp->t_in6pcb) 1414 mss -= ip6_optlen(tp->t_in6pcb); 1415 #endif 1416 1417 /* 1418 * If there's a pipesize, change the socket buffer to that size. 1419 * Make the socket buffer an integral number of MSS units. If 1420 * the MSS is larger than the socket buffer, artificially decrease 1421 * the MSS. 1422 */ 1423 #ifdef RTV_SPIPE 1424 if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0) 1425 bufsize = rt->rt_rmx.rmx_sendpipe; 1426 else 1427 #endif 1428 bufsize = so->so_snd.sb_hiwat; 1429 if (bufsize < mss) 1430 mss = bufsize; 1431 else { 1432 bufsize = roundup(bufsize, mss); 1433 if (bufsize > sb_max) 1434 bufsize = sb_max; 1435 (void) sbreserve(&so->so_snd, bufsize); 1436 } 1437 tp->t_segsz = mss; 1438 1439 #ifdef RTV_SSTHRESH 1440 if (rt != NULL && rt->rt_rmx.rmx_ssthresh) { 1441 /* 1442 * There's some sort of gateway or interface buffer 1443 * limit on the path. Use this to set the slow 1444 * start threshold, but set the threshold to no less 1445 * than 2 * MSS. 1446 */ 1447 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); 1448 } 1449 #endif 1450 } 1451 1452 /* 1453 * Processing necessary when a TCP connection is established. 1454 */ 1455 void 1456 tcp_established(tp) 1457 struct tcpcb *tp; 1458 { 1459 struct socket *so; 1460 #ifdef RTV_RPIPE 1461 struct rtentry *rt; 1462 #endif 1463 u_long bufsize; 1464 1465 so = NULL; 1466 rt = NULL; 1467 if (tp->t_inpcb) { 1468 so = tp->t_inpcb->inp_socket; 1469 #if defined(RTV_RPIPE) 1470 rt = in_pcbrtentry(tp->t_inpcb); 1471 #endif 1472 } 1473 #ifdef INET6 1474 else if (tp->t_in6pcb) { 1475 so = tp->t_in6pcb->in6p_socket; 1476 #if defined(RTV_RPIPE) 1477 #ifdef TCP6 1478 rt = NULL; 1479 #else 1480 rt = in6_pcbrtentry(tp->t_in6pcb); 1481 #endif 1482 #endif 1483 } 1484 #endif 1485 1486 tp->t_state = TCPS_ESTABLISHED; 1487 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle); 1488 1489 #ifdef RTV_RPIPE 1490 if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0) 1491 bufsize = rt->rt_rmx.rmx_recvpipe; 1492 else 1493 #endif 1494 bufsize = so->so_rcv.sb_hiwat; 1495 if (bufsize > tp->t_ourmss) { 1496 bufsize = roundup(bufsize, tp->t_ourmss); 1497 if (bufsize > sb_max) 1498 bufsize = sb_max; 1499 (void) sbreserve(&so->so_rcv, bufsize); 1500 } 1501 } 1502 1503 /* 1504 * Check if there's an initial rtt or rttvar. Convert from the 1505 * route-table units to scaled multiples of the slow timeout timer. 1506 * Called only during the 3-way handshake. 1507 */ 1508 void 1509 tcp_rmx_rtt(tp) 1510 struct tcpcb *tp; 1511 { 1512 #ifdef RTV_RTT 1513 struct rtentry *rt = NULL; 1514 int rtt; 1515 1516 if (tp->t_inpcb) 1517 rt = in_pcbrtentry(tp->t_inpcb); 1518 #ifdef INET6 1519 else if (tp->t_in6pcb) { 1520 #ifdef TCP6 1521 rt = NULL; 1522 #else 1523 rt = in6_pcbrtentry(tp->t_in6pcb); 1524 #endif 1525 } 1526 #endif 1527 if (rt == NULL) 1528 return; 1529 1530 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { 1531 /* 1532 * XXX The lock bit for MTU indicates that the value 1533 * is also a minimum value; this is subject to time. 1534 */ 1535 if (rt->rt_rmx.rmx_locks & RTV_RTT) 1536 TCPT_RANGESET(tp->t_rttmin, 1537 rtt / (RTM_RTTUNIT / PR_SLOWHZ), 1538 TCPTV_MIN, TCPTV_REXMTMAX); 1539 tp->t_srtt = rtt / 1540 ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2)); 1541 if (rt->rt_rmx.rmx_rttvar) { 1542 tp->t_rttvar = rt->rt_rmx.rmx_rttvar / 1543 ((RTM_RTTUNIT / PR_SLOWHZ) >> 1544 (TCP_RTTVAR_SHIFT + 2)); 1545 } else { 1546 /* Default variation is +- 1 rtt */ 1547 tp->t_rttvar = 1548 tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT); 1549 } 1550 TCPT_RANGESET(tp->t_rxtcur, 1551 ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2), 1552 tp->t_rttmin, TCPTV_REXMTMAX); 1553 } 1554 #endif 1555 } 1556 1557 tcp_seq tcp_iss_seq = 0; /* tcp initial seq # */ 1558 1559 /* 1560 * Get a new sequence value given a tcp control block 1561 */ 1562 tcp_seq 1563 tcp_new_iss(tp, len, addin) 1564 void *tp; 1565 u_long len; 1566 tcp_seq addin; 1567 { 1568 tcp_seq tcp_iss; 1569 1570 /* 1571 * Randomize. 1572 */ 1573 #if NRND > 0 1574 rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY); 1575 #else 1576 tcp_iss = random(); 1577 #endif 1578 1579 /* 1580 * If we were asked to add some amount to a known value, 1581 * we will take a random value obtained above, mask off the upper 1582 * bits, and add in the known value. We also add in a constant to 1583 * ensure that we are at least a certain distance from the original 1584 * value. 1585 * 1586 * This is used when an old connection is in timed wait 1587 * and we have a new one coming in, for instance. 1588 */ 1589 if (addin != 0) { 1590 #ifdef TCPISS_DEBUG 1591 printf("Random %08x, ", tcp_iss); 1592 #endif 1593 tcp_iss &= TCP_ISS_RANDOM_MASK; 1594 tcp_iss += addin + TCP_ISSINCR; 1595 #ifdef TCPISS_DEBUG 1596 printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss); 1597 #endif 1598 } else { 1599 tcp_iss &= TCP_ISS_RANDOM_MASK; 1600 tcp_iss += tcp_iss_seq; 1601 tcp_iss_seq += TCP_ISSINCR; 1602 #ifdef TCPISS_DEBUG 1603 printf("ISS %08x\n", tcp_iss); 1604 #endif 1605 } 1606 1607 if (tcp_compat_42) { 1608 /* 1609 * Limit it to the positive range for really old TCP 1610 * implementations. 1611 */ 1612 if (tcp_iss >= 0x80000000) 1613 tcp_iss &= 0x7fffffff; /* XXX */ 1614 } 1615 1616 return tcp_iss; 1617 } 1618 1619 #ifdef IPSEC 1620 /* compute ESP/AH header size for TCP, including outer IP header. */ 1621 size_t 1622 ipsec4_hdrsiz_tcp(tp) 1623 struct tcpcb *tp; 1624 { 1625 struct inpcb *inp; 1626 size_t hdrsiz; 1627 1628 /* XXX mapped addr case (tp->t_in6pcb) */ 1629 if (!tp || !tp->t_template || !(inp = tp->t_inpcb)) 1630 return 0; 1631 switch (tp->t_family) { 1632 case AF_INET: 1633 hdrsiz = ipsec4_hdrsiz(tp->t_template, inp); 1634 break; 1635 default: 1636 hdrsiz = 0; 1637 break; 1638 } 1639 1640 return hdrsiz; 1641 } 1642 1643 #if defined(INET6) && !defined(TCP6) 1644 size_t 1645 ipsec6_hdrsiz_tcp(tp) 1646 struct tcpcb *tp; 1647 { 1648 struct in6pcb *in6p; 1649 size_t hdrsiz; 1650 1651 if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb)) 1652 return 0; 1653 switch (tp->t_family) { 1654 case AF_INET6: 1655 hdrsiz = ipsec6_hdrsiz(tp->t_template, in6p); 1656 break; 1657 case AF_INET: 1658 /* mapped address case - tricky */ 1659 default: 1660 hdrsiz = 0; 1661 break; 1662 } 1663 1664 return hdrsiz; 1665 } 1666 #endif 1667 #endif /*IPSEC*/ 1668 1669 /* 1670 * Determine the length of the TCP options for this connection. 1671 * 1672 * XXX: What do we do for SACK, when we add that? Just reserve 1673 * all of the space? Otherwise we can't exactly be incrementing 1674 * cwnd by an amount that varies depending on the amount we last 1675 * had to SACK! 1676 */ 1677 1678 u_int 1679 tcp_optlen(tp) 1680 struct tcpcb *tp; 1681 { 1682 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 1683 (TF_REQ_TSTMP | TF_RCVD_TSTMP)) 1684 return TCPOLEN_TSTAMP_APPA; 1685 else 1686 return 0; 1687 } 1688