doc: Move sections around. Move higher-level stuff above the low-level implmentation details. * doc/guile-netlink.texi (API Reference, IP Library): Inverse order of sections.
doc/guile-netlink.texi
| 31 | 31 | ||
| 32 | 32 | @menu | |
| 33 | 33 | * Introduction:: What is netlink? | |
| 34 | - | * API Reference:: Description of the library interface. | |
| 35 | 34 | * IP Library:: High-level functions for network devices. | |
| 35 | + | * API Reference:: Description of the library interface. | |
| 36 | 36 | ||
| 37 | 37 | * Concept Index:: Concepts. | |
| 38 | 38 | * Programming Index:: Data types, procedures, and variables. | |
… | |||
| 40 | 40 | @detailmenu | |
| 41 | 41 | --- The Detailed Node Listing --- | |
| 42 | 42 | ||
| 43 | + | IP Library | |
| 44 | + | ||
| 45 | + | * Link:: Actions on network links. | |
| 46 | + | * Addr:: Actions on network addresses. | |
| 47 | + | * Route:: Actions on network routes. | |
| 48 | + | ||
| 43 | 49 | API Reference | |
| 44 | 50 | ||
| 45 | 51 | * Common API:: Common functions and data types for defining netlink | |
… | |||
| 47 | 53 | * Netlink API:: Common structures and data types for every protocols. | |
| 48 | 54 | * Rtnetlink API:: The ROUTE_NETLINK protocol. | |
| 49 | 55 | ||
| 50 | - | IP Library | |
| 51 | - | ||
| 52 | - | * Link:: Actions on network links. | |
| 53 | - | * Addr:: Actions on network addresses. | |
| 54 | - | * Route:: Actions on network routes. | |
| 55 | - | ||
| 56 | 56 | @end detailmenu | |
| 57 | 57 | @end menu | |
| 58 | 58 | ||
… | |||
| 71 | 71 | structures that are close to their C counterpart, and basic procedures to | |
| 72 | 72 | initiate communication. | |
| 73 | 73 | ||
| 74 | - | @node API Reference | |
| 75 | - | @chapter API Reference | |
| 74 | + | @node IP Library | |
| 75 | + | @chapter IP Library | |
| 76 | 76 | ||
| 77 | - | @node Common API | |
| 78 | - | @section Common API | |
| 77 | + | This library comes with higher-level procedures that let you access and modify | |
| 78 | + | the state of network on your computer. | |
| 79 | 79 | ||
| 80 | - | Guile-netlink implements a common API for expressing other protocols. This | |
| 81 | - | section describes how to use this API to augment guile-netlink with additional | |
| 82 | - | protocols. | |
| 80 | + | @node Link | |
| 81 | + | @section Link | |
| 83 | 82 | ||
| 84 | - | @node Data Types | |
| 85 | - | @subsection Data Types | |
| 83 | + | The @code{(ip link)} module introduces procedures to access and modify the | |
| 84 | + | network links on your machine. They are equivalent to the @command{ip link} | |
| 85 | + | family of commands, from @code{iproute2}. | |
| 86 | 86 | ||
| 87 | - | Guile-netlink defines data types that are used in the various Netlink protocols. | |
| 88 | - | We need to be able to serialize and deserialize data that guile-netlink | |
| 89 | - | understands, but we also want to let users of guile-netlink extend this process | |
| 90 | - | easily. This need has lead to the creating of the following data structure, | |
| 91 | - | defined in @code{(netlink data}). | |
| 87 | + | @deffn {Datatype} <link> | |
| 92 | 88 | ||
| 93 | - | @deffn {Datatype} nl-data | |
| 89 | + | Datatype representing the status of a network link. | |
| 90 | + | ||
| 91 | + | get-links | |
| 92 | + | print-link | |
| 93 | + | ||
| 94 | + | <link> make-link link? | |
| 95 | + | link-name link-id link-type link-flags link-mtu link-qdisc | |
| 96 | + | link-state link-mode link-group link-qlen link-addr link-brd | |
| 94 | 97 | ||
| 95 | 98 | @table @asis | |
| 96 | - | @item @code{data} | |
| 97 | - | The data that is held by this record. | |
| 99 | + | @item @code{name} | |
| 100 | + | Name of the link, such as @code{"enp1s0"}. | |
| 98 | 101 | ||
| 99 | - | @item @code{size-proc} | |
| 100 | - | A procedure that takes a data (of the same type as the data recorded in the | |
| 101 | - | @code{data} field) and returns the size of its serialization. | |
| 102 | + | @item @code{id} | |
| 103 | + | Index of the link, a unique number used to identify the link. | |
| 102 | 104 | ||
| 103 | - | @item @code{serialization-proc} | |
| 104 | - | A procedure that takes a data (of the same type as the data recorded in the | |
| 105 | - | @code{data} field), the position at which to start serializing, and a | |
| 106 | - | bytevector in which to serialize. This procedure should modify the bytevector | |
| 107 | - | and its return value is ignored. | |
| 105 | + | @item @code{type} | |
| 106 | + | Type of the link, as an integer. | |
| 108 | 107 | ||
| 109 | - | @end table | |
| 110 | - | @end deffn | |
| 108 | + | @item @code{flags} | |
| 109 | + | Flags associated with the device, as a list of symbols, such as | |
| 110 | + | @code{'(UP LOOPBACK)}. | |
| 111 | 111 | ||
| 112 | - | The module also defines the following function, that takes a @code{nl-data} | |
| 113 | - | structure and provides its serialization in a bytevector: | |
| 112 | + | @item @code{mtu} | |
| 113 | + | MTU of the link, as an integer. | |
| 114 | 114 | ||
| 115 | - | @deffn {Scheme Procedure} serialize @var{data} @var{pos} @var{bv} | |
| 116 | - | Takes a @code{nl-data} structure as @var{data}, a position @var{pos} in | |
| 117 | - | the bytevector @var{bv}, and returns an unspecified value. | |
| 115 | + | @item @code{qdisc} | |
| 116 | + | Queuing discipline of the link, as a string, such as @code{"noqueue"}. | |
| 118 | 117 | ||
| 119 | - | This function updates the bytevector and adds the serialization of @var{data} | |
| 120 | - | into @var{bv} at @var{pos}. | |
| 121 | - | @end deffn | |
| 118 | + | @item @code{state} | |
| 119 | + | State of the link, as an integer. Use @code{int->operstate} from | |
| 120 | + | @code{(netlink constant)} to get a symbol, such as @code{IF_OPER_UP}. | |
| 122 | 121 | ||
| 123 | - | By providing a @code{nl-data} structure, we defer the knowledge of how to | |
| 124 | - | serialize the data to the structure itself, instead of the @code{serialize} | |
| 125 | - | function. This allows for more flexibility and extensibility, as the user | |
| 126 | - | of the procedure can pass any kind of data, even if it is not yet supported by | |
| 127 | - | guile-netlink. | |
| 122 | + | @item @code{mode} | |
| 123 | + | Mode of the link. 0 means @code{DORMANT}, 1 means @code{DEFAULT}. | |
| 128 | 124 | ||
| 129 | - | Similarly, we need to be able to deserialize netlink answers into a data | |
| 130 | - | structure. To do so, we also defer the knowledge of the datastructure to | |
| 131 | - | deserialize to, to a decoder structure that is passed to the deserialization | |
| 132 | - | procedure. @code{(netlink data)} also defines the following procedures to | |
| 133 | - | deserialize data: | |
| 125 | + | @item @code{group} | |
| 126 | + | Identifier of the group it belongs to. 0 for @code{default}. | |
| 134 | 127 | ||
| 135 | - | @deffn {Scheme Procedure} deserialize @var{type} @var{decoder} @var{bv} @var{pos} | |
| 136 | - | Takes a bytevector @var{bv} and starts deserializing the data starting at | |
| 137 | - | position @var{pos}. To do so, it uses the @var{type} variable as the lookup | |
| 138 | - | key in the @var{decoder}. @var{type} is a symbol that represents the type of | |
| 139 | - | data to deserialize to. | |
| 128 | + | @item @code{qlen} | |
| 129 | + | Size of the queue. | |
| 140 | 130 | ||
| 141 | - | The decoder is a structure that associates each known type to its deserializer | |
| 142 | - | (a function that takes a decoder, a bytevector and a position and returns some | |
| 143 | - | data) and an alist that associates a type (an integer, as returned by the | |
| 144 | - | protocol in use) to the proper decoder of that type. | |
| 145 | - | @end deffn | |
| 131 | + | @item @code{addr} | |
| 132 | + | Ethernet address of the link, as a string. | |
| 146 | 133 | ||
| 147 | - | @deffn {Scheme Procedure} get-current-deserialize @var{decoder} @var{current-type} | |
| 148 | - | Takes a decoder and a type, and returns the deserialization procedure associated | |
| 149 | - | with the type (a symbol) in @var{decoder}. | |
| 134 | + | @item @code{brd} | |
| 135 | + | Broadcast (ethernet) address of the link, as a string. | |
| 136 | + | ||
| 137 | + | @end table | |
| 150 | 138 | @end deffn | |
| 151 | 139 | ||
| 152 | - | @deffn {Scheme Procedure} get-next-deserialize @var{decoder} @var{current-type} @ | |
| 153 | - | @var{target-type} | |
| 154 | - | Takes a decoder, a type (a symbol that represents the type of data being | |
| 155 | - | deserialized) and another type (an integer as returned by the protocol), and | |
| 156 | - | returns the deserialization procedure needed to continue decoding the data | |
| 157 | - | associated with the currently being deserialized data. | |
| 140 | + | @deffn {Scheme Procedure} get-links | |
| 141 | + | Returns the list of existing links in the system, as a list of @code{<link>} | |
| 142 | + | objects. | |
| 143 | + | @end deffn | |
| 158 | 144 | ||
| 159 | - | For example, when decoding an answer in the netlink protocol, we first deserialize | |
| 160 | - | the header into a @code{message} structure. That header contains a type field | |
| 161 | - | that contains an integer constant representing the type of data of the body. | |
| 162 | - | Similarly, when deserializing a routing attribute in the rtnetlink protocol, | |
| 163 | - | we first find a header of the attribute that defines an integer constant | |
| 164 | - | corresponding to the type of attribute in the body. | |
| 145 | + | @deffn {Scheme Procedure} wait-for-link @var{name} [#:blocking? #t] | |
| 146 | + | Wait until a link called @var{name} (a string such as @code{"ens3"}) shows | |
| 147 | + | up. | |
| 165 | 148 | ||
| 166 | - | By knowing the context in which the type is declared, this procedure can return | |
| 167 | - | the correct deserializing procedure. For instance, when deserializing a | |
| 168 | - | @code{message}, type @code{16} means @code{RTM_NEWLINK} in the rtnetlink | |
| 169 | - | protocol, whereas it means @code{IFLA_OPERSTATE} when deserializing a | |
| 170 | - | @code{route-attribute}. | |
| 149 | + | When @var{blocking?} is false, use a non-blocking socket and cooperate via | |
| 150 | + | @code{current-read-waiter}---useful when using Fibers. | |
| 171 | 151 | @end deffn | |
| 172 | 152 | ||
| 173 | - | guile-netlink provides the following default decoder for the rtnetlink | |
| 174 | - | protocol in @code{(netlink deserialize)}: | |
| 175 | - | ||
| 176 | - | @deffn {Scheme Variable} %default-route-decoder | |
| 177 | - | Contains the default decoder for the NETLINK_ROUTE protocol. | |
| 153 | + | @deffn {Sceme Procedure} print-link @var{link} | |
| 154 | + | Display @var{link} on the standard output, using a format similar to | |
| 155 | + | @command{ip link} from @code{iproute2}. | |
| 178 | 156 | @end deffn | |
| 179 | 157 | ||
| 180 | - | For convenience, guile-netlink defines the following structures that can be used | |
| 181 | - | to create a custom decoder. | |
| 158 | + | @deffn {Scheme Procedure} link-set @var{device} [#:up @code{#f}] @ | |
| 159 | + | [#:down @code{#f}] [#:type @code{#f}] [#:arp-on @code{#f}] @ | |
| 160 | + | [#:arp-off @code{#f}] [#:dynamic-on @code{#f}] [#:dynamic-off @code{#f}] @ | |
| 161 | + | [#:multicast-on @code{#f}] [#:multicast-off @code{#f}] @ | |
| 162 | + | [#:allmulticast-on @code{#f}] [#:allmulticast-off @code{#f}] @ | |
| 163 | + | [#:promisc-on @code{#f}] [#:promisc-off @code{#f}] [#:trailers-on @code{#f}] @ | |
| 164 | + | [#:trailers-off @code{#f}] [#:carrier-on @code{#f}] [#:carrier-off @code{#f}] @ | |
| 165 | + | [#:txqueuelen @code{#f}] [#:name @code{#f}] [#:address @code{#f}] @ | |
| 166 | + | [#:broadcast @code{#f}] [#:mtu @code{#f}] [#:netns @code{#f}] | |
| 167 | + | Modify an existing link and set its flags and attributes to the ones specified | |
| 168 | + | by the various keywords. When a keyword is omited, the corresponding attribute | |
| 169 | + | is not changed. | |
| 182 | 170 | ||
| 183 | - | @deffn {Scheme Variable} %default-message-decoder | |
| 184 | - | Contains the default association list for the common message types of netlink, | |
| 185 | - | associating each of them to a deserialization procedure. | |
| 186 | - | @end deffn | |
| 171 | + | @var{device} can be a device index (as a number) or a device name (as a string). | |
| 187 | 172 | ||
| 188 | - | @deffn {Scheme Procedure} default-route-attr-decoder @var{deserialize-addr} | |
| 189 | - | Creates the default association list for a route protocol, given the specified | |
| 190 | - | address deserializer. This is useful because the @code{IFA_ADDRESS}, | |
| 191 | - | @code{IFA_BROADCAST}, etc, contain a different type of address depending on | |
| 192 | - | the message type or its header. This is defined an @code{(netlink route attrs)} | |
| 193 | - | and used by the following variables: | |
| 173 | + | Do not set @code{#:up} and @code{#:down} at the same time. Do not set | |
| 174 | + | @code{*-on} and @code{*-off} at the same time. | |
| 194 | 175 | @end deffn | |
| 195 | 176 | ||
| 196 | - | @deffn {Scheme Variable} %default-route-link-attr-decoder | |
| 197 | - | Contains the default association list for the known types of routing attributes | |
| 198 | - | for link messages. This list is defined in @code{(netlink route attrs)}. | |
| 177 | + | @deffn {Scheme Procedure} link-show [#:device @code{#f}] [#:group @code{#f}] @ | |
| 178 | + | [#:up @code{#f}] [#:master @code{#f}] [#:vrf @code{#f}] [#:type @code{#f}] | |
| 179 | + | Print the set of devices on standard output. Setting any of the keyword to a | |
| 180 | + | non-false value will filter the results to only show results that match the | |
| 181 | + | corresponding value. You may set more than one keyword. | |
| 199 | 182 | @end deffn | |
| 200 | 183 | ||
| 201 | - | @node Constants | |
| 202 | - | @subsection Constants | |
| 203 | - | ||
| 204 | - | Guile-netlink defines constants used by the Netlink protocols in the | |
| 205 | - | @code{(netlink constant)} module. The constants are the ones present in the | |
| 206 | - | kernel and are too numerous to list here. Please see the source for the | |
| 207 | - | complete list. | |
| 208 | - | ||
| 209 | - | The module also defines the following macro: | |
| 184 | + | @deffn {Scheme Procedure} link-add @var{name} @var{type} [#:type-args @code{'()}] | |
| 185 | + | Add a new link with given name and type. Additional arguments can be passed to | |
| 186 | + | control the state of the link at creation. @var{type-args} is an association | |
| 187 | + | list containing additional values for the given type. | |
| 210 | 188 | ||
| 211 | - | @deffn {Scheme Macro} define-enum @var{integer->symbol} @var{name-spec} ... | |
| 212 | - | This macros defines an enumeration. @var{integer->symbol} is the name of | |
| 213 | - | a procedure that is publicly defined, that takes an integer and returns the | |
| 214 | - | associated symbol in the enumeration. | |
| 189 | + | When @var{type} is @code{"vlan"}, @var{type-args} can contain a number associated | |
| 190 | + | with @code{'id}: the VLAN id to be created and a link name associated with | |
| 191 | + | @code{"link"}: the name of the link on which the vlan is created. | |
| 215 | 192 | ||
| 216 | - | The macro also publicly defines variables whose names are in @var{name-spec} | |
| 217 | - | to an integer. | |
| 193 | + | The following is an example in which we create a new vlan link: | |
| 194 | + | @example | |
| 195 | + | ;; same as "ip l add link eth0 name eth0.7 type vlan id 7" | |
| 196 | + | (link-add "eth0.7" "vlan" #:type-args '((id . 7) (link . "eth0"))) | |
| 197 | + | @end example | |
| 218 | 198 | ||
| 219 | - | A @var{name-spec} is either a single name, and the associated value is 0 for | |
| 220 | - | the first @var{name-spec}, or one more than the previous @var{name-spec}. | |
| 221 | - | It can also be a pair of a name and an integer, in which case the associated | |
| 222 | - | value is that integer. For instance: | |
| 199 | + | When @var{type} is @code{"veth"}, @var{type-args} can contain a string associated | |
| 200 | + | with @code{'peer}: the name of the peer. | |
| 223 | 201 | ||
| 202 | + | The following is an example in which we create a new veth (virtual ethernet) | |
| 203 | + | pair and give them a name: | |
| 224 | 204 | @example | |
| 225 | - | (define-enum get-foo FOO0 FOO1 (FOO10 10) FOO11 FOO12) | |
| 226 | - | (get-foo 9) -> #<unspecified> | |
| 227 | - | (get-foo 0) -> FOO0 | |
| 228 | - | FOO11 -> 11 | |
| 205 | + | ;; same as "ip l add v0p0 type veth peer v0p1" | |
| 206 | + | (link-add "v0p0" "veth" #:type-args '((peer . "v0p1"))) | |
| 229 | 207 | @end example | |
| 230 | 208 | @end deffn | |
| 231 | 209 | ||
| 232 | - | @node Netlink Connections | |
| 233 | - | @subsection Netlink Connections | |
| 234 | - | ||
| 235 | - | The @code{(netlink connection)} module defines the following procedures, used | |
| 236 | - | to connect and communicate with another process or the kernel using a netlink | |
| 237 | - | socket. | |
| 238 | - | ||
| 239 | - | @deffn {Scheme Procedure} get-addr @var{family} @var{pid} @var{groups} | |
| 240 | - | Return a bytevector that represents a netlink address. @var{family} | |
| 241 | - | should be @code{AF_NETLINK}, @var{pid} is the PID of the process with which | |
| 242 | - | to communicate or 0 for the kernel. @var{groups} is an integer representing | |
| 243 | - | the set of broadcast groups to which the connection subscribes. | |
| 210 | + | @deffn {Scheme Procedure} link-del @var{device} | |
| 211 | + | Delete a link. @var{device} can contain the name of the link, as a string, | |
| 212 | + | or its index, as a number. | |
| 244 | 213 | @end deffn | |
| 245 | 214 | ||
| 246 | - | @cindex non-blocking socket | |
| 247 | - | @deffn {Scheme Procedure} connect @var{proto} @var{addr} [#:flags 0] | |
| 248 | - | Creates a netlink socket for @var{proto} and binds it to @var{addr}. | |
| 249 | - | ||
| 250 | - | @var{proto} is the integer representing the protocol. For instance, rtnetlink | |
| 251 | - | can be selected by usin @code{NETLINK_ROUTE} (defined in | |
| 252 | - | @code{(netlink constant)}). | |
| 215 | + | @node Addr | |
| 216 | + | @section Addr | |
| 253 | 217 | ||
| 254 | - | @var{addr} is a bytevector, as returned by @code{get-addr}. | |
| 218 | + | The @code{(ip addr)} module introduces procedures to access and modify the | |
| 219 | + | network addresses on your machine. They are equivalent to the @command{ip addr} | |
| 220 | + | family of commands, from @code{iproute2}. | |
| 255 | 221 | ||
| 256 | - | @var{flags} is a set of additional flags to pass as the second argument | |
| 257 | - | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. | |
| 258 | - | @end deffn | |
| 222 | + | @deffn {Scheme Procedure} addr-add @var{device} @var{cidr} [@var{#:ipv6?} #f] @ | |
| 223 | + | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ | |
| 224 | + | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ | |
| 225 | + | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ | |
| 226 | + | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ | |
| 227 | + | [@var{#:autojoin?} #f] | |
| 228 | + | Add the address given in @var{cidr} to @var{device}. @var{device} can | |
| 229 | + | contain the name of the link, as a string, or its index, as a number. | |
| 259 | 230 | ||
| 260 | - | @deffn {Scheme Procedure} connect-route [#:groups 0] [#:flags 0] | |
| 261 | - | This procedure is a wrapper for @code{connect} that creates a socket for the | |
| 262 | - | rtnetlink protocol, binds it to the kernel and returns it. By passing the | |
| 263 | - | optional @var{groups} keyword, you can select broadcast groups to subscribe to. | |
| 231 | + | @var{cidr} must be a string containing the address and prefix length, in | |
| 232 | + | CIDR notation (@code{addr/prefix}). | |
| 264 | 233 | ||
| 265 | - | @var{flags} is a set of additional flags to pass as the second argument | |
| 266 | - | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. | |
| 267 | - | @end deffn | |
| 234 | + | @example | |
| 235 | + | (addr-add "enp1s0" "192.0.2.15/24") | |
| 236 | + | @end example | |
| 268 | 237 | ||
| 269 | - | @cindex subscribing, to an rtnetlink group | |
| 270 | - | @deffn {Scheme Procedure} add-socket-membership @var{sock} @var{group} | |
| 271 | - | Make @var{sock} a member of @var{group}, an @code{RTNLGRP_} constant, | |
| 272 | - | meaning that it will be subscribed to events of that group. | |
| 238 | + | If you wish to add an IPv6 address instead, set @code{#:ipv6} to @code{#t}, | |
| 239 | + | as in the following example. | |
| 273 | 240 | ||
| 274 | - | For example, here is how you could create a netlink socket and subscribe | |
| 275 | - | it to the ``link'' group so that it receives notifications for new and | |
| 276 | - | removed links: | |
| 241 | + | @example | |
| 242 | + | (addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) | |
| 243 | + | @end example | |
| 277 | 244 | ||
| 278 | - | @lisp | |
| 279 | - | (let ((sock (connect-route))) | |
| 280 | - | (add-socket-membership sock RTNLGRP_LINK) | |
| 281 | - | @dots{}) | |
| 282 | - | @end lisp | |
| 245 | + | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 246 | + | flag will result in a @code{Bad address} exception from inet-pton. | |
| 283 | 247 | ||
| 284 | - | This procedure is implemented as a @code{setsockopt} call. | |
| 285 | - | @end deffn | |
| 248 | + | Additional flags are available; they follow the same semantics as Iproute2. | |
| 249 | + | For pointopoint interfaces, you can specify the address of the remote endpoint | |
| 250 | + | with @var{#:peer}. You can specify a broadcast or anycast address with | |
| 251 | + | @var{#:broadcast} and @var{#:anycast}. All three require an IP address passed | |
| 252 | + | as a string when specified. | |
| 286 | 253 | ||
| 287 | - | @deffn {Scheme Procedure} send-msg @var{msg} @var{sock} [#:@var{addr}] | |
| 288 | - | Send @var{msg} (it must be of type message, @xref{Netlink Headers}) to | |
| 289 | - | @var{addr} using @var{sock}. If not passed, @var{addr} is the address of | |
| 290 | - | the kernel. | |
| 291 | - | @end deffn | |
| 254 | + | You can specify a label for the address with @var{#:label}. The parameter must | |
| 255 | + | be a string that is either the name of the device, or starts with the name of | |
| 256 | + | the device, followed by a colon and must contain at most 15 characters. | |
| 292 | 257 | ||
| 293 | - | @deffn {Scheme Procedure} receive-msg @var{sock} [#:@var{addr}] | |
| 294 | - | Receives a message from @var{sock} from @var{addr}. This procedure is blocking. | |
| 295 | - | If not passed, @var{addr} defaults to the address of the kernel. This | |
| 296 | - | procedure returns the message as a bytevector, that you can deserialize with | |
| 297 | - | @code{deserialize} (@xref{Data Types}) | |
| 298 | - | @end deffn | |
| 258 | + | You can specify a scope with @var{#:scope}, whose value is either @code{'global}, | |
| 259 | + | @code{'link}, @code{'host} or a numeric value. | |
| 299 | 260 | ||
| 300 | - | @deffn {Scheme Procedure} receive-and-decode-msg @var{sock} @var{decoder} @ | |
| 301 | - | [#:@var{addr}] | |
| 302 | - | Receives one or more messages from @var{sock} from @var{addr}. this procedure | |
| 303 | - | is blocking. If not passed, @var{addr} defaults to the address of the kernel. | |
| 304 | - | This procedure returns a list of messages that were decoded using @var{decoder}. | |
| 261 | + | You can specify the priority of the prefix route associated with this address | |
| 262 | + | using @code{#:metric}, a number. | |
| 305 | 263 | ||
| 306 | - | When the answer has the @code{NLM_F_MULTI} flag, this procedure decodes the next | |
| 307 | - | message, until it receives a @code{NLMSG_DONE} message. It returns the list | |
| 308 | - | of every netlink messages it received, including the @code{NLMSG_DONE}. | |
| 264 | + | Finally, this procedures accepts address configuration flags, whose values are | |
| 265 | + | booleans. They are unset by default. Some flags only work for IPv6 addresses, | |
| 266 | + | those are @var{#:home?} to designate this address as the ``home address'', | |
| 267 | + | @var{#:mngtmpaddr?}, @var{#:nodad?} and @var{#:optimistic?}. The flags | |
| 268 | + | @var{#:noprefixroute?} and @var{#:autojoin?} can be set for IPv4 and IPv6 | |
| 269 | + | addresses. | |
| 309 | 270 | @end deffn | |
| 310 | 271 | ||
| 311 | - | @node Netlink API | |
| 312 | - | @section Netlink API | |
| 272 | + | @deffn {Scheme Procedure} addr-del @var{device} @var{cidr} [@var{#:ipv6?} #f] @ | |
| 273 | + | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ | |
| 274 | + | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ | |
| 275 | + | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ | |
| 276 | + | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ | |
| 277 | + | [@var{#:autojoin?} #f] | |
| 278 | + | Delete the address given in @var{cidr} from @var{device}. @var{device} can | |
| 279 | + | contain the name of the link, as a string, or its index, as a number. | |
| 313 | 280 | ||
| 314 | - | This section introduces the data structures used for all the netlink protocols. | |
| 315 | - | First, we introduce the structure of a netlink message, then we present the | |
| 316 | - | standard types of netlink messages, that can be used with every protocol. | |
| 281 | + | @var{cidr} must be a string containing the address and prefix length, in | |
| 282 | + | CIDR notation (@code{addr/prefix}). | |
| 317 | 283 | ||
| 318 | - | @node Netlink Headers | |
| 319 | - | @subsection Netlink Headers | |
| 284 | + | @example | |
| 285 | + | (addr-del "enp1s0" "192.0.2.15/24") | |
| 286 | + | @end example | |
| 320 | 287 | ||
| 321 | - | The @code{(netlink message)} module defines the message structure that contains | |
| 322 | - | a netlink message. It is composed of a header and a body, and is the data | |
| 323 | - | structure to pass to @code{send-msg} (@xref{Netlink Connections}). | |
| 288 | + | If you wish to remove an IPv6 address instead, set @code{#:ipv6} to @code{#t}, | |
| 289 | + | as in the following example. | |
| 324 | 290 | ||
| 325 | - | This module defines the following data structure: | |
| 291 | + | @example | |
| 292 | + | (addr-del "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) | |
| 293 | + | @end example | |
| 326 | 294 | ||
| 327 | - | @deffn {Datatype} message | |
| 328 | - | @table @asis | |
| 329 | - | @item @code{type} | |
| 330 | - | The type of data in the body of the message. For instance, @code{RTM_GETLINK}. | |
| 295 | + | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 296 | + | flag will result in a @code{Bad address} exception from inet-pton. | |
| 331 | 297 | ||
| 332 | - | @item @code{flags} | |
| 333 | - | The set of flags that are set in the header. For instance, | |
| 334 | - | @code{(logior NLM_F_REQUEST NLM_F_DUMP)}. | |
| 298 | + | Additional flags are available, see the description in @code{addr-add} for more | |
| 299 | + | details. | |
| 300 | + | @end deffn | |
| 335 | 301 | ||
| 336 | - | @item @code{seq} | |
| 337 | - | The sequence number of the message. If this message is an answer to a request, | |
| 338 | - | it must keep the same sequence number. Otherwise, you must generate a new and | |
| 339 | - | unique sequence number, to track the answers. | |
| 302 | + | @deffn {Scheme Procedure} addr-show [@var{device}] | |
| 303 | + | Print the list of addresses for each device on standard output. Setting | |
| 304 | + | @code{device} to a link name or link identifier will restrict the output | |
| 305 | + | to addresses of that device. | |
| 306 | + | @end deffn | |
| 340 | 307 | ||
| 341 | - | @item @code{pid} | |
| 342 | - | The PID of the receiving process, or 0 for the kernel. | |
| 308 | + | @node Route | |
| 309 | + | @section Route | |
| 343 | 310 | ||
| 344 | - | @item @code{data} | |
| 345 | - | The actual body, as an @code{nl-data} structure. | |
| 311 | + | The @code{(ip route)} module introduces procedures to access and modify the | |
| 312 | + | network routes on your machine. They are equivalent to the @command{ip route} | |
| 313 | + | family of commands, from @code{iproute2}. | |
| 346 | 314 | ||
| 347 | - | @end table | |
| 348 | - | @end deffn | |
| 315 | + | @deffn {Scheme Procedure} route-add @var{dest} [@var{#:ipv6?} #f] @ | |
| 316 | + | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ | |
| 317 | + | [@var{#:scope} RT_SCOPE_LINK] [@var{#:type} RTN_UNICAST] @ | |
| 318 | + | [@var{#:priority} #f] [@var{#:src} #f] [@var{#:via} #f] | |
| 319 | + | Add the route described by the argmuents. @var{dest} is the destination network, | |
| 320 | + | in cidr notation (@code{addr/prefix}) or the string @code{"default"}. | |
| 349 | 321 | ||
| 350 | - | @node Standard Message Types | |
| 351 | - | @subsection Standard Message Types | |
| 322 | + | @var{#:device} is the name or index of a network link. @var{#:table} is the | |
| 323 | + | index of a routing table, one of @code{RT_TABLE_COMPAT}, @code{RT_TABLE_DEFAULT}, | |
| 324 | + | @code{RT_TABLE_MAIN} or @code{RT_TABLE_LOCAL}, as defined in | |
| 325 | + | @code{(netlink constant)}. | |
| 352 | 326 | ||
| 353 | - | The @code{(netlink standard)} module defines the set of standard message types | |
| 354 | - | and their data type. | |
| 327 | + | If it is set, @var{#:protocol} must be the routing protocol, @code{RTPROT_*}, | |
| 328 | + | as defined in @code{(netlink constant)}. | |
| 355 | 329 | ||
| 356 | - | @deffn {Datatype} error-message | |
| 357 | - | @table @asis | |
| 358 | - | @item @code{err} | |
| 359 | - | The error code, as a negative number. | |
| 330 | + | @var{#:scope} must be the scope of the route, one of @code{RT_SCOPE_*}, as | |
| 331 | + | defined in @code{(netlink constant)}. | |
| 360 | 332 | ||
| 361 | - | @item @code{hdr} | |
| 362 | - | The message on which this error applies. | |
| 333 | + | @var{#:type} must be the type of route, one of @code{RTN_*}, as defined in | |
| 334 | + | @code{(netlink constant)}. | |
| 363 | 335 | ||
| 364 | - | @end table | |
| 336 | + | If set, @var{#:priority} is a number specifying the priority of the rule | |
| 337 | + | when the kernel is looking for a matching rule. This is also known as the | |
| 338 | + | metric of the route. | |
| 365 | 339 | ||
| 366 | - | @deffn {Scheme Variable} no-data | |
| 367 | - | This variable defines the absence of data. This is useful when a structure | |
| 368 | - | is expecting a body part, but the protocol specifically defines that it should | |
| 369 | - | not take any data in some cases. For instance, a @code{NLMSG_NOOP} message | |
| 370 | - | takes no data, so the @code{data} field of the message will contain this | |
| 371 | - | @code{no-data} value. | |
| 340 | + | If set, @var{#:src} is the source address in cidr notation, or as a single | |
| 341 | + | address. | |
| 342 | + | ||
| 343 | + | If set, @var{#:via} is the gateway address. This is not in cidr notation, as | |
| 344 | + | the gateway is a single address, not a network. | |
| 345 | + | ||
| 346 | + | @example | |
| 347 | + | (route-add "default" #:device "enp1s0" #:via "192.0.2.1") | |
| 348 | + | (route-add "192.0.2.0/24" #:device "enp1s0" #:src "192.0.2.15") | |
| 349 | + | @end example | |
| 350 | + | ||
| 351 | + | If you wish to add an IPv6 route instead, set @code{#:ipv6} to @code{#t}, | |
| 352 | + | as in the following example. | |
| 353 | + | ||
| 354 | + | @example | |
| 355 | + | (addr-add "2001:db8::/64" #:device "enp1s0" #:src "2001:db8::1a4c" #:ipv6? #t) | |
| 356 | + | @end example | |
| 357 | + | ||
| 358 | + | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 359 | + | flag will result in a @code{Bad address} exception from inet-pton. | |
| 372 | 360 | @end deffn | |
| 373 | 361 | ||
| 362 | + | @deffn {Scheme Procedure} route-del @var{dest} [@var{#:ipv6?} #f] @ | |
| 363 | + | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ | |
| 364 | + | [@var{#:scope} #f] [@var{#:type} #f] [@var{#:priority} #f] @ | |
| 365 | + | [@var{#:src} #f] [@var{#:via} #f] | |
| 366 | + | Delete the route given in arguments. The arguments follow the same structure | |
| 367 | + | as @code{route-add}. By specifying more arguments, you can narrow down the | |
| 368 | + | search for the rule to delete further. Each call will only remove one route, | |
| 369 | + | so being more precise ensures you target the rule you wish to delete. It | |
| 370 | + | is not clear which route is deleted if multiple routes match your query. | |
| 374 | 371 | @end deffn | |
| 375 | 372 | ||
| 376 | - | @node Rtnetlink API | |
| 377 | - | @section Rtnetlink API | |
| 378 | - | @cindex rtnetlink | |
| 379 | - | @cindex ROUTE_NETLINK | |
| 373 | + | @deffn {Scheme Procedure} route-show [@var{#:table} RT_TABLE_MAIN] @ | |
| 374 | + | [@var{#:family} AF_UNSPEC] | |
| 375 | + | Print the list of routes on standard output. Note that, contrary to | |
| 376 | + | @command{ip route show}, we show both IPv4 and IPv6 routes. To narrow down the | |
| 377 | + | number of routes displayed, you can specify the family as in this example. | |
| 380 | 378 | ||
| 381 | - | This section describes the support for rtnetlink in guile-netlink. Rtnetlink | |
| 382 | - | is the protocol responsible for everything related to network routing. It | |
| 383 | - | allows you to manage links, addresses, routing tables, neighbor chaces, | |
| 384 | - | routing rules, queueing disciplines, traffic classes, traffic filters and | |
| 385 | - | more. | |
| 379 | + | @example | |
| 380 | + | (route-show #:family AF_INET6) | |
| 381 | + | @end example | |
| 382 | + | @end deffn | |
| 386 | 383 | ||
| 387 | - | @node Routing Attributes | |
| 388 | - | @subsection Routing Attributes | |
| 384 | + | @node API Reference | |
| 385 | + | @chapter API Reference | |
| 389 | 386 | ||
| 390 | - | The @code{(netlink route attrs)} module defines the following data types: | |
| 387 | + | @node Common API | |
| 388 | + | @section Common API | |
| 391 | 389 | ||
| 392 | - | @deffn {Datatype} route-attr | |
| 393 | - | This defines a header structure for the attribute, as well as its body. | |
| 390 | + | Guile-netlink implements a common API for expressing other protocols. This | |
| 391 | + | section describes how to use this API to augment guile-netlink with additional | |
| 392 | + | protocols. | |
| 394 | 393 | ||
| 395 | - | @table @asis | |
| 396 | - | @item @code{type} | |
| 397 | - | This is the type of the attribute, for instance @code{IFLA_ADDRESS}. | |
| 394 | + | @node Data Types | |
| 395 | + | @subsection Data Types | |
| 396 | + | ||
| 397 | + | Guile-netlink defines data types that are used in the various Netlink protocols. | |
| 398 | + | We need to be able to serialize and deserialize data that guile-netlink | |
| 399 | + | understands, but we also want to let users of guile-netlink extend this process | |
| 400 | + | easily. This need has lead to the creating of the following data structure, | |
| 401 | + | defined in @code{(netlink data}). | |
| 398 | 402 | ||
| 403 | + | @deffn {Datatype} nl-data | |
| 404 | + | ||
| 405 | + | @table @asis | |
| 399 | 406 | @item @code{data} | |
| 400 | - | This is the body of the attribute, ie.@: its value. | |
| 401 | - | @end table | |
| 402 | - | @end deffn | |
| 407 | + | The data that is held by this record. | |
| 403 | 408 | ||
| 404 | - | The module also defines additional data types that are not represented as | |
| 405 | - | a record, but by a simple type. For each of the following types, there is | |
| 406 | - | a @code{make-*-route-attr} procedure to produce a @code{nl-data} value | |
| 407 | - | for this type. There is also @code{deserialize-route-attr-data-*} procedure | |
| 408 | - | to deserialize a value of this type. | |
| 409 | + | @item @code{size-proc} | |
| 410 | + | A procedure that takes a data (of the same type as the data recorded in the | |
| 411 | + | @code{data} field) and returns the size of its serialization. | |
| 409 | 412 | ||
| 410 | - | @table @asis | |
| 411 | - | @item @code{u8} | |
| 412 | - | A one-byte unsigned integer | |
| 413 | - | @item @code{u16} | |
| 414 | - | A two-bytes unsigned integer | |
| 415 | - | @item @code{u32} | |
| 416 | - | A four-bytes unsigned integer | |
| 417 | - | @item @code{s32} | |
| 418 | - | A four-bytes signed integer | |
| 419 | - | @item @code{string} | |
| 420 | - | A string | |
| 421 | - | @item @code{ethernet} | |
| 422 | - | An ethernet address. Its value is a string that represents that address, | |
| 423 | - | for instnace @code{"01:23:45:67:89:ab"} | |
| 424 | - | @item @code{ipv4} | |
| 425 | - | An IPv4 address. Its value is a string that represents that address, | |
| 426 | - | for instnace @code{"192.0.2.152"} | |
| 427 | - | @item @code{ipv6} | |
| 428 | - | An IPv6 address. Its value is a string that represents that address, | |
| 429 | - | for instnace @code{"2001:db8::0123:4567:89ab:cdef"} | |
| 430 | - | @item @code{bv} | |
| 431 | - | A bytevector. This is used by default when the type is not supported. | |
| 432 | - | @end table | |
| 413 | + | @item @code{serialization-proc} | |
| 414 | + | A procedure that takes a data (of the same type as the data recorded in the | |
| 415 | + | @code{data} field), the position at which to start serializing, and a | |
| 416 | + | bytevector in which to serialize. This procedure should modify the bytevector | |
| 417 | + | and its return value is ignored. | |
| 433 | 418 | ||
| 434 | - | @node Link Messages | |
| 435 | - | @subsection Link Messages | |
| 419 | + | @end table | |
| 420 | + | @end deffn | |
| 436 | 421 | ||
| 437 | - | The @code{(netlink route link)} package defines the following data type: | |
| 422 | + | The module also defines the following function, that takes a @code{nl-data} | |
| 423 | + | structure and provides its serialization in a bytevector: | |
| 438 | 424 | ||
| 439 | - | @deffn {Datatype} link-message | |
| 440 | - | This datatype represents a link message with its routing attributes. This type | |
| 441 | - | of message is expected when using the @var{RTM_*LINK} message types. | |
| 425 | + | @deffn {Scheme Procedure} serialize @var{data} @var{pos} @var{bv} | |
| 426 | + | Takes a @code{nl-data} structure as @var{data}, a position @var{pos} in | |
| 427 | + | the bytevector @var{bv}, and returns an unspecified value. | |
| 442 | 428 | ||
| 443 | - | @table @asis | |
| 444 | - | @item @code{family} | |
| 445 | - | The network family, defined as @code{AF_UNSPEC} in the rtnetlink documentation, | |
| 446 | - | although it holds different values in practice. | |
| 429 | + | This function updates the bytevector and adds the serialization of @var{data} | |
| 430 | + | into @var{bv} at @var{pos}. | |
| 431 | + | @end deffn | |
| 447 | 432 | ||
| 448 | - | @item @code{type} | |
| 449 | - | The device type. | |
| 433 | + | By providing a @code{nl-data} structure, we defer the knowledge of how to | |
| 434 | + | serialize the data to the structure itself, instead of the @code{serialize} | |
| 435 | + | function. This allows for more flexibility and extensibility, as the user | |
| 436 | + | of the procedure can pass any kind of data, even if it is not yet supported by | |
| 437 | + | guile-netlink. | |
| 450 | 438 | ||
| 451 | - | @item @code{index} | |
| 452 | - | The index of the device. This is used to select a specific device by its index, | |
| 453 | - | or 0 to not filter by device index. | |
| 439 | + | Similarly, we need to be able to deserialize netlink answers into a data | |
| 440 | + | structure. To do so, we also defer the knowledge of the datastructure to | |
| 441 | + | deserialize to, to a decoder structure that is passed to the deserialization | |
| 442 | + | procedure. @code{(netlink data)} also defines the following procedures to | |
| 443 | + | deserialize data: | |
| 454 | 444 | ||
| 455 | - | @item @code{flags} | |
| 456 | - | The device flags. See @code{man 7 netdevices} for a list. | |
| 445 | + | @deffn {Scheme Procedure} deserialize @var{type} @var{decoder} @var{bv} @var{pos} | |
| 446 | + | Takes a bytevector @var{bv} and starts deserializing the data starting at | |
| 447 | + | position @var{pos}. To do so, it uses the @var{type} variable as the lookup | |
| 448 | + | key in the @var{decoder}. @var{type} is a symbol that represents the type of | |
| 449 | + | data to deserialize to. | |
| 457 | 450 | ||
| 458 | - | @item @code{attrs} | |
| 459 | - | A list of attributes. This field must contain a list of @code{nl-data} | |
| 460 | - | structures, not a structure by itself. | |
| 461 | - | @end table | |
| 451 | + | The decoder is a structure that associates each known type to its deserializer | |
| 452 | + | (a function that takes a decoder, a bytevector and a position and returns some | |
| 453 | + | data) and an alist that associates a type (an integer, as returned by the | |
| 454 | + | protocol in use) to the proper decoder of that type. | |
| 462 | 455 | @end deffn | |
| 463 | 456 | ||
| 464 | - | @node Address Messages | |
| 465 | - | @subsection Address Messages | |
| 457 | + | @deffn {Scheme Procedure} get-current-deserialize @var{decoder} @var{current-type} | |
| 458 | + | Takes a decoder and a type, and returns the deserialization procedure associated | |
| 459 | + | with the type (a symbol) in @var{decoder}. | |
| 460 | + | @end deffn | |
| 466 | 461 | ||
| 467 | - | The @code{(netlink route addr)} package defines the following data type: | |
| 462 | + | @deffn {Scheme Procedure} get-next-deserialize @var{decoder} @var{current-type} @ | |
| 463 | + | @var{target-type} | |
| 464 | + | Takes a decoder, a type (a symbol that represents the type of data being | |
| 465 | + | deserialized) and another type (an integer as returned by the protocol), and | |
| 466 | + | returns the deserialization procedure needed to continue decoding the data | |
| 467 | + | associated with the currently being deserialized data. | |
| 468 | 468 | ||
| 469 | - | @deffn {Datatype} addr-message | |
| 470 | - | This datatype represents an address message with its routing attributes. This | |
| 471 | - | type of message is expected when using the @var{RTM_*ADDR} message types. | |
| 469 | + | For example, when decoding an answer in the netlink protocol, we first deserialize | |
| 470 | + | the header into a @code{message} structure. That header contains a type field | |
| 471 | + | that contains an integer constant representing the type of data of the body. | |
| 472 | + | Similarly, when deserializing a routing attribute in the rtnetlink protocol, | |
| 473 | + | we first find a header of the attribute that defines an integer constant | |
| 474 | + | corresponding to the type of attribute in the body. | |
| 472 | 475 | ||
| 473 | - | @table @asis | |
| 474 | - | @item @code{family} | |
| 475 | - | The network family, either @code{AF_INET} for IPv4 addresses, or @code{AF_INET6} | |
| 476 | - | for IPv6 addresses. | |
| 476 | + | By knowing the context in which the type is declared, this procedure can return | |
| 477 | + | the correct deserializing procedure. For instance, when deserializing a | |
| 478 | + | @code{message}, type @code{16} means @code{RTM_NEWLINK} in the rtnetlink | |
| 479 | + | protocol, whereas it means @code{IFLA_OPERSTATE} when deserializing a | |
| 480 | + | @code{route-attribute}. | |
| 481 | + | @end deffn | |
| 477 | 482 | ||
| 478 | - | @item @code{prefix-len} | |
| 479 | - | The prefix length, i.e.@: the length of the prefix mask, in bits, if defined | |
| 480 | - | for the address family. | |
| 483 | + | guile-netlink provides the following default decoder for the rtnetlink | |
| 484 | + | protocol in @code{(netlink deserialize)}: | |
| 481 | 485 | ||
| 482 | - | @item @code{flags} | |
| 483 | - | Address flags. This can be a flag word of @code{IFA_F_SECONDARY} for secondary | |
| 484 | - | address (old alias interface), @code{IFA_F_PERMANENT} for a permanent | |
| 485 | - | address set by the user and other undocumented flags. | |
| 486 | + | @deffn {Scheme Variable} %default-route-decoder | |
| 487 | + | Contains the default decoder for the NETLINK_ROUTE protocol. | |
| 488 | + | @end deffn | |
| 486 | 489 | ||
| 487 | - | @item @code{scope} | |
| 488 | - | The address scope. | |
| 490 | + | For convenience, guile-netlink defines the following structures that can be used | |
| 491 | + | to create a custom decoder. | |
| 489 | 492 | ||
| 490 | - | @item @code{index} | |
| 491 | - | The index of the device this address is for. | |
| 493 | + | @deffn {Scheme Variable} %default-message-decoder | |
| 494 | + | Contains the default association list for the common message types of netlink, | |
| 495 | + | associating each of them to a deserialization procedure. | |
| 496 | + | @end deffn | |
| 492 | 497 | ||
| 493 | - | @item @code{attrs} | |
| 494 | - | A list of attributes. This field must contain a list of @code{nl-data} | |
| 495 | - | structures, not a structure by itself. | |
| 496 | - | @end table | |
| 498 | + | @deffn {Scheme Procedure} default-route-attr-decoder @var{deserialize-addr} | |
| 499 | + | Creates the default association list for a route protocol, given the specified | |
| 500 | + | address deserializer. This is useful because the @code{IFA_ADDRESS}, | |
| 501 | + | @code{IFA_BROADCAST}, etc, contain a different type of address depending on | |
| 502 | + | the message type or its header. This is defined an @code{(netlink route attrs)} | |
| 503 | + | and used by the following variables: | |
| 497 | 504 | @end deffn | |
| 498 | 505 | ||
| 499 | - | @node IP Library | |
| 500 | - | @chapter IP Library | |
| 506 | + | @deffn {Scheme Variable} %default-route-link-attr-decoder | |
| 507 | + | Contains the default association list for the known types of routing attributes | |
| 508 | + | for link messages. This list is defined in @code{(netlink route attrs)}. | |
| 509 | + | @end deffn | |
| 501 | 510 | ||
| 502 | - | This library comes with higher-level procedures that let you access and modify | |
| 503 | - | the state of network on your computer. | |
| 511 | + | @node Constants | |
| 512 | + | @subsection Constants | |
| 504 | 513 | ||
| 505 | - | @node Link | |
| 506 | - | @section Link | |
| 514 | + | Guile-netlink defines constants used by the Netlink protocols in the | |
| 515 | + | @code{(netlink constant)} module. The constants are the ones present in the | |
| 516 | + | kernel and are too numerous to list here. Please see the source for the | |
| 517 | + | complete list. | |
| 507 | 518 | ||
| 508 | - | The @code{(ip link)} module introduces procedures to access and modify the | |
| 509 | - | network links on your machine. They are equivalent to the @command{ip link} | |
| 510 | - | family of commands, from @code{iproute2}. | |
| 519 | + | The module also defines the following macro: | |
| 511 | 520 | ||
| 512 | - | @deffn {Datatype} <link> | |
| 521 | + | @deffn {Scheme Macro} define-enum @var{integer->symbol} @var{name-spec} ... | |
| 522 | + | This macros defines an enumeration. @var{integer->symbol} is the name of | |
| 523 | + | a procedure that is publicly defined, that takes an integer and returns the | |
| 524 | + | associated symbol in the enumeration. | |
| 513 | 525 | ||
| 514 | - | Datatype representing the status of a network link. | |
| 526 | + | The macro also publicly defines variables whose names are in @var{name-spec} | |
| 527 | + | to an integer. | |
| 515 | 528 | ||
| 516 | - | get-links | |
| 517 | - | print-link | |
| 518 | - | ||
| 519 | - | <link> make-link link? | |
| 520 | - | link-name link-id link-type link-flags link-mtu link-qdisc | |
| 521 | - | link-state link-mode link-group link-qlen link-addr link-brd | |
| 529 | + | A @var{name-spec} is either a single name, and the associated value is 0 for | |
| 530 | + | the first @var{name-spec}, or one more than the previous @var{name-spec}. | |
| 531 | + | It can also be a pair of a name and an integer, in which case the associated | |
| 532 | + | value is that integer. For instance: | |
| 522 | 533 | ||
| 523 | - | @table @asis | |
| 524 | - | @item @code{name} | |
| 525 | - | Name of the link, such as @code{"enp1s0"}. | |
| 534 | + | @example | |
| 535 | + | (define-enum get-foo FOO0 FOO1 (FOO10 10) FOO11 FOO12) | |
| 536 | + | (get-foo 9) -> #<unspecified> | |
| 537 | + | (get-foo 0) -> FOO0 | |
| 538 | + | FOO11 -> 11 | |
| 539 | + | @end example | |
| 540 | + | @end deffn | |
| 526 | 541 | ||
| 527 | - | @item @code{id} | |
| 528 | - | Index of the link, a unique number used to identify the link. | |
| 542 | + | @node Netlink Connections | |
| 543 | + | @subsection Netlink Connections | |
| 529 | 544 | ||
| 530 | - | @item @code{type} | |
| 531 | - | Type of the link, as an integer. | |
| 545 | + | The @code{(netlink connection)} module defines the following procedures, used | |
| 546 | + | to connect and communicate with another process or the kernel using a netlink | |
| 547 | + | socket. | |
| 532 | 548 | ||
| 533 | - | @item @code{flags} | |
| 534 | - | Flags associated with the device, as a list of symbols, such as | |
| 535 | - | @code{'(UP LOOPBACK)}. | |
| 549 | + | @deffn {Scheme Procedure} get-addr @var{family} @var{pid} @var{groups} | |
| 550 | + | Return a bytevector that represents a netlink address. @var{family} | |
| 551 | + | should be @code{AF_NETLINK}, @var{pid} is the PID of the process with which | |
| 552 | + | to communicate or 0 for the kernel. @var{groups} is an integer representing | |
| 553 | + | the set of broadcast groups to which the connection subscribes. | |
| 554 | + | @end deffn | |
| 536 | 555 | ||
| 537 | - | @item @code{mtu} | |
| 538 | - | MTU of the link, as an integer. | |
| 556 | + | @cindex non-blocking socket | |
| 557 | + | @deffn {Scheme Procedure} connect @var{proto} @var{addr} [#:flags 0] | |
| 558 | + | Creates a netlink socket for @var{proto} and binds it to @var{addr}. | |
| 539 | 559 | ||
| 540 | - | @item @code{qdisc} | |
| 541 | - | Queuing discipline of the link, as a string, such as @code{"noqueue"}. | |
| 560 | + | @var{proto} is the integer representing the protocol. For instance, rtnetlink | |
| 561 | + | can be selected by usin @code{NETLINK_ROUTE} (defined in | |
| 562 | + | @code{(netlink constant)}). | |
| 542 | 563 | ||
| 543 | - | @item @code{state} | |
| 544 | - | State of the link, as an integer. Use @code{int->operstate} from | |
| 545 | - | @code{(netlink constant)} to get a symbol, such as @code{IF_OPER_UP}. | |
| 564 | + | @var{addr} is a bytevector, as returned by @code{get-addr}. | |
| 546 | 565 | ||
| 547 | - | @item @code{mode} | |
| 548 | - | Mode of the link. 0 means @code{DORMANT}, 1 means @code{DEFAULT}. | |
| 566 | + | @var{flags} is a set of additional flags to pass as the second argument | |
| 567 | + | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. | |
| 568 | + | @end deffn | |
| 549 | 569 | ||
| 550 | - | @item @code{group} | |
| 551 | - | Identifier of the group it belongs to. 0 for @code{default}. | |
| 570 | + | @deffn {Scheme Procedure} connect-route [#:groups 0] [#:flags 0] | |
| 571 | + | This procedure is a wrapper for @code{connect} that creates a socket for the | |
| 572 | + | rtnetlink protocol, binds it to the kernel and returns it. By passing the | |
| 573 | + | optional @var{groups} keyword, you can select broadcast groups to subscribe to. | |
| 552 | 574 | ||
| 553 | - | @item @code{qlen} | |
| 554 | - | Size of the queue. | |
| 575 | + | @var{flags} is a set of additional flags to pass as the second argument | |
| 576 | + | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. | |
| 577 | + | @end deffn | |
| 555 | 578 | ||
| 556 | - | @item @code{addr} | |
| 557 | - | Ethernet address of the link, as a string. | |
| 579 | + | @cindex subscribing, to an rtnetlink group | |
| 580 | + | @deffn {Scheme Procedure} add-socket-membership @var{sock} @var{group} | |
| 581 | + | Make @var{sock} a member of @var{group}, an @code{RTNLGRP_} constant, | |
| 582 | + | meaning that it will be subscribed to events of that group. | |
| 558 | 583 | ||
| 559 | - | @item @code{brd} | |
| 560 | - | Broadcast (ethernet) address of the link, as a string. | |
| 584 | + | For example, here is how you could create a netlink socket and subscribe | |
| 585 | + | it to the ``link'' group so that it receives notifications for new and | |
| 586 | + | removed links: | |
| 561 | 587 | ||
| 562 | - | @end table | |
| 563 | - | @end deffn | |
| 588 | + | @lisp | |
| 589 | + | (let ((sock (connect-route))) | |
| 590 | + | (add-socket-membership sock RTNLGRP_LINK) | |
| 591 | + | @dots{}) | |
| 592 | + | @end lisp | |
| 564 | 593 | ||
| 565 | - | @deffn {Scheme Procedure} get-links | |
| 566 | - | Returns the list of existing links in the system, as a list of @code{<link>} | |
| 567 | - | objects. | |
| 594 | + | This procedure is implemented as a @code{setsockopt} call. | |
| 568 | 595 | @end deffn | |
| 569 | 596 | ||
| 570 | - | @deffn {Scheme Procedure} wait-for-link @var{name} [#:blocking? #t] | |
| 571 | - | Wait until a link called @var{name} (a string such as @code{"ens3"}) shows | |
| 572 | - | up. | |
| 573 | - | ||
| 574 | - | When @var{blocking?} is false, use a non-blocking socket and cooperate via | |
| 575 | - | @code{current-read-waiter}---useful when using Fibers. | |
| 597 | + | @deffn {Scheme Procedure} send-msg @var{msg} @var{sock} [#:@var{addr}] | |
| 598 | + | Send @var{msg} (it must be of type message, @xref{Netlink Headers}) to | |
| 599 | + | @var{addr} using @var{sock}. If not passed, @var{addr} is the address of | |
| 600 | + | the kernel. | |
| 576 | 601 | @end deffn | |
| 577 | 602 | ||
| 578 | - | @deffn {Sceme Procedure} print-link @var{link} | |
| 579 | - | Display @var{link} on the standard output, using a format similar to | |
| 580 | - | @command{ip link} from @code{iproute2}. | |
| 603 | + | @deffn {Scheme Procedure} receive-msg @var{sock} [#:@var{addr}] | |
| 604 | + | Receives a message from @var{sock} from @var{addr}. This procedure is blocking. | |
| 605 | + | If not passed, @var{addr} defaults to the address of the kernel. This | |
| 606 | + | procedure returns the message as a bytevector, that you can deserialize with | |
| 607 | + | @code{deserialize} (@xref{Data Types}) | |
| 581 | 608 | @end deffn | |
| 582 | 609 | ||
| 583 | - | @deffn {Scheme Procedure} link-set @var{device} [#:up @code{#f}] @ | |
| 584 | - | [#:down @code{#f}] [#:type @code{#f}] [#:arp-on @code{#f}] @ | |
| 585 | - | [#:arp-off @code{#f}] [#:dynamic-on @code{#f}] [#:dynamic-off @code{#f}] @ | |
| 586 | - | [#:multicast-on @code{#f}] [#:multicast-off @code{#f}] @ | |
| 587 | - | [#:allmulticast-on @code{#f}] [#:allmulticast-off @code{#f}] @ | |
| 588 | - | [#:promisc-on @code{#f}] [#:promisc-off @code{#f}] [#:trailers-on @code{#f}] @ | |
| 589 | - | [#:trailers-off @code{#f}] [#:carrier-on @code{#f}] [#:carrier-off @code{#f}] @ | |
| 590 | - | [#:txqueuelen @code{#f}] [#:name @code{#f}] [#:address @code{#f}] @ | |
| 591 | - | [#:broadcast @code{#f}] [#:mtu @code{#f}] [#:netns @code{#f}] | |
| 592 | - | Modify an existing link and set its flags and attributes to the ones specified | |
| 593 | - | by the various keywords. When a keyword is omited, the corresponding attribute | |
| 594 | - | is not changed. | |
| 595 | - | ||
| 596 | - | @var{device} can be a device index (as a number) or a device name (as a string). | |
| 597 | - | ||
| 598 | - | Do not set @code{#:up} and @code{#:down} at the same time. Do not set | |
| 599 | - | @code{*-on} and @code{*-off} at the same time. | |
| 600 | - | @end deffn | |
| 610 | + | @deffn {Scheme Procedure} receive-and-decode-msg @var{sock} @var{decoder} @ | |
| 611 | + | [#:@var{addr}] | |
| 612 | + | Receives one or more messages from @var{sock} from @var{addr}. this procedure | |
| 613 | + | is blocking. If not passed, @var{addr} defaults to the address of the kernel. | |
| 614 | + | This procedure returns a list of messages that were decoded using @var{decoder}. | |
| 601 | 615 | ||
| 602 | - | @deffn {Scheme Procedure} link-show [#:device @code{#f}] [#:group @code{#f}] @ | |
| 603 | - | [#:up @code{#f}] [#:master @code{#f}] [#:vrf @code{#f}] [#:type @code{#f}] | |
| 604 | - | Print the set of devices on standard output. Setting any of the keyword to a | |
| 605 | - | non-false value will filter the results to only show results that match the | |
| 606 | - | corresponding value. You may set more than one keyword. | |
| 616 | + | When the answer has the @code{NLM_F_MULTI} flag, this procedure decodes the next | |
| 617 | + | message, until it receives a @code{NLMSG_DONE} message. It returns the list | |
| 618 | + | of every netlink messages it received, including the @code{NLMSG_DONE}. | |
| 607 | 619 | @end deffn | |
| 608 | 620 | ||
| 609 | - | @deffn {Scheme Procedure} link-add @var{name} @var{type} [#:type-args @code{'()}] | |
| 610 | - | Add a new link with given name and type. Additional arguments can be passed to | |
| 611 | - | control the state of the link at creation. @var{type-args} is an association | |
| 612 | - | list containing additional values for the given type. | |
| 613 | - | ||
| 614 | - | When @var{type} is @code{"vlan"}, @var{type-args} can contain a number associated | |
| 615 | - | with @code{'id}: the VLAN id to be created and a link name associated with | |
| 616 | - | @code{"link"}: the name of the link on which the vlan is created. | |
| 621 | + | @node Netlink API | |
| 622 | + | @section Netlink API | |
| 617 | 623 | ||
| 618 | - | The following is an example in which we create a new vlan link: | |
| 619 | - | @example | |
| 620 | - | ;; same as "ip l add link eth0 name eth0.7 type vlan id 7" | |
| 621 | - | (link-add "eth0.7" "vlan" #:type-args '((id . 7) (link . "eth0"))) | |
| 622 | - | @end example | |
| 624 | + | This section introduces the data structures used for all the netlink protocols. | |
| 625 | + | First, we introduce the structure of a netlink message, then we present the | |
| 626 | + | standard types of netlink messages, that can be used with every protocol. | |
| 623 | 627 | ||
| 624 | - | When @var{type} is @code{"veth"}, @var{type-args} can contain a string associated | |
| 625 | - | with @code{'peer}: the name of the peer. | |
| 628 | + | @node Netlink Headers | |
| 629 | + | @subsection Netlink Headers | |
| 626 | 630 | ||
| 627 | - | The following is an example in which we create a new veth (virtual ethernet) | |
| 628 | - | pair and give them a name: | |
| 629 | - | @example | |
| 630 | - | ;; same as "ip l add v0p0 type veth peer v0p1" | |
| 631 | - | (link-add "v0p0" "veth" #:type-args '((peer . "v0p1"))) | |
| 632 | - | @end example | |
| 633 | - | @end deffn | |
| 631 | + | The @code{(netlink message)} module defines the message structure that contains | |
| 632 | + | a netlink message. It is composed of a header and a body, and is the data | |
| 633 | + | structure to pass to @code{send-msg} (@xref{Netlink Connections}). | |
| 634 | 634 | ||
| 635 | - | @deffn {Scheme Procedure} link-del @var{device} | |
| 636 | - | Delete a link. @var{device} can contain the name of the link, as a string, | |
| 637 | - | or its index, as a number. | |
| 638 | - | @end deffn | |
| 635 | + | This module defines the following data structure: | |
| 639 | 636 | ||
| 640 | - | @node Addr | |
| 641 | - | @section Addr | |
| 637 | + | @deffn {Datatype} message | |
| 638 | + | @table @asis | |
| 639 | + | @item @code{type} | |
| 640 | + | The type of data in the body of the message. For instance, @code{RTM_GETLINK}. | |
| 642 | 641 | ||
| 643 | - | The @code{(ip addr)} module introduces procedures to access and modify the | |
| 644 | - | network addresses on your machine. They are equivalent to the @command{ip addr} | |
| 645 | - | family of commands, from @code{iproute2}. | |
| 642 | + | @item @code{flags} | |
| 643 | + | The set of flags that are set in the header. For instance, | |
| 644 | + | @code{(logior NLM_F_REQUEST NLM_F_DUMP)}. | |
| 646 | 645 | ||
| 647 | - | @deffn {Scheme Procedure} addr-add @var{device} @var{cidr} [@var{#:ipv6?} #f] @ | |
| 648 | - | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ | |
| 649 | - | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ | |
| 650 | - | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ | |
| 651 | - | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ | |
| 652 | - | [@var{#:autojoin?} #f] | |
| 653 | - | Add the address given in @var{cidr} to @var{device}. @var{device} can | |
| 654 | - | contain the name of the link, as a string, or its index, as a number. | |
| 646 | + | @item @code{seq} | |
| 647 | + | The sequence number of the message. If this message is an answer to a request, | |
| 648 | + | it must keep the same sequence number. Otherwise, you must generate a new and | |
| 649 | + | unique sequence number, to track the answers. | |
| 655 | 650 | ||
| 656 | - | @var{cidr} must be a string containing the address and prefix length, in | |
| 657 | - | CIDR notation (@code{addr/prefix}). | |
| 651 | + | @item @code{pid} | |
| 652 | + | The PID of the receiving process, or 0 for the kernel. | |
| 658 | 653 | ||
| 659 | - | @example | |
| 660 | - | (addr-add "enp1s0" "192.0.2.15/24") | |
| 661 | - | @end example | |
| 654 | + | @item @code{data} | |
| 655 | + | The actual body, as an @code{nl-data} structure. | |
| 662 | 656 | ||
| 663 | - | If you wish to add an IPv6 address instead, set @code{#:ipv6} to @code{#t}, | |
| 664 | - | as in the following example. | |
| 657 | + | @end table | |
| 658 | + | @end deffn | |
| 665 | 659 | ||
| 666 | - | @example | |
| 667 | - | (addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) | |
| 668 | - | @end example | |
| 660 | + | @node Standard Message Types | |
| 661 | + | @subsection Standard Message Types | |
| 669 | 662 | ||
| 670 | - | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 671 | - | flag will result in a @code{Bad address} exception from inet-pton. | |
| 663 | + | The @code{(netlink standard)} module defines the set of standard message types | |
| 664 | + | and their data type. | |
| 672 | 665 | ||
| 673 | - | Additional flags are available; they follow the same semantics as Iproute2. | |
| 674 | - | For pointopoint interfaces, you can specify the address of the remote endpoint | |
| 675 | - | with @var{#:peer}. You can specify a broadcast or anycast address with | |
| 676 | - | @var{#:broadcast} and @var{#:anycast}. All three require an IP address passed | |
| 677 | - | as a string when specified. | |
| 666 | + | @deffn {Datatype} error-message | |
| 667 | + | @table @asis | |
| 668 | + | @item @code{err} | |
| 669 | + | The error code, as a negative number. | |
| 678 | 670 | ||
| 679 | - | You can specify a label for the address with @var{#:label}. The parameter must | |
| 680 | - | be a string that is either the name of the device, or starts with the name of | |
| 681 | - | the device, followed by a colon and must contain at most 15 characters. | |
| 671 | + | @item @code{hdr} | |
| 672 | + | The message on which this error applies. | |
| 682 | 673 | ||
| 683 | - | You can specify a scope with @var{#:scope}, whose value is either @code{'global}, | |
| 684 | - | @code{'link}, @code{'host} or a numeric value. | |
| 674 | + | @end table | |
| 685 | 675 | ||
| 686 | - | You can specify the priority of the prefix route associated with this address | |
| 687 | - | using @code{#:metric}, a number. | |
| 676 | + | @deffn {Scheme Variable} no-data | |
| 677 | + | This variable defines the absence of data. This is useful when a structure | |
| 678 | + | is expecting a body part, but the protocol specifically defines that it should | |
| 679 | + | not take any data in some cases. For instance, a @code{NLMSG_NOOP} message | |
| 680 | + | takes no data, so the @code{data} field of the message will contain this | |
| 681 | + | @code{no-data} value. | |
| 682 | + | @end deffn | |
| 688 | 683 | ||
| 689 | - | Finally, this procedures accepts address configuration flags, whose values are | |
| 690 | - | booleans. They are unset by default. Some flags only work for IPv6 addresses, | |
| 691 | - | those are @var{#:home?} to designate this address as the ``home address'', | |
| 692 | - | @var{#:mngtmpaddr?}, @var{#:nodad?} and @var{#:optimistic?}. The flags | |
| 693 | - | @var{#:noprefixroute?} and @var{#:autojoin?} can be set for IPv4 and IPv6 | |
| 694 | - | addresses. | |
| 695 | 684 | @end deffn | |
| 696 | 685 | ||
| 697 | - | @deffn {Scheme Procedure} addr-del @var{device} @var{cidr} [@var{#:ipv6?} #f] @ | |
| 698 | - | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ | |
| 699 | - | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ | |
| 700 | - | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ | |
| 701 | - | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ | |
| 702 | - | [@var{#:autojoin?} #f] | |
| 703 | - | Delete the address given in @var{cidr} from @var{device}. @var{device} can | |
| 704 | - | contain the name of the link, as a string, or its index, as a number. | |
| 686 | + | @node Rtnetlink API | |
| 687 | + | @section Rtnetlink API | |
| 688 | + | @cindex rtnetlink | |
| 689 | + | @cindex ROUTE_NETLINK | |
| 705 | 690 | ||
| 706 | - | @var{cidr} must be a string containing the address and prefix length, in | |
| 707 | - | CIDR notation (@code{addr/prefix}). | |
| 691 | + | This section describes the support for rtnetlink in guile-netlink. Rtnetlink | |
| 692 | + | is the protocol responsible for everything related to network routing. It | |
| 693 | + | allows you to manage links, addresses, routing tables, neighbor chaces, | |
| 694 | + | routing rules, queueing disciplines, traffic classes, traffic filters and | |
| 695 | + | more. | |
| 708 | 696 | ||
| 709 | - | @example | |
| 710 | - | (addr-del "enp1s0" "192.0.2.15/24") | |
| 711 | - | @end example | |
| 697 | + | @node Routing Attributes | |
| 698 | + | @subsection Routing Attributes | |
| 712 | 699 | ||
| 713 | - | If you wish to remove an IPv6 address instead, set @code{#:ipv6} to @code{#t}, | |
| 714 | - | as in the following example. | |
| 700 | + | The @code{(netlink route attrs)} module defines the following data types: | |
| 715 | 701 | ||
| 716 | - | @example | |
| 717 | - | (addr-del "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) | |
| 718 | - | @end example | |
| 702 | + | @deffn {Datatype} route-attr | |
| 703 | + | This defines a header structure for the attribute, as well as its body. | |
| 719 | 704 | ||
| 720 | - | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 721 | - | flag will result in a @code{Bad address} exception from inet-pton. | |
| 705 | + | @table @asis | |
| 706 | + | @item @code{type} | |
| 707 | + | This is the type of the attribute, for instance @code{IFLA_ADDRESS}. | |
| 722 | 708 | ||
| 723 | - | Additional flags are available, see the description in @code{addr-add} for more | |
| 724 | - | details. | |
| 709 | + | @item @code{data} | |
| 710 | + | This is the body of the attribute, ie.@: its value. | |
| 711 | + | @end table | |
| 725 | 712 | @end deffn | |
| 726 | 713 | ||
| 727 | - | @deffn {Scheme Procedure} addr-show [@var{device}] | |
| 728 | - | Print the list of addresses for each device on standard output. Setting | |
| 729 | - | @code{device} to a link name or link identifier will restrict the output | |
| 730 | - | to addresses of that device. | |
| 731 | - | @end deffn | |
| 714 | + | The module also defines additional data types that are not represented as | |
| 715 | + | a record, but by a simple type. For each of the following types, there is | |
| 716 | + | a @code{make-*-route-attr} procedure to produce a @code{nl-data} value | |
| 717 | + | for this type. There is also @code{deserialize-route-attr-data-*} procedure | |
| 718 | + | to deserialize a value of this type. | |
| 732 | 719 | ||
| 733 | - | @node Route | |
| 734 | - | @section Route | |
| 720 | + | @table @asis | |
| 721 | + | @item @code{u8} | |
| 722 | + | A one-byte unsigned integer | |
| 723 | + | @item @code{u16} | |
| 724 | + | A two-bytes unsigned integer | |
| 725 | + | @item @code{u32} | |
| 726 | + | A four-bytes unsigned integer | |
| 727 | + | @item @code{s32} | |
| 728 | + | A four-bytes signed integer | |
| 729 | + | @item @code{string} | |
| 730 | + | A string | |
| 731 | + | @item @code{ethernet} | |
| 732 | + | An ethernet address. Its value is a string that represents that address, | |
| 733 | + | for instnace @code{"01:23:45:67:89:ab"} | |
| 734 | + | @item @code{ipv4} | |
| 735 | + | An IPv4 address. Its value is a string that represents that address, | |
| 736 | + | for instnace @code{"192.0.2.152"} | |
| 737 | + | @item @code{ipv6} | |
| 738 | + | An IPv6 address. Its value is a string that represents that address, | |
| 739 | + | for instnace @code{"2001:db8::0123:4567:89ab:cdef"} | |
| 740 | + | @item @code{bv} | |
| 741 | + | A bytevector. This is used by default when the type is not supported. | |
| 742 | + | @end table | |
| 735 | 743 | ||
| 736 | - | The @code{(ip route)} module introduces procedures to access and modify the | |
| 737 | - | network routes on your machine. They are equivalent to the @command{ip route} | |
| 738 | - | family of commands, from @code{iproute2}. | |
| 744 | + | @node Link Messages | |
| 745 | + | @subsection Link Messages | |
| 739 | 746 | ||
| 740 | - | @deffn {Scheme Procedure} route-add @var{dest} [@var{#:ipv6?} #f] @ | |
| 741 | - | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ | |
| 742 | - | [@var{#:scope} RT_SCOPE_LINK] [@var{#:type} RTN_UNICAST] @ | |
| 743 | - | [@var{#:priority} #f] [@var{#:src} #f] [@var{#:via} #f] | |
| 744 | - | Add the route described by the argmuents. @var{dest} is the destination network, | |
| 745 | - | in cidr notation (@code{addr/prefix}) or the string @code{"default"}. | |
| 747 | + | The @code{(netlink route link)} package defines the following data type: | |
| 746 | 748 | ||
| 747 | - | @var{#:device} is the name or index of a network link. @var{#:table} is the | |
| 748 | - | index of a routing table, one of @code{RT_TABLE_COMPAT}, @code{RT_TABLE_DEFAULT}, | |
| 749 | - | @code{RT_TABLE_MAIN} or @code{RT_TABLE_LOCAL}, as defined in | |
| 750 | - | @code{(netlink constant)}. | |
| 749 | + | @deffn {Datatype} link-message | |
| 750 | + | This datatype represents a link message with its routing attributes. This type | |
| 751 | + | of message is expected when using the @var{RTM_*LINK} message types. | |
| 751 | 752 | ||
| 752 | - | If it is set, @var{#:protocol} must be the routing protocol, @code{RTPROT_*}, | |
| 753 | - | as defined in @code{(netlink constant)}. | |
| 753 | + | @table @asis | |
| 754 | + | @item @code{family} | |
| 755 | + | The network family, defined as @code{AF_UNSPEC} in the rtnetlink documentation, | |
| 756 | + | although it holds different values in practice. | |
| 754 | 757 | ||
| 755 | - | @var{#:scope} must be the scope of the route, one of @code{RT_SCOPE_*}, as | |
| 756 | - | defined in @code{(netlink constant)}. | |
| 758 | + | @item @code{type} | |
| 759 | + | The device type. | |
| 757 | 760 | ||
| 758 | - | @var{#:type} must be the type of route, one of @code{RTN_*}, as defined in | |
| 759 | - | @code{(netlink constant)}. | |
| 761 | + | @item @code{index} | |
| 762 | + | The index of the device. This is used to select a specific device by its index, | |
| 763 | + | or 0 to not filter by device index. | |
| 760 | 764 | ||
| 761 | - | If set, @var{#:priority} is a number specifying the priority of the rule | |
| 762 | - | when the kernel is looking for a matching rule. This is also known as the | |
| 763 | - | metric of the route. | |
| 765 | + | @item @code{flags} | |
| 766 | + | The device flags. See @code{man 7 netdevices} for a list. | |
| 764 | 767 | ||
| 765 | - | If set, @var{#:src} is the source address in cidr notation, or as a single | |
| 766 | - | address. | |
| 768 | + | @item @code{attrs} | |
| 769 | + | A list of attributes. This field must contain a list of @code{nl-data} | |
| 770 | + | structures, not a structure by itself. | |
| 771 | + | @end table | |
| 772 | + | @end deffn | |
| 767 | 773 | ||
| 768 | - | If set, @var{#:via} is the gateway address. This is not in cidr notation, as | |
| 769 | - | the gateway is a single address, not a network. | |
| 774 | + | @node Address Messages | |
| 775 | + | @subsection Address Messages | |
| 770 | 776 | ||
| 771 | - | @example | |
| 772 | - | (route-add "default" #:device "enp1s0" #:via "192.0.2.1") | |
| 773 | - | (route-add "192.0.2.0/24" #:device "enp1s0" #:src "192.0.2.15") | |
| 774 | - | @end example | |
| 777 | + | The @code{(netlink route addr)} package defines the following data type: | |
| 775 | 778 | ||
| 776 | - | If you wish to add an IPv6 route instead, set @code{#:ipv6} to @code{#t}, | |
| 777 | - | as in the following example. | |
| 779 | + | @deffn {Datatype} addr-message | |
| 780 | + | This datatype represents an address message with its routing attributes. This | |
| 781 | + | type of message is expected when using the @var{RTM_*ADDR} message types. | |
| 778 | 782 | ||
| 779 | - | @example | |
| 780 | - | (addr-add "2001:db8::/64" #:device "enp1s0" #:src "2001:db8::1a4c" #:ipv6? #t) | |
| 781 | - | @end example | |
| 783 | + | @table @asis | |
| 784 | + | @item @code{family} | |
| 785 | + | The network family, either @code{AF_INET} for IPv4 addresses, or @code{AF_INET6} | |
| 786 | + | for IPv6 addresses. | |
| 782 | 787 | ||
| 783 | - | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} | |
| 784 | - | flag will result in a @code{Bad address} exception from inet-pton. | |
| 785 | - | @end deffn | |
| 788 | + | @item @code{prefix-len} | |
| 789 | + | The prefix length, i.e.@: the length of the prefix mask, in bits, if defined | |
| 790 | + | for the address family. | |
| 786 | 791 | ||
| 787 | - | @deffn {Scheme Procedure} route-del @var{dest} [@var{#:ipv6?} #f] @ | |
| 788 | - | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ | |
| 789 | - | [@var{#:scope} #f] [@var{#:type} #f] [@var{#:priority} #f] @ | |
| 790 | - | [@var{#:src} #f] [@var{#:via} #f] | |
| 791 | - | Delete the route given in arguments. The arguments follow the same structure | |
| 792 | - | as @code{route-add}. By specifying more arguments, you can narrow down the | |
| 793 | - | search for the rule to delete further. Each call will only remove one route, | |
| 794 | - | so being more precise ensures you target the rule you wish to delete. It | |
| 795 | - | is not clear which route is deleted if multiple routes match your query. | |
| 796 | - | @end deffn | |
| 792 | + | @item @code{flags} | |
| 793 | + | Address flags. This can be a flag word of @code{IFA_F_SECONDARY} for secondary | |
| 794 | + | address (old alias interface), @code{IFA_F_PERMANENT} for a permanent | |
| 795 | + | address set by the user and other undocumented flags. | |
| 797 | 796 | ||
| 798 | - | @deffn {Scheme Procedure} route-show [@var{#:table} RT_TABLE_MAIN] @ | |
| 799 | - | [@var{#:family} AF_UNSPEC] | |
| 800 | - | Print the list of routes on standard output. Note that, contrary to | |
| 801 | - | @command{ip route show}, we show both IPv4 and IPv6 routes. To narrow down the | |
| 802 | - | number of routes displayed, you can specify the family as in this example. | |
| 797 | + | @item @code{scope} | |
| 798 | + | The address scope. | |
| 803 | 799 | ||
| 804 | - | @example | |
| 805 | - | (route-show #:family AF_INET6) | |
| 806 | - | @end example | |
| 800 | + | @item @code{index} | |
| 801 | + | The index of the device this address is for. | |
| 802 | + | ||
| 803 | + | @item @code{attrs} | |
| 804 | + | A list of attributes. This field must contain a list of @code{nl-data} | |
| 805 | + | structures, not a structure by itself. | |
| 806 | + | @end table | |
| 807 | 807 | @end deffn | |
| 808 | 808 | ||
| 809 | 809 | @c ********************************************************************* | |