//! Isolated active-open handshake. The transport adapter commits each TX only //! after its actual device completion. This is not a complete TCP endpoint. use super::ipv4_fragments::Datagram as IpDatagram; #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(u64)] pub enum Error{Principal=1,Tuple=2,State=3,Protocol=4,Length=5,Options=6,Payload=7,Flags=8,Checksum=9,Acknowledgment=10,Foreign=11,Exhausted=12} #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Principal{pub table:u64,pub slot:u64,pub generation:u64} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Tuple{local:[u8;4],remote:[u8;4],local_port:u16,remote_port:u16} fn unicast(ip:[u8;4])->bool{ip[0]!=0&&ip[0]!=127&&ip[0]<224} impl Tuple{ pub fn checked(local:[u8;4],local_port:u16,remote:[u8;4],remote_port:u16)->Result{if !unicast(local)||!unicast(remote)||local==remote||local_port<1024||remote_port==0{return Err(Error::Tuple);}Ok(Self{local,remote,local_port,remote_port})} pub fn local(self)->[u8;4]{self.local}pub fn remote(self)->[u8;4]{self.remote}pub fn local_port(self)->u16{self.local_port}pub fn remote_port(self)->u16{self.remote_port} } #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Snapshot{pub identity:u64,pub principal:Principal,pub local:u64,pub remote:u64,pub local_port:u64,pub remote_port:u64,pub phase:u64,pub iss:u64,pub snd_una:u64,pub snd_nxt:u64,pub irs:u64,pub rcv_nxt:u64,pub peer_window:u64,pub tx_completed:u64,pub accepted:u64} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Control{identity:u64,kind:u64,bytes:[u8;20]} impl Control{pub fn bytes(&self)->&[u8;20]{&self.bytes}} #[repr(C)] pub struct Session{state:Snapshot} #[unsafe(no_mangle)]pub static mut OS_K1885_NONCE_MASK:u64=0; fn identity()->Result{use core::ptr::{read_volatile,write_volatile};static mut NEXT:u64=1;let cpu:u64;let daif:u64;unsafe{core::arch::asm!("mrs {cpu}, MPIDR_EL1","mrs {daif}, DAIF",cpu=out(reg)cpu,daif=out(reg)daif,options(nomem,nostack,preserves_flags));}if cpu&0xff!=0||daif&0x3c0!=0x3c0{return Err(Error::State);}unsafe{let value=read_volatile(core::ptr::addr_of!(NEXT));let next=value.checked_add(1).ok_or(Error::Exhausted)?;write_volatile(core::ptr::addr_of_mut!(NEXT),next);write_volatile(core::ptr::addr_of_mut!(OS_K1885_NONCE_MASK),daif&0x3c0);Ok(value)}} fn sum(bytes:&[u8])->u32{bytes.chunks(2).map(|p|(u32::from(p[0])<<8)|u32::from(*p.get(1).unwrap_or(&0))).sum()} fn folded(mut sum:u32)->u16{while sum>>16!=0{sum=(sum&65535)+(sum>>16);}sum as u16} impl Session{ /// ISS is supplied by the trusted caller. Fixtures are deterministic; this /// module does not provide secure ISN selection or global TCP tuple custody. pub fn new(principal:Principal,tuple:Tuple,iss:u32)->Result{if principal.table==0||principal.generation==0||principal.slot>=64{return Err(Error::Principal);}Ok(Self{state:Snapshot{identity:identity()?,principal,local:u32::from_be_bytes(tuple.local)as u64,remote:u32::from_be_bytes(tuple.remote)as u64,local_port:tuple.local_port as u64,remote_port:tuple.remote_port as u64,phase:0,iss:iss as u64,snd_una:iss as u64,snd_nxt:iss as u64,irs:0,rcv_nxt:0,peer_window:0,tx_completed:0,accepted:0}})} pub fn snapshot(&self)->Snapshot{self.state} fn owner(&self,p:Principal)->Result<(),Error>{if p!=self.state.principal{return Err(Error::Principal);}Ok(())} fn packet(&self,kind:u64,sequence:u32,ack:u32,flags:u8)->Control{ let mut bytes=[0u8;20];bytes[..2].copy_from_slice(&(self.state.local_port as u16).to_be_bytes());bytes[2..4].copy_from_slice(&(self.state.remote_port as u16).to_be_bytes());bytes[4..8].copy_from_slice(&sequence.to_be_bytes());bytes[8..12].copy_from_slice(&ack.to_be_bytes());bytes[12]=0x50;bytes[13]=flags;bytes[14..16].copy_from_slice(&512u16.to_be_bytes()); let checksum=!folded(sum(&(self.state.local as u32).to_be_bytes())+sum(&(self.state.remote as u32).to_be_bytes())+6+20+sum(&bytes));bytes[16..18].copy_from_slice(&checksum.to_be_bytes());Control{identity:self.state.identity,kind,bytes} } pub fn begin(&mut self,principal:Principal)->Result{self.owner(principal)?;if self.state.phase!=0{return Err(Error::State);}let syn=self.packet(1,self.state.iss as u32,0,2);self.state.phase=1;Ok(syn)} /// Pending SYN (1) -> SYN sent (2); pending ACK (3) -> established (4). /// A copied completion token cannot complete a different session or phase. pub fn complete(&mut self,principal:Principal,control:Control)->Result<(),Error>{self.owner(principal)?;if control.identity!=self.state.identity{return Err(Error::Foreign);}let phase=match(control.kind,self.state.phase){(1,1)=>2,(2,3)=>4,_=>return Err(Error::State)};let count=self.state.tx_completed.checked_add(1).ok_or(Error::Exhausted)?;self.state.phase=phase;self.state.tx_completed=count;if phase==2{self.state.snd_nxt=(self.state.iss as u32).wrapping_add(1)as u64;}Ok(())} pub fn receive(&mut self,principal:Principal,datagram:IpDatagram<'_>)->Result{ self.owner(principal)?;if self.state.phase!=2{return Err(Error::State);}let ip=datagram.packet();if ip.protocol()!=6{return Err(Error::Protocol);}if u32::from_be_bytes(ip.source())as u64!=self.state.remote||u32::from_be_bytes(ip.destination())as u64!=self.state.local{return Err(Error::Tuple);} let b=datagram.payload();if b.len()<20{return Err(Error::Length);}if u16::from_be_bytes([b[0],b[1]])as u64!=self.state.remote_port||u16::from_be_bytes([b[2],b[3]])as u64!=self.state.local_port{return Err(Error::Tuple);} let header=(b[12]>>4)as usize*4;if header<20||header>b.len(){return Err(Error::Length);}if header!=20{return Err(Error::Options);}if b.len()!=header{return Err(Error::Payload);}if b[12]&15!=0||b[13]!=0x12||b[18]!=0||b[19]!=0{return Err(Error::Flags);} if folded(sum(&ip.source())+sum(&ip.destination())+6+b.len()as u32+sum(b))!=65535{return Err(Error::Checksum);} let ack=u32::from_be_bytes(b[8..12].try_into().unwrap());if ack!=self.state.snd_nxt as u32{return Err(Error::Acknowledgment);} let seq=u32::from_be_bytes(b[4..8].try_into().unwrap());let next=seq.wrapping_add(1);let accepted=self.state.accepted.checked_add(1).ok_or(Error::Exhausted)?; let control=self.packet(2,self.state.snd_nxt as u32,next,0x10);self.state.phase=3;self.state.snd_una=ack as u64;self.state.irs=seq as u64;self.state.rcv_nxt=next as u64;self.state.peer_window=u16::from_be_bytes([b[14],b[15]])as u64;self.state.accepted=accepted;Ok(control) } } impl Session{pub const fn os_k1885_physical_layout()->[u64;16]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Snapshot,identity)as u64,offset_of!(Snapshot,principal)as u64,offset_of!(Snapshot,local)as u64,offset_of!(Snapshot,remote)as u64,offset_of!(Snapshot,local_port)as u64,offset_of!(Snapshot,remote_port)as u64,offset_of!(Snapshot,phase)as u64,offset_of!(Snapshot,iss)as u64,offset_of!(Snapshot,snd_una)as u64,offset_of!(Snapshot,snd_nxt)as u64,offset_of!(Snapshot,irs)as u64,offset_of!(Snapshot,rcv_nxt)as u64,offset_of!(Snapshot,peer_window)as u64,offset_of!(Snapshot,tx_completed)as u64,offset_of!(Snapshot,accepted)as u64]}}