xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/pktverify/layers.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2019, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Workerimport logging
30*cfb92d14SAndroid Build Coastguard Workerfrom typing import Optional
31*cfb92d14SAndroid Build Coastguard Worker
32*cfb92d14SAndroid Build Coastguard Workerfrom pyshark.packet.fields import LayerField, LayerFieldsContainer
33*cfb92d14SAndroid Build Coastguard Workerfrom pyshark.packet.layer import Layer as RawLayer
34*cfb92d14SAndroid Build Coastguard Workerfrom pyshark.packet.packet import Packet as RawPacket
35*cfb92d14SAndroid Build Coastguard Worker
36*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.layer_fields import get_layer_field, check_layer_field_exists
37*cfb92d14SAndroid Build Coastguard Worker
38*cfb92d14SAndroid Build Coastguard Worker
39*cfb92d14SAndroid Build Coastguard Workerclass Layer(object):
40*cfb92d14SAndroid Build Coastguard Worker    """
41*cfb92d14SAndroid Build Coastguard Worker    Represents a layer of a packet.
42*cfb92d14SAndroid Build Coastguard Worker    """
43*cfb92d14SAndroid Build Coastguard Worker
44*cfb92d14SAndroid Build Coastguard Worker    def __init__(self, packet: RawPacket, layer_name: str):
45*cfb92d14SAndroid Build Coastguard Worker        assert isinstance(packet, RawPacket)
46*cfb92d14SAndroid Build Coastguard Worker        assert isinstance(layer_name, str)
47*cfb92d14SAndroid Build Coastguard Worker        self._packet = packet
48*cfb92d14SAndroid Build Coastguard Worker        self._layer_name = layer_name
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Worker    @property
51*cfb92d14SAndroid Build Coastguard Worker    def _layer(self) -> Optional[RawLayer]:
52*cfb92d14SAndroid Build Coastguard Worker        try:
53*cfb92d14SAndroid Build Coastguard Worker            return getattr(self._packet, self._layer_name)
54*cfb92d14SAndroid Build Coastguard Worker        except AttributeError:
55*cfb92d14SAndroid Build Coastguard Worker            return None
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Worker    @property
58*cfb92d14SAndroid Build Coastguard Worker    def layer_name(self) -> str:
59*cfb92d14SAndroid Build Coastguard Worker        """
60*cfb92d14SAndroid Build Coastguard Worker        Returns the layer name.
61*cfb92d14SAndroid Build Coastguard Worker        """
62*cfb92d14SAndroid Build Coastguard Worker        return self._layer_name
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Worker    def show(self):
65*cfb92d14SAndroid Build Coastguard Worker        """
66*cfb92d14SAndroid Build Coastguard Worker        Print the layer information.
67*cfb92d14SAndroid Build Coastguard Worker        """
68*cfb92d14SAndroid Build Coastguard Worker        print(self._layer)
69*cfb92d14SAndroid Build Coastguard Worker
70*cfb92d14SAndroid Build Coastguard Worker    def has(self, name) -> bool:
71*cfb92d14SAndroid Build Coastguard Worker        """
72*cfb92d14SAndroid Build Coastguard Worker        Returns if the layer has a given field.
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Worker        :param name: The field name.
75*cfb92d14SAndroid Build Coastguard Worker        """
76*cfb92d14SAndroid Build Coastguard Worker        path = '%s.%s' % (self.layer_name, name)
77*cfb92d14SAndroid Build Coastguard Worker        return check_layer_field_exists(self._packet, path)
78*cfb92d14SAndroid Build Coastguard Worker
79*cfb92d14SAndroid Build Coastguard Worker    def __bool__(self):
80*cfb92d14SAndroid Build Coastguard Worker        """
81*cfb92d14SAndroid Build Coastguard Worker        Returns if this layer exists in the packet.
82*cfb92d14SAndroid Build Coastguard Worker        """
83*cfb92d14SAndroid Build Coastguard Worker        layer_exists = hasattr(self._packet, self._layer_name)
84*cfb92d14SAndroid Build Coastguard Worker        return layer_exists
85*cfb92d14SAndroid Build Coastguard Worker
86*cfb92d14SAndroid Build Coastguard Worker    def __getattr__(self, name):
87*cfb92d14SAndroid Build Coastguard Worker        """
88*cfb92d14SAndroid Build Coastguard Worker        Returns the layer field or container of a given field name.
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Worker        :param name: The name of layer field or container.
91*cfb92d14SAndroid Build Coastguard Worker        """
92*cfb92d14SAndroid Build Coastguard Worker        path = '%s.%s' % (self.layer_name, name)
93*cfb92d14SAndroid Build Coastguard Worker        v = get_layer_field(self._packet, path)
94*cfb92d14SAndroid Build Coastguard Worker        assert not isinstance(v, (LayerField, LayerFieldsContainer)), '%s = %s(%r)' % (path, v.__class__.__name__, v)
95*cfb92d14SAndroid Build Coastguard Worker        setattr(self, name, v)
96*cfb92d14SAndroid Build Coastguard Worker        return v
97*cfb92d14SAndroid Build Coastguard Worker
98*cfb92d14SAndroid Build Coastguard Worker    def _add_field(self, key: str, val: str):
99*cfb92d14SAndroid Build Coastguard Worker        logging.debug("layer %s add field: %s = %s", self.layer_name, key, val)
100*cfb92d14SAndroid Build Coastguard Worker        field = LayerField(name=key, value=val)
101*cfb92d14SAndroid Build Coastguard Worker        all_fields = self._layer._all_fields
102*cfb92d14SAndroid Build Coastguard Worker        if key not in all_fields:
103*cfb92d14SAndroid Build Coastguard Worker            all_fields[key] = LayerFieldsContainer(main_field=field)
104*cfb92d14SAndroid Build Coastguard Worker        else:
105*cfb92d14SAndroid Build Coastguard Worker            all_fields[key].fields.append(field)
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Workerclass ThreadMeshcopLayer(Layer):
109*cfb92d14SAndroid Build Coastguard Worker    """
110*cfb92d14SAndroid Build Coastguard Worker    Represents the Thread MeshCop layer of a packet.
111*cfb92d14SAndroid Build Coastguard Worker    """
112*cfb92d14SAndroid Build Coastguard Worker
113*cfb92d14SAndroid Build Coastguard Worker    def __bool__(self):
114*cfb92d14SAndroid Build Coastguard Worker        raise NotImplementedError("thread_meshcop is not a real layer, please do not check as bool")
115*cfb92d14SAndroid Build Coastguard Worker
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Workerclass ThreadNetworkDataLayer(Layer):
118*cfb92d14SAndroid Build Coastguard Worker    """
119*cfb92d14SAndroid Build Coastguard Worker    Represents the Thread NetworkData layer of a packet.
120*cfb92d14SAndroid Build Coastguard Worker    """
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker    def __bool__(self):
123*cfb92d14SAndroid Build Coastguard Worker        raise NotImplementedError("thread_nwd is not a real layer, please do not check as bool")
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker
126*cfb92d14SAndroid Build Coastguard Workerclass Icmpv6Layer(Layer):
127*cfb92d14SAndroid Build Coastguard Worker    """
128*cfb92d14SAndroid Build Coastguard Worker    Represents the ICMPv6 layer of a packet.
129*cfb92d14SAndroid Build Coastguard Worker    """
130*cfb92d14SAndroid Build Coastguard Worker
131*cfb92d14SAndroid Build Coastguard Worker    @property
132*cfb92d14SAndroid Build Coastguard Worker    def is_ping(self) -> bool:
133*cfb92d14SAndroid Build Coastguard Worker        """
134*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Ping Request or Reply.
135*cfb92d14SAndroid Build Coastguard Worker        """
136*cfb92d14SAndroid Build Coastguard Worker        return self.type in (128, 129)
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker    @property
139*cfb92d14SAndroid Build Coastguard Worker    def is_ping_request(self) -> bool:
140*cfb92d14SAndroid Build Coastguard Worker        """
141*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Ping Request.
142*cfb92d14SAndroid Build Coastguard Worker        """
143*cfb92d14SAndroid Build Coastguard Worker        return self.type == 128
144*cfb92d14SAndroid Build Coastguard Worker
145*cfb92d14SAndroid Build Coastguard Worker    @property
146*cfb92d14SAndroid Build Coastguard Worker    def is_ping_reply(self) -> bool:
147*cfb92d14SAndroid Build Coastguard Worker        """
148*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Ping Reply.
149*cfb92d14SAndroid Build Coastguard Worker        """
150*cfb92d14SAndroid Build Coastguard Worker        return self.type == 129
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Worker    @property
153*cfb92d14SAndroid Build Coastguard Worker    def is_neighbor_advertisement(self) -> bool:
154*cfb92d14SAndroid Build Coastguard Worker        """
155*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Neighbor Advertisement.
156*cfb92d14SAndroid Build Coastguard Worker        """
157*cfb92d14SAndroid Build Coastguard Worker        return self.type == 136
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Worker    @property
160*cfb92d14SAndroid Build Coastguard Worker    def is_neighbor_solicitation(self) -> bool:
161*cfb92d14SAndroid Build Coastguard Worker        """
162*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Neighbor Solicitation.
163*cfb92d14SAndroid Build Coastguard Worker        """
164*cfb92d14SAndroid Build Coastguard Worker        return self.type == 135
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Worker    @property
167*cfb92d14SAndroid Build Coastguard Worker    def is_router_advertisement(self) -> bool:
168*cfb92d14SAndroid Build Coastguard Worker        """
169*cfb92d14SAndroid Build Coastguard Worker        Returns if the ICMPv6 layer is a Router Advertisement.
170*cfb92d14SAndroid Build Coastguard Worker        """
171*cfb92d14SAndroid Build Coastguard Worker        return self.type == 134
172*cfb92d14SAndroid Build Coastguard Worker
173*cfb92d14SAndroid Build Coastguard Worker
174*cfb92d14SAndroid Build Coastguard Workerclass WpanLayer(Layer):
175*cfb92d14SAndroid Build Coastguard Worker    """
176*cfb92d14SAndroid Build Coastguard Worker    Represents the WPAN layer of a packet.
177*cfb92d14SAndroid Build Coastguard Worker    """
178*cfb92d14SAndroid Build Coastguard Worker
179*cfb92d14SAndroid Build Coastguard Worker    @property
180*cfb92d14SAndroid Build Coastguard Worker    def is_ack(self) -> bool:
181*cfb92d14SAndroid Build Coastguard Worker        return self.frame_type == 0x2
182*cfb92d14SAndroid Build Coastguard Worker
183*cfb92d14SAndroid Build Coastguard Worker
184*cfb92d14SAndroid Build Coastguard Workerclass DnsLayer(Layer):
185*cfb92d14SAndroid Build Coastguard Worker    """
186*cfb92d14SAndroid Build Coastguard Worker    Represents the DNS layer of a packet.
187*cfb92d14SAndroid Build Coastguard Worker    """
188*cfb92d14SAndroid Build Coastguard Worker    pass
189