rtnetlink

package module
v0.0.0-...-cc5f13f Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2017 License: MIT Imports: 7 Imported by: 0

README

Package rtnetlink provides low-level access to Linux rtnetlink. MIT Licensed.

This package is heavily inspired by https://github.com/mdlayher/netlink/tree/master/genetlink.

Documentation

Overview

Package rtnetlink allows the kernel's routing tables to be read and altered. Network routes, IP addresses, Link parameters, Neighbor setups, Queueing disciplines, Traffic classes and Packet classifiers may all be controlled. It is based on netlink messages.

Index

Constants

View Source
const (
	AFInet  = 2
	AFInet6 = 10
)

Address family constants

View Source
const (
	RTM_NEWADDR = syscall.RTM_NEWADDR
	RTM_DELADDR = syscall.RTM_DELADDR
	RTM_GETADDR = syscall.RTM_GETADDR
)

Constants used to request information from rtnetlink addresses.

View Source
const (
	// Protocol is the netlink protocol constant used to specify rtnetlink.
	Protocol = 0x0

	RTNLGRP_LINK = syscall.RTNLGRP_LINK
)
View Source
const (
	RTM_GETLINK = syscall.RTM_GETLINK
	RTM_NEWLINK = syscall.RTM_NEWLINK
	RTM_DELLINK = syscall.RTM_DELLINK
	RTM_SETLINK = syscall.RTM_SETLINK
)

Constants used to request information from rtnetlink links.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddressAttributes

type AddressAttributes struct {
	Address   net.IP // Interface Ip address
	Local     net.IP // Local Ip address
	Label     string
	Broadcast net.IP    // Broadcast Ip address
	Anycast   net.IP    // Anycast Ip address
	CacheInfo CacheInfo // Address information
	Multicast net.IP    // Multicast Ip address
	Flags     uint32    // Address flags
}

AddressAttributes contains all attributes for an interface.

func (*AddressAttributes) MarshalBinary

func (a *AddressAttributes) MarshalBinary() ([]byte, error)

MarshalBinary marshals a AddressAttributes into a byte slice.

func (*AddressAttributes) UnmarshalBinary

func (a *AddressAttributes) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a AddressMessage.

type AddressMessage

type AddressMessage struct {
	// Address family (current AFInet or AFInet6)
	Family uint8

	// Prefix length
	PrefixLength uint8

	// Contains address flags
	Flags uint8

	// Address Scope
	Scope uint8

	// Interface index
	Index uint32

	// Attributes List
	Attributes AddressAttributes
}

A AddressMessage is a route netlink address message.

func (*AddressMessage) MarshalBinary

func (m *AddressMessage) MarshalBinary() ([]byte, error)

MarshalBinary marshals a AddressMessage into a byte slice.

func (*AddressMessage) UnmarshalBinary

func (m *AddressMessage) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a AddressMessage.

type AddressService

type AddressService struct {
	// contains filtered or unexported fields
}

AddressService is used to retrieve rtnetlink family information.

func (*AddressService) Delete

func (a *AddressService) Delete(address net.IP, index uint32) error

Delete removes an address by ip and interface index.

func (*AddressService) List

func (a *AddressService) List() ([]AddressMessage, error)

List retrieves all addresses.

func (*AddressService) New

func (a *AddressService) New(req *AddressMessage) error

New creates a new address using the AddressMessage information.

type CacheInfo

type CacheInfo struct {
	Prefered uint32
	Valid    uint32
	Created  uint32
	Updated  uint32
}

CacheInfo contains address information

func (*CacheInfo) UnmarshalBinary

func (c *CacheInfo) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.

type Conn

type Conn struct {
	Link    *LinkService
	Address *AddressService
	// contains filtered or unexported fields
}

A Conn is a route netlink connection. A Conn can be used to send and receive route netlink messages to and from netlink.

func Dial

func Dial(config *netlink.Config) (*Conn, error)

Dial dials a route netlink connection. Config specifies optional configuration for the underlying netlink connection. If config is nil, a default configuration will be used.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) Execute

func (c *Conn) Execute(m Message, family uint16, flags netlink.HeaderFlags) ([]Message, error)

Execute sends a single Message to netlink using Conn.Send, receives one or more replies using Conn.Receive, and then checks the validity of the replies against the request using netlink.Validate.

See the documentation of Conn.Send, Conn.Receive, and netlink.Validate for details about each function.

func (*Conn) Receive

func (c *Conn) Receive() ([]Message, []netlink.Message, error)

Receive receives one or more Messages from netlink. The netlink.Messages used to wrap each Message are available for later validation.

func (*Conn) Send

func (c *Conn) Send(m Message, family uint16, flags netlink.HeaderFlags) (netlink.Message, error)

Send sends a single Message to netlink, wrapping it in a netlink.Message using the specified generic netlink family and flags. On success, Send returns a copy of the netlink.Message with all parameters populated, for later validation.

type LinkAttributes

type LinkAttributes struct {
	Address   net.HardwareAddr // Interface L2 address
	Broadcast net.HardwareAddr // L2 broadcast address
	Name      string           // Device name
	MTU       uint32           // MTU of the device
	Type      uint32           // Link type
	QueueDisc string           // Queueing discipline
	Stats     *LinkStats       // Interface Statistics
}

LinkAttributes contains all attributes for an interface.

func (*LinkAttributes) MarshalBinary

func (a *LinkAttributes) MarshalBinary() ([]byte, error)

MarshalBinary marshals a LinkAttributes into a byte slice.

func (*LinkAttributes) UnmarshalBinary

func (a *LinkAttributes) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.

type LinkMessage

type LinkMessage struct {
	// Always set to AF_UNSPEC (0)
	Family uint16

	// Device Type
	Type uint16

	// Unique interface index, using a nonzero value with
	// NewLink will instruct the kernel to create a
	// device with the given index (kernel 3.7+ required)
	Index uint32

	// Contains device flags, see netdevice(7)
	Flags uint32

	// Change Flags, reserved for future use and should
	// always be 0xffffffff
	Change uint32

	// Attributes List
	Attributes LinkAttributes
}

A LinkMessage is a route netlink link message.

func (*LinkMessage) MarshalBinary

func (m *LinkMessage) MarshalBinary() ([]byte, error)

MarshalBinary marshals a LinkMessage into a byte slice.

func (*LinkMessage) UnmarshalBinary

func (m *LinkMessage) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.

type LinkService

type LinkService struct {
	// contains filtered or unexported fields
}

LinkService is used to retrieve rtnetlink family information.

func (*LinkService) Delete

func (l *LinkService) Delete(index uint32) error

Delete removes an interface by index.

func (*LinkService) Get

func (l *LinkService) Get(index uint32) (LinkMessage, error)

Get retrieves interface information by index.

func (*LinkService) List

func (l *LinkService) List() ([]LinkMessage, error)

List retrieves all interfaces.

func (*LinkService) New

func (l *LinkService) New(req *LinkMessage) error

New creates a new interface using the LinkMessage information.

func (*LinkService) Set

func (l *LinkService) Set(req *LinkMessage) error

Set sets interface attributes according to the LinkMessage information.

type LinkStats

type LinkStats struct {
	RXPackets  uint32 // total packets received
	TXPackets  uint32 // total packets transmitted
	RXBytes    uint32 // total bytes received
	TXBytes    uint32 // total bytes transmitted
	RXErrors   uint32 // bad packets received
	TXErrors   uint32 // packet transmit problems
	RXDropped  uint32 // no space in linux buffers
	TXDropped  uint32 // no space available in linux
	Multicast  uint32 // multicast packets received
	Collisions uint32

	// detailed rx_errors:
	RXLengthErrors uint32
	RXOverErrors   uint32 // receiver ring buff overflow
	RXCRCErrors    uint32 // recved pkt with crc error
	RXFrameErrors  uint32 // recv'd frame alignment error
	RXFIFOErrors   uint32 // recv'r fifo overrun
	RXMissedErrors uint32 // receiver missed packet

	// detailed tx_errors
	TXAbortedErrors   uint32
	TXCarrierErrors   uint32
	TXFIFOErrors      uint32
	TXHeartbeatErrors uint32
	TXWindowErrors    uint32

	// for cslip etc
	RXCompressed uint32
	TXCompressed uint32

	RXNoHandler uint32 // dropped, no handler found
}

LinkStats contains packet statistics

func (*LinkStats) UnmarshalBinary

func (a *LinkStats) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the contents of a byte slice into a LinkMessage.

type Message

type Message interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	// contains filtered or unexported methods
}

Message is the interface used for passing around different kinds of rtnetlink messages

Jump to

Keyboard shortcuts

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