* [PATCH] netlink: Fix nl_route_dup by 3-step processing
@ 2026-11-07 11:46 Martin Schitter
2026-11-11 1:47 ` Stefano Brivio
2026-11-27 14:31 ` [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch Jon Maloy
0 siblings, 2 replies; 8+ messages in thread
From: Martin Schitter @ 2026-11-07 11:46 UTC (permalink / raw)
To: passt-dev; +Cc: Martin Schitter
Eliminate the route entries capacity limitations of nl_route_dup
by copying them to the destination namespace in three successive
steps to bypass dependency issues. This approach is inspired by
the code design used in the restore subcommand of iproute2.
Fixes: #221
Link: https://bugs.passt.top/show_bug.cgi?id=221
Signed-off-by: Martin Schitter <ms+git@mur.at>
---
netlink.c | 216 +++++++++++++++++++++++++++---------------------------
1 file changed, 107 insertions(+), 109 deletions(-)
diff --git a/netlink.c b/netlink.c
index 225b666..d5c704b 100644
--- a/netlink.c
+++ b/netlink.c
@@ -525,6 +525,21 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
return nl_do(s, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
}
+/**
+ * nl_rtattr_cmp() - Compare two routing attributes
+ * @rta1: Routing attribute
+ * @rta2: Routing attribute
+ *
+ * Return: 0 if equal
+ */
+static int nl_rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
+{
+ if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
+ return 1;
+
+ return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
+}
+
/**
* nl_route_dup() - Copy routes for given interface and address family
* @s_src: Netlink socket in source namespace
@@ -535,56 +550,81 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
*
* Return: 0 on success, negative error code on failure
*/
-int nl_route_dup(int s_src, unsigned int ifi_src,
- int s_dst, unsigned int ifi_dst, sa_family_t af)
+int nl_route_dup(int s_src, unsigned int ifi_src, int s_dst,
+ unsigned int ifi_dst, sa_family_t af)
{
- struct req_t {
- struct nlmsghdr nlh;
- struct rtmsg rtm;
- struct rtattr rta;
- unsigned int ifi;
- } req = {
- .rtm.rtm_family = af,
- .rtm.rtm_table = RT_TABLE_MAIN,
- .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
- .rtm.rtm_type = RTN_UNICAST,
-
- .rta.rta_type = RTA_OIF,
- .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
- .ifi = ifi_src,
+ /* Duplicate routes in correct order using 3 successive stages: */
+ enum tasks {
+ DUP_LOCAL_ADDR, /* Duplicate local route address entries */
+ DUP_LOCAL_NET, /* Duplicate local network entries */
+ DUP_OTHER, /* Duplicate all other entries */
+ NR_OF_TASKS,
};
- ssize_t nlmsgs_size, left, status;
- unsigned dup_routes = 0;
- struct nlmsghdr *nh;
- char buf[NLBUFSIZ];
- uint32_t seq;
- unsigned i;
- seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
+ for (enum tasks task = DUP_LOCAL_ADDR; task < NR_OF_TASKS; task++) {
+ struct req_t {
+ struct nlmsghdr nlh;
+ struct rtmsg rtm;
+ struct rtattr rta;
+ unsigned int ifi;
+ } req = {
+ .rtm.rtm_family = af,
+ .rtm.rtm_table = RT_TABLE_MAIN,
+ .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
+ .rtm.rtm_type = RTN_UNICAST,
+
+ .rta.rta_type = RTA_OIF,
+ .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
+ .ifi = ifi_src,
+ };
+ struct nlmsghdr *nh;
+ char buf[NLBUFSIZ];
+ ssize_t status;
+ uint32_t seq;
+
+ seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP,
+ sizeof(req));
+
+ nl_foreach(nh, status, s_src, buf, seq) {
+ struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
+ uint16_t flags = nh->nlmsg_flags;
+ struct rtattr *rta;
+ size_t na;
+ int rc;
- /* nl_foreach() will step through multiple response datagrams,
- * which we don't want here because we need to have all the
- * routes in the buffer at once.
- */
- nh = nl_next(s_src, buf, NULL, &nlmsgs_size);
- for (left = nlmsgs_size;
- NLMSG_OK(nh, left) && (status = nl_status(nh, left, seq)) > 0;
- nh = NLMSG_NEXT(nh, left)) {
- struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
- bool discard = false;
- struct rtattr *rta;
- size_t na;
+ if (nh->nlmsg_type != RTM_NEWROUTE)
+ continue;
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
+ struct rtattr *tb[RTA_MAX + 1] = { 0 };
- /* nexthop state flags don't apply to freshly created routes,
- * and the kernel will refuse our route if they are set.
- */
- rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
+ for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh);
+ RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
+ tb[rta->rta_type] = rta;
+ }
+
+ /* Duplicate only those routes that belong
+ * to the current processing stage.
+ * An approach inspired by iproute2's
+ * restore_handler() function.
+ * https://github.com/iproute2/iproute2/blob/e6471d7/ip/iproute.c#L2314
+ */
+ if (task == DUP_OTHER && !tb[RTA_GATEWAY])
+ continue;
+ if (task == DUP_LOCAL_NET &&
+ (tb[RTA_GATEWAY] || !tb[RTA_PREFSRC] ||
+ !nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
+ continue;
+ if (task == DUP_LOCAL_ADDR && (tb[RTA_GATEWAY] ||
+ (tb[RTA_PREFSRC] &&
+ nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))))
+ continue;
+
+ /* nexthop state flags don't apply to freshly created
+ * routes, and the kernel will refuse our route if they
+ * are set.
+ */
+ rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
- for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
- rta = RTA_NEXT(rta, na)) {
/* RTA_OIF and RTA_MULTIPATH attributes carry the
* identifier of a host interface. If they match the
* host interface we're copying from, change them to
@@ -595,103 +635,61 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
* available), or if any interface index in nexthop
* objects differ from the host interface, discard the
* route altogether.
- */
- if (rta->rta_type == RTA_OIF) {
- if (*(unsigned int *)RTA_DATA(rta) != ifi_src) {
- discard = true;
- break;
- }
-
- *(unsigned int *)RTA_DATA(rta) = ifi_dst;
- } else if (rta->rta_type == RTA_MULTIPATH) {
- int nh_len = RTA_PAYLOAD(rta);
+ */
+ if (tb[RTA_OIF]) {
+ if (*(unsigned int *)RTA_DATA(tb[RTA_OIF]) !=
+ ifi_src)
+ continue;
+ *(unsigned int *)RTA_DATA(tb[RTA_OIF]) =
+ ifi_dst;
+ } else if (tb[RTA_MULTIPATH]) {
+ int nh_len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
struct rtnexthop *rtnh;
-
- for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
+ bool discard = false;
+ for (rtnh = (struct rtnexthop *)
+ RTA_DATA(tb[RTA_MULTIPATH]);
RTNH_OK(rtnh, nh_len);
rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
int src = (int)ifi_src;
-
if (rtnh->rtnh_ifindex != src) {
discard = true;
break;
}
-
rtnh->rtnh_ifindex = ifi_dst;
}
-
if (discard)
- break;
- } else if (rta->rta_type == RTA_PREFSRC ||
- rta->rta_type == RTA_NH_ID) {
+ continue;
+ } else if (tb[RTA_PREFSRC]) {
/* Strip RTA_PREFSRC attributes: host routes
* might include a preferred source address,
* which must be one of the host's addresses.
* However, with -a, pasta will use a different
* namespace address, making such a route
* invalid in the namespace.
- *
- * Strip RTA_NH_ID attributes: host routes set
+ */
+ tb[RTA_PREFSRC]->rta_type = RTA_UNSPEC;
+ } else if (tb[RTA_NH_ID]) {
+ /* Strip RTA_NH_ID attributes: host routes set
* up via routing protocols (e.g. OSPF) might
* contain a nexthop ID (and not nexthop
* objects, which are taken care of in the
* RTA_MULTIPATH case above) that's not valid
* in the target namespace.
*/
- rta->rta_type = RTA_UNSPEC;
+ tb[RTA_NH_ID]->rta_type = RTA_UNSPEC;
}
- }
-
- if (discard)
- nh->nlmsg_type = NLMSG_NOOP;
- else
- dup_routes++;
- }
-
- if (!NLMSG_OK(nh, left)) {
- /* Process any remaining datagrams in a different
- * buffer so we don't overwrite the first one.
- */
- char tail[NLBUFSIZ];
- unsigned extra = 0;
-
- nl_foreach_oftype(nh, status, s_src, tail, seq, RTM_NEWROUTE)
- extra++;
-
- if (extra) {
- err("netlink: Too many routes to duplicate");
- return -E2BIG;
- }
- }
- if (status < 0)
- return status;
-
- /* Routes might have dependencies between each other, and the kernel
- * processes RTM_NEWROUTE messages sequentially. For n routes, we might
- * need to send the requests up to n times to get all of them inserted.
- * Routes that have been already inserted will return -EEXIST, but we
- * can safely ignore that and repeat the requests. This avoids the need
- * to calculate dependencies: let the kernel do that.
- */
- for (i = 0; i < dup_routes; i++) {
- for (nh = (struct nlmsghdr *)buf, left = nlmsgs_size;
- NLMSG_OK(nh, left);
- nh = NLMSG_NEXT(nh, left)) {
- uint16_t flags = nh->nlmsg_flags;
- int rc;
-
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
rc = nl_do(s_dst, nh, RTM_NEWROUTE,
- (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
- nh->nlmsg_len);
+ (flags & ~NLM_F_DUMP_FILTERED) |
+ NLM_F_CREATE,
+ nh->nlmsg_len);
if (rc < 0 && rc != -EEXIST &&
rc != -ENETUNREACH && rc != -EHOSTUNREACH)
return rc;
}
+ if (status < 0)
+ return status;
}
-
return 0;
}
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] netlink: Fix nl_route_dup by 3-step processing
2026-11-07 11:46 [PATCH] netlink: Fix nl_route_dup by 3-step processing Martin Schitter
@ 2026-11-11 1:47 ` Stefano Brivio
2026-11-27 14:31 ` [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch Jon Maloy
1 sibling, 0 replies; 8+ messages in thread
From: Stefano Brivio @ 2026-11-11 1:47 UTC (permalink / raw)
To: Martin Schitter; +Cc: passt-dev
On Sat, 7 Nov 2026 12:46:21 +0100
Martin Schitter <ms+git@mur.at> wrote:
> Eliminate the route entries capacity limitations of nl_route_dup
> by copying them to the destination namespace in three successive
> steps to bypass dependency issues. This approach is inspired by
> the code design used in the restore subcommand of iproute2.
>
> [...]
Sorry, I'm not sure why, but this breaks Podman tests on my setup,
which has (for IPv4) a single /27 address, and a default, single route
via the gateway. If I do:
./pasta -a 192.0.2.1
I get EINVAL on the second route insertion in nl_route_dup(). If the
address is configured as /32 it works, if it's anything else (/31 or
shorter mask), it doesn't.
Isolated one-line reproducer (which should be independent from the
network setup):
$ ./pasta -Ix -- sh -c 'ip l set dev x up && ip a a 198.50.100.2/31 dev x && ./pasta -4 --config-net -a 192.0.2.1'
Couldn't set IPv4 route(s) in guest: Invalid argument
I didn't debug further than that.
--
Stefano
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
2026-11-07 11:46 [PATCH] netlink: Fix nl_route_dup by 3-step processing Martin Schitter
2026-11-11 1:47 ` Stefano Brivio
@ 2026-11-27 14:31 ` Jon Maloy
2026-11-27 15:26 ` Stefano Brivio
2026-11-27 16:05 ` Martin Schitter
1 sibling, 2 replies; 8+ messages in thread
From: Jon Maloy @ 2026-11-27 14:31 UTC (permalink / raw)
To: sbrivio, jmaloy, passt-dev; +Cc: Martin Schitter
From: Martin Schitter <ms+git@mur.at>
The kernel splits large route dumps over several netlink datagrams, but
nl_route_dup() currently only processes the first one, and fails with
"Too many routes to duplicate" if there are more. This happens when
there are more than about 50 routes on the template interface.
Change the code so that all datagrams are processed. Routes are copied
to the destination namespace in three successive steps, to satisfy
dependencies between them:
- routes to local addresses
- routes to local networks
- routes with a gateway
This approach is inspired by the restore subcommand of iproute2.
Link: https://bugs.passt.top/show_bug.cgi?id=221
Signed-off-by: Martin Schitter <ms+git@mur.at>
[jmaloy: Keep stripping of RTA_PREFSRC and RTA_NH_ID, bounds check on
attribute types, style fixes]
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v2 (Jon Maloy): Martin hasn't been available lately, so I picked this
up, with Stefano's consent:
- RTA_OIF, RTA_MULTIPATH, RTA_PREFSRC and RTA_NH_ID were handled in a
single if/else chain, but they're not mutually exclusive: as almost
every route has RTA_OIF, RTA_PREFSRC and RTA_NH_ID were no longer
stripped, which caused the EINVAL Stefano reported with a /31
template address and -a. Strip them independently again, as the
original code did
- Ignore attribute types above RTA_MAX instead of writing past tb[]
- Reworded commit message, style fixes
- Tested with Stefano's reproducer, with 300 routes on the template
interface, and with routes copied together with the device route
for the gateway.
---
netlink.c | 233 ++++++++++++++++++++++++++++--------------------------
1 file changed, 120 insertions(+), 113 deletions(-)
diff --git a/netlink.c b/netlink.c
index a0f8783b..8b16ebcc 100644
--- a/netlink.c
+++ b/netlink.c
@@ -533,6 +533,21 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
return nl_do(s, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
}
+/**
+ * nl_rtattr_cmp() - Compare two routing attributes
+ * @rta1: Routing attribute
+ * @rta2: Routing attribute
+ *
+ * Return: 0 if equal
+ */
+static int nl_rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
+{
+ if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
+ return 1;
+
+ return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
+}
+
/**
* nl_route_dup() - Copy routes for given interface and address family
* @s_src: Netlink socket in source namespace
@@ -546,53 +561,82 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
int nl_route_dup(int s_src, unsigned int ifi_src,
int s_dst, unsigned int ifi_dst, sa_family_t af)
{
- struct req_t {
- struct nlmsghdr nlh;
- struct rtmsg rtm;
- struct rtattr rta;
- unsigned int ifi;
- } req = {
- .rtm.rtm_family = af,
- .rtm.rtm_table = RT_TABLE_MAIN,
- .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
- .rtm.rtm_type = RTN_UNICAST,
-
- .rta.rta_type = RTA_OIF,
- .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
- .ifi = ifi_src,
+ /* Duplicate routes in correct order using 3 successive stages: */
+ enum tasks {
+ DUP_LOCAL_ADDR, /* Duplicate local route address entries */
+ DUP_LOCAL_NET, /* Duplicate local network entries */
+ DUP_OTHER, /* Duplicate all other entries */
+ NR_OF_TASKS,
};
- ssize_t nlmsgs_size, left, status;
- unsigned dup_routes = 0;
- struct nlmsghdr *nh;
- char buf[NLBUFSIZ];
- uint32_t seq;
- unsigned i;
- seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
+ for (enum tasks task = DUP_LOCAL_ADDR; task < NR_OF_TASKS; task++) {
+ struct req_t {
+ struct nlmsghdr nlh;
+ struct rtmsg rtm;
+ struct rtattr rta;
+ unsigned int ifi;
+ } req = {
+ .rtm.rtm_family = af,
+ .rtm.rtm_table = RT_TABLE_MAIN,
+ .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
+ .rtm.rtm_type = RTN_UNICAST,
+
+ .rta.rta_type = RTA_OIF,
+ .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
+ .ifi = ifi_src,
+ };
+ struct nlmsghdr *nh;
+ char buf[NLBUFSIZ];
+ ssize_t status;
+ uint32_t seq;
+
+ seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP,
+ sizeof(req));
+
+ nl_foreach(nh, status, s_src, buf, seq) {
+ struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
+ struct rtattr *tb[RTA_MAX + 1] = { 0 };
+ uint16_t flags = nh->nlmsg_flags;
+ struct rtattr *rta;
+ size_t na;
+ int rc;
- /* nl_foreach() will step through multiple response datagrams,
- * which we don't want here because we need to have all the
- * routes in the buffer at once.
- */
- nh = nl_next(s_src, buf, NULL, &nlmsgs_size);
- for (left = nlmsgs_size;
- NLMSG_OK(nh, left) && (status = nl_status(nh, left, seq)) > 0;
- nh = NLMSG_NEXT(nh, left)) {
- struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
- bool discard = false;
- struct rtattr *rta;
- size_t na;
+ if (nh->nlmsg_type != RTM_NEWROUTE)
+ continue;
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
+ for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh);
+ RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
+ /* Newer kernels might send attributes we don't
+ * know about: ignore them
+ */
+ if (rta->rta_type <= RTA_MAX)
+ tb[rta->rta_type] = rta;
+ }
+
+ /* Duplicate only those routes that belong
+ * to the current processing stage.
+ * An approach inspired by iproute2's
+ * restore_handler() function.
+ * https://github.com/iproute2/iproute2/blob/e6471d7/ip/iproute.c#L2314
+ */
+ if (task == DUP_OTHER && !tb[RTA_GATEWAY])
+ continue;
+ if (task == DUP_LOCAL_NET &&
+ (tb[RTA_GATEWAY] || !tb[RTA_PREFSRC] ||
+ !nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
+ continue;
+ if (task == DUP_LOCAL_ADDR &&
+ (tb[RTA_GATEWAY] ||
+ (tb[RTA_PREFSRC] &&
+ nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))))
+ continue;
- /* nexthop state flags don't apply to freshly created routes,
- * and the kernel will refuse our route if they are set.
- */
- rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
+ /* nexthop state flags don't apply to freshly created
+ * routes, and the kernel will refuse our route if they
+ * are set.
+ */
+ rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
- for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
- rta = RTA_NEXT(rta, na)) {
/* RTA_OIF and RTA_MULTIPATH attributes carry the
* identifier of a host interface. If they match the
* host interface we're copying from, change them to
@@ -603,19 +647,20 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
* available), or if any interface index in nexthop
* objects differ from the host interface, discard the
* route altogether.
- */
- if (rta->rta_type == RTA_OIF) {
- if (*(unsigned int *)RTA_DATA(rta) != ifi_src) {
- discard = true;
- break;
- }
-
- *(unsigned int *)RTA_DATA(rta) = ifi_dst;
- } else if (rta->rta_type == RTA_MULTIPATH) {
- int nh_len = RTA_PAYLOAD(rta);
+ */
+ if (tb[RTA_OIF]) {
+ if (*(unsigned int *)RTA_DATA(tb[RTA_OIF]) !=
+ ifi_src)
+ continue;
+ *(unsigned int *)RTA_DATA(tb[RTA_OIF]) =
+ ifi_dst;
+ } else if (tb[RTA_MULTIPATH]) {
+ int nh_len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
struct rtnexthop *rtnh;
+ bool discard = false;
- for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
+ for (rtnh = (struct rtnexthop *)
+ RTA_DATA(tb[RTA_MULTIPATH]);
RTNH_OK(rtnh, nh_len);
rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
int src = (int)ifi_src;
@@ -629,75 +674,37 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
}
if (discard)
- break;
- } else if (rta->rta_type == RTA_PREFSRC ||
- rta->rta_type == RTA_NH_ID) {
- /* Strip RTA_PREFSRC attributes: host routes
- * might include a preferred source address,
- * which must be one of the host's addresses.
- * However, with -a, pasta will use a different
- * namespace address, making such a route
- * invalid in the namespace.
- *
- * Strip RTA_NH_ID attributes: host routes set
- * up via routing protocols (e.g. OSPF) might
- * contain a nexthop ID (and not nexthop
- * objects, which are taken care of in the
- * RTA_MULTIPATH case above) that's not valid
- * in the target namespace.
- */
- rta->rta_type = RTA_UNSPEC;
+ continue;
}
- }
-
- if (discard)
- nh->nlmsg_type = NLMSG_NOOP;
- else
- dup_routes++;
- }
- if (!NLMSG_OK(nh, left)) {
- /* Process any remaining datagrams in a different
- * buffer so we don't overwrite the first one.
- */
- char tail[NLBUFSIZ];
- unsigned extra = 0;
-
- nl_foreach_oftype(nh, status, s_src, tail, seq, RTM_NEWROUTE)
- extra++;
-
- if (extra) {
- err("netlink: Too many routes to duplicate");
- return -E2BIG;
- }
- }
- if (status < 0)
- return status;
-
- /* Routes might have dependencies between each other, and the kernel
- * processes RTM_NEWROUTE messages sequentially. For n routes, we might
- * need to send the requests up to n times to get all of them inserted.
- * Routes that have been already inserted will return -EEXIST, but we
- * can safely ignore that and repeat the requests. This avoids the need
- * to calculate dependencies: let the kernel do that.
- */
- for (i = 0; i < dup_routes; i++) {
- for (nh = (struct nlmsghdr *)buf, left = nlmsgs_size;
- NLMSG_OK(nh, left);
- nh = NLMSG_NEXT(nh, left)) {
- uint16_t flags = nh->nlmsg_flags;
- int rc;
-
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
+ /* Strip RTA_PREFSRC attributes: host routes might
+ * include a preferred source address, which must be one
+ * of the host's addresses. However, with -a, pasta will
+ * use a different namespace address, making such a
+ * route invalid in the namespace.
+ */
+ if (tb[RTA_PREFSRC])
+ tb[RTA_PREFSRC]->rta_type = RTA_UNSPEC;
+
+ /* Strip RTA_NH_ID attributes: host routes set up via
+ * routing protocols (e.g. OSPF) might contain a nexthop
+ * ID (and not nexthop objects, which are taken care of
+ * in the RTA_MULTIPATH case above) that's not valid in
+ * the target namespace.
+ */
+ if (tb[RTA_NH_ID])
+ tb[RTA_NH_ID]->rta_type = RTA_UNSPEC;
rc = nl_do(s_dst, nh, RTM_NEWROUTE,
- (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
- nh->nlmsg_len);
+ (flags & ~NLM_F_DUMP_FILTERED) |
+ NLM_F_CREATE, nh->nlmsg_len);
if (rc < 0 && rc != -EEXIST &&
rc != -ENETUNREACH && rc != -EHOSTUNREACH)
return rc;
}
+
+ if (status < 0)
+ return status;
}
return 0;
--
2.56.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
2026-11-27 14:31 ` [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch Jon Maloy
@ 2026-11-27 15:26 ` Stefano Brivio
2026-11-27 16:26 ` Martin Schitter
2026-11-27 16:05 ` Martin Schitter
1 sibling, 1 reply; 8+ messages in thread
From: Stefano Brivio @ 2026-11-27 15:26 UTC (permalink / raw)
To: Martin Schitter; +Cc: Jon Maloy, passt-dev
Martin, this new version looks good to me and also fixes the issue I
hit on my setup, so I would go ahead and merge it.
If you plan to give it a try as well, though, I'd wait for you to
confirm before merging. Let me know.
By the way, even on my setup with two routes, it cuts pasta's starting
time by about 10 ms (79 ms instead of 87 ms, average of 10k runs).
That's quite impressive!
--
Stefano
On Fri, 27 Nov 2026 09:31:04 -0500
Jon Maloy <jmaloy@redhat.com> wrote:
> From: Martin Schitter <ms+git@mur.at>
>
> The kernel splits large route dumps over several netlink datagrams, but
> nl_route_dup() currently only processes the first one, and fails with
> "Too many routes to duplicate" if there are more. This happens when
> there are more than about 50 routes on the template interface.
>
> Change the code so that all datagrams are processed. Routes are copied
> to the destination namespace in three successive steps, to satisfy
> dependencies between them:
> - routes to local addresses
> - routes to local networks
> - routes with a gateway
>
> This approach is inspired by the restore subcommand of iproute2.
>
> Link: https://bugs.passt.top/show_bug.cgi?id=221
> Signed-off-by: Martin Schitter <ms+git@mur.at>
> [jmaloy: Keep stripping of RTA_PREFSRC and RTA_NH_ID, bounds check on
> attribute types, style fixes]
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
>
> ---
> v2 (Jon Maloy): Martin hasn't been available lately, so I picked this
> up, with Stefano's consent:
> - RTA_OIF, RTA_MULTIPATH, RTA_PREFSRC and RTA_NH_ID were handled in a
> single if/else chain, but they're not mutually exclusive: as almost
> every route has RTA_OIF, RTA_PREFSRC and RTA_NH_ID were no longer
> stripped, which caused the EINVAL Stefano reported with a /31
> template address and -a. Strip them independently again, as the
> original code did
> - Ignore attribute types above RTA_MAX instead of writing past tb[]
> - Reworded commit message, style fixes
> - Tested with Stefano's reproducer, with 300 routes on the template
> interface, and with routes copied together with the device route
> for the gateway.
> ---
> netlink.c | 233 ++++++++++++++++++++++++++++--------------------------
> 1 file changed, 120 insertions(+), 113 deletions(-)
>
> diff --git a/netlink.c b/netlink.c
> index a0f8783b..8b16ebcc 100644
> --- a/netlink.c
> +++ b/netlink.c
> @@ -533,6 +533,21 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
> return nl_do(s, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
> }
>
> +/**
> + * nl_rtattr_cmp() - Compare two routing attributes
> + * @rta1: Routing attribute
> + * @rta2: Routing attribute
> + *
> + * Return: 0 if equal
> + */
> +static int nl_rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
> +{
> + if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
> + return 1;
> +
> + return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
> +}
> +
> /**
> * nl_route_dup() - Copy routes for given interface and address family
> * @s_src: Netlink socket in source namespace
> @@ -546,53 +561,82 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
> int nl_route_dup(int s_src, unsigned int ifi_src,
> int s_dst, unsigned int ifi_dst, sa_family_t af)
> {
> - struct req_t {
> - struct nlmsghdr nlh;
> - struct rtmsg rtm;
> - struct rtattr rta;
> - unsigned int ifi;
> - } req = {
> - .rtm.rtm_family = af,
> - .rtm.rtm_table = RT_TABLE_MAIN,
> - .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
> - .rtm.rtm_type = RTN_UNICAST,
> -
> - .rta.rta_type = RTA_OIF,
> - .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
> - .ifi = ifi_src,
> + /* Duplicate routes in correct order using 3 successive stages: */
> + enum tasks {
> + DUP_LOCAL_ADDR, /* Duplicate local route address entries */
> + DUP_LOCAL_NET, /* Duplicate local network entries */
> + DUP_OTHER, /* Duplicate all other entries */
> + NR_OF_TASKS,
> };
> - ssize_t nlmsgs_size, left, status;
> - unsigned dup_routes = 0;
> - struct nlmsghdr *nh;
> - char buf[NLBUFSIZ];
> - uint32_t seq;
> - unsigned i;
>
> - seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
> + for (enum tasks task = DUP_LOCAL_ADDR; task < NR_OF_TASKS; task++) {
> + struct req_t {
> + struct nlmsghdr nlh;
> + struct rtmsg rtm;
> + struct rtattr rta;
> + unsigned int ifi;
> + } req = {
> + .rtm.rtm_family = af,
> + .rtm.rtm_table = RT_TABLE_MAIN,
> + .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
> + .rtm.rtm_type = RTN_UNICAST,
> +
> + .rta.rta_type = RTA_OIF,
> + .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
> + .ifi = ifi_src,
> + };
> + struct nlmsghdr *nh;
> + char buf[NLBUFSIZ];
> + ssize_t status;
> + uint32_t seq;
> +
> + seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP,
> + sizeof(req));
> +
> + nl_foreach(nh, status, s_src, buf, seq) {
> + struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
> + struct rtattr *tb[RTA_MAX + 1] = { 0 };
> + uint16_t flags = nh->nlmsg_flags;
> + struct rtattr *rta;
> + size_t na;
> + int rc;
>
> - /* nl_foreach() will step through multiple response datagrams,
> - * which we don't want here because we need to have all the
> - * routes in the buffer at once.
> - */
> - nh = nl_next(s_src, buf, NULL, &nlmsgs_size);
> - for (left = nlmsgs_size;
> - NLMSG_OK(nh, left) && (status = nl_status(nh, left, seq)) > 0;
> - nh = NLMSG_NEXT(nh, left)) {
> - struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
> - bool discard = false;
> - struct rtattr *rta;
> - size_t na;
> + if (nh->nlmsg_type != RTM_NEWROUTE)
> + continue;
>
> - if (nh->nlmsg_type != RTM_NEWROUTE)
> - continue;
> + for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh);
> + RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
> + /* Newer kernels might send attributes we don't
> + * know about: ignore them
> + */
> + if (rta->rta_type <= RTA_MAX)
> + tb[rta->rta_type] = rta;
> + }
> +
> + /* Duplicate only those routes that belong
> + * to the current processing stage.
> + * An approach inspired by iproute2's
> + * restore_handler() function.
> + * https://github.com/iproute2/iproute2/blob/e6471d7/ip/iproute.c#L2314
> + */
> + if (task == DUP_OTHER && !tb[RTA_GATEWAY])
> + continue;
> + if (task == DUP_LOCAL_NET &&
> + (tb[RTA_GATEWAY] || !tb[RTA_PREFSRC] ||
> + !nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
> + continue;
> + if (task == DUP_LOCAL_ADDR &&
> + (tb[RTA_GATEWAY] ||
> + (tb[RTA_PREFSRC] &&
> + nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))))
> + continue;
>
> - /* nexthop state flags don't apply to freshly created routes,
> - * and the kernel will refuse our route if they are set.
> - */
> - rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
> + /* nexthop state flags don't apply to freshly created
> + * routes, and the kernel will refuse our route if they
> + * are set.
> + */
> + rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
>
> - for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
> - rta = RTA_NEXT(rta, na)) {
> /* RTA_OIF and RTA_MULTIPATH attributes carry the
> * identifier of a host interface. If they match the
> * host interface we're copying from, change them to
> @@ -603,19 +647,20 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
> * available), or if any interface index in nexthop
> * objects differ from the host interface, discard the
> * route altogether.
> - */
> - if (rta->rta_type == RTA_OIF) {
> - if (*(unsigned int *)RTA_DATA(rta) != ifi_src) {
> - discard = true;
> - break;
> - }
> -
> - *(unsigned int *)RTA_DATA(rta) = ifi_dst;
> - } else if (rta->rta_type == RTA_MULTIPATH) {
> - int nh_len = RTA_PAYLOAD(rta);
> + */
> + if (tb[RTA_OIF]) {
> + if (*(unsigned int *)RTA_DATA(tb[RTA_OIF]) !=
> + ifi_src)
> + continue;
> + *(unsigned int *)RTA_DATA(tb[RTA_OIF]) =
> + ifi_dst;
> + } else if (tb[RTA_MULTIPATH]) {
> + int nh_len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
> struct rtnexthop *rtnh;
> + bool discard = false;
>
> - for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
> + for (rtnh = (struct rtnexthop *)
> + RTA_DATA(tb[RTA_MULTIPATH]);
> RTNH_OK(rtnh, nh_len);
> rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
> int src = (int)ifi_src;
> @@ -629,75 +674,37 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
> }
>
> if (discard)
> - break;
> - } else if (rta->rta_type == RTA_PREFSRC ||
> - rta->rta_type == RTA_NH_ID) {
> - /* Strip RTA_PREFSRC attributes: host routes
> - * might include a preferred source address,
> - * which must be one of the host's addresses.
> - * However, with -a, pasta will use a different
> - * namespace address, making such a route
> - * invalid in the namespace.
> - *
> - * Strip RTA_NH_ID attributes: host routes set
> - * up via routing protocols (e.g. OSPF) might
> - * contain a nexthop ID (and not nexthop
> - * objects, which are taken care of in the
> - * RTA_MULTIPATH case above) that's not valid
> - * in the target namespace.
> - */
> - rta->rta_type = RTA_UNSPEC;
> + continue;
> }
> - }
> -
> - if (discard)
> - nh->nlmsg_type = NLMSG_NOOP;
> - else
> - dup_routes++;
> - }
>
> - if (!NLMSG_OK(nh, left)) {
> - /* Process any remaining datagrams in a different
> - * buffer so we don't overwrite the first one.
> - */
> - char tail[NLBUFSIZ];
> - unsigned extra = 0;
> -
> - nl_foreach_oftype(nh, status, s_src, tail, seq, RTM_NEWROUTE)
> - extra++;
> -
> - if (extra) {
> - err("netlink: Too many routes to duplicate");
> - return -E2BIG;
> - }
> - }
> - if (status < 0)
> - return status;
> -
> - /* Routes might have dependencies between each other, and the kernel
> - * processes RTM_NEWROUTE messages sequentially. For n routes, we might
> - * need to send the requests up to n times to get all of them inserted.
> - * Routes that have been already inserted will return -EEXIST, but we
> - * can safely ignore that and repeat the requests. This avoids the need
> - * to calculate dependencies: let the kernel do that.
> - */
> - for (i = 0; i < dup_routes; i++) {
> - for (nh = (struct nlmsghdr *)buf, left = nlmsgs_size;
> - NLMSG_OK(nh, left);
> - nh = NLMSG_NEXT(nh, left)) {
> - uint16_t flags = nh->nlmsg_flags;
> - int rc;
> -
> - if (nh->nlmsg_type != RTM_NEWROUTE)
> - continue;
> + /* Strip RTA_PREFSRC attributes: host routes might
> + * include a preferred source address, which must be one
> + * of the host's addresses. However, with -a, pasta will
> + * use a different namespace address, making such a
> + * route invalid in the namespace.
> + */
> + if (tb[RTA_PREFSRC])
> + tb[RTA_PREFSRC]->rta_type = RTA_UNSPEC;
> +
> + /* Strip RTA_NH_ID attributes: host routes set up via
> + * routing protocols (e.g. OSPF) might contain a nexthop
> + * ID (and not nexthop objects, which are taken care of
> + * in the RTA_MULTIPATH case above) that's not valid in
> + * the target namespace.
> + */
> + if (tb[RTA_NH_ID])
> + tb[RTA_NH_ID]->rta_type = RTA_UNSPEC;
>
> rc = nl_do(s_dst, nh, RTM_NEWROUTE,
> - (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
> - nh->nlmsg_len);
> + (flags & ~NLM_F_DUMP_FILTERED) |
> + NLM_F_CREATE, nh->nlmsg_len);
> if (rc < 0 && rc != -EEXIST &&
> rc != -ENETUNREACH && rc != -EHOSTUNREACH)
> return rc;
> }
> +
> + if (status < 0)
> + return status;
> }
>
> return 0;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
2026-11-27 14:31 ` [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch Jon Maloy
2026-11-27 15:26 ` Stefano Brivio
@ 2026-11-27 16:05 ` Martin Schitter
1 sibling, 0 replies; 8+ messages in thread
From: Martin Schitter @ 2026-11-27 16:05 UTC (permalink / raw)
To: Jon Maloy, sbrivio, passt-dev
On 11/27/26 15:31, Jon Maloy wrote:
> v2 (Jon Maloy): Martin hasn't been available lately, so I picked this
> up, with Stefano's consent:
> - RTA_OIF, RTA_MULTIPATH, RTA_PREFSRC and RTA_NH_ID were handled in a
> single if/else chain, but they're not mutually exclusive: as almost
> every route has RTA_OIF, RTA_PREFSRC and RTA_NH_ID were no longer
> stripped, which caused the EINVAL Stefano reported with a /31
> template address and -a. Strip them independently again, as the
> original code did
Thanks for this finding!
It looks like plausible solution to me, but I don't have an adequate
test environment to check it more carefully in practice.
> - Ignore attribute types above RTA_MAX instead of writing past tb[]
That's a really important additional improvement -- thanks!
> - Tested with Stefano's reproducer, with 300 routes on the template
> interface, and with routes copied together with the device route
> for the gateway.
IMHO it's really important to collect this kind of test scenarios,
because nobody can fully grasp the complexity of these issues and future
kernel changes.
Thanks for your work -- It looks good to me!
Martin
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
2026-11-27 15:26 ` Stefano Brivio
@ 2026-11-27 16:26 ` Martin Schitter
0 siblings, 0 replies; 8+ messages in thread
From: Martin Schitter @ 2026-11-27 16:26 UTC (permalink / raw)
To: Stefano Brivio; +Cc: Jon Maloy, passt-dev
On 11/27/26 16:26, Stefano Brivio wrote:
> Martin, this new version looks good to me and also fixes the issue I
> hit on my setup, so I would go ahead and merge it.
Yes -- it looks like a very nice solution.
> If you plan to give it a try as well, though, I'd wait for you to
> confirm before merging. Let me know.
I can't test it sufficiently right now, but the explanations and changes
by jmaloy look convincing and perfectly acceptable to me.
> By the way, even on my setup with two routes, it cuts pasta's starting
> time by about 10 ms (79 ms instead of 87 ms, average of 10k runs).
> That's quite impressive!
To be honest, it wasn't all that hard to keep up with that extremely
inefficient original implementation.
Thanks for all your efforts in cleaning up and constructive revisiting!
Martin
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
2026-11-26 21:52 Jon Maloy
@ 2026-11-26 23:57 ` Stefano Brivio
0 siblings, 0 replies; 8+ messages in thread
From: Stefano Brivio @ 2026-11-26 23:57 UTC (permalink / raw)
To: Jon Maloy; +Cc: david, passt-dev
On Thu, 26 Nov 2026 16:52:04 -0500
Jon Maloy <jmaloy@redhat.com> wrote:
> From: Martin Schitter <ms+git@mur.at>
...can you please re-send with Martin Cc'ed, so that he gets the full
patch (not a quote, might be useful for testing)? By the way I don't
think David is particularly interested to be Cc'ed on this one instead.
--
Stefano
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch
@ 2026-11-26 21:52 Jon Maloy
2026-11-26 23:57 ` Stefano Brivio
0 siblings, 1 reply; 8+ messages in thread
From: Jon Maloy @ 2026-11-26 21:52 UTC (permalink / raw)
To: sbrivio, david, jmaloy, passt-dev
From: Martin Schitter <ms+git@mur.at>
The kernel splits large route dumps over several netlink datagrams, but
nl_route_dup() currently only processes the first one, and fails with
"Too many routes to duplicate" if there are more. This happens when
there are more than about 50 routes on the template interface.
Change the code so that all datagrams are processed. Routes are copied
to the destination namespace in three successive steps, to satisfy
dependencies between them:
- routes to local addresses
- routes to local networks
- routes with a gateway
This approach is inspired by the restore subcommand of iproute2.
Link: https://bugs.passt.top/show_bug.cgi?id=221
Signed-off-by: Martin Schitter <ms+git@mur.at>
[jmaloy: Keep stripping of RTA_PREFSRC and RTA_NH_ID, bounds check on
attribute types, style fixes]
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v2 (Jon Maloy): Martin hasn't been available lately, so I picked this
up, with Stefano's consent:
- RTA_OIF, RTA_MULTIPATH, RTA_PREFSRC and RTA_NH_ID were handled in a
single if/else chain, but they're not mutually exclusive: as almost
every route has RTA_OIF, RTA_PREFSRC and RTA_NH_ID were no longer
stripped, which caused the EINVAL Stefano reported with a /31
template address and -a. Strip them independently again, as the
original code did
- Ignore attribute types above RTA_MAX instead of writing past tb[]
- Reworded commit message, style fixes
- Tested with Stefano's reproducer, with 300 routes on the template
interface, and with routes copied together with the device route
for the gateway.
---
netlink.c | 233 ++++++++++++++++++++++++++++--------------------------
1 file changed, 120 insertions(+), 113 deletions(-)
diff --git a/netlink.c b/netlink.c
index a0f8783b..8b16ebcc 100644
--- a/netlink.c
+++ b/netlink.c
@@ -533,6 +533,21 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
return nl_do(s, &req, RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, len);
}
+/**
+ * nl_rtattr_cmp() - Compare two routing attributes
+ * @rta1: Routing attribute
+ * @rta2: Routing attribute
+ *
+ * Return: 0 if equal
+ */
+static int nl_rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
+{
+ if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
+ return 1;
+
+ return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
+}
+
/**
* nl_route_dup() - Copy routes for given interface and address family
* @s_src: Netlink socket in source namespace
@@ -546,53 +561,82 @@ int nl_route_set_def(int s, unsigned int ifi, sa_family_t af, const void *gw)
int nl_route_dup(int s_src, unsigned int ifi_src,
int s_dst, unsigned int ifi_dst, sa_family_t af)
{
- struct req_t {
- struct nlmsghdr nlh;
- struct rtmsg rtm;
- struct rtattr rta;
- unsigned int ifi;
- } req = {
- .rtm.rtm_family = af,
- .rtm.rtm_table = RT_TABLE_MAIN,
- .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
- .rtm.rtm_type = RTN_UNICAST,
-
- .rta.rta_type = RTA_OIF,
- .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
- .ifi = ifi_src,
+ /* Duplicate routes in correct order using 3 successive stages: */
+ enum tasks {
+ DUP_LOCAL_ADDR, /* Duplicate local route address entries */
+ DUP_LOCAL_NET, /* Duplicate local network entries */
+ DUP_OTHER, /* Duplicate all other entries */
+ NR_OF_TASKS,
};
- ssize_t nlmsgs_size, left, status;
- unsigned dup_routes = 0;
- struct nlmsghdr *nh;
- char buf[NLBUFSIZ];
- uint32_t seq;
- unsigned i;
- seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP, sizeof(req));
+ for (enum tasks task = DUP_LOCAL_ADDR; task < NR_OF_TASKS; task++) {
+ struct req_t {
+ struct nlmsghdr nlh;
+ struct rtmsg rtm;
+ struct rtattr rta;
+ unsigned int ifi;
+ } req = {
+ .rtm.rtm_family = af,
+ .rtm.rtm_table = RT_TABLE_MAIN,
+ .rtm.rtm_scope = RT_SCOPE_UNIVERSE,
+ .rtm.rtm_type = RTN_UNICAST,
+
+ .rta.rta_type = RTA_OIF,
+ .rta.rta_len = RTA_LENGTH(sizeof(unsigned int)),
+ .ifi = ifi_src,
+ };
+ struct nlmsghdr *nh;
+ char buf[NLBUFSIZ];
+ ssize_t status;
+ uint32_t seq;
+
+ seq = nl_send(s_src, &req, RTM_GETROUTE, NLM_F_DUMP,
+ sizeof(req));
+
+ nl_foreach(nh, status, s_src, buf, seq) {
+ struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
+ struct rtattr *tb[RTA_MAX + 1] = { 0 };
+ uint16_t flags = nh->nlmsg_flags;
+ struct rtattr *rta;
+ size_t na;
+ int rc;
- /* nl_foreach() will step through multiple response datagrams,
- * which we don't want here because we need to have all the
- * routes in the buffer at once.
- */
- nh = nl_next(s_src, buf, NULL, &nlmsgs_size);
- for (left = nlmsgs_size;
- NLMSG_OK(nh, left) && (status = nl_status(nh, left, seq)) > 0;
- nh = NLMSG_NEXT(nh, left)) {
- struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nh);
- bool discard = false;
- struct rtattr *rta;
- size_t na;
+ if (nh->nlmsg_type != RTM_NEWROUTE)
+ continue;
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
+ for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh);
+ RTA_OK(rta, na); rta = RTA_NEXT(rta, na)) {
+ /* Newer kernels might send attributes we don't
+ * know about: ignore them
+ */
+ if (rta->rta_type <= RTA_MAX)
+ tb[rta->rta_type] = rta;
+ }
+
+ /* Duplicate only those routes that belong
+ * to the current processing stage.
+ * An approach inspired by iproute2's
+ * restore_handler() function.
+ * https://github.com/iproute2/iproute2/blob/e6471d7/ip/iproute.c#L2314
+ */
+ if (task == DUP_OTHER && !tb[RTA_GATEWAY])
+ continue;
+ if (task == DUP_LOCAL_NET &&
+ (tb[RTA_GATEWAY] || !tb[RTA_PREFSRC] ||
+ !nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
+ continue;
+ if (task == DUP_LOCAL_ADDR &&
+ (tb[RTA_GATEWAY] ||
+ (tb[RTA_PREFSRC] &&
+ nl_rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))))
+ continue;
- /* nexthop state flags don't apply to freshly created routes,
- * and the kernel will refuse our route if they are set.
- */
- rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
+ /* nexthop state flags don't apply to freshly created
+ * routes, and the kernel will refuse our route if they
+ * are set.
+ */
+ rtm->rtm_flags &= ~RTNH_COMPARE_MASK;
- for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
- rta = RTA_NEXT(rta, na)) {
/* RTA_OIF and RTA_MULTIPATH attributes carry the
* identifier of a host interface. If they match the
* host interface we're copying from, change them to
@@ -603,19 +647,20 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
* available), or if any interface index in nexthop
* objects differ from the host interface, discard the
* route altogether.
- */
- if (rta->rta_type == RTA_OIF) {
- if (*(unsigned int *)RTA_DATA(rta) != ifi_src) {
- discard = true;
- break;
- }
-
- *(unsigned int *)RTA_DATA(rta) = ifi_dst;
- } else if (rta->rta_type == RTA_MULTIPATH) {
- int nh_len = RTA_PAYLOAD(rta);
+ */
+ if (tb[RTA_OIF]) {
+ if (*(unsigned int *)RTA_DATA(tb[RTA_OIF]) !=
+ ifi_src)
+ continue;
+ *(unsigned int *)RTA_DATA(tb[RTA_OIF]) =
+ ifi_dst;
+ } else if (tb[RTA_MULTIPATH]) {
+ int nh_len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
struct rtnexthop *rtnh;
+ bool discard = false;
- for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
+ for (rtnh = (struct rtnexthop *)
+ RTA_DATA(tb[RTA_MULTIPATH]);
RTNH_OK(rtnh, nh_len);
rtnh = RTNH_NEXT_AND_DEC(rtnh, nh_len)) {
int src = (int)ifi_src;
@@ -629,75 +674,37 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
}
if (discard)
- break;
- } else if (rta->rta_type == RTA_PREFSRC ||
- rta->rta_type == RTA_NH_ID) {
- /* Strip RTA_PREFSRC attributes: host routes
- * might include a preferred source address,
- * which must be one of the host's addresses.
- * However, with -a, pasta will use a different
- * namespace address, making such a route
- * invalid in the namespace.
- *
- * Strip RTA_NH_ID attributes: host routes set
- * up via routing protocols (e.g. OSPF) might
- * contain a nexthop ID (and not nexthop
- * objects, which are taken care of in the
- * RTA_MULTIPATH case above) that's not valid
- * in the target namespace.
- */
- rta->rta_type = RTA_UNSPEC;
+ continue;
}
- }
-
- if (discard)
- nh->nlmsg_type = NLMSG_NOOP;
- else
- dup_routes++;
- }
- if (!NLMSG_OK(nh, left)) {
- /* Process any remaining datagrams in a different
- * buffer so we don't overwrite the first one.
- */
- char tail[NLBUFSIZ];
- unsigned extra = 0;
-
- nl_foreach_oftype(nh, status, s_src, tail, seq, RTM_NEWROUTE)
- extra++;
-
- if (extra) {
- err("netlink: Too many routes to duplicate");
- return -E2BIG;
- }
- }
- if (status < 0)
- return status;
-
- /* Routes might have dependencies between each other, and the kernel
- * processes RTM_NEWROUTE messages sequentially. For n routes, we might
- * need to send the requests up to n times to get all of them inserted.
- * Routes that have been already inserted will return -EEXIST, but we
- * can safely ignore that and repeat the requests. This avoids the need
- * to calculate dependencies: let the kernel do that.
- */
- for (i = 0; i < dup_routes; i++) {
- for (nh = (struct nlmsghdr *)buf, left = nlmsgs_size;
- NLMSG_OK(nh, left);
- nh = NLMSG_NEXT(nh, left)) {
- uint16_t flags = nh->nlmsg_flags;
- int rc;
-
- if (nh->nlmsg_type != RTM_NEWROUTE)
- continue;
+ /* Strip RTA_PREFSRC attributes: host routes might
+ * include a preferred source address, which must be one
+ * of the host's addresses. However, with -a, pasta will
+ * use a different namespace address, making such a
+ * route invalid in the namespace.
+ */
+ if (tb[RTA_PREFSRC])
+ tb[RTA_PREFSRC]->rta_type = RTA_UNSPEC;
+
+ /* Strip RTA_NH_ID attributes: host routes set up via
+ * routing protocols (e.g. OSPF) might contain a nexthop
+ * ID (and not nexthop objects, which are taken care of
+ * in the RTA_MULTIPATH case above) that's not valid in
+ * the target namespace.
+ */
+ if (tb[RTA_NH_ID])
+ tb[RTA_NH_ID]->rta_type = RTA_UNSPEC;
rc = nl_do(s_dst, nh, RTM_NEWROUTE,
- (flags & ~NLM_F_DUMP_FILTERED) | NLM_F_CREATE,
- nh->nlmsg_len);
+ (flags & ~NLM_F_DUMP_FILTERED) |
+ NLM_F_CREATE, nh->nlmsg_len);
if (rc < 0 && rc != -EEXIST &&
rc != -ENETUNREACH && rc != -EHOSTUNREACH)
return rc;
}
+
+ if (status < 0)
+ return status;
}
return 0;
--
2.56.1
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-27 16:26 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-07 11:46 [PATCH] netlink: Fix nl_route_dup by 3-step processing Martin Schitter
2026-11-11 1:47 ` Stefano Brivio
2026-11-27 14:31 ` [PATCH v2] netlink: Copy all routes in nl_route_dup(), not just the first batch Jon Maloy
2026-11-27 15:26 ` Stefano Brivio
2026-11-27 16:26 ` Martin Schitter
2026-11-27 16:05 ` Martin Schitter
2026-11-26 21:52 Jon Maloy
2026-11-26 23:57 ` Stefano Brivio
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).