zhhyu7 opened a new pull request, #22487: URL: https://github.com/apache/nuttx/pull/22487
## Summary 1. net/netlink: always report RTM_NEWLINK in an RTM_GETLINK dump. netlink_get_device() chose the message type from IFF_IS_UP(), but its two callers need different types. A dump must always report RTM_NEWLINK, because the link state is carried by ifi_flags; only the RTNLGRP_LINK broadcast reports the actual up/down event. Reporting RTM_DELLINK inside a dump made every interface that is down vanish from the enumeration, since a client collects only RTM_NEWLINK entries. 2. net/netlink: move the NLA type enum to the public header. The NLA_UNSPEC..NLA_TYPE_MAX enum describes the attribute types of the netlink protocol itself but lived in the private net/netlink/netlink.h, so an application had to redefine the values to build an attribute policy. Also adds NLMSG_ALIGNTO, which the attribute macros of libnl expect. 3. net/netlink: add the Generic Netlink protocol. A kernel subsystem that wants to talk to userspace over netlink had to get its own protocol number in NETLINK_* and its own sendto handler in netlink_sockif.c. Generic Netlink exists so that it does not have to: a subsystem registers a family by name at run time, userspace resolves the name to an id with the GENL_ID_CTRL controller, and every family shares the single NETLINK_GENERIC protocol. genl_register_family() assigns the family id and the multicast group offset; netlink_generic_sendto() routes a request by nlmsg_type to the controller or to the cmd_handler of the family that owns that id. ## Impact The first commit changes what a client sees in an RTM_GETLINK dump: an interface that is down now appears, as RTM_NEWLINK with IFF_UP clear in ifi_flags. A client that relied on the old behaviour to learn about a down interface has to read ifi_flags instead, which is how Linux has always reported it. netlink_add_broadcast() takes the protocol as its first argument, since a multicast group number is only meaningful within one protocol. All nine callers are updated; the two in the wifi simulation pass NETLINK_ROUTE, which is what they were broadcasting to all along. include/netpacket/netlink.h is split: the rtnetlink definitions move to netpacket/rtnetlink.h, the netfilter ones to netpacket/nfnetlink.h, and netpacket/genetlink.h is new. The move carries no definition away, and adds only NETLINK_ADD_MEMBERSHIP and NETLINK_DROP_MEMBERSHIP, which the new SOL_NETLINK socket options need. nuttx-apps needs the matching include changes, hence the dependency below. The protocol itself is behind CONFIG_NETLINK_GENERIC, default n, so a configuration that does not use it builds exactly as before. depends-on: https://github.com/apache/nuttx-apps/pull/4177 ## Testing sim:dynconns (CI config, tools/ci/testlist/sim-02.dat), checkpatch clean on everything this series touches. Build matrix — both new gates, on and off, plus a second compiler: | NETLINK_ROUTE | NETLINK_GENERIC | compiler | result | |---|---|---|---| | y | n (default) | gcc 13.4 | pass | | y | y | gcc 13.4 | pass | | n | y | gcc 13.4 | pass | | n | n | gcc 13.4 | pass | | y | y | gcc 11 | pass | ROUTE=n GENERIC=y found a real defect while this series was prepared: nla_next() and nla_parse() are generic attribute helpers that netlink_generic.c also uses, and netlink_attr.c is built unconditionally, so the CONFIG_NETLINK_ROUTE guard now closes before them instead of after. RTM_GETLINK dump. A program sends RTM_GETLINK with NLM_F_DUMP and prints the type of every response. Before this series, eth0 disappears from the dump while it is down: ---A-all-up--- GETLINK: msg=RTM_NEWLINK ifindex=1 flags=0x3006 up=1 GETLINK: msg=RTM_NEWLINK ifindex=2 flags=0x26 up=1 GETLINK RESULT: responses=2 RTM_NEWLINK=2 RTM_DELLINK=0 ifdown eth0...OK ---B-eth0-down--- GETLINK: msg=RTM_DELLINK ifindex=1 flags=0x3000 up=0 <-- a dump client drops this GETLINK: msg=RTM_NEWLINK ifindex=2 flags=0x26 up=1 GETLINK RESULT: responses=2 RTM_NEWLINK=1 RTM_DELLINK=1 With this series it stays in the dump and the state is in the flags (0x3006 -> 0x3000, IFF_UP clear): ---B-eth0-down--- GETLINK: msg=RTM_NEWLINK ifindex=1 flags=0x3000 up=0 GETLINK: msg=RTM_NEWLINK ifindex=2 flags=0x26 up=1 GETLINK RESULT: responses=2 RTM_NEWLINK=2 RTM_DELLINK=0 ifup restores 0x3006 and the dump is unchanged. Generic Netlink. A program registers a family in the kernel and then resolves it from userspace over NETLINK_GENERIC: GENL: socket(NETLINK_GENERIC) = 3 --- 1. the built-in controller answers for itself --- GETFAMILY(nlctrl) got=0 want=0 PASS nlctrl: id=16 name="nlctrl" version=1 nlctrl id is GENL_ID_CTRL got=16 want=16 PASS --- 2. an unknown family is refused --- GETFAMILY(nosuchfamily) got=-2 want=-2 PASS --- 3. register a family, then resolve it by name --- genl_register_family got=0 want=0 PASS assigned id=19 mcgrp_offset=1 GETFAMILY(nxtest) got=0 want=0 PASS id matches the assigned one got=19 want=19 PASS version is reported got=3 want=3 PASS nxtest: id=19 name="nxtest" version=3 --- 4. lookup by id is not implemented --- GETFAMILY(by id) refused got=-2 want=-2 PASS --- 5. the same name cannot be registered twice --- duplicate name refused got=-17 want=-17 PASS --- 6. unregister, then the name is gone --- genl_unregister_family got=0 want=0 PASS GETFAMILY(nxtest) after del got=-2 want=-2 PASS Lookup by CTRL_ATTR_FAMILY_ID is not implemented; libnl resolves a family by name, and the test records the current behaviour rather than asserting a feature that is absent. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
