baris
baris

Reputation: 237

How to add PPPoE tag to a PADI packet using Go?

This is how I create a PADI packet using google/gopacket library.

func createDefaultPADI() layers.PPPoE {
    return layers.PPPoE{
        Version:   0x1,
        Type:      0x1,
        Code:      layers.PPPoECodePADI,
        SessionId: 0x0000,
        Length:    0x00,
    }
}

Following structs are defined within the package.

type PPPoE struct {
    BaseLayer
    Version   uint8
    Type      uint8
    Code      PPPoECode
    SessionId uint16
    Length    uint16
}

// BaseLayer is a convenience struct which implements the LayerData and
// LayerPayload functions of the Layer interface.
type BaseLayer struct {
    // Contents is the set of bytes that make up this layer.  IE: for an
    // Ethernet packet, this would be the set of bytes making up the
    // Ethernet frame.
    Contents []byte
    // Payload is the set of bytes contained by (but not part of) this
    // Layer.  Again, to take Ethernet as an example, this would be the
    // set of bytes encapsulated by the Ethernet protocol.
    Payload []byte
}


Afterwards I serialize the packet as follows:

func serializePPPoEPacket(pppoe *layers.PPPoE) {
    buffer := gopacket.NewSerializeBuffer()
    options := gopacket.SerializeOptions{
        ComputeChecksums: true,
        FixLengths:       false,
    }

    ethernetLayer := &layers.Ethernet{
        SrcMAC:       net.HardwareAddr{0xca, 0x01, 0x03, 0x88, 0x00, 0x06},
        DstMAC:       net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
        EthernetType: layers.EthernetTypePPPoEDiscovery,
    }
    if err := gopacket.SerializeLayers(buffer, options, ethernetLayer, pppoe); err != nil {
        pppoeLogger.Error("serialize-error")
    }

    pkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypePPPoE, gopacket.Default)
    log.Println("pkt: ", hex.EncodeToString(pkt.Data()))

}

The hex output of the packet is: ffffffffffffca0103880006886311090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

I want to add some PPPoE tags to the packet such as vendor-specific tag and host-uniq etc. I have tried to manipulate the payload as follows to see if it's gonna work:

// tried this
pppoe.BaseLayer.Payload = append(pppoe.BaseLayer.Payload, []byte("asdasd"))
pppoe.BaseLayer.Contents = append(pppoe.BaseLayer.Contents, []byte("asdasd"))

Nothing happened, I just can't append or set the payload properly. What would be the possible solution here?

In the comment section where BaseLayer is defined within the package, it says set of bytes encapsulated by the Ethernet protocol does that mean that I need to set them in ethernet layer or? If so, I have tried again somethink like that as follows:

// value here is just a byte array.
// ethernetLayer.Payload = append(ethernetLayer.Payload, value...)

Nothing worked again. I want to be able to add tags to a PPPoE packet.

Upvotes: 1

Views: 72

Answers (0)

Related Questions