Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -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 | |
| 5 | #include "shill/ipconfig.h" |
| 6 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 7 | #include <sys/time.h> |
| 8 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 9 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 10 | |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 11 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 12 | #include "shill/control_interface.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 13 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 14 | #include "shill/logging.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 15 | #include "shill/net/shill_time.h" |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 16 | #include "shill/static_ip_parameters.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 17 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 18 | using base::Callback; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 19 | using std::string; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 23 | namespace Logging { |
| 24 | static auto kModuleLogScope = ScopeLogger::kInet; |
| 25 | static string ObjectID(IPConfig *i) { return i->GetRpcIdentifier(); } |
| 26 | } |
| 27 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | const time_t kDefaultLeaseExpirationTime = LONG_MAX; |
| 31 | |
| 32 | } // namespace |
| 33 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 34 | // static |
| 35 | const char IPConfig::kType[] = "ip"; |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 36 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 37 | // static |
| 38 | uint IPConfig::global_serial_ = 0; |
| 39 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 40 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 41 | const std::string &device_name) |
| 42 | : device_name_(device_name), |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 43 | type_(kType), |
| 44 | serial_(global_serial_++), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 45 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 46 | Init(); |
| 47 | } |
| 48 | |
| 49 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 50 | const std::string &device_name, |
| 51 | const std::string &type) |
| 52 | : device_name_(device_name), |
| 53 | type_(type), |
| 54 | serial_(global_serial_++), |
| 55 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
| 56 | Init(); |
| 57 | } |
| 58 | |
| 59 | void IPConfig::Init() { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 60 | store_.RegisterConstString(kAddressProperty, &properties_.address); |
| 61 | store_.RegisterConstString(kBroadcastProperty, |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 62 | &properties_.broadcast_address); |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 63 | store_.RegisterConstString(kDomainNameProperty, &properties_.domain_name); |
| 64 | store_.RegisterConstString(kGatewayProperty, &properties_.gateway); |
| 65 | store_.RegisterConstString(kMethodProperty, &properties_.method); |
| 66 | store_.RegisterConstInt32(kMtuProperty, &properties_.mtu); |
| 67 | store_.RegisterConstStrings(kNameServersProperty, &properties_.dns_servers); |
| 68 | store_.RegisterConstString(kPeerAddressProperty, &properties_.peer_address); |
| 69 | store_.RegisterConstInt32(kPrefixlenProperty, &properties_.subnet_prefix); |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 70 | store_.RegisterConstStrings(kSearchDomainsProperty, |
Christopher Wiley | 53b3f47 | 2012-09-17 09:43:39 -0700 | [diff] [blame] | 71 | &properties_.domain_search); |
Paul Stewart | c3fdba9 | 2013-12-02 11:12:38 -0800 | [diff] [blame] | 72 | store_.RegisterConstString(kVendorEncapsulatedOptionsProperty, |
| 73 | &properties_.vendor_encapsulated_options); |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 74 | store_.RegisterConstString(kWebProxyAutoDiscoveryUrlProperty, |
| 75 | &properties_.web_proxy_auto_discovery); |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 76 | time_ = Time::GetInstance(); |
| 77 | current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0}; |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 78 | SLOG(this, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | IPConfig::~IPConfig() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 82 | SLOG(this, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 85 | string IPConfig::GetRpcIdentifier() { |
| 86 | return adaptor_->GetRpcIdentifier(); |
| 87 | } |
| 88 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 89 | bool IPConfig::RequestIP() { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 93 | bool IPConfig::RenewIP() { |
| 94 | return false; |
| 95 | } |
| 96 | |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 97 | bool IPConfig::ReleaseIP(ReleaseReason reason) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 101 | void IPConfig::Refresh(Error */*error*/) { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 102 | if (!refresh_callback_.is_null()) { |
| 103 | refresh_callback_.Run(this); |
| 104 | } |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 105 | RenewIP(); |
| 106 | } |
| 107 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 108 | void IPConfig::ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 109 | StaticIPParameters *static_ip_parameters) { |
| 110 | static_ip_parameters->ApplyTo(&properties_); |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 111 | EmitChanges(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 114 | void IPConfig::RestoreSavedIPParameters( |
| 115 | StaticIPParameters *static_ip_parameters) { |
| 116 | static_ip_parameters->RestoreTo(&properties_); |
| 117 | EmitChanges(); |
| 118 | } |
| 119 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 120 | void IPConfig::UpdateLeaseExpirationTime(uint32_t new_lease_duration) { |
| 121 | struct timeval new_expiration_time; |
| 122 | time_->GetTimeBoottime(&new_expiration_time); |
| 123 | new_expiration_time.tv_sec += new_lease_duration; |
| 124 | current_lease_expiration_time_ = new_expiration_time; |
| 125 | } |
| 126 | |
| 127 | void IPConfig::ResetLeaseExpirationTime() { |
| 128 | current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0}; |
| 129 | } |
| 130 | |
| 131 | bool IPConfig::TimeToLeaseExpiry(uint32_t *time_left) { |
| 132 | if (current_lease_expiration_time_.tv_sec == kDefaultLeaseExpirationTime) { |
| 133 | LOG(ERROR) << __func__ << ": " |
| 134 | << "No current DHCP lease"; |
| 135 | return false; |
| 136 | } |
| 137 | struct timeval now; |
| 138 | time_->GetTimeBoottime(&now); |
| 139 | if (now.tv_sec > current_lease_expiration_time_.tv_sec) { |
| 140 | LOG(ERROR) << __func__ << ": " |
| 141 | << "Current DHCP lease has already expired"; |
| 142 | return false; |
| 143 | } |
| 144 | *time_left = current_lease_expiration_time_.tv_sec - now.tv_sec; |
| 145 | return true; |
| 146 | } |
| 147 | |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame^] | 148 | void IPConfig::UpdateProperties(const Properties &properties, |
| 149 | bool new_lease_acquired) { |
mukesh agrawal | 1c1dd35 | 2013-05-08 15:58:34 -0700 | [diff] [blame] | 150 | // Take a reference of this instance to make sure we don't get destroyed in |
| 151 | // the middle of this call. (The |update_callback_| may cause a reference |
| 152 | // to be dropped. See, e.g., EthernetService::Disconnect and |
| 153 | // Ethernet::DropConnection.) |
| 154 | IPConfigRefPtr me = this; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 155 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 156 | properties_ = properties; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 157 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 158 | if (!update_callback_.is_null()) { |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame^] | 159 | update_callback_.Run(this, new_lease_acquired); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 160 | } |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 161 | EmitChanges(); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 164 | void IPConfig::UpdateDNSServers(const std::vector<std::string> &dns_servers) { |
| 165 | properties_.dns_servers = dns_servers; |
| 166 | EmitChanges(); |
| 167 | } |
| 168 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 169 | void IPConfig::NotifyFailure() { |
| 170 | // Take a reference of this instance to make sure we don't get destroyed in |
| 171 | // the middle of this call. (The |update_callback_| may cause a reference |
| 172 | // to be dropped. See, e.g., EthernetService::Disconnect and |
| 173 | // Ethernet::DropConnection.) |
| 174 | IPConfigRefPtr me = this; |
| 175 | |
| 176 | if (!failure_callback_.is_null()) { |
| 177 | failure_callback_.Run(this); |
| 178 | } |
| 179 | } |
| 180 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 181 | void IPConfig::NotifyExpiry() { |
| 182 | if (!expire_callback_.is_null()) { |
| 183 | expire_callback_.Run(this); |
| 184 | } |
| 185 | } |
| 186 | |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame^] | 187 | void IPConfig::RegisterUpdateCallback(const UpdateCallback &callback) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 188 | update_callback_ = callback; |
| 189 | } |
| 190 | |
| 191 | void IPConfig::RegisterFailureCallback(const Callback &callback) { |
| 192 | failure_callback_ = callback; |
| 193 | } |
| 194 | |
| 195 | void IPConfig::RegisterRefreshCallback(const Callback &callback) { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 196 | refresh_callback_ = callback; |
| 197 | } |
| 198 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 199 | void IPConfig::RegisterExpireCallback(const Callback &callback) { |
| 200 | expire_callback_ = callback; |
| 201 | } |
| 202 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 203 | void IPConfig::ResetProperties() { |
| 204 | properties_ = Properties(); |
| 205 | EmitChanges(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 206 | } |
| 207 | |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 208 | void IPConfig::EmitChanges() { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 209 | adaptor_->EmitStringChanged(kAddressProperty, properties_.address); |
| 210 | adaptor_->EmitStringsChanged(kNameServersProperty, properties_.dns_servers); |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 213 | } // namespace shill |