tcp_syncache.c revision 90361
186764Sjlemon/*- 286764Sjlemon * Copyright (c) 2001 Networks Associates Technologies, 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 90361 2002-02-07 20:58:47Z julian $ 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 */ 55586764Sjlemon#ifdef INET6 55686764Sjlemon if (sc->sc_inc.inc_isipv6) { 55786764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 55886764Sjlemon } else { 55986764Sjlemon inp->inp_vflag &= ~INP_IPV6; 56086764Sjlemon inp->inp_vflag |= INP_IPV4; 56186764Sjlemon#endif 56286764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 56386764Sjlemon#ifdef INET6 56486764Sjlemon } 56586764Sjlemon#endif 56686764Sjlemon inp->inp_lport = sc->sc_inc.inc_lport; 56786764Sjlemon if (in_pcbinshash(inp) != 0) { 56886764Sjlemon /* 56986764Sjlemon * Undo the assignments above if we failed to 57086764Sjlemon * put the PCB on the hash lists. 57186764Sjlemon */ 57286764Sjlemon#ifdef INET6 57386764Sjlemon if (sc->sc_inc.inc_isipv6) 57486764Sjlemon inp->in6p_laddr = in6addr_any; 57586764Sjlemon else 57686764Sjlemon#endif 57786764Sjlemon inp->inp_laddr.s_addr = INADDR_ANY; 57886764Sjlemon inp->inp_lport = 0; 57986764Sjlemon goto abort; 58086764Sjlemon } 58186764Sjlemon#ifdef IPSEC 58286764Sjlemon /* copy old policy into new socket's */ 58386764Sjlemon if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp)) 58486764Sjlemon printf("syncache_expand: could not copy policy\n"); 58586764Sjlemon#endif 58686764Sjlemon#ifdef INET6 58786764Sjlemon if (sc->sc_inc.inc_isipv6) { 58886764Sjlemon struct inpcb *oinp = sotoinpcb(lso); 58986764Sjlemon struct in6_addr laddr6; 59086764Sjlemon struct sockaddr_in6 *sin6; 59186764Sjlemon /* 59286764Sjlemon * Inherit socket options from the listening socket. 59386764Sjlemon * Note that in6p_inputopts are not (and should not be) 59486764Sjlemon * copied, since it stores previously received options and is 59586764Sjlemon * used to detect if each new option is different than the 59686764Sjlemon * previous one and hence should be passed to a user. 59786764Sjlemon * If we copied in6p_inputopts, a user would not be able to 59886764Sjlemon * receive options just after calling the accept system call. 59986764Sjlemon */ 60086764Sjlemon inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS; 60186764Sjlemon if (oinp->in6p_outputopts) 60286764Sjlemon inp->in6p_outputopts = 60386764Sjlemon ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT); 60486764Sjlemon inp->in6p_route = sc->sc_route6; 60586764Sjlemon sc->sc_route6.ro_rt = NULL; 60686764Sjlemon 60786764Sjlemon MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 60886958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 60986764Sjlemon if (sin6 == NULL) 61086764Sjlemon goto abort; 61186764Sjlemon sin6->sin6_family = AF_INET6; 61286764Sjlemon sin6->sin6_len = sizeof(*sin6); 61386764Sjlemon sin6->sin6_addr = sc->sc_inc.inc6_faddr; 61486764Sjlemon sin6->sin6_port = sc->sc_inc.inc_fport; 61586764Sjlemon laddr6 = inp->in6p_laddr; 61686764Sjlemon if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 61786764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 61890361Sjulian if (in6_pcbconnect(inp, (struct sockaddr *)sin6, &thread0)) { 61986764Sjlemon inp->in6p_laddr = laddr6; 62086764Sjlemon FREE(sin6, M_SONAME); 62186764Sjlemon goto abort; 62286764Sjlemon } 62386764Sjlemon FREE(sin6, M_SONAME); 62486764Sjlemon } else 62586764Sjlemon#endif 62686764Sjlemon { 62786764Sjlemon struct in_addr laddr; 62886764Sjlemon struct sockaddr_in *sin; 62986764Sjlemon 63086764Sjlemon inp->inp_options = ip_srcroute(); 63186764Sjlemon if (inp->inp_options == NULL) { 63286764Sjlemon inp->inp_options = sc->sc_ipopts; 63386764Sjlemon sc->sc_ipopts = NULL; 63486764Sjlemon } 63586764Sjlemon inp->inp_route = sc->sc_route; 63686764Sjlemon sc->sc_route.ro_rt = NULL; 63786764Sjlemon 63886764Sjlemon MALLOC(sin, struct sockaddr_in *, sizeof *sin, 63986958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 64086764Sjlemon if (sin == NULL) 64186764Sjlemon goto abort; 64286764Sjlemon sin->sin_family = AF_INET; 64386764Sjlemon sin->sin_len = sizeof(*sin); 64486764Sjlemon sin->sin_addr = sc->sc_inc.inc_faddr; 64586764Sjlemon sin->sin_port = sc->sc_inc.inc_fport; 64686764Sjlemon bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 64786764Sjlemon laddr = inp->inp_laddr; 64886764Sjlemon if (inp->inp_laddr.s_addr == INADDR_ANY) 64986764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 65090361Sjulian if (in_pcbconnect(inp, (struct sockaddr *)sin, &thread0)) { 65186764Sjlemon inp->inp_laddr = laddr; 65286764Sjlemon FREE(sin, M_SONAME); 65386764Sjlemon goto abort; 65486764Sjlemon } 65586764Sjlemon FREE(sin, M_SONAME); 65686764Sjlemon } 65786764Sjlemon 65886764Sjlemon tp = intotcpcb(inp); 65986764Sjlemon tp->t_state = TCPS_SYN_RECEIVED; 66086764Sjlemon tp->iss = sc->sc_iss; 66186764Sjlemon tp->irs = sc->sc_irs; 66286764Sjlemon tcp_rcvseqinit(tp); 66386764Sjlemon tcp_sendseqinit(tp); 66486764Sjlemon tp->snd_wl1 = sc->sc_irs; 66586764Sjlemon tp->rcv_up = sc->sc_irs + 1; 66686764Sjlemon tp->rcv_wnd = sc->sc_wnd; 66786764Sjlemon tp->rcv_adv += tp->rcv_wnd; 66886764Sjlemon 66987193Sdillon tp->t_flags = sc->sc_tp->t_flags & (TF_NOPUSH|TF_NODELAY); 67086764Sjlemon if (sc->sc_flags & SCF_NOOPT) 67186764Sjlemon tp->t_flags |= TF_NOOPT; 67286764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 67386764Sjlemon tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 67486764Sjlemon tp->requested_s_scale = sc->sc_requested_s_scale; 67586764Sjlemon tp->request_r_scale = sc->sc_request_r_scale; 67686764Sjlemon } 67786764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 67886764Sjlemon tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 67986764Sjlemon tp->ts_recent = sc->sc_tsrecent; 68086764Sjlemon tp->ts_recent_age = ticks; 68186764Sjlemon } 68286764Sjlemon if (sc->sc_flags & SCF_CC) { 68386764Sjlemon /* 68486764Sjlemon * Initialization of the tcpcb for transaction; 68586764Sjlemon * set SND.WND = SEG.WND, 68686764Sjlemon * initialize CCsend and CCrecv. 68786764Sjlemon */ 68886764Sjlemon tp->t_flags |= TF_REQ_CC|TF_RCVD_CC; 68986764Sjlemon tp->cc_send = sc->sc_cc_send; 69086764Sjlemon tp->cc_recv = sc->sc_cc_recv; 69186764Sjlemon } 69286764Sjlemon 69386764Sjlemon tcp_mss(tp, sc->sc_peer_mss); 69486764Sjlemon 69586764Sjlemon /* 69686764Sjlemon * If the SYN,ACK was retransmitted, reset cwnd to 1 segment. 69786764Sjlemon */ 69886764Sjlemon if (sc->sc_rxtslot != 0) 69986764Sjlemon tp->snd_cwnd = tp->t_maxseg; 70086764Sjlemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 70186764Sjlemon 70286764Sjlemon tcpstat.tcps_accepts++; 70386764Sjlemon return (so); 70486764Sjlemon 70586764Sjlemonabort: 70686764Sjlemon if (so != NULL) 70786764Sjlemon (void) soabort(so); 70886764Sjlemon return (NULL); 70986764Sjlemon} 71086764Sjlemon 71186764Sjlemon/* 71286764Sjlemon * This function gets called when we receive an ACK for a 71386764Sjlemon * socket in the LISTEN state. We look up the connection 71486764Sjlemon * in the syncache, and if its there, we pull it out of 71586764Sjlemon * the cache and turn it into a full-blown connection in 71686764Sjlemon * the SYN-RECEIVED state. 71786764Sjlemon */ 71886764Sjlemonint 71986764Sjlemonsyncache_expand(inc, th, sop, m) 72086764Sjlemon struct in_conninfo *inc; 72186764Sjlemon struct tcphdr *th; 72286764Sjlemon struct socket **sop; 72386764Sjlemon struct mbuf *m; 72486764Sjlemon{ 72586764Sjlemon struct syncache *sc; 72686764Sjlemon struct syncache_head *sch; 72786764Sjlemon struct socket *so; 72886764Sjlemon 72986764Sjlemon sc = syncache_lookup(inc, &sch); 73088180Sjlemon if (sc == NULL) { 73188180Sjlemon /* 73288180Sjlemon * There is no syncache entry, so see if this ACK is 73388180Sjlemon * a returning syncookie. To do this, first: 73488180Sjlemon * A. See if this socket has had a syncache entry dropped in 73588180Sjlemon * the past. We don't want to accept a bogus syncookie 73688180Sjlemon * if we've never received a SYN. 73788180Sjlemon * B. check that the syncookie is valid. If it is, then 73888180Sjlemon * cobble up a fake syncache entry, and return. 73988180Sjlemon */ 74088180Sjlemon if (!tcp_syncookies) 74188180Sjlemon return (0); 74288180Sjlemon sc = syncookie_lookup(inc, th, *sop); 74388180Sjlemon if (sc == NULL) 74488180Sjlemon return (0); 74588180Sjlemon sch = NULL; 74688180Sjlemon tcpstat.tcps_sc_recvcookie++; 74788180Sjlemon } 74886764Sjlemon 74986764Sjlemon /* 75086764Sjlemon * If seg contains an ACK, but not for our SYN/ACK, send a RST. 75186764Sjlemon */ 75286764Sjlemon if (th->th_ack != sc->sc_iss + 1) 75386764Sjlemon return (0); 75486764Sjlemon 75586764Sjlemon so = syncache_socket(sc, *sop); 75686764Sjlemon if (so == NULL) { 75786764Sjlemon#if 0 75886764Sjlemonresetandabort: 75986764Sjlemon /* XXXjlemon check this - is this correct? */ 76086764Sjlemon (void) tcp_respond(NULL, m, m, th, 76186764Sjlemon th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK); 76286764Sjlemon#endif 76386764Sjlemon m_freem(m); /* XXX only needed for above */ 76486764Sjlemon tcpstat.tcps_sc_aborted++; 76586764Sjlemon } else { 76686764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 76786764Sjlemon tcpstat.tcps_sc_completed++; 76886764Sjlemon } 76986764Sjlemon if (sch == NULL) 77086764Sjlemon syncache_free(sc); 77186764Sjlemon else 77286764Sjlemon syncache_drop(sc, sch); 77386764Sjlemon *sop = so; 77486764Sjlemon return (1); 77586764Sjlemon} 77686764Sjlemon 77786764Sjlemon/* 77886764Sjlemon * Given a LISTEN socket and an inbound SYN request, add 77986764Sjlemon * this to the syn cache, and send back a segment: 78086764Sjlemon * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 78186764Sjlemon * to the source. 78286764Sjlemon * 78386764Sjlemon * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 78486764Sjlemon * Doing so would require that we hold onto the data and deliver it 78586764Sjlemon * to the application. However, if we are the target of a SYN-flood 78686764Sjlemon * DoS attack, an attacker could send data which would eventually 78786764Sjlemon * consume all available buffer space if it were ACKed. By not ACKing 78886764Sjlemon * the data, we avoid this DoS scenario. 78986764Sjlemon */ 79086764Sjlemonint 79186764Sjlemonsyncache_add(inc, to, th, sop, m) 79286764Sjlemon struct in_conninfo *inc; 79386764Sjlemon struct tcpopt *to; 79486764Sjlemon struct tcphdr *th; 79586764Sjlemon struct socket **sop; 79686764Sjlemon struct mbuf *m; 79786764Sjlemon{ 79886764Sjlemon struct tcpcb *tp; 79986764Sjlemon struct socket *so; 80086764Sjlemon struct syncache *sc = NULL; 80186764Sjlemon struct syncache_head *sch; 80286764Sjlemon struct mbuf *ipopts = NULL; 80386764Sjlemon struct rmxp_tao *taop; 80486764Sjlemon int i, s, win; 80586764Sjlemon 80686764Sjlemon so = *sop; 80786764Sjlemon tp = sototcpcb(so); 80886764Sjlemon 80986764Sjlemon /* 81086764Sjlemon * Remember the IP options, if any. 81186764Sjlemon */ 81286764Sjlemon#ifdef INET6 81386764Sjlemon if (!inc->inc_isipv6) 81486764Sjlemon#endif 81586764Sjlemon ipopts = ip_srcroute(); 81686764Sjlemon 81786764Sjlemon /* 81886764Sjlemon * See if we already have an entry for this connection. 81986764Sjlemon * If we do, resend the SYN,ACK, and reset the retransmit timer. 82086764Sjlemon * 82186764Sjlemon * XXX 82286764Sjlemon * should the syncache be re-initialized with the contents 82386764Sjlemon * of the new SYN here (which may have different options?) 82486764Sjlemon */ 82586764Sjlemon sc = syncache_lookup(inc, &sch); 82686764Sjlemon if (sc != NULL) { 82786764Sjlemon tcpstat.tcps_sc_dupsyn++; 82886764Sjlemon if (ipopts) { 82986764Sjlemon /* 83086764Sjlemon * If we were remembering a previous source route, 83186764Sjlemon * forget it and use the new one we've been given. 83286764Sjlemon */ 83386764Sjlemon if (sc->sc_ipopts) 83486764Sjlemon (void) m_free(sc->sc_ipopts); 83586764Sjlemon sc->sc_ipopts = ipopts; 83686764Sjlemon } 83786764Sjlemon /* 83886764Sjlemon * Update timestamp if present. 83986764Sjlemon */ 84086764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) 84186764Sjlemon sc->sc_tsrecent = to->to_tsval; 84286764Sjlemon if (syncache_respond(sc, m) == 0) { 84386764Sjlemon s = splnet(); 84486764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], 84586764Sjlemon sc, sc_timerq); 84686764Sjlemon SYNCACHE_TIMEOUT(sc, sc->sc_rxtslot); 84786764Sjlemon splx(s); 84886764Sjlemon tcpstat.tcps_sndacks++; 84986764Sjlemon tcpstat.tcps_sndtotal++; 85086764Sjlemon } 85186764Sjlemon *sop = NULL; 85286764Sjlemon return (1); 85386764Sjlemon } 85486764Sjlemon 85586764Sjlemon sc = zalloc(tcp_syncache.zone); 85686764Sjlemon if (sc == NULL) { 85786764Sjlemon /* 85886764Sjlemon * The zone allocator couldn't provide more entries. 85986764Sjlemon * Treat this as if the cache was full; drop the oldest 86086764Sjlemon * entry and insert the new one. 86186764Sjlemon */ 86286764Sjlemon s = splnet(); 86386764Sjlemon for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 86486764Sjlemon sc = TAILQ_FIRST(&tcp_syncache.timerq[i]); 86586764Sjlemon if (sc != NULL) 86686764Sjlemon break; 86786764Sjlemon } 86888180Sjlemon sc->sc_tp->ts_recent = ticks; 86986764Sjlemon syncache_drop(sc, NULL); 87086764Sjlemon splx(s); 87186764Sjlemon tcpstat.tcps_sc_zonefail++; 87286764Sjlemon sc = zalloc(tcp_syncache.zone); 87386764Sjlemon if (sc == NULL) { 87486764Sjlemon if (ipopts) 87586764Sjlemon (void) m_free(ipopts); 87686764Sjlemon return (0); 87786764Sjlemon } 87886764Sjlemon } 87986764Sjlemon 88086764Sjlemon /* 88186764Sjlemon * Fill in the syncache values. 88286764Sjlemon */ 88386958Stanimura bzero(sc, sizeof(*sc)); 88486764Sjlemon sc->sc_tp = tp; 88586764Sjlemon sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 88686764Sjlemon sc->sc_ipopts = ipopts; 88786764Sjlemon sc->sc_inc.inc_fport = inc->inc_fport; 88886764Sjlemon sc->sc_inc.inc_lport = inc->inc_lport; 88986764Sjlemon#ifdef INET6 89086764Sjlemon sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 89186764Sjlemon if (inc->inc_isipv6) { 89286764Sjlemon sc->sc_inc.inc6_faddr = inc->inc6_faddr; 89386764Sjlemon sc->sc_inc.inc6_laddr = inc->inc6_laddr; 89486764Sjlemon sc->sc_route6.ro_rt = NULL; 89586764Sjlemon } else 89686764Sjlemon#endif 89786764Sjlemon { 89886764Sjlemon sc->sc_inc.inc_faddr = inc->inc_faddr; 89986764Sjlemon sc->sc_inc.inc_laddr = inc->inc_laddr; 90086764Sjlemon sc->sc_route.ro_rt = NULL; 90186764Sjlemon } 90286764Sjlemon sc->sc_irs = th->th_seq; 90388330Sjlemon if (tcp_syncookies) 90488330Sjlemon sc->sc_iss = syncookie_generate(sc); 90588330Sjlemon else 90688330Sjlemon sc->sc_iss = arc4random(); 90786764Sjlemon 90886764Sjlemon /* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */ 90986764Sjlemon win = sbspace(&so->so_rcv); 91086764Sjlemon win = imax(win, 0); 91186764Sjlemon win = imin(win, TCP_MAXWIN); 91286764Sjlemon sc->sc_wnd = win; 91386764Sjlemon 91486764Sjlemon sc->sc_flags = 0; 91586764Sjlemon sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0; 91686764Sjlemon if (tcp_do_rfc1323) { 91786764Sjlemon /* 91886764Sjlemon * A timestamp received in a SYN makes 91986764Sjlemon * it ok to send timestamp requests and replies. 92086764Sjlemon */ 92186764Sjlemon if (to->to_flags & TOF_TS) { 92286764Sjlemon sc->sc_tsrecent = to->to_tsval; 92386764Sjlemon sc->sc_flags |= SCF_TIMESTAMP; 92486764Sjlemon } 92586764Sjlemon if (to->to_flags & TOF_SCALE) { 92686764Sjlemon int wscale = 0; 92786764Sjlemon 92886764Sjlemon /* Compute proper scaling value from buffer space */ 92986764Sjlemon while (wscale < TCP_MAX_WINSHIFT && 93086764Sjlemon (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat) 93186764Sjlemon wscale++; 93286764Sjlemon sc->sc_request_r_scale = wscale; 93386764Sjlemon sc->sc_requested_s_scale = to->to_requested_s_scale; 93486764Sjlemon sc->sc_flags |= SCF_WINSCALE; 93586764Sjlemon } 93686764Sjlemon } 93786764Sjlemon if (tcp_do_rfc1644) { 93886764Sjlemon /* 93986764Sjlemon * A CC or CC.new option received in a SYN makes 94086764Sjlemon * it ok to send CC in subsequent segments. 94186764Sjlemon */ 94286764Sjlemon if (to->to_flags & (TOF_CC|TOF_CCNEW)) { 94386764Sjlemon sc->sc_cc_recv = to->to_cc; 94486764Sjlemon sc->sc_cc_send = CC_INC(tcp_ccgen); 94586764Sjlemon sc->sc_flags |= SCF_CC; 94686764Sjlemon } 94786764Sjlemon } 94886764Sjlemon if (tp->t_flags & TF_NOOPT) 94986764Sjlemon sc->sc_flags = SCF_NOOPT; 95086764Sjlemon 95186764Sjlemon /* 95286764Sjlemon * XXX 95386764Sjlemon * We have the option here of not doing TAO (even if the segment 95486764Sjlemon * qualifies) and instead fall back to a normal 3WHS via the syncache. 95586764Sjlemon * This allows us to apply synflood protection to TAO-qualifying SYNs 95686764Sjlemon * also. However, there should be a hueristic to determine when to 95786764Sjlemon * do this, and is not present at the moment. 95886764Sjlemon */ 95986764Sjlemon 96086764Sjlemon /* 96186764Sjlemon * Perform TAO test on incoming CC (SEG.CC) option, if any. 96286764Sjlemon * - compare SEG.CC against cached CC from the same host, if any. 96386764Sjlemon * - if SEG.CC > chached value, SYN must be new and is accepted 96486764Sjlemon * immediately: save new CC in the cache, mark the socket 96586764Sjlemon * connected, enter ESTABLISHED state, turn on flag to 96686764Sjlemon * send a SYN in the next segment. 96786764Sjlemon * A virtual advertised window is set in rcv_adv to 96886764Sjlemon * initialize SWS prevention. Then enter normal segment 96986764Sjlemon * processing: drop SYN, process data and FIN. 97086764Sjlemon * - otherwise do a normal 3-way handshake. 97186764Sjlemon */ 97286764Sjlemon taop = tcp_gettaocache(&sc->sc_inc); 97386764Sjlemon if ((to->to_flags & TOF_CC) != 0) { 97486764Sjlemon if (((tp->t_flags & TF_NOPUSH) != 0) && 97586764Sjlemon sc->sc_flags & SCF_CC && 97686764Sjlemon taop != NULL && taop->tao_cc != 0 && 97786764Sjlemon CC_GT(to->to_cc, taop->tao_cc)) { 97886764Sjlemon sc->sc_rxtslot = 0; 97986764Sjlemon so = syncache_socket(sc, *sop); 98086764Sjlemon if (so != NULL) { 98186764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 98286764Sjlemon taop->tao_cc = to->to_cc; 98386764Sjlemon *sop = so; 98486764Sjlemon } 98586764Sjlemon syncache_free(sc); 98686764Sjlemon return (so != NULL); 98786764Sjlemon } 98886764Sjlemon } else { 98986764Sjlemon /* 99086764Sjlemon * No CC option, but maybe CC.NEW: invalidate cached value. 99186764Sjlemon */ 99286764Sjlemon if (taop != NULL) 99386764Sjlemon taop->tao_cc = 0; 99486764Sjlemon } 99586764Sjlemon /* 99686764Sjlemon * TAO test failed or there was no CC option, 99786764Sjlemon * do a standard 3-way handshake. 99886764Sjlemon */ 99988180Sjlemon if (syncache_respond(sc, m) == 0) { 100088180Sjlemon syncache_insert(sc, sch); 100188180Sjlemon tcpstat.tcps_sndacks++; 100288180Sjlemon tcpstat.tcps_sndtotal++; 100386764Sjlemon } else { 100486764Sjlemon syncache_free(sc); 100588180Sjlemon tcpstat.tcps_sc_dropped++; 100686764Sjlemon } 100786764Sjlemon *sop = NULL; 100886764Sjlemon return (1); 100986764Sjlemon} 101086764Sjlemon 101186764Sjlemonstatic int 101286764Sjlemonsyncache_respond(sc, m) 101386764Sjlemon struct syncache *sc; 101486764Sjlemon struct mbuf *m; 101586764Sjlemon{ 101686764Sjlemon u_int8_t *optp; 101786764Sjlemon int optlen, error; 101886764Sjlemon u_int16_t tlen, hlen, mssopt; 101986764Sjlemon struct ip *ip = NULL; 102086764Sjlemon struct rtentry *rt; 102186764Sjlemon struct tcphdr *th; 102286764Sjlemon#ifdef INET6 102386764Sjlemon struct ip6_hdr *ip6 = NULL; 102486764Sjlemon#endif 102586764Sjlemon 102686764Sjlemon#ifdef INET6 102786764Sjlemon if (sc->sc_inc.inc_isipv6) { 102886764Sjlemon rt = tcp_rtlookup6(&sc->sc_inc); 102986764Sjlemon if (rt != NULL) 103086764Sjlemon mssopt = rt->rt_ifp->if_mtu - 103186764Sjlemon (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 103286764Sjlemon else 103386764Sjlemon mssopt = tcp_v6mssdflt; 103486764Sjlemon hlen = sizeof(struct ip6_hdr); 103586764Sjlemon } else 103686764Sjlemon#endif 103786764Sjlemon { 103886764Sjlemon rt = tcp_rtlookup(&sc->sc_inc); 103986764Sjlemon if (rt != NULL) 104086764Sjlemon mssopt = rt->rt_ifp->if_mtu - 104186764Sjlemon (sizeof(struct ip) + sizeof(struct tcphdr)); 104286764Sjlemon else 104386764Sjlemon mssopt = tcp_mssdflt; 104486764Sjlemon hlen = sizeof(struct ip); 104586764Sjlemon } 104686764Sjlemon 104786764Sjlemon /* Compute the size of the TCP options. */ 104886764Sjlemon if (sc->sc_flags & SCF_NOOPT) { 104986764Sjlemon optlen = 0; 105086764Sjlemon } else { 105186764Sjlemon optlen = TCPOLEN_MAXSEG + 105286764Sjlemon ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) + 105386764Sjlemon ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) + 105486764Sjlemon ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0); 105586764Sjlemon } 105686764Sjlemon tlen = hlen + sizeof(struct tcphdr) + optlen; 105786764Sjlemon 105886764Sjlemon /* 105986764Sjlemon * XXX 106086764Sjlemon * assume that the entire packet will fit in a header mbuf 106186764Sjlemon */ 106286764Sjlemon KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small")); 106386764Sjlemon 106486764Sjlemon /* 106586764Sjlemon * XXX shouldn't this reuse the mbuf if possible ? 106686764Sjlemon * Create the IP+TCP header from scratch. 106786764Sjlemon */ 106886764Sjlemon if (m) 106986764Sjlemon m_freem(m); 107086764Sjlemon 107186764Sjlemon m = m_gethdr(M_DONTWAIT, MT_HEADER); 107286764Sjlemon if (m == NULL) 107386764Sjlemon return (ENOBUFS); 107486764Sjlemon m->m_data += max_linkhdr; 107586764Sjlemon m->m_len = tlen; 107686764Sjlemon m->m_pkthdr.len = tlen; 107786764Sjlemon m->m_pkthdr.rcvif = NULL; 107886764Sjlemon 107986764Sjlemon#ifdef IPSEC 108086764Sjlemon /* use IPsec policy on listening socket to send SYN,ACK */ 108186764Sjlemon if (ipsec_setsocket(m, sc->sc_tp->t_inpcb->inp_socket) != 0) { 108286764Sjlemon m_freem(m); 108386764Sjlemon return (ENOBUFS); 108486764Sjlemon } 108586764Sjlemon#endif 108686764Sjlemon 108786764Sjlemon#ifdef INET6 108886764Sjlemon if (sc->sc_inc.inc_isipv6) { 108986764Sjlemon ip6 = mtod(m, struct ip6_hdr *); 109086764Sjlemon ip6->ip6_vfc = IPV6_VERSION; 109186764Sjlemon ip6->ip6_nxt = IPPROTO_TCP; 109286764Sjlemon ip6->ip6_src = sc->sc_inc.inc6_laddr; 109386764Sjlemon ip6->ip6_dst = sc->sc_inc.inc6_faddr; 109486764Sjlemon ip6->ip6_plen = htons(tlen - hlen); 109586764Sjlemon /* ip6_hlim is set after checksum */ 109686764Sjlemon /* ip6_flow = ??? */ 109786764Sjlemon 109886764Sjlemon th = (struct tcphdr *)(ip6 + 1); 109986764Sjlemon } else 110086764Sjlemon#endif 110186764Sjlemon { 110286764Sjlemon ip = mtod(m, struct ip *); 110386764Sjlemon ip->ip_v = IPVERSION; 110486764Sjlemon ip->ip_hl = sizeof(struct ip) >> 2; 110586764Sjlemon ip->ip_tos = 0; 110686764Sjlemon ip->ip_len = tlen; 110786764Sjlemon ip->ip_id = 0; 110886764Sjlemon ip->ip_off = 0; 110986764Sjlemon ip->ip_ttl = ip_defttl; 111086764Sjlemon ip->ip_sum = 0; 111186764Sjlemon ip->ip_p = IPPROTO_TCP; 111286764Sjlemon ip->ip_src = sc->sc_inc.inc_laddr; 111386764Sjlemon ip->ip_dst = sc->sc_inc.inc_faddr; 111486764Sjlemon 111586764Sjlemon th = (struct tcphdr *)(ip + 1); 111686764Sjlemon } 111786764Sjlemon th->th_sport = sc->sc_inc.inc_lport; 111886764Sjlemon th->th_dport = sc->sc_inc.inc_fport; 111986764Sjlemon 112086764Sjlemon th->th_seq = htonl(sc->sc_iss); 112186764Sjlemon th->th_ack = htonl(sc->sc_irs + 1); 112286764Sjlemon th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 112386764Sjlemon th->th_x2 = 0; 112486764Sjlemon th->th_flags = TH_SYN|TH_ACK; 112586764Sjlemon th->th_win = htons(sc->sc_wnd); 112686764Sjlemon th->th_urp = 0; 112786764Sjlemon 112886764Sjlemon /* Tack on the TCP options. */ 112986764Sjlemon if (optlen == 0) 113086764Sjlemon goto no_options; 113186764Sjlemon optp = (u_int8_t *)(th + 1); 113286764Sjlemon *optp++ = TCPOPT_MAXSEG; 113386764Sjlemon *optp++ = TCPOLEN_MAXSEG; 113486764Sjlemon *optp++ = (mssopt >> 8) & 0xff; 113586764Sjlemon *optp++ = mssopt & 0xff; 113686764Sjlemon 113786764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 113886764Sjlemon *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 113986764Sjlemon TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 114086764Sjlemon sc->sc_request_r_scale); 114186764Sjlemon optp += 4; 114286764Sjlemon } 114386764Sjlemon 114486764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 114586764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 114686764Sjlemon 114786764Sjlemon /* Form timestamp option as shown in appendix A of RFC 1323. */ 114886764Sjlemon *lp++ = htonl(TCPOPT_TSTAMP_HDR); 114986764Sjlemon *lp++ = htonl(ticks); 115086764Sjlemon *lp = htonl(sc->sc_tsrecent); 115186764Sjlemon optp += TCPOLEN_TSTAMP_APPA; 115286764Sjlemon } 115386764Sjlemon 115486764Sjlemon /* 115586764Sjlemon * Send CC and CC.echo if we received CC from our peer. 115686764Sjlemon */ 115786764Sjlemon if (sc->sc_flags & SCF_CC) { 115886764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 115986764Sjlemon 116086764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC)); 116186764Sjlemon *lp++ = htonl(sc->sc_cc_send); 116286764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO)); 116386764Sjlemon *lp = htonl(sc->sc_cc_recv); 116486764Sjlemon optp += TCPOLEN_CC_APPA * 2; 116586764Sjlemon } 116686764Sjlemonno_options: 116786764Sjlemon 116886764Sjlemon#ifdef INET6 116986764Sjlemon if (sc->sc_inc.inc_isipv6) { 117086764Sjlemon struct route_in6 *ro6 = &sc->sc_route6; 117186764Sjlemon 117286764Sjlemon th->th_sum = 0; 117386764Sjlemon th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 117486764Sjlemon ip6->ip6_hlim = in6_selecthlim(NULL, 117586764Sjlemon ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL); 117686764Sjlemon error = ip6_output(m, NULL, ro6, 0, NULL, NULL); 117786764Sjlemon } else 117886764Sjlemon#endif 117986764Sjlemon { 118086764Sjlemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 118186764Sjlemon htons(tlen - hlen + IPPROTO_TCP)); 118286764Sjlemon m->m_pkthdr.csum_flags = CSUM_TCP; 118386764Sjlemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 118486764Sjlemon error = ip_output(m, sc->sc_ipopts, &sc->sc_route, 0, NULL); 118586764Sjlemon } 118686764Sjlemon return (error); 118786764Sjlemon} 118888180Sjlemon 118988180Sjlemon/* 119088180Sjlemon * cookie layers: 119188180Sjlemon * 119288180Sjlemon * |. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .| 119388180Sjlemon * | peer iss | 119488180Sjlemon * | MD5(laddr,faddr,lport,fport,secret) |. . . . . . .| 119588180Sjlemon * | 0 |(A)| | 119688180Sjlemon * (A): peer mss index 119788180Sjlemon */ 119888180Sjlemon 119988180Sjlemon/* 120088180Sjlemon * The values below are chosen to minimize the size of the tcp_secret 120188180Sjlemon * table, as well as providing roughly a 4 second lifetime for the cookie. 120288180Sjlemon */ 120388180Sjlemon 120488180Sjlemon#define SYNCOOKIE_HASHSHIFT 2 /* log2(# of 32bit words from hash) */ 120588180Sjlemon#define SYNCOOKIE_WNDBITS 7 /* exposed bits for window indexing */ 120688180Sjlemon#define SYNCOOKIE_TIMESHIFT 5 /* scale ticks to window time units */ 120788180Sjlemon 120888180Sjlemon#define SYNCOOKIE_HASHMASK ((1 << SYNCOOKIE_HASHSHIFT) - 1) 120988180Sjlemon#define SYNCOOKIE_WNDMASK ((1 << SYNCOOKIE_WNDBITS) - 1) 121088180Sjlemon#define SYNCOOKIE_NSECRETS (1 << (SYNCOOKIE_WNDBITS - SYNCOOKIE_HASHSHIFT)) 121188180Sjlemon#define SYNCOOKIE_TIMEOUT \ 121288180Sjlemon (hz * (1 << SYNCOOKIE_WNDBITS) / (1 << SYNCOOKIE_TIMESHIFT)) 121388180Sjlemon#define SYNCOOKIE_DATAMASK ((3 << SYNCOOKIE_WNDBITS) | SYNCOOKIE_WNDMASK) 121488180Sjlemon 121588180Sjlemonstatic struct { 121688180Sjlemon u_int32_t ts_secbits; 121788180Sjlemon u_int ts_expire; 121888180Sjlemon} tcp_secret[SYNCOOKIE_NSECRETS]; 121988180Sjlemon 122088180Sjlemonstatic int tcp_msstab[] = { 0, 536, 1460, 8960 }; 122188180Sjlemon 122288180Sjlemonstatic MD5_CTX syn_ctx; 122388180Sjlemon 122488180Sjlemon#define MD5Add(v) MD5Update(&syn_ctx, (u_char *)&v, sizeof(v)) 122588180Sjlemon 122688180Sjlemon/* 122788180Sjlemon * Consider the problem of a recreated (and retransmitted) cookie. If the 122888180Sjlemon * original SYN was accepted, the connection is established. The second 122988180Sjlemon * SYN is inflight, and if it arrives with an ISN that falls within the 123088180Sjlemon * receive window, the connection is killed. 123188180Sjlemon * 123288180Sjlemon * However, since cookies have other problems, this may not be worth 123388180Sjlemon * worrying about. 123488180Sjlemon */ 123588180Sjlemon 123688180Sjlemonstatic u_int32_t 123788180Sjlemonsyncookie_generate(struct syncache *sc) 123888180Sjlemon{ 123988180Sjlemon u_int32_t md5_buffer[4]; 124088180Sjlemon u_int32_t data; 124188180Sjlemon int wnd, idx; 124288180Sjlemon 124388180Sjlemon wnd = ((ticks << SYNCOOKIE_TIMESHIFT) / hz) & SYNCOOKIE_WNDMASK; 124488180Sjlemon idx = wnd >> SYNCOOKIE_HASHSHIFT; 124588180Sjlemon if (tcp_secret[idx].ts_expire < ticks) { 124688180Sjlemon tcp_secret[idx].ts_secbits = arc4random(); 124788180Sjlemon tcp_secret[idx].ts_expire = ticks + SYNCOOKIE_TIMEOUT; 124888180Sjlemon } 124988180Sjlemon for (data = sizeof(tcp_msstab) / sizeof(int) - 1; data > 0; data--) 125088180Sjlemon if (tcp_msstab[data] <= sc->sc_peer_mss) 125188180Sjlemon break; 125288180Sjlemon data = (data << SYNCOOKIE_WNDBITS) | wnd; 125388180Sjlemon data ^= sc->sc_irs; /* peer's iss */ 125488180Sjlemon MD5Init(&syn_ctx); 125588180Sjlemon#ifdef INET6 125688180Sjlemon if (sc->sc_inc.inc_isipv6) { 125788180Sjlemon MD5Add(sc->sc_inc.inc6_laddr); 125888180Sjlemon MD5Add(sc->sc_inc.inc6_faddr); 125988180Sjlemon } else 126088180Sjlemon#endif 126188180Sjlemon { 126288180Sjlemon MD5Add(sc->sc_inc.inc_laddr); 126388180Sjlemon MD5Add(sc->sc_inc.inc_faddr); 126488180Sjlemon } 126588180Sjlemon MD5Add(sc->sc_inc.inc_lport); 126688180Sjlemon MD5Add(sc->sc_inc.inc_fport); 126788180Sjlemon MD5Add(tcp_secret[idx].ts_secbits); 126888180Sjlemon MD5Final((u_char *)&md5_buffer, &syn_ctx); 126988180Sjlemon data ^= (md5_buffer[wnd & SYNCOOKIE_HASHMASK] & ~SYNCOOKIE_WNDMASK); 127088180Sjlemon return (data); 127188180Sjlemon} 127288180Sjlemon 127388180Sjlemonstatic struct syncache * 127488180Sjlemonsyncookie_lookup(inc, th, so) 127588180Sjlemon struct in_conninfo *inc; 127688180Sjlemon struct tcphdr *th; 127788180Sjlemon struct socket *so; 127888180Sjlemon{ 127988180Sjlemon u_int32_t md5_buffer[4]; 128088180Sjlemon struct syncache *sc; 128188180Sjlemon u_int32_t data; 128288180Sjlemon int wnd, idx; 128388180Sjlemon 128488180Sjlemon data = (th->th_ack - 1) ^ (th->th_seq - 1); /* remove ISS */ 128588180Sjlemon wnd = data & SYNCOOKIE_WNDMASK; 128688180Sjlemon idx = wnd >> SYNCOOKIE_HASHSHIFT; 128788180Sjlemon if (tcp_secret[idx].ts_expire < ticks || 128888180Sjlemon sototcpcb(so)->ts_recent + SYNCOOKIE_TIMEOUT < ticks) 128988180Sjlemon return (NULL); 129088180Sjlemon MD5Init(&syn_ctx); 129188180Sjlemon#ifdef INET6 129288180Sjlemon if (inc->inc_isipv6) { 129388180Sjlemon MD5Add(inc->inc6_laddr); 129488180Sjlemon MD5Add(inc->inc6_faddr); 129588180Sjlemon } else 129688180Sjlemon#endif 129788180Sjlemon { 129888180Sjlemon MD5Add(inc->inc_laddr); 129988180Sjlemon MD5Add(inc->inc_faddr); 130088180Sjlemon } 130188180Sjlemon MD5Add(inc->inc_lport); 130288180Sjlemon MD5Add(inc->inc_fport); 130388180Sjlemon MD5Add(tcp_secret[idx].ts_secbits); 130488180Sjlemon MD5Final((u_char *)&md5_buffer, &syn_ctx); 130588180Sjlemon data ^= md5_buffer[wnd & SYNCOOKIE_HASHMASK]; 130688180Sjlemon if ((data & ~SYNCOOKIE_DATAMASK) != 0) 130788180Sjlemon return (NULL); 130888180Sjlemon data = data >> SYNCOOKIE_WNDBITS; 130988180Sjlemon 131088180Sjlemon sc = zalloc(tcp_syncache.zone); 131188180Sjlemon if (sc == NULL) 131288180Sjlemon return (NULL); 131388180Sjlemon /* 131488180Sjlemon * Fill in the syncache values. 131588180Sjlemon * XXX duplicate code from syncache_add 131688180Sjlemon */ 131788180Sjlemon sc->sc_ipopts = NULL; 131888180Sjlemon sc->sc_inc.inc_fport = inc->inc_fport; 131988180Sjlemon sc->sc_inc.inc_lport = inc->inc_lport; 132088180Sjlemon#ifdef INET6 132188180Sjlemon sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 132288180Sjlemon if (inc->inc_isipv6) { 132388180Sjlemon sc->sc_inc.inc6_faddr = inc->inc6_faddr; 132488180Sjlemon sc->sc_inc.inc6_laddr = inc->inc6_laddr; 132588180Sjlemon sc->sc_route6.ro_rt = NULL; 132688180Sjlemon } else 132788180Sjlemon#endif 132888180Sjlemon { 132988180Sjlemon sc->sc_inc.inc_faddr = inc->inc_faddr; 133088180Sjlemon sc->sc_inc.inc_laddr = inc->inc_laddr; 133188180Sjlemon sc->sc_route.ro_rt = NULL; 133288180Sjlemon } 133388180Sjlemon sc->sc_irs = th->th_seq - 1; 133488180Sjlemon sc->sc_iss = th->th_ack - 1; 133588180Sjlemon wnd = sbspace(&so->so_rcv); 133688180Sjlemon wnd = imax(wnd, 0); 133788180Sjlemon wnd = imin(wnd, TCP_MAXWIN); 133888180Sjlemon sc->sc_wnd = wnd; 133988180Sjlemon sc->sc_flags = 0; 134088180Sjlemon sc->sc_rxtslot = 0; 134188180Sjlemon sc->sc_peer_mss = tcp_msstab[data]; 134288180Sjlemon return (sc); 134388180Sjlemon} 1344