p4lang / ptf

Packet Test Framework
Apache License 2.0
144 stars 99 forks source link

dhcp_discover_packet fills incorrect client mac address in Discover packet #174

Open ZhaohuiS opened 2 years ago

ZhaohuiS commented 2 years ago

PTF version: 0.9.3 Python:3.5

issue: Run testutils.dhcp_discover_packet to send DHCP discover packet with testutils.dhcp_discover_packet(eth_client="fe:54:00:1c:7b:01", set_broadcast_bit=True), the client mac addr turns out to be c3be54001c7b I found it fills incorrect client mac address in Discover packet.

The root cause is that it still uses str for chaddr not bytes type. Can someone have a took for this issue? I provide the fix in last, please correct me if I am wrong. Thanks.

>>> import ptf.testutils as testutils
/env-python3/lib/python3.5/site-packages/scapy/config.py:520: CryptographyDeprecationWarning: Python 3.5 support will be dropped in the next release of cryptography. Please upgrade your Python.
  import cryptography
Using packet manipulation module: ptf.packet_scapy
>>> testutils.dhcp_discover_packet(eth_client="fe:54:00:1c:7b:01", set_broadcast_bit=True) 
<Ether  dst=ff:ff:ff:ff:ff:ff src=fe:54:00:1c:7b:01 type=IPv4 |<IP  frag=0 proto=udp src=0.0.0.0 dst=255.255.255.255 |<UDP  sport=bootpc dport=bootps |<BOOTP  op=BOOTREQUEST htype=1 hlen=6 hops=0 xid=0 secs=0 flags=B ciaddr=0.0.0.0 yiaddr=0.0.0.0 siaddr=0.0.0.0 giaddr=0.0.0.0 chaddr='þT\x00\x1c{\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' options='c\\x82Sc' |<DHCP  options=[message-type='discover' end] |>>>>>
>>> chaddr='þT\x00\x1c{\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> chaddr.encode('utf-8').hex()
'c3be54001c7b0100000000000000000000'
>>> 

image

solution: Change __dhcp_mac_to_chaddr as below to fix this issue.

def __dhcp_mac_to_chaddr(mac_addr="00:01:02:03:04:05"):
    """
    Private helper function to convert a 6-byte MAC address of form:
      '00:01:02:03:04:05'
    into a 16-byte chaddr byte string of form:
      '\x00\x01\x02\x03\x04\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

    """
    # chaddr = "".join([chr(int(octet, 16)) for octet in mac_addr.split(":")])
    # chaddr += "\x00" * 10
    import binascii
    chaddr = binascii.unhexlify(mac_addr.replace(':', ''))
    chaddr += b'\x00\x00\x00\x00\x00\x00'
    return chaddr

Then it prints out the correct client mac address:

>>> import ptf.testutils as testutils
/env-python3/lib/python3.5/site-packages/scapy/config.py:520: CryptographyDeprecationWarning: Python 3.5 support will be dropped in the next release of cryptography. Please upgrade your Python.
  import cryptography
Using packet manipulation module: ptf.packet_scapy
>>> testutils.dhcp_discover_packet(eth_client="fe:54:00:1c:7b:01", set_broadcast_bit=True)
<Ether  dst=ff:ff:ff:ff:ff:ff src=fe:54:00:1c:7b:01 type=IPv4 |<IP  frag=0 proto=udp src=0.0.0.0 dst=255.255.255.255 |<UDP  sport=bootpc dport=bootps |<BOOTP  op=BOOTREQUEST htype=1 hlen=6 hops=0 xid=0 secs=0 flags=B ciaddr=0.0.0.0 yiaddr=0.0.0.0 siaddr=0.0.0.0 giaddr=0.0.0.0 chaddr=b'\xfeT\x00\x1c{\x01\x00\x00\x00\x00\x00\x00' options='c\\x82Sc' |<DHCP  options=[message-type='discover' end] |>>>>>
>>> chaddr=b'\xfeT\x00\x1c{\x01\x00\x00\x00\x00\x00\x00'
>>> chaddr.hex()
'fe54001c7b01000000000000'