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