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