tools: ynl: add ipv4-or-v6 display hint

The attribute WGALLOWEDIP_A_IPADDR can contain either an IPv4
or an IPv6 address depending on WGALLOWEDIP_A_FAMILY, however
in practice it is enough to look at the attribute length.

This patch implements an ipv4-or-v6 display hint, that can
deal with this kind of attribute.

It only implements this display hint for genetlink-legacy, it
can be added to other protocol variants if needed, but we don't
want to encourage it's use.

Signed-off-by: Asbjørn Sloth Tønnesen <ast@fiberby.net>
Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Link: https://patch.msgid.link/20250915144301.725949-12-ast@fiberby.net
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Asbjørn Sloth Tønnesen 2025-09-15 14:42:56 +00:00 committed by Jakub Kicinski
parent 52550d518d
commit 1b255e1bea
2 changed files with 3 additions and 3 deletions

View File

@ -154,7 +154,7 @@ properties:
Optional format indicator that is intended only for choosing Optional format indicator that is intended only for choosing
the right formatting mechanism when displaying values of this the right formatting mechanism when displaying values of this
type. type.
enum: [ hex, mac, fddi, ipv4, ipv6, uuid ] enum: [ hex, mac, fddi, ipv4, ipv6, ipv4-or-v6, uuid ]
struct: struct:
description: Name of the nested struct type. description: Name of the nested struct type.
type: string type: string

View File

@ -956,7 +956,7 @@ class YnlFamily(SpecFamily):
formatted = hex(raw) formatted = hex(raw)
else: else:
formatted = bytes.hex(raw, ' ') formatted = bytes.hex(raw, ' ')
elif display_hint in [ 'ipv4', 'ipv6' ]: elif display_hint in [ 'ipv4', 'ipv6', 'ipv4-or-v6' ]:
formatted = format(ipaddress.ip_address(raw)) formatted = format(ipaddress.ip_address(raw))
elif display_hint == 'uuid': elif display_hint == 'uuid':
formatted = str(uuid.UUID(bytes=raw)) formatted = str(uuid.UUID(bytes=raw))
@ -965,7 +965,7 @@ class YnlFamily(SpecFamily):
return formatted return formatted
def _from_string(self, string, attr_spec): def _from_string(self, string, attr_spec):
if attr_spec.display_hint in ['ipv4', 'ipv6']: if attr_spec.display_hint in ['ipv4', 'ipv6', 'ipv4-or-v6']:
ip = ipaddress.ip_address(string) ip = ipaddress.ip_address(string)
if attr_spec['type'] == 'binary': if attr_spec['type'] == 'binary':
raw = ip.packed raw = ip.packed