nvpn 4.1.9

CLI and daemon for Nostr VPN private mesh networks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#[cfg(target_os = "linux")]
impl FipsPrivateTunnelRuntime {
    fn reconcile_linux_exit_node_forwarding(
        &mut self,
        config: &FipsPrivateTunnelConfig,
        seller_egress_ready: bool,
    ) -> Result<()> {
        let local_address = config.local_address.as_str();
        let seller_egress = config.local_exit_seller_egress.as_ref();
        let wireguard_exit = &config.wireguard_exit;
        let ipv4_mss_clamp = exit_node_ipv4_mss_clamp(config.mesh_mtu.tunnel);
        let mut route_families =
            crate::linux_exit_node_default_route_families(&config.local_exit_forwarding_routes);
        if route_families.ipv6 {
            eprintln!(
                "fips: IPv6 exit-node forwarding is disabled until nvpn has IPv6 mesh source filtering"
            );
            route_families.ipv6 = false;
        }
        // WG upstream as this host's own egress does not imply mesh
        // exit-node forwarding. Only advertised default routes should
        // turn on ip_forward/NAT below.
        let needs_ipv4_tunnel_source = route_families.ipv4 || wireguard_exit.enabled;
        let ipv4_tunnel_source_cidr = if needs_ipv4_tunnel_source {
            let Some(tunnel_source_cidr) = crate::linux_exit_node_source_cidr(local_address) else {
                self.reconcile_linux_exit_node_forwarding_cleanup()?;
                return Err(anyhow!(
                    "invalid IPv4 tunnel address '{local_address}' for exit forwarding"
                ));
            };
            Some(tunnel_source_cidr)
        } else {
            None
        };

        let wireguard_exit_iface = if wireguard_exit.enabled {
            let Some(source_cidr) = ipv4_tunnel_source_cidr.as_deref() else {
                self.reconcile_linux_exit_node_forwarding_cleanup()?;
                return Err(anyhow!("WireGuard exit has no IPv4 tunnel source"));
            };
            match crate::validate_linux_wireguard_exit_config(wireguard_exit) {
                Ok(iface) => {
                    if !crate::linux_wireguard_exit_ipv6_default(wireguard_exit) {
                        route_families.ipv6 = false;
                    }
                    if let Err(error) =
                        self.apply_linux_wireguard_exit_upstream(wireguard_exit, source_cidr)
                    {
                        let _ = self.cleanup_linux_exit_node_forwarding_rules();
                        self.block_linux_wireguard_exit_if_strict(config.exit_node_leak_protection);
                        return Err(error).context("failed to configure WireGuard exit upstream");
                    }
                    Some((iface, source_cidr.to_string()))
                }
                Err(error) => {
                    let _ = self.cleanup_linux_exit_node_forwarding_rules();
                    self.cleanup_linux_wireguard_exit_upstream()?;
                    self.block_linux_wireguard_exit_if_strict(
                        config.exit_node_leak_protection && wireguard_exit.enabled,
                    );
                    return Err(error).context("WireGuard exit upstream is not ready");
                }
            }
        } else {
            self.cleanup_linux_wireguard_exit_upstream()?;
            None
        };

        if !route_families.ipv4 && !route_families.ipv6 {
            self.cleanup_linux_exit_node_forwarding_rules()?;
            return Ok(());
        }

        let seller_egress_ready = match seller_egress {
            Some(PaidExitSellerEgress::WireGuard) => wireguard_exit_iface.is_some(),
            Some(PaidExitSellerEgress::Direct | PaidExitSellerEgress::PrivatePeer { .. }) => {
                seller_egress_ready
            }
            None => false,
        };
        if !seller_egress_ready {
            self.cleanup_linux_exit_node_forwarding_rules()?;
            return Ok(());
        }

        let ipv4_outbound_iface = if route_families.ipv4 {
            let host_default_iface = if matches!(seller_egress, Some(PaidExitSellerEgress::Direct))
            {
                match crate::linux_default_route() {
                    Ok(route) => Some(route.dev),
                    Err(error) => {
                        let _ = self.cleanup_linux_exit_node_forwarding_rules();
                        return Err(error).context("failed to resolve default IPv4 route device");
                    }
                }
            } else {
                None
            };
            local_exit_outbound_interface(
                seller_egress,
                &self.iface,
                wireguard_exit_iface
                    .as_ref()
                    .map(|(iface, _)| iface.as_str()),
                host_default_iface.as_deref(),
            )
        } else {
            None
        };

        let ipv6_outbound_iface = None;

        let already_configured = self.exit_node_runtime.ipv4_outbound_iface == ipv4_outbound_iface
            && self.exit_node_runtime.ipv6_outbound_iface == ipv6_outbound_iface
            && self.exit_node_runtime.ipv4_tunnel_source_cidr == ipv4_tunnel_source_cidr
            && self.exit_node_runtime.ipv4_mss_clamp == Some(ipv4_mss_clamp);
        if already_configured {
            return Ok(());
        }

        self.cleanup_linux_exit_node_forwarding_rules()?;

        self.exit_node_runtime.ipv4_outbound_iface = ipv4_outbound_iface.clone();
        self.exit_node_runtime.ipv6_outbound_iface = ipv6_outbound_iface.clone();
        self.exit_node_runtime.ipv4_tunnel_source_cidr = ipv4_tunnel_source_cidr.clone();
        self.exit_node_runtime.ipv4_mss_clamp = Some(ipv4_mss_clamp);
        self.persist_network_cleanup_ownership()?;

        if route_families.ipv4 {
            match crate::read_linux_ip_forward(crate::LinuxExitNodeIpFamily::V4) {
                Ok(previous) => {
                    self.exit_node_runtime.ipv4_forward_was_enabled = Some(previous);
                    self.persist_network_cleanup_ownership()?;
                    if !previous
                        && let Err(error) =
                            crate::write_linux_ip_forward(crate::LinuxExitNodeIpFamily::V4, true)
                    {
                        let _ = self.cleanup_linux_exit_node_forwarding_rules();
                        return Err(error).context("failed to enable IPv4 forwarding");
                    }
                }
                Err(error) => {
                    let _ = self.cleanup_linux_exit_node_forwarding_rules();
                    return Err(error).context("failed to read IPv4 forwarding state");
                }
            }
        }

        if let (Some(outbound_iface), Some(tunnel_source_cidr)) = (
            ipv4_outbound_iface.as_deref(),
            ipv4_tunnel_source_cidr.as_deref(),
        ) {
            eprintln!(
                "fips: enabling IPv4 exit forwarding on {} via {} source {}",
                self.iface, outbound_iface, tunnel_source_cidr
            );
            self.cleanup_linux_legacy_exit_node_forwarding_rules()?;
            let forward_in = crate::linux_exit_node_forward_in_rule(
                &self.iface,
                outbound_iface,
                tunnel_source_cidr,
                crate::LinuxExitNodeIpFamily::V4,
            );
            let forward_out = crate::linux_exit_node_forward_out_rule(
                &self.iface,
                outbound_iface,
                crate::LinuxExitNodeIpFamily::V4,
            );
            let masquerade =
                crate::linux_exit_node_ipv4_masquerade_rule(outbound_iface, tunnel_source_cidr);
            let mss_clamp = crate::linux_exit_node_ipv4_mss_clamp_rule(
                &self.iface,
                outbound_iface,
                tunnel_source_cidr,
                ipv4_mss_clamp,
            );

            if let Err(error) = crate::linux_iptables_ensure_rule_at_front(
                crate::LinuxExitNodeIpFamily::V4,
                None,
                &forward_in,
            )
            .and_then(|()| {
                crate::linux_iptables_ensure_rule_at_front(
                    crate::LinuxExitNodeIpFamily::V4,
                    None,
                    &forward_out,
                )
            })
            .and_then(|()| {
                crate::linux_iptables_ensure_rule(
                    crate::LinuxExitNodeIpFamily::V4,
                    Some("nat"),
                    &masquerade,
                )
            })
            .and_then(|()| {
                crate::linux_iptables_ensure_rule_at_front(
                    crate::LinuxExitNodeIpFamily::V4,
                    Some("mangle"),
                    &mss_clamp,
                )
            }) {
                let _ = self.cleanup_linux_exit_node_forwarding_rules();
                return Err(error).context("failed to install IPv4 exit firewall rules");
            }
        }

        self.cleanup_linux_legacy_exit_node_forwarding_rules()?;
        Ok(())
    }

    fn apply_linux_wireguard_exit_upstream(
        &mut self,
        config: &WireGuardExitConfig,
        source_cidr: &str,
    ) -> Result<()> {
        self.cleanup_pending_linux_wireguard_exit_obligations()
            .context("retry incomplete prior WireGuard apply cleanup")?;
        let mut previous_runtime = self.exit_node_runtime.wireguard_exit.clone();
        if previous_runtime.as_ref().is_some_and(|runtime| {
            runtime.interface != config.interface.trim()
                || runtime.managed_address != config.address.trim()
                || runtime.source_cidr != source_cidr
        }) {
            let runtime = self
                .exit_node_runtime
                .wireguard_exit
                .take()
                .expect("checked WireGuard runtime");
            previous_runtime = None;
            if let Err(error) = self.cleanup_detached_linux_wireguard_exit_upstream(&runtime) {
                self.exit_node_runtime.wireguard_exit = Some(runtime);
                return Err(error);
            }
        }
        if let Some(runtime) = previous_runtime.as_mut()
            && let Some(refreshed_default) = crate::select_linux_wireguard_underlay_default_route(
                self.original_default_route.as_deref(),
                runtime.previous_default_route.as_deref(),
                None,
                config.interface.trim(),
            )
        {
            runtime.refresh_underlay_default_route(refreshed_default);
        }
        let original_default_route = self.original_default_route.clone();
        let mesh_iface = self.iface.clone();
        let apply_result = {
            let mut persist_cleanup_intent =
                |obligation: &crate::LinuxWireGuardExitCleanupObligation| {
                    retain_linux_wireguard_apply_cleanup(
                        &mut self.exit_node_runtime,
                        previous_runtime.as_ref(),
                        obligation,
                    );
                    self.persist_network_cleanup_ownership()
                };
            crate::apply_linux_wireguard_exit_upstream(
                config,
                source_cidr,
                &mesh_iface,
                previous_runtime.as_ref(),
                original_default_route.as_deref(),
                &mut persist_cleanup_intent,
            )
        };
        let runtime = match apply_result {
            Ok(runtime) => runtime,
            Err(failure) => {
                let (error, cleanup_obligation) = failure.into_parts();
                if let Some(obligation) = cleanup_obligation {
                    self.exit_node_runtime
                        .pending_wireguard_exit_cleanup
                        .clear();
                    self.exit_node_runtime
                        .pending_wireguard_exit_cleanup
                        .push(obligation);
                    self.exit_node_runtime.wireguard_exit = previous_runtime;
                    return match self.persist_network_cleanup_ownership() {
                        Ok(()) => Err(error),
                        Err(persist) => Err(anyhow!(
                            "{error:#}; failed to persist remaining WireGuard cleanup \
                             ownership: {persist:#}"
                        )),
                    };
                }
                self.exit_node_runtime
                    .pending_wireguard_exit_cleanup
                    .clear();
                if let Some(runtime) = previous_runtime.take() {
                    self.exit_node_runtime.wireguard_exit = None;
                    if let Err(cleanup) =
                        self.cleanup_detached_linux_wireguard_exit_upstream(&runtime)
                    {
                        self.exit_node_runtime.wireguard_exit = Some(runtime);
                        return Err(anyhow!(
                            "{error:#}; failed to clean previous WireGuard runtime: {cleanup:#}"
                        ));
                    }
                }
                return match self.persist_network_cleanup_ownership() {
                    Ok(()) => Err(error),
                    Err(persist) => Err(anyhow!(
                        "{error:#}; failed to persist completed WireGuard rollback: {persist:#}"
                    )),
                };
            }
        };
        self.exit_node_runtime
            .pending_wireguard_exit_cleanup
            .clear();
        self.exit_node_runtime.wireguard_exit = Some(runtime);
        // Persist the complete runtime before the inbound firewall mutation.
        // The conservative apply rollback remains on disk if this replacement
        // fails, so either journal can restore native internet after a crash.
        self.persist_network_cleanup_ownership()?;
        let inbound_guard = self
            .exit_node_runtime
            .wireguard_exit
            .as_ref()
            .ok_or_else(|| anyhow!("WireGuard runtime disappeared before inbound guard"))
            .and_then(|runtime| self.ensure_linux_wireguard_exit_inbound_guard(runtime));
        if let Err(error) = inbound_guard {
            let runtime = self
                .exit_node_runtime
                .wireguard_exit
                .take()
                .expect("WireGuard runtime exists for inbound-guard rollback");
            if let Err(cleanup) = self.cleanup_detached_linux_wireguard_exit_upstream(&runtime) {
                self.exit_node_runtime.wireguard_exit = Some(runtime);
                let persist = self.persist_network_cleanup_ownership().err();
                return Err(match persist {
                    Some(persist) => anyhow!(
                        "{error:#}; failed to roll back WireGuard after inbound-guard failure \
                         ({cleanup:#}); failed to persist remaining ownership ({persist:#})"
                    ),
                    None => anyhow!(
                        "{error:#}; failed to roll back WireGuard after inbound-guard failure: \
                         {cleanup:#}"
                    ),
                });
            }
            self.persist_network_cleanup_ownership()?;
            return Err(error);
        }
        self.persist_network_cleanup_ownership()?;
        Ok(())
    }

    fn ensure_linux_wireguard_exit_inbound_guard(
        &self,
        runtime: &crate::LinuxWireGuardExitRuntime,
    ) -> Result<()> {
        let drop_inbound = crate::linux_wireguard_exit_inbound_drop_rule(
            &runtime.interface,
            &self.iface,
            &runtime.source_cidr,
        );
        crate::linux_iptables_ensure_rule_at_front(
            crate::LinuxExitNodeIpFamily::V4,
            None,
            &drop_inbound,
        )
    }

    fn cleanup_linux_wireguard_exit_inbound_guard(
        &self,
        runtime: &crate::LinuxWireGuardExitRuntime,
    ) -> Result<()> {
        let drop_inbound = crate::linux_wireguard_exit_inbound_drop_rule(
            &runtime.interface,
            &self.iface,
            &runtime.source_cidr,
        );
        let mut last_error = None;
        for _ in 0..3 {
            match crate::linux_iptables_delete_rule(
                crate::LinuxExitNodeIpFamily::V4,
                None,
                &drop_inbound,
            ) {
                Ok(()) => return Ok(()),
                Err(error) => last_error = Some(error),
            }
        }
        Err(last_error.expect("bounded inbound-guard cleanup attempted"))
            .context("failed to remove WireGuard inbound guard rule after three attempts")
    }

    fn block_linux_wireguard_exit_if_strict(&mut self, enabled: bool) {
        if !enabled {
            return;
        }
        if let Err(error) = self.capture_linux_original_default_route(None, false) {
            eprintln!("fips: failed to capture WireGuard underlay default route: {error:#}");
        }
        if let Err(error) = self.persist_network_cleanup_ownership() {
            eprintln!(
                "fips: refusing to block the Linux default route without durable cleanup \
                 ownership: {error:#}"
            );
            return;
        }
        self.block_linux_original_default_route(false);
    }

    fn cleanup_linux_wireguard_exit_upstream(&mut self) -> Result<()> {
        let iface = self.iface.clone();
        cleanup_linux_wireguard_state(&iface, &mut self.exit_node_runtime)
    }

    fn cleanup_pending_linux_wireguard_exit_obligations(&mut self) -> Result<()> {
        let pending = std::mem::take(
            &mut self
                .exit_node_runtime
                .pending_wireguard_exit_cleanup,
        );
        let mut remaining = Vec::new();
        let mut failures = Vec::new();
        for mut obligation in pending {
            if let Err(error) =
                crate::cleanup_linux_wireguard_exit_obligation(&mut obligation)
            {
                failures.push(format!("{error:#}"));
                remaining.push(obligation);
            }
        }
        self.exit_node_runtime.pending_wireguard_exit_cleanup = remaining;
        if failures.is_empty() {
            Ok(())
        } else {
            Err(anyhow!(
                "retained WireGuard apply cleanup incomplete: {}",
                failures.join("; ")
            ))
        }
    }

    fn cleanup_detached_linux_wireguard_exit_upstream(
        &self,
        runtime: &crate::LinuxWireGuardExitRuntime,
    ) -> Result<()> {
        let guard = self.cleanup_linux_wireguard_exit_inbound_guard(runtime);
        let network = crate::cleanup_linux_wireguard_exit_upstream(runtime);
        match (guard, network) {
            (Ok(()), Ok(())) => Ok(()),
            (Err(guard), Ok(())) => Err(guard),
            (Ok(()), Err(network)) => Err(network),
            (Err(guard), Err(network)) => Err(anyhow!(
                "inbound guard cleanup failed ({guard:#}); network cleanup failed ({network:#})"
            )),
        }
    }

    fn cleanup_linux_exit_node_forwarding_rules(&mut self) -> Result<()> {
        let iface = self.iface.clone();
        cleanup_linux_forwarding_state(&iface, &mut self.exit_node_runtime)
    }

    fn cleanup_linux_legacy_exit_node_forwarding_rules(&self) -> Result<()> {
        cleanup_linux_legacy_forwarding_rules(&self.iface)
    }

    fn reconcile_linux_exit_node_forwarding_cleanup(&mut self) -> Result<()> {
        let iface = self.iface.clone();
        cleanup_linux_exit_node_state(&iface, &mut self.exit_node_runtime)
    }

    fn cleanup_linux_network_state(&mut self) -> Result<()> {
        self.linux_network_state_initialized = false;
        cleanup_linux_network_state_with_actions(self)
    }
}