blob: ab58d1a3c95f8ad2199919d848ce0befcb8f49cf [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"
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070020#include "shill/minijail.h"
Darin Petkovaceede32011-07-18 15:32:38 -070021#include "shill/proxy_factory.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070022#include "shill/scope_logger.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070023
Darin Petkove7cb7f82011-06-03 13:21:51 -070024using std::string;
25using std::vector;
26
Darin Petkov50308cd2011-06-01 18:25:07 -070027namespace shill {
28
Chris Masone0756f232011-07-21 17:24:00 -070029// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070030const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
31const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
32const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
33const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
34const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
35const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
36const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
37const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Thieu Le94eed562012-02-21 15:57:29 -080038const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50;
39const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000;
Darin Petkovd1b715b2011-06-02 21:21:22 -070040const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Paul Stewartd408fdf2012-05-07 17:15:57 -070041const char DHCPConfig::kDHCPCDPathFormatLease[] =
42 "var/lib/dhcpcd/dhcpcd-%s.lease";
Jorge Lucangeli Obes2f3169d2012-04-25 11:38:25 -070043const char DHCPConfig::kDHCPCDPathFormatPID[] =
44 "var/run/dhcpcd/dhcpcd-%s.pid";
mukesh agrawalcc0fded2012-05-09 13:40:58 -070045const int DHCPConfig::kDHCPTimeoutSeconds = 30;
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070046const char DHCPConfig::kDHCPCDUser[] = "dhcp";
Darin Petkov14c29ec2012-03-02 11:34:19 +010047const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070048const char DHCPConfig::kReasonBound[] = "BOUND";
49const char DHCPConfig::kReasonFail[] = "FAIL";
50const char DHCPConfig::kReasonRebind[] = "REBIND";
51const char DHCPConfig::kReasonReboot[] = "REBOOT";
52const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070053// static
54const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070055
Darin Petkove7cb7f82011-06-03 13:21:51 -070056
Chris Masone19e30402011-07-19 15:48:47 -070057DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070058 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070059 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070060 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080061 const string &request_hostname,
Paul Stewartd408fdf2012-05-07 17:15:57 -070062 const string &lease_file_suffix,
63 bool arp_gateway,
Darin Petkov3258a812011-06-23 11:28:45 -070064 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070065 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070066 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070067 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080068 request_hostname_(request_hostname),
Paul Stewartd408fdf2012-05-07 17:15:57 -070069 lease_file_suffix_(lease_file_suffix),
70 arp_gateway_(arp_gateway),
Darin Petkovf7897bc2011-06-08 17:13:36 -070071 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070072 child_watch_tag_(0),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070073 lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds),
Darin Petkov92c43902011-06-09 20:46:06 -070074 root_("/"),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070075 weak_ptr_factory_(this),
Darin Petkova7b89492011-07-27 12:48:17 -070076 dispatcher_(dispatcher),
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070077 glib_(glib),
78 minijail_(Minijail::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070079 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070080 if (lease_file_suffix_.empty()) {
81 lease_file_suffix_ = device_name;
82 }
Darin Petkov50308cd2011-06-01 18:25:07 -070083}
84
85DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070086 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070087
88 // Don't leave behind dhcpcd running.
89 Stop();
90
Darin Petkov98dd6a02011-06-10 15:12:57 -070091 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070092 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070093}
94
Darin Petkov92c43902011-06-09 20:46:06 -070095bool DHCPConfig::RequestIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070096 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070097 if (!pid_) {
98 return Start();
99 }
100 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700101 LOG(ERROR) << "Unable to request IP before acquiring destination.";
102 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700103 }
Darin Petkov92c43902011-06-09 20:46:06 -0700104 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700105}
106
Darin Petkov92c43902011-06-09 20:46:06 -0700107bool DHCPConfig::RenewIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700108 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700109 if (!pid_) {
110 return false;
111 }
Darin Petkovaceede32011-07-18 15:32:38 -0700112 proxy_->Rebind(device_name());
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700113 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700114 return true;
115}
116
Darin Petkov92c43902011-06-09 20:46:06 -0700117bool DHCPConfig::ReleaseIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700118 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700119 if (!pid_) {
120 return true;
121 }
Darin Petkova7b89492011-07-27 12:48:17 -0700122 if (proxy_.get()) {
123 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700124 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700125 Stop();
126 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700127}
128
Darin Petkova7b89492011-07-27 12:48:17 -0700129void DHCPConfig::InitProxy(const string &service) {
Darin Petkova7b89492011-07-27 12:48:17 -0700130 if (!proxy_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700131 SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700132 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700133 }
134}
135
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700136void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700137 const Configuration &configuration) {
138 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700139 if (reason == kReasonFail) {
140 LOG(ERROR) << "Received failure event from DHCP client.";
141 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700142 return;
143 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700144 if (reason != kReasonBound &&
145 reason != kReasonRebind &&
146 reason != kReasonReboot &&
147 reason != kReasonRenew) {
148 LOG(WARNING) << "Event ignored.";
149 return;
150 }
151 IPConfig::Properties properties;
152 CHECK(ParseConfiguration(configuration, &properties));
153 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700154}
155
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700156void DHCPConfig::UpdateProperties(const Properties &properties, bool success) {
157 StopDHCPTimeout();
158 IPConfig::UpdateProperties(properties, success);
159}
160
Darin Petkovd1b715b2011-06-02 21:21:22 -0700161bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700162 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700163
Paul Stewartd32f4842012-01-11 16:08:13 -0800164 vector<char *> args;
165 args.push_back(const_cast<char *>(kDHCPCDPath));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700166 args.push_back(const_cast<char *>("-B")); // Run in foreground.
Paul Stewartd32f4842012-01-11 16:08:13 -0800167 if (!request_hostname_.empty()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700168 args.push_back(const_cast<char *>("-h")); // Request hostname from server.
Paul Stewartd32f4842012-01-11 16:08:13 -0800169 args.push_back(const_cast<char *>(request_hostname_.c_str()));
170 }
Paul Stewartd408fdf2012-05-07 17:15:57 -0700171 if (arp_gateway_) {
172 args.push_back(const_cast<char *>("-R")); // ARP for default gateway.
173 }
174 string interface_arg(device_name());
175 if (lease_file_suffix_ != device_name()) {
176 interface_arg = base::StringPrintf("%s=%s", device_name().c_str(),
177 lease_file_suffix_.c_str());
178 }
179 args.push_back(const_cast<char *>(interface_arg.c_str()));
Paul Stewartd32f4842012-01-11 16:08:13 -0800180 args.push_back(NULL);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700181
182 struct minijail *jail = minijail_->New();
183 minijail_->DropRoot(jail, kDHCPCDUser);
184 minijail_->UseCapabilities(jail,
185 CAP_TO_MASK(CAP_NET_BIND_SERVICE) |
186 CAP_TO_MASK(CAP_NET_BROADCAST) |
187 CAP_TO_MASK(CAP_NET_ADMIN) |
188 CAP_TO_MASK(CAP_NET_RAW));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700189
Darin Petkov98dd6a02011-06-10 15:12:57 -0700190 CHECK(!pid_);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700191 if (!minijail_->RunAndDestroy(jail, args, &pid_)) {
192 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail.";
Darin Petkovd1b715b2011-06-02 21:21:22 -0700193 return false;
194 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700195 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700196 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700197 CHECK(!child_watch_tag_);
198 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700199 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700200 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700201}
202
Darin Petkov92c43902011-06-09 20:46:06 -0700203void DHCPConfig::Stop() {
204 if (pid_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700205 SLOG(DHCP, 2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800206 if (kill(pid_, SIGTERM) < 0) {
207 PLOG(ERROR);
208 return;
209 }
210 pid_t ret;
211 int num_iterations =
212 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
213 for (int count = 0; count < num_iterations; ++count) {
214 ret = waitpid(pid_, NULL, WNOHANG);
215 if (ret == pid_ || ret == -1)
216 break;
217 usleep(kDHCPCDExitPollMilliseconds * 1000);
218 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
219 kill(pid_, SIGKILL);
220 }
221 if (ret != pid_)
222 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700223 }
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700224 StopDHCPTimeout();
Darin Petkov92c43902011-06-09 20:46:06 -0700225}
226
Darin Petkov98dd6a02011-06-10 15:12:57 -0700227bool DHCPConfig::Restart() {
228 // Check to ensure that this instance doesn't get destroyed in the middle of
229 // this call. If stopping a running client while there's only one reference to
230 // this instance, we will end up destroying it when the PID is unbound from
231 // the Provider. Since the Provider doesn't invoke Restart, this would mean
232 // that Restart was erroneously executed through a bare reference.
233 CHECK(!pid_ || !HasOneRef());
234 Stop();
235 if (pid_) {
236 provider_->UnbindPID(pid_);
237 }
238 CleanupClientState();
239 return Start();
240}
241
Darin Petkove7cb7f82011-06-03 13:21:51 -0700242string DHCPConfig::GetIPv4AddressString(unsigned int address) {
243 char str[INET_ADDRSTRLEN];
244 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
245 return str;
246 }
247 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
248 return "";
249}
250
251bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
252 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700253 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700254 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700255 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700256 for (Configuration::const_iterator it = configuration.begin();
257 it != configuration.end(); ++it) {
258 const string &key = it->first;
259 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700260 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700261 if (key == kConfigurationKeyIPAddress) {
262 properties->address = GetIPv4AddressString(value.reader().get_uint32());
263 if (properties->address.empty()) {
264 return false;
265 }
266 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700267 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700268 } else if (key == kConfigurationKeyBroadcastAddress) {
269 properties->broadcast_address =
270 GetIPv4AddressString(value.reader().get_uint32());
271 if (properties->broadcast_address.empty()) {
272 return false;
273 }
274 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700275 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700276 if (routers.empty()) {
277 LOG(ERROR) << "No routers provided.";
278 return false;
279 }
280 properties->gateway = GetIPv4AddressString(routers[0]);
281 if (properties->gateway.empty()) {
282 return false;
283 }
284 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700285 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700286 for (vector<unsigned int>::const_iterator it = servers.begin();
287 it != servers.end(); ++it) {
288 string server = GetIPv4AddressString(*it);
289 if (server.empty()) {
290 return false;
291 }
292 properties->dns_servers.push_back(server);
293 }
294 } else if (key == kConfigurationKeyDomainName) {
295 properties->domain_name = value.reader().get_string();
296 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700297 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700298 } else if (key == kConfigurationKeyMTU) {
299 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100300 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700301 properties->mtu = mtu;
302 }
303 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700304 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700305 }
306 }
307 return true;
308}
309
Darin Petkov92c43902011-06-09 20:46:06 -0700310void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700311 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
Darin Petkov92c43902011-06-09 20:46:06 -0700312 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
313 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700314 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700315 config->CleanupClientState();
316
317 // |config| instance may be destroyed after this call.
318 config->provider_->UnbindPID(pid);
319}
320
321void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700322 if (child_watch_tag_) {
323 glib_->SourceRemove(child_watch_tag_);
324 child_watch_tag_ = 0;
325 }
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700326 pid_ = 0;
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