blob: ee9ec53eddc6d856fce897a8aca164052165a290 [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";
Darin Petkov92c43902011-06-09 20:46:06 -070040const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease";
41const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid";
Darin Petkov14c29ec2012-03-02 11:34:19 +010042const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070043const char DHCPConfig::kReasonBound[] = "BOUND";
44const char DHCPConfig::kReasonFail[] = "FAIL";
45const char DHCPConfig::kReasonRebind[] = "REBIND";
46const char DHCPConfig::kReasonReboot[] = "REBOOT";
47const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070048// static
49const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070050
Darin Petkove7cb7f82011-06-03 13:21:51 -070051
Chris Masone19e30402011-07-19 15:48:47 -070052DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070053 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070054 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070055 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080056 const string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070057 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070058 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070059 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070060 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080061 request_hostname_(request_hostname),
Darin Petkovf7897bc2011-06-08 17:13:36 -070062 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070063 child_watch_tag_(0),
64 root_("/"),
Darin Petkova7b89492011-07-27 12:48:17 -070065 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));
Ben Chanfad4a0b2012-04-18 15:49:59 -070069 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070070}
71
72DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070073 SLOG(DHCP, 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() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070083 SLOG(DHCP, 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() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070095 SLOG(DHCP, 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() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700104 SLOG(DHCP, 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) {
Darin Petkova7b89492011-07-27 12:48:17 -0700116 if (!proxy_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700117 SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700118 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700119 }
120}
121
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700122void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700123 const Configuration &configuration) {
124 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700125 if (reason == kReasonFail) {
126 LOG(ERROR) << "Received failure event from DHCP client.";
127 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700128 return;
129 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700130 if (reason != kReasonBound &&
131 reason != kReasonRebind &&
132 reason != kReasonReboot &&
133 reason != kReasonRenew) {
134 LOG(WARNING) << "Event ignored.";
135 return;
136 }
137 IPConfig::Properties properties;
138 CHECK(ParseConfiguration(configuration, &properties));
139 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700140}
141
Darin Petkovd1b715b2011-06-02 21:21:22 -0700142bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700143 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700144
Paul Stewartd32f4842012-01-11 16:08:13 -0800145 vector<char *> args;
146 args.push_back(const_cast<char *>(kDHCPCDPath));
147 args.push_back(const_cast<char *>("-B")); // foreground
148 args.push_back(const_cast<char *>(device_name().c_str()));
149 if (!request_hostname_.empty()) {
150 args.push_back(const_cast<char *>("-h")); // request hostname
151 args.push_back(const_cast<char *>(request_hostname_.c_str()));
152 }
153 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700154 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700155
Darin Petkov98dd6a02011-06-10 15:12:57 -0700156 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700157 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800158 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700159 envp,
160 G_SPAWN_DO_NOT_REAP_CHILD,
161 NULL,
162 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700163 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700164 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700165 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
166 return false;
167 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700168 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700169 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700170 CHECK(!child_watch_tag_);
171 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700172 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700173}
174
Darin Petkov92c43902011-06-09 20:46:06 -0700175void DHCPConfig::Stop() {
176 if (pid_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700177 SLOG(DHCP, 2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800178 if (kill(pid_, SIGTERM) < 0) {
179 PLOG(ERROR);
180 return;
181 }
182 pid_t ret;
183 int num_iterations =
184 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
185 for (int count = 0; count < num_iterations; ++count) {
186 ret = waitpid(pid_, NULL, WNOHANG);
187 if (ret == pid_ || ret == -1)
188 break;
189 usleep(kDHCPCDExitPollMilliseconds * 1000);
190 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
191 kill(pid_, SIGKILL);
192 }
193 if (ret != pid_)
194 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700195 }
196}
197
Darin Petkov98dd6a02011-06-10 15:12:57 -0700198bool DHCPConfig::Restart() {
199 // Check to ensure that this instance doesn't get destroyed in the middle of
200 // this call. If stopping a running client while there's only one reference to
201 // this instance, we will end up destroying it when the PID is unbound from
202 // the Provider. Since the Provider doesn't invoke Restart, this would mean
203 // that Restart was erroneously executed through a bare reference.
204 CHECK(!pid_ || !HasOneRef());
205 Stop();
206 if (pid_) {
207 provider_->UnbindPID(pid_);
208 }
209 CleanupClientState();
210 return Start();
211}
212
Darin Petkove7cb7f82011-06-03 13:21:51 -0700213string DHCPConfig::GetIPv4AddressString(unsigned int address) {
214 char str[INET_ADDRSTRLEN];
215 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
216 return str;
217 }
218 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
219 return "";
220}
221
222bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
223 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700224 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700225 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700226 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700227 for (Configuration::const_iterator it = configuration.begin();
228 it != configuration.end(); ++it) {
229 const string &key = it->first;
230 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700231 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700232 if (key == kConfigurationKeyIPAddress) {
233 properties->address = GetIPv4AddressString(value.reader().get_uint32());
234 if (properties->address.empty()) {
235 return false;
236 }
237 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700238 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700239 } else if (key == kConfigurationKeyBroadcastAddress) {
240 properties->broadcast_address =
241 GetIPv4AddressString(value.reader().get_uint32());
242 if (properties->broadcast_address.empty()) {
243 return false;
244 }
245 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700246 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700247 if (routers.empty()) {
248 LOG(ERROR) << "No routers provided.";
249 return false;
250 }
251 properties->gateway = GetIPv4AddressString(routers[0]);
252 if (properties->gateway.empty()) {
253 return false;
254 }
255 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700256 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700257 for (vector<unsigned int>::const_iterator it = servers.begin();
258 it != servers.end(); ++it) {
259 string server = GetIPv4AddressString(*it);
260 if (server.empty()) {
261 return false;
262 }
263 properties->dns_servers.push_back(server);
264 }
265 } else if (key == kConfigurationKeyDomainName) {
266 properties->domain_name = value.reader().get_string();
267 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700268 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700269 } else if (key == kConfigurationKeyMTU) {
270 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100271 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700272 properties->mtu = mtu;
273 }
274 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700275 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700276 }
277 }
278 return true;
279}
280
Darin Petkov92c43902011-06-09 20:46:06 -0700281void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700282 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
Darin Petkov92c43902011-06-09 20:46:06 -0700283 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
284 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700285 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700286 config->CleanupClientState();
287
288 // |config| instance may be destroyed after this call.
289 config->provider_->UnbindPID(pid);
290}
291
292void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700293 if (child_watch_tag_) {
294 glib_->SourceRemove(child_watch_tag_);
295 child_watch_tag_ = 0;
296 }
297 if (pid_) {
298 glib_->SpawnClosePID(pid_);
299 pid_ = 0;
300 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700301 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700302 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700303 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700304 false);
305 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700306 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700307 false);
308}
309
Darin Petkov50308cd2011-06-01 18:25:07 -0700310} // namespace shill