1#!/usr/bin/env python3 2# 3# Copyright (c) 2016, 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# 29import os 30from enum import Enum 31 32import coap 33import dtls 34import ipv6 35import lowpan 36import mesh_cop 37import message 38import mle 39import net_crypto 40import network_data 41import network_diag 42import network_layer 43import simulator 44import sniffer 45from tlvs_parsing import SubTlvsFactory 46 47MESH_LOCAL_PREFIX = 'fd00:db8::/64' 48MESH_LOCAL_PREFIX_REGEX_PATTERN = '^fd00:0?db8:0{0,4}:0{0,4}' 49ROUTING_LOCATOR = '64/:0:ff:fe00:/16' 50ROUTING_LOCATOR_REGEX_PATTERN = r'.*:(0)?:0{0,2}ff:fe00:\w{1,4}$' 51LINK_LOCAL = 'fe80:/112' 52LINK_LOCAL_REGEX_PATTERN = '^fe80:.*' 53ALOC_FLAG_REGEX_PATTERN = '.*:fc..$' 54LINK_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff32:40:fd00:db8:0:0:0:1' 55REALM_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff33:40:fd00:db8:0:0:0:1' 56REALM_LOCAL_ALL_NODES_ADDRESS = 'ff03::1' 57REALM_LOCAL_ALL_ROUTERS_ADDRESS = 'ff03::2' 58LINK_LOCAL_ALL_NODES_ADDRESS = 'ff02::1' 59LINK_LOCAL_ALL_ROUTERS_ADDRESS = 'ff02::2' 60TMF_PORT = 61631 61 62DOMAIN_PREFIX = 'fd00:7d03:7d03:7d03::/64' 63DOMAIN_PREFIX_REGEX_PATTERN = '^fd00:7d03:7d03:7d03:' 64DOMAIN_PREFIX_ALTER = 'fd00:7d04:7d04:7d04::/64' 65 66PORT_OFFSET = int(os.getenv('PORT_OFFSET', '0')) 67BACKBONE_PREFIX = f'{0x9100 + PORT_OFFSET:04x}::/64' 68BACKBONE_PREFIX_REGEX_PATTERN = f'^{0x9100 + PORT_OFFSET:04x}:' 69BACKBONE_DOCKER_NETWORK_NAME = f'backbone{PORT_OFFSET}' 70BACKBONE_IFNAME = 'eth0' 71THREAD_IFNAME = 'wpan0' 72 73OTBR_DOCKER_IMAGE = os.getenv('OTBR_DOCKER_IMAGE', 'otbr-ot12-backbone-ci') 74OTBR_DOCKER_NAME_PREFIX = f'otbr_{PORT_OFFSET}_' 75 76ALL_NETWORK_BBRS_ADDRESS = 'ff32:40:fd00:db8:0:0:0:3' 77 78ALL_DOMAIN_BBRS_ADDRESS = 'ff32:40:fd00:7d03:7d03:7d03:0:3' 79ALL_DOMAIN_BBRS_ADDRESS_ALTER = 'ff32:40:fd00:7d04:7d04:7d04:0:3' 80 81ONLINK_GUA_PREFIX = '2021::/64' 82 83# Any address starts with 'fd' are considered on-link address. 84ONLINK_PREFIX_REGEX_PATTERN = '^fd' 85 86DEFAULT_NETWORK_KEY = bytearray([ 87 0x00, 88 0x11, 89 0x22, 90 0x33, 91 0x44, 92 0x55, 93 0x66, 94 0x77, 95 0x88, 96 0x99, 97 0xaa, 98 0xbb, 99 0xcc, 100 0xdd, 101 0xee, 102 0xff, 103]) 104 105 106class ADDRESS_TYPE(Enum): 107 LINK_LOCAL = 'LINK_LOCAL' 108 GLOBAL = 'GLOBAL' 109 RLOC = 'RLOC' 110 ALOC = 'ALOC' 111 ML_EID = 'ML_EID' 112 DUA = 'DUA' 113 BACKBONE_GUA = 'BACKBONE_GUA' 114 OMR = 'OMR' 115 ONLINK_ULA = 'ONLINK_ULA' 116 ONLINK_GUA = 'ONLINK_GUA' 117 118 119RSSI = { 120 'LINK_QULITY_0': -100, 121 'LINK_QULITY_1': -95, 122 'LINK_QULITY_2': -85, 123 'LINK_QULITY_3': -65, 124} 125 126SNIFFER_ID = int(os.getenv('SNIFFER_ID', 34)) 127 128CHANNEL = 11 129CHANNEL_MASK = 0x07fff800 130EXTENDED_PANID = 'dead00beef00cafe' 131NETWORK_NAME = 'OpenThread' 132PANID = 0xface 133PSKC = 'c23a76e98f1a6483639b1ac1271e2e27' 134SECURITY_POLICY = [672, 'onrc'] 135 136LEADER_STARTUP_DELAY = 12 137ROUTER_STARTUP_DELAY = 10 138ED_STARTUP_DELAY = 5 139BORDER_ROUTER_STARTUP_DELAY = 20 140MAX_NEIGHBOR_AGE = 100 141INFINITE_COST_TIMEOUT = 90 142 143MAX_ADVERTISEMENT_INTERVAL = 32 144MLE_END_DEVICE_TIMEOUT = 100 145 146AQ_TIMEOUT = 3 147ADDRESS_QUERY_INITIAL_RETRY_DELAY = 15 148DEFAULT_CHILD_TIMEOUT = 6 149VIRTUAL_TIME = int(os.getenv('VIRTUAL_TIME', 0)) 150PARENT_AGGREGATIOIN_DELAY = 5 151DUA_DAD_DELAY = 5 152DEFAULT_BBR_REGISTRATION_JITTER = 2 153DEFAULT_ROUTER_SELECTION_JITTER = 1 154 155LEADER_NOTIFY_SED_BY_CHILD_UPDATE_REQUEST = True 156 157THREAD_VERSION_1_1 = 2 158THREAD_VERSION_1_2 = 3 159THREAD_VERSION_1_3 = 4 160 161PACKET_VERIFICATION_NONE = 0 162PACKET_VERIFICATION_DEFAULT = 1 163PACKET_VERIFICATION_TREL = 2 164 165# After leader reset it may retransmit link request 6 times with max 5.5s interval 166LEADER_RESET_DELAY = 41 167# After router reset it may retransmit link request 3 times with max 5.5s interval 168ROUTER_RESET_DELAY = 23 169MLE_MAX_CRITICAL_TRANSMISSION_COUNT = 6 170MLE_MAX_TRANSMISSION_COUNT = 3 171 172 173def create_default_network_data_prefix_sub_tlvs_factories(): 174 return { 175 network_data.TlvType.HAS_ROUTE: 176 network_data.HasRouteFactory(routes_factory=network_data.RoutesFactory( 177 route_factory=network_data.RouteFactory())), 178 network_data.TlvType.BORDER_ROUTER: 179 network_data.BorderRouterFactory(), 180 network_data.TlvType.LOWPAN_ID: 181 network_data.LowpanIdFactory(), 182 } 183 184 185def create_default_network_data_prefix_sub_tlvs_factory(): 186 return network_data.PrefixSubTlvsFactory( 187 sub_tlvs_factories=create_default_network_data_prefix_sub_tlvs_factories()) 188 189 190def create_default_network_data_service_sub_tlvs_factories(): 191 return {network_data.TlvType.SERVER: network_data.ServerFactory()} 192 193 194def create_default_network_data_service_sub_tlvs_factory(): 195 return network_data.ServiceSubTlvsFactory( 196 sub_tlvs_factories=create_default_network_data_service_sub_tlvs_factories()) 197 198 199def create_default_network_data_commissioning_data_sub_tlvs_factories(): 200 return { 201 mesh_cop.TlvType.CHANNEL: mesh_cop.ChannelFactory(), 202 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 203 mesh_cop.TlvType.BORDER_AGENT_LOCATOR: mesh_cop.BorderAgentLocatorFactory(), 204 mesh_cop.TlvType.COMMISSIONER_SESSION_ID: mesh_cop.CommissionerSessionIdFactory(), 205 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 206 } 207 208 209def create_default_network_data_commissioning_data_sub_tlvs_factory(): 210 return network_data.CommissioningDataSubTlvsFactory( 211 sub_tlvs_factories=create_default_network_data_commissioning_data_sub_tlvs_factories()) 212 213 214def create_default_network_data_tlvs_factories(): 215 return { 216 network_data.TlvType.PREFIX: 217 network_data.PrefixFactory(sub_tlvs_factory=create_default_network_data_prefix_sub_tlvs_factory()), 218 network_data.TlvType.SERVICE: 219 network_data.ServiceFactory(sub_tlvs_factory=create_default_network_data_service_sub_tlvs_factory()), 220 network_data.TlvType.COMMISSIONING: 221 network_data.CommissioningDataFactory( 222 sub_tlvs_factory=create_default_network_data_commissioning_data_sub_tlvs_factory()), 223 } 224 225 226def create_default_network_data_tlvs_factory(): 227 return network_data.NetworkDataTlvsFactory(sub_tlvs_factories=create_default_network_data_tlvs_factories()) 228 229 230def create_default_mle_tlv_route64_factory(): 231 return mle.Route64Factory(link_quality_and_route_data_factory=mle.LinkQualityAndRouteDataFactory()) 232 233 234def create_default_mle_tlv_network_data_factory(): 235 return mle.NetworkDataFactory(network_data_tlvs_factory=create_default_network_data_tlvs_factory()) 236 237 238def create_default_mle_tlv_address_registration_factory(): 239 return mle.AddressRegistrationFactory( 240 addr_compressed_factory=mle.AddressCompressedFactory(), 241 addr_full_factory=mle.AddressFullFactory(), 242 ) 243 244 245def create_default_mle_tlv_thread_discovery_factory(): 246 return mle.ThreadDiscoveryFactory(thread_discovery_tlvs_factory=create_default_thread_discovery_tlvs_factory()) 247 248 249def create_default_thread_discovery_tlvs_factory(): 250 return mesh_cop.ThreadDiscoveryTlvsFactory(sub_tlvs_factories=create_default_thread_discovery_sub_tlvs_factories()) 251 252 253def create_default_thread_discovery_sub_tlvs_factories(): 254 return { 255 mesh_cop.TlvType.DISCOVERY_REQUEST: mesh_cop.DiscoveryRequestFactory(), 256 mesh_cop.TlvType.DISCOVERY_RESPONSE: mesh_cop.DiscoveryResponseFactory(), 257 mesh_cop.TlvType.EXTENDED_PANID: mesh_cop.ExtendedPanidFactory(), 258 mesh_cop.TlvType.NETWORK_NAME: mesh_cop.NetworkNameFactory(), 259 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 260 mesh_cop.TlvType.JOINER_UDP_PORT: mesh_cop.JoinerUdpPortFactory(), 261 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 262 } 263 264 265def create_default_mle_tlvs_factories(): 266 return { 267 mle.TlvType.SOURCE_ADDRESS: mle.SourceAddressFactory(), 268 mle.TlvType.MODE: mle.ModeFactory(), 269 mle.TlvType.TIMEOUT: mle.TimeoutFactory(), 270 mle.TlvType.CHALLENGE: mle.ChallengeFactory(), 271 mle.TlvType.RESPONSE: mle.ResponseFactory(), 272 mle.TlvType.LINK_LAYER_FRAME_COUNTER: mle.LinkLayerFrameCounterFactory(), 273 mle.TlvType.MLE_FRAME_COUNTER: mle.MleFrameCounterFactory(), 274 mle.TlvType.ROUTE64: create_default_mle_tlv_route64_factory(), 275 mle.TlvType.ADDRESS16: mle.Address16Factory(), 276 mle.TlvType.LEADER_DATA: mle.LeaderDataFactory(), 277 mle.TlvType.NETWORK_DATA: create_default_mle_tlv_network_data_factory(), 278 mle.TlvType.TLV_REQUEST: mle.TlvRequestFactory(), 279 mle.TlvType.SCAN_MASK: mle.ScanMaskFactory(), 280 mle.TlvType.CONNECTIVITY: mle.ConnectivityFactory(), 281 mle.TlvType.LINK_MARGIN: mle.LinkMarginFactory(), 282 mle.TlvType.STATUS: mle.StatusFactory(), 283 mle.TlvType.VERSION: mle.VersionFactory(), 284 mle.TlvType.ADDRESS_REGISTRATION: create_default_mle_tlv_address_registration_factory(), 285 mle.TlvType.CHANNEL: mle.ChannelFactory(), 286 mle.TlvType.PANID: mle.PanIdFactory(), 287 mle.TlvType.ACTIVE_TIMESTAMP: mle.ActiveTimestampFactory(), 288 mle.TlvType.PENDING_TIMESTAMP: mle.PendingTimestampFactory(), 289 mle.TlvType.CSL_CHANNEL: mle.CslChannelFactory(), 290 mle.TlvType.CSL_SYNCHRONIZED_TIMEOUT: mle.CslSynchronizedTimeoutFactory(), 291 mle.TlvType.CSL_CLOCK_ACCURACY: mle.CslClockAccuracyFactory(), 292 mle.TlvType.ACTIVE_OPERATIONAL_DATASET: mle.ActiveOperationalDatasetFactory(), 293 mle.TlvType.PENDING_OPERATIONAL_DATASET: mle.PendingOperationalDatasetFactory(), 294 mle.TlvType.TIME_REQUEST: mle.TimeRequestFactory(), 295 mle.TlvType.TIME_PARAMETER: mle.TimeParameterFactory(), 296 mle.TlvType.THREAD_DISCOVERY: create_default_mle_tlv_thread_discovery_factory(), 297 mle.TlvType.LINK_METRICS_QUERY: mle.LinkMetricsQueryFactory(), 298 mle.TlvType.LINK_METRICS_MANAGEMENT: mle.LinkMetricsManagementFactory(), 299 mle.TlvType.LINK_METRICS_REPORT: mle.LinkMetricsReportFactory(), 300 mle.TlvType.LINK_PROBE: mle.LinkProbeFactory(), 301 } 302 303 304def create_default_mle_crypto_engine(network_key): 305 return net_crypto.CryptoEngine(crypto_material_creator=net_crypto.MleCryptoMaterialCreator(network_key)) 306 307 308def create_default_mle_message_factory(network_key): 309 return mle.MleMessageFactory( 310 aux_sec_hdr_factory=net_crypto.AuxiliarySecurityHeaderFactory(), 311 mle_command_factory=mle.MleCommandFactory(tlvs_factories=create_default_mle_tlvs_factories()), 312 crypto_engine=create_default_mle_crypto_engine(network_key), 313 ) 314 315 316def create_deafult_network_tlvs_factories(): 317 return { 318 network_layer.TlvType.TARGET_EID: 319 network_layer.TargetEidFactory(), 320 network_layer.TlvType.MAC_EXTENDED_ADDRESS: 321 network_layer.MacExtendedAddressFactory(), 322 network_layer.TlvType.RLOC16: 323 network_layer.Rloc16Factory(), 324 network_layer.TlvType.ML_EID: 325 network_layer.MlEidFactory(), 326 network_layer.TlvType.STATUS: 327 network_layer.StatusFactory(), 328 network_layer.TlvType.TIME_SINCE_LAST_TRANSACTION: 329 network_layer.TimeSinceLastTransactionFactory(), 330 network_layer.TlvType.ROUTER_MASK: 331 network_layer.RouterMaskFactory(), 332 network_layer.TlvType.ND_OPTION: 333 network_layer.NdOptionFactory(), 334 network_layer.TlvType.ND_DATA: 335 network_layer.NdDataFactory(), 336 network_layer.TlvType.THREAD_NETWORK_DATA: 337 network_layer.ThreadNetworkDataFactory(create_default_network_data_tlvs_factory()), 338 network_layer.TlvType.XTAL_ACCURACY: 339 network_layer.XtalAccuracyFactory(), 340 # Routing information are distributed in a Thread network by MLE Routing TLV 341 # which is in fact MLE Route64 TLV. Thread specification v1.1. - Chapter 5.20 342 network_layer.TlvType.MLE_ROUTING: 343 create_default_mle_tlv_route64_factory(), 344 network_layer.TlvType.IPv6_ADDRESSES: 345 network_layer.IPv6AddressesFactory(), 346 } 347 348 349def create_default_network_tlvs_factory(): 350 return SubTlvsFactory(sub_tlvs_factories=create_deafult_network_tlvs_factories()) 351 352 353def create_default_mesh_cop_tlvs_factories(): 354 return { 355 mesh_cop.TlvType.CHANNEL: mesh_cop.ChannelFactory(), 356 mesh_cop.TlvType.PAN_ID: mesh_cop.PanidFactory(), 357 mesh_cop.TlvType.EXTENDED_PANID: mesh_cop.ExtendedPanidFactory(), 358 mesh_cop.TlvType.NETWORK_NAME: mesh_cop.NetworkNameFactory(), 359 mesh_cop.TlvType.PSKC: mesh_cop.PSKcFactory(), 360 mesh_cop.TlvType.NETWORK_KEY: mesh_cop.NetworkKeyFactory(), 361 mesh_cop.TlvType.NETWORK_KEY_SEQUENCE_COUNTER: mesh_cop.NetworkKeySequenceCounterFactory(), 362 mesh_cop.TlvType.NETWORK_MESH_LOCAL_PREFIX: mesh_cop.NetworkMeshLocalPrefixFactory(), 363 mesh_cop.TlvType.STEERING_DATA: mesh_cop.SteeringDataFactory(), 364 mesh_cop.TlvType.BORDER_AGENT_LOCATOR: mesh_cop.BorderAgentLocatorFactory(), 365 mesh_cop.TlvType.COMMISSIONER_ID: mesh_cop.CommissionerIdFactory(), 366 mesh_cop.TlvType.COMMISSIONER_SESSION_ID: mesh_cop.CommissionerSessionIdFactory(), 367 mesh_cop.TlvType.SECURITY_POLICY: mesh_cop.SecurityPolicyFactory(), 368 mesh_cop.TlvType.GET: mesh_cop.GetFactory(), 369 mesh_cop.TlvType.ACTIVE_TIMESTAMP: mesh_cop.ActiveTimestampFactory(), 370 mesh_cop.TlvType.COMMISSIONER_UDP_PORT: mesh_cop.CommissionerUdpPortFactory(), 371 mesh_cop.TlvType.STATE: mesh_cop.StateFactory(), 372 mesh_cop.TlvType.JOINER_DTLS_ENCAPSULATION: mesh_cop.JoinerDtlsEncapsulationFactory(), 373 mesh_cop.TlvType.JOINER_UDP_PORT: mesh_cop.JoinerUdpPortFactory(), 374 mesh_cop.TlvType.JOINER_IID: mesh_cop.JoinerIIDFactory(), 375 mesh_cop.TlvType.JOINER_ROUTER_LOCATOR: mesh_cop.JoinerRouterLocatorFactory(), 376 mesh_cop.TlvType.JOINER_ROUTER_KEK: mesh_cop.JoinerRouterKEKFactory(), 377 mesh_cop.TlvType.PROVISIONING_URL: mesh_cop.ProvisioningUrlFactory(), 378 mesh_cop.TlvType.VENDOR_NAME: mesh_cop.VendorNameFactory(), 379 mesh_cop.TlvType.VENDOR_MODEL: mesh_cop.VendorModelFactory(), 380 mesh_cop.TlvType.VENDOR_SW_VERSION: mesh_cop.VendorSWVersionFactory(), 381 mesh_cop.TlvType.VENDOR_DATA: mesh_cop.VendorDataFactory(), 382 mesh_cop.TlvType.VENDOR_STACK_VERSION: mesh_cop.VendorStackVersionFactory(), 383 mesh_cop.TlvType.UDP_ENCAPSULATION: mesh_cop.UdpEncapsulationFactory(), 384 mesh_cop.TlvType.IPV6_ADDRESS: mesh_cop.Ipv6AddressFactory(), 385 mesh_cop.TlvType.PENDING_TIMESTAMP: mesh_cop.PendingTimestampFactory(), 386 mesh_cop.TlvType.DELAY_TIMER: mesh_cop.DelayTimerFactory(), 387 mesh_cop.TlvType.CHANNEL_MASK: mesh_cop.ChannelMaskFactory(), 388 mesh_cop.TlvType.COUNT: mesh_cop.CountFactory(), 389 mesh_cop.TlvType.PERIOD: mesh_cop.PeriodFactory(), 390 mesh_cop.TlvType.SCAN_DURATION: mesh_cop.ScanDurationFactory(), 391 mesh_cop.TlvType.ENERGY_LIST: mesh_cop.EnergyListFactory(), 392 } 393 394 395def create_default_mesh_cop_tlvs_factory(): 396 return SubTlvsFactory(sub_tlvs_factories=create_default_mesh_cop_tlvs_factories()) 397 398 399def create_default_network_diag_tlv_factories(): 400 return { 401 network_diag.TlvType.EXT_ADDRESS: network_layer.MacExtendedAddressFactory(), 402 network_diag.TlvType.ADDRESS16: mle.Address16Factory(), 403 network_diag.TlvType.MODE: mle.ModeFactory(), 404 network_diag.TlvType.POLLING_PERIOD: mle.TimeoutFactory(), 405 network_diag.TlvType.CONNECTIVITY: mle.ConnectivityFactory(), 406 network_diag.TlvType.ROUTE64: create_default_mle_tlv_route64_factory(), 407 network_diag.TlvType.LEADER_DATA: mle.LeaderDataFactory(), 408 network_diag.TlvType.NETWORK_DATA: create_default_mle_tlv_network_data_factory(), 409 network_diag.TlvType.IPV6_ADDRESS_LIST: network_diag.Ipv6AddressListFactory(), 410 network_diag.TlvType.MAC_COUNTERS: network_diag.MacCountersFactory(), 411 network_diag.TlvType.BATTERY_LEVEL: network_diag.BatteryLevelFactory(), 412 network_diag.TlvType.SUPPLY_VOLTAGE: network_diag.SupplyVoltageFactory(), 413 network_diag.TlvType.CHILD_TABLE: network_diag.ChildTableFactory(), 414 network_diag.TlvType.CHANNEL_PAGES: network_diag.ChannelPagesFactory(), 415 network_diag.TlvType.TYPE_LIST: network_diag.TypeListFactory(), 416 network_diag.TlvType.MAX_CHILD_TIMEOUT: network_diag.MaxChildTimeoutFactory() 417 } 418 419 420def create_default_network_diag_tlvs_factory(): 421 return SubTlvsFactory(sub_tlvs_factories=create_default_network_diag_tlv_factories()) 422 423 424def create_default_uri_path_based_payload_factories(): 425 network_layer_tlvs_factory = create_default_network_tlvs_factory() 426 mesh_cop_tlvs_factory = create_default_mesh_cop_tlvs_factory() 427 network_diag_tlvs_factory = create_default_network_diag_tlvs_factory() 428 429 return { 430 '/a/as': network_layer_tlvs_factory, 431 '/a/aq': network_layer_tlvs_factory, 432 '/a/ar': network_layer_tlvs_factory, 433 '/a/ae': network_layer_tlvs_factory, 434 '/a/an': network_layer_tlvs_factory, 435 '/a/sd': network_layer_tlvs_factory, 436 '/c/lp': mesh_cop_tlvs_factory, 437 '/c/cs': mesh_cop_tlvs_factory, 438 '/d/da': network_diag_tlvs_factory, 439 '/d/dg': network_diag_tlvs_factory, 440 '/d/dq': network_diag_tlvs_factory, 441 '/d/dr': network_diag_tlvs_factory, 442 '/n/mr': network_layer_tlvs_factory, 443 '/n/dr': network_layer_tlvs_factory, 444 '/n/dn': network_layer_tlvs_factory, 445 } 446 447 448def create_default_coap_message_factory(): 449 return coap.CoapMessageFactory( 450 options_factory=coap.CoapOptionsFactory(), 451 uri_path_based_payload_factories=create_default_uri_path_based_payload_factories(), 452 message_id_to_uri_path_binder=coap.CoapMessageIdToUriPathBinder(), 453 ) 454 455 456def create_default_ipv6_hop_by_hop_options_factories(): 457 return {109: ipv6.MPLOptionFactory()} 458 459 460def create_default_ipv6_hop_by_hop_options_factory(): 461 return ipv6.HopByHopOptionsFactory(options_factories=create_default_ipv6_hop_by_hop_options_factories()) 462 463 464def create_default_based_on_src_dst_ports_udp_payload_factory(network_key): 465 mle_message_factory = create_default_mle_message_factory(network_key) 466 coap_message_factory = create_default_coap_message_factory() 467 dtls_message_factory = create_default_dtls_message_factory() 468 469 return ipv6.UdpBasedOnSrcDstPortsPayloadFactory( 470 src_dst_port_based_payload_factories={ 471 19788: mle_message_factory, 472 61631: coap_message_factory, 473 1000: dtls_message_factory, 474 5683: coap_message_factory, 475 5684: dtls_message_factory, 476 }) 477 478 479def create_default_dtls_message_factory(): 480 return dtls.MessageFactory() 481 482 483def create_default_ipv6_icmp_body_factories(): 484 return { 485 ipv6.ICMP_DESTINATION_UNREACHABLE: ipv6.ICMPv6DestinationUnreachableFactory(), 486 ipv6.ICMP_ECHO_REQUEST: ipv6.ICMPv6EchoBodyFactory(), 487 ipv6.ICMP_ECHO_RESPONSE: ipv6.ICMPv6EchoBodyFactory(), 488 'default': ipv6.BytesPayloadFactory(), 489 } 490 491 492def create_default_ipv6_upper_layer_factories(network_key): 493 return { 494 ipv6.IPV6_NEXT_HEADER_UDP: 495 ipv6.UDPDatagramFactory( 496 udp_header_factory=ipv6.UDPHeaderFactory(), 497 udp_payload_factory=create_default_based_on_src_dst_ports_udp_payload_factory(network_key), 498 ), 499 ipv6.IPV6_NEXT_HEADER_ICMP: 500 ipv6.ICMPv6Factory(body_factories=create_default_ipv6_icmp_body_factories()), 501 } 502 503 504def create_default_lowpan_extension_headers_factories(): 505 return { 506 ipv6.IPV6_NEXT_HEADER_HOP_BY_HOP: 507 lowpan.LowpanHopByHopFactory(hop_by_hop_options_factory=create_default_ipv6_hop_by_hop_options_factory()) 508 } 509 510 511def create_default_ipv6_extension_headers_factories(): 512 return { 513 ipv6.IPV6_NEXT_HEADER_HOP_BY_HOP: 514 ipv6.HopByHopFactory(hop_by_hop_options_factory=create_default_ipv6_hop_by_hop_options_factory()) 515 } 516 517 518def create_default_ipv6_packet_factory(network_key): 519 return ipv6.IPv6PacketFactory( 520 ehf=create_default_ipv6_extension_headers_factories(), 521 ulpf=create_default_ipv6_upper_layer_factories(network_key), 522 ) 523 524 525def create_default_lowpan_decompressor(context_manager): 526 return lowpan.LowpanDecompressor( 527 lowpan_ip_header_factory=lowpan.LowpanIpv6HeaderFactory(context_manager=context_manager), 528 lowpan_extension_headers_factory=lowpan.LowpanExtensionHeadersFactory( 529 ext_headers_factories=create_default_lowpan_extension_headers_factories()), 530 lowpan_udp_header_factory=lowpan.LowpanUdpHeaderFactory(), 531 ) 532 533 534def create_default_thread_context_manager(): 535 context_manager = lowpan.ContextManager() 536 context_manager[0] = lowpan.Context(MESH_LOCAL_PREFIX) 537 538 return context_manager 539 540 541def create_default_lowpan_parser(context_manager, network_key=DEFAULT_NETWORK_KEY): 542 return lowpan.LowpanParser( 543 lowpan_mesh_header_factory=lowpan.LowpanMeshHeaderFactory(), 544 lowpan_decompressor=create_default_lowpan_decompressor(context_manager), 545 lowpan_fragements_buffers_manager=lowpan.LowpanFragmentsBuffersManager(), 546 ipv6_packet_factory=create_default_ipv6_packet_factory(network_key), 547 ) 548 549 550def create_default_thread_message_factory(network_key=DEFAULT_NETWORK_KEY): 551 context_manager = create_default_thread_context_manager() 552 lowpan_parser = create_default_lowpan_parser(context_manager, network_key) 553 554 return message.MessageFactory(lowpan_parser=lowpan_parser) 555 556 557def create_default_thread_sniffer(use_message_factory=True): 558 return sniffer.Sniffer(create_default_thread_message_factory() if use_message_factory else None) 559 560 561def create_default_simulator(use_message_factory=True): 562 if VIRTUAL_TIME: 563 return simulator.VirtualTime(use_message_factory=use_message_factory) 564 return simulator.RealTime(use_message_factory=use_message_factory) 565