Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

docs: netlink: basic introduction to Netlink

Provide a bit of a brain dump of netlink related information
as documentation. Hopefully this will be useful to people
trying to navigate implementing YAML based parsing in languages
we won't be able to help with.

I started writing this doc while trying to figure out what
it'd take to widen the applicability of YAML to good old rtnl,
but the doc grew beyond that as it usually happens.

In all honesty a lot of this information is new to me as I usually
follow the "copy an existing example, drink to forget" process
of writing netlink user space, so reviews will be much appreciated.

Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Acked-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/20220819200221.422801-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+656
+1
Documentation/userspace-api/index.rst
··· 26 26 ioctl/index 27 27 iommu 28 28 media/index 29 + netlink/index 29 30 sysfs-platform_profile 30 31 vduse 31 32 futex2
+12
Documentation/userspace-api/netlink/index.rst
··· 1 + .. SPDX-License-Identifier: BSD-3-Clause 2 + 3 + ================ 4 + Netlink Handbook 5 + ================ 6 + 7 + Netlink documentation for users. 8 + 9 + .. toctree:: 10 + :maxdepth: 2 11 + 12 + intro
+643
Documentation/userspace-api/netlink/intro.rst
··· 1 + .. SPDX-License-Identifier: BSD-3-Clause 2 + 3 + ======================= 4 + Introduction to Netlink 5 + ======================= 6 + 7 + Netlink is often described as an ioctl() replacement. 8 + It aims to replace fixed-format C structures as supplied 9 + to ioctl() with a format which allows an easy way to add 10 + or extended the arguments. 11 + 12 + To achieve this Netlink uses a minimal fixed-format metadata header 13 + followed by multiple attributes in the TLV (type, length, value) format. 14 + 15 + Unfortunately the protocol has evolved over the years, in an organic 16 + and undocumented fashion, making it hard to coherently explain. 17 + To make the most practical sense this document starts by describing 18 + netlink as it is used today and dives into more "historical" uses 19 + in later sections. 20 + 21 + Opening a socket 22 + ================ 23 + 24 + Netlink communication happens over sockets, a socket needs to be 25 + opened first: 26 + 27 + .. code-block:: c 28 + 29 + fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); 30 + 31 + The use of sockets allows for a natural way of exchanging information 32 + in both directions (to and from the kernel). The operations are still 33 + performed synchronously when applications send() the request but 34 + a separate recv() system call is needed to read the reply. 35 + 36 + A very simplified flow of a Netlink "call" will therefore look 37 + something like: 38 + 39 + .. code-block:: c 40 + 41 + fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); 42 + 43 + /* format the request */ 44 + send(fd, &request, sizeof(request)); 45 + n = recv(fd, &response, RSP_BUFFER_SIZE); 46 + /* interpret the response */ 47 + 48 + Netlink also provides natural support for "dumping", i.e. communicating 49 + to user space all objects of a certain type (e.g. dumping all network 50 + interfaces). 51 + 52 + .. code-block:: c 53 + 54 + fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); 55 + 56 + /* format the dump request */ 57 + send(fd, &request, sizeof(request)); 58 + while (1) { 59 + n = recv(fd, &buffer, RSP_BUFFER_SIZE); 60 + /* one recv() call can read multiple messages, hence the loop below */ 61 + for (nl_msg in buffer) { 62 + if (nl_msg.nlmsg_type == NLMSG_DONE) 63 + goto dump_finished; 64 + /* process the object */ 65 + } 66 + } 67 + dump_finished: 68 + 69 + The first two arguments of the socket() call require little explanation - 70 + it is opening a Netlink socket, with all headers provided by the user 71 + (hence NETLINK, RAW). The last argument is the protocol within Netlink. 72 + This field used to identify the subsystem with which the socket will 73 + communicate. 74 + 75 + Classic vs Generic Netlink 76 + -------------------------- 77 + 78 + Initial implementation of Netlink depended on a static allocation 79 + of IDs to subsystems and provided little supporting infrastructure. 80 + Let us refer to those protocols collectively as **Classic Netlink**. 81 + The list of them is defined on top of the ``include/uapi/linux/netlink.h`` 82 + file, they include among others - general networking (NETLINK_ROUTE), 83 + iSCSI (NETLINK_ISCSI), and audit (NETLINK_AUDIT). 84 + 85 + **Generic Netlink** (introduced in 2005) allows for dynamic registration of 86 + subsystems (and subsystem ID allocation), introspection and simplifies 87 + implementing the kernel side of the interface. 88 + 89 + The following section describes how to use Generic Netlink, as the 90 + number of subsystems using Generic Netlink outnumbers the older 91 + protocols by an order of magnitude. There are also no plans for adding 92 + more Classic Netlink protocols to the kernel. 93 + Basic information on how communicating with core networking parts of 94 + the Linux kernel (or another of the 20 subsystems using Classic 95 + Netlink) differs from Generic Netlink is provided later in this document. 96 + 97 + Generic Netlink 98 + =============== 99 + 100 + In addition to the Netlink fixed metadata header each Netlink protocol 101 + defines its own fixed metadata header. (Similarly to how network 102 + headers stack - Ethernet > IP > TCP we have Netlink > Generic N. > Family.) 103 + 104 + A Netlink message always starts with struct nlmsghdr, which is followed 105 + by a protocol-specific header. In case of Generic Netlink the protocol 106 + header is struct genlmsghdr. 107 + 108 + The practical meaning of the fields in case of Generic Netlink is as follows: 109 + 110 + .. code-block:: c 111 + 112 + struct nlmsghdr { 113 + __u32 nlmsg_len; /* Length of message including headers */ 114 + __u16 nlmsg_type; /* Generic Netlink Family (subsystem) ID */ 115 + __u16 nlmsg_flags; /* Flags - request or dump */ 116 + __u32 nlmsg_seq; /* Sequence number */ 117 + __u32 nlmsg_pid; /* Port ID, set to 0 */ 118 + }; 119 + struct genlmsghdr { 120 + __u8 cmd; /* Command, as defined by the Family */ 121 + __u8 version; /* Irrelevant, set to 1 */ 122 + __u16 reserved; /* Reserved, set to 0 */ 123 + }; 124 + /* TLV attributes follow... */ 125 + 126 + In Classic Netlink :c:member:`nlmsghdr.nlmsg_type` used to identify 127 + which operation within the subsystem the message was referring to 128 + (e.g. get information about a netdev). Generic Netlink needs to mux 129 + multiple subsystems in a single protocol so it uses this field to 130 + identify the subsystem, and :c:member:`genlmsghdr.cmd` identifies 131 + the operation instead. (See :ref:`res_fam` for 132 + information on how to find the Family ID of the subsystem of interest.) 133 + Note that the first 16 values (0 - 15) of this field are reserved for 134 + control messages both in Classic Netlink and Generic Netlink. 135 + See :ref:`nl_msg_type` for more details. 136 + 137 + There are 3 usual types of message exchanges on a Netlink socket: 138 + 139 + - performing a single action (``do``); 140 + - dumping information (``dump``); 141 + - getting asynchronous notifications (``multicast``). 142 + 143 + Classic Netlink is very flexible and presumably allows other types 144 + of exchanges to happen, but in practice those are the three that get 145 + used. 146 + 147 + Asynchronous notifications are sent by the kernel and received by 148 + the user sockets which subscribed to them. ``do`` and ``dump`` requests 149 + are initiated by the user. :c:member:`nlmsghdr.nlmsg_flags` should 150 + be set as follows: 151 + 152 + - for ``do``: ``NLM_F_REQUEST | NLM_F_ACK`` 153 + - for ``dump``: ``NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP`` 154 + 155 + :c:member:`nlmsghdr.nlmsg_seq` should be a set to a monotonically 156 + increasing value. The value gets echoed back in responses and doesn't 157 + matter in practice, but setting it to an increasing value for each 158 + message sent is considered good hygiene. The purpose of the field is 159 + matching responses to requests. Asynchronous notifications will have 160 + :c:member:`nlmsghdr.nlmsg_seq` of ``0``. 161 + 162 + :c:member:`nlmsghdr.nlmsg_pid` is the Netlink equivalent of an address. 163 + This field can be set to ``0`` when talking to the kernel. 164 + See :ref:`nlmsg_pid` for the (uncommon) uses of the field. 165 + 166 + The expected use for :c:member:`genlmsghdr.version` was to allow 167 + versioning of the APIs provided by the subsystems. No subsystem to 168 + date made significant use of this field, so setting it to ``1`` seems 169 + like a safe bet. 170 + 171 + .. _nl_msg_type: 172 + 173 + Netlink message types 174 + --------------------- 175 + 176 + As previously mentioned :c:member:`nlmsghdr.nlmsg_type` carries 177 + protocol specific values but the first 16 identifiers are reserved 178 + (first subsystem specific message type should be equal to 179 + ``NLMSG_MIN_TYPE`` which is ``0x10``). 180 + 181 + There are only 4 Netlink control messages defined: 182 + 183 + - ``NLMSG_NOOP`` - ignore the message, not used in practice; 184 + - ``NLMSG_ERROR`` - carries the return code of an operation; 185 + - ``NLMSG_DONE`` - marks the end of a dump; 186 + - ``NLMSG_OVERRUN`` - socket buffer has overflown, not used to date. 187 + 188 + ``NLMSG_ERROR`` and ``NLMSG_DONE`` are of practical importance. 189 + They carry return codes for operations. Note that unless 190 + the ``NLM_F_ACK`` flag is set on the request Netlink will not respond 191 + with ``NLMSG_ERROR`` if there is no error. To avoid having to special-case 192 + this quirk it is recommended to always set ``NLM_F_ACK``. 193 + 194 + The format of ``NLMSG_ERROR`` is described by struct nlmsgerr:: 195 + 196 + ---------------------------------------------- 197 + | struct nlmsghdr - response header | 198 + ---------------------------------------------- 199 + | int error | 200 + ---------------------------------------------- 201 + | struct nlmsghdr - original request header | 202 + ---------------------------------------------- 203 + | ** optionally (1) payload of the request | 204 + ---------------------------------------------- 205 + | ** optionally (2) extended ACK | 206 + ---------------------------------------------- 207 + 208 + There are two instances of struct nlmsghdr here, first of the response 209 + and second of the request. ``NLMSG_ERROR`` carries the information about 210 + the request which led to the error. This could be useful when trying 211 + to match requests to responses or re-parse the request to dump it into 212 + logs. 213 + 214 + The payload of the request is not echoed in messages reporting success 215 + (``error == 0``) or if ``NETLINK_CAP_ACK`` setsockopt() was set. 216 + The latter is common 217 + and perhaps recommended as having to read a copy of every request back 218 + from the kernel is rather wasteful. The absence of request payload 219 + is indicated by ``NLM_F_CAPPED`` in :c:member:`nlmsghdr.nlmsg_flags`. 220 + 221 + The second optional element of ``NLMSG_ERROR`` are the extended ACK 222 + attributes. See :ref:`ext_ack` for more details. The presence 223 + of extended ACK is indicated by ``NLM_F_ACK_TLVS`` in 224 + :c:member:`nlmsghdr.nlmsg_flags`. 225 + 226 + ``NLMSG_DONE`` is simpler, the request is never echoed but the extended 227 + ACK attributes may be present:: 228 + 229 + ---------------------------------------------- 230 + | struct nlmsghdr - response header | 231 + ---------------------------------------------- 232 + | int error | 233 + ---------------------------------------------- 234 + | ** optionally extended ACK | 235 + ---------------------------------------------- 236 + 237 + .. _res_fam: 238 + 239 + Resolving the Family ID 240 + ----------------------- 241 + 242 + This section explains how to find the Family ID of a subsystem. 243 + It also serves as an example of Generic Netlink communication. 244 + 245 + Generic Netlink is itself a subsystem exposed via the Generic Netlink API. 246 + To avoid a circular dependency Generic Netlink has a statically allocated 247 + Family ID (``GENL_ID_CTRL`` which is equal to ``NLMSG_MIN_TYPE``). 248 + The Generic Netlink family implements a command used to find out information 249 + about other families (``CTRL_CMD_GETFAMILY``). 250 + 251 + To get information about the Generic Netlink family named for example 252 + ``"test1"`` we need to send a message on the previously opened Generic Netlink 253 + socket. The message should target the Generic Netlink Family (1), be a 254 + ``do`` (2) call to ``CTRL_CMD_GETFAMILY`` (3). A ``dump`` version of this 255 + call would make the kernel respond with information about *all* the families 256 + it knows about. Last but not least the name of the family in question has 257 + to be specified (4) as an attribute with the appropriate type:: 258 + 259 + struct nlmsghdr: 260 + __u32 nlmsg_len: 32 261 + __u16 nlmsg_type: GENL_ID_CTRL // (1) 262 + __u16 nlmsg_flags: NLM_F_REQUEST | NLM_F_ACK // (2) 263 + __u32 nlmsg_seq: 1 264 + __u32 nlmsg_pid: 0 265 + 266 + struct genlmsghdr: 267 + __u8 cmd: CTRL_CMD_GETFAMILY // (3) 268 + __u8 version: 2 /* or 1, doesn't matter */ 269 + __u16 reserved: 0 270 + 271 + struct nlattr: // (4) 272 + __u16 nla_len: 10 273 + __u16 nla_type: CTRL_ATTR_FAMILY_NAME 274 + char data: test1\0 275 + 276 + (padding:) 277 + char data: \0\0 278 + 279 + The length fields in Netlink (:c:member:`nlmsghdr.nlmsg_len` 280 + and :c:member:`nlattr.nla_len`) always *include* the header. 281 + Attribute headers in netlink must be aligned to 4 bytes from the start 282 + of the message, hence the extra ``\0\0`` after ``CTRL_ATTR_FAMILY_NAME``. 283 + The attribute lengths *exclude* the padding. 284 + 285 + If the family is found kernel will reply with two messages, the response 286 + with all the information about the family:: 287 + 288 + /* Message #1 - reply */ 289 + struct nlmsghdr: 290 + __u32 nlmsg_len: 136 291 + __u16 nlmsg_type: GENL_ID_CTRL 292 + __u16 nlmsg_flags: 0 293 + __u32 nlmsg_seq: 1 /* echoed from our request */ 294 + __u32 nlmsg_pid: 5831 /* The PID of our user space process */ 295 + 296 + struct genlmsghdr: 297 + __u8 cmd: CTRL_CMD_GETFAMILY 298 + __u8 version: 2 299 + __u16 reserved: 0 300 + 301 + struct nlattr: 302 + __u16 nla_len: 10 303 + __u16 nla_type: CTRL_ATTR_FAMILY_NAME 304 + char data: test1\0 305 + 306 + (padding:) 307 + data: \0\0 308 + 309 + struct nlattr: 310 + __u16 nla_len: 6 311 + __u16 nla_type: CTRL_ATTR_FAMILY_ID 312 + __u16: 123 /* The Family ID we are after */ 313 + 314 + (padding:) 315 + char data: \0\0 316 + 317 + struct nlattr: 318 + __u16 nla_len: 9 319 + __u16 nla_type: CTRL_ATTR_FAMILY_VERSION 320 + __u16: 1 321 + 322 + /* ... etc, more attributes will follow. */ 323 + 324 + And the error code (success) since ``NLM_F_ACK`` had been set on the request:: 325 + 326 + /* Message #2 - the ACK */ 327 + struct nlmsghdr: 328 + __u32 nlmsg_len: 36 329 + __u16 nlmsg_type: NLMSG_ERROR 330 + __u16 nlmsg_flags: NLM_F_CAPPED /* There won't be a payload */ 331 + __u32 nlmsg_seq: 1 /* echoed from our request */ 332 + __u32 nlmsg_pid: 5831 /* The PID of our user space process */ 333 + 334 + int error: 0 335 + 336 + struct nlmsghdr: /* Copy of the request header as we sent it */ 337 + __u32 nlmsg_len: 32 338 + __u16 nlmsg_type: GENL_ID_CTRL 339 + __u16 nlmsg_flags: NLM_F_REQUEST | NLM_F_ACK 340 + __u32 nlmsg_seq: 1 341 + __u32 nlmsg_pid: 0 342 + 343 + The order of attributes (struct nlattr) is not guaranteed so the user 344 + has to walk the attributes and parse them. 345 + 346 + Note that Generic Netlink sockets are not associated or bound to a single 347 + family. A socket can be used to exchange messages with many different 348 + families, selecting the recipient family on message-by-message basis using 349 + the :c:member:`nlmsghdr.nlmsg_type` field. 350 + 351 + .. _ext_ack: 352 + 353 + Extended ACK 354 + ------------ 355 + 356 + Extended ACK controls reporting of additional error/warning TLVs 357 + in ``NLMSG_ERROR`` and ``NLMSG_DONE`` messages. To maintain backward 358 + compatibility this feature has to be explicitly enabled by setting 359 + the ``NETLINK_EXT_ACK`` setsockopt() to ``1``. 360 + 361 + Types of extended ack attributes are defined in enum nlmsgerr_attrs. 362 + The two most commonly used attributes are ``NLMSGERR_ATTR_MSG`` 363 + and ``NLMSGERR_ATTR_OFFS``. 364 + 365 + ``NLMSGERR_ATTR_MSG`` carries a message in English describing 366 + the encountered problem. These messages are far more detailed 367 + than what can be expressed thru standard UNIX error codes. 368 + 369 + ``NLMSGERR_ATTR_OFFS`` points to the attribute which caused the problem. 370 + 371 + Extended ACKs can be reported on errors as well as in case of success. 372 + The latter should be treated as a warning. 373 + 374 + Extended ACKs greatly improve the usability of Netlink and should 375 + always be enabled, appropriately parsed and reported to the user. 376 + 377 + Advanced topics 378 + =============== 379 + 380 + Dump consistency 381 + ---------------- 382 + 383 + Some of the data structures kernel uses for storing objects make 384 + it hard to provide an atomic snapshot of all the objects in a dump 385 + (without impacting the fast-paths updating them). 386 + 387 + Kernel may set the ``NLM_F_DUMP_INTR`` flag on any message in a dump 388 + (including the ``NLMSG_DONE`` message) if the dump was interrupted and 389 + may be inconsistent (e.g. missing objects). User space should retry 390 + the dump if it sees the flag set. 391 + 392 + Introspection 393 + ------------- 394 + 395 + The basic introspection abilities are enabled by access to the Family 396 + object as reported in :ref:`res_fam`. User can query information about 397 + the Generic Netlink family, including which operations are supported 398 + by the kernel and what attributes the kernel understands. 399 + Family information includes the highest ID of an attribute kernel can parse, 400 + a separate command (``CTRL_CMD_GETPOLICY``) provides detailed information 401 + about supported attributes, including ranges of values the kernel accepts. 402 + 403 + Querying family information is useful in cases when user space needs 404 + to make sure that the kernel has support for a feature before issuing 405 + a request. 406 + 407 + .. _nlmsg_pid: 408 + 409 + nlmsg_pid 410 + --------- 411 + 412 + :c:member:`nlmsghdr.nlmsg_pid` is the Netlink equivalent of an address. 413 + It is referred to as Port ID, sometimes Process ID because for historical 414 + reasons if the application does not select (bind() to) an explicit Port ID 415 + kernel will automatically assign it the ID equal to its Process ID 416 + (as reported by the getpid() system call). 417 + 418 + Similarly to the bind() semantics of the TCP/IP network protocols the value 419 + of zero means "assign automatically", hence it is common for applications 420 + to leave the :c:member:`nlmsghdr.nlmsg_pid` field initialized to ``0``. 421 + 422 + The field is still used today in rare cases when kernel needs to send 423 + a unicast notification. User space application can use bind() to associate 424 + its socket with a specific PID, it then communicates its PID to the kernel. 425 + This way the kernel can reach the specific user space process. 426 + 427 + This sort of communication is utilized in UMH (User Mode Helper)-like 428 + scenarios when kernel needs to trigger user space processing or ask user 429 + space for a policy decision. 430 + 431 + Multicast notifications 432 + ----------------------- 433 + 434 + One of the strengths of Netlink is the ability to send event notifications 435 + to user space. This is a unidirectional form of communication (kernel -> 436 + user) and does not involve any control messages like ``NLMSG_ERROR`` or 437 + ``NLMSG_DONE``. 438 + 439 + For example the Generic Netlink family itself defines a set of multicast 440 + notifications about registered families. When a new family is added the 441 + sockets subscribed to the notifications will get the following message:: 442 + 443 + struct nlmsghdr: 444 + __u32 nlmsg_len: 136 445 + __u16 nlmsg_type: GENL_ID_CTRL 446 + __u16 nlmsg_flags: 0 447 + __u32 nlmsg_seq: 0 448 + __u32 nlmsg_pid: 0 449 + 450 + struct genlmsghdr: 451 + __u8 cmd: CTRL_CMD_NEWFAMILY 452 + __u8 version: 2 453 + __u16 reserved: 0 454 + 455 + struct nlattr: 456 + __u16 nla_len: 10 457 + __u16 nla_type: CTRL_ATTR_FAMILY_NAME 458 + char data: test1\0 459 + 460 + (padding:) 461 + data: \0\0 462 + 463 + struct nlattr: 464 + __u16 nla_len: 6 465 + __u16 nla_type: CTRL_ATTR_FAMILY_ID 466 + __u16: 123 /* The Family ID we are after */ 467 + 468 + (padding:) 469 + char data: \0\0 470 + 471 + struct nlattr: 472 + __u16 nla_len: 9 473 + __u16 nla_type: CTRL_ATTR_FAMILY_VERSION 474 + __u16: 1 475 + 476 + /* ... etc, more attributes will follow. */ 477 + 478 + The notification contains the same information as the response 479 + to the ``CTRL_CMD_GETFAMILY`` request. 480 + 481 + The Netlink headers of the notification are mostly 0 and irrelevant. 482 + The :c:member:`nlmsghdr.nlmsg_seq` may be either zero or a monotonically 483 + increasing notification sequence number maintained by the family. 484 + 485 + To receive notifications the user socket must subscribe to the relevant 486 + notification group. Much like the Family ID, the Group ID for a given 487 + multicast group is dynamic and can be found inside the Family information. 488 + The ``CTRL_ATTR_MCAST_GROUPS`` attribute contains nests with names 489 + (``CTRL_ATTR_MCAST_GRP_NAME``) and IDs (``CTRL_ATTR_MCAST_GRP_ID``) of 490 + the groups family. 491 + 492 + Once the Group ID is known a setsockopt() call adds the socket to the group: 493 + 494 + .. code-block:: c 495 + 496 + unsigned int group_id; 497 + 498 + /* .. find the group ID... */ 499 + 500 + setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, 501 + &group_id, sizeof(group_id)); 502 + 503 + The socket will now receive notifications. 504 + 505 + It is recommended to use separate sockets for receiving notifications 506 + and sending requests to the kernel. The asynchronous nature of notifications 507 + means that they may get mixed in with the responses making the message 508 + handling much harder. 509 + 510 + Buffer sizing 511 + ------------- 512 + 513 + Netlink sockets are datagram sockets rather than stream sockets, 514 + meaning that each message must be received in its entirety by a single 515 + recv()/recvmsg() system call. If the buffer provided by the user is too 516 + short, the message will be truncated and the ``MSG_TRUNC`` flag set 517 + in struct msghdr (struct msghdr is the second argument 518 + of the recvmsg() system call, *not* a Netlink header). 519 + 520 + Upon truncation the remaining part of the message is discarded. 521 + 522 + Netlink expects that the user buffer will be at least 8kB or a page 523 + size of the CPU architecture, whichever is bigger. Particular Netlink 524 + families may, however, require a larger buffer. 32kB buffer is recommended 525 + for most efficient handling of dumps (larger buffer fits more dumped 526 + objects and therefore fewer recvmsg() calls are needed). 527 + 528 + Classic Netlink 529 + =============== 530 + 531 + The main differences between Classic and Generic Netlink are the dynamic 532 + allocation of subsystem identifiers and availability of introspection. 533 + In theory the protocol does not differ significantly, however, in practice 534 + Classic Netlink experimented with concepts which were abandoned in Generic 535 + Netlink (really, they usually only found use in a small corner of a single 536 + subsystem). This section is meant as an explainer of a few of such concepts, 537 + with the explicit goal of giving the Generic Netlink 538 + users the confidence to ignore them when reading the uAPI headers. 539 + 540 + Most of the concepts and examples here refer to the ``NETLINK_ROUTE`` family, 541 + which covers much of the configuration of the Linux networking stack. 542 + Real documentation of that family, deserves a chapter (or a book) of its own. 543 + 544 + Families 545 + -------- 546 + 547 + Netlink refers to subsystems as families. This is a remnant of using 548 + sockets and the concept of protocol families, which are part of message 549 + demultiplexing in ``NETLINK_ROUTE``. 550 + 551 + Sadly every layer of encapsulation likes to refer to whatever it's carrying 552 + as "families" making the term very confusing: 553 + 554 + 1. AF_NETLINK is a bona fide socket protocol family 555 + 2. AF_NETLINK's documentation refers to what comes after its own 556 + header (struct nlmsghdr) in a message as a "Family Header" 557 + 3. Generic Netlink is a family for AF_NETLINK (struct genlmsghdr follows 558 + struct nlmsghdr), yet it also calls its users "Families". 559 + 560 + Note that the Generic Netlink Family IDs are in a different "ID space" 561 + and overlap with Classic Netlink protocol numbers (e.g. ``NETLINK_CRYPTO`` 562 + has the Classic Netlink protocol ID of 21 which Generic Netlink will 563 + happily allocate to one of its families as well). 564 + 565 + Strict checking 566 + --------------- 567 + 568 + The ``NETLINK_GET_STRICT_CHK`` socket option enables strict input checking 569 + in ``NETLINK_ROUTE``. It was needed because historically kernel did not 570 + validate the fields of structures it didn't process. This made it impossible 571 + to start using those fields later without risking regressions in applications 572 + which initialized them incorrectly or not at all. 573 + 574 + ``NETLINK_GET_STRICT_CHK`` declares that the application is initializing 575 + all fields correctly. It also opts into validating that message does not 576 + contain trailing data and requests that kernel rejects attributes with 577 + type higher than largest attribute type known to the kernel. 578 + 579 + ``NETLINK_GET_STRICT_CHK`` is not used outside of ``NETLINK_ROUTE``. 580 + 581 + Unknown attributes 582 + ------------------ 583 + 584 + Historically Netlink ignored all unknown attributes. The thinking was that 585 + it would free the application from having to probe what kernel supports. 586 + The application could make a request to change the state and check which 587 + parts of the request "stuck". 588 + 589 + This is no longer the case for new Generic Netlink families and those opting 590 + in to strict checking. See enum netlink_validation for validation types 591 + performed. 592 + 593 + Fixed metadata and structures 594 + ----------------------------- 595 + 596 + Classic Netlink made liberal use of fixed-format structures within 597 + the messages. Messages would commonly have a structure with 598 + a considerable number of fields after struct nlmsghdr. It was also 599 + common to put structures with multiple members inside attributes, 600 + without breaking each member into an attribute of its own. 601 + 602 + This has caused problems with validation and extensibility and 603 + therefore using binary structures is actively discouraged for new 604 + attributes. 605 + 606 + Request types 607 + ------------- 608 + 609 + ``NETLINK_ROUTE`` categorized requests into 4 types ``NEW``, ``DEL``, ``GET``, 610 + and ``SET``. Each object can handle all or some of those requests 611 + (objects being netdevs, routes, addresses, qdiscs etc.) Request type 612 + is defined by the 2 lowest bits of the message type, so commands for 613 + new objects would always be allocated with a stride of 4. 614 + 615 + Each object would also have it's own fixed metadata shared by all request 616 + types (e.g. struct ifinfomsg for netdev requests, struct ifaddrmsg for address 617 + requests, struct tcmsg for qdisc requests). 618 + 619 + Even though other protocols and Generic Netlink commands often use 620 + the same verbs in their message names (``GET``, ``SET``) the concept 621 + of request types did not find wider adoption. 622 + 623 + Message flags 624 + ------------- 625 + 626 + The earlier section has already covered the basic request flags 627 + (``NLM_F_REQUEST``, ``NLM_F_ACK``, ``NLM_F_DUMP``) and the ``NLMSG_ERROR`` / 628 + ``NLMSG_DONE`` flags (``NLM_F_CAPPED``, ``NLM_F_ACK_TLVS``). 629 + Dump flags were also mentioned (``NLM_F_MULTI``, ``NLM_F_DUMP_INTR``). 630 + 631 + Those are the main flags of note, with a small exception (of ``ieee802154``) 632 + Generic Netlink does not make use of other flags. If the protocol needs 633 + to communicate special constraints for a request it should use 634 + an attribute, not the flags in struct nlmsghdr. 635 + 636 + Classic Netlink, however, defined various flags for its ``GET``, ``NEW`` 637 + and ``DEL`` requests. Since request types have not been generalized 638 + the request type specific flags should not be used either. 639 + 640 + uAPI reference 641 + ============== 642 + 643 + .. kernel-doc:: include/uapi/linux/netlink.h