Linx.Netlink.Rtnl.Neighbour (Linx v0.1.0)

Copy Markdown View Source

rtnetlink neighbours — the kernel's ARP (IPv4) and NDP (IPv6) tables.

A neighbour entry maps an IP address to a link-layer (MAC) address on a given interface. list/1 and list/2 read entries; add/4 and delete/3 install and remove them.

%Neighbour{}.dst is a Linx.IP; :lladdr is a Linx.MAC. Verbs accept either strings or the corresponding structs.

Example

{:ok, sock} = Rtnl.open()

# A static ARP entry: 10.0.0.1 lives at this MAC on eth0.
:ok = Neighbour.add(sock, "eth0", "10.0.0.1", "52:54:00:ab:cd:ef")

{:ok, neighs} = Neighbour.list(sock, "eth0")
# => [#Linx.Netlink.Rtnl.Neighbour<10.0.0.1 -> 52:54:00:ab:cd:ef ifindex=2>]

:ok = Neighbour.delete(sock, "eth0", "10.0.0.1")

The wire format — struct ndmsg and the NDA_* attributes (include/uapi/linux/neighbour.h) — is declared with the Linx.Netlink.Codec DSL.

Summary

Functions

Adds a permanent neighbour entry — ip resolves to mac on link_name.

Decodes a netlink message body into a t/0.

Removes the neighbour entry for ip on link link_name.

Encodes a t/0 into its netlink message body.

Lists every neighbour entry in the socket's network namespace.

Lists the neighbour entries on link link_name.

Types

t()

@type t() :: %Linx.Netlink.Rtnl.Neighbour{
  dst: term(),
  family: term(),
  flags: term(),
  ifindex: term(),
  lladdr: term(),
  state: term(),
  type: term()
}

Functions

add(socket, link_name, ip, mac)

@spec add(
  Linx.Netlink.Socket.t(),
  binary(),
  binary() | Linx.IP.t(),
  binary() | Linx.MAC.t()
) ::
  :ok | {:error, term()}

Adds a permanent neighbour entry — ip resolves to mac on link_name.

ip may be a string or Linx.IP; mac may be a string or Linx.MAC.

decode(body)

@spec decode(binary()) :: t()

Decodes a netlink message body into a t/0.

delete(socket, link_name, ip)

@spec delete(Linx.Netlink.Socket.t(), binary(), binary() | Linx.IP.t()) ::
  :ok | {:error, term()}

Removes the neighbour entry for ip on link link_name.

encode(message)

@spec encode(t()) :: binary()

Encodes a t/0 into its netlink message body.

list(socket)

@spec list(Linx.Netlink.Socket.t()) :: {:ok, [t()]} | {:error, term()}

Lists every neighbour entry in the socket's network namespace.

list(socket, link_name)

@spec list(Linx.Netlink.Socket.t(), binary()) :: {:ok, [t()]} | {:error, term()}

Lists the neighbour entries on link link_name.