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
//! This crate is a simple wrapper around the ioctls exposed by Linux.
//!
//! The goal of this crate is to make it easy to create networking bridges in Rust.
#[macro_use]
extern crate nix;
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};

extern crate libc;

use std::ffi::CString;

mod private {
    extern crate libc;
    /// The maximum length of an interface name.
    pub const IFNAMSIZ: usize = 16;
    pub const SIOCBRADDBR: u16 = 0x89a0;
    pub const SIOCBRDELBR: u16 = 0x89a1;
    pub const SIOCGIFINDEX: u16 = 0x8933;
    pub const SIOCBRADDIF: u16 = 0x89a2;
    pub const SIOCBRDELIF: u16 = 0x89a3;

    #[repr(C)]
    pub struct ifreq {
       pub ifrn_name: [libc::c_char; IFNAMSIZ],
       pub ifru_ivalue: u32,
    }

    ioctl_write_ptr_bad!(ioctl_addbr, SIOCBRADDBR, libc::c_char);
    ioctl_write_ptr_bad!(ioctl_delbr, SIOCBRDELBR, libc::c_char);
    ioctl_write_ptr_bad!(ioctl_ifindex, SIOCGIFINDEX, ifreq);
    ioctl_write_ptr_bad!(ioctl_addif, SIOCBRADDIF, ifreq);
    ioctl_write_ptr_bad!(ioctl_delif, SIOCBRDELIF, ifreq);
}
use crate::private::{ifreq, ioctl_addif, ioctl_delif, ioctl_ifindex, ioctl_delbr, ioctl_addbr};
pub use crate::private::IFNAMSIZ;

/// Builder pattern for constructing networking bridges.
///
/// # Example
/// 
/// Create a bridge named `hello_world_br` and attach two interfaces: `eth0` and `eth1`.
///
/// ```rust,no_run
///# use ::network_bridge::BridgeBuilder;
///   let result = BridgeBuilder::new("hello_world_br")
///                 .interface("eth0")
///                 .interface("eth1")
///                 .build();
/// ```
pub struct BridgeBuilder {
    name: String,
    interfaces: Vec<i32>,
}

impl BridgeBuilder {
    /// Start building a new bridge, setting its interface name.
    pub fn new(name: &str) -> BridgeBuilder {
        BridgeBuilder {
            name: name.to_string(),
            interfaces: Vec::new(),
        }
    }

    /// Override the name of the bridge.
    pub fn name(self, name: &str) -> BridgeBuilder {
        BridgeBuilder {
            name: name.to_string(),
            interfaces: self.interfaces,
        }
    }

    /// Attach an interface to the bridge.
    /// 
    /// Note that this will fail _silently_ if the interface name supplied cannot be converted into
    /// the appropriate interface index.
    pub fn interface(self, name: &str) -> BridgeBuilder {
        let idx = interface_id(name);
        if idx.is_ok() {
            BridgeBuilder {
                name: self.name,
                interfaces: {
                    let mut ifs = self.interfaces;
                    ifs.push(idx.unwrap());
                    ifs
                }
            }
        } else {
            self
        }
    }

    /// Remove an interface from the bridge.
    ///
    /// Note that this will fail _silently_ if the interface name supplied cannot be converted into
    /// the appropriate interface index.
    pub fn remove_interface(self, name: &str) -> BridgeBuilder {
        let idx = interface_id(name);
        if idx.is_ok() {
            BridgeBuilder {
                name: self.name,
                interfaces:
                    self.interfaces.into_iter()
                        .filter(|x| *x != idx.unwrap())
                        .collect()
                ,
            }
        } else {
            self
        }
    }

    /// Finalize the builder, creating the bridge and attaching any interfaces.
    pub fn build(self) -> Result<(), nix::Error> {
        create_bridge(&self.name)?;
        for i in self.interfaces {
            add_interface_to_bridge(i, &self.name)?;
        }

        Ok(())
    }
}

/// Create a network bridge using the interface name supplied.
pub fn create_bridge(name: &str) -> Result<i32, nix::Error> {
    /* Open a socket */
    let res = socket(AddressFamily::Unix,
                     SockType::Stream,
                     SockFlag::empty(),
                     None)?;

    /* use the SIOCBRADDRBR ioctl to add the bridge */
    let cstr = CString::new(name).unwrap();
    unsafe {
        ioctl_addbr(res, cstr.as_ptr())
    }
}

/// Delete an existing network bridge of the interface name supplied.
pub fn delete_bridge(name: &str) -> Result<i32, nix::Error> {
    /* Open a socket */
    let res = socket(AddressFamily::Unix,
                     SockType::Stream,
                     SockFlag::empty(),
                     None)?;

    /* use the SIOCBRDELBR ioctl to delete the bridge */
    let cstr = CString::new(name).unwrap();
    unsafe {
        ioctl_delbr(res, cstr.as_ptr())
    }
}

/// Converts an interface name into the identifier used by the kernel.
///
/// This can also be retrieved via sysfs, if mounted to /sys:
///
/// ```shell
/// $ cat /sys/class/net/eth0/ifindex
/// 1
/// ```
pub fn interface_id(interface: &str) -> Result<i32, nix::Error> {
    /* do some validation */
    if interface.len() == 0 || interface.len() >= IFNAMSIZ {
        return Err(nix::Error::from(nix::errno::Errno::EINVAL));
    }
    let length = interface.len();

    /* Open a socket */
    let sock = socket(AddressFamily::Unix,
                     SockType::Stream,
                     SockFlag::empty(),
                     None)?;

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

    /* create the ifreq structure */
    let ifr = ifreq {
        ifrn_name: [0; IFNAMSIZ],
        ifru_ivalue: 0,
    };

    let result = unsafe {
         /*
          * This is safe because length is guaranteed to be less than IFNAMSIZ,
          * and the two variables can never overlap 
          */
        std::ptr::copy_nonoverlapping(cstr.as_ptr(),
            ifr.ifrn_name.as_ptr() as *mut i8, length);
        /*
         * SIOCGIFINDEX doesn't care about the rest of the fields, so this
         * should be safe
         */
        ioctl_ifindex(sock, &ifr)
    };

    if result.is_err() {
        result
    } else {
        Ok(ifr.ifru_ivalue as i32)
    }
}

fn bridge_del_add_if(interface_id: i32, bridge: &str, add: bool)
    -> Result<i32, nix::Error> {
    /* validate bridge name */
    if bridge.len() == 0 || bridge.len() >= IFNAMSIZ {
        return Err(nix::Error::from(nix::errno::Errno::EINVAL));
    }
    let length = bridge.len();

    /* Open a socket */
    let sock = socket(AddressFamily::Unix,
                     SockType::Stream,
                     SockFlag::empty(),
                     None)?;

    let ifr = ifreq {
        ifrn_name: [0; IFNAMSIZ],
        ifru_ivalue: interface_id as u32,
    };

    let br_cstr = CString::new(bridge).unwrap();

    unsafe {
        /* copy the bridge name to the ifreq */
        std::ptr::copy_nonoverlapping(br_cstr.as_ptr(),
            ifr.ifrn_name.as_ptr() as *mut i8, length);

        if add {
            ioctl_addif(sock, &ifr)
        } else {
            ioctl_delif(sock, &ifr)
        }
    }
}

/// Attach an interface to a bridge.
///
/// The bridge must already exist.
pub fn add_interface_to_bridge(interface_id: i32, bridge: &str)
    -> Result<i32, nix::Error> {
    bridge_del_add_if(interface_id, bridge, true)
}

/// Remove an interface from a bridge.
///
/// The bridge must already exist and the interface must already be attached to the bridge.
pub fn delete_interface_from_bridge(interface_id: i32, bridge: &str)
    -> Result<i32, nix::Error> {
    bridge_del_add_if(interface_id, bridge, false)
}

#[cfg(test)]
mod tests {
    use crate::{create_bridge, delete_bridge, interface_id, add_interface_to_bridge};

    #[test]
    fn add_and_delete_bridge() {
        assert!(create_bridge("hello_br0").is_ok());
        assert!(delete_bridge("hello_br0").is_ok());
    }

    #[test]
    fn get_interface_id() {
        assert!(interface_id("eth0").is_ok());
    }

    #[test]
    fn test_adding_to_bridge() {
        assert!(create_bridge("hello_br1").is_ok());
        assert!(add_interface_to_bridge(interface_id("eth0").unwrap(),
                    "hello_br1").is_ok());
        assert!(delete_bridge("hello_br1").is_ok());
    }
}