Categorygithub.com/st0nx/netlink
modulepackage
1.0.0
Repository: https://github.com/st0nx/netlink.git
Documentation: pkg.go.dev

# README

netlink - netlink library for go

Build Status GoDoc

The netlink package provides a simple netlink library for go. Netlink is the interface a user-space program in linux uses to communicate with the kernel. It can be used to add and remove interfaces, set ip addresses and routes, and configure ipsec. Netlink communication requires elevated privileges, so in most cases this code needs to be run as root. Since low-level netlink messages are inscrutable at best, the library attempts to provide an api that is loosely modeled on the CLI provided by iproute2. Actions like ip link add will be accomplished via a similarly named function like AddLink(). This library began its life as a fork of the netlink functionality in docker/libcontainer but was heavily rewritten to improve testability, performance, and to add new functionality like ipsec xfrm handling.

Local Build and Test

You can use go get command:

go get github.com/vishvananda/netlink

Testing dependencies:

go get github.com/vishvananda/netns

Testing (requires root):

sudo -E go test github.com/vishvananda/netlink

Examples

Add a new bridge and add eth1 into it:

package main

import (
    "fmt"
    "github.com/vishvananda/netlink"
)

func main() {
    la := netlink.NewLinkAttrs()
    la.Name = "foo"
    mybridge := &netlink.Bridge{LinkAttrs: la}
    err := netlink.LinkAdd(mybridge)
    if err != nil  {
        fmt.Printf("could not add %s: %v\n", la.Name, err)
    }
    eth1, _ := netlink.LinkByName("eth1")
    netlink.LinkSetMaster(eth1, mybridge)
}

Note NewLinkAttrs constructor, it sets default values in structure. For now it sets only TxQLen to -1, so kernel will set default by itself. If you're using simple initialization(LinkAttrs{Name: "foo"}) TxQLen will be set to 0 unless you specify it like LinkAttrs{Name: "foo", TxQLen: 1000}.

Add a new ip address to loopback:

package main

import (
    "github.com/vishvananda/netlink"
)

func main() {
    lo, _ := netlink.LinkByName("lo")
    addr, _ := netlink.ParseAddr("169.254.169.254/32")
    netlink.AddrAdd(lo, addr)
}

Future Work

Many pieces of netlink are not yet fully supported in the high-level interface. Aspects of virtually all of the high-level objects don't exist. Many of the underlying primitives are there, so its a matter of putting the right fields into the high-level objects and making sure that they are serialized and deserialized correctly in the Add and List methods.

There are also a few pieces of low level netlink functionality that still need to be implemented. Routing rules are not in place and some of the more advanced link types. Hopefully there is decent structure and testing in place to make these fairly straightforward to add.

# Packages

Package nl has low level primitives for making Netlink calls.

# Functions

AddrAdd will add an IP address to a link device.
AddrDel will delete an IP address from a link device.
AddrList gets a list of IP addresses in the system.
AddrReplace will replace (or, if not present, add) an IP address on a link device.
AddrSubscribe takes a chan down which notifications will be sent when addresses change.
AddrSubscribeAt works like AddrSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).
AddrSubscribeWithOptions work like AddrSubscribe but enable to provide additional options to modify the behavior.
No description provided by the author
No description provided by the author
No description provided by the author
BridgeVlanAdd adds a new vlan filter entry Equivalent to: `bridge vlan add dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`.
BridgeVlanDel adds a new vlan filter entry Equivalent to: `bridge vlan del dev DEV vid VID [ pvid ] [ untagged ] [ self ] [ master ]`.
BridgeVlanList gets a map of device id to bridge vlan infos.
No description provided by the author
ClassAdd will add a class to the system.
ClassChange will change a class in place Equivalent to: `tc class change $class` The parent and handle MUST NOT be changed.
ClassDel will delete a class from the system.
ClassList gets a list of classes in the system.
ClassReplace will replace a class to the system.
No description provided by the author
ConntrackDeleteFilter deletes entries on the specified table on the base of the filter conntrack -D [table] parameters Delete conntrack or expectation.
ConntrackTableFlush flushes all the flows of a specified table conntrack -F [table] Flush table The flush operation applies to all the family types.
ConntrackTableList returns the flow list of a table of a specific family conntrack -L [table] [options] List conntrack or expectation table.
No description provided by the author
No description provided by the author
FilterAdd will add a filter to the system.
FilterDel will delete a filter from the system.
FilterList gets a list of filters in the system.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LinkAdd adds a new link device.
LinkByAlias finds a link by its alias and returns a pointer to the object.
LinkByIndex finds a link by index and returns a pointer to the object.
LinkByName finds a link by name and returns a pointer to the object.
LinkDel deletes link device.
linkDeserialize deserializes a raw message received from netlink into a link object.
No description provided by the author
LinkList gets a list of link devices.
LinkSetAlias sets the alias of the link device.
No description provided by the author
No description provided by the author
LinkSetBondSlave add slave to bond link via ioctl interface.
No description provided by the author
No description provided by the author
LinkSetDown disables link device.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LinkSetHardwareAddr sets the hardware address of the link device.
No description provided by the author
LinkSetMaster sets the master of the link device.
LinkSetMasterByIndex sets the master of the link device.
LinkSetMTU sets the mtu of the link device.
LinkSetName sets the name of the link device.
LinkSetNoMaster removes the master of the link device.
LinkSetNsFd puts the device into a new network namespace.
LinkSetNsPid puts the device into a new network namespace.
No description provided by the author
LinkSetTxQLen sets the transaction queue length for the link.
LinkSetUp enables the link device.
LinkSetVfHardwareAddr sets the hardware address of a vf for the link.
LinkSetVfSpoofchk enables/disables spoof check on a vf for the link.
LinkSetVfTrust enables/disables trust state on a vf for the link.
LinkSetVfTxRate sets the tx rate of a vf for the link.
LinkSetVfVlan sets the vlan of a vf for the link.
LinkSetXdpFd adds a bpf function to the driver.
LinkSetXdpFdWithFlags adds a bpf function to the driver with the given options.
LinkSubscribe takes a chan down which notifications will be sent when links change.
LinkSubscribeAt works like LinkSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).
LinkSubscribeWithOptions work like LinkSubscribe but enable to provide additional options to modify the behavior.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NeighAdd will add an IP to MAC mapping to the ARP table Equivalent to: `ip neigh add ....`.
NeighAppend will append an entry to FDB Equivalent to: `bridge fdb append...`.
NeighDel will delete an IP address from a link device.
No description provided by the author
NeighList gets a list of IP-MAC mappings in the system (ARP table).
NeighProxyList gets a list of neighbor proxies in the system.
NeighSet will add or replace an IP to MAC mapping to the ARP table Equivalent to: `ip neigh replace....`.
No description provided by the author
No description provided by the author
No description provided by the author
NewHandle returns a netlink handle on the current network namespace.
NewHandle returns a netlink handle on the network namespace specified by ns.
NewHandleAtFrom works as NewHandle but allows client to specify the new and the origin netns Handle.
No description provided by the author
NOTE: function is in here because it uses other linux functions.
NewIPNet generates an IPNet from an ip address using a netmask of 32 or 128.
NewLinkAttrs returns LinkAttrs structure filled with default values.
No description provided by the author
No description provided by the author
NOTE function is here because it uses other linux functions.
No description provided by the author
NewRule return empty rules.
ParseAddr parses the string representation of an address in the form $ip/$netmask $label.
ParseIPNet parses a string in ip/net format and returns a net.IPNet.
No description provided by the author
QdiscAdd will add a qdisc to the system.
QdiscChange will change a qdisc in place Equivalent to: `tc qdisc change $qdisc` The parent and handle MUST NOT be changed.
QdiscDel will delete a qdisc from the system.
QdiscList gets a list of qdiscs in the system.
QdiscReplace will replace a qdisc to the system.
RouteAdd will add a route to the system.
RouteDel will delete a route from the system.
RouteGet gets a route to a specific destination from the host system.
RouteList gets a list of routes in the system.
RouteListFiltered gets a list of routes in the system filtered with specified rules.
RouteReplace will add a route to the system.
RouteSubscribe takes a chan down which notifications will be sent when routes are added or deleted.
RouteSubscribeAt works like RouteSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).
RouteSubscribeWithOptions work like RouteSubscribe but enable to provide additional options to modify the behavior.
RuleAdd adds a rule to the system.
RuleDel deletes a rule from the system.
RuleList lists rules in the system.
No description provided by the author
No description provided by the author
No description provided by the author
SocketGet returns the Socket identified by its local and remote addresses.
StringToBondLacpRate returns bond lacp arte, or uknonw is the s is invalid.
StringToBondMode returns bond mode, or uknonw is the s is invalid.
StringToBondXmitHashPolicy returns bond lacp arte, or uknonw is the s is invalid.
No description provided by the author
VethPeerIndex get veth peer index.
No description provided by the author
XfrmPolicyAdd will add an xfrm policy to the system.
XfrmPolicyDel will delete an xfrm policy from the system.
XfrmPolicyFlush will flush the policies on the system.
XfrmPolicyGet gets a the policy described by the index or selector, if found.
XfrmPolicyList gets a list of xfrm policies in the system.
XfrmPolicyUpdate will update an xfrm policy to the system.
XfrmStateAdd will add an xfrm state to the system.
XfrmStateAllocSpi will allocate an xfrm state in the system.
XfrmStateDel will delete an xfrm state from the system.
XfrmStateFlush will flush the xfrm state on the system.
XfrmStateGet gets the xfrm state described by the ID, if found.
XfrmStateList gets a list of xfrm states in the system.
XfrmStateUpdate will update an xfrm state to the system.
No description provided by the author

# Constants

Flag mask for bond options.
Possible BondAdSelect value.
Possible BondAdSelect value.
Flag mask for bond options.
Possible BondAdSelect value.
Flag mask for bond options.
Possible BondArpAllTargets value.
Possible BondArpAllTargets value.
Flag mask for bond options.
Flag mask for bond options.
Possible BondArpValidate value.
Possible BondArpValidate value.
Possible BondArpValidate value.
Flag mask for bond options.
Possible BondArpValidate value.
Flag mask for bond options.
Possible BondFailOverMac value.
Possible BondFailOverMac value.
Flag mask for bond options.
Possible BondFailOverMac value.
Possible BondLacpRate value.
Flag mask for bond options.
Possible BondLacpRate value.
Possible BondLacpRate value.
Flag mask for bond options.
Flag mask for bond options.
Flag mask for bond options.
Possible BondMode.
Possible BondMode.
Possible BondMode.
Possible BondMode.
Possible BondMode.
Possible BondMode.
Possible BondMode.
Flag mask for bond options.
Possible BondMode.
Flag mask for bond options.
Flag mask for bond options.
Flag mask for bond options.
Possible BondPrimaryReselect value.
Possible BondPrimaryReselect value.
Possible BondPrimaryReselect value.
Flag mask for bond options.
Flag mask for bond options.
Flag mask for bond options.
Flag mask for bond options.
Possible BondXmitHashPolicy value.
Possible BondXmitHashPolicy value.
Possible BondXmitHashPolicy value.
Possible BondXmitHashPolicy value.
Possible BondXmitHashPolicy value.
Flag mask for bond options.
Possible BondXmitHashPolicy value.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ConntrackExpectTable Conntrack expect table https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK_EXP 2.
-any-nat ip Source or destination NAT ip.
-dst-nat ip Destination NAT ip.
-src-nat ip Source NAT ip.
-orig-dst ip Destination address from original direction.
-orig-src ip Source address from original direction.
ConntrackTable Conntrack table https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK 1.
ETH_SS_FEATURES are device feature names.
ETH_SS_PRIV_FLAGS are driver private flag names.
ETH_SS_RSS_HASH_FUNCS is RSS hush function names.
ETH_SS_STATS statistic names, for use with %ETHTOOL_GSTATS.
ETH_SS_TEST is self-test result names, for use with %ETHTOOL_TEST.
ETHTOOL_GSSET_INFO gets string set info.
ETHTOOL_GSTATS gets NIC-specific statistics.
ETHTOOL_GSTRINGS gets specified string set.
Family type definitions.
Family type definitions.
Family type definitions.
Family type definitions.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IFA_FLAGS is a u32 attribute.
ideally golang.org/x/sys/unix would define IfReq but it only has IFNAMSIZ, hence this minimalistic implementation.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Neighbor Flags.
Neighbor Flags.
Neighbor Flags.
Neighbor Flags.
Neighbor Flags.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Neighbor Cache Entry States.
Not up but pending an external event.
Down.
Down due to state of lower layer.
Some component is missing.
In some test mode.
Status can't be determined.
Up, ready to send packets.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SIOCETHTOOL is Ethtool interface.
ideally golang.org/x/sys/unix would define IfReq but it only has IFNAMSIZ, hence this minimalistic implementation.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Constants used in TcU32Sel.Flags.
Constants used in TcU32Sel.Flags.
Constants used in TcU32Sel.Flags.
Constants used in TcU32Sel.Flags.
mirror packet to EGRESS */.
packet redirect to EGRESS*/.
mirror packet to INGRESS */.
packet redirect to INGRESS*/.
For Parsing Mark.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NOTE not defined on darwin.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

ErrAttrBodyTruncated is returned when a netlink attribute's body is truncated.
ErrAttrHeaderTruncated is returned when a netlink attribute's header is truncated.
ErrNotImplemented is returned when a requested feature is not implemented.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Addr represents an IP address from netlink.
AddrSubscribeOptions contains a set of options to use with AddrSubscribeWithOptions.
No description provided by the author
Bond representation.
BondAdInfo represents ad info for bond.
No description provided by the author
No description provided by the author
No description provided by the author
Bridge links are simple linux bridges.
ClassAttrs represents a netlink class.
No description provided by the author
No description provided by the author
Device links cannot be created via netlink.
Dummy links are dummy ethernet devices.
FilterAttrs represents a netlink filter.
No description provided by the author
No description provided by the author
Fq is a classless packet scheduler meant to be mostly used for locally generated traffic.
FQ_Codel (Fair Queuing Controlled Delay) is queuing discipline that combines Fair Queuing with the CoDel AQM scheme.
Fw filter filters on firewall marks NOTE: this is in filter_linux because it refers to nl.TcPolice which is defined in nl/tc_linux.go.
No description provided by the author
GenericClass classes represent types that are not currently understood by this netlink library.
GenericFilter filters represent types that are not currently understood by this netlink library.
GenericLink links represent types that are not currently understood by this netlink library.
GenericQdisc qdiscs represent types that are not currently understood by this netlink library.
No description provided by the author
No description provided by the author
No description provided by the author
Gretap devices must specify LocalIP and RemoteIP on create.
No description provided by the author
No description provided by the author
Handle is an handle for the netlink requests on a specific network namespace.
Htb is a classful qdisc that rate limits based on tokens.
HtbClass represents an Htb class.
No description provided by the author
Ifb links are advanced dummy devices for packet filtering.
Ifreq is a struct for ioctl ethernet manipulation syscalls.
IfreqSlave is a struct for ioctl bond manipulation syscalls.
Ingress is a qdisc for adding ingress filters.
No description provided by the author
No description provided by the author
LinkAttrs represents data shared by most link types.
LinkNotFoundError wraps the various not found errors when getting/reading links.
Ref: struct rtnl_link_stats {...} */.
Ref: struct rtnl_link_stats64 {...} */.
LinkSubscribeOptions contains a set of options to use with LinkSubscribeWithOptions.
LinkUpdate is used to pass information back from LinkSubscribe().
No description provided by the author
Macvlan links have ParentIndex set in their Attrs().
Macvtap - macvtap is a virtual interfaces based on macvlan.
MatchAll filters match all packets.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Neigh represents a link layer neighbor from netlink.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PfifoFast is the default qdisc created by the kernel if one has not been defined for the interface.
Prio is a basic qdisc that works just like PfifoFast.
Protinfo represents bridge flags from netlink.
QdiscAttrs represents a netlink qdisc.
Route represents a netlink route.
RouteSubscribeOptions contains a set of options to use with RouteSubscribeWithOptions.
RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE.
Rule represents a netlink rule.
SEG6 definitions.
No description provided by the author
Socket represents a netlink socket.
SocketID identifies a single socket.
Tbf is a classless qdisc that rate limits based on tokens.
TcU32Key contained of Sel in the U32 filters.
Sel of the U32 filters that contains multiple TcU32Key.
Tuntap links created via /dev/tun/tap, but can be destroyed via netlink.
U32 filters on many packet related properties.
Veth devices must specify PeerName on create.
Vlan links have ParentIndex set in their Attrs().
No description provided by the author
No description provided by the author
No description provided by the author
XfrmMark represents the mark associated to the state or policy.
No description provided by the author
XfrmPolicy represents an ipsec policy.
XfrmPolicyTmpl encapsulates a rule for the base addresses of an ipsec policy.
XfrmState represents the state of an ipsec policy.
XfrmStateAlgo represents the algorithm to use for the ipsec encryption.
XfrmStateEncap represents the encapsulation to use for the ipsec encryption.
XfrmStateLimits represents the configured limits for the state.
XfrmStateStats represents the current number of bytes/packets processed by this State, the State's installation and first use time and the replay window counters.

# Interfaces

Action represents an action in any supported filter.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Link represents a link device from netlink.
No description provided by the author
No description provided by the author

# Type aliases

BondAdSelect type.
BondArpAllTargets type.
BondArpValidate type.
BondFailOverMac type.
BondLacpRate type.
BondMode type.
BondPrimaryReselect type.
BondXmitHashPolicy type.
No description provided by the author
Filter types.
ConntrackTableType Conntrack table for the netlink operation.
Dir is an enum representing an ipsec template direction.
EncapType is an enum representing the optional packet encapsulation.
InetFamily Family type.
No description provided by the author
LinkOperState represents the values of the IFLA_OPERSTATE link attribute, which contains the RFC2863 state of the interface.
No description provided by the author
No description provided by the author
No description provided by the author
Mode is an enum representing an ipsec transport.
No description provided by the author
No description provided by the author
No description provided by the author
Proto is an enum representing an ipsec protocol.
Scope is an enum representing a route scope.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author