Skip to content

Commit f608e85

Browse files
committed
BSD: Fix netmask family and length for incoming route msgs
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.
1 parent ab2d7cd commit f608e85

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/if-bsd.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,10 +887,22 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm)
887887

888888
rt->rt_flags = (unsigned int)rtm->rtm_flags;
889889
if_copysa(&rt->rt_dest, rti_info[RTAX_DST]);
890+
890891
if (rtm->rtm_addrs & RTA_NETMASK) {
891892
if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
892-
if (rt->rt_netmask.sa_family == 255) /* Why? */
893-
rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
893+
/*
894+
* Netmask family and length are ignored by traditional
895+
* userland tools such as route and netstat and are assumed
896+
* to match the destination sockaddr.
897+
* This is fortunate because BSD kernels use a radix tree
898+
* to store routes which adjusts the netmask at the point
899+
* of insertion where this information is lost.
900+
* We can just sub in the values from the destination address.
901+
*
902+
* This is currently true for all BSD kernels.
903+
*/
904+
rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
905+
rt->rt_netmask.sa_len = rt->rt_dest.sa_len;
894906
}
895907

896908
/* dhcpcd likes an unspecified gateway to indicate via the link.

src/sa.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,6 @@ sa_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
419419
assert(sa1 != NULL);
420420
assert(sa2 != NULL);
421421

422-
/* Treat AF_UNSPEC as the unspecified address. */
423-
if ((sa1->sa_family == AF_UNSPEC || sa2->sa_family == AF_UNSPEC) &&
424-
sa_is_unspecified(sa1) && sa_is_unspecified(sa2))
425-
return 0;
426-
427422
if (sa1->sa_family != sa2->sa_family)
428423
return sa1->sa_family - sa2->sa_family;
429424

0 commit comments

Comments
 (0)