nl

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package nl has low level primitives for making Netlink calls.

Index

Constants

View Source
const (
	SizeofNfgenmsg      = 4
	SizeofNfattr        = 4
	SizeofNfConntrack   = 376
	SizeofNfctTupleHead = 52
)

Track the message sizes for the correct serialization/deserialization

View Source
const (
	IPCTNL_MSG_CT_GET    = 1
	IPCTNL_MSG_CT_DELETE = 2
)
enum cntl_msg_types {
	IPCTNL_MSG_CT_NEW,
	IPCTNL_MSG_CT_GET,
	IPCTNL_MSG_CT_DELETE,
	IPCTNL_MSG_CT_GET_CTRZERO,
	IPCTNL_MSG_CT_GET_STATS_CPU,
	IPCTNL_MSG_CT_GET_STATS,
	IPCTNL_MSG_CT_GET_DYING,
	IPCTNL_MSG_CT_GET_UNCONFIRMED,

	IPCTNL_MSG_MAX
};
View Source
const (
	CTA_TUPLE_ORIG     = 1
	CTA_TUPLE_REPLY    = 2
	CTA_STATUS         = 3
	CTA_TIMEOUT        = 7
	CTA_MARK           = 8
	CTA_COUNTERS_ORIG  = 9
	CTA_COUNTERS_REPLY = 10
	CTA_PROTOINFO      = 4
)
enum ctattr_type {
	CTA_UNSPEC,
	CTA_TUPLE_ORIG,
	CTA_TUPLE_REPLY,
	CTA_STATUS,
	CTA_PROTOINFO,
	CTA_HELP,
	CTA_NAT_SRC,

#define CTA_NAT CTA_NAT_SRC /* backwards compatibility */

	CTA_TIMEOUT,
	CTA_MARK,
	CTA_COUNTERS_ORIG,
	CTA_COUNTERS_REPLY,
	CTA_USE,
	CTA_ID,
	CTA_NAT_DST,
	CTA_TUPLE_MASTER,
	CTA_SEQ_ADJ_ORIG,
	CTA_NAT_SEQ_ADJ_ORIG	= CTA_SEQ_ADJ_ORIG,
	CTA_SEQ_ADJ_REPLY,
	CTA_NAT_SEQ_ADJ_REPLY	= CTA_SEQ_ADJ_REPLY,
	CTA_SECMARK,		/* obsolete */
	CTA_ZONE,
	CTA_SECCTX,
	CTA_TIMESTAMP,
	CTA_MARK_MASK,
	CTA_LABELS,
	CTA_LABELS_MASK,
	__CTA_MAX
};
View Source
const (
	CTA_TUPLE_IP    = 1
	CTA_TUPLE_PROTO = 2
)
enum ctattr_tuple {
	CTA_TUPLE_UNSPEC,
	CTA_TUPLE_IP,
	CTA_TUPLE_PROTO,
	CTA_TUPLE_ZONE,
	__CTA_TUPLE_MAX
};

#define CTA_TUPLE_MAX (__CTA_TUPLE_MAX - 1)

View Source
const (
	CTA_IP_V4_SRC = 1
	CTA_IP_V4_DST = 2
	CTA_IP_V6_SRC = 3
	CTA_IP_V6_DST = 4
)
enum ctattr_ip {
	CTA_IP_UNSPEC,
	CTA_IP_V4_SRC,
	CTA_IP_V4_DST,
	CTA_IP_V6_SRC,
	CTA_IP_V6_DST,
	__CTA_IP_MAX
};

#define CTA_IP_MAX (__CTA_IP_MAX - 1)

View Source
const (
	CTA_PROTO_NUM      = 1
	CTA_PROTO_SRC_PORT = 2
	CTA_PROTO_DST_PORT = 3
)
enum ctattr_l4proto {
	CTA_PROTO_UNSPEC,
	CTA_PROTO_NUM,
	CTA_PROTO_SRC_PORT,
	CTA_PROTO_DST_PORT,
	CTA_PROTO_ICMP_ID,
	CTA_PROTO_ICMP_TYPE,
	CTA_PROTO_ICMP_CODE,
	CTA_PROTO_ICMPV6_ID,
	CTA_PROTO_ICMPV6_TYPE,
	CTA_PROTO_ICMPV6_CODE,
	__CTA_PROTO_MAX
};

#define CTA_PROTO_MAX (__CTA_PROTO_MAX - 1)

View Source
const (
	CTA_PROTOINFO_TCP_STATE           = 1
	CTA_PROTOINFO_TCP_WSCALE_ORIGINAL = 2
	CTA_PROTOINFO_TCP_WSCALE_REPLY    = 3
	CTA_PROTOINFO_TCP_FLAGS_ORIGINAL  = 4
	CTA_PROTOINFO_TCP_FLAGS_REPLY     = 5
)
enum ctattr_protoinfo_tcp {
	CTA_PROTOINFO_TCP_UNSPEC,
	CTA_PROTOINFO_TCP_STATE,
	CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
	CTA_PROTOINFO_TCP_WSCALE_REPLY,
	CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
	CTA_PROTOINFO_TCP_FLAGS_REPLY,
	__CTA_PROTOINFO_TCP_MAX
};

#define CTA_PROTOINFO_TCP_MAX (__CTA_PROTOINFO_TCP_MAX - 1)

View Source
const (
	CTA_COUNTERS_PACKETS = 1
	CTA_COUNTERS_BYTES   = 2
)
enum ctattr_counters {
	CTA_COUNTERS_UNSPEC,
	CTA_COUNTERS_PACKETS,		/* 64bit counters */
	CTA_COUNTERS_BYTES,		/* 64bit counters */
	CTA_COUNTERS32_PACKETS,		/* old 32bit counters, unused */
	CTA_COUNTERS32_BYTES,		/* old 32bit counters, unused */
	CTA_COUNTERS_PAD,
	__CTA_COUNTERS_M
};

#define CTA_COUNTERS_MAX (__CTA_COUNTERS_MAX - 1)

View Source
const (
	// Family type definitions
	FAMILY_ALL  = unix.AF_UNSPEC
	FAMILY_V4   = unix.AF_INET
	FAMILY_V6   = unix.AF_INET6
	FAMILY_MPLS = AF_MPLS
	// Arbitrary set value (greater than default 4k) to allow receiving
	// from kernel more verbose messages e.g. for statistics,
	// tc rules or filters, or other more memory requiring data.
	RECEIVE_BUFFER_SIZE = 65536
)
View Source
const (
	FRA_UNSPEC  = iota
	FRA_DST     /* destination address */
	FRA_SRC     /* source address */
	FRA_IIFNAME /* interface name */
	FRA_GOTO    /* target to jump to (FR_ACT_GOTO) */
	FRA_UNUSED2
	FRA_PRIORITY /* priority/preference */
	FRA_UNUSED3
	FRA_UNUSED4
	FRA_UNUSED5
	FRA_FWMARK /* mark */
	FRA_FLOW   /* flow/class id */
	FRA_TUN_ID
	FRA_SUPPRESS_IFGROUP
	FRA_SUPPRESS_PREFIXLEN
	FRA_TABLE  /* Extended table id */
	FRA_FWMASK /* mask for netfilter mark */
	FRA_OIFNAME
)

syscall package lack of rule atributes type. Thus there are defined below

View Source
const (
	FR_ACT_UNSPEC = iota
	FR_ACT_TO_TBL /* Pass to fixed table */
	FR_ACT_GOTO   /* Jump to another rule */
	FR_ACT_NOP    /* No operation */
	FR_ACT_RES3
	FR_ACT_RES4
	FR_ACT_BLACKHOLE   /* Drop without notification */
	FR_ACT_UNREACHABLE /* Drop with ENETUNREACH */
	FR_ACT_PROHIBIT    /* Drop with EACCES */
)

ip rule netlink request types

View Source
const (
	SOCK_DIAG_BY_FAMILY = 20         /* linux.sock_diag.h */
	TCPDIAG_NOCOOKIE    = 0xFFFFFFFF /* TCPDIAG_NOCOOKIE in net/ipv4/tcp_diag.h*/
)

socket diags related

View Source
const (
	RTA_NEWDST     = 0x13
	RTA_ENCAP_TYPE = 0x15
	RTA_ENCAP      = 0x16
)
View Source
const (
	MPLS_IPTUNNEL_UNSPEC = iota
	MPLS_IPTUNNEL_DST
)

RTA_ENCAP subtype

View Source
const (
	LWTUNNEL_ENCAP_NONE = iota
	LWTUNNEL_ENCAP_MPLS
	LWTUNNEL_ENCAP_IP
	LWTUNNEL_ENCAP_ILA
	LWTUNNEL_ENCAP_IP6
	LWTUNNEL_ENCAP_SEG6
	LWTUNNEL_ENCAP_BPF
	LWTUNNEL_ENCAP_SEG6_LOCAL
)

light weight tunnel encap types

View Source
const (
	IPV6_SRCRT_STRICT = 0x01 // Deprecated; will be removed
	IPV6_SRCRT_TYPE_0 = 0    // Deprecated; will be removed
	IPV6_SRCRT_TYPE_2 = 2    // IPv6 type 2 Routing Header
	IPV6_SRCRT_TYPE_4 = 4    // Segment Routing with IPv6
)

routing header types

View Source
const (
	AF_MPLS = 28
)
View Source
const (
	CTA_PROTOINFO_TCP = 1
)
enum ctattr_protoinfo {
	CTA_PROTOINFO_UNSPEC,
	CTA_PROTOINFO_TCP,
	CTA_PROTOINFO_DCCP,
	CTA_PROTOINFO_SCTP,
	__CTA_PROTOINFO_MAX
};

#define CTA_PROTOINFO_MAX (__CTA_PROTOINFO_MAX - 1)

View Source
const (
	NFNETLINK_V0 = 0
)

#define NFNETLINK_V0 0

View Source
const (
	NLA_F_NESTED = (1 << 15)
)

#define NLA_F_NESTED (1 << 15)

Variables

View Source
var L4ProtoMap = map[uint8]string{
	6:  "tcp",
	17: "udp",
}
View Source
var SupportedNlFamilies = []int{}

SupportedNlFamilies contains the list of netlink families this netlink package supports

Functions

func BytesToString added in v1.1.0

func BytesToString(b []byte) string

func GetIPFamily added in v1.1.0

func GetIPFamily(ip net.IP) int

GetIPFamily returns the family type of a net.IP.

func NativeEndian

func NativeEndian() binary.ByteOrder

Get native endianness for the system

func NonZeroTerminated added in v1.1.0

func NonZeroTerminated(s string) []byte

func ParseZeroTerminated added in v1.1.0

func ParseZeroTerminated(data []byte) string

func Swap16 added in v1.1.0

func Swap16(i uint16) uint16

Byte swap a 16 bit value if we aren't big endian

func Swap32 added in v1.1.0

func Swap32(i uint32) uint32

Byte swap a 32 bit value if aren't big endian

func Uint16Attr added in v1.1.0

func Uint16Attr(v uint16) []byte

func Uint16AttrNetEndian added in v1.1.0

func Uint16AttrNetEndian(v uint16) []byte

func Uint32Attr added in v1.1.0

func Uint32Attr(v uint32) []byte

func Uint32AttrNetEndian added in v1.1.0

func Uint32AttrNetEndian(v uint32) []byte

func Uint64Attr added in v1.1.0

func Uint64Attr(v uint64) []byte

func Uint64AttrNetEndian added in v1.1.0

func Uint64AttrNetEndian(v uint64) []byte

func Uint8Attr added in v1.1.0

func Uint8Attr(v uint8) []byte

func ZeroTerminated added in v1.1.0

func ZeroTerminated(s string) []byte

Types

type IfInfomsg added in v1.1.0

type IfInfomsg struct {
}

IfInfomsg is related to links, but it is used for list requests as well

func DeserializeIfInfomsg added in v1.1.0

func DeserializeIfInfomsg(b []byte) *IfInfomsg

func NewIfInfomsg added in v1.1.0

func NewIfInfomsg(family int) *IfInfomsg

Create an IfInfomsg with family specified

func NewIfInfomsgChild added in v1.1.0

func NewIfInfomsgChild(parent *RtAttr, family int) *IfInfomsg

func (*IfInfomsg) EncapType added in v1.1.0

func (msg *IfInfomsg) EncapType() string

func (*IfInfomsg) Len added in v1.1.0

func (msg *IfInfomsg) Len() int

func (*IfInfomsg) Serialize added in v1.1.0

func (msg *IfInfomsg) Serialize() []byte

type NetlinkRequest added in v1.1.0

type NetlinkRequest struct {
	Data    []NetlinkRequestData
	RawData []byte
	Sockets map[int]*SocketHandle
}

func NewNetlinkRequest added in v1.1.0

func NewNetlinkRequest(proto, flags int) *NetlinkRequest

Create a new netlink request from proto and flags Note the Len value will be inaccurate once data is added until the message is serialized

func (*NetlinkRequest) AddData added in v1.1.0

func (req *NetlinkRequest) AddData(data NetlinkRequestData)

func (*NetlinkRequest) AddRawData added in v1.1.0

func (req *NetlinkRequest) AddRawData(data []byte)

AddRawData adds raw bytes to the end of the NetlinkRequest object during serialization

func (*NetlinkRequest) Execute added in v1.1.0

func (req *NetlinkRequest) Execute(sockType int, resType uint16) ([][]byte, error)

Execute the request against a the given sockType. Returns a list of netlink messages in serialized format, optionally filtered by resType.

func (*NetlinkRequest) Serialize added in v1.1.0

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

Serialize the Netlink Request into a byte array

type NetlinkRequestData added in v1.1.0

type NetlinkRequestData interface {
	Len() int
	Serialize() []byte
}

type NetlinkSocket added in v1.1.0

type NetlinkSocket struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func GetNetlinkSocketAt added in v1.1.0

func GetNetlinkSocketAt(newNs, curNs netns.NsHandle, protocol int) (*NetlinkSocket, error)

GetNetlinkSocketAt opens a netlink socket in the network namespace newNs and positions the thread back into the network namespace specified by curNs, when done. If curNs is close, the function derives the current namespace and moves back into it when done. If newNs is close, the socket will be opened in the current network namespace.

func Subscribe added in v1.1.0

func Subscribe(protocol int, groups ...uint) (*NetlinkSocket, error)

Create a netlink socket with a given protocol (e.g. NETLINK_ROUTE) and subscribe it to multicast groups passed in variable argument list. Returns the netlink socket on which Receive() method can be called to retrieve the messages from the kernel.

func SubscribeAt added in v1.1.0

func SubscribeAt(newNs, curNs netns.NsHandle, protocol int, groups ...uint) (*NetlinkSocket, error)

SubscribeAt works like Subscribe plus let's the caller choose the network namespace in which the socket would be opened (newNs). Then control goes back to curNs if open, otherwise to the netns at the time this function was called.

func (*NetlinkSocket) Close added in v1.1.0

func (s *NetlinkSocket) Close()

func (*NetlinkSocket) GetFd added in v1.1.0

func (s *NetlinkSocket) GetFd() int

func (*NetlinkSocket) GetPid added in v1.1.0

func (s *NetlinkSocket) GetPid() (uint32, error)

func (*NetlinkSocket) Send added in v1.1.0

func (s *NetlinkSocket) Send(request *NetlinkRequest) error

func (*NetlinkSocket) SetReceiveTimeout added in v1.1.0

func (s *NetlinkSocket) SetReceiveTimeout(timeout *unix.Timeval) error

SetReceiveTimeout allows to set a receive timeout on the socket

func (*NetlinkSocket) SetSendTimeout added in v1.1.0

func (s *NetlinkSocket) SetSendTimeout(timeout *unix.Timeval) error

SetSendTimeout allows to set a send timeout on the socket

type Nfgenmsg added in v1.1.0

type Nfgenmsg struct {
	NfgenFamily uint8
	Version     uint8
	ResId       uint16 // big endian
}

/* General form of address family dependent message.

*/
struct nfgenmsg {
	__u8  nfgen_family;		/* AF_xxx */
	__u8  version;		/* nfnetlink version */
	__be16    res_id;		/* resource id */
};

func DeserializeNfgenmsg added in v1.1.0

func DeserializeNfgenmsg(b []byte) *Nfgenmsg

func (*Nfgenmsg) Len added in v1.1.0

func (msg *Nfgenmsg) Len() int

func (*Nfgenmsg) Serialize added in v1.1.0

func (msg *Nfgenmsg) Serialize() []byte

type RtAttr added in v1.1.0

type RtAttr struct {
	Data []byte
	// contains filtered or unexported fields
}

Extend RtAttr to handle data and children

func NewRtAttr added in v1.1.0

func NewRtAttr(attrType int, data []byte) *RtAttr

Create a new Extended RtAttr object

func NewRtAttrChild added in v1.1.0

func NewRtAttrChild(parent *RtAttr, attrType int, data []byte) *RtAttr

Create a new RtAttr obj anc add it as a child of an existing object

func (*RtAttr) AddChild added in v1.1.0

func (a *RtAttr) AddChild(attr *RtAttr)

AddChild adds an existing RtAttr as a child.

func (*RtAttr) Len added in v1.1.0

func (a *RtAttr) Len() int

func (*RtAttr) Serialize added in v1.1.0

func (a *RtAttr) Serialize() []byte

Serialize the RtAttr into a byte array This can't just unsafe.cast because it must iterate through children.

type SocketHandle added in v1.1.0

type SocketHandle struct {
	Seq    uint32
	Socket *NetlinkSocket
}

SocketHandle contains the netlink socket and the associated sequence counter for a specific netlink family

func (*SocketHandle) Close added in v1.1.0

func (sh *SocketHandle) Close()

Close closes the netlink socket

Jump to

Keyboard shortcuts

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