moat docs ยท language 1

Language Sets and variables

Sets and variables state that changes while the rules run.

Sets

FormKindHolds
set NAME from "FILE"prefixesLoaded from a file at load time.
set NAME prefixesprefixesStarts empty; filled from outside.
set NAME addressesaddressesExact addresses; filled from outside or by rules.

size N sets the capacity, from 1 to 100000000. The default is 65536; for a set loaded from a file it is at least twice the number of entries. ttl D (addresses sets only, at least 1s) makes entries expire.

An addresses set is a least recently used map: when it is full it forgets old entries rather than refusing new ones. The kernel keeps its free entries per CPU, so a very small set, fewer than about a hundred entries per CPU, can forget entries before it is full. Give sets room. Entries from a file or set sync are no exception: rules that add to a full set push them out, so a set that holds a list and also collects addresses from rules needs room for both.

A set file holds one address or prefix per line. # starts a comment, blank lines are ignored, a UTF-8 BOM and CRLF line endings are accepted, and file paths are relative to the .moat file.

An IPv4-mapped address or prefix, such as ::ffff:192.0.2.1 (the way programs listening on [::] log IPv4 clients), means the IPv4 one. The whole mapped range, ::ffff:0:0/96 as bogon lists carry it, stays IPv6, as it would in a rule; it does not turn into all of IPv4.

Keyed sets

keyed after the kind gives a set one list per key. A program has at most 64 BPF maps and a prefixes set takes 2, so a file that needs many lists (one per customer, one per country) keeps them all in one keyed set:

set lists prefixes keyed size 1M
set geo from "countries.txt" keyed
set bans addresses keyed ttl 1h

rule allow_345 { when ip.dst == 192.0.2.10 and ip.src in lists[345] then pass }
rule iberia { when ip.dst == 192.0.2.10 and ip.src not in geo[ES, PT] then drop }
rule ssh { when tcp.dport == 22 then limit 5/m per ip.src burst 5 on exceed add ip.src to bans[12], drop }

A key is a number from 0 to 2147483647, a constant holding one, or a country code in capitals. A constant cannot have the name of a country code.

  • ADDR in SET[K1, K2] is true when the address is in the list of any of the keys, up to 16 in one test. add takes exactly one key.
  • A keyed set is always used with keys, and a plain set never is.
  • size counts the entries of all keys together, ttl applies to all of them, and the maps are the same as for a plain set.
  • The keys share that capacity: when a keyed addresses set is full, a rule's add makes it forget the least recently used entries of any key, so give such a set room for every key's entries at once.

A keyed set's file holds KEY ENTRY lines:

# countries.txt
ES 2.136.0.0/13
ES 2a0c:5a80::/29
PT 2.80.0.0/12

To change one list while the rules run, see sets at run time.

Variables

var udp_limit = 5000
var under_attack = false

rule strict { when under_attack and udp.dport != 27015 then drop }
rule udp_flood { when udp then limit udp_limit/s per ip.src burst 10000 }

A variable holds a number or true/false. A true/false variable is a condition on its own. A number can be compared with an integer field (==, !=, <, <=, >, >=, not in) and used as the rate or the burst of a limit. A rate of 0 turns the limit off; a burst of 0 lets no packet through below the rate. Used in a limit, a variable holds at most 1000000000, and so does a burst written next to a variable rate.

Values are read for every packet, so moat var set changes behaviour at once. load starts from the values in the file; reload keeps the current value of every variable that still exists with the same kind, unless the new rules use it where that value does not fit. Then it goes back to the file's value, and reload says so: "variable X held a value the new rules cannot use and is back to the file's".

The unit that moat enable installs reloads from the file when the interface comes back, so values set by hand are lost then.