1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - Google
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#         http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import itertools
18
19BAND_2G = '2g'
20BAND_5G = '5g'
21WPA1 = 1
22WPA2 = 2
23MIXED = 3
24MAX_WPA_PSK_LENGTH = 64
25MIN_WPA_PSK_LENGTH = 8
26WPA_STRICT_REKEY = 1
27WPA_DEFAULT_CIPHER = 'TKIP'
28WPA2_DEFAULT_CIPER = 'CCMP'
29WPA_GROUP_KEY_ROTATION_TIME = 600
30WPA_STRICT_REKEY_DEFAULT = True
31WPA_STRING = 'wpa'
32WPA2_STRING = 'wpa2'
33WPA_MIXED_STRING = 'wpa/wpa2'
34WLAN0_STRING = 'wlan0'
35WLAN1_STRING = 'wlan1'
36WLAN2_STRING = 'wlan2'
37WLAN3_STRING = 'wlan3'
38AP_DEFAULT_CHANNEL_2G = 6
39AP_DEFAULT_CHANNEL_5G = 36
40AP_DEFAULT_MAX_SSIDS_2G = 8
41AP_DEFAULT_MAX_SSIDS_5G = 8
42AP_SSID_LENGTH_2G = 8
43AP_PASSPHRASE_LENGTH_2G = 10
44AP_SSID_LENGTH_5G = 8
45AP_PASSPHRASE_LENGTH_5G = 10
46
47# A mapping of frequency to channel number.  This includes some
48# frequencies used outside the US.
49CHANNEL_MAP = {
50    2412: 1,
51    2417: 2,
52    2422: 3,
53    2427: 4,
54    2432: 5,
55    2437: 6,
56    2442: 7,
57    2447: 8,
58    2452: 9,
59    2457: 10,
60    2462: 11,
61    # 12, 13 are only legitimate outside the US.
62    # 2467: 12,
63    # 2472: 13,
64    # 14 is for Japan, DSSS and CCK only.
65    # 2484: 14,
66    # 34 valid in Japan.
67    # 5170: 34,
68    # 36-116 valid in the US, except 38, 42, and 46, which have
69    # mixed international support.
70    5180: 36,
71    5190: 38,
72    5200: 40,
73    5210: 42,
74    5220: 44,
75    5230: 46,
76    5240: 48,
77    # DFS channels.
78    5260: 52,
79    5280: 56,
80    5300: 60,
81    5320: 64,
82    5500: 100,
83    5520: 104,
84    5540: 108,
85    5560: 112,
86    5580: 116,
87    # 120, 124, 128 valid in Europe/Japan.
88    5600: 120,
89    5620: 124,
90    5640: 128,
91    # 132+ valid in US.
92    5660: 132,
93    5680: 136,
94    5700: 140,
95    # 144 is supported by a subset of WiFi chips
96    # (e.g. bcm4354, but not ath9k).
97    5720: 144,
98    # End DFS channels.
99    5745: 149,
100    5755: 151,
101    5765: 153,
102    5775: 155,
103    5795: 159,
104    5785: 157,
105    5805: 161,
106    5825: 165
107}
108
109MODE_11A = 'a'
110MODE_11B = 'b'
111MODE_11G = 'g'
112MODE_11N_MIXED = 'n-mixed'
113MODE_11N_PURE = 'n-only'
114MODE_11AC_MIXED = 'ac-mixed'
115MODE_11AC_PURE = 'ac-only'
116
117N_CAPABILITY_LDPC = object()
118N_CAPABILITY_HT20 = object()
119N_CAPABILITY_HT40_PLUS = object()
120N_CAPABILITY_HT40_MINUS = object()
121N_CAPABILITY_GREENFIELD = object()
122N_CAPABILITY_SGI20 = object()
123N_CAPABILITY_SGI40 = object()
124N_CAPABILITY_TX_STBC = object()
125N_CAPABILITY_RX_STBC1 = object()
126N_CAPABILITY_RX_STBC12 = object()
127N_CAPABILITY_RX_STBC123 = object()
128N_CAPABILITY_DSSS_CCK_40 = object()
129N_CAPABILITIES_MAPPING = {
130    N_CAPABILITY_LDPC: '[LDPC]',
131    N_CAPABILITY_HT20: '[HT20]',
132    N_CAPABILITY_HT40_PLUS: '[HT40+]',
133    N_CAPABILITY_HT40_MINUS: '[HT40-]',
134    N_CAPABILITY_GREENFIELD: '[GF]',
135    N_CAPABILITY_SGI20: '[SHORT-GI-20]',
136    N_CAPABILITY_SGI40: '[SHORT-GI-40]',
137    N_CAPABILITY_TX_STBC: '[TX-STBC]',
138    N_CAPABILITY_RX_STBC1: '[RX-STBC1]',
139    N_CAPABILITY_RX_STBC12: '[RX-STBC12]',
140    N_CAPABILITY_RX_STBC123: '[RX-STBC123]',
141    N_CAPABILITY_DSSS_CCK_40: '[DSSS_CCK-40]'
142}
143N_CAPABILITY_HT40_MINUS_CHANNELS = object()
144N_CAPABILITY_HT40_PLUS_CHANNELS = object()
145AC_CAPABILITY_VHT160 = object()
146AC_CAPABILITY_VHT160_80PLUS80 = object()
147AC_CAPABILITY_RXLDPC = object()
148AC_CAPABILITY_SHORT_GI_80 = object()
149AC_CAPABILITY_SHORT_GI_160 = object()
150AC_CAPABILITY_TX_STBC_2BY1 = object()
151AC_CAPABILITY_RX_STBC_1 = object()
152AC_CAPABILITY_RX_STBC_12 = object()
153AC_CAPABILITY_RX_STBC_123 = object()
154AC_CAPABILITY_RX_STBC_1234 = object()
155AC_CAPABILITY_SU_BEAMFORMER = object()
156AC_CAPABILITY_SU_BEAMFORMEE = object()
157AC_CAPABILITY_BF_ANTENNA_2 = object()
158AC_CAPABILITY_SOUNDING_DIMENSION_2 = object()
159AC_CAPABILITY_MU_BEAMFORMER = object()
160AC_CAPABILITY_MU_BEAMFORMEE = object()
161AC_CAPABILITY_VHT_TXOP_PS = object()
162AC_CAPABILITY_HTC_VHT = object()
163AC_CAPABILITY_MAX_A_MPDU_LEN_EXP0 = object()
164AC_CAPABILITY_MAX_A_MPDU_LEN_EXP1 = object()
165AC_CAPABILITY_MAX_A_MPDU_LEN_EXP2 = object()
166AC_CAPABILITY_MAX_A_MPDU_LEN_EXP3 = object()
167AC_CAPABILITY_MAX_A_MPDU_LEN_EXP4 = object()
168AC_CAPABILITY_MAX_A_MPDU_LEN_EXP5 = object()
169AC_CAPABILITY_MAX_A_MPDU_LEN_EXP6 = object()
170AC_CAPABILITY_MAX_A_MPDU_LEN_EXP7 = object()
171AC_CAPABILITY_VHT_LINK_ADAPT2 = object()
172AC_CAPABILITY_VHT_LINK_ADAPT3 = object()
173AC_CAPABILITY_RX_ANTENNA_PATTERN = object()
174AC_CAPABILITY_TX_ANTENNA_PATTERN = object()
175AC_CAPABILITY_MAX_MPDU_7991 = object()
176AC_CAPABILITY_MAX_MPDU_11454 = object()
177AC_CAPABILITIES_MAPPING = {
178    AC_CAPABILITY_VHT160: '[VHT160]',
179    AC_CAPABILITY_VHT160_80PLUS80: '[VHT160-80PLUS80]',
180    AC_CAPABILITY_RXLDPC: '[RXLDPC]',
181    AC_CAPABILITY_SHORT_GI_80: '[SHORT-GI-80]',
182    AC_CAPABILITY_SHORT_GI_160: '[SHORT-GI-160]',
183    AC_CAPABILITY_TX_STBC_2BY1: '[TX-STBC-2BY1]',
184    AC_CAPABILITY_RX_STBC_1: '[RX-STBC-1]',
185    AC_CAPABILITY_RX_STBC_12: '[RX-STBC-12]',
186    AC_CAPABILITY_RX_STBC_123: '[RX-STBC-123]',
187    AC_CAPABILITY_RX_STBC_1234: '[RX-STBC-1234]',
188    AC_CAPABILITY_SU_BEAMFORMER: '[SU-BEAMFORMER]',
189    AC_CAPABILITY_SU_BEAMFORMEE: '[SU-BEAMFORMEE]',
190    AC_CAPABILITY_BF_ANTENNA_2: '[BF-ANTENNA-2]',
191    AC_CAPABILITY_SOUNDING_DIMENSION_2: '[SOUNDING-DIMENSION-2]',
192    AC_CAPABILITY_MU_BEAMFORMER: '[MU-BEAMFORMER]',
193    AC_CAPABILITY_MU_BEAMFORMEE: '[MU-BEAMFORMEE]',
194    AC_CAPABILITY_VHT_TXOP_PS: '[VHT-TXOP-PS]',
195    AC_CAPABILITY_HTC_VHT: '[HTC-VHT]',
196    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP0: '[MAX-A-MPDU-LEN-EXP0]',
197    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP1: '[MAX-A-MPDU-LEN-EXP1]',
198    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP2: '[MAX-A-MPDU-LEN-EXP2]',
199    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP3: '[MAX-A-MPDU-LEN-EXP3]',
200    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP4: '[MAX-A-MPDU-LEN-EXP4]',
201    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP5: '[MAX-A-MPDU-LEN-EXP5]',
202    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP6: '[MAX-A-MPDU-LEN-EXP6]',
203    AC_CAPABILITY_MAX_A_MPDU_LEN_EXP7: '[MAX-A-MPDU-LEN-EXP7]',
204    AC_CAPABILITY_VHT_LINK_ADAPT2: '[VHT-LINK-ADAPT2]',
205    AC_CAPABILITY_VHT_LINK_ADAPT3: '[VHT-LINK-ADAPT3]',
206    AC_CAPABILITY_RX_ANTENNA_PATTERN: '[RX-ANTENNA-PATTERN]',
207    AC_CAPABILITY_TX_ANTENNA_PATTERN: '[TX-ANTENNA-PATTERN]',
208    AC_CAPABILITY_MAX_MPDU_11454: '[MAX-MPDU-11454]',
209    AC_CAPABILITY_MAX_MPDU_7991: '[MAX-MPDU-7991]'
210}
211VHT_CHANNEL_WIDTH_40 = 0
212VHT_CHANNEL_WIDTH_80 = 1
213VHT_CHANNEL_WIDTH_160 = 2
214VHT_CHANNEL_WIDTH_80_80 = 3
215
216# This is a loose merging of the rules for US and EU regulatory
217# domains as taken from IEEE Std 802.11-2012 Appendix E.  For instance,
218# we tolerate HT40 in channels 149-161 (not allowed in EU), but also
219# tolerate HT40+ on channel 7 (not allowed in the US).  We take the loose
220# definition so that we don't prohibit testing in either domain.
221HT40_ALLOW_MAP = {
222    N_CAPABILITY_HT40_MINUS_CHANNELS:
223    tuple(
224        itertools.chain(
225            range(6, 14), range(40, 65, 8), range(104, 137, 8), [153, 161])),
226    N_CAPABILITY_HT40_PLUS_CHANNELS:
227    tuple(
228        itertools.chain(
229            range(1, 8), range(36, 61, 8), range(100, 133, 8), [149, 157]))
230}
231
232PMF_SUPPORT_DISABLED = 0
233PMF_SUPPORT_ENABLED = 1
234PMF_SUPPORT_REQUIRED = 2
235PMF_SUPPORT_VALUES = (PMF_SUPPORT_DISABLED, PMF_SUPPORT_ENABLED,
236                      PMF_SUPPORT_REQUIRED)
237
238DRIVER_NAME = 'nl80211'
239
240CENTER_CHANNEL_MAP = {
241    VHT_CHANNEL_WIDTH_40: {
242        'delta': 2,
243        'channels': ((36, 40), (44, 48), (52, 56), (60, 64), (100, 104),
244                     (108, 112), (116, 120), (124, 128), (132, 136),
245                     (140, 144), (149, 153), (147, 161))
246    },
247    VHT_CHANNEL_WIDTH_80: {
248        'delta': 6,
249        'channels': ((36, 48), (52, 64), (100, 112), (116, 128), (132, 144),
250                     (149, 161))
251    },
252    VHT_CHANNEL_WIDTH_160: {
253        'delta': 14,
254        'channels': ((36, 64), (100, 128))
255    }
256}
257