WifiAccessPoint *createNMWifiAccessPoint(NMAccessPoint *ap) { const GByteArray *ssid = nm_access_point_get_ssid(ap); //GBytes *ssid = nm_access_point_get_ssid(ap); char *ssid_str = NULL, *ssid_hex_str = NULL; bool security = resolveAPSecurity(ap); /* Convert to strings */ if (ssid) { const guint8 *ssid_data = ssid->data; gsize ssid_len = ssid->len; //ssid_data = (const guint8 *) g_bytes_get_data(ssid, &ssid_len); ssid_str = nm_utils_ssid_to_utf8(ssid); } if (!ssid_str || !ssid) DBG("libnm conversion of ssid to utf8 failed, using empty string"); return new WifiAccessPoint { !ssid_str ? "" : ssid_str, nm_access_point_get_strength(ap), security, utils_hash_ap(nm_access_point_get_ssid(ap), nm_access_point_get_mode(ap), nm_access_point_get_flags(ap), nm_access_point_get_wpa_flags(ap), nm_access_point_get_rsn_flags(ap)), }; }
static void detail_access_point (gpointer data, gpointer user_data) { NMAccessPoint *ap = NM_ACCESS_POINT (data); const char *active_bssid = (const char *) user_data; GString *str; gboolean active = FALSE; guint32 flags, wpa_flags, rsn_flags; const GByteArray * ssid; char *tmp; flags = nm_access_point_get_flags (ap); wpa_flags = nm_access_point_get_wpa_flags (ap); rsn_flags = nm_access_point_get_rsn_flags (ap); if (active_bssid) { const char *current_bssid = nm_access_point_get_hw_address (ap); if (current_bssid && !strcmp (current_bssid, active_bssid)) active = TRUE; } str = g_string_new (NULL); g_string_append_printf (str, "%s, %s, Freq %d MHz, Rate %d Mb/s, Strength %d", (nm_access_point_get_mode (ap) == NM_802_11_MODE_INFRA) ? "Infra" : "Ad-Hoc", nm_access_point_get_hw_address (ap), nm_access_point_get_frequency (ap), nm_access_point_get_max_bitrate (ap) / 1000, nm_access_point_get_strength (ap)); if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags != NM_802_11_AP_SEC_NONE) && (rsn_flags != NM_802_11_AP_SEC_NONE)) g_string_append (str, ", Encrypted: "); if ( (flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) g_string_append (str, " WEP"); if (wpa_flags != NM_802_11_AP_SEC_NONE) g_string_append (str, " WPA"); if (rsn_flags != NM_802_11_AP_SEC_NONE) g_string_append (str, " WPA2"); if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) g_string_append (str, " Enterprise"); /* FIXME: broadcast/hidden */ ssid = nm_access_point_get_ssid (ap); tmp = g_strdup_printf (" %s%s", active ? "*" : "", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)"); print_string (tmp, str->str); g_string_free (str, TRUE); g_free (tmp); }
static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { NMAccessPoint *ap = NM_ACCESS_POINT (object); _nm_object_ensure_inited (NM_OBJECT (object)); switch (prop_id) { case PROP_FLAGS: g_value_set_uint (value, nm_access_point_get_flags (ap)); break; case PROP_WPA_FLAGS: g_value_set_uint (value, nm_access_point_get_wpa_flags (ap)); break; case PROP_RSN_FLAGS: g_value_set_uint (value, nm_access_point_get_rsn_flags (ap)); break; case PROP_SSID: g_value_set_boxed (value, nm_access_point_get_ssid (ap)); break; case PROP_FREQUENCY: g_value_set_uint (value, nm_access_point_get_frequency (ap)); break; case PROP_HW_ADDRESS: g_value_set_string (value, nm_access_point_get_bssid (ap)); break; case PROP_BSSID: g_value_set_string (value, nm_access_point_get_bssid (ap)); break; case PROP_MODE: g_value_set_uint (value, nm_access_point_get_mode (ap)); break; case PROP_MAX_BITRATE: g_value_set_uint (value, nm_access_point_get_max_bitrate (ap)); break; case PROP_STRENGTH: g_value_set_uchar (value, nm_access_point_get_strength (ap)); break; case PROP_LAST_SEEN: g_value_set_int (value, nm_access_point_get_last_seen (ap)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
/* stolen from nm-applet */ static char * hash_ap (NMAccessPoint *ap) { unsigned char input[66]; GBytes *ssid; NM80211Mode mode; guint32 flags; guint32 wpa_flags; guint32 rsn_flags; memset (&input[0], 0, sizeof (input)); ssid = nm_access_point_get_ssid (ap); if (ssid) memcpy (input, g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid)); mode = nm_access_point_get_mode (ap); if (mode == NM_802_11_MODE_INFRA) input[32] |= (1 << 0); else if (mode == NM_802_11_MODE_ADHOC) input[32] |= (1 << 1); else input[32] |= (1 << 2); /* Separate out no encryption, WEP-only, and WPA-capable */ flags = nm_access_point_get_flags (ap); wpa_flags = nm_access_point_get_wpa_flags (ap); rsn_flags = nm_access_point_get_rsn_flags (ap); if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) input[32] |= (1 << 3); else if ( (flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) input[32] |= (1 << 4); else if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags != NM_802_11_AP_SEC_NONE) && (rsn_flags != NM_802_11_AP_SEC_NONE)) input[32] |= (1 << 5); else input[32] |= (1 << 6); /* duplicate it */ memcpy (&input[33], &input[0], 32); return g_compute_checksum_for_data (G_CHECKSUM_MD5, input, sizeof (input)); }
static void dump_access_point (NMAccessPoint *ap) { const GByteArray * ssid; const char * str; ssid = nm_access_point_get_ssid (ap); g_print ("\tSsid: %s\n", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)"); str = nm_access_point_get_hw_address (ap); g_print ("\tMAC Address: %s\n", str); g_print ("\tFlags: 0x%X\n", nm_access_point_get_flags (ap)); g_print ("\tWPA Flags: 0x%X\n", nm_access_point_get_wpa_flags (ap)); g_print ("\tRSN Flags: 0x%X\n", nm_access_point_get_rsn_flags (ap)); g_print ("\tFrequency: %u\n", nm_access_point_get_frequency (ap)); g_print ("\tMode: %d\n", nm_access_point_get_mode (ap)); g_print ("\tRate: %d\n", nm_access_point_get_max_bitrate (ap)); g_print ("\tStrength: %d\n", nm_access_point_get_strength (ap)); }
void nm_network_menu_item_set_detail (NMNetworkMenuItem *item, NMAccessPoint *ap, GdkPixbuf *adhoc_icon, guint32 dev_caps) { gboolean is_adhoc = FALSE; guint32 ap_flags, ap_wpa, ap_rsn; g_return_if_fail (item != NULL); g_return_if_fail (NM_IS_NETWORK_MENU_ITEM (item)); ap_flags = nm_access_point_get_flags (ap); ap_wpa = nm_access_point_get_wpa_flags (ap); ap_rsn = nm_access_point_get_rsn_flags (ap); if ((ap_flags & NM_802_11_AP_FLAGS_PRIVACY) || ap_wpa || ap_rsn) item->is_encrypted = TRUE; if (nm_access_point_get_mode (ap) == NM_802_11_MODE_ADHOC) { item->is_adhoc = is_adhoc = TRUE; gtk_image_set_from_pixbuf (GTK_IMAGE (item->detail), adhoc_icon); } else gtk_image_set_from_stock (GTK_IMAGE (item->detail), NULL, GTK_ICON_SIZE_MENU); /* Don't enable the menu item the device can't even connect to the AP */ if ( !nm_utils_security_valid (NMU_SEC_NONE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_STATIC_WEP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_LEAP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_WPA_PSK, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_WPA2_PSK, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn) && !nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, dev_caps, TRUE, is_adhoc, ap_flags, ap_wpa, ap_rsn)) { gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE); } }
static NMConnection * create_connection (NMConnectionItem *item) { NMConnection *connection; NMDeviceWifi *device; NMAccessPoint *ap; NMSetting *s_con; NMSetting *s_wireless; NMSettingWirelessSecurity *s_wireless_sec; NMSetting8021x *s_8021x = NULL; const GByteArray *ap_ssid; char *id; char buf[33]; int buf_len; NM80211Mode mode; guint32 dev_caps; gboolean supported = TRUE; device = NM_DEVICE_WIFI (nm_device_item_get_device (NM_DEVICE_ITEM (item))); ap = nm_wifi_item_get_ap (NM_WIFI_ITEM (item)); dev_caps = nm_device_wifi_get_capabilities (device); s_wireless_sec = get_security_for_ap (ap, dev_caps, &supported, &s_8021x); if (!supported) return NULL; if (NM_CONNECTION_ITEM_CLASS (nm_wifi_item_parent_class)->create_connection) connection = NM_CONNECTION_ITEM_CLASS (nm_wifi_item_parent_class)->create_connection (item); if (!connection) return NULL; s_wireless = nm_setting_wireless_new (); ap_ssid = nm_access_point_get_ssid (ap); g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, ap_ssid, NULL); mode = nm_access_point_get_mode (ap); if (mode == NM_802_11_MODE_ADHOC) g_object_set (s_wireless, NM_SETTING_WIRELESS_MODE, "adhoc", NULL); else if (mode == NM_802_11_MODE_INFRA) g_object_set (s_wireless, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); else g_assert_not_reached (); nm_connection_add_setting (connection, s_wireless); if (s_wireless_sec) { g_object_set (s_wireless, NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NULL); nm_connection_add_setting (connection, NM_SETTING (s_wireless_sec)); } if (s_8021x) nm_connection_add_setting (connection, NM_SETTING (s_8021x)); s_con = nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, nm_setting_get_name (s_wireless), NM_SETTING_CONNECTION_AUTOCONNECT, !is_manufacturer_default_ssid (ap_ssid), NULL); memset (buf, 0, sizeof (buf)); buf_len = MIN (ap_ssid->len, sizeof (buf) - 1); memcpy (buf, ap_ssid->data, buf_len); id = nm_utils_ssid_to_utf8 (buf, buf_len); g_object_set (s_con, NM_SETTING_CONNECTION_ID, id, NULL); g_free (id); return connection; }
static NMSettingWirelessSecurity * get_security_for_ap (NMAccessPoint *ap, guint32 dev_caps, gboolean *supported, NMSetting8021x **s_8021x) { NMSettingWirelessSecurity *sec; NM80211Mode mode; guint32 flags; guint32 wpa_flags; guint32 rsn_flags; g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), NULL); g_return_val_if_fail (supported != NULL, NULL); g_return_val_if_fail (*supported == TRUE, NULL); g_return_val_if_fail (s_8021x != NULL, NULL); g_return_val_if_fail (*s_8021x == NULL, NULL); sec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); mode = nm_access_point_get_mode (ap); flags = nm_access_point_get_flags (ap); wpa_flags = nm_access_point_get_wpa_flags (ap); rsn_flags = nm_access_point_get_rsn_flags (ap); /* No security */ if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) goto none; /* Static WEP, Dynamic WEP, or LEAP */ if (flags & NM_802_11_AP_FLAGS_PRIVACY) { if ((dev_caps & NM_WIFI_DEVICE_CAP_RSN) || (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) { /* If the device can do WPA/RSN but the AP has no WPA/RSN informatoin * elements, it must be LEAP or static/dynamic WEP. */ if ((wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) { g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none", NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, 0, NULL); return sec; } /* Otherwise, the AP supports WPA or RSN, which is preferred */ } else { /* Device can't do WPA/RSN, but can at least pass through the * WPA/RSN information elements from a scan. Since Privacy was * advertised, LEAP or static/dynamic WEP must be in use. */ g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none", NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, 0, NULL); return sec; } } /* Stuff after this point requires infrastructure */ if (mode != NM_802_11_MODE_INFRA) { *supported = FALSE; goto none; } /* WPA2 PSK first */ if ( (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) && (dev_caps & NM_WIFI_DEVICE_CAP_RSN)) { g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", NULL); nm_setting_wireless_security_add_proto (sec, "rsn"); add_ciphers_from_flags (sec, rsn_flags, TRUE); add_ciphers_from_flags (sec, rsn_flags, FALSE); return sec; } /* WPA PSK */ if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) && (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) { g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", NULL); nm_setting_wireless_security_add_proto (sec, "wpa"); add_ciphers_from_flags (sec, wpa_flags, TRUE); add_ciphers_from_flags (sec, wpa_flags, FALSE); return sec; } /* WPA2 Enterprise */ if ( (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) && (dev_caps & NM_WIFI_DEVICE_CAP_RSN)) { g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-eap", NULL); nm_setting_wireless_security_add_proto (sec, "rsn"); add_ciphers_from_flags (sec, rsn_flags, TRUE); add_ciphers_from_flags (sec, rsn_flags, FALSE); *s_8021x = NM_SETTING_802_1X (nm_setting_802_1x_new ()); nm_setting_802_1x_add_eap_method (*s_8021x, "ttls"); g_object_set (*s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", NULL); return sec; } /* WPA Enterprise */ if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) && (dev_caps & NM_WIFI_DEVICE_CAP_WPA)) { g_object_set (sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-eap", NULL); nm_setting_wireless_security_add_proto (sec, "wpa"); add_ciphers_from_flags (sec, wpa_flags, TRUE); add_ciphers_from_flags (sec, wpa_flags, FALSE); *s_8021x = NM_SETTING_802_1X (nm_setting_802_1x_new ()); nm_setting_802_1x_add_eap_method (*s_8021x, "ttls"); g_object_set (*s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", NULL); return sec; } *supported = FALSE; none: g_object_unref (sec); return NULL; }
static void add_access_point (GisNetworkPage *page, NMAccessPoint *ap, NMAccessPoint *active) { GisNetworkPagePrivate *priv = page->priv; const GByteArray *ssid; const gchar *ssid_text; const gchar *object_path; GtkTreeIter iter; gboolean activated, activating; ssid = nm_access_point_get_ssid (ap); object_path = nm_object_get_path (NM_OBJECT (ap)); if (ssid == NULL) return; ssid_text = nm_utils_escape_ssid (ssid->data, ssid->len); if (active && nm_utils_same_ssid (ssid, nm_access_point_get_ssid (active), TRUE)) { switch (nm_device_get_state (priv->nm_device)) { case NM_DEVICE_STATE_PREPARE: case NM_DEVICE_STATE_CONFIG: case NM_DEVICE_STATE_NEED_AUTH: case NM_DEVICE_STATE_IP_CONFIG: case NM_DEVICE_STATE_SECONDARIES: activated = FALSE; activating = TRUE; break; case NM_DEVICE_STATE_ACTIVATED: activated = TRUE; activating = FALSE; break; default: activated = FALSE; activating = FALSE; break; } } else { activated = FALSE; activating = FALSE; } gtk_list_store_append (priv->ap_list, &iter); gtk_list_store_set (priv->ap_list, &iter, PANEL_WIRELESS_COLUMN_ID, object_path, PANEL_WIRELESS_COLUMN_TITLE, ssid_text, PANEL_WIRELESS_COLUMN_STRENGTH, nm_access_point_get_strength (ap), PANEL_WIRELESS_COLUMN_MODE, nm_access_point_get_mode (ap), PANEL_WIRELESS_COLUMN_SECURITY, get_access_point_security (ap), PANEL_WIRELESS_COLUMN_ACTIVATING, activating, PANEL_WIRELESS_COLUMN_ACTIVE, activated, PANEL_WIRELESS_COLUMN_PULSE, priv->pulse, -1); if (activating) { GtkTreePath *path; GtkTreeModel *model; model = (GtkTreeModel*)priv->ap_list; path = gtk_tree_model_get_path (model, &iter); priv->row = gtk_tree_row_reference_new (model, path); gtk_tree_path_free (path); g_timeout_add (160, bump_pulse, page); } }
void WifiStatusNM::setConnectedAccessPoint(WifiAccessPoint *ap, String psk) { ScopedPointer<StringArray> cmd; for (const auto& listener : listeners) listener->handleWifiBusy(); // disconnect if no ap provided if (ap == nullptr) { NMActiveConnection *conn = nm_device_get_active_connection(nmdevice); removeNMConnection(nmdevice, conn); return; } // try to connect to ap, dispatch events on success and failure else { NMConnection *connection = NULL; NMSettingWireless *s_wifi = NULL; NMSettingWirelessSecurity *s_wsec = NULL; const char *nm_ap_path = NULL; const GPtrArray *ap_list; NMAccessPoint *candidate_ap; //FIXME: expand WifiAccessPoint struct to know which NMAccessPoint it is ap_list = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(nmdevice)); if (ap_list == NULL) return; for (int i = 0; i < ap_list->len; i++) { const char *candidate_hash; candidate_ap = (NMAccessPoint *) g_ptr_array_index(ap_list, i); candidate_hash = utils_hash_ap(nm_access_point_get_ssid(candidate_ap), nm_access_point_get_mode(candidate_ap), nm_access_point_get_flags(candidate_ap), nm_access_point_get_wpa_flags(candidate_ap), nm_access_point_get_rsn_flags(candidate_ap)); if (ap->hash == candidate_hash) { nm_ap_path = nm_object_get_path(NM_OBJECT(candidate_ap)); break; } } if (!nm_ap_path) return; connecting = true; connection = nm_connection_new(); s_wifi = (NMSettingWireless *) nm_setting_wireless_new(); nm_connection_add_setting(connection, NM_SETTING(s_wifi)); g_object_set(s_wifi, NM_SETTING_WIRELESS_SSID, nm_access_point_get_ssid(candidate_ap), NM_SETTING_WIRELESS_HIDDEN, false, NULL); if (!psk.isEmpty()) { s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new(); nm_connection_add_setting(connection, NM_SETTING(s_wsec)); if (nm_access_point_get_wpa_flags(candidate_ap) == NM_802_11_AP_SEC_NONE && nm_access_point_get_rsn_flags(candidate_ap) == NM_802_11_AP_SEC_NONE) { /* WEP */ nm_setting_wireless_security_set_wep_key(s_wsec, 0, psk.toRawUTF8()); if (isValidWEPKeyFormat(psk)) g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, NM_WEP_KEY_TYPE_KEY, NULL); else if (isValidWEPPassphraseFormat(psk)) g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, NM_WEP_KEY_TYPE_PASSPHRASE, NULL); else DBG("User input invalid WEP Key type, psk.length() = " << psk.length() << ", not in [5,10,13,26]"); } else { g_object_set(s_wsec, NM_SETTING_WIRELESS_SECURITY_PSK, psk.toRawUTF8(), NULL); } } nm_client_add_and_activate_connection(nmclient, connection, nmdevice, nm_ap_path, handle_add_and_activate_finish, this); } }
static void print_access_point_info (NMAccessPoint * ap, gboolean active, guint32 device_capas) { const char * bssid; const GByteArray * ssid; NM80211Mode mode; guint32 frequency; guint32 max_bitrate; guint32 signal_strength; guint32 ap_flags; guint32 wpa_flags; guint32 rsn_flags; char *ssid_str; gboolean is_adhoc; gchar * sec_opts[5]; /* Currently five security options is defined */ gint sec_opts_num, i; g_return_if_fail (NM_IS_ACCESS_POINT (ap)); mode = nm_access_point_get_mode (ap); ap_flags = nm_access_point_get_flags (ap); wpa_flags = nm_access_point_get_wpa_flags (ap); rsn_flags = nm_access_point_get_rsn_flags (ap); is_adhoc = (mode == NM_802_11_MODE_ADHOC); /* Skip access point not compatible with device's capabilities */ if ( !nm_utils_security_valid (NMU_SEC_NONE, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_STATIC_WEP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_LEAP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_DYNAMIC_WEP, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_WPA_PSK, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_WPA2_PSK, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_WPA_ENTERPRISE, device_capas,TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags) && !nm_utils_security_valid (NMU_SEC_WPA2_ENTERPRISE, device_capas, TRUE, is_adhoc, ap_flags, wpa_flags, rsn_flags)) return; bssid = nm_access_point_get_hw_address (ap); ssid = nm_access_point_get_ssid (ap); frequency = nm_access_point_get_frequency (ap); max_bitrate = nm_access_point_get_max_bitrate (ap); signal_strength = nm_access_point_get_strength (ap); ssid_str = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len); sec_opts_num = 0; if ((ap_flags & NM_802_11_AP_FLAGS_PRIVACY) && !wpa_flags && !rsn_flags) sec_opts[sec_opts_num++] = "wep"; if (!is_adhoc) { if (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) sec_opts[sec_opts_num++] = "wpa-psk"; if (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK) sec_opts[sec_opts_num++] = "wpa2-psk"; if (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) sec_opts[sec_opts_num++] = "wpa-eap"; if (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) sec_opts[sec_opts_num++] = "wpa2-eap"; } g_print ("%-9s BSSID:%s Frequency:%dMHz", "", bssid, frequency); if (active) g_print (" <-- ACTIVE"); g_print ("\n"); g_print ("%-15s SSID:%s Mode:%s\n", "", ssid_str, wifi_mode_to_string(mode)); g_print ("%-15s Signal:%d MaxBitrate:%.1fMb/s Security:", "", signal_strength, max_bitrate/1000.0); if (sec_opts_num == 0) { g_print ("none"); } else { for (i = 0; i < sec_opts_num; i++) { g_print ("%s", sec_opts[i]); if (i != sec_opts_num - 1) g_print (" "); } } g_print ("\n"); }
/** * nm_access_point_connection_valid: * @ap: an #NMAccessPoint to validate @connection against * @connection: an #NMConnection to validate against @ap * * Validates a given connection against a given Wi-Fi access point to ensure that * the connection may be activated with that AP. The connection must match the * @ap's SSID, (if given) BSSID, and other attributes like security settings, * channel, band, etc. * * Returns: %TRUE if the connection may be activated with this Wi-Fi AP, * %FALSE if it cannot be. **/ gboolean nm_access_point_connection_valid (NMAccessPoint *ap, NMConnection *connection) { NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; const char *ctype, *ap_bssid_str; const GByteArray *setting_ssid; const GByteArray *ap_ssid; const GByteArray *setting_bssid; struct ether_addr *ap_bssid; const char *setting_mode; NM80211Mode ap_mode; const char *setting_band; guint32 ap_freq, setting_chan, ap_chan; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); ctype = nm_setting_connection_get_connection_type (s_con); if (strcmp (ctype, NM_SETTING_WIRELESS_SETTING_NAME) != 0) return FALSE; s_wifi = nm_connection_get_setting_wireless (connection); if (!s_wifi) return FALSE; /* SSID checks */ ap_ssid = nm_access_point_get_ssid (ap); g_warn_if_fail (ap_ssid != NULL); setting_ssid = nm_setting_wireless_get_ssid (s_wifi); if (!setting_ssid || !ap_ssid || (setting_ssid->len != ap_ssid->len)) return FALSE; if (memcmp (setting_ssid->data, ap_ssid->data, ap_ssid->len) != 0) return FALSE; /* BSSID checks */ ap_bssid_str = nm_access_point_get_bssid (ap); g_warn_if_fail (ap_bssid_str); setting_bssid = nm_setting_wireless_get_bssid (s_wifi); if (setting_bssid && ap_bssid_str) { g_assert (setting_bssid->len == ETH_ALEN); ap_bssid = ether_aton (ap_bssid_str); g_warn_if_fail (ap_bssid); if (ap_bssid) { if (memcmp (ap_bssid->ether_addr_octet, setting_bssid->data, ETH_ALEN) != 0) return FALSE; } } /* Mode */ ap_mode = nm_access_point_get_mode (ap); g_warn_if_fail (ap_mode != NM_802_11_MODE_UNKNOWN); setting_mode = nm_setting_wireless_get_mode (s_wifi); if (setting_mode && ap_mode) { if (!strcmp (setting_mode, "infrastructure") && (ap_mode != NM_802_11_MODE_INFRA)) return FALSE; if (!strcmp (setting_mode, "adhoc") && (ap_mode != NM_802_11_MODE_ADHOC)) return FALSE; /* Hotspot never matches against APs as it's a device-specific mode. */ if (!strcmp (setting_mode, "ap")) return FALSE; } /* Band and Channel/Frequency */ ap_freq = nm_access_point_get_frequency (ap); if (ap_freq) { setting_band = nm_setting_wireless_get_band (s_wifi); if (g_strcmp0 (setting_band, "a") == 0) { if (ap_freq < 4915 || ap_freq > 5825) return FALSE; } else if (g_strcmp0 (setting_band, "bg") == 0) { if (ap_freq < 2412 || ap_freq > 2484) return FALSE; } setting_chan = nm_setting_wireless_get_channel (s_wifi); if (setting_chan) { ap_chan = nm_utils_wifi_freq_to_channel (ap_freq); if (setting_chan != ap_chan) return FALSE; } } s_wsec = nm_connection_get_setting_wireless_security (connection); if (!nm_setting_wireless_ap_security_compatible (s_wifi, s_wsec, nm_access_point_get_flags (ap), nm_access_point_get_wpa_flags (ap), nm_access_point_get_rsn_flags (ap), ap_mode)) return FALSE; return TRUE; }
static void show_access_point_info (NMAccessPoint *ap) { guint32 flags, wpa_flags, rsn_flags, freq, bitrate; guint8 strength; const GByteArray *ssid; const char *hwaddr; NM80211Mode mode; char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str; GString *security_str; /* Get AP properties */ flags = nm_access_point_get_flags (ap); wpa_flags = nm_access_point_get_wpa_flags (ap); rsn_flags = nm_access_point_get_rsn_flags (ap); ssid = nm_access_point_get_ssid (ap); hwaddr = nm_access_point_get_hw_address (ap); freq = nm_access_point_get_frequency (ap); mode = nm_access_point_get_mode (ap); bitrate = nm_access_point_get_max_bitrate (ap); strength = nm_access_point_get_strength (ap); /* Convert to strings */ ssid_str = nm_utils_ssid_to_utf8 (ssid); freq_str = g_strdup_printf ("%u MHz", freq); bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000); strength_str = g_strdup_printf ("%u", strength); wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags); rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags); security_str = g_string_new (NULL); if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags != NM_802_11_AP_SEC_NONE) && (rsn_flags != NM_802_11_AP_SEC_NONE)) g_string_append (security_str, "Encrypted: "); if ( (flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_flags == NM_802_11_AP_SEC_NONE) && (rsn_flags == NM_802_11_AP_SEC_NONE)) g_string_append (security_str, "WEP "); if (wpa_flags != NM_802_11_AP_SEC_NONE) g_string_append (security_str, "WPA "); if (rsn_flags != NM_802_11_AP_SEC_NONE) g_string_append (security_str, "WPA2 "); if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X) || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) g_string_append (security_str, "Enterprise "); if (security_str->len > 0) g_string_truncate (security_str, security_str->len-1); /* Chop off last space */ printf ("SSID: %s\n", ssid_str); printf ("BSSID: %s\n", hwaddr); printf ("Mode: %s\n", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc" : mode == NM_802_11_MODE_INFRA ? "Infrastructure" : "Unknown"); printf ("Freq: %s\n", freq_str); printf ("Bitrate: %s\n", bitrate_str); printf ("Strength: %s\n", strength_str); printf ("Security: %s\n", security_str->str); printf ("WPA flags: %s\n", wpa_flags_str); printf ("RSN flags: %s\n", rsn_flags_str); printf ("D-Bus path: %s\n\n", nm_object_get_path (NM_OBJECT (ap))); g_free (ssid_str); g_free (freq_str); g_free (bitrate_str); g_free (strength_str); g_free (wpa_flags_str); g_free (rsn_flags_str); g_string_free (security_str, TRUE); }