xref: /freebsd/sys/netinet/tcp_syncache.c (revision 341d1aabc13e47911d2eb38e857b90f7d356134e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001 McAfee, Inc.
5  * Copyright (c) 2006,2013 Andre Oppermann, Internet Business Solutions AG
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Jonathan Lemon
9  * and McAfee Research, the Security Research Division of McAfee, Inc. under
10  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program. [2001 McAfee, Inc.]
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
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 
35 #include <sys/cdefs.h>
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/hash.h>
43 #include <sys/refcount.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.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 #include <sys/syslog.h>
56 #include <sys/ucred.h>
57 
58 #include <sys/md5.h>
59 #include <crypto/siphash/siphash.h>
60 
61 #include <vm/uma.h>
62 
63 #include <net/if.h>
64 #include <net/if_var.h>
65 #include <net/route.h>
66 #include <net/vnet.h>
67 
68 #include <netinet/in.h>
69 #include <netinet/in_kdtrace.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/in_var.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_options.h>
76 #ifdef INET6
77 #include <netinet/ip6.h>
78 #include <netinet/icmp6.h>
79 #include <netinet6/nd6.h>
80 #include <netinet6/ip6_var.h>
81 #include <netinet6/in6_pcb.h>
82 #endif
83 #include <netinet/tcp.h>
84 #include <netinet/tcp_fastopen.h>
85 #include <netinet/tcp_fsm.h>
86 #include <netinet/tcp_seq.h>
87 #include <netinet/tcp_timer.h>
88 #include <netinet/tcp_var.h>
89 #include <netinet/tcp_syncache.h>
90 #include <netinet/tcp_ecn.h>
91 #ifdef TCP_BLACKBOX
92 #include <netinet/tcp_log_buf.h>
93 #endif
94 #ifdef TCP_OFFLOAD
95 #include <netinet/toecore.h>
96 #endif
97 #include <netinet/udp.h>
98 
99 #include <netipsec/ipsec_support.h>
100 
101 #include <machine/in_cksum.h>
102 
103 #include <security/mac/mac_framework.h>
104 
105 VNET_DEFINE_STATIC(bool, tcp_syncookies) = true;
106 #define	V_tcp_syncookies		VNET(tcp_syncookies)
107 SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_VNET | CTLFLAG_RW,
108     &VNET_NAME(tcp_syncookies), 0,
109     "Use TCP SYN cookies if the syncache overflows");
110 
111 VNET_DEFINE_STATIC(bool, tcp_syncookiesonly) = false;
112 #define	V_tcp_syncookiesonly		VNET(tcp_syncookiesonly)
113 SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, syncookies_only, CTLFLAG_VNET | CTLFLAG_RW,
114     &VNET_NAME(tcp_syncookiesonly), 0,
115     "Use only TCP SYN cookies");
116 
117 #ifdef TCP_OFFLOAD
118 #define ADDED_BY_TOE(sc) ((sc)->sc_tod != NULL)
119 #endif
120 
121 static void	 syncache_drop(struct syncache *, struct syncache_head *);
122 static void	 syncache_free(struct syncache *);
123 static void	 syncache_insert(struct syncache *, struct syncache_head *);
124 static int	 syncache_respond(struct syncache *, const struct mbuf *, int);
125 static struct	 socket *syncache_socket(struct syncache *, struct socket *,
126 		    struct mbuf *m);
127 static void	 syncache_timeout(struct syncache *sc, struct syncache_head *sch,
128 		    int docallout);
129 static void	 syncache_timer(void *);
130 
131 static uint32_t	 syncookie_mac(struct in_conninfo *, tcp_seq, uint8_t,
132 		    uint8_t *, uintptr_t);
133 static tcp_seq	 syncookie_generate(struct syncache_head *, struct syncache *);
134 static bool	syncookie_expand(struct in_conninfo *,
135 		    const struct syncache_head *, struct syncache *,
136 		    struct tcphdr *, struct tcpopt *, struct socket *,
137 		    uint16_t);
138 static void	syncache_pause(struct in_conninfo *);
139 static void	syncache_unpause(void *);
140 static void	 syncookie_reseed(void *);
141 #ifdef INVARIANTS
142 static void	syncookie_cmp(struct in_conninfo *,
143 		    const struct syncache_head *, struct syncache *,
144 		    struct tcphdr *, struct tcpopt *, struct socket *,
145 		    uint16_t);
146 #endif
147 
148 /*
149  * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies.
150  * 3 retransmits corresponds to a timeout with default values of
151  * tcp_rexmit_initial * (             1 +
152  *                       tcp_backoff[1] +
153  *                       tcp_backoff[2] +
154  *                       tcp_backoff[3]) + 3 * tcp_rexmit_slop,
155  * 1000 ms * (1 + 2 + 4 + 8) +  3 * 200 ms = 15600 ms,
156  * the odds are that the user has given up attempting to connect by then.
157  */
158 #define SYNCACHE_MAXREXMTS		3
159 
160 /* Arbitrary values */
161 #define TCP_SYNCACHE_HASHSIZE		512
162 #define TCP_SYNCACHE_BUCKETLIMIT	30
163 
164 VNET_DEFINE_STATIC(struct tcp_syncache, tcp_syncache);
165 #define	V_tcp_syncache			VNET(tcp_syncache)
166 
167 static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache,
168     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
169     "TCP SYN cache");
170 
171 SYSCTL_UINT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
172     &VNET_NAME(tcp_syncache.bucket_limit), 0,
173     "Per-bucket hash limit for syncache");
174 
175 SYSCTL_UINT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_VNET | CTLFLAG_RDTUN,
176     &VNET_NAME(tcp_syncache.cache_limit), 0,
177     "Overall entry limit for syncache");
178 
179 SYSCTL_UMA_CUR(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_VNET,
180     &VNET_NAME(tcp_syncache.zone), "Current number of entries in syncache");
181 
182 SYSCTL_UINT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_VNET | CTLFLAG_RDTUN,
183     &VNET_NAME(tcp_syncache.hashsize), 0,
184     "Size of TCP syncache hashtable");
185 
186 SYSCTL_BOOL(_net_inet_tcp_syncache, OID_AUTO, see_other, CTLFLAG_VNET |
187     CTLFLAG_RW, &VNET_NAME(tcp_syncache.see_other), 0,
188     "All syncache(4) entries are visible, ignoring UID/GID, jail(2) "
189     "and mac(4) checks");
190 
191 static int
192 sysctl_net_inet_tcp_syncache_rexmtlimit_check(SYSCTL_HANDLER_ARGS)
193 {
194 	int error;
195 	u_int new;
196 
197 	new = V_tcp_syncache.rexmt_limit;
198 	error = sysctl_handle_int(oidp, &new, 0, req);
199 	if ((error == 0) && (req->newptr != NULL)) {
200 		if (new > TCP_MAXRXTSHIFT)
201 			error = EINVAL;
202 		else
203 			V_tcp_syncache.rexmt_limit = new;
204 	}
205 	return (error);
206 }
207 
208 SYSCTL_PROC(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit,
209     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
210     &VNET_NAME(tcp_syncache.rexmt_limit), 0,
211     sysctl_net_inet_tcp_syncache_rexmtlimit_check, "IU",
212     "Limit on SYN/ACK retransmissions");
213 
214 VNET_DEFINE(int, tcp_sc_rst_sock_fail) = 1;
215 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rst_on_sock_fail,
216     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_sc_rst_sock_fail), 0,
217     "Send reset on socket allocation failure");
218 
219 static MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache");
220 
221 #define	SCH_LOCK(sch)		mtx_lock(&(sch)->sch_mtx)
222 #define	SCH_UNLOCK(sch)		mtx_unlock(&(sch)->sch_mtx)
223 #define	SCH_LOCK_ASSERT(sch)	mtx_assert(&(sch)->sch_mtx, MA_OWNED)
224 
225 /*
226  * Requires the syncache entry to be already removed from the bucket list.
227  */
228 static void
229 syncache_free(struct syncache *sc)
230 {
231 
232 	if (sc->sc_ipopts)
233 		(void)m_free(sc->sc_ipopts);
234 	if (sc->sc_cred)
235 		crfree(sc->sc_cred);
236 #ifdef MAC
237 	mac_syncache_destroy(&sc->sc_label);
238 #endif
239 
240 	uma_zfree(V_tcp_syncache.zone, sc);
241 }
242 
243 void
244 syncache_init(void)
245 {
246 	int i;
247 
248 	V_tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
249 	V_tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT;
250 	V_tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS;
251 	V_tcp_syncache.hash_secret = arc4random();
252 
253 	TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize",
254 	    &V_tcp_syncache.hashsize);
255 	TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit",
256 	    &V_tcp_syncache.bucket_limit);
257 	if (!powerof2(V_tcp_syncache.hashsize) ||
258 	    V_tcp_syncache.hashsize == 0) {
259 		printf("WARNING: syncache hash size is not a power of 2.\n");
260 		V_tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
261 	}
262 	V_tcp_syncache.hashmask = V_tcp_syncache.hashsize - 1;
263 
264 	/* Set limits. */
265 	V_tcp_syncache.cache_limit =
266 	    V_tcp_syncache.hashsize * V_tcp_syncache.bucket_limit;
267 	TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit",
268 	    &V_tcp_syncache.cache_limit);
269 
270 	/* Allocate the hash table. */
271 	V_tcp_syncache.hashbase = malloc(V_tcp_syncache.hashsize *
272 	    sizeof(struct syncache_head), M_SYNCACHE, M_WAITOK | M_ZERO);
273 
274 #ifdef VIMAGE
275 	V_tcp_syncache.vnet = curvnet;
276 #endif
277 
278 	/* Initialize the hash buckets. */
279 	for (i = 0; i < V_tcp_syncache.hashsize; i++) {
280 		TAILQ_INIT(&V_tcp_syncache.hashbase[i].sch_bucket);
281 		mtx_init(&V_tcp_syncache.hashbase[i].sch_mtx, "tcp_sc_head",
282 			 NULL, MTX_DEF);
283 		callout_init_mtx(&V_tcp_syncache.hashbase[i].sch_timer,
284 			 &V_tcp_syncache.hashbase[i].sch_mtx, 0);
285 		V_tcp_syncache.hashbase[i].sch_length = 0;
286 		V_tcp_syncache.hashbase[i].sch_sc = &V_tcp_syncache;
287 		V_tcp_syncache.hashbase[i].sch_last_overflow =
288 		    -(SYNCOOKIE_LIFETIME + 1);
289 	}
290 
291 	/* Create the syncache entry zone. */
292 	V_tcp_syncache.zone = uma_zcreate("syncache", sizeof(struct syncache),
293 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
294 	V_tcp_syncache.cache_limit = uma_zone_set_max(V_tcp_syncache.zone,
295 	    V_tcp_syncache.cache_limit);
296 
297 	/* Start the SYN cookie reseeder callout. */
298 	callout_init(&V_tcp_syncache.secret.reseed, 1);
299 	arc4rand(V_tcp_syncache.secret.key[0], SYNCOOKIE_SECRET_SIZE, 0);
300 	arc4rand(V_tcp_syncache.secret.key[1], SYNCOOKIE_SECRET_SIZE, 0);
301 	callout_reset(&V_tcp_syncache.secret.reseed, SYNCOOKIE_LIFETIME * hz,
302 	    syncookie_reseed, &V_tcp_syncache);
303 
304 	/* Initialize the pause machinery. */
305 	mtx_init(&V_tcp_syncache.pause_mtx, "tcp_sc_pause", NULL, MTX_DEF);
306 	callout_init_mtx(&V_tcp_syncache.pause_co, &V_tcp_syncache.pause_mtx,
307 	    0);
308 	V_tcp_syncache.pause_until = time_uptime - TCP_SYNCACHE_PAUSE_TIME;
309 	V_tcp_syncache.pause_backoff = 0;
310 	V_tcp_syncache.paused = false;
311 }
312 
313 #ifdef VIMAGE
314 void
315 syncache_destroy(void)
316 {
317 	struct syncache_head *sch;
318 	struct syncache *sc, *nsc;
319 	int i;
320 
321 	/*
322 	 * Stop the re-seed timer before freeing resources.  No need to
323 	 * possibly schedule it another time.
324 	 */
325 	callout_drain(&V_tcp_syncache.secret.reseed);
326 
327 	/* Stop the SYN cache pause callout. */
328 	mtx_lock(&V_tcp_syncache.pause_mtx);
329 	if (callout_stop(&V_tcp_syncache.pause_co) == 0) {
330 		mtx_unlock(&V_tcp_syncache.pause_mtx);
331 		callout_drain(&V_tcp_syncache.pause_co);
332 	} else
333 		mtx_unlock(&V_tcp_syncache.pause_mtx);
334 
335 	/* Cleanup hash buckets: stop timers, free entries, destroy locks. */
336 	for (i = 0; i < V_tcp_syncache.hashsize; i++) {
337 		sch = &V_tcp_syncache.hashbase[i];
338 		callout_drain(&sch->sch_timer);
339 
340 		SCH_LOCK(sch);
341 		TAILQ_FOREACH_SAFE(sc, &sch->sch_bucket, sc_hash, nsc)
342 			syncache_drop(sc, sch);
343 		SCH_UNLOCK(sch);
344 		KASSERT(TAILQ_EMPTY(&sch->sch_bucket),
345 		    ("%s: sch->sch_bucket not empty", __func__));
346 		KASSERT(sch->sch_length == 0, ("%s: sch->sch_length %d not 0",
347 		    __func__, sch->sch_length));
348 		mtx_destroy(&sch->sch_mtx);
349 	}
350 
351 	KASSERT(uma_zone_get_cur(V_tcp_syncache.zone) == 0,
352 	    ("%s: cache_count not 0", __func__));
353 
354 	/* Free the allocated global resources. */
355 	uma_zdestroy(V_tcp_syncache.zone);
356 	free(V_tcp_syncache.hashbase, M_SYNCACHE);
357 	mtx_destroy(&V_tcp_syncache.pause_mtx);
358 }
359 #endif
360 
361 /*
362  * Inserts a syncache entry into the specified bucket row.
363  * Locks and unlocks the syncache_head autonomously.
364  */
365 static void
366 syncache_insert(struct syncache *sc, struct syncache_head *sch)
367 {
368 	struct syncache *sc2;
369 
370 	SCH_LOCK(sch);
371 
372 	/*
373 	 * Make sure that we don't overflow the per-bucket limit.
374 	 * If the bucket is full, toss the oldest element.
375 	 */
376 	if (sch->sch_length >= V_tcp_syncache.bucket_limit) {
377 		KASSERT(!TAILQ_EMPTY(&sch->sch_bucket),
378 			("sch->sch_length incorrect"));
379 		syncache_pause(&sc->sc_inc);
380 		sc2 = TAILQ_LAST(&sch->sch_bucket, sch_head);
381 		sch->sch_last_overflow = time_uptime;
382 		syncache_drop(sc2, sch);
383 	}
384 
385 	/* Put it into the bucket. */
386 	TAILQ_INSERT_HEAD(&sch->sch_bucket, sc, sc_hash);
387 	sch->sch_length++;
388 
389 #ifdef TCP_OFFLOAD
390 	if (ADDED_BY_TOE(sc)) {
391 		struct toedev *tod = sc->sc_tod;
392 
393 		tod->tod_syncache_added(tod, sc->sc_todctx);
394 	}
395 #endif
396 
397 	/* Reinitialize the bucket row's timer. */
398 	if (sch->sch_length == 1)
399 		sch->sch_nextc = ticks + INT_MAX;
400 	syncache_timeout(sc, sch, 1);
401 
402 	SCH_UNLOCK(sch);
403 
404 	TCPSTATES_INC(TCPS_SYN_RECEIVED);
405 	TCPSTAT_INC(tcps_sc_added);
406 }
407 
408 /*
409  * Remove and free entry from syncache bucket row.
410  * Expects locked syncache head.
411  */
412 static void
413 syncache_drop(struct syncache *sc, struct syncache_head *sch)
414 {
415 
416 	SCH_LOCK_ASSERT(sch);
417 
418 	TCPSTATES_DEC(TCPS_SYN_RECEIVED);
419 	TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
420 	sch->sch_length--;
421 
422 #ifdef TCP_OFFLOAD
423 	if (ADDED_BY_TOE(sc)) {
424 		struct toedev *tod = sc->sc_tod;
425 
426 		tod->tod_syncache_removed(tod, sc->sc_todctx);
427 	}
428 #endif
429 
430 	syncache_free(sc);
431 }
432 
433 /*
434  * Engage/reengage time on bucket row.
435  */
436 static void
437 syncache_timeout(struct syncache *sc, struct syncache_head *sch, int docallout)
438 {
439 	int rexmt;
440 
441 	if (sc->sc_rxmits == 0)
442 		rexmt = tcp_rexmit_initial;
443 	else
444 		TCPT_RANGESET(rexmt,
445 		    tcp_rexmit_initial * tcp_backoff[sc->sc_rxmits],
446 		    tcp_rexmit_min, tcp_rexmit_max);
447 	sc->sc_rxttime = ticks + rexmt;
448 	sc->sc_rxmits++;
449 	if (TSTMP_LT(sc->sc_rxttime, sch->sch_nextc)) {
450 		sch->sch_nextc = sc->sc_rxttime;
451 		if (docallout)
452 			callout_reset(&sch->sch_timer, sch->sch_nextc - ticks,
453 			    syncache_timer, (void *)sch);
454 	}
455 }
456 
457 /*
458  * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
459  * If we have retransmitted an entry the maximum number of times, expire it.
460  * One separate timer for each bucket row.
461  */
462 static void
463 syncache_timer(void *xsch)
464 {
465 	struct syncache_head *sch = (struct syncache_head *)xsch;
466 	struct syncache *sc, *nsc;
467 	struct epoch_tracker et;
468 	int tick = ticks;
469 	char *s;
470 	bool paused;
471 
472 	CURVNET_SET(sch->sch_sc->vnet);
473 
474 	/* NB: syncache_head has already been locked by the callout. */
475 	SCH_LOCK_ASSERT(sch);
476 
477 	/*
478 	 * In the following cycle we may remove some entries and/or
479 	 * advance some timeouts, so re-initialize the bucket timer.
480 	 */
481 	sch->sch_nextc = tick + INT_MAX;
482 
483 	/*
484 	 * If we have paused processing, unconditionally remove
485 	 * all syncache entries.
486 	 */
487 	mtx_lock(&V_tcp_syncache.pause_mtx);
488 	paused = V_tcp_syncache.paused;
489 	mtx_unlock(&V_tcp_syncache.pause_mtx);
490 
491 	TAILQ_FOREACH_SAFE(sc, &sch->sch_bucket, sc_hash, nsc) {
492 		if (paused) {
493 			syncache_drop(sc, sch);
494 			continue;
495 		}
496 		/*
497 		 * We do not check if the listen socket still exists
498 		 * and accept the case where the listen socket may be
499 		 * gone by the time we resend the SYN/ACK.  We do
500 		 * not expect this to happens often. If it does,
501 		 * then the RST will be sent by the time the remote
502 		 * host does the SYN/ACK->ACK.
503 		 */
504 		if (TSTMP_GT(sc->sc_rxttime, tick)) {
505 			if (TSTMP_LT(sc->sc_rxttime, sch->sch_nextc))
506 				sch->sch_nextc = sc->sc_rxttime;
507 			continue;
508 		}
509 		if (sc->sc_rxmits > V_tcp_ecn_maxretries) {
510 			sc->sc_flags &= ~SCF_ECN_MASK;
511 		}
512 		if (sc->sc_rxmits > V_tcp_syncache.rexmt_limit) {
513 			if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
514 				log(LOG_DEBUG, "%s; %s: Retransmits exhausted, "
515 				    "giving up and removing syncache entry\n",
516 				    s, __func__);
517 				free(s, M_TCPLOG);
518 			}
519 			syncache_drop(sc, sch);
520 			TCPSTAT_INC(tcps_sc_stale);
521 			continue;
522 		}
523 		if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
524 			log(LOG_DEBUG, "%s; %s: Response timeout, "
525 			    "retransmitting (%u) SYN|ACK\n",
526 			    s, __func__, sc->sc_rxmits);
527 			free(s, M_TCPLOG);
528 		}
529 
530 		NET_EPOCH_ENTER(et);
531 		if (syncache_respond(sc, NULL, TH_SYN|TH_ACK) == 0) {
532 			syncache_timeout(sc, sch, 0);
533 			TCPSTAT_INC(tcps_sndacks);
534 			TCPSTAT_INC(tcps_sndtotal);
535 			TCPSTAT_INC(tcps_sc_retransmitted);
536 		} else {
537 			syncache_drop(sc, sch);
538 			TCPSTAT_INC(tcps_sc_dropped);
539 		}
540 		NET_EPOCH_EXIT(et);
541 	}
542 	if (!TAILQ_EMPTY(&(sch)->sch_bucket))
543 		callout_reset(&(sch)->sch_timer, (sch)->sch_nextc - tick,
544 			syncache_timer, (void *)(sch));
545 	CURVNET_RESTORE();
546 }
547 
548 /*
549  * Returns true if the system is only using cookies at the moment.
550  * This could be due to a sysadmin decision to only use cookies, or it
551  * could be due to the system detecting an attack.
552  */
553 static inline bool
554 syncache_cookiesonly(void)
555 {
556 	return ((V_tcp_syncookies && V_tcp_syncache.paused) ||
557 	    V_tcp_syncookiesonly);
558 }
559 
560 /*
561  * Find the hash bucket for the given connection.
562  */
563 static struct syncache_head *
564 syncache_hashbucket(struct in_conninfo *inc)
565 {
566 	uint32_t hash;
567 
568 	/*
569 	 * The hash is built on foreign port + local port + foreign address.
570 	 * We rely on the fact that struct in_conninfo starts with 16 bits
571 	 * of foreign port, then 16 bits of local port then followed by 128
572 	 * bits of foreign address.  In case of IPv4 address, the first 3
573 	 * 32-bit words of the address always are zeroes.
574 	 */
575 	hash = jenkins_hash32((uint32_t *)&inc->inc_ie, 5,
576 	    V_tcp_syncache.hash_secret) & V_tcp_syncache.hashmask;
577 
578 	return (&V_tcp_syncache.hashbase[hash]);
579 }
580 
581 /*
582  * Find an entry in the syncache.
583  * Returns always with locked syncache_head plus a matching entry or NULL.
584  */
585 static struct syncache *
586 syncache_lookup(struct in_conninfo *inc, struct syncache_head **schp)
587 {
588 	struct syncache *sc;
589 	struct syncache_head *sch;
590 
591 	*schp = sch = syncache_hashbucket(inc);
592 	SCH_LOCK(sch);
593 
594 	/* Circle through bucket row to find matching entry. */
595 	TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash)
596 		if (bcmp(&inc->inc_ie, &sc->sc_inc.inc_ie,
597 		    sizeof(struct in_endpoints)) == 0)
598 			break;
599 
600 	return (sc);	/* Always returns with locked sch. */
601 }
602 
603 /*
604  * This function is called when we get a RST for a
605  * non-existent connection, so that we can see if the
606  * connection is in the syn cache.  If it is, zap it.
607  * If required send a challenge ACK.
608  */
609 void
610 syncache_chkrst(struct in_conninfo *inc, struct tcphdr *th, struct mbuf *m,
611     uint16_t port)
612 {
613 	struct syncache *sc;
614 	struct syncache_head *sch;
615 	char *s = NULL;
616 
617 	if (syncache_cookiesonly())
618 		return;
619 	sc = syncache_lookup(inc, &sch);	/* returns locked sch */
620 	SCH_LOCK_ASSERT(sch);
621 
622 	/*
623 	 * No corresponding connection was found in syncache.
624 	 * If syncookies are enabled and possibly exclusively
625 	 * used, or we are under memory pressure, a valid RST
626 	 * may not find a syncache entry.  In that case we're
627 	 * done and no SYN|ACK retransmissions will happen.
628 	 * Otherwise the RST was misdirected or spoofed.
629 	 */
630 	if (sc == NULL) {
631 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
632 			log(LOG_DEBUG, "%s; %s: Spurious RST without matching "
633 			    "syncache entry (possibly syncookie only), "
634 			    "segment ignored\n", s, __func__);
635 		TCPSTAT_INC(tcps_badrst);
636 		goto done;
637 	}
638 
639 	/* The remote UDP encaps port does not match. */
640 	if (sc->sc_port != port) {
641 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
642 			log(LOG_DEBUG, "%s; %s: Spurious RST with matching "
643 			    "syncache entry but non-matching UDP encaps port, "
644 			    "segment ignored\n", s, __func__);
645 		TCPSTAT_INC(tcps_badrst);
646 		goto done;
647 	}
648 
649 	/*
650 	 * If the RST bit is set, check the sequence number to see
651 	 * if this is a valid reset segment.
652 	 *
653 	 * RFC 793 page 37:
654 	 *   In all states except SYN-SENT, all reset (RST) segments
655 	 *   are validated by checking their SEQ-fields.  A reset is
656 	 *   valid if its sequence number is in the window.
657 	 *
658 	 * RFC 793 page 69:
659 	 *   There are four cases for the acceptability test for an incoming
660 	 *   segment:
661 	 *
662 	 * Segment Receive  Test
663 	 * Length  Window
664 	 * ------- -------  -------------------------------------------
665 	 *    0       0     SEG.SEQ = RCV.NXT
666 	 *    0      >0     RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
667 	 *   >0       0     not acceptable
668 	 *   >0      >0     RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
669 	 *               or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
670 	 *
671 	 * Note that when receiving a SYN segment in the LISTEN state,
672 	 * IRS is set to SEG.SEQ and RCV.NXT is set to SEG.SEQ+1, as
673 	 * described in RFC 793, page 66.
674 	 */
675 	if ((SEQ_GEQ(th->th_seq, sc->sc_irs + 1) &&
676 	    SEQ_LT(th->th_seq, sc->sc_irs + 1 + sc->sc_wnd)) ||
677 	    (sc->sc_wnd == 0 && th->th_seq == sc->sc_irs + 1)) {
678 		if (V_tcp_insecure_rst ||
679 		    th->th_seq == sc->sc_irs + 1) {
680 			syncache_drop(sc, sch);
681 			if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
682 				log(LOG_DEBUG,
683 				    "%s; %s: Our SYN|ACK was rejected, "
684 				    "connection attempt aborted by remote "
685 				    "endpoint\n",
686 				    s, __func__);
687 			TCPSTAT_INC(tcps_sc_reset);
688 		} else {
689 			TCPSTAT_INC(tcps_badrst);
690 			/* Send challenge ACK. */
691 			if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
692 				log(LOG_DEBUG, "%s; %s: RST with invalid "
693 				    " SEQ %u != NXT %u (+WND %u), "
694 				    "sending challenge ACK\n",
695 				    s, __func__,
696 				    th->th_seq, sc->sc_irs + 1, sc->sc_wnd);
697 			if (syncache_respond(sc, m, TH_ACK) == 0) {
698 				TCPSTAT_INC(tcps_sndacks);
699 				TCPSTAT_INC(tcps_sndtotal);
700 			} else {
701 				syncache_drop(sc, sch);
702 				TCPSTAT_INC(tcps_sc_dropped);
703 			}
704 		}
705 	} else {
706 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
707 			log(LOG_DEBUG, "%s; %s: RST with invalid SEQ %u != "
708 			    "NXT %u (+WND %u), segment ignored\n",
709 			    s, __func__,
710 			    th->th_seq, sc->sc_irs + 1, sc->sc_wnd);
711 		TCPSTAT_INC(tcps_badrst);
712 	}
713 
714 done:
715 	if (s != NULL)
716 		free(s, M_TCPLOG);
717 	SCH_UNLOCK(sch);
718 }
719 
720 void
721 syncache_badack(struct in_conninfo *inc, uint16_t port)
722 {
723 	struct syncache *sc;
724 	struct syncache_head *sch;
725 
726 	if (syncache_cookiesonly())
727 		return;
728 	sc = syncache_lookup(inc, &sch);	/* returns locked sch */
729 	SCH_LOCK_ASSERT(sch);
730 	if ((sc != NULL) && (sc->sc_port == port)) {
731 		syncache_drop(sc, sch);
732 		TCPSTAT_INC(tcps_sc_badack);
733 	}
734 	SCH_UNLOCK(sch);
735 }
736 
737 void
738 syncache_unreach(struct in_conninfo *inc, tcp_seq th_seq, uint16_t port)
739 {
740 	struct syncache *sc;
741 	struct syncache_head *sch;
742 
743 	if (syncache_cookiesonly())
744 		return;
745 	sc = syncache_lookup(inc, &sch);	/* returns locked sch */
746 	SCH_LOCK_ASSERT(sch);
747 	if (sc == NULL)
748 		goto done;
749 
750 	/* If the port != sc_port, then it's a bogus ICMP msg */
751 	if (port != sc->sc_port)
752 		goto done;
753 
754 	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
755 	if (ntohl(th_seq) != sc->sc_iss)
756 		goto done;
757 
758 	/*
759 	 * If we've rertransmitted 3 times and this is our second error,
760 	 * we remove the entry.  Otherwise, we allow it to continue on.
761 	 * This prevents us from incorrectly nuking an entry during a
762 	 * spurious network outage.
763 	 *
764 	 * See tcp_notify().
765 	 */
766 	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxmits < 3 + 1) {
767 		sc->sc_flags |= SCF_UNREACH;
768 		goto done;
769 	}
770 	syncache_drop(sc, sch);
771 	TCPSTAT_INC(tcps_sc_unreach);
772 done:
773 	SCH_UNLOCK(sch);
774 }
775 
776 /*
777  * Build a new TCP socket structure from a syncache entry.
778  *
779  * On success return the newly created socket with its underlying inp locked.
780  */
781 static struct socket *
782 syncache_socket(struct syncache *sc, struct socket *lso, struct mbuf *m)
783 {
784 	struct inpcb *inp = NULL;
785 	struct socket *so;
786 	struct tcpcb *tp;
787 	int error;
788 	char *s;
789 
790 	NET_EPOCH_ASSERT();
791 
792 	/*
793 	 * Ok, create the full blown connection, and set things up
794 	 * as they would have been set up if we had created the
795 	 * connection when the SYN arrived.
796 	 */
797 	if ((so = solisten_clone(lso)) == NULL)
798 		goto allocfail;
799 #ifdef MAC
800 	mac_socketpeer_set_from_mbuf(m, so);
801 #endif
802 	error = in_pcballoc(so, &V_tcbinfo);
803 	if (error) {
804 		sodealloc(so);
805 		goto allocfail;
806 	}
807 	inp = sotoinpcb(so);
808 	if ((tp = tcp_newtcpcb(inp, sototcpcb(lso))) == NULL) {
809 		in_pcbfree(inp);
810 		sodealloc(so);
811 		goto allocfail;
812 	}
813 	inp->inp_inc.inc_flags = sc->sc_inc.inc_flags;
814 #ifdef INET6
815 	if (sc->sc_inc.inc_flags & INC_ISIPV6) {
816 		inp->inp_vflag &= ~INP_IPV4;
817 		inp->inp_vflag |= INP_IPV6;
818 		inp->in6p_laddr = sc->sc_inc.inc6_laddr;
819 	} else {
820 		inp->inp_vflag &= ~INP_IPV6;
821 		inp->inp_vflag |= INP_IPV4;
822 #endif
823 		inp->inp_ip_ttl = sc->sc_ip_ttl;
824 		inp->inp_ip_tos = sc->sc_ip_tos;
825 		inp->inp_laddr = sc->sc_inc.inc_laddr;
826 #ifdef INET6
827 	}
828 #endif
829 
830 	/*
831 	 * If there's an mbuf and it has a flowid, then let's initialise the
832 	 * inp with that particular flowid.
833 	 */
834 	if (m != NULL && M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
835 		inp->inp_flowid = m->m_pkthdr.flowid;
836 		inp->inp_flowtype = M_HASHTYPE_GET(m);
837 #ifdef NUMA
838 		inp->inp_numa_domain = m->m_pkthdr.numa_domain;
839 #endif
840 	}
841 
842 	inp->inp_lport = sc->sc_inc.inc_lport;
843 #ifdef INET6
844 	if (inp->inp_vflag & INP_IPV6PROTO) {
845 		struct inpcb *oinp = sotoinpcb(lso);
846 
847 		/*
848 		 * Inherit socket options from the listening socket.
849 		 * Note that in6p_inputopts are not (and should not be)
850 		 * copied, since it stores previously received options and is
851 		 * used to detect if each new option is different than the
852 		 * previous one and hence should be passed to a user.
853 		 * If we copied in6p_inputopts, a user would not be able to
854 		 * receive options just after calling the accept system call.
855 		 */
856 		inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS;
857 		if (oinp->in6p_outputopts)
858 			inp->in6p_outputopts =
859 			    ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT);
860 		inp->in6p_hops = oinp->in6p_hops;
861 	}
862 
863 	if (sc->sc_inc.inc_flags & INC_ISIPV6) {
864 		struct sockaddr_in6 sin6;
865 
866 		sin6.sin6_family = AF_INET6;
867 		sin6.sin6_len = sizeof(sin6);
868 		sin6.sin6_addr = sc->sc_inc.inc6_faddr;
869 		sin6.sin6_port = sc->sc_inc.inc_fport;
870 		sin6.sin6_flowinfo = sin6.sin6_scope_id = 0;
871 		INP_HASH_WLOCK(&V_tcbinfo);
872 		error = in6_pcbconnect(inp, &sin6, thread0.td_ucred, false);
873 		INP_HASH_WUNLOCK(&V_tcbinfo);
874 		if (error != 0)
875 			goto abort;
876 		/* Override flowlabel from in6_pcbconnect. */
877 		inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
878 		inp->inp_flow |= sc->sc_flowlabel;
879 	}
880 #endif /* INET6 */
881 #if defined(INET) && defined(INET6)
882 	else
883 #endif
884 #ifdef INET
885 	{
886 		struct sockaddr_in sin;
887 
888 		inp->inp_options = (m) ? ip_srcroute(m) : NULL;
889 
890 		if (inp->inp_options == NULL) {
891 			inp->inp_options = sc->sc_ipopts;
892 			sc->sc_ipopts = NULL;
893 		}
894 
895 		sin.sin_family = AF_INET;
896 		sin.sin_len = sizeof(sin);
897 		sin.sin_addr = sc->sc_inc.inc_faddr;
898 		sin.sin_port = sc->sc_inc.inc_fport;
899 		bzero((caddr_t)sin.sin_zero, sizeof(sin.sin_zero));
900 		INP_HASH_WLOCK(&V_tcbinfo);
901 		error = in_pcbconnect(inp, &sin, thread0.td_ucred);
902 		INP_HASH_WUNLOCK(&V_tcbinfo);
903 		if (error != 0)
904 			goto abort;
905 	}
906 #endif /* INET */
907 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
908 	/* Copy old policy into new socket's. */
909 	if (ipsec_copy_pcbpolicy(sotoinpcb(lso), inp) != 0)
910 		printf("syncache_socket: could not copy policy\n");
911 #endif
912 	tp->t_state = TCPS_SYN_RECEIVED;
913 	tp->iss = sc->sc_iss;
914 	tp->irs = sc->sc_irs;
915 	tp->t_port = sc->sc_port;
916 	tcp_rcvseqinit(tp);
917 	tcp_sendseqinit(tp);
918 	tp->snd_wl1 = sc->sc_irs;
919 	tp->snd_max = tp->iss + 1;
920 	tp->snd_nxt = tp->iss + 1;
921 	tp->rcv_up = sc->sc_irs + 1;
922 	tp->rcv_wnd = sc->sc_wnd;
923 	tp->rcv_adv += tp->rcv_wnd;
924 	tp->last_ack_sent = tp->rcv_nxt;
925 
926 	tp->t_flags = sototcpcb(lso)->t_flags &
927 	    (TF_LRD|TF_NOPUSH|TF_NODELAY);
928 	if (sc->sc_flags & SCF_NOOPT)
929 		tp->t_flags |= TF_NOOPT;
930 	else {
931 		if (sc->sc_flags & SCF_WINSCALE) {
932 			tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
933 			tp->snd_scale = sc->sc_requested_s_scale;
934 			tp->request_r_scale = sc->sc_requested_r_scale;
935 		}
936 		if (sc->sc_flags & SCF_TIMESTAMP) {
937 			tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
938 			tp->ts_recent = sc->sc_tsreflect;
939 			tp->ts_recent_age = tcp_ts_getticks();
940 			tp->ts_offset = sc->sc_tsoff;
941 		}
942 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
943 		if (sc->sc_flags & SCF_SIGNATURE)
944 			tp->t_flags |= TF_SIGNATURE;
945 #endif
946 		if (sc->sc_flags & SCF_SACK)
947 			tp->t_flags |= TF_SACK_PERMIT;
948 	}
949 
950 	tcp_ecn_syncache_socket(tp, sc);
951 
952 	/*
953 	 * Set up MSS and get cached values from tcp_hostcache.
954 	 * This might overwrite some of the defaults we just set.
955 	 */
956 	tcp_mss(tp, sc->sc_peer_mss);
957 
958 	/*
959 	 * If the SYN,ACK was retransmitted, indicate that CWND to be
960 	 * limited to one segment in cc_conn_init().
961 	 * NB: sc_rxmits counts all SYN,ACK transmits, not just retransmits.
962 	 */
963 	if (sc->sc_rxmits > 1)
964 		tp->snd_cwnd = 1;
965 
966 #ifdef TCP_OFFLOAD
967 	/*
968 	 * Allow a TOE driver to install its hooks.  Note that we hold the
969 	 * pcbinfo lock too and that prevents tcp_usr_accept from accepting a
970 	 * new connection before the TOE driver has done its thing.
971 	 */
972 	if (ADDED_BY_TOE(sc)) {
973 		struct toedev *tod = sc->sc_tod;
974 
975 		tod->tod_offload_socket(tod, sc->sc_todctx, so);
976 	}
977 #endif
978 #ifdef TCP_BLACKBOX
979 	/*
980 	 * Inherit the log state from the listening socket, if
981 	 * - the log state of the listening socket is not off and
982 	 * - the listening socket was not auto selected from all sessions and
983 	 * - a log id is not set on the listening socket.
984 	 * This avoids inheriting a log state which was automatically set.
985 	 */
986 	if ((tcp_get_bblog_state(sototcpcb(lso)) != TCP_LOG_STATE_OFF) &&
987 	    ((sototcpcb(lso)->t_flags2 & TF2_LOG_AUTO) == 0) &&
988 	    (sototcpcb(lso)->t_lib == NULL)) {
989 		tcp_log_state_change(tp, tcp_get_bblog_state(sototcpcb(lso)));
990 	}
991 #endif
992 	/*
993 	 * Copy and activate timers.
994 	 */
995 	tp->t_maxunacktime = sototcpcb(lso)->t_maxunacktime;
996 	tp->t_keepinit = sototcpcb(lso)->t_keepinit;
997 	tp->t_keepidle = sototcpcb(lso)->t_keepidle;
998 	tp->t_keepintvl = sototcpcb(lso)->t_keepintvl;
999 	tp->t_keepcnt = sototcpcb(lso)->t_keepcnt;
1000 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
1001 
1002 	TCPSTAT_INC(tcps_accepts);
1003 	TCP_PROBE6(state__change, NULL, tp, NULL, tp, NULL, TCPS_LISTEN);
1004 
1005 	if (!solisten_enqueue(so, SS_ISCONNECTED))
1006 		tp->t_flags |= TF_SONOTCONN;
1007 	/* Can we inherit anything from the listener? */
1008 	if (tp->t_fb->tfb_inherit != NULL) {
1009 		(*tp->t_fb->tfb_inherit)(tp, sotoinpcb(lso));
1010 	}
1011 	return (so);
1012 
1013 allocfail:
1014 	/*
1015 	 * Drop the connection; we will either send a RST or have the peer
1016 	 * retransmit its SYN again after its RTO and try again.
1017 	 */
1018 	if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
1019 		log(LOG_DEBUG, "%s; %s: Socket create failed "
1020 		    "due to limits or memory shortage\n",
1021 		    s, __func__);
1022 		free(s, M_TCPLOG);
1023 	}
1024 	TCPSTAT_INC(tcps_listendrop);
1025 	return (NULL);
1026 
1027 abort:
1028 	tcp_discardcb(tp);
1029 	in_pcbfree(inp);
1030 	sodealloc(so);
1031 	if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
1032 		log(LOG_DEBUG, "%s; %s: in%s_pcbconnect failed with error %i\n",
1033 		    s, __func__, (sc->sc_inc.inc_flags & INC_ISIPV6) ? "6" : "",
1034 		    error);
1035 		free(s, M_TCPLOG);
1036 	}
1037 	TCPSTAT_INC(tcps_listendrop);
1038 	return (NULL);
1039 }
1040 
1041 /*
1042  * This function gets called when we receive an ACK for a
1043  * socket in the LISTEN state.  We look up the connection
1044  * in the syncache, and if its there, we pull it out of
1045  * the cache and turn it into a full-blown connection in
1046  * the SYN-RECEIVED state.
1047  *
1048  * On syncache_socket() success the newly created socket
1049  * has its underlying inp locked.
1050  */
1051 int
1052 syncache_expand(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
1053     struct socket **lsop, struct mbuf *m, uint16_t port)
1054 {
1055 	struct syncache *sc;
1056 	struct syncache_head *sch;
1057 	struct syncache scs;
1058 	char *s;
1059 	bool locked;
1060 
1061 	NET_EPOCH_ASSERT();
1062 	KASSERT((tcp_get_flags(th) & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK,
1063 	    ("%s: can handle only ACK", __func__));
1064 
1065 	if (syncache_cookiesonly()) {
1066 		sc = NULL;
1067 		sch = syncache_hashbucket(inc);
1068 		locked = false;
1069 	} else {
1070 		sc = syncache_lookup(inc, &sch);	/* returns locked sch */
1071 		locked = true;
1072 		SCH_LOCK_ASSERT(sch);
1073 	}
1074 
1075 #ifdef INVARIANTS
1076 	/*
1077 	 * Test code for syncookies comparing the syncache stored
1078 	 * values with the reconstructed values from the cookie.
1079 	 */
1080 	if (sc != NULL)
1081 		syncookie_cmp(inc, sch, sc, th, to, *lsop, port);
1082 #endif
1083 
1084 	if (sc == NULL) {
1085 		if (locked) {
1086 			/*
1087 			 * The syncache is currently in use (neither disabled,
1088 			 * nor paused), but no entry was found.
1089 			 */
1090 			if (!V_tcp_syncookies) {
1091 				/*
1092 				 * Since no syncookies are used in case of
1093 				 * a bucket overflow, don't even check for
1094 				 * a valid syncookie.
1095 				 */
1096 				SCH_UNLOCK(sch);
1097 				TCPSTAT_INC(tcps_sc_spurcookie);
1098 				if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
1099 					log(LOG_DEBUG, "%s; %s: Spurious ACK, "
1100 					    "segment rejected "
1101 					    "(syncookies disabled)\n",
1102 					    s, __func__);
1103 				goto failed;
1104 			}
1105 			if (sch->sch_last_overflow <
1106 			    time_uptime - SYNCOOKIE_LIFETIME) {
1107 				/*
1108 				 * Since the bucket did not overflow recently,
1109 				 * don't even check for a valid syncookie.
1110 				 */
1111 				SCH_UNLOCK(sch);
1112 				TCPSTAT_INC(tcps_sc_spurcookie);
1113 				if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
1114 					log(LOG_DEBUG, "%s; %s: Spurious ACK, "
1115 					    "segment rejected "
1116 					    "(no syncache entry)\n",
1117 					    s, __func__);
1118 				goto failed;
1119 			}
1120 			SCH_UNLOCK(sch);
1121 		}
1122 		bzero(&scs, sizeof(scs));
1123 		/*
1124 		 * Now check, if the syncookie is valid. If it is, create an on
1125 		 * stack syncache entry.
1126 		 */
1127 		if (syncookie_expand(inc, sch, &scs, th, to, *lsop, port)) {
1128 			sc = &scs;
1129 			TCPSTAT_INC(tcps_sc_recvcookie);
1130 		} else {
1131 			TCPSTAT_INC(tcps_sc_failcookie);
1132 			if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
1133 				log(LOG_DEBUG, "%s; %s: Segment failed "
1134 				    "SYNCOOKIE authentication, segment rejected "
1135 				    "(probably spoofed)\n", s, __func__);
1136 			goto failed;
1137 		}
1138 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1139 		/* If received ACK has MD5 signature, check it. */
1140 		if ((to->to_flags & TOF_SIGNATURE) != 0 &&
1141 		    (!TCPMD5_ENABLED() ||
1142 		    TCPMD5_INPUT(m, th, to->to_signature) != 0)) {
1143 			/* Drop the ACK. */
1144 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1145 				log(LOG_DEBUG, "%s; %s: Segment rejected, "
1146 				    "MD5 signature doesn't match.\n",
1147 				    s, __func__);
1148 				free(s, M_TCPLOG);
1149 			}
1150 			TCPSTAT_INC(tcps_sig_err_sigopt);
1151 			return (-1); /* Do not send RST */
1152 		}
1153 #endif /* TCP_SIGNATURE */
1154 		TCPSTATES_INC(TCPS_SYN_RECEIVED);
1155 	} else {
1156 		if (sc->sc_port != port) {
1157 			SCH_UNLOCK(sch);
1158 			return (0);
1159 		}
1160 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1161 		/*
1162 		 * If listening socket requested TCP digests, check that
1163 		 * received ACK has signature and it is correct.
1164 		 * If not, drop the ACK and leave sc entry in th cache,
1165 		 * because SYN was received with correct signature.
1166 		 */
1167 		if (sc->sc_flags & SCF_SIGNATURE) {
1168 			if ((to->to_flags & TOF_SIGNATURE) == 0) {
1169 				/* No signature */
1170 				TCPSTAT_INC(tcps_sig_err_nosigopt);
1171 				SCH_UNLOCK(sch);
1172 				if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1173 					log(LOG_DEBUG, "%s; %s: Segment "
1174 					    "rejected, MD5 signature wasn't "
1175 					    "provided.\n", s, __func__);
1176 					free(s, M_TCPLOG);
1177 				}
1178 				return (-1); /* Do not send RST */
1179 			}
1180 			if (!TCPMD5_ENABLED() ||
1181 			    TCPMD5_INPUT(m, th, to->to_signature) != 0) {
1182 				/* Doesn't match or no SA */
1183 				SCH_UNLOCK(sch);
1184 				if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1185 					log(LOG_DEBUG, "%s; %s: Segment "
1186 					    "rejected, MD5 signature doesn't "
1187 					    "match.\n", s, __func__);
1188 					free(s, M_TCPLOG);
1189 				}
1190 				return (-1); /* Do not send RST */
1191 			}
1192 		}
1193 #endif /* TCP_SIGNATURE */
1194 
1195 		/*
1196 		 * RFC 7323 PAWS: If we have a timestamp on this segment and
1197 		 * it's less than ts_recent, drop it.
1198 		 * XXXMT: RFC 7323 also requires to send an ACK.
1199 		 *        In tcp_input.c this is only done for TCP segments
1200 		 *        with user data, so be consistent here and just drop
1201 		 *        the segment.
1202 		 */
1203 		if (sc->sc_flags & SCF_TIMESTAMP && to->to_flags & TOF_TS &&
1204 		    TSTMP_LT(to->to_tsval, sc->sc_tsreflect)) {
1205 			SCH_UNLOCK(sch);
1206 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1207 				log(LOG_DEBUG,
1208 				    "%s; %s: SEG.TSval %u < TS.Recent %u, "
1209 				    "segment dropped\n", s, __func__,
1210 				    to->to_tsval, sc->sc_tsreflect);
1211 				free(s, M_TCPLOG);
1212 			}
1213 			return (-1);  /* Do not send RST */
1214 		}
1215 
1216 		/*
1217 		 * If timestamps were not negotiated during SYN/ACK and a
1218 		 * segment with a timestamp is received, ignore the
1219 		 * timestamp and process the packet normally.
1220 		 * See section 3.2 of RFC 7323.
1221 		 */
1222 		if (!(sc->sc_flags & SCF_TIMESTAMP) &&
1223 		    (to->to_flags & TOF_TS)) {
1224 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1225 				log(LOG_DEBUG, "%s; %s: Timestamp not "
1226 				    "expected, segment processed normally\n",
1227 				    s, __func__);
1228 				free(s, M_TCPLOG);
1229 				s = NULL;
1230 			}
1231 		}
1232 
1233 		/*
1234 		 * If timestamps were negotiated during SYN/ACK and a
1235 		 * segment without a timestamp is received, silently drop
1236 		 * the segment, unless the missing timestamps are tolerated.
1237 		 * See section 3.2 of RFC 7323.
1238 		 */
1239 		if ((sc->sc_flags & SCF_TIMESTAMP) &&
1240 		    !(to->to_flags & TOF_TS)) {
1241 			if (V_tcp_tolerate_missing_ts) {
1242 				if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1243 					log(LOG_DEBUG,
1244 					    "%s; %s: Timestamp missing, "
1245 					    "segment processed normally\n",
1246 					    s, __func__);
1247 					free(s, M_TCPLOG);
1248 				}
1249 			} else {
1250 				SCH_UNLOCK(sch);
1251 				if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1252 					log(LOG_DEBUG,
1253 					    "%s; %s: Timestamp missing, "
1254 					    "segment silently dropped\n",
1255 					    s, __func__);
1256 					free(s, M_TCPLOG);
1257 				}
1258 				return (-1);  /* Do not send RST */
1259 			}
1260 		}
1261 		TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
1262 		sch->sch_length--;
1263 #ifdef TCP_OFFLOAD
1264 		if (ADDED_BY_TOE(sc)) {
1265 			struct toedev *tod = sc->sc_tod;
1266 
1267 			tod->tod_syncache_removed(tod, sc->sc_todctx);
1268 		}
1269 #endif
1270 		SCH_UNLOCK(sch);
1271 	}
1272 
1273 	/*
1274 	 * Segment validation:
1275 	 * ACK must match our initial sequence number + 1 (the SYN|ACK).
1276 	 */
1277 	if (th->th_ack != sc->sc_iss + 1) {
1278 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
1279 			log(LOG_DEBUG, "%s; %s: ACK %u != ISS+1 %u, segment "
1280 			    "rejected\n", s, __func__, th->th_ack, sc->sc_iss);
1281 		goto failed;
1282 	}
1283 
1284 	/*
1285 	 * The SEQ must fall in the window starting at the received
1286 	 * initial receive sequence number + 1 (the SYN).
1287 	 */
1288 	if (SEQ_LEQ(th->th_seq, sc->sc_irs) ||
1289 	    SEQ_GT(th->th_seq, sc->sc_irs + sc->sc_wnd)) {
1290 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
1291 			log(LOG_DEBUG, "%s; %s: SEQ %u != IRS+1 %u, segment "
1292 			    "rejected\n", s, __func__, th->th_seq, sc->sc_irs);
1293 		goto failed;
1294 	}
1295 
1296 	*lsop = syncache_socket(sc, *lsop, m);
1297 
1298 	if (__predict_false(*lsop == NULL)) {
1299 		TCPSTAT_INC(tcps_sc_aborted);
1300 		TCPSTATES_DEC(TCPS_SYN_RECEIVED);
1301 	} else if (sc != &scs)
1302 		TCPSTAT_INC(tcps_sc_completed);
1303 
1304 	if (sc != &scs)
1305 		syncache_free(sc);
1306 	return (1);
1307 failed:
1308 	if (sc != NULL) {
1309 		TCPSTATES_DEC(TCPS_SYN_RECEIVED);
1310 		if (sc != &scs)
1311 			syncache_free(sc);
1312 	}
1313 	if (s != NULL)
1314 		free(s, M_TCPLOG);
1315 	*lsop = NULL;
1316 	return (0);
1317 }
1318 
1319 static struct socket *
1320 syncache_tfo_expand(struct syncache *sc, struct socket *lso, struct mbuf *m,
1321     uint64_t response_cookie)
1322 {
1323 	struct inpcb *inp;
1324 	struct tcpcb *tp;
1325 	unsigned int *pending_counter;
1326 	struct socket *so;
1327 
1328 	NET_EPOCH_ASSERT();
1329 
1330 	pending_counter = intotcpcb(sotoinpcb(lso))->t_tfo_pending;
1331 	so = syncache_socket(sc, lso, m);
1332 	if (so == NULL) {
1333 		TCPSTAT_INC(tcps_sc_aborted);
1334 		atomic_subtract_int(pending_counter, 1);
1335 	} else {
1336 		soisconnected(so);
1337 		inp = sotoinpcb(so);
1338 		tp = intotcpcb(inp);
1339 		tp->t_flags |= TF_FASTOPEN;
1340 		tp->t_tfo_cookie.server = response_cookie;
1341 		tp->snd_max = tp->iss;
1342 		tp->snd_nxt = tp->iss;
1343 		tp->t_tfo_pending = pending_counter;
1344 		TCPSTATES_INC(TCPS_SYN_RECEIVED);
1345 		TCPSTAT_INC(tcps_sc_completed);
1346 	}
1347 
1348 	return (so);
1349 }
1350 
1351 /*
1352  * Given a LISTEN socket and an inbound SYN request, add
1353  * this to the syn cache, and send back a segment:
1354  *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1355  * to the source.
1356  *
1357  * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
1358  * Doing so would require that we hold onto the data and deliver it
1359  * to the application.  However, if we are the target of a SYN-flood
1360  * DoS attack, an attacker could send data which would eventually
1361  * consume all available buffer space if it were ACKed.  By not ACKing
1362  * the data, we avoid this DoS scenario.
1363  *
1364  * The exception to the above is when a SYN with a valid TCP Fast Open (TFO)
1365  * cookie is processed and a new socket is created.  In this case, any data
1366  * accompanying the SYN will be queued to the socket by tcp_input() and will
1367  * be ACKed either when the application sends response data or the delayed
1368  * ACK timer expires, whichever comes first.
1369  */
1370 struct socket *
1371 syncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
1372     struct inpcb *inp, struct socket *so, struct mbuf *m, void *tod,
1373     void *todctx, uint8_t iptos, uint16_t port)
1374 {
1375 	struct tcpcb *tp;
1376 	struct socket *rv = NULL;
1377 	struct syncache *sc = NULL;
1378 	struct syncache_head *sch;
1379 	struct mbuf *ipopts = NULL;
1380 	u_int ltflags;
1381 	int win, ip_ttl, ip_tos;
1382 	char *s;
1383 #ifdef INET6
1384 	int autoflowlabel = 0;
1385 #endif
1386 #ifdef MAC
1387 	struct label *maclabel = NULL;
1388 #endif
1389 	struct syncache scs;
1390 	uint64_t tfo_response_cookie;
1391 	unsigned int *tfo_pending = NULL;
1392 	int tfo_cookie_valid = 0;
1393 	int tfo_response_cookie_valid = 0;
1394 	bool locked;
1395 
1396 	INP_RLOCK_ASSERT(inp);			/* listen socket */
1397 	KASSERT((tcp_get_flags(th) & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN,
1398 	    ("%s: unexpected tcp flags", __func__));
1399 
1400 	/*
1401 	 * Combine all so/tp operations very early to drop the INP lock as
1402 	 * soon as possible.
1403 	 */
1404 	KASSERT(SOLISTENING(so), ("%s: %p not listening", __func__, so));
1405 	tp = sototcpcb(so);
1406 
1407 #ifdef INET6
1408 	if (inc->inc_flags & INC_ISIPV6) {
1409 		if (inp->inp_flags & IN6P_AUTOFLOWLABEL) {
1410 			autoflowlabel = 1;
1411 		}
1412 		ip_ttl = in6_selecthlim(inp, NULL);
1413 		if ((inp->in6p_outputopts == NULL) ||
1414 		    (inp->in6p_outputopts->ip6po_tclass == -1)) {
1415 			ip_tos = 0;
1416 		} else {
1417 			ip_tos = inp->in6p_outputopts->ip6po_tclass;
1418 		}
1419 	}
1420 #endif
1421 #if defined(INET6) && defined(INET)
1422 	else
1423 #endif
1424 #ifdef INET
1425 	{
1426 		ip_ttl = inp->inp_ip_ttl;
1427 		ip_tos = inp->inp_ip_tos;
1428 	}
1429 #endif
1430 	win = so->sol_sbrcv_hiwat;
1431 	ltflags = (tp->t_flags & (TF_NOOPT | TF_SIGNATURE));
1432 
1433 	if (V_tcp_fastopen_server_enable && (tp->t_flags & TF_FASTOPEN) &&
1434 	    (tp->t_tfo_pending != NULL) &&
1435 	    (to->to_flags & TOF_FASTOPEN)) {
1436 		/*
1437 		 * Limit the number of pending TFO connections to
1438 		 * approximately half of the queue limit.  This prevents TFO
1439 		 * SYN floods from starving the service by filling the
1440 		 * listen queue with bogus TFO connections.
1441 		 */
1442 		if (atomic_fetchadd_int(tp->t_tfo_pending, 1) <=
1443 		    (so->sol_qlimit / 2)) {
1444 			int result;
1445 
1446 			result = tcp_fastopen_check_cookie(inc,
1447 			    to->to_tfo_cookie, to->to_tfo_len,
1448 			    &tfo_response_cookie);
1449 			tfo_cookie_valid = (result > 0);
1450 			tfo_response_cookie_valid = (result >= 0);
1451 		}
1452 
1453 		/*
1454 		 * Remember the TFO pending counter as it will have to be
1455 		 * decremented below if we don't make it to syncache_tfo_expand().
1456 		 */
1457 		tfo_pending = tp->t_tfo_pending;
1458 	}
1459 
1460 #ifdef MAC
1461 	if (mac_syncache_init(&maclabel) != 0) {
1462 		INP_RUNLOCK(inp);
1463 		goto done;
1464 	} else
1465 		mac_syncache_create(maclabel, inp);
1466 #endif
1467 	if (!tfo_cookie_valid)
1468 		INP_RUNLOCK(inp);
1469 
1470 	/*
1471 	 * Remember the IP options, if any.
1472 	 */
1473 #ifdef INET6
1474 	if (!(inc->inc_flags & INC_ISIPV6))
1475 #endif
1476 #ifdef INET
1477 		ipopts = (m) ? ip_srcroute(m) : NULL;
1478 #else
1479 		ipopts = NULL;
1480 #endif
1481 
1482 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1483 	/*
1484 	 * When the socket is TCP-MD5 enabled check that,
1485 	 *  - a signed packet is valid
1486 	 *  - a non-signed packet does not have a security association
1487 	 *
1488 	 *  If a signed packet fails validation or a non-signed packet has a
1489 	 *  security association, the packet will be dropped.
1490 	 */
1491 	if (ltflags & TF_SIGNATURE) {
1492 		if (to->to_flags & TOF_SIGNATURE) {
1493 			if (!TCPMD5_ENABLED() ||
1494 			    TCPMD5_INPUT(m, th, to->to_signature) != 0)
1495 				goto done;
1496 		} else {
1497 			if (TCPMD5_ENABLED() &&
1498 			    TCPMD5_INPUT(m, NULL, NULL) != ENOENT)
1499 				goto done;
1500 		}
1501 	} else if (to->to_flags & TOF_SIGNATURE)
1502 		goto done;
1503 #endif	/* TCP_SIGNATURE */
1504 	/*
1505 	 * See if we already have an entry for this connection.
1506 	 * If we do, resend the SYN,ACK, and reset the retransmit timer.
1507 	 *
1508 	 * XXX: should the syncache be re-initialized with the contents
1509 	 * of the new SYN here (which may have different options?)
1510 	 *
1511 	 * XXX: We do not check the sequence number to see if this is a
1512 	 * real retransmit or a new connection attempt.  The question is
1513 	 * how to handle such a case; either ignore it as spoofed, or
1514 	 * drop the current entry and create a new one?
1515 	 */
1516 	if (syncache_cookiesonly()) {
1517 		sc = NULL;
1518 		sch = syncache_hashbucket(inc);
1519 		locked = false;
1520 	} else {
1521 		sc = syncache_lookup(inc, &sch);	/* returns locked sch */
1522 		locked = true;
1523 		SCH_LOCK_ASSERT(sch);
1524 	}
1525 	if (sc != NULL) {
1526 		if (tfo_cookie_valid)
1527 			INP_RUNLOCK(inp);
1528 		TCPSTAT_INC(tcps_sc_dupsyn);
1529 		if (ipopts) {
1530 			/*
1531 			 * If we were remembering a previous source route,
1532 			 * forget it and use the new one we've been given.
1533 			 */
1534 			if (sc->sc_ipopts)
1535 				(void)m_free(sc->sc_ipopts);
1536 			sc->sc_ipopts = ipopts;
1537 		}
1538 		/*
1539 		 * Update timestamp if present.
1540 		 */
1541 		if ((sc->sc_flags & SCF_TIMESTAMP) && (to->to_flags & TOF_TS))
1542 			sc->sc_tsreflect = to->to_tsval;
1543 		else
1544 			sc->sc_flags &= ~SCF_TIMESTAMP;
1545 		/*
1546 		 * Adjust ECN response if needed, e.g. different
1547 		 * IP ECN field, or a fallback by the remote host.
1548 		 */
1549 		if (sc->sc_flags & SCF_ECN_MASK) {
1550 			sc->sc_flags &= ~SCF_ECN_MASK;
1551 			sc->sc_flags |= tcp_ecn_syncache_add(tcp_get_flags(th), iptos);
1552 		}
1553 #ifdef MAC
1554 		/*
1555 		 * Since we have already unconditionally allocated label
1556 		 * storage, free it up.  The syncache entry will already
1557 		 * have an initialized label we can use.
1558 		 */
1559 		mac_syncache_destroy(&maclabel);
1560 #endif
1561 		TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
1562 		/* Retransmit SYN|ACK and reset retransmit count. */
1563 		if ((s = tcp_log_addrs(&sc->sc_inc, th, NULL, NULL))) {
1564 			log(LOG_DEBUG, "%s; %s: Received duplicate SYN, "
1565 			    "resetting timer and retransmitting SYN|ACK\n",
1566 			    s, __func__);
1567 			free(s, M_TCPLOG);
1568 		}
1569 		if (syncache_respond(sc, m, TH_SYN|TH_ACK) == 0) {
1570 			sc->sc_rxmits = 0;
1571 			syncache_timeout(sc, sch, 1);
1572 			TCPSTAT_INC(tcps_sndacks);
1573 			TCPSTAT_INC(tcps_sndtotal);
1574 		} else {
1575 			syncache_drop(sc, sch);
1576 			TCPSTAT_INC(tcps_sc_dropped);
1577 		}
1578 		SCH_UNLOCK(sch);
1579 		goto donenoprobe;
1580 	}
1581 
1582 	KASSERT(sc == NULL, ("sc(%p) != NULL", sc));
1583 	/*
1584 	 * Skip allocating a syncache entry if we are just going to discard
1585 	 * it later.
1586 	 */
1587 	if (!locked || tfo_cookie_valid) {
1588 		bzero(&scs, sizeof(scs));
1589 		sc = &scs;
1590 	} else {
1591 		sc = uma_zalloc(V_tcp_syncache.zone, M_NOWAIT | M_ZERO);
1592 		if (sc == NULL) {
1593 			/*
1594 			 * The zone allocator couldn't provide more entries.
1595 			 * Treat this as if the cache was full; drop the oldest
1596 			 * entry and insert the new one.
1597 			 */
1598 			TCPSTAT_INC(tcps_sc_zonefail);
1599 			sc = TAILQ_LAST(&sch->sch_bucket, sch_head);
1600 			if (sc != NULL) {
1601 				sch->sch_last_overflow = time_uptime;
1602 				syncache_drop(sc, sch);
1603 				syncache_pause(inc);
1604 			}
1605 			sc = uma_zalloc(V_tcp_syncache.zone, M_NOWAIT | M_ZERO);
1606 			if (sc == NULL) {
1607 				if (V_tcp_syncookies) {
1608 					bzero(&scs, sizeof(scs));
1609 					sc = &scs;
1610 				} else {
1611 					KASSERT(locked,
1612 					    ("%s: bucket unexpectedly unlocked",
1613 					    __func__));
1614 					SCH_UNLOCK(sch);
1615 					goto done;
1616 				}
1617 			}
1618 		}
1619 	}
1620 
1621 	KASSERT(sc != NULL, ("sc == NULL"));
1622 	if (!tfo_cookie_valid && tfo_response_cookie_valid)
1623 		sc->sc_tfo_cookie = &tfo_response_cookie;
1624 
1625 	/*
1626 	 * Fill in the syncache values.
1627 	 */
1628 #ifdef MAC
1629 	sc->sc_label = maclabel;
1630 #endif
1631 	/*
1632 	 * sc_cred is only used in syncache_pcblist() to list TCP endpoints in
1633 	 * TCPS_SYN_RECEIVED state when V_tcp_syncache.see_other is false.
1634 	 * Therefore, store the credentials and take a reference count only
1635 	 * when needed:
1636 	 * - sc is allocated from the zone and not using the on stack instance.
1637 	 * - the sysctl variable net.inet.tcp.syncache.see_other is false.
1638 	 * The reference count is decremented when a zone allocated sc is
1639 	 * freed in syncache_free().
1640 	 */
1641 	if (sc != &scs && !V_tcp_syncache.see_other)
1642 		sc->sc_cred = crhold(so->so_cred);
1643 	else
1644 		sc->sc_cred = NULL;
1645 	sc->sc_port = port;
1646 	sc->sc_ipopts = ipopts;
1647 	bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo));
1648 	sc->sc_ip_tos = ip_tos;
1649 	sc->sc_ip_ttl = ip_ttl;
1650 #ifdef TCP_OFFLOAD
1651 	sc->sc_tod = tod;
1652 	sc->sc_todctx = todctx;
1653 #endif
1654 	sc->sc_irs = th->th_seq;
1655 	sc->sc_flags = 0;
1656 	sc->sc_flowlabel = 0;
1657 
1658 	/*
1659 	 * Initial receive window: clip sbspace to [0 .. TCP_MAXWIN].
1660 	 * win was derived from socket earlier in the function.
1661 	 */
1662 	win = imax(win, 0);
1663 	win = imin(win, TCP_MAXWIN);
1664 	sc->sc_wnd = win;
1665 
1666 	if (V_tcp_do_rfc1323 &&
1667 	    !(ltflags & TF_NOOPT)) {
1668 		/*
1669 		 * A timestamp received in a SYN makes
1670 		 * it ok to send timestamp requests and replies.
1671 		 */
1672 		if ((to->to_flags & TOF_TS) && (V_tcp_do_rfc1323 != 2)) {
1673 			sc->sc_tsreflect = to->to_tsval;
1674 			sc->sc_flags |= SCF_TIMESTAMP;
1675 			sc->sc_tsoff = tcp_new_ts_offset(inc);
1676 		}
1677 		if ((to->to_flags & TOF_SCALE) && (V_tcp_do_rfc1323 != 3)) {
1678 			u_int wscale = 0;
1679 
1680 			/*
1681 			 * Pick the smallest possible scaling factor that
1682 			 * will still allow us to scale up to sb_max, aka
1683 			 * kern.ipc.maxsockbuf.
1684 			 *
1685 			 * We do this because there are broken firewalls that
1686 			 * will corrupt the window scale option, leading to
1687 			 * the other endpoint believing that our advertised
1688 			 * window is unscaled.  At scale factors larger than
1689 			 * 5 the unscaled window will drop below 1500 bytes,
1690 			 * leading to serious problems when traversing these
1691 			 * broken firewalls.
1692 			 *
1693 			 * With the default maxsockbuf of 256K, a scale factor
1694 			 * of 3 will be chosen by this algorithm.  Those who
1695 			 * choose a larger maxsockbuf should watch out
1696 			 * for the compatibility problems mentioned above.
1697 			 *
1698 			 * RFC1323: The Window field in a SYN (i.e., a <SYN>
1699 			 * or <SYN,ACK>) segment itself is never scaled.
1700 			 */
1701 			while (wscale < TCP_MAX_WINSHIFT &&
1702 			    (TCP_MAXWIN << wscale) < sb_max)
1703 				wscale++;
1704 			sc->sc_requested_r_scale = wscale;
1705 			sc->sc_requested_s_scale = to->to_wscale;
1706 			sc->sc_flags |= SCF_WINSCALE;
1707 		}
1708 	}
1709 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1710 	/*
1711 	 * If incoming packet has an MD5 signature, flag this in the
1712 	 * syncache so that syncache_respond() will do the right thing
1713 	 * with the SYN+ACK.
1714 	 */
1715 	if (to->to_flags & TOF_SIGNATURE)
1716 		sc->sc_flags |= SCF_SIGNATURE;
1717 #endif	/* TCP_SIGNATURE */
1718 	if (to->to_flags & TOF_SACKPERM)
1719 		sc->sc_flags |= SCF_SACK;
1720 	if (to->to_flags & TOF_MSS)
1721 		sc->sc_peer_mss = to->to_mss;	/* peer mss may be zero */
1722 	if (ltflags & TF_NOOPT)
1723 		sc->sc_flags |= SCF_NOOPT;
1724 	/* ECN Handshake */
1725 	if (V_tcp_do_ecn && (tp->t_flags2 & TF2_CANNOT_DO_ECN) == 0)
1726 		sc->sc_flags |= tcp_ecn_syncache_add(tcp_get_flags(th), iptos);
1727 
1728 	if (V_tcp_syncookies || V_tcp_syncookiesonly)
1729 		sc->sc_iss = syncookie_generate(sch, sc);
1730 	else
1731 		sc->sc_iss = arc4random();
1732 #ifdef INET6
1733 	if (autoflowlabel) {
1734 		if (V_tcp_syncookies || V_tcp_syncookiesonly)
1735 			sc->sc_flowlabel = sc->sc_iss;
1736 		else
1737 			sc->sc_flowlabel = ip6_randomflowlabel();
1738 		sc->sc_flowlabel = htonl(sc->sc_flowlabel) & IPV6_FLOWLABEL_MASK;
1739 	}
1740 #endif
1741 	if (locked)
1742 		SCH_UNLOCK(sch);
1743 
1744 	if (tfo_cookie_valid) {
1745 		rv = syncache_tfo_expand(sc, so, m, tfo_response_cookie);
1746 		/* INP_RUNLOCK(inp) will be performed by the caller */
1747 		goto tfo_expanded;
1748 	}
1749 
1750 	TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
1751 	/*
1752 	 * Do a standard 3-way handshake.
1753 	 */
1754 	if (syncache_respond(sc, m, TH_SYN|TH_ACK) == 0) {
1755 		if (sc != &scs)
1756 			syncache_insert(sc, sch);   /* locks and unlocks sch */
1757 		TCPSTAT_INC(tcps_sndacks);
1758 		TCPSTAT_INC(tcps_sndtotal);
1759 	} else {
1760 		if (sc != &scs)
1761 			syncache_free(sc);
1762 		TCPSTAT_INC(tcps_sc_dropped);
1763 	}
1764 	goto donenoprobe;
1765 
1766 done:
1767 	TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
1768 donenoprobe:
1769 	if (m)
1770 		m_freem(m);
1771 	/*
1772 	 * If tfo_pending is not NULL here, then a TFO SYN that did not
1773 	 * result in a new socket was processed and the associated pending
1774 	 * counter has not yet been decremented.  All such TFO processing paths
1775 	 * transit this point.
1776 	 */
1777 	if (tfo_pending != NULL)
1778 		tcp_fastopen_decrement_counter(tfo_pending);
1779 
1780 tfo_expanded:
1781 	if (sc == NULL || sc == &scs) {
1782 #ifdef MAC
1783 		mac_syncache_destroy(&maclabel);
1784 #endif
1785 		if (ipopts)
1786 			(void)m_free(ipopts);
1787 	}
1788 	return (rv);
1789 }
1790 
1791 /*
1792  * Send SYN|ACK or ACK to the peer.  Either in response to a peer's segment,
1793  * i.e. m0 != NULL, or upon 3WHS ACK timeout, i.e. m0 == NULL.
1794  */
1795 static int
1796 syncache_respond(struct syncache *sc, const struct mbuf *m0, int flags)
1797 {
1798 	struct ip *ip = NULL;
1799 	struct mbuf *m;
1800 	struct tcphdr *th = NULL;
1801 	struct udphdr *udp = NULL;
1802 	int optlen, error = 0;	/* Make compiler happy */
1803 	u_int16_t hlen, tlen, mssopt, ulen;
1804 	struct tcpopt to;
1805 #ifdef INET6
1806 	struct ip6_hdr *ip6 = NULL;
1807 #endif
1808 
1809 	NET_EPOCH_ASSERT();
1810 
1811 	hlen =
1812 #ifdef INET6
1813 	       (sc->sc_inc.inc_flags & INC_ISIPV6) ? sizeof(struct ip6_hdr) :
1814 #endif
1815 		sizeof(struct ip);
1816 	tlen = hlen + sizeof(struct tcphdr);
1817 	if (sc->sc_port) {
1818 		tlen += sizeof(struct udphdr);
1819 	}
1820 	/* Determine MSS we advertize to other end of connection. */
1821 	mssopt = tcp_mssopt(&sc->sc_inc);
1822 	if (sc->sc_port)
1823 		mssopt -= V_tcp_udp_tunneling_overhead;
1824 	mssopt = max(mssopt, V_tcp_minmss);
1825 
1826 	/* XXX: Assume that the entire packet will fit in a header mbuf. */
1827 	KASSERT(max_linkhdr + tlen + TCP_MAXOLEN <= MHLEN,
1828 	    ("syncache: mbuf too small: hlen %u, sc_port %u, max_linkhdr %d + "
1829 	    "tlen %d + TCP_MAXOLEN %ju <= MHLEN %d", hlen, sc->sc_port,
1830 	    max_linkhdr, tlen, (uintmax_t)TCP_MAXOLEN, MHLEN));
1831 
1832 	/* Create the IP+TCP header from scratch. */
1833 	m = m_gethdr(M_NOWAIT, MT_DATA);
1834 	if (m == NULL)
1835 		return (ENOBUFS);
1836 #ifdef MAC
1837 	mac_syncache_create_mbuf(sc->sc_label, m);
1838 #endif
1839 	m->m_data += max_linkhdr;
1840 	m->m_len = tlen;
1841 	m->m_pkthdr.len = tlen;
1842 	m->m_pkthdr.rcvif = NULL;
1843 
1844 #ifdef INET6
1845 	if (sc->sc_inc.inc_flags & INC_ISIPV6) {
1846 		ip6 = mtod(m, struct ip6_hdr *);
1847 		ip6->ip6_vfc = IPV6_VERSION;
1848 		ip6->ip6_src = sc->sc_inc.inc6_laddr;
1849 		ip6->ip6_dst = sc->sc_inc.inc6_faddr;
1850 		ip6->ip6_plen = htons(tlen - hlen);
1851 		/* ip6_hlim is set after checksum */
1852 		/* Zero out traffic class and flow label. */
1853 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
1854 		ip6->ip6_flow |= sc->sc_flowlabel;
1855 		if (sc->sc_port != 0) {
1856 			ip6->ip6_nxt = IPPROTO_UDP;
1857 			udp = (struct udphdr *)(ip6 + 1);
1858 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
1859 			udp->uh_dport = sc->sc_port;
1860 			ulen = (tlen - sizeof(struct ip6_hdr));
1861 			th = (struct tcphdr *)(udp + 1);
1862 		} else {
1863 			ip6->ip6_nxt = IPPROTO_TCP;
1864 			th = (struct tcphdr *)(ip6 + 1);
1865 		}
1866 		ip6->ip6_flow |= htonl(sc->sc_ip_tos << IPV6_FLOWLABEL_LEN);
1867 	}
1868 #endif
1869 #if defined(INET6) && defined(INET)
1870 	else
1871 #endif
1872 #ifdef INET
1873 	{
1874 		ip = mtod(m, struct ip *);
1875 		ip->ip_v = IPVERSION;
1876 		ip->ip_hl = sizeof(struct ip) >> 2;
1877 		ip->ip_len = htons(tlen);
1878 		ip->ip_id = 0;
1879 		ip->ip_off = 0;
1880 		ip->ip_sum = 0;
1881 		ip->ip_src = sc->sc_inc.inc_laddr;
1882 		ip->ip_dst = sc->sc_inc.inc_faddr;
1883 		ip->ip_ttl = sc->sc_ip_ttl;
1884 		ip->ip_tos = sc->sc_ip_tos;
1885 
1886 		/*
1887 		 * See if we should do MTU discovery.  Route lookups are
1888 		 * expensive, so we will only unset the DF bit if:
1889 		 *
1890 		 *	1) path_mtu_discovery is disabled
1891 		 *	2) the SCF_UNREACH flag has been set
1892 		 */
1893 		if (V_path_mtu_discovery && ((sc->sc_flags & SCF_UNREACH) == 0))
1894 		       ip->ip_off |= htons(IP_DF);
1895 		if (sc->sc_port == 0) {
1896 			ip->ip_p = IPPROTO_TCP;
1897 			th = (struct tcphdr *)(ip + 1);
1898 		} else {
1899 			ip->ip_p = IPPROTO_UDP;
1900 			udp = (struct udphdr *)(ip + 1);
1901 			udp->uh_sport = htons(V_tcp_udp_tunneling_port);
1902 			udp->uh_dport = sc->sc_port;
1903 			ulen = (tlen - sizeof(struct ip));
1904 			th = (struct tcphdr *)(udp + 1);
1905 		}
1906 	}
1907 #endif /* INET */
1908 	th->th_sport = sc->sc_inc.inc_lport;
1909 	th->th_dport = sc->sc_inc.inc_fport;
1910 
1911 	if (flags & TH_SYN)
1912 		th->th_seq = htonl(sc->sc_iss);
1913 	else
1914 		th->th_seq = htonl(sc->sc_iss + 1);
1915 	th->th_ack = htonl(sc->sc_irs + 1);
1916 	th->th_off = sizeof(struct tcphdr) >> 2;
1917 	th->th_win = htons(sc->sc_wnd);
1918 	th->th_urp = 0;
1919 
1920 	flags = tcp_ecn_syncache_respond(flags, sc);
1921 	tcp_set_flags(th, flags);
1922 
1923 	/* Tack on the TCP options. */
1924 	if ((sc->sc_flags & SCF_NOOPT) == 0) {
1925 		to.to_flags = 0;
1926 
1927 		if (flags & TH_SYN) {
1928 			to.to_mss = mssopt;
1929 			to.to_flags = TOF_MSS;
1930 			if (sc->sc_flags & SCF_WINSCALE) {
1931 				to.to_wscale = sc->sc_requested_r_scale;
1932 				to.to_flags |= TOF_SCALE;
1933 			}
1934 			if (sc->sc_flags & SCF_SACK)
1935 				to.to_flags |= TOF_SACKPERM;
1936 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1937 			if (sc->sc_flags & SCF_SIGNATURE)
1938 				to.to_flags |= TOF_SIGNATURE;
1939 #endif
1940 			if (sc->sc_tfo_cookie) {
1941 				to.to_flags |= TOF_FASTOPEN;
1942 				to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN;
1943 				to.to_tfo_cookie = sc->sc_tfo_cookie;
1944 				/* don't send cookie again when retransmitting response */
1945 				sc->sc_tfo_cookie = NULL;
1946 			}
1947 		}
1948 		if (sc->sc_flags & SCF_TIMESTAMP) {
1949 			to.to_tsval = sc->sc_tsoff + tcp_ts_getticks();
1950 			to.to_tsecr = sc->sc_tsreflect;
1951 			to.to_flags |= TOF_TS;
1952 		}
1953 		optlen = tcp_addoptions(&to, (u_char *)(th + 1));
1954 
1955 		/* Adjust headers by option size. */
1956 		th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
1957 		m->m_len += optlen;
1958 		m->m_pkthdr.len += optlen;
1959 #ifdef INET6
1960 		if (sc->sc_inc.inc_flags & INC_ISIPV6)
1961 			ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) + optlen);
1962 		else
1963 #endif
1964 			ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1965 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1966 		if (sc->sc_flags & SCF_SIGNATURE) {
1967 			KASSERT(to.to_flags & TOF_SIGNATURE,
1968 			    ("tcp_addoptions() didn't set tcp_signature"));
1969 
1970 			/* NOTE: to.to_signature is inside of mbuf */
1971 			if (!TCPMD5_ENABLED() ||
1972 			    TCPMD5_OUTPUT(m, th, to.to_signature) != 0) {
1973 				m_freem(m);
1974 				return (EACCES);
1975 			}
1976 		}
1977 #endif
1978 	} else
1979 		optlen = 0;
1980 
1981 	if (udp) {
1982 		ulen += optlen;
1983 		udp->uh_ulen = htons(ulen);
1984 	}
1985 	M_SETFIB(m, sc->sc_inc.inc_fibnum);
1986 	/*
1987 	 * If we have peer's SYN and it has a flowid, then let's assign it to
1988 	 * our SYN|ACK.  ip6_output() and ip_output() will not assign flowid
1989 	 * to SYN|ACK due to lack of inp here.
1990 	 */
1991 	if (m0 != NULL && M_HASHTYPE_GET(m0) != M_HASHTYPE_NONE) {
1992 		m->m_pkthdr.flowid = m0->m_pkthdr.flowid;
1993 		M_HASHTYPE_SET(m, M_HASHTYPE_GET(m0));
1994 	}
1995 #ifdef INET6
1996 	if (sc->sc_inc.inc_flags & INC_ISIPV6) {
1997 		if (sc->sc_port) {
1998 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
1999 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2000 			udp->uh_sum = in6_cksum_pseudo(ip6, ulen,
2001 			      IPPROTO_UDP, 0);
2002 			th->th_sum = htons(0);
2003 		} else {
2004 			m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
2005 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2006 			th->th_sum = in6_cksum_pseudo(ip6, tlen + optlen - hlen,
2007 			    IPPROTO_TCP, 0);
2008 		}
2009 		ip6->ip6_hlim = sc->sc_ip_ttl;
2010 #ifdef TCP_OFFLOAD
2011 		if (ADDED_BY_TOE(sc)) {
2012 			struct toedev *tod = sc->sc_tod;
2013 
2014 			error = tod->tod_syncache_respond(tod, sc->sc_todctx, m);
2015 
2016 			return (error);
2017 		}
2018 #endif
2019 		TCP_PROBE5(send, NULL, NULL, ip6, NULL, th);
2020 		error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
2021 	}
2022 #endif
2023 #if defined(INET6) && defined(INET)
2024 	else
2025 #endif
2026 #ifdef INET
2027 	{
2028 		if (sc->sc_port) {
2029 			m->m_pkthdr.csum_flags = CSUM_UDP;
2030 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2031 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
2032 			      ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP));
2033 			th->th_sum = htons(0);
2034 		} else {
2035 			m->m_pkthdr.csum_flags = CSUM_TCP;
2036 			m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2037 			th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2038 			    htons(tlen + optlen - hlen + IPPROTO_TCP));
2039 		}
2040 #ifdef TCP_OFFLOAD
2041 		if (ADDED_BY_TOE(sc)) {
2042 			struct toedev *tod = sc->sc_tod;
2043 
2044 			error = tod->tod_syncache_respond(tod, sc->sc_todctx, m);
2045 
2046 			return (error);
2047 		}
2048 #endif
2049 		TCP_PROBE5(send, NULL, NULL, ip, NULL, th);
2050 		error = ip_output(m, sc->sc_ipopts, NULL, 0, NULL, NULL);
2051 	}
2052 #endif
2053 	return (error);
2054 }
2055 
2056 /*
2057  * The purpose of syncookies is to handle spoofed SYN flooding DoS attacks
2058  * that exceed the capacity of the syncache by avoiding the storage of any
2059  * of the SYNs we receive.  Syncookies defend against blind SYN flooding
2060  * attacks where the attacker does not have access to our responses.
2061  *
2062  * Syncookies encode and include all necessary information about the
2063  * connection setup within the SYN|ACK that we send back.  That way we
2064  * can avoid keeping any local state until the ACK to our SYN|ACK returns
2065  * (if ever).  Normally the syncache and syncookies are running in parallel
2066  * with the latter taking over when the former is exhausted.  When matching
2067  * syncache entry is found the syncookie is ignored.
2068  *
2069  * The only reliable information persisting the 3WHS is our initial sequence
2070  * number ISS of 32 bits.  Syncookies embed a cryptographically sufficient
2071  * strong hash (MAC) value and a few bits of TCP SYN options in the ISS
2072  * of our SYN|ACK.  The MAC can be recomputed when the ACK to our SYN|ACK
2073  * returns and signifies a legitimate connection if it matches the ACK.
2074  *
2075  * The available space of 32 bits to store the hash and to encode the SYN
2076  * option information is very tight and we should have at least 24 bits for
2077  * the MAC to keep the number of guesses by blind spoofing reasonably high.
2078  *
2079  * SYN option information we have to encode to fully restore a connection:
2080  * MSS: is imporant to chose an optimal segment size to avoid IP level
2081  *   fragmentation along the path.  The common MSS values can be encoded
2082  *   in a 3-bit table.  Uncommon values are captured by the next lower value
2083  *   in the table leading to a slight increase in packetization overhead.
2084  * WSCALE: is necessary to allow large windows to be used for high delay-
2085  *   bandwidth product links.  Not scaling the window when it was initially
2086  *   negotiated is bad for performance as lack of scaling further decreases
2087  *   the apparent available send window.  We only need to encode the WSCALE
2088  *   we received from the remote end.  Our end can be recalculated at any
2089  *   time.  The common WSCALE values can be encoded in a 3-bit table.
2090  *   Uncommon values are captured by the next lower value in the table
2091  *   making us under-estimate the available window size halving our
2092  *   theoretically possible maximum throughput for that connection.
2093  * SACK: Greatly assists in packet loss recovery and requires 1 bit.
2094  * TIMESTAMP and SIGNATURE is not encoded because they are permanent options
2095  *   that are included in all segments on a connection.  We enable them when
2096  *   the ACK has them.
2097  *
2098  * Security of syncookies and attack vectors:
2099  *
2100  * The MAC is computed over (faddr||laddr||fport||lport||irs||flags||secmod)
2101  * together with the gloabl secret to make it unique per connection attempt.
2102  * Thus any change of any of those parameters results in a different MAC output
2103  * in an unpredictable way unless a collision is encountered.  24 bits of the
2104  * MAC are embedded into the ISS.
2105  *
2106  * To prevent replay attacks two rotating global secrets are updated with a
2107  * new random value every 15 seconds.  The life-time of a syncookie is thus
2108  * 15-30 seconds.
2109  *
2110  * Vector 1: Attacking the secret.  This requires finding a weakness in the
2111  * MAC itself or the way it is used here.  The attacker can do a chosen plain
2112  * text attack by varying and testing the all parameters under his control.
2113  * The strength depends on the size and randomness of the secret, and the
2114  * cryptographic security of the MAC function.  Due to the constant updating
2115  * of the secret the attacker has at most 29.999 seconds to find the secret
2116  * and launch spoofed connections.  After that he has to start all over again.
2117  *
2118  * Vector 2: Collision attack on the MAC of a single ACK.  With a 24 bit MAC
2119  * size an average of 4,823 attempts are required for a 50% chance of success
2120  * to spoof a single syncookie (birthday collision paradox).  However the
2121  * attacker is blind and doesn't know if one of his attempts succeeded unless
2122  * he has a side channel to interfere success from.  A single connection setup
2123  * success average of 90% requires 8,790 packets, 99.99% requires 17,578 packets.
2124  * This many attempts are required for each one blind spoofed connection.  For
2125  * every additional spoofed connection he has to launch another N attempts.
2126  * Thus for a sustained rate 100 spoofed connections per second approximately
2127  * 1,800,000 packets per second would have to be sent.
2128  *
2129  * NB: The MAC function should be fast so that it doesn't become a CPU
2130  * exhaustion attack vector itself.
2131  *
2132  * References:
2133  *  RFC4987 TCP SYN Flooding Attacks and Common Mitigations
2134  *  SYN cookies were first proposed by cryptographer Dan J. Bernstein in 1996
2135  *   http://cr.yp.to/syncookies.html    (overview)
2136  *   http://cr.yp.to/syncookies/archive (details)
2137  *
2138  *
2139  * Schematic construction of a syncookie enabled Initial Sequence Number:
2140  *  0        1         2         3
2141  *  12345678901234567890123456789012
2142  * |xxxxxxxxxxxxxxxxxxxxxxxxWWWMMMSP|
2143  *
2144  *  x 24 MAC (truncated)
2145  *  W  3 Send Window Scale index
2146  *  M  3 MSS index
2147  *  S  1 SACK permitted
2148  *  P  1 Odd/even secret
2149  */
2150 
2151 /*
2152  * Distribution and probability of certain MSS values.  Those in between are
2153  * rounded down to the next lower one.
2154  * [An Analysis of TCP Maximum Segment Sizes, S. Alcock and R. Nelson, 2011]
2155  *                            .2%  .3%   5%    7%    7%    20%   15%   45%
2156  */
2157 static int tcp_sc_msstab[] = { 216, 536, 1200, 1360, 1400, 1440, 1452, 1460 };
2158 
2159 /*
2160  * Distribution and probability of certain WSCALE values.  We have to map the
2161  * (send) window scale (shift) option with a range of 0-14 from 4 bits into 3
2162  * bits based on prevalence of certain values.  Where we don't have an exact
2163  * match for are rounded down to the next lower one letting us under-estimate
2164  * the true available window.  At the moment this would happen only for the
2165  * very uncommon values 3, 5 and those above 8 (more than 16MB socket buffer
2166  * and window size).  The absence of the WSCALE option (no scaling in either
2167  * direction) is encoded with index zero.
2168  * [WSCALE values histograms, Allman, 2012]
2169  *                            X 10 10 35  5  6 14 10%   by host
2170  *                            X 11  4  5  5 18 49  3%   by connections
2171  */
2172 static int tcp_sc_wstab[] = { 0, 0, 1, 2, 4, 6, 7, 8 };
2173 
2174 /*
2175  * Compute the MAC for the SYN cookie.  SIPHASH-2-4 is chosen for its speed
2176  * and good cryptographic properties.
2177  */
2178 static uint32_t
2179 syncookie_mac(struct in_conninfo *inc, tcp_seq irs, uint8_t flags,
2180     uint8_t *secbits, uintptr_t secmod)
2181 {
2182 	SIPHASH_CTX ctx;
2183 	uint32_t siphash[2];
2184 
2185 	SipHash24_Init(&ctx);
2186 	SipHash_SetKey(&ctx, secbits);
2187 	switch (inc->inc_flags & INC_ISIPV6) {
2188 #ifdef INET
2189 	case 0:
2190 		SipHash_Update(&ctx, &inc->inc_faddr, sizeof(inc->inc_faddr));
2191 		SipHash_Update(&ctx, &inc->inc_laddr, sizeof(inc->inc_laddr));
2192 		break;
2193 #endif
2194 #ifdef INET6
2195 	case INC_ISIPV6:
2196 		SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(inc->inc6_faddr));
2197 		SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(inc->inc6_laddr));
2198 		break;
2199 #endif
2200 	}
2201 	SipHash_Update(&ctx, &inc->inc_fport, sizeof(inc->inc_fport));
2202 	SipHash_Update(&ctx, &inc->inc_lport, sizeof(inc->inc_lport));
2203 	SipHash_Update(&ctx, &irs, sizeof(irs));
2204 	SipHash_Update(&ctx, &flags, sizeof(flags));
2205 	SipHash_Update(&ctx, &secmod, sizeof(secmod));
2206 	SipHash_Final((u_int8_t *)&siphash, &ctx);
2207 
2208 	return (siphash[0] ^ siphash[1]);
2209 }
2210 
2211 static tcp_seq
2212 syncookie_generate(struct syncache_head *sch, struct syncache *sc)
2213 {
2214 	u_int i, secbit, wscale;
2215 	uint32_t iss, hash;
2216 	uint8_t *secbits;
2217 	union syncookie cookie;
2218 
2219 	cookie.cookie = 0;
2220 
2221 	/* Map our computed MSS into the 3-bit index. */
2222 	for (i = nitems(tcp_sc_msstab) - 1;
2223 	     tcp_sc_msstab[i] > sc->sc_peer_mss && i > 0;
2224 	     i--)
2225 		;
2226 	cookie.flags.mss_idx = i;
2227 
2228 	/*
2229 	 * Map the send window scale into the 3-bit index but only if
2230 	 * the wscale option was received.
2231 	 */
2232 	if (sc->sc_flags & SCF_WINSCALE) {
2233 		wscale = sc->sc_requested_s_scale;
2234 		for (i = nitems(tcp_sc_wstab) - 1;
2235 		    tcp_sc_wstab[i] > wscale && i > 0;
2236 		     i--)
2237 			;
2238 		cookie.flags.wscale_idx = i;
2239 	}
2240 
2241 	/* Can we do SACK? */
2242 	if (sc->sc_flags & SCF_SACK)
2243 		cookie.flags.sack_ok = 1;
2244 
2245 	/* Which of the two secrets to use. */
2246 	secbit = V_tcp_syncache.secret.oddeven & 0x1;
2247 	cookie.flags.odd_even = secbit;
2248 
2249 	secbits = V_tcp_syncache.secret.key[secbit];
2250 	hash = syncookie_mac(&sc->sc_inc, sc->sc_irs, cookie.cookie, secbits,
2251 	    (uintptr_t)sch);
2252 
2253 	/*
2254 	 * Put the flags into the hash and XOR them to get better ISS number
2255 	 * variance.  This doesn't enhance the cryptographic strength and is
2256 	 * done to prevent the 8 cookie bits from showing up directly on the
2257 	 * wire.
2258 	 */
2259 	iss = hash & ~0xff;
2260 	iss |= cookie.cookie ^ (hash >> 24);
2261 
2262 	TCPSTAT_INC(tcps_sc_sendcookie);
2263 	return (iss);
2264 }
2265 
2266 static bool
2267 syncookie_expand(struct in_conninfo *inc, const struct syncache_head *sch,
2268     struct syncache *sc, struct tcphdr *th, struct tcpopt *to,
2269     struct socket *lso, uint16_t port)
2270 {
2271 	uint32_t hash;
2272 	uint8_t *secbits;
2273 	tcp_seq ack, seq;
2274 	int wnd;
2275 	union syncookie cookie;
2276 
2277 	/*
2278 	 * Pull information out of SYN-ACK/ACK and revert sequence number
2279 	 * advances.
2280 	 */
2281 	ack = th->th_ack - 1;
2282 	seq = th->th_seq - 1;
2283 
2284 	/*
2285 	 * Unpack the flags containing enough information to restore the
2286 	 * connection.
2287 	 */
2288 	cookie.cookie = (ack & 0xff) ^ (ack >> 24);
2289 
2290 	/* Which of the two secrets to use. */
2291 	secbits = V_tcp_syncache.secret.key[cookie.flags.odd_even];
2292 
2293 	hash = syncookie_mac(inc, seq, cookie.cookie, secbits, (uintptr_t)sch);
2294 
2295 	/* The recomputed hash matches the ACK if this was a genuine cookie. */
2296 	if ((ack & ~0xff) != (hash & ~0xff))
2297 		return (false);
2298 
2299 	/* Fill in the syncache values. */
2300 	sc->sc_flags = 0;
2301 	bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo));
2302 	sc->sc_ipopts = NULL;
2303 
2304 	sc->sc_irs = seq;
2305 	sc->sc_iss = ack;
2306 
2307 	switch (inc->inc_flags & INC_ISIPV6) {
2308 #ifdef INET
2309 	case 0:
2310 		sc->sc_ip_ttl = sotoinpcb(lso)->inp_ip_ttl;
2311 		sc->sc_ip_tos = sotoinpcb(lso)->inp_ip_tos;
2312 		break;
2313 #endif
2314 #ifdef INET6
2315 	case INC_ISIPV6:
2316 		if (sotoinpcb(lso)->inp_flags & IN6P_AUTOFLOWLABEL)
2317 			sc->sc_flowlabel =
2318 			    htonl(sc->sc_iss) & IPV6_FLOWLABEL_MASK;
2319 		break;
2320 #endif
2321 	}
2322 
2323 	sc->sc_peer_mss = tcp_sc_msstab[cookie.flags.mss_idx];
2324 
2325 	/* Only use wscale if it was enabled in the orignal SYN. */
2326 	if (cookie.flags.wscale_idx > 0) {
2327 		u_int wscale = 0;
2328 
2329 		/* Recompute the receive window scale that was sent earlier. */
2330 		while (wscale < TCP_MAX_WINSHIFT &&
2331 		    (TCP_MAXWIN << wscale) < sb_max)
2332 			wscale++;
2333 		sc->sc_requested_r_scale = wscale;
2334 		sc->sc_requested_s_scale = tcp_sc_wstab[cookie.flags.wscale_idx];
2335 		sc->sc_flags |= SCF_WINSCALE;
2336 	}
2337 
2338 	wnd = lso->sol_sbrcv_hiwat;
2339 	wnd = imax(wnd, 0);
2340 	wnd = imin(wnd, TCP_MAXWIN);
2341 	sc->sc_wnd = wnd;
2342 
2343 	if (cookie.flags.sack_ok)
2344 		sc->sc_flags |= SCF_SACK;
2345 
2346 	if (to->to_flags & TOF_TS) {
2347 		sc->sc_flags |= SCF_TIMESTAMP;
2348 		sc->sc_tsreflect = to->to_tsval;
2349 		sc->sc_tsoff = tcp_new_ts_offset(inc);
2350 	}
2351 
2352 	if (to->to_flags & TOF_SIGNATURE)
2353 		sc->sc_flags |= SCF_SIGNATURE;
2354 
2355 	sc->sc_rxmits = 0;
2356 
2357 	sc->sc_port = port;
2358 
2359 	return (true);
2360 }
2361 
2362 #ifdef INVARIANTS
2363 static void
2364 syncookie_cmp(struct in_conninfo *inc, const struct syncache_head *sch,
2365     struct syncache *sc, struct tcphdr *th, struct tcpopt *to,
2366     struct socket *lso, uint16_t port)
2367 {
2368 	struct syncache scs;
2369 	char *s;
2370 
2371 	bzero(&scs, sizeof(scs));
2372 	if (syncookie_expand(inc, sch, &scs, th, to, lso, port) &&
2373 	    (sc->sc_peer_mss != scs.sc_peer_mss ||
2374 	     sc->sc_requested_r_scale != scs.sc_requested_r_scale ||
2375 	     sc->sc_requested_s_scale != scs.sc_requested_s_scale ||
2376 	     (sc->sc_flags & SCF_SACK) != (scs.sc_flags & SCF_SACK))) {
2377 
2378 		if ((s = tcp_log_addrs(inc, th, NULL, NULL)) == NULL)
2379 			return;
2380 
2381 		if (sc->sc_peer_mss != scs.sc_peer_mss)
2382 			log(LOG_DEBUG, "%s; %s: mss different %i vs %i\n",
2383 			    s, __func__, sc->sc_peer_mss, scs.sc_peer_mss);
2384 
2385 		if (sc->sc_requested_r_scale != scs.sc_requested_r_scale)
2386 			log(LOG_DEBUG, "%s; %s: rwscale different %i vs %i\n",
2387 			    s, __func__, sc->sc_requested_r_scale,
2388 			    scs.sc_requested_r_scale);
2389 
2390 		if (sc->sc_requested_s_scale != scs.sc_requested_s_scale)
2391 			log(LOG_DEBUG, "%s; %s: swscale different %i vs %i\n",
2392 			    s, __func__, sc->sc_requested_s_scale,
2393 			    scs.sc_requested_s_scale);
2394 
2395 		if ((sc->sc_flags & SCF_SACK) != (scs.sc_flags & SCF_SACK))
2396 			log(LOG_DEBUG, "%s; %s: SACK different\n", s, __func__);
2397 
2398 		free(s, M_TCPLOG);
2399 	}
2400 }
2401 #endif /* INVARIANTS */
2402 
2403 static void
2404 syncookie_reseed(void *arg)
2405 {
2406 	struct tcp_syncache *sc = arg;
2407 	uint8_t *secbits;
2408 	int secbit;
2409 
2410 	/*
2411 	 * Reseeding the secret doesn't have to be protected by a lock.
2412 	 * It only must be ensured that the new random values are visible
2413 	 * to all CPUs in a SMP environment.  The atomic with release
2414 	 * semantics ensures that.
2415 	 */
2416 	secbit = (sc->secret.oddeven & 0x1) ? 0 : 1;
2417 	secbits = sc->secret.key[secbit];
2418 	arc4rand(secbits, SYNCOOKIE_SECRET_SIZE, 0);
2419 	atomic_add_rel_int(&sc->secret.oddeven, 1);
2420 
2421 	/* Reschedule ourself. */
2422 	callout_schedule(&sc->secret.reseed, SYNCOOKIE_LIFETIME * hz);
2423 }
2424 
2425 /*
2426  * We have overflowed a bucket. Let's pause dealing with the syncache.
2427  * This function will increment the bucketoverflow statistics appropriately
2428  * (once per pause when pausing is enabled; otherwise, once per overflow).
2429  */
2430 static void
2431 syncache_pause(struct in_conninfo *inc)
2432 {
2433 	time_t delta;
2434 	const char *s;
2435 
2436 	/* XXX:
2437 	 * 2. Add sysctl read here so we don't get the benefit of this
2438 	 * change without the new sysctl.
2439 	 */
2440 
2441 	/*
2442 	 * Try an unlocked read. If we already know that another thread
2443 	 * has activated the feature, there is no need to proceed.
2444 	 */
2445 	if (V_tcp_syncache.paused)
2446 		return;
2447 
2448 	/* Are cookied enabled? If not, we can't pause. */
2449 	if (!V_tcp_syncookies) {
2450 		TCPSTAT_INC(tcps_sc_bucketoverflow);
2451 		return;
2452 	}
2453 
2454 	/*
2455 	 * We may be the first thread to find an overflow. Get the lock
2456 	 * and evaluate if we need to take action.
2457 	 */
2458 	mtx_lock(&V_tcp_syncache.pause_mtx);
2459 	if (V_tcp_syncache.paused) {
2460 		mtx_unlock(&V_tcp_syncache.pause_mtx);
2461 		return;
2462 	}
2463 
2464 	/* Activate protection. */
2465 	V_tcp_syncache.paused = true;
2466 	TCPSTAT_INC(tcps_sc_bucketoverflow);
2467 
2468 	/*
2469 	 * Determine the last backoff time. If we are seeing a re-newed
2470 	 * attack within that same time after last reactivating the syncache,
2471 	 * consider it an extension of the same attack.
2472 	 */
2473 	delta = TCP_SYNCACHE_PAUSE_TIME << V_tcp_syncache.pause_backoff;
2474 	if (V_tcp_syncache.pause_until + delta - time_uptime > 0) {
2475 		if (V_tcp_syncache.pause_backoff < TCP_SYNCACHE_MAX_BACKOFF) {
2476 			delta <<= 1;
2477 			V_tcp_syncache.pause_backoff++;
2478 		}
2479 	} else {
2480 		delta = TCP_SYNCACHE_PAUSE_TIME;
2481 		V_tcp_syncache.pause_backoff = 0;
2482 	}
2483 
2484 	/* Log a warning, including IP addresses, if able. */
2485 	if (inc != NULL)
2486 		s = tcp_log_addrs(inc, NULL, NULL, NULL);
2487 	else
2488 		s = (const char *)NULL;
2489 	log(LOG_WARNING, "TCP syncache overflow detected; using syncookies for "
2490 	    "the next %lld seconds%s%s%s\n", (long long)delta,
2491 	    (s != NULL) ? " (last SYN: " : "", (s != NULL) ? s : "",
2492 	    (s != NULL) ? ")" : "");
2493 	free(__DECONST(void *, s), M_TCPLOG);
2494 
2495 	/* Use the calculated delta to set a new pause time. */
2496 	V_tcp_syncache.pause_until = time_uptime + delta;
2497 	callout_reset(&V_tcp_syncache.pause_co, delta * hz, syncache_unpause,
2498 	    &V_tcp_syncache);
2499 	mtx_unlock(&V_tcp_syncache.pause_mtx);
2500 }
2501 
2502 /* Evaluate whether we need to unpause. */
2503 static void
2504 syncache_unpause(void *arg)
2505 {
2506 	struct tcp_syncache *sc;
2507 	time_t delta;
2508 
2509 	sc = arg;
2510 	mtx_assert(&sc->pause_mtx, MA_OWNED | MA_NOTRECURSED);
2511 	callout_deactivate(&sc->pause_co);
2512 
2513 	/*
2514 	 * Check to make sure we are not running early. If the pause
2515 	 * time has expired, then deactivate the protection.
2516 	 */
2517 	if ((delta = sc->pause_until - time_uptime) > 0)
2518 		callout_schedule(&sc->pause_co, delta * hz);
2519 	else
2520 		sc->paused = false;
2521 }
2522 
2523 /*
2524  * Exports the syncache entries to userland so that netstat can display
2525  * them alongside the other sockets.  This function is intended to be
2526  * called only from tcp_pcblist.
2527  *
2528  * Due to concurrency on an active system, the number of pcbs exported
2529  * may have no relation to max_pcbs.  max_pcbs merely indicates the
2530  * amount of space the caller allocated for this function to use.
2531  */
2532 int
2533 syncache_pcblist(struct sysctl_req *req)
2534 {
2535 	struct xtcpcb xt;
2536 	struct syncache *sc;
2537 	struct syncache_head *sch;
2538 	int error, i;
2539 
2540 	bzero(&xt, sizeof(xt));
2541 	xt.xt_len = sizeof(xt);
2542 	xt.t_state = TCPS_SYN_RECEIVED;
2543 	xt.xt_inp.xi_socket.xso_protocol = IPPROTO_TCP;
2544 	xt.xt_inp.xi_socket.xso_len = sizeof (struct xsocket);
2545 	xt.xt_inp.xi_socket.so_type = SOCK_STREAM;
2546 	xt.xt_inp.xi_socket.so_state = SS_ISCONNECTING;
2547 
2548 	for (i = 0; i < V_tcp_syncache.hashsize; i++) {
2549 		sch = &V_tcp_syncache.hashbase[i];
2550 		SCH_LOCK(sch);
2551 		TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
2552 			if (sc->sc_cred != NULL &&
2553 			    cr_cansee(req->td->td_ucred, sc->sc_cred) != 0)
2554 				continue;
2555 			if (sc->sc_inc.inc_flags & INC_ISIPV6)
2556 				xt.xt_inp.inp_vflag = INP_IPV6;
2557 			else
2558 				xt.xt_inp.inp_vflag = INP_IPV4;
2559 			xt.xt_encaps_port = sc->sc_port;
2560 			bcopy(&sc->sc_inc, &xt.xt_inp.inp_inc,
2561 			    sizeof (struct in_conninfo));
2562 			error = SYSCTL_OUT(req, &xt, sizeof xt);
2563 			if (error) {
2564 				SCH_UNLOCK(sch);
2565 				return (0);
2566 			}
2567 		}
2568 		SCH_UNLOCK(sch);
2569 	}
2570 
2571 	return (0);
2572 }
2573