From 9053166acc8ffe38d74b60dba396225d4e648026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Stein?= Date: Fri, 18 Oct 2019 00:03:17 +0200 Subject: [PATCH] eth: increase desc list safety --- src/eth/rx.rs | 7 ++++--- src/eth/tx.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/eth/rx.rs b/src/eth/rx.rs index 309a0ba..9022ff9 100644 --- a/src/eth/rx.rs +++ b/src/eth/rx.rs @@ -69,7 +69,8 @@ impl<'a> DescList<'a> { } DescList { - list, + // Shorten the list of descriptors to the required number. + list: &mut list[0..=last], buffers, next: 0, } @@ -85,7 +86,7 @@ impl<'a> DescList<'a> { if entry.word0.read().used() { let word1 = entry.word1.read(); let len = word1.frame_length_lsbs().into(); - let buffer = &self.buffers[self.next][0..len]; + let buffer = &mut self.buffers[self.next][0..len]; self.next += 1; if self.next >= list_len { @@ -107,7 +108,7 @@ impl<'a> DescList<'a> { /// Releases a buffer back to the HW upon Drop pub struct PktRef<'a> { entry: &'a mut DescEntry, - buffer: &'a [u8], + buffer: &'a mut [u8], } impl<'a> Drop for PktRef<'a> { diff --git a/src/eth/tx.rs b/src/eth/tx.rs index 8852bfe..4bf0185 100644 --- a/src/eth/tx.rs +++ b/src/eth/tx.rs @@ -40,6 +40,12 @@ pub struct DescList<'a> { impl<'a> DescList<'a> { pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [[u8; MTU]]) -> Self { let last = list.len().min(buffers.len()) - 1; + // Sending seems to not work properly with only one packet + // buffer (two duplicates get send with every packet), so + // check that at least 2 are allocated, i.e. that the index of + // the last one is at least one. + assert!(last > 0); + for (i, (entry, buffer)) in list.iter_mut().zip(buffers.iter_mut()).enumerate() { let is_last = i == last; let buffer_addr = &mut buffer[0] as *mut _ as u32; @@ -58,7 +64,8 @@ impl<'a> DescList<'a> { } DescList { - list, + // Shorten the list of descriptors to the required number. + list: &mut list[0..=last], buffers, next: 0, }