specs.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. .. SPDX-License-Identifier: BSD-3-Clause
  2. =========================================
  3. Netlink protocol specifications (in YAML)
  4. =========================================
  5. Netlink protocol specifications are complete, machine readable descriptions of
  6. Netlink protocols written in YAML. The goal of the specifications is to allow
  7. separating Netlink parsing from user space logic and minimize the amount of
  8. hand written Netlink code for each new family, command, attribute.
  9. Netlink specs should be complete and not depend on any other spec
  10. or C header file, making it easy to use in languages which can't include
  11. kernel headers directly.
  12. Internally kernel uses the YAML specs to generate:
  13. - the C uAPI header
  14. - documentation of the protocol as a ReST file - see :ref:`Documentation/networking/netlink_spec/index.rst <specs>`
  15. - policy tables for input attribute validation
  16. - operation tables
  17. YAML specifications can be found under ``Documentation/netlink/specs/``
  18. This document describes details of the schema.
  19. See :doc:`intro-specs` for a practical starting guide.
  20. All specs must be licensed under
  21. ``((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)``
  22. to allow for easy adoption in user space code.
  23. Compatibility levels
  24. ====================
  25. There are four schema levels for Netlink specs, from the simplest used
  26. by new families to the most complex covering all the quirks of the old ones.
  27. Each next level inherits the attributes of the previous level, meaning that
  28. user capable of parsing more complex ``genetlink`` schemas is also compatible
  29. with simpler ones. The levels are:
  30. - ``genetlink`` - most streamlined, should be used by all new families
  31. - ``genetlink-c`` - superset of ``genetlink`` with extra attributes allowing
  32. customization of define and enum type and value names; this schema should
  33. be equivalent to ``genetlink`` for all implementations which don't interact
  34. directly with C uAPI headers
  35. - ``genetlink-legacy`` - Generic Netlink catch all schema supporting quirks of
  36. all old genetlink families, strange attribute formats, binary structures etc.
  37. - ``netlink-raw`` - catch all schema supporting pre-Generic Netlink protocols
  38. such as ``NETLINK_ROUTE``
  39. The definition of the schemas (in ``jsonschema``) can be found
  40. under ``Documentation/netlink/``.
  41. Schema structure
  42. ================
  43. YAML schema has the following conceptual sections:
  44. - globals
  45. - definitions
  46. - attributes
  47. - operations
  48. - multicast groups
  49. Most properties in the schema accept (or in fact require) a ``doc``
  50. sub-property documenting the defined object.
  51. The following sections describe the properties of the most modern ``genetlink``
  52. schema. See the documentation of :doc:`genetlink-c <c-code-gen>`
  53. for information on how C names are derived from name properties.
  54. See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>` for
  55. information on the Netlink specification properties that are only relevant to
  56. the kernel space and not part of the user space API.
  57. genetlink
  58. =========
  59. Globals
  60. -------
  61. Attributes listed directly at the root level of the spec file.
  62. name
  63. ~~~~
  64. Name of the family. Name identifies the family in a unique way, since
  65. the Family IDs are allocated dynamically.
  66. protocol
  67. ~~~~~~~~
  68. The schema level, default is ``genetlink``, which is the only value
  69. allowed for new ``genetlink`` families.
  70. definitions
  71. -----------
  72. Array of type and constant definitions.
  73. name
  74. ~~~~
  75. Name of the type / constant.
  76. type
  77. ~~~~
  78. One of the following types:
  79. - const - a single, standalone constant
  80. - enum - defines an integer enumeration, with values for each entry
  81. incrementing by 1, (e.g. 0, 1, 2, 3)
  82. - flags - defines an integer enumeration, with values for each entry
  83. occupying a bit, starting from bit 0, (e.g. 1, 2, 4, 8)
  84. value
  85. ~~~~~
  86. The value for the ``const``.
  87. value-start
  88. ~~~~~~~~~~~
  89. The first value for ``enum`` and ``flags``, allows overriding the default
  90. start value of ``0`` (for ``enum``) and starting bit (for ``flags``).
  91. For ``flags`` ``value-start`` selects the starting bit, not the shifted value.
  92. Sparse enumerations are not supported.
  93. entries
  94. ~~~~~~~
  95. Array of names of the entries for ``enum`` and ``flags``.
  96. header
  97. ~~~~~~
  98. For C-compatible languages, header which already defines this value.
  99. In case the definition is shared by multiple families (e.g. ``IFNAMSIZ``)
  100. code generators for C-compatible languages may prefer to add an appropriate
  101. include instead of rendering a new definition.
  102. attribute-sets
  103. --------------
  104. This property contains information about netlink attributes of the family.
  105. All families have at least one attribute set, most have multiple.
  106. ``attribute-sets`` is an array, with each entry describing a single set.
  107. Note that the spec is "flattened" and is not meant to visually resemble
  108. the format of the netlink messages (unlike certain ad-hoc documentation
  109. formats seen in kernel comments). In the spec subordinate attribute sets
  110. are not defined inline as a nest, but defined in a separate attribute set
  111. referred to with a ``nested-attributes`` property of the container.
  112. Spec may also contain fractional sets - sets which contain a ``subset-of``
  113. property. Such sets describe a section of a full set, allowing narrowing down
  114. which attributes are allowed in a nest or refining the validation criteria.
  115. Fractional sets can only be used in nests. They are not rendered to the uAPI
  116. in any fashion.
  117. name
  118. ~~~~
  119. Uniquely identifies the attribute set, operations and nested attributes
  120. refer to the sets by the ``name``.
  121. subset-of
  122. ~~~~~~~~~
  123. Re-defines a portion of another set (a fractional set).
  124. Allows narrowing down fields and changing validation criteria
  125. or even types of attributes depending on the nest in which they
  126. are contained. The ``value`` of each attribute in the fractional
  127. set is implicitly the same as in the main set.
  128. attributes
  129. ~~~~~~~~~~
  130. List of attributes in the set.
  131. .. _attribute_properties:
  132. Attribute properties
  133. --------------------
  134. name
  135. ~~~~
  136. Identifies the attribute, unique within the set.
  137. type
  138. ~~~~
  139. Netlink attribute type, see :ref:`attr_types`.
  140. .. _assign_val:
  141. value
  142. ~~~~~
  143. Numerical attribute ID, used in serialized Netlink messages.
  144. The ``value`` property can be skipped, in which case the attribute ID
  145. will be the value of the previous attribute plus one (recursively)
  146. and ``1`` for the first attribute in the attribute set.
  147. Attributes (and operations) use ``1`` as the default value for the first
  148. entry (unlike enums in definitions which start from ``0``) because
  149. entry ``0`` is almost always reserved as undefined. Spec can explicitly
  150. set value to ``0`` if needed.
  151. Note that the ``value`` of an attribute is defined only in its main set
  152. (not in subsets).
  153. enum
  154. ~~~~
  155. For integer types specifies that values in the attribute belong
  156. to an ``enum`` or ``flags`` from the ``definitions`` section.
  157. enum-as-flags
  158. ~~~~~~~~~~~~~
  159. Treat ``enum`` as ``flags`` regardless of its type in ``definitions``.
  160. When both ``enum`` and ``flags`` forms are needed ``definitions`` should
  161. contain an ``enum`` and attributes which need the ``flags`` form should
  162. use this attribute.
  163. nested-attributes
  164. ~~~~~~~~~~~~~~~~~
  165. Identifies the attribute space for attributes nested within given attribute.
  166. Only valid for complex attributes which may have sub-attributes.
  167. multi-attr (arrays)
  168. ~~~~~~~~~~~~~~~~~~~
  169. Boolean property signifying that the attribute may be present multiple times.
  170. Allowing an attribute to repeat is the recommended way of implementing arrays
  171. (no extra nesting).
  172. byte-order
  173. ~~~~~~~~~~
  174. For integer types specifies attribute byte order - ``little-endian``
  175. or ``big-endian``.
  176. checks
  177. ~~~~~~
  178. Input validation constraints used by the kernel. User space should query
  179. the policy of the running kernel using Generic Netlink introspection,
  180. rather than depend on what is specified in the spec file.
  181. The validation policy in the kernel is formed by combining the type
  182. definition (``type`` and ``nested-attributes``) and the ``checks``.
  183. sub-type
  184. ~~~~~~~~
  185. Legacy families have special ways of expressing arrays. ``sub-type`` can be
  186. used to define the type of array members in case array members are not
  187. fully defined as attributes (in a bona fide attribute space). For instance
  188. a C array of u32 values can be specified with ``type: binary`` and
  189. ``sub-type: u32``. Binary types and legacy array formats are described in
  190. more detail in :doc:`genetlink-legacy`.
  191. display-hint
  192. ~~~~~~~~~~~~
  193. Optional format indicator that is intended only for choosing the right
  194. formatting mechanism when displaying values of this type. Currently supported
  195. hints are ``hex``, ``mac``, ``fddi``, ``ipv4``, ``ipv6`` and ``uuid``.
  196. operations
  197. ----------
  198. This section describes messages passed between the kernel and the user space.
  199. There are three types of entries in this section - operations, notifications
  200. and events.
  201. Operations describe the most common request - response communication. User
  202. sends a request and kernel replies. Each operation may contain any combination
  203. of the two modes familiar to netlink users - ``do`` and ``dump``.
  204. ``do`` and ``dump`` in turn contain a combination of ``request`` and
  205. ``response`` properties. If no explicit message with attributes is passed
  206. in a given direction (e.g. a ``dump`` which does not accept filter, or a ``do``
  207. of a SET operation to which the kernel responds with just the netlink error
  208. code) ``request`` or ``response`` section can be skipped.
  209. ``request`` and ``response`` sections list the attributes allowed in a message.
  210. The list contains only the names of attributes from a set referred
  211. to by the ``attribute-set`` property.
  212. Notifications and events both refer to the asynchronous messages sent by
  213. the kernel to members of a multicast group. The difference between the
  214. two is that a notification shares its contents with a GET operation
  215. (the name of the GET operation is specified in the ``notify`` property).
  216. This arrangement is commonly used for notifications about
  217. objects where the notification carries the full object definition.
  218. Events are more focused and carry only a subset of information rather than full
  219. object state (a made up example would be a link state change event with just
  220. the interface name and the new link state). Events contain the ``event``
  221. property. Events are considered less idiomatic for netlink and notifications
  222. should be preferred.
  223. list
  224. ~~~~
  225. The only property of ``operations`` for ``genetlink``, holds the list of
  226. operations, notifications etc.
  227. Operation properties
  228. --------------------
  229. name
  230. ~~~~
  231. Identifies the operation.
  232. value
  233. ~~~~~
  234. Numerical message ID, used in serialized Netlink messages.
  235. The same enumeration rules are applied as to
  236. :ref:`attribute values<assign_val>`.
  237. attribute-set
  238. ~~~~~~~~~~~~~
  239. Specifies the attribute set contained within the message.
  240. do
  241. ~~~
  242. Specification for the ``doit`` request. Should contain ``request``, ``reply``
  243. or both of these properties, each holding a :ref:`attr_list`.
  244. dump
  245. ~~~~
  246. Specification for the ``dumpit`` request. Should contain ``request``, ``reply``
  247. or both of these properties, each holding a :ref:`attr_list`.
  248. notify
  249. ~~~~~~
  250. Designates the message as a notification. Contains the name of the operation
  251. (possibly the same as the operation holding this property) which shares
  252. the contents with the notification (``do``).
  253. event
  254. ~~~~~
  255. Specification of attributes in the event, holds a :ref:`attr_list`.
  256. ``event`` property is mutually exclusive with ``notify``.
  257. mcgrp
  258. ~~~~~
  259. Used with ``event`` and ``notify``, specifies which multicast group
  260. message belongs to.
  261. .. _attr_list:
  262. Message attribute list
  263. ----------------------
  264. ``request``, ``reply`` and ``event`` properties have a single ``attributes``
  265. property which holds the list of attribute names.
  266. Messages can also define ``pre`` and ``post`` properties which will be rendered
  267. as ``pre_doit`` and ``post_doit`` calls in the kernel (these properties should
  268. be ignored by user space).
  269. mcast-groups
  270. ------------
  271. This section lists the multicast groups of the family.
  272. list
  273. ~~~~
  274. The only property of ``mcast-groups`` for ``genetlink``, holds the list
  275. of groups.
  276. Multicast group properties
  277. --------------------------
  278. name
  279. ~~~~
  280. Uniquely identifies the multicast group in the family. Similarly to
  281. Family ID, Multicast Group ID needs to be resolved at runtime, based
  282. on the name.
  283. .. _attr_types:
  284. Attribute types
  285. ===============
  286. This section describes the attribute types supported by the ``genetlink``
  287. compatibility level. Refer to documentation of different levels for additional
  288. attribute types.
  289. Common integer types
  290. --------------------
  291. ``sint`` and ``uint`` represent signed and unsigned 64 bit integers.
  292. If the value can fit on 32 bits only 32 bits are carried in netlink
  293. messages, otherwise full 64 bits are carried. Note that the payload
  294. is only aligned to 4B, so the full 64 bit value may be unaligned!
  295. Common integer types should be preferred over fix-width types in majority
  296. of cases.
  297. Fix-width integer types
  298. -----------------------
  299. Fixed-width integer types include:
  300. ``u8``, ``u16``, ``u32``, ``u64``, ``s8``, ``s16``, ``s32``, ``s64``.
  301. Note that types smaller than 32 bit should be avoided as using them
  302. does not save any memory in Netlink messages (due to alignment).
  303. See :ref:`pad_type` for padding of 64 bit attributes.
  304. The payload of the attribute is the integer in host order unless ``byte-order``
  305. specifies otherwise.
  306. 64 bit values are usually aligned by the kernel but it is recommended
  307. that the user space is able to deal with unaligned values.
  308. .. _pad_type:
  309. pad
  310. ---
  311. Special attribute type used for padding attributes which require alignment
  312. bigger than standard 4B alignment required by netlink (e.g. 64 bit integers).
  313. There can only be a single attribute of the ``pad`` type in any attribute set
  314. and it should be automatically used for padding when needed.
  315. flag
  316. ----
  317. Attribute with no payload, its presence is the entire information.
  318. binary
  319. ------
  320. Raw binary data attribute, the contents are opaque to generic code.
  321. string
  322. ------
  323. Character string. Unless ``checks`` has ``unterminated-ok`` set to ``true``
  324. the string is required to be null terminated.
  325. ``max-len`` in ``checks`` indicates the longest possible string,
  326. if not present the length of the string is unbounded.
  327. Note that ``max-len`` does not count the terminating character.
  328. nest
  329. ----
  330. Attribute containing other (nested) attributes.
  331. ``nested-attributes`` specifies which attribute set is used inside.