Skip to content

Commit 9add601

Browse files
ghananigansRebase bot
authored andcommitted
[starnix] Don't hardcode iface name in SIOCGIFINDEX
Before this change, `ioctl(_, SIOCGIFINDEX, _)` returned a value of of `1` if the requested interface name was `sta-iface-name`. The value `1` is usually assigned to the loopback interface has a different name and the name `sta-iface-name` may not be present on all systems. The changes implements `ioctl(_, SIOCGIFINDEX, _)` (for inet sockets) by requesting the interface index for the interface name from the netstack. Bug: 128604, 129059 Bug: b/286855878 Change-Id: I4c8cf3eaf8a011ba9f8b970a3c330bb87e9a7a89 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/870847 Reviewed-by: Theodore Dubois <[email protected]> Commit-Queue: Auto-Submit <[email protected]> Reviewed-by: Kiet Tran <[email protected]> Fuchsia-Auto-Submit: Ghanan Gowripalan <[email protected]>
1 parent bd34d9e commit 9add601

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/starnix/kernel/fs/socket/socket_inet.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use crate::{
1111
task::*,
1212
types::*,
1313
};
14+
1415
use fidl::endpoints::DiscoverableProtocolMarker as _;
1516
use fidl_fuchsia_posix_socket as fposix_socket;
1617
use fidl_fuchsia_posix_socket_raw as fposix_socket_raw;
18+
use fuchsia_component::client::connect_channel_to_protocol;
1719
use fuchsia_zircon as zx;
1820
use static_assertions::const_assert_eq;
1921
use std::{cell::OnceCell, ffi::CStr, sync::Arc};
@@ -285,20 +287,31 @@ impl SocketOps for InetSocket {
285287
request: u32,
286288
arg: SyscallArg,
287289
) -> Result<SyscallResult, Errno> {
288-
// TODO(fxbug.dev/128604): Remove hardcoded constant
289-
const DEFAULT_IFACE_NAME: &[u8; 16usize] = b"sta-iface-name\0\0";
290290
let user_addr = UserAddress::from(arg);
291+
// TODO(https://fxbug.dev/129059): Share this implementation with `fdio`
292+
// by moving things to `zxio`.
291293
match request {
292294
SIOCGIFINDEX => {
293295
let in_ifreq: ifreq = current_task.mm.read_object(UserRef::new(user_addr))?;
294-
let iface_name = unsafe { CStr::from_ptr(in_ifreq.ifr_ifrn.ifrn_name.as_ptr()) };
295-
if iface_name.to_bytes()[..] != DEFAULT_IFACE_NAME[..] {
296-
return error!(EINVAL);
297-
}
296+
let iface_name = unsafe { CStr::from_ptr(in_ifreq.ifr_ifrn.ifrn_name.as_ptr()) }
297+
.to_str()
298+
.map_err(|std::str::Utf8Error { .. }| errno!(EINVAL))?;
299+
let (provider, server_end) = zx::Channel::create();
300+
connect_channel_to_protocol::<fposix_socket::ProviderMarker>(server_end)
301+
.expect("should always have access to `fuchsia.posix.socket/Provider`");
302+
let provider = fposix_socket::ProviderSynchronousProxy::new(provider);
303+
let index = provider
304+
.interface_name_to_index(iface_name, zx::Time::INFINITE)
305+
.expect("netstack should never close its end of the channel");
306+
let index: u64 = index.map_err(|zx_err| {
307+
let zx_err = zx::Status::from_raw(zx_err);
308+
from_status_like_fdio!(zx_err)
309+
})?;
310+
// TODO(https://fxbug.dev/129172): Avoid this panic opportunity.
311+
let index = i32::try_from(index).expect("interface ID should fit in an i32");
298312
let out_ifreq: [u8; std::mem::size_of::<ifreq>()] = struct_with_union_into_bytes!(ifreq {
299313
ifr_ifrn.ifrn_name: unsafe { in_ifreq.ifr_ifrn.ifrn_name },
300-
// TODO(fxbug.dev/128604): Don't hardcode iface index
301-
ifr_ifru.ifru_ivalue: 1,
314+
ifr_ifru.ifru_ivalue: index,
302315
});
303316
current_task.mm.write_object(UserRef::new(user_addr), &out_ifreq)?;
304317
Ok(SUCCESS)

0 commit comments

Comments
 (0)