tests.rs 57 KB

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