blob: 8708f12525fbe1057213f8d6d696b8116f8be0e6 [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) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070068}
69
70DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070071 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070072
73 // Don't leave behind dhcpcd running.
74 Stop();
75
Darin Petkov98dd6a02011-06-10 15:12:57 -070076 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070077 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070078}
79
Darin Petkov92c43902011-06-09 20:46:06 -070080bool DHCPConfig::RequestIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070081 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070082 if (!pid_) {
83 return Start();
84 }
85 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070086 LOG(ERROR) << "Unable to request IP before acquiring destination.";
87 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070088 }
Darin Petkov92c43902011-06-09 20:46:06 -070089 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070090}
91
Darin Petkov92c43902011-06-09 20:46:06 -070092bool DHCPConfig::RenewIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070093 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070094 if (!pid_) {
95 return false;
96 }
Darin Petkovaceede32011-07-18 15:32:38 -070097 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070098 return true;
99}
100
Darin Petkov92c43902011-06-09 20:46:06 -0700101bool DHCPConfig::ReleaseIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700102 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700103 if (!pid_) {
104 return true;
105 }
Darin Petkova7b89492011-07-27 12:48:17 -0700106 if (proxy_.get()) {
107 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700108 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700109 Stop();
110 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700111}
112
Darin Petkova7b89492011-07-27 12:48:17 -0700113void DHCPConfig::InitProxy(const string &service) {
Darin Petkova7b89492011-07-27 12:48:17 -0700114 if (!proxy_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700115 SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700116 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700117 }
118}
119
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700120void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700121 const Configuration &configuration) {
122 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700123 if (reason == kReasonFail) {
124 LOG(ERROR) << "Received failure event from DHCP client.";
125 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700126 return;
127 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700128 if (reason != kReasonBound &&
129 reason != kReasonRebind &&
130 reason != kReasonReboot &&
131 reason != kReasonRenew) {
132 LOG(WARNING) << "Event ignored.";
133 return;
134 }
135 IPConfig::Properties properties;
136 CHECK(ParseConfiguration(configuration, &properties));
137 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700138}
139
Darin Petkovd1b715b2011-06-02 21:21:22 -0700140bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700141 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700142
Paul Stewartd32f4842012-01-11 16:08:13 -0800143 vector<char *> args;
144 args.push_back(const_cast<char *>(kDHCPCDPath));
145 args.push_back(const_cast<char *>("-B")); // foreground
146 args.push_back(const_cast<char *>(device_name().c_str()));
147 if (!request_hostname_.empty()) {
148 args.push_back(const_cast<char *>("-h")); // request hostname
149 args.push_back(const_cast<char *>(request_hostname_.c_str()));
150 }
151 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700152 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700153
Darin Petkov98dd6a02011-06-10 15:12:57 -0700154 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700155 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800156 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700157 envp,
158 G_SPAWN_DO_NOT_REAP_CHILD,
159 NULL,
160 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700161 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700162 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700163 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
164 return false;
165 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700166 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700167 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700168 CHECK(!child_watch_tag_);
169 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700170 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700171}
172
Darin Petkov92c43902011-06-09 20:46:06 -0700173void DHCPConfig::Stop() {
174 if (pid_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700175 SLOG(DHCP, 2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800176 if (kill(pid_, SIGTERM) < 0) {
177 PLOG(ERROR);
178 return;
179 }
180 pid_t ret;
181 int num_iterations =
182 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
183 for (int count = 0; count < num_iterations; ++count) {
184 ret = waitpid(pid_, NULL, WNOHANG);
185 if (ret == pid_ || ret == -1)
186 break;
187 usleep(kDHCPCDExitPollMilliseconds * 1000);
188 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
189 kill(pid_, SIGKILL);
190 }
191 if (ret != pid_)
192 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700193 }
194}
195
Darin Petkov98dd6a02011-06-10 15:12:57 -0700196bool DHCPConfig::Restart() {
197 // Check to ensure that this instance doesn't get destroyed in the middle of
198 // this call. If stopping a running client while there's only one reference to
199 // this instance, we will end up destroying it when the PID is unbound from
200 // the Provider. Since the Provider doesn't invoke Restart, this would mean
201 // that Restart was erroneously executed through a bare reference.
202 CHECK(!pid_ || !HasOneRef());
203 Stop();
204 if (pid_) {
205 provider_->UnbindPID(pid_);
206 }
207 CleanupClientState();
208 return Start();
209}
210
Darin Petkove7cb7f82011-06-03 13:21:51 -0700211string DHCPConfig::GetIPv4AddressString(unsigned int address) {
212 char str[INET_ADDRSTRLEN];
213 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
214 return str;
215 }
216 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
217 return "";
218}
219
220bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
221 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700222 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700223 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700224 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700225 for (Configuration::const_iterator it = configuration.begin();
226 it != configuration.end(); ++it) {
227 const string &key = it->first;
228 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700229 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700230 if (key == kConfigurationKeyIPAddress) {
231 properties->address = GetIPv4AddressString(value.reader().get_uint32());
232 if (properties->address.empty()) {
233 return false;
234 }
235 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700236 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700237 } else if (key == kConfigurationKeyBroadcastAddress) {
238 properties->broadcast_address =
239 GetIPv4AddressString(value.reader().get_uint32());
240 if (properties->broadcast_address.empty()) {
241 return false;
242 }
243 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700244 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700245 if (routers.empty()) {
246 LOG(ERROR) << "No routers provided.";
247 return false;
248 }
249 properties->gateway = GetIPv4AddressString(routers[0]);
250 if (properties->gateway.empty()) {
251 return false;
252 }
253 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700254 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700255 for (vector<unsigned int>::const_iterator it = servers.begin();
256 it != servers.end(); ++it) {
257 string server = GetIPv4AddressString(*it);
258 if (server.empty()) {
259 return false;
260 }
261 properties->dns_servers.push_back(server);
262 }
263 } else if (key == kConfigurationKeyDomainName) {
264 properties->domain_name = value.reader().get_string();
265 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700266 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700267 } else if (key == kConfigurationKeyMTU) {
268 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100269 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700270 properties->mtu = mtu;
271 }
272 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700273 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700274 }
275 }
276 return true;
277}
278
Darin Petkov92c43902011-06-09 20:46:06 -0700279void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700280 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
Darin Petkov92c43902011-06-09 20:46:06 -0700281 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
282 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700283 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700284 config->CleanupClientState();
285
286 // |config| instance may be destroyed after this call.
287 config->provider_->UnbindPID(pid);
288}
289
290void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700291 if (child_watch_tag_) {
292 glib_->SourceRemove(child_watch_tag_);
293 child_watch_tag_ = 0;
294 }
295 if (pid_) {
296 glib_->SpawnClosePID(pid_);
297 pid_ = 0;
298 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700299 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700300 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700301 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700302 false);
303 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700304 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700305 false);
306}
307
Darin Petkov50308cd2011-06-01 18:25:07 -0700308} // namespace shill