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