118 u_int hashsize; 119 u_int hashmask; 120 u_int bucket_limit; 121 u_int cache_count; 122 u_int cache_limit; 123 u_int rexmt_limit; 124 u_int hash_secret; 125 u_int next_reseed; 126 TAILQ_HEAD(, syncache) timerq[SYNCACHE_MAXREXMTS + 1]; 127 struct callout tt_timerq[SYNCACHE_MAXREXMTS + 1]; 128}; 129static struct tcp_syncache tcp_syncache; 130 131SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache"); 132 133SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RD, 134 &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache"); 135 136SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RD, 137 &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache"); 138 139SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD, 140 &tcp_syncache.cache_count, 0, "Current number of entries in syncache"); 141 142SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RD, 143 &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable"); 144 145SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW, 146 &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions"); 147 148static MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache"); 149 150#define SYNCACHE_HASH(inc, mask) \ 151 ((tcp_syncache.hash_secret ^ \ 152 (inc)->inc_faddr.s_addr ^ \ 153 ((inc)->inc_faddr.s_addr >> 16) ^ \ 154 (inc)->inc_fport ^ (inc)->inc_lport) & mask) 155 156#define SYNCACHE_HASH6(inc, mask) \ 157 ((tcp_syncache.hash_secret ^ \ 158 (inc)->inc6_faddr.s6_addr32[0] ^ \ 159 (inc)->inc6_faddr.s6_addr32[3] ^ \ 160 (inc)->inc_fport ^ (inc)->inc_lport) & mask) 161 162#define ENDPTS_EQ(a, b) ( \ 163 (a)->ie_fport == (b)->ie_fport && \ 164 (a)->ie_lport == (b)->ie_lport && \ 165 (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \ 166 (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \ 167) 168 169#define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0) 170 171#define SYNCACHE_TIMEOUT(sc, slot) do { \ 172 sc->sc_rxtslot = slot; \ 173 sc->sc_rxttime = ticks + TCPTV_RTOBASE * tcp_backoff[slot]; \ 174 TAILQ_INSERT_TAIL(&tcp_syncache.timerq[slot], sc, sc_timerq); \ 175 if (!callout_active(&tcp_syncache.tt_timerq[slot])) \ 176 callout_reset(&tcp_syncache.tt_timerq[slot], \ 177 TCPTV_RTOBASE * tcp_backoff[slot], \ 178 syncache_timer, (void *)((intptr_t)slot)); \ 179} while (0) 180 181static void 182syncache_free(struct syncache *sc) 183{ 184 struct rtentry *rt; 185 186 if (sc->sc_ipopts) 187 (void) m_free(sc->sc_ipopts); 188#ifdef INET6 189 if (sc->sc_inc.inc_isipv6) 190 rt = sc->sc_route6.ro_rt; 191 else 192#endif 193 rt = sc->sc_route.ro_rt; 194 if (rt != NULL) { 195 /* 196 * If this is the only reference to a protocol cloned 197 * route, remove it immediately. 198 */ 199 if (rt->rt_flags & RTF_WASCLONED && 200 (sc->sc_flags & SCF_KEEPROUTE) == 0 && 201 rt->rt_refcnt == 1) 202 rtrequest(RTM_DELETE, rt_key(rt), 203 rt->rt_gateway, rt_mask(rt), 204 rt->rt_flags, NULL); 205 RTFREE(rt); 206 } 207 zfree(tcp_syncache.zone, sc); 208} 209 210void 211syncache_init(void) 212{ 213 int i; 214 215 tcp_syncache.cache_count = 0; 216 tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE; 217 tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT; 218 tcp_syncache.cache_limit = 219 tcp_syncache.hashsize * tcp_syncache.bucket_limit; 220 tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS; 221 tcp_syncache.next_reseed = 0; 222 tcp_syncache.hash_secret = arc4random(); 223 224 TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize", 225 &tcp_syncache.hashsize); 226 TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit", 227 &tcp_syncache.cache_limit); 228 TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit", 229 &tcp_syncache.bucket_limit); 230 if (!powerof2(tcp_syncache.hashsize)) { 231 printf("WARNING: syncache hash size is not a power of 2.\n"); 232 tcp_syncache.hashsize = 512; /* safe default */ 233 } 234 tcp_syncache.hashmask = tcp_syncache.hashsize - 1; 235 236 /* Allocate the hash table. */ 237 MALLOC(tcp_syncache.hashbase, struct syncache_head *, 238 tcp_syncache.hashsize * sizeof(struct syncache_head), 239 M_SYNCACHE, M_WAITOK | M_ZERO); 240 241 /* Initialize the hash buckets. */ 242 for (i = 0; i < tcp_syncache.hashsize; i++) { 243 TAILQ_INIT(&tcp_syncache.hashbase[i].sch_bucket); 244 tcp_syncache.hashbase[i].sch_length = 0; 245 } 246 247 /* Initialize the timer queues. */ 248 for (i = 0; i <= SYNCACHE_MAXREXMTS; i++) { 249 TAILQ_INIT(&tcp_syncache.timerq[i]); 250 callout_init(&tcp_syncache.tt_timerq[i], 0); 251 } 252 253 /* 254 * Allocate the syncache entries. Allow the zone to allocate one 255 * more entry than cache limit, so a new entry can bump out an 256 * older one. 257 */ 258 tcp_syncache.cache_limit -= 1; 259 tcp_syncache.zone = zinit("syncache", sizeof(struct syncache), 260 tcp_syncache.cache_limit, ZONE_INTERRUPT, 0); 261} 262 263static void 264syncache_insert(sc, sch) 265 struct syncache *sc; 266 struct syncache_head *sch; 267{ 268 struct syncache *sc2; 269 int s, i; 270 271 /* 272 * Make sure that we don't overflow the per-bucket 273 * limit or the total cache size limit. 274 */ 275 s = splnet(); 276 if (sch->sch_length >= tcp_syncache.bucket_limit) { 277 /* 278 * The bucket is full, toss the oldest element. 279 */ 280 sc2 = TAILQ_FIRST(&sch->sch_bucket); 281 sc2->sc_tp->ts_recent = ticks; 282 syncache_drop(sc2, sch); 283 tcpstat.tcps_sc_bucketoverflow++; 284 } else if (tcp_syncache.cache_count >= tcp_syncache.cache_limit) { 285 /* 286 * The cache is full. Toss the oldest entry in the 287 * entire cache. This is the front entry in the 288 * first non-empty timer queue with the largest 289 * timeout value. 290 */ 291 for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 292 sc2 = TAILQ_FIRST(&tcp_syncache.timerq[i]); 293 if (sc2 != NULL) 294 break; 295 } 296 sc2->sc_tp->ts_recent = ticks; 297 syncache_drop(sc2, NULL); 298 tcpstat.tcps_sc_cacheoverflow++; 299 } 300 301 /* Initialize the entry's timer. */ 302 SYNCACHE_TIMEOUT(sc, 0); 303 304 /* Put it into the bucket. */ 305 TAILQ_INSERT_TAIL(&sch->sch_bucket, sc, sc_hash); 306 sch->sch_length++; 307 tcp_syncache.cache_count++; 308 tcpstat.tcps_sc_added++; 309 splx(s); 310} 311 312static void 313syncache_drop(sc, sch) 314 struct syncache *sc; 315 struct syncache_head *sch; 316{ 317 int s; 318 319 if (sch == NULL) { 320#ifdef INET6 321 if (sc->sc_inc.inc_isipv6) { 322 sch = &tcp_syncache.hashbase[ 323 SYNCACHE_HASH6(&sc->sc_inc, tcp_syncache.hashmask)]; 324 } else 325#endif 326 { 327 sch = &tcp_syncache.hashbase[ 328 SYNCACHE_HASH(&sc->sc_inc, tcp_syncache.hashmask)]; 329 } 330 } 331 332 s = splnet(); 333 334 TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash); 335 sch->sch_length--; 336 tcp_syncache.cache_count--; 337 338 TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], sc, sc_timerq); 339 if (TAILQ_EMPTY(&tcp_syncache.timerq[sc->sc_rxtslot])) 340 callout_stop(&tcp_syncache.tt_timerq[sc->sc_rxtslot]); 341 splx(s); 342 343 syncache_free(sc); 344} 345 346/* 347 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 348 * If we have retransmitted an entry the maximum number of times, expire it. 349 */ 350static void 351syncache_timer(xslot) 352 void *xslot; 353{ 354 intptr_t slot = (intptr_t)xslot; 355 struct syncache *sc, *nsc; 356 struct inpcb *inp; 357 int s; 358 359 s = splnet(); 360 if (callout_pending(&tcp_syncache.tt_timerq[slot]) || 361 !callout_active(&tcp_syncache.tt_timerq[slot])) { 362 splx(s); 363 return; 364 } 365 callout_deactivate(&tcp_syncache.tt_timerq[slot]); 366 367 nsc = TAILQ_FIRST(&tcp_syncache.timerq[slot]); 368 while (nsc != NULL) { 369 if (ticks < nsc->sc_rxttime) 370 break; 371 sc = nsc; 372 nsc = TAILQ_NEXT(sc, sc_timerq); 373 inp = sc->sc_tp->t_inpcb; 374 if (slot == SYNCACHE_MAXREXMTS || 375 slot >= tcp_syncache.rexmt_limit || 376 inp->inp_gencnt != sc->sc_inp_gencnt) { 377 syncache_drop(sc, NULL); 378 tcpstat.tcps_sc_stale++; 379 continue; 380 } 381 (void) syncache_respond(sc, NULL); 382 tcpstat.tcps_sc_retransmitted++; 383 TAILQ_REMOVE(&tcp_syncache.timerq[slot], sc, sc_timerq); 384 SYNCACHE_TIMEOUT(sc, slot + 1); 385 } 386 if (nsc != NULL) 387 callout_reset(&tcp_syncache.tt_timerq[slot], 388 nsc->sc_rxttime - ticks, syncache_timer, (void *)(slot)); 389 splx(s); 390} 391 392/* 393 * Find an entry in the syncache. 394 */ 395struct syncache * 396syncache_lookup(inc, schp) 397 struct in_conninfo *inc; 398 struct syncache_head **schp; 399{ 400 struct syncache *sc; 401 struct syncache_head *sch; 402 int s; 403 404#ifdef INET6 405 if (inc->inc_isipv6) { 406 sch = &tcp_syncache.hashbase[ 407 SYNCACHE_HASH6(inc, tcp_syncache.hashmask)]; 408 *schp = sch; 409 s = splnet(); 410 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 411 if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 412 splx(s); 413 return (sc); 414 } 415 } 416 splx(s); 417 } else 418#endif 419 { 420 sch = &tcp_syncache.hashbase[ 421 SYNCACHE_HASH(inc, tcp_syncache.hashmask)]; 422 *schp = sch; 423 s = splnet(); 424 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 425#ifdef INET6 426 if (sc->sc_inc.inc_isipv6) 427 continue; 428#endif 429 if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 430 splx(s); 431 return (sc); 432 } 433 } 434 splx(s); 435 } 436 return (NULL); 437} 438 439/* 440 * This function is called when we get a RST for a 441 * non-existent connection, so that we can see if the 442 * connection is in the syn cache. If it is, zap it. 443 */ 444void 445syncache_chkrst(inc, th) 446 struct in_conninfo *inc; 447 struct tcphdr *th; 448{ 449 struct syncache *sc; 450 struct syncache_head *sch; 451 452 sc = syncache_lookup(inc, &sch); 453 if (sc == NULL) 454 return; 455 /* 456 * If the RST bit is set, check the sequence number to see 457 * if this is a valid reset segment. 458 * RFC 793 page 37: 459 * In all states except SYN-SENT, all reset (RST) segments 460 * are validated by checking their SEQ-fields. A reset is 461 * valid if its sequence number is in the window. 462 * 463 * The sequence number in the reset segment is normally an 464 * echo of our outgoing acknowlegement numbers, but some hosts 465 * send a reset with the sequence number at the rightmost edge 466 * of our receive window, and we have to handle this case. 467 */ 468 if (SEQ_GEQ(th->th_seq, sc->sc_irs) && 469 SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) { 470 syncache_drop(sc, sch); 471 tcpstat.tcps_sc_reset++; 472 } 473} 474 475void 476syncache_badack(inc) 477 struct in_conninfo *inc; 478{ 479 struct syncache *sc; 480 struct syncache_head *sch; 481 482 sc = syncache_lookup(inc, &sch); 483 if (sc != NULL) { 484 syncache_drop(sc, sch); 485 tcpstat.tcps_sc_badack++; 486 } 487} 488 489void 490syncache_unreach(inc, th) 491 struct in_conninfo *inc; 492 struct tcphdr *th; 493{ 494 struct syncache *sc; 495 struct syncache_head *sch; 496 497 /* we are called at splnet() here */ 498 sc = syncache_lookup(inc, &sch); 499 if (sc == NULL) 500 return; 501 502 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 503 if (ntohl(th->th_seq) != sc->sc_iss) 504 return; 505 506 /* 507 * If we've rertransmitted 3 times and this is our second error, 508 * we remove the entry. Otherwise, we allow it to continue on. 509 * This prevents us from incorrectly nuking an entry during a 510 * spurious network outage. 511 * 512 * See tcp_notify(). 513 */ 514 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtslot < 3) { 515 sc->sc_flags |= SCF_UNREACH; 516 return; 517 } 518 syncache_drop(sc, sch); 519 tcpstat.tcps_sc_unreach++; 520} 521 522/* 523 * Build a new TCP socket structure from a syncache entry. 524 */ 525static struct socket * 526syncache_socket(sc, lso) 527 struct syncache *sc; 528 struct socket *lso; 529{ 530 struct inpcb *inp = NULL; 531 struct socket *so; 532 struct tcpcb *tp; 533 534 /* 535 * Ok, create the full blown connection, and set things up 536 * as they would have been set up if we had created the 537 * connection when the SYN arrived. If we can't create 538 * the connection, abort it. 539 */ 540 so = sonewconn(lso, SS_ISCONNECTED); 541 if (so == NULL) { 542 /* 543 * Drop the connection; we will send a RST if the peer 544 * retransmits the ACK, 545 */ 546 tcpstat.tcps_listendrop++; 547 goto abort; 548 } 549 550 inp = sotoinpcb(so); 551 552 /* 553 * Insert new socket into hash list. 554 */ 555 inp->inp_inc.inc_isipv6 = sc->sc_inc.inc_isipv6; 556#ifdef INET6 557 if (sc->sc_inc.inc_isipv6) { 558 inp->in6p_laddr = sc->sc_inc.inc6_laddr; 559 } else { 560 inp->inp_vflag &= ~INP_IPV6; 561 inp->inp_vflag |= INP_IPV4; 562#endif 563 inp->inp_laddr = sc->sc_inc.inc_laddr; 564#ifdef INET6 565 } 566#endif 567 inp->inp_lport = sc->sc_inc.inc_lport; 568 if (in_pcbinshash(inp) != 0) { 569 /* 570 * Undo the assignments above if we failed to 571 * put the PCB on the hash lists. 572 */ 573#ifdef INET6 574 if (sc->sc_inc.inc_isipv6) 575 inp->in6p_laddr = in6addr_any; 576 else 577#endif 578 inp->inp_laddr.s_addr = INADDR_ANY; 579 inp->inp_lport = 0; 580 goto abort; 581 } 582#ifdef IPSEC 583 /* copy old policy into new socket's */ 584 if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp)) 585 printf("syncache_expand: could not copy policy\n"); 586#endif 587#ifdef INET6 588 if (sc->sc_inc.inc_isipv6) { 589 struct inpcb *oinp = sotoinpcb(lso); 590 struct in6_addr laddr6; 591 struct sockaddr_in6 *sin6; 592 /* 593 * Inherit socket options from the listening socket. 594 * Note that in6p_inputopts are not (and should not be) 595 * copied, since it stores previously received options and is 596 * used to detect if each new option is different than the 597 * previous one and hence should be passed to a user. 598 * If we copied in6p_inputopts, a user would not be able to 599 * receive options just after calling the accept system call. 600 */ 601 inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS; 602 if (oinp->in6p_outputopts) 603 inp->in6p_outputopts = 604 ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT); 605 inp->in6p_route = sc->sc_route6; 606 sc->sc_route6.ro_rt = NULL; 607 608 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 609 M_SONAME, M_NOWAIT | M_ZERO); 610 if (sin6 == NULL) 611 goto abort; 612 sin6->sin6_family = AF_INET6; 613 sin6->sin6_len = sizeof(*sin6); 614 sin6->sin6_addr = sc->sc_inc.inc6_faddr; 615 sin6->sin6_port = sc->sc_inc.inc_fport; 616 laddr6 = inp->in6p_laddr; 617 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 618 inp->in6p_laddr = sc->sc_inc.inc6_laddr; 619 if (in6_pcbconnect(inp, (struct sockaddr *)sin6, &thread0)) { 620 inp->in6p_laddr = laddr6; 621 FREE(sin6, M_SONAME); 622 goto abort; 623 } 624 FREE(sin6, M_SONAME); 625 } else 626#endif 627 { 628 struct in_addr laddr; 629 struct sockaddr_in *sin; 630 631 inp->inp_options = ip_srcroute(); 632 if (inp->inp_options == NULL) { 633 inp->inp_options = sc->sc_ipopts; 634 sc->sc_ipopts = NULL; 635 } 636 inp->inp_route = sc->sc_route; 637 sc->sc_route.ro_rt = NULL; 638 639 MALLOC(sin, struct sockaddr_in *, sizeof *sin, 640 M_SONAME, M_NOWAIT | M_ZERO); 641 if (sin == NULL) 642 goto abort; 643 sin->sin_family = AF_INET; 644 sin->sin_len = sizeof(*sin); 645 sin->sin_addr = sc->sc_inc.inc_faddr; 646 sin->sin_port = sc->sc_inc.inc_fport; 647 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 648 laddr = inp->inp_laddr; 649 if (inp->inp_laddr.s_addr == INADDR_ANY) 650 inp->inp_laddr = sc->sc_inc.inc_laddr; 651 if (in_pcbconnect(inp, (struct sockaddr *)sin, &thread0)) { 652 inp->inp_laddr = laddr; 653 FREE(sin, M_SONAME); 654 goto abort; 655 } 656 FREE(sin, M_SONAME); 657 } 658 659 tp = intotcpcb(inp); 660 tp->t_state = TCPS_SYN_RECEIVED; 661 tp->iss = sc->sc_iss; 662 tp->irs = sc->sc_irs; 663 tcp_rcvseqinit(tp); 664 tcp_sendseqinit(tp); 665 tp->snd_wl1 = sc->sc_irs; 666 tp->rcv_up = sc->sc_irs + 1; 667 tp->rcv_wnd = sc->sc_wnd; 668 tp->rcv_adv += tp->rcv_wnd; 669 670 tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH|TF_NODELAY); 671 if (sc->sc_flags & SCF_NOOPT) 672 tp->t_flags |= TF_NOOPT; 673 if (sc->sc_flags & SCF_WINSCALE) { 674 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 675 tp->requested_s_scale = sc->sc_requested_s_scale; 676 tp->request_r_scale = sc->sc_request_r_scale; 677 } 678 if (sc->sc_flags & SCF_TIMESTAMP) { 679 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 680 tp->ts_recent = sc->sc_tsrecent; 681 tp->ts_recent_age = ticks; 682 } 683 if (sc->sc_flags & SCF_CC) { 684 /* 685 * Initialization of the tcpcb for transaction; 686 * set SND.WND = SEG.WND, 687 * initialize CCsend and CCrecv. 688 */ 689 tp->t_flags |= TF_REQ_CC|TF_RCVD_CC; 690 tp->cc_send = sc->sc_cc_send; 691 tp->cc_recv = sc->sc_cc_recv; 692 } 693 694 tcp_mss(tp, sc->sc_peer_mss); 695 696 /* 697 * If the SYN,ACK was retransmitted, reset cwnd to 1 segment. 698 */ 699 if (sc->sc_rxtslot != 0) 700 tp->snd_cwnd = tp->t_maxseg; 701 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 702 703 tcpstat.tcps_accepts++; 704 return (so); 705 706abort: 707 if (so != NULL) 708 (void) soabort(so); 709 return (NULL); 710} 711 712/* 713 * This function gets called when we receive an ACK for a 714 * socket in the LISTEN state. We look up the connection 715 * in the syncache, and if its there, we pull it out of 716 * the cache and turn it into a full-blown connection in 717 * the SYN-RECEIVED state. 718 */ 719int 720syncache_expand(inc, th, sop, m) 721 struct in_conninfo *inc; 722 struct tcphdr *th; 723 struct socket **sop; 724 struct mbuf *m; 725{ 726 struct syncache *sc; 727 struct syncache_head *sch; 728 struct socket *so; 729 730 sc = syncache_lookup(inc, &sch); 731 if (sc == NULL) { 732 /* 733 * There is no syncache entry, so see if this ACK is 734 * a returning syncookie. To do this, first: 735 * A. See if this socket has had a syncache entry dropped in 736 * the past. We don't want to accept a bogus syncookie 737 * if we've never received a SYN. 738 * B. check that the syncookie is valid. If it is, then 739 * cobble up a fake syncache entry, and return. 740 */ 741 if (!tcp_syncookies) 742 return (0); 743 sc = syncookie_lookup(inc, th, *sop); 744 if (sc == NULL) 745 return (0); 746 sch = NULL; 747 tcpstat.tcps_sc_recvcookie++; 748 } 749 750 /* 751 * If seg contains an ACK, but not for our SYN/ACK, send a RST. 752 */ 753 if (th->th_ack != sc->sc_iss + 1) 754 return (0); 755 756 so = syncache_socket(sc, *sop); 757 if (so == NULL) { 758#if 0 759resetandabort: 760 /* XXXjlemon check this - is this correct? */ 761 (void) tcp_respond(NULL, m, m, th, 762 th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK); 763#endif 764 m_freem(m); /* XXX only needed for above */ 765 tcpstat.tcps_sc_aborted++; 766 } else { 767 sc->sc_flags |= SCF_KEEPROUTE; 768 tcpstat.tcps_sc_completed++; 769 } 770 if (sch == NULL) 771 syncache_free(sc); 772 else 773 syncache_drop(sc, sch); 774 *sop = so; 775 return (1); 776} 777 778/* 779 * Given a LISTEN socket and an inbound SYN request, add 780 * this to the syn cache, and send back a segment: 781 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 782 * to the source. 783 * 784 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 785 * Doing so would require that we hold onto the data and deliver it 786 * to the application. However, if we are the target of a SYN-flood 787 * DoS attack, an attacker could send data which would eventually 788 * consume all available buffer space if it were ACKed. By not ACKing 789 * the data, we avoid this DoS scenario. 790 */ 791int 792syncache_add(inc, to, th, sop, m) 793 struct in_conninfo *inc; 794 struct tcpopt *to; 795 struct tcphdr *th; 796 struct socket **sop; 797 struct mbuf *m; 798{ 799 struct tcpcb *tp; 800 struct socket *so; 801 struct syncache *sc = NULL; 802 struct syncache_head *sch; 803 struct mbuf *ipopts = NULL; 804 struct rmxp_tao *taop; 805 int i, s, win; 806 807 so = *sop; 808 tp = sototcpcb(so); 809 810 /* 811 * Remember the IP options, if any. 812 */ 813#ifdef INET6 814 if (!inc->inc_isipv6) 815#endif 816 ipopts = ip_srcroute(); 817 818 /* 819 * See if we already have an entry for this connection. 820 * If we do, resend the SYN,ACK, and reset the retransmit timer. 821 * 822 * XXX 823 * should the syncache be re-initialized with the contents 824 * of the new SYN here (which may have different options?) 825 */ 826 sc = syncache_lookup(inc, &sch); 827 if (sc != NULL) { 828 tcpstat.tcps_sc_dupsyn++; 829 if (ipopts) { 830 /* 831 * If we were remembering a previous source route, 832 * forget it and use the new one we've been given. 833 */ 834 if (sc->sc_ipopts) 835 (void) m_free(sc->sc_ipopts); 836 sc->sc_ipopts = ipopts; 837 } 838 /* 839 * Update timestamp if present. 840 */ 841 if (sc->sc_flags & SCF_TIMESTAMP) 842 sc->sc_tsrecent = to->to_tsval; 843 /* 844 * PCB may have changed, pick up new values. 845 */ 846 sc->sc_tp = tp; 847 sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 848 if (syncache_respond(sc, m) == 0) { 849 s = splnet(); 850 TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], 851 sc, sc_timerq); 852 SYNCACHE_TIMEOUT(sc, sc->sc_rxtslot); 853 splx(s); 854 tcpstat.tcps_sndacks++; 855 tcpstat.tcps_sndtotal++; 856 } 857 *sop = NULL; 858 return (1); 859 } 860 861 sc = zalloc(tcp_syncache.zone); 862 if (sc == NULL) { 863 /* 864 * The zone allocator couldn't provide more entries. 865 * Treat this as if the cache was full; drop the oldest 866 * entry and insert the new one. 867 */ 868 s = splnet(); 869 for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 870 sc = TAILQ_FIRST(&tcp_syncache.timerq[i]); 871 if (sc != NULL) 872 break; 873 } 874 sc->sc_tp->ts_recent = ticks; 875 syncache_drop(sc, NULL); 876 splx(s); 877 tcpstat.tcps_sc_zonefail++; 878 sc = zalloc(tcp_syncache.zone); 879 if (sc == NULL) { 880 if (ipopts) 881 (void) m_free(ipopts); 882 return (0); 883 } 884 } 885 886 /* 887 * Fill in the syncache values. 888 */ 889 bzero(sc, sizeof(*sc)); 890 sc->sc_tp = tp; 891 sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 892 sc->sc_ipopts = ipopts; 893 sc->sc_inc.inc_fport = inc->inc_fport; 894 sc->sc_inc.inc_lport = inc->inc_lport; 895#ifdef INET6 896 sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 897 if (inc->inc_isipv6) { 898 sc->sc_inc.inc6_faddr = inc->inc6_faddr; 899 sc->sc_inc.inc6_laddr = inc->inc6_laddr; 900 sc->sc_route6.ro_rt = NULL; 901 } else 902#endif 903 { 904 sc->sc_inc.inc_faddr = inc->inc_faddr; 905 sc->sc_inc.inc_laddr = inc->inc_laddr; 906 sc->sc_route.ro_rt = NULL; 907 } 908 sc->sc_irs = th->th_seq; 909 if (tcp_syncookies) 910 sc->sc_iss = syncookie_generate(sc); 911 else 912 sc->sc_iss = arc4random(); 913 914 /* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */ 915 win = sbspace(&so->so_rcv); 916 win = imax(win, 0); 917 win = imin(win, TCP_MAXWIN); 918 sc->sc_wnd = win; 919 920 sc->sc_flags = 0; 921 sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0; 922 if (tcp_do_rfc1323) { 923 /* 924 * A timestamp received in a SYN makes 925 * it ok to send timestamp requests and replies. 926 */ 927 if (to->to_flags & TOF_TS) { 928 sc->sc_tsrecent = to->to_tsval; 929 sc->sc_flags |= SCF_TIMESTAMP; 930 } 931 if (to->to_flags & TOF_SCALE) { 932 int wscale = 0; 933 934 /* Compute proper scaling value from buffer space */ 935 while (wscale < TCP_MAX_WINSHIFT && 936 (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat) 937 wscale++; 938 sc->sc_request_r_scale = wscale; 939 sc->sc_requested_s_scale = to->to_requested_s_scale; 940 sc->sc_flags |= SCF_WINSCALE; 941 } 942 } 943 if (tcp_do_rfc1644) { 944 /* 945 * A CC or CC.new option received in a SYN makes 946 * it ok to send CC in subsequent segments. 947 */ 948 if (to->to_flags & (TOF_CC|TOF_CCNEW)) { 949 sc->sc_cc_recv = to->to_cc; 950 sc->sc_cc_send = CC_INC(tcp_ccgen); 951 sc->sc_flags |= SCF_CC; 952 } 953 } 954 if (tp->t_flags & TF_NOOPT) 955 sc->sc_flags = SCF_NOOPT; 956 957 /* 958 * XXX 959 * We have the option here of not doing TAO (even if the segment 960 * qualifies) and instead fall back to a normal 3WHS via the syncache. 961 * This allows us to apply synflood protection to TAO-qualifying SYNs 962 * also. However, there should be a hueristic to determine when to 963 * do this, and is not present at the moment. 964 */ 965 966 /* 967 * Perform TAO test on incoming CC (SEG.CC) option, if any. 968 * - compare SEG.CC against cached CC from the same host, if any. 969 * - if SEG.CC > chached value, SYN must be new and is accepted 970 * immediately: save new CC in the cache, mark the socket 971 * connected, enter ESTABLISHED state, turn on flag to 972 * send a SYN in the next segment. 973 * A virtual advertised window is set in rcv_adv to 974 * initialize SWS prevention. Then enter normal segment 975 * processing: drop SYN, process data and FIN. 976 * - otherwise do a normal 3-way handshake. 977 */ 978 taop = tcp_gettaocache(&sc->sc_inc); 979 if ((to->to_flags & TOF_CC) != 0) { 980 if (((tp->t_flags & TF_NOPUSH) != 0) && 981 sc->sc_flags & SCF_CC && 982 taop != NULL && taop->tao_cc != 0 && 983 CC_GT(to->to_cc, taop->tao_cc)) { 984 sc->sc_rxtslot = 0; 985 so = syncache_socket(sc, *sop); 986 if (so != NULL) { 987 sc->sc_flags |= SCF_KEEPROUTE; 988 taop->tao_cc = to->to_cc; 989 *sop = so; 990 } 991 syncache_free(sc); 992 return (so != NULL); 993 } 994 } else { 995 /* 996 * No CC option, but maybe CC.NEW: invalidate cached value. 997 */ 998 if (taop != NULL) 999 taop->tao_cc = 0; 1000 } 1001 /* 1002 * TAO test failed or there was no CC option, 1003 * do a standard 3-way handshake. 1004 */ 1005 if (syncache_respond(sc, m) == 0) { 1006 syncache_insert(sc, sch); 1007 tcpstat.tcps_sndacks++; 1008 tcpstat.tcps_sndtotal++; 1009 } else { 1010 syncache_free(sc); 1011 tcpstat.tcps_sc_dropped++; 1012 } 1013 *sop = NULL; 1014 return (1); 1015} 1016 1017static int 1018syncache_respond(sc, m) 1019 struct syncache *sc; 1020 struct mbuf *m; 1021{ 1022 u_int8_t *optp; 1023 int optlen, error; 1024 u_int16_t tlen, hlen, mssopt; 1025 struct ip *ip = NULL; 1026 struct rtentry *rt; 1027 struct tcphdr *th; 1028#ifdef INET6 1029 struct ip6_hdr *ip6 = NULL; 1030#endif 1031 1032#ifdef INET6 1033 if (sc->sc_inc.inc_isipv6) { 1034 rt = tcp_rtlookup6(&sc->sc_inc); 1035 if (rt != NULL) 1036 mssopt = rt->rt_ifp->if_mtu - 1037 (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 1038 else 1039 mssopt = tcp_v6mssdflt; 1040 hlen = sizeof(struct ip6_hdr); 1041 } else 1042#endif 1043 { 1044 rt = tcp_rtlookup(&sc->sc_inc); 1045 if (rt != NULL) 1046 mssopt = rt->rt_ifp->if_mtu - 1047 (sizeof(struct ip) + sizeof(struct tcphdr)); 1048 else 1049 mssopt = tcp_mssdflt; 1050 hlen = sizeof(struct ip); 1051 } 1052 1053 /* Compute the size of the TCP options. */ 1054 if (sc->sc_flags & SCF_NOOPT) { 1055 optlen = 0; 1056 } else { 1057 optlen = TCPOLEN_MAXSEG + 1058 ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) + 1059 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) + 1060 ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0); 1061 } 1062 tlen = hlen + sizeof(struct tcphdr) + optlen; 1063 1064 /* 1065 * XXX 1066 * assume that the entire packet will fit in a header mbuf 1067 */ 1068 KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small")); 1069 1070 /* 1071 * XXX shouldn't this reuse the mbuf if possible ? 1072 * Create the IP+TCP header from scratch. 1073 */ 1074 if (m) 1075 m_freem(m); 1076 1077 m = m_gethdr(M_DONTWAIT, MT_HEADER); 1078 if (m == NULL) 1079 return (ENOBUFS); 1080 m->m_data += max_linkhdr; 1081 m->m_len = tlen; 1082 m->m_pkthdr.len = tlen; 1083 m->m_pkthdr.rcvif = NULL; 1084 1085#ifdef IPSEC 1086 /* use IPsec policy on listening socket to send SYN,ACK */ 1087 if (ipsec_setsocket(m, sc->sc_tp->t_inpcb->inp_socket) != 0) { 1088 m_freem(m); 1089 return (ENOBUFS); 1090 } 1091#endif 1092 1093#ifdef INET6 1094 if (sc->sc_inc.inc_isipv6) { 1095 ip6 = mtod(m, struct ip6_hdr *); 1096 ip6->ip6_vfc = IPV6_VERSION; 1097 ip6->ip6_nxt = IPPROTO_TCP; 1098 ip6->ip6_src = sc->sc_inc.inc6_laddr; 1099 ip6->ip6_dst = sc->sc_inc.inc6_faddr; 1100 ip6->ip6_plen = htons(tlen - hlen); 1101 /* ip6_hlim is set after checksum */ 1102 /* ip6_flow = ??? */ 1103 1104 th = (struct tcphdr *)(ip6 + 1); 1105 } else 1106#endif 1107 { 1108 ip = mtod(m, struct ip *); 1109 ip->ip_v = IPVERSION; 1110 ip->ip_hl = sizeof(struct ip) >> 2; 1111 ip->ip_tos = 0; 1112 ip->ip_len = tlen; 1113 ip->ip_id = 0; 1114 ip->ip_off = 0; 1115 ip->ip_ttl = ip_defttl; 1116 ip->ip_sum = 0; 1117 ip->ip_p = IPPROTO_TCP; 1118 ip->ip_src = sc->sc_inc.inc_laddr; 1119 ip->ip_dst = sc->sc_inc.inc_faddr; 1120 1121 th = (struct tcphdr *)(ip + 1); 1122 } 1123 th->th_sport = sc->sc_inc.inc_lport; 1124 th->th_dport = sc->sc_inc.inc_fport; 1125 1126 th->th_seq = htonl(sc->sc_iss); 1127 th->th_ack = htonl(sc->sc_irs + 1); 1128 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 1129 th->th_x2 = 0; 1130 th->th_flags = TH_SYN|TH_ACK; 1131 th->th_win = htons(sc->sc_wnd); 1132 th->th_urp = 0; 1133 1134 /* Tack on the TCP options. */ 1135 if (optlen == 0) 1136 goto no_options; 1137 optp = (u_int8_t *)(th + 1); 1138 *optp++ = TCPOPT_MAXSEG; 1139 *optp++ = TCPOLEN_MAXSEG; 1140 *optp++ = (mssopt >> 8) & 0xff; 1141 *optp++ = mssopt & 0xff; 1142 1143 if (sc->sc_flags & SCF_WINSCALE) { 1144 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 1145 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 1146 sc->sc_request_r_scale); 1147 optp += 4; 1148 } 1149 1150 if (sc->sc_flags & SCF_TIMESTAMP) { 1151 u_int32_t *lp = (u_int32_t *)(optp); 1152 1153 /* Form timestamp option as shown in appendix A of RFC 1323. */ 1154 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 1155 *lp++ = htonl(ticks); 1156 *lp = htonl(sc->sc_tsrecent); 1157 optp += TCPOLEN_TSTAMP_APPA; 1158 } 1159 1160 /* 1161 * Send CC and CC.echo if we received CC from our peer. 1162 */ 1163 if (sc->sc_flags & SCF_CC) { 1164 u_int32_t *lp = (u_int32_t *)(optp); 1165 1166 *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC)); 1167 *lp++ = htonl(sc->sc_cc_send); 1168 *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO)); 1169 *lp = htonl(sc->sc_cc_recv); 1170 optp += TCPOLEN_CC_APPA * 2; 1171 } 1172no_options: 1173 1174#ifdef INET6 1175 if (sc->sc_inc.inc_isipv6) { 1176 struct route_in6 *ro6 = &sc->sc_route6; 1177 1178 th->th_sum = 0; 1179 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 1180 ip6->ip6_hlim = in6_selecthlim(NULL, 1181 ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL); 1182 error = ip6_output(m, NULL, ro6, 0, NULL, NULL); 1183 } else 1184#endif 1185 { 1186 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 1187 htons(tlen - hlen + IPPROTO_TCP)); 1188 m->m_pkthdr.csum_flags = CSUM_TCP; 1189 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1190 error = ip_output(m, sc->sc_ipopts, &sc->sc_route, 0, NULL); 1191 } 1192 return (error); 1193} 1194 1195/* 1196 * cookie layers: 1197 * 1198 * |. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .| 1199 * | peer iss | 1200 * | MD5(laddr,faddr,lport,fport,secret) |. . . . . . .| 1201 * | 0 |(A)| | 1202 * (A): peer mss index 1203 */ 1204 1205/* 1206 * The values below are chosen to minimize the size of the tcp_secret 1207 * table, as well as providing roughly a 4 second lifetime for the cookie. 1208 */ 1209 1210#define SYNCOOKIE_HASHSHIFT 2 /* log2(# of 32bit words from hash) */ 1211#define SYNCOOKIE_WNDBITS 7 /* exposed bits for window indexing */ 1212#define SYNCOOKIE_TIMESHIFT 5 /* scale ticks to window time units */ 1213 1214#define SYNCOOKIE_HASHMASK ((1 << SYNCOOKIE_HASHSHIFT) - 1) 1215#define SYNCOOKIE_WNDMASK ((1 << SYNCOOKIE_WNDBITS) - 1) 1216#define SYNCOOKIE_NSECRETS (1 << (SYNCOOKIE_WNDBITS - SYNCOOKIE_HASHSHIFT)) 1217#define SYNCOOKIE_TIMEOUT \ 1218 (hz * (1 << SYNCOOKIE_WNDBITS) / (1 << SYNCOOKIE_TIMESHIFT)) 1219#define SYNCOOKIE_DATAMASK ((3 << SYNCOOKIE_WNDBITS) | SYNCOOKIE_WNDMASK) 1220 1221static struct { 1222 u_int32_t ts_secbits; 1223 u_int ts_expire; 1224} tcp_secret[SYNCOOKIE_NSECRETS]; 1225 1226static int tcp_msstab[] = { 0, 536, 1460, 8960 }; 1227 1228static MD5_CTX syn_ctx; 1229 1230#define MD5Add(v) MD5Update(&syn_ctx, (u_char *)&v, sizeof(v)) 1231 1232/* 1233 * Consider the problem of a recreated (and retransmitted) cookie. If the 1234 * original SYN was accepted, the connection is established. The second 1235 * SYN is inflight, and if it arrives with an ISN that falls within the 1236 * receive window, the connection is killed. 1237 * 1238 * However, since cookies have other problems, this may not be worth 1239 * worrying about. 1240 */ 1241 1242static u_int32_t 1243syncookie_generate(struct syncache *sc) 1244{ 1245 u_int32_t md5_buffer[4]; 1246 u_int32_t data; 1247 int wnd, idx; 1248 1249 wnd = ((ticks << SYNCOOKIE_TIMESHIFT) / hz) & SYNCOOKIE_WNDMASK; 1250 idx = wnd >> SYNCOOKIE_HASHSHIFT; 1251 if (tcp_secret[idx].ts_expire < ticks) { 1252 tcp_secret[idx].ts_secbits = arc4random(); 1253 tcp_secret[idx].ts_expire = ticks + SYNCOOKIE_TIMEOUT; 1254 } 1255 for (data = sizeof(tcp_msstab) / sizeof(int) - 1; data > 0; data--) 1256 if (tcp_msstab[data] <= sc->sc_peer_mss) 1257 break; 1258 data = (data << SYNCOOKIE_WNDBITS) | wnd; 1259 data ^= sc->sc_irs; /* peer's iss */ 1260 MD5Init(&syn_ctx); 1261#ifdef INET6 1262 if (sc->sc_inc.inc_isipv6) { 1263 MD5Add(sc->sc_inc.inc6_laddr); 1264 MD5Add(sc->sc_inc.inc6_faddr); 1265 } else 1266#endif 1267 { 1268 MD5Add(sc->sc_inc.inc_laddr); 1269 MD5Add(sc->sc_inc.inc_faddr); 1270 } 1271 MD5Add(sc->sc_inc.inc_lport); 1272 MD5Add(sc->sc_inc.inc_fport); 1273 MD5Add(tcp_secret[idx].ts_secbits); 1274 MD5Final((u_char *)&md5_buffer, &syn_ctx); 1275 data ^= (md5_buffer[wnd & SYNCOOKIE_HASHMASK] & ~SYNCOOKIE_WNDMASK); 1276 return (data); 1277} 1278 1279static struct syncache * 1280syncookie_lookup(inc, th, so) 1281 struct in_conninfo *inc; 1282 struct tcphdr *th; 1283 struct socket *so; 1284{ 1285 u_int32_t md5_buffer[4]; 1286 struct syncache *sc; 1287 u_int32_t data; 1288 int wnd, idx; 1289 1290 data = (th->th_ack - 1) ^ (th->th_seq - 1); /* remove ISS */ 1291 wnd = data & SYNCOOKIE_WNDMASK; 1292 idx = wnd >> SYNCOOKIE_HASHSHIFT; 1293 if (tcp_secret[idx].ts_expire < ticks || 1294 sototcpcb(so)->ts_recent + SYNCOOKIE_TIMEOUT < ticks) 1295 return (NULL); 1296 MD5Init(&syn_ctx); 1297#ifdef INET6 1298 if (inc->inc_isipv6) { 1299 MD5Add(inc->inc6_laddr); 1300 MD5Add(inc->inc6_faddr); 1301 } else 1302#endif 1303 { 1304 MD5Add(inc->inc_laddr); 1305 MD5Add(inc->inc_faddr); 1306 } 1307 MD5Add(inc->inc_lport); 1308 MD5Add(inc->inc_fport); 1309 MD5Add(tcp_secret[idx].ts_secbits); 1310 MD5Final((u_char *)&md5_buffer, &syn_ctx); 1311 data ^= md5_buffer[wnd & SYNCOOKIE_HASHMASK]; 1312 if ((data & ~SYNCOOKIE_DATAMASK) != 0) 1313 return (NULL); 1314 data = data >> SYNCOOKIE_WNDBITS; 1315 1316 sc = zalloc(tcp_syncache.zone); 1317 if (sc == NULL) 1318 return (NULL); 1319 /* 1320 * Fill in the syncache values. 1321 * XXX duplicate code from syncache_add 1322 */ 1323 sc->sc_ipopts = NULL; 1324 sc->sc_inc.inc_fport = inc->inc_fport; 1325 sc->sc_inc.inc_lport = inc->inc_lport; 1326#ifdef INET6 1327 sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 1328 if (inc->inc_isipv6) { 1329 sc->sc_inc.inc6_faddr = inc->inc6_faddr; 1330 sc->sc_inc.inc6_laddr = inc->inc6_laddr; 1331 sc->sc_route6.ro_rt = NULL; 1332 } else 1333#endif 1334 { 1335 sc->sc_inc.inc_faddr = inc->inc_faddr; 1336 sc->sc_inc.inc_laddr = inc->inc_laddr; 1337 sc->sc_route.ro_rt = NULL; 1338 } 1339 sc->sc_irs = th->th_seq - 1; 1340 sc->sc_iss = th->th_ack - 1; 1341 wnd = sbspace(&so->so_rcv); 1342 wnd = imax(wnd, 0); 1343 wnd = imin(wnd, TCP_MAXWIN); 1344 sc->sc_wnd = wnd; 1345 sc->sc_flags = 0; 1346 sc->sc_rxtslot = 0; 1347 sc->sc_peer_mss = tcp_msstab[data]; 1348 return (sc); 1349}
|