1#!/usr/bin/env python3
2#
3#  Copyright (c) 2020, 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 logging
30import unittest
31
32import config
33import thread_cert
34# Test description: Here is the test case `5.11.1 DUA-TC-04: DUA re-registration`
35#
36# Topology:
37#    -----------(eth)----------------
38#                  |     |
39#     Router_1----BR_1---HOST
40#        \     /
41#        Router_2
42#
43from pktverify.packet_verifier import PacketVerifier
44
45BR_1 = 1
46ROUTER1 = 2
47ROUTER2 = 3
48HOST = 4
49
50
51class BBR_5_11_01(thread_cert.TestCase):
52    USE_MESSAGE_FACTORY = False
53
54    TOPOLOGY = {
55        BR_1: {
56            'name': 'BR_1',
57            'allowlist': [ROUTER1, ROUTER2],
58            'is_otbr': True,
59            'version': '1.2',
60        },
61        ROUTER1: {
62            'name': 'Router_1',
63            'allowlist': [ROUTER2, BR_1],
64            'version': '1.2',
65        },
66        ROUTER2: {
67            'name': 'Router_2',
68            'allowlist': [ROUTER1, BR_1],
69            'version': '1.2',
70        },
71        HOST: {
72            'name': 'Host',
73            'is_host': True
74        },
75    }
76
77    def test(self):
78        self.nodes[HOST].start()
79        # P1: Router_1 is configured with leader weight of 72 in case the test is executed on a CCM network
80
81        self.nodes[ROUTER1].set_weight(72)
82        self.nodes[ROUTER1].start()
83
84        self.simulator.go(config.LEADER_STARTUP_DELAY)
85        self.assertEqual('leader', self.nodes[ROUTER1].get_state())
86
87        self.nodes[ROUTER2].start()
88        self.simulator.go(5)
89        self.assertEqual('router', self.nodes[ROUTER2].get_state())
90
91        # The OTBR docker enables SRP Server by default, lets explicitly
92        # disable SRP server to avoid Network Data population.
93        # TODO: Enhance the test script to tolerate additional Sertivce TLV
94        # in Network Data.
95        self.nodes[BR_1].srp_server_set_enabled(False)
96        self.nodes[BR_1].start()
97        self.simulator.go(5)
98        self.assertEqual('router', self.nodes[BR_1].get_state())
99        self.nodes[BR_1].enable_backbone_router()
100        self.simulator.go(3)
101        self.assertTrue(self.nodes[BR_1].is_primary_backbone_router)
102        self.nodes[BR_1].add_prefix(config.DOMAIN_PREFIX, "parosD")
103        self.nodes[BR_1].register_netdata()
104
105        self.simulator.go(5)
106        self.assertIsNotNone(self.nodes[ROUTER2].get_ip6_address(config.ADDRESS_TYPE.DUA))
107
108        self.simulator.go(10)  # must wait for DUA_DAD_REPEATS to complete
109        logging.info("Host addresses: %r", self.nodes[HOST].get_addrs())
110        self.assertGreaterEqual(len(self.nodes[HOST].get_addrs()), 2)
111
112        self.collect_ipaddrs()
113        self.collect_rloc16s()
114        Dg = self.nodes[ROUTER2].get_ip6_address(config.ADDRESS_TYPE.DUA)
115        self.collect_extra_vars(Dg=Dg)
116
117        logging.info("BR_1 addrs: %r", self.nodes[BR_1].get_addrs())
118        logging.info("Host addrs: %r", self.nodes[HOST].get_addrs())
119
120        # BR_1 and Host can ping each other on the Backbone link
121        self.assertTrue(self.nodes[HOST].ping(self.nodes[BR_1].get_ip6_address(config.ADDRESS_TYPE.BACKBONE_GUA),
122                                              backbone=True))
123        self.assertTrue(self.nodes[BR_1].ping(self.nodes[HOST].get_ip6_address(config.ADDRESS_TYPE.BACKBONE_GUA),
124                                              backbone=True))
125
126        # Step 23: Host sends ping packet to destination Dg (successful if DUA features work)
127        self.assertTrue(self.nodes[HOST].ping(Dg, backbone=True))
128
129    def verify(self, pv: PacketVerifier):
130        pkts = pv.pkts
131        pv.add_common_vars()
132        pv.summary.show()
133        pv.verify_attached('BR_1')
134
135        MM = pv.vars['MM_PORT']
136        BB = pv.vars['BB_PORT']
137        BR_1 = pv.vars['BR_1']
138        BR_1_ETH = pv.vars['BR_1_ETH']
139        Host_ETH = pv.vars['Host_ETH']
140        BR_1_BGUA = pv.vars['BR_1_BGUA']
141        Host_BGUA = pv.vars['Host_BGUA']
142        Dg = pv.vars['Dg']  # DUA of Router_2
143        ROUTER2 = pv.vars['Router_2']
144
145        # Step 3: BR_1: Checks received Network Data and determines that it needs to send its BBR Dataset to the
146        #               leader to become primary BBR.
147        pkts.filter_wpan_src64(BR_1).filter_coap_request('/a/sd', port=MM).must_next().must_verify("""
148            thread_nwd.tlv.server_16 is not null
149            and thread_nwd.tlv.service.s_data.seqno is not null
150            and thread_nwd.tlv.service.s_data.rrdelay is not null
151            and thread_nwd.tlv.service.s_data.mlrtimeout is not null
152        """)
153
154        pv.verify_dua_registration(ROUTER2, Dg, pbbr_eth=BR_1_ETH, pbbr_src64=BR_1)
155
156        # Verify Host ping BBR
157        pkts.filter_eth_src(Host_ETH).filter_ipv6_src_dst(Host_BGUA, BR_1_BGUA).filter_ping_request().must_next()
158        pkts.filter_eth_src(BR_1_ETH).filter_ipv6_src_dst(BR_1_BGUA, Host_BGUA).filter_ping_reply().must_next()
159
160        # Verify BR_1 ping Host
161        pkts.filter_eth_src(BR_1_ETH).filter_ipv6_src_dst(BR_1_BGUA, Host_BGUA).filter_ping_request().must_next()
162        pkts.filter_eth_src(Host_ETH).filter_ipv6_src_dst(Host_BGUA, BR_1_BGUA).filter_ping_reply().must_next()
163
164        # Step 16: Host: Queries DUA, Dg, with ND-NS
165        pkts.filter_eth_src(Host_ETH).filter_icmpv6_nd_ns(Dg).must_next()
166
167        # Step 17: BR_1: Responds with a neighbor advertisement.
168        pkts.filter_eth_src(BR_1_ETH).filter_icmpv6_nd_na(Dg).must_next()
169
170
171if __name__ == '__main__':
172    unittest.main()
173