Documentation
¶
Overview ¶
Package netlink provides a simple library for netlink. 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 up ip addresses and routes, and confiugre ipsec. Netlink communication requires elevated privileges, so in most cases this code needs to be run as root. The low level primitives for netlink are contained in the nl subpackage. This package attempts to provide a high-level interface that is loosly modeled on the iproute2 cli.
Index ¶
- Constants
- Variables
- func AddrAdd(link *Link, addr *Addr) error
- func AddrDel(link *Link, addr *Addr) error
- func HandleStr(handle uint32) string
- func LinkAdd(link *Link) error
- func LinkDel(link *Link) error
- func LinkSetDown(link *Link) error
- func LinkSetMTU(link *Link, mtu int) error
- func LinkSetMaster(link *Link, master *Link) error
- func LinkSetNsFd(link *Link, fd int) error
- func LinkSetNsPid(link *Link, nspid int) error
- func LinkSetUp(link *Link) error
- func MajorMinor(handle uint32) (uint16, uint16)
- func MakeHandle(major, minor uint16) uint32
- func NeighAdd(neigh *Neigh) error
- func NeighAppend(neigh *Neigh) error
- func NeighDel(neigh *Neigh) error
- func NeighDeserialize(m []byte) (*Ndmsg, *Neigh, error)
- func NeighSet(neigh *Neigh) error
- func NewIPNet(ip net.IP) *net.IPNet
- func ParseIPNet(s string) (*net.IPNet, error)
- func RouteAdd(route *Route) error
- func RouteDel(route *Route) error
- func SetFastLeave(link Link, mode bool) error
- func SetFlood(link Link, mode bool) error
- func SetGuard(link Link, mode bool) error
- func SetHairpin(link Link, mode bool) error
- func SetLearning(link Link, mode bool) error
- func SetRootBlock(link Link, mode bool) error
- func XfrmPolicyAdd(policy *XfrmPolicy) error
- func XfrmPolicyDel(policy *XfrmPolicy) error
- func XfrmStateAdd(policy *XfrmState) error
- func XfrmStateDel(policy *XfrmState) error
- type Addr
- type Bridge
- type Device
- type Dir
- type Dummy
- type EncapType
- type Filter
- type FilterAttrs
- type GenericFilter
- type GenericLink
- type GenericQdisc
- type IPVlan
- type IPVlanMode
- type Ifb
- type Ingress
- type Link
- type LinkAttrs
- type Macvlan
- type MacvlanMode
- type Macvtap
- type Mode
- type Neigh
- type NsFd
- type NsPid
- type PfifoFast
- type Prio
- type Protinfo
- type Proto
- type Qdisc
- type QdiscAttrs
- type Route
- type Scope
- type Tbf
- type U32
- type Veth
- type Vlan
- type Vxlan
- type XfrmPolicy
- type XfrmPolicyTmpl
- type XfrmState
- type XfrmStateAlgo
- type XfrmStateEncap
Constants ¶
const ( // Family type definitions FAMILY_ALL = nl.FAMILY_ALL FAMILY_V4 = nl.FAMILY_V4 FAMILY_V6 = nl.FAMILY_V6 )
const ( HANDLE_NONE = 0 HANDLE_INGRESS = 0xFFFFFFF1 HANDLE_ROOT = 0xFFFFFFFF PRIORITY_MAP_LEN = 16 )
Variables ¶
Functions ¶
func NewIPNet ¶
NewIPNet generates an IPNet from an ip address using a netmask of 32.
func ParseIPNet ¶
ParseIPNet parses a string in ip/net format and returns a net.IPNet. This is valuable because addresses in netlink are often IPNets and ParseCIDR returns an IPNet with the IP part set to the base IP of the range.
func XfrmPolicyAdd ¶
func XfrmPolicyAdd(policy *XfrmPolicy) error
func XfrmPolicyDel ¶
func XfrmPolicyDel(policy *XfrmPolicy) error
Types ¶
type Addr ¶
Addr represents an IP address from netlink. Netlink ip addresses include a mask, so it stores the address as a net.IPNet.
func ParseAddr ¶
ParseAddr parses the string representation of an address in the form $ip/$netmask $label. The label portion is optional
func (Addr) Equal ¶
Equal returns true if both Addrs have the same net.IPNet value.
type Device ¶
type Device struct {
LinkAttrs
}
Device links cannot be created via netlink. These links are links created by udev like 'lo' and 'etho0'
type EncapType ¶
type EncapType uint8
EncapType is an enum representing an ipsec template direction.
type Filter ¶
type Filter interface {
Attrs() *FilterAttrs
Type() string
}
type FilterAttrs ¶
type FilterAttrs struct {
LinkIndex int
Handle uint32
Parent uint32
Priority uint16 // lower is higher priority
Protocol uint16 // syscall.ETH_P_*
}
Filter represents a netlink filter. A filter is associated with a link, has a handle and a parent. The root filter of a device should have a parent == HANDLE_ROOT.
func (FilterAttrs) String ¶
func (q FilterAttrs) String() string
type GenericFilter ¶
type GenericFilter struct {
FilterAttrs
FilterType string
}
GenericFilter filters represent types that are not currently understood by this netlink library.
func (*GenericFilter) Attrs ¶
func (filter *GenericFilter) Attrs() *FilterAttrs
func (*GenericFilter) Type ¶
func (filter *GenericFilter) Type() string
type GenericLink ¶
GenericLink links represent types that are not currently understood by this netlink library.
func (*GenericLink) Attrs ¶
func (generic *GenericLink) Attrs() *LinkAttrs
func (*GenericLink) Type ¶
func (generic *GenericLink) Type() string
type GenericQdisc ¶
type GenericQdisc struct {
QdiscAttrs
QdiscType string
}
GenericQdisc qdiscs represent types that are not currently understood by this netlink library.
func (*GenericQdisc) Attrs ¶
func (qdisc *GenericQdisc) Attrs() *QdiscAttrs
func (*GenericQdisc) Type ¶
func (qdisc *GenericQdisc) Type() string
type IPVlan ¶
type IPVlan struct {
LinkAttrs
Mode IPVlanMode
}
type IPVlanMode ¶
type IPVlanMode uint16
const ( IPVLAN_MODE_L2 IPVlanMode = iota IPVLAN_MODE_L3 IPVLAN_MODE_MAX )
type Ingress ¶
type Ingress struct {
QdiscAttrs
}
Ingress is a qdisc for adding ingress filters
func (*Ingress) Attrs ¶
func (qdisc *Ingress) Attrs() *QdiscAttrs
type Link ¶
Link represents a link device from netlink. Shared link attributes like name may be retrieved using the Attrs() method. Unique data can be retrieved by casting the object to the proper type.
type LinkAttrs ¶
type LinkAttrs struct {
Index int
MTU int
TxQLen int // Transmit Queue Length
Name string
HardwareAddr net.HardwareAddr
Flags net.Flags
ParentIndex int // index of the parent link device
MasterIndex int // must be the index of a bridge
Namespace interface{} // nil | NsPid | NsFd
}
LinkAttrs represents data shared by most link types
type Macvlan ¶
type Macvlan struct {
LinkAttrs
Mode MacvlanMode
}
Macvlan links have ParentIndex set in their Attrs()
type MacvlanMode ¶
type MacvlanMode uint16
const ( MACVLAN_MODE_DEFAULT MacvlanMode = iota MACVLAN_MODE_PRIVATE MACVLAN_MODE_VEPA MACVLAN_MODE_BRIDGE MACVLAN_MODE_PASSTHRU MACVLAN_MODE_SOURCE )
type Macvtap ¶
type Macvtap struct {
Macvlan
}
Macvtap - macvtap is a virtual interfaces based on macvlan
type Neigh ¶
type Neigh struct {
LinkIndex int
Family int
State int
Type int
Flags int
IP net.IP
HardwareAddr net.HardwareAddr
}
Neigh represents a link layer neighbor from netlink.
type PfifoFast ¶
type PfifoFast struct {
QdiscAttrs
Bands uint8
PriorityMap [PRIORITY_MAP_LEN]uint8
}
PfifoFast is the default qdisc created by the kernel if one has not been defined for the interface
func (*PfifoFast) Attrs ¶
func (qdisc *PfifoFast) Attrs() *QdiscAttrs
type Prio ¶
type Prio struct {
QdiscAttrs
Bands uint8
PriorityMap [PRIORITY_MAP_LEN]uint8
}
Prio is a basic qdisc that works just like PfifoFast
func NewPrio ¶
func NewPrio(attrs QdiscAttrs) *Prio
func (*Prio) Attrs ¶
func (qdisc *Prio) Attrs() *QdiscAttrs
type Protinfo ¶
type Protinfo struct {
Hairpin bool
Guard bool
FastLeave bool
RootBlock bool
Learning bool
Flood bool
}
Protinfo represents bridge flags from netlink.
type Proto ¶
type Proto uint8
Proto is an enum representing an ipsec protocol.
const ( XFRM_PROTO_ROUTE2 Proto = syscall.IPPROTO_ROUTING XFRM_PROTO_ESP Proto = syscall.IPPROTO_ESP XFRM_PROTO_AH Proto = syscall.IPPROTO_AH XFRM_PROTO_HAO Proto = syscall.IPPROTO_DSTOPTS XFRM_PROTO_COMP Proto = syscall.IPPROTO_COMP XFRM_PROTO_IPSEC_ANY Proto = syscall.IPPROTO_RAW )
type Qdisc ¶
type Qdisc interface {
Attrs() *QdiscAttrs
Type() string
}
type QdiscAttrs ¶
Qdisc represents a netlink qdisc. A qdisc is associated with a link, has a handle, a parent and a refcnt. The root qdisc of a device should have parent == HANDLE_ROOT.
func (QdiscAttrs) String ¶
func (q QdiscAttrs) String() string
type Route ¶
Route represents a netlink route. A route is associated with a link, has a destination network, an optional source ip, and optional gateway. Advanced route parameters and non-main routing tables are currently not supported.
type Scope ¶
type Scope uint8
Scope is an enum representing a route scope.
const ( SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE SCOPE_SITE Scope = syscall.RT_SCOPE_SITE SCOPE_LINK Scope = syscall.RT_SCOPE_LINK SCOPE_HOST Scope = syscall.RT_SCOPE_HOST SCOPE_NOWHERE Scope = syscall.RT_SCOPE_NOWHERE )
type Tbf ¶
type Tbf struct {
QdiscAttrs
// TODO: handle 64bit rate properly
Rate uint64
Limit uint32
Buffer uint32
}
Tbf is a classful qdisc that rate limits based on tokens
func (*Tbf) Attrs ¶
func (qdisc *Tbf) Attrs() *QdiscAttrs
type U32 ¶
type U32 struct {
FilterAttrs
// Currently only supports redirecting to another interface
RedirIndex int
}
U32 filters on many packet related properties
func (*U32) Attrs ¶
func (filter *U32) Attrs() *FilterAttrs
type Veth ¶
Veth devices must specify PeerName on create
type Vlan ¶
Vlan links have ParentIndex set in their Attrs()
type Vxlan ¶
type XfrmPolicy ¶
type XfrmPolicy struct {
Dst *net.IPNet
Src *net.IPNet
Dir Dir
Priority int
Index int
Tmpls []XfrmPolicyTmpl
}
XfrmPolicy represents an ipsec policy. It represents the overlay network and has a list of XfrmPolicyTmpls representing the base addresses of the policy.
func XfrmPolicyList ¶
func XfrmPolicyList(family int) ([]XfrmPolicy, error)
type XfrmPolicyTmpl ¶
XfrmPolicyTmpl encapsulates a rule for the base addresses of an ipsec policy. These rules are matched with XfrmState to determine encryption and authentication algorithms.
type XfrmState ¶
type XfrmState struct {
Dst net.IP
Src net.IP
Proto Proto
Mode Mode
Spi int
Reqid int
ReplayWindow int
Auth *XfrmStateAlgo
Crypt *XfrmStateAlgo
Encap *XfrmStateEncap
}
XfrmState represents the state of an ipsec policy. It optionally contains an XfrmStateAlgo for encryption and one for authentication.
type XfrmStateAlgo ¶
XfrmStateAlgo represents the algorithm to use for the ipsec encryption.
Source Files
¶
- addr.go
- filter.go
- link.go
- neigh.go
- netlink.go
- netlink_unspecified.go
- protinfo.go
- qdisc.go
- route.go
- xfrm.go
- xfrm_policy.go
- xfrm_state.go
