From 47dd026ce3a9b726d1613586b8923db2c1afd645 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Tue, 3 Oct 2023 22:13:00 -0500 Subject: [PATCH 1/3] Use a uint32 for the type of a Linux mark It is typical to store and manipulate Linux packet marks using unsigned values. --- packet/probe.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packet/probe.h b/packet/probe.h index caf1314b..5a5dc3af 100644 --- a/packet/probe.h +++ b/packet/probe.h @@ -69,7 +69,7 @@ struct probe_param_t { int type_of_service; /* The packet "mark" used for mark-based routing on Linux */ - int routing_mark; + uint32_t routing_mark; /* Time to live for the transmitted probe */ int ttl; From 23486b312c0dd9c147799d5f312c0b9ac015eec3 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Tue, 3 Oct 2023 22:31:23 -0500 Subject: [PATCH 2/3] 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 efeb7822..45c33ec8 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); } } From 7d8b704e8d6d7ea40919659a763a64d43c416725 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Tue, 3 Oct 2023 22:45:20 -0500 Subject: [PATCH 3/3] Support Hexadecimal Arguments for Packet Marking Packet marks are often specified in hexadecimal format. Update the `strtonum_or_err` function to parse both hexadecimal and decimal values. --- ui/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/utils.c b/ui/utils.c index edd59055..5e3a701c 100644 --- a/ui/utils.c +++ b/ui/utils.c @@ -80,7 +80,7 @@ int strtonum_or_err( if (str != NULL && *str != '\0') { errno = 0; - num = strtoul(str, &end, 10); + num = strtoul(str, &end, 0); if (errno == 0 && str != end && end != NULL && *end == '\0') { switch (type) { case STRTO_INT: