87d178
From 905a97ce65352d80af7260d34b74fd8342792c35 Mon Sep 17 00:00:00 2001
87d178
From: Lennart Poettering <lennart@poettering.net>
87d178
Date: Wed, 27 May 2020 19:36:56 +0200
87d178
Subject: [PATCH] core: add new PassPacketInfo= socket unit property
87d178
87d178
(cherry picked from commit a3d19f5d99c44940831a33df8b5bece4aaf749f7)
87d178
87d178
Resolves: #1887181
87d178
---
87d178
 doc/TRANSIENT-SETTINGS.md                   | 1 +
87d178
 man/systemd.socket.xml                      | 9 +++++++++
87d178
 src/core/dbus-socket.c                      | 4 ++++
87d178
 src/core/load-fragment-gperf.gperf.m4       | 1 +
87d178
 src/core/socket.c                           | 8 ++++++++
87d178
 src/core/socket.h                           | 1 +
87d178
 src/shared/bus-unit-util.c                  | 3 +--
87d178
 test/fuzz/fuzz-unit-file/directives.service | 1 +
87d178
 8 files changed, 26 insertions(+), 2 deletions(-)
87d178
87d178
diff --git a/doc/TRANSIENT-SETTINGS.md b/doc/TRANSIENT-SETTINGS.md
87d178
index 995b8797ef..de0ef9cc49 100644
87d178
--- a/doc/TRANSIENT-SETTINGS.md
87d178
+++ b/doc/TRANSIENT-SETTINGS.md
87d178
@@ -410,6 +410,7 @@ Most socket unit settings are available to transient units.
87d178
 ✓ Broadcast=
87d178
 ✓ PassCredentials=
87d178
 ✓ PassSecurity=
87d178
+✓ PassPacketInfo=
87d178
 ✓ TCPCongestion=
87d178
 ✓ ReusePort=
87d178
 ✓ MessageQueueMaxMessages=
87d178
diff --git a/man/systemd.socket.xml b/man/systemd.socket.xml
87d178
index 8676b4e03f..a908d5b6d8 100644
87d178
--- a/man/systemd.socket.xml
87d178
+++ b/man/systemd.socket.xml
87d178
@@ -712,6 +712,15 @@
87d178
         Defaults to <option>false</option>.</para></listitem>
87d178
       </varlistentry>
87d178
 
87d178
+      <varlistentry>
87d178
+        <term><varname>PassPacketInfo=</varname></term>
87d178
+        <listitem><para>Takes a boolean value. This controls the <constant>IP_PKTINFO</constant>,
87d178
+        <constant>IPV6_RECVPKTINFO</constant> and <constant>NETLINK_PKTINFO</constant> socket options, which
87d178
+        enable reception of additional per-packet metadata as ancillary message, on
87d178
+        <constant>AF_INET</constant>, <constant>AF_INET6</constant> and <constant>AF_UNIX</constant> sockets.
87d178
+        Defaults to <option>false</option>.</para></listitem>
87d178
+      </varlistentry>
87d178
+
87d178
       <varlistentry>
87d178
         <term><varname>TCPCongestion=</varname></term>
87d178
         <listitem><para>Takes a string value. Controls the TCP
87d178
diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c
87d178
index fa6bbe2c6f..17494b80c8 100644
87d178
--- a/src/core/dbus-socket.c
87d178
+++ b/src/core/dbus-socket.c
87d178
@@ -104,6 +104,7 @@ const sd_bus_vtable bus_socket_vtable[] = {
87d178
         SD_BUS_PROPERTY("Broadcast", "b", bus_property_get_bool, offsetof(Socket, broadcast), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
         SD_BUS_PROPERTY("PassCredentials", "b", bus_property_get_bool, offsetof(Socket, pass_cred), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
         SD_BUS_PROPERTY("PassSecurity", "b", bus_property_get_bool, offsetof(Socket, pass_sec), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
+        SD_BUS_PROPERTY("PassPacketInfo", "b", bus_property_get_bool, offsetof(Socket, pass_pktinfo), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
         SD_BUS_PROPERTY("RemoveOnStop", "b", bus_property_get_bool, offsetof(Socket, remove_on_stop), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
         SD_BUS_PROPERTY("Listen", "a(ss)", property_get_listen, 0, SD_BUS_VTABLE_PROPERTY_CONST),
87d178
         SD_BUS_PROPERTY("Symlinks", "as", NULL, offsetof(Socket, symlinks), SD_BUS_VTABLE_PROPERTY_CONST),
87d178
@@ -205,6 +206,9 @@ static int bus_socket_set_transient_property(
87d178
         if (streq(name, "PassSecurity"))
87d178
                 return bus_set_transient_bool(u, name, &s->pass_sec, message, flags, error);
87d178
 
87d178
+        if (streq(name, "PassPacketInfo"))
87d178
+                return bus_set_transient_bool(u, name, &s->pass_pktinfo, message, flags, error);
87d178
+
87d178
         if (streq(name, "ReusePort"))
87d178
                 return bus_set_transient_bool(u, name, &s->reuse_port, message, flags, error);
87d178
 
87d178
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
87d178
index 156a4d0a6d..7d683cc84b 100644
87d178
--- a/src/core/load-fragment-gperf.gperf.m4
87d178
+++ b/src/core/load-fragment-gperf.gperf.m4
87d178
@@ -381,6 +381,7 @@ Socket.Transparent,              config_parse_bool,                  0,
87d178
 Socket.Broadcast,                config_parse_bool,                  0,                             offsetof(Socket, broadcast)
87d178
 Socket.PassCredentials,          config_parse_bool,                  0,                             offsetof(Socket, pass_cred)
87d178
 Socket.PassSecurity,             config_parse_bool,                  0,                             offsetof(Socket, pass_sec)
87d178
+Socket.PassPacketInfo,           config_parse_bool,                  0,                             offsetof(Socket, pass_pktinfo)
87d178
 Socket.TCPCongestion,            config_parse_string,                0,                             offsetof(Socket, tcp_congestion)
87d178
 Socket.ReusePort,                config_parse_bool,                  0,                             offsetof(Socket, reuse_port)
87d178
 Socket.MessageQueueMaxMessages,  config_parse_long,                  0,                             offsetof(Socket, mq_maxmsg)
87d178
diff --git a/src/core/socket.c b/src/core/socket.c
87d178
index 97c3a7fc9a..50c32ed8f4 100644
87d178
--- a/src/core/socket.c
87d178
+++ b/src/core/socket.c
87d178
@@ -660,6 +660,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
87d178
                 "%sBroadcast: %s\n"
87d178
                 "%sPassCredentials: %s\n"
87d178
                 "%sPassSecurity: %s\n"
87d178
+                "%sPassPacketInfo: %s\n"
87d178
                 "%sTCPCongestion: %s\n"
87d178
                 "%sRemoveOnStop: %s\n"
87d178
                 "%sWritable: %s\n"
87d178
@@ -678,6 +679,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
87d178
                 prefix, yes_no(s->broadcast),
87d178
                 prefix, yes_no(s->pass_cred),
87d178
                 prefix, yes_no(s->pass_sec),
87d178
+                prefix, yes_no(s->pass_pktinfo),
87d178
                 prefix, strna(s->tcp_congestion),
87d178
                 prefix, yes_no(s->remove_on_stop),
87d178
                 prefix, yes_no(s->writable),
87d178
@@ -1099,6 +1101,12 @@ static void socket_apply_socket_options(Socket *s, int fd) {
87d178
                         log_unit_warning_errno(UNIT(s), errno, "SO_PASSSEC failed: %m");
87d178
         }
87d178
 
87d178
+        if (s->pass_pktinfo) {
87d178
+                r = socket_pass_pktinfo(fd, true);
87d178
+                if (r < 0)
87d178
+                        log_unit_warning_errno(UNIT(s), r, "Failed to enable packet info socket option: %m");
87d178
+        }
87d178
+
87d178
         if (s->priority >= 0)
87d178
                 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &s->priority, sizeof(s->priority)) < 0)
87d178
                         log_unit_warning_errno(UNIT(s), errno, "SO_PRIORITY failed: %m");
87d178
diff --git a/src/core/socket.h b/src/core/socket.h
87d178
index b7a25d91fd..2409dbf2a0 100644
87d178
--- a/src/core/socket.h
87d178
+++ b/src/core/socket.h
87d178
@@ -121,6 +121,7 @@ struct Socket {
87d178
         bool broadcast;
87d178
         bool pass_cred;
87d178
         bool pass_sec;
87d178
+        bool pass_pktinfo;
87d178
 
87d178
         /* Only for INET6 sockets: issue IPV6_V6ONLY sockopt */
87d178
         SocketAddressBindIPv6Only bind_ipv6_only;
87d178
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
87d178
index daa2c2dce5..9010448aaf 100644
87d178
--- a/src/shared/bus-unit-util.c
87d178
+++ b/src/shared/bus-unit-util.c
87d178
@@ -1478,8 +1478,7 @@ static int bus_append_socket_property(sd_bus_message *m, const char *field, cons
87d178
         if (STR_IN_SET(field,
87d178
                        "Accept", "Writable", "KeepAlive", "NoDelay", "FreeBind", "Transparent", "Broadcast",
87d178
                        "PassCredentials", "PassSecurity", "ReusePort", "RemoveOnStop", "SELinuxContextFromNet",
87d178
-                       "FlushPending"))
87d178
-
87d178
+                       "FlushPending", "PassPacketInfo"))
87d178
                 return bus_append_parse_boolean(m, field, eq);
87d178
 
87d178
         if (STR_IN_SET(field, "Priority", "IPTTL", "Mark"))
87d178
diff --git a/test/fuzz/fuzz-unit-file/directives.service b/test/fuzz/fuzz-unit-file/directives.service
87d178
index 9d0530df72..8fde27fc90 100644
87d178
--- a/test/fuzz/fuzz-unit-file/directives.service
87d178
+++ b/test/fuzz/fuzz-unit-file/directives.service
87d178
@@ -161,6 +161,7 @@ PIDFile=
87d178
 PartOf=
87d178
 PassCredentials=
87d178
 PassSecurity=
87d178
+PassPacketInfo=
87d178
 PathChanged=
87d178
 PathExists=
87d178
 PathExistsGlob=