summary refs log tree commit diff
path: root/src/nm-wifi-ap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nm-wifi-ap.c')
-rw-r--r--src/nm-wifi-ap.c193
1 files changed, 123 insertions, 70 deletions
diff --git a/src/nm-wifi-ap.c b/src/nm-wifi-ap.c
index 7770b8bc..5fbdcea6 100644
--- a/src/nm-wifi-ap.c
+++ b/src/nm-wifi-ap.c
@@ -22,8 +22,10 @@
 #include "wireless-helper.h"
 
 #include <string.h>
+#include <stdlib.h>
 
 #include "nm-wifi-ap.h"
+#include "nm-wifi-ap-utils.h"
 #include "NetworkManagerUtils.h"
 #include "nm-utils.h"
 #include "nm-logging.h"
@@ -369,10 +371,69 @@ NMAccessPoint *nm_ap_new (void)
 	return (NMAccessPoint *) object;
 }
 
+static guint32
+pair_to_flags (const char *str)
+{
+	g_return_val_if_fail (str != NULL, NM_802_11_AP_SEC_NONE);
+
+	if (strcmp (str, "wep40") == 0)
+		return NM_802_11_AP_SEC_PAIR_WEP40;
+	if (strcmp (str, "wep104") == 0)
+		return NM_802_11_AP_SEC_PAIR_WEP104;
+	if (strcmp (str, "tkip") == 0)
+		return NM_802_11_AP_SEC_PAIR_TKIP;
+	if (strcmp (str, "ccmp") == 0)
+		return NM_802_11_AP_SEC_PAIR_CCMP;
+	return NM_802_11_AP_SEC_NONE;
+}
+
+static guint32
+group_to_flags (const char *str)
+{
+	g_return_val_if_fail (str != NULL, NM_802_11_AP_SEC_NONE);
+
+	if (strcmp (str, "wep40") == 0)
+		return NM_802_11_AP_SEC_GROUP_WEP40;
+	if (strcmp (str, "wep104") == 0)
+		return NM_802_11_AP_SEC_GROUP_WEP104;
+	if (strcmp (str, "tkip") == 0)
+		return NM_802_11_AP_SEC_GROUP_TKIP;
+	if (strcmp (str, "ccmp") == 0)
+		return NM_802_11_AP_SEC_GROUP_CCMP;
+	return NM_802_11_AP_SEC_NONE;
+}
 
-#define IEEE80211_CAP_ESS       0x0001
-#define IEEE80211_CAP_IBSS      0x0002
-#define IEEE80211_CAP_PRIVACY   0x0010
+static guint32
+security_from_dict (GHashTable *security)
+{
+	GValue *value;
+	guint32 flags = NM_802_11_AP_SEC_NONE;
+	const char **items, **iter;
+
+	value = g_hash_table_lookup (security, "KeyMgmt");
+	if (value) {
+		items = g_value_get_boxed (value);
+		for (iter = items; iter && *iter; iter++) {
+			if (strcmp (*iter, "wpa-psk") == 0)
+				flags |= NM_802_11_AP_SEC_KEY_MGMT_PSK;
+			else if (strcmp (*iter, "wpa-eap") == 0)
+				flags |= NM_802_11_AP_SEC_KEY_MGMT_802_1X;
+		}
+	}
+
+	value = g_hash_table_lookup (security, "Pairwise");
+	if (value) {
+		items = g_value_get_boxed (value);
+		for (iter = items; iter && *iter; iter++)
+			flags |= pair_to_flags (*iter);
+	}
+
+	value = g_hash_table_lookup (security, "Group");
+	if (value)
+		flags |= group_to_flags (g_value_get_string (value));
+
+	return flags;
+}
 
 static void
 foreach_property_cb (gpointer key, gpointer value, gpointer user_data)
@@ -383,9 +444,9 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data)
 	if (G_VALUE_HOLDS_BOXED (variant)) {
 		GArray *array = g_value_get_boxed (variant);
 
-		if (!strcmp (key, "ssid")) {
+		if (!strcmp (key, "SSID")) {
 			guint32 len = MIN (IW_ESSID_MAX_SIZE, array->len);
-			GByteArray * ssid;
+			GByteArray *ssid;
 
 			/* Stupid ieee80211 layer uses <hidden> */
 			if (((len == 8) || (len == 9))
@@ -399,7 +460,7 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data)
 			g_byte_array_append (ssid, (const guint8 *) array->data, len);
 			nm_ap_set_ssid (ap, ssid);
 			g_byte_array_free (ssid, TRUE);
-		} else if (!strcmp (key, "bssid")) {
+		} else if (!strcmp (key, "BSSID")) {
 			struct ether_addr addr;
 
 			if (array->len != ETH_ALEN)
@@ -407,43 +468,54 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data)
 			memset (&addr, 0, sizeof (struct ether_addr));
 			memcpy (&addr, array->data, ETH_ALEN);
 			nm_ap_set_address (ap, &addr);
-		} else if (!strcmp (key, "wpaie")) {
-			guint8 * ie = (guint8 *) array->data;
+		} else if (!strcmp (key, "Rates")) {
+			guint32 maxrate = 0;
+			int i;
+
+			/* Find the max AP rate */
+			for (i = 0; i < array->len; i++) {
+				guint32 r = g_array_index (array, guint32, i);
+
+				if (r > maxrate) {
+					maxrate = r;
+					nm_ap_set_max_bitrate (ap, r / 1000);
+				}
+			}
+		} else if (!strcmp (key, "WPA")) {
 			guint32 flags = nm_ap_get_wpa_flags (ap);
 
-			if (array->len <= 0 || array->len > WPA_MAX_IE_LEN)
-				return;
-			flags = nm_ap_add_security_from_ie (flags, ie, array->len);
+			flags |= security_from_dict (g_value_get_boxed (variant));
 			nm_ap_set_wpa_flags (ap, flags);
-		} else if (!strcmp (key, "rsnie")) {
-			guint8 * ie = (guint8 *) array->data;
+		} else if (!strcmp (key, "RSN")) {
 			guint32 flags = nm_ap_get_rsn_flags (ap);
 
-			if (array->len <= 0 || array->len > WPA_MAX_IE_LEN)
-				return;
-			flags = nm_ap_add_security_from_ie (flags, ie, array->len);
+			flags |= security_from_dict (g_value_get_boxed (variant));
 			nm_ap_set_rsn_flags (ap, flags);
 		}
-	} else if (G_VALUE_HOLDS_INT (variant)) {
-		gint32 int_val = g_value_get_int (variant);
-
-		if (!strcmp (key, "frequency")) {
-			nm_ap_set_freq (ap, (guint32) int_val);
-		} else if (!strcmp (key, "maxrate")) {
-			/* Supplicant reports as b/s, we use Kb/s internally */
-			nm_ap_set_max_bitrate (ap, int_val / 1000);
-		}
 	} else if (G_VALUE_HOLDS_UINT (variant)) {
 		guint32 val = g_value_get_uint (variant);
 
-		if (!strcmp (key, "capabilities")) {
-			if (val & IEEE80211_CAP_ESS) {
+		if (!strcmp (key, "Frequency"))
+			nm_ap_set_freq (ap, val);
+	} else if (G_VALUE_HOLDS_INT (variant)) {
+		gint val = g_value_get_int (variant);
+
+		if (!strcmp (key, "Signal"))
+			nm_ap_set_strength (ap, nm_ap_utils_level_to_quality (val));
+	} else if (G_VALUE_HOLDS_STRING (variant)) {
+		const char *val = g_value_get_string (variant);
+
+		if (val && !strcmp (key, "Mode")) {
+			if (strcmp (val, "infrastructure") == 0)
 				nm_ap_set_mode (ap, NM_802_11_MODE_INFRA);
-			} else if (val & IEEE80211_CAP_IBSS) {
+			else if (strcmp (val, "ad-hoc") == 0)
 				nm_ap_set_mode (ap, NM_802_11_MODE_ADHOC);
-			}
+		}
+	} else if (G_VALUE_HOLDS_BOOLEAN (variant)) {
+		gboolean val = g_value_get_boolean (variant);
 
-			if (val & IEEE80211_CAP_PRIVACY) {
+		if (strcmp (key, "Privacy") == 0) {
+			if (val) {
 				guint32 flags = nm_ap_get_flags (ap);
 				nm_ap_set_flags (ap, flags | NM_802_11_AP_FLAGS_PRIVACY);
 			}
@@ -451,7 +523,6 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data)
 	}
 }
 
-
 NMAccessPoint *
 nm_ap_new_from_properties (GHashTable *properties)
 {
@@ -1170,45 +1241,6 @@ void nm_ap_set_user_addresses (NMAccessPoint *ap, GSList *list)
 }
 
 
-guint32
-nm_ap_add_security_from_ie (guint32 flags,
-                            const guint8 *wpa_ie,
-                            guint32 length)
-{
-	wpa_ie_data * cap_data;
-
-	if (!(cap_data = wpa_parse_wpa_ie (wpa_ie, length)))
-		return NM_802_11_AP_SEC_NONE;
-
-	/* Pairwise cipher flags */
-	if (cap_data->pairwise_cipher & IW_AUTH_CIPHER_WEP40)
-		flags |= NM_802_11_AP_SEC_PAIR_WEP40;
-	if (cap_data->pairwise_cipher & IW_AUTH_CIPHER_WEP104)
-		flags |= NM_802_11_AP_SEC_PAIR_WEP104;
-	if (cap_data->pairwise_cipher & IW_AUTH_CIPHER_TKIP)
-		flags |= NM_802_11_AP_SEC_PAIR_TKIP;
-	if (cap_data->pairwise_cipher & IW_AUTH_CIPHER_CCMP)
-		flags |= NM_802_11_AP_SEC_PAIR_CCMP;
-
-	/* Group cipher flags */
-	if (cap_data->group_cipher & IW_AUTH_CIPHER_WEP40)
-		flags |= NM_802_11_AP_SEC_GROUP_WEP40;
-	if (cap_data->group_cipher & IW_AUTH_CIPHER_WEP104)
-		flags |= NM_802_11_AP_SEC_GROUP_WEP104;
-	if (cap_data->group_cipher & IW_AUTH_CIPHER_TKIP)
-		flags |= NM_802_11_AP_SEC_GROUP_TKIP;
-	if (cap_data->group_cipher & IW_AUTH_CIPHER_CCMP)
-		flags |= NM_802_11_AP_SEC_GROUP_CCMP;
-
-	if (cap_data->key_mgmt & IW_AUTH_KEY_MGMT_802_1X)
-		flags |= NM_802_11_AP_SEC_KEY_MGMT_802_1X;
-	if (cap_data->key_mgmt & IW_AUTH_KEY_MGMT_PSK)
-		flags |= NM_802_11_AP_SEC_KEY_MGMT_PSK;
-
-	g_slice_free (wpa_ie_data, cap_data);
-	return flags;
-}
-
 gboolean
 nm_ap_check_compatible (NMAccessPoint *self,
                         NMConnection *connection)
@@ -1275,6 +1307,27 @@ nm_ap_check_compatible (NMAccessPoint *self,
 	                                                   nm_ap_get_mode (self));
 }
 
+gboolean
+nm_ap_complete_connection (NMAccessPoint *self,
+                           NMConnection *connection,
+                           gboolean lock_bssid,
+                           GError **error)
+{
+	NMAccessPointPrivate *priv = NM_AP_GET_PRIVATE (self);
+
+	g_return_val_if_fail (connection != NULL, FALSE);
+
+	return nm_ap_utils_complete_connection (priv->ssid,
+	                                        priv->address.ether_addr_octet,
+	                                        priv->mode,
+	                                        priv->flags,
+	                                        priv->wpa_flags,
+	                                        priv->rsn_flags,
+	                                        connection,
+	                                        lock_bssid,
+	                                        error);
+}
+
 static gboolean
 capabilities_compatible (guint32 a_flags, guint32 b_flags)
 {