Skip to content

agent win: Support several simultaneous tunnels - #446

Open
sa363 wants to merge 11 commits into
OpenVPN:masterfrom
sa363:win-agent-multi-session
Open

sa363 wants to merge 11 commits into
OpenVPN:masterfrom
sa363:win-agent-multi-session

Conversation

@sa363

@sa363 sa363 commented Oct 16, 2026 •

Copy link
Copy Markdown

This series lets several clients use ovpnagent at the same time on Windows, so that several VPN connections can be up at once, each run by its own omicliagent process. Today the agent keeps a single tunnel, and a /tun-setup from a second client destroys the tunnel of the first one.

I'm sending this as a PR for initial review and discussion, as described in CONTRIBUTING.md; the final patches will go to openvpn-devel.

Agent (openvpn/ovpnagent/win)

  • One session per client. The per tunnel state (tun setup, client process, confirm and destroy events, ring buffer, bypass routes, VPN interface index) moves into a TunSession, kept in a map keyed by the PID of the named pipe client. Requests only touch the session of the calling client; a session is torn down when its client exits or signals the destroy event.
  • omi_exe_path is optional. It is only read for /start, so the agent no longer exits on startup when the registry value is missing.
  • Refuses to run next to another agent. With ovpnagent run started while the service is up, both serve \\.\pipe\ovpnagent. Windows reports the process that created the pipe first as its server, so once that one is gone clients fail to validate the server and every request fails with "communication error".
  • Tunnels don't get in each other's way.
    • BestGateway takes a set of excluded interfaces, and server bypass routes skip the VPN interfaces of all tunnels.
    • The WFP filters of block-outside-dns / block-local of one tunnel get permit filters for the interfaces of the others, updated as tunnels come and go.
  • Only one tunnel may redirect the default gateway. Pushed options are only known at /tun-setup, so a second such tunnel is refused there with an error naming the process that already redirects it.
  • Shared block-ipv6 routes. The blackhole routes are the same for every tunnel: TunWin::Setup can leave them to its owner, and the agent adds them for the first session that needs them and removes them with the last one. Before, the first tunnel to go down removed them for all.
  • Route conflicts are reported. Identical routes of two tunnels are reported to the client as ROUTE CONFLICT: ... in the reply; nested ones are logged as ROUTE OVERLAP.

Client side

  • cmdagent errors. The transaction status or the agent's error text goes into the exception; before it was written to a stream that is only logged on success, so clients only saw "communication error" / "request error".
  • omi profile files. Profiles are read through ProfileMerge (FOLLOW_FULL, as in the test client), so ca ca.crt, tls-auth ta.key 1, auth-user-pass file and the other file references of OpenVPN 2.x profiles work.
  • omi DCO fallback. DCO is disabled, with a log line saying why, for profiles that are not DCO compatible (compress, comp-lzo, http-proxy), instead of failing with "not compatible with dco". windows-driver only selects the driver used then.

Testing

On Windows 11 x64 with ovpn-dco-win and TAP-Windows6, MSVC 2022, preset win-x64-release:

  • Three simultaneous connections to three servers through the agent running as a service, each on its own ovpn-dco-win adapter:
    • two split tunnels with block-ipv6, one of them with block-outside-dns;
    • one profile with route-nopull that drops a pushed redirect-gateway.
    • Checked in agent.log and with route print / netsh: bypass routes go through the physical gateway; the WFP filters of the block-outside-dns tunnel permit the two other VPN interfaces, so the split DNS of another tunnel keeps working; the block-ipv6 routes stay while one of the two tunnels that need them is up; the identical route pushed by two of the servers is reported.
  • A TAP and a DCO tunnel at the same time, with an earlier revision of the series.
  • Profiles with external files: same directory, subdirectory, absolute paths with / and \\, tls-auth with key direction, auth-user-pass file, a missing file.
  • DCO fallback for comp-lzo and http-proxy; windows-driver wintun without Wintun adapters now uses DCO.
  • A second agent refuses to start while the service runs.
  • Unit tests. New BestGateway tests, and the rest of coreUnitTests passes except tests that need administrator rights (IpHelperTest.TestAddRoute4) or the OpenSSL legacy provider module (Ssl.EnablelegacyProvider, Proto.IvCiphersLegacy), which are missing in this environment and unrelated to the changes.

The Build workflow passes in the fork for MSVC x64, x86 and arm64 and MinGW x64 and x86, including the unit tests on MSVC x64 and x86 (https://github.com/sa363/openvpn3/actions/runs/38891819005).

Not tested: Wintun.

Not part of this PR

A Qt tray GUI that runs several connections through these pieces, and an MSI installer for it, live in the agent-multi-session branch of the fork (gui/, doc/windows_multi_connection.md). I left them out, as they are not part of the core library; happy to discuss if there is interest.

🤖 Generated with Claude Code

Анисимов Сергей Викторович and others added 11 commits October 16, 2026 12:16
The agent kept a single tunnel, so a tun setup request from a second
client destroyed the tunnel of the first one.

Move the per-tunnel state (tun setup, client process, confirm and
destroy events, ring buffer, bypass routes, VPN interface index) into
a TunSession class and keep sessions in a map keyed by the PID of the
named pipe client. Requests only affect the session of the calling
client, and a session is torn down and forgotten when its client
exits or signals the destroy event. On service stop all sessions,
including their bypass routes, are closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
The agent read HKLM\SOFTWARE\OpenVPN\omi_exe_path on startup and
exited when the value was missing, although only the /start request
uses it. Clients which start their own process, such as omicliagent
launched by a GUI, could therefore not use the agent without that
registry value.

Read the path in start_openvpn_process() instead, so a missing value
only fails the /start request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
The OMI client read the profile as is, so directives that refer to
external files, such as "ca ca.crt" or "tls-auth ta.key 1", failed
with errors like "CA not defined". OpenVPN 2.x profiles commonly keep
keys and certificates in separate files.

Read the profile through ProfileMerge with FOLLOW_FULL, like the test
client does, which inlines ca, cert, key, tls-auth, tls-crypt,
auth-user-pass, crl-verify, pkcs12 and the other file references.
Relative paths are resolved against the directory of the profile.
Reading the profile from stdin is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
DCO is enabled by default, and the client throws "config/options are
not compatible with dco" for profiles with compress, comp-lzo or
http-proxy, which are common in OpenVPN 2.x profiles.

Like openvpn2, disable DCO and use the TAP or Wintun driver when the
profile is not DCO compatible or asks for another windows-driver, and
log why.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
On a failed agent transaction the details were written to a stream
that is only logged on success, so clients only reported
"communication error" or "request error". Include the transaction
status, or the error text returned by the agent, in the exception.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
Running "ovpnagent run" while the service is up makes both serve
\.\pipe\ovpnagent. Windows reports the process that created the pipe
first as its server, so once that process is gone clients fail to
validate the server and every request fails.

Exit with an error when the pipe is already served.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
With several tunnels up at the same time the agent only considered
the tunnel of the requesting client:

- The server bypass route excluded only the client's own VPN
  interface, so with another full tunnel up it went through that
  tunnel instead of the physical network.
- The WFP filters of block-outside-dns or block-local permitted only
  the client's own interface and blocked DNS and other traffic of the
  other tunnels.

BestGateway now takes a set of excluded interfaces, and TunWin::Setup
and the WFP context keep the interfaces of the other tunnels: the
bypass route skips them and extra permit filters let their traffic
through. The agent passes them when a tunnel is set up and updates
the other sessions when a tunnel comes up or goes down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
Two tunnels with redirect-gateway would both install the 0/1 and
128/1 routes. The pushed options are only known when the tunnel is
set up, so refuse a second such tunnel in /tun-setup with an error
that names the process whose tunnel already redirects the gateway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
The DCO fallback also disabled DCO for profiles with a windows-driver
other than ovpn-dco. With "windows-driver wintun" and no Wintun
adapter installed, which is common as openvpn 2.7 dropped Wintun,
the connection then failed with "cannot acquire TAP handle" although
free DCO adapters were available. The core used DCO for such profiles
before.

Only fall back from DCO when the profile is not DCO compatible;
windows-driver just picks the driver used then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
block-ipv6 adds the same blackhole routes (2000::/4, 3000::/4,
fc00::/7 on the loopback interface) for every tunnel. With two such
tunnels the second add failed with "already exists", and the first
tunnel to go down removed the routes for both, so IPv6 traffic of the
other one was no longer blocked.

TunWin::Setup can now leave these routes to its owner. The agent adds
them for the first session that needs them and removes them with the
last one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
When two tunnels push the same route, e.g. two gateways of one
organization, destinations in it may go through either tunnel.

Compare the routes of a new tunnel with those of the other sessions
and add "ROUTE CONFLICT: <route> overlaps <route> of the tunnel of
process <pid>" for identical routes to the reply, which the client
logs. Nested routes are only logged as "ROUTE OVERLAP", as the more
specific route always wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Анисимов Сергей Викторович <s.anisimov@itfbgroup.ru>
@sa363
sa363 force-pushed the win-agent-multi-session branch from 654711c to 82daa47 Compare October 16, 2026 11:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant