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