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