tests.rs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. use std::collections::BTreeMap;
  2. #[cfg(feature = "proto-igmp")]
  3. use std::vec::Vec;
  4. use super::*;
  5. use crate::iface::Interface;
  6. #[cfg(feature = "medium-ethernet")]
  7. use crate::iface::NeighborCache;
  8. use crate::phy::{ChecksumCapabilities, Loopback};
  9. #[cfg(feature = "proto-igmp")]
  10. use crate::time::Instant;
  11. use crate::{Error, Result};
  12. #[allow(unused)]
  13. fn fill_slice(s: &mut [u8], val: u8) {
  14. for x in s.iter_mut() {
  15. *x = val
  16. }
  17. }
  18. #[cfg(feature = "medium-ethernet")]
  19. const MEDIUM: Medium = Medium::Ethernet;
  20. #[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ip"))]
  21. const MEDIUM: Medium = Medium::Ip;
  22. #[cfg(all(not(feature = "medium-ethernet"), feature = "medium-ieee802154"))]
  23. const MEDIUM: Medium = Medium::Ieee802154;
  24. fn create<'a>(medium: Medium) -> (Interface<'a>, SocketSet<'a>, Loopback) {
  25. match medium {
  26. #[cfg(feature = "medium-ethernet")]
  27. Medium::Ethernet => create_ethernet(),
  28. #[cfg(feature = "medium-ip")]
  29. Medium::Ip => create_ip(),
  30. #[cfg(feature = "medium-ieee802154")]
  31. Medium::Ieee802154 => create_ieee802154(),
  32. }
  33. }
  34. #[cfg(feature = "medium-ip")]
  35. #[allow(unused)]
  36. fn create_ip<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
  37. // Create a basic device
  38. let mut device = Loopback::new(Medium::Ip);
  39. let ip_addrs = [
  40. #[cfg(feature = "proto-ipv4")]
  41. IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8),
  42. #[cfg(feature = "proto-ipv6")]
  43. IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128),
  44. #[cfg(feature = "proto-ipv6")]
  45. IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64),
  46. ];
  47. let iface_builder = InterfaceBuilder::new().ip_addrs(ip_addrs);
  48. #[cfg(feature = "proto-ipv4-fragmentation")]
  49. let iface_builder = iface_builder
  50. .ipv4_reassembly_buffer(PacketAssemblerSet::new(vec![], BTreeMap::new()))
  51. .ipv4_fragmentation_buffer(vec![]);
  52. #[cfg(feature = "proto-igmp")]
  53. let iface_builder = iface_builder.ipv4_multicast_groups(BTreeMap::new());
  54. let iface = iface_builder.finalize(&mut device);
  55. (iface, SocketSet::new(vec![]), device)
  56. }
  57. #[cfg(feature = "medium-ethernet")]
  58. fn create_ethernet<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
  59. // Create a basic device
  60. let mut device = Loopback::new(Medium::Ethernet);
  61. let ip_addrs = [
  62. #[cfg(feature = "proto-ipv4")]
  63. IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8),
  64. #[cfg(feature = "proto-ipv6")]
  65. IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128),
  66. #[cfg(feature = "proto-ipv6")]
  67. IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64),
  68. ];
  69. let iface_builder = InterfaceBuilder::new()
  70. .hardware_addr(EthernetAddress::default().into())
  71. .neighbor_cache(NeighborCache::new(BTreeMap::new()))
  72. .ip_addrs(ip_addrs);
  73. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  74. let iface_builder = iface_builder
  75. .sixlowpan_reassembly_buffer(PacketAssemblerSet::new(vec![], BTreeMap::new()))
  76. .sixlowpan_fragmentation_buffer(vec![]);
  77. #[cfg(feature = "proto-ipv4-fragmentation")]
  78. let iface_builder = iface_builder
  79. .ipv4_reassembly_buffer(PacketAssemblerSet::new(vec![], BTreeMap::new()))
  80. .ipv4_fragmentation_buffer(vec![]);
  81. #[cfg(feature = "proto-igmp")]
  82. let iface_builder = iface_builder.ipv4_multicast_groups(BTreeMap::new());
  83. let iface = iface_builder.finalize(&mut device);
  84. (iface, SocketSet::new(vec![]), device)
  85. }
  86. #[cfg(feature = "medium-ieee802154")]
  87. fn create_ieee802154<'a>() -> (Interface<'a>, SocketSet<'a>, Loopback) {
  88. // Create a basic device
  89. let mut device = Loopback::new(Medium::Ieee802154);
  90. let ip_addrs = [
  91. #[cfg(feature = "proto-ipv6")]
  92. IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128),
  93. #[cfg(feature = "proto-ipv6")]
  94. IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64),
  95. ];
  96. let iface_builder = InterfaceBuilder::new()
  97. .hardware_addr(Ieee802154Address::default().into())
  98. .neighbor_cache(NeighborCache::new(BTreeMap::new()))
  99. .ip_addrs(ip_addrs);
  100. #[cfg(feature = "proto-sixlowpan-fragmentation")]
  101. let iface_builder = iface_builder
  102. .sixlowpan_reassembly_buffer(PacketAssemblerSet::new(vec![], BTreeMap::new()))
  103. .sixlowpan_fragmentation_buffer(vec![]);
  104. let iface = iface_builder.finalize(&mut device);
  105. (iface, SocketSet::new(vec![]), device)
  106. }
  107. #[cfg(feature = "proto-igmp")]
  108. fn recv_all(device: &mut Loopback, timestamp: Instant) -> Vec<Vec<u8>> {
  109. let mut pkts = Vec::new();
  110. while let Some((rx, _tx)) = device.receive() {
  111. rx.consume(timestamp, |pkt| {
  112. pkts.push(pkt.to_vec());
  113. Ok(())
  114. })
  115. .unwrap();
  116. }
  117. pkts
  118. }
  119. #[derive(Debug, PartialEq)]
  120. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  121. struct MockTxToken;
  122. impl TxToken for MockTxToken {
  123. fn consume<R, F>(self, _: Instant, _: usize, _: F) -> Result<R>
  124. where
  125. F: FnOnce(&mut [u8]) -> Result<R>,
  126. {
  127. Err(Error::Unaddressable)
  128. }
  129. }
  130. #[test]
  131. #[should_panic(expected = "hardware_addr required option was not set")]
  132. #[cfg(all(feature = "medium-ethernet"))]
  133. fn test_builder_initialization_panic() {
  134. let mut device = Loopback::new(Medium::Ethernet);
  135. InterfaceBuilder::new().finalize(&mut device);
  136. }
  137. #[test]
  138. #[cfg(feature = "proto-ipv4")]
  139. fn test_no_icmp_no_unicast_ipv4() {
  140. let (mut iface, mut sockets, _device) = create(MEDIUM);
  141. // Unknown Ipv4 Protocol
  142. //
  143. // Because the destination is the broadcast address
  144. // this should not trigger and Destination Unreachable
  145. // response. See RFC 1122 § 3.2.2.
  146. let repr = IpRepr::Ipv4(Ipv4Repr {
  147. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  148. dst_addr: Ipv4Address::BROADCAST,
  149. next_header: IpProtocol::Unknown(0x0c),
  150. payload_len: 0,
  151. hop_limit: 0x40,
  152. });
  153. let mut bytes = vec![0u8; 54];
  154. repr.emit(&mut bytes, &ChecksumCapabilities::default());
  155. let frame = Ipv4Packet::new_unchecked(&bytes);
  156. // Ensure that the unknown protocol frame does not trigger an
  157. // ICMP error response when the destination address is a
  158. // broadcast address
  159. #[cfg(not(feature = "proto-ipv4-fragmentation"))]
  160. assert_eq!(iface.inner.process_ipv4(&mut sockets, &frame, None), None);
  161. #[cfg(feature = "proto-ipv4-fragmentation")]
  162. assert_eq!(
  163. iface.inner.process_ipv4(
  164. &mut sockets,
  165. &frame,
  166. Some(&mut iface.fragments.ipv4_fragments)
  167. ),
  168. None
  169. );
  170. }
  171. #[test]
  172. #[cfg(feature = "proto-ipv6")]
  173. fn test_no_icmp_no_unicast_ipv6() {
  174. let (mut iface, mut sockets, _device) = create(MEDIUM);
  175. // Unknown Ipv6 Protocol
  176. //
  177. // Because the destination is the broadcast address
  178. // this should not trigger and Destination Unreachable
  179. // response. See RFC 1122 § 3.2.2.
  180. let repr = IpRepr::Ipv6(Ipv6Repr {
  181. src_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
  182. dst_addr: Ipv6Address::LINK_LOCAL_ALL_NODES,
  183. next_header: IpProtocol::Unknown(0x0c),
  184. payload_len: 0,
  185. hop_limit: 0x40,
  186. });
  187. let mut bytes = vec![0u8; 54];
  188. repr.emit(&mut bytes, &ChecksumCapabilities::default());
  189. let frame = Ipv6Packet::new_unchecked(&bytes);
  190. // Ensure that the unknown protocol frame does not trigger an
  191. // ICMP error response when the destination address is a
  192. // broadcast address
  193. assert_eq!(iface.inner.process_ipv6(&mut sockets, &frame), None);
  194. }
  195. #[test]
  196. #[cfg(feature = "proto-ipv4")]
  197. fn test_icmp_error_no_payload() {
  198. static NO_BYTES: [u8; 0] = [];
  199. let (mut iface, mut sockets, _device) = create(MEDIUM);
  200. // Unknown Ipv4 Protocol with no payload
  201. let repr = IpRepr::Ipv4(Ipv4Repr {
  202. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  203. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  204. next_header: IpProtocol::Unknown(0x0c),
  205. payload_len: 0,
  206. hop_limit: 0x40,
  207. });
  208. let mut bytes = vec![0u8; 34];
  209. repr.emit(&mut bytes, &ChecksumCapabilities::default());
  210. let frame = Ipv4Packet::new_unchecked(&bytes);
  211. // The expected Destination Unreachable response due to the
  212. // unknown protocol
  213. let icmp_repr = Icmpv4Repr::DstUnreachable {
  214. reason: Icmpv4DstUnreachable::ProtoUnreachable,
  215. header: Ipv4Repr {
  216. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  217. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  218. next_header: IpProtocol::Unknown(12),
  219. payload_len: 0,
  220. hop_limit: 64,
  221. },
  222. data: &NO_BYTES,
  223. };
  224. let expected_repr = IpPacket::Icmpv4((
  225. Ipv4Repr {
  226. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  227. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  228. next_header: IpProtocol::Icmp,
  229. payload_len: icmp_repr.buffer_len(),
  230. hop_limit: 64,
  231. },
  232. icmp_repr,
  233. ));
  234. // Ensure that the unknown protocol triggers an error response.
  235. // And we correctly handle no payload.
  236. #[cfg(not(feature = "proto-ipv4-fragmentation"))]
  237. assert_eq!(
  238. iface.inner.process_ipv4(&mut sockets, &frame, None),
  239. Some(expected_repr)
  240. );
  241. #[cfg(feature = "proto-ipv4-fragmentation")]
  242. assert_eq!(
  243. iface.inner.process_ipv4(
  244. &mut sockets,
  245. &frame,
  246. Some(&mut iface.fragments.ipv4_fragments)
  247. ),
  248. Some(expected_repr)
  249. );
  250. }
  251. #[test]
  252. #[cfg(feature = "proto-ipv4")]
  253. fn test_local_subnet_broadcasts() {
  254. let (mut iface, _, _device) = create(MEDIUM);
  255. iface.update_ip_addrs(|addrs| {
  256. addrs.iter_mut().next().map(|addr| {
  257. *addr = IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address([192, 168, 1, 23]), 24));
  258. });
  259. });
  260. assert!(iface
  261. .inner
  262. .is_subnet_broadcast(Ipv4Address([192, 168, 1, 255])),);
  263. assert!(!iface
  264. .inner
  265. .is_subnet_broadcast(Ipv4Address([192, 168, 1, 254])),);
  266. iface.update_ip_addrs(|addrs| {
  267. addrs.iter_mut().next().map(|addr| {
  268. *addr = IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address([192, 168, 23, 24]), 16));
  269. });
  270. });
  271. assert!(!iface
  272. .inner
  273. .is_subnet_broadcast(Ipv4Address([192, 168, 23, 255])),);
  274. assert!(!iface
  275. .inner
  276. .is_subnet_broadcast(Ipv4Address([192, 168, 23, 254])),);
  277. assert!(!iface
  278. .inner
  279. .is_subnet_broadcast(Ipv4Address([192, 168, 255, 254])),);
  280. assert!(iface
  281. .inner
  282. .is_subnet_broadcast(Ipv4Address([192, 168, 255, 255])),);
  283. iface.update_ip_addrs(|addrs| {
  284. addrs.iter_mut().next().map(|addr| {
  285. *addr = IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address([192, 168, 23, 24]), 8));
  286. });
  287. });
  288. assert!(!iface
  289. .inner
  290. .is_subnet_broadcast(Ipv4Address([192, 23, 1, 255])),);
  291. assert!(!iface
  292. .inner
  293. .is_subnet_broadcast(Ipv4Address([192, 23, 1, 254])),);
  294. assert!(!iface
  295. .inner
  296. .is_subnet_broadcast(Ipv4Address([192, 255, 255, 254])),);
  297. assert!(iface
  298. .inner
  299. .is_subnet_broadcast(Ipv4Address([192, 255, 255, 255])),);
  300. }
  301. #[test]
  302. #[cfg(all(feature = "socket-udp", feature = "proto-ipv4"))]
  303. fn test_icmp_error_port_unreachable() {
  304. static UDP_PAYLOAD: [u8; 12] = [
  305. 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x6c, 0x64, 0x21,
  306. ];
  307. let (mut iface, mut sockets, _device) = create(MEDIUM);
  308. let mut udp_bytes_unicast = vec![0u8; 20];
  309. let mut udp_bytes_broadcast = vec![0u8; 20];
  310. let mut packet_unicast = UdpPacket::new_unchecked(&mut udp_bytes_unicast);
  311. let mut packet_broadcast = UdpPacket::new_unchecked(&mut udp_bytes_broadcast);
  312. let udp_repr = UdpRepr {
  313. src_port: 67,
  314. dst_port: 68,
  315. };
  316. let ip_repr = IpRepr::Ipv4(Ipv4Repr {
  317. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  318. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  319. next_header: IpProtocol::Udp,
  320. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  321. hop_limit: 64,
  322. });
  323. // Emit the representations to a packet
  324. udp_repr.emit(
  325. &mut packet_unicast,
  326. &ip_repr.src_addr(),
  327. &ip_repr.dst_addr(),
  328. UDP_PAYLOAD.len(),
  329. |buf| buf.copy_from_slice(&UDP_PAYLOAD),
  330. &ChecksumCapabilities::default(),
  331. );
  332. let data = packet_unicast.into_inner();
  333. // The expected Destination Unreachable ICMPv4 error response due
  334. // to no sockets listening on the destination port.
  335. let icmp_repr = Icmpv4Repr::DstUnreachable {
  336. reason: Icmpv4DstUnreachable::PortUnreachable,
  337. header: Ipv4Repr {
  338. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  339. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  340. next_header: IpProtocol::Udp,
  341. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  342. hop_limit: 64,
  343. },
  344. data,
  345. };
  346. let expected_repr = IpPacket::Icmpv4((
  347. Ipv4Repr {
  348. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  349. dst_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  350. next_header: IpProtocol::Icmp,
  351. payload_len: icmp_repr.buffer_len(),
  352. hop_limit: 64,
  353. },
  354. icmp_repr,
  355. ));
  356. // Ensure that the unknown protocol triggers an error response.
  357. // And we correctly handle no payload.
  358. assert_eq!(
  359. iface
  360. .inner
  361. .process_udp(&mut sockets, ip_repr, udp_repr, false, &UDP_PAYLOAD, data),
  362. Some(expected_repr)
  363. );
  364. let ip_repr = IpRepr::Ipv4(Ipv4Repr {
  365. src_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x02]),
  366. dst_addr: Ipv4Address::BROADCAST,
  367. next_header: IpProtocol::Udp,
  368. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  369. hop_limit: 64,
  370. });
  371. // Emit the representations to a packet
  372. udp_repr.emit(
  373. &mut packet_broadcast,
  374. &ip_repr.src_addr(),
  375. &IpAddress::Ipv4(Ipv4Address::BROADCAST),
  376. UDP_PAYLOAD.len(),
  377. |buf| buf.copy_from_slice(&UDP_PAYLOAD),
  378. &ChecksumCapabilities::default(),
  379. );
  380. // Ensure that the port unreachable error does not trigger an
  381. // ICMP error response when the destination address is a
  382. // broadcast address and no socket is bound to the port.
  383. assert_eq!(
  384. iface.inner.process_udp(
  385. &mut sockets,
  386. ip_repr,
  387. udp_repr,
  388. false,
  389. &UDP_PAYLOAD,
  390. packet_broadcast.into_inner(),
  391. ),
  392. None
  393. );
  394. }
  395. #[test]
  396. #[cfg(feature = "socket-udp")]
  397. fn test_handle_udp_broadcast() {
  398. use crate::wire::IpEndpoint;
  399. static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];
  400. let (mut iface, mut sockets, _device) = create(MEDIUM);
  401. let rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
  402. let tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
  403. let udp_socket = udp::Socket::new(rx_buffer, tx_buffer);
  404. let mut udp_bytes = vec![0u8; 13];
  405. let mut packet = UdpPacket::new_unchecked(&mut udp_bytes);
  406. let socket_handle = sockets.add(udp_socket);
  407. #[cfg(feature = "proto-ipv6")]
  408. let src_ip = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
  409. #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
  410. let src_ip = Ipv4Address::new(0x7f, 0x00, 0x00, 0x02);
  411. let udp_repr = UdpRepr {
  412. src_port: 67,
  413. dst_port: 68,
  414. };
  415. #[cfg(feature = "proto-ipv6")]
  416. let ip_repr = IpRepr::Ipv6(Ipv6Repr {
  417. src_addr: src_ip,
  418. dst_addr: Ipv6Address::LINK_LOCAL_ALL_NODES,
  419. next_header: IpProtocol::Udp,
  420. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  421. hop_limit: 0x40,
  422. });
  423. #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
  424. let ip_repr = IpRepr::Ipv4(Ipv4Repr {
  425. src_addr: src_ip,
  426. dst_addr: Ipv4Address::BROADCAST,
  427. next_header: IpProtocol::Udp,
  428. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  429. hop_limit: 0x40,
  430. });
  431. // Bind the socket to port 68
  432. let socket = sockets.get_mut::<udp::Socket>(socket_handle);
  433. assert_eq!(socket.bind(68), Ok(()));
  434. assert!(!socket.can_recv());
  435. assert!(socket.can_send());
  436. udp_repr.emit(
  437. &mut packet,
  438. &ip_repr.src_addr(),
  439. &ip_repr.dst_addr(),
  440. UDP_PAYLOAD.len(),
  441. |buf| buf.copy_from_slice(&UDP_PAYLOAD),
  442. &ChecksumCapabilities::default(),
  443. );
  444. // Packet should be handled by bound UDP socket
  445. assert_eq!(
  446. iface.inner.process_udp(
  447. &mut sockets,
  448. ip_repr,
  449. udp_repr,
  450. false,
  451. &UDP_PAYLOAD,
  452. packet.into_inner(),
  453. ),
  454. None
  455. );
  456. // Make sure the payload to the UDP packet processed by process_udp is
  457. // appended to the bound sockets rx_buffer
  458. let socket = sockets.get_mut::<udp::Socket>(socket_handle);
  459. assert!(socket.can_recv());
  460. assert_eq!(
  461. socket.recv(),
  462. Ok((&UDP_PAYLOAD[..], IpEndpoint::new(src_ip.into(), 67)))
  463. );
  464. }
  465. #[test]
  466. #[cfg(feature = "proto-ipv4")]
  467. fn test_handle_ipv4_broadcast() {
  468. use crate::wire::{Icmpv4Packet, Icmpv4Repr, Ipv4Packet};
  469. let (mut iface, mut sockets, _device) = create(MEDIUM);
  470. let our_ipv4_addr = iface.ipv4_address().unwrap();
  471. let src_ipv4_addr = Ipv4Address([127, 0, 0, 2]);
  472. // ICMPv4 echo request
  473. let icmpv4_data: [u8; 4] = [0xaa, 0x00, 0x00, 0xff];
  474. let icmpv4_repr = Icmpv4Repr::EchoRequest {
  475. ident: 0x1234,
  476. seq_no: 0xabcd,
  477. data: &icmpv4_data,
  478. };
  479. // Send to IPv4 broadcast address
  480. let ipv4_repr = Ipv4Repr {
  481. src_addr: src_ipv4_addr,
  482. dst_addr: Ipv4Address::BROADCAST,
  483. next_header: IpProtocol::Icmp,
  484. hop_limit: 64,
  485. payload_len: icmpv4_repr.buffer_len(),
  486. };
  487. // Emit to ip frame
  488. let mut bytes = vec![0u8; ipv4_repr.buffer_len() + icmpv4_repr.buffer_len()];
  489. let frame = {
  490. ipv4_repr.emit(
  491. &mut Ipv4Packet::new_unchecked(&mut bytes),
  492. &ChecksumCapabilities::default(),
  493. );
  494. icmpv4_repr.emit(
  495. &mut Icmpv4Packet::new_unchecked(&mut bytes[ipv4_repr.buffer_len()..]),
  496. &ChecksumCapabilities::default(),
  497. );
  498. Ipv4Packet::new_unchecked(&bytes)
  499. };
  500. // Expected ICMPv4 echo reply
  501. let expected_icmpv4_repr = Icmpv4Repr::EchoReply {
  502. ident: 0x1234,
  503. seq_no: 0xabcd,
  504. data: &icmpv4_data,
  505. };
  506. let expected_ipv4_repr = Ipv4Repr {
  507. src_addr: our_ipv4_addr,
  508. dst_addr: src_ipv4_addr,
  509. next_header: IpProtocol::Icmp,
  510. hop_limit: 64,
  511. payload_len: expected_icmpv4_repr.buffer_len(),
  512. };
  513. let expected_packet = IpPacket::Icmpv4((expected_ipv4_repr, expected_icmpv4_repr));
  514. #[cfg(not(feature = "proto-ipv4-fragmentation"))]
  515. assert_eq!(
  516. iface.inner.process_ipv4(&mut sockets, &frame, None),
  517. Some(expected_packet)
  518. );
  519. #[cfg(feature = "proto-ipv4-fragmentation")]
  520. assert_eq!(
  521. iface.inner.process_ipv4(
  522. &mut sockets,
  523. &frame,
  524. Some(&mut iface.fragments.ipv4_fragments)
  525. ),
  526. Some(expected_packet)
  527. );
  528. }
  529. #[test]
  530. #[cfg(feature = "socket-udp")]
  531. fn test_icmp_reply_size() {
  532. #[cfg(feature = "proto-ipv6")]
  533. use crate::wire::Icmpv6DstUnreachable;
  534. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  535. use crate::wire::IPV4_MIN_MTU as MIN_MTU;
  536. #[cfg(feature = "proto-ipv6")]
  537. use crate::wire::IPV6_MIN_MTU as MIN_MTU;
  538. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  539. const MAX_PAYLOAD_LEN: usize = 528;
  540. #[cfg(feature = "proto-ipv6")]
  541. const MAX_PAYLOAD_LEN: usize = 1192;
  542. let (mut iface, mut sockets, _device) = create(MEDIUM);
  543. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  544. let src_addr = Ipv4Address([192, 168, 1, 1]);
  545. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  546. let dst_addr = Ipv4Address([192, 168, 1, 2]);
  547. #[cfg(feature = "proto-ipv6")]
  548. let src_addr = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
  549. #[cfg(feature = "proto-ipv6")]
  550. let dst_addr = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2);
  551. // UDP packet that if not tructated will cause a icmp port unreachable reply
  552. // to exeed the minimum mtu bytes in length.
  553. let udp_repr = UdpRepr {
  554. src_port: 67,
  555. dst_port: 68,
  556. };
  557. let mut bytes = vec![0xff; udp_repr.header_len() + MAX_PAYLOAD_LEN];
  558. let mut packet = UdpPacket::new_unchecked(&mut bytes[..]);
  559. udp_repr.emit(
  560. &mut packet,
  561. &src_addr.into(),
  562. &dst_addr.into(),
  563. MAX_PAYLOAD_LEN,
  564. |buf| fill_slice(buf, 0x2a),
  565. &ChecksumCapabilities::default(),
  566. );
  567. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  568. let ip_repr = Ipv4Repr {
  569. src_addr,
  570. dst_addr,
  571. next_header: IpProtocol::Udp,
  572. hop_limit: 64,
  573. payload_len: udp_repr.header_len() + MAX_PAYLOAD_LEN,
  574. };
  575. #[cfg(feature = "proto-ipv6")]
  576. let ip_repr = Ipv6Repr {
  577. src_addr,
  578. dst_addr,
  579. next_header: IpProtocol::Udp,
  580. hop_limit: 64,
  581. payload_len: udp_repr.header_len() + MAX_PAYLOAD_LEN,
  582. };
  583. let payload = packet.into_inner();
  584. // Expected packets
  585. #[cfg(feature = "proto-ipv6")]
  586. let expected_icmp_repr = Icmpv6Repr::DstUnreachable {
  587. reason: Icmpv6DstUnreachable::PortUnreachable,
  588. header: ip_repr,
  589. data: &payload[..MAX_PAYLOAD_LEN],
  590. };
  591. #[cfg(feature = "proto-ipv6")]
  592. let expected_ip_repr = Ipv6Repr {
  593. src_addr: dst_addr,
  594. dst_addr: src_addr,
  595. next_header: IpProtocol::Icmpv6,
  596. hop_limit: 64,
  597. payload_len: expected_icmp_repr.buffer_len(),
  598. };
  599. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  600. let expected_icmp_repr = Icmpv4Repr::DstUnreachable {
  601. reason: Icmpv4DstUnreachable::PortUnreachable,
  602. header: ip_repr,
  603. data: &payload[..MAX_PAYLOAD_LEN],
  604. };
  605. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  606. let expected_ip_repr = Ipv4Repr {
  607. src_addr: dst_addr,
  608. dst_addr: src_addr,
  609. next_header: IpProtocol::Icmp,
  610. hop_limit: 64,
  611. payload_len: expected_icmp_repr.buffer_len(),
  612. };
  613. // The expected packet does not exceed the IPV4_MIN_MTU
  614. #[cfg(feature = "proto-ipv6")]
  615. assert_eq!(
  616. expected_ip_repr.buffer_len() + expected_icmp_repr.buffer_len(),
  617. MIN_MTU
  618. );
  619. // The expected packet does not exceed the IPV4_MIN_MTU
  620. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  621. assert_eq!(
  622. expected_ip_repr.buffer_len() + expected_icmp_repr.buffer_len(),
  623. MIN_MTU
  624. );
  625. // The expected packet and the generated packet are equal
  626. #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
  627. assert_eq!(
  628. iface.inner.process_udp(
  629. &mut sockets,
  630. ip_repr.into(),
  631. udp_repr,
  632. false,
  633. &vec![0x2a; MAX_PAYLOAD_LEN],
  634. payload,
  635. ),
  636. Some(IpPacket::Icmpv4((expected_ip_repr, expected_icmp_repr)))
  637. );
  638. #[cfg(feature = "proto-ipv6")]
  639. assert_eq!(
  640. iface.inner.process_udp(
  641. &mut sockets,
  642. ip_repr.into(),
  643. udp_repr,
  644. false,
  645. &vec![0x2a; MAX_PAYLOAD_LEN],
  646. payload,
  647. ),
  648. Some(IpPacket::Icmpv6((expected_ip_repr, expected_icmp_repr)))
  649. );
  650. }
  651. #[test]
  652. #[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))]
  653. fn test_handle_valid_arp_request() {
  654. let (mut iface, mut sockets, _device) = create_ethernet();
  655. let mut eth_bytes = vec![0u8; 42];
  656. let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
  657. let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
  658. let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  659. let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
  660. let repr = ArpRepr::EthernetIpv4 {
  661. operation: ArpOperation::Request,
  662. source_hardware_addr: remote_hw_addr,
  663. source_protocol_addr: remote_ip_addr,
  664. target_hardware_addr: EthernetAddress::default(),
  665. target_protocol_addr: local_ip_addr,
  666. };
  667. let mut frame = EthernetFrame::new_unchecked(&mut eth_bytes);
  668. frame.set_dst_addr(EthernetAddress::BROADCAST);
  669. frame.set_src_addr(remote_hw_addr);
  670. frame.set_ethertype(EthernetProtocol::Arp);
  671. let mut packet = ArpPacket::new_unchecked(frame.payload_mut());
  672. repr.emit(&mut packet);
  673. // Ensure an ARP Request for us triggers an ARP Reply
  674. assert_eq!(
  675. iface
  676. .inner
  677. .process_ethernet(&mut sockets, frame.into_inner(), &mut iface.fragments),
  678. Some(EthernetPacket::Arp(ArpRepr::EthernetIpv4 {
  679. operation: ArpOperation::Reply,
  680. source_hardware_addr: local_hw_addr,
  681. source_protocol_addr: local_ip_addr,
  682. target_hardware_addr: remote_hw_addr,
  683. target_protocol_addr: remote_ip_addr
  684. }))
  685. );
  686. // Ensure the address of the requestor was entered in the cache
  687. assert_eq!(
  688. iface.inner.lookup_hardware_addr(
  689. MockTxToken,
  690. &IpAddress::Ipv4(local_ip_addr),
  691. &IpAddress::Ipv4(remote_ip_addr)
  692. ),
  693. Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken))
  694. );
  695. }
  696. #[test]
  697. #[cfg(all(feature = "medium-ethernet", feature = "proto-ipv6"))]
  698. fn test_handle_valid_ndisc_request() {
  699. let (mut iface, mut sockets, _device) = create_ethernet();
  700. let mut eth_bytes = vec![0u8; 86];
  701. let local_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 1);
  702. let remote_ip_addr = Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 2);
  703. let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  704. let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
  705. let solicit = Icmpv6Repr::Ndisc(NdiscRepr::NeighborSolicit {
  706. target_addr: local_ip_addr,
  707. lladdr: Some(remote_hw_addr.into()),
  708. });
  709. let ip_repr = IpRepr::Ipv6(Ipv6Repr {
  710. src_addr: remote_ip_addr,
  711. dst_addr: local_ip_addr.solicited_node(),
  712. next_header: IpProtocol::Icmpv6,
  713. hop_limit: 0xff,
  714. payload_len: solicit.buffer_len(),
  715. });
  716. let mut frame = EthernetFrame::new_unchecked(&mut eth_bytes);
  717. frame.set_dst_addr(EthernetAddress([0x33, 0x33, 0x00, 0x00, 0x00, 0x00]));
  718. frame.set_src_addr(remote_hw_addr);
  719. frame.set_ethertype(EthernetProtocol::Ipv6);
  720. ip_repr.emit(frame.payload_mut(), &ChecksumCapabilities::default());
  721. solicit.emit(
  722. &remote_ip_addr.into(),
  723. &local_ip_addr.solicited_node().into(),
  724. &mut Icmpv6Packet::new_unchecked(&mut frame.payload_mut()[ip_repr.header_len()..]),
  725. &ChecksumCapabilities::default(),
  726. );
  727. let icmpv6_expected = Icmpv6Repr::Ndisc(NdiscRepr::NeighborAdvert {
  728. flags: NdiscNeighborFlags::SOLICITED,
  729. target_addr: local_ip_addr,
  730. lladdr: Some(local_hw_addr.into()),
  731. });
  732. let ipv6_expected = Ipv6Repr {
  733. src_addr: local_ip_addr,
  734. dst_addr: remote_ip_addr,
  735. next_header: IpProtocol::Icmpv6,
  736. hop_limit: 0xff,
  737. payload_len: icmpv6_expected.buffer_len(),
  738. };
  739. // Ensure an Neighbor Solicitation triggers a Neighbor Advertisement
  740. assert_eq!(
  741. iface
  742. .inner
  743. .process_ethernet(&mut sockets, frame.into_inner(), &mut iface.fragments),
  744. Some(EthernetPacket::Ip(IpPacket::Icmpv6((
  745. ipv6_expected,
  746. icmpv6_expected
  747. ))))
  748. );
  749. // Ensure the address of the requestor was entered in the cache
  750. assert_eq!(
  751. iface.inner.lookup_hardware_addr(
  752. MockTxToken,
  753. &IpAddress::Ipv6(local_ip_addr),
  754. &IpAddress::Ipv6(remote_ip_addr)
  755. ),
  756. Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken))
  757. );
  758. }
  759. #[test]
  760. #[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))]
  761. fn test_handle_other_arp_request() {
  762. let (mut iface, mut sockets, _device) = create_ethernet();
  763. let mut eth_bytes = vec![0u8; 42];
  764. let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
  765. let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
  766. let repr = ArpRepr::EthernetIpv4 {
  767. operation: ArpOperation::Request,
  768. source_hardware_addr: remote_hw_addr,
  769. source_protocol_addr: remote_ip_addr,
  770. target_hardware_addr: EthernetAddress::default(),
  771. target_protocol_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x03]),
  772. };
  773. let mut frame = EthernetFrame::new_unchecked(&mut eth_bytes);
  774. frame.set_dst_addr(EthernetAddress::BROADCAST);
  775. frame.set_src_addr(remote_hw_addr);
  776. frame.set_ethertype(EthernetProtocol::Arp);
  777. let mut packet = ArpPacket::new_unchecked(frame.payload_mut());
  778. repr.emit(&mut packet);
  779. // Ensure an ARP Request for someone else does not trigger an ARP Reply
  780. assert_eq!(
  781. iface
  782. .inner
  783. .process_ethernet(&mut sockets, frame.into_inner(), &mut iface.fragments),
  784. None
  785. );
  786. // Ensure the address of the requestor was NOT entered in the cache
  787. assert_eq!(
  788. iface.inner.lookup_hardware_addr(
  789. MockTxToken,
  790. &IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])),
  791. &IpAddress::Ipv4(remote_ip_addr)
  792. ),
  793. Err(Error::Unaddressable)
  794. );
  795. }
  796. #[test]
  797. #[cfg(all(
  798. feature = "medium-ethernet",
  799. feature = "proto-ipv4",
  800. not(feature = "medium-ieee802154")
  801. ))]
  802. fn test_arp_flush_after_update_ip() {
  803. let (mut iface, mut sockets, _device) = create_ethernet();
  804. let mut eth_bytes = vec![0u8; 42];
  805. let local_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
  806. let remote_ip_addr = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
  807. let local_hw_addr = EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  808. let remote_hw_addr = EthernetAddress([0x52, 0x54, 0x00, 0x00, 0x00, 0x00]);
  809. let repr = ArpRepr::EthernetIpv4 {
  810. operation: ArpOperation::Request,
  811. source_hardware_addr: remote_hw_addr,
  812. source_protocol_addr: remote_ip_addr,
  813. target_hardware_addr: EthernetAddress::default(),
  814. target_protocol_addr: Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
  815. };
  816. let mut frame = EthernetFrame::new_unchecked(&mut eth_bytes);
  817. frame.set_dst_addr(EthernetAddress::BROADCAST);
  818. frame.set_src_addr(remote_hw_addr);
  819. frame.set_ethertype(EthernetProtocol::Arp);
  820. {
  821. let mut packet = ArpPacket::new_unchecked(frame.payload_mut());
  822. repr.emit(&mut packet);
  823. }
  824. // Ensure an ARP Request for us triggers an ARP Reply
  825. assert_eq!(
  826. iface
  827. .inner
  828. .process_ethernet(&mut sockets, frame.into_inner(), &mut iface.fragments),
  829. Some(EthernetPacket::Arp(ArpRepr::EthernetIpv4 {
  830. operation: ArpOperation::Reply,
  831. source_hardware_addr: local_hw_addr,
  832. source_protocol_addr: local_ip_addr,
  833. target_hardware_addr: remote_hw_addr,
  834. target_protocol_addr: remote_ip_addr
  835. }))
  836. );
  837. // Ensure the address of the requestor was entered in the cache
  838. assert_eq!(
  839. iface.inner.lookup_hardware_addr(
  840. MockTxToken,
  841. &IpAddress::Ipv4(local_ip_addr),
  842. &IpAddress::Ipv4(remote_ip_addr)
  843. ),
  844. Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken))
  845. );
  846. // Update IP addrs to trigger ARP cache flush
  847. let local_ip_addr_new = Ipv4Address([0x7f, 0x00, 0x00, 0x01]);
  848. iface.update_ip_addrs(|addrs| {
  849. addrs.iter_mut().next().map(|addr| {
  850. *addr = IpCidr::Ipv4(Ipv4Cidr::new(local_ip_addr_new, 24));
  851. });
  852. });
  853. // ARP cache flush after address change
  854. assert!(!iface.inner.has_neighbor(&IpAddress::Ipv4(remote_ip_addr)));
  855. }
  856. #[test]
  857. #[cfg(all(feature = "socket-icmp", feature = "proto-ipv4"))]
  858. fn test_icmpv4_socket() {
  859. use crate::wire::Icmpv4Packet;
  860. let (mut iface, mut sockets, _device) = create(MEDIUM);
  861. let rx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 24]);
  862. let tx_buffer = icmp::PacketBuffer::new(vec![icmp::PacketMetadata::EMPTY], vec![0; 24]);
  863. let icmpv4_socket = icmp::Socket::new(rx_buffer, tx_buffer);
  864. let socket_handle = sockets.add(icmpv4_socket);
  865. let ident = 0x1234;
  866. let seq_no = 0x5432;
  867. let echo_data = &[0xff; 16];
  868. let socket = sockets.get_mut::<icmp::Socket>(socket_handle);
  869. // Bind to the ID 0x1234
  870. assert_eq!(socket.bind(icmp::Endpoint::Ident(ident)), Ok(()));
  871. // Ensure the ident we bound to and the ident of the packet are the same.
  872. let mut bytes = [0xff; 24];
  873. let mut packet = Icmpv4Packet::new_unchecked(&mut bytes[..]);
  874. let echo_repr = Icmpv4Repr::EchoRequest {
  875. ident,
  876. seq_no,
  877. data: echo_data,
  878. };
  879. echo_repr.emit(&mut packet, &ChecksumCapabilities::default());
  880. let icmp_data = &*packet.into_inner();
  881. let ipv4_repr = Ipv4Repr {
  882. src_addr: Ipv4Address::new(0x7f, 0x00, 0x00, 0x02),
  883. dst_addr: Ipv4Address::new(0x7f, 0x00, 0x00, 0x01),
  884. next_header: IpProtocol::Icmp,
  885. payload_len: 24,
  886. hop_limit: 64,
  887. };
  888. let ip_repr = IpRepr::Ipv4(ipv4_repr);
  889. // Open a socket and ensure the packet is handled due to the listening
  890. // socket.
  891. assert!(!sockets.get_mut::<icmp::Socket>(socket_handle).can_recv());
  892. // Confirm we still get EchoReply from `smoltcp` even with the ICMP socket listening
  893. let echo_reply = Icmpv4Repr::EchoReply {
  894. ident,
  895. seq_no,
  896. data: echo_data,
  897. };
  898. let ipv4_reply = Ipv4Repr {
  899. src_addr: ipv4_repr.dst_addr,
  900. dst_addr: ipv4_repr.src_addr,
  901. ..ipv4_repr
  902. };
  903. assert_eq!(
  904. iface.inner.process_icmpv4(&mut sockets, ip_repr, icmp_data),
  905. Some(IpPacket::Icmpv4((ipv4_reply, echo_reply)))
  906. );
  907. let socket = sockets.get_mut::<icmp::Socket>(socket_handle);
  908. assert!(socket.can_recv());
  909. assert_eq!(
  910. socket.recv(),
  911. Ok((
  912. icmp_data,
  913. IpAddress::Ipv4(Ipv4Address::new(0x7f, 0x00, 0x00, 0x02))
  914. ))
  915. );
  916. }
  917. #[test]
  918. #[cfg(feature = "proto-ipv6")]
  919. fn test_solicited_node_addrs() {
  920. let (mut iface, _, _device) = create(MEDIUM);
  921. let mut new_addrs = vec![
  922. IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 1, 2, 0, 2), 64),
  923. IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 3, 4, 0, 0xffff), 64),
  924. ];
  925. iface.update_ip_addrs(|addrs| {
  926. new_addrs.extend(addrs.to_vec());
  927. *addrs = From::from(new_addrs);
  928. });
  929. assert!(iface
  930. .inner
  931. .has_solicited_node(Ipv6Address::new(0xff02, 0, 0, 0, 0, 1, 0xff00, 0x0002)));
  932. assert!(iface
  933. .inner
  934. .has_solicited_node(Ipv6Address::new(0xff02, 0, 0, 0, 0, 1, 0xff00, 0xffff)));
  935. assert!(!iface
  936. .inner
  937. .has_solicited_node(Ipv6Address::new(0xff02, 0, 0, 0, 0, 1, 0xff00, 0x0003)));
  938. }
  939. #[test]
  940. #[cfg(feature = "proto-ipv6")]
  941. fn test_icmpv6_nxthdr_unknown() {
  942. let (mut iface, mut sockets, _device) = create(MEDIUM);
  943. let remote_ip_addr = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
  944. let payload = [0x12, 0x34, 0x56, 0x78];
  945. let ipv6_repr = Ipv6Repr {
  946. src_addr: remote_ip_addr,
  947. dst_addr: Ipv6Address::LOOPBACK,
  948. next_header: IpProtocol::HopByHop,
  949. payload_len: 12,
  950. hop_limit: 0x40,
  951. };
  952. let mut bytes = vec![0; 52];
  953. let frame = {
  954. let ip_repr = IpRepr::Ipv6(ipv6_repr);
  955. ip_repr.emit(&mut bytes, &ChecksumCapabilities::default());
  956. let mut offset = ipv6_repr.buffer_len();
  957. {
  958. let mut hbh_pkt = Ipv6HopByHopHeader::new_unchecked(&mut bytes[offset..]);
  959. hbh_pkt.set_next_header(IpProtocol::Unknown(0x0c));
  960. hbh_pkt.set_header_len(0);
  961. offset += 8;
  962. {
  963. let mut pad_pkt = Ipv6Option::new_unchecked(&mut *hbh_pkt.options_mut());
  964. Ipv6OptionRepr::PadN(3).emit(&mut pad_pkt);
  965. }
  966. {
  967. let mut pad_pkt = Ipv6Option::new_unchecked(&mut hbh_pkt.options_mut()[5..]);
  968. Ipv6OptionRepr::Pad1.emit(&mut pad_pkt);
  969. }
  970. }
  971. bytes[offset..].copy_from_slice(&payload);
  972. Ipv6Packet::new_unchecked(&bytes)
  973. };
  974. let reply_icmp_repr = Icmpv6Repr::ParamProblem {
  975. reason: Icmpv6ParamProblem::UnrecognizedNxtHdr,
  976. pointer: 40,
  977. header: ipv6_repr,
  978. data: &payload[..],
  979. };
  980. let reply_ipv6_repr = Ipv6Repr {
  981. src_addr: Ipv6Address::LOOPBACK,
  982. dst_addr: remote_ip_addr,
  983. next_header: IpProtocol::Icmpv6,
  984. payload_len: reply_icmp_repr.buffer_len(),
  985. hop_limit: 0x40,
  986. };
  987. // Ensure the unknown next header causes a ICMPv6 Parameter Problem
  988. // error message to be sent to the sender.
  989. assert_eq!(
  990. iface.inner.process_ipv6(&mut sockets, &frame),
  991. Some(IpPacket::Icmpv6((reply_ipv6_repr, reply_icmp_repr)))
  992. );
  993. }
  994. #[test]
  995. #[cfg(feature = "proto-igmp")]
  996. fn test_handle_igmp() {
  997. fn recv_igmp(device: &mut Loopback, timestamp: Instant) -> Vec<(Ipv4Repr, IgmpRepr)> {
  998. let caps = device.capabilities();
  999. let checksum_caps = &caps.checksum;
  1000. recv_all(device, timestamp)
  1001. .iter()
  1002. .filter_map(|frame| {
  1003. let ipv4_packet = match caps.medium {
  1004. #[cfg(feature = "medium-ethernet")]
  1005. Medium::Ethernet => {
  1006. let eth_frame = EthernetFrame::new_checked(frame).ok()?;
  1007. Ipv4Packet::new_checked(eth_frame.payload()).ok()?
  1008. }
  1009. #[cfg(feature = "medium-ip")]
  1010. Medium::Ip => Ipv4Packet::new_checked(&frame[..]).ok()?,
  1011. #[cfg(feature = "medium-ieee802154")]
  1012. Medium::Ieee802154 => todo!(),
  1013. };
  1014. let ipv4_repr = Ipv4Repr::parse(&ipv4_packet, checksum_caps).ok()?;
  1015. let ip_payload = ipv4_packet.payload();
  1016. let igmp_packet = IgmpPacket::new_checked(ip_payload).ok()?;
  1017. let igmp_repr = IgmpRepr::parse(&igmp_packet).ok()?;
  1018. Some((ipv4_repr, igmp_repr))
  1019. })
  1020. .collect::<Vec<_>>()
  1021. }
  1022. let groups = [
  1023. Ipv4Address::new(224, 0, 0, 22),
  1024. Ipv4Address::new(224, 0, 0, 56),
  1025. ];
  1026. let (mut iface, mut sockets, mut device) = create(MEDIUM);
  1027. // Join multicast groups
  1028. let timestamp = Instant::now();
  1029. for group in &groups {
  1030. iface
  1031. .join_multicast_group(&mut device, *group, timestamp)
  1032. .unwrap();
  1033. }
  1034. let reports = recv_igmp(&mut device, timestamp);
  1035. assert_eq!(reports.len(), 2);
  1036. for (i, group_addr) in groups.iter().enumerate() {
  1037. assert_eq!(reports[i].0.next_header, IpProtocol::Igmp);
  1038. assert_eq!(reports[i].0.dst_addr, *group_addr);
  1039. assert_eq!(
  1040. reports[i].1,
  1041. IgmpRepr::MembershipReport {
  1042. group_addr: *group_addr,
  1043. version: IgmpVersion::Version2,
  1044. }
  1045. );
  1046. }
  1047. // General query
  1048. let timestamp = Instant::now();
  1049. const GENERAL_QUERY_BYTES: &[u8] = &[
  1050. 0x46, 0xc0, 0x00, 0x24, 0xed, 0xb4, 0x00, 0x00, 0x01, 0x02, 0x47, 0x43, 0xac, 0x16, 0x63,
  1051. 0x04, 0xe0, 0x00, 0x00, 0x01, 0x94, 0x04, 0x00, 0x00, 0x11, 0x64, 0xec, 0x8f, 0x00, 0x00,
  1052. 0x00, 0x00, 0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1053. 0x00,
  1054. ];
  1055. {
  1056. // Transmit GENERAL_QUERY_BYTES into loopback
  1057. let tx_token = device.transmit().unwrap();
  1058. tx_token
  1059. .consume(timestamp, GENERAL_QUERY_BYTES.len(), |buffer| {
  1060. buffer.copy_from_slice(GENERAL_QUERY_BYTES);
  1061. Ok(())
  1062. })
  1063. .unwrap();
  1064. }
  1065. // Trigger processing until all packets received through the
  1066. // loopback have been processed, including responses to
  1067. // GENERAL_QUERY_BYTES. Therefore `recv_all()` would return 0
  1068. // pkts that could be checked.
  1069. iface.socket_ingress(&mut device, &mut sockets);
  1070. // Leave multicast groups
  1071. let timestamp = Instant::now();
  1072. for group in &groups {
  1073. iface
  1074. .leave_multicast_group(&mut device, *group, timestamp)
  1075. .unwrap();
  1076. }
  1077. let leaves = recv_igmp(&mut device, timestamp);
  1078. assert_eq!(leaves.len(), 2);
  1079. for (i, group_addr) in groups.iter().cloned().enumerate() {
  1080. assert_eq!(leaves[i].0.next_header, IpProtocol::Igmp);
  1081. assert_eq!(leaves[i].0.dst_addr, Ipv4Address::MULTICAST_ALL_ROUTERS);
  1082. assert_eq!(leaves[i].1, IgmpRepr::LeaveGroup { group_addr });
  1083. }
  1084. }
  1085. #[test]
  1086. #[cfg(all(feature = "proto-ipv4", feature = "socket-raw"))]
  1087. fn test_raw_socket_no_reply() {
  1088. use crate::wire::{IpVersion, Ipv4Packet, UdpPacket, UdpRepr};
  1089. let (mut iface, mut sockets, _device) = create(MEDIUM);
  1090. let packets = 1;
  1091. let rx_buffer =
  1092. raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
  1093. let tx_buffer = raw::PacketBuffer::new(
  1094. vec![raw::PacketMetadata::EMPTY; packets],
  1095. vec![0; 48 * packets],
  1096. );
  1097. let raw_socket = raw::Socket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
  1098. sockets.add(raw_socket);
  1099. let src_addr = Ipv4Address([127, 0, 0, 2]);
  1100. let dst_addr = Ipv4Address([127, 0, 0, 1]);
  1101. const PAYLOAD_LEN: usize = 10;
  1102. let udp_repr = UdpRepr {
  1103. src_port: 67,
  1104. dst_port: 68,
  1105. };
  1106. let mut bytes = vec![0xff; udp_repr.header_len() + PAYLOAD_LEN];
  1107. let mut packet = UdpPacket::new_unchecked(&mut bytes[..]);
  1108. udp_repr.emit(
  1109. &mut packet,
  1110. &src_addr.into(),
  1111. &dst_addr.into(),
  1112. PAYLOAD_LEN,
  1113. |buf| fill_slice(buf, 0x2a),
  1114. &ChecksumCapabilities::default(),
  1115. );
  1116. let ipv4_repr = Ipv4Repr {
  1117. src_addr,
  1118. dst_addr,
  1119. next_header: IpProtocol::Udp,
  1120. hop_limit: 64,
  1121. payload_len: udp_repr.header_len() + PAYLOAD_LEN,
  1122. };
  1123. // Emit to frame
  1124. let mut bytes = vec![0u8; ipv4_repr.buffer_len() + udp_repr.header_len() + PAYLOAD_LEN];
  1125. let frame = {
  1126. ipv4_repr.emit(
  1127. &mut Ipv4Packet::new_unchecked(&mut bytes),
  1128. &ChecksumCapabilities::default(),
  1129. );
  1130. udp_repr.emit(
  1131. &mut UdpPacket::new_unchecked(&mut bytes[ipv4_repr.buffer_len()..]),
  1132. &src_addr.into(),
  1133. &dst_addr.into(),
  1134. PAYLOAD_LEN,
  1135. |buf| fill_slice(buf, 0x2a),
  1136. &ChecksumCapabilities::default(),
  1137. );
  1138. Ipv4Packet::new_unchecked(&bytes)
  1139. };
  1140. #[cfg(not(feature = "proto-ipv4-fragmentation"))]
  1141. assert_eq!(iface.inner.process_ipv4(&mut sockets, &frame, None), None);
  1142. #[cfg(feature = "proto-ipv4-fragmentation")]
  1143. assert_eq!(
  1144. iface.inner.process_ipv4(
  1145. &mut sockets,
  1146. &frame,
  1147. Some(&mut iface.fragments.ipv4_fragments)
  1148. ),
  1149. None
  1150. );
  1151. }
  1152. #[test]
  1153. #[cfg(all(feature = "proto-ipv4", feature = "socket-raw", feature = "socket-udp"))]
  1154. fn test_raw_socket_with_udp_socket() {
  1155. use crate::wire::{IpEndpoint, IpVersion, Ipv4Packet, UdpPacket, UdpRepr};
  1156. static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f];
  1157. let (mut iface, mut sockets, _device) = create(MEDIUM);
  1158. let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
  1159. let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 15]);
  1160. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  1161. let udp_socket_handle = sockets.add(udp_socket);
  1162. // Bind the socket to port 68
  1163. let socket = sockets.get_mut::<udp::Socket>(udp_socket_handle);
  1164. assert_eq!(socket.bind(68), Ok(()));
  1165. assert!(!socket.can_recv());
  1166. assert!(socket.can_send());
  1167. let packets = 1;
  1168. let raw_rx_buffer =
  1169. raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * 1]);
  1170. let raw_tx_buffer = raw::PacketBuffer::new(
  1171. vec![raw::PacketMetadata::EMPTY; packets],
  1172. vec![0; 48 * packets],
  1173. );
  1174. let raw_socket = raw::Socket::new(
  1175. IpVersion::Ipv4,
  1176. IpProtocol::Udp,
  1177. raw_rx_buffer,
  1178. raw_tx_buffer,
  1179. );
  1180. sockets.add(raw_socket);
  1181. let src_addr = Ipv4Address([127, 0, 0, 2]);
  1182. let dst_addr = Ipv4Address([127, 0, 0, 1]);
  1183. let udp_repr = UdpRepr {
  1184. src_port: 67,
  1185. dst_port: 68,
  1186. };
  1187. let mut bytes = vec![0xff; udp_repr.header_len() + UDP_PAYLOAD.len()];
  1188. let mut packet = UdpPacket::new_unchecked(&mut bytes[..]);
  1189. udp_repr.emit(
  1190. &mut packet,
  1191. &src_addr.into(),
  1192. &dst_addr.into(),
  1193. UDP_PAYLOAD.len(),
  1194. |buf| buf.copy_from_slice(&UDP_PAYLOAD),
  1195. &ChecksumCapabilities::default(),
  1196. );
  1197. let ipv4_repr = Ipv4Repr {
  1198. src_addr,
  1199. dst_addr,
  1200. next_header: IpProtocol::Udp,
  1201. hop_limit: 64,
  1202. payload_len: udp_repr.header_len() + UDP_PAYLOAD.len(),
  1203. };
  1204. // Emit to frame
  1205. let mut bytes = vec![0u8; ipv4_repr.buffer_len() + udp_repr.header_len() + UDP_PAYLOAD.len()];
  1206. let frame = {
  1207. ipv4_repr.emit(
  1208. &mut Ipv4Packet::new_unchecked(&mut bytes),
  1209. &ChecksumCapabilities::default(),
  1210. );
  1211. udp_repr.emit(
  1212. &mut UdpPacket::new_unchecked(&mut bytes[ipv4_repr.buffer_len()..]),
  1213. &src_addr.into(),
  1214. &dst_addr.into(),
  1215. UDP_PAYLOAD.len(),
  1216. |buf| buf.copy_from_slice(&UDP_PAYLOAD),
  1217. &ChecksumCapabilities::default(),
  1218. );
  1219. Ipv4Packet::new_unchecked(&bytes)
  1220. };
  1221. #[cfg(not(feature = "proto-ipv4-fragmentation"))]
  1222. assert_eq!(iface.inner.process_ipv4(&mut sockets, &frame, None), None);
  1223. #[cfg(feature = "proto-ipv4-fragmentation")]
  1224. assert_eq!(
  1225. iface.inner.process_ipv4(
  1226. &mut sockets,
  1227. &frame,
  1228. Some(&mut iface.fragments.ipv4_fragments)
  1229. ),
  1230. None
  1231. );
  1232. // Make sure the UDP socket can still receive in presence of a Raw socket that handles UDP
  1233. let socket = sockets.get_mut::<udp::Socket>(udp_socket_handle);
  1234. assert!(socket.can_recv());
  1235. assert_eq!(
  1236. socket.recv(),
  1237. Ok((&UDP_PAYLOAD[..], IpEndpoint::new(src_addr.into(), 67)))
  1238. );
  1239. }
  1240. #[cfg(all(
  1241. not(feature = "medium-ethernet"),
  1242. feature = "proto-sixlowpan",
  1243. feature = "proto-sixlowpan-fragmentation"
  1244. ))]
  1245. #[test]
  1246. fn test_echo_request_sixlowpan_128_bytes() {
  1247. use crate::phy::Checksum;
  1248. let (mut iface, mut sockets, mut device) = create(Medium::Ieee802154);
  1249. // TODO: modify the example, such that we can also test if the checksum is correctly
  1250. // computed.
  1251. iface.inner.caps.checksum.icmpv6 = Checksum::None;
  1252. assert_eq!(iface.inner.caps.medium, Medium::Ieee802154);
  1253. let now = iface.inner.now();
  1254. iface.inner.neighbor_cache.as_mut().unwrap().fill(
  1255. Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x2, 0, 0, 0, 0, 0, 0, 0]).into(),
  1256. HardwareAddress::Ieee802154(Ieee802154Address::default()),
  1257. now,
  1258. );
  1259. let mut ieee802154_repr = Ieee802154Repr {
  1260. frame_type: Ieee802154FrameType::Data,
  1261. security_enabled: false,
  1262. frame_pending: false,
  1263. ack_request: false,
  1264. sequence_number: Some(5),
  1265. pan_id_compression: true,
  1266. frame_version: Ieee802154FrameVersion::Ieee802154_2003,
  1267. dst_pan_id: Some(Ieee802154Pan(0xbeef)),
  1268. dst_addr: Some(Ieee802154Address::Extended([
  1269. 0x90, 0xfc, 0x48, 0xc2, 0xa4, 0x41, 0xfc, 0x76,
  1270. ])),
  1271. src_pan_id: Some(Ieee802154Pan(0xbeef)),
  1272. src_addr: Some(Ieee802154Address::Extended([
  1273. 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x0b, 0x1a,
  1274. ])),
  1275. };
  1276. // NOTE: this data is retrieved from tests with Contiki-NG
  1277. let request_first_part_packet = SixlowpanFragPacket::new_checked(&[
  1278. 0xc0, 0xb0, 0x00, 0x8e, 0x6a, 0x33, 0x05, 0x25, 0x2c, 0x3a, 0x80, 0x00, 0xe0, 0x71, 0x00,
  1279. 0x27, 0x00, 0x02, 0xa2, 0xc2, 0x2d, 0x63, 0x00, 0x00, 0x00, 0x00, 0xd9, 0x5e, 0x0c, 0x00,
  1280. 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
  1281. 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
  1282. 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  1283. 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  1284. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  1285. ])
  1286. .unwrap();
  1287. let request_first_part_iphc_packet =
  1288. SixlowpanIphcPacket::new_checked(request_first_part_packet.payload()).unwrap();
  1289. let request_first_part_iphc_repr = SixlowpanIphcRepr::parse(
  1290. &request_first_part_iphc_packet,
  1291. ieee802154_repr.src_addr,
  1292. ieee802154_repr.dst_addr,
  1293. iface.inner.sixlowpan_address_context,
  1294. )
  1295. .unwrap();
  1296. assert_eq!(
  1297. request_first_part_iphc_repr.src_addr,
  1298. Ipv6Address([
  1299. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42, 0xb,
  1300. 0x1a,
  1301. ]),
  1302. );
  1303. assert_eq!(
  1304. request_first_part_iphc_repr.dst_addr,
  1305. Ipv6Address([
  1306. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x92, 0xfc, 0x48, 0xc2, 0xa4, 0x41, 0xfc,
  1307. 0x76,
  1308. ]),
  1309. );
  1310. let request_second_part = [
  1311. 0xe0, 0xb0, 0x00, 0x8e, 0x10, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  1312. 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  1313. 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  1314. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  1315. ];
  1316. assert_eq!(
  1317. iface.inner.process_sixlowpan(
  1318. &mut sockets,
  1319. &ieee802154_repr,
  1320. &request_first_part_packet.into_inner(),
  1321. Some((
  1322. &mut iface.fragments.sixlowpan_fragments,
  1323. iface.fragments.sixlowpan_fragments_cache_timeout,
  1324. )),
  1325. ),
  1326. None
  1327. );
  1328. ieee802154_repr.sequence_number = Some(6);
  1329. // data that was generated when using `ping -s 128`
  1330. let data = &[
  1331. 0xa2, 0xc2, 0x2d, 0x63, 0x00, 0x00, 0x00, 0x00, 0xd9, 0x5e, 0x0c, 0x00, 0x00, 0x00, 0x00,
  1332. 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  1333. 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
  1334. 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  1335. 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
  1336. 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  1337. 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  1338. 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  1339. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  1340. ];
  1341. let result = iface.inner.process_sixlowpan(
  1342. &mut sockets,
  1343. &ieee802154_repr,
  1344. &request_second_part,
  1345. Some((
  1346. &mut iface.fragments.sixlowpan_fragments,
  1347. iface.fragments.sixlowpan_fragments_cache_timeout,
  1348. )),
  1349. );
  1350. assert_eq!(
  1351. result,
  1352. Some(IpPacket::Icmpv6((
  1353. Ipv6Repr {
  1354. src_addr: Ipv6Address([
  1355. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x92, 0xfc, 0x48, 0xc2, 0xa4, 0x41,
  1356. 0xfc, 0x76,
  1357. ]),
  1358. dst_addr: Ipv6Address([
  1359. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42,
  1360. 0xb, 0x1a,
  1361. ]),
  1362. next_header: IpProtocol::Icmpv6,
  1363. payload_len: 136,
  1364. hop_limit: 64,
  1365. },
  1366. Icmpv6Repr::EchoReply {
  1367. ident: 39,
  1368. seq_no: 2,
  1369. data,
  1370. }
  1371. )))
  1372. );
  1373. iface.inner.neighbor_cache.as_mut().unwrap().fill(
  1374. IpAddress::Ipv6(Ipv6Address([
  1375. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42, 0xb, 0x1a,
  1376. ])),
  1377. HardwareAddress::Ieee802154(Ieee802154Address::default()),
  1378. Instant::now(),
  1379. );
  1380. let tx_token = device.transmit().unwrap();
  1381. iface
  1382. .inner
  1383. .dispatch_ieee802154(
  1384. Ieee802154Address::default(),
  1385. tx_token,
  1386. result.unwrap(),
  1387. Some(&mut iface.out_packets),
  1388. )
  1389. .unwrap();
  1390. assert_eq!(
  1391. device.queue[0],
  1392. &[
  1393. 0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1394. 0x0, 0x0, 0x0, 0x0, 0xc0, 0xb0, 0x5, 0x4e, 0x7a, 0x11, 0x3a, 0x92, 0xfc, 0x48, 0xc2,
  1395. 0xa4, 0x41, 0xfc, 0x76, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42, 0xb, 0x1a, 0x81, 0x0, 0x0,
  1396. 0x0, 0x0, 0x27, 0x0, 0x2, 0xa2, 0xc2, 0x2d, 0x63, 0x0, 0x0, 0x0, 0x0, 0xd9, 0x5e, 0xc,
  1397. 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
  1398. 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  1399. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
  1400. 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43,
  1401. 0x44, 0x45, 0x46, 0x47,
  1402. ]
  1403. );
  1404. iface.poll(Instant::now(), &mut device, &mut sockets);
  1405. assert_eq!(
  1406. device.queue[1],
  1407. &[
  1408. 0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1409. 0x0, 0x0, 0x0, 0x0, 0xe0, 0xb0, 0x5, 0x4e, 0xf, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
  1410. 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
  1411. 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  1412. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  1413. 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  1414. ]
  1415. );
  1416. }
  1417. #[cfg(all(
  1418. not(feature = "medium-ethernet"),
  1419. feature = "proto-sixlowpan",
  1420. feature = "proto-sixlowpan-fragmentation"
  1421. ))]
  1422. #[test]
  1423. fn test_sixlowpan_udp_with_fragmentation() {
  1424. use crate::phy::Checksum;
  1425. let mut ieee802154_repr = Ieee802154Repr {
  1426. frame_type: Ieee802154FrameType::Data,
  1427. security_enabled: false,
  1428. frame_pending: false,
  1429. ack_request: false,
  1430. sequence_number: Some(5),
  1431. pan_id_compression: true,
  1432. frame_version: Ieee802154FrameVersion::Ieee802154_2003,
  1433. dst_pan_id: Some(Ieee802154Pan(0xbeef)),
  1434. dst_addr: Some(Ieee802154Address::Extended([
  1435. 0x90, 0xfc, 0x48, 0xc2, 0xa4, 0x41, 0xfc, 0x76,
  1436. ])),
  1437. src_pan_id: Some(Ieee802154Pan(0xbeef)),
  1438. src_addr: Some(Ieee802154Address::Extended([
  1439. 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x0b, 0x1a,
  1440. ])),
  1441. };
  1442. let (mut iface, mut sockets, mut device) = create(Medium::Ieee802154);
  1443. iface.inner.caps.checksum.udp = Checksum::None;
  1444. let udp_rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 1024 * 4]);
  1445. let udp_tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 1024 * 4]);
  1446. let udp_socket = udp::Socket::new(udp_rx_buffer, udp_tx_buffer);
  1447. let udp_socket_handle = sockets.add(udp_socket);
  1448. {
  1449. let socket = sockets.get_mut::<udp::Socket>(udp_socket_handle);
  1450. assert_eq!(socket.bind(6969), Ok(()));
  1451. assert!(!socket.can_recv());
  1452. assert!(socket.can_send());
  1453. }
  1454. let udp_first_part = &[
  1455. 0xc0, 0xbc, 0x00, 0x92, 0x6e, 0x33, 0x07, 0xe7, 0xdc, 0xf0, 0xd3, 0xc9, 0x1b, 0x39, 0xbf,
  1456. 0xa0, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f,
  1457. 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c, 0x20, 0x63,
  1458. 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70,
  1459. 0x69, 0x73, 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74, 0x2e, 0x20, 0x49, 0x6e,
  1460. 0x20, 0x61, 0x74, 0x20, 0x72, 0x68, 0x6f, 0x6e, 0x63, 0x75, 0x73, 0x20, 0x74, 0x6f, 0x72,
  1461. 0x74, 0x6f, 0x72, 0x2e, 0x20, 0x43, 0x72, 0x61, 0x73, 0x20, 0x62, 0x6c, 0x61, 0x6e,
  1462. ];
  1463. assert_eq!(
  1464. iface.inner.process_sixlowpan(
  1465. &mut sockets,
  1466. &ieee802154_repr,
  1467. udp_first_part,
  1468. Some((
  1469. &mut iface.fragments.sixlowpan_fragments,
  1470. iface.fragments.sixlowpan_fragments_cache_timeout
  1471. ))
  1472. ),
  1473. None
  1474. );
  1475. ieee802154_repr.sequence_number = Some(6);
  1476. let udp_second_part = &[
  1477. 0xe0, 0xbc, 0x00, 0x92, 0x11, 0x64, 0x69, 0x74, 0x20, 0x74, 0x65, 0x6c, 0x6c, 0x75, 0x73,
  1478. 0x20, 0x64, 0x69, 0x61, 0x6d, 0x2c, 0x20, 0x76, 0x61, 0x72, 0x69, 0x75, 0x73, 0x20, 0x76,
  1479. 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c, 0x75, 0x6d, 0x20, 0x6e, 0x69, 0x62, 0x68, 0x20,
  1480. 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x64, 0x6f, 0x20, 0x6e, 0x65, 0x63, 0x2e,
  1481. ];
  1482. assert_eq!(
  1483. iface.inner.process_sixlowpan(
  1484. &mut sockets,
  1485. &ieee802154_repr,
  1486. udp_second_part,
  1487. Some((
  1488. &mut iface.fragments.sixlowpan_fragments,
  1489. iface.fragments.sixlowpan_fragments_cache_timeout
  1490. ))
  1491. ),
  1492. None
  1493. );
  1494. let socket = sockets.get_mut::<udp::Socket>(udp_socket_handle);
  1495. let udp_data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
  1496. In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo nec.";
  1497. assert_eq!(
  1498. socket.recv(),
  1499. Ok((
  1500. &udp_data[..],
  1501. IpEndpoint {
  1502. addr: IpAddress::Ipv6(Ipv6Address([
  1503. 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x42, 0x42, 0x42, 0x42, 0x42,
  1504. 0xb, 0x1a,
  1505. ])),
  1506. port: 54217,
  1507. }
  1508. ))
  1509. );
  1510. let tx_token = device.transmit().unwrap();
  1511. iface
  1512. .inner
  1513. .dispatch_ieee802154(
  1514. Ieee802154Address::default(),
  1515. tx_token,
  1516. IpPacket::Udp((
  1517. IpRepr::Ipv6(Ipv6Repr {
  1518. src_addr: Ipv6Address::default(),
  1519. dst_addr: Ipv6Address::default(),
  1520. next_header: IpProtocol::Udp,
  1521. payload_len: udp_data.len(),
  1522. hop_limit: 64,
  1523. }),
  1524. UdpRepr {
  1525. src_port: 1234,
  1526. dst_port: 1234,
  1527. },
  1528. udp_data,
  1529. )),
  1530. Some(&mut iface.out_packets),
  1531. )
  1532. .unwrap();
  1533. iface.poll(Instant::now(), &mut device, &mut sockets);
  1534. assert_eq!(
  1535. device.queue[0],
  1536. &[
  1537. 0x41, 0xcc, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1538. 0x0, 0x0, 0x0, 0x0, 0xc0, 0xb4, 0x5, 0x4e, 0x7e, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1539. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf0, 0x4, 0xd2, 0x4, 0xd2, 0xf6,
  1540. 0x4d, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64,
  1541. 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74, 0x2c,
  1542. 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61,
  1543. 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c, 0x69, 0x74,
  1544. 0x2e, 0x20, 0x49, 0x6e, 0x20, 0x61, 0x74, 0x20, 0x72, 0x68, 0x6f, 0x6e, 0x63, 0x75,
  1545. 0x73, 0x20, 0x74,
  1546. ]
  1547. );
  1548. assert_eq!(
  1549. device.queue[1],
  1550. &[
  1551. 0x41, 0xcc, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  1552. 0x0, 0x0, 0x0, 0x0, 0xe0, 0xb4, 0x5, 0x4e, 0xf, 0x6f, 0x72, 0x74, 0x6f, 0x72, 0x2e,
  1553. 0x20, 0x43, 0x72, 0x61, 0x73, 0x20, 0x62, 0x6c, 0x61, 0x6e, 0x64, 0x69, 0x74, 0x20,
  1554. 0x74, 0x65, 0x6c, 0x6c, 0x75, 0x73, 0x20, 0x64, 0x69, 0x61, 0x6d, 0x2c, 0x20, 0x76,
  1555. 0x61, 0x72, 0x69, 0x75, 0x73, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6c,
  1556. 0x75, 0x6d, 0x20, 0x6e, 0x69, 0x62, 0x68, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x64,
  1557. 0x6f, 0x20, 0x6e, 0x65, 0x63, 0x2e,
  1558. ]
  1559. );
  1560. }