blob: 7435432f95802c117279eb5235f6774e059c0df2 [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 Petkovf9b0ca82011-06-20 12:10:23 -070041const char DHCPConfig::kReasonBound[] = "BOUND";
42const char DHCPConfig::kReasonFail[] = "FAIL";
43const char DHCPConfig::kReasonRebind[] = "REBIND";
44const char DHCPConfig::kReasonReboot[] = "REBOOT";
45const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070046// static
47const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070048
Darin Petkove7cb7f82011-06-03 13:21:51 -070049
Chris Masone19e30402011-07-19 15:48:47 -070050DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070051 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070052 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070053 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080054 const string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070055 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070056 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070057 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070058 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080059 request_hostname_(request_hostname),
Darin Petkovf7897bc2011-06-08 17:13:36 -070060 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070061 child_watch_tag_(0),
62 root_("/"),
Darin Petkova7b89492011-07-27 12:48:17 -070063 task_factory_(this),
64 dispatcher_(dispatcher),
Darin Petkovf7897bc2011-06-08 17:13:36 -070065 glib_(glib) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070066 mutable_store()->RegisterConstString(flimflam::kAddressProperty,
67 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070068 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070069}
70
71DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070072 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070073
74 // Don't leave behind dhcpcd running.
75 Stop();
76
Darin Petkov98dd6a02011-06-10 15:12:57 -070077 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070078 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070079}
80
Darin Petkov92c43902011-06-09 20:46:06 -070081bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070082 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070083 if (!pid_) {
84 return Start();
85 }
86 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070087 LOG(ERROR) << "Unable to request IP before acquiring destination.";
88 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070089 }
Darin Petkov92c43902011-06-09 20:46:06 -070090 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070091}
92
Darin Petkov92c43902011-06-09 20:46:06 -070093bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070094 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070095 if (!pid_) {
96 return false;
97 }
Darin Petkovaceede32011-07-18 15:32:38 -070098 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070099 return true;
100}
101
Darin Petkov92c43902011-06-09 20:46:06 -0700102bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700103 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700104 if (!pid_) {
105 return true;
106 }
Darin Petkova7b89492011-07-27 12:48:17 -0700107 if (proxy_.get()) {
108 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700109 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700110 Stop();
111 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700112}
113
Darin Petkova7b89492011-07-27 12:48:17 -0700114void DHCPConfig::InitProxy(const string &service) {
115 // Defer proxy creation because dbus-c++ doesn't allow registration of new
116 // D-Bus objects in the context of a D-Bus signal handler.
Darin Petkovd1b715b2011-06-02 21:21:22 -0700117 if (!proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700118 dispatcher_->PostTask(
119 task_factory_.NewRunnableMethod(&DHCPConfig::InitProxyTask, service));
120 }
121}
122
123void DHCPConfig::InitProxyTask(const string &service) {
124 if (!proxy_.get()) {
125 VLOG(2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700126 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700127 }
128}
129
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700130void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700131 const Configuration &configuration) {
132 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700133 if (reason == kReasonFail) {
134 LOG(ERROR) << "Received failure event from DHCP client.";
135 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700136 return;
137 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700138 if (reason != kReasonBound &&
139 reason != kReasonRebind &&
140 reason != kReasonReboot &&
141 reason != kReasonRenew) {
142 LOG(WARNING) << "Event ignored.";
143 return;
144 }
145 IPConfig::Properties properties;
146 CHECK(ParseConfiguration(configuration, &properties));
147 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700148}
149
Darin Petkovd1b715b2011-06-02 21:21:22 -0700150bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700151 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700152
Paul Stewartd32f4842012-01-11 16:08:13 -0800153 vector<char *> args;
154 args.push_back(const_cast<char *>(kDHCPCDPath));
155 args.push_back(const_cast<char *>("-B")); // foreground
156 args.push_back(const_cast<char *>(device_name().c_str()));
157 if (!request_hostname_.empty()) {
158 args.push_back(const_cast<char *>("-h")); // request hostname
159 args.push_back(const_cast<char *>(request_hostname_.c_str()));
160 }
161 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700162 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700163
Darin Petkov98dd6a02011-06-10 15:12:57 -0700164 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700165 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800166 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700167 envp,
168 G_SPAWN_DO_NOT_REAP_CHILD,
169 NULL,
170 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700171 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700172 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700173 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
174 return false;
175 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700176 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700177 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700178 CHECK(!child_watch_tag_);
179 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700180 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700181}
182
Darin Petkov92c43902011-06-09 20:46:06 -0700183void DHCPConfig::Stop() {
184 if (pid_) {
185 VLOG(2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800186 if (kill(pid_, SIGTERM) < 0) {
187 PLOG(ERROR);
188 return;
189 }
190 pid_t ret;
191 int num_iterations =
192 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
193 for (int count = 0; count < num_iterations; ++count) {
194 ret = waitpid(pid_, NULL, WNOHANG);
195 if (ret == pid_ || ret == -1)
196 break;
197 usleep(kDHCPCDExitPollMilliseconds * 1000);
198 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
199 kill(pid_, SIGKILL);
200 }
201 if (ret != pid_)
202 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700203 }
204}
205
Darin Petkov98dd6a02011-06-10 15:12:57 -0700206bool DHCPConfig::Restart() {
207 // Check to ensure that this instance doesn't get destroyed in the middle of
208 // this call. If stopping a running client while there's only one reference to
209 // this instance, we will end up destroying it when the PID is unbound from
210 // the Provider. Since the Provider doesn't invoke Restart, this would mean
211 // that Restart was erroneously executed through a bare reference.
212 CHECK(!pid_ || !HasOneRef());
213 Stop();
214 if (pid_) {
215 provider_->UnbindPID(pid_);
216 }
217 CleanupClientState();
218 return Start();
219}
220
Darin Petkove7cb7f82011-06-03 13:21:51 -0700221string DHCPConfig::GetIPv4AddressString(unsigned int address) {
222 char str[INET_ADDRSTRLEN];
223 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
224 return str;
225 }
226 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
227 return "";
228}
229
230bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
231 IPConfig::Properties *properties) {
232 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700233 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700234 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700235 for (Configuration::const_iterator it = configuration.begin();
236 it != configuration.end(); ++it) {
237 const string &key = it->first;
238 const DBus::Variant &value = it->second;
239 VLOG(2) << "Processing key: " << key;
240 if (key == kConfigurationKeyIPAddress) {
241 properties->address = GetIPv4AddressString(value.reader().get_uint32());
242 if (properties->address.empty()) {
243 return false;
244 }
245 } else if (key == kConfigurationKeySubnetCIDR) {
246 properties->subnet_cidr = value.reader().get_byte();
247 } else if (key == kConfigurationKeyBroadcastAddress) {
248 properties->broadcast_address =
249 GetIPv4AddressString(value.reader().get_uint32());
250 if (properties->broadcast_address.empty()) {
251 return false;
252 }
253 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700254 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700255 if (routers.empty()) {
256 LOG(ERROR) << "No routers provided.";
257 return false;
258 }
259 properties->gateway = GetIPv4AddressString(routers[0]);
260 if (properties->gateway.empty()) {
261 return false;
262 }
263 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700264 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700265 for (vector<unsigned int>::const_iterator it = servers.begin();
266 it != servers.end(); ++it) {
267 string server = GetIPv4AddressString(*it);
268 if (server.empty()) {
269 return false;
270 }
271 properties->dns_servers.push_back(server);
272 }
273 } else if (key == kConfigurationKeyDomainName) {
274 properties->domain_name = value.reader().get_string();
275 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700276 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700277 } else if (key == kConfigurationKeyMTU) {
278 int mtu = value.reader().get_uint16();
279 if (mtu >= 576) {
280 properties->mtu = mtu;
281 }
282 } else {
283 VLOG(2) << "Key ignored.";
284 }
285 }
286 return true;
287}
288
Darin Petkov92c43902011-06-09 20:46:06 -0700289void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
290 VLOG(2) << "pid " << pid << " exit status " << status;
291 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
292 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700293 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700294 config->CleanupClientState();
295
296 // |config| instance may be destroyed after this call.
297 config->provider_->UnbindPID(pid);
298}
299
300void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700301 if (child_watch_tag_) {
302 glib_->SourceRemove(child_watch_tag_);
303 child_watch_tag_ = 0;
304 }
305 if (pid_) {
306 glib_->SpawnClosePID(pid_);
307 pid_ = 0;
308 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700309 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700310 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700311 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700312 false);
313 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700314 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700315 false);
316}
317
Darin Petkov50308cd2011-06-01 18:25:07 -0700318} // namespace shill