]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
BSD: Fix netmask family and length for incoming route msgs
authorRoy Marples <roy@marples.name>
Tue, 18 Feb 2025 09:48:19 +0000 (09:48 +0000)
committerRoy Marples <roy@marples.name>
Tue, 18 Feb 2025 09:48:19 +0000 (09:48 +0000)
Netmask family and length are ignored by traditional
userland tools such as route and netstat and are assumed
to match the destination sockaddr.
This is fortunate because BSD kernels use a radix tree
to store routes which adjusts the netmask at the point
of insertion where this information is lost.
We can just sub in the values from the destination address.

This is currently true for all BSD kernels.

src/if-bsd.c
src/sa.c

index 8af59610cf725cff5243ac32dc3a8a3454f1bcc0..32b51ba758d92b2c14956f4fd9281afcc80f9811 100644 (file)
@@ -887,10 +887,22 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm)
 
        rt->rt_flags = (unsigned int)rtm->rtm_flags;
        if_copysa(&rt->rt_dest, rti_info[RTAX_DST]);
+
        if (rtm->rtm_addrs & RTA_NETMASK) {
                if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
-               if (rt->rt_netmask.sa_family == 255) /* Why? */
-                       rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
+               /*
+                * Netmask family and length are ignored by traditional
+                * userland tools such as route and netstat and are assumed
+                * to match the destination sockaddr.
+                * This is fortunate because BSD kernels use a radix tree
+                * to store routes which adjusts the netmask at the point
+                * of insertion where this information is lost.
+                * We can just sub in the values from the destination address.
+                *
+                * This is currently true for all BSD kernels.
+                */
+               rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
+               rt->rt_netmask.sa_len = rt->rt_dest.sa_len;
        }
 
        /* dhcpcd likes an unspecified gateway to indicate via the link.
index 9fdccb693ad4397bf107c853c795b5414f9890c9..e8bff04fab1873091cd844f12d843e8e34b388ff 100644 (file)
--- a/src/sa.c
+++ b/src/sa.c
@@ -419,11 +419,6 @@ sa_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
        assert(sa1 != NULL);
        assert(sa2 != NULL);
 
-       /* Treat AF_UNSPEC as the unspecified address. */
-       if ((sa1->sa_family == AF_UNSPEC || sa2->sa_family == AF_UNSPEC) &&
-           sa_is_unspecified(sa1) && sa_is_unspecified(sa2))
-               return 0;
-
        if (sa1->sa_family != sa2->sa_family)
                return sa1->sa_family - sa2->sa_family;