]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-link.c
tree-wide: use TAKE_PTR() and TAKE_FD() macros
[thirdparty/systemd.git] / src / network / networkd-link.c
index 6c4711e2e8d2e17870545efa3ec1954537f090b1..fa72023c16a61847f466b9a628b63aa7f763a6fe 100644 (file)
@@ -21,6 +21,7 @@
 #include <netinet/ether.h>
 #include <linux/if.h>
 #include <unistd.h>
+#include <stdio_ext.h>
 
 #include "alloc-util.h"
 #include "bus-util.h"
@@ -91,6 +92,9 @@ static bool link_ipv4ll_enabled(Link *link) {
         if (!link->network)
                 return false;
 
+        if (streq_ptr(link->kind, "wireguard"))
+                return false;
+
         return link->network->link_local & ADDRESS_FAMILY_IPV4;
 }
 
@@ -106,6 +110,9 @@ static bool link_ipv6ll_enabled(Link *link) {
         if (!link->network)
                 return false;
 
+        if (streq_ptr(link->kind, "wireguard"))
+                return false;
+
         return link->network->link_local & ADDRESS_FAMILY_IPV6;
 }
 
@@ -128,7 +135,7 @@ static bool link_radv_enabled(Link *link) {
         if (!link_ipv6ll_enabled(link))
                 return false;
 
-        return link->network->router_prefix_delegation;
+        return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE;
 }
 
 static bool link_lldp_rx_enabled(Link *link) {
@@ -494,8 +501,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
         if (r < 0)
                 return r;
 
-        *ret = link;
-        link = NULL;
+        *ret = TAKE_PTR(link);
 
         return 0;
 }
@@ -848,6 +854,8 @@ static int link_enter_set_routes(Link *link) {
         assert(link->network);
         assert(link->state == LINK_STATE_SETTING_ADDRESSES);
 
+        (void) link_set_routing_policy_rule(link);
+
         link_set_state(link, LINK_STATE_SETTING_ROUTES);
 
         LIST_FOREACH(routes, rt, link->network->static_routes) {
@@ -861,8 +869,6 @@ static int link_enter_set_routes(Link *link) {
                 link->route_messages++;
         }
 
-        (void) link_set_routing_policy_rule(link);
-
         if (link->route_messages == 0) {
                 link->static_routes_configured = true;
                 link_check_ready(link);
@@ -2957,7 +2963,7 @@ network_file_fail:
                         if (r < 0)
                                 return log_link_error_errno(link, r, "Failed to add route: %m");
 
-                        if (lifetime != USEC_INFINITY) {
+                        if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
                                 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime,
                                                       0, route_expire_handler, route);
                                 if (r < 0)
@@ -2966,8 +2972,7 @@ network_file_fail:
 
                         route->lifetime = lifetime;
                         sd_event_source_unref(route->expire);
-                        route->expire = expire;
-                        expire = NULL;
+                        route->expire = TAKE_PTR(expire);
                 }
         }
 
@@ -3275,6 +3280,17 @@ int link_update(Link *link, sd_netlink_message *m) {
                                                 return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m");
                                         break;
                                 }
+                                case DHCP_CLIENT_ID_DUID_ONLY: {
+                                        const DUID *duid = link_duid(link);
+
+                                        r = sd_dhcp_client_set_duid(link->dhcp_client,
+                                                                    duid->type,
+                                                                    duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                                                    duid->raw_data_len);
+                                        if (r < 0)
+                                                return log_link_warning_errno(link, r, "Could not update DUID in DHCP client: %m");
+                                        break;
+                                }
                                 case DHCP_CLIENT_ID_MAC:
                                         r = sd_dhcp_client_set_client_id(link->dhcp_client,
                                                                          ARPHRD_ETHER,
@@ -3316,6 +3332,12 @@ int link_update(Link *link, sd_netlink_message *m) {
                                 if (r < 0)
                                         return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m");
                         }
+
+                        if (link->ndisc) {
+                                r = sd_ndisc_set_mac(link->ndisc, &link->mac);
+                                if (r < 0)
+                                        return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m");
+                        }
                 }
         }
 
@@ -3360,16 +3382,16 @@ static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) {
         if (hashmap_isempty(h))
                 return;
 
-        fputs_unlocked(prefix, f);
+        fputs(prefix, f);
         HASHMAP_FOREACH(link, h, i) {
                 if (space)
-                        fputc_unlocked(' ', f);
+                        fputc(' ', f);
 
                 fprintf(f, "%i", link->ifindex);
                 space = true;
         }
 
-        fputc_unlocked('\n', f);
+        fputc('\n', f);
 }
 
 int link_save(Link *link) {
@@ -3403,6 +3425,7 @@ int link_save(Link *link) {
         if (r < 0)
                 goto fail;
 
+        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
         (void) fchmod(fileno(f), 0644);
 
         fprintf(f,
@@ -3430,7 +3453,7 @@ int link_save(Link *link) {
 
                 fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
 
-                fputs_unlocked("DNS=", f);
+                fputs("DNS=", f);
                 space = false;
 
                 for (j = 0; j < link->network->n_dns; j++) {
@@ -3444,8 +3467,8 @@ int link_save(Link *link) {
                         }
 
                         if (space)
-                                fputc_unlocked(' ', f);
-                        fputs_unlocked(b, f);
+                                fputc(' ', f);
+                        fputs(b, f);
                         space = true;
                 }
 
@@ -3456,7 +3479,7 @@ int link_save(Link *link) {
                         r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
                         if (r > 0) {
                                 if (space)
-                                        fputc_unlocked(' ', f);
+                                        fputc(' ', f);
                                 serialize_in_addrs(f, addresses, r);
                                 space = true;
                         }
@@ -3468,7 +3491,7 @@ int link_save(Link *link) {
                         r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs);
                         if (r > 0) {
                                 if (space)
-                                        fputc_unlocked(' ', f);
+                                        fputc(' ', f);
                                 serialize_in6_addrs(f, in6_addrs, r);
                                 space = true;
                         }
@@ -3482,16 +3505,16 @@ int link_save(Link *link) {
 
                         SET_FOREACH(dd, link->ndisc_rdnss, i) {
                                 if (space)
-                                        fputc_unlocked(' ', f);
+                                        fputc(' ', f);
 
                                 serialize_in6_addrs(f, &dd->address, 1);
                                 space = true;
                         }
                 }
 
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
 
-                fputs_unlocked("NTP=", f);
+                fputs("NTP=", f);
                 space = false;
                 fputstrv(f, link->network->ntp, NULL, &space);
 
@@ -3502,7 +3525,7 @@ int link_save(Link *link) {
                         r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
                         if (r > 0) {
                                 if (space)
-                                        fputc_unlocked(' ', f);
+                                        fputc(' ', f);
                                 serialize_in_addrs(f, addresses, r);
                                 space = true;
                         }
@@ -3516,7 +3539,7 @@ int link_save(Link *link) {
                                                          &in6_addrs);
                         if (r > 0) {
                                 if (space)
-                                        fputc_unlocked(' ', f);
+                                        fputc(' ', f);
                                 serialize_in6_addrs(f, in6_addrs, r);
                                 space = true;
                         }
@@ -3526,7 +3549,7 @@ int link_save(Link *link) {
                                 fputstrv(f, hosts, NULL, &space);
                 }
 
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
 
                 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
                         if (link->dhcp_lease) {
@@ -3537,7 +3560,7 @@ int link_save(Link *link) {
                                 (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains);
                 }
 
-                fputs_unlocked("DOMAINS=", f);
+                fputs("DOMAINS=", f);
                 space = false;
                 fputstrv(f, link->network->search_domains, NULL, &space);
 
@@ -3555,9 +3578,9 @@ int link_save(Link *link) {
                                 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
                 }
 
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
 
-                fputs_unlocked("ROUTE_DOMAINS=", f);
+                fputs("ROUTE_DOMAINS=", f);
                 space = false;
                 fputstrv(f, link->network->route_domains, NULL, &space);
 
@@ -3575,7 +3598,7 @@ int link_save(Link *link) {
                                 fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space);
                 }
 
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
 
                 fprintf(f, "LLMNR=%s\n",
                         resolve_support_to_string(link->network->llmnr));
@@ -3589,14 +3612,14 @@ int link_save(Link *link) {
                 if (!set_isempty(link->network->dnssec_negative_trust_anchors)) {
                         const char *n;
 
-                        fputs_unlocked("DNSSEC_NTA=", f);
+                        fputs("DNSSEC_NTA=", f);
                         space = false;
                         SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i)
                                 fputs_with_space(f, n, NULL, &space);
-                        fputc_unlocked('\n', f);
+                        fputc('\n', f);
                 }
 
-                fputs_unlocked("ADDRESSES=", f);
+                fputs("ADDRESSES=", f);
                 space = false;
                 SET_FOREACH(a, link->addresses, i) {
                         _cleanup_free_ char *address_str = NULL;
@@ -3608,9 +3631,9 @@ int link_save(Link *link) {
                         fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen);
                         space = true;
                 }
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
 
-                fputs_unlocked("ROUTES=", f);
+                fputs("ROUTES=", f);
                 space = false;
                 SET_FOREACH(route, link->routes, i) {
                         _cleanup_free_ char *route_str = NULL;
@@ -3625,7 +3648,7 @@ int link_save(Link *link) {
                         space = true;
                 }
 
-                fputc_unlocked('\n', f);
+                fputc('\n', f);
         }
 
         print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links);
@@ -3643,9 +3666,9 @@ int link_save(Link *link) {
 
                 r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
                 if (r >= 0) {
-                        fputs_unlocked("DHCP4_ADDRESS=", f);
+                        fputs("DHCP4_ADDRESS=", f);
                         serialize_in_addrs(f, &address, 1);
-                        fputc_unlocked('\n', f);
+                        fputc('\n', f);
                 }
 
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
@@ -3663,9 +3686,9 @@ int link_save(Link *link) {
 
                 r = sd_ipv4ll_get_address(link->ipv4ll, &address);
                 if (r >= 0) {
-                        fputs_unlocked("IPV4LL_ADDRESS=", f);
+                        fputs("IPV4LL_ADDRESS=", f);
                         serialize_in_addrs(f, &address, 1);
-                        fputc_unlocked('\n', f);
+                        fputc('\n', f);
                 }
         }