Orbital (#16)
* Port previous ethernet scheme * Add ipd * Fix initfs rebuilds, use QEMU user networking addresses in ipd * Add tcp/udp, netutils, dns, and network config * Add fsync to network driver * Add dns, router, subnet by default * Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks * Add orbital server, WIP * Add futex * Add orbutils and orbital * Update libstd, orbutils, and orbital Move ANSI key encoding to vesad * Add orbital assets * Update orbital * Update to add login manager * Add blocking primitives, block for most things except waitpid, update orbital * Wait in waitpid and IRQ, improvements for other waits * Fevent in root scheme * WIP: Switch to using fevent * Reorganize * Event based e1000d driver * Superuser-only access to some network schemes, display, and disk * Superuser root and irq schemes * Fix orbital
This commit is contained in:
parent
372d44f88c
commit
224c43f761
92 changed files with 3415 additions and 473 deletions
275
schemes/ipd/src/common.rs
Normal file
275
schemes/ipd/src/common.rs
Normal file
|
@ -0,0 +1,275 @@
|
|||
use std::{mem, slice, u8, u16};
|
||||
|
||||
pub static mut MAC_ADDR: MacAddr = MacAddr { bytes: [0x50, 0x51, 0x52, 0x53, 0x54, 0x55] };
|
||||
pub static BROADCAST_MAC_ADDR: MacAddr = MacAddr { bytes: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] };
|
||||
|
||||
pub static mut IP_ADDR: Ipv4Addr = Ipv4Addr { bytes: [10, 0, 2, 15] };
|
||||
pub static mut IP_ROUTER_ADDR: Ipv4Addr = Ipv4Addr { bytes: [10, 0, 2, 2] };
|
||||
pub static mut IP_SUBNET: Ipv4Addr = Ipv4Addr { bytes: [255, 255, 255, 0] };
|
||||
pub static BROADCAST_IP_ADDR: Ipv4Addr = Ipv4Addr { bytes: [255, 255, 255, 255] };
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(packed)]
|
||||
pub struct n16(u16);
|
||||
|
||||
impl n16 {
|
||||
pub fn new(value: u16) -> Self {
|
||||
n16(value.to_be())
|
||||
}
|
||||
|
||||
pub fn get(&self) -> u16 {
|
||||
u16::from_be(self.0)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, value: u16) {
|
||||
self.0 = value.to_be();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct MacAddr {
|
||||
pub bytes: [u8; 6],
|
||||
}
|
||||
|
||||
impl MacAddr {
|
||||
pub fn equals(&self, other: Self) -> bool {
|
||||
for i in 0..6 {
|
||||
if self.bytes[i] != other.bytes[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn from_str(string: &str) -> Self {
|
||||
let mut addr = MacAddr { bytes: [0, 0, 0, 0, 0, 0] };
|
||||
|
||||
let mut i = 0;
|
||||
for part in string.split('.') {
|
||||
let octet = u8::from_str_radix(part, 16).unwrap_or(0);
|
||||
match i {
|
||||
0 => addr.bytes[0] = octet,
|
||||
1 => addr.bytes[1] = octet,
|
||||
2 => addr.bytes[2] = octet,
|
||||
3 => addr.bytes[3] = octet,
|
||||
4 => addr.bytes[4] = octet,
|
||||
5 => addr.bytes[5] = octet,
|
||||
_ => break,
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
addr
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let mut string = String::new();
|
||||
for i in 0..6 {
|
||||
if i > 0 {
|
||||
string.push('.');
|
||||
}
|
||||
string.push_str(&format!("{:X}", self.bytes[i]));
|
||||
}
|
||||
string
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Ipv4Addr {
|
||||
pub bytes: [u8; 4],
|
||||
}
|
||||
|
||||
impl Ipv4Addr {
|
||||
pub fn equals(&self, other: Self) -> bool {
|
||||
for i in 0..4 {
|
||||
if self.bytes[i] != other.bytes[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn from_str(string: &str) -> Self {
|
||||
let mut addr = Ipv4Addr { bytes: [0, 0, 0, 0] };
|
||||
|
||||
let mut i = 0;
|
||||
for part in string.split('.') {
|
||||
let octet = part.parse::<u8>().unwrap_or(0);
|
||||
match i {
|
||||
0 => addr.bytes[0] = octet,
|
||||
1 => addr.bytes[1] = octet,
|
||||
2 => addr.bytes[2] = octet,
|
||||
3 => addr.bytes[3] = octet,
|
||||
_ => break,
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
addr
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let mut string = String::new();
|
||||
|
||||
for i in 0..4 {
|
||||
if i > 0 {
|
||||
string = string + ".";
|
||||
}
|
||||
string = string + &format!("{}", self.bytes[i]);
|
||||
}
|
||||
|
||||
string
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Checksum {
|
||||
pub data: u16,
|
||||
}
|
||||
|
||||
impl Checksum {
|
||||
pub unsafe fn check(&self, mut ptr: usize, mut len: usize) -> bool {
|
||||
let mut sum: usize = 0;
|
||||
while len > 1 {
|
||||
sum += *(ptr as *const u16) as usize;
|
||||
len -= 2;
|
||||
ptr += 2;
|
||||
}
|
||||
|
||||
if len > 0 {
|
||||
sum += *(ptr as *const u8) as usize;
|
||||
}
|
||||
|
||||
while (sum >> 16) > 0 {
|
||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||
}
|
||||
|
||||
sum == 0xFFFF
|
||||
}
|
||||
|
||||
pub unsafe fn calculate(&mut self, ptr: usize, len: usize) {
|
||||
self.data = 0;
|
||||
|
||||
let sum = Checksum::sum(ptr, len);
|
||||
|
||||
self.data = Checksum::compile(sum);
|
||||
}
|
||||
|
||||
pub unsafe fn sum(mut ptr: usize, mut len: usize) -> usize {
|
||||
let mut sum = 0;
|
||||
|
||||
while len > 1 {
|
||||
sum += *(ptr as *const u16) as usize;
|
||||
len -= 2;
|
||||
ptr += 2;
|
||||
}
|
||||
|
||||
if len > 0 {
|
||||
sum += *(ptr as *const u8) as usize;
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
pub fn compile(mut sum: usize) -> u16 {
|
||||
while (sum >> 16) > 0 {
|
||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||
}
|
||||
|
||||
0xFFFF - (sum as u16)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(packed)]
|
||||
pub struct ArpHeader {
|
||||
pub htype: n16,
|
||||
pub ptype: n16,
|
||||
pub hlen: u8,
|
||||
pub plen: u8,
|
||||
pub oper: n16,
|
||||
pub src_mac: MacAddr,
|
||||
pub src_ip: Ipv4Addr,
|
||||
pub dst_mac: MacAddr,
|
||||
pub dst_ip: Ipv4Addr,
|
||||
}
|
||||
|
||||
pub struct Arp {
|
||||
pub header: ArpHeader,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Arp {
|
||||
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
||||
if bytes.len() >= mem::size_of::<ArpHeader>() {
|
||||
unsafe {
|
||||
return Some(Arp {
|
||||
header: *(bytes.as_ptr() as *const ArpHeader),
|
||||
data: bytes[mem::size_of::<ArpHeader>() ..].to_vec(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
unsafe {
|
||||
let header_ptr: *const ArpHeader = &self.header;
|
||||
let mut ret = Vec::from(slice::from_raw_parts(header_ptr as *const u8,
|
||||
mem::size_of::<ArpHeader>()));
|
||||
ret.extend_from_slice(&self.data);
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(packed)]
|
||||
pub struct Ipv4Header {
|
||||
pub ver_hlen: u8,
|
||||
pub services: u8,
|
||||
pub len: n16,
|
||||
pub id: n16,
|
||||
pub flags_fragment: n16,
|
||||
pub ttl: u8,
|
||||
pub proto: u8,
|
||||
pub checksum: Checksum,
|
||||
pub src: Ipv4Addr,
|
||||
pub dst: Ipv4Addr,
|
||||
}
|
||||
|
||||
pub struct Ipv4 {
|
||||
pub header: Ipv4Header,
|
||||
pub options: Vec<u8>,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Ipv4 {
|
||||
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
||||
if bytes.len() >= mem::size_of::<Ipv4Header>() {
|
||||
unsafe {
|
||||
let header = *(bytes.as_ptr() as *const Ipv4Header);
|
||||
let header_len = ((header.ver_hlen & 0xF) << 2) as usize;
|
||||
|
||||
return Some(Ipv4 {
|
||||
header: header,
|
||||
options: bytes[mem::size_of::<Ipv4Header>() .. header_len].to_vec(),
|
||||
data: bytes[header_len .. header.len.get() as usize].to_vec(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
unsafe {
|
||||
let header_ptr: *const Ipv4Header = &self.header;
|
||||
let mut ret = Vec::<u8>::from(slice::from_raw_parts(header_ptr as *const u8,
|
||||
mem::size_of::<Ipv4Header>()));
|
||||
ret.extend_from_slice(&self.options);
|
||||
ret.extend_from_slice(&self.data);
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
30
schemes/ipd/src/main.rs
Normal file
30
schemes/ipd/src/main.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
#![feature(rand)]
|
||||
|
||||
extern crate resource_scheme;
|
||||
extern crate syscall;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::thread;
|
||||
|
||||
use resource_scheme::ResourceScheme;
|
||||
use syscall::Packet;
|
||||
|
||||
use scheme::IpScheme;
|
||||
|
||||
pub mod common;
|
||||
pub mod resource;
|
||||
pub mod scheme;
|
||||
|
||||
fn main() {
|
||||
thread::spawn(move || {
|
||||
let mut socket = File::create(":ip").expect("ipd: failed to create ip scheme");
|
||||
let scheme = IpScheme::new();
|
||||
loop {
|
||||
let mut packet = Packet::default();
|
||||
socket.read(&mut packet).expect("ipd: failed to read events from ip scheme");
|
||||
scheme.handle(&mut packet);
|
||||
socket.write(&packet).expect("ipd: failed to write responses to ip scheme");
|
||||
}
|
||||
});
|
||||
}
|
114
schemes/ipd/src/resource.rs
Normal file
114
schemes/ipd/src/resource.rs
Normal file
|
@ -0,0 +1,114 @@
|
|||
use std::{cmp, mem};
|
||||
|
||||
use resource_scheme::Resource;
|
||||
use syscall;
|
||||
use syscall::error::*;
|
||||
|
||||
use common::{n16, Ipv4Addr, Checksum, Ipv4Header, Ipv4, IP_ADDR, BROADCAST_IP_ADDR};
|
||||
|
||||
/// A IP (internet protocole) resource
|
||||
pub struct IpResource {
|
||||
pub link: usize,
|
||||
pub data: Vec<u8>,
|
||||
pub peer_addr: Ipv4Addr,
|
||||
pub proto: u8,
|
||||
pub id: u16,
|
||||
}
|
||||
|
||||
impl Resource for IpResource {
|
||||
fn dup(&self) -> Result<Box<Self>> {
|
||||
let link = try!(syscall::dup(self.link));
|
||||
Ok(Box::new(IpResource {
|
||||
link: link,
|
||||
data: self.data.clone(),
|
||||
peer_addr: self.peer_addr,
|
||||
proto: self.proto,
|
||||
id: self.id,
|
||||
}))
|
||||
}
|
||||
|
||||
fn path(&self, buf: &mut [u8]) -> Result<usize> {
|
||||
let path_string = format!("ip:{}/{:X}", self.peer_addr.to_string(), self.proto);
|
||||
let path = path_string.as_bytes();
|
||||
|
||||
for (b, p) in buf.iter_mut().zip(path.iter()) {
|
||||
*b = *p;
|
||||
}
|
||||
|
||||
Ok(cmp::min(buf.len(), path.len()))
|
||||
}
|
||||
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
if !self.data.is_empty() {
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
mem::swap(&mut self.data, &mut data);
|
||||
|
||||
for (b, d) in buf.iter_mut().zip(data.iter()) {
|
||||
*b = *d;
|
||||
}
|
||||
|
||||
return Ok(cmp::min(buf.len(), data.len()));
|
||||
}
|
||||
|
||||
let mut bytes = [0; 65536];
|
||||
let count = try!(syscall::read(self.link, &mut bytes));
|
||||
|
||||
if let Some(packet) = Ipv4::from_bytes(&bytes[..count]) {
|
||||
if packet.header.proto == self.proto &&
|
||||
(packet.header.dst.equals(unsafe { IP_ADDR }) || packet.header.dst.equals(BROADCAST_IP_ADDR)) &&
|
||||
(packet.header.src.equals(self.peer_addr) || self.peer_addr.equals(BROADCAST_IP_ADDR)) {
|
||||
for (b, d) in buf.iter_mut().zip(packet.data.iter()) {
|
||||
*b = *d;
|
||||
}
|
||||
|
||||
return Ok(cmp::min(buf.len(), packet.data.len()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
let ip_data = Vec::from(buf);
|
||||
|
||||
self.id += 1;
|
||||
let mut ip = Ipv4 {
|
||||
header: Ipv4Header {
|
||||
ver_hlen: 0x40 | (mem::size_of::<Ipv4Header>() / 4 & 0xF) as u8, // No Options
|
||||
services: 0,
|
||||
len: n16::new((mem::size_of::<Ipv4Header>() + ip_data.len()) as u16), // No Options
|
||||
id: n16::new(self.id),
|
||||
flags_fragment: n16::new(0),
|
||||
ttl: 128,
|
||||
proto: self.proto,
|
||||
checksum: Checksum { data: 0 },
|
||||
src: unsafe { IP_ADDR },
|
||||
dst: self.peer_addr,
|
||||
},
|
||||
options: Vec::new(),
|
||||
data: ip_data,
|
||||
};
|
||||
|
||||
unsafe {
|
||||
let header_ptr: *const Ipv4Header = &ip.header;
|
||||
ip.header.checksum.data =
|
||||
Checksum::compile(Checksum::sum(header_ptr as usize, mem::size_of::<Ipv4Header>()) +
|
||||
Checksum::sum(ip.options.as_ptr() as usize, ip.options.len()));
|
||||
}
|
||||
|
||||
match syscall::write(self.link, &ip.to_bytes()) {
|
||||
Ok(_) => Ok(buf.len()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn sync(&mut self) -> Result<usize> {
|
||||
syscall::fsync(self.link)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for IpResource {
|
||||
fn drop(&mut self) {
|
||||
let _ = syscall::close(self.link);
|
||||
}
|
||||
}
|
155
schemes/ipd/src/scheme.rs
Normal file
155
schemes/ipd/src/scheme.rs
Normal file
|
@ -0,0 +1,155 @@
|
|||
use std::cell::RefCell;
|
||||
use std::rand;
|
||||
use std::{str, u16};
|
||||
|
||||
use resource_scheme::ResourceScheme;
|
||||
use syscall;
|
||||
use syscall::error::{Error, Result, EACCES, ENOENT, EINVAL};
|
||||
use syscall::flag::O_RDWR;
|
||||
|
||||
use common::{n16, MacAddr, Ipv4Addr, ArpHeader, Arp, Ipv4, MAC_ADDR, BROADCAST_MAC_ADDR, BROADCAST_IP_ADDR, IP_ADDR, IP_ROUTER_ADDR, IP_SUBNET};
|
||||
use resource::IpResource;
|
||||
|
||||
/// A ARP entry (MAC + IP)
|
||||
pub struct ArpEntry {
|
||||
ip: Ipv4Addr,
|
||||
mac: MacAddr,
|
||||
}
|
||||
|
||||
/// A IP scheme
|
||||
pub struct IpScheme {
|
||||
pub arp: RefCell<Vec<ArpEntry>>,
|
||||
}
|
||||
|
||||
impl IpScheme {
|
||||
pub fn new() -> IpScheme {
|
||||
IpScheme {
|
||||
arp: RefCell::new(Vec::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResourceScheme<IpResource> for IpScheme {
|
||||
fn open_resource(&self, url: &[u8], _flags: usize, uid: u32, _gid: u32) -> Result<Box<IpResource>> {
|
||||
if uid == 0 {
|
||||
let path = try!(str::from_utf8(url).or(Err(Error::new(EINVAL))));
|
||||
let mut parts = path.split('/');
|
||||
if let Some(host_string) = parts.next() {
|
||||
if let Some(proto_string) = parts.next() {
|
||||
let proto = u8::from_str_radix(proto_string, 16).unwrap_or(0);
|
||||
|
||||
if ! host_string.is_empty() {
|
||||
let peer_addr = Ipv4Addr::from_str(host_string);
|
||||
let mut route_mac = BROADCAST_MAC_ADDR;
|
||||
|
||||
if ! peer_addr.equals(BROADCAST_IP_ADDR) {
|
||||
let mut needs_routing = false;
|
||||
|
||||
for octet in 0..4 {
|
||||
let me = unsafe { IP_ADDR.bytes[octet] };
|
||||
let mask = unsafe { IP_SUBNET.bytes[octet] };
|
||||
let them = peer_addr.bytes[octet];
|
||||
if me & mask != them & mask {
|
||||
needs_routing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let route_addr = if needs_routing {
|
||||
unsafe { IP_ROUTER_ADDR }
|
||||
} else {
|
||||
peer_addr
|
||||
};
|
||||
|
||||
for entry in self.arp.borrow().iter() {
|
||||
if entry.ip.equals(route_addr) {
|
||||
route_mac = entry.mac;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if route_mac.equals(BROADCAST_MAC_ADDR) {
|
||||
if let Ok(link) = syscall::open(&format!("ethernet:{}/806", &route_mac.to_string()), O_RDWR) {
|
||||
let arp = Arp {
|
||||
header: ArpHeader {
|
||||
htype: n16::new(1),
|
||||
ptype: n16::new(0x800),
|
||||
hlen: 6,
|
||||
plen: 4,
|
||||
oper: n16::new(1),
|
||||
src_mac: unsafe { MAC_ADDR },
|
||||
src_ip: unsafe { IP_ADDR },
|
||||
dst_mac: route_mac,
|
||||
dst_ip: route_addr,
|
||||
},
|
||||
data: Vec::new(),
|
||||
};
|
||||
|
||||
match syscall::write(link, &arp.to_bytes()) {
|
||||
Ok(_) => loop {
|
||||
let mut bytes = [0; 65536];
|
||||
match syscall::read(link, &mut bytes) {
|
||||
Ok(count) => if let Some(packet) = Arp::from_bytes(&bytes[..count]) {
|
||||
if packet.header.oper.get() == 2 &&
|
||||
packet.header.src_ip.equals(route_addr) {
|
||||
route_mac = packet.header.src_mac;
|
||||
self.arp.borrow_mut().push(ArpEntry {
|
||||
ip: route_addr,
|
||||
mac: route_mac,
|
||||
});
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(_) => (),
|
||||
}
|
||||
},
|
||||
Err(err) => println!("IP: ARP Write Failed: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(link) = syscall::open(&format!("ethernet:{}/800", &route_mac.to_string()), O_RDWR) {
|
||||
return Ok(Box::new(IpResource {
|
||||
link: link,
|
||||
data: Vec::new(),
|
||||
peer_addr: peer_addr,
|
||||
proto: proto,
|
||||
id: (rand() % 65536) as u16,
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
while let Ok(link) = syscall::open("ethernet:/800", O_RDWR) {
|
||||
let mut bytes = [0; 65536];
|
||||
match syscall::read(link, &mut bytes) {
|
||||
Ok(count) => {
|
||||
if let Some(packet) = Ipv4::from_bytes(&bytes[..count]) {
|
||||
if packet.header.proto == proto &&
|
||||
(packet.header.dst.equals(unsafe { IP_ADDR }) || packet.header.dst.equals(BROADCAST_IP_ADDR)) {
|
||||
return Ok(Box::new(IpResource {
|
||||
link: link,
|
||||
data: packet.data,
|
||||
peer_addr: packet.header.src,
|
||||
proto: proto,
|
||||
id: (rand() % 65536) as u16,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("IP: No protocol provided");
|
||||
}
|
||||
} else {
|
||||
println!("IP: No host provided");
|
||||
}
|
||||
|
||||
Err(Error::new(ENOENT))
|
||||
} else {
|
||||
Err(Error::new(EACCES))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue