1#!/usr/bin/env python3
2#
3#  Copyright (c) 2019, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30from pktverify.addrs import Ipv6Addr
31from pktverify.bytes import Bytes
32
33DOMAIN_PREFIX = Bytes('fd00:7d03:7d03:7d03')
34BACKBONE_IPV6_PREFIX = Bytes('91')
35
36LINK_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = Ipv6Addr('ff32:40:fd00:db8::1')
37REALM_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = Ipv6Addr('ff33:40:fd00:db8::1')
38REALM_LOCAL_ALL_NODES_ADDRESS = Ipv6Addr('ff03::1')
39REALM_LOCAL_ALL_ROUTERS_ADDRESS = Ipv6Addr('ff03::2')
40LINK_LOCAL_ALL_NODES_MULTICAST_ADDRESS = Ipv6Addr('ff02::1')
41LINK_LOCAL_ALL_ROUTERS_MULTICAST_ADDRESS = Ipv6Addr('ff02::2')
42LINK_LOCAL_ALL_BBRS_MULTICAST_ADDRESS = Ipv6Addr('ff32:40:fd00:7d03:7d03:7d03:0:3')
43
44# MA in Test Plan, make sure these are same as ../config.py
45MA1 = Ipv6Addr('ff04::1234:777a:1')
46MA1g = Ipv6Addr('ff0e::1234:777a:1')
47MA2 = Ipv6Addr('ff05::1234:777a:1')
48MA3 = Ipv6Addr('ff0e::1234:777a:3')
49MA4 = Ipv6Addr('ff05::1234:777a:4')
50MA5 = Ipv6Addr('ff03::1234:777a:5')
51MA6 = Ipv6Addr('ff02::1')
52MAe1 = Ipv6Addr('fd0e::1234:777a:1')
53MAe2 = Ipv6Addr('::')
54MAe3 = Ipv6Addr('cafe::e0ff')
55MAS = [[Ipv6Addr(f'ff0e::{j:x}:777a:{i:x}') for j in range(16)] for i in range(16)]
56ALL_MPL_FORWARDERS_MA = Ipv6Addr('ff03::fc')
57
58LINK_LOCAL_PREFIX = Bytes("fe80")
59DEFAULT_MESH_LOCAL_PREFIX = Bytes("fd00:0db8:0000:0000")
60LEADER_ALOC_IID = Bytes("0000:00ff:fe00:fc00")
61PBBR_ALOC_IID = Bytes("0000:00ff:fe00:fc38")
62LEADER_ALOC = Ipv6Addr(DEFAULT_MESH_LOCAL_PREFIX + LEADER_ALOC_IID)
63PBBR_ALOC = Ipv6Addr(DEFAULT_MESH_LOCAL_PREFIX + PBBR_ALOC_IID)
64
65# Minimum value of the MLR Timeout parameter in the BBR Dataset
66MLR_TIMEOUT_MIN = 300
67
68# Max response delay
69MLE_MAX_RESPONSE_DELAY = 1
70
71# WPAN CMDs
72WPAN_DATA_REQUEST = 4
73
74# WPAN Frame Types
75WPAN_BEACON = 0
76WPAN_DATA = 1
77WPAN_ACK = 2
78WPAN_CMD = 3
79
80# COAP methods
81COAP_CODE_POST = 2
82COAP_CODE_ACK = 68
83
84MLE_LINK_REQUEST = 0
85MLE_LINK_ACCEPT = 1
86MLE_LINK_ACCEPT_AND_REQUEST = 2
87MLE_ADVERTISEMENT = 4
88MLE_DATA_REQUEST = 7
89MLE_DATA_RESPONSE = 8
90MLE_PARENT_REQUEST = 9
91MLE_PARENT_RESPONSE = 10
92MLE_CHILD_ID_REQUEST = 11
93MLE_CHILD_ID_RESPONSE = 12
94MLE_CHILD_UPDATE_REQUEST = 13
95MLE_CHILD_UPDATE_RESPONSE = 14
96MLE_ANNOUNCE = 15
97MLE_DISCOVERY_REQUEST = 16
98MLE_DISCOVERY_RESPONSE = 17
99MLE_LINK_METRICS_MANAGEMENT_REQUEST = 18
100MLE_LINK_METRICS_MANAGEMENT_RESPONSE = 19
101MLE_LINK_PROBE = 20
102
103# COAP URIs
104ADDR_QRY_URI = '/a/aq'
105ADDR_NTF_URI = '/a/an'
106ADDR_ERR_URI = '/a/ae'
107ADDR_SOL_URI = '/a/as'
108ADDR_REL_URI = '/a/ar'
109SVR_DATA_URI = '/a/sd'
110ND_DATA_URI = '/a/nd'
111RLY_RX_URI = '/c/rx'
112RLY_TX_URI = '/c/tx'
113MGMT_PANID_QUERY = '/c/pq'
114MGMT_PANID_CONFLICT = '/c/pc'
115MGMT_ED_SCAN = '/c/es'
116MGMT_ED_REPORT = '/c/er'
117MGMT_ACTIVE_GET_URI = '/c/ag'
118MGMT_ACTIVE_SET_URI = '/c/as'
119MGMT_PENDING_SET_URI = '/c/ps'
120MGMT_PENDING_GET_URI = '/c/pg'
121MGMT_DATASET_CHANGED_URI = '/c/dc'
122MGMT_COMMISSIONER_GET_URI = '/c/cg'
123DIAG_GET_URI = '/d/dg'
124DIAG_RST_URI = '/d/dr'
125MGMT_COMMISSIONER_SET_URI = '/c/cs'
126LEAD_PET_URI = '/c/lp'
127LEAD_KA_URI = '/c/la'
128DIAG_GET_QRY_URI = '/d/dq'
129DIAG_GET_ANS_URI = '/d/da'
130BACKBONE_QUERY_URI = '/b/bq'
131BACKBONE_ANSWER_URI = '/b/ba'
132
133# ADDR SOL Status
134ADDR_SOL_SUCCESS = 0
135ADDR_SOL_NA = 1
136
137# MLE TLVs
138SOURCE_ADDRESS_TLV = 0
139MODE_TLV = 1
140TIMEOUT_TLV = 2
141CHALLENGE_TLV = 3
142RESPONSE_TLV = 4
143LINK_LAYER_FRAME_COUNTER_TLV = 5
144LINK_QUALITY_TLV = 6
145PARAMETER_TLV = 7
146MLE_FRAME_COUNTER_TLV = 8
147ROUTE64_TLV = 9
148ADDRESS16_TLV = 10
149LEADER_DATA_TLV = 11
150NETWORK_DATA_TLV = 12
151TLV_REQUEST_TLV = 13
152SCAN_MASK_TLV = 14
153CONNECTIVITY_TLV = 15
154LINK_MARGIN_TLV = 16
155STATUS_TLV = 17
156VERSION_TLV = 18
157ADDRESS_REGISTRATION_TLV = 19
158CHANNEL_TLV = 20
159PAN_ID_TLV = 21
160ACTIVE_TIMESTAMP_TLV = 22
161PENDING_TIMESTAMP_TLV = 23
162ACTIVE_OPERATION_DATASET_TLV = 24
163PENDING_OPERATION_DATASET_TLV = 25
164THREAD_DISCOVERY_TLV = 26
165CSL_SYNCHRONIZED_TIMEOUT = 85
166CSL_CLOCK_ACCURACY = 86
167LINK_METRICS_QUERY_TLV = 87
168LINK_METRICS_MANAGEMENT_TLV = 88
169LINK_METRICS_REPORT_TLV = 89
170LINK_PROBE_TLV = 90
171
172# Network Layer TLVs
173NL_TARGET_EID_TLV = 0
174NL_MAC_EXTENDED_ADDRESS_TLV = 1
175NL_RLOC16_TLV = 2
176NL_ML_EID_TLV = 3
177NL_STATUS_TLV = 4
178NL_TIME_SINCE_LAST_TRANSACTION_TLV = 6
179NL_ROUTER_MASK_TLV = 7
180NL_ND_OPTION_TLV = 8
181NL_ND_DATA_TLV = 9
182NL_THREAD_NETWORK_DATA_TLV = 10
183
184# Network Layer Status
185NL_SUCESS = 0
186NL_NO_ADDRESS_AVAILABLE = 1
187NL_TOO_FEW_ROUTERS = 2
188NL_HAVE_CHILD_ID_REQUEST = 3
189NL_PARENT_PARTITION_CHANGE = 4
190
191# MeshCop TLVs
192NM_CHANNEL_TLV = 0
193NM_PAN_ID_TLV = 1
194NM_EXTENDED_PAN_ID_TLV = 2
195NM_NETWORK_NAME_TLV = 3
196NM_PSKC_TLV = 4
197NM_NETWORK_KEY_TLV = 5
198NM_NETWORK_KEY_SEQUENCE_COUNTER_TLV = 6
199NM_NETWORK_MESH_LOCAL_PREFIX_TLV = 7
200NM_STEERING_DATA_TLV = 8
201NM_BORDER_AGENT_LOCATOR_TLV = 9
202NM_COMMISSIONER_ID_TLV = 10
203NM_COMMISSIONER_SESSION_ID_TLV = 11
204NM_SECURITY_POLICY_TLV = 12
205NM_ACTIVE_TIMESTAMP_TLV = 14
206NM_COMMISSIONER_UDP_PORT_TLV = 15
207NM_STATE_TLV = 16
208NM_JOINER_DTLS_ENCAPSULATION_TLV = 17
209NM_JOINER_UDP_PORT_TLV = 18
210NM_JOINER_IID_TLV = 19
211NM_JOINER_ROUTER_LOCATOR_TLV = 20
212NM_JOINER_ROUTER_KEK_TLV = 21
213NM_PENDING_TIMESTAMP_TLV = 51
214NM_DELAY_TIMER_TLV = 52
215NM_CHANNEL_MASK_TLV = 53
216NM_SCAN_DURATION = 56
217NM_ENERGY_LIST_TLV = 57
218NM_DISCOVERY_REQUEST_TLV = 128
219NM_DISCOVERY_RESPONSE_TLV = 129
220
221# Diagnostic TLVs
222DG_MAC_EXTENDED_ADDRESS_TLV = 0
223DG_MAC_ADDRESS_TLV = 1
224DG_MODE_TLV = 2
225DG_TIMEOUT_TLV = 3
226DG_CONNECTIVITY_TLV = 4
227DG_ROUTE64_TLV = 5
228DG_LEADER_DATA_TLV = 6
229DG_NETWORK_DATA_TLV = 7
230DG_IPV6_ADDRESS_LIST_TLV = 8
231DG_MAC_COUNTERS_TLV = 9
232DG_BATTERY_LEVEL_TLV = 14
233DG_SUPPLY_VOLTAGE_TLV = 15
234DG_CHILD_TABLE_TLV = 16
235DG_CHANNEL_PAGES_TLV = 17
236DG_TYPE_LIST_TLV = 18
237DG_MAX_CHILD_TIMEOUT_TLV = 19
238
239# MeshCop State
240MESHCOP_ACCEPT = 1
241MESHCOP_PENDING = 0
242MESHCOP_REJECT = -1
243
244# DTLS
245HANDSHAKE_HELLO_REQUEST = 0
246HANDSHAKE_CLIENT_HELLO = 1
247HANDSHAKE_SERVER_HELLO = 2
248HANDSHAKE_HELLO_VERIFY_REQUEST = 3
249HANDSHAKE_CERTIFICATE = 11
250HANDSHAKE_SERVER_KEY_EXCHANGE = 12
251HANDSHAKE_CERTIFICATE_REQUEST = 13
252HANDSHAKE_SERVER_HELLO_DONE = 14
253HANDSHAKE_CERTIFICATE_VERIFY = 15
254HANDSHAKE_CLIENT_KEY_EXCHANGE = 16
255HANDSHAKE_FINISHED = 20
256CONTENT_CHANGE_CIPHER_SPEC = 20
257CONTENT_ALERT = 21
258CONTENT_HANDSHAKE = 22
259CONTENT_APPLICATION_DATA = 23
260
261# Network Data TLVs
262NWD_HAS_ROUTER_TLV = 0
263NWD_PREFIX_TLV = 1
264NWD_BORDER_ROUTER_TLV = 2
265NWD_6LOWPAN_ID_TLV = 3
266NWD_COMMISSIONING_DATA_TLV = 4
267NWD_SERVICE_TLV = 5
268NWD_SERVER_TLV = 6
269
270# Link Metrics TLVs
271LM_FORWARD_PROBING_REGISTRATION_SUB_TLV = 3
272LM_ENHANCED_ACK_CONFIGURATION_SUB_TLV = 7
273
274# DUA related constants
275
276ADDRESS_QUERY_INITIAL_RETRY_DELAY = 15
277ADDRESS_QUERY_MAX_RETRY_DELAY = 8
278ADDRESS_QUERY_TIMEOUT = 3
279ADVERTISEMENT_I_MAX = 32
280ADVERTISEMENT_I_MIN = 1
281
282CONTEXT_ID_REUSE_DELAY = 48
283
284DATA_RESUBMIT_DELAY = 300
285
286DUA_DAD_PERIOD = 100
287DUA_DAD_QUERY_TIMEOUT = 1.0
288DUA_DAD_REPEATS = 2
289DUA_RECENT_TIME = 20
290FAILED_ROUTER_TRANSMISSIONS = 4
291ID_REUSE_DELAY = 100
292ID_SEQUENCE_PERIOD = 10
293INFINITE_COST_TIMEOUT = 90
294
295REAL_LAYER_NAMES = {
296    'mle',
297    'coap',
298    'dtls',
299    'wpan',
300    'eth',
301    'tcp',
302    'udp',
303    'ip',
304    'ipv6',
305    'icmpv6',
306    '6lowpan',
307    'arp',
308    'thread_bl',
309    'thread_address',
310    'thread_diagnostic',
311    'thread_nm',
312    'thread_bcn',
313    'ssdp',
314    'dns',
315    'igmp',
316    'mdns',
317    'dns',
318}
319
320FAKE_LAYER_NAMES = {'thread_nwd', 'thread_meshcop', 'ipv6inner'}
321
322VALID_LAYER_NAMES = REAL_LAYER_NAMES | FAKE_LAYER_NAMES
323
324AUTO_SEEK_BACK_MAX_DURATION = 0.02
325
326# Wireshark configs
327WIRESHARK_OVERRIDE_PREFS = {
328    '6lowpan.context0':
329        'fd00:db8::/64',
330    '6lowpan.context1':
331        'fd00:7d03:7d03:7d03::/64',
332    'wpan.802154_fcs_ok':
333        'FALSE',
334    'wpan.802154_sec_suite':
335        'AES-128 Encryption, 32-bit Integrity Protection',
336    'thread.thr_seq_ctr':
337        '00000000',
338    'uat:ieee802154_keys':
339        '''"00112233445566778899aabbccddeeff","1","Thread hash"
340                              "ffeeddccbbaa99887766554433221100","1","Thread hash"''',
341}
342
343WIRESHARK_DECODE_AS_ENTRIES = {
344    'udp.port==61631': 'coap',
345    # SRP server ports for dissecting SRP requests and responses
346    'udp.port==53535': 'dns',
347    'udp.port==53536': 'dns',
348    'udp.port==53537': 'dns',
349    'udp.port==53538': 'dns',
350    'udp.port==53539': 'dns',
351    'udp.port==53540': 'dns',
352    'udp.port==53541': 'dns',
353    'udp.port==53542': 'dns',
354    'udp.port==53543': 'dns',
355    'udp.port==53544': 'dns',
356    'udp.port==53545': 'dns',
357    'udp.port==53546': 'dns',
358    'udp.port==53547': 'dns',
359    'udp.port==53548': 'dns',
360    'udp.port==53549': 'dns',
361    'udp.port==53550': 'dns',
362    'udp.port==53551': 'dns',
363    'udp.port==53552': 'dns',
364    'udp.port==53553': 'dns',
365    'udp.port==53554': 'dns',
366}
367
368TIMEOUT_JOIN_NETWORK = 10
369TIMEOUT_DUA_REGISTRATION = 10
370TIMEOUT_DUA_DAD = 15
371TIMEOUT_HOST_READY = 10
372TIMEOUT_CHILD_DETACH = 120
373TIMEOUT_REGISTER_MA = 5
374
375# 802.15.4 Frame Version
376MAC_FRAME_VERSION_2006 = 1
377MAC_FRAME_VERSION_2015 = 2
378
379# 802.15.4 Frame Type
380MAC_FRAME_TYPE_BEACON = 0x0
381MAC_FRAME_TYPE_DATA = 0x1
382MAC_FRAME_TYPE_ACK = 0x2
383MAC_FRAME_TYPE_MAC_CMD = 0x3
384
385# CSL
386CSL_DEFAULT_PERIOD = 3125  # 0.5s, 3125 in units of ten symbols
387CSL_DEFAULT_PERIOD_IN_SECOND = 0.5
388US_PER_TEN_SYMBOLS = 160
389CSL_IE_ID = 0x1a
390CSL_DEFAULT_TIMEOUT = 30
391CSL_DEFAULT_CHANNEL = 12
392
393# Thread Version TLV value
394THREAD_VERSION_1_1 = 2
395THREAD_VERSION_1_2 = 3
396THREAD_VERSION_1_3 = 4
397
398# ICMPv6 Types
399ICMPV6_TYPE_DESTINATION_UNREACHABLE = 1
400ICMPV6_TYPE_PACKET_TO_BIG = 2
401ICMPV6_TYPE_TIME_EXCEEDED = 3
402ICMPV6_TYPE_PARAMETER_PROBLEM = 4
403ICMPV6_TYPE_ECHO_REQUEST = 128
404ICMPV6_TYPE_ECHO_REPLY = 129
405
406ICMPV6_RA_OPT_TYPE_RIO = 24
407
408THREAD_ALLOWED_ICMPV6_TYPES = [
409    ICMPV6_TYPE_DESTINATION_UNREACHABLE,
410    ICMPV6_TYPE_PACKET_TO_BIG,
411    ICMPV6_TYPE_TIME_EXCEEDED,
412    ICMPV6_TYPE_PARAMETER_PROBLEM,
413    ICMPV6_TYPE_ECHO_REQUEST,
414    ICMPV6_TYPE_ECHO_REPLY,
415]
416
417# Link Metrics
418LINK_METRICS_STATUS_SUCCESS = 0
419LINK_METRICS_STATUS_CANNOT_SUPPORT_NEW_SERIES = 1
420LINK_METRICS_STATUS_SERIES_ID_ALREADY_REGISTERED = 2
421LINK_METRICS_STATUS_SERIES_ID_NOT_RECOGNIZED = 3
422LINK_METRICS_STATUS_NO_MATCHING_FRAMES_RECEIVED = 4
423LINK_METRICS_STATUS_OTHER_ERROR = 254
424
425LINK_METRICS_TYPE_AVERAGE_ENUM_COUNT = 0
426LINK_METRICS_TYPE_AVERAGE_ENUM_EXPONENTIAL = 1
427
428LINK_METRICS_METRIC_TYPE_ENUM_PDU_COUNT = 0
429LINK_METRICS_METRIC_TYPE_ENUM_LQI = 1
430LINK_METRICS_METRIC_TYPE_ENUM_LINK_MARGIN = 2
431LINK_METRICS_METRIC_TYPE_ENUM_RSSI = 3
432
433LINK_METRICS_ENH_ACK_PROBING_CLEAR = 0
434LINK_METRICS_ENH_ACK_PROBING_REGISTER = 1
435
436# THREAD_COMPANY_ID
437THREAD_IEEE_802154_COMPANY_ID = 0xEAB89B
438
439if __name__ == '__main__':
440    from pktverify.addrs import Ipv6Addr
441
442    assert Ipv6Addr("fe80:0000:0000:0000:0200:0000:0000:0004").startswith(LINK_LOCAL_PREFIX)
443    assert Ipv6Addr("fd00:0db8:0000:0000:0000:00ff:fe00:8001").startswith(DEFAULT_MESH_LOCAL_PREFIX)
444