icmpv6.rs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. use byteorder::{ByteOrder, NetworkEndian};
  2. use core::{cmp, fmt};
  3. use super::{Error, Result};
  4. use crate::phy::ChecksumCapabilities;
  5. use crate::wire::ip::checksum;
  6. use crate::wire::MldRepr;
  7. #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
  8. use crate::wire::NdiscRepr;
  9. #[cfg(feature = "proto-rpl")]
  10. use crate::wire::RplRepr;
  11. use crate::wire::{IpAddress, IpProtocol, Ipv6Packet, Ipv6Repr};
  12. use crate::wire::{IPV6_HEADER_LEN, IPV6_MIN_MTU};
  13. /// Error packets must not exceed min MTU
  14. const MAX_ERROR_PACKET_LEN: usize = IPV6_MIN_MTU - IPV6_HEADER_LEN;
  15. enum_with_unknown! {
  16. /// Internet protocol control message type.
  17. pub enum Message(u8) {
  18. /// Destination Unreachable.
  19. DstUnreachable = 0x01,
  20. /// Packet Too Big.
  21. PktTooBig = 0x02,
  22. /// Time Exceeded.
  23. TimeExceeded = 0x03,
  24. /// Parameter Problem.
  25. ParamProblem = 0x04,
  26. /// Echo Request
  27. EchoRequest = 0x80,
  28. /// Echo Reply
  29. EchoReply = 0x81,
  30. /// Multicast Listener Query
  31. MldQuery = 0x82,
  32. /// Router Solicitation
  33. RouterSolicit = 0x85,
  34. /// Router Advertisement
  35. RouterAdvert = 0x86,
  36. /// Neighbor Solicitation
  37. NeighborSolicit = 0x87,
  38. /// Neighbor Advertisement
  39. NeighborAdvert = 0x88,
  40. /// Redirect
  41. Redirect = 0x89,
  42. /// Multicast Listener Report
  43. MldReport = 0x8f,
  44. /// RPL Control Message
  45. RplControl = 0x9b,
  46. }
  47. }
  48. impl Message {
  49. /// Per [RFC 4443 § 2.1] ICMPv6 message types with the highest order
  50. /// bit set are informational messages while message types without
  51. /// the highest order bit set are error messages.
  52. ///
  53. /// [RFC 4443 § 2.1]: https://tools.ietf.org/html/rfc4443#section-2.1
  54. pub fn is_error(&self) -> bool {
  55. (u8::from(*self) & 0x80) != 0x80
  56. }
  57. /// Return a boolean value indicating if the given message type
  58. /// is an [NDISC] message type.
  59. ///
  60. /// [NDISC]: https://tools.ietf.org/html/rfc4861
  61. pub const fn is_ndisc(&self) -> bool {
  62. match *self {
  63. Message::RouterSolicit
  64. | Message::RouterAdvert
  65. | Message::NeighborSolicit
  66. | Message::NeighborAdvert
  67. | Message::Redirect => true,
  68. _ => false,
  69. }
  70. }
  71. /// Return a boolean value indicating if the given message type
  72. /// is an [MLD] message type.
  73. ///
  74. /// [MLD]: https://tools.ietf.org/html/rfc3810
  75. pub const fn is_mld(&self) -> bool {
  76. match *self {
  77. Message::MldQuery | Message::MldReport => true,
  78. _ => false,
  79. }
  80. }
  81. }
  82. impl fmt::Display for Message {
  83. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  84. match *self {
  85. Message::DstUnreachable => write!(f, "destination unreachable"),
  86. Message::PktTooBig => write!(f, "packet too big"),
  87. Message::TimeExceeded => write!(f, "time exceeded"),
  88. Message::ParamProblem => write!(f, "parameter problem"),
  89. Message::EchoReply => write!(f, "echo reply"),
  90. Message::EchoRequest => write!(f, "echo request"),
  91. Message::RouterSolicit => write!(f, "router solicitation"),
  92. Message::RouterAdvert => write!(f, "router advertisement"),
  93. Message::NeighborSolicit => write!(f, "neighbor solicitation"),
  94. Message::NeighborAdvert => write!(f, "neighbor advert"),
  95. Message::Redirect => write!(f, "redirect"),
  96. Message::MldQuery => write!(f, "multicast listener query"),
  97. Message::MldReport => write!(f, "multicast listener report"),
  98. Message::RplControl => write!(f, "RPL control message"),
  99. Message::Unknown(id) => write!(f, "{id}"),
  100. }
  101. }
  102. }
  103. enum_with_unknown! {
  104. /// Internet protocol control message subtype for type "Destination Unreachable".
  105. pub enum DstUnreachable(u8) {
  106. /// No Route to destination.
  107. NoRoute = 0,
  108. /// Communication with destination administratively prohibited.
  109. AdminProhibit = 1,
  110. /// Beyond scope of source address.
  111. BeyondScope = 2,
  112. /// Address unreachable.
  113. AddrUnreachable = 3,
  114. /// Port unreachable.
  115. PortUnreachable = 4,
  116. /// Source address failed ingress/egress policy.
  117. FailedPolicy = 5,
  118. /// Reject route to destination.
  119. RejectRoute = 6
  120. }
  121. }
  122. impl fmt::Display for DstUnreachable {
  123. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  124. match *self {
  125. DstUnreachable::NoRoute => write!(f, "no route to destination"),
  126. DstUnreachable::AdminProhibit => write!(
  127. f,
  128. "communication with destination administratively prohibited"
  129. ),
  130. DstUnreachable::BeyondScope => write!(f, "beyond scope of source address"),
  131. DstUnreachable::AddrUnreachable => write!(f, "address unreachable"),
  132. DstUnreachable::PortUnreachable => write!(f, "port unreachable"),
  133. DstUnreachable::FailedPolicy => {
  134. write!(f, "source address failed ingress/egress policy")
  135. }
  136. DstUnreachable::RejectRoute => write!(f, "reject route to destination"),
  137. DstUnreachable::Unknown(id) => write!(f, "{id}"),
  138. }
  139. }
  140. }
  141. enum_with_unknown! {
  142. /// Internet protocol control message subtype for the type "Parameter Problem".
  143. pub enum ParamProblem(u8) {
  144. /// Erroneous header field encountered.
  145. ErroneousHdrField = 0,
  146. /// Unrecognized Next Header type encountered.
  147. UnrecognizedNxtHdr = 1,
  148. /// Unrecognized IPv6 option encountered.
  149. UnrecognizedOption = 2
  150. }
  151. }
  152. impl fmt::Display for ParamProblem {
  153. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  154. match *self {
  155. ParamProblem::ErroneousHdrField => write!(f, "erroneous header field."),
  156. ParamProblem::UnrecognizedNxtHdr => write!(f, "unrecognized next header type."),
  157. ParamProblem::UnrecognizedOption => write!(f, "unrecognized IPv6 option."),
  158. ParamProblem::Unknown(id) => write!(f, "{id}"),
  159. }
  160. }
  161. }
  162. enum_with_unknown! {
  163. /// Internet protocol control message subtype for the type "Time Exceeded".
  164. pub enum TimeExceeded(u8) {
  165. /// Hop limit exceeded in transit.
  166. HopLimitExceeded = 0,
  167. /// Fragment reassembly time exceeded.
  168. FragReassemExceeded = 1
  169. }
  170. }
  171. impl fmt::Display for TimeExceeded {
  172. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  173. match *self {
  174. TimeExceeded::HopLimitExceeded => write!(f, "hop limit exceeded in transit"),
  175. TimeExceeded::FragReassemExceeded => write!(f, "fragment reassembly time exceeded"),
  176. TimeExceeded::Unknown(id) => write!(f, "{id}"),
  177. }
  178. }
  179. }
  180. /// A read/write wrapper around an Internet Control Message Protocol version 6 packet buffer.
  181. #[derive(Debug, PartialEq, Eq, Clone)]
  182. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  183. pub struct Packet<T: AsRef<[u8]>> {
  184. pub(super) buffer: T,
  185. }
  186. // Ranges and constants describing key boundaries in the ICMPv6 header.
  187. pub(super) mod field {
  188. use crate::wire::field::*;
  189. // ICMPv6: See https://tools.ietf.org/html/rfc4443
  190. pub const TYPE: usize = 0;
  191. pub const CODE: usize = 1;
  192. pub const CHECKSUM: Field = 2..4;
  193. pub const UNUSED: Field = 4..8;
  194. pub const MTU: Field = 4..8;
  195. pub const POINTER: Field = 4..8;
  196. pub const ECHO_IDENT: Field = 4..6;
  197. pub const ECHO_SEQNO: Field = 6..8;
  198. pub const HEADER_END: usize = 8;
  199. // NDISC: See https://tools.ietf.org/html/rfc4861
  200. // Router Advertisement message offsets
  201. pub const CUR_HOP_LIMIT: usize = 4;
  202. pub const ROUTER_FLAGS: usize = 5;
  203. pub const ROUTER_LT: Field = 6..8;
  204. pub const REACHABLE_TM: Field = 8..12;
  205. pub const RETRANS_TM: Field = 12..16;
  206. // Neighbor Solicitation message offsets
  207. pub const TARGET_ADDR: Field = 8..24;
  208. // Neighbor Advertisement message offsets
  209. pub const NEIGH_FLAGS: usize = 4;
  210. // Redirected Header message offsets
  211. pub const DEST_ADDR: Field = 24..40;
  212. // MLD:
  213. // - https://tools.ietf.org/html/rfc3810
  214. // - https://tools.ietf.org/html/rfc3810
  215. // Multicast Listener Query message
  216. pub const MAX_RESP_CODE: Field = 4..6;
  217. pub const QUERY_RESV: Field = 6..8;
  218. pub const QUERY_MCAST_ADDR: Field = 8..24;
  219. pub const SQRV: usize = 24;
  220. pub const QQIC: usize = 25;
  221. pub const QUERY_NUM_SRCS: Field = 26..28;
  222. // Multicast Listener Report Message
  223. pub const RECORD_RESV: Field = 4..6;
  224. pub const NR_MCAST_RCRDS: Field = 6..8;
  225. // Multicast Address Record Offsets
  226. pub const RECORD_TYPE: usize = 0;
  227. pub const AUX_DATA_LEN: usize = 1;
  228. pub const RECORD_NUM_SRCS: Field = 2..4;
  229. pub const RECORD_MCAST_ADDR: Field = 4..20;
  230. }
  231. impl<T: AsRef<[u8]>> Packet<T> {
  232. /// Imbue a raw octet buffer with ICMPv6 packet structure.
  233. pub const fn new_unchecked(buffer: T) -> Packet<T> {
  234. Packet { buffer }
  235. }
  236. /// Shorthand for a combination of [new_unchecked] and [check_len].
  237. ///
  238. /// [new_unchecked]: #method.new_unchecked
  239. /// [check_len]: #method.check_len
  240. pub fn new_checked(buffer: T) -> Result<Packet<T>> {
  241. let packet = Self::new_unchecked(buffer);
  242. packet.check_len()?;
  243. Ok(packet)
  244. }
  245. /// Ensure that no accessor method will panic if called.
  246. /// Returns `Err(Error)` if the buffer is too short.
  247. pub fn check_len(&self) -> Result<()> {
  248. let len = self.buffer.as_ref().len();
  249. if len < 4 {
  250. return Err(Error);
  251. }
  252. match self.msg_type() {
  253. Message::DstUnreachable
  254. | Message::PktTooBig
  255. | Message::TimeExceeded
  256. | Message::ParamProblem
  257. | Message::EchoRequest
  258. | Message::EchoReply
  259. | Message::MldQuery
  260. | Message::RouterSolicit
  261. | Message::RouterAdvert
  262. | Message::NeighborSolicit
  263. | Message::NeighborAdvert
  264. | Message::Redirect
  265. | Message::MldReport => {
  266. if len < field::HEADER_END || len < self.header_len() {
  267. return Err(Error);
  268. }
  269. }
  270. #[cfg(feature = "proto-rpl")]
  271. Message::RplControl => match super::rpl::RplControlMessage::from(self.msg_code()) {
  272. super::rpl::RplControlMessage::DodagInformationSolicitation => {
  273. // TODO(thvdveld): replace magic number
  274. if len < 6 {
  275. return Err(Error);
  276. }
  277. }
  278. super::rpl::RplControlMessage::DodagInformationObject => {
  279. // TODO(thvdveld): replace magic number
  280. if len < 28 {
  281. return Err(Error);
  282. }
  283. }
  284. super::rpl::RplControlMessage::DestinationAdvertisementObject => {
  285. // TODO(thvdveld): replace magic number
  286. if len < 8 || (self.dao_dodag_id_present() && len < 24) {
  287. return Err(Error);
  288. }
  289. }
  290. super::rpl::RplControlMessage::DestinationAdvertisementObjectAck => {
  291. // TODO(thvdveld): replace magic number
  292. if len < 8 || (self.dao_dodag_id_present() && len < 24) {
  293. return Err(Error);
  294. }
  295. }
  296. super::rpl::RplControlMessage::SecureDodagInformationSolicitation
  297. | super::rpl::RplControlMessage::SecureDodagInformationObject
  298. | super::rpl::RplControlMessage::SecureDestinationAdvertisementObject
  299. | super::rpl::RplControlMessage::SecureDestinationAdvertisementObjectAck
  300. | super::rpl::RplControlMessage::ConsistencyCheck => return Err(Error),
  301. super::rpl::RplControlMessage::Unknown(_) => return Err(Error),
  302. },
  303. #[cfg(not(feature = "proto-rpl"))]
  304. Message::RplControl => return Err(Error),
  305. Message::Unknown(_) => return Err(Error),
  306. }
  307. Ok(())
  308. }
  309. /// Consume the packet, returning the underlying buffer.
  310. pub fn into_inner(self) -> T {
  311. self.buffer
  312. }
  313. /// Return the message type field.
  314. #[inline]
  315. pub fn msg_type(&self) -> Message {
  316. let data = self.buffer.as_ref();
  317. Message::from(data[field::TYPE])
  318. }
  319. /// Return the message code field.
  320. #[inline]
  321. pub fn msg_code(&self) -> u8 {
  322. let data = self.buffer.as_ref();
  323. data[field::CODE]
  324. }
  325. /// Return the checksum field.
  326. #[inline]
  327. pub fn checksum(&self) -> u16 {
  328. let data = self.buffer.as_ref();
  329. NetworkEndian::read_u16(&data[field::CHECKSUM])
  330. }
  331. /// Return the identifier field (for echo request and reply packets).
  332. #[inline]
  333. pub fn echo_ident(&self) -> u16 {
  334. let data = self.buffer.as_ref();
  335. NetworkEndian::read_u16(&data[field::ECHO_IDENT])
  336. }
  337. /// Return the sequence number field (for echo request and reply packets).
  338. #[inline]
  339. pub fn echo_seq_no(&self) -> u16 {
  340. let data = self.buffer.as_ref();
  341. NetworkEndian::read_u16(&data[field::ECHO_SEQNO])
  342. }
  343. /// Return the MTU field (for packet too big messages).
  344. #[inline]
  345. pub fn pkt_too_big_mtu(&self) -> u32 {
  346. let data = self.buffer.as_ref();
  347. NetworkEndian::read_u32(&data[field::MTU])
  348. }
  349. /// Return the pointer field (for parameter problem messages).
  350. #[inline]
  351. pub fn param_problem_ptr(&self) -> u32 {
  352. let data = self.buffer.as_ref();
  353. NetworkEndian::read_u32(&data[field::POINTER])
  354. }
  355. /// Return the header length. The result depends on the value of
  356. /// the message type field.
  357. pub fn header_len(&self) -> usize {
  358. match self.msg_type() {
  359. Message::DstUnreachable => field::UNUSED.end,
  360. Message::PktTooBig => field::MTU.end,
  361. Message::TimeExceeded => field::UNUSED.end,
  362. Message::ParamProblem => field::POINTER.end,
  363. Message::EchoRequest => field::ECHO_SEQNO.end,
  364. Message::EchoReply => field::ECHO_SEQNO.end,
  365. Message::RouterSolicit => field::UNUSED.end,
  366. Message::RouterAdvert => field::RETRANS_TM.end,
  367. Message::NeighborSolicit => field::TARGET_ADDR.end,
  368. Message::NeighborAdvert => field::TARGET_ADDR.end,
  369. Message::Redirect => field::DEST_ADDR.end,
  370. Message::MldQuery => field::QUERY_NUM_SRCS.end,
  371. Message::MldReport => field::NR_MCAST_RCRDS.end,
  372. // For packets that are not included in RFC 4443, do not
  373. // include the last 32 bits of the ICMPv6 header in
  374. // `header_bytes`. This must be done so that these bytes
  375. // can be accessed in the `payload`.
  376. _ => field::CHECKSUM.end,
  377. }
  378. }
  379. /// Validate the header checksum.
  380. ///
  381. /// # Fuzzing
  382. /// This function always returns `true` when fuzzing.
  383. pub fn verify_checksum(&self, src_addr: &IpAddress, dst_addr: &IpAddress) -> bool {
  384. if cfg!(fuzzing) {
  385. return true;
  386. }
  387. let data = self.buffer.as_ref();
  388. checksum::combine(&[
  389. checksum::pseudo_header(src_addr, dst_addr, IpProtocol::Icmpv6, data.len() as u32),
  390. checksum::data(data),
  391. ]) == !0
  392. }
  393. }
  394. impl<'a, T: AsRef<[u8]> + ?Sized> Packet<&'a T> {
  395. /// Return a pointer to the type-specific data.
  396. #[inline]
  397. pub fn payload(&self) -> &'a [u8] {
  398. let data = self.buffer.as_ref();
  399. &data[self.header_len()..]
  400. }
  401. }
  402. impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
  403. /// Set the message type field.
  404. #[inline]
  405. pub fn set_msg_type(&mut self, value: Message) {
  406. let data = self.buffer.as_mut();
  407. data[field::TYPE] = value.into()
  408. }
  409. /// Set the message code field.
  410. #[inline]
  411. pub fn set_msg_code(&mut self, value: u8) {
  412. let data = self.buffer.as_mut();
  413. data[field::CODE] = value
  414. }
  415. /// Clear any reserved fields in the message header.
  416. ///
  417. /// # Panics
  418. /// This function panics if the message type has not been set.
  419. /// See [set_msg_type].
  420. ///
  421. /// [set_msg_type]: #method.set_msg_type
  422. #[inline]
  423. pub fn clear_reserved(&mut self) {
  424. match self.msg_type() {
  425. Message::RouterSolicit
  426. | Message::NeighborSolicit
  427. | Message::NeighborAdvert
  428. | Message::Redirect => {
  429. let data = self.buffer.as_mut();
  430. NetworkEndian::write_u32(&mut data[field::UNUSED], 0);
  431. }
  432. Message::MldQuery => {
  433. let data = self.buffer.as_mut();
  434. NetworkEndian::write_u16(&mut data[field::QUERY_RESV], 0);
  435. data[field::SQRV] &= 0xf;
  436. }
  437. Message::MldReport => {
  438. let data = self.buffer.as_mut();
  439. NetworkEndian::write_u16(&mut data[field::RECORD_RESV], 0);
  440. }
  441. ty => panic!("Message type `{ty}` does not have any reserved fields."),
  442. }
  443. }
  444. #[inline]
  445. pub fn set_checksum(&mut self, value: u16) {
  446. let data = self.buffer.as_mut();
  447. NetworkEndian::write_u16(&mut data[field::CHECKSUM], value)
  448. }
  449. /// Set the identifier field (for echo request and reply packets).
  450. ///
  451. /// # Panics
  452. /// This function may panic if this packet is not an echo request or reply packet.
  453. #[inline]
  454. pub fn set_echo_ident(&mut self, value: u16) {
  455. let data = self.buffer.as_mut();
  456. NetworkEndian::write_u16(&mut data[field::ECHO_IDENT], value)
  457. }
  458. /// Set the sequence number field (for echo request and reply packets).
  459. ///
  460. /// # Panics
  461. /// This function may panic if this packet is not an echo request or reply packet.
  462. #[inline]
  463. pub fn set_echo_seq_no(&mut self, value: u16) {
  464. let data = self.buffer.as_mut();
  465. NetworkEndian::write_u16(&mut data[field::ECHO_SEQNO], value)
  466. }
  467. /// Set the MTU field (for packet too big messages).
  468. ///
  469. /// # Panics
  470. /// This function may panic if this packet is not an packet too big packet.
  471. #[inline]
  472. pub fn set_pkt_too_big_mtu(&mut self, value: u32) {
  473. let data = self.buffer.as_mut();
  474. NetworkEndian::write_u32(&mut data[field::MTU], value)
  475. }
  476. /// Set the pointer field (for parameter problem messages).
  477. ///
  478. /// # Panics
  479. /// This function may panic if this packet is not a parameter problem message.
  480. #[inline]
  481. pub fn set_param_problem_ptr(&mut self, value: u32) {
  482. let data = self.buffer.as_mut();
  483. NetworkEndian::write_u32(&mut data[field::POINTER], value)
  484. }
  485. /// Compute and fill in the header checksum.
  486. pub fn fill_checksum(&mut self, src_addr: &IpAddress, dst_addr: &IpAddress) {
  487. self.set_checksum(0);
  488. let checksum = {
  489. let data = self.buffer.as_ref();
  490. !checksum::combine(&[
  491. checksum::pseudo_header(src_addr, dst_addr, IpProtocol::Icmpv6, data.len() as u32),
  492. checksum::data(data),
  493. ])
  494. };
  495. self.set_checksum(checksum)
  496. }
  497. /// Return a mutable pointer to the type-specific data.
  498. #[inline]
  499. pub fn payload_mut(&mut self) -> &mut [u8] {
  500. let range = self.header_len()..;
  501. let data = self.buffer.as_mut();
  502. &mut data[range]
  503. }
  504. }
  505. impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {
  506. fn as_ref(&self) -> &[u8] {
  507. self.buffer.as_ref()
  508. }
  509. }
  510. /// A high-level representation of an Internet Control Message Protocol version 6 packet header.
  511. #[derive(Debug, PartialEq, Eq, Clone, Copy)]
  512. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  513. #[non_exhaustive]
  514. pub enum Repr<'a> {
  515. DstUnreachable {
  516. reason: DstUnreachable,
  517. header: Ipv6Repr,
  518. data: &'a [u8],
  519. },
  520. PktTooBig {
  521. mtu: u32,
  522. header: Ipv6Repr,
  523. data: &'a [u8],
  524. },
  525. TimeExceeded {
  526. reason: TimeExceeded,
  527. header: Ipv6Repr,
  528. data: &'a [u8],
  529. },
  530. ParamProblem {
  531. reason: ParamProblem,
  532. pointer: u32,
  533. header: Ipv6Repr,
  534. data: &'a [u8],
  535. },
  536. EchoRequest {
  537. ident: u16,
  538. seq_no: u16,
  539. data: &'a [u8],
  540. },
  541. EchoReply {
  542. ident: u16,
  543. seq_no: u16,
  544. data: &'a [u8],
  545. },
  546. #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
  547. Ndisc(NdiscRepr<'a>),
  548. Mld(MldRepr<'a>),
  549. #[cfg(feature = "proto-rpl")]
  550. Rpl(RplRepr<'a>),
  551. }
  552. impl<'a> Repr<'a> {
  553. /// Parse an Internet Control Message Protocol version 6 packet and return
  554. /// a high-level representation.
  555. pub fn parse<T>(
  556. src_addr: &IpAddress,
  557. dst_addr: &IpAddress,
  558. packet: &Packet<&'a T>,
  559. checksum_caps: &ChecksumCapabilities,
  560. ) -> Result<Repr<'a>>
  561. where
  562. T: AsRef<[u8]> + ?Sized,
  563. {
  564. fn create_packet_from_payload<'a, T>(packet: &Packet<&'a T>) -> Result<(&'a [u8], Ipv6Repr)>
  565. where
  566. T: AsRef<[u8]> + ?Sized,
  567. {
  568. // The packet must be truncated to fit the min MTU. Since we don't know the offset of
  569. // the ICMPv6 header in the L2 frame, we should only check whether the payload's IPv6
  570. // header is present, the rest is allowed to be truncated.
  571. let ip_packet = if packet.payload().len() >= IPV6_HEADER_LEN {
  572. Ipv6Packet::new_unchecked(packet.payload())
  573. } else {
  574. return Err(Error);
  575. };
  576. let payload = &packet.payload()[ip_packet.header_len()..];
  577. let repr = Ipv6Repr {
  578. src_addr: ip_packet.src_addr(),
  579. dst_addr: ip_packet.dst_addr(),
  580. next_header: ip_packet.next_header(),
  581. payload_len: ip_packet.payload_len().into(),
  582. hop_limit: ip_packet.hop_limit(),
  583. };
  584. Ok((payload, repr))
  585. }
  586. // Valid checksum is expected.
  587. if checksum_caps.icmpv6.rx() && !packet.verify_checksum(src_addr, dst_addr) {
  588. return Err(Error);
  589. }
  590. match (packet.msg_type(), packet.msg_code()) {
  591. (Message::DstUnreachable, code) => {
  592. let (payload, repr) = create_packet_from_payload(packet)?;
  593. Ok(Repr::DstUnreachable {
  594. reason: DstUnreachable::from(code),
  595. header: repr,
  596. data: payload,
  597. })
  598. }
  599. (Message::PktTooBig, 0) => {
  600. let (payload, repr) = create_packet_from_payload(packet)?;
  601. Ok(Repr::PktTooBig {
  602. mtu: packet.pkt_too_big_mtu(),
  603. header: repr,
  604. data: payload,
  605. })
  606. }
  607. (Message::TimeExceeded, code) => {
  608. let (payload, repr) = create_packet_from_payload(packet)?;
  609. Ok(Repr::TimeExceeded {
  610. reason: TimeExceeded::from(code),
  611. header: repr,
  612. data: payload,
  613. })
  614. }
  615. (Message::ParamProblem, code) => {
  616. let (payload, repr) = create_packet_from_payload(packet)?;
  617. Ok(Repr::ParamProblem {
  618. reason: ParamProblem::from(code),
  619. pointer: packet.param_problem_ptr(),
  620. header: repr,
  621. data: payload,
  622. })
  623. }
  624. (Message::EchoRequest, 0) => Ok(Repr::EchoRequest {
  625. ident: packet.echo_ident(),
  626. seq_no: packet.echo_seq_no(),
  627. data: packet.payload(),
  628. }),
  629. (Message::EchoReply, 0) => Ok(Repr::EchoReply {
  630. ident: packet.echo_ident(),
  631. seq_no: packet.echo_seq_no(),
  632. data: packet.payload(),
  633. }),
  634. #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
  635. (msg_type, 0) if msg_type.is_ndisc() => NdiscRepr::parse(packet).map(Repr::Ndisc),
  636. (msg_type, 0) if msg_type.is_mld() => MldRepr::parse(packet).map(Repr::Mld),
  637. #[cfg(feature = "proto-rpl")]
  638. (Message::RplControl, _) => RplRepr::parse(packet).map(Repr::Rpl),
  639. _ => Err(Error),
  640. }
  641. }
  642. /// Return the length of a packet that will be emitted from this high-level representation.
  643. pub fn buffer_len(&self) -> usize {
  644. match self {
  645. &Repr::DstUnreachable { header, data, .. }
  646. | &Repr::PktTooBig { header, data, .. }
  647. | &Repr::TimeExceeded { header, data, .. }
  648. | &Repr::ParamProblem { header, data, .. } => cmp::min(
  649. field::UNUSED.end + header.buffer_len() + data.len(),
  650. MAX_ERROR_PACKET_LEN,
  651. ),
  652. &Repr::EchoRequest { data, .. } | &Repr::EchoReply { data, .. } => {
  653. field::ECHO_SEQNO.end + data.len()
  654. }
  655. #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
  656. &Repr::Ndisc(ndisc) => ndisc.buffer_len(),
  657. &Repr::Mld(mld) => mld.buffer_len(),
  658. #[cfg(feature = "proto-rpl")]
  659. Repr::Rpl(rpl) => rpl.buffer_len(),
  660. }
  661. }
  662. /// Emit a high-level representation into an Internet Control Message Protocol version 6
  663. /// packet.
  664. pub fn emit<T>(
  665. &self,
  666. src_addr: &IpAddress,
  667. dst_addr: &IpAddress,
  668. packet: &mut Packet<&mut T>,
  669. checksum_caps: &ChecksumCapabilities,
  670. ) where
  671. T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
  672. {
  673. fn emit_contained_packet<T>(packet: &mut Packet<&mut T>, header: Ipv6Repr, data: &[u8])
  674. where
  675. T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
  676. {
  677. let icmp_header_len = packet.header_len();
  678. let mut ip_packet = Ipv6Packet::new_unchecked(packet.payload_mut());
  679. header.emit(&mut ip_packet);
  680. let payload = &mut ip_packet.into_inner()[header.buffer_len()..];
  681. // FIXME: this should rather be checked at link level, as we can't know in advance how
  682. // much space we have for the packet due to IPv6 options and etc
  683. let payload_len = cmp::min(
  684. data.len(),
  685. MAX_ERROR_PACKET_LEN - icmp_header_len - IPV6_HEADER_LEN,
  686. );
  687. payload[..payload_len].copy_from_slice(&data[..payload_len]);
  688. }
  689. match *self {
  690. Repr::DstUnreachable {
  691. reason,
  692. header,
  693. data,
  694. } => {
  695. packet.set_msg_type(Message::DstUnreachable);
  696. packet.set_msg_code(reason.into());
  697. emit_contained_packet(packet, header, data);
  698. }
  699. Repr::PktTooBig { mtu, header, data } => {
  700. packet.set_msg_type(Message::PktTooBig);
  701. packet.set_msg_code(0);
  702. packet.set_pkt_too_big_mtu(mtu);
  703. emit_contained_packet(packet, header, data);
  704. }
  705. Repr::TimeExceeded {
  706. reason,
  707. header,
  708. data,
  709. } => {
  710. packet.set_msg_type(Message::TimeExceeded);
  711. packet.set_msg_code(reason.into());
  712. emit_contained_packet(packet, header, data);
  713. }
  714. Repr::ParamProblem {
  715. reason,
  716. pointer,
  717. header,
  718. data,
  719. } => {
  720. packet.set_msg_type(Message::ParamProblem);
  721. packet.set_msg_code(reason.into());
  722. packet.set_param_problem_ptr(pointer);
  723. emit_contained_packet(packet, header, data);
  724. }
  725. Repr::EchoRequest {
  726. ident,
  727. seq_no,
  728. data,
  729. } => {
  730. packet.set_msg_type(Message::EchoRequest);
  731. packet.set_msg_code(0);
  732. packet.set_echo_ident(ident);
  733. packet.set_echo_seq_no(seq_no);
  734. let data_len = cmp::min(packet.payload_mut().len(), data.len());
  735. packet.payload_mut()[..data_len].copy_from_slice(&data[..data_len])
  736. }
  737. Repr::EchoReply {
  738. ident,
  739. seq_no,
  740. data,
  741. } => {
  742. packet.set_msg_type(Message::EchoReply);
  743. packet.set_msg_code(0);
  744. packet.set_echo_ident(ident);
  745. packet.set_echo_seq_no(seq_no);
  746. let data_len = cmp::min(packet.payload_mut().len(), data.len());
  747. packet.payload_mut()[..data_len].copy_from_slice(&data[..data_len])
  748. }
  749. #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
  750. Repr::Ndisc(ndisc) => ndisc.emit(packet),
  751. Repr::Mld(mld) => mld.emit(packet),
  752. #[cfg(feature = "proto-rpl")]
  753. Repr::Rpl(ref rpl) => rpl.emit(packet),
  754. }
  755. if checksum_caps.icmpv6.tx() {
  756. packet.fill_checksum(src_addr, dst_addr);
  757. } else {
  758. // make sure we get a consistently zeroed checksum, since implementations might rely on it
  759. packet.set_checksum(0);
  760. }
  761. }
  762. }
  763. #[cfg(test)]
  764. mod test {
  765. use super::*;
  766. use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2};
  767. use crate::wire::{IpProtocol, Ipv6Address, Ipv6Repr};
  768. static ECHO_PACKET_BYTES: [u8; 12] = [
  769. 0x80, 0x00, 0x19, 0xb3, 0x12, 0x34, 0xab, 0xcd, 0xaa, 0x00, 0x00, 0xff,
  770. ];
  771. static ECHO_PACKET_PAYLOAD: [u8; 4] = [0xaa, 0x00, 0x00, 0xff];
  772. static PKT_TOO_BIG_BYTES: [u8; 60] = [
  773. 0x02, 0x00, 0x0f, 0xc9, 0x00, 0x00, 0x05, 0xdc, 0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11,
  774. 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  775. 0x00, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  776. 0x00, 0x00, 0x02, 0xbf, 0x00, 0x00, 0x35, 0x00, 0x0c, 0x12, 0x4d, 0xaa, 0x00, 0x00, 0xff,
  777. ];
  778. static PKT_TOO_BIG_IP_PAYLOAD: [u8; 52] = [
  779. 0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
  780. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00,
  781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xbf, 0x00, 0x00, 0x35, 0x00,
  782. 0x0c, 0x12, 0x4d, 0xaa, 0x00, 0x00, 0xff,
  783. ];
  784. static PKT_TOO_BIG_UDP_PAYLOAD: [u8; 12] = [
  785. 0xbf, 0x00, 0x00, 0x35, 0x00, 0x0c, 0x12, 0x4d, 0xaa, 0x00, 0x00, 0xff,
  786. ];
  787. fn echo_packet_repr() -> Repr<'static> {
  788. Repr::EchoRequest {
  789. ident: 0x1234,
  790. seq_no: 0xabcd,
  791. data: &ECHO_PACKET_PAYLOAD,
  792. }
  793. }
  794. fn too_big_packet_repr() -> Repr<'static> {
  795. Repr::PktTooBig {
  796. mtu: 1500,
  797. header: Ipv6Repr {
  798. src_addr: Ipv6Address([
  799. 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  800. 0x00, 0x00, 0x01,
  801. ]),
  802. dst_addr: Ipv6Address([
  803. 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  804. 0x00, 0x00, 0x02,
  805. ]),
  806. next_header: IpProtocol::Udp,
  807. payload_len: 12,
  808. hop_limit: 0x40,
  809. },
  810. data: &PKT_TOO_BIG_UDP_PAYLOAD,
  811. }
  812. }
  813. #[test]
  814. fn test_echo_deconstruct() {
  815. let packet = Packet::new_unchecked(&ECHO_PACKET_BYTES[..]);
  816. assert_eq!(packet.msg_type(), Message::EchoRequest);
  817. assert_eq!(packet.msg_code(), 0);
  818. assert_eq!(packet.checksum(), 0x19b3);
  819. assert_eq!(packet.echo_ident(), 0x1234);
  820. assert_eq!(packet.echo_seq_no(), 0xabcd);
  821. assert_eq!(packet.payload(), &ECHO_PACKET_PAYLOAD[..]);
  822. assert!(packet.verify_checksum(&MOCK_IP_ADDR_1, &MOCK_IP_ADDR_2));
  823. assert!(!packet.msg_type().is_error());
  824. }
  825. #[test]
  826. fn test_echo_construct() {
  827. let mut bytes = vec![0xa5; 12];
  828. let mut packet = Packet::new_unchecked(&mut bytes);
  829. packet.set_msg_type(Message::EchoRequest);
  830. packet.set_msg_code(0);
  831. packet.set_echo_ident(0x1234);
  832. packet.set_echo_seq_no(0xabcd);
  833. packet
  834. .payload_mut()
  835. .copy_from_slice(&ECHO_PACKET_PAYLOAD[..]);
  836. packet.fill_checksum(&MOCK_IP_ADDR_1, &MOCK_IP_ADDR_2);
  837. assert_eq!(&*packet.into_inner(), &ECHO_PACKET_BYTES[..]);
  838. }
  839. #[test]
  840. fn test_echo_repr_parse() {
  841. let packet = Packet::new_unchecked(&ECHO_PACKET_BYTES[..]);
  842. let repr = Repr::parse(
  843. &MOCK_IP_ADDR_1,
  844. &MOCK_IP_ADDR_2,
  845. &packet,
  846. &ChecksumCapabilities::default(),
  847. )
  848. .unwrap();
  849. assert_eq!(repr, echo_packet_repr());
  850. }
  851. #[test]
  852. fn test_echo_emit() {
  853. let repr = echo_packet_repr();
  854. let mut bytes = vec![0xa5; repr.buffer_len()];
  855. let mut packet = Packet::new_unchecked(&mut bytes);
  856. repr.emit(
  857. &MOCK_IP_ADDR_1,
  858. &MOCK_IP_ADDR_2,
  859. &mut packet,
  860. &ChecksumCapabilities::default(),
  861. );
  862. assert_eq!(&*packet.into_inner(), &ECHO_PACKET_BYTES[..]);
  863. }
  864. #[test]
  865. fn test_too_big_deconstruct() {
  866. let packet = Packet::new_unchecked(&PKT_TOO_BIG_BYTES[..]);
  867. assert_eq!(packet.msg_type(), Message::PktTooBig);
  868. assert_eq!(packet.msg_code(), 0);
  869. assert_eq!(packet.checksum(), 0x0fc9);
  870. assert_eq!(packet.pkt_too_big_mtu(), 1500);
  871. assert_eq!(packet.payload(), &PKT_TOO_BIG_IP_PAYLOAD[..]);
  872. assert!(packet.verify_checksum(&MOCK_IP_ADDR_1, &MOCK_IP_ADDR_2));
  873. assert!(packet.msg_type().is_error());
  874. }
  875. #[test]
  876. fn test_too_big_construct() {
  877. let mut bytes = vec![0xa5; 60];
  878. let mut packet = Packet::new_unchecked(&mut bytes);
  879. packet.set_msg_type(Message::PktTooBig);
  880. packet.set_msg_code(0);
  881. packet.set_pkt_too_big_mtu(1500);
  882. packet
  883. .payload_mut()
  884. .copy_from_slice(&PKT_TOO_BIG_IP_PAYLOAD[..]);
  885. packet.fill_checksum(&MOCK_IP_ADDR_1, &MOCK_IP_ADDR_2);
  886. assert_eq!(&*packet.into_inner(), &PKT_TOO_BIG_BYTES[..]);
  887. }
  888. #[test]
  889. fn test_too_big_repr_parse() {
  890. let packet = Packet::new_unchecked(&PKT_TOO_BIG_BYTES[..]);
  891. let repr = Repr::parse(
  892. &MOCK_IP_ADDR_1,
  893. &MOCK_IP_ADDR_2,
  894. &packet,
  895. &ChecksumCapabilities::default(),
  896. )
  897. .unwrap();
  898. assert_eq!(repr, too_big_packet_repr());
  899. }
  900. #[test]
  901. fn test_too_big_emit() {
  902. let repr = too_big_packet_repr();
  903. let mut bytes = vec![0xa5; repr.buffer_len()];
  904. let mut packet = Packet::new_unchecked(&mut bytes);
  905. repr.emit(
  906. &MOCK_IP_ADDR_1,
  907. &MOCK_IP_ADDR_2,
  908. &mut packet,
  909. &ChecksumCapabilities::default(),
  910. );
  911. assert_eq!(&*packet.into_inner(), &PKT_TOO_BIG_BYTES[..]);
  912. }
  913. #[test]
  914. fn test_buffer_length_is_truncated_to_mtu() {
  915. let repr = Repr::PktTooBig {
  916. mtu: 1280,
  917. header: Ipv6Repr {
  918. src_addr: Default::default(),
  919. dst_addr: Default::default(),
  920. next_header: IpProtocol::Tcp,
  921. hop_limit: 64,
  922. payload_len: 1280,
  923. },
  924. data: &vec![0; 9999],
  925. };
  926. assert_eq!(repr.buffer_len(), 1280 - IPV6_HEADER_LEN);
  927. }
  928. #[test]
  929. fn test_mtu_truncated_payload_roundtrip() {
  930. let ip_packet_repr = Ipv6Repr {
  931. src_addr: Default::default(),
  932. dst_addr: Default::default(),
  933. next_header: IpProtocol::Tcp,
  934. hop_limit: 64,
  935. payload_len: IPV6_MIN_MTU - IPV6_HEADER_LEN,
  936. };
  937. let mut ip_packet = Ipv6Packet::new_unchecked(vec![0; IPV6_MIN_MTU]);
  938. ip_packet_repr.emit(&mut ip_packet);
  939. let repr1 = Repr::PktTooBig {
  940. mtu: IPV6_MIN_MTU as u32,
  941. header: ip_packet_repr,
  942. data: &ip_packet.as_ref()[IPV6_HEADER_LEN..],
  943. };
  944. // this is needed to make sure roundtrip gives the same value
  945. // it is not needed for ensuring the correct bytes get emitted
  946. let repr1 = Repr::PktTooBig {
  947. mtu: IPV6_MIN_MTU as u32,
  948. header: ip_packet_repr,
  949. data: &ip_packet.as_ref()[IPV6_HEADER_LEN..repr1.buffer_len() - field::UNUSED.end],
  950. };
  951. let mut data = vec![0; MAX_ERROR_PACKET_LEN];
  952. let mut packet = Packet::new_unchecked(&mut data);
  953. repr1.emit(
  954. &MOCK_IP_ADDR_1,
  955. &MOCK_IP_ADDR_2,
  956. &mut packet,
  957. &ChecksumCapabilities::default(),
  958. );
  959. let packet = Packet::new_unchecked(&data);
  960. let repr2 = Repr::parse(
  961. &MOCK_IP_ADDR_1,
  962. &MOCK_IP_ADDR_2,
  963. &packet,
  964. &ChecksumCapabilities::default(),
  965. )
  966. .unwrap();
  967. assert_eq!(repr1, repr2);
  968. }
  969. #[test]
  970. fn test_truncated_payload_ipv6_header_parse_fails() {
  971. let repr = too_big_packet_repr();
  972. let mut bytes = vec![0xa5; repr.buffer_len()];
  973. let mut packet = Packet::new_unchecked(&mut bytes);
  974. repr.emit(
  975. &MOCK_IP_ADDR_1,
  976. &MOCK_IP_ADDR_2,
  977. &mut packet,
  978. &ChecksumCapabilities::default(),
  979. );
  980. let packet = Packet::new_unchecked(&bytes[..field::HEADER_END + IPV6_HEADER_LEN - 1]);
  981. assert!(Repr::parse(
  982. &MOCK_IP_ADDR_1,
  983. &MOCK_IP_ADDR_2,
  984. &packet,
  985. &ChecksumCapabilities::ignored(),
  986. )
  987. .is_err());
  988. }
  989. }