From 23486b312c0dd9c147799d5f312c0b9ac015eec3 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Tue, 3 Oct 2023 22:31:23 -0500 Subject: [PATCH] Use Packet Marking for IP Address Selection In certain scenarios, the routing policy database may affect packet routing. When selecting an address in `mtr`, assign a packet mark if `SO_MARK` is defined and a mark has been supplied. --- ui/net.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ui/net.c b/ui/net.c index efeb782..45c33ec 100644 --- a/ui/net.c +++ b/ui/net.c @@ -687,8 +687,8 @@ static void net_find_interface_address_from_name( host by connecting a UDP socket and checking the address the socket is bound to. */ -static void net_find_local_address( - void) +static +void net_find_local_address(struct mtr_ctl * ctl) { int udp_socket; int addr_length; @@ -700,6 +700,15 @@ static void net_find_local_address( error(EXIT_FAILURE, errno, "udp socket creation failed"); } +#ifdef SO_MARK + /* On Linux, the packet mark can affect the selection of the source address */ + if(ctl->mark) { + if(setsockopt(udp_socket, SOL_SOCKET, SO_MARK, &ctl->mark, sizeof(ctl->mark))) { + error(EXIT_FAILURE, errno, "failed to set the packet mark"); + } + } +#endif + /* We need to set the port to a non-zero value for the connect to succeed. @@ -778,7 +787,7 @@ void net_reopen( &sourcesockaddr_struct, ctl->af, ctl->InterfaceName); inet_ntop(sourcesockaddr->sa_family, sourceaddress, localaddr, sizeof(localaddr)); } else { - net_find_local_address(); + net_find_local_address(ctl); } }