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