Skip to content

GTPv1U SerializeTo: GTPExtensionHeaders out of place #67

@yoursunny

Description

@yoursunny

When encoding a packet that contains a GTPv1U layer that is not the last layer, the GTPExtensionHeaders are incorrectly placed at the end of the packet, instead of right after the GTPv1U minimum header before the next layer.

Environment

Go v1.22.4
GoPacket revision 8c73654

This issue is copied from google/gopacket#1175 but adjusted for this fork.

Snippet to Reproduce

https://go.dev/play/p/3hnQC2spi_k

package main

import (
	"encoding/hex"
	"fmt"
	"net"

	"github.com/gopacket/gopacket"
	"github.com/gopacket/gopacket/layers"
)

func main() {
	payload := gopacket.Payload{0xA0, 0xA1, 0xA2, 0xA3}
	udpI := &layers.UDP{SrcPort: 6363, DstPort: 6363}
	ip4I := &layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.ParseIP("192.168.60.4"), DstIP: net.ParseIP("192.168.60.3")}
	gtpExt := []layers.GTPExtensionHeader{{Type: 0x85, Content: []byte{10, 01}}}
	gtp := &layers.GTPv1U{Version: 1, ProtocolType: 1, MessageType: 0xFF, MessageLength: uint16(8 + 20 + 8 + len(payload)), TEID: 0x10000008, GTPExtensionHeaders: gtpExt}
	udp := &layers.UDP{SrcPort: 2152, DstPort: 2152}
	ip4 := &layers.IPv4{Version: 4, TTL: 64, Protocol: layers.IPProtocolUDP, SrcIP: net.ParseIP("192.168.37.2"), DstIP: net.ParseIP("192.168.37.1")}
	eth := &layers.Ethernet{SrcMAC: net.HardwareAddr{0x02, 0x00, 0x00, 0x00, 0x00, 0x02}, DstMAC: net.HardwareAddr{0x02, 0x00, 0x00, 0x00, 0x00, 0x01}, EthernetType: layers.EthernetTypeIPv4}
	udp.SetNetworkLayerForChecksum(ip4)
	udpI.SetNetworkLayerForChecksum(ip4I)

	buf := gopacket.NewSerializeBuffer()
	if e := gopacket.SerializeLayers(buf, gopacket.SerializeOptions{
		FixLengths:       true,
		ComputeChecksums: true,
	}, eth, ip4, udp, gtp, ip4I, udpI, payload); e != nil {
		panic(e)
	}

	fmt.Println(hex.EncodeToString(buf.Bytes()))
}

Expected Output

0200000000010200000000020800 Ethernet
4500004c000000004011af4dc0a82502c0a82501 outer-IPv4
086808680038d511 outer-UDP
34ff00281000000800000085010a0100 GTPv1U
450000200000000040118175c0a83c04c0a83c03 inner-IPv4
18db18db000c9182 inner-UDP
a0a1a2a3 payload

Actual Output

0200000000010200000000020800 Ethernet
4500004c000000004011af4dc0a82502c0a82501 outer-IPv4
086808680038d511 outer-UDP
34ff002810000008 GTPv1U:header
450000200000000040118175c0a83c04c0a83c03 inner-IPv4
18db18db000c9182 inner-UDP
a0a1a2a3 payload
00000085010a0100 GTPv1U:extension

Root Cause

data, err := b.AppendBytes(4)

SerializeBuffer.AppendBytes is called on a non-empty buffer, so that extension headers are placed after the inner layers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions