Blame src/core/devices/nm-device-bridge.c

Packit Service 5ffa24
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit Service 5ffa24
/*
Packit Service 5ffa24
 * Copyright (C) 2011 - 2015 Red Hat, Inc.
Packit Service 5ffa24
 */
Packit Service 5ffa24
Packit Service 5ffa24
#include "nm-default.h"
Packit Service 5ffa24
Packit Service 5ffa24
#include "nm-device-bridge.h"
Packit Service 5ffa24
Packit Service 5ffa24
#include <stdlib.h>
Packit Service 5ffa24
#include <linux/if_ether.h>
Packit Service 5ffa24
Packit Service 5ffa24
#include "NetworkManagerUtils.h"
Packit Service 5ffa24
#include "nm-device-private.h"
Packit Service 5ffa24
#include "platform/nm-platform.h"
Packit Service 5ffa24
#include "nm-device-factory.h"
Packit Service 5ffa24
#include "nm-core-internal.h"
Packit Service 5ffa24
Packit Service 5ffa24
#define _NMLOG_DEVICE_TYPE NMDeviceBridge
Packit Service 5ffa24
#include "nm-device-logging.h"
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
Packit Service 5ffa24
struct _NMDeviceBridge {
Packit Service 5ffa24
    NMDevice      parent;
Packit Service 5ffa24
    GCancellable *bt_cancellable;
Packit Service 5ffa24
    bool          vlan_configured : 1;
Packit Service 5ffa24
    bool          bt_registered : 1;
Packit Service 5ffa24
};
Packit Service 5ffa24
Packit Service 5ffa24
struct _NMDeviceBridgeClass {
Packit Service 5ffa24
    NMDeviceClass parent;
Packit Service 5ffa24
};
Packit Service 5ffa24
Packit Service 5ffa24
G_DEFINE_TYPE(NMDeviceBridge, nm_device_bridge, NM_TYPE_DEVICE)
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
Packit Service 5ffa24
const NMBtVTableNetworkServer *nm_bt_vtable_network_server = NULL;
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
Packit Service 5ffa24
static NMDeviceCapabilities
Packit Service 5ffa24
get_generic_capabilities(NMDevice *dev)
Packit Service 5ffa24
{
Packit Service 5ffa24
    return NM_DEVICE_CAP_CARRIER_DETECT | NM_DEVICE_CAP_IS_SOFTWARE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
check_connection_available(NMDevice *                     device,
Packit Service 5ffa24
                           NMConnection *                 connection,
Packit Service 5ffa24
                           NMDeviceCheckConAvailableFlags flags,
Packit Service 5ffa24
                           const char *                   specific_object,
Packit Service 5ffa24
                           GError **                      error)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *    self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    NMSettingBluetooth *s_bt;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!NM_DEVICE_CLASS(nm_device_bridge_parent_class)
Packit Service 5ffa24
             ->check_connection_available(device, connection, flags, specific_object, error))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    s_bt = _nm_connection_get_setting_bluetooth_for_nap(connection);
Packit Service 5ffa24
    if (s_bt) {
Packit Service 5ffa24
        const char *bdaddr;
Packit Service 5ffa24
Packit Service 5ffa24
        if (!nm_bt_vtable_network_server) {
Packit Service 5ffa24
            nm_utils_error_set_literal(error,
Packit Service 5ffa24
                                       NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
Packit Service 5ffa24
                                       "bluetooth plugin not available to activate NAP profile");
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        bdaddr = nm_setting_bluetooth_get_bdaddr(s_bt);
Packit Service 5ffa24
        if (!nm_bt_vtable_network_server->is_available(
Packit Service 5ffa24
                nm_bt_vtable_network_server,
Packit Service 5ffa24
                bdaddr,
Packit Service 5ffa24
                (self->bt_cancellable || self->bt_registered) ? device : NULL)) {
Packit Service 5ffa24
            if (bdaddr)
Packit Service 5ffa24
                nm_utils_error_set(error,
Packit Service 5ffa24
                                   NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
Packit Service 5ffa24
                                   "no suitable NAP device \"%s\" available",
Packit Service 5ffa24
                                   bdaddr);
Packit Service 5ffa24
            else
Packit Service 5ffa24
                nm_utils_error_set_literal(error,
Packit Service 5ffa24
                                           NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
Packit Service 5ffa24
                                           "no suitable NAP device available");
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
        }
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
check_connection_compatible(NMDevice *device, NMConnection *connection, GError **error)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMSettingBridge *s_bridge;
Packit Service 5ffa24
    const char *     mac_address;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!NM_DEVICE_CLASS(nm_device_bridge_parent_class)
Packit Service 5ffa24
             ->check_connection_compatible(device, connection, error))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    if (nm_connection_is_type(connection, NM_SETTING_BLUETOOTH_SETTING_NAME)
Packit Service 5ffa24
        && _nm_connection_get_setting_bluetooth_for_nap(connection)) {
Packit Service 5ffa24
        s_bridge = nm_connection_get_setting_bridge(connection);
Packit Service 5ffa24
        if (!s_bridge) {
Packit Service 5ffa24
            nm_utils_error_set_literal(error,
Packit Service 5ffa24
                                       NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
Packit Service 5ffa24
                                       "missing bridge setting for bluetooth NAP profile");
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        /* a bluetooth NAP connection is handled by the bridge.
Packit Service 5ffa24
         *
Packit Service 5ffa24
         * Proceed... */
Packit Service 5ffa24
    } else {
Packit Service 5ffa24
        s_bridge =
Packit Service 5ffa24
            _nm_connection_check_main_setting(connection, NM_SETTING_BRIDGE_SETTING_NAME, error);
Packit Service 5ffa24
        if (!s_bridge)
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    mac_address = nm_setting_bridge_get_mac_address(s_bridge);
Packit Service 5ffa24
    if (mac_address && nm_device_is_real(device)) {
Packit Service 5ffa24
        const char *hw_addr;
Packit Service 5ffa24
Packit Service 5ffa24
        hw_addr = nm_device_get_hw_address(device);
Packit Service 5ffa24
        if (!hw_addr || !nm_utils_hwaddr_matches(hw_addr, -1, mac_address, -1)) {
Packit Service 5ffa24
            nm_utils_error_set_literal(error,
Packit Service 5ffa24
                                       NM_UTILS_ERROR_CONNECTION_AVAILABLE_TEMPORARY,
Packit Service 5ffa24
                                       "mac address mismatches");
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
        }
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
complete_connection(NMDevice *           device,
Packit Service 5ffa24
                    NMConnection *       connection,
Packit Service 5ffa24
                    const char *         specific_object,
Packit Service 5ffa24
                    NMConnection *const *existing_connections,
Packit Service 5ffa24
                    GError **            error)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMSettingBridge *s_bridge;
Packit Service 5ffa24
Packit Service 5ffa24
    nm_utils_complete_generic(nm_device_get_platform(device),
Packit Service 5ffa24
                              connection,
Packit Service 5ffa24
                              NM_SETTING_BRIDGE_SETTING_NAME,
Packit Service 5ffa24
                              existing_connections,
Packit Service 5ffa24
                              NULL,
Packit Service 5ffa24
                              _("Bridge connection"),
Packit Service 5ffa24
                              "bridge",
Packit Service 5ffa24
                              NULL,
Packit Service 5ffa24
                              TRUE);
Packit Service 5ffa24
Packit Service 5ffa24
    s_bridge = nm_connection_get_setting_bridge(connection);
Packit Service 5ffa24
    if (!s_bridge) {
Packit Service 5ffa24
        s_bridge = (NMSettingBridge *) nm_setting_bridge_new();
Packit Service 5ffa24
        nm_connection_add_setting(connection, NM_SETTING(s_bridge));
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
to_sysfs_group_address_sys(const char *group_address, NMEtherAddr *out_addr)
Packit Service 5ffa24
{
Packit Service 5ffa24
    if (group_address == NULL) {
Packit Service 5ffa24
        *out_addr = NM_ETHER_ADDR_INIT(NM_BRIDGE_GROUP_ADDRESS_DEF_BIN);
Packit Service 5ffa24
        return;
Packit Service 5ffa24
    }
Packit Service 5ffa24
    if (!nm_utils_hwaddr_aton(group_address, out_addr, ETH_ALEN))
Packit Service 5ffa24
        nm_assert_not_reached();
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
from_sysfs_group_address(const char *value, GValue *out)
Packit Service 5ffa24
{
Packit Service 5ffa24
    if (!nm_utils_hwaddr_matches(value, -1, NM_BRIDGE_GROUP_ADDRESS_DEF_STR, -1))
Packit Service 5ffa24
        g_value_set_string(out, value);
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static const char *
Packit Service 5ffa24
to_sysfs_group_address(GValue *value)
Packit Service 5ffa24
{
Packit Service 5ffa24
    return g_value_get_string(value) ?: NM_BRIDGE_GROUP_ADDRESS_DEF_STR;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static int
Packit Service 5ffa24
to_sysfs_vlan_protocol_sys(const char *value)
Packit Service 5ffa24
{
Packit Service 5ffa24
    if (nm_streq0(value, "802.1ad"))
Packit Service 5ffa24
        return ETH_P_8021AD;
Packit Service 5ffa24
Packit Service 5ffa24
    return ETH_P_8021Q;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
from_sysfs_vlan_protocol(const char *value, GValue *out)
Packit Service 5ffa24
{
Packit Service 5ffa24
    switch (_nm_utils_ascii_str_to_uint64(value, 16, 0, G_MAXUINT, -1)) {
Packit Service 5ffa24
    case ETH_P_8021Q:
Packit Service 5ffa24
        /* default value */
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    case ETH_P_8021AD:
Packit Service 5ffa24
        g_value_set_string(out, "802.1ad");
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static const char *
Packit Service 5ffa24
to_sysfs_vlan_protocol(GValue *value)
Packit Service 5ffa24
{
Packit Service 5ffa24
    const char *str = g_value_get_string(value);
Packit Service 5ffa24
Packit Service 5ffa24
    if (nm_streq0(str, "802.1ad")) {
Packit Service 5ffa24
        G_STATIC_ASSERT_EXPR(ETH_P_8021AD == 0x88A8);
Packit Service 5ffa24
        return "0x88A8";
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    G_STATIC_ASSERT_EXPR(ETH_P_8021Q == 0x8100);
Packit Service 5ffa24
    return "0x8100";
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static int
Packit Service 5ffa24
to_sysfs_multicast_router_sys(const char *value)
Packit Service 5ffa24
{
Packit Service 5ffa24
    if (nm_streq0(value, "disabled"))
Packit Service 5ffa24
        return 0;
Packit Service 5ffa24
    if (nm_streq0(value, "auto"))
Packit Service 5ffa24
        return 1;
Packit Service 5ffa24
    if (nm_streq0(value, "enabled"))
Packit Service 5ffa24
        return 2;
Packit Service 5ffa24
Packit Service 5ffa24
    return 1;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static const char *
Packit Service 5ffa24
to_sysfs_multicast_router(GValue *value)
Packit Service 5ffa24
{
Packit Service 5ffa24
    const char *str = g_value_get_string(value);
Packit Service 5ffa24
Packit Service 5ffa24
    if (nm_streq0(str, "disabled"))
Packit Service 5ffa24
        return "0";
Packit Service 5ffa24
    if (nm_streq0(str, "auto"))
Packit Service 5ffa24
        return "1";
Packit Service 5ffa24
    if (nm_streq0(str, "enabled"))
Packit Service 5ffa24
        return "2";
Packit Service 5ffa24
Packit Service 5ffa24
    return "1";
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
from_sysfs_multicast_router(const char *value, GValue *out)
Packit Service 5ffa24
{
Packit Service 5ffa24
    switch (_nm_utils_ascii_str_to_uint64(value, 10, 0, G_MAXUINT, -1)) {
Packit Service 5ffa24
    case 0:
Packit Service 5ffa24
        g_value_set_string(out, "disabled");
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    case 2:
Packit Service 5ffa24
        g_value_set_string(out, "enabled");
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    case 1:
Packit Service 5ffa24
    default:
Packit Service 5ffa24
        /* default value */
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
#define _DEFAULT_IF_ZERO(val, def_val)    \
Packit Service 5ffa24
    ({                                    \
Packit Service 5ffa24
        typeof(val) _val     = (val);     \
Packit Service 5ffa24
        typeof(val) _def_val = (def_val); \
Packit Service 5ffa24
                                          \
Packit Service 5ffa24
        (_val == 0) ? _def_val : _val;    \
Packit Service 5ffa24
    })
Packit Service 5ffa24
Packit Service 5ffa24
typedef struct {
Packit Service 5ffa24
    const char *name;
Packit Service 5ffa24
    const char *sysname;
Packit Service 5ffa24
    const char *(*to_sysfs)(GValue *value);
Packit Service 5ffa24
    void (*from_sysfs)(const char *value, GValue *out);
Packit Service 5ffa24
    guint64 nm_min;
Packit Service 5ffa24
    guint64 nm_max;
Packit Service 5ffa24
    guint64 nm_default;
Packit Service 5ffa24
    bool    default_if_zero;
Packit Service 5ffa24
    bool    user_hz_compensate;
Packit Service 5ffa24
    bool    only_with_stp;
Packit Service 5ffa24
} Option;
Packit Service 5ffa24
Packit Service 5ffa24
#define OPTION(_name, _sysname, ...) \
Packit Service 5ffa24
    {                                \
Packit Service 5ffa24
        .name = ""_name              \
Packit Service 5ffa24
                "",                  \
Packit Service 5ffa24
        .sysname = ""_sysname        \
Packit Service 5ffa24
                   "",               \
Packit Service 5ffa24
        __VA_ARGS__                  \
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
#define OPTION_TYPE_INT(min, max, def) .nm_min = (min), .nm_max = (max), .nm_default = (def)
Packit Service 5ffa24
Packit Service 5ffa24
#define OPTION_TYPE_BOOL(def) OPTION_TYPE_INT(FALSE, TRUE, def)
Packit Service 5ffa24
Packit Service 5ffa24
#define OPTION_TYPE_TOFROM(to, fro) .to_sysfs = (to), .from_sysfs = (fro)
Packit Service 5ffa24
Packit Service 5ffa24
static const Option master_options[] = {
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_STP, /* this must stay as the first item */
Packit Service 5ffa24
           "stp_state",
Packit Service 5ffa24
           OPTION_TYPE_BOOL(NM_BRIDGE_STP_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_PRIORITY,
Packit Service 5ffa24
           "priority",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_PRIORITY_MIN, NM_BRIDGE_PRIORITY_MAX, NM_BRIDGE_PRIORITY_DEF),
Packit Service 5ffa24
           .default_if_zero = TRUE,
Packit Service 5ffa24
           .only_with_stp   = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_FORWARD_DELAY,
Packit Service 5ffa24
           "forward_delay",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_FORWARD_DELAY_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_FORWARD_DELAY_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_FORWARD_DELAY_DEF),
Packit Service 5ffa24
           .default_if_zero    = TRUE,
Packit Service 5ffa24
           .user_hz_compensate = TRUE,
Packit Service 5ffa24
           .only_with_stp      = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_HELLO_TIME,
Packit Service 5ffa24
           "hello_time",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_HELLO_TIME_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_HELLO_TIME_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_HELLO_TIME_DEF),
Packit Service 5ffa24
           .default_if_zero    = TRUE,
Packit Service 5ffa24
           .user_hz_compensate = TRUE,
Packit Service 5ffa24
           .only_with_stp      = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MAX_AGE,
Packit Service 5ffa24
           "max_age",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MAX_AGE_MIN, NM_BRIDGE_MAX_AGE_MAX, NM_BRIDGE_MAX_AGE_DEF),
Packit Service 5ffa24
           .default_if_zero    = TRUE,
Packit Service 5ffa24
           .user_hz_compensate = TRUE,
Packit Service 5ffa24
           .only_with_stp      = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_AGEING_TIME,
Packit Service 5ffa24
           "ageing_time",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_AGEING_TIME_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_AGEING_TIME_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_AGEING_TIME_DEF),
Packit Service 5ffa24
           .default_if_zero    = TRUE,
Packit Service 5ffa24
           .user_hz_compensate = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_GROUP_FORWARD_MASK, "group_fwd_mask", OPTION_TYPE_INT(0, 0xFFFF, 0), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_HASH_MAX,
Packit Service 5ffa24
           "hash_max",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_HASH_MAX_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_HASH_MAX_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_HASH_MAX_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_LAST_MEMBER_COUNT,
Packit Service 5ffa24
           "multicast_last_member_count",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_LAST_MEMBER_COUNT_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_LAST_MEMBER_COUNT_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_LAST_MEMBER_COUNT_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_LAST_MEMBER_INTERVAL,
Packit Service 5ffa24
           "multicast_last_member_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_LAST_MEMBER_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_LAST_MEMBER_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_LAST_MEMBER_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_MEMBERSHIP_INTERVAL,
Packit Service 5ffa24
           "multicast_membership_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_MEMBERSHIP_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_MEMBERSHIP_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_MEMBERSHIP_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_QUERIER,
Packit Service 5ffa24
           "multicast_querier",
Packit Service 5ffa24
           OPTION_TYPE_BOOL(NM_BRIDGE_MULTICAST_QUERIER_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_QUERIER_INTERVAL,
Packit Service 5ffa24
           "multicast_querier_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_QUERIER_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERIER_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERIER_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_QUERY_INTERVAL,
Packit Service 5ffa24
           "multicast_query_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_QUERY_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERY_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERY_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_QUERY_RESPONSE_INTERVAL,
Packit Service 5ffa24
           "multicast_query_response_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_QUERY_RESPONSE_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERY_RESPONSE_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_QUERY_RESPONSE_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_QUERY_USE_IFADDR,
Packit Service 5ffa24
           "multicast_query_use_ifaddr",
Packit Service 5ffa24
           OPTION_TYPE_BOOL(NM_BRIDGE_MULTICAST_QUERY_USE_IFADDR_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_SNOOPING,
Packit Service 5ffa24
           "multicast_snooping",
Packit Service 5ffa24
           OPTION_TYPE_BOOL(NM_BRIDGE_MULTICAST_SNOOPING_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_ROUTER,
Packit Service 5ffa24
           "multicast_router",
Packit Service 5ffa24
           OPTION_TYPE_TOFROM(to_sysfs_multicast_router, from_sysfs_multicast_router), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_STARTUP_QUERY_COUNT,
Packit Service 5ffa24
           "multicast_startup_query_count",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_STARTUP_QUERY_COUNT_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_STARTUP_QUERY_COUNT_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_STARTUP_QUERY_COUNT_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_MULTICAST_STARTUP_QUERY_INTERVAL,
Packit Service 5ffa24
           "multicast_startup_query_interval",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_MULTICAST_STARTUP_QUERY_INTERVAL_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_STARTUP_QUERY_INTERVAL_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_MULTICAST_STARTUP_QUERY_INTERVAL_DEF), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_GROUP_ADDRESS,
Packit Service 5ffa24
           "group_addr",
Packit Service 5ffa24
           OPTION_TYPE_TOFROM(to_sysfs_group_address, from_sysfs_group_address), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_VLAN_PROTOCOL,
Packit Service 5ffa24
           "vlan_protocol",
Packit Service 5ffa24
           OPTION_TYPE_TOFROM(to_sysfs_vlan_protocol, from_sysfs_vlan_protocol), ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_VLAN_STATS_ENABLED,
Packit Service 5ffa24
           "vlan_stats_enabled",
Packit Service 5ffa24
           OPTION_TYPE_BOOL(NM_BRIDGE_VLAN_STATS_ENABLED_DEF)),
Packit Service 5ffa24
    {
Packit Service 5ffa24
        0,
Packit Service 5ffa24
    }};
Packit Service 5ffa24
Packit Service 5ffa24
static const Option slave_options[] = {
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_PORT_PRIORITY,
Packit Service 5ffa24
           "priority",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_PORT_PRIORITY_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_PORT_PRIORITY_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_PORT_PRIORITY_DEF),
Packit Service 5ffa24
           .default_if_zero = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_PORT_PATH_COST,
Packit Service 5ffa24
           "path_cost",
Packit Service 5ffa24
           OPTION_TYPE_INT(NM_BRIDGE_PORT_PATH_COST_MIN,
Packit Service 5ffa24
                           NM_BRIDGE_PORT_PATH_COST_MAX,
Packit Service 5ffa24
                           NM_BRIDGE_PORT_PATH_COST_DEF),
Packit Service 5ffa24
           .default_if_zero = TRUE, ),
Packit Service 5ffa24
    OPTION(NM_SETTING_BRIDGE_PORT_HAIRPIN_MODE, "hairpin_mode", OPTION_TYPE_BOOL(FALSE), ),
Packit Service 5ffa24
    {0}};
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
commit_option(NMDevice *device, NMSetting *setting, const Option *option, gboolean slave)
Packit Service 5ffa24
{
Packit Service 5ffa24
    int                         ifindex = nm_device_get_ifindex(device);
Packit Service 5ffa24
    nm_auto_unset_gvalue GValue val     = G_VALUE_INIT;
Packit Service 5ffa24
    GParamSpec *                pspec;
Packit Service 5ffa24
    const char *                value;
Packit Service 5ffa24
    char                        value_buf[100];
Packit Service 5ffa24
Packit Service 5ffa24
    if (slave)
Packit Service 5ffa24
        nm_assert(NM_IS_SETTING_BRIDGE_PORT(setting));
Packit Service 5ffa24
    else
Packit Service 5ffa24
        nm_assert(NM_IS_SETTING_BRIDGE(setting));
Packit Service 5ffa24
Packit Service 5ffa24
    pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(setting), option->name);
Packit Service 5ffa24
    nm_assert(pspec);
Packit Service 5ffa24
Packit Service 5ffa24
    g_value_init(&val, G_PARAM_SPEC_VALUE_TYPE(pspec));
Packit Service 5ffa24
    g_object_get_property((GObject *) setting, option->name, &val;;
Packit Service 5ffa24
Packit Service 5ffa24
    if (option->to_sysfs) {
Packit Service 5ffa24
        value = option->to_sysfs(&val;;
Packit Service 5ffa24
        goto out;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    switch (pspec->value_type) {
Packit Service 5ffa24
    case G_TYPE_BOOLEAN:
Packit Service 5ffa24
        value = g_value_get_boolean(&val) ? "1" : "0";
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    case G_TYPE_UINT64:
Packit Service 5ffa24
    case G_TYPE_UINT:
Packit Service 5ffa24
    {
Packit Service 5ffa24
        guint64 uval;
Packit Service 5ffa24
Packit Service 5ffa24
        if (pspec->value_type == G_TYPE_UINT64)
Packit Service 5ffa24
            uval = g_value_get_uint64(&val;;
Packit Service 5ffa24
        else
Packit Service 5ffa24
            uval = (guint) g_value_get_uint(&val;;
Packit Service 5ffa24
Packit Service 5ffa24
        /* zero means "unspecified" for some NM properties but isn't in the
Packit Service 5ffa24
             * allowed kernel range, so reset the property to the default value.
Packit Service 5ffa24
             */
Packit Service 5ffa24
        if (option->default_if_zero && uval == 0) {
Packit Service 5ffa24
            if (pspec->value_type == G_TYPE_UINT64)
Packit Service 5ffa24
                uval = NM_G_PARAM_SPEC_GET_DEFAULT_UINT64(pspec);
Packit Service 5ffa24
            else
Packit Service 5ffa24
                uval = NM_G_PARAM_SPEC_GET_DEFAULT_UINT(pspec);
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        /* Linux kernel bridge interfaces use 'centiseconds' for time-based values.
Packit Service 5ffa24
             * In reality it's not centiseconds, but depends on HZ and USER_HZ, which
Packit Service 5ffa24
             * is almost always works out to be a multiplier of 100, so we can assume
Packit Service 5ffa24
             * centiseconds.  See clock_t_to_jiffies().
Packit Service 5ffa24
             */
Packit Service 5ffa24
        if (option->user_hz_compensate)
Packit Service 5ffa24
            uval *= 100;
Packit Service 5ffa24
Packit Service 5ffa24
        if (pspec->value_type == G_TYPE_UINT64)
Packit Service 5ffa24
            nm_sprintf_buf(value_buf, "%" G_GUINT64_FORMAT, uval);
Packit Service 5ffa24
        else
Packit Service 5ffa24
            nm_sprintf_buf(value_buf, "%u", (guint) uval);
Packit Service 5ffa24
Packit Service 5ffa24
        value = value_buf;
Packit Service 5ffa24
    } break;
Packit Service 5ffa24
    case G_TYPE_STRING:
Packit Service 5ffa24
        value = g_value_get_string(&val;;
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    default:
Packit Service 5ffa24
        nm_assert_not_reached();
Packit Service 5ffa24
        value = NULL;
Packit Service 5ffa24
        break;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
out:
Packit Service 5ffa24
    if (!value)
Packit Service 5ffa24
        return;
Packit Service 5ffa24
Packit Service 5ffa24
    if (slave) {
Packit Service 5ffa24
        nm_platform_sysctl_slave_set_option(nm_device_get_platform(device),
Packit Service 5ffa24
                                            ifindex,
Packit Service 5ffa24
                                            option->sysname,
Packit Service 5ffa24
                                            value);
Packit Service 5ffa24
    } else {
Packit Service 5ffa24
        nm_platform_sysctl_master_set_option(nm_device_get_platform(device),
Packit Service 5ffa24
                                             ifindex,
Packit Service 5ffa24
                                             option->sysname,
Packit Service 5ffa24
                                             value);
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static const NMPlatformBridgeVlan **
Packit Service 5ffa24
setting_vlans_to_platform(GPtrArray *array)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMPlatformBridgeVlan **arr;
Packit Service 5ffa24
    NMPlatformBridgeVlan * p_data;
Packit Service 5ffa24
    guint                  i;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!array || !array->len)
Packit Service 5ffa24
        return NULL;
Packit Service 5ffa24
Packit Service 5ffa24
    G_STATIC_ASSERT_EXPR(_nm_alignof(NMPlatformBridgeVlan *) >= _nm_alignof(NMPlatformBridgeVlan));
Packit Service 5ffa24
    arr    = g_malloc((sizeof(NMPlatformBridgeVlan *) * (array->len + 1))
Packit Service 5ffa24
                   + (sizeof(NMPlatformBridgeVlan) * (array->len)));
Packit Service 5ffa24
    p_data = (NMPlatformBridgeVlan *) &arr[array->len + 1];
Packit Service 5ffa24
Packit Service 5ffa24
    for (i = 0; i < array->len; i++) {
Packit Service 5ffa24
        NMBridgeVlan *vlan = array->pdata[i];
Packit Service 5ffa24
        guint16       vid_start, vid_end;
Packit Service 5ffa24
Packit Service 5ffa24
        nm_bridge_vlan_get_vid_range(vlan, &vid_start, &vid_end);
Packit Service 5ffa24
Packit Service 5ffa24
        p_data[i] = (NMPlatformBridgeVlan){
Packit Service 5ffa24
            .vid_start = vid_start,
Packit Service 5ffa24
            .vid_end   = vid_end,
Packit Service 5ffa24
            .pvid      = nm_bridge_vlan_is_pvid(vlan),
Packit Service 5ffa24
            .untagged  = nm_bridge_vlan_is_untagged(vlan),
Packit Service 5ffa24
        };
Packit Service 5ffa24
        arr[i] = &p_data[i];
Packit Service 5ffa24
    }
Packit Service 5ffa24
    arr[i] = NULL;
Packit Service 5ffa24
    return (const NMPlatformBridgeVlan **) arr;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
commit_slave_options(NMDevice *device, NMSettingBridgePort *setting)
Packit Service 5ffa24
{
Packit Service 5ffa24
    const Option *  option;
Packit Service 5ffa24
    NMSetting *     s;
Packit Service 5ffa24
    gs_unref_object NMSetting *s_clear = NULL;
Packit Service 5ffa24
Packit Service 5ffa24
    if (setting)
Packit Service 5ffa24
        s = NM_SETTING(setting);
Packit Service 5ffa24
    else
Packit Service 5ffa24
        s = s_clear = nm_setting_bridge_port_new();
Packit Service 5ffa24
Packit Service 5ffa24
    for (option = slave_options; option->name; option++)
Packit Service 5ffa24
        commit_option(device, s, option, TRUE);
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
update_connection(NMDevice *device, NMConnection *connection)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge * self     = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    NMSettingBridge *s_bridge = nm_connection_get_setting_bridge(connection);
Packit Service 5ffa24
    int              ifindex  = nm_device_get_ifindex(device);
Packit Service 5ffa24
    const Option *   option;
Packit Service 5ffa24
    gs_free char *   stp = NULL;
Packit Service 5ffa24
    int              stp_value;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!s_bridge) {
Packit Service 5ffa24
        s_bridge = (NMSettingBridge *) nm_setting_bridge_new();
Packit Service 5ffa24
        nm_connection_add_setting(connection, (NMSetting *) s_bridge);
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    option = master_options;
Packit Service 5ffa24
    nm_assert(nm_streq(option->sysname, "stp_state"));
Packit Service 5ffa24
Packit Service 5ffa24
    stp = nm_platform_sysctl_master_get_option(nm_device_get_platform(device),
Packit Service 5ffa24
                                               ifindex,
Packit Service 5ffa24
                                               option->sysname);
Packit Service 5ffa24
    stp_value =
Packit Service 5ffa24
        _nm_utils_ascii_str_to_int64(stp, 10, option->nm_min, option->nm_max, option->nm_default);
Packit Service 5ffa24
    g_object_set(s_bridge, option->name, stp_value, NULL);
Packit Service 5ffa24
    option++;
Packit Service 5ffa24
Packit Service 5ffa24
    for (; option->name; option++) {
Packit Service 5ffa24
        nm_auto_unset_gvalue GValue value = G_VALUE_INIT;
Packit Service 5ffa24
        gs_free char *              str   = NULL;
Packit Service 5ffa24
        GParamSpec *                pspec;
Packit Service 5ffa24
Packit Service 5ffa24
        str   = nm_platform_sysctl_master_get_option(nm_device_get_platform(device),
Packit Service 5ffa24
                                                   ifindex,
Packit Service 5ffa24
                                                   option->sysname);
Packit Service 5ffa24
        pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(s_bridge), option->name);
Packit Service 5ffa24
Packit Service 5ffa24
        if (!stp_value && option->only_with_stp)
Packit Service 5ffa24
            continue;
Packit Service 5ffa24
Packit Service 5ffa24
        if (!str) {
Packit Service 5ffa24
            _LOGW(LOGD_BRIDGE, "failed to read bridge setting '%s'", option->sysname);
Packit Service 5ffa24
            continue;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
Packit Service 5ffa24
Packit Service 5ffa24
        if (option->from_sysfs) {
Packit Service 5ffa24
            option->from_sysfs(str, &value);
Packit Service 5ffa24
            goto out;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        switch (pspec->value_type) {
Packit Service 5ffa24
        case G_TYPE_UINT64:
Packit Service 5ffa24
        case G_TYPE_UINT:
Packit Service 5ffa24
        {
Packit Service 5ffa24
            guint64 uvalue;
Packit Service 5ffa24
Packit Service 5ffa24
            /* See comments in set_sysfs_uint() about centiseconds. */
Packit Service 5ffa24
            if (option->user_hz_compensate) {
Packit Service 5ffa24
                uvalue = _nm_utils_ascii_str_to_int64(str,
Packit Service 5ffa24
                                                      10,
Packit Service 5ffa24
                                                      option->nm_min * 100,
Packit Service 5ffa24
                                                      option->nm_max * 100,
Packit Service 5ffa24
                                                      option->nm_default * 100);
Packit Service 5ffa24
                uvalue /= 100;
Packit Service 5ffa24
            } else {
Packit Service 5ffa24
                uvalue = _nm_utils_ascii_str_to_uint64(str,
Packit Service 5ffa24
                                                       10,
Packit Service 5ffa24
                                                       option->nm_min,
Packit Service 5ffa24
                                                       option->nm_max,
Packit Service 5ffa24
                                                       option->nm_default);
Packit Service 5ffa24
            }
Packit Service 5ffa24
Packit Service 5ffa24
            if (pspec->value_type == G_TYPE_UINT64)
Packit Service 5ffa24
                g_value_set_uint64(&value, uvalue);
Packit Service 5ffa24
            else
Packit Service 5ffa24
                g_value_set_uint(&value, (guint) uvalue);
Packit Service 5ffa24
        } break;
Packit Service 5ffa24
        case G_TYPE_BOOLEAN:
Packit Service 5ffa24
        {
Packit Service 5ffa24
            gboolean bvalue;
Packit Service 5ffa24
Packit Service 5ffa24
            bvalue = _nm_utils_ascii_str_to_int64(str,
Packit Service 5ffa24
                                                  10,
Packit Service 5ffa24
                                                  option->nm_min,
Packit Service 5ffa24
                                                  option->nm_max,
Packit Service 5ffa24
                                                  option->nm_default);
Packit Service 5ffa24
            g_value_set_boolean(&value, bvalue);
Packit Service 5ffa24
        } break;
Packit Service 5ffa24
        case G_TYPE_STRING:
Packit Service 5ffa24
            g_value_set_string(&value, str);
Packit Service 5ffa24
            break;
Packit Service 5ffa24
        default:
Packit Service 5ffa24
            nm_assert_not_reached();
Packit Service 5ffa24
            break;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
out:
Packit Service 5ffa24
        g_object_set_property(G_OBJECT(s_bridge), option->name, &value);
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
master_update_slave_connection(NMDevice *    device,
Packit Service 5ffa24
                               NMDevice *    slave,
Packit Service 5ffa24
                               NMConnection *connection,
Packit Service 5ffa24
                               GError **     error)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *     self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    NMSettingConnection *s_con;
Packit Service 5ffa24
    NMSettingBridgePort *s_port;
Packit Service 5ffa24
    int                  ifindex_slave = nm_device_get_ifindex(slave);
Packit Service 5ffa24
    const char *         iface         = nm_device_get_iface(device);
Packit Service 5ffa24
    const Option *       option;
Packit Service 5ffa24
Packit Service 5ffa24
    g_return_val_if_fail(ifindex_slave > 0, FALSE);
Packit Service 5ffa24
Packit Service 5ffa24
    s_con  = nm_connection_get_setting_connection(connection);
Packit Service 5ffa24
    s_port = nm_connection_get_setting_bridge_port(connection);
Packit Service 5ffa24
    if (!s_port) {
Packit Service 5ffa24
        s_port = (NMSettingBridgePort *) nm_setting_bridge_port_new();
Packit Service 5ffa24
        nm_connection_add_setting(connection, NM_SETTING(s_port));
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    for (option = slave_options; option->name; option++) {
Packit Service 5ffa24
        gs_free char *str = nm_platform_sysctl_slave_get_option(nm_device_get_platform(device),
Packit Service 5ffa24
                                                                ifindex_slave,
Packit Service 5ffa24
                                                                option->sysname);
Packit Service 5ffa24
        uint          value;
Packit Service 5ffa24
Packit Service 5ffa24
        if (str) {
Packit Service 5ffa24
            /* See comments in set_sysfs_uint() about centiseconds. */
Packit Service 5ffa24
            if (option->user_hz_compensate) {
Packit Service 5ffa24
                value = _nm_utils_ascii_str_to_int64(str,
Packit Service 5ffa24
                                                     10,
Packit Service 5ffa24
                                                     option->nm_min * 100,
Packit Service 5ffa24
                                                     option->nm_max * 100,
Packit Service 5ffa24
                                                     option->nm_default * 100);
Packit Service 5ffa24
                value /= 100;
Packit Service 5ffa24
            } else {
Packit Service 5ffa24
                value = _nm_utils_ascii_str_to_int64(str,
Packit Service 5ffa24
                                                     10,
Packit Service 5ffa24
                                                     option->nm_min,
Packit Service 5ffa24
                                                     option->nm_max,
Packit Service 5ffa24
                                                     option->nm_default);
Packit Service 5ffa24
            }
Packit Service 5ffa24
            g_object_set(s_port, option->name, value, NULL);
Packit Service 5ffa24
        } else
Packit Service 5ffa24
            _LOGW(LOGD_BRIDGE, "failed to read bridge port setting '%s'", option->sysname);
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    g_object_set(s_con,
Packit Service 5ffa24
                 NM_SETTING_CONNECTION_MASTER,
Packit Service 5ffa24
                 iface,
Packit Service 5ffa24
                 NM_SETTING_CONNECTION_SLAVE_TYPE,
Packit Service 5ffa24
                 NM_SETTING_BRIDGE_SETTING_NAME,
Packit Service 5ffa24
                 NULL);
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
bridge_set_vlan_options(NMDevice *device, NMSettingBridge *s_bridge)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *  self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    gconstpointer     hwaddr;
Packit Service 5ffa24
    size_t            length;
Packit Service 5ffa24
    gboolean          enabled;
Packit Service 5ffa24
    guint16           pvid;
Packit Service 5ffa24
    NMPlatform *      plat;
Packit Service 5ffa24
    int               ifindex;
Packit Service 5ffa24
    gs_unref_ptrarray GPtrArray *vlans              = NULL;
Packit Service 5ffa24
    gs_free const NMPlatformBridgeVlan **plat_vlans = NULL;
Packit Service 5ffa24
Packit Service 5ffa24
    if (self->vlan_configured)
Packit Service 5ffa24
        return TRUE;
Packit Service 5ffa24
Packit Service 5ffa24
    plat    = nm_device_get_platform(device);
Packit Service 5ffa24
    ifindex = nm_device_get_ifindex(device);
Packit Service 5ffa24
    enabled = nm_setting_bridge_get_vlan_filtering(s_bridge);
Packit Service 5ffa24
Packit Service 5ffa24
    if (!enabled) {
Packit Service 5ffa24
        nm_platform_sysctl_master_set_option(plat, ifindex, "vlan_filtering", "0");
Packit Service 5ffa24
        nm_platform_sysctl_master_set_option(plat, ifindex, "default_pvid", "1");
Packit Service 5ffa24
        nm_platform_link_set_bridge_vlans(plat, ifindex, FALSE, NULL);
Packit Service 5ffa24
        return TRUE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    hwaddr = nm_platform_link_get_address(plat, ifindex, &length);
Packit Service 5ffa24
    g_return_val_if_fail(length == ETH_ALEN, FALSE);
Packit Service 5ffa24
    if (nm_utils_hwaddr_matches(hwaddr, length, &nm_ether_addr_zero, ETH_ALEN)) {
Packit Service 5ffa24
        /* We need a non-zero MAC address to set the default pvid.
Packit Service 5ffa24
         * Retry later. */
Packit Service 5ffa24
        return TRUE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    self->vlan_configured = TRUE;
Packit Service 5ffa24
Packit Service 5ffa24
    /* Filtering must be disabled to change the default PVID */
Packit Service 5ffa24
    if (!nm_platform_sysctl_master_set_option(plat, ifindex, "vlan_filtering", "0"))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    /* Clear the default PVID so that we later can force the re-creation of
Packit Service 5ffa24
     * default PVID VLANs by writing the option again. */
Packit Service 5ffa24
    if (!nm_platform_sysctl_master_set_option(plat, ifindex, "default_pvid", "0"))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    /* Clear all existing VLANs */
Packit Service 5ffa24
    if (!nm_platform_link_set_bridge_vlans(plat, ifindex, FALSE, NULL))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    /* Now set the default PVID. After this point the kernel creates
Packit Service 5ffa24
     * a PVID VLAN on each port, including the bridge itself. */
Packit Service 5ffa24
    pvid = nm_setting_bridge_get_vlan_default_pvid(s_bridge);
Packit Service 5ffa24
    if (pvid) {
Packit Service 5ffa24
        char value[32];
Packit Service 5ffa24
Packit Service 5ffa24
        nm_sprintf_buf(value, "%u", pvid);
Packit Service 5ffa24
        if (!nm_platform_sysctl_master_set_option(plat, ifindex, "default_pvid", value))
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    /* Create VLANs only after setting the default PVID, so that
Packit Service 5ffa24
     * any PVID VLAN overrides the bridge's default PVID. */
Packit Service 5ffa24
    g_object_get(s_bridge, NM_SETTING_BRIDGE_VLANS, &vlans, NULL);
Packit Service 5ffa24
    plat_vlans = setting_vlans_to_platform(vlans);
Packit Service 5ffa24
    if (plat_vlans && !nm_platform_link_set_bridge_vlans(plat, ifindex, FALSE, plat_vlans))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!nm_platform_sysctl_master_set_option(plat, ifindex, "vlan_filtering", "1"))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static NMActStageReturn
Packit Service 5ffa24
act_stage1_prepare(NMDevice *device, NMDeviceStateReason *out_failure_reason)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMConnection *connection;
Packit Service 5ffa24
    NMSetting *   s_bridge;
Packit Service 5ffa24
    const Option *option;
Packit Service 5ffa24
Packit Service 5ffa24
    connection = nm_device_get_applied_connection(device);
Packit Service 5ffa24
    g_return_val_if_fail(connection, NM_ACT_STAGE_RETURN_FAILURE);
Packit Service 5ffa24
Packit Service 5ffa24
    s_bridge = (NMSetting *) nm_connection_get_setting_bridge(connection);
Packit Service 5ffa24
    g_return_val_if_fail(s_bridge, NM_ACT_STAGE_RETURN_FAILURE);
Packit Service 5ffa24
Packit Service 5ffa24
    for (option = master_options; option->name; option++)
Packit Service 5ffa24
        commit_option(device, s_bridge, option, FALSE);
Packit Service 5ffa24
Packit Service 5ffa24
    if (!bridge_set_vlan_options(device, (NMSettingBridge *) s_bridge)) {
Packit Service 5ffa24
        NM_SET_OUT(out_failure_reason, NM_DEVICE_STATE_REASON_CONFIG_FAILED);
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_FAILURE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return NM_ACT_STAGE_RETURN_SUCCESS;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
_bt_register_bridge_cb(GError *error, gpointer user_data)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *self;
Packit Service 5ffa24
Packit Service 5ffa24
    if (nm_utils_error_is_cancelled(error))
Packit Service 5ffa24
        return;
Packit Service 5ffa24
Packit Service 5ffa24
    self = user_data;
Packit Service 5ffa24
Packit Service 5ffa24
    g_clear_object(&self->bt_cancellable);
Packit Service 5ffa24
Packit Service 5ffa24
    if (error) {
Packit Service 5ffa24
        _LOGD(LOGD_DEVICE, "bluetooth NAP server failed to register bridge: %s", error->message);
Packit Service 5ffa24
        nm_device_state_changed(NM_DEVICE(self),
Packit Service 5ffa24
                                NM_DEVICE_STATE_FAILED,
Packit Service 5ffa24
                                NM_DEVICE_STATE_REASON_BT_FAILED);
Packit Service 5ffa24
        return;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    nm_device_activate_schedule_stage2_device_config(NM_DEVICE(self), FALSE);
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
void
Packit Service 5ffa24
_nm_device_bridge_notify_unregister_bt_nap(NMDevice *device, const char *reason)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
Packit Service 5ffa24
    _LOGD(LOGD_DEVICE,
Packit Service 5ffa24
          "bluetooth NAP server unregistered from bridge: %s%s",
Packit Service 5ffa24
          reason,
Packit Service 5ffa24
          self->bt_registered ? "" : " (was no longer registered)");
Packit Service 5ffa24
Packit Service 5ffa24
    nm_clear_g_cancellable(&self->bt_cancellable);
Packit Service 5ffa24
Packit Service 5ffa24
    if (self->bt_registered) {
Packit Service 5ffa24
        self->bt_registered = FALSE;
Packit Service 5ffa24
        nm_device_state_changed(device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_BT_FAILED);
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static NMActStageReturn
Packit Service 5ffa24
act_stage2_config(NMDevice *device, NMDeviceStateReason *out_failure_reason)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *    self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    NMConnection *      connection;
Packit Service 5ffa24
    NMSettingBluetooth *s_bt;
Packit Service 5ffa24
    gs_free_error GError *error = NULL;
Packit Service 5ffa24
Packit Service 5ffa24
    connection = nm_device_get_applied_connection(device);
Packit Service 5ffa24
Packit Service 5ffa24
    s_bt = _nm_connection_get_setting_bluetooth_for_nap(connection);
Packit Service 5ffa24
    if (!s_bt)
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_SUCCESS;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!nm_bt_vtable_network_server) {
Packit Service 5ffa24
        _LOGD(LOGD_DEVICE, "bluetooth NAP server failed because bluetooth plugin not available");
Packit Service 5ffa24
        *out_failure_reason = NM_DEVICE_STATE_REASON_BT_FAILED;
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_FAILURE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    if (self->bt_cancellable)
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_POSTPONE;
Packit Service 5ffa24
Packit Service 5ffa24
    if (self->bt_registered)
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_POSTPONE;
Packit Service 5ffa24
Packit Service 5ffa24
    self->bt_cancellable = g_cancellable_new();
Packit Service 5ffa24
    if (!nm_bt_vtable_network_server->register_bridge(nm_bt_vtable_network_server,
Packit Service 5ffa24
                                                      nm_setting_bluetooth_get_bdaddr(s_bt),
Packit Service 5ffa24
                                                      device,
Packit Service 5ffa24
                                                      self->bt_cancellable,
Packit Service 5ffa24
                                                      _bt_register_bridge_cb,
Packit Service 5ffa24
                                                      device,
Packit Service 5ffa24
                                                      &error)) {
Packit Service 5ffa24
        _LOGD(LOGD_DEVICE, "bluetooth NAP server failed to register bridge: %s", error->message);
Packit Service 5ffa24
        *out_failure_reason = NM_DEVICE_STATE_REASON_BT_FAILED;
Packit Service 5ffa24
        return NM_ACT_STAGE_RETURN_FAILURE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    self->bt_registered = TRUE;
Packit Service 5ffa24
    return NM_ACT_STAGE_RETURN_POSTPONE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
deactivate(NMDevice *device)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
Packit Service 5ffa24
    _LOGD(LOGD_DEVICE,
Packit Service 5ffa24
          "deactivate bridge%s",
Packit Service 5ffa24
          self->bt_registered ? " (registered as NAP bluetooth device)" : "");
Packit Service 5ffa24
Packit Service 5ffa24
    self->vlan_configured = FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    nm_clear_g_cancellable(&self->bt_cancellable);
Packit Service 5ffa24
Packit Service 5ffa24
    if (self->bt_registered) {
Packit Service 5ffa24
        self->bt_registered = FALSE;
Packit Service 5ffa24
        nm_bt_vtable_network_server->unregister_bridge(nm_bt_vtable_network_server, device);
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
enslave_slave(NMDevice *device, NMDevice *slave, NMConnection *connection, gboolean configure)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *     self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    NMConnection *       master_connection;
Packit Service 5ffa24
    NMSettingBridge *    s_bridge;
Packit Service 5ffa24
    NMSettingBridgePort *s_port;
Packit Service 5ffa24
Packit Service 5ffa24
    if (configure) {
Packit Service 5ffa24
        if (!nm_platform_link_enslave(nm_device_get_platform(device),
Packit Service 5ffa24
                                      nm_device_get_ip_ifindex(device),
Packit Service 5ffa24
                                      nm_device_get_ip_ifindex(slave)))
Packit Service 5ffa24
            return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
        master_connection = nm_device_get_applied_connection(device);
Packit Service 5ffa24
        nm_assert(master_connection);
Packit Service 5ffa24
        s_bridge = nm_connection_get_setting_bridge(master_connection);
Packit Service 5ffa24
        nm_assert(s_bridge);
Packit Service 5ffa24
        s_port = nm_connection_get_setting_bridge_port(connection);
Packit Service 5ffa24
Packit Service 5ffa24
        bridge_set_vlan_options(device, s_bridge);
Packit Service 5ffa24
Packit Service 5ffa24
        if (nm_setting_bridge_get_vlan_filtering(s_bridge)) {
Packit Service 5ffa24
            gs_free const NMPlatformBridgeVlan **plat_vlans = NULL;
Packit Service 5ffa24
            gs_unref_ptrarray GPtrArray *vlans              = NULL;
Packit Service 5ffa24
Packit Service 5ffa24
            if (s_port)
Packit Service 5ffa24
                g_object_get(s_port, NM_SETTING_BRIDGE_PORT_VLANS, &vlans, NULL);
Packit Service 5ffa24
Packit Service 5ffa24
            plat_vlans = setting_vlans_to_platform(vlans);
Packit Service 5ffa24
Packit Service 5ffa24
            /* Since the link was just enslaved, there are no existing VLANs
Packit Service 5ffa24
             * (except for the default one) and so there's no need to flush. */
Packit Service 5ffa24
Packit Service 5ffa24
            if (plat_vlans
Packit Service 5ffa24
                && !nm_platform_link_set_bridge_vlans(nm_device_get_platform(slave),
Packit Service 5ffa24
                                                      nm_device_get_ifindex(slave),
Packit Service 5ffa24
                                                      TRUE,
Packit Service 5ffa24
                                                      plat_vlans))
Packit Service 5ffa24
                return FALSE;
Packit Service 5ffa24
        }
Packit Service 5ffa24
Packit Service 5ffa24
        commit_slave_options(slave, s_port);
Packit Service 5ffa24
Packit Service 5ffa24
        _LOGI(LOGD_BRIDGE, "attached bridge port %s", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
    } else {
Packit Service 5ffa24
        _LOGI(LOGD_BRIDGE, "bridge port %s was attached", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
release_slave(NMDevice *device, NMDevice *slave, gboolean configure)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDeviceBridge *self = NM_DEVICE_BRIDGE(device);
Packit Service 5ffa24
    gboolean        success;
Packit Service 5ffa24
    int             ifindex_slave;
Packit Service 5ffa24
    int             ifindex;
Packit Service 5ffa24
Packit Service 5ffa24
    if (configure) {
Packit Service 5ffa24
        ifindex = nm_device_get_ifindex(device);
Packit Service 5ffa24
        if (ifindex <= 0 || !nm_platform_link_get(nm_device_get_platform(device), ifindex))
Packit Service 5ffa24
            configure = FALSE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    ifindex_slave = nm_device_get_ip_ifindex(slave);
Packit Service 5ffa24
Packit Service 5ffa24
    if (ifindex_slave <= 0) {
Packit Service 5ffa24
        _LOGD(LOGD_TEAM, "bond slave %s is already released", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
        return;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    if (configure) {
Packit Service 5ffa24
        success = nm_platform_link_release(nm_device_get_platform(device),
Packit Service 5ffa24
                                           nm_device_get_ip_ifindex(device),
Packit Service 5ffa24
                                           ifindex_slave);
Packit Service 5ffa24
Packit Service 5ffa24
        if (success) {
Packit Service 5ffa24
            _LOGI(LOGD_BRIDGE, "detached bridge port %s", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
        } else {
Packit Service 5ffa24
            _LOGW(LOGD_BRIDGE, "failed to detach bridge port %s", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
        }
Packit Service 5ffa24
    } else {
Packit Service 5ffa24
        _LOGI(LOGD_BRIDGE, "bridge port %s was detached", nm_device_get_ip_iface(slave));
Packit Service 5ffa24
    }
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
create_and_realize(NMDevice *             device,
Packit Service 5ffa24
                   NMConnection *         connection,
Packit Service 5ffa24
                   NMDevice *             parent,
Packit Service 5ffa24
                   const NMPlatformLink **out_plink,
Packit Service 5ffa24
                   GError **              error)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMSettingWired *    s_wired;
Packit Service 5ffa24
    NMSettingBridge *   s_bridge;
Packit Service 5ffa24
    const char *        iface = nm_device_get_iface(device);
Packit Service 5ffa24
    const char *        hwaddr;
Packit Service 5ffa24
    gs_free char *      hwaddr_cloned = NULL;
Packit Service 5ffa24
    guint8              mac_address[NM_UTILS_HWADDR_LEN_MAX];
Packit Service 5ffa24
    NMPlatformLnkBridge props;
Packit Service 5ffa24
    int                 r;
Packit Service 5ffa24
    guint32             mtu = 0;
Packit Service 5ffa24
Packit Service 5ffa24
    nm_assert(iface);
Packit Service 5ffa24
Packit Service 5ffa24
    s_bridge = nm_connection_get_setting_bridge(connection);
Packit Service 5ffa24
    nm_assert(s_bridge);
Packit Service 5ffa24
Packit Service 5ffa24
    s_wired = nm_connection_get_setting_wired(connection);
Packit Service 5ffa24
    if (s_wired)
Packit Service 5ffa24
        mtu = nm_setting_wired_get_mtu(s_wired);
Packit Service 5ffa24
Packit Service 5ffa24
    hwaddr = nm_setting_bridge_get_mac_address(s_bridge);
Packit Service 5ffa24
    if (!hwaddr
Packit Service 5ffa24
        && nm_device_hw_addr_get_cloned(device, connection, FALSE, &hwaddr_cloned, NULL, NULL)) {
Packit Service 5ffa24
        /* FIXME: we set the MAC address when creating the interface, while the
Packit Service 5ffa24
         * NMDevice is still unrealized. As we afterwards realize the device, it
Packit Service 5ffa24
         * forgets the parameters for the cloned MAC address, and in stage 1
Packit Service 5ffa24
         * it might create a different MAC address. That should be fixed by
Packit Service 5ffa24
         * better handling device realization. */
Packit Service 5ffa24
        hwaddr = hwaddr_cloned;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    if (hwaddr) {
Packit Service 5ffa24
        if (!nm_utils_hwaddr_aton(hwaddr, mac_address, ETH_ALEN)) {
Packit Service 5ffa24
            g_set_error(error,
Packit Service 5ffa24
                        NM_DEVICE_ERROR,
Packit Service 5ffa24
                        NM_DEVICE_ERROR_FAILED,
Packit Service 5ffa24
                        "Invalid hardware address '%s'",
Packit Service 5ffa24
                        hwaddr);
Packit Service 5ffa24
            g_return_val_if_reached(FALSE);
Packit Service 5ffa24
        }
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    props = (NMPlatformLnkBridge){
Packit Service 5ffa24
        .forward_delay = _DEFAULT_IF_ZERO(nm_setting_bridge_get_forward_delay(s_bridge) * 100u,
Packit Service 5ffa24
                                          NM_BRIDGE_FORWARD_DELAY_DEF_SYS),
Packit Service 5ffa24
        .hello_time    = _DEFAULT_IF_ZERO(nm_setting_bridge_get_hello_time(s_bridge) * 100u,
Packit Service 5ffa24
                                       NM_BRIDGE_HELLO_TIME_DEF_SYS),
Packit Service 5ffa24
        .max_age       = _DEFAULT_IF_ZERO(nm_setting_bridge_get_max_age(s_bridge) * 100u,
Packit Service 5ffa24
                                    NM_BRIDGE_MAX_AGE_DEF_SYS),
Packit Service 5ffa24
        .ageing_time   = _DEFAULT_IF_ZERO(nm_setting_bridge_get_ageing_time(s_bridge) * 100u,
Packit Service 5ffa24
                                        NM_BRIDGE_AGEING_TIME_DEF_SYS),
Packit Service 5ffa24
        .stp_state     = nm_setting_bridge_get_stp(s_bridge),
Packit Service 5ffa24
        .priority      = nm_setting_bridge_get_priority(s_bridge),
Packit Service 5ffa24
        .vlan_protocol = to_sysfs_vlan_protocol_sys(nm_setting_bridge_get_vlan_protocol(s_bridge)),
Packit Service 5ffa24
        .vlan_stats_enabled = nm_setting_bridge_get_vlan_stats_enabled(s_bridge),
Packit Service 5ffa24
        .group_fwd_mask     = nm_setting_bridge_get_group_forward_mask(s_bridge),
Packit Service 5ffa24
        .mcast_snooping     = nm_setting_bridge_get_multicast_snooping(s_bridge),
Packit Service 5ffa24
        .mcast_router =
Packit Service 5ffa24
            to_sysfs_multicast_router_sys(nm_setting_bridge_get_multicast_router(s_bridge)),
Packit Service 5ffa24
        .mcast_query_use_ifaddr    = nm_setting_bridge_get_multicast_query_use_ifaddr(s_bridge),
Packit Service 5ffa24
        .mcast_querier             = nm_setting_bridge_get_multicast_querier(s_bridge),
Packit Service 5ffa24
        .mcast_hash_max            = nm_setting_bridge_get_multicast_hash_max(s_bridge),
Packit Service 5ffa24
        .mcast_last_member_count   = nm_setting_bridge_get_multicast_last_member_count(s_bridge),
Packit Service 5ffa24
        .mcast_startup_query_count = nm_setting_bridge_get_multicast_startup_query_count(s_bridge),
Packit Service 5ffa24
        .mcast_last_member_interval =
Packit Service 5ffa24
            nm_setting_bridge_get_multicast_last_member_interval(s_bridge),
Packit Service 5ffa24
        .mcast_membership_interval = nm_setting_bridge_get_multicast_membership_interval(s_bridge),
Packit Service 5ffa24
        .mcast_querier_interval    = nm_setting_bridge_get_multicast_querier_interval(s_bridge),
Packit Service 5ffa24
        .mcast_query_interval      = nm_setting_bridge_get_multicast_query_interval(s_bridge),
Packit Service 5ffa24
        .mcast_query_response_interval =
Packit Service 5ffa24
            nm_setting_bridge_get_multicast_query_response_interval(s_bridge),
Packit Service 5ffa24
        .mcast_startup_query_interval =
Packit Service 5ffa24
            nm_setting_bridge_get_multicast_startup_query_interval(s_bridge),
Packit Service 5ffa24
    };
Packit Service 5ffa24
Packit Service 5ffa24
    to_sysfs_group_address_sys(nm_setting_bridge_get_group_address(s_bridge), &props.group_addr);
Packit Service 5ffa24
Packit Service 5ffa24
    /* If mtu != 0, we set the MTU of the new bridge at creation time. However, kernel will still
Packit Service 5ffa24
     * automatically adjust the MTU of the bridge based on the minimum of the slave's MTU.
Packit Service 5ffa24
     * We don't want this automatism as the user asked for a fixed MTU.
Packit Service 5ffa24
     *
Packit Service 5ffa24
     * To workaround this behavior of kernel, we will later toggle the MTU twice. See
Packit Service 5ffa24
     * NMDeviceClass.mtu_force_set. */
Packit Service 5ffa24
    r = nm_platform_link_bridge_add(nm_device_get_platform(device),
Packit Service 5ffa24
                                    iface,
Packit Service 5ffa24
                                    hwaddr ? mac_address : NULL,
Packit Service 5ffa24
                                    hwaddr ? ETH_ALEN : 0,
Packit Service 5ffa24
                                    mtu,
Packit Service 5ffa24
                                    &props,
Packit Service 5ffa24
                                    out_plink);
Packit Service 5ffa24
    if (r < 0) {
Packit Service 5ffa24
        g_set_error(error,
Packit Service 5ffa24
                    NM_DEVICE_ERROR,
Packit Service 5ffa24
                    NM_DEVICE_ERROR_CREATION_FAILED,
Packit Service 5ffa24
                    "Failed to create bridge interface '%s' for '%s': %s",
Packit Service 5ffa24
                    iface,
Packit Service 5ffa24
                    nm_connection_get_id(connection),
Packit Service 5ffa24
                    nm_strerror(r));
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
nm_device_bridge_init(NMDeviceBridge *self)
Packit Service 5ffa24
{
Packit Service 5ffa24
    nm_assert(nm_device_is_master(NM_DEVICE(self)));
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static const NMDBusInterfaceInfoExtended interface_info_device_bridge = {
Packit Service 5ffa24
    .parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT(
Packit Service 5ffa24
        NM_DBUS_INTERFACE_DEVICE_BRIDGE,
Packit Service 5ffa24
        .signals    = NM_DEFINE_GDBUS_SIGNAL_INFOS(&nm_signal_info_property_changed_legacy, ),
Packit Service 5ffa24
        .properties = NM_DEFINE_GDBUS_PROPERTY_INFOS(
Packit Service 5ffa24
            NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L("HwAddress",
Packit Service 5ffa24
                                                             "s",
Packit Service 5ffa24
                                                             NM_DEVICE_HW_ADDRESS),
Packit Service 5ffa24
            NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L("Carrier", "b", NM_DEVICE_CARRIER),
Packit Service 5ffa24
            NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE_L("Slaves",
Packit Service 5ffa24
                                                             "ao",
Packit Service 5ffa24
                                                             NM_DEVICE_SLAVES), ), ),
Packit Service 5ffa24
    .legacy_property_changed = TRUE,
Packit Service 5ffa24
};
Packit Service 5ffa24
Packit Service 5ffa24
static void
Packit Service 5ffa24
nm_device_bridge_class_init(NMDeviceBridgeClass *klass)
Packit Service 5ffa24
{
Packit Service 5ffa24
    NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS(klass);
Packit Service 5ffa24
    NMDeviceClass *    device_class      = NM_DEVICE_CLASS(klass);
Packit Service 5ffa24
Packit Service 5ffa24
    dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS(&interface_info_device_bridge);
Packit Service 5ffa24
Packit Service 5ffa24
    device_class->connection_type_supported = NM_SETTING_BRIDGE_SETTING_NAME;
Packit Service 5ffa24
    device_class->link_types                = NM_DEVICE_DEFINE_LINK_TYPES(NM_LINK_TYPE_BRIDGE);
Packit Service 5ffa24
Packit Service 5ffa24
    device_class->is_master                   = TRUE;
Packit Service 5ffa24
    device_class->mtu_force_set               = TRUE;
Packit Service 5ffa24
    device_class->get_generic_capabilities    = get_generic_capabilities;
Packit Service 5ffa24
    device_class->check_connection_compatible = check_connection_compatible;
Packit Service 5ffa24
    device_class->check_connection_available  = check_connection_available;
Packit Service 5ffa24
    device_class->complete_connection         = complete_connection;
Packit Service 5ffa24
Packit Service 5ffa24
    device_class->update_connection              = update_connection;
Packit Service 5ffa24
    device_class->master_update_slave_connection = master_update_slave_connection;
Packit Service 5ffa24
Packit Service 5ffa24
    device_class->create_and_realize                     = create_and_realize;
Packit Service 5ffa24
    device_class->act_stage1_prepare_set_hwaddr_ethernet = TRUE;
Packit Service 5ffa24
    device_class->act_stage1_prepare                     = act_stage1_prepare;
Packit Service 5ffa24
    device_class->act_stage2_config                      = act_stage2_config;
Packit Service 5ffa24
    device_class->deactivate                             = deactivate;
Packit Service 5ffa24
    device_class->enslave_slave                          = enslave_slave;
Packit Service 5ffa24
    device_class->release_slave                          = release_slave;
Packit Service 5ffa24
    device_class->get_configured_mtu                     = nm_device_get_configured_mtu_for_wired;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
/*****************************************************************************/
Packit Service 5ffa24
Packit Service 5ffa24
#define NM_TYPE_BRIDGE_DEVICE_FACTORY (nm_bridge_device_factory_get_type())
Packit Service 5ffa24
#define NM_BRIDGE_DEVICE_FACTORY(obj) \
Packit Service 5ffa24
    (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_BRIDGE_DEVICE_FACTORY, NMBridgeDeviceFactory))
Packit Service 5ffa24
Packit Service 5ffa24
static NMDevice *
Packit Service 5ffa24
create_device(NMDeviceFactory *     factory,
Packit Service 5ffa24
              const char *          iface,
Packit Service 5ffa24
              const NMPlatformLink *plink,
Packit Service 5ffa24
              NMConnection *        connection,
Packit Service 5ffa24
              gboolean *            out_ignore)
Packit Service 5ffa24
{
Packit Service 5ffa24
    return g_object_new(NM_TYPE_DEVICE_BRIDGE,
Packit Service 5ffa24
                        NM_DEVICE_IFACE,
Packit Service 5ffa24
                        iface,
Packit Service 5ffa24
                        NM_DEVICE_DRIVER,
Packit Service 5ffa24
                        "bridge",
Packit Service 5ffa24
                        NM_DEVICE_TYPE_DESC,
Packit Service 5ffa24
                        "Bridge",
Packit Service 5ffa24
                        NM_DEVICE_DEVICE_TYPE,
Packit Service 5ffa24
                        NM_DEVICE_TYPE_BRIDGE,
Packit Service 5ffa24
                        NM_DEVICE_LINK_TYPE,
Packit Service 5ffa24
                        NM_LINK_TYPE_BRIDGE,
Packit Service 5ffa24
                        NULL);
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
static gboolean
Packit Service 5ffa24
match_connection(NMDeviceFactory *factory, NMConnection *connection)
Packit Service 5ffa24
{
Packit Service 5ffa24
    const char *type = nm_connection_get_connection_type(connection);
Packit Service 5ffa24
Packit Service 5ffa24
    if (nm_streq(type, NM_SETTING_BRIDGE_SETTING_NAME))
Packit Service 5ffa24
        return TRUE;
Packit Service 5ffa24
Packit Service 5ffa24
    nm_assert(nm_streq(type, NM_SETTING_BLUETOOTH_SETTING_NAME));
Packit Service 5ffa24
Packit Service 5ffa24
    if (!_nm_connection_get_setting_bluetooth_for_nap(connection))
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
Packit Service 5ffa24
    if (!g_type_from_name("NMBluezManager")) {
Packit Service 5ffa24
        /* bluetooth NAP connections are handled by bridge factory. However,
Packit Service 5ffa24
         * it needs help from the bluetooth plugin, so if the plugin is not loaded,
Packit Service 5ffa24
         * we claim not to support it. */
Packit Service 5ffa24
        return FALSE;
Packit Service 5ffa24
    }
Packit Service 5ffa24
Packit Service 5ffa24
    return TRUE;
Packit Service 5ffa24
}
Packit Service 5ffa24
Packit Service 5ffa24
NM_DEVICE_FACTORY_DEFINE_INTERNAL(
Packit Service 5ffa24
    BRIDGE,
Packit Service 5ffa24
    Bridge,
Packit Service 5ffa24
    bridge,
Packit Service 5ffa24
    NM_DEVICE_FACTORY_DECLARE_LINK_TYPES(NM_LINK_TYPE_BRIDGE)
Packit Service 5ffa24
        NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES(NM_SETTING_BRIDGE_SETTING_NAME,
Packit Service 5ffa24
                                                NM_SETTING_BLUETOOTH_SETTING_NAME),
Packit Service 5ffa24
    factory_class->create_device    = create_device;
Packit Service 5ffa24
    factory_class->match_connection = match_connection;);