From: Yu Watanabe Date: Thu, 6 Jul 2023 01:31:01 +0000 (+0900) Subject: network: introduce link_get_captive_portal() X-Git-Tag: v254-rc1~16^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6341ea54675a1aadbc0a61c27bb74f1df17c979e;p=thirdparty%2Fsystemd.git network: introduce link_get_captive_portal() Then, downgrade log level of the message about mis-match of captive portals in different protocols. --- diff --git a/src/network/networkd-dhcp-common.c b/src/network/networkd-dhcp-common.c index ca9a825e7b3..c3a2b92f3c4 100644 --- a/src/network/networkd-dhcp-common.c +++ b/src/network/networkd-dhcp-common.c @@ -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, diff --git a/src/network/networkd-dhcp-common.h b/src/network/networkd-dhcp-common.h index 6f57b78a7f3..07367d4d24b 100644 --- a/src/network/networkd-dhcp-common.h +++ b/src/network/networkd-dhcp-common.h @@ -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_; diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c index 31ccb9679a5..da26f797fe4 100644 --- a/src/network/networkd-json.c +++ b/src/network/networkd-json.c @@ -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; diff --git a/src/network/networkd-state-file.c b/src/network/networkd-state-file.c index c9366e97d80..85c9d210820 100644 --- a/src/network/networkd-state-file.c +++ b/src/network/networkd-state-file.c @@ -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); /************************************************************/