1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
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.
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.
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.
18 * Copyright 2015 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <arpa/inet.h>
26 #include "nm-setting-connection.h"
27 #include "nm-setting-tun.h"
30 #include "nm-device-tun.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
34 G_DEFINE_TYPE (NMDeviceTun, nm_device_tun, NM_TYPE_DEVICE)
36 #define NM_DEVICE_TUN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_TUN, NMDeviceTunPrivate))
62 * nm_device_tun_get_hw_address:
63 * @device: a #NMDeviceTun
65 * Gets the hardware (MAC) address of the #NMDeviceTun
67 * Returns: the hardware address. This is the internal string used by the
68 * device, and must not be modified.
73 nm_device_tun_get_hw_address (NMDeviceTun *device)
75 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), NULL);
77 return NM_DEVICE_TUN_GET_PRIVATE (device)->hw_address;
81 * nm_device_tun_get_mode:
82 * @device: a #NMDeviceTun
84 * Returns the TUN/TAP mode for the device.
86 * Returns: 'tun' or 'tap'
91 nm_device_tun_get_mode (NMDeviceTun *device)
93 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), NULL);
95 return NM_DEVICE_TUN_GET_PRIVATE (device)->mode;
99 * nm_device_tun_get_owner:
100 * @device: a #NMDeviceTun
102 * Gets the tunnel owner.
104 * Returns: the uid of the tunnel owner, or -1 if it has no owner.
109 nm_device_tun_get_owner (NMDeviceTun *device)
111 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), -1);
113 return NM_DEVICE_TUN_GET_PRIVATE (device)->owner;
117 * nm_device_tun_get_group:
118 * @device: a #NMDeviceTun
120 * Gets the tunnel group.
122 * Returns: the gid of the tunnel group, or -1 if it has no owner.
127 nm_device_tun_get_group (NMDeviceTun *device)
129 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), -1);
131 return NM_DEVICE_TUN_GET_PRIVATE (device)->group;
135 * nm_device_tun_get_pi:
136 * @device: a #NMDeviceTun
138 * Returns whether the #NMDeviceTun has the IFF_NO_PI flag.
140 * Returns: %TRUE if the device has the flag, %FALSE otherwise
145 nm_device_tun_get_no_pi (NMDeviceTun *device)
147 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
149 return NM_DEVICE_TUN_GET_PRIVATE (device)->no_pi;
153 * nm_device_tun_get_vnet_hdr:
154 * @device: a #NMDeviceTun
156 * Returns whether the #NMDeviceTun has the IFF_VNET_HDR flag.
158 * Returns: %TRUE if the device has the flag, %FALSE otherwise
163 nm_device_tun_get_vnet_hdr (NMDeviceTun *device)
165 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
167 return NM_DEVICE_TUN_GET_PRIVATE (device)->vnet_hdr;
171 * nm_device_tun_get_multi_queue:
172 * @device: a #NMDeviceTun
174 * Returns whether the #NMDeviceTun has the IFF_MULTI_QUEUE flag.
176 * Returns: %TRUE if the device doesn't have the flag, %FALSE otherwise
181 nm_device_tun_get_multi_queue (NMDeviceTun *device)
183 g_return_val_if_fail (NM_IS_DEVICE_TUN (device), FALSE);
185 return NM_DEVICE_TUN_GET_PRIVATE (device)->multi_queue;
189 tun_mode_from_string (const char *string)
191 if (!g_strcmp0 (string, "tap"))
192 return NM_SETTING_TUN_MODE_TAP;
194 return NM_SETTING_TUN_MODE_TUN;
198 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
200 NMDeviceTunPrivate *priv = NM_DEVICE_TUN_GET_PRIVATE (device);
201 NMSettingTunMode mode;
204 if (!NM_DEVICE_CLASS (nm_device_tun_parent_class)->connection_compatible (device, connection, error))
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."));
213 s_tun = nm_connection_get_setting_tun (connection);
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"));
226 get_setting_type (NMDevice *device)
228 return NM_TYPE_SETTING_TUN;
232 get_hw_address (NMDevice *device)
234 return nm_device_tun_get_hw_address (NM_DEVICE_TUN (device));
237 /***********************************************************/
240 nm_device_tun_init (NMDeviceTun *device)
242 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_TUN);
246 init_dbus (NMObject *object)
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 },
260 NM_OBJECT_CLASS (nm_device_tun_parent_class)->init_dbus (object);
262 _nm_object_register_properties (object,
263 NM_DBUS_INTERFACE_DEVICE_TUN,
268 finalize (GObject *object)
270 NMDeviceTunPrivate *priv = NM_DEVICE_TUN_GET_PRIVATE (object);
274 G_OBJECT_CLASS (nm_device_tun_parent_class)->finalize (object);
278 get_property (GObject *object,
283 NMDeviceTun *device = NM_DEVICE_TUN (object);
286 case PROP_HW_ADDRESS:
287 g_value_set_string (value, nm_device_tun_get_hw_address (device));
290 g_value_set_string (value, nm_device_tun_get_mode (device));
293 g_value_set_int64 (value, nm_device_tun_get_owner (device));
296 g_value_set_int64 (value, nm_device_tun_get_group (device));
299 g_value_set_boolean (value, nm_device_tun_get_no_pi (device));
302 g_value_set_boolean (value, nm_device_tun_get_vnet_hdr (device));
304 case PROP_MULTI_QUEUE:
305 g_value_set_boolean (value, nm_device_tun_get_multi_queue (device));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 nm_device_tun_class_init (NMDeviceTunClass *gre_class)
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);
321 g_type_class_add_private (gre_class, sizeof (NMDeviceTunPrivate));
323 _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_TUN);
325 /* virtual methods */
326 object_class->finalize = finalize;
327 object_class->get_property = get_property;
329 nm_object_class->init_dbus = init_dbus;
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;
338 * NMDeviceTun:hw-address:
340 * The hardware (MAC) address of the device.
344 g_object_class_install_property
345 (object_class, PROP_HW_ADDRESS,
346 g_param_spec_string (NM_DEVICE_TUN_HW_ADDRESS, "", "",
349 G_PARAM_STATIC_STRINGS));
354 * The tunnel mode, either "tun" or "tap".
358 g_object_class_install_property
359 (object_class, PROP_MODE,
360 g_param_spec_string (NM_DEVICE_TUN_MODE, "", "",
363 G_PARAM_STATIC_STRINGS));
368 * The uid of the tunnel owner, or -1 if it has no owner.
372 g_object_class_install_property
373 (object_class, PROP_OWNER,
374 g_param_spec_int64 (NM_DEVICE_TUN_OWNER, "", "",
377 G_PARAM_STATIC_STRINGS));
382 * The gid of the tunnel group, or -1 if it has no owner.
386 g_object_class_install_property
387 (object_class, PROP_GROUP,
388 g_param_spec_int64 (NM_DEVICE_TUN_GROUP, "", "",
391 G_PARAM_STATIC_STRINGS));
396 * The tunnel's "TUN_NO_PI" flag; true if no protocol info is
397 * prepended to the tunnel packets.
401 g_object_class_install_property
402 (object_class, PROP_NO_PI,
403 g_param_spec_boolean (NM_DEVICE_TUN_NO_PI, "", "",
406 G_PARAM_STATIC_STRINGS));
409 * NMDeviceTun:vnet-hdr:
411 * The tunnel's "TUN_VNET_HDR" flag; true if the tunnel packets
412 * include a virtio network header.
416 g_object_class_install_property
417 (object_class, PROP_VNET_HDR,
418 g_param_spec_boolean (NM_DEVICE_TUN_VNET_HDR, "", "",
421 G_PARAM_STATIC_STRINGS));
424 * NMDeviceTun:multi-queue:
426 * The tunnel's "TUN_TAP_MQ" flag; true if callers can connect to
427 * the tap device multiple times, for multiple send/receive
432 g_object_class_install_property
433 (object_class, PROP_MULTI_QUEUE,
434 g_param_spec_boolean (NM_DEVICE_TUN_MULTI_QUEUE, "", "",
437 G_PARAM_STATIC_STRINGS));