inetdiag

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package inetdiag provides basic structs and utilities for INET_DIAG messaages. Based on uapi/linux/inet_diag.h.

Index

Constants

View Source
const (
	INET_DIAG_NONE = iota
	INET_DIAG_MEMINFO
	INET_DIAG_INFO
	INET_DIAG_VEGASINFO
	INET_DIAG_CONG
	INET_DIAG_TOS
	INET_DIAG_TCLASS
	INET_DIAG_SKMEMINFO
	INET_DIAG_SHUTDOWN
	INET_DIAG_DCTCPINFO
	INET_DIAG_PROTOCOL
	INET_DIAG_SKV6ONLY
	INET_DIAG_LOCALS
	INET_DIAG_PEERS
	INET_DIAG_PAD
	INET_DIAG_MARK
	INET_DIAG_BBRINFO
	INET_DIAG_CLASS_ID
	INET_DIAG_MD5SIG
	// TODO - Should check whether this matches the current linux header.
	INET_DIAG_MAX
)
View Source
const AF_INET = 0x02

NOTE: windows does not have unix.AF_INET available.

View Source
const AF_INET6 = 0x0a

NOTE: darwin unix.AF_INET6 and syscall.AF_INET6 are incorrect for our purposes (0x1e), so, we set this explicitly.

View Source
const (
	// RTA_ALIGNTO previously came from syscall, but explicit here to work on Darwin.
	RTA_ALIGNTO = 4
)
View Source
const (
	SOCK_DIAG_BY_FAMILY = 20 // uapi/linux/sock_diag.h
)

Constants from linux.

View Source
const SizeofReqV2 = int(unsafe.Sizeof(ReqV2{})) // Should be 0x38

SizeofReqV2 is the size of the struct. TODO should we just make this explicit in the code?

Variables

View Source
var (
	// ErrBadPid is used when the PID is mismatched between the netlink socket and the calling process.
	ErrBadPid = errors.New("bad PID, can't listen to NL socket")

	// ErrBadSequence is used when the Netlink response has a bad sequence number.
	ErrBadSequence = errors.New("bad sequence number, can't interpret NetLink response")

	// ErrBadMsgData is used when the NHetlink response has bad or missing data.
	ErrBadMsgData = errors.New("bad message data from netlink message")
)
View Source
var ErrParseFailed = errors.New("Unable to parse InetDiagMsg")

ErrParseFailed is returned if InetDiagMsg parsing fails.

View Source
var ErrUnknownAF = errors.New("unknown address family")

ErrUnknownAF is returned when the InetDiagMsg.IDiagFamily is unknown.

View Source
var InetDiagType = map[int32]string{
	INET_DIAG_MEMINFO:   "MemInfo",
	INET_DIAG_INFO:      "TCPInfo",
	INET_DIAG_VEGASINFO: "Vegas",
	INET_DIAG_CONG:      "Congestion",
	INET_DIAG_TOS:       "TOS",
	INET_DIAG_TCLASS:    "TClass",
	INET_DIAG_SKMEMINFO: "SKMemInfo",
	INET_DIAG_SHUTDOWN:  "Shutdown",
	INET_DIAG_DCTCPINFO: "DCTCPInfo",
	INET_DIAG_PROTOCOL:  "Protocol",
	INET_DIAG_SKV6ONLY:  "SKV6Only",
	INET_DIAG_LOCALS:    "Locals",
	INET_DIAG_PEERS:     "Peers",
	INET_DIAG_PAD:       "Pad",
	INET_DIAG_MARK:      "Mark",
	INET_DIAG_BBRINFO:   "BBRInfo",
	INET_DIAG_CLASS_ID:  "ClassID",
	INET_DIAG_MD5SIG:    "MD5Sig",
}

InetDiagType provides human readable strings for decoding attribute types.

View Source
var ProtocolName = map[int32]string{
	0:  "IPPROTO_UNUSED",
	6:  "IPPROTO_TCP",
	17: "IPPROTO_UDP",
	33: "IPPROTO_DCCP",
}

ProtocolName is used to convert Protocol values to strings.

Functions

func MakeReq

func MakeReq(inetType uint8) *nl.NetlinkRequest

TODO - Figure out why we aren't seeing INET_DIAG_DCTCPINFO or INET_DIAG_BBRINFO messages.

func ProcessMessage

func ProcessMessage(m *syscall.NetlinkMessage, seq uint32, pid uint32) (msg *syscall.NetlinkMessage, shouldContinue bool, err error)

Types

type BBRInfo

type BBRInfo struct {
	BW         int64
	MinRTT     uint32
	PacingGain uint32
	CwndGain   uint32
}

BBRInfo implements the struct associated with INET_DIAG_BBRINFO attribute, corresponding with linux struct tcp_bbr_info in uapi/linux/inet_diag.h.

type DCTCPInfo

type DCTCPInfo struct {
	Enabled uint16
	CEState uint16
	Alpha   uint32
	ABEcn   uint32
	ABTot   uint32
}

DCTCPInfo implements the struct associated with INET_DIAG_DCTCPINFO attribute, corresponding with linux struct tcp_dctcp_info in uapi/linux/inet_diag.h.

type HostCond

type HostCond struct {
	Family    uint8  // __u8 family
	PrefixLen uint8  // __u8 prefix_len
	Port      uint16 // int port
	Addr      uint32 // __be32	addr[0];
}

HostCond is related to filters. We don't currently use filters, so we don't actually use this type.

type InetDiagMsg

type InetDiagMsg struct {
	IDiagFamily  uint8
	IDiagState   uint8
	IDiagTimer   uint8
	IDiagRetrans uint8
	ID           LinuxSockID
	IDiagExpires uint32
	IDiagRqueue  uint32
	IDiagWqueue  uint32
	IDiagUID     uint32
	IDiagInode   uint32
}

InetDiagMsg is the linux binary representation of a InetDiag message header, as in linux/inet_diag.h Note that netlink messages use host byte ordering, unless NLA_F_NET_BYTEORDER flag is present.

type LinuxSockID

type LinuxSockID struct {
	IDiagSPort  Port
	IDiagDPort  Port
	IDiagSrc    ipType
	IDiagDst    ipType
	IDiagIf     netIF
	IDiagCookie cookieType
}

LinuxSockID is the binary linux representation of a socket, as in linux/inet_diag.h Linux code comments indicate this struct uses the network byte order!!! All fields are ignored for bigquery, and handled in code. TODO make this unexported

func (*LinuxSockID) Cookie

func (id *LinuxSockID) Cookie() uint64

Cookie returns the LinuxSockID's 64 bit unsigned cookie.

func (*LinuxSockID) DPort

func (id *LinuxSockID) DPort() uint16

DPort returns the host byte ordered port. In general, Netlink is supposed to use host byte order, but this seems to be an exception. Perhaps Netlink is reading a tcp stack structure that holds the port in network byte order.

func (*LinuxSockID) DstIP

func (id *LinuxSockID) DstIP() net.IP

DstIP returns a golang net encoding of destination address.

func (*LinuxSockID) GetSockID

func (id *LinuxSockID) GetSockID() SockID

GetSockID extracts the SockID from the LinuxSockID.

func (*LinuxSockID) Interface

func (id *LinuxSockID) Interface() uint32

Interface returns the interface number.

func (*LinuxSockID) SPort

func (id *LinuxSockID) SPort() uint16

SPort returns the host byte ordered port. In general, Netlink is supposed to use host byte order, but this seems to be an exception. Perhaps Netlink is reading a tcp stack structure that holds the port in network byte order.

func (*LinuxSockID) SrcIP

func (id *LinuxSockID) SrcIP() net.IP

SrcIP returns a golang net encoding of source address.

type MarkCond

type MarkCond struct {
	Mark uint32
	Mask uint32
}

MarkCond is related to filters. We don't currently use filters, so we don't actually use this type.

type MemInfo

type MemInfo struct {
	Rmem uint32
	Wmem uint32
	Fmem uint32
	Tmem uint32
}

MemInfo implements the struct associated with INET_DIAG_MEMINFO, corresponding with linux struct inet_diag_meminfo in uapi/linux/inet_diag.h.

type Port

type Port [2]byte

Port encodes a LinuxSockID Port

func (*Port) Marshal

func (p *Port) Marshal() (string, error)

type Protocol

type Protocol uint8

Protocol defines the type corresponding to INET_DIAG_PROTOCOL 8 bit field.

const (
	// Protocol_IPPROTO_UNUSED ...
	Protocol_IPPROTO_UNUSED Protocol = 0
	// Protocol_IPPROTO_TCP    indicates TCP traffic.
	Protocol_IPPROTO_TCP Protocol = 6
	// Protocol_IPPROTO_UDP   indicates UDP traffic.
	Protocol_IPPROTO_UDP Protocol = 17
	// Protocol_IPPROTO_DCCP indicates DCCP traffic.
	Protocol_IPPROTO_DCCP Protocol = 33
)

type RawInetDiagMsg

type RawInetDiagMsg []byte

RawInetDiagMsg holds the []byte representation of an InetDiagMsg

func SplitInetDiagMsg

func SplitInetDiagMsg(data []byte) (RawInetDiagMsg, []byte)

SplitInetDiagMsg pulls the InetDiagMsg out, and returns the msg and the remaining data slice.

func (RawInetDiagMsg) Parse

func (raw RawInetDiagMsg) Parse() (*InetDiagMsg, error)

Parse returns the InetDiagMsg itself Modified from original to also return attribute data array.

type ReqV2

type ReqV2 struct {
	SDiagFamily   uint8
	SDiagProtocol uint8
	IDiagExt      uint8
	Pad           uint8
	IDiagStates   uint32
	ID            LinuxSockID
}

ReqV2 is the Netlink request struct, as in linux/inet_diag.h Note that netlink messages use host byte ordering, unless NLA_F_NET_BYTEORDER flag is present.

func NewReqV2

func NewReqV2(family, protocol uint8, states uint32) *ReqV2

NewReqV2 creates a new request.

func (*ReqV2) Len

func (req *ReqV2) Len() int

Len is provided for json serialization?

func (*ReqV2) Serialize

func (req *ReqV2) Serialize() []byte

Serialize is provided for json serialization? TODO - should use binary functions instead?

type SockID

type SockID struct {
	SPort     uint16
	DPort     uint16
	SrcIP     string
	DstIP     string
	Interface uint32
	Cookie    uint64
}

SockID is the natural golang struct equivalent of LinuxSockID

type SocketMemInfo

type SocketMemInfo struct {
	RmemAlloc  uint32
	Rcvbuf     uint32
	WmemAlloc  uint32
	Sndbuf     uint32
	FwdAlloc   uint32
	WmemQueued uint32
	Optmem     uint32
	Backlog    uint32
	Drops      uint32
}

SocketMemInfo implements the struct associated with INET_DIAG_SKMEMINFO Haven't found a corresponding linux struct, but the message is described in https://manpages.debian.org/stretch/manpages/sock_diag.7.en.html

type VegasInfo

type VegasInfo struct {
	Enabled  uint32
	RTTCount uint32
	RTT      uint32
	MinRTT   uint32
}

VegasInfo implements the struct associated with INET_DIAG_VEGASINFO, corresponding with linux struct tcpvegas_info in uapi/linux/inet_diag.h.

Jump to

Keyboard shortcuts

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