core: convert VLAN interfaces to a device subclass

Many different interface types can support VLANs, including
Infiniband, WiFi, etc.  So we have to create a new device class
for them instead of keeping the support in NMDeviceEthernet.
This commit is contained in:
Dan Williams 2012-02-22 23:59:50 -06:00
parent 5ee7841475
commit 65a13f9d8a
11 changed files with 939 additions and 173 deletions

View file

@ -121,6 +121,7 @@ typedef enum {
NM_DEVICE_TYPE_MODEM = 8,
NM_DEVICE_TYPE_INFINIBAND = 9,
NM_DEVICE_TYPE_BOND = 10,
NM_DEVICE_TYPE_VLAN = 11,
} NMDeviceType;
/**

View file

@ -12,6 +12,7 @@ EXTRA_DIST = \
nm-device-wimax.xml \
nm-device-infiniband.xml \
nm-device-bond.xml \
nm-device-vlan.xml \
nm-device.xml \
nm-ip4-config.xml \
nm-ip6-config.xml \

View file

@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.</
<xi:include href="nm-device-wimax.xml"/>
<xi:include href="nm-device-infiniband.xml"/>
<xi:include href="nm-device-bond.xml"/>
<xi:include href="nm-device-vlan.xml"/>
<xi:include href="nm-wimax-nsp.xml"/>
<xi:include href="nm-ip4-config.xml"/>
<xi:include href="nm-ip6-config.xml"/>

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<node name="/" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0">
<interface name="org.freedesktop.NetworkManager.Device.Vlan">
<property name="HwAddress" type="s" access="read">
<tp:docstring>
Hardware address of the device.
</tp:docstring>
</property>
<property name="Carrier" type="b" access="read">
<tp:docstring>
Indicates whether the physical carrier is found (e.g. whether a cable is plugged in or not).
</tp:docstring>
</property>
<property name="VlanId" type="u" access="read">
<tp:docstring>
The VLAN ID of this VLAN interface.
</tp:docstring>
</property>
<signal name="PropertiesChanged">
<arg name="properties" type="a{sv}" tp:type="String_Variant_Map">
<tp:docstring>
A dictionary mapping property names to variant boxed values
</tp:docstring>
</arg>
</signal>
</interface>
</node>

View file

@ -251,6 +251,11 @@
The device is a bond master interface.
</tp:docstring>
</tp:enumvalue>
<tp:enumvalue suffix="VLAN" value="11">
<tp:docstring>
The device is a VLAN interface.
</tp:docstring>
</tp:enumvalue>
</tp:enum>
<tp:flags name="NM_DEVICE_CAP" value-prefix="NM_DEVICE_CAP" type="u">

View file

@ -26,6 +26,7 @@ src/nm-device-bt.c
src/nm-device-ethernet.c
src/nm-device-infiniband.c
src/nm-device-olpc-mesh.c
src/nm-device-vlan.c
src/nm-manager.c
src/nm-netlink-monitor.c
src/settings/plugins/ifcfg-rh/reader.c

View file

@ -134,6 +134,8 @@ NetworkManager_SOURCES = \
nm-device-infiniband.h \
nm-device-bond.c \
nm-device-bond.h \
nm-device-vlan.c \
nm-device-vlan.h \
nm-wifi-ap.c \
nm-wifi-ap.h \
nm-wifi-ap-utils.c \
@ -228,6 +230,9 @@ nm-device-olpc-mesh-glue.h: $(top_srcdir)/introspection/nm-device-olpc-mesh.xml
nm-device-bond-glue.h: $(top_srcdir)/introspection/nm-device-bond.xml
$(AM_V_GEN) dbus-binding-tool --prefix=nm_device_bond --mode=glib-server --output=$@ $<
nm-device-vlan-glue.h: $(top_srcdir)/introspection/nm-device-vlan.xml
$(AM_V_GEN) dbus-binding-tool --prefix=nm_device_vlan --mode=glib-server --output=$@ $<
nm-ip4-config-glue.h: $(top_srcdir)/introspection/nm-ip4-config.xml
$(AM_V_GEN) dbus-binding-tool --prefix=nm_ip4_config --mode=glib-server --output=$@ $<
@ -253,6 +258,7 @@ BUILT_SOURCES = \
nm-device-ethernet-glue.h \
nm-device-infiniband-glue.h \
nm-device-bond-glue.h \
nm-device-vlan-glue.h \
nm-device-wifi-glue.h \
nm-device-olpc-mesh-glue.h \
nm-device-bt-glue.h \

View file

@ -49,7 +49,6 @@
#include "nm-setting-8021x.h"
#include "nm-setting-pppoe.h"
#include "nm-setting-bond.h"
#include "nm-setting-vlan.h"
#include "ppp-manager/nm-ppp-manager.h"
#include "nm-logging.h"
#include "nm-properties-changed-signal.h"
@ -97,10 +96,6 @@ typedef struct {
/* PPPoE */
NMPPPManager *ppp_manager;
NMIP4Config *pending_ip4_config;
/* VLAN stuff */
int vlan_id;
int vlan_master_ifindex;
} NMDeviceEthernetPrivate;
enum {
@ -250,56 +245,23 @@ constructor (GType type,
object = G_OBJECT_CLASS (nm_device_ethernet_parent_class)->constructor (type,
n_construct_params,
construct_params);
if (!object)
return NULL;
if (object) {
self = NM_DEVICE (object);
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
self = NM_DEVICE (object);
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
// FIXME: Convert this into a no-export property so type can be specified
// when the device is created.
itype = nm_system_get_iface_type (nm_device_get_ifindex (self), nm_device_get_iface (self));
g_assert (itype == NM_IFACE_TYPE_UNSPEC);
// FIXME: Convert this into a no-export property so type can be specified
// when the device is created.
itype = nm_system_get_iface_type (nm_device_get_ifindex (self), nm_device_get_iface (self));
if (itype == NM_IFACE_TYPE_UNSPEC) {
/* normal ethernet, pass */
} else if (itype == NM_IFACE_TYPE_VLAN) {
char *master_iface;
nm_log_dbg (LOGD_HW | LOGD_ETHER, "(%s): kernel ifindex %d",
nm_device_get_iface (NM_DEVICE (self)),
nm_device_get_ifindex (NM_DEVICE (self)));
if (!nm_system_get_iface_vlan_info (nm_device_get_ifindex (self),
&priv->vlan_master_ifindex,
&priv->vlan_id)) {
nm_log_warn (LOGD_DEVICE, "(%s): failed to get VLAN interface info.",
nm_device_get_iface (self));
g_object_unref (object);
return NULL;
}
if (priv->vlan_master_ifindex < 0 || priv->vlan_id < 0) {
nm_log_warn (LOGD_DEVICE, "(%s): VLAN master ifindex (%d) or VLAN ID (%d) invalid.",
nm_device_get_iface (self),
priv->vlan_master_ifindex,
priv->vlan_id);
g_object_unref (object);
return NULL;
}
master_iface = nm_netlink_index_to_iface (priv->vlan_master_ifindex);
nm_log_info (LOGD_DEVICE, "(%s): VLAN ID %d with master %s (ifindex: %d)",
nm_device_get_iface (self),
priv->vlan_id,
master_iface ? master_iface : "(unknown)",
priv->vlan_master_ifindex);
g_free (master_iface);
} else {
g_assert_not_reached ();
/* s390 stuff */
_update_s390_subchannels (NM_DEVICE_ETHERNET (self));
}
nm_log_dbg (LOGD_HW | LOGD_ETHER, "(%s): kernel ifindex %d",
nm_device_get_iface (NM_DEVICE (self)),
nm_device_get_ifindex (NM_DEVICE (self)));
/* s390 stuff */
_update_s390_subchannels (NM_DEVICE_ETHERNET (self));
return object;
}
@ -338,11 +300,6 @@ device_state_changed (NMDevice *device,
static void
nm_device_ethernet_init (NMDeviceEthernet * self)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
priv->vlan_id = -1;
priv->vlan_master_ifindex = -1;
g_signal_connect (self, "state-changed", G_CALLBACK (device_state_changed), NULL);
}
@ -602,74 +559,6 @@ match_ethernet_connection (NMDevice *device, NMConnection *connection,
if (nm_connection_is_type (connection, NM_SETTING_PPPOE_SETTING_NAME)) {
/* NOP */
} else if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) {
NMSettingConnection *s_con = nm_connection_get_setting_connection (connection);
NMSettingVlan *s_vlan = nm_connection_get_setting_vlan (connection);
const char *master, *iface = NULL;
char *tmp_iface = NULL;
int con_master_ifindex = -1;
gboolean iface_matches;
g_assert (s_vlan);
g_assert (s_con);
if (priv->vlan_id < 0) {
g_set_error (error, NM_ETHERNET_ERROR, NM_ETHERNET_ERROR_CONNECTION_INVALID,
"The device was not a VLAN interface.");
return FALSE;
}
if (nm_setting_vlan_get_id (s_vlan) != priv->vlan_id) {
g_set_error (error, NM_ETHERNET_ERROR, NM_ETHERNET_ERROR_CONNECTION_INVALID,
"The connection's VLAN ID did not match the device's VLAN ID.");
return FALSE;
}
/* Check master interface */
master = nm_setting_connection_get_master (s_con);
if (!master) {
g_set_error (error, NM_ETHERNET_ERROR, NM_ETHERNET_ERROR_CONNECTION_INVALID,
"The connection did not specify a VLAN master.");
return FALSE;
}
if (nm_utils_is_uuid (master)) {
/* FIXME: find the master device based on the connection UUID; this
* is a bit hard because NMDevice objects (by design) have no
* knowledge of other NMDevice objects, and we need to look through
* all active NMDevices to see if they are using the given
* connection UUID.
*/
g_warn_if_reached ();
} else {
/* It's an interface name; match it against our master */
con_master_ifindex = nm_netlink_iface_to_index (master);
if (con_master_ifindex < 0 || con_master_ifindex != priv->vlan_master_ifindex) {
g_set_error (error, NM_ETHERNET_ERROR, NM_ETHERNET_ERROR_CONNECTION_INVALID,
"The connection's VLAN master did not match the device's VLAN master interface.");
return FALSE;
}
}
/* Ensure the interface name matches, if it's given */
iface = nm_connection_get_virtual_iface_name (connection);
if (!iface) {
/* If the connection doesn't specify an interface name for the
* VLAN interface, we construct it from the master interface name
* and the VLAN ID.
*/
if (con_master_ifindex >= 0)
iface = tmp_iface = nm_utils_new_vlan_name (master, nm_setting_vlan_get_id (s_vlan));
}
iface_matches = (g_strcmp0 (nm_device_get_ip_iface (device), iface) == 0);
g_free (tmp_iface);
if (!iface_matches) {
g_set_error (error, NM_ETHERNET_ERROR, NM_ETHERNET_ERROR_CONNECTION_INVALID,
"The VLAN connection virtual interface name did not match.");
return FALSE;
}
} else if (nm_connection_is_type (connection, NM_SETTING_WIRED_SETTING_NAME)) {
if (!s_wired) {
g_set_error (error,

766
src/nm-device-vlan.c Normal file
View file

@ -0,0 +1,766 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2011 - 2012 Red Hat, Inc.
*/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <netinet/ether.h>
#include "nm-device-vlan.h"
#include "nm-logging.h"
#include "nm-properties-changed-signal.h"
#include "nm-utils.h"
#include "NetworkManagerUtils.h"
#include "nm-device-private.h"
#include "nm-netlink-monitor.h"
#include "nm-enum-types.h"
#include "nm-system.h"
#include "nm-device-vlan-glue.h"
G_DEFINE_TYPE (NMDeviceVlan, nm_device_vlan, NM_TYPE_DEVICE)
#define NM_DEVICE_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VLAN, NMDeviceVlanPrivate))
#define NM_VLAN_ERROR (nm_vlan_error_quark ())
typedef struct {
gboolean disposed;
NMDevice *parent;
guint parent_state_id;
guint vlan_id;
guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX];
guint hw_addr_len;
gboolean carrier;
NMNetlinkMonitor *monitor;
gulong link_connected_id;
gulong link_disconnected_id;
guint carrier_action_defer_id;
} NMDeviceVlanPrivate;
enum {
PROPERTIES_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_HW_ADDRESS,
PROP_CARRIER,
PROP_VLAN_ID,
LAST_PROP
};
/******************************************************************/
static GQuark
nm_vlan_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("nm-vlan-error");
return quark;
}
/******************************************************************/
static guint32
real_get_generic_capabilities (NMDevice *dev)
{
/* We assume VLAN interfaces always support carrier detect */
return NM_DEVICE_CAP_CARRIER_DETECT | NM_DEVICE_CAP_NM_SUPPORTED;
}
static gboolean
real_hw_is_up (NMDevice *device)
{
return nm_system_iface_is_up (nm_device_get_ip_ifindex (device));
}
static gboolean
real_hw_bring_up (NMDevice *dev, gboolean *no_firmware)
{
return nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), TRUE, no_firmware);
}
static void
real_hw_take_down (NMDevice *dev)
{
nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), FALSE, NULL);
}
static void
real_update_hw_address (NMDevice *dev)
{
NMDeviceVlan *self = NM_DEVICE_VLAN (dev);
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
struct rtnl_link *rtnl;
struct nl_addr *addr;
rtnl = nm_netlink_index_to_rtnl_link (nm_device_get_ip_ifindex (dev));
if (!rtnl) {
nm_log_err (LOGD_HW | LOGD_DEVICE,
"(%s) failed to read hardware address (error %d)",
nm_device_get_iface (dev), errno);
return;
}
addr = rtnl_link_get_addr (rtnl);
if (!addr) {
nm_log_err (LOGD_HW | LOGD_DEVICE,
"(%s) no hardware address?",
nm_device_get_iface (dev));
goto out;
}
if (nl_addr_get_len (addr) > sizeof (priv->hw_addr)) {
nm_log_err (LOGD_HW | LOGD_DEVICE,
"(%s) hardware address is wrong length (got %d max %zd)",
nm_device_get_iface (dev),
nl_addr_get_len (addr),
sizeof (priv->hw_addr));
} else {
priv->hw_addr_len = nl_addr_get_len (addr);
memcpy (&priv->hw_addr, nl_addr_get_binary_addr (addr), priv->hw_addr_len);
g_object_notify (G_OBJECT (self), NM_DEVICE_VLAN_HW_ADDRESS);
}
out:
rtnl_link_put (rtnl);
}
static gboolean
real_can_interrupt_activation (NMDevice *dev)
{
/* Can interrupt activation if the carrier drops while activating */
return NM_DEVICE_VLAN_GET_PRIVATE (dev)->carrier ? FALSE : TRUE;
}
static gboolean
real_is_available (NMDevice *dev)
{
return NM_DEVICE_VLAN_GET_PRIVATE (dev)->carrier ? TRUE : FALSE;
}
/******************************************************************/
static gboolean
match_parent (NMDeviceVlan *self, const char *parent, GError **error)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
g_return_val_if_fail (parent != NULL, FALSE);
if (nm_utils_is_uuid (parent)) {
NMActRequest *parent_req;
NMConnection *parent_connection;
/* If the parent is a UUID, the connection matches if our parent
* device has that connection activated.
*/
parent_req = nm_device_get_act_request (priv->parent);
if (!parent_req) {
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"Parent interface not active; could not match UUID");
return FALSE;
}
parent_connection = nm_active_connection_get_connection (NM_ACTIVE_CONNECTION (parent_req));
if (!parent_connection) {
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"Parent interface had no connection; could not match UUID");
return FALSE;
}
if (g_strcmp0 (parent, nm_connection_get_uuid (parent_connection)) != 0) {
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"Parent interface UUID did not match connection UUID");
return FALSE;
}
} else {
/* interface name */
if (g_strcmp0 (parent, nm_device_get_ip_iface (priv->parent)) != 0) {
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"Parent interface name did not match connection");
return FALSE;
}
}
return TRUE;
}
static gboolean
match_vlan_connection (NMDeviceVlan *self, NMConnection *connection, GError **error)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
NMSettingVlan *s_vlan;
const char *parent, *iface = NULL;
s_vlan = nm_connection_get_setting_vlan (connection);
if (!s_vlan) {
g_set_error (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"The connection was not a VLAN connection.");
return FALSE;
}
if (nm_setting_vlan_get_id (s_vlan) != priv->vlan_id) {
g_set_error (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"The connection's VLAN ID did not match the device's VLAN ID.");
return FALSE;
}
/* Check parent interface; could be an interface name or a UUID */
parent = nm_setting_vlan_get_parent (s_vlan);
if (parent) {
if (!match_parent (self, parent, error))
return FALSE;
} else {
/* Parent could be a MAC address in a hardware-specific setting */
if (!nm_device_hwaddr_matches (priv->parent, connection, NULL, 0, TRUE)) {
g_set_error (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"Failed to match the VLAN parent interface via hardware address.");
return FALSE;
}
}
/* Ensure the interface name matches. If not specified we assume a match
* since both the parent interface and the VLAN ID matched by the time we
* get here.
*/
iface = nm_connection_get_virtual_iface_name (connection);
if (iface) {
if (g_strcmp0 (nm_device_get_ip_iface (NM_DEVICE (self)), iface) != 0) {
g_set_error (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"The VLAN connection virtual interface name did not match.");
return FALSE;
}
}
return TRUE;
}
static NMConnection *
real_get_best_auto_connection (NMDevice *dev,
GSList *connections,
char **specific_object)
{
GSList *iter;
for (iter = connections; iter; iter = g_slist_next (iter)) {
NMConnection *connection = NM_CONNECTION (iter->data);
NMSettingConnection *s_con;
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
if ( nm_setting_connection_get_autoconnect (s_con)
&& match_vlan_connection (NM_DEVICE_VLAN (dev), connection, NULL))
return connection;
}
return NULL;
}
static gboolean
real_check_connection_compatible (NMDevice *device,
NMConnection *connection,
GError **error)
{
return match_vlan_connection (NM_DEVICE_VLAN (device), connection, error);
}
static gboolean
real_complete_connection (NMDevice *device,
NMConnection *connection,
const char *specific_object,
const GSList *existing_connections,
GError **error)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
NMSettingVlan *s_vlan;
nm_utils_complete_generic (connection,
NM_SETTING_VLAN_SETTING_NAME,
existing_connections,
_("Vlan connection %d"),
NULL,
TRUE);
s_vlan = nm_connection_get_setting_vlan (connection);
if (!s_vlan) {
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"A 'vlan' setting is required.");
return FALSE;
}
/* If there's no VLAN interface, no parent, and no hardware address in the
* settings, then there's not enough information to complete the setting.
*/
if (!nm_setting_vlan_get_parent (s_vlan)) {
if (!nm_device_hwaddr_matches (priv->parent, connection, NULL, 0, TRUE)) {
/* FIXME: put priv->hw_addr into the connection in the appropriate
* hardware-specific setting.
*/
g_set_error_literal (error, NM_VLAN_ERROR, NM_VLAN_ERROR_CONNECTION_INVALID,
"The 'vlan' setting had no interface name, parent, or hardware address.");
return FALSE;
}
}
return TRUE;
}
static gboolean
spec_match_list (NMDevice *device, const GSList *specs)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
char *hwaddr;
gboolean matched;
int itype = nm_utils_hwaddr_type (priv->hw_addr_len);
hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, itype);
matched = nm_match_spec_hwaddr (specs, hwaddr);
g_free (hwaddr);
return matched;
}
static gboolean
vlan_match_config (NMDevice *device, NMConnection *connection)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
NMSettingVlan *s_vlan;
const char *ifname, *parent;
gboolean fail_if_no_hwaddr = FALSE;
s_vlan = nm_connection_get_setting_vlan (connection);
if (!s_vlan)
return FALSE;
/* Interface name */
ifname = nm_setting_vlan_get_interface_name (s_vlan);
if (g_strcmp0 (ifname, nm_device_get_ip_iface (device)) != 0)
return FALSE;
if (nm_setting_vlan_get_id (s_vlan) != priv->vlan_id)
return FALSE;
parent = nm_setting_vlan_get_parent (s_vlan);
if (parent) {
if (!match_parent (NM_DEVICE_VLAN (device), parent, NULL))
return FALSE;
} else {
/* If there's no parent and no interface name given, then the only way
* we have to identify the VLAN interface the connection matches is
* a hardware-specific setting's hardware address property, so we want
* to fail the match below if we there is none.
*/
if (ifname == NULL)
fail_if_no_hwaddr = TRUE;
}
/* MAC address check; we ask the parent to check our own MAC address,
* because only the parent knows what kind of NMSetting the MAC
* address will be in. The VLAN device shouldn't have to know what kind
* of interface the parent is.
*/
if (!nm_device_hwaddr_matches (priv->parent, connection, priv->hw_addr, priv->hw_addr_len, fail_if_no_hwaddr))
return FALSE;
return TRUE;
}
static NMConnection *
connection_match_config (NMDevice *self, const GSList *connections)
{
const GSList *iter;
/* First narrow @connections down to those that match in their
* NMSettingVlan configuration.
*/
for (iter = connections; iter; iter = iter->next) {
NMConnection *candidate = iter->data;
if (!nm_connection_is_type (candidate, NM_SETTING_VLAN_SETTING_NAME))
continue;
if (!vlan_match_config (self, candidate))
continue;
if (!nm_device_match_ip_config (self, candidate))
continue;
return candidate;
}
return NULL;
}
/******************************************************************/
static void
carrier_action_defer_clear (NMDeviceVlan *self)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
if (priv->carrier_action_defer_id) {
g_source_remove (priv->carrier_action_defer_id);
priv->carrier_action_defer_id = 0;
}
}
static gboolean
carrier_action_defer_cb (gpointer user_data)
{
NMDeviceVlan *self = NM_DEVICE_VLAN (user_data);
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
NMDeviceState state;
priv->carrier_action_defer_id = 0;
state = nm_device_get_state (NM_DEVICE (self));
if (state == NM_DEVICE_STATE_UNAVAILABLE) {
if (priv->carrier)
nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_DISCONNECTED, NM_DEVICE_STATE_REASON_CARRIER);
} else if (state >= NM_DEVICE_STATE_DISCONNECTED) {
if (!priv->carrier)
nm_device_state_changed (NM_DEVICE (self), NM_DEVICE_STATE_UNAVAILABLE, NM_DEVICE_STATE_REASON_CARRIER);
}
return FALSE;
}
static void
set_carrier (NMDeviceVlan *self,
const gboolean carrier,
const gboolean defer_action)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
NMDeviceState state;
if (priv->carrier == carrier)
return;
/* Clear any previous deferred action */
carrier_action_defer_clear (self);
priv->carrier = carrier;
g_object_notify (G_OBJECT (self), NM_DEVICE_VLAN_CARRIER);
state = nm_device_get_state (NM_DEVICE (self));
nm_log_info (LOGD_HW | LOGD_DEVICE,
"(%s): carrier now %s (device state %d%s)",
nm_device_get_iface (NM_DEVICE (self)),
carrier ? "ON" : "OFF",
state,
defer_action ? ", deferring action for 4 seconds" : "");
if (defer_action)
priv->carrier_action_defer_id = g_timeout_add_seconds (4, carrier_action_defer_cb, self);
else
carrier_action_defer_cb (self);
}
static void
carrier_on (NMNetlinkMonitor *monitor, int idx, NMDevice *device)
{
/* Make sure signal is for us */
if (idx == nm_device_get_ifindex (device))
set_carrier (NM_DEVICE_VLAN (device), TRUE, FALSE);
}
static void
carrier_off (NMNetlinkMonitor *monitor, int idx, NMDevice *device)
{
NMDeviceState state;
gboolean defer = FALSE;
/* Make sure signal is for us */
if (idx == nm_device_get_ifindex (device)) {
/* Defer carrier-off event actions while connected by a few seconds
* so that tripping over a cable, power-cycling a switch, or breaking
* off the RJ45 locking tab isn't so catastrophic.
*/
state = nm_device_get_state (device);
if (state > NM_DEVICE_STATE_DISCONNECTED)
defer = TRUE;
set_carrier (NM_DEVICE_VLAN (device), FALSE, defer);
}
}
static void
carrier_watch_init (NMDeviceVlan *self)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
GError *error = NULL;
guint32 ifflags = 0;
priv->monitor = nm_netlink_monitor_get ();
priv->link_connected_id = g_signal_connect (priv->monitor, "carrier-on",
G_CALLBACK (carrier_on),
self);
priv->link_disconnected_id = g_signal_connect (priv->monitor, "carrier-off",
G_CALLBACK (carrier_off),
self);
/* Get initial link state */
if (!nm_netlink_monitor_get_flags_sync (priv->monitor,
nm_device_get_ifindex (NM_DEVICE (self)),
&ifflags,
&error)) {
nm_log_warn (LOGD_HW | LOGD_DEVICE,
"(%s): couldn't get initial carrier state: (%d) %s",
nm_device_get_iface (NM_DEVICE (self)),
error ? error->code : -1,
(error && error->message) ? error->message : "unknown");
g_clear_error (&error);
} else
priv->carrier = !!(ifflags & IFF_LOWER_UP);
nm_log_info (LOGD_HW | LOGD_DEVICE, "(%s): carrier is %s",
nm_device_get_iface (NM_DEVICE (self)),
priv->carrier ? "ON" : "OFF");
/* Request link state again just in case an error occurred getting the
* initial link state.
*/
nm_netlink_monitor_request_status (priv->monitor);
}
/******************************************************************/
static void
parent_state_changed (NMDevice *parent,
NMDeviceState new_state,
NMDeviceState old_state,
NMDeviceStateReason reason,
gpointer user_data)
{
NMDeviceVlan *self = NM_DEVICE_VLAN (user_data);
if (new_state < NM_DEVICE_STATE_DISCONNECTED) {
/* If the parent becomes unavailable or unmanaged so does the VLAN */
nm_device_state_changed (NM_DEVICE (self), new_state, reason);
} else if ( new_state == NM_DEVICE_STATE_DISCONNECTED
&& old_state < NM_DEVICE_STATE_DISCONNECTED) {
/* Mark VLAN interface as available/disconnected when the parent
* becomes available as a result of carrier changes or becoming
* initialized.
*/
nm_device_state_changed (NM_DEVICE (self), new_state, reason);
}
}
/******************************************************************/
NMDevice *
nm_device_vlan_new (const char *udi, const char *iface, NMDevice *parent)
{
NMDevice *device;
g_return_val_if_fail (udi != NULL, NULL);
g_return_val_if_fail (iface != NULL, NULL);
g_return_val_if_fail (parent != NULL, NULL);
device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN,
NM_DEVICE_UDI, udi,
NM_DEVICE_IFACE, iface,
NM_DEVICE_DRIVER, "8021q",
NM_DEVICE_TYPE_DESC, "VLAN",
NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN,
NULL);
if (device) {
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
int ifindex = nm_device_get_ifindex (device);
int parent_ifindex = -1, itype;
int vlan_id;
itype = nm_system_get_iface_type (ifindex, iface);
g_assert (itype == NM_IFACE_TYPE_VLAN);
if (!nm_system_get_iface_vlan_info (ifindex, &parent_ifindex, &vlan_id)) {
nm_log_warn (LOGD_DEVICE, "(%s): failed to get VLAN interface info.", iface);
g_object_unref (device);
return NULL;
}
priv->vlan_id = vlan_id;
if ( parent_ifindex < 0
|| parent_ifindex != nm_device_get_ip_ifindex (parent)
|| priv->vlan_id < 0) {
nm_log_warn (LOGD_DEVICE, "(%s): VLAN parent ifindex (%d) or VLAN ID (%d) invalid.",
iface, parent_ifindex, priv->vlan_id);
g_object_unref (device);
return NULL;
}
priv->parent = g_object_ref (parent);
priv->parent_state_id = g_signal_connect (priv->parent,
"state-changed",
G_CALLBACK (parent_state_changed),
device);
carrier_watch_init (NM_DEVICE_VLAN (device));
nm_log_dbg (LOGD_HW | LOGD_ETHER, "(%s): kernel ifindex %d", iface, ifindex);
nm_log_info (LOGD_HW | LOGD_ETHER, "(%s): VLAN ID %d with parent %s",
iface, priv->vlan_id, nm_device_get_iface (parent));
}
return device;
}
static void
nm_device_vlan_init (NMDeviceVlan * self)
{
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
char *hwaddr;
switch (prop_id) {
case PROP_HW_ADDRESS:
hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, nm_utils_hwaddr_type (priv->hw_addr_len));
g_value_take_string (value, hwaddr);
break;
case PROP_CARRIER:
g_value_set_boolean (value, priv->carrier);
break;
case PROP_VLAN_ID:
g_value_set_uint (value, priv->vlan_id);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
switch (prop_id) {
case PROP_VLAN_ID:
priv->vlan_id = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
NMDeviceVlan *self = NM_DEVICE_VLAN (object);
NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
if (priv->disposed) {
G_OBJECT_CLASS (nm_device_vlan_parent_class)->dispose (object);
return;
}
priv->disposed = TRUE;
if (priv->link_connected_id)
g_signal_handler_disconnect (priv->monitor, priv->link_connected_id);
if (priv->link_disconnected_id)
g_signal_handler_disconnect (priv->monitor, priv->link_disconnected_id);
carrier_action_defer_clear (self);
g_object_unref (priv->monitor);
g_signal_handler_disconnect (priv->parent, priv->parent_state_id);
g_object_unref (priv->parent);
}
static void
nm_device_vlan_class_init (NMDeviceVlanClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NMDeviceClass *parent_class = NM_DEVICE_CLASS (klass);
g_type_class_add_private (object_class, sizeof (NMDeviceVlanPrivate));
/* virtual methods */
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->dispose = dispose;
parent_class->get_generic_capabilities = real_get_generic_capabilities;
parent_class->update_hw_address = real_update_hw_address;
parent_class->hw_is_up = real_hw_is_up;
parent_class->hw_bring_up = real_hw_bring_up;
parent_class->hw_take_down = real_hw_take_down;
parent_class->can_interrupt_activation = real_can_interrupt_activation;
parent_class->is_available = real_is_available;
parent_class->get_best_auto_connection = real_get_best_auto_connection;
parent_class->check_connection_compatible = real_check_connection_compatible;
parent_class->complete_connection = real_complete_connection;
parent_class->spec_match_list = spec_match_list;
parent_class->connection_match_config = connection_match_config;
/* properties */
g_object_class_install_property
(object_class, PROP_HW_ADDRESS,
g_param_spec_string (NM_DEVICE_VLAN_HW_ADDRESS,
"Active MAC Address",
"Currently set hardware MAC address",
NULL,
G_PARAM_READABLE));
g_object_class_install_property
(object_class, PROP_CARRIER,
g_param_spec_boolean (NM_DEVICE_VLAN_CARRIER,
"Carrier",
"Carrier",
FALSE,
G_PARAM_READABLE));
g_object_class_install_property
(object_class, PROP_VLAN_ID,
g_param_spec_uint (NM_DEVICE_VLAN_ID,
"VLAN ID",
"VLAN ID",
0, 4095, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/* Signals */
signals[PROPERTIES_CHANGED] =
nm_properties_changed_signal_new (object_class,
G_STRUCT_OFFSET (NMDeviceVlanClass, properties_changed));
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass),
&dbus_glib_nm_device_vlan_object_info);
dbus_g_error_domain_register (NM_VLAN_ERROR, NULL, NM_TYPE_VLAN_ERROR);
}

67
src/nm-device-vlan.h Normal file
View file

@ -0,0 +1,67 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2012 Red Hat, Inc.
*/
#ifndef NM_DEVICE_VLAN_H
#define NM_DEVICE_VLAN_H
#include <glib-object.h>
#include "nm-device-wired.h"
G_BEGIN_DECLS
#define NM_TYPE_DEVICE_VLAN (nm_device_vlan_get_type ())
#define NM_DEVICE_VLAN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_VLAN, NMDeviceVlan))
#define NM_DEVICE_VLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEVICE_VLAN, NMDeviceVlanClass))
#define NM_IS_DEVICE_VLAN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_VLAN))
#define NM_IS_DEVICE_VLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE_VLAN))
#define NM_DEVICE_VLAN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_VLAN, NMDeviceVlanClass))
typedef enum {
NM_VLAN_ERROR_CONNECTION_NOT_VLAN = 0,
NM_VLAN_ERROR_CONNECTION_INVALID,
NM_VLAN_ERROR_CONNECTION_INCOMPATIBLE,
} NMVlanError;
#define NM_DEVICE_VLAN_HW_ADDRESS "hw-address"
#define NM_DEVICE_VLAN_CARRIER "carrier"
#define NM_DEVICE_VLAN_ID "vlan-id"
typedef struct {
NMDevice parent;
} NMDeviceVlan;
typedef struct {
NMDeviceClass parent;
/* Signals */
void (*properties_changed) (NMDeviceVlan *device, GHashTable *properties);
} NMDeviceVlanClass;
GType nm_device_vlan_get_type (void);
NMDevice *nm_device_vlan_new (const char *udi,
const char *iface,
NMDevice *parent);
G_END_DECLS
#endif /* NM_DEVICE_VLAN_H */

View file

@ -47,6 +47,7 @@
#include "nm-device-modem.h"
#include "nm-device-infiniband.h"
#include "nm-device-bond.h"
#include "nm-device-vlan.h"
#include "nm-system.h"
#include "nm-properties-changed-signal.h"
#include "nm-setting-bluetooth.h"
@ -914,38 +915,30 @@ get_active_connections (NMManager *manager, NMConnection *filter)
/* Settings stuff via NMSettings */
/*******************************************************************/
static const char *
get_iface_from_hwaddr (NMManager *self,
NMConnection *connection,
int *out_ifindex)
static NMDevice *
get_device_from_hwaddr (NMManager *self, NMConnection *connection)
{
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
GSList *iter;
for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
NMDevice *candidate = iter->data;
if (nm_device_hwaddr_matches (candidate, connection, NULL, 0, TRUE)) {
if (out_ifindex)
*out_ifindex = nm_device_get_ip_ifindex (candidate);
return nm_device_get_ip_iface (candidate);
}
if (nm_device_hwaddr_matches (NM_DEVICE (iter->data), connection, NULL, 0, TRUE))
return iter->data;
}
return NULL;
}
static const char *
find_vlan_parent_iface (NMManager *self,
NMConnection *connection,
gboolean check_hwaddr,
int *out_parent_ifindex)
static NMDevice*
find_vlan_parent (NMManager *self,
NMConnection *connection,
gboolean check_hwaddr)
{
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
NMSettingVlan *s_vlan;
const char *parent;
NMConnection *parent_connection;
const char *parent_iface;
NMDevice *parent = NULL;
GSList *iter;
NMDevice *device;
/* The 'parent' property could be either an interface name, a connection
* UUID, or even given by the MAC address of the connection's ethernet,
@ -954,33 +947,32 @@ find_vlan_parent_iface (NMManager *self,
s_vlan = nm_connection_get_setting_vlan (connection);
g_return_val_if_fail (s_vlan != NULL, NULL);
parent = nm_setting_vlan_get_parent (s_vlan);
if (parent) {
device = find_device_by_ip_iface (self, parent);
if (device) {
if (out_parent_ifindex)
*out_parent_ifindex = nm_device_get_ip_ifindex (device);
parent_iface = nm_setting_vlan_get_parent (s_vlan);
if (parent_iface) {
parent = find_device_by_ip_iface (self, parent_iface);
if (parent)
return parent;
}
if (nm_utils_is_uuid (parent)) {
if (nm_utils_is_uuid (parent_iface)) {
/* Try as a connection UUID */
parent_connection = (NMConnection *) nm_settings_get_connection_by_uuid (priv->settings, parent);
parent_connection = (NMConnection *) nm_settings_get_connection_by_uuid (priv->settings, parent_iface);
if (parent_connection) {
/* Check if the parent connection is activated on some device already */
for (iter = priv->devices; iter; iter = g_slist_next (iter)) {
NMDevice *candidate = NM_DEVICE (iter->data);
NMActRequest *req;
NMConnection *candidate;
if (nm_device_get_connection (candidate) == parent_connection) {
if (out_parent_ifindex)
*out_parent_ifindex = nm_device_get_ip_ifindex (candidate);
return nm_device_get_iface (candidate);
req = nm_device_get_act_request (NM_DEVICE (iter->data));
if (req) {
candidate = nm_active_connection_get_connection (NM_ACTIVE_CONNECTION (req));
if (candidate == parent_connection)
return parent;
}
}
/* Check the hardware address of the parent connection */
if (check_hwaddr)
return get_iface_from_hwaddr (self, parent_connection, out_parent_ifindex);
return get_device_from_hwaddr (self, parent_connection);
}
return NULL;
}
@ -988,7 +980,7 @@ find_vlan_parent_iface (NMManager *self,
/* Try the hardware address from the VLAN connection's hardware setting */
if (check_hwaddr)
return get_iface_from_hwaddr (self, connection, out_parent_ifindex);
return get_device_from_hwaddr (self, connection);
return NULL;
}
@ -997,7 +989,7 @@ find_vlan_parent_iface (NMManager *self,
* get_virtual_iface_name:
* @self: the #NMManager
* @connection: the #NMConnection representing a virtual interface
* @out_parent_ifindex: on success, the parent interface index if any
* @out_parent: on success, the parent device if any
*
* Given @connection, returns the interface name that the connection
* would represent. If the interface name is not given by the connection,
@ -1009,24 +1001,25 @@ find_vlan_parent_iface (NMManager *self,
static char *
get_virtual_iface_name (NMManager *self,
NMConnection *connection,
int *out_parent_ifindex)
NMDevice **out_parent)
{
char *vname = NULL;
NMDevice *parent = NULL;
if (out_parent_ifindex)
*out_parent_ifindex = -1;
if (out_parent)
*out_parent = NULL;
if (nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME))
return g_strdup (nm_connection_get_virtual_iface_name (connection));
if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) {
NMSettingVlan *s_vlan;
const char *ifname, *parent;
const char *ifname;
s_vlan = nm_connection_get_setting_vlan (connection);
g_return_val_if_fail (s_vlan != NULL, NULL);
parent = find_vlan_parent_iface (self, connection, TRUE, out_parent_ifindex);
parent = find_vlan_parent (self, connection, TRUE);
if (parent) {
/* If the connection doesn't specify the interface name for the VLAN
* device, we create one for it using the VLAN ID and the parent
@ -1035,8 +1028,12 @@ get_virtual_iface_name (NMManager *self,
ifname = nm_connection_get_virtual_iface_name (connection);
if (ifname)
vname = g_strdup (ifname);
else
vname = nm_utils_new_vlan_name (parent, nm_setting_vlan_get_id (s_vlan));
else {
vname = nm_utils_new_vlan_name (nm_device_get_ip_iface (parent),
nm_setting_vlan_get_id (s_vlan));
}
if (out_parent)
*out_parent = parent;
}
}
@ -1078,10 +1075,9 @@ system_create_virtual_device (NMManager *self, NMConnection *connection)
NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
GSList *iter;
char *iface = NULL, *udi;
NMDevice *device = NULL;
int parent_ifindex = -1;
NMDevice *device = NULL, *parent = NULL;
iface = get_virtual_iface_name (self, connection, &parent_ifindex);
iface = get_virtual_iface_name (self, connection, &parent);
if (!iface) {
nm_log_warn (LOGD_DEVICE, "(%s) failed to determine virtual interface name",
nm_connection_get_id (connection));
@ -1112,15 +1108,15 @@ system_create_virtual_device (NMManager *self, NMConnection *connection)
device = nm_device_bond_new (udi, iface);
g_free (udi);
} else if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) {
g_return_val_if_fail (parent_ifindex >= 0, FALSE);
g_return_val_if_fail (parent != NULL, FALSE);
if (!nm_system_add_vlan_iface (connection, iface, parent_ifindex)) {
if (!nm_system_add_vlan_iface (connection, iface, nm_device_get_ip_ifindex (parent))) {
nm_log_warn (LOGD_DEVICE, "(%s): failed to add VLAN interface for '%s'",
iface, nm_connection_get_id (connection));
goto out;
}
udi = get_virtual_iface_placeholder_udi ();
device = nm_device_ethernet_new (udi, iface, "8021q");
device = nm_device_vlan_new (udi, iface, parent);
g_free (udi);
}
@ -2456,7 +2452,7 @@ nm_manager_activate_connection (NMManager *manager,
} else {
NMDeviceState state;
char *iface;
int master_ifindex = -1;
NMDevice *parent = NULL;
/* Device-based connection */
if (device_path) {
@ -2503,7 +2499,7 @@ nm_manager_activate_connection (NMManager *manager,
return NULL;
}
iface = get_virtual_iface_name (manager, connection, &master_ifindex);
iface = get_virtual_iface_name (manager, connection, &parent);
if (!iface) {
g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_DEVICE,
"Failed to determine connection's virtual interface name");