netweft 0.2.0

Deterministic network planning and configuration generation for portable infrastructure.
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
use std::collections::{BTreeMap, BTreeSet};

use anyhow::{Result, bail};

use crate::model::{
    ConfigBundle, GuestKind, Ipv6Mode, RoutingMode, SCHEMA_VERSION, TailscaleStrategy,
};
use crate::plan::dns_access::derive_dns_access;
use crate::plan::docker::resolve_docker_plan;
use crate::plan::os_network::resolve_os_network_plan;
use crate::plan::proxmox_sdn::resolve_proxmox_sdn_plan;
use crate::plan::proxy::resolve_proxy_plan;
use crate::plan::ssh::resolve_ssh_plan;

#[derive(Debug, Default)]
pub struct ValidationReport {
    pub warnings: Vec<String>,
}

pub fn validate_bundle(bundle: &ConfigBundle) -> Result<ValidationReport> {
    validate_schema_versions(bundle)?;
    validate_location(bundle)?;
    validate_references(bundle)?;
    validate_addresses(bundle)?;
    validate_host_networks(bundle)?;
    validate_os_networks(bundle)?;
    validate_proxmox_sdn(bundle)?;
    validate_guest_and_mount_references(bundle)?;
    validate_storage_and_nas_references(bundle)?;
    resolve_proxy_plan(bundle)?;
    for client in bundle.ssh.clients.keys() {
        resolve_ssh_plan(bundle, client)?;
    }

    if bundle.dns.dns.recursion.enabled {
        derive_dns_access(bundle)?;
    }

    let mut report = ValidationReport::default();

    if bundle.location.ipv6.mode == Ipv6Mode::RouterAdvertised
        && bundle.location.ipv6.publish_public_aaaa
    {
        bail!(
            "location '{}' uses router-advertised IPv6 but requests durable public AAAA records",
            bundle.location.name
        );
    }

    if bundle.location.ipv6.mode == Ipv6Mode::RouterAdvertised
        && bundle.location.ipv6.prefix.is_some()
    {
        report.warnings.push(format!(
            "location '{}' has a dynamic router-advertised IPv6 prefix; Netweft will allow it for recursion but will not treat it as allocatable",
            bundle.location.name
        ));
    }

    if bundle.location.router.supports_vlans {
        let vlans = bundle
            .location
            .segments
            .values()
            .filter(|segment| segment.vlan_id.unwrap_or(0) != 0)
            .count();

        if vlans == 0 {
            report.warnings.push(format!(
                "router '{}' claims VLAN support but the location defines no VLAN segments",
                bundle.location.router.kind
            ));
        }
    }

    Ok(report)
}

fn validate_os_networks(bundle: &ConfigBundle) -> Result<()> {
    for (host_name, host) in &bundle.inventory.hosts {
        if host.os_network.is_some() {
            resolve_os_network_plan(bundle, host_name)?;
        }
    }
    Ok(())
}

fn validate_proxmox_sdn(bundle: &ConfigBundle) -> Result<()> {
    let mut zone_names = BTreeSet::new();
    for (name, zone) in &bundle.proxmox_sdn.zones {
        if !zone_names.insert(name.as_str()) {
            bail!("duplicate Proxmox SDN zone '{name}'");
        }
        if !bundle.inventory.hosts.contains_key(&zone.host) {
            bail!(
                "Proxmox SDN zone '{name}' references unknown host '{}'",
                zone.host
            );
        }
    }

    for (name, vnet) in &bundle.proxmox_sdn.vnets {
        if !bundle.proxmox_sdn.zones.contains_key(&vnet.zone) {
            bail!(
                "Proxmox SDN VNet '{name}' references unknown zone '{}'",
                vnet.zone
            );
        }
    }

    let hosts: BTreeSet<_> = bundle
        .proxmox_sdn
        .zones
        .values()
        .map(|zone| zone.host.as_str())
        .collect();

    for host in hosts {
        resolve_proxmox_sdn_plan(bundle, host)?;
    }

    Ok(())
}

fn validate_guest_and_mount_references(bundle: &ConfigBundle) -> Result<()> {
    let mut vmids = BTreeSet::new();
    let mut macs = BTreeSet::new();
    let mut addresses = BTreeSet::new();
    let mut pci = BTreeMap::<String, String>::new();

    for (name, guest) in &bundle.guests.guests {
        if !bundle.inventory.hosts.contains_key(&guest.host) {
            bail!(
                "guest '{name}' references unknown parent host '{}'",
                guest.host
            );
        }
        if !vmids.insert((guest.host.clone(), guest.vmid)) {
            bail!(
                "guest '{name}' duplicates VMID {} on host '{}'",
                guest.vmid,
                guest.host
            );
        }
        let mac = guest.mac.to_ascii_uppercase();
        if !macs.insert(mac.clone()) {
            bail!("guest '{name}' duplicates MAC '{mac}'");
        }
        if !addresses.insert(guest.ipv4) {
            bail!("guest '{name}' duplicates IPv4 {}", guest.ipv4);
        }
        if guest.kind != GuestKind::Vm
            && (!guest.pci_devices.is_empty() || !guest.virtiofs.is_empty())
        {
            bail!("guest '{name}' is not a VM but declares VM-only attachments");
        }

        let mut pci_slots = BTreeSet::new();
        for attachment in &guest.pci_devices {
            if !pci_slots.insert(attachment.slot) {
                bail!(
                    "guest '{name}' duplicates PCI attachment slot {}",
                    attachment.slot
                );
            }
            if attachment.device.trim().is_empty() {
                bail!(
                    "guest '{name}' PCI attachment slot {} has an empty device",
                    attachment.slot
                );
            }
            if let Some(owner) = pci.insert(attachment.device.clone(), name.clone()) {
                bail!(
                    "guests '{owner}' and '{name}' both claim PCI device '{}'",
                    attachment.device
                );
            }
        }

        let mut virtiofs_slots = BTreeSet::new();
        for attachment in &guest.virtiofs {
            if !virtiofs_slots.insert(attachment.slot) {
                bail!(
                    "guest '{name}' duplicates VirtioFS attachment slot {}",
                    attachment.slot
                );
            }
            if attachment.directory.trim().is_empty() {
                bail!(
                    "guest '{name}' VirtioFS attachment slot {} has an empty directory",
                    attachment.slot
                );
            }
        }
    }

    for (id, mount) in &bundle.mounts.mounts {
        if !bundle.inventory.hosts.contains_key(&mount.host) {
            bail!(
                "network mount '{id}' references unknown consumer host '{}'",
                mount.host
            );
        }
        if !bundle.inventory.hosts.contains_key(&mount.server_host) {
            bail!(
                "network mount '{id}' references unknown server host '{}'",
                mount.server_host
            );
        }
        if !mount.mount_path.starts_with('/') {
            bail!(
                "network mount '{id}' path '{}' must be absolute",
                mount.mount_path
            );
        }
    }
    Ok(())
}

fn validate_storage_and_nas_references(bundle: &ConfigBundle) -> Result<()> {
    let mut storage_ids = BTreeSet::new();

    for (id, permission) in &bundle.nas_permissions.permissions {
        if !bundle.inventory.hosts.contains_key(&permission.nas) {
            bail!(
                "NAS permission '{id}' references unknown NAS host '{}'",
                permission.nas
            );
        }
        if !bundle.inventory.hosts.contains_key(&permission.client_host) {
            bail!(
                "NAS permission '{id}' references unknown client host '{}'",
                permission.client_host
            );
        }
        if permission.share.trim().is_empty() {
            bail!("NAS permission '{id}' has an empty share name");
        }
        if permission.security.trim().is_empty() {
            bail!("NAS permission '{id}' has empty security mode");
        }
    }

    for (id, storage) in &bundle.proxmox_storages.storages {
        if !bundle.inventory.hosts.contains_key(&storage.host) {
            bail!(
                "Proxmox storage '{id}' references unknown host '{}'",
                storage.host
            );
        }
        if !bundle.inventory.hosts.contains_key(&storage.server_host) {
            bail!(
                "Proxmox storage '{id}' references unknown server host '{}'",
                storage.server_host
            );
        }
        if !storage.mount_path.starts_with('/') {
            bail!(
                "Proxmox storage '{id}' path '{}' must be absolute",
                storage.mount_path
            );
        }
        if storage.export.trim().is_empty() || !storage.export.starts_with('/') {
            bail!(
                "Proxmox storage '{id}' export '{}' must be an absolute export path",
                storage.export
            );
        }
        if storage.content.is_empty() {
            bail!("Proxmox storage '{id}' must declare at least one content type");
        }
        if !storage_ids.insert((storage.host.clone(), storage.storage_id.clone())) {
            bail!(
                "Proxmox storage '{id}' duplicates storage ID '{}' on host '{}'",
                storage.storage_id,
                storage.host
            );
        }
    }
    Ok(())
}

fn validate_schema_versions(bundle: &ConfigBundle) -> Result<()> {
    let versions = [
        ("netweft.toml", bundle.settings.schema_version),
        ("inventory.toml", bundle.inventory.schema_version),
        ("networks.toml", bundle.networks.schema_version),
        ("ssh.toml", bundle.ssh.schema_version),
        ("services.toml", bundle.services.schema_version),
        ("guests.toml", bundle.guests.schema_version),
        ("mounts.toml", bundle.mounts.schema_version),
        (
            "nas-permissions.toml",
            bundle.nas_permissions.schema_version,
        ),
        (
            "proxmox-storages.toml",
            bundle.proxmox_storages.schema_version,
        ),
        ("dns.toml", bundle.dns.schema_version),
        ("allocations.toml", bundle.allocations.schema_version),
        ("location", bundle.location.schema_version),
    ];

    for (name, version) in versions {
        if version != SCHEMA_VERSION {
            bail!(
                "{name} uses schema version {version}, but this Netweft build supports version {SCHEMA_VERSION}"
            );
        }
    }

    Ok(())
}

fn validate_location(bundle: &ConfigBundle) -> Result<()> {
    if bundle.location.name != bundle.settings.active_location {
        // This is valid when the CLI overrides the active location, so it is
        // intentionally not rejected here.
    }

    match bundle.location.ipv6.mode {
        Ipv6Mode::Disabled => {
            if bundle.location.ipv6.prefix.is_some() {
                bail!("IPv6 mode is disabled but an IPv6 prefix is configured");
            }
        }
        Ipv6Mode::RouterAdvertised => {
            let prefix =
                bundle.location.ipv6.prefix.ok_or_else(|| {
                    anyhow::anyhow!("router-advertised IPv6 mode requires prefix")
                })?;

            if prefix.prefix_len() != 64 {
                bail!("router-advertised IPv6 prefix must be /64, got {prefix}");
            }
        }
        Ipv6Mode::Delegated => {
            let prefix = bundle
                .location
                .ipv6
                .prefix
                .ok_or_else(|| anyhow::anyhow!("delegated IPv6 mode requires prefix"))?;
            let subnet_length = bundle.location.ipv6.subnet_prefix_length.unwrap_or(64);

            if subnet_length != 64 {
                bail!("Netweft v0.1 only allocates /64 networks from delegated IPv6 prefixes");
            }

            if prefix.prefix_len() > subnet_length {
                bail!(
                    "delegated prefix {} is longer than the requested /{} subnet size",
                    prefix,
                    subnet_length
                );
            }
        }
    }

    Ok(())
}

fn validate_references(bundle: &ConfigBundle) -> Result<()> {
    for (name, host) in &bundle.inventory.hosts {
        if let Some(parent) = &host.parent
            && !bundle.inventory.hosts.contains_key(parent)
        {
            bail!("host '{name}' references unknown parent host '{parent}'");
        }
    }

    for (name, network) in &bundle.networks.networks {
        if let Some(owner) = &network.owner
            && !bundle.inventory.hosts.contains_key(owner)
        {
            bail!("network '{name}' references unknown owner host '{owner}'");
        }
    }

    for (name, service) in &bundle.services.services {
        if !bundle.inventory.hosts.contains_key(&service.host) {
            bail!(
                "service '{name}' references unknown host '{}'",
                service.host
            );
        }
        if !bundle.networks.networks.contains_key(&service.network) {
            bail!(
                "service '{name}' references unknown network '{}'",
                service.network
            );
        }

        if let Some(web) = &service.web
            && !bundle.services.services.contains_key(&web.proxy)
        {
            bail!(
                "service '{name}' references unknown proxy service '{}'",
                web.proxy
            );
        }
    }

    for (name, location_host) in &bundle.location.hosts {
        if !bundle.inventory.hosts.contains_key(name) {
            bail!("location references unknown host '{name}'");
        }

        for (interface_name, interface) in &location_host.interfaces {
            if !bundle.location.segments.contains_key(&interface.segment) {
                bail!(
                    "host '{name}' interface '{interface_name}' references unknown location segment '{}'",
                    interface.segment
                );
            }
        }
    }

    validate_network_routes(bundle)?;
    validate_docker_profiles(bundle)?;
    validate_tailscale(bundle)?;

    if !bundle
        .services
        .services
        .contains_key(&bundle.dns.dns.service)
    {
        bail!(
            "DNS configuration references unknown service '{}'",
            bundle.dns.dns.service
        );
    }

    for record in &bundle.dns.records {
        match record.kind {
            crate::model::DnsRecordKind::Host => {
                let target = record.target.as_ref().ok_or_else(|| {
                    anyhow::anyhow!("DNS host record '{}' requires target", record.name)
                })?;
                if !bundle.inventory.hosts.contains_key(target) {
                    bail!(
                        "DNS record '{}' references unknown host '{target}'",
                        record.name
                    );
                }
                if record.interface.is_none() {
                    bail!("DNS host record '{}' requires interface", record.name);
                }
            }
            crate::model::DnsRecordKind::Service | crate::model::DnsRecordKind::Proxy => {
                let target = record.target.as_ref().ok_or_else(|| {
                    anyhow::anyhow!("DNS service record '{}' requires target", record.name)
                })?;
                if !bundle.services.services.contains_key(target) {
                    bail!(
                        "DNS record '{}' references unknown service '{target}'",
                        record.name
                    );
                }
            }
            crate::model::DnsRecordKind::Cname => {
                if record.target.is_none() {
                    bail!("DNS CNAME record '{}' requires target", record.name);
                }
            }
            crate::model::DnsRecordKind::SegmentGateway => {
                let target = record.target.as_ref().ok_or_else(|| {
                    anyhow::anyhow!(
                        "DNS gateway record '{}' requires segment target",
                        record.name
                    )
                })?;
                if !bundle.location.segments.contains_key(target) {
                    bail!(
                        "DNS record '{}' references unknown segment '{target}'",
                        record.name
                    );
                }
            }
        }
    }

    for allocation_key in bundle
        .allocations
        .ula
        .as_ref()
        .into_iter()
        .flat_map(|ula| ula.networks.keys())
    {
        if !bundle
            .networks
            .networks
            .values()
            .any(|network| &network.allocation_key == allocation_key)
        {
            bail!("ULA allocation '{allocation_key}' does not match any network allocation_key");
        }
    }

    Ok(())
}

fn validate_docker_profiles(bundle: &ConfigBundle) -> Result<()> {
    for host in bundle.docker.hosts.keys() {
        resolve_docker_plan(bundle, host)?;
    }
    Ok(())
}

fn validate_network_routes(bundle: &ConfigBundle) -> Result<()> {
    for (name, network) in &bundle.networks.networks {
        let Some(routing) = &network.routing else {
            continue;
        };

        match routing.mode {
            RoutingMode::Direct => {
                if routing.via.is_some() {
                    bail!("network '{name}' uses direct routing but also defines 'via'");
                }
            }
            RoutingMode::ViaHost => {
                let via = routing.via.as_ref().ok_or_else(|| {
                    anyhow::anyhow!(
                        "network '{name}' uses via-host routing but does not define 'via'"
                    )
                })?;
                if !bundle.inventory.hosts.contains_key(via) {
                    bail!("network '{name}' references unknown gateway host '{via}'");
                }
            }
            RoutingMode::HostPrivate => {
                if routing.from.is_some() || routing.via.is_some() {
                    bail!("host-private network '{name}' must not define 'from' or 'via'");
                }
            }
        }

        if let Some(from) = &routing.from
            && !bundle.inventory.hosts.contains_key(from)
        {
            bail!("network '{name}' references unknown source host '{from}'");
        }
    }
    Ok(())
}

fn validate_tailscale(bundle: &ConfigBundle) -> Result<()> {
    let tailscale = &bundle.location.tailscale;

    if !tailscale.enabled {
        if tailscale.primary_router.is_some() || !tailscale.routers.is_empty() {
            bail!("Tailscale is disabled but router settings are present");
        }
        return Ok(());
    }

    let strategy = tailscale
        .strategy
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Tailscale is enabled but no strategy is configured"))?;

    match strategy {
        TailscaleStrategy::SubnetRouter => {
            let primary = tailscale
                .primary_router
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("subnet-router strategy requires primary_router"))?;
            if !tailscale.routers.contains_key(primary) {
                bail!("primary Tailscale router '{primary}' has no router configuration");
            }
        }
        TailscaleStrategy::HaSubnetRouter => {
            if tailscale.routers.len() < 2 {
                bail!("ha-subnet-router strategy requires at least two routers");
            }
        }
        TailscaleStrategy::DirectNodes => {}
    }

    for (router_name, router) in &tailscale.routers {
        if !bundle.inventory.hosts.contains_key(router_name) {
            bail!("Tailscale router '{router_name}' is not a known host");
        }

        for route_ref in &router.advertise {
            let Some((kind, name)) = route_ref.split_once(':') else {
                bail!("invalid advertise reference '{route_ref}'");
            };

            match kind {
                "segment" => {
                    if !bundle.location.segments.contains_key(name) {
                        bail!(
                            "Tailscale router '{router_name}' advertises unknown segment '{name}'"
                        );
                    }
                }
                "network" => {
                    let network = bundle.networks.networks.get(name).ok_or_else(|| {
                        anyhow::anyhow!(
                            "Tailscale router '{router_name}' advertises unknown network '{name}'"
                        )
                    })?;
                    let routing = network.routing.as_ref().ok_or_else(|| {
                        anyhow::anyhow!("advertised network '{name}' has no routing policy")
                    })?;

                    if routing.mode == RoutingMode::HostPrivate {
                        bail!("cannot advertise host-private network '{name}'");
                    }
                    if routing.from.as_deref() != Some(router_name) {
                        bail!("network '{name}' is not routed from '{router_name}'");
                    }
                }
                _ => bail!("unsupported advertise reference '{route_ref}'"),
            }
        }
    }

    Ok(())
}

fn validate_addresses(bundle: &ConfigBundle) -> Result<()> {
    for (name, segment) in &bundle.location.segments {
        if !segment.ipv4_cidr.contains(&segment.ipv4_gateway) {
            bail!(
                "segment '{name}' gateway {} is outside {}",
                segment.ipv4_gateway,
                segment.ipv4_cidr
            );
        }
    }

    for (host_name, host) in &bundle.location.hosts {
        for (interface_name, interface) in &host.interfaces {
            if let Some(ipv4) = interface.ipv4 {
                let segment = &bundle.location.segments[&interface.segment];
                if !segment.ipv4_cidr.contains(&ipv4) {
                    bail!(
                        "host '{host_name}' interface '{interface_name}' address {ipv4} is outside {}",
                        segment.ipv4_cidr
                    );
                }
            }
        }
    }

    for (name, network) in &bundle.networks.networks {
        if let (Some(cidr), Some(gateway)) = (network.ipv4_cidr, network.ipv4_gateway)
            && !cidr.contains(&gateway)
        {
            bail!("network '{name}' gateway {gateway} is outside {cidr}");
        }
    }

    let mut service_ipv4 = BTreeSet::new();
    let mut host_ports = BTreeSet::new();

    for (name, service) in &bundle.services.services {
        if let Some(address) = &service.address
            && let Some(ipv4) = address.ipv4
        {
            let network = &bundle.networks.networks[&service.network];
            let cidr = network.ipv4_cidr.ok_or_else(|| {
                anyhow::anyhow!(
                    "service '{name}' has IPv4 address {ipv4}, but network '{}' has no IPv4 CIDR",
                    service.network
                )
            })?;

            if !cidr.contains(&ipv4) {
                bail!(
                    "service '{name}' address {ipv4} is outside network '{}' ({cidr})",
                    service.network
                );
            }

            if !service_ipv4.insert(ipv4) {
                bail!("duplicate service IPv4 address detected: {ipv4}");
            }
        }

        for port in &service.ports {
            let key = (&service.host, port.host, format!("{:?}", port.protocol));
            if !host_ports.insert(key) {
                bail!(
                    "duplicate host port {}/{:?} on host '{}'",
                    port.host,
                    port.protocol,
                    service.host
                );
            }
        }

        if let Some(ssh) = &service.ssh {
            let key = (&service.host, ssh.host_port, "Tcp".to_owned());
            if !host_ports.insert(key) {
                bail!(
                    "duplicate host port {}/tcp on host '{}'",
                    ssh.host_port,
                    service.host
                );
            }
        }
    }

    Ok(())
}

fn validate_host_networks(bundle: &ConfigBundle) -> Result<()> {
    let mut static_ipv4_owners = BTreeMap::new();

    for (host_name, host) in &bundle.inventory.hosts {
        let Some(network) = &host.network else {
            continue;
        };

        if !host.enabled {
            continue;
        }

        let location_host = bundle.location.hosts.get(host_name).ok_or_else(|| {
            anyhow::anyhow!(
                "host '{host_name}' defines a network profile but is not attached to location '{}'",
                bundle.location.name
            )
        })?;

        let mut link_names = BTreeSet::new();

        for link in &network.links {
            if link.name.trim().is_empty() {
                bail!("host '{host_name}' defines an empty network link name");
            }

            if !link_names.insert(link.name.as_str()) {
                bail!(
                    "host '{host_name}' defines duplicate network link '{}'",
                    link.name
                );
            }
        }

        let mut bridge_names = BTreeSet::new();
        let mut claimed_links: BTreeMap<&str, &str> = BTreeMap::new();
        let mut management_bridge_count = 0usize;

        for bridge in &network.bridges {
            if bridge.name.trim().is_empty() {
                bail!("host '{host_name}' defines an empty bridge name");
            }

            if !bridge_names.insert(bridge.name.as_str()) {
                bail!(
                    "host '{host_name}' defines duplicate bridge '{}'",
                    bridge.name
                );
            }

            if bridge.allowed_vlans.is_some() && !bridge.vlan_aware {
                bail!(
                    "host '{host_name}' bridge '{}' defines allowed_vlans but is not VLAN-aware",
                    bridge.name
                );
            }

            if let Some(vlans) = &bridge.allowed_vlans {
                validate_vlan_expression(host_name, &bridge.name, vlans)?;
            }

            for port in &bridge.ports {
                if !link_names.contains(port.as_str()) {
                    bail!(
                        "host '{host_name}' bridge '{}' references unknown link '{port}'",
                        bridge.name
                    );
                }

                if let Some(existing_bridge) =
                    claimed_links.insert(port.as_str(), bridge.name.as_str())
                {
                    bail!(
                        "host '{host_name}' link '{port}' is assigned to both bridge '{existing_bridge}' and bridge '{}'",
                        bridge.name
                    );
                }
            }

            let Some(interface_name) = &bridge.location_interface else {
                continue;
            };

            if interface_name == &network.management_interface {
                management_bridge_count += 1;
            }

            let interface = location_host
                .interfaces
                .get(interface_name)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "host '{host_name}' bridge '{}' references missing location interface '{interface_name}'",
                        bridge.name
                    )
                })?;

            let segment = bundle
                .location
                .segments
                .get(&interface.segment)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "host '{host_name}' interface '{interface_name}' references unknown segment '{}'",
                        interface.segment
                    )
                })?;

            if let Some(ipv4) = interface.ipv4 {
                if ipv4 == segment.ipv4_gateway {
                    bail!(
                        "host '{host_name}' interface '{interface_name}' address {ipv4} conflicts with segment '{}' gateway",
                        interface.segment
                    );
                }

                if let Some((other_host, other_interface)) =
                    static_ipv4_owners.insert(ipv4, (host_name.as_str(), interface_name.as_str()))
                {
                    bail!(
                        "duplicate host IPv4 address {ipv4}: '{other_host}.{other_interface}' and '{host_name}.{interface_name}'"
                    );
                }
            }

            if matches!(
                interface.ipv6_mode,
                Some(crate::model::InterfaceIpv6Mode::Slaac)
            ) && bundle.location.ipv6.mode == Ipv6Mode::Disabled
            {
                bail!(
                    "host '{host_name}' interface '{interface_name}' requests SLAAC, but location '{}' disables IPv6",
                    bundle.location.name
                );
            }
        }

        if management_bridge_count == 0 {
            bail!(
                "host '{host_name}' has no bridge attached to management interface '{}'",
                network.management_interface
            );
        }

        if management_bridge_count > 1 {
            bail!(
                "host '{host_name}' has multiple bridges attached to management interface '{}'",
                network.management_interface
            );
        }

        if !location_host
            .interfaces
            .contains_key(&network.management_interface)
        {
            bail!(
                "host '{host_name}' management interface '{}' is not defined at location '{}'",
                network.management_interface,
                bundle.location.name
            );
        }
    }

    Ok(())
}

fn validate_vlan_expression(host_name: &str, bridge_name: &str, expression: &str) -> Result<()> {
    if expression.trim().is_empty() {
        bail!("host '{host_name}' bridge '{bridge_name}' has an empty allowed_vlans value");
    }

    for item in expression.split(',') {
        let item = item.trim();

        if item.is_empty() {
            bail!(
                "host '{host_name}' bridge '{bridge_name}' has an invalid VLAN expression '{expression}'"
            );
        }

        if let Some((start, end)) = item.split_once('-') {
            let start: u16 = start.parse().map_err(|_| {
                anyhow::anyhow!(
                    "host '{host_name}' bridge '{bridge_name}' has invalid VLAN value '{item}'"
                )
            })?;

            let end: u16 = end.parse().map_err(|_| {
                anyhow::anyhow!(
                    "host '{host_name}' bridge '{bridge_name}' has invalid VLAN value '{item}'"
                )
            })?;

            validate_vlan_id(host_name, bridge_name, start)?;
            validate_vlan_id(host_name, bridge_name, end)?;

            if start > end {
                bail!(
                    "host '{host_name}' bridge '{bridge_name}' has descending VLAN range '{item}'"
                );
            }
        } else {
            let vlan: u16 = item.parse().map_err(|_| {
                anyhow::anyhow!(
                    "host '{host_name}' bridge '{bridge_name}' has invalid VLAN value '{item}'"
                )
            })?;

            validate_vlan_id(host_name, bridge_name, vlan)?;
        }
    }

    Ok(())
}

fn validate_vlan_id(host_name: &str, bridge_name: &str, vlan: u16) -> Result<()> {
    if !(1..=4094).contains(&vlan) {
        bail!("host '{host_name}' bridge '{bridge_name}' VLAN {vlan} is outside 1-4094");
    }

    Ok(())
}