Next: Introduction, Up: (dir) [Contents][Index]
This document describes guile-netlink version unknown, a guile implementation of the netlink protocol.
Next: API Reference, Previous: guile-netlink, Up: guile-netlink [Contents][Index]
Netlink is an inter-process communication protocol that can be used for communication between processes, or with the kernel. It is implemented by Linux.
Many protocols exist on top of Netlink. The most famous are used to configure network-related functions in the kernel, such as firewall, route table or IP addresses of interfaces.
This library implements the low-level bits of the code by providing data structures that are close to their C counterpart, and basic procedures to initiate communication.
Next: IP Library, Previous: Introduction, Up: guile-netlink [Contents][Index]
Next: Netlink API, Up: API Reference [Contents][Index]
Guile-netlink implements a common API for expressing other protocols. This section describes how to use this API to augment guile-netlink with additional protocols.
Next: Constants, Up: Common API [Contents][Index]
Guile-netlink defines data types that are used in the various Netlink protocols.
We need to be able to serialize and deserialize data that guile-netlink
understands, but we also want to let users of guile-netlink extend this process
easily. This need has lead to the creating of the following data structure,
defined in (netlink data
).
data
The data that is held by this record.
size-proc
A procedure that takes a data (of the same type as the data recorded in the
data
field) and returns the size of its serialization.
serialization-proc
A procedure that takes a data (of the same type as the data recorded in the
data
field), the position at which to start serializing, and a
bytevector in which to serialize. This procedure should modify the bytevector
and its return value is ignored.
The module also defines the following function, that takes a nl-data
structure and provides its serialization in a bytevector:
Takes a nl-data
structure as data, a position pos in
the bytevector bv, and returns an unspecified value.
This function updates the bytevector and adds the serialization of data into bv at pos.
By providing a nl-data
structure, we defer the knowledge of how to
serialize the data to the structure itself, instead of the serialize
function. This allows for more flexibility and extensibility, as the user
of the procedure can pass any kind of data, even if it is not yet supported by
guile-netlink.
Similarly, we need to be able to deserialize netlink answers into a data
structure. To do so, we also defer the knowledge of the datastructure to
deserialize to, to a decoder structure that is passed to the deserialization
procedure. (netlink data)
also defines the following procedures to
deserialize data:
Takes a bytevector bv and starts deserializing the data starting at position pos. To do so, it uses the type variable as the lookup key in the decoder. type is a symbol that represents the type of data to deserialize to.
The decoder is a structure that associates each known type to its deserializer (a function that takes a decoder, a bytevector and a position and returns some data) and an alist that associates a type (an integer, as returned by the protocol in use) to the proper decoder of that type.
Takes a decoder and a type, and returns the deserialization procedure associated with the type (a symbol) in decoder.
Takes a decoder, a type (a symbol that represents the type of data being deserialized) and another type (an integer as returned by the protocol), and returns the deserialization procedure needed to continue decoding the data associated with the currently being deserialized data.
For example, when decoding an answer in the netlink protocol, we first deserialize
the header into a message
structure. That header contains a type field
that contains an integer constant representing the type of data of the body.
Similarly, when deserializing a routing attribute in the rtnetlink protocol,
we first find a header of the attribute that defines an integer constant
corresponding to the type of attribute in the body.
By knowing the context in which the type is declared, this procedure can return
the correct deserializing procedure. For instance, when deserializing a
message
, type 16
means RTM_NEWLINK
in the rtnetlink
protocol, whereas it means IFLA_OPERSTATE
when deserializing a
route-attribute
.
guile-netlink provides the following default decoder for the rtnetlink
protocol in (netlink deserialize)
:
Contains the default decoder for the NETLINK_ROUTE protocol.
For convenience, guile-netlink defines the following structures that can be used to create a custom decoder.
Contains the default association list for the common message types of netlink, associating each of them to a deserialization procedure.
Creates the default association list for a route protocol, given the specified
address deserializer. This is useful because the IFA_ADDRESS
,
IFA_BROADCAST
, etc, contain a different type of address depending on
the message type or its header. This is defined an (netlink route attrs)
and used by the following variables:
Contains the default association list for the known types of routing attributes
for link messages. This list is defined in (netlink route attrs)
.
Next: Netlink Connections, Previous: Data Types, Up: Common API [Contents][Index]
Guile-netlink defines constants used by the Netlink protocols in the
(netlink constant)
module. The constants are the ones present in the
kernel and are too numerous to list here. Please see the source for the
complete list.
The module also defines the following macro:
This macros defines an enumeration. integer->symbol is the name of a procedure that is publicly defined, that takes an integer and returns the associated symbol in the enumeration.
The macro also publicly defines variables whose names are in name-spec to an integer.
A name-spec is either a single name, and the associated value is 0 for the first name-spec, or one more than the previous name-spec. It can also be a pair of a name and an integer, in which case the associated value is that integer. For instance:
(define-enum get-foo FOO0 FOO1 (FOO10 10) FOO11 FOO12) (get-foo 9) -> #<unspecified> (get-foo 0) -> FOO0 FOO11 -> 11
Previous: Constants, Up: Common API [Contents][Index]
The (netlink connection)
module defines the following procedures, used
to connect and communicate with another process or the kernel using a netlink
socket.
Return a bytevector that represents a netlink address. family
should be AF_NETLINK
, pid is the PID of the process with which
to communicate or 0 for the kernel. groups is an integer representing
the set of broadcast groups to which the connection subscribes.
Creates a netlink socket for proto and binds it to addr.
proto is the integer representing the protocol. For instance, rtnetlink
can be selected by usin NETLINK_ROUTE
(defined in
(netlink constant)
).
addr is a bytevector, as returned by get-addr
.
flags is a set of additional flags to pass as the second argument
to the socket
system call—e.g., SOCK_NONBLOCK
.
This procedure is a wrapper for connect
that creates a socket for the
rtnetlink protocol, binds it to the kernel and returns it. By passing the
optional groups keyword, you can select broadcast groups to subscribe to.
flags is a set of additional flags to pass as the second argument
to the socket
system call—e.g., SOCK_NONBLOCK
.
Make sock a member of group, an RTNLGRP_
constant,
meaning that it will be subscribed to events of that group.
For example, here is how you could create a netlink socket and subscribe it to the “link” group so that it receives notifications for new and removed links:
(let ((sock (connect-route))) (add-socket-membership sock RTNLGRP_LINK) …)
This procedure is implemented as a setsockopt
call.
Send msg (it must be of type message, See Netlink Headers) to addr using sock. If not passed, addr is the address of the kernel.
Receives a message from sock from addr. This procedure is blocking.
If not passed, addr defaults to the address of the kernel. This
procedure returns the message as a bytevector, that you can deserialize with
deserialize
(See Data Types)
Receives one or more messages from sock from addr. this procedure is blocking. If not passed, addr defaults to the address of the kernel. This procedure returns a list of messages that were decoded using decoder.
When the answer has the NLM_F_MULTI
flag, this procedure decodes the next
message, until it receives a NLMSG_DONE
message. It returns the list
of every netlink messages it received, including the NLMSG_DONE
.
Next: Rtnetlink API, Previous: Common API, Up: API Reference [Contents][Index]
This section introduces the data structures used for all the netlink protocols. First, we introduce the structure of a netlink message, then we present the standard types of netlink messages, that can be used with every protocol.
Next: Standard Message Types, Up: Netlink API [Contents][Index]
The (netlink message)
module defines the message structure that contains
a netlink message. It is composed of a header and a body, and is the data
structure to pass to send-msg
(See Netlink Connections).
This module defines the following data structure:
type
The type of data in the body of the message. For instance, RTM_GETLINK
.
flags
The set of flags that are set in the header. For instance,
(logior NLM_F_REQUEST NLM_F_DUMP)
.
seq
The sequence number of the message. If this message is an answer to a request, it must keep the same sequence number. Otherwise, you must generate a new and unique sequence number, to track the answers.
pid
The PID of the receiving process, or 0 for the kernel.
data
The actual body, as an nl-data
structure.
Previous: Netlink Headers, Up: Netlink API [Contents][Index]
The (netlink standard)
module defines the set of standard message types
and their data type.
err
The error code, as a negative number.
hdr
The message on which this error applies.
This variable defines the absence of data. This is useful when a structure
is expecting a body part, but the protocol specifically defines that it should
not take any data in some cases. For instance, a NLMSG_NOOP
message
takes no data, so the data
field of the message will contain this
no-data
value.
Previous: Netlink API, Up: API Reference [Contents][Index]
This section describes the support for rtnetlink in guile-netlink. Rtnetlink is the protocol responsible for everything related to network routing. It allows you to manage links, addresses, routing tables, neighbor chaces, routing rules, queueing disciplines, traffic classes, traffic filters and more.
Next: Link Messages, Up: Rtnetlink API [Contents][Index]
The (netlink route attrs)
module defines the following data types:
This defines a header structure for the attribute, as well as its body.
type
This is the type of the attribute, for instance IFLA_ADDRESS
.
data
This is the body of the attribute, ie. its value.
The module also defines additional data types that are not represented as
a record, but by a simple type. For each of the following types, there is
a make-*-route-attr
procedure to produce a nl-data
value
for this type. There is also deserialize-route-attr-data-*
procedure
to deserialize a value of this type.
u8
A one-byte unsigned integer
u16
A two-bytes unsigned integer
u32
A four-bytes unsigned integer
s32
A four-bytes signed integer
string
A string
ethernet
An ethernet address. Its value is a string that represents that address,
for instnace "01:23:45:67:89:ab"
ipv4
An IPv4 address. Its value is a string that represents that address,
for instnace "192.0.2.152"
ipv6
An IPv6 address. Its value is a string that represents that address,
for instnace "2001:db8::0123:4567:89ab:cdef"
bv
A bytevector. This is used by default when the type is not supported.
Next: Address Messages, Previous: Routing Attributes, Up: Rtnetlink API [Contents][Index]
The (netlink route link)
package defines the following data type:
This datatype represents a link message with its routing attributes. This type of message is expected when using the RTM_*LINK message types.
family
The network family, defined as AF_UNSPEC
in the rtnetlink documentation,
although it holds different values in practice.
type
The device type.
index
The index of the device. This is used to select a specific device by its index, or 0 to not filter by device index.
flags
The device flags. See man 7 netdevices
for a list.
attrs
A list of attributes. This field must contain a list of nl-data
structures, not a structure by itself.
Previous: Link Messages, Up: Rtnetlink API [Contents][Index]
The (netlink route addr)
package defines the following data type:
This datatype represents an address message with its routing attributes. This type of message is expected when using the RTM_*ADDR message types.
family
The network family, either AF_INET
for IPv4 addresses, or AF_INET6
for IPv6 addresses.
prefix-len
The prefix length, i.e. the length of the prefix mask, in bits, if defined for the address family.
flags
Address flags. This can be a flag word of IFA_F_SECONDARY
for secondary
address (old alias interface), IFA_F_PERMANENT
for a permanent
address set by the user and other undocumented flags.
scope
The address scope.
index
The index of the device this address is for.
attrs
A list of attributes. This field must contain a list of nl-data
structures, not a structure by itself.
Next: Concept Index, Previous: API Reference, Up: guile-netlink [Contents][Index]
This library comes with higher-level procedures that let you access and modify the state of network on your computer.
Next: Addr, Up: IP Library [Contents][Index]
The (ip link)
module introduces procedures to access and modify the
network links on your machine. They are equivalent to the ip link
family of commands, from iproute2
.
Datatype representing the status of a network link.
get-links print-link
<link> make-link link? link-name link-id link-type link-flags link-mtu link-qdisc link-state link-mode link-group link-qlen link-addr link-brd
name
Name of the link, such as "enp1s0"
.
id
Index of the link, a unique number used to identify the link.
type
Type of the link, as an integer.
flags
Flags associated with the device, as a list of symbols, such as
'(UP LOOPBACK)
.
mtu
MTU of the link, as an integer.
qdisc
Queuing discipline of the link, as a string, such as "noqueue"
.
state
State of the link, as an integer. Use int->operstate
from
(netlink constant)
to get a symbol, such as IF_OPER_UP
.
mode
Mode of the link. 0 means DORMANT
, 1 means DEFAULT
.
group
Identifier of the group it belongs to. 0 for default
.
qlen
Size of the queue.
addr
Ethernet address of the link, as a string.
brd
Broadcast (ethernet) address of the link, as a string.
Returns the list of existing links in the system, as a list of <link>
objects.
Wait until a link called name (a string such as "ens3"
) shows
up.
When blocking? is false, use a non-blocking socket and cooperate via
current-read-waiter
—useful when using Fibers.
Display link on the standard output, using a format similar to
ip link
from iproute2
.
#f
] [#:down #f
] [#:type #f
] [#:arp-on #f
] [#:arp-off #f
] [#:dynamic-on #f
] [#:dynamic-off #f
] [#:multicast-on #f
] [#:multicast-off #f
] [#:allmulticast-on #f
] [#:allmulticast-off #f
] [#:promisc-on #f
] [#:promisc-off #f
] [#:trailers-on #f
] [#:trailers-off #f
] [#:carrier-on #f
] [#:carrier-off #f
] [#:txqueuelen #f
] [#:name #f
] [#:address #f
] [#:broadcast #f
] [#:mtu #f
] [#:netns #f
] ¶Modify an existing link and set its flags and attributes to the ones specified by the various keywords. When a keyword is omited, the corresponding attribute is not changed.
device can be a device index (as a number) or a device name (as a string).
Do not set #:up
and #:down
at the same time. Do not set
*-on
and *-off
at the same time.
#f
] [#:group #f
] [#:up #f
] [#:master #f
] [#:vrf #f
] [#:type #f
] ¶Print the set of devices on standard output. Setting any of the keyword to a non-false value will filter the results to only show results that match the corresponding value. You may set more than one keyword.
'()
] ¶Add a new link with given name and type. Additional arguments can be passed to control the state of the link at creation. type-args is an association list containing additional values for the given type.
When type is "vlan"
, type-args can contain a number associated
with 'id
: the VLAN id to be created and a link name associated with
"link"
: the name of the link on which the vlan is created.
The following is an example in which we create a new vlan link:
;; same as "ip l add link eth0 name eth0.7 type vlan id 7" (link-add "eth0.7" "vlan" #:type-args '((id . 7) (link . "eth0")))
When type is "veth"
, type-args can contain a string associated
with 'peer
: the name of the peer.
The following is an example in which we create a new veth (virtual ethernet) pair and give them a name:
;; same as "ip l add v0p0 type veth peer v0p1" (link-add "v0p0" "veth" #:type-args '((peer . "v0p1")))
Delete a link. device can contain the name of the link, as a string, or its index, as a number.
Next: Route, Previous: Link, Up: IP Library [Contents][Index]
The (ip addr)
module introduces procedures to access and modify the
network addresses on your machine. They are equivalent to the ip addr
family of commands, from iproute2
.
(cidr->addr cidr)
] [#:broadcast #f] [#:anycast #f] [#:label #f] [#:scope 'global
] [#:metric #f] [#:home? #f] [#:mngtmpaddr? #f] [#:nodad? #f] [optimistic? #f] [noprefixroute? #f] [#:autojoin? #f] ¶Add the address given in cidr to device. device can contain the name of the link, as a string, or its index, as a number.
cidr must be a string containing the address and prefix length, in
CIDR notation (addr/prefix
).
(addr-add "enp1s0" "192.0.2.15/24")
If you wish to add an IPv6 address instead, set #:ipv6
to #t
,
as in the following example.
(addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t)
Note that using the wrong ip type with the wrong value for the #:ipv6?
flag will result in a Bad address
exception from inet-pton.
Additional flags are available; they follow the same semantics as Iproute2. For pointopoint interfaces, you can specify the address of the remote endpoint with #:peer. You can specify a broadcast or anycast address with #:broadcast and #:anycast. All three require an IP address passed as a string when specified.
You can specify a label for the address with #:label. The parameter must be a string that is either the name of the device, or starts with the name of the device, followed by a colon and must contain at most 15 characters.
You can specify a scope with #:scope, whose value is either 'global
,
'link
, 'host
or a numeric value.
You can specify the priority of the prefix route associated with this address
using #:metric
, a number.
Finally, this procedures accepts address configuration flags, whose values are booleans. They are unset by default. Some flags only work for IPv6 addresses, those are #:home? to designate this address as the “home address”, #:mngtmpaddr?, #:nodad? and #:optimistic?. The flags #:noprefixroute? and #:autojoin? can be set for IPv4 and IPv6 addresses.
(cidr->addr cidr)
] [#:broadcast #f] [#:anycast #f] [#:label #f] [#:scope 'global
] [#:metric #f] [#:home? #f] [#:mngtmpaddr? #f] [#:nodad? #f] [optimistic? #f] [noprefixroute? #f] [#:autojoin? #f] ¶Delete the address given in cidr from device. device can contain the name of the link, as a string, or its index, as a number.
cidr must be a string containing the address and prefix length, in
CIDR notation (addr/prefix
).
(addr-del "enp1s0" "192.0.2.15/24")
If you wish to remove an IPv6 address instead, set #:ipv6
to #t
,
as in the following example.
(addr-del "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t)
Note that using the wrong ip type with the wrong value for the #:ipv6?
flag will result in a Bad address
exception from inet-pton.
Additional flags are available, see the description in addr-add
for more
details.
Print the list of addresses for each device on standard output. Setting
device
to a link name or link identifier will restrict the output
to addresses of that device.
Previous: Addr, Up: IP Library [Contents][Index]
The (ip route)
module introduces procedures to access and modify the
network routes on your machine. They are equivalent to the ip route
family of commands, from iproute2
.
Add the route described by the argmuents. dest is the destination network,
in cidr notation (addr/prefix
) or the string "default"
.
#:device is the name or index of a network link. #:table is the
index of a routing table, one of RT_TABLE_COMPAT
, RT_TABLE_DEFAULT
,
RT_TABLE_MAIN
or RT_TABLE_LOCAL
, as defined in
(netlink constant)
.
If it is set, #:protocol must be the routing protocol, RTPROT_*
,
as defined in (netlink constant)
.
#:scope must be the scope of the route, one of RT_SCOPE_*
, as
defined in (netlink constant)
.
#:type must be the type of route, one of RTN_*
, as defined in
(netlink constant)
.
If set, #:priority is a number specifying the priority of the rule when the kernel is looking for a matching rule. This is also known as the metric of the route.
If set, #:src is the source address in cidr notation, or as a single address.
If set, #:via is the gateway address. This is not in cidr notation, as the gateway is a single address, not a network.
(route-add "default" #:device "enp1s0" #:via "192.0.2.1") (route-add "192.0.2.0/24" #:device "enp1s0" #:src "192.0.2.15")
If you wish to add an IPv6 route instead, set #:ipv6
to #t
,
as in the following example.
(addr-add "2001:db8::/64" #:device "enp1s0" #:src "2001:db8::1a4c" #:ipv6? #t)
Note that using the wrong ip type with the wrong value for the #:ipv6?
flag will result in a Bad address
exception from inet-pton.
Delete the route given in arguments. The arguments follow the same structure
as route-add
. By specifying more arguments, you can narrow down the
search for the rule to delete further. Each call will only remove one route,
so being more precise ensures you target the rule you wish to delete. It
is not clear which route is deleted if multiple routes match your query.
Print the list of routes on standard output. Note that, contrary to
ip route show
, we show both IPv4 and IPv6 routes. To narrow down the
number of routes displayed, you can specify the family as in this example.
(route-show #:family AF_INET6)
Next: Programming Index, Previous: IP Library, Up: guile-netlink [Contents][Index]
Jump to: | N R S |
---|
Index Entry | Section | ||
---|---|---|---|
| |||
N | |||
non-blocking socket: | Netlink Connections | ||
| |||
R | |||
ROUTE_NETLINK: | Rtnetlink API | ||
rtnetlink: | Rtnetlink API | ||
| |||
S | |||
subscribing, to an rtnetlink group: | Netlink Connections | ||
|
Jump to: | N R S |
---|
Previous: Concept Index, Up: guile-netlink [Contents][Index]
Jump to: | %
<
A C D E G L M N P R S W |
---|
Jump to: | %
<
A C D E G L M N P R S W |
---|