2 .\" Copyright (C) 2022 Alexander Chernikov <melifaro@FreeBSD.org>.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .Nm snl_read_message ,
38 .Nm snl_parse_header ,
40 .Nm snl_parse_attrs_raw ,
41 .Nm snl_attr_get_flag ,
43 .Nm snl_attr_get_uint16 ,
44 .Nm snl_attr_get_uint32 ,
45 .Nm snl_attr_get_string ,
46 .Nm snl_attr_get_stringn ,
47 .Nm snl_attr_get_nla ,
48 .Nm snl_field_get_uint8 ,
49 .Nm snl_field_get_uint16 ,
50 .Nm snl_field_get_uint32
51 .Nd "simple netlink library"
53 .In netlink/netlink_snl.h
54 .In netlink/netlink_snl_route.h
56 .Fn snl_init "struct snl_state *ss" "int netlink_family"
57 .Fn snl_free "struct snl_state *ss"
58 .Ft "struct nlmsghdr *"
59 .Fn snl_read_message "struct snl_state *ss"
61 .Fn snl_send "struct snl_state *ss" "void *data" "int sz"
63 .Fn snl_get_seq "struct snl_state *ss"
65 .Fn snl_allocz "struct snl_state *ss" "int len"
66 .Fn snl_clear_lb "struct snl_state *ss"
68 .Fn snl_parse_nlmsg "struct snl_state *ss" "struct nlmsghdr *hdr" "const struct snl_hdr_parser *ps" "void *target"
70 .Fn snl_parse_header "struct snl_state *ss" "void *hdr" "int len" "const struct snl_hdr_parser *ps" "int pslen" "void *target"
72 .Fn snl_parse_attrs "struct snl_state *ss" "struct nlmsghdr *hdr" "int hdrlen" "const struct snl_attr_parser *ps" "int pslen" "void *target"
74 .Fn snl_parse_attrs_raw "struct snl_state *ss" "struct nlattr *nla_head" "int len" "const struct snl_attr_parser *ps" "int pslen" "void *target"
76 .Fn snl_attr_get_flag "struct snl_state *ss" "struct nlattr *nla" "void *target"
78 .Fn snl_attr_get_uint16 "struct snl_state *ss" "struct nlattr *nla" "void *target"
80 .Fn snl_attr_get_uint32 "struct snl_state *ss" "struct nlattr *nla" "void *target"
82 .Fn snl_attr_get_string "struct snl_state *ss" "struct nlattr *nla" "void *target"
84 .Fn snl_attr_get_stringn "struct snl_state *ss" "struct nlattr *nla" "void *target"
86 .Fn snl_attr_get_nla "struct snl_state *ss" "struct nlattr *nla" "void *target"
88 .Fn snl_attr_get_ip "struct snl_state *ss" "struct nlattr *nla" "void *target"
90 .Fn snl_attr_get_ipvia "struct snl_state *ss" "struct nlattr *nla" "void *target"
94 library provides an easy way of sending and receiving Netlink messages,
95 taking care of serialisation and deserialisation.
101 and the desired Netlink family to initialise the library instance.
102 To free the library instance, call
105 The library functions are NOT multithread-safe.
106 If multithreading is desired, consider initializing an instance
108 .Ss MEMORY ALLOCATION
109 The library uses pre-allocated extendable memory buffers to handle message parsing.
110 The typical usage pattern is to allocate the necessary data structures during the
111 message parsing or writing process via
113 and free all allocated data at once using
115 after handling the message.
116 .Ss COMPOSING AND SENDING MESSAGES
117 The library does not currently offer any wrappers for writing netlink messages.
118 Simple request messages can be composed by filling in all needed fields directly.
119 Example for constructing an interface dump request:
123 struct ifinfomsg ifmsg;
125 .hdr.nlmsg_type = RTM_GETLINK,
126 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
127 .hdr.nlmsg_seq = snl_get_seq(ss),
129 msg.hdr.nlmsg_len = sizeof(msg);
132 can be used to generate a unique message number.
133 To send the resulting message,
136 .Ss RECEIVING AND PARSING MESSAGES
137 To receive a message, use
138 .Fn snl_read_message .
139 Currently, this call is blocking.
141 The library provides an easy way to convert the message to the pre-defined C
143 For each message type, one needs to define rules, converting the protocol header
144 fields and the desired attributes to the specified structure.
145 It can be accomplished by using message parsers.
146 Each message parser consists of an array of attribute getters and an array of
147 header field getters.
148 The former array needs to be sorted by the attribute type.
150 .Fn SNL_VERIFY_PARSERS
151 macro to check if the order is correct.
152 .Fn SNL_DECLARE_PARSER "parser_name" "family header type" "struct snl_field_parser[]" "struct snl_attr_parser[]"
153 can be used to create a new parser.
154 .Fn SNL_DECLARE_ATTR_PARSER "parser_name" "struct snl_field_parser[]"
155 can be used to create an attribute-only message parser.
157 Each attribute getter needs to be embedded in the following structure:
159 typedef bool snl_parse_attr_f(struct snl_state *ss, struct nlattr *attr, const void *arg, void *target);
160 struct snl_attr_parser {
161 uint16_t type; /* Attribute type */
162 uint16_t off; /* field offset in the target structure */
163 snl_parse_attr_f *cb; /* getter function to call */
164 const void *arg; /* getter function custom argument */
167 The generic attribute getter has the following signature:
169 .Fn snl_attr_get_<type> "struct snl_state *ss" "struct nlattr *nla" "const void *arg" "void *target" .
170 nla contains the pointer of the attribute to use as the datasource.
171 The target field is the pointer to the field in the target structure.
172 It is up to the getter to know the type of the target field.
173 The getter must check the input attribute and return
174 false if the attribute is not formed correctly.
175 Otherwise, the getter fetches the attribute value and stores it in the target,
177 It is possible to use
179 to create the desired data structure .
180 A number of predefined getters for the common data types exist.
181 .Fn snl_attr_get_flag
182 converts a flag-type attribute to an uint8_t value of 1 or 0, depending on the
184 .Fn snl_attr_get_uint16
185 stores a uint16_t type attribute into the uint16_t target field.
186 .Fn snl_attr_get_uint32
187 stores a uint32_t type attribute into the uint32_t target field.
190 .Fn snl_attr_get_ipvia
191 stores a pointer to the sockaddr structure with the IPv4/IPv6 address contained
193 Sockaddr is allocated using
195 .Fn snl_attr_get_string
196 stores a pointer to the NULL-terminated string.
197 The string itself is allocated using
200 stores a pointer to the specified attribute.
201 .Fn snl_attr_get_stringn
202 stores a pointer to the non-NULL-terminated string.
204 Similarly, each family header getter needs to be embedded in the following structure:
206 typedef void snl_parse_field_f(struct snl_state *ss, void *hdr, void *target);
207 struct snl_field_parser {
208 uint16_t off_in; /* field offset in the input structure */
209 uint16_t off_out;/* field offset in the target structure */
210 snl_parse_field_f *cb; /* getter function to call */
213 The generic field getter has the following signature:
215 snl_field_get_<type> "struct snl_state *ss" "void *src" "void *target" .
216 A number of pre-defined getters for the common data types exist.
217 .Fn "snl_field_get_uint8"
218 fetches an uint8_t value and stores it in the target.
219 .Fn "snl_field_get_uint16"
220 fetches an uint8_t value and stores it in the target.
221 .Fn "snl_field_get_uint32"
222 fetches an uint32_t value and stores it in the target.
224 The following example demonstrates how to list all system interfaces
229 #include <netlink/netlink.h>
230 #include <netlink/netlink_route.h>
231 #include "netlink/netlink_snl.h"
232 #include "netlink/netlink_snl_route.h"
234 struct nl_parsed_link {
240 #define _IN(_field) offsetof(struct ifinfomsg, _field)
241 #define _OUT(_field) offsetof(struct nl_parsed_link, _field)
242 static const struct snl_attr_parser ap_link[] = {
243 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string },
244 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
246 static const struct snl_field_parser fp_link[] = {
247 {.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
251 SNL_DECLARE_PARSER(link_parser, struct ifinfomsg, fp_link, ap_link);
255 main(int ac, char *argv[])
259 if (!snl_init(&ss, NETLINK_ROUTE))
264 struct ifinfomsg ifmsg;
266 .hdr.nlmsg_type = RTM_GETLINK,
267 .hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
268 .hdr.nlmsg_seq = snl_get_seq(&ss),
270 msg.hdr.nlmsg_len = sizeof(msg);
272 if (!snl_send(&ss, &msg, sizeof(msg))) {
277 struct nlmsghdr *hdr;
278 while ((hdr = snl_read_message(&ss)) != NULL && hdr->nlmsg_type != NLMSG_DONE) {
279 if (hdr->nlmsg_seq != msg.hdr.nlmsg_seq)
282 struct nl_parsed_link link = {};
283 if (!snl_parse_nlmsg(&ss, hdr, &link_parser, &link))
285 printf("Link#%u %s mtu %u\n", link.ifi_index, link.ifla_ifname, link.ifla_mtu);
302 This library was implemented by
303 .An Alexander Chernikov Aq Mt melifaro@FreeBSD.org .