Skip to content

TUN mode

Source files: 13 · checked against muffin v0.3.0
  • muffin/crates/muffin-service/src/privileged/linux.rs
  • muffin/crates/muffin-service/src/privileged/macos.rs
  • muffin/crates/muffin-service/src/privileged/windows.rs
  • muffin/crates/muffin-service/src/netroute/mod.rs
  • muffin/crates/muffin-service/src/netroute/linux.rs
  • muffin/crates/muffin-service/src/netroute/split.rs
  • muffin/crates/muffin-service/src/render.rs
  • muffin/crates/muffin-service/src/kernel.rs
  • muffin/crates/muffin/src/pages/overview.rs
  • muffin/crates/muffin/src/pages/settings.rs
  • muffin/crates/muffin/src/service_manager/linux.rs
  • muffin/crates/muffin/src/service_manager/macos.rs
  • muffin/crates/muffin/src/service_manager/windows.rs

In TUN mode, Muffin creates a virtual network interface and routes the system’s traffic into it, so every program goes through Muffin, including those that ignore the system proxy. Creating an interface needs administrator rights, which Muffin asks for once, when you grant the TUN permission.

  1. Grant the permission. Open Settings › TUN, and next to Privileged helper click Grant Permission…. Your system asks for an administrator password, as described below. Muffin then restarts its service with the permission, and the row says Granted.

  2. Switch TUN mode on: TUN Mode on the Overview, TUN mode in Settings › TUN, or TUN Mode in the tray menu. Settings › TUN then says Running on <device>, and the sidebar’s status card shows TUN Mode.

Until you grant the permission, every TUN Mode switch is disabled, and the Overview’s TUN Mode switch says Needs permission · Grant…: Grant… starts the same grant. While TUN mode is switched on but the device does not run, Settings › TUN says Turned on, but not running.

Muffin does not run as an administrator. Granting the permission installs a second, privileged copy of its service, which can create the interface; the window starts that copy instead of the ordinary one from then on.

Grant Permission runs pkexec, which asks for your password through the desktop’s polkit agent. It copies the service to /var/lib/muffin/bin/muffin-service, owned by root and your group, and gives that copy the capabilities cap_net_admin, cap_net_bind_service and cap_net_raw. The service runs as your user, as a systemd user unit, with those capabilities.

When pkexec is missing, no polkit agent runs, or the prompt fails, Muffin shows the equivalent sudo command with a Copy button. Run it in a terminal, then close the dialog to restart the service.

The privileged copy must be the same version as the window. After you upgrade Muffin, grant the permission again; until then the ordinary service runs and TUN mode is not available.

Revoke removes the privileged copy, behind the same administrator prompt, and restarts the ordinary service. The Overview’s footer says Privileged Mode while the privileged copy runs, and User Mode otherwise.

The Device group of Settings › TUN holds four values, saved in the [tun] table of settings.toml. Edit… changes the first three; IPv6 is a switch:

Setting Default Meaning
Device Name muffin0 The interface’s name. macOS names it utun<N> unless you write such a name.
MTU 1500 From 1280 to 9000
Bootstrap DNS 223.5.5.5:53 The DNS server, an IP address and port, that the proxy itself uses in TUN mode, over UDP, outside the tunnel
IPv6 off Relay IPv6 through the interface. Off, IPv6 connections into the interface are refused.

With IPv6 off, every system still routes IPv6 into the interface, which refuses IPv6 connections, so programs fall back to IPv4 at once and no IPv6 traffic goes around Muffin.

The Advanced group below it lists Stack, Excluded routes and DNS hijack, marked Needs engine: Muffin 0.3.0 has no such settings.

Switching TUN mode on or off restarts the kernel, and so does changing Device Name, MTU or IPv6 while TUN mode is on: open connections end. If the new configuration fails to start, Muffin brings the previous one back and shows the error. A new Bootstrap DNS does not restart the kernel: Muffin reloads it in place, as etemenanki-app reloads a changed [dns] table (see Hot reload). While TUN mode is off, these values are only saved.

The interface takes the address 172.19.0.1/30, and also fdfe:dcba:9876::1/126 with IPv6 on, or always on macOS and Windows. The proxy’s own connections to its servers must not loop back into the interface, so each system keeps them out a different way. Only the kernel’s connections, and the node latency test, are kept out: the service’s other connections, such as subscription and geodata downloads, go into the interface like any other program’s and follow the rules.

Muffin adds a default route through the interface in a routing table of its own, 28021, and policy rules at priorities 28000 to 28004 that send traffic there:

  • the proxy’s own sockets carry the firewall mark 0x6d75 and stay on the main table;
  • DNS, on port 53 over UDP and TCP, always goes into the interface, even to a resolver on your local network;
  • routes in the main table that are more specific than a default route, such as your local network, still apply.

Muffin removes these rules when TUN mode turns off, when the service stops, and when it starts, in case a crash left some behind.

In TUN mode, programs’ DNS queries go into the interface, and the profile’s [dns] section decides how they are answered; see Subscribe files.

  • A profile whose [dns] names the system resolver cannot run in TUN mode: the system resolver would send its queries back into the interface. Turning TUN mode on with such a profile fails with TUN mode cannot use the "system" DNS server in this profile's [dns].
  • A profile without [dns] runs, with a warning in the logs, and programs’ DNS queries are routed like any other traffic.
  • The proxy’s own lookups, such as the names of its servers, use Bootstrap DNS.