]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/nspawn/nspawn-expose-ports.c
tree-wide: use in_addr_is_set() or friends
[thirdparty/systemd.git] / src / nspawn / nspawn-expose-ports.c
index 0e2da276e70899ccb06af807a055cc8bcdd1e589..9d5051d46d92252a86382bc147769e021cb08abd 100644 (file)
@@ -1,25 +1,8 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2015 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "sd-netlink.h"
 
+#include "af-list.h"
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "firewall-util.h"
@@ -70,7 +53,7 @@ int expose_port_parse(ExposePort **l, const char *s) {
         }
 
         if (r < 0)
-                return -EINVAL;
+                return r;
 
         LIST_FOREACH(ports, p, *l)
                 if (p->protocol == protocol && p->host_port == host_port)
@@ -80,9 +63,11 @@ int expose_port_parse(ExposePort **l, const char *s) {
         if (!p)
                 return -ENOMEM;
 
-        p->protocol = protocol;
-        p->host_port = host_port;
-        p->container_port = container_port;
+        *p = (ExposePort) {
+                .protocol = protocol,
+                .host_port = host_port,
+                .container_port = container_port,
+        };
 
         LIST_PREPEND(ports, *l, p);
 
@@ -98,46 +83,43 @@ void expose_port_free_all(ExposePort *p) {
         }
 }
 
-int expose_port_flush(ExposePort* l, union in_addr_union *exposed) {
+int expose_port_flush(FirewallContext **fw_ctx, ExposePort* l, int af, union in_addr_union *exposed) {
         ExposePort *p;
-        int r, af = AF_INET;
+        int r;
 
         assert(exposed);
 
         if (!l)
                 return 0;
 
-        if (in_addr_is_null(af, exposed))
+        if (!in_addr_is_set(af, exposed))
                 return 0;
 
         log_debug("Lost IP address.");
 
         LIST_FOREACH(ports, p, l) {
-                r = fw_add_local_dnat(false,
+                r = fw_add_local_dnat(fw_ctx,
+                                      false,
                                       af,
                                       p->protocol,
-                                      NULL,
-                                      NULL, 0,
-                                      NULL, 0,
                                       p->host_port,
                                       exposed,
                                       p->container_port,
                                       NULL);
                 if (r < 0)
-                        log_warning_errno(r, "Failed to modify firewall: %m");
+                        log_warning_errno(r, "Failed to modify %s firewall: %m", af_to_name(af));
         }
 
         *exposed = IN_ADDR_NULL;
         return 0;
 }
 
-int expose_port_execute(sd_netlink *rtnl, ExposePort *l, union in_addr_union *exposed) {
+int expose_port_execute(sd_netlink *rtnl, FirewallContext **fw_ctx, ExposePort *l, int af, union in_addr_union *exposed) {
         _cleanup_free_ struct local_address *addresses = NULL;
-        _cleanup_free_ char *pretty = NULL;
         union in_addr_union new_exposed;
         ExposePort *p;
         bool add;
-        int af = AF_INET, r;
+        int r;
 
         assert(exposed);
 
@@ -156,29 +138,30 @@ int expose_port_execute(sd_netlink *rtnl, ExposePort *l, union in_addr_union *ex
                 addresses[0].scope < RT_SCOPE_LINK;
 
         if (!add)
-                return expose_port_flush(l, exposed);
+                return expose_port_flush(fw_ctx, l, af, exposed);
 
         new_exposed = addresses[0].address;
         if (in_addr_equal(af, exposed, &new_exposed))
                 return 0;
 
-        in_addr_to_string(af, &new_exposed, &pretty);
-        log_debug("New container IP is %s.", strna(pretty));
+        if (DEBUG_LOGGING) {
+                _cleanup_free_ char *pretty = NULL;
+                in_addr_to_string(af, &new_exposed, &pretty);
+                log_debug("New container IP is %s.", strna(pretty));
+        }
 
         LIST_FOREACH(ports, p, l) {
 
-                r = fw_add_local_dnat(true,
+                r = fw_add_local_dnat(fw_ctx,
+                                      true,
                                       af,
                                       p->protocol,
-                                      NULL,
-                                      NULL, 0,
-                                      NULL, 0,
                                       p->host_port,
                                       &new_exposed,
                                       p->container_port,
-                                      in_addr_is_null(af, exposed) ? NULL : exposed);
+                                      in_addr_is_set(af, exposed) ? exposed : NULL);
                 if (r < 0)
-                        log_warning_errno(r, "Failed to modify firewall: %m");
+                        log_warning_errno(r, "Failed to modify %s firewall: %m", af_to_name(af));
         }
 
         *exposed = new_exposed;
@@ -208,7 +191,7 @@ int expose_port_watch_rtnl(
                 sd_event *event,
                 int recv_fd,
                 sd_netlink_message_handler_t handler,
-                union in_addr_union *exposed,
+                void *userdata,
                 sd_netlink **ret) {
         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
         int fd, r;
@@ -227,17 +210,17 @@ int expose_port_watch_rtnl(
                 return log_error_errno(r, "Failed to create rtnl object: %m");
         }
 
-        r = sd_netlink_add_match(rtnl, RTM_NEWADDR, handler, exposed);
+        r = sd_netlink_add_match(rtnl, NULL, RTM_NEWADDR, handler, NULL, userdata, "nspawn-NEWADDR");
         if (r < 0)
                 return log_error_errno(r, "Failed to subscribe to RTM_NEWADDR messages: %m");
 
-        r = sd_netlink_add_match(rtnl, RTM_DELADDR, handler, exposed);
+        r = sd_netlink_add_match(rtnl, NULL, RTM_DELADDR, handler, NULL, userdata, "nspawn-DELADDR");
         if (r < 0)
                 return log_error_errno(r, "Failed to subscribe to RTM_DELADDR messages: %m");
 
         r = sd_netlink_attach_event(rtnl, event, 0);
         if (r < 0)
-                return log_error_errno(r, "Failed to add to even loop: %m");
+                return log_error_errno(r, "Failed to add to event loop: %m");
 
         *ret = TAKE_PTR(rtnl);