Skip to content

Commit 5e770dc

Browse files
authored
LocalIP: consider preferred source address when retrieving default route (#1883)
* LocalIP: consider preferred source address when retrieving default route * LocalIP: fix logic error when there is no preferred source address * LocalIP: use macros rather than manual free/close * LocalIP(BSD): fix build error * LocalIP: address Copilot suggestions
1 parent 137d5df commit 5e770dc

File tree

7 files changed

+214
-25
lines changed

7 files changed

+214
-25
lines changed

src/common/netif/netif.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
#include <iphlpapi.h>
88
#endif
99

10-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex);
10+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr);
1111
enum { IF_INDEX_UNINITIALIZED = (uint32_t) -1, IF_INDEX_INVALID = (uint32_t) -2 };
1212
static uint32_t ifIndex = IF_INDEX_UNINITIALIZED;
1313
static char ifName[IF_NAMESIZE + 1];
14+
static uint32_t preferredSourceAddr = 0;
1415

1516
static inline void init()
1617
{
17-
if (ifIndex == (uint32_t) IF_INDEX_UNINITIALIZED && !ffNetifGetDefaultRouteImpl(ifName, &ifIndex))
18-
ifIndex = (uint32_t) IF_INDEX_INVALID;
18+
if (ifIndex == (uint32_t) IF_INDEX_UNINITIALIZED) {
19+
if (!ffNetifGetDefaultRouteImpl(ifName, &ifIndex, &preferredSourceAddr))
20+
ifIndex = (uint32_t) IF_INDEX_INVALID;
21+
}
1922
}
2023

2124
const char* ffNetifGetDefaultRouteIfName()
@@ -29,3 +32,9 @@ uint32_t ffNetifGetDefaultRouteIfIndex()
2932
init();
3033
return ifIndex;
3134
}
35+
36+
uint32_t ffNetifGetDefaultRoutePreferredSourceAddr()
37+
{
38+
init();
39+
return preferredSourceAddr;
40+
}

src/common/netif/netif.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
const char* ffNetifGetDefaultRouteIfName();
66
uint32_t ffNetifGetDefaultRouteIfIndex();
7+
uint32_t ffNetifGetDefaultRoutePreferredSourceAddr();

src/common/netif/netif_bsd.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ get_rt_address(struct rt_msghdr *rtm, int desired)
5050
return NULL;
5151
}
5252

53-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
53+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
5454
{
55+
if (preferredSourceAddr)
56+
*preferredSourceAddr = 0;
57+
5558
#if defined(__OpenBSD__) || defined(__DragonFly__)
5659
int mib[6] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_GATEWAY};
5760
size_t needed;
@@ -80,6 +83,12 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
8083
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
8184
iface[sdl->sdl_nlen] = '\0';
8285
*ifIndex = sdl->sdl_index;
86+
87+
// Get the preferred source address
88+
struct sockaddr_in* src = (struct sockaddr_in*)get_rt_address(rtm, RTA_IFA);
89+
if (preferredSourceAddr && src && src->sin_family == AF_INET)
90+
*preferredSourceAddr = src->sin_addr.s_addr;
91+
8392
return true;
8493
}
8594
}
@@ -108,7 +117,7 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
108117
.rtm_type = RTM_GET,
109118
.rtm_flags = RTF_UP | RTF_GATEWAY,
110119
.rtm_version = RTM_VERSION,
111-
.rtm_addrs = RTA_DST | RTA_IFP,
120+
.rtm_addrs = RTA_DST | RTA_IFP | RTA_IFA,
112121
.rtm_msglen = sizeof(rtmsg.hdr) + sizeof(rtmsg.dst),
113122
.rtm_pid = pid,
114123
.rtm_seq = 1,
@@ -139,6 +148,12 @@ bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
139148
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
140149
iface[sdl->sdl_nlen] = '\0';
141150
*ifIndex = sdl->sdl_index;
151+
152+
// Get the preferred source address
153+
struct sockaddr_in* src = (struct sockaddr_in*)get_rt_address(&rtmsg.hdr, RTA_IFA);
154+
if (preferredSourceAddr && src && src->sin_family == AF_INET)
155+
*preferredSourceAddr = src->sin_addr.s_addr;
156+
142157
return true;
143158
}
144159
return false;

src/common/netif/netif_haiku.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
// loosely based on Haiku's src/bin/network/route/route.cpp
1313

14-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
14+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
1515
{
16+
if (preferredSourceAddr)
17+
*preferredSourceAddr = 0;
18+
1619
// TODO: AF_INET6
1720
FF_AUTO_CLOSE_FD int pfRoute = socket(AF_INET, SOCK_RAW, AF_INET);
1821
if (pfRoute < 0)

src/common/netif/netif_linux.c

Lines changed: 165 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,179 @@
11
#include "netif.h"
22
#include "common/io/io.h"
3+
#include "util/mallocHelper.h"
34

5+
#include <arpa/inet.h>
6+
#include <linux/rtnetlink.h>
47
#include <net/if.h>
5-
#include <stdio.h>
68

79
#define FF_STR_INDIR(x) #x
810
#define FF_STR(x) FF_STR_INDIR(x)
911

10-
static bool getDefaultRouteIPv4(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
12+
struct req_t {
13+
struct nlmsghdr nlh;
14+
struct rtmsg rtm;
15+
struct rtattr rta;
16+
uint32_t table;
17+
};
18+
19+
struct Route4Entry {
20+
uint32_t dest;
21+
uint32_t gateway;
22+
uint32_t src;
23+
uint8_t prefix_length;
24+
uint32_t metric;
25+
uint32_t ifindex;
26+
};
27+
28+
static bool getDefaultRouteIPv4(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
1129
{
12-
FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/net/route", "r");
13-
if (!netRoute) return false;
30+
FF_AUTO_CLOSE_FD int sock_fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
31+
if (sock_fd < 0)
32+
return false;
1433

15-
// skip first line
16-
FF_UNUSED(fscanf(netRoute, "%*[^\n]\n"));
34+
// Bind socket
35+
struct sockaddr_nl addr;
36+
memset(&addr, 0, sizeof(addr));
37+
addr.nl_family = AF_NETLINK;
38+
addr.nl_pid = 0; // Let kernel assign PID
39+
addr.nl_groups = 0;
1740

18-
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, ...
19-
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
20-
{
21-
if (destination != 0) continue;
22-
*ifIndex = if_nametoindex(iface);
41+
if (bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
42+
return false;
43+
}
44+
45+
struct req_t req;
46+
memset(&req, 0, sizeof(req));
47+
48+
// Netlink message header
49+
req.nlh.nlmsg_len = sizeof(req);
50+
req.nlh.nlmsg_type = RTM_GETROUTE;
51+
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
52+
req.nlh.nlmsg_seq = 0;
53+
req.nlh.nlmsg_pid = 0;
54+
55+
// Route message
56+
req.rtm.rtm_family = AF_INET;
57+
req.rtm.rtm_dst_len = 0;
58+
req.rtm.rtm_src_len = 0;
59+
req.rtm.rtm_tos = 0;
60+
req.rtm.rtm_table = RT_TABLE_UNSPEC;
61+
req.rtm.rtm_protocol = RTPROT_UNSPEC;
62+
req.rtm.rtm_scope = RT_SCOPE_UNIVERSE;
63+
req.rtm.rtm_type = RTN_UNSPEC;
64+
req.rtm.rtm_flags = 0;
65+
66+
// Route attribute for main table
67+
req.rta.rta_len = RTA_LENGTH(sizeof(uint32_t));
68+
req.rta.rta_type = RTA_TABLE;
69+
req.table = RT_TABLE_MAIN;
70+
71+
struct sockaddr_nl dest_addr;
72+
memset(&dest_addr, 0, sizeof(dest_addr));
73+
dest_addr.nl_family = AF_NETLINK;
74+
dest_addr.nl_pid = 0;
75+
dest_addr.nl_groups = 0;
76+
77+
ssize_t sent = sendto(sock_fd, &req, sizeof(req), 0,
78+
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
79+
80+
if (sent != sizeof(req)) {
81+
return false;
82+
}
83+
84+
struct sockaddr_nl src_addr;
85+
socklen_t src_addr_len = sizeof(src_addr);
86+
struct iovec iov = {NULL, 0};
87+
struct msghdr msg;
88+
89+
memset(&msg, 0, sizeof(msg));
90+
msg.msg_name = &src_addr;
91+
msg.msg_namelen = sizeof(src_addr);
92+
msg.msg_iov = &iov;
93+
msg.msg_iovlen = 1;
94+
95+
ssize_t peek_size = recvmsg(sock_fd, &msg, MSG_PEEK | MSG_TRUNC);
96+
if (peek_size < 0) {
97+
return false;
98+
}
99+
100+
FF_AUTO_FREE uint8_t* buffer = malloc((size_t)peek_size);
101+
102+
ssize_t received = recvfrom(sock_fd, buffer, (size_t)peek_size, 0,
103+
(struct sockaddr*)&src_addr, &src_addr_len);
104+
if (received != peek_size) {
105+
return false;
106+
}
107+
108+
struct Route4Entry best_gw;
109+
memset(&best_gw, 0, sizeof(best_gw));
110+
111+
for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer;
112+
NLMSG_OK(nlh, received);
113+
nlh = NLMSG_NEXT(nlh, received)) {
114+
115+
if (nlh->nlmsg_type == NLMSG_DONE)
116+
break;
117+
118+
if (nlh->nlmsg_type != RTM_NEWROUTE)
119+
continue;
120+
121+
struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh);
122+
if (rtm->rtm_family != AF_INET)
123+
continue;
124+
125+
struct Route4Entry entry;
126+
memset(&entry, 0, sizeof(struct Route4Entry));
127+
entry.prefix_length = rtm->rtm_dst_len;
128+
129+
// Parse route attributes
130+
size_t rtm_len = RTM_PAYLOAD(nlh);
131+
for (struct rtattr* rta = RTM_RTA(rtm);
132+
RTA_OK(rta, rtm_len);
133+
rta = RTA_NEXT(rta, rtm_len)) {
134+
135+
switch (rta->rta_type) {
136+
case RTA_DST:
137+
entry.dest = *(uint32_t*)RTA_DATA(rta);
138+
break;
139+
case RTA_GATEWAY:
140+
entry.gateway = *(uint32_t*)RTA_DATA(rta);
141+
break;
142+
case RTA_PREFSRC:
143+
entry.src = *(uint32_t*)RTA_DATA(rta);
144+
break;
145+
case RTA_PRIORITY:
146+
entry.metric = *(uint32_t*)RTA_DATA(rta);
147+
break;
148+
case RTA_OIF:
149+
entry.ifindex = *(uint32_t*)RTA_DATA(rta);
150+
break;
151+
}
152+
}
153+
154+
if (entry.gateway == 0 || entry.dest != 0 || entry.prefix_length != 0)
155+
continue;
156+
157+
if (best_gw.gateway == 0 || entry.metric < best_gw.metric) {
158+
memcpy(&best_gw, &entry, sizeof(struct Route4Entry));
159+
}
160+
}
161+
162+
if (best_gw.gateway != 0) {
163+
if (ifIndex) {
164+
*ifIndex = best_gw.ifindex;
165+
}
166+
if (iface) {
167+
if (if_indextoname(best_gw.ifindex, iface) == NULL) {
168+
iface[0] = '\0';
169+
}
170+
}
171+
if (preferredSourceAddr) {
172+
*preferredSourceAddr = best_gw.src;
173+
}
23174
return true;
24175
}
176+
25177
iface[0] = '\0';
26178
return false;
27179
}
@@ -43,9 +195,9 @@ static bool getDefaultRouteIPv6(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
43195
return false;
44196
}
45197

46-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
198+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
47199
{
48-
if (getDefaultRouteIPv4(iface, ifIndex))
200+
if (getDefaultRouteIPv4(iface, ifIndex, preferredSourceAddr))
49201
return true;
50202

51203
return getDefaultRouteIPv6(iface, ifIndex);

src/common/netif/netif_windows.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
#include <ws2tcpip.h> // AF_INET6, IN6_IS_ADDR_UNSPECIFIED
55
#include <iphlpapi.h>
66

7-
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex)
7+
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1], uint32_t* ifIndex, uint32_t* preferredSourceAddr)
88
{
9+
if (preferredSourceAddr)
10+
*preferredSourceAddr = 0;
11+
912
PMIB_IPFORWARD_TABLE2 pIpForwardTable = NULL;
1013

1114
if (!NETIO_SUCCESS(GetIpForwardTable2(AF_UNSPEC, &pIpForwardTable)))

src/detection/localip/localip_linux.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
136136
return "getifaddrs(&ifAddrStruct) failed";
137137

138138
const char* defaultRouteIfName = ffNetifGetDefaultRouteIfName();
139+
const uint32_t preferredSourceAddr = ffNetifGetDefaultRoutePreferredSourceAddr();
139140

140141
for (struct ifaddrs* ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next)
141142
{
@@ -146,8 +147,8 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
146147
continue;
147148
#endif
148149

149-
bool isDefaultRoute = ffStrEquals(defaultRouteIfName, ifa->ifa_name);
150-
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRoute)
150+
bool isDefaultRouteIf = ffStrEquals(defaultRouteIfName, ifa->ifa_name);
151+
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRouteIf)
151152
continue;
152153

153154
if ((ifa->ifa_flags & IFF_LOOPBACK) && !(options->showType & FF_LOCALIP_TYPE_LOOP_BIT))
@@ -164,6 +165,11 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
164165
continue;
165166

166167
struct sockaddr_in* ipv4 = (struct sockaddr_in*) ifa->ifa_addr;
168+
169+
bool isDefaultRoute = isDefaultRouteIf && (preferredSourceAddr == 0 || ipv4->sin_addr.s_addr == preferredSourceAddr);
170+
if ((options->showType & FF_LOCALIP_TYPE_DEFAULT_ROUTE_ONLY_BIT) && !isDefaultRoute)
171+
continue;
172+
167173
char addressBuffer[INET_ADDRSTRLEN + 16];
168174
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
169175

@@ -203,7 +209,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
203209
}
204210
}
205211

206-
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRoute, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
212+
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRouteIf, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
207213
}
208214
#if __FreeBSD__ || __OpenBSD__ || __APPLE__ || __NetBSD__ || __HAIKU__
209215
else if (ifa->ifa_addr->sa_family == AF_LINK)
@@ -215,7 +221,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
215221
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
216222
snprintf(addressBuffer, ARRAY_SIZE(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
217223
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
218-
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false);
224+
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRouteIf, flags, false);
219225
}
220226
#else
221227
else if (ifa->ifa_addr->sa_family == AF_PACKET)
@@ -227,7 +233,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
227233
uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr;
228234
snprintf(addressBuffer, ARRAY_SIZE(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
229235
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
230-
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false);
236+
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRouteIf, flags, false);
231237
}
232238
#endif
233239
}

0 commit comments

Comments
 (0)