blob: 7fa0b3377a010a74498c6961046935d3b1479b44 [file] [log] [blame]
Ben Chanfad4a0b2012-04-18 15:49:59 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove02b3ca2011-05-31 16:00:44 -07002// 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 Tan815a6fb2014-10-23 16:53:59 -07007#include <sys/time.h>
8
Chris Masone43b48a12011-07-01 13:37:07 -07009#include <chromeos/dbus/service_constants.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070010
Chris Masonec6c6c132011-06-30 11:29:52 -070011#include "shill/adaptor_interfaces.h"
Chris Masone19e30402011-07-19 15:48:47 -070012#include "shill/control_interface.h"
Chris Masone43b48a12011-07-01 13:37:07 -070013#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070015#include "shill/net/shill_time.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070016#include "shill/static_ip_parameters.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070017
Eric Shienbrood3e20a232012-02-16 11:35:56 -050018using base::Callback;
Darin Petkove02b3ca2011-05-31 16:00:44 -070019using std::string;
20
21namespace shill {
22
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070023namespace Logging {
24static auto kModuleLogScope = ScopeLogger::kInet;
Paul Stewart8ae18742015-06-16 13:13:10 -070025static string ObjectID(IPConfig* i) { return i->GetRpcIdentifier(); }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070026}
27
Samuel Tan815a6fb2014-10-23 16:53:59 -070028namespace {
29
30const time_t kDefaultLeaseExpirationTime = LONG_MAX;
31
32} // namespace
33
Chris Masone0756f232011-07-21 17:24:00 -070034// static
Paul Stewart024a6c82015-01-23 14:59:40 -080035const int IPConfig::kDefaultMTU = 1500;
36const int IPConfig::kMinIPv4MTU = 576;
37const int IPConfig::kMinIPv6MTU = 1280;
38const int IPConfig::kUndefinedMTU = 0;
Chris Masone0756f232011-07-21 17:24:00 -070039const char IPConfig::kType[] = "ip";
Samuel Tan815a6fb2014-10-23 16:53:59 -070040
Chris Masone0756f232011-07-21 17:24:00 -070041// static
42uint IPConfig::global_serial_ = 0;
43
Paul Stewart8ae18742015-06-16 13:13:10 -070044IPConfig::IPConfig(ControlInterface* control_interface,
45 const std::string& device_name)
Chris Masone19e30402011-07-19 15:48:47 -070046 : device_name_(device_name),
Chris Masone0756f232011-07-21 17:24:00 -070047 type_(kType),
48 serial_(global_serial_++),
Chris Masone19e30402011-07-19 15:48:47 -070049 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
Chris Masone0756f232011-07-21 17:24:00 -070050 Init();
51}
52
Paul Stewart8ae18742015-06-16 13:13:10 -070053IPConfig::IPConfig(ControlInterface* control_interface,
54 const std::string& device_name,
55 const std::string& type)
Chris Masone0756f232011-07-21 17:24:00 -070056 : device_name_(device_name),
57 type_(type),
58 serial_(global_serial_++),
59 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
60 Init();
61}
62
63void IPConfig::Init() {
Ben Chan0295e0f2013-09-20 13:47:29 -070064 store_.RegisterConstString(kAddressProperty, &properties_.address);
65 store_.RegisterConstString(kBroadcastProperty,
Paul Stewart10241e32012-04-23 18:15:06 -070066 &properties_.broadcast_address);
Ben Chan0295e0f2013-09-20 13:47:29 -070067 store_.RegisterConstString(kDomainNameProperty, &properties_.domain_name);
Matthew Wein08add482015-04-20 13:26:48 -070068 store_.RegisterConstString(kAcceptedHostnameProperty,
69 &properties_.accepted_hostname);
Ben Chan0295e0f2013-09-20 13:47:29 -070070 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 Stewarta63f5212013-06-25 15:29:40 -070076 store_.RegisterConstStrings(kSearchDomainsProperty,
Christopher Wiley53b3f472012-09-17 09:43:39 -070077 &properties_.domain_search);
Paul Stewartc3fdba92013-12-02 11:12:38 -080078 store_.RegisterConstString(kVendorEncapsulatedOptionsProperty,
79 &properties_.vendor_encapsulated_options);
Paul Stewarta63f5212013-06-25 15:29:40 -070080 store_.RegisterConstString(kWebProxyAutoDiscoveryUrlProperty,
81 &properties_.web_proxy_auto_discovery);
Peter Qiub255a762015-06-10 10:09:12 -070082 store_.RegisterConstString(kDelegatedPrefixProperty,
83 &properties_.delegated_prefix);
84 store_.RegisterConstInt32(kDelegatedPrefixLengthProperty,
85 &properties_.delegated_prefix_length);
Samuel Tan815a6fb2014-10-23 16:53:59 -070086 time_ = Time::GetInstance();
87 current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0};
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070088 SLOG(this, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070089}
90
91IPConfig::~IPConfig() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070092 SLOG(this, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070093}
94
Chris Masone4e851612011-07-01 10:46:53 -070095string IPConfig::GetRpcIdentifier() {
96 return adaptor_->GetRpcIdentifier();
97}
98
Darin Petkov92c43902011-06-09 20:46:06 -070099bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700100 return false;
101}
102
Darin Petkov92c43902011-06-09 20:46:06 -0700103bool IPConfig::RenewIP() {
104 return false;
105}
106
Paul Stewart217c61d2013-06-13 15:12:02 -0700107bool IPConfig::ReleaseIP(ReleaseReason reason) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700108 return false;
109}
110
Paul Stewart8ae18742015-06-16 13:13:10 -0700111void IPConfig::Refresh(Error* /*error*/) {
Paul Stewart82236532013-12-10 15:33:11 -0800112 if (!refresh_callback_.is_null()) {
113 refresh_callback_.Run(this);
114 }
Paul Stewart4558bda2012-08-03 10:44:10 -0700115 RenewIP();
116}
117
Paul Stewart1062d9d2012-04-27 10:42:27 -0700118void IPConfig::ApplyStaticIPParameters(
Paul Stewart8ae18742015-06-16 13:13:10 -0700119 StaticIPParameters* static_ip_parameters) {
Paul Stewartdef189e2012-08-02 20:12:09 -0700120 static_ip_parameters->ApplyTo(&properties_);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700121 EmitChanges();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700122}
123
Paul Stewart82236532013-12-10 15:33:11 -0800124void IPConfig::RestoreSavedIPParameters(
Paul Stewart8ae18742015-06-16 13:13:10 -0700125 StaticIPParameters* static_ip_parameters) {
Paul Stewart82236532013-12-10 15:33:11 -0800126 static_ip_parameters->RestoreTo(&properties_);
127 EmitChanges();
128}
129
Samuel Tan815a6fb2014-10-23 16:53:59 -0700130void 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
137void IPConfig::ResetLeaseExpirationTime() {
138 current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0};
139}
140
Paul Stewart8ae18742015-06-16 13:13:10 -0700141bool IPConfig::TimeToLeaseExpiry(uint32_t* time_left) {
Samuel Tan815a6fb2014-10-23 16:53:59 -0700142 if (current_lease_expiration_time_.tv_sec == kDefaultLeaseExpirationTime) {
Paul Stewart35ed9082015-06-10 10:46:17 -0700143 SLOG(this, 2) << __func__ << ": No current DHCP lease";
Samuel Tan815a6fb2014-10-23 16:53:59 -0700144 return false;
145 }
146 struct timeval now;
147 time_->GetTimeBoottime(&now);
148 if (now.tv_sec > current_lease_expiration_time_.tv_sec) {
Paul Stewart35ed9082015-06-10 10:46:17 -0700149 SLOG(this, 2) << __func__ << ": Current DHCP lease has already expired";
Samuel Tan815a6fb2014-10-23 16:53:59 -0700150 return false;
151 }
152 *time_left = current_lease_expiration_time_.tv_sec - now.tv_sec;
153 return true;
154}
155
Paul Stewart8ae18742015-06-16 13:13:10 -0700156void IPConfig::UpdateProperties(const Properties& properties,
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800157 bool new_lease_acquired) {
mukesh agrawal1c1dd352013-05-08 15:58:34 -0700158 // 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 Stewartc5099532013-12-12 07:53:15 -0800163
Darin Petkove7cb7f82011-06-03 13:21:51 -0700164 properties_ = properties;
Paul Stewartc5099532013-12-12 07:53:15 -0800165
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500166 if (!update_callback_.is_null()) {
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800167 update_callback_.Run(this, new_lease_acquired);
Darin Petkovefb09c32011-06-07 20:24:17 -0700168 }
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700169 EmitChanges();
Darin Petkovefb09c32011-06-07 20:24:17 -0700170}
171
Paul Stewart8ae18742015-06-16 13:13:10 -0700172void IPConfig::UpdateDNSServers(const std::vector<std::string>& dns_servers) {
Peter Qiua89154b2014-05-23 15:45:42 -0700173 properties_.dns_servers = dns_servers;
174 EmitChanges();
175}
176
Paul Stewartc5099532013-12-12 07:53:15 -0800177void 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 Stewart1f916e42013-12-23 09:52:54 -0800189void IPConfig::NotifyExpiry() {
190 if (!expire_callback_.is_null()) {
191 expire_callback_.Run(this);
192 }
193}
194
Paul Stewart8ae18742015-06-16 13:13:10 -0700195void IPConfig::RegisterUpdateCallback(const UpdateCallback& callback) {
Paul Stewartc5099532013-12-12 07:53:15 -0800196 update_callback_ = callback;
197}
198
Paul Stewart8ae18742015-06-16 13:13:10 -0700199void IPConfig::RegisterFailureCallback(const Callback& callback) {
Paul Stewartc5099532013-12-12 07:53:15 -0800200 failure_callback_ = callback;
201}
202
Paul Stewart8ae18742015-06-16 13:13:10 -0700203void IPConfig::RegisterRefreshCallback(const Callback& callback) {
Paul Stewart82236532013-12-10 15:33:11 -0800204 refresh_callback_ = callback;
205}
206
Paul Stewart8ae18742015-06-16 13:13:10 -0700207void IPConfig::RegisterExpireCallback(const Callback& callback) {
Paul Stewart1f916e42013-12-23 09:52:54 -0800208 expire_callback_ = callback;
209}
210
Paul Stewartc5099532013-12-12 07:53:15 -0800211void IPConfig::ResetProperties() {
212 properties_ = Properties();
213 EmitChanges();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700214}
215
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700216void IPConfig::EmitChanges() {
Ben Chan0295e0f2013-09-20 13:47:29 -0700217 adaptor_->EmitStringChanged(kAddressProperty, properties_.address);
218 adaptor_->EmitStringsChanged(kNameServersProperty, properties_.dns_servers);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700219}
220
Darin Petkove02b3ca2011-05-31 16:00:44 -0700221} // namespace shill