python - IPv6 Hop-by-Hop Scapy: ValueError: Missing 'dst' attribute - Stack Overflow

admin2025-05-01  0

I'm experimenting with IPv6 Scapy and trying to set Router Alert Hop By Hop option. Here is the code sample:

hdr = IPv6ExtHdrHopByHop(options=[("Router Alert", b'\x01\x00')])
ip6 = IPv6(src="xxxx::xxxx", dst="yyyy::yyyy")
send(ip6 / hdr)

Execution of this code produces the following error:

Traceback (most recent call last):                                                                                                                                                                                 
  File "/home/sn/scapy/scapy/supersocket.py", line 391, in send                                                                                                                                               
    sx = raw(x)                                                                                                                                                                                                    
         ^^^^^^                                                                                                                                                                                                    
  File "/home/sn/scapy/scapy/compat.py", line 123, in raw
    return bytes(x)
           ^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 609, in __bytes__
    return self.build()
           ^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 768, in build
    p = self.do_build()
        ^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 751, in do_build
    pay = self.do_build_payload()
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 737, in do_build_payload
    return self.payload.do_build()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 748, in do_build
    pkt = self.self_build()
          ^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 727, in self_build
    raise ex
  File "/home/sn/scapy/scapy/packet.py", line 718, in self_build
    p = f.addfield(self, p, val)
        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/fields.py", line 240, in addfield
    return s + self.struct.pack(self.i2m(pkt, val))
                                ^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/fields.py", line 2201, in i2m
    f = fld.i2len(pkt, fval)
        ^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/layers/inet6.py", line 890, in i2len
    return len(self.i2m(pkt, i))
               ^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/layers/inet6.py", line 905, in i2m
    d = p.alignment_delta(curpos)
        ^^^^^^^^^^^^^^^^^
AttributeError: While dissecting field 'len': 'tuple' object has no attribute 'alignment_delta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/sn/scapy/scapy/sendrecv.py", line 486, in send
    return _send(
           ^^^^^^
  File "/home/sn/scapy/scapy/sendrecv.py", line 447, in _send
    results = __gen_send(socket, x, inter=inter, loop=loop,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/sendrecv.py", line 409, in __gen_send
    s.send(p)
  File "/home/sn/scapy/scapy/supersocket.py", line 399, in send
    raise ValueError(
ValueError: Missing 'dst' attribute in the first layer to be sent using a native L3 socket ! (make sure you passed the IP layer)

If I remove the options=[("Router Alert", b'\x01\x00')] and send essentially empty HopByHop extension header as

send (ip6 / IPv6ExtHdrHopByHop())

it works fine, but does not make a lot of sense. What's wrong with actually specifying the options?

I'm experimenting with IPv6 Scapy and trying to set Router Alert Hop By Hop option. Here is the code sample:

hdr = IPv6ExtHdrHopByHop(options=[("Router Alert", b'\x01\x00')])
ip6 = IPv6(src="xxxx::xxxx", dst="yyyy::yyyy")
send(ip6 / hdr)

Execution of this code produces the following error:

Traceback (most recent call last):                                                                                                                                                                                 
  File "/home/sn/scapy/scapy/supersocket.py", line 391, in send                                                                                                                                               
    sx = raw(x)                                                                                                                                                                                                    
         ^^^^^^                                                                                                                                                                                                    
  File "/home/sn/scapy/scapy/compat.py", line 123, in raw
    return bytes(x)
           ^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 609, in __bytes__
    return self.build()
           ^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 768, in build
    p = self.do_build()
        ^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 751, in do_build
    pay = self.do_build_payload()
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 737, in do_build_payload
    return self.payload.do_build()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 748, in do_build
    pkt = self.self_build()
          ^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/packet.py", line 727, in self_build
    raise ex
  File "/home/sn/scapy/scapy/packet.py", line 718, in self_build
    p = f.addfield(self, p, val)
        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/fields.py", line 240, in addfield
    return s + self.struct.pack(self.i2m(pkt, val))
                                ^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/fields.py", line 2201, in i2m
    f = fld.i2len(pkt, fval)
        ^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/layers/inet6.py", line 890, in i2len
    return len(self.i2m(pkt, i))
               ^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/layers/inet6.py", line 905, in i2m
    d = p.alignment_delta(curpos)
        ^^^^^^^^^^^^^^^^^
AttributeError: While dissecting field 'len': 'tuple' object has no attribute 'alignment_delta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/sn/scapy/scapy/sendrecv.py", line 486, in send
    return _send(
           ^^^^^^
  File "/home/sn/scapy/scapy/sendrecv.py", line 447, in _send
    results = __gen_send(socket, x, inter=inter, loop=loop,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sn/scapy/scapy/sendrecv.py", line 409, in __gen_send
    s.send(p)
  File "/home/sn/scapy/scapy/supersocket.py", line 399, in send
    raise ValueError(
ValueError: Missing 'dst' attribute in the first layer to be sent using a native L3 socket ! (make sure you passed the IP layer)

If I remove the options=[("Router Alert", b'\x01\x00')] and send essentially empty HopByHop extension header as

send (ip6 / IPv6ExtHdrHopByHop())

it works fine, but does not make a lot of sense. What's wrong with actually specifying the options?

Share Improve this question asked Jan 2 at 18:09 Some NameSome Name 9,5808 gold badges35 silver badges96 bronze badges 6
  • The required Option Type (8 bits) and Opt Data Len (8 bits) before the Option Data? Also remember that "the complete Hop-by-Hop Options header is an integer multiple of 8 octets long." – Ron Maupin Commented Jan 2 at 18:28
  • @RonMaupin Right, I was slightly misunderstanding the way to use options field. – Some Name Commented Jan 2 at 18:35
  • 1 Also, remember that the routers, especially in the public Internet, are free to ignore this option header. – Ron Maupin Commented Jan 2 at 19:28
  • @RonMaupin Definitely, RFC 8200 specifices that padding options should be recognised by all IPv6 implementations. Are there any other options mandatory for IPv6? – Some Name Commented Jan 2 at 19:37
  • 1 In RFC 6564: "Any IPv6 header or option that has hop-by-hop behavior, and is intended for general use in the public IPv6 Internet, could be subverted to create an attack on IPv6 routers that process packets containing such a header or option. Reports from the field indicate that some IP routers deployed within the global Internet are configured either to ignore the presence of headers with hop-by-hop behavior or to drop packets containing headers with hop-by-hop behavior." – Ron Maupin Commented Jan 2 at 20:15
 |  Show 1 more comment

1 Answer 1

Reset to default 0

The correct way to add RouterAlert option to a HopByHop extension header is to use RouterAlert class provided by scapy:

ip6 = IPv6(src="xxxx::xxxx", dst="yyyy::yyyy")
hbh = IPv6ExtHdrHopByHop(options = [RouterAlert()])
packet = ip6 / hbh
send(packet)

tcpdump reports this as follows:

14:31:53.650727 IP6 (hlim 64, next-header Options (0) payload length: 8)
    xxxx::xxxx > yyyy::yyyy: HBH (rtalert: 0x0000) (padn) no next header
转载请注明原文地址:http://anycun.com/QandA/1746103507a91714.html