From 5ebbc05628ea353a6c8c200af2aabb38d0c79249 Mon Sep 17 00:00:00 2001 From: Gordon Ross Date: Sun, 22 Feb 2026 11:03:56 -0500 Subject: [PATCH 1/2] Use PF_LINK on SunOS --- .../System.Native/pal_interfaceaddresses.c | 7 +++++++ .../libs/System.Native/pal_maphardwaretype.c | 7 +++++++ .../libs/System.Native/pal_networking.c | 20 +++++++++++++++++++ .../libs/System.Native/pal_networking.h | 1 + 4 files changed, 35 insertions(+) diff --git a/src/native/libs/System.Native/pal_interfaceaddresses.c b/src/native/libs/System.Native/pal_interfaceaddresses.c index e81e2110860b95..b05119512216b5 100644 --- a/src/native/libs/System.Native/pal_interfaceaddresses.c +++ b/src/native/libs/System.Native/pal_interfaceaddresses.c @@ -46,6 +46,13 @@ #include "ios/net/if_media.h" #endif +// SunOS defines both AF_LINK and AF_PACKET but AF_LINK is preferred. +// Using AF_PACKET on SunOS requires access to system-private headers. +// Use undef to keep all the changes here. +#if defined(TARGET_SUNOS) +#undef AF_PACKET +#endif + #if defined(AF_PACKET) #include #if HAVE_NETPACKET_PACKET_H diff --git a/src/native/libs/System.Native/pal_maphardwaretype.c b/src/native/libs/System.Native/pal_maphardwaretype.c index 2a9e78216074ae..fd7520da21bfe8 100644 --- a/src/native/libs/System.Native/pal_maphardwaretype.c +++ b/src/native/libs/System.Native/pal_maphardwaretype.c @@ -7,6 +7,13 @@ #include #include +// SunOS defines both AF_LINK and AF_PACKET but AF_LINK is preferred. +// Using AF_PACKET on SunOS requires access to system-private headers. +// Use undef to keep all the changes here. +#if defined(TARGET_SUNOS) +#undef AF_PACKET +#endif + #if defined(AF_PACKET) #if HAVE_NETPACKET_PACKET_H #include diff --git a/src/native/libs/System.Native/pal_networking.c b/src/native/libs/System.Native/pal_networking.c index 3b460d4c4e8296..07ed77b7cf7b81 100644 --- a/src/native/libs/System.Native/pal_networking.c +++ b/src/native/libs/System.Native/pal_networking.c @@ -204,6 +204,11 @@ static bool TryConvertAddressFamilyPlatformToPal(sa_family_t platformAddressFami *palAddressFamily = AddressFamily_AF_PACKET; return true; #endif +#ifdef AF_LINK + case AF_LINK: + *palAddressFamily = AddressFamily_AF_LINK; + return true; +#endif #ifdef AF_CAN case AF_CAN: *palAddressFamily = AddressFamily_AF_CAN; @@ -241,6 +246,11 @@ static bool TryConvertAddressFamilyPalToPlatform(int32_t palAddressFamily, sa_fa *platformAddressFamily = AF_PACKET; return true; #endif +#ifdef AF_LINK + case AddressFamily_AF_LINK: + *platformAddressFamily = AF_LINK; + return true; +#endif #ifdef AF_CAN case AddressFamily_AF_CAN: *platformAddressFamily = AF_CAN; @@ -2546,6 +2556,11 @@ static bool TryConvertProtocolTypePalToPlatform(int32_t palAddressFamily, int32_ *platformProtocolType = palProtocolType; return true; #endif +#ifdef AF_LINK + case AddressFamily_AF_LINK: + *platformProtocolType = palProtocolType; + return true; +#endif #if HAVE_LINUX_CAN_H case AddressFamily_AF_CAN: switch (palProtocolType) @@ -2685,6 +2700,11 @@ static bool TryConvertProtocolTypePlatformToPal(int32_t palAddressFamily, int pl *palProtocolType = platformProtocolType; return true; #endif +#ifdef AF_LINK + case AddressFamily_AF_LINK: + *palProtocolType = platformProtocolType; + return true; +#endif #if HAVE_LINUX_CAN_H case AddressFamily_AF_CAN: switch (platformProtocolType) diff --git a/src/native/libs/System.Native/pal_networking.h b/src/native/libs/System.Native/pal_networking.h index c393a3dbad4619..2a42329fc02fe1 100644 --- a/src/native/libs/System.Native/pal_networking.h +++ b/src/native/libs/System.Native/pal_networking.h @@ -66,6 +66,7 @@ typedef enum AddressFamily_AF_UNIX = 1, // System.Net.AddressFamily.Unix AddressFamily_AF_INET = 2, // System.Net.AddressFamily.InterNetwork AddressFamily_AF_INET6 = 23, // System.Net.AddressFamily.InterNetworkV6 + AddressFamily_AF_LINK = 13, // System.Net.AddressFamily.DataLink AddressFamily_AF_PACKET = 65536, // System.Net.AddressFamily.Packet AddressFamily_AF_CAN = 65537, // System.Net.AddressFamily.ControllerAreaNetwork } AddressFamily; From 3207ccc518ffa172761a0d3138766bc2e4d7fa4b Mon Sep 17 00:00:00 2001 From: Gordon Ross Date: Fri, 13 Mar 2026 10:25:40 -0400 Subject: [PATCH 2/2] Add SIOCGIFMTU to the AF_LINK section like AF_PACKET does so this change won't regress about filling in nii->Mtu --- .../System.Native/pal_interfaceaddresses.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/native/libs/System.Native/pal_interfaceaddresses.c b/src/native/libs/System.Native/pal_interfaceaddresses.c index b05119512216b5..c30687fc7d9f2e 100644 --- a/src/native/libs/System.Native/pal_interfaceaddresses.c +++ b/src/native/libs/System.Native/pal_interfaceaddresses.c @@ -455,6 +455,24 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter nii->HardwareType = MapHardwareType(sadl->sdl_type); nii->NumAddressBytes = sadl->sdl_alen; memcpy_s(&nii->AddressBytes, sizeof_member(NetworkInterfaceInfo, AddressBytes), (uint8_t*)LLADDR(sadl), sadl->sdl_alen); + +#if defined(SIOCGIFMTU) + struct ifreq ifr; + strncpy(ifr.ifr_name, nii->Name, sizeof(ifr.ifr_name)); + ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; + if (socketfd == -1) + { + socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + } + + if (socketfd > -1) + { + if (ioctl(socketfd, SIOCGIFMTU, &ifr) == 0) + { + nii->Mtu = ifr.ifr_mtu; + } + } +#endif // SIOCGIFMTU } #endif #if defined(AF_PACKET)