tests.rs 57 KB

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