1use core::num::NonZeroU8;
8use core::time::Duration;
9
10use net_types::ip::{Ipv6, Ipv6Addr};
11use zerocopy::byteorder::network_endian::{U16, U32};
12use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, SplitByteSlice, Unaligned};
13
14use crate::icmp::{IcmpIpExt, IcmpPacket, IcmpPacketRaw, IcmpZeroCode};
15use crate::utils::NonZeroDuration;
16
17#[allow(missing_docs)]
19#[derive(Debug)]
20pub enum NdpPacket<B: SplitByteSlice> {
21 RouterSolicitation(IcmpPacket<Ipv6, B, RouterSolicitation>),
22 RouterAdvertisement(IcmpPacket<Ipv6, B, RouterAdvertisement>),
23 NeighborSolicitation(IcmpPacket<Ipv6, B, NeighborSolicitation>),
24 NeighborAdvertisement(IcmpPacket<Ipv6, B, NeighborAdvertisement>),
25 Redirect(IcmpPacket<Ipv6, B, Redirect>),
26}
27
28#[allow(missing_docs)]
30#[derive(Debug)]
31pub enum NdpPacketRaw<B: SplitByteSlice> {
32 RouterSolicitation(IcmpPacketRaw<Ipv6, B, RouterSolicitation>),
33 RouterAdvertisement(IcmpPacketRaw<Ipv6, B, RouterAdvertisement>),
34 NeighborSolicitation(IcmpPacketRaw<Ipv6, B, NeighborSolicitation>),
35 NeighborAdvertisement(IcmpPacketRaw<Ipv6, B, NeighborAdvertisement>),
36 Redirect(IcmpPacketRaw<Ipv6, B, Redirect>),
37}
38
39#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
41pub enum NonZeroNdpLifetime {
42 Finite(NonZeroDuration),
50
51 Infinite,
53}
54
55impl NonZeroNdpLifetime {
56 pub fn from_u32_with_infinite(lifetime: u32) -> Option<NonZeroNdpLifetime> {
59 match lifetime {
77 u32::MAX => Some(NonZeroNdpLifetime::Infinite),
78 finite => NonZeroDuration::new(Duration::from_secs(finite.into()))
79 .map(NonZeroNdpLifetime::Finite),
80 }
81 }
82
83 pub fn min_finite_duration(self, other: NonZeroDuration) -> NonZeroDuration {
85 match self {
86 NonZeroNdpLifetime::Finite(lifetime) => core::cmp::min(lifetime, other),
87 NonZeroNdpLifetime::Infinite => other,
88 }
89 }
90}
91
92pub type Options<B> = packet::records::options::Options<B, options::NdpOptionsImpl>;
98
99pub type OptionSequenceBuilder<'a, I> =
105 packet::records::options::OptionSequenceBuilder<options::NdpOptionBuilder<'a>, I>;
106
107#[derive(
109 Copy,
110 Clone,
111 Default,
112 Debug,
113 KnownLayout,
114 FromBytes,
115 IntoBytes,
116 Immutable,
117 Unaligned,
118 PartialEq,
119 Eq,
120)]
121#[repr(C)]
122pub struct RouterSolicitation {
123 _reserved: [u8; 4],
124}
125
126impl_icmp_message!(Ipv6, RouterSolicitation, RouterSolicitation, IcmpZeroCode, Options<B>);
127
128#[allow(missing_docs)]
132#[derive(Copy, Clone, Debug, PartialEq, Eq)]
133pub enum RoutePreference {
134 High,
140 Medium,
141 Low,
142}
143
144impl Default for RoutePreference {
145 fn default() -> RoutePreference {
146 RoutePreference::Medium
156 }
157}
158
159impl From<RoutePreference> for u8 {
160 fn from(v: RoutePreference) -> u8 {
161 match v {
171 RoutePreference::High => 0b01,
172 RoutePreference::Medium => 0b00,
173 RoutePreference::Low => 0b11,
174 }
175 }
176}
177
178impl TryFrom<u8> for RoutePreference {
179 type Error = ();
180
181 fn try_from(v: u8) -> Result<Self, Self::Error> {
182 match v {
192 0b01 => Ok(RoutePreference::High),
193 0b00 => Ok(RoutePreference::Medium),
194 0b11 => Ok(RoutePreference::Low),
195 _ => Err(()),
196 }
197 }
198}
199
200#[derive(
202 Copy, Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq,
203)]
204#[repr(C)]
205pub struct RouterAdvertisement {
206 current_hop_limit: u8,
207 configuration_mo: u8,
208 router_lifetime: U16,
209 reachable_time: U32,
210 retransmit_timer: U32,
211}
212
213impl_icmp_message!(Ipv6, RouterAdvertisement, RouterAdvertisement, IcmpZeroCode, Options<B>);
214
215impl RouterAdvertisement {
216 const MANAGED_FLAG: u8 = 0x80;
224
225 const OTHER_CONFIGURATION_FLAG: u8 = 0x40;
231
232 const DEFAULT_ROUTER_PREFERENCE_SHIFT: u8 = 3;
256 const DEFAULT_ROUTER_PREFERENCE_MASK: u8 = 0b11 << Self::DEFAULT_ROUTER_PREFERENCE_SHIFT;
257
258 pub fn new(
262 current_hop_limit: u8,
263 managed_flag: bool,
264 other_config_flag: bool,
265 router_lifetime: u16,
266 reachable_time: u32,
267 retransmit_timer: u32,
268 ) -> Self {
269 Self::with_prf(
270 current_hop_limit,
271 managed_flag,
272 other_config_flag,
273 RoutePreference::default(),
274 router_lifetime,
275 reachable_time,
276 retransmit_timer,
277 )
278 }
279
280 pub fn with_prf(
282 current_hop_limit: u8,
283 managed_flag: bool,
284 other_config_flag: bool,
285 preference: RoutePreference,
286 router_lifetime: u16,
287 reachable_time: u32,
288 retransmit_timer: u32,
289 ) -> Self {
290 let mut configuration_mo = 0;
291
292 if managed_flag {
293 configuration_mo |= Self::MANAGED_FLAG;
294 }
295
296 if other_config_flag {
297 configuration_mo |= Self::OTHER_CONFIGURATION_FLAG;
298 }
299
300 configuration_mo |= (u8::from(preference) << Self::DEFAULT_ROUTER_PREFERENCE_SHIFT)
301 & Self::DEFAULT_ROUTER_PREFERENCE_MASK;
302
303 Self {
304 current_hop_limit,
305 configuration_mo,
306 router_lifetime: U16::new(router_lifetime),
307 reachable_time: U32::new(reachable_time),
308 retransmit_timer: U32::new(retransmit_timer),
309 }
310 }
311
312 pub fn current_hop_limit(&self) -> Option<NonZeroU8> {
316 NonZeroU8::new(self.current_hop_limit)
317 }
318
319 pub fn router_lifetime(&self) -> Option<NonZeroDuration> {
324 NonZeroDuration::new(Duration::from_secs(self.router_lifetime.get().into()))
327 }
328
329 pub fn reachable_time(&self) -> Option<NonZeroDuration> {
333 NonZeroDuration::new(Duration::from_millis(self.reachable_time.get().into()))
336 }
337
338 pub fn retransmit_timer(&self) -> Option<NonZeroDuration> {
342 NonZeroDuration::new(Duration::from_millis(self.retransmit_timer.get().into()))
345 }
346
347 pub fn preference(&self) -> RoutePreference {
349 let preference = (self.configuration_mo & Self::DEFAULT_ROUTER_PREFERENCE_MASK)
350 >> Self::DEFAULT_ROUTER_PREFERENCE_SHIFT;
351 RoutePreference::try_from(preference).unwrap_or_default()
355 }
356}
357
358#[derive(
360 Copy, Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq,
361)]
362#[repr(C)]
363pub struct NeighborSolicitation {
364 _reserved: [u8; 4],
365 target_address: Ipv6Addr,
366}
367
368impl_icmp_message!(Ipv6, NeighborSolicitation, NeighborSolicitation, IcmpZeroCode, Options<B>);
369
370impl NeighborSolicitation {
371 pub fn new(target_address: Ipv6Addr) -> Self {
374 Self { _reserved: [0; 4], target_address }
375 }
376
377 pub fn target_address(&self) -> &Ipv6Addr {
379 &self.target_address
380 }
381}
382
383#[derive(
385 Copy, Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq,
386)]
387#[repr(C)]
388pub struct NeighborAdvertisement {
389 flags_rso: u8,
390 _reserved: [u8; 3],
391 target_address: Ipv6Addr,
392}
393
394impl_icmp_message!(Ipv6, NeighborAdvertisement, NeighborAdvertisement, IcmpZeroCode, Options<B>);
395
396impl NeighborAdvertisement {
397 const FLAG_ROUTER: u8 = 0x80;
403
404 const FLAG_SOLICITED: u8 = 0x40;
412
413 const FLAG_OVERRIDE: u8 = 0x20;
424
425 pub fn new(
428 router_flag: bool,
429 solicited_flag: bool,
430 override_flag: bool,
431 target_address: Ipv6Addr,
432 ) -> Self {
433 let mut flags_rso = 0;
434
435 if router_flag {
436 flags_rso |= Self::FLAG_ROUTER;
437 }
438
439 if solicited_flag {
440 flags_rso |= Self::FLAG_SOLICITED;
441 }
442
443 if override_flag {
444 flags_rso |= Self::FLAG_OVERRIDE;
445 }
446
447 Self { flags_rso, _reserved: [0; 3], target_address }
448 }
449
450 pub fn target_address(&self) -> &Ipv6Addr {
452 &self.target_address
453 }
454
455 pub fn router_flag(&self) -> bool {
457 (self.flags_rso & Self::FLAG_ROUTER) != 0
458 }
459
460 pub fn solicited_flag(&self) -> bool {
462 (self.flags_rso & Self::FLAG_SOLICITED) != 0
463 }
464
465 pub fn override_flag(&self) -> bool {
467 (self.flags_rso & Self::FLAG_OVERRIDE) != 0
468 }
469}
470
471#[derive(
473 Copy, Clone, Debug, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq,
474)]
475#[repr(C)]
476pub struct Redirect {
477 _reserved: [u8; 4],
478 target_address: Ipv6Addr,
479 destination_address: Ipv6Addr,
480}
481
482impl_icmp_message!(Ipv6, Redirect, Redirect, IcmpZeroCode, Options<B>);
483
484pub mod options {
486 use core::num::NonZeroUsize;
487 use core::time::Duration;
488
489 use byteorder::{ByteOrder, NetworkEndian};
490 use net_types::UnicastAddress;
491 use net_types::ip::{IpAddress as _, Ipv6Addr, Subnet, SubnetError};
492 use packet::BufferView as _;
493 use packet::records::options::{
494 LengthEncoding, OptionBuilder, OptionLayout, OptionParseErr, OptionParseLayout, OptionsImpl,
495 };
496 use zerocopy::byteorder::network_endian::U32;
497 use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, SplitByteSlice, Unaligned};
498
499 use super::NonZeroNdpLifetime;
500 use crate::utils::NonZeroDuration;
501
502 pub const INFINITE_LIFETIME_SECONDS: u32 = u32::MAX;
504
505 pub const INFINITE_LIFETIME: NonZeroDuration =
508 NonZeroDuration::from_secs(INFINITE_LIFETIME_SECONDS as u64).unwrap();
509
510 const REDIRECTED_HEADER_OPTION_RESERVED_BYTES_LENGTH: usize = 6;
517
518 const MTU_OPTION_LENGTH: usize = 6;
524
525 const MTU_OPTION_RESERVED_BYTES_LENGTH: usize = 2;
532
533 pub const MIN_NONCE_LENGTH: usize = 6;
539
540 const MIN_RECURSIVE_DNS_SERVER_OPTION_LENGTH: usize = 22;
550
551 const RECURSIVE_DNS_SERVER_OPTION_RESERVED_BYTES_LENGTH: usize = 2;
558
559 const ROUTE_INFORMATION_PREFERENCE_RESERVED_BITS_RIGHT: u8 = 3;
565
566 const ROUTE_INFORMATION_PREFERENCE_MASK: u8 = 0x18;
572
573 const OPTION_BYTES_PER_LENGTH_UNIT: usize = 8;
579
580 #[derive(Debug, PartialEq, Eq, Clone)]
586 pub struct RecursiveDnsServer<'a> {
587 lifetime: u32,
588 addresses: &'a [Ipv6Addr],
589 }
590
591 impl<'a> RecursiveDnsServer<'a> {
592 pub const INFINITE_LIFETIME: u32 = INFINITE_LIFETIME_SECONDS;
594
595 pub fn new(lifetime: u32, addresses: &'a [Ipv6Addr]) -> RecursiveDnsServer<'a> {
597 RecursiveDnsServer { lifetime, addresses }
598 }
599
600 pub fn lifetime(&self) -> Option<NonZeroDuration> {
606 NonZeroDuration::new(Duration::from_secs(self.lifetime.into()))
607 }
608
609 pub fn iter_addresses(&self) -> &'a [Ipv6Addr] {
611 self.addresses
612 }
613
614 pub fn parse(data: &'a [u8]) -> Result<Self, OptionParseErr> {
617 if data.len() < MIN_RECURSIVE_DNS_SERVER_OPTION_LENGTH {
618 return Err(OptionParseErr);
619 }
620
621 let (_, data) = data.split_at(RECURSIVE_DNS_SERVER_OPTION_RESERVED_BYTES_LENGTH);
624
625 let (lifetime, data) = Ref::<_, U32>::from_prefix(data).map_err(|_| OptionParseErr)?;
628
629 let addresses = Ref::into_ref(
632 Ref::<_, [Ipv6Addr]>::from_bytes(data)
633 .map_err(Into::into)
634 .map_err(|_: zerocopy::SizeError<_, _>| OptionParseErr)?,
635 );
636
637 if !addresses.iter().all(UnicastAddress::is_unicast) {
639 return Err(OptionParseErr);
640 }
641
642 Ok(Self::new(lifetime.get(), addresses))
643 }
644 }
645
646 #[derive(KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned)]
669 #[repr(C)]
670 struct RouteInformationHeader {
671 prefix_length: u8,
672 flags: u8,
673 route_lifetime: U32,
674 }
675
676 impl RouteInformationHeader {
677 const PREFERENCE_SHIFT: u8 = 3;
693 const PREFERENCE_MASK: u8 = 0b11 << Self::PREFERENCE_SHIFT;
694
695 fn set_preference(&mut self, preference: super::RoutePreference) {
696 let preference: u8 = preference.into();
697
698 self.flags &= !Self::PREFERENCE_MASK;
699 self.flags |= (preference << Self::PREFERENCE_SHIFT) & Self::PREFERENCE_MASK;
700 }
701 }
702
703 #[derive(Debug, PartialEq, Eq)]
709 pub struct RouteInformation {
710 prefix: Subnet<Ipv6Addr>,
711 route_lifetime_seconds: u32,
712 preference: super::RoutePreference,
713 }
714
715 impl RouteInformation {
716 pub fn new(
718 prefix: Subnet<Ipv6Addr>,
719 route_lifetime_seconds: u32,
720 preference: super::RoutePreference,
721 ) -> Self {
722 Self { prefix, route_lifetime_seconds, preference }
723 }
724
725 pub fn prefix(&self) -> &Subnet<Ipv6Addr> {
727 &self.prefix
728 }
729
730 pub fn preference(&self) -> super::RoutePreference {
732 self.preference
733 }
734
735 pub fn route_lifetime(&self) -> Option<NonZeroNdpLifetime> {
737 NonZeroNdpLifetime::from_u32_with_infinite(self.route_lifetime_seconds)
738 }
739
740 fn prefix_bytes_len(&self) -> usize {
741 let RouteInformation { prefix, route_lifetime_seconds: _, preference: _ } = self;
742
743 let prefix_length = prefix.prefix();
744 if prefix_length == 0 {
757 0
758 } else if prefix_length <= 64 {
759 core::mem::size_of::<Ipv6Addr>() / 2
760 } else {
761 core::mem::size_of::<Ipv6Addr>()
762 }
763 }
764
765 fn serialized_len(&self) -> usize {
766 core::mem::size_of::<RouteInformationHeader>() + self.prefix_bytes_len()
767 }
768
769 fn serialize(&self, buffer: &mut [u8]) {
770 let (mut hdr, buffer) = Ref::<_, RouteInformationHeader>::from_prefix(buffer)
771 .expect("expected buffer to hold enough bytes for serialization");
772
773 let prefix_bytes_len = self.prefix_bytes_len();
774 let RouteInformation { prefix, route_lifetime_seconds, preference } = self;
775
776 hdr.prefix_length = prefix.prefix();
777 hdr.set_preference(*preference);
778 hdr.route_lifetime.set(*route_lifetime_seconds);
779 buffer[..prefix_bytes_len]
780 .copy_from_slice(&prefix.network().bytes()[..prefix_bytes_len])
781 }
782 }
783
784 const PREFIX_INFORMATION_OPTION_LENGTH: usize = 30;
791
792 #[derive(
798 Debug, KnownLayout, FromBytes, IntoBytes, Immutable, Unaligned, PartialEq, Eq, Clone,
799 )]
800 #[repr(C)]
801 pub struct PrefixInformation {
802 prefix_length: u8,
803 flags_la: u8,
804 valid_lifetime: U32,
805 preferred_lifetime: U32,
806 _reserved: [u8; 4],
807 prefix: Ipv6Addr,
808 }
809
810 impl PrefixInformation {
811 const ON_LINK_FLAG: u8 = 0x80;
817
818 const AUTONOMOUS_ADDRESS_CONFIGURATION_FLAG: u8 = 0x40;
825
826 pub fn new(
828 prefix_length: u8,
829 on_link_flag: bool,
830 autonomous_address_configuration_flag: bool,
831 valid_lifetime: u32,
832 preferred_lifetime: u32,
833 prefix: Ipv6Addr,
834 ) -> Self {
835 let mut flags_la = 0;
836
837 if on_link_flag {
838 flags_la |= Self::ON_LINK_FLAG;
839 }
840
841 if autonomous_address_configuration_flag {
842 flags_la |= Self::AUTONOMOUS_ADDRESS_CONFIGURATION_FLAG;
843 }
844
845 Self {
846 prefix_length,
847 flags_la,
848 valid_lifetime: U32::new(valid_lifetime),
849 preferred_lifetime: U32::new(preferred_lifetime),
850 _reserved: [0; 4],
851 prefix,
852 }
853 }
854
855 pub fn prefix_length(&self) -> u8 {
857 self.prefix_length
858 }
859
860 pub fn on_link_flag(&self) -> bool {
867 (self.flags_la & Self::ON_LINK_FLAG) != 0
868 }
869
870 pub fn autonomous_address_configuration_flag(&self) -> bool {
872 (self.flags_la & Self::AUTONOMOUS_ADDRESS_CONFIGURATION_FLAG) != 0
873 }
874
875 pub fn valid_lifetime(&self) -> Option<NonZeroNdpLifetime> {
881 NonZeroNdpLifetime::from_u32_with_infinite(self.valid_lifetime.get())
882 }
883
884 pub fn preferred_lifetime(&self) -> Option<NonZeroNdpLifetime> {
890 NonZeroNdpLifetime::from_u32_with_infinite(self.preferred_lifetime.get())
891 }
892
893 pub fn prefix(&self) -> &Ipv6Addr {
900 &self.prefix
901 }
902
903 pub fn subnet(&self) -> Result<Subnet<Ipv6Addr>, SubnetError> {
905 Subnet::new(self.prefix, self.prefix_length)
906 }
907 }
908
909 pub mod option_types {
911 pub const PREFIX_INFORMATION: u8 = 3;
913
914 pub const RECURSIVE_DNS_SERVER: u8 = 25;
916
917 pub const DNS_SEARCH_LIST: u8 = 31;
919
920 pub const SIXLOWPAN_CONTEXT: u8 = 34;
922
923 pub const CAPTIVE_PORTAL: u8 = 37;
925
926 pub const PREF64: u8 = 38;
928
929 pub fn debug_name(option_type: u8) -> Option<&'static str> {
931 match option_type {
934 super::option_types::PREFIX_INFORMATION => Some("PREFIX_INFORMATION"),
935 super::option_types::RECURSIVE_DNS_SERVER => Some("RECURSIVE_DNS_SERVER"),
936 super::option_types::DNS_SEARCH_LIST => Some("DNS_SEARCH_LIST"),
937 super::option_types::SIXLOWPAN_CONTEXT => Some("SIXLOWPAN_CONTEXT"),
938 super::option_types::CAPTIVE_PORTAL => Some("CAPTIVE_PORTAL"),
939 super::option_types::PREF64 => Some("PREF64"),
940 _ => None,
941 }
942 }
943 }
944
945 use option_types::{PREFIX_INFORMATION, RECURSIVE_DNS_SERVER};
946
947 create_protocol_enum!(
948 #[allow(missing_docs)]
950 pub enum NdpOptionType: u8 {
951 SourceLinkLayerAddress, 1, "Source Link-Layer Address";
952 TargetLinkLayerAddress, 2, "Target Link-Layer Address";
953 PrefixInformation, PREFIX_INFORMATION, "Prefix Information";
954 RedirectedHeader, 4, "Redirected Header";
955 Mtu, 5, "MTU";
956 Nonce, 14, "Nonce";
957 RouteInformation, 24, "Route Information";
958 RecursiveDnsServer, RECURSIVE_DNS_SERVER, "Recursive DNS Server";
959 }
960 );
961
962 #[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
969 pub struct NdpNonce<B: SplitByteSlice> {
970 nonce: B,
971 }
972
973 impl<B: SplitByteSlice> NdpNonce<B> {
974 pub fn bytes(&self) -> &[u8] {
976 let Self { nonce } = self;
977 nonce.deref()
978 }
979
980 pub fn new(value: B) -> Result<Self, InvalidNonceError> {
983 let bytes = value.deref();
984 let nonce_option_length_bytes = bytes.len() + 2;
988 if nonce_option_length_bytes % 8 != 0 {
989 return Err(InvalidNonceError::ResultsInNonMultipleOf8);
990 }
991
992 let nonce_option_length_in_groups_of_8_bytes = nonce_option_length_bytes / 8;
993
994 match u8::try_from(nonce_option_length_in_groups_of_8_bytes) {
997 Ok(_) => (),
998 Err(_) => return Err(InvalidNonceError::TooLong),
999 };
1000
1001 Ok(Self { nonce: value })
1002 }
1003 }
1004
1005 impl<B: SplitByteSlice> AsRef<[u8]> for NdpNonce<B> {
1006 fn as_ref(&self) -> &[u8] {
1007 self.bytes()
1008 }
1009 }
1010
1011 impl<'a> From<&'a [u8; MIN_NONCE_LENGTH]> for NdpNonce<&'a [u8]> {
1014 fn from(value: &'a [u8; MIN_NONCE_LENGTH]) -> Self {
1015 Self { nonce: &value[..] }
1016 }
1017 }
1018
1019 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
1021 pub enum InvalidNonceError {
1022 ResultsInNonMultipleOf8,
1025 TooLong,
1027 }
1028
1029 #[allow(missing_docs)]
1031 #[derive(Debug, PartialEq, Eq)]
1032 pub enum NdpOption<'a> {
1033 SourceLinkLayerAddress(&'a [u8]),
1034 TargetLinkLayerAddress(&'a [u8]),
1035 PrefixInformation(&'a PrefixInformation),
1036
1037 RedirectedHeader { original_packet: &'a [u8] },
1038
1039 Mtu(u32),
1040 Nonce(NdpNonce<&'a [u8]>),
1041
1042 RecursiveDnsServer(RecursiveDnsServer<'a>),
1043 RouteInformation(RouteInformation),
1044 }
1045
1046 impl<'a> NdpOption<'a> {
1047 pub fn nonce(self) -> Option<NdpNonce<&'a [u8]>> {
1049 match self {
1050 NdpOption::Nonce(nonce) => Some(nonce),
1051 _ => None,
1052 }
1053 }
1054
1055 pub fn source_link_layer_address(self) -> Option<&'a [u8]> {
1057 match self {
1058 NdpOption::SourceLinkLayerAddress(a) => Some(a),
1059 _ => None,
1060 }
1061 }
1062
1063 pub fn target_link_layer_address(self) -> Option<&'a [u8]> {
1065 match self {
1066 NdpOption::TargetLinkLayerAddress(a) => Some(a),
1067 _ => None,
1068 }
1069 }
1070 }
1071
1072 #[derive(Debug)]
1074 pub struct NdpOptionsImpl;
1075
1076 impl<'a> OptionLayout for NdpOptionsImpl {
1077 type KindLenField = u8;
1078
1079 const LENGTH_ENCODING: LengthEncoding = LengthEncoding::TypeLengthValue {
1081 option_len_multiplier: NonZeroUsize::new(8).unwrap(),
1082 };
1083 }
1084
1085 impl OptionParseLayout for NdpOptionsImpl {
1086 type Error = OptionParseErr;
1088
1089 const END_OF_OPTIONS: Option<u8> = None;
1091 const NOP: Option<u8> = None;
1092 }
1093
1094 impl OptionsImpl for NdpOptionsImpl {
1095 type Option<'a> = NdpOption<'a>;
1096
1097 fn parse<'a>(
1098 kind: u8,
1099 mut data: &'a [u8],
1100 ) -> Result<Option<NdpOption<'a>>, OptionParseErr> {
1101 let kind = if let Ok(k) = NdpOptionType::try_from(kind) {
1102 k
1103 } else {
1104 return Ok(None);
1105 };
1106
1107 let opt = match kind {
1108 NdpOptionType::SourceLinkLayerAddress => NdpOption::SourceLinkLayerAddress(data),
1109 NdpOptionType::TargetLinkLayerAddress => NdpOption::TargetLinkLayerAddress(data),
1110 NdpOptionType::PrefixInformation => {
1111 let data = Ref::<_, PrefixInformation>::from_bytes(data)
1112 .map_err(|_| OptionParseErr)?;
1113 NdpOption::PrefixInformation(Ref::into_ref(data))
1114 }
1115 NdpOptionType::RedirectedHeader => NdpOption::RedirectedHeader {
1116 original_packet: &data[REDIRECTED_HEADER_OPTION_RESERVED_BYTES_LENGTH..],
1117 },
1118 NdpOptionType::Mtu => NdpOption::Mtu(NetworkEndian::read_u32(
1119 &data[MTU_OPTION_RESERVED_BYTES_LENGTH..],
1120 )),
1121 NdpOptionType::Nonce => NdpOption::Nonce(
1122 NdpNonce::new(data).map_err(|_: InvalidNonceError| OptionParseErr)?,
1123 ),
1124 NdpOptionType::RecursiveDnsServer => {
1125 NdpOption::RecursiveDnsServer(RecursiveDnsServer::parse(data)?)
1126 }
1127 NdpOptionType::RouteInformation => {
1128 #[derive(KnownLayout, FromBytes, Immutable, Unaligned)]
1131 #[repr(C)]
1132 struct RouteInfoFixed {
1133 prefix_length: u8,
1134 preference_raw: u8,
1135 route_lifetime_seconds: U32,
1136 }
1137
1138 let mut buf = &mut data;
1139
1140 let fixed = buf.take_obj_front::<RouteInfoFixed>().ok_or(OptionParseErr)?;
1141
1142 let preference = super::RoutePreference::try_from(
1144 (fixed.preference_raw & ROUTE_INFORMATION_PREFERENCE_MASK)
1145 >> ROUTE_INFORMATION_PREFERENCE_RESERVED_BITS_RIGHT,
1146 )
1147 .map_err(|()| OptionParseErr)?;
1148
1149 let buf_len = buf.len();
1161 if buf_len % OPTION_BYTES_PER_LENGTH_UNIT != 0 {
1162 return Err(OptionParseErr);
1163 }
1164 let length = buf_len / OPTION_BYTES_PER_LENGTH_UNIT;
1165 match (fixed.prefix_length, length) {
1166 (65..=128, 2) => {}
1167 (1..=64, 1 | 2) => {}
1168 (0, 0 | 1 | 2) => {}
1169 _ => return Err(OptionParseErr),
1170 }
1171
1172 let mut prefix_buf = [0; 16];
1173 prefix_buf[..buf_len].copy_from_slice(&buf);
1175 let prefix = Ipv6Addr::from_bytes(prefix_buf);
1176
1177 NdpOption::RouteInformation(RouteInformation::new(
1178 Subnet::new(prefix, fixed.prefix_length).map_err(|_| OptionParseErr)?,
1179 fixed.route_lifetime_seconds.get(),
1180 preference,
1181 ))
1182 }
1183 };
1184
1185 Ok(Some(opt))
1186 }
1187 }
1188
1189 #[allow(missing_docs)]
1191 #[derive(Debug)]
1192 pub enum NdpOptionBuilder<'a> {
1193 SourceLinkLayerAddress(&'a [u8]),
1194 TargetLinkLayerAddress(&'a [u8]),
1195 PrefixInformation(PrefixInformation),
1196
1197 RedirectedHeader { original_packet: &'a [u8] },
1198
1199 Mtu(u32),
1200 Nonce(NdpNonce<&'a [u8]>),
1201
1202 RouteInformation(RouteInformation),
1203 RecursiveDnsServer(RecursiveDnsServer<'a>),
1204 }
1205
1206 impl<'a> From<&NdpOptionBuilder<'a>> for NdpOptionType {
1207 fn from(v: &NdpOptionBuilder<'a>) -> Self {
1208 match v {
1209 NdpOptionBuilder::SourceLinkLayerAddress(_) => {
1210 NdpOptionType::SourceLinkLayerAddress
1211 }
1212 NdpOptionBuilder::TargetLinkLayerAddress(_) => {
1213 NdpOptionType::TargetLinkLayerAddress
1214 }
1215 NdpOptionBuilder::PrefixInformation(_) => NdpOptionType::PrefixInformation,
1216 NdpOptionBuilder::RedirectedHeader { .. } => NdpOptionType::RedirectedHeader,
1217 NdpOptionBuilder::Mtu { .. } => NdpOptionType::Mtu,
1218 NdpOptionBuilder::Nonce(_) => NdpOptionType::Nonce,
1219 NdpOptionBuilder::RouteInformation(_) => NdpOptionType::RouteInformation,
1220 NdpOptionBuilder::RecursiveDnsServer(_) => NdpOptionType::RecursiveDnsServer,
1221 }
1222 }
1223 }
1224
1225 impl<'a> OptionBuilder for NdpOptionBuilder<'a> {
1226 type Layout = NdpOptionsImpl;
1227
1228 fn serialized_len(&self) -> usize {
1229 match self {
1230 NdpOptionBuilder::SourceLinkLayerAddress(data)
1231 | NdpOptionBuilder::TargetLinkLayerAddress(data) => data.len(),
1232 NdpOptionBuilder::PrefixInformation(_) => PREFIX_INFORMATION_OPTION_LENGTH,
1233 NdpOptionBuilder::RedirectedHeader { original_packet } => {
1234 REDIRECTED_HEADER_OPTION_RESERVED_BYTES_LENGTH + original_packet.len()
1235 }
1236 NdpOptionBuilder::Mtu(_) => MTU_OPTION_LENGTH,
1237 NdpOptionBuilder::Nonce(NdpNonce { nonce }) => nonce.len(),
1238 NdpOptionBuilder::RouteInformation(o) => o.serialized_len(),
1239 NdpOptionBuilder::RecursiveDnsServer(RecursiveDnsServer {
1240 lifetime,
1241 addresses,
1242 }) => {
1243 RECURSIVE_DNS_SERVER_OPTION_RESERVED_BYTES_LENGTH
1244 + core::mem::size_of_val(lifetime)
1245 + core::mem::size_of_val(*addresses)
1246 }
1247 }
1248 }
1249
1250 fn option_kind(&self) -> u8 {
1251 NdpOptionType::from(self).into()
1252 }
1253
1254 fn serialize_into(&self, buffer: &mut [u8]) {
1255 match self {
1256 NdpOptionBuilder::SourceLinkLayerAddress(data)
1257 | NdpOptionBuilder::TargetLinkLayerAddress(data) => buffer.copy_from_slice(data),
1258 NdpOptionBuilder::PrefixInformation(pfx_info) => {
1259 buffer.copy_from_slice(pfx_info.as_bytes());
1260 }
1261 NdpOptionBuilder::RedirectedHeader { original_packet } => {
1262 let (reserved_bytes, original_packet_bytes) =
1266 buffer.split_at_mut(REDIRECTED_HEADER_OPTION_RESERVED_BYTES_LENGTH);
1267 reserved_bytes
1268 .copy_from_slice(&[0; REDIRECTED_HEADER_OPTION_RESERVED_BYTES_LENGTH]);
1269 original_packet_bytes.copy_from_slice(original_packet);
1270 }
1271 NdpOptionBuilder::Mtu(mtu) => {
1272 let (reserved_bytes, mtu_bytes) =
1275 buffer.split_at_mut(MTU_OPTION_RESERVED_BYTES_LENGTH);
1276 reserved_bytes.copy_from_slice(&[0; MTU_OPTION_RESERVED_BYTES_LENGTH]);
1277 mtu_bytes.copy_from_slice(U32::new(*mtu).as_bytes());
1278 }
1279 NdpOptionBuilder::Nonce(NdpNonce { nonce }) => {
1280 buffer.copy_from_slice(nonce);
1281 }
1282 NdpOptionBuilder::RouteInformation(p) => p.serialize(buffer),
1283 NdpOptionBuilder::RecursiveDnsServer(RecursiveDnsServer {
1284 lifetime,
1285 addresses,
1286 }) => {
1287 let (reserved_bytes, buffer) =
1290 buffer.split_at_mut(RECURSIVE_DNS_SERVER_OPTION_RESERVED_BYTES_LENGTH);
1291 reserved_bytes
1292 .copy_from_slice(&[0; RECURSIVE_DNS_SERVER_OPTION_RESERVED_BYTES_LENGTH]);
1293
1294 let (lifetime_bytes, addresses_bytes) =
1298 buffer.split_at_mut(core::mem::size_of_val(lifetime));
1299 lifetime_bytes.copy_from_slice(U32::new(*lifetime).as_bytes());
1300 addresses_bytes.copy_from_slice(addresses.as_bytes());
1301 }
1302 }
1303 }
1304 }
1305}
1306
1307#[cfg(test)]
1308mod tests {
1309 use byteorder::{ByteOrder, NetworkEndian};
1310 use net_types::ip::{Ip, IpAddress, Subnet};
1311 use packet::{EmptyBuf, InnerPacketBuilder, PacketBuilder, ParseBuffer, Serializer};
1312 use test_case::test_case;
1313 use zerocopy::Ref;
1314
1315 use super::*;
1316 use crate::icmp::{IcmpPacketBuilder, IcmpParseArgs};
1317 use crate::ipv6::{Ipv6Header, Ipv6Packet};
1318
1319 #[test]
1320 fn parse_serialize_redirected_header() {
1321 let expected_packet = [1, 2, 3, 4, 5, 6, 7, 8];
1322 let options =
1323 &[options::NdpOptionBuilder::RedirectedHeader { original_packet: &expected_packet }];
1324 let serialized = OptionSequenceBuilder::new(options.iter())
1325 .into_serializer()
1326 .serialize_vec_outer()
1327 .unwrap();
1328 let mut expected = [0; 16];
1330 (&mut expected[..2]).copy_from_slice(&[4, 2]);
1335 (&mut expected[8..]).copy_from_slice(&expected_packet);
1336 assert_eq!(serialized.as_ref(), expected);
1337
1338 let parsed = Options::parse(&expected[..]).unwrap();
1339 let parsed = parsed.iter().collect::<Vec<options::NdpOption<'_>>>();
1340 assert_eq!(parsed.len(), 1);
1341 assert_eq!(
1342 options::NdpOption::RedirectedHeader { original_packet: &expected_packet },
1343 parsed[0]
1344 );
1345 }
1346
1347 #[test]
1348 fn parse_serialize_mtu_option() {
1349 let expected_mtu = 5781;
1350 let options = &[options::NdpOptionBuilder::Mtu(expected_mtu)];
1351 let serialized = OptionSequenceBuilder::new(options.iter())
1352 .into_serializer()
1353 .serialize_vec_outer()
1354 .unwrap();
1355 let mut expected = [5, 1, 0, 0, 0, 0, 0, 0];
1360 NetworkEndian::write_u32(&mut expected[4..], expected_mtu);
1361 assert_eq!(serialized.as_ref(), expected);
1362
1363 let parsed = Options::parse(&expected[..]).unwrap();
1364 let parsed = parsed.iter().collect::<Vec<options::NdpOption<'_>>>();
1365 assert_eq!(parsed.len(), 1);
1366 assert_eq!(options::NdpOption::Mtu(expected_mtu), parsed[0]);
1367 }
1368
1369 #[test_case(
1370 options::MIN_NONCE_LENGTH - 1 =>
1371 matches Err(options::InvalidNonceError::ResultsInNonMultipleOf8);
1372 "resulting nonce option length must be multiple of 8")]
1373 #[test_case(
1374 options::MIN_NONCE_LENGTH => matches Ok(_);
1375 "MIN_NONCE_LENGTH must validate successfully")]
1376 #[test_case(
1377 usize::from(u8::MAX) * 8 - 2 => matches Ok(_);
1378 "maximum possible nonce length must validate successfully")]
1379 #[test_case(
1380 usize::from(u8::MAX) * 8 - 2 + 8 =>
1381 matches Err(options::InvalidNonceError::TooLong);
1382 "nonce option's length must fit in u8")]
1383 fn nonce_length_validation(
1384 length: usize,
1385 ) -> Result<options::NdpNonce<&'static [u8]>, options::InvalidNonceError> {
1386 const LEN: usize = (u8::MAX as usize + 1) * 8;
1387 const BYTES: [u8; LEN] = [0u8; LEN];
1388 options::NdpNonce::new(&BYTES[..length])
1389 }
1390
1391 #[test]
1392 fn parse_serialize_nonce_option() {
1393 let expected_nonce: [u8; 6] = [1, 2, 3, 4, 5, 6];
1394 let nonce = options::NdpNonce::new(&expected_nonce[..]).expect("should be valid nonce");
1395 let options = &[options::NdpOptionBuilder::Nonce(nonce)];
1396 let serialized = OptionSequenceBuilder::new(options.iter())
1397 .into_serializer()
1398 .serialize_vec_outer()
1399 .unwrap();
1400
1401 let mut expected_bytes: [u8; 8] = [14, 1, 0, 0, 0, 0, 0, 0];
1404 expected_bytes[2..].copy_from_slice(&expected_nonce);
1405
1406 assert_eq!(serialized.as_ref(), expected_bytes);
1407
1408 let parsed = Options::parse(&expected_bytes[..]).unwrap();
1409 let parsed = parsed.iter().collect::<Vec<options::NdpOption<'_>>>();
1410 assert_eq!(parsed.len(), 1);
1411 assert_eq!(parsed[0], options::NdpOption::Nonce(nonce));
1412 }
1413
1414 #[test]
1415 fn parse_serialize_prefix_option() {
1416 let expected_prefix_info = options::PrefixInformation::new(
1417 120,
1418 true,
1419 false,
1420 100,
1421 100,
1422 Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 168, 0, 0]),
1423 );
1424 let options = &[options::NdpOptionBuilder::PrefixInformation(expected_prefix_info.clone())];
1425 let serialized = OptionSequenceBuilder::new(options.iter())
1426 .into_serializer()
1427 .serialize_vec_outer()
1428 .unwrap();
1429 let mut expected = [0; 32];
1434 expected[0] = 3;
1435 expected[1] = 4;
1436 (&mut expected[2..]).copy_from_slice(expected_prefix_info.as_bytes());
1437 assert_eq!(serialized.as_ref(), expected);
1438
1439 let parsed = Options::parse(&expected[..]).unwrap();
1440 let parsed = parsed.iter().collect::<Vec<options::NdpOption<'_>>>();
1441 assert_eq!(parsed.len(), 1);
1442 assert_eq!(options::NdpOption::PrefixInformation(&expected_prefix_info), parsed[0]);
1443 }
1444
1445 #[test]
1446 fn parse_serialize_rdnss_option() {
1447 let test = |addrs: &[Ipv6Addr]| {
1448 let lifetime = 120;
1449 let expected_rdnss = options::RecursiveDnsServer::new(lifetime, addrs);
1450 let options = &[options::NdpOptionBuilder::RecursiveDnsServer(expected_rdnss.clone())];
1451 let serialized = OptionSequenceBuilder::new(options.iter())
1452 .into_serializer()
1453 .serialize_vec_outer()
1454 .unwrap();
1455 let mut expected = vec![0; 8 + addrs.len() * usize::from(Ipv6Addr::BYTES)];
1458 (&mut expected[..4]).copy_from_slice(&[
1463 25,
1464 1 + u8::try_from(addrs.len()).unwrap() * 2,
1465 0,
1466 0,
1467 ]);
1468 NetworkEndian::write_u32(&mut expected[4..8], lifetime);
1470 (&mut expected[8..]).copy_from_slice(addrs.as_bytes());
1472 assert_eq!(serialized.as_ref(), expected.as_slice());
1473
1474 let parsed = Options::parse(&expected[..])
1475 .expect("should have parsed a valid recursive dns erver option");
1476 let parsed = parsed.iter().collect::<Vec<options::NdpOption<'_>>>();
1477 assert_eq!(parsed.len(), 1);
1478
1479 assert_eq!(
1481 options::RecursiveDnsServer::parse(&expected[2..]).expect("parsing should succeed"),
1482 expected_rdnss
1483 );
1484
1485 assert_eq!(options::NdpOption::RecursiveDnsServer(expected_rdnss), parsed[0]);
1486 };
1487 test(&[Ipv6Addr::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])]);
1488 test(&[
1489 Ipv6Addr::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
1490 Ipv6Addr::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]),
1491 ]);
1492 }
1493
1494 #[test]
1495 fn parse_serialize_rdnss_option_error() {
1496 let addrs = [
1497 Ipv6Addr::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
1498 Ipv6Addr::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]),
1499 ];
1500 let lifetime = 120;
1501 let mut buf = vec![0; 8 + addrs.len() * usize::from(Ipv6Addr::BYTES)];
1504 (&mut buf[..4]).copy_from_slice(&[25, 1 + u8::try_from(addrs.len()).unwrap() * 2, 0, 0]);
1509 NetworkEndian::write_u32(&mut buf[4..8], lifetime);
1511 (&mut buf[8..]).copy_from_slice(addrs.as_bytes());
1513
1514 let _parsed = Options::parse(&buf[..])
1516 .expect("should have parsed a valid recursive dns erver option");
1517
1518 let _err = Options::parse(&buf[..8]).expect_err(
1520 "should not have parsed a recursive dns server option that has no addresses",
1521 );
1522
1523 let _err = Options::parse(&buf[..buf.len()-1])
1525 .expect_err("should not have parsed a recursive dns server option that cuts off in the middle of an address");
1526
1527 (&mut buf[8..8 + usize::from(Ipv6Addr::BYTES)])
1529 .copy_from_slice(Ipv6::UNSPECIFIED_ADDRESS.as_bytes());
1530 let _parsed = Options::parse(&buf[..]).expect_err(
1531 "should not have parsed a recursive dns erver option with an unspecified address",
1532 );
1533
1534 (&mut buf[8..8 + usize::from(Ipv6Addr::BYTES)])
1536 .copy_from_slice(Ipv6::ALL_NODES_LINK_LOCAL_MULTICAST_ADDRESS.as_bytes());
1537 let _parsed = Options::parse(&buf[..]).expect_err(
1538 "should not have parsed a recursive dns erver option with a multicast address",
1539 );
1540 }
1541
1542 #[test]
1543 fn parse_neighbor_solicitation() {
1544 use crate::icmp::testdata::ndp_neighbor::*;
1545 let mut buf = SOLICITATION_IP_PACKET_BYTES;
1546 let ip = buf.parse::<Ipv6Packet<_>>().unwrap();
1547 let ipv6_builder = ip.builder();
1548 let (src_ip, dst_ip) = (ip.src_ip(), ip.dst_ip());
1549 let icmp = buf
1550 .parse_with::<_, IcmpPacket<_, _, NeighborSolicitation>>(IcmpParseArgs::new(
1551 src_ip, dst_ip,
1552 ))
1553 .unwrap();
1554
1555 assert_eq!(icmp.message().target_address.ipv6_bytes(), TARGET_ADDRESS);
1556 let collected = icmp.ndp_options().iter().collect::<Vec<options::NdpOption<'_>>>();
1557 for option in collected.iter() {
1558 match option {
1559 options::NdpOption::SourceLinkLayerAddress(address) => {
1560 assert_eq!(address, &SOURCE_LINK_LAYER_ADDRESS);
1561 }
1562 o => panic!("Found unexpected option: {:?}", o),
1563 }
1564 }
1565 let option_builders =
1566 [options::NdpOptionBuilder::SourceLinkLayerAddress(&SOURCE_LINK_LAYER_ADDRESS)];
1567 let serialized = OptionSequenceBuilder::new(option_builders.iter())
1568 .into_serializer()
1569 .wrap_in(IcmpPacketBuilder::<Ipv6, _>::new(
1570 src_ip,
1571 dst_ip,
1572 IcmpZeroCode,
1573 *icmp.message(),
1574 ))
1575 .wrap_in(ipv6_builder)
1576 .serialize_vec_outer()
1577 .unwrap()
1578 .as_ref()
1579 .to_vec();
1580 assert_eq!(&serialized, &SOLICITATION_IP_PACKET_BYTES)
1581 }
1582
1583 #[test]
1584 fn parse_neighbor_advertisement() {
1585 use crate::icmp::testdata::ndp_neighbor::*;
1586 let mut buf = ADVERTISEMENT_IP_PACKET_BYTES;
1587 let ip = buf.parse::<Ipv6Packet<_>>().unwrap();
1588 let ipv6_builder = ip.builder();
1589 let (src_ip, dst_ip) = (ip.src_ip(), ip.dst_ip());
1590 let icmp = buf
1591 .parse_with::<_, IcmpPacket<_, _, NeighborAdvertisement>>(IcmpParseArgs::new(
1592 src_ip, dst_ip,
1593 ))
1594 .unwrap();
1595 assert_eq!(icmp.message().target_address.ipv6_bytes(), TARGET_ADDRESS);
1596 assert_eq!(icmp.ndp_options().iter().count(), 0);
1597
1598 let serialized = EmptyBuf
1599 .wrap_in(IcmpPacketBuilder::<Ipv6, _>::new(
1600 src_ip,
1601 dst_ip,
1602 IcmpZeroCode,
1603 *icmp.message(),
1604 ))
1605 .wrap_in(ipv6_builder)
1606 .serialize_vec_outer()
1607 .unwrap()
1608 .as_ref()
1609 .to_vec();
1610 assert_eq!(&serialized, &ADVERTISEMENT_IP_PACKET_BYTES);
1611 }
1612
1613 #[test]
1614 fn parse_router_advertisement() {
1615 use crate::icmp::ndp::options::RouteInformation;
1616 use crate::icmp::testdata::ndp_router::*;
1617
1618 let mut buf = ADVERTISEMENT_IP_PACKET_BYTES;
1619 let ip = buf.parse::<Ipv6Packet<_>>().unwrap();
1620 let ipv6_builder = ip.builder();
1621 let (src_ip, dst_ip) = (ip.src_ip(), ip.dst_ip());
1622 let icmp = buf
1623 .parse_with::<_, IcmpPacket<_, _, RouterAdvertisement>>(IcmpParseArgs::new(
1624 src_ip, dst_ip,
1625 ))
1626 .unwrap();
1627 assert_eq!(icmp.message().current_hop_limit(), HOP_LIMIT);
1628 assert_eq!(icmp.message().router_lifetime(), LIFETIME);
1629 assert_eq!(icmp.message().reachable_time(), REACHABLE_TIME);
1630 assert_eq!(icmp.message().retransmit_timer(), RETRANS_TIMER);
1631
1632 assert_eq!(icmp.ndp_options().iter().count(), 5);
1633
1634 let collected = icmp.ndp_options().iter().collect::<Vec<options::NdpOption<'_>>>();
1635 for option in collected.iter() {
1636 match option {
1637 options::NdpOption::SourceLinkLayerAddress(address) => {
1638 assert_eq!(address, &SOURCE_LINK_LAYER_ADDRESS);
1639 }
1640 options::NdpOption::PrefixInformation(info) => {
1641 assert_eq!(info.on_link_flag(), PREFIX_INFO_ON_LINK_FLAG);
1642 assert_eq!(
1643 info.autonomous_address_configuration_flag(),
1644 PREFIX_INFO_AUTONOMOUS_ADDRESS_CONFIGURATION_FLAG
1645 );
1646 assert_eq!(
1647 info.valid_lifetime(),
1648 NonZeroNdpLifetime::from_u32_with_infinite(
1649 PREFIX_INFO_VALID_LIFETIME_SECONDS
1650 )
1651 );
1652 assert_eq!(
1653 info.preferred_lifetime(),
1654 NonZeroNdpLifetime::from_u32_with_infinite(
1655 PREFIX_INFO_PREFERRED_LIFETIME_SECONDS
1656 )
1657 );
1658 assert_eq!(info.prefix_length(), PREFIX_INFO_PREFIX.prefix());
1659 assert_eq!(info.prefix(), &PREFIX_INFO_PREFIX.network());
1660 }
1661 options::NdpOption::RouteInformation(_) => {
1662 }
1664 o => panic!("Found unexpected option: {:?}", o),
1665 }
1666 }
1667
1668 let mut route_information_options = collected
1669 .iter()
1670 .filter_map(|o| match o {
1671 options::NdpOption::RouteInformation(info) => Some(info),
1672 _ => None,
1673 })
1674 .collect::<Vec<&RouteInformation>>();
1675 route_information_options.sort_by_key(|o| o.prefix().prefix());
1680 assert_eq!(
1681 route_information_options,
1682 [
1683 &options::RouteInformation::new(
1684 ROUTE_INFO_LOW_PREF_PREFIX,
1685 ROUTE_INFO_LOW_PREF_VALID_LIFETIME_SECONDS,
1686 ROUTE_INFO_LOW_PREF,
1687 ),
1688 &options::RouteInformation::new(
1689 ROUTE_INFO_MEDIUM_PREF_PREFIX,
1690 ROUTE_INFO_MEDIUM_PREF_VALID_LIFETIME_SECONDS,
1691 ROUTE_INFO_MEDIUM_PREF,
1692 ),
1693 &options::RouteInformation::new(
1694 ROUTE_INFO_HIGH_PREF_PREFIX,
1695 ROUTE_INFO_HIGH_PREF_VALID_LIFETIME_SECONDS,
1696 ROUTE_INFO_HIGH_PREF,
1697 )
1698 ]
1699 );
1700
1701 let option_builders = [
1702 options::NdpOptionBuilder::SourceLinkLayerAddress(&SOURCE_LINK_LAYER_ADDRESS),
1703 options::NdpOptionBuilder::PrefixInformation(options::PrefixInformation::new(
1704 PREFIX_INFO_PREFIX.prefix(),
1705 PREFIX_INFO_ON_LINK_FLAG,
1706 PREFIX_INFO_AUTONOMOUS_ADDRESS_CONFIGURATION_FLAG,
1707 PREFIX_INFO_VALID_LIFETIME_SECONDS,
1708 PREFIX_INFO_PREFERRED_LIFETIME_SECONDS,
1709 PREFIX_INFO_PREFIX.network(),
1710 )),
1711 options::NdpOptionBuilder::RouteInformation(options::RouteInformation::new(
1712 ROUTE_INFO_HIGH_PREF_PREFIX,
1713 ROUTE_INFO_HIGH_PREF_VALID_LIFETIME_SECONDS,
1714 ROUTE_INFO_HIGH_PREF,
1715 )),
1716 options::NdpOptionBuilder::RouteInformation(options::RouteInformation::new(
1717 ROUTE_INFO_MEDIUM_PREF_PREFIX,
1718 ROUTE_INFO_MEDIUM_PREF_VALID_LIFETIME_SECONDS,
1719 ROUTE_INFO_MEDIUM_PREF,
1720 )),
1721 options::NdpOptionBuilder::RouteInformation(options::RouteInformation::new(
1722 ROUTE_INFO_LOW_PREF_PREFIX,
1723 ROUTE_INFO_LOW_PREF_VALID_LIFETIME_SECONDS,
1724 ROUTE_INFO_LOW_PREF,
1725 )),
1726 ];
1727 let serialized = OptionSequenceBuilder::new(option_builders.iter())
1728 .into_serializer()
1729 .wrap_in(IcmpPacketBuilder::<Ipv6, _>::new(
1730 src_ip,
1731 dst_ip,
1732 IcmpZeroCode,
1733 *icmp.message(),
1734 ))
1735 .wrap_in(ipv6_builder)
1736 .serialize_vec_outer()
1737 .unwrap()
1738 .as_ref()
1739 .to_vec();
1740 assert_eq!(&serialized, &ADVERTISEMENT_IP_PACKET_BYTES);
1741 }
1742
1743 struct SerializeRATest {
1744 hop_limit: u8,
1745 managed_flag: bool,
1746 other_config_flag: bool,
1747 preference: RoutePreference,
1748 router_lifetime_seconds: u16,
1749 reachable_time_seconds: u32,
1750 retransmit_timer_seconds: u32,
1751 }
1752
1753 #[test_case(
1754 SerializeRATest{
1755 hop_limit: 1,
1756 managed_flag: true,
1757 other_config_flag: false,
1758 preference: RoutePreference::High,
1759 router_lifetime_seconds: 1_000,
1760 reachable_time_seconds: 1_000_000,
1761 retransmit_timer_seconds: 5,
1762 }; "test_1")]
1763 #[test_case(
1764 SerializeRATest{
1765 hop_limit: 64,
1766 managed_flag: false,
1767 other_config_flag: true,
1768 preference: RoutePreference::Low,
1769 router_lifetime_seconds: 5,
1770 reachable_time_seconds: 23425621,
1771 retransmit_timer_seconds: 13252521,
1772 }; "test_2")]
1773 fn serialize_router_advertisement(test: SerializeRATest) {
1774 let SerializeRATest {
1775 hop_limit,
1776 managed_flag,
1777 other_config_flag,
1778 preference,
1779 router_lifetime_seconds,
1780 reachable_time_seconds,
1781 retransmit_timer_seconds,
1782 } = test;
1783
1784 const SRC_IP: Ipv6Addr =
1785 Ipv6Addr::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1786 const DST_IP: Ipv6Addr =
1787 Ipv6Addr::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17]);
1788 let icmp = IcmpPacketBuilder::<Ipv6, _>::new(
1789 SRC_IP,
1790 DST_IP,
1791 IcmpZeroCode,
1792 RouterAdvertisement::with_prf(
1793 hop_limit,
1794 managed_flag,
1795 other_config_flag,
1796 preference,
1797 router_lifetime_seconds,
1798 reachable_time_seconds,
1799 retransmit_timer_seconds,
1800 ),
1801 );
1802 let serialized = icmp.wrap_body(EmptyBuf).serialize_vec_outer().unwrap();
1803
1804 const RA_LEN: u32 = 16;
1826 let mut expected = [0; RA_LEN as usize];
1827 expected[0] = 134;
1828 expected[4] = hop_limit;
1829 if managed_flag {
1830 expected[5] |= 1 << 7;
1831 }
1832 if other_config_flag {
1833 expected[5] |= 1 << 6;
1834 }
1835 expected[5] |= u8::from(preference) << 3;
1836 let (mut router_lifetime, _rest) = Ref::<_, U16>::from_prefix(&mut expected[6..]).unwrap();
1837 router_lifetime.set(router_lifetime_seconds);
1838 let (mut reachable_time, _rest) = Ref::<_, U32>::from_prefix(&mut expected[8..]).unwrap();
1839 reachable_time.set(reachable_time_seconds);
1840 let (mut retransmit_timer, _rest) =
1841 Ref::<_, U32>::from_prefix(&mut expected[12..]).unwrap();
1842 retransmit_timer.set(retransmit_timer_seconds);
1843
1844 let mut c = internet_checksum::Checksum::new();
1845 c.add_bytes(SRC_IP.bytes());
1847 c.add_bytes(DST_IP.bytes());
1848 c.add_bytes(U32::new(RA_LEN).as_bytes());
1849 c.add_bytes(&[0, crate::ip::Ipv6Proto::Icmpv6.into()]);
1850 c.add_bytes(&expected[..]);
1852 expected[2..4].copy_from_slice(&c.checksum()[..]);
1853
1854 assert_eq!(serialized.as_ref(), &expected[..]);
1855 }
1856
1857 struct SerializeRioTest {
1858 prefix_length: u8,
1859 route_lifetime_seconds: u32,
1860 preference: RoutePreference,
1861 expected_option_length: u8,
1862 }
1863
1864 #[test_case(
1874 SerializeRioTest{
1875 prefix_length: 0,
1876 route_lifetime_seconds: 1,
1877 preference: RoutePreference::High,
1878 expected_option_length: 8,
1879 }; "prefix_length_0")]
1880 #[test_case(
1881 SerializeRioTest{
1882 prefix_length: 1,
1883 route_lifetime_seconds: 1000,
1884 preference: RoutePreference::Medium,
1885 expected_option_length: 16,
1886 }; "prefix_length_1")]
1887 #[test_case(
1888 SerializeRioTest{
1889 prefix_length: 64,
1890 route_lifetime_seconds: 100000,
1891 preference: RoutePreference::Low,
1892 expected_option_length: 16,
1893 }; "prefix_length_64")]
1894 #[test_case(
1895 SerializeRioTest{
1896 prefix_length: 65,
1897 route_lifetime_seconds: 1000000,
1898 preference: RoutePreference::Medium,
1899 expected_option_length: 24,
1900 }; "prefix_length_65")]
1901 #[test_case(
1902 SerializeRioTest{
1903 prefix_length: 128,
1904 route_lifetime_seconds: 10000000,
1905 preference: RoutePreference::Medium,
1906 expected_option_length: 24,
1907 }; "prefix_length_128")]
1908 fn serialize_route_information_option(test: SerializeRioTest) {
1909 const IPV6ADDR: Ipv6Addr =
1910 Ipv6Addr::new([0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff]);
1911
1912 let SerializeRioTest {
1913 prefix_length,
1914 route_lifetime_seconds,
1915 preference,
1916 expected_option_length,
1917 } = test;
1918 let prefix = IPV6ADDR.mask(prefix_length);
1919
1920 let option_builders =
1921 [options::NdpOptionBuilder::RouteInformation(options::RouteInformation::new(
1922 Subnet::new(prefix, prefix_length).unwrap(),
1923 route_lifetime_seconds,
1924 preference,
1925 ))];
1926
1927 let serialized = OptionSequenceBuilder::new(option_builders.iter())
1928 .into_serializer()
1929 .serialize_vec_outer()
1930 .unwrap();
1931
1932 let mut expected = [0; 24];
1960 expected[0] = 24;
1961 expected[1] = expected_option_length / 8;
1962 expected[2] = prefix_length;
1963 expected[3] = u8::from(preference) << 3;
1964 let (mut lifetime_seconds, _rest) = Ref::<_, U32>::from_prefix(&mut expected[4..]).unwrap();
1965 lifetime_seconds.set(route_lifetime_seconds);
1966 expected[8..].copy_from_slice(prefix.bytes());
1967
1968 assert_eq!(serialized.as_ref(), &expected[..expected_option_length.into()]);
1969 }
1970
1971 #[test_case(0, None)]
1972 #[test_case(
1973 1,
1974 Some(NonZeroNdpLifetime::Finite(NonZeroDuration::new(
1975 Duration::from_secs(1),
1976 ).unwrap()))
1977 )]
1978 #[test_case(
1979 u32::MAX - 1,
1980 Some(NonZeroNdpLifetime::Finite(NonZeroDuration::new(
1981 Duration::from_secs(u64::from(u32::MAX) - 1),
1982 ).unwrap()))
1983 )]
1984 #[test_case(u32::MAX, Some(NonZeroNdpLifetime::Infinite))]
1985 fn non_zero_ndp_lifetime_non_zero_or_max_u32_from_u32_with_infinite(
1986 t: u32,
1987 expected: Option<NonZeroNdpLifetime>,
1988 ) {
1989 assert_eq!(NonZeroNdpLifetime::from_u32_with_infinite(t), expected)
1990 }
1991
1992 const MIN_NON_ZERO_DURATION: Duration = Duration::new(0, 1);
1993 #[test_case(
1994 NonZeroNdpLifetime::Infinite,
1995 NonZeroDuration::new(MIN_NON_ZERO_DURATION).unwrap(),
1996 NonZeroDuration::new(MIN_NON_ZERO_DURATION).unwrap()
1997 )]
1998 #[test_case(
1999 NonZeroNdpLifetime::Infinite,
2000 NonZeroDuration::new(Duration::MAX).unwrap(),
2001 NonZeroDuration::new(Duration::MAX).unwrap()
2002 )]
2003 #[test_case(
2004 NonZeroNdpLifetime::Finite(NonZeroDuration::new(
2005 Duration::from_secs(2)).unwrap()
2006 ),
2007 NonZeroDuration::new(Duration::from_secs(1)).unwrap(),
2008 NonZeroDuration::new(Duration::from_secs(1)).unwrap()
2009 )]
2010 #[test_case(
2011 NonZeroNdpLifetime::Finite(NonZeroDuration::new(
2012 Duration::from_secs(3)).unwrap()
2013 ),
2014 NonZeroDuration::new(Duration::from_secs(4)).unwrap(),
2015 NonZeroDuration::new(Duration::from_secs(3)).unwrap()
2016 )]
2017 fn non_zero_ndp_lifetime_min_finite_duration(
2018 lifetime: NonZeroNdpLifetime,
2019 duration: NonZeroDuration,
2020 expected: NonZeroDuration,
2021 ) {
2022 assert_eq!(lifetime.min_finite_duration(duration), expected)
2023 }
2024}