186764Sjlemon/*- 2141063Srwatson * Copyright (c) 2001 McAfee, Inc. 3253210Sandre * Copyright (c) 2006,2013 Andre Oppermann, Internet Business Solutions AG 486764Sjlemon * All rights reserved. 586764Sjlemon * 686764Sjlemon * This software was developed for the FreeBSD Project by Jonathan Lemon 7141063Srwatson * and McAfee Research, the Security Research Division of McAfee, Inc. under 8141063Srwatson * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9253210Sandre * DARPA CHATS research program. [2001 McAfee, Inc.] 1086764Sjlemon * 1186764Sjlemon * Redistribution and use in source and binary forms, with or without 1286764Sjlemon * modification, are permitted provided that the following conditions 1386764Sjlemon * are met: 1486764Sjlemon * 1. Redistributions of source code must retain the above copyright 1586764Sjlemon * notice, this list of conditions and the following disclaimer. 1686764Sjlemon * 2. Redistributions in binary form must reproduce the above copyright 1786764Sjlemon * notice, this list of conditions and the following disclaimer in the 1886764Sjlemon * documentation and/or other materials provided with the distribution. 1986764Sjlemon * 2086764Sjlemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2186764Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2286764Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2386764Sjlemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2486764Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2586764Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2686764Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2786764Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2886764Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2986764Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3086764Sjlemon * SUCH DAMAGE. 3186764Sjlemon */ 3286764Sjlemon 33172467Ssilby#include <sys/cdefs.h> 34172467Ssilby__FBSDID("$FreeBSD: stable/10/sys/netinet/tcp_syncache.c 324520 2017-10-11 06:28:46Z sephe $"); 35172467Ssilby 36125680Sbms#include "opt_inet.h" 3786764Sjlemon#include "opt_inet6.h" 3886764Sjlemon#include "opt_ipsec.h" 39222748Srwatson#include "opt_pcbgroup.h" 4086764Sjlemon 4186764Sjlemon#include <sys/param.h> 4286764Sjlemon#include <sys/systm.h> 4386764Sjlemon#include <sys/kernel.h> 4486764Sjlemon#include <sys/sysctl.h> 45174775Sru#include <sys/limits.h> 46159695Sandre#include <sys/lock.h> 47159695Sandre#include <sys/mutex.h> 4886764Sjlemon#include <sys/malloc.h> 4986764Sjlemon#include <sys/mbuf.h> 5086764Sjlemon#include <sys/proc.h> /* for proc0 declaration */ 5186764Sjlemon#include <sys/random.h> 5286764Sjlemon#include <sys/socket.h> 5386764Sjlemon#include <sys/socketvar.h> 54169685Sandre#include <sys/syslog.h> 55182056Sbz#include <sys/ucred.h> 5686764Sjlemon 57253210Sandre#include <sys/md5.h> 58253210Sandre#include <crypto/siphash/siphash.h> 59253210Sandre 60162278Sandre#include <vm/uma.h> 61162278Sandre 6286764Sjlemon#include <net/if.h> 6386764Sjlemon#include <net/route.h> 64195699Srwatson#include <net/vnet.h> 6586764Sjlemon 6686764Sjlemon#include <netinet/in.h> 6786764Sjlemon#include <netinet/in_systm.h> 6886764Sjlemon#include <netinet/ip.h> 6986764Sjlemon#include <netinet/in_var.h> 7086764Sjlemon#include <netinet/in_pcb.h> 7186764Sjlemon#include <netinet/ip_var.h> 72152592Sandre#include <netinet/ip_options.h> 7386764Sjlemon#ifdef INET6 7486764Sjlemon#include <netinet/ip6.h> 7586764Sjlemon#include <netinet/icmp6.h> 7686764Sjlemon#include <netinet6/nd6.h> 7786764Sjlemon#include <netinet6/ip6_var.h> 7886764Sjlemon#include <netinet6/in6_pcb.h> 7986764Sjlemon#endif 8086764Sjlemon#include <netinet/tcp.h> 81292823Spkelsey#ifdef TCP_RFC7413 82292823Spkelsey#include <netinet/tcp_fastopen.h> 83292823Spkelsey#endif 8486764Sjlemon#include <netinet/tcp_fsm.h> 8586764Sjlemon#include <netinet/tcp_seq.h> 8686764Sjlemon#include <netinet/tcp_timer.h> 8786764Sjlemon#include <netinet/tcp_var.h> 88171605Ssilby#include <netinet/tcp_syncache.h> 8986764Sjlemon#ifdef INET6 9086764Sjlemon#include <netinet6/tcp6_var.h> 9186764Sjlemon#endif 92237263Snp#ifdef TCP_OFFLOAD 93237263Snp#include <netinet/toecore.h> 94237263Snp#endif 9586764Sjlemon 96171167Sgnn#ifdef IPSEC 97105199Ssam#include <netipsec/ipsec.h> 98105199Ssam#ifdef INET6 99105199Ssam#include <netipsec/ipsec6.h> 100105199Ssam#endif 101105199Ssam#include <netipsec/key.h> 102171167Sgnn#endif /*IPSEC*/ 103105199Ssam 10486764Sjlemon#include <machine/in_cksum.h> 10586764Sjlemon 106163606Srwatson#include <security/mac/mac_framework.h> 107163606Srwatson 108215701Sdimstatic VNET_DEFINE(int, tcp_syncookies) = 1; 109195727Srwatson#define V_tcp_syncookies VNET(tcp_syncookies) 110195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_RW, 111195699Srwatson &VNET_NAME(tcp_syncookies), 0, 11288180Sjlemon "Use TCP SYN cookies if the syncache overflows"); 11388180Sjlemon 114215701Sdimstatic VNET_DEFINE(int, tcp_syncookiesonly) = 0; 115207369Sbz#define V_tcp_syncookiesonly VNET(tcp_syncookiesonly) 116195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, syncookies_only, CTLFLAG_RW, 117195699Srwatson &VNET_NAME(tcp_syncookiesonly), 0, 118162277Sandre "Use only TCP SYN cookies"); 119162277Sandre 120237263Snp#ifdef TCP_OFFLOAD 121237263Snp#define ADDED_BY_TOE(sc) ((sc)->sc_tod != NULL) 122174704Skmacy#endif 123174704Skmacy 12486764Sjlemonstatic void syncache_drop(struct syncache *, struct syncache_head *); 12586764Sjlemonstatic void syncache_free(struct syncache *); 12688180Sjlemonstatic void syncache_insert(struct syncache *, struct syncache_head *); 127300004Ssephestatic int syncache_respond(struct syncache *, const struct mbuf *); 128133874Srwatsonstatic struct socket *syncache_socket(struct syncache *, struct socket *, 12996602Srwatson struct mbuf *m); 130242254Sandrestatic int syncache_sysctl_count(SYSCTL_HANDLER_ARGS); 131171639Sandrestatic void syncache_timeout(struct syncache *sc, struct syncache_head *sch, 132171639Sandre int docallout); 13386764Sjlemonstatic void syncache_timer(void *); 134253210Sandre 135253210Sandrestatic uint32_t syncookie_mac(struct in_conninfo *, tcp_seq, uint8_t, 136253210Sandre uint8_t *, uintptr_t); 137253210Sandrestatic tcp_seq syncookie_generate(struct syncache_head *, struct syncache *); 138159697Sandrestatic struct syncache 139162277Sandre *syncookie_lookup(struct in_conninfo *, struct syncache_head *, 140253210Sandre struct syncache *, struct tcphdr *, struct tcpopt *, 141159697Sandre struct socket *); 142253210Sandrestatic void syncookie_reseed(void *); 143253210Sandre#ifdef INVARIANTS 144253210Sandrestatic int syncookie_cmp(struct in_conninfo *inc, struct syncache_head *sch, 145253210Sandre struct syncache *sc, struct tcphdr *th, struct tcpopt *to, 146253210Sandre struct socket *lso); 147253210Sandre#endif 14886764Sjlemon 14986764Sjlemon/* 15086764Sjlemon * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies. 151174775Sru * 3 retransmits corresponds to a timeout of 3 * (1 + 2 + 4 + 8) == 45 seconds, 15286764Sjlemon * the odds are that the user has given up attempting to connect by then. 15386764Sjlemon */ 15486764Sjlemon#define SYNCACHE_MAXREXMTS 3 15586764Sjlemon 15686764Sjlemon/* Arbitrary values */ 15786764Sjlemon#define TCP_SYNCACHE_HASHSIZE 512 15886764Sjlemon#define TCP_SYNCACHE_BUCKETLIMIT 30 15986764Sjlemon 160215701Sdimstatic VNET_DEFINE(struct tcp_syncache, tcp_syncache); 161207369Sbz#define V_tcp_syncache VNET(tcp_syncache) 162207369Sbz 163227309Sedstatic SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, 164227309Sed "TCP SYN cache"); 16586764Sjlemon 166217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN, 167195699Srwatson &VNET_NAME(tcp_syncache.bucket_limit), 0, 168195699Srwatson "Per-bucket hash limit for syncache"); 16986764Sjlemon 170217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RDTUN, 171195699Srwatson &VNET_NAME(tcp_syncache.cache_limit), 0, 172195699Srwatson "Overall entry limit for syncache"); 17386764Sjlemon 174242254SandreSYSCTL_VNET_PROC(_net_inet_tcp_syncache, OID_AUTO, count, (CTLTYPE_UINT|CTLFLAG_RD), 175242254Sandre NULL, 0, &syncache_sysctl_count, "IU", 176195699Srwatson "Current number of entries in syncache"); 17786764Sjlemon 178217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RDTUN, 179195699Srwatson &VNET_NAME(tcp_syncache.hashsize), 0, 180195699Srwatson "Size of TCP syncache hashtable"); 18186764Sjlemon 182217322SmdfSYSCTL_VNET_UINT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW, 183195699Srwatson &VNET_NAME(tcp_syncache.rexmt_limit), 0, 184195699Srwatson "Limit on SYN/ACK retransmissions"); 18586764Sjlemon 186207369SbzVNET_DEFINE(int, tcp_sc_rst_sock_fail) = 1; 187195699SrwatsonSYSCTL_VNET_INT(_net_inet_tcp_syncache, OID_AUTO, rst_on_sock_fail, 188195699Srwatson CTLFLAG_RW, &VNET_NAME(tcp_sc_rst_sock_fail), 0, 189195699Srwatson "Send reset on socket allocation failure"); 190170055Sandre 19186764Sjlemonstatic MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache"); 19286764Sjlemon 193133874Srwatson#define SYNCACHE_HASH(inc, mask) \ 194181803Sbz ((V_tcp_syncache.hash_secret ^ \ 19586764Sjlemon (inc)->inc_faddr.s_addr ^ \ 196133874Srwatson ((inc)->inc_faddr.s_addr >> 16) ^ \ 19786764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 19886764Sjlemon 199133874Srwatson#define SYNCACHE_HASH6(inc, mask) \ 200181803Sbz ((V_tcp_syncache.hash_secret ^ \ 201133874Srwatson (inc)->inc6_faddr.s6_addr32[0] ^ \ 202133874Srwatson (inc)->inc6_faddr.s6_addr32[3] ^ \ 20386764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 20486764Sjlemon 20586764Sjlemon#define ENDPTS_EQ(a, b) ( \ 20689667Sjlemon (a)->ie_fport == (b)->ie_fport && \ 20786764Sjlemon (a)->ie_lport == (b)->ie_lport && \ 20886764Sjlemon (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \ 20986764Sjlemon (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \ 21086764Sjlemon) 21186764Sjlemon 21286764Sjlemon#define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0) 21386764Sjlemon 214159695Sandre#define SCH_LOCK(sch) mtx_lock(&(sch)->sch_mtx) 215159695Sandre#define SCH_UNLOCK(sch) mtx_unlock(&(sch)->sch_mtx) 216159695Sandre#define SCH_LOCK_ASSERT(sch) mtx_assert(&(sch)->sch_mtx, MA_OWNED) 217159695Sandre 218159695Sandre/* 219159695Sandre * Requires the syncache entry to be already removed from the bucket list. 220159695Sandre */ 22186764Sjlemonstatic void 22286764Sjlemonsyncache_free(struct syncache *sc) 22386764Sjlemon{ 224183550Szec 22586764Sjlemon if (sc->sc_ipopts) 22686764Sjlemon (void) m_free(sc->sc_ipopts); 227182056Sbz if (sc->sc_cred) 228182056Sbz crfree(sc->sc_cred); 229165149Scsjp#ifdef MAC 230172970Srwatson mac_syncache_destroy(&sc->sc_label); 231165149Scsjp#endif 232122922Sandre 233181803Sbz uma_zfree(V_tcp_syncache.zone, sc); 23486764Sjlemon} 23586764Sjlemon 23686764Sjlemonvoid 23786764Sjlemonsyncache_init(void) 23886764Sjlemon{ 23986764Sjlemon int i; 24086764Sjlemon 241181803Sbz V_tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE; 242181803Sbz V_tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT; 243181803Sbz V_tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS; 244181803Sbz V_tcp_syncache.hash_secret = arc4random(); 24586764Sjlemon 246133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize", 247181803Sbz &V_tcp_syncache.hashsize); 248133874Srwatson TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit", 249181803Sbz &V_tcp_syncache.bucket_limit); 250181887Sjulian if (!powerof2(V_tcp_syncache.hashsize) || 251181887Sjulian V_tcp_syncache.hashsize == 0) { 252133874Srwatson printf("WARNING: syncache hash size is not a power of 2.\n"); 253181803Sbz V_tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE; 254133874Srwatson } 255181803Sbz V_tcp_syncache.hashmask = V_tcp_syncache.hashsize - 1; 25686764Sjlemon 257159695Sandre /* Set limits. */ 258181803Sbz V_tcp_syncache.cache_limit = 259181803Sbz V_tcp_syncache.hashsize * V_tcp_syncache.bucket_limit; 260159695Sandre TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit", 261181803Sbz &V_tcp_syncache.cache_limit); 262159695Sandre 26386764Sjlemon /* Allocate the hash table. */ 264184214Sdes V_tcp_syncache.hashbase = malloc(V_tcp_syncache.hashsize * 265184214Sdes sizeof(struct syncache_head), M_SYNCACHE, M_WAITOK | M_ZERO); 26686764Sjlemon 267253210Sandre#ifdef VIMAGE 268253210Sandre V_tcp_syncache.vnet = curvnet; 269253210Sandre#endif 270253210Sandre 27186764Sjlemon /* Initialize the hash buckets. */ 272181803Sbz for (i = 0; i < V_tcp_syncache.hashsize; i++) { 273181803Sbz TAILQ_INIT(&V_tcp_syncache.hashbase[i].sch_bucket); 274181803Sbz mtx_init(&V_tcp_syncache.hashbase[i].sch_mtx, "tcp_sc_head", 275159695Sandre NULL, MTX_DEF); 276181803Sbz callout_init_mtx(&V_tcp_syncache.hashbase[i].sch_timer, 277181803Sbz &V_tcp_syncache.hashbase[i].sch_mtx, 0); 278181803Sbz V_tcp_syncache.hashbase[i].sch_length = 0; 279253210Sandre V_tcp_syncache.hashbase[i].sch_sc = &V_tcp_syncache; 280322454Stuexen V_tcp_syncache.hashbase[i].sch_last_overflow = 281322454Stuexen -(SYNCOOKIE_LIFETIME + 1); 28286764Sjlemon } 28386764Sjlemon 284159695Sandre /* Create the syncache entry zone. */ 285181803Sbz V_tcp_syncache.zone = uma_zcreate("syncache", sizeof(struct syncache), 286159695Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 287246208Sandre V_tcp_syncache.cache_limit = uma_zone_set_max(V_tcp_syncache.zone, 288246208Sandre V_tcp_syncache.cache_limit); 289253210Sandre 290253210Sandre /* Start the SYN cookie reseeder callout. */ 291253210Sandre callout_init(&V_tcp_syncache.secret.reseed, 1); 292253210Sandre arc4rand(V_tcp_syncache.secret.key[0], SYNCOOKIE_SECRET_SIZE, 0); 293253210Sandre arc4rand(V_tcp_syncache.secret.key[1], SYNCOOKIE_SECRET_SIZE, 0); 294253210Sandre callout_reset(&V_tcp_syncache.secret.reseed, SYNCOOKIE_LIFETIME * hz, 295253210Sandre syncookie_reseed, &V_tcp_syncache); 29686764Sjlemon} 29786764Sjlemon 298193731Szec#ifdef VIMAGE 299193731Szecvoid 300193731Szecsyncache_destroy(void) 301193731Szec{ 302204143Sbz struct syncache_head *sch; 303204143Sbz struct syncache *sc, *nsc; 304204143Sbz int i; 305193731Szec 306204143Sbz /* Cleanup hash buckets: stop timers, free entries, destroy locks. */ 307204143Sbz for (i = 0; i < V_tcp_syncache.hashsize; i++) { 308193731Szec 309204143Sbz sch = &V_tcp_syncache.hashbase[i]; 310204143Sbz callout_drain(&sch->sch_timer); 311204143Sbz 312204143Sbz SCH_LOCK(sch); 313204143Sbz TAILQ_FOREACH_SAFE(sc, &sch->sch_bucket, sc_hash, nsc) 314204143Sbz syncache_drop(sc, sch); 315204143Sbz SCH_UNLOCK(sch); 316204143Sbz KASSERT(TAILQ_EMPTY(&sch->sch_bucket), 317204143Sbz ("%s: sch->sch_bucket not empty", __func__)); 318204143Sbz KASSERT(sch->sch_length == 0, ("%s: sch->sch_length %d not 0", 319204143Sbz __func__, sch->sch_length)); 320204143Sbz mtx_destroy(&sch->sch_mtx); 321204143Sbz } 322204143Sbz 323242254Sandre KASSERT(uma_zone_get_cur(V_tcp_syncache.zone) == 0, 324242254Sandre ("%s: cache_count not 0", __func__)); 325204143Sbz 326204143Sbz /* Free the allocated global resources. */ 327193731Szec uma_zdestroy(V_tcp_syncache.zone); 328204143Sbz free(V_tcp_syncache.hashbase, M_SYNCACHE); 329253210Sandre 330253210Sandre callout_drain(&V_tcp_syncache.secret.reseed); 331193731Szec} 332193731Szec#endif 333193731Szec 334242254Sandrestatic int 335242254Sandresyncache_sysctl_count(SYSCTL_HANDLER_ARGS) 336242254Sandre{ 337242254Sandre int count; 338242254Sandre 339242254Sandre count = uma_zone_get_cur(V_tcp_syncache.zone); 340244680Sglebius return (sysctl_handle_int(oidp, &count, 0, req)); 341242254Sandre} 342242254Sandre 343159695Sandre/* 344159695Sandre * Inserts a syncache entry into the specified bucket row. 345159695Sandre * Locks and unlocks the syncache_head autonomously. 346159695Sandre */ 34788180Sjlemonstatic void 348159697Sandresyncache_insert(struct syncache *sc, struct syncache_head *sch) 34986764Sjlemon{ 35086764Sjlemon struct syncache *sc2; 35186764Sjlemon 352159695Sandre SCH_LOCK(sch); 353122496Ssam 35486764Sjlemon /* 355159695Sandre * Make sure that we don't overflow the per-bucket limit. 356159695Sandre * If the bucket is full, toss the oldest element. 35786764Sjlemon */ 358181803Sbz if (sch->sch_length >= V_tcp_syncache.bucket_limit) { 359159695Sandre KASSERT(!TAILQ_EMPTY(&sch->sch_bucket), 360159695Sandre ("sch->sch_length incorrect")); 361159695Sandre sc2 = TAILQ_LAST(&sch->sch_bucket, sch_head); 362322315Stuexen sch->sch_last_overflow = time_uptime; 36386764Sjlemon syncache_drop(sc2, sch); 364190948Srwatson TCPSTAT_INC(tcps_sc_bucketoverflow); 36586764Sjlemon } 36686764Sjlemon 36786764Sjlemon /* Put it into the bucket. */ 368159695Sandre TAILQ_INSERT_HEAD(&sch->sch_bucket, sc, sc_hash); 36986764Sjlemon sch->sch_length++; 370159695Sandre 371237263Snp#ifdef TCP_OFFLOAD 372237263Snp if (ADDED_BY_TOE(sc)) { 373237263Snp struct toedev *tod = sc->sc_tod; 374237263Snp 375237263Snp tod->tod_syncache_added(tod, sc->sc_todctx); 376237263Snp } 377237263Snp#endif 378237263Snp 379159695Sandre /* Reinitialize the bucket row's timer. */ 380174775Sru if (sch->sch_length == 1) 381174775Sru sch->sch_nextc = ticks + INT_MAX; 382171639Sandre syncache_timeout(sc, sch, 1); 383159695Sandre 384159695Sandre SCH_UNLOCK(sch); 385159695Sandre 386190948Srwatson TCPSTAT_INC(tcps_sc_added); 38786764Sjlemon} 38886764Sjlemon 389159695Sandre/* 390159695Sandre * Remove and free entry from syncache bucket row. 391159695Sandre * Expects locked syncache head. 392159695Sandre */ 39386764Sjlemonstatic void 394159697Sandresyncache_drop(struct syncache *sc, struct syncache_head *sch) 39586764Sjlemon{ 39686764Sjlemon 397159695Sandre SCH_LOCK_ASSERT(sch); 39886764Sjlemon 39986764Sjlemon TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash); 40086764Sjlemon sch->sch_length--; 40186764Sjlemon 402237263Snp#ifdef TCP_OFFLOAD 403237263Snp if (ADDED_BY_TOE(sc)) { 404237263Snp struct toedev *tod = sc->sc_tod; 405237263Snp 406237263Snp tod->tod_syncache_removed(tod, sc->sc_todctx); 407237263Snp } 408237263Snp#endif 409237263Snp 41086764Sjlemon syncache_free(sc); 41186764Sjlemon} 41286764Sjlemon 41386764Sjlemon/* 414171639Sandre * Engage/reengage time on bucket row. 415171639Sandre */ 416171639Sandrestatic void 417171639Sandresyncache_timeout(struct syncache *sc, struct syncache_head *sch, int docallout) 418171639Sandre{ 419171639Sandre sc->sc_rxttime = ticks + 420242261Sandre TCPTV_RTOBASE * (tcp_syn_backoff[sc->sc_rxmits]); 421171639Sandre sc->sc_rxmits++; 422174775Sru if (TSTMP_LT(sc->sc_rxttime, sch->sch_nextc)) { 423171639Sandre sch->sch_nextc = sc->sc_rxttime; 424174775Sru if (docallout) 425174775Sru callout_reset(&sch->sch_timer, sch->sch_nextc - ticks, 426174775Sru syncache_timer, (void *)sch); 427174775Sru } 428171639Sandre} 429171639Sandre 430171639Sandre/* 43186764Sjlemon * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 43286764Sjlemon * If we have retransmitted an entry the maximum number of times, expire it. 433159695Sandre * One separate timer for each bucket row. 43486764Sjlemon */ 43586764Sjlemonstatic void 436159697Sandresyncache_timer(void *xsch) 43786764Sjlemon{ 438159695Sandre struct syncache_head *sch = (struct syncache_head *)xsch; 43986764Sjlemon struct syncache *sc, *nsc; 440159695Sandre int tick = ticks; 441169685Sandre char *s; 44286764Sjlemon 443253210Sandre CURVNET_SET(sch->sch_sc->vnet); 444185348Szec 445159695Sandre /* NB: syncache_head has already been locked by the callout. */ 446159695Sandre SCH_LOCK_ASSERT(sch); 44786764Sjlemon 448174775Sru /* 449174775Sru * In the following cycle we may remove some entries and/or 450174775Sru * advance some timeouts, so re-initialize the bucket timer. 451174775Sru */ 452174775Sru sch->sch_nextc = tick + INT_MAX; 453174775Sru 454159695Sandre TAILQ_FOREACH_SAFE(sc, &sch->sch_bucket, sc_hash, nsc) { 455159695Sandre /* 456159695Sandre * We do not check if the listen socket still exists 457159695Sandre * and accept the case where the listen socket may be 458159695Sandre * gone by the time we resend the SYN/ACK. We do 459159695Sandre * not expect this to happens often. If it does, 460159695Sandre * then the RST will be sent by the time the remote 461159695Sandre * host does the SYN/ACK->ACK. 462159695Sandre */ 463174775Sru if (TSTMP_GT(sc->sc_rxttime, tick)) { 464174775Sru if (TSTMP_LT(sc->sc_rxttime, sch->sch_nextc)) 465159695Sandre sch->sch_nextc = sc->sc_rxttime; 466159695Sandre continue; 467159695Sandre } 468181803Sbz if (sc->sc_rxmits > V_tcp_syncache.rexmt_limit) { 469169685Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 470171639Sandre log(LOG_DEBUG, "%s; %s: Retransmits exhausted, " 471171639Sandre "giving up and removing syncache entry\n", 472169685Sandre s, __func__); 473169685Sandre free(s, M_TCPLOG); 474169685Sandre } 475159695Sandre syncache_drop(sc, sch); 476190948Srwatson TCPSTAT_INC(tcps_sc_stale); 47786764Sjlemon continue; 47886764Sjlemon } 479171639Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 480171639Sandre log(LOG_DEBUG, "%s; %s: Response timeout, " 481171639Sandre "retransmitting (%u) SYN|ACK\n", 482171639Sandre s, __func__, sc->sc_rxmits); 483171639Sandre free(s, M_TCPLOG); 484171639Sandre } 485159695Sandre 486300004Ssephe (void) syncache_respond(sc, NULL); 487190948Srwatson TCPSTAT_INC(tcps_sc_retransmitted); 488171639Sandre syncache_timeout(sc, sch, 0); 48986764Sjlemon } 490159695Sandre if (!TAILQ_EMPTY(&(sch)->sch_bucket)) 491159695Sandre callout_reset(&(sch)->sch_timer, (sch)->sch_nextc - tick, 492159695Sandre syncache_timer, (void *)(sch)); 493185348Szec CURVNET_RESTORE(); 49486764Sjlemon} 49586764Sjlemon 49686764Sjlemon/* 49786764Sjlemon * Find an entry in the syncache. 498159695Sandre * Returns always with locked syncache_head plus a matching entry or NULL. 49986764Sjlemon */ 500270055Sbzstatic struct syncache * 501159697Sandresyncache_lookup(struct in_conninfo *inc, struct syncache_head **schp) 50286764Sjlemon{ 50386764Sjlemon struct syncache *sc; 50486764Sjlemon struct syncache_head *sch; 50586764Sjlemon 50686764Sjlemon#ifdef INET6 507186222Sbz if (inc->inc_flags & INC_ISIPV6) { 508181803Sbz sch = &V_tcp_syncache.hashbase[ 509181803Sbz SYNCACHE_HASH6(inc, V_tcp_syncache.hashmask)]; 51086764Sjlemon *schp = sch; 511159695Sandre 512159695Sandre SCH_LOCK(sch); 513159695Sandre 514159695Sandre /* Circle through bucket row to find matching entry. */ 51586764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 516122496Ssam if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) 51786764Sjlemon return (sc); 51886764Sjlemon } 51986764Sjlemon } else 52086764Sjlemon#endif 52186764Sjlemon { 522181803Sbz sch = &V_tcp_syncache.hashbase[ 523181803Sbz SYNCACHE_HASH(inc, V_tcp_syncache.hashmask)]; 52486764Sjlemon *schp = sch; 525159695Sandre 526159695Sandre SCH_LOCK(sch); 527159695Sandre 528159695Sandre /* Circle through bucket row to find matching entry. */ 52986764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 53086764Sjlemon#ifdef INET6 531186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) 53286764Sjlemon continue; 53386764Sjlemon#endif 534122496Ssam if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) 53586764Sjlemon return (sc); 53686764Sjlemon } 53786764Sjlemon } 538159695Sandre SCH_LOCK_ASSERT(*schp); 539159695Sandre return (NULL); /* always returns with locked sch */ 54086764Sjlemon} 54186764Sjlemon 54286764Sjlemon/* 54386764Sjlemon * This function is called when we get a RST for a 54486764Sjlemon * non-existent connection, so that we can see if the 54586764Sjlemon * connection is in the syn cache. If it is, zap it. 54686764Sjlemon */ 54786764Sjlemonvoid 548159697Sandresyncache_chkrst(struct in_conninfo *inc, struct tcphdr *th) 54986764Sjlemon{ 55086764Sjlemon struct syncache *sc; 55186764Sjlemon struct syncache_head *sch; 552171638Sandre char *s = NULL; 55386764Sjlemon 554159695Sandre sc = syncache_lookup(inc, &sch); /* returns locked sch */ 555159695Sandre SCH_LOCK_ASSERT(sch); 556171638Sandre 557171638Sandre /* 558171638Sandre * Any RST to our SYN|ACK must not carry ACK, SYN or FIN flags. 559171638Sandre * See RFC 793 page 65, section SEGMENT ARRIVES. 560171638Sandre */ 561171638Sandre if (th->th_flags & (TH_ACK|TH_SYN|TH_FIN)) { 562171638Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 563171638Sandre log(LOG_DEBUG, "%s; %s: Spurious RST with ACK, SYN or " 564171638Sandre "FIN flag set, segment ignored\n", s, __func__); 565190948Srwatson TCPSTAT_INC(tcps_badrst); 566159695Sandre goto done; 567171638Sandre } 568122496Ssam 56986764Sjlemon /* 570171638Sandre * No corresponding connection was found in syncache. 571171638Sandre * If syncookies are enabled and possibly exclusively 572171638Sandre * used, or we are under memory pressure, a valid RST 573171638Sandre * may not find a syncache entry. In that case we're 574171638Sandre * done and no SYN|ACK retransmissions will happen. 575218909Sbrucec * Otherwise the RST was misdirected or spoofed. 576171638Sandre */ 577171638Sandre if (sc == NULL) { 578171638Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 579171638Sandre log(LOG_DEBUG, "%s; %s: Spurious RST without matching " 580171638Sandre "syncache entry (possibly syncookie only), " 581171638Sandre "segment ignored\n", s, __func__); 582190948Srwatson TCPSTAT_INC(tcps_badrst); 583171638Sandre goto done; 584171638Sandre } 585171638Sandre 586171638Sandre /* 58786764Sjlemon * If the RST bit is set, check the sequence number to see 58886764Sjlemon * if this is a valid reset segment. 58986764Sjlemon * RFC 793 page 37: 59086764Sjlemon * In all states except SYN-SENT, all reset (RST) segments 59186764Sjlemon * are validated by checking their SEQ-fields. A reset is 59286764Sjlemon * valid if its sequence number is in the window. 59386764Sjlemon * 59486764Sjlemon * The sequence number in the reset segment is normally an 59586764Sjlemon * echo of our outgoing acknowlegement numbers, but some hosts 59686764Sjlemon * send a reset with the sequence number at the rightmost edge 59786764Sjlemon * of our receive window, and we have to handle this case. 59886764Sjlemon */ 59986764Sjlemon if (SEQ_GEQ(th->th_seq, sc->sc_irs) && 60086764Sjlemon SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) { 60186764Sjlemon syncache_drop(sc, sch); 602171638Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 603171638Sandre log(LOG_DEBUG, "%s; %s: Our SYN|ACK was rejected, " 604171638Sandre "connection attempt aborted by remote endpoint\n", 605171638Sandre s, __func__); 606190948Srwatson TCPSTAT_INC(tcps_sc_reset); 607178862Sjhb } else { 608178862Sjhb if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 609178862Sjhb log(LOG_DEBUG, "%s; %s: RST with invalid SEQ %u != " 610178862Sjhb "IRS %u (+WND %u), segment ignored\n", 611178862Sjhb s, __func__, th->th_seq, sc->sc_irs, sc->sc_wnd); 612190948Srwatson TCPSTAT_INC(tcps_badrst); 61386764Sjlemon } 614171638Sandre 615159695Sandredone: 616171638Sandre if (s != NULL) 617171638Sandre free(s, M_TCPLOG); 618159695Sandre SCH_UNLOCK(sch); 61986764Sjlemon} 62086764Sjlemon 62186764Sjlemonvoid 622159697Sandresyncache_badack(struct in_conninfo *inc) 62386764Sjlemon{ 62486764Sjlemon struct syncache *sc; 62586764Sjlemon struct syncache_head *sch; 62686764Sjlemon 627159695Sandre sc = syncache_lookup(inc, &sch); /* returns locked sch */ 628159695Sandre SCH_LOCK_ASSERT(sch); 62986764Sjlemon if (sc != NULL) { 63086764Sjlemon syncache_drop(sc, sch); 631190948Srwatson TCPSTAT_INC(tcps_sc_badack); 63286764Sjlemon } 633159695Sandre SCH_UNLOCK(sch); 63486764Sjlemon} 63586764Sjlemon 63686764Sjlemonvoid 637159697Sandresyncache_unreach(struct in_conninfo *inc, struct tcphdr *th) 63886764Sjlemon{ 63986764Sjlemon struct syncache *sc; 64086764Sjlemon struct syncache_head *sch; 64186764Sjlemon 642159695Sandre sc = syncache_lookup(inc, &sch); /* returns locked sch */ 643159695Sandre SCH_LOCK_ASSERT(sch); 64486764Sjlemon if (sc == NULL) 645159695Sandre goto done; 64686764Sjlemon 64786764Sjlemon /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 64886764Sjlemon if (ntohl(th->th_seq) != sc->sc_iss) 649159695Sandre goto done; 65086764Sjlemon 65186764Sjlemon /* 65286764Sjlemon * If we've rertransmitted 3 times and this is our second error, 65386764Sjlemon * we remove the entry. Otherwise, we allow it to continue on. 65486764Sjlemon * This prevents us from incorrectly nuking an entry during a 65586764Sjlemon * spurious network outage. 65686764Sjlemon * 65786764Sjlemon * See tcp_notify(). 65886764Sjlemon */ 659159695Sandre if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxmits < 3 + 1) { 66086764Sjlemon sc->sc_flags |= SCF_UNREACH; 661159695Sandre goto done; 66286764Sjlemon } 66386764Sjlemon syncache_drop(sc, sch); 664190948Srwatson TCPSTAT_INC(tcps_sc_unreach); 665159695Sandredone: 666159695Sandre SCH_UNLOCK(sch); 66786764Sjlemon} 66886764Sjlemon 66986764Sjlemon/* 67086764Sjlemon * Build a new TCP socket structure from a syncache entry. 671309108Sjch * 672309108Sjch * On success return the newly created socket with its underlying inp locked. 67386764Sjlemon */ 67486764Sjlemonstatic struct socket * 675159697Sandresyncache_socket(struct syncache *sc, struct socket *lso, struct mbuf *m) 67686764Sjlemon{ 67786764Sjlemon struct inpcb *inp = NULL; 67886764Sjlemon struct socket *so; 67986764Sjlemon struct tcpcb *tp; 680211332Sandre int error; 681169685Sandre char *s; 68286764Sjlemon 683309108Sjch INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 684122496Ssam 68586764Sjlemon /* 68686764Sjlemon * Ok, create the full blown connection, and set things up 68786764Sjlemon * as they would have been set up if we had created the 68886764Sjlemon * connection when the SYN arrived. If we can't create 68986764Sjlemon * the connection, abort it. 69086764Sjlemon */ 691302995Sjch so = sonewconn(lso, 0); 69286764Sjlemon if (so == NULL) { 69386764Sjlemon /* 694169685Sandre * Drop the connection; we will either send a RST or 695169685Sandre * have the peer retransmit its SYN again after its 696169685Sandre * RTO and try again. 69786764Sjlemon */ 698190948Srwatson TCPSTAT_INC(tcps_listendrop); 699169685Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 700169685Sandre log(LOG_DEBUG, "%s; %s: Socket create failed " 701169685Sandre "due to limits or memory shortage\n", 702169685Sandre s, __func__); 703169685Sandre free(s, M_TCPLOG); 704169685Sandre } 705122496Ssam goto abort2; 70686764Sjlemon } 707101106Srwatson#ifdef MAC 708172930Srwatson mac_socketpeer_set_from_mbuf(m, so); 709101106Srwatson#endif 71086764Sjlemon 71186764Sjlemon inp = sotoinpcb(so); 712195922Sjulian inp->inp_inc.inc_fibnum = so->so_fibnum; 713178285Srwatson INP_WLOCK(inp); 714309108Sjch /* 715309108Sjch * Exclusive pcbinfo lock is not required in syncache socket case even 716309108Sjch * if two inpcb locks can be acquired simultaneously: 717309108Sjch * - the inpcb in LISTEN state, 718309108Sjch * - the newly created inp. 719309108Sjch * 720309108Sjch * In this case, an inp cannot be at same time in LISTEN state and 721309108Sjch * just created by an accept() call. 722309108Sjch */ 723222488Srwatson INP_HASH_WLOCK(&V_tcbinfo); 72486764Sjlemon 725159695Sandre /* Insert new socket into PCB hash list. */ 726186222Sbz inp->inp_inc.inc_flags = sc->sc_inc.inc_flags; 72786764Sjlemon#ifdef INET6 728186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) { 72986764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 73086764Sjlemon } else { 73186764Sjlemon inp->inp_vflag &= ~INP_IPV6; 73286764Sjlemon inp->inp_vflag |= INP_IPV4; 73386764Sjlemon#endif 73486764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 73586764Sjlemon#ifdef INET6 73686764Sjlemon } 73786764Sjlemon#endif 738222748Srwatson 739222748Srwatson /* 740261705Sadrian * If there's an mbuf and it has a flowid, then let's initialise the 741261705Sadrian * inp with that particular flowid. 742261705Sadrian */ 743281955Shiren if (m != NULL && M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 744261705Sadrian inp->inp_flowid = m->m_pkthdr.flowid; 745284603Shiren inp->inp_flowtype = M_HASHTYPE_GET(m); 746261705Sadrian } 747261705Sadrian 748261705Sadrian /* 749222748Srwatson * Install in the reservation hash table for now, but don't yet 750222748Srwatson * install a connection group since the full 4-tuple isn't yet 751222748Srwatson * configured. 752222748Srwatson */ 75386764Sjlemon inp->inp_lport = sc->sc_inc.inc_lport; 754222748Srwatson if ((error = in_pcbinshash_nopcbgroup(inp)) != 0) { 75586764Sjlemon /* 75686764Sjlemon * Undo the assignments above if we failed to 75786764Sjlemon * put the PCB on the hash lists. 75886764Sjlemon */ 75986764Sjlemon#ifdef INET6 760186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) 76186764Sjlemon inp->in6p_laddr = in6addr_any; 762133874Srwatson else 76386764Sjlemon#endif 76486764Sjlemon inp->inp_laddr.s_addr = INADDR_ANY; 76586764Sjlemon inp->inp_lport = 0; 766211327Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 767211327Sandre log(LOG_DEBUG, "%s; %s: in_pcbinshash failed " 768211327Sandre "with error %i\n", 769211327Sandre s, __func__, error); 770211327Sandre free(s, M_TCPLOG); 771211327Sandre } 772222488Srwatson INP_HASH_WUNLOCK(&V_tcbinfo); 77386764Sjlemon goto abort; 77486764Sjlemon } 775171167Sgnn#ifdef IPSEC 776159697Sandre /* Copy old policy into new socket's. */ 77786764Sjlemon if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp)) 778159950Sandre printf("syncache_socket: could not copy policy\n"); 77986764Sjlemon#endif 78086764Sjlemon#ifdef INET6 781186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) { 78286764Sjlemon struct inpcb *oinp = sotoinpcb(lso); 78386764Sjlemon struct in6_addr laddr6; 784124847Sandre struct sockaddr_in6 sin6; 78586764Sjlemon /* 78686764Sjlemon * Inherit socket options from the listening socket. 78786764Sjlemon * Note that in6p_inputopts are not (and should not be) 78886764Sjlemon * copied, since it stores previously received options and is 78986764Sjlemon * used to detect if each new option is different than the 79086764Sjlemon * previous one and hence should be passed to a user. 791133874Srwatson * If we copied in6p_inputopts, a user would not be able to 79286764Sjlemon * receive options just after calling the accept system call. 79386764Sjlemon */ 79486764Sjlemon inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS; 79586764Sjlemon if (oinp->in6p_outputopts) 79686764Sjlemon inp->in6p_outputopts = 79786764Sjlemon ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT); 79886764Sjlemon 799124847Sandre sin6.sin6_family = AF_INET6; 800124847Sandre sin6.sin6_len = sizeof(sin6); 801124847Sandre sin6.sin6_addr = sc->sc_inc.inc6_faddr; 802124847Sandre sin6.sin6_port = sc->sc_inc.inc_fport; 803124847Sandre sin6.sin6_flowinfo = sin6.sin6_scope_id = 0; 80486764Sjlemon laddr6 = inp->in6p_laddr; 80586764Sjlemon if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 80686764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 807222691Srwatson if ((error = in6_pcbconnect_mbuf(inp, (struct sockaddr *)&sin6, 808222691Srwatson thread0.td_ucred, m)) != 0) { 80986764Sjlemon inp->in6p_laddr = laddr6; 810211327Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 811211327Sandre log(LOG_DEBUG, "%s; %s: in6_pcbconnect failed " 812211327Sandre "with error %i\n", 813211327Sandre s, __func__, error); 814211327Sandre free(s, M_TCPLOG); 815211327Sandre } 816222488Srwatson INP_HASH_WUNLOCK(&V_tcbinfo); 81786764Sjlemon goto abort; 81886764Sjlemon } 819132307Sdwmalone /* Override flowlabel from in6_pcbconnect. */ 820186141Sbz inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 821186141Sbz inp->inp_flow |= sc->sc_flowlabel; 822221250Sbz } 823221250Sbz#endif /* INET6 */ 824221250Sbz#if defined(INET) && defined(INET6) 825221250Sbz else 82686764Sjlemon#endif 827221250Sbz#ifdef INET 82886764Sjlemon { 82986764Sjlemon struct in_addr laddr; 830124847Sandre struct sockaddr_in sin; 83186764Sjlemon 832174704Skmacy inp->inp_options = (m) ? ip_srcroute(m) : NULL; 833174704Skmacy 83486764Sjlemon if (inp->inp_options == NULL) { 83586764Sjlemon inp->inp_options = sc->sc_ipopts; 83686764Sjlemon sc->sc_ipopts = NULL; 83786764Sjlemon } 83886764Sjlemon 839124847Sandre sin.sin_family = AF_INET; 840124847Sandre sin.sin_len = sizeof(sin); 841124847Sandre sin.sin_addr = sc->sc_inc.inc_faddr; 842124847Sandre sin.sin_port = sc->sc_inc.inc_fport; 843124847Sandre bzero((caddr_t)sin.sin_zero, sizeof(sin.sin_zero)); 84486764Sjlemon laddr = inp->inp_laddr; 84586764Sjlemon if (inp->inp_laddr.s_addr == INADDR_ANY) 84686764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 847222691Srwatson if ((error = in_pcbconnect_mbuf(inp, (struct sockaddr *)&sin, 848222691Srwatson thread0.td_ucred, m)) != 0) { 84986764Sjlemon inp->inp_laddr = laddr; 850211327Sandre if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) { 851211327Sandre log(LOG_DEBUG, "%s; %s: in_pcbconnect failed " 852211327Sandre "with error %i\n", 853211327Sandre s, __func__, error); 854211327Sandre free(s, M_TCPLOG); 855211327Sandre } 856222488Srwatson INP_HASH_WUNLOCK(&V_tcbinfo); 85786764Sjlemon goto abort; 85886764Sjlemon } 85986764Sjlemon } 860221250Sbz#endif /* INET */ 861222488Srwatson INP_HASH_WUNLOCK(&V_tcbinfo); 86286764Sjlemon tp = intotcpcb(inp); 863254889Smarkj tcp_state_change(tp, TCPS_SYN_RECEIVED); 86486764Sjlemon tp->iss = sc->sc_iss; 86586764Sjlemon tp->irs = sc->sc_irs; 86686764Sjlemon tcp_rcvseqinit(tp); 86786764Sjlemon tcp_sendseqinit(tp); 86886764Sjlemon tp->snd_wl1 = sc->sc_irs; 869168368Sandre tp->snd_max = tp->iss + 1; 870168368Sandre tp->snd_nxt = tp->iss + 1; 87186764Sjlemon tp->rcv_up = sc->sc_irs + 1; 87286764Sjlemon tp->rcv_wnd = sc->sc_wnd; 87386764Sjlemon tp->rcv_adv += tp->rcv_wnd; 874168368Sandre tp->last_ack_sent = tp->rcv_nxt; 87586764Sjlemon 87690982Sjlemon tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH|TF_NODELAY); 87786764Sjlemon if (sc->sc_flags & SCF_NOOPT) 87886764Sjlemon tp->t_flags |= TF_NOOPT; 879159950Sandre else { 880159950Sandre if (sc->sc_flags & SCF_WINSCALE) { 881159950Sandre tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 882159950Sandre tp->snd_scale = sc->sc_requested_s_scale; 883159950Sandre tp->request_r_scale = sc->sc_requested_r_scale; 884159950Sandre } 885159950Sandre if (sc->sc_flags & SCF_TIMESTAMP) { 886159950Sandre tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 887159950Sandre tp->ts_recent = sc->sc_tsreflect; 888231767Sbz tp->ts_recent_age = tcp_ts_getticks(); 889162277Sandre tp->ts_offset = sc->sc_tsoff; 890159950Sandre } 891125680Sbms#ifdef TCP_SIGNATURE 892159950Sandre if (sc->sc_flags & SCF_SIGNATURE) 893159950Sandre tp->t_flags |= TF_SIGNATURE; 894125783Sbms#endif 895169317Sandre if (sc->sc_flags & SCF_SACK) 896159950Sandre tp->t_flags |= TF_SACK_PERMIT; 897130989Sps } 898159695Sandre 899181056Srpaulo if (sc->sc_flags & SCF_ECN) 900181056Srpaulo tp->t_flags |= TF_ECN_PERMIT; 901181056Srpaulo 902122922Sandre /* 903122922Sandre * Set up MSS and get cached values from tcp_hostcache. 904122922Sandre * This might overwrite some of the defaults we just set. 905122922Sandre */ 90686764Sjlemon tcp_mss(tp, sc->sc_peer_mss); 90786764Sjlemon 90886764Sjlemon /* 909242250Sandre * If the SYN,ACK was retransmitted, indicate that CWND to be 910242250Sandre * limited to one segment in cc_conn_init(). 911210666Sandre * NB: sc_rxmits counts all SYN,ACK transmits, not just retransmits. 91286764Sjlemon */ 913210666Sandre if (sc->sc_rxmits > 1) 914242250Sandre tp->snd_cwnd = 1; 91586764Sjlemon 916237263Snp#ifdef TCP_OFFLOAD 917231025Sglebius /* 918237263Snp * Allow a TOE driver to install its hooks. Note that we hold the 919237263Snp * pcbinfo lock too and that prevents tcp_usr_accept from accepting a 920237263Snp * new connection before the TOE driver has done its thing. 921237263Snp */ 922237263Snp if (ADDED_BY_TOE(sc)) { 923237263Snp struct toedev *tod = sc->sc_tod; 924237263Snp 925237263Snp tod->tod_offload_socket(tod, sc->sc_todctx, so); 926237263Snp } 927237263Snp#endif 928237263Snp /* 929231025Sglebius * Copy and activate timers. 930231025Sglebius */ 931231025Sglebius tp->t_keepinit = sototcpcb(lso)->t_keepinit; 932231025Sglebius tp->t_keepidle = sototcpcb(lso)->t_keepidle; 933231025Sglebius tp->t_keepintvl = sototcpcb(lso)->t_keepintvl; 934231025Sglebius tp->t_keepcnt = sototcpcb(lso)->t_keepcnt; 935231025Sglebius tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 936231025Sglebius 937190948Srwatson TCPSTAT_INC(tcps_accepts); 93886764Sjlemon return (so); 93986764Sjlemon 94086764Sjlemonabort: 941178285Srwatson INP_WUNLOCK(inp); 942122496Ssamabort2: 94386764Sjlemon if (so != NULL) 944156763Srwatson soabort(so); 94586764Sjlemon return (NULL); 94686764Sjlemon} 94786764Sjlemon 94886764Sjlemon/* 94986764Sjlemon * This function gets called when we receive an ACK for a 95086764Sjlemon * socket in the LISTEN state. We look up the connection 95186764Sjlemon * in the syncache, and if its there, we pull it out of 95286764Sjlemon * the cache and turn it into a full-blown connection in 95386764Sjlemon * the SYN-RECEIVED state. 954309108Sjch * 955309108Sjch * On syncache_socket() success the newly created socket 956309108Sjch * has its underlying inp locked. 95786764Sjlemon */ 95886764Sjlemonint 959162277Sandresyncache_expand(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th, 960159697Sandre struct socket **lsop, struct mbuf *m) 96186764Sjlemon{ 96286764Sjlemon struct syncache *sc; 96386764Sjlemon struct syncache_head *sch; 964162277Sandre struct syncache scs; 965169685Sandre char *s; 96686764Sjlemon 967159695Sandre /* 968159695Sandre * Global TCP locks are held because we manipulate the PCB lists 969159695Sandre * and create a new socket. 970159695Sandre */ 971309108Sjch INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 972169685Sandre KASSERT((th->th_flags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK, 973169685Sandre ("%s: can handle only ACK", __func__)); 974122496Ssam 975159695Sandre sc = syncache_lookup(inc, &sch); /* returns locked sch */ 976159695Sandre SCH_LOCK_ASSERT(sch); 977253210Sandre 978253210Sandre#ifdef INVARIANTS 979253210Sandre /* 980253210Sandre * Test code for syncookies comparing the syncache stored 981253210Sandre * values with the reconstructed values from the cookie. 982253210Sandre */ 983253210Sandre if (sc != NULL) 984253210Sandre syncookie_cmp(inc, sch, sc, th, to, *lsop); 985253210Sandre#endif 986253210Sandre 98788180Sjlemon if (sc == NULL) { 98888180Sjlemon /* 989133874Srwatson * There is no syncache entry, so see if this ACK is 99088180Sjlemon * a returning syncookie. To do this, first: 991322315Stuexen * A. Check if syncookies are used in case of syncache 992322315Stuexen * overflows 993322315Stuexen * B. See if this socket has had a syncache entry dropped in 994322315Stuexen * the recent past. We don't want to accept a bogus 995322315Stuexen * syncookie if we've never received a SYN or accept it 996322315Stuexen * twice. 997322315Stuexen * C. check that the syncookie is valid. If it is, then 99888180Sjlemon * cobble up a fake syncache entry, and return. 99988180Sjlemon */ 1000185348Szec if (!V_tcp_syncookies) { 1001162277Sandre SCH_UNLOCK(sch); 1002169685Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1003170078Sandre log(LOG_DEBUG, "%s; %s: Spurious ACK, " 1004170078Sandre "segment rejected (syncookies disabled)\n", 1005169685Sandre s, __func__); 1006162277Sandre goto failed; 1007162277Sandre } 1008322315Stuexen if (!V_tcp_syncookiesonly && 1009322315Stuexen sch->sch_last_overflow < time_uptime - SYNCOOKIE_LIFETIME) { 1010322315Stuexen SCH_UNLOCK(sch); 1011322315Stuexen if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1012322315Stuexen log(LOG_DEBUG, "%s; %s: Spurious ACK, " 1013322315Stuexen "segment rejected (no syncache entry)\n", 1014322315Stuexen s, __func__); 1015322315Stuexen goto failed; 1016322315Stuexen } 1017162277Sandre bzero(&scs, sizeof(scs)); 1018253210Sandre sc = syncookie_lookup(inc, sch, &scs, th, to, *lsop); 1019159695Sandre SCH_UNLOCK(sch); 1020169685Sandre if (sc == NULL) { 1021169685Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1022169685Sandre log(LOG_DEBUG, "%s; %s: Segment failed " 1023170078Sandre "SYNCOOKIE authentication, segment rejected " 1024170078Sandre "(probably spoofed)\n", s, __func__); 1025159695Sandre goto failed; 1026169685Sandre } 1027159695Sandre } else { 1028159695Sandre /* Pull out the entry to unlock the bucket row. */ 1029159695Sandre TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash); 1030159695Sandre sch->sch_length--; 1031237263Snp#ifdef TCP_OFFLOAD 1032237263Snp if (ADDED_BY_TOE(sc)) { 1033237263Snp struct toedev *tod = sc->sc_tod; 1034237263Snp 1035237263Snp tod->tod_syncache_removed(tod, sc->sc_todctx); 1036237263Snp } 1037237263Snp#endif 1038159695Sandre SCH_UNLOCK(sch); 103988180Sjlemon } 104086764Sjlemon 104186764Sjlemon /* 1042169685Sandre * Segment validation: 1043169685Sandre * ACK must match our initial sequence number + 1 (the SYN|ACK). 104486764Sjlemon */ 1045237263Snp if (th->th_ack != sc->sc_iss + 1) { 1046169685Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1047170078Sandre log(LOG_DEBUG, "%s; %s: ACK %u != ISS+1 %u, segment " 1048170078Sandre "rejected\n", s, __func__, th->th_ack, sc->sc_iss); 1049159695Sandre goto failed; 1050169685Sandre } 1051181337Sjhb 1052169686Sandre /* 1053181337Sjhb * The SEQ must fall in the window starting at the received 1054181337Sjhb * initial receive sequence number + 1 (the SYN). 1055169686Sandre */ 1056237263Snp if (SEQ_LEQ(th->th_seq, sc->sc_irs) || 1057237263Snp SEQ_GT(th->th_seq, sc->sc_irs + sc->sc_wnd)) { 1058169686Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1059170078Sandre log(LOG_DEBUG, "%s; %s: SEQ %u != IRS+1 %u, segment " 1060170385Sandre "rejected\n", s, __func__, th->th_seq, sc->sc_irs); 1061169686Sandre goto failed; 1062169686Sandre } 1063174545Skmacy 1064253150Sandre /* 1065253150Sandre * If timestamps were not negotiated during SYN/ACK they 1066253150Sandre * must not appear on any segment during this session. 1067253150Sandre */ 1068169686Sandre if (!(sc->sc_flags & SCF_TIMESTAMP) && (to->to_flags & TOF_TS)) { 1069169686Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1070170078Sandre log(LOG_DEBUG, "%s; %s: Timestamp not expected, " 1071170078Sandre "segment rejected\n", s, __func__); 1072169686Sandre goto failed; 1073169686Sandre } 1074253150Sandre 1075169686Sandre /* 1076253150Sandre * If timestamps were negotiated during SYN/ACK they should 1077253150Sandre * appear on every segment during this session. 1078253150Sandre * XXXAO: This is only informal as there have been unverified 1079253150Sandre * reports of non-compliants stacks. 1080253150Sandre */ 1081253150Sandre if ((sc->sc_flags & SCF_TIMESTAMP) && !(to->to_flags & TOF_TS)) { 1082253395Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) { 1083253150Sandre log(LOG_DEBUG, "%s; %s: Timestamp missing, " 1084253150Sandre "no action\n", s, __func__); 1085253395Sandre free(s, M_TCPLOG); 1086253395Sandre s = NULL; 1087253395Sandre } 1088253150Sandre } 1089253150Sandre 1090253150Sandre /* 1091169686Sandre * If timestamps were negotiated the reflected timestamp 1092169686Sandre * must be equal to what we actually sent in the SYN|ACK. 1093169686Sandre */ 1094237263Snp if ((to->to_flags & TOF_TS) && to->to_tsecr != sc->sc_ts) { 1095169686Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL))) 1096170078Sandre log(LOG_DEBUG, "%s; %s: TSECR %u != TS %u, " 1097170078Sandre "segment rejected\n", 1098169686Sandre s, __func__, to->to_tsecr, sc->sc_ts); 1099169686Sandre goto failed; 1100169686Sandre } 110186764Sjlemon 1102168902Sandre *lsop = syncache_socket(sc, *lsop, m); 1103159695Sandre 1104168902Sandre if (*lsop == NULL) 1105190948Srwatson TCPSTAT_INC(tcps_sc_aborted); 1106168902Sandre else 1107190948Srwatson TCPSTAT_INC(tcps_sc_completed); 1108122922Sandre 1109178888Sjulian/* how do we find the inp for the new socket? */ 1110162277Sandre if (sc != &scs) 1111162277Sandre syncache_free(sc); 1112159695Sandre return (1); 1113159695Sandrefailed: 1114162277Sandre if (sc != NULL && sc != &scs) 111586764Sjlemon syncache_free(sc); 1116169685Sandre if (s != NULL) 1117169685Sandre free(s, M_TCPLOG); 1118168902Sandre *lsop = NULL; 1119159695Sandre return (0); 112086764Sjlemon} 112186764Sjlemon 1122292823Spkelsey#ifdef TCP_RFC7413 1123292823Spkelseystatic void 1124292823Spkelseysyncache_tfo_expand(struct syncache *sc, struct socket **lsop, struct mbuf *m, 1125292823Spkelsey uint64_t response_cookie) 1126292823Spkelsey{ 1127292823Spkelsey struct inpcb *inp; 1128292823Spkelsey struct tcpcb *tp; 1129292823Spkelsey unsigned int *pending_counter; 1130292823Spkelsey 1131292823Spkelsey /* 1132292823Spkelsey * Global TCP locks are held because we manipulate the PCB lists 1133292823Spkelsey * and create a new socket. 1134292823Spkelsey */ 1135309108Sjch INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1136292823Spkelsey 1137292823Spkelsey pending_counter = intotcpcb(sotoinpcb(*lsop))->t_tfo_pending; 1138292823Spkelsey *lsop = syncache_socket(sc, *lsop, m); 1139292823Spkelsey if (*lsop == NULL) { 1140292823Spkelsey TCPSTAT_INC(tcps_sc_aborted); 1141292823Spkelsey atomic_subtract_int(pending_counter, 1); 1142292823Spkelsey } else { 1143292823Spkelsey inp = sotoinpcb(*lsop); 1144292823Spkelsey tp = intotcpcb(inp); 1145292823Spkelsey tp->t_flags |= TF_FASTOPEN; 1146292823Spkelsey tp->t_tfo_cookie = response_cookie; 1147292823Spkelsey tp->snd_max = tp->iss; 1148292823Spkelsey tp->snd_nxt = tp->iss; 1149292823Spkelsey tp->t_tfo_pending = pending_counter; 1150292823Spkelsey TCPSTAT_INC(tcps_sc_completed); 1151292823Spkelsey } 1152292823Spkelsey} 1153292823Spkelsey#endif /* TCP_RFC7413 */ 1154292823Spkelsey 115586764Sjlemon/* 115686764Sjlemon * Given a LISTEN socket and an inbound SYN request, add 115786764Sjlemon * this to the syn cache, and send back a segment: 115886764Sjlemon * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 115986764Sjlemon * to the source. 116086764Sjlemon * 116186764Sjlemon * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 116286764Sjlemon * Doing so would require that we hold onto the data and deliver it 116386764Sjlemon * to the application. However, if we are the target of a SYN-flood 116486764Sjlemon * DoS attack, an attacker could send data which would eventually 116586764Sjlemon * consume all available buffer space if it were ACKed. By not ACKing 116686764Sjlemon * the data, we avoid this DoS scenario. 1167292823Spkelsey * 1168292823Spkelsey * The exception to the above is when a SYN with a valid TCP Fast Open (TFO) 1169292823Spkelsey * cookie is processed, V_tcp_fastopen_enabled set to true, and the 1170292823Spkelsey * TCP_FASTOPEN socket option is set. In this case, a new socket is created 1171292823Spkelsey * and returned via lsop, the mbuf is not freed so that tcp_input() can 1172292823Spkelsey * queue its data to the socket, and 1 is returned to indicate the 1173292823Spkelsey * TFO-socket-creation path was taken. 117486764Sjlemon */ 1175292823Spkelseyint 1176237263Snpsyncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th, 1177237263Snp struct inpcb *inp, struct socket **lsop, struct mbuf *m, void *tod, 1178237263Snp void *todctx) 117986764Sjlemon{ 118086764Sjlemon struct tcpcb *tp; 118186764Sjlemon struct socket *so; 118286764Sjlemon struct syncache *sc = NULL; 118386764Sjlemon struct syncache_head *sch; 118486764Sjlemon struct mbuf *ipopts = NULL; 1185221023Sattilio u_int ltflags; 1186221023Sattilio int win, sb_hiwat, ip_ttl, ip_tos; 1187171639Sandre char *s; 1188292823Spkelsey int rv = 0; 1189159701Sandre#ifdef INET6 1190159701Sandre int autoflowlabel = 0; 1191159701Sandre#endif 1192165149Scsjp#ifdef MAC 1193165149Scsjp struct label *maclabel; 1194165149Scsjp#endif 1195162277Sandre struct syncache scs; 1196182056Sbz struct ucred *cred; 1197292823Spkelsey#ifdef TCP_RFC7413 1198292823Spkelsey uint64_t tfo_response_cookie; 1199292823Spkelsey int tfo_cookie_valid = 0; 1200292823Spkelsey int tfo_response_cookie_valid = 0; 1201292823Spkelsey#endif 120286764Sjlemon 1203178285Srwatson INP_WLOCK_ASSERT(inp); /* listen socket */ 1204171643Ssilby KASSERT((th->th_flags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN, 1205171639Sandre ("%s: unexpected tcp flags", __func__)); 1206122496Ssam 1207159695Sandre /* 1208159695Sandre * Combine all so/tp operations very early to drop the INP lock as 1209159695Sandre * soon as possible. 1210159695Sandre */ 1211159695Sandre so = *lsop; 121286764Sjlemon tp = sototcpcb(so); 1213182056Sbz cred = crhold(so->so_cred); 121486764Sjlemon 1215159695Sandre#ifdef INET6 1216186222Sbz if ((inc->inc_flags & INC_ISIPV6) && 1217186141Sbz (inp->inp_flags & IN6P_AUTOFLOWLABEL)) 1218159695Sandre autoflowlabel = 1; 1219159695Sandre#endif 1220159695Sandre ip_ttl = inp->inp_ip_ttl; 1221159695Sandre ip_tos = inp->inp_ip_tos; 1222159695Sandre win = sbspace(&so->so_rcv); 1223159695Sandre sb_hiwat = so->so_rcv.sb_hiwat; 1224221023Sattilio ltflags = (tp->t_flags & (TF_NOOPT | TF_SIGNATURE)); 1225159695Sandre 1226292823Spkelsey#ifdef TCP_RFC7413 1227292823Spkelsey if (V_tcp_fastopen_enabled && (tp->t_flags & TF_FASTOPEN) && 1228292823Spkelsey (tp->t_tfo_pending != NULL) && (to->to_flags & TOF_FASTOPEN)) { 1229292823Spkelsey /* 1230292823Spkelsey * Limit the number of pending TFO connections to 1231292823Spkelsey * approximately half of the queue limit. This prevents TFO 1232292823Spkelsey * SYN floods from starving the service by filling the 1233292823Spkelsey * listen queue with bogus TFO connections. 1234292823Spkelsey */ 1235292823Spkelsey if (atomic_fetchadd_int(tp->t_tfo_pending, 1) <= 1236292823Spkelsey (so->so_qlimit / 2)) { 1237292823Spkelsey int result; 1238292823Spkelsey 1239292823Spkelsey result = tcp_fastopen_check_cookie(inc, 1240292823Spkelsey to->to_tfo_cookie, to->to_tfo_len, 1241292823Spkelsey &tfo_response_cookie); 1242292823Spkelsey tfo_cookie_valid = (result > 0); 1243292823Spkelsey tfo_response_cookie_valid = (result >= 0); 1244292823Spkelsey } else 1245292823Spkelsey atomic_subtract_int(tp->t_tfo_pending, 1); 1246292823Spkelsey } 1247292823Spkelsey#endif 1248292823Spkelsey 1249182045Sbz /* By the time we drop the lock these should no longer be used. */ 1250159695Sandre so = NULL; 1251159695Sandre tp = NULL; 1252159695Sandre 1253165149Scsjp#ifdef MAC 1254172970Srwatson if (mac_syncache_init(&maclabel) != 0) { 1255178285Srwatson INP_WUNLOCK(inp); 1256168900Sandre goto done; 1257165149Scsjp } else 1258172970Srwatson mac_syncache_create(maclabel, inp); 1259165149Scsjp#endif 1260292823Spkelsey#ifdef TCP_RFC7413 1261303371Sjch if (!tfo_cookie_valid) 1262303371Sjch#endif 1263292823Spkelsey INP_WUNLOCK(inp); 1264303371Sjch 126586764Sjlemon /* 126686764Sjlemon * Remember the IP options, if any. 126786764Sjlemon */ 126886764Sjlemon#ifdef INET6 1269186222Sbz if (!(inc->inc_flags & INC_ISIPV6)) 127086764Sjlemon#endif 1271221250Sbz#ifdef INET 1272174704Skmacy ipopts = (m) ? ip_srcroute(m) : NULL; 1273221250Sbz#else 1274221250Sbz ipopts = NULL; 1275221250Sbz#endif 127686764Sjlemon 127786764Sjlemon /* 127886764Sjlemon * See if we already have an entry for this connection. 127986764Sjlemon * If we do, resend the SYN,ACK, and reset the retransmit timer. 128086764Sjlemon * 1281159697Sandre * XXX: should the syncache be re-initialized with the contents 128286764Sjlemon * of the new SYN here (which may have different options?) 1283171639Sandre * 1284171639Sandre * XXX: We do not check the sequence number to see if this is a 1285171639Sandre * real retransmit or a new connection attempt. The question is 1286171639Sandre * how to handle such a case; either ignore it as spoofed, or 1287171639Sandre * drop the current entry and create a new one? 128886764Sjlemon */ 1289159695Sandre sc = syncache_lookup(inc, &sch); /* returns locked entry */ 1290159695Sandre SCH_LOCK_ASSERT(sch); 129186764Sjlemon if (sc != NULL) { 1292292823Spkelsey#ifdef TCP_RFC7413 1293303371Sjch if (tfo_cookie_valid) 1294292823Spkelsey INP_WUNLOCK(inp); 1295292823Spkelsey#endif 1296190948Srwatson TCPSTAT_INC(tcps_sc_dupsyn); 129786764Sjlemon if (ipopts) { 129886764Sjlemon /* 129986764Sjlemon * If we were remembering a previous source route, 130086764Sjlemon * forget it and use the new one we've been given. 130186764Sjlemon */ 130286764Sjlemon if (sc->sc_ipopts) 130386764Sjlemon (void) m_free(sc->sc_ipopts); 130486764Sjlemon sc->sc_ipopts = ipopts; 130586764Sjlemon } 130686764Sjlemon /* 130786764Sjlemon * Update timestamp if present. 130886764Sjlemon */ 1309168901Sandre if ((sc->sc_flags & SCF_TIMESTAMP) && (to->to_flags & TOF_TS)) 1310159950Sandre sc->sc_tsreflect = to->to_tsval; 1311168901Sandre else 1312168901Sandre sc->sc_flags &= ~SCF_TIMESTAMP; 1313165149Scsjp#ifdef MAC 1314165149Scsjp /* 1315165149Scsjp * Since we have already unconditionally allocated label 1316165149Scsjp * storage, free it up. The syncache entry will already 1317165149Scsjp * have an initialized label we can use. 1318165149Scsjp */ 1319172970Srwatson mac_syncache_destroy(&maclabel); 1320165149Scsjp#endif 1321171639Sandre /* Retransmit SYN|ACK and reset retransmit count. */ 1322171639Sandre if ((s = tcp_log_addrs(&sc->sc_inc, th, NULL, NULL))) { 1323171652Sbmah log(LOG_DEBUG, "%s; %s: Received duplicate SYN, " 1324171639Sandre "resetting timer and retransmitting SYN|ACK\n", 1325171639Sandre s, __func__); 1326171639Sandre free(s, M_TCPLOG); 1327171639Sandre } 1328300004Ssephe if (syncache_respond(sc, m) == 0) { 1329171639Sandre sc->sc_rxmits = 0; 1330171639Sandre syncache_timeout(sc, sch, 1); 1331190948Srwatson TCPSTAT_INC(tcps_sndacks); 1332190948Srwatson TCPSTAT_INC(tcps_sndtotal); 133386764Sjlemon } 1334159695Sandre SCH_UNLOCK(sch); 1335159695Sandre goto done; 133686764Sjlemon } 133786764Sjlemon 1338292823Spkelsey#ifdef TCP_RFC7413 1339292823Spkelsey if (tfo_cookie_valid) { 1340292823Spkelsey bzero(&scs, sizeof(scs)); 1341292823Spkelsey sc = &scs; 1342292823Spkelsey goto skip_alloc; 1343292823Spkelsey } 1344292823Spkelsey#endif 1345292823Spkelsey 1346181803Sbz sc = uma_zalloc(V_tcp_syncache.zone, M_NOWAIT | M_ZERO); 134786764Sjlemon if (sc == NULL) { 134886764Sjlemon /* 134986764Sjlemon * The zone allocator couldn't provide more entries. 1350133874Srwatson * Treat this as if the cache was full; drop the oldest 135186764Sjlemon * entry and insert the new one. 135286764Sjlemon */ 1353190948Srwatson TCPSTAT_INC(tcps_sc_zonefail); 1354322315Stuexen if ((sc = TAILQ_LAST(&sch->sch_bucket, sch_head)) != NULL) { 1355322315Stuexen sch->sch_last_overflow = time_uptime; 1356168817Sandre syncache_drop(sc, sch); 1357322315Stuexen } 1358181803Sbz sc = uma_zalloc(V_tcp_syncache.zone, M_NOWAIT | M_ZERO); 135986764Sjlemon if (sc == NULL) { 1360185348Szec if (V_tcp_syncookies) { 1361162277Sandre bzero(&scs, sizeof(scs)); 1362162277Sandre sc = &scs; 1363162277Sandre } else { 1364162277Sandre SCH_UNLOCK(sch); 1365162277Sandre if (ipopts) 1366162277Sandre (void) m_free(ipopts); 1367162277Sandre goto done; 1368162277Sandre } 136986764Sjlemon } 1370162277Sandre } 1371292823Spkelsey 1372292823Spkelsey#ifdef TCP_RFC7413 1373292823Spkelseyskip_alloc: 1374292823Spkelsey if (!tfo_cookie_valid && tfo_response_cookie_valid) 1375292823Spkelsey sc->sc_tfo_cookie = &tfo_response_cookie; 1376292823Spkelsey#endif 1377292823Spkelsey 137886764Sjlemon /* 137986764Sjlemon * Fill in the syncache values. 138086764Sjlemon */ 1381165149Scsjp#ifdef MAC 1382165149Scsjp sc->sc_label = maclabel; 1383165149Scsjp#endif 1384182056Sbz sc->sc_cred = cred; 1385182056Sbz cred = NULL; 138686764Sjlemon sc->sc_ipopts = ipopts; 1387159950Sandre bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo)); 138886764Sjlemon#ifdef INET6 1389186222Sbz if (!(inc->inc_flags & INC_ISIPV6)) 139086764Sjlemon#endif 139186764Sjlemon { 1392159695Sandre sc->sc_ip_tos = ip_tos; 1393159695Sandre sc->sc_ip_ttl = ip_ttl; 139486764Sjlemon } 1395237263Snp#ifdef TCP_OFFLOAD 1396237263Snp sc->sc_tod = tod; 1397237263Snp sc->sc_todctx = todctx; 1398174558Skmacy#endif 139986764Sjlemon sc->sc_irs = th->th_seq; 1400162277Sandre sc->sc_iss = arc4random(); 1401110023Ssilby sc->sc_flags = 0; 1402132307Sdwmalone sc->sc_flowlabel = 0; 140386764Sjlemon 1404159695Sandre /* 1405159695Sandre * Initial receive window: clip sbspace to [0 .. TCP_MAXWIN]. 1406159695Sandre * win was derived from socket earlier in the function. 1407159695Sandre */ 140886764Sjlemon win = imax(win, 0); 140986764Sjlemon win = imin(win, TCP_MAXWIN); 141086764Sjlemon sc->sc_wnd = win; 141186764Sjlemon 1412181803Sbz if (V_tcp_do_rfc1323) { 141386764Sjlemon /* 141486764Sjlemon * A timestamp received in a SYN makes 141586764Sjlemon * it ok to send timestamp requests and replies. 141686764Sjlemon */ 141786764Sjlemon if (to->to_flags & TOF_TS) { 1418159950Sandre sc->sc_tsreflect = to->to_tsval; 1419231767Sbz sc->sc_ts = tcp_ts_getticks(); 142086764Sjlemon sc->sc_flags |= SCF_TIMESTAMP; 142186764Sjlemon } 142286764Sjlemon if (to->to_flags & TOF_SCALE) { 142386764Sjlemon int wscale = 0; 142486764Sjlemon 1425166403Sandre /* 1426172795Ssilby * Pick the smallest possible scaling factor that 1427172795Ssilby * will still allow us to scale up to sb_max, aka 1428172795Ssilby * kern.ipc.maxsockbuf. 1429167606Sandre * 1430172795Ssilby * We do this because there are broken firewalls that 1431172795Ssilby * will corrupt the window scale option, leading to 1432172795Ssilby * the other endpoint believing that our advertised 1433172795Ssilby * window is unscaled. At scale factors larger than 1434172795Ssilby * 5 the unscaled window will drop below 1500 bytes, 1435172795Ssilby * leading to serious problems when traversing these 1436172795Ssilby * broken firewalls. 1437172795Ssilby * 1438172795Ssilby * With the default maxsockbuf of 256K, a scale factor 1439172795Ssilby * of 3 will be chosen by this algorithm. Those who 1440172795Ssilby * choose a larger maxsockbuf should watch out 1441172795Ssilby * for the compatiblity problems mentioned above. 1442172795Ssilby * 1443167606Sandre * RFC1323: The Window field in a SYN (i.e., a <SYN> 1444167606Sandre * or <SYN,ACK>) segment itself is never scaled. 1445166403Sandre */ 144686764Sjlemon while (wscale < TCP_MAX_WINSHIFT && 1447172795Ssilby (TCP_MAXWIN << wscale) < sb_max) 144886764Sjlemon wscale++; 1449159950Sandre sc->sc_requested_r_scale = wscale; 1450167606Sandre sc->sc_requested_s_scale = to->to_wscale; 145186764Sjlemon sc->sc_flags |= SCF_WINSCALE; 145286764Sjlemon } 145386764Sjlemon } 1454125680Sbms#ifdef TCP_SIGNATURE 1455125680Sbms /* 1456145371Sps * If listening socket requested TCP digests, and received SYN 1457145371Sps * contains the option, flag this in the syncache so that 1458145371Sps * syncache_respond() will do the right thing with the SYN+ACK. 1459159697Sandre * XXX: Currently we always record the option by default and will 1460145371Sps * attempt to use it in syncache_respond(). 1461125680Sbms */ 1462221023Sattilio if (to->to_flags & TOF_SIGNATURE || ltflags & TF_SIGNATURE) 1463150131Sandre sc->sc_flags |= SCF_SIGNATURE; 1464125783Sbms#endif 1465174248Ssilby if (to->to_flags & TOF_SACKPERM) 1466130989Sps sc->sc_flags |= SCF_SACK; 1467162277Sandre if (to->to_flags & TOF_MSS) 1468162277Sandre sc->sc_peer_mss = to->to_mss; /* peer mss may be zero */ 1469221023Sattilio if (ltflags & TF_NOOPT) 1470159727Sandre sc->sc_flags |= SCF_NOOPT; 1471181803Sbz if ((th->th_flags & (TH_ECE|TH_CWR)) && V_tcp_do_ecn) 1472181056Srpaulo sc->sc_flags |= SCF_ECN; 1473130989Sps 1474253210Sandre if (V_tcp_syncookies) 1475253210Sandre sc->sc_iss = syncookie_generate(sch, sc); 1476162277Sandre#ifdef INET6 1477253210Sandre if (autoflowlabel) { 1478253210Sandre if (V_tcp_syncookies) 1479253210Sandre sc->sc_flowlabel = sc->sc_iss; 1480253210Sandre else 1481253210Sandre sc->sc_flowlabel = ip6_randomflowlabel(); 1482253210Sandre sc->sc_flowlabel = htonl(sc->sc_flowlabel) & IPV6_FLOWLABEL_MASK; 1483253210Sandre } 1484162277Sandre#endif 1485162277Sandre SCH_UNLOCK(sch); 1486162277Sandre 1487292823Spkelsey#ifdef TCP_RFC7413 1488292823Spkelsey if (tfo_cookie_valid) { 1489292823Spkelsey syncache_tfo_expand(sc, lsop, m, tfo_response_cookie); 1490292823Spkelsey /* INP_WUNLOCK(inp) will be performed by the called */ 1491292823Spkelsey rv = 1; 1492292823Spkelsey goto tfo_done; 1493292823Spkelsey } 1494292823Spkelsey#endif 1495292823Spkelsey 149686764Sjlemon /* 1497137139Sandre * Do a standard 3-way handshake. 149886764Sjlemon */ 1499300004Ssephe if (syncache_respond(sc, m) == 0) { 1500185348Szec if (V_tcp_syncookies && V_tcp_syncookiesonly && sc != &scs) 1501162277Sandre syncache_free(sc); 1502162277Sandre else if (sc != &scs) 1503162277Sandre syncache_insert(sc, sch); /* locks and unlocks sch */ 1504190948Srwatson TCPSTAT_INC(tcps_sndacks); 1505190948Srwatson TCPSTAT_INC(tcps_sndtotal); 150686764Sjlemon } else { 1507165149Scsjp if (sc != &scs) 1508165149Scsjp syncache_free(sc); 1509190948Srwatson TCPSTAT_INC(tcps_sc_dropped); 151086764Sjlemon } 1511159695Sandre 1512159695Sandredone: 1513292823Spkelsey if (m) { 1514292823Spkelsey *lsop = NULL; 1515292823Spkelsey m_freem(m); 1516292823Spkelsey } 1517292823Spkelsey#ifdef TCP_RFC7413 1518292823Spkelseytfo_done: 1519292823Spkelsey#endif 1520182056Sbz if (cred != NULL) 1521182056Sbz crfree(cred); 1522168900Sandre#ifdef MAC 1523168900Sandre if (sc == &scs) 1524172970Srwatson mac_syncache_destroy(&maclabel); 1525168900Sandre#endif 1526292823Spkelsey return (rv); 152786764Sjlemon} 152886764Sjlemon 1529300004Ssephe/* 1530300004Ssephe * Send SYN|ACK to the peer. Either in response to the peer's SYN, 1531300004Ssephe * i.e. m0 != NULL, or upon 3WHS ACK timeout, i.e. m0 == NULL. 1532300004Ssephe */ 153386764Sjlemonstatic int 1534300004Ssephesyncache_respond(struct syncache *sc, const struct mbuf *m0) 153586764Sjlemon{ 1536159950Sandre struct ip *ip = NULL; 1537168900Sandre struct mbuf *m; 1538221250Sbz struct tcphdr *th = NULL; 1539221250Sbz int optlen, error = 0; /* Make compiler happy */ 1540167606Sandre u_int16_t hlen, tlen, mssopt; 1541167606Sandre struct tcpopt to; 154286764Sjlemon#ifdef INET6 154386764Sjlemon struct ip6_hdr *ip6 = NULL; 154486764Sjlemon#endif 154586764Sjlemon 1546122922Sandre hlen = 154786764Sjlemon#ifdef INET6 1548186222Sbz (sc->sc_inc.inc_flags & INC_ISIPV6) ? sizeof(struct ip6_hdr) : 154986764Sjlemon#endif 1550122922Sandre sizeof(struct ip); 1551167606Sandre tlen = hlen + sizeof(struct tcphdr); 155286764Sjlemon 1553159697Sandre /* Determine MSS we advertize to other end of connection. */ 1554324520Ssephe mssopt = max(tcp_mssopt(&sc->sc_inc), V_tcp_minmss); 1555122922Sandre 1556167606Sandre /* XXX: Assume that the entire packet will fit in a header mbuf. */ 1557168904Sandre KASSERT(max_linkhdr + tlen + TCP_MAXOLEN <= MHLEN, 1558167606Sandre ("syncache: mbuf too small")); 155986764Sjlemon 1560159695Sandre /* Create the IP+TCP header from scratch. */ 1561243882Sglebius m = m_gethdr(M_NOWAIT, MT_DATA); 156286764Sjlemon if (m == NULL) 156386764Sjlemon return (ENOBUFS); 1564165149Scsjp#ifdef MAC 1565172970Srwatson mac_syncache_create_mbuf(sc->sc_label, m); 1566165149Scsjp#endif 156786764Sjlemon m->m_data += max_linkhdr; 156886764Sjlemon m->m_len = tlen; 156986764Sjlemon m->m_pkthdr.len = tlen; 157086764Sjlemon m->m_pkthdr.rcvif = NULL; 1571159695Sandre 157286764Sjlemon#ifdef INET6 1573186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) { 157486764Sjlemon ip6 = mtod(m, struct ip6_hdr *); 157586764Sjlemon ip6->ip6_vfc = IPV6_VERSION; 157686764Sjlemon ip6->ip6_nxt = IPPROTO_TCP; 157786764Sjlemon ip6->ip6_src = sc->sc_inc.inc6_laddr; 157886764Sjlemon ip6->ip6_dst = sc->sc_inc.inc6_faddr; 157986764Sjlemon ip6->ip6_plen = htons(tlen - hlen); 158086764Sjlemon /* ip6_hlim is set after checksum */ 1581132307Sdwmalone ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK; 1582132307Sdwmalone ip6->ip6_flow |= sc->sc_flowlabel; 158386764Sjlemon 158486764Sjlemon th = (struct tcphdr *)(ip6 + 1); 1585221250Sbz } 158686764Sjlemon#endif 1587221250Sbz#if defined(INET6) && defined(INET) 1588221250Sbz else 1589221250Sbz#endif 1590221250Sbz#ifdef INET 159186764Sjlemon { 159286764Sjlemon ip = mtod(m, struct ip *); 159386764Sjlemon ip->ip_v = IPVERSION; 159486764Sjlemon ip->ip_hl = sizeof(struct ip) >> 2; 1595241913Sglebius ip->ip_len = htons(tlen); 159686764Sjlemon ip->ip_id = 0; 159786764Sjlemon ip->ip_off = 0; 159886764Sjlemon ip->ip_sum = 0; 159986764Sjlemon ip->ip_p = IPPROTO_TCP; 160086764Sjlemon ip->ip_src = sc->sc_inc.inc_laddr; 160186764Sjlemon ip->ip_dst = sc->sc_inc.inc_faddr; 1602159695Sandre ip->ip_ttl = sc->sc_ip_ttl; 1603159695Sandre ip->ip_tos = sc->sc_ip_tos; 160486764Sjlemon 160598204Ssilby /* 1606108125Shsu * See if we should do MTU discovery. Route lookups are 1607108125Shsu * expensive, so we will only unset the DF bit if: 1608101405Ssilby * 1609101405Ssilby * 1) path_mtu_discovery is disabled 1610101405Ssilby * 2) the SCF_UNREACH flag has been set 161198204Ssilby */ 1612181803Sbz if (V_path_mtu_discovery && ((sc->sc_flags & SCF_UNREACH) == 0)) 1613241913Sglebius ip->ip_off |= htons(IP_DF); 161498204Ssilby 161586764Sjlemon th = (struct tcphdr *)(ip + 1); 161686764Sjlemon } 1617221250Sbz#endif /* INET */ 161886764Sjlemon th->th_sport = sc->sc_inc.inc_lport; 161986764Sjlemon th->th_dport = sc->sc_inc.inc_fport; 162086764Sjlemon 162186764Sjlemon th->th_seq = htonl(sc->sc_iss); 162286764Sjlemon th->th_ack = htonl(sc->sc_irs + 1); 1623167606Sandre th->th_off = sizeof(struct tcphdr) >> 2; 162486764Sjlemon th->th_x2 = 0; 162586764Sjlemon th->th_flags = TH_SYN|TH_ACK; 162686764Sjlemon th->th_win = htons(sc->sc_wnd); 162786764Sjlemon th->th_urp = 0; 162886764Sjlemon 1629181056Srpaulo if (sc->sc_flags & SCF_ECN) { 1630181056Srpaulo th->th_flags |= TH_ECE; 1631190948Srwatson TCPSTAT_INC(tcps_ecn_shs); 1632181056Srpaulo } 1633181056Srpaulo 163486764Sjlemon /* Tack on the TCP options. */ 1635167606Sandre if ((sc->sc_flags & SCF_NOOPT) == 0) { 1636167606Sandre to.to_flags = 0; 163786764Sjlemon 1638167606Sandre to.to_mss = mssopt; 1639167606Sandre to.to_flags = TOF_MSS; 1640108125Shsu if (sc->sc_flags & SCF_WINSCALE) { 1641167606Sandre to.to_wscale = sc->sc_requested_r_scale; 1642167606Sandre to.to_flags |= TOF_SCALE; 1643108125Shsu } 1644108125Shsu if (sc->sc_flags & SCF_TIMESTAMP) { 1645167606Sandre /* Virgin timestamp or TCP cookie enhanced one. */ 1646169686Sandre to.to_tsval = sc->sc_ts; 1647167606Sandre to.to_tsecr = sc->sc_tsreflect; 1648167606Sandre to.to_flags |= TOF_TS; 1649108125Shsu } 1650167606Sandre if (sc->sc_flags & SCF_SACK) 1651167606Sandre to.to_flags |= TOF_SACKPERM; 1652167606Sandre#ifdef TCP_SIGNATURE 1653167606Sandre if (sc->sc_flags & SCF_SIGNATURE) 1654167606Sandre to.to_flags |= TOF_SIGNATURE; 1655167606Sandre#endif 1656292823Spkelsey 1657292823Spkelsey#ifdef TCP_RFC7413 1658292823Spkelsey if (sc->sc_tfo_cookie) { 1659292823Spkelsey to.to_flags |= TOF_FASTOPEN; 1660292823Spkelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 1661292823Spkelsey to.to_tfo_cookie = sc->sc_tfo_cookie; 1662292823Spkelsey /* don't send cookie again when retransmitting response */ 1663292823Spkelsey sc->sc_tfo_cookie = NULL; 1664292823Spkelsey } 1665292823Spkelsey#endif 1666167606Sandre optlen = tcp_addoptions(&to, (u_char *)(th + 1)); 166786764Sjlemon 1668167606Sandre /* Adjust headers by option size. */ 1669167606Sandre th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 1670167606Sandre m->m_len += optlen; 1671167606Sandre m->m_pkthdr.len += optlen; 1672174119Sbz 1673174119Sbz#ifdef TCP_SIGNATURE 1674174119Sbz if (sc->sc_flags & SCF_SIGNATURE) 1675183001Sbz tcp_signature_compute(m, 0, 0, optlen, 1676174119Sbz to.to_signature, IPSEC_DIR_OUTBOUND); 1677174119Sbz#endif 1678167606Sandre#ifdef INET6 1679186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) 1680167606Sandre ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) + optlen); 1681167658Skmacy else 1682167606Sandre#endif 1683241913Sglebius ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 1684167606Sandre } else 1685167606Sandre optlen = 0; 1686130989Sps 1687195922Sjulian M_SETFIB(m, sc->sc_inc.inc_fibnum); 1688235961Sbz m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1689300004Ssephe /* 1690300004Ssephe * If we have peer's SYN and it has a flowid, then let's assign it to 1691300004Ssephe * our SYN|ACK. ip6_output() and ip_output() will not assign flowid 1692300004Ssephe * to SYN|ACK due to lack of inp here. 1693300004Ssephe */ 1694300004Ssephe if (m0 != NULL && M_HASHTYPE_GET(m0) != M_HASHTYPE_NONE) { 1695300004Ssephe m->m_pkthdr.flowid = m0->m_pkthdr.flowid; 1696300004Ssephe M_HASHTYPE_SET(m, M_HASHTYPE_GET(m0)); 1697300004Ssephe } 169886764Sjlemon#ifdef INET6 1699186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) { 1700236170Sbz m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 1701235961Sbz th->th_sum = in6_cksum_pseudo(ip6, tlen + optlen - hlen, 1702235961Sbz IPPROTO_TCP, 0); 1703122922Sandre ip6->ip6_hlim = in6_selecthlim(NULL, NULL); 1704245919Snp#ifdef TCP_OFFLOAD 1705245919Snp if (ADDED_BY_TOE(sc)) { 1706245919Snp struct toedev *tod = sc->sc_tod; 1707245919Snp 1708245919Snp error = tod->tod_syncache_respond(tod, sc->sc_todctx, m); 1709245919Snp 1710245919Snp return (error); 1711245919Snp } 1712245919Snp#endif 1713159695Sandre error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 1714221250Sbz } 171586764Sjlemon#endif 1716221250Sbz#if defined(INET6) && defined(INET) 1717221250Sbz else 1718221250Sbz#endif 1719221250Sbz#ifdef INET 172086764Sjlemon { 1721236170Sbz m->m_pkthdr.csum_flags = CSUM_TCP; 1722133874Srwatson th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 1723167606Sandre htons(tlen + optlen - hlen + IPPROTO_TCP)); 1724237263Snp#ifdef TCP_OFFLOAD 1725237263Snp if (ADDED_BY_TOE(sc)) { 1726237263Snp struct toedev *tod = sc->sc_tod; 1727237263Snp 1728237263Snp error = tod->tod_syncache_respond(tod, sc->sc_todctx, m); 1729237263Snp 1730237263Snp return (error); 1731237263Snp } 1732237263Snp#endif 1733159695Sandre error = ip_output(m, sc->sc_ipopts, NULL, 0, NULL, NULL); 173486764Sjlemon } 1735221250Sbz#endif 173686764Sjlemon return (error); 173786764Sjlemon} 173888180Sjlemon 173988180Sjlemon/* 1740253210Sandre * The purpose of syncookies is to handle spoofed SYN flooding DoS attacks 1741253210Sandre * that exceed the capacity of the syncache by avoiding the storage of any 1742253210Sandre * of the SYNs we receive. Syncookies defend against blind SYN flooding 1743253210Sandre * attacks where the attacker does not have access to our responses. 174488180Sjlemon * 1745253210Sandre * Syncookies encode and include all necessary information about the 1746253210Sandre * connection setup within the SYN|ACK that we send back. That way we 1747253210Sandre * can avoid keeping any local state until the ACK to our SYN|ACK returns 1748253210Sandre * (if ever). Normally the syncache and syncookies are running in parallel 1749253210Sandre * with the latter taking over when the former is exhausted. When matching 1750253210Sandre * syncache entry is found the syncookie is ignored. 1751162277Sandre * 1752253210Sandre * The only reliable information persisting the 3WHS is our inital sequence 1753253210Sandre * number ISS of 32 bits. Syncookies embed a cryptographically sufficient 1754253210Sandre * strong hash (MAC) value and a few bits of TCP SYN options in the ISS 1755253210Sandre * of our SYN|ACK. The MAC can be recomputed when the ACK to our SYN|ACK 1756253210Sandre * returns and signifies a legitimate connection if it matches the ACK. 1757162277Sandre * 1758253210Sandre * The available space of 32 bits to store the hash and to encode the SYN 1759253210Sandre * option information is very tight and we should have at least 24 bits for 1760253210Sandre * the MAC to keep the number of guesses by blind spoofing reasonably high. 1761162277Sandre * 1762253210Sandre * SYN option information we have to encode to fully restore a connection: 1763253210Sandre * MSS: is imporant to chose an optimal segment size to avoid IP level 1764253210Sandre * fragmentation along the path. The common MSS values can be encoded 1765253210Sandre * in a 3-bit table. Uncommon values are captured by the next lower value 1766253210Sandre * in the table leading to a slight increase in packetization overhead. 1767253210Sandre * WSCALE: is necessary to allow large windows to be used for high delay- 1768253210Sandre * bandwidth product links. Not scaling the window when it was initially 1769253210Sandre * negotiated is bad for performance as lack of scaling further decreases 1770253210Sandre * the apparent available send window. We only need to encode the WSCALE 1771253210Sandre * we received from the remote end. Our end can be recalculated at any 1772253210Sandre * time. The common WSCALE values can be encoded in a 3-bit table. 1773253210Sandre * Uncommon values are captured by the next lower value in the table 1774253210Sandre * making us under-estimate the available window size halving our 1775253210Sandre * theoretically possible maximum throughput for that connection. 1776253210Sandre * SACK: Greatly assists in packet loss recovery and requires 1 bit. 1777253210Sandre * TIMESTAMP and SIGNATURE is not encoded because they are permanent options 1778253210Sandre * that are included in all segments on a connection. We enable them when 1779253210Sandre * the ACK has them. 1780162277Sandre * 1781253210Sandre * Security of syncookies and attack vectors: 1782162277Sandre * 1783253210Sandre * The MAC is computed over (faddr||laddr||fport||lport||irs||flags||secmod) 1784253210Sandre * together with the gloabl secret to make it unique per connection attempt. 1785253210Sandre * Thus any change of any of those parameters results in a different MAC output 1786253210Sandre * in an unpredictable way unless a collision is encountered. 24 bits of the 1787253210Sandre * MAC are embedded into the ISS. 1788162277Sandre * 1789253210Sandre * To prevent replay attacks two rotating global secrets are updated with a 1790253210Sandre * new random value every 15 seconds. The life-time of a syncookie is thus 1791253210Sandre * 15-30 seconds. 1792162277Sandre * 1793253210Sandre * Vector 1: Attacking the secret. This requires finding a weakness in the 1794253210Sandre * MAC itself or the way it is used here. The attacker can do a chosen plain 1795253210Sandre * text attack by varying and testing the all parameters under his control. 1796253210Sandre * The strength depends on the size and randomness of the secret, and the 1797253210Sandre * cryptographic security of the MAC function. Due to the constant updating 1798253210Sandre * of the secret the attacker has at most 29.999 seconds to find the secret 1799253210Sandre * and launch spoofed connections. After that he has to start all over again. 1800162277Sandre * 1801253210Sandre * Vector 2: Collision attack on the MAC of a single ACK. With a 24 bit MAC 1802253210Sandre * size an average of 4,823 attempts are required for a 50% chance of success 1803253210Sandre * to spoof a single syncookie (birthday collision paradox). However the 1804253210Sandre * attacker is blind and doesn't know if one of his attempts succeeded unless 1805253210Sandre * he has a side channel to interfere success from. A single connection setup 1806253210Sandre * success average of 90% requires 8,790 packets, 99.99% requires 17,578 packets. 1807253210Sandre * This many attempts are required for each one blind spoofed connection. For 1808253210Sandre * every additional spoofed connection he has to launch another N attempts. 1809253210Sandre * Thus for a sustained rate 100 spoofed connections per second approximately 1810253210Sandre * 1,800,000 packets per second would have to be sent. 181188180Sjlemon * 1812253210Sandre * NB: The MAC function should be fast so that it doesn't become a CPU 1813253210Sandre * exhaustion attack vector itself. 1814253210Sandre * 1815253210Sandre * References: 1816253210Sandre * RFC4987 TCP SYN Flooding Attacks and Common Mitigations 1817253210Sandre * SYN cookies were first proposed by cryptographer Dan J. Bernstein in 1996 1818253210Sandre * http://cr.yp.to/syncookies.html (overview) 1819253210Sandre * http://cr.yp.to/syncookies/archive (details) 1820253210Sandre * 1821253210Sandre * 1822253210Sandre * Schematic construction of a syncookie enabled Initial Sequence Number: 1823253210Sandre * 0 1 2 3 1824253210Sandre * 12345678901234567890123456789012 1825253210Sandre * |xxxxxxxxxxxxxxxxxxxxxxxxWWWMMMSP| 1826253210Sandre * 1827253210Sandre * x 24 MAC (truncated) 1828253210Sandre * W 3 Send Window Scale index 1829253210Sandre * M 3 MSS index 1830253210Sandre * S 1 SACK permitted 1831253210Sandre * P 1 Odd/even secret 183288180Sjlemon */ 183388180Sjlemon 1834253210Sandre/* 1835253210Sandre * Distribution and probability of certain MSS values. Those in between are 1836253210Sandre * rounded down to the next lower one. 1837253210Sandre * [An Analysis of TCP Maximum Segment Sizes, S. Alcock and R. Nelson, 2011] 1838253210Sandre * .2% .3% 5% 7% 7% 20% 15% 45% 1839253210Sandre */ 1840253210Sandrestatic int tcp_sc_msstab[] = { 216, 536, 1200, 1360, 1400, 1440, 1452, 1460 }; 1841253210Sandre 1842253210Sandre/* 1843253210Sandre * Distribution and probability of certain WSCALE values. We have to map the 1844253210Sandre * (send) window scale (shift) option with a range of 0-14 from 4 bits into 3 1845253210Sandre * bits based on prevalence of certain values. Where we don't have an exact 1846253210Sandre * match for are rounded down to the next lower one letting us under-estimate 1847253210Sandre * the true available window. At the moment this would happen only for the 1848253210Sandre * very uncommon values 3, 5 and those above 8 (more than 16MB socket buffer 1849253210Sandre * and window size). The absence of the WSCALE option (no scaling in either 1850253210Sandre * direction) is encoded with index zero. 1851253210Sandre * [WSCALE values histograms, Allman, 2012] 1852253210Sandre * X 10 10 35 5 6 14 10% by host 1853253210Sandre * X 11 4 5 5 18 49 3% by connections 1854253210Sandre */ 1855253210Sandrestatic int tcp_sc_wstab[] = { 0, 0, 1, 2, 4, 6, 7, 8 }; 1856253210Sandre 1857253210Sandre/* 1858253210Sandre * Compute the MAC for the SYN cookie. SIPHASH-2-4 is chosen for its speed 1859253210Sandre * and good cryptographic properties. 1860253210Sandre */ 1861253210Sandrestatic uint32_t 1862253210Sandresyncookie_mac(struct in_conninfo *inc, tcp_seq irs, uint8_t flags, 1863253210Sandre uint8_t *secbits, uintptr_t secmod) 1864162277Sandre{ 1865253210Sandre SIPHASH_CTX ctx; 1866253210Sandre uint32_t siphash[2]; 1867159695Sandre 1868253210Sandre SipHash24_Init(&ctx); 1869253210Sandre SipHash_SetKey(&ctx, secbits); 1870253210Sandre switch (inc->inc_flags & INC_ISIPV6) { 1871253210Sandre#ifdef INET 1872253210Sandre case 0: 1873253210Sandre SipHash_Update(&ctx, &inc->inc_faddr, sizeof(inc->inc_faddr)); 1874253210Sandre SipHash_Update(&ctx, &inc->inc_laddr, sizeof(inc->inc_laddr)); 1875253210Sandre break; 1876253210Sandre#endif 1877253210Sandre#ifdef INET6 1878253210Sandre case INC_ISIPV6: 1879253210Sandre SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(inc->inc6_faddr)); 1880253210Sandre SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(inc->inc6_laddr)); 1881253210Sandre break; 1882253210Sandre#endif 1883253210Sandre } 1884253210Sandre SipHash_Update(&ctx, &inc->inc_fport, sizeof(inc->inc_fport)); 1885253210Sandre SipHash_Update(&ctx, &inc->inc_lport, sizeof(inc->inc_lport)); 1886293402Sstas SipHash_Update(&ctx, &irs, sizeof(irs)); 1887253210Sandre SipHash_Update(&ctx, &flags, sizeof(flags)); 1888253210Sandre SipHash_Update(&ctx, &secmod, sizeof(secmod)); 1889253210Sandre SipHash_Final((u_int8_t *)&siphash, &ctx); 1890253210Sandre 1891253210Sandre return (siphash[0] ^ siphash[1]); 1892253210Sandre} 1893253210Sandre 1894253210Sandrestatic tcp_seq 1895253210Sandresyncookie_generate(struct syncache_head *sch, struct syncache *sc) 1896253210Sandre{ 1897324520Ssephe u_int i, secbit, wscale; 1898253210Sandre uint32_t iss, hash; 1899253210Sandre uint8_t *secbits; 1900253210Sandre union syncookie cookie; 1901253210Sandre 1902162277Sandre SCH_LOCK_ASSERT(sch); 1903162277Sandre 1904253210Sandre cookie.cookie = 0; 1905162277Sandre 1906253210Sandre /* Map our computed MSS into the 3-bit index. */ 1907253210Sandre for (i = sizeof(tcp_sc_msstab) / sizeof(*tcp_sc_msstab) - 1; 1908324520Ssephe tcp_sc_msstab[i] > sc->sc_peer_mss && i > 0; 1909253210Sandre i--) 1910253210Sandre ; 1911253210Sandre cookie.flags.mss_idx = i; 1912253210Sandre 1913253210Sandre /* 1914253210Sandre * Map the send window scale into the 3-bit index but only if 1915253210Sandre * the wscale option was received. 1916253210Sandre */ 1917253210Sandre if (sc->sc_flags & SCF_WINSCALE) { 1918253210Sandre wscale = sc->sc_requested_s_scale; 1919253210Sandre for (i = sizeof(tcp_sc_wstab) / sizeof(*tcp_sc_wstab) - 1; 1920253210Sandre tcp_sc_wstab[i] > wscale && i > 0; 1921253210Sandre i--) 1922253210Sandre ; 1923253210Sandre cookie.flags.wscale_idx = i; 1924159695Sandre } 1925159695Sandre 1926253210Sandre /* Can we do SACK? */ 1927253210Sandre if (sc->sc_flags & SCF_SACK) 1928253210Sandre cookie.flags.sack_ok = 1; 192988180Sjlemon 1930253210Sandre /* Which of the two secrets to use. */ 1931253210Sandre secbit = sch->sch_sc->secret.oddeven & 0x1; 1932253210Sandre cookie.flags.odd_even = secbit; 1933162277Sandre 1934253210Sandre secbits = sch->sch_sc->secret.key[secbit]; 1935253210Sandre hash = syncookie_mac(&sc->sc_inc, sc->sc_irs, cookie.cookie, secbits, 1936253210Sandre (uintptr_t)sch); 1937162277Sandre 1938253210Sandre /* 1939253210Sandre * Put the flags into the hash and XOR them to get better ISS number 1940253210Sandre * variance. This doesn't enhance the cryptographic strength and is 1941253210Sandre * done to prevent the 8 cookie bits from showing up directly on the 1942253210Sandre * wire. 1943253210Sandre */ 1944253210Sandre iss = hash & ~0xff; 1945253210Sandre iss |= cookie.cookie ^ (hash >> 24); 1946162277Sandre 1947253210Sandre /* Randomize the timestamp. */ 1948162277Sandre if (sc->sc_flags & SCF_TIMESTAMP) { 1949253210Sandre sc->sc_ts = arc4random(); 1950253210Sandre sc->sc_tsoff = sc->sc_ts - tcp_ts_getticks(); 1951169686Sandre } 1952162277Sandre 1953190948Srwatson TCPSTAT_INC(tcps_sc_sendcookie); 1954253210Sandre return (iss); 195588180Sjlemon} 195688180Sjlemon 195788180Sjlemonstatic struct syncache * 1958162277Sandresyncookie_lookup(struct in_conninfo *inc, struct syncache_head *sch, 1959253210Sandre struct syncache *sc, struct tcphdr *th, struct tcpopt *to, 1960253210Sandre struct socket *lso) 196188180Sjlemon{ 1962253210Sandre uint32_t hash; 1963253210Sandre uint8_t *secbits; 1964162277Sandre tcp_seq ack, seq; 1965253210Sandre int wnd, wscale = 0; 1966253210Sandre union syncookie cookie; 196788180Sjlemon 1968162277Sandre SCH_LOCK_ASSERT(sch); 1969162277Sandre 1970162277Sandre /* 1971253210Sandre * Pull information out of SYN-ACK/ACK and revert sequence number 1972253210Sandre * advances. 1973162277Sandre */ 1974162277Sandre ack = th->th_ack - 1; 1975162277Sandre seq = th->th_seq - 1; 1976162277Sandre 1977162277Sandre /* 1978253210Sandre * Unpack the flags containing enough information to restore the 1979253210Sandre * connection. 1980162277Sandre */ 1981253210Sandre cookie.cookie = (ack & 0xff) ^ (ack >> 24); 1982162277Sandre 1983253210Sandre /* Which of the two secrets to use. */ 1984253210Sandre secbits = sch->sch_sc->secret.key[cookie.flags.odd_even]; 1985162277Sandre 1986253210Sandre hash = syncookie_mac(inc, seq, cookie.cookie, secbits, (uintptr_t)sch); 1987253210Sandre 1988253210Sandre /* The recomputed hash matches the ACK if this was a genuine cookie. */ 1989253210Sandre if ((ack & ~0xff) != (hash & ~0xff)) 199088180Sjlemon return (NULL); 199188180Sjlemon 1992162277Sandre /* Fill in the syncache values. */ 1993253210Sandre sc->sc_flags = 0; 1994162277Sandre bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo)); 199588180Sjlemon sc->sc_ipopts = NULL; 1996162277Sandre 1997162277Sandre sc->sc_irs = seq; 1998162277Sandre sc->sc_iss = ack; 1999162277Sandre 2000253210Sandre switch (inc->inc_flags & INC_ISIPV6) { 2001253210Sandre#ifdef INET 2002253210Sandre case 0: 2003253210Sandre sc->sc_ip_ttl = sotoinpcb(lso)->inp_ip_ttl; 2004253210Sandre sc->sc_ip_tos = sotoinpcb(lso)->inp_ip_tos; 2005253210Sandre break; 2006253210Sandre#endif 200788180Sjlemon#ifdef INET6 2008253210Sandre case INC_ISIPV6: 2009253210Sandre if (sotoinpcb(lso)->inp_flags & IN6P_AUTOFLOWLABEL) 2010253210Sandre sc->sc_flowlabel = sc->sc_iss & IPV6_FLOWLABEL_MASK; 2011253210Sandre break; 201288180Sjlemon#endif 201388180Sjlemon } 2014162277Sandre 2015253210Sandre sc->sc_peer_mss = tcp_sc_msstab[cookie.flags.mss_idx]; 2016253210Sandre 2017253210Sandre /* We can simply recompute receive window scale we sent earlier. */ 2018253210Sandre while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < sb_max) 2019253210Sandre wscale++; 2020253210Sandre 2021253210Sandre /* Only use wscale if it was enabled in the orignal SYN. */ 2022253210Sandre if (cookie.flags.wscale_idx > 0) { 2023253210Sandre sc->sc_requested_r_scale = wscale; 2024253210Sandre sc->sc_requested_s_scale = tcp_sc_wstab[cookie.flags.wscale_idx]; 2025253210Sandre sc->sc_flags |= SCF_WINSCALE; 2026253210Sandre } 2027253210Sandre 2028253210Sandre wnd = sbspace(&lso->so_rcv); 2029253210Sandre wnd = imax(wnd, 0); 2030253210Sandre wnd = imin(wnd, TCP_MAXWIN); 2031253210Sandre sc->sc_wnd = wnd; 2032253210Sandre 2033253210Sandre if (cookie.flags.sack_ok) 2034253210Sandre sc->sc_flags |= SCF_SACK; 2035253210Sandre 2036253210Sandre if (to->to_flags & TOF_TS) { 2037162277Sandre sc->sc_flags |= SCF_TIMESTAMP; 2038162277Sandre sc->sc_tsreflect = to->to_tsval; 2039169686Sandre sc->sc_ts = to->to_tsecr; 2040231767Sbz sc->sc_tsoff = to->to_tsecr - tcp_ts_getticks(); 2041253210Sandre } 2042162277Sandre 2043253210Sandre if (to->to_flags & TOF_SIGNATURE) 2044253210Sandre sc->sc_flags |= SCF_SIGNATURE; 2045162277Sandre 2046159695Sandre sc->sc_rxmits = 0; 2047162277Sandre 2048190948Srwatson TCPSTAT_INC(tcps_sc_recvcookie); 204988180Sjlemon return (sc); 205088180Sjlemon} 2051171605Ssilby 2052253210Sandre#ifdef INVARIANTS 2053253210Sandrestatic int 2054253210Sandresyncookie_cmp(struct in_conninfo *inc, struct syncache_head *sch, 2055253210Sandre struct syncache *sc, struct tcphdr *th, struct tcpopt *to, 2056253210Sandre struct socket *lso) 2057253210Sandre{ 2058253210Sandre struct syncache scs, *scx; 2059253210Sandre char *s; 2060253210Sandre 2061253210Sandre bzero(&scs, sizeof(scs)); 2062253210Sandre scx = syncookie_lookup(inc, sch, &scs, th, to, lso); 2063253210Sandre 2064253210Sandre if ((s = tcp_log_addrs(inc, th, NULL, NULL)) == NULL) 2065253210Sandre return (0); 2066253210Sandre 2067253210Sandre if (scx != NULL) { 2068253210Sandre if (sc->sc_peer_mss != scx->sc_peer_mss) 2069253210Sandre log(LOG_DEBUG, "%s; %s: mss different %i vs %i\n", 2070253210Sandre s, __func__, sc->sc_peer_mss, scx->sc_peer_mss); 2071253210Sandre 2072253210Sandre if (sc->sc_requested_r_scale != scx->sc_requested_r_scale) 2073253210Sandre log(LOG_DEBUG, "%s; %s: rwscale different %i vs %i\n", 2074253210Sandre s, __func__, sc->sc_requested_r_scale, 2075253210Sandre scx->sc_requested_r_scale); 2076253210Sandre 2077253210Sandre if (sc->sc_requested_s_scale != scx->sc_requested_s_scale) 2078253210Sandre log(LOG_DEBUG, "%s; %s: swscale different %i vs %i\n", 2079253210Sandre s, __func__, sc->sc_requested_s_scale, 2080253210Sandre scx->sc_requested_s_scale); 2081253210Sandre 2082253210Sandre if ((sc->sc_flags & SCF_SACK) != (scx->sc_flags & SCF_SACK)) 2083253210Sandre log(LOG_DEBUG, "%s; %s: SACK different\n", s, __func__); 2084253210Sandre } 2085253210Sandre 2086253210Sandre if (s != NULL) 2087253210Sandre free(s, M_TCPLOG); 2088253210Sandre return (0); 2089253210Sandre} 2090253210Sandre#endif /* INVARIANTS */ 2091253210Sandre 2092253210Sandrestatic void 2093253210Sandresyncookie_reseed(void *arg) 2094253210Sandre{ 2095253210Sandre struct tcp_syncache *sc = arg; 2096253210Sandre uint8_t *secbits; 2097253210Sandre int secbit; 2098253210Sandre 2099253210Sandre /* 2100253210Sandre * Reseeding the secret doesn't have to be protected by a lock. 2101253210Sandre * It only must be ensured that the new random values are visible 2102253210Sandre * to all CPUs in a SMP environment. The atomic with release 2103253210Sandre * semantics ensures that. 2104253210Sandre */ 2105253210Sandre secbit = (sc->secret.oddeven & 0x1) ? 0 : 1; 2106253210Sandre secbits = sc->secret.key[secbit]; 2107253210Sandre arc4rand(secbits, SYNCOOKIE_SECRET_SIZE, 0); 2108253210Sandre atomic_add_rel_int(&sc->secret.oddeven, 1); 2109253210Sandre 2110253210Sandre /* Reschedule ourself. */ 2111253210Sandre callout_schedule(&sc->secret.reseed, SYNCOOKIE_LIFETIME * hz); 2112253210Sandre} 2113253210Sandre 2114171605Ssilby/* 2115171605Ssilby * Returns the current number of syncache entries. This number 2116171605Ssilby * will probably change before you get around to calling 2117171605Ssilby * syncache_pcblist. 2118171605Ssilby */ 2119171605Ssilbyint 2120171605Ssilbysyncache_pcbcount(void) 2121171605Ssilby{ 2122171605Ssilby struct syncache_head *sch; 2123171605Ssilby int count, i; 2124171605Ssilby 2125181803Sbz for (count = 0, i = 0; i < V_tcp_syncache.hashsize; i++) { 2126171605Ssilby /* No need to lock for a read. */ 2127181803Sbz sch = &V_tcp_syncache.hashbase[i]; 2128171605Ssilby count += sch->sch_length; 2129171605Ssilby } 2130171605Ssilby return count; 2131171605Ssilby} 2132171605Ssilby 2133171605Ssilby/* 2134171605Ssilby * Exports the syncache entries to userland so that netstat can display 2135171605Ssilby * them alongside the other sockets. This function is intended to be 2136171605Ssilby * called only from tcp_pcblist. 2137171605Ssilby * 2138171605Ssilby * Due to concurrency on an active system, the number of pcbs exported 2139171605Ssilby * may have no relation to max_pcbs. max_pcbs merely indicates the 2140171605Ssilby * amount of space the caller allocated for this function to use. 2141171605Ssilby */ 2142171605Ssilbyint 2143171605Ssilbysyncache_pcblist(struct sysctl_req *req, int max_pcbs, int *pcbs_exported) 2144171605Ssilby{ 2145171605Ssilby struct xtcpcb xt; 2146171605Ssilby struct syncache *sc; 2147171605Ssilby struct syncache_head *sch; 2148171605Ssilby int count, error, i; 2149171605Ssilby 2150181803Sbz for (count = 0, error = 0, i = 0; i < V_tcp_syncache.hashsize; i++) { 2151181803Sbz sch = &V_tcp_syncache.hashbase[i]; 2152171605Ssilby SCH_LOCK(sch); 2153171605Ssilby TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 2154171605Ssilby if (count >= max_pcbs) { 2155171605Ssilby SCH_UNLOCK(sch); 2156171605Ssilby goto exit; 2157171605Ssilby } 2158182056Sbz if (cr_cansee(req->td->td_ucred, sc->sc_cred) != 0) 2159182056Sbz continue; 2160171605Ssilby bzero(&xt, sizeof(xt)); 2161171605Ssilby xt.xt_len = sizeof(xt); 2162186222Sbz if (sc->sc_inc.inc_flags & INC_ISIPV6) 2163171605Ssilby xt.xt_inp.inp_vflag = INP_IPV6; 2164171605Ssilby else 2165171605Ssilby xt.xt_inp.inp_vflag = INP_IPV4; 2166171605Ssilby bcopy(&sc->sc_inc, &xt.xt_inp.inp_inc, sizeof (struct in_conninfo)); 2167171605Ssilby xt.xt_tp.t_inpcb = &xt.xt_inp; 2168171605Ssilby xt.xt_tp.t_state = TCPS_SYN_RECEIVED; 2169171605Ssilby xt.xt_socket.xso_protocol = IPPROTO_TCP; 2170171605Ssilby xt.xt_socket.xso_len = sizeof (struct xsocket); 2171171605Ssilby xt.xt_socket.so_type = SOCK_STREAM; 2172171605Ssilby xt.xt_socket.so_state = SS_ISCONNECTING; 2173171605Ssilby error = SYSCTL_OUT(req, &xt, sizeof xt); 2174171605Ssilby if (error) { 2175171605Ssilby SCH_UNLOCK(sch); 2176171605Ssilby goto exit; 2177171605Ssilby } 2178171605Ssilby count++; 2179171605Ssilby } 2180171605Ssilby SCH_UNLOCK(sch); 2181171605Ssilby } 2182171605Ssilbyexit: 2183171605Ssilby *pcbs_exported = count; 2184171605Ssilby return error; 2185171605Ssilby} 2186