Skip to content

Networking ​

Every cairn machine gets a private network of its own, reaches the internet through NAT, and is reachable from the host only on the ports you publish. This page explains networks, published ports, DNS, the egress allowlist, the cairn net commands and the two privileged helpers behind it all. The [network] and [[services]] keys are in manifest.

What you get by default ​

You do not need to configure anything. A deploy of an app named notes:

  • creates a network named notes (the app's own) if it does not exist, and starts it;
  • gives the machine an address on a private guest subnet (default 10.100.0.0/24; the machine gets .2, the gateway is .1);
  • lets the guest open connections to the internet, translated through the host's address;
  • uses the host's DNS resolvers for the guest;
  • publishes only the ports in [[services]], on 127.0.0.1.
toml
[[services]]
internal_port = 8080            # the guest's port
  [[services.ports]]
  port = 8080                   # the host's port, on 127.0.0.1

Different networks can use the same subnet number, because each lives in its own network namespace. This host runs several networks that are all 10.100.0.0/24.

Published ports ​

Each [[services.ports]] entry forwards a host port to the service's internal_port.

  • The default bind address is 127.0.0.1, reachable only from this host. Set bind = "0.0.0.0" for every address, or the address of one host interface.
  • protocol is tcp (default) or udp on the service.
  • A host port can go to only one guest port. Publishing the same host port twice in a manifest is an error.
  • Binding below port 1024, or publishing on an address other than 127.0.0.1, is allowed only for users the administrator listed in /etc/cairn/net.conf (allow_privileged_ports, allow_public_publish, allow_public_uids). Per user the defaults are at most 16 networks and 64 published ports. See configuration.
  • Cairn reserves the port on the host when the machine attaches to its network. If another program already holds it, the new machine does not start, the deploy fails naming the port (PortInUseOnHost), and the previous machine runs again.

A guest cannot reach a published port through the host's address, not even its own app's. There is no hairpin. A port published on 0.0.0.0 or a LAN address is for clients outside.

A redeploy stops the old machine, then starts the new one, which publishes its ports as it attaches: clients see one gap. Ports you remove from the manifest are unpublished then. A stopped app keeps its ports reserved: a client on this host is accepted and then closed, within about 3 s, and a remote one gets no answer until the app runs again. A paused machine keeps its ports, but connections to them are reset after about 3 s.

Networks ​

A network is the thing a machine attaches to. It has a name (1 to 32 lowercase letters, digits or inner hyphens), a guest subnet, an optional egress allowlist, and a small private "transit" link to the host. Several apps can share one network (see Sharing a network). --network <name> (or [network] name) joins a named network instead of the app's own.

sh
cairn net ls
cairn net inspect notes
console
$ cairn net ls
NAME          SUBNET         STATE  RUNNING  MEMBERS  LEAVING  ATTACHED  PORTS  STALE  UNREADABLE
sample-nginx  10.100.0.0/16  ready  running  1        0        1         1      0      0
CommandWhat it does
cairn net create <name> [--subnet CIDR]Defines and starts a network: a systemd user unit runs its agent, cairn-netd. The subnet is /16 to /29 (default 10.100.0.0/16) and must stay out of the transit pool.
cairn net lsLists networks: subnet, recorded state, whether it runs, members, leaving members, attached machines, published ports, and stale or unreadable records.
cairn net inspect <name>Shows each app (address, attached, services, published ports), the egress list and the gate's last ten refusals, and every difference as a fact, such as a wanted port that is not published and why. While the network is not running its taps are shown as unknown.
cairn net up [name] [--all]Starts a stopped or failed network, restarts one whose agent does not answer, then repairs it. Safe to repeat.
cairn net destroy <name>Refused while any app is a member (cairn destroy <app> first). Stops the network and removes its host state, unit and definition. Running it again finishes an interrupted destroy.

cairn stop and cairn destroy <app> leave the network in place, since you may share it.

A network's unit is cairn's alone: cairn changes it only when the unit file is the one it wrote and the running agent is the one that file starts. Drop-ins from root-owned directories, such as Fedora's 10-timeout-abort.conf, which applies to every user service, are accepted. A drop-in you can write yourself, for example one under ~/.config/systemd/user/, makes cairn refuse the network with UnitNotOwned until you remove it.

Stopping or restarting a network's agent does not stop the network: running machines keep their traffic, which the kernel forwards. DNS answers, new connections to published ports and new attaches wait until it runs again; cairn net up brings it back.

Network mode off ​

--network-mode off ([network] mode = "off") gives the machine no network at all except loopback. It cannot publish ports, and it takes no name, dns or allow.

Sharing a network ​

Apps deployed to the same named network can reach each other by name. Say a front proxy and a backend, with only the proxy published on the host:

toml
# api/cairn.toml
app = "api"
[network]
name = "shop"
[[services]]
internal_port = 8080          # reachable inside "shop" as api.internal:8080, not published

# front/cairn.toml
app = "front"
[network]
name = "shop"
[[services]]
internal_port = 80
  [[services.ports]]
  port = 8080                 # only the front is published on the host

Create the network once (cairn net create shop), then deploy both apps. The front's nginx configuration can say proxy_pass http://api.internal:8080/;, and it keeps working across redeploys of api.

Names. Each app on a network is reachable as <app>.internal. The guest's resolver also searches internal, so a bare api works as a typing convenience; examples and tools use the qualified name. An app named with digits only (123) cannot be resolved bare, because getaddrinfo("123") is an address; 123.internal works.

App addresses. Every app gets one address on the network, picked at random from the free ones, and its machine uses it. The address belongs to the app for as long as it is a member of the network. It does not change when the app is redeployed, stopped or started. An address an app gave up is not picked again while its cleanup is under way. <app>.internal resolves to it, and cairn net inspect shows it.

What a peer may reach. A peer reaches another app only at its app address, and only on the ports the app declares as [[services]] (internal_port, with its protocol). Everything else is refused. A [[services]] entry without ports is reachable by peers only; with ports it is also published on the host. There is no per-peer rule: a peer reaches every declared service. A service you declare only so that readiness waits for it is reachable by the other apps on the network too. cairn deploy prints what became reachable the first time an app joins a network.

Isolation. Machines on a shared network cannot reach each other at layer 2, see each other's broadcasts or send as another machine's address. They reach each other only at the declared services.

Egress is per app. An app's [network] allow applies to its own machines only. It never filters traffic to a peer, and another app's deploy cannot change it. A network holds at most 16 distinct allowlists at once; the 17th is refused with a message that lists which apps hold the others.

A stopped app does not answer. While an app is stopped (a redeploy stops it first), a peer's connection to it gets no answer and times out; nothing refuses it. The app's name still resolves while it is stopped, to its reserved address. Once the app is destroyed its name no longer resolves (NXDOMAIN).

Resolvers.

The guest's dns settingNames
"host" (default)The gateway owns the whole internal zone: api.internal resolves, and a name in it that is not an app (missing.internal) is NXDOMAIN, whatever the host's resolvers hold. The bare api resolves through the guest's search domain (internal), which turns it into api.internal; a query for the single label itself (dig api, no search) is answered NXDOMAIN, as is any single label (acer). Everything else is forwarded to the host's resolvers; under an allowlist, only the names it allows (see Egress allowlist).
[ "1.1.1.1", ... ]The guest uses those resolvers: app names do not resolve. The first deploy of such an app to a named network says so.
cairn run appsnameserver only, no internal search: a run app's network is its own.

Limits.

  • An app runs one machine at a time; there is no load balancing. A redeploy stops the old machine before it starts the new one, so peers see a short gap.
  • Names resolve only with dns = "host".
  • The server sees the gateway's address when an app calls its own app address.
  • Peer traffic is routed and tracked: bulk throughput is roughly 1 to 15% lower than machine-to-machine on one bridge.
  • Connection tracking is per network. One client opening connections to one server port as fast as it can plateaued at about 14,100 entries; each further client and server pair adds its own. The kernel's maximum of 262,144 applies per network and cannot be changed from inside.
  • A cairn run app keeps a private network of its own; it cannot join a shared one, and a deploy cannot join a run app's network.
  • cairn net destroy is refused while any app is a member, leaving ones included; the message names the apps.
  • cairn changes a network's systemd unit and its boot link (~/.config/systemd/user/default.target.wants/) only while it holds its own lock. Do not edit, enable or disable a cairn-net-*.service by hand while a cairn net command runs. If you did, run cairn doctor, then cairn net destroy or cairn net create again.

Upgrading ​

Networks made by an older cairn are not supported, and this version does not read, list or remove them. Before installing it, remove them with the old build: stop and destroy each app on them, then cairn net destroy each network.

DNS ​

SettingBehaviour
dns = "host" (default)The guest asks the gateway address, which answers the whole internal zone itself (<app>.internal for the apps on the network, NXDOMAIN for any other name in it) and a single label NXDOMAIN, and forwards other names to the host's own resolvers (under an allowlist, only the names it allows). A deployed app also searches internal, so a bare api finds api.internal.
dns = ["1.1.1.1", "9.9.9.9"]The guest uses 1 to 3 resolvers you name. They must be reachable by guests (not a blocked range).

An app that names its own resolvers cannot resolve app names (see Sharing a network). Set it with [network] dns or --dns host|ip[,ip...]. You cannot combine an explicit dns list with an allowlist: under an allowlist the guest asks the gateway, which answers names in internal itself (an app's name, or NXDOMAIN for one that is not an app, whatever the host's resolvers hold), answers a bare single label NXDOMAIN, refuses a name the list does not allow, and forwards only allowed names to the host's resolvers.

Egress allowlist ​

An allowlist makes the guest's traffic leave through a gate that admits only the names and addresses you list. It keeps an image in its lane. It is not a sandbox for a hostile image.

sh
cairn deploy --allow pypi.org,files.pythonhosted.org,140.82.112.0/20
cairn deploy --allow ''          # no outbound traffic; only app names on the network resolve
toml
[network]
allow = ["pypi.org", "files.pythonhosted.org"]

With no allow, the machine has whatever the host's NAT reaches. An empty list is different from no list: it allows nothing. On this host, --allow '' blocks all egress, and every name lookup except the network's own app names (api.internal still resolves, from the gateway); and --allow example.com lets that name through while connections to other IPs stay blocked.

An entry is one of:

  • a name, pypi.org: HTTPS on 443 and HTTP on 80 to that name;
  • a wildcard, *.pythonhosted.org: the same, for any name ending in the zone. It does not cover pythonhosted.org itself: list the apex too;
  • an address or range, 140.82.112.0/20 or 8.8.8.8: everything at those addresses, on every port.

DNS is part of the policy for external names. The guest's lookups of names outside the network are answered only for names the list opens, and the gate resolves a listed name itself, so the guest cannot point a name at an address you did not intend. TLS is relayed by name and not terminated, so you see the real server's certificate.

Limits, in plain words:

  • For external names, only A questions are answered. For a listed name AAAA and HTTPS get an empty answer; TXT, SRV, MX and the rest are refused. None of this applies to names under internal (below): the network's own resolver answers those, of any type, and the allowlist never sees them, even with allow = [].
  • Names cover TCP 443 and 80 only. SSH to github.com:22 needs GitHub's range listed.
  • An IP entry opens the whole address on every port. Listing a public DNS resolver's address is a DNS bypass.
  • A wildcard trusts whoever runs the zone. Prefer exact names.
  • Changing the list ends open connections through the gate, including a DNS connection the guest kept open. The gate restarting does the same.
  • A single-label question (acer) is answered NXDOMAIN, and never leaves the network. Names under internal belong to the network itself: the gate passes those questions to the network's own resolver unchanged, whatever their type, and they never count against the list. Any other name is refused unless the list allows it; an allowed name's A question is forwarded to the host's resolver as asked, so a guest learns that name's address in a private zone the host's resolver knows. A name the list does not allow is refused even if the host's resolver knows it.
  • A host whose own interface has a public address is not protected by the built-in floor: a listed address that covers it is admitted.

When a machine cannot reach a name you expected, run cairn net inspect <network>. Every refusal is also written to $CAIRN_HOME/run/net/<network>/egress.log. cairn status <app> marks the network row when a list applies.

A deploy that changes allow gives the new list to the new machine. --replace-allowlist is accepted and does nothing.

For cairn run, a list takes effect at once, belongs to the app's network and applies to every later session. cairn run --allow any removes it. See run.

Firewall, Docker and firewalld ​

Cairn's host rules live in their own nftables table, table inet cairn. It blocks guest traffic to a built-in list of special-use ranges and the host's own addresses. Ranges you add with extra_blocked in /etc/cairn/net.conf are blocked too. Cairn leaves firewalld alone and touches Docker's chains only as described below.

  • If Docker's FORWARD policy is DROP, or a DOCKER-USER chain exists, the helper makes sure two narrow rules are present (in DOCKER-USER when it exists, else first in FORWARD), because a drop in any table beats cairn's accept. It records them and removes them when no networks remain.
  • firewalld needs nothing from cairn and cairn does not change it. cairn doctor reports whether it runs and checks its uplink zone.
  • A deleted table inet cairn is put back by the helper's timer within a minute. cairn net up <network> does it at once. cairn doctor and cairn net inspect show whether it is present.
  • Cleanup of connections on a removed port deletes only conntrack entries cairn itself translated. A foreign DNAT of Docker's or firewalld's is never touched.
  • The transit pool (default 10.250.0.0/16, one /30 per network) must not overlap a route on the host. cairn doctor checks this.

The kernel must be Linux 6.17 or later for the network helper to tell its user namespaces from the host's.

The pieces ​

You never run these yourself, but their names appear in doctor and ps.

  • cairn-helper is the one privileged program: setuid to a cairn service account with only CAP_NET_ADMIN and CAP_NET_BIND_SERVICE. It does what a user cannot: creates the network's veth link, writes the nftables table, reserves host ports, cleans conntrack, and enforces /etc/cairn/net.conf. It takes the caller's uid from the kernel and keeps a ledger of what each user owns, so one user cannot touch another's network. Its audit detail is in its SECURITY.md.
  • cairn-netd is the per-network agent, run as you inside a user namespace and network namespace. It holds the guest's gateway, tap devices, the forwards and the DNS front. One systemd unit per network keeps it running.
  • cairn-egress is the gate that enforces the allowlist inside the network namespace: DNS answers, plain HTTP by Host header and TLS by SNI.

See architecture and security for how they fit together.