blob: ad7a4a8e147c7b045edcbb2113ea006878793a2c [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.
mukesh agrawal7eb02892012-05-29 11:22:37 -0700167 args.push_back(const_cast<char *>("-q")); // Only warnings+errors to stderr.
Paul Stewartd32f4842012-01-11 16:08:13 -0800168 if (!request_hostname_.empty()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700169 args.push_back(const_cast<char *>("-h")); // Request hostname from server.
Paul Stewartd32f4842012-01-11 16:08:13 -0800170 args.push_back(const_cast<char *>(request_hostname_.c_str()));
171 }
Paul Stewartd408fdf2012-05-07 17:15:57 -0700172 if (arp_gateway_) {
173 args.push_back(const_cast<char *>("-R")); // ARP for default gateway.
174 }
175 string interface_arg(device_name());
176 if (lease_file_suffix_ != device_name()) {
177 interface_arg = base::StringPrintf("%s=%s", device_name().c_str(),
178 lease_file_suffix_.c_str());
179 }
180 args.push_back(const_cast<char *>(interface_arg.c_str()));
Paul Stewartd32f4842012-01-11 16:08:13 -0800181 args.push_back(NULL);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700182
183 struct minijail *jail = minijail_->New();
184 minijail_->DropRoot(jail, kDHCPCDUser);
185 minijail_->UseCapabilities(jail,
186 CAP_TO_MASK(CAP_NET_BIND_SERVICE) |
187 CAP_TO_MASK(CAP_NET_BROADCAST) |
188 CAP_TO_MASK(CAP_NET_ADMIN) |
189 CAP_TO_MASK(CAP_NET_RAW));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700190
Darin Petkov98dd6a02011-06-10 15:12:57 -0700191 CHECK(!pid_);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700192 if (!minijail_->RunAndDestroy(jail, args, &pid_)) {
193 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail.";
Darin Petkovd1b715b2011-06-02 21:21:22 -0700194 return false;
195 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700196 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700197 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700198 CHECK(!child_watch_tag_);
199 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700200 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700201 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700202}
203
Darin Petkov92c43902011-06-09 20:46:06 -0700204void DHCPConfig::Stop() {
205 if (pid_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700206 SLOG(DHCP, 2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800207 if (kill(pid_, SIGTERM) < 0) {
208 PLOG(ERROR);
209 return;
210 }
211 pid_t ret;
212 int num_iterations =
213 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
214 for (int count = 0; count < num_iterations; ++count) {
215 ret = waitpid(pid_, NULL, WNOHANG);
216 if (ret == pid_ || ret == -1)
217 break;
218 usleep(kDHCPCDExitPollMilliseconds * 1000);
219 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
220 kill(pid_, SIGKILL);
221 }
222 if (ret != pid_)
223 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700224 }
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700225 StopDHCPTimeout();
Darin Petkov92c43902011-06-09 20:46:06 -0700226}
227
Darin Petkov98dd6a02011-06-10 15:12:57 -0700228bool DHCPConfig::Restart() {
229 // Check to ensure that this instance doesn't get destroyed in the middle of
230 // this call. If stopping a running client while there's only one reference to
231 // this instance, we will end up destroying it when the PID is unbound from
232 // the Provider. Since the Provider doesn't invoke Restart, this would mean
233 // that Restart was erroneously executed through a bare reference.
234 CHECK(!pid_ || !HasOneRef());
235 Stop();
236 if (pid_) {
237 provider_->UnbindPID(pid_);
238 }
239 CleanupClientState();
240 return Start();
241}
242
Darin Petkove7cb7f82011-06-03 13:21:51 -0700243string DHCPConfig::GetIPv4AddressString(unsigned int address) {
244 char str[INET_ADDRSTRLEN];
245 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
246 return str;
247 }
248 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
249 return "";
250}
251
252bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
253 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700254 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700255 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700256 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700257 for (Configuration::const_iterator it = configuration.begin();
258 it != configuration.end(); ++it) {
259 const string &key = it->first;
260 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700261 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700262 if (key == kConfigurationKeyIPAddress) {
263 properties->address = GetIPv4AddressString(value.reader().get_uint32());
264 if (properties->address.empty()) {
265 return false;
266 }
267 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700268 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700269 } else if (key == kConfigurationKeyBroadcastAddress) {
270 properties->broadcast_address =
271 GetIPv4AddressString(value.reader().get_uint32());
272 if (properties->broadcast_address.empty()) {
273 return false;
274 }
275 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700276 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700277 if (routers.empty()) {
278 LOG(ERROR) << "No routers provided.";
279 return false;
280 }
281 properties->gateway = GetIPv4AddressString(routers[0]);
282 if (properties->gateway.empty()) {
283 return false;
284 }
285 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700286 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700287 for (vector<unsigned int>::const_iterator it = servers.begin();
288 it != servers.end(); ++it) {
289 string server = GetIPv4AddressString(*it);
290 if (server.empty()) {
291 return false;
292 }
293 properties->dns_servers.push_back(server);
294 }
295 } else if (key == kConfigurationKeyDomainName) {
296 properties->domain_name = value.reader().get_string();
297 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700298 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700299 } else if (key == kConfigurationKeyMTU) {
300 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100301 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700302 properties->mtu = mtu;
303 }
304 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700305 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700306 }
307 }
308 return true;
309}
310
Darin Petkov92c43902011-06-09 20:46:06 -0700311void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700312 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
Darin Petkov92c43902011-06-09 20:46:06 -0700313 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
314 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700315 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700316 config->CleanupClientState();
317
318 // |config| instance may be destroyed after this call.
319 config->provider_->UnbindPID(pid);
320}
321
322void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700323 if (child_watch_tag_) {
324 glib_->SourceRemove(child_watch_tag_);
325 child_watch_tag_ = 0;
326 }
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700327 pid_ = 0;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700328 proxy_.reset();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700329 if (lease_file_suffix_ == device_name()) {
330 // If the lease file suffix was left as default, clean it up at exit.
331 file_util::Delete(root_.Append(
332 base::StringPrintf(kDHCPCDPathFormatLease,
333 device_name().c_str())), false);
334 }
335 file_util::Delete(root_.Append(
336 base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false);
Darin Petkov92c43902011-06-09 20:46:06 -0700337}
338
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700339void DHCPConfig::StartDHCPTimeout() {
340 lease_acquisition_timeout_callback_.Reset(
341 Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr()));
342 dispatcher_->PostDelayedTask(
343 lease_acquisition_timeout_callback_.callback(),
344 lease_acquisition_timeout_seconds_ * 1000);
345}
346
347void DHCPConfig::StopDHCPTimeout() {
348 lease_acquisition_timeout_callback_.Cancel();
349}
350
351void DHCPConfig::ProcessDHCPTimeout() {
352 LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " "
353 << "(after " << lease_acquisition_timeout_seconds_ << " seconds).";
354 UpdateProperties(IPConfig::Properties(), false);
355}
356
Darin Petkov50308cd2011-06-01 18:25:07 -0700357} // namespace shill