1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2011 - 2013 Red Hat, Inc.
22 #include "nm-default.h"
27 #include <dbus/dbus-glib.h>
28 #include <linux/if_ether.h>
30 #include "nm-setting-bridge.h"
31 #include "nm-param-spec-specialized.h"
32 #include "nm-setting-private.h"
34 #include "nm-utils-private.h"
35 #include "nm-dbus-glib-types.h"
38 * SECTION:nm-setting-bridge
39 * @short_description: Describes connection properties for bridges
40 * @include: nm-setting-bridge.h
42 * The #NMSettingBridge object is a #NMSetting subclass that describes properties
43 * necessary for bridging connections.
49 * nm_setting_bridge_error_quark:
51 * Registers an error quark for #NMSettingBridge if necessary.
53 * Returns: the error quark used for #NMSettingBridge errors.
58 nm_setting_bridge_error_quark (void)
62 if (G_UNLIKELY (!quark))
63 quark = g_quark_from_static_string ("nm-setting-bridge-error-quark");
68 G_DEFINE_TYPE_WITH_CODE (NMSettingBridge, nm_setting_bridge, NM_TYPE_SETTING,
69 _nm_register_setting (NM_SETTING_BRIDGE_SETTING_NAME,
72 NM_SETTING_BRIDGE_ERROR))
73 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BRIDGE)
75 #define NM_SETTING_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE, NMSettingBridgePrivate))
78 char * interface_name;
79 GByteArray *mac_address;
82 guint16 forward_delay;
86 } NMSettingBridgePrivate;
102 * nm_setting_bridge_new:
104 * Creates a new #NMSettingBridge object with default values.
106 * Returns: (transfer full): the new empty #NMSettingBridge object
111 nm_setting_bridge_new (void)
113 return (NMSetting *) g_object_new (NM_TYPE_SETTING_BRIDGE, NULL);
117 * nm_setting_bridge_get_interface_name:
118 * @setting: the #NMSettingBridge
120 * Returns: the #NMSettingBridge:interface-name property of the setting
125 nm_setting_bridge_get_interface_name (NMSettingBridge *setting)
127 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
129 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->interface_name;
133 * nm_setting_bridge_get_mac_address:
134 * @setting: the #NMSettingBridge
136 * Returns: the #NMSettingBridge:mac-address property of the setting
141 nm_setting_bridge_get_mac_address (NMSettingBridge *setting)
143 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), NULL);
145 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->mac_address;
149 * nm_setting_bridge_get_stp:
150 * @setting: the #NMSettingBridge
152 * Returns: the #NMSettingBridge:stp property of the setting
157 nm_setting_bridge_get_stp (NMSettingBridge *setting)
159 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
161 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->stp;
165 * nm_setting_bridge_get_priority:
166 * @setting: the #NMSettingBridge
168 * Returns: the #NMSettingBridge:priority property of the setting
173 nm_setting_bridge_get_priority (NMSettingBridge *setting)
175 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
177 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->priority;
181 * nm_setting_bridge_get_forward_delay:
182 * @setting: the #NMSettingBridge
184 * Returns: the #NMSettingBridge:forward-delay property of the setting
189 nm_setting_bridge_get_forward_delay (NMSettingBridge *setting)
191 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
193 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->forward_delay;
197 * nm_setting_bridge_get_hello_time:
198 * @setting: the #NMSettingBridge
200 * Returns: the #NMSettingBridge:hello-time property of the setting
205 nm_setting_bridge_get_hello_time (NMSettingBridge *setting)
207 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
209 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->hello_time;
213 * nm_setting_bridge_get_max_age:
214 * @setting: the #NMSettingBridge
216 * Returns: the #NMSettingBridge:max-age property of the setting
221 nm_setting_bridge_get_max_age (NMSettingBridge *setting)
223 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
225 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->max_age;
229 * nm_setting_bridge_get_ageing_time:
230 * @setting: the #NMSettingBridge
232 * Returns: the #NMSettingBridge:ageing-time property of the setting
237 nm_setting_bridge_get_ageing_time (NMSettingBridge *setting)
239 g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
241 return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->ageing_time;
244 /* IEEE 802.1D-1998 timer values */
245 #define BR_MIN_HELLO_TIME 1
246 #define BR_MAX_HELLO_TIME 10
248 #define BR_MIN_FORWARD_DELAY 2
249 #define BR_MAX_FORWARD_DELAY 30
251 #define BR_MIN_MAX_AGE 6
252 #define BR_MAX_MAX_AGE 40
254 /* IEEE 802.1D-1998 Table 7.4 */
255 #define BR_MIN_AGEING_TIME 0
256 #define BR_MAX_AGEING_TIME 1000000
258 static inline gboolean
259 check_range (guint32 val,
265 if ((val != 0) && (val < min || val > max)) {
267 NM_SETTING_BRIDGE_ERROR,
268 NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
269 _("value '%d' is out of range <%d-%d>"),
271 g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, prop);
278 verify (NMSetting *setting, GSList *all_settings, GError **error)
280 NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);
282 if (priv->mac_address && priv->mac_address->len != ETH_ALEN) {
283 g_set_error_literal (error,
284 NM_SETTING_BRIDGE_ERROR,
285 NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
286 _("is not a valid MAC address"));
287 g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_MAC_ADDRESS);
291 if (!check_range (priv->forward_delay,
292 BR_MIN_FORWARD_DELAY,
293 BR_MAX_FORWARD_DELAY,
294 NM_SETTING_BRIDGE_FORWARD_DELAY,
298 if (!check_range (priv->hello_time,
301 NM_SETTING_BRIDGE_HELLO_TIME,
305 if (!check_range (priv->max_age,
308 NM_SETTING_BRIDGE_MAX_AGE,
312 if (!check_range (priv->ageing_time,
315 NM_SETTING_BRIDGE_AGEING_TIME,
319 return _nm_setting_verify_deprecated_virtual_iface_name (
320 priv->interface_name, FALSE,
321 NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_INTERFACE_NAME,
322 NM_SETTING_BRIDGE_ERROR,
323 NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
324 NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY,
325 all_settings, error);
329 get_virtual_iface_name (NMSetting *setting)
331 NMSettingBridge *self = NM_SETTING_BRIDGE (setting);
333 return nm_setting_bridge_get_interface_name (self);
337 nm_setting_bridge_init (NMSettingBridge *setting)
342 finalize (GObject *object)
344 NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
346 g_free (priv->interface_name);
348 if (priv->mac_address)
349 g_byte_array_free (priv->mac_address, TRUE);
351 G_OBJECT_CLASS (nm_setting_bridge_parent_class)->finalize (object);
355 set_property (GObject *object, guint prop_id,
356 const GValue *value, GParamSpec *pspec)
358 NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
361 case PROP_INTERFACE_NAME:
362 g_free (priv->interface_name);
363 priv->interface_name = g_value_dup_string (value);
365 case PROP_MAC_ADDRESS:
366 if (priv->mac_address)
367 g_byte_array_free (priv->mac_address, TRUE);
368 priv->mac_address = g_value_dup_boxed (value);
371 priv->stp = g_value_get_boolean (value);
374 priv->priority = (guint16) g_value_get_uint (value);
376 case PROP_FORWARD_DELAY:
377 priv->forward_delay = (guint16) g_value_get_uint (value);
379 case PROP_HELLO_TIME:
380 priv->hello_time = (guint16) g_value_get_uint (value);
383 priv->max_age = (guint16) g_value_get_uint (value);
385 case PROP_AGEING_TIME:
386 priv->ageing_time = g_value_get_uint (value);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395 get_property (GObject *object, guint prop_id,
396 GValue *value, GParamSpec *pspec)
398 NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
399 NMSettingBridge *setting = NM_SETTING_BRIDGE (object);
402 case PROP_INTERFACE_NAME:
403 g_value_set_string (value, nm_setting_bridge_get_interface_name (setting));
405 case PROP_MAC_ADDRESS:
406 g_value_set_boxed (value, nm_setting_bridge_get_mac_address (setting));
409 g_value_set_boolean (value, priv->stp);
412 g_value_set_uint (value, priv->priority);
414 case PROP_FORWARD_DELAY:
415 g_value_set_uint (value, priv->forward_delay);
417 case PROP_HELLO_TIME:
418 g_value_set_uint (value, priv->hello_time);
421 g_value_set_uint (value, priv->max_age);
423 case PROP_AGEING_TIME:
424 g_value_set_uint (value, priv->ageing_time);
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
433 nm_setting_bridge_class_init (NMSettingBridgeClass *setting_class)
435 GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
436 NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
438 g_type_class_add_private (setting_class, sizeof (NMSettingBridgePrivate));
440 /* virtual methods */
441 object_class->set_property = set_property;
442 object_class->get_property = get_property;
443 object_class->finalize = finalize;
444 parent_class->verify = verify;
445 parent_class->get_virtual_iface_name = get_virtual_iface_name;
449 * NMSettingBridge:interface-name:
451 * The name of the virtual in-kernel bridging network interface
455 g_object_class_install_property
456 (object_class, PROP_INTERFACE_NAME,
457 g_param_spec_string (NM_SETTING_BRIDGE_INTERFACE_NAME, "", "",
460 NM_SETTING_PARAM_INFERRABLE |
461 G_PARAM_STATIC_STRINGS));
464 * NMSettingBridge:mac-address:
466 * If specified, the MAC address of bridge. When creating a new bridge, this
467 * MAC address will be set. When matching an existing (outside
468 * NetworkManager created) bridge, this MAC address must match.
472 g_object_class_install_property
473 (object_class, PROP_MAC_ADDRESS,
474 _nm_param_spec_specialized (NM_SETTING_BRIDGE_MAC_ADDRESS, "", "",
475 DBUS_TYPE_G_UCHAR_ARRAY,
477 NM_SETTING_PARAM_INFERRABLE |
478 G_PARAM_STATIC_STRINGS));
481 * NMSettingBridge:stp:
483 * Controls whether Spanning Tree Protocol (STP) is enabled for this bridge.
487 g_object_class_install_property
488 (object_class, PROP_STP,
489 g_param_spec_boolean (NM_SETTING_BRIDGE_STP, "", "",
493 NM_SETTING_PARAM_INFERRABLE |
494 G_PARAM_STATIC_STRINGS));
497 * NMSettingBridge:priority:
499 * Sets the Spanning Tree Protocol (STP) priority for this bridge. Lower
500 * values are "better"; the lowest priority bridge will be elected the root
505 g_object_class_install_property
506 (object_class, PROP_PRIORITY,
507 g_param_spec_uint (NM_SETTING_BRIDGE_PRIORITY, "", "",
508 0, G_MAXUINT16, 0x8000,
511 NM_SETTING_PARAM_INFERRABLE |
512 G_PARAM_STATIC_STRINGS));
515 * NMSettingBridge:forward-delay:
517 * The Spanning Tree Protocol (STP) forwarding delay, in seconds.
521 g_object_class_install_property
522 (object_class, PROP_FORWARD_DELAY,
523 g_param_spec_uint (NM_SETTING_BRIDGE_FORWARD_DELAY, "", "",
524 0, BR_MAX_FORWARD_DELAY, 15,
527 NM_SETTING_PARAM_INFERRABLE |
528 G_PARAM_STATIC_STRINGS));
531 * NMSettingBridge:hello-time:
533 * The Spanning Tree Protocol (STP) hello time, in seconds.
537 g_object_class_install_property
538 (object_class, PROP_HELLO_TIME,
539 g_param_spec_uint (NM_SETTING_BRIDGE_HELLO_TIME, "", "",
540 0, BR_MAX_HELLO_TIME, 2,
543 NM_SETTING_PARAM_INFERRABLE |
544 G_PARAM_STATIC_STRINGS));
547 * NMSettingBridge:max-age:
549 * The Spanning Tree Protocol (STP) maximum message age, in seconds.
553 g_object_class_install_property
554 (object_class, PROP_MAX_AGE,
555 g_param_spec_uint (NM_SETTING_BRIDGE_MAX_AGE, "", "",
556 0, BR_MAX_MAX_AGE, 20,
559 NM_SETTING_PARAM_INFERRABLE |
560 G_PARAM_STATIC_STRINGS));
563 * NMSettingBridge:ageing-time:
565 * The Ethernet MAC address aging time, in seconds.
569 g_object_class_install_property
570 (object_class, PROP_AGEING_TIME,
571 g_param_spec_uint (NM_SETTING_BRIDGE_AGEING_TIME, "", "",
572 0, BR_MAX_AGEING_TIME, 300,
575 NM_SETTING_PARAM_INFERRABLE |
576 G_PARAM_STATIC_STRINGS));