crafter 0.3.1

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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
//! IEEE 802.11 MAC layer.

use core::any::Any;
use core::ops::Div;

use crate::error::{CrafterError, Result};
use crate::field::Field;
use crate::mac::MacAddr;
use crate::packet::{IntoPacket, Layer, LayerContext, Packet};
use crate::protocols::rsn::RsnInformation;

use super::header::*;
use super::util::{dot11_hex_bytes, value_or_copy};
use super::*;

/// IEEE 802.11 MAC layer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dot11 {
    pub(super) frame_control: Field<Dot11FrameControl>,
    pub(super) duration_id: Field<u16>,
    pub(super) addr1: Field<MacAddr>,
    pub(super) addr2: Field<MacAddr>,
    pub(super) addr3: Field<MacAddr>,
    pub(super) sequence_control: Field<Dot11SequenceControl>,
    pub(super) addr4: Field<MacAddr>,
    pub(super) qos_control: Field<u16>,
    pub(super) ht_control: Field<u32>,
    pub(super) fixed_parameters: Vec<u8>,
    pub(super) tagged_parameters: Vec<Dot11TaggedParameter>,
    pub(super) encrypted_body_len: Option<usize>,
}

impl Dot11 {
    /// Create a three-address data MAC header with deterministic safe defaults.
    pub fn new() -> Self {
        Self {
            frame_control: Field::defaulted(
                Dot11FrameControl::new()
                    .with_frame_type(DOT11_FRAME_TYPE_DATA)
                    .with_subtype(DOT11_DATA_SUBTYPE_DATA),
            ),
            duration_id: Field::defaulted(0),
            addr1: Field::defaulted(MacAddr::BROADCAST),
            addr2: Field::defaulted(MacAddr::ZERO),
            addr3: Field::defaulted(MacAddr::ZERO),
            sequence_control: Field::defaulted(Dot11SequenceControl::new()),
            addr4: Field::unset(),
            qos_control: Field::unset(),
            ht_control: Field::unset(),
            fixed_parameters: Vec::new(),
            tagged_parameters: Vec::new(),
            encrypted_body_len: None,
        }
    }

    /// Create a management frame with the given subtype.
    pub fn management(subtype: Dot11ManagementSubtype) -> Self {
        Self::new().with_default_frame_control(DOT11_FRAME_TYPE_MANAGEMENT, subtype.raw())
    }

    /// Create a control frame with the given subtype.
    pub fn control(subtype: Dot11ControlSubtype) -> Self {
        Self::new().with_default_frame_control(DOT11_FRAME_TYPE_CONTROL, subtype.raw())
    }

    /// Create a data frame.
    pub fn data() -> Self {
        Self::data_with_subtype(Dot11DataSubtype::Data)
    }

    /// Create a QoS data frame with a zero QoS control field.
    pub fn qos_data() -> Self {
        Self::data_with_subtype(Dot11DataSubtype::QosData).with_default_qos_control(0)
    }

    /// Create a beacon management frame.
    pub fn beacon() -> Self {
        Self::management(Dot11ManagementSubtype::Beacon)
            .with_default_fixed_parameters(DOT11_MGMT_BEACON_FIXED_LEN)
    }

    /// Create a probe request management frame.
    pub fn probe_request() -> Self {
        Self::management(Dot11ManagementSubtype::ProbeRequest)
    }

    /// Create a probe response management frame.
    pub fn probe_response() -> Self {
        Self::management(Dot11ManagementSubtype::ProbeResponse)
            .with_default_fixed_parameters(DOT11_MGMT_PROBE_RESPONSE_FIXED_LEN)
    }

    /// Create an association request management frame.
    pub fn association_request() -> Self {
        Self::management(Dot11ManagementSubtype::AssociationRequest)
            .with_default_fixed_parameters(DOT11_MGMT_ASSOCIATION_REQUEST_FIXED_LEN)
    }

    /// Create an association response management frame.
    pub fn association_response() -> Self {
        Self::management(Dot11ManagementSubtype::AssociationResponse)
            .with_default_fixed_parameters(DOT11_MGMT_ASSOCIATION_RESPONSE_FIXED_LEN)
    }

    /// Create a reassociation request management frame.
    pub fn reassociation_request() -> Self {
        Self::management(Dot11ManagementSubtype::ReassociationRequest)
            .with_default_fixed_parameters(DOT11_MGMT_REASSOCIATION_REQUEST_FIXED_LEN)
    }

    /// Create a reassociation response management frame.
    pub fn reassociation_response() -> Self {
        Self::management(Dot11ManagementSubtype::ReassociationResponse)
            .with_default_fixed_parameters(DOT11_MGMT_REASSOCIATION_RESPONSE_FIXED_LEN)
    }

    /// Create an authentication management frame.
    pub fn authentication() -> Self {
        Self::management(Dot11ManagementSubtype::Authentication)
            .with_default_fixed_parameters(DOT11_MGMT_AUTHENTICATION_FIXED_LEN)
    }

    /// Create a deauthentication management frame.
    pub fn deauthentication() -> Self {
        Self::management(Dot11ManagementSubtype::Deauthentication)
            .with_default_fixed_parameters(DOT11_MGMT_DEAUTHENTICATION_FIXED_LEN)
    }

    /// Create a disassociation management frame.
    pub fn disassociation() -> Self {
        Self::management(Dot11ManagementSubtype::Disassociation)
            .with_default_fixed_parameters(DOT11_MGMT_DISASSOCIATION_FIXED_LEN)
    }

    /// Create an action management frame with a zero category field.
    pub fn action() -> Self {
        Self::management(Dot11ManagementSubtype::Action)
            .with_default_fixed_parameters(DOT11_MGMT_ACTION_FIXED_LEN)
    }

    /// Create an action-no-ack management frame with a zero category field.
    pub fn action_no_ack() -> Self {
        Self::management(Dot11ManagementSubtype::ActionNoAck)
            .with_default_fixed_parameters(DOT11_MGMT_ACTION_FIXED_LEN)
    }

    /// Create an ACK control frame.
    pub fn ack() -> Self {
        Self::control(Dot11ControlSubtype::Ack)
    }

    /// Create an RTS control frame.
    pub fn rts() -> Self {
        Self::control(Dot11ControlSubtype::Rts)
    }

    /// Create a CTS control frame.
    pub fn cts() -> Self {
        Self::control(Dot11ControlSubtype::Cts)
    }

    /// Create a Block Ack control frame.
    pub fn block_ack() -> Self {
        Self::control(Dot11ControlSubtype::BlockAck)
    }

    /// Create a Block Ack Request control frame.
    pub fn block_ack_request() -> Self {
        Self::control(Dot11ControlSubtype::BlockAckRequest)
    }

    /// Create a PS-Poll control frame.
    pub fn ps_poll() -> Self {
        Self::control(Dot11ControlSubtype::PsPoll)
    }

    /// Create a CF-End control frame.
    pub fn cf_end() -> Self {
        Self::control(Dot11ControlSubtype::CfEnd)
    }

    /// Create a CF-End + CF-Ack control frame.
    pub fn cf_end_cf_ack() -> Self {
        Self::control(Dot11ControlSubtype::CfEndCfAck)
    }

    /// Set the frame-control field.
    pub fn frame_control(mut self, frame_control: Dot11FrameControl) -> Self {
        self.frame_control.set_user(frame_control);
        self
    }

    /// Set or clear the More Fragments frame-control bit.
    pub fn more_fragments(mut self, enabled: bool) -> Self {
        let frame_control = self.frame_control_value().with_more_fragments(enabled);
        self.frame_control.set_user(frame_control);
        self
    }

    /// Set the duration/ID field.
    pub fn duration_id(mut self, duration_id: u16) -> Self {
        self.duration_id.set_user(duration_id);
        self
    }

    /// Set address 1.
    pub fn addr1(mut self, addr: impl Into<MacAddr>) -> Self {
        self.addr1.set_user(addr.into());
        self
    }

    /// Set address 2.
    pub fn addr2(mut self, addr: impl Into<MacAddr>) -> Self {
        self.addr2.set_user(addr.into());
        self
    }

    /// Set address 3.
    pub fn addr3(mut self, addr: impl Into<MacAddr>) -> Self {
        self.addr3.set_user(addr.into());
        self
    }

    /// Set address 4.
    pub fn addr4(mut self, addr: impl Into<MacAddr>) -> Self {
        self.addr4.set_user(addr.into());
        self
    }

    /// Set the sequence-control field.
    pub fn sequence_control(mut self, sequence_control: Dot11SequenceControl) -> Self {
        self.sequence_control.set_user(sequence_control);
        self
    }

    /// Set the twelve-bit sequence number in the sequence-control field.
    pub fn sequence_number(mut self, sequence_number: u16) -> Self {
        let sequence_control = self
            .effective_sequence_control()
            .with_sequence_number(sequence_number);
        self.sequence_control.set_user(sequence_control);
        self
    }

    /// Set the four-bit fragment number in the sequence-control field.
    pub fn fragment_number(mut self, fragment_number: u8) -> Self {
        let sequence_control = self
            .effective_sequence_control()
            .with_fragment_number(fragment_number);
        self.sequence_control.set_user(sequence_control);
        self
    }

    /// Set the QoS control field.
    pub fn qos_control(mut self, qos_control: u16) -> Self {
        self.qos_control.set_user(qos_control);
        self
    }

    /// Set the QoS control field from typed subfields.
    pub fn with_qos_control_fields(mut self, qos_control: Dot11QosControl) -> Self {
        self.qos_control.set_user(qos_control.bits());
        self
    }

    /// Set the HT Control field.
    pub fn ht_control(mut self, ht_control: u32) -> Self {
        self.ht_control.set_user(ht_control);
        self
    }

    /// Set raw fixed management-field bytes to emit after the MAC header.
    pub fn fixed_parameters(mut self, fixed_parameters: impl Into<Vec<u8>>) -> Self {
        self.fixed_parameters = fixed_parameters.into();
        self
    }

    /// Set Beacon fixed fields without changing the frame subtype.
    pub fn with_beacon_fixed_fields(mut self, fixed_fields: Dot11BeaconFixedFields) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Probe Response fixed fields without changing the frame subtype.
    pub fn with_probe_response_fixed_fields(
        mut self,
        fixed_fields: Dot11BeaconFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Association Request fixed fields without changing the frame subtype.
    pub fn with_association_request_fixed_fields(
        mut self,
        fixed_fields: Dot11AssociationRequestFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Association Response fixed fields without changing the frame subtype.
    pub fn with_association_response_fixed_fields(
        mut self,
        fixed_fields: Dot11AssociationResponseFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Reassociation Request fixed fields without changing the frame subtype.
    pub fn with_reassociation_request_fixed_fields(
        mut self,
        fixed_fields: Dot11ReassociationRequestFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Reassociation Response fixed fields without changing the frame subtype.
    pub fn with_reassociation_response_fixed_fields(
        mut self,
        fixed_fields: Dot11AssociationResponseFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Authentication fixed fields without changing the frame subtype.
    pub fn with_authentication_fixed_fields(
        mut self,
        fixed_fields: Dot11AuthenticationFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Deauthentication fixed fields without changing the frame subtype.
    pub fn with_deauthentication_fixed_fields(
        mut self,
        fixed_fields: Dot11ReasonCodeFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Disassociation fixed fields without changing the frame subtype.
    pub fn with_disassociation_fixed_fields(
        mut self,
        fixed_fields: Dot11ReasonCodeFixedFields,
    ) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Set Action fixed fields without changing the frame subtype.
    pub fn with_action_fixed_fields(mut self, fixed_fields: Dot11ActionFixedFields) -> Self {
        self.fixed_parameters = fixed_fields.to_bytes().to_vec();
        self
    }

    /// Append a raw tagged parameter.
    pub fn tag(mut self, tag: Dot11TaggedParameter) -> Self {
        self.tagged_parameters.push(tag);
        self
    }

    /// Replace raw tagged parameters.
    pub fn tags(mut self, tags: impl Into<Vec<Dot11TaggedParameter>>) -> Self {
        self.tagged_parameters = tags.into();
        self
    }

    /// Append an SSID tagged parameter.
    pub fn ssid(self, ssid: impl Into<Vec<u8>>) -> Self {
        self.tag(Dot11TaggedParameter::ssid(ssid))
    }

    /// Append a Supported Rates tagged parameter.
    pub fn supported_rates(self, rates: impl Into<Vec<u8>>) -> Self {
        self.tag(Dot11TaggedParameter::supported_rates(rates))
    }

    /// Append a DS Parameter Set tagged parameter with the current channel.
    pub fn ds_parameter_set(self, current_channel: u8) -> Self {
        self.tag(Dot11TaggedParameter::ds_parameter_set(current_channel))
    }

    /// Append a raw TIM tagged parameter.
    pub fn tim(self, data: impl Into<Vec<u8>>) -> Self {
        self.tag(Dot11TaggedParameter::tim(data))
    }

    /// Append a raw RSN tagged parameter.
    pub fn rsn(self, data: impl Into<Vec<u8>>) -> Self {
        self.tag(Dot11TaggedParameter::rsn(data))
    }

    /// Append an RSN tagged parameter from typed RSN information.
    pub fn with_rsn_information(self, rsn: &RsnInformation) -> Result<Self> {
        Ok(self.tag(Dot11TaggedParameter::from_rsn_information(rsn)?))
    }

    /// Current frame-control value.
    pub fn frame_control_value(&self) -> Dot11FrameControl {
        value_or_copy(&self.frame_control, Dot11::default_frame_control())
    }

    /// Current typed frame type.
    pub fn frame_type(&self) -> Dot11FrameType {
        self.frame_control_value().frame_type_value()
    }

    /// Current typed management subtype, when applicable.
    pub fn management_subtype(&self) -> Option<Dot11ManagementSubtype> {
        self.frame_control_value().management_subtype_value()
    }

    /// Current typed control subtype, when applicable.
    pub fn control_subtype(&self) -> Option<Dot11ControlSubtype> {
        self.frame_control_value().control_subtype_value()
    }

    /// Current typed data subtype, when applicable.
    pub fn data_subtype(&self) -> Option<Dot11DataSubtype> {
        self.frame_control_value().data_subtype_value()
    }

    /// Current duration/ID field value, if present.
    pub fn duration_id_value(&self) -> Option<u16> {
        self.duration_id.value().copied()
    }

    /// Current address 1 value, if present.
    pub fn addr1_value(&self) -> Option<MacAddr> {
        self.addr1.value().copied()
    }

    /// Current address 2 value, if present.
    pub fn addr2_value(&self) -> Option<MacAddr> {
        self.addr2.value().copied()
    }

    /// Current address 3 value, if present.
    pub fn addr3_value(&self) -> Option<MacAddr> {
        self.addr3.value().copied()
    }

    /// Current address 4 value, if present.
    pub fn addr4_value(&self) -> Option<MacAddr> {
        self.addr4.value().copied()
    }

    /// Receiver address for data, management, or supported control frames.
    pub fn receiver(&self) -> Option<MacAddr> {
        match self.frame_type() {
            Dot11FrameType::Data | Dot11FrameType::Management => self.addr1_value(),
            Dot11FrameType::Control if dot11_control_subtype_has_known_address_layout(self) => {
                self.addr1_value()
            }
            _ => None,
        }
    }

    /// Transmitter address for data, management, or supported two-address control frames.
    pub fn transmitter(&self) -> Option<MacAddr> {
        match self.frame_type() {
            Dot11FrameType::Data | Dot11FrameType::Management => self.addr2_value(),
            Dot11FrameType::Control if dot11_control_subtype_has_addr2(self.control_subtype()) => {
                self.addr2_value()
            }
            _ => None,
        }
    }

    /// Destination address for data or management frames.
    pub fn destination(&self) -> Option<MacAddr> {
        let frame_control = self.frame_control_value();

        match frame_control.frame_type_value() {
            Dot11FrameType::Management => self.addr1_value(),
            Dot11FrameType::Data if frame_control.to_ds() && frame_control.from_ds() => {
                self.addr3_value()
            }
            Dot11FrameType::Data if frame_control.to_ds() => self.addr3_value(),
            Dot11FrameType::Data => self.addr1_value(),
            _ => None,
        }
    }

    /// Source address for data or management frames.
    pub fn source(&self) -> Option<MacAddr> {
        let frame_control = self.frame_control_value();

        match frame_control.frame_type_value() {
            Dot11FrameType::Management => self.addr2_value(),
            Dot11FrameType::Data if frame_control.to_ds() && frame_control.from_ds() => {
                self.fourth_address()
            }
            Dot11FrameType::Data if frame_control.from_ds() => self.addr3_value(),
            Dot11FrameType::Data => self.addr2_value(),
            _ => None,
        }
    }

    /// BSSID address for data or management frames when the role is defined.
    pub fn bssid(&self) -> Option<MacAddr> {
        let frame_control = self.frame_control_value();

        match frame_control.frame_type_value() {
            Dot11FrameType::Management => self.addr3_value(),
            Dot11FrameType::Control => match frame_control.control_subtype_value() {
                Some(Dot11ControlSubtype::PsPoll) => self.addr1_value(),
                Some(Dot11ControlSubtype::CfEnd | Dot11ControlSubtype::CfEndCfAck) => {
                    self.addr2_value()
                }
                _ => None,
            },
            Dot11FrameType::Data if frame_control.to_ds() && frame_control.from_ds() => None,
            Dot11FrameType::Data if frame_control.to_ds() => self.addr1_value(),
            Dot11FrameType::Data if frame_control.from_ds() => self.addr2_value(),
            Dot11FrameType::Data => self.addr3_value(),
            _ => None,
        }
    }

    /// Fourth address for four-address data frames when present.
    pub fn fourth_address(&self) -> Option<MacAddr> {
        let frame_control = self.frame_control_value();

        if frame_control.frame_type_value() == Dot11FrameType::Data
            && frame_control.to_ds()
            && frame_control.from_ds()
        {
            self.addr4_value()
        } else {
            None
        }
    }

    /// Current sequence-control field value, if present.
    pub fn sequence_control_value(&self) -> Option<Dot11SequenceControl> {
        self.sequence_control.value().copied()
    }

    /// Current sequence-number subfield value, if the frame has sequence control.
    pub fn sequence_number_value(&self) -> Option<u16> {
        self.sequence_control_value()
            .map(|sequence_control| sequence_control.sequence_number())
    }

    /// Current fragment-number subfield value, if the frame has sequence control.
    pub fn fragment_number_value(&self) -> Option<u8> {
        self.sequence_control_value()
            .map(|sequence_control| sequence_control.fragment_number())
    }

    /// Current QoS control field value, if present.
    pub fn qos_control_value(&self) -> Option<u16> {
        self.qos_control.value().copied()
    }

    /// Current typed QoS control field value, if present.
    pub fn qos_control_fields(&self) -> Option<Dot11QosControl> {
        self.qos_control_value().map(Dot11QosControl::from_bits)
    }

    /// Current HT Control field value, if present.
    pub fn ht_control_value(&self) -> Option<u32> {
        self.ht_control.value().copied()
    }

    /// Raw fixed management-field bytes.
    pub fn fixed_parameters_value(&self) -> &[u8] {
        &self.fixed_parameters
    }

    /// Raw fixed management-field bytes, including unsupported or malformed shapes.
    pub fn raw_fixed_parameters(&self) -> &[u8] {
        &self.fixed_parameters
    }

    /// Typed management fixed fields, or raw bytes when the shape is unsupported.
    pub fn management_fixed_fields(&self) -> Dot11ManagementFixedFields<'_> {
        match self.management_subtype() {
            Some(Dot11ManagementSubtype::Beacon) => self
                .beacon_fixed_fields()
                .map(Dot11ManagementFixedFields::Beacon)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::ProbeResponse) => self
                .probe_response_fixed_fields()
                .map(Dot11ManagementFixedFields::ProbeResponse)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::AssociationRequest) => self
                .association_request_fixed_fields()
                .map(Dot11ManagementFixedFields::AssociationRequest)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::AssociationResponse) => self
                .association_response_fixed_fields()
                .map(Dot11ManagementFixedFields::AssociationResponse)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::ReassociationRequest) => self
                .reassociation_request_fixed_fields()
                .map(Dot11ManagementFixedFields::ReassociationRequest)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::ReassociationResponse) => self
                .reassociation_response_fixed_fields()
                .map(Dot11ManagementFixedFields::ReassociationResponse)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::Authentication) => self
                .authentication_fixed_fields()
                .map(Dot11ManagementFixedFields::Authentication)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::Deauthentication) => self
                .deauthentication_fixed_fields()
                .map(Dot11ManagementFixedFields::Deauthentication)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::Disassociation) => self
                .disassociation_fixed_fields()
                .map(Dot11ManagementFixedFields::Disassociation)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            Some(Dot11ManagementSubtype::Action | Dot11ManagementSubtype::ActionNoAck) => self
                .action_fixed_fields()
                .map(Dot11ManagementFixedFields::Action)
                .unwrap_or(Dot11ManagementFixedFields::Raw(&self.fixed_parameters)),
            _ => Dot11ManagementFixedFields::Raw(&self.fixed_parameters),
        }
    }

    /// Beacon fixed fields when this frame has the supported Beacon shape.
    pub fn beacon_fixed_fields(&self) -> Option<Dot11BeaconFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::Beacon) {
            Dot11BeaconFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Probe Response fixed fields when this frame has the supported shape.
    pub fn probe_response_fixed_fields(&self) -> Option<Dot11BeaconFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::ProbeResponse) {
            Dot11BeaconFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Association Request fixed fields when this frame has the supported shape.
    pub fn association_request_fixed_fields(&self) -> Option<Dot11AssociationRequestFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::AssociationRequest) {
            Dot11AssociationRequestFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Association Response fixed fields when this frame has the supported shape.
    pub fn association_response_fixed_fields(&self) -> Option<Dot11AssociationResponseFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::AssociationResponse) {
            Dot11AssociationResponseFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Reassociation Request fixed fields when this frame has the supported shape.
    pub fn reassociation_request_fixed_fields(
        &self,
    ) -> Option<Dot11ReassociationRequestFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::ReassociationRequest) {
            Dot11ReassociationRequestFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Reassociation Response fixed fields when this frame has the supported shape.
    pub fn reassociation_response_fixed_fields(
        &self,
    ) -> Option<Dot11AssociationResponseFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::ReassociationResponse) {
            Dot11AssociationResponseFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Authentication fixed fields when this frame has the supported shape.
    pub fn authentication_fixed_fields(&self) -> Option<Dot11AuthenticationFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::Authentication) {
            Dot11AuthenticationFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Deauthentication fixed fields when this frame has the supported shape.
    pub fn deauthentication_fixed_fields(&self) -> Option<Dot11ReasonCodeFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::Deauthentication) {
            Dot11ReasonCodeFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Disassociation fixed fields when this frame has the supported shape.
    pub fn disassociation_fixed_fields(&self) -> Option<Dot11ReasonCodeFixedFields> {
        if self.management_subtype() == Some(Dot11ManagementSubtype::Disassociation) {
            Dot11ReasonCodeFixedFields::from_bytes(&self.fixed_parameters)
        } else {
            None
        }
    }

    /// Action fixed fields when this frame has the supported Action shape.
    pub fn action_fixed_fields(&self) -> Option<Dot11ActionFixedFields> {
        match self.management_subtype() {
            Some(Dot11ManagementSubtype::Action | Dot11ManagementSubtype::ActionNoAck) => {
                Dot11ActionFixedFields::from_bytes(&self.fixed_parameters)
            }
            _ => None,
        }
    }

    /// Raw tagged parameters.
    pub fn tags_value(&self) -> &[Dot11TaggedParameter] {
        &self.tagged_parameters
    }

    /// Raw tagged parameters.
    pub fn tagged_parameters(&self) -> &[Dot11TaggedParameter] {
        &self.tagged_parameters
    }

    /// First RSN information element carried by the raw tagged parameters.
    pub fn rsn_information(&self) -> Option<Result<RsnInformation>> {
        self.tagged_parameters
            .iter()
            .find_map(Dot11TaggedParameter::rsn_information)
    }

    /// RSN information elements carried by the raw tagged parameters.
    pub fn rsn_information_elements(&self) -> impl Iterator<Item = Result<RsnInformation>> + '_ {
        self.tagged_parameters
            .iter()
            .filter_map(Dot11TaggedParameter::rsn_information)
    }

    /// Return true when the Protected Frame bit is set.
    pub fn is_protected(&self) -> bool {
        self.frame_control_value().protected()
    }

    /// Return true when the More Fragments frame-control bit is set.
    pub fn has_more_fragments(&self) -> bool {
        self.frame_control_value().more_fragments()
    }

    /// Return true when this frame carries 802.11 fragmentation metadata.
    ///
    /// `crafter` exposes this metadata for inspection but does not reassemble
    /// fragmented 802.11 payloads.
    pub fn is_fragmented(&self) -> bool {
        self.has_more_fragments()
            || self
                .fragment_number_value()
                .is_some_and(|number| number != 0)
    }

    /// Decoded encrypted body length for protected data frames.
    pub fn encrypted_body_len(&self) -> Option<usize> {
        self.encrypted_body_len
    }

    /// Minimum header length implied by this frame's type/subtype and flags.
    ///
    /// This is the source-backed decode boundary: MAC header fields, optional
    /// address four, QoS control, HT Control when signaled by supported
    /// +HTC/Order shapes, and selected management fixed fields. It does not
    /// include tagged parameters or higher-layer payload bytes.
    pub fn minimum_header_len(&self) -> usize {
        dot11_required_header_len(self.frame_control_value())
    }

    /// Minimum header length implied by a frame-control word.
    pub fn minimum_header_len_for(frame_control: Dot11FrameControl) -> usize {
        dot11_required_header_len(frame_control)
    }

    /// Read frame control from `bytes`, compute the minimum header length, and
    /// return a structured error when the buffer is truncated before that
    /// boundary.
    pub fn header_len_from_bytes(bytes: impl AsRef<[u8]>) -> Result<usize> {
        let bytes = bytes.as_ref();
        let frame_control = Dot11FrameControl::decode(bytes)?;
        let header_len = dot11_required_header_len(frame_control);

        if bytes.len() < header_len {
            return Err(CrafterError::buffer_too_short(
                "dot11.header",
                header_len,
                bytes.len(),
            ));
        }

        Ok(header_len)
    }

    fn default_frame_control() -> Dot11FrameControl {
        Dot11FrameControl::new()
            .with_frame_type(DOT11_FRAME_TYPE_DATA)
            .with_subtype(DOT11_DATA_SUBTYPE_DATA)
    }

    fn with_default_frame_control(mut self, frame_type: u8, subtype: u8) -> Self {
        self.frame_control = Field::defaulted(
            Dot11FrameControl::new()
                .with_frame_type(frame_type)
                .with_subtype(subtype),
        );
        self
    }

    fn data_with_subtype(subtype: Dot11DataSubtype) -> Self {
        Self::new().with_default_frame_control(DOT11_FRAME_TYPE_DATA, subtype.raw())
    }

    fn with_default_fixed_parameters(mut self, len: usize) -> Self {
        self.fixed_parameters = vec![0; len];
        self
    }

    fn with_default_qos_control(mut self, qos_control: u16) -> Self {
        self.qos_control = Field::defaulted(qos_control);
        self
    }

    fn effective_duration_id(&self) -> u16 {
        value_or_copy(&self.duration_id, 0)
    }

    fn effective_addr1(&self) -> MacAddr {
        value_or_copy(&self.addr1, MacAddr::BROADCAST)
    }

    fn effective_addr2(&self) -> MacAddr {
        value_or_copy(&self.addr2, MacAddr::ZERO)
    }

    fn effective_addr3(&self) -> MacAddr {
        value_or_copy(&self.addr3, MacAddr::ZERO)
    }

    fn effective_sequence_control(&self) -> Dot11SequenceControl {
        value_or_copy(&self.sequence_control, Dot11SequenceControl::new())
    }
}

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

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

    fn summary(&self) -> String {
        let frame_control = self.frame_control_value();
        let mut fields = vec![
            format!(
                "type={}",
                dot11_frame_type_label(frame_control.frame_type())
            ),
            format!(
                "subtype={}",
                dot11_subtype_label(frame_control.frame_type(), frame_control.subtype())
            ),
        ];

        if let Some(source) = self.source() {
            fields.push(format!("src={source}"));
        }
        if let Some(destination) = self.destination() {
            fields.push(format!("dst={destination}"));
        }
        if let Some(bssid) = self.bssid() {
            fields.push(format!("bssid={bssid}"));
        }

        fields.push(format!("protected={}", frame_control.protected()));
        if let Some(encrypted_body_len) = self.encrypted_body_len() {
            fields.push(format!("encrypted_body_len={encrypted_body_len}"));
        }

        if let Some(sequence_control) = self.sequence_control_value() {
            fields.push(format!("seq={}", sequence_control.sequence_number()));
            fields.push(format!("frag={}", sequence_control.fragment_number()));
        }
        if let Some(qos_control) = self.qos_control_value() {
            fields.push(format!("qos=0x{qos_control:04x}"));
        }

        format!("Dot11({})", fields.join(", "))
    }

    fn inspection_fields(&self) -> Vec<(&'static str, String)> {
        let frame_control = self.frame_control_value();
        let mut fields = Vec::new();

        fields.extend([
            (
                "frame_control",
                format!(
                    "0x{:04x} ({}/{})",
                    frame_control.bits(),
                    dot11_frame_type_label(frame_control.frame_type()),
                    dot11_subtype_label(frame_control.frame_type(), frame_control.subtype())
                ),
            ),
            ("type", dot11_frame_type_label(frame_control.frame_type())),
            (
                "subtype",
                dot11_subtype_label(frame_control.frame_type(), frame_control.subtype()),
            ),
            ("duration_id", self.effective_duration_id().to_string()),
            ("protected", frame_control.protected().to_string()),
        ]);
        if let Some(encrypted_body_len) = self.encrypted_body_len() {
            fields.push(("encrypted_body_len", encrypted_body_len.to_string()));
        }

        if let Some(addr1) = self.addr1_value() {
            fields.push(("addr1", addr1.to_string()));
        }
        if let Some(addr2) = self.addr2_value() {
            fields.push(("addr2", addr2.to_string()));
        }
        if let Some(addr3) = self.addr3_value() {
            fields.push(("addr3", addr3.to_string()));
        }

        if let Some(addr4) = self.addr4_value() {
            fields.push(("addr4", addr4.to_string()));
        }
        if let Some(source) = self.source() {
            fields.push(("src", source.to_string()));
        }
        if let Some(destination) = self.destination() {
            fields.push(("dst", destination.to_string()));
        }
        if let Some(bssid) = self.bssid() {
            fields.push(("bssid", bssid.to_string()));
        }
        if let Some(sequence_control) = self.sequence_control_value() {
            fields.push((
                "sequence_control",
                format!("0x{:04x}", sequence_control.bits()),
            ));
            fields.push((
                "sequence_number",
                sequence_control.sequence_number().to_string(),
            ));
            fields.push((
                "fragment_number",
                sequence_control.fragment_number().to_string(),
            ));
        }
        if let Some(qos_control) = self.qos_control_value() {
            fields.push(("qos", "present".to_string()));
            fields.push(("qos_control", format!("0x{qos_control:04x}")));
            let qos = Dot11QosControl::from_bits(qos_control);
            fields.push(("qos_tid", qos.tid().to_string()));
            fields.push(("qos_eosp", qos.eosp().to_string()));
            fields.push(("qos_ack_policy", qos.ack_policy().to_string()));
            fields.push(("qos_a_msdu_present", qos.a_msdu_present().to_string()));
            fields.push(("qos_txop_queue_size", qos.txop_queue_size().to_string()));
        }
        if let Some(ht_control) = self.ht_control_value() {
            fields.push(("ht_control", format!("0x{ht_control:08x}")));
        }
        if !self.fixed_parameters.is_empty() {
            fields.push(("fixed_parameters", dot11_hex_bytes(&self.fixed_parameters)));
        }
        match self.management_fixed_fields() {
            Dot11ManagementFixedFields::Beacon(fixed)
            | Dot11ManagementFixedFields::ProbeResponse(fixed) => {
                fields.push(("timestamp", fixed.timestamp().to_string()));
                fields.push(("beacon_interval", fixed.beacon_interval().to_string()));
                fields.push((
                    "capability_information",
                    format!("0x{:04x}", fixed.capability_information()),
                ));
            }
            Dot11ManagementFixedFields::AssociationRequest(fixed) => {
                fields.push((
                    "capability_information",
                    format!("0x{:04x}", fixed.capability_information()),
                ));
                fields.push(("listen_interval", fixed.listen_interval().to_string()));
            }
            Dot11ManagementFixedFields::AssociationResponse(fixed)
            | Dot11ManagementFixedFields::ReassociationResponse(fixed) => {
                fields.push((
                    "capability_information",
                    format!("0x{:04x}", fixed.capability_information()),
                ));
                fields.push(("status_code", fixed.status_code().to_string()));
                fields.push(("association_id", fixed.association_id().to_string()));
            }
            Dot11ManagementFixedFields::ReassociationRequest(fixed) => {
                fields.push((
                    "capability_information",
                    format!("0x{:04x}", fixed.capability_information()),
                ));
                fields.push(("listen_interval", fixed.listen_interval().to_string()));
                fields.push(("current_ap_address", fixed.current_ap_address().to_string()));
            }
            Dot11ManagementFixedFields::Authentication(fixed) => {
                fields.push(("algorithm_number", fixed.algorithm_number().to_string()));
                fields.push((
                    "transaction_sequence_number",
                    fixed.transaction_sequence_number().to_string(),
                ));
                fields.push(("status_code", fixed.status_code().to_string()));
            }
            Dot11ManagementFixedFields::Deauthentication(fixed)
            | Dot11ManagementFixedFields::Disassociation(fixed) => {
                fields.push(("reason_code", fixed.reason_code().to_string()));
            }
            Dot11ManagementFixedFields::Action(fixed) => {
                fields.push(("category", fixed.category().to_string()));
                fields.push(("category_label", dot11_category_label(fixed.category())));
            }
            Dot11ManagementFixedFields::Raw(_) => {}
        }
        if !self.tagged_parameters.is_empty() {
            fields.push((
                "tagged_parameters",
                self.tagged_parameters.len().to_string(),
            ));
        }

        fields
    }

    fn encoded_len(&self) -> usize {
        dot11_encoded_mac_header_len(self)
            + self.fixed_parameters.len()
            + self
                .tagged_parameters
                .iter()
                .map(Dot11TaggedParameter::encoded_len)
                .sum::<usize>()
    }

    fn compile(&self, _ctx: &LayerContext<'_>, out: &mut Vec<u8>) -> Result<()> {
        out.extend_from_slice(&self.frame_control_value().compile());
        out.extend_from_slice(&self.effective_duration_id().to_le_bytes());
        out.extend_from_slice(&self.effective_addr1().octets());

        let frame_control = self.frame_control_value();
        if frame_control.frame_type_value() == Dot11FrameType::Control {
            if dot11_control_emit_addr2(self) {
                out.extend_from_slice(&self.effective_addr2().octets());
            }
            if dot11_control_emit_addr3(self) {
                out.extend_from_slice(&self.effective_addr3().octets());
            }
            if dot11_control_emit_sequence_control(self) {
                out.extend_from_slice(&self.effective_sequence_control().compile());
            }
        } else {
            out.extend_from_slice(&self.effective_addr2().octets());
            out.extend_from_slice(&self.effective_addr3().octets());
            out.extend_from_slice(&self.effective_sequence_control().compile());
        }

        if let Some(addr4) = self.addr4_value() {
            out.extend_from_slice(&addr4.octets());
        }
        if let Some(qos_control) = self.qos_control_value() {
            out.extend_from_slice(&qos_control.to_le_bytes());
        }
        if let Some(ht_control) = self.ht_control_value() {
            out.extend_from_slice(&ht_control.to_le_bytes());
        }

        out.extend_from_slice(&self.fixed_parameters);
        for tag in &self.tagged_parameters {
            tag.compile(out)?;
        }

        Ok(())
    }

    fn clone_layer(&self) -> Box<dyn Layer> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

impl<R> Div<R> for Dot11
where
    R: IntoPacket,
{
    type Output = Packet;

    fn div(self, rhs: R) -> Self::Output {
        Packet::from_layer(self).concat(rhs)
    }
}