1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2012 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <netinet/ether.h>
26 #include "nm-setting-connection.h"
27 #include "nm-setting-vlan.h"
30 #include "nm-device-vlan.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
34 G_DEFINE_TYPE (NMDeviceVlan, nm_device_vlan, NM_TYPE_DEVICE)
36 #define NM_DEVICE_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VLAN, NMDeviceVlanPrivate))
45 } NMDeviceVlanPrivate;
58 * nm_device_vlan_error_quark:
60 * Registers an error quark for #NMDeviceVlan if necessary.
62 * Returns: the error quark used for #NMDeviceVlan errors.
65 nm_device_vlan_error_quark (void)
67 static GQuark quark = 0;
69 if (G_UNLIKELY (quark == 0))
70 quark = g_quark_from_static_string ("nm-device-vlan-error-quark");
76 * @connection: the #DBusGConnection
77 * @path: the DBus object path of the device
79 * Creates a new #NMDeviceVlan.
81 * Returns: (transfer full): a new device
84 nm_device_vlan_new (DBusGConnection *connection, const char *path)
88 g_return_val_if_fail (connection != NULL, NULL);
89 g_return_val_if_fail (path != NULL, NULL);
91 device = g_object_new (NM_TYPE_DEVICE_VLAN,
92 NM_OBJECT_DBUS_CONNECTION, connection,
93 NM_OBJECT_DBUS_PATH, path,
95 _nm_object_ensure_inited (NM_OBJECT (device));
100 * nm_device_vlan_get_hw_address:
101 * @device: a #NMDeviceVlan
103 * Gets the hardware (MAC) address of the #NMDeviceVlan
105 * Returns: the hardware address. This is the internal string used by the
106 * device, and must not be modified.
109 nm_device_vlan_get_hw_address (NMDeviceVlan *device)
111 g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), NULL);
113 _nm_object_ensure_inited (NM_OBJECT (device));
114 return NM_DEVICE_VLAN_GET_PRIVATE (device)->hw_address;
118 * nm_device_vlan_get_carrier:
119 * @device: a #NMDeviceVlan
121 * Whether the device has carrier.
123 * Returns: %TRUE if the device has carrier
126 nm_device_vlan_get_carrier (NMDeviceVlan *device)
128 g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
130 _nm_object_ensure_inited (NM_OBJECT (device));
131 return NM_DEVICE_VLAN_GET_PRIVATE (device)->carrier;
135 * nm_device_vlan_get_parent:
136 * @device: a #NMDeviceVlan
138 * Returns: (transfer none): the device's parent device
143 nm_device_vlan_get_parent (NMDeviceVlan *device)
145 g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
147 _nm_object_ensure_inited (NM_OBJECT (device));
148 return NM_DEVICE_VLAN_GET_PRIVATE (device)->parent;
152 * nm_device_vlan_get_vlan_id:
153 * @device: a #NMDeviceVlan
155 * Returns: the device's VLAN ID
158 nm_device_vlan_get_vlan_id (NMDeviceVlan *device)
160 g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
162 _nm_object_ensure_inited (NM_OBJECT (device));
163 return NM_DEVICE_VLAN_GET_PRIVATE (device)->vlan_id;
167 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
169 NMSettingConnection *s_con;
170 NMSettingVlan *s_vlan;
171 NMSettingWired *s_wired;
172 const char *ctype, *dev_iface_name, *vlan_iface_name;
173 const GByteArray *mac_address;
174 char *mac_address_str;
176 s_con = nm_connection_get_setting_connection (connection);
179 ctype = nm_setting_connection_get_connection_type (s_con);
180 if (strcmp (ctype, NM_SETTING_VLAN_SETTING_NAME) != 0) {
181 g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_NOT_VLAN_CONNECTION,
182 "The connection was not a VLAN connection.");
186 s_vlan = nm_connection_get_setting_vlan (connection);
188 g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_INVALID_VLAN_CONNECTION,
189 "The connection was not a valid VLAN connection.");
193 if (nm_setting_vlan_get_id (s_vlan) != nm_device_vlan_get_vlan_id (NM_DEVICE_VLAN (device))) {
194 g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_ID_MISMATCH,
195 "The VLAN identifiers of the device and the connection didn't match.");
199 dev_iface_name = nm_device_get_iface (device);
200 vlan_iface_name = nm_setting_vlan_get_interface_name (s_vlan);
201 if (vlan_iface_name && g_strcmp0 (dev_iface_name, vlan_iface_name) != 0) {
202 g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_INTERFACE_MISMATCH,
203 "The interfaces of the device and the connection didn't match.");
207 s_wired = nm_connection_get_setting_wired (connection);
209 mac_address = nm_setting_wired_get_mac_address (s_wired);
213 mac_address_str = nm_utils_hwaddr_ntoa_len (mac_address->data, mac_address->len);
214 if (!g_strcmp0 (mac_address_str, NM_DEVICE_VLAN_GET_PRIVATE (device)->hw_address)) {
215 g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_MAC_MISMATCH,
216 "The hardware address of the device and the connection didn't match.");
218 g_free (mac_address_str);
221 return NM_DEVICE_CLASS (nm_device_vlan_parent_class)->connection_compatible (device, connection, error);
225 get_setting_type (NMDevice *device)
227 return NM_TYPE_SETTING_VLAN;
231 get_hw_address (NMDevice *device)
233 return nm_device_vlan_get_hw_address (NM_DEVICE_VLAN (device));
236 /***********************************************************/
239 nm_device_vlan_init (NMDeviceVlan *device)
241 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_VLAN);
245 register_properties (NMDeviceVlan *device)
247 NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
248 const NMPropertiesInfo property_info[] = {
249 { NM_DEVICE_VLAN_HW_ADDRESS, &priv->hw_address },
250 { NM_DEVICE_VLAN_CARRIER, &priv->carrier },
251 { NM_DEVICE_VLAN_PARENT, &priv->parent, NULL, NM_TYPE_DEVICE },
252 { NM_DEVICE_VLAN_VLAN_ID, &priv->vlan_id },
256 _nm_object_register_properties (NM_OBJECT (device),
262 constructed (GObject *object)
264 NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
266 G_OBJECT_CLASS (nm_device_vlan_parent_class)->constructed (object);
268 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_VLAN);
269 register_properties (NM_DEVICE_VLAN (object));
273 dispose (GObject *object)
275 NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
277 g_clear_object (&priv->parent);
278 g_clear_object (&priv->proxy);
280 G_OBJECT_CLASS (nm_device_vlan_parent_class)->dispose (object);
284 finalize (GObject *object)
286 NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
288 g_free (priv->hw_address);
290 G_OBJECT_CLASS (nm_device_vlan_parent_class)->finalize (object);
294 get_property (GObject *object,
299 NMDeviceVlan *device = NM_DEVICE_VLAN (object);
301 _nm_object_ensure_inited (NM_OBJECT (object));
304 case PROP_HW_ADDRESS:
305 g_value_set_string (value, nm_device_vlan_get_hw_address (device));
308 g_value_set_boolean (value, nm_device_vlan_get_carrier (device));
311 g_value_set_object (value, nm_device_vlan_get_parent (device));
314 g_value_set_uint (value, nm_device_vlan_get_vlan_id (device));
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 nm_device_vlan_class_init (NMDeviceVlanClass *vlan_class)
325 GObjectClass *object_class = G_OBJECT_CLASS (vlan_class);
326 NMDeviceClass *device_class = NM_DEVICE_CLASS (vlan_class);
328 g_type_class_add_private (vlan_class, sizeof (NMDeviceVlanPrivate));
330 /* virtual methods */
331 object_class->constructed = constructed;
332 object_class->dispose = dispose;
333 object_class->finalize = finalize;
334 object_class->get_property = get_property;
335 device_class->connection_compatible = connection_compatible;
336 device_class->get_setting_type = get_setting_type;
337 device_class->get_hw_address = get_hw_address;
342 * NMDeviceVlan:hw-address:
344 * The hardware (MAC) address of the device.
346 g_object_class_install_property
347 (object_class, PROP_HW_ADDRESS,
348 g_param_spec_string (NM_DEVICE_VLAN_HW_ADDRESS, "", "",
351 G_PARAM_STATIC_STRINGS));
354 * NMDeviceVlan:carrier:
356 * Whether the device has carrier.
358 g_object_class_install_property
359 (object_class, PROP_CARRIER,
360 g_param_spec_boolean (NM_DEVICE_VLAN_CARRIER, "", "",
363 G_PARAM_STATIC_STRINGS));
366 * NMDeviceVlan:parent:
368 * The devices's parent device.
372 g_object_class_install_property
373 (object_class, PROP_PARENT,
374 g_param_spec_object (NM_DEVICE_VLAN_PARENT, "", "",
377 G_PARAM_STATIC_STRINGS));
380 * NMDeviceVlan:vlan-id:
382 * The device's VLAN ID.
384 g_object_class_install_property
385 (object_class, PROP_VLAN_ID,
386 g_param_spec_uint (NM_DEVICE_VLAN_VLAN_ID, "", "",
389 G_PARAM_STATIC_STRINGS));