nm-setting-vlan.c 25.9 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 - 2014 Red Hat, Inc.
20 21
 */

22
#include "nm-default.h"
23

24 25
#include <stdlib.h>
#include <string.h>
26
#include <dbus/dbus-glib.h>
27

28 29 30 31
#include "nm-setting-vlan.h"
#include "nm-param-spec-specialized.h"
#include "nm-utils.h"
#include "nm-dbus-glib-types.h"
32
#include "nm-setting-connection.h"
33
#include "nm-setting-private.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

/**
 * SECTION:nm-setting-vlan
 * @short_description: Describes connection properties for VLAN interfaces
 * @include: nm-setting-vlan.h
 *
 * The #NMSettingVlan object is a #NMSetting subclass that describes properties
 * necessary for connection to VLAN interfaces.
 **/

/**
 * nm_setting_vlan_error_quark:
 *
 * Registers an error quark for #NMSettingVlan if necessary.
 *
 * Returns: the error quark used for #NMSettingVlan errors.
 **/
GQuark
nm_setting_vlan_error_quark (void)
{
	static GQuark quark;

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

61 62 63 64 65 66
G_DEFINE_TYPE_WITH_CODE (NMSettingVlan, nm_setting_vlan, NM_TYPE_SETTING,
                         _nm_register_setting (NM_SETTING_VLAN_SETTING_NAME,
                                               g_define_type_id,
                                               1,
                                               NM_SETTING_VLAN_ERROR))
NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_VLAN)
67 68 69 70

#define NM_SETTING_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_VLAN, NMSettingVlanPrivate))

typedef struct {
71
	char *interface_name;
72
	char *parent;
73 74 75 76 77 78 79 80
	guint32 id;
	guint32 flags;
	GSList *ingress_priority_map;
	GSList *egress_priority_map;
} NMSettingVlanPrivate;

enum {
	PROP_0,
81
	PROP_INTERFACE_NAME,
82
	PROP_PARENT,
83 84 85 86 87 88 89
	PROP_ID,
	PROP_FLAGS,
	PROP_INGRESS_PRIORITY_MAP,
	PROP_EGRESS_PRIORITY_MAP,
	LAST_PROP
};

90 91 92
#define MAX_SKB_PRIO   G_MAXUINT32
#define MAX_8021P_PRIO 7  /* Max 802.1p priority */

93 94 95 96 97 98 99
typedef struct {
	guint32 from;
	guint32 to;
} PriorityMap;

/**
 * nm_setting_vlan_new:
100
 *
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
 * Creates a new #NMSettingVlan object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingVlan object
 **/
NMSetting *
nm_setting_vlan_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_VLAN, NULL);
}

/**
 * nm_setting_vlan_get_interface_name:
 * @setting: the #NMSettingVlan
 *
 * Returns: the #NMSettingVlan:interface_name property of the setting
 **/
const char *
nm_setting_vlan_get_interface_name (NMSettingVlan *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL);
121
	return NM_SETTING_VLAN_GET_PRIVATE (setting)->interface_name;
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * nm_setting_vlan_get_parent:
 * @setting: the #NMSettingVlan
 *
 * Returns: the #NMSettingVlan:parent property of the setting
 **/
const char *
nm_setting_vlan_get_parent (NMSettingVlan *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL);
	return NM_SETTING_VLAN_GET_PRIVATE (setting)->parent;
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/**
 * nm_setting_vlan_get_id:
 * @setting: the #NMSettingVlan
 *
 * Returns: the #NMSettingVlan:id property of the setting
 **/
guint32
nm_setting_vlan_get_id (NMSettingVlan *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), 0);
	return NM_SETTING_VLAN_GET_PRIVATE (setting)->id;
}

/**
 * nm_setting_vlan_get_flags:
 * @setting: the #NMSettingVlan
 *
 * Returns: the #NMSettingVlan:flags property of the setting
 **/
guint32
nm_setting_vlan_get_flags (NMSettingVlan *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), 0);
	return NM_SETTING_VLAN_GET_PRIVATE (setting)->flags;
}

163 164 165 166 167 168 169 170 171 172
static guint32
get_max_prio (NMVlanPriorityMap map, gboolean from)
{
	if (map == NM_VLAN_INGRESS_MAP)
		return from ? MAX_8021P_PRIO : MAX_SKB_PRIO;
	else if (map == NM_VLAN_EGRESS_MAP)
		return from ? MAX_SKB_PRIO : MAX_8021P_PRIO;
	g_assert_not_reached ();
}

173
static PriorityMap *
174
priority_map_new_from_str (NMVlanPriorityMap map, const char *str)
175 176 177 178
{
	PriorityMap *p = NULL;
	gchar **t = NULL;
	guint32 len;
179
	guint64 from, to;
180 181 182 183 184 185

	g_return_val_if_fail (str && str[0], NULL);

	t = g_strsplit (str, ":", 0);
	len = g_strv_length (t);
	if (len == 2) {
186 187 188 189 190 191 192 193
		from = g_ascii_strtoull (t[0], NULL, 10);
		to = g_ascii_strtoull (t[1], NULL, 10);

		if ((from <= get_max_prio (map, TRUE)) && (to <= get_max_prio (map, FALSE))) {
			p = g_malloc0 (sizeof (PriorityMap));
			p->from = from;
			p->to = to;
		}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	} else {
		/* Warn */
		g_warn_if_fail (len == 2);
	}

	g_strfreev (t);
	return p;
}

static void
priority_map_free (PriorityMap *map)
{
	g_return_if_fail (map != NULL);
	g_free (map);
}

static GSList *
get_map (NMSettingVlan *self, NMVlanPriorityMap map)
{
	if (map == NM_VLAN_INGRESS_MAP)
		return NM_SETTING_VLAN_GET_PRIVATE (self)->ingress_priority_map;
	else if (map == NM_VLAN_EGRESS_MAP)
		return NM_SETTING_VLAN_GET_PRIVATE (self)->egress_priority_map;
	g_assert_not_reached ();
	return NULL;
}

static void
set_map (NMSettingVlan *self, NMVlanPriorityMap map, GSList *list)
{
224
	if (map == NM_VLAN_INGRESS_MAP) {
225
		NM_SETTING_VLAN_GET_PRIVATE (self)->ingress_priority_map = list;
226 227
		g_object_notify (G_OBJECT (self), NM_SETTING_VLAN_INGRESS_PRIORITY_MAP);
	} else if (map == NM_VLAN_EGRESS_MAP) {
228
		NM_SETTING_VLAN_GET_PRIVATE (self)->egress_priority_map = list;
229 230
		g_object_notify (G_OBJECT (self), NM_SETTING_VLAN_EGRESS_PRIORITY_MAP);
	} else
231
		g_assert_not_reached ();
232 233 234
}

/**
235
 * nm_setting_vlan_add_priority_str:
236 237 238 239 240 241
 * @setting: the #NMSettingVlan
 * @map: the type of priority map
 * @str: the string which contains a priority map, like "3:7"
 *
 * Adds a priority map entry into either the #NMSettingVlan:ingress_priority_map
 * or the #NMSettingVlan:egress_priority_map properties.  The priority map maps
242
 * the Linux SKB priorities to 802.1p priorities.
243
 *
244 245
 * Returns: %TRUE if the entry was successfully added to the list, or it
 * overwrote the old value, %FALSE if error
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
 */
gboolean
nm_setting_vlan_add_priority_str (NMSettingVlan *setting,
                                  NMVlanPriorityMap map,
                                  const char *str)
{
	GSList *list = NULL, *iter = NULL;
	PriorityMap *item = NULL;

	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, FALSE);
	g_return_val_if_fail (str && str[0], FALSE);

	list = get_map (setting, map);

261
	item = priority_map_new_from_str (map, str);
262 263
	if (!item)
		g_return_val_if_reached (FALSE);
264 265 266 267 268 269 270 271

	/* Duplicates get replaced */
	for (iter = list; iter; iter = g_slist_next (iter)) {
		PriorityMap *p = iter->data;

		if (p->from == item->from) {
			p->to = item->to;
			g_free (item);
272 273 274 275
			if (map == NM_VLAN_INGRESS_MAP)
				g_object_notify (G_OBJECT (setting), NM_SETTING_VLAN_INGRESS_PRIORITY_MAP);
			else
				g_object_notify (G_OBJECT (setting), NM_SETTING_VLAN_EGRESS_PRIORITY_MAP);
276 277 278 279 280 281 282 283 284 285 286
			return TRUE;
		}
	}

	set_map (setting, map, g_slist_append (list, item));
	return TRUE;
}

/**
 * nm_setting_vlan_get_num_priorities:
 * @setting: the #NMSettingVlan
287
 * @map: the type of priority map
288
 *
289
 * Returns the number of entries in the
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
 * #NMSettingVlan:ingress_priority_map or #NMSettingVlan:egress_priority_map
 * properties of this setting.
 *
 * Returns: return the number of ingress/egress priority entries, -1 if error
 **/
gint32
nm_setting_vlan_get_num_priorities (NMSettingVlan *setting, NMVlanPriorityMap map)
{
	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), -1);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, -1);

	return g_slist_length (get_map (setting, map));
}

/**
 * nm_setting_vlan_get_priority:
 * @setting: the #NMSettingVlan
307
 * @map: the type of priority map
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
 * @idx: the zero-based index of the ingress/egress priority map entry
 * @out_from: (out): on return the value of the priority map's 'from' item
 * @out_to: (out): on return the value of priority map's 'to' item
 *
 * Retrieve one of the entries of the #NMSettingVlan:ingress_priority_map
 * or #NMSettingVlan:egress_priority_map properties of this setting.
 *
 * Returns: %TRUE if a priority map was returned, %FALSE if error
 **/
gboolean
nm_setting_vlan_get_priority (NMSettingVlan *setting,
                              NMVlanPriorityMap map,
                              guint32 idx,
                              guint32 *out_from,
                              guint32 *out_to)
{
	GSList *list = NULL;
	PriorityMap *item = NULL;

	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, FALSE);
	g_return_val_if_fail (out_from != NULL, FALSE);
	g_return_val_if_fail (out_to != NULL, FALSE);

	list = get_map (setting, map);
	g_return_val_if_fail (idx < g_slist_length (list), FALSE);

	item = g_slist_nth_data (list, idx);
	g_assert (item);
	*out_from = item->from;
	*out_to = item->to;
	return TRUE;
}

/**
 * nm_setting_vlan_add_priority:
 * @setting: the #NMSettingVlan
345
 * @map: the type of priority map
346 347
 * @from: the priority to map to @to
 * @to: the priority to map @from to
348 349 350 351 352 353
 *
 * Adds a priority mapping to the #NMSettingVlan:ingress_priority_map or
 * #NMSettingVlan:egress_priority_map properties of the setting. If @from is
 * already in the given priority map, this function will overwrite the
 * existing entry with the new @to.
 *
354 355 356 357 358 359
 * If @map is #NM_VLAN_INGRESS_MAP then @from is the incoming 802.1q VLAN
 * Priority Code Point (PCP) value, and @to is the Linux SKB priority value.
 *
 * If @map is #NM_VLAN_EGRESS_MAP then @from is the Linux SKB priority value and
 * @to is the outgoing 802.1q VLAN Priority Code Point (PCP) value.
 *
360 361
 * Returns: %TRUE if the new priority mapping was successfully added to the
 * list, %FALSE if error
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
 */
gboolean
nm_setting_vlan_add_priority (NMSettingVlan *setting,
                              NMVlanPriorityMap map,
                              guint32 from,
                              guint32 to)
{
	GSList *list = NULL, *iter = NULL;
	PriorityMap *item;

	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, FALSE);

	list = get_map (setting, map);
	for (iter = list; iter; iter = g_slist_next (iter)) {
		item = iter->data;
		if (item->from == from) {
			item->to = to;
380 381 382 383
			if (map == NM_VLAN_INGRESS_MAP)
				g_object_notify (G_OBJECT (setting), NM_SETTING_VLAN_INGRESS_PRIORITY_MAP);
			else
				g_object_notify (G_OBJECT (setting), NM_SETTING_VLAN_EGRESS_PRIORITY_MAP);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
			return TRUE;
		}
	}

	item = g_malloc0 (sizeof (PriorityMap));
	item->from = from;
	item->to = to;
	set_map (setting, map, g_slist_append (list, item));

	return TRUE;
}

/**
 * nm_setting_vlan_remove_priority:
 * @setting: the #NMSettingVlan
399
 * @map: the type of priority map
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
 * @idx: the zero-based index of the priority map to remove
 *
 * Removes the priority map at index @idx from the
 * #NMSettingVlan:ingress_priority_map or #NMSettingVlan:egress_priority_map
 * properties.
 */
void
nm_setting_vlan_remove_priority (NMSettingVlan *setting,
                                 NMVlanPriorityMap map,
                                 guint32 idx)
{
	GSList *list = NULL, *item = NULL;

	g_return_if_fail (NM_IS_SETTING_VLAN (setting));
	g_return_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP);

	list = get_map (setting, map);
	g_return_if_fail (idx < g_slist_length (list));

419 420
	item = g_slist_nth (list, idx);
	priority_map_free ((PriorityMap *) (item->data));
421 422 423
	set_map (setting, map, g_slist_delete_link (list, item));
}

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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/**
 * nm_setting_vlan_remove_priority_by_value:
 * @setting: the #NMSettingVlan
 * @map: the type of priority map
 * @from: the priority to map to @to
 * @to: the priority to map @from to
 *
 * Removes the priority map @form:@to from the #NMSettingVlan:ingress_priority_map
 * or #NMSettingVlan:egress_priority_map (according to @map argument)
 * properties.
 *
 * Returns: %TRUE if the priority mapping was found and removed; %FALSE if it was not.
 *
 * Since: 0.9.10
 */
gboolean
nm_setting_vlan_remove_priority_by_value (NMSettingVlan *setting,
                                          NMVlanPriorityMap map,
                                          guint32 from,
                                          guint32 to)
{
	GSList *list = NULL, *iter = NULL;
	PriorityMap *item;

	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, FALSE);

	list = get_map (setting, map);
	for (iter = list; iter; iter = g_slist_next (iter)) {
		item = iter->data;
		if (item->from == from && item->to == to) {
			priority_map_free ((PriorityMap *) (iter->data));
			set_map (setting, map, g_slist_delete_link (list, iter));
			return TRUE;
		}
	}
	return FALSE;
}

/**
 * nm_setting_vlan_remove_priority_str_by_value:
 * @setting: the #NMSettingVlan
 * @map: the type of priority map
 * @str: the string which contains a priority map, like "3:7"
 *
 * Removes the priority map @str from the #NMSettingVlan:ingress_priority_map
 * or #NMSettingVlan:egress_priority_map (according to @map argument)
 * properties.
 *
 * Returns: %TRUE if the priority mapping was found and removed; %FALSE if it was not.
 *
 * Since: 0.9.10
 */
gboolean
nm_setting_vlan_remove_priority_str_by_value (NMSettingVlan *setting,
                                              NMVlanPriorityMap map,
                                              const char *str)
{
	PriorityMap *item;
483
	gboolean found;
484 485 486 487 488 489 490 491

	g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE);
	g_return_val_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP, FALSE);

	item = priority_map_new_from_str (map, str);
	if (!item)
		return FALSE;

492 493 494
	found = nm_setting_vlan_remove_priority_by_value (setting, map, item->from, item->to);
	g_free (item);
	return found;
495 496
}

497 498 499
/**
 * nm_setting_vlan_clear_priorities:
 * @setting: the #NMSettingVlan
500
 * @map: the type of priority map
501
 *
502
 * Clear all the entries from #NMSettingVlan:ingress_priority_map or
503 504 505 506 507 508 509 510 511 512 513
 * #NMSettingVlan:egress_priority_map properties.
 */
void
nm_setting_vlan_clear_priorities (NMSettingVlan *setting, NMVlanPriorityMap map)
{
	GSList *list = NULL;

	g_return_if_fail (NM_IS_SETTING_VLAN (setting));
	g_return_if_fail (map == NM_VLAN_INGRESS_MAP || map == NM_VLAN_EGRESS_MAP);

	list = get_map (setting, map);
514
	g_slist_free_full (list, g_free);
515 516 517
	set_map (setting, map, NULL);
}

518
/*****************************************************************************/
519 520 521 522 523 524

static void
nm_setting_vlan_init (NMSettingVlan *setting)
{
}

525 526 527 528
static gboolean
verify (NMSetting *setting, GSList *all_settings, GError **error)
{
	NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting);
529 530 531 532 533 534 535 536 537
	NMSettingConnection *s_con = NULL;
	NMSettingWired *s_wired = NULL;
	GSList *iter;

	for (iter = all_settings; iter; iter = iter->next) {
		if (NM_IS_SETTING_CONNECTION (iter->data))
			s_con = iter->data;
		else if (NM_IS_SETTING_WIRED (iter->data))
			s_wired = iter->data;
538 539
	}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	if (priv->parent) {
		if (nm_utils_is_uuid (priv->parent)) {
			/* If we have an NMSettingConnection:master with slave-type="vlan",
			 * then it must be the same UUID.
			 */
			if (s_con) {
				const char *master = NULL, *slave_type = NULL;

				slave_type = nm_setting_connection_get_slave_type (s_con);
				if (!g_strcmp0 (slave_type, NM_SETTING_VLAN_SETTING_NAME))
					master = nm_setting_connection_get_master (s_con);

				if (master && g_strcmp0 (priv->parent, master) != 0) {
					g_set_error (error,
					             NM_SETTING_VLAN_ERROR,
					             NM_SETTING_VLAN_ERROR_INVALID_PARENT,
556 557
					             _("'%s' value doesn't match '%s=%s'"),
					             priv->parent, NM_SETTING_CONNECTION_MASTER, master);
558
					g_prefix_error (error, "%s.%s: ", NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_PARENT);
559 560 561 562 563 564 565 566
					return FALSE;
				}
			}
		} else if (!nm_utils_iface_valid_name (priv->parent)) {
			/* parent must be either a UUID or an interface name */
			g_set_error (error,
			             NM_SETTING_VLAN_ERROR,
			             NM_SETTING_VLAN_ERROR_INVALID_PROPERTY,
567 568
			             _("'%s' is neither an UUID nor an interface name"),
			             priv->parent);
569
			g_prefix_error (error, "%s.%s: ", NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_PARENT);
570
			return FALSE;
571
		}
572 573 574 575 576 577 578 579
	} else {
		/* If parent is NULL, the parent must be specified via
		 * NMSettingWired:mac-address.
		 */
		if (!s_wired || !nm_setting_wired_get_mac_address (s_wired)) {
			g_set_error (error,
			             NM_SETTING_VLAN_ERROR,
			             NM_SETTING_VLAN_ERROR_MISSING_PROPERTY,
580 581
			             _("property is not specified and neither is '%s:%s'"),
			             NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS);
582
			g_prefix_error (error, "%s.%s: ", NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_PARENT);
583
			return FALSE;
584
		}
585 586
	}

587
	if (priv->flags & ~NM_VLAN_FLAGS_ALL) {
588 589 590 591
		g_set_error_literal (error,
		                     NM_SETTING_VLAN_ERROR,
		                     NM_SETTING_VLAN_ERROR_INVALID_PROPERTY,
		                     _("flags are invalid"));
592
		g_prefix_error (error, "%s.%s: ", NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_FLAGS);
593 594 595
		return FALSE;
	}

596 597 598 599 600 601 602 603 604 605
	if (priv->id >= 4095) {
		g_set_error (error,
		             NM_SETTING_VLAN_ERROR,
		             NM_SETTING_VLAN_ERROR_INVALID_PROPERTY,
		             _("the vlan id must be in range 0-4094 but is %u"),
		             priv->id);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_ID);
		return FALSE;
	}

606 607 608 609 610 611 612 613 614 615 616
	/* If interface_name is specified, it must be a valid interface name. We
	 * don't check that it matches parent and/or id, because we allow
	 * renaming vlans to arbitrary names.
	 */
	return _nm_setting_verify_deprecated_virtual_iface_name (
	         priv->interface_name, TRUE,
	         NM_SETTING_VLAN_SETTING_NAME, NM_SETTING_VLAN_INTERFACE_NAME,
	         NM_SETTING_VLAN_ERROR,
	         NM_SETTING_VLAN_ERROR_INVALID_PROPERTY,
	         NM_SETTING_VLAN_ERROR_MISSING_PROPERTY,
	         all_settings, error);
617 618
}

619 620 621 622 623 624 625
static const char *
get_virtual_iface_name (NMSetting *setting)
{
	return nm_setting_vlan_get_interface_name (NM_SETTING_VLAN (setting));
}

static GSList *
626
priority_stringlist_to_maplist (NMVlanPriorityMap map, GSList *strlist)
627 628 629 630 631 632
{
	GSList *list = NULL, *iter;

	for (iter = strlist; iter; iter = g_slist_next (iter)) {
		PriorityMap *item;

633
		item = priority_map_new_from_str (map, (const char *) iter->data);
634 635 636 637 638 639 640 641 642 643 644 645 646 647
		if (item)
			list = g_slist_prepend (list, item);
	}
	return g_slist_reverse (list);
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMSettingVlan *setting = NM_SETTING_VLAN (object);
	NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting);

	switch (prop_id) {
648 649 650
	case PROP_INTERFACE_NAME:
		g_free (priv->interface_name);
		priv->interface_name = g_value_dup_string (value);
651
		break;
652 653 654 655
	case PROP_PARENT:
		g_free (priv->parent);
		priv->parent = g_value_dup_string (value);
		break;
656 657 658 659 660 661 662
	case PROP_ID:
		priv->id = g_value_get_uint (value);
		break;
	case PROP_FLAGS:
		priv->flags = g_value_get_uint (value);
		break;
	case PROP_INGRESS_PRIORITY_MAP:
663
		g_slist_free_full (priv->ingress_priority_map, g_free);
664
		priv->ingress_priority_map =
665
			priority_stringlist_to_maplist (NM_VLAN_INGRESS_MAP, g_value_get_boxed (value));
666 667
		break;
	case PROP_EGRESS_PRIORITY_MAP:
668
		g_slist_free_full (priv->egress_priority_map, g_free);
669
		priv->egress_priority_map =
670
			priority_stringlist_to_maplist (NM_VLAN_EGRESS_MAP, g_value_get_boxed (value));
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static GSList *
priority_maplist_to_stringlist (GSList *list)
{
	GSList *strlist = NULL, *iter;

	for (iter = list; iter; iter = g_slist_next (iter)) {
		PriorityMap *item = iter->data;

		strlist = g_slist_prepend (strlist, g_strdup_printf ("%d:%d", item->from, item->to));
	}
	return g_slist_reverse (strlist);
}

static void
get_property (GObject *object, guint prop_id,
              GValue *value, GParamSpec *pspec)
{
	NMSettingVlan *setting = NM_SETTING_VLAN (object);
	NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting);

	switch (prop_id) {
699 700
	case PROP_INTERFACE_NAME:
		g_value_set_string (value, priv->interface_name);
701
		break;
702 703 704
	case PROP_PARENT:
		g_value_set_string (value, priv->parent);
		break;
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	case PROP_ID:
		g_value_set_uint (value, priv->id);
		break;
	case PROP_FLAGS:
		g_value_set_uint (value, priv->flags);
		break;
	case PROP_INGRESS_PRIORITY_MAP:
		g_value_take_boxed (value, priority_maplist_to_stringlist (priv->ingress_priority_map));
		break;
	case PROP_EGRESS_PRIORITY_MAP:
		g_value_take_boxed (value, priority_maplist_to_stringlist (priv->egress_priority_map));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
finalize (GObject *object)
{
	NMSettingVlan *setting = NM_SETTING_VLAN (object);
	NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting);

729
	g_free (priv->interface_name);
730
	g_free (priv->parent);
731 732
	g_slist_free_full (priv->ingress_priority_map, g_free);
	g_slist_free_full (priv->egress_priority_map, g_free);
733 734

	G_OBJECT_CLASS (nm_setting_vlan_parent_class)->finalize (object);
735 736 737 738 739 740
}

static void
nm_setting_vlan_class_init (NMSettingVlanClass *setting_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
741
	NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
742 743 744 745 746 747 748

	g_type_class_add_private (setting_class, sizeof (NMSettingVlanPrivate));

	/* virtual methods */
	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
749
	parent_class->verify       = verify;
750 751 752
	parent_class->get_virtual_iface_name = get_virtual_iface_name;

	/* Properties */
753 754 755 756

	/**
	 * NMSettingVlan:interface-name:
	 *
757 758
	 * If given, specifies the kernel name of the VLAN interface. If not given,
	 * a default name will be constructed from the interface described by the
759 760 761
	 * parent interface and the #NMSettingVlan:id property, eg "eth2.1". The
	 * parent interface may be given by the #NMSettingVlan:parent property or by
	 * the #NMSettingWired:mac-address property of an #NMSettingWired setting.
762
	 **/
763
	g_object_class_install_property
764
		(object_class, PROP_INTERFACE_NAME,
765 766
		 g_param_spec_string (NM_SETTING_VLAN_INTERFACE_NAME, "", "",
		                      NULL,
767 768 769
		                      G_PARAM_READWRITE |
		                      G_PARAM_CONSTRUCT |
		                      NM_SETTING_PARAM_INFERRABLE |
770
		                      G_PARAM_STATIC_STRINGS));
771 772 773 774 775 776

	/**
	 * NMSettingVlan:parent:
	 *
	 * If given, specifies the parent interface name or parent connection UUID
	 * from which this VLAN interface should be created.  If this property is
777 778
	 * not specified, the connection must contain an #NMSettingWired setting
	 * with a #NMSettingWired:mac-address property.
779 780 781
	 **/
	g_object_class_install_property
		(object_class, PROP_PARENT,
782 783
		 g_param_spec_string (NM_SETTING_VLAN_PARENT, "", "",
		                      NULL,
784 785 786
		                      G_PARAM_READWRITE |
		                      G_PARAM_CONSTRUCT |
		                      NM_SETTING_PARAM_INFERRABLE |
787
		                      G_PARAM_STATIC_STRINGS));
788 789 790 791

	/**
	 * NMSettingVlan:id:
	 *
792 793
	 * The VLAN identifier that the interface created by this connection should
	 * be assigned.
794
	 **/
795 796
	g_object_class_install_property
		(object_class, PROP_ID,
797
		 g_param_spec_uint (NM_SETTING_VLAN_ID, "", "",
798
		                    0, 4095, 0,
799 800 801
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
802
		                    G_PARAM_STATIC_STRINGS));
803 804 805 806

	/**
	 * NMSettingVlan:flags:
	 *
807 808 809 810
	 * One or more flags which control the behavior and features of the VLAN
	 * interface.  Flags include %NM_VLAN_FLAG_REORDER_HEADERS (reordering of
	 * output packet headers), %NM_VLAN_FLAG_GVRP (use of the GVRP protocol),
	 * and %NM_VLAN_FLAG_LOOSE_BINDING (loose binding of the interface to its
811 812
	 * master device's operating state), %NM_VLAN_FLAG_MVRP (use of the MVRP
	 * protocol).
813
	 **/
814 815
	g_object_class_install_property
		(object_class, PROP_FLAGS,
816
		 g_param_spec_uint (NM_SETTING_VLAN_FLAGS, "", "",
817
		                    0, G_MAXUINT32, 0,
818 819 820
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_INFERRABLE |
821
		                    G_PARAM_STATIC_STRINGS));
822 823 824 825 826

	/**
	 * NMSettingVlan:ingress-priority-map:
	 *
	 * For incoming packets, a list of mappings from 802.1p priorities to Linux
827 828
	 * SKB priorities.  The mapping is given in the format "from:to" where both
	 * "from" and "to" are unsigned integers, ie "7:3".
829
	 **/
830 831
	g_object_class_install_property
		(object_class, PROP_INGRESS_PRIORITY_MAP,
832 833
		 _nm_param_spec_specialized (NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, "", "",
		                             DBUS_TYPE_G_LIST_OF_STRING,
834 835
		                             G_PARAM_READWRITE |
		                             NM_SETTING_PARAM_INFERRABLE |
836
		                             G_PARAM_STATIC_STRINGS));
837 838 839 840 841

	/**
	 * NMSettingVlan:egress-priority-map:
	 *
	 * For outgoing packets, a list of mappings from Linux SKB priorities to
842 843
	 * 802.1p priorities.  The mapping is given in the format "from:to" where
	 * both "from" and "to" are unsigned integers, ie "7:3".
844
	 **/
845 846
	g_object_class_install_property
		(object_class, PROP_EGRESS_PRIORITY_MAP,
847 848
		 _nm_param_spec_specialized (NM_SETTING_VLAN_EGRESS_PRIORITY_MAP, "", "",
		                             DBUS_TYPE_G_LIST_OF_STRING,
849 850
		                             G_PARAM_READWRITE |
		                             NM_SETTING_PARAM_INFERRABLE |
851
		                             G_PARAM_STATIC_STRINGS));
852
}