aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2025-04-15 17:16:22 +1000
committerStefano Brivio <sbrivio@redhat.com>2025-04-15 19:48:35 +0200
commitf107a86cc05c83c5755861b00b85cdf0eb5c9534 (patch)
tree2f9206d0cad451922e4d4594d433af0fb234a66b
parent04984578b00f7507a05544b7a5490b03ab2d5135 (diff)
downloadpasst-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar.gz
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar.bz2
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar.lz
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar.xz
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.tar.zst
passt-f107a86cc05c83c5755861b00b85cdf0eb5c9534.zip
udp: Add udp_pktinfo() helper
Currently we open code parsing the control message for IP_PKTINFO in udp_peek_addr(). We have an upcoming case where we want to parse PKTINFO in another place, so split this out into a helper function. While we're there, make the parsing a bit more robust: scan all cmsgs to look for the one we want, rather than assuming there's only one. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> [sbrivio: udp_pktinfo(): Fix typo in comment and change err() to debug()] Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--udp.c52
1 files changed, 36 insertions, 16 deletions
diff --git a/udp.c b/udp.c
index 0bec499..97034f6 100644
--- a/udp.c
+++ b/udp.c
@@ -465,6 +465,41 @@ static void udp_send_tap_icmp6(const struct ctx *c,
}
/**
+ * udp_pktinfo() - Retrieve packet destination address from cmsg
+ * @msg: msghdr into which message has been received
+ * @dst: (Local) destination address of message in @mh (output)
+ *
+ * Return: 0 on success, -1 if the information was missing (@dst is set to
+ * inany_any6).
+ */
+static int udp_pktinfo(struct msghdr *msg, union inany_addr *dst)
+{
+ struct cmsghdr *hdr;
+
+ for (hdr = CMSG_FIRSTHDR(msg); hdr; hdr = CMSG_NXTHDR(msg, hdr)) {
+ if (hdr->cmsg_level == IPPROTO_IP &&
+ hdr->cmsg_type == IP_PKTINFO) {
+ const struct in_pktinfo *i4 = (void *)CMSG_DATA(hdr);
+
+ *dst = inany_from_v4(i4->ipi_addr);
+ return 0;
+ }
+
+ if (hdr->cmsg_level == IPPROTO_IPV6 &&
+ hdr->cmsg_type == IPV6_PKTINFO) {
+ const struct in6_pktinfo *i6 = (void *)CMSG_DATA(hdr);
+
+ dst->a6 = i6->ipi6_addr;
+ return 0;
+ }
+ }
+
+ debug("Missing PKTINFO cmsg on datagram");
+ *dst = inany_any6;
+ return -1;
+}
+
+/**
* udp_sock_recverr() - Receive and clear an error from a socket
* @c: Execution context
* @s: Socket to receive errors from
@@ -607,7 +642,6 @@ static int udp_peek_addr(int s, union sockaddr_inany *src,
union inany_addr *dst)
{
char sastr[SOCKADDR_STRLEN], dstr[INANY_ADDRSTRLEN];
- const struct cmsghdr *hdr;
char cmsg[PKTINFO_SPACE];
struct msghdr msg = {
.msg_name = src,
@@ -624,21 +658,7 @@ static int udp_peek_addr(int s, union sockaddr_inany *src,
return -errno;
}
- hdr = CMSG_FIRSTHDR(&msg);
- if (hdr && hdr->cmsg_level == IPPROTO_IP &&
- hdr->cmsg_type == IP_PKTINFO) {
- const struct in_pktinfo *info4 = (void *)CMSG_DATA(hdr);
-
- *dst = inany_from_v4(info4->ipi_addr);
- } else if (hdr && hdr->cmsg_level == IPPROTO_IPV6 &&
- hdr->cmsg_type == IPV6_PKTINFO) {
- const struct in6_pktinfo *info6 = (void *)CMSG_DATA(hdr);
-
- dst->a6 = info6->ipi6_addr;
- } else {
- debug("Unexpected cmsg on UDP datagram");
- *dst = inany_any6;
- }
+ udp_pktinfo(&msg, dst);
trace("Peeked UDP datagram: %s -> %s",
sockaddr_ntop(src, sastr, sizeof(sastr)),