Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 5 | #include "shill/wifi_endpoint.h" |
| 6 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <base/stl_util.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 12 | #include <base/string_util.h> |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 14 | |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 15 | #include "shill/ieee80211.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.h" |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 17 | #include "shill/proxy_factory.h" |
| 18 | #include "shill/supplicant_bss_proxy_interface.h" |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 19 | #include "shill/wifi.h" |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 20 | #include "shill/wifi_endpoint.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 21 | #include "shill/wpa_supplicant.h" |
| 22 | |
| 23 | using std::map; |
| 24 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 25 | using std::string; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 26 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
| 29 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 30 | WiFiEndpoint::WiFiEndpoint(ProxyFactory *proxy_factory, |
| 31 | const WiFiRefPtr &device, |
| 32 | const string &rpc_id, |
| 33 | const map<string, ::DBus::Variant> &properties) |
| 34 | : frequency_(0), |
| 35 | proxy_factory_(proxy_factory), |
| 36 | device_(device), |
| 37 | rpc_id_(rpc_id) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 38 | // XXX will segfault on missing properties |
| 39 | ssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 40 | properties.find(wpa_supplicant::kBSSPropertySSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 41 | operator std::vector<uint8_t>(); |
| 42 | bssid_ = |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 43 | properties.find(wpa_supplicant::kBSSPropertyBSSID)->second. |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 44 | operator std::vector<uint8_t>(); |
| 45 | signal_strength_ = |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 46 | properties.find(wpa_supplicant::kBSSPropertySignal)->second. |
| 47 | reader().get_int16(); |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 48 | map<string, ::DBus::Variant>::const_iterator it = |
| 49 | properties.find(wpa_supplicant::kBSSPropertyFrequency); |
| 50 | if (it != properties.end()) |
| 51 | frequency_ = it->second.reader().get_uint16(); |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 52 | |
| 53 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 54 | if (!ParseIEs(properties, &phy_mode, &vendor_information_)) { |
| 55 | phy_mode = DeterminePhyModeFromFrequency(properties, frequency_); |
| 56 | } |
| 57 | physical_mode_ = phy_mode; |
| 58 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 59 | network_mode_ = ParseMode( |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 60 | properties.find(wpa_supplicant::kBSSPropertyMode)->second); |
| 61 | security_mode_ = ParseSecurity(properties); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 62 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 63 | if (network_mode_.empty()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 64 | // XXX log error? |
| 65 | } |
| 66 | |
| 67 | ssid_string_ = string(ssid_.begin(), ssid_.end()); |
mukesh agrawal | 16bc1b8 | 2012-02-09 18:38:26 -0800 | [diff] [blame] | 68 | WiFi::SanitizeSSID(&ssid_string_); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 69 | ssid_hex_ = base::HexEncode(&(*ssid_.begin()), ssid_.size()); |
| 70 | bssid_string_ = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 71 | bssid_[0], bssid_[1], bssid_[2], |
| 72 | bssid_[3], bssid_[4], bssid_[5]); |
| 73 | bssid_hex_ = base::HexEncode(&(*bssid_.begin()), bssid_.size()); |
| 74 | } |
| 75 | |
| 76 | WiFiEndpoint::~WiFiEndpoint() {} |
| 77 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 78 | void WiFiEndpoint::Start() { |
| 79 | supplicant_bss_proxy_.reset( |
| 80 | proxy_factory_->CreateSupplicantBSSProxy( |
| 81 | this, rpc_id_, wpa_supplicant::kDBusAddr)); |
| 82 | } |
| 83 | |
| 84 | void WiFiEndpoint::PropertiesChanged( |
| 85 | const map<string, ::DBus::Variant> &properties) { |
Darin Petkov | 3abc3be | 2012-06-27 10:48:23 +0200 | [diff] [blame] | 86 | SLOG(WiFi, 2) << __func__; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 87 | map<string, ::DBus::Variant>::const_iterator properties_it = |
| 88 | properties.find(wpa_supplicant::kBSSPropertySignal); |
| 89 | if (properties_it != properties.end()) { |
| 90 | signal_strength_ = properties_it->second.reader().get_int16(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 91 | SLOG(WiFi, 2) << "WiFiEndpoint " << bssid_string_ << " signal is now " |
| 92 | << signal_strength_; |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 93 | device_->NotifyEndpointChanged(*this); |
| 94 | } |
| 95 | } |
| 96 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 97 | |
| 98 | map<string, string> WiFiEndpoint::GetVendorInformation() const { |
| 99 | map<string, string> vendor_information; |
| 100 | if (!vendor_information_.wps_manufacturer.empty()) { |
| 101 | vendor_information[kVendorWPSManufacturerProperty] = |
| 102 | vendor_information_.wps_manufacturer; |
| 103 | } |
| 104 | if (!vendor_information_.wps_model_name.empty()) { |
| 105 | vendor_information[kVendorWPSModelNameProperty] = |
| 106 | vendor_information_.wps_model_name; |
| 107 | } |
| 108 | if (!vendor_information_.wps_model_number.empty()) { |
| 109 | vendor_information[kVendorWPSModelNumberProperty] = |
| 110 | vendor_information_.wps_model_number; |
| 111 | } |
| 112 | if (!vendor_information_.wps_device_name.empty()) { |
| 113 | vendor_information[kVendorWPSDeviceNameProperty] = |
| 114 | vendor_information_.wps_device_name; |
| 115 | } |
| 116 | if (!vendor_information_.oui_list.empty()) { |
| 117 | vector<string> oui_list; |
| 118 | set<uint32_t>::const_iterator it; |
| 119 | for (it = vendor_information_.oui_list.begin(); |
| 120 | it != vendor_information_.oui_list.end(); |
| 121 | ++it) { |
| 122 | oui_list.push_back( |
| 123 | StringPrintf("%02x-%02x-%02x", |
| 124 | *it >> 16, (*it >> 8) & 0xff, *it & 0xff)); |
| 125 | } |
| 126 | vendor_information[kVendorOUIListProperty] = |
| 127 | JoinString(oui_list, ' '); |
| 128 | } |
| 129 | return vendor_information; |
| 130 | } |
| 131 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 132 | // static |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 133 | uint32_t WiFiEndpoint::ModeStringToUint(const string &mode_string) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 134 | if (mode_string == flimflam::kModeManaged) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 135 | return wpa_supplicant::kNetworkModeInfrastructureInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 136 | else if (mode_string == flimflam::kModeAdhoc) |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 137 | return wpa_supplicant::kNetworkModeAdHocInt; |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 138 | else |
| 139 | NOTIMPLEMENTED() << "Shill dos not support " << mode_string |
| 140 | << " mode at this time."; |
| 141 | return 0; |
| 142 | } |
| 143 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 144 | const vector<uint8_t> &WiFiEndpoint::ssid() const { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 145 | return ssid_; |
| 146 | } |
| 147 | |
| 148 | const string &WiFiEndpoint::ssid_string() const { |
| 149 | return ssid_string_; |
| 150 | } |
| 151 | |
| 152 | const string &WiFiEndpoint::ssid_hex() const { |
| 153 | return ssid_hex_; |
| 154 | } |
| 155 | |
| 156 | const string &WiFiEndpoint::bssid_string() const { |
| 157 | return bssid_string_; |
| 158 | } |
| 159 | |
| 160 | const string &WiFiEndpoint::bssid_hex() const { |
| 161 | return bssid_hex_; |
| 162 | } |
| 163 | |
| 164 | int16_t WiFiEndpoint::signal_strength() const { |
| 165 | return signal_strength_; |
| 166 | } |
| 167 | |
Thieu Le | e41a72d | 2012-02-06 20:46:51 +0000 | [diff] [blame] | 168 | uint16 WiFiEndpoint::frequency() const { |
| 169 | return frequency_; |
| 170 | } |
| 171 | |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 172 | uint16 WiFiEndpoint::physical_mode() const { |
| 173 | return physical_mode_; |
| 174 | } |
| 175 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 176 | const string &WiFiEndpoint::network_mode() const { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 177 | return network_mode_; |
| 178 | } |
| 179 | |
| 180 | const string &WiFiEndpoint::security_mode() const { |
| 181 | return security_mode_; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 184 | // static |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 185 | WiFiEndpoint *WiFiEndpoint::MakeOpenEndpoint(ProxyFactory *proxy_factory, |
| 186 | const WiFiRefPtr &wifi, |
| 187 | const string &ssid, |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 188 | const string &bssid, |
| 189 | uint16 frequency, |
| 190 | int16 signal_dbm) { |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 191 | map <string, ::DBus::Variant> args; |
| 192 | ::DBus::MessageIter writer; |
| 193 | |
| 194 | writer = args[wpa_supplicant::kBSSPropertySSID].writer(); |
| 195 | writer << vector<uint8_t>(ssid.begin(), ssid.end()); |
| 196 | |
| 197 | string bssid_nosep; |
| 198 | RemoveChars(bssid, ":", &bssid_nosep); |
| 199 | vector<uint8_t> bssid_bytes; |
| 200 | base::HexStringToBytes(bssid_nosep, &bssid_bytes); |
| 201 | writer = args[wpa_supplicant::kBSSPropertyBSSID].writer(); |
| 202 | writer << bssid_bytes; |
| 203 | |
mukesh agrawal | e1d90e9 | 2012-02-15 17:36:08 -0800 | [diff] [blame] | 204 | args[wpa_supplicant::kBSSPropertySignal].writer().append_int16(signal_dbm); |
| 205 | args[wpa_supplicant::kBSSPropertyFrequency].writer().append_uint16(frequency); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 206 | args[wpa_supplicant::kBSSPropertyMode].writer().append_string( |
| 207 | wpa_supplicant::kNetworkModeInfrastructure); |
| 208 | // We indicate this is an open BSS by leaving out all security properties. |
| 209 | |
mukesh agrawal | b20776f | 2012-02-10 16:00:36 -0800 | [diff] [blame] | 210 | return new WiFiEndpoint( |
| 211 | proxy_factory, wifi, bssid, args); // |bssid| fakes an RPC ID |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // static |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 215 | const char *WiFiEndpoint::ParseMode(const string &mode_string) { |
| 216 | if (mode_string == wpa_supplicant::kNetworkModeInfrastructure) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 217 | return flimflam::kModeManaged; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 218 | } else if (mode_string == wpa_supplicant::kNetworkModeAdHoc) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 219 | return flimflam::kModeAdhoc; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 220 | } else if (mode_string == wpa_supplicant::kNetworkModeAccessPoint) { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 221 | NOTREACHED() << "Shill does not support AP mode at this time."; |
| 222 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 223 | } else { |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 224 | NOTREACHED() << "Unknown WiFi endpoint mode!"; |
| 225 | return NULL; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 229 | // static |
| 230 | const char *WiFiEndpoint::ParseSecurity( |
| 231 | const map<string, ::DBus::Variant> &properties) { |
| 232 | set<KeyManagement> rsn_key_management_methods; |
| 233 | if (ContainsKey(properties, wpa_supplicant::kPropertyRSN)) { |
| 234 | // TODO(quiche): check type before casting |
| 235 | const map<string, ::DBus::Variant> rsn_properties( |
| 236 | properties.find(wpa_supplicant::kPropertyRSN)->second. |
| 237 | operator map<string, ::DBus::Variant>()); |
| 238 | ParseKeyManagementMethods(rsn_properties, &rsn_key_management_methods); |
| 239 | } |
| 240 | |
| 241 | set<KeyManagement> wpa_key_management_methods; |
| 242 | if (ContainsKey(properties, wpa_supplicant::kPropertyWPA)) { |
mukesh agrawal | 165e614 | 2011-11-22 02:22:56 +0000 | [diff] [blame] | 243 | // TODO(quiche): check type before casting |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 244 | const map<string, ::DBus::Variant> rsn_properties( |
| 245 | properties.find(wpa_supplicant::kPropertyWPA)->second. |
| 246 | operator map<string, ::DBus::Variant>()); |
| 247 | ParseKeyManagementMethods(rsn_properties, &wpa_key_management_methods); |
| 248 | } |
| 249 | |
| 250 | bool wep_privacy = false; |
| 251 | if (ContainsKey(properties, wpa_supplicant::kPropertyPrivacy)) { |
| 252 | wep_privacy = properties.find(wpa_supplicant::kPropertyPrivacy)->second. |
| 253 | reader().get_bool(); |
| 254 | } |
| 255 | |
| 256 | if (ContainsKey(rsn_key_management_methods, kKeyManagement802_1x) || |
| 257 | ContainsKey(wpa_key_management_methods, kKeyManagement802_1x)) { |
| 258 | return flimflam::kSecurity8021x; |
| 259 | } else if (ContainsKey(rsn_key_management_methods, kKeyManagementPSK)) { |
| 260 | return flimflam::kSecurityRsn; |
| 261 | } else if (ContainsKey(wpa_key_management_methods, kKeyManagementPSK)) { |
| 262 | return flimflam::kSecurityWpa; |
| 263 | } else if (wep_privacy) { |
| 264 | return flimflam::kSecurityWep; |
| 265 | } else { |
| 266 | return flimflam::kSecurityNone; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // static |
| 271 | void WiFiEndpoint::ParseKeyManagementMethods( |
| 272 | const map<string, ::DBus::Variant> &security_method_properties, |
| 273 | set<KeyManagement> *key_management_methods) { |
| 274 | if (!ContainsKey(security_method_properties, |
| 275 | wpa_supplicant::kSecurityMethodPropertyKeyManagement)) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // TODO(quiche): check type before cast |
| 280 | const vector<string> key_management_vec = |
| 281 | security_method_properties. |
| 282 | find(wpa_supplicant::kSecurityMethodPropertyKeyManagement)->second. |
| 283 | operator vector<string>(); |
| 284 | for (vector<string>::const_iterator it = key_management_vec.begin(); |
| 285 | it != key_management_vec.end(); |
| 286 | ++it) { |
| 287 | if (EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixEAP, true)) { |
| 288 | key_management_methods->insert(kKeyManagement802_1x); |
| 289 | } else if ( |
| 290 | EndsWith(*it, wpa_supplicant::kKeyManagementMethodSuffixPSK, true)) { |
| 291 | key_management_methods->insert(kKeyManagementPSK); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 296 | // static |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 297 | Metrics::WiFiNetworkPhyMode WiFiEndpoint::DeterminePhyModeFromFrequency( |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 298 | const map<string, ::DBus::Variant> &properties, uint16 frequency) { |
| 299 | uint32_t max_rate = 0; |
| 300 | map<string, ::DBus::Variant>::const_iterator it = |
| 301 | properties.find(wpa_supplicant::kBSSPropertyRates); |
| 302 | if (it != properties.end()) { |
| 303 | vector<uint32_t> rates = it->second.operator vector<uint32_t>(); |
| 304 | if (rates.size() > 0) |
| 305 | max_rate = rates[0]; // Rates are sorted in descending order |
| 306 | } |
| 307 | |
| 308 | Metrics::WiFiNetworkPhyMode phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 309 | if (frequency < 3000) { |
| 310 | // 2.4GHz legacy, check for tx rate for 11b-only |
| 311 | // (note 22M is valid) |
| 312 | if (max_rate < 24000000) |
| 313 | phy_mode = Metrics::kWiFiNetworkPhyMode11b; |
| 314 | else |
| 315 | phy_mode = Metrics::kWiFiNetworkPhyMode11g; |
| 316 | } else { |
| 317 | phy_mode = Metrics::kWiFiNetworkPhyMode11a; |
| 318 | } |
| 319 | |
| 320 | return phy_mode; |
| 321 | } |
| 322 | |
| 323 | // static |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 324 | bool WiFiEndpoint::ParseIEs( |
| 325 | const map<string, ::DBus::Variant> &properties, |
| 326 | Metrics::WiFiNetworkPhyMode *phy_mode, |
| 327 | VendorInformation *vendor_information) { |
| 328 | |
| 329 | map<string, ::DBus::Variant>::const_iterator ies_property = |
| 330 | properties.find(wpa_supplicant::kBSSPropertyIEs); |
| 331 | if (ies_property == properties.end()) { |
| 332 | SLOG(WiFi, 2) << __func__ << ": No IE property in BSS."; |
| 333 | return false; |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Paul Stewart | 72b2fdc | 2012-06-02 08:58:51 -0700 | [diff] [blame] | 336 | vector<uint8_t> ies = ies_property->second.operator vector<uint8_t>(); |
| 337 | |
| 338 | |
| 339 | // Format of an information element: |
| 340 | // 1 1 1 - 252 |
| 341 | // +------+--------+----------------+ |
| 342 | // | Type | Length | Data | |
| 343 | // +------+--------+----------------+ |
| 344 | *phy_mode = Metrics::kWiFiNetworkPhyModeUndef; |
| 345 | bool found_ht = false; |
| 346 | bool found_erp = false; |
| 347 | int ie_len = 0; |
| 348 | vector<uint8_t>::iterator it; |
| 349 | for (it = ies.begin(); |
| 350 | std::distance(it, ies.end()) > 1; // Ensure Length field is within PDU. |
| 351 | it += ie_len) { |
| 352 | ie_len = 2 + *(it + 1); |
| 353 | if (std::distance(it, ies.end()) < ie_len) { |
| 354 | LOG(ERROR) << __func__ << ": IE extends past containing PDU."; |
| 355 | break; |
| 356 | } |
| 357 | switch (*it) { |
| 358 | case IEEE_80211::kElemIdErp: |
| 359 | if (!found_ht) { |
| 360 | *phy_mode = Metrics::kWiFiNetworkPhyMode11g; |
| 361 | } |
| 362 | found_erp = true; |
| 363 | break; |
| 364 | case IEEE_80211::kElemIdHTCap: |
| 365 | case IEEE_80211::kElemIdHTInfo: |
| 366 | *phy_mode = Metrics::kWiFiNetworkPhyMode11n; |
| 367 | found_ht = true; |
| 368 | break; |
| 369 | case IEEE_80211::kElemIdVendor: |
| 370 | ParseVendorIE(it + 2, it + ie_len, vendor_information); |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | return found_ht || found_erp; |
| 375 | } |
| 376 | |
| 377 | // static |
| 378 | void WiFiEndpoint::ParseVendorIE(vector<uint8_t>::const_iterator ie, |
| 379 | vector<uint8_t>::const_iterator end, |
| 380 | VendorInformation *vendor_information) { |
| 381 | // Format of an vendor-specific information element (with type |
| 382 | // and length field for the IE removed by the caller): |
| 383 | // 3 1 1 - 248 |
| 384 | // +------------+----------+----------------+ |
| 385 | // | OUI | OUI Type | Data | |
| 386 | // +------------+----------+----------------+ |
| 387 | |
| 388 | if (std::distance(ie, end) < 4) { |
| 389 | LOG(ERROR) << __func__ << ": no room in IE for OUI and type field."; |
| 390 | return; |
| 391 | } |
| 392 | uint32_t oui = (*ie << 16) | (*(ie + 1) << 8) | *(ie + 2); |
| 393 | uint8_t oui_type = *(ie + 3); |
| 394 | ie += 4; |
| 395 | |
| 396 | if (oui == IEEE_80211::kOUIVendorMicrosoft && |
| 397 | oui_type == IEEE_80211::kOUIMicrosoftWPS) { |
| 398 | // Format of a WPS data element: |
| 399 | // 2 2 |
| 400 | // +------+--------+----------------+ |
| 401 | // | Type | Length | Data | |
| 402 | // +------+--------+----------------+ |
| 403 | while (std::distance(ie, end) >= 4) { |
| 404 | int element_type = (*ie << 8) | *(ie + 1); |
| 405 | int element_length = (*(ie + 2) << 8) | *(ie + 3); |
| 406 | ie += 4; |
| 407 | if (std::distance(ie, end) < element_length) { |
| 408 | LOG(ERROR) << __func__ << ": WPS element extends past containing PDU."; |
| 409 | break; |
| 410 | } |
| 411 | string s(ie, ie + element_length); |
| 412 | if (IsStringASCII(s)) { |
| 413 | switch (element_type) { |
| 414 | case IEEE_80211::kWPSElementManufacturer: |
| 415 | vendor_information->wps_manufacturer = s; |
| 416 | break; |
| 417 | case IEEE_80211::kWPSElementModelName: |
| 418 | vendor_information->wps_model_name = s; |
| 419 | break; |
| 420 | case IEEE_80211::kWPSElementModelNumber: |
| 421 | vendor_information->wps_model_number = s; |
| 422 | break; |
| 423 | case IEEE_80211::kWPSElementDeviceName: |
| 424 | vendor_information->wps_device_name = s; |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | ie += element_length; |
| 429 | } |
| 430 | } else if (oui != IEEE_80211::kOUIVendorEpigram && |
| 431 | oui != IEEE_80211::kOUIVendorMicrosoft) { |
| 432 | vendor_information->oui_list.insert(oui); |
| 433 | } |
Thieu Le | 1df7f4e | 2012-02-10 15:21:45 -0800 | [diff] [blame] | 434 | } |
| 435 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 436 | } // namespace shill |