]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-dhcp-common.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / network / networkd-dhcp-common.c
index 19c888287c5ecdf0bdfe0dc5ad784a5a059d8b8c..6526d3378fae25f65c945049a87fa3bd743b2bc5 100644 (file)
@@ -4,12 +4,13 @@
 #include <linux/if_arp.h>
 
 #include "bus-error.h"
+#include "bus-locator.h"
 #include "dhcp-identifier.h"
 #include "dhcp-internal.h"
 #include "dhcp6-internal.h"
 #include "escape.h"
 #include "hexdecoct.h"
-#include "in-addr-util.h"
+#include "in-addr-prefix-util.h"
 #include "networkd-dhcp-common.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
 #include "socket-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "vrf.h"
+
+static uint32_t link_get_vrf_table(Link *link) {
+        assert(link);
+        assert(link->network);
+
+        return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
+}
+
+uint32_t link_get_dhcp4_route_table(Link *link) {
+        assert(link);
+        assert(link->network);
+
+        /* When the interface is part of an VRF use the VRFs routing table, unless
+         * another table is explicitly specified. */
+
+        if (link->network->dhcp_route_table_set)
+                return link->network->dhcp_route_table;
+        return link_get_vrf_table(link);
+}
+
+uint32_t link_get_ipv6_accept_ra_route_table(Link *link) {
+        assert(link);
+        assert(link->network);
+
+        if (link->network->ipv6_accept_ra_route_table_set)
+                return link->network->ipv6_accept_ra_route_table;
+        return link_get_vrf_table(link);
+}
 
 bool link_dhcp_enabled(Link *link, int family) {
         assert(link);
         assert(IN_SET(family, AF_INET, AF_INET6));
 
+        /* Currently, sd-dhcp-client supports only ethernet and infiniband. */
+        if (family == AF_INET && !IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
+                return false;
+
         if (family == AF_INET6 && !socket_ipv6_is_supported())
                 return false;
 
@@ -55,7 +89,7 @@ void network_adjust_dhcp(Network *network) {
 
         if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) &&
             FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV6)) {
-                log_warning("%s: DHCPv6 client is enabled but IPv6 link local addressing is disabled. "
+                log_warning("%s: DHCPv6 client is enabled but IPv6 link-local addressing is disabled. "
                             "Disabling DHCPv6 client.", network->filename);
                 SET_FLAG(network->dhcp, ADDRESS_FAMILY_IPV6, false);
         }
@@ -97,14 +131,13 @@ const DUID *link_get_duid(Link *link, int family) {
 }
 
 static int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
-        Manager *manager = userdata;
+        Manager *manager = ASSERT_PTR(userdata);
         const sd_bus_error *e;
         const void *a;
         size_t sz;
         int r;
 
         assert(m);
-        assert(manager);
 
         /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
          * even if the method fails. */
@@ -154,12 +187,10 @@ int manager_request_product_uuid(Manager *m) {
 
         m->product_uuid_requested = false;
 
-        r = sd_bus_call_method_async(
+        r = bus_call_method_async(
                         m->bus,
                         NULL,
-                        "org.freedesktop.hostname1",
-                        "/org/freedesktop/hostname1",
-                        "org.freedesktop.hostname1",
+                        bus_hostname,
                         "GetProductUUID",
                         get_product_uuid_handler,
                         m,
@@ -203,6 +234,30 @@ int dhcp_configure_duid(Link *link, const DUID *duid) {
         return 0;
 }
 
+bool address_is_filtered(int family, const union in_addr_union *address, uint8_t prefixlen, Set *allow_list, Set *deny_list) {
+        struct in_addr_prefix *p;
+
+        assert(IN_SET(family, AF_INET, AF_INET6));
+        assert(address);
+
+        if (allow_list) {
+                SET_FOREACH(p, allow_list)
+                        if (p->family == family &&
+                            p->prefixlen <= prefixlen &&
+                            in_addr_prefix_covers(family, &p->address, p->prefixlen, address) > 0)
+                                return false;
+
+                return true;
+        }
+
+        SET_FOREACH(p, deny_list)
+                if (p->family == family &&
+                    in_addr_prefix_intersect(family, &p->address, p->prefixlen, address, prefixlen) > 0)
+                        return true;
+
+        return false;
+}
+
 int config_parse_dhcp(
                 const char* unit,
                 const char *filename,
@@ -248,7 +303,7 @@ int config_parse_dhcp(
         return 0;
 }
 
-int config_parse_dhcp_route_metric(
+int config_parse_dhcp_or_ra_route_metric(
                 const char* unit,
                 const char *filename,
                 unsigned line,
@@ -266,6 +321,7 @@ int config_parse_dhcp_route_metric(
 
         assert(filename);
         assert(lvalue);
+        assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
         assert(rvalue);
         assert(data);
 
@@ -276,17 +332,24 @@ int config_parse_dhcp_route_metric(
                 return 0;
         }
 
-        if (streq_ptr(section, "DHCPv4")) {
+        switch (ltype) {
+        case AF_INET:
                 network->dhcp_route_metric = metric;
                 network->dhcp_route_metric_set = true;
-        } else if (STRPTR_IN_SET(section, "DHCPv6", "IPv6AcceptRA")) {
+                break;
+        case AF_INET6:
                 network->ipv6_accept_ra_route_metric = metric;
                 network->ipv6_accept_ra_route_metric_set = true;
-        } else { /* [DHCP] section */
+                break;
+        case AF_UNSPEC:
+                /* For backward compatibility. */
                 if (!network->dhcp_route_metric_set)
                         network->dhcp_route_metric = metric;
                 if (!network->ipv6_accept_ra_route_metric_set)
                         network->ipv6_accept_ra_route_metric = metric;
+                break;
+        default:
+                assert_not_reached();
         }
 
         return 0;
@@ -309,6 +372,7 @@ int config_parse_dhcp_use_dns(
 
         assert(filename);
         assert(lvalue);
+        assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
         assert(rvalue);
         assert(data);
 
@@ -319,17 +383,24 @@ int config_parse_dhcp_use_dns(
                 return 0;
         }
 
-        if (streq_ptr(section, "DHCPv4")) {
+        switch (ltype) {
+        case AF_INET:
                 network->dhcp_use_dns = r;
                 network->dhcp_use_dns_set = true;
-        } else if (streq_ptr(section, "DHCPv6")) {
+                break;
+        case AF_INET6:
                 network->dhcp6_use_dns = r;
                 network->dhcp6_use_dns_set = true;
-        } else { /* [DHCP] section */
+                break;
+        case AF_UNSPEC:
+                /* For backward compatibility. */
                 if (!network->dhcp_use_dns_set)
                         network->dhcp_use_dns = r;
                 if (!network->dhcp6_use_dns_set)
                         network->dhcp6_use_dns = r;
+                break;
+        default:
+                assert_not_reached();
         }
 
         return 0;
@@ -352,6 +423,7 @@ int config_parse_dhcp_use_domains(
 
         assert(filename);
         assert(lvalue);
+        assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
         assert(rvalue);
         assert(data);
 
@@ -362,17 +434,24 @@ int config_parse_dhcp_use_domains(
                 return 0;
         }
 
-        if (streq_ptr(section, "DHCPv4")) {
+        switch (ltype) {
+        case AF_INET:
                 network->dhcp_use_domains = d;
                 network->dhcp_use_domains_set = true;
-        } else if (streq_ptr(section, "DHCPv6")) {
+                break;
+        case AF_INET6:
                 network->dhcp6_use_domains = d;
                 network->dhcp6_use_domains_set = true;
-        } else { /* [DHCP] section */
+                break;
+        case AF_UNSPEC:
+                /* For backward compatibility. */
                 if (!network->dhcp_use_domains_set)
                         network->dhcp_use_domains = d;
                 if (!network->dhcp6_use_domains_set)
                         network->dhcp6_use_domains = d;
+                break;
+        default:
+                assert_not_reached();
         }
 
         return 0;
@@ -395,6 +474,7 @@ int config_parse_dhcp_use_ntp(
 
         assert(filename);
         assert(lvalue);
+        assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
         assert(rvalue);
         assert(data);
 
@@ -405,23 +485,30 @@ int config_parse_dhcp_use_ntp(
                 return 0;
         }
 
-        if (streq_ptr(section, "DHCPv4")) {
+        switch (ltype) {
+        case AF_INET:
                 network->dhcp_use_ntp = r;
                 network->dhcp_use_ntp_set = true;
-        } else if (streq_ptr(section, "DHCPv6")) {
+                break;
+        case AF_INET6:
                 network->dhcp6_use_ntp = r;
                 network->dhcp6_use_ntp_set = true;
-        } else { /* [DHCP] section */
+                break;
+        case AF_UNSPEC:
+                /* For backward compatibility. */
                 if (!network->dhcp_use_ntp_set)
                         network->dhcp_use_ntp = r;
                 if (!network->dhcp6_use_ntp_set)
                         network->dhcp6_use_ntp = r;
+                break;
+        default:
+                assert_not_reached();
         }
 
         return 0;
 }
 
-int config_parse_section_route_table(
+int config_parse_dhcp_or_ra_route_table(
                 const char *unit,
                 const char *filename,
                 unsigned line,
@@ -433,14 +520,14 @@ int config_parse_section_route_table(
                 void *data,
                 void *userdata) {
 
-        Network *network = userdata;
+        Network *network = ASSERT_PTR(userdata);
         uint32_t rt;
         int r;
 
         assert(filename);
         assert(lvalue);
+        assert(IN_SET(ltype, AF_INET, AF_INET6));
         assert(rvalue);
-        assert(data);
 
         r = safe_atou32(rvalue, &rt);
         if (r < 0) {
@@ -449,12 +536,17 @@ int config_parse_section_route_table(
                 return 0;
         }
 
-        if (STRPTR_IN_SET(section, "DHCP", "DHCPv4")) {
+        switch (ltype) {
+        case AF_INET:
                 network->dhcp_route_table = rt;
                 network->dhcp_route_table_set = true;
-        } else { /* section is IPv6AcceptRA */
+                break;
+        case AF_INET6:
                 network->ipv6_accept_ra_route_table = rt;
                 network->ipv6_accept_ra_route_table_set = true;
+                break;
+        default:
+                assert_not_reached();
         }
 
         return 0;
@@ -472,14 +564,13 @@ int config_parse_iaid(
                 void *data,
                 void *userdata) {
 
-        Network *network = userdata;
+        Network *network = ASSERT_PTR(userdata);
         uint32_t iaid;
         int r;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(network);
         assert(IN_SET(ltype, AF_INET, AF_INET6));
 
         r = safe_atou32(rvalue, &iaid);
@@ -520,10 +611,9 @@ int config_parse_dhcp_user_or_vendor_class(
                 void *data,
                 void *userdata) {
 
-        char ***l = data;
+        char ***l = ASSERT_PTR(data);
         int r;
 
-        assert(l);
         assert(lvalue);
         assert(rvalue);
         assert(IN_SET(ltype, AF_INET, AF_INET6));
@@ -581,11 +671,13 @@ int config_parse_dhcp_send_option(
                 void *data,
                 void *userdata) {
 
-        _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt4 = NULL, *old4 = NULL;
-        _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL, *old6 = NULL;
+        _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt4 = NULL;
+        _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL;
+        _unused_ _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *old4 = NULL;
+        _unused_ _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *old6 = NULL;
         uint32_t uint32_data, enterprise_identifier = 0;
         _cleanup_free_ char *word = NULL, *q = NULL;
-        OrderedHashmap **options = data;
+        OrderedHashmap **options = ASSERT_PTR(data);
         uint16_t u16, uint16_data;
         union in_addr_union addr;
         DHCPOptionDataType type;
@@ -598,7 +690,6 @@ int config_parse_dhcp_send_option(
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(data);
 
         if (isempty(rvalue)) {
                 *options = ordered_hashmap_free(*options);
@@ -677,7 +768,7 @@ int config_parse_dhcp_send_option(
                 return 0;
         }
 
-        switch(type) {
+        switch (type) {
         case DHCP_OPTION_DATA_UINT8:{
                 r = safe_atou8(p, &uint8_data);
                 if (r < 0) {
@@ -909,16 +1000,14 @@ int config_parse_duid_type(
                 void *userdata) {
 
         _cleanup_free_ char *type_string = NULL;
-        const char *p = rvalue;
+        const char *p = ASSERT_PTR(rvalue);
         bool force = ltype;
-        DUID *duid = data;
+        DUID *duid = ASSERT_PTR(data);
         DUIDType type;
         int r;
 
         assert(filename);
         assert(lvalue);
-        assert(rvalue);
-        assert(duid);
 
         if (!force && duid->set)
                 return 0;
@@ -981,11 +1070,9 @@ int config_parse_manager_duid_type(
                 void *data,
                 void *userdata) {
 
-        Manager *manager = userdata;
+        Manager *manager = ASSERT_PTR(userdata);
         int r;
 
-        assert(manager);
-
         /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
 
         r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
@@ -1007,11 +1094,9 @@ int config_parse_network_duid_type(
                 void *data,
                 void *userdata) {
 
-        Network *network = userdata;
+        Network *network = ASSERT_PTR(userdata);
         int r;
 
-        assert(network);
-
         r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
         if (r < 0)
                 return r;
@@ -1035,12 +1120,11 @@ int config_parse_duid_rawdata(
         uint8_t raw_data[MAX_DUID_LEN];
         unsigned count = 0;
         bool force = ltype;
-        DUID *duid = data;
+        DUID *duid = ASSERT_PTR(data);
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(duid);
 
         if (!force && duid->set)
                 return 0;
@@ -1106,11 +1190,9 @@ int config_parse_manager_duid_rawdata(
                 void *data,
                 void *userdata) {
 
-        Manager *manager = userdata;
+        Manager *manager = ASSERT_PTR(userdata);
         int r;
 
-        assert(manager);
-
         /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
 
         r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
@@ -1132,11 +1214,9 @@ int config_parse_network_duid_rawdata(
                 void *data,
                 void *userdata) {
 
-        Network *network = userdata;
+        Network *network = ASSERT_PTR(userdata);
         int r;
 
-        assert(network);
-
         r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
         if (r < 0)
                 return r;
@@ -1144,3 +1224,80 @@ int config_parse_network_duid_rawdata(
         /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
         return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
 }
+
+int config_parse_uplink(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        Network *network = ASSERT_PTR(userdata);
+        bool accept_none = true;
+        int *index, r;
+        char **name;
+
+        assert(filename);
+        assert(section);
+        assert(lvalue);
+        assert(rvalue);
+
+        if (streq(section, "DHCPServer")) {
+                index = &network->dhcp_server_uplink_index;
+                name = &network->dhcp_server_uplink_name;
+        } else if (streq(section, "IPv6SendRA")) {
+                index = &network->router_uplink_index;
+                name = &network->router_uplink_name;
+        } else if (STR_IN_SET(section, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
+                index = &network->dhcp_pd_uplink_index;
+                name = &network->dhcp_pd_uplink_name;
+                accept_none = false;
+        } else
+                assert_not_reached();
+
+        if (isempty(rvalue) || streq(rvalue, ":auto")) {
+                *index = UPLINK_INDEX_AUTO;
+                *name = mfree(*name);
+                return 0;
+        }
+
+        if (accept_none && streq(rvalue, ":none")) {
+                *index = UPLINK_INDEX_NONE;
+                *name = mfree(*name);
+                return 0;
+        }
+
+        if (!accept_none && streq(rvalue, ":self")) {
+                *index = UPLINK_INDEX_SELF;
+                *name = mfree(*name);
+                return 0;
+        }
+
+        r = parse_ifindex(rvalue);
+        if (r > 0) {
+                *index = r;
+                *name = mfree(*name);
+                return 0;
+        }
+
+        if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) {
+                log_syntax(unit, LOG_WARNING, filename, line, 0,
+                           "Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue);
+                return 0;
+        }
+
+        /* The interface name will be resolved later. */
+        r = free_and_strdup_warn(name, rvalue);
+        if (r < 0)
+                return r;
+
+        /* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
+         * an uplink interface will be selected automatically. */
+        *index = UPLINK_INDEX_AUTO;
+        return 0;
+}