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