Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dhcp_discover_packet fills incorrect client mac address in Discover packet #174

Open
ZhaohuiS opened this issue Apr 11, 2022 · 0 comments
Open

Comments

@ZhaohuiS
Copy link

ZhaohuiS commented Apr 11, 2022

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'
ZhaohuiS added a commit to sonic-net/sonic-buildimage that referenced this issue Apr 26, 2022
Why I did it
Migrate ptftests script to python3, in order to do an incremental migration, add python virtual environment firstly, install all required python packages in virtual env as well.
Then migrate ptftests scripts from python2 to python3 one by one avoid impacting non-changed scripts.

Signed-off-by: Zhaohui Sun zhaohuisun@microsoft.com

How I did it
Add python3 virtual environment for docker-ptf.
Add submodule ptf-py3 and install patched ptf 0.9.3 into virtual environment as well, two ptf issues were reported here:
p4lang/ptf#173
p4lang/ptf#174

Signed-off-by: Zhaohui Sun <zhaohuisun@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant