# Flows

> Connection state for moat rules: let replies in, drop strays, cap connections per source.

Source: https://moatfw.dev/docs/flows

XDP sees each packet on its own. Flows add connection state: a table of
the connections the protected side takes part in, which rules read with
three fields.

| Field | Type | Meaning |
|-------|------|---------|
| `flow.new` | condition | no connection is known for the packet |
| `flow.established` | condition | a connection is known and the local side has sent in it |
| `flow.src_count` | 32-bit integer | open TCP connections whose remote end is the packet's source |

The fields are present on TCP, UDP, and ICMP or ICMPv6 echo requests and
replies. Other packets have no flow, so `flow.new` is false for them too:
ICMP errors, neighbour discovery, and fragments other than the first.

## Recipes

```moat
# Only these sources, and the replies to our own connections. ICMP
# errors keep path MTU discovery working, and IPv6 needs neighbour
# discovery.
set allowed prefixes
rule trusted { when ip.src in allowed then pass }
rule replies { when flow.established then pass }
rule errors { when icmp and icmp.type in {3, 11, 12} then pass }
rule icmp6_needed { when icmp6 and icmp.type in {1, 2, 3, 4, 133, 134, 135, 136, 137} then pass }
rule rest { when ip then drop }
```

Other uses, each in a ruleset of its own:

```moat
# TCP that starts nothing and belongs to nothing.
rule stray { when tcp and flow.new and not tcp.flags has {syn} then drop }

# At most 100 connections per source.
rule cap { when tcp.flags has {syn} and not tcp.flags has {ack} and flow.src_count >= 100 then drop }
```

For a complete ruleset built on these, with cases for every rule, see
`flows/web-server` in [the examples](https://moatfw.dev/docs/examples#flows): a default-drop
web server that lets in only replies to its own connections and caps
connections per source.

## How the table is kept

- A second program, on the interface's egress, learns connections from
  what the local side sends: the host, or the VMs behind it when the
  interface carries their traffic. It never filters.
- A connection the local side opens or answers starts out as opening.
  It becomes confirmed once an inbound packet of it passes the rules.
  For a connection that comes in, the server's SYN-ACK opens it and the
  client's ACK confirms it. An opening connection already counts as
  known and established, so that ACK passes `flow.new` and
  `flow.established` rules.
- Opening connections live in their own, smaller table and last 30
  seconds without a packet. A flood the rules let through, which the
  host answers but which never answers back (spoofed SYNs, queries to a
  UDP service), only churns that table. Confirmed connections stay. An
  RST the host sends never opens a connection, and inbound packets never
  create one.

## Lifetimes

- A packet that passes refreshes its connection. An idle confirmed
  connection is forgotten after the ttl, 24 hours by default.
- UDP and ICMP use the ttl too, but at most 2 minutes.
- After an RST, or a FIN from both sides, a connection lingers 10
  seconds. A FIN from one side alone (a half close) changes nothing. A
  SYN on the ports of a closing connection starts a new one.
- moat does not track sequence numbers, so an inbound RST with a
  connection's addresses and ports starts those 10 seconds even when the
  host would ignore it; whoever can guess the ports can make a
  connection count as new again 10 seconds later.

## Counting per source

`flow.src_count` counts confirmed TCP connections until they close or
expire. It is an approximation:

- a connection that vanishes without FIN or RST counts until it expires;
- when the table is full, the least recently used connections are
  forgotten without being taken off their source's count, so the count
  can run high;
- a count not updated for the ttl reads as 0;
- under heavy concurrency the count is approximate.

## Warm-up

When the egress program starts learning (the first load, a load after an
unload, the interface coming back), it does not know the connections
already open. For 60 seconds, an inbound TCP packet without SYN, a UDP
packet, or an echo reply that matches nothing is adopted as an opening,
established connection, so open connections are not cut. That minute
admits such packets from anyone, and an adopted connection the host does
not answer within 30 seconds is forgotten. `moat reload` keeps the table
and does not warm up again.

## Sizing

`flows [ttl D] [size N]` sizes the table: N confirmed connections (default
256k, that is 256000; at most 4194304), forgotten after D idle (default
24h; at least 1s). The opening
table holds a quarter of N, at least 1024. Without the declaration the
defaults apply as soon as a rule reads a flow field.

## Requirements and operations

- Flows need Linux 6.6 or newer, which attaches the egress program with
  TCX. `moat load` refuses rules that read flow fields on older kernels,
  and `moat doctor` says whether the kernel can run them.
- `moat load` also attaches the egress program and `moat reload` replaces
  it; the table is kept like a set, unless its size changed. `moat unload`
  removes the egress program too.
- `moat status` shows how many connections the table holds, confirmed and
  opening, and whether it is still warming up.
- `moat metrics` exports `moat_flows`, with `state="confirmed"` or
  `state="opening"`, and `moat_flows_size`.
- In case files, `dir = "out"` makes a case's packet one the local side
  sends: it runs the egress program, so the case has no `expect`. Cases
  run in file order, so a test can send a SYN out and then check that the
  reply passes. `icmp_id` sets the identifier of an echo. Captures
  replayed with `--pcap` are all treated as inbound.
