pelagos 0.64.0

Fast Linux container runtime — OCI-compatible, namespaces, cgroups v2, seccomp, networking, image management
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
//! Native Linux networking primitives: ioctl-based bridge/link management and
//! RTNETLINK messages for address, route, neighbour, veth, and netns operations.
//!
//! All functions that map to operations with natural "already exists" semantics
//! (`addr_add_*`, `route_add_*`, `neigh_add_*`, `create_bridge`, `netns_create`)
//! treat `EEXIST` as success.

use std::ffi::CString;
use std::io;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::os::unix::io::RawFd;

// ── Constants not exported by libc ────────────────────────────────────────────

const SIOCBRADDBR: libc::c_ulong = 0x89a0;
const SIOCBRADDIF: libc::c_ulong = 0x89a2;

const IFLA_IFNAME: u16 = 3;
const IFLA_LINKINFO: u16 = 18;
const IFLA_NET_NS_FD: u16 = 28;
const IFLA_INFO_KIND: u16 = 1;
const IFLA_INFO_DATA: u16 = 2;
const VETH_INFO_PEER: u16 = 1;

const IFA_ADDRESS: u16 = 1;
const IFA_LOCAL: u16 = 2;
const IFA_BROADCAST: u16 = 4;
const IFA_FLAGS: u16 = 8;

const RTA_OIF: u16 = 4;
const RTA_GATEWAY: u16 = 5;

const NDA_DST: u16 = 1;
const NDA_LLADDR: u16 = 2;

const RT_SCOPE_UNIVERSE: u8 = 0;
const RTPROT_STATIC: u8 = 4;
const RTN_UNICAST: u8 = 1;

const NUD_STALE: u16 = 4;
const IFA_F_NODAD: u32 = 0x02;

const NLM_F_REQUEST: u16 = 0x0001;
const NLM_F_ACK: u16 = 0x0004;
const NLM_F_CREATE: u16 = 0x0400;
const NLM_F_EXCL: u16 = 0x0200;
const NLM_F_REPLACE: u16 = 0x0100;
const NLMSG_ERROR_TYPE: u16 = 2;

// ── ioctl structs ─────────────────────────────────────────────────────────────

#[repr(C)]
struct IfreqIdx {
    ifr_name: [u8; 16],
    ifr_ifindex: i32,
    _pad: [u8; 18],
}

// ── Low-level helpers ─────────────────────────────────────────────────────────

fn copy_ifname(dst: &mut [u8; 16], name: &str) -> io::Result<()> {
    let b = name.as_bytes();
    if b.len() >= 16 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "interface name too long",
        ));
    }
    dst[..b.len()].copy_from_slice(b);
    dst[b.len()] = 0;
    Ok(())
}

fn if_index_of(name: &str) -> io::Result<i32> {
    let cname = CString::new(name)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "interface name contains NUL"))?;
    let idx = unsafe { libc::if_nametoindex(cname.as_ptr()) };
    if idx == 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(idx as i32)
}

fn udp_sock() -> io::Result<RawFd> {
    let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM | libc::SOCK_CLOEXEC, 0) };
    if fd < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(fd)
}

// ── Netlink message builder ───────────────────────────────────────────────────

struct NlBuf(Vec<u8>);

impl NlBuf {
    fn new() -> Self {
        NlBuf(Vec::with_capacity(256))
    }

    fn push_u8(&mut self, v: u8) {
        self.0.push(v);
    }

    fn push_u16(&mut self, v: u16) {
        self.0.extend_from_slice(&v.to_le_bytes());
    }

    fn push_u32(&mut self, v: u32) {
        self.0.extend_from_slice(&v.to_le_bytes());
    }

    fn push_i32(&mut self, v: i32) {
        self.0.extend_from_slice(&v.to_le_bytes());
    }

    fn push_bytes(&mut self, b: &[u8]) {
        self.0.extend_from_slice(b);
    }

    fn len(&self) -> usize {
        self.0.len()
    }

    fn pad_to_4(&mut self) {
        let rem = self.0.len() % 4;
        if rem != 0 {
            for _ in 0..(4 - rem) {
                self.0.push(0);
            }
        }
    }

    /// Append an rtattr (type, data), padded to 4 bytes.
    fn rta(&mut self, rta_type: u16, data: &[u8]) {
        let total = 4 + data.len();
        self.push_u16(total as u16);
        self.push_u16(rta_type);
        self.push_bytes(data);
        self.pad_to_4();
    }

    /// Reserve space for a nested rtattr header; returns the offset of the
    /// length field so it can be patched after the nested content is written.
    fn rta_begin_nested(&mut self, rta_type: u16) -> usize {
        let pos = self.0.len();
        self.push_u16(0); // placeholder length
        self.push_u16(rta_type);
        pos
    }

    /// Patch the length of a nested rtattr started with `rta_begin_nested`.
    fn rta_end_nested(&mut self, start: usize) {
        let len = (self.0.len() - start) as u16;
        self.0[start..start + 2].copy_from_slice(&len.to_le_bytes());
        self.pad_to_4();
    }
}

// ── Netlink socket send/recv ───────────────────────────────────────────────────

fn nl_socket() -> io::Result<RawFd> {
    let fd = unsafe {
        libc::socket(
            libc::AF_NETLINK,
            libc::SOCK_RAW | libc::SOCK_CLOEXEC,
            libc::NETLINK_ROUTE,
        )
    };
    if fd < 0 {
        return Err(io::Error::last_os_error());
    }

    // sockaddr_nl has a private padding field; zero-initialise then patch family.
    let mut sa: libc::sockaddr_nl = unsafe { std::mem::zeroed() };
    sa.nl_family = libc::AF_NETLINK as libc::sa_family_t;
    sa.nl_pid = 0;
    sa.nl_groups = 0;
    let ret = unsafe {
        libc::bind(
            fd,
            &sa as *const libc::sockaddr_nl as *const libc::sockaddr,
            std::mem::size_of::<libc::sockaddr_nl>() as libc::socklen_t,
        )
    };
    if ret < 0 {
        let e = io::Error::last_os_error();
        unsafe { libc::close(fd) };
        return Err(e);
    }

    Ok(fd)
}

/// Finalise the nlmsghdr length field (first 4 bytes) and send the message.
fn nl_send(fd: RawFd, buf: &mut NlBuf) -> io::Result<()> {
    let total = buf.len() as u32;
    buf.0[0..4].copy_from_slice(&total.to_le_bytes());

    let ret = unsafe { libc::send(fd, buf.0.as_ptr() as *const libc::c_void, buf.0.len(), 0) };
    if ret < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Receive the ACK and return Ok(()) or the kernel's errno as an error.
fn nl_recv_ack(fd: RawFd) -> io::Result<()> {
    let mut rbuf = [0u8; 4096];
    let n = unsafe { libc::recv(fd, rbuf.as_mut_ptr() as *mut libc::c_void, rbuf.len(), 0) };
    if n < 0 {
        return Err(io::Error::last_os_error());
    }
    let n = n as usize;
    // nlmsghdr is 16 bytes; NLMSG_ERROR payload starts at byte 16 with i32 errno.
    if n < 20 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "netlink response too short",
        ));
    }
    let msg_type = u16::from_le_bytes([rbuf[4], rbuf[5]]);
    if msg_type != NLMSG_ERROR_TYPE {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("unexpected netlink msg type {msg_type}"),
        ));
    }
    let errno = i32::from_le_bytes([rbuf[16], rbuf[17], rbuf[18], rbuf[19]]);
    if errno < 0 {
        return Err(io::Error::from_raw_os_error(-errno));
    }
    Ok(())
}

/// Build and send one netlink request, receive ACK; translate EEXIST to Ok when requested.
fn nl_request(buf: &mut NlBuf, eexist_ok: bool) -> io::Result<()> {
    let fd = nl_socket()?;
    let res = nl_send(fd, buf).and_then(|()| nl_recv_ack(fd));
    unsafe { libc::close(fd) };
    match res {
        Err(e) if eexist_ok && e.raw_os_error() == Some(libc::EEXIST) => Ok(()),
        other => other,
    }
}

// ── nlmsghdr helpers ──────────────────────────────────────────────────────────

/// Write the 16-byte nlmsghdr at the current position.
/// The `len` field is a placeholder (0) — patched later in `nl_send`.
fn push_nlmsghdr(buf: &mut NlBuf, msg_type: u16, flags: u16) {
    buf.push_u32(0); // len — patched in nl_send
    buf.push_u16(msg_type);
    buf.push_u16(flags);
    buf.push_u32(1); // seq
    buf.push_u32(0); // pid
}

/// Write the 16-byte ifinfomsg.
fn push_ifinfomsg(buf: &mut NlBuf, index: i32, flags: u32, change: u32) {
    buf.push_u8(libc::AF_UNSPEC as u8);
    buf.push_u8(0); // pad
    buf.push_u16(0); // ifi_type (ARPHRD_NETROM = 0, kernel fills in)
    buf.push_i32(index);
    buf.push_u32(flags);
    buf.push_u32(change);
}

/// Write the 8-byte ifaddrmsg.
fn push_ifaddrmsg(buf: &mut NlBuf, family: u8, prefixlen: u8, scope: u8, index: i32) {
    buf.push_u8(family);
    buf.push_u8(prefixlen);
    buf.push_u8(0); // flags
    buf.push_u8(scope);
    buf.push_u32(index as u32);
}

/// Write the 12-byte rtmsg.
fn push_rtmsg(
    buf: &mut NlBuf,
    family: u8,
    dst_len: u8,
    table: u8,
    protocol: u8,
    scope: u8,
    rt_type: u8,
) {
    buf.push_u8(family);
    buf.push_u8(dst_len);
    buf.push_u8(0); // src_len
    buf.push_u8(0); // tos
    buf.push_u8(table);
    buf.push_u8(protocol);
    buf.push_u8(scope);
    buf.push_u8(rt_type);
    buf.push_u32(0); // flags
}

/// Write the 12-byte ndmsg.
fn push_ndmsg(buf: &mut NlBuf, family: u8, ifindex: i32, state: u16) {
    buf.push_u8(family);
    buf.push_u8(0); // pad1
    buf.push_u16(0); // pad2
    buf.push_i32(ifindex);
    buf.push_u16(state);
    buf.push_u8(0); // flags
    buf.push_u8(0); // type
}

// ── Public API ────────────────────────────────────────────────────────────────

/// Bring up a network interface using RTM_NEWLINK with IFF_UP.
pub fn link_set_up(ifname: &str) -> io::Result<()> {
    let idx = if_index_of(ifname)?;
    let mut buf = NlBuf::new();
    push_nlmsghdr(&mut buf, libc::RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
    push_ifinfomsg(&mut buf, idx, libc::IFF_UP as u32, libc::IFF_UP as u32);
    nl_request(&mut buf, false)
}

/// Create a Linux bridge.  Returns Ok if it already exists.
pub fn create_bridge(name: &str) -> io::Result<()> {
    let cname = CString::new(name)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "bridge name contains NUL"))?;
    let fd = udp_sock()?;
    let ret = unsafe { libc::ioctl(fd, SIOCBRADDBR as _, cname.as_ptr()) };
    let err = io::Error::last_os_error();
    unsafe { libc::close(fd) };
    if ret < 0 && err.raw_os_error() != Some(libc::EEXIST) {
        return Err(err);
    }
    Ok(())
}

/// Assign an IPv4 address to an interface.  Returns Ok if already assigned.
pub fn addr_add_ipv4(ifname: &str, addr: Ipv4Addr, prefix: u8) -> io::Result<()> {
    let idx = if_index_of(ifname)?;
    let addr_b = addr.octets();

    // Compute broadcast: addr | ~mask
    let mask: u32 = if prefix == 0 {
        0
    } else {
        !0u32 << (32 - prefix)
    };
    let bcast = u32::from(addr) | !mask;
    let bcast_b = bcast.to_be_bytes();

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWADDR,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK,
    );
    push_ifaddrmsg(
        &mut buf,
        libc::AF_INET as u8,
        prefix,
        RT_SCOPE_UNIVERSE,
        idx,
    );
    buf.rta(IFA_LOCAL, &addr_b);
    buf.rta(IFA_ADDRESS, &addr_b);
    buf.rta(IFA_BROADCAST, &bcast_b);
    nl_request(&mut buf, true)
}

/// Assign an IPv6 address to an interface.  Returns Ok if already assigned.
///
/// When `nodad` is true the kernel skips Duplicate Address Detection, which
/// avoids ~1 s of tentative-state first-packet loss for deterministic ULA addresses.
pub fn addr_add_ipv6(ifname: &str, addr: Ipv6Addr, prefix: u8, nodad: bool) -> io::Result<()> {
    let idx = if_index_of(ifname)?;
    let addr_b = addr.octets();

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWADDR,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK,
    );
    push_ifaddrmsg(
        &mut buf,
        libc::AF_INET6 as u8,
        prefix,
        RT_SCOPE_UNIVERSE,
        idx,
    );
    buf.rta(IFA_ADDRESS, &addr_b);
    buf.rta(IFA_LOCAL, &addr_b);
    if nodad {
        buf.rta(IFA_FLAGS, &IFA_F_NODAD.to_le_bytes());
    }
    nl_request(&mut buf, true)
}

/// Attach an interface to a bridge using SIOCBRADDIF.
pub fn link_set_master(ifname: &str, master: &str) -> io::Result<()> {
    let slave_idx = if_index_of(ifname)?;
    let mut req = IfreqIdx {
        ifr_name: [0u8; 16],
        ifr_ifindex: slave_idx,
        _pad: [0u8; 18],
    };
    copy_ifname(&mut req.ifr_name, master)?;

    let fd = udp_sock()?;
    let ret = unsafe { libc::ioctl(fd, SIOCBRADDIF as _, &req as *const IfreqIdx) };
    let err = io::Error::last_os_error();
    unsafe { libc::close(fd) };
    if ret < 0 {
        return Err(err);
    }
    Ok(())
}

/// Create a veth pair `(host_name, peer_name)` using RTM_NEWLINK.
pub fn create_veth(host_name: &str, peer_name: &str) -> io::Result<()> {
    let mut host_name_b = host_name.as_bytes().to_vec();
    host_name_b.push(0);
    let mut peer_name_b = peer_name.as_bytes().to_vec();
    peer_name_b.push(0);

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWLINK,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
    );
    push_ifinfomsg(&mut buf, 0, 0, 0);
    buf.rta(IFLA_IFNAME, &host_name_b);

    let li = buf.rta_begin_nested(IFLA_LINKINFO);
    buf.rta(IFLA_INFO_KIND, b"veth\0");

    let id = buf.rta_begin_nested(IFLA_INFO_DATA);
    let peer = buf.rta_begin_nested(VETH_INFO_PEER);
    // peer ifinfomsg (16 zero bytes = all-default)
    push_ifinfomsg(&mut buf, 0, 0, 0);
    buf.rta(IFLA_IFNAME, &peer_name_b);
    buf.rta_end_nested(peer);
    buf.rta_end_nested(id);
    buf.rta_end_nested(li);

    nl_request(&mut buf, false)
}

/// Move an interface into a network namespace (by fd), optionally renaming it atomically.
pub fn link_move_to_netns(
    ifname: &str,
    netns_fd: RawFd,
    rename_to: Option<&str>,
) -> io::Result<()> {
    let idx = if_index_of(ifname)?;

    let mut buf = NlBuf::new();
    push_nlmsghdr(&mut buf, libc::RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK);
    push_ifinfomsg(&mut buf, idx, 0, 0);
    buf.rta(IFLA_NET_NS_FD, &(netns_fd as u32).to_le_bytes());
    if let Some(new_name) = rename_to {
        let mut nb = new_name.as_bytes().to_vec();
        nb.push(0);
        buf.rta(IFLA_IFNAME, &nb);
    }
    nl_request(&mut buf, false)
}

/// Delete an interface by name using RTM_DELLINK.
pub fn link_del(ifname: &str) -> io::Result<()> {
    let idx = if_index_of(ifname)?;
    let mut buf = NlBuf::new();
    push_nlmsghdr(&mut buf, libc::RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK);
    push_ifinfomsg(&mut buf, idx, 0, 0);
    nl_request(&mut buf, false)
}

/// Add an IPv4 default route via `gw` on `dev`.  Returns Ok if already present.
pub fn route_add_default_ipv4(gw: Ipv4Addr, dev: &str) -> io::Result<()> {
    let idx = if_index_of(dev)?;
    let gw_b = gw.octets();

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWROUTE,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK,
    );
    push_rtmsg(
        &mut buf,
        libc::AF_INET as u8,
        0,
        libc::RT_TABLE_MAIN,
        RTPROT_STATIC,
        RT_SCOPE_UNIVERSE,
        RTN_UNICAST,
    );
    buf.rta(RTA_GATEWAY, &gw_b);
    buf.rta(RTA_OIF, &(idx as u32).to_le_bytes());
    nl_request(&mut buf, true)
}

/// Add an IPv6 default route via `gw` on `dev`.  Returns Ok if already present.
pub fn route_add_default_ipv6(gw: Ipv6Addr, dev: &str) -> io::Result<()> {
    let idx = if_index_of(dev)?;
    let gw_b = gw.octets();

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWROUTE,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK,
    );
    push_rtmsg(
        &mut buf,
        libc::AF_INET6 as u8,
        0,
        libc::RT_TABLE_MAIN,
        RTPROT_STATIC,
        RT_SCOPE_UNIVERSE,
        RTN_UNICAST,
    );
    buf.rta(RTA_GATEWAY, &gw_b);
    buf.rta(RTA_OIF, &(idx as u32).to_le_bytes());
    nl_request(&mut buf, true)
}

/// Add a static NDP neighbour entry (IPv6, NUD_STALE).  Returns Ok if already present.
pub fn neigh_add_ipv6(dev: &str, ip: Ipv6Addr, mac: &[u8; 6]) -> io::Result<()> {
    let idx = if_index_of(dev)?;

    let mut buf = NlBuf::new();
    push_nlmsghdr(
        &mut buf,
        libc::RTM_NEWNEIGH,
        NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK,
    );
    push_ndmsg(&mut buf, libc::AF_INET6 as u8, idx, NUD_STALE);
    buf.rta(NDA_DST, &ip.octets());
    buf.rta(NDA_LLADDR, mac);
    nl_request(&mut buf, true)
}

/// Create a named network namespace at `/run/netns/<name>`.
///
/// Spawns a thread that calls `unshare(CLONE_NEWNET)` and then bind-mounts
/// `/proc/thread-self/ns/net` onto the file.  Returns Ok if the namespace
/// already exists.
pub fn netns_create(name: &str) -> io::Result<()> {
    let path = format!("/run/netns/{name}");
    std::fs::create_dir_all("/run/netns")?;

    // Try to create the bind-mount target file exclusively.
    let cpath = CString::new(path.as_bytes())
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "netns name contains NUL"))?;
    let fd = unsafe {
        libc::open(
            cpath.as_ptr(),
            libc::O_RDONLY | libc::O_CREAT | libc::O_EXCL | libc::O_CLOEXEC,
            0o444_u32,
        )
    };
    if fd < 0 {
        let e = io::Error::last_os_error();
        if e.raw_os_error() == Some(libc::EEXIST) {
            return Ok(());
        }
        return Err(e);
    }
    unsafe { libc::close(fd) };

    // Spawn a thread to create a new net namespace and bind-mount it.
    let path_clone = path.clone();
    let result = std::thread::spawn(move || -> io::Result<()> {
        let ret = unsafe { libc::unshare(libc::CLONE_NEWNET) };
        if ret < 0 {
            return Err(io::Error::last_os_error());
        }

        let src = CString::new("/proc/thread-self/ns/net").unwrap();
        let dst = CString::new(path_clone.as_bytes())
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "bad path"))?;
        let ret = unsafe {
            libc::mount(
                src.as_ptr(),
                dst.as_ptr(),
                std::ptr::null(),
                libc::MS_BIND,
                std::ptr::null(),
            )
        };
        if ret < 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    })
    .join()
    .map_err(|_| io::Error::other("netns_create thread panicked"))?;

    if result.is_err() {
        let _ = std::fs::remove_file(&path);
    }
    result
}

/// Run a closure inside a named network namespace.
///
/// Opens `/run/netns/<name>`, spawns a thread, calls `setns` to enter the
/// namespace, then executes `f`.  The calling thread's namespace is unaffected.
pub fn in_netns<T, F>(netns_path: &str, f: F) -> io::Result<T>
where
    T: Send + 'static,
    F: FnOnce() -> io::Result<T> + Send + 'static,
{
    let cpath = CString::new(netns_path.as_bytes())
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contains NUL"))?;
    let fd = unsafe { libc::open(cpath.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC) };
    if fd < 0 {
        return Err(io::Error::last_os_error());
    }

    std::thread::spawn(move || -> io::Result<T> {
        let ret = unsafe { libc::setns(fd, libc::CLONE_NEWNET) };
        unsafe { libc::close(fd) };
        if ret < 0 {
            return Err(io::Error::last_os_error());
        }
        f()
    })
    .join()
    .map_err(|_| io::Error::other("in_netns thread panicked"))?
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn nlbuf_pad() {
        let mut b = NlBuf::new();
        b.push_u8(1);
        b.push_u8(2);
        b.push_u8(3);
        b.pad_to_4();
        assert_eq!(b.0.len(), 4);
        assert_eq!(b.0[3], 0);
    }

    #[test]
    fn nlbuf_rta_roundtrip() {
        let mut b = NlBuf::new();
        // Append a dummy nlmsghdr placeholder first so offsets look realistic.
        for _ in 0..16 {
            b.push_u8(0);
        }
        let data = [192u8, 168, 1, 1];
        b.rta(IFA_LOCAL, &data);
        // rta_len = 4 + 4 = 8, no padding needed
        assert_eq!(&b.0[16..18], &(8u16).to_le_bytes());
        assert_eq!(&b.0[18..20], &IFA_LOCAL.to_le_bytes());
        assert_eq!(&b.0[20..24], &data);
    }

    #[test]
    fn nlbuf_nested_rta() {
        let mut b = NlBuf::new();
        let start = b.rta_begin_nested(IFLA_LINKINFO);
        b.rta(IFLA_INFO_KIND, b"veth\0");
        b.rta_end_nested(start);
        // The length at `start` must equal the total bytes written from start.
        let len = u16::from_le_bytes([b.0[start], b.0[start + 1]]) as usize;
        assert_eq!(len, b.0.len() - start);
    }
}