starry-kernel 0.7.7

A Linux-compatible OS kernel built on ArceOS unikernel
Documentation
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
use alloc::{
    borrow::{Cow, ToOwned},
    format,
    sync::Arc,
};
use core::{
    ffi::c_int,
    mem::offset_of,
    ops::Deref,
    sync::atomic::{AtomicBool, AtomicI32, Ordering},
    task::Context,
};

use ax_errno::{AxError, AxResult};
use ax_net::{
    InterfaceFlags, InterfaceId, InterfaceInfo, InterfaceKind, RecvOptions, SendOptions,
    Socket as SocketInner, SocketOps,
    options::{Configurable, GetSocketOption, SetSocketOption, UnixCredentials},
};
use ax_task::current;
use axpoll::{IoEvents, Pollable};
use linux_raw_sys::{
    general::{CAP_NET_ADMIN, O_RDWR, S_IFSOCK},
    ioctl::{
        FIONREAD, SIOCGIFADDR, SIOCGIFBRDADDR, SIOCGIFCONF, SIOCGIFDSTADDR, SIOCGIFFLAGS,
        SIOCGIFHWADDR, SIOCGIFINDEX, SIOCGIFMAP, SIOCGIFMETRIC, SIOCGIFMTU, SIOCGIFNETMASK,
        SIOCGIFSLAVE, SIOCGIFTXQLEN, SIOCSIFFLAGS,
    },
    net::{AF_INET, ifreq},
};
use starry_vm::{VmMutPtr, vm_read_slice, vm_write_slice};

use super::{FileLike, Kstat};
use crate::{
    file::{IoDst, IoSrc, get_file_like},
    syscall::in_root_net_ns,
    task::AsThread,
};

pub(super) const ARPHRD_ETHER: u16 = 1;
pub(super) const ARPHRD_LOOPBACK: u16 = 772;
const IFF_UP: i16 = 0x0001;
const IFF_BROADCAST: i16 = 0x0002;
const IFF_LOOPBACK: i16 = 0x0008;
const IFF_RUNNING: i16 = 0x0040;
const IFF_MULTICAST: i16 = 0x1000;
const IFREQ_NAME_LEN: usize = 16;
const IFREQ_DATA_OFFSET: usize = 16;
const IFREQ_COMPAT_LEN: usize = 40;
// ethtool ioctl; not exported by linux-raw-sys. The value is arch-independent.
const SIOCETHTOOL: u32 = 0x8946;
// Map an interface index to its name (Linux net/core/dev_ioctl.c dev_ifname).
// Arch-independent; the inverse of SIOCGIFINDEX.
const SIOCGIFNAME: u32 = 0x8910;
const IFCONF_LEN_OFFSET: usize = 0;
const IFCONF_BUF_OFFSET: usize = 8;

pub struct Socket {
    inner: SocketInner,
    ip_domain: u32,
    async_mode: AtomicBool,
    owner: AtomicI32,
}

impl Socket {
    pub fn new(inner: SocketInner, ip_domain: u32) -> Self {
        Self {
            inner,
            ip_domain,
            async_mode: AtomicBool::new(false),
            owner: AtomicI32::new(0),
        }
    }

    pub fn ip_domain(&self) -> u32 {
        self.ip_domain
    }

    pub(crate) fn with_current_sender_credentials(mut options: SendOptions) -> SendOptions {
        let current = current();
        let credentials = current.as_thread().cred();
        options.sender_credentials = Some(UnixCredentials {
            pid: current.as_thread().proc_data.proc.pid(),
            uid: credentials.uid,
            gid: credentials.gid,
        });
        options
    }
}

pub(super) fn visible_interfaces() -> impl Iterator<Item = InterfaceInfo> {
    ax_net::interfaces()
        .into_iter()
        .filter(|info| in_root_net_ns() || info.kind == InterfaceKind::Loopback)
}

pub(super) fn visible_interface_by_id(id: InterfaceId) -> AxResult<InterfaceInfo> {
    ax_net::interface_by_id(id)
        .filter(|info| in_root_net_ns() || info.kind == InterfaceKind::Loopback)
        .ok_or(AxError::NoSuchDevice)
}

pub(super) fn first_visible_ethernet() -> AxResult<InterfaceInfo> {
    visible_interfaces()
        .find(|info| info.kind == InterfaceKind::Ethernet)
        .ok_or(AxError::NoSuchDevice)
}

fn read_user_bytes<const N: usize>(ptr: *const u8) -> AxResult<[u8; N]> {
    let mut buf = [core::mem::MaybeUninit::<u8>::uninit(); N];
    vm_read_slice(ptr, &mut buf)?;
    Ok(buf.map(|v| unsafe { v.assume_init() }))
}

fn read_ifreq_name(arg: usize) -> AxResult<alloc::string::String> {
    let name = read_user_bytes::<IFREQ_NAME_LEN>(arg as *const u8)?;
    let end = name.iter().position(|&b| b == 0).unwrap_or(name.len());
    core::str::from_utf8(&name[..end])
        .map(str::to_owned)
        .map_err(|_| AxError::InvalidInput)
}

fn read_ifreq_interface(arg: usize) -> AxResult<InterfaceInfo> {
    let name = read_ifreq_name(arg)?;
    ax_net::interface_by_name(&name)
        .filter(|info| in_root_net_ns() || info.kind == InterfaceKind::Loopback)
        .ok_or(AxError::NoSuchDevice)
}

fn write_ifreq_data(arg: usize, data: &[u8]) -> AxResult<()> {
    Ok(vm_write_slice((arg + IFREQ_DATA_OFFSET) as *mut u8, data)?)
}

fn read_ifreq_flags(arg: usize) -> AxResult<i16> {
    Ok(i16::from_ne_bytes(read_user_bytes::<2>(
        (arg + IFREQ_DATA_OFFSET) as *const u8,
    )?))
}

// Writes an interface name into `ifr_name` (offset 0), NUL-padded to IFNAMSIZ.
fn write_ifreq_name(arg: usize, name: &str) -> AxResult<()> {
    let mut buf = [0u8; IFREQ_NAME_LEN];
    let bytes = name.as_bytes();
    let n = bytes.len().min(IFREQ_NAME_LEN - 1);
    buf[..n].copy_from_slice(&bytes[..n]);
    Ok(vm_write_slice(arg as *mut u8, &buf)?)
}

/// Device-level socket ioctls (`SIOCGIF*`), shared across every socket family.
///
/// Linux routes these through `sock_ioctl` -> `dev_ioctl` regardless of the
/// socket's address family (net/socket.c), so `AF_UNIX`/`AF_NETLINK` sockets
/// answer them too - `if_indextoname(3)` in musl issues `SIOCGIFNAME` on an
/// `AF_UNIX` socket, which must resolve rather than return `ENOTTY`. Returns
/// `Some(result)` when `cmd` is a device ioctl this layer owns, `None` otherwise
/// so the caller can try family-specific commands or fall back to `ENOTTY`.
pub(super) fn device_ioctl(cmd: u32, arg: usize) -> Option<AxResult<usize>> {
    let result = (|| -> AxResult<usize> {
        match cmd {
            SIOCGIFCONF => write_ifconf(arg)?,
            SIOCGIFNAME => {
                // Map ifr_ifindex -> ifr_name (Linux dev_ifname); inverse of
                // SIOCGIFINDEX. The index arrives in the ifr_ifru union.
                let idx = i32::from_ne_bytes(read_user_bytes::<4>(
                    (arg + IFREQ_DATA_OFFSET) as *const u8,
                )?);
                let info = visible_interface_by_id(InterfaceId::new(idx as u32))?;
                write_ifreq_name(arg, &info.name)?;
            }
            SIOCGIFFLAGS => {
                let info = read_ifreq_interface(arg)?;
                write_ifreq_data(arg, &linux_flags(&info).to_ne_bytes())?;
            }
            SIOCSIFFLAGS => {
                let info = read_ifreq_interface(arg)?;
                if !current().as_thread().cred().has_cap(CAP_NET_ADMIN) {
                    return Err(AxError::OperationNotPermitted);
                }
                if read_ifreq_flags(arg)? != linux_flags(&info) {
                    return Err(AxError::OperationNotSupported);
                }
            }
            SIOCGIFADDR => {
                let info = read_ifreq_interface(arg)?;
                write_ifreq_sockaddr(arg, interface_ipv4(&info)?.address.address().octets())?;
            }
            SIOCGIFDSTADDR => {
                let info = read_ifreq_interface(arg)?;
                let addr = if info.kind == InterfaceKind::Loopback {
                    interface_ipv4(&info)?.address.address().octets()
                } else {
                    [0, 0, 0, 0]
                };
                write_ifreq_sockaddr(arg, addr)?;
            }
            SIOCGIFBRDADDR => {
                let info = read_ifreq_interface(arg)?;
                let addr = if info.kind == InterfaceKind::Loopback {
                    interface_ipv4(&info)?.address.address().octets()
                } else {
                    ipv4_broadcast(interface_ipv4(&info)?)
                };
                write_ifreq_sockaddr(arg, addr)?;
            }
            SIOCGIFNETMASK => {
                let info = read_ifreq_interface(arg)?;
                write_ifreq_sockaddr(
                    arg,
                    ipv4_netmask(interface_ipv4(&info)?.address.prefix_len()),
                )?;
            }
            SIOCGIFHWADDR => {
                let info = read_ifreq_interface(arg)?;
                match info.kind {
                    InterfaceKind::Ethernet => {
                        let mac = info.mac.ok_or(AxError::NoSuchDevice)?;
                        write_ifreq_hwaddr(arg, ARPHRD_ETHER, &mac.0)?
                    }
                    InterfaceKind::Loopback => write_ifreq_hwaddr(arg, ARPHRD_LOOPBACK, &[])?,
                }
            }
            SIOCGIFMTU => {
                let mtu = read_ifreq_interface(arg)?.mtu as i32;
                write_ifreq_data(arg, &mtu.to_ne_bytes())?;
            }
            SIOCGIFMETRIC => {
                read_ifreq_interface(arg)?;
                write_ifreq_data(arg, &0i32.to_ne_bytes())?;
            }
            SIOCGIFMAP => {
                read_ifreq_interface(arg)?;
                write_ifreq_data(arg, &[0; 24])?;
            }
            // In the "can be done by all, return a value" read-only group with the
            // other SIOCGIF* getters, but dev_ifsioc_locked has no bonding master to
            // report: an unknown name is ENODEV (read_ifreq_interface) and a resolved
            // interface is EINVAL (Linux net/core/dev_ioctl.c dev_ifsioc_locked).
            SIOCGIFSLAVE => {
                read_ifreq_interface(arg)?;
                return Err(AxError::InvalidInput);
            }
            SIOCGIFTXQLEN => {
                read_ifreq_interface(arg)?;
                let qlen_ptr = (arg + offset_of!(ifreq, ifr_ifru)) as *mut i32;
                qlen_ptr.vm_write(1000)?;
            }
            SIOCGIFINDEX => {
                let idx = read_ifreq_interface(arg)?.id.get() as i32;
                write_ifreq_data(arg, &idx.to_ne_bytes())?;
            }
            // Link speed/duplex query. No PHY is emulated, so report "not supported" the way a
            // virtual NIC (loopback, tun/tap) does. Tools like psutil's net_if_stats() treat
            // EOPNOTSUPP as "no ethtool" and degrade gracefully; any other errno makes them abort
            // the whole interface-status probe. Resolve the interface first so an unknown name
            // yields ENODEV, then fault on a bad ifr_data pointer, keeping Linux's error priority
            // (ENODEV, then EFAULT, then EOPNOTSUPP) and parity with the sibling SIOC*IF* arms.
            SIOCETHTOOL => {
                read_ifreq_interface(arg)?;
                let data_ptr = usize::from_ne_bytes(read_user_bytes::<8>(
                    (arg + IFREQ_DATA_OFFSET) as *const u8,
                )?);
                read_user_bytes::<4>(data_ptr as *const u8)?;
                return Err(AxError::OperationNotSupported);
            }
            _ => return Err(AxError::NotATty),
        }
        Ok(0)
    })();
    match result {
        Err(AxError::NotATty) => None,
        other => Some(other),
    }
}

fn sockaddr_in_bytes(ip: [u8; 4]) -> [u8; 16] {
    let mut addr = [0; 16];
    addr[..2].copy_from_slice(&(AF_INET as u16).to_ne_bytes());
    addr[4..8].copy_from_slice(&ip);
    addr
}

fn write_ifreq_sockaddr(arg: usize, ip: [u8; 4]) -> AxResult<()> {
    write_ifreq_data(arg, &sockaddr_in_bytes(ip))
}

fn write_ifreq_hwaddr(arg: usize, hw_type: u16, hwaddr: &[u8]) -> AxResult<()> {
    let mut addr = [0; 16];
    addr[..2].copy_from_slice(&hw_type.to_ne_bytes());
    addr[2..2 + hwaddr.len()].copy_from_slice(hwaddr);
    write_ifreq_data(arg, &addr)
}

fn write_ifconf_entry(buf: usize, offset: usize, name: &str, ip: [u8; 4]) -> AxResult<()> {
    let mut ifreq = [0; IFREQ_COMPAT_LEN];
    let name = name.as_bytes();
    let name_len = name.len().min(IFREQ_NAME_LEN - 1);
    ifreq[..name_len].copy_from_slice(&name[..name_len]);
    ifreq[IFREQ_DATA_OFFSET..IFREQ_DATA_OFFSET + 16].copy_from_slice(&sockaddr_in_bytes(ip));
    Ok(vm_write_slice((buf + offset) as *mut u8, &ifreq)?)
}

fn interface_ipv4(info: &InterfaceInfo) -> AxResult<ax_net::Ipv4InterfaceConfig> {
    info.ipv4.ok_or(AxError::NoSuchDeviceOrAddress)
}

fn ipv4_netmask(prefix_len: u8) -> [u8; 4] {
    if prefix_len == 0 {
        return [0; 4];
    }
    (!0u32 << (32 - prefix_len)).to_be_bytes()
}

fn ipv4_broadcast(config: ax_net::Ipv4InterfaceConfig) -> [u8; 4] {
    let ip = u32::from_be_bytes(config.address.address().octets());
    let mask = u32::from_be_bytes(ipv4_netmask(config.address.prefix_len()));
    (ip | !mask).to_be_bytes()
}

fn linux_flags(info: &InterfaceInfo) -> i16 {
    let mut flags = 0;
    if info.flags.contains(InterfaceFlags::UP) {
        flags |= IFF_UP;
    }
    if info.flags.contains(InterfaceFlags::RUNNING) {
        flags |= IFF_RUNNING;
    }
    if info.flags.contains(InterfaceFlags::LOOPBACK) {
        flags |= IFF_LOOPBACK;
    }
    if info.flags.contains(InterfaceFlags::BROADCAST) {
        flags |= IFF_BROADCAST;
    }
    if info.flags.contains(InterfaceFlags::MULTICAST) {
        flags |= IFF_MULTICAST;
    }
    flags
}

fn write_ifconf(arg: usize) -> AxResult<()> {
    let mut len = read_user_bytes::<4>((arg + IFCONF_LEN_OFFSET) as *const u8)?;
    let ifc_len = i32::from_ne_bytes(len);
    let buf = usize::from_ne_bytes(read_user_bytes::<{ core::mem::size_of::<usize>() }>(
        (arg + IFCONF_BUF_OFFSET) as *const u8,
    )?);
    let interfaces: alloc::vec::Vec<_> = visible_interfaces()
        .filter_map(|info| {
            info.ipv4
                .map(|ipv4| (info.name, ipv4.address.address().octets()))
        })
        .collect();

    if buf != 0 {
        let mut written = 0;
        for (name, ip) in interfaces {
            if ifc_len < (written + IFREQ_COMPAT_LEN) as i32 {
                break;
            }
            write_ifconf_entry(buf, written, &name, ip)?;
            written += IFREQ_COMPAT_LEN;
        }
        len = (written as i32).to_ne_bytes();
    } else {
        len = ((interfaces.len() * IFREQ_COMPAT_LEN) as i32).to_ne_bytes();
    }
    vm_write_slice((arg + IFCONF_LEN_OFFSET) as *mut u8, &len)?;
    Ok(())
}

impl Deref for Socket {
    type Target = SocketInner;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl FileLike for Socket {
    fn read(&self, dst: &mut IoDst) -> AxResult<usize> {
        self.recv(dst, RecvOptions::default())
    }

    fn write(&self, src: &mut IoSrc) -> AxResult<usize> {
        self.send(
            src,
            Self::with_current_sender_credentials(SendOptions::default()),
        )
    }

    fn stat(&self) -> AxResult<Kstat> {
        Ok(Kstat {
            mode: S_IFSOCK | 0o777u32,
            blksize: 4096,
            ..Default::default()
        })
    }

    fn nonblocking(&self) -> bool {
        let mut result = false;
        self.get_option(GetSocketOption::NonBlocking(&mut result))
            .unwrap();
        result
    }

    fn set_nonblocking(&self, nonblocking: bool) -> AxResult<()> {
        self.inner
            .set_option(SetSocketOption::NonBlocking(&nonblocking))
    }

    fn async_mode(&self) -> bool {
        self.async_mode.load(Ordering::Acquire)
    }

    fn supports_async_mode(&self) -> bool {
        true
    }

    fn set_async_mode(&self, async_mode: bool) -> AxResult {
        self.async_mode.store(async_mode, Ordering::Release);
        Ok(())
    }

    fn owner(&self) -> AxResult<i32> {
        Ok(self.owner.load(Ordering::Acquire))
    }

    fn set_owner(&self, owner: i32) -> AxResult {
        self.owner.store(owner, Ordering::Release);
        Ok(())
    }

    fn path(&self) -> Cow<'_, str> {
        format!("socket:[{}]", self as *const _ as usize).into()
    }

    fn open_flags(&self) -> u32 {
        O_RDWR
    }

    fn ioctl(&self, cmd: u32, arg: usize) -> AxResult<usize> {
        // Socket-specific query first, then the family-agnostic device ioctls
        // (SIOCGIF*), mirroring Linux sock_ioctl dispatching to dev_ioctl.
        if cmd == FIONREAD {
            let available = self.inner.recv_available()?.min(c_int::MAX as usize) as c_int;
            (arg as *mut c_int).vm_write(available)?;
            return Ok(0);
        }
        if let Some(result) = device_ioctl(cmd, arg) {
            return result;
        }
        if super::wext::is_wext_ioctl(cmd) {
            return super::wext::handle(cmd, arg);
        }
        Err(AxError::NotATty)
    }

    fn from_fd(fd: c_int) -> AxResult<Arc<Self>>
    where
        Self: Sized + 'static,
    {
        get_file_like(fd)?
            .downcast_arc()
            .map_err(|_| AxError::NotASocket)
    }
}

impl Pollable for Socket {
    fn poll(&self) -> IoEvents {
        self.inner.poll()
    }

    fn register(&self, context: &mut Context<'_>, events: IoEvents) {
        self.inner.register(context, events);
    }
}