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