-
Notifications
You must be signed in to change notification settings - Fork 435
/
ipv6.rs
1144 lines (1024 loc) · 38.2 KB
/
ipv6.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![deny(missing_docs)]
use byteorder::{ByteOrder, NetworkEndian};
use core::fmt;
use super::{Error, Result};
use crate::wire::ip::pretty_print_ip_payload;
pub use super::IpProtocol as Protocol;
/// Minimum MTU required of all links supporting IPv6. See [RFC 8200 § 5].
///
/// [RFC 8200 § 5]: https://tools.ietf.org/html/rfc8200#section-5
pub const MIN_MTU: usize = 1280;
/// Size of IPv6 adderess in octets.
///
/// [RFC 8200 § 2]: https://www.rfc-editor.org/rfc/rfc4291#section-2
pub const ADDR_SIZE: usize = 16;
/// The link-local [all nodes multicast address].
///
/// [all nodes multicast address]: https://tools.ietf.org/html/rfc4291#section-2.7.1
pub const LINK_LOCAL_ALL_NODES: Address = Address::new(0xff02, 0, 0, 0, 0, 0, 0, 1);
/// The link-local [all routers multicast address].
///
/// [all routers multicast address]: https://tools.ietf.org/html/rfc4291#section-2.7.1
pub const LINK_LOCAL_ALL_ROUTERS: Address = Address::new(0xff02, 0, 0, 0, 0, 0, 0, 2);
/// The link-local [all MLVDv2-capable routers multicast address].
///
/// [all MLVDv2-capable routers multicast address]: https://tools.ietf.org/html/rfc3810#section-11
pub const LINK_LOCAL_ALL_MLDV2_ROUTERS: Address = Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x16);
/// The link-local [all RPL nodes multicast address].
///
/// [all RPL nodes multicast address]: https://www.rfc-editor.org/rfc/rfc6550.html#section-20.19
pub const LINK_LOCAL_ALL_RPL_NODES: Address = Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x1a);
/// The [scope] of an address.
///
/// [scope]: https://www.rfc-editor.org/rfc/rfc4291#section-2.7
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MulticastScope {
/// Interface Local scope
InterfaceLocal = 0x1,
/// Link local scope
LinkLocal = 0x2,
/// Administratively configured
AdminLocal = 0x4,
/// Single site scope
SiteLocal = 0x5,
/// Organization scope
OrganizationLocal = 0x8,
/// Global scope
Global = 0xE,
/// Unknown scope
Unknown = 0xFF,
}
impl From<u8> for MulticastScope {
fn from(value: u8) -> Self {
match value {
0x1 => Self::InterfaceLocal,
0x2 => Self::LinkLocal,
0x4 => Self::AdminLocal,
0x5 => Self::SiteLocal,
0x8 => Self::OrganizationLocal,
0xE => Self::Global,
_ => Self::Unknown,
}
}
}
pub use core::net::Ipv6Addr as Address;
pub(crate) trait AddressExt {
/// Construct an IPv6 address from a sequence of octets, in big-endian.
///
/// # Panics
/// The function panics if `data` is not sixteen octets long.
fn from_bytes(data: &[u8]) -> Address;
/// Query whether the IPv6 address is an [unicast address].
///
/// [unicast address]: https://tools.ietf.org/html/rfc4291#section-2.5
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_is_unicast(&self) -> bool;
/// Query whether the IPv6 address is a [global unicast address].
///
/// [global unicast address]: https://datatracker.ietf.org/doc/html/rfc3587
fn is_global_unicast(&self) -> bool;
/// Query whether the IPv6 address is in the [link-local] scope.
///
/// [link-local]: https://tools.ietf.org/html/rfc4291#section-2.5.6
fn is_link_local(&self) -> bool;
/// Query whether the IPv6 address is a [Unique Local Address] (ULA).
///
/// [Unique Local Address]: https://tools.ietf.org/html/rfc4193
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_is_unique_local(&self) -> bool;
/// Helper function used to mask an address given a prefix.
///
/// # Panics
/// This function panics if `mask` is greater than 128.
fn mask(&self, mask: u8) -> [u8; ADDR_SIZE];
/// The solicited node for the given unicast address.
///
/// # Panics
/// This function panics if the given address is not
/// unicast.
fn solicited_node(&self) -> Address;
/// Return the scope of the address.
///
/// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`.
fn x_multicast_scope(&self) -> MulticastScope;
/// Query whether the IPv6 address is a [solicited-node multicast address].
///
/// [Solicited-node multicast address]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.7.1
fn is_solicited_node_multicast(&self) -> bool;
/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
fn prefix_len(&self) -> Option<u8>;
}
impl AddressExt for Address {
fn from_bytes(data: &[u8]) -> Address {
let mut bytes = [0; ADDR_SIZE];
bytes.copy_from_slice(data);
Address::from(bytes)
}
fn x_is_unicast(&self) -> bool {
!(self.is_multicast() || self.is_unspecified())
}
fn is_global_unicast(&self) -> bool {
(self.octets()[0] >> 5) == 0b001
}
fn is_link_local(&self) -> bool {
self.octets()[0..8] == [0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}
fn x_is_unique_local(&self) -> bool {
(self.octets()[0] & 0b1111_1110) == 0xfc
}
fn mask(&self, mask: u8) -> [u8; ADDR_SIZE] {
assert!(mask <= 128);
let mut bytes = [0u8; ADDR_SIZE];
let idx = (mask as usize) / 8;
let modulus = (mask as usize) % 8;
let octets = self.octets();
let (first, second) = octets.split_at(idx);
bytes[0..idx].copy_from_slice(first);
if idx < ADDR_SIZE {
let part = second[0];
bytes[idx] = part & (!(0xff >> modulus) as u8);
}
bytes
}
fn solicited_node(&self) -> Address {
assert!(self.x_is_unicast());
let o = self.octets();
Address::from([
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, o[13],
o[14], o[15],
])
}
fn x_multicast_scope(&self) -> MulticastScope {
if self.is_multicast() {
return MulticastScope::from(self.octets()[1] & 0b1111);
}
if self.is_link_local() {
MulticastScope::LinkLocal
} else if self.x_is_unique_local() || self.is_global_unicast() {
// ULA are considered global scope
// https://www.rfc-editor.org/rfc/rfc6724#section-3.1
MulticastScope::Global
} else {
MulticastScope::Unknown
}
}
fn is_solicited_node_multicast(&self) -> bool {
self.octets()[0..13]
== [
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF,
]
}
fn prefix_len(&self) -> Option<u8> {
let mut ones = true;
let mut prefix_len = 0;
for byte in self.octets() {
let mut mask = 0x80;
for _ in 0..8 {
let one = byte & mask != 0;
if ones {
// Expect 1s until first 0
if one {
prefix_len += 1;
} else {
ones = false;
}
} else if one {
// 1 where 0 was expected
return None;
}
mask >>= 1;
}
}
Some(prefix_len)
}
}
/// A specification of an IPv6 CIDR block, containing an address and a variable-length
/// subnet masking prefix length.
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Cidr {
address: Address,
prefix_len: u8,
}
impl Cidr {
/// The [solicited node prefix].
///
/// [solicited node prefix]: https://tools.ietf.org/html/rfc4291#section-2.7.1
pub const SOLICITED_NODE_PREFIX: Cidr = Cidr {
address: Address::new(0xff02, 0, 0, 0, 0, 1, 0xff00, 0),
prefix_len: 104,
};
/// Create an IPv6 CIDR block from the given address and prefix length.
///
/// # Panics
/// This function panics if the prefix length is larger than 128.
pub const fn new(address: Address, prefix_len: u8) -> Cidr {
assert!(prefix_len <= 128);
Cidr {
address,
prefix_len,
}
}
/// Return the address of this IPv6 CIDR block.
pub const fn address(&self) -> Address {
self.address
}
/// Return the prefix length of this IPv6 CIDR block.
pub const fn prefix_len(&self) -> u8 {
self.prefix_len
}
/// Query whether the subnetwork described by this IPv6 CIDR block contains
/// the given address.
pub fn contains_addr(&self, addr: &Address) -> bool {
// right shift by 128 is not legal
if self.prefix_len == 0 {
return true;
}
self.address.mask(self.prefix_len) == addr.mask(self.prefix_len)
}
/// Query whether the subnetwork described by this IPV6 CIDR block contains
/// the subnetwork described by the given IPv6 CIDR block.
pub fn contains_subnet(&self, subnet: &Cidr) -> bool {
self.prefix_len <= subnet.prefix_len && self.contains_addr(&subnet.address)
}
}
impl fmt::Display for Cidr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// https://tools.ietf.org/html/rfc4291#section-2.3
write!(f, "{}/{}", self.address, self.prefix_len)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for Cidr {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}/{=u8}", self.address, self.prefix_len);
}
}
/// A read/write wrapper around an Internet Protocol version 6 packet buffer.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Packet<T: AsRef<[u8]>> {
buffer: T,
}
// Ranges and constants describing the IPv6 header
//
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |Version| Traffic Class | Flow Label |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Payload Length | Next Header | Hop Limit |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + +
// | |
// + Source Address +
// | |
// + +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + +
// | |
// + Destination Address +
// | |
// + +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// See https://tools.ietf.org/html/rfc2460#section-3 for details.
mod field {
use crate::wire::field::*;
// 4-bit version number, 8-bit traffic class, and the
// 20-bit flow label.
pub const VER_TC_FLOW: Field = 0..4;
// 16-bit value representing the length of the payload.
// Note: Options are included in this length.
pub const LENGTH: Field = 4..6;
// 8-bit value identifying the type of header following this
// one. Note: The same numbers are used in IPv4.
pub const NXT_HDR: usize = 6;
// 8-bit value decremented by each node that forwards this
// packet. The packet is discarded when the value is 0.
pub const HOP_LIMIT: usize = 7;
// IPv6 address of the source node.
pub const SRC_ADDR: Field = 8..24;
// IPv6 address of the destination node.
pub const DST_ADDR: Field = 24..40;
}
/// Length of an IPv6 header.
pub const HEADER_LEN: usize = field::DST_ADDR.end;
impl<T: AsRef<[u8]>> Packet<T> {
/// Create a raw octet buffer with an IPv6 packet structure.
#[inline]
pub const fn new_unchecked(buffer: T) -> Packet<T> {
Packet { buffer }
}
/// Shorthand for a combination of [new_unchecked] and [check_len].
///
/// [new_unchecked]: #method.new_unchecked
/// [check_len]: #method.check_len
#[inline]
pub fn new_checked(buffer: T) -> Result<Packet<T>> {
let packet = Self::new_unchecked(buffer);
packet.check_len()?;
Ok(packet)
}
/// Ensure that no accessor method will panic if called.
/// Returns `Err(Error)` if the buffer is too short.
///
/// The result of this check is invalidated by calling [set_payload_len].
///
/// [set_payload_len]: #method.set_payload_len
#[inline]
pub fn check_len(&self) -> Result<()> {
let len = self.buffer.as_ref().len();
if len < field::DST_ADDR.end || len < self.total_len() {
Err(Error)
} else {
Ok(())
}
}
/// Consume the packet, returning the underlying buffer.
#[inline]
pub fn into_inner(self) -> T {
self.buffer
}
/// Return the header length.
#[inline]
pub const fn header_len(&self) -> usize {
// This is not a strictly necessary function, but it makes
// code more readable.
field::DST_ADDR.end
}
/// Return the version field.
#[inline]
pub fn version(&self) -> u8 {
let data = self.buffer.as_ref();
data[field::VER_TC_FLOW.start] >> 4
}
/// Return the traffic class.
#[inline]
pub fn traffic_class(&self) -> u8 {
let data = self.buffer.as_ref();
((NetworkEndian::read_u16(&data[0..2]) & 0x0ff0) >> 4) as u8
}
/// Return the flow label field.
#[inline]
pub fn flow_label(&self) -> u32 {
let data = self.buffer.as_ref();
NetworkEndian::read_u24(&data[1..4]) & 0x000fffff
}
/// Return the payload length field.
#[inline]
pub fn payload_len(&self) -> u16 {
let data = self.buffer.as_ref();
NetworkEndian::read_u16(&data[field::LENGTH])
}
/// Return the payload length added to the known header length.
#[inline]
pub fn total_len(&self) -> usize {
self.header_len() + self.payload_len() as usize
}
/// Return the next header field.
#[inline]
pub fn next_header(&self) -> Protocol {
let data = self.buffer.as_ref();
Protocol::from(data[field::NXT_HDR])
}
/// Return the hop limit field.
#[inline]
pub fn hop_limit(&self) -> u8 {
let data = self.buffer.as_ref();
data[field::HOP_LIMIT]
}
/// Return the source address field.
#[inline]
pub fn src_addr(&self) -> Address {
let data = self.buffer.as_ref();
Address::from_bytes(&data[field::SRC_ADDR])
}
/// Return the destination address field.
#[inline]
pub fn dst_addr(&self) -> Address {
let data = self.buffer.as_ref();
Address::from_bytes(&data[field::DST_ADDR])
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> Packet<&'a T> {
/// Return a pointer to the payload.
#[inline]
pub fn payload(&self) -> &'a [u8] {
let data = self.buffer.as_ref();
let range = self.header_len()..self.total_len();
&data[range]
}
}
impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
/// Set the version field.
#[inline]
pub fn set_version(&mut self, value: u8) {
let data = self.buffer.as_mut();
// Make sure to retain the lower order bits which contain
// the higher order bits of the traffic class
data[0] = (data[0] & 0x0f) | ((value & 0x0f) << 4);
}
/// Set the traffic class field.
#[inline]
pub fn set_traffic_class(&mut self, value: u8) {
let data = self.buffer.as_mut();
// Put the higher order 4-bits of value in the lower order
// 4-bits of the first byte
data[0] = (data[0] & 0xf0) | ((value & 0xf0) >> 4);
// Put the lower order 4-bits of value in the higher order
// 4-bits of the second byte
data[1] = (data[1] & 0x0f) | ((value & 0x0f) << 4);
}
/// Set the flow label field.
#[inline]
pub fn set_flow_label(&mut self, value: u32) {
let data = self.buffer.as_mut();
// Retain the lower order 4-bits of the traffic class
let raw = (((data[1] & 0xf0) as u32) << 16) | (value & 0x0fffff);
NetworkEndian::write_u24(&mut data[1..4], raw);
}
/// Set the payload length field.
#[inline]
pub fn set_payload_len(&mut self, value: u16) {
let data = self.buffer.as_mut();
NetworkEndian::write_u16(&mut data[field::LENGTH], value);
}
/// Set the next header field.
#[inline]
pub fn set_next_header(&mut self, value: Protocol) {
let data = self.buffer.as_mut();
data[field::NXT_HDR] = value.into();
}
/// Set the hop limit field.
#[inline]
pub fn set_hop_limit(&mut self, value: u8) {
let data = self.buffer.as_mut();
data[field::HOP_LIMIT] = value;
}
/// Set the source address field.
#[inline]
pub fn set_src_addr(&mut self, value: Address) {
let data = self.buffer.as_mut();
data[field::SRC_ADDR].copy_from_slice(&value.octets());
}
/// Set the destination address field.
#[inline]
pub fn set_dst_addr(&mut self, value: Address) {
let data = self.buffer.as_mut();
data[field::DST_ADDR].copy_from_slice(&value.octets());
}
/// Return a mutable pointer to the payload.
#[inline]
pub fn payload_mut(&mut self) -> &mut [u8] {
let range = self.header_len()..self.total_len();
let data = self.buffer.as_mut();
&mut data[range]
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> fmt::Display for Packet<&'a T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match Repr::parse(self) {
Ok(repr) => write!(f, "{repr}"),
Err(err) => {
write!(f, "IPv6 ({err})")?;
Ok(())
}
}
}
}
impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T> {
fn as_ref(&self) -> &[u8] {
self.buffer.as_ref()
}
}
/// A high-level representation of an Internet Protocol version 6 packet header.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Repr {
/// IPv6 address of the source node.
pub src_addr: Address,
/// IPv6 address of the destination node.
pub dst_addr: Address,
/// Protocol contained in the next header.
pub next_header: Protocol,
/// Length of the payload including the extension headers.
pub payload_len: usize,
/// The 8-bit hop limit field.
pub hop_limit: u8,
}
impl Repr {
/// Parse an Internet Protocol version 6 packet and return a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Packet<&T>) -> Result<Repr> {
// Ensure basic accessors will work
packet.check_len()?;
if packet.version() != 6 {
return Err(Error);
}
Ok(Repr {
src_addr: packet.src_addr(),
dst_addr: packet.dst_addr(),
next_header: packet.next_header(),
payload_len: packet.payload_len() as usize,
hop_limit: packet.hop_limit(),
})
}
/// Return the length of a header that will be emitted from this high-level representation.
pub const fn buffer_len(&self) -> usize {
// This function is not strictly necessary, but it can make client code more readable.
field::DST_ADDR.end
}
/// Emit a high-level representation into an Internet Protocol version 6 packet.
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, packet: &mut Packet<T>) {
// Make no assumptions about the original state of the packet buffer.
// Make sure to set every byte.
packet.set_version(6);
packet.set_traffic_class(0);
packet.set_flow_label(0);
packet.set_payload_len(self.payload_len as u16);
packet.set_hop_limit(self.hop_limit);
packet.set_next_header(self.next_header);
packet.set_src_addr(self.src_addr);
packet.set_dst_addr(self.dst_addr);
}
}
impl fmt::Display for Repr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"IPv6 src={} dst={} nxt_hdr={} hop_limit={}",
self.src_addr, self.dst_addr, self.next_header, self.hop_limit
)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for Repr {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(
fmt,
"IPv6 src={} dst={} nxt_hdr={} hop_limit={}",
self.src_addr,
self.dst_addr,
self.next_header,
self.hop_limit
)
}
}
use crate::wire::pretty_print::{PrettyIndent, PrettyPrint};
// TODO: This is very similar to the implementation for IPv4. Make
// a way to have less copy and pasted code here.
impl<T: AsRef<[u8]>> PrettyPrint for Packet<T> {
fn pretty_print(
buffer: &dyn AsRef<[u8]>,
f: &mut fmt::Formatter,
indent: &mut PrettyIndent,
) -> fmt::Result {
let (ip_repr, payload) = match Packet::new_checked(buffer) {
Err(err) => return write!(f, "{indent}({err})"),
Ok(ip_packet) => match Repr::parse(&ip_packet) {
Err(_) => return Ok(()),
Ok(ip_repr) => {
write!(f, "{indent}{ip_repr}")?;
(ip_repr, ip_packet.payload())
}
},
};
pretty_print_ip_payload(f, indent, ip_repr, payload)
}
}
#[cfg(test)]
pub(crate) mod test {
use super::*;
use crate::wire::pretty_print::PrettyPrinter;
#[allow(unused)]
pub(crate) const MOCK_IP_ADDR_1: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
#[allow(unused)]
pub(crate) const MOCK_IP_ADDR_2: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2);
#[allow(unused)]
pub(crate) const MOCK_IP_ADDR_3: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 3);
#[allow(unused)]
pub(crate) const MOCK_IP_ADDR_4: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 4);
#[allow(unused)]
pub(crate) const MOCK_UNSPECIFIED: Address = Address::UNSPECIFIED;
const LINK_LOCAL_ADDR: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
const UNIQUE_LOCAL_ADDR: Address = Address::new(0xfd00, 0, 0, 201, 1, 1, 1, 1);
const GLOBAL_UNICAST_ADDR: Address = Address::new(0x2001, 0xdb8, 0x3, 0, 0, 0, 0, 1);
const TEST_SOL_NODE_MCAST_ADDR: Address = Address::new(0xff02, 0, 0, 0, 0, 1, 0xff01, 101);
#[test]
fn test_basic_multicast() {
assert!(!LINK_LOCAL_ALL_ROUTERS.is_unspecified());
assert!(LINK_LOCAL_ALL_ROUTERS.is_multicast());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_link_local());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_loopback());
assert!(!LINK_LOCAL_ALL_ROUTERS.x_is_unique_local());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_global_unicast());
assert!(!LINK_LOCAL_ALL_ROUTERS.is_solicited_node_multicast());
assert!(!LINK_LOCAL_ALL_NODES.is_unspecified());
assert!(LINK_LOCAL_ALL_NODES.is_multicast());
assert!(!LINK_LOCAL_ALL_NODES.is_link_local());
assert!(!LINK_LOCAL_ALL_NODES.is_loopback());
assert!(!LINK_LOCAL_ALL_NODES.x_is_unique_local());
assert!(!LINK_LOCAL_ALL_NODES.is_global_unicast());
assert!(!LINK_LOCAL_ALL_NODES.is_solicited_node_multicast());
}
#[test]
fn test_basic_link_local() {
assert!(!LINK_LOCAL_ADDR.is_unspecified());
assert!(!LINK_LOCAL_ADDR.is_multicast());
assert!(LINK_LOCAL_ADDR.is_link_local());
assert!(!LINK_LOCAL_ADDR.is_loopback());
assert!(!LINK_LOCAL_ADDR.x_is_unique_local());
assert!(!LINK_LOCAL_ADDR.is_global_unicast());
assert!(!LINK_LOCAL_ADDR.is_solicited_node_multicast());
}
#[test]
fn test_basic_loopback() {
assert!(!Address::LOCALHOST.is_unspecified());
assert!(!Address::LOCALHOST.is_multicast());
assert!(!Address::LOCALHOST.is_link_local());
assert!(Address::LOCALHOST.is_loopback());
assert!(!Address::LOCALHOST.x_is_unique_local());
assert!(!Address::LOCALHOST.is_global_unicast());
assert!(!Address::LOCALHOST.is_solicited_node_multicast());
}
#[test]
fn test_unique_local() {
assert!(!UNIQUE_LOCAL_ADDR.is_unspecified());
assert!(!UNIQUE_LOCAL_ADDR.is_multicast());
assert!(!UNIQUE_LOCAL_ADDR.is_link_local());
assert!(!UNIQUE_LOCAL_ADDR.is_loopback());
assert!(UNIQUE_LOCAL_ADDR.x_is_unique_local());
assert!(!UNIQUE_LOCAL_ADDR.is_global_unicast());
assert!(!UNIQUE_LOCAL_ADDR.is_solicited_node_multicast());
}
#[test]
fn test_global_unicast() {
assert!(!GLOBAL_UNICAST_ADDR.is_unspecified());
assert!(!GLOBAL_UNICAST_ADDR.is_multicast());
assert!(!GLOBAL_UNICAST_ADDR.is_link_local());
assert!(!GLOBAL_UNICAST_ADDR.is_loopback());
assert!(!GLOBAL_UNICAST_ADDR.x_is_unique_local());
assert!(GLOBAL_UNICAST_ADDR.is_global_unicast());
assert!(!GLOBAL_UNICAST_ADDR.is_solicited_node_multicast());
}
#[test]
fn test_sollicited_node_multicast() {
assert!(!TEST_SOL_NODE_MCAST_ADDR.is_unspecified());
assert!(TEST_SOL_NODE_MCAST_ADDR.is_multicast());
assert!(!TEST_SOL_NODE_MCAST_ADDR.is_link_local());
assert!(!TEST_SOL_NODE_MCAST_ADDR.is_loopback());
assert!(!TEST_SOL_NODE_MCAST_ADDR.x_is_unique_local());
assert!(!TEST_SOL_NODE_MCAST_ADDR.is_global_unicast());
assert!(TEST_SOL_NODE_MCAST_ADDR.is_solicited_node_multicast());
}
#[test]
fn test_mask() {
let addr = Address::new(0x0123, 0x4567, 0x89ab, 0, 0, 0, 0, 1);
assert_eq!(
addr.mask(11),
[0x01, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
assert_eq!(
addr.mask(15),
[0x01, 0x22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
assert_eq!(
addr.mask(26),
[0x01, 0x23, 0x45, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
assert_eq!(
addr.mask(128),
[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
);
assert_eq!(
addr.mask(127),
[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
}
#[test]
fn test_cidr() {
// fe80::1/56
// 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
let cidr = Cidr::new(LINK_LOCAL_ADDR, 56);
let inside_subnet = [
// fe80::2
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02,
],
// fe80::1122:3344:5566:7788
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x77, 0x88,
],
// fe80::ff00:0:0:0
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
],
// fe80::ff
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff,
],
];
let outside_subnet = [
// fe80:0:0:101::1
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01,
],
// ::1
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01,
],
// ff02::1
[
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01,
],
// ff02::2
[
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x02,
],
];
let subnets = [
// fe80::ffff:ffff:ffff:ffff/65
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff,
],
65,
),
// fe80::1/128
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01,
],
128,
),
// fe80::1234:5678/96
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
0x34, 0x56, 0x78,
],
96,
),
];
let not_subnets = [
// fe80::101:ffff:ffff:ffff:ffff/55
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff,
],
55,
),
// fe80::101:ffff:ffff:ffff:ffff/56
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff,
],
56,
),
// fe80::101:ffff:ffff:ffff:ffff/57
(
[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff,
],
57,
),
// ::1/128
(
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01,
],
128,
),
];
for addr in inside_subnet.iter().map(|a| Address::from_bytes(a)) {
assert!(cidr.contains_addr(&addr));
}
for addr in outside_subnet.iter().map(|a| Address::from_bytes(a)) {
assert!(!cidr.contains_addr(&addr));
}
for subnet in subnets.iter().map(|&(a, p)| Cidr::new(Address::from(a), p)) {
assert!(cidr.contains_subnet(&subnet));
}
for subnet in not_subnets
.iter()
.map(|&(a, p)| Cidr::new(Address::from(a), p))
{
assert!(!cidr.contains_subnet(&subnet));
}
let cidr_without_prefix = Cidr::new(LINK_LOCAL_ADDR, 0);
assert!(cidr_without_prefix.contains_addr(&Address::LOCALHOST));
}
#[test]
#[should_panic(expected = "length")]
fn test_from_bytes_too_long() {
let _ = Address::from_bytes(&[0u8; 15]);
}
#[test]
fn test_scope() {
use super::*;
assert_eq!(
Address::new(0xff01, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::InterfaceLocal
);
assert_eq!(
Address::new(0xff02, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::LinkLocal
);
assert_eq!(
Address::new(0xff03, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::Unknown
);
assert_eq!(
Address::new(0xff04, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::AdminLocal
);
assert_eq!(
Address::new(0xff05, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::SiteLocal
);
assert_eq!(
Address::new(0xff08, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::OrganizationLocal
);
assert_eq!(
Address::new(0xff0e, 0, 0, 0, 0, 0, 0, 1).x_multicast_scope(),
MulticastScope::Global
);
assert_eq!(
LINK_LOCAL_ALL_NODES.x_multicast_scope(),
MulticastScope::LinkLocal
);
// For source address selection, unicast addresses also have a scope:
assert_eq!(
LINK_LOCAL_ADDR.x_multicast_scope(),
MulticastScope::LinkLocal
);
assert_eq!(
GLOBAL_UNICAST_ADDR.x_multicast_scope(),
MulticastScope::Global
);
assert_eq!(
UNIQUE_LOCAL_ADDR.x_multicast_scope(),
MulticastScope::Global
);
}
static REPR_PACKET_BYTES: [u8; 52] = [
0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00,
0x0c, 0x02, 0x4e, 0xff, 0xff, 0xff, 0xff,
];
static REPR_PAYLOAD_BYTES: [u8; 12] = [
0x00, 0x01, 0x00, 0x02, 0x00, 0x0c, 0x02, 0x4e, 0xff, 0xff, 0xff, 0xff,
];