]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Use Packet Marking for IP Address Selection
authorflu0r1ne <flu0r1ne@flu0r1ne.net>
Wed, 4 Oct 2023 03:31:23 +0000 (22:31 -0500)
committerflu0r1ne <flu0r1ne@flu0r1ne.net>
Wed, 4 Oct 2023 04:19:01 +0000 (23:19 -0500)
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

index efeb7822e87f492b57e58e3abc37a7139a3b362e..45c33ec858d11b5f0b5726b8ce67bd0adc29bf64 100644 (file)
--- 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);
     }
 
 }