blob: e1f610d037ef594d8a04c55cb79f684925b4c673 [file] [log] [blame]
Thieu Le94eed562012-02-21 15:57:29 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov50308cd2011-06-01 18:25:07 -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/dhcp_config.h"
6
Darin Petkove7cb7f82011-06-03 13:21:51 -07007#include <arpa/inet.h>
Thieu Le94eed562012-02-21 15:57:29 -08008#include <sys/wait.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -07009
Darin Petkov92c43902011-06-09 20:46:06 -070010#include <base/file_util.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070011#include <base/logging.h>
Darin Petkov92c43902011-06-09 20:46:06 -070012#include <base/stringprintf.h>
Chris Masone43b48a12011-07-01 13:37:07 -070013#include <chromeos/dbus/service_constants.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
15#include "shill/dhcpcd_proxy.h"
16#include "shill/dhcp_provider.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070017#include "shill/event_dispatcher.h"
Darin Petkov3258a812011-06-23 11:28:45 -070018#include "shill/glib.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070019#include "shill/ip_address.h"
Darin Petkovaceede32011-07-18 15:32:38 -070020#include "shill/proxy_factory.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070021#include "shill/scope_logger.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070022
Darin Petkove7cb7f82011-06-03 13:21:51 -070023using std::string;
24using std::vector;
25
Darin Petkov50308cd2011-06-01 18:25:07 -070026namespace shill {
27
Chris Masone0756f232011-07-21 17:24:00 -070028// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070029const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
30const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
31const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
32const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
33const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
34const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
35const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
36const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Thieu Le94eed562012-02-21 15:57:29 -080037const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50;
38const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000;
Darin Petkovd1b715b2011-06-02 21:21:22 -070039const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Paul Stewartd408fdf2012-05-07 17:15:57 -070040const char DHCPConfig::kDHCPCDPathFormatLease[] =
41 "var/lib/dhcpcd/dhcpcd-%s.lease";
Jorge Lucangeli Obes2f3169d2012-04-25 11:38:25 -070042const char DHCPConfig::kDHCPCDPathFormatPID[] =
43 "var/run/dhcpcd/dhcpcd-%s.pid";
mukesh agrawalcc0fded2012-05-09 13:40:58 -070044const int DHCPConfig::kDHCPTimeoutSeconds = 30;
Darin Petkov14c29ec2012-03-02 11:34:19 +010045const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070046const char DHCPConfig::kReasonBound[] = "BOUND";
47const char DHCPConfig::kReasonFail[] = "FAIL";
48const char DHCPConfig::kReasonRebind[] = "REBIND";
49const char DHCPConfig::kReasonReboot[] = "REBOOT";
50const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070051// static
52const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070053
Darin Petkove7cb7f82011-06-03 13:21:51 -070054
Chris Masone19e30402011-07-19 15:48:47 -070055DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070056 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070057 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070058 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080059 const string &request_hostname,
Paul Stewartd408fdf2012-05-07 17:15:57 -070060 const string &lease_file_suffix,
61 bool arp_gateway,
Darin Petkov3258a812011-06-23 11:28:45 -070062 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070063 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070064 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070065 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080066 request_hostname_(request_hostname),
Paul Stewartd408fdf2012-05-07 17:15:57 -070067 lease_file_suffix_(lease_file_suffix),
68 arp_gateway_(arp_gateway),
Darin Petkovf7897bc2011-06-08 17:13:36 -070069 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070070 child_watch_tag_(0),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070071 lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds),
Darin Petkov92c43902011-06-09 20:46:06 -070072 root_("/"),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070073 weak_ptr_factory_(this),
Darin Petkova7b89492011-07-27 12:48:17 -070074 dispatcher_(dispatcher),
Darin Petkovf7897bc2011-06-08 17:13:36 -070075 glib_(glib) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070076 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070077 if (lease_file_suffix_.empty()) {
78 lease_file_suffix_ = device_name;
79 }
Darin Petkov50308cd2011-06-01 18:25:07 -070080}
81
82DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070083 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070084
85 // Don't leave behind dhcpcd running.
86 Stop();
87
Darin Petkov98dd6a02011-06-10 15:12:57 -070088 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070089 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070090}
91
Darin Petkov92c43902011-06-09 20:46:06 -070092bool DHCPConfig::RequestIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070093 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070094 if (!pid_) {
95 return Start();
96 }
97 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070098 LOG(ERROR) << "Unable to request IP before acquiring destination.";
99 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700100 }
Darin Petkov92c43902011-06-09 20:46:06 -0700101 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700102}
103
Darin Petkov92c43902011-06-09 20:46:06 -0700104bool DHCPConfig::RenewIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700105 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700106 if (!pid_) {
107 return false;
108 }
Darin Petkovaceede32011-07-18 15:32:38 -0700109 proxy_->Rebind(device_name());
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700110 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700111 return true;
112}
113
Darin Petkov92c43902011-06-09 20:46:06 -0700114bool DHCPConfig::ReleaseIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700115 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700116 if (!pid_) {
117 return true;
118 }
Darin Petkova7b89492011-07-27 12:48:17 -0700119 if (proxy_.get()) {
120 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700121 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700122 Stop();
123 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700124}
125
Darin Petkova7b89492011-07-27 12:48:17 -0700126void DHCPConfig::InitProxy(const string &service) {
Darin Petkova7b89492011-07-27 12:48:17 -0700127 if (!proxy_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700128 SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700129 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700130 }
131}
132
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700133void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700134 const Configuration &configuration) {
135 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700136 if (reason == kReasonFail) {
137 LOG(ERROR) << "Received failure event from DHCP client.";
138 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700139 return;
140 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700141 if (reason != kReasonBound &&
142 reason != kReasonRebind &&
143 reason != kReasonReboot &&
144 reason != kReasonRenew) {
145 LOG(WARNING) << "Event ignored.";
146 return;
147 }
148 IPConfig::Properties properties;
149 CHECK(ParseConfiguration(configuration, &properties));
150 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700151}
152
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700153void DHCPConfig::UpdateProperties(const Properties &properties, bool success) {
154 StopDHCPTimeout();
155 IPConfig::UpdateProperties(properties, success);
156}
157
Darin Petkovd1b715b2011-06-02 21:21:22 -0700158bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700159 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700160
Paul Stewartd32f4842012-01-11 16:08:13 -0800161 vector<char *> args;
162 args.push_back(const_cast<char *>(kDHCPCDPath));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700163 args.push_back(const_cast<char *>("-B")); // Run in foreground.
Paul Stewartd32f4842012-01-11 16:08:13 -0800164 if (!request_hostname_.empty()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700165 args.push_back(const_cast<char *>("-h")); // Request hostname from server.
Paul Stewartd32f4842012-01-11 16:08:13 -0800166 args.push_back(const_cast<char *>(request_hostname_.c_str()));
167 }
Paul Stewartd408fdf2012-05-07 17:15:57 -0700168 if (arp_gateway_) {
169 args.push_back(const_cast<char *>("-R")); // ARP for default gateway.
170 }
171 string interface_arg(device_name());
172 if (lease_file_suffix_ != device_name()) {
173 interface_arg = base::StringPrintf("%s=%s", device_name().c_str(),
174 lease_file_suffix_.c_str());
175 }
176 args.push_back(const_cast<char *>(interface_arg.c_str()));
Paul Stewartd32f4842012-01-11 16:08:13 -0800177 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700178 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700179
Darin Petkov98dd6a02011-06-10 15:12:57 -0700180 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700181 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800182 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700183 envp,
184 G_SPAWN_DO_NOT_REAP_CHILD,
185 NULL,
186 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700187 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700188 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700189 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
190 return false;
191 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700192 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700193 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700194 CHECK(!child_watch_tag_);
195 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700196 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700197 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700198}
199
Darin Petkov92c43902011-06-09 20:46:06 -0700200void DHCPConfig::Stop() {
201 if (pid_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700202 SLOG(DHCP, 2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800203 if (kill(pid_, SIGTERM) < 0) {
204 PLOG(ERROR);
205 return;
206 }
207 pid_t ret;
208 int num_iterations =
209 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
210 for (int count = 0; count < num_iterations; ++count) {
211 ret = waitpid(pid_, NULL, WNOHANG);
212 if (ret == pid_ || ret == -1)
213 break;
214 usleep(kDHCPCDExitPollMilliseconds * 1000);
215 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
216 kill(pid_, SIGKILL);
217 }
218 if (ret != pid_)
219 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700220 }
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700221 StopDHCPTimeout();
Darin Petkov92c43902011-06-09 20:46:06 -0700222}
223
Darin Petkov98dd6a02011-06-10 15:12:57 -0700224bool DHCPConfig::Restart() {
225 // Check to ensure that this instance doesn't get destroyed in the middle of
226 // this call. If stopping a running client while there's only one reference to
227 // this instance, we will end up destroying it when the PID is unbound from
228 // the Provider. Since the Provider doesn't invoke Restart, this would mean
229 // that Restart was erroneously executed through a bare reference.
230 CHECK(!pid_ || !HasOneRef());
231 Stop();
232 if (pid_) {
233 provider_->UnbindPID(pid_);
234 }
235 CleanupClientState();
236 return Start();
237}
238
Darin Petkove7cb7f82011-06-03 13:21:51 -0700239string DHCPConfig::GetIPv4AddressString(unsigned int address) {
240 char str[INET_ADDRSTRLEN];
241 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
242 return str;
243 }
244 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
245 return "";
246}
247
248bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
249 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700250 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700251 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700252 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700253 for (Configuration::const_iterator it = configuration.begin();
254 it != configuration.end(); ++it) {
255 const string &key = it->first;
256 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700257 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700258 if (key == kConfigurationKeyIPAddress) {
259 properties->address = GetIPv4AddressString(value.reader().get_uint32());
260 if (properties->address.empty()) {
261 return false;
262 }
263 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700264 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700265 } else if (key == kConfigurationKeyBroadcastAddress) {
266 properties->broadcast_address =
267 GetIPv4AddressString(value.reader().get_uint32());
268 if (properties->broadcast_address.empty()) {
269 return false;
270 }
271 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700272 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700273 if (routers.empty()) {
274 LOG(ERROR) << "No routers provided.";
275 return false;
276 }
277 properties->gateway = GetIPv4AddressString(routers[0]);
278 if (properties->gateway.empty()) {
279 return false;
280 }
281 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700282 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700283 for (vector<unsigned int>::const_iterator it = servers.begin();
284 it != servers.end(); ++it) {
285 string server = GetIPv4AddressString(*it);
286 if (server.empty()) {
287 return false;
288 }
289 properties->dns_servers.push_back(server);
290 }
291 } else if (key == kConfigurationKeyDomainName) {
292 properties->domain_name = value.reader().get_string();
293 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700294 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700295 } else if (key == kConfigurationKeyMTU) {
296 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100297 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700298 properties->mtu = mtu;
299 }
300 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700301 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700302 }
303 }
304 return true;
305}
306
Darin Petkov92c43902011-06-09 20:46:06 -0700307void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700308 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
Darin Petkov92c43902011-06-09 20:46:06 -0700309 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
310 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700311 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700312 config->CleanupClientState();
313
314 // |config| instance may be destroyed after this call.
315 config->provider_->UnbindPID(pid);
316}
317
318void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700319 if (child_watch_tag_) {
320 glib_->SourceRemove(child_watch_tag_);
321 child_watch_tag_ = 0;
322 }
323 if (pid_) {
324 glib_->SpawnClosePID(pid_);
325 pid_ = 0;
326 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700327 proxy_.reset();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700328 if (lease_file_suffix_ == device_name()) {
329 // If the lease file suffix was left as default, clean it up at exit.
330 file_util::Delete(root_.Append(
331 base::StringPrintf(kDHCPCDPathFormatLease,
332 device_name().c_str())), false);
333 }
334 file_util::Delete(root_.Append(
335 base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false);
Darin Petkov92c43902011-06-09 20:46:06 -0700336}
337
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700338void DHCPConfig::StartDHCPTimeout() {
339 lease_acquisition_timeout_callback_.Reset(
340 Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr()));
341 dispatcher_->PostDelayedTask(
342 lease_acquisition_timeout_callback_.callback(),
343 lease_acquisition_timeout_seconds_ * 1000);
344}
345
346void DHCPConfig::StopDHCPTimeout() {
347 lease_acquisition_timeout_callback_.Cancel();
348}
349
350void DHCPConfig::ProcessDHCPTimeout() {
351 LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " "
352 << "(after " << lease_acquisition_timeout_seconds_ << " seconds).";
353 UpdateProperties(IPConfig::Properties(), false);
354}
355
Darin Petkov50308cd2011-06-01 18:25:07 -0700356} // namespace shill