dhcp_client/
udpsocket.rs

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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::num::NonZeroU64;
use std::os::fd::AsRawFd;

use dhcp_client_core::deps::UdpSocketProvider;
use {fidl_fuchsia_posix as fposix, fuchsia_async as fasync};

pub(crate) struct UdpSocket {
    inner: fasync::net::UdpSocket,
}

fn translate_io_error(e: std::io::Error) -> dhcp_client_core::deps::SocketError {
    use fposix::Errno as E;
    match e.raw_os_error().and_then(fposix::Errno::from_primitive) {
        None => dhcp_client_core::deps::SocketError::Other(e),
        Some(errno) => match errno {
            // ============
            // These errors are documented in `man 7 udp`.
            E::Econnrefused => dhcp_client_core::deps::SocketError::Other(e),

            // ============
            // These errors are documented in `man 7 ip`.
            E::Eagain | E::Ealready => panic!(
                "unexpected error {e:?}; nonblocking logic should be
                 handled by fuchsia_async UDP socket wrapper"
            ),
            E::Econnaborted => panic!("unexpected error {e:?}; not using stream sockets"),
            E::Ehostunreach => dhcp_client_core::deps::SocketError::HostUnreachable,
            E::Enetunreach => dhcp_client_core::deps::SocketError::NetworkUnreachable,
            E::Enobufs | E::Enomem => panic!("out of memory: {e:?}"),
            E::Eacces
            | E::Eaddrinuse
            | E::Eaddrnotavail
            | E::Einval
            | E::Eisconn
            | E::Emsgsize
            | E::Enoent
            | E::Enopkg
            | E::Enoprotoopt
            | E::Eopnotsupp
            | E::Enotconn
            | E::Eperm
            | E::Epipe
            | E::Esocktnosupport => dhcp_client_core::deps::SocketError::Other(e),

            // TODO(https://fxbug.dev/42077996): Revisit whether we should
            // actually panic on this error once we establish whether this error
            // is set for UDP sockets.
            E::Ehostdown => dhcp_client_core::deps::SocketError::Other(e),

            // ============
            // These errors are documented in `man bind`.
            E::Enotsock => {
                panic!("tried to perform socket operation with non-socket file descriptor: {:?}", e)
            }
            E::Ebadf => {
                panic!("tried to perform socket operation with bad file descriptor: {:?}", e)
            }

            // ============
            // These errors can be returned from `sendto()` at the socket layer.
            E::Eintr => panic!("got EINTR, should be handled by lower-level library: {:?}", e),
            E::Econnreset => panic!("got ECONNRESET, but we aren't using TCP: {:?}", e),

            // ============
            // The following errors aren't expected to be returned by any of the
            // socket operations we use.
            E::Esrch
            | E::Eio
            | E::E2Big
            | E::Enoexec
            | E::Echild
            | E::Enotblk
            | E::Ebusy
            | E::Eexist
            | E::Exdev
            | E::Enotdir
            | E::Eisdir
            | E::Enfile
            | E::Emfile
            | E::Enotty
            | E::Etxtbsy
            | E::Efbig
            | E::Enospc
            | E::Espipe
            | E::Erofs
            | E::Emlink
            | E::Edom
            | E::Erange
            | E::Edeadlk
            | E::Enametoolong
            | E::Enolck
            | E::Enosys
            | E::Enotempty
            | E::Eloop
            | E::Enomsg
            | E::Eidrm
            | E::Echrng
            | E::El2Nsync
            | E::El3Hlt
            | E::El3Rst
            | E::Elnrng
            | E::Eunatch
            | E::Enocsi
            | E::El2Hlt
            | E::Ebade
            | E::Ebadr
            | E::Exfull
            | E::Enoano
            | E::Ebadrqc
            | E::Ebadslt
            | E::Ebfont
            | E::Enostr
            | E::Enodata
            | E::Etime
            | E::Enosr
            | E::Enonet
            | E::Eremote
            | E::Enolink
            | E::Eadv
            | E::Esrmnt
            | E::Ecomm
            | E::Eproto
            | E::Emultihop
            | E::Edotdot
            | E::Ebadmsg
            | E::Eoverflow
            | E::Enotuniq
            | E::Ebadfd
            | E::Eremchg
            | E::Elibacc
            | E::Elibbad
            | E::Elibscn
            | E::Elibmax
            | E::Elibexec
            | E::Eilseq
            | E::Erestart
            | E::Estrpipe
            | E::Eusers
            | E::Eprototype
            | E::Eprotonosupport
            | E::Epfnosupport
            | E::Eafnosupport
            | E::Enetreset
            | E::Eshutdown
            | E::Etoomanyrefs
            | E::Etimedout
            | E::Einprogress
            | E::Estale
            | E::Euclean
            | E::Enotnam
            | E::Enavail
            | E::Eisnam
            | E::Eremoteio
            | E::Edquot
            | E::Enomedium
            | E::Emediumtype
            | E::Ecanceled
            | E::Enokey
            | E::Ekeyexpired
            | E::Ekeyrevoked
            | E::Ekeyrejected
            | E::Eownerdead
            | E::Enotrecoverable
            | E::Erfkill
            | E::Enxio
            | E::Efault
            | E::Enodev
            | E::Edestaddrreq
            | E::Enetdown
            | E::Ehwpoison => panic!("unexpected error from socket: {:?}", e),
        },
    }
}

impl dhcp_client_core::deps::Socket<std::net::SocketAddr> for UdpSocket {
    async fn send_to(
        &self,
        buf: &[u8],
        addr: std::net::SocketAddr,
    ) -> Result<(), dhcp_client_core::deps::SocketError> {
        let Self { inner } = self;

        let n = inner.send_to(buf, addr).await.map_err(translate_io_error)?;
        // UDP sockets never have short sends.
        assert_eq!(n, buf.len());
        Ok(())
    }

    async fn recv_from(
        &self,
        buf: &mut [u8],
    ) -> Result<
        dhcp_client_core::deps::DatagramInfo<std::net::SocketAddr>,
        dhcp_client_core::deps::SocketError,
    > {
        let Self { inner } = self;
        let (length, address) = inner.recv_from(buf).await.map_err(translate_io_error)?;
        Ok(dhcp_client_core::deps::DatagramInfo { length, address })
    }
}

fn set_bindtoifindex(
    socket: &impl AsRawFd,
    interface_id: NonZeroU64,
) -> Result<(), dhcp_client_core::deps::SocketError> {
    let interface_id =
        libc::c_int::try_from(interface_id.get()).expect("interface_id should fit in c_int");

    // SAFETY: `setsockopt` does not take ownership of anything passed to it.
    if unsafe {
        libc::setsockopt(
            socket.as_raw_fd(),
            libc::SOL_SOCKET,
            libc::SO_BINDTOIFINDEX,
            &interface_id as *const _ as *const libc::c_void,
            std::mem::size_of_val(&interface_id) as libc::socklen_t,
        )
    } != 0
    {
        return Err(translate_io_error(std::io::Error::last_os_error()));
    }

    Ok(())
}

pub(crate) struct LibcUdpSocketProvider {
    pub(crate) interface_id: NonZeroU64,
}

impl UdpSocketProvider for LibcUdpSocketProvider {
    type Sock = UdpSocket;

    async fn bind_new_udp_socket(
        &self,
        bound_addr: std::net::SocketAddr,
    ) -> Result<Self::Sock, dhcp_client_core::deps::SocketError> {
        let socket = std::net::UdpSocket::bind(&bound_addr).map_err(translate_io_error)?;
        set_bindtoifindex(&socket, self.interface_id)?;

        let socket = fasync::net::UdpSocket::from_socket(socket).map_err(translate_io_error)?;
        socket.set_broadcast(true).map_err(translate_io_error)?;
        Ok(UdpSocket { inner: socket })
    }
}

#[cfg(test)]
mod testutil {
    use super::*;
    use {
        fidl_fuchsia_posix_socket as fposix_socket,
        fidl_fuchsia_posix_socket_ext as fposix_socket_ext,
    };

    pub(crate) struct TestUdpSocketProvider {
        provider: fposix_socket::ProviderProxy,
        interface_id: NonZeroU64,
    }

    impl TestUdpSocketProvider {
        pub(crate) fn new(
            provider: fposix_socket::ProviderProxy,
            interface_id: NonZeroU64,
        ) -> Self {
            Self { provider, interface_id }
        }
    }

    impl UdpSocketProvider for TestUdpSocketProvider {
        type Sock = UdpSocket;

        async fn bind_new_udp_socket(
            &self,
            bound_addr: std::net::SocketAddr,
        ) -> Result<Self::Sock, dhcp_client_core::deps::SocketError> {
            let Self { provider, interface_id } = self;

            let socket = fposix_socket_ext::datagram_socket(
                provider,
                fposix_socket::Domain::Ipv4,
                fposix_socket::DatagramSocketProtocol::Udp,
            )
            .await
            .map_err(|e: fidl::Error| dhcp_client_core::deps::SocketError::FailedToOpen(e.into()))?
            .map_err(translate_io_error)?;

            socket.bind(&bound_addr.into()).map_err(translate_io_error)?;
            socket.set_broadcast(true).map_err(translate_io_error)?;

            set_bindtoifindex(&socket, *interface_id)?;

            let socket =
                fasync::net::UdpSocket::from_socket(socket.into()).map_err(translate_io_error)?;

            Ok(UdpSocket { inner: socket })
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::udpsocket::testutil::TestUdpSocketProvider;
    use dhcp_client_core::deps::{DatagramInfo, Socket as _};
    use futures::{join, FutureExt as _};
    use net_declare::std_socket_addr;
    use netstack_testing_common::realms::TestSandboxExt as _;
    use {
        fidl_fuchsia_net_ext as fnet_ext, fidl_fuchsia_netemul_network as fnetemul_network,
        fidl_fuchsia_posix_socket as fposix_socket, fuchsia_async as fasync,
    };

    #[fasync::run_singlethreaded(test)]
    async fn udp_socket_provider_impl_send_receive() {
        let sandbox: netemul::TestSandbox = netemul::TestSandbox::new().unwrap();

        let network = sandbox.create_network("dhcp-test-network").await.expect("create network");
        let realm_a: netemul::TestRealm<'_> = sandbox
            .create_netstack_realm::<netstack_testing_common::realms::Netstack2, _>(
                "dhcp-test-realm-a",
            )
            .expect("create realm");
        let realm_b: netemul::TestRealm<'_> = sandbox
            .create_netstack_realm::<netstack_testing_common::realms::Netstack2, _>(
                "dhcp-test-realm-b",
            )
            .expect("create realm");

        const MAC_A: net_types::ethernet::Mac = net_declare::net_mac!("00:00:00:00:00:01");
        const MAC_B: net_types::ethernet::Mac = net_declare::net_mac!("00:00:00:00:00:02");
        const FIDL_SUBNET_A: fidl_fuchsia_net::Subnet = net_declare::fidl_subnet!("1.1.1.1/24");
        const SOCKET_ADDR_A: std::net::SocketAddr = std_socket_addr!("1.1.1.1:1111");
        const FIDL_SUBNET_B: fidl_fuchsia_net::Subnet = net_declare::fidl_subnet!("1.1.1.2/24");
        const SOCKET_ADDR_B: std::net::SocketAddr = std_socket_addr!("1.1.1.2:2222");

        let iface_a = realm_a
            .join_network_with(
                &network,
                "iface_a",
                fnetemul_network::EndpointConfig {
                    mtu: netemul::DEFAULT_MTU,
                    mac: Some(Box::new(fnet_ext::MacAddress { octets: MAC_A.bytes() }.into())),
                    port_class: fidl_fuchsia_hardware_network::PortClass::Virtual,
                },
                netemul::InterfaceConfig { name: Some("iface_a".into()), ..Default::default() },
            )
            .await
            .expect("join network with realm_a");
        let iface_b = realm_b
            .join_network_with(
                &network,
                "iface_b",
                fnetemul_network::EndpointConfig {
                    mtu: netemul::DEFAULT_MTU,
                    mac: Some(Box::new(fnet_ext::MacAddress { octets: MAC_B.bytes() }.into())),
                    port_class: fidl_fuchsia_hardware_network::PortClass::Virtual,
                },
                netemul::InterfaceConfig { name: Some("iface_b".into()), ..Default::default() },
            )
            .await
            .expect("join network with realm_b");

        iface_a
            .add_address_and_subnet_route(FIDL_SUBNET_A)
            .await
            .expect("add address should succeed");
        iface_b
            .add_address_and_subnet_route(FIDL_SUBNET_B)
            .await
            .expect("add address should succeed");

        let socket_a = TestUdpSocketProvider::new(
            realm_a.connect_to_protocol::<fposix_socket::ProviderMarker>().unwrap(),
            NonZeroU64::new(iface_a.id()).unwrap(),
        )
        .bind_new_udp_socket(SOCKET_ADDR_A)
        .await
        .expect("get udp socket");

        let socket_b = TestUdpSocketProvider::new(
            realm_b.connect_to_protocol::<fposix_socket::ProviderMarker>().unwrap(),
            NonZeroU64::new(iface_b.id()).unwrap(),
        )
        .bind_new_udp_socket(SOCKET_ADDR_B)
        .await
        .expect("get udp socket");

        let mut buf = [0u8; netemul::DEFAULT_MTU as usize];

        let payload = b"hello world!";

        let DatagramInfo { length, address } = {
            let send_fut = async {
                socket_a.send_to(payload.as_ref(), SOCKET_ADDR_B).await.expect("send_to");
            }
            .fuse();

            let receive_fut =
                async { socket_b.recv_from(&mut buf).await.expect("recv_from") }.fuse();

            let ((), datagram_info) = join!(send_fut, receive_fut);
            datagram_info
        };

        assert_eq!(&buf[..length], payload.as_ref());
        assert_eq!(address, SOCKET_ADDR_A);
    }
}