tcp_syncache.c revision 87193
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 87193 2001-12-02 08:49:29Z dillon $ 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 8886764Sjlemonstatic void syncache_drop(struct syncache *, struct syncache_head *); 8986764Sjlemonstatic void syncache_free(struct syncache *); 9086764Sjlemonstatic int syncache_insert(struct syncache *, struct syncache_head *); 9186764Sjlemonstruct syncache *syncache_lookup(struct in_conninfo *, struct syncache_head **); 9286764Sjlemonstatic int syncache_respond(struct syncache *, struct mbuf *); 9386764Sjlemonstatic struct socket *syncache_socket(struct syncache *, struct socket *); 9486764Sjlemonstatic void syncache_timer(void *); 9586764Sjlemon 9686764Sjlemon/* 9786764Sjlemon * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies. 9886764Sjlemon * 3 retransmits corresponds to a timeout of (1 + 2 + 4 + 8 == 15) seconds, 9986764Sjlemon * the odds are that the user has given up attempting to connect by then. 10086764Sjlemon */ 10186764Sjlemon#define SYNCACHE_MAXREXMTS 3 10286764Sjlemon 10386764Sjlemon/* Arbitrary values */ 10486764Sjlemon#define TCP_SYNCACHE_HASHSIZE 512 10586764Sjlemon#define TCP_SYNCACHE_BUCKETLIMIT 30 10686764Sjlemon 10786764Sjlemonstruct tcp_syncache { 10886764Sjlemon struct syncache_head *hashbase; 10986764Sjlemon struct vm_zone *zone; 11086764Sjlemon u_int hashsize; 11186764Sjlemon u_int hashmask; 11286764Sjlemon u_int bucket_limit; 11386764Sjlemon u_int cache_count; 11486764Sjlemon u_int cache_limit; 11586764Sjlemon u_int rexmt_limit; 11686764Sjlemon u_int hash_secret; 11786764Sjlemon u_int next_reseed; 11886764Sjlemon TAILQ_HEAD(, syncache) timerq[SYNCACHE_MAXREXMTS + 1]; 11986764Sjlemon struct callout tt_timerq[SYNCACHE_MAXREXMTS + 1]; 12086764Sjlemon}; 12186764Sjlemonstatic struct tcp_syncache tcp_syncache; 12286764Sjlemon 12386764SjlemonSYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache"); 12486764Sjlemon 12586764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RD, 12686764Sjlemon &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache"); 12786764Sjlemon 12886764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RD, 12986764Sjlemon &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache"); 13086764Sjlemon 13186764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD, 13286764Sjlemon &tcp_syncache.cache_count, 0, "Current number of entries in syncache"); 13386764Sjlemon 13486764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RD, 13586764Sjlemon &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable"); 13686764Sjlemon 13786764SjlemonSYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW, 13886764Sjlemon &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions"); 13986764Sjlemon 14086764Sjlemonstatic MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache"); 14186764Sjlemon 14286764Sjlemon#define SYNCACHE_HASH(inc, mask) \ 14386764Sjlemon ((tcp_syncache.hash_secret ^ \ 14486764Sjlemon (inc)->inc_faddr.s_addr ^ \ 14586764Sjlemon ((inc)->inc_faddr.s_addr >> 16) ^ \ 14686764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 14786764Sjlemon 14886764Sjlemon#define SYNCACHE_HASH6(inc, mask) \ 14986764Sjlemon ((tcp_syncache.hash_secret ^ \ 15086764Sjlemon (inc)->inc6_faddr.s6_addr32[0] ^ \ 15186764Sjlemon (inc)->inc6_faddr.s6_addr32[3] ^ \ 15286764Sjlemon (inc)->inc_fport ^ (inc)->inc_lport) & mask) 15386764Sjlemon 15486764Sjlemon#define ENDPTS_EQ(a, b) ( \ 15586764Sjlemon (a)->ie_fport == (a)->ie_fport && \ 15686764Sjlemon (a)->ie_lport == (b)->ie_lport && \ 15786764Sjlemon (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \ 15886764Sjlemon (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \ 15986764Sjlemon) 16086764Sjlemon 16186764Sjlemon#define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0) 16286764Sjlemon 16386764Sjlemon#define SYNCACHE_TIMEOUT(sc, slot) do { \ 16486764Sjlemon sc->sc_rxtslot = slot; \ 16586764Sjlemon sc->sc_rxttime = ticks + TCPTV_RTOBASE * tcp_backoff[slot]; \ 16686764Sjlemon TAILQ_INSERT_TAIL(&tcp_syncache.timerq[slot], sc, sc_timerq); \ 16786764Sjlemon if (!callout_active(&tcp_syncache.tt_timerq[slot])) \ 16886764Sjlemon callout_reset(&tcp_syncache.tt_timerq[slot], \ 16986764Sjlemon TCPTV_RTOBASE * tcp_backoff[slot], \ 17086764Sjlemon syncache_timer, (void *)((int)slot)); \ 17186764Sjlemon} while (0) 17286764Sjlemon 17386764Sjlemonstatic void 17486764Sjlemonsyncache_free(struct syncache *sc) 17586764Sjlemon{ 17686764Sjlemon struct rtentry *rt; 17786764Sjlemon 17886764Sjlemon if (sc->sc_ipopts) 17986764Sjlemon (void) m_free(sc->sc_ipopts); 18086764Sjlemon#ifdef INET6 18186764Sjlemon if (sc->sc_inc.inc_isipv6) 18286764Sjlemon rt = sc->sc_route6.ro_rt; 18386764Sjlemon else 18486764Sjlemon#endif 18586764Sjlemon rt = sc->sc_route.ro_rt; 18686764Sjlemon if (rt != NULL) { 18786764Sjlemon /* 18886764Sjlemon * If this is the only reference to a protocol cloned 18986764Sjlemon * route, remove it immediately. 19086764Sjlemon */ 19186764Sjlemon if (rt->rt_flags & RTF_WASCLONED && 19286764Sjlemon (sc->sc_flags & SCF_KEEPROUTE) == 0 && 19386764Sjlemon rt->rt_refcnt == 1) 19486764Sjlemon rtrequest(RTM_DELETE, rt_key(rt), 19586764Sjlemon rt->rt_gateway, rt_mask(rt), 19686764Sjlemon rt->rt_flags, NULL); 19786764Sjlemon RTFREE(rt); 19886764Sjlemon } 19986764Sjlemon zfree(tcp_syncache.zone, sc); 20086764Sjlemon} 20186764Sjlemon 20286764Sjlemonvoid 20386764Sjlemonsyncache_init(void) 20486764Sjlemon{ 20586764Sjlemon int i; 20686764Sjlemon 20786764Sjlemon tcp_syncache.cache_count = 0; 20886764Sjlemon tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE; 20986764Sjlemon tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT; 21086764Sjlemon tcp_syncache.cache_limit = 21186764Sjlemon tcp_syncache.hashsize * tcp_syncache.bucket_limit; 21286764Sjlemon tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS; 21386764Sjlemon tcp_syncache.next_reseed = 0; 21486764Sjlemon tcp_syncache.hash_secret = arc4random(); 21586764Sjlemon 21686764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize", 21786764Sjlemon &tcp_syncache.hashsize); 21886764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit", 21986764Sjlemon &tcp_syncache.cache_limit); 22086764Sjlemon TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit", 22186764Sjlemon &tcp_syncache.bucket_limit); 22286764Sjlemon if (!powerof2(tcp_syncache.hashsize)) { 22386764Sjlemon printf("WARNING: syncache hash size is not a power of 2.\n"); 22486764Sjlemon tcp_syncache.hashsize = 512; /* safe default */ 22586764Sjlemon } 22686764Sjlemon tcp_syncache.hashmask = tcp_syncache.hashsize - 1; 22786764Sjlemon 22886764Sjlemon /* Allocate the hash table. */ 22986764Sjlemon MALLOC(tcp_syncache.hashbase, struct syncache_head *, 23086764Sjlemon tcp_syncache.hashsize * sizeof(struct syncache_head), 23186958Stanimura M_SYNCACHE, M_WAITOK | M_ZERO); 23286764Sjlemon 23386764Sjlemon /* Initialize the hash buckets. */ 23486764Sjlemon for (i = 0; i < tcp_syncache.hashsize; i++) { 23586764Sjlemon TAILQ_INIT(&tcp_syncache.hashbase[i].sch_bucket); 23686764Sjlemon tcp_syncache.hashbase[i].sch_length = 0; 23786764Sjlemon } 23886764Sjlemon 23986764Sjlemon /* Initialize the timer queues. */ 24086814Sbde for (i = 0; i <= SYNCACHE_MAXREXMTS; i++) { 24186764Sjlemon TAILQ_INIT(&tcp_syncache.timerq[i]); 24286764Sjlemon callout_init(&tcp_syncache.tt_timerq[i], 0); 24386764Sjlemon } 24486764Sjlemon 24586764Sjlemon /* 24686764Sjlemon * Allocate the syncache entries. Allow the zone to allocate one 24786764Sjlemon * more entry than cache limit, so a new entry can bump out an 24886764Sjlemon * older one. 24986764Sjlemon */ 25086764Sjlemon tcp_syncache.cache_limit -= 1; 25186764Sjlemon tcp_syncache.zone = zinit("syncache", sizeof(struct syncache), 25286764Sjlemon tcp_syncache.cache_limit, ZONE_INTERRUPT, 0); 25386764Sjlemon} 25486764Sjlemon 25586764Sjlemonstatic int 25686764Sjlemonsyncache_insert(sc, sch) 25786764Sjlemon struct syncache *sc; 25886764Sjlemon struct syncache_head *sch; 25986764Sjlemon{ 26086764Sjlemon struct syncache *sc2; 26186764Sjlemon int s, i; 26286764Sjlemon 26386764Sjlemon /* 26486764Sjlemon * Make sure that we don't overflow the per-bucket 26586764Sjlemon * limit or the total cache size limit. 26686764Sjlemon */ 26786764Sjlemon s = splnet(); 26886764Sjlemon if (sch->sch_length >= tcp_syncache.bucket_limit) { 26986764Sjlemon /* 27086764Sjlemon * The bucket is full, toss the oldest element. 27186764Sjlemon */ 27286764Sjlemon sc2 = TAILQ_FIRST(&sch->sch_bucket); 27386764Sjlemon syncache_drop(sc2, sch); 27486764Sjlemon tcpstat.tcps_sc_bucketoverflow++; 27586764Sjlemon } else if (tcp_syncache.cache_count >= tcp_syncache.cache_limit) { 27686764Sjlemon /* 27786764Sjlemon * The cache is full. Toss the oldest entry in the 27886764Sjlemon * entire cache. This is the front entry in the 27986764Sjlemon * first non-empty timer queue with the largest 28086764Sjlemon * timeout value. 28186764Sjlemon */ 28286764Sjlemon for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 28386764Sjlemon sc2 = TAILQ_FIRST(&tcp_syncache.timerq[i]); 28486764Sjlemon if (sc2 != NULL) 28586764Sjlemon break; 28686764Sjlemon } 28786764Sjlemon syncache_drop(sc2, NULL); 28886764Sjlemon tcpstat.tcps_sc_cacheoverflow++; 28986764Sjlemon } 29086764Sjlemon 29186764Sjlemon /* Initialize the entry's timer. */ 29286764Sjlemon SYNCACHE_TIMEOUT(sc, 0); 29386764Sjlemon 29486764Sjlemon /* Put it into the bucket. */ 29586764Sjlemon TAILQ_INSERT_TAIL(&sch->sch_bucket, sc, sc_hash); 29686764Sjlemon sch->sch_length++; 29786764Sjlemon tcp_syncache.cache_count++; 29886764Sjlemon tcpstat.tcps_sc_added++; 29986764Sjlemon splx(s); 30086764Sjlemon return (1); 30186764Sjlemon} 30286764Sjlemon 30386764Sjlemonstatic void 30486764Sjlemonsyncache_drop(sc, sch) 30586764Sjlemon struct syncache *sc; 30686764Sjlemon struct syncache_head *sch; 30786764Sjlemon{ 30886764Sjlemon int s; 30986764Sjlemon 31086764Sjlemon if (sch == NULL) { 31186764Sjlemon#ifdef INET6 31286764Sjlemon if (sc->sc_inc.inc_isipv6) { 31386764Sjlemon sch = &tcp_syncache.hashbase[ 31486764Sjlemon SYNCACHE_HASH6(&sc->sc_inc, tcp_syncache.hashmask)]; 31586764Sjlemon } else 31686764Sjlemon#endif 31786764Sjlemon { 31886764Sjlemon sch = &tcp_syncache.hashbase[ 31986764Sjlemon SYNCACHE_HASH(&sc->sc_inc, tcp_syncache.hashmask)]; 32086764Sjlemon } 32186764Sjlemon } 32286764Sjlemon 32386764Sjlemon s = splnet(); 32486764Sjlemon 32586764Sjlemon TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash); 32686764Sjlemon sch->sch_length--; 32786764Sjlemon tcp_syncache.cache_count--; 32886764Sjlemon 32986764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], sc, sc_timerq); 33086764Sjlemon if (TAILQ_EMPTY(&tcp_syncache.timerq[sc->sc_rxtslot])) 33186764Sjlemon callout_stop(&tcp_syncache.tt_timerq[sc->sc_rxtslot]); 33286764Sjlemon splx(s); 33386764Sjlemon 33486764Sjlemon syncache_free(sc); 33586764Sjlemon} 33686764Sjlemon 33786764Sjlemon/* 33886764Sjlemon * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted. 33986764Sjlemon * If we have retransmitted an entry the maximum number of times, expire it. 34086764Sjlemon */ 34186764Sjlemonstatic void 34286764Sjlemonsyncache_timer(xslot) 34386764Sjlemon void *xslot; 34486764Sjlemon{ 34586764Sjlemon int slot = (int)xslot; 34686764Sjlemon struct syncache *sc, *nsc; 34786764Sjlemon struct inpcb *inp; 34886764Sjlemon int s; 34986764Sjlemon 35086764Sjlemon s = splnet(); 35186764Sjlemon if (callout_pending(&tcp_syncache.tt_timerq[slot]) || 35286764Sjlemon !callout_active(&tcp_syncache.tt_timerq[slot])) { 35386764Sjlemon splx(s); 35486764Sjlemon return; 35586764Sjlemon } 35686764Sjlemon callout_deactivate(&tcp_syncache.tt_timerq[slot]); 35786764Sjlemon 35886764Sjlemon nsc = TAILQ_FIRST(&tcp_syncache.timerq[slot]); 35986764Sjlemon while (nsc != NULL) { 36086764Sjlemon if (ticks < nsc->sc_rxttime) 36186764Sjlemon break; 36286764Sjlemon sc = nsc; 36386764Sjlemon nsc = TAILQ_NEXT(sc, sc_timerq); 36486764Sjlemon inp = sc->sc_tp->t_inpcb; 36586764Sjlemon if (slot == SYNCACHE_MAXREXMTS || 36686764Sjlemon slot >= tcp_syncache.rexmt_limit || 36786764Sjlemon inp->inp_gencnt != sc->sc_inp_gencnt) { 36886764Sjlemon syncache_drop(sc, NULL); 36986764Sjlemon tcpstat.tcps_sc_stale++; 37086764Sjlemon continue; 37186764Sjlemon } 37286764Sjlemon (void) syncache_respond(sc, NULL); 37386764Sjlemon tcpstat.tcps_sc_retransmitted++; 37486764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[slot], sc, sc_timerq); 37586764Sjlemon SYNCACHE_TIMEOUT(sc, slot + 1); 37686764Sjlemon } 37786764Sjlemon if (nsc != NULL) 37886764Sjlemon callout_reset(&tcp_syncache.tt_timerq[slot], 37986764Sjlemon nsc->sc_rxttime - ticks, syncache_timer, (void *)(slot)); 38086764Sjlemon splx(s); 38186764Sjlemon} 38286764Sjlemon 38386764Sjlemon/* 38486764Sjlemon * Find an entry in the syncache. 38586764Sjlemon */ 38686764Sjlemonstruct syncache * 38786764Sjlemonsyncache_lookup(inc, schp) 38886764Sjlemon struct in_conninfo *inc; 38986764Sjlemon struct syncache_head **schp; 39086764Sjlemon{ 39186764Sjlemon struct syncache *sc; 39286764Sjlemon struct syncache_head *sch; 39386764Sjlemon int s; 39486764Sjlemon 39586764Sjlemon#ifdef INET6 39686764Sjlemon if (inc->inc_isipv6) { 39786764Sjlemon sch = &tcp_syncache.hashbase[ 39886764Sjlemon SYNCACHE_HASH6(inc, tcp_syncache.hashmask)]; 39986764Sjlemon *schp = sch; 40086764Sjlemon s = splnet(); 40186764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 40286764Sjlemon if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 40386764Sjlemon splx(s); 40486764Sjlemon return (sc); 40586764Sjlemon } 40686764Sjlemon } 40786764Sjlemon splx(s); 40886764Sjlemon } else 40986764Sjlemon#endif 41086764Sjlemon { 41186764Sjlemon sch = &tcp_syncache.hashbase[ 41286764Sjlemon SYNCACHE_HASH(inc, tcp_syncache.hashmask)]; 41386764Sjlemon *schp = sch; 41486764Sjlemon s = splnet(); 41586764Sjlemon TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) { 41686764Sjlemon#ifdef INET6 41786764Sjlemon if (sc->sc_inc.inc_isipv6) 41886764Sjlemon continue; 41986764Sjlemon#endif 42086764Sjlemon if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie)) { 42186764Sjlemon splx(s); 42286764Sjlemon return (sc); 42386764Sjlemon } 42486764Sjlemon } 42586764Sjlemon splx(s); 42686764Sjlemon } 42786764Sjlemon return (NULL); 42886764Sjlemon} 42986764Sjlemon 43086764Sjlemon/* 43186764Sjlemon * This function is called when we get a RST for a 43286764Sjlemon * non-existent connection, so that we can see if the 43386764Sjlemon * connection is in the syn cache. If it is, zap it. 43486764Sjlemon */ 43586764Sjlemonvoid 43686764Sjlemonsyncache_chkrst(inc, th) 43786764Sjlemon struct in_conninfo *inc; 43886764Sjlemon struct tcphdr *th; 43986764Sjlemon{ 44086764Sjlemon struct syncache *sc; 44186764Sjlemon struct syncache_head *sch; 44286764Sjlemon 44386764Sjlemon sc = syncache_lookup(inc, &sch); 44486764Sjlemon if (sc == NULL) 44586764Sjlemon return; 44686764Sjlemon /* 44786764Sjlemon * If the RST bit is set, check the sequence number to see 44886764Sjlemon * if this is a valid reset segment. 44986764Sjlemon * RFC 793 page 37: 45086764Sjlemon * In all states except SYN-SENT, all reset (RST) segments 45186764Sjlemon * are validated by checking their SEQ-fields. A reset is 45286764Sjlemon * valid if its sequence number is in the window. 45386764Sjlemon * 45486764Sjlemon * The sequence number in the reset segment is normally an 45586764Sjlemon * echo of our outgoing acknowlegement numbers, but some hosts 45686764Sjlemon * send a reset with the sequence number at the rightmost edge 45786764Sjlemon * of our receive window, and we have to handle this case. 45886764Sjlemon */ 45986764Sjlemon if (SEQ_GEQ(th->th_seq, sc->sc_irs) && 46086764Sjlemon SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) { 46186764Sjlemon syncache_drop(sc, sch); 46286764Sjlemon tcpstat.tcps_sc_reset++; 46386764Sjlemon } 46486764Sjlemon} 46586764Sjlemon 46686764Sjlemonvoid 46786764Sjlemonsyncache_badack(inc) 46886764Sjlemon struct in_conninfo *inc; 46986764Sjlemon{ 47086764Sjlemon struct syncache *sc; 47186764Sjlemon struct syncache_head *sch; 47286764Sjlemon 47386764Sjlemon sc = syncache_lookup(inc, &sch); 47486764Sjlemon if (sc != NULL) { 47586764Sjlemon syncache_drop(sc, sch); 47686764Sjlemon tcpstat.tcps_sc_badack++; 47786764Sjlemon } 47886764Sjlemon} 47986764Sjlemon 48086764Sjlemonvoid 48186764Sjlemonsyncache_unreach(inc, th) 48286764Sjlemon struct in_conninfo *inc; 48386764Sjlemon struct tcphdr *th; 48486764Sjlemon{ 48586764Sjlemon struct syncache *sc; 48686764Sjlemon struct syncache_head *sch; 48786764Sjlemon 48886764Sjlemon /* we are called at splnet() here */ 48986764Sjlemon sc = syncache_lookup(inc, &sch); 49086764Sjlemon if (sc == NULL) 49186764Sjlemon return; 49286764Sjlemon 49386764Sjlemon /* If the sequence number != sc_iss, then it's a bogus ICMP msg */ 49486764Sjlemon if (ntohl(th->th_seq) != sc->sc_iss) 49586764Sjlemon return; 49686764Sjlemon 49786764Sjlemon /* 49886764Sjlemon * If we've rertransmitted 3 times and this is our second error, 49986764Sjlemon * we remove the entry. Otherwise, we allow it to continue on. 50086764Sjlemon * This prevents us from incorrectly nuking an entry during a 50186764Sjlemon * spurious network outage. 50286764Sjlemon * 50386764Sjlemon * See tcp_notify(). 50486764Sjlemon */ 50586764Sjlemon if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtslot < 3) { 50686764Sjlemon sc->sc_flags |= SCF_UNREACH; 50786764Sjlemon return; 50886764Sjlemon } 50986764Sjlemon syncache_drop(sc, sch); 51086764Sjlemon tcpstat.tcps_sc_unreach++; 51186764Sjlemon} 51286764Sjlemon 51386764Sjlemon/* 51486764Sjlemon * Build a new TCP socket structure from a syncache entry. 51586764Sjlemon */ 51686764Sjlemonstatic struct socket * 51786764Sjlemonsyncache_socket(sc, lso) 51886764Sjlemon struct syncache *sc; 51986764Sjlemon struct socket *lso; 52086764Sjlemon{ 52186764Sjlemon struct inpcb *inp = NULL; 52286764Sjlemon struct socket *so; 52386764Sjlemon struct tcpcb *tp; 52486764Sjlemon 52586764Sjlemon /* 52686764Sjlemon * Ok, create the full blown connection, and set things up 52786764Sjlemon * as they would have been set up if we had created the 52886764Sjlemon * connection when the SYN arrived. If we can't create 52986764Sjlemon * the connection, abort it. 53086764Sjlemon */ 53186764Sjlemon so = sonewconn(lso, SS_ISCONNECTED); 53286764Sjlemon if (so == NULL) { 53386764Sjlemon /* 53486764Sjlemon * Drop the connection; we will send a RST if the peer 53586764Sjlemon * retransmits the ACK, 53686764Sjlemon */ 53786764Sjlemon tcpstat.tcps_listendrop++; 53886764Sjlemon goto abort; 53986764Sjlemon } 54086764Sjlemon 54186764Sjlemon inp = sotoinpcb(so); 54286764Sjlemon 54386764Sjlemon /* 54486764Sjlemon * Insert new socket into hash list. 54586764Sjlemon */ 54686764Sjlemon#ifdef INET6 54786764Sjlemon if (sc->sc_inc.inc_isipv6) { 54886764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 54986764Sjlemon } else { 55086764Sjlemon inp->inp_vflag &= ~INP_IPV6; 55186764Sjlemon inp->inp_vflag |= INP_IPV4; 55286764Sjlemon#endif 55386764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 55486764Sjlemon#ifdef INET6 55586764Sjlemon } 55686764Sjlemon#endif 55786764Sjlemon inp->inp_lport = sc->sc_inc.inc_lport; 55886764Sjlemon if (in_pcbinshash(inp) != 0) { 55986764Sjlemon /* 56086764Sjlemon * Undo the assignments above if we failed to 56186764Sjlemon * put the PCB on the hash lists. 56286764Sjlemon */ 56386764Sjlemon#ifdef INET6 56486764Sjlemon if (sc->sc_inc.inc_isipv6) 56586764Sjlemon inp->in6p_laddr = in6addr_any; 56686764Sjlemon else 56786764Sjlemon#endif 56886764Sjlemon inp->inp_laddr.s_addr = INADDR_ANY; 56986764Sjlemon inp->inp_lport = 0; 57086764Sjlemon goto abort; 57186764Sjlemon } 57286764Sjlemon#ifdef IPSEC 57386764Sjlemon /* copy old policy into new socket's */ 57486764Sjlemon if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp)) 57586764Sjlemon printf("syncache_expand: could not copy policy\n"); 57686764Sjlemon#endif 57786764Sjlemon#ifdef INET6 57886764Sjlemon if (sc->sc_inc.inc_isipv6) { 57986764Sjlemon struct inpcb *oinp = sotoinpcb(lso); 58086764Sjlemon struct in6_addr laddr6; 58186764Sjlemon struct sockaddr_in6 *sin6; 58286764Sjlemon /* 58386764Sjlemon * Inherit socket options from the listening socket. 58486764Sjlemon * Note that in6p_inputopts are not (and should not be) 58586764Sjlemon * copied, since it stores previously received options and is 58686764Sjlemon * used to detect if each new option is different than the 58786764Sjlemon * previous one and hence should be passed to a user. 58886764Sjlemon * If we copied in6p_inputopts, a user would not be able to 58986764Sjlemon * receive options just after calling the accept system call. 59086764Sjlemon */ 59186764Sjlemon inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS; 59286764Sjlemon if (oinp->in6p_outputopts) 59386764Sjlemon inp->in6p_outputopts = 59486764Sjlemon ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT); 59586764Sjlemon inp->in6p_route = sc->sc_route6; 59686764Sjlemon sc->sc_route6.ro_rt = NULL; 59786764Sjlemon 59886764Sjlemon MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, 59986958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 60086764Sjlemon if (sin6 == NULL) 60186764Sjlemon goto abort; 60286764Sjlemon sin6->sin6_family = AF_INET6; 60386764Sjlemon sin6->sin6_len = sizeof(*sin6); 60486764Sjlemon sin6->sin6_addr = sc->sc_inc.inc6_faddr; 60586764Sjlemon sin6->sin6_port = sc->sc_inc.inc_fport; 60686764Sjlemon laddr6 = inp->in6p_laddr; 60786764Sjlemon if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 60886764Sjlemon inp->in6p_laddr = sc->sc_inc.inc6_laddr; 60986764Sjlemon if (in6_pcbconnect(inp, (struct sockaddr *)sin6, thread0)) { 61086764Sjlemon inp->in6p_laddr = laddr6; 61186764Sjlemon FREE(sin6, M_SONAME); 61286764Sjlemon goto abort; 61386764Sjlemon } 61486764Sjlemon FREE(sin6, M_SONAME); 61586764Sjlemon } else 61686764Sjlemon#endif 61786764Sjlemon { 61886764Sjlemon struct in_addr laddr; 61986764Sjlemon struct sockaddr_in *sin; 62086764Sjlemon 62186764Sjlemon inp->inp_options = ip_srcroute(); 62286764Sjlemon if (inp->inp_options == NULL) { 62386764Sjlemon inp->inp_options = sc->sc_ipopts; 62486764Sjlemon sc->sc_ipopts = NULL; 62586764Sjlemon } 62686764Sjlemon inp->inp_route = sc->sc_route; 62786764Sjlemon sc->sc_route.ro_rt = NULL; 62886764Sjlemon 62986764Sjlemon MALLOC(sin, struct sockaddr_in *, sizeof *sin, 63086958Stanimura M_SONAME, M_NOWAIT | M_ZERO); 63186764Sjlemon if (sin == NULL) 63286764Sjlemon goto abort; 63386764Sjlemon sin->sin_family = AF_INET; 63486764Sjlemon sin->sin_len = sizeof(*sin); 63586764Sjlemon sin->sin_addr = sc->sc_inc.inc_faddr; 63686764Sjlemon sin->sin_port = sc->sc_inc.inc_fport; 63786764Sjlemon bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); 63886764Sjlemon laddr = inp->inp_laddr; 63986764Sjlemon if (inp->inp_laddr.s_addr == INADDR_ANY) 64086764Sjlemon inp->inp_laddr = sc->sc_inc.inc_laddr; 64186764Sjlemon if (in_pcbconnect(inp, (struct sockaddr *)sin, thread0)) { 64286764Sjlemon inp->inp_laddr = laddr; 64386764Sjlemon FREE(sin, M_SONAME); 64486764Sjlemon goto abort; 64586764Sjlemon } 64686764Sjlemon FREE(sin, M_SONAME); 64786764Sjlemon } 64886764Sjlemon 64986764Sjlemon tp = intotcpcb(inp); 65086764Sjlemon tp->t_state = TCPS_SYN_RECEIVED; 65186764Sjlemon tp->iss = sc->sc_iss; 65286764Sjlemon tp->irs = sc->sc_irs; 65386764Sjlemon tcp_rcvseqinit(tp); 65486764Sjlemon tcp_sendseqinit(tp); 65586764Sjlemon tp->snd_wl1 = sc->sc_irs; 65686764Sjlemon tp->rcv_up = sc->sc_irs + 1; 65786764Sjlemon tp->rcv_wnd = sc->sc_wnd; 65886764Sjlemon tp->rcv_adv += tp->rcv_wnd; 65986764Sjlemon 66087193Sdillon tp->t_flags = sc->sc_tp->t_flags & (TF_NOPUSH|TF_NODELAY); 66186764Sjlemon if (sc->sc_flags & SCF_NOOPT) 66286764Sjlemon tp->t_flags |= TF_NOOPT; 66386764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 66486764Sjlemon tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE; 66586764Sjlemon tp->requested_s_scale = sc->sc_requested_s_scale; 66686764Sjlemon tp->request_r_scale = sc->sc_request_r_scale; 66786764Sjlemon } 66886764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 66986764Sjlemon tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP; 67086764Sjlemon tp->ts_recent = sc->sc_tsrecent; 67186764Sjlemon tp->ts_recent_age = ticks; 67286764Sjlemon } 67386764Sjlemon if (sc->sc_flags & SCF_CC) { 67486764Sjlemon /* 67586764Sjlemon * Initialization of the tcpcb for transaction; 67686764Sjlemon * set SND.WND = SEG.WND, 67786764Sjlemon * initialize CCsend and CCrecv. 67886764Sjlemon */ 67986764Sjlemon tp->t_flags |= TF_REQ_CC|TF_RCVD_CC; 68086764Sjlemon tp->cc_send = sc->sc_cc_send; 68186764Sjlemon tp->cc_recv = sc->sc_cc_recv; 68286764Sjlemon } 68386764Sjlemon 68486764Sjlemon tcp_mss(tp, sc->sc_peer_mss); 68586764Sjlemon 68686764Sjlemon /* 68786764Sjlemon * If the SYN,ACK was retransmitted, reset cwnd to 1 segment. 68886764Sjlemon */ 68986764Sjlemon if (sc->sc_rxtslot != 0) 69086764Sjlemon tp->snd_cwnd = tp->t_maxseg; 69186764Sjlemon callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 69286764Sjlemon 69386764Sjlemon tcpstat.tcps_accepts++; 69486764Sjlemon return (so); 69586764Sjlemon 69686764Sjlemonabort: 69786764Sjlemon if (so != NULL) 69886764Sjlemon (void) soabort(so); 69986764Sjlemon return (NULL); 70086764Sjlemon} 70186764Sjlemon 70286764Sjlemon/* 70386764Sjlemon * This function gets called when we receive an ACK for a 70486764Sjlemon * socket in the LISTEN state. We look up the connection 70586764Sjlemon * in the syncache, and if its there, we pull it out of 70686764Sjlemon * the cache and turn it into a full-blown connection in 70786764Sjlemon * the SYN-RECEIVED state. 70886764Sjlemon */ 70986764Sjlemonint 71086764Sjlemonsyncache_expand(inc, th, sop, m) 71186764Sjlemon struct in_conninfo *inc; 71286764Sjlemon struct tcphdr *th; 71386764Sjlemon struct socket **sop; 71486764Sjlemon struct mbuf *m; 71586764Sjlemon{ 71686764Sjlemon struct syncache *sc; 71786764Sjlemon struct syncache_head *sch; 71886764Sjlemon struct socket *so; 71986764Sjlemon 72086764Sjlemon sc = syncache_lookup(inc, &sch); 72186764Sjlemon if (sc == NULL) 72286764Sjlemon return (0); 72386764Sjlemon 72486764Sjlemon /* 72586764Sjlemon * If seg contains an ACK, but not for our SYN/ACK, send a RST. 72686764Sjlemon */ 72786764Sjlemon if (th->th_ack != sc->sc_iss + 1) 72886764Sjlemon return (0); 72986764Sjlemon 73086764Sjlemon so = syncache_socket(sc, *sop); 73186764Sjlemon if (so == NULL) { 73286764Sjlemon#if 0 73386764Sjlemonresetandabort: 73486764Sjlemon /* XXXjlemon check this - is this correct? */ 73586764Sjlemon (void) tcp_respond(NULL, m, m, th, 73686764Sjlemon th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK); 73786764Sjlemon#endif 73886764Sjlemon m_freem(m); /* XXX only needed for above */ 73986764Sjlemon tcpstat.tcps_sc_aborted++; 74086764Sjlemon } else { 74186764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 74286764Sjlemon tcpstat.tcps_sc_completed++; 74386764Sjlemon } 74486764Sjlemon if (sch == NULL) 74586764Sjlemon syncache_free(sc); 74686764Sjlemon else 74786764Sjlemon syncache_drop(sc, sch); 74886764Sjlemon *sop = so; 74986764Sjlemon return (1); 75086764Sjlemon} 75186764Sjlemon 75286764Sjlemon/* 75386764Sjlemon * Given a LISTEN socket and an inbound SYN request, add 75486764Sjlemon * this to the syn cache, and send back a segment: 75586764Sjlemon * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 75686764Sjlemon * to the source. 75786764Sjlemon * 75886764Sjlemon * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN. 75986764Sjlemon * Doing so would require that we hold onto the data and deliver it 76086764Sjlemon * to the application. However, if we are the target of a SYN-flood 76186764Sjlemon * DoS attack, an attacker could send data which would eventually 76286764Sjlemon * consume all available buffer space if it were ACKed. By not ACKing 76386764Sjlemon * the data, we avoid this DoS scenario. 76486764Sjlemon */ 76586764Sjlemonint 76686764Sjlemonsyncache_add(inc, to, th, sop, m) 76786764Sjlemon struct in_conninfo *inc; 76886764Sjlemon struct tcpopt *to; 76986764Sjlemon struct tcphdr *th; 77086764Sjlemon struct socket **sop; 77186764Sjlemon struct mbuf *m; 77286764Sjlemon{ 77386764Sjlemon struct tcpcb *tp; 77486764Sjlemon struct socket *so; 77586764Sjlemon struct syncache *sc = NULL; 77686764Sjlemon struct syncache_head *sch; 77786764Sjlemon struct mbuf *ipopts = NULL; 77886764Sjlemon struct rmxp_tao *taop; 77986764Sjlemon int i, s, win; 78086764Sjlemon 78186764Sjlemon so = *sop; 78286764Sjlemon tp = sototcpcb(so); 78386764Sjlemon 78486764Sjlemon /* 78586764Sjlemon * Remember the IP options, if any. 78686764Sjlemon */ 78786764Sjlemon#ifdef INET6 78886764Sjlemon if (!inc->inc_isipv6) 78986764Sjlemon#endif 79086764Sjlemon ipopts = ip_srcroute(); 79186764Sjlemon 79286764Sjlemon /* 79386764Sjlemon * See if we already have an entry for this connection. 79486764Sjlemon * If we do, resend the SYN,ACK, and reset the retransmit timer. 79586764Sjlemon * 79686764Sjlemon * XXX 79786764Sjlemon * should the syncache be re-initialized with the contents 79886764Sjlemon * of the new SYN here (which may have different options?) 79986764Sjlemon */ 80086764Sjlemon sc = syncache_lookup(inc, &sch); 80186764Sjlemon if (sc != NULL) { 80286764Sjlemon tcpstat.tcps_sc_dupsyn++; 80386764Sjlemon if (ipopts) { 80486764Sjlemon /* 80586764Sjlemon * If we were remembering a previous source route, 80686764Sjlemon * forget it and use the new one we've been given. 80786764Sjlemon */ 80886764Sjlemon if (sc->sc_ipopts) 80986764Sjlemon (void) m_free(sc->sc_ipopts); 81086764Sjlemon sc->sc_ipopts = ipopts; 81186764Sjlemon } 81286764Sjlemon /* 81386764Sjlemon * Update timestamp if present. 81486764Sjlemon */ 81586764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) 81686764Sjlemon sc->sc_tsrecent = to->to_tsval; 81786764Sjlemon if (syncache_respond(sc, m) == 0) { 81886764Sjlemon s = splnet(); 81986764Sjlemon TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], 82086764Sjlemon sc, sc_timerq); 82186764Sjlemon SYNCACHE_TIMEOUT(sc, sc->sc_rxtslot); 82286764Sjlemon splx(s); 82386764Sjlemon tcpstat.tcps_sndacks++; 82486764Sjlemon tcpstat.tcps_sndtotal++; 82586764Sjlemon } 82686764Sjlemon *sop = NULL; 82786764Sjlemon return (1); 82886764Sjlemon } 82986764Sjlemon 83086764Sjlemon sc = zalloc(tcp_syncache.zone); 83186764Sjlemon if (sc == NULL) { 83286764Sjlemon /* 83386764Sjlemon * The zone allocator couldn't provide more entries. 83486764Sjlemon * Treat this as if the cache was full; drop the oldest 83586764Sjlemon * entry and insert the new one. 83686764Sjlemon */ 83786764Sjlemon s = splnet(); 83886764Sjlemon for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) { 83986764Sjlemon sc = TAILQ_FIRST(&tcp_syncache.timerq[i]); 84086764Sjlemon if (sc != NULL) 84186764Sjlemon break; 84286764Sjlemon } 84386764Sjlemon syncache_drop(sc, NULL); 84486764Sjlemon splx(s); 84586764Sjlemon tcpstat.tcps_sc_zonefail++; 84686764Sjlemon sc = zalloc(tcp_syncache.zone); 84786764Sjlemon if (sc == NULL) { 84886764Sjlemon if (ipopts) 84986764Sjlemon (void) m_free(ipopts); 85086764Sjlemon return (0); 85186764Sjlemon } 85286764Sjlemon } 85386764Sjlemon 85486764Sjlemon /* 85586764Sjlemon * Fill in the syncache values. 85686764Sjlemon */ 85786958Stanimura bzero(sc, sizeof(*sc)); 85886764Sjlemon sc->sc_tp = tp; 85986764Sjlemon sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt; 86086764Sjlemon sc->sc_ipopts = ipopts; 86186764Sjlemon sc->sc_inc.inc_fport = inc->inc_fport; 86286764Sjlemon sc->sc_inc.inc_lport = inc->inc_lport; 86386764Sjlemon#ifdef INET6 86486764Sjlemon sc->sc_inc.inc_isipv6 = inc->inc_isipv6; 86586764Sjlemon if (inc->inc_isipv6) { 86686764Sjlemon sc->sc_inc.inc6_faddr = inc->inc6_faddr; 86786764Sjlemon sc->sc_inc.inc6_laddr = inc->inc6_laddr; 86886764Sjlemon sc->sc_route6.ro_rt = NULL; 86986764Sjlemon } else 87086764Sjlemon#endif 87186764Sjlemon { 87286764Sjlemon sc->sc_inc.inc_faddr = inc->inc_faddr; 87386764Sjlemon sc->sc_inc.inc_laddr = inc->inc_laddr; 87486764Sjlemon sc->sc_route.ro_rt = NULL; 87586764Sjlemon } 87686764Sjlemon sc->sc_irs = th->th_seq; 87786764Sjlemon 87886764Sjlemon /* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */ 87986764Sjlemon win = sbspace(&so->so_rcv); 88086764Sjlemon win = imax(win, 0); 88186764Sjlemon win = imin(win, TCP_MAXWIN); 88286764Sjlemon sc->sc_wnd = win; 88386764Sjlemon 88486764Sjlemon sc->sc_flags = 0; 88586764Sjlemon sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0; 88686764Sjlemon if (tcp_do_rfc1323) { 88786764Sjlemon /* 88886764Sjlemon * A timestamp received in a SYN makes 88986764Sjlemon * it ok to send timestamp requests and replies. 89086764Sjlemon */ 89186764Sjlemon if (to->to_flags & TOF_TS) { 89286764Sjlemon sc->sc_tsrecent = to->to_tsval; 89386764Sjlemon sc->sc_flags |= SCF_TIMESTAMP; 89486764Sjlemon } 89586764Sjlemon if (to->to_flags & TOF_SCALE) { 89686764Sjlemon int wscale = 0; 89786764Sjlemon 89886764Sjlemon /* Compute proper scaling value from buffer space */ 89986764Sjlemon while (wscale < TCP_MAX_WINSHIFT && 90086764Sjlemon (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat) 90186764Sjlemon wscale++; 90286764Sjlemon sc->sc_request_r_scale = wscale; 90386764Sjlemon sc->sc_requested_s_scale = to->to_requested_s_scale; 90486764Sjlemon sc->sc_flags |= SCF_WINSCALE; 90586764Sjlemon } 90686764Sjlemon } 90786764Sjlemon if (tcp_do_rfc1644) { 90886764Sjlemon /* 90986764Sjlemon * A CC or CC.new option received in a SYN makes 91086764Sjlemon * it ok to send CC in subsequent segments. 91186764Sjlemon */ 91286764Sjlemon if (to->to_flags & (TOF_CC|TOF_CCNEW)) { 91386764Sjlemon sc->sc_cc_recv = to->to_cc; 91486764Sjlemon sc->sc_cc_send = CC_INC(tcp_ccgen); 91586764Sjlemon sc->sc_flags |= SCF_CC; 91686764Sjlemon } 91786764Sjlemon } 91886764Sjlemon if (tp->t_flags & TF_NOOPT) 91986764Sjlemon sc->sc_flags = SCF_NOOPT; 92086764Sjlemon 92186764Sjlemon /* 92286764Sjlemon * XXX 92386764Sjlemon * We have the option here of not doing TAO (even if the segment 92486764Sjlemon * qualifies) and instead fall back to a normal 3WHS via the syncache. 92586764Sjlemon * This allows us to apply synflood protection to TAO-qualifying SYNs 92686764Sjlemon * also. However, there should be a hueristic to determine when to 92786764Sjlemon * do this, and is not present at the moment. 92886764Sjlemon */ 92986764Sjlemon 93086764Sjlemon /* 93186764Sjlemon * Perform TAO test on incoming CC (SEG.CC) option, if any. 93286764Sjlemon * - compare SEG.CC against cached CC from the same host, if any. 93386764Sjlemon * - if SEG.CC > chached value, SYN must be new and is accepted 93486764Sjlemon * immediately: save new CC in the cache, mark the socket 93586764Sjlemon * connected, enter ESTABLISHED state, turn on flag to 93686764Sjlemon * send a SYN in the next segment. 93786764Sjlemon * A virtual advertised window is set in rcv_adv to 93886764Sjlemon * initialize SWS prevention. Then enter normal segment 93986764Sjlemon * processing: drop SYN, process data and FIN. 94086764Sjlemon * - otherwise do a normal 3-way handshake. 94186764Sjlemon */ 94286764Sjlemon taop = tcp_gettaocache(&sc->sc_inc); 94386764Sjlemon if ((to->to_flags & TOF_CC) != 0) { 94486764Sjlemon if (((tp->t_flags & TF_NOPUSH) != 0) && 94586764Sjlemon sc->sc_flags & SCF_CC && 94686764Sjlemon taop != NULL && taop->tao_cc != 0 && 94786764Sjlemon CC_GT(to->to_cc, taop->tao_cc)) { 94886764Sjlemon sc->sc_rxtslot = 0; 94986764Sjlemon so = syncache_socket(sc, *sop); 95086764Sjlemon if (so != NULL) { 95186764Sjlemon sc->sc_flags |= SCF_KEEPROUTE; 95286764Sjlemon taop->tao_cc = to->to_cc; 95386764Sjlemon *sop = so; 95486764Sjlemon } 95586764Sjlemon syncache_free(sc); 95686764Sjlemon return (so != NULL); 95786764Sjlemon } 95886764Sjlemon } else { 95986764Sjlemon /* 96086764Sjlemon * No CC option, but maybe CC.NEW: invalidate cached value. 96186764Sjlemon */ 96286764Sjlemon if (taop != NULL) 96386764Sjlemon taop->tao_cc = 0; 96486764Sjlemon } 96586764Sjlemon /* 96686764Sjlemon * TAO test failed or there was no CC option, 96786764Sjlemon * do a standard 3-way handshake. 96886764Sjlemon */ 96986764Sjlemon sc->sc_iss = arc4random(); 97086764Sjlemon if (syncache_insert(sc, sch)) { 97186764Sjlemon if (syncache_respond(sc, m) == 0) { 97286764Sjlemon tcpstat.tcps_sndacks++; 97386764Sjlemon tcpstat.tcps_sndtotal++; 97486764Sjlemon } else { 97586764Sjlemon syncache_drop(sc, sch); 97686764Sjlemon tcpstat.tcps_sc_dropped++; 97786764Sjlemon } 97886764Sjlemon } else { 97986764Sjlemon syncache_free(sc); 98086764Sjlemon } 98186764Sjlemon *sop = NULL; 98286764Sjlemon return (1); 98386764Sjlemon} 98486764Sjlemon 98586764Sjlemonstatic int 98686764Sjlemonsyncache_respond(sc, m) 98786764Sjlemon struct syncache *sc; 98886764Sjlemon struct mbuf *m; 98986764Sjlemon{ 99086764Sjlemon u_int8_t *optp; 99186764Sjlemon int optlen, error; 99286764Sjlemon u_int16_t tlen, hlen, mssopt; 99386764Sjlemon struct ip *ip = NULL; 99486764Sjlemon struct rtentry *rt; 99586764Sjlemon struct tcphdr *th; 99686764Sjlemon#ifdef INET6 99786764Sjlemon struct ip6_hdr *ip6 = NULL; 99886764Sjlemon#endif 99986764Sjlemon 100086764Sjlemon#ifdef INET6 100186764Sjlemon if (sc->sc_inc.inc_isipv6) { 100286764Sjlemon rt = tcp_rtlookup6(&sc->sc_inc); 100386764Sjlemon if (rt != NULL) 100486764Sjlemon mssopt = rt->rt_ifp->if_mtu - 100586764Sjlemon (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 100686764Sjlemon else 100786764Sjlemon mssopt = tcp_v6mssdflt; 100886764Sjlemon hlen = sizeof(struct ip6_hdr); 100986764Sjlemon } else 101086764Sjlemon#endif 101186764Sjlemon { 101286764Sjlemon rt = tcp_rtlookup(&sc->sc_inc); 101386764Sjlemon if (rt != NULL) 101486764Sjlemon mssopt = rt->rt_ifp->if_mtu - 101586764Sjlemon (sizeof(struct ip) + sizeof(struct tcphdr)); 101686764Sjlemon else 101786764Sjlemon mssopt = tcp_mssdflt; 101886764Sjlemon hlen = sizeof(struct ip); 101986764Sjlemon } 102086764Sjlemon 102186764Sjlemon /* Compute the size of the TCP options. */ 102286764Sjlemon if (sc->sc_flags & SCF_NOOPT) { 102386764Sjlemon optlen = 0; 102486764Sjlemon } else { 102586764Sjlemon optlen = TCPOLEN_MAXSEG + 102686764Sjlemon ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) + 102786764Sjlemon ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) + 102886764Sjlemon ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0); 102986764Sjlemon } 103086764Sjlemon tlen = hlen + sizeof(struct tcphdr) + optlen; 103186764Sjlemon 103286764Sjlemon /* 103386764Sjlemon * XXX 103486764Sjlemon * assume that the entire packet will fit in a header mbuf 103586764Sjlemon */ 103686764Sjlemon KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small")); 103786764Sjlemon 103886764Sjlemon /* 103986764Sjlemon * XXX shouldn't this reuse the mbuf if possible ? 104086764Sjlemon * Create the IP+TCP header from scratch. 104186764Sjlemon */ 104286764Sjlemon if (m) 104386764Sjlemon m_freem(m); 104486764Sjlemon 104586764Sjlemon m = m_gethdr(M_DONTWAIT, MT_HEADER); 104686764Sjlemon if (m == NULL) 104786764Sjlemon return (ENOBUFS); 104886764Sjlemon m->m_data += max_linkhdr; 104986764Sjlemon m->m_len = tlen; 105086764Sjlemon m->m_pkthdr.len = tlen; 105186764Sjlemon m->m_pkthdr.rcvif = NULL; 105286764Sjlemon 105386764Sjlemon#ifdef IPSEC 105486764Sjlemon /* use IPsec policy on listening socket to send SYN,ACK */ 105586764Sjlemon if (ipsec_setsocket(m, sc->sc_tp->t_inpcb->inp_socket) != 0) { 105686764Sjlemon m_freem(m); 105786764Sjlemon return (ENOBUFS); 105886764Sjlemon } 105986764Sjlemon#endif 106086764Sjlemon 106186764Sjlemon#ifdef INET6 106286764Sjlemon if (sc->sc_inc.inc_isipv6) { 106386764Sjlemon ip6 = mtod(m, struct ip6_hdr *); 106486764Sjlemon ip6->ip6_vfc = IPV6_VERSION; 106586764Sjlemon ip6->ip6_nxt = IPPROTO_TCP; 106686764Sjlemon ip6->ip6_src = sc->sc_inc.inc6_laddr; 106786764Sjlemon ip6->ip6_dst = sc->sc_inc.inc6_faddr; 106886764Sjlemon ip6->ip6_plen = htons(tlen - hlen); 106986764Sjlemon /* ip6_hlim is set after checksum */ 107086764Sjlemon /* ip6_flow = ??? */ 107186764Sjlemon 107286764Sjlemon th = (struct tcphdr *)(ip6 + 1); 107386764Sjlemon } else 107486764Sjlemon#endif 107586764Sjlemon { 107686764Sjlemon ip = mtod(m, struct ip *); 107786764Sjlemon ip->ip_v = IPVERSION; 107886764Sjlemon ip->ip_hl = sizeof(struct ip) >> 2; 107986764Sjlemon ip->ip_tos = 0; 108086764Sjlemon ip->ip_len = tlen; 108186764Sjlemon ip->ip_id = 0; 108286764Sjlemon ip->ip_off = 0; 108386764Sjlemon ip->ip_ttl = ip_defttl; 108486764Sjlemon ip->ip_sum = 0; 108586764Sjlemon ip->ip_p = IPPROTO_TCP; 108686764Sjlemon ip->ip_src = sc->sc_inc.inc_laddr; 108786764Sjlemon ip->ip_dst = sc->sc_inc.inc_faddr; 108886764Sjlemon 108986764Sjlemon th = (struct tcphdr *)(ip + 1); 109086764Sjlemon } 109186764Sjlemon th->th_sport = sc->sc_inc.inc_lport; 109286764Sjlemon th->th_dport = sc->sc_inc.inc_fport; 109386764Sjlemon 109486764Sjlemon th->th_seq = htonl(sc->sc_iss); 109586764Sjlemon th->th_ack = htonl(sc->sc_irs + 1); 109686764Sjlemon th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 109786764Sjlemon th->th_x2 = 0; 109886764Sjlemon th->th_flags = TH_SYN|TH_ACK; 109986764Sjlemon th->th_win = htons(sc->sc_wnd); 110086764Sjlemon th->th_urp = 0; 110186764Sjlemon 110286764Sjlemon /* Tack on the TCP options. */ 110386764Sjlemon if (optlen == 0) 110486764Sjlemon goto no_options; 110586764Sjlemon optp = (u_int8_t *)(th + 1); 110686764Sjlemon *optp++ = TCPOPT_MAXSEG; 110786764Sjlemon *optp++ = TCPOLEN_MAXSEG; 110886764Sjlemon *optp++ = (mssopt >> 8) & 0xff; 110986764Sjlemon *optp++ = mssopt & 0xff; 111086764Sjlemon 111186764Sjlemon if (sc->sc_flags & SCF_WINSCALE) { 111286764Sjlemon *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 | 111386764Sjlemon TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 | 111486764Sjlemon sc->sc_request_r_scale); 111586764Sjlemon optp += 4; 111686764Sjlemon } 111786764Sjlemon 111886764Sjlemon if (sc->sc_flags & SCF_TIMESTAMP) { 111986764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 112086764Sjlemon 112186764Sjlemon /* Form timestamp option as shown in appendix A of RFC 1323. */ 112286764Sjlemon *lp++ = htonl(TCPOPT_TSTAMP_HDR); 112386764Sjlemon *lp++ = htonl(ticks); 112486764Sjlemon *lp = htonl(sc->sc_tsrecent); 112586764Sjlemon optp += TCPOLEN_TSTAMP_APPA; 112686764Sjlemon } 112786764Sjlemon 112886764Sjlemon /* 112986764Sjlemon * Send CC and CC.echo if we received CC from our peer. 113086764Sjlemon */ 113186764Sjlemon if (sc->sc_flags & SCF_CC) { 113286764Sjlemon u_int32_t *lp = (u_int32_t *)(optp); 113386764Sjlemon 113486764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC)); 113586764Sjlemon *lp++ = htonl(sc->sc_cc_send); 113686764Sjlemon *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO)); 113786764Sjlemon *lp = htonl(sc->sc_cc_recv); 113886764Sjlemon optp += TCPOLEN_CC_APPA * 2; 113986764Sjlemon } 114086764Sjlemonno_options: 114186764Sjlemon 114286764Sjlemon#ifdef INET6 114386764Sjlemon if (sc->sc_inc.inc_isipv6) { 114486764Sjlemon struct route_in6 *ro6 = &sc->sc_route6; 114586764Sjlemon 114686764Sjlemon th->th_sum = 0; 114786764Sjlemon th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen); 114886764Sjlemon ip6->ip6_hlim = in6_selecthlim(NULL, 114986764Sjlemon ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL); 115086764Sjlemon error = ip6_output(m, NULL, ro6, 0, NULL, NULL); 115186764Sjlemon } else 115286764Sjlemon#endif 115386764Sjlemon { 115486764Sjlemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 115586764Sjlemon htons(tlen - hlen + IPPROTO_TCP)); 115686764Sjlemon m->m_pkthdr.csum_flags = CSUM_TCP; 115786764Sjlemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 115886764Sjlemon error = ip_output(m, sc->sc_ipopts, &sc->sc_route, 0, NULL); 115986764Sjlemon } 116086764Sjlemon return (error); 116186764Sjlemon} 1162