Documentation ¶
Overview ¶
Package nl has low level primitives for making Netlink calls.
Index ¶
- Constants
- Variables
- func BytesToString(b []byte) string
- func GetIPFamily(ip net.IP) int
- func NativeEndian() binary.ByteOrder
- func NonZeroTerminated(s string) []byte
- func ParseZeroTerminated(data []byte) string
- func Swap16(i uint16) uint16
- func Swap32(i uint32) uint32
- func Uint16Attr(v uint16) []byte
- func Uint16AttrNetEndian(v uint16) []byte
- func Uint32Attr(v uint32) []byte
- func Uint32AttrNetEndian(v uint32) []byte
- func Uint64Attr(v uint64) []byte
- func Uint64AttrNetEndian(v uint64) []byte
- func Uint8Attr(v uint8) []byte
- func ZeroTerminated(s string) []byte
- type IfInfomsg
- type NetlinkRequest
- type NetlinkRequestData
- type NetlinkSocket
- func (s *NetlinkSocket) Close()
- func (s *NetlinkSocket) GetFd() int
- func (s *NetlinkSocket) GetPid() (uint32, error)
- func (s *NetlinkSocket) Send(request *NetlinkRequest) error
- func (s *NetlinkSocket) SetReceiveTimeout(timeout *unix.Timeval) error
- func (s *NetlinkSocket) SetSendTimeout(timeout *unix.Timeval) error
- type Nfgenmsg
- type RtAttr
- type SocketHandle
Constants ¶
const ( SizeofNfgenmsg = 4 SizeofNfattr = 4 SizeofNfConntrack = 376 SizeofNfctTupleHead = 52 )
Track the message sizes for the correct serialization/deserialization
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 };
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 };
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)
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)
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)
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)
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)
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 )
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
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
const ( SOCK_DIAG_BY_FAMILY = 20 /* linux.sock_diag.h */ TCPDIAG_NOCOOKIE = 0xFFFFFFFF /* TCPDIAG_NOCOOKIE in net/ipv4/tcp_diag.h*/ )
socket diags related
const ( RTA_NEWDST = 0x13 RTA_ENCAP_TYPE = 0x15 RTA_ENCAP = 0x16 )
const ( MPLS_IPTUNNEL_UNSPEC = iota MPLS_IPTUNNEL_DST )
RTA_ENCAP subtype
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
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
const (
AF_MPLS = 28
)
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)
const (
NFNETLINK_V0 = 0
)
#define NFNETLINK_V0 0
const (
NLA_F_NESTED = (1 << 15)
)
#define NLA_F_NESTED (1 << 15)
Variables ¶
var L4ProtoMap = map[uint8]string{
6: "tcp",
17: "udp",
}
var SupportedNlFamilies = []int{}
SupportedNlFamilies contains the list of netlink families this netlink package supports
Functions ¶
func BytesToString ¶ added in v1.1.0
func GetIPFamily ¶ added in v1.1.0
GetIPFamily returns the family type of a net.IP.
func NonZeroTerminated ¶ added in v1.1.0
func ParseZeroTerminated ¶ added in v1.1.0
func Uint16Attr ¶ added in v1.1.0
func Uint16AttrNetEndian ¶ added in v1.1.0
func Uint32Attr ¶ added in v1.1.0
func Uint32AttrNetEndian ¶ added in v1.1.0
func Uint64Attr ¶ added in v1.1.0
func Uint64AttrNetEndian ¶ added in v1.1.0
func ZeroTerminated ¶ added in v1.1.0
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 NewIfInfomsg ¶ added in v1.1.0
Create an IfInfomsg with family specified
func NewIfInfomsgChild ¶ added in v1.1.0
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 NetlinkSocket ¶ added in v1.1.0
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
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
/* 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
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 NewRtAttrChild ¶ added in v1.1.0
Create a new RtAttr obj anc add it as a child of an existing object
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