Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -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 | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 5 | #include "shill/ethernet.h" |
| 6 | |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 7 | #include <linux/ethtool.h> |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 8 | #include <netinet/ether.h> |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 9 | #include <netinet/in.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <linux/if.h> // Needs definitions from netinet/ether.h |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 11 | #include <linux/sockios.h> |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <time.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 16 | #include <map> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 17 | #include <string> |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 18 | #include <vector> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 19 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 20 | #include <base/bind.h> |
| 21 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 22 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
| 24 | #include "shill/device.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 25 | #include "shill/device_info.h" |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 26 | #include "shill/eap_credentials.h" |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 27 | #include "shill/eap_listener.h" |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 28 | #include "shill/ethernet_eap_provider.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 29 | #include "shill/ethernet_service.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 31 | #include "shill/logging.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 32 | #include "shill/manager.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 33 | #include "shill/nss.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 34 | #include "shill/profile.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 35 | #include "shill/proxy_factory.h" |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 36 | #include "shill/rtnl_handler.h" |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 37 | #include "shill/supplicant_interface_proxy_interface.h" |
| 38 | #include "shill/supplicant_process_proxy_interface.h" |
| 39 | #include "shill/wpa_supplicant.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 40 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 41 | using std::map; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 42 | using std::string; |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 43 | using std::vector; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 44 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 45 | namespace shill { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 46 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 47 | Ethernet::Ethernet(ControlInterface *control_interface, |
| 48 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 49 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 50 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 51 | const string &link_name, |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 52 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 53 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 54 | : Device(control_interface, |
| 55 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 56 | metrics, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 57 | manager, |
| 58 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 59 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 60 | interface_index, |
| 61 | Technology::kEthernet), |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 62 | link_up_(false), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 63 | is_eap_authenticated_(false), |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 64 | is_eap_detected_(false), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 65 | eap_listener_(new EapListener(dispatcher, interface_index)), |
| 66 | nss_(NSS::GetInstance()), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 67 | proxy_factory_(ProxyFactory::GetInstance()), |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 68 | sockets_(new Sockets()), |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 69 | weak_ptr_factory_(this) { |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 70 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 71 | store->RegisterConstBool(kEapAuthenticationCompletedProperty, |
| 72 | &is_eap_authenticated_); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 73 | store->RegisterConstBool(kEapAuthenticatorDetectedProperty, |
| 74 | &is_eap_detected_); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 75 | eap_listener_->set_request_received_callback( |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 76 | base::Bind(&Ethernet::OnEapDetected, weak_ptr_factory_.GetWeakPtr())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 77 | SLOG(Ethernet, 2) << "Ethernet device " << link_name << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Ethernet::~Ethernet() { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 81 | Stop(NULL, EnabledStateChangedCallback()); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 84 | void Ethernet::Start(Error *error, |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 85 | const EnabledStateChangedCallback &/*callback*/) { |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 86 | service_ = new EthernetService(control_interface(), |
| 87 | dispatcher(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 88 | metrics(), |
Paul Stewart | 2713d6c | 2011-08-25 15:38:15 -0700 | [diff] [blame] | 89 | manager(), |
| 90 | this); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 91 | rtnl_handler()->SetInterfaceFlags(interface_index(), IFF_UP, IFF_UP); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 92 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 93 | if (error) |
| 94 | error->Reset(); // indicate immediate completion |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 95 | } |
| 96 | |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 97 | void Ethernet::Stop(Error *error, |
| 98 | const EnabledStateChangedCallback &/*callback*/) { |
Paul Stewart | f2792e8 | 2011-12-12 10:23:11 -0800 | [diff] [blame] | 99 | if (service_) { |
| 100 | manager()->DeregisterService(service_); |
| 101 | service_ = NULL; |
| 102 | } |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 103 | StopSupplicant(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 104 | OnEnabledStateChanged(EnabledStateChangedCallback(), Error()); |
| 105 | if (error) |
| 106 | error->Reset(); // indicate immediate completion |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 109 | void Ethernet::LinkEvent(unsigned int flags, unsigned int change) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 110 | Device::LinkEvent(flags, change); |
| 111 | if ((flags & IFF_LOWER_UP) != 0 && !link_up_) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 112 | link_up_ = true; |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 113 | // We EnableWakeOnLan() here, instead of in Start(), because with |
| 114 | // r8139, "ethtool -s eth0 wol g" fails when no cable is plugged |
| 115 | // in. |
| 116 | EnableWakeOnLan(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 117 | if (service_) { |
Christopher Wiley | 2f1bbf0 | 2012-10-25 15:31:13 -0700 | [diff] [blame] | 118 | LOG(INFO) << "Registering " << link_name() << " with manager."; |
| 119 | // Manager will bring up L3 for us. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 120 | manager()->RegisterService(service_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 121 | } |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 122 | eap_listener_->Start(); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 123 | } else if ((flags & IFF_LOWER_UP) == 0 && link_up_) { |
| 124 | link_up_ = false; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 125 | is_eap_detected_ = false; |
Paul Stewart | 63c9393 | 2012-01-06 11:24:12 -0800 | [diff] [blame] | 126 | DestroyIPConfig(); |
Thieu Le | 9c49707 | 2012-10-31 14:06:20 -0700 | [diff] [blame] | 127 | if (service_) |
| 128 | manager()->DeregisterService(service_); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 129 | SelectService(NULL); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 130 | GetEapProvider()->ClearCredentialChangeCallback(this); |
| 131 | SetIsEapAuthenticated(false); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 132 | StopSupplicant(); |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 133 | eap_listener_->Stop(); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Christopher Wiley | 2f1bbf0 | 2012-10-25 15:31:13 -0700 | [diff] [blame] | 137 | void Ethernet::ConnectTo(EthernetService *service) { |
| 138 | CHECK(service == service_.get()) << "Ethernet was asked to connect the " |
| 139 | << "wrong service?"; |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 140 | SelectService(service); |
Christopher Wiley | 2f1bbf0 | 2012-10-25 15:31:13 -0700 | [diff] [blame] | 141 | if (AcquireIPConfigWithLeaseName(service->GetStorageIdentifier())) { |
Christopher Wiley | 2f1bbf0 | 2012-10-25 15:31:13 -0700 | [diff] [blame] | 142 | SetServiceState(Service::kStateConfiguring); |
| 143 | } else { |
| 144 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 145 | SetServiceState(Service::kStateFailure); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 146 | DestroyIPConfig(); |
Christopher Wiley | 2f1bbf0 | 2012-10-25 15:31:13 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void Ethernet::DisconnectFrom(EthernetService *service) { |
| 151 | CHECK(service == service_.get()) << "Ethernet was asked to disconnect the " |
| 152 | << "wrong service?"; |
| 153 | DropConnection(); |
| 154 | } |
| 155 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 156 | void Ethernet::TryEapAuthentication() { |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 157 | try_eap_authentication_callback_.Reset( |
| 158 | Bind(&Ethernet::TryEapAuthenticationTask, |
| 159 | weak_ptr_factory_.GetWeakPtr())); |
| 160 | dispatcher()->PostTask(try_eap_authentication_callback_.callback()); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 163 | void Ethernet::BSSAdded(const ::DBus::Path &path, |
| 164 | const map<string, ::DBus::Variant> &properties) { |
| 165 | NOTREACHED() << __func__ << " is not implemented for Ethernet"; |
| 166 | } |
| 167 | |
| 168 | void Ethernet::BSSRemoved(const ::DBus::Path &path) { |
| 169 | NOTREACHED() << __func__ << " is not implemented for Ethernet"; |
| 170 | } |
| 171 | |
| 172 | void Ethernet::Certification(const map<string, ::DBus::Variant> &properties) { |
| 173 | string subject; |
| 174 | uint32 depth; |
| 175 | if (WPASupplicant::ExtractRemoteCertification(properties, &subject, &depth)) { |
| 176 | dispatcher()->PostTask(Bind(&Ethernet::CertificationTask, |
| 177 | weak_ptr_factory_.GetWeakPtr(), |
| 178 | subject, depth)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void Ethernet::EAPEvent(const string &status, const string ¶meter) { |
| 183 | dispatcher()->PostTask(Bind(&Ethernet::EAPEventTask, |
| 184 | weak_ptr_factory_.GetWeakPtr(), |
| 185 | status, |
| 186 | parameter)); |
| 187 | } |
| 188 | |
| 189 | void Ethernet::PropertiesChanged( |
| 190 | const map<string, ::DBus::Variant> &properties) { |
| 191 | const map<string, ::DBus::Variant>::const_iterator properties_it = |
| 192 | properties.find(WPASupplicant::kInterfacePropertyState); |
| 193 | if (properties_it == properties.end()) { |
| 194 | return; |
| 195 | } |
| 196 | dispatcher()->PostTask(Bind(&Ethernet::SupplicantStateChangedTask, |
| 197 | weak_ptr_factory_.GetWeakPtr(), |
| 198 | properties_it->second.reader().get_string())); |
| 199 | } |
| 200 | |
| 201 | void Ethernet::ScanDone() { |
| 202 | NOTREACHED() << __func__ << " is not implented for Ethernet"; |
| 203 | } |
| 204 | |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 205 | EthernetEapProvider *Ethernet::GetEapProvider() { |
| 206 | EthernetEapProvider *eap_provider = manager()->ethernet_eap_provider(); |
| 207 | CHECK(eap_provider); |
| 208 | return eap_provider; |
| 209 | } |
| 210 | |
| 211 | ServiceConstRefPtr Ethernet::GetEapService() { |
| 212 | ServiceConstRefPtr eap_service = GetEapProvider()->service(); |
| 213 | CHECK(eap_service); |
| 214 | return eap_service; |
| 215 | } |
| 216 | |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 217 | void Ethernet::OnEapDetected() { |
| 218 | is_eap_detected_ = true; |
| 219 | eap_listener_->Stop(); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 220 | GetEapProvider()->SetCredentialChangeCallback( |
| 221 | this, |
| 222 | base::Bind(&Ethernet::TryEapAuthentication, |
| 223 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 224 | TryEapAuthentication(); |
| 225 | } |
| 226 | |
| 227 | bool Ethernet::StartSupplicant() { |
| 228 | if (supplicant_process_proxy_.get()) { |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | supplicant_process_proxy_.reset( |
| 233 | proxy_factory_->CreateSupplicantProcessProxy( |
| 234 | WPASupplicant::kDBusPath, WPASupplicant::kDBusAddr)); |
| 235 | ::DBus::Path interface_path; |
| 236 | try { |
| 237 | map<string, DBus::Variant> create_interface_args; |
| 238 | create_interface_args[WPASupplicant::kInterfacePropertyName].writer(). |
| 239 | append_string(link_name().c_str()); |
| 240 | create_interface_args[WPASupplicant::kInterfacePropertyDriver].writer(). |
| 241 | append_string(WPASupplicant::kDriverWired); |
| 242 | create_interface_args[ |
| 243 | WPASupplicant::kInterfacePropertyConfigFile].writer(). |
| 244 | append_string(WPASupplicant::kSupplicantConfPath); |
| 245 | interface_path = |
| 246 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
| 247 | } catch (const DBus::Error &e) { // NOLINT |
| 248 | if (!strcmp(e.name(), WPASupplicant::kErrorInterfaceExists)) { |
| 249 | interface_path = supplicant_process_proxy_->GetInterface(link_name()); |
| 250 | } else { |
| 251 | LOG(ERROR) << __func__ << ": Failed to create interface with supplicant."; |
| 252 | StopSupplicant(); |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | supplicant_interface_proxy_.reset( |
| 257 | proxy_factory_->CreateSupplicantInterfaceProxy( |
| 258 | this, interface_path, WPASupplicant::kDBusAddr)); |
| 259 | supplicant_interface_path_ = interface_path; |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | bool Ethernet::StartEapAuthentication() { |
| 264 | map<string, DBus::Variant> params; |
| 265 | vector<char> nss_identifier(link_name().begin(), link_name().end()); |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 266 | GetEapService()->eap()->PopulateSupplicantProperties( |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 267 | &certificate_file_, nss_, nss_identifier, ¶ms); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 268 | params[WPASupplicant::kNetworkPropertyEapKeyManagement].writer(). |
| 269 | append_string(WPASupplicant::kKeyManagementIeee8021X); |
| 270 | params[WPASupplicant::kNetworkPropertyEapolFlags].writer(). |
| 271 | append_uint32(0); |
| 272 | params[WPASupplicant::kNetworkPropertyScanSSID].writer(). |
| 273 | append_uint32(0); |
| 274 | service_->ClearEAPCertification(); |
| 275 | eap_state_handler_.Reset(); |
| 276 | |
| 277 | try { |
| 278 | if (!supplicant_network_path_.empty()) { |
| 279 | supplicant_interface_proxy_->RemoveNetwork(supplicant_network_path_); |
| 280 | } |
| 281 | supplicant_network_path_ = supplicant_interface_proxy_->AddNetwork(params); |
| 282 | CHECK(!supplicant_network_path_.empty()); |
| 283 | } catch (const DBus::Error &e) { // NOLINT |
| 284 | LOG(ERROR) << "exception while adding network: " << e.what(); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | supplicant_interface_proxy_->SelectNetwork(supplicant_network_path_); |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 289 | supplicant_interface_proxy_->EAPLogon(); |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
| 293 | void Ethernet::StopSupplicant() { |
Paul Stewart | be9abfd | 2013-04-22 12:18:48 -0700 | [diff] [blame] | 294 | if (supplicant_interface_proxy_.get()) { |
| 295 | supplicant_interface_proxy_->EAPLogoff(); |
| 296 | } |
Paul Stewart | 9413bcc | 2013-04-04 16:12:43 -0700 | [diff] [blame] | 297 | supplicant_interface_proxy_.reset(); |
| 298 | if (!supplicant_interface_path_.empty() && supplicant_process_proxy_.get()) { |
| 299 | try { |
| 300 | supplicant_process_proxy_->RemoveInterface( |
| 301 | ::DBus::Path(supplicant_interface_path_)); |
| 302 | } catch (const DBus::Error &e) { // NOLINT |
| 303 | LOG(ERROR) << __func__ << ": Failed to remove interface from supplicant."; |
| 304 | } |
| 305 | } |
| 306 | supplicant_network_path_ = ""; |
| 307 | supplicant_interface_path_ = ""; |
| 308 | supplicant_process_proxy_.reset(); |
| 309 | SetIsEapAuthenticated(false); |
| 310 | } |
| 311 | |
| 312 | void Ethernet::SetIsEapAuthenticated(bool is_eap_authenticated) { |
| 313 | if (is_eap_authenticated == is_eap_authenticated_) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | // If our EAP authentication state changes, we have now joined a different |
| 318 | // network. Restart the DHCP process and any other connection state. |
| 319 | DisconnectFrom(service_); |
| 320 | if (service_ && link_up_) { |
| 321 | ConnectTo(service_); |
| 322 | } |
| 323 | is_eap_authenticated_ = is_eap_authenticated; |
| 324 | adaptor()->EmitBoolChanged(kEapAuthenticationCompletedProperty, |
| 325 | is_eap_authenticated_); |
| 326 | } |
| 327 | |
| 328 | void Ethernet::CertificationTask(const string &subject, uint32 depth) { |
| 329 | if (!service_) { |
| 330 | LOG(ERROR) << "Ethernet " << link_name() << " " << __func__ |
| 331 | << " with no service."; |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | service_->AddEAPCertification(subject, depth); |
| 336 | } |
| 337 | |
| 338 | void Ethernet::EAPEventTask(const string &status, const string ¶meter) { |
| 339 | LOG(INFO) << "In " << __func__ << " with status " << status |
| 340 | << ", parameter " << parameter; |
| 341 | Service::ConnectFailure failure = Service::kFailureUnknown; |
| 342 | if (eap_state_handler_.ParseStatus(status, parameter, &failure)) { |
| 343 | LOG(INFO) << "EAP authentication succeeded!"; |
| 344 | SetIsEapAuthenticated(true); |
| 345 | } else if (failure != Service::Service::kFailureUnknown) { |
| 346 | LOG(INFO) << "EAP authentication failed!"; |
| 347 | SetIsEapAuthenticated(false); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void Ethernet::SupplicantStateChangedTask(const string &state) { |
| 352 | LOG(INFO) << "Supplicant state changed to " << state; |
Paul Stewart | ced3ad7 | 2013-04-03 13:39:25 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Paul Stewart | 35eff13 | 2013-04-12 12:08:40 -0700 | [diff] [blame] | 355 | void Ethernet::TryEapAuthenticationTask() { |
| 356 | if (!service_) { |
| 357 | LOG(INFO) << "Service is missing; not doing EAP authentication."; |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (!GetEapService()->Is8021xConnectable()) { |
| 362 | if (is_eap_authenticated_) { |
| 363 | LOG(INFO) << "EAP Service lost 802.1X credentials; " |
| 364 | << "terminating EAP authentication."; |
| 365 | } else { |
| 366 | LOG(INFO) << "EAP Service lacks 802.1X credentials; " |
| 367 | << "not doing EAP authentication."; |
| 368 | } |
| 369 | StopSupplicant(); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (!is_eap_detected_) { |
| 374 | LOG(WARNING) << "EAP authenticator not detected; " |
| 375 | << "not doing EAP authentication."; |
| 376 | return; |
| 377 | } |
| 378 | if (!StartSupplicant()) { |
| 379 | LOG(ERROR) << "Failed to start supplicant."; |
| 380 | return; |
| 381 | } |
| 382 | StartEapAuthentication(); |
| 383 | } |
| 384 | |
mukesh agrawal | 8bda796 | 2014-04-01 17:09:35 -0700 | [diff] [blame] | 385 | void Ethernet::EnableWakeOnLan() { |
| 386 | int sock; |
| 387 | struct ifreq interface_command; |
| 388 | struct ethtool_wolinfo wake_on_lan_command; |
| 389 | |
| 390 | if (link_name().length() >= sizeof(interface_command.ifr_name)) { |
| 391 | LOG(WARNING) << "Interface name " << link_name() << " too long: " |
| 392 | << link_name().size() << " >= " |
| 393 | << sizeof(interface_command.ifr_name); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | sock = sockets_->Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 398 | if (sock < 0) { |
| 399 | LOG(WARNING) << "Failed to allocate socket: " |
| 400 | << sockets_->ErrorString() << "."; |
| 401 | return; |
| 402 | } |
| 403 | ScopedSocketCloser socket_closer(sockets_.get(), sock); |
| 404 | |
| 405 | memset(&interface_command, 0, sizeof(interface_command)); |
| 406 | memset(&wake_on_lan_command, 0, sizeof(wake_on_lan_command)); |
| 407 | wake_on_lan_command.cmd = ETHTOOL_SWOL; |
| 408 | wake_on_lan_command.wolopts = WAKE_MAGIC; |
| 409 | interface_command.ifr_data = &wake_on_lan_command; |
| 410 | memcpy(interface_command.ifr_name, |
| 411 | link_name().data(), link_name().length()); |
| 412 | |
| 413 | int res = sockets_->Ioctl(sock, SIOCETHTOOL, &interface_command); |
| 414 | if (res < 0) { |
| 415 | LOG(WARNING) << "Failed to enable wake-on-lan: " |
| 416 | << sockets_->ErrorString() << "."; |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 421 | } // namespace shill |