ipsec_ah.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use byteorder::{ByteOrder, NetworkEndian};
  2. use super::{Error, IpProtocol, Result};
  3. /// A read/write wrapper around an IPSec Authentication Header packet buffer.
  4. #[derive(Debug, PartialEq, Eq)]
  5. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  6. pub struct Packet<T: AsRef<[u8]>> {
  7. buffer: T,
  8. }
  9. mod field {
  10. use crate::wire::field::Field;
  11. pub const NEXT_HEADER: usize = 0;
  12. pub const PAYLOAD_LEN: usize = 1;
  13. pub const RESERVED: Field = 2..4;
  14. pub const SPI: Field = 4..8;
  15. pub const SEQUENCE_NUMBER: Field = 8..16;
  16. }
  17. impl<T: AsRef<[u8]>> Packet<T> {
  18. /// Imbue a raw octet buffer with IPsec Authentication Header packet structure.
  19. pub const fn new_unchecked(buffer: T) -> Packet<T> {
  20. Packet { buffer }
  21. }
  22. /// Shorthand for a combination of [new_unchecked] and [check_len].
  23. ///
  24. /// [new_unchecked]: #method.new_unchecked
  25. /// [check_len]: #method.check_len
  26. pub fn new_checked(buffer: T) -> Result<Packet<T>> {
  27. let packet = Self::new_unchecked(buffer);
  28. packet.check_len()?;
  29. Ok(packet)
  30. }
  31. /// Ensure that no accessor method will panic if called.
  32. /// Returns `Err(Error)` if the buffer is too short.
  33. /// Returns `Err(Error)` if the buffer is shorter than payload length
  34. ///
  35. /// The result of this check is invalidated by calling [set_payload_len].
  36. ///
  37. /// [set_payload_len]: #method.set_payload_len
  38. #[allow(clippy::if_same_then_else)]
  39. pub fn check_len(&self) -> Result<()> {
  40. let len = self.buffer.as_ref().len();
  41. if len < field::SEQUENCE_NUMBER.end {
  42. Err(Error)
  43. } else if len < self.header_len() {
  44. Err(Error)
  45. } else {
  46. Ok(())
  47. }
  48. }
  49. /// Consume the packet, returning the underlying buffer.
  50. pub fn into_inner(self) -> T {
  51. self.buffer
  52. }
  53. /// Return length of Authentication Header in octets
  54. pub fn header_len(&self) -> usize {
  55. (self.payload_len() as usize + 2) * 4
  56. }
  57. /// Return next header protocol type
  58. /// The value is taken from the list of IP protocol numbers.
  59. pub fn next_header(&self) -> IpProtocol {
  60. let data = self.buffer.as_ref();
  61. IpProtocol::from(data[field::NEXT_HEADER])
  62. }
  63. /// Return the length of this Authentication Header in 4-octet units, minus 2
  64. pub fn payload_len(&self) -> u8 {
  65. let data = self.buffer.as_ref();
  66. data[field::PAYLOAD_LEN]
  67. }
  68. /// Return the security parameters index
  69. pub fn security_parameters_index(&self) -> u32 {
  70. let field = &self.buffer.as_ref()[field::SPI];
  71. NetworkEndian::read_u32(field)
  72. }
  73. /// Return sequence number
  74. pub fn sequence_number(&self) -> u32 {
  75. let field = &self.buffer.as_ref()[field::SEQUENCE_NUMBER];
  76. NetworkEndian::read_u32(field)
  77. }
  78. }
  79. impl<'a, T: AsRef<[u8]> + ?Sized> Packet<&'a T> {
  80. /// Return a pointer to the integrity check value
  81. #[inline]
  82. pub fn integrity_check_value(&self) -> &'a [u8] {
  83. let range = field::SEQUENCE_NUMBER.end..self.header_len();
  84. let data = self.buffer.as_ref();
  85. &data[range]
  86. }
  87. }
  88. impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {
  89. fn as_ref(&self) -> &[u8] {
  90. self.buffer.as_ref()
  91. }
  92. }
  93. impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
  94. /// Set next header protocol field
  95. fn set_next_header(&mut self, value: IpProtocol) {
  96. let data = self.buffer.as_mut();
  97. data[field::NEXT_HEADER] = value.into()
  98. }
  99. /// Set payload length field
  100. fn set_payload_len(&mut self, value: u8) {
  101. let data = self.buffer.as_mut();
  102. data[field::PAYLOAD_LEN] = value
  103. }
  104. /// Set security parameters index field
  105. fn set_security_parameters_index(&mut self, value: u32) {
  106. let data = self.buffer.as_mut();
  107. NetworkEndian::write_u32(&mut data[field::SPI], value)
  108. }
  109. /// Set sequence number
  110. fn set_sequence_number(&mut self, value: u32) {
  111. let data = self.buffer.as_mut();
  112. NetworkEndian::write_u32(&mut data[field::SEQUENCE_NUMBER], value)
  113. }
  114. /// Return a mutable pointer to the integrity check value.
  115. #[inline]
  116. pub fn integrity_check_value_mut(&mut self) -> &mut [u8] {
  117. let range = field::SEQUENCE_NUMBER.end..self.header_len();
  118. let data = self.buffer.as_mut();
  119. &mut data[range]
  120. }
  121. }