Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/wifi.h" |
| 6 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 7 | #include <time.h> |
| 8 | #include <stdio.h> |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 9 | #include <string.h> |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 10 | #include <netinet/ether.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 11 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 12 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 13 | #include <map> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 14 | #include <string> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 15 | #include <vector> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 16 | |
| 17 | #include <base/logging.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 18 | #include <base/string_number_conversions.h> |
| 19 | #include <base/string_util.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 20 | #include <chromeos/dbus/service_constants.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 21 | |
| 22 | #include "shill/control_interface.h" |
| 23 | #include "shill/device.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 24 | #include "shill/error.h" |
| 25 | #include "shill/key_value_store.h" |
| 26 | #include "shill/ieee80211.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 27 | #include "shill/manager.h" |
| 28 | #include "shill/profile.h" |
Darin Petkov | d196726 | 2011-07-18 14:55:18 -0700 | [diff] [blame] | 29 | #include "shill/proxy_factory.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 30 | #include "shill/shill_event.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 31 | #include "shill/supplicant_interface_proxy_interface.h" |
| 32 | #include "shill/supplicant_process_proxy_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 33 | #include "shill/wifi_endpoint.h" |
| 34 | #include "shill/wifi_service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 35 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 36 | using std::map; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 37 | using std::string; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 38 | using std::vector; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 39 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 40 | namespace shill { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 41 | |
| 42 | // statics |
| 43 | // |
| 44 | // Note that WiFi generates some manager-level errors, because it implements |
| 45 | // the Manager.GetWiFiService flimflam API. The API is implemented here, |
| 46 | // rather than in manager, to keep WiFi-specific logic in the right place. |
| 47 | const char WiFi::kManagerErrorPassphraseRequired[] = "must specify passphrase"; |
| 48 | const char WiFi::kManagerErrorSSIDRequired[] = "must specify SSID"; |
| 49 | const char WiFi::kManagerErrorSSIDTooLong[] = "SSID is too long"; |
| 50 | const char WiFi::kManagerErrorSSIDTooShort[] = "SSID is too short"; |
| 51 | const char WiFi::kManagerErrorTypeRequired[] = "must specify service type"; |
| 52 | const char WiFi::kManagerErrorUnsupportedSecurityMode[] = |
| 53 | "security mode is unsupported"; |
| 54 | const char WiFi::kManagerErrorUnsupportedServiceType[] = |
| 55 | "service type is unsupported"; |
| 56 | const char WiFi::kManagerErrorUnsupportedServiceMode[] = |
| 57 | "service mode is unsupported"; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 58 | const char WiFi::kSupplicantPath[] = "/fi/w1/wpa_supplicant1"; |
| 59 | const char WiFi::kSupplicantDBusAddr[] = "fi.w1.wpa_supplicant1"; |
| 60 | const char WiFi::kSupplicantWiFiDriver[] = "nl80211"; |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 61 | const char WiFi::kSupplicantErrorInterfaceExists[] = |
| 62 | "fi.w1.wpa_supplicant1.InterfaceExists"; |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 63 | const char WiFi::kSupplicantPropertySSID[] = "ssid"; |
| 64 | const char WiFi::kSupplicantPropertyNetworkMode[] = "mode"; |
| 65 | const char WiFi::kSupplicantPropertyKeyMode[] = "key_mgmt"; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 66 | const char WiFi::kSupplicantPropertyScanType[] = "Type"; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 67 | const char WiFi::kSupplicantKeyModeNone[] = "NONE"; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 68 | const char WiFi::kSupplicantScanTypeActive[] = "active"; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 69 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 70 | // NB: we assume supplicant is already running. [quiche.20110518] |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 71 | WiFi::WiFi(ControlInterface *control_interface, |
| 72 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 73 | Manager *manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 74 | const string& link, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 75 | const std::string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 76 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 77 | : Device(control_interface, |
| 78 | dispatcher, |
| 79 | manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 80 | link, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 81 | address, |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 82 | interface_index), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 83 | task_factory_(this), |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 84 | bgscan_short_interval_(0), |
| 85 | bgscan_signal_threshold_(0), |
| 86 | scan_pending_(false), |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 87 | scan_interval_(0), |
| 88 | link_up_(false) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 89 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 90 | store->RegisterString(flimflam::kBgscanMethodProperty, &bgscan_method_); |
| 91 | store->RegisterUint16(flimflam::kBgscanShortIntervalProperty, |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 92 | &bgscan_short_interval_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 93 | store->RegisterInt32(flimflam::kBgscanSignalThresholdProperty, |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 94 | &bgscan_signal_threshold_); |
| 95 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 96 | // TODO(quiche): Decide if scan_pending_ is close enough to |
| 97 | // "currently scanning" that we don't care, or if we want to track |
| 98 | // scan pending/currently scanning/no scan scheduled as a tri-state |
| 99 | // kind of thing. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 100 | store->RegisterConstBool(flimflam::kScanningProperty, &scan_pending_); |
| 101 | store->RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_); |
| 102 | VLOG(2) << "WiFi device " << link_name() << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 103 | } |
| 104 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 105 | WiFi::~WiFi() {} |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 106 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 107 | void WiFi::Start() { |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 108 | ::DBus::Path interface_path; |
| 109 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 110 | supplicant_process_proxy_.reset( |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 111 | ProxyFactory::factory()->CreateSupplicantProcessProxy( |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 112 | kSupplicantPath, kSupplicantDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 113 | try { |
| 114 | std::map<string, DBus::Variant> create_interface_args; |
| 115 | create_interface_args["Ifname"].writer(). |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 116 | append_string(link_name().c_str()); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 117 | create_interface_args["Driver"].writer(). |
| 118 | append_string(kSupplicantWiFiDriver); |
| 119 | // TODO(quiche) create_interface_args["ConfigFile"].writer().append_string |
| 120 | // (file with pkcs config info) |
| 121 | interface_path = |
| 122 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
| 123 | } catch (const DBus::Error e) { // NOLINT |
| 124 | if (!strcmp(e.name(), kSupplicantErrorInterfaceExists)) { |
| 125 | interface_path = |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 126 | supplicant_process_proxy_->GetInterface(link_name()); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 127 | // XXX crash here, if device missing? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 128 | } else { |
| 129 | // XXX |
| 130 | } |
| 131 | } |
| 132 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 133 | supplicant_interface_proxy_.reset( |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 134 | ProxyFactory::factory()->CreateSupplicantInterfaceProxy( |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 135 | this, interface_path, kSupplicantDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 136 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 137 | // TODO(quiche) set ApScan=1 and BSSExpireAge=190, like flimflam does? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 138 | |
| 139 | // clear out any networks that might previously have been configured |
| 140 | // for this interface. |
| 141 | supplicant_interface_proxy_->RemoveAllNetworks(); |
| 142 | |
| 143 | // flush interface's BSS cache, so that we get BSSAdded signals for |
| 144 | // all BSSes (not just new ones since the last scan). |
| 145 | supplicant_interface_proxy_->FlushBSS(0); |
| 146 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 147 | Scan(NULL); |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 148 | Device::Start(); |
| 149 | } |
| 150 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 151 | void WiFi::Stop() { |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 152 | VLOG(2) << "WiFi " << link_name() << " stopping."; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 153 | // TODO(quiche): remove interface from supplicant |
| 154 | supplicant_interface_proxy_.reset(); // breaks a reference cycle |
| 155 | supplicant_process_proxy_.reset(); |
| 156 | endpoint_by_bssid_.clear(); |
| 157 | service_by_private_id_.clear(); // breaks reference cycles |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 158 | |
| 159 | for (std::vector<ServiceRefPtr>::const_iterator it = services()->begin(); |
| 160 | it != services()->end(); |
| 161 | ++it) { |
| 162 | manager()->DeregisterService(*it); |
| 163 | } |
| 164 | services()->clear(); // breaks reference cycles |
| 165 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 166 | Device::Stop(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 167 | // XXX anything else to do? |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 168 | |
| 169 | VLOG(3) << "WiFi " << link_name() << " task_factory_ " |
| 170 | << (task_factory_.empty() ? "is empty." : "is not empty."); |
| 171 | VLOG(3) << "WiFi " << link_name() << " supplicant_process_proxy_ " |
| 172 | << (supplicant_process_proxy_.get() ? "is set." : "is not set."); |
| 173 | VLOG(3) << "WiFi " << link_name() << " supplicant_interface_proxy_ " |
| 174 | << (supplicant_interface_proxy_.get() ? "is set." : "is not set."); |
| 175 | VLOG(3) << "WiFi " << link_name() << " has " << endpoint_by_bssid_.size() |
| 176 | << " EndpointMap entries."; |
| 177 | VLOG(3) << "WiFi " << link_name() << " has " << service_by_private_id_.size() |
| 178 | << " ServiceMap entries."; |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 181 | void WiFi::Scan(Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 182 | LOG(INFO) << __func__; |
| 183 | |
| 184 | // needs to send a D-Bus message, but may be called from D-Bus |
| 185 | // signal handler context (via Manager::RequestScan). so defer work |
| 186 | // to event loop. |
| 187 | dispatcher()->PostTask( |
| 188 | task_factory_.NewRunnableMethod(&WiFi::ScanTask)); |
| 189 | } |
| 190 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 191 | bool WiFi::TechnologyIs(const Device::Technology type) const { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 192 | return type == Device::kWifi; |
| 193 | } |
| 194 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 195 | void WiFi::LinkEvent(unsigned int flags, unsigned int change) { |
| 196 | // TODO(quiche): figure out how to relate these events to supplicant |
| 197 | // events. e.g., may be we can ignore LinkEvent, in favor of events |
| 198 | // from SupplicantInterfaceProxy? |
| 199 | Device::LinkEvent(flags, change); |
| 200 | if ((flags & IFF_LOWER_UP) != 0 && !link_up_) { |
| 201 | LOG(INFO) << link_name() << " is up; should start L3!"; |
| 202 | link_up_ = true; |
| 203 | if (AcquireDHCPConfig()) { |
| 204 | SetServiceState(Service::kStateConfiguring); |
| 205 | } else { |
| 206 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 207 | } |
| 208 | } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) { |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 209 | LOG(INFO) << link_name() << " is down"; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 210 | link_up_ = false; |
| 211 | // TODO(quiche): attempt to reconnect to current SSID, another SSID, |
| 212 | // or initiate a scan. |
| 213 | } |
| 214 | } |
| 215 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 216 | void WiFi::BSSAdded( |
| 217 | const ::DBus::Path &BSS, |
| 218 | const std::map<string, ::DBus::Variant> &properties) { |
| 219 | // TODO(quiche): write test to verify correct behavior in the case |
| 220 | // where we get multiple BSSAdded events for a single endpoint. |
| 221 | // (old Endpoint's refcount should fall to zero, and old Endpoint |
| 222 | // should be destroyed) |
| 223 | // |
| 224 | // note: we assume that BSSIDs are unique across endpoints. this |
| 225 | // means that if an AP reuses the same BSSID for multiple SSIDs, we |
| 226 | // lose. |
| 227 | WiFiEndpointRefPtr endpoint(new WiFiEndpoint(properties)); |
| 228 | endpoint_by_bssid_[endpoint->bssid_hex()] = endpoint; |
| 229 | } |
| 230 | |
| 231 | void WiFi::ScanDone() { |
| 232 | LOG(INFO) << __func__; |
| 233 | |
| 234 | // defer handling of scan result processing, because that processing |
| 235 | // may require the the registration of new D-Bus objects. and such |
| 236 | // registration can't be done in the context of a D-Bus signal |
| 237 | // handler. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 238 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 239 | task_factory_.NewRunnableMethod(&WiFi::ScanDoneTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 240 | } |
| 241 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 242 | void WiFi::ConnectTo(WiFiService *service) { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 243 | std::map<string, DBus::Variant> add_network_args; |
| 244 | DBus::MessageIter writer; |
| 245 | DBus::Path network_path; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 246 | |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 247 | // TODO(quiche): handle cases where already connected |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 248 | |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 249 | add_network_args[kSupplicantPropertyNetworkMode].writer(). |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 250 | append_uint32(WiFiEndpoint::ModeStringToUint(service->mode())); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 251 | add_network_args[kSupplicantPropertyKeyMode].writer(). |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 252 | append_string(service->key_management().c_str()); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 253 | // TODO(quiche): figure out why we can't use operator<< without the |
| 254 | // temporary variable. |
| 255 | writer = add_network_args[kSupplicantPropertySSID].writer(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 256 | writer << service->ssid(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 257 | // TODO(quiche): set scan_ssid=1, like flimflam does? |
| 258 | |
| 259 | network_path = |
| 260 | supplicant_interface_proxy_->AddNetwork(add_network_args); |
| 261 | supplicant_interface_proxy_->SelectNetwork(network_path); |
| 262 | // XXX add to favorite networks list? |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 263 | |
| 264 | // SelectService here (instead of in LinkEvent, like Ethernet), so |
| 265 | // that, if we fail to bring up L2, we can attribute failure correctly. |
| 266 | // |
| 267 | // TODO(quiche): when we add code for dealing with connection failures, |
| 268 | // reconsider if this is the right place to change the selected service. |
| 269 | // see discussion in crosbug.com/20191. |
| 270 | SelectService(service); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 271 | } |
| 272 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 273 | void WiFi::ScanDoneTask() { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 274 | LOG(INFO) << __func__; |
| 275 | |
| 276 | scan_pending_ = false; |
| 277 | |
| 278 | // TODO(quiche): group endpoints into services, instead of creating |
| 279 | // a service for every endpoint. |
| 280 | for (EndpointMap::iterator i(endpoint_by_bssid_.begin()); |
| 281 | i != endpoint_by_bssid_.end(); ++i) { |
| 282 | const WiFiEndpoint &endpoint(*(i->second)); |
| 283 | string service_id_private; |
| 284 | |
| 285 | service_id_private = |
| 286 | endpoint.ssid_hex() + "_" + endpoint.bssid_hex(); |
| 287 | if (service_by_private_id_.find(service_id_private) == |
| 288 | service_by_private_id_.end()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 289 | LOG(INFO) << "found new endpoint. " |
| 290 | << "ssid: " << endpoint.ssid_string() << ", " |
| 291 | << "bssid: " << endpoint.bssid_string() << ", " |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 292 | << "signal: " << endpoint.signal_strength(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 293 | |
| 294 | // XXX key mode should reflect endpoint params (not always use |
| 295 | // kSupplicantKeyModeNone) |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 296 | WiFiServiceRefPtr service( |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 297 | new WiFiService(control_interface(), |
| 298 | dispatcher(), |
| 299 | manager(), |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 300 | this, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 301 | endpoint.ssid(), |
| 302 | endpoint.network_mode(), |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 303 | kSupplicantKeyModeNone)); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 304 | services()->push_back(service); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 305 | service_by_private_id_[service_id_private] = service; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 306 | manager()->RegisterService(service); |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 307 | |
| 308 | LOG(INFO) << "new service " << service->GetRpcIdentifier(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 312 | // TODO(quiche): unregister removed services from manager |
| 313 | } |
| 314 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 315 | void WiFi::ScanTask() { |
| 316 | VLOG(2) << "WiFi " << link_name() << " scan requested."; |
| 317 | std::map<string, DBus::Variant> scan_args; |
| 318 | scan_args[kSupplicantPropertyScanType].writer(). |
| 319 | append_string(kSupplicantScanTypeActive); |
| 320 | // TODO(quiche) indicate scanning in UI |
| 321 | supplicant_interface_proxy_->Scan(scan_args); |
| 322 | scan_pending_ = true; |
| 323 | } |
| 324 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame^] | 325 | // used by manager, via static WiFi::GetService method |
| 326 | WiFiServiceRefPtr WiFi::GetService(const KeyValueStore &args, Error *error) { |
| 327 | if (!args.ContainsString(flimflam::kTypeProperty)) { |
| 328 | error->Populate(Error::kInvalidArguments, kManagerErrorTypeRequired); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | if (args.GetString(flimflam::kTypeProperty) != flimflam::kTypeWifi) { |
| 333 | error->Populate(Error::kNotSupported, kManagerErrorUnsupportedServiceType); |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | if (args.ContainsString(flimflam::kModeProperty) && |
| 338 | args.GetString(flimflam::kModeProperty) != |
| 339 | flimflam::kModeManaged) { |
| 340 | error->Populate(Error::kNotSupported, kManagerErrorUnsupportedServiceMode); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | if (!args.ContainsString(flimflam::kSSIDProperty)) { |
| 345 | error->Populate(Error::kInvalidArguments, kManagerErrorSSIDRequired); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | string ssid = args.GetString(flimflam::kSSIDProperty); |
| 350 | if (ssid.length() < 1) { |
| 351 | error->Populate(Error::kInvalidNetworkName, kManagerErrorSSIDTooShort); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | if (ssid.length() > IEEE_80211::kMaxSSIDLen) { |
| 356 | error->Populate(Error::kInvalidNetworkName, kManagerErrorSSIDTooLong); |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | string security_method; |
| 361 | if (args.ContainsString(flimflam::kSecurityProperty)) { |
| 362 | security_method = args.GetString(flimflam::kSecurityProperty); |
| 363 | } else { |
| 364 | security_method = flimflam::kSecurityNone; |
| 365 | } |
| 366 | |
| 367 | if (security_method != flimflam::kSecurityNone && |
| 368 | security_method != flimflam::kSecurityWep && |
| 369 | security_method != flimflam::kSecurityPsk && |
| 370 | security_method != flimflam::kSecurityWpa && |
| 371 | security_method != flimflam::kSecurityRsn && |
| 372 | security_method != flimflam::kSecurity8021x) { |
| 373 | error->Populate(Error::kNotSupported, |
| 374 | kManagerErrorUnsupportedSecurityMode); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | if ((security_method == flimflam::kSecurityWep || |
| 379 | security_method == flimflam::kSecurityPsk || |
| 380 | security_method == flimflam::kSecurityWpa || |
| 381 | security_method == flimflam::kSecurityRsn) && |
| 382 | !args.ContainsString(flimflam::kPassphraseProperty)) { |
| 383 | error->Populate(Error::kInvalidArguments, |
| 384 | kManagerErrorPassphraseRequired); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | if (security_method == flimflam::kSecurityWep) { |
| 389 | string passphrase = args.GetString(flimflam::kPassphraseProperty); |
| 390 | passphrase = ParseWEPPassphrase(passphrase, error); |
| 391 | if (error->IsFailure()) { |
| 392 | return NULL; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | WiFiService *service = NULL; |
| 397 | |
| 398 | // TODO(quiche): search for existing service |
| 399 | |
| 400 | if (service == NULL) { |
| 401 | // TODO(quiche): construct a new service |
| 402 | } |
| 403 | |
| 404 | // TODO(quiche): apply configuration parameters |
| 405 | |
| 406 | return service; |
| 407 | } |
| 408 | |
| 409 | // static |
| 410 | string WiFi::ParseWEPPassphrase(const string &passphrase, Error *error) { |
| 411 | unsigned int length = passphrase.length(); |
| 412 | |
| 413 | switch (length) { |
| 414 | case IEEE_80211::kWEP40AsciiLen: |
| 415 | case IEEE_80211::kWEP104AsciiLen: |
| 416 | break; |
| 417 | case IEEE_80211::kWEP40AsciiLen + 2: |
| 418 | case IEEE_80211::kWEP104AsciiLen + 2: |
| 419 | CheckWEPKeyIndex(passphrase, error); |
| 420 | break; |
| 421 | case IEEE_80211::kWEP40HexLen: |
| 422 | case IEEE_80211::kWEP104HexLen: |
| 423 | CheckWEPIsHex(passphrase, error); |
| 424 | break; |
| 425 | case IEEE_80211::kWEP40HexLen + 2: |
| 426 | case IEEE_80211::kWEP104HexLen + 2: |
| 427 | (CheckWEPKeyIndex(passphrase, error) || |
| 428 | CheckWEPPrefix(passphrase, error)) && |
| 429 | CheckWEPIsHex(passphrase.substr(2), error); |
| 430 | break; |
| 431 | case IEEE_80211::kWEP40HexLen + 4: |
| 432 | case IEEE_80211::kWEP104HexLen + 4: |
| 433 | CheckWEPKeyIndex(passphrase, error) && |
| 434 | CheckWEPPrefix(passphrase.substr(2), error) && |
| 435 | CheckWEPIsHex(passphrase.substr(4), error); |
| 436 | break; |
| 437 | default: |
| 438 | error->Populate(Error::kInvalidPassphrase); |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | // TODO(quiche): may need to normalize passphrase format |
| 443 | if (error->IsSuccess()) { |
| 444 | return passphrase; |
| 445 | } else { |
| 446 | return ""; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // static |
| 451 | bool WiFi::CheckWEPIsHex(const string &passphrase, Error *error) { |
| 452 | vector<uint8> passphrase_bytes; |
| 453 | if (base::HexStringToBytes(passphrase, &passphrase_bytes)) { |
| 454 | return true; |
| 455 | } else { |
| 456 | error->Populate(Error::kInvalidPassphrase); |
| 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // static |
| 462 | bool WiFi::CheckWEPKeyIndex(const string &passphrase, Error *error) { |
| 463 | if (StartsWithASCII(passphrase, "0:", false) || |
| 464 | StartsWithASCII(passphrase, "1:", false) || |
| 465 | StartsWithASCII(passphrase, "2:", false) || |
| 466 | StartsWithASCII(passphrase, "3:", false)) { |
| 467 | return true; |
| 468 | } else { |
| 469 | error->Populate(Error::kInvalidPassphrase); |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // static |
| 475 | bool WiFi::CheckWEPPrefix(const string &passphrase, Error *error) { |
| 476 | if (StartsWithASCII(passphrase, "0x", false)) { |
| 477 | return true; |
| 478 | } else { |
| 479 | error->Populate(Error::kInvalidPassphrase); |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 484 | } // namespace shill |