nm-setting-bridge.c 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
19
 * Copyright 2011 - 2013 Red Hat, Inc.
20 21
 */

22
#include "nm-default.h"
23

24 25 26 27
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <dbus/dbus-glib.h>
28
#include <linux/if_ether.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include "nm-setting-bridge.h"
#include "nm-param-spec-specialized.h"
#include "nm-setting-private.h"
#include "nm-utils.h"
#include "nm-utils-private.h"
#include "nm-dbus-glib-types.h"

/**
 * SECTION:nm-setting-bridge
 * @short_description: Describes connection properties for bridges
 * @include: nm-setting-bridge.h
 *
 * The #NMSettingBridge object is a #NMSetting subclass that describes properties
 * necessary for bridging connections.
44 45
 *
 * Since: 0.9.8
46 47 48 49 50 51 52 53
 **/

/**
 * nm_setting_bridge_error_quark:
 *
 * Registers an error quark for #NMSettingBridge if necessary.
 *
 * Returns: the error quark used for #NMSettingBridge errors.
54 55
 *
 * Since: 0.9.8
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 **/
GQuark
nm_setting_bridge_error_quark (void)
{
	static GQuark quark;

	if (G_UNLIKELY (!quark))
		quark = g_quark_from_static_string ("nm-setting-bridge-error-quark");
	return quark;
}

G_DEFINE_TYPE_WITH_CODE (NMSettingBridge, nm_setting_bridge, NM_TYPE_SETTING,
                         _nm_register_setting (NM_SETTING_BRIDGE_SETTING_NAME,
                                               g_define_type_id,
                                               1,
                                               NM_SETTING_BRIDGE_ERROR))
NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BRIDGE)

#define NM_SETTING_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE, NMSettingBridgePrivate))

typedef struct {
	char *   interface_name;
78
	GByteArray *mac_address;
79 80 81 82 83 84 85 86 87 88 89
	gboolean stp;
	guint16  priority;
	guint16  forward_delay;
	guint16  hello_time;
	guint16  max_age;
	guint32  ageing_time;
} NMSettingBridgePrivate;

enum {
	PROP_0,
	PROP_INTERFACE_NAME,
90
	PROP_MAC_ADDRESS,
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	PROP_STP,
	PROP_PRIORITY,
	PROP_FORWARD_DELAY,
	PROP_HELLO_TIME,
	PROP_MAX_AGE,
	PROP_AGEING_TIME,
	LAST_PROP
};

/**
 * nm_setting_bridge_new:
 *
 * Creates a new #NMSettingBridge object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingBridge object
106 107
 *
 * Since: 0.9.8
108 109 110 111 112 113 114 115
 **/
NMSetting *
nm_setting_bridge_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_BRIDGE, NULL);
}

/**
116
 * nm_setting_bridge_get_interface_name:
117 118 119
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:interface-name property of the setting
120 121
 *
 * Since: 0.9.8
122 123 124 125 126 127 128 129 130
 **/
const char *
nm_setting_bridge_get_interface_name (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->interface_name;
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
 * nm_setting_bridge_get_mac_address:
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:mac-address property of the setting
 *
 * Since: 0.9.10
 **/
const GByteArray *
nm_setting_bridge_get_mac_address (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), NULL);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->mac_address;
}

147
/**
148
 * nm_setting_bridge_get_stp:
149 150 151
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:stp property of the setting
152 153
 *
 * Since: 0.9.8
154 155 156 157 158 159 160 161 162 163
 **/
gboolean
nm_setting_bridge_get_stp (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->stp;
}

/**
164
 * nm_setting_bridge_get_priority:
165 166 167
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:priority property of the setting
168 169
 *
 * Since: 0.9.8
170 171 172 173 174 175 176 177 178 179
 **/
guint16
nm_setting_bridge_get_priority (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->priority;
}

/**
180
 * nm_setting_bridge_get_forward_delay:
181 182 183
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:forward-delay property of the setting
184 185
 *
 * Since: 0.9.8
186 187 188 189 190 191 192 193 194 195
 **/
guint16
nm_setting_bridge_get_forward_delay (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->forward_delay;
}

/**
196
 * nm_setting_bridge_get_hello_time:
197 198 199
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:hello-time property of the setting
200 201
 *
 * Since: 0.9.8
202 203 204 205 206 207 208 209 210 211
 **/
guint16
nm_setting_bridge_get_hello_time (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->hello_time;
}

/**
212
 * nm_setting_bridge_get_max_age:
213 214 215
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:max-age property of the setting
216 217
 *
 * Since: 0.9.8
218 219 220 221 222 223 224 225 226 227
 **/
guint16
nm_setting_bridge_get_max_age (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->max_age;
}

/**
228
 * nm_setting_bridge_get_ageing_time:
229 230 231
 * @setting: the #NMSettingBridge
 *
 * Returns: the #NMSettingBridge:ageing-time property of the setting
232 233
 *
 * Since: 0.9.8
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
 **/
guint
nm_setting_bridge_get_ageing_time (NMSettingBridge *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);

	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->ageing_time;
}

/* IEEE 802.1D-1998 timer values */
#define BR_MIN_HELLO_TIME    1
#define BR_MAX_HELLO_TIME    10

#define BR_MIN_FORWARD_DELAY 2
#define BR_MAX_FORWARD_DELAY 30

#define BR_MIN_MAX_AGE       6
#define BR_MAX_MAX_AGE       40

/* IEEE 802.1D-1998 Table 7.4 */
#define BR_MIN_AGEING_TIME   0
#define BR_MAX_AGEING_TIME   1000000

static inline gboolean
check_range (guint32 val,
             guint32 min,
             guint32 max,
             const char *prop,
             GError **error)
{
	if ((val != 0) && (val < min || val > max)) {
265 266 267 268 269
		g_set_error (error,
		             NM_SETTING_BRIDGE_ERROR,
		             NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
		             _("value '%d' is out of range <%d-%d>"),
		             val, min, max);
270
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, prop);
271 272 273 274 275 276 277 278 279 280
		return FALSE;
	}
	return TRUE;
}

static gboolean
verify (NMSetting *setting, GSList *all_settings, GError **error)
{
	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);

281 282 283 284 285 286 287 288 289
	if (priv->mac_address && priv->mac_address->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_BRIDGE_ERROR,
		                     NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
		                     _("is not a valid MAC address"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_MAC_ADDRESS);
		return FALSE;
	}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	if (!check_range (priv->forward_delay,
	                  BR_MIN_FORWARD_DELAY,
	                  BR_MAX_FORWARD_DELAY,
	                  NM_SETTING_BRIDGE_FORWARD_DELAY,
	                  error))
		return FALSE;

	if (!check_range (priv->hello_time,
	                  BR_MIN_HELLO_TIME,
	                  BR_MAX_HELLO_TIME,
	                  NM_SETTING_BRIDGE_HELLO_TIME,
	                  error))
		return FALSE;

	if (!check_range (priv->max_age,
	                  BR_MIN_MAX_AGE,
	                  BR_MAX_MAX_AGE,
	                  NM_SETTING_BRIDGE_MAX_AGE,
	                  error))
		return FALSE;

	if (!check_range (priv->ageing_time,
	                  BR_MIN_AGEING_TIME,
	                  BR_MAX_AGEING_TIME,
	                  NM_SETTING_BRIDGE_AGEING_TIME,
	                  error))
		return FALSE;

318 319 320 321 322 323 324
	return _nm_setting_verify_deprecated_virtual_iface_name (
	         priv->interface_name, FALSE,
	         NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_INTERFACE_NAME,
	         NM_SETTING_BRIDGE_ERROR,
	         NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
	         NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY,
	         all_settings, error);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
}

static const char *
get_virtual_iface_name (NMSetting *setting)
{
	NMSettingBridge *self = NM_SETTING_BRIDGE (setting);

	return nm_setting_bridge_get_interface_name (self);
}

static void
nm_setting_bridge_init (NMSettingBridge *setting)
{
}

static void
finalize (GObject *object)
{
	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);

	g_free (priv->interface_name);

347 348 349
	if (priv->mac_address)
		g_byte_array_free (priv->mac_address, TRUE);

350 351 352 353 354 355 356 357 358 359 360 361 362 363
	G_OBJECT_CLASS (nm_setting_bridge_parent_class)->finalize (object);
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_INTERFACE_NAME:
		g_free (priv->interface_name);
		priv->interface_name = g_value_dup_string (value);
		break;
364 365 366 367 368
	case PROP_MAC_ADDRESS:
		if (priv->mac_address)
			g_byte_array_free (priv->mac_address, TRUE);
		priv->mac_address = g_value_dup_boxed (value);
		break;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	case PROP_STP:
		priv->stp = g_value_get_boolean (value);
		break;
	case PROP_PRIORITY:
		priv->priority = (guint16) g_value_get_uint (value);
		break;
	case PROP_FORWARD_DELAY:
		priv->forward_delay = (guint16) g_value_get_uint (value);
		break;
	case PROP_HELLO_TIME:
		priv->hello_time = (guint16) g_value_get_uint (value);
		break;
	case PROP_MAX_AGE:
		priv->max_age = (guint16) g_value_get_uint (value);
		break;
	case PROP_AGEING_TIME:
		priv->ageing_time = g_value_get_uint (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
	NMSettingBridge *setting = NM_SETTING_BRIDGE (object);

	switch (prop_id) {
	case PROP_INTERFACE_NAME:
		g_value_set_string (value, nm_setting_bridge_get_interface_name (setting));
		break;
404 405 406
	case PROP_MAC_ADDRESS:
		g_value_set_boxed (value, nm_setting_bridge_get_mac_address (setting));
		break;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	case PROP_STP:
		g_value_set_boolean (value, priv->stp);
		break;
	case PROP_PRIORITY:
		g_value_set_uint (value, priv->priority);
		break;
	case PROP_FORWARD_DELAY:
		g_value_set_uint (value, priv->forward_delay);
		break;
	case PROP_HELLO_TIME:
		g_value_set_uint (value, priv->hello_time);
		break;
	case PROP_MAX_AGE:
		g_value_set_uint (value, priv->max_age);
		break;
	case PROP_AGEING_TIME:
		g_value_set_uint (value, priv->ageing_time);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_setting_bridge_class_init (NMSettingBridgeClass *setting_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
	NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);

	g_type_class_add_private (setting_class, sizeof (NMSettingBridgePrivate));

	/* virtual methods */
	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
	parent_class->verify       = verify;
	parent_class->get_virtual_iface_name = get_virtual_iface_name;

	/* Properties */
	/**
	 * NMSettingBridge:interface-name:
	 *
450
	 * The name of the virtual in-kernel bridging network interface
451 452
	 *
	 * Since: 0.9.8
453 454 455
	 **/
	g_object_class_install_property
		(object_class, PROP_INTERFACE_NAME,
456
		 g_param_spec_string (NM_SETTING_BRIDGE_INTERFACE_NAME, "", "",
457
		                      NULL,
458 459
		                      G_PARAM_READWRITE |
		                      NM_SETTING_PARAM_INFERRABLE |
460
		                      G_PARAM_STATIC_STRINGS));
461

462 463 464
	/**
	 * NMSettingBridge:mac-address:
	 *
465 466 467
	 * If specified, the MAC address of bridge. When creating a new bridge, this
	 * MAC address will be set. When matching an existing (outside
	 * NetworkManager created) bridge, this MAC address must match.
468 469 470 471
	 *
	 * Since: 0.9.10
	 **/
	g_object_class_install_property
472 473 474
		(object_class, PROP_MAC_ADDRESS,
		 _nm_param_spec_specialized (NM_SETTING_BRIDGE_MAC_ADDRESS, "", "",
		                             DBUS_TYPE_G_UCHAR_ARRAY,
475 476
		                             G_PARAM_READWRITE |
		                             NM_SETTING_PARAM_INFERRABLE |
477
		                             G_PARAM_STATIC_STRINGS));
478

479 480 481 482
	/**
	 * NMSettingBridge:stp:
	 *
	 * Controls whether Spanning Tree Protocol (STP) is enabled for this bridge.
483 484
	 *
	 * Since: 0.9.8
485
	 **/
486 487 488 489
	g_object_class_install_property
		(object_class, PROP_STP,
		 g_param_spec_boolean (NM_SETTING_BRIDGE_STP, "", "",
		                       TRUE,
490 491 492
		                       G_PARAM_READWRITE |
		                       G_PARAM_CONSTRUCT |
		                       NM_SETTING_PARAM_INFERRABLE |
493
		                       G_PARAM_STATIC_STRINGS));
494 495 496 497 498 499 500

	/**
	 * NMSettingBridge:priority:
	 *
	 * Sets the Spanning Tree Protocol (STP) priority for this bridge.  Lower
	 * values are "better"; the lowest priority bridge will be elected the root
	 * bridge.
501 502
	 *
	 * Since: 0.9.8
503
	 **/
504 505 506 507
	g_object_class_install_property
		(object_class, PROP_PRIORITY,
		 g_param_spec_uint (NM_SETTING_BRIDGE_PRIORITY, "", "",
		                    0, G_MAXUINT16, 0x8000,
508 509 510
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
511
		                    G_PARAM_STATIC_STRINGS));
512 513 514 515 516

	/**
	 * NMSettingBridge:forward-delay:
	 *
	 * The Spanning Tree Protocol (STP) forwarding delay, in seconds.
517 518
	 *
	 * Since: 0.9.8
519
	 **/
520 521 522 523
	g_object_class_install_property
		(object_class, PROP_FORWARD_DELAY,
		 g_param_spec_uint (NM_SETTING_BRIDGE_FORWARD_DELAY, "", "",
		                    0, BR_MAX_FORWARD_DELAY, 15,
524 525 526
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
527
		                    G_PARAM_STATIC_STRINGS));
528 529 530 531 532

	/**
	 * NMSettingBridge:hello-time:
	 *
	 * The Spanning Tree Protocol (STP) hello time, in seconds.
533 534
	 *
	 * Since: 0.9.8
535
	 **/
536 537 538 539
	g_object_class_install_property
		(object_class, PROP_HELLO_TIME,
		 g_param_spec_uint (NM_SETTING_BRIDGE_HELLO_TIME, "", "",
		                    0, BR_MAX_HELLO_TIME, 2,
540 541 542
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
543
		                    G_PARAM_STATIC_STRINGS));
544 545 546 547 548

	/**
	 * NMSettingBridge:max-age:
	 *
	 * The Spanning Tree Protocol (STP) maximum message age, in seconds.
549 550
	 *
	 * Since: 0.9.8
551
	 **/
552 553 554 555
	g_object_class_install_property
		(object_class, PROP_MAX_AGE,
		 g_param_spec_uint (NM_SETTING_BRIDGE_MAX_AGE, "", "",
		                    0, BR_MAX_MAX_AGE, 20,
556 557 558
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
559
		                    G_PARAM_STATIC_STRINGS));
560 561 562 563

	/**
	 * NMSettingBridge:ageing-time:
	 *
564
	 * The Ethernet MAC address aging time, in seconds.
565 566
	 *
	 * Since: 0.9.8
567
	 **/
568 569 570 571
	g_object_class_install_property
		(object_class, PROP_AGEING_TIME,
		 g_param_spec_uint (NM_SETTING_BRIDGE_AGEING_TIME, "", "",
		                    0, BR_MAX_AGEING_TIME, 300,
572 573 574
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
575
		                    G_PARAM_STATIC_STRINGS));
576
}