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