device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-device-ip-tunnel.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
25 #include "nm-setting-connection.h"
26 #include "nm-setting-ip-tunnel.h"
27 #include "nm-utils.h"
28
29 #include "nm-device-ip-tunnel.h"
30 #include "nm-device-private.h"
31 #include "nm-object-private.h"
32 #include "nm-core-internal.h"
33
34 G_DEFINE_TYPE (NMDeviceIPTunnel, nm_device_ip_tunnel, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_IP_TUNNEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_IP_TUNNEL, NMDeviceIPTunnelPrivate))
37
38 typedef struct {
39         NMIPTunnelMode mode;
40         NMDevice *parent;
41         char *local;
42         char *remote;
43         guint8 ttl;
44         guint8 tos;
45         gboolean path_mtu_discovery;
46         char *input_key;
47         char *output_key;
48         guint8 encap_limit;
49         guint32 flow_label;
50 } NMDeviceIPTunnelPrivate;
51
52 enum {
53         PROP_0,
54         PROP_MODE,
55         PROP_PARENT,
56         PROP_LOCAL,
57         PROP_REMOTE,
58         PROP_TTL,
59         PROP_TOS,
60         PROP_PATH_MTU_DISCOVERY,
61         PROP_INPUT_KEY,
62         PROP_OUTPUT_KEY,
63         PROP_ENCAPSULATION_LIMIT,
64         PROP_FLOW_LABEL,
65
66         LAST_PROP
67 };
68
69 /**
70  * nm_device_ip_tunnel_get_mode:
71  * @device: a #NMDeviceIPTunnel
72  *
73  * Returns: the tunneling mode
74  *
75  * Since: 1.2
76  **/
77 NMIPTunnelMode
78 nm_device_ip_tunnel_get_mode (NMDeviceIPTunnel *device)
79 {
80         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), 0);
81
82         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->mode;
83 }
84
85 /**
86  * nm_device_ip_tunnel_get_parent:
87  * @device: a #NMDeviceIPTunnel
88  *
89  * Returns: (transfer none): the device's parent device
90  *
91  * Since: 1.2
92  **/
93 NMDevice *
94 nm_device_ip_tunnel_get_parent (NMDeviceIPTunnel *device)
95 {
96         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), NULL);
97
98         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->parent;
99 }
100
101 /**
102  * nm_device_ip_tunnel_get_local:
103  * @device: a #NMDeviceIPTunnel
104  *
105  * Returns: the local endpoint of the tunnel
106  *
107  * Since: 1.2
108  **/
109 const char *
110 nm_device_ip_tunnel_get_local (NMDeviceIPTunnel *device)
111 {
112         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), NULL);
113
114         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->local;
115 }
116
117 /**
118  * nm_device_ip_tunnel_get_remote:
119  * @device: a #NMDeviceIPTunnel
120  *
121  * Returns: the remote endpoint of the tunnel
122  *
123  * Since: 1.2
124  **/
125 const char *
126 nm_device_ip_tunnel_get_remote (NMDeviceIPTunnel *device)
127 {
128         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), NULL);
129
130         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->remote;
131 }
132
133 /**
134  * nm_device_ip_tunnel_get_ttl:
135  * @device: a #NMDeviceIPTunnel
136  *
137  * Returns: the TTL assigned to tunneled packets
138  *
139  * Since: 1.2
140  **/
141 guint8
142 nm_device_ip_tunnel_get_ttl (NMDeviceIPTunnel *device)
143 {
144         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), 0);
145
146         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->ttl;
147 }
148
149 /**
150  * nm_device_ip_tunnel_get_tos:
151  * @device: a #NMDeviceIPTunnel
152  *
153  * Returns: type of service (IPv4) or traffic class (IPv6) assigned
154  * to tunneled packets.
155  *
156  * Since: 1.2
157  **/
158 guint8
159 nm_device_ip_tunnel_get_tos (NMDeviceIPTunnel *device)
160 {
161         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), 0);
162
163         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->tos;
164 }
165
166 /**
167  * nm_device_ip_tunnel_get_path_mtu_discovery:
168  * @device: a #NMDeviceIPTunnel
169  *
170  * Returns: whether path MTU discovery is enabled
171  *
172  * Since: 1.2
173  **/
174 gboolean
175 nm_device_ip_tunnel_get_path_mtu_discovery (NMDeviceIPTunnel *device)
176 {
177         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), TRUE);
178
179         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->path_mtu_discovery;
180 }
181
182 /**
183  * nm_device_ip_tunnel_get_input_key:
184  * @device: a #NMDeviceIPTunnel
185  *
186  * Returns: the key used for incoming packets
187  *
188  * Since: 1.2
189  **/
190 const char *
191 nm_device_ip_tunnel_get_input_key (NMDeviceIPTunnel *device)
192 {
193         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), NULL);
194
195         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->input_key;
196 }
197
198 /**
199  * nm_device_ip_tunnel_get_output_key:
200  * @device: a #NMDeviceIPTunnel
201  *
202  * Returns: the key used for outgoing packets
203  *
204  * Since: 1.2
205  **/
206 const char *
207 nm_device_ip_tunnel_get_output_key (NMDeviceIPTunnel *device)
208 {
209         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), NULL);
210
211         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->output_key;
212 }
213
214 /**
215  * nm_device_ip_tunnel_get_encapsulation_limit:
216  * @device: a #NMDeviceIPTunnel
217  *
218  * Returns: the maximum permitted encapsulation level
219  *
220  * Since: 1.2
221  **/
222 guint8
223 nm_device_ip_tunnel_get_encapsulation_limit (NMDeviceIPTunnel *device)
224 {
225         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), 0);
226
227         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->encap_limit;
228 }
229
230 /**
231  * nm_device_ip_tunnel_get_flow_label:
232  * @device: a #NMDeviceIPTunnel
233  *
234  * Returns: the flow label assigned to tunnel packets
235  *
236  * Since: 1.2
237  **/
238 guint
239 nm_device_ip_tunnel_get_flow_label (NMDeviceIPTunnel *device)
240 {
241         g_return_val_if_fail (NM_IS_DEVICE_IP_TUNNEL (device), 0);
242
243         return NM_DEVICE_IP_TUNNEL_GET_PRIVATE (device)->flow_label;
244 }
245
246 static gboolean
247 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
248 {
249         if (!NM_DEVICE_CLASS (nm_device_ip_tunnel_parent_class)->connection_compatible (device, connection, error))
250                 return FALSE;
251
252         if (!nm_connection_is_type (connection, NM_SETTING_IP_TUNNEL_SETTING_NAME)) {
253                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
254                                      _("The connection was not an IP tunnel connection."));
255                 return FALSE;
256         }
257
258         return TRUE;
259 }
260
261 static GType
262 get_setting_type (NMDevice *device)
263 {
264         return NM_TYPE_SETTING_IP_TUNNEL;
265 }
266
267 /***********************************************************/
268
269 static void
270 nm_device_ip_tunnel_init (NMDeviceIPTunnel *device)
271 {
272         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_IP_TUNNEL);
273 }
274
275 static void
276 init_dbus (NMObject *object)
277 {
278         NMDeviceIPTunnelPrivate *priv = NM_DEVICE_IP_TUNNEL_GET_PRIVATE (object);
279         const NMPropertiesInfo property_info[] = {
280                 { NM_DEVICE_IP_TUNNEL_PARENT,               &priv->parent, NULL, NM_TYPE_DEVICE },
281                 { NM_DEVICE_IP_TUNNEL_MODE,                 &priv->mode },
282                 { NM_DEVICE_IP_TUNNEL_LOCAL,                &priv->local },
283                 { NM_DEVICE_IP_TUNNEL_REMOTE,               &priv->remote },
284                 { NM_DEVICE_IP_TUNNEL_TTL,                  &priv->ttl },
285                 { NM_DEVICE_IP_TUNNEL_TOS,                  &priv->tos },
286                 { NM_DEVICE_IP_TUNNEL_PATH_MTU_DISCOVERY,   &priv->path_mtu_discovery },
287                 { NM_DEVICE_IP_TUNNEL_INPUT_KEY,            &priv->input_key },
288                 { NM_DEVICE_IP_TUNNEL_OUTPUT_KEY,           &priv->output_key },
289                 { NM_DEVICE_IP_TUNNEL_ENCAPSULATION_LIMIT,  &priv->encap_limit },
290                 { NM_DEVICE_IP_TUNNEL_FLOW_LABEL,           &priv->flow_label },
291                 { NULL },
292         };
293
294         NM_OBJECT_CLASS (nm_device_ip_tunnel_parent_class)->init_dbus (object);
295
296         _nm_object_register_properties (object,
297                                         NM_DBUS_INTERFACE_DEVICE_IP_TUNNEL,
298                                         property_info);
299 }
300
301 static void
302 finalize (GObject *object)
303 {
304         NMDeviceIPTunnelPrivate *priv = NM_DEVICE_IP_TUNNEL_GET_PRIVATE (object);
305
306         g_free (priv->local);
307         g_free (priv->remote);
308         g_free (priv->input_key);
309         g_free (priv->output_key);
310         g_clear_object (&priv->parent);
311
312         G_OBJECT_CLASS (nm_device_ip_tunnel_parent_class)->finalize (object);
313 }
314
315 static void
316 get_property (GObject *object,
317               guint prop_id,
318               GValue *value,
319               GParamSpec *pspec)
320 {
321         NMDeviceIPTunnel *device = NM_DEVICE_IP_TUNNEL (object);
322
323         switch (prop_id) {
324         case PROP_PARENT:
325                 g_value_set_object (value, nm_device_ip_tunnel_get_parent (device));
326                 break;
327         case PROP_MODE:
328                 g_value_set_uint (value, nm_device_ip_tunnel_get_mode (device));
329                 break;
330         case PROP_LOCAL:
331                 g_value_set_string (value, nm_device_ip_tunnel_get_local (device));
332                 break;
333         case PROP_REMOTE:
334                 g_value_set_string (value, nm_device_ip_tunnel_get_remote (device));
335                 break;
336         case PROP_TTL:
337                 g_value_set_uint (value, nm_device_ip_tunnel_get_ttl (device));
338                 break;
339         case PROP_TOS:
340                 g_value_set_uint (value, nm_device_ip_tunnel_get_tos (device));
341                 break;
342         case PROP_PATH_MTU_DISCOVERY:
343                 g_value_set_boolean (value, nm_device_ip_tunnel_get_path_mtu_discovery (device));
344                 break;
345         case PROP_INPUT_KEY:
346                 g_value_set_string (value, nm_device_ip_tunnel_get_input_key (device));
347                 break;
348         case PROP_OUTPUT_KEY:
349                 g_value_set_string (value, nm_device_ip_tunnel_get_output_key (device));
350                 break;
351         case PROP_ENCAPSULATION_LIMIT:
352                 g_value_set_uint (value, nm_device_ip_tunnel_get_encapsulation_limit (device));
353                 break;
354         case PROP_FLOW_LABEL:
355                 g_value_set_uint (value, nm_device_ip_tunnel_get_flow_label (device));
356                 break;
357         default:
358                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359                 break;
360         }
361 }
362
363 static void
364 nm_device_ip_tunnel_class_init (NMDeviceIPTunnelClass *bond_class)
365 {
366         GObjectClass *object_class = G_OBJECT_CLASS (bond_class);
367         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (bond_class);
368         NMDeviceClass *device_class = NM_DEVICE_CLASS (bond_class);
369
370         g_type_class_add_private (bond_class, sizeof (NMDeviceIPTunnelPrivate));
371
372         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_IP_TUNNEL);
373
374         /* virtual methods */
375         object_class->finalize = finalize;
376         object_class->get_property = get_property;
377
378         nm_object_class->init_dbus = init_dbus;
379
380         device_class->connection_compatible = connection_compatible;
381         device_class->get_setting_type = get_setting_type;
382
383         /* properties */
384
385         /**
386          * NMDeviceIPTunnel:mode:
387          *
388          * The tunneling mode of the device.
389          *
390          * Since: 1.2
391          **/
392         g_object_class_install_property
393                 (object_class, PROP_MODE,
394                  g_param_spec_uint (NM_DEVICE_IP_TUNNEL_MODE, "", "",
395                                     0, G_MAXUINT, 0,
396                                     G_PARAM_READABLE |
397                                     G_PARAM_STATIC_STRINGS));
398
399         /**
400          * NMDeviceIPTunnel:parent:
401          *
402          * The devices's parent device.
403          *
404          * Since: 1.2
405          **/
406         g_object_class_install_property
407             (object_class, PROP_PARENT,
408              g_param_spec_object (NM_DEVICE_IP_TUNNEL_PARENT, "", "",
409                                   NM_TYPE_DEVICE,
410                                   G_PARAM_READABLE |
411                                   G_PARAM_STATIC_STRINGS));
412
413         /**
414          * NMDeviceIPTunnel:local:
415          *
416          * The local endpoint of the tunnel.
417          *
418          * Since: 1.2
419          **/
420         g_object_class_install_property
421                 (object_class, PROP_LOCAL,
422                  g_param_spec_string (NM_DEVICE_IP_TUNNEL_LOCAL, "", "",
423                                       NULL,
424                                       G_PARAM_READABLE |
425                                       G_PARAM_STATIC_STRINGS));
426
427         /**
428          * NMDeviceIPTunnel:remote:
429          *
430          * The remote endpoint of the tunnel.
431          *
432          * Since: 1.2
433          **/
434         g_object_class_install_property
435                 (object_class, PROP_REMOTE,
436                  g_param_spec_string (NM_DEVICE_IP_TUNNEL_REMOTE, "", "",
437                                       NULL,
438                                       G_PARAM_READABLE |
439                                       G_PARAM_STATIC_STRINGS));
440
441         /**
442          * NMDeviceIPTunnel:ttl:
443          *
444          * The TTL assigned to tunneled packets. 0 is a special value
445          *  meaning that packets inherit the TTL value
446          *
447          * Since: 1.2
448          **/
449         g_object_class_install_property
450                 (object_class, PROP_TTL,
451                  g_param_spec_uchar (NM_DEVICE_IP_TUNNEL_TTL, "", "",
452                                      0, 255, 0,
453                                      G_PARAM_READABLE |
454                                      G_PARAM_STATIC_STRINGS));
455
456         /**
457          * NMDeviceIPTunnel:tos:
458          *
459          * The type of service (IPv4) or traffic class (IPv6) assigned to
460          * tunneled packets.
461          *
462          * Since: 1.2
463          **/
464         g_object_class_install_property
465                 (object_class, PROP_TOS,
466                  g_param_spec_uchar (NM_DEVICE_IP_TUNNEL_TOS, "", "",
467                                      0, 255, 0,
468                                      G_PARAM_READABLE |
469                                      G_PARAM_STATIC_STRINGS));
470
471         /**
472          * NMDeviceIPTunnel:path-mtu-discovery:
473          *
474          * Whether path MTU discovery is enabled on this tunnel.
475          *
476          * Since: 1.2
477          **/
478         g_object_class_install_property
479                 (object_class, PROP_PATH_MTU_DISCOVERY,
480                  g_param_spec_boolean (NM_DEVICE_IP_TUNNEL_PATH_MTU_DISCOVERY, "", "",
481                                        FALSE,
482                                        G_PARAM_READABLE |
483                                        G_PARAM_STATIC_STRINGS));
484
485         /**
486          * NMDeviceIPTunnel:input-key:
487          *
488          * The key used for tunneled input packets, if applicable.
489          *
490          * Since: 1.2
491          **/
492         g_object_class_install_property
493                 (object_class, PROP_INPUT_KEY,
494                  g_param_spec_string (NM_DEVICE_IP_TUNNEL_INPUT_KEY, "", "",
495                                       NULL,
496                                       G_PARAM_READABLE |
497                                       G_PARAM_STATIC_STRINGS));
498
499         /**
500          * NMDeviceIPTunnel:output-key:
501          *
502          * The key used for tunneled output packets, if applicable.
503          *
504          * Since: 1.2
505          **/
506         g_object_class_install_property
507                 (object_class, PROP_OUTPUT_KEY,
508                  g_param_spec_string (NM_DEVICE_IP_TUNNEL_OUTPUT_KEY, "", "",
509                                       NULL,
510                                       G_PARAM_READABLE |
511                                       G_PARAM_STATIC_STRINGS));
512
513         /**
514          * NMDeviceIPTunnel:encapsulation-limit:
515          *
516          * How many additional levels of encapsulation are permitted to
517          * be prepended to packets. This property applies only to IPv6
518          * tunnels.
519          *
520          * Since: 1.2
521          **/
522         g_object_class_install_property
523                 (object_class, PROP_ENCAPSULATION_LIMIT,
524                  g_param_spec_uchar (NM_DEVICE_IP_TUNNEL_ENCAPSULATION_LIMIT, "", "",
525                                      0, 255, 0,
526                                      G_PARAM_READABLE |
527                                      G_PARAM_STATIC_STRINGS));
528
529         /**
530          * NMDeviceIPTunnel:flow-label:
531          *
532          * The flow label to assign to tunnel packets. This property
533          * applies only to IPv6 tunnels.
534          *
535          * Since: 1.2
536          **/
537         g_object_class_install_property
538                 (object_class, PROP_FLOW_LABEL,
539                  g_param_spec_uint (NM_DEVICE_IP_TUNNEL_FLOW_LABEL, "", "",
540                                     0, (1 << 20) - 1, 0,
541                                     G_PARAM_READABLE |
542                                     G_PARAM_STATIC_STRINGS));
543 }