Skip to main content

shadow_rs/host/network/
namespace.rs

1use std::cell::{Cell, Ref, RefCell};
2use std::net::{Ipv4Addr, SocketAddrV4};
3use std::ops::{Deref, DerefMut};
4use std::sync::Arc;
5
6use atomic_refcell::AtomicRefCell;
7
8use crate::core::configuration::QDiscMode;
9use crate::core::worker::Worker;
10use crate::host::descriptor::socket::abstract_unix_ns::AbstractUnixNamespace;
11use crate::host::descriptor::socket::inet::InetSocket;
12use crate::host::network::interface::{NetworkInterface, PcapOptions};
13use crate::network::PacketDevice;
14use crate::network::packet::IanaProtocol;
15
16// The start of our random port range in host order, used if application doesn't
17// specify the port it wants to bind to, and for client connections.
18const MIN_RANDOM_PORT: u16 = 10000;
19
20/// Represents a network namespace.
21///
22/// Can be thought of as roughly equivalent to a Linux `struct net`. Shadow doesn't support multiple
23/// network namespaces, but this `NetworkNamespace` allows us to consolidate the host's networking
24/// objects, and hopefully might make it easier to support multiple network namespaces if we want to
25/// in the future.
26pub struct NetworkNamespace {
27    // map abstract socket addresses to unix sockets
28    pub unix: Arc<AtomicRefCell<AbstractUnixNamespace>>,
29
30    pub localhost: RefCell<NetworkInterface>,
31    pub internet: RefCell<NetworkInterface>,
32
33    pub default_ip: Ipv4Addr,
34
35    // used for debugging to make sure we've cleaned up before being dropped
36    has_run_cleanup: Cell<bool>,
37}
38
39impl NetworkNamespace {
40    pub fn new(public_ip: Ipv4Addr, pcap: Option<PcapOptions>, qdisc: QDiscMode) -> Self {
41        let localhost = NetworkInterface::new("lo", Ipv4Addr::LOCALHOST, pcap.clone(), qdisc);
42
43        let internet = NetworkInterface::new("eth0", public_ip, pcap, qdisc);
44
45        Self {
46            unix: Arc::new(AtomicRefCell::new(AbstractUnixNamespace::new())),
47            localhost: RefCell::new(localhost),
48            internet: RefCell::new(internet),
49            default_ip: public_ip,
50            has_run_cleanup: Cell::new(false),
51        }
52    }
53
54    /// Clean up the network namespace. This should be called while `Worker` has the active host
55    /// set.
56    pub fn cleanup(&self) {
57        assert!(!self.has_run_cleanup.get());
58
59        // we need to unref all sockets and free them before we drop the host, otherwise they'll try
60        // to access the global host and panic since there is no host
61        self.localhost.borrow().remove_all_sockets();
62        self.internet.borrow().remove_all_sockets();
63
64        self.has_run_cleanup.set(true);
65    }
66
67    /// Returns `None` if there is no such interface.
68    #[track_caller]
69    pub fn interface_borrow(
70        &self,
71        addr: Ipv4Addr,
72    ) -> Option<impl Deref<Target = NetworkInterface> + '_> {
73        // Notes:
74        // - The `is_loopback` matches all loopback addresses, but shadow will only work correctly
75        //   with 127.0.0.1. Using any other loopback address will lead to problems.
76        // - If the address is 0.0.0.0, we return the `internet` interface. This is not ideal if a
77        //   socket bound to 0.0.0.0 is trying to send a localhost packet and uses this method to
78        //   get the network interface, since the packet will be sent on the internet interface
79        //   instead of loopback. It's not clear if this will lead to bugs.
80        if addr.is_loopback() {
81            Some(self.localhost.borrow())
82        } else if addr == self.default_ip || addr.is_unspecified() {
83            Some(self.internet.borrow())
84        } else {
85            None
86        }
87    }
88
89    /// Returns `None` if there is no such interface.
90    #[track_caller]
91    pub fn interface_borrow_mut(
92        &self,
93        addr: Ipv4Addr,
94    ) -> Option<impl DerefMut<Target = NetworkInterface> + '_> {
95        // Same notes as for [`Self::interface_borrow`].
96        if addr.is_loopback() {
97            Some(self.localhost.borrow_mut())
98        } else if addr == self.default_ip || addr.is_unspecified() {
99            Some(self.internet.borrow_mut())
100        } else {
101            None
102        }
103    }
104
105    /// Returns `None` if there is no such interface.
106    #[track_caller]
107    pub fn interface_borrow_as_packet_device(
108        &self,
109        addr: Ipv4Addr,
110    ) -> Option<Ref<'_, dyn PacketDevice>> {
111        // Same notes as for [`Self::interface_borrow`].
112        if addr.is_loopback() {
113            Some(self.localhost.borrow())
114        } else if addr == self.default_ip || addr.is_unspecified() {
115            Some(self.internet.borrow())
116        } else {
117            None
118        }
119    }
120
121    pub fn is_addr_in_use(
122        &self,
123        protocol_type: IanaProtocol,
124        src: SocketAddrV4,
125        dst: SocketAddrV4,
126    ) -> Result<bool, NoInterface> {
127        if src.ip().is_unspecified() {
128            Ok(self
129                .localhost
130                .borrow()
131                .is_addr_in_use(protocol_type, src.port(), dst)
132                || self
133                    .internet
134                    .borrow()
135                    .is_addr_in_use(protocol_type, src.port(), dst))
136        } else {
137            match self.interface_borrow(*src.ip()) {
138                Some(i) => Ok(i.is_addr_in_use(protocol_type, src.port(), dst)),
139                None => Err(NoInterface),
140            }
141        }
142    }
143
144    /// Returns a random port in host byte order.
145    pub fn get_random_free_port(
146        &self,
147        protocol_type: IanaProtocol,
148        interface_ip: Ipv4Addr,
149        peer: SocketAddrV4,
150        mut rng: impl rand::RngExt,
151    ) -> Option<u16> {
152        // we need a random port that is free everywhere we need it to be.
153        // we have two modes here: first we just try grabbing a random port until we
154        // get a free one. if we cannot find one fast enough, then as a fallback we
155        // do an inefficient linear search that is guaranteed to succeed or fail.
156
157        // if choosing randomly doesn't succeed within 10 tries, then we have already
158        // allocated a lot of ports (>90% on average). then we fall back to linear search.
159        for _ in 0..10 {
160            let random_port = rng.random_range(MIN_RANDOM_PORT..=u16::MAX);
161
162            // `is_addr_in_use` will check all interfaces in the case of INADDR_ANY
163            let specific_in_use = self
164                .is_addr_in_use(
165                    protocol_type,
166                    SocketAddrV4::new(interface_ip, random_port),
167                    peer,
168                )
169                .unwrap_or(true);
170            let generic_in_use = self
171                .is_addr_in_use(
172                    protocol_type,
173                    SocketAddrV4::new(interface_ip, random_port),
174                    SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0),
175                )
176                .unwrap_or(true);
177            if !specific_in_use && !generic_in_use {
178                return Some(random_port);
179            }
180        }
181
182        // now if we tried too many times and still don't have a port, fall back
183        // to a linear search to make sure we get a free port if we have one.
184        // but start from a random port instead of the min.
185        let start = rng.random_range(MIN_RANDOM_PORT..=u16::MAX);
186        for port in (start..=u16::MAX).chain(MIN_RANDOM_PORT..start) {
187            let specific_in_use = self
188                .is_addr_in_use(protocol_type, SocketAddrV4::new(interface_ip, port), peer)
189                .unwrap_or(true);
190            let generic_in_use = self
191                .is_addr_in_use(
192                    protocol_type,
193                    SocketAddrV4::new(interface_ip, port),
194                    SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0),
195                )
196                .unwrap_or(true);
197            if !specific_in_use && !generic_in_use {
198                return Some(port);
199            }
200        }
201
202        log::warn!("unable to find free ephemeral port for {protocol_type:?} peer {peer}");
203        None
204    }
205
206    /// Associate the socket with any applicable network interfaces. The socket will be
207    /// automatically disassociated when the returned handle is dropped.
208    ///
209    /// # Safety
210    ///
211    /// Pointer args must be safely dereferenceable.
212    pub unsafe fn associate_interface(
213        &self,
214        socket: &InetSocket,
215        protocol: IanaProtocol,
216        bind_addr: SocketAddrV4,
217        peer_addr: SocketAddrV4,
218    ) -> AssociationHandle {
219        if bind_addr.ip().is_unspecified() {
220            // need to associate all interfaces
221            self.localhost
222                .borrow()
223                .associate(socket, protocol, bind_addr.port(), peer_addr);
224            self.internet
225                .borrow()
226                .associate(socket, protocol, bind_addr.port(), peer_addr);
227        } else {
228            // TODO: return error if interface does not exist
229            if let Some(iface) = self.interface_borrow(*bind_addr.ip()) {
230                iface.associate(socket, protocol, bind_addr.port(), peer_addr);
231            }
232        }
233
234        AssociationHandle {
235            protocol,
236            local_addr: bind_addr,
237            remote_addr: peer_addr,
238        }
239    }
240
241    /// Disassociate the socket associated using the local and remote addresses from all network
242    /// interfaces.
243    ///
244    /// Is only public so that it can be called from `host_disassociateInterface`. Normally this
245    /// should only be called from the [`AssociationHandle`].
246    pub fn disassociate_interface(
247        &self,
248        protocol: IanaProtocol,
249        bind_addr: SocketAddrV4,
250        peer_addr: SocketAddrV4,
251    ) {
252        if bind_addr.ip().is_unspecified() {
253            // need to disassociate all interfaces
254            self.localhost
255                .borrow()
256                .disassociate(protocol, bind_addr.port(), peer_addr);
257
258            self.internet
259                .borrow()
260                .disassociate(protocol, bind_addr.port(), peer_addr);
261        } else {
262            // TODO: return error if interface does not exist
263            if let Some(iface) = self.interface_borrow(*bind_addr.ip()) {
264                iface.disassociate(protocol, bind_addr.port(), peer_addr);
265            }
266        }
267    }
268}
269
270impl std::ops::Drop for NetworkNamespace {
271    fn drop(&mut self) {
272        if !self.has_run_cleanup.get() && !std::thread::panicking() {
273            warn_and_debug_panic!("Dropped the network namespace before it has been cleaned up");
274        }
275    }
276}
277
278#[derive(Copy, Clone, Debug, PartialEq, Eq)]
279pub struct NoInterface;
280
281impl std::fmt::Display for NoInterface {
282    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
283        write!(f, "No interface available")
284    }
285}
286
287impl std::error::Error for NoInterface {}
288
289/// A handle for a socket association with a network interface(s).
290///
291/// The network association will be dissolved when this handle is dropped (similar to
292/// [`callback_queue::Handle`](crate::utility::callback_queue::Handle)).
293#[derive(Debug)]
294pub struct AssociationHandle {
295    protocol: IanaProtocol,
296    local_addr: SocketAddrV4,
297    remote_addr: SocketAddrV4,
298}
299
300impl AssociationHandle {
301    pub fn local_addr(&self) -> SocketAddrV4 {
302        self.local_addr
303    }
304
305    pub fn remote_addr(&self) -> SocketAddrV4 {
306        self.remote_addr
307    }
308}
309
310impl std::ops::Drop for AssociationHandle {
311    fn drop(&mut self) {
312        Worker::with_active_host(|host| {
313            host.network_namespace_borrow().disassociate_interface(
314                self.protocol,
315                self.local_addr,
316                self.remote_addr,
317            );
318        })
319        .unwrap();
320    }
321}