zhhyu7 opened a new pull request, #22472: URL: https://github.com/apache/nuttx/pull/22472
## Summary
Three commits that let a netlink client configure routes and CAN gateway
rules, which the NETLINK_ROUTE protocol could read but never set.
1. net/netlink: accept RTM_NEWROUTE to set the default gateway.
RTM_GETROUTE could read the routing table, but RTM_NEWROUTE was rejected
with ENOSYS, so a netlink-only client such as libnl had no way to set the
gateway of a device. The request is now parsed with nlmsg_parse(), and
RTA_DST, RTA_GATEWAY and RTA_OIF are each length-checked. When
RTA_GATEWAY is present and
rtm_dst_len is 0, the gateway becomes
d_draddr (d_ipv6draddr for AF_INET6) of the device RTA_OIF selects.
A route to a specific destination is accepted and broadcast to
RTNLGRP_IPV4_ROUTE / RTNLGRP_IPV6_ROUTE, but does not reach the routing
table yet.
2. net/netlink: add CAN gateway rules over rtnetlink.
net/netforward/can_forward.c walks dev->d_cangw_rules on every
received CAN frame, but nothing ever put a rule on that list, so the list
stayed empty and CAN gateway forwarding could not be used at all. AF_CAN
is now accepted in RTM_NEWROUTE: struct rtcanmsg carries the gateway
type and flags, CGW_FILTER the struct can_filter, and CGW_SRC_IF /
CGW_DST_IF the two devices. add_cangw_rule() appends the rule under the
device lock, because the receive path walks the same list.
3. net/netlink: remove CAN gateway rules over rtnetlink.
A rule could be added but never taken away. AF_CAN is now accepted in
RTM_DELROUTE: with CGW_FILTER or CGW_DST_IF only the matching rules are
removed, with neither every rule of the device is. remove_cangw_rule()
reports how many rules it removed and takes a rule to match instead of an
array.
## Impact
New behaviour, no change to an existing path. Each new message type is
guarded by its own Kconfig option (NETLINK_DISABLE_NEWROUTE,
NETLINK_DISABLE_DELROUTE), matching the six message types this protocol
already had, so a configuration that does not need them pays nothing.
AF_CAN handling additionally needs CONFIG_NET_CANFORWARD.
include/netpacket/cangw.h is new and holds struct rtcanmsg, the
CGW_TYPE_* values its gwtype field takes, and the attribute and flag
definitions, so a client can build a request without a kernel header.
The CGW_TYPE_* enum moved there out of include/nuttx/net/cangw.h,
which now includes the public header; no in-tree user changes.
remove_cangw_rule() changes signature. net/netlink/netlink_route.c is
its only caller.
## Testing
Built and run on sim:dynconns (CI config, tools/ci/testlist/sim-02.dat)
with SIM_NET_BRIDGE, NET_CANFORWARD, SIM_CANDEV_SOCK and a host
vcan named can0.
Build matrix — every gate the diff introduces, on and off:
| # | configuration | compiler | result |
|---|---|---|---|
| 1 | sim:dynconns, NET_CANFORWARD=n | gcc 13.4 | pass |
| 2 | NET_CANFORWARD=y | gcc 13.4 | pass |
| 3 | DISABLE_NEWROUTE=y + DISABLE_DELROUTE=y | gcc 13.4 | pass |
| 4 | DISABLE_NEWROUTE=n + DISABLE_DELROUTE=y | gcc 13.4 | pass |
| 5 | DISABLE_NEWROUTE=y + DISABLE_DELROUTE=n | gcc 13.4 | pass |
| 6 | everything enabled | gcc 11 | pass |
No unused-function warning with a gate off.
RTM_NEWROUTE, before this series:
nsh> rttest
gw(before): 10.0.0.1
RT default via 10.0.1.9 send=-38
default route accepted got=-38 want=0 FAIL
gw(after ): 10.0.0.1
RT 192.168.5.0/24 via 10.0.1.8 send=-38
host route accepted got=-38 want=0 FAIL
gw(after ): 10.0.0.1
RT oif does not exist send=-38
unknown oif rejected got=-38 want=-19 FAIL
With this series:
[51/1839]
nsh> rttest
gw(before): 10.0.0.1
RT default via 10.0.1.9 send=0
default route accepted got=0 want=0 PASS
gw(after ): 10.0.1.9
RT 192.168.5.0/24 via 10.0.1.8 send=0
host route accepted got=0 want=0 PASS
gw(after ): 10.0.1.9
RT oif does not exist send=-19
unknown oif rejected got=-19 want=-19 PASS
The gateway follows the default route, a route to a host leaves it alone,
and an unknown RTA_OIF gives ENODEV.
CAN gateway rules, before this series: the first request gets no
reply and recv() blocks for ever. With this series, 17 of 17 cases
behave as expected:
--- 1. reject malformed requests ---
add without src got=-19 want=-19 PASS
add without dst got=-19 want=-19 PASS
add with bad dst got=-19 want=-19 PASS
add with bad gwtype got=-97 want=-97 PASS
--- 2. remove from an empty list ---
del from empty list got=-2 want=-2 PASS
del without src got=-22 want=-22 PASS
--- 3. add, then remove the matching rule ---
add one rule got=0 want=0 PASS
del non-matching filter got=-2 want=-2 PASS
del matching filter got=0 want=0 PASS
del same rule twice got=-2 want=-2 PASS
--- 4. flush every rule of the device ---
add rule 1 got=0 want=0 PASS
add rule 2 got=0 want=0 PASS
add rule 3 got=0 want=0 PASS
flush all rules got=0 want=0 PASS
list empty after flush got=-2 want=-2 PASS
--- 5. a rule with no filter matches everything ---
add filterless rule got=0 want=0 PASS
flush filterless rule got=0 want=0 PASS
No regression — IP traffic over the bridge, before and after the rule
churn above:
=== ICMP baseline ===
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
=== ICMP after rule churn ===
5 packets transmitted, 5 received, 0% packet loss, time 4039ms
=== fragmented ===
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
No assertion fired with DEBUG_ASSERTIONS=y.
--
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]
