Skip to content

Instantly share code, notes, and snippets.

@higebu
Created October 11, 2021 02:18
Show Gist options
  • Select an option

  • Save higebu/9503a3b90c047d5bbf677c0d3eb156df to your computer and use it in GitHub Desktop.

Select an option

Save higebu/9503a3b90c047d5bbf677c0d3eb156df to your computer and use it in GitHub Desktop.
Generate GTPv1-U packet in Go
package main
import (
"fmt"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func main() {
icmpPayload := []byte{
0xe0, 0x57, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
}
opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
buf := gopacket.NewSerializeBuffer()
iph := &layers.IPv4{
Version: 4, Protocol: layers.IPProtocolUDP, Flags: layers.IPv4DontFragment, TTL: 64, IHL: 5, Id: 1212,
SrcIP: net.IP{192, 168, 10, 1}, DstIP: net.IP{192, 168, 10, 5},
}
udp := &layers.UDP{SrcPort: 2152, DstPort: 2152}
udp.SetNetworkLayerForChecksum(iph)
err := gopacket.SerializeLayers(buf, opts,
&layers.Ethernet{DstMAC: []byte{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, SrcMAC: []byte{0x00, 0x00, 0x5e, 0x00, 0x53, 0x02}, EthernetType: layers.EthernetTypeDot1Q},
&layers.Dot1Q{VLANIdentifier: 100, Type: layers.EthernetTypeIPv4},
iph, udp,
&layers.GTPv1U{Version: 1, ProtocolType: 1, Reserved: 0, ExtensionHeaderFlag: false, SequenceNumberFlag: false, NPDUFlag: false, MessageType: 255, MessageLength: 76, TEID: 2},
&layers.IPv4{
Version: 4, Protocol: layers.IPProtocolICMPv4, Flags: layers.IPv4DontFragment, TTL: 64, IHL: 5, Id: 1160,
SrcIP: net.IP{192, 168, 100, 200}, DstIP: net.IP{192, 168, 30, 1},
},
&layers.ICMPv4{TypeCode: layers.CreateICMPv4TypeCode(layers.ICMPv4TypeEchoRequest, 0), Id: 1, Seq: 1},
gopacket.Payload(icmpPayload),
)
if err != nil {
panic(err)
}
fmt.Printf("%x", buf.Bytes())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment