device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-infiniband.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 2011 - 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24 #include <linux/if_infiniband.h>
25 #include <netinet/ether.h>
26
27 #include "nm-setting-connection.h"
28 #include "nm-setting-infiniband.h"
29 #include "nm-utils.h"
30
31 #include "nm-device-infiniband.h"
32 #include "nm-device-private.h"
33 #include "nm-object-private.h"
34
35 G_DEFINE_TYPE (NMDeviceInfiniband, nm_device_infiniband, NM_TYPE_DEVICE)
36
37 #define NM_DEVICE_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_INFINIBAND, NMDeviceInfinibandPrivate))
38
39 typedef struct {
40         DBusGProxy *proxy;
41
42         char *hw_address;
43         gboolean carrier;
44 } NMDeviceInfinibandPrivate;
45
46 enum {
47         PROP_0,
48         PROP_HW_ADDRESS,
49         PROP_CARRIER,
50
51         LAST_PROP
52 };
53
54 /**
55  * nm_device_infiniband_error_quark:
56  *
57  * Registers an error quark for #NMDeviceInfiniband if necessary.
58  *
59  * Returns: the error quark used for #NMDeviceInfiniband errors.
60  **/
61 GQuark
62 nm_device_infiniband_error_quark (void)
63 {
64         static GQuark quark = 0;
65
66         if (G_UNLIKELY (quark == 0))
67                 quark = g_quark_from_static_string ("nm-device-infiniband-error-quark");
68         return quark;
69 }
70
71 /**
72  * nm_device_infiniband_new:
73  * @connection: the #DBusGConnection
74  * @path: the DBus object path of the device
75  *
76  * Creates a new #NMDeviceInfiniband.
77  *
78  * Returns: (transfer full): a new device
79  **/
80 GObject *
81 nm_device_infiniband_new (DBusGConnection *connection, const char *path)
82 {
83         GObject *device;
84
85         g_return_val_if_fail (connection != NULL, NULL);
86         g_return_val_if_fail (path != NULL, NULL);
87
88         device = g_object_new (NM_TYPE_DEVICE_INFINIBAND,
89                                NM_OBJECT_DBUS_CONNECTION, connection,
90                                NM_OBJECT_DBUS_PATH, path,
91                                NULL);
92         _nm_object_ensure_inited (NM_OBJECT (device));
93         return device;
94 }
95
96 /**
97  * nm_device_infiniband_get_hw_address:
98  * @device: a #NMDeviceInfiniband
99  *
100  * Gets the hardware (MAC) address of the #NMDeviceInfiniband
101  *
102  * Returns: the hardware address. This is the internal string used by the
103  * device, and must not be modified.
104  **/
105 const char *
106 nm_device_infiniband_get_hw_address (NMDeviceInfiniband *device)
107 {
108         g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), NULL);
109
110         _nm_object_ensure_inited (NM_OBJECT (device));
111         return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->hw_address;
112 }
113
114 /**
115  * nm_device_infiniband_get_carrier:
116  * @device: a #NMDeviceInfiniband
117  *
118  * Whether the device has carrier.
119  *
120  * Returns: %TRUE if the device has carrier
121  **/
122 gboolean
123 nm_device_infiniband_get_carrier (NMDeviceInfiniband *device)
124 {
125         g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (device), FALSE);
126
127         _nm_object_ensure_inited (NM_OBJECT (device));
128         return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->carrier;
129 }
130
131 static gboolean
132 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
133 {
134         NMSettingConnection *s_con;
135         NMSettingInfiniband *s_infiniband;
136         const char *ctype, *hwaddr_str;
137         const GByteArray *mac;
138         guint8 *hwaddr, hwaddr_buf[INFINIBAND_ALEN];
139
140         s_con = nm_connection_get_setting_connection (connection);
141         g_assert (s_con);
142
143         ctype = nm_setting_connection_get_connection_type (s_con);
144         if (strcmp (ctype, NM_SETTING_INFINIBAND_SETTING_NAME) != 0) {
145                 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_NOT_INFINIBAND_CONNECTION,
146                              "The connection was not a InfiniBand connection.");
147                 return FALSE;
148         }
149
150         s_infiniband = nm_connection_get_setting_infiniband (connection);
151         if (!s_infiniband) {
152                 g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_INVALID_INFINIBAND_CONNECTION,
153                              "The connection was not a valid InfiniBand connection.");
154                 return FALSE;
155         }
156
157         hwaddr_str = nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
158         if (hwaddr_str) {
159                 hwaddr = nm_utils_hwaddr_aton (hwaddr_str, ARPHRD_INFINIBAND, hwaddr_buf);
160                 if (!hwaddr) {
161                         g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_INVALID_DEVICE_MAC,
162                                      "Invalid device MAC address.");
163                         return FALSE;
164                 }
165                 mac = nm_setting_infiniband_get_mac_address (s_infiniband);
166
167                 /* We only match against the last 8 bytes */
168                 if (mac && hwaddr && memcmp (mac->data + INFINIBAND_ALEN - 8, hwaddr + INFINIBAND_ALEN - 8, 8)) {
169                         g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_MAC_MISMATCH,
170                                      "The MACs of the device and the connection didn't match.");
171                         return FALSE;
172                 }
173         }
174
175         return NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_compatible (device, connection, error);
176 }
177
178 static GType
179 get_setting_type (NMDevice *device)
180 {
181         return NM_TYPE_SETTING_INFINIBAND;
182 }
183
184 static const char *
185 get_hw_address (NMDevice *device)
186 {
187         return nm_device_infiniband_get_hw_address (NM_DEVICE_INFINIBAND (device));
188 }
189
190 /***********************************************************/
191
192 static void
193 nm_device_infiniband_init (NMDeviceInfiniband *device)
194 {
195         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_INFINIBAND);
196 }
197
198 static void
199 register_properties (NMDeviceInfiniband *device)
200 {
201         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device);
202         const NMPropertiesInfo property_info[] = {
203                 { NM_DEVICE_INFINIBAND_HW_ADDRESS, &priv->hw_address },
204                 { NM_DEVICE_INFINIBAND_CARRIER,    &priv->carrier },
205                 { NULL },
206         };
207
208         _nm_object_register_properties (NM_OBJECT (device),
209                                         priv->proxy,
210                                         property_info);
211 }
212
213 static void
214 constructed (GObject *object)
215 {
216         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
217
218         G_OBJECT_CLASS (nm_device_infiniband_parent_class)->constructed (object);
219
220         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_INFINIBAND);
221         register_properties (NM_DEVICE_INFINIBAND (object));
222 }
223
224 static void
225 dispose (GObject *object)
226 {
227         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
228
229         g_clear_object (&priv->proxy);
230
231         G_OBJECT_CLASS (nm_device_infiniband_parent_class)->dispose (object);
232 }
233
234 static void
235 finalize (GObject *object)
236 {
237         NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object);
238
239         g_free (priv->hw_address);
240
241         G_OBJECT_CLASS (nm_device_infiniband_parent_class)->finalize (object);
242 }
243
244 static void
245 get_property (GObject *object,
246               guint prop_id,
247               GValue *value,
248               GParamSpec *pspec)
249 {
250         NMDeviceInfiniband *device = NM_DEVICE_INFINIBAND (object);
251
252         _nm_object_ensure_inited (NM_OBJECT (object));
253
254         switch (prop_id) {
255         case PROP_HW_ADDRESS:
256                 g_value_set_string (value, nm_device_infiniband_get_hw_address (device));
257                 break;
258         case PROP_CARRIER:
259                 g_value_set_boolean (value, nm_device_infiniband_get_carrier (device));
260                 break;
261         default:
262                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263                 break;
264         }
265 }
266
267 static void
268 nm_device_infiniband_class_init (NMDeviceInfinibandClass *ib_class)
269 {
270         GObjectClass *object_class = G_OBJECT_CLASS (ib_class);
271         NMDeviceClass *device_class = NM_DEVICE_CLASS (ib_class);
272
273         g_type_class_add_private (ib_class, sizeof (NMDeviceInfinibandPrivate));
274
275         /* virtual methods */
276         object_class->constructed = constructed;
277         object_class->dispose = dispose;
278         object_class->finalize = finalize;
279         object_class->get_property = get_property;
280         device_class->connection_compatible = connection_compatible;
281         device_class->get_setting_type = get_setting_type;
282         device_class->get_hw_address = get_hw_address;
283
284         /* properties */
285
286         /**
287          * NMDeviceInfiniband:hw-address:
288          *
289          * The hardware (MAC) address of the device.
290          **/
291         g_object_class_install_property
292                 (object_class, PROP_HW_ADDRESS,
293                  g_param_spec_string (NM_DEVICE_INFINIBAND_HW_ADDRESS, "", "",
294                                       NULL,
295                                       G_PARAM_READABLE |
296                                       G_PARAM_STATIC_STRINGS));
297
298         /**
299          * NMDeviceInfiniband:carrier:
300          *
301          * Whether the device has carrier.
302          **/
303         g_object_class_install_property
304                 (object_class, PROP_CARRIER,
305                  g_param_spec_boolean (NM_DEVICE_INFINIBAND_CARRIER, "", "",
306                                        FALSE,
307                                        G_PARAM_READABLE |
308                                        G_PARAM_STATIC_STRINGS));
309
310 }