device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-bond.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-bond.h"
28 #include "nm-utils.h"
29
30 #include "nm-device-bond.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33 #include "nm-types.h"
34
35 G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE)
36
37 #define NM_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BOND, NMDeviceBondPrivate))
38
39 typedef struct {
40         DBusGProxy *proxy;
41
42         char *hw_address;
43         gboolean carrier;
44         GPtrArray *slaves;
45 } NMDeviceBondPrivate;
46
47 enum {
48         PROP_0,
49         PROP_HW_ADDRESS,
50         PROP_CARRIER,
51         PROP_SLAVES,
52
53         LAST_PROP
54 };
55
56 /**
57  * nm_device_bond_error_quark:
58  *
59  * Registers an error quark for #NMDeviceBond if necessary.
60  *
61  * Returns: the error quark used for #NMDeviceBond errors.
62  **/
63 GQuark
64 nm_device_bond_error_quark (void)
65 {
66         static GQuark quark = 0;
67
68         if (G_UNLIKELY (quark == 0))
69                 quark = g_quark_from_static_string ("nm-device-bond-error-quark");
70         return quark;
71 }
72
73 /**
74  * nm_device_bond_new:
75  * @connection: the #DBusGConnection
76  * @path: the DBus object path of the device
77  *
78  * Creates a new #NMDeviceBond.
79  *
80  * Returns: (transfer full): a new device
81  **/
82 GObject *
83 nm_device_bond_new (DBusGConnection *connection, const char *path)
84 {
85         GObject *device;
86
87         g_return_val_if_fail (connection != NULL, NULL);
88         g_return_val_if_fail (path != NULL, NULL);
89
90         device = g_object_new (NM_TYPE_DEVICE_BOND,
91                                NM_OBJECT_DBUS_CONNECTION, connection,
92                                NM_OBJECT_DBUS_PATH, path,
93                                NULL);
94         _nm_object_ensure_inited (NM_OBJECT (device));
95         return device;
96 }
97
98 /**
99  * nm_device_bond_get_hw_address:
100  * @device: a #NMDeviceBond
101  *
102  * Gets the hardware (MAC) address of the #NMDeviceBond
103  *
104  * Returns: the hardware address. This is the internal string used by the
105  * device, and must not be modified.
106  **/
107 const char *
108 nm_device_bond_get_hw_address (NMDeviceBond *device)
109 {
110         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), NULL);
111
112         _nm_object_ensure_inited (NM_OBJECT (device));
113         return NM_DEVICE_BOND_GET_PRIVATE (device)->hw_address;
114 }
115
116 /**
117  * nm_device_bond_get_carrier:
118  * @device: a #NMDeviceBond
119  *
120  * Whether the device has carrier.
121  *
122  * Returns: %TRUE if the device has carrier
123  **/
124 gboolean
125 nm_device_bond_get_carrier (NMDeviceBond *device)
126 {
127         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
128
129         _nm_object_ensure_inited (NM_OBJECT (device));
130         return NM_DEVICE_BOND_GET_PRIVATE (device)->carrier;
131 }
132
133 /**
134  * nm_device_bond_get_slaves:
135  * @device: a #NMDeviceBond
136  *
137  * Gets the devices currently slaved to @device.
138  *
139  * Returns: (element-type NMDevice): the #GPtrArray containing
140  * #NMDevices that are slaves of @device. This is the internal
141  * copy used by the device, and must not be modified.
142  *
143  * Since: 0.9.6.4
144  **/
145 const GPtrArray *
146 nm_device_bond_get_slaves (NMDeviceBond *device)
147 {
148         g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
149
150         _nm_object_ensure_inited (NM_OBJECT (device));
151         return handle_ptr_array_return (NM_DEVICE_BOND_GET_PRIVATE (device)->slaves);
152 }
153
154 static gboolean
155 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
156 {
157         NMSettingConnection *s_con;
158         NMSettingBond *s_bond;
159         const char *ctype, *dev_iface_name, *bond_iface_name;
160
161         s_con = nm_connection_get_setting_connection (connection);
162         g_assert (s_con);
163
164         ctype = nm_setting_connection_get_connection_type (s_con);
165         if (strcmp (ctype, NM_SETTING_BOND_SETTING_NAME) != 0) {
166                 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_NOT_BOND_CONNECTION,
167                              "The connection was not a bond connection.");
168                 return FALSE;
169         }
170
171         s_bond = nm_connection_get_setting_bond (connection);
172         if (!s_bond) {
173                 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_INVALID_BOND_CONNECTION,
174                              "The connection was not a valid bond connection.");
175                 return FALSE;
176         }
177
178         dev_iface_name = nm_device_get_iface (device);
179         bond_iface_name = nm_setting_bond_get_interface_name (s_bond);
180         if (g_strcmp0 (dev_iface_name, bond_iface_name) != 0) {
181                 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_INTERFACE_MISMATCH,
182                              "The interfaces of the device and the connection didn't match.");
183                 return FALSE;
184         }
185
186         /* FIXME: check slaves? */
187
188         return NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_compatible (device, connection, error);
189 }
190
191 static GType
192 get_setting_type (NMDevice *device)
193 {
194         return NM_TYPE_SETTING_BOND;
195 }
196
197 static const char *
198 get_hw_address (NMDevice *device)
199 {
200         return nm_device_bond_get_hw_address (NM_DEVICE_BOND (device));
201 }
202
203 /***********************************************************/
204
205 static void
206 nm_device_bond_init (NMDeviceBond *device)
207 {
208         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BOND);
209 }
210
211 static void
212 register_properties (NMDeviceBond *device)
213 {
214         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (device);
215         const NMPropertiesInfo property_info[] = {
216                 { NM_DEVICE_BOND_HW_ADDRESS, &priv->hw_address },
217                 { NM_DEVICE_BOND_CARRIER,    &priv->carrier },
218                 { NM_DEVICE_BOND_SLAVES,     &priv->slaves, NULL, NM_TYPE_DEVICE },
219                 { NULL },
220         };
221
222         _nm_object_register_properties (NM_OBJECT (device),
223                                         priv->proxy,
224                                         property_info);
225 }
226
227 static void
228 constructed (GObject *object)
229 {
230         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
231
232         G_OBJECT_CLASS (nm_device_bond_parent_class)->constructed (object);
233
234         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_BOND);
235         register_properties (NM_DEVICE_BOND (object));
236 }
237
238 static void
239 dispose (GObject *object)
240 {
241         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
242
243         g_clear_object (&priv->proxy);
244
245         if (priv->slaves) {
246                 g_ptr_array_set_free_func (priv->slaves, g_object_unref);
247                 g_ptr_array_free (priv->slaves, TRUE);
248                 priv->slaves = NULL;
249         }
250
251         G_OBJECT_CLASS (nm_device_bond_parent_class)->dispose (object);
252 }
253
254 static void
255 finalize (GObject *object)
256 {
257         NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
258
259         g_free (priv->hw_address);
260
261         G_OBJECT_CLASS (nm_device_bond_parent_class)->finalize (object);
262 }
263
264 static void
265 get_property (GObject *object,
266               guint prop_id,
267               GValue *value,
268               GParamSpec *pspec)
269 {
270         NMDeviceBond *device = NM_DEVICE_BOND (object);
271
272         _nm_object_ensure_inited (NM_OBJECT (object));
273
274         switch (prop_id) {
275         case PROP_HW_ADDRESS:
276                 g_value_set_string (value, nm_device_bond_get_hw_address (device));
277                 break;
278         case PROP_CARRIER:
279                 g_value_set_boolean (value, nm_device_bond_get_carrier (device));
280                 break;
281         case PROP_SLAVES:
282                 g_value_set_boxed (value, nm_device_bond_get_slaves (device));
283                 break;
284         default:
285                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286                 break;
287         }
288 }
289
290 static void
291 nm_device_bond_class_init (NMDeviceBondClass *bond_class)
292 {
293         GObjectClass *object_class = G_OBJECT_CLASS (bond_class);
294         NMDeviceClass *device_class = NM_DEVICE_CLASS (bond_class);
295
296         g_type_class_add_private (bond_class, sizeof (NMDeviceBondPrivate));
297
298         /* virtual methods */
299         object_class->constructed = constructed;
300         object_class->dispose = dispose;
301         object_class->finalize = finalize;
302         object_class->get_property = get_property;
303         device_class->connection_compatible = connection_compatible;
304         device_class->get_setting_type = get_setting_type;
305         device_class->get_hw_address = get_hw_address;
306
307         /* properties */
308
309         /**
310          * NMDeviceBond:hw-address:
311          *
312          * The hardware (MAC) address of the device.
313          **/
314         g_object_class_install_property
315                 (object_class, PROP_HW_ADDRESS,
316                  g_param_spec_string (NM_DEVICE_BOND_HW_ADDRESS, "", "",
317                                       NULL,
318                                       G_PARAM_READABLE |
319                                       G_PARAM_STATIC_STRINGS));
320
321         /**
322          * NMDeviceBond:carrier:
323          *
324          * Whether the device has carrier.
325          **/
326         g_object_class_install_property
327                 (object_class, PROP_CARRIER,
328                  g_param_spec_boolean (NM_DEVICE_BOND_CARRIER, "", "",
329                                        FALSE,
330                                        G_PARAM_READABLE |
331                                        G_PARAM_STATIC_STRINGS));
332
333         /**
334          * NMDeviceBond:slaves:
335          *
336          * The devices (#NMDevice) slaved to the bond device.
337          *
338          * Since: 0.9.8
339          **/
340         g_object_class_install_property
341                 (object_class, PROP_SLAVES,
342                  g_param_spec_boxed (NM_DEVICE_BOND_SLAVES, "", "",
343                                      NM_TYPE_OBJECT_ARRAY,
344                                      G_PARAM_READABLE |
345                                      G_PARAM_STATIC_STRINGS));
346 }