//! Process-scoped UDP port ownership. Caller credentials are trusted kernel //! inputs, not fields an EL0 request may choose. No packet I/O is provided here. #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(u64)] pub enum Error { Configuration=1, Exhausted=2, Credentials=3, Access=4, Address=5, InUse=6, Full=7, Foreign=8, Stale=9 } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Principal { pub table:u64,pub slot:u64,pub generation:u64 } impl Principal { fn valid(self)->bool { self.table!=0&&self.slot<16&&self.generation!=0 } } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Credentials { pub principal:Principal,pub privileged_ports:bool } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Handle { namespace:u64,slot:u64,generation:u64,principal:Principal } impl Handle { pub fn slot(self)->u64{self.slot} pub fn generation(self)->u64{self.generation} pub fn principal(self)->Principal{self.principal} } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Entry { pub live:u64,pub generation:u64,pub principal:Principal,pub address:u64,pub port:u64 } const EMPTY:Entry=Entry{live:0,generation:0,principal:Principal{table:0,slot:0,generation:0},address:0,port:0}; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Snapshot{pub namespace:u64,pub local:u64,pub next_ephemeral:u64,pub entries:[Entry;N]} #[repr(C)] pub struct Bindings { entries:[Entry;N],namespace:u64,local:u64,next_ephemeral:u64 } fn identity()->Result{ use core::sync::atomic::{AtomicU64,Ordering}; static NEXT:AtomicU64=AtomicU64::new(1); NEXT.try_update(Ordering::Relaxed,Ordering::Relaxed,|v|v.checked_add(1)).map_err(|_|Error::Exhausted) } fn unicast(ip:u32)->bool { let first=ip>>24;first!=0&&first!=127&&first<224 } impl Bindings { pub fn new(local:[u8;4])->Result{let local=u32::from_be_bytes(local);if N==0||N>64||!unicast(local){return Err(Error::Configuration);}Ok(Self{entries:[EMPTY;N],namespace:identity()?,local:local as u64,next_ephemeral:49152})} pub fn snapshot(&self)->Snapshot{Snapshot{namespace:self.namespace,local:self.local,next_ephemeral:self.next_ephemeral,entries:self.entries}} /// Wildcard and exact-local binds conflict on the same port. No port sharing /// or SO_REUSEADDR behavior is implied. Port zero requests an ephemeral port. pub fn bind(&mut self,credentials:Credentials,address:[u8;4],port:u16)->Result{ let principal=credentials.principal;if !principal.valid(){return Err(Error::Credentials);} if port!=0&&port<1024&&!credentials.privileged_ports{return Err(Error::Access);} let address=u32::from_be_bytes(address)as u64;if address!=0&&address!=self.local{return Err(Error::Address);} let slot=self.entries.iter().position(|e|e.live==0&&e.generation!=u64::MAX).ok_or(Error::Full)?; let mut selected=u64::from(port);let mut next=self.next_ephemeral; if port==0{let mut found=None;for _ in 0..=N{let probe=next;next=if next==65535{49152}else{next+1};if !self.entries.iter().any(|e|e.live==1&&e.port==probe){found=Some(probe);break;}}selected=found.ok_or(Error::Full)?;} if self.entries.iter().any(|e|e.live==1&&e.port==selected){return Err(Error::InUse);} let generation=self.entries[slot].generation.checked_add(1).ok_or(Error::Exhausted)?; self.entries[slot]=Entry{live:1,generation,principal,address,port:selected};self.next_ephemeral=next;Ok(Handle{namespace:self.namespace,slot:slot as u64,generation,principal}) } pub fn lookup(&self,principal:Principal,handle:Handle)->Result{ if !principal.valid(){return Err(Error::Credentials);} if handle.namespace!=self.namespace||handle.principal!=principal{return Err(Error::Foreign);} let e=self.entries.get(handle.slot as usize).ok_or(Error::Stale)?; if e.live!=1||e.generation!=handle.generation||e.principal!=principal{return Err(Error::Stale);}Ok(*e) } pub fn close(&mut self,principal:Principal,handle:Handle)->Result<(),Error>{let e=self.lookup(principal,handle)?;self.entries[handle.slot as usize]=Entry{generation:e.generation,..EMPTY};Ok(())} /// Kernel exit cleanup uses the actual process generation, so reused process /// slots cannot close a different owner's bindings. pub fn close_principal(&mut self,principal:Principal)->Result{if !principal.valid(){return Err(Error::Credentials);}let mut closed=0;for e in &mut self.entries{if e.live==1&&e.principal==principal{*e=Entry{generation:e.generation,..EMPTY};closed+=1;}}Ok(closed)} }