packet

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2019 License: BSD-3-Clause Imports: 14 Imported by: 8

Documentation

Overview

Package packet provides functionality for checking and comparing packets. Besides parsing packets user can request comparing packets. In some flow functions user needs to compare packet with some rules, for example access control lists. It can be done manually after packet parsing, however NFF-GO library provides more convenient way for checking and comparing packets via rules.

Rules construction

Rules should be constructed before their usage via "get" functions. Three such functions are provided:

GetL2ACLFromJSON
GetL3ACLFromJSON
GetL3ACLFromORIG

GetL2RulesFromORIG function for L2 level is not added yet. TODO These functions should be used before any usage of rules, however they also can be used dynamically in parallel which make a possibility of changing rules during execution.

After rules are constructed the four functions can be used to filter packets according to rules:

L2ACLPermit
L2ACLPort
L3ACLPermit
L3ACLPort

Package packet provides functionality for fast parsing and generating of packets with known structure. The following header types are supported:

  • L2 Ethernet
  • L3 IPv4 and IPv6
  • L4 TCP, UDP and ICMP

At the moment IPv6 is supported without extension headers.

For performance reasons NFF-GO provides a set of functions each of them parse exact network level.

Packet parsing

Family of parsing functions can parse packets with known structure of headers and parse exactly required protocols.

NFF-GO provides two groups of parsing functions: conditional and unconditional parsing functions.

Unconditional parsing functions are used to parse exactly required protocols. They use constant offsets and pointer arithmetic to get required pointers. There are no checks for correctness because of performance reasons.

Conditional parsing functions are used to parse any supported protocols.

Packet generation

Packets should be placed in special memory to be sent, so user should use additional functions to generate packets. There are two possibilities to do this:

GeneratePacketFromByte function

This function get slice of bytes of any size. Returns packet which contains only these bytes.

CreateEmpty function family

There is family of functions to generate empty packets of predefined size with known header types. All these functions return empty but parsed packet with required protocol headers and preallocated space for payload. All these functions get size of payload as argument. After using one of this functions user can fill any required fields headers of the packet and also fill payload.

Index

Constants

View Source
const (
	ARPRequest = 1
	ARPReply   = 2
)

ARP protocol operations

View Source
const (
	NoExtensionHeaders             = 0x00
	UDPPortExtensionHeader         = 0x40
	PDCP_PDU_NumberExtensionHeader = 0xc0
)

TODO current support of GTP-U: only two extension headers NextExtensionHeader values

View Source
const (
	// Signalling messages: path management
	EchoRequest                           = 1
	EchoResponse                          = 2
	SupportedExtensionHeadersNotification = 31
	// Signalling messages: tunnel management
	ErrorIndication = 26
	EndMarker       = 254
	// G-PDU message = encapsulated user data
	G_PDU = 255

	UDPPortGTPU     = 2152
	SwapUDPPortGTPU = 26632
)

TODO for GTP-U only MessageType values

View Source
const (
	ICMPv6NDSourceLinkLayerAddress uint8 = 1
	ICMPv6NDTargetLinkLayerAddress uint8 = 2
	ICMPv6NDPrefixInformation      uint8 = 3
	ICMPv6NDRedirectedHeader       uint8 = 4
	ICMPv6NDMTU                    uint8 = 5

	ICMPv6RNDouterFlag    uint16 = 0x8000
	ICMPv6NDSolicitedFlag uint16 = 0x4000
	ICMPv6NDOverrideFlag  uint16 = 0x2000

	ICMPv6NDMessageOptionUnitSize = 8
)
View Source
const PcapGlobHdrSize int64 = 24

PcapGlobHdrSize is a size of cap global header.

Variables

View Source
var (
	ICMPv6NeighborSolicitationMessageSize    uint = uint(unsafe.Sizeof(ICMPv6NeighborSolicitationMessage{}))
	ICMPv6NeighborAdvertisementMessageSize   uint = uint(unsafe.Sizeof(ICMPv6NeighborAdvertisementMessage{}))
	ICMPv6NDSourceLinkLayerAddressOptionSize uint = uint(unsafe.Sizeof(ICMPv6NDSourceLinkLayerAddressOption{}))
	ICMPv6NDTargetLinkLayerAddressOptionSize uint = uint(unsafe.Sizeof(ICMPv6NDTargetLinkLayerAddressOption{}))
)

Functions

func ArrayToIPv4

func ArrayToIPv4(a [IPv4AddrLen]byte) uint32

ArrayToIPv4 converts four element array to uint32 representation

func BytesToIPv4

func BytesToIPv4(a byte, b byte, c byte, d byte) uint32

BytesToIPv4 converts four element address to uint32 representation

func CalculateIPv4Checksum

func CalculateIPv4Checksum(hdr *IPv4Hdr) uint16

CalculateIPv4Checksum calculates checksum of IP header

func CalculateIPv4ICMPChecksum

func CalculateIPv4ICMPChecksum(hdr *IPv4Hdr, icmp *ICMPHdr, data unsafe.Pointer) uint16

CalculateIPv4ICMPChecksum calculates ICMP checksum in case if L3 protocol is IPv4.

func CalculateIPv4TCPChecksum

func CalculateIPv4TCPChecksum(hdr *IPv4Hdr, tcp *TCPHdr, data unsafe.Pointer) uint16

CalculateIPv4TCPChecksum calculates TCP checksum for case if L3 protocol is IPv4. Here data pointer should point to end of minimal TCP header because we consider TCP options as part of data.

func CalculateIPv4UDPChecksum

func CalculateIPv4UDPChecksum(hdr *IPv4Hdr, udp *UDPHdr, data unsafe.Pointer) uint16

CalculateIPv4UDPChecksum calculates UDP checksum for case if L3 protocol is IPv4.

func CalculateIPv6BroadcastMACForDstMulticastIP

func CalculateIPv6BroadcastMACForDstMulticastIP(dstMAC *[common.EtherAddrLen]uint8, dstIP [common.IPv6AddrLen]uint8)

func CalculateIPv6ICMPChecksum

func CalculateIPv6ICMPChecksum(hdr *IPv6Hdr, icmp *ICMPHdr, data unsafe.Pointer) uint16

CalculateIPv6ICMPChecksum calculates ICMP checksum in case if L3 protocol is IPv6.

func CalculateIPv6LinkLocalAddrForMAC

func CalculateIPv6LinkLocalAddrForMAC(llAddr *[common.IPv6AddrLen]uint8, mac [common.EtherAddrLen]uint8)

CalculateIPv6LinkLocalAddrForMAC generates IPv6 link local address based on interface MAC address.

func CalculateIPv6MulticastAddrForDstIP

func CalculateIPv6MulticastAddrForDstIP(muticastAddr *[common.IPv6AddrLen]uint8, dstIP [common.IPv6AddrLen]uint8)

CalculateIPv6MulticastAddrForDstIP generates IPv6 multicast address that other hosts use to solicit its MAC address. This address is used as destination for all Neighbor Solicitation ICMPv6 messages and NAT should answer packets coming to it.

func CalculateIPv6TCPChecksum

func CalculateIPv6TCPChecksum(hdr *IPv6Hdr, tcp *TCPHdr, data unsafe.Pointer) uint16

CalculateIPv6TCPChecksum calculates TCP checksum for case if L3 protocol is IPv6.

func CalculateIPv6UDPChecksum

func CalculateIPv6UDPChecksum(hdr *IPv6Hdr, udp *UDPHdr, data unsafe.Pointer) uint16

CalculateIPv6UDPChecksum calculates UDP checksum for case if L3 protocol is IPv6.

func CalculatePseudoHdrIPv4TCPCksum

func CalculatePseudoHdrIPv4TCPCksum(hdr *IPv4Hdr) uint16

CalculatePseudoHdrIPv4TCPCksum implements one step of TCP checksum calculation. Separately computes checksum for TCP pseudo-header for case if L3 protocol is IPv4. This precalculation is required for checksum compute by hardware offload. Result should be put into TCP.Cksum field. See testCksum as an example.

func CalculatePseudoHdrIPv4UDPCksum

func CalculatePseudoHdrIPv4UDPCksum(hdr *IPv4Hdr, udp *UDPHdr) uint16

CalculatePseudoHdrIPv4UDPCksum implements one step of UDP checksum calculation. Separately computes checksum for UDP pseudo-header for case if L3 protocol is IPv4. This precalculation is required for checksum compute by hardware offload. Result should be put into UDP.DgramCksum field. See testCksum as an example.

func CalculatePseudoHdrIPv6TCPCksum

func CalculatePseudoHdrIPv6TCPCksum(hdr *IPv6Hdr) uint16

CalculatePseudoHdrIPv6TCPCksum implements one step of TCP checksum calculation. Separately computes checksum for TCP pseudo-header for case if L3 protocol is IPv6. This precalculation is required for checksum compute by hardware offload. Result should be put into TCP.Cksum field. See testCksum as an example.

func CalculatePseudoHdrIPv6UDPCksum

func CalculatePseudoHdrIPv6UDPCksum(hdr *IPv6Hdr, udp *UDPHdr) uint16

CalculatePseudoHdrIPv6UDPCksum implements one step of UDP checksum calculation. Separately computes checksum for UDP pseudo-header for case if L3 protocol is IPv6. This precalculation is required for checksum compute by hardware offload. Result should be put into UDP.DgramCksum field. See testCksum as an example.

func ExtractPacketAddr

func ExtractPacketAddr(IN uintptr) uintptr

ExtractPacketAddr extracts packet structure from mbuf used in package flow

func ExtractPackets

func ExtractPackets(packet []*Packet, IN []uintptr, n uint)

ExtractPackets creates vector of packets by calling ExtractPacket function is unexported, used in flow package

func GeneratePacketFromByte

func GeneratePacketFromByte(packet *Packet, data []byte) bool

GeneratePacketFromByte function gets non-initialized packet and slice of bytes of any size. Initializes input packet and fills it with these bytes.

func IPv4ToBytes

func IPv4ToBytes(v uint32) [IPv4AddrLen]byte

IPv4ToBytes converts four element address to uint32 representation

func IPv4ToString

func IPv4ToString(addr uint32) string

func IPv6ToString

func IPv6ToString(addr [IPv6AddrLen]uint8) string

func InitARPReplyPacket

func InitARPReplyPacket(packet *Packet, SHA, THA [common.EtherAddrLen]uint8, SPA, TPA uint32) bool

InitARPReplyPacket initialize ARP reply packet for IPv4 protocol. SHA and SPA specify sender MAC and IP addresses, THA and TPA specify target MAC and IP addresses. Destination MAC address in L2 Ethernet header is set to THA and source address is set to SHA.

func InitARPRequestPacket

func InitARPRequestPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA, TPA uint32) bool

InitARPRequestPacket initialize ARP request packet for IPv4 protocol request with broadcast (zero) for THA (Target HW address). SHA and SPA specify sender MAC and IP addresses, TPA specifies IP address for host which request is sent for. Destination MAC address in L2 Ethernet header is set to FF:FF:FF:FF:FF:FF (broadcast) and source address is set to SHA.

func InitEmptyARPPacket

func InitEmptyARPPacket(packet *Packet) bool

InitEmptyARPPacket initializes empty ARP packet

func InitEmptyIPv4ICMPPacket

func InitEmptyIPv4ICMPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv4ICMPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv4 and ICMP headers. This function supposes that IPv4 header has minimum length. In fact length can be higher due to optional fields. Now setting optional fields explicitly is not supported.

func InitEmptyIPv4Packet

func InitEmptyIPv4Packet(packet *Packet, plSize uint) bool

InitEmptyIPv4Packet initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet and IPv4 headers.

func InitEmptyIPv4TCPPacket

func InitEmptyIPv4TCPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv4TCPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv4 and TCP headers. This function supposes that IPv4 and TCP headers have minimum length. In fact length can be higher due to optional fields. Now setting optional fields explicitly is not supported.

func InitEmptyIPv4UDPPacket

func InitEmptyIPv4UDPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv4UDPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv4 and UDP headers. This function supposes that IPv4 header has minimum length. In fact length can be higher due to optional fields. Now setting optional fields explicitly is not supported.

func InitEmptyIPv6ICMPPacket

func InitEmptyIPv6ICMPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv6ICMPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv6 and ICMP headers.

func InitEmptyIPv6Packet

func InitEmptyIPv6Packet(packet *Packet, plSize uint) bool

InitEmptyIPv6Packet initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet and IPv6 headers.

func InitEmptyIPv6TCPPacket

func InitEmptyIPv6TCPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv6TCPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv6 and TCP headers. This function supposes that IPv6 and TCP headers have minimum length. In fact length can be higher due to optional fields. Now setting optional fields explicitly is not supported.

func InitEmptyIPv6UDPPacket

func InitEmptyIPv6UDPPacket(packet *Packet, plSize uint) bool

InitEmptyIPv6UDPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv6 and UDP headers. This function supposes that IPv6 header has minimum length. In fact length can be higher due to optional fields. Now setting optional fields explicitly is not supported.

func InitEmptyPacket

func InitEmptyPacket(packet *Packet, plSize uint) bool

InitEmptyPacket initializes input packet with preallocated plSize of bytes for payload and init pointer to Ethernet header.

func InitGARPAnnouncementReplyPacket

func InitGARPAnnouncementReplyPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA uint32) bool

InitGARPAnnouncementReplyPacket initialize gratuitous ARP reply packet (ARP announcement) for IPv4 protocol. SHA and SPA specify sender MAC and IP addresses, TPA is set to the value of SPA and THA to zeroes (according to RFC 5227). Destination MAC address in L2 Ethernet header is set to FF:FF:FF:FF:FF:FF (broadcast) and source address is set to SHA.

func InitGARPAnnouncementRequestPacket

func InitGARPAnnouncementRequestPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA uint32) bool

InitGARPAnnouncementRequestPacket initialize gratuitous ARP request (preferred over reply) packet (ARP announcement) for IPv4 protocol request with broadcast (zero) for THA (Target HW address). SHA and SPA specify sender MAC and IP addresses, TPA is set to the value of SPA. Destination MAC address in L2 Ethernet header is set to FF:FF:FF:FF:FF:FF (broadcast) and source address is set to SHA.

func InitICMPv6NeighborAdvertisementPacket

func InitICMPv6NeighborAdvertisementPacket(packet *Packet, srcMAC, dstMAC [common.EtherAddrLen]uint8, srcIP, dstIP [common.IPv6AddrLen]uint8)

InitICMPv6NeighborAdvertisementPacket allocates and initializes ICMPv6 Neighbor Advertisement answer message packet with source MAC and IPv6 address and target IPv6 address.

func InitICMPv6NeighborSolicitationPacket

func InitICMPv6NeighborSolicitationPacket(packet *Packet, srcMAC [common.EtherAddrLen]uint8, srcIP, dstIP [common.IPv6AddrLen]uint8)

InitICMPv6NeighborSolicitationPacket allocates and initializes ICMPv6 Neighbor Solicitation request message packet with source MAC and IPv6 address and target IPv6 address.

func MACToString

func MACToString(mac [EtherAddrLen]uint8) string

MACToString return MAC address like string

func ReadPcapGlobalHdr

func ReadPcapGlobalHdr(f io.Reader, glHdr *PcapGlobHdr) error

ReadPcapGlobalHdr read global pcap header into file.

func SetHWOffloadingHdrChecksum

func SetHWOffloadingHdrChecksum(p *Packet)

SetHWOffloadingHdrChecksum makes precalculation of pseudo header checksum. Separately computes checksum for required pseudo-header and writes result to correct place. This is required for checksum compute by hardware offload.

func SetHWTXChecksumFlag

func SetHWTXChecksumFlag(flag bool)

SetHWTXChecksumFlag should not be exported but it is used in flow.

func SetNonPerfMempool

func SetNonPerfMempool(m *low.Mempool)

SetNonPerfMempool sets default mempool for non performance critical allocations. Shouldn't be called by user

func SwapBytesUint16

func SwapBytesUint16(x uint16) uint16

SwapBytesUint16 swaps uint16 in Little Endian and Big Endian

func SwapBytesUint32

func SwapBytesUint32(x uint32) uint32

SwapBytesUint32 swaps uint32 in Little Endian and Big Endian

func WritePcapGlobalHdr

func WritePcapGlobalHdr(f io.Writer) error

WritePcapGlobalHdr writes global pcap header into file.

Types

type ARPHdr

type ARPHdr struct {
	HType     uint16                     // Hardware type, e.g. 1 for Ethernet
	PType     uint16                     // Protocol type, e.g. 0x0800 for IPv4
	HLen      uint8                      // Hardware address length, e.g. 6 for MAC length
	PLen      uint8                      // Protocol address length, e.g. 4 for IPv4 address length
	Operation uint16                     // Operation type, see ARP constants
	SHA       [common.EtherAddrLen]uint8 // Sender hardware address (sender MAC address)
	SPA       [common.IPv4AddrLen]uint8  // Sender protocol address (sender IPv4 address)
	// array is used to avoid alignment (compiler alignes uint32 on 4 bytes)
	THA [common.EtherAddrLen]uint8 // Target hardware address (target MAC address)
	TPA [common.IPv4AddrLen]uint8  // Target protocol address (target IPv4 address)

}

ARPHdr is protocol structure used in Address Resolution Protocol for IPv4 to MAC mapping

func (*ARPHdr) String

func (hdr *ARPHdr) String() string

type EtherHdr

type EtherHdr struct {
	DAddr     [EtherAddrLen]uint8 // Destination address
	SAddr     [EtherAddrLen]uint8 // Source address
	EtherType uint16              // Frame type
}

EtherHdr L2 header from DPDK: lib/librte_ether/rte_ehter.h

func (*EtherHdr) String

func (hdr *EtherHdr) String() string

type GTPHdr

type GTPHdr struct {
	HeaderType          uint8 // version, protocol type, extension header/sequence number/N-PDU flags
	MessageType         uint8
	MessageLength       uint16
	TEID                uint32
	SequenceNumber      uint16 // this is valid only with sequence number flag
	NPDUNumber          uint8  // this is valid only with N-PDU flag
	NextExtensionHeader uint8  // this is valid only with exatension header flag
}

GTPv1-U and GTPv1-C

func (*GTPHdr) String

func (hdr *GTPHdr) String() string

type ICMPHdr

type ICMPHdr struct {
	Type       uint8  // ICMP message type
	Code       uint8  // ICMP message code
	Cksum      uint16 // ICMP checksum
	Identifier uint16 // ICMP message identifier in some messages
	SeqNum     uint16 // ICMP message sequence number in some messages
}

ICMPHdr L4 header.

func (*ICMPHdr) String

func (hdr *ICMPHdr) String() string

type ICMPv6NDMTUOption

type ICMPv6NDMTUOption struct {
	Type   uint8
	Length uint8
	MTU    uint32
}

type ICMPv6NDPrefixInformationOption

type ICMPv6NDPrefixInformationOption struct {
	Type              uint8
	Length            uint8
	PrefixLength      uint8
	LAFlags           uint8
	ValidLifetime     uint32
	PreferredLifetime uint32
	Reserved2         uint32
	Prefix            [common.IPv6AddrLen]uint8
}

type ICMPv6NDRedirectedHeaderOption

type ICMPv6NDRedirectedHeaderOption struct {
	Type     uint8
	Length   uint8
	Reserved uint32
}

type ICMPv6NDSourceLinkLayerAddressOption

type ICMPv6NDSourceLinkLayerAddressOption struct {
	Type             uint8
	Length           uint8
	LinkLayerAddress [common.EtherAddrLen]uint8
}

type ICMPv6NDTargetLinkLayerAddressOption

type ICMPv6NDTargetLinkLayerAddressOption struct {
	Type             uint8
	Length           uint8
	LinkLayerAddress [common.EtherAddrLen]uint8
}

type ICMPv6NeighborAdvertisementMessage

type ICMPv6NeighborAdvertisementMessage struct {
	TargetAddr [common.IPv6AddrLen]uint8
}

type ICMPv6NeighborSolicitationMessage

type ICMPv6NeighborSolicitationMessage struct {
	TargetAddr [common.IPv6AddrLen]uint8
}

type IPv4Hdr

type IPv4Hdr struct {
	VersionIhl     uint8  // version and header length
	TypeOfService  uint8  // type of service
	TotalLength    uint16 // length of packet
	PacketID       uint16 // packet ID
	FragmentOffset uint16 // fragmentation offset
	TimeToLive     uint8  // time to live
	NextProtoID    uint8  // protocol ID
	HdrChecksum    uint16 // header checksum
	SrcAddr        uint32 // source address
	DstAddr        uint32 // destination address
}

IPv4Hdr L3 header from DPDK: lib/librte_net/rte_ip.h

func (*IPv4Hdr) String

func (hdr *IPv4Hdr) String() string

type IPv6Hdr

type IPv6Hdr struct {
	VtcFlow    uint32             // IP version, traffic class & flow label
	PayloadLen uint16             // IP packet length - includes sizeof(ip_header)
	Proto      uint8              // Protocol, next header
	HopLimits  uint8              // Hop limits
	SrcAddr    [IPv6AddrLen]uint8 // IP address of source host
	DstAddr    [IPv6AddrLen]uint8 // IP address of destination host(s)
}

IPv6Hdr L3 header from DPDK: lib/librte_net/rte_ip.h

func (*IPv6Hdr) String

func (hdr *IPv6Hdr) String() string

type L2Rules

type L2Rules struct {
	// contains filtered or unexported fields
}

L2Rules - struct for rules of l2 level

func GetL2ACLFromJSON

func GetL2ACLFromJSON(filename string) (*L2Rules, error)

GetL2ACLFromJSON gets name of JSON structed file with L2 rules, returns L2Rules

func GetL2ACLFromORIG

func GetL2ACLFromORIG(filename string) (*L2Rules, error)

GetL2ACLFromORIG gets name of fields structed file with combined L2 rules, returns L2Rules

type L3Rules

type L3Rules struct {
	// contains filtered or unexported fields
}

L3Rules - struct for rules of l3 level

func GetL3ACLFromJSON

func GetL3ACLFromJSON(filename string) (*L3Rules, error)

GetL3ACLFromJSON gets name of JSON structed file with combined L3 and L4 rules, returns L2Rules

func GetL3ACLFromORIG

func GetL3ACLFromORIG(filename string) (*L3Rules, error)

GetL3ACLFromORIG gets name of fields structed file with combined L3 and L4 rules, returns L3Rules

type LPM

type LPM struct {
	// contains filtered or unexported fields
}

func CreateLPM

func CreateLPM(name string, socket uint8, maxRules uint32, numberTbl8 uint32) *LPM

CreateLPM creates longest prefix match structure with given name at given socket maxRules - maximum number of LPM rules inside table, numberTbl8 - maximum number of rules with mask length more than 24 bits LPM is stored in C management memory - no garbage collectors there. You should use Free function after working with it.

func (*LPM) Add

func (lpm *LPM) Add(ip uint32, depth uint8, nextHop uint32) int

Add adds longest prefix match rule with specified ip, depth and nextHop inside LPM table. Returns 0 if success and negative value otherwise

func (*LPM) Delete

func (lpm *LPM) Delete(ip uint32, depth uint8) int

Delete removes longest prefix match rule with diven ip and depth from LPM table. Returns 0 if success and negative value otherwise

func (*LPM) Free

func (lpm *LPM) Free()

Free frees LPM C management memory

func (*LPM) Lookup

func (lpm *LPM) Lookup(ip uint32, nextHop *uint32) bool

Lookup looks for given ip inside LPM table. If ip was matched with LPM rule true is returned and nextHop contains next hop identifier for this rule. Else false is returned. Heavily based on DPDK rte_lpm_lookup with constants from there No error checking (lpm == NULL or nextHop == NULL) due to performance User should check it manually

type MPLSHdr

type MPLSHdr struct {
	// contains filtered or unexported fields
}

func (*MPLSHdr) DecreaseTTL

func (hdr *MPLSHdr) DecreaseTTL() bool

SetMPLSLabel sets Label (20 first bits of MPLS header to specified value).

func (*MPLSHdr) GetMPLSLabel

func (hdr *MPLSHdr) GetMPLSLabel() uint32

GetMPLSLabel returns Label (20 first bits of MPLS header).

func (*MPLSHdr) GetMPLSS

func (hdr *MPLSHdr) GetMPLSS() uint32

GetMPLSS returns the Bottom-Of-Stack value

func (*MPLSHdr) GetMPLSTC

func (hdr *MPLSHdr) GetMPLSTC() uint32

GetMPLSTC returns the Traffic Class (formerly known as EXP).

func (*MPLSHdr) GetMPLSTTL

func (hdr *MPLSHdr) GetMPLSTTL() uint32

GetMPLSTTL returns the Time-to-Live value

func (*MPLSHdr) SetMPLSLabel

func (hdr *MPLSHdr) SetMPLSLabel(tag uint32)

SetMPLSLabel sets Label (20 first bits of MPLS header to specified value).

func (*MPLSHdr) String

func (hdr *MPLSHdr) String() string

type PDCP_PDU_Number

type PDCP_PDU_Number struct {
	Length              uint8 // in 4 octets, here always 0x01
	PDCP_PDU_Number_1   uint8
	PDCP_PDU_Number_2   uint8
	NextExtensionHeader uint8
}

type Packet

type Packet struct {
	L3   unsafe.Pointer // Pointer to L3 header in mbuf
	L4   unsafe.Pointer // Pointer to L4 header in mbuf
	Data unsafe.Pointer // Pointer to the packet payload data

	// Last two fields of this structure is filled during InitMbuf macros inside low.c file
	// Need to change low.c for all changes in these fields or adding/removing fields before them.
	Ether *EtherHdr // Pointer to L2 header in mbuf. It is always parsed and point beginning of packet.
	CMbuf *low.Mbuf // Private pointer to mbuf. Users shouldn't know anything about mbuf

	Next *Packet // non nil if packet consists of several chained mbufs
}

Packet is a set of pointers in NFF-GO library. Each pointer points to one of five headers: Mac, IPv4, IPv6, TCP and UDP plus raw pointer.

Empty packet means that only raw pointer is not nil: it points to beginning of packet data – raw bits. User should extract packet data somehow.

Parsing means to fill required header pointers with corresponding headers. For example, after user fills IPv4 pointer to right place inside packet he can use its fields like packet.IPv4.SrcAddr or packet.IPv4.DstAddr.

func ExtractPacket

func ExtractPacket(IN uintptr) *Packet

ExtractPacket extracts packet structure from mbuf used in package flow.

func NewPacket

func NewPacket() (*Packet, error)

NewPacket shouldn't be used for performance critical allocations. Allocate mbufs one by one is very inefficient. FastGenerate or copy functions give developer a packet from previously bulk allocated set Should be used only for testing or single events like ARP or ICMP answers

func ToPacket

func ToPacket(IN uintptr) *Packet

ToPacket should be unexported, used in flow package.

func (*Packet) AddMPLS

func (packet *Packet) AddMPLS(mpls uint32) bool

AddMPLS increases size of packet on MPLSLen and adds MPLS header after Ether header, mpls is a whole MPLS header. Returns false if error.

func (*Packet) AddVLANTag

func (packet *Packet) AddVLANTag(tag uint16) bool

AddVLANTag increases size of packet on VLANLen and adds 802.1Q VLAN header after Ether header, tag is a tag control information. Returns false if error.

func (*Packet) DecapsulateHead

func (packet *Packet) DecapsulateHead(start uint, length uint) bool

DecapsulateHead removes bytes from packet. start - number of beginning byte, length - number of removed bytes. This function should be used to remove bytes from the first half of packet. Return false if error. TODO change this for scattered packet case (multiple mbufs)

func (*Packet) DecapsulateIPv4GTP

func (packet *Packet) DecapsulateIPv4GTP() bool

DecapsulateIPv4GTP assumes that user has etherNet->IPv4->UDP->GTP->payload data structure with standart IPv4 header size and wants to leave only ether->payload part It is also assumed that payload is encapsulated IPv4 datagram, so no etherType changes are needed

func (*Packet) DecapsulateTail

func (packet *Packet) DecapsulateTail(start uint, length uint) bool

DecapsulateTail removes bytes from packet. start - number of beginning byte, length - number of removed bytes. This function should be used to remove bytes from the second half of packet. Return false if error. TODO change this for scattered packet case (multiple mbufs)

func (*Packet) EncapsulateHead

func (packet *Packet) EncapsulateHead(start uint, length uint) bool

EncapsulateHead adds bytes to packet. start - number of beginning byte, length - number of added bytes. This function should be used to add bytes to the first half of packet. Return false if error. TODO change this for scattered packet case (multiple mbufs)

func (*Packet) EncapsulateIPv4GTP

func (packet *Packet) EncapsulateIPv4GTP(TEID uint32) bool

EncapsulateIPv4GTP assumes that user wants to build ether->IPv4->UDP->GTP->payload data structure with standart IPv4 header size. It is also assumed that payload type is IPv4, so no etherType changes are needed

func (*Packet) EncapsulateTail

func (packet *Packet) EncapsulateTail(start uint, length uint) bool

EncapsulateTail adds bytes to packet. start - number of beginning byte, length - number of added bytes. This function should be used to add bytes to the second half of packet. Return false if error. TODO change this for scattered packet case (multiple mbufs)

func (*Packet) GTPIPv4AllParsing

func (packet *Packet) GTPIPv4AllParsing() *GTPHdr

GTPIPv4AllParsing assumes that nothing was parsed, however packet has ether->IPv4->UDP->GTP->payload data structure Returns GTP header, fills L3, L4 and Data packet fields

func (*Packet) GTPIPv4FastParsing

func (packet *Packet) GTPIPv4FastParsing() *GTPHdr

GTPIPv4FastParsing assumes that nothing was parsed, however packet has ether->IPv4->UDP->GTP->payload data structure with standart IPv4 header size. Returns GTP header

func (*Packet) GetARP

func (packet *Packet) GetARP() *ARPHdr

GetARP ensures if EtherType is ARP and casts L3 pointer to ARPHdr type.

func (*Packet) GetARPCheckVLAN

func (packet *Packet) GetARPCheckVLAN() *ARPHdr

GetARPCheckVLAN ensures if EtherType is ARP and casts L3 pointer to ARPHdr type. VLAN presence is checked if necessary.

func (*Packet) GetARPNoCheck

func (packet *Packet) GetARPNoCheck() *ARPHdr

GetARPNoCheck casts L3 pointer to ARPHdr type.

func (*Packet) GetEtherType

func (packet *Packet) GetEtherType() uint16

GetEtherType correctly returns EtherType from Ethernet header or VLAN header.

func (*Packet) GetGTP

func (packet *Packet) GetGTP() *GTPHdr

GetGTP assumes that packet is already parsed. Returns GTP header as payload after L4 header

func (*Packet) GetICMPForIPv4

func (packet *Packet) GetICMPForIPv4() *ICMPHdr

GetICMPForIPv4 ensures if L4 type is ICMP and cast L4 pointer to *ICMPHdr type. L3 supposed to be parsed before and of IPv4 type.

func (*Packet) GetICMPForIPv6

func (packet *Packet) GetICMPForIPv6() *ICMPHdr

GetICMPForIPv6 ensures if L4 type is ICMP and cast L4 pointer to *ICMPHdr type. L3 supposed to be parsed before and of IPv6 type.

func (*Packet) GetICMPNoCheck

func (packet *Packet) GetICMPNoCheck() *ICMPHdr

GetICMPNoCheck casts L4 pointer to *ICMPHdr type.

func (*Packet) GetICMPv6NDSourceLinkLayerAddressOption

func (packet *Packet) GetICMPv6NDSourceLinkLayerAddressOption(msgLength uint) *ICMPv6NDSourceLinkLayerAddressOption

GetICMPv6NDSourceLinkLayerAddressOption returns Neighbor Discovery Source Link Layer option for an ICMPv6 message packet following a message of length msgLength. If packet is not long enough to contain this option, nil is returned.

func (*Packet) GetICMPv6NDTargetLinkLayerAddressOption

func (packet *Packet) GetICMPv6NDTargetLinkLayerAddressOption(msgLength uint) *ICMPv6NDSourceLinkLayerAddressOption

GetICMPv6NDTargetLinkLayerAddressOption returns Neighbor Discovery Target Link Layer option for an ICMPv6 message packet following a message of length msgLength. If packet is not long enough to contain this option, nil is returned.

func (*Packet) GetICMPv6NeighborAdvertisementMessage

func (packet *Packet) GetICMPv6NeighborAdvertisementMessage() *ICMPv6NeighborAdvertisementMessage

GetICMPv6NeighborAdvertisementMessage returns pointer to ICMPv6 Neighbor Solicitation message buffer. It should be called after packet.Data field is initialized with ParseL7 or ParseData calls.

func (*Packet) GetICMPv6NeighborSolicitationMessage

func (packet *Packet) GetICMPv6NeighborSolicitationMessage() *ICMPv6NeighborSolicitationMessage

GetICMPv6NeighborSolicitationMessage returns pointer to ICMPv6 Neighbor Solicitation message buffer. It should be called after packet.Data field is initialized with ParseL7 or ParseData calls.

func (*Packet) GetIPv4

func (packet *Packet) GetIPv4() *IPv4Hdr

GetIPv4 ensures if EtherType is IPv4 and casts L3 pointer to IPv4Hdr type.

func (*Packet) GetIPv4CheckVLAN

func (packet *Packet) GetIPv4CheckVLAN() *IPv4Hdr

GetIPv4CheckVLAN ensures if EtherType is IPv4 and casts L3 pointer to IPv4Hdr type. VLAN presence is checked if necessary.

func (*Packet) GetIPv4NoCheck

func (packet *Packet) GetIPv4NoCheck() *IPv4Hdr

GetIPv4NoCheck casts L3 pointer to IPv4Hdr type.

func (*Packet) GetIPv6

func (packet *Packet) GetIPv6() *IPv6Hdr

GetIPv6 ensures if EtherType is IPv6 and cast L3 pointer to IPv6Hdr type.

func (*Packet) GetIPv6CheckVLAN

func (packet *Packet) GetIPv6CheckVLAN() *IPv6Hdr

GetIPv6CheckVLAN ensures if EtherType is IPv6 and cast L3 pointer to IPv6Hdr type. VLAN presence is checked if necessary.

func (*Packet) GetIPv6NoCheck

func (packet *Packet) GetIPv6NoCheck() *IPv6Hdr

GetIPv6NoCheck ensures if EtherType is IPv6 and cast L3 pointer to IPv6Hdr type.

func (*Packet) GetMPLS

func (packet *Packet) GetMPLS() *MPLSHdr

GetMPLS returns MPLS header pointer if it is present in the packet.

func (*Packet) GetMPLSNoCheck

func (packet *Packet) GetMPLSNoCheck() *MPLSHdr

GetMPLSNoCheck casts pointer to memory right after Ethernet header to MPLSHdr type.

func (*Packet) GetPacketLen

func (packet *Packet) GetPacketLen() uint

GetPacketLen returns length of this packet. Sum of length of all segments if scattered.

func (*Packet) GetPacketPayload

func (packet *Packet) GetPacketPayload() ([]byte, bool)

GetPacketPayload returns extracted packet payload as byte array and bool status. Works only for protocols, supported by ParseData (IPv4, IPv6, TCP, UDP, ICMP). Not zero-copy.

func (*Packet) GetPacketSegmentLen

func (packet *Packet) GetPacketSegmentLen() uint

GetPacketSegmentLen returns length of this segment of packet. It is equal to whole length if packet not scattered

func (*Packet) GetRawPacketBytes

func (packet *Packet) GetRawPacketBytes() []byte

GetRawPacketBytes returns all bytes from this packet. Not zero-copy.

func (*Packet) GetTCPForIPv4

func (packet *Packet) GetTCPForIPv4() *TCPHdr

GetTCPForIPv4 ensures if L4 type is TCP and cast L4 pointer to TCPHdr type.

func (*Packet) GetTCPForIPv6

func (packet *Packet) GetTCPForIPv6() *TCPHdr

GetTCPForIPv6 ensures if L4 type is TCP and cast L4 pointer to *TCPHdr type.

func (*Packet) GetTCPNoCheck

func (packet *Packet) GetTCPNoCheck() *TCPHdr

GetTCPNoCheck casts L4 pointer to TCPHdr type.

func (*Packet) GetUDPForIPv4

func (packet *Packet) GetUDPForIPv4() *UDPHdr

GetUDPForIPv4 ensures if L4 type is UDP and cast L4 pointer to *UDPHdr type.

func (*Packet) GetUDPForIPv6

func (packet *Packet) GetUDPForIPv6() *UDPHdr

GetUDPForIPv6 ensures if L4 type is UDP and cast L4 pointer to *UDPHdr type.

func (*Packet) GetUDPNoCheck

func (packet *Packet) GetUDPNoCheck() *UDPHdr

GetUDPNoCheck casts L4 pointer to *UDPHdr type.

func (*Packet) GetVLAN

func (packet *Packet) GetVLAN() *VLANHdr

GetVLAN returns VLAN header pointer if it is present in the packet.

func (*Packet) GetVLANNoCheck

func (packet *Packet) GetVLANNoCheck() *VLANHdr

GetVLANNoCheck casts pointer to memory right after Ethernet header to VLANHdr type.

func (*Packet) L2ACLPermit

func (pkt *Packet) L2ACLPermit(rules *L2Rules) bool

L2ACLPermit gets packet (with parsed L2) and L2Rules. Returns accept or reject for this packet

func (*Packet) L2ACLPort

func (pkt *Packet) L2ACLPort(rules *L2Rules) uint

L2ACLPort gets packet (with parsed L2) and L2Rules. Returns number of output for packet

func (*Packet) L3ACLPermit

func (pkt *Packet) L3ACLPermit(rules *L3Rules) bool

L3ACLPermit gets packet (with parsed L3 or L3 with L4) and L3Rules. Returns accept or reject for this packet

func (*Packet) L3ACLPort

func (pkt *Packet) L3ACLPort(rules *L3Rules) uint

L3ACLPort gets packet (with parsed L3 or L3 with L4) and L3Rules. Returns number of output for this packet

func (*Packet) PacketBytesChange

func (packet *Packet) PacketBytesChange(start uint, bytes []byte) bool

PacketBytesChange changes packet bytes from start byte to given bytes. Return false if error.

func (*Packet) ParseAllKnownL3

func (packet *Packet) ParseAllKnownL3() (*IPv4Hdr, *IPv6Hdr, *ARPHdr)

ParseAllKnownL3 parses L3 field and returns pointers to parsed headers.

func (*Packet) ParseAllKnownL3CheckVLAN

func (packet *Packet) ParseAllKnownL3CheckVLAN() (*IPv4Hdr, *IPv6Hdr, *ARPHdr)

ParseAllKnownL3CheckVLAN parses L3 field and returns pointers to parsed headers taking possible presence of VLAN header into account.

func (*Packet) ParseAllKnownL4ForIPv4

func (packet *Packet) ParseAllKnownL4ForIPv4() (*TCPHdr, *UDPHdr, *ICMPHdr)

ParseAllKnownL4ForIPv4 parses L4 field if L3 type is IPv4 and returns pointers to parsed headers.

func (*Packet) ParseAllKnownL4ForIPv6

func (packet *Packet) ParseAllKnownL4ForIPv6() (*TCPHdr, *UDPHdr, *ICMPHdr)

ParseAllKnownL4ForIPv6 parses L4 field if L3 type is IPv6 and returns pointers to parsed headers.

func (*Packet) ParseData

func (packet *Packet) ParseData() int

ParseData parses L3, L4 and fills the field packet.Data. returns 0 in case of success and -1 in case of failure to parse L3 or L4.

func (*Packet) ParseDataCheckVLAN

func (packet *Packet) ParseDataCheckVLAN() int

ParseDataCheckVLAN parses L3, L4 and fills the field packet.Data. returns 0 in case of success and -1 in case of failure to parse L3 or L4. VLAN presence is checked.

func (*Packet) ParseL3

func (packet *Packet) ParseL3()

ParseL3 set pointer to start of L3 header

func (*Packet) ParseL3CheckMPLS

func (packet *Packet) ParseL3CheckMPLS() *MPLSHdr

ParseL3CheckMPLS set pointer to start of L3 header taking possible presence of MPLS header into account.

func (*Packet) ParseL3CheckVLAN

func (packet *Packet) ParseL3CheckVLAN() *VLANHdr

ParseL3CheckVLAN set pointer to start of L3 header taking possible presence of VLAN header into account.

func (*Packet) ParseL4ForIPv4

func (packet *Packet) ParseL4ForIPv4()

ParseL4ForIPv4 set L4 to start of L4 header, if L3 protocol is IPv4.

func (*Packet) ParseL4ForIPv6

func (packet *Packet) ParseL4ForIPv6()

ParseL4ForIPv6 set L4 to start of L4 header, if L3 protocol is IPv6.

func (*Packet) ParseL7

func (packet *Packet) ParseL7(protocol uint)

ParseL7 fills pointers to all supported headers and data field.

func (*Packet) ReadPcapOnePacket

func (pkt *Packet) ReadPcapOnePacket(f io.Reader) (bool, error)

ReadPcapOnePacket read one packet with pcap header from file. Assumes that global pcap header is already read.

func (*Packet) RemoveMPLS

func (packet *Packet) RemoveMPLS() bool

RemoveMPLS decreases size of packet on MPLSLen THIS FUNCTION DOESN'T SET ETHERTYPE!!! IT SHOULD BE SET ACCORDING TO LABEL!!! ETHERTYPE WILL REMAIN MPLS-LIKE = 0x8847

func (*Packet) RemoveVLANTag

func (packet *Packet) RemoveVLANTag() bool

RemoveVLANTag decreases size of packet on VLANLen

func (*Packet) SendPacket

func (p *Packet) SendPacket(port uint16) bool

SendPacket immidiately sends packet to specified port via calling C function. Packet is freed. Function return true if packet was actually sent. Port should be initialized. Packet is sent to zero queue (is always present). Sending simultaneously to one port is permitted in DPDK. Is very inefficient. Should be used only for testing or single events like ARP or ICMP answers

func (*Packet) SetHWCksumOLFlags

func (packet *Packet) SetHWCksumOLFlags()

SetHWCksumOLFlags sets hardware offloading flags to packet

func (*Packet) SetTXIPv4OLFlags

func (packet *Packet) SetTXIPv4OLFlags(l2len, l3len uint32)

SetTXIPv4OLFlags sets mbuf flags for IPv4 header checksum calculation offloading.

func (*Packet) SetTXIPv4TCPOLFlags

func (packet *Packet) SetTXIPv4TCPOLFlags(l2len, l3len uint32)

SetTXIPv4TCPOLFlags sets mbuf flags for IPv4 and TCP headers checksum calculation hardware offloading.

func (*Packet) SetTXIPv4UDPOLFlags

func (packet *Packet) SetTXIPv4UDPOLFlags(l2len, l3len uint32)

SetTXIPv4UDPOLFlags sets mbuf flags for IPv4 and UDP headers checksum calculation hardware offloading.

func (*Packet) SetTXIPv6TCPOLFlags

func (packet *Packet) SetTXIPv6TCPOLFlags(l2len, l3len uint32)

SetTXIPv6TCPOLFlags sets mbuf flags for IPv6 TCP header checksum calculation hardware offloading.

func (*Packet) SetTXIPv6UDPOLFlags

func (packet *Packet) SetTXIPv6UDPOLFlags(l2len, l3len uint32)

SetTXIPv6UDPOLFlags sets mbuf flags for IPv6 UDP header checksum calculation hardware offloading.

func (*Packet) StartAtOffset

func (packet *Packet) StartAtOffset(offset uintptr) unsafe.Pointer

StartAtOffset function return pointer to first byte of packet with given offset.

func (*Packet) ToUintptr

func (p *Packet) ToUintptr() uintptr

ToUintptr returns start of mbuf for current packet

func (*Packet) WritePcapOnePacket

func (pkt *Packet) WritePcapOnePacket(f io.Writer) error

WritePcapOnePacket writes one packet with pcap header in file. Assumes global pcap header is already present in file. Packet timestamps have nanosecond resolution.

type PcapGlobHdr

type PcapGlobHdr struct {
	MagicNumber  uint32 /* magic number */
	VersionMajor uint16 /* major version number */
	VersionMinor uint16 /* minor version number */
	Thiszone     int32  /* GMT to local correction */
	Sigfigs      uint32 /* accuracy of timestamps */
	Snaplen      uint32 /* max length of captured packets, in octets */
	Network      uint32 /* data link type */
}

PcapGlobHdr is a Pcap global header.

type PcapRecHdr

type PcapRecHdr struct {
	TsSec   uint32 /* timestamp seconds */
	TsUsec  uint32 /* timestamp nanoseconds */
	InclLen uint32 /* number of octets of packet saved in file */
	OrigLen uint32 /* actual length of packet */
}

PcapRecHdr is a Pcap packet header.

type TCPHdr

type TCPHdr struct {
	SrcPort  uint16   // TCP source port
	DstPort  uint16   // TCP destination port
	SentSeq  uint32   // TX data sequence number
	RecvAck  uint32   // RX data acknowledgement sequence number
	DataOff  uint8    // Data offset
	TCPFlags TCPFlags // TCP flags
	RxWin    uint16   // RX flow control window
	Cksum    uint16   // TCP checksum
	TCPUrp   uint16   // TCP urgent pointer, if any
}

TCPHdr L4 header from DPDK: lib/librte_net/rte_tcp.h

func (*TCPHdr) String

func (hdr *TCPHdr) String() string

type UDPHdr

type UDPHdr struct {
	SrcPort    uint16 // UDP source port
	DstPort    uint16 // UDP destination port
	DgramLen   uint16 // UDP datagram length
	DgramCksum uint16 // UDP datagram checksum
}

UDPHdr L4 header from DPDK: lib/librte_net/rte_udp.h

func (*UDPHdr) String

func (hdr *UDPHdr) String() string

type UDPPort

type UDPPort struct {
	Lenght              uint8 // in 4 octets, here always 0x01
	UDPPortNumber       uint16
	NextExtensionHeader uint8
}

type VLANHdr

type VLANHdr struct {
	TCI       uint16 // Tag control information. Contains PCP, DEI and VID bit-fields
	EtherType uint16 // Real EtherType instead of VLANNumber in EtherHdr.EtherType
}

VLANHdr 802.1Q VLAN header. We interpret it as an addition after EtherHdr structure, so it contains actual frame EtherType after TCI while TPID=0x8100 is present in EtherHdr.

func (*VLANHdr) GetVLANTagIdentifier

func (hdr *VLANHdr) GetVLANTagIdentifier() uint16

GetVLANTagIdentifier returns VID (12 bits of VLAN tag from VLAN header).

func (*VLANHdr) SetVLANTagIdentifier

func (hdr *VLANHdr) SetVLANTagIdentifier(tag uint16)

SetVLANTagIdentifier sets VID (12 bits of VLAN tag to specified value).

func (*VLANHdr) String

func (hdr *VLANHdr) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL