device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-vlan.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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.
7  *
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.
12  *
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.
17  *
18  * Copyright 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24 #include <netinet/ether.h>
25
26 #include "nm-setting-connection.h"
27 #include "nm-setting-vlan.h"
28 #include "nm-utils.h"
29
30 #include "nm-device-vlan.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33
34 G_DEFINE_TYPE (NMDeviceVlan, nm_device_vlan, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VLAN, NMDeviceVlanPrivate))
37
38 typedef struct {
39         DBusGProxy *proxy;
40
41         char *hw_address;
42         gboolean carrier;
43         NMDevice *parent;
44         guint vlan_id;
45 } NMDeviceVlanPrivate;
46
47 enum {
48         PROP_0,
49         PROP_HW_ADDRESS,
50         PROP_CARRIER,
51         PROP_PARENT,
52         PROP_VLAN_ID,
53
54         LAST_PROP
55 };
56
57 /**
58  * nm_device_vlan_error_quark:
59  *
60  * Registers an error quark for #NMDeviceVlan if necessary.
61  *
62  * Returns: the error quark used for #NMDeviceVlan errors.
63  **/
64 GQuark
65 nm_device_vlan_error_quark (void)
66 {
67         static GQuark quark = 0;
68
69         if (G_UNLIKELY (quark == 0))
70                 quark = g_quark_from_static_string ("nm-device-vlan-error-quark");
71         return quark;
72 }
73
74 /**
75  * nm_device_vlan_new:
76  * @connection: the #DBusGConnection
77  * @path: the DBus object path of the device
78  *
79  * Creates a new #NMDeviceVlan.
80  *
81  * Returns: (transfer full): a new device
82  **/
83 GObject *
84 nm_device_vlan_new (DBusGConnection *connection, const char *path)
85 {
86         GObject *device;
87
88         g_return_val_if_fail (connection != NULL, NULL);
89         g_return_val_if_fail (path != NULL, NULL);
90
91         device = g_object_new (NM_TYPE_DEVICE_VLAN,
92                                NM_OBJECT_DBUS_CONNECTION, connection,
93                                NM_OBJECT_DBUS_PATH, path,
94                                NULL);
95         _nm_object_ensure_inited (NM_OBJECT (device));
96         return device;
97 }
98
99 /**
100  * nm_device_vlan_get_hw_address:
101  * @device: a #NMDeviceVlan
102  *
103  * Gets the hardware (MAC) address of the #NMDeviceVlan
104  *
105  * Returns: the hardware address. This is the internal string used by the
106  * device, and must not be modified.
107  **/
108 const char *
109 nm_device_vlan_get_hw_address (NMDeviceVlan *device)
110 {
111         g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), NULL);
112
113         _nm_object_ensure_inited (NM_OBJECT (device));
114         return NM_DEVICE_VLAN_GET_PRIVATE (device)->hw_address;
115 }
116
117 /**
118  * nm_device_vlan_get_carrier:
119  * @device: a #NMDeviceVlan
120  *
121  * Whether the device has carrier.
122  *
123  * Returns: %TRUE if the device has carrier
124  **/
125 gboolean
126 nm_device_vlan_get_carrier (NMDeviceVlan *device)
127 {
128         g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
129
130         _nm_object_ensure_inited (NM_OBJECT (device));
131         return NM_DEVICE_VLAN_GET_PRIVATE (device)->carrier;
132 }
133
134 /**
135  * nm_device_vlan_get_parent:
136  * @device: a #NMDeviceVlan
137  *
138  * Returns: (transfer none): the device's parent device
139  *
140  * Since: 1.0
141  **/
142 NMDevice *
143 nm_device_vlan_get_parent (NMDeviceVlan *device)
144 {
145         g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
146
147         _nm_object_ensure_inited (NM_OBJECT (device));
148         return NM_DEVICE_VLAN_GET_PRIVATE (device)->parent;
149 }
150
151 /**
152  * nm_device_vlan_get_vlan_id:
153  * @device: a #NMDeviceVlan
154  *
155  * Returns: the device's VLAN ID
156  **/
157 guint
158 nm_device_vlan_get_vlan_id (NMDeviceVlan *device)
159 {
160         g_return_val_if_fail (NM_IS_DEVICE_VLAN (device), FALSE);
161
162         _nm_object_ensure_inited (NM_OBJECT (device));
163         return NM_DEVICE_VLAN_GET_PRIVATE (device)->vlan_id;
164 }
165
166 static gboolean
167 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
168 {
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;
175
176         s_con = nm_connection_get_setting_connection (connection);
177         g_assert (s_con);
178
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.");
183                 return FALSE;
184         }
185
186         s_vlan = nm_connection_get_setting_vlan (connection);
187         if (!s_vlan) {
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.");
190                 return FALSE;
191         }
192
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.");
196                 return FALSE;
197         }
198
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.");
204                 return FALSE;
205         }
206
207         s_wired = nm_connection_get_setting_wired (connection);
208         if (s_wired)
209                 mac_address = nm_setting_wired_get_mac_address (s_wired);
210         else
211                 mac_address = NULL;
212         if (mac_address) {
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.");
217                 }
218                 g_free (mac_address_str);
219         }
220
221         return NM_DEVICE_CLASS (nm_device_vlan_parent_class)->connection_compatible (device, connection, error);
222 }
223
224 static GType
225 get_setting_type (NMDevice *device)
226 {
227         return NM_TYPE_SETTING_VLAN;
228 }
229
230 static const char *
231 get_hw_address (NMDevice *device)
232 {
233         return nm_device_vlan_get_hw_address (NM_DEVICE_VLAN (device));
234 }
235
236 /***********************************************************/
237
238 static void
239 nm_device_vlan_init (NMDeviceVlan *device)
240 {
241         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_VLAN);
242 }
243
244 static void
245 register_properties (NMDeviceVlan *device)
246 {
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 },
253                 { NULL },
254         };
255
256         _nm_object_register_properties (NM_OBJECT (device),
257                                         priv->proxy,
258                                         property_info);
259 }
260
261 static void
262 constructed (GObject *object)
263 {
264         NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
265
266         G_OBJECT_CLASS (nm_device_vlan_parent_class)->constructed (object);
267
268         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_VLAN);
269         register_properties (NM_DEVICE_VLAN (object));
270 }
271
272 static void
273 dispose (GObject *object)
274 {
275         NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
276
277         g_clear_object (&priv->parent);
278         g_clear_object (&priv->proxy);
279
280         G_OBJECT_CLASS (nm_device_vlan_parent_class)->dispose (object);
281 }
282
283 static void
284 finalize (GObject *object)
285 {
286         NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (object);
287
288         g_free (priv->hw_address);
289
290         G_OBJECT_CLASS (nm_device_vlan_parent_class)->finalize (object);
291 }
292
293 static void
294 get_property (GObject *object,
295               guint prop_id,
296               GValue *value,
297               GParamSpec *pspec)
298 {
299         NMDeviceVlan *device = NM_DEVICE_VLAN (object);
300
301         _nm_object_ensure_inited (NM_OBJECT (object));
302
303         switch (prop_id) {
304         case PROP_HW_ADDRESS:
305                 g_value_set_string (value, nm_device_vlan_get_hw_address (device));
306                 break;
307         case PROP_CARRIER:
308                 g_value_set_boolean (value, nm_device_vlan_get_carrier (device));
309                 break;
310         case PROP_PARENT:
311                 g_value_set_object (value, nm_device_vlan_get_parent (device));
312                 break;
313         case PROP_VLAN_ID:
314                 g_value_set_uint (value, nm_device_vlan_get_vlan_id (device));
315                 break;
316         default:
317                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318                 break;
319         }
320 }
321
322 static void
323 nm_device_vlan_class_init (NMDeviceVlanClass *vlan_class)
324 {
325         GObjectClass *object_class = G_OBJECT_CLASS (vlan_class);
326         NMDeviceClass *device_class = NM_DEVICE_CLASS (vlan_class);
327
328         g_type_class_add_private (vlan_class, sizeof (NMDeviceVlanPrivate));
329
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;
338
339         /* properties */
340
341         /**
342          * NMDeviceVlan:hw-address:
343          *
344          * The hardware (MAC) address of the device.
345          **/
346         g_object_class_install_property
347                 (object_class, PROP_HW_ADDRESS,
348                  g_param_spec_string (NM_DEVICE_VLAN_HW_ADDRESS, "", "",
349                                       NULL,
350                                       G_PARAM_READABLE |
351                                       G_PARAM_STATIC_STRINGS));
352
353         /**
354          * NMDeviceVlan:carrier:
355          *
356          * Whether the device has carrier.
357          **/
358         g_object_class_install_property
359                 (object_class, PROP_CARRIER,
360                  g_param_spec_boolean (NM_DEVICE_VLAN_CARRIER, "", "",
361                                        FALSE,
362                                        G_PARAM_READABLE |
363                                        G_PARAM_STATIC_STRINGS));
364
365         /**
366          * NMDeviceVlan:parent:
367          *
368          * The devices's parent device.
369          *
370          * Since: 1.0
371          **/
372         g_object_class_install_property
373             (object_class, PROP_PARENT,
374              g_param_spec_object (NM_DEVICE_VLAN_PARENT, "", "",
375                                   NM_TYPE_DEVICE,
376                                   G_PARAM_READABLE |
377                                   G_PARAM_STATIC_STRINGS));
378
379         /**
380          * NMDeviceVlan:vlan-id:
381          *
382          * The device's VLAN ID.
383          **/
384         g_object_class_install_property
385                 (object_class, PROP_VLAN_ID,
386                  g_param_spec_uint (NM_DEVICE_VLAN_VLAN_ID, "", "",
387                                     0, 4095, 0,
388                                     G_PARAM_READABLE |
389                                     G_PARAM_STATIC_STRINGS));
390 }