blackship 0.1.4

A FreeBSD jail manager with state machine control
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
//! FreeBSD network ioctl syscalls
//!
//! Native ioctl operations for network interface management, replacing ifconfig commands.

use crate::error::{Error, Result};
use std::ffi::CString;
use std::os::unix::io::AsRawFd;

/// Safely copy interface name into fixed-size buffer
/// Returns error if name is too long (max 15 chars + null terminator)
fn copy_ifname(dest: &mut [libc::c_char; libc::IF_NAMESIZE], name: &str) -> Result<()> {
    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();

    if name_bytes.len() > libc::IF_NAMESIZE {
        return Err(Error::Network(format!(
            "Interface name too long: {} (max {} chars)",
            name,
            libc::IF_NAMESIZE - 1
        )));
    }

    dest[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    Ok(())
}

/// Create a network interface (epair, bridge, etc.)
pub fn create_interface(iftype: &str, name: Option<&str>) -> Result<String> {
    use std::net::UdpSocket;

    // Create a socket for ioctl operations
    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // SIOCIFCREATE structure for FreeBSD
    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_data: *mut libc::c_void,
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    // Set interface type
    let type_cstr = CString::new(iftype)
        .map_err(|e| Error::Network(format!("Invalid interface type: {}", e)))?;

    if let Some(n) = name {
        let name_cstr = CString::new(n)
            .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
        let name_bytes = name_cstr.as_bytes_with_nul();
        req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
            std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
        });
    } else {
        let type_bytes = type_cstr.as_bytes_with_nul();
        req.ifr_name[..type_bytes.len()].copy_from_slice(unsafe {
            std::slice::from_raw_parts(type_bytes.as_ptr() as *const i8, type_bytes.len())
        });
    }

    // SIOCIFCREATE ioctl
    const SIOCIFCREATE: libc::c_ulong = 0xc020697a;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCIFCREATE, &mut req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to create interface: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Extract the created interface name
    let name_len = req
        .ifr_name
        .iter()
        .position(|&c| c == 0)
        .unwrap_or(libc::IF_NAMESIZE);
    let name_bytes: Vec<u8> = req.ifr_name[..name_len]
        .iter()
        .map(|&c| c as u8)
        .collect();

    String::from_utf8(name_bytes)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))
}

/// Destroy a network interface
pub fn destroy_interface(name: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_data: *mut libc::c_void,
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();
    req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    // SIOCIFDESTROY ioctl
    const SIOCIFDESTROY: libc::c_ulong = 0x80206979;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCIFDESTROY, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to destroy interface: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Bring an interface up or down
pub fn set_interface_up(name: &str, up: bool) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_flags: libc::c_short,
        _padding: [u8; 24],
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();
    req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    // Get current flags
    const SIOCGIFFLAGS: libc::c_ulong = 0xc0206911;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGIFFLAGS, &mut req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to get interface flags: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Modify flags
    const IFF_UP: libc::c_short = 0x1;
    if up {
        req.ifr_flags |= IFF_UP;
    } else {
        req.ifr_flags &= !IFF_UP;
    }

    // Set new flags
    const SIOCSIFFLAGS: libc::c_ulong = 0x80206910;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFFLAGS, &req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to set interface flags: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Rename a network interface
pub fn rename_interface(old_name: &str, new_name: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_data: *mut libc::c_void,
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    let old_cstr = CString::new(old_name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let old_bytes = old_cstr.as_bytes_with_nul();
    req.ifr_name[..old_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(old_bytes.as_ptr() as *const i8, old_bytes.len())
    });

    let new_cstr = CString::new(new_name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;

    req.ifr_data = new_cstr.as_ptr() as *mut libc::c_void;

    // SIOCSIFNAME ioctl
    const SIOCSIFNAME: libc::c_ulong = 0x80206928;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFNAME, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to rename interface: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Set MAC address on an interface
pub fn set_mac_address(name: &str, mac: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // Parse MAC address
    let mac_parts: Vec<&str> = mac.split(':').collect();
    if mac_parts.len() != 6 {
        return Err(Error::Network(format!("Invalid MAC address format: {}", mac)));
    }

    let mut mac_bytes = [0u8; 6];
    for (i, part) in mac_parts.iter().enumerate() {
        mac_bytes[i] = u8::from_str_radix(part, 16)
            .map_err(|e| Error::Network(format!("Invalid MAC address: {}", e)))?;
    }

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_addr: libc::sockaddr,
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();
    req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    // Set up sockaddr_dl for MAC address
    req.ifr_addr.sa_family = libc::AF_LINK as u8;
    req.ifr_addr.sa_len = 20;
    unsafe {
        std::ptr::copy_nonoverlapping(
            mac_bytes.as_ptr(),
            req.ifr_addr.sa_data.as_mut_ptr() as *mut u8,
            6,
        );
    }

    // SIOCSIFLLADDR ioctl
    const SIOCSIFLLADDR: libc::c_ulong = 0x8020693c;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFLLADDR, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to set MAC address: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Move interface to VNET jail
pub fn move_to_vnet(name: &str, jid: i32) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_jid: libc::c_int,
        _padding: [u8; 20],
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();
    req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    req.ifr_jid = jid;

    // SIOCSIFVNET ioctl (FreeBSD-specific)
    const SIOCSIFVNET: libc::c_ulong = 0xc020695a;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFVNET, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to move interface to VNET: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Add a member interface to a bridge
pub fn bridge_add_member(bridge: &str, member: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfBReq {
        ifbr_ifsname: [libc::c_char; libc::IF_NAMESIZE],
    }

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_data: *mut libc::c_void,
    }

    let mut breq: IfBReq = unsafe { std::mem::zeroed() };
    let mut req: IfReq = unsafe { std::mem::zeroed() };

    // Set bridge name
    let bridge_cstr = CString::new(bridge)
        .map_err(|e| Error::Network(format!("Invalid bridge name: {}", e)))?;
    let bridge_bytes = bridge_cstr.as_bytes_with_nul();
    req.ifr_name[..bridge_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(bridge_bytes.as_ptr() as *const i8, bridge_bytes.len())
    });

    // Set member interface name
    let member_cstr = CString::new(member)
        .map_err(|e| Error::Network(format!("Invalid member name: {}", e)))?;
    let member_bytes = member_cstr.as_bytes_with_nul();
    breq.ifbr_ifsname[..member_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(member_bytes.as_ptr() as *const i8, member_bytes.len())
    });

    req.ifr_data = &mut breq as *mut _ as *mut libc::c_void;

    // SIOCBRDGADD ioctl
    const SIOCBRDGADD: libc::c_ulong = 0x8028695c;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCBRDGADD, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to add member to bridge: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Remove a member interface from a bridge
pub fn bridge_delete_member(bridge: &str, member: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfBReq {
        ifbr_ifsname: [libc::c_char; libc::IF_NAMESIZE],
    }

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_data: *mut libc::c_void,
    }

    let mut breq: IfBReq = unsafe { std::mem::zeroed() };
    let mut req: IfReq = unsafe { std::mem::zeroed() };

    // Set bridge name
    let bridge_cstr = CString::new(bridge)
        .map_err(|e| Error::Network(format!("Invalid bridge name: {}", e)))?;
    let bridge_bytes = bridge_cstr.as_bytes_with_nul();
    req.ifr_name[..bridge_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(bridge_bytes.as_ptr() as *const i8, bridge_bytes.len())
    });

    // Set member interface name
    let member_cstr = CString::new(member)
        .map_err(|e| Error::Network(format!("Invalid member name: {}", e)))?;
    let member_bytes = member_cstr.as_bytes_with_nul();
    breq.ifbr_ifsname[..member_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(member_bytes.as_ptr() as *const i8, member_bytes.len())
    });

    req.ifr_data = &mut breq as *mut _ as *mut libc::c_void;

    // SIOCBRDGDEL ioctl
    const SIOCBRDGDEL: libc::c_ulong = 0x8028695d;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCBRDGDEL, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to remove member from bridge: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Check if an interface exists
pub fn interface_exists(name: &str) -> Result<bool> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_flags: libc::c_short,
        _padding: [u8; 24],
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };

    // Validate name length - if too long, interface definitely doesn't exist
    if name.len() >= libc::IF_NAMESIZE {
        return Ok(false);
    }
    copy_ifname(&mut req.ifr_name, name)?;

    // Try to get interface flags - if it succeeds, interface exists
    const SIOCGIFFLAGS: libc::c_ulong = 0xc0206911;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGIFFLAGS, &mut req) };

    Ok(result >= 0)
}

/// Set IPv4 address on an interface
///
/// Supports CIDR notation like "10.0.0.1/24" or plain IP like "10.0.0.1"
/// When prefix length is specified, also sets the netmask.
pub fn set_ipv4_address(name: &str, addr: &str) -> Result<()> {
    use std::net::{Ipv4Addr, UdpSocket};

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // Parse address with optional CIDR notation
    let (ip_str, prefix_len) = if let Some(slash_pos) = addr.find('/') {
        let ip = &addr[..slash_pos];
        let prefix: u8 = addr[slash_pos + 1..]
            .parse()
            .map_err(|_| Error::Network(format!("Invalid prefix length in: {}", addr)))?;
        (ip, Some(prefix))
    } else {
        (addr, None)
    };

    let ip: Ipv4Addr = ip_str
        .parse()
        .map_err(|_| Error::Network(format!("Invalid IPv4 address: {}", ip_str)))?;

    #[repr(C)]
    struct IfReqAddr {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_addr: libc::sockaddr_in,
    }

    let mut req: IfReqAddr = unsafe { std::mem::zeroed() };

    let name_cstr = CString::new(name)
        .map_err(|e| Error::Network(format!("Invalid interface name: {}", e)))?;
    let name_bytes = name_cstr.as_bytes_with_nul();
    req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
        std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
    });

    // Set up sockaddr_in
    req.ifr_addr.sin_len = std::mem::size_of::<libc::sockaddr_in>() as u8;
    req.ifr_addr.sin_family = libc::AF_INET as u8;
    req.ifr_addr.sin_addr.s_addr = u32::from_be_bytes(ip.octets()).to_be();

    // SIOCSIFADDR ioctl
    const SIOCSIFADDR: libc::c_ulong = 0x8020690c;

    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFADDR, &req) };

    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to set IP address: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Set netmask if prefix length was specified
    if let Some(prefix) = prefix_len {
        let netmask = if prefix == 0 {
            0u32
        } else {
            !0u32 << (32 - prefix)
        };

        let mut mask_req: IfReqAddr = unsafe { std::mem::zeroed() };
        mask_req.ifr_name[..name_bytes.len()].copy_from_slice(unsafe {
            std::slice::from_raw_parts(name_bytes.as_ptr() as *const i8, name_bytes.len())
        });
        mask_req.ifr_addr.sin_len = std::mem::size_of::<libc::sockaddr_in>() as u8;
        mask_req.ifr_addr.sin_family = libc::AF_INET as u8;
        mask_req.ifr_addr.sin_addr.s_addr = netmask.to_be();

        // SIOCSIFNETMASK ioctl
        const SIOCSIFNETMASK: libc::c_ulong = 0x80206916;

        let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFNETMASK, &mask_req) };

        if result < 0 {
            return Err(Error::Network(format!(
                "Failed to set netmask: {}",
                std::io::Error::last_os_error()
            )));
        }
    }

    Ok(())
}

/// List all bridge interfaces on the system
///
/// Uses if_nameindex(3) and filters for interfaces matching "bridge*" pattern
pub fn list_bridges() -> Result<Vec<String>> {
    let mut bridges = Vec::new();

    // Get list of all network interfaces
    let if_list = unsafe { libc::if_nameindex() };
    if if_list.is_null() {
        return Err(Error::Network(format!(
            "Failed to get interface list: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Iterate through interfaces
    let mut i = 0;
    loop {
        let entry = unsafe { *if_list.add(i) };
        if entry.if_index == 0 || entry.if_name.is_null() {
            break;
        }

        let name = unsafe { std::ffi::CStr::from_ptr(entry.if_name) }
            .to_string_lossy()
            .into_owned();

        // Filter for bridge interfaces
        if name.starts_with("bridge") {
            bridges.push(name);
        }

        i += 1;
    }

    // Free the interface list
    unsafe { libc::if_freenameindex(if_list) };

    Ok(bridges)
}

/// Disable hardware VLAN filtering on an interface
///
/// Uses SIOCGIFCAP/SIOCSIFCAP ioctls to clear IFCAP_VLAN_HWFILTER flag.
/// Some NICs (especially Broadcom) have buggy VLAN hardware filtering.
pub fn disable_hwfilter(name: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    #[repr(C)]
    struct IfReq {
        ifr_name: [libc::c_char; libc::IF_NAMESIZE],
        ifr_curcap: libc::c_int,
        ifr_reqcap: libc::c_int,
        _padding: [u8; 16],
    }

    let mut req: IfReq = unsafe { std::mem::zeroed() };
    copy_ifname(&mut req.ifr_name, name)?;

    // Get current capabilities
    const SIOCGIFCAP: libc::c_ulong = 0xc020693f;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGIFCAP, &mut req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to get interface capabilities: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Clear VLAN hardware filter capability
    const IFCAP_VLAN_HWFILTER: libc::c_int = 1 << 17;
    req.ifr_reqcap = req.ifr_curcap & !IFCAP_VLAN_HWFILTER;

    // Set new capabilities
    const SIOCSIFCAP: libc::c_ulong = 0x80206937;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFCAP, &req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to disable hw VLAN filter: {}",
            std::io::Error::last_os_error()
        )));
    }

    // Also bring the interface up
    set_interface_up(name, true)?;

    Ok(())
}

/// Enable VLAN filtering on a bridge
///
/// Uses SIOCSDRVSPEC ioctl with BRDGSFLAGS command.
pub fn bridge_enable_vlan_filtering(bridge: &str) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // Bridge parameter structure
    #[repr(C)]
    struct IfBrParam {
        ifbrp_csize: u32,
    }

    // Driver-specific ioctl structure
    #[repr(C)]
    struct IfDrv {
        ifd_name: [libc::c_char; libc::IF_NAMESIZE],
        ifd_cmd: libc::c_ulong,
        ifd_len: libc::size_t,
        ifd_data: *mut libc::c_void,
    }

    // First get current flags
    let mut get_param = IfBrParam { ifbrp_csize: 0 };
    let mut get_req: IfDrv = unsafe { std::mem::zeroed() };
    copy_ifname(&mut get_req.ifd_name, bridge)?;

    const BRDGGFLT: libc::c_ulong = 36; // Get filtering flags
    get_req.ifd_cmd = BRDGGFLT;
    get_req.ifd_len = std::mem::size_of::<IfBrParam>();
    get_req.ifd_data = &mut get_param as *mut _ as *mut libc::c_void;

    const SIOCGDRVSPEC: libc::c_ulong = 0xc0286977;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGDRVSPEC, &mut get_req) };

    // Start with current flags or 0 if get failed
    let current_flags = if result >= 0 { get_param.ifbrp_csize } else { 0 };

    // Set VLAN filtering flag
    const IFBRF_VLANFILTER: u32 = 1;
    let mut set_param = IfBrParam {
        ifbrp_csize: current_flags | IFBRF_VLANFILTER,
    };

    let mut set_req: IfDrv = unsafe { std::mem::zeroed() };
    copy_ifname(&mut set_req.ifd_name, bridge)?;

    const BRDGSFLAGS: libc::c_ulong = 35; // Set filtering flags
    set_req.ifd_cmd = BRDGSFLAGS;
    set_req.ifd_len = std::mem::size_of::<IfBrParam>();
    set_req.ifd_data = &mut set_param as *mut _ as *mut libc::c_void;

    const SIOCSDRVSPEC: libc::c_ulong = 0x8028695e;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSDRVSPEC, &set_req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to enable VLAN filtering: {}",
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Set PVID (Port VLAN ID) on a bridge member interface
///
/// Uses SIOCSDRVSPEC ioctl with BRDGSIFPVID command.
pub fn bridge_set_pvid(bridge: &str, member: &str, pvid: u16) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // Bridge request structure with PVID
    #[repr(C)]
    struct IfBReq {
        ifbr_ifsname: [libc::c_char; libc::IF_NAMESIZE],
        ifbr_ifsflags: u32,
        ifbr_stpflags: u32,
        ifbr_path_cost: u32,
        ifbr_portno: u8,
        ifbr_priority: u8,
        ifbr_pvid: u16,
    }

    #[repr(C)]
    struct IfDrv {
        ifd_name: [libc::c_char; libc::IF_NAMESIZE],
        ifd_cmd: libc::c_ulong,
        ifd_len: libc::size_t,
        ifd_data: *mut libc::c_void,
    }

    let mut breq: IfBReq = unsafe { std::mem::zeroed() };
    copy_ifname(&mut breq.ifbr_ifsname, member)?;
    breq.ifbr_pvid = pvid;

    let mut req: IfDrv = unsafe { std::mem::zeroed() };
    copy_ifname(&mut req.ifd_name, bridge)?;

    const BRDGSIFPVID: libc::c_ulong = 31;
    req.ifd_cmd = BRDGSIFPVID;
    req.ifd_len = std::mem::size_of::<IfBReq>();
    req.ifd_data = &mut breq as *mut _ as *mut libc::c_void;

    const SIOCSDRVSPEC: libc::c_ulong = 0x8028695e;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSDRVSPEC, &req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to set PVID on {}: {}",
            member,
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// Set tagged VLANs on a bridge member (trunk port)
///
/// Uses SIOCSDRVSPEC ioctl with BRDGSIFVLANSET command.
/// The vlans slice contains VLAN IDs (1-4094) to tag.
pub fn bridge_set_tagged_vlans(bridge: &str, member: &str, vlans: &[u16]) -> Result<()> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // VLAN bitmap - 4096 bits = 512 bytes
    const BRVLAN_SETSIZE: usize = 4096;
    const BRVLAN_BYTES: usize = BRVLAN_SETSIZE / 8;

    #[repr(C)]
    struct IfBifVlanReq {
        bv_ifname: [libc::c_char; libc::IF_NAMESIZE],
        bv_op: u8,
        _padding: [u8; 3],
        bv_set: [u8; BRVLAN_BYTES],
    }

    #[repr(C)]
    struct IfDrv {
        ifd_name: [libc::c_char; libc::IF_NAMESIZE],
        ifd_cmd: libc::c_ulong,
        ifd_len: libc::size_t,
        ifd_data: *mut libc::c_void,
    }

    let mut vreq: IfBifVlanReq = unsafe { std::mem::zeroed() };
    copy_ifname(&mut vreq.bv_ifname, member)?;

    // Operation: SET (replace entire VLAN set)
    const BRDG_VLAN_OP_SET: u8 = 1;
    vreq.bv_op = BRDG_VLAN_OP_SET;

    // Build VLAN bitmap
    for &vlan in vlans {
        if vlan > 0 && vlan < 4095 {
            let byte_idx = vlan as usize / 8;
            let bit_idx = vlan as usize % 8;
            vreq.bv_set[byte_idx] |= 1 << bit_idx;
        }
    }

    let mut req: IfDrv = unsafe { std::mem::zeroed() };
    copy_ifname(&mut req.ifd_name, bridge)?;

    const BRDGSIFVLANSET: libc::c_ulong = 32;
    req.ifd_cmd = BRDGSIFVLANSET;
    req.ifd_len = std::mem::size_of::<IfBifVlanReq>();
    req.ifd_data = &mut vreq as *mut _ as *mut libc::c_void;

    const SIOCSDRVSPEC: libc::c_ulong = 0x8028695e;
    let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSDRVSPEC, &req) };
    if result < 0 {
        return Err(Error::Network(format!(
            "Failed to set tagged VLANs on {}: {}",
            member,
            std::io::Error::last_os_error()
        )));
    }

    Ok(())
}

/// List member interfaces of a bridge
///
/// Uses SIOCGDRVSPEC ioctl with BRDGGIFS command.
pub fn bridge_list_members(bridge: &str) -> Result<Vec<String>> {
    use std::net::UdpSocket;

    let sock = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| Error::Network(format!("Failed to create socket: {}", e)))?;

    // Bridge interface request
    #[repr(C)]
    #[derive(Clone, Copy)]
    struct IfBReq {
        ifbr_ifsname: [libc::c_char; libc::IF_NAMESIZE],
        ifbr_ifsflags: u32,
        ifbr_stpflags: u32,
        ifbr_path_cost: u32,
        ifbr_portno: u8,
        ifbr_priority: u8,
        ifbr_pvid: u16,
    }

    // Bridge interface config
    #[repr(C)]
    struct IfBifConf {
        ifbic_len: u32,
        ifbic_req: *mut IfBReq,
    }

    #[repr(C)]
    struct IfDrv {
        ifd_name: [libc::c_char; libc::IF_NAMESIZE],
        ifd_cmd: libc::c_ulong,
        ifd_len: libc::size_t,
        ifd_data: *mut libc::c_void,
    }

    // Start with space for 16 members, grow if needed
    let mut capacity: usize = 16;
    let mut members = Vec::new();

    loop {
        let mut buffer: Vec<IfBReq> = vec![unsafe { std::mem::zeroed() }; capacity];

        let mut bifc = IfBifConf {
            ifbic_len: (capacity * std::mem::size_of::<IfBReq>()) as u32,
            ifbic_req: buffer.as_mut_ptr(),
        };

        let mut req: IfDrv = unsafe { std::mem::zeroed() };
        copy_ifname(&mut req.ifd_name, bridge)?;

        const BRDGGIFS: libc::c_ulong = 6;
        req.ifd_cmd = BRDGGIFS;
        req.ifd_len = std::mem::size_of::<IfBifConf>();
        req.ifd_data = &mut bifc as *mut _ as *mut libc::c_void;

        const SIOCGDRVSPEC: libc::c_ulong = 0xc0286977;
        let result = unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGDRVSPEC, &mut req) };
        if result < 0 {
            let err = std::io::Error::last_os_error();
            // ENOMEM means we need more space
            if err.raw_os_error() == Some(libc::ENOMEM) {
                capacity *= 2;
                continue;
            }
            return Err(Error::Network(format!(
                "Failed to list bridge members: {}",
                err
            )));
        }

        // Parse results
        let count = bifc.ifbic_len as usize / std::mem::size_of::<IfBReq>();
        for entry in buffer.iter().take(count) {
            let name_len = entry
                .ifbr_ifsname
                .iter()
                .position(|&c| c == 0)
                .unwrap_or(libc::IF_NAMESIZE);
            let name_bytes: Vec<u8> = entry.ifbr_ifsname[..name_len]
                .iter()
                .map(|&c| c as u8)
                .collect();
            if let Ok(name) = String::from_utf8(name_bytes)
                && !name.is_empty()
            {
                members.push(name);
            }
        }

        break;
    }

    Ok(members)
}