SYN cookies: answering a SYN without remembering it

A SYN cookie is a way for a TCP server to reply to a connection request without storing anything about it. The server folds the details it would have kept into the initial sequence number of its reply, and rebuilds them only if the client answers correctly. A flood of forged connection requests then has no table to fill. The price is paid in TCP options that cannot be carried along and in work done for every request that arrives.

Section DDoS protection Updated Works at the TCP handshake

Quick facts

Core idea
Encode the half-open connection in the server's initial sequence number instead of storing it (RFC 4987)
What is checked
The client's final ACK must acknowledge a valid cookie plus one; the SYN occupies one sequence number (RFC 9293)
Main loss
TCP options such as window scaling; the segment size is limited to a few values (RFC 4987, appendix A)
Linux default
Cookies are sent when a socket's SYN backlog overflows (kernel documentation)
What it does not fix
A full network link, or floods of other kinds
Official caveat
The Linux documentation calls the feature a fallback that must not be used to help loaded servers handle legitimate connection rates (tcp_syncookies)

This page is about the endpoint defence only. The attack it answers is described on SYN flood, and the wider response sequence is on DDoS mitigation.

The problem: a table that fills up

TCP opens a connection with three messages. The client sends a SYN, the server replies with a SYN-ACK, and the client confirms with an ACK. RFC 9293 calls the server's state after sending the SYN-ACK SYN-RECEIVED: it is waiting for a confirming acknowledgment after having both received and sent a connection request.

In the ordinary design that waiting costs memory. RFC 4987 explains that when a SYN arrives for a listening port, the stack allocates a record for the new connection, and because each record uses memory the operating system caps how many half-open connections can exist. That cap is the backlog. Once it is full, new SYNs may be ignored or older entries replaced, and what happens depends on the implementation. A SYN flood sends forged or abandoned requests that never complete, so the backlog fills with entries that no client will ever confirm and real clients are turned away.

The idea behind a cookie is to stop the fill. If the server keeps nothing for a request, there is nothing to exhaust.

How a cookie works

Every TCP segment carries a sequence number, and the server gets to choose its own starting value, the initial sequence number. Normally that choice is hard to guess. RFC 9293 describes a 32-bit clock that ticks roughly every four microseconds, combined with a function of the connection endpoints and a secret key that must not be computable from outside. A SYN cookie reuses this freedom: the number is still unguessable to an outsider, but it also carries information.

  1. Reply. A SYN arrives. Instead of allocating a record, the server computes a number from the connection's addresses and ports, a secret value and a coarse time counter, and sends it as the initial sequence number of the SYN-ACK. Then it forgets the request.
  2. Wait. A real client answers with an ACK whose acknowledgment number is the server's number plus one, because the SYN counts as one sequence number. A forged source address never sees the SYN-ACK and never answers.
  3. Check. When an ACK arrives for a connection the server has no record of, the server subtracts one from the acknowledgment number and recomputes what a valid cookie would be for that address pair and a recent time value. If it matches, the server builds the connection record at that moment, as though the SYN had just been received.

RFC 4987 states that cookies encode most of the state, and all of the strictly required state, that the server would normally keep. Its appendix A shows one layout: a few bits of a counter, a few bits that index a table of segment sizes, and the rest a keyed function of the addresses, ports and counter. It also says the exact split varies between descriptions, so treat that layout as an example, not a specification. What matters is the shape: a keyed value the server can verify later without having stored it.

The check has a useful side effect. A client that completes it has proved it received the SYN-ACK, so it can be reached at the address it claims. This is the same property a half-open entry would eventually prove, without keeping the entry. If you work with firewalls, the contrast is with a stateful firewall, whose job is to remember each connection in a table; a cookie is a way to avoid having that table for connections that have not yet been confirmed.

What is lost

A 32-bit number is small. Everything the server would have remembered has to fit inside it, and some things do not. RFC 4987 is direct about the compromises.

  • Segment size. The maximum segment size is reduced to one of a few predefined values, because only a few bits can name it. A client may therefore end up with a smaller size than it offered.
  • Window scaling. The RFC calls disabling of the window scaling option the largest price of cookies, since the option limits throughput on fast, long paths.
  • Other options. Commonly implemented schemes are incompatible with some options unless the encoding makes room for them. Selective acknowledgment, for example, is affected: the RFC says SACK blocks should normally not be sent unless SACK was first received, which limits its use for one-directional transfers.
  • Data on the SYN. Application data piggybacked on a SYN is not handled.
  • Lost final ACK. If the confirming ACK is lost, the server has no record that anything happened, so its application is never told about the connection. In a protocol where the server speaks first, such as the service-ready greeting of SMTP, the server never sends it, while the client believes the connection is open.

None of this affects a server that is not under pressure, if cookies are only used when the backlog overflows, as in the default on Linux. The loss of options is the argument for using cookies as a fallback and not for every connection.

What it costs

Processor time

Memory is traded for computation. Every SYN means a keyed hash to produce the cookie, and every ACK of an unknown connection means another one to check it. RFC 4987 judges that the added work of computing hashes for each SYN is not significant compared with outages on typical web servers, and adds that the answer might differ on constrained mobile or embedded devices. That is the source's view, not a measurement for your hardware; the cost grows with the packet rate, and on a small machine it can be the next thing to fail.

The check on the way back is why this defence shifts load instead of removing it. A stray ACK is not simply discarded; the server has to look at it and decide whether it fits a valid cookie. How attackers use that is covered on ACK flood, which targets the lookup that follows an arriving ACK.

Bandwidth is untouched

Cookies live in the host's TCP stack, after the packets have crossed the network link. A flood large enough to fill that link is a different problem, and the defences for it sit upstream: absorbing capacity in a scrubbing center or spreading it over sites with anycast. The classes of attack are laid out on protocol attacks.

A fallback, as documented

The Linux kernel documentation states that cookies are a fallback facility, and that they must not be used to help highly loaded servers stand against a legitimate connection rate. If the warnings in your logs come from real traffic, the documentation says to tune other parameters until they stop. In other words, cookies are for hostile requests, not for capacity planning.

Where it is switched on

SYN cookies are a property of whichever machine terminates the TCP connection.

Operating system kernel. On Linux the behaviour is controlled by the tcp_syncookies setting, which exists only if the kernel was built with cookie support. According to the kernel documentation, the default value sends cookies when the SYN backlog queue of a socket overflows, and a second value generates them unconditionally. That second value takes the option cost described above on every connection. Related settings in the same document cap the number of remembered connection requests and set how many times a SYN-ACK is retransmitted. These are host settings with real effects on live traffic, so read the documentation for your kernel version and follow your change process before altering any of them; this page does not give a recipe.

Balancers and other front devices. A device that accepts connections on behalf of servers has its own TCP stack, and can apply the same technique before a connection reaches the server. RFC 4987 also discusses firewalls and proxies that answer on a server's behalf. Which devices do this, with which limits, differs by product and is a matter for that product's documentation.

A hosting provider. For a service you rent, the setting may be fixed by the provider and invisible to you. Ask.

Two notes on testing. First, observe before changing: kernel statistics and logs show whether the backlog is overflowing at all. Second, a quiet server may never emit a cookie in the default setting, so absence of cookies is not evidence of a problem.

Where a WAF helps and where it does not

It does not help with the handshake. A WAF works on HTTP requests, and an HTTP request exists only after the TCP connection is open. A SYN, a SYN-ACK and the cookie check all happen below that point, in the kernel or a device in front of it, so a WAF neither sees these packets nor can change how they are answered. This page does not say that Bridge WAF or Bridge CDN uses SYN cookies; the technique is described as a general part of TCP.

What a WAF can do is deal with the next stage, after a connection exists. Bridge WAF has three modes:

off

Nothing is inspected. Requests reach your origin as they otherwise would.

monitor

Matches are recorded and every request is still served.

block

Matching requests are refused and still recorded.

Start in monitor, read what would have been refused, and move to block only when the matches are the traffic you meant to stop (WAF monitor mode). The activity log records what the filter flagged and shows nothing of handshakes that never completed. Per-client caps on request rates are on rate limiting. A server whose connection slots are all taken by half-open entries never reaches the point at which a filter could say anything, which is why the transport-layer defences on this page exist.

Questions

What is a SYN cookie?

A SYN cookie is an initial sequence number chosen by the server so that it encodes the connection details the server would normally store. The server keeps no record of the half-open connection. When the client's final ACK returns, the server checks that the acknowledgment number matches a valid cookie and only then creates the connection.

Do SYN cookies stop a SYN flood?

They remove the main target of a SYN flood, the table of half-open connections, because nothing is stored per request. They do not add bandwidth, so a flood that fills the network link is not helped, and the server still spends CPU time on every SYN. The Linux documentation calls the feature a fallback, not a way to carry legitimate connection load.

What do SYN cookies break?

Whatever the sequence number has no room for. RFC 4987 notes that the maximum segment size is squeezed into a few preset values, that window scaling is normally disabled, and that SACK and data carried on the SYN are affected. Connections set up this way can be slower for that reason.

Where are SYN cookies switched on?

In the TCP stack of the machine that accepts the connection. On Linux that is a kernel setting, tcp_syncookies, which by default sends cookies only when the SYN backlog of a socket overflows. Load balancers and other devices in front of a server may do something similar in their own stacks; check their documentation.

Does Bridge WAF use SYN cookies?

No claim is made that Bridge CDN or Bridge WAF uses them. Bridge WAF is an application-layer filter with three modes, off, monitor and block. It sees HTTP requests after the TCP handshake is complete, so SYN handling is below the layer it works on.