
# Network

A guest has **no network by default**: no interface but `lo`, no route to the host, its loopback
or the LAN. Containers of one runtime still reach each other, as `localhost` and by name (see
[The runtime and the pod](/concepts/runtime)). Everything else is something you give it.

## Connections in

- **From your code**, anywhere: `container.fetch()` sends an HTTP request to a port of the
  container, `container.connect()` opens a TCP connection to it. Nothing listens on the host.
- **From the host**, on Node: `publishPort()` (`untainer run -p`) serves one container port on
  the host, on `127.0.0.1` unless `host` says otherwise.

A browser tab cannot listen, so in a page the first way is the only one.

## Connections out

| Given with | What the guest reaches |
| --- | --- |
| nothing (the default) | Nothing outside the VM. |
| `net: policyBackend(directBackend())`, `untainer run --net` (Node) | The internet only. Each connection is checked on the address it goes to, and refused for the host's loopback and its own addresses, private, shared and link-local ranges (cloud metadata at `169.254.169.254` among them), multicast and reserved ranges, and their IPv6 equivalents. |
| `net: directBackend()`, `untainer run --net=all` (Node) | Whatever the host process reaches, the LAN and services bound to the host's LAN address included. Not the host's loopback: the guest's `127.0.0.1` is its own, and the gateway `10.0.2.2` is refused. |
| `net` with unkernel's wisp backend (browser) | What the wisp server you name allows. This is unkernel's; it is not tested with untainer. |

For an image you do not trust, use the policy or no network at all.

## The network policy

`policyBackend(inner, options)` (Node) wraps a backend: unkernel's network stack hands it each
guest connection with the IPv4 address the guest chose, and the policy refuses it before
anything is dialled when that address is blocked. The guest resolves names through the host
(`directBackend()`'s DNS), so a name that resolves to a local address is refused when the guest
connects to it. A backend that dials names itself gets the name resolved by the policy, refused
when any of its addresses is blocked, and dialled at the address that was checked, so that it
cannot resolve to another one in between. The guest has IPv4 only; IPv6 matters for names a
backend resolves, and the unit tests cover its classes.

- `untainer run --net` and `untainer up --net`: the internet only, with a warning on stderr for
  each address refused; `--net=all`: everything the host process reaches.
- SDK: `policyBackend(directBackend())`, `policyBackend(directBackend(), { private: false })`
  (the same as `directBackend()` alone), `onBlocked` to hear of refusals.
- Not in a worker: the policy is code of this thread, which unkernel cannot hand to a VM in a
  worker (it takes only a spec there: `directBackend()`, with no policy). A runtime with a policy
  and no `worker` option runs its VM on this thread; with `worker: true` it is `ERR_UNSUPPORTED`.

Tested by [`test/security.vm.test.ts`](https://github.com/OskarLebuda/untainer/blob/main/packages/untainer/test/security.vm.test.ts) (ports,
the network with and without `net`, the policy in the guest) and
[`test/net-policy.test.ts`](https://github.com/OskarLebuda/untainer/blob/main/packages/untainer/test/net-policy.test.ts) (every class of
addresses, IPv4 and IPv6, and names).
