sixlowpan.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. use super::*;
  2. use crate::phy::ChecksumCapabilities;
  3. use crate::wire::*;
  4. // Max len of non-fragmented packets after decompression (including ipv6 header and payload)
  5. // TODO: lower. Should be (6lowpan mtu) - (min 6lowpan header size) + (max ipv6 header size)
  6. pub(crate) const MAX_DECOMPRESSED_LEN: usize = 1500;
  7. impl InterfaceInner {
  8. pub(super) fn process_sixlowpan<'output, 'payload: 'output, T: AsRef<[u8]> + ?Sized>(
  9. &mut self,
  10. sockets: &mut SocketSet,
  11. ieee802154_repr: &Ieee802154Repr,
  12. payload: &'payload T,
  13. f: &'output mut FragmentsBuffer,
  14. ) -> Option<IpPacket<'output>> {
  15. let payload = match check!(SixlowpanPacket::dispatch(payload)) {
  16. #[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
  17. SixlowpanPacket::FragmentHeader => {
  18. net_debug!(
  19. "Fragmentation is not supported, \
  20. use the `proto-sixlowpan-fragmentation` feature to add support."
  21. );
  22. return None;
  23. }
  24. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  25. SixlowpanPacket::FragmentHeader => {
  26. match self.process_sixlowpan_fragment(ieee802154_repr, payload, f) {
  27. Some(payload) => payload,
  28. None => return None,
  29. }
  30. }
  31. SixlowpanPacket::IphcHeader => {
  32. match self.decompress_sixlowpan(
  33. ieee802154_repr,
  34. payload.as_ref(),
  35. None,
  36. &mut f.decompress_buf,
  37. ) {
  38. Ok(len) => &f.decompress_buf[..len],
  39. Err(e) => {
  40. net_debug!("sixlowpan decompress failed: {:?}", e);
  41. return None;
  42. }
  43. }
  44. }
  45. };
  46. self.process_ipv6(sockets, &check!(Ipv6Packet::new_checked(payload)))
  47. }
  48. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  49. fn process_sixlowpan_fragment<'output, 'payload: 'output, T: AsRef<[u8]> + ?Sized>(
  50. &mut self,
  51. ieee802154_repr: &Ieee802154Repr,
  52. payload: &'payload T,
  53. f: &'output mut FragmentsBuffer,
  54. ) -> Option<&'output [u8]> {
  55. use crate::iface::fragmentation::{AssemblerError, AssemblerFullError};
  56. // We have a fragment header, which means we cannot process the 6LoWPAN packet,
  57. // unless we have a complete one after processing this fragment.
  58. let frag = check!(SixlowpanFragPacket::new_checked(payload));
  59. // The key specifies to which 6LoWPAN fragment it belongs too.
  60. // It is based on the link layer addresses, the tag and the size.
  61. let key = FragKey::Sixlowpan(frag.get_key(ieee802154_repr));
  62. // The offset of this fragment in increments of 8 octets.
  63. let offset = frag.datagram_offset() as usize * 8;
  64. // We reserve a spot in the packet assembler set and add the required
  65. // information to the packet assembler.
  66. // This information is the total size of the packet when it is fully assmbled.
  67. // We also pass the header size, since this is needed when other fragments
  68. // (other than the first one) are added.
  69. let frag_slot = match f.assembler.get(&key, self.now + f.reassembly_timeout) {
  70. Ok(frag) => frag,
  71. Err(AssemblerFullError) => {
  72. net_debug!("No available packet assembler for fragmented packet");
  73. return None;
  74. }
  75. };
  76. if frag.is_first_fragment() {
  77. // The first fragment contains the total size of the IPv6 packet.
  78. // However, we received a packet that is compressed following the 6LoWPAN
  79. // standard. This means we need to convert the IPv6 packet size to a 6LoWPAN
  80. // packet size. The packet size can be different because of first the
  81. // compression of the IP header and when UDP is used (because the UDP header
  82. // can also be compressed). Other headers are not compressed by 6LoWPAN.
  83. // First segment tells us the total size.
  84. let total_size = frag.datagram_size() as usize;
  85. if frag_slot.set_total_size(total_size).is_err() {
  86. net_debug!("No available packet assembler for fragmented packet");
  87. return None;
  88. }
  89. // Decompress headers+payload into the assembler.
  90. if let Err(e) = frag_slot.add_with(0, |buffer| {
  91. self.decompress_sixlowpan(ieee802154_repr, frag.payload(), Some(total_size), buffer)
  92. .map_err(|_| AssemblerError)
  93. }) {
  94. net_debug!("fragmentation error: {:?}", e);
  95. return None;
  96. }
  97. } else {
  98. // Add the fragment to the packet assembler.
  99. if let Err(e) = frag_slot.add(frag.payload(), offset) {
  100. net_debug!("fragmentation error: {:?}", e);
  101. return None;
  102. }
  103. }
  104. match frag_slot.assemble() {
  105. Some(payload) => {
  106. net_trace!("6LoWPAN: fragmented packet now complete");
  107. Some(payload)
  108. }
  109. None => None,
  110. }
  111. }
  112. fn decompress_sixlowpan(
  113. &self,
  114. ieee802154_repr: &Ieee802154Repr,
  115. iphc_payload: &[u8],
  116. total_size: Option<usize>,
  117. buffer: &mut [u8],
  118. ) -> core::result::Result<usize, crate::wire::Error> {
  119. let iphc = SixlowpanIphcPacket::new_checked(iphc_payload)?;
  120. let iphc_repr = SixlowpanIphcRepr::parse(
  121. &iphc,
  122. ieee802154_repr.src_addr,
  123. ieee802154_repr.dst_addr,
  124. &self.sixlowpan_address_context,
  125. )?;
  126. let mut decompressed_size = 40 + iphc.payload().len();
  127. let next_header = match iphc_repr.next_header {
  128. SixlowpanNextHeader::Compressed => {
  129. match SixlowpanNhcPacket::dispatch(iphc.payload())? {
  130. SixlowpanNhcPacket::ExtHeader => {
  131. net_debug!("Extension headers are currently not supported for 6LoWPAN");
  132. IpProtocol::Unknown(0)
  133. }
  134. SixlowpanNhcPacket::UdpHeader => {
  135. let udp_packet = SixlowpanUdpNhcPacket::new_checked(iphc.payload())?;
  136. let udp_repr = SixlowpanUdpNhcRepr::parse(
  137. &udp_packet,
  138. &iphc_repr.src_addr,
  139. &iphc_repr.dst_addr,
  140. &crate::phy::ChecksumCapabilities::ignored(),
  141. )?;
  142. decompressed_size += 8;
  143. decompressed_size -= udp_repr.header_len();
  144. IpProtocol::Udp
  145. }
  146. }
  147. }
  148. SixlowpanNextHeader::Uncompressed(proto) => proto,
  149. };
  150. if buffer.len() < decompressed_size {
  151. net_debug!("sixlowpan decompress: buffer too short");
  152. return Err(crate::wire::Error);
  153. }
  154. let buffer = &mut buffer[..decompressed_size];
  155. let total_size = if let Some(size) = total_size {
  156. size
  157. } else {
  158. decompressed_size
  159. };
  160. let ipv6_repr = Ipv6Repr {
  161. src_addr: iphc_repr.src_addr,
  162. dst_addr: iphc_repr.dst_addr,
  163. next_header,
  164. payload_len: total_size - 40,
  165. hop_limit: iphc_repr.hop_limit,
  166. };
  167. // Emit the decompressed IPHC header (decompressed to an IPv6 header).
  168. let mut ipv6_packet = Ipv6Packet::new_unchecked(&mut buffer[..ipv6_repr.buffer_len()]);
  169. ipv6_repr.emit(&mut ipv6_packet);
  170. let buffer = &mut buffer[ipv6_repr.buffer_len()..];
  171. match iphc_repr.next_header {
  172. SixlowpanNextHeader::Compressed => {
  173. match SixlowpanNhcPacket::dispatch(iphc.payload())? {
  174. SixlowpanNhcPacket::ExtHeader => todo!(),
  175. SixlowpanNhcPacket::UdpHeader => {
  176. // We need to uncompress the UDP packet and emit it to the
  177. // buffer.
  178. let udp_packet = SixlowpanUdpNhcPacket::new_checked(iphc.payload())?;
  179. let udp_repr = SixlowpanUdpNhcRepr::parse(
  180. &udp_packet,
  181. &iphc_repr.src_addr,
  182. &iphc_repr.dst_addr,
  183. &ChecksumCapabilities::ignored(),
  184. )?;
  185. let mut udp = UdpPacket::new_unchecked(
  186. &mut buffer[..udp_repr.0.header_len() + iphc.payload().len()
  187. - udp_repr.header_len()],
  188. );
  189. udp_repr.0.emit_header(&mut udp, ipv6_repr.payload_len - 8);
  190. buffer[8..].copy_from_slice(&iphc.payload()[udp_repr.header_len()..]);
  191. }
  192. }
  193. }
  194. SixlowpanNextHeader::Uncompressed(_) => {
  195. // For uncompressed headers we just copy the slice.
  196. let len = iphc.payload().len();
  197. buffer[..len].copy_from_slice(iphc.payload());
  198. }
  199. };
  200. Ok(decompressed_size)
  201. }
  202. pub(super) fn dispatch_sixlowpan<Tx: TxToken>(
  203. &mut self,
  204. tx_token: Tx,
  205. packet: IpPacket,
  206. ieee_repr: Ieee802154Repr,
  207. frag: &mut Fragmenter,
  208. ) {
  209. let ip_repr = packet.ip_repr();
  210. let (src_addr, dst_addr) = match (ip_repr.src_addr(), ip_repr.dst_addr()) {
  211. (IpAddress::Ipv6(src_addr), IpAddress::Ipv6(dst_addr)) => (src_addr, dst_addr),
  212. #[allow(unreachable_patterns)]
  213. _ => {
  214. unreachable!()
  215. }
  216. };
  217. // Create the 6LoWPAN IPHC header.
  218. let iphc_repr = SixlowpanIphcRepr {
  219. src_addr,
  220. ll_src_addr: ieee_repr.src_addr,
  221. dst_addr,
  222. ll_dst_addr: ieee_repr.dst_addr,
  223. next_header: match &packet {
  224. IpPacket::Icmpv6(_) => SixlowpanNextHeader::Uncompressed(IpProtocol::Icmpv6),
  225. #[cfg(feature = "socket-tcp")]
  226. IpPacket::Tcp(_) => SixlowpanNextHeader::Uncompressed(IpProtocol::Tcp),
  227. #[cfg(feature = "socket-udp")]
  228. IpPacket::Udp(_) => SixlowpanNextHeader::Compressed,
  229. #[allow(unreachable_patterns)]
  230. _ => {
  231. net_debug!("dispatch_ieee802154: dropping, unhandled protocol.");
  232. return;
  233. }
  234. },
  235. hop_limit: ip_repr.hop_limit(),
  236. ecn: None,
  237. dscp: None,
  238. flow_label: None,
  239. };
  240. // Now we calculate the total size of the packet.
  241. // We need to know this, such that we know when to do the fragmentation.
  242. let mut total_size = 0;
  243. total_size += iphc_repr.buffer_len();
  244. let mut _compressed_headers_len = iphc_repr.buffer_len();
  245. let mut _uncompressed_headers_len = ip_repr.header_len();
  246. match packet {
  247. #[cfg(feature = "socket-udp")]
  248. IpPacket::Udp((_, udpv6_repr, payload)) => {
  249. let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
  250. _compressed_headers_len += udp_repr.header_len();
  251. _uncompressed_headers_len += udpv6_repr.header_len();
  252. total_size += udp_repr.header_len() + payload.len();
  253. }
  254. #[cfg(feature = "socket-tcp")]
  255. IpPacket::Tcp((_, tcp_repr)) => {
  256. total_size += tcp_repr.buffer_len();
  257. }
  258. #[cfg(feature = "proto-ipv6")]
  259. IpPacket::Icmpv6((_, icmp_repr)) => {
  260. total_size += icmp_repr.buffer_len();
  261. }
  262. #[allow(unreachable_patterns)]
  263. _ => unreachable!(),
  264. }
  265. let ieee_len = ieee_repr.buffer_len();
  266. if total_size + ieee_len > 125 {
  267. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  268. {
  269. // The packet does not fit in one Ieee802154 frame, so we need fragmentation.
  270. // We do this by emitting everything in the `frag.buffer` from the interface.
  271. // After emitting everything into that buffer, we send the first fragment heere.
  272. // When `poll` is called again, we check if frag was fully sent, otherwise we
  273. // call `dispatch_ieee802154_frag`, which will transmit the other fragments.
  274. // `dispatch_ieee802154_frag` requires some information about the total packet size,
  275. // the link local source and destination address...
  276. let pkt = frag;
  277. if pkt.buffer.len() < total_size {
  278. net_debug!(
  279. "dispatch_ieee802154: dropping, \
  280. fragmentation buffer is too small, at least {} needed",
  281. total_size
  282. );
  283. return;
  284. }
  285. pkt.sixlowpan.ll_dst_addr = ieee_repr.dst_addr.unwrap();
  286. pkt.sixlowpan.ll_src_addr = ieee_repr.src_addr.unwrap();
  287. let mut iphc_packet =
  288. SixlowpanIphcPacket::new_unchecked(&mut pkt.buffer[..iphc_repr.buffer_len()]);
  289. iphc_repr.emit(&mut iphc_packet);
  290. let b = &mut pkt.buffer[iphc_repr.buffer_len()..];
  291. match packet {
  292. #[cfg(feature = "socket-udp")]
  293. IpPacket::Udp((_, udpv6_repr, payload)) => {
  294. let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
  295. let mut udp_packet = SixlowpanUdpNhcPacket::new_unchecked(
  296. &mut b[..udp_repr.header_len() + payload.len()],
  297. );
  298. udp_repr.emit(
  299. &mut udp_packet,
  300. &iphc_repr.src_addr,
  301. &iphc_repr.dst_addr,
  302. payload.len(),
  303. |buf| buf.copy_from_slice(payload),
  304. );
  305. }
  306. #[cfg(feature = "socket-tcp")]
  307. IpPacket::Tcp((_, tcp_repr)) => {
  308. let mut tcp_packet =
  309. TcpPacket::new_unchecked(&mut b[..tcp_repr.buffer_len()]);
  310. tcp_repr.emit(
  311. &mut tcp_packet,
  312. &iphc_repr.src_addr.into(),
  313. &iphc_repr.dst_addr.into(),
  314. &self.caps.checksum,
  315. );
  316. }
  317. #[cfg(feature = "proto-ipv6")]
  318. IpPacket::Icmpv6((_, icmp_repr)) => {
  319. let mut icmp_packet =
  320. Icmpv6Packet::new_unchecked(&mut b[..icmp_repr.buffer_len()]);
  321. icmp_repr.emit(
  322. &iphc_repr.src_addr.into(),
  323. &iphc_repr.dst_addr.into(),
  324. &mut icmp_packet,
  325. &self.caps.checksum,
  326. );
  327. }
  328. #[allow(unreachable_patterns)]
  329. _ => unreachable!(),
  330. }
  331. pkt.packet_len = total_size;
  332. // The datagram size that we need to set in the first fragment header is equal to the
  333. // IPv6 payload length + 40.
  334. pkt.sixlowpan.datagram_size = (packet.ip_repr().payload_len() + 40) as u16;
  335. // We generate a random tag.
  336. let tag = self.get_sixlowpan_fragment_tag();
  337. // We save the tag for the other fragments that will be created when calling `poll`
  338. // multiple times.
  339. pkt.sixlowpan.datagram_tag = tag;
  340. let frag1 = SixlowpanFragRepr::FirstFragment {
  341. size: pkt.sixlowpan.datagram_size,
  342. tag,
  343. };
  344. let fragn = SixlowpanFragRepr::Fragment {
  345. size: pkt.sixlowpan.datagram_size,
  346. tag,
  347. offset: 0,
  348. };
  349. // We calculate how much data we can send in the first fragment and the other
  350. // fragments. The eventual IPv6 sizes of these fragments need to be a multiple of eight
  351. // (except for the last fragment) since the offset field in the fragment is an offset
  352. // in multiples of 8 octets. This is explained in [RFC 4944 § 5.3].
  353. //
  354. // [RFC 4944 § 5.3]: https://datatracker.ietf.org/doc/html/rfc4944#section-5.3
  355. let header_diff = _uncompressed_headers_len - _compressed_headers_len;
  356. let frag1_size =
  357. (125 - ieee_len - frag1.buffer_len() + header_diff) / 8 * 8 - (header_diff);
  358. pkt.sixlowpan.fragn_size = (125 - ieee_len - fragn.buffer_len()) / 8 * 8;
  359. pkt.sent_bytes = frag1_size;
  360. pkt.sixlowpan.datagram_offset = frag1_size + header_diff;
  361. tx_token.consume(ieee_len + frag1.buffer_len() + frag1_size, |mut tx_buf| {
  362. // Add the IEEE header.
  363. let mut ieee_packet = Ieee802154Frame::new_unchecked(&mut tx_buf[..ieee_len]);
  364. ieee_repr.emit(&mut ieee_packet);
  365. tx_buf = &mut tx_buf[ieee_len..];
  366. // Add the first fragment header
  367. let mut frag1_packet = SixlowpanFragPacket::new_unchecked(&mut tx_buf);
  368. frag1.emit(&mut frag1_packet);
  369. tx_buf = &mut tx_buf[frag1.buffer_len()..];
  370. // Add the buffer part.
  371. tx_buf[..frag1_size].copy_from_slice(&pkt.buffer[..frag1_size]);
  372. });
  373. }
  374. #[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
  375. {
  376. net_debug!(
  377. "Enable the `proto-sixlowpan-fragmentation` feature for fragmentation support."
  378. );
  379. return;
  380. }
  381. } else {
  382. // We don't need fragmentation, so we emit everything to the TX token.
  383. tx_token.consume(total_size + ieee_len, |mut tx_buf| {
  384. let mut ieee_packet = Ieee802154Frame::new_unchecked(&mut tx_buf[..ieee_len]);
  385. ieee_repr.emit(&mut ieee_packet);
  386. tx_buf = &mut tx_buf[ieee_len..];
  387. let mut iphc_packet =
  388. SixlowpanIphcPacket::new_unchecked(&mut tx_buf[..iphc_repr.buffer_len()]);
  389. iphc_repr.emit(&mut iphc_packet);
  390. tx_buf = &mut tx_buf[iphc_repr.buffer_len()..];
  391. match packet {
  392. #[cfg(feature = "socket-udp")]
  393. IpPacket::Udp((_, udpv6_repr, payload)) => {
  394. let udp_repr = SixlowpanUdpNhcRepr(udpv6_repr);
  395. let mut udp_packet = SixlowpanUdpNhcPacket::new_unchecked(
  396. &mut tx_buf[..udp_repr.header_len() + payload.len()],
  397. );
  398. udp_repr.emit(
  399. &mut udp_packet,
  400. &iphc_repr.src_addr,
  401. &iphc_repr.dst_addr,
  402. payload.len(),
  403. |buf| buf.copy_from_slice(payload),
  404. );
  405. }
  406. #[cfg(feature = "socket-tcp")]
  407. IpPacket::Tcp((_, tcp_repr)) => {
  408. let mut tcp_packet =
  409. TcpPacket::new_unchecked(&mut tx_buf[..tcp_repr.buffer_len()]);
  410. tcp_repr.emit(
  411. &mut tcp_packet,
  412. &iphc_repr.src_addr.into(),
  413. &iphc_repr.dst_addr.into(),
  414. &self.caps.checksum,
  415. );
  416. }
  417. #[cfg(feature = "proto-ipv6")]
  418. IpPacket::Icmpv6((_, icmp_repr)) => {
  419. let mut icmp_packet =
  420. Icmpv6Packet::new_unchecked(&mut tx_buf[..icmp_repr.buffer_len()]);
  421. icmp_repr.emit(
  422. &iphc_repr.src_addr.into(),
  423. &iphc_repr.dst_addr.into(),
  424. &mut icmp_packet,
  425. &self.caps.checksum,
  426. );
  427. }
  428. #[allow(unreachable_patterns)]
  429. _ => unreachable!(),
  430. }
  431. });
  432. }
  433. }
  434. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  435. pub(super) fn dispatch_sixlowpan_frag<Tx: TxToken>(
  436. &mut self,
  437. tx_token: Tx,
  438. ieee_repr: Ieee802154Repr,
  439. frag: &mut Fragmenter,
  440. ) {
  441. // Create the FRAG_N header.
  442. let fragn = SixlowpanFragRepr::Fragment {
  443. size: frag.sixlowpan.datagram_size,
  444. tag: frag.sixlowpan.datagram_tag,
  445. offset: (frag.sixlowpan.datagram_offset / 8) as u8,
  446. };
  447. let ieee_len = ieee_repr.buffer_len();
  448. let frag_size = (frag.packet_len - frag.sent_bytes).min(frag.sixlowpan.fragn_size);
  449. tx_token.consume(
  450. ieee_repr.buffer_len() + fragn.buffer_len() + frag_size,
  451. |mut tx_buf| {
  452. let mut ieee_packet = Ieee802154Frame::new_unchecked(&mut tx_buf[..ieee_len]);
  453. ieee_repr.emit(&mut ieee_packet);
  454. tx_buf = &mut tx_buf[ieee_len..];
  455. let mut frag_packet =
  456. SixlowpanFragPacket::new_unchecked(&mut tx_buf[..fragn.buffer_len()]);
  457. fragn.emit(&mut frag_packet);
  458. tx_buf = &mut tx_buf[fragn.buffer_len()..];
  459. // Add the buffer part
  460. tx_buf[..frag_size].copy_from_slice(&frag.buffer[frag.sent_bytes..][..frag_size]);
  461. frag.sent_bytes += frag_size;
  462. frag.sixlowpan.datagram_offset += frag_size;
  463. },
  464. );
  465. }
  466. }