Skip to content

Commit 320d1ec

Browse files
authored
Merge pull request #88 from ivmarkov/session-eviction
Handle out of sessions and out of exchanges
2 parents 188fe1b + e171e33 commit 320d1ec

File tree

8 files changed

+359
-170
lines changed

8 files changed

+359
-170
lines changed

rs-matter/src/core.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use core::{borrow::Borrow, cell::RefCell};
1919

20+
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, mutex::Mutex};
21+
2022
use crate::{
2123
acl::AclMgr,
2224
data_model::{
@@ -61,6 +63,8 @@ pub struct Matter<'a> {
6163
dev_att: &'a dyn DevAttDataFetcher,
6264
pub(crate) port: u16,
6365
pub(crate) exchanges: RefCell<heapless::Vec<ExchangeCtx, MAX_EXCHANGES>>,
66+
pub(crate) ephemeral: RefCell<Option<ExchangeCtx>>,
67+
pub(crate) ephemeral_mutex: Mutex<NoopRawMutex, ()>,
6468
pub session_mgr: RefCell<SessionMgr>, // Public for tests
6569
}
6670

@@ -108,6 +112,8 @@ impl<'a> Matter<'a> {
108112
dev_att,
109113
port,
110114
exchanges: RefCell::new(heapless::Vec::new()),
115+
ephemeral: RefCell::new(None),
116+
ephemeral_mutex: Mutex::new(()),
111117
session_mgr: RefCell::new(SessionMgr::new(epoch, rand)),
112118
}
113119
}

rs-matter/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub enum ErrorCode {
4747
NoMemory,
4848
NoSession,
4949
NoSpace,
50+
NoSpaceExchanges,
51+
NoSpaceSessions,
5052
NoSpaceAckTable,
5153
NoSpaceRetransTable,
5254
NoTagFound,

rs-matter/src/secure_channel/case.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a> Case<'a> {
9696
) -> Result<(), Error> {
9797
rx.check_proto_opcode(OpCode::CASESigma3 as _)?;
9898

99-
let status = {
99+
let result = {
100100
let fabric_mgr = self.fabric_mgr.borrow();
101101

102102
let fabric = fabric_mgr.get_fabric(case_session.local_fabric_idx)?;
@@ -133,7 +133,7 @@ impl<'a> Case<'a> {
133133

134134
if let Err(e) = Case::validate_certs(fabric, &initiator_noc, initiator_icac_mut) {
135135
error!("Certificate Chain doesn't match: {}", e);
136-
SCStatusCodes::InvalidParameter
136+
Err(SCStatusCodes::InvalidParameter)
137137
} else if let Err(e) = Case::validate_sigma3_sign(
138138
d.initiator_noc.0,
139139
d.initiator_icac.map(|a| a.0),
@@ -142,30 +142,33 @@ impl<'a> Case<'a> {
142142
case_session,
143143
) {
144144
error!("Sigma3 Signature doesn't match: {}", e);
145-
SCStatusCodes::InvalidParameter
145+
Err(SCStatusCodes::InvalidParameter)
146146
} else {
147147
// Only now do we add this message to the TT Hash
148148
let mut peer_catids: NocCatIds = Default::default();
149149
initiator_noc.get_cat_ids(&mut peer_catids);
150150
case_session.tt_hash.update(rx.as_slice())?;
151-
let clone_data = Case::get_session_clone_data(
151+
152+
Ok(Case::get_session_clone_data(
152153
fabric.ipk.op_key(),
153154
fabric.get_node_id(),
154155
initiator_noc.get_node_id()?,
155156
exchange.with_session(|sess| Ok(sess.get_peer_addr()))?,
156157
case_session,
157158
&peer_catids,
158-
)?;
159-
160-
// TODO: Handle NoSpace
161-
exchange
162-
.with_session_mgr_mut(|sess_mgr| sess_mgr.clone_session(&clone_data))?;
163-
164-
SCStatusCodes::SessionEstablishmentSuccess
159+
)?)
165160
}
166161
} else {
167-
SCStatusCodes::NoSharedTrustRoots
162+
Err(SCStatusCodes::NoSharedTrustRoots)
163+
}
164+
};
165+
166+
let status = match result {
167+
Ok(clone_data) => {
168+
exchange.clone_session(tx, &clone_data).await?;
169+
SCStatusCodes::SessionEstablishmentSuccess
168170
}
171+
Err(status) => status,
169172
};
170173

171174
complete_with_status(exchange, tx, status, None).await
@@ -201,7 +204,7 @@ impl<'a> Case<'a> {
201204
return Ok(());
202205
}
203206

204-
let local_sessid = exchange.with_session_mgr_mut(|mgr| Ok(mgr.get_next_sess_id()))?;
207+
let local_sessid = exchange.get_next_sess_id();
205208
case_session.peer_sessid = r.initiator_sessid;
206209
case_session.local_sessid = local_sessid;
207210
case_session.tt_hash.update(rx_buf)?;

rs-matter/src/secure_channel/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ pub fn create_sc_status_report(
7878
// the session will be closed soon
7979
GeneralCode::Success
8080
}
81-
SCStatusCodes::Busy
82-
| SCStatusCodes::InvalidParameter
81+
SCStatusCodes::Busy => GeneralCode::Busy,
82+
SCStatusCodes::InvalidParameter
8383
| SCStatusCodes::NoSharedTrustRoots
8484
| SCStatusCodes::SessionNotFound => GeneralCode::Failure,
8585
};

rs-matter/src/secure_channel/pake.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ impl<'a> Pake<'a> {
167167
self.update_timeout(exchange, tx, true).await?;
168168

169169
let cA = extract_pasepake_1_or_3_params(rx.as_slice())?;
170-
let (status_code, ke) = spake2p.handle_cA(cA);
170+
let (status, ke) = spake2p.handle_cA(cA);
171171

172-
let clone_data = if status_code == SCStatusCodes::SessionEstablishmentSuccess {
172+
let result = if status == SCStatusCodes::SessionEstablishmentSuccess {
173173
// Get the keys
174174
let ke = ke.ok_or(ErrorCode::Invalid)?;
175175
let mut session_keys: [u8; 48] = [0; 48];
@@ -194,22 +194,22 @@ impl<'a> Pake<'a> {
194194
.att_challenge
195195
.copy_from_slice(&session_keys[32..48]);
196196

197-
// Queue a transport mgr request to add a new session
198-
Some(clone_data)
197+
Ok(clone_data)
199198
} else {
200-
None
199+
Err(status)
201200
};
202201

203-
if let Some(clone_data) = clone_data {
204-
// TODO: Handle NoSpace
205-
exchange.with_session_mgr_mut(|sess_mgr| sess_mgr.clone_session(&clone_data))?;
202+
let status = match result {
203+
Ok(clone_data) => {
204+
exchange.clone_session(tx, &clone_data).await?;
205+
self.pase.borrow_mut().disable_pase_session(mdns)?;
206206

207-
self.pase.borrow_mut().disable_pase_session(mdns)?;
208-
}
209-
210-
complete_with_status(exchange, tx, status_code, None).await?;
207+
SCStatusCodes::SessionEstablishmentSuccess
208+
}
209+
Err(status) => status,
210+
};
211211

212-
Ok(())
212+
complete_with_status(exchange, tx, status, None).await
213213
}
214214

215215
#[allow(non_snake_case)]
@@ -273,7 +273,7 @@ impl<'a> Pake<'a> {
273273
let mut our_random: [u8; 32] = [0; 32];
274274
(self.pase.borrow().rand)(&mut our_random);
275275

276-
let local_sessid = exchange.with_session_mgr_mut(|mgr| Ok(mgr.get_next_sess_id()))?;
276+
let local_sessid = exchange.get_next_sess_id();
277277
let spake2p_data: u32 = ((local_sessid as u32) << 16) | a.initiator_ssid as u32;
278278
spake2p.set_app_data(spake2p_data);
279279

0 commit comments

Comments
 (0)