17xande
17xande

Reputation: 2813

Generate and send ARP request on specific VLAN with gopacket

I'm successfully using gopacket to create an ARP request and send it.

Now I want to do the same thing, but with a vLAN (802.1Q) tagged packet.

Here's the code:

func writeARP(handle *pcap.Handle, iface *net.Interface, sourceAddr, targetAddr netip.Addr, vlanID uint16) error {
    vlan := layers.Dot1Q{
        VLANIdentifier: vlanID,
    }

    eth := layers.Ethernet{
        SrcMAC:       iface.HardwareAddr,
        DstMAC:       net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
        EthernetType: layers.EthernetTypeARP,
    }

    arp := layers.ARP{
        AddrType:          layers.LinkTypeEthernet,
        Protocol:          layers.EthernetTypeIPv4,
        HwAddressSize:     6,
        ProtAddressSize:   4,
        Operation:         layers.ARPRequest,
        SourceHwAddress:   []byte(iface.HardwareAddr),
        SourceProtAddress: sourceAddr.AsSlice(),
        DstHwAddress:      net.HardwareAddr{0, 0, 0, 0, 0, 0},
        DstProtAddress:    targetAddr.AsSlice(),
    }

    buf := gopacket.NewSerializeBuffer()
    opts := gopacket.SerializeOptions{
        FixLengths:       true,
        ComputeChecksums: true,
    }

    if err := gopacket.SerializeLayers(buf, opts, &eth, &vlan, &arp); err != nil {
        return err
    }
    if err := handle.WritePacketData(buf.Bytes()); err != nil {
        return err
    }
    return nil
}

If I omit the vlan code sections from this code, it sends a perfect ARP request that I can see on WireShark. This request also generates a correct response from the device on the receiving end of the request, and I can see this response on WireShark as well.

If I add the vLAN code exactly as above, it seems to execute successfully, no errors are returned. However, I think the packet is not being sent out. I cannot see the packet in WireShark.

NOTE: I can see the vLAN layer in WireShark for traffic comming from other vLANs. I had to chase a few wild geese to get that right. I should be able to see this packet going out (and its response) on WireShark if the packet was being sent correctly.

I suspect there is a problem with the vLAN layer, or a problem with the serialization, but I have not been able to find it.

Upvotes: 1

Views: 319

Answers (0)

Related Questions