浏览代码

Merge #759

759: Put IEEE802154 in separate iface module r=thvdveld a=thvdveld

And thus splitting IEEE802154 from 6LoWPAN.

Co-authored-by: Thibaut Vandervelden <thvdveld@vub.be>
bors[bot] 2 年之前
父节点
当前提交
bd2086527b
共有 3 个文件被更改,包括 112 次插入90 次删除
  1. 90 0
      src/iface/interface/ieee802154.rs
  2. 4 2
      src/iface/interface/mod.rs
  3. 18 88
      src/iface/interface/sixlowpan.rs

+ 90 - 0
src/iface/interface/ieee802154.rs

@@ -0,0 +1,90 @@
+use super::*;
+
+use crate::phy::TxToken;
+use crate::wire::*;
+
+impl InterfaceInner {
+    pub(super) fn process_ieee802154<'output, 'payload: 'output, T: AsRef<[u8]> + ?Sized>(
+        &mut self,
+        sockets: &mut SocketSet,
+        sixlowpan_payload: &'payload T,
+        _fragments: &'output mut FragmentsBuffer,
+    ) -> Option<IpPacket<'output>> {
+        let ieee802154_frame = check!(Ieee802154Frame::new_checked(sixlowpan_payload));
+        let ieee802154_repr = check!(Ieee802154Repr::parse(&ieee802154_frame));
+
+        if ieee802154_repr.frame_type != Ieee802154FrameType::Data {
+            return None;
+        }
+
+        // Drop frames when the user has set a PAN id and the PAN id from frame is not equal to this
+        // When the user didn't set a PAN id (so it is None), then we accept all PAN id's.
+        // We always accept the broadcast PAN id.
+        if self.pan_id.is_some()
+            && ieee802154_repr.dst_pan_id != self.pan_id
+            && ieee802154_repr.dst_pan_id != Some(Ieee802154Pan::BROADCAST)
+        {
+            net_debug!(
+                "IEEE802.15.4: dropping {:?} because not our PAN id (or not broadcast)",
+                ieee802154_repr
+            );
+            return None;
+        }
+
+        match ieee802154_frame.payload() {
+            Some(payload) => self.process_sixlowpan(sockets, &ieee802154_repr, payload, _fragments),
+            None => None,
+        }
+    }
+
+    pub(super) fn dispatch_ieee802154<Tx: TxToken>(
+        &mut self,
+        ll_dst_a: Ieee802154Address,
+        tx_token: Tx,
+        packet: IpPacket,
+        frag: &mut Fragmenter,
+    ) {
+        let ll_src_a = self.hardware_addr.unwrap().ieee802154_or_panic();
+
+        // Create the IEEE802.15.4 header.
+        let ieee_repr = Ieee802154Repr {
+            frame_type: Ieee802154FrameType::Data,
+            security_enabled: false,
+            frame_pending: false,
+            ack_request: false,
+            sequence_number: Some(self.get_sequence_number()),
+            pan_id_compression: true,
+            frame_version: Ieee802154FrameVersion::Ieee802154_2003,
+            dst_pan_id: self.pan_id,
+            dst_addr: Some(ll_dst_a),
+            src_pan_id: self.pan_id,
+            src_addr: Some(ll_src_a),
+        };
+
+        self.dispatch_sixlowpan(tx_token, packet, ieee_repr, frag);
+    }
+
+    #[cfg(feature = "proto-sixlowpan-fragmentation")]
+    pub(super) fn dispatch_ieee802154_frag<Tx: TxToken>(
+        &mut self,
+        tx_token: Tx,
+        frag: &mut Fragmenter,
+    ) {
+        // Create the IEEE802.15.4 header.
+        let ieee_repr = Ieee802154Repr {
+            frame_type: Ieee802154FrameType::Data,
+            security_enabled: false,
+            frame_pending: false,
+            ack_request: false,
+            sequence_number: Some(self.get_sequence_number()),
+            pan_id_compression: true,
+            frame_version: Ieee802154FrameVersion::Ieee802154_2003,
+            dst_pan_id: self.pan_id,
+            dst_addr: Some(frag.sixlowpan.ll_dst_addr),
+            src_pan_id: self.pan_id,
+            src_addr: Some(frag.sixlowpan.ll_src_addr),
+        };
+
+        self.dispatch_sixlowpan_frag(tx_token, ieee_repr, frag);
+    }
+}

+ 4 - 2
src/iface/interface/mod.rs

@@ -7,13 +7,15 @@ mod tests;
 
 #[cfg(feature = "medium-ethernet")]
 mod ethernet;
-#[cfg(feature = "proto-sixlowpan")]
-mod sixlowpan;
+#[cfg(feature = "medium-ieee802154")]
+mod ieee802154;
 
 #[cfg(feature = "proto-ipv4")]
 mod ipv4;
 #[cfg(feature = "proto-ipv6")]
 mod ipv6;
+#[cfg(feature = "proto-sixlowpan")]
+mod sixlowpan;
 
 #[cfg(feature = "proto-igmp")]
 mod igmp;

+ 18 - 88
src/iface/interface/sixlowpan.rs

@@ -1,7 +1,6 @@
 use super::*;
 
 use crate::phy::ChecksumCapabilities;
-use crate::phy::TxToken;
 use crate::wire::*;
 
 // Max len of non-fragmented packets after decompression (including ipv6 header and payload)
@@ -9,40 +8,6 @@ use crate::wire::*;
 pub(crate) const MAX_DECOMPRESSED_LEN: usize = 1500;
 
 impl InterfaceInner {
-    #[cfg(feature = "medium-ieee802154")]
-    pub(super) fn process_ieee802154<'output, 'payload: 'output, T: AsRef<[u8]> + ?Sized>(
-        &mut self,
-        sockets: &mut SocketSet,
-        sixlowpan_payload: &'payload T,
-        _fragments: &'output mut FragmentsBuffer,
-    ) -> Option<IpPacket<'output>> {
-        let ieee802154_frame = check!(Ieee802154Frame::new_checked(sixlowpan_payload));
-        let ieee802154_repr = check!(Ieee802154Repr::parse(&ieee802154_frame));
-
-        if ieee802154_repr.frame_type != Ieee802154FrameType::Data {
-            return None;
-        }
-
-        // Drop frames when the user has set a PAN id and the PAN id from frame is not equal to this
-        // When the user didn't set a PAN id (so it is None), then we accept all PAN id's.
-        // We always accept the broadcast PAN id.
-        if self.pan_id.is_some()
-            && ieee802154_repr.dst_pan_id != self.pan_id
-            && ieee802154_repr.dst_pan_id != Some(Ieee802154Pan::BROADCAST)
-        {
-            net_debug!(
-                "IEEE802.15.4: dropping {:?} because not our PAN id (or not broadcast)",
-                ieee802154_repr
-            );
-            return None;
-        }
-
-        match ieee802154_frame.payload() {
-            Some(payload) => self.process_sixlowpan(sockets, &ieee802154_repr, payload, _fragments),
-            None => None,
-        }
-    }
-
     pub(super) fn process_sixlowpan<'output, 'payload: 'output, T: AsRef<[u8]> + ?Sized>(
         &mut self,
         sockets: &mut SocketSet,
@@ -53,7 +18,10 @@ impl InterfaceInner {
         let payload = match check!(SixlowpanPacket::dispatch(payload)) {
             #[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
             SixlowpanPacket::FragmentHeader => {
-                net_debug!("Fragmentation is not supported, use the `proto-sixlowpan-fragmentation` feature to add support.");
+                net_debug!(
+                    "Fragmentation is not supported, \
+                    use the `proto-sixlowpan-fragmentation` feature to add support."
+                );
                 return None;
             }
             #[cfg(feature = "proto-sixlowpan-fragmentation")]
@@ -257,51 +225,29 @@ impl InterfaceInner {
         Ok(decompressed_size)
     }
 
-    #[cfg(feature = "medium-ieee802154")]
-    pub(super) fn dispatch_ieee802154<Tx: TxToken>(
+    pub(super) fn dispatch_sixlowpan<Tx: TxToken>(
         &mut self,
-        ll_dst_a: Ieee802154Address,
         tx_token: Tx,
         packet: IpPacket,
+        ieee_repr: Ieee802154Repr,
         frag: &mut Fragmenter,
     ) {
-        // We first need to convert the IPv6 packet to a 6LoWPAN compressed packet.
-        // Whenever this packet is to big to fit in the IEEE802.15.4 packet, then we need to
-        // fragment it.
-        let ll_src_a = self.hardware_addr.unwrap().ieee802154_or_panic();
-
         let ip_repr = packet.ip_repr();
 
         let (src_addr, dst_addr) = match (ip_repr.src_addr(), ip_repr.dst_addr()) {
             (IpAddress::Ipv6(src_addr), IpAddress::Ipv6(dst_addr)) => (src_addr, dst_addr),
             #[allow(unreachable_patterns)]
             _ => {
-                net_debug!("dispatch_ieee802154: dropping because src or dst addrs are not ipv6.");
-                return;
+                unreachable!()
             }
         };
 
-        // Create the IEEE802.15.4 header.
-        let ieee_repr = Ieee802154Repr {
-            frame_type: Ieee802154FrameType::Data,
-            security_enabled: false,
-            frame_pending: false,
-            ack_request: false,
-            sequence_number: Some(self.get_sequence_number()),
-            pan_id_compression: true,
-            frame_version: Ieee802154FrameVersion::Ieee802154_2003,
-            dst_pan_id: self.pan_id,
-            dst_addr: Some(ll_dst_a),
-            src_pan_id: self.pan_id,
-            src_addr: Some(ll_src_a),
-        };
-
         // Create the 6LoWPAN IPHC header.
         let iphc_repr = SixlowpanIphcRepr {
             src_addr,
-            ll_src_addr: Some(ll_src_a),
+            ll_src_addr: ieee_repr.src_addr,
             dst_addr,
-            ll_dst_addr: Some(ll_dst_a),
+            ll_dst_addr: ieee_repr.dst_addr,
             next_header: match &packet {
                 IpPacket::Icmpv6(_) => SixlowpanNextHeader::Uncompressed(IpProtocol::Icmpv6),
                 #[cfg(feature = "socket-tcp")]
@@ -364,14 +310,15 @@ impl InterfaceInner {
 
                 if pkt.buffer.len() < total_size {
                     net_debug!(
-                                "dispatch_ieee802154: dropping, fragmentation buffer is too small, at least {} needed",
-                                total_size
-                            );
+                        "dispatch_ieee802154: dropping, \
+                        fragmentation buffer is too small, at least {} needed",
+                        total_size
+                    );
                     return;
                 }
 
-                pkt.sixlowpan.ll_dst_addr = ll_dst_a;
-                pkt.sixlowpan.ll_src_addr = ll_src_a;
+                pkt.sixlowpan.ll_dst_addr = ieee_repr.dst_addr.unwrap();
+                pkt.sixlowpan.ll_src_addr = ieee_repr.src_addr.unwrap();
 
                 let mut iphc_packet =
                     SixlowpanIphcPacket::new_unchecked(&mut pkt.buffer[..iphc_repr.buffer_len()]);
@@ -537,30 +484,13 @@ impl InterfaceInner {
         }
     }
 
-    #[cfg(all(
-        feature = "medium-ieee802154",
-        feature = "proto-sixlowpan-fragmentation"
-    ))]
-    pub(super) fn dispatch_ieee802154_frag<Tx: TxToken>(
+    #[cfg(feature = "proto-sixlowpan-fragmentation")]
+    pub(super) fn dispatch_sixlowpan_frag<Tx: TxToken>(
         &mut self,
         tx_token: Tx,
+        ieee_repr: Ieee802154Repr,
         frag: &mut Fragmenter,
     ) {
-        // Create the IEEE802.15.4 header.
-        let ieee_repr = Ieee802154Repr {
-            frame_type: Ieee802154FrameType::Data,
-            security_enabled: false,
-            frame_pending: false,
-            ack_request: false,
-            sequence_number: Some(self.get_sequence_number()),
-            pan_id_compression: true,
-            frame_version: Ieee802154FrameVersion::Ieee802154_2003,
-            dst_pan_id: self.pan_id,
-            dst_addr: Some(frag.sixlowpan.ll_dst_addr),
-            src_pan_id: self.pan_id,
-            src_addr: Some(frag.sixlowpan.ll_src_addr),
-        };
-
         // Create the FRAG_N header.
         let fragn = SixlowpanFragRepr::Fragment {
             size: frag.sixlowpan.datagram_size,