tailscale-systray 0.3.0

Application Indicator (SNI) for Tailscale
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
use anyhow::Context;
use futures_util::TryStreamExt;
use log::{debug, error, info, trace, warn};
use netlink_sys::AsyncSocketExt;
use netlink_sys::{protocols::NETLINK_NETFILTER, AsyncSocket, SocketAddr, TokioSocket};
use nftnl::expr::Expression;
use nftnl::nftnl_sys::libc;
use nftnl::{nft_expr, Chain, ProtoFamily, Rule, Table};
use nftnl::{nftnl_sys as sys, Batch};
use nix::mount::{mount, MsFlags};
use nix::sched::{clone, setns, CloneFlags};
use nix::sys::signal::Signal;
use nix::sys::stat::Mode;
use nix::unistd::mkdir;
use rtnetlink::{new_connection, Handle, LinkUnspec, LinkVeth, RouteMessageBuilder};
use static_assertions::const_assert_eq;
use std::ffi::{CStr, CString};
use std::fs::File;
use std::future::Future;
use std::io::{IoSlice, Write};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
use std::path::Path;

const VETH_NAME_HOST: &str = "veth-ts-host";
const VETH_NAME_NS: &str = "veth-ts-ns";

// mount_attr structure for mount_setattr syscall
#[repr(C)]
#[derive(Debug, Default)]
struct MountAttr {
    attr_set: u64,
    attr_clr: u64,
    propagation: u64,
    userns_fd: u64,
}

async fn create_veth_pair(handle: &Handle, ts_ns: &Namespace) -> anyhow::Result<()> {
    info!(
        "Creating veth pair: {} and {}",
        VETH_NAME_HOST, VETH_NAME_NS
    );

    // create interfaces
    let mut retried = false;
    loop {
        let res = handle
            .link()
            .add(LinkVeth::new(VETH_NAME_HOST, VETH_NAME_NS).build())
            .execute()
            .await;

        if let err @ Err(rtnetlink::Error::NetlinkError(rtnetlink::packet_core::ErrorMessage {
            code: Some(errno),
            ..
        })) = res
        {
            if errno.get() == -libc::EEXIST && !retried {
                info!("Veth pair already exists, deleting and recreating");

                let idx = ifname_to_index(handle, VETH_NAME_HOST)
                    .await
                    .context(format!("Finding existing interface {}", VETH_NAME_HOST))?;

                info!("Deleting existing interface: {}", VETH_NAME_HOST);
                handle
                    .link()
                    .del(idx)
                    .execute()
                    .await
                    .context(format!("Deleting existing interface {}", VETH_NAME_HOST))?;
                retried = true;
                continue; // try again
            } else {
                err.context(format!(
                    "Creating veth pair {} and {}",
                    VETH_NAME_HOST, VETH_NAME_NS
                ))?;
            }
        } else {
            debug!("Veth pair created successfully");
        }
        break;
    }

    // get their indices
    debug!("Retrieving interface indices");
    let veth_host_idx = ifname_to_index(handle, VETH_NAME_HOST).await?;
    let veth_ns_idx = ifname_to_index(handle, VETH_NAME_NS).await?;
    debug!(
        "Retrieved indices: host={}, ns={}",
        veth_host_idx, veth_ns_idx
    );

    // configure the host side
    info!("Configuring host side interface ({})", VETH_NAME_HOST);
    bring_up_interface(handle, veth_host_idx).await?;
    debug!("Adding IP address 172.31.0.1/30 to {}", VETH_NAME_HOST);
    handle
        .address()
        .add(veth_host_idx, "172.31.0.1".parse()?, 30)
        .execute()
        .await
        .context(format!("Adding address to {}", VETH_NAME_HOST))?;
    debug!("Adding IPv6 address fc00::1/64 to {}", VETH_NAME_HOST);
    handle
        .address()
        .add(veth_host_idx, "fc00::1".parse()?, 64)
        .execute()
        .await
        .context(format!("Adding IPv6 address to {}", VETH_NAME_HOST))?;

    info!("Moving interface {} to namespace", VETH_NAME_NS);
    handle
        .link()
        .set(
            LinkUnspec::new_with_index(veth_ns_idx)
                .setns_by_fd(ts_ns.0.as_raw_fd())
                .build(),
        )
        .execute()
        .await
        .with_context(|| format!("Failed to move interface {} to namespace", VETH_NAME_NS))?;
    info!("Successfully moved interface to namespace");

    Ok(())
}

async fn configure_veth_ns(handle: &Handle, veth_ns_idx: u32) -> anyhow::Result<()> {
    // configure the ns side
    info!("Configuring namespace side interface ({})", VETH_NAME_NS);
    bring_up_interface(handle, veth_ns_idx).await?;
    debug!("Adding IP address 172.31.0.2/30 to {}", VETH_NAME_NS);
    handle
        .address()
        .add(veth_ns_idx, "172.31.0.2".parse()?, 30)
        .execute()
        .await
        .context(format!("Adding address to {}", VETH_NAME_NS))?;
    debug!("Adding IPv6 address fc00::2/64 to {}", VETH_NAME_NS);
    handle
        .address()
        .add(veth_ns_idx, "fc00::2".parse()?, 64)
        .execute()
        .await
        .context(format!("Adding IPv6 address to {}", VETH_NAME_NS))?;
    Ok(())
}

async fn add_default_route(handle: &Handle) -> anyhow::Result<()> {
    info!("Adding IPv4 default route via 172.31.0.1");
    handle
        .route()
        .add(
            RouteMessageBuilder::<Ipv4Addr>::new()
                .gateway("172.31.0.1".parse()?)
                .build(),
        )
        .execute()
        .await
        .context("Error adding IPv4 default route")?;
    debug!("IPv4 default route added successfully");

    info!("Adding IPv6 default route via fc00::1");
    handle
        .route()
        .add(
            RouteMessageBuilder::<Ipv6Addr>::new()
                .gateway("fc00::1".parse()?)
                .build(),
        )
        .execute()
        .await
        .context("Error adding IPv6 default route")?;
    debug!("IPv6 default route added successfully");

    Ok(())
}

struct Namespace(OwnedFd);
impl Namespace {
    fn current_net() -> anyhow::Result<Self> {
        trace!("Getting current network namespace");
        let file = File::open("/proc/self/ns/net").context("Failed to open /proc/self/ns/net")?;
        debug!("Successfully retrieved current network namespace");
        Ok(Self(file.into()))
    }
}
impl From<File> for Namespace {
    fn from(fd: File) -> Self {
        Self(fd.into())
    }
}

struct BoundNamespaces {
    net: Namespace,
    #[allow(dead_code)]
    mnt: Namespace,
}
const RUN_DIR: &str = "/var/run/tailscale-net";
const NS_DIR: &str = "/var/run/tailscale-net/ns";
pub const NET_NS_PATH: &str = "/var/run/tailscale-net/ns/net";
pub const MNT_NS_PATH: &str = "/var/run/tailscale-net/ns/mnt";

fn create_namespace() -> anyhow::Result<BoundNamespaces> {
    // create a directory for the namespace files
    let rundir_path = Path::new(RUN_DIR);
    if !rundir_path.exists() {
        debug!("Creating tailscale network runtime directory: {}", RUN_DIR);
        mkdir(rundir_path, Mode::from_bits(0o755).unwrap())
            .with_context(|| format!("Failed to create netns directory: {:?}", rundir_path))?;
    } else {
        debug!("Directory already exists: {}", RUN_DIR);
    }

    // create /var/run/tailscale-net/ns and mount a private tmpfs there
    let nsdir_path = Path::new(NS_DIR);
    if !nsdir_path.exists() {
        debug!("Creating namespace directory: {}", NS_DIR);
        mkdir(nsdir_path, Mode::from_bits(0o755).unwrap())
            .with_context(|| format!("Failed to create namespace directory: {:?}", nsdir_path))?;
    } else {
        debug!("Namespace directory already exists: {}", NS_DIR);
    }
    mount(
        None::<&str>,
        NS_DIR,
        Some("tmpfs"),
        MsFlags::MS_NOEXEC | MsFlags::MS_NOSUID,
        Some("mode=0755"),
    )
    .map_err(|e| anyhow::anyhow!("Failed to mount tmpfs at {}: {}", NS_DIR, e))?;

    mount(
        None::<&str>,
        NS_DIR,
        None::<&str>,
        MsFlags::MS_PRIVATE,
        None::<&str>,
    )
    .map_err(|e| anyhow::anyhow!("Failed to make mount at {} private: {}", NS_DIR, e))?;

    // create the namespace files
    debug!("Creating network namespace file: {}", NET_NS_PATH);
    File::create(NET_NS_PATH)
        .with_context(|| format!("Failed to create network namespace file: {}", NET_NS_PATH))?;
    debug!("Creating mount namespace file: {}", MNT_NS_PATH);
    File::create(MNT_NS_PATH)
        .with_context(|| format!("Failed to create mount namespace file: {}", MNT_NS_PATH))?;

    // to signal the child when it can exit
    let (pipe_reader, pipe_writer) =
        nix::unistd::pipe().context("Failed to create pipe for parent-child communication")?;

    // allocate stack for the child process
    const STACK_SIZE: usize = 1024 * 1024; // 1 MB
    let mut stack: Vec<u8> = vec![0; STACK_SIZE];

    debug!("Cloning process to create new namespaces");
    let child_pid = unsafe {
        clone(
            Box::new(move || child_create_ts_ns(RUN_DIR, pipe_reader.as_raw_fd())),
            &mut stack,
            CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_NEWNET,
            Some(Signal::SIGCHLD as i32),
        )
    }
    .context("Failed to clone")?;
    debug!("Parent: Child process created with PID: {}", child_pid);

    // Parent process will now perform the bind mounts
    debug!("Parent: Binding namespaces from child PID {}", child_pid);

    // Path to child's namespace files
    let child_net_ns = format!("/proc/{}/ns/net", child_pid);
    let child_mnt_ns = format!("/proc/{}/ns/mnt", child_pid);

    // Bind mount network namespace
    debug!(
        "Parent: Binding child's network namespace to {}",
        NET_NS_PATH
    );
    mount(
        Some(child_net_ns.as_str()),
        NET_NS_PATH,
        None::<&str>,
        MsFlags::MS_BIND,
        None::<&str>,
    )
    .with_context(|| {
        format!(
            "Failed to bind mount child's network namespace at {} to {}",
            child_net_ns, NET_NS_PATH
        )
    })?;

    // Bind mount mount namespace
    debug!("Parent: Binding child's mount namespace to {}", MNT_NS_PATH);
    mount(
        Some(child_mnt_ns.as_str()),
        MNT_NS_PATH,
        None::<&str>,
        MsFlags::MS_BIND,
        None::<&str>,
    )
    .with_context(|| {
        format!(
            "Failed to bind mount child's mount namespace at {} to {}",
            child_mnt_ns, MNT_NS_PATH
        )
    })?;

    // Signal the child to continue by writing a byte to the pipe
    debug!("Parent: Signaling child to continue");
    nix::unistd::write(&pipe_writer, &[1]).context("Failed to signal child process")?;

    drop(pipe_writer);

    // Wait for child to finish
    debug!("Parent: Waiting for child process to complete");
    let status = nix::sys::wait::waitpid(child_pid, None).context("Failed to wait for child")?;
    debug!("Parent: Child process completed with status: {:?}", status);

    match status {
        nix::sys::wait::WaitStatus::Exited(_, code) => {
            if code != 0 {
                error!("Child exited with non-zero code {}", code);
                anyhow::bail!("Child exited with code {}", code);
            }
            debug!("Child completed successfully with exit code 0");
        }
        _ => {
            error!("Child exited with unexpected status: {:?}", status);
            anyhow::bail!("Child exited with status {:?}", status);
        }
    }

    info!("Opening namespace file descriptors");
    debug!("Opening network namespace at {}", NET_NS_PATH);
    let net_namespace = File::open(NET_NS_PATH)
        .with_context(|| format!("Failed to open network namespace file: {}", NET_NS_PATH))?;

    debug!("Opening mount namespace at {}", MNT_NS_PATH);
    let mnt_namespace = File::open(MNT_NS_PATH)
        .with_context(|| format!("Failed to open mount namespace file: {}", MNT_NS_PATH))?;

    info!("Successfully created and bound to new namespaces");
    Ok(BoundNamespaces {
        net: net_namespace.into(),
        mnt: mnt_namespace.into(),
    })
}

fn child_create_ts_ns(ns_path_prefix: &str, sync_pipe: i32) -> isize {
    debug!("Child: Process started to create tailscale namespaces");

    if let Err(e) = setup_child_namespace(ns_path_prefix, sync_pipe) {
        error!("Failed to setup namespace: {:#}", e);
        eprintln!("Failed to setup namespace: {:#}", e);
        return 1;
    }

    debug!("Child process completed successfully");
    0
}

fn setup_child_namespace(run_dir: &str, sync_pipe: i32) -> anyhow::Result<()> {
    // do not propagate mounts to the parent
    debug!("Child: Setting mount propagation to MS_PRIVATE");
    mount(
        None::<&str>,
        "/",
        None::<&str>,
        MsFlags::MS_REC | MsFlags::MS_SLAVE,
        None::<&str>,
    )
    .context("Failed to change mount propagation")?;

    // Wait for parent to signal that it's done binding our namespaces
    debug!("Child: Waiting for parent to bind namespaces...");
    let mut buf = [0u8; 1];
    let sync_pipe_fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(sync_pipe) };
    let bytes_read =
        nix::unistd::read(sync_pipe_fd, &mut buf).context("Failed to read from sync pipe")?;

    if bytes_read != 1 || buf[0] != 1 {
        return Err(anyhow::anyhow!("Unexpected data from parent sync pipe"));
    }

    debug!("Child: Parent has bound namespaces, continuing setup");
    nix::unistd::close(sync_pipe).context("Failed to close sync pipe")?;

    // create a resolv.conf
    let tmp_resolv = format!("{}/{}", run_dir, "resolv.conf");
    debug!("Child: Creating custom resolv.conf at {}", tmp_resolv);
    let mut file = File::create(&tmp_resolv)
        .with_context(|| format!("Failed to create resolv.conf at {}", tmp_resolv))?;

    debug!("Child: Writing nameserver entries to resolv.conf");
    // we could consider copying the host's resolv.conf entries here instead
    writeln!(file, "nameserver 8.8.8.8\nnameserver 1.1.1.1")
        .context("Failed to write nameserver entries to resolv.conf")?;

    // bind mount the resolv.conf using new mount API to avoid following symlinks
    // The traditional mount() syscall would follow the symlink, but we want to
    // mount over the symlink itself so changes don't propagate to the parent namespace
    debug!("Child: Mounting custom resolv.conf to /etc/resolv.conf using new mount API");

    let source_path = CString::new(tmp_resolv.as_bytes()).context("Invalid source path")?;

    // Step 1: open_tree() with AT_SYMLINK_NOFOLLOW to get fd for source
    let mount_fd = unsafe {
        let fd = libc::syscall(
            libc::SYS_open_tree,
            libc::AT_FDCWD,
            source_path.as_ptr(),
            libc::AT_NO_AUTOMOUNT | libc::AT_SYMLINK_NOFOLLOW | libc::OPEN_TREE_CLONE as i32,
        );
        if fd < 0 {
            anyhow::bail!("open_tree failed: {}", std::io::Error::last_os_error());
        }
        OwnedFd::from_raw_fd(fd as i32)
    };

    // Step 2: mount_setattr() to set MS_PRIVATE propagation
    let attr = MountAttr {
        propagation: libc::MS_PRIVATE,
        ..Default::default()
    };

    let result = unsafe {
        libc::syscall(
            libc::SYS_mount_setattr,
            mount_fd.as_raw_fd(),
            c"".as_ptr(),
            libc::AT_EMPTY_PATH | libc::AT_SYMLINK_NOFOLLOW,
            &attr as *const MountAttr,
            std::mem::size_of::<MountAttr>(),
        )
    };

    if result < 0 {
        anyhow::bail!("mount_setattr failed: {}", std::io::Error::last_os_error());
    }

    // Step 3: move_mount() to attach to /etc/resolv.conf without following symlink
    let result = unsafe {
        libc::syscall(
            libc::SYS_move_mount,
            mount_fd.as_raw_fd(),
            c"".as_ptr(),
            libc::AT_FDCWD,
            c"/etc/resolv.conf".as_ptr(),
            #[cfg(target_env = "gnu")]
            libc::MOVE_MOUNT_F_EMPTY_PATH,
            #[cfg(not(target_env = "gnu"))]
            0x00000004,
        )
    };

    if result < 0 {
        anyhow::bail!("move_mount failed: {}", std::io::Error::last_os_error());
    }

    debug!("Child: Successfully mounted resolv.conf without following symlink");

    Ok(())
}

async fn ifname_to_index(handle: &Handle, if_name: &str) -> anyhow::Result<u32> {
    trace!("Looking up index for interface: {}", if_name);
    let mut links = handle.link().get().match_name(if_name.to_owned()).execute();
    let link = match links.try_next().await {
        Ok(Some(link)) => {
            debug!("Found interface {}: index={}", if_name, link.header.index);
            link
        }
        Ok(None) => {
            warn!("Interface {} not found", if_name);
            anyhow::bail!("Interface {} not found", if_name)
        }
        Err(e) => {
            error!("Error finding interface {}: {}", if_name, e);
            anyhow::bail!("Error finding interface {}: {}", if_name, e)
        }
    };

    Ok(link.header.index)
}

async fn bring_up_interface(handle: &Handle, if_index: u32) -> anyhow::Result<()> {
    debug!("Bringing up interface with index {}", if_index);
    handle
        .link()
        .set(
            LinkUnspec::new_with_index(if_index)
                .up() // This sets the interface to the "up" state
                .build(),
        )
        .execute()
        .await
        .with_context(|| format!("Failed to bring up interface {}", if_index))?;
    debug!("Successfully brought up interface {}", if_index);

    Ok(())
}

async fn enter_ns<O, FunRet, FutRetOutput>(ns: &Namespace, future: FunRet) -> anyhow::Result<O>
where
    FunRet: Future<Output = FutRetOutput>,
    FutRetOutput: Into<anyhow::Result<O>>,
{
    debug!("Entering network namespace");
    let cur_ns = Namespace::current_net()?;
    trace!("Saved current network namespace");

    debug!("Switching to target namespace");
    setns(ns.0.as_fd(), CloneFlags::CLONE_NEWNET)?;
    debug!("Successfully switched to target namespace");

    debug!("Executing function in namespace");
    let res: anyhow::Result<O> = future.await.into();

    debug!("Returning to original namespace");
    setns(cur_ns.0, CloneFlags::CLONE_NEWNET)?;
    debug!("Successfully returned to original namespace");

    res
}

// Unique identifier for our tailscale NAT rules
const TS_NAT_RULE_MARKER: &[u8] = b"tailscale-systray-nat-v1";
const TS_NAT6_RULE_MARKER: &[u8] = b"tailscale-systray-nat6-v1";
const TS_FORWARD_OUT_RULE_MARKER: &[u8] = b"tailscale-systray-fwd-out-v1";
const TS_FORWARD_IN_RULE_MARKER: &[u8] = b"tailscale-systray-fwd-in-v1";
const TS_FORWARD6_OUT_RULE_MARKER: &[u8] = b"tailscale-systray-fwd6-out-v1";
const TS_FORWARD6_IN_RULE_MARKER: &[u8] = b"tailscale-systray-fwd6-in-v1";

// Netlink message parsing helpers (these are macros in C but we implement as functions)
const NLMSG_ALIGNTO: u32 = 4;

#[inline]
unsafe fn nlmsg_align(len: u32) -> u32 {
    (len + NLMSG_ALIGNTO - 1) & !(NLMSG_ALIGNTO - 1)
}

#[inline]
unsafe fn nlmsg_ok(nlh: *const libc::nlmsghdr, len: i32) -> bool {
    len >= std::mem::size_of::<libc::nlmsghdr>() as i32
        && (*nlh).nlmsg_len >= std::mem::size_of::<libc::nlmsghdr>() as u32
        && (*nlh).nlmsg_len as i32 <= len
}

#[inline]
unsafe fn nlmsg_next(nlh: *const libc::nlmsghdr) -> *const libc::nlmsghdr {
    let offset = nlmsg_align((*nlh).nlmsg_len);
    (nlh as *const u8).add(offset as usize) as *const libc::nlmsghdr
}

// Extension trait for TokioSocket to support sendmsg with iovecs
trait TokioSocketExt {
    async fn sendmsg<'a, M>(&mut self, messages: M, addr: &SocketAddr) -> std::io::Result<usize>
    where
        M: IntoIterator<Item = &'a [u8]>;
}

impl TokioSocketExt for TokioSocket {
    async fn sendmsg<'a, M>(&mut self, messages: M, addr: &SocketAddr) -> std::io::Result<usize>
    where
        M: IntoIterator<Item = &'a [u8]>,
    {
        let iovecs: Vec<IoSlice> = messages.into_iter().map(IoSlice::new).collect();

        // TokioSocket is TokioSocket(AsyncFd<Socket>), so we can transmute it
        // to access the AsyncFd's async_io method
        let async_fd: &mut tokio::io::unix::AsyncFd<netlink_sys::Socket> =
            unsafe { std::mem::transmute(self) };

        async_fd
            .async_io(tokio::io::Interest::WRITABLE, |inner| {
                let fd = inner.as_raw_fd();

                let mut msg: libc::msghdr = unsafe { std::mem::zeroed() };
                msg.msg_name = addr as *const _ as *mut libc::c_void;
                msg.msg_namelen = std::mem::size_of::<SocketAddr>() as u32;
                msg.msg_iov = iovecs.as_ptr() as *mut libc::iovec;

                #[cfg(target_env = "musl")]
                {
                    msg.msg_iovlen = iovecs.len() as i32;
                }

                #[cfg(not(target_env = "musl"))]
                {
                    msg.msg_iovlen = iovecs.len();
                }

                let result = unsafe { libc::sendmsg(fd, &msg, 0) };

                if result < 0 {
                    Err(std::io::Error::last_os_error())
                } else {
                    Ok(result as usize)
                }
            })
            .await
    }
}

// Helper function to create and configure a netlink netfilter socket
fn create_netfilter_socket() -> anyhow::Result<netlink_sys::TokioSocket> {
    let mut socket =
        TokioSocket::new(NETLINK_NETFILTER).context("Failed to create netlink socket")?;

    // Set NETLINK_EXT_ACK option for better error messages
    unsafe {
        let fd = socket.socket_mut().as_raw_fd();
        let optval: libc::c_int = 1;
        libc::setsockopt(
            fd,
            libc::SOL_NETLINK,
            libc::NETLINK_EXT_ACK,
            &optval as *const _ as *const libc::c_void,
            std::mem::size_of::<libc::c_int>() as libc::socklen_t,
        );
    }

    let mut addr = SocketAddr::new(0, 0);
    socket
        .socket_mut()
        .bind(&addr)
        .context("Failed to bind netlink socket")?;

    socket
        .socket_mut()
        .get_address(&mut addr)
        .context("Failed to get socket address")?;

    Ok(socket)
}

// XT Target expression for iptables compatibility
pub struct XtTarget {
    name: &'static CStr,
    is_ipv6: bool,
}

impl XtTarget {
    pub fn masquerade() -> Self {
        Self {
            name: c"MASQUERADE",
            is_ipv6: false,
        }
    }

    pub fn masquerade_v6() -> Self {
        Self {
            name: c"MASQUERADE",
            is_ipv6: true,
        }
    }
}

// target info buffer for MASQUERADE xt target (24 bytes for IPv4)
const XT_TG_INFO: [u8; 24] = [
    1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

// target info buffer for MASQUERADE xt target (40 bytes for IPv6)
const XT_TG_INFO_V6: [u8; 40] = [
    0u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
];

impl Expression for XtTarget {
    fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr {
        unsafe {
            debug!("XtTarget: Allocating xt target expression");
            let expr = sys::nftnl_expr_alloc(c"target".as_ptr());
            if expr.is_null() {
                error!("XtTarget: nftnl_expr_alloc returned null for 'target'");
                return std::ptr::null_mut();
            }

            // set target name - nftnl_expr_set_str copies the string, so this is safe
            sys::nftnl_expr_set_str(expr, sys::NFTNL_EXPR_TG_NAME as u16, self.name.as_ptr());

            // set revision to 0
            sys::nftnl_expr_set_u32(expr, sys::NFTNL_EXPR_TG_REV as u16, 0);

            // Use the correct target info buffer based on IPv4/IPv6
            let (tg_info_ptr, tg_info_len) = if self.is_ipv6 {
                (XT_TG_INFO_V6.as_ptr(), XT_TG_INFO_V6.len())
            } else {
                (XT_TG_INFO.as_ptr(), XT_TG_INFO.len())
            };

            let tg_info_copy = libc::malloc(tg_info_len);
            std::ptr::copy_nonoverlapping(tg_info_ptr, tg_info_copy as *mut _, tg_info_len);

            // set target info (takes ownership of the data buffer)
            sys::nftnl_expr_set(
                expr,
                sys::NFTNL_EXPR_TG_INFO as u16,
                tg_info_copy as *const _,
                tg_info_len as u32,
            );

            expr
        }
    }
}

// Connection tracking state bits (from nf_conntrack_common.h)
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum NfCtState {
    Invalid = 0x0001,     // NF_CT_STATE_INVALID_BIT
    Established = 0x0002, // NF_CT_STATE_BIT(IP_CT_ESTABLISHED)
    Related = 0x0004,     // NF_CT_STATE_BIT(IP_CT_RELATED)
    New = 0x0008,         // NF_CT_STATE_BIT(IP_CT_NEW)
    Untracked = 0x0040,   // NF_CT_STATE_UNTRACKED_BIT
}

// xt_conntrack match flags (from xt_conntrack.h)
#[repr(u16)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum XtConntrackFlags {
    State = 1 << 0,        // XT_CONNTRACK_STATE
    Proto = 1 << 1,        // XT_CONNTRACK_PROTO
    OrigSrc = 1 << 2,      // XT_CONNTRACK_ORIGSRC
    OrigDst = 1 << 3,      // XT_CONNTRACK_ORIGDST
    ReplSrc = 1 << 4,      // XT_CONNTRACK_REPLSRC
    ReplDst = 1 << 5,      // XT_CONNTRACK_REPLDST
    Status = 1 << 6,       // XT_CONNTRACK_STATUS
    Expires = 1 << 7,      // XT_CONNTRACK_EXPIRES
    OrigSrcPort = 1 << 8,  // XT_CONNTRACK_ORIGSRC_PORT
    OrigDstPort = 1 << 9,  // XT_CONNTRACK_ORIGDST_PORT
    ReplSrcPort = 1 << 10, // XT_CONNTRACK_REPLSRC_PORT
    ReplDstPort = 1 << 11, // XT_CONNTRACK_REPLDST_PORT
    Direction = 1 << 12,   // XT_CONNTRACK_DIRECTION
}

// Matches the kernel's xt_conntrack_mtinfo3 structure (168 bytes)
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
#[allow(dead_code)]
struct XtConntrackMtinfo3 {
    origsrc_addr: [u8; 16],
    origsrc_mask: [u8; 16],
    origdst_addr: [u8; 16],
    origdst_mask: [u8; 16],
    replsrc_addr: [u8; 16],
    replsrc_mask: [u8; 16],
    repldst_addr: [u8; 16],
    repldst_mask: [u8; 16],
    expires_min: u32,
    expires_max: u32,
    l4proto: u16,
    origsrc_port: u16,
    origdst_port: u16,
    replsrc_port: u16,
    repldst_port: u16,
    match_flags: u16,
    invert_flags: u16,
    state_mask: u16,
    status_mask: u16,
    origsrc_port_high: u16,
    origdst_port_high: u16,
    replsrc_port_high: u16,
    repldst_port_high: u16,
    _padding: [u8; 4], // Explicit padding to reach 168 bytes
}

const_assert_eq!(std::mem::size_of::<XtConntrackMtinfo3>(), 168);

pub struct XtConntrack3 {
    mtinfo: XtConntrackMtinfo3,
}

impl XtConntrack3 {
    // Match ESTABLISHED and RELATED states (verified: 0x02 | 0x04 = 0x06)
    pub fn conntrack_state_established_related() -> Self {
        let mtinfo = XtConntrackMtinfo3 {
            match_flags: XtConntrackFlags::State as u16,
            state_mask: NfCtState::Established as u16 | NfCtState::Related as u16,
            ..Default::default()
        };

        Self { mtinfo }
    }
}

impl Expression for XtConntrack3 {
    fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr {
        unsafe {
            trace!("XtConntrack3: Allocating xt match expression for conntrack");
            let expr = sys::nftnl_expr_alloc(c"match".as_ptr());
            if expr.is_null() {
                error!("XtConntrack3: nftnl_expr_alloc returned null for 'match'");
                return std::ptr::null_mut();
            }

            sys::nftnl_expr_set_str(expr, sys::NFTNL_EXPR_MT_NAME as u16, c"conntrack".as_ptr());
            sys::nftnl_expr_set_u32(expr, sys::NFTNL_EXPR_MT_REV as u16, 3);

            // Convert the structure to bytes
            let mt_info_ptr = &self.mtinfo as *const XtConntrackMtinfo3 as *const u8;
            let mt_info_len = std::mem::size_of::<XtConntrackMtinfo3>();

            let mt_info_copy = libc::malloc(mt_info_len);
            std::ptr::copy_nonoverlapping(mt_info_ptr, mt_info_copy as *mut u8, mt_info_len);

            sys::nftnl_expr_set(
                expr,
                sys::NFTNL_EXPR_MT_INFO as u16,
                mt_info_copy as *const _,
                mt_info_len as u32,
            );

            debug!(
                "XtConntrack3: Set match info (state_mask=0x{:04x}, match_flags=0x{:04x})",
                self.mtinfo.state_mask, self.mtinfo.match_flags
            );

            expr
        }
    }
}

async fn check_rule_exists(
    socket: &mut netlink_sys::TokioSocket,
    proto_family: u32,
    table: &CStr,
    chain: &CStr,
    marker: &[u8],
    proto_name: &str,
) -> anyhow::Result<bool> {
    use nftnl::nftnl_sys as sys;

    debug!(
        "Checking if {} rule already exists in {}/{}",
        proto_name,
        table.to_str().unwrap_or("?"),
        chain.to_str().unwrap_or("?")
    );

    // use nftnl_sys, we don't have high-level bindings for rule listing
    let (buffer, msg_len) = unsafe {
        let rule = sys::nftnl_rule_alloc();
        if rule.is_null() {
            anyhow::bail!("Failed to allocate nftnl_rule for GETRULE");
        }

        // Set the table and chain we want to query
        sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_TABLE as u16, table.as_ptr());
        sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_CHAIN as u16, chain.as_ptr());
        sys::nftnl_rule_set_u32(rule, sys::NFTNL_RULE_FAMILY as u16, proto_family);

        let mut buffer = vec![0u8; 4096];

        // Build netlink message header with GETRULE type and DUMP flag
        let nlh = sys::nftnl_nlmsg_build_hdr(
            buffer.as_mut_ptr() as *mut _,
            libc::NFT_MSG_GETRULE as u16,
            proto_family as u16,
            (libc::NLM_F_REQUEST | libc::NLM_F_DUMP) as u16,
            0, // seq
        );

        if nlh.is_null() {
            sys::nftnl_rule_free(rule);
            anyhow::bail!("Failed to build netlink header for GETRULE");
        }

        sys::nftnl_rule_nlmsg_build_payload(nlh, rule);

        let msg_len = (*nlh).nlmsg_len as usize;

        // Free the rule object - the message is now in the buffer
        sys::nftnl_rule_free(rule);

        (buffer, msg_len)
    };

    let kernel_addr = SocketAddr::new(0, 0);
    debug!("Sending {} GETRULE request ({} bytes)", proto_name, msg_len);
    socket
        .send_to(&buffer[..msg_len], &kernel_addr)
        .await
        .context(format!("Failed to send {} GETRULE request", proto_name))?;

    debug!("Waiting for {} GETRULE responses", proto_name);
    // Read responses with timeout
    let timeout_duration = std::time::Duration::from_secs(2);

    let mut found = false;

    loop {
        let recv_result = tokio::time::timeout(timeout_duration, socket.recv_from_full()).await;

        let (recv_buffer, _addr) = match recv_result {
            Ok(Ok((buf, addr))) => {
                debug!("Received netlink response ({} bytes)", buf.len());
                (buf, addr)
            }
            Ok(Err(e)) => {
                return Err(anyhow::anyhow!(
                    "Failed to receive {} GETRULE response: {}",
                    proto_name,
                    e
                ));
            }
            Err(_) => {
                // Timeout - no more messages
                debug!(
                    "Timeout waiting for responses, {} rule found: {}",
                    proto_name, found
                );
                return Ok(found);
            }
        };

        // Parse netlink messages using netlink iteration helpers
        unsafe {
            let mut nlh = recv_buffer.as_ptr() as *const libc::nlmsghdr;
            let mut remaining = recv_buffer.len() as i32;

            // Iterate through netlink messages
            while nlmsg_ok(nlh, remaining) {
                let nlmsg_type = (*nlh).nlmsg_type;
                let nlmsg_len = (*nlh).nlmsg_len;

                debug!("Parsing message: type={}, len={}", nlmsg_type, nlmsg_len);

                // Check for NLMSG_DONE
                if nlmsg_type == libc::NLMSG_DONE as u16 {
                    debug!(
                        "Received NLMSG_DONE, finished checking {} rules",
                        proto_name
                    );
                    return Ok(found);
                }

                // Check for NLMSG_ERROR
                if nlmsg_type == libc::NLMSG_ERROR as u16 {
                    debug!("Received NLMSG_ERROR");
                    // Update remaining length and move to next message
                    remaining -= nlmsg_align(nlmsg_len) as i32;
                    nlh = nlmsg_next(nlh);
                    continue;
                }

                // NFT_MSG_NEWRULE = ((NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWRULE)
                let nft_msg_newrule =
                    ((libc::NFNL_SUBSYS_NFTABLES << 8) | libc::NFT_MSG_NEWRULE) as u16;
                if nlmsg_type == nft_msg_newrule {
                    debug!(
                        "Found NFT_MSG_NEWRULE, checking for our {} marker",
                        proto_name
                    );

                    let rule_ptr = sys::nftnl_rule_alloc();
                    if rule_ptr.is_null() {
                        debug!("Failed to allocate nftnl_rule");
                        remaining -= nlmsg_align(nlmsg_len) as i32;
                        nlh = nlmsg_next(nlh);
                        continue;
                    }

                    // Parse the netlink message into the rule structure
                    if sys::nftnl_rule_nlmsg_parse(nlh, rule_ptr) < 0 {
                        debug!("Failed to parse rule message");
                        sys::nftnl_rule_free(rule_ptr);
                        remaining -= nlmsg_align(nlmsg_len) as i32;
                        nlh = nlmsg_next(nlh);
                        continue;
                    }

                    // Get userdata from the rule
                    let mut userdata_len: u32 = 0;
                    let userdata_ptr = sys::nftnl_rule_get_data(
                        rule_ptr,
                        sys::NFTNL_RULE_USERDATA as u16,
                        &mut userdata_len as *mut u32,
                    );

                    if !userdata_ptr.is_null() && userdata_len == marker.len() as u32 {
                        let userdata_slice = std::slice::from_raw_parts(
                            userdata_ptr as *const u8,
                            userdata_len as usize,
                        );
                        trace!(
                            "Found userdata (len={}): {:?}, looking for: {:?}",
                            userdata_len,
                            std::str::from_utf8(userdata_slice).unwrap_or("?"),
                            std::str::from_utf8(marker).unwrap_or("?")
                        );
                        if userdata_slice == marker {
                            debug!("Found existing {} rule with our marker!", proto_name);
                            found = true;
                        } else {
                            debug!("Found userdata but it doesn't match our marker");
                        }
                    } else {
                        trace!(
                            "No userdata found in this rule (ptr null={}, len={})",
                            userdata_ptr.is_null(),
                            userdata_len
                        );
                    }

                    sys::nftnl_rule_free(rule_ptr);
                }

                // Update remaining length and move to next message
                remaining -= nlmsg_align(nlmsg_len) as i32;
                nlh = nlmsg_next(nlh);
            }
        }
    }
}

unsafe fn set_rule_userdata(rule: &Rule, marker: &[u8]) {
    // alas, we need to break the abstraction as Rule.rule is not public
    let rule_as_bytes = rule as *const Rule as *const u8;
    // the rust compiler is swapping the two fields of Rule;
    // *mut sys::nftnl_rule shows up later in memory
    let rule_ptr_ptr = rule_as_bytes.offset(8) as *const *mut sys::nftnl_rule;
    let rule_ptr = *rule_ptr_ptr;

    sys::nftnl_rule_set_data(
        rule_ptr,
        sys::NFTNL_RULE_USERDATA as u16,
        marker.as_ptr() as *const std::os::raw::c_void,
        marker.len() as u32,
    );
}

async fn nat_rule_exists(socket: &mut netlink_sys::TokioSocket) -> anyhow::Result<bool> {
    check_rule_exists(
        socket,
        libc::NFPROTO_IPV4 as u32,
        c"nat",
        c"POSTROUTING",
        TS_NAT_RULE_MARKER,
        "IPv4 NAT",
    )
    .await
}

async fn nat6_rule_exists(socket: &mut netlink_sys::TokioSocket) -> anyhow::Result<bool> {
    check_rule_exists(
        socket,
        libc::NFPROTO_IPV6 as u32,
        c"nat",
        c"POSTROUTING",
        TS_NAT6_RULE_MARKER,
        "IPv6 NAT",
    )
    .await
}

async fn create_nat6_rule() -> anyhow::Result<()> {
    info!("Creating IPv6 NAT rule for fc00::/64 subnet");

    let mut socket = create_netfilter_socket()?;

    // First, ensure the nat table and POSTROUTING chain exist
    ensure_nat_table_and_chain_for_family(&mut socket, ProtoFamily::Ipv6).await?;

    // Check if our rule already exists
    if nat6_rule_exists(&mut socket).await? {
        info!("IPv6 NAT rule already exists, skipping creation");
        return Ok(());
    }

    // Create the NAT rule
    let table = Table::new(&c"nat", ProtoFamily::Ipv6);
    let chain = Chain::new(&c"POSTROUTING", &table);
    let mut rule = Rule::new(&chain);

    debug!("Building IPv6 NAT rule for POSTROUTING chain");

    // Load nfproto metadata (check if IPv6)
    rule.add_expr(&nft_expr!(meta nfproto));
    rule.add_expr(&nft_expr!(cmp == libc::NFPROTO_IPV6 as u8));

    // Load source IPv6 address
    rule.add_expr(&nft_expr!(payload ipv6 saddr));

    // Apply bitwise mask for fc00::/64 network
    // Network mask: ffff:ffff:ffff:ffff:0:0:0:0 (64-bit prefix)
    let network_mask: [u8; 16] = [
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ];
    let zero_xor: [u8; 16] = [0u8; 16];
    rule.add_expr(&nft_expr!(bitwise mask &network_mask[..], xor &zero_xor[..]));

    // Compare with network address fc00::
    let network_addr: [u8; 16] = [
        0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ];
    rule.add_expr(&nft_expr!(cmp == &network_addr[..]));

    // Add a counter
    rule.add_expr(&nft_expr!(counter));

    debug!("About to add XtTarget expression for IPv6");
    rule.add_expr(&XtTarget::masquerade_v6());
    debug!("XtTarget expression added for IPv6");

    debug!("Adding user data marker to IPv6 NAT rule");
    unsafe {
        set_rule_userdata(&rule, TS_NAT6_RULE_MARKER);
    }

    debug!("Sending IPv6 NAT rule to netfilter");
    send_rule(&mut socket, &rule).await?;

    info!("IPv6 NAT rule created successfully");
    Ok(())
}

async fn create_nat_rule() -> anyhow::Result<()> {
    info!("Creating NAT rule for 172.31.0.0/30 subnet");

    let mut socket = create_netfilter_socket()?;

    // First, ensure the nat table and POSTROUTING chain exist
    ensure_nat_table_and_chain_for_family(&mut socket, ProtoFamily::Ipv4).await?;

    // Check if our rule already exists
    if nat_rule_exists(&mut socket).await? {
        info!("NAT rule already exists, skipping creation");
        return Ok(());
    }

    // Create the NAT rule
    let table = Table::new(&c"nat", ProtoFamily::Ipv4);
    let chain = Chain::new(&c"POSTROUTING", &table);
    let mut rule = Rule::new(&chain);

    debug!("Building NAT rule for POSTROUTING chain");

    // Load nfproto metadata (check if IPv4)
    rule.add_expr(&nft_expr!(meta nfproto));
    rule.add_expr(&nft_expr!(cmp == libc::NFPROTO_IPV4 as u8));

    // Load source IP address
    rule.add_expr(&nft_expr!(payload ipv4 saddr));

    // Apply bitwise mask for 172.31.0.0/30 network
    // Network mask: 255.255.255.252 (30-bit prefix)
    let network_mask = 0xfffffffcu32.to_be_bytes();
    let zero_xor: &[u8] = &[0u8, 0u8, 0u8, 0u8];
    rule.add_expr(&nft_expr!(bitwise mask &network_mask[..], xor zero_xor));

    // Compare with network address 172.31.0.0
    let network_addr = Ipv4Addr::new(172, 31, 0, 0).octets();
    rule.add_expr(&nft_expr!(cmp == &network_addr[..]));

    // Add a counter
    rule.add_expr(&nft_expr!(counter));

    // Add masquerade action using xt target for iptables compatibility
    debug!("About to add XtTarget expression");
    rule.add_expr(&XtTarget::masquerade());
    debug!("XtTarget expression added");
    // alternative not compatible with iptables:
    // rule.add_expr(&nftnl::expr::Masquerade);

    // Add our unique marker as userdata to identify this rule later
    debug!("Adding user data marker to NAT rule");
    unsafe {
        set_rule_userdata(&rule, TS_NAT_RULE_MARKER);
    }

    debug!("Sending NAT rule to netfilter");
    send_rule(&mut socket, &rule).await?;

    info!("NAT rule created successfully");
    Ok(())
}

async fn ensure_nat_table_and_chain_for_family(
    socket: &mut netlink_sys::TokioSocket,
    family: ProtoFamily,
) -> anyhow::Result<()> {
    let family_name = match family {
        ProtoFamily::Ipv4 => "IPv4",
        ProtoFamily::Ipv6 => "IPv6",
        _ => "unknown",
    };
    info!(
        "Ensuring {} nat table and POSTROUTING chain exist",
        family_name
    );

    let mut batch = Batch::new();
    let table = Table::new(&c"nat", family);
    let mut chain = Chain::new(&c"POSTROUTING", &table);
    chain.set_hook(nftnl::Hook::PostRouting, 100); // NF_INET_POST_ROUTING = 4, priority = 100 (NF_IP_PRI_NAT_SRC)
    chain.set_policy(nftnl::Policy::Accept);
    chain.set_type(nftnl::ChainType::Nat);
    batch.add(&table, nftnl::MsgType::Add);
    batch.add(&chain, nftnl::MsgType::Add);
    let finalized_batch = batch.finalize();

    let kernel_addr = SocketAddr::new(0, 0);
    let bytes_sent = socket
        .sendmsg(&finalized_batch, &kernel_addr)
        .await
        .context(format!(
            "Failed to send {} table/chain creation batch",
            family_name
        ))?;

    debug!("Sent {} bytes", bytes_sent);
    info!("{} table and chain creation batch sent", family_name);
    Ok(())
}

async fn send_rule<'a>(
    socket: &mut netlink_sys::TokioSocket,
    rule: &'a Rule<'a>,
) -> anyhow::Result<()> {
    debug!("Sending NAT rule to netfilter");

    let mut batch = Batch::new();
    batch.add(rule, nftnl::MsgType::Add);
    let finalized_batch = batch.finalize();

    let kernel_addr = SocketAddr::new(0, 0);
    let bytes_sent = socket
        .sendmsg(&finalized_batch, &kernel_addr)
        .await
        .context("Failed to send batch to netfilter")?;

    debug!("Sent {} bytes", bytes_sent);

    // The kernel doesn't send ACKs for successful batched operations (no NLM_F_ACK flag?),
    // but it DOES send NLMSG_ERROR immediately if there's a problem. Check for errors
    // with a short timeout to catch any immediate failures.
    let timeout_duration = std::time::Duration::from_millis(100);
    match tokio::time::timeout(timeout_duration, socket.recv_from_full()).await {
        Ok(Ok((recv_buffer, _addr))) => {
            debug!(
                "Received response from kernel ({} bytes)",
                recv_buffer.len()
            );

            // check if it's an error
            unsafe {
                let nlh = recv_buffer.as_ptr() as *const libc::nlmsghdr;
                let remaining = recv_buffer.len() as i32;

                if nlmsg_ok(nlh, remaining) {
                    let nlmsg_type = (*nlh).nlmsg_type;

                    if nlmsg_type == libc::NLMSG_ERROR as u16 {
                        let err_msg = (nlh as *const u8).add(std::mem::size_of::<libc::nlmsghdr>())
                            as *const libc::nlmsgerr;
                        let error_code = (*err_msg).error;

                        if error_code != 0 {
                            error!("Kernel rejected NAT rule with error code: {}", error_code);
                            anyhow::bail!(
                                "Kernel rejected NAT rule: {} ({})",
                                std::io::Error::from_raw_os_error(-error_code),
                                error_code
                            );
                        }
                    }
                }
            }
        }
        Ok(Err(e)) => {
            anyhow::bail!("Failed to receive response from kernel: {}", e);
        }
        Err(_) => {
            // Timeout is expected - no error means success
            debug!("No error response from kernel (success)");
        }
    }

    info!("NAT rule batch sent successfully");
    Ok(())
}

fn enable_interface_forwarding(interface: &str) -> anyhow::Result<()> {
    // Enable IPv4 forwarding on the specific interface
    let ipv4_path = format!("/proc/sys/net/ipv4/conf/{}/forwarding", interface);
    let mut f =
        File::create(&ipv4_path).with_context(|| format!("Failed to open {}", ipv4_path))?;
    f.write_all(b"1")
        .context("Failed to enable IPv4 forwarding")?;
    info!("IPv4 forwarding enabled on {} interface", interface);

    // Enable IPv6 forwarding on the specific interface
    let ipv6_path = format!("/proc/sys/net/ipv6/conf/{}/forwarding", interface);
    let mut f =
        File::create(&ipv6_path).with_context(|| format!("Failed to open {}", ipv6_path))?;
    f.write_all(b"1")
        .context("Failed to enable IPv6 forwarding")?;
    info!("IPv6 forwarding enabled on {} interface", interface);

    Ok(())
}

async fn ensure_filter_table_and_chain(
    socket: &mut netlink_sys::TokioSocket,
    family: ProtoFamily,
) -> anyhow::Result<()> {
    let family_name = match family {
        ProtoFamily::Ipv4 => "IPv4",
        ProtoFamily::Ipv6 => "IPv6",
        _ => "unknown",
    };
    info!(
        "Ensuring {} filter table and FORWARD chain exist",
        family_name
    );

    let mut batch = Batch::new();
    let table = Table::new(&c"filter", family);
    let mut chain = Chain::new(&c"FORWARD", &table);
    chain.set_hook(nftnl::Hook::Forward, 0);
    chain.set_policy(nftnl::Policy::Accept);
    chain.set_type(nftnl::ChainType::Filter);
    batch.add(&table, nftnl::MsgType::Add);
    batch.add(&chain, nftnl::MsgType::Add);
    let finalized_batch = batch.finalize();

    let kernel_addr = SocketAddr::new(0, 0);
    let bytes_sent = socket
        .sendmsg(&finalized_batch, &kernel_addr)
        .await
        .context("Failed to send filter table/chain creation batch")?;

    debug!("Sent {} bytes", bytes_sent);
    info!("{} filter table and chain creation batch sent", family_name);
    Ok(())
}

async fn create_forward_rules(interface: &str) -> anyhow::Result<()> {
    let mut socket = create_netfilter_socket()?;

    ensure_filter_table_and_chain(&mut socket, ProtoFamily::Ipv4).await?;
    ensure_filter_table_and_chain(&mut socket, ProtoFamily::Ipv6).await?;

    create_forward_rules_for_family(&mut socket, ProtoFamily::Ipv4, interface).await?;
    create_forward_rules_for_family(&mut socket, ProtoFamily::Ipv6, interface).await?;

    info!("FORWARD rules created successfully for {}", interface);
    Ok(())
}

async fn create_forward_rules_for_family(
    socket: &mut netlink_sys::TokioSocket,
    family: ProtoFamily,
    interface: &str,
) -> anyhow::Result<()> {
    let (family_name, proto_family, out_marker, in_marker) = match family {
        ProtoFamily::Ipv4 => (
            "IPv4",
            libc::NFPROTO_IPV4 as u32,
            TS_FORWARD_OUT_RULE_MARKER,
            TS_FORWARD_IN_RULE_MARKER,
        ),
        ProtoFamily::Ipv6 => (
            "IPv6",
            libc::NFPROTO_IPV6 as u32,
            TS_FORWARD6_OUT_RULE_MARKER,
            TS_FORWARD6_IN_RULE_MARKER,
        ),
        _ => anyhow::bail!("Unsupported protocol family"),
    };

    let c_interface = CString::new(interface).unwrap();

    info!("Creating {} FORWARD rules for {}", family_name, interface);

    let table = Table::new(&c"filter", family);
    let chain = Chain::new(&c"FORWARD", &table);

    // Rule 1: Accept packets from interface (outbound from namespace)
    let out_exists = check_rule_exists(
        socket,
        proto_family,
        c"filter",
        c"FORWARD",
        out_marker,
        &format!("{} FORWARD out", family_name),
    )
    .await?;

    if !out_exists {
        let mut rule_out = Rule::new(&chain);
        rule_out.add_expr(&nft_expr!(meta iifname));
        rule_out.add_expr(&nft_expr!(cmp == c_interface.as_bytes_with_nul()));
        rule_out.add_expr(&nft_expr!(counter));
        rule_out.add_expr(&nft_expr!(verdict accept));

        // Add marker to rule 1
        debug!(
            "Setting {} FORWARD out marker: {:?}",
            family_name,
            std::str::from_utf8(out_marker).unwrap_or("?")
        );
        unsafe {
            set_rule_userdata(&rule_out, out_marker);
        }

        send_rule(socket, &rule_out).await?;
        info!("{} FORWARD out rule created", family_name);
    } else {
        info!("{} FORWARD out rule already exists", family_name);
    }

    // Rule 2: Accept RELATED,ESTABLISHED packets to interface (return traffic)
    let in_exists = check_rule_exists(
        socket,
        proto_family,
        c"filter",
        c"FORWARD",
        in_marker,
        &format!("{} FORWARD in", family_name),
    )
    .await?;

    if !in_exists {
        // Use xt_match for iptables compatibility
        let mut rule_in = Rule::new(&chain);
        rule_in.add_expr(&nft_expr!(meta oifname));
        rule_in.add_expr(&nft_expr!(cmp == c_interface.as_bytes_with_nul()));
        rule_in.add_expr(&XtConntrack3::conntrack_state_established_related());
        rule_in.add_expr(&nft_expr!(counter));
        rule_in.add_expr(&nft_expr!(verdict accept));

        // Add marker to rule 2
        debug!(
            "Setting {} FORWARD in marker: {:?}",
            family_name,
            std::str::from_utf8(in_marker).unwrap_or("?")
        );
        unsafe {
            set_rule_userdata(&rule_in, in_marker);
        }

        send_rule(socket, &rule_in).await?;
        info!("{} FORWARD in rule created", family_name);
    } else {
        info!("{} FORWARD in rule already exists", family_name);
    }

    info!("{} FORWARD rules created for {}", family_name, interface);
    Ok(())
}

pub async fn setup_interfaces() -> anyhow::Result<()> {
    info!("Setting up network interfaces for Tailscale in isolated namespace");

    debug!("Creating namespaces");
    let namespaces = create_namespace()?;

    debug!("Establishing rtnetlink connection");
    let (connection, handle, _) = new_connection()?;
    tokio::spawn(connection);
    debug!("Rtnetlink connection established");

    info!("Setting up veth interface pair");
    create_veth_pair(&handle, &namespaces.net).await?;

    info!("Adding default route in namespace");
    enter_ns(&namespaces.net, async {
        debug!("Establishing rtnetlink connection inside namespace");
        let (connection, handle, _) = new_connection()?;
        tokio::spawn(connection);
        debug!("Rtnetlink connection established inside namespace");

        let veth_ns_idx = ifname_to_index(&handle, VETH_NAME_NS).await?;
        debug!(
            "Index of {} inside namespace: {}",
            VETH_NAME_NS, veth_ns_idx
        );

        bring_up_interface(&handle, veth_ns_idx).await?;
        configure_veth_ns(&handle, veth_ns_idx).await?;
        add_default_route(&handle).await?;
        Ok(())
    })
    .await?;

    info!("Setting up NAT for namespace traffic");
    create_nat_rule().await?;
    create_nat6_rule().await?;

    info!("Enabling forwarding on {} interface", VETH_NAME_HOST);
    enable_interface_forwarding(VETH_NAME_HOST)?;

    info!("Creating FORWARD rules for {} interface", VETH_NAME_HOST);
    create_forward_rules(VETH_NAME_HOST).await?;

    info!("Network interfaces setup completed successfully");
    Ok(())
}