2 * Copyright (c) 2001 McAfee, Inc.
3 * Copyright (c) 2006 Andre Oppermann, Internet Business Solutions AG
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
49 #include <sys/proc.h> /* for proc0 declaration */
50 #include <sys/random.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/syslog.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_options.h>
68 #include <netinet/ip6.h>
69 #include <netinet/icmp6.h>
70 #include <netinet6/nd6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/in6_pcb.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_fsm.h>
76 #include <netinet/tcp_seq.h>
77 #include <netinet/tcp_timer.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/tcp_syncache.h>
81 #include <netinet6/tcp6_var.h>
85 #include <netipsec/ipsec.h>
87 #include <netipsec/ipsec6.h>
89 #include <netipsec/key.h>
92 #include <machine/in_cksum.h>
94 #include <security/mac/mac_framework.h>
96 static int tcp_syncookies = 1;
97 SYSCTL_INT(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_RW,
99 "Use TCP SYN cookies if the syncache overflows");
101 static int tcp_syncookiesonly = 0;
102 SYSCTL_INT(_net_inet_tcp, OID_AUTO, syncookies_only, CTLFLAG_RW,
103 &tcp_syncookiesonly, 0,
104 "Use only TCP SYN cookies");
106 #define SYNCOOKIE_SECRET_SIZE 8 /* dwords */
107 #define SYNCOOKIE_LIFETIME 16 /* seconds */
110 TAILQ_ENTRY(syncache) sc_hash;
111 struct in_conninfo sc_inc; /* addresses */
112 u_long sc_rxttime; /* retransmit time */
113 u_int16_t sc_rxmits; /* retransmit counter */
115 u_int32_t sc_tsreflect; /* timestamp to reflect */
116 u_int32_t sc_ts; /* our timestamp to send */
117 u_int32_t sc_tsoff; /* ts offset w/ syncookies */
118 u_int32_t sc_flowlabel; /* IPv6 flowlabel */
119 tcp_seq sc_irs; /* seq from peer */
120 tcp_seq sc_iss; /* our ISS */
121 struct mbuf *sc_ipopts; /* source route */
123 u_int16_t sc_peer_mss; /* peer's MSS */
124 u_int16_t sc_wnd; /* advertised window */
125 u_int8_t sc_ip_ttl; /* IPv4 TTL */
126 u_int8_t sc_ip_tos; /* IPv4 TOS */
127 u_int8_t sc_requested_s_scale:4,
128 sc_requested_r_scale:4;
130 #define SCF_NOOPT 0x01 /* no TCP options */
131 #define SCF_WINSCALE 0x02 /* negotiated window scaling */
132 #define SCF_TIMESTAMP 0x04 /* negotiated timestamps */
133 /* MSS is implicit */
134 #define SCF_UNREACH 0x10 /* icmp unreachable received */
135 #define SCF_SIGNATURE 0x20 /* send MD5 digests */
136 #define SCF_SACK 0x80 /* send SACK option */
138 struct label *sc_label; /* MAC label reference */
142 struct syncache_head {
144 TAILQ_HEAD(sch_head, syncache) sch_bucket;
145 struct callout sch_timer;
149 u_int32_t sch_secbits_odd[SYNCOOKIE_SECRET_SIZE];
150 u_int32_t sch_secbits_even[SYNCOOKIE_SECRET_SIZE];
151 u_int sch_reseed; /* time_uptime, seconds */
154 static void syncache_drop(struct syncache *, struct syncache_head *);
155 static void syncache_free(struct syncache *);
156 static void syncache_insert(struct syncache *, struct syncache_head *);
157 struct syncache *syncache_lookup(struct in_conninfo *, struct syncache_head **);
158 static int syncache_respond(struct syncache *);
159 static struct socket *syncache_socket(struct syncache *, struct socket *,
161 static void syncache_timeout(struct syncache *sc, struct syncache_head *sch,
163 static void syncache_timer(void *);
164 static void syncookie_generate(struct syncache_head *, struct syncache *,
166 static struct syncache
167 *syncookie_lookup(struct in_conninfo *, struct syncache_head *,
168 struct syncache *, struct tcpopt *, struct tcphdr *,
172 * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies.
173 * 3 retransmits corresponds to a timeout of (1 + 2 + 4 + 8 == 15) seconds,
174 * the odds are that the user has given up attempting to connect by then.
176 #define SYNCACHE_MAXREXMTS 3
178 /* Arbitrary values */
179 #define TCP_SYNCACHE_HASHSIZE 512
180 #define TCP_SYNCACHE_BUCKETLIMIT 30
182 struct tcp_syncache {
183 struct syncache_head *hashbase;
188 u_int cache_count; /* XXX: unprotected */
193 static struct tcp_syncache tcp_syncache;
195 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache");
197 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
198 &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache");
200 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
201 &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache");
203 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD,
204 &tcp_syncache.cache_count, 0, "Current number of entries in syncache");
206 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
207 &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable");
209 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW,
210 &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions");
212 int tcp_sc_rst_sock_fail = 1;
213 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rst_on_sock_fail, CTLFLAG_RW,
214 &tcp_sc_rst_sock_fail, 0, "Send reset on socket allocation failure");
216 static MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache");
218 #define SYNCACHE_HASH(inc, mask) \
219 ((tcp_syncache.hash_secret ^ \
220 (inc)->inc_faddr.s_addr ^ \
221 ((inc)->inc_faddr.s_addr >> 16) ^ \
222 (inc)->inc_fport ^ (inc)->inc_lport) & mask)
224 #define SYNCACHE_HASH6(inc, mask) \
225 ((tcp_syncache.hash_secret ^ \
226 (inc)->inc6_faddr.s6_addr32[0] ^ \
227 (inc)->inc6_faddr.s6_addr32[3] ^ \
228 (inc)->inc_fport ^ (inc)->inc_lport) & mask)
230 #define ENDPTS_EQ(a, b) ( \
231 (a)->ie_fport == (b)->ie_fport && \
232 (a)->ie_lport == (b)->ie_lport && \
233 (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \
234 (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \
237 #define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0)
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)
244 * Requires the syncache entry to be already removed from the bucket list.
247 syncache_free(struct syncache *sc)
250 (void) m_free(sc->sc_ipopts);
252 mac_destroy_syncache(&sc->sc_label);
255 uma_zfree(tcp_syncache.zone, sc);
263 tcp_syncache.cache_count = 0;
264 tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
265 tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT;
266 tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS;
267 tcp_syncache.hash_secret = arc4random();
269 TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize",
270 &tcp_syncache.hashsize);
271 TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit",
272 &tcp_syncache.bucket_limit);
273 if (!powerof2(tcp_syncache.hashsize) || tcp_syncache.hashsize == 0) {
274 printf("WARNING: syncache hash size is not a power of 2.\n");
275 tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
277 tcp_syncache.hashmask = tcp_syncache.hashsize - 1;
280 tcp_syncache.cache_limit =
281 tcp_syncache.hashsize * tcp_syncache.bucket_limit;
282 TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit",
283 &tcp_syncache.cache_limit);
285 /* Allocate the hash table. */
286 MALLOC(tcp_syncache.hashbase, struct syncache_head *,
287 tcp_syncache.hashsize * sizeof(struct syncache_head),
288 M_SYNCACHE, M_WAITOK | M_ZERO);
290 /* Initialize the hash buckets. */
291 for (i = 0; i < tcp_syncache.hashsize; i++) {
292 TAILQ_INIT(&tcp_syncache.hashbase[i].sch_bucket);
293 mtx_init(&tcp_syncache.hashbase[i].sch_mtx, "tcp_sc_head",
295 callout_init_mtx(&tcp_syncache.hashbase[i].sch_timer,
296 &tcp_syncache.hashbase[i].sch_mtx, 0);
297 tcp_syncache.hashbase[i].sch_length = 0;
300 /* Create the syncache entry zone. */
301 tcp_syncache.zone = uma_zcreate("syncache", sizeof(struct syncache),
302 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
303 uma_zone_set_max(tcp_syncache.zone, tcp_syncache.cache_limit);
307 * Inserts a syncache entry into the specified bucket row.
308 * Locks and unlocks the syncache_head autonomously.
311 syncache_insert(struct syncache *sc, struct syncache_head *sch)
313 struct syncache *sc2;
318 * Make sure that we don't overflow the per-bucket limit.
319 * If the bucket is full, toss the oldest element.
321 if (sch->sch_length >= tcp_syncache.bucket_limit) {
322 KASSERT(!TAILQ_EMPTY(&sch->sch_bucket),
323 ("sch->sch_length incorrect"));
324 sc2 = TAILQ_LAST(&sch->sch_bucket, sch_head);
325 syncache_drop(sc2, sch);
326 tcpstat.tcps_sc_bucketoverflow++;
329 /* Put it into the bucket. */
330 TAILQ_INSERT_HEAD(&sch->sch_bucket, sc, sc_hash);
333 /* Reinitialize the bucket row's timer. */
334 syncache_timeout(sc, sch, 1);
338 tcp_syncache.cache_count++;
339 tcpstat.tcps_sc_added++;
343 * Remove and free entry from syncache bucket row.
344 * Expects locked syncache head.
347 syncache_drop(struct syncache *sc, struct syncache_head *sch)
350 SCH_LOCK_ASSERT(sch);
352 TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
356 tcp_syncache.cache_count--;
360 * Engage/reengage time on bucket row.
363 syncache_timeout(struct syncache *sc, struct syncache_head *sch, int docallout)
365 sc->sc_rxttime = ticks +
366 TCPTV_RTOBASE * (tcp_backoff[sc->sc_rxmits]);
368 if (sch->sch_nextc > sc->sc_rxttime)
369 sch->sch_nextc = sc->sc_rxttime;
370 if (!TAILQ_EMPTY(&sch->sch_bucket) && docallout)
371 callout_reset(&sch->sch_timer, sch->sch_nextc - ticks,
372 syncache_timer, (void *)sch);
376 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
377 * If we have retransmitted an entry the maximum number of times, expire it.
378 * One separate timer for each bucket row.
381 syncache_timer(void *xsch)
383 struct syncache_head *sch = (struct syncache_head *)xsch;
384 struct syncache *sc, *nsc;
388 /* NB: syncache_head has already been locked by the callout. */
389 SCH_LOCK_ASSERT(sch);
391 TAILQ_FOREACH_SAFE(sc, &sch->sch_bucket, sc_hash, nsc) {
393 * We do not check if the listen socket still exists
394 * and accept the case where the listen socket may be
395 * gone by the time we resend the SYN/ACK. We do
396 * not expect this to happens often. If it does,
397 * then the RST will be sent by the time the remote
398 * host does the SYN/ACK->ACK.
400 if (sc->sc_rxttime > tick) {
401 if (sc->sc_rxttime < sch->sch_nextc)
402 sch->sch_nextc = sc->sc_rxttime;
406 if (sc->sc_rxmits > tcp_syncache.rexmt_limit) {
407 if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
408 log(LOG_DEBUG, "%s; %s: Retransmits exhausted, "
409 "giving up and removing syncache entry\n",
413 syncache_drop(sc, sch);
414 tcpstat.tcps_sc_stale++;
417 if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
418 log(LOG_DEBUG, "%s; %s: Response timeout, "
419 "retransmitting (%u) SYN|ACK\n",
420 s, __func__, sc->sc_rxmits);
424 (void) syncache_respond(sc);
425 tcpstat.tcps_sc_retransmitted++;
426 syncache_timeout(sc, sch, 0);
428 if (!TAILQ_EMPTY(&(sch)->sch_bucket))
429 callout_reset(&(sch)->sch_timer, (sch)->sch_nextc - tick,
430 syncache_timer, (void *)(sch));
434 * Find an entry in the syncache.
435 * Returns always with locked syncache_head plus a matching entry or NULL.
438 syncache_lookup(struct in_conninfo *inc, struct syncache_head **schp)
441 struct syncache_head *sch;
444 if (inc->inc_isipv6) {
445 sch = &tcp_syncache.hashbase[
446 SYNCACHE_HASH6(inc, tcp_syncache.hashmask)];
451 /* Circle through bucket row to find matching entry. */
452 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
453 if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
459 sch = &tcp_syncache.hashbase[
460 SYNCACHE_HASH(inc, tcp_syncache.hashmask)];
465 /* Circle through bucket row to find matching entry. */
466 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
468 if (sc->sc_inc.inc_isipv6)
471 if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
475 SCH_LOCK_ASSERT(*schp);
476 return (NULL); /* always returns with locked sch */
480 * This function is called when we get a RST for a
481 * non-existent connection, so that we can see if the
482 * connection is in the syn cache. If it is, zap it.
485 syncache_chkrst(struct in_conninfo *inc, struct tcphdr *th)
488 struct syncache_head *sch;
491 sc = syncache_lookup(inc, &sch); /* returns locked sch */
492 SCH_LOCK_ASSERT(sch);
495 * Any RST to our SYN|ACK must not carry ACK, SYN or FIN flags.
496 * See RFC 793 page 65, section SEGMENT ARRIVES.
498 if (th->th_flags & (TH_ACK|TH_SYN|TH_FIN)) {
499 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
500 log(LOG_DEBUG, "%s; %s: Spurious RST with ACK, SYN or "
501 "FIN flag set, segment ignored\n", s, __func__);
502 tcpstat.tcps_badrst++;
507 * No corresponding connection was found in syncache.
508 * If syncookies are enabled and possibly exclusively
509 * used, or we are under memory pressure, a valid RST
510 * may not find a syncache entry. In that case we're
511 * done and no SYN|ACK retransmissions will happen.
512 * Otherwise the the RST was misdirected or spoofed.
515 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
516 log(LOG_DEBUG, "%s; %s: Spurious RST without matching "
517 "syncache entry (possibly syncookie only), "
518 "segment ignored\n", s, __func__);
519 tcpstat.tcps_badrst++;
524 * If the RST bit is set, check the sequence number to see
525 * if this is a valid reset segment.
527 * In all states except SYN-SENT, all reset (RST) segments
528 * are validated by checking their SEQ-fields. A reset is
529 * valid if its sequence number is in the window.
531 * The sequence number in the reset segment is normally an
532 * echo of our outgoing acknowlegement numbers, but some hosts
533 * send a reset with the sequence number at the rightmost edge
534 * of our receive window, and we have to handle this case.
536 if (SEQ_GEQ(th->th_seq, sc->sc_irs) &&
537 SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) {
538 syncache_drop(sc, sch);
539 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
540 log(LOG_DEBUG, "%s; %s: Our SYN|ACK was rejected, "
541 "connection attempt aborted by remote endpoint\n",
543 tcpstat.tcps_sc_reset++;
544 } else if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
545 log(LOG_DEBUG, "%s; %s: RST with invalid SEQ %u != IRS %u "
546 "(+WND %u), segment ignored\n",
547 s, __func__, th->th_seq, sc->sc_irs, sc->sc_wnd);
548 tcpstat.tcps_badrst++;
558 syncache_badack(struct in_conninfo *inc)
561 struct syncache_head *sch;
563 sc = syncache_lookup(inc, &sch); /* returns locked sch */
564 SCH_LOCK_ASSERT(sch);
566 syncache_drop(sc, sch);
567 tcpstat.tcps_sc_badack++;
573 syncache_unreach(struct in_conninfo *inc, struct tcphdr *th)
576 struct syncache_head *sch;
578 sc = syncache_lookup(inc, &sch); /* returns locked sch */
579 SCH_LOCK_ASSERT(sch);
583 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
584 if (ntohl(th->th_seq) != sc->sc_iss)
588 * If we've rertransmitted 3 times and this is our second error,
589 * we remove the entry. Otherwise, we allow it to continue on.
590 * This prevents us from incorrectly nuking an entry during a
591 * spurious network outage.
595 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxmits < 3 + 1) {
596 sc->sc_flags |= SCF_UNREACH;
599 syncache_drop(sc, sch);
600 tcpstat.tcps_sc_unreach++;
606 * Build a new TCP socket structure from a syncache entry.
608 static struct socket *
609 syncache_socket(struct syncache *sc, struct socket *lso, struct mbuf *m)
611 struct inpcb *inp = NULL;
616 INP_INFO_WLOCK_ASSERT(&tcbinfo);
619 * Ok, create the full blown connection, and set things up
620 * as they would have been set up if we had created the
621 * connection when the SYN arrived. If we can't create
622 * the connection, abort it.
624 so = sonewconn(lso, SS_ISCONNECTED);
627 * Drop the connection; we will either send a RST or
628 * have the peer retransmit its SYN again after its
631 tcpstat.tcps_listendrop++;
632 if ((s = tcp_log_addrs(&sc->sc_inc, NULL, NULL, NULL))) {
633 log(LOG_DEBUG, "%s; %s: Socket create failed "
634 "due to limits or memory shortage\n",
642 mac_set_socket_peer_from_mbuf(m, so);
649 /* Insert new socket into PCB hash list. */
650 inp->inp_inc.inc_isipv6 = sc->sc_inc.inc_isipv6;
652 if (sc->sc_inc.inc_isipv6) {
653 inp->in6p_laddr = sc->sc_inc.inc6_laddr;
655 inp->inp_vflag &= ~INP_IPV6;
656 inp->inp_vflag |= INP_IPV4;
658 inp->inp_laddr = sc->sc_inc.inc_laddr;
662 inp->inp_lport = sc->sc_inc.inc_lport;
663 if (in_pcbinshash(inp) != 0) {
665 * Undo the assignments above if we failed to
666 * put the PCB on the hash lists.
669 if (sc->sc_inc.inc_isipv6)
670 inp->in6p_laddr = in6addr_any;
673 inp->inp_laddr.s_addr = INADDR_ANY;
678 /* Copy old policy into new socket's. */
679 if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp))
680 printf("syncache_socket: could not copy policy\n");
683 if (sc->sc_inc.inc_isipv6) {
684 struct inpcb *oinp = sotoinpcb(lso);
685 struct in6_addr laddr6;
686 struct sockaddr_in6 sin6;
688 * Inherit socket options from the listening socket.
689 * Note that in6p_inputopts are not (and should not be)
690 * copied, since it stores previously received options and is
691 * used to detect if each new option is different than the
692 * previous one and hence should be passed to a user.
693 * If we copied in6p_inputopts, a user would not be able to
694 * receive options just after calling the accept system call.
696 inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS;
697 if (oinp->in6p_outputopts)
698 inp->in6p_outputopts =
699 ip6_copypktopts(oinp->in6p_outputopts, M_NOWAIT);
701 sin6.sin6_family = AF_INET6;
702 sin6.sin6_len = sizeof(sin6);
703 sin6.sin6_addr = sc->sc_inc.inc6_faddr;
704 sin6.sin6_port = sc->sc_inc.inc_fport;
705 sin6.sin6_flowinfo = sin6.sin6_scope_id = 0;
706 laddr6 = inp->in6p_laddr;
707 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
708 inp->in6p_laddr = sc->sc_inc.inc6_laddr;
709 if (in6_pcbconnect(inp, (struct sockaddr *)&sin6,
711 inp->in6p_laddr = laddr6;
714 /* Override flowlabel from in6_pcbconnect. */
715 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
716 inp->in6p_flowinfo |= sc->sc_flowlabel;
720 struct in_addr laddr;
721 struct sockaddr_in sin;
723 inp->inp_options = ip_srcroute(m);
724 if (inp->inp_options == NULL) {
725 inp->inp_options = sc->sc_ipopts;
726 sc->sc_ipopts = NULL;
729 sin.sin_family = AF_INET;
730 sin.sin_len = sizeof(sin);
731 sin.sin_addr = sc->sc_inc.inc_faddr;
732 sin.sin_port = sc->sc_inc.inc_fport;
733 bzero((caddr_t)sin.sin_zero, sizeof(sin.sin_zero));
734 laddr = inp->inp_laddr;
735 if (inp->inp_laddr.s_addr == INADDR_ANY)
736 inp->inp_laddr = sc->sc_inc.inc_laddr;
737 if (in_pcbconnect(inp, (struct sockaddr *)&sin,
739 inp->inp_laddr = laddr;
744 tp->t_state = TCPS_SYN_RECEIVED;
745 tp->iss = sc->sc_iss;
746 tp->irs = sc->sc_irs;
749 tp->snd_wl1 = sc->sc_irs;
750 tp->snd_max = tp->iss + 1;
751 tp->snd_nxt = tp->iss + 1;
752 tp->rcv_up = sc->sc_irs + 1;
753 tp->rcv_wnd = sc->sc_wnd;
754 tp->rcv_adv += tp->rcv_wnd;
755 tp->last_ack_sent = tp->rcv_nxt;
757 tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH|TF_NODELAY);
758 if (sc->sc_flags & SCF_NOOPT)
759 tp->t_flags |= TF_NOOPT;
761 if (sc->sc_flags & SCF_WINSCALE) {
762 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
763 tp->snd_scale = sc->sc_requested_s_scale;
764 tp->request_r_scale = sc->sc_requested_r_scale;
766 if (sc->sc_flags & SCF_TIMESTAMP) {
767 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
768 tp->ts_recent = sc->sc_tsreflect;
769 tp->ts_recent_age = ticks;
770 tp->ts_offset = sc->sc_tsoff;
773 if (sc->sc_flags & SCF_SIGNATURE)
774 tp->t_flags |= TF_SIGNATURE;
776 if (sc->sc_flags & SCF_SACK)
777 tp->t_flags |= TF_SACK_PERMIT;
781 * Set up MSS and get cached values from tcp_hostcache.
782 * This might overwrite some of the defaults we just set.
784 tcp_mss(tp, sc->sc_peer_mss);
787 * If the SYN,ACK was retransmitted, reset cwnd to 1 segment.
790 tp->snd_cwnd = tp->t_maxseg;
791 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
795 tcpstat.tcps_accepts++;
807 * This function gets called when we receive an ACK for a
808 * socket in the LISTEN state. We look up the connection
809 * in the syncache, and if its there, we pull it out of
810 * the cache and turn it into a full-blown connection in
811 * the SYN-RECEIVED state.
814 syncache_expand(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
815 struct socket **lsop, struct mbuf *m)
818 struct syncache_head *sch;
823 * Global TCP locks are held because we manipulate the PCB lists
824 * and create a new socket.
826 INP_INFO_WLOCK_ASSERT(&tcbinfo);
827 KASSERT((th->th_flags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK,
828 ("%s: can handle only ACK", __func__));
830 sc = syncache_lookup(inc, &sch); /* returns locked sch */
831 SCH_LOCK_ASSERT(sch);
834 * There is no syncache entry, so see if this ACK is
835 * a returning syncookie. To do this, first:
836 * A. See if this socket has had a syncache entry dropped in
837 * the past. We don't want to accept a bogus syncookie
838 * if we've never received a SYN.
839 * B. check that the syncookie is valid. If it is, then
840 * cobble up a fake syncache entry, and return.
842 if (!tcp_syncookies) {
844 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
845 log(LOG_DEBUG, "%s; %s: Spurious ACK, "
846 "segment rejected (syncookies disabled)\n",
850 bzero(&scs, sizeof(scs));
851 sc = syncookie_lookup(inc, sch, &scs, to, th, *lsop);
854 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
855 log(LOG_DEBUG, "%s; %s: Segment failed "
856 "SYNCOOKIE authentication, segment rejected "
857 "(probably spoofed)\n", s, __func__);
861 /* Pull out the entry to unlock the bucket row. */
862 TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
864 tcp_syncache.cache_count--;
869 * Segment validation:
870 * ACK must match our initial sequence number + 1 (the SYN|ACK).
872 if (th->th_ack != sc->sc_iss + 1) {
873 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
874 log(LOG_DEBUG, "%s; %s: ACK %u != ISS+1 %u, segment "
875 "rejected\n", s, __func__, th->th_ack, sc->sc_iss);
879 * The SEQ must match the received initial receive sequence
880 * number + 1 (the SYN) because we didn't ACK any data that
881 * may have come with the SYN.
883 if (th->th_seq != sc->sc_irs + 1) {
884 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
885 log(LOG_DEBUG, "%s; %s: SEQ %u != IRS+1 %u, segment "
886 "rejected\n", s, __func__, th->th_seq, sc->sc_irs);
890 * If timestamps were present in the SYN and we accepted
891 * them in our SYN|ACK we require them to be present from
892 * now on. And vice versa.
894 if ((sc->sc_flags & SCF_TIMESTAMP) && !(to->to_flags & TOF_TS)) {
895 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
896 log(LOG_DEBUG, "%s; %s: Timestamp missing, "
897 "segment rejected\n", s, __func__);
900 if (!(sc->sc_flags & SCF_TIMESTAMP) && (to->to_flags & TOF_TS)) {
901 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
902 log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
903 "segment rejected\n", s, __func__);
907 * If timestamps were negotiated the reflected timestamp
908 * must be equal to what we actually sent in the SYN|ACK.
910 if ((to->to_flags & TOF_TS) && to->to_tsecr != sc->sc_ts) {
911 if ((s = tcp_log_addrs(inc, th, NULL, NULL)))
912 log(LOG_DEBUG, "%s; %s: TSECR %u != TS %u, "
913 "segment rejected\n",
914 s, __func__, to->to_tsecr, sc->sc_ts);
918 *lsop = syncache_socket(sc, *lsop, m);
921 tcpstat.tcps_sc_aborted++;
923 tcpstat.tcps_sc_completed++;
929 if (sc != NULL && sc != &scs)
938 * Given a LISTEN socket and an inbound SYN request, add
939 * this to the syn cache, and send back a segment:
940 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
943 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
944 * Doing so would require that we hold onto the data and deliver it
945 * to the application. However, if we are the target of a SYN-flood
946 * DoS attack, an attacker could send data which would eventually
947 * consume all available buffer space if it were ACKed. By not ACKing
948 * the data, we avoid this DoS scenario.
951 syncache_add(struct in_conninfo *inc, struct tcpopt *to, struct tcphdr *th,
952 struct inpcb *inp, struct socket **lsop, struct mbuf *m)
956 struct syncache *sc = NULL;
957 struct syncache_head *sch;
958 struct mbuf *ipopts = NULL;
960 int win, sb_hiwat, ip_ttl, ip_tos, noopt;
963 int autoflowlabel = 0;
966 struct label *maclabel;
970 INP_INFO_WLOCK_ASSERT(&tcbinfo);
971 INP_LOCK_ASSERT(inp); /* listen socket */
972 KASSERT((th->th_flags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN,
973 ("%s: unexpected tcp flags", __func__));
976 * Combine all so/tp operations very early to drop the INP lock as
983 if (inc->inc_isipv6 &&
984 (inp->in6p_flags & IN6P_AUTOFLOWLABEL))
987 ip_ttl = inp->inp_ip_ttl;
988 ip_tos = inp->inp_ip_tos;
989 win = sbspace(&so->so_rcv);
990 sb_hiwat = so->so_rcv.sb_hiwat;
991 noopt = (tp->t_flags & TF_NOOPT);
997 if (mac_init_syncache(&maclabel) != 0) {
999 INP_INFO_WUNLOCK(&tcbinfo);
1002 mac_init_syncache_from_inpcb(maclabel, inp);
1005 INP_INFO_WUNLOCK(&tcbinfo);
1008 * Remember the IP options, if any.
1011 if (!inc->inc_isipv6)
1013 ipopts = ip_srcroute(m);
1016 * See if we already have an entry for this connection.
1017 * If we do, resend the SYN,ACK, and reset the retransmit timer.
1019 * XXX: should the syncache be re-initialized with the contents
1020 * of the new SYN here (which may have different options?)
1022 * XXX: We do not check the sequence number to see if this is a
1023 * real retransmit or a new connection attempt. The question is
1024 * how to handle such a case; either ignore it as spoofed, or
1025 * drop the current entry and create a new one?
1027 sc = syncache_lookup(inc, &sch); /* returns locked entry */
1028 SCH_LOCK_ASSERT(sch);
1030 tcpstat.tcps_sc_dupsyn++;
1033 * If we were remembering a previous source route,
1034 * forget it and use the new one we've been given.
1037 (void) m_free(sc->sc_ipopts);
1038 sc->sc_ipopts = ipopts;
1041 * Update timestamp if present.
1043 if ((sc->sc_flags & SCF_TIMESTAMP) && (to->to_flags & TOF_TS))
1044 sc->sc_tsreflect = to->to_tsval;
1046 sc->sc_flags &= ~SCF_TIMESTAMP;
1049 * Since we have already unconditionally allocated label
1050 * storage, free it up. The syncache entry will already
1051 * have an initialized label we can use.
1053 mac_destroy_syncache(&maclabel);
1054 KASSERT(sc->sc_label != NULL,
1055 ("%s: label not initialized", __func__));
1057 /* Retransmit SYN|ACK and reset retransmit count. */
1058 if ((s = tcp_log_addrs(&sc->sc_inc, th, NULL, NULL))) {
1059 log(LOG_DEBUG, "%s; %s: Received duplicate SYN, "
1060 "resetting timer and retransmitting SYN|ACK\n",
1064 if (syncache_respond(sc) == 0) {
1066 syncache_timeout(sc, sch, 1);
1067 tcpstat.tcps_sndacks++;
1068 tcpstat.tcps_sndtotal++;
1074 sc = uma_zalloc(tcp_syncache.zone, M_NOWAIT | M_ZERO);
1077 * The zone allocator couldn't provide more entries.
1078 * Treat this as if the cache was full; drop the oldest
1079 * entry and insert the new one.
1081 tcpstat.tcps_sc_zonefail++;
1082 if ((sc = TAILQ_LAST(&sch->sch_bucket, sch_head)) != NULL)
1083 syncache_drop(sc, sch);
1084 sc = uma_zalloc(tcp_syncache.zone, M_NOWAIT | M_ZERO);
1086 if (tcp_syncookies) {
1087 bzero(&scs, sizeof(scs));
1092 (void) m_free(ipopts);
1099 * Fill in the syncache values.
1102 sc->sc_label = maclabel;
1104 sc->sc_ipopts = ipopts;
1105 bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo));
1107 if (!inc->inc_isipv6)
1110 sc->sc_ip_tos = ip_tos;
1111 sc->sc_ip_ttl = ip_ttl;
1114 sc->sc_irs = th->th_seq;
1115 sc->sc_iss = arc4random();
1117 sc->sc_flowlabel = 0;
1120 * Initial receive window: clip sbspace to [0 .. TCP_MAXWIN].
1121 * win was derived from socket earlier in the function.
1124 win = imin(win, TCP_MAXWIN);
1127 if (tcp_do_rfc1323) {
1129 * A timestamp received in a SYN makes
1130 * it ok to send timestamp requests and replies.
1132 if (to->to_flags & TOF_TS) {
1133 sc->sc_tsreflect = to->to_tsval;
1135 sc->sc_flags |= SCF_TIMESTAMP;
1137 if (to->to_flags & TOF_SCALE) {
1141 * Compute proper scaling value from buffer space.
1142 * Leave enough room for the socket buffer to grow
1143 * with auto sizing. This allows us to scale the
1144 * receive buffer over a wide range while not losing
1145 * any efficiency or fine granularity.
1147 * RFC1323: The Window field in a SYN (i.e., a <SYN>
1148 * or <SYN,ACK>) segment itself is never scaled.
1150 while (wscale < TCP_MAX_WINSHIFT &&
1151 (0x1 << wscale) < tcp_minmss)
1153 sc->sc_requested_r_scale = wscale;
1154 sc->sc_requested_s_scale = to->to_wscale;
1155 sc->sc_flags |= SCF_WINSCALE;
1158 #ifdef TCP_SIGNATURE
1160 * If listening socket requested TCP digests, and received SYN
1161 * contains the option, flag this in the syncache so that
1162 * syncache_respond() will do the right thing with the SYN+ACK.
1163 * XXX: Currently we always record the option by default and will
1164 * attempt to use it in syncache_respond().
1166 if (to->to_flags & TOF_SIGNATURE)
1167 sc->sc_flags |= SCF_SIGNATURE;
1169 if (to->to_flags & TOF_SACK)
1170 sc->sc_flags |= SCF_SACK;
1171 if (to->to_flags & TOF_MSS)
1172 sc->sc_peer_mss = to->to_mss; /* peer mss may be zero */
1174 sc->sc_flags |= SCF_NOOPT;
1176 if (tcp_syncookies) {
1177 syncookie_generate(sch, sc, &flowtmp);
1180 sc->sc_flowlabel = flowtmp;
1186 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1192 * Do a standard 3-way handshake.
1194 if (syncache_respond(sc) == 0) {
1195 if (tcp_syncookies && tcp_syncookiesonly && sc != &scs)
1197 else if (sc != &scs)
1198 syncache_insert(sc, sch); /* locks and unlocks sch */
1199 tcpstat.tcps_sndacks++;
1200 tcpstat.tcps_sndtotal++;
1204 tcpstat.tcps_sc_dropped++;
1210 mac_destroy_syncache(&maclabel);
1218 syncache_respond(struct syncache *sc)
1220 struct ip *ip = NULL;
1224 u_int16_t hlen, tlen, mssopt;
1227 struct ip6_hdr *ip6 = NULL;
1232 (sc->sc_inc.inc_isipv6) ? sizeof(struct ip6_hdr) :
1235 tlen = hlen + sizeof(struct tcphdr);
1237 /* Determine MSS we advertize to other end of connection. */
1238 mssopt = tcp_mssopt(&sc->sc_inc);
1239 if (sc->sc_peer_mss)
1240 mssopt = max( min(sc->sc_peer_mss, mssopt), tcp_minmss);
1242 /* XXX: Assume that the entire packet will fit in a header mbuf. */
1243 KASSERT(max_linkhdr + tlen + TCP_MAXOLEN <= MHLEN,
1244 ("syncache: mbuf too small"));
1246 /* Create the IP+TCP header from scratch. */
1247 m = m_gethdr(M_DONTWAIT, MT_DATA);
1251 mac_create_mbuf_from_syncache(sc->sc_label, m);
1253 m->m_data += max_linkhdr;
1255 m->m_pkthdr.len = tlen;
1256 m->m_pkthdr.rcvif = NULL;
1259 if (sc->sc_inc.inc_isipv6) {
1260 ip6 = mtod(m, struct ip6_hdr *);
1261 ip6->ip6_vfc = IPV6_VERSION;
1262 ip6->ip6_nxt = IPPROTO_TCP;
1263 ip6->ip6_src = sc->sc_inc.inc6_laddr;
1264 ip6->ip6_dst = sc->sc_inc.inc6_faddr;
1265 ip6->ip6_plen = htons(tlen - hlen);
1266 /* ip6_hlim is set after checksum */
1267 ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
1268 ip6->ip6_flow |= sc->sc_flowlabel;
1270 th = (struct tcphdr *)(ip6 + 1);
1274 ip = mtod(m, struct ip *);
1275 ip->ip_v = IPVERSION;
1276 ip->ip_hl = sizeof(struct ip) >> 2;
1281 ip->ip_p = IPPROTO_TCP;
1282 ip->ip_src = sc->sc_inc.inc_laddr;
1283 ip->ip_dst = sc->sc_inc.inc_faddr;
1284 ip->ip_ttl = sc->sc_ip_ttl;
1285 ip->ip_tos = sc->sc_ip_tos;
1288 * See if we should do MTU discovery. Route lookups are
1289 * expensive, so we will only unset the DF bit if:
1291 * 1) path_mtu_discovery is disabled
1292 * 2) the SCF_UNREACH flag has been set
1294 if (path_mtu_discovery && ((sc->sc_flags & SCF_UNREACH) == 0))
1295 ip->ip_off |= IP_DF;
1297 th = (struct tcphdr *)(ip + 1);
1299 th->th_sport = sc->sc_inc.inc_lport;
1300 th->th_dport = sc->sc_inc.inc_fport;
1302 th->th_seq = htonl(sc->sc_iss);
1303 th->th_ack = htonl(sc->sc_irs + 1);
1304 th->th_off = sizeof(struct tcphdr) >> 2;
1306 th->th_flags = TH_SYN|TH_ACK;
1307 th->th_win = htons(sc->sc_wnd);
1310 /* Tack on the TCP options. */
1311 if ((sc->sc_flags & SCF_NOOPT) == 0) {
1315 to.to_flags = TOF_MSS;
1316 if (sc->sc_flags & SCF_WINSCALE) {
1317 to.to_wscale = sc->sc_requested_r_scale;
1318 to.to_flags |= TOF_SCALE;
1320 if (sc->sc_flags & SCF_TIMESTAMP) {
1321 /* Virgin timestamp or TCP cookie enhanced one. */
1322 to.to_tsval = sc->sc_ts;
1323 to.to_tsecr = sc->sc_tsreflect;
1324 to.to_flags |= TOF_TS;
1326 if (sc->sc_flags & SCF_SACK)
1327 to.to_flags |= TOF_SACKPERM;
1328 #ifdef TCP_SIGNATURE
1329 if (sc->sc_flags & SCF_SIGNATURE)
1330 to.to_flags |= TOF_SIGNATURE;
1332 optlen = tcp_addoptions(&to, (u_char *)(th + 1));
1334 #ifdef TCP_SIGNATURE
1335 tcp_signature_compute(m, sizeof(struct ip), 0, optlen,
1336 to.to_signature, IPSEC_DIR_OUTBOUND);
1339 /* Adjust headers by option size. */
1340 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
1342 m->m_pkthdr.len += optlen;
1344 if (sc->sc_inc.inc_isipv6)
1345 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) + optlen);
1348 ip->ip_len += optlen;
1353 if (sc->sc_inc.inc_isipv6) {
1355 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen,
1356 tlen + optlen - hlen);
1357 ip6->ip6_hlim = in6_selecthlim(NULL, NULL);
1358 error = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1362 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1363 htons(tlen + optlen - hlen + IPPROTO_TCP));
1364 m->m_pkthdr.csum_flags = CSUM_TCP;
1365 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1366 error = ip_output(m, sc->sc_ipopts, NULL, 0, NULL, NULL);
1372 * The purpose of SYN cookies is to avoid keeping track of all SYN's we
1373 * receive and to be able to handle SYN floods from bogus source addresses
1374 * (where we will never receive any reply). SYN floods try to exhaust all
1375 * our memory and available slots in the SYN cache table to cause a denial
1376 * of service to legitimate users of the local host.
1378 * The idea of SYN cookies is to encode and include all necessary information
1379 * about the connection setup state within the SYN-ACK we send back and thus
1380 * to get along without keeping any local state until the ACK to the SYN-ACK
1381 * arrives (if ever). Everything we need to know should be available from
1382 * the information we encoded in the SYN-ACK.
1384 * More information about the theory behind SYN cookies and its first
1385 * discussion and specification can be found at:
1386 * http://cr.yp.to/syncookies.html (overview)
1387 * http://cr.yp.to/syncookies/archive (gory details)
1389 * This implementation extends the orginal idea and first implementation
1390 * of FreeBSD by using not only the initial sequence number field to store
1391 * information but also the timestamp field if present. This way we can
1392 * keep track of the entire state we need to know to recreate the session in
1393 * its original form. Almost all TCP speakers implement RFC1323 timestamps
1394 * these days. For those that do not we still have to live with the known
1395 * shortcomings of the ISN only SYN cookies.
1399 * Initial sequence number we send:
1400 * 31|................................|0
1401 * DDDDDDDDDDDDDDDDDDDDDDDDDMMMRRRP
1402 * D = MD5 Digest (first dword)
1404 * R = Rotation of secret
1405 * P = Odd or Even secret
1407 * The MD5 Digest is computed with over following parameters:
1408 * a) randomly rotated secret
1409 * b) struct in_conninfo containing the remote/local ip/port (IPv4&IPv6)
1410 * c) the received initial sequence number from remote host
1411 * d) the rotation offset and odd/even bit
1413 * Timestamp we send:
1414 * 31|................................|0
1415 * DDDDDDDDDDDDDDDDDDDDDDSSSSRRRRA5
1416 * D = MD5 Digest (third dword) (only as filler)
1417 * S = Requested send window scale
1418 * R = Requested receive window scale
1420 * 5 = TCP-MD5 enabled (not implemented yet)
1421 * XORed with MD5 Digest (forth dword)
1423 * The timestamp isn't cryptographically secure and doesn't need to be.
1424 * The double use of the MD5 digest dwords ties it to a specific remote/
1425 * local host/port, remote initial sequence number and our local time
1426 * limited secret. A received timestamp is reverted (XORed) and then
1427 * the contained MD5 dword is compared to the computed one to ensure the
1428 * timestamp belongs to the SYN-ACK we sent. The other parameters may
1429 * have been tampered with but this isn't different from supplying bogus
1430 * values in the SYN in the first place.
1432 * Some problems with SYN cookies remain however:
1433 * Consider the problem of a recreated (and retransmitted) cookie. If the
1434 * original SYN was accepted, the connection is established. The second
1435 * SYN is inflight, and if it arrives with an ISN that falls within the
1436 * receive window, the connection is killed.
1439 * A heuristic to determine when to accept syn cookies is not necessary.
1440 * An ACK flood would cause the syncookie verification to be attempted,
1441 * but a SYN flood causes syncookies to be generated. Both are of equal
1442 * cost, so there's no point in trying to optimize the ACK flood case.
1443 * Also, if you don't process certain ACKs for some reason, then all someone
1444 * would have to do is launch a SYN and ACK flood at the same time, which
1445 * would stop cookie verification and defeat the entire purpose of syncookies.
1447 static int tcp_sc_msstab[] = { 0, 256, 468, 536, 996, 1452, 1460, 8960 };
1450 syncookie_generate(struct syncache_head *sch, struct syncache *sc,
1451 u_int32_t *flowlabel)
1454 u_int32_t md5_buffer[MD5_DIGEST_LENGTH / sizeof(u_int32_t)];
1457 u_int off, pmss, mss;
1460 SCH_LOCK_ASSERT(sch);
1462 /* Which of the two secrets to use. */
1463 secbits = sch->sch_oddeven ?
1464 sch->sch_secbits_odd : sch->sch_secbits_even;
1466 /* Reseed secret if too old. */
1467 if (sch->sch_reseed < time_uptime) {
1468 sch->sch_oddeven = sch->sch_oddeven ? 0 : 1; /* toggle */
1469 secbits = sch->sch_oddeven ?
1470 sch->sch_secbits_odd : sch->sch_secbits_even;
1471 for (i = 0; i < SYNCOOKIE_SECRET_SIZE; i++)
1472 secbits[i] = arc4random();
1473 sch->sch_reseed = time_uptime + SYNCOOKIE_LIFETIME;
1476 /* Secret rotation offset. */
1477 off = sc->sc_iss & 0x7; /* iss was randomized before */
1479 /* Maximum segment size calculation. */
1480 pmss = max( min(sc->sc_peer_mss, tcp_mssopt(&sc->sc_inc)), tcp_minmss);
1481 for (mss = sizeof(tcp_sc_msstab) / sizeof(int) - 1; mss > 0; mss--)
1482 if (tcp_sc_msstab[mss] <= pmss)
1485 /* Fold parameters and MD5 digest into the ISN we will send. */
1486 data = sch->sch_oddeven;/* odd or even secret, 1 bit */
1487 data |= off << 1; /* secret offset, derived from iss, 3 bits */
1488 data |= mss << 4; /* mss, 3 bits */
1491 MD5Update(&ctx, ((u_int8_t *)secbits) + off,
1492 SYNCOOKIE_SECRET_SIZE * sizeof(*secbits) - off);
1493 MD5Update(&ctx, secbits, off);
1494 MD5Update(&ctx, &sc->sc_inc, sizeof(sc->sc_inc));
1495 MD5Update(&ctx, &sc->sc_irs, sizeof(sc->sc_irs));
1496 MD5Update(&ctx, &data, sizeof(data));
1497 MD5Final((u_int8_t *)&md5_buffer, &ctx);
1499 data |= (md5_buffer[0] << 7);
1503 *flowlabel = md5_buffer[1] & IPV6_FLOWLABEL_MASK;
1506 /* Additional parameters are stored in the timestamp if present. */
1507 if (sc->sc_flags & SCF_TIMESTAMP) {
1508 data = ((sc->sc_flags & SCF_SIGNATURE) ? 1 : 0); /* TCP-MD5, 1 bit */
1509 data |= ((sc->sc_flags & SCF_SACK) ? 1 : 0) << 1; /* SACK, 1 bit */
1510 data |= sc->sc_requested_s_scale << 2; /* SWIN scale, 4 bits */
1511 data |= sc->sc_requested_r_scale << 6; /* RWIN scale, 4 bits */
1512 data |= md5_buffer[2] << 10; /* more digest bits */
1513 data ^= md5_buffer[3];
1515 sc->sc_tsoff = data - ticks; /* after XOR */
1518 tcpstat.tcps_sc_sendcookie++;
1522 static struct syncache *
1523 syncookie_lookup(struct in_conninfo *inc, struct syncache_head *sch,
1524 struct syncache *sc, struct tcpopt *to, struct tcphdr *th,
1528 u_int32_t md5_buffer[MD5_DIGEST_LENGTH / sizeof(u_int32_t)];
1532 int off, mss, wnd, flags;
1534 SCH_LOCK_ASSERT(sch);
1537 * Pull information out of SYN-ACK/ACK and
1538 * revert sequence number advances.
1540 ack = th->th_ack - 1;
1541 seq = th->th_seq - 1;
1542 off = (ack >> 1) & 0x7;
1543 mss = (ack >> 4) & 0x7;
1546 /* Which of the two secrets to use. */
1547 secbits = (flags & 0x1) ? sch->sch_secbits_odd : sch->sch_secbits_even;
1550 * The secret wasn't updated for the lifetime of a syncookie,
1551 * so this SYN-ACK/ACK is either too old (replay) or totally bogus.
1553 if (sch->sch_reseed < time_uptime) {
1557 /* Recompute the digest so we can compare it. */
1559 MD5Update(&ctx, ((u_int8_t *)secbits) + off,
1560 SYNCOOKIE_SECRET_SIZE * sizeof(*secbits) - off);
1561 MD5Update(&ctx, secbits, off);
1562 MD5Update(&ctx, inc, sizeof(*inc));
1563 MD5Update(&ctx, &seq, sizeof(seq));
1564 MD5Update(&ctx, &flags, sizeof(flags));
1565 MD5Final((u_int8_t *)&md5_buffer, &ctx);
1567 /* Does the digest part of or ACK'ed ISS match? */
1568 if ((ack & (~0x7f)) != (md5_buffer[0] << 7))
1571 /* Does the digest part of our reflected timestamp match? */
1572 if (to->to_flags & TOF_TS) {
1573 data = md5_buffer[3] ^ to->to_tsecr;
1574 if ((data & (~0x3ff)) != (md5_buffer[2] << 10))
1578 /* Fill in the syncache values. */
1579 bcopy(inc, &sc->sc_inc, sizeof(struct in_conninfo));
1580 sc->sc_ipopts = NULL;
1586 if (inc->inc_isipv6) {
1587 if (sotoinpcb(so)->in6p_flags & IN6P_AUTOFLOWLABEL)
1588 sc->sc_flowlabel = md5_buffer[1] & IPV6_FLOWLABEL_MASK;
1592 sc->sc_ip_ttl = sotoinpcb(so)->inp_ip_ttl;
1593 sc->sc_ip_tos = sotoinpcb(so)->inp_ip_tos;
1596 /* Additional parameters that were encoded in the timestamp. */
1598 sc->sc_flags |= SCF_TIMESTAMP;
1599 sc->sc_tsreflect = to->to_tsval;
1600 sc->sc_ts = to->to_tsecr;
1601 sc->sc_tsoff = to->to_tsecr - ticks;
1602 sc->sc_flags |= (data & 0x1) ? SCF_SIGNATURE : 0;
1603 sc->sc_flags |= ((data >> 1) & 0x1) ? SCF_SACK : 0;
1604 sc->sc_requested_s_scale = min((data >> 2) & 0xf,
1606 sc->sc_requested_r_scale = min((data >> 6) & 0xf,
1608 if (sc->sc_requested_s_scale || sc->sc_requested_r_scale)
1609 sc->sc_flags |= SCF_WINSCALE;
1611 sc->sc_flags |= SCF_NOOPT;
1613 wnd = sbspace(&so->so_rcv);
1615 wnd = imin(wnd, TCP_MAXWIN);
1619 sc->sc_peer_mss = tcp_sc_msstab[mss];
1621 tcpstat.tcps_sc_recvcookie++;
1626 * Returns the current number of syncache entries. This number
1627 * will probably change before you get around to calling
1632 syncache_pcbcount(void)
1634 struct syncache_head *sch;
1637 for (count = 0, i = 0; i < tcp_syncache.hashsize; i++) {
1638 /* No need to lock for a read. */
1639 sch = &tcp_syncache.hashbase[i];
1640 count += sch->sch_length;
1646 * Exports the syncache entries to userland so that netstat can display
1647 * them alongside the other sockets. This function is intended to be
1648 * called only from tcp_pcblist.
1650 * Due to concurrency on an active system, the number of pcbs exported
1651 * may have no relation to max_pcbs. max_pcbs merely indicates the
1652 * amount of space the caller allocated for this function to use.
1655 syncache_pcblist(struct sysctl_req *req, int max_pcbs, int *pcbs_exported)
1658 struct syncache *sc;
1659 struct syncache_head *sch;
1660 int count, error, i;
1662 for (count = 0, error = 0, i = 0; i < tcp_syncache.hashsize; i++) {
1663 sch = &tcp_syncache.hashbase[i];
1665 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
1666 if (count >= max_pcbs) {
1670 bzero(&xt, sizeof(xt));
1671 xt.xt_len = sizeof(xt);
1672 if (sc->sc_inc.inc_isipv6)
1673 xt.xt_inp.inp_vflag = INP_IPV6;
1675 xt.xt_inp.inp_vflag = INP_IPV4;
1676 bcopy(&sc->sc_inc, &xt.xt_inp.inp_inc, sizeof (struct in_conninfo));
1677 xt.xt_tp.t_inpcb = &xt.xt_inp;
1678 xt.xt_tp.t_state = TCPS_SYN_RECEIVED;
1679 xt.xt_socket.xso_protocol = IPPROTO_TCP;
1680 xt.xt_socket.xso_len = sizeof (struct xsocket);
1681 xt.xt_socket.so_type = SOCK_STREAM;
1682 xt.xt_socket.so_state = SS_ISCONNECTING;
1683 error = SYSCTL_OUT(req, &xt, sizeof xt);
1693 *pcbs_exported = count;