2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Jeffrey M. Hsu.
56 * Copyright (c) 2001 Networks Associates Technologies, Inc.
57 * All rights reserved.
59 * This software was developed for the FreeBSD Project by Jonathan Lemon
60 * and NAI Labs, the Security Research Division of Network Associates, Inc.
61 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
62 * DARPA CHATS research program.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. The name of the author may not be used to endorse or promote
73 * products derived from this software without specific prior written
76 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
77 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * $FreeBSD: src/sys/netinet/tcp_syncache.c,v 1.5.2.14 2003/02/24 04:02:27 silby Exp $
89 * $DragonFly: src/sys/netinet/tcp_syncache.c,v 1.18 2004/10/15 22:59:10 hsu Exp $
92 #include "opt_inet6.h"
93 #include "opt_ipsec.h"
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/kernel.h>
98 #include <sys/sysctl.h>
99 #include <sys/malloc.h>
100 #include <sys/mbuf.h>
102 #include <sys/proc.h> /* for proc0 declaration */
103 #include <sys/random.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/in_cksum.h>
108 #include <sys/msgport2.h>
111 #include <net/route.h>
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
116 #include <netinet/in_var.h>
117 #include <netinet/in_pcb.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/ip6.h>
121 #include <netinet/icmp6.h>
122 #include <netinet6/nd6.h>
124 #include <netinet6/ip6_var.h>
125 #include <netinet6/in6_pcb.h>
126 #include <netinet/tcp.h>
127 #include <netinet/tcp_fsm.h>
128 #include <netinet/tcp_seq.h>
129 #include <netinet/tcp_timer.h>
130 #include <netinet/tcp_var.h>
131 #include <netinet6/tcp6_var.h>
134 #include <netinet6/ipsec.h>
136 #include <netinet6/ipsec6.h>
138 #include <netproto/key/key.h>
142 #include <netproto/ipsec/ipsec.h>
144 #include <netproto/ipsec/ipsec6.h>
146 #include <netproto/ipsec/key.h>
148 #endif /*FAST_IPSEC*/
150 #include <vm/vm_zone.h>
152 static int tcp_syncookies = 1;
153 SYSCTL_INT(_net_inet_tcp, OID_AUTO, syncookies, CTLFLAG_RW,
155 "Use TCP SYN cookies if the syncache overflows");
157 static void syncache_drop(struct syncache *, struct syncache_head *);
158 static void syncache_free(struct syncache *);
159 static void syncache_insert(struct syncache *, struct syncache_head *);
160 struct syncache *syncache_lookup(struct in_conninfo *, struct syncache_head **);
161 static int syncache_respond(struct syncache *, struct mbuf *);
162 static struct socket *syncache_socket(struct syncache *, struct socket *);
163 static void syncache_timer(void *);
164 static u_int32_t syncookie_generate(struct syncache *);
165 static struct syncache *syncookie_lookup(struct in_conninfo *,
166 struct tcphdr *, struct socket *);
169 * Transmit the SYN,ACK fewer times than TCP_MAXRXTSHIFT specifies.
170 * 3 retransmits corresponds to a timeout of (1 + 2 + 4 + 8 == 15) seconds,
171 * the odds are that the user has given up attempting to connect by then.
173 #define SYNCACHE_MAXREXMTS 3
175 /* Arbitrary values */
176 #define TCP_SYNCACHE_HASHSIZE 512
177 #define TCP_SYNCACHE_BUCKETLIMIT 30
179 struct netmsg_sc_timer {
180 struct lwkt_msg nm_lmsg;
181 struct msgrec *nm_mrec; /* back pointer to containing msgrec */
185 struct netmsg_sc_timer msg;
186 lwkt_port_t port; /* constant after init */
187 int slot; /* constant after init */
190 static int syncache_timer_handler(lwkt_msg_t);
192 struct tcp_syncache {
193 struct vm_zone *zone;
201 static struct tcp_syncache tcp_syncache;
203 struct tcp_syncache_percpu {
204 struct syncache_head *hashbase;
206 TAILQ_HEAD(, syncache) timerq[SYNCACHE_MAXREXMTS + 1];
207 struct callout tt_timerq[SYNCACHE_MAXREXMTS + 1];
208 struct msgrec mrec[SYNCACHE_MAXREXMTS + 1];
210 static struct tcp_syncache_percpu tcp_syncache_percpu[MAXCPU];
212 static struct lwkt_port syncache_null_rport;
214 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, syncache, CTLFLAG_RW, 0, "TCP SYN cache");
216 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, bucketlimit, CTLFLAG_RD,
217 &tcp_syncache.bucket_limit, 0, "Per-bucket hash limit for syncache");
219 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, cachelimit, CTLFLAG_RD,
220 &tcp_syncache.cache_limit, 0, "Overall entry limit for syncache");
224 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, count, CTLFLAG_RD,
225 &tcp_syncache.cache_count, 0, "Current number of entries in syncache");
228 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, hashsize, CTLFLAG_RD,
229 &tcp_syncache.hashsize, 0, "Size of TCP syncache hashtable");
231 SYSCTL_INT(_net_inet_tcp_syncache, OID_AUTO, rexmtlimit, CTLFLAG_RW,
232 &tcp_syncache.rexmt_limit, 0, "Limit on SYN/ACK retransmissions");
234 static MALLOC_DEFINE(M_SYNCACHE, "syncache", "TCP syncache");
236 #define SYNCACHE_HASH(inc, mask) \
237 ((tcp_syncache.hash_secret ^ \
238 (inc)->inc_faddr.s_addr ^ \
239 ((inc)->inc_faddr.s_addr >> 16) ^ \
240 (inc)->inc_fport ^ (inc)->inc_lport) & mask)
242 #define SYNCACHE_HASH6(inc, mask) \
243 ((tcp_syncache.hash_secret ^ \
244 (inc)->inc6_faddr.s6_addr32[0] ^ \
245 (inc)->inc6_faddr.s6_addr32[3] ^ \
246 (inc)->inc_fport ^ (inc)->inc_lport) & mask)
248 #define ENDPTS_EQ(a, b) ( \
249 (a)->ie_fport == (b)->ie_fport && \
250 (a)->ie_lport == (b)->ie_lport && \
251 (a)->ie_faddr.s_addr == (b)->ie_faddr.s_addr && \
252 (a)->ie_laddr.s_addr == (b)->ie_laddr.s_addr \
255 #define ENDPTS6_EQ(a, b) (memcmp(a, b, sizeof(*a)) == 0)
258 syncache_timeout(struct tcp_syncache_percpu *syncache_percpu,
259 struct syncache *sc, int slot)
261 sc->sc_rxtslot = slot;
262 sc->sc_rxttime = ticks + TCPTV_RTOBASE * tcp_backoff[slot];
263 TAILQ_INSERT_TAIL(&syncache_percpu->timerq[slot], sc, sc_timerq);
264 if (!callout_active(&syncache_percpu->tt_timerq[slot])) {
265 callout_reset(&syncache_percpu->tt_timerq[slot],
266 TCPTV_RTOBASE * tcp_backoff[slot],
268 &syncache_percpu->mrec[slot]);
273 syncache_free(struct syncache *sc)
277 const boolean_t isipv6 = sc->sc_inc.inc_isipv6;
279 const boolean_t isipv6 = FALSE;
283 (void) m_free(sc->sc_ipopts);
285 rt = sc->sc_route6.ro_rt;
287 rt = sc->sc_route.ro_rt;
290 * If this is the only reference to a protocol cloned
291 * route, remove it immediately.
293 if (rt->rt_flags & RTF_WASCLONED &&
294 (sc->sc_flags & SCF_KEEPROUTE) == 0 &&
295 rt->rt_refcnt == 1) {
296 rtrequest(RTM_DELETE, rt_key(rt),
297 rt->rt_gateway, rt_mask(rt),
302 zfree(tcp_syncache.zone, sc);
310 tcp_syncache.hashsize = TCP_SYNCACHE_HASHSIZE;
311 tcp_syncache.bucket_limit = TCP_SYNCACHE_BUCKETLIMIT;
312 tcp_syncache.cache_limit =
313 tcp_syncache.hashsize * tcp_syncache.bucket_limit;
314 tcp_syncache.rexmt_limit = SYNCACHE_MAXREXMTS;
315 tcp_syncache.hash_secret = arc4random();
317 TUNABLE_INT_FETCH("net.inet.tcp.syncache.hashsize",
318 &tcp_syncache.hashsize);
319 TUNABLE_INT_FETCH("net.inet.tcp.syncache.cachelimit",
320 &tcp_syncache.cache_limit);
321 TUNABLE_INT_FETCH("net.inet.tcp.syncache.bucketlimit",
322 &tcp_syncache.bucket_limit);
323 if (!powerof2(tcp_syncache.hashsize)) {
324 printf("WARNING: syncache hash size is not a power of 2.\n");
325 tcp_syncache.hashsize = 512; /* safe default */
327 tcp_syncache.hashmask = tcp_syncache.hashsize - 1;
329 lwkt_initport_null_rport(&syncache_null_rport, NULL);
331 for (cpu = 0; cpu < ncpus2; cpu++) {
332 struct tcp_syncache_percpu *syncache_percpu;
334 syncache_percpu = &tcp_syncache_percpu[cpu];
335 /* Allocate the hash table. */
336 MALLOC(syncache_percpu->hashbase, struct syncache_head *,
337 tcp_syncache.hashsize * sizeof(struct syncache_head),
338 M_SYNCACHE, M_WAITOK);
340 /* Initialize the hash buckets. */
341 for (i = 0; i < tcp_syncache.hashsize; i++) {
342 struct syncache_head *bucket;
344 bucket = &syncache_percpu->hashbase[i];
345 TAILQ_INIT(&bucket->sch_bucket);
346 bucket->sch_length = 0;
349 for (i = 0; i <= SYNCACHE_MAXREXMTS; i++) {
350 /* Initialize the timer queues. */
351 TAILQ_INIT(&syncache_percpu->timerq[i]);
352 callout_init(&syncache_percpu->tt_timerq[i]);
354 syncache_percpu->mrec[i].slot = i;
355 syncache_percpu->mrec[i].port = tcp_cport(cpu);
356 syncache_percpu->mrec[i].msg.nm_mrec =
357 &syncache_percpu->mrec[i];
358 lwkt_initmsg(&syncache_percpu->mrec[i].msg.nm_lmsg,
359 &syncache_null_rport, 0,
360 lwkt_cmd_func(syncache_timer_handler),
366 * Allocate the syncache entries. Allow the zone to allocate one
367 * more entry than cache limit, so a new entry can bump out an
370 tcp_syncache.zone = zinit("syncache", sizeof(struct syncache),
371 tcp_syncache.cache_limit, ZONE_INTERRUPT, 0);
372 tcp_syncache.cache_limit -= 1;
376 syncache_insert(sc, sch)
378 struct syncache_head *sch;
380 struct tcp_syncache_percpu *syncache_percpu;
381 struct syncache *sc2;
384 syncache_percpu = &tcp_syncache_percpu[mycpu->gd_cpuid];
387 * Make sure that we don't overflow the per-bucket
388 * limit or the total cache size limit.
390 if (sch->sch_length >= tcp_syncache.bucket_limit) {
392 * The bucket is full, toss the oldest element.
394 sc2 = TAILQ_FIRST(&sch->sch_bucket);
395 sc2->sc_tp->ts_recent = ticks;
396 syncache_drop(sc2, sch);
397 tcpstat.tcps_sc_bucketoverflow++;
398 } else if (syncache_percpu->cache_count >= tcp_syncache.cache_limit) {
400 * The cache is full. Toss the oldest entry in the
401 * entire cache. This is the front entry in the
402 * first non-empty timer queue with the largest
405 for (i = SYNCACHE_MAXREXMTS; i >= 0; i--) {
406 sc2 = TAILQ_FIRST(&syncache_percpu->timerq[i]);
410 sc2->sc_tp->ts_recent = ticks;
411 syncache_drop(sc2, NULL);
412 tcpstat.tcps_sc_cacheoverflow++;
415 /* Initialize the entry's timer. */
416 syncache_timeout(syncache_percpu, sc, 0);
418 /* Put it into the bucket. */
419 TAILQ_INSERT_TAIL(&sch->sch_bucket, sc, sc_hash);
421 syncache_percpu->cache_count++;
422 tcpstat.tcps_sc_added++;
426 syncache_drop(sc, sch)
428 struct syncache_head *sch;
430 struct tcp_syncache_percpu *syncache_percpu;
432 const boolean_t isipv6 = sc->sc_inc.inc_isipv6;
434 const boolean_t isipv6 = FALSE;
437 syncache_percpu = &tcp_syncache_percpu[mycpu->gd_cpuid];
441 sch = &syncache_percpu->hashbase[
442 SYNCACHE_HASH6(&sc->sc_inc, tcp_syncache.hashmask)];
444 sch = &syncache_percpu->hashbase[
445 SYNCACHE_HASH(&sc->sc_inc, tcp_syncache.hashmask)];
449 TAILQ_REMOVE(&sch->sch_bucket, sc, sc_hash);
451 syncache_percpu->cache_count--;
454 * Remove the entry from the syncache timer/timeout queue. Note
455 * that we do not try to stop any running timer since we do not know
456 * whether the timer's message is in-transit or not. Since timeouts
457 * are fairly long, taking an unneeded callout does not detrimentally
458 * effect performance.
460 TAILQ_REMOVE(&syncache_percpu->timerq[sc->sc_rxtslot], sc, sc_timerq);
466 * Place a timeout message on the TCP thread's message queue.
467 * This routine runs in soft interrupt context.
469 * An invariant is for this routine to be called, the callout must
470 * have been active. Note that the callout is not deactivated until
471 * after the message has been processed in syncache_timer_handler() below.
474 syncache_timer(void *p)
476 struct netmsg_sc_timer *msg = p;
478 lwkt_sendmsg(msg->nm_mrec->port, &msg->nm_lmsg);
482 * Service a timer message queued by timer expiration.
483 * This routine runs in the TCP protocol thread.
485 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
486 * If we have retransmitted an entry the maximum number of times, expire it.
488 * When we finish processing timed-out entries, we restart the timer if there
489 * are any entries still on the queue and deactivate it otherwise. Only after
490 * a timer has been deactivated here can it be restarted by syncache_timeout().
493 syncache_timer_handler(lwkt_msg_t msg)
495 struct tcp_syncache_percpu *syncache_percpu;
496 struct syncache *sc, *nsc;
500 slot = ((struct netmsg_sc_timer *)msg)->nm_mrec->slot;
501 syncache_percpu = &tcp_syncache_percpu[mycpu->gd_cpuid];
503 nsc = TAILQ_FIRST(&syncache_percpu->timerq[slot]);
504 while (nsc != NULL) {
505 if (ticks < nsc->sc_rxttime)
506 break; /* finished because timerq sorted by time */
508 inp = sc->sc_tp->t_inpcb;
509 if (slot == SYNCACHE_MAXREXMTS ||
510 slot >= tcp_syncache.rexmt_limit ||
511 inp->inp_gencnt != sc->sc_inp_gencnt) {
512 nsc = TAILQ_NEXT(sc, sc_timerq);
513 syncache_drop(sc, NULL);
514 tcpstat.tcps_sc_stale++;
518 * syncache_respond() may call back into the syncache to
519 * to modify another entry, so do not obtain the next
520 * entry on the timer chain until it has completed.
522 (void) syncache_respond(sc, NULL);
523 nsc = TAILQ_NEXT(sc, sc_timerq);
524 tcpstat.tcps_sc_retransmitted++;
525 TAILQ_REMOVE(&syncache_percpu->timerq[slot], sc, sc_timerq);
526 syncache_timeout(syncache_percpu, sc, slot + 1);
529 callout_reset(&syncache_percpu->tt_timerq[slot],
530 nsc->sc_rxttime - ticks, syncache_timer,
531 &syncache_percpu->mrec[slot]);
533 callout_deactivate(&syncache_percpu->tt_timerq[slot]);
535 lwkt_replymsg(msg, 0);
540 * Find an entry in the syncache.
543 syncache_lookup(inc, schp)
544 struct in_conninfo *inc;
545 struct syncache_head **schp;
547 struct tcp_syncache_percpu *syncache_percpu;
549 struct syncache_head *sch;
551 syncache_percpu = &tcp_syncache_percpu[mycpu->gd_cpuid];
553 if (inc->inc_isipv6) {
554 sch = &syncache_percpu->hashbase[
555 SYNCACHE_HASH6(inc, tcp_syncache.hashmask)];
557 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash)
558 if (ENDPTS6_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
563 sch = &syncache_percpu->hashbase[
564 SYNCACHE_HASH(inc, tcp_syncache.hashmask)];
566 TAILQ_FOREACH(sc, &sch->sch_bucket, sc_hash) {
568 if (sc->sc_inc.inc_isipv6)
571 if (ENDPTS_EQ(&inc->inc_ie, &sc->sc_inc.inc_ie))
579 * This function is called when we get a RST for a
580 * non-existent connection, so that we can see if the
581 * connection is in the syn cache. If it is, zap it.
584 syncache_chkrst(inc, th)
585 struct in_conninfo *inc;
589 struct syncache_head *sch;
591 sc = syncache_lookup(inc, &sch);
595 * If the RST bit is set, check the sequence number to see
596 * if this is a valid reset segment.
598 * In all states except SYN-SENT, all reset (RST) segments
599 * are validated by checking their SEQ-fields. A reset is
600 * valid if its sequence number is in the window.
602 * The sequence number in the reset segment is normally an
603 * echo of our outgoing acknowlegement numbers, but some hosts
604 * send a reset with the sequence number at the rightmost edge
605 * of our receive window, and we have to handle this case.
607 if (SEQ_GEQ(th->th_seq, sc->sc_irs) &&
608 SEQ_LEQ(th->th_seq, sc->sc_irs + sc->sc_wnd)) {
609 syncache_drop(sc, sch);
610 tcpstat.tcps_sc_reset++;
616 struct in_conninfo *inc;
619 struct syncache_head *sch;
621 sc = syncache_lookup(inc, &sch);
623 syncache_drop(sc, sch);
624 tcpstat.tcps_sc_badack++;
629 syncache_unreach(inc, th)
630 struct in_conninfo *inc;
634 struct syncache_head *sch;
636 /* we are called at splnet() here */
637 sc = syncache_lookup(inc, &sch);
641 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
642 if (ntohl(th->th_seq) != sc->sc_iss)
646 * If we've rertransmitted 3 times and this is our second error,
647 * we remove the entry. Otherwise, we allow it to continue on.
648 * This prevents us from incorrectly nuking an entry during a
649 * spurious network outage.
653 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtslot < 3) {
654 sc->sc_flags |= SCF_UNREACH;
657 syncache_drop(sc, sch);
658 tcpstat.tcps_sc_unreach++;
662 * Build a new TCP socket structure from a syncache entry.
664 static struct socket *
665 syncache_socket(sc, lso)
669 struct inpcb *inp = NULL;
673 const boolean_t isipv6 = sc->sc_inc.inc_isipv6;
675 const boolean_t isipv6 = FALSE;
679 * Ok, create the full blown connection, and set things up
680 * as they would have been set up if we had created the
681 * connection when the SYN arrived. If we can't create
682 * the connection, abort it.
684 so = sonewconn(lso, SS_ISCONNECTED);
687 * Drop the connection; we will send a RST if the peer
688 * retransmits the ACK,
690 tcpstat.tcps_listendrop++;
697 * Insert new socket into hash list.
699 inp->inp_inc.inc_isipv6 = sc->sc_inc.inc_isipv6;
701 inp->in6p_laddr = sc->sc_inc.inc6_laddr;
704 inp->inp_vflag &= ~INP_IPV6;
705 inp->inp_vflag |= INP_IPV4;
707 inp->inp_laddr = sc->sc_inc.inc_laddr;
709 inp->inp_lport = sc->sc_inc.inc_lport;
710 if (in_pcbinsporthash(inp) != 0) {
712 * Undo the assignments above if we failed to
713 * put the PCB on the hash lists.
716 inp->in6p_laddr = in6addr_any;
718 inp->inp_laddr.s_addr = INADDR_ANY;
723 /* copy old policy into new socket's */
724 if (ipsec_copy_policy(sotoinpcb(lso)->inp_sp, inp->inp_sp))
725 printf("syncache_expand: could not copy policy\n");
728 struct inpcb *oinp = sotoinpcb(lso);
729 struct in6_addr laddr6;
730 struct sockaddr_in6 sin6;
732 * Inherit socket options from the listening socket.
733 * Note that in6p_inputopts are not (and should not be)
734 * copied, since it stores previously received options and is
735 * used to detect if each new option is different than the
736 * previous one and hence should be passed to a user.
737 * If we copied in6p_inputopts, a user would not be able to
738 * receive options just after calling the accept system call.
740 inp->inp_flags |= oinp->inp_flags & INP_CONTROLOPTS;
741 if (oinp->in6p_outputopts)
742 inp->in6p_outputopts =
743 ip6_copypktopts(oinp->in6p_outputopts, M_INTWAIT);
744 inp->in6p_route = sc->sc_route6;
745 sc->sc_route6.ro_rt = NULL;
747 sin6.sin6_family = AF_INET6;
748 sin6.sin6_len = sizeof sin6;
749 sin6.sin6_addr = sc->sc_inc.inc6_faddr;
750 sin6.sin6_port = sc->sc_inc.inc_fport;
751 sin6.sin6_flowinfo = sin6.sin6_scope_id = 0;
752 laddr6 = inp->in6p_laddr;
753 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
754 inp->in6p_laddr = sc->sc_inc.inc6_laddr;
755 if (in6_pcbconnect(inp, (struct sockaddr *)&sin6, &thread0)) {
756 inp->in6p_laddr = laddr6;
760 struct in_addr laddr;
761 struct sockaddr_in sin;
763 inp->inp_options = ip_srcroute();
764 if (inp->inp_options == NULL) {
765 inp->inp_options = sc->sc_ipopts;
766 sc->sc_ipopts = NULL;
768 inp->inp_route = sc->sc_route;
769 sc->sc_route.ro_rt = NULL;
771 sin.sin_family = AF_INET;
772 sin.sin_len = sizeof sin;
773 sin.sin_addr = sc->sc_inc.inc_faddr;
774 sin.sin_port = sc->sc_inc.inc_fport;
775 bzero(sin.sin_zero, sizeof sin.sin_zero);
776 laddr = inp->inp_laddr;
777 if (inp->inp_laddr.s_addr == INADDR_ANY)
778 inp->inp_laddr = sc->sc_inc.inc_laddr;
779 if (in_pcbconnect(inp, (struct sockaddr *)&sin, &thread0)) {
780 inp->inp_laddr = laddr;
786 tp->t_state = TCPS_SYN_RECEIVED;
787 tp->iss = sc->sc_iss;
788 tp->irs = sc->sc_irs;
791 tp->snd_wl1 = sc->sc_irs;
792 tp->rcv_up = sc->sc_irs + 1;
793 tp->rcv_wnd = sc->sc_wnd;
794 tp->rcv_adv += tp->rcv_wnd;
796 tp->t_flags = sototcpcb(lso)->t_flags & (TF_NOPUSH | TF_NODELAY);
797 if (sc->sc_flags & SCF_NOOPT)
798 tp->t_flags |= TF_NOOPT;
799 if (sc->sc_flags & SCF_WINSCALE) {
800 tp->t_flags |= TF_REQ_SCALE | TF_RCVD_SCALE;
801 tp->requested_s_scale = sc->sc_requested_s_scale;
802 tp->request_r_scale = sc->sc_request_r_scale;
804 if (sc->sc_flags & SCF_TIMESTAMP) {
805 tp->t_flags |= TF_REQ_TSTMP | TF_RCVD_TSTMP;
806 tp->ts_recent = sc->sc_tsrecent;
807 tp->ts_recent_age = ticks;
809 if (sc->sc_flags & SCF_CC) {
811 * Initialization of the tcpcb for transaction;
812 * set SND.WND = SEG.WND,
813 * initialize CCsend and CCrecv.
815 tp->t_flags |= TF_REQ_CC | TF_RCVD_CC;
816 tp->cc_send = sc->sc_cc_send;
817 tp->cc_recv = sc->sc_cc_recv;
820 tcp_mss(tp, sc->sc_peer_mss);
823 * If the SYN,ACK was retransmitted, reset cwnd to 1 segment.
825 if (sc->sc_rxtslot != 0)
826 tp->snd_cwnd = tp->t_maxseg;
827 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
829 tcpstat.tcps_accepts++;
839 * This function gets called when we receive an ACK for a
840 * socket in the LISTEN state. We look up the connection
841 * in the syncache, and if its there, we pull it out of
842 * the cache and turn it into a full-blown connection in
843 * the SYN-RECEIVED state.
846 syncache_expand(inc, th, sop, m)
847 struct in_conninfo *inc;
853 struct syncache_head *sch;
856 sc = syncache_lookup(inc, &sch);
859 * There is no syncache entry, so see if this ACK is
860 * a returning syncookie. To do this, first:
861 * A. See if this socket has had a syncache entry dropped in
862 * the past. We don't want to accept a bogus syncookie
863 * if we've never received a SYN.
864 * B. check that the syncookie is valid. If it is, then
865 * cobble up a fake syncache entry, and return.
869 sc = syncookie_lookup(inc, th, *sop);
873 tcpstat.tcps_sc_recvcookie++;
877 * If seg contains an ACK, but not for our SYN/ACK, send a RST.
879 if (th->th_ack != sc->sc_iss + 1)
882 so = syncache_socket(sc, *sop);
886 /* XXXjlemon check this - is this correct? */
887 (void) tcp_respond(NULL, m, m, th,
888 th->th_seq + tlen, (tcp_seq)0, TH_RST | TH_ACK);
890 m_freem(m); /* XXX only needed for above */
891 tcpstat.tcps_sc_aborted++;
893 sc->sc_flags |= SCF_KEEPROUTE;
894 tcpstat.tcps_sc_completed++;
899 syncache_drop(sc, sch);
905 * Given a LISTEN socket and an inbound SYN request, add
906 * this to the syn cache, and send back a segment:
907 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
910 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
911 * Doing so would require that we hold onto the data and deliver it
912 * to the application. However, if we are the target of a SYN-flood
913 * DoS attack, an attacker could send data which would eventually
914 * consume all available buffer space if it were ACKed. By not ACKing
915 * the data, we avoid this DoS scenario.
918 syncache_add(inc, to, th, sop, m)
919 struct in_conninfo *inc;
925 struct tcp_syncache_percpu *syncache_percpu;
928 struct syncache *sc = NULL;
929 struct syncache_head *sch;
930 struct mbuf *ipopts = NULL;
931 struct rmxp_tao *taop;
934 syncache_percpu = &tcp_syncache_percpu[mycpu->gd_cpuid];
939 * Remember the IP options, if any.
942 if (!inc->inc_isipv6)
944 ipopts = ip_srcroute();
947 * See if we already have an entry for this connection.
948 * If we do, resend the SYN,ACK, and reset the retransmit timer.
951 * should the syncache be re-initialized with the contents
952 * of the new SYN here (which may have different options?)
954 sc = syncache_lookup(inc, &sch);
956 tcpstat.tcps_sc_dupsyn++;
959 * If we were remembering a previous source route,
960 * forget it and use the new one we've been given.
963 (void) m_free(sc->sc_ipopts);
964 sc->sc_ipopts = ipopts;
967 * Update timestamp if present.
969 if (sc->sc_flags & SCF_TIMESTAMP)
970 sc->sc_tsrecent = to->to_tsval;
972 * PCB may have changed, pick up new values.
975 sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt;
976 if (syncache_respond(sc, m) == 0) {
977 TAILQ_REMOVE(&syncache_percpu->timerq[sc->sc_rxtslot],
979 syncache_timeout(syncache_percpu, sc, sc->sc_rxtslot);
980 tcpstat.tcps_sndacks++;
981 tcpstat.tcps_sndtotal++;
988 * This allocation is guaranteed to succeed because we
989 * preallocate one more syncache entry than cache_limit.
991 sc = zalloc(tcp_syncache.zone);
994 * Fill in the syncache values.
997 sc->sc_inp_gencnt = tp->t_inpcb->inp_gencnt;
998 sc->sc_ipopts = ipopts;
999 sc->sc_inc.inc_fport = inc->inc_fport;
1000 sc->sc_inc.inc_lport = inc->inc_lport;
1002 sc->sc_inc.inc_isipv6 = inc->inc_isipv6;
1003 if (inc->inc_isipv6) {
1004 sc->sc_inc.inc6_faddr = inc->inc6_faddr;
1005 sc->sc_inc.inc6_laddr = inc->inc6_laddr;
1006 sc->sc_route6.ro_rt = NULL;
1010 sc->sc_inc.inc_faddr = inc->inc_faddr;
1011 sc->sc_inc.inc_laddr = inc->inc_laddr;
1012 sc->sc_route.ro_rt = NULL;
1014 sc->sc_irs = th->th_seq;
1016 sc->sc_peer_mss = to->to_flags & TOF_MSS ? to->to_mss : 0;
1018 sc->sc_iss = syncookie_generate(sc);
1020 sc->sc_iss = arc4random();
1022 /* Initial receive window: clip sbspace to [0 .. TCP_MAXWIN] */
1023 win = sbspace(&so->so_rcv);
1025 win = imin(win, TCP_MAXWIN);
1028 if (tcp_do_rfc1323) {
1030 * A timestamp received in a SYN makes
1031 * it ok to send timestamp requests and replies.
1033 if (to->to_flags & TOF_TS) {
1034 sc->sc_tsrecent = to->to_tsval;
1035 sc->sc_flags |= SCF_TIMESTAMP;
1037 if (to->to_flags & TOF_SCALE) {
1040 /* Compute proper scaling value from buffer space */
1041 while (wscale < TCP_MAX_WINSHIFT &&
1042 (TCP_MAXWIN << wscale) < so->so_rcv.sb_hiwat)
1044 sc->sc_request_r_scale = wscale;
1045 sc->sc_requested_s_scale = to->to_requested_s_scale;
1046 sc->sc_flags |= SCF_WINSCALE;
1049 if (tcp_do_rfc1644) {
1051 * A CC or CC.new option received in a SYN makes
1052 * it ok to send CC in subsequent segments.
1054 if (to->to_flags & (TOF_CC | TOF_CCNEW)) {
1055 sc->sc_cc_recv = to->to_cc;
1056 sc->sc_cc_send = CC_INC(tcp_ccgen);
1057 sc->sc_flags |= SCF_CC;
1060 if (tp->t_flags & TF_NOOPT)
1061 sc->sc_flags = SCF_NOOPT;
1065 * We have the option here of not doing TAO (even if the segment
1066 * qualifies) and instead fall back to a normal 3WHS via the syncache.
1067 * This allows us to apply synflood protection to TAO-qualifying SYNs
1068 * also. However, there should be a hueristic to determine when to
1069 * do this, and is not present at the moment.
1073 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1074 * - compare SEG.CC against cached CC from the same host, if any.
1075 * - if SEG.CC > chached value, SYN must be new and is accepted
1076 * immediately: save new CC in the cache, mark the socket
1077 * connected, enter ESTABLISHED state, turn on flag to
1078 * send a SYN in the next segment.
1079 * A virtual advertised window is set in rcv_adv to
1080 * initialize SWS prevention. Then enter normal segment
1081 * processing: drop SYN, process data and FIN.
1082 * - otherwise do a normal 3-way handshake.
1084 taop = tcp_gettaocache(&sc->sc_inc);
1085 if ((to->to_flags & TOF_CC) != 0) {
1086 if (((tp->t_flags & TF_NOPUSH) != 0) &&
1087 sc->sc_flags & SCF_CC &&
1088 taop != NULL && taop->tao_cc != 0 &&
1089 CC_GT(to->to_cc, taop->tao_cc)) {
1091 so = syncache_socket(sc, *sop);
1093 sc->sc_flags |= SCF_KEEPROUTE;
1094 taop->tao_cc = to->to_cc;
1098 return (so != NULL);
1102 * No CC option, but maybe CC.NEW: invalidate cached value.
1108 * TAO test failed or there was no CC option,
1109 * do a standard 3-way handshake.
1111 if (syncache_respond(sc, m) == 0) {
1112 syncache_insert(sc, sch);
1113 tcpstat.tcps_sndacks++;
1114 tcpstat.tcps_sndtotal++;
1117 tcpstat.tcps_sc_dropped++;
1124 syncache_respond(sc, m)
1125 struct syncache *sc;
1130 u_int16_t tlen, hlen, mssopt;
1131 struct ip *ip = NULL;
1134 struct ip6_hdr *ip6 = NULL;
1136 const boolean_t isipv6 = sc->sc_inc.inc_isipv6;
1138 const boolean_t isipv6 = FALSE;
1142 rt = tcp_rtlookup6(&sc->sc_inc);
1144 mssopt = rt->rt_ifp->if_mtu -
1145 (sizeof(struct ip6_hdr) + sizeof(struct tcphdr));
1147 mssopt = tcp_v6mssdflt;
1148 hlen = sizeof(struct ip6_hdr);
1150 rt = tcp_rtlookup(&sc->sc_inc);
1152 mssopt = rt->rt_ifp->if_mtu -
1153 (sizeof(struct ip) + sizeof(struct tcphdr));
1155 mssopt = tcp_mssdflt;
1156 hlen = sizeof(struct ip);
1159 /* Compute the size of the TCP options. */
1160 if (sc->sc_flags & SCF_NOOPT) {
1163 optlen = TCPOLEN_MAXSEG +
1164 ((sc->sc_flags & SCF_WINSCALE) ? 4 : 0) +
1165 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0) +
1166 ((sc->sc_flags & SCF_CC) ? TCPOLEN_CC_APPA * 2 : 0);
1168 tlen = hlen + sizeof(struct tcphdr) + optlen;
1172 * assume that the entire packet will fit in a header mbuf
1174 KASSERT(max_linkhdr + tlen <= MHLEN, ("syncache: mbuf too small"));
1177 * XXX shouldn't this reuse the mbuf if possible ?
1178 * Create the IP+TCP header from scratch.
1183 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1186 m->m_data += max_linkhdr;
1188 m->m_pkthdr.len = tlen;
1189 m->m_pkthdr.rcvif = NULL;
1192 ip6 = mtod(m, struct ip6_hdr *);
1193 ip6->ip6_vfc = IPV6_VERSION;
1194 ip6->ip6_nxt = IPPROTO_TCP;
1195 ip6->ip6_src = sc->sc_inc.inc6_laddr;
1196 ip6->ip6_dst = sc->sc_inc.inc6_faddr;
1197 ip6->ip6_plen = htons(tlen - hlen);
1198 /* ip6_hlim is set after checksum */
1199 /* ip6_flow = ??? */
1201 th = (struct tcphdr *)(ip6 + 1);
1203 ip = mtod(m, struct ip *);
1204 ip->ip_v = IPVERSION;
1205 ip->ip_hl = sizeof(struct ip) >> 2;
1210 ip->ip_p = IPPROTO_TCP;
1211 ip->ip_src = sc->sc_inc.inc_laddr;
1212 ip->ip_dst = sc->sc_inc.inc_faddr;
1213 ip->ip_ttl = sc->sc_tp->t_inpcb->inp_ip_ttl; /* XXX */
1214 ip->ip_tos = sc->sc_tp->t_inpcb->inp_ip_tos; /* XXX */
1217 * See if we should do MTU discovery. Route lookups are
1218 * expensive, so we will only unset the DF bit if:
1220 * 1) path_mtu_discovery is disabled
1221 * 2) the SCF_UNREACH flag has been set
1223 if (path_mtu_discovery
1224 && ((sc->sc_flags & SCF_UNREACH) == 0)) {
1225 ip->ip_off |= IP_DF;
1228 th = (struct tcphdr *)(ip + 1);
1230 th->th_sport = sc->sc_inc.inc_lport;
1231 th->th_dport = sc->sc_inc.inc_fport;
1233 th->th_seq = htonl(sc->sc_iss);
1234 th->th_ack = htonl(sc->sc_irs + 1);
1235 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
1237 th->th_flags = TH_SYN | TH_ACK;
1238 th->th_win = htons(sc->sc_wnd);
1241 /* Tack on the TCP options. */
1244 optp = (u_int8_t *)(th + 1);
1245 *optp++ = TCPOPT_MAXSEG;
1246 *optp++ = TCPOLEN_MAXSEG;
1247 *optp++ = (mssopt >> 8) & 0xff;
1248 *optp++ = mssopt & 0xff;
1250 if (sc->sc_flags & SCF_WINSCALE) {
1251 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
1252 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
1253 sc->sc_request_r_scale);
1257 if (sc->sc_flags & SCF_TIMESTAMP) {
1258 u_int32_t *lp = (u_int32_t *)(optp);
1260 /* Form timestamp option as shown in appendix A of RFC 1323. */
1261 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1262 *lp++ = htonl(ticks);
1263 *lp = htonl(sc->sc_tsrecent);
1264 optp += TCPOLEN_TSTAMP_APPA;
1268 * Send CC and CC.echo if we received CC from our peer.
1270 if (sc->sc_flags & SCF_CC) {
1271 u_int32_t *lp = (u_int32_t *)(optp);
1273 *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CC));
1274 *lp++ = htonl(sc->sc_cc_send);
1275 *lp++ = htonl(TCPOPT_CC_HDR(TCPOPT_CCECHO));
1276 *lp = htonl(sc->sc_cc_recv);
1277 optp += TCPOLEN_CC_APPA * 2;
1282 struct route_in6 *ro6 = &sc->sc_route6;
1285 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
1286 ip6->ip6_hlim = in6_selecthlim(NULL,
1287 ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL);
1288 error = ip6_output(m, NULL, ro6, 0, NULL, NULL,
1289 sc->sc_tp->t_inpcb);
1291 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1292 htons(tlen - hlen + IPPROTO_TCP));
1293 m->m_pkthdr.csum_flags = CSUM_TCP;
1294 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1295 error = ip_output(m, sc->sc_ipopts, &sc->sc_route, 0, NULL,
1296 sc->sc_tp->t_inpcb);
1304 * |. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .|
1306 * | MD5(laddr,faddr,secret,lport,fport) |. . . . . . .|
1308 * (A): peer mss index
1312 * The values below are chosen to minimize the size of the tcp_secret
1313 * table, as well as providing roughly a 16 second lifetime for the cookie.
1316 #define SYNCOOKIE_WNDBITS 5 /* exposed bits for window indexing */
1317 #define SYNCOOKIE_TIMESHIFT 1 /* scale ticks to window time units */
1319 #define SYNCOOKIE_WNDMASK ((1 << SYNCOOKIE_WNDBITS) - 1)
1320 #define SYNCOOKIE_NSECRETS (1 << SYNCOOKIE_WNDBITS)
1321 #define SYNCOOKIE_TIMEOUT \
1322 (hz * (1 << SYNCOOKIE_WNDBITS) / (1 << SYNCOOKIE_TIMESHIFT))
1323 #define SYNCOOKIE_DATAMASK ((3 << SYNCOOKIE_WNDBITS) | SYNCOOKIE_WNDMASK)
1326 u_int32_t ts_secbits[4];
1328 } tcp_secret[SYNCOOKIE_NSECRETS];
1330 static int tcp_msstab[] = { 0, 536, 1460, 8960 };
1332 static MD5_CTX syn_ctx;
1334 #define MD5Add(v) MD5Update(&syn_ctx, (u_char *)&v, sizeof(v))
1337 u_int32_t laddr, faddr;
1338 u_int32_t secbits[4];
1339 u_int16_t lport, fport;
1343 CTASSERT(sizeof(struct md5_add) == 28);
1347 * Consider the problem of a recreated (and retransmitted) cookie. If the
1348 * original SYN was accepted, the connection is established. The second
1349 * SYN is inflight, and if it arrives with an ISN that falls within the
1350 * receive window, the connection is killed.
1352 * However, since cookies have other problems, this may not be worth
1357 syncookie_generate(struct syncache *sc)
1359 u_int32_t md5_buffer[4];
1364 const boolean_t isipv6 = sc->sc_inc.inc_isipv6;
1366 const boolean_t isipv6 = FALSE;
1369 idx = ((ticks << SYNCOOKIE_TIMESHIFT) / hz) & SYNCOOKIE_WNDMASK;
1370 if (tcp_secret[idx].ts_expire < ticks) {
1371 for (i = 0; i < 4; i++)
1372 tcp_secret[idx].ts_secbits[i] = arc4random();
1373 tcp_secret[idx].ts_expire = ticks + SYNCOOKIE_TIMEOUT;
1375 for (data = sizeof(tcp_msstab) / sizeof(int) - 1; data > 0; data--)
1376 if (tcp_msstab[data] <= sc->sc_peer_mss)
1378 data = (data << SYNCOOKIE_WNDBITS) | idx;
1379 data ^= sc->sc_irs; /* peer's iss */
1382 MD5Add(sc->sc_inc.inc6_laddr);
1383 MD5Add(sc->sc_inc.inc6_faddr);
1387 add.laddr = sc->sc_inc.inc_laddr.s_addr;
1388 add.faddr = sc->sc_inc.inc_faddr.s_addr;
1390 add.lport = sc->sc_inc.inc_lport;
1391 add.fport = sc->sc_inc.inc_fport;
1392 add.secbits[0] = tcp_secret[idx].ts_secbits[0];
1393 add.secbits[1] = tcp_secret[idx].ts_secbits[1];
1394 add.secbits[2] = tcp_secret[idx].ts_secbits[2];
1395 add.secbits[3] = tcp_secret[idx].ts_secbits[3];
1397 MD5Final((u_char *)&md5_buffer, &syn_ctx);
1398 data ^= (md5_buffer[0] & ~SYNCOOKIE_WNDMASK);
1402 static struct syncache *
1403 syncookie_lookup(inc, th, so)
1404 struct in_conninfo *inc;
1408 u_int32_t md5_buffer[4];
1409 struct syncache *sc;
1414 data = (th->th_ack - 1) ^ (th->th_seq - 1); /* remove ISS */
1415 idx = data & SYNCOOKIE_WNDMASK;
1416 if (tcp_secret[idx].ts_expire < ticks ||
1417 sototcpcb(so)->ts_recent + SYNCOOKIE_TIMEOUT < ticks)
1421 if (inc->inc_isipv6) {
1422 MD5Add(inc->inc6_laddr);
1423 MD5Add(inc->inc6_faddr);
1429 add.laddr = inc->inc_laddr.s_addr;
1430 add.faddr = inc->inc_faddr.s_addr;
1432 add.lport = inc->inc_lport;
1433 add.fport = inc->inc_fport;
1434 add.secbits[0] = tcp_secret[idx].ts_secbits[0];
1435 add.secbits[1] = tcp_secret[idx].ts_secbits[1];
1436 add.secbits[2] = tcp_secret[idx].ts_secbits[2];
1437 add.secbits[3] = tcp_secret[idx].ts_secbits[3];
1439 MD5Final((u_char *)&md5_buffer, &syn_ctx);
1440 data ^= md5_buffer[0];
1441 if ((data & ~SYNCOOKIE_DATAMASK) != 0)
1443 data = data >> SYNCOOKIE_WNDBITS;
1446 * This allocation is guaranteed to succeed because we
1447 * preallocate one more syncache entry than cache_limit.
1449 sc = zalloc(tcp_syncache.zone);
1452 * Fill in the syncache values.
1453 * XXX duplicate code from syncache_add
1455 sc->sc_ipopts = NULL;
1456 sc->sc_inc.inc_fport = inc->inc_fport;
1457 sc->sc_inc.inc_lport = inc->inc_lport;
1459 sc->sc_inc.inc_isipv6 = inc->inc_isipv6;
1460 if (inc->inc_isipv6) {
1461 sc->sc_inc.inc6_faddr = inc->inc6_faddr;
1462 sc->sc_inc.inc6_laddr = inc->inc6_laddr;
1463 sc->sc_route6.ro_rt = NULL;
1467 sc->sc_inc.inc_faddr = inc->inc_faddr;
1468 sc->sc_inc.inc_laddr = inc->inc_laddr;
1469 sc->sc_route.ro_rt = NULL;
1471 sc->sc_irs = th->th_seq - 1;
1472 sc->sc_iss = th->th_ack - 1;
1473 wnd = sbspace(&so->so_rcv);
1475 wnd = imin(wnd, TCP_MAXWIN);
1479 sc->sc_peer_mss = tcp_msstab[data];