]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-bridge-fdb.c
network: make address_configure() and friends take Request object
[thirdparty/systemd.git] / src / network / networkd-bridge-fdb.c
index 8000d0ffe4c1d4c39df406a6a7ed6ac16ce4358f..a7ba2239c6aa020010eb193fdc958e1f34af07b7 100644 (file)
@@ -32,11 +32,13 @@ BridgeFDB *bridge_fdb_free(BridgeFDB *fdb) {
                 hashmap_remove(fdb->network->bridge_fdb_entries_by_section, fdb->section);
         }
 
-        network_config_section_free(fdb->section);
+        config_section_free(fdb->section);
+
+        free(fdb->outgoing_ifname);
         return mfree(fdb);
 }
 
-DEFINE_NETWORK_SECTION_FUNCTIONS(BridgeFDB, bridge_fdb_free);
+DEFINE_SECTION_CLEANUP_FUNCTIONS(BridgeFDB, bridge_fdb_free);
 
 /* create a new FDB entry or get an existing one. */
 static int bridge_fdb_new_static(
@@ -45,7 +47,7 @@ static int bridge_fdb_new_static(
                 unsigned section_line,
                 BridgeFDB **ret) {
 
-        _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
+        _cleanup_(config_section_freep) ConfigSection *n = NULL;
         _cleanup_(bridge_fdb_freep) BridgeFDB *fdb = NULL;
         int r;
 
@@ -54,7 +56,7 @@ static int bridge_fdb_new_static(
         assert(filename);
         assert(section_line > 0);
 
-        r = network_config_section_new(filename, section_line, &n);
+        r = config_section_new(filename, section_line, &n);
         if (r < 0)
                 return r;
 
@@ -81,7 +83,7 @@ static int bridge_fdb_new_static(
                 .ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF,
         };
 
-        r = hashmap_ensure_put(&network->bridge_fdb_entries_by_section, &network_config_hash_ops, fdb->section, fdb);
+        r = hashmap_ensure_put(&network->bridge_fdb_entries_by_section, &config_section_hash_ops, fdb->section, fdb);
         if (r < 0)
                 return r;
 
@@ -119,60 +121,120 @@ static int bridge_fdb_configure_handler(sd_netlink *rtnl, sd_netlink_message *m,
 }
 
 /* send a request to the kernel to add a FDB entry in its static MAC table. */
-static int bridge_fdb_configure(const BridgeFDB *fdb, Link *link, link_netlink_message_handler_t callback) {
-        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
+static int bridge_fdb_configure_message(const BridgeFDB *fdb, Link *link, sd_netlink_message *req) {
         int r;
 
         assert(fdb);
         assert(link);
-        assert(link->manager);
-        assert(link->manager->rtnl);
-        assert(callback);
-
-        /* create new RTM message */
-        r = sd_rtnl_message_new_neigh(link->manager->rtnl, &req, RTM_NEWNEIGH, link->ifindex, AF_BRIDGE);
-        if (r < 0)
-                return log_link_error_errno(link, r, "Could not create RTM_NEWNEIGH message: %m");
 
         r = sd_rtnl_message_neigh_set_flags(req, fdb->ntf_flags);
         if (r < 0)
-                return log_link_error_errno(link, r, "Could not set neighbor flags: %m");
+                return r;
 
         /* only NUD_PERMANENT state supported. */
         r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
         if (r < 0)
-                return log_link_error_errno(link, r, "Could not set neighbor state: %m");
+                return r;
 
         r = sd_netlink_message_append_data(req, NDA_LLADDR, &fdb->mac_addr, sizeof(fdb->mac_addr));
         if (r < 0)
-                return log_link_error_errno(link, r, "Could not append NDA_LLADDR attribute: %m");
+                return r;
 
         /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
         if (fdb->vlan_id > 0) {
                 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb->vlan_id);
                 if (r < 0)
-                        return log_link_error_errno(link, r, "Could not append NDA_VLAN attribute: %m");
+                        return r;
+        }
+
+        if (fdb->outgoing_ifindex > 0) {
+                r = sd_netlink_message_append_u32(req, NDA_IFINDEX, fdb->outgoing_ifindex);
+                if (r < 0)
+                        return r;
         }
 
         if (in_addr_is_set(fdb->family, &fdb->destination_addr)) {
                 r = netlink_message_append_in_addr_union(req, NDA_DST, fdb->family, &fdb->destination_addr);
                 if (r < 0)
-                        return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
+                        return r;
         }
 
         if (fdb->vni <= VXLAN_VID_MAX) {
                 r = sd_netlink_message_append_u32(req, NDA_VNI, fdb->vni);
                 if (r < 0)
-                        return log_link_error_errno(link, r, "Could not append NDA_VNI attribute: %m");
+                        return r;
         }
 
-        /* send message to the kernel to update its internal static MAC table. */
-        r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
+        return 0;
+}
+
+static int bridge_fdb_configure(BridgeFDB *fdb, Link *link, Request *req) {
+        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
+        int r;
+
+        assert(fdb);
+        assert(link);
+        assert(link->manager);
+        assert(req);
+
+        r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH, link->ifindex, AF_BRIDGE);
+        if (r < 0)
+                return r;
+
+        r = bridge_fdb_configure_message(fdb, link, m);
+        if (r < 0)
+                return r;
+
+        r = netlink_call_async(link->manager->rtnl, NULL, m, req->netlink_handler,
                                link_netlink_destroy_callback, link);
         if (r < 0)
-                return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
+                return r;
 
         link_ref(link);
+        return 0;
+}
+
+static bool bridge_fdb_is_ready_to_configure(BridgeFDB *fdb, Link *link) {
+        Link *out = NULL;
+
+        assert(fdb);
+        assert(link);
+        assert(link->manager);
+
+        if (!link_is_ready_to_configure(link, false))
+                return false;
+
+        if (fdb->outgoing_ifname) {
+                if (link_get_by_name(link->manager, fdb->outgoing_ifname, &out) < 0)
+                        return false;
+
+                fdb->outgoing_ifindex = out->ifindex;
+        } else if (fdb->outgoing_ifindex > 0) {
+                if (link_get_by_index(link->manager, fdb->outgoing_ifindex, &out) < 0)
+                        return false;
+        }
+        if (out && !link_is_ready_to_configure(out, false))
+                return false;
+
+        return true;
+}
+
+int request_process_bridge_fdb(Request *req) {
+        BridgeFDB *fdb;
+        Link *link;
+        int r;
+
+        assert(req);
+        assert(req->type == REQUEST_TYPE_BRIDGE_FDB);
+        assert_se(link = req->link);
+        assert_se(fdb = req->fdb);
+
+        if (!bridge_fdb_is_ready_to_configure(fdb, link))
+                return 0;
+
+        r = bridge_fdb_configure(fdb, link, req);
+        if (r < 0)
+                return log_link_warning_errno(link, r, "Failed to configure bridge FDB: %m");
 
         return 1;
 }
@@ -204,18 +266,6 @@ int link_request_static_bridge_fdb(Link *link) {
         return 0;
 }
 
-int request_process_bridge_fdb(Request *req) {
-        assert(req);
-        assert(req->link);
-        assert(req->fdb);
-        assert(req->type == REQUEST_TYPE_BRIDGE_FDB);
-
-        if (!link_is_ready_to_configure(req->link, false))
-                return 0;
-
-        return bridge_fdb_configure(req->fdb, req->link, req->netlink_handler);
-}
-
 void network_drop_invalid_bridge_fdb_entries(Network *network) {
         BridgeFDB *fdb;
 
@@ -253,7 +303,7 @@ int config_parse_fdb_hwaddr(
         if (r < 0)
                 return log_oom();
 
-        r = ether_addr_from_string(rvalue, &fdb->mac_addr);
+        r = parse_ether_addr(rvalue, &fdb->mac_addr);
         if (r < 0) {
                 log_syntax(unit, LOG_WARNING, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
                 return 0;
@@ -435,3 +485,59 @@ int config_parse_fdb_ntf_flags(
         TAKE_PTR(fdb);
         return 0;
 }
+
+int config_parse_fdb_interface(
+                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) {
+
+        _cleanup_(bridge_fdb_free_or_set_invalidp) BridgeFDB *fdb = NULL;
+        Network *network = userdata;
+        int r;
+
+        assert(filename);
+        assert(section);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = bridge_fdb_new_static(network, filename, section_line, &fdb);
+        if (r < 0)
+                return log_oom();
+
+        if (isempty(rvalue)) {
+                fdb->outgoing_ifname = mfree(fdb->outgoing_ifname);
+                fdb->outgoing_ifindex = 0;
+                TAKE_PTR(fdb);
+                return 0;
+        }
+
+        r = parse_ifindex(rvalue);
+        if (r > 0) {
+                fdb->outgoing_ifname = mfree(fdb->outgoing_ifname);
+                fdb->outgoing_ifindex = r;
+                TAKE_PTR(fdb);
+                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;
+        }
+
+        r = free_and_strdup(&fdb->outgoing_ifname, rvalue);
+        if (r < 0)
+                return log_oom();
+        fdb->outgoing_ifindex = 0;
+
+        TAKE_PTR(fdb);
+        return 0;
+}