Skip to content

fix: renew_ip keeps the default route - #79

Merged
Frando merged 3 commits into
n0-computer:mainfrom
outpostHQ:pr/renew-ip
Nov 26, 2026
Merged

Frando merged 3 commits into
n0-computer:mainfrom
outpostHQ:pr/renew-ip

Conversation

@shubham-kaushal

@shubham-kaushal shubham-kaushal commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

renew_ip removes the old address, and the kernel drops the default route with it. After a renewal the device has no default route.

renew_ip now adds the default route back when the renewed interface carries it. When the link is down it skips that step, and link_up adds the route once the link is back.

add_default_route_v4 now takes the interface and sets it as the route's oif. Without it the kernel picked the interface through the gateway's subnet route, so with two interfaces on the same router the route could end up on the other one after a renewal.

renew_ip_keeps_default_route fails on main and passes with this change. renew_ip_while_link_down covers the down-link case, and renew_ip_keeps_default_route_on_its_iface covers two interfaces on one router.

@Frando Frando left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the fix is right for the common case.

It adds a regression though that needs to be fixed: renew_ip now fails on an interface that is down. When renew_ip runs after link_down(), or on an interface built with start_down, the address change succeeds. Then replace_default_route_v4 asks the kernel for a gateway route through a down device. The kernel rejects it: the prefix route for the subnet is not installed while the device is down, so the gateway is unreachable. At that point the core record and the kernel already hold the new IP. The caller gets an Err, and no DeviceIpChanged event is emitted. On main the same call succeeds.

link_up() already re-adds the default route for the default_via interface. So renew_ip can skip the route when the link is down, or ignore that specific error. A test that does link_down, renew_ip, link_up and then pings across the NAT would cover it.

Smaller points:

  • wire_iface_async adds the default route with add_default_route_v4(gw), without an output interface. renew_ip now uses replace_default_route_v4(&ifname, gw), which sets the output interface. Both work, but the two paths now produce slightly different routes. Using the same helper in both places keeps them identical.

@shubham-kaushal

Copy link
Copy Markdown
Contributor Author

Thanks, good catch on the down link. Fixed in the new commit:

  • renew_ip now skips the default route when the link is down. link_up adds it back for the default_via interface, so nothing else is needed.
  • It uses add_default_route_v4(gw) now, same as wire_iface_async.
  • New test renew_ip_while_link_down: link_down, renew_ip, link_up, then a ping to a peer across the NAT. It fails without the link check.

I also rebased on main, since the netlink code moved in #83.

@Frando

Frando commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Link-down fix looks good, thanks.

One more issue: add_default_route_v4(gw) sets no output interface, so the kernel picks it via the gateway's subnet route. After renewal that route sits behind the other interfaces' routes. With two ifaces on the same router:

before eth0.renew_ip():  default via 198.18.1.1 dev eth0
after eth0.renew_ip():   default via 198.18.1.1 dev eth1

Setup only works by ordering. I'd fix it in the helper: give add_default_route_v4 an ifname and set the oif, and pass the interface at all four call sites (the three in wiring.rs and renew_ip). Both paths stay identical. I tried it locally: the route stays on eth0, and the full suite passes. A test for the two-iface case would be good too.

Removing the old address drops the default route, so renew_ip adds it again.
link_up adds it back. Use add_default_route_v4, as wire_iface_async does.
add_default_route_v4 set no output interface, so the kernel chose one
through the gateway's subnet route. With two interfaces on the same
router, renew_ip on eth0 re-added its subnet route behind eth1's, and
the default route moved to eth1.

The helper now takes the interface name and sets it as the oif. The
three callers in wiring.rs and renew_ip pass their interface.
@shubham-kaushal

Copy link
Copy Markdown
Contributor Author

You're right, thanks. I only tested with one interface, so the ordering never showed up.

Fixed it in the helper as you suggested: add_default_route_v4 takes the interface name and sets the oif, and all four call sites pass it (the three in wiring.rs and renew_ip).

New test renew_ip_keeps_default_route_on_its_iface puts eth0 and eth1 on the same router, renews eth0, and checks that ip -4 route show default still says dev eth0. With the oif removed from the helper it fails with default via 198.18.1.1 dev eth1, same as what you saw. I moved ip_output from link_condition.rs to tests/mod.rs so the iface tests can use it too.

Also rebased on main.

@Frando
Frando merged commit 8c6996a into n0-computer:main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants