blob: 4053508bd2959c541979f02361a14a0206f4de52 [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 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) {
Darin Petkova7b89492011-07-27 12:48:17 -0700115 if (!proxy_.get()) {
116 VLOG(2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700117 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700118 }
119}
120
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700121void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700122 const Configuration &configuration) {
123 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700124 if (reason == kReasonFail) {
125 LOG(ERROR) << "Received failure event from DHCP client.";
126 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700127 return;
128 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700129 if (reason != kReasonBound &&
130 reason != kReasonRebind &&
131 reason != kReasonReboot &&
132 reason != kReasonRenew) {
133 LOG(WARNING) << "Event ignored.";
134 return;
135 }
136 IPConfig::Properties properties;
137 CHECK(ParseConfiguration(configuration, &properties));
138 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700139}
140
Darin Petkovd1b715b2011-06-02 21:21:22 -0700141bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700142 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700143
Paul Stewartd32f4842012-01-11 16:08:13 -0800144 vector<char *> args;
145 args.push_back(const_cast<char *>(kDHCPCDPath));
146 args.push_back(const_cast<char *>("-B")); // foreground
147 args.push_back(const_cast<char *>(device_name().c_str()));
148 if (!request_hostname_.empty()) {
149 args.push_back(const_cast<char *>("-h")); // request hostname
150 args.push_back(const_cast<char *>(request_hostname_.c_str()));
151 }
152 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700153 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700154
Darin Petkov98dd6a02011-06-10 15:12:57 -0700155 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700156 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800157 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700158 envp,
159 G_SPAWN_DO_NOT_REAP_CHILD,
160 NULL,
161 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700162 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700163 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700164 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
165 return false;
166 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700167 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700168 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700169 CHECK(!child_watch_tag_);
170 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700171 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700172}
173
Darin Petkov92c43902011-06-09 20:46:06 -0700174void DHCPConfig::Stop() {
175 if (pid_) {
176 VLOG(2) << "Terminating " << pid_;
Thieu Le94eed562012-02-21 15:57:29 -0800177 if (kill(pid_, SIGTERM) < 0) {
178 PLOG(ERROR);
179 return;
180 }
181 pid_t ret;
182 int num_iterations =
183 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
184 for (int count = 0; count < num_iterations; ++count) {
185 ret = waitpid(pid_, NULL, WNOHANG);
186 if (ret == pid_ || ret == -1)
187 break;
188 usleep(kDHCPCDExitPollMilliseconds * 1000);
189 if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd.
190 kill(pid_, SIGKILL);
191 }
192 if (ret != pid_)
193 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700194 }
195}
196
Darin Petkov98dd6a02011-06-10 15:12:57 -0700197bool DHCPConfig::Restart() {
198 // Check to ensure that this instance doesn't get destroyed in the middle of
199 // this call. If stopping a running client while there's only one reference to
200 // this instance, we will end up destroying it when the PID is unbound from
201 // the Provider. Since the Provider doesn't invoke Restart, this would mean
202 // that Restart was erroneously executed through a bare reference.
203 CHECK(!pid_ || !HasOneRef());
204 Stop();
205 if (pid_) {
206 provider_->UnbindPID(pid_);
207 }
208 CleanupClientState();
209 return Start();
210}
211
Darin Petkove7cb7f82011-06-03 13:21:51 -0700212string DHCPConfig::GetIPv4AddressString(unsigned int address) {
213 char str[INET_ADDRSTRLEN];
214 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
215 return str;
216 }
217 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
218 return "";
219}
220
221bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
222 IPConfig::Properties *properties) {
223 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700224 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700225 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700226 for (Configuration::const_iterator it = configuration.begin();
227 it != configuration.end(); ++it) {
228 const string &key = it->first;
229 const DBus::Variant &value = it->second;
230 VLOG(2) << "Processing key: " << key;
231 if (key == kConfigurationKeyIPAddress) {
232 properties->address = GetIPv4AddressString(value.reader().get_uint32());
233 if (properties->address.empty()) {
234 return false;
235 }
236 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700237 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700238 } else if (key == kConfigurationKeyBroadcastAddress) {
239 properties->broadcast_address =
240 GetIPv4AddressString(value.reader().get_uint32());
241 if (properties->broadcast_address.empty()) {
242 return false;
243 }
244 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700245 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700246 if (routers.empty()) {
247 LOG(ERROR) << "No routers provided.";
248 return false;
249 }
250 properties->gateway = GetIPv4AddressString(routers[0]);
251 if (properties->gateway.empty()) {
252 return false;
253 }
254 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700255 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700256 for (vector<unsigned int>::const_iterator it = servers.begin();
257 it != servers.end(); ++it) {
258 string server = GetIPv4AddressString(*it);
259 if (server.empty()) {
260 return false;
261 }
262 properties->dns_servers.push_back(server);
263 }
264 } else if (key == kConfigurationKeyDomainName) {
265 properties->domain_name = value.reader().get_string();
266 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700267 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700268 } else if (key == kConfigurationKeyMTU) {
269 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100270 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700271 properties->mtu = mtu;
272 }
273 } else {
274 VLOG(2) << "Key ignored.";
275 }
276 }
277 return true;
278}
279
Darin Petkov92c43902011-06-09 20:46:06 -0700280void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
281 VLOG(2) << "pid " << pid << " exit status " << status;
282 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
283 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700284 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700285 config->CleanupClientState();
286
287 // |config| instance may be destroyed after this call.
288 config->provider_->UnbindPID(pid);
289}
290
291void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700292 if (child_watch_tag_) {
293 glib_->SourceRemove(child_watch_tag_);
294 child_watch_tag_ = 0;
295 }
296 if (pid_) {
297 glib_->SpawnClosePID(pid_);
298 pid_ = 0;
299 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700300 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700301 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700302 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700303 false);
304 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700305 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700306 false);
307}
308
Darin Petkov50308cd2011-06-01 18:25:07 -0700309} // namespace shill