guile-netlink.texi
1 | \input texinfo |
2 | @setfilename guile-netlink.info |
3 | @documentencoding UTF-8 |
4 | @settitle guile-netlink |
5 | |
6 | @include version.texi |
7 | |
8 | @copying |
9 | Copyright @copyright{} 2020 Julien Lepiller |
10 | |
11 | @quotation |
12 | Permission is granted to copy, distribute and/or modify this document |
13 | under the terms of the GNU Free Documentation License, Version 1.3 or |
14 | any later version published by the Free Software Foundation; with no |
15 | Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A |
16 | copy of the license is included in the section entitled ``GNU Free |
17 | Documentation License''. |
18 | @end quotation |
19 | @end copying |
20 | |
21 | @titlepage |
22 | @end titlepage |
23 | |
24 | @contents |
25 | |
26 | @node Top |
27 | @top guile-netlink |
28 | |
29 | This document describes guile-netlink version @value{VERSION}, a guile |
30 | implementation of the netlink protocol. |
31 | |
32 | @menu |
33 | * Introduction:: What is netlink? |
34 | * IP Library:: High-level functions for network devices. |
35 | * API Reference:: Description of the library interface. |
36 | |
37 | * Concept Index:: Concepts. |
38 | * Programming Index:: Data types, procedures, and variables. |
39 | |
40 | @detailmenu |
41 | --- The Detailed Node Listing --- |
42 | |
43 | IP Library |
44 | |
45 | * Link:: Actions on network links. |
46 | * Addr:: Actions on network addresses. |
47 | * Route:: Actions on network routes. |
48 | |
49 | API Reference |
50 | |
51 | * Common API:: Common functions and data types for defining netlink |
52 | protocols. |
53 | * Netlink API:: Common structures and data types for every protocols. |
54 | * Rtnetlink API:: The ROUTE_NETLINK protocol. |
55 | |
56 | @end detailmenu |
57 | @end menu |
58 | |
59 | @node Introduction |
60 | @chapter Introduction |
61 | |
62 | Netlink is an inter-process communication protocol that can be used for |
63 | communication between processes, or with the kernel. It is implemented by |
64 | Linux. |
65 | |
66 | Many protocols exist on top of Netlink. The most famous are used to configure |
67 | network-related functions in the kernel, such as firewall, route table or |
68 | IP addresses of interfaces. |
69 | |
70 | This library implements the low-level bits of the code by providing data |
71 | structures that are close to their C counterpart, and basic procedures to |
72 | initiate communication. |
73 | |
74 | @node IP Library |
75 | @chapter IP Library |
76 | |
77 | This library comes with higher-level procedures that let you access and modify |
78 | the state of network on your computer. |
79 | |
80 | @node Link |
81 | @section Link |
82 | |
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 | |
87 | @deffn {Datatype} <link> |
88 | |
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 |
97 | |
98 | @table @asis |
99 | @item @code{name} |
100 | Name of the link, such as @code{"enp1s0"}. |
101 | |
102 | @item @code{id} |
103 | Index of the link, a unique number used to identify the link. |
104 | |
105 | @item @code{type} |
106 | Type of the link, as an integer. |
107 | |
108 | @item @code{flags} |
109 | Flags associated with the device, as a list of symbols, such as |
110 | @code{'(UP LOOPBACK)}. |
111 | |
112 | @item @code{mtu} |
113 | MTU of the link, as an integer. |
114 | |
115 | @item @code{qdisc} |
116 | Queuing discipline of the link, as a string, such as @code{"noqueue"}. |
117 | |
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}. |
121 | |
122 | @item @code{mode} |
123 | Mode of the link. 0 means @code{DORMANT}, 1 means @code{DEFAULT}. |
124 | |
125 | @item @code{group} |
126 | Identifier of the group it belongs to. 0 for @code{default}. |
127 | |
128 | @item @code{qlen} |
129 | Size of the queue. |
130 | |
131 | @item @code{addr} |
132 | Ethernet address of the link, as a string. |
133 | |
134 | @item @code{brd} |
135 | Broadcast (ethernet) address of the link, as a string. |
136 | |
137 | @end table |
138 | @end deffn |
139 | |
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 |
144 | |
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. |
148 | |
149 | When @var{blocking?} is false, use a non-blocking socket and cooperate via |
150 | @code{current-read-waiter}---useful when using Fibers. |
151 | @end deffn |
152 | |
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}. |
156 | @end deffn |
157 | |
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. |
170 | |
171 | @var{device} can be a device index (as a number) or a device name (as a string). |
172 | |
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. |
175 | @end deffn |
176 | |
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. |
182 | @end deffn |
183 | |
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. |
188 | |
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. |
192 | |
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 |
198 | |
199 | When @var{type} is @code{"veth"}, @var{type-args} can contain a string associated |
200 | with @code{'peer}: the name of the peer. |
201 | |
202 | The following is an example in which we create a new veth (virtual ethernet) |
203 | pair and give them a name: |
204 | @example |
205 | ;; same as "ip l add v0p0 type veth peer v0p1" |
206 | (link-add "v0p0" "veth" #:type-args '((peer . "v0p1"))) |
207 | @end example |
208 | @end deffn |
209 | |
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. |
213 | @end deffn |
214 | |
215 | @node Addr |
216 | @section Addr |
217 | |
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}. |
221 | |
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. |
230 | |
231 | @var{cidr} must be a string containing the address and prefix length, in |
232 | CIDR notation (@code{addr/prefix}). |
233 | |
234 | @example |
235 | (addr-add "enp1s0" "192.0.2.15/24") |
236 | @end example |
237 | |
238 | If you wish to add an IPv6 address instead, set @code{#:ipv6} to @code{#t}, |
239 | as in the following example. |
240 | |
241 | @example |
242 | (addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) |
243 | @end example |
244 | |
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. |
247 | |
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. |
253 | |
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. |
257 | |
258 | You can specify a scope with @var{#:scope}, whose value is either @code{'global}, |
259 | @code{'link}, @code{'host} or a numeric value. |
260 | |
261 | You can specify the priority of the prefix route associated with this address |
262 | using @code{#:metric}, a number. |
263 | |
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. |
270 | @end deffn |
271 | |
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. |
280 | |
281 | @var{cidr} must be a string containing the address and prefix length, in |
282 | CIDR notation (@code{addr/prefix}). |
283 | |
284 | @example |
285 | (addr-del "enp1s0" "192.0.2.15/24") |
286 | @end example |
287 | |
288 | If you wish to remove an IPv6 address instead, set @code{#:ipv6} to @code{#t}, |
289 | as in the following example. |
290 | |
291 | @example |
292 | (addr-del "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) |
293 | @end example |
294 | |
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. |
297 | |
298 | Additional flags are available, see the description in @code{addr-add} for more |
299 | details. |
300 | @end deffn |
301 | |
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 |
307 | |
308 | @node Route |
309 | @section Route |
310 | |
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}. |
314 | |
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"}. |
321 | |
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)}. |
326 | |
327 | If it is set, @var{#:protocol} must be the routing protocol, @code{RTPROT_*}, |
328 | as defined in @code{(netlink constant)}. |
329 | |
330 | @var{#:scope} must be the scope of the route, one of @code{RT_SCOPE_*}, as |
331 | defined in @code{(netlink constant)}. |
332 | |
333 | @var{#:type} must be the type of route, one of @code{RTN_*}, as defined in |
334 | @code{(netlink constant)}. |
335 | |
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. |
339 | |
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. |
360 | @end deffn |
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. |
371 | @end deffn |
372 | |
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. |
378 | |
379 | @example |
380 | (route-show #:family AF_INET6) |
381 | @end example |
382 | @end deffn |
383 | |
384 | @node API Reference |
385 | @chapter API Reference |
386 | |
387 | @node Common API |
388 | @section Common API |
389 | |
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. |
393 | |
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}). |
402 | |
403 | @deffn {Datatype} nl-data |
404 | |
405 | @table @asis |
406 | @item @code{data} |
407 | The data that is held by this record. |
408 | |
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. |
412 | |
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. |
418 | |
419 | @end table |
420 | @end deffn |
421 | |
422 | The module also defines the following function, that takes a @code{nl-data} |
423 | structure and provides its serialization in a bytevector: |
424 | |
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. |
428 | |
429 | This function updates the bytevector and adds the serialization of @var{data} |
430 | into @var{bv} at @var{pos}. |
431 | @end deffn |
432 | |
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. |
438 | |
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: |
444 | |
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. |
450 | |
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. |
455 | @end deffn |
456 | |
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 |
461 | |
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 | |
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. |
475 | |
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 |
482 | |
483 | guile-netlink provides the following default decoder for the rtnetlink |
484 | protocol in @code{(netlink deserialize)}: |
485 | |
486 | @deffn {Scheme Variable} %default-route-decoder |
487 | Contains the default decoder for the NETLINK_ROUTE protocol. |
488 | @end deffn |
489 | |
490 | For convenience, guile-netlink defines the following structures that can be used |
491 | to create a custom decoder. |
492 | |
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 |
497 | |
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: |
504 | @end deffn |
505 | |
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 |
510 | |
511 | @node Constants |
512 | @subsection Constants |
513 | |
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. |
518 | |
519 | The module also defines the following macro: |
520 | |
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. |
525 | |
526 | The macro also publicly defines variables whose names are in @var{name-spec} |
527 | to an integer. |
528 | |
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: |
533 | |
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 |
541 | |
542 | @node Netlink Connections |
543 | @subsection Netlink Connections |
544 | |
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. |
548 | |
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 |
555 | |
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}. |
559 | |
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)}). |
563 | |
564 | @var{addr} is a bytevector, as returned by @code{get-addr}. |
565 | |
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 |
569 | |
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. |
574 | |
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 |
578 | |
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. |
583 | |
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: |
587 | |
588 | @lisp |
589 | (let ((sock (connect-route))) |
590 | (add-socket-membership sock RTNLGRP_LINK) |
591 | @dots{}) |
592 | @end lisp |
593 | |
594 | This procedure is implemented as a @code{setsockopt} call. |
595 | @end deffn |
596 | |
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. |
601 | @end deffn |
602 | |
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}) |
608 | @end deffn |
609 | |
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}. |
615 | |
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}. |
619 | @end deffn |
620 | |
621 | @node Netlink API |
622 | @section Netlink API |
623 | |
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. |
627 | |
628 | @node Netlink Headers |
629 | @subsection Netlink Headers |
630 | |
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 | |
635 | This module defines the following data structure: |
636 | |
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}. |
641 | |
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)}. |
645 | |
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. |
650 | |
651 | @item @code{pid} |
652 | The PID of the receiving process, or 0 for the kernel. |
653 | |
654 | @item @code{data} |
655 | The actual body, as an @code{nl-data} structure. |
656 | |
657 | @end table |
658 | @end deffn |
659 | |
660 | @node Standard Message Types |
661 | @subsection Standard Message Types |
662 | |
663 | The @code{(netlink standard)} module defines the set of standard message types |
664 | and their data type. |
665 | |
666 | @deffn {Datatype} error-message |
667 | @table @asis |
668 | @item @code{err} |
669 | The error code, as a negative number. |
670 | |
671 | @item @code{hdr} |
672 | The message on which this error applies. |
673 | |
674 | @end table |
675 | |
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 |
683 | |
684 | @end deffn |
685 | |
686 | @node Rtnetlink API |
687 | @section Rtnetlink API |
688 | @cindex rtnetlink |
689 | @cindex ROUTE_NETLINK |
690 | |
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. |
696 | |
697 | @node Routing Attributes |
698 | @subsection Routing Attributes |
699 | |
700 | The @code{(netlink route attrs)} module defines the following data types: |
701 | |
702 | @deffn {Datatype} route-attr |
703 | This defines a header structure for the attribute, as well as its body. |
704 | |
705 | @table @asis |
706 | @item @code{type} |
707 | This is the type of the attribute, for instance @code{IFLA_ADDRESS}. |
708 | |
709 | @item @code{data} |
710 | This is the body of the attribute, ie.@: its value. |
711 | @end table |
712 | @end deffn |
713 | |
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. |
719 | |
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 |
743 | |
744 | @node Link Messages |
745 | @subsection Link Messages |
746 | |
747 | The @code{(netlink route link)} package defines the following data type: |
748 | |
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. |
752 | |
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. |
757 | |
758 | @item @code{type} |
759 | The device type. |
760 | |
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. |
764 | |
765 | @item @code{flags} |
766 | The device flags. See @code{man 7 netdevices} for a list. |
767 | |
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 |
773 | |
774 | @node Address Messages |
775 | @subsection Address Messages |
776 | |
777 | The @code{(netlink route addr)} package defines the following data type: |
778 | |
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. |
782 | |
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. |
787 | |
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. |
791 | |
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. |
796 | |
797 | @item @code{scope} |
798 | The address scope. |
799 | |
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 | @end deffn |
808 | |
809 | @c ********************************************************************* |
810 | @node Concept Index |
811 | @unnumbered Concept Index |
812 | @printindex cp |
813 | |
814 | @node Programming Index |
815 | @unnumbered Programming Index |
816 | @syncodeindex tp fn |
817 | @syncodeindex vr fn |
818 | @printindex fn |
819 | |
820 | @bye |
821 |