tcp_syncache.c revision 130398
1/*-
2 * Copyright (c) 2001 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Jonathan Lemon
6 * and NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 *    products derived from this software without specific prior written
20 *    permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/netinet/tcp_syncache.c 130398 2004-06-13 02:50:07Z rwatson $
35 */
36
37#include "opt_inet.h"
38#include "opt_inet6.h"
39#include "opt_ipsec.h"
40#include "opt_mac.h"
41#include "opt_tcpdebug.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/malloc.h>
48#include <sys/mac.h>
49#include <sys/mbuf.h>
50#include <sys/md5.h>
51#include <sys/proc.h>		/* for proc0 declaration */
52#include <sys/random.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55
56#include <net/if.h>
57#include <net/route.h>
58
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/ip.h>
62#include <netinet/in_var.h>
63#include <netinet/in_pcb.h>
64#include <netinet/ip_var.h>
65#ifdef INET6
66#include <netinet/ip6.h>
67#include <netinet/icmp6.h>
68#include <netinet6/nd6.h>
69#include <netinet6/ip6_var.h>
70#include <netinet6/in6_pcb.h>
71#endif
72#include <netinet/tcp.h>
73#ifdef TCPDEBUG
74#include <netinet/tcpip.h>
75#endif
76#include <netinet/tcp_fsm.h>
77#include <netinet/tcp_seq.h>
78#include <netinet/tcp_timer.h>
79#include <netinet/tcp_var.h>
80#ifdef TCPDEBUG
81#include <netinet/tcp_debug.h>
82#endif
83#ifdef INET6
84#include <netinet6/tcp6_var.h>
85#endif
86
87#ifdef IPSEC
88#include <netinet6/ipsec.h>
89#ifdef INET6
90#include <netinet6/ipsec6.h>
91#endif
92#endif /*IPSEC*/
93
94#ifdef FAST_IPSEC
95#include <netipsec/ipsec.h>
96#ifdef INET6
97#include <netipsec/ipsec6.h>
98#endif
99#include <netipsec/key.h>
100#endif /*FAST_IPSEC*/
101
102#include <machine/in_cksum.h>
103#include <vm/uma.h>
104
105static int tcp_syncookies = 1;
106SYSCTL_INT(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_RW,
107    &tcp_syncookies, 0,
108    "Use TCP SYN cookies if the syncache overflows");
109
110static void	 syncache_drop(struct syncache *, struct syncache_head *);
111static void	 syncache_free(struct syncache *);
112static void	 syncache_insert(struct syncache *, struct syncache_head *);
113struct syncache *syncache_lookup(struct in_conninfo *, struct syncache_head **);
114#ifdef TCPDEBUG
115static int	 syncache_respond(struct syncache *, struct mbuf *, struct socket *);
116#else
117static int	 syncache_respond(struct syncache *, struct mbuf *);
118#endif
119static struct 	 socket *syncache_socket(struct syncache *, struct socket *,
120		    struct mbuf *m);
121static void	 syncache_timer(void *);
122static u_int32_t syncookie_generate(struct syncache *);
123static struct syncache *syncookie_lookup(struct in_conninfo *,
124		    struct tcphdr *, struct socket *);
125
126/*
127 * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies.
128 * 3 retransmits corresponds to a timeout of (1 + 2 + 4 + 8 == 15) seconds,
129 * the odds are that the user has given up attempting to connect by then.
130 */
131#define SYNCACHE_MAXREXMTS		3
132
133/* Arbitrary values */
134#define TCP_SYNCACHE_HASHSIZE		512
135#define TCP_SYNCACHE_BUCKETLIMIT	30
136
137struct tcp_syncache {
138	struct	syncache_head *hashbase;
139	uma_zone_t zone;
140	u_int	hashsize;
141	u_int	hashmask;
142	u_int	bucket_limit;
143	u_int	cache_count;
144	u_int	cache_limit;
145	u_int	rexmt_limit;
146	u_int	hash_secret;
147	TAILQ_HEAD(, syncache) timerq[SYNCACHE_MAXREXMTS + 1];
148	struct	callout tt_timerq[SYNCACHE_MAXREXMTS + 1];
149};
150static struct tcp_syncache tcp_syncache;
151
152SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache");
153
154SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
155     &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache");
156
157SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
158     &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache");
159
160SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD,
161     &tcp_syncache.cache_count, 0, "Current number of entries in syncache");
162
163SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
164     &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable");
165
166SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW,
167     &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions");
168
169static MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache");
170
171#define SYNCACHE_HASH(inc, mask) 					\
172	((tcp_syncache.hash_secret ^					\
173	  (inc)->inc_faddr.s_addr ^					\
174	  ((inc)->inc_faddr.s_addr >> 16) ^ 				\
175	  (inc)->inc_fport ^ (inc)->inc_lport) & mask)
176
177#define SYNCACHE_HASH6(inc, mask) 					\
178	((tcp_syncache.hash_secret ^					\
179	  (inc)->inc6_faddr.s6_addr32[0] ^ 				\
180	  (inc)->inc6_faddr.s6_addr32[3] ^ 				\
181	  (inc)->inc_fport ^ (inc)->inc_lport) & mask)
182
183#define ENDPTS_EQ(a, b) (						\
184	(a)->ie_fport == (b)->ie_fport &&				\
185	(a)->ie_lport == (b)->ie_lport &&				\
186	(a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr &&			\
187	(a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr			\
188)
189
190#define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0)
191
192#define SYNCACHE_TIMEOUT(sc, slot) do {				\
193	sc->sc_rxtslot = (slot);					\
194	sc->sc_rxttime = ticks + TCPTV_RTOBASE * tcp_backoff[(slot)];	\
195	TAILQ_INSERT_TAIL(&tcp_syncache.timerq[(slot)], sc, sc_timerq);	\
196	if (!callout_active(&tcp_syncache.tt_timerq[(slot)]))		\
197		callout_reset(&tcp_syncache.tt_timerq[(slot)],		\
198		    TCPTV_RTOBASE * tcp_backoff[(slot)],		\
199		    syncache_timer, (void *)((intptr_t)(slot)));	\
200} while (0)
201
202static void
203syncache_free(struct syncache *sc)
204{
205	if (sc->sc_ipopts)
206		(void) m_free(sc->sc_ipopts);
207
208	uma_zfree(tcp_syncache.zone, sc);
209}
210
211void
212syncache_init(void)
213{
214	int i;
215
216	tcp_syncache.cache_count = 0;
217	tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
218	tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT;
219	tcp_syncache.cache_limit =
220	    tcp_syncache.hashsize * tcp_syncache.bucket_limit;
221	tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS;
222	tcp_syncache.hash_secret = arc4random();
223
224        TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize",
225	    &tcp_syncache.hashsize);
226        TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit",
227	    &tcp_syncache.cache_limit);
228        TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit",
229	    &tcp_syncache.bucket_limit);
230	if (!powerof2(tcp_syncache.hashsize)) {
231                printf("WARNING: syncache hash size is not a power of 2.\n");
232		tcp_syncache.hashsize = 512;	/* safe default */
233        }
234	tcp_syncache.hashmask = tcp_syncache.hashsize - 1;
235
236	/* Allocate the hash table. */
237	MALLOC(tcp_syncache.hashbase, struct syncache_head *,
238	    tcp_syncache.hashsize * sizeof(struct syncache_head),
239	    M_SYNCACHE, M_WAITOK);
240
241	/* Initialize the hash buckets. */
242	for (i = 0; i < tcp_syncache.hashsize; i++) {
243		TAILQ_INIT(&tcp_syncache.hashbase[i].sch_bucket);
244		tcp_syncache.hashbase[i].sch_length = 0;
245	}
246
247	/* Initialize the timer queues. */
248	for (i = 0; i <= SYNCACHE_MAXREXMTS; i++) {
249		TAILQ_INIT(&tcp_syncache.timerq[i]);
250		callout_init(&tcp_syncache.tt_timerq[i],
251			debug_mpsafenet ? CALLOUT_MPSAFE : 0);
252	}
253
254	/*
255	 * Allocate the syncache entries.  Allow the zone to allocate one
256	 * more entry than cache limit, so a new entry can bump out an
257	 * older one.
258	 */
259	tcp_syncache.zone = uma_zcreate("syncache", sizeof(struct syncache),
260	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
261	uma_zone_set_max(tcp_syncache.zone, tcp_syncache.cache_limit);
262	tcp_syncache.cache_limit -= 1;
263}
264
265static void
266syncache_insert(sc, sch)
267	struct syncache *sc;
268	struct syncache_head *sch;
269{
270	struct syncache *sc2;
271	int i;
272
273	INP_INFO_WLOCK_ASSERT(&tcbinfo);
274
275	/*
276	 * Make sure that we don't overflow the per-bucket
277	 * limit or the total cache size limit.
278	 */
279	if (sch->sch_length >= tcp_syncache.bucket_limit) {
280		/*
281		 * The bucket is full, toss the oldest element.
282		 */
283		sc2 = TAILQ_FIRST(&sch->sch_bucket);
284		sc2->sc_tp->ts_recent = ticks;
285		syncache_drop(sc2, sch);
286		tcpstat.tcps_sc_bucketoverflow++;
287	} else if (tcp_syncache.cache_count >= tcp_syncache.cache_limit) {
288		/*
289		 * The cache is full.  Toss the oldest entry in the
290		 * entire cache.  This is the front entry in the
291		 * first non-empty timer queue with the largest
292		 * timeout value.
293		 */
294		for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) {
295			sc2 = TAILQ_FIRST(&tcp_syncache.timerq[i]);
296			if (sc2 != NULL)
297				break;
298		}
299		sc2->sc_tp->ts_recent = ticks;
300		syncache_drop(sc2, NULL);
301		tcpstat.tcps_sc_cacheoverflow++;
302	}
303
304	/* Initialize the entry's timer. */
305	SYNCACHE_TIMEOUT(sc, 0);
306
307	/* Put it into the bucket. */
308	TAILQ_INSERT_TAIL(&sch->sch_bucket, sc, sc_hash);
309	sch->sch_length++;
310	tcp_syncache.cache_count++;
311	tcpstat.tcps_sc_added++;
312}
313
314static void
315syncache_drop(sc, sch)
316	struct syncache *sc;
317	struct syncache_head *sch;
318{
319	INP_INFO_WLOCK_ASSERT(&tcbinfo);
320
321	if (sch == NULL) {
322#ifdef INET6
323		if (sc->sc_inc.inc_isipv6) {
324			sch = &tcp_syncache.hashbase[
325			    SYNCACHE_HASH6(&sc->sc_inc, tcp_syncache.hashmask)];
326		} else
327#endif
328		{
329			sch = &tcp_syncache.hashbase[
330			    SYNCACHE_HASH(&sc->sc_inc, tcp_syncache.hashmask)];
331		}
332	}
333
334	TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
335	sch->sch_length--;
336	tcp_syncache.cache_count--;
337
338	TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot], sc, sc_timerq);
339	if (TAILQ_EMPTY(&tcp_syncache.timerq[sc->sc_rxtslot]))
340		callout_stop(&tcp_syncache.tt_timerq[sc->sc_rxtslot]);
341
342	syncache_free(sc);
343}
344
345/*
346 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
347 * If we have retransmitted an entry the maximum number of times, expire it.
348 */
349static void
350syncache_timer(xslot)
351	void *xslot;
352{
353	intptr_t slot = (intptr_t)xslot;
354	struct syncache *sc, *nsc;
355	struct inpcb *inp;
356
357	INP_INFO_WLOCK(&tcbinfo);
358        if (callout_pending(&tcp_syncache.tt_timerq[slot]) ||
359            !callout_active(&tcp_syncache.tt_timerq[slot])) {
360		/* XXX can this happen? */
361		INP_INFO_WUNLOCK(&tcbinfo);
362                return;
363        }
364        callout_deactivate(&tcp_syncache.tt_timerq[slot]);
365
366        nsc = TAILQ_FIRST(&tcp_syncache.timerq[slot]);
367	while (nsc != NULL) {
368		if (ticks < nsc->sc_rxttime)
369			break;
370		sc = nsc;
371		inp = sc->sc_tp->t_inpcb;
372		if (slot == SYNCACHE_MAXREXMTS ||
373		    slot >= tcp_syncache.rexmt_limit ||
374		    inp == NULL || inp->inp_gencnt != sc->sc_inp_gencnt) {
375			nsc = TAILQ_NEXT(sc, sc_timerq);
376			syncache_drop(sc, NULL);
377			tcpstat.tcps_sc_stale++;
378			continue;
379		}
380		/*
381		 * syncache_respond() may call back into the syncache to
382		 * to modify another entry, so do not obtain the next
383		 * entry on the timer chain until it has completed.
384		 */
385#ifdef TCPDEBUG
386		(void) syncache_respond(sc, NULL, NULL);
387#else
388		(void) syncache_respond(sc, NULL);
389#endif
390		nsc = TAILQ_NEXT(sc, sc_timerq);
391		tcpstat.tcps_sc_retransmitted++;
392		TAILQ_REMOVE(&tcp_syncache.timerq[slot], sc, sc_timerq);
393		SYNCACHE_TIMEOUT(sc, slot + 1);
394	}
395	if (nsc != NULL)
396		callout_reset(&tcp_syncache.tt_timerq[slot],
397		    nsc->sc_rxttime - ticks, syncache_timer, (void *)(slot));
398	INP_INFO_WUNLOCK(&tcbinfo);
399}
400
401/*
402 * Find an entry in the syncache.
403 */
404struct syncache *
405syncache_lookup(inc, schp)
406	struct in_conninfo *inc;
407	struct syncache_head **schp;
408{
409	struct syncache *sc;
410	struct syncache_head *sch;
411
412	INP_INFO_WLOCK_ASSERT(&tcbinfo);
413
414#ifdef INET6
415	if (inc->inc_isipv6) {
416		sch = &tcp_syncache.hashbase[
417		    SYNCACHE_HASH6(inc, tcp_syncache.hashmask)];
418		*schp = sch;
419		TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
420			if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
421				return (sc);
422		}
423	} else
424#endif
425	{
426		sch = &tcp_syncache.hashbase[
427		    SYNCACHE_HASH(inc, tcp_syncache.hashmask)];
428		*schp = sch;
429		TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
430#ifdef INET6
431			if (sc->sc_inc.inc_isipv6)
432				continue;
433#endif
434			if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
435				return (sc);
436		}
437	}
438	return (NULL);
439}
440
441/*
442 * This function is called when we get a RST for a
443 * non-existent connection, so that we can see if the
444 * connection is in the syn cache.  If it is, zap it.
445 */
446void
447syncache_chkrst(inc, th)
448	struct in_conninfo *inc;
449	struct tcphdr *th;
450{
451	struct syncache *sc;
452	struct syncache_head *sch;
453
454	INP_INFO_WLOCK_ASSERT(&tcbinfo);
455
456	sc = syncache_lookup(inc, &sch);
457	if (sc == NULL)
458		return;
459	/*
460	 * If the RST bit is set, check the sequence number to see
461	 * if this is a valid reset segment.
462	 * RFC 793 page 37:
463	 *   In all states except SYN-SENT, all reset (RST) segments
464	 *   are validated by checking their SEQ-fields.  A reset is
465	 *   valid if its sequence number is in the window.
466	 *
467	 *   The sequence number in the reset segment is normally an
468	 *   echo of our outgoing acknowlegement numbers, but some hosts
469	 *   send a reset with the sequence number at the rightmost edge
470	 *   of our receive window, and we have to handle this case.
471	 */
472	if (SEQ_GEQ(th->th_seq, sc->sc_irs) &&
473	    SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) {
474		syncache_drop(sc, sch);
475		tcpstat.tcps_sc_reset++;
476	}
477}
478
479void
480syncache_badack(inc)
481	struct in_conninfo *inc;
482{
483	struct syncache *sc;
484	struct syncache_head *sch;
485
486	INP_INFO_WLOCK_ASSERT(&tcbinfo);
487
488	sc = syncache_lookup(inc, &sch);
489	if (sc != NULL) {
490		syncache_drop(sc, sch);
491		tcpstat.tcps_sc_badack++;
492	}
493}
494
495void
496syncache_unreach(inc, th)
497	struct in_conninfo *inc;
498	struct tcphdr *th;
499{
500	struct syncache *sc;
501	struct syncache_head *sch;
502
503	INP_INFO_WLOCK_ASSERT(&tcbinfo);
504
505	/* we are called at splnet() here */
506	sc = syncache_lookup(inc, &sch);
507	if (sc == NULL)
508		return;
509
510	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
511	if (ntohl(th->th_seq) != sc->sc_iss)
512		return;
513
514	/*
515	 * If we've rertransmitted 3 times and this is our second error,
516	 * we remove the entry.  Otherwise, we allow it to continue on.
517	 * This prevents us from incorrectly nuking an entry during a
518	 * spurious network outage.
519	 *
520	 * See tcp_notify().
521	 */
522	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtslot < 3) {
523		sc->sc_flags |= SCF_UNREACH;
524		return;
525	}
526	syncache_drop(sc, sch);
527	tcpstat.tcps_sc_unreach++;
528}
529
530/*
531 * Build a new TCP socket structure from a syncache entry.
532 */
533static struct socket *
534syncache_socket(sc, lso, m)
535	struct syncache *sc;
536	struct socket *lso;
537	struct mbuf *m;
538{
539	struct inpcb *inp = NULL;
540	struct socket *so;
541	struct tcpcb *tp;
542
543	GIANT_REQUIRED;			/* XXX until socket locking */
544	INP_INFO_WLOCK_ASSERT(&tcbinfo);
545
546	/*
547	 * Ok, create the full blown connection, and set things up
548	 * as they would have been set up if we had created the
549	 * connection when the SYN arrived.  If we can't create
550	 * the connection, abort it.
551	 */
552	so = sonewconn(lso, SS_ISCONNECTED);
553	if (so == NULL) {
554		/*
555		 * Drop the connection; we will send a RST if the peer
556		 * retransmits the ACK,
557		 */
558		tcpstat.tcps_listendrop++;
559		goto abort2;
560	}
561#ifdef MAC
562	SOCK_LOCK(so);
563	mac_set_socket_peer_from_mbuf(m, so);
564	SOCK_UNLOCK(so);
565#endif
566
567	inp = sotoinpcb(so);
568	INP_LOCK(inp);
569
570	/*
571	 * Insert new socket into hash list.
572	 */
573	inp->inp_inc.inc_isipv6 = sc->sc_inc.inc_isipv6;
574#ifdef INET6
575	if (sc->sc_inc.inc_isipv6) {
576		inp->in6p_laddr = sc->sc_inc.inc6_laddr;
577	} else {
578		inp->inp_vflag &= ~INP_IPV6;
579		inp->inp_vflag |= INP_IPV4;
580#endif
581		inp->inp_laddr = sc->sc_inc.inc_laddr;
582#ifdef INET6
583	}
584#endif
585	inp->inp_lport = sc->sc_inc.inc_lport;
586	if (in_pcbinshash(inp) != 0) {
587		/*
588		 * Undo the assignments above if we failed to
589		 * put the PCB on the hash lists.
590		 */
591#ifdef INET6
592		if (sc->sc_inc.inc_isipv6)
593			inp->in6p_laddr = in6addr_any;
594       		else
595#endif
596			inp->inp_laddr.s_addr = INADDR_ANY;
597		inp->inp_lport = 0;
598		goto abort;
599	}
600#ifdef IPSEC
601	/* copy old policy into new socket's */
602	if (ipsec_copy_pcbpolicy(sotoinpcb(lso)->inp_sp, inp->inp_sp))
603		printf("syncache_expand: could not copy policy\n");
604#endif
605#ifdef FAST_IPSEC
606	/* copy old policy into new socket's */
607	if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp))
608		printf("syncache_expand: could not copy policy\n");
609#endif
610#ifdef INET6
611	if (sc->sc_inc.inc_isipv6) {
612		struct inpcb *oinp = sotoinpcb(lso);
613		struct in6_addr laddr6;
614		struct sockaddr_in6 sin6;
615		/*
616		 * Inherit socket options from the listening socket.
617		 * Note that in6p_inputopts are not (and should not be)
618		 * copied, since it stores previously received options and is
619		 * used to detect if each new option is different than the
620		 * previous one and hence should be passed to a user.
621                 * If we copied in6p_inputopts, a user would not be able to
622		 * receive options just after calling the accept system call.
623		 */
624		inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS;
625		if (oinp->in6p_outputopts)
626			inp->in6p_outputopts =
627			    ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT);
628
629		sin6.sin6_family = AF_INET6;
630		sin6.sin6_len = sizeof(sin6);
631		sin6.sin6_addr = sc->sc_inc.inc6_faddr;
632		sin6.sin6_port = sc->sc_inc.inc_fport;
633		sin6.sin6_flowinfo = sin6.sin6_scope_id = 0;
634		laddr6 = inp->in6p_laddr;
635		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
636			inp->in6p_laddr = sc->sc_inc.inc6_laddr;
637		if (in6_pcbconnect(inp, (struct sockaddr *)&sin6,
638		    thread0.td_ucred)) {
639			inp->in6p_laddr = laddr6;
640			goto abort;
641		}
642	} else
643#endif
644	{
645		struct in_addr laddr;
646		struct sockaddr_in sin;
647
648		inp->inp_options = ip_srcroute();
649		if (inp->inp_options == NULL) {
650			inp->inp_options = sc->sc_ipopts;
651			sc->sc_ipopts = NULL;
652		}
653
654		sin.sin_family = AF_INET;
655		sin.sin_len = sizeof(sin);
656		sin.sin_addr = sc->sc_inc.inc_faddr;
657		sin.sin_port = sc->sc_inc.inc_fport;
658		bzero((caddr_t)sin.sin_zero, sizeof(sin.sin_zero));
659		laddr = inp->inp_laddr;
660		if (inp->inp_laddr.s_addr == INADDR_ANY)
661			inp->inp_laddr = sc->sc_inc.inc_laddr;
662		if (in_pcbconnect(inp, (struct sockaddr *)&sin,
663		    thread0.td_ucred)) {
664			inp->inp_laddr = laddr;
665			goto abort;
666		}
667	}
668
669	tp = intotcpcb(inp);
670	tp->t_state = TCPS_SYN_RECEIVED;
671	tp->iss = sc->sc_iss;
672	tp->irs = sc->sc_irs;
673	tcp_rcvseqinit(tp);
674	tcp_sendseqinit(tp);
675	tp->snd_wl1 = sc->sc_irs;
676	tp->rcv_up = sc->sc_irs + 1;
677	tp->rcv_wnd = sc->sc_wnd;
678	tp->rcv_adv += tp->rcv_wnd;
679
680	tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH|TF_NODELAY);
681	if (sc->sc_flags & SCF_NOOPT)
682		tp->t_flags |= TF_NOOPT;
683	if (sc->sc_flags & SCF_WINSCALE) {
684		tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
685		tp->requested_s_scale = sc->sc_requested_s_scale;
686		tp->request_r_scale = sc->sc_request_r_scale;
687	}
688	if (sc->sc_flags & SCF_TIMESTAMP) {
689		tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
690		tp->ts_recent = sc->sc_tsrecent;
691		tp->ts_recent_age = ticks;
692	}
693	if (sc->sc_flags & SCF_CC) {
694		/*
695		 * Initialization of the tcpcb for transaction;
696		 *   set SND.WND = SEG.WND,
697		 *   initialize CCsend and CCrecv.
698		 */
699		tp->t_flags |= TF_REQ_CC|TF_RCVD_CC;
700		tp->cc_send = sc->sc_cc_send;
701		tp->cc_recv = sc->sc_cc_recv;
702	}
703#ifdef TCP_SIGNATURE
704	if (sc->sc_flags & SCF_SIGNATURE)
705		tp->t_flags |= TF_SIGNATURE;
706#endif
707
708	/*
709	 * Set up MSS and get cached values from tcp_hostcache.
710	 * This might overwrite some of the defaults we just set.
711	 */
712	tcp_mss(tp, sc->sc_peer_mss);
713
714	/*
715	 * If the SYN,ACK was retransmitted, reset cwnd to 1 segment.
716	 */
717	if (sc->sc_rxtslot != 0)
718                tp->snd_cwnd = tp->t_maxseg;
719	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
720
721	INP_UNLOCK(inp);
722
723	tcpstat.tcps_accepts++;
724	return (so);
725
726abort:
727	INP_UNLOCK(inp);
728abort2:
729	if (so != NULL)
730		(void) soabort(so);
731	return (NULL);
732}
733
734/*
735 * This function gets called when we receive an ACK for a
736 * socket in the LISTEN state.  We look up the connection
737 * in the syncache, and if its there, we pull it out of
738 * the cache and turn it into a full-blown connection in
739 * the SYN-RECEIVED state.
740 */
741int
742syncache_expand(inc, th, sop, m)
743	struct in_conninfo *inc;
744	struct tcphdr *th;
745	struct socket **sop;
746	struct mbuf *m;
747{
748	struct syncache *sc;
749	struct syncache_head *sch;
750	struct socket *so;
751
752	INP_INFO_WLOCK_ASSERT(&tcbinfo);
753
754	sc = syncache_lookup(inc, &sch);
755	if (sc == NULL) {
756		/*
757		 * There is no syncache entry, so see if this ACK is
758		 * a returning syncookie.  To do this, first:
759		 *  A. See if this socket has had a syncache entry dropped in
760		 *     the past.  We don't want to accept a bogus syncookie
761 		 *     if we've never received a SYN.
762		 *  B. check that the syncookie is valid.  If it is, then
763		 *     cobble up a fake syncache entry, and return.
764		 */
765		if (!tcp_syncookies)
766			return (0);
767		sc = syncookie_lookup(inc, th, *sop);
768		if (sc == NULL)
769			return (0);
770		sch = NULL;
771		tcpstat.tcps_sc_recvcookie++;
772	}
773
774	/*
775	 * If seg contains an ACK, but not for our SYN/ACK, send a RST.
776	 */
777	if (th->th_ack != sc->sc_iss + 1)
778		return (0);
779
780	so = syncache_socket(sc, *sop, m);
781	if (so == NULL) {
782#if 0
783resetandabort:
784		/* XXXjlemon check this - is this correct? */
785		(void) tcp_respond(NULL, m, m, th,
786		    th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK);
787#endif
788		m_freem(m);			/* XXX only needed for above */
789		tcpstat.tcps_sc_aborted++;
790	} else
791		tcpstat.tcps_sc_completed++;
792
793	if (sch == NULL)
794		syncache_free(sc);
795	else
796		syncache_drop(sc, sch);
797	*sop = so;
798	return (1);
799}
800
801/*
802 * Given a LISTEN socket and an inbound SYN request, add
803 * this to the syn cache, and send back a segment:
804 *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
805 * to the source.
806 *
807 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
808 * Doing so would require that we hold onto the data and deliver it
809 * to the application.  However, if we are the target of a SYN-flood
810 * DoS attack, an attacker could send data which would eventually
811 * consume all available buffer space if it were ACKed.  By not ACKing
812 * the data, we avoid this DoS scenario.
813 */
814int
815syncache_add(inc, to, th, sop, m)
816	struct in_conninfo *inc;
817	struct tcpopt *to;
818	struct tcphdr *th;
819	struct socket **sop;
820	struct mbuf *m;
821{
822	struct tcpcb *tp;
823	struct socket *so;
824	struct syncache *sc = NULL;
825	struct syncache_head *sch;
826	struct mbuf *ipopts = NULL;
827	struct rmxp_tao tao;
828	int i, win;
829
830	INP_INFO_WLOCK_ASSERT(&tcbinfo);
831
832	so = *sop;
833	tp = sototcpcb(so);
834	bzero(&tao, sizeof(tao));
835
836	/*
837	 * Remember the IP options, if any.
838	 */
839#ifdef INET6
840	if (!inc->inc_isipv6)
841#endif
842		ipopts = ip_srcroute();
843
844	/*
845	 * See if we already have an entry for this connection.
846	 * If we do, resend the SYN,ACK, and reset the retransmit timer.
847	 *
848	 * XXX
849	 * should the syncache be re-initialized with the contents
850	 * of the new SYN here (which may have different options?)
851	 */
852	sc = syncache_lookup(inc, &sch);
853	if (sc != NULL) {
854		tcpstat.tcps_sc_dupsyn++;
855		if (ipopts) {
856			/*
857			 * If we were remembering a previous source route,
858			 * forget it and use the new one we've been given.
859			 */
860			if (sc->sc_ipopts)
861				(void) m_free(sc->sc_ipopts);
862			sc->sc_ipopts = ipopts;
863		}
864		/*
865		 * Update timestamp if present.
866		 */
867		if (sc->sc_flags & SCF_TIMESTAMP)
868			sc->sc_tsrecent = to->to_tsval;
869		/*
870		 * PCB may have changed, pick up new values.
871		 */
872		sc->sc_tp = tp;
873		sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt;
874#ifdef TCPDEBUG
875		if (syncache_respond(sc, m, so) == 0) {
876#else
877		if (syncache_respond(sc, m) == 0) {
878#endif
879			/* NB: guarded by INP_INFO_WLOCK(&tcbinfo) */
880			TAILQ_REMOVE(&tcp_syncache.timerq[sc->sc_rxtslot],
881			    sc, sc_timerq);
882			SYNCACHE_TIMEOUT(sc, sc->sc_rxtslot);
883		 	tcpstat.tcps_sndacks++;
884			tcpstat.tcps_sndtotal++;
885		}
886		*sop = NULL;
887		return (1);
888	}
889
890	sc = uma_zalloc(tcp_syncache.zone, M_NOWAIT);
891	if (sc == NULL) {
892		/*
893		 * The zone allocator couldn't provide more entries.
894		 * Treat this as if the cache was full; drop the oldest
895		 * entry and insert the new one.
896		 */
897		/* NB: guarded by INP_INFO_WLOCK(&tcbinfo) */
898		for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) {
899			sc = TAILQ_FIRST(&tcp_syncache.timerq[i]);
900			if (sc != NULL)
901				break;
902		}
903		sc->sc_tp->ts_recent = ticks;
904		syncache_drop(sc, NULL);
905		tcpstat.tcps_sc_zonefail++;
906		sc = uma_zalloc(tcp_syncache.zone, M_NOWAIT);
907		if (sc == NULL) {
908			if (ipopts)
909				(void) m_free(ipopts);
910			return (0);
911		}
912	}
913
914	/*
915	 * Fill in the syncache values.
916	 */
917	bzero(sc, sizeof(*sc));
918	sc->sc_tp = tp;
919	sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt;
920	sc->sc_ipopts = ipopts;
921	sc->sc_inc.inc_fport = inc->inc_fport;
922	sc->sc_inc.inc_lport = inc->inc_lport;
923#ifdef INET6
924	sc->sc_inc.inc_isipv6 = inc->inc_isipv6;
925	if (inc->inc_isipv6) {
926		sc->sc_inc.inc6_faddr = inc->inc6_faddr;
927		sc->sc_inc.inc6_laddr = inc->inc6_laddr;
928	} else
929#endif
930	{
931		sc->sc_inc.inc_faddr = inc->inc_faddr;
932		sc->sc_inc.inc_laddr = inc->inc_laddr;
933	}
934	sc->sc_irs = th->th_seq;
935	sc->sc_flags = 0;
936	sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0;
937	if (tcp_syncookies)
938		sc->sc_iss = syncookie_generate(sc);
939	else
940		sc->sc_iss = arc4random();
941
942	/* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */
943	win = sbspace(&so->so_rcv);
944	win = imax(win, 0);
945	win = imin(win, TCP_MAXWIN);
946	sc->sc_wnd = win;
947
948	if (tcp_do_rfc1323) {
949		/*
950		 * A timestamp received in a SYN makes
951		 * it ok to send timestamp requests and replies.
952		 */
953		if (to->to_flags & TOF_TS) {
954			sc->sc_tsrecent = to->to_tsval;
955			sc->sc_flags |= SCF_TIMESTAMP;
956		}
957		if (to->to_flags & TOF_SCALE) {
958			int wscale = 0;
959
960			/* Compute proper scaling value from buffer space */
961			while (wscale < TCP_MAX_WINSHIFT &&
962			    (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat)
963				wscale++;
964			sc->sc_request_r_scale = wscale;
965			sc->sc_requested_s_scale = to->to_requested_s_scale;
966			sc->sc_flags |= SCF_WINSCALE;
967		}
968	}
969	if (tcp_do_rfc1644) {
970		/*
971		 * A CC or CC.new option received in a SYN makes
972		 * it ok to send CC in subsequent segments.
973		 */
974		if (to->to_flags & (TOF_CC|TOF_CCNEW)) {
975			sc->sc_cc_recv = to->to_cc;
976			sc->sc_cc_send = CC_INC(tcp_ccgen);
977			sc->sc_flags |= SCF_CC;
978		}
979	}
980	if (tp->t_flags & TF_NOOPT)
981		sc->sc_flags = SCF_NOOPT;
982#ifdef TCP_SIGNATURE
983	/*
984	 * If listening socket requested TCP digests, and received SYN
985	 * contains the option, flag this in the syncache so that
986	 * syncache_respond() will do the right thing with the SYN+ACK.
987	 * XXX Currently we always record the option by default and will
988	 * attempt to use it in syncache_respond().
989	 */
990	if (to->to_flags & TOF_SIGNATURE)
991		sc->sc_flags = SCF_SIGNATURE;
992#endif
993
994	/*
995	 * XXX
996	 * We have the option here of not doing TAO (even if the segment
997	 * qualifies) and instead fall back to a normal 3WHS via the syncache.
998	 * This allows us to apply synflood protection to TAO-qualifying SYNs
999	 * also. However, there should be a hueristic to determine when to
1000	 * do this, and is not present at the moment.
1001	 */
1002
1003	/*
1004	 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1005	 * - compare SEG.CC against cached CC from the same host, if any.
1006	 * - if SEG.CC > chached value, SYN must be new and is accepted
1007	 *	immediately: save new CC in the cache, mark the socket
1008	 *	connected, enter ESTABLISHED state, turn on flag to
1009	 *	send a SYN in the next segment.
1010	 *	A virtual advertised window is set in rcv_adv to
1011	 *	initialize SWS prevention.  Then enter normal segment
1012	 *	processing: drop SYN, process data and FIN.
1013	 * - otherwise do a normal 3-way handshake.
1014	 */
1015	if (tcp_do_rfc1644)
1016		tcp_hc_gettao(&sc->sc_inc, &tao);
1017
1018	if ((to->to_flags & TOF_CC) != 0) {
1019		if (((tp->t_flags & TF_NOPUSH) != 0) &&
1020		    sc->sc_flags & SCF_CC && tao.tao_cc != 0 &&
1021		    CC_GT(to->to_cc, tao.tao_cc)) {
1022			sc->sc_rxtslot = 0;
1023			so = syncache_socket(sc, *sop, m);
1024			if (so != NULL) {
1025				tao.tao_cc = to->to_cc;
1026				tcp_hc_updatetao(&sc->sc_inc, TCP_HC_TAO_CC,
1027						 tao.tao_cc, 0);
1028				*sop = so;
1029			}
1030			syncache_free(sc);
1031			return (so != NULL);
1032		}
1033	} else {
1034		/*
1035		 * No CC option, but maybe CC.NEW: invalidate cached value.
1036		 */
1037		if (tcp_do_rfc1644) {
1038			tao.tao_cc = 0;
1039			tcp_hc_updatetao(&sc->sc_inc, TCP_HC_TAO_CC,
1040					 tao.tao_cc, 0);
1041		}
1042	}
1043
1044	/*
1045	 * TAO test failed or there was no CC option,
1046	 *    do a standard 3-way handshake.
1047	 */
1048#ifdef TCPDEBUG
1049	if (syncache_respond(sc, m, so) == 0) {
1050#else
1051	if (syncache_respond(sc, m) == 0) {
1052#endif
1053		syncache_insert(sc, sch);
1054		tcpstat.tcps_sndacks++;
1055		tcpstat.tcps_sndtotal++;
1056	} else {
1057		syncache_free(sc);
1058		tcpstat.tcps_sc_dropped++;
1059	}
1060	*sop = NULL;
1061	return (1);
1062}
1063
1064#ifdef TCPDEBUG
1065static int
1066syncache_respond(sc, m, so)
1067	struct syncache *sc;
1068	struct mbuf *m;
1069	struct socket *so;
1070#else
1071static int
1072syncache_respond(sc, m)
1073	struct syncache *sc;
1074	struct mbuf *m;
1075#endif
1076{
1077	u_int8_t *optp;
1078	int optlen, error;
1079	u_int16_t tlen, hlen, mssopt;
1080	struct ip *ip = NULL;
1081	struct tcphdr *th;
1082	struct inpcb *inp;
1083#ifdef INET6
1084	struct ip6_hdr *ip6 = NULL;
1085#endif
1086
1087	hlen =
1088#ifdef INET6
1089	       (sc->sc_inc.inc_isipv6) ? sizeof(struct ip6_hdr) :
1090#endif
1091		sizeof(struct ip);
1092
1093	KASSERT((&sc->sc_inc) != NULL, ("syncache_respond with NULL in_conninfo pointer"));
1094
1095	/* Determine MSS we advertize to other end of connection */
1096	mssopt = tcp_mssopt(&sc->sc_inc);
1097
1098	/* Compute the size of the TCP options. */
1099	if (sc->sc_flags & SCF_NOOPT) {
1100		optlen = 0;
1101	} else {
1102		optlen = TCPOLEN_MAXSEG +
1103		    ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) +
1104		    ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) +
1105		    ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0);
1106#ifdef TCP_SIGNATURE
1107		optlen += (sc->sc_flags & SCF_SIGNATURE) ?
1108		    TCPOLEN_SIGNATURE + 2 : 0;
1109#endif
1110	}
1111	tlen = hlen + sizeof(struct tcphdr) + optlen;
1112
1113	/*
1114	 * XXX
1115	 * assume that the entire packet will fit in a header mbuf
1116	 */
1117	KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small"));
1118
1119	/*
1120	 * XXX shouldn't this reuse the mbuf if possible ?
1121	 * Create the IP+TCP header from scratch.
1122	 */
1123	if (m)
1124		m_freem(m);
1125
1126	m = m_gethdr(M_DONTWAIT, MT_HEADER);
1127	if (m == NULL)
1128		return (ENOBUFS);
1129	m->m_data += max_linkhdr;
1130	m->m_len = tlen;
1131	m->m_pkthdr.len = tlen;
1132	m->m_pkthdr.rcvif = NULL;
1133	inp = sc->sc_tp->t_inpcb;
1134	INP_LOCK(inp);
1135#ifdef MAC
1136	mac_create_mbuf_from_inpcb(inp, m);
1137#endif
1138
1139#ifdef INET6
1140	if (sc->sc_inc.inc_isipv6) {
1141		ip6 = mtod(m, struct ip6_hdr *);
1142		ip6->ip6_vfc = IPV6_VERSION;
1143		ip6->ip6_nxt = IPPROTO_TCP;
1144		ip6->ip6_src = sc->sc_inc.inc6_laddr;
1145		ip6->ip6_dst = sc->sc_inc.inc6_faddr;
1146		ip6->ip6_plen = htons(tlen - hlen);
1147		/* ip6_hlim is set after checksum */
1148		/* ip6_flow = ??? */
1149
1150		th = (struct tcphdr *)(ip6 + 1);
1151	} else
1152#endif
1153	{
1154		ip = mtod(m, struct ip *);
1155		ip->ip_v = IPVERSION;
1156		ip->ip_hl = sizeof(struct ip) >> 2;
1157		ip->ip_len = tlen;
1158		ip->ip_id = 0;
1159		ip->ip_off = 0;
1160		ip->ip_sum = 0;
1161		ip->ip_p = IPPROTO_TCP;
1162		ip->ip_src = sc->sc_inc.inc_laddr;
1163		ip->ip_dst = sc->sc_inc.inc_faddr;
1164		ip->ip_ttl = inp->inp_ip_ttl;   /* XXX */
1165		ip->ip_tos = inp->inp_ip_tos;   /* XXX */
1166
1167		/*
1168		 * See if we should do MTU discovery.  Route lookups are
1169		 * expensive, so we will only unset the DF bit if:
1170		 *
1171		 *	1) path_mtu_discovery is disabled
1172		 *	2) the SCF_UNREACH flag has been set
1173		 */
1174		if (path_mtu_discovery && ((sc->sc_flags & SCF_UNREACH) == 0))
1175		       ip->ip_off |= IP_DF;
1176
1177		th = (struct tcphdr *)(ip + 1);
1178	}
1179	th->th_sport = sc->sc_inc.inc_lport;
1180	th->th_dport = sc->sc_inc.inc_fport;
1181
1182	th->th_seq = htonl(sc->sc_iss);
1183	th->th_ack = htonl(sc->sc_irs + 1);
1184	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
1185	th->th_x2 = 0;
1186	th->th_flags = TH_SYN|TH_ACK;
1187	th->th_win = htons(sc->sc_wnd);
1188	th->th_urp = 0;
1189
1190	/* Tack on the TCP options. */
1191	if (optlen != 0) {
1192		optp = (u_int8_t *)(th + 1);
1193		*optp++ = TCPOPT_MAXSEG;
1194		*optp++ = TCPOLEN_MAXSEG;
1195		*optp++ = (mssopt >> 8) & 0xff;
1196		*optp++ = mssopt & 0xff;
1197
1198		if (sc->sc_flags & SCF_WINSCALE) {
1199			*((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
1200			    TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
1201			    sc->sc_request_r_scale);
1202			optp += 4;
1203		}
1204
1205		if (sc->sc_flags & SCF_TIMESTAMP) {
1206			u_int32_t *lp = (u_int32_t *)(optp);
1207
1208			/* Form timestamp option per appendix A of RFC 1323. */
1209			*lp++ = htonl(TCPOPT_TSTAMP_HDR);
1210			*lp++ = htonl(ticks);
1211			*lp   = htonl(sc->sc_tsrecent);
1212			optp += TCPOLEN_TSTAMP_APPA;
1213		}
1214
1215		/*
1216		 * Send CC and CC.echo if we received CC from our peer.
1217		 */
1218		if (sc->sc_flags & SCF_CC) {
1219			u_int32_t *lp = (u_int32_t *)(optp);
1220
1221			*lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC));
1222			*lp++ = htonl(sc->sc_cc_send);
1223			*lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO));
1224			*lp   = htonl(sc->sc_cc_recv);
1225			optp += TCPOLEN_CC_APPA * 2;
1226		}
1227
1228#ifdef TCP_SIGNATURE
1229		/*
1230		 * Handle TCP-MD5 passive opener response.
1231		 */
1232		if (sc->sc_flags & SCF_SIGNATURE) {
1233			u_int8_t *bp = optp;
1234			int i;
1235
1236			*bp++ = TCPOPT_SIGNATURE;
1237			*bp++ = TCPOLEN_SIGNATURE;
1238			for (i = 0; i < TCP_SIGLEN; i++)
1239				*bp++ = 0;
1240			tcp_signature_compute(m, sizeof(struct ip), 0, optlen,
1241			    optp + 2, IPSEC_DIR_OUTBOUND);
1242			*bp++ = TCPOPT_NOP;
1243			*bp++ = TCPOPT_EOL;
1244			optp += TCPOLEN_SIGNATURE + 2;
1245		}
1246#endif /* TCP_SIGNATURE */
1247	}
1248
1249#ifdef INET6
1250	if (sc->sc_inc.inc_isipv6) {
1251		th->th_sum = 0;
1252		th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
1253		ip6->ip6_hlim = in6_selecthlim(NULL, NULL);
1254		error = ip6_output(m, NULL, NULL, 0, NULL, NULL, inp);
1255	} else
1256#endif
1257	{
1258        	th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1259		    htons(tlen - hlen + IPPROTO_TCP));
1260		m->m_pkthdr.csum_flags = CSUM_TCP;
1261		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1262#ifdef TCPDEBUG
1263		/*
1264		 * Trace.
1265		 */
1266		if (so != NULL && so->so_options & SO_DEBUG) {
1267			struct tcpcb *tp = sototcpcb(so);
1268			tcp_trace(TA_OUTPUT, tp->t_state, tp,
1269			    mtod(m, void *), th, 0);
1270		}
1271#endif
1272		error = ip_output(m, sc->sc_ipopts, NULL, 0, NULL, inp);
1273	}
1274	INP_UNLOCK(inp);
1275	return (error);
1276}
1277
1278/*
1279 * cookie layers:
1280 *
1281 *	|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .|
1282 *	| peer iss                                                      |
1283 *	| MD5(laddr,faddr,secret,lport,fport)             |. . . . . . .|
1284 *	|                     0                       |(A)|             |
1285 * (A): peer mss index
1286 */
1287
1288/*
1289 * The values below are chosen to minimize the size of the tcp_secret
1290 * table, as well as providing roughly a 16 second lifetime for the cookie.
1291 */
1292
1293#define SYNCOOKIE_WNDBITS	5	/* exposed bits for window indexing */
1294#define SYNCOOKIE_TIMESHIFT	1	/* scale ticks to window time units */
1295
1296#define SYNCOOKIE_WNDMASK	((1 << SYNCOOKIE_WNDBITS) - 1)
1297#define SYNCOOKIE_NSECRETS	(1 << SYNCOOKIE_WNDBITS)
1298#define SYNCOOKIE_TIMEOUT \
1299    (hz * (1 << SYNCOOKIE_WNDBITS) / (1 << SYNCOOKIE_TIMESHIFT))
1300#define SYNCOOKIE_DATAMASK 	((3 << SYNCOOKIE_WNDBITS) | SYNCOOKIE_WNDMASK)
1301
1302static struct {
1303	u_int32_t	ts_secbits[4];
1304	u_int		ts_expire;
1305} tcp_secret[SYNCOOKIE_NSECRETS];
1306
1307static int tcp_msstab[] = { 0, 536, 1460, 8960 };
1308
1309static MD5_CTX syn_ctx;
1310
1311#define MD5Add(v)	MD5Update(&syn_ctx, (u_char *)&v, sizeof(v))
1312
1313struct md5_add {
1314	u_int32_t laddr, faddr;
1315	u_int32_t secbits[4];
1316	u_int16_t lport, fport;
1317};
1318
1319#ifdef CTASSERT
1320CTASSERT(sizeof(struct md5_add) == 28);
1321#endif
1322
1323/*
1324 * Consider the problem of a recreated (and retransmitted) cookie.  If the
1325 * original SYN was accepted, the connection is established.  The second
1326 * SYN is inflight, and if it arrives with an ISN that falls within the
1327 * receive window, the connection is killed.
1328 *
1329 * However, since cookies have other problems, this may not be worth
1330 * worrying about.
1331 */
1332
1333static u_int32_t
1334syncookie_generate(struct syncache *sc)
1335{
1336	u_int32_t md5_buffer[4];
1337	u_int32_t data;
1338	int idx, i;
1339	struct md5_add add;
1340
1341	/* NB: single threaded; could add INP_INFO_WLOCK_ASSERT(&tcbinfo) */
1342
1343	idx = ((ticks << SYNCOOKIE_TIMESHIFT) / hz) & SYNCOOKIE_WNDMASK;
1344	if (tcp_secret[idx].ts_expire < ticks) {
1345		for (i = 0; i < 4; i++)
1346			tcp_secret[idx].ts_secbits[i] = arc4random();
1347		tcp_secret[idx].ts_expire = ticks + SYNCOOKIE_TIMEOUT;
1348	}
1349	for (data = sizeof(tcp_msstab) / sizeof(int) - 1; data > 0; data--)
1350		if (tcp_msstab[data] <= sc->sc_peer_mss)
1351			break;
1352	data = (data << SYNCOOKIE_WNDBITS) | idx;
1353	data ^= sc->sc_irs;				/* peer's iss */
1354	MD5Init(&syn_ctx);
1355#ifdef INET6
1356	if (sc->sc_inc.inc_isipv6) {
1357		MD5Add(sc->sc_inc.inc6_laddr);
1358		MD5Add(sc->sc_inc.inc6_faddr);
1359		add.laddr = 0;
1360		add.faddr = 0;
1361	} else
1362#endif
1363	{
1364		add.laddr = sc->sc_inc.inc_laddr.s_addr;
1365		add.faddr = sc->sc_inc.inc_faddr.s_addr;
1366	}
1367	add.lport = sc->sc_inc.inc_lport;
1368	add.fport = sc->sc_inc.inc_fport;
1369	add.secbits[0] = tcp_secret[idx].ts_secbits[0];
1370	add.secbits[1] = tcp_secret[idx].ts_secbits[1];
1371	add.secbits[2] = tcp_secret[idx].ts_secbits[2];
1372	add.secbits[3] = tcp_secret[idx].ts_secbits[3];
1373	MD5Add(add);
1374	MD5Final((u_char *)&md5_buffer, &syn_ctx);
1375	data ^= (md5_buffer[0] & ~SYNCOOKIE_WNDMASK);
1376	return (data);
1377}
1378
1379static struct syncache *
1380syncookie_lookup(inc, th, so)
1381	struct in_conninfo *inc;
1382	struct tcphdr *th;
1383	struct socket *so;
1384{
1385	u_int32_t md5_buffer[4];
1386	struct syncache *sc;
1387	u_int32_t data;
1388	int wnd, idx;
1389	struct md5_add add;
1390
1391	/* NB: single threaded; could add INP_INFO_WLOCK_ASSERT(&tcbinfo) */
1392
1393	data = (th->th_ack - 1) ^ (th->th_seq - 1);	/* remove ISS */
1394	idx = data & SYNCOOKIE_WNDMASK;
1395	if (tcp_secret[idx].ts_expire < ticks ||
1396	    sototcpcb(so)->ts_recent + SYNCOOKIE_TIMEOUT < ticks)
1397		return (NULL);
1398	MD5Init(&syn_ctx);
1399#ifdef INET6
1400	if (inc->inc_isipv6) {
1401		MD5Add(inc->inc6_laddr);
1402		MD5Add(inc->inc6_faddr);
1403		add.laddr = 0;
1404		add.faddr = 0;
1405	} else
1406#endif
1407	{
1408		add.laddr = inc->inc_laddr.s_addr;
1409		add.faddr = inc->inc_faddr.s_addr;
1410	}
1411	add.lport = inc->inc_lport;
1412	add.fport = inc->inc_fport;
1413	add.secbits[0] = tcp_secret[idx].ts_secbits[0];
1414	add.secbits[1] = tcp_secret[idx].ts_secbits[1];
1415	add.secbits[2] = tcp_secret[idx].ts_secbits[2];
1416	add.secbits[3] = tcp_secret[idx].ts_secbits[3];
1417	MD5Add(add);
1418	MD5Final((u_char *)&md5_buffer, &syn_ctx);
1419	data ^= md5_buffer[0];
1420	if ((data & ~SYNCOOKIE_DATAMASK) != 0)
1421		return (NULL);
1422	data = data >> SYNCOOKIE_WNDBITS;
1423
1424	sc = uma_zalloc(tcp_syncache.zone, M_NOWAIT);
1425	if (sc == NULL)
1426		return (NULL);
1427	/*
1428	 * Fill in the syncache values.
1429	 * XXX duplicate code from syncache_add
1430	 */
1431	sc->sc_ipopts = NULL;
1432	sc->sc_inc.inc_fport = inc->inc_fport;
1433	sc->sc_inc.inc_lport = inc->inc_lport;
1434#ifdef INET6
1435	sc->sc_inc.inc_isipv6 = inc->inc_isipv6;
1436	if (inc->inc_isipv6) {
1437		sc->sc_inc.inc6_faddr = inc->inc6_faddr;
1438		sc->sc_inc.inc6_laddr = inc->inc6_laddr;
1439	} else
1440#endif
1441	{
1442		sc->sc_inc.inc_faddr = inc->inc_faddr;
1443		sc->sc_inc.inc_laddr = inc->inc_laddr;
1444	}
1445	sc->sc_irs = th->th_seq - 1;
1446	sc->sc_iss = th->th_ack - 1;
1447	wnd = sbspace(&so->so_rcv);
1448	wnd = imax(wnd, 0);
1449	wnd = imin(wnd, TCP_MAXWIN);
1450	sc->sc_wnd = wnd;
1451	sc->sc_flags = 0;
1452	sc->sc_rxtslot = 0;
1453	sc->sc_peer_mss = tcp_msstab[data];
1454	return (sc);
1455}
1456