device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-device-tun.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 2015 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24 #include <arpa/inet.h>
25
26 #include "nm-setting-connection.h"
27 #include "nm-setting-tun.h"
28 #include "nm-utils.h"
29
30 #include "nm-device-tun.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33
34 G_DEFINE_TYPE (NMDeviceTun, nm_device_tun, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_TUN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_TUN, NMDeviceTunPrivate))
37
38 typedef struct {
39         char *hw_address;
40         char *mode;
41         gint64 owner;
42         gint64 group;
43         gboolean no_pi;
44         gboolean vnet_hdr;
45         gboolean multi_queue;
46 } NMDeviceTunPrivate;
47
48 enum {
49         PROP_0,
50         PROP_HW_ADDRESS,
51         PROP_MODE,
52         PROP_OWNER,
53         PROP_GROUP,
54         PROP_NO_PI,
55         PROP_VNET_HDR,
56         PROP_MULTI_QUEUE,
57
58         LAST_PROP
59 };
60
61 /**
62  * nm_device_tun_get_hw_address:
63  * @device: a #NMDeviceTun
64  *
65  * Gets the hardware (MAC) address of the #NMDeviceTun
66  *
67  * Returns: the hardware address. This is the internal string used by the
68  * device, and must not be modified.
69  *
70  * Since: 1.2
71  **/
72 const char *
73 nm_device_tun_get_hw_address (NMDeviceTun *device)
74 {
75         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), NULL);
76
77         return NM_DEVICE_TUN_GET_PRIVATE (device)->hw_address;
78 }
79
80 /**
81  * nm_device_tun_get_mode:
82  * @device: a #NMDeviceTun
83  *
84  * Returns the TUN/TAP mode for the device.
85  *
86  * Returns: 'tun' or 'tap'
87  *
88  * Since: 1.2
89  **/
90 const char *
91 nm_device_tun_get_mode (NMDeviceTun *device)
92 {
93         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), NULL);
94
95         return NM_DEVICE_TUN_GET_PRIVATE (device)->mode;
96 }
97
98 /**
99  * nm_device_tun_get_owner:
100  * @device: a #NMDeviceTun
101  *
102  * Gets the tunnel owner.
103  *
104  * Returns: the uid of the tunnel owner, or -1 if it has no owner.
105  *
106  * Since: 1.2
107  **/
108 gint64
109 nm_device_tun_get_owner (NMDeviceTun *device)
110 {
111         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), -1);
112
113         return NM_DEVICE_TUN_GET_PRIVATE (device)->owner;
114 }
115
116 /**
117  * nm_device_tun_get_group:
118  * @device: a #NMDeviceTun
119  *
120  * Gets the tunnel group.
121  *
122  * Returns: the gid of the tunnel group, or -1 if it has no owner.
123  *
124  * Since: 1.2
125  **/
126 gint64
127 nm_device_tun_get_group (NMDeviceTun *device)
128 {
129         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), -1);
130
131         return NM_DEVICE_TUN_GET_PRIVATE (device)->group;
132 }
133
134 /**
135  * nm_device_tun_get_pi:
136  * @device: a #NMDeviceTun
137  *
138  * Returns whether the #NMDeviceTun has the IFF_NO_PI flag.
139  *
140  * Returns: %TRUE if the device has the flag, %FALSE otherwise
141  *
142  * Since: 1.2
143  **/
144 gboolean
145 nm_device_tun_get_no_pi (NMDeviceTun *device)
146 {
147         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
148
149         return NM_DEVICE_TUN_GET_PRIVATE (device)->no_pi;
150 }
151
152 /**
153  * nm_device_tun_get_vnet_hdr:
154  * @device: a #NMDeviceTun
155  *
156  * Returns whether the #NMDeviceTun has the IFF_VNET_HDR flag.
157  *
158  * Returns: %TRUE if the device has the flag, %FALSE otherwise
159  *
160  * Since: 1.2
161  **/
162 gboolean
163 nm_device_tun_get_vnet_hdr (NMDeviceTun *device)
164 {
165         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
166
167         return NM_DEVICE_TUN_GET_PRIVATE (device)->vnet_hdr;
168 }
169
170 /**
171  * nm_device_tun_get_multi_queue:
172  * @device: a #NMDeviceTun
173  *
174  * Returns whether the #NMDeviceTun has the IFF_MULTI_QUEUE flag.
175  *
176  * Returns: %TRUE if the device doesn't have the flag, %FALSE otherwise
177  *
178  * Since: 1.2
179  **/
180 gboolean
181 nm_device_tun_get_multi_queue (NMDeviceTun *device)
182 {
183         g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
184
185         return NM_DEVICE_TUN_GET_PRIVATE (device)->multi_queue;
186 }
187
188 static int
189 tun_mode_from_string (const char *string)
190 {
191         if (!g_strcmp0 (string, "tap"))
192                 return NM_SETTING_TUN_MODE_TAP;
193         else
194                 return NM_SETTING_TUN_MODE_TUN;
195 }
196
197 static gboolean
198 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
199 {
200         NMDeviceTunPrivate *priv = NM_DEVICE_TUN_GET_PRIVATE (device);
201         NMSettingTunMode mode;
202         NMSettingTun *s_tun;
203
204         if (!NM_DEVICE_CLASS (nm_device_tun_parent_class)->connection_compatible (device, connection, error))
205                 return FALSE;
206
207         if (!nm_connection_is_type (connection, NM_SETTING_TUN_SETTING_NAME)) {
208                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
209                                      _("The connection was not a tun connection."));
210                 return FALSE;
211         }
212
213         s_tun = nm_connection_get_setting_tun (connection);
214
215         mode = tun_mode_from_string (priv->mode);
216         if (s_tun && mode != nm_setting_tun_get_mode (s_tun)) {
217                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
218                                      _("The mode of the device and the connection didn't match"));
219                 return FALSE;
220         }
221
222         return TRUE;
223 }
224
225 static GType
226 get_setting_type (NMDevice *device)
227 {
228         return NM_TYPE_SETTING_TUN;
229 }
230
231 static const char *
232 get_hw_address (NMDevice *device)
233 {
234         return nm_device_tun_get_hw_address (NM_DEVICE_TUN (device));
235 }
236
237 /***********************************************************/
238
239 static void
240 nm_device_tun_init (NMDeviceTun *device)
241 {
242         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_TUN);
243 }
244
245 static void
246 init_dbus (NMObject *object)
247 {
248         NMDeviceTunPrivate *priv = NM_DEVICE_TUN_GET_PRIVATE (object);
249         const NMPropertiesInfo property_info[] = {
250                 { NM_DEVICE_TUN_HW_ADDRESS,  &priv->hw_address },
251                 { NM_DEVICE_TUN_MODE,        &priv->mode },
252                 { NM_DEVICE_TUN_OWNER,       &priv->owner },
253                 { NM_DEVICE_TUN_GROUP,       &priv->group },
254                 { NM_DEVICE_TUN_NO_PI,       &priv->no_pi },
255                 { NM_DEVICE_TUN_VNET_HDR,    &priv->vnet_hdr },
256                 { NM_DEVICE_TUN_MULTI_QUEUE, &priv->multi_queue },
257                 { NULL },
258         };
259
260         NM_OBJECT_CLASS (nm_device_tun_parent_class)->init_dbus (object);
261
262         _nm_object_register_properties (object,
263                                         NM_DBUS_INTERFACE_DEVICE_TUN,
264                                         property_info);
265 }
266
267 static void
268 finalize (GObject *object)
269 {
270         NMDeviceTunPrivate *priv = NM_DEVICE_TUN_GET_PRIVATE (object);
271
272         g_free (priv->mode);
273
274         G_OBJECT_CLASS (nm_device_tun_parent_class)->finalize (object);
275 }
276
277 static void
278 get_property (GObject *object,
279               guint prop_id,
280               GValue *value,
281               GParamSpec *pspec)
282 {
283         NMDeviceTun *device = NM_DEVICE_TUN (object);
284
285         switch (prop_id) {
286         case PROP_HW_ADDRESS:
287                 g_value_set_string (value, nm_device_tun_get_hw_address (device));
288                 break;
289         case PROP_MODE:
290                 g_value_set_string (value, nm_device_tun_get_mode (device));
291                 break;
292         case PROP_OWNER:
293                 g_value_set_int64 (value, nm_device_tun_get_owner (device));
294                 break;
295         case PROP_GROUP:
296                 g_value_set_int64 (value, nm_device_tun_get_group (device));
297                 break;
298         case PROP_NO_PI:
299                 g_value_set_boolean (value, nm_device_tun_get_no_pi (device));
300                 break;
301         case PROP_VNET_HDR:
302                 g_value_set_boolean (value, nm_device_tun_get_vnet_hdr (device));
303                 break;
304         case PROP_MULTI_QUEUE:
305                 g_value_set_boolean (value, nm_device_tun_get_multi_queue (device));
306                 break;
307
308         default:
309                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310                 break;
311         }
312 }
313
314 static void
315 nm_device_tun_class_init (NMDeviceTunClass *gre_class)
316 {
317         GObjectClass *object_class = G_OBJECT_CLASS (gre_class);
318         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (gre_class);
319         NMDeviceClass *device_class = NM_DEVICE_CLASS (gre_class);
320
321         g_type_class_add_private (gre_class, sizeof (NMDeviceTunPrivate));
322
323         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_TUN);
324
325         /* virtual methods */
326         object_class->finalize = finalize;
327         object_class->get_property = get_property;
328
329         nm_object_class->init_dbus = init_dbus;
330
331         device_class->connection_compatible = connection_compatible;
332         device_class->get_setting_type = get_setting_type;
333         device_class->get_hw_address = get_hw_address;
334
335         /* properties */
336
337         /**
338          * NMDeviceTun:hw-address:
339          *
340          * The hardware (MAC) address of the device.
341          *
342          * Since: 1.2
343          **/
344         g_object_class_install_property
345                 (object_class, PROP_HW_ADDRESS,
346                  g_param_spec_string (NM_DEVICE_TUN_HW_ADDRESS, "", "",
347                                       NULL,
348                                       G_PARAM_READABLE |
349                                       G_PARAM_STATIC_STRINGS));
350
351         /**
352          * NMDeviceTun:mode:
353          *
354          * The tunnel mode, either "tun" or "tap".
355          *
356          * Since: 1.2
357          **/
358         g_object_class_install_property
359                 (object_class, PROP_MODE,
360                  g_param_spec_string (NM_DEVICE_TUN_MODE, "", "",
361                                       NULL,
362                                       G_PARAM_READABLE |
363                                       G_PARAM_STATIC_STRINGS));
364
365         /**
366          * NMDeviceTun:owner:
367          *
368          * The uid of the tunnel owner, or -1 if it has no owner.
369          *
370          * Since: 1.2
371          **/
372         g_object_class_install_property
373                 (object_class, PROP_OWNER,
374                  g_param_spec_int64 (NM_DEVICE_TUN_OWNER, "", "",
375                                      -1, G_MAXUINT32, -1,
376                                      G_PARAM_READABLE |
377                                      G_PARAM_STATIC_STRINGS));
378
379         /**
380          * NMDeviceTun:group:
381          *
382          * The gid of the tunnel group, or -1 if it has no owner.
383          *
384          * Since: 1.2
385          **/
386         g_object_class_install_property
387                 (object_class, PROP_GROUP,
388                  g_param_spec_int64 (NM_DEVICE_TUN_GROUP, "", "",
389                                      -1, G_MAXUINT32, -1,
390                                      G_PARAM_READABLE |
391                                      G_PARAM_STATIC_STRINGS));
392
393         /**
394          * NMDeviceTun:no-pi:
395          *
396          * The tunnel's "TUN_NO_PI" flag; true if no protocol info is
397          * prepended to the tunnel packets.
398          *
399          * Since: 1.2
400          **/
401         g_object_class_install_property
402                 (object_class, PROP_NO_PI,
403                  g_param_spec_boolean (NM_DEVICE_TUN_NO_PI, "", "",
404                                        FALSE,
405                                        G_PARAM_READABLE |
406                                        G_PARAM_STATIC_STRINGS));
407
408         /**
409          * NMDeviceTun:vnet-hdr:
410          *
411          * The tunnel's "TUN_VNET_HDR" flag; true if the tunnel packets
412          * include a virtio network header.
413          *
414          * Since: 1.2
415          **/
416         g_object_class_install_property
417                 (object_class, PROP_VNET_HDR,
418                  g_param_spec_boolean (NM_DEVICE_TUN_VNET_HDR, "", "",
419                                        FALSE,
420                                        G_PARAM_READABLE |
421                                        G_PARAM_STATIC_STRINGS));
422
423         /**
424          * NMDeviceTun:multi-queue:
425          *
426          * The tunnel's "TUN_TAP_MQ" flag; true if callers can connect to
427          * the tap device multiple times, for multiple send/receive
428          * queues.
429          *
430          * Since: 1.2
431          **/
432         g_object_class_install_property
433                 (object_class, PROP_MULTI_QUEUE,
434                  g_param_spec_boolean (NM_DEVICE_TUN_MULTI_QUEUE, "", "",
435                                        FALSE,
436                                        G_PARAM_READABLE |
437                                        G_PARAM_STATIC_STRINGS));
438 }