public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Jon Maloy <jmaloy@redhat.com>
To: passt-dev@passt.top, sbrivio@redhat.com, lvivier@redhat.com,
	dgibson@redhat.com, jmaloy@redhat.com
Subject: [PATCH v2 2/2] udp: support traceroute with udp
Date: Thu, 27 Mar 2025 12:51:55 -0400	[thread overview]
Message-ID: <20250327165155.3354811-3-jmaloy@redhat.com> (raw)
In-Reply-To: <20250327165155.3354811-1-jmaloy@redhat.com>

Now that ICMP pass-through from socket-to-tap and TTL/hop limit based
outgoing flow separation is in place, it is easy to support UDP based
traceroute functionality in direction tap-to-socket.

We fix that in this commit.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
v2: Using ancillary data instead  of setsockopt to transfer outgoing
    TTL.
---
 udp.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/udp.c b/udp.c
index 8a2c593..7361ffa 100644
--- a/udp.c
+++ b/udp.c
@@ -861,6 +861,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 		    uint8_t ttl, const struct pool *p, int idx,
 		    const struct timespec *now)
 {
+	char ancillary[CMSG_SPACE(sizeof(int))];
 	const struct flowside *toside;
 	struct mmsghdr mm[UIO_MAXIOV];
 	union sockaddr_inany to_sa;
@@ -868,6 +869,8 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 	const struct udphdr *uh;
 	struct udp_flow *uflow;
 	int i, s, count = 0;
+	char *control = NULL;
+	int control_len = 0;
 	flow_sidx_t tosidx;
 	in_port_t src, dst;
 	uint8_t topif;
@@ -914,6 +917,22 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 
 	pif_sockaddr(c, &to_sa, &sl, topif, &toside->eaddr, toside->eport);
 
+	if (ttl != DEFAULT_TTL) {
+		struct cmsghdr *cmsg = (void *) ancillary;
+
+		if (af == AF_INET) {
+			cmsg->cmsg_level = IPPROTO_IP;
+			cmsg->cmsg_type = IP_TTL;
+		} else {
+			cmsg->cmsg_level = IPPROTO_IPV6;
+			cmsg->cmsg_type = IPV6_HOPLIMIT;
+		}
+		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+		*((int *) CMSG_DATA(cmsg)) = ttl;
+		control = ancillary;
+		control_len = sizeof(ancillary);
+	}
+
 	for (i = 0; i < (int)p->count - idx; i++) {
 		struct udphdr *uh_send;
 		size_t len;
@@ -936,8 +955,8 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 			mm[i].msg_hdr.msg_iovlen = 0;
 		}
 
-		mm[i].msg_hdr.msg_control = NULL;
-		mm[i].msg_hdr.msg_controllen = 0;
+		mm[i].msg_hdr.msg_control = control;
+		mm[i].msg_hdr.msg_controllen = control_len;
 		mm[i].msg_hdr.msg_flags = 0;
 
 		count++;
-- 
@@ -861,6 +861,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 		    uint8_t ttl, const struct pool *p, int idx,
 		    const struct timespec *now)
 {
+	char ancillary[CMSG_SPACE(sizeof(int))];
 	const struct flowside *toside;
 	struct mmsghdr mm[UIO_MAXIOV];
 	union sockaddr_inany to_sa;
@@ -868,6 +869,8 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 	const struct udphdr *uh;
 	struct udp_flow *uflow;
 	int i, s, count = 0;
+	char *control = NULL;
+	int control_len = 0;
 	flow_sidx_t tosidx;
 	in_port_t src, dst;
 	uint8_t topif;
@@ -914,6 +917,22 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 
 	pif_sockaddr(c, &to_sa, &sl, topif, &toside->eaddr, toside->eport);
 
+	if (ttl != DEFAULT_TTL) {
+		struct cmsghdr *cmsg = (void *) ancillary;
+
+		if (af == AF_INET) {
+			cmsg->cmsg_level = IPPROTO_IP;
+			cmsg->cmsg_type = IP_TTL;
+		} else {
+			cmsg->cmsg_level = IPPROTO_IPV6;
+			cmsg->cmsg_type = IPV6_HOPLIMIT;
+		}
+		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+		*((int *) CMSG_DATA(cmsg)) = ttl;
+		control = ancillary;
+		control_len = sizeof(ancillary);
+	}
+
 	for (i = 0; i < (int)p->count - idx; i++) {
 		struct udphdr *uh_send;
 		size_t len;
@@ -936,8 +955,8 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 			mm[i].msg_hdr.msg_iovlen = 0;
 		}
 
-		mm[i].msg_hdr.msg_control = NULL;
-		mm[i].msg_hdr.msg_controllen = 0;
+		mm[i].msg_hdr.msg_control = control;
+		mm[i].msg_hdr.msg_controllen = control_len;
 		mm[i].msg_hdr.msg_flags = 0;
 
 		count++;
-- 
2.48.1


      parent reply	other threads:[~2025-03-27 16:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-27 16:51 [PATCH v2 0/2] udp: add guest-to-remote traceroute Jon Maloy
2025-03-27 16:51 ` [PATCH v2 1/2] Add ttl to outgoing flow structure lookups Jon Maloy
2025-03-28  1:50   ` David Gibson
2025-03-27 16:51 ` Jon Maloy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250327165155.3354811-3-jmaloy@redhat.com \
    --to=jmaloy@redhat.com \
    --cc=dgibson@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).