crafter 0.3.0

Packet-level network interaction for Rust tools and agents.
Documentation
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
//! ICMPv6 layer implementation.
//!
//! Moved out of `icmp/icmpv6.rs` into the `icmp/v6/` subtree so ICMPv6 sits as a
//! sibling of the ICMPv4 code in `icmp/v4/` under the shared core. This holds the
//! [`Icmpv6`] header struct and its builder/decode/compile impls plus the ICMPv6
//! (`ICMPV6_*`) codepoint constants (in `v6/constants.rs`). Shared compile/
//! auto-fill helpers, the version-neutral [`IcmpKind`]/[`IcmpLayer`] contract,
//! and the codepoint naming/predicate helpers are reached through the `icmp`
//! module root (`use super::*;`), one level above which this file now lives.
//! This is a pure internal move:
//! `crate::protocols::icmp::Icmpv6`, the `protocols::mod.rs` re-exports, the
//! registry IPv6 next-header 58 binding, and the prelude all keep resolving to
//! the same names.

use super::*;

// The ICMPv6 (`ICMPV6_*`) codepoint constants used below live in
// `v6/constants.rs`. They are reached through the `use super::*;` glob above:
// `icmp/mod.rs` re-exports them at the root (`pub use self::constants::*;`),
// which in turn re-exports `super::v6::constants::*`.
pub(crate) mod constants;

// The typed ICMPv6 message-body model ([`Icmpv6Body`] / [`Icmpv6ErrorBody`]),
// dispatched by the `type` byte the way ICMPv4 dispatches its bodies. The
// `Icmpv6` header below routes through it for classification, `summary()`, and
// `show()`. Re-exported at the `icmp` root so it surfaces through
// `protocols::mod.rs` and the prelude like the other ICMPv6 types.
mod body;
pub use self::body::{Icmpv6Body, Icmpv6ErrorBody};

// The ICMPv6 decode path: the `type` byte → typed-body dispatch
// ([`append_icmpv6_packet`] and its private header parser), the analogue of the
// ICMPv4 decode in `icmp/decode.rs`. Kept in its own module so the dispatch sits
// alongside the message bodies it routes to under `v6/`. Re-export
// `append_icmpv6_packet` at this module level (and onward at the `icmp` root,
// `pub(crate) use self::v6::append_icmpv6_packet;`) so the registry IPv6
// next-header 58 binding in `crate::registry` keeps resolving to the same name.
mod decode;
pub(crate) use self::decode::append_icmpv6_packet;

// ICMPv6-specific message bodies (Neighbor Discovery, MLD, node information,
// extended echo) live under `v6/message/`. This step adds the shared Neighbor
// Discovery option TLV framework ([`NdpOption`] / [`NdpOptions`] and the
// `NDP_OPT_*` codepoints) per RFC 4861 section 4.6; later steps grow the
// concrete message bodies. Re-exported at the `icmp` root (and onward through
// `protocols::mod.rs` and the prelude) like the other ICMPv6 types.
mod message;
// RFC 8335 ICMPv6 extended echo (types 160/161). The builders live in
// `message/extended_echo.rs` (an `impl Icmpv6` that sets the type/code, like the
// NDP builders); the flag-byte masks, reply codes, and State values are
// re-exported here so the header's own rest-of-header packing and the typed
// flag accessors in this file resolve them, and so the `icmp` root surfaces them
// like the other ICMPv6 codepoints.
pub use self::message::extended_echo::{
    ICMPV6_CODE_EXTENDED_ECHO_REPLY_MALFORMED_QUERY,
    ICMPV6_CODE_EXTENDED_ECHO_REPLY_MULTIPLE_INTERFACES, ICMPV6_CODE_EXTENDED_ECHO_REPLY_NO_ERROR,
    ICMPV6_CODE_EXTENDED_ECHO_REPLY_NO_SUCH_INTERFACE,
    ICMPV6_CODE_EXTENDED_ECHO_REPLY_NO_SUCH_TABLE_ENTRY, ICMPV6_EXTENDED_ECHO_REPLY_ACTIVE,
    ICMPV6_EXTENDED_ECHO_REPLY_IPV4, ICMPV6_EXTENDED_ECHO_REPLY_IPV6,
    ICMPV6_EXTENDED_ECHO_REPLY_STATE_DELAY, ICMPV6_EXTENDED_ECHO_REPLY_STATE_FAILED,
    ICMPV6_EXTENDED_ECHO_REPLY_STATE_INCOMPLETE, ICMPV6_EXTENDED_ECHO_REPLY_STATE_PROBE,
    ICMPV6_EXTENDED_ECHO_REPLY_STATE_REACHABLE, ICMPV6_EXTENDED_ECHO_REPLY_STATE_RESERVED,
    ICMPV6_EXTENDED_ECHO_REPLY_STATE_STALE, ICMPV6_EXTENDED_ECHO_REQUEST_L_BIT,
};
// MLDv1 (RFC 2710, types 130-132): one body type reused across Query / Report /
// Done, distinguished by the header `type`. The builders live in
// `message/mld.rs` (an `impl Icmpv6`, like the NDP builders); re-export the body
// type and its decoder so the `icmp` root surfaces it and the decode dispatch
// below reaches it.
pub(crate) use self::message::mld::{
    decode_mldv2_query, decode_mldv2_report, decode_multicast_listener_message,
};
pub use self::message::mld::{
    Mldv2Query, Mldv2Report, MulticastAddressRecord, MulticastListenerMessage, MulticastRecordType,
    MLDV2_QUERY_MIN_BODY_LEN, MLDV2_QUERY_QRV_MASK, MLDV2_QUERY_RESV_MASK, MLDV2_QUERY_S_FLAG,
};
// RFC 4620 Node Information Query (139) / Response (140), **experimental**: one
// body type ([`NodeInformation`]) reused across both, carrying the Nonce + Data
// (the Qtype/Flags live on the header rest-of-header). The builders live in
// `message/node_info.rs` (an `impl Icmpv6`, like the NDP/MLD builders); re-export
// the body type, the Qtype/Code constants, and the decoder so the `icmp` root
// surfaces them and the decode dispatch below reaches them.
pub(crate) use self::message::ndp::{
    decode_neighbor_advertisement, decode_neighbor_solicitation, decode_redirect,
    decode_router_advertisement, decode_router_solicitation,
};
pub use self::message::ndp::{
    NeighborAdvertisement, NeighborSolicitation, Redirect, RouterAdvertisement, RouterSolicitation,
    ICMPV6_NA_FLAGS_RESERVED, ICMPV6_NA_FLAG_OVERRIDE, ICMPV6_NA_FLAG_ROUTER,
    ICMPV6_NA_FLAG_SOLICITED, ICMPV6_RA_DEFAULT_CUR_HOP_LIMIT, ICMPV6_RA_DEFAULT_ROUTER_LIFETIME,
    ICMPV6_RA_FLAGS_RESERVED, ICMPV6_RA_FLAG_MANAGED, ICMPV6_RA_FLAG_OTHER,
};
pub use self::message::ndp_option::{
    ndp_option_type_is_known, ndp_option_type_name, ndp_rdnss_length_units, NdpOption, NdpOptions,
    Pref64Plc, Prf, NDP_DNS_LIFETIME_INFINITY, NDP_DNS_RESERVED_LEN,
    NDP_LINK_LAYER_ADDR_ETHERNET_LEN, NDP_MTU_OPTION_LEN, NDP_MTU_OPTION_UNITS, NDP_NONCE_MIN_LEN,
    NDP_OPTION_HEADER_LEN, NDP_OPTION_LENGTH_UNIT, NDP_OPT_CAPTIVE_PORTAL, NDP_OPT_DNSSL,
    NDP_OPT_MTU, NDP_OPT_NONCE, NDP_OPT_PREF64, NDP_OPT_PREFIX_INFORMATION,
    NDP_OPT_RA_FLAGS_EXTENSION, NDP_OPT_RDNSS, NDP_OPT_REDIRECTED_HEADER,
    NDP_OPT_ROUTE_INFORMATION, NDP_OPT_SOURCE_LINK_LAYER_ADDR, NDP_OPT_TARGET_LINK_LAYER_ADDR,
    NDP_PREF64_LEN, NDP_PREF64_PLC_MASK, NDP_PREF64_PREFIX_LEN, NDP_PREF64_SCALED_LIFETIME_MAX,
    NDP_PREF64_SCALED_LIFETIME_SHIFT, NDP_PREF64_UNITS, NDP_PREFIX_FLAGS_RESERVED,
    NDP_PREFIX_FLAG_AUTONOMOUS, NDP_PREFIX_FLAG_ON_LINK, NDP_PREFIX_INFORMATION_LEN,
    NDP_PREFIX_INFORMATION_UNITS, NDP_PREFIX_LIFETIME_INFINITY, NDP_PRF_MASK, NDP_PRF_SHIFT,
    NDP_RA_FLAGS_EXTENSION_BITS_LEN, NDP_RA_FLAGS_EXTENSION_LEN, NDP_RA_FLAGS_EXTENSION_UNITS,
    NDP_RDNSS_ADDRESS_LEN, NDP_REDIRECTED_HEADER_RESERVED_LEN,
    NDP_ROUTE_INFORMATION_LEN_FULL_PREFIX, NDP_ROUTE_INFORMATION_LEN_HALF_PREFIX,
    NDP_ROUTE_INFORMATION_LEN_NO_PREFIX, NDP_ROUTE_LIFETIME_INFINITY,
};
pub(crate) use self::message::node_info::decode_node_information;
pub use self::message::node_info::{
    NodeInformation, NI_NONCE_LEN, NI_QTYPE_IPV4_ADDRESSES, NI_QTYPE_NODE_ADDRESSES,
    NI_QTYPE_NODE_NAME, NI_QTYPE_NOOP, NI_QUERY_CODE_SUBJECT_IPV4, NI_QUERY_CODE_SUBJECT_IPV6,
    NI_QUERY_CODE_SUBJECT_NAME, NI_RESPONSE_CODE_REFUSED, NI_RESPONSE_CODE_SUCCESS,
    NI_RESPONSE_CODE_UNKNOWN_QTYPE,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Icmpv6 {
    icmp_type: Field<u8>,
    code: Field<u8>,
    checksum: Field<u16>,
    rest_of_header: Field<[u8; 4]>,
    identifier: Field<u16>,
    sequence_number: Field<u16>,
    length: Field<u8>,
    mtu: Field<u32>,
    pointer: Field<u32>,
    // RFC 8335 extended echo flag byte (the fourth byte of the rest-of-header):
    // the L-bit for an Extended Echo Request and the State/A/4/6 status bits for
    // an Extended Echo Reply. Carried on the header (like the identifier and
    // sequence number) because it lives in the fixed rest-of-header; the typed
    // builders/accessors live in `message/extended_echo.rs`.
    extended_flags: Field<u8>,
}

impl Icmpv6 {
    /// Create an ICMPv6 echo-request header with deterministic defaults.
    pub fn new() -> Self {
        Self {
            icmp_type: Field::defaulted(ICMPV6_ECHO_REQUEST),
            code: Field::defaulted(0),
            checksum: Field::unset(),
            rest_of_header: Field::defaulted([0; 4]),
            identifier: Field::defaulted(0),
            sequence_number: Field::defaulted(0),
            length: Field::unset(),
            mtu: Field::unset(),
            pointer: Field::unset(),
            extended_flags: Field::unset(),
        }
    }

    /// Create an echo request.
    pub fn echo_request() -> Self {
        Self::new().kind(IcmpKind::EchoRequest)
    }

    /// Create an echo reply.
    pub fn echo_reply() -> Self {
        Self::new().kind(IcmpKind::EchoReply)
    }

    /// Create a time-exceeded message.
    pub fn time_exceeded() -> Self {
        Self::new().kind(IcmpKind::TimeExceeded)
    }

    /// Create a destination-unreachable message.
    pub fn destination_unreachable() -> Self {
        Self::new().kind(IcmpKind::DestinationUnreachable)
    }

    /// Create a packet-too-big message.
    pub fn packet_too_big() -> Self {
        Self::new().icmp_type(ICMPV6_PACKET_TOO_BIG)
    }

    /// Set the ICMPv6 type from a common kind.
    pub fn kind(mut self, kind: IcmpKind) -> Self {
        self.icmp_type.set_user(kind.ipv6_type());
        self
    }

    /// Set the raw ICMPv6 type.
    pub fn icmp_type(mut self, icmp_type: u8) -> Self {
        self.icmp_type.set_user(icmp_type);
        self
    }

    /// Alias for generated code that wants the protocol field name.
    pub fn type_(self, icmp_type: u8) -> Self {
        self.icmp_type(icmp_type)
    }

    /// Set the ICMPv6 code.
    pub fn code(mut self, code: u8) -> Self {
        self.code.set_user(code);
        self
    }

    /// Set the checksum explicitly.
    pub fn checksum(mut self, checksum: u16) -> Self {
        self.checksum.set_user(checksum);
        self
    }

    /// Compatibility alias for checksum.
    pub fn chksum(self, checksum: u16) -> Self {
        self.checksum(checksum)
    }

    /// Set the raw four-byte rest-of-header field.
    pub fn rest_of_header(mut self, rest_of_header: [u8; 4]) -> Self {
        self.rest_of_header.set_user(rest_of_header);
        self
    }

    /// Set the echo identifier.
    pub fn identifier(mut self, identifier: u16) -> Self {
        self.identifier.set_user(identifier);
        self
    }

    /// Compatibility alias for identifier.
    pub fn id(self, identifier: u16) -> Self {
        self.identifier(identifier)
    }

    /// Set the echo sequence number.
    pub fn sequence_number(mut self, sequence_number: u16) -> Self {
        self.sequence_number.set_user(sequence_number);
        self
    }

    /// Compatibility alias for sequence number.
    pub fn seq(self, sequence_number: u16) -> Self {
        self.sequence_number(sequence_number)
    }

    /// Set the RFC 4884 original datagram length field explicitly.
    pub fn length(mut self, length: u8) -> Self {
        self.length.set_user(length);
        self
    }

    /// Compatibility alias for RFC 4884 length.
    pub fn len(self, length: u8) -> Self {
        self.length(length)
    }

    /// Set the packet-too-big MTU field.
    pub fn mtu(mut self, mtu: u32) -> Self {
        self.mtu.set_user(mtu);
        self
    }

    /// Set the parameter-problem pointer field.
    pub fn pointer(mut self, pointer: u32) -> Self {
        self.pointer.set_user(pointer);
        self
    }

    /// Set the raw RFC 8335 extended echo flag byte (the fourth byte of the
    /// rest-of-header) explicitly.
    ///
    /// This is the escape hatch for crafting reserved bits and flag combinations
    /// the typed builders do not expose; an explicit byte survives compilation
    /// untouched. The typed setters ([`Icmpv6::extended_l_bit`],
    /// [`Icmpv6::extended_state`], [`Icmpv6::extended_active`],
    /// [`Icmpv6::extended_ipv4`], [`Icmpv6::extended_ipv6`]) build on it.
    pub fn extended_flags(mut self, extended_flags: u8) -> Self {
        self.extended_flags.set_user(extended_flags);
        self
    }

    /// Set the RFC 8335 extended echo request L-bit (the probed interface is on
    /// a proxy node).
    ///
    /// The L-bit is the rightmost bit of the request flag byte (RFC 8335 section
    /// 3); the other seven Reserved bits are left untouched (use
    /// [`Icmpv6::extended_flags`] to set Reserved bits deliberately).
    pub fn extended_l_bit(mut self, set: bool) -> Self {
        let base = self.extended_flags.value().copied().unwrap_or(0);
        let value = if set {
            base | ICMPV6_EXTENDED_ECHO_REQUEST_L_BIT
        } else {
            base & !ICMPV6_EXTENDED_ECHO_REQUEST_L_BIT
        };
        self.extended_flags.set_user(value);
        self
    }

    /// Set the RFC 8335 extended echo reply State field (3 bits, occupying the
    /// top of the reply flag byte).
    pub fn extended_state(mut self, state: u8) -> Self {
        let base = self.extended_flags.value().copied().unwrap_or(0);
        let value = (base & 0x1f) | ((state & 0x07) << 5);
        self.extended_flags.set_user(value);
        self
    }

    /// Set the RFC 8335 extended echo reply Active (A) flag.
    pub fn extended_active(mut self, set: bool) -> Self {
        let base = self.extended_flags.value().copied().unwrap_or(0);
        let value = if set {
            base | ICMPV6_EXTENDED_ECHO_REPLY_ACTIVE
        } else {
            base & !ICMPV6_EXTENDED_ECHO_REPLY_ACTIVE
        };
        self.extended_flags.set_user(value);
        self
    }

    /// Set the RFC 8335 extended echo reply IPv4 (4) flag.
    pub fn extended_ipv4(mut self, set: bool) -> Self {
        let base = self.extended_flags.value().copied().unwrap_or(0);
        let value = if set {
            base | ICMPV6_EXTENDED_ECHO_REPLY_IPV4
        } else {
            base & !ICMPV6_EXTENDED_ECHO_REPLY_IPV4
        };
        self.extended_flags.set_user(value);
        self
    }

    /// Set the RFC 8335 extended echo reply IPv6 (6) flag.
    pub fn extended_ipv6(mut self, set: bool) -> Self {
        let base = self.extended_flags.value().copied().unwrap_or(0);
        let value = if set {
            base | ICMPV6_EXTENDED_ECHO_REPLY_IPV6
        } else {
            base & !ICMPV6_EXTENDED_ECHO_REPLY_IPV6
        };
        self.extended_flags.set_user(value);
        self
    }

    /// Raw ICMPv6 type value.
    pub fn icmp_type_value(&self) -> u8 {
        value_or_copy(&self.icmp_type, ICMPV6_ECHO_REQUEST)
    }

    /// ICMPv6 code value.
    pub fn code_value(&self) -> u8 {
        value_or_copy(&self.code, 0)
    }

    /// Stored checksum value, when explicit or decoded.
    pub fn checksum_value(&self) -> Option<u16> {
        self.checksum.value().copied()
    }

    /// Identifier value when meaningful for the current type.
    ///
    /// Surfaced for the RFC 4443 echo messages and for the RFC 8335 extended echo
    /// messages, both of which carry a 16-bit identifier in the first half of the
    /// rest-of-header.
    pub fn identifier_value(&self) -> Option<u16> {
        let icmp_type = self.icmp_type_value();
        if is_echo_v6(icmp_type) || is_extended_echo_v6(icmp_type) {
            Some(value_or_u16_from_rest(
                &self.identifier,
                &self.rest_of_header,
                0,
            ))
        } else {
            None
        }
    }

    /// Sequence number when meaningful for the current type.
    ///
    /// Surfaced for the RFC 4443 echo messages as a 16-bit value, and for the
    /// RFC 8335 extended echo messages as an 8-bit value (the third byte of the
    /// rest-of-header, zero-extended here; the fourth byte is the flag byte).
    pub fn sequence_number_value(&self) -> Option<u16> {
        let icmp_type = self.icmp_type_value();
        if is_extended_echo_v6(icmp_type) {
            Some(u16::from(value_or_u8_from_rest(
                &self.extended_sequence_number_byte(),
                &self.rest_of_header,
                2,
            )))
        } else if is_echo_v6(icmp_type) {
            Some(value_or_u16_from_rest(
                &self.sequence_number,
                &self.rest_of_header,
                2,
            ))
        } else {
            None
        }
    }

    /// Raw four-byte rest-of-header value after applying typed fields.
    pub fn rest_of_header_value(&self) -> [u8; 4] {
        self.effective_rest_of_header(None, 8).unwrap_or([0; 4])
    }

    /// RFC 4884 length field when explicit or decoded.
    pub fn length_value(&self) -> Option<u8> {
        self.length.value().copied()
    }

    /// RFC 8335 extended echo flag byte (the fourth byte of the rest-of-header)
    /// when the type is an extended echo request or reply.
    ///
    /// The byte is surfaced verbatim so Reserved bits stay inspectable; the typed
    /// accessors below interpret the individual flags.
    pub fn extended_flags_value(&self) -> Option<u8> {
        if is_extended_echo_v6(self.icmp_type_value()) {
            Some(value_or_u8_from_rest(
                &self.extended_flags,
                &self.rest_of_header,
                3,
            ))
        } else {
            None
        }
    }

    /// RFC 8335 extended echo request L-bit when the type is an extended echo
    /// request.
    pub fn extended_l_bit_value(&self) -> Option<bool> {
        if self.icmp_type_value() == ICMPV6_EXTENDED_ECHO_REQUEST {
            self.extended_flags_value()
                .map(|flags| flags & ICMPV6_EXTENDED_ECHO_REQUEST_L_BIT != 0)
        } else {
            None
        }
    }

    /// RFC 8335 extended echo reply State field (3 bits) when the type is an
    /// extended echo reply.
    pub fn extended_state_value(&self) -> Option<u8> {
        if self.icmp_type_value() == ICMPV6_EXTENDED_ECHO_REPLY {
            self.extended_flags_value().map(|flags| (flags >> 5) & 0x07)
        } else {
            None
        }
    }

    /// RFC 8335 extended echo reply Active (A) flag when the type is an extended
    /// echo reply.
    pub fn extended_active_value(&self) -> Option<bool> {
        if self.icmp_type_value() == ICMPV6_EXTENDED_ECHO_REPLY {
            self.extended_flags_value()
                .map(|flags| flags & ICMPV6_EXTENDED_ECHO_REPLY_ACTIVE != 0)
        } else {
            None
        }
    }

    /// RFC 8335 extended echo reply IPv4 (4) flag when the type is an extended
    /// echo reply.
    pub fn extended_ipv4_value(&self) -> Option<bool> {
        if self.icmp_type_value() == ICMPV6_EXTENDED_ECHO_REPLY {
            self.extended_flags_value()
                .map(|flags| flags & ICMPV6_EXTENDED_ECHO_REPLY_IPV4 != 0)
        } else {
            None
        }
    }

    /// RFC 8335 extended echo reply IPv6 (6) flag when the type is an extended
    /// echo reply.
    pub fn extended_ipv6_value(&self) -> Option<bool> {
        if self.icmp_type_value() == ICMPV6_EXTENDED_ECHO_REPLY {
            self.extended_flags_value()
                .map(|flags| flags & ICMPV6_EXTENDED_ECHO_REPLY_IPV6 != 0)
        } else {
            None
        }
    }

    /// Common ICMP kind, when the type is version-independent.
    pub fn kind_value(&self) -> Option<IcmpKind> {
        match self.icmp_type_value() {
            ICMPV6_DESTINATION_UNREACHABLE => Some(IcmpKind::DestinationUnreachable),
            ICMPV6_TIME_EXCEEDED => Some(IcmpKind::TimeExceeded),
            ICMPV6_PARAMETER_PROBLEM => Some(IcmpKind::ParameterProblem),
            ICMPV6_ECHO_REQUEST => Some(IcmpKind::EchoRequest),
            ICMPV6_ECHO_REPLY => Some(IcmpKind::EchoReply),
            _ => None,
        }
    }

    /// The typed message body this header carries, dispatched by the ICMPv6
    /// `type`.
    ///
    /// This is the ICMPv6 analogue of the ICMPv4 type-dispatched body model: it
    /// classifies the message (echo / error / unknown) and surfaces the
    /// type-specific rest-of-header fields as a typed [`Icmpv6Body`]. The body is
    /// a view derived from the header's fields, so reading it never changes the
    /// bytes the header emits; later steps grow it with the NDP, MLD,
    /// node-information, and extended-echo families.
    pub fn body(&self) -> Icmpv6Body {
        Icmpv6Body::from_header(self)
    }

    fn effective_rest_of_header(
        &self,
        ctx: Option<LayerContext<'_>>,
        extension_unit: usize,
    ) -> Result<[u8; 4]> {
        // Same precedence rule as ICMPv4: an explicit raw rest-of-header is the
        // base, and auto-filled typed values never overwrite it.
        let raw_is_user = self.rest_of_header.is_user_set();
        let mut rest = value_or_copy(&self.rest_of_header, [0; 4]);

        match self.icmp_type_value() {
            ICMPV6_ECHO_REQUEST | ICMPV6_ECHO_REPLY => {
                if self.identifier.is_user_set() || !raw_is_user {
                    let identifier =
                        value_or_u16_from_rest(&self.identifier, &self.rest_of_header, 0);
                    rest[..2].copy_from_slice(&identifier.to_be_bytes());
                }
                if self.sequence_number.is_user_set() || !raw_is_user {
                    let sequence =
                        value_or_u16_from_rest(&self.sequence_number, &self.rest_of_header, 2);
                    rest[2..4].copy_from_slice(&sequence.to_be_bytes());
                }
            }
            ICMPV6_DESTINATION_UNREACHABLE | ICMPV6_TIME_EXCEEDED => {
                if let Some(length) =
                    self.overriding_extension_length(ctx, extension_unit, raw_is_user)?
                {
                    rest[0] = length;
                }
            }
            ICMPV6_PACKET_TOO_BIG => {
                if self.mtu.is_user_set() || !raw_is_user {
                    if let Some(mtu) = self.mtu.value().copied() {
                        rest.copy_from_slice(&mtu.to_be_bytes());
                    }
                }
            }
            ICMPV6_PARAMETER_PROBLEM => {
                if self.pointer.is_user_set() || !raw_is_user {
                    if let Some(pointer) = self.pointer.value().copied() {
                        rest.copy_from_slice(&pointer.to_be_bytes());
                    }
                }
            }
            ICMPV6_EXTENDED_ECHO_REQUEST | ICMPV6_EXTENDED_ECHO_REPLY => {
                // RFC 8335 section 3: identifier (bytes 0-1), an 8-bit sequence
                // number (byte 2), and a flag byte (byte 3). The flag byte holds
                // the L-bit for requests and State/A/4/6 for replies; the typed
                // builders pre-pack it into `extended_flags`. This mirrors the
                // ICMPv4 extended-echo rest-of-header layout exactly (the only
                // wire difference is the type number and the IPv6-pseudo-header
                // checksum, which `compile()` already handles).
                if self.identifier.is_user_set() || !raw_is_user {
                    let identifier =
                        value_or_u16_from_rest(&self.identifier, &self.rest_of_header, 0);
                    rest[..2].copy_from_slice(&identifier.to_be_bytes());
                }
                if self.sequence_number.is_user_set() || !raw_is_user {
                    let sequence = value_or_u8_from_rest(
                        &self.extended_sequence_number_byte(),
                        &self.rest_of_header,
                        2,
                    );
                    rest[2] = sequence;
                }
                if self.extended_flags.is_user_set() || !raw_is_user {
                    if let Some(flags) = self.extended_flags.value().copied() {
                        rest[3] = flags;
                    }
                }
            }
            _ => {}
        }

        Ok(rest)
    }

    /// The RFC 8335 8-bit sequence number as a `Field<u8>` derived from the
    /// 16-bit `sequence_number` field: a user-set sequence number contributes its
    /// low octet, otherwise the field stays unset so the rest-of-header supplies
    /// the byte. Mirrors the ICMPv4 `sequence_number_byte` helper.
    fn extended_sequence_number_byte(&self) -> Field<u8> {
        match self.sequence_number.value().copied() {
            Some(value) => Field::user(value as u8),
            None => Field::unset(),
        }
    }

    /// RFC 4884 length byte that should override the raw rest-of-header.
    fn overriding_extension_length(
        &self,
        ctx: Option<LayerContext<'_>>,
        extension_unit: usize,
        raw_is_user: bool,
    ) -> Result<Option<u8>> {
        if self.length.is_user_set() {
            return Ok(self.length.value().copied());
        }
        if raw_is_user {
            return Ok(None);
        }
        self.effective_extension_length(ctx, extension_unit)
    }

    fn effective_extension_length(
        &self,
        ctx: Option<LayerContext<'_>>,
        unit: usize,
    ) -> Result<Option<u8>> {
        if let Some(length) = self.length.value().copied() {
            return Ok(Some(length));
        }
        let Some(ctx) = ctx else {
            return Ok(None);
        };
        if !icmpv6_type_allows_extensions(self.icmp_type_value()) {
            return Ok(None);
        }

        let len = encoded_len_until_extension(ctx);
        u8::try_from(len / unit).map(Some).map_err(|_| {
            CrafterError::invalid_field_value(
                "icmpv6.length",
                "ICMPv6 original datagram length does not fit in one byte",
            )
        })
    }
}

impl Default for Icmpv6 {
    fn default() -> Self {
        Self::new()
    }
}

impl IcmpLayer for Icmpv6 {
    fn icmp_type_value(&self) -> u8 {
        Icmpv6::icmp_type_value(self)
    }

    fn code_value(&self) -> u8 {
        Icmpv6::code_value(self)
    }

    fn checksum_value(&self) -> Option<u16> {
        Icmpv6::checksum_value(self)
    }

    fn identifier_value(&self) -> Option<u16> {
        Icmpv6::identifier_value(self)
    }

    fn sequence_number_value(&self) -> Option<u16> {
        Icmpv6::sequence_number_value(self)
    }

    fn kind(&self) -> Option<IcmpKind> {
        self.kind_value()
    }
}

impl Layer for Icmpv6 {
    fn name(&self) -> &'static str {
        "Icmpv6"
    }

    fn summary(&self) -> String {
        format!(
            "Icmpv6(type={}, code={}, id={}, seq={})",
            icmpv6_type_summary(self.icmp_type_value()),
            self.code_value(),
            self.identifier_value()
                .map(|value| value.to_string())
                .unwrap_or_else(|| "-".to_string()),
            self.sequence_number_value()
                .map(|value| value.to_string())
                .unwrap_or_else(|| "-".to_string())
        )
    }

    fn inspection_fields(&self) -> Vec<(&'static str, String)> {
        let body = self.body();
        vec![
            ("type", icmpv6_type_summary(self.icmp_type_value())),
            ("code", self.code_value().to_string()),
            ("body", body.detail()),
            (
                "checksum",
                self.checksum_value()
                    .map(|value| format!("0x{value:04x}"))
                    .unwrap_or_else(|| "auto".to_string()),
            ),
            ("rest_of_header", hex_bytes(&self.rest_of_header_value())),
            (
                "identifier",
                self.identifier_value()
                    .map(|value| format!("0x{value:04x}"))
                    .unwrap_or_else(|| "-".to_string()),
            ),
            (
                "sequence_number",
                self.sequence_number_value()
                    .map(|value| value.to_string())
                    .unwrap_or_else(|| "-".to_string()),
            ),
            (
                "length",
                self.length_value()
                    .map(|value| value.to_string())
                    .unwrap_or_else(|| "auto".to_string()),
            ),
        ]
    }

    fn encoded_len(&self) -> usize {
        ICMP_HEADER_LEN
    }

    fn encoded_len_with_context(&self, ctx: &LayerContext<'_>) -> usize {
        ICMP_HEADER_LEN + payload_len_after(*ctx)
    }

    fn compile(&self, ctx: &LayerContext<'_>, out: &mut Vec<u8>) -> Result<()> {
        let rest_of_header = self.effective_rest_of_header(Some(*ctx), 8)?;
        let start = out.len();
        out.push(self.icmp_type_value());
        out.push(self.code_value());
        out.extend_from_slice(&0u16.to_be_bytes());
        out.extend_from_slice(&rest_of_header);

        if let Err(err) = compile_payload_after_into(*ctx, out) {
            out.truncate(start);
            return Err(err);
        }
        let checksum = self.checksum.value().copied().unwrap_or_else(|| {
            checksum_context(*ctx, IPPROTO_ICMPV6)
                .map(|pseudo_header| pseudo_header.checksum(&out[start..]))
                .unwrap_or(0)
        });
        out[start + 2..start + 4].copy_from_slice(&checksum.to_be_bytes());
        Ok(())
    }

    fn consumes_following(&self) -> bool {
        true
    }

    impl_layer_object!(Icmpv6);
}

impl_layer_div!(Icmpv6);

#[cfg(test)]
mod icmpv6 {
    use super::{IcmpKind, Icmpv6, Icmpv6Body, Icmpv6ErrorBody};
    use crate::packet::Layer;
    use crate::protocols::icmp::{
        ICMPV6_ECHO_REQUEST, ICMPV6_PACKET_TOO_BIG, ICMPV6_PARAMETER_PROBLEM,
    };

    // An ICMPv6 `type` that is never modeled with a typed body — used as the
    // stand-in "unknown type" in `icmpv6_body_preserves_unknown_type`. Type 200
    // is unassigned in the IANA ICMPv6 type registry and is not implemented by
    // any NDP/MLD/extended-echo step, so it stays a genuine Unknown-classification
    // test even as more message types are modeled.
    const UNMODELED_ICMPV6_TYPE: u8 = 200;
    use crate::{Ipv6, NetworkLayer, Packet, Raw};
    use core::net::Ipv6Addr;

    const IPV6_ICMP_FIXTURE: &[u8] = fixture_bytes!("bytes/ipv6-icmp-echo-request.bin");

    fn src() -> Ipv6Addr {
        Ipv6Addr::new(0x2001, 0x0db8, 1, 0, 0, 0, 0, 0x0010)
    }

    fn dst() -> Ipv6Addr {
        Ipv6Addr::new(0x2001, 0x0db8, 2, 0, 0, 0, 0, 0x0020)
    }

    #[test]
    fn icmpv6_echo_request_matches_golden_bytes() {
        let packet = Ipv6::new().src(src()).dst(dst()).fl(0x12345).hlim(64)
            / Icmpv6::echo_request().id(0x4242).seq(2)
            / Raw::from("libcrafter-ipv6");
        let bytes = packet.compile().unwrap();

        assert_eq!(bytes.as_bytes(), IPV6_ICMP_FIXTURE);
        assert_eq!(&bytes.as_bytes()[40..42], &[ICMPV6_ECHO_REQUEST, 0]);
        assert_eq!(&bytes.as_bytes()[42..44], &0x00d0u16.to_be_bytes());
    }

    #[test]
    fn icmpv6_decode_from_ipv6_exposes_echo_fields_and_payload() {
        let decoded = Packet::decode_from_l3(NetworkLayer::Ipv6, IPV6_ICMP_FIXTURE).unwrap();
        let ipv6 = decoded.layer::<Ipv6>().unwrap();
        let icmpv6 = decoded.layer::<Icmpv6>().unwrap();
        let raw = decoded.layer::<Raw>().unwrap();

        assert_eq!(ipv6.source(), src());
        assert_eq!(ipv6.destination(), dst());
        assert_eq!(ipv6.flow_label_value(), 0x12345);
        assert_eq!(ipv6.payload_length_value(), Some(23));
        assert_eq!(icmpv6.kind_value(), Some(IcmpKind::EchoRequest));
        assert_eq!(icmpv6.checksum_value(), Some(0x00d0));
        assert_eq!(icmpv6.identifier_value(), Some(0x4242));
        assert_eq!(icmpv6.sequence_number_value(), Some(2));
        assert_eq!(raw.as_bytes(), b"libcrafter-ipv6");
        assert_eq!(decoded.compile().unwrap().as_bytes(), IPV6_ICMP_FIXTURE);
    }

    #[test]
    fn icmpv6_explicit_checksum_is_preserved() {
        let bytes = (Ipv6::new().src(src()).dst(dst())
            / Icmpv6::echo_request().id(7).seq(8).checksum(0x2222)
            / Raw::from("abc"))
        .compile()
        .unwrap();

        assert_eq!(&bytes.as_bytes()[42..44], &[0x22, 0x22]);
    }

    #[test]
    fn icmpv6_decode_rejects_short_inputs() {
        let short = (Ipv6::new().nh(crate::IPPROTO_ICMPV6) / Raw::from_bytes([0u8; 7]))
            .compile()
            .unwrap();
        assert!(Packet::decode_from_l3(NetworkLayer::Ipv6, short.as_bytes()).is_err());
    }

    // The typed-body model classifies echo / error / unknown messages by type,
    // surfacing the type-specific rest-of-header fields per variant.
    #[test]
    fn icmpv6_body_classifies_echo() {
        let body = Icmpv6::echo_request().id(0x4242).seq(2).body();
        assert_eq!(
            body,
            Icmpv6Body::Echo {
                identifier: 0x4242,
                sequence_number: 2,
            }
        );
        assert_eq!(body.label(), "echo");
    }

    #[test]
    fn icmpv6_body_classifies_errors() {
        let du = Icmpv6::destination_unreachable().code(4).body();
        assert_eq!(
            du,
            Icmpv6Body::Error(Icmpv6ErrorBody::DestinationUnreachable)
        );
        assert_eq!(du.label(), "error");
        assert_eq!(
            Icmpv6::packet_too_big().mtu(1280).body(),
            Icmpv6Body::Error(Icmpv6ErrorBody::PacketTooBig { mtu: 1280 })
        );
        assert_eq!(
            Icmpv6::time_exceeded().body(),
            Icmpv6Body::Error(Icmpv6ErrorBody::TimeExceeded)
        );
        assert_eq!(
            Icmpv6::new()
                .icmp_type(ICMPV6_PARAMETER_PROBLEM)
                .pointer(6)
                .body(),
            Icmpv6Body::Error(Icmpv6ErrorBody::ParameterProblem { pointer: 6 })
        );
    }

    // RFC 8335 extended echo request/reply bodies classify by type, surfacing the
    // identifier / 8-bit sequence / flag-byte fields per variant.
    #[test]
    fn icmpv6_body_classifies_extended_echo() {
        let request = Icmpv6::extended_echo_request()
            .id(0x1234)
            .seq(7)
            .extended_l_bit(true)
            .body();
        assert_eq!(
            request,
            Icmpv6Body::ExtendedEchoRequest {
                identifier: 0x1234,
                sequence_number: 7,
                local: true,
                reserved_flags: 0,
            }
        );
        assert_eq!(request.label(), "extended-echo-request");

        let reply = Icmpv6::extended_echo_reply()
            .id(0xabcd)
            .seq(5)
            .extended_state(2)
            .extended_active(true)
            .extended_ipv6(true)
            .body();
        assert_eq!(
            reply,
            Icmpv6Body::ExtendedEchoReply {
                identifier: 0xabcd,
                sequence_number: 5,
                state: 2,
                active: true,
                ipv4: false,
                ipv6: true,
            }
        );
        assert_eq!(reply.label(), "extended-echo-reply");
    }

    // An unrecognized type (here type 200, which the IANA ICMPv6 registry leaves
    // unassigned and no message step models) falls through to the extensible
    // `Unknown` variant with its raw rest-of-header preserved.
    #[test]
    fn icmpv6_body_preserves_unknown_type() {
        let icmpv6 = Icmpv6::new()
            .icmp_type(UNMODELED_ICMPV6_TYPE)
            .rest_of_header([0xde, 0xad, 0xbe, 0xef]);
        let body = icmpv6.body();
        assert_eq!(
            body,
            Icmpv6Body::Unknown {
                icmp_type: UNMODELED_ICMPV6_TYPE,
                rest_of_header: [0xde, 0xad, 0xbe, 0xef],
            }
        );
        assert_eq!(body.label(), "unknown");
    }

    // The body model is a view over the header: it does not change the existing
    // echo/error summary string (other tests and the oracle bind to it), and the
    // body variant is surfaced through `show()`/inspection fields instead.
    #[test]
    fn icmpv6_summary_is_stable_and_show_reports_body() {
        let echo = Icmpv6::echo_request().id(0x4242).seq(2);
        assert_eq!(
            echo.summary(),
            "Icmpv6(type=echo-request(128), code=0, id=16962, seq=2)"
        );
        let echo_body = echo
            .inspection_fields()
            .into_iter()
            .find(|(name, _)| *name == "body")
            .map(|(_, value)| value)
            .expect("show() exposes a body field");
        assert_eq!(echo_body, "echo(id=0x4242, seq=2)");

        let ptb = Icmpv6::packet_too_big().mtu(1280);
        assert_eq!(
            ptb.summary(),
            "Icmpv6(type=packet-too-big(2), code=0, id=-, seq=-)"
        );
        let ptb_body = ptb
            .inspection_fields()
            .into_iter()
            .find(|(name, _)| *name == "body")
            .map(|(_, value)| value)
            .expect("show() exposes a body field");
        assert_eq!(ptb_body, "error(packet-too-big, mtu=1280)");

        let unknown = Icmpv6::new().icmp_type(ICMPV6_PACKET_TOO_BIG ^ 0xff);
        let unknown_body = unknown
            .inspection_fields()
            .into_iter()
            .find(|(name, _)| *name == "body")
            .map(|(_, value)| value)
            .expect("show() exposes a body field");
        assert!(unknown_body.starts_with("unknown(type="));
    }
}