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

GTPv1U SerializeTo: GTPExtensionHeaders out of place #1175

Open
yoursunny opened this issue Jun 14, 2024 · 0 comments
Open

GTPv1U SerializeTo: GTPExtensionHeaders out of place #1175

yoursunny opened this issue Jun 14, 2024 · 0 comments

Comments

@yoursunny
Copy link

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 v1.1.19

Snippet to Reproduce

https://go.dev/play/p/E97DgsjS18d

package main

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

	"github.com/google/gopacket"
	"github.com/google/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.

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