intro.rst 26 KB

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