[PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE

Yuyang Huang posted 10 patches 1 month, 4 weeks ago
There is a newer version of this series
Documentation/netlink/specs/rt-route.yaml     |  68 ++++++-
include/net/ip6_fib.h                         |   5 +-
include/net/ip6_route.h                       |   2 +
include/uapi/linux/rtnetlink.h                |  22 +++
net/ipv6/addrconf.c                           |   3 +-
net/ipv6/ip6_fib.c                            |  14 +-
net/ipv6/ndisc.c                              |   4 +-
net/ipv6/route.c                              |  75 ++++++--
.../testing/selftests/net/lib/py/__init__.py  |   4 +-
tools/testing/selftests/net/lib/py/ynl.py     |   7 +-
tools/testing/selftests/net/rtnetlink.py      | 181 +++++++++++++++++-
11 files changed, 349 insertions(+), 36 deletions(-)
[PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE
Posted by Yuyang Huang 1 month, 4 weeks ago
When the kernel deletes an IPv6 route on its own, the RTM_DELROUTE
notification does not say why. User space cannot tell a route that
expired from one the router explicitly withdrew, yet the two call for
different reactions: an expired RA route means the router failed to
refresh it in time, which points at a misconfigured or unreliable
router and may warrant action such as disabling IPv6 on that network,
while a zero-lifetime withdrawal is normal, RFC-compliant operation.

This is a general problem for any consumer device running Linux,
especially on Wi-Fi networks, where multicast delivery is not
guaranteed (e.g. frames can be lost around DTIM for clients in power
save mode). The motivating case is Android: the userspace NetworkStack
process listens on RTMGRP_IPV6_ROUTE and today treats any loss of the
IPv6 default route as "router lost". To avoid the device repeatedly
gaining and losing IPv6 connectivity on a badly configured network,
when it detects the device is on a dual-stack network with working
IPv4 connectivity, it defensively clears accept_ra_defrtr and restarts
IPv6, so user space apps stop using broken global IPv6 connectivity
while link-local IPv6 keeps working. That reaction is wrong if the
route was withdrawn by a zero-lifetime RA (some ISPs do this
intentionally for reconfiguration) - with accept_ra_defrtr off, IPv6
never recovers once the router advertises again. It is the right
reaction if the route genuinely expired, since the router failed to
refresh it in time.

Fixing this in user space is not practical: RTM_NEWROUTE carries the
initial route lifetime (in rta_cacheinfo), but the kernel does not
resend it when a later RA refreshes the lifetime. So distinguishing
the cause of an RTM_DELROUTE from user space would mean opening a raw
socket, listening to RAs, and tracking lifetimes independently,
duplicating logic the kernel already has. Sending RTM_NEWROUTE on
every RA lifetime refresh was also considered, but that would be
spammy and is technically wrong, since a lifetime update does not add
a new route.

This series proposes RTA_DEL_REASON instead: it tells user space why
the route was deleted so it can react accordingly. In the Android
case, NetworkStack would defensively disable global IPv6 only on
RT_DEL_REASON_EXPIRED, and take no action on
RT_DEL_REASON_RA_WITHDRAWN, since that is RFC-compliant behavior.

Patches 1 to 6 add RTA_DEL_REASON and enum rt_del_reason to the
rtnetlink uAPI, thread the reason from the kernel-initiated IPv6
deletion paths down to the RTM_DELROUTE notification, and record the
cause: RT_DEL_REASON_EXPIRED for routes garbage collected after their
RTF_EXPIRES lifetime ran out, and RT_DEL_REASON_RA_WITHDRAWN for
default routes, prefix routes and RFC 4191 route information routes
withdrawn by Router Advertisements. Patches 1 to 5 are no-ops on the
wire; the attribute first appears in patch 6. The route addition path
is not touched.

Patches 7 to 9 extend the rt-route Netlink spec with the route
notifications and their multicast groups, split the newroute and
delroute request attribute lists out of the shared getroute reply
list, and add the new attribute and its enum.

Only kernel-initiated deletions that user space cannot otherwise
explain are attributed. User-requested deletions are self-explanatory
to the requester, so they carry no reason; the UAPI documents that
absence and RT_DEL_REASON_UNSPEC must be treated identically, which
keeps the door open for attributing more paths (nexthop removal
cascades, device removal) later.

Patch 10 adds selftests covering all three producer paths: a
GC-expired route, and a default route + PIO prefix route + RIO route
advertised and then withdrawn by hand-crafted RAs over a raw ICMPv6
socket (no external RA tool needed), plus a check that user-requested
deletions carry no attribute. The notifications are decoded with YNL,
which also exercises the rt-route spec additions.

Changes since v4:
- Rename the payload values to RT_DEL_REASON_*, so they do not read as
  attribute ids. The attribute id is still RTA_DEL_REASON.
- Document that the reason value space is family-agnostic.
- Drop the skip_notify argument from ip6_del_rt_reason(); all callers
  passed false, and skipping the notification discards the reason.
- Drop the unused !CONFIG_IPV6 stub for ip6_del_rt_reason().

Changes since v3:
- Split the single kernel patch into six, one logical step each, per
  review.
- Use the enum throughout instead of a plain u32.
- Add inet6_rt_del_notify() and call it from fib6_del_route(), so the
  route addition path is unchanged.
- Move the route notifications and their multicast groups to their own
  spec patch.
- Split the newroute and delroute request attribute lists out of the
  getroute reply list in a separate spec patch, so the attribute
  addition is a one-line diff.

Changes since v2:
- Fix the patch 1 commit message, which still said u8.
- Keep del-reason out of the newroute and delroute request attribute
  lists in the rt-route spec, since the kernel rejects it in requests.

Changes since v1:
- Expand the motivation with the Android use case, per review request.
- Widen RTA_DEL_REASON from u8 to u32, per Netlink uAPI convention.
- Convert dict_keys to a set before the set difference in the
  selftest, for clarity.

Yuyang Huang (10):
  ipv6: add ip6_del_rt_reason()
  ipv6: propagate the route deletion reason to fib6_del_route()
  ipv6: record the reason for kernel-initiated route deletions
  ipv6: add a deletion reason argument to rt6_fill_node()
  ipv6: expose the route deletion reason in RTM_DELROUTE
  ipv6: add inet6_rt_del_notify()
  netlink: specs: rt-route: add route notifications
  netlink: specs: rt-route: split out the request attribute list
  netlink: specs: rt-route: add the route deletion reason
  selftests: net: verify RTA_DEL_REASON on route deletion

 Documentation/netlink/specs/rt-route.yaml     |  68 ++++++-
 include/net/ip6_fib.h                         |   5 +-
 include/net/ip6_route.h                       |   2 +
 include/uapi/linux/rtnetlink.h                |  22 +++
 net/ipv6/addrconf.c                           |   3 +-
 net/ipv6/ip6_fib.c                            |  14 +-
 net/ipv6/ndisc.c                              |   4 +-
 net/ipv6/route.c                              |  75 ++++++--
 .../testing/selftests/net/lib/py/__init__.py  |   4 +-
 tools/testing/selftests/net/lib/py/ynl.py     |   7 +-
 tools/testing/selftests/net/rtnetlink.py      | 181 +++++++++++++++++-
 11 files changed, 349 insertions(+), 36 deletions(-)

--
2.43.0
Re: [PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE
Posted by Yuyang Huang 1 month, 3 weeks ago
I checked the sashiko's comment:
https://patchwork.kernel.org/project/netdevbpf/patch/20260804014714.4362-10-sigefriedhyy@gmail.com/

> Should this new UAPI attribute also be added to the Netlink YAML
> specification?

> Is this new attribute missing from the netlink YAML
> specification?
> should this new attribute be documented in
> Documentation/netlink/specs/rt-route.yaml

> Is the del-reason (RTA_DEL_REASON) attribute missing from the YAML
> specification?

> Is there a missing patch in this series to actually report the
> deletion reason?

> Does this del_reason actually get passed from the deletion path?

These are answered by later patches in the same series.

> This is a pre-existing issue, but could this RTM_NEWROUTE be emitted
> out of order with respect to an RTM_DELROUTE?

Not related to the current series but might be worth sending a follow
up separately.

> Should this new enum and its entries include doc properties to
> describe the specific deletion reasons for the uAPI documentation?

Agreed, but it is a nit. If I need to send out patch series 6 due to
adjusting later review comments, I will include the fix for this
comment as well.

> Is RTA_DEL_REASON missing from rtm_ipv6_policy and rtm_ipv4_policy?
> [...] Should it be added to the policies as NLA_IGNORE or
> NLA_REJECT?
> Will the new RTA_DEL_REASON attribute be rejected if user space
> echoes it back? [...] causing an ABI breakage.

The comment seems wrong. The attribute is kernel to user space
notification-only, user space code should never set it.


Thanks,

Yuyang
Re: [PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE
Posted by Yuyang Huang 1 month, 3 weeks ago
Reply to comment in the other sashiko run:

https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260804014714.4362-1-sigefriedhyy%40gmail.com

> Should the netlink socket returned here be closed by the callers?
> [...] Would a defer(rtnl.close) [...] make teardown explicit?

> Same question for this raw ICMPv6 socket [...] Would a
> defer(sock.close) here be preferable?

> is the stated reason for the retry loop accurate? [...]
> addrconf_dad_begin() takes the early branch and never sends a DAD
> probe at all [...] Could the docstring describe that tentative
> window instead?

The comments above look valid, but I do not think they affect the
correctness of the test code, so it does not seem worth sending a v6
just to fix them. If a v6 is needed for other reasons, I will fix
them there.

> Should the #else branch also get a stub for ip6_del_rt_reason()?

The ip6_del_rt() stub exists only because __remove_nexthop_fib() in
net/ipv4/nexthop.c is obj-y and calls it with CONFIG_IPV6=n. The new
helper has no caller outside net/ipv6/, so a stub for it would be
dead code. I would rather add one when a caller needs it.

Thanks,

Yuyang
Re: [PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE
Posted by Ido Schimmel 1 month, 3 weeks ago
On Fri, Aug 07, 2026 at 07:11:29PM +0900, Yuyang Huang wrote:
> Reply to comment in the other sashiko run:
> 
> https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260804014714.4362-1-sigefriedhyy%40gmail.com

[...]

> > Should the #else branch also get a stub for ip6_del_rt_reason()?
> 
> The ip6_del_rt() stub exists only because __remove_nexthop_fib() in
> net/ipv4/nexthop.c is obj-y and calls it with CONFIG_IPV6=n. The new
> helper has no caller outside net/ipv6/, so a stub for it would be
> dead code. I would rather add one when a caller needs it.

But why put it under '#if IS_ENABLED(CONFIG_IPV6)' in the first place?
Just move it above, next to ip6_ins_rt()...
Re: [PATCH net-next v5 00/10] ipv6: report why a route was deleted in RTM_DELROUTE
Posted by Yuyang Huang 1 month, 3 weeks ago
On Sat, Aug 8, 2026 at 12:52 AM Ido Schimmel <idosch@nvidia.com> wrote:

> But why put it under '#if IS_ENABLED(CONFIG_IPV6)' in the first place?
> Just move it above, next to ip6_ins_rt()...

Thanks for the comment, will fix it in patchset v6.