wlan_common/
sequence.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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use ieee80211::MacAddr;
use std::collections::HashMap;

const SEQ_START_NUM: SequenceNum = 1;
pub type SequenceNum = u32;

/// IEEE Std 802.11-2016, 10.3.2.11.2, 10.3.2.11.3
/// A specific Sequence Number Space such as SNS1, SNS2, etc.
/// A STA owns multiple of such SNS maps, each of which holds
/// sequence numbers for its peers.
struct SnsMap<K> {
    inner: HashMap<K, SequenceNum>,
    modulo_divisor: SequenceNum,
}

impl<K: std::hash::Hash + Eq + Clone> SnsMap<K> {
    pub fn new(modulo_divisor: SequenceNum) -> Self {
        Self { inner: HashMap::new(), modulo_divisor }
    }

    pub fn next(&mut self, key: &K) -> SequenceNum {
        match self.inner.get_mut(key) {
            None => {
                self.inner.insert(key.clone(), SEQ_START_NUM);
                SEQ_START_NUM
            }
            Some(seq) => {
                *seq = (*seq + 1) % self.modulo_divisor;
                *seq
            }
        }
    }
}

#[derive(Hash, PartialEq, Eq, Clone)]
struct Sns2Key {
    sta_addr: MacAddr,
    tid: u16,
}

#[derive(Hash, PartialEq, Eq, Clone)]
struct Sns4Key {
    sta_addr: MacAddr,
    aci: u8,
}

/// Manages all SNS for a STA.
pub struct SequenceManager {
    sns1: SnsMap<MacAddr>,
    sns2: SnsMap<Sns2Key>,
    sns4: SnsMap<Sns4Key>,
    sns5: u32,
}

impl SequenceManager {
    pub fn new() -> Self {
        Self {
            sns1: SnsMap::new(4096),
            sns2: SnsMap::new(4096),
            sns4: SnsMap::new(1024),
            sns5: SEQ_START_NUM,
        }
    }

    pub fn next_sns1(&mut self, sta_addr: &MacAddr) -> SequenceNum {
        self.sns1.next(sta_addr)
    }

    pub fn next_sns2(&mut self, sta_addr: &MacAddr, tid: u16) -> SequenceNum {
        self.sns2.next(&Sns2Key { sta_addr: sta_addr.clone(), tid })
    }

    // Sns3 optional

    pub fn next_sns4(&mut self, sta_addr: &MacAddr, aci: u8) -> SequenceNum {
        self.sns4.next(&Sns4Key { sta_addr: sta_addr.clone(), aci })
    }

    pub fn next_sns5(&mut self) -> SequenceNum {
        // Arbitrary value by spec. Increment to assist debugging.
        self.sns5 = (self.sns5 + 1) % 4096;
        self.sns5
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lazy_static::lazy_static;

    lazy_static! {
        static ref FIRST_PEER: MacAddr = MacAddr::from([1; 6]);
        static ref SECOND_PEER: MacAddr = MacAddr::from([2; 6]);
    }

    #[test]
    fn sns1_next() {
        let mut seq_mgr = SequenceManager::new();

        for i in 0..4095 {
            let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
            assert_eq!(i + 1, seq_num);
        }

        let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
        assert_eq!(0, seq_num); // wrapped

        let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
        assert_eq!(0 + 1, seq_num);
    }

    #[test]
    fn sns1_next_multiple_peers() {
        let mut seq_mgr = SequenceManager::new();

        seq_mgr.next_sns1(&FIRST_PEER);
        seq_mgr.next_sns1(&FIRST_PEER);
        let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
        assert_eq!(3, seq_num);

        seq_mgr.next_sns1(&SECOND_PEER);
        let seq_num = seq_mgr.next_sns1(&SECOND_PEER);
        assert_eq!(2, seq_num);

        let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
        assert_eq!(4, seq_num);
    }

    #[test]
    fn sns2_next_multiple_tids() {
        let mut seq_mgr = SequenceManager::new();

        seq_mgr.next_sns2(&FIRST_PEER, 0);
        seq_mgr.next_sns2(&FIRST_PEER, 0);
        let seq_num = seq_mgr.next_sns2(&&FIRST_PEER, 0);
        assert_eq!(3, seq_num);

        seq_mgr.next_sns2(&FIRST_PEER, 1);
        let seq_num = seq_mgr.next_sns2(&FIRST_PEER, 1);
        assert_eq!(2, seq_num);

        let seq_num = seq_mgr.next_sns2(&FIRST_PEER, 0);
        assert_eq!(4, seq_num);
    }

    #[test]
    fn sns4_next_multiple_acis() {
        let mut seq_mgr = SequenceManager::new();

        seq_mgr.next_sns4(&FIRST_PEER, 0);
        seq_mgr.next_sns4(&FIRST_PEER, 0);
        let seq_num = seq_mgr.next_sns4(&FIRST_PEER, 0);
        assert_eq!(3, seq_num);

        seq_mgr.next_sns4(&FIRST_PEER, 1);
        let seq_num = seq_mgr.next_sns4(&FIRST_PEER, 1);
        assert_eq!(2, seq_num);

        let seq_num = seq_mgr.next_sns4(&FIRST_PEER, 0);
        assert_eq!(4, seq_num);
    }

    #[test]
    fn sns1_sns2_sns4_next() {
        let mut seq_mgr = SequenceManager::new();

        seq_mgr.next_sns1(&FIRST_PEER);
        seq_mgr.next_sns1(&FIRST_PEER);
        let seq_num = seq_mgr.next_sns1(&FIRST_PEER);
        assert_eq!(3, seq_num);

        seq_mgr.next_sns2(&FIRST_PEER, 0);
        let seq_num = seq_mgr.next_sns2(&FIRST_PEER, 0);
        assert_eq!(2, seq_num);

        let seq_num = seq_mgr.next_sns4(&FIRST_PEER, 3);
        assert_eq!(1, seq_num);
    }
}