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