Merge branch 'tools-net-ynl-add-support-for-netlink-raw-families'

Donald Hunter says:

====================
tools/net/ynl: Add support for netlink-raw families

This patchset adds support for netlink-raw families such as rtnetlink.

Patch 1 fixes a typo in existing schemas
Patch 2 contains the schema definition
Patches 3 & 4 update the schema documentation
Patches 5 - 9 extends ynl
Patches 10 - 12 add several netlink-raw specs

The netlink-raw schema is very similar to genetlink-legacy and I thought
about making the changes there and symlinking to it. On balance I
thought that might be problematic for accurate schema validation.

rtnetlink doesn't seem to fit into unified or directional message
enumeration models. It seems like an 'explicit' model would be useful,
to force the schema author to specify the message ids directly.

There is not yet support for notifications because ynl currently doesn't
support defining 'event' properties on a 'do' operation. The message ids
are shared so ops need to be both sync and async. I plan to look at this
in a future patch.

The link and route messages contain different nested attributes
dependent on the type of link or route. Decoding these will need some
kind of attr-space selection that uses the value of another attribute as
the selector key. These nested attributes have been left with type
'binary' for now.
====================

Link: https://lore.kernel.org/r/20230825122756.7603-1-donald.hunter@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2023-08-27 17:17:19 -07:00
15 changed files with 2632 additions and 72 deletions

View File

@@ -67,10 +67,11 @@ Globals
kernel-policy
~~~~~~~~~~~~~
Defines if the kernel validation policy is per operation (``per-op``)
or for the entire family (``global``). New families should use ``per-op``
(default) to be able to narrow down the attributes accepted by a specific
command.
Defines whether the kernel validation policy is ``global`` i.e. the same for all
operations of the family, defined for each operation individually - ``per-op``,
or separately for each operation and operation type (do vs dump) - ``split``.
New families should use ``per-op`` (default) to be able to narrow down the
attributes accepted by a specific command.
checks
------

View File

@@ -41,7 +41,7 @@ properties:
description: Name of the define for the family name.
type: string
c-version-name:
description: Name of the define for the verion of the family.
description: Name of the define for the version of the family.
type: string
max-by-define:
description: Makes the number of attributes and commands be specified by a define, not an enum value.

View File

@@ -41,7 +41,7 @@ properties:
description: Name of the define for the family name.
type: string
c-version-name:
description: Name of the define for the verion of the family.
description: Name of the define for the version of the family.
type: string
max-by-define:
description: Makes the number of attributes and commands be specified by a define, not an enum value.

View File

@@ -0,0 +1,410 @@
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
%YAML 1.2
---
$id: http://kernel.org/schemas/netlink/netlink-raw.yaml#
$schema: https://json-schema.org/draft-07/schema
# Common defines
$defs:
uint:
type: integer
minimum: 0
len-or-define:
type: [ string, integer ]
pattern: ^[0-9A-Za-z_]+( - 1)?$
minimum: 0
# Schema for specs
title: Protocol
description: Specification of a raw netlink protocol
type: object
required: [ name, doc, attribute-sets, operations ]
additionalProperties: False
properties:
name:
description: Name of the netlink family.
type: string
doc:
type: string
protocol:
description: Schema compatibility level.
enum: [ netlink-raw ] # Trim
# Start netlink-raw
protonum:
description: Protocol number to use for netlink-raw
type: integer
# End netlink-raw
uapi-header:
description: Path to the uAPI header, default is linux/${family-name}.h
type: string
# Start genetlink-c
c-family-name:
description: Name of the define for the family name.
type: string
c-version-name:
description: Name of the define for the version of the family.
type: string
max-by-define:
description: Makes the number of attributes and commands be specified by a define, not an enum value.
type: boolean
# End genetlink-c
# Start genetlink-legacy
kernel-policy:
description: |
Defines if the input policy in the kernel is global, per-operation, or split per operation type.
Default is split.
enum: [ split, per-op, global ]
# End genetlink-legacy
definitions:
description: List of type and constant definitions (enums, flags, defines).
type: array
items:
type: object
required: [ type, name ]
additionalProperties: False
properties:
name:
type: string
header:
description: For C-compatible languages, header which already defines this value.
type: string
type:
enum: [ const, enum, flags, struct ] # Trim
doc:
type: string
# For const
value:
description: For const - the value.
type: [ string, integer ]
# For enum and flags
value-start:
description: For enum or flags the literal initializer for the first value.
type: [ string, integer ]
entries:
description: For enum or flags array of values.
type: array
items:
oneOf:
- type: string
- type: object
required: [ name ]
additionalProperties: False
properties:
name:
type: string
value:
type: integer
doc:
type: string
render-max:
description: Render the max members for this enum.
type: boolean
# Start genetlink-c
enum-name:
description: Name for enum, if empty no name will be used.
type: [ string, "null" ]
name-prefix:
description: For enum the prefix of the values, optional.
type: string
# End genetlink-c
# Start genetlink-legacy
members:
description: List of struct members. Only scalars and strings members allowed.
type: array
items:
type: object
required: [ name, type ]
additionalProperties: False
properties:
name:
type: string
type:
description: The netlink attribute type
enum: [ u8, u16, u32, u64, s8, s16, s32, s64, string, binary ]
len:
$ref: '#/$defs/len-or-define'
byte-order:
enum: [ little-endian, big-endian ]
doc:
description: Documentation for the struct member attribute.
type: string
enum:
description: Name of the enum type used for the attribute.
type: string
enum-as-flags:
description: |
Treat the enum as flags. In most cases enum is either used as flags or as values.
Sometimes, however, both forms are necessary, in which case header contains the enum
form while specific attributes may request to convert the values into a bitfield.
type: boolean
display-hint: &display-hint
description: |
Optional format indicator that is intended only for choosing
the right formatting mechanism when displaying values of this
type.
enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
# End genetlink-legacy
attribute-sets:
description: Definition of attribute spaces for this family.
type: array
items:
description: Definition of a single attribute space.
type: object
required: [ name, attributes ]
additionalProperties: False
properties:
name:
description: |
Name used when referring to this space in other definitions, not used outside of the spec.
type: string
name-prefix:
description: |
Prefix for the C enum name of the attributes. Default family[name]-set[name]-a-
type: string
enum-name:
description: Name for the enum type of the attribute.
type: string
doc:
description: Documentation of the space.
type: string
subset-of:
description: |
Name of another space which this is a logical part of. Sub-spaces can be used to define
a limited group of attributes which are used in a nest.
type: string
# Start genetlink-c
attr-cnt-name:
description: The explicit name for constant holding the count of attributes (last attr + 1).
type: string
attr-max-name:
description: The explicit name for last member of attribute enum.
type: string
# End genetlink-c
attributes:
description: List of attributes in the space.
type: array
items:
type: object
required: [ name, type ]
additionalProperties: False
properties:
name:
type: string
type: &attr-type
description: The netlink attribute type
enum: [ unused, pad, flag, binary, u8, u16, u32, u64, s32, s64,
string, nest, array-nest, nest-type-value ]
doc:
description: Documentation of the attribute.
type: string
value:
description: Value for the enum item representing this attribute in the uAPI.
$ref: '#/$defs/uint'
type-value:
description: Name of the value extracted from the type of a nest-type-value attribute.
type: array
items:
type: string
byte-order:
enum: [ little-endian, big-endian ]
multi-attr:
type: boolean
nested-attributes:
description: Name of the space (sub-space) used inside the attribute.
type: string
enum:
description: Name of the enum type used for the attribute.
type: string
enum-as-flags:
description: |
Treat the enum as flags. In most cases enum is either used as flags or as values.
Sometimes, however, both forms are necessary, in which case header contains the enum
form while specific attributes may request to convert the values into a bitfield.
type: boolean
checks:
description: Kernel input validation.
type: object
additionalProperties: False
properties:
flags-mask:
description: Name of the flags constant on which to base mask (unsigned scalar types only).
type: string
min:
description: Min value for an integer attribute.
type: integer
min-len:
description: Min length for a binary attribute.
$ref: '#/$defs/len-or-define'
max-len:
description: Max length for a string or a binary attribute.
$ref: '#/$defs/len-or-define'
sub-type: *attr-type
display-hint: *display-hint
# Start genetlink-c
name-prefix:
type: string
# End genetlink-c
# Start genetlink-legacy
struct:
description: Name of the struct type used for the attribute.
type: string
# End genetlink-legacy
# Make sure name-prefix does not appear in subsets (subsets inherit naming)
dependencies:
name-prefix:
not:
required: [ subset-of ]
subset-of:
not:
required: [ name-prefix ]
operations:
description: Operations supported by the protocol.
type: object
required: [ list ]
additionalProperties: False
properties:
enum-model:
description: |
The model of assigning values to the operations.
"unified" is the recommended model where all message types belong
to a single enum.
"directional" has the messages sent to the kernel and from the kernel
enumerated separately.
enum: [ unified, directional ] # Trim
name-prefix:
description: |
Prefix for the C enum name of the command. The name is formed by concatenating
the prefix with the upper case name of the command, with dashes replaced by underscores.
type: string
enum-name:
description: Name for the enum type with commands.
type: string
async-prefix:
description: Same as name-prefix but used to render notifications and events to separate enum.
type: string
async-enum:
description: Name for the enum type with notifications/events.
type: string
# Start genetlink-legacy
fixed-header: &fixed-header
description: |
Name of the structure defining the optional fixed-length protocol
header. This header is placed in a message after the netlink and
genetlink headers and before any attributes.
type: string
# End genetlink-legacy
list:
description: List of commands
type: array
items:
type: object
additionalProperties: False
required: [ name, doc ]
properties:
name:
description: Name of the operation, also defining its C enum value in uAPI.
type: string
doc:
description: Documentation for the command.
type: string
value:
description: Value for the enum in the uAPI.
$ref: '#/$defs/uint'
attribute-set:
description: |
Attribute space from which attributes directly in the requests and replies
to this command are defined.
type: string
flags: &cmd_flags
description: Command flags.
type: array
items:
enum: [ admin-perm ]
dont-validate:
description: Kernel attribute validation flags.
type: array
items:
enum: [ strict, dump ]
# Start genetlink-legacy
fixed-header: *fixed-header
# End genetlink-legacy
do: &subop-type
description: Main command handler.
type: object
additionalProperties: False
properties:
request: &subop-attr-list
description: Definition of the request message for a given command.
type: object
additionalProperties: False
properties:
attributes:
description: |
Names of attributes from the attribute-set (not full attribute
definitions, just names).
type: array
items:
type: string
# Start genetlink-legacy
value:
description: |
ID of this message if value for request and response differ,
i.e. requests and responses have different message enums.
$ref: '#/$defs/uint'
# End genetlink-legacy
reply: *subop-attr-list
pre:
description: Hook for a function to run before the main callback (pre_doit or start).
type: string
post:
description: Hook for a function to run after the main callback (post_doit or done).
type: string
dump: *subop-type
notify:
description: Name of the command sharing the reply type with this notification.
type: string
event:
type: object
additionalProperties: False
properties:
attributes:
description: Explicit list of the attributes for the notification.
type: array
items:
type: string
mcgrp:
description: Name of the multicast group generating given notification.
type: string
mcast-groups:
description: List of multicast groups.
type: object
required: [ list ]
additionalProperties: False
properties:
list:
description: List of groups.
type: array
items:
type: object
required: [ name ]
additionalProperties: False
properties:
name:
description: |
The name for the group, used to form the define and the value of the define.
type: string
# Start genetlink-c
c-define-name:
description: Override for the name of the define in C uAPI.
type: string
# End genetlink-c
flags: *cmd_flags
# Start netlink-raw
value:
description: Value of the netlink multicast group in the uAPI.
type: integer
# End netlink-raw

View File

@@ -0,0 +1,179 @@
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
name: rt-addr
protocol: netlink-raw
protonum: 0
doc:
Address configuration over rtnetlink.
definitions:
-
name: ifaddrmsg
type: struct
members:
-
name: ifa-family
type: u8
-
name: ifa-prefixlen
type: u8
-
name: ifa-flags
type: u8
enum: ifa-flags
enum-as-flags: true
-
name: ifa-scope
type: u8
-
name: ifa-index
type: u32
-
name: ifa-cacheinfo
type: struct
members:
-
name: ifa-prefered
type: u32
-
name: ifa-valid
type: u32
-
name: cstamp
type: u32
-
name: tstamp
type: u32
-
name: ifa-flags
type: flags
entries:
-
name: secondary
-
name: nodad
-
name: optimistic
-
name: dadfailed
-
name: homeaddress
-
name: deprecated
-
name: tentative
-
name: permanent
-
name: managetempaddr
-
name: noprefixroute
-
name: mcautojoin
-
name: stable-privacy
attribute-sets:
-
name: addr-attrs
attributes:
-
name: ifa-address
type: binary
display-hint: ipv4
-
name: ifa-local
type: binary
display-hint: ipv4
-
name: ifa-label
type: string
-
name: ifa-broadcast
type: binary
display-hint: ipv4
-
name: ifa-anycast
type: binary
-
name: ifa-cacheinfo
type: binary
struct: ifa-cacheinfo
-
name: ifa-multicast
type: binary
-
name: ifa-flags
type: u32
enum: ifa-flags
enum-as-flags: true
-
name: ifa-rt-priority
type: u32
-
name: ifa-target-netnsid
type: binary
-
name: ifa-proto
type: u8
operations:
fixed-header: ifaddrmsg
enum-model: directional
list:
-
name: newaddr
doc: Add new address
attribute-set: addr-attrs
do:
request:
value: 20
attributes: &ifaddr-all
- ifa-family
- ifa-flags
- ifa-prefixlen
- ifa-scope
- ifa-index
- ifa-address
- ifa-label
- ifa-local
- ifa-cacheinfo
-
name: deladdr
doc: Remove address
attribute-set: addr-attrs
do:
request:
value: 21
attributes:
- ifa-family
- ifa-flags
- ifa-prefixlen
- ifa-scope
- ifa-index
- ifa-address
- ifa-local
-
name: getaddr
doc: Dump address information.
attribute-set: addr-attrs
dump:
request:
value: 22
attributes:
- ifa-index
reply:
value: 20
attributes: *ifaddr-all
mcast-groups:
list:
-
name: rtnlgrp-ipv4-ifaddr
value: 5
-
name: rtnlgrp-ipv6-ifaddr
value: 9

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,327 @@
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
name: rt-route
protocol: netlink-raw
protonum: 0
doc:
Route configuration over rtnetlink.
definitions:
-
name: rtm-type
name-prefix: rtn-
type: enum
entries:
- unspec
- unicast
- local
- broadcast
- anycast
- multicast
- blackhole
- unreachable
- prohibit
- throw
- nat
- xresolve
-
name: rtmsg
type: struct
members:
-
name: rtm-family
type: u8
-
name: rtm-dst-len
type: u8
-
name: rtm-src-len
type: u8
-
name: rtm-tos
type: u8
-
name: rtm-table
type: u8
-
name: rtm-protocol
type: u8
-
name: rtm-scope
type: u8
-
name: rtm-type
type: u8
enum: rtm-type
-
name: rtm-flags
type: u32
-
name: rta-cacheinfo
type: struct
members:
-
name: rta-clntref
type: u32
-
name: rta-lastuse
type: u32
-
name: rta-expires
type: u32
-
name: rta-error
type: u32
-
name: rta-used
type: u32
attribute-sets:
-
name: route-attrs
attributes:
-
name: rta-dst
type: binary
display-hint: ipv4
-
name: rta-src
type: binary
display-hint: ipv4
-
name: rta-iif
type: u32
-
name: rta-oif
type: u32
-
name: rta-gateway
type: binary
display-hint: ipv4
-
name: rta-priority
type: u32
-
name: rta-prefsrc
type: binary
display-hint: ipv4
-
name: rta-metrics
type: nest
nested-attributes: rta-metrics
-
name: rta-multipath
type: binary
-
name: rta-protoinfo # not used
type: binary
-
name: rta-flow
type: u32
-
name: rta-cacheinfo
type: binary
struct: rta-cacheinfo
-
name: rta-session # not used
type: binary
-
name: rta-mp-algo # not used
type: binary
-
name: rta-table
type: u32
-
name: rta-mark
type: u32
-
name: rta-mfc-stats
type: binary
-
name: rta-via
type: binary
-
name: rta-newdst
type: binary
-
name: rta-pref
type: u8
-
name: rta-encap-type
type: u16
-
name: rta-encap
type: binary # tunnel specific nest
-
name: rta-expires
type: u32
-
name: rta-pad
type: binary
-
name: rta-uid
type: u32
-
name: rta-ttl-propagate
type: u8
-
name: rta-ip-proto
type: u8
-
name: rta-sport
type: u16
-
name: rta-dport
type: u16
-
name: rta-nh-id
type: u32
-
name: rta-metrics
attributes:
-
name: rtax-unspec
type: unused
value: 0
-
name: rtax-lock
type: u32
-
name: rtax-mtu
type: u32
-
name: rtax-window
type: u32
-
name: rtax-rtt
type: u32
-
name: rtax-rttvar
type: u32
-
name: rtax-ssthresh
type: u32
-
name: rtax-cwnd
type: u32
-
name: rtax-advmss
type: u32
-
name: rtax-reordering
type: u32
-
name: rtax-hoplimit
type: u32
-
name: rtax-initcwnd
type: u32
-
name: rtax-features
type: u32
-
name: rtax-rto-min
type: u32
-
name: rtax-initrwnd
type: u32
-
name: rtax-quickack
type: u32
-
name: rtax-cc-algo
type: string
-
name: rtax-fastopen-no-cookie
type: u32
operations:
enum-model: directional
list:
-
name: getroute
doc: Dump route information.
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 26
attributes:
- rtm-family
- rta-src
- rtm-src-len
- rta-dst
- rtm-dst-len
- rta-iif
- rta-oif
- rta-ip-proto
- rta-sport
- rta-dport
- rta-mark
- rta-uid
reply:
value: 24
attributes: &all-route-attrs
- rtm-family
- rtm-dst-len
- rtm-src-len
- rtm-tos
- rtm-table
- rtm-protocol
- rtm-scope
- rtm-type
- rtm-flags
- rta-dst
- rta-src
- rta-iif
- rta-oif
- rta-gateway
- rta-priority
- rta-prefsrc
- rta-metrics
- rta-multipath
- rta-flow
- rta-cacheinfo
- rta-table
- rta-mark
- rta-mfc-stats
- rta-via
- rta-newdst
- rta-pref
- rta-encap-type
- rta-encap
- rta-expires
- rta-pad
- rta-uid
- rta-ttl-propagate
- rta-ip-proto
- rta-sport
- rta-dport
- rta-nh-id
dump:
request:
value: 26
attributes:
- rtm-family
reply:
value: 24
attributes: *all-route-attrs
-
name: newroute
doc: Create a new route
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 24
attributes: *all-route-attrs
-
name: delroute
doc: Delete an existing route
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 25
attributes: *all-route-attrs

View File

@@ -8,11 +8,8 @@ This document describes the many additional quirks and properties
required to describe older Generic Netlink families which form
the ``genetlink-legacy`` protocol level.
The spec is a work in progress, some of the quirks are just documented
for future reference.
Specification (defined)
=======================
Specification
=============
Attribute type nests
--------------------
@@ -156,16 +153,27 @@ it will be allocated 3 for the request (``a`` is the previous operation
with a request section and the value of 2) and 8 for response (``c`` is
the previous operation in the "from-kernel" direction).
Other quirks (todo)
===================
Other quirks
============
Structures
----------
Legacy families can define C structures both to be used as the contents of
an attribute and as a fixed message header. Structures are defined in
``definitions`` and referenced in operations or attributes. Note that
structures defined in YAML are implicitly packed according to C
``definitions`` and referenced in operations or attributes.
members
~~~~~~~
- ``name`` - The attribute name of the struct member
- ``type`` - One of the scalar types ``u8``, ``u16``, ``u32``, ``u64``, ``s8``,
``s16``, ``s32``, ``s64``, ``string`` or ``binary``.
- ``byte-order`` - ``big-endian`` or ``little-endian``
- ``doc``, ``enum``, ``enum-as-flags``, ``display-hint`` - Same as for
:ref:`attribute definitions <attribute_properties>`
Note that structures defined in YAML are implicitly packed according to C
conventions. For example, the following struct is 4 bytes, not 6 bytes:
.. code-block:: c

View File

@@ -14,5 +14,6 @@ Netlink documentation for users.
specs
c-code-gen
genetlink-legacy
netlink-raw
See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>`.

View File

@@ -0,0 +1,58 @@
.. SPDX-License-Identifier: BSD-3-Clause
======================================================
Netlink specification support for raw Netlink families
======================================================
This document describes the additional properties required by raw Netlink
families such as ``NETLINK_ROUTE`` which use the ``netlink-raw`` protocol
specification.
Specification
=============
The netlink-raw schema extends the :doc:`genetlink-legacy <genetlink-legacy>`
schema with properties that are needed to specify the protocol numbers and
multicast IDs used by raw netlink families. See :ref:`classic_netlink` for more
information.
Globals
-------
protonum
~~~~~~~~
The ``protonum`` property is used to specify the protocol number to use when
opening a netlink socket.
.. code-block:: yaml
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
name: rt-addr
protocol: netlink-raw
protonum: 0 # part of the NETLINK_ROUTE protocol
Multicast group properties
--------------------------
value
~~~~~
The ``value`` property is used to specify the group ID to use for multicast
group registration.
.. code-block:: yaml
mcast-groups:
list:
-
name: rtnlgrp-ipv4-ifaddr
value: 5
-
name: rtnlgrp-ipv6-ifaddr
value: 9
-
name: rtnlgrp-mctp-ifaddr
value: 34

View File

@@ -68,6 +68,10 @@ The following sections describe the properties of the most modern ``genetlink``
schema. See the documentation of :doc:`genetlink-c <c-code-gen>`
for information on how C names are derived from name properties.
See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>` for
information on the Netlink specification properties that are only relevant to
the kernel space and not part of the user space API.
genetlink
=========
@@ -180,6 +184,8 @@ attributes
List of attributes in the set.
.. _attribute_properties:
Attribute properties
--------------------
@@ -264,6 +270,13 @@ a C array of u32 values can be specified with ``type: binary`` and
``sub-type: u32``. Binary types and legacy array formats are described in
more detail in :doc:`genetlink-legacy`.
display-hint
~~~~~~~~~~~~
Optional format indicator that is intended only for choosing the right
formatting mechanism when displaying values of this type. Currently supported
hints are ``hex``, ``mac``, ``fddi``, ``ipv4``, ``ipv6`` and ``uuid``.
operations
----------

View File

@@ -6,7 +6,7 @@ import json
import pprint
import time
from lib import YnlFamily
from lib import YnlFamily, Netlink
def main():
@@ -19,6 +19,14 @@ def main():
parser.add_argument('--dump', dest='dump', type=str)
parser.add_argument('--sleep', dest='sleep', type=int)
parser.add_argument('--subscribe', dest='ntf', type=str)
parser.add_argument('--replace', dest='flags', action='append_const',
const=Netlink.NLM_F_REPLACE)
parser.add_argument('--excl', dest='flags', action='append_const',
const=Netlink.NLM_F_EXCL)
parser.add_argument('--create', dest='flags', action='append_const',
const=Netlink.NLM_F_CREATE)
parser.add_argument('--append', dest='flags', action='append_const',
const=Netlink.NLM_F_APPEND)
args = parser.parse_args()
if args.no_schema:
@@ -37,7 +45,7 @@ def main():
time.sleep(args.sleep)
if args.do:
reply = ynl.do(args.do, attrs)
reply = ynl.do(args.do, attrs, args.flags)
pprint.PrettyPrinter().pprint(reply)
if args.dump:
reply = ynl.dump(args.dump, attrs)

View File

@@ -2,7 +2,7 @@
from .nlspec import SpecAttr, SpecAttrSet, SpecEnumEntry, SpecEnumSet, \
SpecFamily, SpecOperation
from .ynl import YnlFamily
from .ynl import YnlFamily, Netlink
__all__ = ["SpecAttr", "SpecAttrSet", "SpecEnumEntry", "SpecEnumSet",
"SpecFamily", "SpecOperation", "YnlFamily"]
"SpecFamily", "SpecOperation", "YnlFamily", "Netlink"]

View File

@@ -322,6 +322,26 @@ class SpecOperation(SpecElement):
self.attr_set = self.family.attr_sets[attr_set_name]
class SpecMcastGroup(SpecElement):
"""Netlink Multicast Group
Information about a multicast group.
Value is only used for classic netlink families that use the
netlink-raw schema. Genetlink families use dynamic ID allocation
where the ids of multicast groups get resolved at runtime. Value
will be None for genetlink families.
Attributes:
name name of the mulitcast group
value integer id of this multicast group for netlink-raw or None
yaml raw spec as loaded from the spec file
"""
def __init__(self, family, yaml):
super().__init__(family, yaml)
self.value = self.yaml.get('value')
class SpecFamily(SpecElement):
""" Netlink Family Spec class.
@@ -343,6 +363,7 @@ class SpecFamily(SpecElement):
ntfs dict of all async events
consts dict of all constants/enums
fixed_header string, optional name of family default fixed header struct
mcast_groups dict of all multicast groups (index by name)
"""
def __init__(self, spec_path, schema_path=None, exclude_ops=None):
with open(spec_path, "r") as stream:
@@ -384,6 +405,7 @@ class SpecFamily(SpecElement):
self.ops = collections.OrderedDict()
self.ntfs = collections.OrderedDict()
self.consts = collections.OrderedDict()
self.mcast_groups = collections.OrderedDict()
last_exception = None
while len(self._resolution_list) > 0:
@@ -416,6 +438,9 @@ class SpecFamily(SpecElement):
def new_operation(self, elem, req_val, rsp_val):
return SpecOperation(self, elem, req_val, rsp_val)
def new_mcast_group(self, elem):
return SpecMcastGroup(self, elem)
def add_unresolved(self, elem):
self._resolution_list.append(elem)
@@ -512,3 +537,9 @@ class SpecFamily(SpecElement):
self.ops[op.name] = op
elif op.is_async:
self.ntfs[op.name] = op
mcgs = self.yaml.get('mcast-groups')
if mcgs:
for elem in mcgs['list']:
mcg = self.new_mcast_group(elem)
self.mcast_groups[elem['name']] = mcg

View File

@@ -25,6 +25,7 @@ class Netlink:
NETLINK_ADD_MEMBERSHIP = 1
NETLINK_CAP_ACK = 10
NETLINK_EXT_ACK = 11
NETLINK_GET_STRICT_CHK = 12
# Netlink message
NLMSG_ERROR = 2
@@ -34,6 +35,10 @@ class Netlink:
NLM_F_ACK = 4
NLM_F_ROOT = 0x100
NLM_F_MATCH = 0x200
NLM_F_REPLACE = 0x100
NLM_F_EXCL = 0x200
NLM_F_CREATE = 0x400
NLM_F_APPEND = 0x800
NLM_F_CAPPED = 0x100
@@ -228,6 +233,9 @@ class NlMsg:
desc += f" ({spec['doc']})"
self.extack['miss-type'] = desc
def cmd(self):
return self.nl_type
def __repr__(self):
msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}\n"
if self.error:
@@ -293,7 +301,7 @@ def _genl_load_families():
gm = GenlMsg(nl_msg)
fam = dict()
for attr in gm.raw_attrs:
for attr in NlAttrs(gm.raw):
if attr.type == Netlink.CTRL_ATTR_FAMILY_ID:
fam['id'] = attr.as_scalar('u16')
elif attr.type == Netlink.CTRL_ATTR_FAMILY_NAME:
@@ -317,23 +325,13 @@ def _genl_load_families():
class GenlMsg:
def __init__(self, nl_msg, fixed_header_members=[]):
def __init__(self, nl_msg):
self.nl = nl_msg
self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
self.raw = nl_msg.raw[4:]
self.hdr = nl_msg.raw[0:4]
offset = 4
self.genl_cmd, self.genl_version, _ = struct.unpack("BBH", self.hdr)
self.fixed_header_attrs = dict()
for m in fixed_header_members:
format = NlAttr.get_format(m.type, m.byte_order)
decoded = format.unpack_from(nl_msg.raw, offset)
offset += format.size
self.fixed_header_attrs[m.name] = decoded[0]
self.raw = nl_msg.raw[offset:]
self.raw_attrs = NlAttrs(self.raw)
def cmd(self):
return self.genl_cmd
def __repr__(self):
msg = repr(self.nl)
@@ -343,9 +341,41 @@ class GenlMsg:
return msg
class GenlFamily:
def __init__(self, family_name):
class NetlinkProtocol:
def __init__(self, family_name, proto_num):
self.family_name = family_name
self.proto_num = proto_num
def _message(self, nl_type, nl_flags, seq=None):
if seq is None:
seq = random.randint(1, 1024)
nlmsg = struct.pack("HHII", nl_type, nl_flags, seq, 0)
return nlmsg
def message(self, flags, command, version, seq=None):
return self._message(command, flags, seq)
def _decode(self, nl_msg):
return nl_msg
def decode(self, ynl, nl_msg):
msg = self._decode(nl_msg)
fixed_header_size = 0
if ynl:
op = ynl.rsp_by_value[msg.cmd()]
fixed_header_size = ynl._fixed_header_size(op)
msg.raw_attrs = NlAttrs(msg.raw[fixed_header_size:])
return msg
def get_mcast_id(self, mcast_name, mcast_groups):
if mcast_name not in mcast_groups:
raise Exception(f'Multicast group "{mcast_name}" not present in the spec')
return mcast_groups[mcast_name].value
class GenlProtocol(NetlinkProtocol):
def __init__(self, family_name):
super().__init__(family_name, Netlink.NETLINK_GENERIC)
global genl_family_name_to_id
if genl_family_name_to_id is None:
@@ -354,6 +384,19 @@ class GenlFamily:
self.genl_family = genl_family_name_to_id[family_name]
self.family_id = genl_family_name_to_id[family_name]['id']
def message(self, flags, command, version, seq=None):
nlmsg = self._message(self.family_id, flags, seq)
genlmsg = struct.pack("BBH", command, version, 0)
return nlmsg + genlmsg
def _decode(self, nl_msg):
return GenlMsg(nl_msg)
def get_mcast_id(self, mcast_name, mcast_groups):
if mcast_name not in self.genl_family['mcast']:
raise Exception(f'Multicast group "{mcast_name}" not present in the family')
return self.genl_family['mcast'][mcast_name]
#
# YNL implementation details.
@@ -366,9 +409,19 @@ class YnlFamily(SpecFamily):
self.include_raw = False
self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, Netlink.NETLINK_GENERIC)
try:
if self.proto == "netlink-raw":
self.nlproto = NetlinkProtocol(self.yaml['name'],
self.yaml['protonum'])
else:
self.nlproto = GenlProtocol(self.yaml['name'])
except KeyError:
raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
self.sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, self.nlproto.proto_num)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_CAP_ACK, 1)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_EXT_ACK, 1)
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1)
self.async_msg_ids = set()
self.async_msg_queue = []
@@ -381,18 +434,12 @@ class YnlFamily(SpecFamily):
bound_f = functools.partial(self._op, op_name)
setattr(self, op.ident_name, bound_f)
try:
self.family = GenlFamily(self.yaml['name'])
except KeyError:
raise Exception(f"Family '{self.yaml['name']}' not supported by the kernel")
def ntf_subscribe(self, mcast_name):
if mcast_name not in self.family.genl_family['mcast']:
raise Exception(f'Multicast group "{mcast_name}" not present in the family')
mcast_id = self.nlproto.get_mcast_id(mcast_name, self.mcast_groups)
self.sock.bind((0, 0))
self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP,
self.family.genl_family['mcast'][mcast_name])
mcast_id)
def _add_attr(self, space, name, value):
try:
@@ -454,6 +501,17 @@ class YnlFamily(SpecFamily):
decoded = NlAttr.formatted_string(decoded, attr_spec.display_hint)
return decoded
def _decode_array_nest(self, attr, attr_spec):
decoded = []
offset = 0
while offset < len(attr.raw):
item = NlAttr(attr.raw, offset)
offset += item.full_len
subattrs = self._decode(NlAttrs(item.raw), attr_spec['nested-attributes'])
decoded.append({ item.type: subattrs })
return decoded
def _decode(self, attrs, space):
attr_space = self.attr_sets[space]
rsp = dict()
@@ -473,6 +531,8 @@ class YnlFamily(SpecFamily):
decoded = True
elif attr_spec["type"] in NlAttr.type_formats:
decoded = attr.as_scalar(attr_spec['type'], attr_spec.byte_order)
elif attr_spec["type"] == 'array-nest':
decoded = self._decode_array_nest(attr, attr_spec)
else:
raise Exception(f'Unknown {attr_spec["type"]} with name {attr_spec["name"]}')
@@ -514,25 +574,53 @@ class YnlFamily(SpecFamily):
return None
def _decode_extack(self, request, attr_space, extack):
def _decode_extack(self, request, op, extack):
if 'bad-attr-offs' not in extack:
return
genl_req = GenlMsg(NlMsg(request, 0, attr_space=attr_space))
path = self._decode_extack_path(genl_req.raw_attrs, attr_space,
20, extack['bad-attr-offs'])
msg = self.nlproto.decode(self, NlMsg(request, 0, op.attr_set))
offset = 20 + self._fixed_header_size(op)
path = self._decode_extack_path(msg.raw_attrs, op.attr_set, offset,
extack['bad-attr-offs'])
if path:
del extack['bad-attr-offs']
extack['bad-attr'] = path
def handle_ntf(self, nl_msg, genl_msg):
def _fixed_header_size(self, op):
if op.fixed_header:
fixed_header_members = self.consts[op.fixed_header].members
size = 0
for m in fixed_header_members:
format = NlAttr.get_format(m.type, m.byte_order)
size += format.size
return size
else:
return 0
def _decode_fixed_header(self, msg, name):
fixed_header_members = self.consts[name].members
fixed_header_attrs = dict()
offset = 0
for m in fixed_header_members:
format = NlAttr.get_format(m.type, m.byte_order)
[ value ] = format.unpack_from(msg.raw, offset)
offset += format.size
if m.enum:
value = self._decode_enum(value, m)
fixed_header_attrs[m.name] = value
return fixed_header_attrs
def handle_ntf(self, decoded):
msg = dict()
if self.include_raw:
msg['nlmsg'] = nl_msg
msg['genlmsg'] = genl_msg
op = self.rsp_by_value[genl_msg.genl_cmd]
msg['raw'] = decoded
op = self.rsp_by_value[decoded.cmd()]
attrs = self._decode(decoded.raw_attrs, op.attr_set.name)
if op.fixed_header:
attrs.update(self._decode_fixed_header(decoded, op.fixed_header))
msg['name'] = op['name']
msg['msg'] = self._decode(genl_msg.raw_attrs, op.attr_set.name)
msg['msg'] = attrs
self.async_msg_queue.append(msg)
def check_ntf(self):
@@ -552,12 +640,12 @@ class YnlFamily(SpecFamily):
print("Netlink done while checking for ntf!?")
continue
gm = GenlMsg(nl_msg)
if gm.genl_cmd not in self.async_msg_ids:
print("Unexpected msg id done while checking for ntf", gm)
decoded = self.nlproto.decode(self, nl_msg)
if decoded.cmd() not in self.async_msg_ids:
print("Unexpected msg id done while checking for ntf", decoded)
continue
self.handle_ntf(nl_msg, gm)
self.handle_ntf(decoded)
def operation_do_attributes(self, name):
"""
@@ -570,15 +658,17 @@ class YnlFamily(SpecFamily):
return op['do']['request']['attributes'].copy()
def _op(self, method, vals, dump=False):
def _op(self, method, vals, flags, dump=False):
op = self.ops[method]
nl_flags = Netlink.NLM_F_REQUEST | Netlink.NLM_F_ACK
for flag in flags or []:
nl_flags |= flag
if dump:
nl_flags |= Netlink.NLM_F_DUMP
req_seq = random.randint(1024, 65535)
msg = _genl_msg(self.family.family_id, nl_flags, op.req_value, 1, req_seq)
msg = self.nlproto.message(nl_flags, op.req_value, 1, req_seq)
fixed_header_members = []
if op.fixed_header:
fixed_header_members = self.consts[op.fixed_header].members
@@ -599,7 +689,7 @@ class YnlFamily(SpecFamily):
nms = NlMsgs(reply, attr_space=op.attr_set)
for nl_msg in nms:
if nl_msg.extack:
self._decode_extack(msg, op.attr_set, nl_msg.extack)
self._decode_extack(msg, op, nl_msg.extack)
if nl_msg.error:
raise NlError(nl_msg)
@@ -610,18 +700,20 @@ class YnlFamily(SpecFamily):
done = True
break
gm = GenlMsg(nl_msg, fixed_header_members)
decoded = self.nlproto.decode(self, nl_msg)
# Check if this is a reply to our request
if nl_msg.nl_seq != req_seq or gm.genl_cmd != op.rsp_value:
if gm.genl_cmd in self.async_msg_ids:
self.handle_ntf(nl_msg, gm)
if nl_msg.nl_seq != req_seq or decoded.cmd() != op.rsp_value:
if decoded.cmd() in self.async_msg_ids:
self.handle_ntf(decoded)
continue
else:
print('Unexpected message: ' + repr(gm))
print('Unexpected message: ' + repr(decoded))
continue
rsp_msg = self._decode(gm.raw_attrs, op.attr_set.name)
rsp_msg.update(gm.fixed_header_attrs)
rsp_msg = self._decode(decoded.raw_attrs, op.attr_set.name)
if op.fixed_header:
rsp_msg.update(self._decode_fixed_header(decoded, op.fixed_header))
rsp.append(rsp_msg)
if not rsp:
@@ -630,8 +722,8 @@ class YnlFamily(SpecFamily):
return rsp[0]
return rsp
def do(self, method, vals):
return self._op(method, vals)
def do(self, method, vals, flags):
return self._op(method, vals, flags)
def dump(self, method, vals):
return self._op(method, vals, dump=True)
return self._op(method, vals, [], dump=True)