Skip to content

Commit 8599330

Browse files
inet: implement SIOCSIFADDR ioctl
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 9d171f0 commit 8599330

File tree

5 files changed

+155
-29
lines changed

5 files changed

+155
-29
lines changed

patches/rust-libc/rust-libc.patch

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
From 1c11650435d49430ddc9838ae0e59a8bcdad8243 Mon Sep 17 00:00:00 2001
1+
From 698a2f63c0fde81a3c2aec2c2de0e7ccfe0f3bd8 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
3-
Date: Fri, 3 Mar 2023 16:39:55 +1100
3+
Date: Fri, 3 Mar 2023 18:52:53 +1100
44
Subject: [PATCH] <xxx>
55

66
---
77
.../linux_like/linux/gnu/b64/x86_64/mod.rs | 4 +-
88
src/unix/linux_like/linux/gnu/mod.rs | 4 +-
99
src/unix/linux_like/linux/mod.rs | 28 +-
10-
src/unix/mlibc/mod.rs | 460 ++++++++++++++++--
11-
4 files changed, 442 insertions(+), 54 deletions(-)
10+
src/unix/mlibc/mod.rs | 499 ++++++++++++++++--
11+
4 files changed, 481 insertions(+), 54 deletions(-)
1212

1313
diff --git a/src/unix/linux_like/linux/gnu/b64/x86_64/mod.rs b/src/unix/linux_like/linux/gnu/b64/x86_64/mod.rs
1414
index e6307e2..cae81c4 100644
@@ -185,7 +185,7 @@ index be12190..c8762d5 100644
185185
.field("ifr_name", &self.ifr_name)
186186
.field("ifr_ifru", &self.ifr_ifru)
187187
diff --git a/src/unix/mlibc/mod.rs b/src/unix/mlibc/mod.rs
188-
index 2046530..0d74760 100644
188+
index 2046530..b479ed2 100644
189189
--- a/src/unix/mlibc/mod.rs
190190
+++ b/src/unix/mlibc/mod.rs
191191
@@ -47,7 +47,6 @@ pub type fsfilcnt_t = ::c_uint;
@@ -287,7 +287,7 @@ index 2046530..0d74760 100644
287287
}
288288
pub struct pthread_rwlock_t {
289289
pub __mlibc_m: ::c_uint,
290-
@@ -319,10 +348,73 @@ s! {
290+
@@ -319,10 +348,111 @@ s! {
291291
pub struct fd_set {
292292
pub fds_bits: [c_char; 128],
293293
}
@@ -333,6 +333,44 @@ index 2046530..0d74760 100644
333333
+ pub l_start: ::off_t,
334334
+ pub l_len: ::off_t,
335335
+ pub l_pid: ::pid_t,
336+
+ }
337+
+}
338+
+
339+
+s_no_extra_traits! {
340+
+ // options/posix/include/net/if.h
341+
+ #[cfg(libc_union)]
342+
+ pub union __c_anonymous_ifr_ifru {
343+
+ pub ifru_addr: ::sockaddr,
344+
+ pub ifru_dstaddr: ::sockaddr,
345+
+ pub ifru_broadaddr: ::sockaddr,
346+
+ pub ifru_netmask: ::sockaddr,
347+
+ pub ifru_hwaddr: ::sockaddr,
348+
+ pub ifru_flags: ::c_short,
349+
+ pub ifru_ifindex: ::c_int,
350+
+ pub ifru_metric: ::c_int,
351+
+ pub ifru_mtu: ::c_int,
352+
+ pub ifru_map: __c_anonymous_ifru_map,
353+
+ pub ifru_slave: [::c_char; ::IFNAMSIZ],
354+
+ pub ifru_newname: [::c_char; ::IFNAMSIZ],
355+
+ pub ifru_data: *mut ::c_char,
356+
+ }
357+
+
358+
+ pub struct __c_anonymous_ifru_map {
359+
+ pub mem_start: ::c_ulong,
360+
+ pub mem_end: ::c_ulong,
361+
+ pub base_addr: ::c_ushort,
362+
+ pub irq: ::c_uchar,
363+
+ pub dma: ::c_uchar,
364+
+ pub port: ::c_uchar,
365+
+ }
366+
+
367+
+ pub struct ifreq {
368+
+ /// if name, e.g. "en0"
369+
+ pub ifr_name: [::c_char; ::IFNAMSIZ],
370+
+ #[cfg(libc_union)]
371+
+ pub ifr_ifru: __c_anonymous_ifr_ifru,
372+
+ #[cfg(not(libc_union))]
373+
+ pub ifr_ifru: ::sockaddr,
336374
+ }
337375
}
338376

@@ -361,7 +399,7 @@ index 2046530..0d74760 100644
361399
pub {const} fn WCOREDUMP(x: ::c_int) -> bool {
362400
x & WCOREFLAG != 0
363401
}
364-
@@ -349,6 +441,104 @@ safe_f! {
402+
@@ -349,6 +479,104 @@ safe_f! {
365403
}
366404
}
367405

@@ -466,7 +504,7 @@ index 2046530..0d74760 100644
466504
// abis/mlibc/vm-flags.h
467505
pub const MAP_ANON: ::c_int = 8;
468506
pub const MAP_PRIVATE: ::c_int = 1;
469-
@@ -356,11 +546,24 @@ pub const MAP_SHARED: ::c_int = 2;
507+
@@ -356,11 +584,24 @@ pub const MAP_SHARED: ::c_int = 2;
470508
pub const PROT_EXEC: ::c_int = 4;
471509
pub const PROT_READ: ::c_int = 1;
472510
pub const PROT_WRITE: ::c_int = 2;
@@ -491,7 +529,7 @@ index 2046530..0d74760 100644
491529

492530
// options/ansi/include/time.h
493531
pub const CLOCK_MONOTONIC: clockid_t = 1;
494-
@@ -369,33 +572,49 @@ pub const CLOCK_REALTIME: clockid_t = 0;
532+
@@ -369,33 +610,49 @@ pub const CLOCK_REALTIME: clockid_t = 0;
495533
// options/posix/include/netdb.h
496534
pub const EAI_SYSTEM: ::c_int = 9;
497535

@@ -563,7 +601,7 @@ index 2046530..0d74760 100644
563601

564602
// abis/linux/signal.h
565603
pub const SIGABRT: ::c_int = 6;
566-
@@ -436,45 +655,121 @@ pub const SIGSYS: ::c_int = 31;
604+
@@ -436,45 +693,121 @@ pub const SIGSYS: ::c_int = 31;
567605
pub const SIGUNUSED: ::c_int = SIGSYS;
568606
pub const SIGCANCEL: ::c_int = 32;
569607

@@ -686,7 +724,7 @@ index 2046530..0d74760 100644
686724
pub const SOCK_STREAM: ::c_int = 4;
687725
pub const SOL_SOCKET: ::c_int = 1;
688726
pub const SO_ACCEPTCONN: ::c_int = 1;
689-
@@ -494,6 +789,8 @@ pub const SO_SNDLOWAT: ::c_int = 14;
727+
@@ -494,6 +827,8 @@ pub const SO_SNDLOWAT: ::c_int = 14;
690728
pub const SO_SNDTIMEO: ::c_int = 15;
691729
pub const SO_TYPE: ::c_int = 16;
692730
pub const SO_SNDBUFFORCE: ::c_int = 17;
@@ -695,7 +733,7 @@ index 2046530..0d74760 100644
695733
pub const SO_PEERCRED: ::c_int = 18;
696734
pub const SO_ATTACH_FILTER: ::c_int = 19;
697735
pub const SO_PASSCRED: ::c_int = 20;
698-
@@ -503,6 +800,12 @@ pub const SO_PROTOCOL: ::c_int = 23;
736+
@@ -503,6 +838,12 @@ pub const SO_PROTOCOL: ::c_int = 23;
699737
pub const SO_REUSEPORT: ::c_int = 24;
700738
pub const SO_TIMESTAMP: ::c_int = 25;
701739

@@ -708,7 +746,7 @@ index 2046530..0d74760 100644
708746
// abis/mlibc/errno.h
709747
pub const EDOM: ::c_int = 1;
710748
pub const EILSEQ: ::c_int = 2;
711-
@@ -592,15 +895,24 @@ pub const ENOTBLK: ::c_int = 1083;
749+
@@ -592,15 +933,24 @@ pub const ENOTBLK: ::c_int = 1083;
712750

713751
// options/posix/include/fcntl.h
714752
pub const AT_FDCWD: ::c_int = -100;
@@ -734,7 +772,7 @@ index 2046530..0d74760 100644
734772
pub const F_GETOWN: ::c_int = 10;
735773
pub const F_SETOWN: ::c_int = 11;
736774
pub const O_ACCMODE: ::c_int = 7;
737-
@@ -644,23 +956,54 @@ pub const S_IXGRP: mode_t = 0o10;
775+
@@ -644,23 +994,55 @@ pub const S_IXGRP: mode_t = 0o10;
738776
pub const S_IXOTH: mode_t = 0o1;
739777
pub const S_IXUSR: mode_t = 0o100;
740778

@@ -786,11 +824,12 @@ index 2046530..0d74760 100644
786824
+pub const PATH_MAX: ::size_t = 4096;
787825
+
788826
+// abis/linux/ioctls.h
789-
+pub const SIOCGIFHWADDR: ::c_ulong = 0x00008927;
827+
+pub const SIOCGIFHWADDR: ::c_ulong = 0x8927;
828+
+pub const SIOCSIFADDR: ::c_ulong = 0x8916;
790829

791830
// options/posix/include/pthread.h
792831
align_const! {
793-
@@ -668,9 +1011,12 @@ align_const! {
832+
@@ -668,9 +1050,12 @@ align_const! {
794833
__mlibc_state: 0,
795834
__mlibc_recursion: 0,
796835
__mlibc_flags: 0,
@@ -803,7 +842,7 @@ index 2046530..0d74760 100644
803842
};
804843
pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = pthread_rwlock_t {
805844
__mlibc_m: 0,
806-
@@ -689,11 +1035,40 @@ pub const PTHREAD_PROCESS_PRIVATE: ::c_int = 0;
845+
@@ -689,11 +1074,40 @@ pub const PTHREAD_PROCESS_PRIVATE: ::c_int = 0;
807846
pub const PTHREAD_PROCESS_SHARED: ::c_int = 1;
808847

809848
extern "C" {
@@ -844,7 +883,7 @@ index 2046530..0d74760 100644
844883
pub fn getgrgid_r(
845884
gid: ::gid_t,
846885
grp: *mut ::group,
847-
@@ -728,13 +1103,24 @@ extern "C" {
886+
@@ -728,13 +1142,24 @@ extern "C" {
848887
buflen: ::size_t,
849888
result: *mut *mut passwd,
850889
) -> ::c_int;
@@ -873,7 +912,7 @@ index 2046530..0d74760 100644
873912
pub fn pthread_create(
874913
thread: *mut ::pthread_t,
875914
attr: *const ::pthread_attr_t,
876-
@@ -752,6 +1138,8 @@ extern "C" {
915+
@@ -752,6 +1177,8 @@ extern "C" {
877916
addr: *mut ::sockaddr,
878917
addrlen: *mut ::socklen_t,
879918
) -> ::ssize_t;

src/aero_kernel/src/drivers/e1000.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::mem::paging::*;
2727
use crate::userland::scheduler;
2828
use crate::utils::sync::{BlockQueue, Mutex};
2929

30-
use crate::net::{self, ethernet};
31-
use crate::net::{MacAddr, NetworkDevice, PacketBaseTrait};
30+
use crate::net::{self, ethernet, NetworkDevice};
31+
use crate::net::{MacAddr, NetworkDriver, PacketBaseTrait};
3232

3333
const TX_DESC_NUM: u32 = 32;
3434
const TX_DESC_SIZE: u32 = TX_DESC_NUM * core::mem::size_of::<TxDescriptor>() as u32;
@@ -596,7 +596,7 @@ impl Device {
596596
}
597597
}
598598

599-
impl NetworkDevice for Device {
599+
impl NetworkDriver for Device {
600600
fn send(&self, packet: net::Packet<net::Eth>) {
601601
self.e1000.lock_irq().send(packet)
602602
}
@@ -644,7 +644,7 @@ impl PciDeviceHandle for Handler {
644644
let device = Arc::new(Device::new(e1000));
645645

646646
DEVICE.call_once(|| device.clone());
647-
net::add_device(device);
647+
net::add_device(NetworkDevice::new(device));
648648
}
649649
}
650650

src/aero_kernel/src/net/mod.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,47 @@ use crate::{
3535
userland::{scheduler, task::Task},
3636
};
3737

38+
use self::ip::Ipv4Addr;
39+
3840
#[downcastable]
39-
pub trait NetworkDevice: Send + Sync {
41+
pub trait NetworkDriver: Send + Sync {
4042
fn send(&self, packet: Packet<Eth>);
4143
fn recv(&self) -> RecvPacket;
4244
fn recv_end(&self, packet_id: usize);
4345
fn mac(&self) -> MacAddr;
4446
}
4547

48+
#[derive(Default)]
49+
struct Metadata {
50+
ip: Ipv4Addr,
51+
}
52+
53+
pub struct NetworkDevice {
54+
driver: Arc<dyn NetworkDriver>,
55+
metadata: RwLock<Metadata>,
56+
}
57+
58+
impl NetworkDevice {
59+
pub fn new(driver: Arc<dyn NetworkDriver>) -> Self {
60+
Self {
61+
driver,
62+
metadata: RwLock::new(Metadata::default()),
63+
}
64+
}
65+
66+
pub fn set_ip(&self, ip: Ipv4Addr) {
67+
self.metadata.write().ip = ip;
68+
}
69+
}
70+
71+
impl core::ops::Deref for NetworkDevice {
72+
type Target = Arc<dyn NetworkDriver>;
73+
74+
fn deref(&self) -> &Self::Target {
75+
&self.driver
76+
}
77+
}
78+
4679
#[derive(Debug)]
4780
pub struct RecvPacket {
4881
pub packet: Packet<Eth>,
@@ -159,8 +192,8 @@ pub trait PacketHeader<H>: PacketBaseTrait {
159192
}
160193
}
161194

162-
static DEVICES: RwLock<Vec<Arc<dyn NetworkDevice>>> = RwLock::new(Vec::new());
163-
static DEFAULT_DEVICE: RwLock<Option<Arc<dyn NetworkDevice>>> = RwLock::new(None);
195+
static DEVICES: RwLock<Vec<Arc<NetworkDevice>>> = RwLock::new(Vec::new());
196+
static DEFAULT_DEVICE: RwLock<Option<Arc<NetworkDevice>>> = RwLock::new(None);
164197

165198
fn packet_processor_thread() {
166199
let device = default_device();
@@ -171,7 +204,8 @@ fn packet_processor_thread() {
171204
}
172205
}
173206

174-
pub fn add_device(device: Arc<dyn NetworkDevice>) {
207+
pub fn add_device(device: NetworkDevice) {
208+
let device = Arc::new(device);
175209
DEVICES.write().push(device.clone());
176210

177211
let mut default_device = DEFAULT_DEVICE.write();
@@ -186,7 +220,7 @@ pub fn has_default_device() -> bool {
186220
DEFAULT_DEVICE.read().as_ref().is_some()
187221
}
188222

189-
pub fn default_device() -> Arc<dyn NetworkDevice> {
223+
pub fn default_device() -> Arc<NetworkDevice> {
190224
DEFAULT_DEVICE
191225
.read()
192226
.as_ref()

src/aero_kernel/src/socket/inet.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use aero_syscall::prelude::SIOCGIFHWADDR;
20+
use aero_syscall::prelude::{IfReq, SIOCGIFHWADDR, SIOCSIFADDR};
2121
use aero_syscall::socket::MessageHeader;
2222
use aero_syscall::{IpProtocol, OpenFlags, SocketAddrInet, SocketType, SyscallError};
2323
use alloc::sync::{Arc, Weak};
@@ -28,11 +28,14 @@ use crate::fs::cache::DirCacheItem;
2828
use crate::fs::file_table::FileHandle;
2929
use crate::fs::inode::{FileType, INodeInterface, Metadata, PollFlags};
3030
use crate::fs::{self, FileSystemError};
31+
use crate::mem::paging::VirtAddr;
3132
use crate::net::ip::Ipv4Addr;
3233
use crate::net::udp::{self, Udp, UdpHandler};
3334
use crate::net::{self, MacAddr, Packet, PacketHeader, PacketTrait};
3435
use crate::utils::sync::{BlockQueue, Mutex};
3536

37+
use super::SocketAddr;
38+
3639
#[derive(Default)]
3740
enum SocketState {
3841
/// The socket is not connected.
@@ -234,6 +237,29 @@ impl INodeInterface for InetSocket {
234237
Ok(0)
235238
}
236239

240+
SIOCSIFADDR => {
241+
let ifreq = VirtAddr::new(arg as _)
242+
.read_mut::<IfReq>()
243+
.ok_or(FileSystemError::NotSupported)?;
244+
245+
let name = ifreq.name().ok_or(FileSystemError::InvalidPath)?;
246+
let socket = SocketAddr::from_family(
247+
VirtAddr::new(&ifreq.sa_family as *const _ as _),
248+
ifreq.sa_family,
249+
)
250+
.ok_or(FileSystemError::NotSupported)?
251+
.as_inet()
252+
.ok_or(FileSystemError::NotSupported)?;
253+
254+
// FIXME:
255+
assert!(name == "eth0");
256+
257+
let device = net::default_device();
258+
device.set_ip(Ipv4Addr::new(socket.addr()));
259+
260+
Ok(0)
261+
}
262+
237263
_ => unreachable!("inet::ioctl(): unknown command {command}"),
238264
}
239265
}

src/aero_syscall/src/consts.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20+
use core::ffi::CStr;
21+
2022
use crate::OpenFlags;
2123

2224
// syscall number constants:
@@ -349,3 +351,28 @@ pub struct FramebufferFScreenInfo {
349351

350352
// networking ioctls:
351353
pub const SIOCGIFHWADDR: usize = 0x8927;
354+
pub const SIOCSIFADDR: usize = 0x8916; // set PA address
355+
356+
const IF_NAME_SIZE: usize = 16;
357+
358+
#[repr(C)]
359+
pub struct IfReq {
360+
/// interface name, e.g. "en0"
361+
pub name: [u8; IF_NAME_SIZE],
362+
pub sa_family: u32,
363+
pub sa_data: [u8; 14],
364+
}
365+
366+
impl IfReq {
367+
/// Get the interface name, e.g. "en0". [`None`] is returned if UTF-8
368+
/// validation failed.
369+
pub fn name(&self) -> Option<&str> {
370+
match CStr::from_bytes_until_nul(&self.name) {
371+
Ok(s) => s.to_str(),
372+
// The name does not have a null terminator, that means
373+
// the whole buffer has the name.
374+
Err(_) => core::str::from_utf8(&self.name),
375+
}
376+
.ok()
377+
}
378+
}

0 commit comments

Comments
 (0)