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 Workerimport sys 31*cfb92d14SAndroid Build Coastguard Workerfrom typing import Iterable, List, Union, Callable 32*cfb92d14SAndroid Build Coastguard Worker 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 import errors 37*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.addrs import EthAddr 38*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.coap import CoapLayer 39*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.consts import VALID_LAYER_NAMES 40*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.decorators import cached_property 41*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.layers import Layer, ThreadMeshcopLayer, Icmpv6Layer, WpanLayer, ThreadNetworkDataLayer, DnsLayer 42*cfb92d14SAndroid Build Coastguard Workerfrom pktverify.utils import make_filter_func 43*cfb92d14SAndroid Build Coastguard Worker 44*cfb92d14SAndroid Build Coastguard Worker 45*cfb92d14SAndroid Build Coastguard Workerclass Packet(object): 46*cfb92d14SAndroid Build Coastguard Worker 47*cfb92d14SAndroid Build Coastguard Worker def __init__(self, packet: RawPacket): 48*cfb92d14SAndroid Build Coastguard Worker self._packet = packet 49*cfb92d14SAndroid Build Coastguard Worker self._strip_wpan_eth_wrapper(packet) 50*cfb92d14SAndroid Build Coastguard Worker 51*cfb92d14SAndroid Build Coastguard Worker def __str__(self) -> str: 52*cfb92d14SAndroid Build Coastguard Worker return str(self._packet) 53*cfb92d14SAndroid Build Coastguard Worker 54*cfb92d14SAndroid Build Coastguard Worker def __repr__(self) -> str: 55*cfb92d14SAndroid Build Coastguard Worker return repr(self._packet) 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Worker def __dir__(self) -> Iterable[str]: 58*cfb92d14SAndroid Build Coastguard Worker return dir(self._packet) 59*cfb92d14SAndroid Build Coastguard Worker 60*cfb92d14SAndroid Build Coastguard Worker def _strip_wpan_eth_wrapper(self, packet: RawPacket): 61*cfb92d14SAndroid Build Coastguard Worker if not hasattr(packet, 'wpan'): 62*cfb92d14SAndroid Build Coastguard Worker return 63*cfb92d14SAndroid Build Coastguard Worker 64*cfb92d14SAndroid Build Coastguard Worker for layer in packet.layers: 65*cfb92d14SAndroid Build Coastguard Worker if layer.layer_name == 'eth': 66*cfb92d14SAndroid Build Coastguard Worker packet.layers.remove(layer) 67*cfb92d14SAndroid Build Coastguard Worker eth_src = EthAddr(layer.get_field('eth.src')) 68*cfb92d14SAndroid Build Coastguard Worker eth_dst = EthAddr(layer.get_field('eth.dst')) 69*cfb92d14SAndroid Build Coastguard Worker logging.debug("stripping eth: src=%s, dst=%s", eth_src, eth_dst) 70*cfb92d14SAndroid Build Coastguard Worker channel = eth_src[5] 71*cfb92d14SAndroid Build Coastguard Worker self.wpan._add_field('wpan.channel', hex(channel)) 72*cfb92d14SAndroid Build Coastguard Worker 73*cfb92d14SAndroid Build Coastguard Worker return 74*cfb92d14SAndroid Build Coastguard Worker 75*cfb92d14SAndroid Build Coastguard Worker @property 76*cfb92d14SAndroid Build Coastguard Worker def layers(self) -> Iterable[RawLayer]: 77*cfb92d14SAndroid Build Coastguard Worker for l in self._packet.layers: 78*cfb92d14SAndroid Build Coastguard Worker if l.layer_name != 'data': 79*cfb92d14SAndroid Build Coastguard Worker yield getattr(self, l.layer_name) 80*cfb92d14SAndroid Build Coastguard Worker 81*cfb92d14SAndroid Build Coastguard Worker @property 82*cfb92d14SAndroid Build Coastguard Worker def layer_names(self) -> List[str]: 83*cfb92d14SAndroid Build Coastguard Worker return [l.layer_name for l in self._packet.layers if l.layer_name != 'data'] 84*cfb92d14SAndroid Build Coastguard Worker 85*cfb92d14SAndroid Build Coastguard Worker @cached_property 86*cfb92d14SAndroid Build Coastguard Worker def wpan(self) -> WpanLayer: 87*cfb92d14SAndroid Build Coastguard Worker return WpanLayer(self._packet, 'wpan') 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Worker @cached_property 90*cfb92d14SAndroid Build Coastguard Worker def coap(self) -> CoapLayer: 91*cfb92d14SAndroid Build Coastguard Worker return CoapLayer(self._packet, 'coap') 92*cfb92d14SAndroid Build Coastguard Worker 93*cfb92d14SAndroid Build Coastguard Worker @cached_property 94*cfb92d14SAndroid Build Coastguard Worker def icmpv6(self) -> Icmpv6Layer: 95*cfb92d14SAndroid Build Coastguard Worker return Icmpv6Layer(self._packet, 'icmpv6') 96*cfb92d14SAndroid Build Coastguard Worker 97*cfb92d14SAndroid Build Coastguard Worker @cached_property 98*cfb92d14SAndroid Build Coastguard Worker def thread_meshcop(self) -> ThreadMeshcopLayer: 99*cfb92d14SAndroid Build Coastguard Worker return ThreadMeshcopLayer(self._packet, 'thread_meshcop') 100*cfb92d14SAndroid Build Coastguard Worker 101*cfb92d14SAndroid Build Coastguard Worker @cached_property 102*cfb92d14SAndroid Build Coastguard Worker def thread_nwd(self) -> ThreadNetworkDataLayer: 103*cfb92d14SAndroid Build Coastguard Worker return ThreadNetworkDataLayer(self._packet, 'thread_nwd') 104*cfb92d14SAndroid Build Coastguard Worker 105*cfb92d14SAndroid Build Coastguard Worker @cached_property 106*cfb92d14SAndroid Build Coastguard Worker def dns(self) -> DnsLayer: 107*cfb92d14SAndroid Build Coastguard Worker return DnsLayer(self._packet, 'dns') 108*cfb92d14SAndroid Build Coastguard Worker 109*cfb92d14SAndroid Build Coastguard Worker def __getattr__(self, layer_name: str) -> Layer: 110*cfb92d14SAndroid Build Coastguard Worker 111*cfb92d14SAndroid Build Coastguard Worker real_layer_name = layer_name 112*cfb92d14SAndroid Build Coastguard Worker if layer_name == 'lowpan': 113*cfb92d14SAndroid Build Coastguard Worker real_layer_name = '6lowpan' 114*cfb92d14SAndroid Build Coastguard Worker 115*cfb92d14SAndroid Build Coastguard Worker assert real_layer_name in VALID_LAYER_NAMES, '%s is not a valid layer name' % real_layer_name 116*cfb92d14SAndroid Build Coastguard Worker _layer = getattr(self._packet, real_layer_name, None) 117*cfb92d14SAndroid Build Coastguard Worker assert _layer is None or isinstance(_layer, RawLayer) 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid Build Coastguard Worker layer = Layer(self._packet, real_layer_name) 120*cfb92d14SAndroid Build Coastguard Worker setattr(self, layer_name, layer) 121*cfb92d14SAndroid Build Coastguard Worker if real_layer_name != layer_name: 122*cfb92d14SAndroid Build Coastguard Worker setattr(self, real_layer_name, layer) 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Worker return layer 125*cfb92d14SAndroid Build Coastguard Worker 126*cfb92d14SAndroid Build Coastguard Worker def verify(self, func: Union[str, Callable], **vars) -> bool: 127*cfb92d14SAndroid Build Coastguard Worker print("\n>>> verifying packet:", file=sys.stderr, flush=False) 128*cfb92d14SAndroid Build Coastguard Worker func = make_filter_func(func, **vars) 129*cfb92d14SAndroid Build Coastguard Worker ok = func(self) 130*cfb92d14SAndroid Build Coastguard Worker print("\t=> %s" % ok, file=sys.stderr) 131*cfb92d14SAndroid Build Coastguard Worker return ok 132*cfb92d14SAndroid Build Coastguard Worker 133*cfb92d14SAndroid Build Coastguard Worker def must_verify(self, func: Union[str, Callable], **vars): 134*cfb92d14SAndroid Build Coastguard Worker if not self.verify(func, **vars): 135*cfb92d14SAndroid Build Coastguard Worker self.debug_fields() 136*cfb92d14SAndroid Build Coastguard Worker raise errors.VerifyFailed(self) 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Worker return self 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard Worker def must_not_verify(self, func: Union[str, Callable], **vars): 141*cfb92d14SAndroid Build Coastguard Worker if self.verify(func, **vars): 142*cfb92d14SAndroid Build Coastguard Worker raise errors.VerifyFailed(self) 143*cfb92d14SAndroid Build Coastguard Worker 144*cfb92d14SAndroid Build Coastguard Worker return self 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Worker @property 147*cfb92d14SAndroid Build Coastguard Worker def sniff_timestamp(self) -> float: 148*cfb92d14SAndroid Build Coastguard Worker return float(self._packet.sniff_timestamp) 149*cfb92d14SAndroid Build Coastguard Worker 150*cfb92d14SAndroid Build Coastguard Worker def show(self): 151*cfb92d14SAndroid Build Coastguard Worker self._packet.show() 152*cfb92d14SAndroid Build Coastguard Worker 153*cfb92d14SAndroid Build Coastguard Worker def debug_fields(self): 154*cfb92d14SAndroid Build Coastguard Worker for layer in self._packet.layers: 155*cfb92d14SAndroid Build Coastguard Worker print("### Layer %s ###" % layer.layer_name) 156*cfb92d14SAndroid Build Coastguard Worker for k, v in layer._all_fields.items(): 157*cfb92d14SAndroid Build Coastguard Worker print("\t\t%r = %r" % (k, v)) 158