This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit edc8f2e2e35fde8cc2fcfd99465f56c8287135c2 Author: wangjinjing1 <[email protected]> AuthorDate: Tue Apr 14 16:44:27 2026 +0800 net/netlink: remove CAN gateway rules over rtnetlink. A CAN gateway rule could be added with RTM_NEWROUTE but never taken away, so a rule lived until the board was reset. Accept AF_CAN in RTM_DELROUTE. CGW_SRC_IF names the device whose rules are examined, and the rest of the request selects what to remove: - With CGW_FILTER or CGW_DST_IF, only the rules that match the given filter and egress device are removed, and a request that matches nothing returns ENOENT. - With neither, every rule of the device is removed, which lets a client start from a known state. remove_cangw_rule() now reports how many rules it removed and takes the rule to match instead of an array, so the caller no longer has to know how the rules are stored. It walks and unlinks the list under the device lock, because the receive path reads the same list, and frees each removed rule unless the caller asks for them back. Assisted-by: Kiro:claude-opus-4 Signed-off-by: wangjinjing1 <[email protected]> --- include/nuttx/net/cangw.h | 6 +- net/netforward/can_forward.c | 104 ++++++++++++++++++++++++++--- net/netlink/Kconfig | 6 ++ net/netlink/netlink_route.c | 151 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 253 insertions(+), 14 deletions(-) diff --git a/include/nuttx/net/cangw.h b/include/nuttx/net/cangw.h index 29975342185..f428576a4dc 100644 --- a/include/nuttx/net/cangw.h +++ b/include/nuttx/net/cangw.h @@ -100,9 +100,9 @@ extern "C" void add_cangw_rule(FAR struct net_driver_s *dev, FAR struct cangw_rule_s *cgw_rules); -void remove_cangw_rule(FAR struct net_driver_s *dev, - FAR struct cangw_rule_s *cgw_rules, - size_t size); +int remove_cangw_rule(FAR struct net_driver_s *dev, + FAR const struct cangw_rule_s *rule, + FAR sq_queue_t *removed); #ifdef __cplusplus } diff --git a/net/netforward/can_forward.c b/net/netforward/can_forward.c index 607b6dc93e3..479a971e4ad 100644 --- a/net/netforward/can_forward.c +++ b/net/netforward/can_forward.c @@ -31,8 +31,9 @@ #include <debug.h> #include <errno.h> -#include <nuttx/mm/iob.h> #include <nuttx/can.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mm/iob.h> #include <nuttx/net/can.h> #include <nuttx/net/net.h> #include <nuttx/net/netdev.h> @@ -186,30 +187,115 @@ void add_cangw_rule(FAR struct net_driver_s *dev, netdev_unlock(dev); } +/**************************************************************************** + * Name: cangw_rule_match + * + * Description: + * Check if a rule in the list matches the given criteria. + * + * Input Parameters: + * rtrule - The rule in the device's rule list. + * rule - The match criteria provided by the caller. + * + * Returned Value: + * true if the rule matches, false otherwise. + * + ****************************************************************************/ + +static inline_function bool +cangw_rule_match(FAR const struct cangw_rule_s *rtrule, + FAR const struct cangw_rule_s *rule) +{ + /* Match filter if caller provided a non-zero filter */ + + if (rule->gw.ccgw.filter.can_id != 0 || + rule->gw.ccgw.filter.can_mask != 0) + { + if (rtrule->gw.ccgw.filter.can_id != + rule->gw.ccgw.filter.can_id || + rtrule->gw.ccgw.filter.can_mask != + rule->gw.ccgw.filter.can_mask) + { + return false; + } + } + + /* Match destination device if specified */ + + if (rule->dest_dev && rtrule->dest_dev != rule->dest_dev) + { + return false; + } + + return true; +} + /**************************************************************************** * Name: remove_cangw_rule * * Description: - * Remove a CAN->CAN gateway rule from device. + * Remove CAN->CAN gateway rules from a device's rule list. + * + * Input Parameters: + * dev - The network device whose rule list to modify. + * rule - Match criteria. If NULL, all rules on the device are removed. + * removed - If non-NULL, removed nodes are collected into this queue + * and the caller is responsible for freeing them. + * If NULL, heap-allocated nodes are freed immediately; + * non-heap nodes (e.g. embedded in a static config struct) + * are only unlinked from the list. + * + * Returned Value: + * The number of rules removed. * ****************************************************************************/ -void remove_cangw_rule(FAR struct net_driver_s *dev, - FAR struct cangw_rule_s *cgw_rules, - size_t size) +int remove_cangw_rule(FAR struct net_driver_s *dev, + FAR const struct cangw_rule_s *rule, + FAR sq_queue_t *removed) { - size_t i; + FAR sq_entry_t *current; + FAR sq_entry_t *tmp; + int count = 0; - DEBUGASSERT(dev && cgw_rules); + DEBUGASSERT(dev); + + if (removed) + { + sq_init(removed); + } netdev_lock(dev); - for (i = 0; i < size; i++) + sq_for_every_safe(&dev->d_cangw_rules, current, tmp) { - sq_rem(&cgw_rules[i].node, &dev->d_cangw_rules); + FAR struct cangw_rule_s *rtrule = + (FAR struct cangw_rule_s *)current; + + /* If rule is NULL, remove all; otherwise match by criteria */ + + if (rule == NULL || cangw_rule_match(rtrule, rule)) + { + sq_rem(current, &dev->d_cangw_rules); + if (removed) + { + /* Caller takes ownership — collect for deferred free */ + + sq_addlast(current, removed); + } + else if (kmm_heapmember(current)) + { + /* Only free nodes that were individually heap-allocated */ + + kmm_free(current); + } + + count++; + } } netdev_unlock(dev); + return count; } /**************************************************************************** diff --git a/net/netlink/Kconfig b/net/netlink/Kconfig index 239fb33ae75..9e9c1b4e70f 100644 --- a/net/netlink/Kconfig +++ b/net/netlink/Kconfig @@ -112,6 +112,12 @@ config NETLINK_DISABLE_NEWROUTE ---help--- RTM_NEWROUTE is used to add a route or a gateway rule. +config NETLINK_DISABLE_DELROUTE + bool "Disable RTM_DELROUTE support" + default n + ---help--- + RTM_DELROUTE is used to delete netdev route rule. + config NETLINK_DISABLE_GETROUTE bool "Disable RTM_GETROUTE support" default n diff --git a/net/netlink/netlink_route.c b/net/netlink/netlink_route.c index 0f5ae12661a..b627e19e41a 100644 --- a/net/netlink/netlink_route.c +++ b/net/netlink/netlink_route.c @@ -288,7 +288,8 @@ static_assert(nitems(g_ifa_ipv6_policy) == # define g_ifa_ipv6_policy NULL #endif -#if !defined(CONFIG_NETLINK_DISABLE_NEWROUTE) && \ +#if (!defined(CONFIG_NETLINK_DISABLE_NEWROUTE) || \ + !defined(CONFIG_NETLINK_DISABLE_DELROUTE)) && \ defined(CONFIG_NET_CANFORWARD) const struct nla_policy g_cgw_policy[CGW_MAX + 1] = { @@ -1608,6 +1609,133 @@ static int netlink_new_can_route(FAR const struct nlmsghdr *nlmsg) } } +#endif /* CONFIG_NET_CANFORWARD && !CONFIG_NETLINK_DISABLE_NEWROUTE */ + +#if !defined(CONFIG_NETLINK_DISABLE_DELROUTE) && \ + defined(CONFIG_NET_CANFORWARD) + +static int netlink_remove_cangw_rule(FAR struct rtcanmsg *rt_msg, + FAR struct nlattr *tb[]) +{ + FAR struct net_driver_s *src_dev; + uint32_t src_if; + int count; + + /* CGW_SRC_IF is mandatory — we need to know which device to operate on */ + + if (!tb[CGW_SRC_IF]) + { + return -EINVAL; + } + + src_if = *(FAR uint32_t *)nla_data(tb[CGW_SRC_IF]); + src_dev = netdev_findbyindex(src_if); + + if (src_dev == NULL) + { + nerr("src_dev is not found\n"); + return -ENODEV; + } + + /* If filter or destination attributes are present, build a match rule + * and remove only matching entries. Otherwise pass NULL to remove all + * rules on the source device. + */ + + if (tb[CGW_FILTER] || tb[CGW_DST_IF]) + { + struct cangw_rule_s rule; + + memset(&rule, 0, sizeof(rule)); + + /* Populate filter criteria if provided */ + + if (tb[CGW_FILTER]) + { + nla_memcpy(&rule.gw.ccgw.filter, tb[CGW_FILTER], + sizeof(struct can_filter)); + } + + /* Resolve destination device if specified; dst_if == 0 means + * match any destination. + */ + + if (tb[CGW_DST_IF]) + { + uint32_t dst_if = *(FAR uint32_t *)nla_data(tb[CGW_DST_IF]); + + if (dst_if != 0) + { + rule.dest_dev = netdev_findbyindex(dst_if); + if (rule.dest_dev == NULL) + { + return -ENODEV; + } + } + } + + /* Pass NULL for removed — let remove_cangw_rule free heap nodes + * directly instead of collecting them. + */ + + count = remove_cangw_rule(src_dev, &rule, NULL); + if (count == 0) + { + nwarn("rule not found\n"); + return -ENOENT; + } + } + else + { + /* No filter/dst specified — flush all rules on this device */ + + count = remove_cangw_rule(src_dev, NULL, NULL); + } + + return OK; +} + +static int netlink_del_can_route(FAR const struct nlmsghdr *nlmsg) +{ + FAR struct nlattr *tb[CGW_MAX + 1]; + FAR struct rtcanmsg *rt_msg = nlmsg_data(nlmsg); + int ret; + + /* nlmsg boundary check */ + + if (nlmsg->nlmsg_len < nlmsg_msg_size(sizeof(struct rtcanmsg))) + { + return -EINVAL; + } + + /* parse nlmsg and acquire tb. */ + + ret = nlmsg_parse(nlmsg, sizeof(struct rtcanmsg), tb, + CGW_MAX, g_cgw_policy, NULL); + if (ret < 0) + { + nwarn("nlmsg_parse failed: %d\n", ret); + return ret; + } + + /* get r->gwtype and switch r->gwtype */ + + switch (rt_msg->gwtype) + { + case CGW_TYPE_CAN_CAN: + return netlink_remove_cangw_rule(rt_msg, tb); + + default: + return -EAFNOSUPPORT; + } +} + +#endif /* CONFIG_NET_CANFORWARD && !CONFIG_NETLINK_DISABLE_DELROUTE */ + +#if (!defined(CONFIG_NETLINK_DISABLE_NEWROUTE) || \ + !defined(CONFIG_NETLINK_DISABLE_DELROUTE)) && \ + defined(CONFIG_NET_CANFORWARD) + static void netlink_can_ack(NETLINK_HANDLE handle, FAR const struct nlmsghdr *nlmsg, int err) @@ -1638,7 +1766,7 @@ static void netlink_can_ack(NETLINK_HANDLE handle, (FAR struct netlink_response_s *)alloc); } -#endif /* CONFIG_NETLINK_DISABLE_NEWROUTE && CONFIG_NET_CANFORWARD */ +#endif /* CONFIG_NET_CANFORWARD && !CONFIG_NETLINK_DISABLE_NEWROUTE/DELROUTE */ /**************************************************************************** * Public Functions @@ -2024,7 +2152,26 @@ ssize_t netlink_route_sendto(NETLINK_HANDLE handle, req->gen.rtgen_family); } break; +#endif /* !CONFIG_NETLINK_DISABLE_NEWROUTE */ + +#ifndef CONFIG_NETLINK_DISABLE_DELROUTE + case RTM_DELROUTE: +#ifdef CONFIG_NET_CANFORWARD + if (req->gen.rtgen_family == AF_CAN) + { + ret = netlink_del_can_route(nlmsg); + if (nlmsg->nlmsg_flags & NLM_F_ACK) + { + netlink_can_ack(handle, nlmsg, ret); + } + } + else #endif + { + ret = -EAFNOSUPPORT; + } + break; +#endif /* !CONFIG_NETLINK_DISABLE_DELROUTE */ default: ret = -ENOSYS;
