sixlowpan_packet.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #![no_main]
  2. use libfuzzer_sys::fuzz_target;
  3. use smoltcp::{phy::ChecksumCapabilities, wire::*};
  4. #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, arbitrary::Arbitrary)]
  5. pub enum AddressFuzzer {
  6. Absent,
  7. Short([u8; 2]),
  8. Extended([u8; 8]),
  9. }
  10. impl From<AddressFuzzer> for Ieee802154Address {
  11. fn from(val: AddressFuzzer) -> Self {
  12. match val {
  13. AddressFuzzer::Absent => Ieee802154Address::Absent,
  14. AddressFuzzer::Short(b) => Ieee802154Address::Short(b),
  15. AddressFuzzer::Extended(b) => Ieee802154Address::Extended(b),
  16. }
  17. }
  18. }
  19. #[derive(Debug, arbitrary::Arbitrary)]
  20. struct SixlowpanPacketFuzzer<'a> {
  21. data: &'a [u8],
  22. ll_src_addr: Option<AddressFuzzer>,
  23. ll_dst_addr: Option<AddressFuzzer>,
  24. }
  25. fuzz_target!(|fuzz: SixlowpanPacketFuzzer| {
  26. match SixlowpanPacket::dispatch(fuzz.data) {
  27. Ok(SixlowpanPacket::FragmentHeader) => {
  28. if let Ok(frame) = SixlowpanFragPacket::new_checked(fuzz.data) {
  29. if let Ok(repr) = SixlowpanFragRepr::parse(&frame) {
  30. let mut buffer = vec![0; repr.buffer_len()];
  31. let mut frame = SixlowpanFragPacket::new_unchecked(&mut buffer[..]);
  32. repr.emit(&mut frame);
  33. }
  34. }
  35. }
  36. Ok(SixlowpanPacket::IphcHeader) => {
  37. if let Ok(frame) = SixlowpanIphcPacket::new_checked(fuzz.data) {
  38. if let Ok(iphc_repr) = SixlowpanIphcRepr::parse(
  39. &frame,
  40. fuzz.ll_src_addr.map(Into::into),
  41. fuzz.ll_dst_addr.map(Into::into),
  42. &[],
  43. ) {
  44. let mut buffer = vec![0; iphc_repr.buffer_len()];
  45. let mut iphc_frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  46. iphc_repr.emit(&mut iphc_frame);
  47. let payload = frame.payload();
  48. match iphc_repr.next_header {
  49. SixlowpanNextHeader::Compressed => {
  50. if let Ok(p) = SixlowpanNhcPacket::dispatch(payload) {
  51. match p {
  52. SixlowpanNhcPacket::ExtHeader => {
  53. if let Ok(frame) =
  54. SixlowpanExtHeaderPacket::new_checked(payload)
  55. {
  56. if let Ok(repr) = SixlowpanExtHeaderRepr::parse(&frame)
  57. {
  58. let mut buffer = vec![0; repr.buffer_len()];
  59. let mut ext_header_frame =
  60. SixlowpanExtHeaderPacket::new_unchecked(
  61. &mut buffer[..],
  62. );
  63. repr.emit(&mut ext_header_frame);
  64. }
  65. }
  66. }
  67. SixlowpanNhcPacket::UdpHeader => {
  68. if let Ok(frame) =
  69. SixlowpanUdpNhcPacket::new_checked(payload)
  70. {
  71. if let Ok(repr) = SixlowpanUdpNhcRepr::parse(
  72. &frame,
  73. &iphc_repr.src_addr,
  74. &iphc_repr.dst_addr,
  75. &Default::default(),
  76. ) {
  77. let mut buffer = vec![
  78. 0;
  79. repr.header_len()
  80. + frame.payload().len()
  81. ];
  82. let mut udp_packet =
  83. SixlowpanUdpNhcPacket::new_unchecked(
  84. &mut buffer[..],
  85. );
  86. repr.emit(
  87. &mut udp_packet,
  88. &iphc_repr.src_addr,
  89. &iphc_repr.dst_addr,
  90. frame.payload().len(),
  91. |b| b.copy_from_slice(frame.payload()),
  92. );
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. SixlowpanNextHeader::Uncompressed(proto) => match proto {
  100. IpProtocol::HopByHop => {
  101. if let Ok(frame) = Ipv6HopByHopHeader::new_checked(payload) {
  102. if let Ok(repr) = Ipv6HopByHopRepr::parse(&frame) {
  103. let mut buffer = vec![0; repr.buffer_len()];
  104. let mut hop_by_hop_frame =
  105. Ipv6HopByHopHeader::new_unchecked(&mut buffer[..]);
  106. repr.emit(&mut hop_by_hop_frame);
  107. }
  108. }
  109. }
  110. IpProtocol::Icmp => {
  111. if let Ok(frame) = Icmpv4Packet::new_checked(payload) {
  112. if let Ok(repr) =
  113. Icmpv4Repr::parse(&frame, &ChecksumCapabilities::default())
  114. {
  115. let mut buffer = vec![0; repr.buffer_len()];
  116. let mut icmpv4_packet =
  117. Icmpv4Packet::new_unchecked(&mut buffer[..]);
  118. repr.emit(
  119. &mut icmpv4_packet,
  120. &ChecksumCapabilities::default(),
  121. );
  122. }
  123. }
  124. }
  125. IpProtocol::Igmp => {
  126. if let Ok(frame) = IgmpPacket::new_checked(payload) {
  127. if let Ok(repr) = IgmpRepr::parse(&frame) {
  128. let mut buffer = vec![0; repr.buffer_len()];
  129. let mut frame = IgmpPacket::new_unchecked(&mut buffer[..]);
  130. repr.emit(&mut frame);
  131. }
  132. }
  133. }
  134. IpProtocol::Tcp => {
  135. if let Ok(frame) = TcpPacket::new_checked(payload) {
  136. if let Ok(repr) = TcpRepr::parse(
  137. &frame,
  138. &iphc_repr.src_addr.into_address(),
  139. &iphc_repr.dst_addr.into_address(),
  140. &ChecksumCapabilities::default(),
  141. ) {
  142. let mut buffer = vec![0; repr.buffer_len()];
  143. let mut frame = TcpPacket::new_unchecked(&mut buffer[..]);
  144. repr.emit(
  145. &mut frame,
  146. &iphc_repr.src_addr.into_address(),
  147. &iphc_repr.dst_addr.into_address(),
  148. &ChecksumCapabilities::default(),
  149. );
  150. }
  151. }
  152. }
  153. IpProtocol::Udp => {
  154. if let Ok(frame) = UdpPacket::new_checked(payload) {
  155. if let Ok(repr) = UdpRepr::parse(
  156. &frame,
  157. &iphc_repr.src_addr.into_address(),
  158. &iphc_repr.dst_addr.into_address(),
  159. &ChecksumCapabilities::default(),
  160. ) {
  161. let mut buffer =
  162. vec![0; repr.header_len() + frame.payload().len()];
  163. let mut packet = UdpPacket::new_unchecked(&mut buffer[..]);
  164. repr.emit(
  165. &mut packet,
  166. &iphc_repr.src_addr.into_address(),
  167. &iphc_repr.dst_addr.into_address(),
  168. frame.payload().len(),
  169. |b| b.copy_from_slice(frame.payload()),
  170. &ChecksumCapabilities::default(),
  171. );
  172. }
  173. }
  174. }
  175. IpProtocol::Ipv6Route => {
  176. if let Ok(frame) = Ipv6RoutingHeader::new_checked(payload) {
  177. if let Ok(repr) = Ipv6RoutingRepr::parse(&frame) {
  178. let mut buffer = vec![0; repr.buffer_len()];
  179. let mut packet = Ipv6RoutingHeader::new(&mut buffer[..]);
  180. repr.emit(&mut packet);
  181. }
  182. }
  183. }
  184. IpProtocol::Ipv6Frag => {
  185. if let Ok(frame) = Ipv6FragmentHeader::new_checked(payload) {
  186. if let Ok(repr) = Ipv6FragmentRepr::parse(&frame) {
  187. let mut buffer = vec![0; repr.buffer_len()];
  188. let mut frame =
  189. Ipv6FragmentHeader::new_unchecked(&mut buffer[..]);
  190. repr.emit(&mut frame);
  191. }
  192. }
  193. }
  194. IpProtocol::Icmpv6 => {
  195. if let Ok(packet) = Icmpv6Packet::new_checked(payload) {
  196. if let Ok(repr) = Icmpv6Repr::parse(
  197. &iphc_repr.src_addr.into_address(),
  198. &iphc_repr.dst_addr.into_address(),
  199. &packet,
  200. &ChecksumCapabilities::default(),
  201. ) {
  202. let mut buffer = vec![0; repr.buffer_len()];
  203. let mut packet =
  204. Icmpv6Packet::new_unchecked(&mut buffer[..]);
  205. repr.emit(
  206. &iphc_repr.src_addr.into_address(),
  207. &iphc_repr.dst_addr.into_address(),
  208. &mut packet,
  209. &ChecksumCapabilities::default(),
  210. );
  211. }
  212. }
  213. }
  214. IpProtocol::Ipv6NoNxt => (),
  215. IpProtocol::Ipv6Opts => {
  216. if let Ok(packet) = Ipv6Option::new_checked(payload) {
  217. if let Ok(repr) = Ipv6OptionRepr::parse(&packet) {
  218. let mut buffer = vec![0; repr.buffer_len()];
  219. let mut packet = Ipv6Option::new_unchecked(&mut buffer[..]);
  220. repr.emit(&mut packet);
  221. }
  222. }
  223. }
  224. IpProtocol::Unknown(_) => (),
  225. },
  226. };
  227. let mut buffer = vec![0; iphc_repr.buffer_len()];
  228. let mut frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  229. iphc_repr.emit(&mut frame);
  230. }
  231. };
  232. }
  233. Err(_) => (),
  234. }
  235. });