blob: c0bcc67287e7c1c5067d5f1d4cc88823e8774ba8 [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"
Darin Petkov50308cd2011-06-01 18:25:07 -070021
Darin Petkove7cb7f82011-06-03 13:21:51 -070022using std::string;
23using std::vector;
24
Darin Petkov50308cd2011-06-01 18:25:07 -070025namespace shill {
26
Chris Masone0756f232011-07-21 17:24:00 -070027// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070028const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
29const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
30const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
31const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
32const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
33const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
34const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
35const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Thieu Le94eed562012-02-21 15:57:29 -080036const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50;
37const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000;
Darin Petkovd1b715b2011-06-02 21:21:22 -070038const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Darin Petkov92c43902011-06-09 20:46:06 -070039const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease";
40const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid";
Darin Petkov14c29ec2012-03-02 11:34:19 +010041const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070042const char DHCPConfig::kReasonBound[] = "BOUND";
43const char DHCPConfig::kReasonFail[] = "FAIL";
44const char DHCPConfig::kReasonRebind[] = "REBIND";
45const char DHCPConfig::kReasonReboot[] = "REBOOT";
46const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070047// static
48const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070049
Darin Petkove7cb7f82011-06-03 13:21:51 -070050
Chris Masone19e30402011-07-19 15:48:47 -070051DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070052 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070053 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070054 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080055 const string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070056 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070057 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070058 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070059 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080060 request_hostname_(request_hostname),
Darin Petkovf7897bc2011-06-08 17:13:36 -070061 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070062 child_watch_tag_(0),
63 root_("/"),
Darin Petkova7b89492011-07-27 12:48:17 -070064 task_factory_(this),
65 dispatcher_(dispatcher),
Darin Petkovf7897bc2011-06-08 17:13:36 -070066 glib_(glib) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070067 mutable_store()->RegisterConstString(flimflam::kAddressProperty,
68 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070069 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070070}
71
72DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070073 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070074
75 // Don't leave behind dhcpcd running.
76 Stop();
77
Darin Petkov98dd6a02011-06-10 15:12:57 -070078 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070079 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070080}
81
Darin Petkov92c43902011-06-09 20:46:06 -070082bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070083 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070084 if (!pid_) {
85 return Start();
86 }
87 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070088 LOG(ERROR) << "Unable to request IP before acquiring destination.";
89 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070090 }
Darin Petkov92c43902011-06-09 20:46:06 -070091 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070092}
93
Darin Petkov92c43902011-06-09 20:46:06 -070094bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070095 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070096 if (!pid_) {
97 return false;
98 }
Darin Petkovaceede32011-07-18 15:32:38 -070099 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -0700100 return true;
101}
102
Darin Petkov92c43902011-06-09 20:46:06 -0700103bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700104 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700105 if (!pid_) {
106 return true;
107 }
Darin Petkova7b89492011-07-27 12:48:17 -0700108 if (proxy_.get()) {
109 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700110 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700111 Stop();
112 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700113}
114
Darin Petkova7b89492011-07-27 12:48:17 -0700115void DHCPConfig::InitProxy(const string &service) {
116 // Defer proxy creation because dbus-c++ doesn't allow registration of new
117 // D-Bus objects in the context of a D-Bus signal handler.
Darin Petkovd1b715b2011-06-02 21:21:22 -0700118 if (!proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700119 dispatcher_->PostTask(
120 task_factory_.NewRunnableMethod(&DHCPConfig::InitProxyTask, service));
121 }
122}
123
124void DHCPConfig::InitProxyTask(const string &service) {
125 if (!proxy_.get()) {
126 VLOG(2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700127 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700128 }
129}
130
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700131void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700132 const Configuration &configuration) {
133 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700134 if (reason == kReasonFail) {
135 LOG(ERROR) << "Received failure event from DHCP client.";
136 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700137 return;
138 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700139 if (reason != kReasonBound &&
140 reason != kReasonRebind &&
141 reason != kReasonReboot &&
142 reason != kReasonRenew) {
143 LOG(WARNING) << "Event ignored.";
144 return;
145 }
146 IPConfig::Properties properties;
147 CHECK(ParseConfiguration(configuration, &properties));
148 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700149}
150
Darin Petkovd1b715b2011-06-02 21:21:22 -0700151bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700152 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700153
Paul Stewartd32f4842012-01-11 16:08:13 -0800154 vector<char *> args;
155 args.push_back(const_cast<char *>(kDHCPCDPath));
156 args.push_back(const_cast<char *>("-B")); // foreground
157 args.push_back(const_cast<char *>(device_name().c_str()));
158 if (!request_hostname_.empty()) {
159 args.push_back(const_cast<char *>("-h")); // request hostname
160 args.push_back(const_cast<char *>(request_hostname_.c_str()));
161 }
162 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700163 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700164
Darin Petkov98dd6a02011-06-10 15:12:57 -0700165 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700166 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800167 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700168 envp,
169 G_SPAWN_DO_NOT_REAP_CHILD,
170 NULL,
171 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700172 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700173 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700174 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
175 return false;
176 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700177 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700178 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700179 CHECK(!child_watch_tag_);
180 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700181 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700182}
183
Darin Petkov92c43902011-06-09 20:46:06 -0700184void DHCPConfig::Stop() {
185 if (pid_) {
186 VLOG(2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800187 if (kill(pid_, SIGTERM) < 0) {
188 PLOG(ERROR);
189 return;
190 }
191 pid_t ret;
192 int num_iterations =
193 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
194 for (int count = 0; count < num_iterations; ++count) {
195 ret = waitpid(pid_, NULL, WNOHANG);
196 if (ret == pid_ || ret == -1)
197 break;
198 usleep(kDHCPCDExitPollMilliseconds * 1000);
199 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
200 kill(pid_, SIGKILL);
201 }
202 if (ret != pid_)
203 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700204 }
205}
206
Darin Petkov98dd6a02011-06-10 15:12:57 -0700207bool DHCPConfig::Restart() {
208 // Check to ensure that this instance doesn't get destroyed in the middle of
209 // this call. If stopping a running client while there's only one reference to
210 // this instance, we will end up destroying it when the PID is unbound from
211 // the Provider. Since the Provider doesn't invoke Restart, this would mean
212 // that Restart was erroneously executed through a bare reference.
213 CHECK(!pid_ || !HasOneRef());
214 Stop();
215 if (pid_) {
216 provider_->UnbindPID(pid_);
217 }
218 CleanupClientState();
219 return Start();
220}
221
Darin Petkove7cb7f82011-06-03 13:21:51 -0700222string DHCPConfig::GetIPv4AddressString(unsigned int address) {
223 char str[INET_ADDRSTRLEN];
224 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
225 return str;
226 }
227 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
228 return "";
229}
230
231bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
232 IPConfig::Properties *properties) {
233 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700234 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700235 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700236 for (Configuration::const_iterator it = configuration.begin();
237 it != configuration.end(); ++it) {
238 const string &key = it->first;
239 const DBus::Variant &value = it->second;
240 VLOG(2) << "Processing key: " << key;
241 if (key == kConfigurationKeyIPAddress) {
242 properties->address = GetIPv4AddressString(value.reader().get_uint32());
243 if (properties->address.empty()) {
244 return false;
245 }
246 } else if (key == kConfigurationKeySubnetCIDR) {
247 properties->subnet_cidr = value.reader().get_byte();
248 } else if (key == kConfigurationKeyBroadcastAddress) {
249 properties->broadcast_address =
250 GetIPv4AddressString(value.reader().get_uint32());
251 if (properties->broadcast_address.empty()) {
252 return false;
253 }
254 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700255 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700256 if (routers.empty()) {
257 LOG(ERROR) << "No routers provided.";
258 return false;
259 }
260 properties->gateway = GetIPv4AddressString(routers[0]);
261 if (properties->gateway.empty()) {
262 return false;
263 }
264 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700265 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700266 for (vector<unsigned int>::const_iterator it = servers.begin();
267 it != servers.end(); ++it) {
268 string server = GetIPv4AddressString(*it);
269 if (server.empty()) {
270 return false;
271 }
272 properties->dns_servers.push_back(server);
273 }
274 } else if (key == kConfigurationKeyDomainName) {
275 properties->domain_name = value.reader().get_string();
276 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700277 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700278 } else if (key == kConfigurationKeyMTU) {
279 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100280 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700281 properties->mtu = mtu;
282 }
283 } else {
284 VLOG(2) << "Key ignored.";
285 }
286 }
287 return true;
288}
289
Darin Petkov92c43902011-06-09 20:46:06 -0700290void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
291 VLOG(2) << "pid " << pid << " exit status " << status;
292 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
293 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700294 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700295 config->CleanupClientState();
296
297 // |config| instance may be destroyed after this call.
298 config->provider_->UnbindPID(pid);
299}
300
301void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700302 if (child_watch_tag_) {
303 glib_->SourceRemove(child_watch_tag_);
304 child_watch_tag_ = 0;
305 }
306 if (pid_) {
307 glib_->SpawnClosePID(pid_);
308 pid_ = 0;
309 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700310 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700311 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700312 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700313 false);
314 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700315 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700316 false);
317}
318
Darin Petkov50308cd2011-06-01 18:25:07 -0700319} // namespace shill