nm-setting-wireless.c 57.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- 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.
 *
 * Copyright 2007 - 2014 Red Hat, Inc.
 * Copyright 2007 - 2008 Novell, Inc.
 */

23
#include "nm-default.h"
24

25 26
#include "nm-setting-wireless.h"

27 28 29 30
#include <string.h>
#include <net/ethernet.h>

#include "nm-utils.h"
31
#include "nm-common-macros.h"
32 33 34 35 36 37 38 39 40 41 42
#include "nm-utils-private.h"
#include "nm-setting-private.h"

/**
 * SECTION:nm-setting-wireless
 * @short_description: Describes connection properties for 802.11 Wi-Fi networks
 *
 * The #NMSettingWireless object is a #NMSetting subclass that describes properties
 * necessary for connection to 802.11 Wi-Fi networks.
 **/

43
G_DEFINE_TYPE (NMSettingWireless, nm_setting_wireless, NM_TYPE_SETTING)
44 45 46 47

#define NM_SETTING_WIRELESS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_WIRELESS, NMSettingWirelessPrivate))

typedef struct {
48
	GBytes *ssid;
49 50 51
	char *mode;
	char *band;
	guint32 channel;
52
	char *bssid;
53 54
	guint32 rate;
	guint32 tx_power;
55 56
	char *device_mac_address;
	char *cloned_mac_address;
57
	char *generate_mac_address_mask;
58
	GArray *mac_address_blacklist;
59 60 61
	guint32 mtu;
	GSList *seen_bssids;
	gboolean hidden;
62
	guint32 powersave;
63
	NMSettingMacRandomization mac_address_randomization;
64
	guint32 wowl;
65 66 67 68 69 70 71 72 73 74 75 76 77
} NMSettingWirelessPrivate;

enum {
	PROP_0,
	PROP_SSID,
	PROP_MODE,
	PROP_BAND,
	PROP_CHANNEL,
	PROP_BSSID,
	PROP_RATE,
	PROP_TX_POWER,
	PROP_MAC_ADDRESS,
	PROP_CLONED_MAC_ADDRESS,
78
	PROP_GENERATE_MAC_ADDRESS_MASK,
79 80 81 82
	PROP_MAC_ADDRESS_BLACKLIST,
	PROP_MTU,
	PROP_SEEN_BSSIDS,
	PROP_HIDDEN,
83
	PROP_POWERSAVE,
84
	PROP_MAC_ADDRESS_RANDOMIZATION,
85
	PROP_WAKE_ON_WLAN,
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 224 225 226 227 228 229 230 231 232 233 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

	LAST_PROP
};

static gboolean
match_cipher (const char *cipher,
              const char *expected,
              guint32 wpa_flags,
              guint32 rsn_flags,
              guint32 flag)
{
	if (strcmp (cipher, expected) != 0)
		return FALSE;

	if (!(wpa_flags & flag) && !(rsn_flags & flag))
		return FALSE;

	return TRUE;
}

/**
 * nm_setting_wireless_ap_security_compatible:
 * @s_wireless: a #NMSettingWireless
 * @s_wireless_sec: a #NMSettingWirelessSecurity or %NULL
 * @ap_flags: the %NM80211ApFlags of the given access point
 * @ap_wpa: the %NM80211ApSecurityFlags of the given access point's WPA
 * capabilities
 * @ap_rsn: the %NM80211ApSecurityFlags of the given access point's WPA2/RSN
 * capabilities
 * @ap_mode: the 802.11 mode of the AP, either Ad-Hoc or Infrastructure
 *
 * Given a #NMSettingWireless and an optional #NMSettingWirelessSecurity,
 * determine if the configuration given by the settings is compatible with
 * the security of an access point using that access point's capability flags
 * and mode.  Useful for clients that wish to filter a set of connections
 * against a set of access points and determine which connections are
 * compatible with which access points.
 *
 * Returns: %TRUE if the given settings are compatible with the access point's
 * security flags and mode, %FALSE if they are not.
 */
gboolean
nm_setting_wireless_ap_security_compatible (NMSettingWireless *s_wireless,
                                            NMSettingWirelessSecurity *s_wireless_sec,
                                            NM80211ApFlags ap_flags,
                                            NM80211ApSecurityFlags ap_wpa,
                                            NM80211ApSecurityFlags ap_rsn,
                                            NM80211Mode ap_mode)
{
	const char *key_mgmt = NULL, *cipher;
	guint32 num, i;
	gboolean found = FALSE;

	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (s_wireless), FALSE);

	if (!s_wireless_sec) {
		if (   (ap_flags & NM_802_11_AP_FLAGS_PRIVACY)
		    || (ap_wpa != NM_802_11_AP_SEC_NONE)
		    || (ap_rsn != NM_802_11_AP_SEC_NONE))
			return FALSE;
		return TRUE;
	}

	key_mgmt = nm_setting_wireless_security_get_key_mgmt (s_wireless_sec);
	if (!key_mgmt)
		return FALSE;

	/* Static WEP */
	if (!strcmp (key_mgmt, "none")) {
		if (   !(ap_flags & NM_802_11_AP_FLAGS_PRIVACY)
		    || (ap_wpa != NM_802_11_AP_SEC_NONE)
		    || (ap_rsn != NM_802_11_AP_SEC_NONE))
			return FALSE;
		return TRUE;
	}

	/* Adhoc WPA */
	if (!strcmp (key_mgmt, "wpa-none")) {
		if (ap_mode != NM_802_11_MODE_ADHOC)
			return FALSE;
		/* FIXME: validate ciphers if they're in the beacon */
		return TRUE;
	}

	/* Adhoc WPA2 (ie, RSN IBSS) */
	if (ap_mode == NM_802_11_MODE_ADHOC) {
		if (strcmp (key_mgmt, "wpa-psk"))
			return FALSE;

		/* Ensure the AP has RSN PSK capability */
		if (!(ap_rsn & NM_802_11_AP_SEC_KEY_MGMT_PSK))
			return FALSE;

		/* Fall through and check ciphers in generic WPA-PSK code */
	}

	/* Dynamic WEP or LEAP */
	if (!strcmp (key_mgmt, "ieee8021x")) {
		if (!(ap_flags & NM_802_11_AP_FLAGS_PRIVACY))
			return FALSE;

		/* If the AP is advertising a WPA IE, make sure it supports WEP ciphers */
		if (ap_wpa != NM_802_11_AP_SEC_NONE) {
			if (!(ap_wpa & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
				return FALSE;

			/* quick check; can't use AP if it doesn't support at least one
			 * WEP cipher in both pairwise and group suites.
			 */
			if (   !(ap_wpa & (NM_802_11_AP_SEC_PAIR_WEP40 | NM_802_11_AP_SEC_PAIR_WEP104))
			    || !(ap_wpa & (NM_802_11_AP_SEC_GROUP_WEP40 | NM_802_11_AP_SEC_GROUP_WEP104)))
				return FALSE;

			/* Match at least one pairwise cipher with AP's capability if the
			 * wireless-security setting explicitly lists pairwise ciphers
			 */
			num = nm_setting_wireless_security_get_num_pairwise (s_wireless_sec);
			for (i = 0, found = FALSE; i < num; i++) {
				cipher = nm_setting_wireless_security_get_pairwise (s_wireless_sec, i);
				if ((found = match_cipher (cipher, "wep40", ap_wpa, ap_wpa, NM_802_11_AP_SEC_PAIR_WEP40)))
					break;
				if ((found = match_cipher (cipher, "wep104", ap_wpa, ap_wpa, NM_802_11_AP_SEC_PAIR_WEP104)))
					break;
			}
			if (!found && num)
				return FALSE;

			/* Match at least one group cipher with AP's capability if the
			 * wireless-security setting explicitly lists group ciphers
			 */
			num = nm_setting_wireless_security_get_num_groups (s_wireless_sec);
			for (i = 0, found = FALSE; i < num; i++) {
				cipher = nm_setting_wireless_security_get_group (s_wireless_sec, i);
				if ((found = match_cipher (cipher, "wep40", ap_wpa, ap_wpa, NM_802_11_AP_SEC_GROUP_WEP40)))
					break;
				if ((found = match_cipher (cipher, "wep104", ap_wpa, ap_wpa, NM_802_11_AP_SEC_GROUP_WEP104)))
					break;
			}
			if (!found && num)
				return FALSE;
		}
		return TRUE;
	}

	/* WPA[2]-PSK and WPA[2] Enterprise */
	if (   !strcmp (key_mgmt, "wpa-psk")
	    || !strcmp (key_mgmt, "wpa-eap")) {

		if (!strcmp (key_mgmt, "wpa-psk")) {
			if (   !(ap_wpa & NM_802_11_AP_SEC_KEY_MGMT_PSK)
			    && !(ap_rsn & NM_802_11_AP_SEC_KEY_MGMT_PSK))
				return FALSE;
		} else if (!strcmp (key_mgmt, "wpa-eap")) {
			if (   !(ap_wpa & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
			    && !(ap_rsn & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
				return FALSE;
		}

		// FIXME: should handle WPA and RSN separately here to ensure that
		// if the Connection only uses WPA we don't match a cipher against
		// the AP's RSN IE instead

		/* Match at least one pairwise cipher with AP's capability if the
		 * wireless-security setting explicitly lists pairwise ciphers
		 */
		num = nm_setting_wireless_security_get_num_pairwise (s_wireless_sec);
		for (i = 0, found = FALSE; i < num; i++) {
			cipher = nm_setting_wireless_security_get_pairwise (s_wireless_sec, i);
			if ((found = match_cipher (cipher, "tkip", ap_wpa, ap_rsn, NM_802_11_AP_SEC_PAIR_TKIP)))
				break;
			if ((found = match_cipher (cipher, "ccmp", ap_wpa, ap_rsn, NM_802_11_AP_SEC_PAIR_CCMP)))
				break;
		}
		if (!found && num)
			return FALSE;

		/* Match at least one group cipher with AP's capability if the
		 * wireless-security setting explicitly lists group ciphers
		 */
		num = nm_setting_wireless_security_get_num_groups (s_wireless_sec);
		for (i = 0, found = FALSE; i < num; i++) {
			cipher = nm_setting_wireless_security_get_group (s_wireless_sec, i);

			if ((found = match_cipher (cipher, "wep40", ap_wpa, ap_rsn, NM_802_11_AP_SEC_GROUP_WEP40)))
				break;
			if ((found = match_cipher (cipher, "wep104", ap_wpa, ap_rsn, NM_802_11_AP_SEC_GROUP_WEP104)))
				break;
			if ((found = match_cipher (cipher, "tkip", ap_wpa, ap_rsn, NM_802_11_AP_SEC_GROUP_TKIP)))
				break;
			if ((found = match_cipher (cipher, "ccmp", ap_wpa, ap_rsn, NM_802_11_AP_SEC_GROUP_CCMP)))
				break;
		}
		if (!found && num)
			return FALSE;

		return TRUE;
	}

	return FALSE;
}

/**
 * nm_setting_wireless_new:
 *
 * Creates a new #NMSettingWireless object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingWireless object
 **/
NMSetting *
nm_setting_wireless_new (void)
{
	return (NMSetting *) g_object_new (NM_TYPE_SETTING_WIRELESS, NULL);
}

/**
 * nm_setting_wireless_get_ssid:
 * @setting: the #NMSettingWireless
 *
304
 * Returns: (transfer none): the #NMSettingWireless:ssid property of the setting
305
 **/
306
GBytes *
307 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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
nm_setting_wireless_get_ssid (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->ssid;
}

/**
 * nm_setting_wireless_get_mode:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:mode property of the setting
 **/
const char *
nm_setting_wireless_get_mode (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->mode;
}

/**
 * nm_setting_wireless_get_band:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:band property of the setting
 **/
const char *
nm_setting_wireless_get_band (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->band;
}

/**
 * nm_setting_wireless_get_channel:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:channel property of the setting
 **/
guint32
nm_setting_wireless_get_channel (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->channel;
}

/**
 * nm_setting_wireless_get_bssid:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:bssid property of the setting
 **/
362
const char *
363 364 365 366 367 368 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
nm_setting_wireless_get_bssid (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->bssid;
}

/**
 * nm_setting_wireless_get_rate:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:rate property of the setting
 **/
guint32
nm_setting_wireless_get_rate (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->rate;
}

/**
 * nm_setting_wireless_get_tx_power:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:tx-power property of the setting
 **/
guint32
nm_setting_wireless_get_tx_power (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->tx_power;
}

/**
 * nm_setting_wireless_get_mac_address:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:mac-address property of the setting
 **/
404
const char *
405 406 407 408 409 410 411 412 413 414 415 416 417
nm_setting_wireless_get_mac_address (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->device_mac_address;
}

/**
 * nm_setting_wireless_get_cloned_mac_address:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:cloned-mac-address property of the setting
 **/
418
const char *
419 420 421 422 423 424 425
nm_setting_wireless_get_cloned_mac_address (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->cloned_mac_address;
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/**
 * nm_setting_wireless_get_generate_mac_address_mask:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:generate-mac-address-mask property of the setting
 *
 * Since: 1.4
 **/
const char *
nm_setting_wireless_get_generate_mac_address_mask (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->generate_mac_address_mask;
}

442 443 444 445
/**
 * nm_setting_wireless_get_mac_address_blacklist:
 * @setting: the #NMSettingWireless
 *
446
 * Returns: the #NMSettingWireless:mac-address-blacklist property of the setting
447
 **/
448
const char * const *
449 450
nm_setting_wireless_get_mac_address_blacklist (NMSettingWireless *setting)
{
451 452
	NMSettingWirelessPrivate *priv;

453 454
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

455 456
	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
	return (const char * const *) priv->mac_address_blacklist->data;
457 458 459 460 461 462 463 464 465 466 467 468 469
}

/**
 * nm_setting_wireless_get_num_mac_blacklist_items:
 * @setting: the #NMSettingWireless
 *
 * Returns: the number of blacklisted MAC addresses
 **/
guint32
nm_setting_wireless_get_num_mac_blacklist_items (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

470
	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->mac_address_blacklist->len;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

/**
 * nm_setting_wireless_get_mac_blacklist_item:
 * @setting: the #NMSettingWireless
 * @idx: the zero-based index of the MAC address entry
 *
 * Returns: the blacklisted MAC address string (hex-digits-and-colons notation)
 * at index @idx
 **/
const char *
nm_setting_wireless_get_mac_blacklist_item (NMSettingWireless *setting, guint32 idx)
{
	NMSettingWirelessPrivate *priv;

	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
489
	g_return_val_if_fail (idx <= priv->mac_address_blacklist->len, NULL);
490

491
	return g_array_index (priv->mac_address_blacklist, const char *, idx);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
}

/**
 * nm_setting_wireless_add_mac_blacklist_item:
 * @setting: the #NMSettingWireless
 * @mac: the MAC address string (hex-digits-and-colons notation) to blacklist
 *
 * Adds a new MAC address to the #NMSettingWireless:mac-address-blacklist property.
 *
 * Returns: %TRUE if the MAC address was added; %FALSE if the MAC address
 * is invalid or was already present
 **/
gboolean
nm_setting_wireless_add_mac_blacklist_item (NMSettingWireless *setting, const char *mac)
{
	NMSettingWirelessPrivate *priv;
508
	const char *candidate;
509
	int i;
510 511 512 513

	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), FALSE);
	g_return_val_if_fail (mac != NULL, FALSE);

514
	if (!nm_utils_hwaddr_valid (mac, ETH_ALEN))
515 516 517
		return FALSE;

	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
518
	for (i = 0; i < priv->mac_address_blacklist->len; i++) {
519 520
		candidate = g_array_index (priv->mac_address_blacklist, char *, i);
		if (nm_utils_hwaddr_matches (mac, -1, candidate, -1))
521 522 523
			return FALSE;
	}

524
	mac = nm_utils_hwaddr_canonical (mac, ETH_ALEN);
525
	g_array_append_val (priv->mac_address_blacklist, mac);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	g_object_notify (G_OBJECT (setting), NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
	return TRUE;
}

/**
 * nm_setting_wireless_remove_mac_blacklist_item:
 * @setting: the #NMSettingWireless
 * @idx: index number of the MAC address
 *
 * Removes the MAC address at index @idx from the blacklist.
 **/
void
nm_setting_wireless_remove_mac_blacklist_item (NMSettingWireless *setting, guint32 idx)
{
	NMSettingWirelessPrivate *priv;

	g_return_if_fail (NM_IS_SETTING_WIRELESS (setting));

	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
545
	g_return_if_fail (idx < priv->mac_address_blacklist->len);
546

547
	g_array_remove_index (priv->mac_address_blacklist, idx);
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	g_object_notify (G_OBJECT (setting), NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
}

/**
 * nm_setting_wireless_remove_mac_blacklist_item_by_value:
 * @setting: the #NMSettingWireless
 * @mac: the MAC address string (hex-digits-and-colons notation) to remove from
 * the blacklist
 *
 * Removes the MAC address @mac from the blacklist.
 *
 * Returns: %TRUE if the MAC address was found and removed; %FALSE if it was not.
 **/
gboolean
nm_setting_wireless_remove_mac_blacklist_item_by_value (NMSettingWireless *setting, const char *mac)
{
	NMSettingWirelessPrivate *priv;
565
	const char *candidate;
566
	int i;
567 568 569 570 571

	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), FALSE);
	g_return_val_if_fail (mac != NULL, FALSE);

	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
572
	for (i = 0; i < priv->mac_address_blacklist->len; i++) {
573 574
		candidate = g_array_index (priv->mac_address_blacklist, char *, i);
		if (!nm_utils_hwaddr_matches (mac, -1, candidate, -1)) {
575
			g_array_remove_index (priv->mac_address_blacklist, i);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
			g_object_notify (G_OBJECT (setting), NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
			return TRUE;
		}
	}
	return FALSE;
}

/**
 * nm_setting_wireless_clear_mac_blacklist_items:
 * @setting: the #NMSettingWireless
 *
 * Removes all blacklisted MAC addresses.
 **/
void
nm_setting_wireless_clear_mac_blacklist_items (NMSettingWireless *setting)
{
	g_return_if_fail (NM_IS_SETTING_WIRELESS (setting));

594
	g_array_set_size (NM_SETTING_WIRELESS_GET_PRIVATE (setting)->mac_address_blacklist, 0);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	g_object_notify (G_OBJECT (setting), NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
}

/**
 * nm_setting_wireless_get_mtu:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:mtu property of the setting
 **/
guint32
nm_setting_wireless_get_mtu (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->mtu;
}

/**
 * nm_setting_wireless_get_hidden:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:hidden property of the setting
 **/
gboolean
nm_setting_wireless_get_hidden (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), FALSE);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->hidden;
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
/**
 * nm_setting_wireless_get_powersave:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:powersave property of the setting
 *
 * Since: 1.2
 **/
guint32
nm_setting_wireless_get_powersave (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->powersave;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
/**
 * nm_setting_wireless_get_mac_address_randomization:
 * @setting: the #NMSettingWireless
 *
 * Returns: the #NMSettingWireless:mac-address-randomization property of the
 * setting
 *
 * Since: 1.2
 **/
NMSettingMacRandomization
nm_setting_wireless_get_mac_address_randomization (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->mac_address_randomization;
}

659 660 661 662 663 664 665 666 667 668 669 670 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
/**
 * nm_setting_wireless_add_seen_bssid:
 * @setting: the #NMSettingWireless
 * @bssid: the new BSSID to add to the list
 *
 * Adds a new Wi-Fi AP's BSSID to the previously seen BSSID list of the setting.
 * NetworkManager now tracks previously seen BSSIDs internally so this function
 * no longer has much use. Actually, changes you make using this function will
 * not be preserved.
 *
 * Returns: %TRUE if @bssid was already known, %FALSE if not
 **/
gboolean
nm_setting_wireless_add_seen_bssid (NMSettingWireless *setting,
                                    const char *bssid)
{
	NMSettingWirelessPrivate *priv;
	char *lower_bssid;
	GSList *iter;
	gboolean found = FALSE;

	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), FALSE);
	g_return_val_if_fail (bssid != NULL, FALSE);

	lower_bssid = g_ascii_strdown (bssid, -1);
	if (!lower_bssid)
		return FALSE;

	priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);

	for (iter = priv->seen_bssids; iter; iter = iter->next) {
		if (!strcmp ((char *) iter->data, lower_bssid)) {
			found = TRUE;
			break;
		}
	}

	if (!found) {
		priv->seen_bssids = g_slist_prepend (priv->seen_bssids, lower_bssid);
		g_object_notify (G_OBJECT (setting), NM_SETTING_WIRELESS_SEEN_BSSIDS);
	} else
		g_free (lower_bssid);

	return !found;
}

/**
 * nm_setting_wireless_get_num_seen_bssids:
 * @setting: the #NMSettingWireless
 *
 * Returns: the number of BSSIDs in the previously seen BSSID list
 **/
guint32
nm_setting_wireless_get_num_seen_bssids (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), 0);

	return g_slist_length (NM_SETTING_WIRELESS_GET_PRIVATE (setting)->seen_bssids);
}

/**
 * nm_setting_wireless_get_seen_bssid:
 * @setting: the #NMSettingWireless
 * @i: index of a BSSID in the previously seen BSSID list
 *
 * Returns: the BSSID at index @i
 **/
const char *
nm_setting_wireless_get_seen_bssid (NMSettingWireless *setting,
                                    guint32 i)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL);

	return (const char *) g_slist_nth_data (NM_SETTING_WIRELESS_GET_PRIVATE (setting)->seen_bssids, i);
}

static gboolean
736
verify (NMSetting *setting, NMConnection *connection, GError **error)
737 738 739 740 741
{
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
	const char *valid_modes[] = { NM_SETTING_WIRELESS_MODE_INFRA, NM_SETTING_WIRELESS_MODE_ADHOC, NM_SETTING_WIRELESS_MODE_AP, NULL };
	const char *valid_bands[] = { "a", "bg", NULL };
	GSList *iter;
742
	int i;
743
	gsize length;
744
	GError *local = NULL;
745 746 747

	if (!priv->ssid) {
		g_set_error_literal (error,
748 749
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_MISSING_PROPERTY,
750 751 752 753 754
		                     _("property is missing"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID);
		return FALSE;
	}

755 756
	length = g_bytes_get_size (priv->ssid);
	if (length == 0 || length > 32) {
757
		g_set_error_literal (error,
758 759
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
760 761 762 763 764
		                     _("SSID length is out of range <1-32> bytes"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID);
		return FALSE;
	}

765
	if (priv->mode && !g_strv_contains (valid_modes, priv->mode)) {
766
		g_set_error (error,
767 768
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
769 770 771 772 773 774
		             _("'%s' is not a valid Wi-Fi mode"),
		             priv->mode);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MODE);
		return FALSE;
	}

775
	if (priv->band && !g_strv_contains (valid_bands, priv->band)) {
776
		g_set_error (error,
777 778
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
779 780 781 782 783 784 785 786
		             _("'%s' is not a valid band"),
		             priv->band);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BAND);
		return FALSE;
	}

	if (priv->channel && !priv->band) {
		g_set_error (error,
787 788 789 790 791
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_MISSING_PROPERTY,
		             _("'%s' requires setting '%s' property"),
		             NM_SETTING_WIRELESS_CHANNEL, NM_SETTING_WIRELESS_BAND);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BAND);
792 793 794 795 796 797
		return FALSE;
	}

	if (priv->channel) {
		if (!nm_utils_wifi_is_channel_valid (priv->channel, priv->band)) {
			g_set_error (error,
798 799
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
800 801 802 803 804 805 806
			             _("'%d' is not a valid channel"),
			             priv->channel);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CHANNEL);
			return FALSE;
		}
	}

807
	if (priv->bssid && !nm_utils_hwaddr_valid (priv->bssid, ETH_ALEN)) {
808
		g_set_error_literal (error,
809 810
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
811 812 813 814 815
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BSSID);
		return FALSE;
	}

816
	if (priv->device_mac_address && !nm_utils_hwaddr_valid (priv->device_mac_address, ETH_ALEN)) {
817
		g_set_error_literal (error,
818 819
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
820 821 822 823 824
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS);
		return FALSE;
	}

825 826 827
	if (   priv->cloned_mac_address
	    && !NM_CLONED_MAC_IS_SPECIAL (priv->cloned_mac_address)
	    && !nm_utils_hwaddr_valid (priv->cloned_mac_address, ETH_ALEN)) {
828
		g_set_error_literal (error,
829 830
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
831 832 833 834 835
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS);
		return FALSE;
	}

836 837 838 839 840 841 842 843 844 845 846 847 848 849
	/* generate-mac-address-mask only makes sense with cloned-mac-address "random" or
	 * "stable". Still, let's not be so strict about that and accept the value
	 * even if it is unused. */
	if (!_nm_utils_generate_mac_address_mask_parse (priv->generate_mac_address_mask,
	                                                NULL, NULL, NULL, &local)) {
		g_set_error_literal (error,
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
		                     local->message);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_GENERATE_MAC_ADDRESS_MASK);
		g_error_free (local);
		return FALSE;
	}

850 851 852 853
	for (i = 0; i < priv->mac_address_blacklist->len; i++) {
		const char *mac = g_array_index (priv->mac_address_blacklist, const char *, i);

		if (!nm_utils_hwaddr_valid (mac, ETH_ALEN)) {
854
			g_set_error (error,
855 856
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
857
			             _("'%s' is not a valid MAC address"),
858
			             mac);
859 860 861 862 863 864
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
			return FALSE;
		}
	}

	for (iter = priv->seen_bssids; iter; iter = iter->next) {
865
		if (!nm_utils_hwaddr_valid (iter->data, ETH_ALEN)) {
866
			g_set_error (error,
867 868
			             NM_CONNECTION_ERROR,
			             NM_CONNECTION_ERROR_INVALID_PROPERTY,
869 870 871 872 873 874 875
			             _("'%s' is not a valid MAC address"),
			             (const char *) iter->data);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SEEN_BSSIDS);
			return FALSE;
		}
	}

876 877 878 879 880 881 882 883 884 885 886 887
	if (!NM_IN_SET (priv->mac_address_randomization,
	                NM_SETTING_MAC_RANDOMIZATION_DEFAULT,
	                NM_SETTING_MAC_RANDOMIZATION_NEVER,
	                NM_SETTING_MAC_RANDOMIZATION_ALWAYS)) {
		g_set_error (error,
		             NM_CONNECTION_ERROR,
		             NM_CONNECTION_ERROR_INVALID_PROPERTY,
		             _("invalid value"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION);
		return FALSE;
	}

888 889 890 891 892 893 894 895 896 897
	if (NM_FLAGS_ANY (priv->wowl, NM_SETTING_WIRELESS_WAKE_ON_WLAN_EXCLUSIVE_FLAGS)) {
		if (!nm_utils_is_power_of_two (priv->wowl)) {
			g_set_error_literal (error,
			                     NM_CONNECTION_ERROR,
			                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
			                     _("Wake-on-WLAN mode 'default' and 'ignore' are exclusive flags"));
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME,
			                NM_SETTING_WIRELESS_WAKE_ON_WLAN);
			return FALSE;
		}
898
	} else if (NM_FLAGS_ANY (priv->wowl, ~NM_SETTING_WIRELESS_WAKE_ON_WLAN_ALL)) {
899 900 901 902 903 904 905 906 907
		g_set_error_literal (error,
		                     NM_CONNECTION_ERROR,
		                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
		                     _("Wake-on-WLAN trying to set unknown flag"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME,
		                NM_SETTING_WIRELESS_WAKE_ON_WLAN);
		return FALSE;
	}

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	/* from here on, check for NM_SETTING_VERIFY_NORMALIZABLE conditions. */

	if (priv->cloned_mac_address) {
		if (   priv->mac_address_randomization == NM_SETTING_MAC_RANDOMIZATION_ALWAYS
		    && nm_streq (priv->cloned_mac_address, "random"))
			goto mac_addr_rand_ok;
		if (   priv->mac_address_randomization == NM_SETTING_MAC_RANDOMIZATION_NEVER
		    && nm_streq (priv->cloned_mac_address, "permanent"))
			goto mac_addr_rand_ok;
		if (priv->mac_address_randomization == NM_SETTING_MAC_RANDOMIZATION_DEFAULT)
			goto mac_addr_rand_ok;
	} else if (priv->mac_address_randomization == NM_SETTING_MAC_RANDOMIZATION_DEFAULT)
		goto mac_addr_rand_ok;
	g_set_error (error,
	             NM_CONNECTION_ERROR,
	             NM_CONNECTION_ERROR_INVALID_PROPERTY,
	             _("conflicting value of mac-address-randomization and cloned-mac-address"));
	g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS);
	return NM_SETTING_VERIFY_NORMALIZABLE;
mac_addr_rand_ok:

929 930 931
	return TRUE;
}

932 933 934 935 936 937
static gboolean
compare_property (NMSetting *setting,
                  NMSetting *other,
                  const GParamSpec *prop_spec,
                  NMSettingCompareFlags flags)
{
938
	NMSettingClass *setting_class;
939 940 941 942 943 944

	if (nm_streq (prop_spec->name, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS)) {
		return nm_streq0 (NM_SETTING_WIRELESS_GET_PRIVATE (setting)->cloned_mac_address,
		                  NM_SETTING_WIRELESS_GET_PRIVATE (other)->cloned_mac_address);
	}

945 946
	setting_class = NM_SETTING_CLASS (nm_setting_wireless_parent_class);
	return setting_class->compare_property (setting, other, prop_spec, flags);
947 948 949 950
}

/*****************************************************************************/

951
static GVariant *
952 953
nm_setting_wireless_get_security (NMSetting    *setting,
                                  NMConnection *connection,
954
                                  const char   *property_name)
955
{
956 957 958 959
	if (nm_connection_get_setting_wireless_security (connection))
		return g_variant_new_string (NM_SETTING_WIRELESS_SECURITY_SETTING_NAME);
	else
		return NULL;
960 961
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
/**
 * nm_setting_wireless_get_wake_on_wlan:
 * @setting: the #NMSettingWireless
 *
 * Returns the Wake-on-WLAN options enabled for the connection
 *
 * Returns: the Wake-on-WLAN options
 *
 * Since: 1.12
 */
NMSettingWirelessWakeOnWLan
nm_setting_wireless_get_wake_on_wlan (NMSettingWireless *setting)
{
	g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NM_SETTING_WIRELESS_WAKE_ON_WLAN_NONE);

	return NM_SETTING_WIRELESS_GET_PRIVATE (setting)->wowl;
}

980 981 982 983 984 985
static void
clear_blacklist_item (char **item_p)
{
	g_free (*item_p);
}

986 987 988
static void
nm_setting_wireless_init (NMSettingWireless *setting)
{
989 990 991 992 993
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);

	/* We use GArray rather than GPtrArray so it will automatically be NULL-terminated */
	priv->mac_address_blacklist = g_array_new (TRUE, FALSE, sizeof (char *));
	g_array_set_clear_func (priv->mac_address_blacklist, (GDestroyNotify) clear_blacklist_item);
994 995 996 997 998 999 1000 1001 1002 1003 1004
}

static void
finalize (GObject *object)
{
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (object);

	g_free (priv->mode);
	g_free (priv->band);

	if (priv->ssid)
1005
		g_bytes_unref (priv->ssid);
1006 1007 1008
	g_free (priv->bssid);
	g_free (priv->device_mac_address);
	g_free (priv->cloned_mac_address);
1009
	g_free (priv->generate_mac_address_mask);
1010
	g_array_unref (priv->mac_address_blacklist);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	g_slist_free_full (priv->seen_bssids, g_free);

	G_OBJECT_CLASS (nm_setting_wireless_parent_class)->finalize (object);
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (object);
1021 1022
	const char * const *blacklist;
	const char *mac;
1023
	gboolean bool_val;
1024
	int i;
1025 1026 1027 1028

	switch (prop_id) {
	case PROP_SSID:
		if (priv->ssid)
1029
			g_bytes_unref (priv->ssid);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		priv->ssid = g_value_dup_boxed (value);
		break;
	case PROP_MODE:
		g_free (priv->mode);
		priv->mode = g_value_dup_string (value);
		break;
	case PROP_BAND:
		g_free (priv->band);
		priv->band = g_value_dup_string (value);
		break;
	case PROP_CHANNEL:
		priv->channel = g_value_get_uint (value);
		break;
	case PROP_BSSID:
1044 1045
		g_free (priv->bssid);
		priv->bssid = g_value_dup_string (value);
1046 1047 1048 1049 1050 1051 1052 1053
		break;
	case PROP_RATE:
		priv->rate = g_value_get_uint (value);
		break;
	case PROP_TX_POWER:
		priv->tx_power = g_value_get_uint (value);
		break;
	case PROP_MAC_ADDRESS:
1054
		g_free (priv->device_mac_address);
1055 1056
		priv->device_mac_address = _nm_utils_hwaddr_canonical_or_invalid (g_value_get_string (value),
		                                                                  ETH_ALEN);
1057 1058
		break;
	case PROP_CLONED_MAC_ADDRESS:
1059
		bool_val = !!priv->cloned_mac_address;
1060
		g_free (priv->cloned_mac_address);
1061 1062
		priv->cloned_mac_address = _nm_utils_hwaddr_canonical_or_invalid (g_value_get_string (value),
		                                                                  ETH_ALEN);
1063 1064 1065 1066 1067 1068 1069 1070
		if (bool_val && !priv->cloned_mac_address) {
			/* cloned-mac-address was set before but was now explicitly cleared.
			 * In this case, we also clear mac-address-randomization flag */
			if (priv->mac_address_randomization != NM_SETTING_MAC_RANDOMIZATION_DEFAULT) {
				priv->mac_address_randomization = NM_SETTING_MAC_RANDOMIZATION_DEFAULT;
				g_object_notify (object, NM_SETTING_WIRELESS_MAC_ADDRESS);
			}
		}
1071
		break;
1072 1073 1074 1075
	case PROP_GENERATE_MAC_ADDRESS_MASK:
		g_free (priv->generate_mac_address_mask);
		priv->generate_mac_address_mask = g_value_dup_string (value);
		break;
1076
	case PROP_MAC_ADDRESS_BLACKLIST:
1077
		blacklist = g_value_get_boxed (value);
1078
		g_array_set_size (priv->mac_address_blacklist, 0);
1079 1080 1081 1082 1083
		if (blacklist && *blacklist) {
			for (i = 0; blacklist[i]; i++) {
				mac = _nm_utils_hwaddr_canonical_or_invalid (blacklist[i], ETH_ALEN);
				g_array_append_val (priv->mac_address_blacklist, mac);
			}
1084
		}
1085 1086 1087 1088 1089 1090
		break;
	case PROP_MTU:
		priv->mtu = g_value_get_uint (value);
		break;
	case PROP_SEEN_BSSIDS:
		g_slist_free_full (priv->seen_bssids, g_free);
1091
		priv->seen_bssids = _nm_utils_strv_to_slist (g_value_get_boxed (value), TRUE);
1092 1093 1094 1095
		break;
	case PROP_HIDDEN:
		priv->hidden = g_value_get_boolean (value);
		break;
1096 1097 1098
	case PROP_POWERSAVE:
		priv->powersave = g_value_get_uint (value);
		break;
1099 1100 1101
	case PROP_MAC_ADDRESS_RANDOMIZATION:
		priv->mac_address_randomization = g_value_get_uint (value);
		break;
1102 1103 1104
	case PROP_WAKE_ON_WLAN:
		priv->wowl = g_value_get_uint (value);
		break;
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	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)
{
	NMSettingWireless *setting = NM_SETTING_WIRELESS (object);
1116
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (object);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

	switch (prop_id) {
	case PROP_SSID:
		g_value_set_boxed (value, nm_setting_wireless_get_ssid (setting));
		break;
	case PROP_MODE:
		g_value_set_string (value, nm_setting_wireless_get_mode (setting));
		break;
	case PROP_BAND:
		g_value_set_string (value, nm_setting_wireless_get_band (setting));
		break;
	case PROP_CHANNEL:
		g_value_set_uint (value, nm_setting_wireless_get_channel (setting));
		break;
	case PROP_BSSID:
1132
		g_value_set_string (value, nm_setting_wireless_get_bssid (setting));
1133 1134 1135 1136 1137 1138 1139 1140
		break;
	case PROP_RATE:
		g_value_set_uint (value, nm_setting_wireless_get_rate (setting));
		break;
	case PROP_TX_POWER:
		g_value_set_uint (value, nm_setting_wireless_get_tx_power (setting));
		break;
	case PROP_MAC_ADDRESS:
1141
		g_value_set_string (value, nm_setting_wireless_get_mac_address (setting));
1142 1143
		break;
	case PROP_CLONED_MAC_ADDRESS:
1144
		g_value_set_string (value, nm_setting_wireless_get_cloned_mac_address (setting));
1145
		break;
1146 1147 1148
	case PROP_GENERATE_MAC_ADDRESS_MASK:
		g_value_set_string (value, nm_setting_wireless_get_generate_mac_address_mask (setting));
		break;
1149
	case PROP_MAC_ADDRESS_BLACKLIST:
1150
		g_value_set_boxed (value, (char **) priv->mac_address_blacklist->data);
1151 1152 1153 1154 1155
		break;
	case PROP_MTU:
		g_value_set_uint (value, nm_setting_wireless_get_mtu (setting));
		break;
	case PROP_SEEN_BSSIDS:
1156
		g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->seen_bssids, TRUE));
1157 1158 1159 1160
		break;
	case PROP_HIDDEN:
		g_value_set_boolean (value, nm_setting_wireless_get_hidden (setting));
		break;
1161 1162 1163
	case PROP_POWERSAVE:
		g_value_set_uint (value, nm_setting_wireless_get_powersave (setting));
		break;
1164 1165 1166
	case PROP_MAC_ADDRESS_RANDOMIZATION:
		g_value_set_uint (value, nm_setting_wireless_get_mac_address_randomization (setting));
		break;
1167 1168 1169
	case PROP_WAKE_ON_WLAN:
		g_value_set_uint (value, nm_setting_wireless_get_wake_on_wlan (setting));
		break;
1170 1171 1172 1173 1174 1175 1176
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
1177
nm_setting_wireless_class_init (NMSettingWirelessClass *klass)
1178
{
1179 1180
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
1181
	GArray *properties_override = _nm_sett_info_property_override_create_array ();
1182

1183
	g_type_class_add_private (klass, sizeof (NMSettingWirelessPrivate));
1184 1185 1186 1187

	object_class->set_property = set_property;
	object_class->get_property = get_property;
	object_class->finalize     = finalize;
1188 1189

	setting_class->verify           = verify;
1190
	setting_class->compare_property = compare_property;
1191 1192 1193 1194 1195 1196

	/**
	 * NMSettingWireless:ssid:
	 *
	 * SSID of the Wi-Fi network. Must be specified.
	 **/
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	/* ---keyfile---
	 * property: ssid
	 * format: string (or decimal-byte list - obsolete)
	 * description: SSID of Wi-Fi network.
	 * example: ssid=Quick Net
	 * ---end---
	 * ---ifcfg-rh---
	 * property: ssid
	 * variable: ESSID
	 * description: SSID of Wi-Fi network.
	 * example: ESSID="Quick Net"
	 * ---end---
	 */
1210 1211
	g_object_class_install_property
		(object_class, PROP_SSID,
1212
		 g_param_spec_boxed (NM_SETTING_WIRELESS_SSID, "", "",
1213
		                     G_TYPE_BYTES,
1214 1215
		                     G_PARAM_READWRITE |
		                     G_PARAM_STATIC_STRINGS));
1216 1217 1218 1219 1220 1221 1222

	/**
	 * NMSettingWireless:mode:
	 *
	 * Wi-Fi network mode; one of "infrastructure", "adhoc" or "ap".  If blank,
	 * infrastructure is assumed.
	 **/
1223 1224 1225 1226 1227 1228 1229
	/* ---ifcfg-rh---
	 * property: mode
	 * variable: MODE
	 * values: Ad-Hoc, Managed (Auto)  [case insensitive]
	 * description: Wi-Fi network mode.
	 * ---end---
	 */
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	g_object_class_install_property
		(object_class, PROP_MODE,
		 g_param_spec_string (NM_SETTING_WIRELESS_MODE, "", "",
		                      NULL,
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));

	/**
	 * NMSettingWireless:band:
	 *
	 * 802.11 frequency band of the network.  One of "a" for 5GHz 802.11a or
	 * "bg" for 2.4GHz 802.11.  This will lock associations to the Wi-Fi network
	 * to the specific band, i.e. if "a" is specified, the device will not
	 * associate with the same network in the 2.4GHz band even if the network's
	 * settings are compatible.  This setting depends on specific driver
	 * capability and may not work with all drivers.
	 **/
1247 1248
	/* ---ifcfg-rh---
	 * property: band
1249 1250 1251 1252 1253
	 * variable: BAND(+)
	 * values: a, bg
	 * description: BAND alone is honored, but CHANNEL overrides BAND since it
	 *   implies a band.
	 * example: BAND=bg
1254 1255
	 * ---end---
	 */
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	g_object_class_install_property
		(object_class, PROP_BAND,
		 g_param_spec_string (NM_SETTING_WIRELESS_BAND, "", "",
		                      NULL,
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));

	/**
	 * NMSettingWireless:channel:
	 *
	 * Wireless channel to use for the Wi-Fi connection.  The device will only
	 * join (or create for Ad-Hoc networks) a Wi-Fi network on the specified
	 * channel.  Because channel numbers overlap between bands, this property
	 * also requires the "band" property to be set.
	 **/
1271 1272 1273 1274
	/* ---ifcfg-rh---
	 * property: channel
	 * variable: CHANNEL
	 * description: Channel used for the Wi-Fi communication.
1275 1276
	 *   Channels greater than 14 mean "a" band, otherwise the
	 *   band is "bg".
1277 1278 1279
	 * example: CHANNEL=6
	 * ---end---
	 */
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	g_object_class_install_property
		(object_class, PROP_CHANNEL,
		 g_param_spec_uint (NM_SETTING_WIRELESS_CHANNEL, "", "",
		                    0, G_MAXUINT32, 0,
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    G_PARAM_STATIC_STRINGS));

	/**
	 * NMSettingWireless:bssid:
	 *
	 * If specified, directs the device to only associate with the given access
	 * point.  This capability is highly driver dependent and not supported by
	 * all devices.  Note: this property does not control the BSSID used when
	 * creating an Ad-Hoc network and is unlikely to in the future.
	 **/
1296 1297 1298 1299 1300 1301 1302
	/* ---ifcfg-rh---
	 * property: bssid
	 * variable: BSSID(+)
	 * description: Restricts association only to a single AP.
	 * example: BSSID=00:1E:BD:64:83:21
	 * ---end---
	 */
1303 1304
	g_object_class_install_property
		(object_class, PROP_BSSID,
1305 1306 1307 1308
		 g_param_spec_string (NM_SETTING_WIRELESS_BSSID, "", "",
		                      NULL,
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));
1309 1310 1311 1312 1313 1314 1315

	_properties_override_add_transform (properties_override,
	                                    g_object_class_find_property (G_OBJECT_CLASS (setting_class),
	                                                                  NM_SETTING_WIRELESS_BSSID),
	                                    G_VARIANT_TYPE_BYTESTRING,
	                                    _nm_utils_hwaddr_to_dbus,
	                                    _nm_utils_hwaddr_from_dbus);
1316 1317 1318 1319 1320 1321 1322 1323 1324

	/**
	 * NMSettingWireless:rate:
	 *
	 * If non-zero, directs the device to only use the specified bitrate for
	 * communication with the access point.  Units are in Kb/s, ie 5500 = 5.5
	 * Mbit/s.  This property is highly driver dependent and not all devices
	 * support setting a static bitrate.
	 **/
1325 1326 1327 1328 1329 1330
	/* ---ifcfg-rh---
	 * property: rate
	 * variable: (none)
	 * description: This property is not handled by ifcfg-rh plugin.
	 * ---end---
	 */
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	g_object_class_install_property
		(object_class, PROP_RATE,
		 g_param_spec_uint (NM_SETTING_WIRELESS_RATE, "", "",
		                    0, G_MAXUINT32, 0,
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_FUZZY_IGNORE |
		                    G_PARAM_STATIC_STRINGS));

	/**
	 * NMSettingWireless:tx-power:
	 *
	 * If non-zero, directs the device to use the specified transmit power.
	 * Units are dBm.  This property is highly driver dependent and not all
	 * devices support setting a static transmit power.
	 **/
1347 1348 1349 1350 1351 1352
	/* ---ifcfg-rh---
	 * property: tx-power
	 * variable: (none)
	 * description: This property is not handled by ifcfg-rh plugin.
	 * ---end---
	 */
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	g_object_class_install_property
		(object_class, PROP_TX_POWER,
		 g_param_spec_uint (NM_SETTING_WIRELESS_TX_POWER, "", "",
		                    0, G_MAXUINT32, 0,
		                    G_PARAM_READWRITE |
		                    G_PARAM_CONSTRUCT |
		                    NM_SETTING_PARAM_FUZZY_IGNORE |
		                    G_PARAM_STATIC_STRINGS));

	/**
	 * NMSettingWireless:mac-address:
	 *
	 * If specified, this connection will only apply to the Wi-Fi device whose
	 * permanent MAC address matches. This property does not change the MAC
	 * address of the device (i.e. MAC spoofing).
	 **/
1369 1370
	/* ---keyfile---
	 * property: mac-address
Beniamino Galvani's avatar
Beniamino Galvani committed
1371
	 * format: usual hex-digits-and-colons notation
1372 1373 1374 1375 1376 1377 1378 1379 1380
	 * description: MAC address in traditional hex-digits-and-colons notation
	 *   (e.g. 00:22:68:12:79:A2), or semicolon separated list of 6 bytes (obsolete)
	 *   (e.g. 0;34;104;18;121;162).
	 * ---end---
	 * ---ifcfg-rh---
	 * property: mac-address
	 * variable: HWADDR
	 * description: Hardware address of the device in traditional hex-digits-and-colons
	 *    notation (e.g. 00:22:68:14:5A:05).
1381 1382 1383
	 *    Note that for initscripts this is the current MAC address of the device as found
	 *    during ifup. For NetworkManager this is the permanent MAC address. Or in case no
	 *    permanent MAC address exists, the MAC address initially configured on the device.
1384 1385
	 * ---end---
	 */
1386 1387
	g_object_class_install_property
		(object_class, PROP_MAC_ADDRESS,
1388 1389 1390 1391
		 g_param_spec_string (NM_SETTING_WIRELESS_MAC_ADDRESS, "", "",
		                      NULL,
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));
1392 1393 1394 1395 1396 1397 1398

	_properties_override_add_transform (properties_override,
	                                    g_object_class_find_property (G_OBJECT_CLASS (setting_class),
	                                                                  NM_SETTING_WIRELESS_MAC_ADDRESS),
	                                    G_VARIANT_TYPE_BYTESTRING,
	                                    _nm_utils_hwaddr_to_dbus,
	                                    _nm_utils_hwaddr_from_dbus);
1399 1400 1401 1402

	/**
	 * NMSettingWireless:cloned-mac-address:
	 *
1403 1404
	 * If specified, request that the device use this MAC address instead.
	 * This is known as MAC cloning or spoofing.
1405
	 *
1406
	 * Beside explicitly specifying a MAC address, the special values "preserve", "permanent",
1407 1408 1409 1410
	 * "random" and "stable" are supported.
	 * "preserve" means not to touch the MAC address on activation.
	 * "permanent" means to use the permanent hardware address of the device.
	 * "random" creates a random MAC address on each connect.
1411 1412
	 * "stable" creates a hashed MAC address based on connection.stable-id and a
	 * machine dependent key.
1413 1414
	 *
	 * If unspecified, the value can be overwritten via global defaults, see manual
1415 1416
	 * of NetworkManager.conf. If still unspecified, it defaults to "preserve"
	 * (older versions of NetworkManager may use a different default value).
1417 1418 1419
	 *
	 * On D-Bus, this field is expressed as "assigned-mac-address" or the deprecated
	 * "cloned-mac-address".
1420
	 **/
1421 1422
	/* ---keyfile---
	 * property: cloned-mac-address
Beniamino Galvani's avatar
Beniamino Galvani committed
1423
	 * format: usual hex-digits-and-colons notation
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	 * description: Cloned MAC address in traditional hex-digits-and-colons notation
	 *   (e.g. 00:22:68:12:79:B2), or semicolon separated list of 6 bytes (obsolete)
	 *   (e.g. 0;34;104;18;121;178).
	 * ---end---
	 * ---ifcfg-rh---
	 * property: cloned-mac-address
	 * variable: MACADDR
	 * description: Cloned (spoofed) MAC address in traditional hex-digits-and-colons
	 *    notation (e.g. 00:22:68:14:5A:99).
	 * ---end---
1434 1435 1436 1437 1438
	 * ---dbus---
	 * property: cloned-mac-address
	 * format: byte array
	 * description: This D-Bus field is deprecated in favor of "assigned-mac-address"
	 *    which is more flexible and allows specifying special variants like "random".
1439
	 *    For libnm and nmcli, this field is called "cloned-mac-address".
1440
	 * ---end---
1441
	 */
1442 1443
	g_object_class_install_property
		(object_class, PROP_CLONED_MAC_ADDRESS,
1444 1445 1446 1447
		 g_param_spec_string (NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, "", "",
		                      NULL,
		                      G_PARAM_READWRITE |
		                      G_PARAM_STATIC_STRINGS));
1448 1449 1450 1451 1452 1453 1454 1455

	_properties_override_add_override (properties_override,
	                                   g_object_class_find_property (G_OBJECT_CLASS (setting_class),
	                                                                 NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS),
	                                   G_VARIANT_TYPE_BYTESTRING,
	                                   _nm_utils_hwaddr_cloned_get,
	                                   _nm_utils_hwaddr_cloned_set,
	                                   _nm_utils_hwaddr_cloned_not_set);
1456 1457 1458 1459 1460 1461

	/* ---dbus---
	 * property: assigned-mac-address
	 * format: string
	 * description: The new field for the cloned MAC address. It can be either
	 *   a hardware address in ASCII representation, or one of the special values
1462
	 *   "preserve", "permanent", "random" or "stable".
1463
	 *   This field replaces the deprecated "cloned-mac-address" on D-Bus, which
luz.paz's avatar
luz.paz committed
1464
	 *   can only contain explicit hardware addresses. Note that this property
1465 1466
	 *   only exists in D-Bus API. libnm and nmcli continue to call this property
	 *   "cloned-mac-address".
1467 1468
	 * ---end---
	 */
1469 1470 1471 1472 1473
	_properties_override_add_dbus_only (properties_override,
	                                    "assigned-mac-address",
	                                    G_VARIANT_TYPE_STRING,
	                                    _nm_utils_hwaddr_cloned_data_synth,
	                                    _nm_utils_hwaddr_cloned_data_set);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	/**
	 * NMSettingWireless:generate-mac-address-mask:
	 *
	 * With #NMSettingWireless:cloned-mac-address setting "random" or "stable",
	 * by default all bits of the MAC address are scrambled and a locally-administered,
	 * unicast MAC address is created. This property allows to specify that certain bits
	 * are fixed. Note that the least significant bit of the first MAC address will
	 * always be unset to create a unicast MAC address.
	 *
	 * If the property is %NULL, it is eligible to be overwritten by a default
	 * connection setting. If the value is still %NULL or an empty string, the
	 * default is to create a locally-administered, unicast MAC address.
	 *
	 * If the value contains one MAC address, this address is used as mask. The set
	 * bits of the mask are to be filled with the current MAC address of the device,
	 * while the unset bits are subject to randomization.
	 * Setting "FE:FF:FF:00:00:00" means to preserve the OUI of the current MAC address
	 * and only randomize the lower 3 bytes using the "random" or "stable" algorithm.
	 *
	 * If the value contains one additional MAC address after the mask,
	 * this address is used instead of the current MAC address to fill the bits
	 * that shall not be randomized. For example, a value of
	 * "FE:FF:FF:00:00:00 68:F7:28:00:00:00" will set the OUI of the MAC address
	 * to 68:F7:28, while the lower bits are randomized. A value of
	 * "02:00:00:00:00:00 00:00:00:00:00:00" will create a fully scrambled
	 * globally-administered, burned-in MAC address.
	 *
1502
	 * If the value contains more than one additional MAC addresses, one of
1503 1504 1505 1506