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