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
- Variables
- func ArrayToIPv4(a [IPv4AddrLen]byte) uint32
- func BytesToIPv4(a byte, b byte, c byte, d byte) uint32
- func CalculateIPv4Checksum(hdr *IPv4Hdr) uint16
- func CalculateIPv4ICMPChecksum(hdr *IPv4Hdr, icmp *ICMPHdr, data unsafe.Pointer) uint16
- func CalculateIPv4TCPChecksum(hdr *IPv4Hdr, tcp *TCPHdr, data unsafe.Pointer) uint16
- func CalculateIPv4UDPChecksum(hdr *IPv4Hdr, udp *UDPHdr, data unsafe.Pointer) uint16
- func CalculateIPv6BroadcastMACForDstMulticastIP(dstMAC *[common.EtherAddrLen]uint8, dstIP [common.IPv6AddrLen]uint8)
- func CalculateIPv6ICMPChecksum(hdr *IPv6Hdr, icmp *ICMPHdr, data unsafe.Pointer) uint16
- func CalculateIPv6LinkLocalAddrForMAC(llAddr *[common.IPv6AddrLen]uint8, mac [common.EtherAddrLen]uint8)
- func CalculateIPv6MulticastAddrForDstIP(muticastAddr *[common.IPv6AddrLen]uint8, dstIP [common.IPv6AddrLen]uint8)
- func CalculateIPv6TCPChecksum(hdr *IPv6Hdr, tcp *TCPHdr, data unsafe.Pointer) uint16
- func CalculateIPv6UDPChecksum(hdr *IPv6Hdr, udp *UDPHdr, data unsafe.Pointer) uint16
- func CalculatePseudoHdrIPv4TCPCksum(hdr *IPv4Hdr) uint16
- func CalculatePseudoHdrIPv4UDPCksum(hdr *IPv4Hdr, udp *UDPHdr) uint16
- func CalculatePseudoHdrIPv6TCPCksum(hdr *IPv6Hdr) uint16
- func CalculatePseudoHdrIPv6UDPCksum(hdr *IPv6Hdr, udp *UDPHdr) uint16
- func ExtractPacketAddr(IN uintptr) uintptr
- func ExtractPackets(packet []*Packet, IN []uintptr, n uint)
- func GeneratePacketFromByte(packet *Packet, data []byte) bool
- func IPv4ToBytes(v uint32) [IPv4AddrLen]byte
- func IPv4ToString(addr uint32) string
- func IPv6ToString(addr [IPv6AddrLen]uint8) string
- func InitARPReplyPacket(packet *Packet, SHA, THA [common.EtherAddrLen]uint8, SPA, TPA uint32) bool
- func InitARPRequestPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA, TPA uint32) bool
- func InitEmptyARPPacket(packet *Packet) bool
- func InitEmptyIPv4ICMPPacket(packet *Packet, plSize uint) bool
- func InitEmptyIPv4Packet(packet *Packet, plSize uint) bool
- func InitEmptyIPv4TCPPacket(packet *Packet, plSize uint) bool
- func InitEmptyIPv4UDPPacket(packet *Packet, plSize uint) bool
- func InitEmptyIPv6ICMPPacket(packet *Packet, plSize uint) bool
- func InitEmptyIPv6Packet(packet *Packet, plSize uint) bool
- func InitEmptyIPv6TCPPacket(packet *Packet, plSize uint) bool
- func InitEmptyIPv6UDPPacket(packet *Packet, plSize uint) bool
- func InitEmptyPacket(packet *Packet, plSize uint) bool
- func InitGARPAnnouncementReplyPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA uint32) bool
- func InitGARPAnnouncementRequestPacket(packet *Packet, SHA [common.EtherAddrLen]uint8, SPA uint32) bool
- func InitICMPv6NeighborAdvertisementPacket(packet *Packet, srcMAC, dstMAC [common.EtherAddrLen]uint8, ...)
- func InitICMPv6NeighborSolicitationPacket(packet *Packet, srcMAC [common.EtherAddrLen]uint8, ...)
- func MACToString(mac [EtherAddrLen]uint8) string
- func ReadPcapGlobalHdr(f io.Reader, glHdr *PcapGlobHdr) error
- func SetHWOffloadingHdrChecksum(p *Packet)
- func SetHWTXChecksumFlag(flag bool)
- func SetNonPerfMempool(m *low.Mempool)
- func SwapBytesUint16(x uint16) uint16
- func SwapBytesUint32(x uint32) uint32
- func WritePcapGlobalHdr(f io.Writer) error
- type ARPHdr
- type EtherHdr
- type GTPHdr
- type ICMPHdr
- type ICMPv6NDMTUOption
- type ICMPv6NDPrefixInformationOption
- type ICMPv6NDRedirectedHeaderOption
- type ICMPv6NDSourceLinkLayerAddressOption
- type ICMPv6NDTargetLinkLayerAddressOption
- type ICMPv6NeighborAdvertisementMessage
- type ICMPv6NeighborSolicitationMessage
- type IPv4Hdr
- type IPv6Hdr
- type L2Rules
- type L3Rules
- type LPM
- type MPLSHdr
- type PDCP_PDU_Number
- type Packet
- func (packet *Packet) AddMPLS(mpls uint32) bool
- func (packet *Packet) AddVLANTag(tag uint16) bool
- func (packet *Packet) DecapsulateHead(start uint, length uint) bool
- func (packet *Packet) DecapsulateIPv4GTP() bool
- func (packet *Packet) DecapsulateTail(start uint, length uint) bool
- func (packet *Packet) EncapsulateHead(start uint, length uint) bool
- func (packet *Packet) EncapsulateIPv4GTP(TEID uint32) bool
- func (packet *Packet) EncapsulateTail(start uint, length uint) bool
- func (packet *Packet) GTPIPv4AllParsing() *GTPHdr
- func (packet *Packet) GTPIPv4FastParsing() *GTPHdr
- func (packet *Packet) GetARP() *ARPHdr
- func (packet *Packet) GetARPCheckVLAN() *ARPHdr
- func (packet *Packet) GetARPNoCheck() *ARPHdr
- func (packet *Packet) GetEtherType() uint16
- func (packet *Packet) GetGTP() *GTPHdr
- func (packet *Packet) GetICMPForIPv4() *ICMPHdr
- func (packet *Packet) GetICMPForIPv6() *ICMPHdr
- func (packet *Packet) GetICMPNoCheck() *ICMPHdr
- func (packet *Packet) GetICMPv6NDSourceLinkLayerAddressOption(msgLength uint) *ICMPv6NDSourceLinkLayerAddressOption
- func (packet *Packet) GetICMPv6NDTargetLinkLayerAddressOption(msgLength uint) *ICMPv6NDSourceLinkLayerAddressOption
- func (packet *Packet) GetICMPv6NeighborAdvertisementMessage() *ICMPv6NeighborAdvertisementMessage
- func (packet *Packet) GetICMPv6NeighborSolicitationMessage() *ICMPv6NeighborSolicitationMessage
- func (packet *Packet) GetIPv4() *IPv4Hdr
- func (packet *Packet) GetIPv4CheckVLAN() *IPv4Hdr
- func (packet *Packet) GetIPv4NoCheck() *IPv4Hdr
- func (packet *Packet) GetIPv6() *IPv6Hdr
- func (packet *Packet) GetIPv6CheckVLAN() *IPv6Hdr
- func (packet *Packet) GetIPv6NoCheck() *IPv6Hdr
- func (packet *Packet) GetMPLS() *MPLSHdr
- func (packet *Packet) GetMPLSNoCheck() *MPLSHdr
- func (packet *Packet) GetPacketLen() uint
- func (packet *Packet) GetPacketPayload() ([]byte, bool)
- func (packet *Packet) GetPacketSegmentLen() uint
- func (packet *Packet) GetRawPacketBytes() []byte
- func (packet *Packet) GetTCPForIPv4() *TCPHdr
- func (packet *Packet) GetTCPForIPv6() *TCPHdr
- func (packet *Packet) GetTCPNoCheck() *TCPHdr
- func (packet *Packet) GetUDPForIPv4() *UDPHdr
- func (packet *Packet) GetUDPForIPv6() *UDPHdr
- func (packet *Packet) GetUDPNoCheck() *UDPHdr
- func (packet *Packet) GetVLAN() *VLANHdr
- func (packet *Packet) GetVLANNoCheck() *VLANHdr
- func (pkt *Packet) L2ACLPermit(rules *L2Rules) bool
- func (pkt *Packet) L2ACLPort(rules *L2Rules) uint
- func (pkt *Packet) L3ACLPermit(rules *L3Rules) bool
- func (pkt *Packet) L3ACLPort(rules *L3Rules) uint
- func (packet *Packet) PacketBytesChange(start uint, bytes []byte) bool
- func (packet *Packet) ParseAllKnownL3() (*IPv4Hdr, *IPv6Hdr, *ARPHdr)
- func (packet *Packet) ParseAllKnownL3CheckVLAN() (*IPv4Hdr, *IPv6Hdr, *ARPHdr)
- func (packet *Packet) ParseAllKnownL4ForIPv4() (*TCPHdr, *UDPHdr, *ICMPHdr)
- func (packet *Packet) ParseAllKnownL4ForIPv6() (*TCPHdr, *UDPHdr, *ICMPHdr)
- func (packet *Packet) ParseData() int
- func (packet *Packet) ParseDataCheckVLAN() int
- func (packet *Packet) ParseL3()
- func (packet *Packet) ParseL3CheckMPLS() *MPLSHdr
- func (packet *Packet) ParseL3CheckVLAN() *VLANHdr
- func (packet *Packet) ParseL4ForIPv4()
- func (packet *Packet) ParseL4ForIPv6()
- func (packet *Packet) ParseL7(protocol uint)
- func (pkt *Packet) ReadPcapOnePacket(f io.Reader) (bool, error)
- func (packet *Packet) RemoveMPLS() bool
- func (packet *Packet) RemoveVLANTag() bool
- func (p *Packet) SendPacket(port uint16) bool
- func (packet *Packet) SetHWCksumOLFlags()
- func (packet *Packet) SetTXIPv4OLFlags(l2len, l3len uint32)
- func (packet *Packet) SetTXIPv4TCPOLFlags(l2len, l3len uint32)
- func (packet *Packet) SetTXIPv4UDPOLFlags(l2len, l3len uint32)
- func (packet *Packet) SetTXIPv6TCPOLFlags(l2len, l3len uint32)
- func (packet *Packet) SetTXIPv6UDPOLFlags(l2len, l3len uint32)
- func (packet *Packet) StartAtOffset(offset uintptr) unsafe.Pointer
- func (p *Packet) ToUintptr() uintptr
- func (pkt *Packet) WritePcapOnePacket(f io.Writer) error
- type PcapGlobHdr
- type PcapRecHdr
- type TCPHdr
- type UDPHdr
- type UDPPort
- type VLANHdr
Constants ¶
const ( ARPRequest = 1 ARPReply = 2 )
ARP protocol operations
const ( NoExtensionHeaders = 0x00 UDPPortExtensionHeader = 0x40 PDCP_PDU_NumberExtensionHeader = 0xc0 )
TODO current support of GTP-U: only two extension headers NextExtensionHeader values
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
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 )
const PcapGlobHdrSize int64 = 24
PcapGlobHdrSize is a size of cap global header.
Variables ¶
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 ¶
ArrayToIPv4 converts four element array to uint32 representation
func BytesToIPv4 ¶
BytesToIPv4 converts four element address to uint32 representation
func CalculateIPv4Checksum ¶
CalculateIPv4Checksum calculates checksum of IP header
func CalculateIPv4ICMPChecksum ¶
CalculateIPv4ICMPChecksum calculates ICMP checksum in case if L3 protocol is IPv4.
func CalculateIPv4TCPChecksum ¶
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 ¶
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 ¶
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 ¶
CalculateIPv6TCPChecksum calculates TCP checksum for case if L3 protocol is IPv6.
func CalculateIPv6UDPChecksum ¶
CalculateIPv6UDPChecksum calculates UDP checksum for case if L3 protocol is IPv6.
func CalculatePseudoHdrIPv4TCPCksum ¶
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 ¶
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 ¶
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 ¶
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 ¶
ExtractPacketAddr extracts packet structure from mbuf used in package flow
func ExtractPackets ¶
ExtractPackets creates vector of packets by calling ExtractPacket function is unexported, used in flow package
func GeneratePacketFromByte ¶
GeneratePacketFromByte function gets non-initialized packet and slice of bytes of any size. Initializes input packet and fills it with these bytes.
func IPv4ToBytes ¶
IPv4ToBytes converts four element address to uint32 representation
func IPv4ToString ¶
func IPv6ToString ¶
func InitARPReplyPacket ¶
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 ¶
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 ¶
InitEmptyARPPacket initializes empty ARP packet
func InitEmptyIPv4ICMPPacket ¶
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 ¶
InitEmptyIPv4Packet initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet and IPv4 headers.
func InitEmptyIPv4TCPPacket ¶
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 ¶
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 ¶
InitEmptyIPv6ICMPPacket initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet, IPv6 and ICMP headers.
func InitEmptyIPv6Packet ¶
InitEmptyIPv6Packet initializes input packet with preallocated plSize of bytes for payload and init pointers to Ethernet and IPv6 headers.
func InitEmptyIPv6TCPPacket ¶
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 ¶
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 ¶
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 ¶
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 ¶
SetNonPerfMempool sets default mempool for non performance critical allocations. Shouldn't be called by user
func SwapBytesUint16 ¶
SwapBytesUint16 swaps uint16 in Little Endian and Big Endian
func SwapBytesUint32 ¶
SwapBytesUint32 swaps uint32 in Little Endian and Big Endian
func WritePcapGlobalHdr ¶
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
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
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
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.
type ICMPv6NDMTUOption ¶
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
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
type L2Rules ¶
type L2Rules struct {
// contains filtered or unexported fields
}
L2Rules - struct for rules of l2 level
func GetL2ACLFromJSON ¶
GetL2ACLFromJSON gets name of JSON structed file with L2 rules, returns L2Rules
func GetL2ACLFromORIG ¶
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 ¶
GetL3ACLFromJSON gets name of JSON structed file with combined L3 and L4 rules, returns L2Rules
func GetL3ACLFromORIG ¶
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 ¶
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 ¶
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 ¶
Delete removes longest prefix match rule with diven ip and depth from LPM table. Returns 0 if success and negative value otherwise
func (*LPM) Lookup ¶
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 ¶
SetMPLSLabel sets Label (20 first bits of MPLS header to specified value).
func (*MPLSHdr) GetMPLSLabel ¶
GetMPLSLabel returns Label (20 first bits of MPLS header).
func (*MPLSHdr) GetMPLSTTL ¶
GetMPLSTTL returns the Time-to-Live value
func (*MPLSHdr) SetMPLSLabel ¶
SetMPLSLabel sets Label (20 first bits of MPLS header to specified value).
type PDCP_PDU_Number ¶
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 ¶
ExtractPacket extracts packet structure from mbuf used in package flow.
func NewPacket ¶
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 (*Packet) AddMPLS ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) GetARPCheckVLAN ¶
GetARPCheckVLAN ensures if EtherType is ARP and casts L3 pointer to ARPHdr type. VLAN presence is checked if necessary.
func (*Packet) GetARPNoCheck ¶
GetARPNoCheck casts L3 pointer to ARPHdr type.
func (*Packet) GetEtherType ¶
GetEtherType correctly returns EtherType from Ethernet header or VLAN header.
func (*Packet) GetGTP ¶
GetGTP assumes that packet is already parsed. Returns GTP header as payload after L4 header
func (*Packet) GetICMPForIPv4 ¶
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 ¶
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 ¶
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) GetIPv4CheckVLAN ¶
GetIPv4CheckVLAN ensures if EtherType is IPv4 and casts L3 pointer to IPv4Hdr type. VLAN presence is checked if necessary.
func (*Packet) GetIPv4NoCheck ¶
GetIPv4NoCheck casts L3 pointer to IPv4Hdr type.
func (*Packet) GetIPv6CheckVLAN ¶
GetIPv6CheckVLAN ensures if EtherType is IPv6 and cast L3 pointer to IPv6Hdr type. VLAN presence is checked if necessary.
func (*Packet) GetIPv6NoCheck ¶
GetIPv6NoCheck ensures if EtherType is IPv6 and cast L3 pointer to IPv6Hdr type.
func (*Packet) GetMPLSNoCheck ¶
GetMPLSNoCheck casts pointer to memory right after Ethernet header to MPLSHdr type.
func (*Packet) GetPacketLen ¶
GetPacketLen returns length of this packet. Sum of length of all segments if scattered.
func (*Packet) GetPacketPayload ¶
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 ¶
GetPacketSegmentLen returns length of this segment of packet. It is equal to whole length if packet not scattered
func (*Packet) GetRawPacketBytes ¶
GetRawPacketBytes returns all bytes from this packet. Not zero-copy.
func (*Packet) GetTCPForIPv4 ¶
GetTCPForIPv4 ensures if L4 type is TCP and cast L4 pointer to TCPHdr type.
func (*Packet) GetTCPForIPv6 ¶
GetTCPForIPv6 ensures if L4 type is TCP and cast L4 pointer to *TCPHdr type.
func (*Packet) GetTCPNoCheck ¶
GetTCPNoCheck casts L4 pointer to TCPHdr type.
func (*Packet) GetUDPForIPv4 ¶
GetUDPForIPv4 ensures if L4 type is UDP and cast L4 pointer to *UDPHdr type.
func (*Packet) GetUDPForIPv6 ¶
GetUDPForIPv6 ensures if L4 type is UDP and cast L4 pointer to *UDPHdr type.
func (*Packet) GetUDPNoCheck ¶
GetUDPNoCheck casts L4 pointer to *UDPHdr type.
func (*Packet) GetVLANNoCheck ¶
GetVLANNoCheck casts pointer to memory right after Ethernet header to VLANHdr type.
func (*Packet) L2ACLPermit ¶
L2ACLPermit gets packet (with parsed L2) and L2Rules. Returns accept or reject for this packet
func (*Packet) L2ACLPort ¶
L2ACLPort gets packet (with parsed L2) and L2Rules. Returns number of output for packet
func (*Packet) L3ACLPermit ¶
L3ACLPermit gets packet (with parsed L3 or L3 with L4) and L3Rules. Returns accept or reject for this packet
func (*Packet) L3ACLPort ¶
L3ACLPort gets packet (with parsed L3 or L3 with L4) and L3Rules. Returns number of output for this packet
func (*Packet) PacketBytesChange ¶
PacketBytesChange changes packet bytes from start byte to given bytes. Return false if error.
func (*Packet) ParseAllKnownL3 ¶
ParseAllKnownL3 parses L3 field and returns pointers to parsed headers.
func (*Packet) ParseAllKnownL3CheckVLAN ¶
ParseAllKnownL3CheckVLAN parses L3 field and returns pointers to parsed headers taking possible presence of VLAN header into account.
func (*Packet) ParseAllKnownL4ForIPv4 ¶
ParseAllKnownL4ForIPv4 parses L4 field if L3 type is IPv4 and returns pointers to parsed headers.
func (*Packet) ParseAllKnownL4ForIPv6 ¶
ParseAllKnownL4ForIPv6 parses L4 field if L3 type is IPv6 and returns pointers to parsed headers.
func (*Packet) ParseData ¶
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 ¶
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) ParseL3CheckMPLS ¶
ParseL3CheckMPLS set pointer to start of L3 header taking possible presence of MPLS header into account.
func (*Packet) ParseL3CheckVLAN ¶
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) ReadPcapOnePacket ¶
ReadPcapOnePacket read one packet with pcap header from file. Assumes that global pcap header is already read.
func (*Packet) RemoveMPLS ¶
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 ¶
RemoveVLANTag decreases size of packet on VLANLen
func (*Packet) SendPacket ¶
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 ¶
SetTXIPv4OLFlags sets mbuf flags for IPv4 header checksum calculation offloading.
func (*Packet) SetTXIPv4TCPOLFlags ¶
SetTXIPv4TCPOLFlags sets mbuf flags for IPv4 and TCP headers checksum calculation hardware offloading.
func (*Packet) SetTXIPv4UDPOLFlags ¶
SetTXIPv4UDPOLFlags sets mbuf flags for IPv4 and UDP headers checksum calculation hardware offloading.
func (*Packet) SetTXIPv6TCPOLFlags ¶
SetTXIPv6TCPOLFlags sets mbuf flags for IPv6 TCP header checksum calculation hardware offloading.
func (*Packet) SetTXIPv6UDPOLFlags ¶
SetTXIPv6UDPOLFlags sets mbuf flags for IPv6 UDP header checksum calculation hardware offloading.
func (*Packet) StartAtOffset ¶
StartAtOffset function return pointer to first byte of packet with given offset.
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
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
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 ¶
GetVLANTagIdentifier returns VID (12 bits of VLAN tag from VLAN header).
func (*VLANHdr) SetVLANTagIdentifier ¶
SetVLANTagIdentifier sets VID (12 bits of VLAN tag to specified value).