1 /* $OpenBSD: tcp_subr.c,v 1.125 2013/10/24 11:31:43 mpi Exp $ */ 2 /* $NetBSD: tcp_subr.c,v 1.22 1996/02/13 23:44:00 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/timeout.h> 77 #include <sys/protosw.h> 78 #include <sys/kernel.h> 79 #include <sys/pool.h> 80 81 #include <net/route.h> 82 #include <net/if.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #include <netinet/in_pcb.h> 88 #include <netinet/ip_var.h> 89 #include <netinet/ip_icmp.h> 90 #include <netinet/tcp.h> 91 #include <netinet/tcp_fsm.h> 92 #include <netinet/tcp_seq.h> 93 #include <netinet/tcp_timer.h> 94 #include <netinet/tcp_var.h> 95 #include <netinet/tcpip.h> 96 #include <dev/rndvar.h> 97 98 #ifdef INET6 99 #include <netinet6/ip6protosw.h> 100 #endif /* INET6 */ 101 102 #include <crypto/md5.h> 103 104 /* patchable/settable parameters for tcp */ 105 int tcp_mssdflt = TCP_MSS; 106 int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 107 108 /* values controllable via sysctl */ 109 int tcp_do_rfc1323 = 1; 110 #ifdef TCP_SACK 111 int tcp_do_sack = 1; /* RFC 2018 selective ACKs */ 112 #endif 113 int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */ 114 #ifdef TCP_ECN 115 int tcp_do_ecn = 0; /* RFC3168 ECN enabled/disabled? */ 116 #endif 117 int tcp_do_rfc3390 = 2; /* Increase TCP's Initial Window to 10*mss */ 118 119 u_int32_t tcp_now = 1; 120 121 #ifndef TCBHASHSIZE 122 #define TCBHASHSIZE 128 123 #endif 124 int tcbhashsize = TCBHASHSIZE; 125 126 /* syn hash parameters */ 127 #define TCP_SYN_HASH_SIZE 293 128 #define TCP_SYN_BUCKET_SIZE 35 129 int tcp_syn_cache_size = TCP_SYN_HASH_SIZE; 130 int tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE; 131 int tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE; 132 struct syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE]; 133 134 int tcp_reass_limit = NMBCLUSTERS / 2; /* hardlimit for tcpqe_pool */ 135 #ifdef TCP_SACK 136 int tcp_sackhole_limit = 32*1024; /* hardlimit for sackhl_pool */ 137 #endif 138 139 struct pool tcpcb_pool; 140 struct pool tcpqe_pool; 141 #ifdef TCP_SACK 142 struct pool sackhl_pool; 143 #endif 144 145 struct tcpstat tcpstat; /* tcp statistics */ 146 tcp_seq tcp_iss; 147 148 /* 149 * Tcp initialization 150 */ 151 void 152 tcp_init() 153 { 154 tcp_iss = 1; /* wrong */ 155 pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", 156 NULL); 157 pool_init(&tcpqe_pool, sizeof(struct tcpqent), 0, 0, 0, "tcpqepl", 158 NULL); 159 pool_sethardlimit(&tcpqe_pool, tcp_reass_limit, NULL, 0); 160 #ifdef TCP_SACK 161 pool_init(&sackhl_pool, sizeof(struct sackhole), 0, 0, 0, "sackhlpl", 162 NULL); 163 pool_sethardlimit(&sackhl_pool, tcp_sackhole_limit, NULL, 0); 164 #endif /* TCP_SACK */ 165 in_pcbinit(&tcbtable, tcbhashsize); 166 167 #ifdef INET6 168 /* 169 * Since sizeof(struct ip6_hdr) > sizeof(struct ip), we 170 * do max length checks/computations only on the former. 171 */ 172 if (max_protohdr < (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) 173 max_protohdr = (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 174 if ((max_linkhdr + sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) > 175 MHLEN) 176 panic("tcp_init"); 177 178 icmp6_mtudisc_callback_register(tcp6_mtudisc_callback); 179 #endif /* INET6 */ 180 181 /* Initialize the compressed state engine. */ 182 syn_cache_init(); 183 184 /* Initialize timer state. */ 185 tcp_timer_init(); 186 } 187 188 /* 189 * Create template to be used to send tcp packets on a connection. 190 * Call after host entry created, allocates an mbuf and fills 191 * in a skeletal tcp/ip header, minimizing the amount of work 192 * necessary when the connection is used. 193 * 194 * To support IPv6 in addition to IPv4 and considering that the sizes of 195 * the IPv4 and IPv6 headers are not the same, we now use a separate pointer 196 * for the TCP header. Also, we made the former tcpiphdr header pointer 197 * into just an IP overlay pointer, with casting as appropriate for v6. rja 198 */ 199 struct mbuf * 200 tcp_template(tp) 201 struct tcpcb *tp; 202 { 203 struct inpcb *inp = tp->t_inpcb; 204 struct mbuf *m; 205 struct tcphdr *th; 206 207 if ((m = tp->t_template) == 0) { 208 m = m_get(M_DONTWAIT, MT_HEADER); 209 if (m == NULL) 210 return (0); 211 212 switch (tp->pf) { 213 case 0: /*default to PF_INET*/ 214 #ifdef INET 215 case AF_INET: 216 m->m_len = sizeof(struct ip); 217 break; 218 #endif /* INET */ 219 #ifdef INET6 220 case AF_INET6: 221 m->m_len = sizeof(struct ip6_hdr); 222 break; 223 #endif /* INET6 */ 224 } 225 m->m_len += sizeof (struct tcphdr); 226 227 /* 228 * The link header, network header, TCP header, and TCP options 229 * all must fit in this mbuf. For now, assume the worst case of 230 * TCP options size. Eventually, compute this from tp flags. 231 */ 232 if (m->m_len + MAX_TCPOPTLEN + max_linkhdr >= MHLEN) { 233 MCLGET(m, M_DONTWAIT); 234 if ((m->m_flags & M_EXT) == 0) { 235 m_free(m); 236 return (0); 237 } 238 } 239 } 240 241 switch(tp->pf) { 242 #ifdef INET 243 case AF_INET: 244 { 245 struct ipovly *ipovly; 246 247 ipovly = mtod(m, struct ipovly *); 248 249 bzero(ipovly->ih_x1, sizeof ipovly->ih_x1); 250 ipovly->ih_pr = IPPROTO_TCP; 251 ipovly->ih_len = htons(sizeof (struct tcphdr)); 252 ipovly->ih_src = inp->inp_laddr; 253 ipovly->ih_dst = inp->inp_faddr; 254 255 th = (struct tcphdr *)(mtod(m, caddr_t) + 256 sizeof(struct ip)); 257 } 258 break; 259 #endif /* INET */ 260 #ifdef INET6 261 case AF_INET6: 262 { 263 struct ip6_hdr *ip6; 264 265 ip6 = mtod(m, struct ip6_hdr *); 266 267 ip6->ip6_src = inp->inp_laddr6; 268 ip6->ip6_dst = inp->inp_faddr6; 269 ip6->ip6_flow = htonl(0x60000000) | 270 (inp->inp_flowinfo & IPV6_FLOWLABEL_MASK); 271 272 ip6->ip6_nxt = IPPROTO_TCP; 273 ip6->ip6_plen = htons(sizeof(struct tcphdr)); /*XXX*/ 274 ip6->ip6_hlim = in6_selecthlim(inp, NULL); /*XXX*/ 275 276 th = (struct tcphdr *)(mtod(m, caddr_t) + 277 sizeof(struct ip6_hdr)); 278 } 279 break; 280 #endif /* INET6 */ 281 } 282 283 th->th_sport = inp->inp_lport; 284 th->th_dport = inp->inp_fport; 285 th->th_seq = 0; 286 th->th_ack = 0; 287 th->th_x2 = 0; 288 th->th_off = 5; 289 th->th_flags = 0; 290 th->th_win = 0; 291 th->th_urp = 0; 292 th->th_sum = 0; 293 return (m); 294 } 295 296 /* 297 * Send a single message to the TCP at address specified by 298 * the given TCP/IP header. If m == 0, then we make a copy 299 * of the tcpiphdr at ti and send directly to the addressed host. 300 * This is used to force keep alive messages out using the TCP 301 * template for a connection tp->t_template. If flags are given 302 * then we send a message back to the TCP which originated the 303 * segment ti, and discard the mbuf containing it and any other 304 * attached mbufs. 305 * 306 * In any case the ack and sequence number of the transmitted 307 * segment are as specified by the parameters. 308 */ 309 #ifdef INET6 310 /* This function looks hairy, because it was so IPv4-dependent. */ 311 #endif /* INET6 */ 312 void 313 tcp_respond(struct tcpcb *tp, caddr_t template, struct tcphdr *th0, 314 tcp_seq ack, tcp_seq seq, int flags, u_int rtableid) 315 { 316 int tlen; 317 int win = 0; 318 struct mbuf *m = 0; 319 struct route *ro = 0; 320 struct tcphdr *th; 321 struct ip *ip; 322 struct ipovly *ih; 323 #ifdef INET6 324 struct ip6_hdr *ip6; 325 #endif 326 int af; /* af on wire */ 327 328 if (tp) { 329 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 330 /* 331 * If this is called with an unconnected 332 * socket/tp/pcb (tp->pf is 0), we lose. 333 */ 334 af = tp->pf; 335 336 /* 337 * The route/route6 distinction is meaningless 338 * unless you're allocating space or passing parameters. 339 */ 340 ro = &tp->t_inpcb->inp_route; 341 } else 342 af = (((struct ip *)template)->ip_v == 6) ? AF_INET6 : AF_INET; 343 344 m = m_gethdr(M_DONTWAIT, MT_HEADER); 345 if (m == NULL) 346 return; 347 m->m_data += max_linkhdr; 348 tlen = 0; 349 350 #define xchg(a,b,type) do { type t; t=a; a=b; b=t; } while (0) 351 switch (af) { 352 #ifdef INET6 353 case AF_INET6: 354 ip6 = mtod(m, struct ip6_hdr *); 355 th = (struct tcphdr *)(ip6 + 1); 356 tlen = sizeof(*ip6) + sizeof(*th); 357 if (th0) { 358 bcopy(template, ip6, sizeof(*ip6)); 359 bcopy(th0, th, sizeof(*th)); 360 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 361 } else { 362 bcopy(template, ip6, tlen); 363 } 364 break; 365 #endif /* INET6 */ 366 case AF_INET: 367 ip = mtod(m, struct ip *); 368 th = (struct tcphdr *)(ip + 1); 369 tlen = sizeof(*ip) + sizeof(*th); 370 if (th0) { 371 bcopy(template, ip, sizeof(*ip)); 372 bcopy(th0, th, sizeof(*th)); 373 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, u_int32_t); 374 } else { 375 bcopy(template, ip, tlen); 376 } 377 break; 378 } 379 if (th0) 380 xchg(th->th_dport, th->th_sport, u_int16_t); 381 else 382 flags = TH_ACK; 383 #undef xchg 384 385 m->m_len = tlen; 386 m->m_pkthdr.len = tlen; 387 m->m_pkthdr.rcvif = (struct ifnet *) 0; 388 th->th_seq = htonl(seq); 389 th->th_ack = htonl(ack); 390 th->th_x2 = 0; 391 th->th_off = sizeof (struct tcphdr) >> 2; 392 th->th_flags = flags; 393 if (tp) 394 win >>= tp->rcv_scale; 395 if (win > TCP_MAXWIN) 396 win = TCP_MAXWIN; 397 th->th_win = htons((u_int16_t)win); 398 th->th_urp = 0; 399 400 /* force routing domain */ 401 if (tp) 402 m->m_pkthdr.rdomain = tp->t_inpcb->inp_rtableid; 403 else 404 m->m_pkthdr.rdomain = rtableid; 405 406 switch (af) { 407 #ifdef INET6 408 case AF_INET6: 409 ip6->ip6_flow = htonl(0x60000000); 410 ip6->ip6_nxt = IPPROTO_TCP; 411 ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL, NULL); /*XXX*/ 412 ip6->ip6_plen = tlen - sizeof(struct ip6_hdr); 413 th->th_sum = 0; 414 th->th_sum = in6_cksum(m, IPPROTO_TCP, 415 sizeof(struct ip6_hdr), ip6->ip6_plen); 416 HTONS(ip6->ip6_plen); 417 ip6_output(m, tp ? tp->t_inpcb->inp_outputopts6 : NULL, 418 (struct route_in6 *)ro, 0, NULL, NULL, 419 tp ? tp->t_inpcb : NULL); 420 break; 421 #endif /* INET6 */ 422 case AF_INET: 423 ih = (struct ipovly *)ip; 424 bzero(ih->ih_x1, sizeof ih->ih_x1); 425 ih->ih_len = htons((u_short)tlen - sizeof(struct ip)); 426 427 /* 428 * There's no point deferring to hardware checksum processing 429 * here, as we only send a minimal TCP packet whose checksum 430 * we need to compute in any case. 431 */ 432 th->th_sum = 0; 433 th->th_sum = in_cksum(m, tlen); 434 ip->ip_len = htons(tlen); 435 ip->ip_ttl = ip_defttl; 436 ip_output(m, (void *)NULL, ro, ip_mtudisc ? IP_MTUDISC : 0, 437 (void *)NULL, tp ? tp->t_inpcb : (void *)NULL); 438 } 439 } 440 441 /* 442 * Create a new TCP control block, making an 443 * empty reassembly queue and hooking it to the argument 444 * protocol control block. 445 */ 446 struct tcpcb * 447 tcp_newtcpcb(struct inpcb *inp) 448 { 449 struct tcpcb *tp; 450 int i; 451 452 tp = pool_get(&tcpcb_pool, PR_NOWAIT|PR_ZERO); 453 if (tp == NULL) 454 return (NULL); 455 TAILQ_INIT(&tp->t_segq); 456 tp->t_maxseg = tcp_mssdflt; 457 tp->t_maxopd = 0; 458 459 TCP_INIT_DELACK(tp); 460 for (i = 0; i < TCPT_NTIMERS; i++) 461 TCP_TIMER_INIT(tp, i); 462 timeout_set(&tp->t_reap_to, tcp_reaper, tp); 463 464 #ifdef TCP_SACK 465 tp->sack_enable = tcp_do_sack; 466 #endif 467 tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 468 tp->t_inpcb = inp; 469 /* 470 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 471 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 472 * reasonable initial retransmit time. 473 */ 474 tp->t_srtt = TCPTV_SRTTBASE; 475 tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 476 (TCP_RTTVAR_SHIFT + TCP_RTT_BASE_SHIFT - 1); 477 tp->t_rttmin = TCPTV_MIN; 478 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 479 TCPTV_MIN, TCPTV_REXMTMAX); 480 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 481 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 482 483 tp->t_pmtud_mtu_sent = 0; 484 tp->t_pmtud_mss_acked = 0; 485 486 #ifdef INET6 487 /* we disallow IPv4 mapped address completely. */ 488 if ((inp->inp_flags & INP_IPV6) == 0) 489 tp->pf = PF_INET; 490 else 491 tp->pf = PF_INET6; 492 #else 493 tp->pf = PF_INET; 494 #endif 495 496 #ifdef INET6 497 if (inp->inp_flags & INP_IPV6) 498 inp->inp_ipv6.ip6_hlim = ip6_defhlim; 499 else 500 #endif /* INET6 */ 501 inp->inp_ip.ip_ttl = ip_defttl; 502 503 inp->inp_ppcb = (caddr_t)tp; 504 return (tp); 505 } 506 507 /* 508 * Drop a TCP connection, reporting 509 * the specified error. If connection is synchronized, 510 * then send a RST to peer. 511 */ 512 struct tcpcb * 513 tcp_drop(tp, errno) 514 struct tcpcb *tp; 515 int errno; 516 { 517 struct socket *so = tp->t_inpcb->inp_socket; 518 519 if (TCPS_HAVERCVDSYN(tp->t_state)) { 520 tp->t_state = TCPS_CLOSED; 521 (void) tcp_output(tp); 522 tcpstat.tcps_drops++; 523 } else 524 tcpstat.tcps_conndrops++; 525 if (errno == ETIMEDOUT && tp->t_softerror) 526 errno = tp->t_softerror; 527 so->so_error = errno; 528 return (tcp_close(tp)); 529 } 530 531 /* 532 * Close a TCP control block: 533 * discard all space held by the tcp 534 * discard internet protocol block 535 * wake up any sleepers 536 */ 537 struct tcpcb * 538 tcp_close(struct tcpcb *tp) 539 { 540 struct inpcb *inp = tp->t_inpcb; 541 struct socket *so = inp->inp_socket; 542 #ifdef TCP_SACK 543 struct sackhole *p, *q; 544 #endif 545 546 /* free the reassembly queue, if any */ 547 tcp_freeq(tp); 548 549 tcp_canceltimers(tp); 550 TCP_CLEAR_DELACK(tp); 551 syn_cache_cleanup(tp); 552 553 #ifdef TCP_SACK 554 /* Free SACK holes. */ 555 q = p = tp->snd_holes; 556 while (p != 0) { 557 q = p->next; 558 pool_put(&sackhl_pool, p); 559 p = q; 560 } 561 #endif 562 if (tp->t_template) 563 (void) m_free(tp->t_template); 564 565 tp->t_flags |= TF_DEAD; 566 timeout_add(&tp->t_reap_to, 0); 567 568 inp->inp_ppcb = 0; 569 soisdisconnected(so); 570 in_pcbdetach(inp); 571 return (NULL); 572 } 573 574 void 575 tcp_reaper(void *arg) 576 { 577 struct tcpcb *tp = arg; 578 int s; 579 580 s = splsoftnet(); 581 pool_put(&tcpcb_pool, tp); 582 splx(s); 583 tcpstat.tcps_closed++; 584 } 585 586 int 587 tcp_freeq(struct tcpcb *tp) 588 { 589 struct tcpqent *qe; 590 int rv = 0; 591 592 while ((qe = TAILQ_FIRST(&tp->t_segq)) != NULL) { 593 TAILQ_REMOVE(&tp->t_segq, qe, tcpqe_q); 594 m_freem(qe->tcpqe_m); 595 pool_put(&tcpqe_pool, qe); 596 rv = 1; 597 } 598 return (rv); 599 } 600 601 /* 602 * Compute proper scaling value for receiver window from buffer space 603 */ 604 605 void 606 tcp_rscale(struct tcpcb *tp, u_long hiwat) 607 { 608 tp->request_r_scale = 0; 609 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 610 TCP_MAXWIN << tp->request_r_scale < hiwat) 611 tp->request_r_scale++; 612 } 613 614 /* 615 * Notify a tcp user of an asynchronous error; 616 * store error as soft error, but wake up user 617 * (for now, won't do anything until can select for soft error). 618 */ 619 void 620 tcp_notify(inp, error) 621 struct inpcb *inp; 622 int error; 623 { 624 struct tcpcb *tp = intotcpcb(inp); 625 struct socket *so = inp->inp_socket; 626 627 /* 628 * Ignore some errors if we are hooked up. 629 * If connection hasn't completed, has retransmitted several times, 630 * and receives a second error, give up now. This is better 631 * than waiting a long time to establish a connection that 632 * can never complete. 633 */ 634 if (tp->t_state == TCPS_ESTABLISHED && 635 (error == EHOSTUNREACH || error == ENETUNREACH || 636 error == EHOSTDOWN)) { 637 return; 638 } else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 && 639 tp->t_rxtshift > 3 && tp->t_softerror) 640 so->so_error = error; 641 else 642 tp->t_softerror = error; 643 wakeup((caddr_t) &so->so_timeo); 644 sorwakeup(so); 645 sowwakeup(so); 646 } 647 648 #ifdef INET6 649 void 650 tcp6_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *d) 651 { 652 struct tcphdr th; 653 struct tcpcb *tp; 654 void (*notify)(struct inpcb *, int) = tcp_notify; 655 struct ip6_hdr *ip6; 656 const struct sockaddr_in6 *sa6_src = NULL; 657 struct sockaddr_in6 *sa6 = satosin6(sa); 658 struct inpcb *inp; 659 struct mbuf *m; 660 tcp_seq seq; 661 int off; 662 struct { 663 u_int16_t th_sport; 664 u_int16_t th_dport; 665 u_int32_t th_seq; 666 } *thp; 667 668 if (sa->sa_family != AF_INET6 || 669 sa->sa_len != sizeof(struct sockaddr_in6) || 670 IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 671 IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) 672 return; 673 if ((unsigned)cmd >= PRC_NCMDS) 674 return; 675 else if (cmd == PRC_QUENCH) { 676 /* 677 * Don't honor ICMP Source Quench messages meant for 678 * TCP connections. 679 */ 680 /* XXX there's no PRC_QUENCH in IPv6 */ 681 return; 682 } else if (PRC_IS_REDIRECT(cmd)) 683 notify = in_rtchange, d = NULL; 684 else if (cmd == PRC_MSGSIZE) 685 ; /* special code is present, see below */ 686 else if (cmd == PRC_HOSTDEAD) 687 d = NULL; 688 else if (inet6ctlerrmap[cmd] == 0) 689 return; 690 691 /* if the parameter is from icmp6, decode it. */ 692 if (d != NULL) { 693 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 694 m = ip6cp->ip6c_m; 695 ip6 = ip6cp->ip6c_ip6; 696 off = ip6cp->ip6c_off; 697 sa6_src = ip6cp->ip6c_src; 698 } else { 699 m = NULL; 700 ip6 = NULL; 701 sa6_src = &sa6_any; 702 } 703 704 if (ip6) { 705 /* 706 * XXX: We assume that when ip6 is non NULL, 707 * M and OFF are valid. 708 */ 709 710 /* check if we can safely examine src and dst ports */ 711 if (m->m_pkthdr.len < off + sizeof(*thp)) 712 return; 713 714 bzero(&th, sizeof(th)); 715 #ifdef DIAGNOSTIC 716 if (sizeof(*thp) > sizeof(th)) 717 panic("assumption failed in tcp6_ctlinput"); 718 #endif 719 m_copydata(m, off, sizeof(*thp), (caddr_t)&th); 720 721 /* 722 * Check to see if we have a valid TCP connection 723 * corresponding to the address in the ICMPv6 message 724 * payload. 725 */ 726 inp = in6_pcbhashlookup(&tcbtable, &sa6->sin6_addr, 727 th.th_dport, (struct in6_addr *)&sa6_src->sin6_addr, 728 th.th_sport, rdomain); 729 if (cmd == PRC_MSGSIZE) { 730 /* 731 * Depending on the value of "valid" and routing table 732 * size (mtudisc_{hi,lo}wat), we will: 733 * - recalcurate the new MTU and create the 734 * corresponding routing entry, or 735 * - ignore the MTU change notification. 736 */ 737 icmp6_mtudisc_update((struct ip6ctlparam *)d, inp != NULL); 738 return; 739 } 740 if (inp) { 741 seq = ntohl(th.th_seq); 742 if (inp->inp_socket && 743 (tp = intotcpcb(inp)) && 744 SEQ_GEQ(seq, tp->snd_una) && 745 SEQ_LT(seq, tp->snd_max)) 746 notify(inp, inet6ctlerrmap[cmd]); 747 } else if (syn_cache_count && 748 (inet6ctlerrmap[cmd] == EHOSTUNREACH || 749 inet6ctlerrmap[cmd] == ENETUNREACH || 750 inet6ctlerrmap[cmd] == EHOSTDOWN)) 751 syn_cache_unreach((struct sockaddr *)sa6_src, 752 sa, &th, rdomain); 753 } else { 754 (void) in6_pcbnotify(&tcbtable, sa6, 0, 755 sa6_src, 0, rdomain, cmd, NULL, notify); 756 } 757 } 758 #endif 759 760 void * 761 tcp_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) 762 { 763 struct ip *ip = v; 764 struct tcphdr *th; 765 struct tcpcb *tp; 766 struct inpcb *inp; 767 struct in_addr faddr; 768 tcp_seq seq; 769 u_int mtu; 770 void (*notify)(struct inpcb *, int) = tcp_notify; 771 int errno; 772 773 if (sa->sa_family != AF_INET) 774 return NULL; 775 faddr = satosin(sa)->sin_addr; 776 if (faddr.s_addr == INADDR_ANY) 777 return NULL; 778 779 if ((unsigned)cmd >= PRC_NCMDS) 780 return NULL; 781 errno = inetctlerrmap[cmd]; 782 if (cmd == PRC_QUENCH) 783 /* 784 * Don't honor ICMP Source Quench messages meant for 785 * TCP connections. 786 */ 787 return NULL; 788 else if (PRC_IS_REDIRECT(cmd)) 789 notify = in_rtchange, ip = 0; 790 else if (cmd == PRC_MSGSIZE && ip_mtudisc && ip) { 791 /* 792 * Verify that the packet in the icmp payload refers 793 * to an existing TCP connection. 794 */ 795 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 796 seq = ntohl(th->th_seq); 797 inp = in_pcbhashlookup(&tcbtable, 798 ip->ip_dst, th->th_dport, ip->ip_src, th->th_sport, 799 rdomain); 800 if (inp && (tp = intotcpcb(inp)) && 801 SEQ_GEQ(seq, tp->snd_una) && 802 SEQ_LT(seq, tp->snd_max)) { 803 struct icmp *icp; 804 icp = (struct icmp *)((caddr_t)ip - 805 offsetof(struct icmp, icmp_ip)); 806 807 /* 808 * If the ICMP message advertises a Next-Hop MTU 809 * equal or larger than the maximum packet size we have 810 * ever sent, drop the message. 811 */ 812 mtu = (u_int)ntohs(icp->icmp_nextmtu); 813 if (mtu >= tp->t_pmtud_mtu_sent) 814 return NULL; 815 if (mtu >= tcp_hdrsz(tp) + tp->t_pmtud_mss_acked) { 816 /* 817 * Calculate new MTU, and create corresponding 818 * route (traditional PMTUD). 819 */ 820 tp->t_flags &= ~TF_PMTUD_PEND; 821 icmp_mtudisc(icp, inp->inp_rtableid); 822 } else { 823 /* 824 * Record the information got in the ICMP 825 * message; act on it later. 826 * If we had already recorded an ICMP message, 827 * replace the old one only if the new message 828 * refers to an older TCP segment 829 */ 830 if (tp->t_flags & TF_PMTUD_PEND) { 831 if (SEQ_LT(tp->t_pmtud_th_seq, seq)) 832 return NULL; 833 } else 834 tp->t_flags |= TF_PMTUD_PEND; 835 tp->t_pmtud_th_seq = seq; 836 tp->t_pmtud_nextmtu = icp->icmp_nextmtu; 837 tp->t_pmtud_ip_len = icp->icmp_ip.ip_len; 838 tp->t_pmtud_ip_hl = icp->icmp_ip.ip_hl; 839 return NULL; 840 } 841 } else { 842 /* ignore if we don't have a matching connection */ 843 return NULL; 844 } 845 notify = tcp_mtudisc, ip = 0; 846 } else if (cmd == PRC_MTUINC) 847 notify = tcp_mtudisc_increase, ip = 0; 848 else if (cmd == PRC_HOSTDEAD) 849 ip = 0; 850 else if (errno == 0) 851 return NULL; 852 853 if (ip) { 854 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 855 inp = in_pcbhashlookup(&tcbtable, 856 ip->ip_dst, th->th_dport, ip->ip_src, th->th_sport, 857 rdomain); 858 if (inp) { 859 seq = ntohl(th->th_seq); 860 if (inp->inp_socket && 861 (tp = intotcpcb(inp)) && 862 SEQ_GEQ(seq, tp->snd_una) && 863 SEQ_LT(seq, tp->snd_max)) 864 notify(inp, errno); 865 } else if (syn_cache_count && 866 (inetctlerrmap[cmd] == EHOSTUNREACH || 867 inetctlerrmap[cmd] == ENETUNREACH || 868 inetctlerrmap[cmd] == EHOSTDOWN)) { 869 struct sockaddr_in sin; 870 871 bzero(&sin, sizeof(sin)); 872 sin.sin_len = sizeof(sin); 873 sin.sin_family = AF_INET; 874 sin.sin_port = th->th_sport; 875 sin.sin_addr = ip->ip_src; 876 syn_cache_unreach((struct sockaddr *)&sin, 877 sa, th, rdomain); 878 } 879 } else 880 in_pcbnotifyall(&tcbtable, sa, rdomain, errno, notify); 881 882 return NULL; 883 } 884 885 886 #ifdef INET6 887 /* 888 * Path MTU Discovery handlers. 889 */ 890 void 891 tcp6_mtudisc_callback(sin6, rdomain) 892 struct sockaddr_in6 *sin6; 893 u_int rdomain; 894 { 895 (void) in6_pcbnotify(&tcbtable, sin6, 0, 896 &sa6_any, 0, rdomain, PRC_MSGSIZE, NULL, tcp_mtudisc); 897 } 898 #endif /* INET6 */ 899 900 /* 901 * On receipt of path MTU corrections, flush old route and replace it 902 * with the new one. Retransmit all unacknowledged packets, to ensure 903 * that all packets will be received. 904 */ 905 void 906 tcp_mtudisc(inp, errno) 907 struct inpcb *inp; 908 int errno; 909 { 910 struct tcpcb *tp = intotcpcb(inp); 911 struct rtentry *rt = in_pcbrtentry(inp); 912 int change = 0; 913 914 if (tp != 0) { 915 int orig_maxseg = tp->t_maxseg; 916 if (rt != 0) { 917 /* 918 * If this was not a host route, remove and realloc. 919 */ 920 if ((rt->rt_flags & RTF_HOST) == 0) { 921 in_rtchange(inp, errno); 922 if ((rt = in_pcbrtentry(inp)) == 0) 923 return; 924 } 925 if (orig_maxseg != tp->t_maxseg || 926 (rt->rt_rmx.rmx_locks & RTV_MTU)) 927 change = 1; 928 } 929 tcp_mss(tp, -1); 930 931 /* 932 * Resend unacknowledged packets 933 */ 934 tp->snd_nxt = tp->snd_una; 935 if (change || errno > 0) 936 tcp_output(tp); 937 } 938 } 939 940 void 941 tcp_mtudisc_increase(inp, errno) 942 struct inpcb *inp; 943 int errno; 944 { 945 struct tcpcb *tp = intotcpcb(inp); 946 struct rtentry *rt = in_pcbrtentry(inp); 947 948 if (tp != 0 && rt != 0) { 949 /* 950 * If this was a host route, remove and realloc. 951 */ 952 if (rt->rt_flags & RTF_HOST) 953 in_rtchange(inp, errno); 954 955 /* also takes care of congestion window */ 956 tcp_mss(tp, -1); 957 } 958 } 959 960 #define TCP_ISS_CONN_INC 4096 961 int tcp_secret_init; 962 u_char tcp_secret[16]; 963 MD5_CTX tcp_secret_ctx; 964 965 void 966 tcp_set_iss_tsm(struct tcpcb *tp) 967 { 968 MD5_CTX ctx; 969 u_int32_t digest[4]; 970 971 if (tcp_secret_init == 0) { 972 arc4random_buf(tcp_secret, sizeof(tcp_secret)); 973 MD5Init(&tcp_secret_ctx); 974 MD5Update(&tcp_secret_ctx, tcp_secret, sizeof(tcp_secret)); 975 tcp_secret_init = 1; 976 } 977 ctx = tcp_secret_ctx; 978 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_lport, sizeof(u_short)); 979 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_fport, sizeof(u_short)); 980 if (tp->pf == AF_INET6) { 981 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_laddr6, 982 sizeof(struct in6_addr)); 983 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_faddr6, 984 sizeof(struct in6_addr)); 985 } else { 986 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_laddr, 987 sizeof(struct in_addr)); 988 MD5Update(&ctx, (char *)&tp->t_inpcb->inp_faddr, 989 sizeof(struct in_addr)); 990 } 991 MD5Final((u_char *)digest, &ctx); 992 tcp_iss += TCP_ISS_CONN_INC; 993 tp->iss = digest[0] + tcp_iss; 994 tp->ts_modulate = digest[1]; 995 } 996 997 #ifdef TCP_SIGNATURE 998 int 999 tcp_signature_tdb_attach() 1000 { 1001 return (0); 1002 } 1003 1004 int 1005 tcp_signature_tdb_init(tdbp, xsp, ii) 1006 struct tdb *tdbp; 1007 struct xformsw *xsp; 1008 struct ipsecinit *ii; 1009 { 1010 if ((ii->ii_authkeylen < 1) || (ii->ii_authkeylen > 80)) 1011 return (EINVAL); 1012 1013 tdbp->tdb_amxkey = malloc(ii->ii_authkeylen, M_XDATA, M_NOWAIT); 1014 if (tdbp->tdb_amxkey == NULL) 1015 return (ENOMEM); 1016 bcopy(ii->ii_authkey, tdbp->tdb_amxkey, ii->ii_authkeylen); 1017 tdbp->tdb_amxkeylen = ii->ii_authkeylen; 1018 1019 return (0); 1020 } 1021 1022 int 1023 tcp_signature_tdb_zeroize(tdbp) 1024 struct tdb *tdbp; 1025 { 1026 if (tdbp->tdb_amxkey) { 1027 explicit_bzero(tdbp->tdb_amxkey, tdbp->tdb_amxkeylen); 1028 free(tdbp->tdb_amxkey, M_XDATA); 1029 tdbp->tdb_amxkey = NULL; 1030 } 1031 1032 return (0); 1033 } 1034 1035 int 1036 tcp_signature_tdb_input(m, tdbp, skip, protoff) 1037 struct mbuf *m; 1038 struct tdb *tdbp; 1039 int skip, protoff; 1040 { 1041 return (0); 1042 } 1043 1044 int 1045 tcp_signature_tdb_output(m, tdbp, mp, skip, protoff) 1046 struct mbuf *m; 1047 struct tdb *tdbp; 1048 struct mbuf **mp; 1049 int skip, protoff; 1050 { 1051 return (EINVAL); 1052 } 1053 1054 int 1055 tcp_signature_apply(fstate, data, len) 1056 caddr_t fstate; 1057 caddr_t data; 1058 unsigned int len; 1059 { 1060 MD5Update((MD5_CTX *)fstate, (char *)data, len); 1061 return 0; 1062 } 1063 1064 int 1065 tcp_signature(struct tdb *tdb, int af, struct mbuf *m, struct tcphdr *th, 1066 int iphlen, int doswap, char *sig) 1067 { 1068 MD5_CTX ctx; 1069 int len; 1070 struct tcphdr th0; 1071 1072 MD5Init(&ctx); 1073 1074 switch(af) { 1075 case 0: 1076 #ifdef INET 1077 case AF_INET: { 1078 struct ippseudo ippseudo; 1079 struct ip *ip; 1080 1081 ip = mtod(m, struct ip *); 1082 1083 ippseudo.ippseudo_src = ip->ip_src; 1084 ippseudo.ippseudo_dst = ip->ip_dst; 1085 ippseudo.ippseudo_pad = 0; 1086 ippseudo.ippseudo_p = IPPROTO_TCP; 1087 ippseudo.ippseudo_len = htons(m->m_pkthdr.len - iphlen); 1088 1089 MD5Update(&ctx, (char *)&ippseudo, 1090 sizeof(struct ippseudo)); 1091 break; 1092 } 1093 #endif 1094 #ifdef INET6 1095 case AF_INET6: { 1096 struct ip6_hdr_pseudo ip6pseudo; 1097 struct ip6_hdr *ip6; 1098 1099 ip6 = mtod(m, struct ip6_hdr *); 1100 bzero(&ip6pseudo, sizeof(ip6pseudo)); 1101 ip6pseudo.ip6ph_src = ip6->ip6_src; 1102 ip6pseudo.ip6ph_dst = ip6->ip6_dst; 1103 in6_clearscope(&ip6pseudo.ip6ph_src); 1104 in6_clearscope(&ip6pseudo.ip6ph_dst); 1105 ip6pseudo.ip6ph_nxt = IPPROTO_TCP; 1106 ip6pseudo.ip6ph_len = htonl(m->m_pkthdr.len - iphlen); 1107 1108 MD5Update(&ctx, (char *)&ip6pseudo, 1109 sizeof(ip6pseudo)); 1110 break; 1111 } 1112 #endif 1113 } 1114 1115 th0 = *th; 1116 th0.th_sum = 0; 1117 1118 if (doswap) { 1119 HTONL(th0.th_seq); 1120 HTONL(th0.th_ack); 1121 HTONS(th0.th_win); 1122 HTONS(th0.th_urp); 1123 } 1124 MD5Update(&ctx, (char *)&th0, sizeof(th0)); 1125 1126 len = m->m_pkthdr.len - iphlen - th->th_off * sizeof(uint32_t); 1127 1128 if (len > 0 && 1129 m_apply(m, iphlen + th->th_off * sizeof(uint32_t), len, 1130 tcp_signature_apply, (caddr_t)&ctx)) 1131 return (-1); 1132 1133 MD5Update(&ctx, tdb->tdb_amxkey, tdb->tdb_amxkeylen); 1134 MD5Final(sig, &ctx); 1135 1136 return (0); 1137 } 1138 #endif /* TCP_SIGNATURE */ 1139