]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce link_get_captive_portal()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Jul 2023 01:31:01 +0000 (10:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Jul 2023 05:55:46 +0000 (14:55 +0900)
Then, downgrade log level of the message about mis-match of captive
portals in different protocols.

src/network/networkd-dhcp-common.c
src/network/networkd-dhcp-common.h
src/network/networkd-json.c
src/network/networkd-state-file.c

index ca9a825e7b36cb5615516017535dbe4a2f1dd6c0..c3a2b92f3c4684a993b94657363655a17ecc14de 100644 (file)
@@ -259,6 +259,58 @@ bool address_is_filtered(int family, const union in_addr_union *address, uint8_t
         return false;
 }
 
+int link_get_captive_portal(Link *link, const char **ret) {
+        const char *dhcp4_cp = NULL, *dhcp6_cp = NULL, *ndisc_cp = NULL;
+        int r;
+
+        assert(link);
+
+        if (!link->network) {
+                *ret = NULL;
+                return 0;
+        }
+
+        if (link->network->dhcp_use_captive_portal && link->dhcp_lease) {
+                r = sd_dhcp_lease_get_captive_portal(link->dhcp_lease, &dhcp4_cp);
+                if (r < 0 && r != -ENODATA)
+                        return r;
+        }
+
+        if (link->network->dhcp6_use_captive_portal && link->dhcp6_lease) {
+                r = sd_dhcp6_lease_get_captive_portal(link->dhcp6_lease, &dhcp6_cp);
+                if (r < 0 && r != -ENODATA)
+                        return r;
+        }
+
+        if (link->network->ipv6_accept_ra_use_captive_portal && link->ndisc_captive_portal)
+                ndisc_cp = link->ndisc_captive_portal;
+
+        if (dhcp4_cp) {
+                if (dhcp6_cp && !streq(dhcp4_cp, dhcp6_cp))
+                        log_link_debug(link, "DHCPv6 captive portal (%s) does not match DHCPv4 (%s), ignoring DHCPv6 captive portal.",
+                                       dhcp6_cp, dhcp4_cp);
+
+                if (ndisc_cp && !streq(dhcp4_cp, ndisc_cp))
+                        log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv4 (%s), ignoring IPv6RA captive portal.",
+                                       ndisc_cp, dhcp4_cp);
+
+                *ret = dhcp4_cp;
+                return 1;
+        }
+
+        if (dhcp6_cp) {
+                if (ndisc_cp && !streq(dhcp6_cp, ndisc_cp))
+                        log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv6 (%s), ignoring IPv6RA captive portal.",
+                                       ndisc_cp, dhcp6_cp);
+
+                *ret = dhcp6_cp;
+                return 1;
+        }
+
+        *ret = ndisc_cp;
+        return !!ndisc_cp;
+}
+
 int config_parse_dhcp(
                 const char* unit,
                 const char *filename,
index 6f57b78a7f373b62f4c7f4a10c6609df1fecf6b0..07367d4d24bfdffa241c5f6a47ff64e8ee27e835 100644 (file)
@@ -85,6 +85,8 @@ static inline bool in6_prefix_is_filtered(const struct in6_addr *prefix, uint8_t
         return address_is_filtered(AF_INET6, &(union in_addr_union) { .in6 = *prefix }, prefixlen, allow_list, deny_list);
 }
 
+int link_get_captive_portal(Link *link, const char **ret);
+
 const char* dhcp_use_domains_to_string(DHCPUseDomains p) _const_;
 DHCPUseDomains dhcp_use_domains_from_string(const char *s) _pure_;
 
index 31ccb9679a5e0a4d7684eae67ca43ae4e0697ebd..da26f797fe4a7f02eceaa80a9a7622ba7202babf 100644 (file)
@@ -7,6 +7,7 @@
 #include "ip-protocol-list.h"
 #include "netif-util.h"
 #include "networkd-address.h"
+#include "networkd-dhcp-common.h"
 #include "networkd-json.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
@@ -874,31 +875,15 @@ finalize:
 }
 
 static int captive_portal_build_json(Link *link, JsonVariant **ret) {
+        const char *captive_portal;
         int r;
-        const char *captive_portal = NULL;
 
         assert(link);
         assert(ret);
 
-        if (!link->network) {
-                *ret = NULL;
-                return 0;
-        }
-
-        if (link->network->dhcp_use_captive_portal && link->dhcp_lease) {
-                r = sd_dhcp_lease_get_captive_portal(link->dhcp_lease, &captive_portal);
-                if (r < 0 && r != -ENODATA)
-                        return r;
-        }
-
-        if (link->network->dhcp6_use_captive_portal && link->dhcp6_lease && !captive_portal) {
-                r = sd_dhcp6_lease_get_captive_portal(link->dhcp6_lease, &captive_portal);
-                if (r < 0 && r != -ENODATA)
-                        return r;
-        }
-
-        if (link->network->ipv6_accept_ra_use_captive_portal && !captive_portal)
-                captive_portal = link->ndisc_captive_portal;
+        r = link_get_captive_portal(link, &captive_portal);
+        if (r < 0)
+                return r;
 
         if (!captive_portal) {
                 *ret = NULL;
index c9366e97d802b6795b16170a65025ada4c5a7ff4..85c9d2108204d391a95122a8cdc0b950d64b052e 100644 (file)
@@ -10,6 +10,7 @@
 #include "fileio.h"
 #include "fs-util.h"
 #include "network-internal.h"
+#include "networkd-dhcp-common.h"
 #include "networkd-link.h"
 #include "networkd-manager-bus.h"
 #include "networkd-manager.h"
@@ -465,7 +466,7 @@ static void link_save_domains(Link *link, FILE *f, OrderedSet *static_domains, D
 
 int link_save(Link *link) {
         const char *admin_state, *oper_state, *carrier_state, *address_state, *ipv4_address_state, *ipv6_address_state,
-              *dhcp_captive_portal = NULL, *dhcp6_captive_portal = NULL;
+                *captive_portal;
         _cleanup_(unlink_and_freep) char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         int r;
@@ -607,37 +608,12 @@ int link_save(Link *link) {
 
                 /************************************************************/
 
-                if (link->dhcp_lease && link->network->dhcp_use_captive_portal) {
-                        r = sd_dhcp_lease_get_captive_portal(link->dhcp_lease, &dhcp_captive_portal);
-                        if (r < 0 && r != -ENODATA)
-                                return r;
-                }
-
-                if (link->dhcp6_lease && link->network->dhcp6_use_captive_portal) {
-                        r = sd_dhcp6_lease_get_captive_portal(link->dhcp6_lease, &dhcp6_captive_portal);
-                        if (r < 0 && r != -ENODATA)
-                                return r;
-                }
-
-                if (dhcp6_captive_portal && dhcp_captive_portal && !streq(dhcp_captive_portal, dhcp6_captive_portal))
-                        log_link_warning(link, "DHCPv6 Captive Portal (%s) does not match DHCPv4 (%s). Ignoring DHCPv6 portal.",
-                                dhcp6_captive_portal, dhcp_captive_portal);
-
-                if (link->network->ipv6_accept_ra_use_captive_portal && link->ndisc_captive_portal) {
-                        if (dhcp_captive_portal && !streq(dhcp_captive_portal, link->ndisc_captive_portal))
-                                log_link_warning(link, "IPv6RA captive portal (%s) does not match DHCPv4 (%s). Ignorning IPv6RA portal.",
-                                        link->ndisc_captive_portal, dhcp_captive_portal);
-                        if (dhcp6_captive_portal && !streq(dhcp6_captive_portal, link->ndisc_captive_portal))
-                                log_link_warning(link, "IPv6RA captive portal (%s) does not match DHCPv6 (%s). Ignorning IPv6RA portal.",
-                                        link->ndisc_captive_portal, dhcp6_captive_portal);
-                }
+                r = link_get_captive_portal(link, &captive_portal);
+                if (r < 0)
+                        return r;
 
-                if (dhcp_captive_portal)
-                        fprintf(f, "CAPTIVE_PORTAL=%s\n", dhcp_captive_portal);
-                else if (dhcp6_captive_portal)
-                        fprintf(f, "CAPTIVE_PORTAL=%s\n", dhcp6_captive_portal);
-                else if (link->ndisc_captive_portal)
-                        fprintf(f, "CAPTIVE_PORTAL=%s\n", link->ndisc_captive_portal);
+                if (captive_portal)
+                        fprintf(f, "CAPTIVE_PORTAL=%s\n", captive_portal);
 
                 /************************************************************/