blob: 8e9e7ff38300f6f5140b085a351910e89b6bb914 [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// 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
5#include "shill/dhcp_config.h"
6
Darin Petkove7cb7f82011-06-03 13:21:51 -07007#include <arpa/inet.h>
8
Darin Petkov92c43902011-06-09 20:46:06 -07009#include <base/file_util.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070010#include <base/logging.h>
Darin Petkov92c43902011-06-09 20:46:06 -070011#include <base/stringprintf.h>
Chris Masone43b48a12011-07-01 13:37:07 -070012#include <chromeos/dbus/service_constants.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070013
14#include "shill/dhcpcd_proxy.h"
15#include "shill/dhcp_provider.h"
Darin Petkov3258a812011-06-23 11:28:45 -070016#include "shill/glib.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070017#include "shill/ip_address.h"
Darin Petkovaceede32011-07-18 15:32:38 -070018#include "shill/proxy_factory.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070019
Darin Petkove7cb7f82011-06-03 13:21:51 -070020using std::string;
21using std::vector;
22
Darin Petkov50308cd2011-06-01 18:25:07 -070023namespace shill {
24
Chris Masone0756f232011-07-21 17:24:00 -070025// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070026const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
27const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
28const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
29const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
30const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
31const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
32const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
33const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Darin Petkovd1b715b2011-06-02 21:21:22 -070034const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Darin Petkov92c43902011-06-09 20:46:06 -070035const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease";
36const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070037const char DHCPConfig::kReasonBound[] = "BOUND";
38const char DHCPConfig::kReasonFail[] = "FAIL";
39const char DHCPConfig::kReasonRebind[] = "REBIND";
40const char DHCPConfig::kReasonReboot[] = "REBOOT";
41const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070042// static
43const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070044
Darin Petkove7cb7f82011-06-03 13:21:51 -070045
Chris Masone19e30402011-07-19 15:48:47 -070046DHCPConfig::DHCPConfig(ControlInterface *control_interface,
47 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070048 const string &device_name,
Darin Petkov3258a812011-06-23 11:28:45 -070049 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070050 : IPConfig(control_interface, device_name, kType),
Darin Petkovd1b715b2011-06-02 21:21:22 -070051 provider_(provider),
Darin Petkovf7897bc2011-06-08 17:13:36 -070052 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070053 child_watch_tag_(0),
54 root_("/"),
Darin Petkovf7897bc2011-06-08 17:13:36 -070055 glib_(glib) {
Chris Masone27c4aa52011-07-02 13:10:14 -070056 store_.RegisterConstString(flimflam::kAddressProperty,
57 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070058 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070059}
60
61DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070062 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070063
64 // Don't leave behind dhcpcd running.
65 Stop();
66
Darin Petkov98dd6a02011-06-10 15:12:57 -070067 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070068 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070069}
70
Darin Petkov92c43902011-06-09 20:46:06 -070071bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070072 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070073 if (!pid_) {
74 return Start();
75 }
76 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070077 LOG(ERROR) << "Unable to request IP before acquiring destination.";
78 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070079 }
Darin Petkov92c43902011-06-09 20:46:06 -070080 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070081}
82
Darin Petkov92c43902011-06-09 20:46:06 -070083bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070084 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070085 if (!pid_) {
86 return false;
87 }
88 if (!proxy_.get()) {
89 LOG(ERROR) << "Unable to renew IP before acquiring destination.";
Darin Petkovd1b715b2011-06-02 21:21:22 -070090 return false;
91 }
Darin Petkovaceede32011-07-18 15:32:38 -070092 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070093 return true;
94}
95
Darin Petkov92c43902011-06-09 20:46:06 -070096bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070097 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070098 if (!pid_) {
99 return true;
100 }
101 if (!proxy_.get()) {
102 LOG(ERROR) << "Unable to release IP before acquiring destination.";
103 return false;
104 }
Darin Petkovaceede32011-07-18 15:32:38 -0700105 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700106 Stop();
107 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700108}
109
Darin Petkovaceede32011-07-18 15:32:38 -0700110void DHCPConfig::InitProxy(const char *service) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700111 if (!proxy_.get()) {
Darin Petkovaceede32011-07-18 15:32:38 -0700112 proxy_.reset(ProxyFactory::factory()->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700113 }
114}
115
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700116void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700117 const Configuration &configuration) {
118 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700119 if (reason == kReasonFail) {
120 LOG(ERROR) << "Received failure event from DHCP client.";
121 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700122 return;
123 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700124 if (reason != kReasonBound &&
125 reason != kReasonRebind &&
126 reason != kReasonReboot &&
127 reason != kReasonRenew) {
128 LOG(WARNING) << "Event ignored.";
129 return;
130 }
131 IPConfig::Properties properties;
132 CHECK(ParseConfiguration(configuration, &properties));
133 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700134}
135
Darin Petkovd1b715b2011-06-02 21:21:22 -0700136bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700137 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700138
Darin Petkov98dd6a02011-06-10 15:12:57 -0700139 char *argv[4] = {
140 const_cast<char *>(kDHCPCDPath),
141 const_cast<char *>("-B"), // foreground
Darin Petkovf65e9282011-06-21 14:29:56 -0700142 const_cast<char *>(device_name().c_str()),
Darin Petkov98dd6a02011-06-10 15:12:57 -0700143 NULL
144 };
145 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700146
Darin Petkov98dd6a02011-06-10 15:12:57 -0700147 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700148 if (!glib_->SpawnAsync(NULL,
149 argv,
150 envp,
151 G_SPAWN_DO_NOT_REAP_CHILD,
152 NULL,
153 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700154 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700155 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700156 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
157 return false;
158 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700159 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700160 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700161 CHECK(!child_watch_tag_);
162 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700163 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700164}
165
Darin Petkov92c43902011-06-09 20:46:06 -0700166void DHCPConfig::Stop() {
167 if (pid_) {
168 VLOG(2) << "Terminating " << pid_;
169 PLOG_IF(ERROR, kill(pid_, SIGTERM) < 0);
170 }
171}
172
Darin Petkov98dd6a02011-06-10 15:12:57 -0700173bool DHCPConfig::Restart() {
174 // Check to ensure that this instance doesn't get destroyed in the middle of
175 // this call. If stopping a running client while there's only one reference to
176 // this instance, we will end up destroying it when the PID is unbound from
177 // the Provider. Since the Provider doesn't invoke Restart, this would mean
178 // that Restart was erroneously executed through a bare reference.
179 CHECK(!pid_ || !HasOneRef());
180 Stop();
181 if (pid_) {
182 provider_->UnbindPID(pid_);
183 }
184 CleanupClientState();
185 return Start();
186}
187
Darin Petkove7cb7f82011-06-03 13:21:51 -0700188string DHCPConfig::GetIPv4AddressString(unsigned int address) {
189 char str[INET_ADDRSTRLEN];
190 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
191 return str;
192 }
193 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
194 return "";
195}
196
197bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
198 IPConfig::Properties *properties) {
199 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700200 properties->method = flimflam::kTypeDHCP;
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700201 properties->address_family = IPAddress::kAddressFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700202 for (Configuration::const_iterator it = configuration.begin();
203 it != configuration.end(); ++it) {
204 const string &key = it->first;
205 const DBus::Variant &value = it->second;
206 VLOG(2) << "Processing key: " << key;
207 if (key == kConfigurationKeyIPAddress) {
208 properties->address = GetIPv4AddressString(value.reader().get_uint32());
209 if (properties->address.empty()) {
210 return false;
211 }
212 } else if (key == kConfigurationKeySubnetCIDR) {
213 properties->subnet_cidr = value.reader().get_byte();
214 } else if (key == kConfigurationKeyBroadcastAddress) {
215 properties->broadcast_address =
216 GetIPv4AddressString(value.reader().get_uint32());
217 if (properties->broadcast_address.empty()) {
218 return false;
219 }
220 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700221 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700222 if (routers.empty()) {
223 LOG(ERROR) << "No routers provided.";
224 return false;
225 }
226 properties->gateway = GetIPv4AddressString(routers[0]);
227 if (properties->gateway.empty()) {
228 return false;
229 }
230 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700231 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700232 for (vector<unsigned int>::const_iterator it = servers.begin();
233 it != servers.end(); ++it) {
234 string server = GetIPv4AddressString(*it);
235 if (server.empty()) {
236 return false;
237 }
238 properties->dns_servers.push_back(server);
239 }
240 } else if (key == kConfigurationKeyDomainName) {
241 properties->domain_name = value.reader().get_string();
242 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700243 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700244 } else if (key == kConfigurationKeyMTU) {
245 int mtu = value.reader().get_uint16();
246 if (mtu >= 576) {
247 properties->mtu = mtu;
248 }
249 } else {
250 VLOG(2) << "Key ignored.";
251 }
252 }
253 return true;
254}
255
Darin Petkov92c43902011-06-09 20:46:06 -0700256void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
257 VLOG(2) << "pid " << pid << " exit status " << status;
258 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
259 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700260 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700261 config->CleanupClientState();
262
263 // |config| instance may be destroyed after this call.
264 config->provider_->UnbindPID(pid);
265}
266
267void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700268 if (child_watch_tag_) {
269 glib_->SourceRemove(child_watch_tag_);
270 child_watch_tag_ = 0;
271 }
272 if (pid_) {
273 glib_->SpawnClosePID(pid_);
274 pid_ = 0;
275 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700276 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700277 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700278 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700279 false);
280 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700281 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700282 false);
283}
284
Darin Petkov50308cd2011-06-01 18:25:07 -0700285} // namespace shill