guile-netlink

Table of Contents

Next: , Up: (dir)   [Contents]

guile-netlink

This document describes guile-netlink version unknown, a guile implementation of the netlink protocol.


Next: , Previous: , Up: Top   [Contents]

1 Introduction

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.


Previous: , Up: Top   [Contents]

2 API Reference


Next: , Up: API Reference   [Contents]

2.1 Common API

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: , Up: Common API   [Contents]

2.1.1 Data Types

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).

Datatype: nl-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:

Scheme Procedure: serialize data pos bv

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:

Scheme Procedure: deserialize type decoder bv pos

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.

Scheme Procedure: get-current-deserialize decoder current-type

Takes a decoder and a type, and returns the deserialization procedure associated with the type (a symbol) in decoder.

Scheme Procedure: get-next-deserialize decoder current-type target-type

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):

Scheme Variable: %default-route-decoder

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.

Scheme Variable: %default-message-decoder

Contains the default association list for the common message types of netlink, associating each of them to a deserialization procedure.

Scheme Procedure: default-route-attr-decoder deserialize-addr

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: , Previous: , Up: Common API   [Contents]

2.1.2 Constants

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:

Scheme Macro: define-enum integer->symbol name-spec ...

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: , Up: Common API   [Contents]

2.1.3 Netlink Connections

The (netlink connection) module defines the following procedures, used to connect and communicate with another process or the kernel using a netlink socket.

Scheme Procedure: get-addr family pid groups

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.

Scheme Procedure: connect proto addr

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.

Scheme Procedure: connect-route [#:groups 0]

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.

Scheme Procedure: close-socket socket

Closes a netlink socket. The socket cannot be used afterwards.

Scheme Procedure: send-msg msg sock [#:addr]

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.

Scheme Procedure: receive-msg sock [#:addr]

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)

Scheme Procedure: receive-and-decode-msg sock decoder [#:addr]

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: , Previous: , Up: API Reference   [Contents]

2.2 Netlink API

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: , Up: Netlink API   [Contents]

2.2.1 Netlink Headers

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:

Datatype: message
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: , Up: Netlink API   [Contents]

2.2.2 Standard Message Types

The (netlink standard) module defines the set of standard message types and their data type.

Datatype: error-message
err

The error code, as a negative number.

hdr

The message on which this error applies.

Scheme Variable: no-data

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: , Up: API Reference   [Contents]

2.3 Rtnetlink API

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: , Up: Rtnetlink API   [Contents]

2.3.1 Routing Attributes

The (netlink route attrs) module defines the following data types:

Datatype: route-attr

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: , Previous: , Up: Rtnetlink API   [Contents]

2.3.2 Link Messages

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: , Up: Rtnetlink API   [Contents]

2.3.3 Address Messages

The (netlink route addr) package defines the following data type:

Datatype: addr-message

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.