21255d
From 96681723232e9eb0182279086fef291283004806 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Wed, 27 May 2020 19:27:51 +0200
21255d
Subject: [PATCH] socket-util: add generic socket_pass_pktinfo() helper
21255d
21255d
The helper turns on the protocol specific "packet info" structure cmsg
21255d
for three relevant protocols we know.
21255d
21255d
(cherry picked from commit 35a3eb9bdc95d1e6ba25bc65c78959ea104e45a1)
21255d
21255d
Related: #1887181
21255d
---
21255d
 src/basic/socket-util.c | 23 +++++++++++++++++++++++
21255d
 src/basic/socket-util.h |  2 ++
21255d
 2 files changed, 25 insertions(+)
21255d
21255d
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
21255d
index 986bc6e67f..053bcba670 100644
21255d
--- a/src/basic/socket-util.c
21255d
+++ b/src/basic/socket-util.c
21255d
@@ -1246,3 +1246,26 @@ int socket_ioctl_fd(void) {
21255d
 
21255d
         return fd;
21255d
 }
21255d
+
21255d
+int socket_pass_pktinfo(int fd, bool b) {
21255d
+        int af;
21255d
+        socklen_t sl = sizeof(af);
21255d
+
21255d
+        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
21255d
+                return -errno;
21255d
+
21255d
+        switch (af) {
21255d
+
21255d
+        case AF_INET:
21255d
+                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
21255d
+
21255d
+        case AF_INET6:
21255d
+                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
21255d
+
21255d
+        case AF_NETLINK:
21255d
+                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
21255d
+
21255d
+        default:
21255d
+                return -EAFNOSUPPORT;
21255d
+        }
21255d
+}
21255d
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
21255d
index 616f2e0d05..c7c9ad34d6 100644
21255d
--- a/src/basic/socket-util.h
21255d
+++ b/src/basic/socket-util.h
21255d
@@ -190,3 +190,5 @@ static inline int setsockopt_int(int fd, int level, int optname, int value) {
21255d
 
21255d
         return 0;
21255d
 }
21255d
+
21255d
+int socket_pass_pktinfo(int fd, bool b);