tcp_syncache.c revision 92275
186764Sjlemon/*- 292275Srwatson * Copyright (c) 2001 Networks Associates Technology, Inc. 386764Sjlemon * All rights reserved. 486764Sjlemon * 586764Sjlemon * This software was developed for the FreeBSD Project by Jonathan Lemon 686764Sjlemon * and NAI Labs, the Security Research Division of Network Associates, Inc. 786764Sjlemon * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 886764Sjlemon * DARPA CHATS research program. 986764Sjlemon * 1086764Sjlemon * Redistribution and use in source and binary forms, with or without 1186764Sjlemon * modification, are permitted provided that the following conditions 1286764Sjlemon * are met: 1386764Sjlemon * 1. Redistributions of source code must retain the above copyright 1486764Sjlemon * notice, this list of conditions and the following disclaimer. 1586764Sjlemon * 2. Redistributions in binary form must reproduce the above copyright 1686764Sjlemon * notice, this list of conditions and the following disclaimer in the 1786764Sjlemon * documentation and/or other materials provided with the distribution. 1886764Sjlemon * 3. The name of the author may not be used to endorse or promote 1986764Sjlemon * products derived from this software without specific prior written 2086764Sjlemon * permission. 2186764Sjlemon * 2286764Sjlemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2386764Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2486764Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2586764Sjlemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2686764Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2786764Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2886764Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2986764Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3086764Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3186764Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3286764Sjlemon * SUCH DAMAGE. 3386764Sjlemon * 3486764Sjlemon * $FreeBSD: head/sys/netinet/tcp_syncache.c 92275 2002-03-14 16:53:39Z rwatson $ 3586764Sjlemon */ 3686764Sjlemon 3786764Sjlemon#include "opt_inet6.h" 3886764Sjlemon#include "opt_ipsec.h" 3986764Sjlemon 4086764Sjlemon#include <sys/param.h> 4186764Sjlemon#include <sys/systm.h> 4286764Sjlemon#include <sys/kernel.h> 4386764Sjlemon#include <sys/sysctl.h> 4486764Sjlemon#include <sys/malloc.h> 4586764Sjlemon#include <sys/mbuf.h> 4686764Sjlemon#include <sys/md5.h> 4786764Sjlemon#include <sys/proc.h> /* for proc0 declaration */ 4886764Sjlemon#include <sys/random.h> 4986764Sjlemon#include <sys/socket.h> 5086764Sjlemon#include <sys/socketvar.h> 5186764Sjlemon 5286764Sjlemon#include <net/if.h> 5386764Sjlemon#include <net/route.h> 5486764Sjlemon 5586764Sjlemon#include <netinet/in.h> 5686764Sjlemon#include <netinet/in_systm.h> 5786764Sjlemon#include <netinet/ip.h> 5886764Sjlemon#include <netinet/in_var.h> 5986764Sjlemon#include <netinet/in_pcb.h> 6086764Sjlemon#include <netinet/ip_var.h> 6186764Sjlemon#ifdef INET6 6286764Sjlemon#include <netinet/ip6.h> 6386764Sjlemon#include <netinet/icmp6.h> 6486764Sjlemon#include <netinet6/nd6.h> 6586764Sjlemon#include <netinet6/ip6_var.h> 6686764Sjlemon#include <netinet6/in6_pcb.h> 6786764Sjlemon#endif 6886764Sjlemon#include <netinet/tcp.h> 6986764Sjlemon#include <netinet/tcp_fsm.h> 7086764Sjlemon#include <netinet/tcp_seq.h> 7186764Sjlemon#include <netinet/tcp_timer.h> 7286764Sjlemon#include <netinet/tcp_var.h> 7386764Sjlemon#ifdef INET6 7486764Sjlemon#include <netinet6/tcp6_var.h> 7586764Sjlemon#endif 7686764Sjlemon 7786764Sjlemon#ifdef IPSEC 7886764Sjlemon#include <netinet6/ipsec.h> 7986764Sjlemon#ifdef INET6 8086764Sjlemon#include <netinet6/ipsec6.h> 8186764Sjlemon#endif 8286764Sjlemon#include <netkey/key.h> 8386764Sjlemon#endif /*IPSEC*/ 8486764Sjlemon 8586764Sjlemon#include <machine/in_cksum.h> 8686764Sjlemon#include <vm/vm_zone.h> 8786764Sjlemon 8888180Sjlemonstatic int tcp_syncookies = 1; 8988180SjlemonSYSCTL_INT(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_RW, 9088180Sjlemon &tcp_syncookies, 0, 9188180Sjlemon "Use TCP SYN cookies if the syncache overflows"); 9288180Sjlemon 9386764Sjlemonstatic void syncache_drop(struct syncache *, struct syncache_head *); 9486764Sjlemonstatic void syncache_free(struct syncache *); 9588180Sjlemonstatic void syncache_insert(struct syncache *, struct syncache_head *); 9686764Sjlemonstruct syncache *syncache_lookup(struct in_conninfo *, struct syncache_head **); 9786764Sjlemonstatic int syncache_respond(struct syncache *, struct mbuf *); 9886764Sjlemonstatic struct socket *syncache_socket(struct syncache *, struct socket *); 9986764Sjlemonstatic void syncache_timer(void *); 10088180Sjlemonstatic u_int32_t syncookie_generate(struct syncache *); 10188180Sjlemonstatic struct syncache *syncookie_lookup(struct in_conninfo *, 10288180Sjlemon struct tcphdr *, struct socket *); 10386764Sjlemon 10486764Sjlemon/* 10586764Sjlemon * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies. 10686764Sjlemon * 3 retransmits corresponds to a timeout of (1 + 2 + 4 + 8 == 15) seconds, 10786764Sjlemon * the odds are that the user has given up attempting to connect by then. 10886764Sjlemon */ 10986764Sjlemon#define SYNCACHE_MAXREXMTS 3 11086764Sjlemon 11186764Sjlemon/* Arbitrary values */ 11286764Sjlemon#define TCP_SYNCACHE_HASHSIZE 512 11386764Sjlemon#define TCP_SYNCACHE_BUCKETLIMIT 30 11486764Sjlemon 11586764Sjlemonstruct tcp_syncache { 11686764Sjlemon struct syncache_head *hashbase; 11786764Sjlemon struct vm_zone *zone; 11886764Sjlemon u_int hashsize; 11986764Sjlemon u_int hashmask; 12086764Sjlemon u_int bucket_limit; 12186764Sjlemon u_int cache_count; 12286764Sjlemon u_int cache_limit; 12386764Sjlemon u_int rexmt_limit; 12486764Sjlemon u_int hash_secret; 12586764Sjlemon u_int next_reseed; 12686764Sjlemon TAILQ_HEAD(, syncache) timerq[SYNCACHE_MAXREXMTS + 1]; 12786764Sjlemon struct callout tt_timerq[SYNCACHE_MAXREXMTS + 1]; 12886764Sjlemon}; 12986764Sjlemonstatic struct tcp_syncache tcp_syncache; 13086764Sjlemon 13186764SjlemonSYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache"); 13286764Sjlemon 13386764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RD, 13486764Sjlemon &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache"); 13586764Sjlemon 13686764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RD, 13786764Sjlemon &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache"); 13886764Sjlemon 13986764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD, 14086764Sjlemon &tcp_syncache.cache_count, 0, "Current number of entries in syncache"); 14186764Sjlemon 14286764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RD, 14386764Sjlemon &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable"); 14486764Sjlemon 14586764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW, 14686764Sjlemon &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions"); 14786764Sjlemon 14886764Sjlemonstatic MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache"); 14986764Sjlemon 15086764Sjlemon#define SYNCACHE_HASH(inc, mask) \ 15186764Sjlemon ((tcp_syncache.hash_secret ^ \ 15286764Sjlemon (inc)->inc_faddr.s_addr ^ \ 15386764Sjlemon ((inc)->inc_faddr.s_addr >> 16) ^ \ 15486764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 15586764Sjlemon 15686764Sjlemon#define SYNCACHE_HASH6(inc, mask) \ 15786764Sjlemon ((tcp_syncache.hash_secret ^ \ 15886764Sjlemon (inc)->inc6_faddr.s6_addr32[0] ^ \ 15986764Sjlemon (inc)->inc6_faddr.s6_addr32[3] ^ \ 16086764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 16186764Sjlemon 16286764Sjlemon#define ENDPTS_EQ(a, b) ( \ 16389667Sjlemon (a)->ie_fport == (b)->ie_fport && \ 16486764Sjlemon (a)->ie_lport == (b)->ie_lport && \ 16586764Sjlemon (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \ 16686764Sjlemon (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \ 16786764Sjlemon) 16886764Sjlemon 16986764Sjlemon#define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0) 17086764Sjlemon 17186764Sjlemon#define SYNCACHE_TIMEOUT(sc, slot) do { \ 17286764Sjlemon sc->sc_rxtslot = slot; \ 17386764Sjlemon sc->sc_rxttime = ticks + TCPTV_RTOBASE * tcp_backoff[slot]; \ 17486764Sjlemon TAILQ_INSERT_TAIL(&tcp_syncache.timerq[slot], sc, sc_timerq); \ 17586764Sjlemon if (!callout_active(&tcp_syncache.tt_timerq[slot])) \ 17686764Sjlemon callout_reset(&tcp_syncache.tt_timerq[slot], \ 17786764Sjlemon TCPTV_RTOBASE * tcp_backoff[slot], \ 17888195Sjlemon syncache_timer, (void *)((intptr_t)slot)); \ 17986764Sjlemon} while (0) 18086764Sjlemon 18186764Sjlemonstatic void 18286764Sjlemonsyncache_free(struct syncache *sc) 18386764Sjlemon{ 18486764Sjlemon struct rtentry *rt; 18586764Sjlemon 18686764Sjlemon if (sc->sc_ipopts) 18786764Sjlemon (void) m_free(sc->sc_ipopts); 18886764Sjlemon#ifdef INET6 18986764Sjlemon if (sc->sc_inc.inc_isipv6) 19086764Sjlemon rt = sc->sc_route6.ro_rt; 19186764Sjlemon else 19286764Sjlemon#endif 19386764Sjlemon rt = sc->sc_route.ro_rt; 19486764Sjlemon if (rt != NULL) { 19586764Sjlemon /* 19686764Sjlemon * If this is the only reference to a protocol cloned 19786764Sjlemon * route, remove it immediately. 19886764Sjlemon */ 19986764Sjlemon if (rt->rt_flags & RTF_WASCLONED && 20086764Sjlemon (sc->sc_flags & SCF_KEEPROUTE) == 0 && 20186764Sjlemon rt->rt_refcnt == 1) 20286764Sjlemon rtrequest(RTM_DELETE, rt_key(rt), 20386764Sjlemon rt->rt_gateway, rt_mask(rt), 20486764Sjlemon rt->rt_flags, NULL); 20586764Sjlemon RTFREE(rt); 20686764Sjlemon } 20786764Sjlemon zfree(tcp_syncache.zone, sc); 20886764Sjlemon} 20986764Sjlemon 21086764Sjlemonvoid 21186764Sjlemonsyncache_init(void) 21286764Sjlemon{ 21386764Sjlemon int i; 21486764Sjlemon 21586764Sjlemon tcp_syncache.cache_count = 0; 21686764Sjlemon tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE; 21786764Sjlemon tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT; 21886764Sjlemon tcp_syncache.cache_limit = 21986764Sjlemon tcp_syncache.hashsize * tcp_syncache.bucket_limit; 22086764Sjlemon tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS; 22186764Sjlemon tcp_syncache.next_reseed = 0; 22286764Sjlemon tcp_syncache.hash_secret = arc4random(); 22386764Sjlemon 22486764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize", 22586764Sjlemon &tcp_syncache.hashsize); 22686764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit", 22786764Sjlemon &tcp_syncache.cache_limit); 22886764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit", 22986764Sjlemon &tcp_syncache.bucket_limit); 23086764Sjlemon if (!powerof2(tcp_syncache.hashsize)) { 23186764Sjlemon printf("WARNING: syncache hash size is not a power of 2.\n"); 23286764Sjlemon tcp_syncache.hashsize = 512; /* safe default */ 23386764Sjlemon } 23486764Sjlemon tcp_syncache.hashmask = tcp_syncache.hashsize - 1; 23586764Sjlemon 23686764Sjlemon /* Allocate the hash table. */ 23786764Sjlemon MALLOC(tcp_syncache.hashbase, struct syncache_head *, 23886764Sjlemon tcp_syncache.hashsize * sizeof(struct syncache_head), 23986958Stanimura M_SYNCACHE, M_WAITOK | M_ZERO); 24086764Sjlemon 24186764Sjlemon /* Initialize the hash buckets. */ 24286764Sjlemon for (i = 0; i < tcp_syncache.hashsize; i++) { 24386764Sjlemon TAILQ_INIT(&tcp_syncache.hashbase[i].sch_bucket); 24486764Sjlemon tcp_syncache.hashbase[i].sch_length = 0; 24586764Sjlemon } 24686764Sjlemon 24786764Sjlemon /* Initialize the timer queues. */ 24886814Sbde for (i = 0; i <= SYNCACHE_MAXREXMTS; i++) { 24986764Sjlemon TAILQ_INIT(&tcp_syncache.timerq[i]); 25086764Sjlemon callout_init(&tcp_syncache.tt_timerq[i], 0); 25186764Sjlemon } 25286764Sjlemon 25386764Sjlemon /* 25486764Sjlemon * Allocate the syncache entries. Allow the zone to allocate one 25586764Sjlemon * more entry than cache limit, so a new entry can bump out an 25686764Sjlemon * older one. 25786764Sjlemon */ 25886764Sjlemon tcp_syncache.cache_limit -= 1; 25986764Sjlemon tcp_syncache.zone = zinit("syncache", sizeof(struct syncache), 26086764Sjlemon tcp_syncache.cache_limit, ZONE_INTERRUPT, 0); 26186764Sjlemon} 26286764Sjlemon 26388180Sjlemonstatic void 26486764Sjlemonsyncache_insert(sc, sch) 26586764Sjlemon struct syncache *sc; 26686764Sjlemon struct syncache_head *sch; 26786764Sjlemon{ 26886764Sjlemon struct syncache *sc2; 26986764Sjlemon int s, i; 27086764Sjlemon 27186764Sjlemon /* 27286764Sjlemon * Make sure that we don't overflow the per-bucket 27386764Sjlemon * limit or the total cache size limit. 27486764Sjlemon */ 27586764Sjlemon s = splnet(); 27686764Sjlemon if (sch->sch_length >= tcp_syncache.bucket_limit) { 27786764Sjlemon /* 27886764Sjlemon * The bucket is full, toss the oldest element. 27986764Sjlemon */ 28086764Sjlemon sc2 = TAILQ_FIRST(&sch->sch_bucket); 28188180Sjlemon sc2->sc_tp->ts_recent = ticks; 28286764Sjlemon syncache_drop(sc2, sch); 28386764Sjlemon tcpstat.tcps_sc_bucketoverflow++; 28486764Sjlemon } else if (tcp_syncache.cache_count >= tcp_syncache.cache_limit) { 28586764Sjlemon /* 28686764Sjlemon * The cache is full. Toss the oldest entry in the 28786764Sjlemon * entire cache. This is the front entry in the 28886764Sjlemon * first non-empty timer queue with the largest 28986764Sjlemon * timeout value. 29086764Sjlemon */ 29186764Sjlemon for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 29286764Sjlemon sc2 = TAILQ_FIRST(&tcp_syncache.timerq[i]); 29386764Sjlemon if (sc2 != NULL) 29486764Sjlemon break; 29586764Sjlemon } 29688180Sjlemon sc2->sc_tp->ts_recent = ticks; 29786764Sjlemon syncache_drop(sc2, NULL); 29886764Sjlemon tcpstat.tcps_sc_cacheoverflow++; 29986764Sjlemon } 30086764Sjlemon 30186764Sjlemon /* Initialize the entry's timer. */ 30286764Sjlemon SYNCACHE_TIMEOUT(sc, 0); 30386764Sjlemon 30486764Sjlemon /* Put it into the bucket. */ 30586764Sjlemon TAILQ_INSERT_TAIL(&sch->sch_bucket, sc, sc_hash); 30686764Sjlemon sch->sch_length++; 30786764Sjlemon tcp_syncache.cache_count++; 30886764Sjlemon tcpstat.tcps_sc_added++; 30986764Sjlemon splx(s); 31086764Sjlemon} 31186764Sjlemon 31286764Sjlemonstatic void 31386764Sjlemonsyncache_drop(sc, sch) 31486764Sjlemon struct syncache *sc; 31586764Sjlemon struct syncache_head *sch; 31686764Sjlemon{ 31786764Sjlemon int s; 31886764Sjlemon 31986764Sjlemon if (sch == NULL) { 32086764Sjlemon#ifdef INET6 32186764Sjlemon if (sc->sc_inc.inc_isipv6) { 32286764Sjlemon sch = &tcp_syncache.hashbase[ 32386764Sjlemon SYNCACHE_HASH6(&sc->sc_inc, tcp_syncache.hashmask)]; 32486764Sjlemon } else 32586764Sjlemon#endif 32686764Sjlemon { 32786764Sjlemon sch = &tcp_syncache.hashbase[ 32886764Sjlemon SYNCACHE_HASH(&sc->sc_inc, tcp_syncache.hashmask)]; 32986764Sjlemon } 33086764Sjlemon } 33186764Sjlemon 33286764Sjlemon s = splnet(); 33386764Sjlemon 33486764Sjlemon TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash); 33586764Sjlemon sch->sch_length--; 33686764Sjlemon tcp_syncache.cache_count--; 33786764Sjlemon 33886764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], sc, sc_timerq); 33986764Sjlemon if (TAILQ_EMPTY(&tcp_syncache.timerq[sc->sc_rxtslot])) 34086764Sjlemon callout_stop(&tcp_syncache.tt_timerq[sc->sc_rxtslot]); 34186764Sjlemon splx(s); 34286764Sjlemon 34386764Sjlemon syncache_free(sc); 34486764Sjlemon} 34586764Sjlemon 34686764Sjlemon/* 34786764Sjlemon * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 34886764Sjlemon * If we have retransmitted an entry the maximum number of times, expire it. 34986764Sjlemon */ 35086764Sjlemonstatic void 35186764Sjlemonsyncache_timer(xslot) 35286764Sjlemon void *xslot; 35386764Sjlemon{ 35488195Sjlemon intptr_t slot = (intptr_t)xslot; 35586764Sjlemon struct syncache *sc, *nsc; 35686764Sjlemon struct inpcb *inp; 35786764Sjlemon int s; 35886764Sjlemon 35986764Sjlemon s = splnet(); 36086764Sjlemon if (callout_pending(&tcp_syncache.tt_timerq[slot]) || 36186764Sjlemon !callout_active(&tcp_syncache.tt_timerq[slot])) { 36286764Sjlemon splx(s); 36386764Sjlemon return; 36486764Sjlemon } 36586764Sjlemon callout_deactivate(&tcp_syncache.tt_timerq[slot]); 36686764Sjlemon 36786764Sjlemon nsc = TAILQ_FIRST(&tcp_syncache.timerq[slot]); 36886764Sjlemon while (nsc != NULL) { 36986764Sjlemon if (ticks < nsc->sc_rxttime) 37086764Sjlemon break; 37186764Sjlemon sc = nsc; 37286764Sjlemon nsc = TAILQ_NEXT(sc, sc_timerq); 37386764Sjlemon inp = sc->sc_tp->t_inpcb; 37486764Sjlemon if (slot == SYNCACHE_MAXREXMTS || 37586764Sjlemon slot >= tcp_syncache.rexmt_limit || 37686764Sjlemon inp->inp_gencnt != sc->sc_inp_gencnt) { 37786764Sjlemon syncache_drop(sc, NULL); 37886764Sjlemon tcpstat.tcps_sc_stale++; 37986764Sjlemon continue; 38086764Sjlemon } 38186764Sjlemon (void) syncache_respond(sc, NULL); 38286764Sjlemon tcpstat.tcps_sc_retransmitted++; 38386764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[slot], sc, sc_timerq); 38486764Sjlemon SYNCACHE_TIMEOUT(sc, slot + 1); 38586764Sjlemon } 38686764Sjlemon if (nsc != NULL) 38786764Sjlemon callout_reset(&tcp_syncache.tt_timerq[slot], 38886764Sjlemon nsc->sc_rxttime - ticks, syncache_timer, (void *)(slot)); 38986764Sjlemon splx(s); 39086764Sjlemon} 39186764Sjlemon 39286764Sjlemon/* 39386764Sjlemon * Find an entry in the syncache. 39486764Sjlemon */ 39586764Sjlemonstruct syncache * 39686764Sjlemonsyncache_lookup(inc, schp) 39786764Sjlemon struct in_conninfo *inc; 39886764Sjlemon struct syncache_head **schp; 39986764Sjlemon{ 40086764Sjlemon struct syncache *sc; 40186764Sjlemon struct syncache_head *sch; 40286764Sjlemon int s; 40386764Sjlemon 40486764Sjlemon#ifdef INET6 40586764Sjlemon if (inc->inc_isipv6) { 40686764Sjlemon sch = &tcp_syncache.hashbase[ 40786764Sjlemon SYNCACHE_HASH6(inc, tcp_syncache.hashmask)]; 40886764Sjlemon *schp = sch; 40986764Sjlemon s = splnet(); 41086764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 41186764Sjlemon if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 41286764Sjlemon splx(s); 41386764Sjlemon return (sc); 41486764Sjlemon } 41586764Sjlemon } 41686764Sjlemon splx(s); 41786764Sjlemon } else 41886764Sjlemon#endif 41986764Sjlemon { 42086764Sjlemon sch = &tcp_syncache.hashbase[ 42186764Sjlemon SYNCACHE_HASH(inc, tcp_syncache.hashmask)]; 42286764Sjlemon *schp = sch; 42386764Sjlemon s = splnet(); 42486764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 42586764Sjlemon#ifdef INET6 42686764Sjlemon if (sc->sc_inc.inc_isipv6) 42786764Sjlemon continue; 42886764Sjlemon#endif 42986764Sjlemon if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 43086764Sjlemon splx(s); 43186764Sjlemon return (sc); 43286764Sjlemon } 43386764Sjlemon } 43486764Sjlemon splx(s); 43586764Sjlemon } 43686764Sjlemon return (NULL); 43786764Sjlemon} 43886764Sjlemon 43986764Sjlemon/* 44086764Sjlemon * This function is called when we get a RST for a 44186764Sjlemon * non-existent connection, so that we can see if the 44286764Sjlemon * connection is in the syn cache. If it is, zap it. 44386764Sjlemon */ 44486764Sjlemonvoid 44586764Sjlemonsyncache_chkrst(inc, th) 44686764Sjlemon struct in_conninfo *inc; 44786764Sjlemon struct tcphdr *th; 44886764Sjlemon{ 44986764Sjlemon struct syncache *sc; 45086764Sjlemon struct syncache_head *sch; 45186764Sjlemon 45286764Sjlemon sc = syncache_lookup(inc, &sch); 45386764Sjlemon if (sc == NULL) 45486764Sjlemon return; 45586764Sjlemon /* 45686764Sjlemon * If the RST bit is set, check the sequence number to see 45786764Sjlemon * if this is a valid reset segment. 45886764Sjlemon * RFC 793 page 37: 45986764Sjlemon * In all states except SYN-SENT, all reset (RST) segments 46086764Sjlemon * are validated by checking their SEQ-fields. A reset is 46186764Sjlemon * valid if its sequence number is in the window. 46286764Sjlemon * 46386764Sjlemon * The sequence number in the reset segment is normally an 46486764Sjlemon * echo of our outgoing acknowlegement numbers, but some hosts 46586764Sjlemon * send a reset with the sequence number at the rightmost edge 46686764Sjlemon * of our receive window, and we have to handle this case. 46786764Sjlemon */ 46886764Sjlemon if (SEQ_GEQ(th->th_seq, sc->sc_irs) && 46986764Sjlemon SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) { 47086764Sjlemon syncache_drop(sc, sch); 47186764Sjlemon tcpstat.tcps_sc_reset++; 47286764Sjlemon } 47386764Sjlemon} 47486764Sjlemon 47586764Sjlemonvoid 47686764Sjlemonsyncache_badack(inc) 47786764Sjlemon struct in_conninfo *inc; 47886764Sjlemon{ 47986764Sjlemon struct syncache *sc; 48086764Sjlemon struct syncache_head *sch; 48186764Sjlemon 48286764Sjlemon sc = syncache_lookup(inc, &sch); 48386764Sjlemon if (sc != NULL) { 48486764Sjlemon syncache_drop(sc, sch); 48586764Sjlemon tcpstat.tcps_sc_badack++; 48686764Sjlemon } 48786764Sjlemon} 48886764Sjlemon 48986764Sjlemonvoid 49086764Sjlemonsyncache_unreach(inc, th) 49186764Sjlemon struct in_conninfo *inc; 49286764Sjlemon struct tcphdr *th; 49386764Sjlemon{ 49486764Sjlemon struct syncache *sc; 49586764Sjlemon struct syncache_head *sch; 49686764Sjlemon 49786764Sjlemon /* we are called at splnet() here */ 49886764Sjlemon sc = syncache_lookup(inc, &sch); 49986764Sjlemon if (sc == NULL) 50086764Sjlemon return; 50186764Sjlemon 50286764Sjlemon /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 50386764Sjlemon if (ntohl(th->th_seq) != sc->sc_iss) 50486764Sjlemon return; 50586764Sjlemon 50686764Sjlemon /* 50786764Sjlemon * If we've rertransmitted 3 times and this is our second error, 50886764Sjlemon * we remove the entry. Otherwise, we allow it to continue on. 50986764Sjlemon * This prevents us from incorrectly nuking an entry during a 51086764Sjlemon * spurious network outage. 51186764Sjlemon * 51286764Sjlemon * See tcp_notify(). 51386764Sjlemon */ 51486764Sjlemon if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtslot < 3) { 51586764Sjlemon sc->sc_flags |= SCF_UNREACH; 51686764Sjlemon return; 51786764Sjlemon } 51886764Sjlemon syncache_drop(sc, sch); 51986764Sjlemon tcpstat.tcps_sc_unreach++; 52086764Sjlemon} 52186764Sjlemon 52286764Sjlemon/* 52386764Sjlemon * Build a new TCP socket structure from a syncache entry. 52486764Sjlemon */ 52586764Sjlemonstatic struct socket * 52686764Sjlemonsyncache_socket(sc, lso) 52786764Sjlemon struct syncache *sc; 52886764Sjlemon struct socket *lso; 52986764Sjlemon{ 53086764Sjlemon struct inpcb *inp = NULL; 53186764Sjlemon struct socket *so; 53286764Sjlemon struct tcpcb *tp; 53386764Sjlemon 53486764Sjlemon /* 53586764Sjlemon * Ok, create the full blown connection, and set things up 53686764Sjlemon * as they would have been set up if we had created the 53786764Sjlemon * connection when the SYN arrived. If we can't create 53886764Sjlemon * the connection, abort it. 53986764Sjlemon */ 54086764Sjlemon so = sonewconn(lso, SS_ISCONNECTED); 54186764Sjlemon if (so == NULL) { 54286764Sjlemon /* 54386764Sjlemon * Drop the connection; we will send a RST if the peer 54486764Sjlemon * retransmits the ACK, 54586764Sjlemon */ 54686764Sjlemon tcpstat.tcps_listendrop++; 54786764Sjlemon goto abort; 54886764Sjlemon } 54986764Sjlemon 55086764Sjlemon inp = sotoinpcb(so); 55186764Sjlemon 55286764Sjlemon /* 55386764Sjlemon * Insert new socket into hash list. 55486764Sjlemon */ 55591492Sume inp->inp_inc.inc_isipv6 = sc->sc_inc.inc_isipv6; 55686764Sjlemon#ifdef INET6 55786764Sjlemon if (sc->sc_inc.inc_isipv6) { 55886764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 55986764Sjlemon } else { 56086764Sjlemon inp->inp_vflag &= ~INP_IPV6; 56186764Sjlemon inp->inp_vflag |= INP_IPV4; 56286764Sjlemon#endif 56386764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 56486764Sjlemon#ifdef INET6 56586764Sjlemon } 56686764Sjlemon#endif 56786764Sjlemon inp->inp_lport = sc->sc_inc.inc_lport; 56886764Sjlemon if (in_pcbinshash(inp) != 0) { 56986764Sjlemon /* 57086764Sjlemon * Undo the assignments above if we failed to 57186764Sjlemon * put the PCB on the hash lists. 57286764Sjlemon */ 57386764Sjlemon#ifdef INET6 57486764Sjlemon if (sc->sc_inc.inc_isipv6) 57586764Sjlemon inp->in6p_laddr = in6addr_any; 57686764Sjlemon else 57786764Sjlemon#endif 57886764Sjlemon inp->inp_laddr.s_addr = INADDR_ANY; 57986764Sjlemon inp->inp_lport = 0; 58086764Sjlemon goto abort; 58186764Sjlemon } 58286764Sjlemon#ifdef IPSEC 58386764Sjlemon /* copy old policy into new socket's */ 58486764Sjlemon if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp)) 58586764Sjlemon printf("syncache_expand: could not copy policy\n"); 58686764Sjlemon#endif 58786764Sjlemon#ifdef INET6 58886764Sjlemon if (sc->sc_inc.inc_isipv6) { 58986764Sjlemon struct inpcb *oinp = sotoinpcb(lso); 59086764Sjlemon struct in6_addr laddr6; 59186764Sjlemon struct sockaddr_in6 *sin6; 59286764Sjlemon /* 59386764Sjlemon * Inherit socket options from the listening socket. 59486764Sjlemon * Note that in6p_inputopts are not (and should not be) 59586764Sjlemon * copied, since it stores previously received options and is 59686764Sjlemon * used to detect if each new option is different than the 59786764Sjlemon * previous one and hence should be passed to a user. 59886764Sjlemon * If we copied in6p_inputopts, a user would not be able to 59986764Sjlemon * receive options just after calling the accept system call. 60086764Sjlemon */ 60186764Sjlemon inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS; 60286764Sjlemon if (oinp->in6p_outputopts) 60386764Sjlemon inp->in6p_outputopts = 60486764Sjlemon ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT); 60586764Sjlemon inp->in6p_route = sc->sc_route6; 60686764Sjlemon sc->sc_route6.ro_rt = NULL; 60786764Sjlemon 60886764Sjlemon MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 60986958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 61086764Sjlemon if (sin6 == NULL) 61186764Sjlemon goto abort; 61286764Sjlemon sin6->sin6_family = AF_INET6; 61386764Sjlemon sin6->sin6_len = sizeof(*sin6); 61486764Sjlemon sin6->sin6_addr = sc->sc_inc.inc6_faddr; 61586764Sjlemon sin6->sin6_port = sc->sc_inc.inc_fport; 61686764Sjlemon laddr6 = inp->in6p_laddr; 61786764Sjlemon if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 61886764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 61990361Sjulian if (in6_pcbconnect(inp, (struct sockaddr *)sin6, &thread0)) { 62086764Sjlemon inp->in6p_laddr = laddr6; 62186764Sjlemon FREE(sin6, M_SONAME); 62286764Sjlemon goto abort; 62386764Sjlemon } 62486764Sjlemon FREE(sin6, M_SONAME); 62586764Sjlemon } else 62686764Sjlemon#endif 62786764Sjlemon { 62886764Sjlemon struct in_addr laddr; 62986764Sjlemon struct sockaddr_in *sin; 63086764Sjlemon 63186764Sjlemon inp->inp_options = ip_srcroute(); 63286764Sjlemon if (inp->inp_options == NULL) { 63386764Sjlemon inp->inp_options = sc->sc_ipopts; 63486764Sjlemon sc->sc_ipopts = NULL; 63586764Sjlemon } 63686764Sjlemon inp->inp_route = sc->sc_route; 63786764Sjlemon sc->sc_route.ro_rt = NULL; 63886764Sjlemon 63986764Sjlemon MALLOC(sin, struct sockaddr_in *, sizeof *sin, 64086958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 64186764Sjlemon if (sin == NULL) 64286764Sjlemon goto abort; 64386764Sjlemon sin->sin_family = AF_INET; 64486764Sjlemon sin->sin_len = sizeof(*sin); 64586764Sjlemon sin->sin_addr = sc->sc_inc.inc_faddr; 64686764Sjlemon sin->sin_port = sc->sc_inc.inc_fport; 64786764Sjlemon bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 64886764Sjlemon laddr = inp->inp_laddr; 64986764Sjlemon if (inp->inp_laddr.s_addr == INADDR_ANY) 65086764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 65190361Sjulian if (in_pcbconnect(inp, (struct sockaddr *)sin, &thread0)) { 65286764Sjlemon inp->inp_laddr = laddr; 65386764Sjlemon FREE(sin, M_SONAME); 65486764Sjlemon goto abort; 65586764Sjlemon } 65686764Sjlemon FREE(sin, M_SONAME); 65786764Sjlemon } 65886764Sjlemon 65986764Sjlemon tp = intotcpcb(inp); 66086764Sjlemon tp->t_state = TCPS_SYN_RECEIVED; 66186764Sjlemon tp->iss = sc->sc_iss; 66286764Sjlemon tp->irs = sc->sc_irs; 66386764Sjlemon tcp_rcvseqinit(tp); 66486764Sjlemon tcp_sendseqinit(tp); 66586764Sjlemon tp->snd_wl1 = sc->sc_irs; 66686764Sjlemon tp->rcv_up = sc->sc_irs + 1; 66786764Sjlemon tp->rcv_wnd = sc->sc_wnd; 66886764Sjlemon tp->rcv_adv += tp->rcv_wnd; 66986764Sjlemon 67090982Sjlemon tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH|TF_NODELAY); 67186764Sjlemon if (sc->sc_flags & SCF_NOOPT) 67286764Sjlemon tp->t_flags |= TF_NOOPT; 67386764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 67486764Sjlemon tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 67586764Sjlemon tp->requested_s_scale = sc->sc_requested_s_scale; 67686764Sjlemon tp->request_r_scale = sc->sc_request_r_scale; 67786764Sjlemon } 67886764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 67986764Sjlemon tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 68086764Sjlemon tp->ts_recent = sc->sc_tsrecent; 68186764Sjlemon tp->ts_recent_age = ticks; 68286764Sjlemon } 68386764Sjlemon if (sc->sc_flags & SCF_CC) { 68486764Sjlemon /* 68586764Sjlemon * Initialization of the tcpcb for transaction; 68686764Sjlemon * set SND.WND = SEG.WND, 68786764Sjlemon * initialize CCsend and CCrecv. 68886764Sjlemon */ 68986764Sjlemon tp->t_flags |= TF_REQ_CC|TF_RCVD_CC; 69086764Sjlemon tp->cc_send = sc->sc_cc_send; 69186764Sjlemon tp->cc_recv = sc->sc_cc_recv; 69286764Sjlemon } 69386764Sjlemon 69486764Sjlemon tcp_mss(tp, sc->sc_peer_mss); 69586764Sjlemon 69686764Sjlemon /* 69786764Sjlemon * If the SYN,ACK was retransmitted, reset cwnd to 1 segment. 69886764Sjlemon */ 69986764Sjlemon if (sc->sc_rxtslot != 0) 70086764Sjlemon tp->snd_cwnd = tp->t_maxseg; 70186764Sjlemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 70286764Sjlemon 70386764Sjlemon tcpstat.tcps_accepts++; 70486764Sjlemon return (so); 70586764Sjlemon 70686764Sjlemonabort: 70786764Sjlemon if (so != NULL) 70886764Sjlemon (void) soabort(so); 70986764Sjlemon return (NULL); 71086764Sjlemon} 71186764Sjlemon 71286764Sjlemon/* 71386764Sjlemon * This function gets called when we receive an ACK for a 71486764Sjlemon * socket in the LISTEN state. We look up the connection 71586764Sjlemon * in the syncache, and if its there, we pull it out of 71686764Sjlemon * the cache and turn it into a full-blown connection in 71786764Sjlemon * the SYN-RECEIVED state. 71886764Sjlemon */ 71986764Sjlemonint 72086764Sjlemonsyncache_expand(inc, th, sop, m) 72186764Sjlemon struct in_conninfo *inc; 72286764Sjlemon struct tcphdr *th; 72386764Sjlemon struct socket **sop; 72486764Sjlemon struct mbuf *m; 72586764Sjlemon{ 72686764Sjlemon struct syncache *sc; 72786764Sjlemon struct syncache_head *sch; 72886764Sjlemon struct socket *so; 72986764Sjlemon 73086764Sjlemon sc = syncache_lookup(inc, &sch); 73188180Sjlemon if (sc == NULL) { 73288180Sjlemon /* 73388180Sjlemon * There is no syncache entry, so see if this ACK is 73488180Sjlemon * a returning syncookie. To do this, first: 73588180Sjlemon * A. See if this socket has had a syncache entry dropped in 73688180Sjlemon * the past. We don't want to accept a bogus syncookie 73788180Sjlemon * if we've never received a SYN. 73888180Sjlemon * B. check that the syncookie is valid. If it is, then 73988180Sjlemon * cobble up a fake syncache entry, and return. 74088180Sjlemon */ 74188180Sjlemon if (!tcp_syncookies) 74288180Sjlemon return (0); 74388180Sjlemon sc = syncookie_lookup(inc, th, *sop); 74488180Sjlemon if (sc == NULL) 74588180Sjlemon return (0); 74688180Sjlemon sch = NULL; 74788180Sjlemon tcpstat.tcps_sc_recvcookie++; 74888180Sjlemon } 74986764Sjlemon 75086764Sjlemon /* 75186764Sjlemon * If seg contains an ACK, but not for our SYN/ACK, send a RST. 75286764Sjlemon */ 75386764Sjlemon if (th->th_ack != sc->sc_iss + 1) 75486764Sjlemon return (0); 75586764Sjlemon 75686764Sjlemon so = syncache_socket(sc, *sop); 75786764Sjlemon if (so == NULL) { 75886764Sjlemon#if 0 75986764Sjlemonresetandabort: 76086764Sjlemon /* XXXjlemon check this - is this correct? */ 76186764Sjlemon (void) tcp_respond(NULL, m, m, th, 76286764Sjlemon th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK); 76386764Sjlemon#endif 76486764Sjlemon m_freem(m); /* XXX only needed for above */ 76586764Sjlemon tcpstat.tcps_sc_aborted++; 76686764Sjlemon } else { 76786764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 76886764Sjlemon tcpstat.tcps_sc_completed++; 76986764Sjlemon } 77086764Sjlemon if (sch == NULL) 77186764Sjlemon syncache_free(sc); 77286764Sjlemon else 77386764Sjlemon syncache_drop(sc, sch); 77486764Sjlemon *sop = so; 77586764Sjlemon return (1); 77686764Sjlemon} 77786764Sjlemon 77886764Sjlemon/* 77986764Sjlemon * Given a LISTEN socket and an inbound SYN request, add 78086764Sjlemon * this to the syn cache, and send back a segment: 78186764Sjlemon * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 78286764Sjlemon * to the source. 78386764Sjlemon * 78486764Sjlemon * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 78586764Sjlemon * Doing so would require that we hold onto the data and deliver it 78686764Sjlemon * to the application. However, if we are the target of a SYN-flood 78786764Sjlemon * DoS attack, an attacker could send data which would eventually 78886764Sjlemon * consume all available buffer space if it were ACKed. By not ACKing 78986764Sjlemon * the data, we avoid this DoS scenario. 79086764Sjlemon */ 79186764Sjlemonint 79286764Sjlemonsyncache_add(inc, to, th, sop, m) 79386764Sjlemon struct in_conninfo *inc; 79486764Sjlemon struct tcpopt *to; 79586764Sjlemon struct tcphdr *th; 79686764Sjlemon struct socket **sop; 79786764Sjlemon struct mbuf *m; 79886764Sjlemon{ 79986764Sjlemon struct tcpcb *tp; 80086764Sjlemon struct socket *so; 80186764Sjlemon struct syncache *sc = NULL; 80286764Sjlemon struct syncache_head *sch; 80386764Sjlemon struct mbuf *ipopts = NULL; 80486764Sjlemon struct rmxp_tao *taop; 80586764Sjlemon int i, s, win; 80686764Sjlemon 80786764Sjlemon so = *sop; 80886764Sjlemon tp = sototcpcb(so); 80986764Sjlemon 81086764Sjlemon /* 81186764Sjlemon * Remember the IP options, if any. 81286764Sjlemon */ 81386764Sjlemon#ifdef INET6 81486764Sjlemon if (!inc->inc_isipv6) 81586764Sjlemon#endif 81686764Sjlemon ipopts = ip_srcroute(); 81786764Sjlemon 81886764Sjlemon /* 81986764Sjlemon * See if we already have an entry for this connection. 82086764Sjlemon * If we do, resend the SYN,ACK, and reset the retransmit timer. 82186764Sjlemon * 82286764Sjlemon * XXX 82386764Sjlemon * should the syncache be re-initialized with the contents 82486764Sjlemon * of the new SYN here (which may have different options?) 82586764Sjlemon */ 82686764Sjlemon sc = syncache_lookup(inc, &sch); 82786764Sjlemon if (sc != NULL) { 82886764Sjlemon tcpstat.tcps_sc_dupsyn++; 82986764Sjlemon if (ipopts) { 83086764Sjlemon /* 83186764Sjlemon * If we were remembering a previous source route, 83286764Sjlemon * forget it and use the new one we've been given. 83386764Sjlemon */ 83486764Sjlemon if (sc->sc_ipopts) 83586764Sjlemon (void) m_free(sc->sc_ipopts); 83686764Sjlemon sc->sc_ipopts = ipopts; 83786764Sjlemon } 83886764Sjlemon /* 83986764Sjlemon * Update timestamp if present. 84086764Sjlemon */ 84186764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) 84286764Sjlemon sc->sc_tsrecent = to->to_tsval; 84390556Sjlemon /* 84490556Sjlemon * PCB may have changed, pick up new values. 84590556Sjlemon */ 84690556Sjlemon sc->sc_tp = tp; 84790556Sjlemon sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 84886764Sjlemon if (syncache_respond(sc, m) == 0) { 84986764Sjlemon s = splnet(); 85086764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], 85186764Sjlemon sc, sc_timerq); 85286764Sjlemon SYNCACHE_TIMEOUT(sc, sc->sc_rxtslot); 85386764Sjlemon splx(s); 85486764Sjlemon tcpstat.tcps_sndacks++; 85586764Sjlemon tcpstat.tcps_sndtotal++; 85686764Sjlemon } 85786764Sjlemon *sop = NULL; 85886764Sjlemon return (1); 85986764Sjlemon } 86086764Sjlemon 86186764Sjlemon sc = zalloc(tcp_syncache.zone); 86286764Sjlemon if (sc == NULL) { 86386764Sjlemon /* 86486764Sjlemon * The zone allocator couldn't provide more entries. 86586764Sjlemon * Treat this as if the cache was full; drop the oldest 86686764Sjlemon * entry and insert the new one. 86786764Sjlemon */ 86886764Sjlemon s = splnet(); 86986764Sjlemon for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 87086764Sjlemon sc = TAILQ_FIRST(&tcp_syncache.timerq[i]); 87186764Sjlemon if (sc != NULL) 87286764Sjlemon break; 87386764Sjlemon } 87488180Sjlemon sc->sc_tp->ts_recent = ticks; 87586764Sjlemon syncache_drop(sc, NULL); 87686764Sjlemon splx(s); 87786764Sjlemon tcpstat.tcps_sc_zonefail++; 87886764Sjlemon sc = zalloc(tcp_syncache.zone); 87986764Sjlemon if (sc == NULL) { 88086764Sjlemon if (ipopts) 88186764Sjlemon (void) m_free(ipopts); 88286764Sjlemon return (0); 88386764Sjlemon } 88486764Sjlemon } 88586764Sjlemon 88686764Sjlemon /* 88786764Sjlemon * Fill in the syncache values. 88886764Sjlemon */ 88986958Stanimura bzero(sc, sizeof(*sc)); 89086764Sjlemon sc->sc_tp = tp; 89186764Sjlemon sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 89286764Sjlemon sc->sc_ipopts = ipopts; 89386764Sjlemon sc->sc_inc.inc_fport = inc->inc_fport; 89486764Sjlemon sc->sc_inc.inc_lport = inc->inc_lport; 89586764Sjlemon#ifdef INET6 89686764Sjlemon sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 89786764Sjlemon if (inc->inc_isipv6) { 89886764Sjlemon sc->sc_inc.inc6_faddr = inc->inc6_faddr; 89986764Sjlemon sc->sc_inc.inc6_laddr = inc->inc6_laddr; 90086764Sjlemon sc->sc_route6.ro_rt = NULL; 90186764Sjlemon } else 90286764Sjlemon#endif 90386764Sjlemon { 90486764Sjlemon sc->sc_inc.inc_faddr = inc->inc_faddr; 90586764Sjlemon sc->sc_inc.inc_laddr = inc->inc_laddr; 90686764Sjlemon sc->sc_route.ro_rt = NULL; 90786764Sjlemon } 90886764Sjlemon sc->sc_irs = th->th_seq; 90988330Sjlemon if (tcp_syncookies) 91088330Sjlemon sc->sc_iss = syncookie_generate(sc); 91188330Sjlemon else 91288330Sjlemon sc->sc_iss = arc4random(); 91386764Sjlemon 91486764Sjlemon /* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */ 91586764Sjlemon win = sbspace(&so->so_rcv); 91686764Sjlemon win = imax(win, 0); 91786764Sjlemon win = imin(win, TCP_MAXWIN); 91886764Sjlemon sc->sc_wnd = win; 91986764Sjlemon 92086764Sjlemon sc->sc_flags = 0; 92186764Sjlemon sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0; 92286764Sjlemon if (tcp_do_rfc1323) { 92386764Sjlemon /* 92486764Sjlemon * A timestamp received in a SYN makes 92586764Sjlemon * it ok to send timestamp requests and replies. 92686764Sjlemon */ 92786764Sjlemon if (to->to_flags & TOF_TS) { 92886764Sjlemon sc->sc_tsrecent = to->to_tsval; 92986764Sjlemon sc->sc_flags |= SCF_TIMESTAMP; 93086764Sjlemon } 93186764Sjlemon if (to->to_flags & TOF_SCALE) { 93286764Sjlemon int wscale = 0; 93386764Sjlemon 93486764Sjlemon /* Compute proper scaling value from buffer space */ 93586764Sjlemon while (wscale < TCP_MAX_WINSHIFT && 93686764Sjlemon (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat) 93786764Sjlemon wscale++; 93886764Sjlemon sc->sc_request_r_scale = wscale; 93986764Sjlemon sc->sc_requested_s_scale = to->to_requested_s_scale; 94086764Sjlemon sc->sc_flags |= SCF_WINSCALE; 94186764Sjlemon } 94286764Sjlemon } 94386764Sjlemon if (tcp_do_rfc1644) { 94486764Sjlemon /* 94586764Sjlemon * A CC or CC.new option received in a SYN makes 94686764Sjlemon * it ok to send CC in subsequent segments. 94786764Sjlemon */ 94886764Sjlemon if (to->to_flags & (TOF_CC|TOF_CCNEW)) { 94986764Sjlemon sc->sc_cc_recv = to->to_cc; 95086764Sjlemon sc->sc_cc_send = CC_INC(tcp_ccgen); 95186764Sjlemon sc->sc_flags |= SCF_CC; 95286764Sjlemon } 95386764Sjlemon } 95486764Sjlemon if (tp->t_flags & TF_NOOPT) 95586764Sjlemon sc->sc_flags = SCF_NOOPT; 95686764Sjlemon 95786764Sjlemon /* 95886764Sjlemon * XXX 95986764Sjlemon * We have the option here of not doing TAO (even if the segment 96086764Sjlemon * qualifies) and instead fall back to a normal 3WHS via the syncache. 96186764Sjlemon * This allows us to apply synflood protection to TAO-qualifying SYNs 96286764Sjlemon * also. However, there should be a hueristic to determine when to 96386764Sjlemon * do this, and is not present at the moment. 96486764Sjlemon */ 96586764Sjlemon 96686764Sjlemon /* 96786764Sjlemon * Perform TAO test on incoming CC (SEG.CC) option, if any. 96886764Sjlemon * - compare SEG.CC against cached CC from the same host, if any. 96986764Sjlemon * - if SEG.CC > chached value, SYN must be new and is accepted 97086764Sjlemon * immediately: save new CC in the cache, mark the socket 97186764Sjlemon * connected, enter ESTABLISHED state, turn on flag to 97286764Sjlemon * send a SYN in the next segment. 97386764Sjlemon * A virtual advertised window is set in rcv_adv to 97486764Sjlemon * initialize SWS prevention. Then enter normal segment 97586764Sjlemon * processing: drop SYN, process data and FIN. 97686764Sjlemon * - otherwise do a normal 3-way handshake. 97786764Sjlemon */ 97886764Sjlemon taop = tcp_gettaocache(&sc->sc_inc); 97986764Sjlemon if ((to->to_flags & TOF_CC) != 0) { 98086764Sjlemon if (((tp->t_flags & TF_NOPUSH) != 0) && 98186764Sjlemon sc->sc_flags & SCF_CC && 98286764Sjlemon taop != NULL && taop->tao_cc != 0 && 98386764Sjlemon CC_GT(to->to_cc, taop->tao_cc)) { 98486764Sjlemon sc->sc_rxtslot = 0; 98586764Sjlemon so = syncache_socket(sc, *sop); 98686764Sjlemon if (so != NULL) { 98786764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 98886764Sjlemon taop->tao_cc = to->to_cc; 98986764Sjlemon *sop = so; 99086764Sjlemon } 99186764Sjlemon syncache_free(sc); 99286764Sjlemon return (so != NULL); 99386764Sjlemon } 99486764Sjlemon } else { 99586764Sjlemon /* 99686764Sjlemon * No CC option, but maybe CC.NEW: invalidate cached value. 99786764Sjlemon */ 99886764Sjlemon if (taop != NULL) 99986764Sjlemon taop->tao_cc = 0; 100086764Sjlemon } 100186764Sjlemon /* 100286764Sjlemon * TAO test failed or there was no CC option, 100386764Sjlemon * do a standard 3-way handshake. 100486764Sjlemon */ 100588180Sjlemon if (syncache_respond(sc, m) == 0) { 100688180Sjlemon syncache_insert(sc, sch); 100788180Sjlemon tcpstat.tcps_sndacks++; 100888180Sjlemon tcpstat.tcps_sndtotal++; 100986764Sjlemon } else { 101086764Sjlemon syncache_free(sc); 101188180Sjlemon tcpstat.tcps_sc_dropped++; 101286764Sjlemon } 101386764Sjlemon *sop = NULL; 101486764Sjlemon return (1); 101586764Sjlemon} 101686764Sjlemon 101786764Sjlemonstatic int 101886764Sjlemonsyncache_respond(sc, m) 101986764Sjlemon struct syncache *sc; 102086764Sjlemon struct mbuf *m; 102186764Sjlemon{ 102286764Sjlemon u_int8_t *optp; 102386764Sjlemon int optlen, error; 102486764Sjlemon u_int16_t tlen, hlen, mssopt; 102586764Sjlemon struct ip *ip = NULL; 102686764Sjlemon struct rtentry *rt; 102786764Sjlemon struct tcphdr *th; 102886764Sjlemon#ifdef INET6 102986764Sjlemon struct ip6_hdr *ip6 = NULL; 103086764Sjlemon#endif 103186764Sjlemon 103286764Sjlemon#ifdef INET6 103386764Sjlemon if (sc->sc_inc.inc_isipv6) { 103486764Sjlemon rt = tcp_rtlookup6(&sc->sc_inc); 103586764Sjlemon if (rt != NULL) 103686764Sjlemon mssopt = rt->rt_ifp->if_mtu - 103786764Sjlemon (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 103886764Sjlemon else 103986764Sjlemon mssopt = tcp_v6mssdflt; 104086764Sjlemon hlen = sizeof(struct ip6_hdr); 104186764Sjlemon } else 104286764Sjlemon#endif 104386764Sjlemon { 104486764Sjlemon rt = tcp_rtlookup(&sc->sc_inc); 104586764Sjlemon if (rt != NULL) 104686764Sjlemon mssopt = rt->rt_ifp->if_mtu - 104786764Sjlemon (sizeof(struct ip) + sizeof(struct tcphdr)); 104886764Sjlemon else 104986764Sjlemon mssopt = tcp_mssdflt; 105086764Sjlemon hlen = sizeof(struct ip); 105186764Sjlemon } 105286764Sjlemon 105386764Sjlemon /* Compute the size of the TCP options. */ 105486764Sjlemon if (sc->sc_flags & SCF_NOOPT) { 105586764Sjlemon optlen = 0; 105686764Sjlemon } else { 105786764Sjlemon optlen = TCPOLEN_MAXSEG + 105886764Sjlemon ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) + 105986764Sjlemon ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) + 106086764Sjlemon ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0); 106186764Sjlemon } 106286764Sjlemon tlen = hlen + sizeof(struct tcphdr) + optlen; 106386764Sjlemon 106486764Sjlemon /* 106586764Sjlemon * XXX 106686764Sjlemon * assume that the entire packet will fit in a header mbuf 106786764Sjlemon */ 106886764Sjlemon KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small")); 106986764Sjlemon 107086764Sjlemon /* 107186764Sjlemon * XXX shouldn't this reuse the mbuf if possible ? 107286764Sjlemon * Create the IP+TCP header from scratch. 107386764Sjlemon */ 107486764Sjlemon if (m) 107586764Sjlemon m_freem(m); 107686764Sjlemon 107786764Sjlemon m = m_gethdr(M_DONTWAIT, MT_HEADER); 107886764Sjlemon if (m == NULL) 107986764Sjlemon return (ENOBUFS); 108086764Sjlemon m->m_data += max_linkhdr; 108186764Sjlemon m->m_len = tlen; 108286764Sjlemon m->m_pkthdr.len = tlen; 108386764Sjlemon m->m_pkthdr.rcvif = NULL; 108486764Sjlemon 108586764Sjlemon#ifdef IPSEC 108686764Sjlemon /* use IPsec policy on listening socket to send SYN,ACK */ 108786764Sjlemon if (ipsec_setsocket(m, sc->sc_tp->t_inpcb->inp_socket) != 0) { 108886764Sjlemon m_freem(m); 108986764Sjlemon return (ENOBUFS); 109086764Sjlemon } 109186764Sjlemon#endif 109286764Sjlemon 109386764Sjlemon#ifdef INET6 109486764Sjlemon if (sc->sc_inc.inc_isipv6) { 109586764Sjlemon ip6 = mtod(m, struct ip6_hdr *); 109686764Sjlemon ip6->ip6_vfc = IPV6_VERSION; 109786764Sjlemon ip6->ip6_nxt = IPPROTO_TCP; 109886764Sjlemon ip6->ip6_src = sc->sc_inc.inc6_laddr; 109986764Sjlemon ip6->ip6_dst = sc->sc_inc.inc6_faddr; 110086764Sjlemon ip6->ip6_plen = htons(tlen - hlen); 110186764Sjlemon /* ip6_hlim is set after checksum */ 110286764Sjlemon /* ip6_flow = ??? */ 110386764Sjlemon 110486764Sjlemon th = (struct tcphdr *)(ip6 + 1); 110586764Sjlemon } else 110686764Sjlemon#endif 110786764Sjlemon { 110886764Sjlemon ip = mtod(m, struct ip *); 110986764Sjlemon ip->ip_v = IPVERSION; 111086764Sjlemon ip->ip_hl = sizeof(struct ip) >> 2; 111186764Sjlemon ip->ip_tos = 0; 111286764Sjlemon ip->ip_len = tlen; 111386764Sjlemon ip->ip_id = 0; 111486764Sjlemon ip->ip_off = 0; 111586764Sjlemon ip->ip_ttl = ip_defttl; 111686764Sjlemon ip->ip_sum = 0; 111786764Sjlemon ip->ip_p = IPPROTO_TCP; 111886764Sjlemon ip->ip_src = sc->sc_inc.inc_laddr; 111986764Sjlemon ip->ip_dst = sc->sc_inc.inc_faddr; 112086764Sjlemon 112186764Sjlemon th = (struct tcphdr *)(ip + 1); 112286764Sjlemon } 112386764Sjlemon th->th_sport = sc->sc_inc.inc_lport; 112486764Sjlemon th->th_dport = sc->sc_inc.inc_fport; 112586764Sjlemon 112686764Sjlemon th->th_seq = htonl(sc->sc_iss); 112786764Sjlemon th->th_ack = htonl(sc->sc_irs + 1); 112886764Sjlemon th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 112986764Sjlemon th->th_x2 = 0; 113086764Sjlemon th->th_flags = TH_SYN|TH_ACK; 113186764Sjlemon th->th_win = htons(sc->sc_wnd); 113286764Sjlemon th->th_urp = 0; 113386764Sjlemon 113486764Sjlemon /* Tack on the TCP options. */ 113586764Sjlemon if (optlen == 0) 113686764Sjlemon goto no_options; 113786764Sjlemon optp = (u_int8_t *)(th + 1); 113886764Sjlemon *optp++ = TCPOPT_MAXSEG; 113986764Sjlemon *optp++ = TCPOLEN_MAXSEG; 114086764Sjlemon *optp++ = (mssopt >> 8) & 0xff; 114186764Sjlemon *optp++ = mssopt & 0xff; 114286764Sjlemon 114386764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 114486764Sjlemon *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 114586764Sjlemon TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 114686764Sjlemon sc->sc_request_r_scale); 114786764Sjlemon optp += 4; 114886764Sjlemon } 114986764Sjlemon 115086764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 115186764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 115286764Sjlemon 115386764Sjlemon /* Form timestamp option as shown in appendix A of RFC 1323. */ 115486764Sjlemon *lp++ = htonl(TCPOPT_TSTAMP_HDR); 115586764Sjlemon *lp++ = htonl(ticks); 115686764Sjlemon *lp = htonl(sc->sc_tsrecent); 115786764Sjlemon optp += TCPOLEN_TSTAMP_APPA; 115886764Sjlemon } 115986764Sjlemon 116086764Sjlemon /* 116186764Sjlemon * Send CC and CC.echo if we received CC from our peer. 116286764Sjlemon */ 116386764Sjlemon if (sc->sc_flags & SCF_CC) { 116486764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 116586764Sjlemon 116686764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC)); 116786764Sjlemon *lp++ = htonl(sc->sc_cc_send); 116886764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO)); 116986764Sjlemon *lp = htonl(sc->sc_cc_recv); 117086764Sjlemon optp += TCPOLEN_CC_APPA * 2; 117186764Sjlemon } 117286764Sjlemonno_options: 117386764Sjlemon 117486764Sjlemon#ifdef INET6 117586764Sjlemon if (sc->sc_inc.inc_isipv6) { 117686764Sjlemon struct route_in6 *ro6 = &sc->sc_route6; 117786764Sjlemon 117886764Sjlemon th->th_sum = 0; 117986764Sjlemon th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 118086764Sjlemon ip6->ip6_hlim = in6_selecthlim(NULL, 118186764Sjlemon ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL); 118286764Sjlemon error = ip6_output(m, NULL, ro6, 0, NULL, NULL); 118386764Sjlemon } else 118486764Sjlemon#endif 118586764Sjlemon { 118686764Sjlemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 118786764Sjlemon htons(tlen - hlen + IPPROTO_TCP)); 118886764Sjlemon m->m_pkthdr.csum_flags = CSUM_TCP; 118986764Sjlemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 119086764Sjlemon error = ip_output(m, sc->sc_ipopts, &sc->sc_route, 0, NULL); 119186764Sjlemon } 119286764Sjlemon return (error); 119386764Sjlemon} 119488180Sjlemon 119588180Sjlemon/* 119688180Sjlemon * cookie layers: 119788180Sjlemon * 119888180Sjlemon * |. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .| 119988180Sjlemon * | peer iss | 120088180Sjlemon * | MD5(laddr,faddr,lport,fport,secret) |. . . . . . .| 120188180Sjlemon * | 0 |(A)| | 120288180Sjlemon * (A): peer mss index 120388180Sjlemon */ 120488180Sjlemon 120588180Sjlemon/* 120688180Sjlemon * The values below are chosen to minimize the size of the tcp_secret 120788180Sjlemon * table, as well as providing roughly a 4 second lifetime for the cookie. 120888180Sjlemon */ 120988180Sjlemon 121088180Sjlemon#define SYNCOOKIE_HASHSHIFT 2 /* log2(# of 32bit words from hash) */ 121188180Sjlemon#define SYNCOOKIE_WNDBITS 7 /* exposed bits for window indexing */ 121288180Sjlemon#define SYNCOOKIE_TIMESHIFT 5 /* scale ticks to window time units */ 121388180Sjlemon 121488180Sjlemon#define SYNCOOKIE_HASHMASK ((1 << SYNCOOKIE_HASHSHIFT) - 1) 121588180Sjlemon#define SYNCOOKIE_WNDMASK ((1 << SYNCOOKIE_WNDBITS) - 1) 121688180Sjlemon#define SYNCOOKIE_NSECRETS (1 << (SYNCOOKIE_WNDBITS - SYNCOOKIE_HASHSHIFT)) 121788180Sjlemon#define SYNCOOKIE_TIMEOUT \ 121888180Sjlemon (hz * (1 << SYNCOOKIE_WNDBITS) / (1 << SYNCOOKIE_TIMESHIFT)) 121988180Sjlemon#define SYNCOOKIE_DATAMASK ((3 << SYNCOOKIE_WNDBITS) | SYNCOOKIE_WNDMASK) 122088180Sjlemon 122188180Sjlemonstatic struct { 122288180Sjlemon u_int32_t ts_secbits; 122388180Sjlemon u_int ts_expire; 122488180Sjlemon} tcp_secret[SYNCOOKIE_NSECRETS]; 122588180Sjlemon 122688180Sjlemonstatic int tcp_msstab[] = { 0, 536, 1460, 8960 }; 122788180Sjlemon 122888180Sjlemonstatic MD5_CTX syn_ctx; 122988180Sjlemon 123088180Sjlemon#define MD5Add(v) MD5Update(&syn_ctx, (u_char *)&v, sizeof(v)) 123188180Sjlemon 123288180Sjlemon/* 123388180Sjlemon * Consider the problem of a recreated (and retransmitted) cookie. If the 123488180Sjlemon * original SYN was accepted, the connection is established. The second 123588180Sjlemon * SYN is inflight, and if it arrives with an ISN that falls within the 123688180Sjlemon * receive window, the connection is killed. 123788180Sjlemon * 123888180Sjlemon * However, since cookies have other problems, this may not be worth 123988180Sjlemon * worrying about. 124088180Sjlemon */ 124188180Sjlemon 124288180Sjlemonstatic u_int32_t 124388180Sjlemonsyncookie_generate(struct syncache *sc) 124488180Sjlemon{ 124588180Sjlemon u_int32_t md5_buffer[4]; 124688180Sjlemon u_int32_t data; 124788180Sjlemon int wnd, idx; 124888180Sjlemon 124988180Sjlemon wnd = ((ticks << SYNCOOKIE_TIMESHIFT) / hz) & SYNCOOKIE_WNDMASK; 125088180Sjlemon idx = wnd >> SYNCOOKIE_HASHSHIFT; 125188180Sjlemon if (tcp_secret[idx].ts_expire < ticks) { 125288180Sjlemon tcp_secret[idx].ts_secbits = arc4random(); 125388180Sjlemon tcp_secret[idx].ts_expire = ticks + SYNCOOKIE_TIMEOUT; 125488180Sjlemon } 125588180Sjlemon for (data = sizeof(tcp_msstab) / sizeof(int) - 1; data > 0; data--) 125688180Sjlemon if (tcp_msstab[data] <= sc->sc_peer_mss) 125788180Sjlemon break; 125888180Sjlemon data = (data << SYNCOOKIE_WNDBITS) | wnd; 125988180Sjlemon data ^= sc->sc_irs; /* peer's iss */ 126088180Sjlemon MD5Init(&syn_ctx); 126188180Sjlemon#ifdef INET6 126288180Sjlemon if (sc->sc_inc.inc_isipv6) { 126388180Sjlemon MD5Add(sc->sc_inc.inc6_laddr); 126488180Sjlemon MD5Add(sc->sc_inc.inc6_faddr); 126588180Sjlemon } else 126688180Sjlemon#endif 126788180Sjlemon { 126888180Sjlemon MD5Add(sc->sc_inc.inc_laddr); 126988180Sjlemon MD5Add(sc->sc_inc.inc_faddr); 127088180Sjlemon } 127188180Sjlemon MD5Add(sc->sc_inc.inc_lport); 127288180Sjlemon MD5Add(sc->sc_inc.inc_fport); 127388180Sjlemon MD5Add(tcp_secret[idx].ts_secbits); 127488180Sjlemon MD5Final((u_char *)&md5_buffer, &syn_ctx); 127588180Sjlemon data ^= (md5_buffer[wnd & SYNCOOKIE_HASHMASK] & ~SYNCOOKIE_WNDMASK); 127688180Sjlemon return (data); 127788180Sjlemon} 127888180Sjlemon 127988180Sjlemonstatic struct syncache * 128088180Sjlemonsyncookie_lookup(inc, th, so) 128188180Sjlemon struct in_conninfo *inc; 128288180Sjlemon struct tcphdr *th; 128388180Sjlemon struct socket *so; 128488180Sjlemon{ 128588180Sjlemon u_int32_t md5_buffer[4]; 128688180Sjlemon struct syncache *sc; 128788180Sjlemon u_int32_t data; 128888180Sjlemon int wnd, idx; 128988180Sjlemon 129088180Sjlemon data = (th->th_ack - 1) ^ (th->th_seq - 1); /* remove ISS */ 129188180Sjlemon wnd = data & SYNCOOKIE_WNDMASK; 129288180Sjlemon idx = wnd >> SYNCOOKIE_HASHSHIFT; 129388180Sjlemon if (tcp_secret[idx].ts_expire < ticks || 129488180Sjlemon sototcpcb(so)->ts_recent + SYNCOOKIE_TIMEOUT < ticks) 129588180Sjlemon return (NULL); 129688180Sjlemon MD5Init(&syn_ctx); 129788180Sjlemon#ifdef INET6 129888180Sjlemon if (inc->inc_isipv6) { 129988180Sjlemon MD5Add(inc->inc6_laddr); 130088180Sjlemon MD5Add(inc->inc6_faddr); 130188180Sjlemon } else 130288180Sjlemon#endif 130388180Sjlemon { 130488180Sjlemon MD5Add(inc->inc_laddr); 130588180Sjlemon MD5Add(inc->inc_faddr); 130688180Sjlemon } 130788180Sjlemon MD5Add(inc->inc_lport); 130888180Sjlemon MD5Add(inc->inc_fport); 130988180Sjlemon MD5Add(tcp_secret[idx].ts_secbits); 131088180Sjlemon MD5Final((u_char *)&md5_buffer, &syn_ctx); 131188180Sjlemon data ^= md5_buffer[wnd & SYNCOOKIE_HASHMASK]; 131288180Sjlemon if ((data & ~SYNCOOKIE_DATAMASK) != 0) 131388180Sjlemon return (NULL); 131488180Sjlemon data = data >> SYNCOOKIE_WNDBITS; 131588180Sjlemon 131688180Sjlemon sc = zalloc(tcp_syncache.zone); 131788180Sjlemon if (sc == NULL) 131888180Sjlemon return (NULL); 131988180Sjlemon /* 132088180Sjlemon * Fill in the syncache values. 132188180Sjlemon * XXX duplicate code from syncache_add 132288180Sjlemon */ 132388180Sjlemon sc->sc_ipopts = NULL; 132488180Sjlemon sc->sc_inc.inc_fport = inc->inc_fport; 132588180Sjlemon sc->sc_inc.inc_lport = inc->inc_lport; 132688180Sjlemon#ifdef INET6 132788180Sjlemon sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 132888180Sjlemon if (inc->inc_isipv6) { 132988180Sjlemon sc->sc_inc.inc6_faddr = inc->inc6_faddr; 133088180Sjlemon sc->sc_inc.inc6_laddr = inc->inc6_laddr; 133188180Sjlemon sc->sc_route6.ro_rt = NULL; 133288180Sjlemon } else 133388180Sjlemon#endif 133488180Sjlemon { 133588180Sjlemon sc->sc_inc.inc_faddr = inc->inc_faddr; 133688180Sjlemon sc->sc_inc.inc_laddr = inc->inc_laddr; 133788180Sjlemon sc->sc_route.ro_rt = NULL; 133888180Sjlemon } 133988180Sjlemon sc->sc_irs = th->th_seq - 1; 134088180Sjlemon sc->sc_iss = th->th_ack - 1; 134188180Sjlemon wnd = sbspace(&so->so_rcv); 134288180Sjlemon wnd = imax(wnd, 0); 134388180Sjlemon wnd = imin(wnd, TCP_MAXWIN); 134488180Sjlemon sc->sc_wnd = wnd; 134588180Sjlemon sc->sc_flags = 0; 134688180Sjlemon sc->sc_rxtslot = 0; 134788180Sjlemon sc->sc_peer_mss = tcp_msstab[data]; 134888180Sjlemon return (sc); 134988180Sjlemon} 1350