agetty: Implement netlink based IP address lookup
PATCH 1/1 agetty: Implement netlink based IP address lookup
The current implementation of the IP address lookup in agetty is inferior:
The network change watch uses netlink. The netlink message contains all needed information. But the netlink message is not used at all. To get an IP address for the \4 and \6 issue file escapes, getifaddrs() is used instead. But getifaddrs() is inferior to netlink, especially for IPv6. It does not provide sufficient information to discriminate between an useful global IPv6 address and an ephemeral IPv6 address that will expire in few seconds. And finally, the current implementation does not support lookup over all interfaces. External helpers like issue-generator are needed.
The new netlink based IP address lokup implements a new issue file escape \i. This lookup is based on netlink messages only. It evaluates all addresses reported in the netlink messages and it attempts to print only the best of them. As it is not possible to discriminate the "best" IP address, it reports all reasonably usable addresses.
To do:
Migrate \4 and \6 from getifaddrs() to netlink as well, and then completely drop getifaddrs() based code.
It could be useful to implement:
- configurable limit for the maximum number of interfaces that could be reported.
- the interface regexp match
- an interface template for fine tuning of the output
- better control over the output: e. g. disable printing of IPv4 or IPv6 addresses, disable printing of temporary addresses or link-local addresses
- cross-protocol checks: e. g. disable reporting of link-local IPv6 address for an IPv4-only interface
Note:
The new network comparison code could potentially render obsolete string based issue comparison (implemented in 6522d88). There are ifaces_list_change_4 and ifaces_list_change_6 variables ready for this purpose.
--- term-utils/agetty.8.adoc | 3 + term-utils/agetty.c | 405 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 388 insertions(+), 20 deletions(-)
term-utils/agetty.8.adoc +3 -0
@@ -262,6 +262,9 @@ Insert the system name (the name of the operating system). Same as 'uname -s'. S S or S{VARIABLE}:: Insert the VARIABLE data from _/etc/os-release_. If this file does not exist then fall back to _/usr/lib/os-release_. If the VARIABLE argument is not specified, then use PRETTY_NAME from the file or the system name (see \s). This escape code can be used to keep _/etc/issue_ distribution and release independent. Note that \S{ANSI_COLOR} is converted to the real terminal escape sequence. +i:: +Dump usable IPv4 and IPv4 addresses of the all network interfaces. It dumps the global or site IP address, if it exists. If it does not exist, it dumps the temporary interface with the longest lifetime. If it does not exist it dumps the link-local IP address. The IPv6 link-local interface of IPv6 is reported as {IPv6 address}@{interface on a local machine}, but for a real use, the inteface on a local machine needs to be replaced by the interface on a remote machine. To prevent overflooding of the interface dump, the feature automatically disables itself, if the number of usable interfaces exceeds 12. + l:: Insert the name of the current tty line.
term-utils/agetty.c +385 -20
471 lines not on this page · raw message
Signature · 1 line
2.48.1
Karel Zak
maintainerQuoted 5 lines · Stanislav Brabec
The new netlink based IP address lokup implements a new issue file escape \i. This lookup is based on netlink messages only. It evaluates all addresses reported in the netlink messages and it attempts to print only the best of them. As it is not possible to discriminate the "best" IP address, it reports all reasonably usable addresses.
I believe your code is a good start.
Quoted 2 lines
Migrate \4 and \6 from
getifaddrs()to netlink as well, and then completely dropgetifaddrs()based code.
Yes, but it would be nice to make the code more readable and extendable :-)
My suggestion:
- Move the code to lib/netlink-ifaces.c.
- Add #ifdef TEST_PROGRAM with test main() and test_netlink to lib/Makemodule.am (see for example lib/linux_version.c).
- Use include/debug.h and UL_NETLINK_DEBUG= environment variable (see lib/loopdev.c, LOOPDEV_DEBUG_* and loopdev_init_debug()).
- Use a prefix for public functions (e.g., ul_iface_*).
- Create a set of small functions for basic operations:
- Add and remove interface (e.g., add_interface()).
- Add and remove address.
- Get the best address.
I also suggest using include/list.h for the lists, but you might find it challenging at first. Ideally, define next() functions for while() loops (e.g., ul_iface_next_interface() and ul_iface_next_addr()). See for example libfdisk/src/table.c:fdisk_table_next_partition(). However, I'll understand if you think it's overkill.
The include/list.h provides a sort function, so it would be possible to sort the lists by "quality", making the first address in the list the best.
Quoted 3 lines
It could be useful to implement:
- configurable limit for the maximum number of interfaces that could be reported.
I believe the limit could be quite large by default (512?) since it's just one malloc() for each address.
Quoted 1 line
- the interface regexp match
Yes, just another small function ul_iface_match_interface(), and if it will be sorted than you get the best one :-)
\4{~eth[0-9]} or \4{match="~eth[0-9]"} if you want to support more options in the { }.
- agetty already supports \foo{ } arguments, see get_escape_argument()
- lib/strutils.c:ul_optstr_next() can parse options list {a="aaa",b="bbb"}
Quoted 1 line
- an interface template for fine tuning of the output
You can introduce printf()-like \4{format="%iface:%addr} (like git-log)
Quoted 4 lines
- better control over the output: e. g. disable printing of IPv4 or IPv6 addresses, disable printing of temporary addresses or link-local addresses
- cross-protocol checks: e. g. disable reporting of link-local IPv6 address for an IPv4-only interface
:-)
Quoted 3 lines
The new network comparison code could potentially render obsolete string based issue comparison (implemented in 6522d88). There are
ifaces_list_change_4andifaces_list_change_6variables ready for this purpose.
Hmm... the global variables used deeply within the netlink message parser do not look very elegant. Maybe introduce a separate struct, ul_ifaces_status, and use it as a global variable in agetty.c, then call process_netlink_msg() with this struct as an argument.
term-utils/agetty.c line 166
Quoted 4 lines
term-utils/agetty.c +4
-0+struct ip_quality_item { +enum ip_quality_item_value quality; +int len; +__u32 ifa_valid;/* IP addres lifetime */
uint32_t if possible ;-)
term-utils/agetty.c line 179
Quoted 13 lines
term-utils/agetty.c +13
-0+struct ip_quality_item *next; +char addr[]; +}; +struct iface_quality_item { +__u32 ifa_index; +struct ip_quality_item *ip_quality_list_4; +struct ip_quality_item *ip_quality_list_6; +struct iface_quality_item *next; +}; +static struct iface_quality_item *ifaces_list =NULL ; +bool ifaces_list_change_4; +bool ifaces_list_change_6; +bool ifaces_skip_dump = false;
Do we need the _quality_ in the sctruct names? What about ul_iface and ul_iface_addr ?
...
term-utils/agetty.c static void open_netlink(void) · line 1836
Quoted 2 lines
term-utils/agetty.c +2
-0+debug_net ("+ allocating new interface\n" ); + ifaceq =malloc (sizeof (struct iface_quality_item ));
if (!ifaceq)
return;
term-utils/agetty.c static void open_netlink(void) · line 1841
Quoted 5 lines
term-utils/agetty.c +5
-0+ ifaceq->ifa_index = ifaddr->ifa_index; + ifaceq->ip_quality_list_4 =NULL ; + ifaceq->ip_quality_list_6 =NULL ; + ifaceq->next =NULL ; + *ifaceq_prev_next = ifaceq;
process_netlink_msg_part() will be simpler and more readable if you move the basic tasks to small functions like add_interface() :-)
Quoted 4 lines
term-utils/agetty.c +4
-0+ }else { +/* Should never happen */ +debug_net ("- interface not found\n" ); +return ;
term-utils/agetty.c static void open_netlink(void) · line 1855
Quoted 10 lines
term-utils/agetty.c +10
-0+ } + } +if (family ==AF_INET ) { + ipq_prev_next = &(ifaceq->ip_quality_list_4); + ifaces_list_change = &ifaces_list_change_4; + } +if (family ==AF_INET6 ) { + ipq_prev_next = &(ifaceq->ip_quality_list_6); + ifaces_list_change = &ifaces_list_change_6; + }
I guess static analyzers won't like this part; ipq_prev_next is uninitialized if the family is something else.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 6 lines · Karel Zak
On Sun, Feb 16, 2025 at 10:24:50PM GMT, Stanislav Brabec wrote:
My suggestion:
- Move the code to
lib/netlink-ifaces.c.
Quoted 7 lines
- Add #ifdef
TEST_PROGRAMwith testmain()andtest_netlinktolib/Makemodule.am(see for examplelib/linux_version.c).- Use
include/debug.hand UL_NETLINK_DEBUG= environment variable (seelib/loopdev.c, LOOPDEV_DEBUG_* andloopdev_init_debug()).- Use a prefix for public functions (e.g., ul_iface_*).
Makes sense.
Quoted 4 lines
- Create a set of small functions for basic operations:
- Add and remove interface (e.g.,add_interface()).
- Add and remove address.
- Get the best address.
It could be done. Just add and delete interface shares lot of code, so it could be one function.
Quoted 9 lines
I also suggest using
include/list.hfor the lists, but you might find it challenging at first. Ideally, definenext()functions forwhile()loops (e.g.,ul_iface_next_interface()andul_iface_next_addr()). See for example libfdisk/src/table.c:fdisk_table_next_partition(). However, I'll understand if you think it's overkill.The
include/list.hprovides a sort function, so it would be possible to sort the lists by "quality", making the first address in the list the best.
Sounds good. I'll look at it.
Quoted 5 lines
Quoted 3 lines
It could be useful to implement:
- configurable limit for the maximum number of interfaces that could be reported.I believe the limit could be quite large by default (512?) since it's just one
malloc()for each address.
Not for agetty. 512 interfaces surely ovefloods all terminals screen and maybe even scrollback memory.
And if you have 512 network interfaces (e. g. modem servers at DSL providers), then you probably will not want to report all those interfaces IP addresses. But yes, the code could simply support it.
Quoted 6 lines
Quoted 1 line
- the interface regexp match
Yes, just another small function
ul_iface_match_interface(), and if it will be sorted than you get the best one :-)\4{~eth[0-9]} or \4{match="~eth[0-9]"} if you want to support more options in the { }.
Sounds like a good syntax.
Quoted 2 lines
- agetty already supports \foo{ } arguments, see
get_escape_argument()
- lib/strutils.c:ul_optstr_next() can parse options list {a="aaa",b="bbb"}
I see.
Quoted 17 lines
Quoted 1 line
- an interface template for fine tuning of the output
You can introduce printf()-like \4{format="%iface:%addr} (like git-log)
Quoted 4 lines
- better control over the output: e. g. disable printing of IPv4 or IPv6 addresses, disable printing of temporary addresses or link-local addresses
- cross-protocol checks: e. g. disable reporting of link-local IPv6 address for an IPv4-only interface:-)
Quoted 3 lines
The new network comparison code could potentially render obsolete string based issue comparison (implemented in 6522d88). There are
ifaces_list_change_4andifaces_list_change_6variables ready for this purpose.Hmm... the global variables used deeply within the netlink message parser do not look very elegant. Maybe introduce a separate struct,
ul_ifaces_status, and use it as a global variable inagetty.c, then callprocess_netlink_msg()with this struct as an argument.
Quoted 5 lines
term-utils/agetty.c line 166
Quoted 4 lines
term-utils/agetty.c +4
-0+struct ip_quality_item { +enum ip_quality_item_value quality; +int len; +__u32 ifa_valid;/* IP addres lifetime */
uint32_tif possible ;-)
The kernel ifa_valid is declared as __u32. If uint32_t is always equal, I'll use it.
Quoted 4 lines
Do we need the _quality_ in the sctruct names? What about
ul_ifaceandul_iface_addr?process_netlink_msg_part()will be simpler and more readable if you move the basic tasks to small functions likeadd_interface():-)
Actually, if we introduce generic library functions, then other consumers may want to do something else than quality evaluation. I can imagine a generic structure with pointer to custom data. And the function will have a callback that will perform the needed operation.
Quoted 8 lines
Quoted 8 lines
term-utils/agetty.c +8
-0+ }else { +/* Should never happen */ +debug_net ("- interface not found\n" ); +return ; + } + } +if (family ==AF_INET ) { + ipq_prev_next = &(ifaceq->ip_quality_list_4);
Quoted 8 lines
term-utils/agetty.c static void open_netlink(void) · line 1855
Quoted 6 lines
term-utils/agetty.c +6
-0+ ifaces_list_change = &ifaces_list_change_4; + } +if (family ==AF_INET6 ) { + ipq_prev_next = &(ifaceq->ip_quality_list_6); + ifaces_list_change = &ifaces_list_change_6; + }I guess static analyzers won't like this part;
ipq_prev_nextis uninitialized if the family is something else.
Should never happen, as we use RTMGRP_IPV4_IFADDR or RTMGRP_IPV6_IFADDR for netlink_groups. But yes, better safe than sorry.
Signature · 10 lines
Best Regards / S pozdravem, Stanislav Brabec software developer --------------------------------------------------------------------- SUSE LINUX, s. r. o. e-mail: [email protected] Křižíkova 148/34 (Corso IIa) tel: +420 284 084 060 186 00 Praha 8-Karlín fax: +420 284 084 001 Czech Republic http://www.suse.cz/ PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76